Skip to content

Commit aeaece7

Browse files
committed
Revert "Reapply: Allow yaml2obj to order implicit sections for ELF"
This reverts commit r317646. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317654 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 29fa40c commit aeaece7

File tree

4 files changed

+38
-86
lines changed

4 files changed

+38
-86
lines changed

lib/ObjectYAML/ELFYAML.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
388388
#define ECase(X) IO.enumCase(Value, #X, ELF::X)
389389
ECase(SHT_NULL);
390390
ECase(SHT_PROGBITS);
391-
ECase(SHT_SYMTAB);
391+
// No SHT_SYMTAB. Use the top-level `Symbols` key instead.
392392
// FIXME: Issue a diagnostic with this information.
393393
ECase(SHT_STRTAB);
394394
ECase(SHT_RELA);

test/tools/yaml2obj/section-ordering.yaml

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/tools/yaml2obj/unnamed-section.yaml

Lines changed: 0 additions & 14 deletions
This file was deleted.

tools/yaml2obj/yaml2elf.cpp

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ class NameToIdxMap {
7474
Idx = I->getValue();
7575
return false;
7676
}
77-
/// asserts if name is not present in the map
78-
unsigned get(StringRef Name) const {
79-
unsigned Idx = 0;
80-
auto missing = lookup(Name, Idx);
81-
assert(!missing && "Expected section not found in index");
82-
return Idx;
83-
}
84-
unsigned size() const { return Map.size(); }
8577
};
8678
} // end anonymous namespace
8779

@@ -152,21 +144,19 @@ class ELFState {
152144
ContiguousBlobAccumulator &CBA);
153145

154146
// - SHT_NULL entry (placed first, i.e. 0'th entry)
155-
// - symbol table (.symtab) (defaults to third to last)
156-
// - string table (.strtab) (defaults to second to last)
157-
// - section header string table (.shstrtab) (defaults to last)
158-
unsigned getDotSymTabSecNo() const { return SN2I.get(".symtab"); }
159-
unsigned getDotStrTabSecNo() const { return SN2I.get(".strtab"); }
160-
unsigned getDotShStrTabSecNo() const { return SN2I.get(".shstrtab"); }
161-
unsigned getSectionCount() const { return SN2I.size() + 1; }
147+
// - symbol table (.symtab) (placed third to last)
148+
// - string table (.strtab) (placed second to last)
149+
// - section header string table (.shstrtab) (placed last)
150+
unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; }
151+
unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; }
152+
unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; }
153+
unsigned getSectionCount() const { return Doc.Sections.size() + 4; }
162154

163155
ELFState(const ELFYAML::Object &D) : Doc(D) {}
164156

165157
public:
166158
static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
167159
};
168-
169-
static const char * const ImplicitSecNames[] = {".symtab", ".strtab", ".shstrtab"};
170160
} // end anonymous namespace
171161

172162
template <class ELFT>
@@ -221,6 +211,10 @@ bool ELFState<ELFT>::initSectionHeaders(std::vector<Elf_Shdr> &SHeaders,
221211
zero(SHeader);
222212
SHeaders.push_back(SHeader);
223213

214+
for (const auto &Sec : Doc.Sections)
215+
DotShStrtab.add(Sec->Name);
216+
DotShStrtab.finalize();
217+
224218
for (const auto &Sec : Doc.Sections) {
225219
zero(SHeader);
226220
SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
@@ -553,9 +547,12 @@ bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
553547
}
554548

555549
template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
550+
SN2I.addName(".symtab", getDotSymTabSecNo());
551+
SN2I.addName(".strtab", getDotStrTabSecNo());
552+
SN2I.addName(".shstrtab", getDotShStrTabSecNo());
553+
556554
for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
557555
StringRef Name = Doc.Sections[i]->Name;
558-
DotShStrtab.add(Name);
559556
if (Name.empty())
560557
continue;
561558
// "+ 1" to take into account the SHT_NULL entry.
@@ -565,17 +562,6 @@ template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
565562
return false;
566563
}
567564
}
568-
569-
auto SecNo = 1 + Doc.Sections.size();
570-
// Add special sections after input sections, if necessary.
571-
for (const auto &Name : ImplicitSecNames)
572-
if (!SN2I.addName(Name, SecNo)) {
573-
// Account for this section, since it wasn't in the Doc
574-
++SecNo;
575-
DotShStrtab.add(Name);
576-
}
577-
578-
DotShStrtab.finalize();
579565
return true;
580566
}
581567

@@ -622,23 +608,32 @@ int ELFState<ELFT>::writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
622608
Header.e_shentsize * Header.e_shnum;
623609
ContiguousBlobAccumulator CBA(SectionContentBeginOffset);
624610

611+
// Doc might not contain .symtab, .strtab and .shstrtab sections,
612+
// but we will emit them, so make sure to add them to ShStrTabSHeader.
613+
State.DotShStrtab.add(".symtab");
614+
State.DotShStrtab.add(".strtab");
615+
State.DotShStrtab.add(".shstrtab");
616+
625617
std::vector<Elf_Shdr> SHeaders;
626-
SHeaders.reserve(State.SN2I.size());
627618
if(!State.initSectionHeaders(SHeaders, CBA))
628619
return 1;
629620

630-
// Populate SHeaders with implicit sections not present in the Doc
631-
for (const auto &Name : ImplicitSecNames)
632-
if (State.SN2I.get(Name) >= SHeaders.size())
633-
SHeaders.push_back({});
634-
635-
// Initialize the implicit sections
636-
auto Index = State.SN2I.get(".symtab");
637-
State.initSymtabSectionHeader(SHeaders[Index], CBA);
638-
Index = State.SN2I.get(".strtab");
639-
State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA);
640-
Index = State.SN2I.get(".shstrtab");
641-
State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA);
621+
// .symtab section.
622+
Elf_Shdr SymtabSHeader;
623+
State.initSymtabSectionHeader(SymtabSHeader, CBA);
624+
SHeaders.push_back(SymtabSHeader);
625+
626+
// .strtab string table header.
627+
Elf_Shdr DotStrTabSHeader;
628+
State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
629+
CBA);
630+
SHeaders.push_back(DotStrTabSHeader);
631+
632+
// .shstrtab string table header.
633+
Elf_Shdr ShStrTabSHeader;
634+
State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
635+
CBA);
636+
SHeaders.push_back(ShStrTabSHeader);
642637

643638
// Now we can decide segment offsets
644639
State.setProgramHeaderLayout(PHeaders, SHeaders);

0 commit comments

Comments
 (0)