@@ -375,39 +375,29 @@ static std::string createFileLineMsg(StringRef path, unsigned line) {
375375 return filename + lineno + " (" + path.str () + lineno + " )" ;
376376}
377377
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))
384392 return createFileLineMsg (info->FileName , info->Line );
385393
386394 // If it failed, look up again as a variable.
387395 if (std::optional<std::pair<std::string, unsigned >> fileLine =
388- file. getVariableLoc (sym.getName ()))
396+ dwarf-> getVariableLoc (sym.getName ()))
389397 return createFileLineMsg (fileLine->first , fileLine->second );
390398
391399 // 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 );
411401}
412402
413403StringRef InputFile::getNameForScript () const {
@@ -480,50 +470,41 @@ static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,
480470 prev->nextInSectionGroup = head;
481471}
482472
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+ }));
494480}
495481
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 ();
516496 }
517- }
518-
519- return getDwarf ()->getDILineInfo (offset, sectionIndex);
497+ });
498+ return dwarf.get ();
520499}
521500
522501ELFFileBase::ELFFileBase (Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef mb)
523502 : InputFile(ctx, k, mb) {
524503 this ->ekind = ekind;
525504}
526505
506+ ELFFileBase::~ELFFileBase () {}
507+
527508template <typename Elf_Shdr>
528509static const Elf_Shdr *findSection (ArrayRef<Elf_Shdr> sections, uint32_t type) {
529510 for (const Elf_Shdr &sec : sections)
0 commit comments