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 "Symbols.h"
11 #include "SyntheticSections.h"
12 #include "Target.h"
13 
14 #include "lld/Common/ErrorHandler.h"
15 
16 using namespace llvm;
17 using namespace lld;
18 using namespace lld::macho;
19 
validateSymbolRelocation(const Symbol * sym,const InputSection * isec,const Reloc & r)20 bool macho::validateSymbolRelocation(const Symbol *sym,
21                                      const InputSection *isec, const Reloc &r) {
22   const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
23   bool valid = true;
24   auto message = [relocAttrs, sym, isec, &valid](const Twine &diagnostic) {
25     valid = false;
26     return (relocAttrs.name + " relocation " + diagnostic + " for `" +
27             sym->getName() + "' in " + toString(isec))
28         .str();
29   };
30 
31   if (relocAttrs.hasAttr(RelocAttrBits::TLV) != sym->isTlv())
32     error(message(Twine("requires that variable ") +
33                   (sym->isTlv() ? "not " : "") + "be thread-local"));
34 
35   return valid;
36 }
37 
reportRangeError(const Reloc & r,const Twine & v,uint8_t bits,int64_t min,uint64_t max)38 void macho::reportRangeError(const Reloc &r, const Twine &v, uint8_t bits,
39                              int64_t min, uint64_t max) {
40   std::string hint;
41   if (auto *sym = r.referent.dyn_cast<Symbol *>())
42     hint = "; references " + toString(*sym);
43   // TODO: get location of reloc using something like LLD-ELF's getErrorPlace()
44   error("relocation " + target->getRelocAttrs(r.type).name +
45         " is out of range: " + v + " is not in [" + Twine(min) + ", " +
46         Twine(max) + "]" + hint);
47 }
48 
reportRangeError(SymbolDiagnostic d,const Twine & v,uint8_t bits,int64_t min,uint64_t max)49 void macho::reportRangeError(SymbolDiagnostic d, const Twine &v, uint8_t bits,
50                              int64_t min, uint64_t max) {
51   std::string hint;
52   if (d.symbol)
53     hint = "; references " + toString(*d.symbol);
54   error(d.reason + " is out of range: " + v + " is not in [" + Twine(min) +
55         ", " + Twine(max) + "]" + hint);
56 }
57 
58 const RelocAttrs macho::invalidRelocAttrs{"INVALID", RelocAttrBits::_0};
59