Bug 33633 - ld --build-id does not use symtab/strtab content
Summary: ld --build-id does not use symtab/strtab content
Status: NEW
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: 2025-11-16 07:17 UTC by Fangrui Song
Modified: 2025-12-12 04:48 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2025-11-16 07:17:45 UTC
Originally reported at https://lore.kernel.org/linux-perf-users/CAP-5=fUC88TVwnjTuTgU+s=LqqP0xOhNTG5hU27cSbaZRH7Jpg@mail.gmail.com/

$ cat a.s
.text
.global main
.global foo
main:
foo:
       ret
$ cat b.s
.text
.global main
.global bar
main:
bar:
       ret
$ gcc -Wl,--build-id a.s -o a.out
$ gcc -Wl,--build-id b.s -o b.out
$ readelf -n a.out
...
   Build ID: 9dd0371b953db5d72929af5d98552e4ee1043616
...
$ readelf -n b.out
...
   Build ID: 9dd0371b953db5d72929af5d98552e4ee1043616
...


write_build_id calls bfd/elfcode.h:elf_checksum_contents, which hashes the ELF header, program headers, section headers and contents.
However, .symstr .strtab .shstrtab are special ` sec = bfd_section_from_elf_index (abfd, count);` returns NULL for the three sections, causing their contents to be unused in the final build ID.
Comment 1 Sam James 2025-12-08 23:55:24 UTC
There's `special_sections` but only xcoff has that. But there is elf_backend_special_sections, though that doesn't have these either.
Comment 2 Sam James 2025-12-09 00:09:01 UTC
Ah, they're in special_sections_s, and then in special_sections in elf.c (sorry, I was wrong about XCOFF-only).
Comment 3 Zheng Bao 2025-12-12 04:48:17 UTC
The answer from Chatgpt:

All SHF_ALLOC sections
These are sections that will be loaded into memory at runtime, such as:
.text
.rodata
.data
.got
.plt
.init
.fini
.ctors / .dtors
Any custom sections marked SHF_ALLOC

Sections not used for build-id
These are ignored:
.note.gnu.build-id itself
Debugging sections: .debug_*, .gdb_index, .debug_line, etc.
.comment
.symtab, .strtab, .dynsym
.rela.*, .rel.*
Any non-ALLOC sections
Section names reserved for toolchain metadata

And Chatgpt also says GNU document doesn't confirm that.