xref: /freebsd/contrib/llvm-project/lld/COFF/Symbols.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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/Demangle/Demangle.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 using namespace llvm;
20 using namespace llvm::object;
21 
22 using namespace lld::coff;
23 
24 namespace lld {
25 
26 static_assert(sizeof(SymbolUnion) <= 48,
27               "symbols should be optimized for memory usage");
28 
29 // Returns a symbol name for an error message.
30 static std::string maybeDemangleSymbol(StringRef symName) {
31   if (config->demangle) {
32     std::string prefix;
33     StringRef prefixless = symName;
34     if (prefixless.consume_front("__imp_"))
35       prefix = "__declspec(dllimport) ";
36     StringRef demangleInput = prefixless;
37     if (config->machine == I386)
38       demangleInput.consume_front("_");
39     std::string demangled = demangle(std::string(demangleInput));
40     if (demangled != demangleInput)
41       return prefix + demangle(std::string(demangleInput));
42     return (prefix + prefixless).str();
43   }
44   return std::string(symName);
45 }
46 std::string toString(coff::Symbol &b) {
47   return maybeDemangleSymbol(b.getName());
48 }
49 std::string toCOFFString(const Archive::Symbol &b) {
50   return maybeDemangleSymbol(b.getName());
51 }
52 
53 namespace coff {
54 
55 void Symbol::computeName() {
56   assert(nameData == nullptr &&
57          "should only compute the name once for DefinedCOFF symbols");
58   auto *d = cast<DefinedCOFF>(this);
59   StringRef nameStr =
60       check(cast<ObjFile>(d->file)->getCOFFObj()->getSymbolName(d->sym));
61   nameData = nameStr.data();
62   nameSize = nameStr.size();
63   assert(nameSize == nameStr.size() && "name length truncated");
64 }
65 
66 InputFile *Symbol::getFile() {
67   if (auto *sym = dyn_cast<DefinedCOFF>(this))
68     return sym->file;
69   if (auto *sym = dyn_cast<LazyArchive>(this))
70     return sym->file;
71   if (auto *sym = dyn_cast<LazyObject>(this))
72     return sym->file;
73   return nullptr;
74 }
75 
76 bool Symbol::isLive() const {
77   if (auto *r = dyn_cast<DefinedRegular>(this))
78     return r->getChunk()->live;
79   if (auto *imp = dyn_cast<DefinedImportData>(this))
80     return imp->file->live;
81   if (auto *imp = dyn_cast<DefinedImportThunk>(this))
82     return imp->wrappedSym->file->thunkLive;
83   // Assume any other kind of symbol is live.
84   return true;
85 }
86 
87 // MinGW specific.
88 void Symbol::replaceKeepingName(Symbol *other, size_t size) {
89   StringRef origName = getName();
90   memcpy(this, other, size);
91   nameData = origName.data();
92   nameSize = origName.size();
93 }
94 
95 COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
96   size_t symSize = cast<ObjFile>(file)->getCOFFObj()->getSymbolTableEntrySize();
97   if (symSize == sizeof(coff_symbol16))
98     return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(sym));
99   assert(symSize == sizeof(coff_symbol32));
100   return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(sym));
101 }
102 
103 uint16_t DefinedAbsolute::numOutputSections;
104 
105 static Chunk *makeImportThunk(DefinedImportData *s, uint16_t machine) {
106   if (machine == AMD64)
107     return make<ImportThunkChunkX64>(s);
108   if (machine == I386)
109     return make<ImportThunkChunkX86>(s);
110   if (machine == ARM64)
111     return make<ImportThunkChunkARM64>(s);
112   assert(machine == ARMNT);
113   return make<ImportThunkChunkARM>(s);
114 }
115 
116 DefinedImportThunk::DefinedImportThunk(StringRef name, DefinedImportData *s,
117                                        uint16_t machine)
118     : Defined(DefinedImportThunkKind, name), wrappedSym(s),
119       data(makeImportThunk(s, machine)) {}
120 
121 Defined *Undefined::getWeakAlias() {
122   // A weak alias may be a weak alias to another symbol, so check recursively.
123   for (Symbol *a = weakAlias; a; a = cast<Undefined>(a)->weakAlias)
124     if (auto *d = dyn_cast<Defined>(a))
125       return d;
126   return nullptr;
127 }
128 
129 MemoryBufferRef LazyArchive::getMemberBuffer() {
130   Archive::Child c =
131     CHECK(sym.getMember(),
132           "could not get the member for symbol " + toCOFFString(sym));
133   return CHECK(c.getMemoryBufferRef(),
134       "could not get the buffer for the member defining symbol " +
135       toCOFFString(sym));
136 }
137 } // namespace coff
138 } // namespace lld
139