@@ -228,12 +228,6 @@ void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const {
228
228
writeRel (reinterpret_cast <Elf_Rela *>(Buf));
229
229
}
230
230
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
-
237
231
// Returns true IFF a section is wholly inside the range of a segment
238
232
static bool sectionWithinSegment (const SectionBase &Section,
239
233
const Segment &Segment) {
@@ -281,20 +275,23 @@ void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
281
275
SymbolTableSection *SymTab) {
282
276
283
277
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" );
290
288
291
289
const Elf_Shdr &Shdr = *unwrapOrError (ElfFile.getSection (SymTab->Index ));
292
290
StringRef StrTabData = unwrapOrError (ElfFile.getStringTableForSymtab (Shdr));
293
291
294
292
for (const auto &Sym : unwrapOrError (ElfFile.symbols (&Shdr))) {
295
293
SectionBase *DefSection = nullptr ;
296
294
StringRef Name = unwrapOrError (Sym.getName (StrTabData));
297
-
298
295
if (Sym.st_shndx >= SHN_LORESERVE) {
299
296
if (!isValidReservedSectionIndex (Sym.st_shndx , Machine)) {
300
297
error (
@@ -303,12 +300,12 @@ void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
303
300
Twine (Sym.st_shndx ));
304
301
}
305
302
} 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 " +
309
306
Twine (Sym.st_shndx ));
307
+ DefSection = Sections[Sym.st_shndx - 1 ].get ();
310
308
}
311
-
312
309
SymTab->addSymbol (Name, Sym.getBinding (), Sym.getType (), DefSection,
313
310
Sym.getValue (), Sym.st_shndx , Sym.st_size );
314
311
}
@@ -335,22 +332,6 @@ void initRelocations(RelocationSection<ELFT> *Relocs,
335
332
}
336
333
}
337
334
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
-
354
335
template <class ELFT >
355
336
std::unique_ptr<SectionBase>
356
337
Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
@@ -361,26 +342,7 @@ Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
361
342
case SHT_RELA:
362
343
return llvm::make_unique<RelocationSection<ELFT>>();
363
344
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
- }
371
345
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);
384
346
case SHT_SYMTAB: {
385
347
auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>();
386
348
SymbolTable = SymTab.get ();
@@ -428,35 +390,28 @@ void Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) {
428
390
// relocation sections.
429
391
for (auto &Section : Sections) {
430
392
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
+ }
438
407
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 ());
445
409
auto Shdr = unwrapOrError (ElfFile.sections ()).begin () + RelSec->Index ;
446
410
if (RelSec->Type == SHT_REL)
447
411
initRelocations (RelSec, SymTab, unwrapOrError (ElfFile.rels (Shdr)));
448
412
else
449
413
initRelocations (RelSec, SymTab, unwrapOrError (ElfFile.relas (Shdr)));
450
414
}
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
- }
460
415
}
461
416
}
462
417
@@ -474,12 +429,8 @@ template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) {
474
429
readSectionHeaders (ElfFile);
475
430
readProgramHeaders (ElfFile);
476
431
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 ());
483
434
}
484
435
485
436
template <class ELFT >
0 commit comments