1 //===- Symbols.cpp --------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "InputFiles.h"
11 #include "lld/Common/ErrorHandler.h"
12 #include "lld/Common/Memory.h"
13 #include "lld/Common/Strings.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm::object;
21 using namespace lld::coff;
23 static_assert(sizeof(SymbolUnion) <= 48,
24 "symbols should be optimized for memory usage");
26 // Returns a symbol name for an error message.
27 std::string lld::toString(coff::Symbol &b) {
29 if (Optional<std::string> s = lld::demangleMSVC(b.getName()))
37 StringRef Symbol::getName() {
38 // COFF symbol names are read lazily for a performance reason.
39 // Non-external symbol names are never used by the linker except for logging
40 // or debugging. Their internal references are resolved not by name but by
41 // symbol index. And because they are not external, no one can refer them by
42 // name. Object files contain lots of non-external symbols, and creating
43 // StringRefs for them (which involves lots of strlen() on the string table)
44 // is a waste of time.
45 if (nameData == nullptr) {
46 auto *d = cast<DefinedCOFF>(this);
48 cast<ObjFile>(d->file)->getCOFFObj()->getSymbolName(d->sym, nameStr);
49 nameData = nameStr.data();
50 nameSize = nameStr.size();
51 assert(nameSize == nameStr.size() && "name length truncated");
53 return StringRef(nameData, nameSize);
56 InputFile *Symbol::getFile() {
57 if (auto *sym = dyn_cast<DefinedCOFF>(this))
59 if (auto *sym = dyn_cast<Lazy>(this))
64 bool Symbol::isLive() const {
65 if (auto *r = dyn_cast<DefinedRegular>(this))
66 return r->getChunk()->live;
67 if (auto *imp = dyn_cast<DefinedImportData>(this))
68 return imp->file->live;
69 if (auto *imp = dyn_cast<DefinedImportThunk>(this))
70 return imp->wrappedSym->file->thunkLive;
71 // Assume any other kind of symbol is live.
76 void Symbol::replaceKeepingName(Symbol *other, size_t size) {
77 StringRef origName = getName();
78 memcpy(this, other, size);
79 nameData = origName.data();
80 nameSize = origName.size();
83 COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
84 size_t symSize = cast<ObjFile>(file)->getCOFFObj()->getSymbolTableEntrySize();
85 if (symSize == sizeof(coff_symbol16))
86 return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(sym));
87 assert(symSize == sizeof(coff_symbol32));
88 return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(sym));
91 uint16_t DefinedAbsolute::numOutputSections;
93 static Chunk *makeImportThunk(DefinedImportData *s, uint16_t machine) {
95 return make<ImportThunkChunkX64>(s);
97 return make<ImportThunkChunkX86>(s);
99 return make<ImportThunkChunkARM64>(s);
100 assert(machine == ARMNT);
101 return make<ImportThunkChunkARM>(s);
104 DefinedImportThunk::DefinedImportThunk(StringRef name, DefinedImportData *s,
106 : Defined(DefinedImportThunkKind, name), wrappedSym(s),
107 data(makeImportThunk(s, machine)) {}
109 Defined *Undefined::getWeakAlias() {
110 // A weak alias may be a weak alias to another symbol, so check recursively.
111 for (Symbol *a = weakAlias; a; a = cast<Undefined>(a)->weakAlias)
112 if (auto *d = dyn_cast<Defined>(a))