xref: /freebsd/contrib/llvm-project/lld/MachO/SymbolTable.h (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 //===- SymbolTable.h --------------------------------------------*- C++ -*-===//
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 #ifndef LLD_MACHO_SYMBOL_TABLE_H
10 #define LLD_MACHO_SYMBOL_TABLE_H
11 
12 #include "Symbols.h"
13 
14 #include "lld/Common/LLVM.h"
15 #include "llvm/ADT/CachedHashString.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/Object/Archive.h"
18 
19 namespace lld::macho {
20 
21 class ArchiveFile;
22 class DylibFile;
23 class InputFile;
24 class ObjFile;
25 class InputSection;
26 class MachHeaderSection;
27 class Symbol;
28 class Defined;
29 class Undefined;
30 
31 /*
32  * Note that the SymbolTable handles name collisions by calling
33  * replaceSymbol(), which does an in-place update of the Symbol via `placement
34  * new`. Therefore, there is no need to update any relocations that hold
35  * pointers the "old" Symbol -- they will automatically point to the new one.
36  */
37 class SymbolTable {
38 public:
39   Defined *addDefined(StringRef name, InputFile *, InputSection *,
40                       uint64_t value, uint64_t size, bool isWeakDef,
41                       bool isPrivateExtern, bool isReferencedDynamically,
42                       bool noDeadStrip, bool isWeakDefCanBeHidden);
43 
44   Defined *aliasDefined(Defined *src, StringRef target, InputFile *newFile,
45                         bool makePrivateExtern = false);
46 
47   Symbol *addUndefined(StringRef name, InputFile *, bool isWeakRef);
48 
49   Symbol *addCommon(StringRef name, InputFile *, uint64_t size, uint32_t align,
50                     bool isPrivateExtern);
51 
52   Symbol *addDylib(StringRef name, DylibFile *file, bool isWeakDef, bool isTlv);
53   Symbol *addDynamicLookup(StringRef name);
54 
55   Symbol *addLazyArchive(StringRef name, ArchiveFile *file,
56                          const llvm::object::Archive::Symbol &sym);
57   Symbol *addLazyObject(StringRef name, InputFile &file);
58 
59   Defined *addSynthetic(StringRef name, InputSection *, uint64_t value,
60                         bool isPrivateExtern, bool includeInSymtab,
61                         bool referencedDynamically);
62 
63   ArrayRef<Symbol *> getSymbols() const { return symVector; }
64   Symbol *find(llvm::CachedHashStringRef name);
65   Symbol *find(StringRef name) { return find(llvm::CachedHashStringRef(name)); }
66 
67 private:
68   std::pair<Symbol *, bool> insert(StringRef name, const InputFile *);
69   llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
70   std::vector<Symbol *> symVector;
71 };
72 
73 void reportPendingUndefinedSymbols();
74 void reportPendingDuplicateSymbols();
75 
76 // Call reportPendingUndefinedSymbols() to emit diagnostics.
77 void treatUndefinedSymbol(const Undefined &, StringRef source);
78 void treatUndefinedSymbol(const Undefined &, const InputSection *,
79                           uint64_t offset);
80 
81 extern std::unique_ptr<SymbolTable> symtab;
82 
83 } // namespace lld::macho
84 
85 #endif
86