llvm-project
118 строк · 4.5 Кб
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(Reloc) == 24,
22"Try to minimize Reloc's size; we create many instances");
23
24InputSection *Reloc::getReferentInputSection() const {
25if (const auto *sym = referent.dyn_cast<Symbol *>()) {
26if (const auto *d = dyn_cast<Defined>(sym))
27return d->isec();
28return nullptr;
29} else {
30return referent.get<InputSection *>();
31}
32}
33
34bool macho::validateSymbolRelocation(const Symbol *sym,
35const InputSection *isec, const Reloc &r) {
36const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
37bool valid = true;
38auto message = [&](const Twine &diagnostic) {
39valid = false;
40return (isec->getLocation(r.offset) + ": " + relocAttrs.name +
41" relocation " + diagnostic)
42.str();
43};
44
45if (relocAttrs.hasAttr(RelocAttrBits::TLV) != sym->isTlv())
46error(message(Twine("requires that symbol ") + sym->getName() + " " +
47(sym->isTlv() ? "not " : "") + "be thread-local"));
48
49return valid;
50}
51
52// Given an offset in the output buffer, figure out which ConcatInputSection (if
53// any) maps to it. At the same time, update the offset such that it is relative
54// to the InputSection rather than to the output buffer.
55//
56// Obtaining the InputSection allows us to have better error diagnostics.
57// However, many of our relocation-handling methods do not take the InputSection
58// as a parameter. Since we are already passing the buffer offsets to our Target
59// methods, this function allows us to emit better errors without threading an
60// additional InputSection argument through the call stack.
61//
62// This is implemented as a slow linear search through OutputSegments,
63// OutputSections, and finally the InputSections themselves. However, this
64// function should be called only on error paths, so some overhead is fine.
65InputSection *macho::offsetToInputSection(uint64_t *off) {
66for (OutputSegment *seg : outputSegments) {
67if (*off < seg->fileOff || *off >= seg->fileOff + seg->fileSize)
68continue;
69
70const std::vector<OutputSection *> §ions = seg->getSections();
71size_t osecIdx = 0;
72for (; osecIdx < sections.size(); ++osecIdx)
73if (*off < sections[osecIdx]->fileOff)
74break;
75assert(osecIdx > 0);
76// We should be only calling this function on offsets that belong to
77// ConcatOutputSections.
78auto *osec = cast<ConcatOutputSection>(sections[osecIdx - 1]);
79*off -= osec->fileOff;
80
81size_t isecIdx = 0;
82for (; isecIdx < osec->inputs.size(); ++isecIdx) {
83const ConcatInputSection *isec = osec->inputs[isecIdx];
84if (*off < isec->outSecOff)
85break;
86}
87assert(isecIdx > 0);
88ConcatInputSection *isec = osec->inputs[isecIdx - 1];
89*off -= isec->outSecOff;
90return isec;
91}
92return nullptr;
93}
94
95void macho::reportRangeError(void *loc, const Reloc &r, const Twine &v,
96uint8_t bits, int64_t min, uint64_t max) {
97std::string hint;
98uint64_t off = reinterpret_cast<const uint8_t *>(loc) - in.bufferStart;
99const InputSection *isec = offsetToInputSection(&off);
100std::string locStr = isec ? isec->getLocation(off) : "(invalid location)";
101if (auto *sym = r.referent.dyn_cast<Symbol *>())
102hint = "; references " + toString(*sym);
103error(locStr + ": relocation " + target->getRelocAttrs(r.type).name +
104" is out of range: " + v + " is not in [" + Twine(min) + ", " +
105Twine(max) + "]" + hint);
106}
107
108void macho::reportRangeError(void *loc, SymbolDiagnostic d, const Twine &v,
109uint8_t bits, int64_t min, uint64_t max) {
110// FIXME: should we use `loc` somehow to provide a better error message?
111std::string hint;
112if (d.symbol)
113hint = "; references " + toString(*d.symbol);
114error(d.reason + " is out of range: " + v + " is not in [" + Twine(min) +
115", " + Twine(max) + "]" + hint);
116}
117
118const RelocAttrs macho::invalidRelocAttrs{"INVALID", RelocAttrBits::_0};
119