1//===- Relocations.cpp ----------------------------------------------------===//
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 "Relocations.h"
10#include "ConcatOutputSection.h"
11#include "Symbols.h"
12#include "SyntheticSections.h"
13#include "Target.h"
14
15#include "lld/Common/ErrorHandler.h"
16
17using namespace llvm;
18using namespace lld;
19using namespace lld::macho;
20
21static_assert(sizeof(void *) != 8 || sizeof(Relocation) == 24,
22 "Try to minimize Reloc's size; we create many instances");
23
24InputSection *Relocation::getReferentInputSection() const {
25 if (const auto *sym = referent.dyn_cast<Symbol *>()) {
26 if (const auto *d = dyn_cast<Defined>(Val: sym))
27 return d->isec();
28 return nullptr;
29 } else {
30 return cast<InputSection *>(Val: referent);
31 }
32}
33
34StringRef Relocation::getReferentString() const {
35 if (auto *isec = dyn_cast<InputSection *>(Val: referent)) {
36 const auto *cisec = dyn_cast<CStringInputSection>(Val: isec);
37 assert(cisec && "referent must be a CStringInputSection");
38 return cisec->getStringRefAtOffset(off: addend);
39 }
40
41 auto *sym = dyn_cast<Defined>(Val: cast<Symbol *>(Val: referent));
42 assert(sym && "referent must be a Defined symbol");
43
44 auto *symIsec = sym->isec();
45 auto symOffset = sym->value + addend;
46
47 if (auto *s = dyn_cast_or_null<CStringInputSection>(Val: symIsec))
48 return s->getStringRefAtOffset(off: symOffset);
49
50 if (isa<ConcatInputSection>(Val: symIsec)) {
51 auto strData = symIsec->data.slice(N: symOffset);
52 const char *pszData = reinterpret_cast<const char *>(strData.data());
53 return StringRef(pszData, strnlen(string: pszData, maxlen: strData.size()));
54 }
55
56 llvm_unreachable("unknown reference section in getReferentString");
57}
58
59bool macho::validateSymbolRelocation(const Symbol *sym,
60 const InputSection *isec,
61 const Relocation &r) {
62 const RelocAttrs &relocAttrs = target->getRelocAttrs(type: r.type);
63 bool valid = true;
64 auto message = [&](const Twine &diagnostic) {
65 valid = false;
66 return (isec->getLocation(off: r.offset) + ": " + relocAttrs.name +
67 " relocation " + diagnostic)
68 .str();
69 };
70
71 if (relocAttrs.hasAttr(b: RelocAttrBits::TLV) != sym->isTlv())
72 error(msg: message(Twine("requires that symbol ") + sym->getName() + " " +
73 (sym->isTlv() ? "not " : "") + "be thread-local"));
74
75 return valid;
76}
77
78// Given an offset in the output buffer, figure out which ConcatInputSection (if
79// any) maps to it. At the same time, update the offset such that it is relative
80// to the InputSection rather than to the output buffer.
81//
82// Obtaining the InputSection allows us to have better error diagnostics.
83// However, many of our relocation-handling methods do not take the InputSection
84// as a parameter. Since we are already passing the buffer offsets to our Target
85// methods, this function allows us to emit better errors without threading an
86// additional InputSection argument through the call stack.
87//
88// This is implemented as a slow linear search through OutputSegments,
89// OutputSections, and finally the InputSections themselves. However, this
90// function should be called only on error paths, so some overhead is fine.
91InputSection *macho::offsetToInputSection(uint64_t *off) {
92 for (OutputSegment *seg : outputSegments) {
93 if (*off < seg->fileOff || *off >= seg->fileOff + seg->fileSize)
94 continue;
95
96 const std::vector<OutputSection *> &sections = seg->getSections();
97 size_t osecIdx = 0;
98 for (; osecIdx < sections.size(); ++osecIdx)
99 if (*off < sections[osecIdx]->fileOff)
100 break;
101 assert(osecIdx > 0);
102 // We should be only calling this function on offsets that belong to
103 // ConcatOutputSections.
104 auto *osec = cast<ConcatOutputSection>(Val: sections[osecIdx - 1]);
105 *off -= osec->fileOff;
106
107 size_t isecIdx = 0;
108 for (; isecIdx < osec->inputs.size(); ++isecIdx) {
109 const ConcatInputSection *isec = osec->inputs[isecIdx];
110 if (*off < isec->outSecOff)
111 break;
112 }
113 assert(isecIdx > 0);
114 ConcatInputSection *isec = osec->inputs[isecIdx - 1];
115 *off -= isec->outSecOff;
116 return isec;
117 }
118 return nullptr;
119}
120
121void macho::reportRangeError(void *loc, const Relocation &r, const Twine &v,
122 uint8_t bits, int64_t min, uint64_t max) {
123 std::string hint;
124 uint64_t off = reinterpret_cast<const uint8_t *>(loc) - in.bufferStart;
125 const InputSection *isec = offsetToInputSection(off: &off);
126 std::string locStr = isec ? isec->getLocation(off) : "(invalid location)";
127 if (auto *sym = r.referent.dyn_cast<Symbol *>())
128 hint = "; references " + toString(*sym);
129 error(msg: locStr + ": relocation " + target->getRelocAttrs(type: r.type).name +
130 " is out of range: " + v + " is not in [" + Twine(min) + ", " +
131 Twine(max) + "]" + hint);
132}
133
134void macho::reportRangeError(void *loc, SymbolDiagnostic d, const Twine &v,
135 uint8_t bits, int64_t min, uint64_t max) {
136 // FIXME: should we use `loc` somehow to provide a better error message?
137 std::string hint;
138 if (d.symbol)
139 hint = "; references " + toString(*d.symbol);
140 error(msg: d.reason + " is out of range: " + v + " is not in [" + Twine(min) +
141 ", " + Twine(max) + "]" + hint);
142}
143
144const RelocAttrs macho::invalidRelocAttrs{.name: "INVALID", .bits: RelocAttrBits::_0};
145