Skip to content

Commit 3177821

Browse files
committed
Revert "[llvm-objcopy] Add support for .dynamic, .dynsym, and .dynstr"
This reverts commit r313663. Broken because overlapping-sections was reverted. llvm-svn: 313665
1 parent 8f10824 commit 3177821

File tree

7 files changed

+31
-237
lines changed

7 files changed

+31
-237
lines changed
-8.9 KB
Binary file not shown.
-13.1 KB
Binary file not shown.

llvm/test/tools/llvm-objcopy/dynamic.test

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

llvm/test/tools/llvm-objcopy/dynstr.test

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

llvm/test/tools/llvm-objcopy/dynsym.test

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

llvm/tools/llvm-objcopy/Object.cpp

Lines changed: 31 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,6 @@ void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const {
228228
writeRel(reinterpret_cast<Elf_Rela *>(Buf));
229229
}
230230

231-
bool SectionWithStrTab::classof(const SectionBase *S) {
232-
return isa<DynamicSymbolTableSection>(S) || isa<DynamicSection>(S);
233-
}
234-
235-
void SectionWithStrTab::finalize() { this->Link = StrTab->Index; }
236-
237231
// Returns true IFF a section is wholly inside the range of a segment
238232
static bool sectionWithinSegment(const SectionBase &Section,
239233
const Segment &Segment) {
@@ -281,20 +275,23 @@ void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
281275
SymbolTableSection *SymTab) {
282276

283277
SymTab->Size = 0;
284-
SymTab->setStrTab(getSectionOfType<StringTableSection>(
285-
SymbolTable->Link,
286-
"Symbol table has link index of " + Twine(SymTab->Link) +
287-
" which is not a valid index",
288-
"Symbol table has link index of " + Twine(SymTab->Link) +
289-
" which is not a string table"));
278+
if (SymbolTable->Link - 1 >= Sections.size())
279+
error("Symbol table has link index of " + Twine(SymbolTable->Link) +
280+
" which is not a valid index");
281+
282+
if (auto StrTab =
283+
dyn_cast<StringTableSection>(Sections[SymbolTable->Link - 1].get()))
284+
SymTab->setStrTab(StrTab);
285+
else
286+
error("Symbol table has link index of " + Twine(SymbolTable->Link) +
287+
"which is not a string table");
290288

291289
const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
292290
StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
293291

294292
for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) {
295293
SectionBase *DefSection = nullptr;
296294
StringRef Name = unwrapOrError(Sym.getName(StrTabData));
297-
298295
if (Sym.st_shndx >= SHN_LORESERVE) {
299296
if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) {
300297
error(
@@ -303,12 +300,12 @@ void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
303300
Twine(Sym.st_shndx));
304301
}
305302
} else if (Sym.st_shndx != SHN_UNDEF) {
306-
DefSection = getSection(
307-
Sym.st_shndx,
308-
"Symbol '" + Name + "' is defined in invalid section with index " +
303+
if (Sym.st_shndx >= Sections.size())
304+
error("Symbol '" + Name +
305+
"' is defined in invalid section with index " +
309306
Twine(Sym.st_shndx));
307+
DefSection = Sections[Sym.st_shndx - 1].get();
310308
}
311-
312309
SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
313310
Sym.getValue(), Sym.st_shndx, Sym.st_size);
314311
}
@@ -335,22 +332,6 @@ void initRelocations(RelocationSection<ELFT> *Relocs,
335332
}
336333
}
337334

338-
template <class ELFT>
339-
SectionBase *Object<ELFT>::getSection(uint16_t Index, Twine ErrMsg) {
340-
if (Index == SHN_UNDEF || Index > Sections.size())
341-
error(ErrMsg);
342-
return Sections[Index - 1].get();
343-
}
344-
345-
template <class ELFT>
346-
template <class T>
347-
T *Object<ELFT>::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
348-
Twine TypeErrMsg) {
349-
if (T *TSec = llvm::dyn_cast<T>(getSection(Index, IndexErrMsg)))
350-
return TSec;
351-
error(TypeErrMsg);
352-
}
353-
354335
template <class ELFT>
355336
std::unique_ptr<SectionBase>
356337
Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
@@ -361,26 +342,7 @@ Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
361342
case SHT_RELA:
362343
return llvm::make_unique<RelocationSection<ELFT>>();
363344
case SHT_STRTAB:
364-
// If a string table is allocated we don't want to mess with it. That would
365-
// mean altering the memory image. There are no special link types or
366-
// anything so we can just use a Section.
367-
if (Shdr.sh_flags & SHF_ALLOC) {
368-
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
369-
return llvm::make_unique<Section>(Data);
370-
}
371345
return llvm::make_unique<StringTableSection>();
372-
case SHT_HASH:
373-
case SHT_GNU_HASH:
374-
// Hash tables should refer to SHT_DYNSYM which we're not going to change.
375-
// Because of this we don't need to mess with the hash tables either.
376-
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
377-
return llvm::make_unique<Section>(Data);
378-
case SHT_DYNSYM:
379-
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
380-
return llvm::make_unique<DynamicSymbolTableSection>(Data);
381-
case SHT_DYNAMIC:
382-
Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
383-
return llvm::make_unique<DynamicSection>(Data);
384346
case SHT_SYMTAB: {
385347
auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
386348
SymbolTable = SymTab.get();
@@ -428,35 +390,28 @@ void Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
428390
// relocation sections.
429391
for (auto &Section : Sections) {
430392
if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) {
431-
432-
auto SymTab = getSectionOfType<SymbolTableSection>(
433-
RelSec->Link,
434-
"Link field value " + Twine(RelSec->Link) + " in section " +
435-
RelSec->Name + " is invalid",
436-
"Link field value " + Twine(RelSec->Link) + " in section " +
437-
RelSec->Name + " is not a symbol table");
393+
if (RelSec->Link - 1 >= Sections.size() || RelSec->Link == 0) {
394+
error("Link field value " + Twine(RelSec->Link) + " in section " +
395+
RelSec->Name + " is invalid");
396+
}
397+
if (RelSec->Info - 1 >= Sections.size() || RelSec->Info == 0) {
398+
error("Info field value " + Twine(RelSec->Link) + " in section " +
399+
RelSec->Name + " is invalid");
400+
}
401+
auto SymTab =
402+
dyn_cast<SymbolTableSection>(Sections[RelSec->Link - 1].get());
403+
if (SymTab == nullptr) {
404+
error("Link field of relocation section " + RelSec->Name +
405+
" is not a symbol table");
406+
}
438407
RelSec->setSymTab(SymTab);
439-
440-
RelSec->setSection(getSection(RelSec->Info,
441-
"Info field value " + Twine(RelSec->Link) +
442-
" in section " + RelSec->Name +
443-
" is invalid"));
444-
408+
RelSec->setSection(Sections[RelSec->Info - 1].get());
445409
auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
446410
if (RelSec->Type == SHT_REL)
447411
initRelocations(RelSec, SymTab, unwrapOrError(ElfFile.rels(Shdr)));
448412
else
449413
initRelocations(RelSec, SymTab, unwrapOrError(ElfFile.relas(Shdr)));
450414
}
451-
452-
if (auto Sec = dyn_cast<SectionWithStrTab>(Section.get())) {
453-
Sec->setStrTab(getSectionOfType<StringTableSection>(
454-
Sec->Link,
455-
"Link field value " + Twine(Sec->Link) + " in section " + Sec->Name +
456-
" is invalid",
457-
"Link field value " + Twine(Sec->Link) + " in section " + Sec->Name +
458-
" is not a string table"));
459-
}
460415
}
461416
}
462417

@@ -474,12 +429,8 @@ template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
474429
readSectionHeaders(ElfFile);
475430
readProgramHeaders(ElfFile);
476431

477-
SectionNames = getSectionOfType<StringTableSection>(
478-
Ehdr.e_shstrndx,
479-
"e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
480-
" is invalid",
481-
"e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " +
482-
" is not a string table");
432+
SectionNames =
433+
dyn_cast<StringTableSection>(Sections[Ehdr.e_shstrndx - 1].get());
483434
}
484435

485436
template <class ELFT>

llvm/tools/llvm-objcopy/Object.h

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -193,34 +193,6 @@ template <class ELFT> class RelocationSection : public SectionBase {
193193
}
194194
};
195195

196-
class SectionWithStrTab : public Section {
197-
private:
198-
StringTableSection *StrTab;
199-
200-
public:
201-
SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {}
202-
void setStrTab(StringTableSection *StringTable) { StrTab = StringTable; }
203-
void finalize() override;
204-
static bool classof(const SectionBase *S);
205-
};
206-
207-
class DynamicSymbolTableSection : public SectionWithStrTab {
208-
public:
209-
DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data)
210-
: SectionWithStrTab(Data) {}
211-
static bool classof(const SectionBase *S) {
212-
return S->Type == llvm::ELF::SHT_DYNSYM;
213-
}
214-
};
215-
216-
class DynamicSection : public SectionWithStrTab {
217-
public:
218-
DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
219-
static bool classof(const SectionBase *S) {
220-
return S->Type == llvm::ELF::SHT_DYNAMIC;
221-
}
222-
};
223-
224196
template <class ELFT> class Object {
225197
private:
226198
typedef std::unique_ptr<SectionBase> SecPtr;
@@ -237,12 +209,6 @@ template <class ELFT> class Object {
237209
void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
238210
void readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
239211

240-
SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg);
241-
242-
template <class T>
243-
T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg,
244-
llvm::Twine TypeErrMsg);
245-
246212
protected:
247213
StringTableSection *SectionNames;
248214
SymbolTableSection *SymbolTable;

0 commit comments

Comments
 (0)