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 
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   if (relocAttrs.hasAttr(RelocAttrBits::DYSYM8) && isa<DylibSymbol>(sym) &&
35       r.length != 3)
36     error(message("has width " + std::to_string(1 << r.length) +
37                   " bytes, but must be 8 bytes"));
38 
39   return valid;
40 }
41 
42 void macho::reportRangeError(const Reloc &r, const Twine &v, uint8_t bits,
43                              int64_t min, uint64_t max) {
44   std::string hint;
45   if (auto *sym = r.referent.dyn_cast<Symbol *>())
46     hint = "; references " + toString(*sym);
47   // TODO: get location of reloc using something like LLD-ELF's getErrorPlace()
48   error("relocation " + target->getRelocAttrs(r.type).name +
49         " is out of range: " + v + " is not in [" + Twine(min) + ", " +
50         Twine(max) + "]" + hint);
51 }
52 
53 void macho::reportRangeError(SymbolDiagnostic d, const Twine &v, uint8_t bits,
54                              int64_t min, uint64_t max) {
55   std::string hint;
56   if (d.symbol)
57     hint = "; references " + toString(*d.symbol);
58   error(d.reason + " is out of range: " + v + " is not in [" + Twine(min) +
59         ", " + Twine(max) + "]" + hint);
60 }
61 
62 const RelocAttrs macho::invalidRelocAttrs{"INVALID", RelocAttrBits::_0};
63