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;
25 static_assert(sizeof(SymbolUnion) <= 48,
26 "symbols should be optimized for memory usage");
28 // Returns a symbol name for an error message.
29 static std::string demangle(StringRef symName) {
31 if (Optional<std::string> s = demangleMSVC(symName))
35 std::string toString(coff::Symbol &b) { return demangle(b.getName()); }
36 std::string toCOFFString(const Archive::Symbol &b) {
37 return demangle(b.getName());
42 StringRef Symbol::getName() {
43 // COFF symbol names are read lazily for a performance reason.
44 // Non-external symbol names are never used by the linker except for logging
45 // or debugging. Their internal references are resolved not by name but by
46 // symbol index. And because they are not external, no one can refer them by
47 // name. Object files contain lots of non-external symbols, and creating
48 // StringRefs for them (which involves lots of strlen() on the string table)
49 // is a waste of time.
50 if (nameData == nullptr) {
51 auto *d = cast<DefinedCOFF>(this);
53 cast<ObjFile>(d->file)->getCOFFObj()->getSymbolName(d->sym, nameStr);
54 nameData = nameStr.data();
55 nameSize = nameStr.size();
56 assert(nameSize == nameStr.size() && "name length truncated");
58 return StringRef(nameData, nameSize);
61 InputFile *Symbol::getFile() {
62 if (auto *sym = dyn_cast<DefinedCOFF>(this))
64 if (auto *sym = dyn_cast<Lazy>(this))
69 bool Symbol::isLive() const {
70 if (auto *r = dyn_cast<DefinedRegular>(this))
71 return r->getChunk()->live;
72 if (auto *imp = dyn_cast<DefinedImportData>(this))
73 return imp->file->live;
74 if (auto *imp = dyn_cast<DefinedImportThunk>(this))
75 return imp->wrappedSym->file->thunkLive;
76 // Assume any other kind of symbol is live.
81 void Symbol::replaceKeepingName(Symbol *other, size_t size) {
82 StringRef origName = getName();
83 memcpy(this, other, size);
84 nameData = origName.data();
85 nameSize = origName.size();
88 COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
89 size_t symSize = cast<ObjFile>(file)->getCOFFObj()->getSymbolTableEntrySize();
90 if (symSize == sizeof(coff_symbol16))
91 return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(sym));
92 assert(symSize == sizeof(coff_symbol32));
93 return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(sym));
96 uint16_t DefinedAbsolute::numOutputSections;
98 static Chunk *makeImportThunk(DefinedImportData *s, uint16_t machine) {
100 return make<ImportThunkChunkX64>(s);
102 return make<ImportThunkChunkX86>(s);
103 if (machine == ARM64)
104 return make<ImportThunkChunkARM64>(s);
105 assert(machine == ARMNT);
106 return make<ImportThunkChunkARM>(s);
109 DefinedImportThunk::DefinedImportThunk(StringRef name, DefinedImportData *s,
111 : Defined(DefinedImportThunkKind, name), wrappedSym(s),
112 data(makeImportThunk(s, machine)) {}
114 Defined *Undefined::getWeakAlias() {
115 // A weak alias may be a weak alias to another symbol, so check recursively.
116 for (Symbol *a = weakAlias; a; a = cast<Undefined>(a)->weakAlias)
117 if (auto *d = dyn_cast<Defined>(a))
122 MemoryBufferRef Lazy::getMemberBuffer() {
124 CHECK(sym.getMember(),
125 "could not get the member for symbol " + toCOFFString(sym));
126 return CHECK(c.getMemoryBufferRef(),
127 "could not get the buffer for the member defining symbol " +