diff --git a/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h b/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h --- a/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h +++ b/llvm/include/llvm/DebugInfo/Symbolize/Symbolize.h @@ -52,6 +52,8 @@ flush(); } + Expected symbolizeCode(const ObjectFile &Obj, + object::SectionedAddress ModuleOffset); Expected symbolizeCode(const std::string &ModuleName, object::SectionedAddress ModuleOffset); Expected @@ -71,7 +73,11 @@ private: // Bundles together object file with code/data and object file with // corresponding debug info. These objects can be the same. - using ObjectPair = std::pair; + using ObjectPair = std::pair; + + Expected + symbolizeCodeCommon(SymbolizableModule *Info, + object::SectionedAddress ModuleOffset); /// Returns a SymbolizableModule or an error if loading debug info failed. /// Only one attempt is made to load a module, and errors during loading are @@ -80,6 +86,11 @@ Expected getOrCreateModuleInfo(const std::string &ModuleName); + Expected + createModuleInfo(const ObjectFile *Obj, + std::unique_ptr Context, + StringRef ModuleName); + ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj, const std::string &ArchName); diff --git a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.h b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.h --- a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.h +++ b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.h @@ -31,7 +31,7 @@ class SymbolizableObjectFile : public SymbolizableModule { public: static ErrorOr> - create(object::ObjectFile *Obj, std::unique_ptr DICtx); + create(const object::ObjectFile *Obj, std::unique_ptr DICtx); DILineInfo symbolizeCode(object::SectionedAddress ModuleOffset, FunctionNameKind FNKind, @@ -68,7 +68,7 @@ /// Search for the first occurence of specified Address in ObjectFile. uint64_t getModuleSectionIndexForAddress(uint64_t Address) const; - object::ObjectFile *Module; + const object::ObjectFile *Module; std::unique_ptr DebugInfoContext; struct SymbolDesc { @@ -84,7 +84,7 @@ std::vector> Functions; std::vector> Objects; - SymbolizableObjectFile(object::ObjectFile *Obj, + SymbolizableObjectFile(const object::ObjectFile *Obj, std::unique_ptr DICtx); }; diff --git a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp --- a/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp +++ b/llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp @@ -42,7 +42,7 @@ } ErrorOr> -SymbolizableObjectFile::create(object::ObjectFile *Obj, +SymbolizableObjectFile::create(const object::ObjectFile *Obj, std::unique_ptr DICtx) { assert(DICtx); std::unique_ptr res( @@ -102,7 +102,7 @@ return std::move(res); } -SymbolizableObjectFile::SymbolizableObjectFile(ObjectFile *Obj, +SymbolizableObjectFile::SymbolizableObjectFile(const ObjectFile *Obj, std::unique_ptr DICtx) : Module(Obj), DebugInfoContext(std::move(DICtx)) {} diff --git a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp --- a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp +++ b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp @@ -52,14 +52,8 @@ namespace symbolize { Expected -LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, - object::SectionedAddress ModuleOffset) { - SymbolizableModule *Info; - if (auto InfoOrErr = getOrCreateModuleInfo(ModuleName)) - Info = InfoOrErr.get(); - else - return InfoOrErr.takeError(); - +LLVMSymbolizer::symbolizeCodeCommon(SymbolizableModule *Info, + object::SectionedAddress ModuleOffset) { // A null module means an error has already been reported. Return an empty // result. if (!Info) @@ -77,6 +71,32 @@ return LineInfo; } +Expected +LLVMSymbolizer::symbolizeCode(const ObjectFile &Obj, + object::SectionedAddress ModuleOffset) { + StringRef ModuleName = Obj.getFileName(); + auto I = Modules.find(ModuleName); + if (I != Modules.end()) + return symbolizeCodeCommon(I->second.get(), ModuleOffset); + + std::unique_ptr Context = + DWARFContext::create(Obj, nullptr, DWARFContext::defaultErrorHandler); + Expected InfoOrErr = + createModuleInfo(&Obj, std::move(Context), ModuleName); + if (!InfoOrErr) + return InfoOrErr.takeError(); + return symbolizeCodeCommon(*InfoOrErr, ModuleOffset); +} + +Expected +LLVMSymbolizer::symbolizeCode(const std::string &ModuleName, + object::SectionedAddress ModuleOffset) { + Expected InfoOrErr = getOrCreateModuleInfo(ModuleName); + if (!InfoOrErr) + return InfoOrErr.takeError(); + return symbolizeCodeCommon(*InfoOrErr, ModuleOffset); +} + Expected LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName, object::SectionedAddress ModuleOffset) { @@ -394,6 +414,23 @@ return errorCodeToError(object_error::arch_not_found); } +Expected +LLVMSymbolizer::createModuleInfo(const ObjectFile *Obj, + std::unique_ptr Context, + StringRef ModuleName) { + auto InfoOrErr = + SymbolizableObjectFile::create(Obj, std::move(Context)); + std::unique_ptr SymMod; + if (InfoOrErr) + SymMod = std::move(*InfoOrErr); + auto InsertResult = + Modules.insert(std::make_pair(ModuleName, std::move(SymMod))); + assert(InsertResult.second); + if (std::error_code EC = InfoOrErr.getError()) + return errorCodeToError(EC); + return InsertResult.first->second.get(); +} + Expected LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) { auto I = Modules.find(ModuleName); @@ -442,16 +479,7 @@ Context = DWARFContext::create(*Objects.second, nullptr, DWARFContext::defaultErrorHandler, Opts.DWPName); - auto InfoOrErr = - SymbolizableObjectFile::create(Objects.first, std::move(Context)); - std::unique_ptr SymMod; - if (InfoOrErr) - SymMod = std::move(InfoOrErr.get()); - auto InsertResult = Modules.emplace(ModuleName, std::move(SymMod)); - assert(InsertResult.second); - if (auto EC = InfoOrErr.getError()) - return errorCodeToError(EC); - return InsertResult.first->second.get(); + return createModuleInfo(Objects.first, std::move(Context), ModuleName); } namespace { diff --git a/llvm/test/tools/llvm-objdump/X86/disassemble-archive-with-source.ll b/llvm/test/tools/llvm-objdump/X86/disassemble-archive-with-source.ll new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objdump/X86/disassemble-archive-with-source.ll @@ -0,0 +1,49 @@ +; This test checks that 'llvm-objdump --source' works with archive files. +; The reason this test is written in .ll is that showing source code +; requires the debug data to have the source file path. Since we create +; the source file ad-hoc in this test, we don't know the path beforehand to +; create the object and archive. + +; RUN: rm -rf %t && mkdir -p %t/subdir && cd %t +; RUN: echo -e "int foo(int a)\n\n{ return a+1; }" > subdir/a.c +; RUN: sed -e "s,DIRNAME,%/t/subdir," %s | llc --filetype=obj -mtriple=x86_64-pc-linux -o a.o +; RUN: llvm-ar rc a.a a.o +; RUN: rm a.o +; RUN: llvm-objdump --source a.a | FileCheck %s + +; CHECK: { return a+1; } + +define i32 @foo(i32 %a) #0 !dbg !8 { +entry: + %a.addr = alloca i32, align 4 + store i32 %a, i32* %a.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %a.addr, metadata !12, metadata !DIExpression()), !dbg !13 + %0 = load i32, i32* %a.addr, align 4, !dbg !14 + %add = add nsw i32 %0, 1, !dbg !14 + ret i32 %add, !dbg !14 +} + +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { noinline nounwind optnone sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!3, !4, !5, !6} +!llvm.ident = !{!7} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None) +!1 = !DIFile(filename: "a.c", directory: "DIRNAME") +!2 = !{} +!3 = !{i32 2, !"Dwarf Version", i32 4} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"wchar_size", i32 2} +!6 = !{i32 7, !"PIC Level", i32 2} +!7 = !{!"clang version 9.0.0"} +!8 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 1, type: !9, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2) +!9 = !DISubroutineType(types: !10) +!10 = !{!11, !11} +!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!12 = !DILocalVariable(name: "a", arg: 1, scope: !8, file: !1, line: 1, type: !11) +!13 = !DILocation(line: 1, scope: !8) +!14 = !DILocation(line: 3, scope: !8) diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -579,8 +579,7 @@ return; DILineInfo LineInfo = DILineInfo(); - auto ExpectedLineInfo = - Symbolizer->symbolizeCode(Obj->getFileName(), Address); + auto ExpectedLineInfo = Symbolizer->symbolizeCode(*Obj, Address); if (!ExpectedLineInfo) consumeError(ExpectedLineInfo.takeError()); else