LLVM 23.0.0git
DWP.cpp
Go to the documentation of this file.
1//===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
10// package files).
11//
12//===----------------------------------------------------------------------===//
13#include "llvm/DWP/DWP.h"
14#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/Twine.h"
18#include "llvm/DWP/DWPError.h"
19#include "llvm/DWP/ELFWriter.h"
24#include "llvm/Support/LEB128.h"
26#include <limits>
27#include <optional>
28
29using namespace llvm;
30using namespace llvm::object;
31
32// Returns the size of debug_str_offsets section headers in bytes.
34 uint16_t DwarfVersion) {
35 if (DwarfVersion <= 4)
36 return 0; // There is no header before dwarf 5.
37 uint64_t Offset = 0;
38 uint64_t Length = StrOffsetsData.getU32(&Offset);
40 return 16; // unit length: 12 bytes, version: 2 bytes, padding: 2 bytes.
41 return 8; // unit length: 4 bytes, version: 2 bytes, padding: 2 bytes.
42}
43
45 uint64_t Offset = 0;
46 DataExtractor AbbrevData(Abbrev, true);
47 while (AbbrevData.isValidOffset(Offset)) {
48 uint64_t Code = AbbrevData.getULEB128(&Offset);
49 if (Code == AbbrCode)
50 return Offset;
51 // A zero abbreviation code marks the end of the abbreviation table.
52 if (Code == 0)
53 break;
54 // Tag
55 AbbrevData.getULEB128(&Offset);
56 // DW_CHILDREN
57 AbbrevData.getU8(&Offset);
58 // Attribute specifications, terminated by a (0, 0) pair.
60 dwarf::Form Form;
61 std::optional<int64_t> ImplicitConst;
62 while (readAbbrevAttribute(AbbrevData, &Offset, Name, Form, ImplicitConst))
63 ;
64 }
65 return make_error<DWPError>("abbrev code " + utostr(AbbrCode) +
66 " not found in abbrev section");
67}
68
71 StringRef StrOffsets, StringRef Str, uint16_t Version) {
72 if (Form == dwarf::DW_FORM_string)
73 return InfoData.getCStr(&InfoOffset);
74 uint64_t StrIndex;
75 switch (Form) {
76 case dwarf::DW_FORM_strx1:
77 StrIndex = InfoData.getU8(&InfoOffset);
78 break;
79 case dwarf::DW_FORM_strx2:
80 StrIndex = InfoData.getU16(&InfoOffset);
81 break;
82 case dwarf::DW_FORM_strx3:
83 StrIndex = InfoData.getU24(&InfoOffset);
84 break;
85 case dwarf::DW_FORM_strx4:
86 StrIndex = InfoData.getU32(&InfoOffset);
87 break;
88 case dwarf::DW_FORM_strx:
89 case dwarf::DW_FORM_GNU_str_index:
90 StrIndex = InfoData.getULEB128(&InfoOffset);
91 break;
92 default:
94 "string field must be encoded with one of the following: "
95 "DW_FORM_string, DW_FORM_strx, DW_FORM_strx1, DW_FORM_strx2, "
96 "DW_FORM_strx3, DW_FORM_strx4, or DW_FORM_GNU_str_index.");
97 }
98 DataExtractor StrOffsetsData(StrOffsets, true);
99 uint64_t StrOffsetsOffset = 4 * StrIndex;
100 StrOffsetsOffset += debugStrOffsetsHeaderSize(StrOffsetsData, Version);
101
102 uint64_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset);
103 DataExtractor StrData(Str, true);
104 return StrData.getCStr(&StrOffset);
105}
106
109 StringRef Info, StringRef StrOffsets, StringRef Str) {
110 DataExtractor InfoData(Info, true);
111 uint64_t Offset = Header.HeaderSize;
112 if (Header.Version >= 5 && Header.UnitType != dwarf::DW_UT_split_compile)
114 std::string("unit type DW_UT_split_compile type not found in "
115 "debug_info header. Unexpected unit type 0x" +
116 utostr(Header.UnitType) + " found"));
117
119
120 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
121 DataExtractor AbbrevData(Abbrev, true);
122 Expected<uint64_t> AbbrevOffsetOrErr = getCUAbbrev(Abbrev, AbbrCode);
123 if (!AbbrevOffsetOrErr)
124 return AbbrevOffsetOrErr.takeError();
125 uint64_t AbbrevOffset = *AbbrevOffsetOrErr;
126 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset));
127 if (Tag != dwarf::DW_TAG_compile_unit)
128 return make_error<DWPError>("top level DIE is not a compile unit");
129 // DW_CHILDREN
130 AbbrevData.getU8(&AbbrevOffset);
131 dwarf::Attribute Name;
132 dwarf::Form Form;
133 std::optional<int64_t> ImplicitConst;
134 while (readAbbrevAttribute(AbbrevData, &AbbrevOffset, Name, Form,
135 ImplicitConst)) {
136 switch (Name) {
137 case dwarf::DW_AT_name: {
139 Form, InfoData, Offset, StrOffsets, Str, Header.Version);
140 if (!EName)
141 return EName.takeError();
142 ID.Name = *EName;
143 break;
144 }
145 case dwarf::DW_AT_GNU_dwo_name:
146 case dwarf::DW_AT_dwo_name: {
148 Form, InfoData, Offset, StrOffsets, Str, Header.Version);
149 if (!EName)
150 return EName.takeError();
151 ID.DWOName = *EName;
152 break;
153 }
154 case dwarf::DW_AT_GNU_dwo_id:
155 Header.Signature = ImplicitConst ? static_cast<uint64_t>(*ImplicitConst)
156 : InfoData.getU64(&Offset);
157 break;
158 default:
160 Form, InfoData, &Offset,
161 dwarf::FormParams({Header.Version, Header.AddrSize, Header.Format}));
162 }
163 }
164 if (!Header.Signature)
165 return make_error<DWPError>("compile unit missing dwo_id");
166 ID.Signature = *Header.Signature;
167 return ID;
168}
169
173
174// Convert an internal section identifier into the index to use with
175// UnitIndexEntry::Contributions.
177 uint32_t IndexVersion) {
178 assert(serializeSectionKind(Kind, IndexVersion) >= DW_SECT_INFO);
179 return serializeSectionKind(Kind, IndexVersion) - DW_SECT_INFO;
180}
181
182// Convert a UnitIndexEntry::Contributions index to the corresponding on-disk
183// value of the section identifier.
184static unsigned getOnDiskSectionId(unsigned Index) {
185 return Index + DW_SECT_INFO;
186}
187
189 const DWARFUnitIndex::Entry &Entry,
191 const auto *Off = Entry.getContribution(Kind);
192 if (!Off)
193 return StringRef();
194 return Section.substr(Off->getOffset(), Off->getLength());
195}
196
198 uint32_t OverflowedOffset,
200 OnCuIndexOverflow OverflowOptValue,
201 bool &AnySectionOverflow) {
202 std::string Msg =
203 (SectionName +
204 Twine(" Section Contribution Offset overflow 4G. Previous Offset ") +
205 Twine(PrevOffset) + Twine(", After overflow offset ") +
206 Twine(OverflowedOffset) + Twine("."))
207 .str();
208 if (OverflowOptValue == OnCuIndexOverflow::Continue) {
210 return Error::success();
211 } else if (OverflowOptValue == OnCuIndexOverflow::SoftStop) {
212 AnySectionOverflow = true;
214 return Error::success();
215 }
216 return make_error<DWPError>(Msg);
217}
218
220 DWPWriter &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
221 const DWARFUnitIndex &TUIndex, DWPSectionId OutputSection, StringRef Types,
222 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset,
223 unsigned TypesContributionIndex, OnCuIndexOverflow OverflowOptValue,
224 bool &AnySectionOverflow) {
225 Out.switchSection(OutputSection);
226 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
227 auto *I = E.getContributions();
228 if (!I)
229 continue;
230 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
231 if (!P.second)
232 continue;
233 auto &Entry = P.first->second;
234 // Zero out the debug_info contribution
235 Entry.Contributions[0] = {};
236 for (auto Kind : TUIndex.getColumnKinds()) {
238 continue;
239 auto &C =
240 Entry.Contributions[getContributionIndex(Kind, TUIndex.getVersion())];
241 C.setOffset(C.getOffset() + I->getOffset());
242 C.setLength(I->getLength());
243 ++I;
244 }
245 auto &C = Entry.Contributions[TypesContributionIndex];
246 Out.emitBytes(Types.substr(
247 C.getOffset() -
248 TUEntry.Contributions[TypesContributionIndex].getOffset(),
249 C.getLength()));
250 C.setOffset(TypesOffset);
251 uint32_t OldOffset = TypesOffset;
252 static_assert(sizeof(OldOffset) == sizeof(TypesOffset));
253 TypesOffset += C.getLength();
254 if (OldOffset > TypesOffset) {
255 if (Error Err = sectionOverflowErrorOrWarning(OldOffset, TypesOffset,
256 "Types", OverflowOptValue,
257 AnySectionOverflow))
258 return Err;
259 if (AnySectionOverflow) {
260 TypesOffset = OldOffset;
261 return Error::success();
262 }
263 }
264 }
265 return Error::success();
266}
267
269 DWPWriter &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
270 DWPSectionId OutputSection, const std::vector<StringRef> &TypesSections,
271 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset,
272 OnCuIndexOverflow OverflowOptValue, bool &AnySectionOverflow) {
273 for (StringRef Types : TypesSections) {
274 Out.switchSection(OutputSection);
275 uint64_t Offset = 0;
276 DataExtractor Data(Types, true);
277 while (Data.isValidOffset(Offset)) {
278 UnitIndexEntry Entry = CUEntry;
279 // Zero out the debug_info contribution
280 Entry.Contributions[0] = {};
281 auto &C = Entry.Contributions[getContributionIndex(DW_SECT_EXT_TYPES, 2)];
282 C.setOffset(TypesOffset);
283 auto PrevOffset = Offset;
284 // Length of the unit, including the 4 byte length field.
285 C.setLength(Data.getU32(&Offset) + 4);
286
287 Data.getU16(&Offset); // Version
288 Data.getU32(&Offset); // Abbrev offset
289 Data.getU8(&Offset); // Address size
290 auto Signature = Data.getU64(&Offset);
291 Offset = PrevOffset + C.getLength32();
292
293 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
294 if (!P.second)
295 continue;
296
297 Out.emitBytes(Types.substr(PrevOffset, C.getLength32()));
298 uint32_t OldOffset = TypesOffset;
299 TypesOffset += C.getLength32();
300 if (OldOffset > TypesOffset) {
301 if (Error Err = sectionOverflowErrorOrWarning(OldOffset, TypesOffset,
302 "Types", OverflowOptValue,
303 AnySectionOverflow))
304 return Err;
305 if (AnySectionOverflow) {
306 TypesOffset = OldOffset;
307 return Error::success();
308 }
309 }
310 }
311 }
312 return Error::success();
313}
314
315static std::string buildDWODescription(StringRef Name, StringRef DWPName,
316 StringRef DWOName) {
317 std::string Text = "\'";
318 Text += Name;
319 Text += '\'';
320 bool HasDWO = !DWOName.empty();
321 bool HasDWP = !DWPName.empty();
322 if (HasDWO || HasDWP) {
323 Text += " (from ";
324 if (HasDWO) {
325 Text += '\'';
326 Text += DWOName;
327 Text += '\'';
328 }
329 if (HasDWO && HasDWP)
330 Text += " in ";
331 if (!DWPName.empty()) {
332 Text += '\'';
333 Text += DWPName;
334 Text += '\'';
335 }
336 Text += ")";
337 }
338 return Text;
339}
340
343 ("failure while decompressing compressed section: '" + Name + "', " +
344 llvm::toString(std::move(E)))
345 .str());
346}
347
348static Error
349handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections,
350 SectionRef Sec, StringRef Name, StringRef &Contents) {
351 auto *Obj = dyn_cast<ELFObjectFileBase>(Sec.getObject());
352 if (!Obj ||
353 !(static_cast<ELFSectionRef>(Sec).getFlags() & ELF::SHF_COMPRESSED))
354 return Error::success();
355 bool IsLE = isa<object::ELF32LEObjectFile>(Obj) ||
357 bool Is64 = isa<object::ELF64LEObjectFile>(Obj) ||
359 Expected<Decompressor> Dec = Decompressor::create(Name, Contents, IsLE, Is64);
360 if (!Dec)
361 return createError(Name, Dec.takeError());
362
363 UncompressedSections.emplace_back();
364 if (Error E = Dec->resizeAndDecompress(UncompressedSections.back()))
365 return createError(Name, std::move(E));
366
367 Contents = UncompressedSections.back();
368 return Error::success();
369}
370
371static Error
372buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
373 const CompileUnitIdentifiers &ID, StringRef DWPName) {
375 std::string("duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " +
376 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName,
377 PrevE.second.DWOName) +
378 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName));
379}
380
381// Create a mask so we don't trigger a emitIntValue() assert below if the
382// NewOffset is over 4GB.
384 DenseMap<uint64_t, uint64_t> &OffsetRemapping,
386 uint32_t OldOffsetSize, uint32_t NewOffsetSize) {
387 const uint64_t NewOffsetMask = NewOffsetSize == 8 ? UINT64_MAX : UINT32_MAX;
388 while (Offset < Size) {
389 const uint64_t OldOffset = Data.getUnsigned(&Offset, OldOffsetSize);
390 const uint64_t NewOffset = OffsetRemapping[OldOffset];
391 // Truncate the string offset like the old llvm-dwp would have if we aren't
392 // promoting the .debug_str_offsets to DWARF64.
393 Out.emitIntValue(NewOffset & NewOffsetMask, NewOffsetSize);
394 }
395}
396
397namespace llvm {
398// Parse and return the header of an info section compile/type unit.
401 Error Err = Error::success();
402 uint64_t Offset = 0;
403 DWARFDataExtractor InfoData(Info, true, 0);
404 std::tie(Header.Length, Header.Format) =
405 InfoData.getInitialLength(&Offset, &Err);
406 if (Err)
407 return make_error<DWPError>("cannot parse compile unit length: " +
408 llvm::toString(std::move(Err)));
409
410 if (!InfoData.isValidOffset(Offset + (Header.Length - 1))) {
412 "compile unit exceeds .debug_info section range: " +
413 utostr(Offset + Header.Length) + " >= " + utostr(InfoData.size()));
414 }
415
416 Header.Version = InfoData.getU16(&Offset, &Err);
417 if (Err)
418 return make_error<DWPError>("cannot parse compile unit version: " +
419 llvm::toString(std::move(Err)));
420
421 uint64_t MinHeaderLength;
422 if (Header.Version >= 5) {
423 // Size: Version (2), UnitType (1), AddrSize (1), DebugAbbrevOffset (4),
424 // Signature (8)
425 MinHeaderLength = 16;
426 } else {
427 // Size: Version (2), DebugAbbrevOffset (4), AddrSize (1)
428 MinHeaderLength = 7;
429 }
430 if (Header.Length < MinHeaderLength) {
431 return make_error<DWPError>("unit length is too small: expected at least " +
432 utostr(MinHeaderLength) + " got " +
433 utostr(Header.Length) + ".");
434 }
435 if (Header.Version >= 5) {
436 Header.UnitType = InfoData.getU8(&Offset);
437 Header.AddrSize = InfoData.getU8(&Offset);
438 Header.DebugAbbrevOffset = InfoData.getU32(&Offset);
439 Header.Signature = InfoData.getU64(&Offset);
440 if (Header.UnitType == dwarf::DW_UT_split_type) {
441 // Type offset.
442 MinHeaderLength += 4;
443 if (Header.Length < MinHeaderLength)
444 return make_error<DWPError>("type unit is missing type offset");
445 InfoData.getU32(&Offset);
446 }
447 } else {
448 // Note that, address_size and debug_abbrev_offset fields have switched
449 // places between dwarf version 4 and 5.
450 Header.DebugAbbrevOffset = InfoData.getU32(&Offset);
451 Header.AddrSize = InfoData.getU8(&Offset);
452 }
453
454 Header.HeaderSize = Offset;
455 return Header;
456}
457
458static void
460 StringRef CurStrSection, StringRef CurStrOffsetSection,
461 uint16_t Version, SectionLengths &SectionLength,
462 const Dwarf64StrOffsetsPromotion StrOffsetsOptValue,
463 bool SingleInput) {
464 // Could possibly produce an error or warning if one of these was non-null but
465 // the other was null.
466 if (CurStrSection.empty() || CurStrOffsetSection.empty())
467 return;
468
469 // Fast path: when there is only one input, all strings are unique and offsets
470 // don't need remapping. Copy both sections directly without any hashing.
471 if (SingleInput && StrOffsetsOptValue != Dwarf64StrOffsetsPromotion::Always) {
473 Out.emitBytes(CurStrSection);
475 Out.emitBytes(CurStrOffsetSection);
476 return;
477 }
478
479 DenseMap<uint64_t, uint64_t> OffsetRemapping;
480 // Pre-reserve based on estimated string count to avoid rehashing.
481 OffsetRemapping.reserve(CurStrSection.size() / 20);
482
483 DataExtractor Data(CurStrSection, true);
484 uint64_t LocalOffset = 0;
485 uint64_t PrevOffset = 0;
486
487 // Keep track if any new string offsets exceed UINT32_MAX. If any do, we can
488 // emit a DWARF64 .debug_str_offsets table for this compile unit. If the
489 // \a StrOffsetsOptValue argument is Dwarf64StrOffsetsPromotion::Always, then
490 // force the emission of DWARF64 .debug_str_offsets for testing.
491 uint32_t OldOffsetSize = 4;
492 uint32_t NewOffsetSize =
493 StrOffsetsOptValue == Dwarf64StrOffsetsPromotion::Always ? 8 : 4;
495 while (const char *S = Data.getCStr(&LocalOffset)) {
496 uint64_t NewOffset = Strings.getOffset(S, LocalOffset - PrevOffset);
497 OffsetRemapping[PrevOffset] = NewOffset;
498 // Only promote the .debug_str_offsets to DWARF64 if our setting allows it.
499 if (StrOffsetsOptValue != Dwarf64StrOffsetsPromotion::Disabled &&
500 NewOffset > UINT32_MAX) {
501 NewOffsetSize = 8;
502 }
503 PrevOffset = LocalOffset;
504 }
505
506 Data = DataExtractor(CurStrOffsetSection, true);
507
509
510 uint64_t Offset = 0;
511 uint64_t Size = CurStrOffsetSection.size();
512 if (Version > 4) {
513 while (Offset < Size) {
514 const uint64_t HeaderSize = debugStrOffsetsHeaderSize(Data, Version);
515 assert(HeaderSize <= Size - Offset &&
516 "StrOffsetSection size is less than its header");
517
518 uint64_t ContributionEnd = 0;
519 uint64_t ContributionSize = 0;
520 uint64_t HeaderLengthOffset = Offset;
521 if (HeaderSize == 8) {
522 ContributionSize = Data.getU32(&HeaderLengthOffset);
523 } else if (HeaderSize == 16) {
524 OldOffsetSize = 8;
525 HeaderLengthOffset += 4; // skip the dwarf64 marker
526 ContributionSize = Data.getU64(&HeaderLengthOffset);
527 }
528 ContributionEnd = ContributionSize + HeaderLengthOffset;
529
530 StringRef HeaderBytes = Data.getBytes(&Offset, HeaderSize);
531 if (OldOffsetSize == 4 && NewOffsetSize == 8) {
532 // We had a DWARF32 .debug_str_offsets header, but we need to emit
533 // some string offsets that require 64 bit offsets on the .debug_str
534 // section. Emit the .debug_str_offsets header in DWARF64 format so we
535 // can emit string offsets that exceed UINT32_MAX without truncating
536 // the string offset.
537
538 // 2 bytes for DWARF version, 2 bytes pad.
539 const uint64_t VersionPadSize = 4;
540 const uint64_t NewLength =
541 (ContributionSize - VersionPadSize) * 2 + VersionPadSize;
542 // Emit the DWARF64 length that starts with a 4 byte DW_LENGTH_DWARF64
543 // value followed by the 8 byte updated length.
545 Out.emitIntValue(NewLength, 8);
546 // Emit DWARF version as a 2 byte integer.
547 Out.emitIntValue(Version, 2);
548 // Emit 2 bytes of padding.
549 Out.emitIntValue(0, 2);
550 // Update the .debug_str_offsets section length contribution for the
551 // this .dwo file.
552 for (auto &Pair : SectionLength) {
553 if (Pair.first == DW_SECT_STR_OFFSETS) {
554 Pair.second = NewLength + 12;
555 break;
556 }
557 }
558 } else {
559 // Just emit the same .debug_str_offsets header.
560 Out.emitBytes(HeaderBytes);
561 }
562 writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, ContributionEnd,
563 OldOffsetSize, NewOffsetSize);
564 }
565
566 } else {
567 assert(OldOffsetSize == NewOffsetSize);
568 writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, Size, OldOffsetSize,
569 NewOffsetSize);
570 }
571}
572
574
575static void
577 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
578 const AccessField &Field) {
579 for (const auto &E : IndexEntries)
580 for (size_t I = 0; I != std::size(E.second.Contributions); ++I)
581 if (ContributionOffsets[I])
583 ? E.second.Contributions[I].getOffset32()
584 : E.second.Contributions[I].getLength32()),
585 4);
586}
587
588static void writeIndex(DWPWriter &Out, DWPSectionId Section,
589 ArrayRef<unsigned> ContributionOffsets,
590 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
591 uint32_t IndexVersion) {
592 if (IndexEntries.empty())
593 return;
594
595 unsigned Columns = 0;
596 for (auto &C : ContributionOffsets)
597 if (C)
598 ++Columns;
599
600 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
601 uint64_t Mask = Buckets.size() - 1;
602 size_t I = 0;
603 for (const auto &P : IndexEntries) {
604 auto S = P.first;
605 auto H = S & Mask;
606 auto HP = ((S >> 32) & Mask) | 1;
607 while (Buckets[H]) {
608 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
609 "Duplicate unit");
610 H = (H + HP) & Mask;
611 }
612 Buckets[H] = I + 1;
613 ++I;
614 }
615
616 Out.switchSection(Section);
617 Out.emitIntValue(IndexVersion, 4); // Version
618 Out.emitIntValue(Columns, 4); // Columns
619 Out.emitIntValue(IndexEntries.size(), 4); // Num Units
620 Out.emitIntValue(Buckets.size(), 4); // Num Buckets
621
622 // Write the signatures.
623 for (const auto &I : Buckets)
624 Out.emitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
625
626 // Write the indexes.
627 for (const auto &I : Buckets)
628 Out.emitIntValue(I, 4);
629
630 // Write the column headers (which sections will appear in the table)
631 for (size_t I = 0; I != ContributionOffsets.size(); ++I)
632 if (ContributionOffsets[I])
634
635 // Write the offsets.
636 writeIndexTable(Out, ContributionOffsets, IndexEntries, AccessField::Offset);
637
638 // Write the lengths.
639 writeIndexTable(Out, ContributionOffsets, IndexEntries, AccessField::Length);
640}
641
642/// Map input ELF section names to DWP section IDs and DWARF section kinds.
646 {"debug_info.dwo", {DS_Info, DW_SECT_INFO}},
647 {"debug_types.dwo", {DS_Types, DW_SECT_EXT_TYPES}},
648 {"debug_str_offsets.dwo", {DS_StrOffsets, DW_SECT_STR_OFFSETS}},
649 {"debug_str.dwo", {DS_Str, static_cast<DWARFSectionKind>(0)}},
650 {"debug_loc.dwo", {DS_Loc, DW_SECT_EXT_LOC}},
651 {"debug_line.dwo", {DS_Line, DW_SECT_LINE}},
652 {"debug_macro.dwo", {DS_Macro, DW_SECT_MACRO}},
653 {"debug_abbrev.dwo", {DS_Abbrev, DW_SECT_ABBREV}},
654 {"debug_loclists.dwo", {DS_Loclists, DW_SECT_LOCLISTS}},
655 {"debug_rnglists.dwo", {DS_Rnglists, DW_SECT_RNGLISTS}},
656 {"debug_cu_index", {DS_CUIndex, static_cast<DWARFSectionKind>(0)}},
657 {"debug_tu_index", {DS_TUIndex, static_cast<DWARFSectionKind>(0)}},
658 };
659 return Map;
660}
661
663 const StringMap<std::pair<DWPSectionId, DWARFSectionKind>> &KnownSections,
664 const SectionRef &Section, DWPWriter &Out,
665 std::deque<SmallString<32>> &UncompressedSections,
666 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
667 StringRef &CurStrSection, StringRef &CurStrOffsetSection,
668 std::vector<StringRef> &CurTypesSection,
669 std::vector<StringRef> &CurInfoSection, StringRef &AbbrevSection,
670 StringRef &CurCUIndexSection, StringRef &CurTUIndexSection,
671 SectionLengths &SectionLength) {
672 if (Section.isBSS())
673 return Error::success();
674
675 if (Section.isVirtual())
676 return Error::success();
677
678 Expected<StringRef> NameOrErr = Section.getName();
679 if (!NameOrErr)
680 return NameOrErr.takeError();
681 StringRef Name = *NameOrErr;
682
683 Expected<StringRef> ContentsOrErr = Section.getContents();
684 if (!ContentsOrErr)
685 return ContentsOrErr.takeError();
686 StringRef Contents = *ContentsOrErr;
687
688 if (auto Err = handleCompressedSection(UncompressedSections, Section, Name,
689 Contents))
690 return Err;
691
692 Name = Name.substr(Name.find_first_not_of("._"));
693
694 auto SectionPair = KnownSections.find(Name);
695 if (SectionPair == KnownSections.end())
696 return Error::success();
697
698 DWPSectionId SectionId = SectionPair->second.first;
699 DWARFSectionKind Kind = SectionPair->second.second;
700
701 if (Kind) {
702 if (Kind != DW_SECT_EXT_TYPES && Kind != DW_SECT_INFO)
703 SectionLength.push_back(std::make_pair(Kind, Contents.size()));
704 if (Kind == DW_SECT_ABBREV)
705 AbbrevSection = Contents;
706 }
707
708 switch (SectionId) {
709 case DS_StrOffsets:
710 CurStrOffsetSection = Contents;
711 break;
712 case DS_Str:
713 CurStrSection = Contents;
714 break;
715 case DS_Types:
716 CurTypesSection.push_back(Contents);
717 break;
718 case DS_CUIndex:
719 CurCUIndexSection = Contents;
720 break;
721 case DS_TUIndex:
722 CurTUIndexSection = Contents;
723 break;
724 case DS_Info:
725 CurInfoSection.push_back(Contents);
726 break;
727 default:
728 // Pass-through: emit directly to output (zero-copy).
729 Out.switchSection(SectionId);
730 Out.emitBytes(Contents);
731 break;
732 }
733 return Error::success();
734}
735
737 OnCuIndexOverflow OverflowOptValue,
738 Dwarf64StrOffsetsPromotion StrOffsetsOptValue,
739 raw_pwrite_stream *OutputOS) {
740 const auto &KnownSections = getKnownSections();
741
744
745 uint32_t ContributionOffsets[8] = {};
746 uint16_t Version = 0;
747 uint32_t IndexVersion = 0;
748 StringRef FirstInput;
749 bool AnySectionOverflow = false;
750
751 DWPStringPool Strings(Out);
752
754 Objects.reserve(Inputs.size());
755
756 std::deque<SmallString<32>> UncompressedSections;
757
758 bool MachineSet = false;
759
760 for (const auto &Input : Inputs) {
762 if (!ErrOrObj) {
763 return handleErrors(ErrOrObj.takeError(),
764 [&](std::unique_ptr<ECError> EC) -> Error {
765 return createFileError(Input, Error(std::move(EC)));
766 });
767 }
768
769 auto &Obj = *ErrOrObj->getBinary();
770 Objects.push_back(std::move(*ErrOrObj));
771
772 // Set output format metadata from the first input file.
773 if (!MachineSet) {
774 if (auto *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj)) {
775 Out.setMachine(ELFObj->getEMachine());
776 Out.setOSABI(ELFObj->getEIdentOSABI());
777 } else if (Obj.isWasm()) {
778 Out.setIsWASM(true);
779 }
780 MachineSet = true;
781 }
782
783 UnitIndexEntry CurEntry = {};
784
785 StringRef CurStrSection;
786 StringRef CurStrOffsetSection;
787 std::vector<StringRef> CurTypesSection;
788 std::vector<StringRef> CurInfoSection;
789 StringRef AbbrevSection;
790 StringRef CurCUIndexSection;
791 StringRef CurTUIndexSection;
792
793 // This maps each section contained in this file to its length.
794 // This information is later on used to calculate the contributions,
795 // i.e. offset and length, of each compile/type unit to a section.
796 SectionLengths SectionLength;
797
798 for (const auto &Section : Obj.sections())
799 if (auto Err = handleSection(
800 KnownSections, Section, Out, UncompressedSections,
801 ContributionOffsets, CurEntry, CurStrSection, CurStrOffsetSection,
802 CurTypesSection, CurInfoSection, AbbrevSection, CurCUIndexSection,
803 CurTUIndexSection, SectionLength))
804 return Err;
805
806 if (CurInfoSection.empty())
807 continue;
808
810 parseInfoSectionUnitHeader(CurInfoSection.front());
811 if (!HeaderOrErr)
812 return HeaderOrErr.takeError();
813 InfoSectionUnitHeader &Header = *HeaderOrErr;
814
815 if (Version == 0) {
816 Version = Header.Version;
817 IndexVersion = Version < 5 ? 2 : 5;
818 FirstInput = Input;
819 } else if (Version != Header.Version) {
821 "incompatible DWARF compile unit version: " + Input + " (version " +
822 utostr(Header.Version) + ") and " + FirstInput.str() + " (version " +
823 utostr(Version) + ")");
824 }
825
826 writeStringsAndOffsets(Out, Strings, CurStrSection, CurStrOffsetSection,
827 Header.Version, SectionLength, StrOffsetsOptValue,
828 Inputs.size() == 1);
829
830 for (auto Pair : SectionLength) {
831 auto Index = getContributionIndex(Pair.first, IndexVersion);
832 CurEntry.Contributions[Index].setOffset(ContributionOffsets[Index]);
833 CurEntry.Contributions[Index].setLength(Pair.second);
834 uint32_t OldOffset = ContributionOffsets[Index];
835 ContributionOffsets[Index] += CurEntry.Contributions[Index].getLength32();
836 if (OldOffset > ContributionOffsets[Index]) {
837 uint32_t SectionIndex = 0;
838 for (auto &Section : Obj.sections()) {
839 if (SectionIndex == Index) {
841 OldOffset, ContributionOffsets[Index], *Section.getName(),
842 OverflowOptValue, AnySectionOverflow))
843 return Err;
844 }
845 ++SectionIndex;
846 }
847 if (AnySectionOverflow)
848 break;
849 }
850 }
851
852 uint32_t &InfoSectionOffset =
853 ContributionOffsets[getContributionIndex(DW_SECT_INFO, IndexVersion)];
854 if (CurCUIndexSection.empty()) {
855 bool FoundCUUnit = false;
857 for (StringRef Info : CurInfoSection) {
858 uint64_t UnitOffset = 0;
859 while (Info.size() > UnitOffset) {
860 Expected<InfoSectionUnitHeader> HeaderOrError =
861 parseInfoSectionUnitHeader(Info.substr(UnitOffset, Info.size()));
862 if (!HeaderOrError)
863 return HeaderOrError.takeError();
864 InfoSectionUnitHeader &Header = *HeaderOrError;
865
866 UnitIndexEntry Entry = CurEntry;
867 auto &C = Entry.Contributions[getContributionIndex(DW_SECT_INFO,
868 IndexVersion)];
869 C.setOffset(InfoSectionOffset);
870 C.setLength(Header.Length + 4);
871
872 if (std::numeric_limits<uint32_t>::max() - InfoSectionOffset <
873 C.getLength32()) {
875 InfoSectionOffset, InfoSectionOffset + C.getLength32(),
876 "debug_info", OverflowOptValue, AnySectionOverflow))
877 return Err;
878 if (AnySectionOverflow) {
879 FoundCUUnit = true;
880 break;
881 }
882 }
883
884 UnitOffset += C.getLength32();
885 if (Header.Version < 5 ||
886 Header.UnitType == dwarf::DW_UT_split_compile) {
888 Header, AbbrevSection,
889 Info.substr(UnitOffset - C.getLength32(), C.getLength32()),
890 CurStrOffsetSection, CurStrSection);
891
892 if (!EID)
893 return createFileError(Input, EID.takeError());
894 const auto &ID = *EID;
895 auto P = IndexEntries.insert(std::make_pair(ID.Signature, Entry));
896 if (!P.second)
897 return buildDuplicateError(*P.first, ID, "");
898 P.first->second.Name = ID.Name;
899 P.first->second.DWOName = ID.DWOName;
900
901 FoundCUUnit = true;
902 } else if (Header.UnitType == dwarf::DW_UT_split_type) {
903 auto P = TypeIndexEntries.insert(
904 std::make_pair(*Header.Signature, Entry));
905 if (!P.second)
906 continue;
907 }
908 Out.emitBytes(
909 Info.substr(UnitOffset - C.getLength32(), C.getLength32()));
910 InfoSectionOffset += C.getLength32();
911 }
912 if (AnySectionOverflow)
913 break;
914 }
915
916 if (!FoundCUUnit)
917 return make_error<DWPError>("no compile unit found in file: " + Input);
918
919 if (IndexVersion == 2) {
920 // Add types from the .debug_types section from DWARF < 5.
922 Out, TypeIndexEntries, DS_Types, CurTypesSection, CurEntry,
923 ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES, 2)],
924 OverflowOptValue, AnySectionOverflow))
925 return Err;
926 }
927 if (AnySectionOverflow)
928 break;
929 continue;
930 }
931
932 if (CurInfoSection.size() != 1)
933 return make_error<DWPError>("expected exactly one occurrence of a debug "
934 "info section in a .dwp file");
935 StringRef DwpSingleInfoSection = CurInfoSection.front();
936
937 DWARFUnitIndex CUIndex(DW_SECT_INFO);
938 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian());
939 if (!CUIndex.parse(CUIndexData))
940 return make_error<DWPError>("failed to parse cu_index");
941 if (CUIndex.getVersion() != IndexVersion)
942 return make_error<DWPError>("incompatible cu_index versions, found " +
943 utostr(CUIndex.getVersion()) +
944 " and expecting " + utostr(IndexVersion));
945
947 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
948 auto *I = E.getContributions();
949 if (!I)
950 continue;
951 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
952 StringRef CUInfoSection =
953 getSubsection(DwpSingleInfoSection, E, DW_SECT_INFO);
954 Expected<InfoSectionUnitHeader> HeaderOrError =
955 parseInfoSectionUnitHeader(CUInfoSection);
956 if (!HeaderOrError)
957 return HeaderOrError.takeError();
958 InfoSectionUnitHeader &Header = *HeaderOrError;
959
961 Header, getSubsection(AbbrevSection, E, DW_SECT_ABBREV),
962 CUInfoSection,
963 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS),
964 CurStrSection);
965 if (!EID)
966 return createFileError(Input, EID.takeError());
967 const auto &ID = *EID;
968 if (!P.second)
969 return buildDuplicateError(*P.first, ID, Input);
970 auto &NewEntry = P.first->second;
971 NewEntry.Name = ID.Name;
972 NewEntry.DWOName = ID.DWOName;
973 NewEntry.DWPName = Input;
974 for (auto Kind : CUIndex.getColumnKinds()) {
976 continue;
977 auto &C =
978 NewEntry.Contributions[getContributionIndex(Kind, IndexVersion)];
979 C.setOffset(C.getOffset() + I->getOffset());
980 C.setLength(I->getLength());
981 ++I;
982 }
983 unsigned Index = getContributionIndex(DW_SECT_INFO, IndexVersion);
984 auto &C = NewEntry.Contributions[Index];
985 Out.emitBytes(CUInfoSection);
986 C.setOffset(InfoSectionOffset);
987 InfoSectionOffset += C.getLength32();
988 }
989
990 if (!CurTUIndexSection.empty()) {
991 llvm::DWARFSectionKind TUSectionKind;
992 DWPSectionId OutSection;
993 StringRef TypeInputSection;
994 // Write type units into debug info section for DWARFv5.
995 if (Version >= 5) {
996 TUSectionKind = DW_SECT_INFO;
997 OutSection = DS_Info;
998 TypeInputSection = DwpSingleInfoSection;
999 } else {
1000 // Write type units into debug types section for DWARF < 5.
1001 if (CurTypesSection.size() != 1)
1002 return make_error<DWPError>(
1003 "multiple type unit sections in .dwp file");
1004
1005 TUSectionKind = DW_SECT_EXT_TYPES;
1006 OutSection = DS_Types;
1007 TypeInputSection = CurTypesSection.front();
1008 }
1009
1010 DWARFUnitIndex TUIndex(TUSectionKind);
1011 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian());
1012 if (!TUIndex.parse(TUIndexData))
1013 return make_error<DWPError>("failed to parse tu_index");
1014 if (TUIndex.getVersion() != IndexVersion)
1015 return make_error<DWPError>("incompatible tu_index versions, found " +
1016 utostr(TUIndex.getVersion()) +
1017 " and expecting " + utostr(IndexVersion));
1018
1019 unsigned TypesContributionIndex =
1020 getContributionIndex(TUSectionKind, IndexVersion);
1021 if (Error Err = addAllTypesFromDWP(
1022 Out, TypeIndexEntries, TUIndex, OutSection, TypeInputSection,
1023 CurEntry, ContributionOffsets[TypesContributionIndex],
1024 TypesContributionIndex, OverflowOptValue, AnySectionOverflow))
1025 return Err;
1026 }
1027 if (AnySectionOverflow)
1028 break;
1029 }
1030
1031 if (Version < 5) {
1032 // Lie about there being no info contributions so the TU index only includes
1033 // the type unit contribution for DWARF < 5. In DWARFv5 the TU index has a
1034 // contribution to the info section, so we do not want to lie about it.
1035 ContributionOffsets[0] = 0;
1036 }
1037 writeIndex(Out, DS_TUIndex, ContributionOffsets, TypeIndexEntries,
1038 IndexVersion);
1039
1040 if (Version < 5) {
1041 // Lie about the type contribution for DWARF < 5. In DWARFv5 the type
1042 // section does not exist, so no need to do anything about this.
1043 ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES, 2)] = 0;
1044 // Unlie about the info contribution
1045 ContributionOffsets[0] = 1;
1046 }
1047
1048 writeIndex(Out, DS_CUIndex, ContributionOffsets, IndexEntries, IndexVersion);
1049
1050 // Write ELF output while input data is still alive (zero-copy chunks
1051 // reference mmap'd input data held by the Objects vector above).
1052 if (OutputOS)
1053 return Out.write(*OutputOS);
1054
1055 return Error::success();
1056}
1057
1058//===----------------------------------------------------------------------===//
1059// DWPWriter::writeELF — produce a minimal ELF64 relocatable object.
1060//===----------------------------------------------------------------------===//
1061
1064
1065 // Section metadata table.
1066 struct SectionMeta {
1067 DWPSectionId Id;
1068 const char *Name;
1069 uint64_t Flags;
1070 uint64_t EntSize;
1071 };
1072 static constexpr SectionMeta Meta[] = {
1073 {DS_Loclists, ".debug_loclists.dwo", ELF::SHF_EXCLUDE, 0},
1074 {DS_Loc, ".debug_loc.dwo", ELF::SHF_EXCLUDE, 0},
1075 {DS_Abbrev, ".debug_abbrev.dwo", ELF::SHF_EXCLUDE, 0},
1076 {DS_Line, ".debug_line.dwo", ELF::SHF_EXCLUDE, 0},
1077 {DS_Rnglists, ".debug_rnglists.dwo", ELF::SHF_EXCLUDE, 0},
1078 {DS_Macro, ".debug_macro.dwo", ELF::SHF_EXCLUDE, 0},
1079 {DS_Str, ".debug_str.dwo",
1081 {DS_StrOffsets, ".debug_str_offsets.dwo", ELF::SHF_EXCLUDE, 0},
1082 {DS_Info, ".debug_info.dwo", ELF::SHF_EXCLUDE, 0},
1083 {DS_Types, ".debug_types.dwo", ELF::SHF_EXCLUDE, 0},
1084 {DS_TUIndex, ".debug_tu_index", 0, 0},
1085 {DS_CUIndex, ".debug_cu_index", 0, 0},
1086 };
1087
1088 // Collect non-empty sections and build the section name string table.
1089 struct OutputEntry {
1090 const SectionData *Data;
1091 const char *Name;
1092 uint64_t Flags;
1093 uint64_t EntSize;
1094 uint32_t NameOffset;
1095 uint64_t FileOffset; // filled in during layout
1096 };
1098
1099 SmallString<256> Strtab;
1100 Strtab.push_back('\0'); // null string at offset 0
1101
1102 for (const auto &M : Meta) {
1103 if (Sections[M.Id].empty())
1104 continue;
1105 uint32_t NameOff = Strtab.size();
1106 Strtab.append(M.Name);
1107 Strtab.push_back('\0');
1108 Entries.push_back(
1109 {&Sections[M.Id], M.Name, M.Flags, M.EntSize, NameOff, 0});
1110 }
1111
1112 // Add .strtab and .symtab name entries.
1113 uint32_t StrtabNameOff = Strtab.size();
1114 Strtab.append(".strtab");
1115 Strtab.push_back('\0');
1116 uint32_t SymtabNameOff = Strtab.size();
1117 Strtab.append(".symtab");
1118 Strtab.push_back('\0');
1119
1120 // Layout:
1121 // [ELF Header] 64 bytes
1122 // [section data...] variable
1123 // [.strtab data] variable
1124 // [padding to 8-byte align]
1125 // [.symtab data] 24 bytes (one null entry)
1126 // [padding to 8-byte align]
1127 // [Section Header Table] 64 * NumSections bytes
1128
1129 constexpr uint64_t EhdrSize = sizeof(ELF::Elf64_Ehdr);
1130 constexpr uint64_t SymEntSize = 24;
1131
1132 uint64_t Offset = EhdrSize;
1133 for (auto &E : Entries) {
1134 E.FileOffset = Offset;
1135 Offset += E.Data->totalSize();
1136 }
1137
1138 uint64_t StrtabOffset = Offset;
1139 Offset += Strtab.size();
1140
1141 uint64_t SymtabOffset = alignTo(Offset, 8);
1142 Offset = SymtabOffset + SymEntSize;
1143
1144 uint64_t SHTOffset = alignTo(Offset, 8);
1145
1146 // Section indices: [0]=null, [1..N]=data, [N+1]=strtab, [N+2]=symtab
1147 uint32_t StrtabIdx = 1 + Entries.size();
1148 uint32_t SymtabIdx = StrtabIdx + 1;
1149 uint32_t NumSections = SymtabIdx + 1;
1150
1151 // --- Write ELF header ---
1152 ELF::writeHeader(Wr, /*Is64Bit=*/true, ELFOSABI, /*ABIVersion=*/0, ELFMachine,
1153 /*EFlags=*/0, SHTOffset, NumSections, StrtabIdx);
1154
1155 // --- Write section data ---
1156 for (const auto &E : Entries)
1157 E.Data->writeTo(OS);
1158
1159 // --- Write .strtab ---
1160 OS.write(Strtab.data(), Strtab.size());
1161
1162 // --- Pad + write .symtab (one null symbol entry) ---
1163 OS.write_zeros(SymtabOffset - (StrtabOffset + Strtab.size()));
1164 OS.write_zeros(SymEntSize);
1165
1166 // --- Pad for section header table ---
1167 uint64_t CurPos = SymtabOffset + SymEntSize;
1168 OS.write_zeros(SHTOffset - CurPos);
1169
1170 // [0] ELF::SHT_NULL
1171 ELF::writeSectionHeader(Wr, true, 0, ELF::SHT_NULL, 0, 0, 0, 0, 0, 0, 0, 0);
1172
1173 // [1..N] data sections
1174 for (const auto &E : Entries)
1175 ELF::writeSectionHeader(Wr, true, E.NameOffset, ELF::SHT_PROGBITS, E.Flags,
1176 0, E.FileOffset, E.Data->totalSize(), 0, 0, 1,
1177 E.EntSize);
1178
1179 // [N+1] .strtab
1180 ELF::writeSectionHeader(Wr, true, StrtabNameOff, ELF::SHT_STRTAB, 0, 0,
1181 StrtabOffset, Strtab.size(), 0, 0, 1, 0);
1182
1183 // [N+2] .symtab
1184 ELF::writeSectionHeader(Wr, true, SymtabNameOff, ELF::SHT_SYMTAB, 0, 0,
1185 SymtabOffset, SymEntSize, StrtabIdx, 1, 8,
1186 SymEntSize);
1187
1188 return Error::success();
1189}
1190
1191//===----------------------------------------------------------------------===//
1192// DWPWriter::writeWASM — produce a minimal WASM object with custom sections.
1193//===----------------------------------------------------------------------===//
1194
1196 // Section name table (same names as ELF but without SHF_EXCLUDE flags).
1197 static constexpr struct {
1198 DWPSectionId Id;
1199 const char *Name;
1200 } Meta[] = {
1201 {DS_Loclists, ".debug_loclists.dwo"},
1202 {DS_Loc, ".debug_loc.dwo"},
1203 {DS_Abbrev, ".debug_abbrev.dwo"},
1204 {DS_Line, ".debug_line.dwo"},
1205 {DS_Rnglists, ".debug_rnglists.dwo"},
1206 {DS_Macro, ".debug_macro.dwo"},
1207 {DS_Str, ".debug_str.dwo"},
1208 {DS_StrOffsets, ".debug_str_offsets.dwo"},
1209 {DS_Info, ".debug_info.dwo"},
1210 {DS_Types, ".debug_types.dwo"},
1211 {DS_TUIndex, ".debug_tu_index"},
1212 {DS_CUIndex, ".debug_cu_index"},
1213 };
1214
1215 // WASM magic and version.
1216 OS.write("\0asm", 4);
1217 const uint8_t Version[] = {0x01, 0x00, 0x00, 0x00};
1218 OS.write(reinterpret_cast<const char *>(Version), 4);
1219
1220 // Emit each non-empty section as a WASM custom section (id=0).
1221 for (const auto &M : Meta) {
1222 const SectionData &SD = Sections[M.Id];
1223 if (SD.empty())
1224 continue;
1225
1226 size_t NameLen = strlen(M.Name);
1227 uint64_t PayloadSize = SD.totalSize();
1228
1229 // Custom section payload = ULEB128(name_len) + name + data.
1230 uint8_t NameLenEncoded[10];
1231 unsigned NameLenSize = encodeULEB128(NameLen, NameLenEncoded);
1232 uint64_t SectionPayloadSize = NameLenSize + NameLen + PayloadSize;
1233
1234 // Section header: id byte + ULEB128(section_payload_size).
1235 OS.write(0x00); // Custom section id
1236 uint8_t SizeEncoded[10];
1237 unsigned SizeLen = encodeULEB128(SectionPayloadSize, SizeEncoded);
1238 OS.write(reinterpret_cast<const char *>(SizeEncoded), SizeLen);
1239
1240 // Name
1241 OS.write(reinterpret_cast<const char *>(NameLenEncoded), NameLenSize);
1242 OS.write(M.Name, NameLen);
1243
1244 // Data
1245 SD.writeTo(OS);
1246 }
1247
1248 return Error::success();
1249}
1250
1251} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static unsigned getContributionIndex(DWARFSectionKind Kind, uint32_t IndexVersion)
Definition DWP.cpp:176
static Error handleCompressedSection(std::deque< SmallString< 32 > > &UncompressedSections, SectionRef Sec, StringRef Name, StringRef &Contents)
Definition DWP.cpp:349
static std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName)
Definition DWP.cpp:315
static Expected< uint64_t > getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode)
Definition DWP.cpp:44
static void writeNewOffsetsTo(DWPWriter &Out, DataExtractor &Data, DenseMap< uint64_t, uint64_t > &OffsetRemapping, uint64_t &Offset, const uint64_t Size, uint32_t OldOffsetSize, uint32_t NewOffsetSize)
Definition DWP.cpp:383
static uint64_t debugStrOffsetsHeaderSize(DataExtractor StrOffsetsData, uint16_t DwarfVersion)
Definition DWP.cpp:33
static Error addAllTypesFromTypesSection(DWPWriter &Out, MapVector< uint64_t, UnitIndexEntry > &TypeIndexEntries, DWPSectionId OutputSection, const std::vector< StringRef > &TypesSections, const UnitIndexEntry &CUEntry, uint32_t &TypesOffset, OnCuIndexOverflow OverflowOptValue, bool &AnySectionOverflow)
Definition DWP.cpp:268
static Expected< const char * > getIndexedString(dwarf::Form Form, DataExtractor InfoData, uint64_t &InfoOffset, StringRef StrOffsets, StringRef Str, uint16_t Version)
Definition DWP.cpp:70
static unsigned getOnDiskSectionId(unsigned Index)
Definition DWP.cpp:184
static Expected< CompileUnitIdentifiers > getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev, StringRef Info, StringRef StrOffsets, StringRef Str)
Definition DWP.cpp:108
static Error sectionOverflowErrorOrWarning(uint32_t PrevOffset, uint32_t OverflowedOffset, StringRef SectionName, OnCuIndexOverflow OverflowOptValue, bool &AnySectionOverflow)
Definition DWP.cpp:197
static Error addAllTypesFromDWP(DWPWriter &Out, MapVector< uint64_t, UnitIndexEntry > &TypeIndexEntries, const DWARFUnitIndex &TUIndex, DWPSectionId OutputSection, StringRef Types, const UnitIndexEntry &TUEntry, uint32_t &TypesOffset, unsigned TypesContributionIndex, OnCuIndexOverflow OverflowOptValue, bool &AnySectionOverflow)
Definition DWP.cpp:219
static StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind)
Definition DWP.cpp:188
static bool isSupportedSectionKind(DWARFSectionKind Kind)
Definition DWP.cpp:170
static Error buildDuplicateError(const std::pair< uint64_t, UnitIndexEntry > &PrevE, const CompileUnitIdentifiers &ID, StringRef DWPName)
Definition DWP.cpp:372
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
OptimizedStructLayoutField Field
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
static uint32_t getFlags(const Symbol *Sym)
Definition TapiFile.cpp:26
The Input class is used to parse a yaml document into in-memory structs and vectors.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
std::pair< uint64_t, dwarf::DwarfFormat > getInitialLength(uint64_t *Off, Error *Err=nullptr) const
Extracts the DWARF "initial length" field, which can either be a 32-bit value smaller than 0xfffffff0...
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
bool skipValue(DataExtractor DebugInfoData, uint64_t *OffsetPtr, const dwarf::FormParams Params) const
Skip a form's value in DebugInfoData at the offset specified by OffsetPtr.
uint32_t getVersion() const
LLVM_ABI bool parse(DataExtractor IndexData)
ArrayRef< DWARFSectionKind > getColumnKinds() const
ArrayRef< Entry > getRows() const
uint64_t getOffset(const char *Str, unsigned Length)
Definition DWP.h:152
Direct ELF writer for DWP output.
Definition DWP.h:59
void switchSection(DWPSectionId Id)
Definition DWP.h:117
Error write(raw_pwrite_stream &OS)
Definition DWP.h:139
Error writeWASM(raw_pwrite_stream &OS)
Definition DWP.cpp:1195
void setIsWASM(bool V)
Definition DWP.h:111
void setMachine(uint16_t Machine)
Definition DWP.h:109
Error writeELF(raw_pwrite_stream &OS)
Definition DWP.cpp:1062
void setOSABI(uint8_t OSABI)
Definition DWP.h:110
void emitBytes(StringRef Data)
Zero-copy: stores a reference to the input data without copying.
Definition DWP.h:121
void emitIntValue(uint64_t Value, unsigned Size)
Definition DWP.h:129
LLVM_ABI uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
size_t size() const
Return the number of bytes in the underlying buffer.
const char * getCStr(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
LLVM_ABI uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
LLVM_ABI uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
LLVM_ABI uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
LLVM_ABI uint64_t getU64(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint64_t value from *offset_ptr.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
LLVM_ABI uint32_t getU24(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a 24-bit unsigned value from *offset_ptr and return it in a uint32_t.
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:178
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
iterator begin()
Definition MapVector.h:67
bool empty() const
Definition MapVector.h:79
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition MapVector.h:126
size_type size() const
Definition MapVector.h:58
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:68
void reserve(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:128
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
char back() const
Get the last character in the string.
Definition StringRef.h:153
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static LLVM_ABI void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
static LLVM_ABI Expected< Decompressor > create(StringRef Name, StringRef Data, bool IsLE, bool Is64Bit)
Create decompressor object.
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
const ObjectFile * getObject() const
Definition ObjectFile.h:607
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream & write(unsigned char C)
An abstract base class for streams implementations that also support a pwrite operation.
#define UINT64_MAX
Definition DataTypes.h:77
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ SHF_MERGE
Definition ELF.h:1262
@ SHF_STRINGS
Definition ELF.h:1265
@ SHF_EXCLUDE
Definition ELF.h:1290
@ SHF_COMPRESSED
Definition ELF.h:1284
LLVM_ABI void writeHeader(support::endian::Writer &W, bool Is64Bit, uint8_t OSABI, uint8_t ABIVersion, uint16_t EMachine, uint32_t EFlags, uint64_t SHOff, uint16_t SHNum, uint16_t SHStrNdx)
Write an ELF file header (Elf32_Ehdr or Elf64_Ehdr) for an ET_REL object.
Definition ELFWriter.cpp:21
LLVM_ABI void writeSectionHeader(support::endian::Writer &W, bool Is64Bit, uint32_t Name, uint32_t Type, uint64_t Flags, uint64_t Address, uint64_t Offset, uint64_t Size, uint32_t Link, uint32_t Info, uint64_t Alignment, uint64_t EntrySize)
Write a single ELF section header entry (Elf32_Shdr or Elf64_Shdr).
Definition ELFWriter.cpp:48
@ SHT_STRTAB
Definition ELF.h:1157
@ SHT_PROGBITS
Definition ELF.h:1155
@ SHT_NULL
Definition ELF.h:1154
@ SHT_SYMTAB
Definition ELF.h:1156
Attribute
Attributes.
Definition Dwarf.h:125
@ DW_LENGTH_DWARF64
Indicator of 64-bit DWARF format.
Definition Dwarf.h:57
Error createError(const Twine &Err)
Definition Error.h:86
This is an optimization pass for GlobalISel generic memory operations.
AccessField
Definition DWP.cpp:573
@ Offset
Definition DWP.cpp:573
@ Length
Definition DWP.cpp:573
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition Error.h:990
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
std::string utostr(uint64_t X, bool isNeg=false)
std::vector< std::pair< DWARFSectionKind, uint32_t > > SectionLengths
Definition DWP.h:216
static void writeIndexTable(DWPWriter &Out, ArrayRef< unsigned > ContributionOffsets, const MapVector< uint64_t, UnitIndexEntry > &IndexEntries, const AccessField &Field)
Definition DWP.cpp:576
DWARFSectionKind
The enum of section identifiers to be used in internal interfaces.
@ DW_SECT_EXT_LOC
@ DW_SECT_EXT_unknown
Denotes a value read from an index section that does not correspond to any of the supported standards...
@ DW_SECT_EXT_TYPES
static void writeStringsAndOffsets(DWPWriter &Out, DWPStringPool &Strings, StringRef CurStrSection, StringRef CurStrOffsetSection, uint16_t Version, SectionLengths &SectionLength, const Dwarf64StrOffsetsPromotion StrOffsetsOptValue, bool SingleInput)
Definition DWP.cpp:459
static Error handleSection(const StringMap< std::pair< DWPSectionId, DWARFSectionKind > > &KnownSections, const SectionRef &Section, DWPWriter &Out, std::deque< SmallString< 32 > > &UncompressedSections, uint32_t(&ContributionOffsets)[8], UnitIndexEntry &CurEntry, StringRef &CurStrSection, StringRef &CurStrOffsetSection, std::vector< StringRef > &CurTypesSection, std::vector< StringRef > &CurInfoSection, StringRef &AbbrevSection, StringRef &CurCUIndexSection, StringRef &CurTUIndexSection, SectionLengths &SectionLength)
Definition DWP.cpp:662
LLVM_ABI uint32_t serializeSectionKind(DWARFSectionKind Kind, unsigned IndexVersion)
Convert the internal value for a section kind to an on-disk value.
LLVM_ABI bool readAbbrevAttribute(const DataExtractor &AbbrevData, uint64_t *Offset, dwarf::Attribute &Name, dwarf::Form &Form, std::optional< int64_t > &ImplicitConst)
Read the next (attribute, form) specification from an abbreviation declaration at Offset,...
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
static void writeIndex(DWPWriter &Out, DWPSectionId Section, ArrayRef< unsigned > ContributionOffsets, const MapVector< uint64_t, UnitIndexEntry > &IndexEntries, uint32_t IndexVersion)
Definition DWP.cpp:588
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI Expected< InfoSectionUnitHeader > parseInfoSectionUnitHeader(StringRef Info)
Definition DWP.cpp:399
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
DWPSectionId
Section identifiers for DWP output.
Definition DWP.h:36
@ DS_Abbrev
Definition DWP.h:39
@ DS_Str
Definition DWP.h:45
@ DS_Rnglists
Definition DWP.h:43
@ DS_Loc
Definition DWP.h:41
@ DS_Types
Definition DWP.h:38
@ DS_Loclists
Definition DWP.h:42
@ DS_TUIndex
Definition DWP.h:48
@ DS_CUIndex
Definition DWP.h:47
@ DS_Info
Definition DWP.h:37
@ DS_Line
Definition DWP.h:40
@ DS_Macro
Definition DWP.h:44
@ DS_StrOffsets
Definition DWP.h:46
static const StringMap< std::pair< DWPSectionId, DWARFSectionKind > > & getKnownSections()
Map input ELF section names to DWP section IDs and DWARF section kinds.
Definition DWP.cpp:644
OnCuIndexOverflow
Definition DWP.h:23
@ SoftStop
Definition DWP.h:25
@ Continue
Definition DWP.h:26
Dwarf64StrOffsetsPromotion
Definition DWP.h:29
@ Always
Always emit .debug_str_offsets talbes as DWARF64 for testing.
Definition DWP.h:32
@ Disabled
Don't do any conversion of .debug_str_offsets tables.
Definition DWP.h:30
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
Definition DWP.cpp:736
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
DWARFUnitIndex::Entry::SectionContribution Contributions[8]
Definition DWP.h:167
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition Dwarf.h:1122
Adapter to write values to a stream in a particular byte order.