Skip to content

Commit 93fc0ba

Browse files
author
Georgii Rymar
committed
[yaml2obj] - Add NBucket and NChain fields for the SHT_HASH section.
These fields allows to override nchain and nbucket fields of a SHT_HASH section. Differential revision: https://reviews.llvm.org/D76834
1 parent 0ec88d0 commit 93fc0ba

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ struct HashSection : Section {
282282
Optional<std::vector<uint32_t>> Bucket;
283283
Optional<std::vector<uint32_t>> Chain;
284284

285+
// The following members are used to override section fields.
286+
// This is useful for creating invalid objects.
287+
Optional<llvm::yaml::Hex64> NBucket;
288+
Optional<llvm::yaml::Hex64> NChain;
289+
285290
HashSection() : Section(ChunkKind::Hash) {}
286291

287292
static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Hash; }

llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,13 @@ void ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
10651065
return;
10661066
}
10671067

1068-
support::endian::write<uint32_t>(OS, Section.Bucket->size(),
1069-
ELFT::TargetEndianness);
1070-
support::endian::write<uint32_t>(OS, Section.Chain->size(),
1071-
ELFT::TargetEndianness);
1068+
support::endian::write<uint32_t>(
1069+
OS, Section.NBucket.getValueOr(llvm::yaml::Hex64(Section.Bucket->size())),
1070+
ELFT::TargetEndianness);
1071+
support::endian::write<uint32_t>(
1072+
OS, Section.NChain.getValueOr(llvm::yaml::Hex64(Section.Chain->size())),
1073+
ELFT::TargetEndianness);
1074+
10721075
for (uint32_t Val : *Section.Bucket)
10731076
support::endian::write<uint32_t>(OS, Val, ELFT::TargetEndianness);
10741077
for (uint32_t Val : *Section.Chain)

llvm/lib/ObjectYAML/ELFYAML.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,13 @@ static void sectionMapping(IO &IO, ELFYAML::HashSection &Section) {
10911091
IO.mapOptional("Bucket", Section.Bucket);
10921092
IO.mapOptional("Chain", Section.Chain);
10931093
IO.mapOptional("Size", Section.Size);
1094+
1095+
// obj2yaml does not dump these fields. They can be used to override nchain
1096+
// and nbucket values for creating broken sections.
1097+
assert(!IO.outputting() ||
1098+
(!Section.NBucket.hasValue() && !Section.NChain.hasValue()));
1099+
IO.mapOptional("NChain", Section.NChain);
1100+
IO.mapOptional("NBucket", Section.NBucket);
10941101
}
10951102

10961103
static void sectionMapping(IO &IO, ELFYAML::NoteSection &Section) {

llvm/test/tools/yaml2obj/ELF/hash-section.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,39 @@ Sections:
276276
Type: SHT_HASH
277277
Size: 0x1
278278
Chain: [ 1 ]
279+
280+
## Check we can override "nbucket" and "nchain" values of a SHT_HASH section using "NBucket"
281+
## and "NChain" tags. Check that the section size is unaffected when we do this.
282+
283+
# RUN: yaml2obj --docnum=14 %s -o %t14
284+
# RUN: llvm-readobj --sections --section-data %t14 | FileCheck %s --check-prefix=OVERRIDE
285+
286+
# OVERRIDE: Name: .hash
287+
# OVERRIDE-NEXT: Type: SHT_HASH
288+
# OVERRIDE-NEXT: Flags [
289+
# OVERRIDE-NEXT: ]
290+
# OVERRIDE-NEXT: Address: 0x0
291+
# OVERRIDE-NEXT: Offset: 0x34
292+
# OVERRIDE-NEXT: Size: 28
293+
# OVERRIDE-NEXT: Link: 0
294+
# OVERRIDE-NEXT: Info: 0
295+
# OVERRIDE-NEXT: AddressAlignment: 0
296+
# OVERRIDE-NEXT: EntrySize: 0
297+
# OVERRIDE-NEXT: SectionData (
298+
# OVERRIDE-NEXT: 0000: AA000000 BB000000 01000000 02000000
299+
# OVERRIDE-NEXT: 0010: 03000000 04000000 05000000
300+
# OVERRIDE-NEXT: )
301+
302+
--- !ELF
303+
FileHeader:
304+
Class: ELFCLASS32
305+
Data: ELFDATA2LSB
306+
Type: ET_DYN
307+
Machine: EM_386
308+
Sections:
309+
- Name: .hash
310+
Type: SHT_HASH
311+
Bucket: [ 1, 2 ]
312+
Chain: [ 3, 4, 5 ]
313+
NBucket: 0xAA
314+
NChain: 0xBB

0 commit comments

Comments
 (0)