[llvm-objcopy] Fix latent bug that allowed some Sections to be improperly cast to StringTableSections
If a Section had Type SHT_STRTAB (which could happen if you had a
.dynstr section) it was possible to cast Section to StringTableSection
and get away with any operation that was supported by SectionBase
without it being noticed. This change makes this bug easier to notice
and fixes it where it occurred. It also made me realize that there was
some duplication of efforts in the loop that calls ::initialize. These
issues are all fixed by this change.
Differential Revision: https://reviews.llvm.org/D38329
llvm-svn: 315372
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp
index bb79e42..2bd3aa1 100644
--- a/llvm/tools/llvm-objcopy/Object.cpp
+++ b/llvm/tools/llvm-objcopy/Object.cpp
@@ -289,11 +289,14 @@
}
void SectionWithStrTab::initialize(SectionTableRef SecTable) {
- setStrTab(SecTable.getSectionOfType<StringTableSection>(
- Link,
- "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
- "Link field value " + Twine(Link) + " in section " + Name +
- " is not a string table"));
+ auto StrTab = SecTable.getSection(Link,
+ "Link field value " + Twine(Link) +
+ " in section " + Name + " is invalid");
+ if (StrTab->Type != SHT_STRTAB) {
+ error("Link field value " + Twine(Link) + " in section " + Name +
+ " is not a string table");
+ }
+ setStrTab(StrTab);
}
void SectionWithStrTab::finalize() { this->Link = StrTab->Index; }
@@ -535,15 +538,6 @@
initRelocations(RelSec, SymbolTable,
unwrapOrError(ElfFile.relas(Shdr)));
}
-
- if (auto Sec = dyn_cast<SectionWithStrTab>(Section.get())) {
- Sec->setStrTab(SecTable.getSectionOfType<StringTableSection>(
- Sec->Link,
- "Link field value " + Twine(Sec->Link) + " in section " + Sec->Name +
- " is invalid",
- "Link field value " + Twine(Sec->Link) + " in section " + Sec->Name +
- " is not a string table"));
- }
}
return SecTable;
@@ -621,7 +615,7 @@
template <class ELFT>
void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
for (auto &Section : Sections)
- Section->writeSection(Out);
+ Section->writeSection(Out);
}
template <class ELFT>