Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion llvm/lib/ObjectYAML/ELFEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ template <class ELFT>
void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
const ELFYAML::NoteSection &Section,
ContiguousBlobAccumulator &CBA) {
if (!Section.Notes)
if (!Section.Notes || Section.Notes->empty())
return;

unsigned Align;
Expand All @@ -1814,6 +1814,13 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
return;
}

if (CBA.getOffset() != alignTo(CBA.getOffset(), Align)) {
reportError(Section.Name + ": invalid offset of a note section: 0x" +
Twine::utohexstr(CBA.getOffset()) + ", should be aligned to " +
Twine(Align));
return;
}

uint64_t Offset = CBA.tell();
for (const ELFYAML::NoteEntry &NE : *Section.Notes) {
// Write name size.
Expand Down
55 changes: 55 additions & 0 deletions llvm/test/tools/yaml2obj/ELF/note-section.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,58 @@ Sections:
Desc: 030405
- Name: GNU
Type: NT_GNU_BUILD_ID

## Check that an incorrect offset for generating notes is reported.

# RUN: not yaml2obj --docnum=19 %s 2>&1 | FileCheck %s --check-prefix=ERR_OFFSET
# ERR_OFFSET: error: .note: invalid offset of a note section: 0x{{.*}}, should be aligned to 4

--- !ELF
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_EXEC
Sections:
- Name: .dummy
Type: SHT_PROGBITS
Size: 1
- Name: .note
Type: SHT_NOTE
Notes:
- Type: 0x1

## Do not issue an error if the notes array is empty.

# RUN: yaml2obj --docnum=20 %s -o - | \
# RUN: llvm-readobj --sections --section-data - | \
# RUN: FileCheck %s --check-prefix=TEST20

# TEST20: Section {
# TEST20: Name: .note
# TEST20-NEXT: Type: SHT_NOTE
# TEST20-NEXT: Flags [ (0x0)
# TEST20-NEXT: ]
# TEST20-NEXT: Address:
# TEST20-NEXT: Offset:
# TEST20-NEXT: Size: 0
# TEST20-NEXT: Link:
# TEST20-NEXT: Info:
# TEST20-NEXT: AddressAlignment: 5
# TEST20-NEXT: EntrySize:
# TEST20-NEXT: SectionData (
# TEST20-NEXT: )
# TEST20-NEXT: }

--- !ELF
FileHeader:
Class: ELFCLASS32
Data: ELFDATA2LSB
Type: ET_EXEC
Sections:
- Name: .dummy
Type: SHT_PROGBITS
Size: 1
- Name: .note
Type: SHT_NOTE
AddressAlign: 5
Notes: []
Loading