60 "invalid integer write size: %zu", Size);
66 std::vector<uint8_t> FillData(Size, 0);
67 OS.
write(
reinterpret_cast<char *
>(FillData.data()), Size);
72 bool IsLittleEndian) {
89 OS.
write(Str.data(), Str.size());
98 "Index should be less than the size of DebugAbbrev array");
99 auto It = AbbrevTableContents.find(
Index);
100 if (It != AbbrevTableContents.cend())
103 std::string AbbrevTableBuffer;
108 AbbrevCode = AbbrevDecl.Code ? (
uint64_t)*AbbrevDecl.Code : AbbrevCode + 1;
111 OS.
write(AbbrevDecl.Children);
112 for (
const auto &Attr : AbbrevDecl.Attributes) {
115 if (Attr.Form == dwarf::DW_FORM_implicit_const)
126 AbbrevTableContents.insert({
Index, AbbrevTableBuffer});
128 return AbbrevTableContents[
Index];
134 OS.
write(AbbrevTableContent.
data(), AbbrevTableContent.
size());
145 AddrSize = *Range.AddrSize;
158 const uint64_t PaddedHeaderLength =
alignTo(HeaderLength, AddrSize * 2);
161 Length = *Range.Length;
163 Length += PaddedHeaderLength - HeaderLength;
164 Length += AddrSize * 2 * (Range.Descriptors.size() + 1);
174 for (
const auto &Descriptor : Range.Descriptors) {
178 "unable to write debug_aranges address: %s",
190 const size_t RangesOffset = OS.
tell();
193 const size_t CurrOffset = OS.
tell() - RangesOffset;
194 if (DebugRanges.Offset && (
uint64_t)*DebugRanges.Offset < CurrOffset)
196 "'Offset' for 'debug_ranges' with index " +
198 " must be greater than or equal to the "
199 "number of bytes written already (0x" +
201 if (DebugRanges.Offset)
205 if (DebugRanges.AddrSize)
206 AddrSize = *DebugRanges.AddrSize;
209 for (
const auto &
Entry : DebugRanges.Entries) {
214 "unable to write debug_ranges address offset: %s",
227 bool IsLittleEndian,
bool IsGNUPubSec =
false) {
232 for (
const auto &Entry : Sect.
Entries) {
235 writeInteger((uint8_t)Entry.Descriptor, OS, IsLittleEndian);
236 OS.
write(Entry.Name.data(), Entry.Name.size());
272 if (AbbrCode == 0 || Entry.Values.empty())
273 return OS.
tell() - EntryBegin;
277 if (!AbbrevTableInfoOrErr)
280 " for compilation unit with index " +
284 DI.
DebugAbbrev[AbbrevTableInfoOrErr->Index].Table);
286 if (AbbrCode > AbbrevDecls.
size())
289 "abbrev code must be less than or equal to the number of "
290 "entries in abbreviation table");
292 auto FormVal = Entry.Values.begin();
294 for (; FormVal != Entry.Values.end() && AbbrForm != Abbrev.
Attributes.end();
295 ++FormVal, ++AbbrForm) {
301 case dwarf::DW_FORM_addr:
304 FormVal->Value, Params.
AddrSize, OS, IsLittleEndian))
307 case dwarf::DW_FORM_ref_addr:
314 case dwarf::DW_FORM_exprloc:
315 case dwarf::DW_FORM_block:
317 OS.
write((
const char *)FormVal->BlockData.data(),
318 FormVal->BlockData.size());
320 case dwarf::DW_FORM_block1: {
321 writeInteger((uint8_t)FormVal->BlockData.size(), OS, IsLittleEndian);
322 OS.
write((
const char *)FormVal->BlockData.data(),
323 FormVal->BlockData.size());
326 case dwarf::DW_FORM_block2: {
328 OS.
write((
const char *)FormVal->BlockData.data(),
329 FormVal->BlockData.size());
332 case dwarf::DW_FORM_block4: {
334 OS.
write((
const char *)FormVal->BlockData.data(),
335 FormVal->BlockData.size());
338 case dwarf::DW_FORM_strx:
339 case dwarf::DW_FORM_addrx:
340 case dwarf::DW_FORM_rnglistx:
341 case dwarf::DW_FORM_loclistx:
342 case dwarf::DW_FORM_udata:
343 case dwarf::DW_FORM_ref_udata:
344 case dwarf::DW_FORM_GNU_addr_index:
345 case dwarf::DW_FORM_GNU_str_index:
348 case dwarf::DW_FORM_data1:
349 case dwarf::DW_FORM_ref1:
350 case dwarf::DW_FORM_flag:
351 case dwarf::DW_FORM_strx1:
352 case dwarf::DW_FORM_addrx1:
353 writeInteger((uint8_t)FormVal->Value, OS, IsLittleEndian);
355 case dwarf::DW_FORM_data2:
356 case dwarf::DW_FORM_ref2:
357 case dwarf::DW_FORM_strx2:
358 case dwarf::DW_FORM_addrx2:
361 case dwarf::DW_FORM_data4:
362 case dwarf::DW_FORM_ref4:
363 case dwarf::DW_FORM_ref_sup4:
364 case dwarf::DW_FORM_strx4:
365 case dwarf::DW_FORM_addrx4:
368 case dwarf::DW_FORM_data8:
369 case dwarf::DW_FORM_ref8:
370 case dwarf::DW_FORM_ref_sup8:
371 case dwarf::DW_FORM_ref_sig8:
374 case dwarf::DW_FORM_sdata:
377 case dwarf::DW_FORM_string:
378 OS.
write(FormVal->CStr.data(), FormVal->CStr.size());
381 case dwarf::DW_FORM_indirect:
387 case dwarf::DW_FORM_strp:
388 case dwarf::DW_FORM_sec_offset:
389 case dwarf::DW_FORM_GNU_ref_alt:
390 case dwarf::DW_FORM_GNU_strp_alt:
391 case dwarf::DW_FORM_line_strp:
392 case dwarf::DW_FORM_strp_sup:
403 return OS.
tell() - EntryBegin;
423 std::string EntryBuffer;
431 Length += *EntryLength;
433 return EntryLength.takeError();
450 AbbrevTableOffset = AbbrevTableInfoOrErr->Offset;
468 OS.
write(EntryBuffer.data(), EntryBuffer.size());
475 OS.
write(File.Name.data(), File.Name.size());
483 uint8_t AddrSize,
bool IsLittleEndian,
490 std::string OpBuffer;
493 switch (
Op.SubOpcode) {
494 case dwarf::DW_LNE_set_address:
498 case dwarf::DW_LNE_define_file:
501 case dwarf::DW_LNE_set_discriminator:
504 case dwarf::DW_LNE_end_sequence:
507 for (
auto OpByte :
Op.UnknownOpcodeData)
508 writeInteger((uint8_t)OpByte, OpBufferOS, IsLittleEndian);
510 uint64_t ExtLen =
Op.ExtLen.value_or(OpBuffer.size());
512 OS.
write(OpBuffer.data(), OpBuffer.size());
516 uint8_t OpcodeBase, uint8_t AddrSize,
519 if (
Op.Opcode == 0) {
521 }
else if (
Op.Opcode < OpcodeBase) {
523 case dwarf::DW_LNS_copy:
524 case dwarf::DW_LNS_negate_stmt:
525 case dwarf::DW_LNS_set_basic_block:
526 case dwarf::DW_LNS_const_add_pc:
527 case dwarf::DW_LNS_set_prologue_end:
528 case dwarf::DW_LNS_set_epilogue_begin:
531 case dwarf::DW_LNS_advance_pc:
532 case dwarf::DW_LNS_set_file:
533 case dwarf::DW_LNS_set_column:
534 case dwarf::DW_LNS_set_isa:
538 case dwarf::DW_LNS_advance_line:
542 case dwarf::DW_LNS_fixed_advance_pc:
547 for (
auto OpData :
Op.StandardOpcodeData) {
554 static std::vector<uint8_t>
558 std::vector<uint8_t> StandardOpcodeLengths{0, 1, 1, 1, 1, 0,
562 StandardOpcodeLengths.resize(9);
563 }
else if (OpcodeBase) {
564 StandardOpcodeLengths.resize(*OpcodeBase > 0 ? *OpcodeBase - 1 : 0, 0);
566 return StandardOpcodeLengths;
584 std::vector<uint8_t> StandardOpcodeLengths =
589 : StandardOpcodeLengths.size() + 1;
591 for (uint8_t OpcodeLength : StandardOpcodeLengths)
596 BufferOS.
write(
'\0');
598 BufferOS.
write(
'\0');
602 BufferOS.
write(
'\0');
618 Length += Buffer.size();
624 OS.
write(Buffer.data(), Buffer.size());
657 "unable to write debug_addr segment: %s",
663 "unable to write debug_addr address: %s",
696 if (Values.
size() != ExpectedOperands)
699 "invalid number (%zu) of operands for the operator: %s, %" PRIu64
701 Values.
size(), EncodingString.
str().c_str(), ExpectedOperands);
708 bool IsLittleEndian) {
711 "unable to write address for the operator %s: %s",
712 EncodingName.
str().c_str(),
721 uint8_t AddrSize,
bool IsLittleEndian) {
722 auto CheckOperands = [&](
uint64_t ExpectedOperands) ->
Error {
730 case dwarf::DW_OP_consts:
731 if (
Error Err = CheckOperands(1))
735 case dwarf::DW_OP_stack_value:
736 if (
Error Err = CheckOperands(0))
742 "DWARF expression: " +
746 " is not supported");
748 return OS.
tell() - ExpressionBegin;
754 bool IsLittleEndian) {
756 writeInteger((uint8_t)Entry.Operator, OS, IsLittleEndian);
760 auto CheckOperands = [&](
uint64_t ExpectedOperands) ->
Error {
769 switch (Entry.Operator) {
770 case dwarf::DW_RLE_end_of_list:
771 if (
Error Err = CheckOperands(0))
774 case dwarf::DW_RLE_base_addressx:
775 if (
Error Err = CheckOperands(1))
779 case dwarf::DW_RLE_startx_endx:
780 case dwarf::DW_RLE_startx_length:
781 case dwarf::DW_RLE_offset_pair:
782 if (
Error Err = CheckOperands(2))
787 case dwarf::DW_RLE_base_address:
788 if (
Error Err = CheckOperands(1))
790 if (
Error Err = WriteAddress(Entry.Values[0]))
793 case dwarf::DW_RLE_start_end:
794 if (
Error Err = CheckOperands(2))
796 if (
Error Err = WriteAddress(Entry.Values[0]))
798 cantFail(WriteAddress(Entry.Values[1]));
800 case dwarf::DW_RLE_start_length:
801 if (
Error Err = CheckOperands(2))
803 if (
Error Err = WriteAddress(Entry.Values[0]))
809 return OS.
tell() - BeginOffset;
815 bool IsLittleEndian) {
817 writeInteger((uint8_t)Entry.Operator, OS, IsLittleEndian);
821 auto CheckOperands = [&](
uint64_t ExpectedOperands) ->
Error {
830 auto WriteDWARFOperations = [&]() ->
Error {
831 std::string OpBuffer;
838 DescriptionsLength += *OpSize;
840 return OpSize.takeError();
843 if (Entry.DescriptionsLength)
844 DescriptionsLength = *Entry.DescriptionsLength;
846 DescriptionsLength = OpBuffer.size();
849 OS.
write(OpBuffer.data(), OpBuffer.size());
854 switch (Entry.Operator) {
855 case dwarf::DW_LLE_end_of_list:
856 if (
Error Err = CheckOperands(0))
859 case dwarf::DW_LLE_base_addressx:
860 if (
Error Err = CheckOperands(1))
864 case dwarf::DW_LLE_startx_endx:
865 case dwarf::DW_LLE_startx_length:
866 case dwarf::DW_LLE_offset_pair:
867 if (
Error Err = CheckOperands(2))
871 if (
Error Err = WriteDWARFOperations())
874 case dwarf::DW_LLE_default_location:
875 if (
Error Err = CheckOperands(0))
877 if (
Error Err = WriteDWARFOperations())
880 case dwarf::DW_LLE_base_address:
881 if (
Error Err = CheckOperands(1))
883 if (
Error Err = WriteAddress(Entry.Values[0]))
886 case dwarf::DW_LLE_start_end:
887 if (
Error Err = CheckOperands(2))
889 if (
Error Err = WriteAddress(Entry.Values[0]))
891 cantFail(WriteAddress(Entry.Values[1]));
892 if (
Error Err = WriteDWARFOperations())
895 case dwarf::DW_LLE_start_length:
896 if (
Error Err = CheckOperands(2))
898 if (
Error Err = WriteAddress(Entry.Values[0]))
901 if (
Error Err = WriteDWARFOperations())
906 return OS.
tell() - BeginOffset;
909 template <
typename EntryType>
912 bool IsLittleEndian,
bool Is64BitAddrSize) {
920 AddrSize = *Table.AddrSize;
922 AddrSize = Is64BitAddrSize ? 8 : 4;
928 std::string ListBuffer;
940 Length +=
List.Content->binary_size();
941 }
else if (
List.Entries) {
942 for (
const EntryType &Entry : *
List.Entries) {
947 Length += *EntrySize;
957 if (Table.OffsetEntryCount)
958 OffsetEntryCount = *Table.OffsetEntryCount;
960 OffsetEntryCount = Table.Offsets ? Table.Offsets->size() :
Offsets.size();
963 Length += OffsetsSize;
968 Length = *Table.Length;
973 writeInteger((uint8_t)Table.SegSelectorSize, OS, IsLittleEndian);
984 Table.Offsets->size()),
986 else if (OffsetEntryCount != 0)
987 EmitOffsets(
Offsets, OffsetsSize);
989 OS.
write(ListBuffer.data(), ListBuffer.size());
997 return writeDWARFLists<DWARFYAML::RnglistEntry>(
1003 return writeDWARFLists<DWARFYAML::LoclistEntry>(
1028 SecName +
" is not supported");
1036 StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
1042 if (
Error Err = EmitFunc(DebugInfoStream, DI))
1044 DebugInfoStream.
flush();
1053 bool Is64BitAddrSize) {
1054 auto CollectDiagnostic = [](
const SMDiagnostic &Diag,
void *DiagContext) {
1059 yaml::Input YIn(YAMLString,
nullptr, CollectDiagnostic,
1073 for (
StringRef SecName : DI.getNonEmptySectionNames())