Path: blob/main/contrib/llvm-project/llvm/lib/DWP/DWP.cpp
35233 views
//===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF9// package files).10//11//===----------------------------------------------------------------------===//12#include "llvm/DWP/DWP.h"13#include "llvm/ADT/Twine.h"14#include "llvm/DWP/DWPError.h"15#include "llvm/MC/MCContext.h"16#include "llvm/MC/MCObjectFileInfo.h"17#include "llvm/MC/MCTargetOptionsCommandFlags.h"18#include "llvm/Object/Decompressor.h"19#include "llvm/Object/ELFObjectFile.h"20#include "llvm/Support/CommandLine.h"21#include "llvm/Support/MemoryBuffer.h"22#include <limits>2324using namespace llvm;25using namespace llvm::object;2627static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;2829// Returns the size of debug_str_offsets section headers in bytes.30static uint64_t debugStrOffsetsHeaderSize(DataExtractor StrOffsetsData,31uint16_t DwarfVersion) {32if (DwarfVersion <= 4)33return 0; // There is no header before dwarf 5.34uint64_t Offset = 0;35uint64_t Length = StrOffsetsData.getU32(&Offset);36if (Length == llvm::dwarf::DW_LENGTH_DWARF64)37return 16; // unit length: 12 bytes, version: 2 bytes, padding: 2 bytes.38return 8; // unit length: 4 bytes, version: 2 bytes, padding: 2 bytes.39}4041static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {42uint64_t Offset = 0;43DataExtractor AbbrevData(Abbrev, true, 0);44while (AbbrevData.getULEB128(&Offset) != AbbrCode) {45// Tag46AbbrevData.getULEB128(&Offset);47// DW_CHILDREN48AbbrevData.getU8(&Offset);49// Attributes50while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))51;52}53return Offset;54}5556static Expected<const char *>57getIndexedString(dwarf::Form Form, DataExtractor InfoData, uint64_t &InfoOffset,58StringRef StrOffsets, StringRef Str, uint16_t Version) {59if (Form == dwarf::DW_FORM_string)60return InfoData.getCStr(&InfoOffset);61uint64_t StrIndex;62switch (Form) {63case dwarf::DW_FORM_strx1:64StrIndex = InfoData.getU8(&InfoOffset);65break;66case dwarf::DW_FORM_strx2:67StrIndex = InfoData.getU16(&InfoOffset);68break;69case dwarf::DW_FORM_strx3:70StrIndex = InfoData.getU24(&InfoOffset);71break;72case dwarf::DW_FORM_strx4:73StrIndex = InfoData.getU32(&InfoOffset);74break;75case dwarf::DW_FORM_strx:76case dwarf::DW_FORM_GNU_str_index:77StrIndex = InfoData.getULEB128(&InfoOffset);78break;79default:80return make_error<DWPError>(81"string field must be encoded with one of the following: "82"DW_FORM_string, DW_FORM_strx, DW_FORM_strx1, DW_FORM_strx2, "83"DW_FORM_strx3, DW_FORM_strx4, or DW_FORM_GNU_str_index.");84}85DataExtractor StrOffsetsData(StrOffsets, true, 0);86uint64_t StrOffsetsOffset = 4 * StrIndex;87StrOffsetsOffset += debugStrOffsetsHeaderSize(StrOffsetsData, Version);8889uint64_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);90DataExtractor StrData(Str, true, 0);91return StrData.getCStr(&StrOffset);92}9394static Expected<CompileUnitIdentifiers>95getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,96StringRef Info, StringRef StrOffsets, StringRef Str) {97DataExtractor InfoData(Info, true, 0);98uint64_t Offset = Header.HeaderSize;99if (Header.Version >= 5 && Header.UnitType != dwarf::DW_UT_split_compile)100return make_error<DWPError>(101std::string("unit type DW_UT_split_compile type not found in "102"debug_info header. Unexpected unit type 0x" +103utostr(Header.UnitType) + " found"));104105CompileUnitIdentifiers ID;106107uint32_t AbbrCode = InfoData.getULEB128(&Offset);108DataExtractor AbbrevData(Abbrev, true, 0);109uint64_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);110auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));111if (Tag != dwarf::DW_TAG_compile_unit)112return make_error<DWPError>("top level DIE is not a compile unit");113// DW_CHILDREN114AbbrevData.getU8(&AbbrevOffset);115uint32_t Name;116dwarf::Form Form;117while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |118(Form = static_cast<dwarf::Form>(119AbbrevData.getULEB128(&AbbrevOffset))) &&120(Name != 0 || Form != 0)) {121switch (Name) {122case dwarf::DW_AT_name: {123Expected<const char *> EName = getIndexedString(124Form, InfoData, Offset, StrOffsets, Str, Header.Version);125if (!EName)126return EName.takeError();127ID.Name = *EName;128break;129}130case dwarf::DW_AT_GNU_dwo_name:131case dwarf::DW_AT_dwo_name: {132Expected<const char *> EName = getIndexedString(133Form, InfoData, Offset, StrOffsets, Str, Header.Version);134if (!EName)135return EName.takeError();136ID.DWOName = *EName;137break;138}139case dwarf::DW_AT_GNU_dwo_id:140Header.Signature = InfoData.getU64(&Offset);141break;142default:143DWARFFormValue::skipValue(144Form, InfoData, &Offset,145dwarf::FormParams({Header.Version, Header.AddrSize, Header.Format}));146}147}148if (!Header.Signature)149return make_error<DWPError>("compile unit missing dwo_id");150ID.Signature = *Header.Signature;151return ID;152}153154static bool isSupportedSectionKind(DWARFSectionKind Kind) {155return Kind != DW_SECT_EXT_unknown;156}157158namespace llvm {159// Convert an internal section identifier into the index to use with160// UnitIndexEntry::Contributions.161unsigned getContributionIndex(DWARFSectionKind Kind, uint32_t IndexVersion) {162assert(serializeSectionKind(Kind, IndexVersion) >= DW_SECT_INFO);163return serializeSectionKind(Kind, IndexVersion) - DW_SECT_INFO;164}165} // namespace llvm166167// Convert a UnitIndexEntry::Contributions index to the corresponding on-disk168// value of the section identifier.169static unsigned getOnDiskSectionId(unsigned Index) {170return Index + DW_SECT_INFO;171}172173static StringRef getSubsection(StringRef Section,174const DWARFUnitIndex::Entry &Entry,175DWARFSectionKind Kind) {176const auto *Off = Entry.getContribution(Kind);177if (!Off)178return StringRef();179return Section.substr(Off->getOffset(), Off->getLength());180}181182static Error sectionOverflowErrorOrWarning(uint32_t PrevOffset,183uint32_t OverflowedOffset,184StringRef SectionName,185OnCuIndexOverflow OverflowOptValue,186bool &AnySectionOverflow) {187std::string Msg =188(SectionName +189Twine(" Section Contribution Offset overflow 4G. Previous Offset ") +190Twine(PrevOffset) + Twine(", After overflow offset ") +191Twine(OverflowedOffset) + Twine("."))192.str();193if (OverflowOptValue == OnCuIndexOverflow::Continue) {194WithColor::defaultWarningHandler(make_error<DWPError>(Msg));195return Error::success();196} else if (OverflowOptValue == OnCuIndexOverflow::SoftStop) {197AnySectionOverflow = true;198WithColor::defaultWarningHandler(make_error<DWPError>(Msg));199return Error::success();200}201return make_error<DWPError>(Msg);202}203204static Error addAllTypesFromDWP(205MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,206const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,207const UnitIndexEntry &TUEntry, uint32_t &TypesOffset,208unsigned TypesContributionIndex, OnCuIndexOverflow OverflowOptValue,209bool &AnySectionOverflow) {210Out.switchSection(OutputTypes);211for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {212auto *I = E.getContributions();213if (!I)214continue;215auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));216if (!P.second)217continue;218auto &Entry = P.first->second;219// Zero out the debug_info contribution220Entry.Contributions[0] = {};221for (auto Kind : TUIndex.getColumnKinds()) {222if (!isSupportedSectionKind(Kind))223continue;224auto &C =225Entry.Contributions[getContributionIndex(Kind, TUIndex.getVersion())];226C.setOffset(C.getOffset() + I->getOffset());227C.setLength(I->getLength());228++I;229}230auto &C = Entry.Contributions[TypesContributionIndex];231Out.emitBytes(Types.substr(232C.getOffset() -233TUEntry.Contributions[TypesContributionIndex].getOffset(),234C.getLength()));235C.setOffset(TypesOffset);236uint32_t OldOffset = TypesOffset;237static_assert(sizeof(OldOffset) == sizeof(TypesOffset));238TypesOffset += C.getLength();239if (OldOffset > TypesOffset) {240if (Error Err = sectionOverflowErrorOrWarning(OldOffset, TypesOffset,241"Types", OverflowOptValue,242AnySectionOverflow))243return Err;244if (AnySectionOverflow) {245TypesOffset = OldOffset;246return Error::success();247}248}249}250return Error::success();251}252253static Error addAllTypesFromTypesSection(254MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,255MCSection *OutputTypes, const std::vector<StringRef> &TypesSections,256const UnitIndexEntry &CUEntry, uint32_t &TypesOffset,257OnCuIndexOverflow OverflowOptValue, bool &AnySectionOverflow) {258for (StringRef Types : TypesSections) {259Out.switchSection(OutputTypes);260uint64_t Offset = 0;261DataExtractor Data(Types, true, 0);262while (Data.isValidOffset(Offset)) {263UnitIndexEntry Entry = CUEntry;264// Zero out the debug_info contribution265Entry.Contributions[0] = {};266auto &C = Entry.Contributions[getContributionIndex(DW_SECT_EXT_TYPES, 2)];267C.setOffset(TypesOffset);268auto PrevOffset = Offset;269// Length of the unit, including the 4 byte length field.270C.setLength(Data.getU32(&Offset) + 4);271272Data.getU16(&Offset); // Version273Data.getU32(&Offset); // Abbrev offset274Data.getU8(&Offset); // Address size275auto Signature = Data.getU64(&Offset);276Offset = PrevOffset + C.getLength32();277278auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));279if (!P.second)280continue;281282Out.emitBytes(Types.substr(PrevOffset, C.getLength32()));283uint32_t OldOffset = TypesOffset;284TypesOffset += C.getLength32();285if (OldOffset > TypesOffset) {286if (Error Err = sectionOverflowErrorOrWarning(OldOffset, TypesOffset,287"Types", OverflowOptValue,288AnySectionOverflow))289return Err;290if (AnySectionOverflow) {291TypesOffset = OldOffset;292return Error::success();293}294}295}296}297return Error::success();298}299300static std::string buildDWODescription(StringRef Name, StringRef DWPName,301StringRef DWOName) {302std::string Text = "\'";303Text += Name;304Text += '\'';305bool HasDWO = !DWOName.empty();306bool HasDWP = !DWPName.empty();307if (HasDWO || HasDWP) {308Text += " (from ";309if (HasDWO) {310Text += '\'';311Text += DWOName;312Text += '\'';313}314if (HasDWO && HasDWP)315Text += " in ";316if (!DWPName.empty()) {317Text += '\'';318Text += DWPName;319Text += '\'';320}321Text += ")";322}323return Text;324}325326static Error createError(StringRef Name, Error E) {327return make_error<DWPError>(328("failure while decompressing compressed section: '" + Name + "', " +329llvm::toString(std::move(E)))330.str());331}332333static Error334handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,335SectionRef Sec, StringRef Name, StringRef &Contents) {336auto *Obj = dyn_cast<ELFObjectFileBase>(Sec.getObject());337if (!Obj ||338!(static_cast<ELFSectionRef>(Sec).getFlags() & ELF::SHF_COMPRESSED))339return Error::success();340bool IsLE = isa<object::ELF32LEObjectFile>(Obj) ||341isa<object::ELF64LEObjectFile>(Obj);342bool Is64 = isa<object::ELF64LEObjectFile>(Obj) ||343isa<object::ELF64BEObjectFile>(Obj);344Expected<Decompressor> Dec = Decompressor::create(Name, Contents, IsLE, Is64);345if (!Dec)346return createError(Name, Dec.takeError());347348UncompressedSections.emplace_back();349if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))350return createError(Name, std::move(E));351352Contents = UncompressedSections.back();353return Error::success();354}355356namespace llvm {357// Parse and return the header of an info section compile/type unit.358Expected<InfoSectionUnitHeader> parseInfoSectionUnitHeader(StringRef Info) {359InfoSectionUnitHeader Header;360Error Err = Error::success();361uint64_t Offset = 0;362DWARFDataExtractor InfoData(Info, true, 0);363std::tie(Header.Length, Header.Format) =364InfoData.getInitialLength(&Offset, &Err);365if (Err)366return make_error<DWPError>("cannot parse compile unit length: " +367llvm::toString(std::move(Err)));368369if (!InfoData.isValidOffset(Offset + (Header.Length - 1))) {370return make_error<DWPError>(371"compile unit exceeds .debug_info section range: " +372utostr(Offset + Header.Length) + " >= " + utostr(InfoData.size()));373}374375Header.Version = InfoData.getU16(&Offset, &Err);376if (Err)377return make_error<DWPError>("cannot parse compile unit version: " +378llvm::toString(std::move(Err)));379380uint64_t MinHeaderLength;381if (Header.Version >= 5) {382// Size: Version (2), UnitType (1), AddrSize (1), DebugAbbrevOffset (4),383// Signature (8)384MinHeaderLength = 16;385} else {386// Size: Version (2), DebugAbbrevOffset (4), AddrSize (1)387MinHeaderLength = 7;388}389if (Header.Length < MinHeaderLength) {390return make_error<DWPError>("unit length is too small: expected at least " +391utostr(MinHeaderLength) + " got " +392utostr(Header.Length) + ".");393}394if (Header.Version >= 5) {395Header.UnitType = InfoData.getU8(&Offset);396Header.AddrSize = InfoData.getU8(&Offset);397Header.DebugAbbrevOffset = InfoData.getU32(&Offset);398Header.Signature = InfoData.getU64(&Offset);399if (Header.UnitType == dwarf::DW_UT_split_type) {400// Type offset.401MinHeaderLength += 4;402if (Header.Length < MinHeaderLength)403return make_error<DWPError>("type unit is missing type offset");404InfoData.getU32(&Offset);405}406} else {407// Note that, address_size and debug_abbrev_offset fields have switched408// places between dwarf version 4 and 5.409Header.DebugAbbrevOffset = InfoData.getU32(&Offset);410Header.AddrSize = InfoData.getU8(&Offset);411}412413Header.HeaderSize = Offset;414return Header;415}416417static void writeNewOffsetsTo(MCStreamer &Out, DataExtractor &Data,418DenseMap<uint64_t, uint32_t> &OffsetRemapping,419uint64_t &Offset, uint64_t &Size) {420421while (Offset < Size) {422auto OldOffset = Data.getU32(&Offset);423auto NewOffset = OffsetRemapping[OldOffset];424Out.emitIntValue(NewOffset, 4);425}426}427428void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,429MCSection *StrOffsetSection,430StringRef CurStrSection,431StringRef CurStrOffsetSection, uint16_t Version) {432// Could possibly produce an error or warning if one of these was non-null but433// the other was null.434if (CurStrSection.empty() || CurStrOffsetSection.empty())435return;436437DenseMap<uint64_t, uint32_t> OffsetRemapping;438439DataExtractor Data(CurStrSection, true, 0);440uint64_t LocalOffset = 0;441uint64_t PrevOffset = 0;442while (const char *S = Data.getCStr(&LocalOffset)) {443OffsetRemapping[PrevOffset] =444Strings.getOffset(S, LocalOffset - PrevOffset);445PrevOffset = LocalOffset;446}447448Data = DataExtractor(CurStrOffsetSection, true, 0);449450Out.switchSection(StrOffsetSection);451452uint64_t Offset = 0;453uint64_t Size = CurStrOffsetSection.size();454if (Version > 4) {455while (Offset < Size) {456uint64_t HeaderSize = debugStrOffsetsHeaderSize(Data, Version);457assert(HeaderSize <= Size - Offset &&458"StrOffsetSection size is less than its header");459460uint64_t ContributionEnd = 0;461uint64_t ContributionSize = 0;462uint64_t HeaderLengthOffset = Offset;463if (HeaderSize == 8) {464ContributionSize = Data.getU32(&HeaderLengthOffset);465} else if (HeaderSize == 16) {466HeaderLengthOffset += 4; // skip the dwarf64 marker467ContributionSize = Data.getU64(&HeaderLengthOffset);468}469ContributionEnd = ContributionSize + HeaderLengthOffset;470Out.emitBytes(Data.getBytes(&Offset, HeaderSize));471writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, ContributionEnd);472}473474} else {475writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, Size);476}477}478479enum AccessField { Offset, Length };480void writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,481const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,482const AccessField &Field) {483for (const auto &E : IndexEntries)484for (size_t I = 0; I != std::size(E.second.Contributions); ++I)485if (ContributionOffsets[I])486Out.emitIntValue((Field == AccessField::Offset487? E.second.Contributions[I].getOffset32()488: E.second.Contributions[I].getLength32()),4894);490}491492void writeIndex(MCStreamer &Out, MCSection *Section,493ArrayRef<unsigned> ContributionOffsets,494const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,495uint32_t IndexVersion) {496if (IndexEntries.empty())497return;498499unsigned Columns = 0;500for (auto &C : ContributionOffsets)501if (C)502++Columns;503504std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));505uint64_t Mask = Buckets.size() - 1;506size_t I = 0;507for (const auto &P : IndexEntries) {508auto S = P.first;509auto H = S & Mask;510auto HP = ((S >> 32) & Mask) | 1;511while (Buckets[H]) {512assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&513"Duplicate unit");514H = (H + HP) & Mask;515}516Buckets[H] = I + 1;517++I;518}519520Out.switchSection(Section);521Out.emitIntValue(IndexVersion, 4); // Version522Out.emitIntValue(Columns, 4); // Columns523Out.emitIntValue(IndexEntries.size(), 4); // Num Units524Out.emitIntValue(Buckets.size(), 4); // Num Buckets525526// Write the signatures.527for (const auto &I : Buckets)528Out.emitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);529530// Write the indexes.531for (const auto &I : Buckets)532Out.emitIntValue(I, 4);533534// Write the column headers (which sections will appear in the table)535for (size_t I = 0; I != ContributionOffsets.size(); ++I)536if (ContributionOffsets[I])537Out.emitIntValue(getOnDiskSectionId(I), 4);538539// Write the offsets.540writeIndexTable(Out, ContributionOffsets, IndexEntries, AccessField::Offset);541542// Write the lengths.543writeIndexTable(Out, ContributionOffsets, IndexEntries, AccessField::Length);544}545546Error buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,547const CompileUnitIdentifiers &ID, StringRef DWPName) {548return make_error<DWPError>(549std::string("duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +550buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,551PrevE.second.DWOName) +552" and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));553}554555Error handleSection(556const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,557const MCSection *StrSection, const MCSection *StrOffsetSection,558const MCSection *TypesSection, const MCSection *CUIndexSection,559const MCSection *TUIndexSection, const MCSection *InfoSection,560const SectionRef &Section, MCStreamer &Out,561std::deque<SmallString<32>> &UncompressedSections,562uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,563StringRef &CurStrSection, StringRef &CurStrOffsetSection,564std::vector<StringRef> &CurTypesSection,565std::vector<StringRef> &CurInfoSection, StringRef &AbbrevSection,566StringRef &CurCUIndexSection, StringRef &CurTUIndexSection,567std::vector<std::pair<DWARFSectionKind, uint32_t>> &SectionLength) {568if (Section.isBSS())569return Error::success();570571if (Section.isVirtual())572return Error::success();573574Expected<StringRef> NameOrErr = Section.getName();575if (!NameOrErr)576return NameOrErr.takeError();577StringRef Name = *NameOrErr;578579Expected<StringRef> ContentsOrErr = Section.getContents();580if (!ContentsOrErr)581return ContentsOrErr.takeError();582StringRef Contents = *ContentsOrErr;583584if (auto Err = handleCompressedSection(UncompressedSections, Section, Name,585Contents))586return Err;587588Name = Name.substr(Name.find_first_not_of("._"));589590auto SectionPair = KnownSections.find(Name);591if (SectionPair == KnownSections.end())592return Error::success();593594if (DWARFSectionKind Kind = SectionPair->second.second) {595if (Kind != DW_SECT_EXT_TYPES && Kind != DW_SECT_INFO) {596SectionLength.push_back(std::make_pair(Kind, Contents.size()));597}598599if (Kind == DW_SECT_ABBREV) {600AbbrevSection = Contents;601}602}603604MCSection *OutSection = SectionPair->second.first;605if (OutSection == StrOffsetSection)606CurStrOffsetSection = Contents;607else if (OutSection == StrSection)608CurStrSection = Contents;609else if (OutSection == TypesSection)610CurTypesSection.push_back(Contents);611else if (OutSection == CUIndexSection)612CurCUIndexSection = Contents;613else if (OutSection == TUIndexSection)614CurTUIndexSection = Contents;615else if (OutSection == InfoSection)616CurInfoSection.push_back(Contents);617else {618Out.switchSection(OutSection);619Out.emitBytes(Contents);620}621return Error::success();622}623624Error write(MCStreamer &Out, ArrayRef<std::string> Inputs,625OnCuIndexOverflow OverflowOptValue) {626const auto &MCOFI = *Out.getContext().getObjectFileInfo();627MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();628MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();629MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();630MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();631MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();632MCSection *const InfoSection = MCOFI.getDwarfInfoDWOSection();633const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {634{"debug_info.dwo", {InfoSection, DW_SECT_INFO}},635{"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_EXT_TYPES}},636{"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},637{"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},638{"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_EXT_LOC}},639{"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},640{"debug_macro.dwo", {MCOFI.getDwarfMacroDWOSection(), DW_SECT_MACRO}},641{"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},642{"debug_loclists.dwo",643{MCOFI.getDwarfLoclistsDWOSection(), DW_SECT_LOCLISTS}},644{"debug_rnglists.dwo",645{MCOFI.getDwarfRnglistsDWOSection(), DW_SECT_RNGLISTS}},646{"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},647{"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};648649MapVector<uint64_t, UnitIndexEntry> IndexEntries;650MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;651652uint32_t ContributionOffsets[8] = {};653uint16_t Version = 0;654uint32_t IndexVersion = 0;655bool AnySectionOverflow = false;656657DWPStringPool Strings(Out, StrSection);658659SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;660Objects.reserve(Inputs.size());661662std::deque<SmallString<32>> UncompressedSections;663664for (const auto &Input : Inputs) {665auto ErrOrObj = object::ObjectFile::createObjectFile(Input);666if (!ErrOrObj) {667return handleErrors(ErrOrObj.takeError(),668[&](std::unique_ptr<ECError> EC) -> Error {669return createFileError(Input, Error(std::move(EC)));670});671}672673auto &Obj = *ErrOrObj->getBinary();674Objects.push_back(std::move(*ErrOrObj));675676UnitIndexEntry CurEntry = {};677678StringRef CurStrSection;679StringRef CurStrOffsetSection;680std::vector<StringRef> CurTypesSection;681std::vector<StringRef> CurInfoSection;682StringRef AbbrevSection;683StringRef CurCUIndexSection;684StringRef CurTUIndexSection;685686// This maps each section contained in this file to its length.687// This information is later on used to calculate the contributions,688// i.e. offset and length, of each compile/type unit to a section.689std::vector<std::pair<DWARFSectionKind, uint32_t>> SectionLength;690691for (const auto &Section : Obj.sections())692if (auto Err = handleSection(693KnownSections, StrSection, StrOffsetSection, TypesSection,694CUIndexSection, TUIndexSection, InfoSection, Section, Out,695UncompressedSections, ContributionOffsets, CurEntry,696CurStrSection, CurStrOffsetSection, CurTypesSection,697CurInfoSection, AbbrevSection, CurCUIndexSection,698CurTUIndexSection, SectionLength))699return Err;700701if (CurInfoSection.empty())702continue;703704Expected<InfoSectionUnitHeader> HeaderOrErr =705parseInfoSectionUnitHeader(CurInfoSection.front());706if (!HeaderOrErr)707return HeaderOrErr.takeError();708InfoSectionUnitHeader &Header = *HeaderOrErr;709710if (Version == 0) {711Version = Header.Version;712IndexVersion = Version < 5 ? 2 : 5;713} else if (Version != Header.Version) {714return make_error<DWPError>("incompatible DWARF compile unit versions.");715}716717writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection,718CurStrOffsetSection, Header.Version);719720for (auto Pair : SectionLength) {721auto Index = getContributionIndex(Pair.first, IndexVersion);722CurEntry.Contributions[Index].setOffset(ContributionOffsets[Index]);723CurEntry.Contributions[Index].setLength(Pair.second);724uint32_t OldOffset = ContributionOffsets[Index];725ContributionOffsets[Index] += CurEntry.Contributions[Index].getLength32();726if (OldOffset > ContributionOffsets[Index]) {727uint32_t SectionIndex = 0;728for (auto &Section : Obj.sections()) {729if (SectionIndex == Index) {730if (Error Err = sectionOverflowErrorOrWarning(731OldOffset, ContributionOffsets[Index], *Section.getName(),732OverflowOptValue, AnySectionOverflow))733return Err;734}735++SectionIndex;736}737if (AnySectionOverflow)738break;739}740}741742uint32_t &InfoSectionOffset =743ContributionOffsets[getContributionIndex(DW_SECT_INFO, IndexVersion)];744if (CurCUIndexSection.empty()) {745bool FoundCUUnit = false;746Out.switchSection(InfoSection);747for (StringRef Info : CurInfoSection) {748uint64_t UnitOffset = 0;749while (Info.size() > UnitOffset) {750Expected<InfoSectionUnitHeader> HeaderOrError =751parseInfoSectionUnitHeader(Info.substr(UnitOffset, Info.size()));752if (!HeaderOrError)753return HeaderOrError.takeError();754InfoSectionUnitHeader &Header = *HeaderOrError;755756UnitIndexEntry Entry = CurEntry;757auto &C = Entry.Contributions[getContributionIndex(DW_SECT_INFO,758IndexVersion)];759C.setOffset(InfoSectionOffset);760C.setLength(Header.Length + 4);761762if (std::numeric_limits<uint32_t>::max() - InfoSectionOffset <763C.getLength32()) {764if (Error Err = sectionOverflowErrorOrWarning(765InfoSectionOffset, InfoSectionOffset + C.getLength32(),766"debug_info", OverflowOptValue, AnySectionOverflow))767return Err;768if (AnySectionOverflow) {769if (Header.Version < 5 ||770Header.UnitType == dwarf::DW_UT_split_compile)771FoundCUUnit = true;772break;773}774}775776UnitOffset += C.getLength32();777if (Header.Version < 5 ||778Header.UnitType == dwarf::DW_UT_split_compile) {779Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(780Header, AbbrevSection,781Info.substr(UnitOffset - C.getLength32(), C.getLength32()),782CurStrOffsetSection, CurStrSection);783784if (!EID)785return createFileError(Input, EID.takeError());786const auto &ID = *EID;787auto P = IndexEntries.insert(std::make_pair(ID.Signature, Entry));788if (!P.second)789return buildDuplicateError(*P.first, ID, "");790P.first->second.Name = ID.Name;791P.first->second.DWOName = ID.DWOName;792793FoundCUUnit = true;794} else if (Header.UnitType == dwarf::DW_UT_split_type) {795auto P = TypeIndexEntries.insert(796std::make_pair(*Header.Signature, Entry));797if (!P.second)798continue;799}800Out.emitBytes(801Info.substr(UnitOffset - C.getLength32(), C.getLength32()));802InfoSectionOffset += C.getLength32();803}804if (AnySectionOverflow)805break;806}807808if (!FoundCUUnit)809return make_error<DWPError>("no compile unit found in file: " + Input);810811if (IndexVersion == 2) {812// Add types from the .debug_types section from DWARF < 5.813if (Error Err = addAllTypesFromTypesSection(814Out, TypeIndexEntries, TypesSection, CurTypesSection, CurEntry,815ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES, 2)],816OverflowOptValue, AnySectionOverflow))817return Err;818}819if (AnySectionOverflow)820break;821continue;822}823824if (CurInfoSection.size() != 1)825return make_error<DWPError>("expected exactly one occurrence of a debug "826"info section in a .dwp file");827StringRef DwpSingleInfoSection = CurInfoSection.front();828829DWARFUnitIndex CUIndex(DW_SECT_INFO);830DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0);831if (!CUIndex.parse(CUIndexData))832return make_error<DWPError>("failed to parse cu_index");833if (CUIndex.getVersion() != IndexVersion)834return make_error<DWPError>("incompatible cu_index versions, found " +835utostr(CUIndex.getVersion()) +836" and expecting " + utostr(IndexVersion));837838Out.switchSection(InfoSection);839for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {840auto *I = E.getContributions();841if (!I)842continue;843auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));844StringRef CUInfoSection =845getSubsection(DwpSingleInfoSection, E, DW_SECT_INFO);846Expected<InfoSectionUnitHeader> HeaderOrError =847parseInfoSectionUnitHeader(CUInfoSection);848if (!HeaderOrError)849return HeaderOrError.takeError();850InfoSectionUnitHeader &Header = *HeaderOrError;851852Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(853Header, getSubsection(AbbrevSection, E, DW_SECT_ABBREV),854CUInfoSection,855getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),856CurStrSection);857if (!EID)858return createFileError(Input, EID.takeError());859const auto &ID = *EID;860if (!P.second)861return buildDuplicateError(*P.first, ID, Input);862auto &NewEntry = P.first->second;863NewEntry.Name = ID.Name;864NewEntry.DWOName = ID.DWOName;865NewEntry.DWPName = Input;866for (auto Kind : CUIndex.getColumnKinds()) {867if (!isSupportedSectionKind(Kind))868continue;869auto &C =870NewEntry.Contributions[getContributionIndex(Kind, IndexVersion)];871C.setOffset(C.getOffset() + I->getOffset());872C.setLength(I->getLength());873++I;874}875unsigned Index = getContributionIndex(DW_SECT_INFO, IndexVersion);876auto &C = NewEntry.Contributions[Index];877Out.emitBytes(CUInfoSection);878C.setOffset(InfoSectionOffset);879InfoSectionOffset += C.getLength32();880}881882if (!CurTUIndexSection.empty()) {883llvm::DWARFSectionKind TUSectionKind;884MCSection *OutSection;885StringRef TypeInputSection;886// Write type units into debug info section for DWARFv5.887if (Version >= 5) {888TUSectionKind = DW_SECT_INFO;889OutSection = InfoSection;890TypeInputSection = DwpSingleInfoSection;891} else {892// Write type units into debug types section for DWARF < 5.893if (CurTypesSection.size() != 1)894return make_error<DWPError>(895"multiple type unit sections in .dwp file");896897TUSectionKind = DW_SECT_EXT_TYPES;898OutSection = TypesSection;899TypeInputSection = CurTypesSection.front();900}901902DWARFUnitIndex TUIndex(TUSectionKind);903DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0);904if (!TUIndex.parse(TUIndexData))905return make_error<DWPError>("failed to parse tu_index");906if (TUIndex.getVersion() != IndexVersion)907return make_error<DWPError>("incompatible tu_index versions, found " +908utostr(TUIndex.getVersion()) +909" and expecting " + utostr(IndexVersion));910911unsigned TypesContributionIndex =912getContributionIndex(TUSectionKind, IndexVersion);913if (Error Err = addAllTypesFromDWP(914Out, TypeIndexEntries, TUIndex, OutSection, TypeInputSection,915CurEntry, ContributionOffsets[TypesContributionIndex],916TypesContributionIndex, OverflowOptValue, AnySectionOverflow))917return Err;918}919if (AnySectionOverflow)920break;921}922923if (Version < 5) {924// Lie about there being no info contributions so the TU index only includes925// the type unit contribution for DWARF < 5. In DWARFv5 the TU index has a926// contribution to the info section, so we do not want to lie about it.927ContributionOffsets[0] = 0;928}929writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,930TypeIndexEntries, IndexVersion);931932if (Version < 5) {933// Lie about the type contribution for DWARF < 5. In DWARFv5 the type934// section does not exist, so no need to do anything about this.935ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES, 2)] = 0;936// Unlie about the info contribution937ContributionOffsets[0] = 1;938}939940writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,941IndexEntries, IndexVersion);942943return Error::success();944}945} // namespace llvm946947948