2015-08-14 22:12:54 +08:00
|
|
|
//===- Symbols.h ------------------------------------------------*- C++ -*-===//
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-14 03:51:57 +08:00
|
|
|
//
|
2018-04-12 03:52:53 +08:00
|
|
|
// This file defines various types of Symbols.
|
2015-10-14 03:51:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
#ifndef LLD_ELF_SYMBOLS_H
|
|
|
|
#define LLD_ELF_SYMBOLS_H
|
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
#include "InputFiles.h"
|
2015-09-22 08:01:39 +08:00
|
|
|
#include "InputSection.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2018-03-01 01:38:19 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2020-04-06 13:27:46 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2015-09-05 06:28:10 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
|
|
|
|
namespace lld {
|
2020-03-29 06:48:38 +08:00
|
|
|
// Returns a string representation for a symbol for diagnostics.
|
2019-10-07 16:31:18 +08:00
|
|
|
std::string toString(const elf::Symbol &);
|
|
|
|
|
|
|
|
// There are two different ways to convert an Archive::Symbol to a string:
|
|
|
|
// One for Microsoft name mangling and one for Itanium name mangling.
|
|
|
|
// Call the functions toCOFFString and toELFString, not just toString.
|
|
|
|
std::string toELFString(const llvm::object::Archive::Symbol &);
|
|
|
|
|
2018-10-13 02:29:18 +08:00
|
|
|
namespace elf {
|
2019-05-23 17:58:08 +08:00
|
|
|
class CommonSymbol;
|
|
|
|
class Defined;
|
2018-10-13 02:29:18 +08:00
|
|
|
class InputFile;
|
2019-05-23 17:58:08 +08:00
|
|
|
class LazyArchive;
|
|
|
|
class LazyObject;
|
|
|
|
class SharedSymbol;
|
|
|
|
class Symbol;
|
|
|
|
class Undefined;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2018-04-26 06:34:21 +08:00
|
|
|
// This is a StringRef-like container that doesn't run strlen().
|
|
|
|
//
|
|
|
|
// ELF string tables contain a lot of null-terminated strings. Most of them
|
|
|
|
// are not necessary for the linker because they are names of local symbols,
|
|
|
|
// and the linker doesn't use local symbol names for name resolution. So, we
|
|
|
|
// use this class to represents strings read from string tables.
|
|
|
|
struct StringRefZ {
|
|
|
|
StringRefZ(const char *s) : data(s), size(-1) {}
|
|
|
|
StringRefZ(StringRef s) : data(s.data()), size(s.size()) {}
|
|
|
|
|
|
|
|
const char *data;
|
|
|
|
const uint32_t size;
|
|
|
|
};
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// The base class for real symbol classes.
|
2017-11-04 05:21:47 +08:00
|
|
|
class Symbol {
|
2015-07-25 05:03:07 +08:00
|
|
|
public:
|
|
|
|
enum Kind {
|
2018-10-13 02:29:18 +08:00
|
|
|
PlaceholderKind,
|
2017-11-06 12:35:31 +08:00
|
|
|
DefinedKind,
|
2019-05-16 11:29:03 +08:00
|
|
|
CommonKind,
|
2017-11-06 12:13:24 +08:00
|
|
|
SharedKind,
|
2016-04-27 08:05:06 +08:00
|
|
|
UndefinedKind,
|
2016-04-08 03:24:51 +08:00
|
|
|
LazyArchiveKind,
|
|
|
|
LazyObjectKind,
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
2017-11-01 00:07:41 +08:00
|
|
|
Kind kind() const { return static_cast<Kind>(symbolKind); }
|
2016-07-18 01:50:09 +08:00
|
|
|
|
2018-04-26 05:44:37 +08:00
|
|
|
// The file from which this symbol was created.
|
|
|
|
InputFile *file;
|
|
|
|
|
|
|
|
protected:
|
2018-04-26 06:34:21 +08:00
|
|
|
const char *nameData;
|
2018-04-26 05:44:37 +08:00
|
|
|
mutable uint32_t nameSize;
|
|
|
|
|
|
|
|
public:
|
|
|
|
uint32_t dynsymIndex = 0;
|
|
|
|
uint32_t gotIndex = -1;
|
|
|
|
uint32_t pltIndex = -1;
|
2018-11-15 01:56:43 +08:00
|
|
|
|
2018-04-26 05:44:37 +08:00
|
|
|
uint32_t globalDynIndex = -1;
|
|
|
|
|
2018-04-27 01:58:58 +08:00
|
|
|
// This field is a index to the symbol's version definition.
|
|
|
|
uint32_t verdefIndex = -1;
|
|
|
|
|
2018-04-26 05:44:37 +08:00
|
|
|
// Version definition index.
|
|
|
|
uint16_t versionId;
|
|
|
|
|
2019-05-20 11:36:33 +08:00
|
|
|
// Symbol binding. This is not overwritten by replace() to track
|
2017-11-01 00:07:41 +08:00
|
|
|
// changes during resolution. In particular:
|
|
|
|
// - An undefined weak is still weak when it resolves to a shared library.
|
|
|
|
// - An undefined weak will not fetch archive members, but we have to
|
|
|
|
// remember it is weak.
|
|
|
|
uint8_t binding;
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
|
2018-04-26 05:44:37 +08:00
|
|
|
// The following fields have the same meaning as the ELF symbol attributes.
|
|
|
|
uint8_t type; // symbol type
|
|
|
|
uint8_t stOther; // st_other field value
|
|
|
|
|
2018-10-13 02:29:18 +08:00
|
|
|
uint8_t symbolKind;
|
2017-11-01 00:07:41 +08:00
|
|
|
|
|
|
|
// Symbol visibility. This is the computed minimum visibility of all
|
|
|
|
// observed non-DSO symbols.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t visibility : 2;
|
2017-11-01 00:07:41 +08:00
|
|
|
|
|
|
|
// True if the symbol was used for linking and thus need to be added to the
|
|
|
|
// output file's symbol table. This is true for all symbols except for
|
2019-03-09 05:10:48 +08:00
|
|
|
// unreferenced DSO symbols, lazy (archive) symbols, and bitcode symbols that
|
|
|
|
// are unreferenced except by other bitcode objects.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t isUsedInRegularObj : 1;
|
2017-11-01 00:07:41 +08:00
|
|
|
|
2019-08-13 17:12:52 +08:00
|
|
|
// Used by a Defined symbol with protected or default visibility, to record
|
|
|
|
// whether it is required to be exported into .dynsym. This is set when any of
|
|
|
|
// the following conditions hold:
|
|
|
|
//
|
|
|
|
// - If there is an interposable symbol from a DSO.
|
|
|
|
// - If -shared or --export-dynamic is specified, any symbol in an object
|
|
|
|
// file/bitcode sets this property, unless suppressed by LTO
|
|
|
|
// canBeOmittedFromSymbolTable().
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t exportDynamic : 1;
|
2017-11-01 00:07:41 +08:00
|
|
|
|
2019-08-13 17:12:52 +08:00
|
|
|
// True if the symbol is in the --dynamic-list file. A Defined symbol with
|
|
|
|
// protected or default visibility with this property is required to be
|
|
|
|
// exported into .dynsym.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t inDynamicList : 1;
|
2019-08-13 17:12:52 +08:00
|
|
|
|
2017-11-01 00:07:41 +08:00
|
|
|
// False if LTO shouldn't inline whatever this symbol points to. If a symbol
|
|
|
|
// is overwritten after LTO, LTO shouldn't inline the symbol because it
|
|
|
|
// doesn't know the final contents of the symbol.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t canInline : 1;
|
2017-11-01 00:07:41 +08:00
|
|
|
|
[ELF] Make binding (weak or non-weak) logic consistent for Undefined and SharedSymbol
This is a case missed by D64136. If %t1.o has a weak reference on foo,
and %t2.so has a non-weak reference on foo:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # undefined symbol: foo; STB_GLOBAL
2. gold %t1.o %t2.so # ok; STB_WEAK
3. gold %t2.so %t1.o # undefined reference to 'foo'; STB_GLOBAL
4. ld.bfd %t1.o %t2.so # undefined reference to `foo'; STB_WEAK
5. ld.bfd %t2.so %t1.o # undefined reference to `foo'; STB_WEAK
```
It can be argued that in both cases, the binding of the undefined foo
should be set to STB_WEAK, because the binding should not be affected by
referenced from shared objects.
--allow-shlib-undefined doesn't suppress errors (3,4,5), but -shared or
--noinhibit-exec allows ld.bfd/gold to produce a binary:
```
3. gold -shared %t2.so %t1.o # ok; STB_GLOBAL
4. ld.bfd -shared %t2.so %t1.o # ok; STB_WEAK
5. ld.bfd -shared %t1.o %t1.o # ok; STB_WEAK
```
If %t2.so has DT_NEEDED entries, ld.bfd will load them (lld/gold don't
have the behavior). If one of the DSO defines foo and it is in the
link-time search path (e.g. DT_NEEDED entry is an absolute path, via
-rpath=, via -rpath-link=, etc),
`ld.bfd %t1.o %t2.so` and `ld.bfd %t1.o %t2.so` will not error.
In this patch, we make Undefined and SharedSymbol share the same binding
computing logic. Case 1 will be allowed:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # ok; STB_WEAK; changed by this patch
```
In the future, we can explore the option that turns both (0,1) into
errors if --no-allow-shlib-undefined (default when linking an
executable) is in action.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D65584
llvm-svn: 368038
2019-08-06 22:03:45 +08:00
|
|
|
// Used by Undefined and SharedSymbol to track if there has been at least one
|
|
|
|
// undefined reference to the symbol. The binding may change to STB_WEAK if
|
|
|
|
// the first undefined reference from a non-shared object is weak.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t referenced : 1;
|
[ELF] Make binding (weak or non-weak) logic consistent for Undefined and SharedSymbol
This is a case missed by D64136. If %t1.o has a weak reference on foo,
and %t2.so has a non-weak reference on foo:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # undefined symbol: foo; STB_GLOBAL
2. gold %t1.o %t2.so # ok; STB_WEAK
3. gold %t2.so %t1.o # undefined reference to 'foo'; STB_GLOBAL
4. ld.bfd %t1.o %t2.so # undefined reference to `foo'; STB_WEAK
5. ld.bfd %t2.so %t1.o # undefined reference to `foo'; STB_WEAK
```
It can be argued that in both cases, the binding of the undefined foo
should be set to STB_WEAK, because the binding should not be affected by
referenced from shared objects.
--allow-shlib-undefined doesn't suppress errors (3,4,5), but -shared or
--noinhibit-exec allows ld.bfd/gold to produce a binary:
```
3. gold -shared %t2.so %t1.o # ok; STB_GLOBAL
4. ld.bfd -shared %t2.so %t1.o # ok; STB_WEAK
5. ld.bfd -shared %t1.o %t1.o # ok; STB_WEAK
```
If %t2.so has DT_NEEDED entries, ld.bfd will load them (lld/gold don't
have the behavior). If one of the DSO defines foo and it is in the
link-time search path (e.g. DT_NEEDED entry is an absolute path, via
-rpath=, via -rpath-link=, etc),
`ld.bfd %t1.o %t2.so` and `ld.bfd %t1.o %t2.so` will not error.
In this patch, we make Undefined and SharedSymbol share the same binding
computing logic. Case 1 will be allowed:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # ok; STB_WEAK; changed by this patch
```
In the future, we can explore the option that turns both (0,1) into
errors if --no-allow-shlib-undefined (default when linking an
executable) is in action.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D65584
llvm-svn: 368038
2019-08-06 22:03:45 +08:00
|
|
|
|
2017-11-01 00:07:41 +08:00
|
|
|
// True if this symbol is specified by --trace-symbol option.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t traced : 1;
|
2017-11-01 00:07:41 +08:00
|
|
|
|
2019-08-13 14:19:39 +08:00
|
|
|
inline void replace(const Symbol &newSym);
|
2019-05-20 11:36:33 +08:00
|
|
|
|
2017-11-01 00:07:41 +08:00
|
|
|
bool includeInDynsym() const;
|
|
|
|
uint8_t computeBinding() const;
|
|
|
|
bool isWeak() const { return binding == llvm::ELF::STB_WEAK; }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-04-27 08:05:06 +08:00
|
|
|
bool isUndefined() const { return symbolKind == UndefinedKind; }
|
2019-05-16 11:29:03 +08:00
|
|
|
bool isCommon() const { return symbolKind == CommonKind; }
|
2017-11-06 12:35:31 +08:00
|
|
|
bool isDefined() const { return symbolKind == DefinedKind; }
|
2017-10-13 11:37:11 +08:00
|
|
|
bool isShared() const { return symbolKind == SharedKind; }
|
2019-05-16 11:29:03 +08:00
|
|
|
bool isPlaceholder() const { return symbolKind == PlaceholderKind; }
|
|
|
|
|
2017-11-17 09:37:50 +08:00
|
|
|
bool isLocal() const { return binding == llvm::ELF::STB_LOCAL; }
|
2017-10-13 11:37:11 +08:00
|
|
|
|
2016-04-08 03:24:51 +08:00
|
|
|
bool isLazy() const {
|
|
|
|
return symbolKind == LazyArchiveKind || symbolKind == LazyObjectKind;
|
|
|
|
}
|
2017-10-13 11:37:11 +08:00
|
|
|
|
2018-09-12 07:00:36 +08:00
|
|
|
// True if this is an undefined weak symbol. This only works once
|
|
|
|
// all input files have been added.
|
|
|
|
bool isUndefWeak() const {
|
|
|
|
// See comment on lazy symbols for details.
|
|
|
|
return isWeak() && (isUndefined() || isLazy());
|
|
|
|
}
|
2017-09-14 04:43:04 +08:00
|
|
|
|
2018-04-26 05:44:37 +08:00
|
|
|
StringRef getName() const {
|
|
|
|
if (nameSize == (uint32_t)-1)
|
2018-04-26 06:34:21 +08:00
|
|
|
nameSize = strlen(nameData);
|
|
|
|
return {nameData, nameSize};
|
2018-04-26 05:44:37 +08:00
|
|
|
}
|
2018-04-26 06:34:21 +08:00
|
|
|
|
Change how we handle -wrap.
We have an issue with -wrap that the option doesn't work well when
renamed symbols get PLT entries. I'll explain what is the issue and
how this patch solves it.
For one -wrap option, we have three symbols: foo, wrap_foo and real_foo.
Currently, we use memcpy to overwrite wrapped symbols so that they get
the same contents. This works in most cases but doesn't when the relocation
processor sets some flags in the symbol. memcpy'ed symbols are just
aliases, so they always have to have the same contents, but the
relocation processor breaks that assumption.
r336609 is an attempt to fix the issue by memcpy'ing again after
processing relocations, so that symbols that are out of sync get the
same contents again. That works in most cases as well, but it breaks
ASan build in a mysterious way.
We could probably fix the issue by choosing symbol attributes that need
to be copied after they are updated. But it feels too complicated to me.
So, in this patch, I fixed it once and for all. With this patch, we no
longer memcpy symbols. All references to renamed symbols point to new
symbols after wrapSymbols() is done.
Differential Revision: https://reviews.llvm.org/D50569
llvm-svn: 340387
2018-08-22 15:02:26 +08:00
|
|
|
void setName(StringRef s) {
|
|
|
|
nameData = s.data();
|
|
|
|
nameSize = s.size();
|
|
|
|
}
|
|
|
|
|
2016-11-23 13:48:40 +08:00
|
|
|
void parseSymbolVersion();
|
2015-09-02 07:12:52 +08:00
|
|
|
|
2015-09-18 22:40:19 +08:00
|
|
|
bool isInGot() const { return gotIndex != -1U; }
|
2015-09-21 23:11:29 +08:00
|
|
|
bool isInPlt() const { return pltIndex != -1U; }
|
|
|
|
|
2017-03-17 19:56:54 +08:00
|
|
|
uint64_t getVA(int64_t addend = 0) const;
|
2016-03-15 07:16:09 +08:00
|
|
|
|
2017-03-17 22:12:51 +08:00
|
|
|
uint64_t getGotOffset() const;
|
2017-05-12 07:28:49 +08:00
|
|
|
uint64_t getGotVA() const;
|
2017-03-16 20:58:11 +08:00
|
|
|
uint64_t getGotPltOffset() const;
|
|
|
|
uint64_t getGotPltVA() const;
|
2017-03-17 19:56:54 +08:00
|
|
|
uint64_t getPltVA() const;
|
2017-10-29 04:15:56 +08:00
|
|
|
uint64_t getSize() const;
|
2017-03-16 19:06:13 +08:00
|
|
|
OutputSection *getOutputSection() const;
|
2016-02-02 05:00:35 +08:00
|
|
|
|
2019-05-23 17:58:08 +08:00
|
|
|
// The following two functions are used for symbol resolution.
|
|
|
|
//
|
|
|
|
// You are expected to call mergeProperties for all symbols in input
|
|
|
|
// files so that attributes that are attached to names rather than
|
|
|
|
// indivisual symbol (such as visibility) are merged together.
|
|
|
|
//
|
|
|
|
// Every time you read a new symbol from an input, you are supposed
|
|
|
|
// to call resolve() with the new symbol. That function replaces
|
|
|
|
// "this" object as a result of name resolution if the new symbol is
|
|
|
|
// more appropriate to be included in the output.
|
|
|
|
//
|
|
|
|
// For example, if "this" is an undefined symbol and a new symbol is
|
|
|
|
// a defined symbol, "this" is replaced with the new symbol.
|
|
|
|
void mergeProperties(const Symbol &other);
|
|
|
|
void resolve(const Symbol &other);
|
|
|
|
|
|
|
|
// If this is a lazy symbol, fetch an input file and add the symbol
|
|
|
|
// in the file to the symbol table. Calling this function on
|
|
|
|
// non-lazy object causes a runtime error.
|
|
|
|
void fetch() const;
|
|
|
|
|
2019-05-17 09:55:20 +08:00
|
|
|
private:
|
|
|
|
static bool isExportDynamic(Kind k, uint8_t visibility) {
|
|
|
|
if (k == SharedKind)
|
|
|
|
return visibility == llvm::ELF::STV_DEFAULT;
|
|
|
|
return config->shared || config->exportDynamic;
|
|
|
|
}
|
|
|
|
|
2019-05-23 17:58:08 +08:00
|
|
|
void resolveUndefined(const Undefined &other);
|
|
|
|
void resolveCommon(const CommonSymbol &other);
|
|
|
|
void resolveDefined(const Defined &other);
|
|
|
|
template <class LazyT> void resolveLazy(const LazyT &other);
|
|
|
|
void resolveShared(const SharedSymbol &other);
|
|
|
|
|
|
|
|
int compare(const Symbol *other) const;
|
|
|
|
|
2019-05-20 11:36:33 +08:00
|
|
|
inline size_t getSymbolSize() const;
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
protected:
|
2017-11-30 06:47:35 +08:00
|
|
|
Symbol(Kind k, InputFile *file, StringRefZ name, uint8_t binding,
|
|
|
|
uint8_t stOther, uint8_t type)
|
2018-04-26 06:34:21 +08:00
|
|
|
: file(file), nameData(name.data), nameSize(name.size), binding(binding),
|
2019-05-16 10:14:00 +08:00
|
|
|
type(type), stOther(stOther), symbolKind(k), visibility(stOther & 3),
|
|
|
|
isUsedInRegularObj(!file || file->kind() == InputFile::ObjKind),
|
2019-08-13 17:12:52 +08:00
|
|
|
exportDynamic(isExportDynamic(k, visibility)), inDynamicList(false),
|
|
|
|
canInline(false), referenced(false), traced(false), needsPltAddr(false),
|
|
|
|
isInIplt(false), gotInIgot(false), isPreemptible(false),
|
|
|
|
used(!config->gcSections), needsTocRestore(false),
|
|
|
|
scriptDefined(false) {}
|
2015-12-25 14:55:39 +08:00
|
|
|
|
2016-02-05 23:27:15 +08:00
|
|
|
public:
|
2017-02-16 14:12:22 +08:00
|
|
|
// True the symbol should point to its PLT entry.
|
|
|
|
// For SharedSymbol only.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t needsPltAddr : 1;
|
2016-10-21 15:22:30 +08:00
|
|
|
|
2019-02-14 05:49:55 +08:00
|
|
|
// True if this symbol is in the Iplt sub-section of the Plt and the Igot
|
|
|
|
// sub-section of the .got.plt or .got.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t isInIplt : 1;
|
2016-12-08 20:58:55 +08:00
|
|
|
|
2019-02-14 05:49:55 +08:00
|
|
|
// True if this symbol needs a GOT entry and its GOT entry is actually in
|
|
|
|
// Igot. This will be true only for certain non-preemptible ifuncs.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t gotInIgot : 1;
|
2016-12-08 20:58:55 +08:00
|
|
|
|
2018-02-24 05:57:49 +08:00
|
|
|
// True if this symbol is preemptible at load time.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t isPreemptible : 1;
|
2017-08-10 23:05:37 +08:00
|
|
|
|
2017-11-29 04:17:58 +08:00
|
|
|
// True if an undefined or shared symbol is used from a live section.
|
2020-04-25 19:58:00 +08:00
|
|
|
//
|
|
|
|
// NOTE: In Writer.cpp the field is used to mark local defined symbols
|
|
|
|
// which are referenced by relocations when -r or --emit-relocs is given.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t used : 1;
|
2017-11-29 04:17:58 +08:00
|
|
|
|
2018-05-07 03:13:29 +08:00
|
|
|
// True if a call to this symbol needs to be followed by a restore of the
|
|
|
|
// PPC64 toc pointer.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t needsTocRestore : 1;
|
2018-05-07 03:13:29 +08:00
|
|
|
|
2018-08-30 07:43:38 +08:00
|
|
|
// True if this symbol is defined by a linker script.
|
2019-11-16 03:13:36 +08:00
|
|
|
uint8_t scriptDefined : 1;
|
2018-08-30 07:43:38 +08:00
|
|
|
|
2019-05-29 11:55:20 +08:00
|
|
|
// The partition whose dynamic symbol table contains this symbol's definition.
|
|
|
|
uint8_t partition = 1;
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2016-04-04 22:04:16 +08:00
|
|
|
bool isSection() const { return type == llvm::ELF::STT_SECTION; }
|
|
|
|
bool isTls() const { return type == llvm::ELF::STT_TLS; }
|
|
|
|
bool isFunc() const { return type == llvm::ELF::STT_FUNC; }
|
|
|
|
bool isGnuIFunc() const { return type == llvm::ELF::STT_GNU_IFUNC; }
|
|
|
|
bool isObject() const { return type == llvm::ELF::STT_OBJECT; }
|
|
|
|
bool isFile() const { return type == llvm::ELF::STT_FILE; }
|
2015-07-25 05:03:07 +08:00
|
|
|
};
|
|
|
|
|
2017-11-06 12:35:31 +08:00
|
|
|
// Represents a symbol that is defined in the current output file.
|
2017-11-04 05:21:47 +08:00
|
|
|
class Defined : public Symbol {
|
2015-12-24 08:47:42 +08:00
|
|
|
public:
|
2017-11-30 06:47:35 +08:00
|
|
|
Defined(InputFile *file, StringRefZ name, uint8_t binding, uint8_t stOther,
|
|
|
|
uint8_t type, uint64_t value, uint64_t size, SectionBase *section)
|
|
|
|
: Symbol(DefinedKind, file, name, binding, stOther, type), value(value),
|
2017-11-25 03:06:14 +08:00
|
|
|
size(size), section(section) {}
|
2016-04-04 22:04:16 +08:00
|
|
|
|
2017-11-06 12:35:31 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->isDefined(); }
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2017-03-01 03:29:55 +08:00
|
|
|
uint64_t value;
|
|
|
|
uint64_t size;
|
2017-03-09 06:36:28 +08:00
|
|
|
SectionBase *section;
|
2015-09-26 02:56:53 +08:00
|
|
|
};
|
|
|
|
|
2019-05-16 11:29:03 +08:00
|
|
|
// Represents a common symbol.
|
|
|
|
//
|
|
|
|
// On Unix, it is traditionally allowed to write variable definitions
|
|
|
|
// without initialization expressions (such as "int foo;") to header
|
|
|
|
// files. Such definition is called "tentative definition".
|
|
|
|
//
|
|
|
|
// Using tentative definition is usually considered a bad practice
|
|
|
|
// because you should write only declarations (such as "extern int
|
|
|
|
// foo;") to header files. Nevertheless, the linker and the compiler
|
|
|
|
// have to do something to support bad code by allowing duplicate
|
|
|
|
// definitions for this particular case.
|
|
|
|
//
|
|
|
|
// Common symbols represent variable definitions without initializations.
|
2019-10-29 09:41:38 +08:00
|
|
|
// The compiler creates common symbols when it sees variable definitions
|
2019-05-16 11:29:03 +08:00
|
|
|
// without initialization (you can suppress this behavior and let the
|
|
|
|
// compiler create a regular defined symbol by -fno-common).
|
|
|
|
//
|
|
|
|
// The linker allows common symbols to be replaced by regular defined
|
|
|
|
// symbols. If there are remaining common symbols after name resolution is
|
|
|
|
// complete, they are converted to regular defined symbols in a .bss
|
|
|
|
// section. (Therefore, the later passes don't see any CommonSymbols.)
|
|
|
|
class CommonSymbol : public Symbol {
|
|
|
|
public:
|
|
|
|
CommonSymbol(InputFile *file, StringRefZ name, uint8_t binding,
|
|
|
|
uint8_t stOther, uint8_t type, uint64_t alignment, uint64_t size)
|
|
|
|
: Symbol(CommonKind, file, name, binding, stOther, type),
|
|
|
|
alignment(alignment), size(size) {}
|
|
|
|
|
|
|
|
static bool classof(const Symbol *s) { return s->isCommon(); }
|
|
|
|
|
|
|
|
uint32_t alignment;
|
|
|
|
uint64_t size;
|
|
|
|
};
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
class Undefined : public Symbol {
|
2015-12-23 07:00:50 +08:00
|
|
|
public:
|
2017-11-30 06:47:35 +08:00
|
|
|
Undefined(InputFile *file, StringRefZ name, uint8_t binding, uint8_t stOther,
|
2019-05-22 17:06:42 +08:00
|
|
|
uint8_t type, uint32_t discardedSecIdx = 0)
|
|
|
|
: Symbol(UndefinedKind, file, name, binding, stOther, type),
|
|
|
|
discardedSecIdx(discardedSecIdx) {}
|
2015-12-23 09:06:39 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; }
|
2019-05-22 17:06:42 +08:00
|
|
|
|
|
|
|
// The section index if in a discarded section, 0 otherwise.
|
|
|
|
uint32_t discardedSecIdx;
|
2015-12-23 07:00:50 +08:00
|
|
|
};
|
2015-08-31 09:46:20 +08:00
|
|
|
|
2017-11-06 12:13:24 +08:00
|
|
|
class SharedSymbol : public Symbol {
|
2015-09-08 23:50:05 +08:00
|
|
|
public:
|
2017-11-04 05:21:47 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == SharedKind; }
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2017-12-21 00:28:19 +08:00
|
|
|
SharedSymbol(InputFile &file, StringRef name, uint8_t binding,
|
2017-11-30 06:47:35 +08:00
|
|
|
uint8_t stOther, uint8_t type, uint64_t value, uint64_t size,
|
2017-12-12 09:45:49 +08:00
|
|
|
uint32_t alignment, uint32_t verdefIndex)
|
2019-07-02 19:37:21 +08:00
|
|
|
: Symbol(SharedKind, &file, name, binding, stOther, type), value(value),
|
|
|
|
size(size), alignment(alignment) {
|
2018-04-27 01:58:58 +08:00
|
|
|
this->verdefIndex = verdefIndex;
|
2017-10-13 05:45:53 +08:00
|
|
|
// GNU ifunc is a mechanism to allow user-supplied functions to
|
|
|
|
// resolve PLT slot values at load-time. This is contrary to the
|
2017-12-24 00:34:58 +08:00
|
|
|
// regular symbol resolution scheme in which symbols are resolved just
|
2017-10-13 05:45:53 +08:00
|
|
|
// by name. Using this hook, you can program how symbols are solved
|
|
|
|
// for you program. For example, you can make "memcpy" to be resolved
|
|
|
|
// to a SSE-enabled version of memcpy only when a machine running the
|
|
|
|
// program supports the SSE instruction set.
|
|
|
|
//
|
|
|
|
// Naturally, such symbols should always be called through their PLT
|
|
|
|
// slots. What GNU ifunc symbols point to are resolver functions, and
|
|
|
|
// calling them directly doesn't make sense (unless you are writing a
|
|
|
|
// loader).
|
|
|
|
//
|
|
|
|
// For DSO symbols, we always call them through PLT slots anyway.
|
|
|
|
// So there's no difference between GNU ifunc and regular function
|
2017-10-13 05:52:14 +08:00
|
|
|
// symbols if they are in DSOs. So we can handle GNU_IFUNC as FUNC.
|
2017-10-13 05:45:53 +08:00
|
|
|
if (this->type == llvm::ELF::STT_GNU_IFUNC)
|
2017-07-11 19:40:59 +08:00
|
|
|
this->type = llvm::ELF::STT_FUNC;
|
2017-08-05 06:31:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-09 01:35:55 +08:00
|
|
|
SharedFile &getFile() const { return *cast<SharedFile>(file); }
|
2015-10-12 04:59:12 +08:00
|
|
|
|
2017-10-29 04:15:56 +08:00
|
|
|
uint64_t value; // st_value
|
2017-10-31 01:43:16 +08:00
|
|
|
uint64_t size; // st_size
|
2019-07-02 19:37:21 +08:00
|
|
|
uint32_t alignment;
|
2015-09-08 23:50:05 +08:00
|
|
|
};
|
|
|
|
|
2018-04-04 01:16:52 +08:00
|
|
|
// LazyArchive and LazyObject represent a symbols that is not yet in the link,
|
|
|
|
// but we know where to find it if needed. If the resolver finds both Undefined
|
|
|
|
// and Lazy for the same name, it will ask the Lazy to load a file.
|
2017-10-25 00:27:31 +08:00
|
|
|
//
|
|
|
|
// A special complication is the handling of weak undefined symbols. They should
|
|
|
|
// not load a file, but we have to remember we have seen both the weak undefined
|
|
|
|
// and the lazy. We represent that with a lazy symbol with a weak binding. This
|
|
|
|
// means that code looking for undefined symbols normally also has to take lazy
|
|
|
|
// symbols into consideration.
|
2016-04-08 03:24:51 +08:00
|
|
|
|
2017-10-25 00:27:31 +08:00
|
|
|
// This class represents a symbol defined in an archive file. It is
|
|
|
|
// created from an archive file header, and it knows how to load an
|
|
|
|
// object file from an archive to replace itself with a defined
|
|
|
|
// symbol.
|
2018-04-04 01:16:52 +08:00
|
|
|
class LazyArchive : public Symbol {
|
2016-04-08 03:24:51 +08:00
|
|
|
public:
|
2019-05-16 10:14:00 +08:00
|
|
|
LazyArchive(InputFile &file, const llvm::object::Archive::Symbol s)
|
2018-04-04 01:16:52 +08:00
|
|
|
: Symbol(LazyArchiveKind, &file, s.getName(), llvm::ELF::STB_GLOBAL,
|
2019-05-16 10:14:00 +08:00
|
|
|
llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE),
|
2018-04-04 01:16:52 +08:00
|
|
|
sym(s) {}
|
2016-04-08 03:24:51 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == LazyArchiveKind; }
|
2016-04-08 03:24:51 +08:00
|
|
|
|
2018-08-09 07:48:12 +08:00
|
|
|
MemoryBufferRef getMemberBuffer();
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
const llvm::object::Archive::Symbol sym;
|
|
|
|
};
|
|
|
|
|
2016-04-08 03:24:51 +08:00
|
|
|
// LazyObject symbols represents symbols in object files between
|
|
|
|
// --start-lib and --end-lib options.
|
2018-04-04 01:16:52 +08:00
|
|
|
class LazyObject : public Symbol {
|
2016-04-08 03:24:51 +08:00
|
|
|
public:
|
2019-05-16 10:14:00 +08:00
|
|
|
LazyObject(InputFile &file, StringRef name)
|
2018-04-04 01:16:52 +08:00
|
|
|
: Symbol(LazyObjectKind, &file, name, llvm::ELF::STB_GLOBAL,
|
2019-05-16 10:14:00 +08:00
|
|
|
llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {}
|
2016-04-08 03:24:51 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; }
|
2016-04-08 03:24:51 +08:00
|
|
|
};
|
|
|
|
|
2015-12-25 14:12:18 +08:00
|
|
|
// Some linker-generated symbols need to be created as
|
2017-11-06 12:35:31 +08:00
|
|
|
// Defined symbols.
|
2017-03-01 03:29:55 +08:00
|
|
|
struct ElfSym {
|
2017-04-14 05:37:56 +08:00
|
|
|
// __bss_start
|
2017-11-06 12:35:31 +08:00
|
|
|
static Defined *bss;
|
2017-04-05 18:03:25 +08:00
|
|
|
|
2017-04-14 05:37:56 +08:00
|
|
|
// etext and _etext
|
2017-11-06 12:35:31 +08:00
|
|
|
static Defined *etext1;
|
|
|
|
static Defined *etext2;
|
2016-02-26 22:36:36 +08:00
|
|
|
|
2017-04-14 05:37:56 +08:00
|
|
|
// edata and _edata
|
2017-11-06 12:35:31 +08:00
|
|
|
static Defined *edata1;
|
|
|
|
static Defined *edata2;
|
2016-02-26 22:36:36 +08:00
|
|
|
|
2017-04-14 05:37:56 +08:00
|
|
|
// end and _end
|
2017-11-06 12:35:31 +08:00
|
|
|
static Defined *end1;
|
|
|
|
static Defined *end2;
|
2015-12-25 14:12:18 +08:00
|
|
|
|
2017-06-26 23:11:24 +08:00
|
|
|
// The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
|
|
|
|
// be at some offset from the base of the .got section, usually 0 or
|
|
|
|
// the end of the .got.
|
2017-11-06 12:35:31 +08:00
|
|
|
static Defined *globalOffsetTable;
|
2017-06-26 23:11:24 +08:00
|
|
|
|
2017-04-14 05:37:56 +08:00
|
|
|
// _gp, _gp_disp and __gnu_local_gp symbols. Only for MIPS.
|
2017-11-06 12:35:31 +08:00
|
|
|
static Defined *mipsGp;
|
|
|
|
static Defined *mipsGpDisp;
|
|
|
|
static Defined *mipsLocalGp;
|
2018-04-20 00:54:30 +08:00
|
|
|
|
2019-01-16 02:30:23 +08:00
|
|
|
// __rel{,a}_iplt_{start,end} symbols.
|
|
|
|
static Defined *relaIpltStart;
|
2018-04-20 00:54:30 +08:00
|
|
|
static Defined *relaIpltEnd;
|
2019-05-30 18:00:20 +08:00
|
|
|
|
2019-06-14 10:14:53 +08:00
|
|
|
// __global_pointer$ for RISC-V.
|
|
|
|
static Defined *riscvGlobalPointer;
|
|
|
|
|
2019-05-30 18:00:20 +08:00
|
|
|
// _TLS_MODULE_BASE_ on targets that support TLSDESC.
|
|
|
|
static Defined *tlsModuleBase;
|
2015-12-25 14:12:18 +08:00
|
|
|
};
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
// A buffer class that is large enough to hold any Symbol-derived
|
2017-11-01 00:07:41 +08:00
|
|
|
// object. We allocate memory using this class and instantiate a symbol
|
|
|
|
// using the placement new.
|
|
|
|
union SymbolUnion {
|
2017-11-06 12:35:31 +08:00
|
|
|
alignas(Defined) char a[sizeof(Defined)];
|
2019-05-17 09:55:20 +08:00
|
|
|
alignas(CommonSymbol) char b[sizeof(CommonSymbol)];
|
2017-11-01 00:07:41 +08:00
|
|
|
alignas(Undefined) char c[sizeof(Undefined)];
|
|
|
|
alignas(SharedSymbol) char d[sizeof(SharedSymbol)];
|
|
|
|
alignas(LazyArchive) char e[sizeof(LazyArchive)];
|
|
|
|
alignas(LazyObject) char f[sizeof(LazyObject)];
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
};
|
|
|
|
|
2019-07-09 15:12:58 +08:00
|
|
|
// It is important to keep the size of SymbolUnion small for performance and
|
|
|
|
// memory usage reasons. 80 bytes is a soft limit based on the size of Defined
|
|
|
|
// on a 64-bit system.
|
|
|
|
static_assert(sizeof(SymbolUnion) <= 80, "SymbolUnion too large");
|
|
|
|
|
2019-05-17 09:55:20 +08:00
|
|
|
template <typename T> struct AssertSymbol {
|
2018-02-14 01:32:31 +08:00
|
|
|
static_assert(std::is_trivially_destructible<T>(),
|
|
|
|
"Symbol types must be trivially destructible");
|
2017-11-01 00:07:41 +08:00
|
|
|
static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
|
|
|
|
static_assert(alignof(T) <= alignof(SymbolUnion),
|
|
|
|
"SymbolUnion not aligned enough");
|
2019-05-17 09:55:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline void assertSymbols() {
|
|
|
|
AssertSymbol<Defined>();
|
|
|
|
AssertSymbol<CommonSymbol>();
|
|
|
|
AssertSymbol<Undefined>();
|
|
|
|
AssertSymbol<SharedSymbol>();
|
|
|
|
AssertSymbol<LazyArchive>();
|
|
|
|
AssertSymbol<LazyObject>();
|
|
|
|
}
|
|
|
|
|
2019-05-24 21:29:17 +08:00
|
|
|
void printTraceSymbol(const Symbol *sym);
|
2019-05-17 09:55:20 +08:00
|
|
|
|
2019-05-20 11:36:33 +08:00
|
|
|
size_t Symbol::getSymbolSize() const {
|
|
|
|
switch (kind()) {
|
|
|
|
case CommonKind:
|
2019-05-17 09:55:20 +08:00
|
|
|
return sizeof(CommonSymbol);
|
2019-05-20 11:36:33 +08:00
|
|
|
case DefinedKind:
|
2019-05-17 09:55:20 +08:00
|
|
|
return sizeof(Defined);
|
2019-05-20 11:36:33 +08:00
|
|
|
case LazyArchiveKind:
|
2019-05-17 09:55:20 +08:00
|
|
|
return sizeof(LazyArchive);
|
2019-05-20 11:36:33 +08:00
|
|
|
case LazyObjectKind:
|
2019-05-17 09:55:20 +08:00
|
|
|
return sizeof(LazyObject);
|
2019-05-20 11:36:33 +08:00
|
|
|
case SharedKind:
|
2019-05-17 09:55:20 +08:00
|
|
|
return sizeof(SharedSymbol);
|
2019-05-20 11:36:33 +08:00
|
|
|
case UndefinedKind:
|
2019-05-17 09:55:20 +08:00
|
|
|
return sizeof(Undefined);
|
2019-05-20 11:36:33 +08:00
|
|
|
case PlaceholderKind:
|
2019-05-17 09:55:20 +08:00
|
|
|
return sizeof(Symbol);
|
|
|
|
}
|
2019-05-18 14:35:47 +08:00
|
|
|
llvm_unreachable("unknown symbol kind");
|
2019-05-17 09:55:20 +08:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:36:33 +08:00
|
|
|
// replace() replaces "this" object with a given symbol by memcpy'ing
|
|
|
|
// it over to "this". This function is called as a result of name
|
|
|
|
// resolution, e.g. to replace an undefind symbol with a defined symbol.
|
2019-08-13 14:19:39 +08:00
|
|
|
void Symbol::replace(const Symbol &newSym) {
|
2019-05-17 09:55:20 +08:00
|
|
|
using llvm::ELF::STT_TLS;
|
2017-11-01 00:07:41 +08:00
|
|
|
|
2020-04-19 06:04:44 +08:00
|
|
|
// st_value of STT_TLS represents the assigned offset, not the actual address
|
|
|
|
// which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can only be
|
|
|
|
// referenced by special TLS relocations. It is usually an error if a STT_TLS
|
|
|
|
// symbol is replaced by a non-STT_TLS symbol, vice versa. There are two
|
|
|
|
// exceptions: (a) a STT_NOTYPE lazy/undefined symbol can be replaced by a
|
|
|
|
// STT_TLS symbol, (b) a STT_TLS undefined symbol can be replaced by a
|
|
|
|
// STT_NOTYPE lazy symbol.
|
|
|
|
if (symbolKind != PlaceholderKind && !newSym.isLazy() &&
|
|
|
|
(type == STT_TLS) != (newSym.type == STT_TLS) &&
|
|
|
|
type != llvm::ELF::STT_NOTYPE)
|
2019-08-13 14:19:39 +08:00
|
|
|
error("TLS attribute mismatch: " + toString(*this) + "\n>>> defined in " +
|
|
|
|
toString(newSym.file) + "\n>>> defined in " + toString(file));
|
2019-05-16 10:14:00 +08:00
|
|
|
|
2019-05-20 11:36:33 +08:00
|
|
|
Symbol old = *this;
|
2019-08-13 14:19:39 +08:00
|
|
|
memcpy(this, &newSym, newSym.getSymbolSize());
|
2018-10-13 02:29:18 +08:00
|
|
|
|
2019-08-14 09:52:47 +08:00
|
|
|
// old may be a placeholder. The referenced fields must be initialized in
|
|
|
|
// SymbolTable::insert.
|
2019-05-20 11:36:33 +08:00
|
|
|
versionId = old.versionId;
|
|
|
|
visibility = old.visibility;
|
|
|
|
isUsedInRegularObj = old.isUsedInRegularObj;
|
|
|
|
exportDynamic = old.exportDynamic;
|
2019-08-13 17:12:52 +08:00
|
|
|
inDynamicList = old.inDynamicList;
|
2019-05-20 11:36:33 +08:00
|
|
|
canInline = old.canInline;
|
[ELF] Make binding (weak or non-weak) logic consistent for Undefined and SharedSymbol
This is a case missed by D64136. If %t1.o has a weak reference on foo,
and %t2.so has a non-weak reference on foo:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # undefined symbol: foo; STB_GLOBAL
2. gold %t1.o %t2.so # ok; STB_WEAK
3. gold %t2.so %t1.o # undefined reference to 'foo'; STB_GLOBAL
4. ld.bfd %t1.o %t2.so # undefined reference to `foo'; STB_WEAK
5. ld.bfd %t2.so %t1.o # undefined reference to `foo'; STB_WEAK
```
It can be argued that in both cases, the binding of the undefined foo
should be set to STB_WEAK, because the binding should not be affected by
referenced from shared objects.
--allow-shlib-undefined doesn't suppress errors (3,4,5), but -shared or
--noinhibit-exec allows ld.bfd/gold to produce a binary:
```
3. gold -shared %t2.so %t1.o # ok; STB_GLOBAL
4. ld.bfd -shared %t2.so %t1.o # ok; STB_WEAK
5. ld.bfd -shared %t1.o %t1.o # ok; STB_WEAK
```
If %t2.so has DT_NEEDED entries, ld.bfd will load them (lld/gold don't
have the behavior). If one of the DSO defines foo and it is in the
link-time search path (e.g. DT_NEEDED entry is an absolute path, via
-rpath=, via -rpath-link=, etc),
`ld.bfd %t1.o %t2.so` and `ld.bfd %t1.o %t2.so` will not error.
In this patch, we make Undefined and SharedSymbol share the same binding
computing logic. Case 1 will be allowed:
```
0. ld.lld %t1.o %t2.so # ok; STB_WEAK; accepted since D64136
1. ld.lld %t2.so %t1.o # ok; STB_WEAK; changed by this patch
```
In the future, we can explore the option that turns both (0,1) into
errors if --no-allow-shlib-undefined (default when linking an
executable) is in action.
Reviewed By: ruiu
Differential Revision: https://reviews.llvm.org/D65584
llvm-svn: 368038
2019-08-06 22:03:45 +08:00
|
|
|
referenced = old.referenced;
|
2019-05-20 11:36:33 +08:00
|
|
|
traced = old.traced;
|
|
|
|
isPreemptible = old.isPreemptible;
|
|
|
|
scriptDefined = old.scriptDefined;
|
2019-05-29 11:55:20 +08:00
|
|
|
partition = old.partition;
|
2018-10-13 02:29:18 +08:00
|
|
|
|
2019-05-22 17:19:30 +08:00
|
|
|
// Symbol length is computed lazily. If we already know a symbol length,
|
|
|
|
// propagate it.
|
|
|
|
if (nameData == old.nameData && nameSize == 0 && old.nameSize != 0)
|
|
|
|
nameSize = old.nameSize;
|
|
|
|
|
2016-07-18 01:50:09 +08:00
|
|
|
// Print out a log message if --trace-symbol was specified.
|
|
|
|
// This is for debugging.
|
2019-05-20 11:36:33 +08:00
|
|
|
if (traced)
|
|
|
|
printTraceSymbol(this);
|
ELF: New symbol table design.
This patch implements a new design for the symbol table that stores
SymbolBodies within a memory region of the Symbol object. Symbols are mutated
by constructing SymbolBodies in place over existing SymbolBodies, rather
than by mutating pointers. As mentioned in the initial proposal [1], this
memory layout helps reduce the cache miss rate by improving memory locality.
Performance numbers:
old(s) new(s)
Without debug info:
chrome 7.178 6.432 (-11.5%)
LLVMgold.so 0.505 0.502 (-0.5%)
clang 0.954 0.827 (-15.4%)
llvm-as 0.052 0.045 (-15.5%)
With debug info:
scylla 5.695 5.613 (-1.5%)
clang 14.396 14.143 (-1.8%)
Performance counter results show that the fewer required indirections is
indeed the cause of the improved performance. For example, when linking
chrome, stalled cycles decreases from 14,556,444,002 to 12,959,238,310, and
instructions per cycle increases from 0.78 to 0.83. We are also executing
many fewer instructions (15,516,401,933 down to 15,002,434,310), probably
because we spend less time allocating SymbolBodies.
The new mechanism by which symbols are added to the symbol table is by calling
add* functions on the SymbolTable.
In this patch, I handle local symbols by storing them inside "unparented"
SymbolBodies. This is suboptimal, but if we do want to try to avoid allocating
these SymbolBodies, we can probably do that separately.
I also removed a few members from the SymbolBody class that were only being
used to pass information from the input file to the symbol table.
This patch implements the new design for the ELF linker only. I intend to
prepare a similar patch for the COFF linker.
[1] http://lists.llvm.org/pipermail/llvm-dev/2016-April/098832.html
Differential Revision: http://reviews.llvm.org/D19752
llvm-svn: 268178
2016-05-01 12:55:03 +08:00
|
|
|
}
|
2018-04-18 07:30:05 +08:00
|
|
|
|
2018-10-26 23:07:12 +08:00
|
|
|
void maybeWarnUnorderableSymbol(const Symbol *sym);
|
2019-11-30 13:58:36 +08:00
|
|
|
bool computeIsPreemptible(const Symbol &sym);
|
2020-04-06 13:27:46 +08:00
|
|
|
void reportBackrefs();
|
|
|
|
|
|
|
|
// A mapping from a symbol to an InputFile referencing it backward. Used by
|
|
|
|
// --warn-backrefs.
|
|
|
|
extern llvm::DenseMap<const Symbol *, const InputFile *> backwardReferences;
|
2019-11-30 13:58:36 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
} // namespace elf
|
2015-07-25 05:03:07 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|