OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 3:02 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [SOLVED] Custom LibC and linking issue...
PostPosted: Thu Feb 15, 2018 3:54 am 
Offline
Member
Member

Joined: Tue Feb 28, 2017 10:22 am
Posts: 25
Hi,

I am trying since yesterday to develop my own LibC for my kernel. The big problem I encountered is that when I tried to compile my implementation of lldiv, I started getting linking errors :
Code:
i686-elf-ld -nostdlib -T kernel/link.ld kernel/i386/isr.o kernel/boot/multiboot.o kernel/video/kernel_video.o kernel/i386/irq.o kernel/i386/exception.o kernel/i386/idt.o kernel/i386/pic.o kernel/i386/gdt.o kernel/boot/kernel.o kernel/drivers/serial.o kernel/libc/stdlib.o kernel/libc/string.o -o kernel/kernel.elf
kernel/libc/stdlib.o: In function `lldiv':
stdlib.c:(.text+0x204): undefined reference to `__divmoddi4'

I think that the problem comes from my libgcc as when I'm trying to link with -lgcc I get the following error :
Code:
i686-elf-ld -nostdlib -lgcc -T kernel/link.ld kernel/i386/isr.o kernel/boot/multiboot.o kernel/video/kernel_video.o kernel/i386/irq.o kernel/i386/exception.o kernel/i386/idt.o kernel/i386/pic.o kernel/i386/gdt.o kernel/boot/kernel.o kernel/drivers/serial.o kernel/libc/stdlib.o kernel/libc/string.o -o kernel/kernel.elf
i686-elf-ld: cannot find -lgcc

But when I'm linking with GCC with -lgcc, I don't get the -lgcc error but the other.
I have built the complete i686-elf toolchain (gcc + binutils) with the given commands on the wiki.

Did anyone ever get this kind of error or know how to solve it ?

Thanks,
Ankeraout.

Edit :
As I thought that it was possible that I had made a mistake while building the toolchain, I have just rebuilt it, and it still does not work.


Last edited by Ankeraout on Thu Feb 15, 2018 10:25 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Custom LibC and linking issue...
PostPosted: Thu Feb 15, 2018 7:40 am 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
You should use i686-elf-gcc to perform the link step, rather than ld. If you need to pass extra options to ld (e.g. linker script), you can do so with the -Wl switch. You are recommended to link with -lgcc for all code compiled by gcc as it is allowed to emit calls to that library.

Having said that, I don't believe _divmoddi4 is implemented in libgcc for i686 as it directly maps to an opcode (the unsigned version is however). How is your lldiv function referencing it?

Regards,
John.

_________________
Tysos | rpi-boot


Top
 Profile  
 
 Post subject: Re: Custom LibC and linking issue...
PostPosted: Thu Feb 15, 2018 8:25 am 
Offline
Member
Member

Joined: Tue Feb 28, 2017 10:22 am
Posts: 25
Thanks for your answer.

I've been trying to link with gcc and I still cannot get it to work.

Here is my code for lldiv :
Code:
lldiv_t lldiv(long long numerator, long long denominator) {
   lldiv_t result;

   result.quotient = numerator / denominator;
   result.remainder = numerator % denominator;

   if(numerator >= 0 && result.remainder < 0) {
      result.quotient++;
      result.remainder -= denominator;
   }

   return result;
}


Once compiled, it looks like this :
Code:
.text:080001D0                 public lldiv
.text:080001D0 lldiv           proc near
.text:080001D0
.text:080001D0 var_2C          = dword ptr -2Ch
.text:080001D0 var_28          = dword ptr -28h
.text:080001D0 var_24          = dword ptr -24h
.text:080001D0 var_20          = dword ptr -20h
.text:080001D0 arg_0           = dword ptr  4
.text:080001D0 arg_4           = dword ptr  8
.text:080001D0 arg_8           = dword ptr  0Ch
.text:080001D0 arg_C           = dword ptr  10h
.text:080001D0 arg_10          = dword ptr  14h
.text:080001D0
.text:080001D0                 push    ebp
.text:080001D1                 push    edi
.text:080001D2                 push    esi
.text:080001D3                 push    ebx
.text:080001D4                 sub     esp, 1Ch
.text:080001D7                 mov     eax, [esp+2Ch+arg_C]
.text:080001DB                 mov     edx, [esp+2Ch+arg_10]
.text:080001DF                 mov     ebp, [esp+2Ch+arg_0]
.text:080001E3                 mov     esi, [esp+2Ch+arg_4]
.text:080001E7                 mov     edi, [esp+2Ch+arg_8]
.text:080001EB                 mov     ecx, edx
.text:080001ED                 mov     edx, eax
.text:080001EF                 lea     eax, [esp+2Ch+var_24]
.text:080001F3                 sub     esp, 0Ch
.text:080001F6                 push    eax
.text:080001F7                 mov     [esp+3Ch+var_28], ecx
.text:080001FB                 mov     [esp+3Ch+var_2C], edx
.text:080001FF                 push    ecx
.text:08000200                 push    edx
.text:08000201                 push    edi
.text:08000202                 push    esi
.text:08000203                 call    __divmoddi4
.text:08000208                 add     esp, 20h
.text:0800020B                 mov     ebx, edx
.text:0800020D                 mov     ecx, eax
.text:0800020F                 mov     edx, [esp+2Ch+var_20]
.text:08000213                 mov     eax, [esp+2Ch+var_24]
.text:08000217                 test    edx, edx
.text:08000219                 jns     short loc_800021F
.text:0800021B                 test    edi, edi
.text:0800021D                 jns     short loc_8000240
.text:0800021F
.text:0800021F loc_800021F:                            ; CODE XREF: lldiv+49↑j
.text:0800021F                                         ; lldiv:loc_800024D↓j
.text:0800021F                 mov     [ebp+8], eax
.text:08000222                 mov     [ebp+0], ecx
.text:08000225                 mov     eax, ebp
.text:08000227                 mov     [ebp+4], ebx
.text:0800022A                 mov     [ebp+0Ch], edx
.text:0800022D                 add     esp, 1Ch
.text:08000230                 pop     ebx
.text:08000231                 pop     esi
.text:08000232                 pop     edi
.text:08000233                 pop     ebp
.text:08000234                 retn    4
.text:08000234 ; ---------------------------------------------------------------------------
.text:08000237                 align 10h
.text:08000240
.text:08000240 loc_8000240:                            ; CODE XREF: lldiv+4D↑j
.text:08000240                 add     ecx, 1
.text:08000243                 adc     ebx, 0
.text:08000246                 sub     eax, [esp+2Ch+var_2C]
.text:08000249                 sbb     edx, [esp+2Ch+var_28]
.text:0800024D
.text:0800024D loc_800024D:                            ; DATA XREF: .eh_frame:080002A0↓o
.text:0800024D                                         ; .eh_frame:080002F8↓o ...
.text:0800024D                 jmp     short loc_800021F
.text:0800024D lldiv           endp


Here is the console output :
Code:
i686-elf-gcc -nodefaultlibs -nostdlib -lgcc -static-libgcc -T kernel/link.ld kernel/i386/isr.o kernel/boot/multiboot.o kernel/video/kernel_video.o kernel/i386/irq.o kernel/i386/exception.o kernel/i386/idt.o kernel/i386/pic.o kernel/i386/gdt.o kernel/boot/kernel.o kernel/drivers/serial.o kernel/libc/stdlib.o kernel/libc/string.o -o kernel/kernel.elf
kernel/libc/stdlib.o: In function `lldiv':
stdlib.c:(.text+0x204): undefined reference to `__divmoddi4'
collect2: error: ld returned 1 exit status


I have found __divmoddi4 in gcc's source code in libgcc/libgcc2.c.
I also have found it in the compiled libgcc files with readelf, so I really cannot understand why it does not work.

Best regards,
Ankeraout.


Top
 Profile  
 
 Post subject: Re: Custom LibC and linking issue...
PostPosted: Thu Feb 15, 2018 9:27 am 
Offline
Member
Member

Joined: Sat Jul 02, 2016 7:02 am
Posts: 207
The compiler and the linker both support verbose output.

If ld raises "cannot find -lgcc", its --verbose option should show the places where it did or did not search for the library.

gcc has -v for the compilation process, and Wl,--verbose to pass to the linker.

Also try the compiler option -print-libgcc-file-name to print out the path to libgcc and see that it matches with the expectations. The libgcc wiki article has this bit and some more.

The link says there could be multiple libgcc, and the choise is based on the options.


Top
 Profile  
 
 Post subject: Re: Custom LibC and linking issue...
PostPosted: Thu Feb 15, 2018 10:11 am 
Offline
Member
Member

Joined: Tue Feb 28, 2017 10:22 am
Posts: 25
Thanks to your instructions, I was able to get the libgcc.a file.
When linking directly with ld, there was absolutely no search directory (this is probably why it was not finding -lgcc).
So I added the -L/usr/local/lib/gcc/i686-elf/7.3.0/ option, and now it prints the undefined reference error.
According to the output, this is the file that ld opens.
Now that we know that, I am back to linking with gcc because it seems to find libgcc without specifying the library path.

So the libgcc.a file contains the _divmoddi4.o file, which contains the __divmoddi4 function.
This symbol is referenced in the 1.txt file of libgcc.a.
I can even disassemble the .o and read a disassembled version of the __divmoddi4 function.
But for some reason, the linker still seems unable to find it, even if it outputs this before printing the error :
Code:
attempt to open /usr/local/lib/gcc/i686-elf/7.3.0/libgcc.a succeeded

I've done some testing, and figured out that __clear_cache also cannot be found, even if it is in libgcc.a.
There is definitely something that I am missing here...

EDIT :
Okay, thanks to some Google search and some guys at Stack Overflow, I discovered that the library options must come after the .o files in the linker command line.
I have tried it, and now it works like a charm.

Many thanks for answering this post, and I hope that this solution will help somebody in the future.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 56 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group