Bug 25207 - ld: support --image-base= for elf (and -Ttext-segment -z separate-code strangeness)
Summary: ld: support --image-base= for elf (and -Ttext-segment -z separate-code strang...
Status: UNCONFIRMED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-11-19 23:26 UTC by Fangrui Song
Modified: 2019-11-19 23:26 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2019-11-19 23:26:29 UTC
% cat a.c
int main() {}

% gcc -fuse-ld=bfd a.c -Wl,-Ttext-segment,0x300000 -z noseparate-code -o a; readelf -Wl a
...
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000300040 0x0000000000300040 0x0001f8 0x0001f8 R   0x8
  INTERP         0x000238 0x0000000000300238 0x0000000000300238 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000300000 0x0000000000300000 0x0007a8 0x0007a8 R E 0x1000
  LOAD           0x000e18 0x0000000000301e18 0x0000000000301e18 0x000210 0x000218 RW  0x1000
...

When -z separate-code is specified, there will be two R PT_LOAD. Notably, -Ttext-segment specifies the address of the first R, instead of the text segment (RX).
Or we may argue that the traditional "text segment" includes both the first R and the RX...

% gcc -fuse-ld=bfd a.c -Wl,-Ttext-segment,0x300000 -z separate-code -o a; readelf -Wl a
...
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000300040 0x0000000000300040 0x000268 0x000268 R   0x8
  INTERP         0x0002a8 0x00000000003002a8 0x00000000003002a8 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000300000 0x0000000000300000 0x000530 0x000530 R   0x1000
  LOAD           0x001000 0x0000000000301000 0x0000000000301000 0x00019d 0x00019d R E 0x1000
  LOAD           0x002000 0x0000000000302000 0x0000000000302000 0x000148 0x000148 R   0x1000
  LOAD           0x002e18 0x0000000000303e18 0x0000000000303e18 0x000210 0x000218 RW  0x1000
...

As a better name for specifying the base address, we can introduce a new elf option --image-base=0x300000 (it exists in pe). The LLVM linker lld has supported --image-base since 2016-07-12 (https://reviews.llvm.org/D22116).