@@ -375,39 +375,29 @@ static std::string createFileLineMsg(StringRef path, unsigned line) {
375
375
return filename + lineno + " (" + path.str () + lineno + " )" ;
376
376
}
377
377
378
- template <class ELFT >
379
- static std::string getSrcMsgAux (ObjFile<ELFT> &file, const Symbol &sym,
380
- const InputSectionBase &sec, uint64_t offset) {
381
- // In DWARF, functions and variables are stored to different places.
382
- // First, look up a function for a given offset.
383
- if (std::optional<DILineInfo> info = file.getDILineInfo (&sec, offset))
378
+ std::string InputFile::getSrcMsg (const InputSectionBase &sec, const Symbol &sym,
379
+ uint64_t offset) {
380
+ if (kind () != ObjKind)
381
+ return " " ;
382
+
383
+ // First, look up the DWARF line table.
384
+ ArrayRef<InputSectionBase *> sections = getSections ();
385
+ auto it = llvm::find (sections, &sec);
386
+ uint64_t sectionIndex = it != sections.end ()
387
+ ? it - sections.begin ()
388
+ : object::SectionedAddress::UndefSection;
389
+ DWARFCache *dwarf = cast<ELFFileBase>(this )->getDwarf ();
390
+ if (std::optional<DILineInfo> info =
391
+ dwarf->getDILineInfo (offset, sectionIndex))
384
392
return createFileLineMsg (info->FileName , info->Line );
385
393
386
394
// If it failed, look up again as a variable.
387
395
if (std::optional<std::pair<std::string, unsigned >> fileLine =
388
- file. getVariableLoc (sym.getName ()))
396
+ dwarf-> getVariableLoc (sym.getName ()))
389
397
return createFileLineMsg (fileLine->first , fileLine->second );
390
398
391
399
// File.sourceFile contains STT_FILE symbol, and that is a last resort.
392
- return std::string (file.sourceFile );
393
- }
394
-
395
- std::string InputFile::getSrcMsg (const Symbol &sym, const InputSectionBase &sec,
396
- uint64_t offset) {
397
- if (kind () != ObjKind)
398
- return " " ;
399
- switch (ekind) {
400
- default :
401
- llvm_unreachable (" Invalid kind" );
402
- case ELF32LEKind:
403
- return getSrcMsgAux (cast<ObjFile<ELF32LE>>(*this ), sym, sec, offset);
404
- case ELF32BEKind:
405
- return getSrcMsgAux (cast<ObjFile<ELF32BE>>(*this ), sym, sec, offset);
406
- case ELF64LEKind:
407
- return getSrcMsgAux (cast<ObjFile<ELF64LE>>(*this ), sym, sec, offset);
408
- case ELF64BEKind:
409
- return getSrcMsgAux (cast<ObjFile<ELF64BE>>(*this ), sym, sec, offset);
410
- }
400
+ return std::string (cast<ELFFileBase>(this )->sourceFile );
411
401
}
412
402
413
403
StringRef InputFile::getNameForScript () const {
@@ -480,50 +470,41 @@ static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
480
470
prev->nextInSectionGroup = head;
481
471
}
482
472
483
- template <class ELFT > DWARFCache *ObjFile<ELFT>::getDwarf() {
484
- llvm::call_once (initDwarf, [this ]() {
485
- dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
486
- std::make_unique<LLDDwarfObj<ELFT>>(this ), " " ,
487
- [&](Error err) { Warn (ctx) << getName () + " : " << std::move (err); },
488
- [&](Error warning) {
489
- Warn (ctx) << getName () << " : " << std::move (warning);
490
- }));
491
- });
492
-
493
- return dwarf.get ();
473
+ template <class ELFT > void ObjFile<ELFT>::initDwarf() {
474
+ dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
475
+ std::make_unique<LLDDwarfObj<ELFT>>(this ), " " ,
476
+ [&](Error err) { Warn (ctx) << getName () + " : " << std::move (err); },
477
+ [&](Error warning) {
478
+ Warn (ctx) << getName () << " : " << std::move (warning);
479
+ }));
494
480
}
495
481
496
- // Returns the pair of file name and line number describing location of data
497
- // object (variable, array, etc) definition.
498
- template <class ELFT >
499
- std::optional<std::pair<std::string, unsigned >>
500
- ObjFile<ELFT>::getVariableLoc(StringRef name) {
501
- return getDwarf ()->getVariableLoc (name);
502
- }
503
-
504
- // Returns source line information for a given offset
505
- // using DWARF debug info.
506
- template <class ELFT >
507
- std::optional<DILineInfo>
508
- ObjFile<ELFT>::getDILineInfo(const InputSectionBase *s, uint64_t offset) {
509
- // Detect SectionIndex for specified section.
510
- uint64_t sectionIndex = object::SectionedAddress::UndefSection;
511
- ArrayRef<InputSectionBase *> sections = s->file ->getSections ();
512
- for (uint64_t curIndex = 0 ; curIndex < sections.size (); ++curIndex) {
513
- if (s == sections[curIndex]) {
514
- sectionIndex = curIndex;
515
- break ;
482
+ DWARFCache *ELFFileBase::getDwarf () {
483
+ assert (fileKind == ObjKind);
484
+ llvm::call_once (initDwarf, [this ]() {
485
+ switch (ekind) {
486
+ default :
487
+ llvm_unreachable (" " );
488
+ case ELF32LEKind:
489
+ return cast<ObjFile<ELF32LE>>(this )->initDwarf ();
490
+ case ELF32BEKind:
491
+ return cast<ObjFile<ELF32BE>>(this )->initDwarf ();
492
+ case ELF64LEKind:
493
+ return cast<ObjFile<ELF64LE>>(this )->initDwarf ();
494
+ case ELF64BEKind:
495
+ return cast<ObjFile<ELF64BE>>(this )->initDwarf ();
516
496
}
517
- }
518
-
519
- return getDwarf ()->getDILineInfo (offset, sectionIndex);
497
+ });
498
+ return dwarf.get ();
520
499
}
521
500
522
501
ELFFileBase::ELFFileBase (Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef mb)
523
502
: InputFile(ctx, k, mb) {
524
503
this ->ekind = ekind;
525
504
}
526
505
506
+ ELFFileBase::~ELFFileBase () {}
507
+
527
508
template <typename Elf_Shdr>
528
509
static const Elf_Shdr *findSection (ArrayRef<Elf_Shdr> sections, uint32_t type) {
529
510
for (const Elf_Shdr &sec : sections)
0 commit comments