diff --git a/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h b/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h --- a/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h +++ b/llvm/lib/DWARFLinkerParallel/DWARFLinkerCompileUnit.h @@ -47,15 +47,18 @@ CompileUnit(LinkingGlobalData &GlobalData, unsigned ID, StringRef ClangModuleName, DWARFFile &File, - OffsetToUnitTy UnitFromOffset) + OffsetToUnitTy UnitFromOffset, dwarf::FormParams Format, + support::endianness Endianess) : DwarfUnit(GlobalData, ID, ClangModuleName), File(File), getUnitFromOffset(UnitFromOffset), Stage(Stage::CreatedNotLoaded) { UnitName = File.FileName; + setOutputFormat(Format, Endianess); } CompileUnit(LinkingGlobalData &GlobalData, DWARFUnit &OrigUnit, unsigned ID, StringRef ClangModuleName, DWARFFile &File, - OffsetToUnitTy UnitFromOffset) + OffsetToUnitTy UnitFromOffset, dwarf::FormParams Format, + support::endianness Endianess) : DwarfUnit(GlobalData, ID, ClangModuleName), File(File), OrigUnit(&OrigUnit), getUnitFromOffset(UnitFromOffset), Stage(Stage::CreatedNotLoaded) { @@ -63,7 +66,7 @@ if (!CUDie) return; - setOutputFormat(OrigUnit); + setOutputFormat(Format, Endianess); Language = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language), 0); if (const char *CUName = CUDie.getName(DINameKind::ShortName)) diff --git a/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h b/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h --- a/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h +++ b/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.h @@ -223,6 +223,12 @@ if (File.Dwarf) { if (!File.Dwarf->compile_units().empty()) CompileUnits.reserve(File.Dwarf->getNumCompileUnits()); + + // Set context format&endianness based on the input file. + Format.Version = File.Dwarf->getMaxVersion(); + Format.AddrSize = File.Dwarf->getCUAddrSize(); + Endianness = File.Dwarf->isLittleEndian() ? support::endianness::little + : support::endianness::big; } } diff --git a/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp b/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp --- a/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp +++ b/llvm/lib/DWARFLinkerParallel/DWARFLinkerImpl.cpp @@ -62,12 +62,21 @@ if (Error Err = validateAndUpdateOptions()) return Err; - std::optional Format; - std::optional Endianess; + dwarf::FormParams GlobalFormat = {GlobalData.getOptions().TargetDWARFVersion, + 0, dwarf::DwarfFormat::DWARF32}; + support::endianness GlobalEndianness = support::endian::system_endianness(); + + if (TheDwarfEmitter) { + GlobalEndianness = TheDwarfEmitter->getTargetTriple().isLittleEndian() + ? support::endianness::little + : support::endianness::big; + } for (std::unique_ptr &Context : ObjectContexts) { - if (Context->InputDWARFFile.Dwarf.get() == nullptr) + if (Context->InputDWARFFile.Dwarf.get() == nullptr) { + Context->setOutputFormat(Context->getFormParams(), GlobalEndianness); continue; + } if (GlobalData.getOptions().Verbose) { outs() << "OBJECT: " << Context->InputDWARFFile.FileName << "\n"; @@ -86,26 +95,23 @@ if (GlobalData.getOptions().VerifyInputDWARF) verifyInput(Context->InputDWARFFile); - for (const std::unique_ptr &OrigCU : - Context->InputDWARFFile.Dwarf->compile_units()) { - if (!Format) - Format = OrigCU.get()->getFormParams(); - } + if (!TheDwarfEmitter) + GlobalEndianness = Context->getEndianness(); + GlobalFormat.AddrSize = + std::max(GlobalFormat.AddrSize, Context->getFormParams().AddrSize); - if (!Endianess) - Endianess = Context->InputDWARFFile.Dwarf->isLittleEndian() - ? support::endianness::little - : support::endianness::big; + Context->setOutputFormat(Context->getFormParams(), GlobalEndianness); } - if (!Format) - Format = {GlobalData.getOptions().TargetDWARFVersion, 8, - dwarf::DwarfFormat::DWARF32}; - Format->Version = GlobalData.getOptions().TargetDWARFVersion; - if (!Endianess) - Endianess = support::endianness::little; + if (GlobalFormat.AddrSize == 0) { + if (TheDwarfEmitter) + GlobalFormat.AddrSize = + TheDwarfEmitter->getTargetTriple().isArch32Bit() ? 4 : 8; + else + GlobalFormat.AddrSize = 8; + } - CommonSections.setOutputFormat(*Format, *Endianess); + CommonSections.setOutputFormat(GlobalFormat, GlobalEndianness); // Set parallel options. if (GlobalData.getOptions().Threads == 0) @@ -350,7 +356,7 @@ // Add this module. Unit = std::make_unique( GlobalData, *CU, UniqueUnitID.fetch_add(1), ModuleName, *ErrOrObj, - getUnitForOffset); + getUnitForOffset, CU->getFormParams(), getEndianness()); } } @@ -403,7 +409,7 @@ !isClangModuleRef(CUDie, PCMFile, 0, true).first) { CompileUnits.emplace_back(std::make_unique( GlobalData, *OrigCU, UniqueUnitID.fetch_add(1), "", InputDWARFFile, - getUnitForOffset)); + getUnitForOffset, OrigCU->getFormParams(), getEndianness())); // Preload line table, as it can't be loaded asynchronously. CompileUnits.back()->loadLineTable(); @@ -710,10 +716,9 @@ } Error DWARFLinkerImpl::LinkContext::cloneAndEmitPaperTrails() { - - CompileUnits.emplace_back( - std::make_unique(GlobalData, UniqueUnitID.fetch_add(1), "", - InputDWARFFile, getUnitForOffset)); + CompileUnits.emplace_back(std::make_unique( + GlobalData, UniqueUnitID.fetch_add(1), "", InputDWARFFile, + getUnitForOffset, Format, Endianness)); CompileUnit &CU = *CompileUnits.back(); diff --git a/llvm/lib/DWARFLinkerParallel/OutputSections.h b/llvm/lib/DWARFLinkerParallel/OutputSections.h --- a/llvm/lib/DWARFLinkerParallel/OutputSections.h +++ b/llvm/lib/DWARFLinkerParallel/OutputSections.h @@ -287,18 +287,11 @@ public: OutputSections(LinkingGlobalData &GlobalData) : GlobalData(GlobalData) {} - /// Sets output format for all keeping sections. - void setOutputFormat(DWARFUnit &OriginalUnit) { - setOutputFormat(OriginalUnit.getFormParams(), - OriginalUnit.isLittleEndian() ? support::endianness::little - : support::endianness::big); - } - /// Sets output format for all keeping sections. void setOutputFormat(dwarf::FormParams Format, - support::endianness Endianess) { + support::endianness Endianness) { this->Format = Format; - this->Endianess = Endianess; + this->Endianness = Endianness; } /// Returns descriptor for the specified section of \p SectionKind. @@ -328,7 +321,7 @@ SectionDescriptor & getOrCreateSectionDescriptor(DebugSectionKind SectionKind) { return SectionDescriptors - .try_emplace(SectionKind, SectionKind, GlobalData, Format, Endianess) + .try_emplace(SectionKind, SectionKind, GlobalData, Format, Endianness) .first->second; } @@ -363,7 +356,7 @@ StringEntryToDwarfStringPoolEntryMap &DebugLineStrStrings); /// Endiannes for the sections. - support::endianness getEndianness() const { return Endianess; } + support::endianness getEndianness() const { return Endianness; } /// Return DWARF version. uint16_t getVersion() const { return Format.Version; } @@ -395,7 +388,7 @@ dwarf::FormParams Format = {4, 4, dwarf::DWARF32}; /// Endiannes for sections. - support::endianness Endianess = support::endianness::little; + support::endianness Endianness = support::endian::system_endianness(); /// All keeping sections. using SectionsSetTy = std::map; diff --git a/llvm/test/tools/dsymutil/X86/location-expression.test b/llvm/test/tools/dsymutil/X86/location-expression.test --- a/llvm/test/tools/dsymutil/X86/location-expression.test +++ b/llvm/test/tools/dsymutil/X86/location-expression.test @@ -11,7 +11,7 @@ # RUN: echo ' - { sym: __Z3foov, objAddr: 0x0, binAddr: 0x10000, size: 0x10 }' >> %t2.map # RUN: echo '...' >> %t2.map # RUN: dsymutil -y %t2.map -f -o - | llvm-dwarfdump -a --verbose - | FileCheck %s -# COM: dsymutil --linker llvm -y %t2.map -f -o - | llvm-dwarfdump -a --verbose - | FileCheck %s +# RUN: dsymutil --linker llvm -y %t2.map -f -o - | llvm-dwarfdump -a --verbose - | FileCheck %s # CHECK: file format Mach-O 64-bit x86-64 # CHECK: .debug_info contents: diff --git a/llvm/test/tools/dsymutil/X86/tls-variable.test b/llvm/test/tools/dsymutil/X86/tls-variable.test --- a/llvm/test/tools/dsymutil/X86/tls-variable.test +++ b/llvm/test/tools/dsymutil/X86/tls-variable.test @@ -10,7 +10,7 @@ # RUN: echo ' - { sym: __Z3foov, objAddr: 0x0, binAddr: 0x10000, size: 0x10 }' >> %t2.map # RUN: echo '...' >> %t2.map # RUN: dsymutil -y %t2.map --keep-function-for-static -f -o - | llvm-dwarfdump -a - | FileCheck %s -# COM: dsymutil --linker llvm -y %t2.map --keep-function-for-static -f -o - | llvm-dwarfdump -a - | FileCheck %s +# RUN: dsymutil --linker llvm -y %t2.map --keep-function-for-static -f -o - | llvm-dwarfdump -a - | FileCheck %s # CHECK: file format Mach-O 64-bit x86-64 # CHECK: .debug_info contents: