Skip to content

Instantly share code, notes, and snippets.

@MaskRay
Last active January 26, 2024 01:02
Show Gist options
  • Save MaskRay/06d3f06ba5a54af6d8a1deb739c9d0da to your computer and use it in GitHub Desktop.
Save MaskRay/06d3f06ba5a54af6d8a1deb739c9d0da to your computer and use it in GitHub Desktop.
lld's -z separate-code
% echo 'int main(){}' > a.cc
% clang -fuse-ld=lld a.cc -z separate-code
% readelf -WSl a.out
There are 30 section headers, starting at offset 0x2868:
Section Headers:
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[ 0] NULL 0000000000000000 000000 000000 00 0 0 0
[ 1] .interp PROGBITS 00000000000002a8 0002a8 00001c 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 00000000000002c4 0002c4 000020 00 A 0 0 4
[ 3] .dynsym DYNSYM 00000000000002e8 0002e8 000090 18 A 7 1 8
[ 4] .gnu.version VERSYM 0000000000000378 000378 00000c 02 A 3 0 2
[ 5] .gnu.version_r VERNEED 0000000000000384 000384 000030 00 A 7 1 4
[ 6] .gnu.hash GNU_HASH 00000000000003b8 0003b8 00001c 00 A 3 0 8
[ 7] .dynstr STRTAB 00000000000003d4 0003d4 000088 00 A 0 0 1
[ 8] .rela.dyn RELA 0000000000000460 000460 0000c0 18 A 3 0 8
[ 9] .rela.plt RELA 0000000000000520 000520 000018 18 AI 3 24 8
[10] .rodata PROGBITS 0000000000000538 000538 000004 04 AM 0 0 4
[11] .eh_frame_hdr PROGBITS 000000000000053c 00053c 00001c 00 A 0 0 4
[12] .eh_frame PROGBITS 0000000000000558 000558 000068 00 A 0 0 8
[13] .text PROGBITS 0000000000001000 001000 0000f8 00 AX 0 0 16
[14] .init PROGBITS 00000000000010f8 0010f8 000017 00 AX 0 0 4
[15] .fini PROGBITS 0000000000001110 001110 000009 00 AX 0 0 4
[16] .plt PROGBITS 0000000000001120 001120 000020 00 AX 0 0 16
[17] .fini_array FINI_ARRAY 0000000000002000 002000 000008 08 WA 0 0 8
[18] .init_array INIT_ARRAY 0000000000002008 002008 000008 08 WA 0 0 8
[19] .dynamic DYNAMIC 0000000000002010 002010 0001a0 10 WA 7 0 8
[20] .got PROGBITS 00000000000021b0 0021b0 000028 00 WA 0 0 8
[21] .relro_padding NOBITS 00000000000021d8 0021d8 000e28 00 WA 0 0 1
[22] .data PROGBITS 00000000000031d8 0021d8 000010 00 WA 0 0 8
[23] .tm_clone_table PROGBITS 00000000000031e8 0021e8 000000 00 WA 0 0 8
[24] .got.plt PROGBITS 00000000000031e8 0021e8 000020 00 WA 0 0 8
[25] .bss NOBITS 0000000000003208 002208 000001 00 WA 0 0 1
[26] .comment PROGBITS 0000000000000000 002208 0000bf 01 MS 0 0 1
[27] .symtab SYMTAB 0000000000000000 0022c8 0002d0 18 29 20 8
[28] .shstrtab STRTAB 0000000000000000 002598 000114 00 0 0 1
[29] .strtab STRTAB 0000000000000000 0026ac 0001ba 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
D (mbind), l (large), p (processor specific)
Elf file type is DYN (Position-Independent Executable file)
Entry point 0x1000
There are 11 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000040 0x0000000000000040 0x0000000000000040 0x000268 0x000268 R 0x8
INTERP 0x0002a8 0x00000000000002a8 0x00000000000002a8 0x00001c 0x00001c R 0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x0005c0 0x0005c0 R 0x1000
LOAD 0x001000 0x0000000000001000 0x0000000000001000 0x000140 0x000140 R E 0x1000
LOAD 0x002000 0x0000000000002000 0x0000000000002000 0x0001d8 0x001000 RW 0x1000
LOAD 0x0021d8 0x00000000000031d8 0x00000000000031d8 0x000030 0x000031 RW 0x1000
DYNAMIC 0x002010 0x0000000000002010 0x0000000000002010 0x0001a0 0x0001a0 RW 0x8
GNU_RELRO 0x002000 0x0000000000002000 0x0000000000002000 0x0001d8 0x001000 R 0x1
GNU_EH_FRAME 0x00053c 0x000000000000053c 0x000000000000053c 0x00001c 0x00001c R 0x4
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0
NOTE 0x0002c4 0x00000000000002c4 0x00000000000002c4 0x000020 0x000020 R 0x4
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .note.ABI-tag .dynsym .gnu.version .gnu.version_r .gnu.hash .dynstr .rela.dyn .rela.plt .rodata .eh_frame_hdr .eh_frame
03 .text .init .fini .plt
04 .fini_array .init_array .dynamic .got .relro_padding
05 .data .tm_clone_table .got.plt .bss
06 .dynamic
07 .fini_array .init_array .dynamic .got .relro_padding
08 .eh_frame_hdr
09
10 .note.ABI-tag
%
% ld.bfd -z relro -z separate-code --verbose | sed -n '1,/===/d;/===/q;p' > a.lds
% clang -fuse-ld=lld a.cc -Wl,-T,a.lds
% readelf -WSl a.out
There are 31 section headers, starting at offset 0x5700:
Section Headers:
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[ 0] NULL 0000000000000000 000000 000000 00 0 0 0
[ 1] .interp PROGBITS 00000000004002e0 0002e0 00001c 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 00000000004002fc 0002fc 000020 00 A 0 0 4
[ 3] .gnu.hash GNU_HASH 0000000000400320 000320 00001c 00 A 4 0 8
[ 4] .dynsym DYNSYM 0000000000400340 000340 000090 18 A 5 1 8
[ 5] .dynstr STRTAB 00000000004003d0 0003d0 000088 00 A 0 0 1
[ 6] .gnu.version VERSYM 0000000000400458 000458 00000c 02 A 4 0 2
[ 7] .gnu.version_r VERNEED 0000000000400464 000464 000030 00 A 5 1 4
[ 8] .rela.dyn RELA 0000000000400498 000498 0000c0 18 A 4 0 8
[ 9] .rela.plt RELA 0000000000400558 000558 000018 18 AI 4 22 8
[10] .init PROGBITS 0000000000401000 001000 000017 00 AX 0 0 4
[11] .plt PROGBITS 0000000000401020 001020 000020 00 AX 0 0 16
[12] .text PROGBITS 0000000000401040 001040 0000f8 00 AX 0 0 16
[13] .fini PROGBITS 0000000000401138 001138 000009 00 AX 0 0 4
[14] .rodata PROGBITS 0000000000402000 002000 000004 04 AM 0 0 4
[15] .eh_frame_hdr PROGBITS 0000000000402004 002004 00001c 00 A 0 0 4
[16] .eh_frame PROGBITS 0000000000402020 002020 000068 00 A 0 0 8
[17] .init_array INIT_ARRAY 0000000000403000 003000 000008 08 WA 0 0 8
[18] .fini_array FINI_ARRAY 0000000000403008 003008 000008 08 WA 0 0 8
[19] .dynamic DYNAMIC 0000000000403010 003010 0001c0 10 WA 5 0 8
[20] .got PROGBITS 00000000004031d0 0031d0 000028 00 WA 0 0 8
[21] .relro_padding NOBITS 00000000004031f8 0031f8 000e08 00 WA 0 0 1
[22] .got.plt PROGBITS 0000000000404000 004000 000020 00 WA 0 0 8
[23] .data PROGBITS 0000000000404020 004020 000010 00 WA 0 0 8
[24] .tm_clone_table PROGBITS 0000000000404030 004030 000000 00 WA 0 0 8
[25] .bss NOBITS 0000000000404030 004030 000008 00 WA 0 0 1
[26] .ldata PROGBITS 0000000000405038 005038 000000 00 WA 0 0 1
[27] .comment PROGBITS 0000000000000000 005038 0000bf 01 MS 0 0 1
[28] .symtab SYMTAB 0000000000000000 0050f8 000318 18 30 20 8
[29] .shstrtab STRTAB 0000000000000000 005410 00011b 00 0 0 1
[30] .strtab STRTAB 0000000000000000 00552b 0001d2 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
D (mbind), l (large), p (processor specific)
Elf file type is DYN (Position-Independent Executable file)
Entry point 0x401040
There are 12 program headers, starting at offset 64
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000040 0x0000000000400040 0x0000000000400040 0x0002a0 0x0002a0 R 0x8
INTERP 0x0002e0 0x00000000004002e0 0x00000000004002e0 0x00001c 0x00001c R 0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x000570 0x000570 R 0x1000
LOAD 0x001000 0x0000000000401000 0x0000000000401000 0x000141 0x000141 R E 0x1000
LOAD 0x002000 0x0000000000402000 0x0000000000402000 0x000088 0x000088 R 0x1000
LOAD 0x003000 0x0000000000403000 0x0000000000403000 0x0001f8 0x001000 RW 0x1000
LOAD 0x004000 0x0000000000404000 0x0000000000404000 0x001038 0x001038 RW 0x1000
DYNAMIC 0x003010 0x0000000000403010 0x0000000000403010 0x0001c0 0x0001c0 RW 0x8
GNU_RELRO 0x003000 0x0000000000403000 0x0000000000403000 0x0001f8 0x001000 R 0x1
GNU_EH_FRAME 0x002004 0x0000000000402004 0x0000000000402004 0x00001c 0x00001c R 0x4
GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0
NOTE 0x0002fc 0x00000000004002fc 0x00000000004002fc 0x000020 0x000020 R 0x4
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt
03 .init .plt .text .fini
04 .rodata .eh_frame_hdr .eh_frame
05 .init_array .fini_array .dynamic .got .relro_padding
06 .got.plt .data .tm_clone_table .bss
07 .dynamic
08 .init_array .fini_array .dynamic .got .relro_padding
09 .eh_frame_hdr
10
11 .note.ABI-tag
% exit
Script done on 2024-01-25 17:02:28-08:00 [COMMAND_EXIT_CODE="0"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment