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 InputSection
*Reloc::getReferentInputSection() const {
25 if (const auto *sym
= referent
.dyn_cast
<Symbol
*>()) {
26 if (const auto *d
= dyn_cast
<Defined
>(sym
))
30 return referent
.get
<InputSection
*>();
34 bool macho::validateSymbolRelocation(const Symbol
*sym
,
35 const InputSection
*isec
, const Reloc
&r
) {
36 const RelocAttrs
&relocAttrs
= target
->getRelocAttrs(r
.type
);
38 auto message
= [&](const Twine
&diagnostic
) {
40 return (isec
->getLocation(r
.offset
) + ": " + relocAttrs
.name
+
41 " relocation " + diagnostic
)
45 if (relocAttrs
.hasAttr(RelocAttrBits::TLV
) != sym
->isTlv())
46 error(message(Twine("requires that symbol ") + sym
->getName() + " " +
47 (sym
->isTlv() ? "not " : "") + "be thread-local"));
52 // Given an offset in the output buffer, figure out which ConcatInputSection (if
53 // any) maps to it. At the same time, update the offset such that it is relative
54 // to the InputSection rather than to the output buffer.
56 // Obtaining the InputSection allows us to have better error diagnostics.
57 // However, many of our relocation-handling methods do not take the InputSection
58 // as a parameter. Since we are already passing the buffer offsets to our Target
59 // methods, this function allows us to emit better errors without threading an
60 // additional InputSection argument through the call stack.
62 // This is implemented as a slow linear search through OutputSegments,
63 // OutputSections, and finally the InputSections themselves. However, this
64 // function should be called only on error paths, so some overhead is fine.
65 InputSection
*macho::offsetToInputSection(uint64_t *off
) {
66 for (OutputSegment
*seg
: outputSegments
) {
67 if (*off
< seg
->fileOff
|| *off
>= seg
->fileOff
+ seg
->fileSize
)
70 const std::vector
<OutputSection
*> §ions
= seg
->getSections();
72 for (; osecIdx
< sections
.size(); ++osecIdx
)
73 if (*off
< sections
[osecIdx
]->fileOff
)
76 // We should be only calling this function on offsets that belong to
77 // ConcatOutputSections.
78 auto *osec
= cast
<ConcatOutputSection
>(sections
[osecIdx
- 1]);
79 *off
-= osec
->fileOff
;
82 for (; isecIdx
< osec
->inputs
.size(); ++isecIdx
) {
83 const ConcatInputSection
*isec
= osec
->inputs
[isecIdx
];
84 if (*off
< isec
->outSecOff
)
88 ConcatInputSection
*isec
= osec
->inputs
[isecIdx
- 1];
89 *off
-= isec
->outSecOff
;
95 void macho::reportRangeError(void *loc
, const Reloc
&r
, const Twine
&v
,
96 uint8_t bits
, int64_t min
, uint64_t max
) {
98 uint64_t off
= reinterpret_cast<const uint8_t *>(loc
) - in
.bufferStart
;
99 const InputSection
*isec
= offsetToInputSection(&off
);
100 std::string locStr
= isec
? isec
->getLocation(off
) : "(invalid location)";
101 if (auto *sym
= r
.referent
.dyn_cast
<Symbol
*>())
102 hint
= "; references " + toString(*sym
);
103 error(locStr
+ ": relocation " + target
->getRelocAttrs(r
.type
).name
+
104 " is out of range: " + v
+ " is not in [" + Twine(min
) + ", " +
105 Twine(max
) + "]" + hint
);
108 void macho::reportRangeError(void *loc
, SymbolDiagnostic d
, const Twine
&v
,
109 uint8_t bits
, int64_t min
, uint64_t max
) {
110 // FIXME: should we use `loc` somehow to provide a better error message?
113 hint
= "; references " + toString(*d
.symbol
);
114 error(d
.reason
+ " is out of range: " + v
+ " is not in [" + Twine(min
) +
115 ", " + Twine(max
) + "]" + hint
);
118 const RelocAttrs
macho::invalidRelocAttrs
{"INVALID", RelocAttrBits::_0
};