1 //===- SymbolTable.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 "SymbolTable.h"
10 #include "InputFiles.h"
11 #include "Symbols.h"
12 #include "lld/Common/ErrorHandler.h"
13 #include "lld/Common/Memory.h"
14 
15 using namespace llvm;
16 using namespace lld;
17 using namespace lld::macho;
18 
19 Symbol *SymbolTable::find(StringRef name) {
20   auto it = symMap.find(llvm::CachedHashStringRef(name));
21   if (it == symMap.end())
22     return nullptr;
23   return symVector[it->second];
24 }
25 
26 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
27   auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
28 
29   // Name already present in the symbol table.
30   if (!p.second)
31     return {symVector[p.first->second], false};
32 
33   // Name is a new symbol.
34   Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
35   symVector.push_back(sym);
36   return {sym, true};
37 }
38 
39 Symbol *SymbolTable::addDefined(StringRef name, InputSection *isec,
40                                 uint32_t value, bool isWeakDef) {
41   Symbol *s;
42   bool wasInserted;
43   std::tie(s, wasInserted) = insert(name);
44 
45   if (!wasInserted) {
46     if (auto *defined = dyn_cast<Defined>(s)) {
47       if (isWeakDef)
48         return s;
49       if (!defined->isWeakDef())
50         error("duplicate symbol: " + name);
51     }
52     // Defined symbols take priority over other types of symbols, so in case
53     // of a name conflict, we fall through to the replaceSymbol() call below.
54   }
55 
56   replaceSymbol<Defined>(s, name, isec, value, isWeakDef);
57   return s;
58 }
59 
60 Symbol *SymbolTable::addUndefined(StringRef name) {
61   Symbol *s;
62   bool wasInserted;
63   std::tie(s, wasInserted) = insert(name);
64 
65   if (wasInserted)
66     replaceSymbol<Undefined>(s, name);
67   else if (LazySymbol *lazy = dyn_cast<LazySymbol>(s))
68     lazy->fetchArchiveMember();
69   return s;
70 }
71 
72 Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef) {
73   Symbol *s;
74   bool wasInserted;
75   std::tie(s, wasInserted) = insert(name);
76 
77   if (wasInserted || isa<Undefined>(s) ||
78       (isa<DylibSymbol>(s) && !isWeakDef && s->isWeakDef()))
79     replaceSymbol<DylibSymbol>(s, file, name, isWeakDef);
80 
81   return s;
82 }
83 
84 Symbol *SymbolTable::addLazy(StringRef name, ArchiveFile *file,
85                              const llvm::object::Archive::Symbol &sym) {
86   Symbol *s;
87   bool wasInserted;
88   std::tie(s, wasInserted) = insert(name);
89 
90   if (wasInserted)
91     replaceSymbol<LazySymbol>(s, file, sym);
92   else if (isa<Undefined>(s) || (isa<DylibSymbol>(s) && s->isWeakDef()))
93     file->fetch(sym);
94   return s;
95 }
96 
97 Symbol *SymbolTable::addDSOHandle(const MachHeaderSection *header) {
98   Symbol *s;
99   bool wasInserted;
100   std::tie(s, wasInserted) = insert(DSOHandle::name);
101   if (!wasInserted) {
102     if (auto *defined = dyn_cast<Defined>(s))
103       error("found defined symbol from " + defined->isec->file->getName() +
104             " with illegal name " + DSOHandle::name);
105   }
106   replaceSymbol<DSOHandle>(s, header);
107   return s;
108 }
109 
110 SymbolTable *macho::symtab;
111