1//===----------------------------------------------------------------------===//
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#include "llvm/DWARFLinker/RelocationFixups.h"
10#include "llvm/ADT/STLExtras.h"
11
12using namespace llvm;
13using namespace dwarf_linker;
14
15void RelocationFixups::noteAttribute(dwarf::Tag Tag, dwarf::Attribute Attr,
16 uint64_t InputStart, uint64_t InputEnd,
17 uint64_t OutputOffset,
18 const AttributeSource &Source) {
19 if (!Source.Expression) {
20 DIEFixups.push_back(Elt: {.InputSection: RelocSectionKind::DebugInfo, .InputStart: InputStart, .InputEnd: InputEnd,
21 .LinkedOffset: int64_t(OutputOffset) - int64_t(InputStart)});
22 } else {
23 uint64_t ExpressionStart = InputEnd - Source.Expression->InputSize;
24 for (const ClonedExpression::Operation &Op :
25 Source.Expression->Operations) {
26 uint64_t Start = Op.InputStart;
27 if (Op.InputSection == RelocSectionKind::DebugInfo)
28 Start += ExpressionStart;
29 DIEFixups.push_back(
30 Elt: {.InputSection: Op.InputSection, .InputStart: Start, .InputEnd: Start + Op.Size,
31 .LinkedOffset: int64_t(OutputOffset + Op.OutputOffset) - int64_t(Start)});
32 }
33 }
34
35 // A compile unit's low_pc is computed rather than taken from its entry. A
36 // high_pc can end where the next symbol starts, in the entry of that symbol's
37 // low_pc, which the relocation of the high_pc would misdescribe.
38 bool IsLowPC = Attr == dwarf::DW_AT_low_pc;
39 if (Source.Entry && Attr != dwarf::DW_AT_high_pc &&
40 !(IsLowPC && Tag == dwarf::DW_TAG_compile_unit))
41 DebugAddrFixups.push_back(Elt: {.Entry: *Source.Entry, .IsLowPC: IsLowPC});
42}
43
44void RelocationFixups::finishDIE(uint64_t AbbrevNumberSize) {
45 for (DebugInfoFixup &Fixup : DIEFixups) {
46 Fixup.LinkedOffset += AbbrevNumberSize;
47 DebugInfoFixups.push_back(Elt: Fixup);
48 }
49 DIEFixups.clear();
50}
51
52void RelocationFixups::save(AddressesMap &Addresses, uint64_t UnitOffset,
53 uint8_t AddrSize) const {
54 for (const DebugInfoFixup &Fixup : DebugInfoFixups)
55 Addresses.updateAndSaveValidRelocs(
56 InSection: Fixup.InputSection, OutSection: RelocSectionKind::DebugInfo, OriginalUnitOffset: UnitOffset,
57 LinkedOffset: Fixup.LinkedOffset, StartOffset: Fixup.InputStart, EndOffset: Fixup.InputEnd);
58
59 if (!DebugAddrTableOffset)
60 return;
61
62 // Entries holding the same address are merged, so symbols can share one: a
63 // DW_AT_call_return_pc ending a function holds the low_pc of the next. Only
64 // one relocation can describe an entry. The low_pc's names the right symbol.
65 SmallVector<DebugAddrFixup> Sorted(DebugAddrFixups);
66 llvm::stable_sort(
67 Range&: Sorted, C: [](const DebugAddrFixup &LHS, const DebugAddrFixup &RHS) {
68 return std::make_pair(x: LHS.Entry.OutputIndex, y: !LHS.IsLowPC) <
69 std::make_pair(x: RHS.Entry.OutputIndex, y: !RHS.IsLowPC);
70 });
71
72 std::optional<uint64_t> SavedIndex;
73 for (const DebugAddrFixup &Fixup : Sorted) {
74 if (SavedIndex == Fixup.Entry.OutputIndex)
75 continue;
76 uint64_t InputOffset = Fixup.Entry.InputOffset;
77 uint64_t OutputOffset =
78 *DebugAddrTableOffset + Fixup.Entry.OutputIndex * AddrSize;
79 if (Addresses.updateAndSaveValidRelocs(
80 InSection: RelocSectionKind::DebugAddr, OutSection: RelocSectionKind::DebugAddr,
81 OriginalUnitOffset: UnitOffset, LinkedOffset: int64_t(OutputOffset) - int64_t(InputOffset),
82 StartOffset: InputOffset, EndOffset: InputOffset + AddrSize))
83 SavedIndex = Fixup.Entry.OutputIndex;
84 }
85}
86
87void RelocationFixups::clear() {
88 DIEFixups.clear();
89 DebugInfoFixups.clear();
90 DebugAddrFixups.clear();
91 DebugAddrTableOffset.reset();
92}
93