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 "ConcatOutputSection.h"
11 #include "Config.h"
12 #include "InputFiles.h"
13 #include "InputSection.h"
14 #include "Symbols.h"
15 #include "SyntheticSections.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "lld/Common/Memory.h"
18 
19 using namespace llvm;
20 using namespace lld;
21 using namespace lld::macho;
22 
23 Symbol *SymbolTable::find(CachedHashStringRef cachedName) {
24   auto it = symMap.find(cachedName);
25   if (it == symMap.end())
26     return nullptr;
27   return symVector[it->second];
28 }
29 
30 std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
31                                               const InputFile *file) {
32   auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
33 
34   Symbol *sym;
35   if (!p.second) {
36     // Name already present in the symbol table.
37     sym = symVector[p.first->second];
38   } else {
39     // Name is a new symbol.
40     sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
41     symVector.push_back(sym);
42   }
43 
44   sym->isUsedInRegularObj |= !file || isa<ObjFile>(file);
45   return {sym, p.second};
46 }
47 
48 Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
49                                  InputSection *isec, uint64_t value,
50                                  uint64_t size, bool isWeakDef,
51                                  bool isPrivateExtern, bool isThumb,
52                                  bool isReferencedDynamically, bool noDeadStrip,
53                                  bool isWeakDefCanBeHidden) {
54   Symbol *s;
55   bool wasInserted;
56   bool overridesWeakDef = false;
57   std::tie(s, wasInserted) = insert(name, file);
58 
59   assert(!isWeakDef || (isa<BitcodeFile>(file) && !isec) ||
60          (isa<ObjFile>(file) && file == isec->getFile()));
61 
62   if (!wasInserted) {
63     if (auto *defined = dyn_cast<Defined>(s)) {
64       if (isWeakDef) {
65         // See further comment in createDefined() in InputFiles.cpp
66         if (defined->isWeakDef()) {
67           defined->privateExtern &= isPrivateExtern;
68           defined->weakDefCanBeHidden &= isWeakDefCanBeHidden;
69           defined->referencedDynamically |= isReferencedDynamically;
70           defined->noDeadStrip |= noDeadStrip;
71         }
72         // FIXME: Handle this for bitcode files.
73         if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(isec))
74           concatIsec->wasCoalesced = true;
75         return defined;
76       }
77 
78       if (defined->isWeakDef()) {
79         // FIXME: Handle this for bitcode files.
80         if (auto concatIsec =
81                 dyn_cast_or_null<ConcatInputSection>(defined->isec)) {
82           concatIsec->wasCoalesced = true;
83           concatIsec->symbols.erase(llvm::find(concatIsec->symbols, defined));
84         }
85       } else {
86         error("duplicate symbol: " + name + "\n>>> defined in " +
87               toString(defined->getFile()) + "\n>>> defined in " +
88               toString(file));
89       }
90 
91     } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
92       overridesWeakDef = !isWeakDef && dysym->isWeakDef();
93       dysym->unreference();
94     }
95     // Defined symbols take priority over other types of symbols, so in case
96     // of a name conflict, we fall through to the replaceSymbol() call below.
97   }
98 
99   // With -flat_namespace, all extern symbols in dylibs are interposable.
100   // FIXME: Add support for `-interposable` (PR53680).
101   bool interposable = config->namespaceKind == NamespaceKind::flat &&
102                       config->outputType != MachO::MH_EXECUTE &&
103                       !isPrivateExtern;
104   Defined *defined = replaceSymbol<Defined>(
105       s, name, file, isec, value, size, isWeakDef, /*isExternal=*/true,
106       isPrivateExtern, isThumb, isReferencedDynamically, noDeadStrip,
107       overridesWeakDef, isWeakDefCanBeHidden, interposable);
108   return defined;
109 }
110 
111 Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file,
112                                   bool isWeakRef) {
113   Symbol *s;
114   bool wasInserted;
115   std::tie(s, wasInserted) = insert(name, file);
116 
117   RefState refState = isWeakRef ? RefState::Weak : RefState::Strong;
118 
119   if (wasInserted)
120     replaceSymbol<Undefined>(s, name, file, refState);
121   else if (auto *lazy = dyn_cast<LazyArchive>(s))
122     lazy->fetchArchiveMember();
123   else if (isa<LazyObject>(s))
124     extract(*s->getFile(), s->getName());
125   else if (auto *dynsym = dyn_cast<DylibSymbol>(s))
126     dynsym->reference(refState);
127   else if (auto *undefined = dyn_cast<Undefined>(s))
128     undefined->refState = std::max(undefined->refState, refState);
129   return s;
130 }
131 
132 Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t size,
133                                uint32_t align, bool isPrivateExtern) {
134   Symbol *s;
135   bool wasInserted;
136   std::tie(s, wasInserted) = insert(name, file);
137 
138   if (!wasInserted) {
139     if (auto *common = dyn_cast<CommonSymbol>(s)) {
140       if (size < common->size)
141         return s;
142     } else if (isa<Defined>(s)) {
143       return s;
144     }
145     // Common symbols take priority over all non-Defined symbols, so in case of
146     // a name conflict, we fall through to the replaceSymbol() call below.
147   }
148 
149   replaceSymbol<CommonSymbol>(s, name, file, size, align, isPrivateExtern);
150   return s;
151 }
152 
153 Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef,
154                               bool isTlv) {
155   Symbol *s;
156   bool wasInserted;
157   std::tie(s, wasInserted) = insert(name, file);
158 
159   RefState refState = RefState::Unreferenced;
160   if (!wasInserted) {
161     if (auto *defined = dyn_cast<Defined>(s)) {
162       if (isWeakDef && !defined->isWeakDef())
163         defined->overridesWeakDef = true;
164     } else if (auto *undefined = dyn_cast<Undefined>(s)) {
165       refState = undefined->refState;
166     } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
167       refState = dysym->getRefState();
168     }
169   }
170 
171   bool isDynamicLookup = file == nullptr;
172   if (wasInserted || isa<Undefined>(s) ||
173       (isa<DylibSymbol>(s) &&
174        ((!isWeakDef && s->isWeakDef()) ||
175         (!isDynamicLookup && cast<DylibSymbol>(s)->isDynamicLookup())))) {
176     if (auto *dynsym = dyn_cast<DylibSymbol>(s))
177       dynsym->unreference();
178     replaceSymbol<DylibSymbol>(s, file, name, isWeakDef, refState, isTlv);
179   }
180 
181   return s;
182 }
183 
184 Symbol *SymbolTable::addDynamicLookup(StringRef name) {
185   return addDylib(name, /*file=*/nullptr, /*isWeakDef=*/false, /*isTlv=*/false);
186 }
187 
188 Symbol *SymbolTable::addLazyArchive(StringRef name, ArchiveFile *file,
189                                     const object::Archive::Symbol &sym) {
190   Symbol *s;
191   bool wasInserted;
192   std::tie(s, wasInserted) = insert(name, file);
193 
194   if (wasInserted) {
195     replaceSymbol<LazyArchive>(s, file, sym);
196   } else if (isa<Undefined>(s)) {
197     file->fetch(sym);
198   } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
199     if (dysym->isWeakDef()) {
200       if (dysym->getRefState() != RefState::Unreferenced)
201         file->fetch(sym);
202       else
203         replaceSymbol<LazyArchive>(s, file, sym);
204     }
205   }
206   return s;
207 }
208 
209 Symbol *SymbolTable::addLazyObject(StringRef name, InputFile &file) {
210   Symbol *s;
211   bool wasInserted;
212   std::tie(s, wasInserted) = insert(name, &file);
213 
214   if (wasInserted) {
215     replaceSymbol<LazyObject>(s, file, name);
216   } else if (isa<Undefined>(s)) {
217     extract(file, name);
218   } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
219     if (dysym->isWeakDef()) {
220       if (dysym->getRefState() != RefState::Unreferenced)
221         extract(file, name);
222       else
223         replaceSymbol<LazyObject>(s, file, name);
224     }
225   }
226   return s;
227 }
228 
229 Defined *SymbolTable::addSynthetic(StringRef name, InputSection *isec,
230                                    uint64_t value, bool isPrivateExtern,
231                                    bool includeInSymtab,
232                                    bool referencedDynamically) {
233   Defined *s =
234       addDefined(name, nullptr, isec, value, /*size=*/0,
235                  /*isWeakDef=*/false, isPrivateExtern,
236                  /*isThumb=*/false, referencedDynamically,
237                  /*noDeadStrip=*/false, /*isWeakDefCanBeHidden=*/false);
238   s->includeInSymtab = includeInSymtab;
239   return s;
240 }
241 
242 enum class Boundary {
243   Start,
244   End,
245 };
246 
247 static Defined *createBoundarySymbol(const Undefined &sym) {
248   return symtab->addSynthetic(
249       sym.getName(), /*isec=*/nullptr, /*value=*/-1, /*isPrivateExtern=*/true,
250       /*includeInSymtab=*/false, /*referencedDynamically=*/false);
251 }
252 
253 static void handleSectionBoundarySymbol(const Undefined &sym, StringRef segSect,
254                                         Boundary which) {
255   StringRef segName, sectName;
256   std::tie(segName, sectName) = segSect.split('$');
257 
258   // Attach the symbol to any InputSection that will end up in the right
259   // OutputSection -- it doesn't matter which one we pick.
260   // Don't bother looking through inputSections for a matching
261   // ConcatInputSection -- we need to create ConcatInputSection for
262   // non-existing sections anyways, and that codepath works even if we should
263   // already have a ConcatInputSection with the right name.
264 
265   OutputSection *osec = nullptr;
266   // This looks for __TEXT,__cstring etc.
267   for (SyntheticSection *ssec : syntheticSections)
268     if (ssec->segname == segName && ssec->name == sectName) {
269       osec = ssec->isec->parent;
270       break;
271     }
272 
273   if (!osec) {
274     ConcatInputSection *isec = makeSyntheticInputSection(segName, sectName);
275 
276     // This runs after markLive() and is only called for Undefineds that are
277     // live. Marking the isec live ensures an OutputSection is created that the
278     // start/end symbol can refer to.
279     assert(sym.isLive());
280     isec->live = true;
281 
282     // This runs after gatherInputSections(), so need to explicitly set parent
283     // and add to inputSections.
284     osec = isec->parent = ConcatOutputSection::getOrCreateForInput(isec);
285     inputSections.push_back(isec);
286   }
287 
288   if (which == Boundary::Start)
289     osec->sectionStartSymbols.push_back(createBoundarySymbol(sym));
290   else
291     osec->sectionEndSymbols.push_back(createBoundarySymbol(sym));
292 }
293 
294 static void handleSegmentBoundarySymbol(const Undefined &sym, StringRef segName,
295                                         Boundary which) {
296   OutputSegment *seg = getOrCreateOutputSegment(segName);
297   if (which == Boundary::Start)
298     seg->segmentStartSymbols.push_back(createBoundarySymbol(sym));
299   else
300     seg->segmentEndSymbols.push_back(createBoundarySymbol(sym));
301 }
302 
303 void lld::macho::treatUndefinedSymbol(const Undefined &sym, StringRef source) {
304   // Handle start/end symbols.
305   StringRef name = sym.getName();
306   if (name.consume_front("section$start$"))
307     return handleSectionBoundarySymbol(sym, name, Boundary::Start);
308   if (name.consume_front("section$end$"))
309     return handleSectionBoundarySymbol(sym, name, Boundary::End);
310   if (name.consume_front("segment$start$"))
311     return handleSegmentBoundarySymbol(sym, name, Boundary::Start);
312   if (name.consume_front("segment$end$"))
313     return handleSegmentBoundarySymbol(sym, name, Boundary::End);
314 
315   // Handle -U.
316   if (config->explicitDynamicLookups.count(sym.getName())) {
317     symtab->addDynamicLookup(sym.getName());
318     return;
319   }
320 
321   // Handle -undefined.
322   auto message = [source, &sym]() {
323     std::string message = "undefined symbol";
324     if (config->archMultiple)
325       message += (" for arch " + getArchitectureName(config->arch())).str();
326     message += ": " + toString(sym);
327     if (!source.empty())
328       message += "\n>>> referenced by " + source.str();
329     else
330       message += "\n>>> referenced by " + toString(sym.getFile());
331     return message;
332   };
333   switch (config->undefinedSymbolTreatment) {
334   case UndefinedSymbolTreatment::error:
335     error(message());
336     break;
337   case UndefinedSymbolTreatment::warning:
338     warn(message());
339     LLVM_FALLTHROUGH;
340   case UndefinedSymbolTreatment::dynamic_lookup:
341   case UndefinedSymbolTreatment::suppress:
342     symtab->addDynamicLookup(sym.getName());
343     break;
344   case UndefinedSymbolTreatment::unknown:
345     llvm_unreachable("unknown -undefined TREATMENT");
346   }
347 }
348 
349 std::unique_ptr<SymbolTable> macho::symtab;
350