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