2

i try to mix files . For example , if i have file1.o and have file2.o , all of them don't have main function , then le1_file2.o is the result of those files . So i have used 2 things :

Using of linker and makefile:

linker.ld:

OUTPUT_FORMAT(elf32-i386)

SECTIONS
{
    .text : {*(.text)}
    .data : {*(.data)}
    .rodata : {*(.rodata)}
    .bss :
    {
        *(COMMON)
        *(.bss)
    }    
    end = .; _end = .; __end = .;
}

Makefile:

CC =gcc
OBJ = file1.o file2.o

all : file1_file2.o

file1_file2.o: $(OBJ)
    ld -m elf_i386 --oformat=binary -Tlinker.ld $^ -o $@

%.o : %.c
    @$(CC) -o $@ -c $< -m32 -g -ffreestanding -fno-PIC -fno-stack-protector -Werror

Using only a makefile:

Makefile:

 CC =gcc
 OBJ = file1.o file2.o

 all : file1_file2.o

 file1_file2.o: $(OBJ)
     $(CC) $^ -o $@ -m32 -g -ffreestanding -fno-PIC -fno-stack-protector -Werror

 %.o : %.c
     @$(CC) -o $@ -c $< -m32 -g -ffreestanding -fno-PIC -fno-stack-protector -Werror
ledoux
  • 91
  • 9
  • 4
    There's no question. – Fiddling Bits Dec 20 '19 at 23:43
  • And gcc is complaining you have no main ? If that's the case you probably need to read about static library or dynamic library. It's easy and you don't need a main. – BobRun Dec 21 '19 at 00:53
  • You have issues with your Makefile and linker scripts. First you need to figure out where in memory you are loading this. I assume thisn't isn't Multiboot and you have created your own custom bootloader to load this kernel? – Michael Petch Dec 21 '19 at 01:00
  • I will assume you are new to Makefiles? – Michael Petch Dec 21 '19 at 01:12
  • 1
    What is the problem or question? – jww Dec 21 '19 at 01:18
  • You might need the `-r` option (or something more ornate, such as `-Wl,-r`) to combine two object files into one. – Jonathan Leffler Dec 21 '19 at 05:13
  • My question is that : how can i mix these 2 files , which have no main function ? – ledoux Dec 21 '19 at 07:04
  • If you want to combine two object files then the proper way to do so would be to create a static library out of them. – user7860670 Dec 21 '19 at 08:26
  • @MichaelPetch for exemple if i have exception_mananger.o and interupt_manager.o and want to mix all of them to have idt_manager.o . How can i link the interrupt file and ecxeption file ? We see that , i don't want to load in memory , just for mix it – ledoux Dec 21 '19 at 08:32

1 Answers1

1

i have found the solution by using

ar cr Archive all of the object (.o) files into one static library (.a) file. https://helpmanual.io/help/gcc-ar/

Using a static library means only one object file need be pulled in during the linking phase. This contrasts having the compiler pull in multiple object files (one for each function etc) during linking. This means instead of having to look for each entity on separate parts of the project or object file, the program need only reference a single single archived object (.a) file that has ordered the entities together. As a consequence of having one ordered object file, the program linking this library can load much faster.

supported targets: elf64-x86-64 elf32-i386 elf32-iamcu elf32-x86-64 a.out-i386-linux pei-i386 pei-x86-64 elf64-l1om elf64-k1om elf64-little elf64-big elf32-little elf32-big pe-x86-64 pe-bigobj-x86-64 pe-i386 plugin srec symbolsrec verilog tekhex binary ihex Report bugs to http://www.sourceware.org/bugzilla/

So :

ar cr first_second.a first.o second.o

thank you for all

ledoux
  • 91
  • 9
  • I could understand doing this if you were creating multiple programs that required a static library, but if this is for OS Development why not just add the object files separately to your final linking stage that creates the kernel? Just curious what is gained by you doing this. – Michael Petch Dec 22 '19 at 18:37
  • yes of course , i want to create multiple programs using static library . Using a static library means only one object file need be pulled in during the linking phase, i just need only reference a single archived object (.a) file that has ordered the entities together. – ledoux Dec 22 '19 at 21:48