annotate lld/MachO/Relocations.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- Relocations.cpp ----------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "Relocations.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "Symbols.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "SyntheticSections.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "lld/Common/ErrorHandler.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 using namespace llvm;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 using namespace lld::macho;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 bool macho::validateSymbolRelocation(const Symbol *sym,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 const InputSection *isec, const Reloc &r) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 bool valid = true;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 auto message = [relocAttrs, sym, isec, &valid](const Twine &diagnostic) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 valid = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 return (relocAttrs.name + " relocation " + diagnostic + " for `" +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 sym->getName() + "' in " + toString(isec))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 .str();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 if (relocAttrs.hasAttr(RelocAttrBits::TLV) != sym->isTlv())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 error(message(Twine("requires that variable ") +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 (sym->isTlv() ? "not " : "") + "be thread-local"));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 return valid;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 void macho::reportRangeError(const Reloc &r, const Twine &v, uint8_t bits,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 int64_t min, uint64_t max) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 std::string hint;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 if (auto *sym = r.referent.dyn_cast<Symbol *>())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 hint = "; references " + toString(*sym);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 // TODO: get location of reloc using something like LLD-ELF's getErrorPlace()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 error("relocation " + target->getRelocAttrs(r.type).name +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 " is out of range: " + v + " is not in [" + Twine(min) + ", " +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 Twine(max) + "]" + hint);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 void macho::reportRangeError(SymbolDiagnostic d, const Twine &v, uint8_t bits,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 int64_t min, uint64_t max) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 std::string hint;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 if (d.symbol)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 hint = "; references " + toString(*d.symbol);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 error(d.reason + " is out of range: " + v + " is not in [" + Twine(min) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 ", " + Twine(max) + "]" + hint);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 const RelocAttrs macho::invalidRelocAttrs{"INVALID", RelocAttrBits::_0};