]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/Symbols.cpp
Merge lld trunk r366426, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / Symbols.cpp
1 //===- Symbols.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 #include "Symbols.h"
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"
17
18 using namespace llvm;
19 using namespace llvm::object;
20
21 using namespace lld::coff;
22
23 static_assert(sizeof(SymbolUnion) <= 48,
24               "symbols should be optimized for memory usage");
25
26 // Returns a symbol name for an error message.
27 std::string lld::toString(coff::Symbol &b) {
28   if (config->demangle)
29     if (Optional<std::string> s = lld::demangleMSVC(b.getName()))
30       return *s;
31   return b.getName();
32 }
33
34 namespace lld {
35 namespace coff {
36
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);
47     StringRef nameStr;
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");
52   }
53   return StringRef(nameData, nameSize);
54 }
55
56 InputFile *Symbol::getFile() {
57   if (auto *sym = dyn_cast<DefinedCOFF>(this))
58     return sym->file;
59   if (auto *sym = dyn_cast<Lazy>(this))
60     return sym->file;
61   return nullptr;
62 }
63
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.
72   return true;
73 }
74
75 // MinGW specific.
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();
81 }
82
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));
89 }
90
91 uint16_t DefinedAbsolute::numOutputSections;
92
93 static Chunk *makeImportThunk(DefinedImportData *s, uint16_t machine) {
94   if (machine == AMD64)
95     return make<ImportThunkChunkX64>(s);
96   if (machine == I386)
97     return make<ImportThunkChunkX86>(s);
98   if (machine == ARM64)
99     return make<ImportThunkChunkARM64>(s);
100   assert(machine == ARMNT);
101   return make<ImportThunkChunkARM>(s);
102 }
103
104 DefinedImportThunk::DefinedImportThunk(StringRef name, DefinedImportData *s,
105                                        uint16_t machine)
106     : Defined(DefinedImportThunkKind, name), wrappedSym(s),
107       data(makeImportThunk(s, machine)) {}
108
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))
113       return d;
114   return nullptr;
115 }
116 } // namespace coff
117 } // namespace lld