aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp
blob: 07ebd55e2c46fe60224f3e9b4da9f1eeeca1684d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//=== DIEAttributeCloner.cpp ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "DIEAttributeCloner.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"

using namespace llvm;
using namespace dwarf_linker;
using namespace dwarf_linker::parallel;

void DIEAttributeCloner::clone() {
  // Extract and clone every attribute.
  DWARFDataExtractor Data = InUnit.getOrigUnit().getDebugInfoExtractor();

  uint64_t Offset = InputDieEntry->getOffset();
  // Point to the next DIE (generally there is always at least a NULL
  // entry after the current one). If this is a lone
  // DW_TAG_compile_unit without any children, point to the next unit.
  uint64_t NextOffset = (InputDIEIdx + 1 < InUnit.getOrigUnit().getNumDIEs())
                            ? InUnit.getDIEAtIndex(InputDIEIdx + 1).getOffset()
                            : InUnit.getOrigUnit().getNextUnitOffset();

  // We could copy the data only if we need to apply a relocation to it. After
  // testing, it seems there is no performance downside to doing the copy
  // unconditionally, and it makes the code simpler.
  SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
  Data =
      DWARFDataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());

  // Modify the copy with relocated addresses.
  InUnit.getContaingFile().Addresses->applyValidRelocs(DIECopy, Offset,
                                                       Data.isLittleEndian());

  // Reset the Offset to 0 as we will be working on the local copy of
  // the data.
  Offset = 0;

  const auto *Abbrev = InputDieEntry->getAbbreviationDeclarationPtr();
  Offset += getULEB128Size(Abbrev->getCode());

  // Set current output offset.
  AttrOutOffset = OutUnit.isCompileUnit() ? OutDIE->getOffset() : 0;
  for (const auto &AttrSpec : Abbrev->attributes()) {
    // Check whether current attribute should be skipped.
    if (shouldSkipAttribute(AttrSpec)) {
      DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset,
                                InUnit.getFormParams());
      continue;
    }

    DWARFFormValue Val = AttrSpec.getFormValue();
    Val.extractValue(Data, &Offset, InUnit.getFormParams(),
                     &InUnit.getOrigUnit());

    // Clone current attribute.
    switch (AttrSpec.Form) {
    case dwarf::DW_FORM_strp:
    case dwarf::DW_FORM_line_strp:
    case dwarf::DW_FORM_string:
    case dwarf::DW_FORM_strx:
    case dwarf::DW_FORM_strx1:
    case dwarf::DW_FORM_strx2:
    case dwarf::DW_FORM_strx3:
    case dwarf::DW_FORM_strx4:
      AttrOutOffset += cloneStringAttr(Val, AttrSpec);
      break;
    case dwarf::DW_FORM_ref_addr:
    case dwarf::DW_FORM_ref1:
    case dwarf::DW_FORM_ref2:
    case dwarf::DW_FORM_ref4:
    case dwarf::DW_FORM_ref8:
    case dwarf::DW_FORM_ref_udata:
      AttrOutOffset += cloneDieRefAttr(Val, AttrSpec);
      break;
    case dwarf::DW_FORM_data1:
    case dwarf::DW_FORM_data2:
    case dwarf::DW_FORM_data4:
    case dwarf::DW_FORM_data8:
    case dwarf::DW_FORM_udata:
    case dwarf::DW_FORM_sdata:
    case dwarf::DW_FORM_sec_offset:
    case dwarf::DW_FORM_flag:
    case dwarf::DW_FORM_flag_present:
    case dwarf::DW_FORM_rnglistx:
    case dwarf::DW_FORM_loclistx:
    case dwarf::DW_FORM_implicit_const:
      AttrOutOffset += cloneScalarAttr(Val, AttrSpec);
      break;
    case dwarf::DW_FORM_block:
    case dwarf::DW_FORM_block1:
    case dwarf::DW_FORM_block2:
    case dwarf::DW_FORM_block4:
    case dwarf::DW_FORM_exprloc:
      AttrOutOffset += cloneBlockAttr(Val, AttrSpec);
      break;
    case dwarf::DW_FORM_addr:
    case dwarf::DW_FORM_addrx:
    case dwarf::DW_FORM_addrx1:
    case dwarf::DW_FORM_addrx2:
    case dwarf::DW_FORM_addrx3:
    case dwarf::DW_FORM_addrx4:
      AttrOutOffset += cloneAddressAttr(Val, AttrSpec);
      break;
    default:
      InUnit.warn("unsupported attribute form " +
                      dwarf::FormEncodingString(AttrSpec.Form) +
                      " in DieAttributeCloner::clone(). Dropping.",
                  InputDieEntry);
    }
  }

  // We convert source strings into the indexed form for DWARFv5.
  // Check if original compile unit already has DW_AT_str_offsets_base
  // attribute.
  if (InputDieEntry->getTag() == dwarf::DW_TAG_compile_unit &&
      InUnit.getVersion() >= 5 && !AttrInfo.HasStringOffsetBaseAttr) {
    DebugInfoOutputSection.notePatchWithOffsetUpdate(
        DebugOffsetPatch{AttrOutOffset,
                         &OutUnit->getOrCreateSectionDescriptor(
                             DebugSectionKind::DebugStrOffsets),
                         true},
        PatchesOffsets);

    AttrOutOffset +=
        Generator
            .addScalarAttribute(dwarf::DW_AT_str_offsets_base,
                                dwarf::DW_FORM_sec_offset,
                                OutUnit->getDebugStrOffsetsHeaderSize())
            .second;
  }
}

bool DIEAttributeCloner::shouldSkipAttribute(
    DWARFAbbreviationDeclaration::AttributeSpec AttrSpec) {
  switch (AttrSpec.Attr) {
  default:
    return false;
  case dwarf::DW_AT_low_pc:
  case dwarf::DW_AT_high_pc:
  case dwarf::DW_AT_ranges:
    if (InUnit.getGlobalData().getOptions().UpdateIndexTablesOnly)
      return false;

    // Skip address attribute if we are in function scope and function does not
    // reference live address.
    return InUnit.getDIEInfo(InputDIEIdx).getIsInFunctionScope() &&
           !FuncAddressAdjustment.has_value();
  case dwarf::DW_AT_rnglists_base:
    // In case !Update the .debug_addr table is not generated/preserved.
    // Thus instead of DW_FORM_rnglistx the DW_FORM_sec_offset is used.
    // Since DW_AT_rnglists_base is used for only DW_FORM_rnglistx the
    // DW_AT_rnglists_base is removed.
    return !InUnit.getGlobalData().getOptions().UpdateIndexTablesOnly;
  case dwarf::DW_AT_loclists_base:
    // In case !Update the .debug_addr table is not generated/preserved.
    // Thus instead of DW_FORM_loclistx the DW_FORM_sec_offset is used.
    // Since DW_AT_loclists_base is used for only DW_FORM_loclistx the
    // DW_AT_loclists_base is removed.
    return !InUnit.getGlobalData().getOptions().UpdateIndexTablesOnly;
  case dwarf::DW_AT_location:
  case dwarf::DW_AT_frame_base:
    if (InUnit.getGlobalData().getOptions().UpdateIndexTablesOnly)
      return false;

    // When location expression contains an address: skip this attribute
    // if it does not reference live address.
    if (HasLocationExpressionAddress)
      return !VarAddressAdjustment.has_value();

    // Skip location attribute if we are in function scope and function does not
    // reference live address.
    return InUnit.getDIEInfo(InputDIEIdx).getIsInFunctionScope() &&
           !FuncAddressAdjustment.has_value();
  }
}

size_t DIEAttributeCloner::cloneStringAttr(
    const DWARFFormValue &Val,
    const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec) {
  std::optional<const char *> String = dwarf::toString(Val);
  if (!String) {
    InUnit.warn("cann't read string attribute.");
    return 0;
  }

  StringEntry *StringInPool =
      InUnit.getGlobalData().getStringPool().insert(*String).first;

  // Update attributes info.
  if (AttrSpec.Attr == dwarf::DW_AT_name)
    AttrInfo.Name = StringInPool;
  else if (AttrSpec.Attr == dwarf::DW_AT_MIPS_linkage_name ||
           AttrSpec.Attr == dwarf::DW_AT_linkage_name)
    AttrInfo.MangledName = StringInPool;

  if (AttrSpec.Form == dwarf::DW_FORM_line_strp) {
    if (OutUnit.isTypeUnit()) {
      DebugInfoOutputSection.notePatch(DebugTypeLineStrPatch{
          AttrOutOffset, OutDIE, InUnit.getDieTypeEntry(InputDIEIdx),
          StringInPool});
    } else {
      DebugInfoOutputSection.notePatchWithOffsetUpdate(
          DebugLineStrPatch{{AttrOutOffset}, StringInPool}, PatchesOffsets);
    }
    return Generator
        .addStringPlaceholderAttribute(AttrSpec.Attr, dwarf::DW_FORM_line_strp)
        .second;
  }

  if (Use_DW_FORM_strp) {
    if (OutUnit.isTypeUnit()) {
      DebugInfoOutputSection.notePatch(
          DebugTypeStrPatch{AttrOutOffset, OutDIE,
                            InUnit.getDieTypeEntry(InputDIEIdx), StringInPool});
    } else {
      DebugInfoOutputSection.notePatchWithOffsetUpdate(
          DebugStrPatch{{AttrOutOffset}, StringInPool}, PatchesOffsets);
    }

    return Generator
        .addStringPlaceholderAttribute(AttrSpec.Attr, dwarf::DW_FORM_strp)
        .second;
  }

  return Generator
      .addIndexedStringAttribute(AttrSpec.Attr, dwarf::DW_FORM_strx,
                                 OutUnit->getDebugStrIndex(StringInPool))
      .second;
}

size_t DIEAttributeCloner::cloneDieRefAttr(
    const DWARFFormValue &Val,
    const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec) {
  if (AttrSpec.Attr == dwarf::DW_AT_sibling)
    return 0;

  std::optional<UnitEntryPairTy> RefDiePair =
      InUnit.resolveDIEReference(Val, ResolveInterCUReferencesMode::Resolve);
  if (!RefDiePair || !RefDiePair->DieEntry) {
    // If the referenced DIE is not found,  drop the attribute.
    InUnit.warn("cann't find referenced DIE.", InputDieEntry);
    return 0;
  }

  TypeEntry *RefTypeName = nullptr;
  const CompileUnit::DIEInfo &RefDIEInfo =
      RefDiePair->CU->getDIEInfo(RefDiePair->DieEntry);
  if (RefDIEInfo.needToPlaceInTypeTable())
    RefTypeName = RefDiePair->CU->getDieTypeEntry(RefDiePair->DieEntry);

  if (OutUnit.isTypeUnit()) {
    assert(RefTypeName && "Type name for referenced DIE is not set");
    assert(InUnit.getDieTypeEntry(InputDIEIdx) &&
           "Type name for DIE is not set");

    DebugInfoOutputSection.notePatch(DebugType2TypeDieRefPatch{
        AttrOutOffset, OutDIE, InUnit.getDieTypeEntry(InputDIEIdx),
        RefTypeName});

    return Generator
        .addScalarAttribute(AttrSpec.Attr, dwarf::DW_FORM_ref4, 0xBADDEF)
        .second;
  }

  if (RefTypeName) {
    DebugInfoOutputSection.notePatchWithOffsetUpdate(
        DebugDieTypeRefPatch{AttrOutOffset, RefTypeName}, PatchesOffsets);

    return Generator
        .addScalarAttribute(AttrSpec.Attr, dwarf::DW_FORM_ref_addr, 0xBADDEF)
        .second;
  }

  // Get output offset for referenced DIE.
  uint64_t OutDieOffset = RefDiePair->CU->getDieOutOffset(RefDiePair->DieEntry);

  // Examine whether referenced DIE is in current compile unit.
  bool IsLocal = OutUnit->getUniqueID() == RefDiePair->CU->getUniqueID();

  // Set attribute form basing on the kind of referenced DIE(local or not?).
  dwarf::Form NewForm = IsLocal ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr;

  // Check whether current attribute references already cloned DIE inside
  // the same compilation unit. If true - write the already known offset value.
  if (IsLocal && (OutDieOffset != 0))
    return Generator.addScalarAttribute(AttrSpec.Attr, NewForm, OutDieOffset)
        .second;

  // If offset value is not known at this point then create patch for the
  // reference value and write dummy value into the attribute.
  DebugInfoOutputSection.notePatchWithOffsetUpdate(
      DebugDieRefPatch{AttrOutOffset, OutUnit.getAsCompileUnit(),
                       RefDiePair->CU,
                       RefDiePair->CU->getDIEIndex(RefDiePair->DieEntry)},
      PatchesOffsets);
  return Generator.addScalarAttribute(AttrSpec.Attr, NewForm, 0xBADDEF).second;
}

size_t DIEAttributeCloner::cloneScalarAttr(
    const DWARFFormValue &Val,
    const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec) {

  // Create patches for attribute referencing other non invariant section.
  // Invariant section could not be updated here as this section and
  // reference to it do not change value in case --update.
  switch (AttrSpec.Attr) {
  case dwarf::DW_AT_macro_info: {
    if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) {
      const DWARFDebugMacro *Macro =
          InUnit.getContaingFile().Dwarf->getDebugMacinfo();
      if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset))
        return 0;

      DebugInfoOutputSection.notePatchWithOffsetUpdate(
          DebugOffsetPatch{AttrOutOffset,
                           &OutUnit->getOrCreateSectionDescriptor(
                               DebugSectionKind::DebugMacinfo)},
          PatchesOffsets);
    }
  } break;
  case dwarf::DW_AT_macros: {
    if (std::optional<uint64_t> Offset = Val.getAsSectionOffset()) {
      const DWARFDebugMacro *Macro =
          InUnit.getContaingFile().Dwarf->getDebugMacro();
      if (Macro == nullptr || !Macro->hasEntryForOffset(*Offset))
        return 0;

      DebugInfoOutputSection.notePatchWithOffsetUpdate(
          DebugOffsetPatch{AttrOutOffset,
                           &OutUnit->getOrCreateSectionDescriptor(
                               DebugSectionKind::DebugMacro)},
          PatchesOffsets);
    }
  } break;
  case dwarf::DW_AT_stmt_list: {
    DebugInfoOutputSection.notePatchWithOffsetUpdate(
        DebugOffsetPatch{AttrOutOffset, &OutUnit->getOrCreateSectionDescriptor(
                                            DebugSectionKind::DebugLine)},
        PatchesOffsets);
  } break;
  case dwarf::DW_AT_str_offsets_base: {
    DebugInfoOutputSection.notePatchWithOffsetUpdate(
        DebugOffsetPatch{AttrOutOffset,
                         &OutUnit->getOrCreateSectionDescriptor(
                             DebugSectionKind::DebugStrOffsets),
                         true},
        PatchesOffsets);

    // Use size of .debug_str_offsets header as attribute value. The offset
    // to .debug_str_offsets would be added later while patching.
    AttrInfo.HasStringOffsetBaseAttr = true;
    return Generator
        .addScalarAttribute(AttrSpec.Attr, AttrSpec.Form,
                            OutUnit->getDebugStrOffsetsHeaderSize())
        .second;
  } break;
  case dwarf::DW_AT_decl_file: {
    // Value of DW_AT_decl_file may exceed original form. Longer
    // form can affect offsets to the following attributes. To not
    // update offsets of the following attributes we always remove
    // original DW_AT_decl_file and attach it to the last position
    // later.
    if (OutUnit.isTypeUnit()) {
      if (std::optional<std::pair<StringRef, StringRef>> DirAndFilename =
              InUnit.getDirAndFilenameFromLineTable(Val))
        DebugInfoOutputSection.notePatch(DebugTypeDeclFilePatch{
            OutDIE,
            InUnit.getDieTypeEntry(InputDIEIdx),
            OutUnit->getGlobalData()
                .getStringPool()
                .insert(DirAndFilename->first)
                .first,
            OutUnit->getGlobalData()
                .getStringPool()
                .insert(DirAndFilename->second)
                .first,
        });
      return 0;
    }
  } break;
  default: {
  } break;
  };

  uint64_t Value;
  if (AttrSpec.Attr == dwarf::DW_AT_const_value &&
      (InputDieEntry->getTag() == dwarf::DW_TAG_variable ||
       InputDieEntry->getTag() == dwarf::DW_TAG_constant))
    AttrInfo.HasLiveAddress = true;

  if (InUnit.getGlobalData().getOptions().UpdateIndexTablesOnly) {
    if (auto OptionalValue = Val.getAsUnsignedConstant())
      Value = *OptionalValue;
    else if (auto OptionalValue = Val.getAsSignedConstant())
      Value = *OptionalValue;
    else if (auto OptionalValue = Val.getAsSectionOffset())
      Value = *OptionalValue;
    else {
      InUnit.warn("unsupported scalar attribute form. Dropping attribute.",
                  InputDieEntry);
      return 0;
    }

    if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
      AttrInfo.IsDeclaration = true;

    if (AttrSpec.Form == dwarf::DW_FORM_loclistx)
      return Generator.addLocListAttribute(AttrSpec.Attr, AttrSpec.Form, Value)
          .second;

    return Generator.addScalarAttribute(AttrSpec.Attr, AttrSpec.Form, Value)
        .second;
  }

  dwarf::Form ResultingForm = AttrSpec.Form;
  if (AttrSpec.Form == dwarf::DW_FORM_rnglistx) {
    // DWARFLinker does not generate .debug_addr table. Thus we need to change
    // all "addrx" related forms to "addr" version. Change DW_FORM_rnglistx
    // to DW_FORM_sec_offset here.
    std::optional<uint64_t> Index = Val.getAsSectionOffset();
    if (!Index) {
      InUnit.warn("cann't read the attribute. Dropping.", InputDieEntry);
      return 0;
    }
    std::optional<uint64_t> Offset =
        InUnit.getOrigUnit().getRnglistOffset(*Index);
    if (!Offset) {
      InUnit.warn("cann't read the attribute. Dropping.", InputDieEntry);
      return 0;
    }

    Value = *Offset;
    ResultingForm = dwarf::DW_FORM_sec_offset;
  } else if (AttrSpec.Form == dwarf::DW_FORM_loclistx) {
    // DWARFLinker does not generate .debug_addr table. Thus we need to change
    // all "addrx" related forms to "addr" version. Change DW_FORM_loclistx
    // to DW_FORM_sec_offset here.
    std::optional<uint64_t> Index = Val.getAsSectionOffset();
    if (!Index) {
      InUnit.warn("cann't read the attribute. Dropping.", InputDieEntry);
      return 0;
    }
    std::optional<uint64_t> Offset =
        InUnit.getOrigUnit().getLoclistOffset(*Index);
    if (!Offset) {
      InUnit.warn("cann't read the attribute. Dropping.", InputDieEntry);
      return 0;
    }

    Value = *Offset;
    ResultingForm = dwarf::DW_FORM_sec_offset;
  } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
             InputDieEntry->getTag() == dwarf::DW_TAG_compile_unit) {
    if (!OutUnit.isCompileUnit())
      return 0;

    std::optional<uint64_t> LowPC = OutUnit.getAsCompileUnit()->getLowPc();
    if (!LowPC)
      return 0;
    // Dwarf >= 4 high_pc is an size, not an address.
    Value = OutUnit.getAsCompileUnit()->getHighPc() - *LowPC;
  } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
    Value = *Val.getAsSectionOffset();
  else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
    Value = *Val.getAsSignedConstant();
  else if (auto OptionalValue = Val.getAsUnsignedConstant())
    Value = *OptionalValue;
  else {
    InUnit.warn("unsupported scalar attribute form. Dropping attribute.",
                InputDieEntry);
    return 0;
  }

  if (AttrSpec.Attr == dwarf::DW_AT_ranges ||
      AttrSpec.Attr == dwarf::DW_AT_start_scope) {
    // Create patch for the range offset value.
    DebugInfoOutputSection.notePatchWithOffsetUpdate(
        DebugRangePatch{{AttrOutOffset},
                        InputDieEntry->getTag() == dwarf::DW_TAG_compile_unit},
        PatchesOffsets);
    AttrInfo.HasRanges = true;
  } else if (DWARFAttribute::mayHaveLocationList(AttrSpec.Attr) &&
             dwarf::doesFormBelongToClass(AttrSpec.Form,
                                          DWARFFormValue::FC_SectionOffset,
                                          InUnit.getOrigUnit().getVersion())) {
    int64_t AddrAdjustmentValue = 0;
    if (VarAddressAdjustment)
      AddrAdjustmentValue = *VarAddressAdjustment;
    else if (FuncAddressAdjustment)
      AddrAdjustmentValue = *FuncAddressAdjustment;

    // Create patch for the location offset value.
    DebugInfoOutputSection.notePatchWithOffsetUpdate(
        DebugLocPatch{{AttrOutOffset}, AddrAdjustmentValue}, PatchesOffsets);
  } else if (AttrSpec.Attr == dwarf::DW_AT_addr_base) {
    DebugInfoOutputSection.notePatchWithOffsetUpdate(
        DebugOffsetPatch{
            AttrOutOffset,
            &OutUnit->getOrCreateSectionDescriptor(DebugSectionKind::DebugAddr),
            true},
        PatchesOffsets);

    // Use size of .debug_addr header as attribute value. The offset to
    // .debug_addr would be added later while patching.
    return Generator
        .addScalarAttribute(AttrSpec.Attr, AttrSpec.Form,
                            OutUnit->getDebugAddrHeaderSize())
        .second;
  } else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
    AttrInfo.IsDeclaration = true;

  return Generator.addScalarAttribute(AttrSpec.Attr, ResultingForm, Value)
      .second;
}

size_t DIEAttributeCloner::cloneBlockAttr(
    const DWARFFormValue &Val,
    const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec) {

  if (OutUnit.isTypeUnit())
    return 0;

  size_t NumberOfPatchesAtStart = PatchesOffsets.size();

  // If the block is a DWARF Expression, clone it into the temporary
  // buffer using cloneExpression(), otherwise copy the data directly.
  SmallVector<uint8_t, 32> Buffer;
  ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
  if (DWARFAttribute::mayHaveLocationExpr(AttrSpec.Attr) &&
      (Val.isFormClass(DWARFFormValue::FC_Block) ||
       Val.isFormClass(DWARFFormValue::FC_Exprloc))) {
    DataExtractor Data(StringRef((const char *)Bytes.data(), Bytes.size()),
                       InUnit.getOrigUnit().isLittleEndian(),
                       InUnit.getOrigUnit().getAddressByteSize());
    DWARFExpression Expr(Data, InUnit.getOrigUnit().getAddressByteSize(),
                         InUnit.getFormParams().Format);

    InUnit.cloneDieAttrExpression(Expr, Buffer, DebugInfoOutputSection,
                                  VarAddressAdjustment, PatchesOffsets);
    Bytes = Buffer;
  }

  // The expression location data might be updated and exceed the original size.
  // Check whether the new data fits into the original form.
  dwarf::Form ResultForm = AttrSpec.Form;
  if ((ResultForm == dwarf::DW_FORM_block1 && Bytes.size() > UINT8_MAX) ||
      (ResultForm == dwarf::DW_FORM_block2 && Bytes.size() > UINT16_MAX) ||
      (ResultForm == dwarf::DW_FORM_block4 && Bytes.size() > UINT32_MAX))
    ResultForm = dwarf::DW_FORM_block;

  size_t FinalAttributeSize;
  if (AttrSpec.Form == dwarf::DW_FORM_exprloc)
    FinalAttributeSize =
        Generator.addLocationAttribute(AttrSpec.Attr, ResultForm, Bytes).second;
  else
    FinalAttributeSize =
        Generator.addBlockAttribute(AttrSpec.Attr, ResultForm, Bytes).second;

  // Update patches offsets with the size of length field for Bytes.
  for (size_t Idx = NumberOfPatchesAtStart; Idx < PatchesOffsets.size();
       Idx++) {
    assert(FinalAttributeSize > Bytes.size());
    *PatchesOffsets[Idx] +=
        (AttrOutOffset + (FinalAttributeSize - Bytes.size()));
  }

  if (HasLocationExpressionAddress)
    AttrInfo.HasLiveAddress =
        VarAddressAdjustment.has_value() ||
        InUnit.getGlobalData().getOptions().UpdateIndexTablesOnly;

  return FinalAttributeSize;
}

size_t DIEAttributeCloner::cloneAddressAttr(
    const DWARFFormValue &Val,
    const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec) {
  if (AttrSpec.Attr == dwarf::DW_AT_low_pc)
    AttrInfo.HasLiveAddress = true;

  if (InUnit.getGlobalData().getOptions().UpdateIndexTablesOnly)
    return Generator
        .addScalarAttribute(AttrSpec.Attr, AttrSpec.Form, Val.getRawUValue())
        .second;

  if (OutUnit.isTypeUnit())
    return 0;

  // Cloned Die may have address attributes relocated to a
  // totally unrelated value. This can happen:
  //   - If high_pc is an address (Dwarf version == 2), then it might have been
  //     relocated to a totally unrelated value (because the end address in the
  //     object file might be start address of another function which got moved
  //     independently by the linker).
  //   - If address relocated in an inline_subprogram that happens at the
  //     beginning of its inlining function.
  //  To avoid above cases and to not apply relocation twice (in
  //  applyValidRelocs and here), read address attribute from InputDIE and apply
  //  Info.PCOffset here.

  std::optional<DWARFFormValue> AddrAttribute =
      InUnit.find(InputDieEntry, AttrSpec.Attr);
  if (!AddrAttribute)
    llvm_unreachable("Cann't find attribute");

  std::optional<uint64_t> Addr = AddrAttribute->getAsAddress();
  if (!Addr) {
    InUnit.warn("cann't read address attribute value.");
    return 0;
  }

  if (InputDieEntry->getTag() == dwarf::DW_TAG_compile_unit &&
      AttrSpec.Attr == dwarf::DW_AT_low_pc) {
    if (std::optional<uint64_t> LowPC = OutUnit.getAsCompileUnit()->getLowPc())
      Addr = *LowPC;
    else
      return 0;
  } else if (InputDieEntry->getTag() == dwarf::DW_TAG_compile_unit &&
             AttrSpec.Attr == dwarf::DW_AT_high_pc) {
    if (uint64_t HighPc = OutUnit.getAsCompileUnit()->getHighPc())
      Addr = HighPc;
    else
      return 0;
  } else {
    if (VarAddressAdjustment)
      *Addr += *VarAddressAdjustment;
    else if (FuncAddressAdjustment)
      *Addr += *FuncAddressAdjustment;
  }

  if (AttrSpec.Form == dwarf::DW_FORM_addr) {
    return Generator.addScalarAttribute(AttrSpec.Attr, AttrSpec.Form, *Addr)
        .second;
  }

  return Generator
      .addScalarAttribute(AttrSpec.Attr, dwarf::Form::DW_FORM_addrx,
                          OutUnit.getAsCompileUnit()->getDebugAddrIndex(*Addr))
      .second;
}

unsigned DIEAttributeCloner::finalizeAbbreviations(bool HasChildrenToClone) {
  // Add the size of the abbreviation number to the output offset.
  AttrOutOffset +=
      Generator.finalizeAbbreviations(HasChildrenToClone, &PatchesOffsets);

  return AttrOutOffset;
}