https://sourceware.org/bugzilla/show_bug.cgi?id=23428
Bug ID: 23428 Summary: ld does not put program headers in a load segment when static linking an executable Product: binutils Version: 2.31 Status: UNCONFIRMED Severity: normal Priority: P2 Component: ld Assignee: unassigned at sourceware dot org Reporter: nszabolcs at gmail dot com Target Milestone: --- simple static linking with is broken with 2.31 ld because it does not put program headers into a load section unless there is some other read only loaded section. $ ld --version GNU ld (GNU Binutils) 2.31.1.20180718 Copyright (C) 2018 Free Software Foundation, Inc. This program is free software; you may redistribute it under the terms of the GNU General Public License version 3 or (at your option) a later version. This program has absolutely no warranty. $ cat t.s .globl _start _start: xor %rdi,%rdi mov $0xe7,%eax syscall $ as -o t.o t.s $ ld -static t.o $ readelf -aW a.out ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x401000 Start of program headers: 64 (bytes into file) Start of section headers: 4320 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 1 Size of section headers: 64 (bytes) Number of section headers: 5 Section header string table index: 4 Section Headers: [Nr] Name Type Address Off Size ES Flg Lk Inf Al [ 0] NULL 0000000000000000 000000 000000 00 0 0 0 [ 1] .text PROGBITS 0000000000401000 001000 00000a 00 AX 0 0 1 [ 2] .symtab SYMTAB 0000000000000000 001010 000090 18 3 2 8 [ 3] .strtab STRTAB 0000000000000000 0010a0 000019 00 0 0 1 [ 4] .shstrtab STRTAB 0000000000000000 0010b9 000021 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), l (large), p (processor specific) There are no section groups in this file. Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align LOAD 0x001000 0x0000000000401000 0x0000000000401000 0x00000a 0x00000a R E 0x1000 Section to Segment mapping: Segment Sections... 00 .text There is no dynamic section in this file. There are no relocations in this file. The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported. Symbol table '.symtab' contains 6 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000401000 0 SECTION LOCAL DEFAULT 1 2: 0000000000401000 0 NOTYPE GLOBAL DEFAULT 1 _start 3: 0000000000402000 0 NOTYPE GLOBAL DEFAULT 1 __bss_start 4: 0000000000402000 0 NOTYPE GLOBAL DEFAULT 1 _edata 5: 0000000000402000 0 NOTYPE GLOBAL DEFAULT 1 _end No version information found in this file. $ ./a.out $ Note that the program headers (starting at 0x400000 + 64) are not in a load segment, so if the executable tries to access it (based on the program header address the kernel passed in auxv) then it will be a runtime fault. (reading program headers is e.g. needed to support tls in static executables) if i add a readonly note section then program headers are correctly loaded: $ ld --build-id -static t.o $ readelf -lW a.out Elf file type is EXEC (Executable file) Entry point 0x401000 There are 3 program headers, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x00010c 0x00010c R 0x1000 LOAD 0x001000 0x0000000000401000 0x0000000000401000 0x00000a 0x00000a R E 0x1000 NOTE 0x0000e8 0x00000000004000e8 0x00000000004000e8 0x000024 0x000024 R 0x4 Section to Segment mapping: Segment Sections... 00 .note.gnu.build-id 01 .text 02 .note.gnu.build-id $ with a 2.30 ld, the program headers are part of the same segment as .text so it is loaded (and executable): $ readelf -lW a.out Elf file type is EXEC (Executable file) Entry point 0x400078 There is 1 program header, starting at offset 64 Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x000082 0x000082 R E 0x200000 Section to Segment mapping: Segment Sections... 00 .text $ -- You are receiving this mail because: You are on the CC list for the bug. _______________________________________________ bug-binutils mailing list bug-binutils@gnu.org https://lists.gnu.org/mailman/listinfo/bug-binutils