Skip to content

Commit 3df90c7

Browse files
committed
[ObjectYAML][ELF] Report incorrect offset to generate notes
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 740ac4f commit 3df90c7

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,58 @@ 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: 0x{{.*}}, should be aligned to 4
522+
523+
--- !ELF
524+
FileHeader:
525+
Class: ELFCLASS32
526+
Data: ELFDATA2LSB
527+
Type: ET_EXEC
528+
Sections:
529+
- Name: .dummy
530+
Type: SHT_PROGBITS
531+
Size: 1
532+
- Name: .note
533+
Type: SHT_NOTE
534+
Notes:
535+
- Type: 0x1
536+
537+
## Do not issue an error if the notes array is empty.
538+
539+
# RUN: yaml2obj --docnum=20 %s -o - | \
540+
# RUN: llvm-readobj --sections --section-data - | \
541+
# RUN: FileCheck %s --check-prefix=TEST20
542+
543+
# TEST20: Section {
544+
# TEST20: Name: .note
545+
# TEST20-NEXT: Type: SHT_NOTE
546+
# TEST20-NEXT: Flags [ (0x0)
547+
# TEST20-NEXT: ]
548+
# TEST20-NEXT: Address:
549+
# TEST20-NEXT: Offset:
550+
# TEST20-NEXT: Size: 0
551+
# TEST20-NEXT: Link:
552+
# TEST20-NEXT: Info:
553+
# TEST20-NEXT: AddressAlignment: 5
554+
# TEST20-NEXT: EntrySize:
555+
# TEST20-NEXT: SectionData (
556+
# TEST20-NEXT: )
557+
# TEST20-NEXT: }
558+
559+
--- !ELF
560+
FileHeader:
561+
Class: ELFCLASS32
562+
Data: ELFDATA2LSB
563+
Type: ET_EXEC
564+
Sections:
565+
- Name: .dummy
566+
Type: SHT_PROGBITS
567+
Size: 1
568+
- Name: .note
569+
Type: SHT_NOTE
570+
AddressAlign: 5
571+
Notes: []

0 commit comments

Comments
 (0)