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