[DAGCombiner] Add target hook function to decide folding (mul (add x, c1), c2)
[llvm-project.git] / lld / COFF / Symbols.cpp
blob8a6a9b27d45fa797bac5eac356dd80a53cbc3741
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 //===----------------------------------------------------------------------===//
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"
19 using namespace llvm;
20 using namespace llvm::object;
22 using namespace lld::coff;
24 namespace lld {
26 static_assert(sizeof(SymbolUnion) <= 48,
27 "symbols should be optimized for memory usage");
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();
44 return std::string(symName);
46 std::string toString(coff::Symbol &b) {
47 return maybeDemangleSymbol(b.getName());
49 std::string toCOFFString(const Archive::Symbol &b) {
50 return maybeDemangleSymbol(b.getName());
53 namespace coff {
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");
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 if (auto *sym = dyn_cast<LazyDLLSymbol>(this))
74 return sym->file;
75 return nullptr;
78 bool Symbol::isLive() const {
79 if (auto *r = dyn_cast<DefinedRegular>(this))
80 return r->getChunk()->live;
81 if (auto *imp = dyn_cast<DefinedImportData>(this))
82 return imp->file->live;
83 if (auto *imp = dyn_cast<DefinedImportThunk>(this))
84 return imp->wrappedSym->file->thunkLive;
85 // Assume any other kind of symbol is live.
86 return true;
89 // MinGW specific.
90 void Symbol::replaceKeepingName(Symbol *other, size_t size) {
91 StringRef origName = getName();
92 memcpy(this, other, size);
93 nameData = origName.data();
94 nameSize = origName.size();
97 COFFSymbolRef DefinedCOFF::getCOFFSymbol() {
98 size_t symSize = cast<ObjFile>(file)->getCOFFObj()->getSymbolTableEntrySize();
99 if (symSize == sizeof(coff_symbol16))
100 return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(sym));
101 assert(symSize == sizeof(coff_symbol32));
102 return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(sym));
105 uint16_t DefinedAbsolute::numOutputSections;
107 static Chunk *makeImportThunk(DefinedImportData *s, uint16_t machine) {
108 if (machine == AMD64)
109 return make<ImportThunkChunkX64>(s);
110 if (machine == I386)
111 return make<ImportThunkChunkX86>(s);
112 if (machine == ARM64)
113 return make<ImportThunkChunkARM64>(s);
114 assert(machine == ARMNT);
115 return make<ImportThunkChunkARM>(s);
118 DefinedImportThunk::DefinedImportThunk(StringRef name, DefinedImportData *s,
119 uint16_t machine)
120 : Defined(DefinedImportThunkKind, name), wrappedSym(s),
121 data(makeImportThunk(s, machine)) {}
123 Defined *Undefined::getWeakAlias() {
124 // A weak alias may be a weak alias to another symbol, so check recursively.
125 for (Symbol *a = weakAlias; a; a = cast<Undefined>(a)->weakAlias)
126 if (auto *d = dyn_cast<Defined>(a))
127 return d;
128 return nullptr;
131 MemoryBufferRef LazyArchive::getMemberBuffer() {
132 Archive::Child c =
133 CHECK(sym.getMember(),
134 "could not get the member for symbol " + toCOFFString(sym));
135 return CHECK(c.getMemoryBufferRef(),
136 "could not get the buffer for the member defining symbol " +
137 toCOFFString(sym));
139 } // namespace coff
140 } // namespace lld