[yaml2obj][ELF] Don't monkeypatch the YAML sections.

Previously, we would monkeypatch the vector of YAML::Section's in order
to ensure that the SHT_NULL entry is present. Now we just add it
unconditionally.

The proliferation of small numerical adjustments is beginning to
frighten me, but I can't think of a way having a single point of truth
for them without introducing a whole new layer  of data structures (i.e.
lots of code and complexity) between the YAML and binary ELF formats.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@184260 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sean Silva
2013-06-18 23:37:23 +00:00
parent ca0170ffa1
commit c2b6adff3e

View File

@@ -209,27 +209,23 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Header.e_shentsize = sizeof(Elf_Shdr); Header.e_shentsize = sizeof(Elf_Shdr);
// Immediately following the ELF header. // Immediately following the ELF header.
Header.e_shoff = sizeof(Header); Header.e_shoff = sizeof(Header);
std::vector<ELFYAML::Section> Sections = Doc.Sections; const std::vector<ELFYAML::Section> &Sections = Doc.Sections;
if (Sections.empty() || Sections.front().Type != SHT_NULL) { // "+ 3" for
ELFYAML::Section S; // - SHT_NULL entry (placed first, i.e. 0'th entry)
S.Type = SHT_NULL; // - string table (.strtab) (placed second to last)
zero(S.Flags); // - section header string table. (placed last)
zero(S.Address); Header.e_shnum = Sections.size() + 3;
zero(S.AddressAlign);
Sections.insert(Sections.begin(), S);
}
// "+ 2" for string table and section header string table.
Header.e_shnum = Sections.size() + 2;
// Place section header string table last. // Place section header string table last.
Header.e_shstrndx = Sections.size() + 1; Header.e_shstrndx = Header.e_shnum - 1;
const unsigned DotStrtabSecNo = Sections.size(); const unsigned DotStrtabSecNo = Header.e_shnum - 2;
SectionNameToIdxMap SN2I; SectionNameToIdxMap SN2I;
for (unsigned i = 0, e = Sections.size(); i != e; ++i) { for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
StringRef Name = Sections[i].Name; StringRef Name = Sections[i].Name;
if (Name.empty()) if (Name.empty())
continue; continue;
if (SN2I.addName(Name, i)) { // "+ 1" to take into account the SHT_NULL entry.
if (SN2I.addName(Name, i + 1)) {
errs() << "error: Repeated section name: '" << Name errs() << "error: Repeated section name: '" << Name
<< "' at YAML section number " << i << ".\n"; << "' at YAML section number " << i << ".\n";
return 1; return 1;
@@ -244,6 +240,13 @@ static int writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
Header.e_ehsize + Header.e_shentsize * Header.e_shnum; Header.e_ehsize + Header.e_shentsize * Header.e_shnum;
ContiguousBlobAccumulator CBA(SectionContentBeginOffset, Buf); ContiguousBlobAccumulator CBA(SectionContentBeginOffset, Buf);
std::vector<Elf_Shdr> SHeaders; std::vector<Elf_Shdr> SHeaders;
{
// Ensure SHN_UNDEF entry is present. An all-zero section header is a
// valid SHN_UNDEF entry since SHT_NULL == 0.
Elf_Shdr SHdr;
zero(SHdr);
SHeaders.push_back(SHdr);
}
StringTableBuilder DotStrTab; StringTableBuilder DotStrTab;
for (unsigned i = 0, e = Sections.size(); i != e; ++i) { for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
const ELFYAML::Section &Sec = Sections[i]; const ELFYAML::Section &Sec = Sections[i];