Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ba37309

Browse files
authoredDec 23, 2024
[ObjectYAML][ELF] Report incorrect offset to generate notes (llvm#118741)
All notes in the note section must be correctly aligned, including the first. The tool should refuse to generate notes if the section offset is incorrect in this respect.
1 parent fbdf652 commit ba37309

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed
 

‎llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,7 @@ template <class ELFT>
17961796
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
17971797
const ELFYAML::NoteSection &Section,
17981798
ContiguousBlobAccumulator &CBA) {
1799-
if (!Section.Notes)
1799+
if (!Section.Notes || Section.Notes->empty())
18001800
return;
18011801

18021802
unsigned Align;
@@ -1814,6 +1814,13 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
18141814
return;
18151815
}
18161816

1817+
if (CBA.getOffset() != alignTo(CBA.getOffset(), Align)) {
1818+
reportError(Section.Name + ": invalid offset of a note section: 0x" +
1819+
Twine::utohexstr(CBA.getOffset()) + ", should be aligned to " +
1820+
Twine(Align));
1821+
return;
1822+
}
1823+
18171824
uint64_t Offset = CBA.tell();
18181825
for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
18191826
// Write name size.

‎llvm/test/tools/yaml2obj/ELF/note-section.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,54 @@ Sections:
514514
Desc: 030405
515515
- Name: GNU
516516
Type: NT_GNU_BUILD_ID
517+
518+
## Check that an incorrect offset for generating notes is reported.
519+
520+
# RUN: not yaml2obj --docnum=19 %s 2>&1 | FileCheck %s --check-prefix=ERR_OFFSET
521+
# ERR_OFFSET: error: .note: invalid offset of a note section: 0x501, should be aligned to 4
522+
523+
--- !ELF
524+
FileHeader:
525+
Class: ELFCLASS32
526+
Data: ELFDATA2LSB
527+
Type: ET_EXEC
528+
Sections:
529+
- Name: .note
530+
Type: SHT_NOTE
531+
Offset: 0x501
532+
Notes:
533+
- Type: 0x1
534+
535+
## Do not issue an error if the notes array is empty.
536+
537+
# RUN: yaml2obj --docnum=20 %s -o - | \
538+
# RUN: llvm-readobj --sections --section-data - | \
539+
# RUN: FileCheck %s --check-prefix=TEST20
540+
541+
# TEST20: Section {
542+
# TEST20: Name: .note
543+
# TEST20-NEXT: Type: SHT_NOTE
544+
# TEST20-NEXT: Flags [ (0x0)
545+
# TEST20-NEXT: ]
546+
# TEST20-NEXT: Address:
547+
# TEST20-NEXT: Offset: 0x501
548+
# TEST20-NEXT: Size: 0
549+
# TEST20-NEXT: Link:
550+
# TEST20-NEXT: Info:
551+
# TEST20-NEXT: AddressAlignment: 5
552+
# TEST20-NEXT: EntrySize:
553+
# TEST20-NEXT: SectionData (
554+
# TEST20-NEXT: )
555+
# TEST20-NEXT: }
556+
557+
--- !ELF
558+
FileHeader:
559+
Class: ELFCLASS32
560+
Data: ELFDATA2LSB
561+
Type: ET_EXEC
562+
Sections:
563+
- Name: .note
564+
Type: SHT_NOTE
565+
Offset: 0x501
566+
AddressAlign: 5
567+
Notes: []

0 commit comments

Comments
 (0)
Please sign in to comment.