[ELF] Place SHT_NOTE sections with the same alignment into one PT_NOTE
authorFangrui Song <maskray@google.com>
Fri, 3 May 2019 00:35:49 +0000 (00:35 +0000)
committerFangrui Song <maskray@google.com>
Fri, 3 May 2019 00:35:49 +0000 (00:35 +0000)
commitd45df094353b8b0b5f1f76d9f5f828c34595fe11
tree878c4a27c7155f81914d0e4c74ec418bdbefec89
parent86e2f169bb7b9fe75dc35bb21767c4961decb0a5
[ELF] Place SHT_NOTE sections with the same alignment into one PT_NOTE

Summary:
While the generic ABI requires notes to be 8-byte aligned in ELF64, many
vendor-specific notes (from Linux, NetBSD, Solaris, etc) use 4-byte
alignment.

In a PT_NOTE segment, if 4-byte aligned notes are followed by an 8-byte
aligned note, the possible 4-byte padding may make consumers fail to
parse the 8-byte aligned note. See PR41000 for a recent report about
.note.gnu.property (NT_GNU_PROPERTY_TYPE_0).
(Note, for NT_GNU_PROPERTY_TYPE_0, the consumers should probably migrate
to PT_GNU_PROPERTY, but the alignment issue affects other notes as well.)

To fix the issue, don't mix notes with different alignments in one
PT_NOTE. If compilers emit 4-byte aligned notes before 8-byte aligned
notes, we'll create at most 2 segments.

sh_size%sh_addralign=0 is actually implied by the rule for linking
unrecognized sections (in generic ABI), so we don't have to check that.
Notes that match in name, type and attribute flags are concatenated into
a single output section. The compilers have to ensure
sh_size%sh_addralign=0 to make concatenated notes parsable.

An alternative approach is to create a PT_NOTE for each SHT_NOTE, but
we'll have to incur the sizeof(Elf64_Phdr)=56 overhead every time a new
note section is introduced.

Reviewers: ruiu, jakehehrlich, phosek, jhenderson, pcc, espindola

Subscribers: emaste, arichardson, krytarowski, fedor.sergeev, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D61296

llvm-svn: 359853
lld/ELF/Writer.cpp
lld/test/ELF/build-id.s
lld/test/ELF/note-alignment.s [new file with mode: 0644]