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/SmallVector.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/BinaryFormat/ELF.h"
17#include "llvm/DWP/DWPError.h"
18#include "llvm/DWP/ELFWriter.h"
19#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
20#include "llvm/Object/Decompressor.h"
21#include "llvm/Object/ELFObjectFile.h"
22#include "llvm/Support/EndianStream.h"
23#include "llvm/Support/LEB128.h"
24#include "llvm/Support/MathExtras.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.
32static uint64_t debugStrOffsetsHeaderSize(DataExtractor StrOffsetsData,
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_ptr: &Offset);
38 if (Length == llvm::dwarf::DW_LENGTH_DWARF64)
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
43static Expected<uint64_t> getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode,
44 bool IsLittleEndian) {
45 uint64_t Offset = 0;
46 DataExtractor AbbrevData(Abbrev, IsLittleEndian);
47 while (AbbrevData.isValidOffset(offset: Offset)) {
48 uint64_t Code = AbbrevData.getULEB128(offset_ptr: &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_ptr: &Offset);
56 // DW_CHILDREN
57 AbbrevData.getU8(offset_ptr: &Offset);
58 // Attribute specifications, terminated by a (0, 0) pair.
59 dwarf::Attribute Name;
60 dwarf::Form Form;
61 std::optional<int64_t> ImplicitConst;
62 while (readAbbrevAttribute(AbbrevData, Offset: &Offset, Name, Form, ImplicitConst))
63 ;
64 }
65 return make_error<DWPError>(Args: "abbrev code " + utostr(X: AbbrCode) +
66 " not found in abbrev section");
67}
68
69static Expected<const char *>
70getIndexedString(dwarf::Form Form, DataExtractor InfoData, uint64_t &InfoOffset,
71 StringRef StrOffsets, StringRef Str, uint16_t Version) {
72 if (Form == dwarf::DW_FORM_string)
73 return InfoData.getCStr(OffsetPtr: &InfoOffset);
74 uint64_t StrIndex;
75 switch (Form) {
76 case dwarf::DW_FORM_strx1:
77 StrIndex = InfoData.getU8(offset_ptr: &InfoOffset);
78 break;
79 case dwarf::DW_FORM_strx2:
80 StrIndex = InfoData.getU16(offset_ptr: &InfoOffset);
81 break;
82 case dwarf::DW_FORM_strx3:
83 StrIndex = InfoData.getU24(OffsetPtr: &InfoOffset);
84 break;
85 case dwarf::DW_FORM_strx4:
86 StrIndex = InfoData.getU32(offset_ptr: &InfoOffset);
87 break;
88 case dwarf::DW_FORM_strx:
89 case dwarf::DW_FORM_GNU_str_index:
90 StrIndex = InfoData.getULEB128(offset_ptr: &InfoOffset);
91 break;
92 default:
93 return make_error<DWPError>(
94 Args: "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, DwarfVersion: Version);
101
102 uint64_t StrOffset = StrOffsetsData.getU32(offset_ptr: &StrOffsetsOffset);
103 DataExtractor StrData(Str, InfoData.isLittleEndian());
104 return StrData.getCStr(OffsetPtr: &StrOffset);
105}
106
107static Expected<CompileUnitIdentifiers>
108getCUIdentifiers(InfoSectionUnitHeader &Header, StringRef Abbrev,
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)
114 return make_error<DWPError>(
115 Args: std::string("unit type DW_UT_split_compile type not found in "
116 "debug_info header. Unexpected unit type 0x" +
117 utostr(X: Header.UnitType) + " found"));
118
119 CompileUnitIdentifiers ID;
120
121 uint32_t AbbrCode = InfoData.getULEB128(offset_ptr: &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(offset_ptr: &AbbrevOffset));
129 if (Tag != dwarf::DW_TAG_compile_unit)
130 return make_error<DWPError>(Args: "top level DIE is not a compile unit");
131 // DW_CHILDREN
132 AbbrevData.getU8(offset_ptr: &AbbrevOffset);
133 dwarf::Attribute Name;
134 dwarf::Form Form;
135 std::optional<int64_t> ImplicitConst;
136 while (readAbbrevAttribute(AbbrevData, Offset: &AbbrevOffset, Name, Form,
137 ImplicitConst)) {
138 switch (Name) {
139 case dwarf::DW_AT_name: {
140 Expected<const char *> EName = getIndexedString(
141 Form, InfoData, InfoOffset&: Offset, StrOffsets, Str, Version: 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: {
149 Expected<const char *> EName = getIndexedString(
150 Form, InfoData, InfoOffset&: Offset, StrOffsets, Str, Version: 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_ptr: &Offset);
159 break;
160 default:
161 DWARFFormValue::skipValue(
162 Form, DebugInfoData: InfoData, OffsetPtr: &Offset,
163 FormParams: dwarf::FormParams({.Version: Header.Version, .AddrSize: Header.AddrSize, .Format: Header.Format}));
164 }
165 }
166 if (!Header.Signature)
167 return make_error<DWPError>(Args: "compile unit missing dwo_id");
168 ID.Signature = *Header.Signature;
169 return ID;
170}
171
172static bool isSupportedSectionKind(DWARFSectionKind Kind) {
173 return Kind != DW_SECT_EXT_unknown;
174}
175
176// Convert an internal section identifier into the index to use with
177// UnitIndexEntry::Contributions.
178static unsigned getContributionIndex(DWARFSectionKind Kind,
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
190static StringRef getSubsection(StringRef Section,
191 const DWARFUnitIndex::Entry &Entry,
192 DWARFSectionKind Kind) {
193 const auto *Off = Entry.getContribution(Sec: Kind);
194 if (!Off)
195 return StringRef();
196 return Section.substr(Start: Off->getOffset(), N: Off->getLength());
197}
198
199static Error sectionOverflowErrorOrWarning(uint32_t PrevOffset,
200 uint32_t OverflowedOffset,
201 StringRef SectionName,
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) {
211 WithColor::defaultWarningHandler(Warning: make_error<DWPError>(Args&: Msg));
212 return Error::success();
213 } else if (OverflowOptValue == OnCuIndexOverflow::SoftStop) {
214 AnySectionOverflow = true;
215 WithColor::defaultWarningHandler(Warning: make_error<DWPError>(Args&: Msg));
216 return Error::success();
217 }
218 return make_error<DWPError>(Args&: Msg);
219}
220
221static Error addAllTypesFromDWP(
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(Id: OutputSection);
228 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
229 auto *I = E.getContributions();
230 if (!I)
231 continue;
232 auto P = TypeIndexEntries.insert(KV: std::make_pair(x: E.getSignature(), y: 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()) {
239 if (!isSupportedSectionKind(Kind))
240 continue;
241 auto &C =
242 Entry.Contributions[getContributionIndex(Kind, IndexVersion: 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(Data: Types.substr(
249 Start: C.getOffset() -
250 TUEntry.Contributions[TypesContributionIndex].getOffset(),
251 N: 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(PrevOffset: OldOffset, OverflowedOffset: TypesOffset,
258 SectionName: "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
270static Error addAllTypesFromTypesSection(
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(Id: OutputSection);
278 uint64_t Offset = 0;
279 DataExtractor Data(Types, IsLittleEndian);
280 while (Data.isValidOffset(offset: Offset)) {
281 UnitIndexEntry Entry = CUEntry;
282 // Zero out the debug_info contribution
283 Entry.Contributions[0] = {};
284 auto &C = Entry.Contributions[getContributionIndex(Kind: DW_SECT_EXT_TYPES, IndexVersion: 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_ptr: &Offset) + 4);
289
290 Data.getU16(offset_ptr: &Offset); // Version
291 Data.getU32(offset_ptr: &Offset); // Abbrev offset
292 Data.getU8(offset_ptr: &Offset); // Address size
293 auto Signature = Data.getU64(offset_ptr: &Offset);
294 Offset = PrevOffset + C.getLength32();
295
296 auto P = TypeIndexEntries.insert(KV: std::make_pair(x&: Signature, y&: Entry));
297 if (!P.second)
298 continue;
299
300 Out.emitBytes(Data: Types.substr(Start: PrevOffset, N: C.getLength32()));
301 uint32_t OldOffset = TypesOffset;
302 TypesOffset += C.getLength32();
303 if (OldOffset > TypesOffset) {
304 if (Error Err = sectionOverflowErrorOrWarning(PrevOffset: OldOffset, OverflowedOffset: TypesOffset,
305 SectionName: "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
344static Error createError(StringRef Name, Error E) {
345 return make_error<DWPError>(
346 Args: ("failure while decompressing compressed section: '" + Name + "', " +
347 llvm::toString(E: 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>(Val: Sec.getObject());
355 if (!Obj ||
356 !(static_cast<ELFSectionRef>(Sec).getFlags() & ELF::SHF_COMPRESSED))
357 return Error::success();
358 bool IsLE = isa<object::ELF32LEObjectFile>(Val: Obj) ||
359 isa<object::ELF64LEObjectFile>(Val: Obj);
360 bool Is64 = isa<object::ELF64LEObjectFile>(Val: Obj) ||
361 isa<object::ELF64BEObjectFile>(Val: Obj);
362 Expected<Decompressor> Dec = Decompressor::create(Name, Data: Contents, IsLE, Is64Bit: Is64);
363 if (!Dec)
364 return createError(Name, E: Dec.takeError());
365
366 UncompressedSections.emplace_back();
367 if (Error E = Dec->resizeAndDecompress(Out&: UncompressedSections.back()))
368 return createError(Name, E: 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) {
377 return make_error<DWPError>(
378 Args: std::string("duplicate DWO ID (") + utohexstr(X: PrevE.first) + ") in " +
379 buildDWODescription(Name: PrevE.second.Name, DWPName: PrevE.second.DWPName,
380 DWOName: PrevE.second.DWOName) +
381 " and " + buildDWODescription(Name: ID.Name, DWPName, DWOName: ID.DWOName));
382}
383
384// Create a mask so we don't trigger a emitIntValue() assert below if the
385// NewOffset is over 4GB.
386static void writeNewOffsetsTo(DWPWriter &Out, DataExtractor &Data,
387 DenseMap<uint64_t, uint64_t> &OffsetRemapping,
388 uint64_t &Offset, const uint64_t Size,
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_ptr: &Offset, byte_size: 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(Value: NewOffset & NewOffsetMask, Size: NewOffsetSize);
397 }
398}
399
400namespace llvm {
401// Parse and return the header of an info section compile/type unit.
402Expected<InfoSectionUnitHeader>
403parseInfoSectionUnitHeader(StringRef Info, bool IsLittleEndian) {
404 InfoSectionUnitHeader Header;
405 Error Err = Error::success();
406 uint64_t Offset = 0;
407 DWARFDataExtractor InfoData(Info, IsLittleEndian, 0);
408 std::tie(args&: Header.Length, args&: Header.Format) =
409 InfoData.getInitialLength(Off: &Offset, Err: &Err);
410 if (Err)
411 return make_error<DWPError>(Args: "cannot parse compile unit length: " +
412 llvm::toString(E: std::move(Err)));
413
414 if (!InfoData.isValidOffset(offset: Offset + (Header.Length - 1))) {
415 return make_error<DWPError>(
416 Args: "compile unit exceeds .debug_info section range: " +
417 utostr(X: Offset + Header.Length) + " >= " + utostr(X: InfoData.size()));
418 }
419
420 Header.Version = InfoData.getU16(offset_ptr: &Offset, Err: &Err);
421 if (Err)
422 return make_error<DWPError>(Args: "cannot parse compile unit version: " +
423 llvm::toString(E: 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>(Args: "unit length is too small: expected at least " +
436 utostr(X: MinHeaderLength) + " got " +
437 utostr(X: Header.Length) + ".");
438 }
439 if (Header.Version >= 5) {
440 Header.UnitType = InfoData.getU8(offset_ptr: &Offset);
441 Header.AddrSize = InfoData.getU8(offset_ptr: &Offset);
442 Header.DebugAbbrevOffset = InfoData.getU32(offset_ptr: &Offset);
443 Header.Signature = InfoData.getU64(offset_ptr: &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>(Args: "type unit is missing type offset");
449 InfoData.getU32(offset_ptr: &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_ptr: &Offset);
455 Header.AddrSize = InfoData.getU8(offset_ptr: &Offset);
456 }
457
458 Header.HeaderSize = Offset;
459 return Header;
460}
461
462static void
463writeStringsAndOffsets(DWPWriter &Out, DWPStringPool &Strings,
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) {
476 Out.switchSection(Id: DS_Str);
477 Out.emitBytes(Data: CurStrSection);
478 Out.switchSection(Id: DS_StrOffsets);
479 Out.emitBytes(Data: 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(NumEntries: 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;
498 Out.switchSection(Id: DS_Str);
499 while (const char *S = Data.getCStr(OffsetPtr: &LocalOffset)) {
500 uint64_t NewOffset = Strings.getOffset(Str: S, Length: 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
512 Out.switchSection(Id: DS_StrOffsets);
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(StrOffsetsData: Data, DwarfVersion: 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(offset_ptr: &HeaderLengthOffset);
527 } else if (HeaderSize == 16) {
528 OldOffsetSize = 8;
529 HeaderLengthOffset += 4; // skip the dwarf64 marker
530 ContributionSize = Data.getU64(offset_ptr: &HeaderLengthOffset);
531 }
532 ContributionEnd = ContributionSize + HeaderLengthOffset;
533
534 StringRef HeaderBytes = Data.getBytes(OffsetPtr: &Offset, Length: 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.
548 Out.emitIntValue(Value: llvm::dwarf::DW_LENGTH_DWARF64, Size: 4);
549 Out.emitIntValue(Value: NewLength, Size: 8);
550 // Emit DWARF version as a 2 byte integer.
551 Out.emitIntValue(Value: Version, Size: 2);
552 // Emit 2 bytes of padding.
553 Out.emitIntValue(Value: 0, Size: 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(Data: HeaderBytes);
565 }
566 writeNewOffsetsTo(Out, Data, OffsetRemapping, Offset, Size: 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
577enum AccessField { Offset, Length };
578
579static void
580writeIndexTable(DWPWriter &Out, ArrayRef<unsigned> ContributionOffsets,
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])
586 Out.emitIntValue(Value: (Field == AccessField::Offset
587 ? E.second.Contributions[I].getOffset32()
588 : E.second.Contributions[I].getLength32()),
589 Size: 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(A: 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(Id: Section);
621 // Header layout differs between v2 and v5; see DWARFUnitIndex::Header::parse.
622 if (IndexVersion >= 5) {
623 Out.emitIntValue(Value: IndexVersion, Size: 2); // Version
624 Out.emitIntValue(Value: 0, Size: 2); // Padding
625 } else {
626 Out.emitIntValue(Value: IndexVersion, Size: 4); // Version
627 }
628 Out.emitIntValue(Value: Columns, Size: 4); // Columns
629 Out.emitIntValue(Value: IndexEntries.size(), Size: 4); // Num Units
630 Out.emitIntValue(Value: Buckets.size(), Size: 4); // Num Buckets
631
632 // Write the signatures.
633 for (const auto &I : Buckets)
634 Out.emitIntValue(Value: I ? IndexEntries.begin()[I - 1].first : 0, Size: 8);
635
636 // Write the indexes.
637 for (const auto &I : Buckets)
638 Out.emitIntValue(Value: I, Size: 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])
643 Out.emitIntValue(Value: getOnDiskSectionId(Index: I), Size: 4);
644
645 // Write the offsets.
646 writeIndexTable(Out, ContributionOffsets, IndexEntries, Field: AccessField::Offset);
647
648 // Write the lengths.
649 writeIndexTable(Out, ContributionOffsets, IndexEntries, Field: AccessField::Length);
650}
651
652/// Map input ELF section names to DWP section IDs and DWARF section kinds.
653static const StringMap<std::pair<DWPSectionId, DWARFSectionKind>> &
654getKnownSections() {
655 static const StringMap<std::pair<DWPSectionId, DWARFSectionKind>> Map = {
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
672static Error handleSection(
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, Sec: Section, Name,
699 Contents))
700 return Err;
701
702 Name = Name.substr(Start: Name.find_first_not_of(Chars: "._"));
703
704 auto SectionPair = KnownSections.find(Key: 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(x: std::make_pair(x&: Kind, y: 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(x: 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(x: Contents);
736 break;
737 default:
738 // Pass-through: emit directly to output (zero-copy).
739 Out.switchSection(Id: SectionId);
740 Out.emitBytes(Data: Contents);
741 break;
742 }
743 return Error::success();
744}
745
746Error write(DWPWriter &Out, ArrayRef<std::string> Inputs,
747 OnCuIndexOverflow OverflowOptValue,
748 Dwarf64StrOffsetsPromotion StrOffsetsOptValue,
749 raw_pwrite_stream *OutputOS) {
750 const auto &KnownSections = getKnownSections();
751
752 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
753 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
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
763 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects;
764 Objects.reserve(N: Inputs.size());
765
766 std::deque<SmallString<32>> UncompressedSections;
767
768 bool MachineSet = false;
769
770 for (const auto &Input : Inputs) {
771 auto ErrOrObj = object::ObjectFile::createObjectFile(ObjectPath: Input);
772 if (!ErrOrObj) {
773 return handleErrors(E: ErrOrObj.takeError(),
774 Hs: [&](std::unique_ptr<ECError> EC) -> Error {
775 return createFileError(F: Input, E: Error(std::move(EC)));
776 });
777 }
778
779 auto &Obj = *ErrOrObj->getBinary();
780 Objects.push_back(Elt: std::move(*ErrOrObj));
781
782 // Set output format metadata from the first input file.
783 if (!MachineSet) {
784 if (auto *ELFObj = dyn_cast<ELFObjectFileBase>(Val: &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
820 Expected<InfoSectionUnitHeader> HeaderOrErr = parseInfoSectionUnitHeader(
821 Info: CurInfoSection.front(), IsLittleEndian: 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) {
831 return make_error<DWPError>(
832 Args: "incompatible DWARF compile unit version: " + Input + " (version " +
833 utostr(X: Header.Version) + ") and " + FirstInput.str() + " (version " +
834 utostr(X: Version) + ")");
835 }
836
837 writeStringsAndOffsets(Out, Strings, CurStrSection, CurStrOffsetSection,
838 Version: Header.Version, SectionLength, StrOffsetsOptValue,
839 SingleInput: Inputs.size() == 1, IsLittleEndian: Obj.isLittleEndian());
840
841 for (auto Pair : SectionLength) {
842 auto Index = getContributionIndex(Kind: 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) {
851 if (Error Err = sectionOverflowErrorOrWarning(
852 PrevOffset: OldOffset, OverflowedOffset: ContributionOffsets[Index], SectionName: *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(Kind: DW_SECT_INFO, IndexVersion)];
865 if (CurCUIndexSection.empty()) {
866 bool FoundCUUnit = false;
867 Out.switchSection(Id: DS_Info);
868 for (StringRef Info : CurInfoSection) {
869 uint64_t UnitOffset = 0;
870 while (Info.size() > UnitOffset) {
871 Expected<InfoSectionUnitHeader> HeaderOrError =
872 parseInfoSectionUnitHeader(Info: Info.substr(Start: UnitOffset, N: Info.size()),
873 IsLittleEndian: Obj.isLittleEndian());
874 if (!HeaderOrError)
875 return HeaderOrError.takeError();
876 InfoSectionUnitHeader &Header = *HeaderOrError;
877
878 UnitIndexEntry Entry = CurEntry;
879 auto &C = Entry.Contributions[getContributionIndex(Kind: 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()) {
886 if (Error Err = sectionOverflowErrorOrWarning(
887 PrevOffset: InfoSectionOffset, OverflowedOffset: InfoSectionOffset + C.getLength32(),
888 SectionName: "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) {
899 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
900 Header, Abbrev: AbbrevSection,
901 Info: Info.substr(Start: UnitOffset - C.getLength32(), N: C.getLength32()),
902 StrOffsets: CurStrOffsetSection, Str: CurStrSection, IsLittleEndian: Obj.isLittleEndian());
903
904 if (!EID)
905 return createFileError(F: Input, E: EID.takeError());
906 const auto &ID = *EID;
907 auto P = IndexEntries.insert(KV: std::make_pair(x: ID.Signature, y&: Entry));
908 if (!P.second)
909 return buildDuplicateError(PrevE: *P.first, ID, DWPName: "");
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 KV: std::make_pair(x&: *Header.Signature, y&: Entry));
917 if (!P.second)
918 continue;
919 }
920 Out.emitBytes(
921 Data: Info.substr(Start: UnitOffset - C.getLength32(), N: C.getLength32()));
922 InfoSectionOffset += C.getLength32();
923 }
924 if (AnySectionOverflow)
925 break;
926 }
927
928 if (!FoundCUUnit)
929 return make_error<DWPError>(Args: "no compile unit found in file: " + Input);
930
931 if (IndexVersion == 2) {
932 // Add types from the .debug_types section from DWARF < 5.
933 if (Error Err = addAllTypesFromTypesSection(
934 Out, TypeIndexEntries, OutputSection: DS_Types, TypesSections: CurTypesSection, CUEntry: CurEntry,
935 TypesOffset&: ContributionOffsets[getContributionIndex(Kind: DW_SECT_EXT_TYPES, IndexVersion: 2)],
936 OverflowOptValue, AnySectionOverflow, IsLittleEndian: 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>(Args: "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(IndexData: CUIndexData))
952 return make_error<DWPError>(Args: "failed to parse cu_index");
953 if (CUIndex.getVersion() != IndexVersion)
954 return make_error<DWPError>(Args: "incompatible cu_index versions, found " +
955 utostr(X: CUIndex.getVersion()) +
956 " and expecting " + utostr(X: IndexVersion));
957
958 Out.switchSection(Id: DS_Info);
959 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
960 auto *I = E.getContributions();
961 if (!I)
962 continue;
963 auto P = IndexEntries.insert(KV: std::make_pair(x: E.getSignature(), y&: CurEntry));
964 StringRef CUInfoSection =
965 getSubsection(Section: DwpSingleInfoSection, Entry: E, Kind: DW_SECT_INFO);
966 Expected<InfoSectionUnitHeader> HeaderOrError =
967 parseInfoSectionUnitHeader(Info: CUInfoSection, IsLittleEndian: Obj.isLittleEndian());
968 if (!HeaderOrError)
969 return HeaderOrError.takeError();
970 InfoSectionUnitHeader &Header = *HeaderOrError;
971
972 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers(
973 Header, Abbrev: getSubsection(Section: AbbrevSection, Entry: E, Kind: DW_SECT_ABBREV),
974 Info: CUInfoSection,
975 StrOffsets: getSubsection(Section: CurStrOffsetSection, Entry: E, Kind: DW_SECT_STR_OFFSETS),
976 Str: CurStrSection, IsLittleEndian: Obj.isLittleEndian());
977 if (!EID)
978 return createFileError(F: Input, E: EID.takeError());
979 const auto &ID = *EID;
980 if (!P.second)
981 return buildDuplicateError(PrevE: *P.first, ID, DWPName: 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()) {
987 if (!isSupportedSectionKind(Kind))
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(Kind: DW_SECT_INFO, IndexVersion);
996 auto &C = NewEntry.Contributions[Index];
997 Out.emitBytes(Data: 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 Args: "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(IndexData: TUIndexData))
1025 return make_error<DWPError>(Args: "failed to parse tu_index");
1026 if (TUIndex.getVersion() != IndexVersion)
1027 return make_error<DWPError>(Args: "incompatible tu_index versions, found " +
1028 utostr(X: TUIndex.getVersion()) +
1029 " and expecting " + utostr(X: IndexVersion));
1030
1031 unsigned TypesContributionIndex =
1032 getContributionIndex(Kind: TUSectionKind, IndexVersion);
1033 if (Error Err = addAllTypesFromDWP(
1034 Out, TypeIndexEntries, TUIndex, OutputSection: OutSection, Types: TypeInputSection,
1035 TUEntry: CurEntry, TypesOffset&: 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, Section: DS_TUIndex, ContributionOffsets, IndexEntries: 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(Kind: DW_SECT_EXT_TYPES, IndexVersion: 2)] = 0;
1058 // Unlie about the info contribution
1059 ContributionOffsets[0] = 1;
1060 }
1061
1062 writeIndex(Out, Section: 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(OS&: *OutputOS);
1068
1069 return Error::success();
1070}
1071
1072//===----------------------------------------------------------------------===//
1073// DWPWriter::writeELF — produce a minimal ELF64 relocatable object.
1074//===----------------------------------------------------------------------===//
1075
1076Error DWPWriter::writeELF(raw_pwrite_stream &OS) {
1077 support::endian::Writer Wr(OS, IsLittleEndian ? llvm::endianness::little
1078 : llvm::endianness::big);
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 {.Id: DS_Loclists, .Name: ".debug_loclists.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1089 {.Id: DS_Loc, .Name: ".debug_loc.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1090 {.Id: DS_Abbrev, .Name: ".debug_abbrev.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1091 {.Id: DS_Line, .Name: ".debug_line.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1092 {.Id: DS_Rnglists, .Name: ".debug_rnglists.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1093 {.Id: DS_Macro, .Name: ".debug_macro.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1094 {.Id: DS_Str, .Name: ".debug_str.dwo",
1095 .Flags: ELF::SHF_EXCLUDE | ELF::SHF_MERGE | ELF::SHF_STRINGS, .EntSize: 1},
1096 {.Id: DS_StrOffsets, .Name: ".debug_str_offsets.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1097 {.Id: DS_Info, .Name: ".debug_info.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1098 {.Id: DS_Types, .Name: ".debug_types.dwo", .Flags: ELF::SHF_EXCLUDE, .EntSize: 0},
1099 {.Id: DS_TUIndex, .Name: ".debug_tu_index", .Flags: 0, .EntSize: 0},
1100 {.Id: DS_CUIndex, .Name: ".debug_cu_index", .Flags: 0, .EntSize: 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 };
1113 SmallVector<OutputEntry> Entries;
1114
1115 SmallString<256> Strtab;
1116 Strtab.push_back(Elt: '\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(RHS: M.Name);
1123 Strtab.push_back(Elt: '\0');
1124 Entries.push_back(
1125 Elt: {.Data: &Sections[M.Id], .Name: M.Name, .Flags: M.Flags, .EntSize: M.EntSize, .NameOffset: NameOff, .FileOffset: 0, .Size: 0});
1126 }
1127
1128 // Add .strtab and .symtab name entries.
1129 uint32_t StrtabNameOff = Strtab.size();
1130 Strtab.append(RHS: ".strtab");
1131 Strtab.push_back(Elt: '\0');
1132 uint32_t SymtabNameOff = Strtab.size();
1133 Strtab.append(RHS: ".symtab");
1134 Strtab.push_back(Elt: '\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(Value: Offset, Align: 8);
1159 Offset = SymtabOffset + SymEntSize;
1160
1161 uint64_t SHTOffset = alignTo(Value: Offset, Align: 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(W&: Wr, /*Is64Bit=*/true, OSABI: ELFOSABI, /*ABIVersion=*/0, EMachine: ELFMachine,
1170 /*EFlags=*/0, SHOff: SHTOffset, SHNum: NumSections, SHStrNdx: StrtabIdx);
1171
1172 // --- Write section data ---
1173 for (auto &E : Entries)
1174 E.Data->writeTo(OS);
1175
1176 // --- Write .strtab ---
1177 OS.write(Ptr: Strtab.data(), Size: Strtab.size());
1178
1179 // --- Pad + write .symtab (one null symbol entry) ---
1180 OS.write_zeros(NumZeros: SymtabOffset - (StrtabOffset + Strtab.size()));
1181 OS.write_zeros(NumZeros: SymEntSize);
1182
1183 // --- Pad for section header table ---
1184 uint64_t CurPos = SymtabOffset + SymEntSize;
1185 OS.write_zeros(NumZeros: SHTOffset - CurPos);
1186
1187 // [0] ELF::SHT_NULL
1188 ELF::writeSectionHeader(W&: Wr, Is64Bit: true, Name: 0, Type: ELF::SHT_NULL, Flags: 0, Address: 0, Offset: 0, Size: 0, Link: 0, Info: 0, Alignment: 0, EntrySize: 0);
1189
1190 // [1..N] data sections
1191 for (const auto &E : Entries)
1192 ELF::writeSectionHeader(W&: Wr, Is64Bit: true, Name: E.NameOffset, Type: ELF::SHT_PROGBITS, Flags: E.Flags,
1193 Address: 0, Offset: E.FileOffset, Size: E.Size, Link: 0, Info: 0, Alignment: 1, EntrySize: E.EntSize);
1194
1195 // [N+1] .strtab
1196 ELF::writeSectionHeader(W&: Wr, Is64Bit: true, Name: StrtabNameOff, Type: ELF::SHT_STRTAB, Flags: 0, Address: 0,
1197 Offset: StrtabOffset, Size: Strtab.size(), Link: 0, Info: 0, Alignment: 1, EntrySize: 0);
1198
1199 // [N+2] .symtab
1200 ELF::writeSectionHeader(W&: Wr, Is64Bit: true, Name: SymtabNameOff, Type: ELF::SHT_SYMTAB, Flags: 0, Address: 0,
1201 Offset: SymtabOffset, Size: SymEntSize, Link: StrtabIdx, Info: 1, Alignment: 8,
1202 EntrySize: SymEntSize);
1203
1204 return Error::success();
1205}
1206
1207//===----------------------------------------------------------------------===//
1208// DWPWriter::writeWASM — produce a minimal WASM object with custom sections.
1209//===----------------------------------------------------------------------===//
1210
1211Error DWPWriter::writeWASM(raw_pwrite_stream &OS) {
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 {.Id: DS_Loclists, .Name: ".debug_loclists.dwo"},
1218 {.Id: DS_Loc, .Name: ".debug_loc.dwo"},
1219 {.Id: DS_Abbrev, .Name: ".debug_abbrev.dwo"},
1220 {.Id: DS_Line, .Name: ".debug_line.dwo"},
1221 {.Id: DS_Rnglists, .Name: ".debug_rnglists.dwo"},
1222 {.Id: DS_Macro, .Name: ".debug_macro.dwo"},
1223 {.Id: DS_Str, .Name: ".debug_str.dwo"},
1224 {.Id: DS_StrOffsets, .Name: ".debug_str_offsets.dwo"},
1225 {.Id: DS_Info, .Name: ".debug_info.dwo"},
1226 {.Id: DS_Types, .Name: ".debug_types.dwo"},
1227 {.Id: DS_TUIndex, .Name: ".debug_tu_index"},
1228 {.Id: DS_CUIndex, .Name: ".debug_cu_index"},
1229 };
1230
1231 // WASM magic and version.
1232 OS.write(Ptr: "\0asm", Size: 4);
1233 const uint8_t Version[] = {0x01, 0x00, 0x00, 0x00};
1234 OS.write(Ptr: reinterpret_cast<const char *>(Version), Size: 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(s: 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(Value: NameLen, p: NameLenEncoded);
1248 uint64_t SectionPayloadSize = NameLenSize + NameLen + PayloadSize;
1249
1250 // Section header: id byte + ULEB128(section_payload_size).
1251 OS.write(C: 0x00); // Custom section id
1252 uint8_t SizeEncoded[10];
1253 unsigned SizeLen = encodeULEB128(Value: SectionPayloadSize, p: SizeEncoded);
1254 OS.write(Ptr: reinterpret_cast<const char *>(SizeEncoded), Size: SizeLen);
1255
1256 // Name
1257 OS.write(Ptr: reinterpret_cast<const char *>(NameLenEncoded), Size: NameLenSize);
1258 OS.write(Ptr: M.Name, Size: NameLen);
1259
1260 // Data
1261 SD.writeTo(OS);
1262 }
1263
1264 return Error::success();
1265}
1266
1267} // namespace llvm
1268