1 | //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===// |
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 | /// \file |
10 | /// The DWARF component of yaml2obj. Provided as library code for tests. |
11 | /// |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "llvm/ObjectYAML/DWARFEmitter.h" |
15 | #include "llvm/ADT/ArrayRef.h" |
16 | #include "llvm/ADT/StringMap.h" |
17 | #include "llvm/ADT/StringRef.h" |
18 | #include "llvm/ADT/StringSwitch.h" |
19 | #include "llvm/BinaryFormat/Dwarf.h" |
20 | #include "llvm/ObjectYAML/DWARFYAML.h" |
21 | #include "llvm/Support/Errc.h" |
22 | #include "llvm/Support/Error.h" |
23 | #include "llvm/Support/LEB128.h" |
24 | #include "llvm/Support/MathExtras.h" |
25 | #include "llvm/Support/MemoryBuffer.h" |
26 | #include "llvm/Support/SourceMgr.h" |
27 | #include "llvm/Support/SwapByteOrder.h" |
28 | #include "llvm/Support/YAMLTraits.h" |
29 | #include "llvm/Support/raw_ostream.h" |
30 | #include "llvm/TargetParser/Host.h" |
31 | #include <algorithm> |
32 | #include <cassert> |
33 | #include <cstddef> |
34 | #include <cstdint> |
35 | #include <memory> |
36 | #include <optional> |
37 | #include <string> |
38 | #include <vector> |
39 | |
40 | using namespace llvm; |
41 | |
42 | template <typename T> |
43 | static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) { |
44 | if (IsLittleEndian != sys::IsLittleEndianHost) |
45 | sys::swapByteOrder(Integer); |
46 | OS.write(Ptr: reinterpret_cast<char *>(&Integer), Size: sizeof(T)); |
47 | } |
48 | |
49 | static Error writeVariableSizedInteger(uint64_t Integer, size_t Size, |
50 | raw_ostream &OS, bool IsLittleEndian) { |
51 | if (8 == Size) |
52 | writeInteger(Integer: (uint64_t)Integer, OS, IsLittleEndian); |
53 | else if (4 == Size) |
54 | writeInteger(Integer: (uint32_t)Integer, OS, IsLittleEndian); |
55 | else if (2 == Size) |
56 | writeInteger(Integer: (uint16_t)Integer, OS, IsLittleEndian); |
57 | else if (1 == Size) |
58 | writeInteger(Integer: (uint8_t)Integer, OS, IsLittleEndian); |
59 | else |
60 | return createStringError(EC: errc::not_supported, |
61 | Fmt: "invalid integer write size: %zu" , Vals: Size); |
62 | |
63 | return Error::success(); |
64 | } |
65 | |
66 | static void ZeroFillBytes(raw_ostream &OS, size_t Size) { |
67 | std::vector<uint8_t> FillData(Size, 0); |
68 | OS.write(Ptr: reinterpret_cast<char *>(FillData.data()), Size); |
69 | } |
70 | |
71 | static void writeInitialLength(const dwarf::DwarfFormat Format, |
72 | const uint64_t Length, raw_ostream &OS, |
73 | bool IsLittleEndian) { |
74 | bool IsDWARF64 = Format == dwarf::DWARF64; |
75 | if (IsDWARF64) |
76 | cantFail(Err: writeVariableSizedInteger(Integer: dwarf::DW_LENGTH_DWARF64, Size: 4, OS, |
77 | IsLittleEndian)); |
78 | cantFail( |
79 | Err: writeVariableSizedInteger(Integer: Length, Size: IsDWARF64 ? 8 : 4, OS, IsLittleEndian)); |
80 | } |
81 | |
82 | static void writeDWARFOffset(uint64_t Offset, dwarf::DwarfFormat Format, |
83 | raw_ostream &OS, bool IsLittleEndian) { |
84 | cantFail(Err: writeVariableSizedInteger(Integer: Offset, Size: Format == dwarf::DWARF64 ? 8 : 4, |
85 | OS, IsLittleEndian)); |
86 | } |
87 | |
88 | Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) { |
89 | for (StringRef Str : *DI.DebugStrings) { |
90 | OS.write(Ptr: Str.data(), Size: Str.size()); |
91 | OS.write(C: '\0'); |
92 | } |
93 | |
94 | return Error::success(); |
95 | } |
96 | |
97 | StringRef DWARFYAML::Data::getAbbrevTableContentByIndex(uint64_t Index) const { |
98 | assert(Index < DebugAbbrev.size() && |
99 | "Index should be less than the size of DebugAbbrev array" ); |
100 | auto It = AbbrevTableContents.find(x: Index); |
101 | if (It != AbbrevTableContents.cend()) |
102 | return It->second; |
103 | |
104 | std::string AbbrevTableBuffer; |
105 | raw_string_ostream OS(AbbrevTableBuffer); |
106 | |
107 | uint64_t AbbrevCode = 0; |
108 | for (const DWARFYAML::Abbrev &AbbrevDecl : DebugAbbrev[Index].Table) { |
109 | AbbrevCode = AbbrevDecl.Code ? (uint64_t)*AbbrevDecl.Code : AbbrevCode + 1; |
110 | encodeULEB128(Value: AbbrevCode, OS); |
111 | encodeULEB128(Value: AbbrevDecl.Tag, OS); |
112 | OS.write(C: AbbrevDecl.Children); |
113 | for (const auto &Attr : AbbrevDecl.Attributes) { |
114 | encodeULEB128(Value: Attr.Attribute, OS); |
115 | encodeULEB128(Value: Attr.Form, OS); |
116 | if (Attr.Form == dwarf::DW_FORM_implicit_const) |
117 | encodeSLEB128(Value: Attr.Value, OS); |
118 | } |
119 | encodeULEB128(Value: 0, OS); |
120 | encodeULEB128(Value: 0, OS); |
121 | } |
122 | |
123 | // The abbreviations for a given compilation unit end with an entry |
124 | // consisting of a 0 byte for the abbreviation code. |
125 | OS.write_zeros(NumZeros: 1); |
126 | |
127 | AbbrevTableContents.insert(x: {Index, AbbrevTableBuffer}); |
128 | |
129 | return AbbrevTableContents[Index]; |
130 | } |
131 | |
132 | Error DWARFYAML::emitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) { |
133 | for (uint64_t I = 0; I < DI.DebugAbbrev.size(); ++I) { |
134 | StringRef AbbrevTableContent = DI.getAbbrevTableContentByIndex(Index: I); |
135 | OS.write(Ptr: AbbrevTableContent.data(), Size: AbbrevTableContent.size()); |
136 | } |
137 | |
138 | return Error::success(); |
139 | } |
140 | |
141 | Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) { |
142 | assert(DI.DebugAranges && "unexpected emitDebugAranges() call" ); |
143 | for (const auto &Range : *DI.DebugAranges) { |
144 | uint8_t AddrSize; |
145 | if (Range.AddrSize) |
146 | AddrSize = *Range.AddrSize; |
147 | else |
148 | AddrSize = DI.Is64BitAddrSize ? 8 : 4; |
149 | |
150 | uint64_t Length = 4; // sizeof(version) 2 + sizeof(address_size) 1 + |
151 | // sizeof(segment_selector_size) 1 |
152 | Length += |
153 | Range.Format == dwarf::DWARF64 ? 8 : 4; // sizeof(debug_info_offset) |
154 | |
155 | const uint64_t = |
156 | Length + (Range.Format == dwarf::DWARF64 |
157 | ? 12 |
158 | : 4); // sizeof(unit_header) = 12 (DWARF64) or 4 (DWARF32) |
159 | const uint64_t = alignTo(Value: HeaderLength, Align: AddrSize * 2); |
160 | |
161 | if (Range.Length) { |
162 | Length = *Range.Length; |
163 | } else { |
164 | Length += PaddedHeaderLength - HeaderLength; |
165 | Length += AddrSize * 2 * (Range.Descriptors.size() + 1); |
166 | } |
167 | |
168 | writeInitialLength(Format: Range.Format, Length, OS, IsLittleEndian: DI.IsLittleEndian); |
169 | writeInteger(Integer: (uint16_t)Range.Version, OS, IsLittleEndian: DI.IsLittleEndian); |
170 | writeDWARFOffset(Offset: Range.CuOffset, Format: Range.Format, OS, IsLittleEndian: DI.IsLittleEndian); |
171 | writeInteger(Integer: (uint8_t)AddrSize, OS, IsLittleEndian: DI.IsLittleEndian); |
172 | writeInteger(Integer: (uint8_t)Range.SegSize, OS, IsLittleEndian: DI.IsLittleEndian); |
173 | ZeroFillBytes(OS, Size: PaddedHeaderLength - HeaderLength); |
174 | |
175 | for (const auto &Descriptor : Range.Descriptors) { |
176 | if (Error Err = writeVariableSizedInteger(Integer: Descriptor.Address, Size: AddrSize, |
177 | OS, IsLittleEndian: DI.IsLittleEndian)) |
178 | return createStringError(EC: errc::not_supported, |
179 | Fmt: "unable to write debug_aranges address: %s" , |
180 | Vals: toString(E: std::move(Err)).c_str()); |
181 | cantFail(Err: writeVariableSizedInteger(Integer: Descriptor.Length, Size: AddrSize, OS, |
182 | IsLittleEndian: DI.IsLittleEndian)); |
183 | } |
184 | ZeroFillBytes(OS, Size: AddrSize * 2); |
185 | } |
186 | |
187 | return Error::success(); |
188 | } |
189 | |
190 | Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) { |
191 | const size_t RangesOffset = OS.tell(); |
192 | uint64_t EntryIndex = 0; |
193 | for (const auto &DebugRanges : *DI.DebugRanges) { |
194 | const size_t CurrOffset = OS.tell() - RangesOffset; |
195 | if (DebugRanges.Offset && (uint64_t)*DebugRanges.Offset < CurrOffset) |
196 | return createStringError(EC: errc::invalid_argument, |
197 | S: "'Offset' for 'debug_ranges' with index " + |
198 | Twine(EntryIndex) + |
199 | " must be greater than or equal to the " |
200 | "number of bytes written already (0x" + |
201 | Twine::utohexstr(Val: CurrOffset) + ")" ); |
202 | if (DebugRanges.Offset) |
203 | ZeroFillBytes(OS, Size: *DebugRanges.Offset - CurrOffset); |
204 | |
205 | uint8_t AddrSize; |
206 | if (DebugRanges.AddrSize) |
207 | AddrSize = *DebugRanges.AddrSize; |
208 | else |
209 | AddrSize = DI.Is64BitAddrSize ? 8 : 4; |
210 | for (const auto &Entry : DebugRanges.Entries) { |
211 | if (Error Err = writeVariableSizedInteger(Integer: Entry.LowOffset, Size: AddrSize, OS, |
212 | IsLittleEndian: DI.IsLittleEndian)) |
213 | return createStringError( |
214 | EC: errc::not_supported, |
215 | Fmt: "unable to write debug_ranges address offset: %s" , |
216 | Vals: toString(E: std::move(Err)).c_str()); |
217 | cantFail(Err: writeVariableSizedInteger(Integer: Entry.HighOffset, Size: AddrSize, OS, |
218 | IsLittleEndian: DI.IsLittleEndian)); |
219 | } |
220 | ZeroFillBytes(OS, Size: AddrSize * 2); |
221 | ++EntryIndex; |
222 | } |
223 | |
224 | return Error::success(); |
225 | } |
226 | |
227 | static Error emitPubSection(raw_ostream &OS, const DWARFYAML::PubSection &Sect, |
228 | bool IsLittleEndian, bool IsGNUPubSec = false) { |
229 | writeInitialLength(Format: Sect.Format, Length: Sect.Length, OS, IsLittleEndian); |
230 | writeInteger(Integer: (uint16_t)Sect.Version, OS, IsLittleEndian); |
231 | writeInteger(Integer: (uint32_t)Sect.UnitOffset, OS, IsLittleEndian); |
232 | writeInteger(Integer: (uint32_t)Sect.UnitSize, OS, IsLittleEndian); |
233 | for (const auto &Entry : Sect.Entries) { |
234 | writeInteger(Integer: (uint32_t)Entry.DieOffset, OS, IsLittleEndian); |
235 | if (IsGNUPubSec) |
236 | writeInteger(Integer: (uint8_t)Entry.Descriptor, OS, IsLittleEndian); |
237 | OS.write(Ptr: Entry.Name.data(), Size: Entry.Name.size()); |
238 | OS.write(C: '\0'); |
239 | } |
240 | return Error::success(); |
241 | } |
242 | |
243 | Error DWARFYAML::emitDebugPubnames(raw_ostream &OS, const Data &DI) { |
244 | assert(DI.PubNames && "unexpected emitDebugPubnames() call" ); |
245 | return emitPubSection(OS, Sect: *DI.PubNames, IsLittleEndian: DI.IsLittleEndian); |
246 | } |
247 | |
248 | Error DWARFYAML::emitDebugPubtypes(raw_ostream &OS, const Data &DI) { |
249 | assert(DI.PubTypes && "unexpected emitDebugPubtypes() call" ); |
250 | return emitPubSection(OS, Sect: *DI.PubTypes, IsLittleEndian: DI.IsLittleEndian); |
251 | } |
252 | |
253 | Error DWARFYAML::emitDebugGNUPubnames(raw_ostream &OS, const Data &DI) { |
254 | assert(DI.GNUPubNames && "unexpected emitDebugGNUPubnames() call" ); |
255 | return emitPubSection(OS, Sect: *DI.GNUPubNames, IsLittleEndian: DI.IsLittleEndian, |
256 | /*IsGNUStyle=*/IsGNUPubSec: true); |
257 | } |
258 | |
259 | Error DWARFYAML::emitDebugGNUPubtypes(raw_ostream &OS, const Data &DI) { |
260 | assert(DI.GNUPubTypes && "unexpected emitDebugGNUPubtypes() call" ); |
261 | return emitPubSection(OS, Sect: *DI.GNUPubTypes, IsLittleEndian: DI.IsLittleEndian, |
262 | /*IsGNUStyle=*/IsGNUPubSec: true); |
263 | } |
264 | |
265 | static Expected<uint64_t> writeDIE(const DWARFYAML::Data &DI, uint64_t CUIndex, |
266 | uint64_t AbbrevTableID, |
267 | const dwarf::FormParams &Params, |
268 | const DWARFYAML::Entry &Entry, |
269 | raw_ostream &OS, bool IsLittleEndian) { |
270 | uint64_t EntryBegin = OS.tell(); |
271 | encodeULEB128(Value: Entry.AbbrCode, OS); |
272 | uint32_t AbbrCode = Entry.AbbrCode; |
273 | if (AbbrCode == 0 || Entry.Values.empty()) |
274 | return OS.tell() - EntryBegin; |
275 | |
276 | Expected<DWARFYAML::Data::AbbrevTableInfo> AbbrevTableInfoOrErr = |
277 | DI.getAbbrevTableInfoByID(ID: AbbrevTableID); |
278 | if (!AbbrevTableInfoOrErr) |
279 | return createStringError(EC: errc::invalid_argument, |
280 | S: toString(E: AbbrevTableInfoOrErr.takeError()) + |
281 | " for compilation unit with index " + |
282 | utostr(X: CUIndex)); |
283 | |
284 | ArrayRef<DWARFYAML::Abbrev> AbbrevDecls( |
285 | DI.DebugAbbrev[AbbrevTableInfoOrErr->Index].Table); |
286 | |
287 | if (AbbrCode > AbbrevDecls.size()) |
288 | return createStringError( |
289 | EC: errc::invalid_argument, |
290 | S: "abbrev code must be less than or equal to the number of " |
291 | "entries in abbreviation table" ); |
292 | const DWARFYAML::Abbrev &Abbrev = AbbrevDecls[AbbrCode - 1]; |
293 | auto FormVal = Entry.Values.begin(); |
294 | auto AbbrForm = Abbrev.Attributes.begin(); |
295 | for (; FormVal != Entry.Values.end() && AbbrForm != Abbrev.Attributes.end(); |
296 | ++FormVal, ++AbbrForm) { |
297 | dwarf::Form Form = AbbrForm->Form; |
298 | bool Indirect; |
299 | do { |
300 | Indirect = false; |
301 | switch (Form) { |
302 | case dwarf::DW_FORM_addr: |
303 | // TODO: Test this error. |
304 | if (Error Err = writeVariableSizedInteger( |
305 | Integer: FormVal->Value, Size: Params.AddrSize, OS, IsLittleEndian)) |
306 | return std::move(Err); |
307 | break; |
308 | case dwarf::DW_FORM_ref_addr: |
309 | // TODO: Test this error. |
310 | if (Error Err = writeVariableSizedInteger(Integer: FormVal->Value, |
311 | Size: Params.getRefAddrByteSize(), |
312 | OS, IsLittleEndian)) |
313 | return std::move(Err); |
314 | break; |
315 | case dwarf::DW_FORM_exprloc: |
316 | case dwarf::DW_FORM_block: |
317 | encodeULEB128(Value: FormVal->BlockData.size(), OS); |
318 | OS.write(Ptr: (const char *)FormVal->BlockData.data(), |
319 | Size: FormVal->BlockData.size()); |
320 | break; |
321 | case dwarf::DW_FORM_block1: { |
322 | writeInteger(Integer: (uint8_t)FormVal->BlockData.size(), OS, IsLittleEndian); |
323 | OS.write(Ptr: (const char *)FormVal->BlockData.data(), |
324 | Size: FormVal->BlockData.size()); |
325 | break; |
326 | } |
327 | case dwarf::DW_FORM_block2: { |
328 | writeInteger(Integer: (uint16_t)FormVal->BlockData.size(), OS, IsLittleEndian); |
329 | OS.write(Ptr: (const char *)FormVal->BlockData.data(), |
330 | Size: FormVal->BlockData.size()); |
331 | break; |
332 | } |
333 | case dwarf::DW_FORM_block4: { |
334 | writeInteger(Integer: (uint32_t)FormVal->BlockData.size(), OS, IsLittleEndian); |
335 | OS.write(Ptr: (const char *)FormVal->BlockData.data(), |
336 | Size: FormVal->BlockData.size()); |
337 | break; |
338 | } |
339 | case dwarf::DW_FORM_strx: |
340 | case dwarf::DW_FORM_addrx: |
341 | case dwarf::DW_FORM_rnglistx: |
342 | case dwarf::DW_FORM_loclistx: |
343 | case dwarf::DW_FORM_udata: |
344 | case dwarf::DW_FORM_ref_udata: |
345 | case dwarf::DW_FORM_GNU_addr_index: |
346 | case dwarf::DW_FORM_GNU_str_index: |
347 | encodeULEB128(Value: FormVal->Value, OS); |
348 | break; |
349 | case dwarf::DW_FORM_data1: |
350 | case dwarf::DW_FORM_ref1: |
351 | case dwarf::DW_FORM_flag: |
352 | case dwarf::DW_FORM_strx1: |
353 | case dwarf::DW_FORM_addrx1: |
354 | writeInteger(Integer: (uint8_t)FormVal->Value, OS, IsLittleEndian); |
355 | break; |
356 | case dwarf::DW_FORM_data2: |
357 | case dwarf::DW_FORM_ref2: |
358 | case dwarf::DW_FORM_strx2: |
359 | case dwarf::DW_FORM_addrx2: |
360 | writeInteger(Integer: (uint16_t)FormVal->Value, OS, IsLittleEndian); |
361 | break; |
362 | case dwarf::DW_FORM_data4: |
363 | case dwarf::DW_FORM_ref4: |
364 | case dwarf::DW_FORM_ref_sup4: |
365 | case dwarf::DW_FORM_strx4: |
366 | case dwarf::DW_FORM_addrx4: |
367 | writeInteger(Integer: (uint32_t)FormVal->Value, OS, IsLittleEndian); |
368 | break; |
369 | case dwarf::DW_FORM_data8: |
370 | case dwarf::DW_FORM_ref8: |
371 | case dwarf::DW_FORM_ref_sup8: |
372 | case dwarf::DW_FORM_ref_sig8: |
373 | writeInteger(Integer: (uint64_t)FormVal->Value, OS, IsLittleEndian); |
374 | break; |
375 | case dwarf::DW_FORM_sdata: |
376 | encodeSLEB128(Value: FormVal->Value, OS); |
377 | break; |
378 | case dwarf::DW_FORM_string: |
379 | OS.write(Ptr: FormVal->CStr.data(), Size: FormVal->CStr.size()); |
380 | OS.write(C: '\0'); |
381 | break; |
382 | case dwarf::DW_FORM_indirect: |
383 | encodeULEB128(Value: FormVal->Value, OS); |
384 | Indirect = true; |
385 | Form = static_cast<dwarf::Form>((uint64_t)FormVal->Value); |
386 | ++FormVal; |
387 | break; |
388 | case dwarf::DW_FORM_strp: |
389 | case dwarf::DW_FORM_sec_offset: |
390 | case dwarf::DW_FORM_GNU_ref_alt: |
391 | case dwarf::DW_FORM_GNU_strp_alt: |
392 | case dwarf::DW_FORM_line_strp: |
393 | case dwarf::DW_FORM_strp_sup: |
394 | cantFail(Err: writeVariableSizedInteger(Integer: FormVal->Value, |
395 | Size: Params.getDwarfOffsetByteSize(), OS, |
396 | IsLittleEndian)); |
397 | break; |
398 | default: |
399 | break; |
400 | } |
401 | } while (Indirect); |
402 | } |
403 | |
404 | return OS.tell() - EntryBegin; |
405 | } |
406 | |
407 | Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) { |
408 | for (uint64_t I = 0; I < DI.CompileUnits.size(); ++I) { |
409 | const DWARFYAML::Unit &Unit = DI.CompileUnits[I]; |
410 | uint8_t AddrSize; |
411 | if (Unit.AddrSize) |
412 | AddrSize = *Unit.AddrSize; |
413 | else |
414 | AddrSize = DI.Is64BitAddrSize ? 8 : 4; |
415 | dwarf::FormParams Params = {.Version: Unit.Version, .AddrSize: AddrSize, .Format: Unit.Format}; |
416 | uint64_t Length = 3; // sizeof(version) + sizeof(address_size) |
417 | Length += Unit.Version >= 5 ? 1 : 0; // sizeof(unit_type) |
418 | Length += Params.getDwarfOffsetByteSize(); // sizeof(debug_abbrev_offset) |
419 | |
420 | // Since the length of the current compilation unit is undetermined yet, we |
421 | // firstly write the content of the compilation unit to a buffer to |
422 | // calculate it and then serialize the buffer content to the actual output |
423 | // stream. |
424 | std::string EntryBuffer; |
425 | raw_string_ostream EntryBufferOS(EntryBuffer); |
426 | |
427 | uint64_t AbbrevTableID = Unit.AbbrevTableID.value_or(u&: I); |
428 | for (const DWARFYAML::Entry &Entry : Unit.Entries) { |
429 | if (Expected<uint64_t> EntryLength = |
430 | writeDIE(DI, CUIndex: I, AbbrevTableID, Params, Entry, OS&: EntryBufferOS, |
431 | IsLittleEndian: DI.IsLittleEndian)) |
432 | Length += *EntryLength; |
433 | else |
434 | return EntryLength.takeError(); |
435 | } |
436 | |
437 | // If the length is specified in the YAML description, we use it instead of |
438 | // the actual length. |
439 | if (Unit.Length) |
440 | Length = *Unit.Length; |
441 | |
442 | writeInitialLength(Format: Unit.Format, Length, OS, IsLittleEndian: DI.IsLittleEndian); |
443 | writeInteger(Integer: (uint16_t)Unit.Version, OS, IsLittleEndian: DI.IsLittleEndian); |
444 | |
445 | uint64_t AbbrevTableOffset = 0; |
446 | if (Unit.AbbrOffset) { |
447 | AbbrevTableOffset = *Unit.AbbrOffset; |
448 | } else { |
449 | if (Expected<DWARFYAML::Data::AbbrevTableInfo> AbbrevTableInfoOrErr = |
450 | DI.getAbbrevTableInfoByID(ID: AbbrevTableID)) { |
451 | AbbrevTableOffset = AbbrevTableInfoOrErr->Offset; |
452 | } else { |
453 | // The current compilation unit may not have DIEs and it will not be |
454 | // able to find the associated abbrev table. We consume the error and |
455 | // assign 0 to the debug_abbrev_offset in such circumstances. |
456 | consumeError(Err: AbbrevTableInfoOrErr.takeError()); |
457 | } |
458 | } |
459 | |
460 | if (Unit.Version >= 5) { |
461 | writeInteger(Integer: (uint8_t)Unit.Type, OS, IsLittleEndian: DI.IsLittleEndian); |
462 | writeInteger(Integer: (uint8_t)AddrSize, OS, IsLittleEndian: DI.IsLittleEndian); |
463 | writeDWARFOffset(Offset: AbbrevTableOffset, Format: Unit.Format, OS, IsLittleEndian: DI.IsLittleEndian); |
464 | } else { |
465 | writeDWARFOffset(Offset: AbbrevTableOffset, Format: Unit.Format, OS, IsLittleEndian: DI.IsLittleEndian); |
466 | writeInteger(Integer: (uint8_t)AddrSize, OS, IsLittleEndian: DI.IsLittleEndian); |
467 | } |
468 | |
469 | OS.write(Ptr: EntryBuffer.data(), Size: EntryBuffer.size()); |
470 | } |
471 | |
472 | return Error::success(); |
473 | } |
474 | |
475 | static void emitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) { |
476 | OS.write(Ptr: File.Name.data(), Size: File.Name.size()); |
477 | OS.write(C: '\0'); |
478 | encodeULEB128(Value: File.DirIdx, OS); |
479 | encodeULEB128(Value: File.ModTime, OS); |
480 | encodeULEB128(Value: File.Length, OS); |
481 | } |
482 | |
483 | static void writeExtendedOpcode(const DWARFYAML::LineTableOpcode &Op, |
484 | uint8_t AddrSize, bool IsLittleEndian, |
485 | raw_ostream &OS) { |
486 | // The first byte of extended opcodes is a zero byte. The next bytes are an |
487 | // ULEB128 integer giving the number of bytes in the instruction itself (does |
488 | // not include the first zero byte or the size). We serialize the instruction |
489 | // itself into the OpBuffer and then write the size of the buffer and the |
490 | // buffer to the real output stream. |
491 | std::string OpBuffer; |
492 | raw_string_ostream OpBufferOS(OpBuffer); |
493 | writeInteger(Integer: (uint8_t)Op.SubOpcode, OS&: OpBufferOS, IsLittleEndian); |
494 | switch (Op.SubOpcode) { |
495 | case dwarf::DW_LNE_set_address: |
496 | cantFail(Err: writeVariableSizedInteger(Integer: Op.Data, Size: AddrSize, OS&: OpBufferOS, |
497 | IsLittleEndian)); |
498 | break; |
499 | case dwarf::DW_LNE_define_file: |
500 | emitFileEntry(OS&: OpBufferOS, File: Op.FileEntry); |
501 | break; |
502 | case dwarf::DW_LNE_set_discriminator: |
503 | encodeULEB128(Value: Op.Data, OS&: OpBufferOS); |
504 | break; |
505 | case dwarf::DW_LNE_end_sequence: |
506 | break; |
507 | default: |
508 | for (auto OpByte : Op.UnknownOpcodeData) |
509 | writeInteger(Integer: (uint8_t)OpByte, OS&: OpBufferOS, IsLittleEndian); |
510 | } |
511 | uint64_t ExtLen = Op.ExtLen.value_or(u: OpBuffer.size()); |
512 | encodeULEB128(Value: ExtLen, OS); |
513 | OS.write(Ptr: OpBuffer.data(), Size: OpBuffer.size()); |
514 | } |
515 | |
516 | static void writeLineTableOpcode(const DWARFYAML::LineTableOpcode &Op, |
517 | uint8_t OpcodeBase, uint8_t AddrSize, |
518 | raw_ostream &OS, bool IsLittleEndian) { |
519 | writeInteger(Integer: (uint8_t)Op.Opcode, OS, IsLittleEndian); |
520 | if (Op.Opcode == 0) { |
521 | writeExtendedOpcode(Op, AddrSize, IsLittleEndian, OS); |
522 | } else if (Op.Opcode < OpcodeBase) { |
523 | switch (Op.Opcode) { |
524 | case dwarf::DW_LNS_copy: |
525 | case dwarf::DW_LNS_negate_stmt: |
526 | case dwarf::DW_LNS_set_basic_block: |
527 | case dwarf::DW_LNS_const_add_pc: |
528 | case dwarf::DW_LNS_set_prologue_end: |
529 | case dwarf::DW_LNS_set_epilogue_begin: |
530 | break; |
531 | |
532 | case dwarf::DW_LNS_advance_pc: |
533 | case dwarf::DW_LNS_set_file: |
534 | case dwarf::DW_LNS_set_column: |
535 | case dwarf::DW_LNS_set_isa: |
536 | encodeULEB128(Value: Op.Data, OS); |
537 | break; |
538 | |
539 | case dwarf::DW_LNS_advance_line: |
540 | encodeSLEB128(Value: Op.SData, OS); |
541 | break; |
542 | |
543 | case dwarf::DW_LNS_fixed_advance_pc: |
544 | writeInteger(Integer: (uint16_t)Op.Data, OS, IsLittleEndian); |
545 | break; |
546 | |
547 | default: |
548 | for (auto OpData : Op.StandardOpcodeData) { |
549 | encodeULEB128(Value: OpData, OS); |
550 | } |
551 | } |
552 | } |
553 | } |
554 | |
555 | static std::vector<uint8_t> |
556 | getStandardOpcodeLengths(uint16_t Version, std::optional<uint8_t> OpcodeBase) { |
557 | // If the opcode_base field isn't specified, we returns the |
558 | // standard_opcode_lengths array according to the version by default. |
559 | std::vector<uint8_t> StandardOpcodeLengths{0, 1, 1, 1, 1, 0, |
560 | 0, 0, 1, 0, 0, 1}; |
561 | if (Version == 2) { |
562 | // DWARF v2 uses the same first 9 standard opcodes as v3-5. |
563 | StandardOpcodeLengths.resize(new_size: 9); |
564 | } else if (OpcodeBase) { |
565 | StandardOpcodeLengths.resize(new_size: *OpcodeBase > 0 ? *OpcodeBase - 1 : 0, x: 0); |
566 | } |
567 | return StandardOpcodeLengths; |
568 | } |
569 | |
570 | Error DWARFYAML::emitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) { |
571 | for (const DWARFYAML::LineTable &LineTable : DI.DebugLines) { |
572 | // Buffer holds the bytes following the header_length (or prologue_length in |
573 | // DWARFv2) field to the end of the line number program itself. |
574 | std::string Buffer; |
575 | raw_string_ostream BufferOS(Buffer); |
576 | |
577 | writeInteger(Integer: LineTable.MinInstLength, OS&: BufferOS, IsLittleEndian: DI.IsLittleEndian); |
578 | // TODO: Add support for emitting DWARFv5 line table. |
579 | if (LineTable.Version >= 4) |
580 | writeInteger(Integer: LineTable.MaxOpsPerInst, OS&: BufferOS, IsLittleEndian: DI.IsLittleEndian); |
581 | writeInteger(Integer: LineTable.DefaultIsStmt, OS&: BufferOS, IsLittleEndian: DI.IsLittleEndian); |
582 | writeInteger(Integer: LineTable.LineBase, OS&: BufferOS, IsLittleEndian: DI.IsLittleEndian); |
583 | writeInteger(Integer: LineTable.LineRange, OS&: BufferOS, IsLittleEndian: DI.IsLittleEndian); |
584 | |
585 | std::vector<uint8_t> StandardOpcodeLengths = |
586 | LineTable.StandardOpcodeLengths.value_or( |
587 | u: getStandardOpcodeLengths(Version: LineTable.Version, OpcodeBase: LineTable.OpcodeBase)); |
588 | uint8_t OpcodeBase = LineTable.OpcodeBase |
589 | ? *LineTable.OpcodeBase |
590 | : StandardOpcodeLengths.size() + 1; |
591 | writeInteger(Integer: OpcodeBase, OS&: BufferOS, IsLittleEndian: DI.IsLittleEndian); |
592 | for (uint8_t OpcodeLength : StandardOpcodeLengths) |
593 | writeInteger(Integer: OpcodeLength, OS&: BufferOS, IsLittleEndian: DI.IsLittleEndian); |
594 | |
595 | for (StringRef IncludeDir : LineTable.IncludeDirs) { |
596 | BufferOS.write(Ptr: IncludeDir.data(), Size: IncludeDir.size()); |
597 | BufferOS.write(C: '\0'); |
598 | } |
599 | BufferOS.write(C: '\0'); |
600 | |
601 | for (const DWARFYAML::File &File : LineTable.Files) |
602 | emitFileEntry(OS&: BufferOS, File); |
603 | BufferOS.write(C: '\0'); |
604 | |
605 | uint64_t = |
606 | LineTable.PrologueLength ? *LineTable.PrologueLength : Buffer.size(); |
607 | |
608 | for (const DWARFYAML::LineTableOpcode &Op : LineTable.Opcodes) |
609 | writeLineTableOpcode(Op, OpcodeBase, AddrSize: DI.Is64BitAddrSize ? 8 : 4, OS&: BufferOS, |
610 | IsLittleEndian: DI.IsLittleEndian); |
611 | |
612 | uint64_t Length; |
613 | if (LineTable.Length) { |
614 | Length = *LineTable.Length; |
615 | } else { |
616 | Length = 2; // sizeof(version) |
617 | Length += |
618 | (LineTable.Format == dwarf::DWARF64 ? 8 : 4); // sizeof(header_length) |
619 | Length += Buffer.size(); |
620 | } |
621 | |
622 | writeInitialLength(Format: LineTable.Format, Length, OS, IsLittleEndian: DI.IsLittleEndian); |
623 | writeInteger(Integer: LineTable.Version, OS, IsLittleEndian: DI.IsLittleEndian); |
624 | writeDWARFOffset(Offset: HeaderLength, Format: LineTable.Format, OS, IsLittleEndian: DI.IsLittleEndian); |
625 | OS.write(Ptr: Buffer.data(), Size: Buffer.size()); |
626 | } |
627 | |
628 | return Error::success(); |
629 | } |
630 | |
631 | Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) { |
632 | for (const AddrTableEntry &TableEntry : *DI.DebugAddr) { |
633 | uint8_t AddrSize; |
634 | if (TableEntry.AddrSize) |
635 | AddrSize = *TableEntry.AddrSize; |
636 | else |
637 | AddrSize = DI.Is64BitAddrSize ? 8 : 4; |
638 | |
639 | uint64_t Length; |
640 | if (TableEntry.Length) |
641 | Length = (uint64_t)*TableEntry.Length; |
642 | else |
643 | // 2 (version) + 1 (address_size) + 1 (segment_selector_size) = 4 |
644 | Length = 4 + (AddrSize + TableEntry.SegSelectorSize) * |
645 | TableEntry.SegAddrPairs.size(); |
646 | |
647 | writeInitialLength(Format: TableEntry.Format, Length, OS, IsLittleEndian: DI.IsLittleEndian); |
648 | writeInteger(Integer: (uint16_t)TableEntry.Version, OS, IsLittleEndian: DI.IsLittleEndian); |
649 | writeInteger(Integer: (uint8_t)AddrSize, OS, IsLittleEndian: DI.IsLittleEndian); |
650 | writeInteger(Integer: (uint8_t)TableEntry.SegSelectorSize, OS, IsLittleEndian: DI.IsLittleEndian); |
651 | |
652 | for (const SegAddrPair &Pair : TableEntry.SegAddrPairs) { |
653 | if (TableEntry.SegSelectorSize != yaml::Hex8{0}) |
654 | if (Error Err = writeVariableSizedInteger(Integer: Pair.Segment, |
655 | Size: TableEntry.SegSelectorSize, |
656 | OS, IsLittleEndian: DI.IsLittleEndian)) |
657 | return createStringError(EC: errc::not_supported, |
658 | Fmt: "unable to write debug_addr segment: %s" , |
659 | Vals: toString(E: std::move(Err)).c_str()); |
660 | if (AddrSize != 0) |
661 | if (Error Err = writeVariableSizedInteger(Integer: Pair.Address, Size: AddrSize, OS, |
662 | IsLittleEndian: DI.IsLittleEndian)) |
663 | return createStringError(EC: errc::not_supported, |
664 | Fmt: "unable to write debug_addr address: %s" , |
665 | Vals: toString(E: std::move(Err)).c_str()); |
666 | } |
667 | } |
668 | |
669 | return Error::success(); |
670 | } |
671 | |
672 | Error DWARFYAML::emitDebugStrOffsets(raw_ostream &OS, const Data &DI) { |
673 | assert(DI.DebugStrOffsets && "unexpected emitDebugStrOffsets() call" ); |
674 | for (const DWARFYAML::StringOffsetsTable &Table : *DI.DebugStrOffsets) { |
675 | uint64_t Length; |
676 | if (Table.Length) |
677 | Length = *Table.Length; |
678 | else |
679 | // sizeof(version) + sizeof(padding) = 4 |
680 | Length = |
681 | 4 + Table.Offsets.size() * (Table.Format == dwarf::DWARF64 ? 8 : 4); |
682 | |
683 | writeInitialLength(Format: Table.Format, Length, OS, IsLittleEndian: DI.IsLittleEndian); |
684 | writeInteger(Integer: (uint16_t)Table.Version, OS, IsLittleEndian: DI.IsLittleEndian); |
685 | writeInteger(Integer: (uint16_t)Table.Padding, OS, IsLittleEndian: DI.IsLittleEndian); |
686 | |
687 | for (uint64_t Offset : Table.Offsets) |
688 | writeDWARFOffset(Offset, Format: Table.Format, OS, IsLittleEndian: DI.IsLittleEndian); |
689 | } |
690 | |
691 | return Error::success(); |
692 | } |
693 | |
694 | namespace { |
695 | /// Emits the header for a DebugNames section. |
696 | void (raw_ostream &OS, bool IsLittleEndian, |
697 | uint32_t NameCount, uint32_t AbbrevSize, |
698 | uint32_t CombinedSizeOtherParts) { |
699 | // Use the same AugmentationString as AsmPrinter. |
700 | StringRef AugmentationString = "LLVM0700" ; |
701 | size_t TotalSize = CombinedSizeOtherParts + 5 * sizeof(uint32_t) + |
702 | 2 * sizeof(uint16_t) + sizeof(NameCount) + |
703 | sizeof(AbbrevSize) + AugmentationString.size(); |
704 | writeInteger(Integer: uint32_t(TotalSize), OS, IsLittleEndian); // Unit length |
705 | |
706 | // Everything below is included in total size. |
707 | writeInteger(Integer: uint16_t(5), OS, IsLittleEndian); // Version |
708 | writeInteger(Integer: uint16_t(0), OS, IsLittleEndian); // Padding |
709 | writeInteger(Integer: uint32_t(1), OS, IsLittleEndian); // Compilation Unit count |
710 | writeInteger(Integer: uint32_t(0), OS, IsLittleEndian); // Local Type Unit count |
711 | writeInteger(Integer: uint32_t(0), OS, IsLittleEndian); // Foreign Type Unit count |
712 | writeInteger(Integer: uint32_t(0), OS, IsLittleEndian); // Bucket count |
713 | writeInteger(Integer: NameCount, OS, IsLittleEndian); |
714 | writeInteger(Integer: AbbrevSize, OS, IsLittleEndian); |
715 | writeInteger(Integer: uint32_t(AugmentationString.size()), OS, IsLittleEndian); |
716 | OS.write(Ptr: AugmentationString.data(), Size: AugmentationString.size()); |
717 | return; |
718 | } |
719 | |
720 | /// Emits the abbreviations for a DebugNames section. |
721 | std::string |
722 | emitDebugNamesAbbrev(ArrayRef<DWARFYAML::DebugNameAbbreviation> Abbrevs) { |
723 | std::string Data; |
724 | raw_string_ostream OS(Data); |
725 | for (const DWARFYAML::DebugNameAbbreviation &Abbrev : Abbrevs) { |
726 | encodeULEB128(Value: Abbrev.Code, OS); |
727 | encodeULEB128(Value: Abbrev.Tag, OS); |
728 | for (auto [Idx, Form] : Abbrev.Indices) { |
729 | encodeULEB128(Value: Idx, OS); |
730 | encodeULEB128(Value: Form, OS); |
731 | } |
732 | encodeULEB128(Value: 0, OS); |
733 | encodeULEB128(Value: 0, OS); |
734 | } |
735 | encodeULEB128(Value: 0, OS); |
736 | return Data; |
737 | } |
738 | |
739 | /// Emits a simple CU offsets list for a DebugNames section containing a single |
740 | /// CU at offset 0. |
741 | std::string emitDebugNamesCUOffsets(bool IsLittleEndian) { |
742 | std::string Data; |
743 | raw_string_ostream OS(Data); |
744 | writeInteger(Integer: uint32_t(0), OS, IsLittleEndian); |
745 | return Data; |
746 | } |
747 | |
748 | /// Emits the "NameTable" for a DebugNames section; according to the spec, it |
749 | /// consists of two arrays: an array of string offsets, followed immediately by |
750 | /// an array of entry offsets. The string offsets are emitted in the order |
751 | /// provided in `Entries`. |
752 | std::string emitDebugNamesNameTable( |
753 | bool IsLittleEndian, |
754 | const DenseMap<uint32_t, std::vector<DWARFYAML::DebugNameEntry>> &Entries, |
755 | ArrayRef<uint32_t> EntryPoolOffsets) { |
756 | assert(Entries.size() == EntryPoolOffsets.size()); |
757 | |
758 | std::string Data; |
759 | raw_string_ostream OS(Data); |
760 | |
761 | for (uint32_t Strp : make_first_range(c: Entries)) |
762 | writeInteger(Integer: Strp, OS, IsLittleEndian); |
763 | for (uint32_t PoolOffset : EntryPoolOffsets) |
764 | writeInteger(Integer: PoolOffset, OS, IsLittleEndian); |
765 | return Data; |
766 | } |
767 | |
768 | /// Groups entries based on their name (strp) code and returns a map. |
769 | DenseMap<uint32_t, std::vector<DWARFYAML::DebugNameEntry>> |
770 | groupEntries(ArrayRef<DWARFYAML::DebugNameEntry> Entries) { |
771 | DenseMap<uint32_t, std::vector<DWARFYAML::DebugNameEntry>> StrpToEntries; |
772 | for (const DWARFYAML::DebugNameEntry &Entry : Entries) |
773 | StrpToEntries[Entry.NameStrp].push_back(x: Entry); |
774 | return StrpToEntries; |
775 | } |
776 | |
777 | /// Finds the abbreviation whose code is AbbrevCode and returns a list |
778 | /// containing the expected size of all non-zero-length forms. |
779 | Expected<SmallVector<uint8_t>> |
780 | getNonZeroDataSizesFor(uint32_t AbbrevCode, |
781 | ArrayRef<DWARFYAML::DebugNameAbbreviation> Abbrevs) { |
782 | const auto *AbbrevIt = find_if(Range&: Abbrevs, P: [&](const auto &Abbrev) { |
783 | return Abbrev.Code.value == AbbrevCode; |
784 | }); |
785 | if (AbbrevIt == Abbrevs.end()) |
786 | return createStringError(EC: inconvertibleErrorCode(), |
787 | S: "did not find an Abbreviation for this code" ); |
788 | |
789 | SmallVector<uint8_t> DataSizes; |
790 | dwarf::FormParams Params{/*Version=*/5, /*AddrSize=*/4, .Format: dwarf::DWARF32}; |
791 | for (auto [Idx, Form] : AbbrevIt->Indices) { |
792 | std::optional<uint8_t> FormSize = dwarf::getFixedFormByteSize(Form, Params); |
793 | if (!FormSize) |
794 | return createStringError(EC: inconvertibleErrorCode(), |
795 | S: "unsupported Form for YAML debug_names emitter" ); |
796 | if (FormSize == 0) |
797 | continue; |
798 | DataSizes.push_back(Elt: *FormSize); |
799 | } |
800 | return DataSizes; |
801 | } |
802 | |
803 | struct PoolOffsetsAndData { |
804 | std::string PoolData; |
805 | std::vector<uint32_t> PoolOffsets; |
806 | }; |
807 | |
808 | /// Emits the entry pool and returns an array of offsets containing the start |
809 | /// offset for the entries of each unique name. |
810 | /// Verifies that the provided number of data values match those expected by |
811 | /// the abbreviation table. |
812 | Expected<PoolOffsetsAndData> emitDebugNamesEntryPool( |
813 | bool IsLittleEndian, |
814 | const DenseMap<uint32_t, std::vector<DWARFYAML::DebugNameEntry>> |
815 | &StrpToEntries, |
816 | ArrayRef<DWARFYAML::DebugNameAbbreviation> Abbrevs) { |
817 | PoolOffsetsAndData Result; |
818 | raw_string_ostream OS(Result.PoolData); |
819 | |
820 | for (ArrayRef<DWARFYAML::DebugNameEntry> EntriesWithSameName : |
821 | make_second_range(c: StrpToEntries)) { |
822 | Result.PoolOffsets.push_back(x: Result.PoolData.size()); |
823 | |
824 | for (const DWARFYAML::DebugNameEntry &Entry : EntriesWithSameName) { |
825 | encodeULEB128(Value: Entry.Code, OS); |
826 | |
827 | Expected<SmallVector<uint8_t>> DataSizes = |
828 | getNonZeroDataSizesFor(AbbrevCode: Entry.Code, Abbrevs); |
829 | if (!DataSizes) |
830 | return DataSizes.takeError(); |
831 | if (DataSizes->size() != Entry.Values.size()) |
832 | return createStringError( |
833 | EC: inconvertibleErrorCode(), |
834 | S: "mismatch between provided and required number of values" ); |
835 | |
836 | for (auto [Value, ValueSize] : zip_equal(t: Entry.Values, u&: *DataSizes)) |
837 | if (Error E = |
838 | writeVariableSizedInteger(Integer: Value, Size: ValueSize, OS, IsLittleEndian)) |
839 | return std::move(E); |
840 | } |
841 | encodeULEB128(Value: 0, OS); |
842 | } |
843 | |
844 | return Result; |
845 | } |
846 | } // namespace |
847 | |
848 | Error DWARFYAML::emitDebugNames(raw_ostream &OS, const Data &DI) { |
849 | assert(DI.DebugNames && "unexpected emitDebugNames() call" ); |
850 | const DebugNamesSection DebugNames = DI.DebugNames.value(); |
851 | |
852 | DenseMap<uint32_t, std::vector<DebugNameEntry>> StrpToEntries = |
853 | groupEntries(Entries: DebugNames.Entries); |
854 | |
855 | // Emit all sub-sections into individual strings so that we may compute |
856 | // relative offsets and sizes. |
857 | Expected<PoolOffsetsAndData> PoolInfo = emitDebugNamesEntryPool( |
858 | IsLittleEndian: DI.IsLittleEndian, StrpToEntries, Abbrevs: DebugNames.Abbrevs); |
859 | if (!PoolInfo) |
860 | return PoolInfo.takeError(); |
861 | std::string NamesTableData = emitDebugNamesNameTable( |
862 | IsLittleEndian: DI.IsLittleEndian, Entries: StrpToEntries, EntryPoolOffsets: PoolInfo->PoolOffsets); |
863 | |
864 | std::string AbbrevData = emitDebugNamesAbbrev(Abbrevs: DebugNames.Abbrevs); |
865 | std::string CUOffsetsData = emitDebugNamesCUOffsets(IsLittleEndian: DI.IsLittleEndian); |
866 | |
867 | size_t TotalSize = PoolInfo->PoolData.size() + NamesTableData.size() + |
868 | AbbrevData.size() + CUOffsetsData.size(); |
869 | |
870 | // Start real emission by combining all individual strings. |
871 | emitDebugNamesHeader(OS, IsLittleEndian: DI.IsLittleEndian, NameCount: StrpToEntries.size(), |
872 | AbbrevSize: AbbrevData.size(), CombinedSizeOtherParts: TotalSize); |
873 | OS.write(Ptr: CUOffsetsData.data(), Size: CUOffsetsData.size()); |
874 | // No local TUs, no foreign TUs, no hash lookups table. |
875 | OS.write(Ptr: NamesTableData.data(), Size: NamesTableData.size()); |
876 | OS.write(Ptr: AbbrevData.data(), Size: AbbrevData.size()); |
877 | OS.write(Ptr: PoolInfo->PoolData.data(), Size: PoolInfo->PoolData.size()); |
878 | |
879 | return Error::success(); |
880 | } |
881 | |
882 | static Error checkOperandCount(StringRef EncodingString, |
883 | ArrayRef<yaml::Hex64> Values, |
884 | uint64_t ExpectedOperands) { |
885 | if (Values.size() != ExpectedOperands) |
886 | return createStringError( |
887 | EC: errc::invalid_argument, |
888 | Fmt: "invalid number (%zu) of operands for the operator: %s, %" PRIu64 |
889 | " expected" , |
890 | Vals: Values.size(), Vals: EncodingString.str().c_str(), Vals: ExpectedOperands); |
891 | |
892 | return Error::success(); |
893 | } |
894 | |
895 | static Error writeListEntryAddress(StringRef EncodingName, raw_ostream &OS, |
896 | uint64_t Addr, uint8_t AddrSize, |
897 | bool IsLittleEndian) { |
898 | if (Error Err = writeVariableSizedInteger(Integer: Addr, Size: AddrSize, OS, IsLittleEndian)) |
899 | return createStringError(EC: errc::invalid_argument, |
900 | Fmt: "unable to write address for the operator %s: %s" , |
901 | Vals: EncodingName.str().c_str(), |
902 | Vals: toString(E: std::move(Err)).c_str()); |
903 | |
904 | return Error::success(); |
905 | } |
906 | |
907 | static Expected<uint64_t> |
908 | writeDWARFExpression(raw_ostream &OS, |
909 | const DWARFYAML::DWARFOperation &Operation, |
910 | uint8_t AddrSize, bool IsLittleEndian) { |
911 | auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error { |
912 | return checkOperandCount(EncodingString: dwarf::OperationEncodingString(Encoding: Operation.Operator), |
913 | Values: Operation.Values, ExpectedOperands); |
914 | }; |
915 | |
916 | uint64_t ExpressionBegin = OS.tell(); |
917 | writeInteger(Integer: (uint8_t)Operation.Operator, OS, IsLittleEndian); |
918 | switch (Operation.Operator) { |
919 | case dwarf::DW_OP_consts: |
920 | if (Error Err = CheckOperands(1)) |
921 | return std::move(Err); |
922 | encodeSLEB128(Value: Operation.Values[0], OS); |
923 | break; |
924 | case dwarf::DW_OP_stack_value: |
925 | if (Error Err = CheckOperands(0)) |
926 | return std::move(Err); |
927 | break; |
928 | default: |
929 | StringRef EncodingStr = dwarf::OperationEncodingString(Encoding: Operation.Operator); |
930 | return createStringError(EC: errc::not_supported, |
931 | S: "DWARF expression: " + |
932 | (EncodingStr.empty() |
933 | ? "0x" + utohexstr(X: Operation.Operator) |
934 | : EncodingStr) + |
935 | " is not supported" ); |
936 | } |
937 | return OS.tell() - ExpressionBegin; |
938 | } |
939 | |
940 | static Expected<uint64_t> writeListEntry(raw_ostream &OS, |
941 | const DWARFYAML::RnglistEntry &Entry, |
942 | uint8_t AddrSize, |
943 | bool IsLittleEndian) { |
944 | uint64_t BeginOffset = OS.tell(); |
945 | writeInteger(Integer: (uint8_t)Entry.Operator, OS, IsLittleEndian); |
946 | |
947 | StringRef EncodingName = dwarf::RangeListEncodingString(Encoding: Entry.Operator); |
948 | |
949 | auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error { |
950 | return checkOperandCount(EncodingString: EncodingName, Values: Entry.Values, ExpectedOperands); |
951 | }; |
952 | |
953 | auto WriteAddress = [&](uint64_t Addr) -> Error { |
954 | return writeListEntryAddress(EncodingName, OS, Addr, AddrSize, |
955 | IsLittleEndian); |
956 | }; |
957 | |
958 | switch (Entry.Operator) { |
959 | case dwarf::DW_RLE_end_of_list: |
960 | if (Error Err = CheckOperands(0)) |
961 | return std::move(Err); |
962 | break; |
963 | case dwarf::DW_RLE_base_addressx: |
964 | if (Error Err = CheckOperands(1)) |
965 | return std::move(Err); |
966 | encodeULEB128(Value: Entry.Values[0], OS); |
967 | break; |
968 | case dwarf::DW_RLE_startx_endx: |
969 | case dwarf::DW_RLE_startx_length: |
970 | case dwarf::DW_RLE_offset_pair: |
971 | if (Error Err = CheckOperands(2)) |
972 | return std::move(Err); |
973 | encodeULEB128(Value: Entry.Values[0], OS); |
974 | encodeULEB128(Value: Entry.Values[1], OS); |
975 | break; |
976 | case dwarf::DW_RLE_base_address: |
977 | if (Error Err = CheckOperands(1)) |
978 | return std::move(Err); |
979 | if (Error Err = WriteAddress(Entry.Values[0])) |
980 | return std::move(Err); |
981 | break; |
982 | case dwarf::DW_RLE_start_end: |
983 | if (Error Err = CheckOperands(2)) |
984 | return std::move(Err); |
985 | if (Error Err = WriteAddress(Entry.Values[0])) |
986 | return std::move(Err); |
987 | cantFail(Err: WriteAddress(Entry.Values[1])); |
988 | break; |
989 | case dwarf::DW_RLE_start_length: |
990 | if (Error Err = CheckOperands(2)) |
991 | return std::move(Err); |
992 | if (Error Err = WriteAddress(Entry.Values[0])) |
993 | return std::move(Err); |
994 | encodeULEB128(Value: Entry.Values[1], OS); |
995 | break; |
996 | } |
997 | |
998 | return OS.tell() - BeginOffset; |
999 | } |
1000 | |
1001 | static Expected<uint64_t> writeListEntry(raw_ostream &OS, |
1002 | const DWARFYAML::LoclistEntry &Entry, |
1003 | uint8_t AddrSize, |
1004 | bool IsLittleEndian) { |
1005 | uint64_t BeginOffset = OS.tell(); |
1006 | writeInteger(Integer: (uint8_t)Entry.Operator, OS, IsLittleEndian); |
1007 | |
1008 | StringRef EncodingName = dwarf::LocListEncodingString(Encoding: Entry.Operator); |
1009 | |
1010 | auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error { |
1011 | return checkOperandCount(EncodingString: EncodingName, Values: Entry.Values, ExpectedOperands); |
1012 | }; |
1013 | |
1014 | auto WriteAddress = [&](uint64_t Addr) -> Error { |
1015 | return writeListEntryAddress(EncodingName, OS, Addr, AddrSize, |
1016 | IsLittleEndian); |
1017 | }; |
1018 | |
1019 | auto WriteDWARFOperations = [&]() -> Error { |
1020 | std::string OpBuffer; |
1021 | raw_string_ostream OpBufferOS(OpBuffer); |
1022 | uint64_t DescriptionsLength = 0; |
1023 | |
1024 | for (const DWARFYAML::DWARFOperation &Op : Entry.Descriptions) { |
1025 | if (Expected<uint64_t> OpSize = |
1026 | writeDWARFExpression(OS&: OpBufferOS, Operation: Op, AddrSize, IsLittleEndian)) |
1027 | DescriptionsLength += *OpSize; |
1028 | else |
1029 | return OpSize.takeError(); |
1030 | } |
1031 | |
1032 | if (Entry.DescriptionsLength) |
1033 | DescriptionsLength = *Entry.DescriptionsLength; |
1034 | else |
1035 | DescriptionsLength = OpBuffer.size(); |
1036 | |
1037 | encodeULEB128(Value: DescriptionsLength, OS); |
1038 | OS.write(Ptr: OpBuffer.data(), Size: OpBuffer.size()); |
1039 | |
1040 | return Error::success(); |
1041 | }; |
1042 | |
1043 | switch (Entry.Operator) { |
1044 | case dwarf::DW_LLE_end_of_list: |
1045 | if (Error Err = CheckOperands(0)) |
1046 | return std::move(Err); |
1047 | break; |
1048 | case dwarf::DW_LLE_base_addressx: |
1049 | if (Error Err = CheckOperands(1)) |
1050 | return std::move(Err); |
1051 | encodeULEB128(Value: Entry.Values[0], OS); |
1052 | break; |
1053 | case dwarf::DW_LLE_startx_endx: |
1054 | case dwarf::DW_LLE_startx_length: |
1055 | case dwarf::DW_LLE_offset_pair: |
1056 | if (Error Err = CheckOperands(2)) |
1057 | return std::move(Err); |
1058 | encodeULEB128(Value: Entry.Values[0], OS); |
1059 | encodeULEB128(Value: Entry.Values[1], OS); |
1060 | if (Error Err = WriteDWARFOperations()) |
1061 | return std::move(Err); |
1062 | break; |
1063 | case dwarf::DW_LLE_default_location: |
1064 | if (Error Err = CheckOperands(0)) |
1065 | return std::move(Err); |
1066 | if (Error Err = WriteDWARFOperations()) |
1067 | return std::move(Err); |
1068 | break; |
1069 | case dwarf::DW_LLE_base_address: |
1070 | if (Error Err = CheckOperands(1)) |
1071 | return std::move(Err); |
1072 | if (Error Err = WriteAddress(Entry.Values[0])) |
1073 | return std::move(Err); |
1074 | break; |
1075 | case dwarf::DW_LLE_start_end: |
1076 | if (Error Err = CheckOperands(2)) |
1077 | return std::move(Err); |
1078 | if (Error Err = WriteAddress(Entry.Values[0])) |
1079 | return std::move(Err); |
1080 | cantFail(Err: WriteAddress(Entry.Values[1])); |
1081 | if (Error Err = WriteDWARFOperations()) |
1082 | return std::move(Err); |
1083 | break; |
1084 | case dwarf::DW_LLE_start_length: |
1085 | if (Error Err = CheckOperands(2)) |
1086 | return std::move(Err); |
1087 | if (Error Err = WriteAddress(Entry.Values[0])) |
1088 | return std::move(Err); |
1089 | encodeULEB128(Value: Entry.Values[1], OS); |
1090 | if (Error Err = WriteDWARFOperations()) |
1091 | return std::move(Err); |
1092 | break; |
1093 | } |
1094 | |
1095 | return OS.tell() - BeginOffset; |
1096 | } |
1097 | |
1098 | template <typename EntryType> |
1099 | static Error writeDWARFLists(raw_ostream &OS, |
1100 | ArrayRef<DWARFYAML::ListTable<EntryType>> Tables, |
1101 | bool IsLittleEndian, bool Is64BitAddrSize) { |
1102 | for (const DWARFYAML::ListTable<EntryType> &Table : Tables) { |
1103 | // sizeof(version) + sizeof(address_size) + sizeof(segment_selector_size) + |
1104 | // sizeof(offset_entry_count) = 8 |
1105 | uint64_t Length = 8; |
1106 | |
1107 | uint8_t AddrSize; |
1108 | if (Table.AddrSize) |
1109 | AddrSize = *Table.AddrSize; |
1110 | else |
1111 | AddrSize = Is64BitAddrSize ? 8 : 4; |
1112 | |
1113 | // Since the length of the current range/location lists entry is |
1114 | // undetermined yet, we firstly write the content of the range/location |
1115 | // lists to a buffer to calculate the length and then serialize the buffer |
1116 | // content to the actual output stream. |
1117 | std::string ListBuffer; |
1118 | raw_string_ostream ListBufferOS(ListBuffer); |
1119 | |
1120 | // Offsets holds offsets for each range/location list. The i-th element is |
1121 | // the offset from the beginning of the first range/location list to the |
1122 | // location of the i-th range list. |
1123 | std::vector<uint64_t> Offsets; |
1124 | |
1125 | for (const DWARFYAML::ListEntries<EntryType> &List : Table.Lists) { |
1126 | Offsets.push_back(x: ListBufferOS.tell()); |
1127 | if (List.Content) { |
1128 | List.Content->writeAsBinary(ListBufferOS, UINT64_MAX); |
1129 | Length += List.Content->binary_size(); |
1130 | } else if (List.Entries) { |
1131 | for (const EntryType &Entry : *List.Entries) { |
1132 | Expected<uint64_t> EntrySize = |
1133 | writeListEntry(ListBufferOS, Entry, AddrSize, IsLittleEndian); |
1134 | if (!EntrySize) |
1135 | return EntrySize.takeError(); |
1136 | Length += *EntrySize; |
1137 | } |
1138 | } |
1139 | } |
1140 | |
1141 | // If the offset_entry_count field isn't specified, yaml2obj will infer it |
1142 | // from the 'Offsets' field in the YAML description. If the 'Offsets' field |
1143 | // isn't specified either, yaml2obj will infer it from the auto-generated |
1144 | // offsets. |
1145 | uint32_t OffsetEntryCount; |
1146 | if (Table.OffsetEntryCount) |
1147 | OffsetEntryCount = *Table.OffsetEntryCount; |
1148 | else |
1149 | OffsetEntryCount = Table.Offsets ? Table.Offsets->size() : Offsets.size(); |
1150 | uint64_t OffsetsSize = |
1151 | OffsetEntryCount * (Table.Format == dwarf::DWARF64 ? 8 : 4); |
1152 | Length += OffsetsSize; |
1153 | |
1154 | // If the length is specified in the YAML description, we use it instead of |
1155 | // the actual length. |
1156 | if (Table.Length) |
1157 | Length = *Table.Length; |
1158 | |
1159 | writeInitialLength(Table.Format, Length, OS, IsLittleEndian); |
1160 | writeInteger(Integer: (uint16_t)Table.Version, OS, IsLittleEndian); |
1161 | writeInteger(Integer: (uint8_t)AddrSize, OS, IsLittleEndian); |
1162 | writeInteger(Integer: (uint8_t)Table.SegSelectorSize, OS, IsLittleEndian); |
1163 | writeInteger(Integer: (uint32_t)OffsetEntryCount, OS, IsLittleEndian); |
1164 | |
1165 | auto EmitOffsets = [&](ArrayRef<uint64_t> Offsets, uint64_t OffsetsSize) { |
1166 | for (uint64_t Offset : Offsets) |
1167 | writeDWARFOffset(OffsetsSize + Offset, Table.Format, OS, |
1168 | IsLittleEndian); |
1169 | }; |
1170 | |
1171 | if (Table.Offsets) |
1172 | EmitOffsets(ArrayRef<uint64_t>((const uint64_t *)Table.Offsets->data(), |
1173 | Table.Offsets->size()), |
1174 | 0); |
1175 | else if (OffsetEntryCount != 0) |
1176 | EmitOffsets(Offsets, OffsetsSize); |
1177 | |
1178 | OS.write(Ptr: ListBuffer.data(), Size: ListBuffer.size()); |
1179 | } |
1180 | |
1181 | return Error::success(); |
1182 | } |
1183 | |
1184 | Error DWARFYAML::emitDebugRnglists(raw_ostream &OS, const Data &DI) { |
1185 | assert(DI.DebugRnglists && "unexpected emitDebugRnglists() call" ); |
1186 | return writeDWARFLists<DWARFYAML::RnglistEntry>( |
1187 | OS, Tables: *DI.DebugRnglists, IsLittleEndian: DI.IsLittleEndian, Is64BitAddrSize: DI.Is64BitAddrSize); |
1188 | } |
1189 | |
1190 | Error DWARFYAML::emitDebugLoclists(raw_ostream &OS, const Data &DI) { |
1191 | assert(DI.DebugLoclists && "unexpected emitDebugRnglists() call" ); |
1192 | return writeDWARFLists<DWARFYAML::LoclistEntry>( |
1193 | OS, Tables: *DI.DebugLoclists, IsLittleEndian: DI.IsLittleEndian, Is64BitAddrSize: DI.Is64BitAddrSize); |
1194 | } |
1195 | |
1196 | std::function<Error(raw_ostream &, const DWARFYAML::Data &)> |
1197 | DWARFYAML::getDWARFEmitterByName(StringRef SecName) { |
1198 | auto EmitFunc = |
1199 | StringSwitch< |
1200 | std::function<Error(raw_ostream &, const DWARFYAML::Data &)>>(SecName) |
1201 | .Case(S: "debug_abbrev" , Value: DWARFYAML::emitDebugAbbrev) |
1202 | .Case(S: "debug_addr" , Value: DWARFYAML::emitDebugAddr) |
1203 | .Case(S: "debug_aranges" , Value: DWARFYAML::emitDebugAranges) |
1204 | .Case(S: "debug_gnu_pubnames" , Value: DWARFYAML::emitDebugGNUPubnames) |
1205 | .Case(S: "debug_gnu_pubtypes" , Value: DWARFYAML::emitDebugGNUPubtypes) |
1206 | .Case(S: "debug_info" , Value: DWARFYAML::emitDebugInfo) |
1207 | .Case(S: "debug_line" , Value: DWARFYAML::emitDebugLine) |
1208 | .Case(S: "debug_loclists" , Value: DWARFYAML::emitDebugLoclists) |
1209 | .Case(S: "debug_pubnames" , Value: DWARFYAML::emitDebugPubnames) |
1210 | .Case(S: "debug_pubtypes" , Value: DWARFYAML::emitDebugPubtypes) |
1211 | .Case(S: "debug_ranges" , Value: DWARFYAML::emitDebugRanges) |
1212 | .Case(S: "debug_rnglists" , Value: DWARFYAML::emitDebugRnglists) |
1213 | .Case(S: "debug_str" , Value: DWARFYAML::emitDebugStr) |
1214 | .Case(S: "debug_str_offsets" , Value: DWARFYAML::emitDebugStrOffsets) |
1215 | .Case(S: "debug_names" , Value: DWARFYAML::emitDebugNames) |
1216 | .Default(Value: [&](raw_ostream &, const DWARFYAML::Data &) { |
1217 | return createStringError(EC: errc::not_supported, |
1218 | S: SecName + " is not supported" ); |
1219 | }); |
1220 | |
1221 | return EmitFunc; |
1222 | } |
1223 | |
1224 | static Error |
1225 | emitDebugSectionImpl(const DWARFYAML::Data &DI, StringRef Sec, |
1226 | StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) { |
1227 | std::string Data; |
1228 | raw_string_ostream DebugInfoStream(Data); |
1229 | |
1230 | auto EmitFunc = DWARFYAML::getDWARFEmitterByName(SecName: Sec); |
1231 | |
1232 | if (Error Err = EmitFunc(DebugInfoStream, DI)) |
1233 | return Err; |
1234 | DebugInfoStream.flush(); |
1235 | if (!Data.empty()) |
1236 | OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(InputData: Data); |
1237 | |
1238 | return Error::success(); |
1239 | } |
1240 | |
1241 | Expected<StringMap<std::unique_ptr<MemoryBuffer>>> |
1242 | DWARFYAML::emitDebugSections(StringRef YAMLString, bool IsLittleEndian, |
1243 | bool Is64BitAddrSize) { |
1244 | auto CollectDiagnostic = [](const SMDiagnostic &Diag, void *DiagContext) { |
1245 | *static_cast<SMDiagnostic *>(DiagContext) = Diag; |
1246 | }; |
1247 | |
1248 | SMDiagnostic GeneratedDiag; |
1249 | yaml::Input YIn(YAMLString, /*Ctxt=*/nullptr, CollectDiagnostic, |
1250 | &GeneratedDiag); |
1251 | |
1252 | DWARFYAML::Data DI; |
1253 | DI.IsLittleEndian = IsLittleEndian; |
1254 | DI.Is64BitAddrSize = Is64BitAddrSize; |
1255 | |
1256 | YIn >> DI; |
1257 | if (YIn.error()) |
1258 | return createStringError(EC: YIn.error(), S: GeneratedDiag.getMessage()); |
1259 | |
1260 | StringMap<std::unique_ptr<MemoryBuffer>> DebugSections; |
1261 | Error Err = Error::success(); |
1262 | |
1263 | for (StringRef SecName : DI.getNonEmptySectionNames()) |
1264 | Err = joinErrors(E1: std::move(Err), |
1265 | E2: emitDebugSectionImpl(DI, Sec: SecName, OutputBuffers&: DebugSections)); |
1266 | |
1267 | if (Err) |
1268 | return std::move(Err); |
1269 | return std::move(DebugSections); |
1270 | } |
1271 | |