1 //===- Relocations.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "Relocations.h"
10 #include "ConcatOutputSection.h"
12 #include "SyntheticSections.h"
15 #include "lld/Common/ErrorHandler.h"
19 using namespace lld::macho
;
21 static_assert(sizeof(void *) != 8 || sizeof(Reloc
) == 24,
22 "Try to minimize Reloc's size; we create many instances");
24 bool macho::validateSymbolRelocation(const Symbol
*sym
,
25 const InputSection
*isec
, const Reloc
&r
) {
26 const RelocAttrs
&relocAttrs
= target
->getRelocAttrs(r
.type
);
28 auto message
= [&](const Twine
&diagnostic
) {
30 return (isec
->getLocation(r
.offset
) + ": " + relocAttrs
.name
+
31 " relocation " + diagnostic
)
35 if (relocAttrs
.hasAttr(RelocAttrBits::TLV
) != sym
->isTlv())
36 error(message(Twine("requires that symbol ") + sym
->getName() + " " +
37 (sym
->isTlv() ? "not " : "") + "be thread-local"));
42 // Given an offset in the output buffer, figure out which ConcatInputSection (if
43 // any) maps to it. At the same time, update the offset such that it is relative
44 // to the InputSection rather than to the output buffer.
46 // Obtaining the InputSection allows us to have better error diagnostics.
47 // However, many of our relocation-handling methods do not take the InputSection
48 // as a parameter. Since we are already passing the buffer offsets to our Target
49 // methods, this function allows us to emit better errors without threading an
50 // additional InputSection argument through the call stack.
52 // This is implemented as a slow linear search through OutputSegments,
53 // OutputSections, and finally the InputSections themselves. However, this
54 // function should be called only on error paths, so some overhead is fine.
55 static InputSection
*offsetToInputSection(uint64_t *off
) {
56 for (OutputSegment
*seg
: outputSegments
) {
57 if (*off
< seg
->fileOff
|| *off
>= seg
->fileOff
+ seg
->fileSize
)
60 const std::vector
<OutputSection
*> §ions
= seg
->getSections();
62 for (; osecIdx
< sections
.size(); ++osecIdx
)
63 if (*off
< sections
[osecIdx
]->fileOff
)
66 // We should be only calling this function on offsets that belong to
67 // ConcatOutputSections.
68 auto *osec
= cast
<ConcatOutputSection
>(sections
[osecIdx
- 1]);
69 *off
-= osec
->fileOff
;
72 for (; isecIdx
< osec
->inputs
.size(); ++isecIdx
) {
73 const ConcatInputSection
*isec
= osec
->inputs
[isecIdx
];
74 if (*off
< isec
->outSecOff
)
78 ConcatInputSection
*isec
= osec
->inputs
[isecIdx
- 1];
79 *off
-= isec
->outSecOff
;
85 void macho::reportRangeError(void *loc
, const Reloc
&r
, const Twine
&v
,
86 uint8_t bits
, int64_t min
, uint64_t max
) {
88 uint64_t off
= reinterpret_cast<const uint8_t *>(loc
) - in
.bufferStart
;
89 const InputSection
*isec
= offsetToInputSection(&off
);
90 std::string locStr
= isec
? isec
->getLocation(off
) : "(invalid location)";
91 if (auto *sym
= r
.referent
.dyn_cast
<Symbol
*>())
92 hint
= "; references " + toString(*sym
);
93 error(locStr
+ ": relocation " + target
->getRelocAttrs(r
.type
).name
+
94 " is out of range: " + v
+ " is not in [" + Twine(min
) + ", " +
95 Twine(max
) + "]" + hint
);
98 void macho::reportRangeError(void *loc
, SymbolDiagnostic d
, const Twine
&v
,
99 uint8_t bits
, int64_t min
, uint64_t max
) {
100 // FIXME: should we use `loc` somehow to provide a better error message?
103 hint
= "; references " + toString(*d
.symbol
);
104 error(d
.reason
+ " is out of range: " + v
+ " is not in [" + Twine(min
) +
105 ", " + Twine(max
) + "]" + hint
);
108 const RelocAttrs
macho::invalidRelocAttrs
{"INVALID", RelocAttrBits::_0
};