-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ObjectYAML][ELF] Take alignment into account when generating notes #118157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The [System V ABI](https://www.sco.com/developers/gabi/latest/ch5.pheader.html#note_section) states that the note entries and their descriptor fields must be aligned to 4 or 8 bytes for 32-bit and 64-bit objects respectively. In practice, 64-bit systems can use both alignments, with the actual format being distinguished by the alignment of the segment. For example, the [Linux gABI extension](https://github.com/hjl-tools/linux-abi/wiki/linux-abi-draft.pdf) contains a special note on this, see 2.1.7 "Alignment of Note Sections". This patch adjusts the format of the generated notes to the specified section alignment. Since `llvm-readobj` was fixed in a similar way in [D150022](https://reviews.llvm.org/D150022], `[Object] Fix handling of Elf_Nhdr with sh_addralign=8`, the generated notes can now be parsed successfully by the tool.
@llvm/pr-subscribers-objectyaml Author: Igor Kudrin (igorkudrin) ChangesThe System V ABI states that the note entries and their descriptor fields must be aligned to 4 or 8 bytes for 32-bit and 64-bit objects respectively. In practice, 64-bit systems can use both alignments, with the actual format being distinguished by the alignment of the segment. For example, the Linux gABI extension contains a special note on this, see 2.1.7 "Alignment of Note Sections". This patch adjusts the format of the generated notes to the specified section alignment. Since Full diff: https://github.com/llvm/llvm-project/pull/118157.diff 2 Files Affected:
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 476334024151a9..8603f2f73b858d 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -1799,6 +1799,20 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
if (!Section.Notes)
return;
+ unsigned Align;
+ switch (SHeader.sh_addralign) {
+ case 0:
+ case 4:
+ Align = 4;
+ break;
+ case 8:
+ Align = 8;
+ break;
+ default:
+ reportError(Section.Name + ": invalid alignment for a note section: 0x" +
+ Twine::utohexstr(SHeader.sh_addralign));
+ }
+
uint64_t Offset = CBA.tell();
for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
// Write name size.
@@ -1820,13 +1834,13 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
if (!NE.Name.empty()) {
CBA.write(NE.Name.data(), NE.Name.size());
CBA.write('\0');
- CBA.padToAlignment(4);
+ CBA.padToAlignment(Align);
}
// Write description and padding.
if (NE.Desc.binary_size() != 0) {
CBA.writeAsBinary(NE.Desc);
- CBA.padToAlignment(4);
+ CBA.padToAlignment(Align);
}
}
diff --git a/llvm/test/tools/yaml2obj/ELF/note-section.yaml b/llvm/test/tools/yaml2obj/ELF/note-section.yaml
index 80359c4ec01833..067690f8f25b00 100644
--- a/llvm/test/tools/yaml2obj/ELF/note-section.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/note-section.yaml
@@ -333,3 +333,74 @@ Sections:
- Name: ABC
Desc: '123456'
Type: NT_VERSION
+
+## Check that an incorrect alignment is reported
+
+# RUN: not yaml2obj --docnum=16 %s 2>&1 | FileCheck %s --check-prefix=ERR_ALIGN1
+# ERR_ALIGN1: error: .note.foo: invalid alignment for a note section: 0x1
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+Sections:
+ - Name: .note.foo
+ Type: SHT_NOTE
+ AddressAlign: 1
+ Notes:
+ - Type: 0x1
+
+## Check that note entries and their `Desc` fields are aligned according to the
+## specified section alignment
+
+# RUN: yaml2obj --docnum=17 %s -o - | \
+# RUN: llvm-readobj --sections --section-data - | \
+# RUN: FileCheck %s --check-prefix=TEST17
+
+# TEST17: Name: .note.foo4
+# TEST17: SectionData (
+# TEST17-NEXT: 0000: 05000000 02000000 01000000 41424344 |............ABCD|
+# TEST17-NEXT: 0010: 00000000 01020000 03000000 03000000 |................|
+# TEST17-NEXT: 0020: 02000000 41420000 03040500 03000000 |....AB..........|
+# TEST17-NEXT: 0030: 00000000 03000000 41420000 |........AB..|
+# TEST17-NEXT: )
+# TEST17: Name: .note.foo8
+# TEST17: SectionData (
+# TEST17-NEXT: 0000: 05000000 02000000 01000000 41424344 |............ABCD|
+# TEST17-NEXT: 0010: 00000000 00000000 01020000 00000000 |................|
+# TEST17-NEXT: 0020: 03000000 03000000 02000000 41420000 |............AB..|
+# TEST17-NEXT: 0030: 03040500 00000000 03000000 00000000 |................|
+# TEST17-NEXT: 0040: 03000000 41420000 |....AB..|
+# TEST17-NEXT: )
+
+--- !ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+Sections:
+ - Name: .note.foo4
+ Type: SHT_NOTE
+ AddressAlign: 4
+ Notes:
+ - Name: ABCD
+ Desc: 0102
+ Type: NT_VERSION
+ - Name: AB
+ Desc: 030405
+ Type: NT_ARCH
+ - Name: AB
+ Type: NT_GNU_BUILD_ID
+ - Name: .note.foo8
+ Type: SHT_NOTE
+ AddressAlign: 8
+ Notes:
+ - Name: ABCD
+ Desc: 0102
+ Type: NT_VERSION
+ - Name: AB
+ Desc: 030405
+ Type: NT_ARCH
+ - Name: AB
+ Type: NT_GNU_BUILD_ID
|
Stray |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/11692 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/140/builds/12139 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/56/builds/13522 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/60/builds/14147 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/16/builds/9941 Here is the relevant piece of the build log for the reference
|
… notes" This relands llvm#118157 with a fix for the use of an uninitialised variable. The [System V ABI](https://www.sco.com/developers/gabi/latest/ch5.pheader.html#note_section) states that the note entries and their descriptor fields must be aligned to 4 or 8 bytes for 32-bit or 64-bit objects respectively. In practice, 64-bit systems can use both alignments, with the actual format being determined by the alignment of the segment. For example, the [Linux gABI extension](https://github.com/hjl-tools/linux-abi/wiki/linux-abi-draft.pdf) contains a special note on this, see 2.1.7 "Alignment of Note Sections". This patch adjusts the format of the generated notes to the specified section alignment. Since `llvm-readobj` was fixed in a similar way in [D150022](https://reviews.llvm.org/D150022), "[Object] Fix handling of Elf_Nhdr with sh_addralign=8", the generated notes can now be parsed successfully by the tool.
… notes" (#118434) This relands #118157 with a fix for the use of an uninitialized variable and additional tests. The System V ABI (https://www.sco.com/developers/gabi/latest/ch5.pheader.html#note_section) states that the note entries and their descriptor fields must be aligned to 4 or 8 bytes for 32-bit or 64-bit objects respectively. In practice, 64-bit systems can use both alignments, with the actual format being determined by the alignment of the segment. For example, the Linux gABI extension (https://github.com/hjl-tools/linux-abi/wiki/linux-abi-draft.pdf) contains a special note on this, see 2.1.7 "Alignment of Note Sections". This patch adjusts the format of the generated notes to the specified section alignment. Since `llvm-readobj` was fixed in a similar way in https://reviews.llvm.org/D150022, "[Object] Fix handling of Elf_Nhdr with sh_addralign=8", the generated notes can now be parsed successfully by the tool.
The System V ABI states that the note entries and their descriptor fields must be aligned to 4 or 8 bytes for 32-bit or 64-bit objects respectively. In practice, 64-bit systems can use both alignments, with the actual format being determined by the alignment of the segment. For example, the Linux gABI extension contains a special note on this, see 2.1.7 "Alignment of Note Sections".
This patch adjusts the format of the generated notes to the specified section alignment. Since
llvm-readobj
was fixed in a similar way in D150022, "[Object] Fix handling of Elf_Nhdr with sh_addralign=8", the generated notes can now be parsed successfully by the tool.