Search code examples
linuxelfdynamic-linking

Is the ELF Section Header Table necessary for Dynamically Linked ELF file?


I have been reading about loading and linking over the past couple days, and I have read countless times that the Section Header in an ELF file is not necessary for an executable. I understand how this could be true for a statically linked executable, but I am wondering if this still holds for dynamically linked executables.

enter image description here

As seen in this image, the Section Header Table is used to access the relocation sections, along with some other sections. These sections are then used in the dynamic linking process. This is also stated in the article, where I found this image. Therefore, I am confused if the statement about not needing Section Headers still holds, or if it only applies to statically linked executables.

Any insights would be appreciated. Thank you.


Solution

  • I am wondering if this still holds for dynamically linked executables.

    Yes: sections are not used for anything during dynamic loading.

    As seen in this image, the Section Header Table is used to access the relocation sections

    That is not true. To find relocations, the PT_DYNAMIC segment (containing the .dynamic section) is used. For example:

    readelf -d /bin/date
    
    Dynamic section at offset 0x1cdb8 contains 26 entries:
      Tag        Type                         Name/Value
     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
     0x000000000000000c (INIT)               0x3000
     0x000000000000000d (FINI)               0x15140
     0x0000000000000019 (INIT_ARRAY)         0x1c1f0
     0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
     0x000000000000001a (FINI_ARRAY)         0x1c1f8
     0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
     0x000000006ffffef5 (GNU_HASH)           0x3a0
     0x0000000000000005 (STRTAB)             0xbc8
     0x0000000000000006 (SYMTAB)             0x400
     0x000000000000000a (STRSZ)              977 (bytes)
     0x000000000000000b (SYMENT)             24 (bytes)
     0x0000000000000015 (DEBUG)              0x0
     0x0000000000000003 (PLTGOT)             0x1cfe8
     0x0000000000000002 (PLTRELSZ)           1584 (bytes)
     0x0000000000000014 (PLTREL)             RELA
     0x0000000000000017 (JMPREL)             0x2238
     0x0000000000000007 (RELA)               0x10e0
     0x0000000000000008 (RELASZ)             4440 (bytes)
     0x0000000000000009 (RELAENT)            24 (bytes)
     0x000000006ffffffb (FLAGS_1)            Flags: PIE
     0x000000006ffffffe (VERNEED)            0x1040
     0x000000006fffffff (VERNEEDNUM)         1
     0x000000006ffffff0 (VERSYM)             0xf9a
     0x000000006ffffff9 (RELACOUNT)          171
     0x0000000000000000 (NULL)               0x0
    

    Here, the DT_PLTGOT, DT_PLTRELSZ, DT_RELA, DT_RELASZ etc. are sufficient to find the necessary relocation tables.