1//===------------------------------------------------------------*- C++ -*-===//
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#ifndef LLD_ELF_RELOCSCAN_H
10#define LLD_ELF_RELOCSCAN_H
11
12#include "Config.h"
13#include "InputFiles.h"
14#include "InputSection.h"
15#include "Relocations.h"
16#include "SyntheticSections.h"
17#include "Target.h"
18
19using namespace llvm;
20using namespace llvm::ELF;
21using namespace llvm::object;
22
23namespace lld::elf {
24
25// Build a bitmask with one bit set for each 64 subset of RelExpr.
26inline constexpr uint64_t buildMask() { return 0; }
27
28template <typename... Tails>
29inline constexpr uint64_t buildMask(int head, Tails... tails) {
30 return (0 <= head && head < 64 ? uint64_t(1) << head : 0) |
31 buildMask(tails...);
32}
33
34// Return true if `Expr` is one of `Exprs`.
35// There are more than 64 but less than 128 RelExprs, so we divide the set of
36// exprs into [0, 64) and [64, 128) and represent each range as a constant
37// 64-bit mask. Then we decide which mask to test depending on the value of
38// expr and use a simple shift and bitwise-and to test for membership.
39template <RelExpr... Exprs> bool oneof(RelExpr expr) {
40 assert(0 <= expr && (int)expr < 128 &&
41 "RelExpr is too large for 128-bit mask!");
42
43 if (expr >= 64)
44 return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...);
45 return (uint64_t(1) << expr) & buildMask(Exprs...);
46}
47
48// This class encapsulates states needed to scan relocations for one
49// InputSectionBase.
50class RelocScan {
51public:
52 Ctx &ctx;
53 InputSectionBase *sec;
54 // `relocsVec` shard that discovered dynamic relocations are appended to.
55 unsigned shard;
56
57 RelocScan(Ctx &ctx, InputSectionBase *sec, unsigned shard)
58 : ctx(ctx), sec(sec), shard(shard) {}
59 void scanEhSection(EhInputSection &s);
60
61 template <class RelTy> int64_t getAddend(const RelTy &r, RelType type) {
62 if constexpr (RelTy::HasAddend)
63 return r.r_addend;
64 else
65 return ctx.target->getImplicitAddend(buf: sec->content().data() + r.r_offset,
66 type);
67 }
68 bool maybeReportUndefined(Undefined &sym, uint64_t offset);
69 bool checkTlsLe(uint64_t offset, Symbol &sym, RelType type);
70 bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym,
71 uint64_t relOff) const;
72 void process(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
73 int64_t addend) const;
74 // Process relocation after needsGot/needsPlt flags are already handled.
75 void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
76 int64_t addend) const;
77
78 // Process R_PC relocations. These are the most common relocation type, so we
79 // inline the isStaticLinkTimeConstant check.
80 void processR_PC(RelType type, uint64_t offset, int64_t addend, Symbol &sym) {
81 if (LLVM_UNLIKELY(sym.isGnuIFunc()))
82 sym.setFlags(HAS_DIRECT_RELOC);
83 if (sym.isPreemptible || (isAbsolute(sym) && ctx.arg.isPic))
84 processAux(expr: R_PC, type, offset, sym, addend);
85 else
86 sec->addReloc(r: {.expr: R_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
87 }
88
89 // Process R_PLT_PC relocations. These are very common (calls), so we inline
90 // the isStaticLinkTimeConstant check. Non-preemptible symbols are optimized
91 // to R_PC (direct call).
92 void processR_PLT_PC(RelType type, uint64_t offset, int64_t addend,
93 Symbol &sym) {
94 if (LLVM_UNLIKELY(sym.isGnuIFunc())) {
95 process(expr: R_PLT_PC, type, offset, sym, addend);
96 return;
97 }
98 if (sym.isPreemptible) {
99 sym.setFlags(NEEDS_PLT);
100 sec->addReloc(r: {.expr: R_PLT_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
101 } else if (!(isAbsolute(sym) && ctx.arg.isPic)) {
102 sec->addReloc(r: {.expr: R_PC, .type: type, .offset: offset, .addend: addend, .sym: &sym});
103 } else {
104 processAux(expr: R_PC, type, offset, sym, addend);
105 }
106 }
107
108 // Handle TLS Initial-Exec relocation.
109 template <bool enableIeToLe = true>
110 void handleTlsIe(RelExpr ieExpr, RelType type, uint64_t offset,
111 int64_t addend, Symbol &sym) {
112 if (enableIeToLe && !ctx.arg.shared && !sym.isPreemptible) {
113 // Optimize to Local Exec.
114 sec->addReloc(r: {.expr: R_TPREL, .type: type, .offset: offset, .addend: addend, .sym: &sym});
115 } else {
116 sym.setFlags(NEEDS_TLSIE);
117 // R_GOT (absolute GOT address) needs a RELATIVE dynamic relocation in
118 // PIC when the relocation uses the full address (not just low page bits).
119 if (ieExpr == R_GOT && ctx.arg.isPic &&
120 !ctx.target->usesOnlyLowPageBits(type))
121 ctx.in.relaDyn->addRelativeReloc<true>(dynType: ctx.target->relativeRel, isec&: *sec,
122 offsetInSec: offset, sym, addend, addendRelType: type,
123 expr: ieExpr, shard);
124 else
125 sec->addReloc(r: {.expr: ieExpr, .type: type, .offset: offset, .addend: addend, .sym: &sym});
126 }
127 }
128
129 // Handle TLS Local-Dynamic relocation. Returns true if the __tls_get_addr
130 // call should be skipped (i.e., caller should ++it).
131 bool handleTlsLd(RelExpr sharedExpr, RelType type, uint64_t offset,
132 int64_t addend, Symbol &sym) {
133 if (ctx.arg.shared) {
134 ctx.needsTlsLd.store(i: true, m: std::memory_order_relaxed);
135 sec->addReloc(r: {.expr: sharedExpr, .type: type, .offset: offset, .addend: addend, .sym: &sym});
136 return false;
137 }
138 // Optimize to Local Exec.
139 sec->addReloc(r: {.expr: R_TPREL, .type: type, .offset: offset, .addend: addend, .sym: &sym});
140 return true;
141 }
142
143 // Handle TLS General-Dynamic relocation. Returns true if the __tls_get_addr
144 // call should be skipped (i.e., caller should ++it). Pass R_NONE for
145 // ieExpr/leExpr to disable GD-to-IE/LE optimization (e.g. ARM, RISC-V).
146 bool handleTlsGd(RelExpr sharedExpr, RelExpr ieExpr, RelExpr leExpr,
147 RelType type, uint64_t offset, int64_t addend, Symbol &sym) {
148 if (!ctx.arg.shared && ieExpr != R_NONE) {
149 if (sym.isPreemptible) {
150 // Optimize to Initial Exec.
151 sym.setFlags(NEEDS_TLSIE);
152 sec->addReloc(r: {.expr: ieExpr, .type: type, .offset: offset, .addend: addend, .sym: &sym});
153 } else {
154 // Optimize to Local Exec.
155 sec->addReloc(r: {.expr: leExpr, .type: type, .offset: offset, .addend: addend, .sym: &sym});
156 }
157 return true;
158 }
159 sym.setFlags(NEEDS_TLSGD);
160 sec->addReloc(r: {.expr: sharedExpr, .type: type, .offset: offset, .addend: addend, .sym: &sym});
161 return false;
162 }
163
164 // Handle TLSDESC relocation.
165 void handleTlsDesc(RelExpr sharedExpr, RelExpr ieExpr, RelType type,
166 uint64_t offset, int64_t addend, Symbol &sym) {
167 if (ctx.arg.shared) {
168 sym.setFlags(NEEDS_TLSDESC);
169 sec->addReloc(r: {.expr: sharedExpr, .type: type, .offset: offset, .addend: addend, .sym: &sym});
170 } else if (sym.isPreemptible) {
171 // Optimize to Initial Exec.
172 sym.setFlags(NEEDS_TLSIE);
173 sec->addReloc(r: {.expr: ieExpr, .type: type, .offset: offset, .addend: addend, .sym: &sym});
174 } else {
175 // Optimize to Local Exec.
176 sec->addReloc(r: {.expr: R_TPREL, .type: type, .offset: offset, .addend: addend, .sym: &sym});
177 }
178 }
179};
180
181// Dispatch to target-specific scanSectionImpl based on relocation format.
182template <class ELFT, class Target>
183void scanSection1(Target &target, InputSectionBase &sec, unsigned shard) {
184 const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
185 if (rels.areRelocsCrel())
186 target.scanSectionImpl(sec, rels.crels, shard);
187 else if (rels.areRelocsRel())
188 target.scanSectionImpl(sec, rels.rels, shard);
189 else
190 target.scanSectionImpl(sec, rels.relas, shard);
191}
192
193} // namespace lld::elf
194
195#endif
196