Skip to content

Commit

Permalink
Revert "Reapply: Allow yaml2obj to order implicit sections for ELF"
Browse files Browse the repository at this point in the history
This reverts commit r317646.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317654 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
kastiglione committed Nov 8, 2017
1 parent 29fa40c commit aeaece7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 86 deletions.
2 changes: 1 addition & 1 deletion lib/ObjectYAML/ELFYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
#define ECase(X) IO.enumCase(Value, #X, ELF::X)
ECase(SHT_NULL);
ECase(SHT_PROGBITS);
ECase(SHT_SYMTAB);
// No SHT_SYMTAB. Use the top-level `Symbols` key instead.
// FIXME: Issue a diagnostic with this information.
ECase(SHT_STRTAB);
ECase(SHT_RELA);
Expand Down
29 changes: 0 additions & 29 deletions test/tools/yaml2obj/section-ordering.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions test/tools/yaml2obj/unnamed-section.yaml

This file was deleted.

79 changes: 37 additions & 42 deletions tools/yaml2obj/yaml2elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ class NameToIdxMap {
Idx = I->getValue();
return false;
}
/// asserts if name is not present in the map
unsigned get(StringRef Name) const {
unsigned Idx = 0;
auto missing = lookup(Name, Idx);
assert(!missing && "Expected section not found in index");
return Idx;
}
unsigned size() const { return Map.size(); }
};
} // end anonymous namespace

Expand Down Expand Up @@ -152,21 +144,19 @@ class ELFState {
ContiguousBlobAccumulator &CBA);

// - SHT_NULL entry (placed first, i.e. 0'th entry)
// - symbol table (.symtab) (defaults to third to last)
// - string table (.strtab) (defaults to second to last)
// - section header string table (.shstrtab) (defaults to last)
unsigned getDotSymTabSecNo() const { return SN2I.get(".symtab"); }
unsigned getDotStrTabSecNo() const { return SN2I.get(".strtab"); }
unsigned getDotShStrTabSecNo() const { return SN2I.get(".shstrtab"); }
unsigned getSectionCount() const { return SN2I.size() + 1; }
// - symbol table (.symtab) (placed third to last)
// - string table (.strtab) (placed second to last)
// - section header string table (.shstrtab) (placed last)
unsigned getDotSymTabSecNo() const { return Doc.Sections.size() + 1; }
unsigned getDotStrTabSecNo() const { return Doc.Sections.size() + 2; }
unsigned getDotShStrTabSecNo() const { return Doc.Sections.size() + 3; }
unsigned getSectionCount() const { return Doc.Sections.size() + 4; }

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

public:
static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc);
};

static const char * const ImplicitSecNames[] = {".symtab", ".strtab", ".shstrtab"};
} // end anonymous namespace

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

for (const auto &Sec : Doc.Sections)
DotShStrtab.add(Sec->Name);
DotShStrtab.finalize();

for (const auto &Sec : Doc.Sections) {
zero(SHeader);
SHeader.sh_name = DotShStrtab.getOffset(Sec->Name);
Expand Down Expand Up @@ -553,9 +547,12 @@ bool ELFState<ELFT>::writeSectionContent(Elf_Shdr &SHeader,
}

template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
SN2I.addName(".symtab", getDotSymTabSecNo());
SN2I.addName(".strtab", getDotStrTabSecNo());
SN2I.addName(".shstrtab", getDotShStrTabSecNo());

for (unsigned i = 0, e = Doc.Sections.size(); i != e; ++i) {
StringRef Name = Doc.Sections[i]->Name;
DotShStrtab.add(Name);
if (Name.empty())
continue;
// "+ 1" to take into account the SHT_NULL entry.
Expand All @@ -565,17 +562,6 @@ template <class ELFT> bool ELFState<ELFT>::buildSectionIndex() {
return false;
}
}

auto SecNo = 1 + Doc.Sections.size();
// Add special sections after input sections, if necessary.
for (const auto &Name : ImplicitSecNames)
if (!SN2I.addName(Name, SecNo)) {
// Account for this section, since it wasn't in the Doc
++SecNo;
DotShStrtab.add(Name);
}

DotShStrtab.finalize();
return true;
}

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

// Doc might not contain .symtab, .strtab and .shstrtab sections,
// but we will emit them, so make sure to add them to ShStrTabSHeader.
State.DotShStrtab.add(".symtab");
State.DotShStrtab.add(".strtab");
State.DotShStrtab.add(".shstrtab");

std::vector<Elf_Shdr> SHeaders;
SHeaders.reserve(State.SN2I.size());
if(!State.initSectionHeaders(SHeaders, CBA))
return 1;

// Populate SHeaders with implicit sections not present in the Doc
for (const auto &Name : ImplicitSecNames)
if (State.SN2I.get(Name) >= SHeaders.size())
SHeaders.push_back({});

// Initialize the implicit sections
auto Index = State.SN2I.get(".symtab");
State.initSymtabSectionHeader(SHeaders[Index], CBA);
Index = State.SN2I.get(".strtab");
State.initStrtabSectionHeader(SHeaders[Index], ".strtab", State.DotStrtab, CBA);
Index = State.SN2I.get(".shstrtab");
State.initStrtabSectionHeader(SHeaders[Index], ".shstrtab", State.DotShStrtab, CBA);
// .symtab section.
Elf_Shdr SymtabSHeader;
State.initSymtabSectionHeader(SymtabSHeader, CBA);
SHeaders.push_back(SymtabSHeader);

// .strtab string table header.
Elf_Shdr DotStrTabSHeader;
State.initStrtabSectionHeader(DotStrTabSHeader, ".strtab", State.DotStrtab,
CBA);
SHeaders.push_back(DotStrTabSHeader);

// .shstrtab string table header.
Elf_Shdr ShStrTabSHeader;
State.initStrtabSectionHeader(ShStrTabSHeader, ".shstrtab", State.DotShStrtab,
CBA);
SHeaders.push_back(ShStrTabSHeader);

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

0 comments on commit aeaece7

Please sign in to comment.