OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 10:49 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: ELF64 Shared Libraries
PostPosted: Tue Feb 17, 2015 1:06 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
I was looking at the ELF64 specification in hope to find out how to dynamically link with a shared library, i understand there is a "dynamic" program header, but it seems like GCC is omitting it from the libc.so when building it (there are no program headers at all). And I've found multiple versions of the ELF64 spec on the internet and cannot seem to find which one is the most accurate.

Does anyone have a good explanation or some sample code to do this?


Top
 Profile  
 
 Post subject: Re: ELF64 Shared Libraries
PostPosted: Wed Feb 18, 2015 3:32 am 
Offline
Member
Member

Joined: Wed Jan 08, 2014 8:41 am
Posts: 100
Location: Moscow, Russia
mariuszp wrote:
I was looking at the ELF64 specification in hope to find out how to dynamically link with a shared library, i understand there is a "dynamic" program header, but it seems like GCC is omitting it from the libc.so when building it (there are no program headers at all). And I've found multiple versions of the ELF64 spec on the internet and cannot seem to find which one is the most accurate.

Does anyone have a good explanation or some sample code to do this?

If there are no program headers, this usually means that you are looking at an object file which is subject to additional (compile-time, not run-time) linking before a program or a shared library is built. Probably you are just inspecting the wrong file.

Here's a sample output for /lib/libc.so.6:

Code:
[12:27] icee@earth ~ $ readelf -l /lib/libc.so.6

Elf file type is DYN (Shared object file)
Entry point 0x16d20
There are 10 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00000034 0x00000034 0x00140 0x00140 R E 0x4
  INTERP         0x12bc10 0x0012bc10 0x0012bc10 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /lib/ld-linux.so.2]
  LOAD           0x000000 0x00000000 0x00000000 0x141a48 0x141a48 R E 0x1000
  LOAD           0x1421c0 0x001431c0 0x001431c0 0x027bc 0x057a8 RW  0x1000
  DYNAMIC        0x143d7c 0x00144d7c 0x00144d7c 0x000f0 0x000f0 RW  0x4
  NOTE           0x000174 0x00000174 0x00000174 0x00044 0x00044 R   0x4
  TLS            0x1421c0 0x001431c0 0x001431c0 0x00008 0x00040 R   0x4
  GNU_EH_FRAME   0x12bc24 0x0012bc24 0x0012bc24 0x0319c 0x0319c R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x4
  GNU_RELRO      0x1421c0 0x001431c0 0x001431c0 0x01e40 0x01e40 R   0x1


The dynamic section can be dumped with "readelf -d <FILESPEC>". The base ELF64 spec is this one, however you should also consider machine-specific supplements for your targets.


Top
 Profile  
 
 Post subject: Re: ELF64 Shared Libraries
PostPosted: Wed Feb 25, 2015 12:26 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
When I build a C library using my OS cross-compiler, it is marked as DYN but has no program headers or dynamic section. The LINK_SPEC in GCC code in glidix.h is:

Code:
#undef LINK_SPEC
#define LINK_SPEC "\
  %{shared:-shared} \
  %{!shared: \
    %{!static: \
      %{rdynamic:-export-dynamic} \
      -dynamic-linker /lib/glidixld.so} \
      %{static:-static}}"


Without this LINK_SPEC it simply marks the library as executable, ignoring the -shared flag, but linux.h does not seem to have LINK_SPEC at all...

Is anything wrong here?


Top
 Profile  
 
 Post subject: Re: ELF64 Shared Libraries
PostPosted: Wed Feb 25, 2015 1:06 pm 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
Does passing '-Wl,-shared' rather than '-shared' to gcc work?

Regards,
John.

_________________
Tysos | rpi-boot


Top
 Profile  
 
 Post subject: Re: ELF64 Shared Libraries
PostPosted: Wed Feb 25, 2015 1:43 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
jnc100 wrote:
Does passing '-Wl,-shared' rather than '-shared' to gcc work?

Regards,
John.


Nope, same effect. And passing -v tells me that -shared is passed to the linker anyway. And it does mark the output as DYN, but without program headers, or a dynamic section, and also no symbol table. libc.so is actually made like this:

Code:
x86_64-glidix-gcc -shared -o out/lib/libc.so out/lib/libc.a


Could this be a reason?


Top
 Profile  
 
 Post subject: Re: ELF64 Shared Libraries
PostPosted: Fri Feb 27, 2015 7:21 pm 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
Linking a static library with the '-shared' option to try and create a shared library is not the way to do it. You should recompile the object files from source with the -fPIC option and then link them together with -shared to create the shared library.

If you get errors then try with a standard x86_64-elf-gcc first to see if it is an error with your port.

Regards,
John.

_________________
Tysos | rpi-boot


Top
 Profile  
 
 Post subject: Re: ELF64 Shared Libraries
PostPosted: Sat Feb 28, 2015 1:52 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
Oops, looks like creating it from a static library was the problem. :P


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 18 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