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
55 RelocScan(Ctx &ctx, InputSectionBase *sec = nullptr) : ctx(ctx), sec(sec) {}
56 template <class ELFT, class RelTy>
57 void scan(typename Relocs<RelTy>::const_iterator &i, RelType type,
58 int64_t addend);
59 void scanEhSection(EhInputSection &s);
60
61 template <class ELFT, class RelTy>
62 int64_t getAddend(const RelTy &r, RelType type);
63 bool maybeReportUndefined(Undefined &sym, uint64_t offset);
64 bool checkTlsLe(uint64_t offset, Symbol &sym, RelType type);
65 bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym,
66 uint64_t relOff) const;
67 void process(RelExpr expr, RelType type, uint64_t offset, Symbol &sym,
68 int64_t addend) const;
69 unsigned handleTlsRelocation(RelExpr expr, RelType type, uint64_t offset,
70 Symbol &sym, int64_t addend);
71};
72
73template <class ELFT, class RelTy>
74int64_t RelocScan::getAddend(const RelTy &r, RelType type) {
75 return RelTy::HasAddend ? elf::getAddend<ELFT>(r)
76 : ctx.target->getImplicitAddend(
77 buf: sec->content().data() + r.r_offset, type);
78}
79
80template <class ELFT, class RelTy>
81void RelocScan::scan(typename Relocs<RelTy>::const_iterator &it, RelType type,
82 int64_t addend) {
83 const RelTy &rel = *it;
84 uint32_t symIdx = rel.getSymbol(false);
85 Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIdx);
86 uint64_t offset = rel.r_offset;
87 RelExpr expr =
88 ctx.target->getRelExpr(type, s: sym, loc: sec->content().data() + offset);
89
90 // Ignore R_*_NONE and other marker relocations.
91 if (expr == R_NONE)
92 return;
93
94 // Error if the target symbol is undefined. Symbol index 0 may be used by
95 // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
96 if (sym.isUndefined() && symIdx != 0 &&
97 maybeReportUndefined(sym&: cast<Undefined>(Val&: sym), offset))
98 return;
99
100 // Ensure GOT or GOTPLT is created for relocations that reference their base
101 // addresses without directly creating entries.
102 if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT,
103 R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
104 ctx.in.gotPlt->hasGotPltOffRel.store(i: true, m: std::memory_order_relaxed);
105 } else if (oneof<R_GOTONLY_PC, R_GOTREL, RE_PPC32_PLTREL>(expr)) {
106 ctx.in.got->hasGotOffRel.store(i: true, m: std::memory_order_relaxed);
107 }
108
109 // Process TLS relocations, including TLS optimizations. Note that
110 // R_TPREL and R_TPREL_NEG relocations are resolved in processAux.
111 //
112 // Some RISCV TLSDESC relocations reference a local NOTYPE symbol,
113 // but we need to process them in handleTlsRelocation.
114 if (sym.isTls() || oneof<R_TLSDESC_PC, R_TLSDESC_CALL>(expr)) {
115 if (unsigned processed =
116 handleTlsRelocation(expr, type, offset, sym, addend)) {
117 it += processed - 1;
118 return;
119 }
120 }
121
122 process(expr, type, offset, sym, addend);
123}
124
125// Dispatch to target-specific scanSectionImpl based on relocation format.
126template <class Target, class ELFT>
127void scanSection1(Target &target, InputSectionBase &sec) {
128 const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();
129 if (rels.areRelocsCrel())
130 target.template scanSectionImpl<ELFT>(sec, rels.crels);
131 else if (rels.areRelocsRel())
132 target.template scanSectionImpl<ELFT>(sec, rels.rels);
133 else
134 target.template scanSectionImpl<ELFT>(sec, rels.relas);
135}
136
137} // namespace lld::elf
138
139#endif
140