Skip to content

Commit a7ba1a0

Browse files
author
George Rimar
committed
[yaml2obj] - Allow setting custom sh_info for RawContentSection sections.
This is for tweaking SHT_SYMTAB sections. Their sh_info contains the (number of symbols + 1) usually. But for creating invalid inputs for test cases it would be convenient to allow explicitly override this field from YAML. Differential revision: https://reviews.llvm.org/D58779 llvm-svn: 355193
1 parent b0224b1 commit a7ba1a0

File tree

7 files changed

+98
-7
lines changed

7 files changed

+98
-7
lines changed

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ struct DynamicSection : Section {
153153
struct RawContentSection : Section {
154154
yaml::BinaryRef Content;
155155
llvm::yaml::Hex64 Size;
156+
llvm::yaml::Hex64 Info;
156157

157158
RawContentSection() : Section(SectionKind::RawContent) {}
158159

llvm/lib/ObjectYAML/ELFYAML.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
887887
commonSectionMapping(IO, Section);
888888
IO.mapOptional("Content", Section.Content);
889889
IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
890+
IO.mapOptional("Info", Section.Info, Hex64(0));
890891
}
891892

892893
static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# RUN: yaml2obj %s -o %t
2+
# RUN: obj2yaml %t | FileCheck %s
3+
4+
## Check obj2yaml is able to dump sh_info field of a section.
5+
6+
# CHECK: - Name: .test
7+
# CHECK-NEXT: Type: SHT_PROGBITS
8+
# CHECK-NEXT: Content: ''
9+
# CHECK-NEXT: Info: 0x000000000000002A
10+
11+
--- !ELF
12+
FileHeader:
13+
Class: ELFCLASS64
14+
Data: ELFDATA2LSB
15+
Type: ET_REL
16+
Machine: EM_X86_64
17+
Sections:
18+
- Name: .test
19+
Type: SHT_PROGBITS
20+
Info: 42
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Check we are able to set sh_info field for SHT_SYMTAB sections.
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-readobj -sections %t | FileCheck %s
4+
5+
# CHECK: Name: .symtab
6+
# CHECK-NEXT: Type: SHT_SYMTAB
7+
# CHECK-NEXT: Flags [
8+
# CHECK-NEXT: ]
9+
# CHECK-NEXT: Address:
10+
# CHECK-NEXT: Offset:
11+
# CHECK-NEXT: Size:
12+
# CHECK-NEXT: Link:
13+
# CHECK-NEXT: Info: 42
14+
# CHECK: Name: .dynsym
15+
# CHECK-NEXT: Type: SHT_DYNSYM
16+
# CHECK-NEXT: Flags [
17+
# CHECK-NEXT: SHF_ALLOC
18+
# CHECK-NEXT: ]
19+
# CHECK-NEXT: Address:
20+
# CHECK-NEXT: Offset:
21+
# CHECK-NEXT: Size:
22+
# CHECK-NEXT: Link:
23+
# CHECK-NEXT: Info: 26
24+
25+
--- !ELF
26+
FileHeader:
27+
Class: ELFCLASS64
28+
Data: ELFDATA2LSB
29+
Type: ET_REL
30+
Machine: EM_X86_64
31+
Sections:
32+
- Name: .symtab
33+
Info: 42
34+
Type: SHT_SYMTAB
35+
- Name: .dynsym
36+
Info: 26
37+
Type: SHT_SYMTAB
38+
Symbols:
39+
Global:
40+
- Name: foo
41+
DynamicSymbols:
42+
Global:
43+
- Name: bar
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Check we dont crash when .symtab has type different from SHT_SYMTAB.
2+
# RUN: yaml2obj %s -o %t
3+
# RUN: llvm-readobj -sections %t | FileCheck %s
4+
5+
## TODO: the output is still SHT_SYMTAB because we do not yet
6+
## support changing it.
7+
# CHECK: Name: .symtab
8+
# CHECK-NEXT: Type: SHT_SYMTAB
9+
10+
--- !ELF
11+
FileHeader:
12+
Class: ELFCLASS64
13+
Data: ELFDATA2LSB
14+
Type: ET_REL
15+
Machine: EM_X86_64
16+
Sections:
17+
- Name: .symtab
18+
Type: SHT_DYNAMIC
19+
Symbols:
20+
Global:
21+
- Name: foo

llvm/tools/obj2yaml/elf2yaml.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
458458
return errorToErrorCode(ContentOrErr.takeError());
459459
S->Content = yaml::BinaryRef(ContentOrErr.get());
460460
S->Size = S->Content.binary_size();
461+
S->Info = Shdr->sh_info;
461462

462463
return S.release();
463464
}

llvm/tools/yaml2obj/yaml2elf.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,16 @@ void ELFState<ELFT>::initSymtabSectionHeader(Elf_Shdr &SHeader,
341341
SHeader.sh_entsize = sizeof(Elf_Sym);
342342
SHeader.sh_addralign = 8;
343343

344-
// If .dynsym section is explicitly described in the YAML
345-
// then we want to use its section address.
346-
if (!IsStatic) {
347-
// Take section index and ignore the SHT_NULL section.
348-
unsigned SecNdx = getDotDynSymSecNo() - 1;
349-
if (SecNdx < Doc.Sections.size())
350-
SHeader.sh_addr = Doc.Sections[SecNdx]->Address;
344+
// Get the section index ignoring the SHT_NULL section.
345+
unsigned SecNdx =
346+
IsStatic ? getDotSymTabSecNo() - 1 : getDotDynSymSecNo() - 1;
347+
// If the symbol table section is explicitly described in the YAML
348+
// then we should set the fields requested.
349+
if (SecNdx < Doc.Sections.size()) {
350+
ELFYAML::Section *Sec = Doc.Sections[SecNdx].get();
351+
SHeader.sh_addr = Sec->Address;
352+
if (auto S = dyn_cast<ELFYAML::RawContentSection>(Sec))
353+
SHeader.sh_info = S->Info;
351354
}
352355

353356
std::vector<Elf_Sym> Syms;
@@ -503,6 +506,7 @@ ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
503506
else
504507
SHeader.sh_entsize = 0;
505508
SHeader.sh_size = Section.Size;
509+
SHeader.sh_info = Section.Info;
506510
}
507511

508512
static bool isMips64EL(const ELFYAML::Object &Doc) {

0 commit comments

Comments
 (0)