OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Dec 11, 2018 10:52 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: What is wrong with this shared object?
PostPosted: Thu Dec 06, 2018 7:09 pm 
Offline
Member
Member
User avatar

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 614
I'm still working on my linker, it's now trying to output ELF64 shared object. I have a really odd test case for a shared library:

Code:
.section .text
.globl myAdd

.func myAdd
   mov rax, rdi
   add rax, rsi
   mov rcx, QWORD PTR testVar@GOTPCREL[rip]
   add rax, QWORD PTR [rcx]
   push rax
   mov rdi, rax
   call DWORD OFFSET dummy@PLT
   pop rax
   ret
.end myAdd


I test it with this C code:

Code:
#include <stdio.h>

int testVar = 67;

int myAdd(int a, int b);

void dummy(int x)
{
   printf("dummy called with argument %d\n", x);
};

int main()
{
   int result = myAdd(2, 7);
   printf("Result of addition: %d\n", result);
   return 0;
};


I let my own assembler+linked create the shared object, named libhello.so. I then use GCC to compile the C file and link it against my shared library: no problem. But when trying to run it:

Code:
./dummy: symbol lookup error: ./dummy: undefined symbol: myAdd


readelf, however, disagrees:

Code:
mariusz@mariusz-pc:~/Madd/CompTools/comptools-build$ readelf -d libhello.so

Dynamic section at offset 0x20f8 contains 13 entries:
  Tag        Type                         Name/Value
0x0000000000000005 (STRTAB)             0x11c8
0x000000000000000a (STRSZ)              41 (bytes)
0x000000000000000e (SONAME)             Library soname: [libhello.so]
0x0000000000000003 (PLTGOT)             0x1010
0x0000000000000002 (PLTRELSZ)           24 (bytes)
0x0000000000000014 (PLTREL)             RELA
0x0000000000000017 (JMPREL)             0x10c0
0x0000000000000007 (RELA)               0x10a8
0x0000000000000008 (RELASZ)             24 (bytes)
0x0000000000000009 (RELAENT)            24 (bytes)
0x0000000000000006 (SYMTAB)             0x1030
0x000000000000000b (SYMENT)             24 (bytes)
0x0000000000000000 (NULL)               0x0
mariusz@mariusz-pc:~/Madd/CompTools/comptools-build$ readelf -s libhello.so

Symbol table '.symtab' contains 16 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 .dynstr
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 .dynamic
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 .plt
     4: 0000000000000000     0 SECTION LOCAL  DEFAULT    7 .plt.rela
     5: 0000000000000000     0 SECTION LOCAL  DEFAULT    8 .got.rela
     6: 0000000000000000     0 SECTION LOCAL  DEFAULT    9 .dynsym
     7: 0000000000000000     0 SECTION LOCAL  DEFAULT   10 .got.plt
     8: 0000000000000000     0 SECTION LOCAL  DEFAULT   11 .got
     9: 0000000000000000     0 SECTION LOCAL  DEFAULT   12 .bss
    10: 0000000000000000     0 SECTION LOCAL  DEFAULT   13 .data
    11: 0000000000000000     0 SECTION LOCAL  DEFAULT   14 .text
    12: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  ABS dummy
    13: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  ABS testVar
    14: 0000000000000000    22 FUNC    GLOBAL DEFAULT   14 myAdd
    15: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT   14 __begin

Symbol table '.dynsym' contains 5 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 OBJECT  GLOBAL DEFAULT  UND dummy
     2: 0000000000000000     0 OBJECT  GLOBAL DEFAULT  UND testVar
     3: 0000000000000000    22 FUNC    GLOBAL DEFAULT   14 myAdd
     4: 0000000000000000     0 OBJECT  GLOBAL DEFAULT   14 __begin


Symbol myAdd is clearly defined in the dynamic symbol table.

I attached the library if anyone wants to inspect any further.

Any reason why the dynamic loader (this is linux x86_64) can't find myAdd() ?

UPDATE: Tried including a DT_HASH table:

Code:
mariusz@mariusz-pc:~/Madd/CompTools/comptools-build$ readelf -D -s libhello.so

Symbol table for image:
  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name
    3   4: 0000000000000000    22 FUNC    GLOBAL DEFAULT  15 myAdd
    1   9: 0000000000000000     0 OBJECT  GLOBAL DEFAULT UND dummy
    2  18: 0000000000000000     0 OBJECT  GLOBAL DEFAULT UND testVar
    4  30: 0000000000000000     0 OBJECT  GLOBAL DEFAULT  15 __begin
mariusz@mariusz-pc:~/Madd/CompTools/comptools-build$ LD_LIBRARY_PATH=. ./dummy
./dummy: symbol lookup error: ./dummy: undefined symbol: myAdd


Still does not solve it :/


Attachments:
libhello.zip [932 Bytes]
Downloaded 1 time

_________________
Glidix: An x86_64 POSIX-compliant operating system, aiming to be as optimized as possible, especially in graphics.
https://github.com/madd-games/glidix
Top
 Profile  
 
 Post subject: Re: What is wrong with this shared object?
PostPosted: Fri Dec 07, 2018 7:07 am 
Offline
Member
Member

Joined: Wed Jan 08, 2014 8:41 am
Posts: 100
Location: Moscow, Russia
Code:
$ LD_LIBRARY_PATH=. ./1 
./1: error while loading shared libraries: libhello.so: failed to map segment from shared object


You seem to have a problem with your program headers:

Code:
Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  NULL           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000         0
  LOAD           0x0000000000000000 0x0000000000001000 0x0000000000001000
                 0x0000000000000000 0x0000000000000000  RW     1000
  LOAD           0x0000000000001000 0x0000000000001000 0x0000000000001000
                 0x0000000000000000 0x0000000000000000  RW     1000
  LOAD           0x0000000000001000 0x0000000000000000 0x0000000000000000
                 0x0000000000000016 0x0000000000000016  R E    1000
  LOAD           0x0000000000002000 0x0000000000001000 0x0000000000001000
                 0x00000000000001f1 0x00000000000001f1  RWE    1000
  DYNAMIC        0x00000000000020f8 0x00000000000010f8 0x00000000000010f8
                 0x00000000000000d0 0x00000000000000d0  RWE    1000


I see three issues here: (1) the first two LOAD segments are both zero size; (2) the other two LOAD segments overlap the first two; (3) the DYNAMIC segment is misaligned -- the alignment is set to 1000 but its address is not page-aligned.


Top
 Profile  
 
 Post subject: Re: What is wrong with this shared object?
PostPosted: Fri Dec 07, 2018 7:46 am 
Offline
Member
Member
User avatar

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 614
Icee wrote:
Code:
$ LD_LIBRARY_PATH=. ./1 
./1: error while loading shared libraries: libhello.so: failed to map segment from shared object


You seem to have a problem with your program headers:

Code:
Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  NULL           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000         0
  LOAD           0x0000000000000000 0x0000000000001000 0x0000000000001000
                 0x0000000000000000 0x0000000000000000  RW     1000
  LOAD           0x0000000000001000 0x0000000000001000 0x0000000000001000
                 0x0000000000000000 0x0000000000000000  RW     1000
  LOAD           0x0000000000001000 0x0000000000000000 0x0000000000000000
                 0x0000000000000016 0x0000000000000016  R E    1000
  LOAD           0x0000000000002000 0x0000000000001000 0x0000000000001000
                 0x00000000000001f1 0x00000000000001f1  RWE    1000
  DYNAMIC        0x00000000000020f8 0x00000000000010f8 0x00000000000010f8
                 0x00000000000000d0 0x00000000000000d0  RWE    1000


I see three issues here: (1) the first two LOAD segments are both zero size; (2) the other two LOAD segments overlap the first two; (3) the DYNAMIC segment is misaligned -- the alignment is set to 1000 but its address is not page-aligned.


OK, I fixed that issue and also made the symbol addresses nonzero. It can now resolve the references, although it jumps to a NULL pointer so segfaults. Probably a PLT issue.

_________________
Glidix: An x86_64 POSIX-compliant operating system, aiming to be as optimized as possible, especially in graphics.
https://github.com/madd-games/glidix


Top
 Profile  
 
 Post subject: Re: What is wrong with this shared object?
PostPosted: Sat Dec 08, 2018 7:39 pm 
Offline
Member
Member
User avatar

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 614
OK, I tried fixing all the issues but there is stil la problem. Behold, the relocation tables:

Code:
mariusz@mariusz-pc:~/Madd/CompTools/comptools-build$ readelf -r libhello.so

Relocation section '.plt.rela' at offset 0x2160 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000002028  000100000007 R_X86_64_JUMP_SLO 0000000000000000 dummy + 0

Relocation section '.got.rela' at offset 0x2148 contains 1 entry:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000002008  000200000006 R_X86_64_GLOB_DAT 0000000000000000 testVar + 0


THe code accesses 'testVar' with no issue. But when trying to call dummy(), it ends up calling the NULL pointer.

I debugged it with GDB. I varified that 0x2028 is indeed the offset from which it fetches the dummy() pointer in the PLT code. THe PLT code goes like this:

1. The first JMP successfully jumps to the next instructions, as expected.
2. It manages to get all the way to the call to the dynamic linker.
3. When the dynamic linker resolves the reference, it decides that dummy() has value 0, and calls that

but why?

I attached the new libhello.so ...


Attachments:
libhello.zip [913 Bytes]
Not downloaded yet

_________________
Glidix: An x86_64 POSIX-compliant operating system, aiming to be as optimized as possible, especially in graphics.
https://github.com/madd-games/glidix
Top
 Profile  
 
 Post subject: Re: What is wrong with this shared object?
PostPosted: Sat Dec 08, 2018 7:45 pm 
Offline
Member
Member
User avatar

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 614
Whoops, sorry! One of the addresses didn't work correctly. I resolved it now.

_________________
Glidix: An x86_64 POSIX-compliant operating system, aiming to be as optimized as possible, especially in graphics.
https://github.com/madd-games/glidix


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: Google [Bot] and 3 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