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