2021-03-12 02:28:09 +08:00
|
|
|
//===- Relocations.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 "Relocations.h"
|
2022-03-01 10:56:38 +08:00
|
|
|
#include "ConcatOutputSection.h"
|
2021-03-12 02:28:09 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
#include "SyntheticSections.h"
|
|
|
|
#include "Target.h"
|
|
|
|
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::macho;
|
|
|
|
|
2021-11-17 09:12:51 +08:00
|
|
|
static_assert(sizeof(void *) != 8 || sizeof(Reloc) == 24,
|
|
|
|
"Try to minimize Reloc's size; we create many instances");
|
|
|
|
|
2021-03-12 02:28:09 +08:00
|
|
|
bool macho::validateSymbolRelocation(const Symbol *sym,
|
|
|
|
const InputSection *isec, const Reloc &r) {
|
|
|
|
const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
|
|
|
|
bool valid = true;
|
2022-02-08 10:06:02 +08:00
|
|
|
auto message = [&](const Twine &diagnostic) {
|
2021-03-12 02:28:09 +08:00
|
|
|
valid = false;
|
2022-02-08 10:06:02 +08:00
|
|
|
return (isec->getLocation(r.offset) + ": " + relocAttrs.name +
|
|
|
|
" relocation " + diagnostic)
|
2021-03-12 02:28:09 +08:00
|
|
|
.str();
|
|
|
|
};
|
|
|
|
|
|
|
|
if (relocAttrs.hasAttr(RelocAttrBits::TLV) != sym->isTlv())
|
2022-02-08 10:06:02 +08:00
|
|
|
error(message(Twine("requires that symbol ") + sym->getName() + " " +
|
2021-03-12 02:28:09 +08:00
|
|
|
(sym->isTlv() ? "not " : "") + "be thread-local"));
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2022-03-01 10:56:38 +08:00
|
|
|
// Given an offset in the output buffer, figure out which ConcatInputSection (if
|
|
|
|
// any) maps to it. At the same time, update the offset such that it is relative
|
|
|
|
// to the InputSection rather than to the output buffer.
|
|
|
|
//
|
|
|
|
// Obtaining the InputSection allows us to have better error diagnostics.
|
|
|
|
// However, many of our relocation-handling methods do not take the InputSection
|
|
|
|
// as a parameter. Since we are already passing the buffer offsets to our Target
|
|
|
|
// methods, this function allows us to emit better errors without threading an
|
|
|
|
// additional InputSection argument through the call stack.
|
|
|
|
//
|
|
|
|
// This is implemented as a slow linear search through OutputSegments,
|
|
|
|
// OutputSections, and finally the InputSections themselves. However, this
|
|
|
|
// function should be called only on error paths, so some overhead is fine.
|
2022-09-04 01:21:12 +08:00
|
|
|
InputSection *macho::offsetToInputSection(uint64_t *off) {
|
2022-03-01 10:56:38 +08:00
|
|
|
for (OutputSegment *seg : outputSegments) {
|
|
|
|
if (*off < seg->fileOff || *off >= seg->fileOff + seg->fileSize)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const std::vector<OutputSection *> §ions = seg->getSections();
|
|
|
|
size_t osecIdx = 0;
|
|
|
|
for (; osecIdx < sections.size(); ++osecIdx)
|
|
|
|
if (*off < sections[osecIdx]->fileOff)
|
|
|
|
break;
|
|
|
|
assert(osecIdx > 0);
|
|
|
|
// We should be only calling this function on offsets that belong to
|
|
|
|
// ConcatOutputSections.
|
|
|
|
auto *osec = cast<ConcatOutputSection>(sections[osecIdx - 1]);
|
|
|
|
*off -= osec->fileOff;
|
|
|
|
|
|
|
|
size_t isecIdx = 0;
|
|
|
|
for (; isecIdx < osec->inputs.size(); ++isecIdx) {
|
|
|
|
const ConcatInputSection *isec = osec->inputs[isecIdx];
|
|
|
|
if (*off < isec->outSecOff)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(isecIdx > 0);
|
|
|
|
ConcatInputSection *isec = osec->inputs[isecIdx - 1];
|
|
|
|
*off -= isec->outSecOff;
|
|
|
|
return isec;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void macho::reportRangeError(void *loc, const Reloc &r, const Twine &v,
|
|
|
|
uint8_t bits, int64_t min, uint64_t max) {
|
2021-03-13 06:26:11 +08:00
|
|
|
std::string hint;
|
2022-03-01 10:56:38 +08:00
|
|
|
uint64_t off = reinterpret_cast<const uint8_t *>(loc) - in.bufferStart;
|
|
|
|
const InputSection *isec = offsetToInputSection(&off);
|
|
|
|
std::string locStr = isec ? isec->getLocation(off) : "(invalid location)";
|
2021-03-13 06:26:11 +08:00
|
|
|
if (auto *sym = r.referent.dyn_cast<Symbol *>())
|
|
|
|
hint = "; references " + toString(*sym);
|
2022-03-01 10:56:38 +08:00
|
|
|
error(locStr + ": relocation " + target->getRelocAttrs(r.type).name +
|
2021-03-13 06:26:11 +08:00
|
|
|
" is out of range: " + v + " is not in [" + Twine(min) + ", " +
|
|
|
|
Twine(max) + "]" + hint);
|
|
|
|
}
|
|
|
|
|
2022-03-01 10:56:38 +08:00
|
|
|
void macho::reportRangeError(void *loc, SymbolDiagnostic d, const Twine &v,
|
|
|
|
uint8_t bits, int64_t min, uint64_t max) {
|
|
|
|
// FIXME: should we use `loc` somehow to provide a better error message?
|
2021-03-13 06:26:11 +08:00
|
|
|
std::string hint;
|
|
|
|
if (d.symbol)
|
|
|
|
hint = "; references " + toString(*d.symbol);
|
|
|
|
error(d.reason + " is out of range: " + v + " is not in [" + Twine(min) +
|
|
|
|
", " + Twine(max) + "]" + hint);
|
|
|
|
}
|
|
|
|
|
2021-03-12 02:28:09 +08:00
|
|
|
const RelocAttrs macho::invalidRelocAttrs{"INVALID", RelocAttrBits::_0};
|