Coverage Report

Created: 2019-07-24 05:18

/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
1
static Optional<std::string> getLinkerScriptLocation(const Symbol &sym) {
69
1
  for (BaseCommand *base : script->sectionCommands)
70
22
    if (auto *cmd = dyn_cast<SymbolAssignment>(base))
71
1
      if (cmd->sym == &sym)
72
1
        return cmd->location;
73
1
  
return None0
;
74
1
}
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
67
                               uint64_t off) {
83
67
  std::string msg = "\n>>> defined in ";
84
67
  if (sym.file)
85
66
    msg += toString(sym.file);
86
1
  else if (Optional<std::string> loc = getLinkerScriptLocation(sym))
87
1
    msg += *loc;
88
67
89
67
  msg += "\n>>> referenced by ";
90
67
  std::string src = s.getSrcMsg(sym, off);
91
67
  if (!src.empty())
92
2
    msg += src + "\n>>>               ";
93
67
  return msg + s.getObjMsg(off);
94
67
}
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
89.2k
  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
760k
  static inline uint64_t build() {
111
760k
    static_assert(0 <= Head && Head < 64,
112
760k
                  "RelExpr is too large for 64-bit mask!");
113
760k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
760k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)11, (lld::elf::RelExpr)13>::build()
Line
Count
Source
110
14.6k
  static inline uint64_t build() {
111
14.6k
    static_assert(0 <= Head && Head < 64,
112
14.6k
                  "RelExpr is too large for 64-bit mask!");
113
14.6k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.6k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)13>::build()
Line
Count
Source
110
14.6k
  static inline uint64_t build() {
111
14.6k
    static_assert(0 <= Head && Head < 64,
112
14.6k
                  "RelExpr is too large for 64-bit mask!");
113
14.6k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.6k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)7, (lld::elf::RelExpr)9, (lld::elf::RelExpr)8, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
14.6k
  static inline uint64_t build() {
111
14.6k
    static_assert(0 <= Head && Head < 64,
112
14.6k
                  "RelExpr is too large for 64-bit mask!");
113
14.6k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.6k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)9, (lld::elf::RelExpr)8, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
14.6k
  static inline uint64_t build() {
111
14.6k
    static_assert(0 <= Head && Head < 64,
112
14.6k
                  "RelExpr is too large for 64-bit mask!");
113
14.6k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.6k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8, (lld::elf::RelExpr)34>::build()
Line
Count
Source
110
14.6k
  static inline uint64_t build() {
111
14.6k
    static_assert(0 <= Head && Head < 64,
112
14.6k
                  "RelExpr is too large for 64-bit mask!");
113
14.6k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.6k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)34>::build()
Line
Count
Source
110
14.6k
  static inline uint64_t build() {
111
14.6k
    static_assert(0 <= Head && Head < 64,
112
14.6k
                  "RelExpr is too large for 64-bit mask!");
113
14.6k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.6k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)6, (lld::elf::RelExpr)10, (lld::elf::RelExpr)60, (lld::elf::RelExpr)59>::build()
Line
Count
Source
110
14.5k
  static inline uint64_t build() {
111
14.5k
    static_assert(0 <= Head && Head < 64,
112
14.5k
                  "RelExpr is too large for 64-bit mask!");
113
14.5k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.5k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)10, (lld::elf::RelExpr)60, (lld::elf::RelExpr)59>::build()
Line
Count
Source
110
14.5k
  static inline uint64_t build() {
111
14.5k
    static_assert(0 <= Head && Head < 64,
112
14.5k
                  "RelExpr is too large for 64-bit mask!");
113
14.5k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.5k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)60, (lld::elf::RelExpr)59>::build()
Line
Count
Source
110
14.5k
  static inline uint64_t build() {
111
14.5k
    static_assert(0 <= Head && Head < 64,
112
14.5k
                  "RelExpr is too large for 64-bit mask!");
113
14.5k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.5k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)59>::build()
Line
Count
Source
110
14.5k
  static inline uint64_t build() {
111
14.5k
    static_assert(0 <= Head && Head < 64,
112
14.5k
                  "RelExpr is too large for 64-bit mask!");
113
14.5k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.5k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)45, (lld::elf::RelExpr)30, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32>::build()
Line
Count
Source
110
597
  static inline uint64_t build() {
111
597
    static_assert(0 <= Head && Head < 64,
112
597
                  "RelExpr is too large for 64-bit mask!");
113
597
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
597
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)30, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32>::build()
Line
Count
Source
110
597
  static inline uint64_t build() {
111
597
    static_assert(0 <= Head && Head < 64,
112
597
                  "RelExpr is too large for 64-bit mask!");
113
597
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
597
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)31, (lld::elf::RelExpr)32>::build()
Line
Count
Source
110
597
  static inline uint64_t build() {
111
597
    static_assert(0 <= Head && Head < 64,
112
597
                  "RelExpr is too large for 64-bit mask!");
113
597
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
597
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)32>::build()
Line
Count
Source
110
597
  static inline uint64_t build() {
111
597
    static_assert(0 <= Head && Head < 64,
112
597
                  "RelExpr is too large for 64-bit mask!");
113
597
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
597
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)37, (lld::elf::RelExpr)38, (lld::elf::RelExpr)41, (lld::elf::RelExpr)40>::build()
Line
Count
Source
110
569
  static inline uint64_t build() {
111
569
    static_assert(0 <= Head && Head < 64,
112
569
                  "RelExpr is too large for 64-bit mask!");
113
569
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
569
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)38, (lld::elf::RelExpr)41, (lld::elf::RelExpr)40>::build()
Line
Count
Source
110
569
  static inline uint64_t build() {
111
569
    static_assert(0 <= Head && Head < 64,
112
569
                  "RelExpr is too large for 64-bit mask!");
113
569
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
569
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)41, (lld::elf::RelExpr)40>::build()
Line
Count
Source
110
569
  static inline uint64_t build() {
111
569
    static_assert(0 <= Head && Head < 64,
112
569
                  "RelExpr is too large for 64-bit mask!");
113
569
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
569
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)40>::build()
Line
Count
Source
110
569
  static inline uint64_t build() {
111
569
    static_assert(0 <= Head && Head < 64,
112
569
                  "RelExpr is too large for 64-bit mask!");
113
569
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
569
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)45, (lld::elf::RelExpr)30, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
475
  static inline uint64_t build() {
111
475
    static_assert(0 <= Head && Head < 64,
112
475
                  "RelExpr is too large for 64-bit mask!");
113
475
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
475
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)30, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
475
  static inline uint64_t build() {
111
475
    static_assert(0 <= Head && Head < 64,
112
475
                  "RelExpr is too large for 64-bit mask!");
113
475
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
475
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
475
  static inline uint64_t build() {
111
475
    static_assert(0 <= Head && Head < 64,
112
475
                  "RelExpr is too large for 64-bit mask!");
113
475
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
475
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
475
  static inline uint64_t build() {
111
475
    static_assert(0 <= Head && Head < 64,
112
475
                  "RelExpr is too large for 64-bit mask!");
113
475
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
475
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
475
  static inline uint64_t build() {
111
475
    static_assert(0 <= Head && Head < 64,
112
475
                  "RelExpr is too large for 64-bit mask!");
113
475
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
475
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)34, (lld::elf::RelExpr)35>::build()
Line
Count
Source
110
475
  static inline uint64_t build() {
111
475
    static_assert(0 <= Head && Head < 64,
112
475
                  "RelExpr is too large for 64-bit mask!");
113
475
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
475
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)35>::build()
Line
Count
Source
110
475
  static inline uint64_t build() {
111
475
    static_assert(0 <= Head && Head < 64,
112
475
                  "RelExpr is too large for 64-bit mask!");
113
475
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
475
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)3, (lld::elf::RelExpr)8, (lld::elf::RelExpr)5, (lld::elf::RelExpr)42, (lld::elf::RelExpr)4, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
363
  static inline uint64_t build() {
111
363
    static_assert(0 <= Head && Head < 64,
112
363
                  "RelExpr is too large for 64-bit mask!");
113
363
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
363
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8, (lld::elf::RelExpr)5, (lld::elf::RelExpr)42, (lld::elf::RelExpr)4, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
363
  static inline uint64_t build() {
111
363
    static_assert(0 <= Head && Head < 64,
112
363
                  "RelExpr is too large for 64-bit mask!");
113
363
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
363
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)5, (lld::elf::RelExpr)42, (lld::elf::RelExpr)4, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
363
  static inline uint64_t build() {
111
363
    static_assert(0 <= Head && Head < 64,
112
363
                  "RelExpr is too large for 64-bit mask!");
113
363
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
363
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)42, (lld::elf::RelExpr)4, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
363
  static inline uint64_t build() {
111
363
    static_assert(0 <= Head && Head < 64,
112
363
                  "RelExpr is too large for 64-bit mask!");
113
363
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
363
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)4, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
363
  static inline uint64_t build() {
111
363
    static_assert(0 <= Head && Head < 64,
112
363
                  "RelExpr is too large for 64-bit mask!");
113
363
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
363
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.5k
  static inline uint64_t build() {
111
14.5k
    static_assert(0 <= Head && Head < 64,
112
14.5k
                  "RelExpr is too large for 64-bit mask!");
113
14.5k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.5k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)16, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)15>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)15>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)58, (lld::elf::RelExpr)15>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)15>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)3, (lld::elf::RelExpr)4, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)4, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)53, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)5, (lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)2, (lld::elf::RelExpr)8, (lld::elf::RelExpr)4, (lld::elf::RelExpr)47, (lld::elf::RelExpr)39, (lld::elf::RelExpr)51, (lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)8, (lld::elf::RelExpr)4, (lld::elf::RelExpr)47, (lld::elf::RelExpr)39, (lld::elf::RelExpr)51, (lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)4, (lld::elf::RelExpr)47, (lld::elf::RelExpr)39, (lld::elf::RelExpr)51, (lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)47, (lld::elf::RelExpr)39, (lld::elf::RelExpr)51, (lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)39, (lld::elf::RelExpr)51, (lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)51, (lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)40, (lld::elf::RelExpr)36>::build()
Line
Count
Source
110
14.2k
  static inline uint64_t build() {
111
14.2k
    static_assert(0 <= Head && Head < 64,
112
14.2k
                  "RelExpr is too large for 64-bit mask!");
113
14.2k
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
14.2k
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)14, (lld::elf::RelExpr)10, (lld::elf::RelExpr)9, (lld::elf::RelExpr)48, (lld::elf::RelExpr)57, (lld::elf::RelExpr)59, (lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)10, (lld::elf::RelExpr)9, (lld::elf::RelExpr)48, (lld::elf::RelExpr)57, (lld::elf::RelExpr)59, (lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)9, (lld::elf::RelExpr)48, (lld::elf::RelExpr)57, (lld::elf::RelExpr)59, (lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)48, (lld::elf::RelExpr)57, (lld::elf::RelExpr)59, (lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)57, (lld::elf::RelExpr)59, (lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)59, (lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)17, (lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
Relocations.cpp:(anonymous namespace)::RelExprMaskBuilder<(lld::elf::RelExpr)62>::build()
Line
Count
Source
110
681
  static inline uint64_t build() {
111
681
    static_assert(0 <= Head && Head < 64,
112
681
                  "RelExpr is too large for 64-bit mask!");
113
681
    return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
114
681
  }
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
89.2k
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
89.2k
  assert(0 <= expr && (int)expr < 64 &&
124
89.2k
         "RelExpr is too large for 64-bit mask!");
125
89.2k
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
89.2k
}
bool oneof<(lld::elf::RelExpr)11, (lld::elf::RelExpr)13>(lld::elf::RelExpr)
Line
Count
Source
122
14.6k
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
14.6k
  assert(0 <= expr && (int)expr < 64 &&
124
14.6k
         "RelExpr is too large for 64-bit mask!");
125
14.6k
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.6k
}
bool oneof<(lld::elf::RelExpr)7, (lld::elf::RelExpr)9, (lld::elf::RelExpr)8, (lld::elf::RelExpr)34>(lld::elf::RelExpr)
Line
Count
Source
122
14.6k
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
14.6k
  assert(0 <= expr && (int)expr < 64 &&
124
14.6k
         "RelExpr is too large for 64-bit mask!");
125
14.6k
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.6k
}
bool oneof<(lld::elf::RelExpr)6, (lld::elf::RelExpr)10, (lld::elf::RelExpr)60, (lld::elf::RelExpr)59>(lld::elf::RelExpr)
Line
Count
Source
122
14.5k
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
14.5k
  assert(0 <= expr && (int)expr < 64 &&
124
14.5k
         "RelExpr is too large for 64-bit mask!");
125
14.5k
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.5k
}
bool oneof<(lld::elf::RelExpr)45, (lld::elf::RelExpr)30, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32>(lld::elf::RelExpr)
Line
Count
Source
122
597
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
597
  assert(0 <= expr && (int)expr < 64 &&
124
597
         "RelExpr is too large for 64-bit mask!");
125
597
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
597
}
bool oneof<(lld::elf::RelExpr)37, (lld::elf::RelExpr)38, (lld::elf::RelExpr)41, (lld::elf::RelExpr)40>(lld::elf::RelExpr)
Line
Count
Source
122
569
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
569
  assert(0 <= expr && (int)expr < 64 &&
124
569
         "RelExpr is too large for 64-bit mask!");
125
569
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
569
}
bool oneof<(lld::elf::RelExpr)45, (lld::elf::RelExpr)30, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35>(lld::elf::RelExpr)
Line
Count
Source
122
475
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
475
  assert(0 <= expr && (int)expr < 64 &&
124
475
         "RelExpr is too large for 64-bit mask!");
125
475
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
475
}
bool oneof<(lld::elf::RelExpr)3, (lld::elf::RelExpr)8, (lld::elf::RelExpr)5, (lld::elf::RelExpr)42, (lld::elf::RelExpr)4, (lld::elf::RelExpr)36>(lld::elf::RelExpr)
Line
Count
Source
122
363
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
363
  assert(0 <= expr && (int)expr < 64 &&
124
363
         "RelExpr is too large for 64-bit mask!");
125
363
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
363
}
bool oneof<(lld::elf::RelExpr)16, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)15>(lld::elf::RelExpr)
Line
Count
Source
122
14.2k
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
14.2k
  assert(0 <= expr && (int)expr < 64 &&
124
14.2k
         "RelExpr is too large for 64-bit mask!");
125
14.2k
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.2k
}
bool oneof<(lld::elf::RelExpr)3, (lld::elf::RelExpr)4, (lld::elf::RelExpr)47, (lld::elf::RelExpr)51, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)8>(lld::elf::RelExpr)
Line
Count
Source
122
14.2k
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
14.2k
  assert(0 <= expr && (int)expr < 64 &&
124
14.2k
         "RelExpr is too large for 64-bit mask!");
125
14.2k
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.2k
}
bool oneof<(lld::elf::RelExpr)2, (lld::elf::RelExpr)8, (lld::elf::RelExpr)4, (lld::elf::RelExpr)47, (lld::elf::RelExpr)39, (lld::elf::RelExpr)51, (lld::elf::RelExpr)48, (lld::elf::RelExpr)52, (lld::elf::RelExpr)53, (lld::elf::RelExpr)50, (lld::elf::RelExpr)54, (lld::elf::RelExpr)42, (lld::elf::RelExpr)5, (lld::elf::RelExpr)6, (lld::elf::RelExpr)7, (lld::elf::RelExpr)16, (lld::elf::RelExpr)33, (lld::elf::RelExpr)34, (lld::elf::RelExpr)35, (lld::elf::RelExpr)56, (lld::elf::RelExpr)58, (lld::elf::RelExpr)59, (lld::elf::RelExpr)61, (lld::elf::RelExpr)31, (lld::elf::RelExpr)32, (lld::elf::RelExpr)45, (lld::elf::RelExpr)11, (lld::elf::RelExpr)40, (lld::elf::RelExpr)36>(lld::elf::RelExpr)
Line
Count
Source
122
14.2k
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
14.2k
  assert(0 <= expr && (int)expr < 64 &&
124
14.2k
         "RelExpr is too large for 64-bit mask!");
125
14.2k
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
14.2k
}
bool oneof<(lld::elf::RelExpr)14, (lld::elf::RelExpr)10, (lld::elf::RelExpr)9, (lld::elf::RelExpr)48, (lld::elf::RelExpr)57, (lld::elf::RelExpr)59, (lld::elf::RelExpr)43, (lld::elf::RelExpr)17, (lld::elf::RelExpr)62>(lld::elf::RelExpr)
Line
Count
Source
122
681
template <RelExpr... Exprs> bool oneof(RelExpr expr) {
123
681
  assert(0 <= expr && (int)expr < 64 &&
124
681
         "RelExpr is too large for 64-bit mask!");
125
681
  return (uint64_t(1) << expr) & RelExprMaskBuilder<Exprs...>::build();
126
681
}
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
// Notes about General Dynamic and Local Dynamic TLS models below. They may
151
// require the generation of a pair of GOT entries that have associated dynamic
152
// relocations. The pair of GOT entries created are of the form GOT[e0] Module
153
// Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of
154
// symbol in TLS block.
155
//
156
// Returns the number of relocations processed.
157
template <class ELFT>
158
static unsigned
159
handleTlsRelocation(RelType type, Symbol &sym, InputSectionBase &c,
160
14.6k
                    typename ELFT::uint offset, int64_t addend, RelExpr expr) {
161
14.6k
  if (!sym.isTls())
162
14.0k
    return 0;
163
640
164
640
  if (config->emachine == EM_MIPS)
165
43
    return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
166
597
167
597
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC>(
168
597
          expr) &&
169
597
      
config->shared55
) {
170
28
    if (in.got->addDynTlsEntry(sym)) {
171
7
      uint64_t off = in.got->getGlobalDynOffset(sym);
172
7
      mainPart->relaDyn->addReloc(
173
7
          {target->tlsDescRel, in.got, off, !sym.isPreemptible, &sym, 0});
174
7
    }
175
28
    if (expr != R_TLSDESC_CALL)
176
15
      c.relocations.push_back({expr, type, offset, addend, &sym});
177
28
    return 1;
178
28
  }
179
569
180
569
  bool canRelax = config->emachine != EM_ARM && 
config->emachine != EM_RISCV541
;
181
569
182
569
  // If we are producing an executable and the symbol is non-preemptable, it
183
569
  // must be defined and the code sequence can be relaxed to use Local-Exec.
184
569
  //
185
569
  // ARM and RISC-V do not support any relaxations for TLS relocations, however,
186
569
  // we can omit the DTPMOD dynamic relocations and resolve them at link time
187
569
  // because them are always 1. This may be necessary for static linking as
188
569
  // DTPMOD may not be expected at load time.
189
569
  bool isLocalInExecutable = !sym.isPreemptible && 
!config->shared417
;
190
569
191
569
  // Local Dynamic is for access to module local TLS variables, while still
192
569
  // being suitable for being dynamically loaded via dlopen. GOT[e0] is the
193
569
  // module index, with a special value of 0 for the current module. GOT[e1] is
194
569
  // unused. There only needs to be one module index entry.
195
569
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
196
569
          expr)) {
197
58
    // Local-Dynamic relocs can be relaxed to Local-Exec.
198
58
    if (canRelax && 
!config->shared56
) {
199
17
      c.relocations.push_back(
200
17
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
201
17
           offset, addend, &sym});
202
17
      return target->getTlsGdRelaxSkip(type);
203
17
    }
204
41
    if (expr == R_TLSLD_HINT)
205
10
      return 1;
206
31
    if (in.got->addTlsIndex()) {
207
12
      if (isLocalInExecutable)
208
1
        in.got->relocations.push_back(
209
1
            {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym});
210
11
      else
211
11
        mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got,
212
11
                                in.got->getTlsIndexOff(), nullptr);
213
12
    }
214
31
    c.relocations.push_back({expr, type, offset, addend, &sym});
215
31
    return 1;
216
31
  }
217
511
218
511
  // Local-Dynamic relocs can be relaxed to Local-Exec.
219
511
  if (expr == R_DTPREL && 
!config->shared71
) {
220
20
    c.relocations.push_back(
221
20
        {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
222
20
         offset, addend, &sym});
223
20
    return 1;
224
20
  }
225
491
226
491
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
227
491
  // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
228
491
  if (expr == R_TLSLD_GOT_OFF) {
229
16
    if (!sym.isInGot()) {
230
8
      in.got->addEntry(sym);
231
8
      uint64_t off = sym.getGotOffset();
232
8
      in.got->relocations.push_back(
233
8
          {R_ABS, target->tlsOffsetRel, off, 0, &sym});
234
8
    }
235
16
    c.relocations.push_back({expr, type, offset, addend, &sym});
236
16
    return 1;
237
16
  }
238
475
239
475
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
240
475
            R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
241
112
    if (!canRelax || 
config->shared78
) {
242
50
      if (in.got->addDynTlsEntry(sym)) {
243
47
        uint64_t off = in.got->getGlobalDynOffset(sym);
244
47
245
47
        if (isLocalInExecutable)
246
18
          // Write one to the GOT slot.
247
18
          in.got->relocations.push_back(
248
18
              {R_ADDEND, target->symbolicRel, off, 1, &sym});
249
29
        else
250
29
          mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got, off, &sym);
251
47
252
47
        // If the symbol is preemptible we need the dynamic linker to write
253
47
        // the offset too.
254
47
        uint64_t offsetOff = off + config->wordsize;
255
47
        if (sym.isPreemptible)
256
21
          mainPart->relaDyn->addReloc(target->tlsOffsetRel, in.got, offsetOff,
257
21
                                  &sym);
258
26
        else
259
26
          in.got->relocations.push_back(
260
26
              {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
261
47
      }
262
50
      c.relocations.push_back({expr, type, offset, addend, &sym});
263
50
      return 1;
264
50
    }
265
62
266
62
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
62
    // depending on the symbol being locally defined or not.
268
62
    if (sym.isPreemptible) {
269
23
      c.relocations.push_back(
270
23
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_IE), type,
271
23
           offset, addend, &sym});
272
23
      if (!sym.isInGot()) {
273
14
        in.got->addEntry(sym);
274
14
        mainPart->relaDyn->addReloc(target->tlsGotRel, in.got, sym.getGotOffset(),
275
14
                                &sym);
276
14
      }
277
39
    } else {
278
39
      c.relocations.push_back(
279
39
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_LE), type,
280
39
           offset, addend, &sym});
281
39
    }
282
62
    return target->getTlsGdRelaxSkip(type);
283
62
  }
284
363
285
363
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
363
  // defined.
287
363
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
288
363
            R_TLSIE_HINT>(expr) &&
289
363
      
canRelax231
&&
isLocalInExecutable218
) {
290
120
    c.relocations.push_back({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
291
120
    return 1;
292
120
  }
293
243
294
243
  if (expr == R_TLSIE_HINT)
295
27
    return 1;
296
216
  return 0;
297
216
}
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
160
1.10k
                    typename ELFT::uint offset, int64_t addend, RelExpr expr) {
161
1.10k
  if (!sym.isTls())
162
994
    return 0;
163
108
164
108
  if (config->emachine == EM_MIPS)
165
0
    return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
166
108
167
108
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC>(
168
108
          expr) &&
169
108
      
config->shared0
) {
170
0
    if (in.got->addDynTlsEntry(sym)) {
171
0
      uint64_t off = in.got->getGlobalDynOffset(sym);
172
0
      mainPart->relaDyn->addReloc(
173
0
          {target->tlsDescRel, in.got, off, !sym.isPreemptible, &sym, 0});
174
0
    }
175
0
    if (expr != R_TLSDESC_CALL)
176
0
      c.relocations.push_back({expr, type, offset, addend, &sym});
177
0
    return 1;
178
0
  }
179
108
180
108
  bool canRelax = config->emachine != EM_ARM && 
config->emachine != EM_RISCV80
;
181
108
182
108
  // If we are producing an executable and the symbol is non-preemptable, it
183
108
  // must be defined and the code sequence can be relaxed to use Local-Exec.
184
108
  //
185
108
  // ARM and RISC-V do not support any relaxations for TLS relocations, however,
186
108
  // we can omit the DTPMOD dynamic relocations and resolve them at link time
187
108
  // because them are always 1. This may be necessary for static linking as
188
108
  // DTPMOD may not be expected at load time.
189
108
  bool isLocalInExecutable = !sym.isPreemptible && 
!config->shared70
;
190
108
191
108
  // Local Dynamic is for access to module local TLS variables, while still
192
108
  // being suitable for being dynamically loaded via dlopen. GOT[e0] is the
193
108
  // module index, with a special value of 0 for the current module. GOT[e1] is
194
108
  // unused. There only needs to be one module index entry.
195
108
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
196
108
          expr)) {
197
7
    // Local-Dynamic relocs can be relaxed to Local-Exec.
198
7
    if (canRelax && 
!config->shared5
) {
199
2
      c.relocations.push_back(
200
2
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
201
2
           offset, addend, &sym});
202
2
      return target->getTlsGdRelaxSkip(type);
203
2
    }
204
5
    if (expr == R_TLSLD_HINT)
205
0
      return 1;
206
5
    if (in.got->addTlsIndex()) {
207
4
      if (isLocalInExecutable)
208
1
        in.got->relocations.push_back(
209
1
            {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym});
210
3
      else
211
3
        mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got,
212
3
                                in.got->getTlsIndexOff(), nullptr);
213
4
    }
214
5
    c.relocations.push_back({expr, type, offset, addend, &sym});
215
5
    return 1;
216
5
  }
217
101
218
101
  // Local-Dynamic relocs can be relaxed to Local-Exec.
219
101
  if (expr == R_DTPREL && 
!config->shared5
) {
220
2
    c.relocations.push_back(
221
2
        {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
222
2
         offset, addend, &sym});
223
2
    return 1;
224
2
  }
225
99
226
99
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
227
99
  // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
228
99
  if (expr == R_TLSLD_GOT_OFF) {
229
0
    if (!sym.isInGot()) {
230
0
      in.got->addEntry(sym);
231
0
      uint64_t off = sym.getGotOffset();
232
0
      in.got->relocations.push_back(
233
0
          {R_ABS, target->tlsOffsetRel, off, 0, &sym});
234
0
    }
235
0
    c.relocations.push_back({expr, type, offset, addend, &sym});
236
0
    return 1;
237
0
  }
238
99
239
99
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
240
99
            R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
241
31
    if (!canRelax || 
config->shared7
) {
242
26
      if (in.got->addDynTlsEntry(sym)) {
243
26
        uint64_t off = in.got->getGlobalDynOffset(sym);
244
26
245
26
        if (isLocalInExecutable)
246
12
          // Write one to the GOT slot.
247
12
          in.got->relocations.push_back(
248
12
              {R_ADDEND, target->symbolicRel, off, 1, &sym});
249
14
        else
250
14
          mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got, off, &sym);
251
26
252
26
        // If the symbol is preemptible we need the dynamic linker to write
253
26
        // the offset too.
254
26
        uint64_t offsetOff = off + config->wordsize;
255
26
        if (sym.isPreemptible)
256
10
          mainPart->relaDyn->addReloc(target->tlsOffsetRel, in.got, offsetOff,
257
10
                                  &sym);
258
16
        else
259
16
          in.got->relocations.push_back(
260
16
              {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
261
26
      }
262
26
      c.relocations.push_back({expr, type, offset, addend, &sym});
263
26
      return 1;
264
26
    }
265
5
266
5
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
5
    // depending on the symbol being locally defined or not.
268
5
    if (sym.isPreemptible) {
269
2
      c.relocations.push_back(
270
2
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_IE), type,
271
2
           offset, addend, &sym});
272
2
      if (!sym.isInGot()) {
273
2
        in.got->addEntry(sym);
274
2
        mainPart->relaDyn->addReloc(target->tlsGotRel, in.got, sym.getGotOffset(),
275
2
                                &sym);
276
2
      }
277
3
    } else {
278
3
      c.relocations.push_back(
279
3
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_LE), type,
280
3
           offset, addend, &sym});
281
3
    }
282
5
    return target->getTlsGdRelaxSkip(type);
283
5
  }
284
68
285
68
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
68
  // defined.
287
68
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
288
68
            R_TLSIE_HINT>(expr) &&
289
68
      
canRelax36
&&
isLocalInExecutable27
) {
290
10
    c.relocations.push_back({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
291
10
    return 1;
292
10
  }
293
58
294
58
  if (expr == R_TLSIE_HINT)
295
0
    return 1;
296
58
  return 0;
297
58
}
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
160
407
                    typename ELFT::uint offset, int64_t addend, RelExpr expr) {
161
407
  if (!sym.isTls())
162
315
    return 0;
163
92
164
92
  if (config->emachine == EM_MIPS)
165
30
    return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
166
62
167
62
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC>(
168
62
          expr) &&
169
62
      
config->shared9
) {
170
3
    if (in.got->addDynTlsEntry(sym)) {
171
0
      uint64_t off = in.got->getGlobalDynOffset(sym);
172
0
      mainPart->relaDyn->addReloc(
173
0
          {target->tlsDescRel, in.got, off, !sym.isPreemptible, &sym, 0});
174
0
    }
175
3
    if (expr != R_TLSDESC_CALL)
176
0
      c.relocations.push_back({expr, type, offset, addend, &sym});
177
3
    return 1;
178
3
  }
179
59
180
59
  bool canRelax = config->emachine != EM_ARM && config->emachine != EM_RISCV;
181
59
182
59
  // If we are producing an executable and the symbol is non-preemptable, it
183
59
  // must be defined and the code sequence can be relaxed to use Local-Exec.
184
59
  //
185
59
  // ARM and RISC-V do not support any relaxations for TLS relocations, however,
186
59
  // we can omit the DTPMOD dynamic relocations and resolve them at link time
187
59
  // because them are always 1. This may be necessary for static linking as
188
59
  // DTPMOD may not be expected at load time.
189
59
  bool isLocalInExecutable = !sym.isPreemptible && 
!config->shared50
;
190
59
191
59
  // Local Dynamic is for access to module local TLS variables, while still
192
59
  // being suitable for being dynamically loaded via dlopen. GOT[e0] is the
193
59
  // module index, with a special value of 0 for the current module. GOT[e1] is
194
59
  // unused. There only needs to be one module index entry.
195
59
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
196
59
          expr)) {
197
8
    // Local-Dynamic relocs can be relaxed to Local-Exec.
198
8
    if (canRelax && !config->shared) {
199
4
      c.relocations.push_back(
200
4
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
201
4
           offset, addend, &sym});
202
4
      return target->getTlsGdRelaxSkip(type);
203
4
    }
204
4
    if (expr == R_TLSLD_HINT)
205
2
      return 1;
206
2
    if (in.got->addTlsIndex()) {
207
1
      if (isLocalInExecutable)
208
0
        in.got->relocations.push_back(
209
0
            {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym});
210
1
      else
211
1
        mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got,
212
1
                                in.got->getTlsIndexOff(), nullptr);
213
1
    }
214
2
    c.relocations.push_back({expr, type, offset, addend, &sym});
215
2
    return 1;
216
2
  }
217
51
218
51
  // Local-Dynamic relocs can be relaxed to Local-Exec.
219
51
  if (expr == R_DTPREL && 
!config->shared14
) {
220
7
    c.relocations.push_back(
221
7
        {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
222
7
         offset, addend, &sym});
223
7
    return 1;
224
7
  }
225
44
226
44
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
227
44
  // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
228
44
  if (expr == R_TLSLD_GOT_OFF) {
229
0
    if (!sym.isInGot()) {
230
0
      in.got->addEntry(sym);
231
0
      uint64_t off = sym.getGotOffset();
232
0
      in.got->relocations.push_back(
233
0
          {R_ABS, target->tlsOffsetRel, off, 0, &sym});
234
0
    }
235
0
    c.relocations.push_back({expr, type, offset, addend, &sym});
236
0
    return 1;
237
0
  }
238
44
239
44
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
240
44
            R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
241
15
    if (!canRelax || config->shared) {
242
3
      if (in.got->addDynTlsEntry(sym)) {
243
3
        uint64_t off = in.got->getGlobalDynOffset(sym);
244
3
245
3
        if (isLocalInExecutable)
246
0
          // Write one to the GOT slot.
247
0
          in.got->relocations.push_back(
248
0
              {R_ADDEND, target->symbolicRel, off, 1, &sym});
249
3
        else
250
3
          mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got, off, &sym);
251
3
252
3
        // If the symbol is preemptible we need the dynamic linker to write
253
3
        // the offset too.
254
3
        uint64_t offsetOff = off + config->wordsize;
255
3
        if (sym.isPreemptible)
256
3
          mainPart->relaDyn->addReloc(target->tlsOffsetRel, in.got, offsetOff,
257
3
                                  &sym);
258
0
        else
259
0
          in.got->relocations.push_back(
260
0
              {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
261
3
      }
262
3
      c.relocations.push_back({expr, type, offset, addend, &sym});
263
3
      return 1;
264
3
    }
265
12
266
12
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
12
    // depending on the symbol being locally defined or not.
268
12
    if (sym.isPreemptible) {
269
4
      c.relocations.push_back(
270
4
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_IE), type,
271
4
           offset, addend, &sym});
272
4
      if (!sym.isInGot()) {
273
2
        in.got->addEntry(sym);
274
2
        mainPart->relaDyn->addReloc(target->tlsGotRel, in.got, sym.getGotOffset(),
275
2
                                &sym);
276
2
      }
277
8
    } else {
278
8
      c.relocations.push_back(
279
8
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_LE), type,
280
8
           offset, addend, &sym});
281
8
    }
282
12
    return target->getTlsGdRelaxSkip(type);
283
12
  }
284
29
285
29
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
29
  // defined.
287
29
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
288
29
            R_TLSIE_HINT>(expr) &&
289
29
      
canRelax18
&&
isLocalInExecutable18
) {
290
9
    c.relocations.push_back({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
291
9
    return 1;
292
9
  }
293
20
294
20
  if (expr == R_TLSIE_HINT)
295
7
    return 1;
296
13
  return 0;
297
13
}
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
160
2.72k
                    typename ELFT::uint offset, int64_t addend, RelExpr expr) {
161
2.72k
  if (!sym.isTls())
162
2.39k
    return 0;
163
330
164
330
  if (config->emachine == EM_MIPS)
165
0
    return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
166
330
167
330
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC>(
168
330
          expr) &&
169
330
      
config->shared46
) {
170
25
    if (in.got->addDynTlsEntry(sym)) {
171
7
      uint64_t off = in.got->getGlobalDynOffset(sym);
172
7
      mainPart->relaDyn->addReloc(
173
7
          {target->tlsDescRel, in.got, off, !sym.isPreemptible, &sym, 0});
174
7
    }
175
25
    if (expr != R_TLSDESC_CALL)
176
15
      c.relocations.push_back({expr, type, offset, addend, &sym});
177
25
    return 1;
178
25
  }
179
305
180
305
  bool canRelax = config->emachine != EM_ARM && config->emachine != EM_RISCV;
181
305
182
305
  // If we are producing an executable and the symbol is non-preemptable, it
183
305
  // must be defined and the code sequence can be relaxed to use Local-Exec.
184
305
  //
185
305
  // ARM and RISC-V do not support any relaxations for TLS relocations, however,
186
305
  // we can omit the DTPMOD dynamic relocations and resolve them at link time
187
305
  // because them are always 1. This may be necessary for static linking as
188
305
  // DTPMOD may not be expected at load time.
189
305
  bool isLocalInExecutable = !sym.isPreemptible && 
!config->shared229
;
190
305
191
305
  // Local Dynamic is for access to module local TLS variables, while still
192
305
  // being suitable for being dynamically loaded via dlopen. GOT[e0] is the
193
305
  // module index, with a special value of 0 for the current module. GOT[e1] is
194
305
  // unused. There only needs to be one module index entry.
195
305
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
196
305
          expr)) {
197
26
    // Local-Dynamic relocs can be relaxed to Local-Exec.
198
26
    if (canRelax && !config->shared) {
199
8
      c.relocations.push_back(
200
8
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
201
8
           offset, addend, &sym});
202
8
      return target->getTlsGdRelaxSkip(type);
203
8
    }
204
18
    if (expr == R_TLSLD_HINT)
205
4
      return 1;
206
14
    if (in.got->addTlsIndex()) {
207
5
      if (isLocalInExecutable)
208
0
        in.got->relocations.push_back(
209
0
            {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym});
210
5
      else
211
5
        mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got,
212
5
                                in.got->getTlsIndexOff(), nullptr);
213
5
    }
214
14
    c.relocations.push_back({expr, type, offset, addend, &sym});
215
14
    return 1;
216
14
  }
217
279
218
279
  // Local-Dynamic relocs can be relaxed to Local-Exec.
219
279
  if (expr == R_DTPREL && 
!config->shared38
) {
220
9
    c.relocations.push_back(
221
9
        {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
222
9
         offset, addend, &sym});
223
9
    return 1;
224
9
  }
225
270
226
270
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
227
270
  // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
228
270
  if (expr == R_TLSLD_GOT_OFF) {
229
8
    if (!sym.isInGot()) {
230
4
      in.got->addEntry(sym);
231
4
      uint64_t off = sym.getGotOffset();
232
4
      in.got->relocations.push_back(
233
4
          {R_ABS, target->tlsOffsetRel, off, 0, &sym});
234
4
    }
235
8
    c.relocations.push_back({expr, type, offset, addend, &sym});
236
8
    return 1;
237
8
  }
238
262
239
262
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
240
262
            R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
241
66
    if (!canRelax || 
config->shared56
) {
242
21
      if (in.got->addDynTlsEntry(sym)) {
243
18
        uint64_t off = in.got->getGlobalDynOffset(sym);
244
18
245
18
        if (isLocalInExecutable)
246
6
          // Write one to the GOT slot.
247
6
          in.got->relocations.push_back(
248
6
              {R_ADDEND, target->symbolicRel, off, 1, &sym});
249
12
        else
250
12
          mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got, off, &sym);
251
18
252
18
        // If the symbol is preemptible we need the dynamic linker to write
253
18
        // the offset too.
254
18
        uint64_t offsetOff = off + config->wordsize;
255
18
        if (sym.isPreemptible)
256
8
          mainPart->relaDyn->addReloc(target->tlsOffsetRel, in.got, offsetOff,
257
8
                                  &sym);
258
10
        else
259
10
          in.got->relocations.push_back(
260
10
              {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
261
18
      }
262
21
      c.relocations.push_back({expr, type, offset, addend, &sym});
263
21
      return 1;
264
21
    }
265
45
266
45
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
45
    // depending on the symbol being locally defined or not.
268
45
    if (sym.isPreemptible) {
269
17
      c.relocations.push_back(
270
17
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_IE), type,
271
17
           offset, addend, &sym});
272
17
      if (!sym.isInGot()) {
273
10
        in.got->addEntry(sym);
274
10
        mainPart->relaDyn->addReloc(target->tlsGotRel, in.got, sym.getGotOffset(),
275
10
                                &sym);
276
10
      }
277
28
    } else {
278
28
      c.relocations.push_back(
279
28
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_LE), type,
280
28
           offset, addend, &sym});
281
28
    }
282
45
    return target->getTlsGdRelaxSkip(type);
283
45
  }
284
196
285
196
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
196
  // defined.
287
196
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
288
196
            R_TLSIE_HINT>(expr) &&
289
196
      
canRelax119
&&
isLocalInExecutable115
) {
290
72
    c.relocations.push_back({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
291
72
    return 1;
292
72
  }
293
124
294
124
  if (expr == R_TLSIE_HINT)
295
10
    return 1;
296
114
  return 0;
297
114
}
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
160
10.4k
                    typename ELFT::uint offset, int64_t addend, RelExpr expr) {
161
10.4k
  if (!sym.isTls())
162
10.3k
    return 0;
163
110
164
110
  if (config->emachine == EM_MIPS)
165
13
    return handleMipsTlsRelocation(type, sym, c, offset, addend, expr);
166
97
167
97
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC>(
168
97
          expr) &&
169
97
      
config->shared0
) {
170
0
    if (in.got->addDynTlsEntry(sym)) {
171
0
      uint64_t off = in.got->getGlobalDynOffset(sym);
172
0
      mainPart->relaDyn->addReloc(
173
0
          {target->tlsDescRel, in.got, off, !sym.isPreemptible, &sym, 0});
174
0
    }
175
0
    if (expr != R_TLSDESC_CALL)
176
0
      c.relocations.push_back({expr, type, offset, addend, &sym});
177
0
    return 1;
178
0
  }
179
97
180
97
  bool canRelax = config->emachine != EM_ARM && config->emachine != EM_RISCV;
181
97
182
97
  // If we are producing an executable and the symbol is non-preemptable, it
183
97
  // must be defined and the code sequence can be relaxed to use Local-Exec.
184
97
  //
185
97
  // ARM and RISC-V do not support any relaxations for TLS relocations, however,
186
97
  // we can omit the DTPMOD dynamic relocations and resolve them at link time
187
97
  // because them are always 1. This may be necessary for static linking as
188
97
  // DTPMOD may not be expected at load time.
189
97
  bool isLocalInExecutable = !sym.isPreemptible && 
!config->shared68
;
190
97
191
97
  // Local Dynamic is for access to module local TLS variables, while still
192
97
  // being suitable for being dynamically loaded via dlopen. GOT[e0] is the
193
97
  // module index, with a special value of 0 for the current module. GOT[e1] is
194
97
  // unused. There only needs to be one module index entry.
195
97
  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(
196
97
          expr)) {
197
17
    // Local-Dynamic relocs can be relaxed to Local-Exec.
198
17
    if (canRelax && !config->shared) {
199
3
      c.relocations.push_back(
200
3
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
201
3
           offset, addend, &sym});
202
3
      return target->getTlsGdRelaxSkip(type);
203
3
    }
204
14
    if (expr == R_TLSLD_HINT)
205
4
      return 1;
206
10
    if (in.got->addTlsIndex()) {
207
2
      if (isLocalInExecutable)
208
0
        in.got->relocations.push_back(
209
0
            {R_ADDEND, target->symbolicRel, in.got->getTlsIndexOff(), 1, &sym});
210
2
      else
211
2
        mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got,
212
2
                                in.got->getTlsIndexOff(), nullptr);
213
2
    }
214
10
    c.relocations.push_back({expr, type, offset, addend, &sym});
215
10
    return 1;
216
10
  }
217
80
218
80
  // Local-Dynamic relocs can be relaxed to Local-Exec.
219
80
  if (expr == R_DTPREL && 
!config->shared14
) {
220
2
    c.relocations.push_back(
221
2
        {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_LD_TO_LE), type,
222
2
         offset, addend, &sym});
223
2
    return 1;
224
2
  }
225
78
226
78
  // Local-Dynamic sequence where offset of tls variable relative to dynamic
227
78
  // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
228
78
  if (expr == R_TLSLD_GOT_OFF) {
229
8
    if (!sym.isInGot()) {
230
4
      in.got->addEntry(sym);
231
4
      uint64_t off = sym.getGotOffset();
232
4
      in.got->relocations.push_back(
233
4
          {R_ABS, target->tlsOffsetRel, off, 0, &sym});
234
4
    }
235
8
    c.relocations.push_back({expr, type, offset, addend, &sym});
236
8
    return 1;
237
8
  }
238
70
239
70
  if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,
240
70
            R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) {
241
0
    if (!canRelax || config->shared) {
242
0
      if (in.got->addDynTlsEntry(sym)) {
243
0
        uint64_t off = in.got->getGlobalDynOffset(sym);
244
0
245
0
        if (isLocalInExecutable)
246
0
          // Write one to the GOT slot.
247
0
          in.got->relocations.push_back(
248
0
              {R_ADDEND, target->symbolicRel, off, 1, &sym});
249
0
        else
250
0
          mainPart->relaDyn->addReloc(target->tlsModuleIndexRel, in.got, off, &sym);
251
0
252
0
        // If the symbol is preemptible we need the dynamic linker to write
253
0
        // the offset too.
254
0
        uint64_t offsetOff = off + config->wordsize;
255
0
        if (sym.isPreemptible)
256
0
          mainPart->relaDyn->addReloc(target->tlsOffsetRel, in.got, offsetOff,
257
0
                                  &sym);
258
0
        else
259
0
          in.got->relocations.push_back(
260
0
              {R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym});
261
0
      }
262
0
      c.relocations.push_back({expr, type, offset, addend, &sym});
263
0
      return 1;
264
0
    }
265
0
266
0
    // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
267
0
    // depending on the symbol being locally defined or not.
268
0
    if (sym.isPreemptible) {
269
0
      c.relocations.push_back(
270
0
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_IE), type,
271
0
           offset, addend, &sym});
272
0
      if (!sym.isInGot()) {
273
0
        in.got->addEntry(sym);
274
0
        mainPart->relaDyn->addReloc(target->tlsGotRel, in.got, sym.getGotOffset(),
275
0
                                &sym);
276
0
      }
277
0
    } else {
278
0
      c.relocations.push_back(
279
0
          {target->adjustRelaxExpr(type, nullptr, R_RELAX_TLS_GD_TO_LE), type,
280
0
           offset, addend, &sym});
281
0
    }
282
0
    return target->getTlsGdRelaxSkip(type);
283
0
  }
284
70
285
70
  // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
286
70
  // defined.
287
70
  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF,
288
70
            R_TLSIE_HINT>(expr) &&
289
70
      
canRelax58
&&
isLocalInExecutable58
) {
290
29
    c.relocations.push_back({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});
291
29
    return 1;
292
29
  }
293
41
294
41
  if (expr == R_TLSIE_HINT)
295
10
    return 1;
296
31
  return 0;
297
31
}
298
299
285
static RelType getMipsPairType(RelType type, bool isLocal) {
300
285
  switch (type) {
301
285
  case R_MIPS_HI16:
302
23
    return R_MIPS_LO16;
303
285
  case R_MIPS_GOT16:
304
40
    // In case of global symbol, the R_MIPS_GOT16 relocation does not
305
40
    // have a pair. Each global symbol has a unique entry in the GOT
306
40
    // and a corresponding instruction with help of the R_MIPS_GOT16
307
40
    // relocation loads an address of the symbol. In case of local
308
40
    // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold
309
40
    // the high 16 bits of the symbol's value. A paired R_MIPS_LO16
310
40
    // relocations handle low 16 bits of the address. That allows
311
40
    // to allocate only one GOT entry for every 64 KBytes of local data.
312
40
    return isLocal ? 
R_MIPS_LO1616
:
R_MIPS_NONE24
;
313
285
  case R_MICROMIPS_GOT16:
314
4
    return isLocal ? 
R_MICROMIPS_LO161
:
R_MIPS_NONE3
;
315
285
  case R_MIPS_PCHI16:
316
1
    return R_MIPS_PCLO16;
317
285
  case R_MICROMIPS_HI16:
318
3
    return R_MICROMIPS_LO16;
319
285
  default:
320
214
    return R_MIPS_NONE;
321
285
  }
322
285
}
323
324
// True if non-preemptable symbol always has the same value regardless of where
325
// the DSO is loaded.
326
813
static bool isAbsolute(const Symbol &sym) {
327
813
  if (sym.isUndefWeak())
328
10
    return true;
329
803
  if (const auto *dr = dyn_cast<Defined>(&sym))
330
801
    return dr->section == nullptr; // Absolute symbol.
331
2
  return false;
332
2
}
333
334
758
static bool isAbsoluteValue(const Symbol &sym) {
335
758
  return isAbsolute(sym) || 
sym.isTls()738
;
336
758
}
337
338
// Returns true if Expr refers a PLT entry.
339
14.2k
static bool needsPlt(RelExpr expr) {
340
14.2k
  return oneof<R_PLT_PC, R_PPC32_PLTREL, R_PPC64_CALL_PLT, R_PLT>(expr);
341
14.2k
}
342
343
// Returns true if Expr refers a GOT entry. Note that this function
344
// returns false for TLS variables even though they need GOT, because
345
// TLS variables uses GOT differently than the regular variables.
346
14.2k
static bool needsGot(RelExpr expr) {
347
14.2k
  return oneof<R_GOT, R_GOT_OFF, R_HEXAGON_GOT, R_MIPS_GOT_LOCAL_PAGE,
348
14.2k
               R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC,
349
14.2k
               R_GOT_PC, R_GOTPLT>(expr);
350
14.2k
}
351
352
// True if this expression is of the form Sym - X, where X is a position in the
353
// file (PC, or GOT for example).
354
681
static bool isRelExpr(RelExpr expr) {
355
681
  return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL,
356
681
               R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC,
357
681
               R_RISCV_PC_INDIRECT>(expr);
358
681
}
359
360
// Returns true if a given relocation can be computed at link-time.
361
//
362
// For instance, we know the offset from a relocation to its target at
363
// link-time if the relocation is PC-relative and refers a
364
// non-interposable function in the same executable. This function
365
// will return true for such relocation.
366
//
367
// If this function returns false, that means we need to emit a
368
// dynamic relocation so that the relocation will be fixed at load-time.
369
static bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym,
370
14.2k
                                     InputSectionBase &s, uint64_t relOff) {
371
14.2k
  // These expressions always compute a constant
372
14.2k
  if (oneof<R_DTPREL, R_GOTPLT, R_GOT_OFF, R_HEXAGON_GOT, R_TLSLD_GOT_OFF,
373
14.2k
            R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOTREL, R_MIPS_GOT_OFF,
374
14.2k
            R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, R_MIPS_TLSGD,
375
14.2k
            R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC,
376
14.2k
            R_PLT_PC, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC, R_PPC32_PLTREL,
377
14.2k
            R_PPC64_CALL_PLT, R_PPC64_RELAX_TOC, R_RISCV_ADD, R_TLSDESC_CALL,
378
14.2k
            R_TLSDESC_PC, R_AARCH64_TLSDESC_PAGE, R_HINT, R_TLSLD_HINT,
379
14.2k
            R_TLSIE_HINT>(e))
380
11.0k
    return true;
381
3.22k
382
3.22k
  // These never do, except if the entire file is position dependent or if
383
3.22k
  // only the low bits are used.
384
3.22k
  if (e == R_GOT || 
e == R_PLT3.14k
||
e == R_TLSDESC3.13k
)
385
92
    return target->usesOnlyLowPageBits(type) || 
!config->isPic31
;
386
3.13k
387
3.13k
  if (sym.isPreemptible)
388
351
    return false;
389
2.77k
  if (!config->isPic)
390
2.12k
    return true;
391
655
392
655
  // The size of a non preemptible symbol is a constant.
393
655
  if (e == R_SIZE)
394
6
    return true;
395
649
396
649
  // For the target and the relocation, we want to know if they are
397
649
  // absolute or relative.
398
649
  bool absVal = isAbsoluteValue(sym);
399
649
  bool relE = isRelExpr(e);
400
649
  if (absVal && 
!relE27
)
401
24
    return true;
402
625
  if (!absVal && 
relE622
)
403
332
    return true;
404
293
  if (!absVal && 
!relE290
)
405
290
    return target->usesOnlyLowPageBits(type);
406
3
407
3
  // Relative relocation to an absolute value. This is normally unrepresentable,
408
3
  // but if the relocation refers to a weak undefined symbol, we allow it to
409
3
  // resolve to the image base. This is a little strange, but it allows us to
410
3
  // link function calls to such symbols. Normally such a call will be guarded
411
3
  // with a comparison, which will load a zero from the GOT.
412
3
  // Another special case is MIPS _gp_disp symbol which represents offset
413
3
  // between start of a function and '_gp' value and defined as absolute just
414
3
  // to simplify the code.
415
3
  assert(absVal && relE);
416
3
  if (sym.isUndefWeak())
417
1
    return true;
418
2
419
2
  // We set the final symbols values for linker script defined symbols later.
420
2
  // They always can be computed as a link time constant.
421
2
  if (sym.scriptDefined)
422
1
      return true;
423
1
424
1
  error("relocation " + toString(type) + " cannot refer to absolute symbol: " +
425
1
        toString(sym) + getLocation(s, sym, relOff));
426
1
  return true;
427
1
}
428
429
2
static RelExpr toPlt(RelExpr expr) {
430
2
  switch (expr) {
431
2
  case R_PPC64_CALL:
432
0
    return R_PPC64_CALL_PLT;
433
2
  case R_PC:
434
2
    return R_PLT_PC;
435
2
  case R_ABS:
436
0
    return R_PLT;
437
2
  default:
438
0
    return expr;
439
2
  }
440
2
}
441
442
13.7k
static RelExpr fromPlt(RelExpr expr) {
443
13.7k
  // We decided not to use a plt. Optimize a reference to the plt to a
444
13.7k
  // reference to the symbol itself.
445
13.7k
  switch (expr) {
446
13.7k
  case R_PLT_PC:
447
714
  case R_PPC32_PLTREL:
448
714
    return R_PC;
449
714
  case R_PPC64_CALL_PLT:
450
143
    return R_PPC64_CALL;
451
714
  case R_PLT:
452
64
    return R_ABS;
453
12.7k
  default:
454
12.7k
    return expr;
455
13.7k
  }
456
13.7k
}
457
458
// Returns true if a given shared symbol is in a read-only segment in a DSO.
459
53
template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
460
53
  using Elf_Phdr = typename ELFT::Phdr;
461
53
462
53
  // Determine if the symbol is read-only by scanning the DSO's program headers.
463
53
  const SharedFile &file = ss.getFile();
464
53
  for (const Elf_Phdr &phdr :
465
53
       check(file.template getObj<ELFT>().program_headers()))
466
335
    if ((phdr.p_type == ELF::PT_LOAD || 
phdr.p_type == ELF::PT_GNU_RELRO188
) &&
467
335
        
!(phdr.p_flags & ELF::PF_W)188
&&
ss.value >= phdr.p_vaddr110
&&
468
335
        
ss.value < phdr.p_vaddr + phdr.p_memsz110
)
469
15
      return true;
470
53
  
return false38
;
471
53
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
459
7
template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
460
7
  using Elf_Phdr = typename ELFT::Phdr;
461
7
462
7
  // Determine if the symbol is read-only by scanning the DSO's program headers.
463
7
  const SharedFile &file = ss.getFile();
464
7
  for (const Elf_Phdr &phdr :
465
7
       check(file.template getObj<ELFT>().program_headers()))
466
47
    if ((phdr.p_type == ELF::PT_LOAD || 
phdr.p_type == ELF::PT_GNU_RELRO27
) &&
467
47
        
!(phdr.p_flags & ELF::PF_W)27
&&
ss.value >= phdr.p_vaddr14
&&
468
47
        
ss.value < phdr.p_vaddr + phdr.p_memsz14
)
469
1
      return true;
470
7
  
return false6
;
471
7
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
459
5
template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
460
5
  using Elf_Phdr = typename ELFT::Phdr;
461
5
462
5
  // Determine if the symbol is read-only by scanning the DSO's program headers.
463
5
  const SharedFile &file = ss.getFile();
464
5
  for (const Elf_Phdr &phdr :
465
5
       check(file.template getObj<ELFT>().program_headers()))
466
40
    if ((phdr.p_type == ELF::PT_LOAD || 
phdr.p_type == ELF::PT_GNU_RELRO25
) &&
467
40
        
!(phdr.p_flags & ELF::PF_W)15
&&
ss.value >= phdr.p_vaddr10
&&
468
40
        
ss.value < phdr.p_vaddr + phdr.p_memsz10
)
469
0
      return true;
470
5
  return false;
471
5
}
Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
459
41
template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {
460
41
  using Elf_Phdr = typename ELFT::Phdr;
461
41
462
41
  // Determine if the symbol is read-only by scanning the DSO's program headers.
463
41
  const SharedFile &file = ss.getFile();
464
41
  for (const Elf_Phdr &phdr :
465
41
       check(file.template getObj<ELFT>().program_headers()))
466
248
    if ((phdr.p_type == ELF::PT_LOAD || 
phdr.p_type == ELF::PT_GNU_RELRO136
) &&
467
248
        
!(phdr.p_flags & ELF::PF_W)146
&&
ss.value >= phdr.p_vaddr86
&&
468
248
        
ss.value < phdr.p_vaddr + phdr.p_memsz86
)
469
14
      return true;
470
41
  
return false27
;
471
41
}
Unexecuted instantiation: Relocations.cpp:bool isReadOnly<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
472
473
// Returns symbols at the same offset as a given symbol, including SS itself.
474
//
475
// If two or more symbols are at the same offset, and at least one of
476
// them are copied by a copy relocation, all of them need to be copied.
477
// Otherwise, they would refer to different places at runtime.
478
template <class ELFT>
479
53
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
480
53
  using Elf_Sym = typename ELFT::Sym;
481
53
482
53
  SharedFile &file = ss.getFile();
483
53
484
53
  SmallSet<SharedSymbol *, 4> ret;
485
165
  for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {
486
165
    if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||
487
165
        
s.getType() == STT_TLS161
||
s.st_value != ss.value160
)
488
92
      continue;
489
73
    StringRef name = check(s.getName(file.getStringTable()));
490
73
    Symbol *sym = symtab->find(name);
491
73
    if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))
492
70
      ret.insert(alias);
493
73
  }
494
53
  return ret;
495
53
}
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
479
7
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
480
7
  using Elf_Sym = typename ELFT::Sym;
481
7
482
7
  SharedFile &file = ss.getFile();
483
7
484
7
  SmallSet<SharedSymbol *, 4> ret;
485
19
  for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {
486
19
    if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||
487
19
        s.getType() == STT_TLS || s.st_value != ss.value)
488
12
      continue;
489
7
    StringRef name = check(s.getName(file.getStringTable()));
490
7
    Symbol *sym = symtab->find(name);
491
7
    if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))
492
7
      ret.insert(alias);
493
7
  }
494
7
  return ret;
495
7
}
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
479
5
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
480
5
  using Elf_Sym = typename ELFT::Sym;
481
5
482
5
  SharedFile &file = ss.getFile();
483
5
484
5
  SmallSet<SharedSymbol *, 4> ret;
485
25
  for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {
486
25
    if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||
487
25
        s.getType() == STT_TLS || s.st_value != ss.value)
488
20
      continue;
489
5
    StringRef name = check(s.getName(file.getStringTable()));
490
5
    Symbol *sym = symtab->find(name);
491
5
    if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))
492
5
      ret.insert(alias);
493
5
  }
494
5
  return ret;
495
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
479
41
static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) {
480
41
  using Elf_Sym = typename ELFT::Sym;
481
41
482
41
  SharedFile &file = ss.getFile();
483
41
484
41
  SmallSet<SharedSymbol *, 4> ret;
485
121
  for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {
486
121
    if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||
487
121
        
s.getType() == STT_TLS117
||
s.st_value != ss.value116
)
488
60
      continue;
489
61
    StringRef name = check(s.getName(file.getStringTable()));
490
61
    Symbol *sym = symtab->find(name);
491
61
    if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))
492
58
      ret.insert(alias);
493
61
  }
494
41
  return ret;
495
41
}
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&)
496
497
// When a symbol is copy relocated or we create a canonical plt entry, it is
498
// effectively a defined symbol. In the case of copy relocation the symbol is
499
// in .bss and in the case of a canonical plt entry it is in .plt. This function
500
// replaces the existing symbol with a Defined pointing to the appropriate
501
// location.
502
static void replaceWithDefined(Symbol &sym, SectionBase *sec, uint64_t value,
503
112
                               uint64_t size) {
504
112
  Symbol old = sym;
505
112
506
112
  sym.replace(Defined{sym.file, sym.getName(), sym.binding, sym.stOther,
507
112
                      sym.type, value, size, sec});
508
112
509
112
  sym.pltIndex = old.pltIndex;
510
112
  sym.gotIndex = old.gotIndex;
511
112
  sym.verdefIndex = old.verdefIndex;
512
112
  sym.ppc64BranchltIndex = old.ppc64BranchltIndex;
513
112
  sym.isPreemptible = true;
514
112
  sym.exportDynamic = true;
515
112
  sym.isUsedInRegularObj = true;
516
112
  sym.used = true;
517
112
}
518
519
// Reserve space in .bss or .bss.rel.ro for copy relocation.
520
//
521
// The copy relocation is pretty much a hack. If you use a copy relocation
522
// in your program, not only the symbol name but the symbol's size, RW/RO
523
// bit and alignment become part of the ABI. In addition to that, if the
524
// symbol has aliases, the aliases become part of the ABI. That's subtle,
525
// but if you violate that implicit ABI, that can cause very counter-
526
// intuitive consequences.
527
//
528
// So, what is the copy relocation? It's for linking non-position
529
// independent code to DSOs. In an ideal world, all references to data
530
// exported by DSOs should go indirectly through GOT. But if object files
531
// are compiled as non-PIC, all data references are direct. There is no
532
// way for the linker to transform the code to use GOT, as machine
533
// instructions are already set in stone in object files. This is where
534
// the copy relocation takes a role.
535
//
536
// A copy relocation instructs the dynamic linker to copy data from a DSO
537
// to a specified address (which is usually in .bss) at load-time. If the
538
// static linker (that's us) finds a direct data reference to a DSO
539
// symbol, it creates a copy relocation, so that the symbol can be
540
// resolved as if it were in .bss rather than in a DSO.
541
//
542
// As you can see in this function, we create a copy relocation for the
543
// dynamic linker, and the relocation contains not only symbol name but
544
// various other informtion about the symbol. So, such attributes become a
545
// part of the ABI.
546
//
547
// Note for application developers: I can give you a piece of advice if
548
// you are writing a shared library. You probably should export only
549
// functions from your library. You shouldn't export variables.
550
//
551
// As an example what can happen when you export variables without knowing
552
// the semantics of copy relocations, assume that you have an exported
553
// variable of type T. It is an ABI-breaking change to add new members at
554
// end of T even though doing that doesn't change the layout of the
555
// existing members. That's because the space for the new members are not
556
// reserved in .bss unless you recompile the main program. That means they
557
// are likely to overlap with other data that happens to be laid out next
558
// to the variable in .bss. This kind of issue is sometimes very hard to
559
// debug. What's a solution? Instead of exporting a varaible V from a DSO,
560
// define an accessor getV().
561
53
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
562
53
  // Copy relocation against zero-sized symbol doesn't make sense.
563
53
  uint64_t symSize = ss.getSize();
564
53
  if (symSize == 0 || ss.alignment == 0)
565
0
    fatal("cannot create a copy relocation for symbol " + toString(ss));
566
53
567
53
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
568
53
  // memory protection by reserving space in the .bss.rel.ro section.
569
53
  bool isRO = isReadOnly<ELFT>(ss);
570
53
  BssSection *sec =
571
53
      make<BssSection>(isRO ? 
".bss.rel.ro"15
:
".bss"38
, symSize, ss.alignment);
572
53
  if (isRO)
573
15
    in.bssRelRo->getParent()->addSection(sec);
574
38
  else
575
38
    in.bss->getParent()->addSection(sec);
576
53
577
53
  // Look through the DSO's dynamic symbol table for aliases and create a
578
53
  // dynamic symbol for each one. This causes the copy relocation to correctly
579
53
  // interpose any aliases.
580
53
  for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
581
69
    replaceWithDefined(*sym, sec, 0, sym->size);
582
53
583
53
  mainPart->relaDyn->addReloc(target->copyRel, sec, 0, &ss);
584
53
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
561
7
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
562
7
  // Copy relocation against zero-sized symbol doesn't make sense.
563
7
  uint64_t symSize = ss.getSize();
564
7
  if (symSize == 0 || ss.alignment == 0)
565
0
    fatal("cannot create a copy relocation for symbol " + toString(ss));
566
7
567
7
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
568
7
  // memory protection by reserving space in the .bss.rel.ro section.
569
7
  bool isRO = isReadOnly<ELFT>(ss);
570
7
  BssSection *sec =
571
7
      make<BssSection>(isRO ? 
".bss.rel.ro"1
:
".bss"6
, symSize, ss.alignment);
572
7
  if (isRO)
573
1
    in.bssRelRo->getParent()->addSection(sec);
574
6
  else
575
6
    in.bss->getParent()->addSection(sec);
576
7
577
7
  // Look through the DSO's dynamic symbol table for aliases and create a
578
7
  // dynamic symbol for each one. This causes the copy relocation to correctly
579
7
  // interpose any aliases.
580
7
  for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
581
7
    replaceWithDefined(*sym, sec, 0, sym->size);
582
7
583
7
  mainPart->relaDyn->addReloc(target->copyRel, sec, 0, &ss);
584
7
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::SharedSymbol&)
Line
Count
Source
561
5
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
562
5
  // Copy relocation against zero-sized symbol doesn't make sense.
563
5
  uint64_t symSize = ss.getSize();
564
5
  if (symSize == 0 || ss.alignment == 0)
565
0
    fatal("cannot create a copy relocation for symbol " + toString(ss));
566
5
567
5
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
568
5
  // memory protection by reserving space in the .bss.rel.ro section.
569
5
  bool isRO = isReadOnly<ELFT>(ss);
570
5
  BssSection *sec =
571
5
      make<BssSection>(isRO ? 
".bss.rel.ro"0
: ".bss", symSize, ss.alignment);
572
5
  if (isRO)
573
0
    in.bssRelRo->getParent()->addSection(sec);
574
5
  else
575
5
    in.bss->getParent()->addSection(sec);
576
5
577
5
  // Look through the DSO's dynamic symbol table for aliases and create a
578
5
  // dynamic symbol for each one. This causes the copy relocation to correctly
579
5
  // interpose any aliases.
580
5
  for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
581
5
    replaceWithDefined(*sym, sec, 0, sym->size);
582
5
583
5
  mainPart->relaDyn->addReloc(target->copyRel, sec, 0, &ss);
584
5
}
Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::SharedSymbol&)
Line
Count
Source
561
41
template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) {
562
41
  // Copy relocation against zero-sized symbol doesn't make sense.
563
41
  uint64_t symSize = ss.getSize();
564
41
  if (symSize == 0 || ss.alignment == 0)
565
0
    fatal("cannot create a copy relocation for symbol " + toString(ss));
566
41
567
41
  // See if this symbol is in a read-only segment. If so, preserve the symbol's
568
41
  // memory protection by reserving space in the .bss.rel.ro section.
569
41
  bool isRO = isReadOnly<ELFT>(ss);
570
41
  BssSection *sec =
571
41
      make<BssSection>(isRO ? 
".bss.rel.ro"14
:
".bss"27
, symSize, ss.alignment);
572
41
  if (isRO)
573
14
    in.bssRelRo->getParent()->addSection(sec);
574
27
  else
575
27
    in.bss->getParent()->addSection(sec);
576
41
577
41
  // Look through the DSO's dynamic symbol table for aliases and create a
578
41
  // dynamic symbol for each one. This causes the copy relocation to correctly
579
41
  // interpose any aliases.
580
41
  for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss))
581
57
    replaceWithDefined(*sym, sec, 0, sym->size);
582
41
583
41
  mainPart->relaDyn->addReloc(target->copyRel, sec, 0, &ss);
584
41
}
Unexecuted instantiation: Relocations.cpp:void addCopyRelSymbol<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::SharedSymbol&)
585
586
// MIPS has an odd notion of "paired" relocations to calculate addends.
587
// For example, if a relocation is of R_MIPS_HI16, there must be a
588
// R_MIPS_LO16 relocation after that, and an addend is calculated using
589
// the two relocations.
590
template <class ELFT, class RelTy>
591
static int64_t computeMipsAddend(const RelTy &rel, const RelTy *end,
592
                                 InputSectionBase &sec, RelExpr expr,
593
10.3k
                                 bool isLocal) {
594
10.3k
  if (expr == R_MIPS_GOTREL && 
isLocal22
)
595
9
    return sec.getFile<ELFT>()->mipsGp0;
596
10.3k
597
10.3k
  // The ABI says that the paired relocation is used only for REL.
598
10.3k
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
599
10.3k
  if (RelTy::IsRela)
600
10.0k
    return 0;
601
285
602
285
  RelType type = rel.getType(config->isMips64EL);
603
285
  uint32_t pairTy = getMipsPairType(type, isLocal);
604
285
  if (pairTy == R_MIPS_NONE)
605
241
    return 0;
606
44
607
44
  const uint8_t *buf = sec.data().data();
608
44
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
609
44
610
44
  // To make things worse, paired relocations might not be contiguous in
611
44
  // the relocation table, so we need to do linear search. *sigh*
612
89
  for (const RelTy *ri = &rel; ri != end; 
++ri45
)
613
88
    if (ri->getType(config->isMips64EL) == pairTy &&
614
88
        
ri->getSymbol(config->isMips64EL) == symIndex43
)
615
43
      return target->getImplicitAddend(buf + ri->r_offset, pairTy);
616
44
617
44
  warn("can't find matching " + toString(pairTy) + " relocation for " +
618
1
       toString(type));
619
1
  return 0;
620
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
593
21
                                 bool isLocal) {
594
21
  if (expr == R_MIPS_GOTREL && 
isLocal1
)
595
0
    return sec.getFile<ELFT>()->mipsGp0;
596
21
597
21
  // The ABI says that the paired relocation is used only for REL.
598
21
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
599
21
  if (RelTy::IsRela)
600
0
    return 0;
601
21
602
21
  RelType type = rel.getType(config->isMips64EL);
603
21
  uint32_t pairTy = getMipsPairType(type, isLocal);
604
21
  if (pairTy == R_MIPS_NONE)
605
20
    return 0;
606
1
607
1
  const uint8_t *buf = sec.data().data();
608
1
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
609
1
610
1
  // To make things worse, paired relocations might not be contiguous in
611
1
  // the relocation table, so we need to do linear search. *sigh*
612
2
  for (const RelTy *ri = &rel; ri != end; 
++ri1
)
613
2
    if (ri->getType(config->isMips64EL) == pairTy &&
614
2
        
ri->getSymbol(config->isMips64EL) == symIndex1
)
615
1
      return target->getImplicitAddend(buf + ri->r_offset, pairTy);
616
1
617
1
  warn("can't find matching " + toString(pairTy) + " relocation for " +
618
0
       toString(type));
619
0
  return 0;
620
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
593
5
                                 bool isLocal) {
594
5
  if (expr == R_MIPS_GOTREL && 
isLocal3
)
595
1
    return sec.getFile<ELFT>()->mipsGp0;
596
4
597
4
  // The ABI says that the paired relocation is used only for REL.
598
4
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
599
4
  if (RelTy::IsRela)
600
4
    return 0;
601
0
602
0
  RelType type = rel.getType(config->isMips64EL);
603
0
  uint32_t pairTy = getMipsPairType(type, isLocal);
604
0
  if (pairTy == R_MIPS_NONE)
605
0
    return 0;
606
0
607
0
  const uint8_t *buf = sec.data().data();
608
0
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
609
0
610
0
  // To make things worse, paired relocations might not be contiguous in
611
0
  // the relocation table, so we need to do linear search. *sigh*
612
0
  for (const RelTy *ri = &rel; ri != end; ++ri)
613
0
    if (ri->getType(config->isMips64EL) == pairTy &&
614
0
        ri->getSymbol(config->isMips64EL) == symIndex)
615
0
      return target->getImplicitAddend(buf + ri->r_offset, pairTy);
616
0
617
0
  warn("can't find matching " + toString(pairTy) + " relocation for " +
618
0
       toString(type));
619
0
  return 0;
620
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
593
271
                                 bool isLocal) {
594
271
  if (expr == R_MIPS_GOTREL && 
isLocal8
)
595
7
    return sec.getFile<ELFT>()->mipsGp0;
596
264
597
264
  // The ABI says that the paired relocation is used only for REL.
598
264
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
599
264
  if (RelTy::IsRela)
600
0
    return 0;
601
264
602
264
  RelType type = rel.getType(config->isMips64EL);
603
264
  uint32_t pairTy = getMipsPairType(type, isLocal);
604
264
  if (pairTy == R_MIPS_NONE)
605
221
    return 0;
606
43
607
43
  const uint8_t *buf = sec.data().data();
608
43
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
609
43
610
43
  // To make things worse, paired relocations might not be contiguous in
611
43
  // the relocation table, so we need to do linear search. *sigh*
612
87
  for (const RelTy *ri = &rel; ri != end; 
++ri44
)
613
86
    if (ri->getType(config->isMips64EL) == pairTy &&
614
86
        
ri->getSymbol(config->isMips64EL) == symIndex42
)
615
42
      return target->getImplicitAddend(buf + ri->r_offset, pairTy);
616
43
617
43
  warn("can't find matching " + toString(pairTy) + " relocation for " +
618
1
       toString(type));
619
1
  return 0;
620
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
593
10.0k
                                 bool isLocal) {
594
10.0k
  if (expr == R_MIPS_GOTREL && 
isLocal10
)
595
1
    return sec.getFile<ELFT>()->mipsGp0;
596
10.0k
597
10.0k
  // The ABI says that the paired relocation is used only for REL.
598
10.0k
  // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
599
10.0k
  if (RelTy::IsRela)
600
10.0k
    return 0;
601
0
602
0
  RelType type = rel.getType(config->isMips64EL);
603
0
  uint32_t pairTy = getMipsPairType(type, isLocal);
604
0
  if (pairTy == R_MIPS_NONE)
605
0
    return 0;
606
0
607
0
  const uint8_t *buf = sec.data().data();
608
0
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
609
0
610
0
  // To make things worse, paired relocations might not be contiguous in
611
0
  // the relocation table, so we need to do linear search. *sigh*
612
0
  for (const RelTy *ri = &rel; ri != end; ++ri)
613
0
    if (ri->getType(config->isMips64EL) == pairTy &&
614
0
        ri->getSymbol(config->isMips64EL) == symIndex)
615
0
      return target->getImplicitAddend(buf + ri->r_offset, pairTy);
616
0
617
0
  warn("can't find matching " + toString(pairTy) + " relocation for " +
618
0
       toString(type));
619
0
  return 0;
620
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)
621
622
// Returns an addend of a given relocation. If it is RELA, an addend
623
// is in a relocation itself. If it is REL, we need to read it from an
624
// input section.
625
template <class ELFT, class RelTy>
626
static int64_t computeAddend(const RelTy &rel, const RelTy *end,
627
                             InputSectionBase &sec, RelExpr expr,
628
14.6k
                             bool isLocal) {
629
14.6k
  int64_t addend;
630
14.6k
  RelType type = rel.getType(config->isMips64EL);
631
14.6k
632
14.6k
  if (RelTy::IsRela) {
633
13.5k
    addend = getAddend<ELFT>(rel);
634
13.5k
  } else {
635
1.13k
    const uint8_t *buf = sec.data().data();
636
1.13k
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
1.13k
  }
638
14.6k
639
14.6k
  if (config->emachine == EM_PPC64 && 
config->isPic993
&&
type == R_PPC64_TOC278
)
640
2
    addend += getPPC64TocBase();
641
14.6k
  if (config->emachine == EM_MIPS)
642
10.3k
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
14.6k
644
14.6k
  return addend;
645
14.6k
}
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
628
242
                             bool isLocal) {
629
242
  int64_t addend;
630
242
  RelType type = rel.getType(config->isMips64EL);
631
242
632
242
  if (RelTy::IsRela) {
633
242
    addend = getAddend<ELFT>(rel);
634
242
  } else {
635
0
    const uint8_t *buf = sec.data().data();
636
0
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
0
  }
638
242
639
242
  if (config->emachine == EM_PPC64 && 
config->isPic0
&&
type == R_PPC64_TOC0
)
640
0
    addend += getPPC64TocBase();
641
242
  if (config->emachine == EM_MIPS)
642
0
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
242
644
242
  return addend;
645
242
}
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
628
860
                             bool isLocal) {
629
860
  int64_t addend;
630
860
  RelType type = rel.getType(config->isMips64EL);
631
860
632
860
  if (RelTy::IsRela) {
633
0
    addend = getAddend<ELFT>(rel);
634
860
  } else {
635
860
    const uint8_t *buf = sec.data().data();
636
860
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
860
  }
638
860
639
860
  if (config->emachine == EM_PPC64 && 
config->isPic0
&&
type == R_PPC64_TOC0
)
640
0
    addend += getPPC64TocBase();
641
860
  if (config->emachine == EM_MIPS)
642
21
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
860
644
860
  return addend;
645
860
}
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
628
136
                             bool isLocal) {
629
136
  int64_t addend;
630
136
  RelType type = rel.getType(config->isMips64EL);
631
136
632
136
  if (RelTy::IsRela) {
633
136
    addend = getAddend<ELFT>(rel);
634
136
  } else {
635
0
    const uint8_t *buf = sec.data().data();
636
0
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
0
  }
638
136
639
136
  if (config->emachine == EM_PPC64 && 
config->isPic0
&&
type == R_PPC64_TOC0
)
640
0
    addend += getPPC64TocBase();
641
136
  if (config->emachine == EM_MIPS)
642
5
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
136
644
136
  return addend;
645
136
}
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
628
271
                             bool isLocal) {
629
271
  int64_t addend;
630
271
  RelType type = rel.getType(config->isMips64EL);
631
271
632
271
  if (RelTy::IsRela) {
633
0
    addend = getAddend<ELFT>(rel);
634
271
  } else {
635
271
    const uint8_t *buf = sec.data().data();
636
271
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
271
  }
638
271
639
271
  if (config->emachine == EM_PPC64 && 
config->isPic0
&&
type == R_PPC64_TOC0
)
640
0
    addend += getPPC64TocBase();
641
271
  if (config->emachine == EM_MIPS)
642
271
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
271
644
271
  return addend;
645
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
628
2.72k
                             bool isLocal) {
629
2.72k
  int64_t addend;
630
2.72k
  RelType type = rel.getType(config->isMips64EL);
631
2.72k
632
2.72k
  if (RelTy::IsRela) {
633
2.72k
    addend = getAddend<ELFT>(rel);
634
2.72k
  } else {
635
0
    const uint8_t *buf = sec.data().data();
636
0
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
0
  }
638
2.72k
639
2.72k
  if (config->emachine == EM_PPC64 && 
config->isPic647
&&
type == R_PPC64_TOC155
)
640
1
    addend += getPPC64TocBase();
641
2.72k
  if (config->emachine == EM_MIPS)
642
0
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
2.72k
644
2.72k
  return addend;
645
2.72k
}
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
628
2
                             bool isLocal) {
629
2
  int64_t addend;
630
2
  RelType type = rel.getType(config->isMips64EL);
631
2
632
2
  if (RelTy::IsRela) {
633
0
    addend = getAddend<ELFT>(rel);
634
2
  } else {
635
2
    const uint8_t *buf = sec.data().data();
636
2
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
2
  }
638
2
639
2
  if (config->emachine == EM_PPC64 && 
config->isPic0
&&
type == R_PPC64_TOC0
)
640
0
    addend += getPPC64TocBase();
641
2
  if (config->emachine == EM_MIPS)
642
0
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
2
644
2
  return addend;
645
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
628
10.4k
                             bool isLocal) {
629
10.4k
  int64_t addend;
630
10.4k
  RelType type = rel.getType(config->isMips64EL);
631
10.4k
632
10.4k
  if (RelTy::IsRela) {
633
10.4k
    addend = getAddend<ELFT>(rel);
634
10.4k
  } else {
635
0
    const uint8_t *buf = sec.data().data();
636
0
    addend = target->getImplicitAddend(buf + rel.r_offset, type);
637
0
  }
638
10.4k
639
10.4k
  if (config->emachine == EM_PPC64 && 
config->isPic346
&&
type == R_PPC64_TOC123
)
640
1
    addend += getPPC64TocBase();
641
10.4k
  if (config->emachine == EM_MIPS)
642
10.0k
    addend += computeMipsAddend<ELFT>(rel, end, sec, expr, isLocal);
643
10.4k
644
10.4k
  return addend;
645
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)
646
647
// Custom error message if Sym is defined in a discarded section.
648
template <class ELFT>
649
99
static std::string maybeReportDiscarded(Undefined &sym) {
650
99
  auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
651
99
  if (!file || 
!sym.discardedSecIdx91
||
652
99
      
file->getSections()[sym.discardedSecIdx] != &InputSection::discarded4
)
653
95
    return "";
654
4
  ArrayRef<Elf_Shdr_Impl<ELFT>> objSections =
655
4
      CHECK(file->getObj().sections(), file);
656
4
657
4
  std::string msg;
658
4
  if (sym.type == ELF::STT_SECTION) {
659
2
    msg = "relocation refers to a discarded section: ";
660
2
    msg += CHECK(
661
2
        file->getObj().getSectionName(&objSections[sym.discardedSecIdx]), file);
662
2
  } else {
663
2
    msg = "relocation refers to a symbol in a discarded section: " +
664
2
          toString(sym);
665
2
  }
666
4
  msg += "\n>>> defined in " + toString(file);
667
4
668
4
  Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1];
669
4
  if (elfSec.sh_type != SHT_GROUP)
670
1
    return msg;
671
3
672
3
  // If the discarded section is a COMDAT.
673
3
  StringRef signature = file->getShtGroupSignature(objSections, elfSec);
674
3
  if (const InputFile *prevailing =
675
3
          symtab->comdatGroups.lookup(CachedHashStringRef(signature)))
676
3
    msg += "\n>>> section group signature: " + signature.str() +
677
3
           "\n>>> prevailing definition is in " + toString(prevailing);
678
3
  return msg;
679
3
}
Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > maybeReportDiscarded<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::Undefined&)
Line
Count
Source
649
2
static std::string maybeReportDiscarded(Undefined &sym) {
650
2
  auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
651
2
  if (!file || !sym.discardedSecIdx ||
652
2
      
file->getSections()[sym.discardedSecIdx] != &InputSection::discarded0
)
653
2
    return "";
654
0
  ArrayRef<Elf_Shdr_Impl<ELFT>> objSections =
655
0
      CHECK(file->getObj().sections(), file);
656
0
657
0
  std::string msg;
658
0
  if (sym.type == ELF::STT_SECTION) {
659
0
    msg = "relocation refers to a discarded section: ";
660
0
    msg += CHECK(
661
0
        file->getObj().getSectionName(&objSections[sym.discardedSecIdx]), file);
662
0
  } else {
663
0
    msg = "relocation refers to a symbol in a discarded section: " +
664
0
          toString(sym);
665
0
  }
666
0
  msg += "\n>>> defined in " + toString(file);
667
0
668
0
  Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1];
669
0
  if (elfSec.sh_type != SHT_GROUP)
670
0
    return msg;
671
0
672
0
  // If the discarded section is a COMDAT.
673
0
  StringRef signature = file->getShtGroupSignature(objSections, elfSec);
674
0
  if (const InputFile *prevailing =
675
0
          symtab->comdatGroups.lookup(CachedHashStringRef(signature)))
676
0
    msg += "\n>>> section group signature: " + signature.str() +
677
0
           "\n>>> prevailing definition is in " + toString(prevailing);
678
0
  return msg;
679
0
}
Unexecuted instantiation: Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > maybeReportDiscarded<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::Undefined&)
Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > maybeReportDiscarded<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::Undefined&)
Line
Count
Source
649
97
static std::string maybeReportDiscarded(Undefined &sym) {
650
97
  auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file);
651
97
  if (!file || 
!sym.discardedSecIdx89
||
652
97
      
file->getSections()[sym.discardedSecIdx] != &InputSection::discarded4
)
653
93
    return "";
654
4
  ArrayRef<Elf_Shdr_Impl<ELFT>> objSections =
655
4
      CHECK(file->getObj().sections(), file);
656
4
657
4
  std::string msg;
658
4
  if (sym.type == ELF::STT_SECTION) {
659
2
    msg = "relocation refers to a discarded section: ";
660
2
    msg += CHECK(
661
2
        file->getObj().getSectionName(&objSections[sym.discardedSecIdx]), file);
662
2
  } else {
663
2
    msg = "relocation refers to a symbol in a discarded section: " +
664
2
          toString(sym);
665
2
  }
666
4
  msg += "\n>>> defined in " + toString(file);
667
4
668
4
  Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1];
669
4
  if (elfSec.sh_type != SHT_GROUP)
670
1
    return msg;
671
3
672
3
  // If the discarded section is a COMDAT.
673
3
  StringRef signature = file->getShtGroupSignature(objSections, elfSec);
674
3
  if (const InputFile *prevailing =
675
3
          symtab->comdatGroups.lookup(CachedHashStringRef(signature)))
676
3
    msg += "\n>>> section group signature: " + signature.str() +
677
3
           "\n>>> prevailing definition is in " + toString(prevailing);
678
3
  return msg;
679
3
}
Unexecuted instantiation: Relocations.cpp:std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > maybeReportDiscarded<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::Undefined&)
680
681
// Undefined diagnostics are collected in a vector and emitted once all of
682
// them are known, so that some postprocessing on the list of undefined symbols
683
// can happen before lld emits diagnostics.
684
struct UndefinedDiag {
685
  Symbol *sym;
686
  struct Loc {
687
    InputSectionBase *sec;
688
    uint64_t offset;
689
  };
690
  std::vector<Loc> locs;
691
  bool isWarning;
692
};
693
694
static std::vector<UndefinedDiag> undefs;
695
696
template <class ELFT>
697
99
static void reportUndefinedSymbol(const UndefinedDiag &undef) {
698
99
  Symbol &sym = *undef.sym;
699
99
700
99
  auto visibility = [&]() -> std::string {
701
95
    switch (sym.visibility) {
702
95
    case STV_INTERNAL:
703
1
      return "internal ";
704
95
    case STV_HIDDEN:
705
7
      return "hidden ";
706
95
    case STV_PROTECTED:
707
5
      return "protected ";
708
95
    default:
709
82
      return "";
710
95
    }
711
95
  };
Relocations.cpp:void reportUndefinedSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(UndefinedDiag const&)::'lambda'()::operator()() const
Line
Count
Source
700
2
  auto visibility = [&]() -> std::string {
701
2
    switch (sym.visibility) {
702
2
    case STV_INTERNAL:
703
0
      return "internal ";
704
2
    case STV_HIDDEN:
705
0
      return "hidden ";
706
2
    case STV_PROTECTED:
707
0
      return "protected ";
708
2
    default:
709
2
      return "";
710
2
    }
711
2
  };
Relocations.cpp:void reportUndefinedSymbol<llvm::object::ELFType<(llvm::support::endianness)1, true> >(UndefinedDiag const&)::'lambda'()::operator()() const
Line
Count
Source
700
93
  auto visibility = [&]() -> std::string {
701
93
    switch (sym.visibility) {
702
93
    case STV_INTERNAL:
703
1
      return "internal ";
704
93
    case STV_HIDDEN:
705
7
      return "hidden ";
706
93
    case STV_PROTECTED:
707
5
      return "protected ";
708
93
    default:
709
80
      return "";
710
93
    }
711
93
  };
712
99
713
99
  std::string msg = maybeReportDiscarded<ELFT>(cast<Undefined>(sym));
714
99
  if (msg.empty())
715
95
    msg = "undefined " + visibility() + "symbol: " + toString(sym);
716
99
717
99
  const size_t maxUndefReferences = 10;
718
99
  size_t i = 0;
719
118
  for (UndefinedDiag::Loc l : undef.locs) {
720
118
    if (i >= maxUndefReferences)
721
1
      break;
722
117
    InputSectionBase &sec = *l.sec;
723
117
    uint64_t offset = l.offset;
724
117
725
117
    msg += "\n>>> referenced by ";
726
117
    std::string src = sec.getSrcMsg(sym, offset);
727
117
    if (!src.empty())
728
44
      msg += src + "\n>>>               ";
729
117
    msg += sec.getObjMsg(offset);
730
117
    i++;
731
117
  }
732
99
733
99
  if (i < undef.locs.size())
734
1
    msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times")
735
1
               .str();
736
99
737
99
  if (sym.getName().startswith("_ZTV"))
738
3
    msg += "\nthe vtable symbol may be undefined because the class is missing "
739
3
           "its key function (see https://lld.llvm.org/missingkeyfunction)";
740
99
741
99
  if (undef.isWarning)
742
16
    warn(msg);
743
83
  else
744
83
    error(msg);
745
99
}
Relocations.cpp:void reportUndefinedSymbol<llvm::object::ELFType<(llvm::support::endianness)1, false> >(UndefinedDiag const&)
Line
Count
Source
697
2
static void reportUndefinedSymbol(const UndefinedDiag &undef) {
698
2
  Symbol &sym = *undef.sym;
699
2
700
2
  auto visibility = [&]() -> std::string {
701
2
    switch (sym.visibility) {
702
2
    case STV_INTERNAL:
703
2
      return "internal ";
704
2
    case STV_HIDDEN:
705
2
      return "hidden ";
706
2
    case STV_PROTECTED:
707
2
      return "protected ";
708
2
    default:
709
2
      return "";
710
2
    }
711
2
  };
712
2
713
2
  std::string msg = maybeReportDiscarded<ELFT>(cast<Undefined>(sym));
714
2
  if (msg.empty())
715
2
    msg = "undefined " + visibility() + "symbol: " + toString(sym);
716
2
717
2
  const size_t maxUndefReferences = 10;
718
2
  size_t i = 0;
719
2
  for (UndefinedDiag::Loc l : undef.locs) {
720
2
    if (i >= maxUndefReferences)
721
0
      break;
722
2
    InputSectionBase &sec = *l.sec;
723
2
    uint64_t offset = l.offset;
724
2
725
2
    msg += "\n>>> referenced by ";
726
2
    std::string src = sec.getSrcMsg(sym, offset);
727
2
    if (!src.empty())
728
0
      msg += src + "\n>>>               ";
729
2
    msg += sec.getObjMsg(offset);
730
2
    i++;
731
2
  }
732
2
733
2
  if (i < undef.locs.size())
734
0
    msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times")
735
0
               .str();
736
2
737
2
  if (sym.getName().startswith("_ZTV"))
738
0
    msg += "\nthe vtable symbol may be undefined because the class is missing "
739
0
           "its key function (see https://lld.llvm.org/missingkeyfunction)";
740
2
741
2
  if (undef.isWarning)
742
1
    warn(msg);
743
1
  else
744
1
    error(msg);
745
2
}
Unexecuted instantiation: Relocations.cpp:void reportUndefinedSymbol<llvm::object::ELFType<(llvm::support::endianness)0, false> >(UndefinedDiag const&)
Relocations.cpp:void reportUndefinedSymbol<llvm::object::ELFType<(llvm::support::endianness)1, true> >(UndefinedDiag const&)
Line
Count
Source
697
97
static void reportUndefinedSymbol(const UndefinedDiag &undef) {
698
97
  Symbol &sym = *undef.sym;
699
97
700
97
  auto visibility = [&]() -> std::string {
701
97
    switch (sym.visibility) {
702
97
    case STV_INTERNAL:
703
97
      return "internal ";
704
97
    case STV_HIDDEN:
705
97
      return "hidden ";
706
97
    case STV_PROTECTED:
707
97
      return "protected ";
708
97
    default:
709
97
      return "";
710
97
    }
711
97
  };
712
97
713
97
  std::string msg = maybeReportDiscarded<ELFT>(cast<Undefined>(sym));
714
97
  if (msg.empty())
715
93
    msg = "undefined " + visibility() + "symbol: " + toString(sym);
716
97
717
97
  const size_t maxUndefReferences = 10;
718
97
  size_t i = 0;
719
116
  for (UndefinedDiag::Loc l : undef.locs) {
720
116
    if (i >= maxUndefReferences)
721
1
      break;
722
115
    InputSectionBase &sec = *l.sec;
723
115
    uint64_t offset = l.offset;
724
115
725
115
    msg += "\n>>> referenced by ";
726
115
    std::string src = sec.getSrcMsg(sym, offset);
727
115
    if (!src.empty())
728
44
      msg += src + "\n>>>               ";
729
115
    msg += sec.getObjMsg(offset);
730
115
    i++;
731
115
  }
732
97
733
97
  if (i < undef.locs.size())
734
1
    msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times")
735
1
               .str();
736
97
737
97
  if (sym.getName().startswith("_ZTV"))
738
3
    msg += "\nthe vtable symbol may be undefined because the class is missing "
739
3
           "its key function (see https://lld.llvm.org/missingkeyfunction)";
740
97
741
97
  if (undef.isWarning)
742
15
    warn(msg);
743
82
  else
744
82
    error(msg);
745
97
}
Unexecuted instantiation: Relocations.cpp:void reportUndefinedSymbol<llvm::object::ELFType<(llvm::support::endianness)0, true> >(UndefinedDiag const&)
746
747
2.66k
template <class ELFT> void elf::reportUndefinedSymbols() {
748
2.66k
  // Find the first "undefined symbol" diagnostic for each diagnostic, and
749
2.66k
  // collect all "referenced from" lines at the first diagnostic.
750
2.66k
  DenseMap<Symbol *, UndefinedDiag *> firstRef;
751
2.66k
  for (UndefinedDiag &undef : undefs) {
752
119
    assert(undef.locs.size() == 1);
753
119
    if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
754
20
      canon->locs.push_back(undef.locs[0]);
755
20
      undef.locs.clear();
756
20
    } else
757
99
      firstRef[undef.sym] = &undef;
758
119
  }
759
2.66k
760
2.66k
  for (const UndefinedDiag &undef : undefs) {
761
119
    if (!undef.locs.empty())
762
99
      reportUndefinedSymbol<ELFT>(undef);
763
119
  }
764
2.66k
  undefs.clear();
765
2.66k
}
void lld::elf::reportUndefinedSymbols<llvm::object::ELFType<(llvm::support::endianness)1, false> >()
Line
Count
Source
747
365
template <class ELFT> void elf::reportUndefinedSymbols() {
748
365
  // Find the first "undefined symbol" diagnostic for each diagnostic, and
749
365
  // collect all "referenced from" lines at the first diagnostic.
750
365
  DenseMap<Symbol *, UndefinedDiag *> firstRef;
751
365
  for (UndefinedDiag &undef : undefs) {
752
2
    assert(undef.locs.size() == 1);
753
2
    if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
754
0
      canon->locs.push_back(undef.locs[0]);
755
0
      undef.locs.clear();
756
0
    } else
757
2
      firstRef[undef.sym] = &undef;
758
2
  }
759
365
760
365
  for (const UndefinedDiag &undef : undefs) {
761
2
    if (!undef.locs.empty())
762
2
      reportUndefinedSymbol<ELFT>(undef);
763
2
  }
764
365
  undefs.clear();
765
365
}
void lld::elf::reportUndefinedSymbols<llvm::object::ELFType<(llvm::support::endianness)0, false> >()
Line
Count
Source
747
146
template <class ELFT> void elf::reportUndefinedSymbols() {
748
146
  // Find the first "undefined symbol" diagnostic for each diagnostic, and
749
146
  // collect all "referenced from" lines at the first diagnostic.
750
146
  DenseMap<Symbol *, UndefinedDiag *> firstRef;
751
146
  for (UndefinedDiag &undef : undefs) {
752
0
    assert(undef.locs.size() == 1);
753
0
    if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
754
0
      canon->locs.push_back(undef.locs[0]);
755
0
      undef.locs.clear();
756
0
    } else
757
0
      firstRef[undef.sym] = &undef;
758
0
  }
759
146
760
146
  for (const UndefinedDiag &undef : undefs) {
761
0
    if (!undef.locs.empty())
762
0
      reportUndefinedSymbol<ELFT>(undef);
763
0
  }
764
146
  undefs.clear();
765
146
}
void lld::elf::reportUndefinedSymbols<llvm::object::ELFType<(llvm::support::endianness)1, true> >()
Line
Count
Source
747
2.05k
template <class ELFT> void elf::reportUndefinedSymbols() {
748
2.05k
  // Find the first "undefined symbol" diagnostic for each diagnostic, and
749
2.05k
  // collect all "referenced from" lines at the first diagnostic.
750
2.05k
  DenseMap<Symbol *, UndefinedDiag *> firstRef;
751
2.05k
  for (UndefinedDiag &undef : undefs) {
752
117
    assert(undef.locs.size() == 1);
753
117
    if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
754
20
      canon->locs.push_back(undef.locs[0]);
755
20
      undef.locs.clear();
756
20
    } else
757
97
      firstRef[undef.sym] = &undef;
758
117
  }
759
2.05k
760
2.05k
  for (const UndefinedDiag &undef : undefs) {
761
117
    if (!undef.locs.empty())
762
97
      reportUndefinedSymbol<ELFT>(undef);
763
117
  }
764
2.05k
  undefs.clear();
765
2.05k
}
void lld::elf::reportUndefinedSymbols<llvm::object::ELFType<(llvm::support::endianness)0, true> >()
Line
Count
Source
747
105
template <class ELFT> void elf::reportUndefinedSymbols() {
748
105
  // Find the first "undefined symbol" diagnostic for each diagnostic, and
749
105
  // collect all "referenced from" lines at the first diagnostic.
750
105
  DenseMap<Symbol *, UndefinedDiag *> firstRef;
751
105
  for (UndefinedDiag &undef : undefs) {
752
0
    assert(undef.locs.size() == 1);
753
0
    if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {
754
0
      canon->locs.push_back(undef.locs[0]);
755
0
      undef.locs.clear();
756
0
    } else
757
0
      firstRef[undef.sym] = &undef;
758
0
  }
759
105
760
105
  for (const UndefinedDiag &undef : undefs) {
761
0
    if (!undef.locs.empty())
762
0
      reportUndefinedSymbol<ELFT>(undef);
763
0
  }
764
105
  undefs.clear();
765
105
}
766
767
// Report an undefined symbol if necessary.
768
// Returns true if the undefined symbol will produce an error message.
769
template <class ELFT>
770
static bool maybeReportUndefined(Symbol &sym, InputSectionBase &sec,
771
14.7k
                                 uint64_t offset) {
772
14.7k
  if (!sym.isUndefined() || 
sym.isWeak()439
)
773
14.4k
    return false;
774
350
775
350
  bool canBeExternal = !sym.isLocal() && 
sym.computeBinding() != STB_LOCAL346
&&
776
350
                       
sym.visibility == STV_DEFAULT336
;
777
350
  if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && 
canBeExternal238
)
778
230
    return false;
779
120
780
120
  // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
781
120
  // which references a switch table in a discarded .rodata/.text section. The
782
120
  // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
783
120
  // spec says references from outside the group to a STB_LOCAL symbol are not
784
120
  // allowed. Work around the bug.
785
120
  if (config->emachine == EM_PPC64 &&
786
120
      
cast<Undefined>(sym).discardedSecIdx != 01
&&
sec.name == ".toc"1
)
787
1
    return false;
788
119
789
119
  bool isWarning =
790
119
      (config->unresolvedSymbols == UnresolvedPolicy::Warn && 
canBeExternal8
) ||
791
119
      
config->noinhibitExec113
;
792
119
  undefs.push_back({&sym, {{&sec, offset}}, isWarning});
793
119
  return !isWarning;
794
119
}
Relocations.cpp:bool maybeReportUndefined<llvm::object::ELFType<(llvm::support::endianness)1, false> >(lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
771
1.12k
                                 uint64_t offset) {
772
1.12k
  if (!sym.isUndefined() || 
sym.isWeak()48
)
773
1.10k
    return false;
774
26
775
26
  bool canBeExternal = !sym.isLocal() && sym.computeBinding() != STB_LOCAL &&
776
26
                       sym.visibility == STV_DEFAULT;
777
26
  if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && 
canBeExternal24
)
778
24
    return false;
779
2
780
2
  // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
781
2
  // which references a switch table in a discarded .rodata/.text section. The
782
2
  // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
783
2
  // spec says references from outside the group to a STB_LOCAL symbol are not
784
2
  // allowed. Work around the bug.
785
2
  if (config->emachine == EM_PPC64 &&
786
2
      
cast<Undefined>(sym).discardedSecIdx != 00
&&
sec.name == ".toc"0
)
787
0
    return false;
788
2
789
2
  bool isWarning =
790
2
      (config->unresolvedSymbols == UnresolvedPolicy::Warn && 
canBeExternal0
) ||
791
2
      config->noinhibitExec;
792
2
  undefs.push_back({&sym, {{&sec, offset}}, isWarning});
793
2
  return !isWarning;
794
2
}
Relocations.cpp:bool maybeReportUndefined<llvm::object::ELFType<(llvm::support::endianness)0, false> >(lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
771
407
                                 uint64_t offset) {
772
407
  if (!sym.isUndefined() || 
sym.isWeak()22
)
773
390
    return false;
774
17
775
17
  bool canBeExternal = !sym.isLocal() && sym.computeBinding() != STB_LOCAL &&
776
17
                       sym.visibility == STV_DEFAULT;
777
17
  if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal)
778
17
    return false;
779
0
780
0
  // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
781
0
  // which references a switch table in a discarded .rodata/.text section. The
782
0
  // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
783
0
  // spec says references from outside the group to a STB_LOCAL symbol are not
784
0
  // allowed. Work around the bug.
785
0
  if (config->emachine == EM_PPC64 &&
786
0
      cast<Undefined>(sym).discardedSecIdx != 0 && sec.name == ".toc")
787
0
    return false;
788
0
789
0
  bool isWarning =
790
0
      (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||
791
0
      config->noinhibitExec;
792
0
  undefs.push_back({&sym, {{&sec, offset}}, isWarning});
793
0
  return !isWarning;
794
0
}
Relocations.cpp:bool maybeReportUndefined<llvm::object::ELFType<(llvm::support::endianness)1, true> >(lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
771
2.83k
                                 uint64_t offset) {
772
2.83k
  if (!sym.isUndefined() || 
sym.isWeak()354
)
773
2.54k
    return false;
774
295
775
295
  bool canBeExternal = !sym.isLocal() && 
sym.computeBinding() != STB_LOCAL291
&&
776
295
                       
sym.visibility == STV_DEFAULT281
;
777
295
  if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && 
canBeExternal185
)
778
177
    return false;
779
118
780
118
  // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
781
118
  // which references a switch table in a discarded .rodata/.text section. The
782
118
  // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
783
118
  // spec says references from outside the group to a STB_LOCAL symbol are not
784
118
  // allowed. Work around the bug.
785
118
  if (config->emachine == EM_PPC64 &&
786
118
      
cast<Undefined>(sym).discardedSecIdx != 01
&&
sec.name == ".toc"1
)
787
1
    return false;
788
117
789
117
  bool isWarning =
790
117
      (config->unresolvedSymbols == UnresolvedPolicy::Warn && 
canBeExternal8
) ||
791
117
      
config->noinhibitExec111
;
792
117
  undefs.push_back({&sym, {{&sec, offset}}, isWarning});
793
117
  return !isWarning;
794
117
}
Relocations.cpp:bool maybeReportUndefined<llvm::object::ELFType<(llvm::support::endianness)0, true> >(lld::elf::Symbol&, lld::elf::InputSectionBase&, unsigned long long)
Line
Count
Source
771
10.4k
                                 uint64_t offset) {
772
10.4k
  if (!sym.isUndefined() || 
sym.isWeak()15
)
773
10.3k
    return false;
774
12
775
12
  bool canBeExternal = !sym.isLocal() && sym.computeBinding() != STB_LOCAL &&
776
12
                       sym.visibility == STV_DEFAULT;
777
12
  if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal)
778
12
    return false;
779
0
780
0
  // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc
781
0
  // which references a switch table in a discarded .rodata/.text section. The
782
0
  // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF
783
0
  // spec says references from outside the group to a STB_LOCAL symbol are not
784
0
  // allowed. Work around the bug.
785
0
  if (config->emachine == EM_PPC64 &&
786
0
      cast<Undefined>(sym).discardedSecIdx != 0 && sec.name == ".toc")
787
0
    return false;
788
0
789
0
  bool isWarning =
790
0
      (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||
791
0
      config->noinhibitExec;
792
0
  undefs.push_back({&sym, {{&sec, offset}}, isWarning});
793
0
  return !isWarning;
794
0
}
795
796
// MIPS N32 ABI treats series of successive relocations with the same offset
797
// as a single relocation. The similar approach used by N64 ABI, but this ABI
798
// packs all relocations into the single relocation record. Here we emulate
799
// this for the N32 ABI. Iterate over relocation with the same offset and put
800
// theirs types into the single bit-set.
801
5
template <class RelTy> static RelType getMipsN32RelType(RelTy *&rel, RelTy *end) {
802
5
  RelType type = 0;
803
5
  uint64_t offset = rel->r_offset;
804
5
805
5
  int n = 0;
806
14
  while (rel != end && 
rel->r_offset == offset10
)
807
9
    type |= (rel++)->getType(config->isMips64EL) << (8 * n++);
808
5
  return type;
809
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
801
5
template <class RelTy> static RelType getMipsN32RelType(RelTy *&rel, RelTy *end) {
802
5
  RelType type = 0;
803
5
  uint64_t offset = rel->r_offset;
804
5
805
5
  int n = 0;
806
14
  while (rel != end && 
rel->r_offset == offset10
)
807
9
    type |= (rel++)->getType(config->isMips64EL) << (8 * n++);
808
5
  return type;
809
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*)
810
811
// .eh_frame sections are mergeable input sections, so their input
812
// offsets are not linearly mapped to output section. For each input
813
// offset, we need to find a section piece containing the offset and
814
// add the piece's base address to the input offset to compute the
815
// output offset. That isn't cheap.
816
//
817
// This class is to speed up the offset computation. When we process
818
// relocations, we access offsets in the monotonically increasing
819
// order. So we can optimize for that access pattern.
820
//
821
// For sections other than .eh_frame, this class doesn't do anything.
822
namespace {
823
class OffsetGetter {
824
public:
825
112k
  explicit OffsetGetter(InputSectionBase &sec) {
826
112k
    if (auto *eh = dyn_cast<EhInputSection>(&sec))
827
117
      pieces = eh->pieces;
828
112k
  }
829
830
  // Translates offsets in input sections to offsets in output sections.
831
  // Given offset must increase monotonically. We assume that Piece is
832
  // sorted by inputOff.
833
14.8k
  uint64_t get(uint64_t off) {
834
14.8k
    if (pieces.empty())
835
14.6k
      return off;
836
151
837
287
    
while (151
i != pieces.size() && pieces[i].inputOff + pieces[i].size <= off)
838
136
      ++i;
839
151
    if (i == pieces.size())
840
0
      fatal(".eh_frame: relocation is not in any piece");
841
151
842
151
    // Pieces must be contiguous, so there must be no holes in between.
843
151
    assert(pieces[i].inputOff <= off && "Relocation not in any piece");
844
151
845
151
    // Offset -1 means that the piece is dead (i.e. garbage collected).
846
151
    if (pieces[i].outputOff == -1)
847
15
      return -1;
848
136
    return pieces[i].outputOff + off - pieces[i].inputOff;
849
136
  }
850
851
private:
852
  ArrayRef<EhSectionPiece> pieces;
853
  size_t i = 0;
854
};
855
} // namespace
856
857
static void addRelativeReloc(InputSectionBase *isec, uint64_t offsetInSec,
858
                             Symbol *sym, int64_t addend, RelExpr expr,
859
281
                             RelType type) {
860
281
  Partition &part = isec->getPartition();
861
281
862
281
  // Add a relative relocation. If relrDyn section is enabled, and the
863
281
  // relocation offset is guaranteed to be even, add the relocation to
864
281
  // the relrDyn section, otherwise add it to the relaDyn section.
865
281
  // relrDyn sections don't support odd offsets. Also, relrDyn sections
866
281
  // don't store the addend values, so we must write it to the relocated
867
281
  // address.
868
281
  if (part.relrDyn && 
isec->alignment >= 286
&&
offsetInSec % 2 == 086
) {
869
84
    isec->relocations.push_back({expr, type, offsetInSec, addend, sym});
870
84
    part.relrDyn->relocs.push_back({isec, offsetInSec});
871
84
    return;
872
84
  }
873
197
  part.relaDyn->addReloc(target->relativeRel, isec, offsetInSec, sym, addend,
874
197
                         expr, type);
875
197
}
876
877
template <class ELFT, class GotPltSection>
878
static void addPltEntry(PltSection *plt, GotPltSection *gotPlt,
879
421
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
421
  plt->addEntry<ELFT>(sym);
881
421
  gotPlt->addEntry(sym);
882
421
  rel->addReloc(
883
421
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
421
}
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
879
79
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
79
  plt->addEntry<ELFT>(sym);
881
79
  gotPlt->addEntry(sym);
882
79
  rel->addReloc(
883
79
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
79
}
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
879
13
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
13
  plt->addEntry<ELFT>(sym);
881
13
  gotPlt->addEntry(sym);
882
13
  rel->addReloc(
883
13
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
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
879
24
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
24
  plt->addEntry<ELFT>(sym);
881
24
  gotPlt->addEntry(sym);
882
24
  rel->addReloc(
883
24
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
24
}
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&)
Line
Count
Source
879
2
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
2
  plt->addEntry<ELFT>(sym);
881
2
  gotPlt->addEntry(sym);
882
2
  rel->addReloc(
883
2
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
2
}
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
879
244
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
244
  plt->addEntry<ELFT>(sym);
881
244
  gotPlt->addEntry(sym);
882
244
  rel->addReloc(
883
244
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
244
}
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
879
45
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
45
  plt->addEntry<ELFT>(sym);
881
45
  gotPlt->addEntry(sym);
882
45
  rel->addReloc(
883
45
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
45
}
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
879
12
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
12
  plt->addEntry<ELFT>(sym);
881
12
  gotPlt->addEntry(sym);
882
12
  rel->addReloc(
883
12
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
12
}
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
879
2
                        RelocationBaseSection *rel, RelType type, Symbol &sym) {
880
2
  plt->addEntry<ELFT>(sym);
881
2
  gotPlt->addEntry(sym);
882
2
  rel->addReloc(
883
2
      {type, gotPlt, sym.getGotPltOffset(), !sym.isPreemptible, &sym, 0});
884
2
}
885
886
179
static void addGotEntry(Symbol &sym) {
887
179
  in.got->addEntry(sym);
888
179
889
179
  RelExpr expr = sym.isTls() ? 
R_TLS46
:
R_ABS133
;
890
179
  uint64_t off = sym.getGotOffset();
891
179
892
179
  // If a GOT slot value can be calculated at link-time, which is now,
893
179
  // we can just fill that out.
894
179
  //
895
179
  // (We don't actually write a value to a GOT slot right now, but we
896
179
  // add a static relocation to a Relocations vector so that
897
179
  // InputSection::relocate will do the work for us. We may be able
898
179
  // to just write a value now, but it is a TODO.)
899
179
  bool isLinkTimeConstant =
900
179
      !sym.isPreemptible && 
(71
!config->isPic71
||
isAbsolute(sym)35
);
901
179
  if (isLinkTimeConstant) {
902
41
    in.got->relocations.push_back({expr, target->symbolicRel, off, 0, &sym});
903
41
    return;
904
41
  }
905
138
906
138
  // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
907
138
  // the GOT slot will be fixed at load-time.
908
138
  if (!sym.isTls() && 
!sym.isPreemptible98
&&
config->isPic20
&&
!isAbsolute(sym)20
) {
909
20
    addRelativeReloc(in.got, off, &sym, 0, R_ABS, target->symbolicRel);
910
20
    return;
911
20
  }
912
118
  mainPart->relaDyn->addReloc(
913
118
      sym.isTls() ? 
target->tlsGotRel40
:
target->gotRel78
, in.got, off, &sym, 0,
914
118
      sym.isPreemptible ? 
R_ADDEND108
:
R_ABS10
, target->symbolicRel);
915
118
}
916
917
// Return true if we can define a symbol in the executable that
918
// contains the value/function of a symbol defined in a shared
919
// library.
920
120
static bool canDefineSymbolInExecutable(Symbol &sym) {
921
120
  // If the symbol has default visibility the symbol defined in the
922
120
  // executable will preempt it.
923
120
  // Note that we want the visibility of the shared symbol itself, not
924
120
  // the visibility of the symbol in the output file we are producing. That is
925
120
  // why we use Sym.stOther.
926
120
  if ((sym.stOther & 0x3) == STV_DEFAULT)
927
113
    return true;
928
7
929
7
  // If we are allowed to break address equality of functions, defining
930
7
  // a plt entry will allow the program to call the function in the
931
7
  // .so, but the .so and the executable will no agree on the address
932
7
  // of the function. Similar logic for objects.
933
7
  return ((sym.isFunc() && 
config->ignoreFunctionAddressEquality3
) ||
934
7
          
(6
sym.isObject()6
&&
config->ignoreDataAddressEquality2
));
935
7
}
936
937
// The reason we have to do this early scan is as follows
938
// * To mmap the output file, we need to know the size
939
// * For that, we need to know how many dynamic relocs we will have.
940
// It might be possible to avoid this by outputting the file with write:
941
// * Write the allocated output sections, computing addresses.
942
// * Apply relocations, recording which ones require a dynamic reloc.
943
// * Write the dynamic relocations.
944
// * Write the rest of the file.
945
// This would have some drawbacks. For example, we would only know if .rela.dyn
946
// is needed after applying relocations. If it is, it will go after rw and rx
947
// sections. Given that it is ro, we will need an extra PT_LOAD. This
948
// complicates things for the dynamic linker and means we would have to reserve
949
// space for the extra PT_LOAD even if we end up not using it.
950
template <class ELFT, class RelTy>
951
static void processRelocAux(InputSectionBase &sec, RelExpr expr, RelType type,
952
                            uint64_t offset, Symbol &sym, const RelTy &rel,
953
14.2k
                            int64_t addend) {
954
14.2k
  // If the relocation is known to be a link-time constant, we know no dynamic
955
14.2k
  // relocation will be created, pass the control to relocateAlloc() or
956
14.2k
  // relocateNonAlloc() to resolve it.
957
14.2k
  //
958
14.2k
  // The behavior of an undefined weak reference is implementation defined. If
959
14.2k
  // the relocation is to a weak undef, and we are producing an executable, let
960
14.2k
  // relocate{,Non}Alloc() resolve it.
961
14.2k
  if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
962
14.2k
      
(629
!config->shared629
&&
sym.isUndefWeak()407
)) {
963
13.6k
    sec.relocations.push_back({expr, type, offset, addend, &sym});
964
13.6k
    return;
965
13.6k
  }
966
614
967
614
  bool canWrite = (sec.flags & SHF_WRITE) || 
!config->zText165
;
968
614
  if (canWrite) {
969
468
    RelType rel = target->getDynRel(type);
970
468
    if (expr == R_GOT || 
(458
rel == target->symbolicRel458
&&
!sym.isPreemptible404
)) {
971
259
      addRelativeReloc(&sec, offset, &sym, addend, expr, type);
972
259
      return;
973
259
    } else 
if (209
rel != 0209
) {
974
179
      if (config->emachine == EM_MIPS && 
rel == target->symbolicRel4
)
975
4
        rel = target->relativeRel;
976
179
      sec.getPartition().relaDyn->addReloc(rel, &sec, offset, &sym, addend,
977
179
                                           R_ADDEND, type);
978
179
979
179
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
980
179
      // While regular ABI uses dynamic relocations to fill up GOT entries
981
179
      // MIPS ABI requires dynamic linker to fills up GOT entries using
982
179
      // specially sorted dynamic symbol table. This affects even dynamic
983
179
      // relocations against symbols which do not require GOT entries
984
179
      // creation explicitly, i.e. do not have any GOT-relocations. So if
985
179
      // a preemptible symbol has a dynamic relocation we anyway have
986
179
      // to create a GOT entry for it.
987
179
      // If a non-preemptible symbol has a dynamic relocation against it,
988
179
      // dynamic linker takes it st_value, adds offset and writes down
989
179
      // result of the dynamic relocation. In case of preemptible symbol
990
179
      // dynamic linker performs symbol resolution, writes the symbol value
991
179
      // to the GOT entry and reads the GOT entry when it needs to perform
992
179
      // a dynamic relocation.
993
179
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
994
179
      if (config->emachine == EM_MIPS)
995
4
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
996
179
      return;
997
179
    }
998
176
  }
999
176
1000
176
  if (!canWrite && 
(146
config->isPic146
&&
!isRelExpr(expr)32
)) {
1001
20
    error(
1002
20
        "can't create dynamic relocation " + toString(type) + " against " +
1003
20
        (sym.getName().empty() ? 
"local symbol"4
:
"symbol: " + toString(sym)16
) +
1004
20
        " in readonly segment; recompile object files with -fPIC "
1005
20
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
1006
20
        getLocation(sec, sym, offset));
1007
20
    return;
1008
20
  }
1009
156
1010
156
  // Copy relocations (for STT_OBJECT) and canonical PLT (for STT_FUNC) are only
1011
156
  // possible in an executable.
1012
156
  //
1013
156
  // Among R_ABS relocatoin types, symbolicRel has the same size as the word
1014
156
  // size. Others have fewer bits and may cause runtime overflow in -pie/-shared
1015
156
  // mode. Disallow them.
1016
156
  if (config->shared ||
1017
156
      
(127
config->pie127
&&
expr == R_ABS8
&&
type != target->symbolicRel4
)) {
1018
33
    errorOrWarn(
1019
33
        "relocation " + toString(type) + " cannot be used against " +
1020
33
        (sym.getName().empty() ? 
"local symbol"6
:
"symbol " + toString(sym)27
) +
1021
33
        "; recompile with -fPIC" + getLocation(sec, sym, offset));
1022
33
    return;
1023
33
  }
1024
123
1025
123
  // If the symbol is undefined we already reported any relevant errors.
1026
123
  if (sym.isUndefined())
1027
3
    return;
1028
120
1029
120
  if (!canDefineSymbolInExecutable(sym)) {
1030
5
    error("cannot preempt symbol: " + toString(sym) +
1031
5
          getLocation(sec, sym, offset));
1032
5
    return;
1033
5
  }
1034
115
1035
115
  if (sym.isObject()) {
1036
66
    // Produce a copy relocation.
1037
66
    if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1038
53
      if (!config->zCopyreloc)
1039
3
        error("unresolvable relocation " + toString(type) +
1040
3
              " against symbol '" + toString(*ss) +
1041
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1042
3
              getLocation(sec, sym, offset));
1043
53
      addCopyRelSymbol<ELFT>(*ss);
1044
53
    }
1045
66
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1046
66
    return;
1047
66
  }
1048
49
1049
49
  if (sym.isFunc()) {
1050
46
    // This handles a non PIC program call to function in a shared library. In
1051
46
    // an ideal world, we could just report an error saying the relocation can
1052
46
    // overflow at runtime. In the real world with glibc, crt1.o has a
1053
46
    // R_X86_64_PC32 pointing to libc.so.
1054
46
    //
1055
46
    // The general idea on how to handle such cases is to create a PLT entry and
1056
46
    // use that as the function value.
1057
46
    //
1058
46
    // For the static linking part, we just return a plt expr and everything
1059
46
    // else will use the PLT entry as the address.
1060
46
    //
1061
46
    // The remaining problem is making sure pointer equality still works. We
1062
46
    // need the help of the dynamic linker for that. We let it know that we have
1063
46
    // a direct reference to a so symbol by creating an undefined symbol with a
1064
46
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1065
46
    // the value of the symbol we created. This is true even for got entries, so
1066
46
    // pointer equality is maintained. To avoid an infinite loop, the only entry
1067
46
    // that points to the real function is a dedicated got entry used by the
1068
46
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1069
46
    // R_386_JMP_SLOT, etc).
1070
46
1071
46
    // For position independent executable on i386, the plt entry requires ebx
1072
46
    // to be set. This causes two problems:
1073
46
    // * If some code has a direct reference to a function, it was probably
1074
46
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
1075
46
    // * If a library definition gets preempted to the executable, it will have
1076
46
    //   the wrong ebx value.
1077
46
    if (config->pie && 
config->emachine == EM_3862
)
1078
1
      errorOrWarn("symbol '" + toString(sym) +
1079
1
                  "' cannot be preempted; recompile with -fPIE" +
1080
1
                  getLocation(sec, sym, offset));
1081
46
    if (!sym.isInPlt())
1082
42
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1083
46
    if (!sym.isDefined())
1084
43
      replaceWithDefined(
1085
43
          sym, in.plt,
1086
43
          target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0);
1087
46
    sym.needsPltAddr = true;
1088
46
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1089
46
    return;
1090
46
  }
1091
3
1092
3
  errorOrWarn("symbol '" + toString(sym) + "' has no type" +
1093
3
              getLocation(sec, sym, offset));
1094
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
953
232
                            int64_t addend) {
954
232
  // If the relocation is known to be a link-time constant, we know no dynamic
955
232
  // relocation will be created, pass the control to relocateAlloc() or
956
232
  // relocateNonAlloc() to resolve it.
957
232
  //
958
232
  // The behavior of an undefined weak reference is implementation defined. If
959
232
  // the relocation is to a weak undef, and we are producing an executable, let
960
232
  // relocate{,Non}Alloc() resolve it.
961
232
  if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
962
232
      
(4
!config->shared4
&&
sym.isUndefWeak()1
)) {
963
228
    sec.relocations.push_back({expr, type, offset, addend, &sym});
964
228
    return;
965
228
  }
966
4
967
4
  bool canWrite = (sec.flags & SHF_WRITE) || 
!config->zText2
;
968
4
  if (canWrite) {
969
2
    RelType rel = target->getDynRel(type);
970
2
    if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) {
971
1
      addRelativeReloc(&sec, offset, &sym, addend, expr, type);
972
1
      return;
973
1
    } else if (rel != 0) {
974
1
      if (config->emachine == EM_MIPS && 
rel == target->symbolicRel0
)
975
0
        rel = target->relativeRel;
976
1
      sec.getPartition().relaDyn->addReloc(rel, &sec, offset, &sym, addend,
977
1
                                           R_ADDEND, type);
978
1
979
1
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
980
1
      // While regular ABI uses dynamic relocations to fill up GOT entries
981
1
      // MIPS ABI requires dynamic linker to fills up GOT entries using
982
1
      // specially sorted dynamic symbol table. This affects even dynamic
983
1
      // relocations against symbols which do not require GOT entries
984
1
      // creation explicitly, i.e. do not have any GOT-relocations. So if
985
1
      // a preemptible symbol has a dynamic relocation we anyway have
986
1
      // to create a GOT entry for it.
987
1
      // If a non-preemptible symbol has a dynamic relocation against it,
988
1
      // dynamic linker takes it st_value, adds offset and writes down
989
1
      // result of the dynamic relocation. In case of preemptible symbol
990
1
      // dynamic linker performs symbol resolution, writes the symbol value
991
1
      // to the GOT entry and reads the GOT entry when it needs to perform
992
1
      // a dynamic relocation.
993
1
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
994
1
      if (config->emachine == EM_MIPS)
995
0
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
996
1
      return;
997
1
    }
998
2
  }
999
2
1000
2
  if (!canWrite && (config->isPic && 
!isRelExpr(expr)1
)) {
1001
0
    error(
1002
0
        "can't create dynamic relocation " + toString(type) + " against " +
1003
0
        (sym.getName().empty() ? "local symbol" : "symbol: " + toString(sym)) +
1004
0
        " in readonly segment; recompile object files with -fPIC "
1005
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
1006
0
        getLocation(sec, sym, offset));
1007
0
    return;
1008
0
  }
1009
2
1010
2
  // Copy relocations (for STT_OBJECT) and canonical PLT (for STT_FUNC) are only
1011
2
  // possible in an executable.
1012
2
  //
1013
2
  // Among R_ABS relocatoin types, symbolicRel has the same size as the word
1014
2
  // size. Others have fewer bits and may cause runtime overflow in -pie/-shared
1015
2
  // mode. Disallow them.
1016
2
  if (config->shared ||
1017
2
      
(1
config->pie1
&&
expr == R_ABS0
&&
type != target->symbolicRel0
)) {
1018
1
    errorOrWarn(
1019
1
        "relocation " + toString(type) + " cannot be used against " +
1020
1
        (sym.getName().empty() ? 
"local symbol"0
: "symbol " + toString(sym)) +
1021
1
        "; recompile with -fPIC" + getLocation(sec, sym, offset));
1022
1
    return;
1023
1
  }
1024
1
1025
1
  // If the symbol is undefined we already reported any relevant errors.
1026
1
  if (sym.isUndefined())
1027
0
    return;
1028
1
1029
1
  if (!canDefineSymbolInExecutable(sym)) {
1030
0
    error("cannot preempt symbol: " + toString(sym) +
1031
0
          getLocation(sec, sym, offset));
1032
0
    return;
1033
0
  }
1034
1
1035
1
  if (sym.isObject()) {
1036
1
    // Produce a copy relocation.
1037
1
    if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1038
1
      if (!config->zCopyreloc)
1039
0
        error("unresolvable relocation " + toString(type) +
1040
0
              " against symbol '" + toString(*ss) +
1041
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1042
0
              getLocation(sec, sym, offset));
1043
1
      addCopyRelSymbol<ELFT>(*ss);
1044
1
    }
1045
1
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1046
1
    return;
1047
1
  }
1048
0
1049
0
  if (sym.isFunc()) {
1050
0
    // This handles a non PIC program call to function in a shared library. In
1051
0
    // an ideal world, we could just report an error saying the relocation can
1052
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
1053
0
    // R_X86_64_PC32 pointing to libc.so.
1054
0
    //
1055
0
    // The general idea on how to handle such cases is to create a PLT entry and
1056
0
    // use that as the function value.
1057
0
    //
1058
0
    // For the static linking part, we just return a plt expr and everything
1059
0
    // else will use the PLT entry as the address.
1060
0
    //
1061
0
    // The remaining problem is making sure pointer equality still works. We
1062
0
    // need the help of the dynamic linker for that. We let it know that we have
1063
0
    // a direct reference to a so symbol by creating an undefined symbol with a
1064
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1065
0
    // the value of the symbol we created. This is true even for got entries, so
1066
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
1067
0
    // that points to the real function is a dedicated got entry used by the
1068
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1069
0
    // R_386_JMP_SLOT, etc).
1070
0
1071
0
    // For position independent executable on i386, the plt entry requires ebx
1072
0
    // to be set. This causes two problems:
1073
0
    // * If some code has a direct reference to a function, it was probably
1074
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
1075
0
    // * If a library definition gets preempted to the executable, it will have
1076
0
    //   the wrong ebx value.
1077
0
    if (config->pie && config->emachine == EM_386)
1078
0
      errorOrWarn("symbol '" + toString(sym) +
1079
0
                  "' cannot be preempted; recompile with -fPIE" +
1080
0
                  getLocation(sec, sym, offset));
1081
0
    if (!sym.isInPlt())
1082
0
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1083
0
    if (!sym.isDefined())
1084
0
      replaceWithDefined(
1085
0
          sym, in.plt,
1086
0
          target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0);
1087
0
    sym.needsPltAddr = true;
1088
0
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1089
0
    return;
1090
0
  }
1091
0
1092
0
  errorOrWarn("symbol '" + toString(sym) + "' has no type" +
1093
0
              getLocation(sec, sym, offset));
1094
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
953
818
                            int64_t addend) {
954
818
  // If the relocation is known to be a link-time constant, we know no dynamic
955
818
  // relocation will be created, pass the control to relocateAlloc() or
956
818
  // relocateNonAlloc() to resolve it.
957
818
  //
958
818
  // The behavior of an undefined weak reference is implementation defined. If
959
818
  // the relocation is to a weak undef, and we are producing an executable, let
960
818
  // relocate{,Non}Alloc() resolve it.
961
818
  if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
962
818
      
(176
!config->shared176
&&
sym.isUndefWeak()148
)) {
963
645
    sec.relocations.push_back({expr, type, offset, addend, &sym});
964
645
    return;
965
645
  }
966
173
967
173
  bool canWrite = (sec.flags & SHF_WRITE) || 
!config->zText16
;
968
173
  if (canWrite) {
969
157
    RelType rel = target->getDynRel(type);
970
157
    if (expr == R_GOT || 
(147
rel == target->symbolicRel147
&&
!sym.isPreemptible138
)) {
971
125
      addRelativeReloc(&sec, offset, &sym, addend, expr, type);
972
125
      return;
973
125
    } else 
if (32
rel != 032
) {
974
32
      if (config->emachine == EM_MIPS && 
rel == target->symbolicRel1
)
975
1
        rel = target->relativeRel;
976
32
      sec.getPartition().relaDyn->addReloc(rel, &sec, offset, &sym, addend,
977
32
                                           R_ADDEND, type);
978
32
979
32
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
980
32
      // While regular ABI uses dynamic relocations to fill up GOT entries
981
32
      // MIPS ABI requires dynamic linker to fills up GOT entries using
982
32
      // specially sorted dynamic symbol table. This affects even dynamic
983
32
      // relocations against symbols which do not require GOT entries
984
32
      // creation explicitly, i.e. do not have any GOT-relocations. So if
985
32
      // a preemptible symbol has a dynamic relocation we anyway have
986
32
      // to create a GOT entry for it.
987
32
      // If a non-preemptible symbol has a dynamic relocation against it,
988
32
      // dynamic linker takes it st_value, adds offset and writes down
989
32
      // result of the dynamic relocation. In case of preemptible symbol
990
32
      // dynamic linker performs symbol resolution, writes the symbol value
991
32
      // to the GOT entry and reads the GOT entry when it needs to perform
992
32
      // a dynamic relocation.
993
32
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
994
32
      if (config->emachine == EM_MIPS)
995
1
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
996
32
      return;
997
32
    }
998
16
  }
999
16
1000
16
  if (!canWrite && (config->isPic && 
!isRelExpr(expr)6
)) {
1001
5
    error(
1002
5
        "can't create dynamic relocation " + toString(type) + " against " +
1003
5
        (sym.getName().empty() ? 
"local symbol"0
: "symbol: " + toString(sym)) +
1004
5
        " in readonly segment; recompile object files with -fPIC "
1005
5
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
1006
5
        getLocation(sec, sym, offset));
1007
5
    return;
1008
5
  }
1009
11
1010
11
  // Copy relocations (for STT_OBJECT) and canonical PLT (for STT_FUNC) are only
1011
11
  // possible in an executable.
1012
11
  //
1013
11
  // Among R_ABS relocatoin types, symbolicRel has the same size as the word
1014
11
  // size. Others have fewer bits and may cause runtime overflow in -pie/-shared
1015
11
  // mode. Disallow them.
1016
11
  if (config->shared ||
1017
11
      (config->pie && 
expr == R_ABS1
&&
type != target->symbolicRel0
)) {
1018
0
    errorOrWarn(
1019
0
        "relocation " + toString(type) + " cannot be used against " +
1020
0
        (sym.getName().empty() ? "local symbol" : "symbol " + toString(sym)) +
1021
0
        "; recompile with -fPIC" + getLocation(sec, sym, offset));
1022
0
    return;
1023
0
  }
1024
11
1025
11
  // If the symbol is undefined we already reported any relevant errors.
1026
11
  if (sym.isUndefined())
1027
0
    return;
1028
11
1029
11
  if (!canDefineSymbolInExecutable(sym)) {
1030
0
    error("cannot preempt symbol: " + toString(sym) +
1031
0
          getLocation(sec, sym, offset));
1032
0
    return;
1033
0
  }
1034
11
1035
11
  if (sym.isObject()) {
1036
7
    // Produce a copy relocation.
1037
7
    if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1038
6
      if (!config->zCopyreloc)
1039
0
        error("unresolvable relocation " + toString(type) +
1040
0
              " against symbol '" + toString(*ss) +
1041
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1042
0
              getLocation(sec, sym, offset));
1043
6
      addCopyRelSymbol<ELFT>(*ss);
1044
6
    }
1045
7
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1046
7
    return;
1047
7
  }
1048
4
1049
4
  if (sym.isFunc()) {
1050
4
    // This handles a non PIC program call to function in a shared library. In
1051
4
    // an ideal world, we could just report an error saying the relocation can
1052
4
    // overflow at runtime. In the real world with glibc, crt1.o has a
1053
4
    // R_X86_64_PC32 pointing to libc.so.
1054
4
    //
1055
4
    // The general idea on how to handle such cases is to create a PLT entry and
1056
4
    // use that as the function value.
1057
4
    //
1058
4
    // For the static linking part, we just return a plt expr and everything
1059
4
    // else will use the PLT entry as the address.
1060
4
    //
1061
4
    // The remaining problem is making sure pointer equality still works. We
1062
4
    // need the help of the dynamic linker for that. We let it know that we have
1063
4
    // a direct reference to a so symbol by creating an undefined symbol with a
1064
4
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1065
4
    // the value of the symbol we created. This is true even for got entries, so
1066
4
    // pointer equality is maintained. To avoid an infinite loop, the only entry
1067
4
    // that points to the real function is a dedicated got entry used by the
1068
4
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1069
4
    // R_386_JMP_SLOT, etc).
1070
4
1071
4
    // For position independent executable on i386, the plt entry requires ebx
1072
4
    // to be set. This causes two problems:
1073
4
    // * If some code has a direct reference to a function, it was probably
1074
4
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
1075
4
    // * If a library definition gets preempted to the executable, it will have
1076
4
    //   the wrong ebx value.
1077
4
    if (config->pie && 
config->emachine == EM_3861
)
1078
1
      errorOrWarn("symbol '" + toString(sym) +
1079
1
                  "' cannot be preempted; recompile with -fPIE" +
1080
1
                  getLocation(sec, sym, offset));
1081
4
    if (!sym.isInPlt())
1082
4
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1083
4
    if (!sym.isDefined())
1084
4
      replaceWithDefined(
1085
4
          sym, in.plt,
1086
4
          target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0);
1087
4
    sym.needsPltAddr = true;
1088
4
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1089
4
    return;
1090
4
  }
1091
0
1092
0
  errorOrWarn("symbol '" + toString(sym) + "' has no type" +
1093
0
              getLocation(sec, sym, offset));
1094
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
953
87
                            int64_t addend) {
954
87
  // If the relocation is known to be a link-time constant, we know no dynamic
955
87
  // relocation will be created, pass the control to relocateAlloc() or
956
87
  // relocateNonAlloc() to resolve it.
957
87
  //
958
87
  // The behavior of an undefined weak reference is implementation defined. If
959
87
  // the relocation is to a weak undef, and we are producing an executable, let
960
87
  // relocate{,Non}Alloc() resolve it.
961
87
  if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
962
87
      
(9
!config->shared9
&&
sym.isUndefWeak()3
)) {
963
78
    sec.relocations.push_back({expr, type, offset, addend, &sym});
964
78
    return;
965
78
  }
966
9
967
9
  bool canWrite = (sec.flags & SHF_WRITE) || 
!config->zText0
;
968
9
  if (canWrite) {
969
9
    RelType rel = target->getDynRel(type);
970
9
    if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) {
971
1
      addRelativeReloc(&sec, offset, &sym, addend, expr, type);
972
1
      return;
973
8
    } else if (rel != 0) {
974
8
      if (config->emachine == EM_MIPS && 
rel == target->symbolicRel0
)
975
0
        rel = target->relativeRel;
976
8
      sec.getPartition().relaDyn->addReloc(rel, &sec, offset, &sym, addend,
977
8
                                           R_ADDEND, type);
978
8
979
8
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
980
8
      // While regular ABI uses dynamic relocations to fill up GOT entries
981
8
      // MIPS ABI requires dynamic linker to fills up GOT entries using
982
8
      // specially sorted dynamic symbol table. This affects even dynamic
983
8
      // relocations against symbols which do not require GOT entries
984
8
      // creation explicitly, i.e. do not have any GOT-relocations. So if
985
8
      // a preemptible symbol has a dynamic relocation we anyway have
986
8
      // to create a GOT entry for it.
987
8
      // If a non-preemptible symbol has a dynamic relocation against it,
988
8
      // dynamic linker takes it st_value, adds offset and writes down
989
8
      // result of the dynamic relocation. In case of preemptible symbol
990
8
      // dynamic linker performs symbol resolution, writes the symbol value
991
8
      // to the GOT entry and reads the GOT entry when it needs to perform
992
8
      // a dynamic relocation.
993
8
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
994
8
      if (config->emachine == EM_MIPS)
995
0
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
996
8
      return;
997
8
    }
998
0
  }
999
0
1000
0
  if (!canWrite && (config->isPic && !isRelExpr(expr))) {
1001
0
    error(
1002
0
        "can't create dynamic relocation " + toString(type) + " against " +
1003
0
        (sym.getName().empty() ? "local symbol" : "symbol: " + toString(sym)) +
1004
0
        " in readonly segment; recompile object files with -fPIC "
1005
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
1006
0
        getLocation(sec, sym, offset));
1007
0
    return;
1008
0
  }
1009
0
1010
0
  // Copy relocations (for STT_OBJECT) and canonical PLT (for STT_FUNC) are only
1011
0
  // possible in an executable.
1012
0
  //
1013
0
  // Among R_ABS relocatoin types, symbolicRel has the same size as the word
1014
0
  // size. Others have fewer bits and may cause runtime overflow in -pie/-shared
1015
0
  // mode. Disallow them.
1016
0
  if (config->shared ||
1017
0
      (config->pie && expr == R_ABS && type != target->symbolicRel)) {
1018
0
    errorOrWarn(
1019
0
        "relocation " + toString(type) + " cannot be used against " +
1020
0
        (sym.getName().empty() ? "local symbol" : "symbol " + toString(sym)) +
1021
0
        "; recompile with -fPIC" + getLocation(sec, sym, offset));
1022
0
    return;
1023
0
  }
1024
0
1025
0
  // If the symbol is undefined we already reported any relevant errors.
1026
0
  if (sym.isUndefined())
1027
0
    return;
1028
0
1029
0
  if (!canDefineSymbolInExecutable(sym)) {
1030
0
    error("cannot preempt symbol: " + toString(sym) +
1031
0
          getLocation(sec, sym, offset));
1032
0
    return;
1033
0
  }
1034
0
1035
0
  if (sym.isObject()) {
1036
0
    // Produce a copy relocation.
1037
0
    if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1038
0
      if (!config->zCopyreloc)
1039
0
        error("unresolvable relocation " + toString(type) +
1040
0
              " against symbol '" + toString(*ss) +
1041
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1042
0
              getLocation(sec, sym, offset));
1043
0
      addCopyRelSymbol<ELFT>(*ss);
1044
0
    }
1045
0
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1046
0
    return;
1047
0
  }
1048
0
1049
0
  if (sym.isFunc()) {
1050
0
    // This handles a non PIC program call to function in a shared library. In
1051
0
    // an ideal world, we could just report an error saying the relocation can
1052
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
1053
0
    // R_X86_64_PC32 pointing to libc.so.
1054
0
    //
1055
0
    // The general idea on how to handle such cases is to create a PLT entry and
1056
0
    // use that as the function value.
1057
0
    //
1058
0
    // For the static linking part, we just return a plt expr and everything
1059
0
    // else will use the PLT entry as the address.
1060
0
    //
1061
0
    // The remaining problem is making sure pointer equality still works. We
1062
0
    // need the help of the dynamic linker for that. We let it know that we have
1063
0
    // a direct reference to a so symbol by creating an undefined symbol with a
1064
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1065
0
    // the value of the symbol we created. This is true even for got entries, so
1066
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
1067
0
    // that points to the real function is a dedicated got entry used by the
1068
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1069
0
    // R_386_JMP_SLOT, etc).
1070
0
1071
0
    // For position independent executable on i386, the plt entry requires ebx
1072
0
    // to be set. This causes two problems:
1073
0
    // * If some code has a direct reference to a function, it was probably
1074
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
1075
0
    // * If a library definition gets preempted to the executable, it will have
1076
0
    //   the wrong ebx value.
1077
0
    if (config->pie && config->emachine == EM_386)
1078
0
      errorOrWarn("symbol '" + toString(sym) +
1079
0
                  "' cannot be preempted; recompile with -fPIE" +
1080
0
                  getLocation(sec, sym, offset));
1081
0
    if (!sym.isInPlt())
1082
0
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1083
0
    if (!sym.isDefined())
1084
0
      replaceWithDefined(
1085
0
          sym, in.plt,
1086
0
          target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0);
1087
0
    sym.needsPltAddr = true;
1088
0
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1089
0
    return;
1090
0
  }
1091
0
1092
0
  errorOrWarn("symbol '" + toString(sym) + "' has no type" +
1093
0
              getLocation(sec, sym, offset));
1094
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
953
260
                            int64_t addend) {
954
260
  // If the relocation is known to be a link-time constant, we know no dynamic
955
260
  // relocation will be created, pass the control to relocateAlloc() or
956
260
  // relocateNonAlloc() to resolve it.
957
260
  //
958
260
  // The behavior of an undefined weak reference is implementation defined. If
959
260
  // the relocation is to a weak undef, and we are producing an executable, let
960
260
  // relocate{,Non}Alloc() resolve it.
961
260
  if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
962
260
      
(17
!config->shared17
&&
sym.isUndefWeak()15
)) {
963
243
    sec.relocations.push_back({expr, type, offset, addend, &sym});
964
243
    return;
965
243
  }
966
17
967
17
  bool canWrite = (sec.flags & SHF_WRITE) || 
!config->zText15
;
968
17
  if (canWrite) {
969
2
    RelType rel = target->getDynRel(type);
970
2
    if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) {
971
1
      addRelativeReloc(&sec, offset, &sym, addend, expr, type);
972
1
      return;
973
1
    } else if (rel != 0) {
974
1
      if (config->emachine == EM_MIPS && rel == target->symbolicRel)
975
1
        rel = target->relativeRel;
976
1
      sec.getPartition().relaDyn->addReloc(rel, &sec, offset, &sym, addend,
977
1
                                           R_ADDEND, type);
978
1
979
1
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
980
1
      // While regular ABI uses dynamic relocations to fill up GOT entries
981
1
      // MIPS ABI requires dynamic linker to fills up GOT entries using
982
1
      // specially sorted dynamic symbol table. This affects even dynamic
983
1
      // relocations against symbols which do not require GOT entries
984
1
      // creation explicitly, i.e. do not have any GOT-relocations. So if
985
1
      // a preemptible symbol has a dynamic relocation we anyway have
986
1
      // to create a GOT entry for it.
987
1
      // If a non-preemptible symbol has a dynamic relocation against it,
988
1
      // dynamic linker takes it st_value, adds offset and writes down
989
1
      // result of the dynamic relocation. In case of preemptible symbol
990
1
      // dynamic linker performs symbol resolution, writes the symbol value
991
1
      // to the GOT entry and reads the GOT entry when it needs to perform
992
1
      // a dynamic relocation.
993
1
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
994
1
      if (config->emachine == EM_MIPS)
995
1
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
996
1
      return;
997
1
    }
998
15
  }
999
15
1000
15
  if (!canWrite && (config->isPic && 
!isRelExpr(expr)0
)) {
1001
0
    error(
1002
0
        "can't create dynamic relocation " + toString(type) + " against " +
1003
0
        (sym.getName().empty() ? "local symbol" : "symbol: " + toString(sym)) +
1004
0
        " in readonly segment; recompile object files with -fPIC "
1005
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
1006
0
        getLocation(sec, sym, offset));
1007
0
    return;
1008
0
  }
1009
15
1010
15
  // Copy relocations (for STT_OBJECT) and canonical PLT (for STT_FUNC) are only
1011
15
  // possible in an executable.
1012
15
  //
1013
15
  // Among R_ABS relocatoin types, symbolicRel has the same size as the word
1014
15
  // size. Others have fewer bits and may cause runtime overflow in -pie/-shared
1015
15
  // mode. Disallow them.
1016
15
  if (config->shared ||
1017
15
      (config->pie && 
expr == R_ABS0
&&
type != target->symbolicRel0
)) {
1018
0
    errorOrWarn(
1019
0
        "relocation " + toString(type) + " cannot be used against " +
1020
0
        (sym.getName().empty() ? "local symbol" : "symbol " + toString(sym)) +
1021
0
        "; recompile with -fPIC" + getLocation(sec, sym, offset));
1022
0
    return;
1023
0
  }
1024
15
1025
15
  // If the symbol is undefined we already reported any relevant errors.
1026
15
  if (sym.isUndefined())
1027
0
    return;
1028
15
1029
15
  if (!canDefineSymbolInExecutable(sym)) {
1030
0
    error("cannot preempt symbol: " + toString(sym) +
1031
0
          getLocation(sec, sym, offset));
1032
0
    return;
1033
0
  }
1034
15
1035
15
  if (sym.isObject()) {
1036
8
    // Produce a copy relocation.
1037
8
    if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1038
5
      if (!config->zCopyreloc)
1039
0
        error("unresolvable relocation " + toString(type) +
1040
0
              " against symbol '" + toString(*ss) +
1041
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1042
0
              getLocation(sec, sym, offset));
1043
5
      addCopyRelSymbol<ELFT>(*ss);
1044
5
    }
1045
8
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1046
8
    return;
1047
8
  }
1048
7
1049
7
  if (sym.isFunc()) {
1050
7
    // This handles a non PIC program call to function in a shared library. In
1051
7
    // an ideal world, we could just report an error saying the relocation can
1052
7
    // overflow at runtime. In the real world with glibc, crt1.o has a
1053
7
    // R_X86_64_PC32 pointing to libc.so.
1054
7
    //
1055
7
    // The general idea on how to handle such cases is to create a PLT entry and
1056
7
    // use that as the function value.
1057
7
    //
1058
7
    // For the static linking part, we just return a plt expr and everything
1059
7
    // else will use the PLT entry as the address.
1060
7
    //
1061
7
    // The remaining problem is making sure pointer equality still works. We
1062
7
    // need the help of the dynamic linker for that. We let it know that we have
1063
7
    // a direct reference to a so symbol by creating an undefined symbol with a
1064
7
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1065
7
    // the value of the symbol we created. This is true even for got entries, so
1066
7
    // pointer equality is maintained. To avoid an infinite loop, the only entry
1067
7
    // that points to the real function is a dedicated got entry used by the
1068
7
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1069
7
    // R_386_JMP_SLOT, etc).
1070
7
1071
7
    // For position independent executable on i386, the plt entry requires ebx
1072
7
    // to be set. This causes two problems:
1073
7
    // * If some code has a direct reference to a function, it was probably
1074
7
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
1075
7
    // * If a library definition gets preempted to the executable, it will have
1076
7
    //   the wrong ebx value.
1077
7
    if (config->pie && 
config->emachine == EM_3860
)
1078
0
      errorOrWarn("symbol '" + toString(sym) +
1079
0
                  "' cannot be preempted; recompile with -fPIE" +
1080
0
                  getLocation(sec, sym, offset));
1081
7
    if (!sym.isInPlt())
1082
4
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1083
7
    if (!sym.isDefined())
1084
4
      replaceWithDefined(
1085
4
          sym, in.plt,
1086
4
          target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0);
1087
7
    sym.needsPltAddr = true;
1088
7
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1089
7
    return;
1090
7
  }
1091
0
1092
0
  errorOrWarn("symbol '" + toString(sym) + "' has no type" +
1093
0
              getLocation(sec, sym, offset));
1094
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
953
2.50k
                            int64_t addend) {
954
2.50k
  // If the relocation is known to be a link-time constant, we know no dynamic
955
2.50k
  // relocation will be created, pass the control to relocateAlloc() or
956
2.50k
  // relocateNonAlloc() to resolve it.
957
2.50k
  //
958
2.50k
  // The behavior of an undefined weak reference is implementation defined. If
959
2.50k
  // the relocation is to a weak undef, and we are producing an executable, let
960
2.50k
  // relocate{,Non}Alloc() resolve it.
961
2.50k
  if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
962
2.50k
      
(405
!config->shared405
&&
sym.isUndefWeak()236
)) {
963
2.10k
    sec.relocations.push_back({expr, type, offset, addend, &sym});
964
2.10k
    return;
965
2.10k
  }
966
393
967
393
  bool canWrite = (sec.flags & SHF_WRITE) || 
!config->zText130
;
968
393
  if (canWrite) {
969
280
    RelType rel = target->getDynRel(type);
970
280
    if (expr == R_GOT || (rel == target->symbolicRel && 
!sym.isPreemptible235
)) {
971
124
      addRelativeReloc(&sec, offset, &sym, addend, expr, type);
972
124
      return;
973
156
    } else if (rel != 0) {
974
126
      if (config->emachine == EM_MIPS && 
rel == target->symbolicRel0
)
975
0
        rel = target->relativeRel;
976
126
      sec.getPartition().relaDyn->addReloc(rel, &sec, offset, &sym, addend,
977
126
                                           R_ADDEND, type);
978
126
979
126
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
980
126
      // While regular ABI uses dynamic relocations to fill up GOT entries
981
126
      // MIPS ABI requires dynamic linker to fills up GOT entries using
982
126
      // specially sorted dynamic symbol table. This affects even dynamic
983
126
      // relocations against symbols which do not require GOT entries
984
126
      // creation explicitly, i.e. do not have any GOT-relocations. So if
985
126
      // a preemptible symbol has a dynamic relocation we anyway have
986
126
      // to create a GOT entry for it.
987
126
      // If a non-preemptible symbol has a dynamic relocation against it,
988
126
      // dynamic linker takes it st_value, adds offset and writes down
989
126
      // result of the dynamic relocation. In case of preemptible symbol
990
126
      // dynamic linker performs symbol resolution, writes the symbol value
991
126
      // to the GOT entry and reads the GOT entry when it needs to perform
992
126
      // a dynamic relocation.
993
126
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
994
126
      if (config->emachine == EM_MIPS)
995
0
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
996
126
      return;
997
126
    }
998
143
  }
999
143
1000
143
  if (!canWrite && 
(113
config->isPic113
&&
!isRelExpr(expr)25
)) {
1001
15
    error(
1002
15
        "can't create dynamic relocation " + toString(type) + " against " +
1003
15
        (sym.getName().empty() ? 
"local symbol"4
:
"symbol: " + toString(sym)11
) +
1004
15
        " in readonly segment; recompile object files with -fPIC "
1005
15
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
1006
15
        getLocation(sec, sym, offset));
1007
15
    return;
1008
15
  }
1009
128
1010
128
  // Copy relocations (for STT_OBJECT) and canonical PLT (for STT_FUNC) are only
1011
128
  // possible in an executable.
1012
128
  //
1013
128
  // Among R_ABS relocatoin types, symbolicRel has the same size as the word
1014
128
  // size. Others have fewer bits and may cause runtime overflow in -pie/-shared
1015
128
  // mode. Disallow them.
1016
128
  if (config->shared ||
1017
128
      
(100
config->pie100
&&
expr == R_ABS7
&&
type != target->symbolicRel4
)) {
1018
32
    errorOrWarn(
1019
32
        "relocation " + toString(type) + " cannot be used against " +
1020
32
        (sym.getName().empty() ? 
"local symbol"6
:
"symbol " + toString(sym)26
) +
1021
32
        "; recompile with -fPIC" + getLocation(sec, sym, offset));
1022
32
    return;
1023
32
  }
1024
96
1025
96
  // If the symbol is undefined we already reported any relevant errors.
1026
96
  if (sym.isUndefined())
1027
3
    return;
1028
93
1029
93
  if (!canDefineSymbolInExecutable(sym)) {
1030
5
    error("cannot preempt symbol: " + toString(sym) +
1031
5
          getLocation(sec, sym, offset));
1032
5
    return;
1033
5
  }
1034
88
1035
88
  if (sym.isObject()) {
1036
50
    // Produce a copy relocation.
1037
50
    if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1038
41
      if (!config->zCopyreloc)
1039
3
        error("unresolvable relocation " + toString(type) +
1040
3
              " against symbol '" + toString(*ss) +
1041
3
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1042
3
              getLocation(sec, sym, offset));
1043
41
      addCopyRelSymbol<ELFT>(*ss);
1044
41
    }
1045
50
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1046
50
    return;
1047
50
  }
1048
38
1049
38
  if (sym.isFunc()) {
1050
35
    // This handles a non PIC program call to function in a shared library. In
1051
35
    // an ideal world, we could just report an error saying the relocation can
1052
35
    // overflow at runtime. In the real world with glibc, crt1.o has a
1053
35
    // R_X86_64_PC32 pointing to libc.so.
1054
35
    //
1055
35
    // The general idea on how to handle such cases is to create a PLT entry and
1056
35
    // use that as the function value.
1057
35
    //
1058
35
    // For the static linking part, we just return a plt expr and everything
1059
35
    // else will use the PLT entry as the address.
1060
35
    //
1061
35
    // The remaining problem is making sure pointer equality still works. We
1062
35
    // need the help of the dynamic linker for that. We let it know that we have
1063
35
    // a direct reference to a so symbol by creating an undefined symbol with a
1064
35
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1065
35
    // the value of the symbol we created. This is true even for got entries, so
1066
35
    // pointer equality is maintained. To avoid an infinite loop, the only entry
1067
35
    // that points to the real function is a dedicated got entry used by the
1068
35
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1069
35
    // R_386_JMP_SLOT, etc).
1070
35
1071
35
    // For position independent executable on i386, the plt entry requires ebx
1072
35
    // to be set. This causes two problems:
1073
35
    // * If some code has a direct reference to a function, it was probably
1074
35
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
1075
35
    // * If a library definition gets preempted to the executable, it will have
1076
35
    //   the wrong ebx value.
1077
35
    if (config->pie && 
config->emachine == EM_3861
)
1078
0
      errorOrWarn("symbol '" + toString(sym) +
1079
0
                  "' cannot be preempted; recompile with -fPIE" +
1080
0
                  getLocation(sec, sym, offset));
1081
35
    if (!sym.isInPlt())
1082
34
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1083
35
    if (!sym.isDefined())
1084
35
      replaceWithDefined(
1085
35
          sym, in.plt,
1086
35
          target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0);
1087
35
    sym.needsPltAddr = true;
1088
35
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1089
35
    return;
1090
35
  }
1091
3
1092
3
  errorOrWarn("symbol '" + toString(sym) + "' has no type" +
1093
3
              getLocation(sec, sym, offset));
1094
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
953
10.3k
                            int64_t addend) {
954
10.3k
  // If the relocation is known to be a link-time constant, we know no dynamic
955
10.3k
  // relocation will be created, pass the control to relocateAlloc() or
956
10.3k
  // relocateNonAlloc() to resolve it.
957
10.3k
  //
958
10.3k
  // The behavior of an undefined weak reference is implementation defined. If
959
10.3k
  // the relocation is to a weak undef, and we are producing an executable, let
960
10.3k
  // relocate{,Non}Alloc() resolve it.
961
10.3k
  if (isStaticLinkTimeConstant(expr, type, sym, sec, offset) ||
962
10.3k
      
(18
!config->shared18
&&
sym.isUndefWeak()4
)) {
963
10.3k
    sec.relocations.push_back({expr, type, offset, addend, &sym});
964
10.3k
    return;
965
10.3k
  }
966
18
967
18
  bool canWrite = (sec.flags & SHF_WRITE) || 
!config->zText2
;
968
18
  if (canWrite) {
969
18
    RelType rel = target->getDynRel(type);
970
18
    if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) {
971
7
      addRelativeReloc(&sec, offset, &sym, addend, expr, type);
972
7
      return;
973
11
    } else if (rel != 0) {
974
11
      if (config->emachine == EM_MIPS && 
rel == target->symbolicRel2
)
975
2
        rel = target->relativeRel;
976
11
      sec.getPartition().relaDyn->addReloc(rel, &sec, offset, &sym, addend,
977
11
                                           R_ADDEND, type);
978
11
979
11
      // MIPS ABI turns using of GOT and dynamic relocations inside out.
980
11
      // While regular ABI uses dynamic relocations to fill up GOT entries
981
11
      // MIPS ABI requires dynamic linker to fills up GOT entries using
982
11
      // specially sorted dynamic symbol table. This affects even dynamic
983
11
      // relocations against symbols which do not require GOT entries
984
11
      // creation explicitly, i.e. do not have any GOT-relocations. So if
985
11
      // a preemptible symbol has a dynamic relocation we anyway have
986
11
      // to create a GOT entry for it.
987
11
      // If a non-preemptible symbol has a dynamic relocation against it,
988
11
      // dynamic linker takes it st_value, adds offset and writes down
989
11
      // result of the dynamic relocation. In case of preemptible symbol
990
11
      // dynamic linker performs symbol resolution, writes the symbol value
991
11
      // to the GOT entry and reads the GOT entry when it needs to perform
992
11
      // a dynamic relocation.
993
11
      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
994
11
      if (config->emachine == EM_MIPS)
995
2
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
996
11
      return;
997
11
    }
998
0
  }
999
0
1000
0
  if (!canWrite && (config->isPic && !isRelExpr(expr))) {
1001
0
    error(
1002
0
        "can't create dynamic relocation " + toString(type) + " against " +
1003
0
        (sym.getName().empty() ? "local symbol" : "symbol: " + toString(sym)) +
1004
0
        " in readonly segment; recompile object files with -fPIC "
1005
0
        "or pass '-Wl,-z,notext' to allow text relocations in the output" +
1006
0
        getLocation(sec, sym, offset));
1007
0
    return;
1008
0
  }
1009
0
1010
0
  // Copy relocations (for STT_OBJECT) and canonical PLT (for STT_FUNC) are only
1011
0
  // possible in an executable.
1012
0
  //
1013
0
  // Among R_ABS relocatoin types, symbolicRel has the same size as the word
1014
0
  // size. Others have fewer bits and may cause runtime overflow in -pie/-shared
1015
0
  // mode. Disallow them.
1016
0
  if (config->shared ||
1017
0
      (config->pie && expr == R_ABS && type != target->symbolicRel)) {
1018
0
    errorOrWarn(
1019
0
        "relocation " + toString(type) + " cannot be used against " +
1020
0
        (sym.getName().empty() ? "local symbol" : "symbol " + toString(sym)) +
1021
0
        "; recompile with -fPIC" + getLocation(sec, sym, offset));
1022
0
    return;
1023
0
  }
1024
0
1025
0
  // If the symbol is undefined we already reported any relevant errors.
1026
0
  if (sym.isUndefined())
1027
0
    return;
1028
0
1029
0
  if (!canDefineSymbolInExecutable(sym)) {
1030
0
    error("cannot preempt symbol: " + toString(sym) +
1031
0
          getLocation(sec, sym, offset));
1032
0
    return;
1033
0
  }
1034
0
1035
0
  if (sym.isObject()) {
1036
0
    // Produce a copy relocation.
1037
0
    if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {
1038
0
      if (!config->zCopyreloc)
1039
0
        error("unresolvable relocation " + toString(type) +
1040
0
              " against symbol '" + toString(*ss) +
1041
0
              "'; recompile with -fPIC or remove '-z nocopyreloc'" +
1042
0
              getLocation(sec, sym, offset));
1043
0
      addCopyRelSymbol<ELFT>(*ss);
1044
0
    }
1045
0
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1046
0
    return;
1047
0
  }
1048
0
1049
0
  if (sym.isFunc()) {
1050
0
    // This handles a non PIC program call to function in a shared library. In
1051
0
    // an ideal world, we could just report an error saying the relocation can
1052
0
    // overflow at runtime. In the real world with glibc, crt1.o has a
1053
0
    // R_X86_64_PC32 pointing to libc.so.
1054
0
    //
1055
0
    // The general idea on how to handle such cases is to create a PLT entry and
1056
0
    // use that as the function value.
1057
0
    //
1058
0
    // For the static linking part, we just return a plt expr and everything
1059
0
    // else will use the PLT entry as the address.
1060
0
    //
1061
0
    // The remaining problem is making sure pointer equality still works. We
1062
0
    // need the help of the dynamic linker for that. We let it know that we have
1063
0
    // a direct reference to a so symbol by creating an undefined symbol with a
1064
0
    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
1065
0
    // the value of the symbol we created. This is true even for got entries, so
1066
0
    // pointer equality is maintained. To avoid an infinite loop, the only entry
1067
0
    // that points to the real function is a dedicated got entry used by the
1068
0
    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
1069
0
    // R_386_JMP_SLOT, etc).
1070
0
1071
0
    // For position independent executable on i386, the plt entry requires ebx
1072
0
    // to be set. This causes two problems:
1073
0
    // * If some code has a direct reference to a function, it was probably
1074
0
    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.
1075
0
    // * If a library definition gets preempted to the executable, it will have
1076
0
    //   the wrong ebx value.
1077
0
    if (config->pie && config->emachine == EM_386)
1078
0
      errorOrWarn("symbol '" + toString(sym) +
1079
0
                  "' cannot be preempted; recompile with -fPIE" +
1080
0
                  getLocation(sec, sym, offset));
1081
0
    if (!sym.isInPlt())
1082
0
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1083
0
    if (!sym.isDefined())
1084
0
      replaceWithDefined(
1085
0
          sym, in.plt,
1086
0
          target->pltHeaderSize + target->pltEntrySize * sym.pltIndex, 0);
1087
0
    sym.needsPltAddr = true;
1088
0
    sec.relocations.push_back({expr, type, offset, addend, &sym});
1089
0
    return;
1090
0
  }
1091
0
1092
0
  errorOrWarn("symbol '" + toString(sym) + "' has no type" +
1093
0
              getLocation(sec, sym, offset));
1094
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)
1095
1096
struct IRelativeReloc {
1097
  RelType type;
1098
  InputSectionBase *sec;
1099
  uint64_t offset;
1100
  Symbol *sym;
1101
};
1102
1103
static std::vector<IRelativeReloc> iRelativeRelocs;
1104
1105
template <class ELFT, class RelTy>
1106
static void scanReloc(InputSectionBase &sec, OffsetGetter &getOffset, RelTy *&i,
1107
14.8k
                      RelTy *end) {
1108
14.8k
  const RelTy &rel = *i;
1109
14.8k
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1110
14.8k
  Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIndex);
1111
14.8k
  RelType type;
1112
14.8k
1113
14.8k
  // Deal with MIPS oddity.
1114
14.8k
  if (config->mipsN32Abi) {
1115
5
    type = getMipsN32RelType(i, end);
1116
14.7k
  } else {
1117
14.7k
    type = rel.getType(config->isMips64EL);
1118
14.7k
    ++i;
1119
14.7k
  }
1120
14.8k
1121
14.8k
  // Get an offset in an output section this relocation is applied to.
1122
14.8k
  uint64_t offset = getOffset.get(rel.r_offset);
1123
14.8k
  if (offset == uint64_t(-1))
1124
15
    return;
1125
14.7k
1126
14.7k
  // Error if the target symbol is undefined. Symbol index 0 may be used by
1127
14.7k
  // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
1128
14.7k
  if (symIndex != 0 && 
maybeReportUndefined<ELFT>(sym, sec, rel.r_offset)14.7k
)
1129
103
    return;
1130
14.6k
1131
14.6k
  const uint8_t *relocatedAddr = sec.data().begin() + rel.r_offset;
1132
14.6k
  RelExpr expr = target->getRelExpr(type, sym, relocatedAddr);
1133
14.6k
1134
14.6k
  // Ignore "hint" relocations because they are only markers for relaxation.
1135
14.6k
  if (oneof<R_HINT, R_NONE>(expr))
1136
38
    return;
1137
14.6k
1138
14.6k
  // We can separate the small code model relocations into 2 categories:
1139
14.6k
  // 1) Those that access the compiler generated .toc sections.
1140
14.6k
  // 2) Those that access the linker allocated got entries.
1141
14.6k
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1142
14.6k
  // the got entries in any way, we don't have to track which objects have
1143
14.6k
  // got-based small code model relocs. The .toc sections get placed after the
1144
14.6k
  // end of the linker allocated .got section and we do sort those so sections
1145
14.6k
  // addressed with small code model relocations come first.
1146
14.6k
  if (config->emachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(type)993
)
1147
15
    sec.file->ppc64SmallCodeModelTocRelocs = true;
1148
14.6k
1149
14.6k
  if (sym.isGnuIFunc() && 
!config->zText93
&&
config->warnIfuncTextrel6
) {
1150
1
    warn("using ifunc symbols when text relocations are allowed may produce "
1151
1
         "a binary that will segfault, if the object file is linked with "
1152
1
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1153
1
         "you, consider recompiling the object files without -fPIC and "
1154
1
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1155
1
         "turn off this warning." +
1156
1
         getLocation(sec, sym, offset));
1157
1
  }
1158
14.6k
1159
14.6k
  // Read an addend.
1160
14.6k
  int64_t addend = computeAddend<ELFT>(rel, end, sec, expr, sym.isLocal());
1161
14.6k
1162
14.6k
  // Relax relocations.
1163
14.6k
  //
1164
14.6k
  // If we know that a PLT entry will be resolved within the same ELF module, we
1165
14.6k
  // can skip PLT access and directly jump to the destination function. For
1166
14.6k
  // example, if we are linking a main exectuable, all dynamic symbols that can
1167
14.6k
  // be resolved within the executable will actually be resolved that way at
1168
14.6k
  // runtime, because the main exectuable is always at the beginning of a search
1169
14.6k
  // list. We can leverage that fact.
1170
14.6k
  if (!sym.isPreemptible && 
(13.5k
!sym.isGnuIFunc()13.5k
||
config->zIfuncNoplt90
)) {
1171
13.4k
    if (expr == R_GOT_PC && 
!isAbsoluteValue(sym)109
) {
1172
61
      expr = target->adjustRelaxExpr(type, relocatedAddr, expr);
1173
13.4k
    } else {
1174
13.4k
      // Addend of R_PPC_PLTREL24 is used to choose call stub type. It should be
1175
13.4k
      // ignored if optimized to R_PC.
1176
13.4k
      if (config->emachine == EM_PPC && 
expr == R_PPC32_PLTREL81
)
1177
3
        addend = 0;
1178
13.4k
      expr = fromPlt(expr);
1179
13.4k
    }
1180
13.4k
  }
1181
14.6k
1182
14.6k
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1183
14.6k
  // uses their addresses, we need GOT or GOTPLT to be created.
1184
14.6k
  //
1185
14.6k
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1186
14.6k
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
1187
54
    in.gotPlt->hasGotPltOffRel = true;
1188
14.5k
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC64_TOCBASE, R_PPC64_RELAX_TOC>(
1189
14.5k
                 expr)) {
1190
204
    in.got->hasGotOffRel = true;
1191
204
  }
1192
14.6k
1193
14.6k
  // Process some TLS relocations, including relaxing TLS relocations.
1194
14.6k
  // Note that this function does not handle all TLS relocations.
1195
14.6k
  if (unsigned processed =
1196
399
          handleTlsRelocation<ELFT>(type, sym, sec, offset, addend, expr)) {
1197
399
    i += (processed - 1);
1198
399
    return;
1199
399
  }
1200
14.2k
1201
14.2k
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1202
14.2k
  // direct relocation on through.
1203
14.2k
  if (sym.isGnuIFunc() && 
config->zIfuncNoplt93
) {
1204
4
    sym.exportDynamic = true;
1205
4
    mainPart->relaDyn->addReloc(type, &sec, offset, &sym, addend, R_ADDEND, type);
1206
4
    return;
1207
4
  }
1208
14.2k
1209
14.2k
  // Non-preemptible ifuncs require special handling. First, handle the usual
1210
14.2k
  // case where the symbol isn't one of these.
1211
14.2k
  if (!sym.isGnuIFunc() || 
sym.isPreemptible89
) {
1212
14.1k
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1213
14.1k
    if (needsPlt(expr) && 
!sym.isInPlt()387
)
1214
317
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1215
14.1k
1216
14.1k
    // Create a GOT slot if a relocation needs GOT.
1217
14.1k
    if (needsGot(expr)) {
1218
10.4k
      if (config->emachine == EM_MIPS) {
1219
10.1k
        // MIPS ABI has special rules to process GOT entries and doesn't
1220
10.1k
        // require relocation entries for them. A special case is TLS
1221
10.1k
        // relocations. In that case dynamic loader applies dynamic
1222
10.1k
        // relocations to initialize TLS GOT entries.
1223
10.1k
        // See "Global Offset Table" in Chapter 5 in the following document
1224
10.1k
        // for detailed description:
1225
10.1k
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1226
10.1k
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
1227
10.1k
      } else 
if (310
!sym.isInGot()310
) {
1228
173
        addGotEntry(sym);
1229
173
      }
1230
10.4k
    }
1231
14.1k
  } else {
1232
86
    // Handle a reference to a non-preemptible ifunc. These are special in a
1233
86
    // few ways:
1234
86
    //
1235
86
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1236
86
    //   a fixed value. But assuming that all references to the ifunc are
1237
86
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1238
86
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1239
86
    //   usually at the end of .plt, which makes an indirect call using a
1240
86
    //   matching GOT entry in igotPlt, which is usually at the end of .got.plt.
1241
86
    //   The GOT entry is relocated using an IRELATIVE relocation in relaIplt,
1242
86
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1243
86
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1244
86
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1245
86
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1246
86
    //   .got.plt without requiring a separate GOT entry.
1247
86
    //
1248
86
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1249
86
    //   that are not passed -fPIC will assume that they do, and will emit
1250
86
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1251
86
    //   symbol. This means that if a direct relocation to the symbol is
1252
86
    //   seen, the linker must set a value for the symbol, and this value must
1253
86
    //   be consistent no matter what type of reference is made to the symbol.
1254
86
    //   This can be done by creating a PLT entry for the symbol in the way
1255
86
    //   described above and making it canonical, that is, making all references
1256
86
    //   point to the PLT entry instead of the resolver. In lld we also store
1257
86
    //   the address of the PLT entry in the dynamic symbol table, which means
1258
86
    //   that the symbol will also have the same value in other modules.
1259
86
    //   Because the value loaded from the GOT needs to be consistent with
1260
86
    //   the value computed using a direct relocation, a non-preemptible ifunc
1261
86
    //   may end up with two GOT entries, one in .got.plt that points to the
1262
86
    //   address returned by the resolver and is used only by the PLT entry,
1263
86
    //   and another in .got that points to the PLT entry and is used by
1264
86
    //   GOT-generating relocations.
1265
86
    //
1266
86
    // - The fact that these symbols do not have a fixed value makes them an
1267
86
    //   exception to the general rule that a statically linked executable does
1268
86
    //   not require any form of dynamic relocation. To handle these relocations
1269
86
    //   correctly, the IRELATIVE relocations are stored in an array which a
1270
86
    //   statically linked executable's startup code must enumerate using the
1271
86
    //   linker-defined symbols __rela?_iplt_{start,end}.
1272
86
    //
1273
86
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1274
86
    //   variable containing a pointer to the ifunc) needs to be relocated in
1275
86
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1276
86
    //   PLT entry canonical by translating such relocations into IRELATIVE
1277
86
    //   relocations in the relaIplt.
1278
86
    if (!sym.isInPlt()) {
1279
62
      // Create PLT and GOTPLT slots for the symbol.
1280
62
      sym.isInIplt = true;
1281
62
1282
62
      // Create a copy of the symbol to use as the target of the IRELATIVE
1283
62
      // relocation in the igotPlt. This is in case we make the PLT canonical
1284
62
      // later, which would overwrite the original symbol.
1285
62
      //
1286
62
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1287
62
      // that's really needed to create the IRELATIVE is the section and value,
1288
62
      // so ideally we should just need to copy those.
1289
62
      auto *directSym = make<Defined>(cast<Defined>(sym));
1290
62
      addPltEntry<ELFT>(in.iplt, in.igotPlt, in.relaIplt, target->iRelativeRel,
1291
62
                        *directSym);
1292
62
      sym.pltIndex = directSym->pltIndex;
1293
62
    }
1294
86
    if (expr == R_ABS && 
addend == 016
&&
(sec.flags & SHF_WRITE)11
) {
1295
8
      // We might be able to represent this as an IRELATIVE. But we don't know
1296
8
      // yet whether some later relocation will make the symbol point to a
1297
8
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1298
8
      // static (non-PIC) relocation. So we keep a record of the information
1299
8
      // required to process the relocation, and after scanRelocs() has been
1300
8
      // called on all relocations, the relocation is resolved by
1301
8
      // addIRelativeRelocs().
1302
8
      iRelativeRelocs.push_back({type, &sec, offset, &sym});
1303
8
      return;
1304
8
    }
1305
78
    if (needsGot(expr)) {
1306
19
      // Redirect GOT accesses to point to the Igot.
1307
19
      //
1308
19
      // This field is also used to keep track of whether we ever needed a GOT
1309
19
      // entry. If we did and we make the PLT canonical later, we'll need to
1310
19
      // create a GOT entry pointing to the PLT entry for Sym.
1311
19
      sym.gotInIgot = true;
1312
59
    } else if (!needsPlt(expr)) {
1313
19
      // Make the ifunc's PLT entry canonical by changing the value of its
1314
19
      // symbol to redirect all references to point to it.
1315
19
      unsigned entryOffset = sym.pltIndex * target->pltEntrySize;
1316
19
      if (config->zRetpolineplt)
1317
1
        entryOffset += target->pltHeaderSize;
1318
19
1319
19
      auto &d = cast<Defined>(sym);
1320
19
      d.section = in.iplt;
1321
19
      d.value = entryOffset;
1322
19
      d.size = 0;
1323
19
      // It's important to set the symbol type here so that dynamic loaders
1324
19
      // don't try to call the PLT as if it were an ifunc resolver.
1325
19
      d.type = STT_FUNC;
1326
19
1327
19
      if (sym.gotInIgot) {
1328
6
        // We previously encountered a GOT generating reference that we
1329
6
        // redirected to the Igot. Now that the PLT entry is canonical we must
1330
6
        // clear the redirection to the Igot and add a GOT entry. As we've
1331
6
        // changed the symbol type to STT_FUNC future GOT generating references
1332
6
        // will naturally use this GOT entry.
1333
6
        //
1334
6
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1335
6
        // aren't a thing on MIPS.
1336
6
        sym.gotInIgot = false;
1337
6
        addGotEntry(sym);
1338
6
      }
1339
19
    }
1340
78
  }
1341
14.2k
1342
14.2k
  processRelocAux<ELFT>(sec, expr, type, offset, sym, rel, addend);
1343
14.2k
}
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
1107
246
                      RelTy *end) {
1108
246
  const RelTy &rel = *i;
1109
246
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1110
246
  Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIndex);
1111
246
  RelType type;
1112
246
1113
246
  // Deal with MIPS oddity.
1114
246
  if (config->mipsN32Abi) {
1115
0
    type = getMipsN32RelType(i, end);
1116
246
  } else {
1117
246
    type = rel.getType(config->isMips64EL);
1118
246
    ++i;
1119
246
  }
1120
246
1121
246
  // Get an offset in an output section this relocation is applied to.
1122
246
  uint64_t offset = getOffset.get(rel.r_offset);
1123
246
  if (offset == uint64_t(-1))
1124
0
    return;
1125
246
1126
246
  // Error if the target symbol is undefined. Symbol index 0 may be used by
1127
246
  // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
1128
246
  if (symIndex != 0 && maybeReportUndefined<ELFT>(sym, sec, rel.r_offset))
1129
0
    return;
1130
246
1131
246
  const uint8_t *relocatedAddr = sec.data().begin() + rel.r_offset;
1132
246
  RelExpr expr = target->getRelExpr(type, sym, relocatedAddr);
1133
246
1134
246
  // Ignore "hint" relocations because they are only markers for relaxation.
1135
246
  if (oneof<R_HINT, R_NONE>(expr))
1136
4
    return;
1137
242
1138
242
  // We can separate the small code model relocations into 2 categories:
1139
242
  // 1) Those that access the compiler generated .toc sections.
1140
242
  // 2) Those that access the linker allocated got entries.
1141
242
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1142
242
  // the got entries in any way, we don't have to track which objects have
1143
242
  // got-based small code model relocs. The .toc sections get placed after the
1144
242
  // end of the linker allocated .got section and we do sort those so sections
1145
242
  // addressed with small code model relocations come first.
1146
242
  if (config->emachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(type)0
)
1147
0
    sec.file->ppc64SmallCodeModelTocRelocs = true;
1148
242
1149
242
  if (sym.isGnuIFunc() && 
!config->zText0
&&
config->warnIfuncTextrel0
) {
1150
0
    warn("using ifunc symbols when text relocations are allowed may produce "
1151
0
         "a binary that will segfault, if the object file is linked with "
1152
0
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1153
0
         "you, consider recompiling the object files without -fPIC and "
1154
0
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1155
0
         "turn off this warning." +
1156
0
         getLocation(sec, sym, offset));
1157
0
  }
1158
242
1159
242
  // Read an addend.
1160
242
  int64_t addend = computeAddend<ELFT>(rel, end, sec, expr, sym.isLocal());
1161
242
1162
242
  // Relax relocations.
1163
242
  //
1164
242
  // If we know that a PLT entry will be resolved within the same ELF module, we
1165
242
  // can skip PLT access and directly jump to the destination function. For
1166
242
  // example, if we are linking a main exectuable, all dynamic symbols that can
1167
242
  // be resolved within the executable will actually be resolved that way at
1168
242
  // runtime, because the main exectuable is always at the beginning of a search
1169
242
  // list. We can leverage that fact.
1170
242
  if (!sym.isPreemptible && 
(220
!sym.isGnuIFunc()220
||
config->zIfuncNoplt0
)) {
1171
220
    if (expr == R_GOT_PC && 
!isAbsoluteValue(sym)6
) {
1172
3
      expr = target->adjustRelaxExpr(type, relocatedAddr, expr);
1173
217
    } else {
1174
217
      // Addend of R_PPC_PLTREL24 is used to choose call stub type. It should be
1175
217
      // ignored if optimized to R_PC.
1176
217
      if (config->emachine == EM_PPC && 
expr == R_PPC32_PLTREL0
)
1177
0
        addend = 0;
1178
217
      expr = fromPlt(expr);
1179
217
    }
1180
220
  }
1181
242
1182
242
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1183
242
  // uses their addresses, we need GOT or GOTPLT to be created.
1184
242
  //
1185
242
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1186
242
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
1187
0
    in.gotPlt->hasGotPltOffRel = true;
1188
242
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC64_TOCBASE, R_PPC64_RELAX_TOC>(
1189
242
                 expr)) {
1190
0
    in.got->hasGotOffRel = true;
1191
0
  }
1192
242
1193
242
  // Process some TLS relocations, including relaxing TLS relocations.
1194
242
  // Note that this function does not handle all TLS relocations.
1195
242
  if (unsigned processed =
1196
10
          handleTlsRelocation<ELFT>(type, sym, sec, offset, addend, expr)) {
1197
10
    i += (processed - 1);
1198
10
    return;
1199
10
  }
1200
232
1201
232
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1202
232
  // direct relocation on through.
1203
232
  if (sym.isGnuIFunc() && 
config->zIfuncNoplt0
) {
1204
0
    sym.exportDynamic = true;
1205
0
    mainPart->relaDyn->addReloc(type, &sec, offset, &sym, addend, R_ADDEND, type);
1206
0
    return;
1207
0
  }
1208
232
1209
232
  // Non-preemptible ifuncs require special handling. First, handle the usual
1210
232
  // case where the symbol isn't one of these.
1211
232
  if (!sym.isGnuIFunc() || 
sym.isPreemptible0
) {
1212
232
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1213
232
    if (needsPlt(expr) && 
!sym.isInPlt()10
)
1214
6
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1215
232
1216
232
    // Create a GOT slot if a relocation needs GOT.
1217
232
    if (needsGot(expr)) {
1218
12
      if (config->emachine == EM_MIPS) {
1219
0
        // MIPS ABI has special rules to process GOT entries and doesn't
1220
0
        // require relocation entries for them. A special case is TLS
1221
0
        // relocations. In that case dynamic loader applies dynamic
1222
0
        // relocations to initialize TLS GOT entries.
1223
0
        // See "Global Offset Table" in Chapter 5 in the following document
1224
0
        // for detailed description:
1225
0
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1226
0
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
1227
12
      } else if (!sym.isInGot()) {
1228
9
        addGotEntry(sym);
1229
9
      }
1230
12
    }
1231
232
  } else {
1232
0
    // Handle a reference to a non-preemptible ifunc. These are special in a
1233
0
    // few ways:
1234
0
    //
1235
0
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1236
0
    //   a fixed value. But assuming that all references to the ifunc are
1237
0
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1238
0
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1239
0
    //   usually at the end of .plt, which makes an indirect call using a
1240
0
    //   matching GOT entry in igotPlt, which is usually at the end of .got.plt.
1241
0
    //   The GOT entry is relocated using an IRELATIVE relocation in relaIplt,
1242
0
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1243
0
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1244
0
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1245
0
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1246
0
    //   .got.plt without requiring a separate GOT entry.
1247
0
    //
1248
0
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1249
0
    //   that are not passed -fPIC will assume that they do, and will emit
1250
0
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1251
0
    //   symbol. This means that if a direct relocation to the symbol is
1252
0
    //   seen, the linker must set a value for the symbol, and this value must
1253
0
    //   be consistent no matter what type of reference is made to the symbol.
1254
0
    //   This can be done by creating a PLT entry for the symbol in the way
1255
0
    //   described above and making it canonical, that is, making all references
1256
0
    //   point to the PLT entry instead of the resolver. In lld we also store
1257
0
    //   the address of the PLT entry in the dynamic symbol table, which means
1258
0
    //   that the symbol will also have the same value in other modules.
1259
0
    //   Because the value loaded from the GOT needs to be consistent with
1260
0
    //   the value computed using a direct relocation, a non-preemptible ifunc
1261
0
    //   may end up with two GOT entries, one in .got.plt that points to the
1262
0
    //   address returned by the resolver and is used only by the PLT entry,
1263
0
    //   and another in .got that points to the PLT entry and is used by
1264
0
    //   GOT-generating relocations.
1265
0
    //
1266
0
    // - The fact that these symbols do not have a fixed value makes them an
1267
0
    //   exception to the general rule that a statically linked executable does
1268
0
    //   not require any form of dynamic relocation. To handle these relocations
1269
0
    //   correctly, the IRELATIVE relocations are stored in an array which a
1270
0
    //   statically linked executable's startup code must enumerate using the
1271
0
    //   linker-defined symbols __rela?_iplt_{start,end}.
1272
0
    //
1273
0
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1274
0
    //   variable containing a pointer to the ifunc) needs to be relocated in
1275
0
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1276
0
    //   PLT entry canonical by translating such relocations into IRELATIVE
1277
0
    //   relocations in the relaIplt.
1278
0
    if (!sym.isInPlt()) {
1279
0
      // Create PLT and GOTPLT slots for the symbol.
1280
0
      sym.isInIplt = true;
1281
0
1282
0
      // Create a copy of the symbol to use as the target of the IRELATIVE
1283
0
      // relocation in the igotPlt. This is in case we make the PLT canonical
1284
0
      // later, which would overwrite the original symbol.
1285
0
      //
1286
0
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1287
0
      // that's really needed to create the IRELATIVE is the section and value,
1288
0
      // so ideally we should just need to copy those.
1289
0
      auto *directSym = make<Defined>(cast<Defined>(sym));
1290
0
      addPltEntry<ELFT>(in.iplt, in.igotPlt, in.relaIplt, target->iRelativeRel,
1291
0
                        *directSym);
1292
0
      sym.pltIndex = directSym->pltIndex;
1293
0
    }
1294
0
    if (expr == R_ABS && addend == 0 && (sec.flags & SHF_WRITE)) {
1295
0
      // We might be able to represent this as an IRELATIVE. But we don't know
1296
0
      // yet whether some later relocation will make the symbol point to a
1297
0
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1298
0
      // static (non-PIC) relocation. So we keep a record of the information
1299
0
      // required to process the relocation, and after scanRelocs() has been
1300
0
      // called on all relocations, the relocation is resolved by
1301
0
      // addIRelativeRelocs().
1302
0
      iRelativeRelocs.push_back({type, &sec, offset, &sym});
1303
0
      return;
1304
0
    }
1305
0
    if (needsGot(expr)) {
1306
0
      // Redirect GOT accesses to point to the Igot.
1307
0
      //
1308
0
      // This field is also used to keep track of whether we ever needed a GOT
1309
0
      // entry. If we did and we make the PLT canonical later, we'll need to
1310
0
      // create a GOT entry pointing to the PLT entry for Sym.
1311
0
      sym.gotInIgot = true;
1312
0
    } else if (!needsPlt(expr)) {
1313
0
      // Make the ifunc's PLT entry canonical by changing the value of its
1314
0
      // symbol to redirect all references to point to it.
1315
0
      unsigned entryOffset = sym.pltIndex * target->pltEntrySize;
1316
0
      if (config->zRetpolineplt)
1317
0
        entryOffset += target->pltHeaderSize;
1318
0
1319
0
      auto &d = cast<Defined>(sym);
1320
0
      d.section = in.iplt;
1321
0
      d.value = entryOffset;
1322
0
      d.size = 0;
1323
0
      // It's important to set the symbol type here so that dynamic loaders
1324
0
      // don't try to call the PLT as if it were an ifunc resolver.
1325
0
      d.type = STT_FUNC;
1326
0
1327
0
      if (sym.gotInIgot) {
1328
0
        // We previously encountered a GOT generating reference that we
1329
0
        // redirected to the Igot. Now that the PLT entry is canonical we must
1330
0
        // clear the redirection to the Igot and add a GOT entry. As we've
1331
0
        // changed the symbol type to STT_FUNC future GOT generating references
1332
0
        // will naturally use this GOT entry.
1333
0
        //
1334
0
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1335
0
        // aren't a thing on MIPS.
1336
0
        sym.gotInIgot = false;
1337
0
        addGotEntry(sym);
1338
0
      }
1339
0
    }
1340
0
  }
1341
232
1342
232
  processRelocAux<ELFT>(sec, expr, type, offset, sym, rel, addend);
1343
232
}
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
1107
884
                      RelTy *end) {
1108
884
  const RelTy &rel = *i;
1109
884
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1110
884
  Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIndex);
1111
884
  RelType type;
1112
884
1113
884
  // Deal with MIPS oddity.
1114
884
  if (config->mipsN32Abi) {
1115
0
    type = getMipsN32RelType(i, end);
1116
884
  } else {
1117
884
    type = rel.getType(config->isMips64EL);
1118
884
    ++i;
1119
884
  }
1120
884
1121
884
  // Get an offset in an output section this relocation is applied to.
1122
884
  uint64_t offset = getOffset.get(rel.r_offset);
1123
884
  if (offset == uint64_t(-1))
1124
0
    return;
1125
884
1126
884
  // Error if the target symbol is undefined. Symbol index 0 may be used by
1127
884
  // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them.
1128
884
  if (symIndex != 0 && 
maybeReportUndefined<ELFT>(sym, sec, rel.r_offset)882
)
1129
1
    return;
1130
883
1131
883
  const uint8_t *relocatedAddr = sec.data().begin() + rel.r_offset;
1132
883
  RelExpr expr = target->getRelExpr(type, sym, relocatedAddr);
1133
883
1134
883
  // Ignore "hint" relocations because they are only markers for relaxation.
1135
883
  if (oneof<R_HINT, R_NONE>(expr))
1136
23
    return;
1137
860
1138
860
  // We can separate the small code model relocations into 2 categories:
1139
860
  // 1) Those that access the compiler generated .toc sections.
1140
860
  // 2) Those that access the linker allocated got entries.
1141
860
  // lld allocates got entries to symbols on demand. Since we don't try to sort
1142
860
  // the got entries in any way, we don't have to track which objects have
1143
860
  // got-based small code model relocs. The .toc sections get placed after the
1144
860
  // end of the linker allocated .got section and we do sort those so sections
1145
860
  // addressed with small code model relocations come first.
1146
860
  if (config->emachine == EM_PPC64 && 
isPPC64SmallCodeModelTocReloc(type)0
)
1147
0
    sec.file->ppc64SmallCodeModelTocRelocs = true;
1148
860
1149
860
  if (sym.isGnuIFunc() && 
!config->zText16
&&
config->warnIfuncTextrel2
) {
1150
0
    warn("using ifunc symbols when text relocations are allowed may produce "
1151
0
         "a binary that will segfault, if the object file is linked with "
1152
0
         "old version of glibc (glibc 2.28 and earlier). If this applies to "
1153
0
         "you, consider recompiling the object files without -fPIC and "
1154
0
         "without -Wl,-z,notext option. Use -no-warn-ifunc-textrel to "
1155
0
         "turn off this warning." +
1156
0
         getLocation(sec, sym, offset));
1157
0
  }
1158
860
1159
860
  // Read an addend.
1160
860
  int64_t addend = computeAddend<ELFT>(rel, end, sec, expr, sym.isLocal());
1161
860
1162
860
  // Relax relocations.
1163
860
  //
1164
860
  // If we know that a PLT entry will be resolved within the same ELF module, we
1165
860
  // can skip PLT access and directly jump to the destination function. For
1166
860
  // example, if we are linking a main exectuable, all dynamic symbols that can
1167
860
  // be resolved within the executable will actually be resolved that way at
1168
860
  // runtime, because the main exectuable is always at the beginning of a search
1169
860
  // list. We can leverage that fact.
1170
860
  if (!sym.isPreemptible && 
(675
!sym.isGnuIFunc()675
||
config->zIfuncNoplt16
)) {
1171
661
    if (expr == R_GOT_PC && 
!isAbsoluteValue(sym)6
) {
1172
3
      expr = target->adjustRelaxExpr(type, relocatedAddr, expr);
1173
658
    } else {
1174
658
      // Addend of R_PPC_PLTREL24 is used to choose call stub type. It should be
1175
658
      // ignored if optimized to R_PC.
1176
658
      if (config->emachine == EM_PPC && 
expr == R_PPC32_PLTREL0
)
1177
0
        addend = 0;
1178
658
      expr = fromPlt(expr);
1179
658
    }
1180
661
  }
1181
860
1182
860
  // If the relocation does not emit a GOT or GOTPLT entry but its computation
1183
860
  // uses their addresses, we need GOT or GOTPLT to be created.
1184
860
  //
1185
860
  // The 4 types that relative GOTPLT are all x86 and x86-64 specific.
1186
860
  if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
1187
47
    in.gotPlt->hasGotPltOffRel = true;
1188
813
  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC64_TOCBASE, R_PPC64_RELAX_TOC>(
1189
813
                 expr)) {
1190
3
    in.got->hasGotOffRel = true;
1191
3
  }
1192
860
1193
860
  // Process some TLS relocations, including relaxing TLS relocations.
1194
860
  // Note that this function does not handle all TLS relocations.
1195
860
  if (unsigned processed =
1196
40
          handleTlsRelocation<ELFT>(type, sym, sec, offset, addend, expr)) {
1197
40
    i += (processed - 1);
1198
40
    return;
1199
40
  }
1200
820
1201
820
  // We were asked not to generate PLT entries for ifuncs. Instead, pass the
1202
820
  // direct relocation on through.
1203
820
  if (sym.isGnuIFunc() && 
config->zIfuncNoplt16
) {
1204
2
    sym.exportDynamic = true;
1205
2
    mainPart->relaDyn->addReloc(type, &sec, offset, &sym, addend, R_ADDEND, type);
1206
2
    return;
1207
2
  }
1208
818
1209
818
  // Non-preemptible ifuncs require special handling. First, handle the usual
1210
818
  // case where the symbol isn't one of these.
1211
818
  if (!sym.isGnuIFunc() || 
sym.isPreemptible14
) {
1212
804
    // If a relocation needs PLT, we create PLT and GOTPLT slots for the symbol.
1213
804
    if (needsPlt(expr) && 
!sym.isInPlt()89
)
1214
69
      addPltEntry<ELFT>(in.plt, in.gotPlt, in.relaPlt, target->pltRel, sym);
1215
804
1216
804
    // Create a GOT slot if a relocation needs GOT.
1217
804
    if (needsGot(expr)) {
1218
65
      if (config->emachine == EM_MIPS) {
1219
6
        // MIPS ABI has special rules to process GOT entries and doesn't
1220
6
        // require relocation entries for them. A special case is TLS
1221
6
        // relocations. In that case dynamic loader applies dynamic
1222
6
        // relocations to initialize TLS GOT entries.
1223
6
        // See "Global Offset Table" in Chapter 5 in the following document
1224
6
        // for detailed description:
1225
6
        // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1226
6
        in.mipsGot->addEntry(*sec.file, sym, addend, expr);
1227
59
      } else if (!sym.isInGot()) {
1228
45
        addGotEntry(sym);
1229
45
      }
1230
65
    }
1231
804
  } else {
1232
14
    // Handle a reference to a non-preemptible ifunc. These are special in a
1233
14
    // few ways:
1234
14
    //
1235
14
    // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have
1236
14
    //   a fixed value. But assuming that all references to the ifunc are
1237
14
    //   GOT-generating or PLT-generating, the handling of an ifunc is
1238
14
    //   relatively straightforward. We create a PLT entry in Iplt, which is
1239
14
    //   usually at the end of .plt, which makes an indirect call using a
1240
14
    //   matching GOT entry in igotPlt, which is usually at the end of .got.plt.
1241
14
    //   The GOT entry is relocated using an IRELATIVE relocation in relaIplt,
1242
14
    //   which is usually at the end of .rela.plt. Unlike most relocations in
1243
14
    //   .rela.plt, which may be evaluated lazily without -z now, dynamic
1244
14
    //   loaders evaluate IRELATIVE relocs eagerly, which means that for
1245
14
    //   IRELATIVE relocs only, GOT-generating relocations can point directly to
1246
14
    //   .got.plt without requiring a separate GOT entry.
1247
14
    //
1248
14
    // - Despite the fact that an ifunc does not have a fixed value, compilers
1249
14
    //   that are not passed -fPIC will assume that they do, and will emit
1250
14
    //   direct (non-GOT-generating, non-PLT-generating) relocations to the
1251
14
    //   symbol. This means that if a direct relocation to the symbol is
1252
14
    //   seen, the linker must set a value for the symbol, and this value must
1253
14
    //   be consistent no matter what type of reference is made to the symbol.
1254
14
    //   This can be done by creating a PLT entry for the symbol in the way
1255
14
    //   described above and making it canonical, that is, making all references
1256
14
    //   point to the PLT entry instead of the resolver. In lld we also store
1257
14
    //   the address of the PLT entry in the dynamic symbol table, which means
1258
14
    //   that the symbol will also have the same value in other modules.
1259
14
    //   Because the value loaded from the GOT needs to be consistent with
1260
14
    //   the value computed using a direct relocation, a non-preemptible ifunc
1261
14
    //   may end up with two GOT entries, one in .got.plt that points to the
1262
14
    //   address returned by the resolver and is used only by the PLT entry,
1263
14
    //   and another in .got that points to the PLT entry and is used by
1264
14
    //   GOT-generating relocations.
1265
14
    //
1266
14
    // - The fact that these symbols do not have a fixed value makes them an
1267
14
    //   exception to the general rule that a statically linked executable does
1268
14
    //   not require any form of dynamic relocation. To handle these relocations
1269
14
    //   correctly, the IRELATIVE relocations are stored in an array which a
1270
14
    //   statically linked executable's startup code must enumerate using the
1271
14
    //   linker-defined symbols __rela?_iplt_{start,end}.
1272
14
    //
1273
14
    // - An absolute relocation to a non-preemptible ifunc (such as a global
1274
14
    //   variable containing a pointer to the ifunc) needs to be relocated in
1275
14
    //   the exact same way as a GOT entry, so we can avoid needing to make the
1276
14
    //   PLT entry canonical by translating such relocations into IRELATIVE
1277
14
    //   relocations in the relaIplt.
1278
14
    if (!sym.isInPlt()) {
1279
13
      // Create PLT and GOTPLT slots for the symbol.
1280
13
      sym.isInIplt = true;
1281
13
1282
13
      // Create a copy of the symbol to use as the target of the IRELATIVE
1283
13
      // relocation in the igotPlt. This is in case we make the PLT canonical
1284
13
      // later, which would overwrite the original symbol.
1285
13
      //
1286
13
      // FIXME: Creating a copy of the symbol here is a bit of a hack. All
1287
13
      // that's really needed to create the IRELATIVE is the section and value,
1288
13
      // so ideally we should just need to copy those.
1289
13
      auto *directSym = make<Defined>(cast<Defined>(sym));
1290
13
      addPltEntry<ELFT>(in.iplt, in.igotPlt, in.relaIplt, target->iRelativeRel,
1291
13
                        *directSym);
1292
13
      sym.pltIndex = directSym->pltIndex;
1293
13
    }
1294
14
    if (expr == R_ABS && 
addend == 00
&&
(sec.flags & SHF_WRITE)0
) {
1295
0
      // We might be able to represent this as an IRELATIVE. But we don't know
1296
0
      // yet whether some later relocation will make the symbol point to a
1297
0
      // canonical PLT, which would make this either a dynamic RELATIVE (PIC) or
1298
0
      // static (non-PIC) relocation. So we keep a record of the information
1299
0
      // required to process the relocation, and after scanRelocs() has been
1300
0
      // called on all relocations, the relocation is resolved by
1301
0
      // addIRelativeRelocs().
1302
0
      iRelativeRelocs.push_back({type, &sec, offset, &sym});
1303
0
      return;
1304
0
    }
1305
14
    if (needsGot(expr)) {
1306
1
      // Redirect GOT accesses to point to the Igot.
1307
1
      //
1308
1
      // This field is also used to keep track of whether we ever needed a GOT
1309
1
      // entry. If we did and we make the PLT canonical later, we'll need to
1310
1
      // create a GOT entry pointing to the PLT entry for Sym.
1311
1
      sym.gotInIgot = true;
1312
13
    } else if (!needsPlt(expr)) {
1313
4
      // Make the ifunc's PLT entry canonical by changing the value of its
1314
4
      // symbol to redirect all references to point to it.
1315
4
      unsigned entryOffset = sym.pltIndex * target->pltEntrySize;
1316
4
      if (config->zRetpolineplt)
1317
0
        entryOffset += target->pltHeaderSize;
1318
4
1319
4
      auto &d = cast<Defined>(sym);
1320
4
      d.section = in.iplt;
1321
4
      d.value = entryOffset;
1322
4
      d.size = 0;
1323
4
      // It's important to set the symbol type here so that dynamic loaders
1324
4
      // don't try to call the PLT as if it were an ifunc resolver.
1325
4
      d.type = STT_FUNC;
1326
4
1327
4
      if (sym.gotInIgot) {
1328
0
        // We previously encountered a GOT generating reference that we
1329
0
        // redirected to the Igot. Now that the PLT entry is canonical we must
1330
0
        // clear the redirection to the Igot and add a GOT entry. As we've
1331
0
        // changed the symbol type to STT_FUNC future GOT generating references
1332
0
        // will naturally use this GOT entry.
1333
0
        //
1334
0
        // We don't need to worry about creating a MIPS GOT here because ifuncs
1335
0
        // aren't a thing on MIPS.
1336
0
        sym.gotInIgot = false;
1337
0
        addGotEntry(sym);
1338
0
      }
1339
4
    }
1340
14
  }
1341
818
1342
818
  processRelocAux<ELFT>(sec, expr, type, offset, sym, rel, addend);
1343
818
}
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
1107
136
                      RelTy *end) {
1108
136
  const RelTy &rel = *i;
1109
136
  uint32_t symIndex = rel.getSymbol(config->isMips64EL);
1110
136
  Symbol &sym = sec.getFile<ELFT>()->getSymbol(symIndex);
1111
136
  RelType type;
1112
136
1113
136
  // Deal with MIPS oddity.
1114
136
  if (config->mipsN32Abi) {
1115
5
    type = getMipsN32RelType(i, end);
1116
131
  } else {
1117
131
    type = rel.getType(config->isMips64EL);
1118
131
    ++i;
1119
131
  }
1120
136