Skip to content

Commit 5b8e6a0

Browse files
MaskRayMrSidims
authored andcommitted
[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
1 parent acdf76f commit 5b8e6a0

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lld/ELF/Writer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,11 +1999,12 @@ template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() {
19991999
if (Config->ZWxneeded)
20002000
AddHdr(PT_OPENBSD_WXNEEDED, PF_X);
20012001

2002-
// Create one PT_NOTE per a group of contiguous .note sections.
2002+
// Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
2003+
// same alignment.
20032004
PhdrEntry *Note = nullptr;
20042005
for (OutputSection *Sec : OutputSections) {
20052006
if (Sec->Type == SHT_NOTE && (Sec->Flags & SHF_ALLOC)) {
2006-
if (!Note || Sec->LMAExpr)
2007+
if (!Note || Sec->LMAExpr || Note->LastSec->Alignment != Sec->Alignment)
20072008
Note = AddHdr(PT_NOTE, PF_R);
20082009
Note->add(Sec);
20092010
} else {

lld/test/ELF/build-id.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ _start:
6565
# DEFAULT: Contents of section .note.test:
6666
# DEFAULT: Contents of section .note.gnu.build-id:
6767
# DEFAULT-NEXT: 04000000 08000000 03000000 474e5500 ............GNU.
68-
# DEFAULT-NEXT: 894c04e8 fbf5556b
68+
# DEFAULT-NEXT: 95849665 2621c734
6969

7070
# MD5: Contents of section .note.gnu.build-id:
7171
# MD5-NEXT: 04000000 10000000 03000000 474e5500 ............GNU.
72-
# MD5-NEXT: 6a51bbd7 9e8ee3f9 2e02d213 711cfec9
72+
# MD5-NEXT: 1882c01f 71698eed 229b3994 eb554c80
7373

7474
# SHA1: Contents of section .note.gnu.build-id:
7575
# SHA1-NEXT: 04000000 14000000 03000000 474e5500 ............GNU.
76-
# SHA1-NEXT: 9a8618b1 d6fd0e5c eda73dd8 76de5596
76+
# SHA1-NEXT: 96820adf d90d5470 0a0c32ff a88c4017
7777

7878
# UUID: Contents of section .note.gnu.build-id:
7979
# UUID-NEXT: 04000000 10000000 03000000 474e5500 ............GNU.

lld/test/ELF/note-alignment.s

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
3+
# RUN: ld.lld %t.o -o %t
4+
# RUN: llvm-readelf -l %t | FileCheck %s
5+
6+
# Check that we don't mix 4-byte and 8-byte aligned notes in one PT_LOAD.
7+
# The possible 4-byte padding before the 8-byte align note may make consumers
8+
# fail to parse it.
9+
10+
# CHECK: NOTE {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} 0x000004 0x000004 R 0x4
11+
# CHECK: NOTE {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} 0x000010 0x000010 R 0x8
12+
# CHECK: NOTE {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} {{0x[0-9a-f]+}} 0x000008 0x000008 R 0x4
13+
14+
# CHECK: 03 .note.a
15+
# CHECK-NEXT: 04 .note.b .note.c
16+
# CHECK-NEXT: 05 .note.d .note.e
17+
18+
.section .note.a, "a", @note
19+
.align 4
20+
.long 0
21+
22+
.section .note.b, "a", @note
23+
.align 8
24+
.quad 0
25+
26+
.section .note.c, "a", @note
27+
.align 8
28+
.quad 0
29+
30+
.section .note.d, "a", @note
31+
.align 4
32+
.long 0
33+
34+
.section .note.e, "a", @note
35+
.align 4
36+
.long 0

0 commit comments

Comments
 (0)