]> git.saurik.com Git - apple/ld64.git/blob - ld64-134.9/src/ld/SymbolTable.h
451d0641fc32420bba3f4007c023afa63a7cca77
[apple/ld64.git] / ld64-134.9 / src / ld / SymbolTable.h
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
2 *
3 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #ifndef __SYMBOL_TABLE_H__
26 #define __SYMBOL_TABLE_H__
27
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32 #include <sys/sysctl.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <unistd.h>
37 #include <mach/mach_time.h>
38 #include <mach/vm_statistics.h>
39 #include <mach/mach_init.h>
40 #include <mach/mach_host.h>
41 #include <dlfcn.h>
42 #include <mach-o/dyld.h>
43
44 #include <vector>
45 #include <ext/hash_map>
46
47 #include "Options.h"
48 #include "ld.hpp"
49
50 namespace ld {
51 namespace tool {
52
53
54 class SymbolTable : public ld::IndirectBindingTable
55 {
56 public:
57 typedef uint32_t IndirectBindingSlot;
58
59 private:
60 class CStringEquals {
61 public:
62 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
63 };
64 typedef __gnu_cxx::hash_map<const char*, IndirectBindingSlot, __gnu_cxx::hash<const char*>, CStringEquals> NameToSlot;
65
66 class ContentFuncs {
67 public:
68 size_t operator()(const ld::Atom*) const;
69 bool operator()(const ld::Atom* left, const ld::Atom* right) const;
70 };
71 typedef __gnu_cxx::hash_map<const ld::Atom*, IndirectBindingSlot, ContentFuncs, ContentFuncs> ContentToSlot;
72
73 class ReferencesHashFuncs {
74 public:
75 size_t operator()(const ld::Atom*) const;
76 bool operator()(const ld::Atom* left, const ld::Atom* right) const;
77 };
78 typedef __gnu_cxx::hash_map<const ld::Atom*, IndirectBindingSlot, ReferencesHashFuncs, ReferencesHashFuncs> ReferencesToSlot;
79
80 class CStringHashFuncs {
81 public:
82 size_t operator()(const ld::Atom*) const;
83 bool operator()(const ld::Atom* left, const ld::Atom* right) const;
84 };
85 typedef __gnu_cxx::hash_map<const ld::Atom*, IndirectBindingSlot, CStringHashFuncs, CStringHashFuncs> CStringToSlot;
86
87 class UTF16StringHashFuncs {
88 public:
89 size_t operator()(const ld::Atom*) const;
90 bool operator()(const ld::Atom* left, const ld::Atom* right) const;
91 };
92 typedef __gnu_cxx::hash_map<const ld::Atom*, IndirectBindingSlot, UTF16StringHashFuncs, UTF16StringHashFuncs> UTF16StringToSlot;
93
94 typedef std::map<IndirectBindingSlot, const char*> SlotToName;
95 typedef __gnu_cxx::hash_map<const char*, CStringToSlot*, __gnu_cxx::hash<const char*>, CStringEquals> NameToMap;
96
97 typedef std::vector<const ld::Atom *> DuplicatedSymbolAtomList;
98 typedef std::map<const char *, DuplicatedSymbolAtomList * > DuplicateSymbols;
99
100 public:
101
102 class byNameIterator {
103 public:
104 byNameIterator& operator++(int) { ++_nameTableIterator; return *this; }
105 const ld::Atom* operator*() { return _slotTable[_nameTableIterator->second]; }
106 bool operator!=(const byNameIterator& lhs) { return _nameTableIterator != lhs._nameTableIterator; }
107
108 private:
109 friend class SymbolTable;
110 byNameIterator(NameToSlot::iterator it, std::vector<const ld::Atom*>& indirectTable)
111 : _nameTableIterator(it), _slotTable(indirectTable) {}
112
113 NameToSlot::iterator _nameTableIterator;
114 std::vector<const ld::Atom*>& _slotTable;
115 };
116
117 SymbolTable(const Options& opts, std::vector<const ld::Atom*>& ibt);
118
119 bool add(const ld::Atom& atom, bool ignoreDuplicates);
120 IndirectBindingSlot findSlotForName(const char* name);
121 IndirectBindingSlot findSlotForContent(const ld::Atom* atom, const ld::Atom** existingAtom);
122 IndirectBindingSlot findSlotForReferences(const ld::Atom* atom, const ld::Atom** existingAtom);
123 const ld::Atom* atomForSlot(IndirectBindingSlot s) { return _indirectBindingTable[s]; }
124 unsigned int updateCount() { return _indirectBindingTable.size(); }
125 void undefines(std::vector<const char*>& undefines);
126 void tentativeDefs(std::vector<const char*>& undefines);
127 bool hasName(const char* name);
128 bool hasExternalTentativeDefinitions() { return _hasExternalTentativeDefinitions; }
129 byNameIterator begin() { return byNameIterator(_byNameTable.begin(),_indirectBindingTable); }
130 byNameIterator end() { return byNameIterator(_byNameTable.end(),_indirectBindingTable); }
131 void printStatistics();
132
133 // from ld::IndirectBindingTable
134 virtual const char* indirectName(IndirectBindingSlot slot) const;
135 virtual const ld::Atom* indirectAtom(IndirectBindingSlot slot) const;
136
137 // Prints the duplicated symbols to stderr and throws. Only valid to call if hasDuplicateSymbols() returns true.
138 void checkDuplicateSymbols() const;
139
140
141 private:
142 bool addByName(const ld::Atom& atom, bool ignoreDuplicates);
143 bool addByContent(const ld::Atom& atom);
144 bool addByReferences(const ld::Atom& atom);
145 void markCoalescedAway(const ld::Atom* atom);
146
147 // Tracks duplicated symbols. Each call adds file to the list of files defining symbol.
148 // The file list is uniqued per symbol, so calling multiple times for the same symbol/file pair is permitted.
149 void addDuplicateSymbol(const char *symbol, const ld::Atom* atom);
150
151 const Options& _options;
152 NameToSlot _byNameTable;
153 SlotToName _byNameReverseTable;
154 ContentToSlot _literal4Table;
155 ContentToSlot _literal8Table;
156 ContentToSlot _literal16Table;
157 UTF16StringToSlot _utf16Table;
158 CStringToSlot _cstringTable;
159 NameToMap _nonStdCStringSectionToMap;
160 ReferencesToSlot _nonLazyPointerTable;
161 ReferencesToSlot _cfStringTable;
162 ReferencesToSlot _objc2ClassRefTable;
163 ReferencesToSlot _pointerToCStringTable;
164 std::vector<const ld::Atom*>& _indirectBindingTable;
165 bool _hasExternalTentativeDefinitions;
166
167 DuplicateSymbols _duplicateSymbols;
168
169 };
170
171 } // namespace tool
172 } // namespace ld
173
174
175 #endif // __SYMBOL_TABLE_H__