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