Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/Relocations.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Relocations.cpp ----------------------------------------------------===//
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
// This file contains platform-independent functions to process relocations.
10
// I'll describe the overview of this file here.
11
//
12
// Simple relocations are easy to handle for the linker. For example,
13
// for R_X86_64_PC64 relocs, the linker just has to fix up locations
14
// with the relative offsets to the target symbols. It would just be
15
// reading records from relocation sections and applying them to output.
16
//
17
// But not all relocations are that easy to handle. For example, for
18
// R_386_GOTOFF relocs, the linker has to create new GOT entries for
19
// symbols if they don't exist, and fix up locations with GOT entry
20
// offsets from the beginning of GOT section. So there is more than
21
// fixing addresses in relocation processing.
22
//
23
// ELF defines a large number of complex relocations.
24
//
25
// The functions in this file analyze relocations and do whatever needs
26
// to be done. It includes, but not limited to, the following.
27
//
28
//  - create GOT/PLT entries
29
//  - create new relocations in .dynsym to let the dynamic linker resolve
30
//    them at runtime (since ELF supports dynamic linking, not all
31
//    relocations can be resolved at link-time)
32
//  - create COPY relocs and reserve space in .bss
33
//  - replace expensive relocs (in terms of runtime cost) with cheap ones
34
//  - error out infeasible combinations such as PIC and non-relative relocs
35
//
36
// Note that the functions in this file don't actually apply relocations
37
// because it doesn't know about the output file nor the output file buffer.
38
// It instead stores Relocation objects to InputSection's Relocations
39
// vector to let it apply later in InputSection::writeTo.
40
//
41
//===----------------------------------------------------------------------===//
42
43
#include "Relocations.h"
44
#include "Config.h"
45
#include "LinkerScript.h"
46
#include "OutputSections.h"
47
#include "SymbolTable.h"
48
#include "Symbols.h"
49
#include "SyntheticSections.h"
50
#include "Target.h"
51
#include "Thunks.h"
52
#include "lld/Common/ErrorHandler.h"
53
#include "lld/Common/Memory.h"
54
#include "lld/Common/Strings.h"
55
#include "llvm/ADT/SmallSet.h"
56
#include "llvm/Support/Endian.h"
57
#include "llvm/Support/raw_ostream.h"
58
#include <algorithm>
59
60
using namespace llvm;
61
using namespace llvm::ELF;
62
using namespace llvm::object;
63
using namespace llvm::support::endian;
64
65
using namespace lld;
66
using namespace lld::elf;
67
68
0
static Optional<std::string> getLinkerScriptLocation(const Symbol &Sym) {
69
0
  for (BaseCommand *Base : Script->SectionCommands)
70
0
    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
71
0
      if (Cmd->Sym == &Sym)
72
0
        return Cmd->Location;
73
0
  return None;
74
0
}
75
76
// Construct a message in the following format.
77
//
78
// >>> defined in /home/alice/src/foo.o
79
// >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)
80
// >>>               /home/alice/src/bar.o:(.text+0x1)
81
static std::string getLocation(InputSectionBase &S, const Symbol &Sym,
82
51
                               uint64_t Off) {
83
51
  std::string Msg = "\n>>> defined in ";
84
51
  if (Sym.File)
85
51
    Msg += toString(Sym.File);
86
0
  else if (Optional<std::string> Loc = getLinkerScriptLocation(Sym))
87
0
    Msg += *Loc;
88
51
89
51
  Msg += "\n>>> referenced by ";
90
51
  std::string Src = S.getSrcMsg(Sym, Off);
91
51
  if (!Src.empty())
92
1
    Msg += Src + "\n>>>               ";
93
51
  return Msg + S.getObjMsg(Off);
94
51
}
95
96
namespace {
97
// Build a bitmask with one bit set for each RelExpr.
98
//
99
// Constexpr function arguments can't be used in static asserts, so we
100
// use template arguments to build the mask.
101
// But function template partial specializations don't exist (needed
102
// for base case of the recursion), so we need a dummy struct.
103
template <RelExpr... Exprs> struct RelExprMaskBuilder {
104
85.3k
  static inline uint64_t build() { return 0; }
105
};
106
107
// Specialization for recursive case.
108
template <RelExpr Head, RelExpr... Tail>
109
struct RelExprMaskBuilder<Head, Tail...> {
110
674k
  static inline uint64_t build() {
111
674k
    static_assert(0 <= Head && Head < 64,
112
674k
                  "RelExpr is too large for 64-bit mask!");
113
674k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
674k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)11, (lld::elf::RelExpr)13>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)13>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)7, (lld::elf::RelExpr)9, (lld::elf::RelExpr)8, (lld::elf::RelExpr)33>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)9, (lld::elf::RelExpr)8, (lld::elf::RelExpr)33>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8, (lld::elf::RelExpr)33>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)33>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)6, (lld::elf::RelExpr)10, (lld::elf::RelExpr)57, (lld::elf::RelExpr)58>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)10, (lld::elf::RelExpr)57, (lld::elf::RelExpr)58>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)57, (lld::elf::RelExpr)58>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)58>::build()
Line
Count
Source
110
14.0k
  static inline uint64_t build() {
111
14.0k
    static_assert(0 <= Head && Head < 64,
112
14.0k
                  "RelExpr is too large for 64-bit mask!");
113
14.0k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.0k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)30, (lld::elf::RelExpr)44, (lld::elf::RelExpr)31>::build()
Line
Count
Source
110
403
  static inline uint64_t build() {
111
403
    static_assert(0 <= Head && Head < 64,
112
403
                  "RelExpr is too large for 64-bit mask!");
113
403
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
403
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)44, (lld::elf::RelExpr)31>::build()
Line
Count
Source
110
403
  static inline uint64_t build() {
111
403
    static_assert(0 <= Head && Head < 64,
112
403
                  "RelExpr is too large for 64-bit mask!");
113
403
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
403
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)31>::build()
Line
Count
Source
110
403
  static inline uint64_t build() {
111
403
    static_assert(0 <= Head && Head < 64,
112
403
                  "RelExpr is too large for 64-bit mask!");
113
403
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
403
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)36, (lld::elf::RelExpr)37, (lld::elf::RelExpr)40, (lld::elf::RelExpr)39>::build()
Line
Count
Source
110
381
  static inline uint64_t build() {
111
381
    static_assert(0 <= Head && Head < 64,
112
381
                  "RelExpr is too large for 64-bit mask!");
113
381
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
381
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)37, (lld::elf::RelExpr)40, (lld::elf::RelExpr)39>::build()
Line
Count
Source
110
381
  static inline uint64_t build() {
111
381
    static_assert(0 <= Head && Head < 64,
112
381
                  "RelExpr is too large for 64-bit mask!");
113
381
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
381
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)40, (lld::elf::RelExpr)39>::build()
Line
Count
Source
110
381
  static inline uint64_t build() {
111
381
    static_assert(0 <= Head && Head < 64,
112
381
                  "RelExpr is too large for 64-bit mask!");
113
381
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
381
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)39>::build()
Line
Count
Source
110
381
  static inline uint64_t build() {
111
381
    static_assert(0 <= Head && Head < 64,
112
381
                  "RelExpr is too large for 64-bit mask!");
113
381
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
381
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)30, (lld::elf::RelExpr)44, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
314
  static inline uint64_t build() {
111
314
    static_assert(0 <= Head && Head < 64,
112
314
                  "RelExpr is too large for 64-bit mask!");
113
314
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
314
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)44, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
314
  static inline uint64_t build() {
111
314
    static_assert(0 <= Head && Head < 64,
112
314
                  "RelExpr is too large for 64-bit mask!");
113
314
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
314
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
314
  static inline uint64_t build() {
111
314
    static_assert(0 <= Head && Head < 64,
112
314
                  "RelExpr is too large for 64-bit mask!");
113
314
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
314
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
314
  static inline uint64_t build() {
111
314
    static_assert(0 <= Head && Head < 64,
112
314
                  "RelExpr is too large for 64-bit mask!");
113
314
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
314
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)33, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
314
  static inline uint64_t build() {
111
314
    static_assert(0 <= Head && Head < 64,
112
314
                  "RelExpr is too large for 64-bit mask!");
113
314
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
314
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)34>::build()
Line
Count
Source
110
314
  static inline uint64_t build() {
111
314
    static_assert(0 <= Head && Head < 64,
112
314
                  "RelExpr is too large for 64-bit mask!");
113
314
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
314
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)3, (lld::elf::RelExpr)8, (lld::elf::RelExpr)5, (lld::elf::RelExpr)41, (lld::elf::RelExpr)4, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
243
  static inline uint64_t build() {
111
243
    static_assert(0 <= Head && Head < 64,
112
243
                  "RelExpr is too large for 64-bit mask!");
113
243
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
243
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8, (lld::elf::RelExpr)5, (lld::elf::RelExpr)41, (lld::elf::RelExpr)4, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
243
  static inline uint64_t build() {
111
243
    static_assert(0 <= Head && Head < 64,
112
243
                  "RelExpr is too large for 64-bit mask!");
113
243
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
243
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)5, (lld::elf::RelExpr)41, (lld::elf::RelExpr)4, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
243
  static inline uint64_t build() {
111
243
    static_assert(0 <= Head && Head < 64,
112
243
                  "RelExpr is too large for 64-bit mask!");
113
243
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
243
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)41, (lld::elf::RelExpr)4, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
243
  static inline uint64_t build() {
111
243
    static_assert(0 <= Head && Head < 64,
112
243
                  "RelExpr is too large for 64-bit mask!");
113
243
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
243
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)4, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
243
  static inline uint64_t build() {
111
243
    static_assert(0 <= Head && Head < 64,
112
243
                  "RelExpr is too large for 64-bit mask!");
113
243
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
243
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.9k
  static inline uint64_t build() {
111
13.9k
    static_assert(0 <= Head && Head < 64,
112
13.9k
                  "RelExpr is too large for 64-bit mask!");
113
13.9k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.9k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)16, (lld::elf::RelExpr)56, (lld::elf::RelExpr)15>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)56, (lld::elf::RelExpr)15>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)15>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)3, (lld::elf::RelExpr)4, (lld::elf::RelExpr)46, (lld::elf::RelExpr)50, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)4, (lld::elf::RelExpr)46, (lld::elf::RelExpr)50, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)46, (lld::elf::RelExpr)50, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)50, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)52, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)2, (lld::elf::RelExpr)8, (lld::elf::RelExpr)4, (lld::elf::RelExpr)46, (lld::elf::RelExpr)38, (lld::elf::RelExpr)50, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8, (lld::elf::RelExpr)4, (lld::elf::RelExpr)46, (lld::elf::RelExpr)38, (lld::elf::RelExpr)50, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)4, (lld::elf::RelExpr)46, (lld::elf::RelExpr)38, (lld::elf::RelExpr)50, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)46, (lld::elf::RelExpr)38, (lld::elf::RelExpr)50, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)38, (lld::elf::RelExpr)50, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)50, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)39, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
13.7k
  static inline uint64_t build() {
111
13.7k
    static_assert(0 <= Head && Head < 64,
112
13.7k
                  "RelExpr is too large for 64-bit mask!");
113
13.7k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
13.7k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)14, (lld::elf::RelExpr)10, (lld::elf::RelExpr)9, (lld::elf::RelExpr)47, (lld::elf::RelExpr)55, (lld::elf::RelExpr)58, (lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)10, (lld::elf::RelExpr)9, (lld::elf::RelExpr)47, (lld::elf::RelExpr)55, (lld::elf::RelExpr)58, (lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)9, (lld::elf::RelExpr)47, (lld::elf::RelExpr)55, (lld::elf::RelExpr)58, (lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)47, (lld::elf::RelExpr)55, (lld::elf::RelExpr)58, (lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)55, (lld::elf::RelExpr)58, (lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)58, (lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)42, (lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)17>::build()
Line
Count
Source
110
615
  static inline uint64_t build() {
111
615
    static_assert(0 <= Head && Head < 64,
112
615
                  "RelExpr is too large for 64-bit mask!");
113
615
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
615
  }
115
};
116
} // namespace
117
118
// Return true if `Expr` is one of `Exprs`.
119
// There are fewer than 64 RelExpr's, so we can represent any set of
120
// RelExpr's as a constant bit mask and test for membership with a
121
// couple cheap bitwise operations.
122
85.3k
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
85.3k
  assert(0 <= Expr && (int)Expr < 64 &&
124
85.3k
         "RelExpr is too large for 64-bit mask!");
125
85.3k
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
85.3k
}
bool oneof<(lld::elf::RelExpr)11, (lld::elf::RelExpr)13>(lld::elf::RelExpr)
Line
Count
Source
122
14.0k
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
14.0k
  assert(0 <= Expr && (int)Expr < 64 &&
124
14.0k
         "RelExpr is too large for 64-bit mask!");
125
14.0k
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.0k
}
bool oneof<(lld::elf::RelExpr)7, (lld::elf::RelExpr)9, (lld::elf::RelExpr)8, (lld::elf::RelExpr)33>(lld::elf::RelExpr)
Line
Count
Source
122
14.0k
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
14.0k
  assert(0 <= Expr && (int)Expr < 64 &&
124
14.0k
         "RelExpr is too large for 64-bit mask!");
125
14.0k
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.0k
}
bool oneof<(lld::elf::RelExpr)6, (lld::elf::RelExpr)10, (lld::elf::RelExpr)57, (lld::elf::RelExpr)58>(lld::elf::RelExpr)
Line
Count
Source
122
14.0k
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
14.0k
  assert(0 <= Expr && (int)Expr < 64 &&
124
14.0k
         "RelExpr is too large for 64-bit mask!");
125
14.0k
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.0k
}
bool oneof<(lld::elf::RelExpr)30, (lld::elf::RelExpr)44, (lld::elf::RelExpr)31>(lld::elf::RelExpr)
Line
Count
Source
122
403
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
403
  assert(0 <= Expr && (int)Expr < 64 &&
124
403
         "RelExpr is too large for 64-bit mask!");
125
403
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
403
}
bool oneof<(lld::elf::RelExpr)36, (lld::elf::RelExpr)37, (lld::elf::RelExpr)40, (lld::elf::RelExpr)39>(lld::elf::RelExpr)
Line
Count
Source
122
381
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
381
  assert(0 <= Expr && (int)Expr < 64 &&
124
381
         "RelExpr is too large for 64-bit mask!");
125
381
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
381
}
bool oneof<(lld::elf::RelExpr)30, (lld::elf::RelExpr)44, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34>(lld::elf::RelExpr)
Line
Count
Source
122
314
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
314
  assert(0 <= Expr && (int)Expr < 64 &&
124
314
         "RelExpr is too large for 64-bit mask!");
125
314
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
314
}
bool oneof<(lld::elf::RelExpr)3, (lld::elf::RelExpr)8, (lld::elf::RelExpr)5, (lld::elf::RelExpr)41, (lld::elf::RelExpr)4, (lld::elf::RelExpr)35>(lld::elf::RelExpr)
Line
Count
Source
122
243
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
243
  assert(0 <= Expr && (int)Expr < 64 &&
124
243
         "RelExpr is too large for 64-bit mask!");
125
243
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
243
}
bool oneof<(lld::elf::RelExpr)16, (lld::elf::RelExpr)56, (lld::elf::RelExpr)15>(lld::elf::RelExpr)
Line
Count
Source
122
13.7k
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
13.7k
  assert(0 <= Expr && (int)Expr < 64 &&
124
13.7k
         "RelExpr is too large for 64-bit mask!");
125
13.7k
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
13.7k
}
bool oneof<(lld::elf::RelExpr)3, (lld::elf::RelExpr)4, (lld::elf::RelExpr)46, (lld::elf::RelExpr)50, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>(lld::elf::RelExpr)
Line
Count
Source
122
13.7k
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
13.7k
  assert(0 <= Expr && (int)Expr < 64 &&
124
13.7k
         "RelExpr is too large for 64-bit mask!");
125
13.7k
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
13.7k
}
bool oneof<(lld::elf::RelExpr)2, (lld::elf::RelExpr)8, (lld::elf::RelExpr)4, (lld::elf::RelExpr)46, (lld::elf::RelExpr)38, (lld::elf::RelExpr)50, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)49, (lld::elf::RelExpr)53, (lld::elf::RelExpr)41, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)31, (lld::elf::RelExpr)44, (lld::elf::RelExpr)11, (lld::elf::RelExpr)39, (lld::elf::RelExpr)35>(lld::elf::RelExpr)
Line
Count
Source
122
13.7k
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
13.7k
  assert(0 <= Expr && (int)Expr < 64 &&
124
13.7k
         "RelExpr is too large for 64-bit mask!");
125
13.7k
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
13.7k
}
bool oneof<(lld::elf::RelExpr)14, (lld::elf::RelExpr)10, (lld::elf::RelExpr)9, (lld::elf::RelExpr)47, (lld::elf::RelExpr)55, (lld::elf::RelExpr)58, (lld::elf::RelExpr)56, (lld::elf::RelExpr)42, (lld::elf::RelExpr)17>(lld::elf::RelExpr)
Line
Count
Source
122
615
template <RelExpr... Exprs> bool oneof(RelExpr Expr) {
123
615
  assert(0 <= Expr && (int)Expr < 64 &&
124
615
         "RelExpr is too large for 64-bit mask!");
125
615
  return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
126
615
}
127
128
// This function is similar to the `handleTlsRelocation`. MIPS does not
129
// support any relaxations for TLS relocations so by factoring out MIPS
130
// handling in to the separate function we can simplify the code and do not
131
// pollute other `handleTlsRelocation` by MIPS `ifs` statements.
132
// Mips has a custom MipsGotSection that handles the writing of GOT entries
133
// without dynamic relocations.
134
static unsigned handleMipsTlsRelocation(RelType Type, Symbol &Sym,
135
                                        InputSectionBase &C, uint64_t Offset,
136
43
                                        int64_t Addend, RelExpr Expr) {
137
43
  if (Expr == R_MIPS_TLSLD) {
138
6
    In.MipsGot->addTlsIndex(*C.File);
139
6
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
140
6
    return 1;
141
6
  }
142
37
  if (Expr == R_MIPS_TLSGD) {
143
12
    In.MipsGot->addDynTlsEntry(*C.File, Sym);
144
12
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
145
12
    return 1;
146
12
  }
147
25
  return 0;
148
25
}
149
150
// This function is similar to the `handleMipsTlsRelocation`. ARM also does not
151
// support any relaxations for TLS relocations. ARM is logically similar to Mips
152
// in how it handles TLS, but Mips uses its own custom GOT which handles some
153
// of the cases that ARM uses GOT relocations for.
154
//
155
// We look for TLS global dynamic and local dynamic relocations, these may
156
// require the generation of a pair of GOT entries that have associated
157
// dynamic relocations. When the results of the dynamic relocations can be
158
// resolved at static link time we do so. This is necessary for static linking
159
// as there will be no dynamic loader to resolve them at load-time.
160
//
161
// The pair of GOT entries created are of the form
162
// GOT[e0] Module Index (Used to find pointer to TLS block at run-time)
163
// GOT[e1] Offset of symbol in TLS block
164
static unsigned handleARMTlsRelocation(RelType Type, Symbol &Sym,
165
                                       InputSectionBase &C, uint64_t Offset,
166
28
                                       int64_t Addend, RelExpr Expr) {
167
28
  // The Dynamic TLS Module Index Relocation for a symbol defined in an
168
28
  // executable is always 1. If the target Symbol is not preemptible then
169
28
  // we know the offset into the TLS block at static link time.
170
28
  bool NeedDynId = Sym.IsPreemptible || 
Config->Shared21
;
171
28
  bool NeedDynOff = Sym.IsPreemptible;
172
28
173
30
  auto AddTlsReloc = [&](uint64_t Off, RelType Type, Symbol *Dest, bool Dyn) {
174
30
    if (Dyn)
175
14
      In.RelaDyn->addReloc(Type, In.Got, Off, Dest);
176
16
    else
177
16
      In.Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest});
178
30
  };
179
28
180
28
  // Local Dynamic is for access to module local TLS variables, while still
181
28
  // being suitable for being dynamically loaded via dlopen.
182
28
  // GOT[e0] is the module index, with a special value of 0 for the current
183
28
  // module. GOT[e1] is unused. There only needs to be one module index entry.
184
28
  if (Expr == R_TLSLD_PC && 
In.Got->addTlsIndex()2
) {
185
2
    AddTlsReloc(In.Got->getTlsIndexOff(), Target->TlsModuleIndexRel,
186
2
                NeedDynId ? 
nullptr1
:
&Sym1
, NeedDynId);
187
2
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
188
2
    return 1;
189
2
  }
190
26
191
26
  // Global Dynamic is the most general purpose access model. When we know
192
26
  // the module index and offset of symbol in TLS block we can fill these in
193
26
  // using static GOT relocations.
194
26
  if (Expr == R_TLSGD_PC) {
195
14
    if (In.Got->addDynTlsEntry(Sym)) {
196
14
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
197
14
      AddTlsReloc(Off, Target->TlsModuleIndexRel, &Sym, NeedDynId);
198
14
      AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Sym,
199
14
                  NeedDynOff);
200
14
    }
201
14
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
202
14
    return 1;
203
14
  }
204
12
  return 0;
205
12
}
206
207
// Returns the number of relocations processed.
208
template <class ELFT>
209
static unsigned
210
handleTlsRelocation(RelType Type, Symbol &Sym, InputSectionBase &C,
211
14.0k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
212
14.0k
  if (!Sym.isTls())
213
13.5k
    return 0;
214
474
215
474
  if (Config->EMachine == EM_ARM)
216
28
    return handleARMTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
217
446
  if (Config->EMachine == EM_MIPS)
218
43
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
219
403
220
403
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
221
403
      
Config->Shared40
) {
222
22
    if (In.Got->addDynTlsEntry(Sym)) {
223
5
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
224
5
      In.RelaDyn->addReloc(
225
5
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
226
5
    }
227
22
    if (Expr != R_TLSDESC_CALL)
228
15
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
229
22
    return 1;
230
22
  }
231
381
232
381
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
233
381
          Expr)) {
234
48
    // Local-Dynamic relocs can be relaxed to Local-Exec.
235
48
    if (!Config->Shared) {
236
13
      C.Relocations.push_back(
237
13
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
238
13
           Offset, Addend, &Sym});
239
13
      return Target->getTlsGdRelaxSkip(Type);
240
13
    }
241
35
    if (Expr == R_TLSLD_HINT)
242
8
      return 1;
243
27
    if (In.Got->addTlsIndex())
244
9
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
245
9
                           In.Got->getTlsIndexOff(), nullptr);
246
27
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
247
27
    return 1;
248
27
  }
249
333
250
333
  // Local-Dynamic relocs can be relaxed to Local-Exec.
251
333
  if (Expr == R_DTPREL && 
!Config->Shared53
) {
252
11
    C.Relocations.push_back(
253
11
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
254
11
         Offset, Addend, &Sym});
255
11
    return 1;
256
11
  }
257
322
258
322
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
259
322
  // thread pointer is stored in the got.
260
322
  if (Expr == R_TLSLD_GOT_OFF) {
261
8
    // Local-Dynamic relocs can be relaxed to local-exec
262
8
    if (!Config->Shared) {
263
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
264
0
      return 1;
265
0
    }
266
8
    if (!Sym.isInGot()) {
267
2
      In.Got->addEntry(Sym);
268
2
      uint64_t Off = Sym.getGotOffset();
269
2
      In.Got->Relocations.push_back(
270
2
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
271
2
    }
272
8
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
273
8
    return 1;
274
8
  }
275
314
276
314
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD_GOT,
277
314
            R_TLSGD_GOTPLT, R_TLSGD_PC>(Expr)) {
278
71
    if (Config->Shared) {
279
16
      if (In.Got->addDynTlsEntry(Sym)) {
280
13
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
281
13
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
282
13
283
13
        // If the symbol is preemptible we need the dynamic linker to write
284
13
        // the offset too.
285
13
        uint64_t OffsetOff = Off + Config->Wordsize;
286
13
        if (Sym.IsPreemptible)
287
10
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
288
3
        else
289
3
          In.Got->Relocations.push_back(
290
3
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
291
13
      }
292
16
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
293
16
      return 1;
294
16
    }
295
55
296
55
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
297
55
    // depending on the symbol being locally defined or not.
298
55
    if (Sym.IsPreemptible) {
299
30
      C.Relocations.push_back(
300
30
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
301
30
           Offset, Addend, &Sym});
302
30
      if (!Sym.isInGot()) {
303
12
        In.Got->addEntry(Sym);
304
12
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
305
12
                             &Sym);
306
12
      }
307
30
    } else {
308
25
      C.Relocations.push_back(
309
25
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
310
25
           Offset, Addend, &Sym});
311
25
    }
312
55
    return Target->getTlsGdRelaxSkip(Type);
313
55
  }
314
243
315
243
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
316
243
  // defined.
317
243
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
318
243
            R_TLSIE_HINT>(Expr) &&
319
243
      
!Config->Shared151
&&
!Sym.IsPreemptible130
) {
320
110
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
321
110
    return 1;
322
110
  }
323
133
324
133
  if (Expr == R_TLSIE_HINT)
325
2
    return 1;
326
131
  return 0;
327
131
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, false>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
211
944
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
212
944
  if (!Sym.isTls())
213
860
    return 0;
214
84
215
84
  if (Config->EMachine == EM_ARM)
216
28
    return handleARMTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
217
56
  if (Config->EMachine == EM_MIPS)
218
0
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
219
56
220
56
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
221
56
      
Config->Shared0
) {
222
0
    if (In.Got->addDynTlsEntry(Sym)) {
223
0
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
224
0
      In.RelaDyn->addReloc(
225
0
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
226
0
    }
227
0
    if (Expr != R_TLSDESC_CALL)
228
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
229
0
    return 1;
230
0
  }
231
56
232
56
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
233
56
          Expr)) {
234
5
    // Local-Dynamic relocs can be relaxed to Local-Exec.
235
5
    if (!Config->Shared) {
236
2
      C.Relocations.push_back(
237
2
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
238
2
           Offset, Addend, &Sym});
239
2
      return Target->getTlsGdRelaxSkip(Type);
240
2
    }
241
3
    if (Expr == R_TLSLD_HINT)
242
0
      return 1;
243
3
    if (In.Got->addTlsIndex())
244
2
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
245
2
                           In.Got->getTlsIndexOff(), nullptr);
246
3
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
247
3
    return 1;
248
3
  }
249
51
250
51
  // Local-Dynamic relocs can be relaxed to Local-Exec.
251
51
  if (Expr == R_DTPREL && 
!Config->Shared5
) {
252
2
    C.Relocations.push_back(
253
2
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
254
2
         Offset, Addend, &Sym});
255
2
    return 1;
256
2
  }
257
49
258
49
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
259
49
  // thread pointer is stored in the got.
260
49
  if (Expr == R_TLSLD_GOT_OFF) {
261
0
    // Local-Dynamic relocs can be relaxed to local-exec
262
0
    if (!Config->Shared) {
263
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
264
0
      return 1;
265
0
    }
266
0
    if (!Sym.isInGot()) {
267
0
      In.Got->addEntry(Sym);
268
0
      uint64_t Off = Sym.getGotOffset();
269
0
      In.Got->Relocations.push_back(
270
0
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
271
0
    }
272
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
273
0
    return 1;
274
0
  }
275
49
276
49
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD_GOT,
277
49
            R_TLSGD_GOTPLT, R_TLSGD_PC>(Expr)) {
278
7
    if (Config->Shared) {
279
2
      if (In.Got->addDynTlsEntry(Sym)) {
280
2
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
281
2
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
282
2
283
2
        // If the symbol is preemptible we need the dynamic linker to write
284
2
        // the offset too.
285
2
        uint64_t OffsetOff = Off + Config->Wordsize;
286
2
        if (Sym.IsPreemptible)
287
2
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
288
0
        else
289
0
          In.Got->Relocations.push_back(
290
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
291
2
      }
292
2
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
293
2
      return 1;
294
2
    }
295
5
296
5
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
297
5
    // depending on the symbol being locally defined or not.
298
5
    if (Sym.IsPreemptible) {
299
2
      C.Relocations.push_back(
300
2
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
301
2
           Offset, Addend, &Sym});
302
2
      if (!Sym.isInGot()) {
303
2
        In.Got->addEntry(Sym);
304
2
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
305
2
                             &Sym);
306
2
      }
307
3
    } else {
308
3
      C.Relocations.push_back(
309
3
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
310
3
           Offset, Addend, &Sym});
311
3
    }
312
5
    return Target->getTlsGdRelaxSkip(Type);
313
5
  }
314
42
315
42
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
316
42
  // defined.
317
42
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
318
42
            R_TLSIE_HINT>(Expr) &&
319
42
      
!Config->Shared27
&&
!Sym.IsPreemptible13
) {
320
10
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
321
10
    return 1;
322
10
  }
323
32
324
32
  if (Expr == R_TLSIE_HINT)
325
0
    return 1;
326
32
  return 0;
327
32
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, false> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, false>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
211
285
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
212
285
  if (!Sym.isTls())
213
255
    return 0;
214
30
215
30
  if (Config->EMachine == EM_ARM)
216
0
    return handleARMTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
217
30
  if (Config->EMachine == EM_MIPS)
218
30
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
219
0
220
0
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
221
0
      Config->Shared) {
222
0
    if (In.Got->addDynTlsEntry(Sym)) {
223
0
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
224
0
      In.RelaDyn->addReloc(
225
0
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
226
0
    }
227
0
    if (Expr != R_TLSDESC_CALL)
228
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
229
0
    return 1;
230
0
  }
231
0
232
0
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
233
0
          Expr)) {
234
0
    // Local-Dynamic relocs can be relaxed to Local-Exec.
235
0
    if (!Config->Shared) {
236
0
      C.Relocations.push_back(
237
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
238
0
           Offset, Addend, &Sym});
239
0
      return Target->getTlsGdRelaxSkip(Type);
240
0
    }
241
0
    if (Expr == R_TLSLD_HINT)
242
0
      return 1;
243
0
    if (In.Got->addTlsIndex())
244
0
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
245
0
                           In.Got->getTlsIndexOff(), nullptr);
246
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
247
0
    return 1;
248
0
  }
249
0
250
0
  // Local-Dynamic relocs can be relaxed to Local-Exec.
251
0
  if (Expr == R_DTPREL && !Config->Shared) {
252
0
    C.Relocations.push_back(
253
0
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
254
0
         Offset, Addend, &Sym});
255
0
    return 1;
256
0
  }
257
0
258
0
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
259
0
  // thread pointer is stored in the got.
260
0
  if (Expr == R_TLSLD_GOT_OFF) {
261
0
    // Local-Dynamic relocs can be relaxed to local-exec
262
0
    if (!Config->Shared) {
263
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
264
0
      return 1;
265
0
    }
266
0
    if (!Sym.isInGot()) {
267
0
      In.Got->addEntry(Sym);
268
0
      uint64_t Off = Sym.getGotOffset();
269
0
      In.Got->Relocations.push_back(
270
0
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
271
0
    }
272
0
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
273
0
    return 1;
274
0
  }
275
0
276
0
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD_GOT,
277
0
            R_TLSGD_GOTPLT, R_TLSGD_PC>(Expr)) {
278
0
    if (Config->Shared) {
279
0
      if (In.Got->addDynTlsEntry(Sym)) {
280
0
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
281
0
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
282
0
283
0
        // If the symbol is preemptible we need the dynamic linker to write
284
0
        // the offset too.
285
0
        uint64_t OffsetOff = Off + Config->Wordsize;
286
0
        if (Sym.IsPreemptible)
287
0
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
288
0
        else
289
0
          In.Got->Relocations.push_back(
290
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
291
0
      }
292
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
293
0
      return 1;
294
0
    }
295
0
296
0
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
297
0
    // depending on the symbol being locally defined or not.
298
0
    if (Sym.IsPreemptible) {
299
0
      C.Relocations.push_back(
300
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
301
0
           Offset, Addend, &Sym});
302
0
      if (!Sym.isInGot()) {
303
0
        In.Got->addEntry(Sym);
304
0
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
305
0
                             &Sym);
306
0
      }
307
0
    } else {
308
0
      C.Relocations.push_back(
309
0
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
310
0
           Offset, Addend, &Sym});
311
0
    }
312
0
    return Target->getTlsGdRelaxSkip(Type);
313
0
  }
314
0
315
0
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
316
0
  // defined.
317
0
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
318
0
            R_TLSIE_HINT>(Expr) &&
319
0
      !Config->Shared && !Sym.IsPreemptible) {
320
0
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
321
0
    return 1;
322
0
  }
323
0
324
0
  if (Expr == R_TLSIE_HINT)
325
0
    return 1;
326
0
  return 0;
327
0
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)1, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)1, true>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
211
2.40k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
212
2.40k
  if (!Sym.isTls())
213
2.14k
    return 0;
214
259
215
259
  if (Config->EMachine == EM_ARM)
216
0
    return handleARMTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
217
259
  if (Config->EMachine == EM_MIPS)
218
0
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
219
259
220
259
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
221
259
      
Config->Shared34
) {
222
21
    if (In.Got->addDynTlsEntry(Sym)) {
223
5
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
224
5
      In.RelaDyn->addReloc(
225
5
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
226
5
    }
227
21
    if (Expr != R_TLSDESC_CALL)
228
15
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
229
21
    return 1;
230
21
  }
231
238
232
238
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
233
238
          Expr)) {
234
26
    // Local-Dynamic relocs can be relaxed to Local-Exec.
235
26
    if (!Config->Shared) {
236
8
      C.Relocations.push_back(
237
8
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
238
8
           Offset, Addend, &Sym});
239
8
      return Target->getTlsGdRelaxSkip(Type);
240
8
    }
241
18
    if (Expr == R_TLSLD_HINT)
242
4
      return 1;
243
14
    if (In.Got->addTlsIndex())
244
5
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
245
5
                           In.Got->getTlsIndexOff(), nullptr);
246
14
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
247
14
    return 1;
248
14
  }
249
212
250
212
  // Local-Dynamic relocs can be relaxed to Local-Exec.
251
212
  if (Expr == R_DTPREL && 
!Config->Shared34
) {
252
7
    C.Relocations.push_back(
253
7
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
254
7
         Offset, Addend, &Sym});
255
7
    return 1;
256
7
  }
257
205
258
205
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
259
205
  // thread pointer is stored in the got.
260
205
  if (Expr == R_TLSLD_GOT_OFF) {
261
4
    // Local-Dynamic relocs can be relaxed to local-exec
262
4
    if (!Config->Shared) {
263
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
264
0
      return 1;
265
0
    }
266
4
    if (!Sym.isInGot()) {
267
1
      In.Got->addEntry(Sym);
268
1
      uint64_t Off = Sym.getGotOffset();
269
1
      In.Got->Relocations.push_back(
270
1
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
271
1
    }
272
4
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
273
4
    return 1;
274
4
  }
275
201
276
201
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD_GOT,
277
201
            R_TLSGD_GOTPLT, R_TLSGD_PC>(Expr)) {
278
46
    if (Config->Shared) {
279
10
      if (In.Got->addDynTlsEntry(Sym)) {
280
8
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
281
8
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
282
8
283
8
        // If the symbol is preemptible we need the dynamic linker to write
284
8
        // the offset too.
285
8
        uint64_t OffsetOff = Off + Config->Wordsize;
286
8
        if (Sym.IsPreemptible)
287
5
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
288
3
        else
289
3
          In.Got->Relocations.push_back(
290
3
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
291
8
      }
292
10
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
293
10
      return 1;
294
10
    }
295
36
296
36
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
297
36
    // depending on the symbol being locally defined or not.
298
36
    if (Sym.IsPreemptible) {
299
19
      C.Relocations.push_back(
300
19
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
301
19
           Offset, Addend, &Sym});
302
19
      if (!Sym.isInGot()) {
303
8
        In.Got->addEntry(Sym);
304
8
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
305
8
                             &Sym);
306
8
      }
307
19
    } else {
308
17
      C.Relocations.push_back(
309
17
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
310
17
           Offset, Addend, &Sym});
311
17
    }
312
36
    return Target->getTlsGdRelaxSkip(Type);
313
36
  }
314
155
315
155
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
316
155
  // defined.
317
155
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
318
155
            R_TLSIE_HINT>(Expr) &&
319
155
      
!Config->Shared90
&&
!Sym.IsPreemptible83
) {
320
71
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
321
71
    return 1;
322
71
  }
323
84
324
84
  if (Expr == R_TLSIE_HINT)
325
1
    return 1;
326
83
  return 0;
327
83
}
Relocations.cpp:unsigned int handleTlsRelocation<llvm::object::ELFType<(llvm::support::endianness)0, true> >(unsigned int, lld::elf::Symbol&, lld::elf::InputSectionBase&, llvm::object::ELFType<(llvm::support::endianness)0, true>::uint, long long, lld::elf::RelExpr)
Line
Count
Source
211
10.4k
                    typename ELFT::uint Offset, int64_t Addend, RelExpr Expr) {
212
10.4k
  if (!Sym.isTls())
213
10.3k
    return 0;
214
101
215
101
  if (Config->EMachine == EM_ARM)
216
0
    return handleARMTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
217
101
  if (Config->EMachine == EM_MIPS)
218
13
    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
219
88
220
88
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
221
88
      
Config->Shared6
) {
222
1
    if (In.Got->addDynTlsEntry(Sym)) {
223
0
      uint64_t Off = In.Got->getGlobalDynOffset(Sym);
224
0
      In.RelaDyn->addReloc(
225
0
          {Target->TlsDescRel, In.Got, Off, !Sym.IsPreemptible, &Sym, 0});
226
0
    }
227
1
    if (Expr != R_TLSDESC_CALL)
228
0
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
229
1
    return 1;
230
1
  }
231
87
232
87
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
233
87
          Expr)) {
234
17
    // Local-Dynamic relocs can be relaxed to Local-Exec.
235
17
    if (!Config->Shared) {
236
3
      C.Relocations.push_back(
237
3
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
238
3
           Offset, Addend, &Sym});
239
3
      return Target->getTlsGdRelaxSkip(Type);
240
3
    }
241
14
    if (Expr == R_TLSLD_HINT)
242
4
      return 1;
243
10
    if (In.Got->addTlsIndex())
244
2
      In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got,
245
2
                           In.Got->getTlsIndexOff(), nullptr);
246
10
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
247
10
    return 1;
248
10
  }
249
70
250
70
  // Local-Dynamic relocs can be relaxed to Local-Exec.
251
70
  if (Expr == R_DTPREL && 
!Config->Shared14
) {
252
2
    C.Relocations.push_back(
253
2
        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
254
2
         Offset, Addend, &Sym});
255
2
    return 1;
256
2
  }
257
68
258
68
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
259
68
  // thread pointer is stored in the got.
260
68
  if (Expr == R_TLSLD_GOT_OFF) {
261
4
    // Local-Dynamic relocs can be relaxed to local-exec
262
4
    if (!Config->Shared) {
263
0
      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
264
0
      return 1;
265
0
    }
266
4
    if (!Sym.isInGot()) {
267
1
      In.Got->addEntry(Sym);
268
1
      uint64_t Off = Sym.getGotOffset();
269
1
      In.Got->Relocations.push_back(
270
1
          {R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
271
1
    }
272
4
    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
273
4
    return 1;
274
4
  }
275
64
276
64
  if (oneof<R_TLSDESC, R_AARCH64_TLSDESC_PAGE, R_TLSDESC_CALL, R_TLSGD_GOT,
277
64
            R_TLSGD_GOTPLT, R_TLSGD_PC>(Expr)) {
278
18
    if (Config->Shared) {
279
4
      if (In.Got->addDynTlsEntry(Sym)) {
280
3
        uint64_t Off = In.Got->getGlobalDynOffset(Sym);
281
3
        In.RelaDyn->addReloc(Target->TlsModuleIndexRel, In.Got, Off, &Sym);
282
3
283
3
        // If the symbol is preemptible we need the dynamic linker to write
284
3
        // the offset too.
285
3
        uint64_t OffsetOff = Off + Config->Wordsize;
286
3
        if (Sym.IsPreemptible)
287
3
          In.RelaDyn->addReloc(Target->TlsOffsetRel, In.Got, OffsetOff, &Sym);
288
0
        else
289
0
          In.Got->Relocations.push_back(
290
0
              {R_ABS, Target->TlsOffsetRel, OffsetOff, 0, &Sym});
291
3
      }
292
4
      C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
293
4
      return 1;
294
4
    }
295
14
296
14
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
297
14
    // depending on the symbol being locally defined or not.
298
14
    if (Sym.IsPreemptible) {
299
9
      C.Relocations.push_back(
300
9
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_IE), Type,
301
9
           Offset, Addend, &Sym});
302
9
      if (!Sym.isInGot()) {
303
2
        In.Got->addEntry(Sym);
304
2
        In.RelaDyn->addReloc(Target->TlsGotRel, In.Got, Sym.getGotOffset(),
305
2
                             &Sym);
306
2
      }
307
9
    } else {
308
5
      C.Relocations.push_back(
309
5
          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_GD_TO_LE), Type,
310
5
           Offset, Addend, &Sym});
311
5
    }
312
14
    return Target->getTlsGdRelaxSkip(Type);
313
14
  }
314
46
315
46
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
316
46
  // defined.
317
46
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
318
46
            R_TLSIE_HINT>(Expr) &&
319
46
      
!Config->Shared34
&&
!Sym.IsPreemptible34
) {
320
29
    C.Relocations.push_back({R_RELAX_TLS_IE_TO_LE, Type, Offset, Addend, &Sym});
321
29
    return 1;
322
29
  }
323
17
324
17
  if (Expr == R_TLSIE_HINT)
325
1
    return 1;
326
16
  return 0;
327
16
}
328
329
285
static RelType getMipsPairType(RelType Type, bool IsLocal) {
330
285
  switch (Type) {
331
285
  case R_MIPS_HI16:
332
23
    return R_MIPS_LO16;
333
285
  case R_MIPS_GOT16:
334
40
    // In case of global symbol, the R_MIPS_GOT16 relocation does not
335
40
    // have a pair. Each global symbol has a unique entry in the GOT
336
40
    // and a corresponding instruction with help of the R_MIPS_GOT16
337
40
    // relocation loads an address of the symbol. In case of local
338
40
    // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold
339
40
    // the high 16 bits of the symbol's value. A paired R_MIPS_LO16
340
40
    // relocations handle low 16 bits of the address. That allows
341
40
    // to allocate only one GOT entry for every 64 KBytes of local data.
342
40
    return IsLocal ? 
R_MIPS_LO1616
:
R_MIPS_NONE24
;
343
285
  case R_MICROMIPS_GOT16:
344
4
    return IsLocal ? 
R_MICROMIPS_LO161
:
R_MIPS_NONE3
;
345
285
  case R_MIPS_PCHI16:
346
1
    return R_MIPS_PCLO16;
347
285
  case R_MICROMIPS_HI16:
348
3
    return R_MICROMIPS_LO16;
349
285
  default:
350
214
    return R_MIPS_NONE;
351
285
  }
352
285
}
353
354
// True if non-preemptable symbol always has the same value regardless of where
355
// the DSO is loaded.
356
733
static bool isAbsolute(const Symbol &Sym) {
357
733
  if (Sym.isUndefWeak())
358
9
    return true;
359
724
  if (const auto *DR = dyn_cast<Defined>(&Sym))
360
724
    return DR->Section == nullptr; // Absolute symbol.
361
0
  return false;
362
0
}
363
364
685
static bool isAbsoluteValue(const Symbol &Sym) {
365
685
  return isAbsolute(Sym) || 
Sym.isTls()656
;
366
685
}
367
368
// Returns true if Expr refers a PLT entry.
369
13.7k
static bool needsPlt(RelExpr Expr) {
370
13.7k
  return oneof<R_PLT_PC, R_PPC_CALL_PLT, R_PLT>(Expr);
371
13.7k
}
372
373
// Returns true if Expr refers a GOT entry. Note that this function
374
// returns false for TLS variables even though they need GOT, because
375
// TLS variables uses GOT differently than the regular variables.
376
13.7k
static bool needsGot(RelExpr Expr) {
377
13.7k
  return oneof<R_GOT, R_GOT_OFF, R_HEXAGON_GOT, R_MIPS_GOT_LOCAL_PAGE,
378
13.7k
               R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC,
379
13.7k
               R_GOT_PC, R_GOTPLT>(Expr);
380
13.7k
}
381
382
// True if this expression is of the form Sym - X, where X is a position in the
383
// file (PC, or GOT for example).
384
615
static bool isRelExpr(RelExpr Expr) {
385
615
  return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC_CALL,
386
615
               R_PPC64_RELAX_TOC, R_PPC_CALL_PLT, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC>(Expr);
387
615
}
388
389
// Returns true if a given relocation can be computed at link-time.
390
//
391
// For instance, we know the offset from a relocation to its target at
392
// link-time if the relocation is PC-relative and refers a
393
// non-interposable function in the same executable. This function
394
// will return true for such relocation.
395
//
396
// If this function returns false, that means we need to emit a
397
// dynamic relocation so that the relocation will be fixed at load-time.
398
static bool isStaticLinkTimeConstant(RelExpr E, RelType Type, const Symbol &Sym,
399
13.7k
                                     InputSectionBase &S, uint64_t RelOff) {
400
13.7k
  // These expressions always compute a constant
401
13.7k
  if (oneof<R_DTPREL, R_GOTPLT, R_GOT_OFF, R_HEXAGON_GOT, R_TLSLD_GOT_OFF,
402
13.7k
            R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOTREL, R_MIPS_GOT_OFF,
403
13.7k
            R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, R_MIPS_TLSGD,
404
13.7k
            R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC,
405
13.7k
            R_PLT_PC, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC, R_PPC_CALL_PLT,
406
13.7k
            R_PPC64_RELAX_TOC, R_TLSDESC_CALL, R_AARCH64_TLSDESC_PAGE, R_HINT, R_TLSLD_HINT,
407
13.7k
            R_TLSIE_HINT>(E))
408
10.8k
    return true;
409
2.87k
410
2.87k
  // These never do, except if the entire file is position dependent or if
411
2.87k
  // only the low bits are used.
412
2.87k
  if (E == R_GOT || 
E == R_PLT2.79k
||
E == R_TLSDESC2.78k
)
413
92
    return Target->usesOnlyLowPageBits(Type) || 
!Config->Pic31
;
414
2.78k
415
2.78k
  if (Sym.IsPreemptible)
416
313
    return false;
417
2.46k
  if (!Config->Pic)
418
1.87k
    return true;
419
594
420
594
  // The size of a non preemptible symbol is a constant.
421
594
  if (E == R_SIZE)
422
6
    return true;
423
588
424
588
  // For the target and the relocation, we want to know if they are
425
588
  // absolute or relative.
426
588
  bool AbsVal = isAbsoluteValue(Sym);
427
588
  bool RelE = isRelExpr(E);
428
588
  if (AbsVal && 
!RelE30
)
429
28
    return true;
430
560
  if (!AbsVal && 
RelE558
)
431
284
    return true;
432
276
  if (!AbsVal && 
!RelE274
)
433
274
    return Target->usesOnlyLowPageBits(Type);
434
2
435
2
  // Relative relocation to an absolute value. This is normally unrepresentable,
436
2
  // but if the relocation refers to a weak undefined symbol, we allow it to
437
2
  // resolve to the image base. This is a little strange, but it allows us to
438
2
  // link function calls to such symbols. Normally such a call will be guarded
439
2
  // with a comparison, which will load a zero from the GOT.
440
2
  // Another special case is MIPS _gp_disp symbol which represents offset
441
2
  // between start of a function and '_gp' value and defined as absolute just
442
2
  // to simplify the code.
443
2
  assert(AbsVal && RelE);
444
2
  if (Sym.isUndefWeak())
445
0
    return true;
446
2
447
2
  // We set the final symbols values for linker script defined symbols later.
448
2
  // They always can be computed as a link time constant.
449
2
  if (Sym.ScriptDefined)
450
1
      return true;
451
1
452
1
  error("relocation " + toString(Type) + " cannot refer to absolute symbol: " +
453
1
        toString(Sym) + getLocation(S, Sym, RelOff));
454
1
  return true;
455
1
}
456
457
2
static RelExpr toPlt(RelExpr Expr) {
458
2
  switch (Expr) {
459
2
  case R_PPC_CALL:
460
0
    return R_PPC_CALL_PLT;
461
2
  case R_PC:
462
2
    return R_PLT_PC;
463
2
  case R_ABS:
464
0
    return R_PLT;
465
2
  default:
466
0
    return Expr;
467
2
  }
468
2
}
469
470
13.2k
static RelExpr fromPlt(RelExpr Expr) {
471
13.2k
  // We decided not to use a plt. Optimize a reference to the plt to a
472
13.2k
  // reference to the symbol itself.
473
13.2k
  switch (Expr) {
474
13.2k
  case R_PLT_PC:
475
626
    return R_PC;
476
13.2k
  case R_PPC_CALL_PLT:
477
148
    return R_PPC_CALL;
478
13.2k
  case R_PLT:
479
64
    return R_ABS;
480
13.2k
  default:
481
12.4k
    return Expr;
482
13.2k
  }
483
13.2k
}
484
485
// Returns true if a given shared symbol is in a read-only segment in a DSO.
486
51
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
487
51
  using Elf_Phdr = typename ELFT::Phdr;
488
51
489
51
  // Determine if the symbol is read-only by scanning the DSO's program headers.
490
51
  const SharedFile &File = SS.getFile();
491
51
  for (const Elf_Phdr &Phdr :
492
51
       check(File.template getObj<ELFT>().program_headers()))
493
321
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO180
) &&
494
321
        
!(Phdr.p_flags & ELF::PF_W)180
&&
SS.Value >= Phdr.p_vaddr106
&&
495
321
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz106
)
496
15
      return true;
497
51
  
return false36
;
498
51
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
486
6
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
487
6
  using Elf_Phdr = typename ELFT::Phdr;
488
6
489
6
  // Determine if the symbol is read-only by scanning the DSO's program headers.
490
6
  const SharedFile &File = SS.getFile();
491
6
  for (const Elf_Phdr &Phdr :
492
6
       check(File.template getObj<ELFT>().program_headers()))
493
40
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO23
) &&
494
40
        
!(Phdr.p_flags & ELF::PF_W)23
&&
SS.Value >= Phdr.p_vaddr12
&&
495
40
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz12
)
496
1
      return true;
497
6
  
return false5
;
498
6
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
486
5
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
487
5
  using Elf_Phdr = typename ELFT::Phdr;
488
5
489
5
  // Determine if the symbol is read-only by scanning the DSO's program headers.
490
5
  const SharedFile &File = SS.getFile();
491
5
  for (const Elf_Phdr &Phdr :
492
5
       check(File.template getObj<ELFT>().program_headers()))
493
40
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO25
) &&
494
40
        
!(Phdr.p_flags & ELF::PF_W)15
&&
SS.Value >= Phdr.p_vaddr10
&&
495
40
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz10
)
496
0
      return true;
497
5
  return false;
498
5
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
486
40
template <class ELFT> static bool isReadOnly(SharedSymbol &SS) {
487
40
  using Elf_Phdr = typename ELFT::Phdr;
488
40
489
40
  // Determine if the symbol is read-only by scanning the DSO's program headers.
490
40
  const SharedFile &File = SS.getFile();
491
40
  for (const Elf_Phdr &Phdr :
492
40
       check(File.template getObj<ELFT>().program_headers()))
493
241
    if ((Phdr.p_type == ELF::PT_LOAD || 
Phdr.p_type == ELF::PT_GNU_RELRO132
) &&
494
241
        
!(Phdr.p_flags & ELF::PF_W)142
&&
SS.Value >= Phdr.p_vaddr84
&&
495
241
        
SS.Value < Phdr.p_vaddr + Phdr.p_memsz84
)
496
14
      return true;
497
40
  
return false26
;
498
40
}
Unexecuted instantiation: Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
499
500
// Returns symbols at the same offset as a given symbol, including SS itself.
501
//
502
// If two or more symbols are at the same offset, and at least one of
503
// them are copied by a copy relocation, all of them need to be copied.
504
// Otherwise, they would refer to different places at runtime.
505
template <class ELFT>
506
51
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
507
51
  using Elf_Sym = typename ELFT::Sym;
508
51
509
51
  SharedFile &File = SS.getFile();
510
51
511
51
  SmallSet<SharedSymbol *, 4> Ret;
512
159
  for (const Elf_Sym &S : File.template getGlobalELFSyms<ELFT>()) {
513
159
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
514
159
        
S.getType() == STT_TLS155
||
S.st_value != SS.Value154
)
515
88
      continue;
516
71
    StringRef Name = check(S.getName(File.getStringTable()));
517
71
    Symbol *Sym = Symtab->find(Name);
518
71
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
519
68
      Ret.insert(Alias);
520
71
  }
521
51
  return Ret;
522
51
}
Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
506
6
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
507
6
  using Elf_Sym = typename ELFT::Sym;
508
6
509
6
  SharedFile &File = SS.getFile();
510
6
511
6
  SmallSet<SharedSymbol *, 4> Ret;
512
16
  for (const Elf_Sym &S : File.template getGlobalELFSyms<ELFT>()) {
513
16
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
514
16
        S.getType() == STT_TLS || S.st_value != SS.Value)
515
10
      continue;
516
6
    StringRef Name = check(S.getName(File.getStringTable()));
517
6
    Symbol *Sym = Symtab->find(Name);
518
6
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
519
6
      Ret.insert(Alias);
520
6
  }
521
6
  return Ret;
522
6
}
Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
506
5
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
507
5
  using Elf_Sym = typename ELFT::Sym;
508
5
509
5
  SharedFile &File = SS.getFile();
510
5
511
5
  SmallSet<SharedSymbol *, 4> Ret;
512
25
  for (const Elf_Sym &S : File.template getGlobalELFSyms<ELFT>()) {
513
25
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
514
25
        S.getType() == STT_TLS || S.st_value != SS.Value)
515
20
      continue;
516
5
    StringRef Name = check(S.getName(File.getStringTable()));
517
5
    Symbol *Sym = Symtab->find(Name);
518
5
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
519
5
      Ret.insert(Alias);
520
5
  }
521
5
  return Ret;
522
5
}
Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
506
40
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
507
40
  using Elf_Sym = typename ELFT::Sym;
508
40
509
40
  SharedFile &File = SS.getFile();
510
40
511
40
  SmallSet<SharedSymbol *, 4> Ret;
512
118
  for (const Elf_Sym &S : File.template getGlobalELFSyms<ELFT>()) {
513
118
    if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
514
118
        
S.getType() == STT_TLS114
||
S.st_value != SS.Value113
)
515
58
      continue;
516
60
    StringRef Name = check(S.getName(File.getStringTable()));
517
60
    Symbol *Sym = Symtab->find(Name);
518
60
    if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
519
57
      Ret.insert(Alias);
520
60
  }
521
40
  return Ret;
522
40
}
Unexecuted instantiation: Relocations.cpp:llvm::SmallSet<lld::elf::SharedSymbol*, 4u, std::__1::less<lld::elf::SharedSymbol*> > getSymbolsAt<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
523
524
// When a symbol is copy relocated or we create a canonical plt entry, it is
525
// effectively a defined symbol. In the case of copy relocation the symbol is
526
// in .bss and in the case of a canonical plt entry it is in .plt. This function
527
// replaces the existing symbol with a Defined pointing to the appropriate
528
// location.
529
static void replaceWithDefined(Symbol &Sym, SectionBase *Sec, uint64_t Value,
530
110
                               uint64_t Size) {
531
110
  Symbol Old = Sym;
532
110
533
110
  replaceSymbol(&Sym, Defined{Sym.File, Sym.getName(), Sym.Binding, Sym.StOther,
534
110
                              Sym.Type, Value, Size, Sec});
535
110
536
110
  Sym.PltIndex = Old.PltIndex;
537
110
  Sym.GotIndex = Old.GotIndex;
538
110
  Sym.VerdefIndex = Old.VerdefIndex;
539
110
  Sym.PPC64BranchltIndex = Old.PPC64BranchltIndex;
540
110
  Sym.IsPreemptible = true;
541
110
  Sym.ExportDynamic = true;
542
110
  Sym.IsUsedInRegularObj = true;
543
110
  Sym.Used = true;
544
110
}
545
546
// Reserve space in .bss or .bss.rel.ro for copy relocation.
547
//
548
// The copy relocation is pretty much a hack. If you use a copy relocation
549
// in your program, not only the symbol name but the symbol's size, RW/RO
550
// bit and alignment become part of the ABI. In addition to that, if the
551
// symbol has aliases, the aliases become part of the ABI. That's subtle,
552
// but if you violate that implicit ABI, that can cause very counter-
553
// intuitive consequences.
554
//
555
// So, what is the copy relocation? It's for linking non-position
556
// independent code to DSOs. In an ideal world, all references to data
557
// exported by DSOs should go indirectly through GOT. But if object files
558
// are compiled as non-PIC, all data references are direct. There is no
559
// way for the linker to transform the code to use GOT, as machine
560
// instructions are already set in stone in object files. This is where
561
// the copy relocation takes a role.
562
//
563
// A copy relocation instructs the dynamic linker to copy data from a DSO
564
// to a specified address (which is usually in .bss) at load-time. If the
565
// static linker (that's us) finds a direct data reference to a DSO
566
// symbol, it creates a copy relocation, so that the symbol can be
567
// resolved as if it were in .bss rather than in a DSO.
568
//
569
// As you can see in this function, we create a copy relocation for the
570
// dynamic linker, and the relocation contains not only symbol name but
571
// various other informtion about the symbol. So, such attributes become a
572
// part of the ABI.
573
//
574
// Note for application developers: I can give you a piece of advice if
575
// you are writing a shared library. You probably should export only
576
// functions from your library. You shouldn't export variables.
577
//
578
// As an example what can happen when you export variables without knowing
579
// the semantics of copy relocations, assume that you have an exported
580
// variable of type T. It is an ABI-breaking change to add new members at
581
// end of T even though doing that doesn't change the layout of the
582
// existing members. That's because the space for the new members are not
583
// reserved in .bss unless you recompile the main program. That means they
584
// are likely to overlap with other data that happens to be laid out next
585
// to the variable in .bss. This kind of issue is sometimes very hard to
586
// debug. What's a solution? Instead of exporting a varaible V from a DSO,
587
// define an accessor getV().
588
51
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
589
51
  // Copy relocation against zero-sized symbol doesn't make sense.
590
51
  uint64_t SymSize = SS.getSize();
591
51
  if (SymSize == 0 || SS.Alignment == 0)
592
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
593
51
594
51
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
595
51
  // memory protection by reserving space in the .bss.rel.ro section.
596
51
  bool IsReadOnly = isReadOnly<ELFT>(SS);
597
51
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"15
:
".bss"36
,
598
51
                                     SymSize, SS.Alignment);
599
51
  if (IsReadOnly)
600
15
    In.BssRelRo->getParent()->addSection(Sec);
601
36
  else
602
36
    In.Bss->getParent()->addSection(Sec);
603
51
604
51
  // Look through the DSO's dynamic symbol table for aliases and create a
605
51
  // dynamic symbol for each one. This causes the copy relocation to correctly
606
51
  // interpose any aliases.
607
51
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
608
67
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
609
51
610
51
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
611
51
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
588
6
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
589
6
  // Copy relocation against zero-sized symbol doesn't make sense.
590
6
  uint64_t SymSize = SS.getSize();
591
6
  if (SymSize == 0 || SS.Alignment == 0)
592
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
593
6
594
6
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
595
6
  // memory protection by reserving space in the .bss.rel.ro section.
596
6
  bool IsReadOnly = isReadOnly<ELFT>(SS);
597
6
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"1
:
".bss"5
,
598
6
                                     SymSize, SS.Alignment);
599
6
  if (IsReadOnly)
600
1
    In.BssRelRo->getParent()->addSection(Sec);
601
5
  else
602
5
    In.Bss->getParent()->addSection(Sec);
603
6
604
6
  // Look through the DSO's dynamic symbol table for aliases and create a
605
6
  // dynamic symbol for each one. This causes the copy relocation to correctly
606
6
  // interpose any aliases.
607
6
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
608
6
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
609
6
610
6
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
611
6
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
588
5
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
589
5
  // Copy relocation against zero-sized symbol doesn't make sense.
590
5
  uint64_t SymSize = SS.getSize();
591
5
  if (SymSize == 0 || SS.Alignment == 0)
592
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
593
5
594
5
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
595
5
  // memory protection by reserving space in the .bss.rel.ro section.
596
5
  bool IsReadOnly = isReadOnly<ELFT>(SS);
597
5
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"0
: ".bss",
598
5
                                     SymSize, SS.Alignment);
599
5
  if (IsReadOnly)
600
0
    In.BssRelRo->getParent()->addSection(Sec);
601
5
  else
602
5
    In.Bss->getParent()->addSection(Sec);
603
5
604
5
  // Look through the DSO's dynamic symbol table for aliases and create a
605
5
  // dynamic symbol for each one. This causes the copy relocation to correctly
606
5
  // interpose any aliases.
607
5
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
608
5
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
609
5
610
5
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
611
5
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
588
40
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
589
40
  // Copy relocation against zero-sized symbol doesn't make sense.
590
40
  uint64_t SymSize = SS.getSize();
591
40
  if (SymSize == 0 || SS.Alignment == 0)
592
0
    fatal("cannot create a copy relocation for symbol " + toString(SS));
593
40
594
40
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
595
40
  // memory protection by reserving space in the .bss.rel.ro section.
596
40
  bool IsReadOnly = isReadOnly<ELFT>(SS);
597
40
  BssSection *Sec = make<BssSection>(IsReadOnly ? 
".bss.rel.ro"14
:
".bss"26
,
598
40
                                     SymSize, SS.Alignment);
599
40
  if (IsReadOnly)
600
14
    In.BssRelRo->getParent()->addSection(Sec);
601
26
  else
602
26
    In.Bss->getParent()->addSection(Sec);
603
40
604
40
  // Look through the DSO's dynamic symbol table for aliases and create a
605
40
  // dynamic symbol for each one. This causes the copy relocation to correctly
606
40
  // interpose any aliases.
607
40
  for (SharedSymbol *Sym : getSymbolsAt<ELFT>(SS))
608
56
    replaceWithDefined(*Sym, Sec, 0, Sym->Size);
609
40
610
40
  In.RelaDyn->addReloc(Target->CopyRel, Sec, 0, &SS);
611
40
}
Unexecuted instantiation: Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
612
613
// MIPS has an odd notion of "paired" relocations to calculate addends.
614
// For example, if a relocation is of R_MIPS_HI16, there must be a
615
// R_MIPS_LO16 relocation after that, and an addend is calculated using
616
// the two relocations.
617
template <class ELFT, class RelTy>
618
static int64_t computeMipsAddend(const RelTy &Rel, const RelTy *End,
619
                                 InputSectionBase &Sec, RelExpr Expr,
620
10.3k
                                 bool IsLocal) {
621
10.3k
  if (Expr == R_MIPS_GOTREL && 
IsLocal22
)
622
9
    return Sec.getFile<ELFT>()->MipsGp0;
623
10.3k
624
10.3k
  // The ABI says that the paired relocation is used only for REL.
625
10.3k
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
626
10.3k
  if (RelTy::IsRela)
627
10.0k
    return 0;
628
285
629
285
  RelType Type = Rel.getType(Config->IsMips64EL);
630
285
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
631
285
  if (PairTy == R_MIPS_NONE)
632
241
    return 0;
633
44
634
44
  const uint8_t *Buf = Sec.data().data();
635
44
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
636
44
637
44
  // To make things worse, paired relocations might not be contiguous in
638
44
  // the relocation table, so we need to do linear search. *sigh*
639
89
  for (const RelTy *RI = &Rel; RI != End; 
++RI45
)
640
88
    if (RI->getType(Config->IsMips64EL) == PairTy &&
641
88
        
RI->getSymbol(Config->IsMips64EL) == SymIndex43
)
642
43
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
643
44
644
44
  warn("can't find matching " + toString(PairTy) + " relocation for " +
645
1
       toString(Type));
646
1
  return 0;
647
44
}
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
620
21
                                 bool IsLocal) {
621
21
  if (Expr == R_MIPS_GOTREL && 
IsLocal1
)
622
0
    return Sec.getFile<ELFT>()->MipsGp0;
623
21
624
21
  // The ABI says that the paired relocation is used only for REL.
625
21
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
626
21
  if (RelTy::IsRela)
627
0
    return 0;
628
21
629
21
  RelType Type = Rel.getType(Config->IsMips64EL);
630
21
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
631
21
  if (PairTy == R_MIPS_NONE)
632
20
    return 0;
633
1
634
1
  const uint8_t *Buf = Sec.data().data();
635
1
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
636
1
637
1
  // To make things worse, paired relocations might not be contiguous in
638
1
  // the relocation table, so we need to do linear search. *sigh*
639
2
  for (const RelTy *RI = &Rel; RI != End; 
++RI1
)
640
2
    if (RI->getType(Config->IsMips64EL) == PairTy &&
641
2
        
RI->getSymbol(Config->IsMips64EL) == SymIndex1
)
642
1
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
643
1
644
1
  warn("can't find matching " + toString(PairTy) + " relocation for " +
645
0
       toString(Type));
646
0
  return 0;
647
1
}
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
620
5
                                 bool IsLocal) {
621
5
  if (Expr == R_MIPS_GOTREL && 
IsLocal3
)
622
1
    return Sec.getFile<ELFT>()->MipsGp0;
623
4
624
4
  // The ABI says that the paired relocation is used only for REL.
625
4
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
626
4
  if (RelTy::IsRela)
627
4
    return 0;
628
0
629
0
  RelType Type = Rel.getType(Config->IsMips64EL);
630
0
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
631
0
  if (PairTy == R_MIPS_NONE)
632
0
    return 0;
633
0
634
0
  const uint8_t *Buf = Sec.data().data();
635
0
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
636
0
637
0
  // To make things worse, paired relocations might not be contiguous in
638
0
  // the relocation table, so we need to do linear search. *sigh*
639
0
  for (const RelTy *RI = &Rel; RI != End; ++RI)
640
0
    if (RI->getType(Config->IsMips64EL) == PairTy &&
641
0
        RI->getSymbol(Config->IsMips64EL) == SymIndex)
642
0
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
643
0
644
0
  warn("can't find matching " + toString(PairTy) + " relocation for " +
645
0
       toString(Type));
646
0
  return 0;
647
0
}
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
620
271
                                 bool IsLocal) {
621
271
  if (Expr == R_MIPS_GOTREL && 
IsLocal8
)
622
7
    return Sec.getFile<ELFT>()->MipsGp0;
623
264
624
264
  // The ABI says that the paired relocation is used only for REL.
625
264
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
626
264
  if (RelTy::IsRela)
627
0
    return 0;
628
264
629
264
  RelType Type = Rel.getType(Config->IsMips64EL);
630
264
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
631
264
  if (PairTy == R_MIPS_NONE)
632
221
    return 0;
633
43
634
43
  const uint8_t *Buf = Sec.data().data();
635
43
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
636
43
637
43
  // To make things worse, paired relocations might not be contiguous in
638
43
  // the relocation table, so we need to do linear search. *sigh*
639
87
  for (const RelTy *RI = &Rel; RI != End; 
++RI44
)
640
86
    if (RI->getType(Config->IsMips64EL) == PairTy &&
641
86
        
RI->getSymbol(Config->IsMips64EL) == SymIndex42
)
642
42
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
643
43
644
43
  warn("can't find matching " + toString(PairTy) + " relocation for " +
645
1
       toString(Type));
646
1
  return 0;
647
43
}
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
620
10.0k
                                 bool IsLocal) {
621
10.0k
  if (Expr == R_MIPS_GOTREL && 
IsLocal10
)
622
1
    return Sec.getFile<ELFT>()->MipsGp0;
623
10.0k
624
10.0k
  // The ABI says that the paired relocation is used only for REL.
625
10.0k
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
626
10.0k
  if (RelTy::IsRela)
627
10.0k
    return 0;
628
0
629
0
  RelType Type = Rel.getType(Config->IsMips64EL);
630
0
  uint32_t PairTy = getMipsPairType(Type, IsLocal);
631
0
  if (PairTy == R_MIPS_NONE)
632
0
    return 0;
633
0
634
0
  const uint8_t *Buf = Sec.data().data();
635
0
  uint32_t SymIndex = Rel.getSymbol(Config->IsMips64EL);
636
0
637
0
  // To make things worse, paired relocations might not be contiguous in
638
0
  // the relocation table, so we need to do linear search. *sigh*
639
0
  for (const RelTy *RI = &Rel; RI != End; ++RI)
640
0
    if (RI->getType(Config->IsMips64EL) == PairTy &&
641
0
        RI->getSymbol(Config->IsMips64EL) == SymIndex)
642
0
      return Target->getImplicitAddend(Buf + RI->r_offset, PairTy);
643
0
644
0
  warn("can't find matching " + toString(PairTy) + " relocation for " +
645
0
       toString(Type));
646
0
  return 0;
647
0
}
Unexecuted instantiation: Relocations.cpp:long long computeMipsAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
648
649
// Returns an addend of a given relocation. If it is RELA, an addend
650
// is in a relocation itself. If it is REL, we need to read it from an
651
// input section.
652
template <class ELFT, class RelTy>
653
static int64_t computeAddend(const RelTy &Rel, const RelTy *End,
654
                             InputSectionBase &Sec, RelExpr Expr,
655
14.0k
                             bool IsLocal) {
656
14.0k
  int64_t Addend;
657
14.0k
  RelType Type = Rel.getType(Config->IsMips64EL);
658
14.0k
659
14.0k
  if (RelTy::IsRela) {
660
12.9k
    Addend = getAddend<ELFT>(Rel);
661
12.9k
  } else {
662
1.10k
    const uint8_t *Buf = Sec.data().data();
663
1.10k
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
1.10k
  }
665
14.0k
666
14.0k
  if (Config->EMachine == EM_PPC64 && 
Config->Pic989
&&
Type == R_PPC64_TOC238
)
667
0
    Addend += getPPC64TocBase();
668
14.0k
  if (Config->EMachine == EM_MIPS)
669
10.3k
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
14.0k
671
14.0k
  return Addend;
672
14.0k
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
655
108
                             bool IsLocal) {
656
108
  int64_t Addend;
657
108
  RelType Type = Rel.getType(Config->IsMips64EL);
658
108
659
108
  if (RelTy::IsRela) {
660
108
    Addend = getAddend<ELFT>(Rel);
661
108
  } else {
662
0
    const uint8_t *Buf = Sec.data().data();
663
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
0
  }
665
108
666
108
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
667
0
    Addend += getPPC64TocBase();
668
108
  if (Config->EMachine == EM_MIPS)
669
0
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
108
671
108
  return Addend;
672
108
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
655
836
                             bool IsLocal) {
656
836
  int64_t Addend;
657
836
  RelType Type = Rel.getType(Config->IsMips64EL);
658
836
659
836
  if (RelTy::IsRela) {
660
0
    Addend = getAddend<ELFT>(Rel);
661
836
  } else {
662
836
    const uint8_t *Buf = Sec.data().data();
663
836
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
836
  }
665
836
666
836
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
667
0
    Addend += getPPC64TocBase();
668
836
  if (Config->EMachine == EM_MIPS)
669
21
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
836
671
836
  return Addend;
672
836
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
655
14
                             bool IsLocal) {
656
14
  int64_t Addend;
657
14
  RelType Type = Rel.getType(Config->IsMips64EL);
658
14
659
14
  if (RelTy::IsRela) {
660
14
    Addend = getAddend<ELFT>(Rel);
661
14
  } else {
662
0
    const uint8_t *Buf = Sec.data().data();
663
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
0
  }
665
14
666
14
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
667
0
    Addend += getPPC64TocBase();
668
14
  if (Config->EMachine == EM_MIPS)
669
5
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
14
671
14
  return Addend;
672
14
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
655
271
                             bool IsLocal) {
656
271
  int64_t Addend;
657
271
  RelType Type = Rel.getType(Config->IsMips64EL);
658
271
659
271
  if (RelTy::IsRela) {
660
0
    Addend = getAddend<ELFT>(Rel);
661
271
  } else {
662
271
    const uint8_t *Buf = Sec.data().data();
663
271
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
271
  }
665
271
666
271
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
667
0
    Addend += getPPC64TocBase();
668
271
  if (Config->EMachine == EM_MIPS)
669
271
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
271
671
271
  return Addend;
672
271
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
655
2.40k
                             bool IsLocal) {
656
2.40k
  int64_t Addend;
657
2.40k
  RelType Type = Rel.getType(Config->IsMips64EL);
658
2.40k
659
2.40k
  if (RelTy::IsRela) {
660
2.40k
    Addend = getAddend<ELFT>(Rel);
661
2.40k
  } else {
662
0
    const uint8_t *Buf = Sec.data().data();
663
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
0
  }
665
2.40k
666
2.40k
  if (Config->EMachine == EM_PPC64 && 
Config->Pic624
&&
Type == R_PPC64_TOC129
)
667
0
    Addend += getPPC64TocBase();
668
2.40k
  if (Config->EMachine == EM_MIPS)
669
0
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
2.40k
671
2.40k
  return Addend;
672
2.40k
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
655
2
                             bool IsLocal) {
656
2
  int64_t Addend;
657
2
  RelType Type = Rel.getType(Config->IsMips64EL);
658
2
659
2
  if (RelTy::IsRela) {
660
0
    Addend = getAddend<ELFT>(Rel);
661
2
  } else {
662
2
    const uint8_t *Buf = Sec.data().data();
663
2
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
2
  }
665
2
666
2
  if (Config->EMachine == EM_PPC64 && 
Config->Pic0
&&
Type == R_PPC64_TOC0
)
667
0
    Addend += getPPC64TocBase();
668
2
  if (Config->EMachine == EM_MIPS)
669
0
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
2
671
2
  return Addend;
672
2
}
Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
Line
Count
Source
655
10.4k
                             bool IsLocal) {
656
10.4k
  int64_t Addend;
657
10.4k
  RelType Type = Rel.getType(Config->IsMips64EL);
658
10.4k
659
10.4k
  if (RelTy::IsRela) {
660
10.4k
    Addend = getAddend<ELFT>(Rel);
661
10.4k
  } else {
662
0
    const uint8_t *Buf = Sec.data().data();
663
0
    Addend = Target->getImplicitAddend(Buf + Rel.r_offset, Type);
664
0
  }
665
10.4k
666
10.4k
  if (Config->EMachine == EM_PPC64 && 
Config->Pic365
&&
Type == R_PPC64_TOC109
)
667
0
    Addend += getPPC64TocBase();
668
10.4k
  if (Config->EMachine == EM_MIPS)
669
10.0k
    Addend += computeMipsAddend<ELFT>(Rel, End, Sec, Expr, IsLocal);
670
10.4k
671
10.4k
  return Addend;
672
10.4k
}
Unexecuted instantiation: Relocations.cpp:long long computeAddend<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*, lld::elf::InputSectionBase&, lld::elf::RelExpr, bool)
673
674
// Report an undefined symbol if necessary.
675
// Returns true if this function printed out an error message.
676
static bool maybeReportUndefined(Symbol &Sym, InputSectionBase &Sec,
677
14.1k
                                 uint64_t Offset) {
678
14.1k
  if (Sym.isLocal() || 
!Sym.isUndefined()13.1k
||
Sym.isWeak()386
)
679
13.8k
    return false;
680
304
681
304
  bool CanBeExternal =
682
304
      Sym.computeBinding() != STB_LOCAL && 
Sym.Visibility == STV_DEFAULT294
;
683
304
  if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && 
CanBeExternal223
)
684
215
    return false;
685
89
686
89
  std::string Msg = "undefined ";
687
89
  if (Sym.Visibility == STV_INTERNAL)
688
1
    Msg += "internal ";
689
88
  else if (Sym.Visibility == STV_HIDDEN)
690
9
    Msg += "hidden ";
691
79
  else if (Sym.Visibility == STV_PROTECTED)
692
7
    Msg += "protected ";
693
89
  Msg += "symbol: " + toString(Sym) + "\n>>> referenced by ";
694
89
695
89
  std::string Src = Sec.getSrcMsg(Sym, Offset);
696
89
  if (!Src.empty())
697
33
    Msg += Src + "\n>>>               ";
698
89
  Msg += Sec.getObjMsg(Offset);
699
89
700
89
  if (Sym.getName().startswith("_ZTV"))
701
3
    Msg += "\nthe vtable symbol may be undefined because the class is missing "
702
3
           "its key function (see https://lld.llvm.org/missingkeyfunction)";
703
89
704
89
  if ((Config->UnresolvedSymbols == UnresolvedPolicy::Warn && 
CanBeExternal5
) ||
705
89
      
Config->NoinhibitExec86
) {
706
10
    warn(Msg);
707
10
    return false;
708
10
  }
709
79
710
79
  error(Msg);
711
79
  return true;
712
79
}
713
714
// MIPS N32 ABI treats series of successive relocations with the same offset
715
// as a single relocation. The similar approach used by N64 ABI, but this ABI
716
// packs all relocations into the single relocation record. Here we emulate
717
// this for the N32 ABI. Iterate over relocation with the same offset and put
718
// theirs types into the single bit-set.
719
5
template <class RelTy> static RelType getMipsN32RelType(RelTy *&Rel, RelTy *End) {
720
5
  RelType Type = 0;
721
5
  uint64_t Offset = Rel->r_offset;
722
5
723
5
  int N = 0;
724
14
  while (Rel != End && 
Rel->r_offset == Offset10
)
725
9
    Type |= (Rel++)->getType(Config->IsMips64EL) << (8 * N++);
726
5
  return Type;
727
5
}
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*)
Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*)
Line
Count
Source
719
5
template <class RelTy> static RelType getMipsN32RelType(RelTy *&Rel, RelTy *End) {
720
5
  RelType Type = 0;
721
5
  uint64_t Offset = Rel->r_offset;
722
5
723
5
  int N = 0;
724
14
  while (Rel != End && 
Rel->r_offset == Offset10
)
725
9
    Type |= (Rel++)->getType(Config->IsMips64EL) << (8 * N++);
726
5
  return Type;
727
5
}
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const*)
Unexecuted instantiation: Relocations.cpp:unsigned int getMipsN32RelType<llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const>(llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const*)
728
729
// .eh_frame sections are mergeable input sections, so their input
730
// offsets are not linearly mapped to output section. For each input
731
// offset, we need to find a section piece containing the offset and
732
// add the piece's base address to the input offset to compute the
733
// output offset. That isn't cheap.
734
//
735
// This class is to speed up the offset computation. When we process
736
// relocations, we access offsets in the monotonically increasing
737
// order. So we can optimize for that access pattern.
738
//
739
// For sections other than .eh_frame, this class doesn't do anything.
740
namespace {
741
class OffsetGetter {
742
public:
743
108k
  explicit OffsetGetter(InputSectionBase &Sec) {
744
108k
    if (auto *Eh = dyn_cast<EhInputSection>(&Sec))
745
115
      Pieces = Eh->Pieces;
746
108k
  }
747
748
  // Translates offsets in input sections to offsets in output sections.
749
  // Given offset must increase monotonically. We assume that Piece is
750
  // sorted by InputOff.
751
14.1k
  uint64_t get(uint64_t Off) {
752
14.1k
    if (Pieces.empty())
753
14.0k
      return Off;
754
147
755
281
    
while (147
I != Pieces.size() && Pieces[I].InputOff + Pieces[I].Size <= Off)
756
134
      ++I;
757
147
    if (I == Pieces.size())
758
0
      fatal(".eh_frame: relocation is not in any piece");
759
147
760
147
    // Pieces must be contiguous, so there must be no holes in between.
761
147
    assert(Pieces[I].InputOff <= Off && "Relocation not in any piece");
762
147
763
147
    // Offset -1 means that the piece is dead (i.e. garbage collected).
764
147
    if (Pieces[I].OutputOff == -1)
765
13
      return -1;
766
134
    return Pieces[I].OutputOff + Off - Pieces[I].InputOff;
767
134
  }
768
769
private:
770
  ArrayRef<EhSectionPiece> Pieces;
771
  size_t I = 0;
772
};
773
} // namespace
774
775
static void addRelativeReloc(InputSectionBase *IS, uint64_t OffsetInSec,
776
                             Symbol *Sym, int64_t Addend, RelExpr Expr,
777
275
                             RelType Type) {
778
275
  // Add a relative relocation. If RelrDyn section is enabled, and the
779
275
  // relocation offset is guaranteed to be even, add the relocation to
780
275
  // the RelrDyn section, otherwise add it to the RelaDyn section.
781
275
  // RelrDyn sections don't support odd offsets. Also, RelrDyn sections
782
275
  // don't store the addend values, so we must write it to the relocated
783
275
  // address.
784
275
  if (In.RelrDyn && 
IS->Alignment >= 284
&&
OffsetInSec % 2 == 084
) {
785
82
    IS->Relocations.push_back({Expr, Type, OffsetInSec, Addend, Sym});
786
82
    In.RelrDyn->Relocs.push_back({IS, OffsetInSec});
787
82
    return;
788
82
  }
789
193
  In.RelaDyn->addReloc(Target->RelativeRel, IS, OffsetInSec, Sym, Addend, Expr,
790
193
                       Type);
791
193
}
792
793
template <class ELFT, class GotPltSection>
794
static void addPltEntry(PltSection *Plt, GotPltSection *GotPlt,
795
381
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
381
  Plt->addEntry<ELFT>(Sym);
797
381
  GotPlt->addEntry(Sym);
798
381
  Rel->addReloc(
799
381
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
381
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
795
75
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
75
  Plt->addEntry<ELFT>(Sym);
797
75
  GotPlt->addEntry(Sym);
798
75
  Rel->addReloc(
799
75
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
75
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, false>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
795
13
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
13
  Plt->addEntry<ELFT>(Sym);
797
13
  GotPlt->addEntry(Sym);
798
13
  Rel->addReloc(
799
13
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
13
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, false>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
795
13
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
13
  Plt->addEntry<ELFT>(Sym);
797
13
  GotPlt->addEntry(Sym);
798
13
  Rel->addReloc(
799
13
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
13
}
Unexecuted instantiation: Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, false>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
795
222
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
222
  Plt->addEntry<ELFT>(Sym);
797
222
  GotPlt->addEntry(Sym);
798
222
  Rel->addReloc(
799
222
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
222
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)1, true>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
795
42
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
42
  Plt->addEntry<ELFT>(Sym);
797
42
  GotPlt->addEntry(Sym);
798
42
  Rel->addReloc(
799
42
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
42
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, true>, lld::elf::GotPltSection>(lld::elf::PltSection*, lld::elf::GotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
795
14
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
14
  Plt->addEntry<ELFT>(Sym);
797
14
  GotPlt->addEntry(Sym);
798
14
  Rel->addReloc(
799
14
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
14
}
Relocations.cpp:void addPltEntry<llvm::object::ELFType<(llvm::support::endianness)0, true>, lld::elf::IgotPltSection>(lld::elf::PltSection*, lld::elf::IgotPltSection*, lld::elf::RelocationBaseSection*, unsigned int, lld::elf::Symbol&)
Line
Count
Source
795
2
                        RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
796
2
  Plt->addEntry<ELFT>(Sym);
797
2
  GotPlt->addEntry(Sym);
798
2
  Rel->addReloc(
799
2
      {Type, GotPlt, Sym.getGotPltOffset(), !Sym.IsPreemptible, &Sym, 0});
800
2
}
801
802
153
static void addGotEntry(Symbol &Sym) {
803
153
  In.Got->addEntry(Sym);
804
153
805
153
  RelExpr Expr = Sym.isTls() ? 
R_TLS34
:
R_ABS119
;
806
153
  uint64_t Off = Sym.getGotOffset();
807
153
808
153
  // If a GOT slot value can be calculated at link-time, which is now,
809
153
  // we can just fill that out.
810
153
  //
811
153
  // (We don't actually write a value to a GOT slot right now, but we
812
153
  // add a static relocation to a Relocations vector so that
813
153
  // InputSection::relocate will do the work for us. We may be able
814
153
  // to just write a value now, but it is a TODO.)
815
153
  bool IsLinkTimeConstant =
816
153
      !Sym.IsPreemptible && 
(57
!Config->Pic57
||
isAbsolute(Sym)30
);
817
153
  if (IsLinkTimeConstant) {
818
32
    In.Got->Relocations.push_back({Expr, Target->GotRel, Off, 0, &Sym});
819
32
    return;
820
32
  }
821
121
822
121
  // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
823
121
  // the GOT slot will be fixed at load-time.
824
121
  if (!Sym.isTls() && 
!Sym.IsPreemptible89
&&
Config->Pic18
&&
!isAbsolute(Sym)18
) {
825
18
    addRelativeReloc(In.Got, Off, &Sym, 0, R_ABS, Target->GotRel);
826
18
    return;
827
18
  }
828
103
  In.RelaDyn->addReloc(Sym.isTls() ? 
Target->TlsGotRel32
:
Target->GotRel71
, In.Got,
829
103
                       Off, &Sym, 0, Sym.IsPreemptible ? 
R_ADDEND96
:
R_ABS7
,
830
103
                       Target->GotRel);
831
103
}
832
833
// Return true if we can define a symbol in the executable that
834
// contains the value/function of a symbol defined in a shared
835
// library.
836
118
static bool canDefineSymbolInExecutable(Symbol &Sym) {
837
118
  // If the symbol has default visibility the symbol defined in the
838
118
  // executable will preempt it.
839
118
  // Note that we want the visibility of the shared symbol itself, not
840
118
  // the visibility of the symbol in the output file we are producing. That is
841
118
  // why we use Sym.StOther.
842
118
  if ((Sym.StOther & 0x3) == STV_DEFAULT)
843
111
    return true;
844
7
845
7
  // If we are allowed to break address equality of functions, defining
846
7
  // a plt entry will allow the program to call the function in the
847
7
  // .so, but the .so and the executable will no agree on the address
848
7
  // of the function. Similar logic for objects.
849
7
  return ((Sym.isFunc() && 
Config->IgnoreFunctionAddressEquality3
) ||
850
7
          
(6
Sym.isObject()6
&&
Config->IgnoreDataAddressEquality2
));
851
7
}
852
853
// The reason we have to do this early scan is as follows
854
// * To mmap the output file, we need to know the size
855
// * For that, we need to know how many dynamic relocs we will have.
856
// It might be possible to avoid this by outputting the file with write:
857
// * Write the allocated output sections, computing addresses.
858
// * Apply relocations, recording which ones require a dynamic reloc.
859
// * Write the dynamic relocations.
860
// * Write the rest of the file.
861
// This would have some drawbacks. For example, we would only know if .rela.dyn
862
// is needed after applying relocations. If it is, it will go after rw and rx
863
// sections. Given that it is ro, we will need an extra PT_LOAD. This
864
// complicates things for the dynamic linker and means we would have to reserve
865
// space for the extra PT_LOAD even if we end up not using it.
866
template <class ELFT, class RelTy>
867
static void processRelocAux(InputSectionBase &Sec, RelExpr Expr, RelType Type,
868
                            uint64_t Offset, Symbol &Sym, const RelTy &Rel,
869
13.7k
                            int64_t Addend) {
870
13.7k
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
871
13.1k
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
872
13.1k
    return;
873
13.1k
  }
874
576
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText166
;
875
576
  if (CanWrite) {
876
428
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
877
428
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT182
;
878
428
879
428
    if (!IsPreemptibleValue) {
880
255
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
881
255
      return;
882
255
    } else 
if (RelType 173
Rel173
= Target->getDynRel(Type)) {
883
155
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
884
155
885
155
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
886
155
      // While regular ABI uses dynamic relocations to fill up GOT entries
887
155
      // MIPS ABI requires dynamic linker to fills up GOT entries using
888
155
      // specially sorted dynamic symbol table. This affects even dynamic
889
155
      // relocations against symbols which do not require GOT entries
890
155
      // creation explicitly, i.e. do not have any GOT-relocations. So if
891
155
      // a preemptible symbol has a dynamic relocation we anyway have
892
155
      // to create a GOT entry for it.
893
155
      // If a non-preemptible symbol has a dynamic relocation against it,
894
155
      // dynamic linker takes it st_value, adds offset and writes down
895
155
      // result of the dynamic relocation. In case of preemptible symbol
896
155
      // dynamic linker performs symbol resolution, writes the symbol value
897
155
      // to the GOT entry and reads the GOT entry when it needs to perform
898
155
      // a dynamic relocation.
899
155
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
900
155
      if (Config->EMachine == EM_MIPS)
901
4
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
902
155
      return;
903
155
    }
904
166
  }
905
166
906
166
  // If the relocation is to a weak undef, and we are producing
907
166
  // executable, give up on it and produce a non preemptible 0.
908
166
  if (!Config->Shared && 
Sym.isUndefWeak()134
) {
909
8
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
910
8
    return;
911
8
  }
912
158
913
158
  if (!CanWrite && 
(140
Config->Pic140
&&
!isRelExpr(Expr)27
)) {
914
19
    error(
915
19
        "can't create dynamic relocation " + toString(Type) + " against " +
916
19
        (Sym.getName().empty() ? 
"local symbol"3
:
"symbol: " + toString(Sym)16
) +
917
19
        " in readonly segment; recompile object files with -fPIC "
918
19
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
919
19
        getLocation(Sec, Sym, Offset));
920
19
    return;
921
19
  }
922
139
923
139
  // Copy relocations are only possible if we are creating an executable.
924
139
  if (Config->Shared) {
925
18
    errorOrWarn("relocation " + toString(Type) +
926
18
                " cannot be used against symbol " + toString(Sym) +
927
18
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
928
18
    return;
929
18
  }
930
121
931
121
  // If the symbol is undefined we already reported any relevant errors.
932
121
  if (Sym.isUndefined())
933
3
    return;
934
118
935
118
  if (!canDefineSymbolInExecutable(Sym)) {
936
5
    error("cannot preempt symbol: " + toString(Sym) +
937
5
          getLocation(Sec, Sym, Offset));
938
5
    return;
939
5
  }
940
113
941
113
  if (Sym.isObject()) {
942
64
    // Produce a copy relocation.
943
64
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
944
51
      if (!Config->ZCopyreloc)
945
3
        error("unresolvable relocation " + toString(Type) +
946
3
              " against symbol '" + toString(*SS) +
947
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
948
3
              getLocation(Sec, Sym, Offset));
949
51
      addCopyRelSymbol<ELFT>(*SS);
950
51
    }
951
64
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
952
64
    return;
953
64
  }
954
49
955
49
  if (Sym.isFunc()) {
956
46
    // This handles a non PIC program call to function in a shared library. In
957
46
    // an ideal world, we could just report an error saying the relocation can
958
46
    // overflow at runtime. In the real world with glibc, crt1.o has a
959
46
    // R_X86_64_PC32 pointing to libc.so.
960
46
    //
961
46
    // The general idea on how to handle such cases is to create a PLT entry and
962
46
    // use that as the function value.
963
46
    //
964
46
    // For the static linking part, we just return a plt expr and everything
965
46
    // else will use the PLT entry as the address.
966
46
    //
967
46
    // The remaining problem is making sure pointer equality still works. We
968
46
    // need the help of the dynamic linker for that. We let it know that we have
969
46
    // a direct reference to a so symbol by creating an undefined symbol with a
970
46
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
971
46
    // the value of the symbol we created. This is true even for got entries, so
972
46
    // pointer equality is maintained. To avoid an infinite loop, the only entry
973
46
    // that points to the real function is a dedicated got entry used by the
974
46
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
975
46
    // R_386_JMP_SLOT, etc).
976
46
977
46
    // For position independent executable on i386, the plt entry requires ebx
978
46
    // to be set. This causes two problems:
979
46
    // * If some code has a direct reference to a function, it was probably
980
46
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
981
46
    // * If a library definition gets preempted to the executable, it will have
982
46
    //   the wrong ebx value.
983
46
    if (Config->Pie && 
Config->EMachine == EM_3862
)
984
1
      errorOrWarn("symbol '" + toString(Sym) +
985
1
                  "' cannot be preempted; recompile with -fPIE" +
986
1
                  getLocation(Sec, Sym, Offset));
987
46
    if (!Sym.isInPlt())
988
42
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
989
46
    if (!Sym.isDefined())
990
43
      replaceWithDefined(
991
43
          Sym, In.Plt,
992
43
          Target->PltHeaderSize + Target->PltEntrySize * Sym.PltIndex, 0);
993
46
    Sym.NeedsPltAddr = true;
994
46
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
995
46
    return;
996
46
  }
997
3
998
3
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
999
3
              getLocation(Sec, Sym, Offset));
1000
3
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const&, long long)
Line
Count
Source
869
108
                            int64_t Addend) {
870
108
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
871
108
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
872
108
    return;
873
108
  }
874
0
  bool CanWrite = (Sec.Flags & SHF_WRITE) || !Config->ZText;
875
0
  if (CanWrite) {
876
0
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
877
0
    bool IsPreemptibleValue = Sym.IsPreemptible && Expr != R_GOT;
878
0
879
0
    if (!IsPreemptibleValue) {
880
0
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
881
0
      return;
882
0
    } else if (RelType Rel = Target->getDynRel(Type)) {
883
0
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
884
0
885
0
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
886
0
      // While regular ABI uses dynamic relocations to fill up GOT entries
887
0
      // MIPS ABI requires dynamic linker to fills up GOT entries using
888
0
      // specially sorted dynamic symbol table. This affects even dynamic
889
0
      // relocations against symbols which do not require GOT entries
890
0
      // creation explicitly, i.e. do not have any GOT-relocations. So if
891
0
      // a preemptible symbol has a dynamic relocation we anyway have
892
0
      // to create a GOT entry for it.
893
0
      // If a non-preemptible symbol has a dynamic relocation against it,
894
0
      // dynamic linker takes it st_value, adds offset and writes down
895
0
      // result of the dynamic relocation. In case of preemptible symbol
896
0
      // dynamic linker performs symbol resolution, writes the symbol value
897
0
      // to the GOT entry and reads the GOT entry when it needs to perform
898
0
      // a dynamic relocation.
899
0
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
900
0
      if (Config->EMachine == EM_MIPS)
901
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
902
0
      return;
903
0
    }
904
0
  }
905
0
906
0
  // If the relocation is to a weak undef, and we are producing
907
0
  // executable, give up on it and produce a non preemptible 0.
908
0
  if (!Config->Shared && Sym.isUndefWeak()) {
909
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
910
0
    return;
911
0
  }
912
0
913
0
  if (!CanWrite && (Config->Pic && !isRelExpr(Expr))) {
914
0
    error(
915
0
        "can't create dynamic relocation " + toString(Type) + " against " +
916
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
917
0
        " in readonly segment; recompile object files with -fPIC "
918
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
919
0
        getLocation(Sec, Sym, Offset));
920
0
    return;
921
0
  }
922
0
923
0
  // Copy relocations are only possible if we are creating an executable.
924
0
  if (Config->Shared) {
925
0
    errorOrWarn("relocation " + toString(Type) +
926
0
                " cannot be used against symbol " + toString(Sym) +
927
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
928
0
    return;
929
0
  }
930
0
931
0
  // If the symbol is undefined we already reported any relevant errors.
932
0
  if (Sym.isUndefined())
933
0
    return;
934
0
935
0
  if (!canDefineSymbolInExecutable(Sym)) {
936
0
    error("cannot preempt symbol: " + toString(Sym) +
937
0
          getLocation(Sec, Sym, Offset));
938
0
    return;
939
0
  }
940
0
941
0
  if (Sym.isObject()) {
942
0
    // Produce a copy relocation.
943
0
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
944
0
      if (!Config->ZCopyreloc)
945
0
        error("unresolvable relocation " + toString(Type) +
946
0
              " against symbol '" + toString(*SS) +
947
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
948
0
              getLocation(Sec, Sym, Offset));
949
0
      addCopyRelSymbol<ELFT>(*SS);
950
0
    }
951
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
952
0
    return;
953
0
  }
954
0
955
0
  if (Sym.isFunc()) {
956
0
    // This handles a non PIC program call to function in a shared library. In
957
0
    // an ideal world, we could just report an error saying the relocation can
958
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
959
0
    // R_X86_64_PC32 pointing to libc.so.
960
0
    //
961
0
    // The general idea on how to handle such cases is to create a PLT entry and
962
0
    // use that as the function value.
963
0
    //
964
0
    // For the static linking part, we just return a plt expr and everything
965
0
    // else will use the PLT entry as the address.
966
0
    //
967
0
    // The remaining problem is making sure pointer equality still works. We
968
0
    // need the help of the dynamic linker for that. We let it know that we have
969
0
    // a direct reference to a so symbol by creating an undefined symbol with a
970
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
971
0
    // the value of the symbol we created. This is true even for got entries, so
972
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
973
0
    // that points to the real function is a dedicated got entry used by the
974
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
975
0
    // R_386_JMP_SLOT, etc).
976
0
977
0
    // For position independent executable on i386, the plt entry requires ebx
978
0
    // to be set. This causes two problems:
979
0
    // * If some code has a direct reference to a function, it was probably
980
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
981
0
    // * If a library definition gets preempted to the executable, it will have
982
0
    //   the wrong ebx value.
983
0
    if (Config->Pie && Config->EMachine == EM_386)
984
0
      errorOrWarn("symbol '" + toString(Sym) +
985
0
                  "' cannot be preempted; recompile with -fPIE" +
986
0
                  getLocation(Sec, Sym, Offset));
987
0
    if (!Sym.isInPlt())
988
0
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
989
0
    if (!Sym.isDefined())
990
0
      replaceWithDefined(
991
0
          Sym, In.Plt,
992
0
          Target->PltHeaderSize + Target->PltEntrySize * Sym.PltIndex, 0);
993
0
    Sym.NeedsPltAddr = true;
994
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
995
0
    return;
996
0
  }
997
0
998
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
999
0
              getLocation(Sec, Sym, Offset));
1000
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const&, long long)
Line
Count
Source
869
794
                            int64_t Addend) {
870
794
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
871
619
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
872
619
    return;
873
619
  }
874
175
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText16
;
875
175
  if (CanWrite) {
876
159
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
877
159
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT44
;
878
159
879
159
    if (!IsPreemptibleValue) {
880
124
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
881
124
      return;
882
124
    } else 
if (RelType 35
Rel35
= Target->getDynRel(Type)) {
883
35
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
884
35
885
35
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
886
35
      // While regular ABI uses dynamic relocations to fill up GOT entries
887
35
      // MIPS ABI requires dynamic linker to fills up GOT entries using
888
35
      // specially sorted dynamic symbol table. This affects even dynamic
889
35
      // relocations against symbols which do not require GOT entries
890
35
      // creation explicitly, i.e. do not have any GOT-relocations. So if
891
35
      // a preemptible symbol has a dynamic relocation we anyway have
892
35
      // to create a GOT entry for it.
893
35
      // If a non-preemptible symbol has a dynamic relocation against it,
894
35
      // dynamic linker takes it st_value, adds offset and writes down
895
35
      // result of the dynamic relocation. In case of preemptible symbol
896
35
      // dynamic linker performs symbol resolution, writes the symbol value
897
35
      // to the GOT entry and reads the GOT entry when it needs to perform
898
35
      // a dynamic relocation.
899
35
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
900
35
      if (Config->EMachine == EM_MIPS)
901
1
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
902
35
      return;
903
35
    }
904
16
  }
905
16
906
16
  // If the relocation is to a weak undef, and we are producing
907
16
  // executable, give up on it and produce a non preemptible 0.
908
16
  if (!Config->Shared && 
Sym.isUndefWeak()14
) {
909
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
910
0
    return;
911
0
  }
912
16
913
16
  if (!CanWrite && (Config->Pic && 
!isRelExpr(Expr)6
)) {
914
5
    error(
915
5
        "can't create dynamic relocation " + toString(Type) + " against " +
916
5
        (Sym.getName().empty() ? 
"local symbol"0
: "symbol: " + toString(Sym)) +
917
5
        " in readonly segment; recompile object files with -fPIC "
918
5
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
919
5
        getLocation(Sec, Sym, Offset));
920
5
    return;
921
5
  }
922
11
923
11
  // Copy relocations are only possible if we are creating an executable.
924
11
  if (Config->Shared) {
925
0
    errorOrWarn("relocation " + toString(Type) +
926
0
                " cannot be used against symbol " + toString(Sym) +
927
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
928
0
    return;
929
0
  }
930
11
931
11
  // If the symbol is undefined we already reported any relevant errors.
932
11
  if (Sym.isUndefined())
933
0
    return;
934
11
935
11
  if (!canDefineSymbolInExecutable(Sym)) {
936
0
    error("cannot preempt symbol: " + toString(Sym) +
937
0
          getLocation(Sec, Sym, Offset));
938
0
    return;
939
0
  }
940
11
941
11
  if (Sym.isObject()) {
942
7
    // Produce a copy relocation.
943
7
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
944
6
      if (!Config->ZCopyreloc)
945
0
        error("unresolvable relocation " + toString(Type) +
946
0
              " against symbol '" + toString(*SS) +
947
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
948
0
              getLocation(Sec, Sym, Offset));
949
6
      addCopyRelSymbol<ELFT>(*SS);
950
6
    }
951
7
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
952
7
    return;
953
7
  }
954
4
955
4
  if (Sym.isFunc()) {
956
4
    // This handles a non PIC program call to function in a shared library. In
957
4
    // an ideal world, we could just report an error saying the relocation can
958
4
    // overflow at runtime. In the real world with glibc, crt1.o has a
959
4
    // R_X86_64_PC32 pointing to libc.so.
960
4
    //
961
4
    // The general idea on how to handle such cases is to create a PLT entry and
962
4
    // use that as the function value.
963
4
    //
964
4
    // For the static linking part, we just return a plt expr and everything
965
4
    // else will use the PLT entry as the address.
966
4
    //
967
4
    // The remaining problem is making sure pointer equality still works. We
968
4
    // need the help of the dynamic linker for that. We let it know that we have
969
4
    // a direct reference to a so symbol by creating an undefined symbol with a
970
4
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
971
4
    // the value of the symbol we created. This is true even for got entries, so
972
4
    // pointer equality is maintained. To avoid an infinite loop, the only entry
973
4
    // that points to the real function is a dedicated got entry used by the
974
4
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
975
4
    // R_386_JMP_SLOT, etc).
976
4
977
4
    // For position independent executable on i386, the plt entry requires ebx
978
4
    // to be set. This causes two problems:
979
4
    // * If some code has a direct reference to a function, it was probably
980
4
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
981
4
    // * If a library definition gets preempted to the executable, it will have
982
4
    //   the wrong ebx value.
983
4
    if (Config->Pie && 
Config->EMachine == EM_3861
)
984
1
      errorOrWarn("symbol '" + toString(Sym) +
985
1
                  "' cannot be preempted; recompile with -fPIE" +
986
1
                  getLocation(Sec, Sym, Offset));
987
4
    if (!Sym.isInPlt())
988
4
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
989
4
    if (!Sym.isDefined())
990
4
      replaceWithDefined(
991
4
          Sym, In.Plt,
992
4
          Target->PltHeaderSize + Target->PltEntrySize * Sym.PltIndex, 0);
993
4
    Sym.NeedsPltAddr = true;
994
4
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
995
4
    return;
996
4
  }
997
0
998
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
999
0
              getLocation(Sec, Sym, Offset));
1000
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const&, long long)
Line
Count
Source
869
14
                            int64_t Addend) {
870
14
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
871
13
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
872
13
    return;
873
13
  }
874
1
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText0
;
875
1
  if (CanWrite) {
876
1
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
877
1
    bool IsPreemptibleValue = Sym.IsPreemptible && Expr != R_GOT;
878
1
879
1
    if (!IsPreemptibleValue) {
880
0
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
881
0
      return;
882
1
    } else if (RelType Rel = Target->getDynRel(Type)) {
883
1
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
884
1
885
1
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
886
1
      // While regular ABI uses dynamic relocations to fill up GOT entries
887
1
      // MIPS ABI requires dynamic linker to fills up GOT entries using
888
1
      // specially sorted dynamic symbol table. This affects even dynamic
889
1
      // relocations against symbols which do not require GOT entries
890
1
      // creation explicitly, i.e. do not have any GOT-relocations. So if
891
1
      // a preemptible symbol has a dynamic relocation we anyway have
892
1
      // to create a GOT entry for it.
893
1
      // If a non-preemptible symbol has a dynamic relocation against it,
894
1
      // dynamic linker takes it st_value, adds offset and writes down
895
1
      // result of the dynamic relocation. In case of preemptible symbol
896
1
      // dynamic linker performs symbol resolution, writes the symbol value
897
1
      // to the GOT entry and reads the GOT entry when it needs to perform
898
1
      // a dynamic relocation.
899
1
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
900
1
      if (Config->EMachine == EM_MIPS)
901
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
902
1
      return;
903
1
    }
904
0
  }
905
0
906
0
  // If the relocation is to a weak undef, and we are producing
907
0
  // executable, give up on it and produce a non preemptible 0.
908
0
  if (!Config->Shared && Sym.isUndefWeak()) {
909
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
910
0
    return;
911
0
  }
912
0
913
0
  if (!CanWrite && (Config->Pic && !isRelExpr(Expr))) {
914
0
    error(
915
0
        "can't create dynamic relocation " + toString(Type) + " against " +
916
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
917
0
        " in readonly segment; recompile object files with -fPIC "
918
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
919
0
        getLocation(Sec, Sym, Offset));
920
0
    return;
921
0
  }
922
0
923
0
  // Copy relocations are only possible if we are creating an executable.
924
0
  if (Config->Shared) {
925
0
    errorOrWarn("relocation " + toString(Type) +
926
0
                " cannot be used against symbol " + toString(Sym) +
927
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
928
0
    return;
929
0
  }
930
0
931
0
  // If the symbol is undefined we already reported any relevant errors.
932
0
  if (Sym.isUndefined())
933
0
    return;
934
0
935
0
  if (!canDefineSymbolInExecutable(Sym)) {
936
0
    error("cannot preempt symbol: " + toString(Sym) +
937
0
          getLocation(Sec, Sym, Offset));
938
0
    return;
939
0
  }
940
0
941
0
  if (Sym.isObject()) {
942
0
    // Produce a copy relocation.
943
0
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
944
0
      if (!Config->ZCopyreloc)
945
0
        error("unresolvable relocation " + toString(Type) +
946
0
              " against symbol '" + toString(*SS) +
947
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
948
0
              getLocation(Sec, Sym, Offset));
949
0
      addCopyRelSymbol<ELFT>(*SS);
950
0
    }
951
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
952
0
    return;
953
0
  }
954
0
955
0
  if (Sym.isFunc()) {
956
0
    // This handles a non PIC program call to function in a shared library. In
957
0
    // an ideal world, we could just report an error saying the relocation can
958
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
959
0
    // R_X86_64_PC32 pointing to libc.so.
960
0
    //
961
0
    // The general idea on how to handle such cases is to create a PLT entry and
962
0
    // use that as the function value.
963
0
    //
964
0
    // For the static linking part, we just return a plt expr and everything
965
0
    // else will use the PLT entry as the address.
966
0
    //
967
0
    // The remaining problem is making sure pointer equality still works. We
968
0
    // need the help of the dynamic linker for that. We let it know that we have
969
0
    // a direct reference to a so symbol by creating an undefined symbol with a
970
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
971
0
    // the value of the symbol we created. This is true even for got entries, so
972
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
973
0
    // that points to the real function is a dedicated got entry used by the
974
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
975
0
    // R_386_JMP_SLOT, etc).
976
0
977
0
    // For position independent executable on i386, the plt entry requires ebx
978
0
    // to be set. This causes two problems:
979
0
    // * If some code has a direct reference to a function, it was probably
980
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
981
0
    // * If a library definition gets preempted to the executable, it will have
982
0
    //   the wrong ebx value.
983
0
    if (Config->Pie && Config->EMachine == EM_386)
984
0
      errorOrWarn("symbol '" + toString(Sym) +
985
0
                  "' cannot be preempted; recompile with -fPIE" +
986
0
                  getLocation(Sec, Sym, Offset));
987
0
    if (!Sym.isInPlt())
988
0
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
989
0
    if (!Sym.isDefined())
990
0
      replaceWithDefined(
991
0
          Sym, In.Plt,
992
0
          Target->PltHeaderSize + Target->PltEntrySize * Sym.PltIndex, 0);
993
0
    Sym.NeedsPltAddr = true;
994
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
995
0
    return;
996
0
  }
997
0
998
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
999
0
              getLocation(Sec, Sym, Offset));
1000
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const&, long long)
Line
Count
Source
869
260
                            int64_t Addend) {
870
260
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
871
243
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
872
243
    return;
873
243
  }
874
17
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText15
;
875
17
  if (CanWrite) {
876
2
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
877
2
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT1
;
878
2
879
2
    if (!IsPreemptibleValue) {
880
1
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
881
1
      return;
882
1
    } else if (RelType Rel = Target->getDynRel(Type)) {
883
1
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
884
1
885
1
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
886
1
      // While regular ABI uses dynamic relocations to fill up GOT entries
887
1
      // MIPS ABI requires dynamic linker to fills up GOT entries using
888
1
      // specially sorted dynamic symbol table. This affects even dynamic
889
1
      // relocations against symbols which do not require GOT entries
890
1
      // creation explicitly, i.e. do not have any GOT-relocations. So if
891
1
      // a preemptible symbol has a dynamic relocation we anyway have
892
1
      // to create a GOT entry for it.
893
1
      // If a non-preemptible symbol has a dynamic relocation against it,
894
1
      // dynamic linker takes it st_value, adds offset and writes down
895
1
      // result of the dynamic relocation. In case of preemptible symbol
896
1
      // dynamic linker performs symbol resolution, writes the symbol value
897
1
      // to the GOT entry and reads the GOT entry when it needs to perform
898
1
      // a dynamic relocation.
899
1
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
900
1
      if (Config->EMachine == EM_MIPS)
901
1
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
902
1
      return;
903
1
    }
904
15
  }
905
15
906
15
  // If the relocation is to a weak undef, and we are producing
907
15
  // executable, give up on it and produce a non preemptible 0.
908
15
  if (!Config->Shared && Sym.isUndefWeak()) {
909
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
910
0
    return;
911
0
  }
912
15
913
15
  if (!CanWrite && (Config->Pic && 
!isRelExpr(Expr)0
)) {
914
0
    error(
915
0
        "can't create dynamic relocation " + toString(Type) + " against " +
916
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
917
0
        " in readonly segment; recompile object files with -fPIC "
918
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
919
0
        getLocation(Sec, Sym, Offset));
920
0
    return;
921
0
  }
922
15
923
15
  // Copy relocations are only possible if we are creating an executable.
924
15
  if (Config->Shared) {
925
0
    errorOrWarn("relocation " + toString(Type) +
926
0
                " cannot be used against symbol " + toString(Sym) +
927
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
928
0
    return;
929
0
  }
930
15
931
15
  // If the symbol is undefined we already reported any relevant errors.
932
15
  if (Sym.isUndefined())
933
0
    return;
934
15
935
15
  if (!canDefineSymbolInExecutable(Sym)) {
936
0
    error("cannot preempt symbol: " + toString(Sym) +
937
0
          getLocation(Sec, Sym, Offset));
938
0
    return;
939
0
  }
940
15
941
15
  if (Sym.isObject()) {
942
8
    // Produce a copy relocation.
943
8
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
944
5
      if (!Config->ZCopyreloc)
945
0
        error("unresolvable relocation " + toString(Type) +
946
0
              " against symbol '" + toString(*SS) +
947
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
948
0
              getLocation(Sec, Sym, Offset));
949
5
      addCopyRelSymbol<ELFT>(*SS);
950
5
    }
951
8
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
952
8
    return;
953
8
  }
954
7
955
7
  if (Sym.isFunc()) {
956
7
    // This handles a non PIC program call to function in a shared library. In
957
7
    // an ideal world, we could just report an error saying the relocation can
958
7
    // overflow at runtime. In the real world with glibc, crt1.o has a
959
7
    // R_X86_64_PC32 pointing to libc.so.
960
7
    //
961
7
    // The general idea on how to handle such cases is to create a PLT entry and
962
7
    // use that as the function value.
963
7
    //
964
7
    // For the static linking part, we just return a plt expr and everything
965
7
    // else will use the PLT entry as the address.
966
7
    //
967
7
    // The remaining problem is making sure pointer equality still works. We
968
7
    // need the help of the dynamic linker for that. We let it know that we have
969
7
    // a direct reference to a so symbol by creating an undefined symbol with a
970
7
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
971
7
    // the value of the symbol we created. This is true even for got entries, so
972
7
    // pointer equality is maintained. To avoid an infinite loop, the only entry
973
7
    // that points to the real function is a dedicated got entry used by the
974
7
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
975
7
    // R_386_JMP_SLOT, etc).
976
7
977
7
    // For position independent executable on i386, the plt entry requires ebx
978
7
    // to be set. This causes two problems:
979
7
    // * If some code has a direct reference to a function, it was probably
980
7
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
981
7
    // * If a library definition gets preempted to the executable, it will have
982
7
    //   the wrong ebx value.
983
7
    if (Config->Pie && 
Config->EMachine == EM_3860
)
984
0
      errorOrWarn("symbol '" + toString(Sym) +
985
0
                  "' cannot be preempted; recompile with -fPIE" +
986
0
                  getLocation(Sec, Sym, Offset));
987
7
    if (!Sym.isInPlt())
988
4
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
989
7
    if (!Sym.isDefined())
990
4
      replaceWithDefined(
991
4
          Sym, In.Plt,
992
4
          Target->PltHeaderSize + Target->PltEntrySize * Sym.PltIndex, 0);
993
7
    Sym.NeedsPltAddr = true;
994
7
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
995
7
    return;
996
7
  }
997
0
998
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
999
0
              getLocation(Sec, Sym, Offset));
1000
0
}
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const&, long long)
Line
Count
Source
869
2.22k
                            int64_t Addend) {
870
2.22k
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
871
1.85k
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
872
1.85k
    return;
873
1.85k
  }
874
363
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText133
;
875
363
  if (CanWrite) {
876
246
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
877
246
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT126
;
878
246
879
246
    if (!IsPreemptibleValue) {
880
120
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
881
120
      return;
882
126
    } else if (RelType Rel = Target->getDynRel(Type)) {
883
108
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
884
108
885
108
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
886
108
      // While regular ABI uses dynamic relocations to fill up GOT entries
887
108
      // MIPS ABI requires dynamic linker to fills up GOT entries using
888
108
      // specially sorted dynamic symbol table. This affects even dynamic
889
108
      // relocations against symbols which do not require GOT entries
890
108
      // creation explicitly, i.e. do not have any GOT-relocations. So if
891
108
      // a preemptible symbol has a dynamic relocation we anyway have
892
108
      // to create a GOT entry for it.
893
108
      // If a non-preemptible symbol has a dynamic relocation against it,
894
108
      // dynamic linker takes it st_value, adds offset and writes down
895
108
      // result of the dynamic relocation. In case of preemptible symbol
896
108
      // dynamic linker performs symbol resolution, writes the symbol value
897
108
      // to the GOT entry and reads the GOT entry when it needs to perform
898
108
      // a dynamic relocation.
899
108
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
900
108
      if (Config->EMachine == EM_MIPS)
901
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
902
108
      return;
903
108
    }
904
135
  }
905
135
906
135
  // If the relocation is to a weak undef, and we are producing
907
135
  // executable, give up on it and produce a non preemptible 0.
908
135
  if (!Config->Shared && 
Sym.isUndefWeak()105
) {
909
8
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
910
8
    return;
911
8
  }
912
127
913
127
  if (!CanWrite && 
(109
Config->Pic109
&&
!isRelExpr(Expr)21
)) {
914
14
    error(
915
14
        "can't create dynamic relocation " + toString(Type) + " against " +
916
14
        (Sym.getName().empty() ? 
"local symbol"3
:
"symbol: " + toString(Sym)11
) +
917
14
        " in readonly segment; recompile object files with -fPIC "
918
14
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
919
14
        getLocation(Sec, Sym, Offset));
920
14
    return;
921
14
  }
922
113
923
113
  // Copy relocations are only possible if we are creating an executable.
924
113
  if (Config->Shared) {
925
18
    errorOrWarn("relocation " + toString(Type) +
926
18
                " cannot be used against symbol " + toString(Sym) +
927
18
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
928
18
    return;
929
18
  }
930
95
931
95
  // If the symbol is undefined we already reported any relevant errors.
932
95
  if (Sym.isUndefined())
933
3
    return;
934
92
935
92
  if (!canDefineSymbolInExecutable(Sym)) {
936
5
    error("cannot preempt symbol: " + toString(Sym) +
937
5
          getLocation(Sec, Sym, Offset));
938
5
    return;
939
5
  }
940
87
941
87
  if (Sym.isObject()) {
942
49
    // Produce a copy relocation.
943
49
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
944
40
      if (!Config->ZCopyreloc)
945
3
        error("unresolvable relocation " + toString(Type) +
946
3
              " against symbol '" + toString(*SS) +
947
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
948
3
              getLocation(Sec, Sym, Offset));
949
40
      addCopyRelSymbol<ELFT>(*SS);
950
40
    }
951
49
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
952
49
    return;
953
49
  }
954
38
955
38
  if (Sym.isFunc()) {
956
35
    // This handles a non PIC program call to function in a shared library. In
957
35
    // an ideal world, we could just report an error saying the relocation can
958
35
    // overflow at runtime. In the real world with glibc, crt1.o has a
959
35
    // R_X86_64_PC32 pointing to libc.so.
960
35
    //
961
35
    // The general idea on how to handle such cases is to create a PLT entry and
962
35
    // use that as the function value.
963
35
    //
964
35
    // For the static linking part, we just return a plt expr and everything
965
35
    // else will use the PLT entry as the address.
966
35
    //
967
35
    // The remaining problem is making sure pointer equality still works. We
968
35
    // need the help of the dynamic linker for that. We let it know that we have
969
35
    // a direct reference to a so symbol by creating an undefined symbol with a
970
35
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
971
35
    // the value of the symbol we created. This is true even for got entries, so
972
35
    // pointer equality is maintained. To avoid an infinite loop, the only entry
973
35
    // that points to the real function is a dedicated got entry used by the
974
35
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
975
35
    // R_386_JMP_SLOT, etc).
976
35
977
35
    // For position independent executable on i386, the plt entry requires ebx
978
35
    // to be set. This causes two problems:
979
35
    // * If some code has a direct reference to a function, it was probably
980
35
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
981
35
    // * If a library definition gets preempted to the executable, it will have
982
35
    //   the wrong ebx value.
983
35
    if (Config->Pie && 
Config->EMachine == EM_3861
)
984
0
      errorOrWarn("symbol '" + toString(Sym) +
985
0
                  "' cannot be preempted; recompile with -fPIE" +
986
0
                  getLocation(Sec, Sym, Offset));
987
35
    if (!Sym.isInPlt())
988
34
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
989
35
    if (!Sym.isDefined())
990
35
      replaceWithDefined(
991
35
          Sym, In.Plt,
992
35
          Target->PltHeaderSize + Target->PltEntrySize * Sym.PltIndex, 0);
993
35
    Sym.NeedsPltAddr = true;
994
35
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
995
35
    return;
996
35
  }
997
3
998
3
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
999
3
              getLocation(Sec, Sym, Offset));
1000
3
}
Unexecuted instantiation: Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false> const&, long long)
Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, true> const&, long long)
Line
Count
Source
869
10.3k
                            int64_t Addend) {
870
10.3k
  if (isStaticLinkTimeConstant(Expr, Type, Sym, Sec, Offset)) {
871
10.3k
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
872
10.3k
    return;
873
10.3k
  }
874
20
  bool CanWrite = (Sec.Flags & SHF_WRITE) || 
!Config->ZText2
;
875
20
  if (CanWrite) {
876
20
    // R_GOT refers to a position in the got, even if the symbol is preemptible.
877
20
    bool IsPreemptibleValue = Sym.IsPreemptible && 
Expr != R_GOT10
;
878
20
879
20
    if (!IsPreemptibleValue) {
880
10
      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
881
10
      return;
882
10
    } else if (RelType Rel = Target->getDynRel(Type)) {
883
10
      In.RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
884
10
885
10
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
886
10
      // While regular ABI uses dynamic relocations to fill up GOT entries
887
10
      // MIPS ABI requires dynamic linker to fills up GOT entries using
888
10
      // specially sorted dynamic symbol table. This affects even dynamic
889
10
      // relocations against symbols which do not require GOT entries
890
10
      // creation explicitly, i.e. do not have any GOT-relocations. So if
891
10
      // a preemptible symbol has a dynamic relocation we anyway have
892
10
      // to create a GOT entry for it.
893
10
      // If a non-preemptible symbol has a dynamic relocation against it,
894
10
      // dynamic linker takes it st_value, adds offset and writes down
895
10
      // result of the dynamic relocation. In case of preemptible symbol
896
10
      // dynamic linker performs symbol resolution, writes the symbol value
897
10
      // to the GOT entry and reads the GOT entry when it needs to perform
898
10
      // a dynamic relocation.
899
10
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
900
10
      if (Config->EMachine == EM_MIPS)
901
2
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
902
10
      return;
903
10
    }
904
0
  }
905
0
906
0
  // If the relocation is to a weak undef, and we are producing
907
0
  // executable, give up on it and produce a non preemptible 0.
908
0
  if (!Config->Shared && Sym.isUndefWeak()) {
909
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
910
0
    return;
911
0
  }
912
0
913
0
  if (!CanWrite && (Config->Pic && !isRelExpr(Expr))) {
914
0
    error(
915
0
        "can't create dynamic relocation " + toString(Type) + " against " +
916
0
        (Sym.getName().empty() ? "local symbol" : "symbol: " + toString(Sym)) +
917
0
        " in readonly segment; recompile object files with -fPIC "
918
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
919
0
        getLocation(Sec, Sym, Offset));
920
0
    return;
921
0
  }
922
0
923
0
  // Copy relocations are only possible if we are creating an executable.
924
0
  if (Config->Shared) {
925
0
    errorOrWarn("relocation " + toString(Type) +
926
0
                " cannot be used against symbol " + toString(Sym) +
927
0
                "; recompile with -fPIC" + getLocation(Sec, Sym, Offset));
928
0
    return;
929
0
  }
930
0
931
0
  // If the symbol is undefined we already reported any relevant errors.
932
0
  if (Sym.isUndefined())
933
0
    return;
934
0
935
0
  if (!canDefineSymbolInExecutable(Sym)) {
936
0
    error("cannot preempt symbol: " + toString(Sym) +
937
0
          getLocation(Sec, Sym, Offset));
938
0
    return;
939
0
  }
940
0
941
0
  if (Sym.isObject()) {
942
0
    // Produce a copy relocation.
943
0
    if (auto *SS = dyn_cast<SharedSymbol>(&Sym)) {
944
0
      if (!Config->ZCopyreloc)
945
0
        error("unresolvable relocation " + toString(Type) +
946
0
              " against symbol '" + toString(*SS) +
947
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
948
0
              getLocation(Sec, Sym, Offset));
949
0
      addCopyRelSymbol<ELFT>(*SS);
950
0
    }
951
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
952
0
    return;
953
0
  }
954
0
955
0
  if (Sym.isFunc()) {
956
0
    // This handles a non PIC program call to function in a shared library. In
957
0
    // an ideal world, we could just report an error saying the relocation can
958
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
959
0
    // R_X86_64_PC32 pointing to libc.so.
960
0
    //
961
0
    // The general idea on how to handle such cases is to create a PLT entry and
962
0
    // use that as the function value.
963
0
    //
964
0
    // For the static linking part, we just return a plt expr and everything
965
0
    // else will use the PLT entry as the address.
966
0
    //
967
0
    // The remaining problem is making sure pointer equality still works. We
968
0
    // need the help of the dynamic linker for that. We let it know that we have
969
0
    // a direct reference to a so symbol by creating an undefined symbol with a
970
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
971
0
    // the value of the symbol we created. This is true even for got entries, so
972
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
973
0
    // that points to the real function is a dedicated got entry used by the
974
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
975
0
    // R_386_JMP_SLOT, etc).
976
0
977
0
    // For position independent executable on i386, the plt entry requires ebx
978
0
    // to be set. This causes two problems:
979
0
    // * If some code has a direct reference to a function, it was probably
980
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
981
0
    // * If a library definition gets preempted to the executable, it will have
982
0
    //   the wrong ebx value.
983
0
    if (Config->Pie && Config->EMachine == EM_386)
984
0
      errorOrWarn("symbol '" + toString(Sym) +
985
0
                  "' cannot be preempted; recompile with -fPIE" +
986
0
                  getLocation(Sec, Sym, Offset));
987
0
    if (!Sym.isInPlt())
988
0
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
989
0
    if (!Sym.isDefined())
990
0
      replaceWithDefined(
991
0
          Sym, In.Plt,
992
0
          Target->PltHeaderSize + Target->PltEntrySize * Sym.PltIndex, 0);
993
0
    Sym.NeedsPltAddr = true;
994
0
    Sec.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
995
0
    return;
996
0
  }
997
0
998
0
  errorOrWarn("symbol '" + toString(Sym) + "' has no type" +
999
0
              getLocation(Sec, Sym, Offset));
1000
0
}
Unexecuted instantiation: Relocations.cpp:void processRelocAux<llvm::object::ELFType<(llvm::support::endianness)0, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> >(lld::elf::InputSectionBase&, lld::elf::RelExpr, unsigned int, unsigned long long, lld::elf::Symbol&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false> const&, long long)
1001
1002
struct IRelativeReloc {
1003
  RelType Type;
1004
  InputSectionBase *Sec;
1005
  uint64_t Offset;
1006
  Symbol *Sym;
1007
};
1008
1009
static std::vector<IRelativeReloc> IRelativeRelocs;
1010
1011
template <class ELFT, class RelTy>
1012
static void scanReloc(InputSectionBase &Sec, OffsetGetter &GetOffset, RelTy *&I,
1013
14.1k
                      RelTy *End) {
1014
14.1k
  const RelTy &Rel = *I;
1015
14.1k
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
1016
14.1k
  RelType Type;
1017
14.1k
1018
14.1k
  // Deal with MIPS oddity.
1019
14.1k
  if (Config->MipsN32Abi) {
1020
5
    Type = getMipsN32RelType(I, End);
1021
14.1k
  } else {
1022
14.1k
    Type = Rel.getType(Config->IsMips64EL);
1023
14.1k
    ++I;
1024
14.1k
  }
1025
14.1k
1026
14.1k
  // Get an offset in an output section this relocation is applied to.
1027
14.1k
  uint64_t Offset = GetOffset.get(Rel.r_offset);
1028
14.1k
  if (Offset == uint64_t(-1))
1029
13
    return;
1030
14.1k
1031
14.1k
  // Skip if the target symbol is an erroneous undefined symbol.
1032
14.1k
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
1033
79
    return;
1034
14.0k
1035
14.0k
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
1036
14.0k
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
1037
14.0k
1038
14.0k
  // Ignore "hint" relocations because they are only markers for relaxation.
1039
14.0k
  if (oneof<R_HINT, R_NONE>(Expr))
1040
30
    return;
1041
14.0k
1042
14.0k
  // We can separate the small code model relocations into 2 categories:
1043
14.0k
  // 1) Those that access the compiler generated .toc sections.
1044
14.0k
  // 2) Those that access the linker allocated got entries.
1045
14.0k
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1046
14.0k
  // the got entries in any way, we don't have to track which objects have
1047
14.0k
  // got-based small code model relocs. The .toc sections get placed after the
1048
14.0k
  // end of the linker allocated .got section and we do sort those so sections
1049
14.0k
  // addressed with small code model relocations come first.
1050
14.0k
  if (Config->EMachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(Type)989
)
1051
15
    Sec.File->PPC64SmallCodeModelTocRelocs = true;
1052
14.0k
1053
14.0k
  if (Sym.isGnuIFunc() && 
!Config->ZText87
&&
Config->WarnIfuncTextrel6
) {
1054
1
    warn("using ifunc symbols when text relocations are allowed may produce "
1055
1
         "a binary that will segfault, if the object file is linked with "
1056
1
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1057
1
         "you, consider recompiling the object files without -fPIC and "
1058
1
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1059
1
         "turn off this warning." +
1060
1
         getLocation(Sec, Sym, Offset));
1061
1
  }
1062
14.0k
1063
14.0k
  // Relax relocations.
1064
14.0k
  //
1065
14.0k
  // If we know that a PLT entry will be resolved within the same ELF module, we
1066
14.0k
  // can skip PLT access and directly jump to the destination function. For
1067
14.0k
  // example, if we are linking a main exectuable, all dynamic symbols that can
1068
14.0k
  // be resolved within the executable will actually be resolved that way at
1069
14.0k
  // runtime, because the main exectuable is always at the beginning of a search
1070
14.0k
  // list. We can leverage that fact.
1071
14.0k
  if (!Sym.IsPreemptible && 
(13.1k
!Sym.isGnuIFunc()13.1k
||
Config->ZIfuncNoplt84
)) {
1072
13.0k
    if (Expr == R_GOT_PC && 
!isAbsoluteValue(Sym)97
)
1073
55
      Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1074
13.0k
    else
1075
13.0k
      Expr = fromPlt(Expr);
1076
13.0k
  }
1077
14.0k
1078
14.0k
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1079
14.0k
  // uses their addresses, we need GOT or GOTPLT to be created.
1080
14.0k
  //
1081
14.0k
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1082
14.0k
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(Expr)) {
1083
54
    In.GotPlt->HasGotPltOffRel = true;
1084
14.0k
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC_TOC, R_PPC64_RELAX_TOC>(Expr)) {
1085
200
    In.Got->HasGotOffRel = true;
1086
200
  }
1087
14.0k
1088
14.0k
  // Read an addend.
1089
14.0k
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1090
14.0k
1091
14.0k
  // Process some TLS relocations, including relaxing TLS relocations.
1092
14.0k
  // Note that this function does not handle all TLS relocations.
1093
14.0k
  if (unsigned Processed =
1094
306
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1095
306
    I += (Processed - 1);
1096
306
    return;
1097
306
  }
1098
13.7k
1099
13.7k
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1100
13.7k
  // direct relocation on through.
1101
13.7k
  if (Sym.isGnuIFunc() && 
Config->ZIfuncNoplt87
) {
1102
4
    Sym.ExportDynamic = true;
1103
4
    In.RelaDyn->addReloc(Type, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
1104
4
    return;
1105
4
  }
1106
13.7k
1107
13.7k
  // Non-preemptible ifuncs require special handling. First, handle the usual
1108
13.7k
  // case where the symbol isn't one of these.
1109
13.7k
  if (!Sym.isGnuIFunc() || 
Sym.IsPreemptible83
) {
1110
13.6k
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1111
13.6k
    if (needsPlt(Expr) && 
!Sym.isInPlt()330
)
1112
282
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1113
13.6k
1114
13.6k
    // Create a GOT slot if a relocation needs GOT.
1115
13.6k
    if (needsGot(Expr)) {
1116
10.3k
      if (Config->EMachine == EM_MIPS) {
1117
10.1k
        // MIPS ABI has special rules to process GOT entries and doesn't
1118
10.1k
        // require relocation entries for them. A special case is TLS
1119
10.1k
        // relocations. In that case dynamic loader applies dynamic
1120
10.1k
        // relocations to initialize TLS GOT entries.
1121
10.1k
        // See "Global Offset Table" in Chapter 5 in the following document
1122
10.1k
        // for detailed description:
1123
10.1k
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1124
10.1k
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1125
10.1k
      } else 
if (256
!Sym.isInGot()256
) {
1126
147
        addGotEntry(Sym);
1127
147
      }
1128
10.3k
    }
1129
13.6k
  } else {
1130
80
    // Handle a reference to a non-preemptible ifunc. These are special in a
1131
80
    // few ways:
1132
80
    //
1133
80
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1134
80
    //   a fixed value. But assuming that all references to the ifunc are
1135
80
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1136
80
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1137
80
    //   usually at the end of .plt, which makes an indirect call using a
1138
80
    //   matching GOT entry in IgotPlt, which is usually at the end of .got.plt.
1139
80
    //   The GOT entry is relocated using an IRELATIVE relocation in RelaIplt,
1140
80
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1141
80
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1142
80
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1143
80
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1144
80
    //   .got.plt without requiring a separate GOT entry.
1145
80
    //
1146
80
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1147
80
    //   that are not passed -fPIC will assume that they do, and will emit
1148
80
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1149
80
    //   symbol. This means that if a direct relocation to the symbol is
1150
80
    //   seen, the linker must set a value for the symbol, and this value must
1151
80
    //   be consistent no matter what type of reference is made to the symbol.
1152
80
    //   This can be done by creating a PLT entry for the symbol in the way
1153
80
    //   described above and making it canonical, that is, making all references
1154
80
    //   point to the PLT entry instead of the resolver. In lld we also store
1155
80
    //   the address of the PLT entry in the dynamic symbol table, which means
1156
80
    //   that the symbol will also have the same value in other modules.
1157
80
    //   Because the value loaded from the GOT needs to be consistent with
1158
80
    //   the value computed using a direct relocation, a non-preemptible ifunc
1159
80
    //   may end up with two GOT entries, one in .got.plt that points to the
1160
80
    //   address returned by the resolver and is used only by the PLT entry,
1161
80
    //   and another in .got that points to the PLT entry and is used by
1162
80
    //   GOT-generating relocations.
1163
80
    //
1164
80
    // - The fact that these symbols do not have a fixed value makes them an
1165
80
    //   exception to the general rule that a statically linked executable does
1166
80
    //   not require any form of dynamic relocation. To handle these relocations
1167
80
    //   correctly, the IRELATIVE relocations are stored in an array which a
1168
80
    //   statically linked executable's startup code must enumerate using the
1169
80
    //   linker-defined symbols __rela?_iplt_{start,end}.
1170
80
    //
1171
80
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1172
80
    //   variable containing a pointer to the ifunc) needs to be relocated in
1173
80
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1174
80
    //   PLT entry canonical by translating such relocations into IRELATIVE
1175
80
    //   relocations in the RelaIplt.
1176
80
    if (!Sym.isInPlt()) {
1177
57
      // Create PLT and GOTPLT slots for the symbol.
1178
57
      Sym.IsInIplt = true;
1179
57
1180
57
      // Create a copy of the symbol to use as the target of the IRELATIVE
1181
57
      // relocation in the IgotPlt. This is in case we make the PLT canonical
1182
57
      // later, which would overwrite the original symbol.
1183
57
      //
1184
57
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1185
57
      // that's really needed to create the IRELATIVE is the section and value,
1186
57
      // so ideally we should just need to copy those.
1187
57
      auto *DirectSym = make<Defined>(cast<Defined>(Sym));
1188
57
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1189
57
                        *DirectSym);
1190
57
      Sym.PltIndex = DirectSym->PltIndex;
1191
57
    }
1192
80
    if (Expr == R_ABS && 
Addend == 015
&&
(Sec.Flags & SHF_WRITE)10
) {
1193
8
      // We might be able to represent this as an IRELATIVE. But we don't know
1194
8
      // yet whether some later relocation will make the symbol point to a
1195
8
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1196
8
      // static (non-PIC) relocation. So we keep a record of the information
1197
8
      // required to process the relocation, and after scanRelocs() has been
1198
8
      // called on all relocations, the relocation is resolved by
1199
8
      // addIRelativeRelocs().
1200
8
      IRelativeRelocs.push_back({Type, &Sec, Offset, &Sym});
1201
8
      return;
1202
8
    }
1203
72
    if (needsGot(Expr)) {
1204
19
      // Redirect GOT accesses to point to the Igot.
1205
19
      //
1206
19
      // This field is also used to keep track of whether we ever needed a GOT
1207
19
      // entry. If we did and we make the PLT canonical later, we'll need to
1208
19
      // create a GOT entry pointing to the PLT entry for Sym.
1209
19
      Sym.GotInIgot = true;
1210
53
    } else if (!needsPlt(Expr)) {
1211
17
      // Make the ifunc's PLT entry canonical by changing the value of its
1212
17
      // symbol to redirect all references to point to it.
1213
17
      unsigned EntryOffset = Sym.PltIndex * Target->PltEntrySize;
1214
17
      if (Config->ZRetpolineplt)
1215
1
        EntryOffset += Target->PltHeaderSize;
1216
17
1217
17
      auto &D = cast<Defined>(Sym);
1218
17
      D.Section = In.Iplt;
1219
17
      D.Value = EntryOffset;
1220
17
      D.Size = 0;
1221
17
      // It's important to set the symbol type here so that dynamic loaders
1222
17
      // don't try to call the PLT as if it were an ifunc resolver.
1223
17
      D.Type = STT_FUNC;
1224
17
1225
17
      if (Sym.GotInIgot) {
1226
6
        // We previously encountered a GOT generating reference that we
1227
6
        // redirected to the Igot. Now that the PLT entry is canonical we must
1228
6
        // clear the redirection to the Igot and add a GOT entry. As we've
1229
6
        // changed the symbol type to STT_FUNC future GOT generating references
1230
6
        // will naturally use this GOT entry.
1231
6
        //
1232
6
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1233
6
        // aren't a thing on MIPS.
1234
6
        Sym.GotInIgot = false;
1235
6
        addGotEntry(Sym);
1236
6
      }
1237
17
    }
1238
72
  }
1239
13.7k
1240
13.7k
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1241
13.7k
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, true> const*)
Line
Count
Source
1013
108
                      RelTy *End) {
1014
108
  const RelTy &Rel = *I;
1015
108
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
1016
108
  RelType Type;
1017
108
1018
108
  // Deal with MIPS oddity.
1019
108
  if (Config->MipsN32Abi) {
1020
0
    Type = getMipsN32RelType(I, End);
1021
108
  } else {
1022
108
    Type = Rel.getType(Config->IsMips64EL);
1023
108
    ++I;
1024
108
  }
1025
108
1026
108
  // Get an offset in an output section this relocation is applied to.
1027
108
  uint64_t Offset = GetOffset.get(Rel.r_offset);
1028
108
  if (Offset == uint64_t(-1))
1029
0
    return;
1030
108
1031
108
  // Skip if the target symbol is an erroneous undefined symbol.
1032
108
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
1033
0
    return;
1034
108
1035
108
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
1036
108
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
1037
108
1038
108
  // Ignore "hint" relocations because they are only markers for relaxation.
1039
108
  if (oneof<R_HINT, R_NONE>(Expr))
1040
0
    return;
1041
108
1042
108
  // We can separate the small code model relocations into 2 categories:
1043
108
  // 1) Those that access the compiler generated .toc sections.
1044
108
  // 2) Those that access the linker allocated got entries.
1045
108
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1046
108
  // the got entries in any way, we don't have to track which objects have
1047
108
  // got-based small code model relocs. The .toc sections get placed after the
1048
108
  // end of the linker allocated .got section and we do sort those so sections
1049
108
  // addressed with small code model relocations come first.
1050
108
  if (Config->EMachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(Type)0
)
1051
0
    Sec.File->PPC64SmallCodeModelTocRelocs = true;
1052
108
1053
108
  if (Sym.isGnuIFunc() && 
!Config->ZText0
&&
Config->WarnIfuncTextrel0
) {
1054
0
    warn("using ifunc symbols when text relocations are allowed may produce "
1055
0
         "a binary that will segfault, if the object file is linked with "
1056
0
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1057
0
         "you, consider recompiling the object files without -fPIC and "
1058
0
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1059
0
         "turn off this warning." +
1060
0
         getLocation(Sec, Sym, Offset));
1061
0
  }
1062
108
1063
108
  // Relax relocations.
1064
108
  //
1065
108
  // If we know that a PLT entry will be resolved within the same ELF module, we
1066
108
  // can skip PLT access and directly jump to the destination function. For
1067
108
  // example, if we are linking a main exectuable, all dynamic symbols that can
1068
108
  // be resolved within the executable will actually be resolved that way at
1069
108
  // runtime, because the main exectuable is always at the beginning of a search
1070
108
  // list. We can leverage that fact.
1071
108
  if (!Sym.IsPreemptible && 
(100
!Sym.isGnuIFunc()100
||
Config->ZIfuncNoplt0
)) {
1072
100
    if (Expr == R_GOT_PC && 
!isAbsoluteValue(Sym)0
)
1073
0
      Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1074
100
    else
1075
100
      Expr = fromPlt(Expr);
1076
100
  }
1077
108
1078
108
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1079
108
  // uses their addresses, we need GOT or GOTPLT to be created.
1080
108
  //
1081
108
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1082
108
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(Expr)) {
1083
0
    In.GotPlt->HasGotPltOffRel = true;
1084
108
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC_TOC, R_PPC64_RELAX_TOC>(Expr)) {
1085
0
    In.Got->HasGotOffRel = true;
1086
0
  }
1087
108
1088
108
  // Read an addend.
1089
108
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1090
108
1091
108
  // Process some TLS relocations, including relaxing TLS relocations.
1092
108
  // Note that this function does not handle all TLS relocations.
1093
108
  if (unsigned Processed =
1094
0
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1095
0
    I += (Processed - 1);
1096
0
    return;
1097
0
  }
1098
108
1099
108
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1100
108
  // direct relocation on through.
1101
108
  if (Sym.isGnuIFunc() && 
Config->ZIfuncNoplt0
) {
1102
0
    Sym.ExportDynamic = true;
1103
0
    In.RelaDyn->addReloc(Type, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
1104
0
    return;
1105
0
  }
1106
108
1107
108
  // Non-preemptible ifuncs require special handling. First, handle the usual
1108
108
  // case where the symbol isn't one of these.
1109
108
  if (!Sym.isGnuIFunc() || 
Sym.IsPreemptible0
) {
1110
108
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1111
108
    if (needsPlt(Expr) && 
!Sym.isInPlt()4
)
1112
2
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1113
108
1114
108
    // Create a GOT slot if a relocation needs GOT.
1115
108
    if (needsGot(Expr)) {
1116
4
      if (Config->EMachine == EM_MIPS) {
1117
0
        // MIPS ABI has special rules to process GOT entries and doesn't
1118
0
        // require relocation entries for them. A special case is TLS
1119
0
        // relocations. In that case dynamic loader applies dynamic
1120
0
        // relocations to initialize TLS GOT entries.
1121
0
        // See "Global Offset Table" in Chapter 5 in the following document
1122
0
        // for detailed description:
1123
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1124
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1125
4
      } else if (!Sym.isInGot()) {
1126
1
        addGotEntry(Sym);
1127
1
      }
1128
4
    }
1129
108
  } else {
1130
0
    // Handle a reference to a non-preemptible ifunc. These are special in a
1131
0
    // few ways:
1132
0
    //
1133
0
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1134
0
    //   a fixed value. But assuming that all references to the ifunc are
1135
0
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1136
0
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1137
0
    //   usually at the end of .plt, which makes an indirect call using a
1138
0
    //   matching GOT entry in IgotPlt, which is usually at the end of .got.plt.
1139
0
    //   The GOT entry is relocated using an IRELATIVE relocation in RelaIplt,
1140
0
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1141
0
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1142
0
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1143
0
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1144
0
    //   .got.plt without requiring a separate GOT entry.
1145
0
    //
1146
0
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1147
0
    //   that are not passed -fPIC will assume that they do, and will emit
1148
0
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1149
0
    //   symbol. This means that if a direct relocation to the symbol is
1150
0
    //   seen, the linker must set a value for the symbol, and this value must
1151
0
    //   be consistent no matter what type of reference is made to the symbol.
1152
0
    //   This can be done by creating a PLT entry for the symbol in the way
1153
0
    //   described above and making it canonical, that is, making all references
1154
0
    //   point to the PLT entry instead of the resolver. In lld we also store
1155
0
    //   the address of the PLT entry in the dynamic symbol table, which means
1156
0
    //   that the symbol will also have the same value in other modules.
1157
0
    //   Because the value loaded from the GOT needs to be consistent with
1158
0
    //   the value computed using a direct relocation, a non-preemptible ifunc
1159
0
    //   may end up with two GOT entries, one in .got.plt that points to the
1160
0
    //   address returned by the resolver and is used only by the PLT entry,
1161
0
    //   and another in .got that points to the PLT entry and is used by
1162
0
    //   GOT-generating relocations.
1163
0
    //
1164
0
    // - The fact that these symbols do not have a fixed value makes them an
1165
0
    //   exception to the general rule that a statically linked executable does
1166
0
    //   not require any form of dynamic relocation. To handle these relocations
1167
0
    //   correctly, the IRELATIVE relocations are stored in an array which a
1168
0
    //   statically linked executable's startup code must enumerate using the
1169
0
    //   linker-defined symbols __rela?_iplt_{start,end}.
1170
0
    //
1171
0
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1172
0
    //   variable containing a pointer to the ifunc) needs to be relocated in
1173
0
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1174
0
    //   PLT entry canonical by translating such relocations into IRELATIVE
1175
0
    //   relocations in the RelaIplt.
1176
0
    if (!Sym.isInPlt()) {
1177
0
      // Create PLT and GOTPLT slots for the symbol.
1178
0
      Sym.IsInIplt = true;
1179
0
1180
0
      // Create a copy of the symbol to use as the target of the IRELATIVE
1181
0
      // relocation in the IgotPlt. This is in case we make the PLT canonical
1182
0
      // later, which would overwrite the original symbol.
1183
0
      //
1184
0
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1185
0
      // that's really needed to create the IRELATIVE is the section and value,
1186
0
      // so ideally we should just need to copy those.
1187
0
      auto *DirectSym = make<Defined>(cast<Defined>(Sym));
1188
0
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1189
0
                        *DirectSym);
1190
0
      Sym.PltIndex = DirectSym->PltIndex;
1191
0
    }
1192
0
    if (Expr == R_ABS && Addend == 0 && (Sec.Flags & SHF_WRITE)) {
1193
0
      // We might be able to represent this as an IRELATIVE. But we don't know
1194
0
      // yet whether some later relocation will make the symbol point to a
1195
0
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1196
0
      // static (non-PIC) relocation. So we keep a record of the information
1197
0
      // required to process the relocation, and after scanRelocs() has been
1198
0
      // called on all relocations, the relocation is resolved by
1199
0
      // addIRelativeRelocs().
1200
0
      IRelativeRelocs.push_back({Type, &Sec, Offset, &Sym});
1201
0
      return;
1202
0
    }
1203
0
    if (needsGot(Expr)) {
1204
0
      // Redirect GOT accesses to point to the Igot.
1205
0
      //
1206
0
      // This field is also used to keep track of whether we ever needed a GOT
1207
0
      // entry. If we did and we make the PLT canonical later, we'll need to
1208
0
      // create a GOT entry pointing to the PLT entry for Sym.
1209
0
      Sym.GotInIgot = true;
1210
0
    } else if (!needsPlt(Expr)) {
1211
0
      // Make the ifunc's PLT entry canonical by changing the value of its
1212
0
      // symbol to redirect all references to point to it.
1213
0
      unsigned EntryOffset = Sym.PltIndex * Target->PltEntrySize;
1214
0
      if (Config->ZRetpolineplt)
1215
0
        EntryOffset += Target->PltHeaderSize;
1216
0
1217
0
      auto &D = cast<Defined>(Sym);
1218
0
      D.Section = In.Iplt;
1219
0
      D.Value = EntryOffset;
1220
0
      D.Size = 0;
1221
0
      // It's important to set the symbol type here so that dynamic loaders
1222
0
      // don't try to call the PLT as if it were an ifunc resolver.
1223
0
      D.Type = STT_FUNC;
1224
0
1225
0
      if (Sym.GotInIgot) {
1226
0
        // We previously encountered a GOT generating reference that we
1227
0
        // redirected to the Igot. Now that the PLT entry is canonical we must
1228
0
        // clear the redirection to the Igot and add a GOT entry. As we've
1229
0
        // changed the symbol type to STT_FUNC future GOT generating references
1230
0
        // will naturally use this GOT entry.
1231
0
        //
1232
0
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1233
0
        // aren't a thing on MIPS.
1234
0
        Sym.GotInIgot = false;
1235
0
        addGotEntry(Sym);
1236
0
      }
1237
0
    }
1238
0
  }
1239
108
1240
108
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1241
108
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)1, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false> const*)
Line
Count
Source
1013
860
                      RelTy *End) {
1014
860
  const RelTy &Rel = *I;
1015
860
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
1016
860
  RelType Type;
1017
860
1018
860
  // Deal with MIPS oddity.
1019
860
  if (Config->MipsN32Abi) {
1020
0
    Type = getMipsN32RelType(I, End);
1021
860
  } else {
1022
860
    Type = Rel.getType(Config->IsMips64EL);
1023
860
    ++I;
1024
860
  }
1025
860
1026
860
  // Get an offset in an output section this relocation is applied to.
1027
860
  uint64_t Offset = GetOffset.get(Rel.r_offset);
1028
860
  if (Offset == uint64_t(-1))
1029
0
    return;
1030
860
1031
860
  // Skip if the target symbol is an erroneous undefined symbol.
1032
860
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
1033
1
    return;
1034
859
1035
859
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
1036
859
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
1037
859
1038
859
  // Ignore "hint" relocations because they are only markers for relaxation.
1039
859
  if (oneof<R_HINT, R_NONE>(Expr))
1040
23
    return;
1041
836
1042
836
  // We can separate the small code model relocations into 2 categories:
1043
836
  // 1) Those that access the compiler generated .toc sections.
1044
836
  // 2) Those that access the linker allocated got entries.
1045
836
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1046
836
  // the got entries in any way, we don't have to track which objects have
1047
836
  // got-based small code model relocs. The .toc sections get placed after the
1048
836
  // end of the linker allocated .got section and we do sort those so sections
1049
836
  // addressed with small code model relocations come first.
1050
836
  if (Config->EMachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(Type)0
)
1051
0
    Sec.File->PPC64SmallCodeModelTocRelocs = true;
1052
836
1053
836
  if (Sym.isGnuIFunc() && 
!Config->ZText16
&&
Config->WarnIfuncTextrel2
) {
1054
0
    warn("using ifunc symbols when text relocations are allowed may produce "
1055
0
         "a binary that will segfault, if the object file is linked with "
1056
0
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1057
0
         "you, consider recompiling the object files without -fPIC and "
1058
0
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1059
0
         "turn off this warning." +
1060
0
         getLocation(Sec, Sym, Offset));
1061
0
  }
1062
836
1063
836
  // Relax relocations.
1064
836
  //
1065
836
  // If we know that a PLT entry will be resolved within the same ELF module, we
1066
836
  // can skip PLT access and directly jump to the destination function. For
1067
836
  // example, if we are linking a main exectuable, all dynamic symbols that can
1068
836
  // be resolved within the executable will actually be resolved that way at
1069
836
  // runtime, because the main exectuable is always at the beginning of a search
1070
836
  // list. We can leverage that fact.
1071
836
  if (!Sym.IsPreemptible && 
(651
!Sym.isGnuIFunc()651
||
Config->ZIfuncNoplt16
)) {
1072
637
    if (Expr == R_GOT_PC && 
!isAbsoluteValue(Sym)6
)
1073
3
      Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1074
634
    else
1075
634
      Expr = fromPlt(Expr);
1076
637
  }
1077
836
1078
836
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1079
836
  // uses their addresses, we need GOT or GOTPLT to be created.
1080
836
  //
1081
836
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1082
836
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(Expr)) {
1083
47
    In.GotPlt->HasGotPltOffRel = true;
1084
789
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC_TOC, R_PPC64_RELAX_TOC>(Expr)) {
1085
3
    In.Got->HasGotOffRel = true;
1086
3
  }
1087
836
1088
836
  // Read an addend.
1089
836
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1090
836
1091
836
  // Process some TLS relocations, including relaxing TLS relocations.
1092
836
  // Note that this function does not handle all TLS relocations.
1093
836
  if (unsigned Processed =
1094
40
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1095
40
    I += (Processed - 1);
1096
40
    return;
1097
40
  }
1098
796
1099
796
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1100
796
  // direct relocation on through.
1101
796
  if (Sym.isGnuIFunc() && 
Config->ZIfuncNoplt16
) {
1102
2
    Sym.ExportDynamic = true;
1103
2
    In.RelaDyn->addReloc(Type, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
1104
2
    return;
1105
2
  }
1106
794
1107
794
  // Non-preemptible ifuncs require special handling. First, handle the usual
1108
794
  // case where the symbol isn't one of these.
1109
794
  if (!Sym.isGnuIFunc() || 
Sym.IsPreemptible14
) {
1110
780
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1111
780
    if (needsPlt(Expr) && 
!Sym.isInPlt()89
)
1112
69
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1113
780
1114
780
    // Create a GOT slot if a relocation needs GOT.
1115
780
    if (needsGot(Expr)) {
1116
65
      if (Config->EMachine == EM_MIPS) {
1117
6
        // MIPS ABI has special rules to process GOT entries and doesn't
1118
6
        // require relocation entries for them. A special case is TLS
1119
6
        // relocations. In that case dynamic loader applies dynamic
1120
6
        // relocations to initialize TLS GOT entries.
1121
6
        // See "Global Offset Table" in Chapter 5 in the following document
1122
6
        // for detailed description:
1123
6
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1124
6
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1125
59
      } else if (!Sym.isInGot()) {
1126
45
        addGotEntry(Sym);
1127
45
      }
1128
65
    }
1129
780
  } else {
1130
14
    // Handle a reference to a non-preemptible ifunc. These are special in a
1131
14
    // few ways:
1132
14
    //
1133
14
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1134
14
    //   a fixed value. But assuming that all references to the ifunc are
1135
14
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1136
14
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1137
14
    //   usually at the end of .plt, which makes an indirect call using a
1138
14
    //   matching GOT entry in IgotPlt, which is usually at the end of .got.plt.
1139
14
    //   The GOT entry is relocated using an IRELATIVE relocation in RelaIplt,
1140
14
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1141
14
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1142
14
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1143
14
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1144
14
    //   .got.plt without requiring a separate GOT entry.
1145
14
    //
1146
14
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1147
14
    //   that are not passed -fPIC will assume that they do, and will emit
1148
14
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1149
14
    //   symbol. This means that if a direct relocation to the symbol is
1150
14
    //   seen, the linker must set a value for the symbol, and this value must
1151
14
    //   be consistent no matter what type of reference is made to the symbol.
1152
14
    //   This can be done by creating a PLT entry for the symbol in the way
1153
14
    //   described above and making it canonical, that is, making all references
1154
14
    //   point to the PLT entry instead of the resolver. In lld we also store
1155
14
    //   the address of the PLT entry in the dynamic symbol table, which means
1156
14
    //   that the symbol will also have the same value in other modules.
1157
14
    //   Because the value loaded from the GOT needs to be consistent with
1158
14
    //   the value computed using a direct relocation, a non-preemptible ifunc
1159
14
    //   may end up with two GOT entries, one in .got.plt that points to the
1160
14
    //   address returned by the resolver and is used only by the PLT entry,
1161
14
    //   and another in .got that points to the PLT entry and is used by
1162
14
    //   GOT-generating relocations.
1163
14
    //
1164
14
    // - The fact that these symbols do not have a fixed value makes them an
1165
14
    //   exception to the general rule that a statically linked executable does
1166
14
    //   not require any form of dynamic relocation. To handle these relocations
1167
14
    //   correctly, the IRELATIVE relocations are stored in an array which a
1168
14
    //   statically linked executable's startup code must enumerate using the
1169
14
    //   linker-defined symbols __rela?_iplt_{start,end}.
1170
14
    //
1171
14
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1172
14
    //   variable containing a pointer to the ifunc) needs to be relocated in
1173
14
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1174
14
    //   PLT entry canonical by translating such relocations into IRELATIVE
1175
14
    //   relocations in the RelaIplt.
1176
14
    if (!Sym.isInPlt()) {
1177
13
      // Create PLT and GOTPLT slots for the symbol.
1178
13
      Sym.IsInIplt = true;
1179
13
1180
13
      // Create a copy of the symbol to use as the target of the IRELATIVE
1181
13
      // relocation in the IgotPlt. This is in case we make the PLT canonical
1182
13
      // later, which would overwrite the original symbol.
1183
13
      //
1184
13
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1185
13
      // that's really needed to create the IRELATIVE is the section and value,
1186
13
      // so ideally we should just need to copy those.
1187
13
      auto *DirectSym = make<Defined>(cast<Defined>(Sym));
1188
13
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1189
13
                        *DirectSym);
1190
13
      Sym.PltIndex = DirectSym->PltIndex;
1191
13
    }
1192
14
    if (Expr == R_ABS && 
Addend == 00
&&
(Sec.Flags & SHF_WRITE)0
) {
1193
0
      // We might be able to represent this as an IRELATIVE. But we don't know
1194
0
      // yet whether some later relocation will make the symbol point to a
1195
0
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1196
0
      // static (non-PIC) relocation. So we keep a record of the information
1197
0
      // required to process the relocation, and after scanRelocs() has been
1198
0
      // called on all relocations, the relocation is resolved by
1199
0
      // addIRelativeRelocs().
1200
0
      IRelativeRelocs.push_back({Type, &Sec, Offset, &Sym});
1201
0
      return;
1202
0
    }
1203
14
    if (needsGot(Expr)) {
1204
1
      // Redirect GOT accesses to point to the Igot.
1205
1
      //
1206
1
      // This field is also used to keep track of whether we ever needed a GOT
1207
1
      // entry. If we did and we make the PLT canonical later, we'll need to
1208
1
      // create a GOT entry pointing to the PLT entry for Sym.
1209
1
      Sym.GotInIgot = true;
1210
13
    } else if (!needsPlt(Expr)) {
1211
4
      // Make the ifunc's PLT entry canonical by changing the value of its
1212
4
      // symbol to redirect all references to point to it.
1213
4
      unsigned EntryOffset = Sym.PltIndex * Target->PltEntrySize;
1214
4
      if (Config->ZRetpolineplt)
1215
0
        EntryOffset += Target->PltHeaderSize;
1216
4
1217
4
      auto &D = cast<Defined>(Sym);
1218
4
      D.Section = In.Iplt;
1219
4
      D.Value = EntryOffset;
1220
4
      D.Size = 0;
1221
4
      // It's important to set the symbol type here so that dynamic loaders
1222
4
      // don't try to call the PLT as if it were an ifunc resolver.
1223
4
      D.Type = STT_FUNC;
1224
4
1225
4
      if (Sym.GotInIgot) {
1226
0
        // We previously encountered a GOT generating reference that we
1227
0
        // redirected to the Igot. Now that the PLT entry is canonical we must
1228
0
        // clear the redirection to the Igot and add a GOT entry. As we've
1229
0
        // changed the symbol type to STT_FUNC future GOT generating references
1230
0
        // will naturally use this GOT entry.
1231
0
        //
1232
0
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1233
0
        // aren't a thing on MIPS.
1234
0
        Sym.GotInIgot = false;
1235
0
        addGotEntry(Sym);
1236
0
      }
1237
4
    }
1238
14
  }
1239
794
1240
794
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1241
794
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, true> const*)
Line
Count
Source
1013
14
                      RelTy *End) {
1014
14
  const RelTy &Rel = *I;
1015
14
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
1016
14
  RelType Type;
1017
14
1018
14
  // Deal with MIPS oddity.
1019
14
  if (Config->MipsN32Abi) {
1020
5
    Type = getMipsN32RelType(I, End);
1021
9
  } else {
1022
9
    Type = Rel.getType(Config->IsMips64EL);
1023
9
    ++I;
1024
9
  }
1025
14
1026
14
  // Get an offset in an output section this relocation is applied to.
1027
14
  uint64_t Offset = GetOffset.get(Rel.r_offset);
1028
14
  if (Offset == uint64_t(-1))
1029
0
    return;
1030
14
1031
14
  // Skip if the target symbol is an erroneous undefined symbol.
1032
14
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
1033
0
    return;
1034
14
1035
14
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
1036
14
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
1037
14
1038
14
  // Ignore "hint" relocations because they are only markers for relaxation.
1039
14
  if (oneof<R_HINT, R_NONE>(Expr))
1040
0
    return;
1041
14
1042
14
  // We can separate the small code model relocations into 2 categories:
1043
14
  // 1) Those that access the compiler generated .toc sections.
1044
14
  // 2) Those that access the linker allocated got entries.
1045
14
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1046
14
  // the got entries in any way, we don't have to track which objects have
1047
14
  // got-based small code model relocs. The .toc sections get placed after the
1048
14
  // end of the linker allocated .got section and we do sort those so sections
1049
14
  // addressed with small code model relocations come first.
1050
14
  if (Config->EMachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(Type)0
)
1051
0
    Sec.File->PPC64SmallCodeModelTocRelocs = true;
1052
14
1053
14
  if (Sym.isGnuIFunc() && 
!Config->ZText0
&&
Config->WarnIfuncTextrel0
) {
1054
0
    warn("using ifunc symbols when text relocations are allowed may produce "
1055
0
         "a binary that will segfault, if the object file is linked with "
1056
0
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1057
0
         "you, consider recompiling the object files without -fPIC and "
1058
0
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1059
0
         "turn off this warning." +
1060
0
         getLocation(Sec, Sym, Offset));
1061
0
  }
1062
14
1063
14
  // Relax relocations.
1064
14
  //
1065
14
  // If we know that a PLT entry will be resolved within the same ELF module, we
1066
14
  // can skip PLT access and directly jump to the destination function. For
1067
14
  // example, if we are linking a main exectuable, all dynamic symbols that can
1068
14
  // be resolved within the executable will actually be resolved that way at
1069
14
  // runtime, because the main exectuable is always at the beginning of a search
1070
14
  // list. We can leverage that fact.
1071
14
  if (!Sym.IsPreemptible && 
(11
!Sym.isGnuIFunc()11
||
Config->ZIfuncNoplt0
)) {
1072
11
    if (Expr == R_GOT_PC && 
!isAbsoluteValue(Sym)0
)
1073
0
      Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1074
11
    else
1075
11
      Expr = fromPlt(Expr);
1076
11
  }
1077
14
1078
14
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1079
14
  // uses their addresses, we need GOT or GOTPLT to be created.
1080
14
  //
1081
14
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1082
14
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(Expr)) {
1083
0
    In.GotPlt->HasGotPltOffRel = true;
1084
14
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC_TOC, R_PPC64_RELAX_TOC>(Expr)) {
1085
0
    In.Got->HasGotOffRel = true;
1086
0
  }
1087
14
1088
14
  // Read an addend.
1089
14
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1090
14
1091
14
  // Process some TLS relocations, including relaxing TLS relocations.
1092
14
  // Note that this function does not handle all TLS relocations.
1093
14
  if (unsigned Processed =
1094
0
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1095
0
    I += (Processed - 1);
1096
0
    return;
1097
0
  }
1098
14
1099
14
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1100
14
  // direct relocation on through.
1101
14
  if (Sym.isGnuIFunc() && 
Config->ZIfuncNoplt0
) {
1102
0
    Sym.ExportDynamic = true;
1103
0
    In.RelaDyn->addReloc(Type, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
1104
0
    return;
1105
0
  }
1106
14
1107
14
  // Non-preemptible ifuncs require special handling. First, handle the usual
1108
14
  // case where the symbol isn't one of these.
1109
14
  if (!Sym.isGnuIFunc() || 
Sym.IsPreemptible0
) {
1110
14
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1111
14
    if (needsPlt(Expr) && 
!Sym.isInPlt()2
)
1112
2
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1113
14
1114
14
    // Create a GOT slot if a relocation needs GOT.
1115
14
    if (needsGot(Expr)) {
1116
0
      if (Config->EMachine == EM_MIPS) {
1117
0
        // MIPS ABI has special rules to process GOT entries and doesn't
1118
0
        // require relocation entries for them. A special case is TLS
1119
0
        // relocations. In that case dynamic loader applies dynamic
1120
0
        // relocations to initialize TLS GOT entries.
1121
0
        // See "Global Offset Table" in Chapter 5 in the following document
1122
0
        // for detailed description:
1123
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1124
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1125
0
      } else if (!Sym.isInGot()) {
1126
0
        addGotEntry(Sym);
1127
0
      }
1128
0
    }
1129
14
  } else {
1130
0
    // Handle a reference to a non-preemptible ifunc. These are special in a
1131
0
    // few ways:
1132
0
    //
1133
0
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1134
0
    //   a fixed value. But assuming that all references to the ifunc are
1135
0
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1136
0
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1137
0
    //   usually at the end of .plt, which makes an indirect call using a
1138
0
    //   matching GOT entry in IgotPlt, which is usually at the end of .got.plt.
1139
0
    //   The GOT entry is relocated using an IRELATIVE relocation in RelaIplt,
1140
0
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1141
0
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1142
0
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1143
0
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1144
0
    //   .got.plt without requiring a separate GOT entry.
1145
0
    //
1146
0
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1147
0
    //   that are not passed -fPIC will assume that they do, and will emit
1148
0
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1149
0
    //   symbol. This means that if a direct relocation to the symbol is
1150
0
    //   seen, the linker must set a value for the symbol, and this value must
1151
0
    //   be consistent no matter what type of reference is made to the symbol.
1152
0
    //   This can be done by creating a PLT entry for the symbol in the way
1153
0
    //   described above and making it canonical, that is, making all references
1154
0
    //   point to the PLT entry instead of the resolver. In lld we also store
1155
0
    //   the address of the PLT entry in the dynamic symbol table, which means
1156
0
    //   that the symbol will also have the same value in other modules.
1157
0
    //   Because the value loaded from the GOT needs to be consistent with
1158
0
    //   the value computed using a direct relocation, a non-preemptible ifunc
1159
0
    //   may end up with two GOT entries, one in .got.plt that points to the
1160
0
    //   address returned by the resolver and is used only by the PLT entry,
1161
0
    //   and another in .got that points to the PLT entry and is used by
1162
0
    //   GOT-generating relocations.
1163
0
    //
1164
0
    // - The fact that these symbols do not have a fixed value makes them an
1165
0
    //   exception to the general rule that a statically linked executable does
1166
0
    //   not require any form of dynamic relocation. To handle these relocations
1167
0
    //   correctly, the IRELATIVE relocations are stored in an array which a
1168
0
    //   statically linked executable's startup code must enumerate using the
1169
0
    //   linker-defined symbols __rela?_iplt_{start,end}.
1170
0
    //
1171
0
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1172
0
    //   variable containing a pointer to the ifunc) needs to be relocated in
1173
0
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1174
0
    //   PLT entry canonical by translating such relocations into IRELATIVE
1175
0
    //   relocations in the RelaIplt.
1176
0
    if (!Sym.isInPlt()) {
1177
0
      // Create PLT and GOTPLT slots for the symbol.
1178
0
      Sym.IsInIplt = true;
1179
0
1180
0
      // Create a copy of the symbol to use as the target of the IRELATIVE
1181
0
      // relocation in the IgotPlt. This is in case we make the PLT canonical
1182
0
      // later, which would overwrite the original symbol.
1183
0
      //
1184
0
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1185
0
      // that's really needed to create the IRELATIVE is the section and value,
1186
0
      // so ideally we should just need to copy those.
1187
0
      auto *DirectSym = make<Defined>(cast<Defined>(Sym));
1188
0
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1189
0
                        *DirectSym);
1190
0
      Sym.PltIndex = DirectSym->PltIndex;
1191
0
    }
1192
0
    if (Expr == R_ABS && Addend == 0 && (Sec.Flags & SHF_WRITE)) {
1193
0
      // We might be able to represent this as an IRELATIVE. But we don't know
1194
0
      // yet whether some later relocation will make the symbol point to a
1195
0
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1196
0
      // static (non-PIC) relocation. So we keep a record of the information
1197
0
      // required to process the relocation, and after scanRelocs() has been
1198
0
      // called on all relocations, the relocation is resolved by
1199
0
      // addIRelativeRelocs().
1200
0
      IRelativeRelocs.push_back({Type, &Sec, Offset, &Sym});
1201
0
      return;
1202
0
    }
1203
0
    if (needsGot(Expr)) {
1204
0
      // Redirect GOT accesses to point to the Igot.
1205
0
      //
1206
0
      // This field is also used to keep track of whether we ever needed a GOT
1207
0
      // entry. If we did and we make the PLT canonical later, we'll need to
1208
0
      // create a GOT entry pointing to the PLT entry for Sym.
1209
0
      Sym.GotInIgot = true;
1210
0
    } else if (!needsPlt(Expr)) {
1211
0
      // Make the ifunc's PLT entry canonical by changing the value of its
1212
0
      // symbol to redirect all references to point to it.
1213
0
      unsigned EntryOffset = Sym.PltIndex * Target->PltEntrySize;
1214
0
      if (Config->ZRetpolineplt)
1215
0
        EntryOffset += Target->PltHeaderSize;
1216
0
1217
0
      auto &D = cast<Defined>(Sym);
1218
0
      D.Section = In.Iplt;
1219
0
      D.Value = EntryOffset;
1220
0
      D.Size = 0;
1221
0
      // It's important to set the symbol type here so that dynamic loaders
1222
0
      // don't try to call the PLT as if it were an ifunc resolver.
1223
0
      D.Type = STT_FUNC;
1224
0
1225
0
      if (Sym.GotInIgot) {
1226
0
        // We previously encountered a GOT generating reference that we
1227
0
        // redirected to the Igot. Now that the PLT entry is canonical we must
1228
0
        // clear the redirection to the Igot and add a GOT entry. As we've
1229
0
        // changed the symbol type to STT_FUNC future GOT generating references
1230
0
        // will naturally use this GOT entry.
1231
0
        //
1232
0
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1233
0
        // aren't a thing on MIPS.
1234
0
        Sym.GotInIgot = false;
1235
0
        addGotEntry(Sym);
1236
0
      }
1237
0
    }
1238
0
  }
1239
14
1240
14
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1241
14
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)0, false>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false> const*)
Line
Count
Source
1013
271
                      RelTy *End) {
1014
271
  const RelTy &Rel = *I;
1015
271
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
1016
271
  RelType Type;
1017
271
1018
271
  // Deal with MIPS oddity.
1019
271
  if (Config->MipsN32Abi) {
1020
0
    Type = getMipsN32RelType(I, End);
1021
271
  } else {
1022
271
    Type = Rel.getType(Config->IsMips64EL);
1023
271
    ++I;
1024
271
  }
1025
271
1026
271
  // Get an offset in an output section this relocation is applied to.
1027
271
  uint64_t Offset = GetOffset.get(Rel.r_offset);
1028
271
  if (Offset == uint64_t(-1))
1029
0
    return;
1030
271
1031
271
  // Skip if the target symbol is an erroneous undefined symbol.
1032
271
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
1033
0
    return;
1034
271
1035
271
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
1036
271
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
1037
271
1038
271
  // Ignore "hint" relocations because they are only markers for relaxation.
1039
271
  if (oneof<R_HINT, R_NONE>(Expr))
1040
0
    return;
1041
271
1042
271
  // We can separate the small code model relocations into 2 categories:
1043
271
  // 1) Those that access the compiler generated .toc sections.
1044
271
  // 2) Those that access the linker allocated got entries.
1045
271
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1046
271
  // the got entries in any way, we don't have to track which objects have
1047
271
  // got-based small code model relocs. The .toc sections get placed after the
1048
271
  // end of the linker allocated .got section and we do sort those so sections
1049
271
  // addressed with small code model relocations come first.
1050
271
  if (Config->EMachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(Type)0
)
1051
0
    Sec.File->PPC64SmallCodeModelTocRelocs = true;
1052
271
1053
271
  if (Sym.isGnuIFunc() && 
!Config->ZText0
&&
Config->WarnIfuncTextrel0
) {
1054
0
    warn("using ifunc symbols when text relocations are allowed may produce "
1055
0
         "a binary that will segfault, if the object file is linked with "
1056
0
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1057
0
         "you, consider recompiling the object files without -fPIC and "
1058
0
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1059
0
         "turn off this warning." +
1060
0
         getLocation(Sec, Sym, Offset));
1061
0
  }
1062
271
1063
271
  // Relax relocations.
1064
271
  //
1065
271
  // If we know that a PLT entry will be resolved within the same ELF module, we
1066
271
  // can skip PLT access and directly jump to the destination function. For
1067
271
  // example, if we are linking a main exectuable, all dynamic symbols that can
1068
271
  // be resolved within the executable will actually be resolved that way at
1069
271
  // runtime, because the main exectuable is always at the beginning of a search
1070
271
  // list. We can leverage that fact.
1071
271
  if (!Sym.IsPreemptible && 
(205
!Sym.isGnuIFunc()205
||
Config->ZIfuncNoplt0
)) {
1072
205
    if (Expr == R_GOT_PC && 
!isAbsoluteValue(Sym)0
)
1073
0
      Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1074
205
    else
1075
205
      Expr = fromPlt(Expr);
1076
205
  }
1077
271
1078
271
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1079
271
  // uses their addresses, we need GOT or GOTPLT to be created.
1080
271
  //
1081
271
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1082
271
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(Expr)) {
1083
0
    In.GotPlt->HasGotPltOffRel = true;
1084
271
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC_TOC, R_PPC64_RELAX_TOC>(Expr)) {
1085
0
    In.Got->HasGotOffRel = true;
1086
0
  }
1087
271
1088
271
  // Read an addend.
1089
271
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1090
271
1091
271
  // Process some TLS relocations, including relaxing TLS relocations.
1092
271
  // Note that this function does not handle all TLS relocations.
1093
271
  if (unsigned Processed =
1094
11
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1095
11
    I += (Processed - 1);
1096
11
    return;
1097
11
  }
1098
260
1099
260
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1100
260
  // direct relocation on through.
1101
260
  if (Sym.isGnuIFunc() && 
Config->ZIfuncNoplt0
) {
1102
0
    Sym.ExportDynamic = true;
1103
0
    In.RelaDyn->addReloc(Type, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
1104
0
    return;
1105
0
  }
1106
260
1107
260
  // Non-preemptible ifuncs require special handling. First, handle the usual
1108
260
  // case where the symbol isn't one of these.
1109
260
  if (!Sym.isGnuIFunc() || 
Sym.IsPreemptible0
) {
1110
260
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1111
260
    if (needsPlt(Expr) && 
!Sym.isInPlt()8
)
1112
7
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1113
260
1114
260
    // Create a GOT slot if a relocation needs GOT.
1115
260
    if (needsGot(Expr)) {
1116
80
      if (Config->EMachine == EM_MIPS) {
1117
80
        // MIPS ABI has special rules to process GOT entries and doesn't
1118
80
        // require relocation entries for them. A special case is TLS
1119
80
        // relocations. In that case dynamic loader applies dynamic
1120
80
        // relocations to initialize TLS GOT entries.
1121
80
        // See "Global Offset Table" in Chapter 5 in the following document
1122
80
        // for detailed description:
1123
80
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1124
80
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1125
80
      } else 
if (0
!Sym.isInGot()0
) {
1126
0
        addGotEntry(Sym);
1127
0
      }
1128
80
    }
1129
260
  } else {
1130
0
    // Handle a reference to a non-preemptible ifunc. These are special in a
1131
0
    // few ways:
1132
0
    //
1133
0
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1134
0
    //   a fixed value. But assuming that all references to the ifunc are
1135
0
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1136
0
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1137
0
    //   usually at the end of .plt, which makes an indirect call using a
1138
0
    //   matching GOT entry in IgotPlt, which is usually at the end of .got.plt.
1139
0
    //   The GOT entry is relocated using an IRELATIVE relocation in RelaIplt,
1140
0
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1141
0
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1142
0
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1143
0
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1144
0
    //   .got.plt without requiring a separate GOT entry.
1145
0
    //
1146
0
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1147
0
    //   that are not passed -fPIC will assume that they do, and will emit
1148
0
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1149
0
    //   symbol. This means that if a direct relocation to the symbol is
1150
0
    //   seen, the linker must set a value for the symbol, and this value must
1151
0
    //   be consistent no matter what type of reference is made to the symbol.
1152
0
    //   This can be done by creating a PLT entry for the symbol in the way
1153
0
    //   described above and making it canonical, that is, making all references
1154
0
    //   point to the PLT entry instead of the resolver. In lld we also store
1155
0
    //   the address of the PLT entry in the dynamic symbol table, which means
1156
0
    //   that the symbol will also have the same value in other modules.
1157
0
    //   Because the value loaded from the GOT needs to be consistent with
1158
0
    //   the value computed using a direct relocation, a non-preemptible ifunc
1159
0
    //   may end up with two GOT entries, one in .got.plt that points to the
1160
0
    //   address returned by the resolver and is used only by the PLT entry,
1161
0
    //   and another in .got that points to the PLT entry and is used by
1162
0
    //   GOT-generating relocations.
1163
0
    //
1164
0
    // - The fact that these symbols do not have a fixed value makes them an
1165
0
    //   exception to the general rule that a statically linked executable does
1166
0
    //   not require any form of dynamic relocation. To handle these relocations
1167
0
    //   correctly, the IRELATIVE relocations are stored in an array which a
1168
0
    //   statically linked executable's startup code must enumerate using the
1169
0
    //   linker-defined symbols __rela?_iplt_{start,end}.
1170
0
    //
1171
0
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1172
0
    //   variable containing a pointer to the ifunc) needs to be relocated in
1173
0
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1174
0
    //   PLT entry canonical by translating such relocations into IRELATIVE
1175
0
    //   relocations in the RelaIplt.
1176
0
    if (!Sym.isInPlt()) {
1177
0
      // Create PLT and GOTPLT slots for the symbol.
1178
0
      Sym.IsInIplt = true;
1179
0
1180
0
      // Create a copy of the symbol to use as the target of the IRELATIVE
1181
0
      // relocation in the IgotPlt. This is in case we make the PLT canonical
1182
0
      // later, which would overwrite the original symbol.
1183
0
      //
1184
0
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1185
0
      // that's really needed to create the IRELATIVE is the section and value,
1186
0
      // so ideally we should just need to copy those.
1187
0
      auto *DirectSym = make<Defined>(cast<Defined>(Sym));
1188
0
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1189
0
                        *DirectSym);
1190
0
      Sym.PltIndex = DirectSym->PltIndex;
1191
0
    }
1192
0
    if (Expr == R_ABS && Addend == 0 && (Sec.Flags & SHF_WRITE)) {
1193
0
      // We might be able to represent this as an IRELATIVE. But we don't know
1194
0
      // yet whether some later relocation will make the symbol point to a
1195
0
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1196
0
      // static (non-PIC) relocation. So we keep a record of the information
1197
0
      // required to process the relocation, and after scanRelocs() has been
1198
0
      // called on all relocations, the relocation is resolved by
1199
0
      // addIRelativeRelocs().
1200
0
      IRelativeRelocs.push_back({Type, &Sec, Offset, &Sym});
1201
0
      return;
1202
0
    }
1203
0
    if (needsGot(Expr)) {
1204
0
      // Redirect GOT accesses to point to the Igot.
1205
0
      //
1206
0
      // This field is also used to keep track of whether we ever needed a GOT
1207
0
      // entry. If we did and we make the PLT canonical later, we'll need to
1208
0
      // create a GOT entry pointing to the PLT entry for Sym.
1209
0
      Sym.GotInIgot = true;
1210
0
    } else if (!needsPlt(Expr)) {
1211
0
      // Make the ifunc's PLT entry canonical by changing the value of its
1212
0
      // symbol to redirect all references to point to it.
1213
0
      unsigned EntryOffset = Sym.PltIndex * Target->PltEntrySize;
1214
0
      if (Config->ZRetpolineplt)
1215
0
        EntryOffset += Target->PltHeaderSize;
1216
0
1217
0
      auto &D = cast<Defined>(Sym);
1218
0
      D.Section = In.Iplt;
1219
0
      D.Value = EntryOffset;
1220
0
      D.Size = 0;
1221
0
      // It's important to set the symbol type here so that dynamic loaders
1222
0
      // don't try to call the PLT as if it were an ifunc resolver.
1223
0
      D.Type = STT_FUNC;
1224
0
1225
0
      if (Sym.GotInIgot) {
1226
0
        // We previously encountered a GOT generating reference that we
1227
0
        // redirected to the Igot. Now that the PLT entry is canonical we must
1228
0
        // clear the redirection to the Igot and add a GOT entry. As we've
1229
0
        // changed the symbol type to STT_FUNC future GOT generating references
1230
0
        // will naturally use this GOT entry.
1231
0
        //
1232
0
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1233
0
        // aren't a thing on MIPS.
1234
0
        Sym.GotInIgot = false;
1235
0
        addGotEntry(Sym);
1236
0
      }
1237
0
    }
1238
0
  }
1239
260
1240
260
  processRelocAux<ELFT>(Sec, Expr, Type, Offset, Sym, Rel, Addend);
1241
260
}
Relocations.cpp:void scanReloc<llvm::object::ELFType<(llvm::support::endianness)1, true>, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const>(lld::elf::InputSectionBase&, (anonymous namespace)::OffsetGetter&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*&, llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, true> const*)
Line
Count
Source
1013
2.50k
                      RelTy *End) {
1014
2.50k
  const RelTy &Rel = *I;
1015
2.50k
  Symbol &Sym = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
1016
2.50k
  RelType Type;
1017
2.50k
1018
2.50k
  // Deal with MIPS oddity.
1019
2.50k
  if (Config->MipsN32Abi) {
1020
0
    Type = getMipsN32RelType(I, End);
1021
2.50k
  } else {
1022
2.50k
    Type = Rel.getType(Config->IsMips64EL);
1023
2.50k
    ++I;
1024
2.50k
  }
1025
2.50k
1026
2.50k
  // Get an offset in an output section this relocation is applied to.
1027
2.50k
  uint64_t Offset = GetOffset.get(Rel.r_offset);
1028
2.50k
  if (Offset == uint64_t(-1))
1029
13
    return;
1030
2.49k
1031
2.49k
  // Skip if the target symbol is an erroneous undefined symbol.
1032
2.49k
  if (maybeReportUndefined(Sym, Sec, Rel.r_offset))
1033
78
    return;
1034
2.41k
1035
2.41k
  const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset;
1036
2.41k
  RelExpr Expr = Target->getRelExpr(Type, Sym, RelocatedAddr);
1037
2.41k
1038
2.41k
  // Ignore "hint" relocations because they are only markers for relaxation.
1039
2.41k
  if (oneof<R_HINT, R_NONE>(Expr))
1040
7
    return;
1041
2.40k
1042
2.40k
  // We can separate the small code model relocations into 2 categories:
1043
2.40k
  // 1) Those that access the compiler generated .toc sections.
1044
2.40k
  // 2) Those that access the linker allocated got entries.
1045
2.40k
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1046
2.40k
  // the got entries in any way, we don't have to track which objects have
1047
2.40k
  // got-based small code model relocs. The .toc sections get placed after the
1048
2.40k
  // end of the linker allocated .got section and we do sort those so sections
1049
2.40k
  // addressed with small code model relocations come first.
1050
2.40k
  if (Config->EMachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(Type)624
)
1051
15
    Sec.File->PPC64SmallCodeModelTocRelocs = true;
1052
2.40k
1053
2.40k
  if (Sym.isGnuIFunc() && 
!Config->ZText69
&&
Config->WarnIfuncTextrel4
) {
1054
1
    warn("using ifunc symbols when text relocations are allowed may produce "
1055
1
         "a binary that will segfault, if the object file is linked with "
1056
1
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1057
1
         "you, consider recompiling the object files without -fPIC and "
1058
1
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1059
1
         "turn off this warning." +
1060
1
         getLocation(Sec, Sym, Offset));
1061
1
  }
1062
2.40k
1063
2.40k
  // Relax relocations.
1064
2.40k
  //
1065
2.40k
  // If we know that a PLT entry will be resolved within the same ELF module, we
1066
2.40k
  // can skip PLT access and directly jump to the destination function. For
1067
2.40k
  // example, if we are linking a main exectuable, all dynamic symbols that can
1068
2.40k
  // be resolved within the executable will actually be resolved that way at
1069
2.40k
  // runtime, because the main exectuable is always at the beginning of a search
1070
2.40k
  // list. We can leverage that fact.
1071
2.40k
  if (!Sym.IsPreemptible && 
(1.82k
!Sym.isGnuIFunc()1.82k
||
Config->ZIfuncNoplt66
)) {
1072
1.75k
    if (Expr == R_GOT_PC && 
!isAbsoluteValue(Sym)89
)
1073
52
      Expr = Target->adjustRelaxExpr(Type, RelocatedAddr, Expr);
1074
1.70k
    else
1075
1.70k
      Expr = fromPlt(Expr);
1076
1.75k
  }
1077
2.40k
1078
2.40k
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1079
2.40k
  // uses their addresses, we need GOT or GOTPLT to be created.
1080
2.40k
  //
1081
2.40k
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1082
2.40k
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(Expr)) {
1083
7
    In.GotPlt->HasGotPltOffRel = true;
1084
2.39k
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC_TOC, R_PPC64_RELAX_TOC>(Expr)) {
1085
157
    In.Got->HasGotOffRel = true;
1086
157
  }
1087
2.40k
1088
2.40k
  // Read an addend.
1089
2.40k
  int64_t Addend = computeAddend<ELFT>(Rel, End, Sec, Expr, Sym.isLocal());
1090
2.40k
1091
2.40k
  // Process some TLS relocations, including relaxing TLS relocations.
1092
2.40k
  // Note that this function does not handle all TLS relocations.
1093
2.40k
  if (unsigned Processed =
1094
174
          handleTlsRelocation<ELFT>(Type, Sym, Sec, Offset, Addend, Expr)) {
1095
174
    I += (Processed - 1);
1096
174
    return;
1097
174
  }
1098
2.23k
1099
2.23k
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1100
2.23k
  // direct relocation on through.
1101
2.23k
  if (Sym.isGnuIFunc() && 
Config->ZIfuncNoplt69
) {
1102
2
    Sym.ExportDynamic = true;
1103
2
    In.RelaDyn->addReloc(Type, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
1104
2
    return;
1105
2
  }
1106
2.22k
1107
2.22k
  // Non-preemptible ifuncs require special handling. First, handle the usual
1108
2.22k
  // case where the symbol isn't one of these.
1109
2.22k
  if (!Sym.isGnuIFunc() || 
Sym.IsPreemptible67
) {
1110
2.16k
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1111
2.16k
    if (needsPlt(Expr) && 
!Sym.isInPlt()210
)
1112
188
      addPltEntry<ELFT>(In.Plt, In.GotPlt, In.RelaPlt, Target->PltRel, Sym);
1113
2.16k
1114
2.16k
    // Create a GOT slot if a relocation needs GOT.
1115
2.16k
    if (needsGot(Expr)) {
1116
177
      if (Config->EMachine == EM_MIPS) {
1117
0
        // MIPS ABI has special rules to process GOT entries and doesn't
1118
0
        // require relocation entries for them. A special case is TLS
1119
0
        // relocations. In that case dynamic loader applies dynamic
1120
0
        // relocations to initialize TLS GOT entries.
1121
0
        // See "Global Offset Table" in Chapter 5 in the following document
1122
0
        // for detailed description:
1123
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1124
0
        In.MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
1125
177
      } else if (!Sym.isInGot()) {
1126
96
        addGotEntry(Sym);
1127
96
      }
1128
177
    }
1129
2.16k
  } else {
1130
64
    // Handle a reference to a non-preemptible ifunc. These are special in a
1131
64
    // few ways:
1132
64
    //
1133
64
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1134
64
    //   a fixed value. But assuming that all references to the ifunc are
1135
64
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1136
64
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1137
64
    //   usually at the end of .plt, which makes an indirect call using a
1138
64
    //   matching GOT entry in IgotPlt, which is usually at the end of .got.plt.
1139
64
    //   The GOT entry is relocated using an IRELATIVE relocation in RelaIplt,
1140
64
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1141
64
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1142
64
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1143
64
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1144
64
    //   .got.plt without requiring a separate GOT entry.
1145
64
    //
1146
64
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1147
64
    //   that are not passed -fPIC will assume that they do, and will emit
1148
64
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1149
64
    //   symbol. This means that if a direct relocation to the symbol is
1150
64
    //   seen, the linker must set a value for the symbol, and this value must
1151
64
    //   be consistent no matter what type of reference is made to the symbol.
1152
64
    //   This can be done by creating a PLT entry for the symbol in the way
1153
64
    //   described above and making it canonical, that is, making all references
1154
64
    //   point to the PLT entry instead of the resolver. In lld we also store
1155
64
    //   the address of the PLT entry in the dynamic symbol table, which means
1156
64
    //   that the symbol will also have the same value in other modules.
1157
64
    //   Because the value loaded from the GOT needs to be consistent with
1158
64
    //   the value computed using a direct relocation, a non-preemptible ifunc
1159
64
    //   may end up with two GOT entries, one in .got.plt that points to the
1160
64
    //   address returned by the resolver and is used only by the PLT entry,
1161
64
    //   and another in .got that points to the PLT entry and is used by
1162
64
    //   GOT-generating relocations.
1163
64
    //
1164
64
    // - The fact that these symbols do not have a fixed value makes them an
1165
64
    //   exception to the general rule that a statically linked executable does
1166
64
    //   not require any form of dynamic relocation. To handle these relocations
1167
64
    //   correctly, the IRELATIVE relocations are stored in an array which a
1168
64
    //   statically linked executable's startup code must enumerate using the
1169
64
    //   linker-defined symbols __rela?_iplt_{start,end}.
1170
64
    //
1171
64
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1172
64
    //   variable containing a pointer to the ifunc) needs to be relocated in
1173
64
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1174
64
    //   PLT entry canonical by translating such relocations into IRELATIVE
1175
64
    //   relocations in the RelaIplt.
1176
64
    if (!Sym.isInPlt()) {
1177
42
      // Create PLT and GOTPLT slots for the symbol.
1178
42
      Sym.IsInIplt = true;
1179
42
1180
42
      // Create a copy of the symbol to use as the target of the IRELATIVE
1181
42
      // relocation in the IgotPlt. This is in case we make the PLT canonical
1182
42
      // later, which would overwrite the original symbol.
1183
42
      //
1184
42
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1185
42
      // that's really needed to create the IRELATIVE is the section and value,
1186
42
      // so ideally we should just need to copy those.
1187
42
      auto *DirectSym = make<Defined>(cast<Defined>(Sym));
1188
42
      addPltEntry<ELFT>(In.Iplt, In.IgotPlt, In.RelaIplt, Target->IRelativeRel,
1189
42
                        *DirectSym);
1190
42
      Sym.PltIndex = DirectSym->PltIndex;
1191
42
    }
1192
64
    if (Expr == R_ABS && 
Addend == 015
&&
(Sec.Flags & SHF_WRITE)10
) {
1193
8
      // We might be able to represent this as an IRELATIVE. But we don't know
1194
8
      // yet whether some later relocation will make the symbol point to a
1195
8
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1196
8
      // static (non-PIC) relocation. So we keep a record of the information
1197
8
      // required to process the relocation, and after scanRelocs() has been
1198
8
      // called on all relocations, the relocation is resolved by
1199
8
      // addIRelativeRelocs().
1200
8
      IRelativeRelocs.push_back({Type, &Sec, Offset, &Sym});
1201
8
      return;
1202
8
    }
1203
56
    if (needsGot(Expr)) {
1204
18
      // Redirect GOT accesses to point to the Igot.
1205
18
      //
1206
18
      // This field is also used to keep track of w