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#include "llvm/Demangle/Demangle.h"
19
20using namespace llvm;
21using namespace lld;
22using namespace lld::macho;
23
24Symbol *SymbolTable::find(CachedHashStringRef cachedName) {
25 auto it = symMap.find(Val: cachedName);
26 if (it == symMap.end())
27 return nullptr;
28 return symVector[it->second];
29}
30
31std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
32 const InputFile *file) {
33 auto p = symMap.insert(KV: {CachedHashStringRef(name), (int)symVector.size()});
34
35 Symbol *sym;
36 if (!p.second) {
37 // Name already present in the symbol table.
38 sym = symVector[p.first->second];
39 } else {
40 // Name is a new symbol.
41 sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
42 symVector.push_back(x: sym);
43 }
44
45 sym->isUsedInRegularObj |= !file || isa<ObjFile>(Val: file);
46 return {sym, p.second};
47}
48
49namespace {
50struct DuplicateSymbolDiag {
51 // Pair containing source location and source file
52 const std::pair<std::string, std::string> src1;
53 const std::pair<std::string, std::string> src2;
54 const Symbol *sym;
55
56 DuplicateSymbolDiag(const std::pair<std::string, std::string> src1,
57 const std::pair<std::string, std::string> src2,
58 const Symbol *sym)
59 : src1(src1), src2(src2), sym(sym) {}
60};
61SmallVector<DuplicateSymbolDiag> dupSymDiags;
62} // namespace
63
64// Move local symbols at \p fromOff in \p fromIsec into \p toIsec, unless that
65// symbol is \p skip, in which case we just remove it.
66static void transplantSymbolsAtOffset(InputSection *fromIsec,
67 InputSection *toIsec, Defined *skip,
68 uint64_t fromOff, uint64_t toOff) {
69 // Ensure the symbols will still be in address order after our insertions.
70 auto symSucceedsOff = [](uint64_t off, const Symbol *s) {
71 return cast<Defined>(Val: s)->value > off;
72 };
73 assert(std::is_partitioned(toIsec->symbols.begin(), toIsec->symbols.end(),
74 [symSucceedsOff, toOff](const Symbol *s) {
75 return !symSucceedsOff(toOff, s);
76 }) &&
77 "Symbols in toIsec must be partitioned by toOff.");
78 auto insertIt = llvm::upper_bound(Range&: toIsec->symbols, Value&: toOff, C: symSucceedsOff);
79 llvm::erase_if(C&: fromIsec->symbols, P: [&](Symbol *s) {
80 auto *d = cast<Defined>(Val: s);
81 if (d == skip)
82 return true;
83 if (d->value != fromOff || d->isExternal())
84 return false;
85
86 // This repeated insertion will be quadratic unless insertIt is the end
87 // iterator. However, that is typically the case for files that have
88 // .subsections_via_symbols set.
89 insertIt = toIsec->symbols.insert(I: insertIt, Elt: d);
90 d->originalIsec = toIsec;
91 d->value = toOff;
92 // We don't want to have more than one unwindEntry at a given address, so
93 // drop the redundant ones. We can safely drop the unwindEntries of the
94 // symbols in fromIsec since we will be adding another unwindEntry as we
95 // finish parsing toIsec's file. (We can assume that toIsec has its own
96 // unwindEntry because of the ODR.)
97 d->originalUnwindEntry = nullptr;
98 return true;
99 });
100}
101
102Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
103 InputSection *isec, uint64_t value,
104 uint64_t size, bool isWeakDef,
105 bool isPrivateExtern,
106 bool isReferencedDynamically, bool noDeadStrip,
107 bool isWeakDefCanBeHidden) {
108 bool overridesWeakDef = false;
109 auto [s, wasInserted] = insert(name, file);
110
111 assert(!file || !isa<BitcodeFile>(file) || !isec);
112
113 if (!wasInserted) {
114 if (auto *defined = dyn_cast<Defined>(Val: s)) {
115 if (isWeakDef) {
116 // See further comment in createDefined() in InputFiles.cpp
117 if (defined->isWeakDef()) {
118 defined->privateExtern &= isPrivateExtern;
119 defined->weakDefCanBeHidden &= isWeakDefCanBeHidden;
120 defined->referencedDynamically |= isReferencedDynamically;
121 defined->noDeadStrip |= noDeadStrip;
122 }
123 if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(Val: isec)) {
124 concatIsec->wasCoalesced = true;
125 // Any local symbols that alias the coalesced symbol should be moved
126 // into the prevailing section. Note that we have sorted the symbols
127 // in ObjFile::parseSymbols() such that extern weak symbols appear
128 // last, so we don't need to worry about subsequent symbols being
129 // added to an already-coalesced section.
130 if (defined->isec())
131 transplantSymbolsAtOffset(fromIsec: concatIsec, toIsec: defined->isec(),
132 /*skip=*/nullptr, fromOff: value, toOff: defined->value);
133 }
134 return defined;
135 }
136
137 if (defined->isWeakDef()) {
138 if (auto concatIsec =
139 dyn_cast_or_null<ConcatInputSection>(Val: defined->isec())) {
140 concatIsec->wasCoalesced = true;
141 if (isec)
142 transplantSymbolsAtOffset(fromIsec: concatIsec, toIsec: isec, skip: defined, fromOff: defined->value,
143 toOff: value);
144 }
145 } else {
146 std::string srcLoc1 = defined->getSourceLocation();
147 std::string srcLoc2 = isec ? isec->getSourceLocation(off: value) : "";
148 std::string srcFile1 = toString(file: defined->getFile());
149 std::string srcFile2 = toString(file);
150
151 dupSymDiags.push_back(Elt: {make_pair(x&: srcLoc1, y&: srcFile1),
152 make_pair(x&: srcLoc2, y&: srcFile2), defined});
153 }
154
155 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: s)) {
156 overridesWeakDef = !isWeakDef && dysym->isWeakDef();
157 dysym->unreference();
158 } else if (auto *undef = dyn_cast<Undefined>(Val: s)) {
159 if (undef->wasBitcodeSymbol) {
160 auto objFile = dyn_cast<ObjFile>(Val: file);
161 if (!objFile) {
162 // The file must be a native object file, as opposed to potentially
163 // being another bitcode file. A situation arises when some symbols
164 // are defined thru `module asm` and thus they are not present in the
165 // bitcode's symbol table. Consider bitcode modules `A`, `B`, and `C`.
166 // LTO compiles only `A` and `C`, since there's no explicit symbol
167 // reference to `B` other than a symbol from `A` via `module asm`.
168 // After LTO is finished, the missing symbol now appears in the
169 // resulting object file for `A`, which prematurely resolves another
170 // prevailing symbol with `B` that hasn't been compiled, instead of
171 // the resulting object for `C`. Consequently, an incorrect
172 // relocation is generated for the prevailing symbol.
173 assert(isa<BitcodeFile>(file) && "Bitcode file is expected.");
174 std::string message =
175 "The pending prevailing symbol(" + name.str() +
176 ") in the bitcode file(" + toString(file: undef->getFile()) +
177 ") is overridden by a non-native object (from bitcode): " +
178 toString(file);
179 error(msg: message);
180 } else if (!objFile->builtFromBitcode) {
181 // Ideally, this should be an object file compiled from a bitcode
182 // file. However, this might not hold true if a LC linker option is
183 // used. In case LTO internalizes a prevailing hidden weak symbol,
184 // there's a situation where an unresolved prevailing symbol might be
185 // linked with the corresponding one from a native library, which is
186 // loaded later after LTO. Although this could potentially result in
187 // an ODR violation, we choose to permit this scenario as a warning.
188 std::string message = "The pending prevailing symbol(" + name.str() +
189 ") in the bitcode file(" +
190 toString(file: undef->getFile()) +
191 ") is overridden by a post-processed native "
192 "object (from native archive): " +
193 toString(file);
194 warn(msg: message);
195 } else {
196 // Preserve the original bitcode file name (instead of using the
197 // object file name).
198 file = undef->getFile();
199 }
200 }
201 }
202 // Defined symbols take priority over other types of symbols, so in case
203 // of a name conflict, we fall through to the replaceSymbol() call below.
204 }
205
206 // With -flat_namespace, all extern symbols in dylibs are interposable.
207 bool interposable = ((config->namespaceKind == NamespaceKind::flat &&
208 config->outputType != MachO::MH_EXECUTE) ||
209 config->interposable) &&
210 !isPrivateExtern;
211 Defined *defined = replaceSymbol<Defined>(
212 s, arg&: name, arg&: file, arg&: isec, arg&: value, arg&: size, arg&: isWeakDef, /*isExternal=*/arg: true,
213 arg&: isPrivateExtern, /*includeInSymtab=*/arg: true, arg&: isReferencedDynamically,
214 arg&: noDeadStrip, arg&: overridesWeakDef, arg&: isWeakDefCanBeHidden, arg&: interposable);
215 return defined;
216}
217
218Defined *SymbolTable::aliasDefined(Defined *src, StringRef target,
219 InputFile *newFile, bool makePrivateExtern) {
220 bool isPrivateExtern = makePrivateExtern || src->privateExtern;
221 return addDefined(name: target, file: newFile, isec: src->isec(), value: src->value, size: src->size,
222 isWeakDef: src->isWeakDef(), isPrivateExtern,
223 isReferencedDynamically: src->referencedDynamically, noDeadStrip: src->noDeadStrip,
224 isWeakDefCanBeHidden: src->weakDefCanBeHidden);
225}
226
227Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file,
228 bool isWeakRef) {
229 auto [s, wasInserted] = insert(name, file);
230
231 RefState refState = isWeakRef ? RefState::Weak : RefState::Strong;
232
233 if (wasInserted)
234 replaceSymbol<Undefined>(s, arg&: name, arg&: file, arg&: refState,
235 /*wasBitcodeSymbol=*/arg: false);
236 else if (auto *lazy = dyn_cast<LazyArchive>(Val: s))
237 lazy->fetchArchiveMember();
238 else if (isa<LazyObject>(Val: s))
239 extract(file&: *s->getFile(), reason: s->getName());
240 else if (auto *dynsym = dyn_cast<DylibSymbol>(Val: s))
241 dynsym->reference(newState: refState);
242 else if (auto *undefined = dyn_cast<Undefined>(Val: s))
243 undefined->refState = std::max(a: undefined->refState, b: refState);
244 return s;
245}
246
247Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t size,
248 uint32_t align, bool isPrivateExtern) {
249 auto [s, wasInserted] = insert(name, file);
250
251 if (!wasInserted) {
252 if (auto *common = dyn_cast<CommonSymbol>(Val: s)) {
253 if (size < common->size)
254 return s;
255 } else if (isa<Defined>(Val: s)) {
256 return s;
257 }
258 // Common symbols take priority over all non-Defined symbols, so in case of
259 // a name conflict, we fall through to the replaceSymbol() call below.
260 }
261
262 replaceSymbol<CommonSymbol>(s, arg&: name, arg&: file, arg&: size, arg&: align, arg&: isPrivateExtern);
263 return s;
264}
265
266Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef,
267 bool isTlv) {
268 auto [s, wasInserted] = insert(name, file);
269
270 RefState refState = RefState::Unreferenced;
271 if (!wasInserted) {
272 if (auto *defined = dyn_cast<Defined>(Val: s)) {
273 if (isWeakDef && !defined->isWeakDef())
274 defined->overridesWeakDef = true;
275 } else if (auto *undefined = dyn_cast<Undefined>(Val: s)) {
276 refState = undefined->refState;
277 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: s)) {
278 refState = dysym->getRefState();
279 }
280 }
281
282 bool isDynamicLookup = file == nullptr;
283 if (wasInserted || isa<Undefined>(Val: s) ||
284 (isa<DylibSymbol>(Val: s) &&
285 ((!isWeakDef && s->isWeakDef()) ||
286 (!isDynamicLookup && cast<DylibSymbol>(Val: s)->isDynamicLookup())))) {
287 if (auto *dynsym = dyn_cast<DylibSymbol>(Val: s))
288 dynsym->unreference();
289 replaceSymbol<DylibSymbol>(s, arg&: file, arg&: name, arg&: isWeakDef, arg&: refState, arg&: isTlv);
290 }
291
292 return s;
293}
294
295Symbol *SymbolTable::addDynamicLookup(StringRef name) {
296 return addDylib(name, /*file=*/nullptr, /*isWeakDef=*/false, /*isTlv=*/false);
297}
298
299Symbol *SymbolTable::addLazyArchive(StringRef name, ArchiveFile *file,
300 const object::Archive::Symbol &sym) {
301 auto [s, wasInserted] = insert(name, file);
302
303 if (wasInserted) {
304 replaceSymbol<LazyArchive>(s, arg&: file, arg: sym);
305 } else if (isa<Undefined>(Val: s)) {
306 file->fetch(sym);
307 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: s)) {
308 if (dysym->isWeakDef()) {
309 if (dysym->getRefState() != RefState::Unreferenced)
310 file->fetch(sym);
311 else
312 replaceSymbol<LazyArchive>(s, arg&: file, arg: sym);
313 }
314 }
315 return s;
316}
317
318Symbol *SymbolTable::addLazyObject(StringRef name, InputFile &file) {
319 auto [s, wasInserted] = insert(name, file: &file);
320
321 if (wasInserted) {
322 replaceSymbol<LazyObject>(s, arg&: file, arg&: name);
323 } else if (isa<Undefined>(Val: s)) {
324 extract(file, reason: name);
325 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: s)) {
326 if (dysym->isWeakDef()) {
327 if (dysym->getRefState() != RefState::Unreferenced)
328 extract(file, reason: name);
329 else
330 replaceSymbol<LazyObject>(s, arg&: file, arg&: name);
331 }
332 }
333 return s;
334}
335
336Defined *SymbolTable::addSynthetic(StringRef name, InputSection *isec,
337 uint64_t value, bool isPrivateExtern,
338 bool includeInSymtab,
339 bool referencedDynamically) {
340 assert(!isec || !isec->getFile()); // See makeSyntheticInputSection().
341 Defined *s = addDefined(name, /*file=*/nullptr, isec, value, /*size=*/0,
342 /*isWeakDef=*/false, isPrivateExtern,
343 isReferencedDynamically: referencedDynamically, /*noDeadStrip=*/false,
344 /*isWeakDefCanBeHidden=*/false);
345 s->includeInSymtab = includeInSymtab;
346 return s;
347}
348
349enum class Boundary {
350 Start,
351 End,
352};
353
354static Defined *createBoundarySymbol(const Undefined &sym) {
355 return symtab->addSynthetic(
356 name: sym.getName(), /*isec=*/nullptr, /*value=*/-1, /*isPrivateExtern=*/true,
357 /*includeInSymtab=*/false, /*referencedDynamically=*/false);
358}
359
360static void handleSectionBoundarySymbol(const Undefined &sym, StringRef segSect,
361 Boundary which) {
362 auto [segName, sectName] = segSect.split(Separator: '$');
363
364 // Attach the symbol to any InputSection that will end up in the right
365 // OutputSection -- it doesn't matter which one we pick.
366 // Don't bother looking through inputSections for a matching
367 // ConcatInputSection -- we need to create ConcatInputSection for
368 // non-existing sections anyways, and that codepath works even if we should
369 // already have a ConcatInputSection with the right name.
370
371 OutputSection *osec = nullptr;
372 // This looks for __TEXT,__cstring etc.
373 for (SyntheticSection *ssec : syntheticSections)
374 if (ssec->segname == segName && ssec->name == sectName) {
375 osec = ssec->isec->parent;
376 break;
377 }
378
379 if (!osec) {
380 ConcatInputSection *isec = makeSyntheticInputSection(segName, sectName);
381
382 // This runs after markLive() and is only called for Undefineds that are
383 // live. Marking the isec live ensures an OutputSection is created that the
384 // start/end symbol can refer to.
385 assert(sym.isLive());
386 assert(isec->live);
387
388 // This runs after gatherInputSections(), so need to explicitly set parent
389 // and add to inputSections.
390 osec = isec->parent = ConcatOutputSection::getOrCreateForInput(isec);
391 inputSections.push_back(x: isec);
392 }
393
394 if (which == Boundary::Start)
395 osec->sectionStartSymbols.push_back(NewVal: createBoundarySymbol(sym));
396 else
397 osec->sectionEndSymbols.push_back(NewVal: createBoundarySymbol(sym));
398}
399
400static void handleSegmentBoundarySymbol(const Undefined &sym, StringRef segName,
401 Boundary which) {
402 OutputSegment *seg = getOrCreateOutputSegment(name: segName);
403 if (which == Boundary::Start)
404 seg->segmentStartSymbols.push_back(NewVal: createBoundarySymbol(sym));
405 else
406 seg->segmentEndSymbols.push_back(NewVal: createBoundarySymbol(sym));
407}
408
409// Try to find a definition for an undefined symbol.
410// Returns true if a definition was found and no diagnostics are needed.
411static bool recoverFromUndefinedSymbol(const Undefined &sym) {
412 // Handle start/end symbols.
413 StringRef name = sym.getName();
414 if (name.consume_front(Prefix: "section$start$")) {
415 handleSectionBoundarySymbol(sym, segSect: name, which: Boundary::Start);
416 return true;
417 }
418 if (name.consume_front(Prefix: "section$end$")) {
419 handleSectionBoundarySymbol(sym, segSect: name, which: Boundary::End);
420 return true;
421 }
422 if (name.consume_front(Prefix: "segment$start$")) {
423 handleSegmentBoundarySymbol(sym, segName: name, which: Boundary::Start);
424 return true;
425 }
426 if (name.consume_front(Prefix: "segment$end$")) {
427 handleSegmentBoundarySymbol(sym, segName: name, which: Boundary::End);
428 return true;
429 }
430
431 // Leave dtrace symbols, since we will handle them when we do the relocation
432 if (name.starts_with(Prefix: "___dtrace_"))
433 return true;
434
435 // Handle -U.
436 if (config->explicitDynamicLookups.contains(key: sym.getName())) {
437 symtab->addDynamicLookup(name: sym.getName());
438 return true;
439 }
440
441 // Handle -undefined.
442 if (config->undefinedSymbolTreatment ==
443 UndefinedSymbolTreatment::dynamic_lookup ||
444 config->undefinedSymbolTreatment == UndefinedSymbolTreatment::suppress) {
445 symtab->addDynamicLookup(name: sym.getName());
446 return true;
447 }
448
449 // We do not return true here, as we still need to print diagnostics.
450 if (config->undefinedSymbolTreatment == UndefinedSymbolTreatment::warning)
451 symtab->addDynamicLookup(name: sym.getName());
452
453 return false;
454}
455
456namespace {
457struct UndefinedDiag {
458 struct SectionAndOffset {
459 const InputSection *isec;
460 uint64_t offset;
461 };
462
463 std::vector<SectionAndOffset> codeReferences;
464 std::vector<std::string> otherReferences;
465};
466
467MapVector<const Undefined *, UndefinedDiag> undefs;
468} // namespace
469
470void macho::reportPendingDuplicateSymbols() {
471 for (const auto &duplicate : dupSymDiags) {
472 if (!config->deadStripDuplicates || duplicate.sym->isLive()) {
473 std::string message =
474 "duplicate symbol: " + toString(*duplicate.sym) + "\n>>> defined in ";
475 if (!duplicate.src1.first.empty())
476 message += duplicate.src1.first + "\n>>> ";
477 message += duplicate.src1.second + "\n>>> defined in ";
478 if (!duplicate.src2.first.empty())
479 message += duplicate.src2.first + "\n>>> ";
480 error(msg: message + duplicate.src2.second);
481 }
482 }
483}
484
485// Check whether the definition name def is a mangled function name that matches
486// the reference name ref.
487static bool canSuggestExternCForCXX(StringRef ref, StringRef def) {
488 llvm::ItaniumPartialDemangler d;
489 std::string name = def.str();
490 if (d.partialDemangle(MangledName: name.c_str()))
491 return false;
492 char *buf = d.getFunctionName(Buf: nullptr, N: nullptr);
493 if (!buf)
494 return false;
495 bool ret = ref == buf;
496 free(ptr: buf);
497 return ret;
498}
499
500// Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns
501// the suggested symbol, which is either in the symbol table, or in the same
502// file of sym.
503static const Symbol *getAlternativeSpelling(const Undefined &sym,
504 std::string &preHint,
505 std::string &postHint) {
506 DenseMap<StringRef, const Symbol *> map;
507 if (sym.getFile() && sym.getFile()->kind() == InputFile::ObjKind) {
508 // Build a map of local defined symbols.
509 for (const Symbol *s : sym.getFile()->symbols)
510 if (auto *defined = dyn_cast_or_null<Defined>(Val: s))
511 if (!defined->isExternal())
512 map.try_emplace(Key: s->getName(), Args&: s);
513 }
514
515 auto suggest = [&](StringRef newName) -> const Symbol * {
516 // If defined locally.
517 if (const Symbol *s = map.lookup(Val: newName))
518 return s;
519
520 // If in the symbol table and not undefined.
521 if (const Symbol *s = symtab->find(name: newName))
522 if (!isa<Undefined>(Val: s))
523 return s;
524
525 return nullptr;
526 };
527
528 // This loop enumerates all strings of Levenshtein distance 1 as typo
529 // correction candidates and suggests the one that exists as a non-undefined
530 // symbol.
531 StringRef name = sym.getName();
532 for (size_t i = 0, e = name.size(); i != e + 1; ++i) {
533 // Insert a character before name[i].
534 std::string newName = (name.substr(Start: 0, N: i) + "0" + name.substr(Start: i)).str();
535 for (char c = '0'; c <= 'z'; ++c) {
536 newName[i] = c;
537 if (const Symbol *s = suggest(newName))
538 return s;
539 }
540 if (i == e)
541 break;
542
543 // Substitute name[i].
544 newName = std::string(name);
545 for (char c = '0'; c <= 'z'; ++c) {
546 newName[i] = c;
547 if (const Symbol *s = suggest(newName))
548 return s;
549 }
550
551 // Transpose name[i] and name[i+1]. This is of edit distance 2 but it is
552 // common.
553 if (i + 1 < e) {
554 newName[i] = name[i + 1];
555 newName[i + 1] = name[i];
556 if (const Symbol *s = suggest(newName))
557 return s;
558 }
559
560 // Delete name[i].
561 newName = (name.substr(Start: 0, N: i) + name.substr(Start: i + 1)).str();
562 if (const Symbol *s = suggest(newName))
563 return s;
564 }
565
566 // Case mismatch, e.g. Foo vs FOO.
567 for (auto &it : map)
568 if (name.equals_insensitive(RHS: it.first))
569 return it.second;
570 for (Symbol *sym : symtab->getSymbols())
571 if (!isa<Undefined>(Val: sym) && name.equals_insensitive(RHS: sym->getName()))
572 return sym;
573
574 // The reference may be a mangled name while the definition is not. Suggest a
575 // missing extern "C".
576 if (name.starts_with(Prefix: "__Z")) {
577 std::string buf = name.str();
578 llvm::ItaniumPartialDemangler d;
579 if (!d.partialDemangle(MangledName: buf.c_str()))
580 if (char *buf = d.getFunctionName(Buf: nullptr, N: nullptr)) {
581 const Symbol *s = suggest((Twine("_") + buf).str());
582 free(ptr: buf);
583 if (s) {
584 preHint = ": extern \"C\" ";
585 return s;
586 }
587 }
588 } else {
589 StringRef nameWithoutUnderscore = name;
590 nameWithoutUnderscore.consume_front(Prefix: "_");
591 const Symbol *s = nullptr;
592 for (auto &it : map)
593 if (canSuggestExternCForCXX(ref: nameWithoutUnderscore, def: it.first)) {
594 s = it.second;
595 break;
596 }
597 if (!s)
598 for (Symbol *sym : symtab->getSymbols())
599 if (canSuggestExternCForCXX(ref: nameWithoutUnderscore, def: sym->getName())) {
600 s = sym;
601 break;
602 }
603 if (s) {
604 preHint = " to declare ";
605 postHint = " as extern \"C\"?";
606 return s;
607 }
608 }
609
610 return nullptr;
611}
612
613static void reportUndefinedSymbol(const Undefined &sym,
614 const UndefinedDiag &locations,
615 bool correctSpelling) {
616 std::string message = "undefined symbol";
617 if (config->archMultiple)
618 message += (" for arch " + getArchitectureName(Arch: config->arch())).str();
619 message += ": " + toString(sym);
620
621 const size_t maxUndefinedReferences = 3;
622 size_t i = 0;
623 for (const std::string &loc : locations.otherReferences) {
624 if (i >= maxUndefinedReferences)
625 break;
626 message += "\n>>> referenced by " + loc;
627 ++i;
628 }
629
630 for (const UndefinedDiag::SectionAndOffset &loc : locations.codeReferences) {
631 if (i >= maxUndefinedReferences)
632 break;
633 message += "\n>>> referenced by ";
634 std::string src = loc.isec->getSourceLocation(off: loc.offset);
635 if (!src.empty())
636 message += src + "\n>>> ";
637 message += loc.isec->getLocation(off: loc.offset);
638 ++i;
639 }
640
641 size_t totalReferences =
642 locations.otherReferences.size() + locations.codeReferences.size();
643 if (totalReferences > i)
644 message +=
645 ("\n>>> referenced " + Twine(totalReferences - i) + " more times")
646 .str();
647
648 if (correctSpelling) {
649 std::string preHint = ": ", postHint;
650 if (const Symbol *corrected =
651 getAlternativeSpelling(sym, preHint, postHint)) {
652 message +=
653 "\n>>> did you mean" + preHint + toString(*corrected) + postHint;
654 if (corrected->getFile())
655 message += "\n>>> defined in: " + toString(file: corrected->getFile());
656 }
657 }
658
659 if (config->undefinedSymbolTreatment == UndefinedSymbolTreatment::error)
660 error(msg: message);
661 else if (config->undefinedSymbolTreatment ==
662 UndefinedSymbolTreatment::warning)
663 warn(msg: message);
664 else
665 assert(false && "diagnostics make sense for -undefined error|warning only");
666}
667
668void macho::reportPendingUndefinedSymbols() {
669 // Enable spell corrector for the first 2 diagnostics.
670 for (const auto &[i, undef] : llvm::enumerate(First&: undefs))
671 reportUndefinedSymbol(sym: *undef.first, locations: undef.second, correctSpelling: i < 2);
672
673 // This function is called multiple times during execution. Clear the printed
674 // diagnostics to avoid printing the same things again the next time.
675 undefs.clear();
676}
677
678void macho::treatUndefinedSymbol(const Undefined &sym, StringRef source) {
679 if (recoverFromUndefinedSymbol(sym))
680 return;
681
682 undefs[&sym].otherReferences.push_back(x: source.str());
683}
684
685void macho::treatUndefinedSymbol(const Undefined &sym, const InputSection *isec,
686 uint64_t offset) {
687 if (recoverFromUndefinedSymbol(sym))
688 return;
689
690 undefs[&sym].codeReferences.push_back(x: {.isec: isec, .offset: offset});
691}
692
693std::unique_ptr<SymbolTable> macho::symtab;
694