[Quesion] What makes the linker contains a PHDR?

605 views
Skip to first unread message

Chen Wang

unread,
Dec 15, 2020, 4:10:18 AM12/15/20
to android-llvm
Hello, aosp llvm team,
I'm investigating the code of bionic/linker. I find the linker built with aosp clang++ would contain a PHDR. Who can tell me aosp-llvm does what and how it inserts a PHDR into the linker, which I think is also a shared object file? When I use gcc to create a shared object, I can not see the PHDR. I can only see PHDR in an executable.

Thanks in adv.

Following is what I see from aosp build:
```
$ prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-readelf -l out/soong/.intermediates/bionic/linker/linker/android_arm_armv8-a_core/unstripped/linker

Elf file type is DYN (Shared object file)
Entry point 0x317d8
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   0x4
  LOAD           0x000000 0x00000000 0x00000000 0x1ceac 0x1ceac R   0x1000
  LOAD           0x01d000 0x0001d000 0x0001d000 0x96ba8 0x96ba8 R E 0x1000
  LOAD           0x0b4000 0x000b4000 0x000b4000 0x046a0 0x0d5ec RW  0x1000
  DYNAMIC        0x0b81e8 0x000b81e8 0x000b81e8 0x00070 0x00070 RW  0x4
  GNU_RELRO      0x0b5000 0x000b5000 0x000b5000 0x036a0 0x04000 R   0x1
  GNU_EH_FRAME   0x01c2d4 0x0001c2d4 0x0001c2d4 0x002f4 0x002f4 R   0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0
  NOTE           0x000174 0x00000174 0x00000174 0x00020 0x00020 R   0x4
  EXIDX          0x000194 0x00000194 0x00000194 0x03ce8 0x03ce8 R   0x4

 Section to Segment mapping:
  Segment Sections...
   00
   01     .note.gnu.build-id .ARM.exidx .dynsym .gnu.hash .dynstr .relr.dyn .rodata .ARM.extab .eh_frame_hdr .eh_frame
   02     .text
   03     .data .data.rel.ro .init_array .dynamic .got .bss
   04     .dynamic
   05     .data.rel.ro .init_array .dynamic .got
   06     .eh_frame_hdr
   07
   08     .note.gnu.build-id
   09     .ARM.exidx
```

Stephen Hines

unread,
Dec 16, 2020, 5:39:50 AM12/16/20
to Chen Wang, android-llvm
PHDR refers to a program header. https://man7.org/linux/man-pages/man5/elf.5.html has some further information about ELF sections and how they're described/defined. http://blog.k3170makan.com/2018/09/introduction-to-elf-format-part-ii.html also has some description of this. For an executable or shared object, the actual program header table (i.e. all that information displayed about the "Program Headers" above) gets mapped into memory as a readable section. Clang appears to do this for shared objects and executables, but regular .o object files do not contain a PHDR entry, because they aren't loadable anyways.

As for why you're only seeing this for executables, I actually think it might have something to do with what linker you're invoking. LLD seems to produce the PHDR entry for both shared objects and executables, while ld.bfd only does so for executables.

Thanks.
Steve

--
You received this message because you are subscribed to the Google Groups "android-llvm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-llvm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-llvm/f50bfda0-844d-4f1d-9e4f-78192a769ef6n%40googlegroups.com.

Chen Wang

unread,
Dec 16, 2020, 7:29:10 PM12/16/20
to android-llvm
Thank you Stephen, I verified and your comments is correct.
>  LLD seems to produce the PHDR entry for both shared objects and executables, while ld.bfd only does so for executables.  

Reply all
Reply to author
Forward
0 new messages