blob: d9cc868cea77a1b7cc6f80fec5d7c5d1dab0d314 [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001//===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lld/Core/SymbolTable.h"
Nick Kledzik6bc04c62012-02-22 21:56:59 +000011#include "lld/Core/AbsoluteAtom.h"
Michael J. Spencer4586fbc2013-01-22 20:49:42 +000012#include "lld/Core/Atom.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000013#include "lld/Core/DefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000014#include "lld/Core/File.h"
Michael J. Spencere6203a52012-04-03 18:39:40 +000015#include "lld/Core/LLVM.h"
Shankar Easwaran2b67fca2014-10-18 05:33:55 +000016#include "lld/Core/LinkingContext.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000017#include "lld/Core/Resolver.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000018#include "lld/Core/SharedLibraryAtom.h"
19#include "lld/Core/UndefinedAtom.h"
Nick Kledzikbfedfc12012-01-09 20:18:15 +000020#include "llvm/ADT/ArrayRef.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000021#include "llvm/ADT/DenseMapInfo.h"
Michael J. Spencer67f25272013-03-20 22:18:22 +000022#include "llvm/ADT/Hashing.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000023#include "llvm/Support/ErrorHandling.h"
Nick Kledzikbb963df2012-04-18 21:55:06 +000024#include "llvm/Support/raw_ostream.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000025#include <algorithm>
26#include <cassert>
Michael J. Spencercfd029f2012-03-28 19:04:02 +000027#include <cstdlib>
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000028#include <vector>
29
30namespace lld {
Simon Atanasyan07000872014-09-08 09:43:38 +000031SymbolTable::SymbolTable(LinkingContext &context) : _context(context) {}
Nick Kledzik38eec3d2011-12-22 02:38:01 +000032
Rui Ueyama2a522512014-05-14 17:29:27 +000033bool SymbolTable::add(const UndefinedAtom &atom) { return addByName(atom); }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000034
Rui Ueyama2a522512014-05-14 17:29:27 +000035bool SymbolTable::add(const SharedLibraryAtom &atom) { return addByName(atom); }
Michael J. Spencer765792d2012-04-03 18:40:27 +000036
Rui Ueyama2a522512014-05-14 17:29:27 +000037bool SymbolTable::add(const AbsoluteAtom &atom) { return addByName(atom); }
Nick Kledzik6bc04c62012-02-22 21:56:59 +000038
Rui Ueyama2a522512014-05-14 17:29:27 +000039bool SymbolTable::add(const DefinedAtom &atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +000040 if (!atom.name().empty() &&
Rui Ueyama8b08c372013-11-25 17:09:29 +000041 atom.scope() != DefinedAtom::scopeTranslationUnit) {
Nick Kledzik233f5372013-01-15 00:17:57 +000042 // Named atoms cannot be merged by content.
43 assert(atom.merge() != DefinedAtom::mergeByContent);
44 // Track named atoms that are not scoped to file (static).
Rui Ueyama2a522512014-05-14 17:29:27 +000045 return addByName(atom);
Rui Ueyama8b08c372013-11-25 17:09:29 +000046 }
47 if (atom.merge() == DefinedAtom::mergeByContent) {
Nick Kledzik233f5372013-01-15 00:17:57 +000048 // Named atoms cannot be merged by content.
49 assert(atom.name().empty());
Nick Kledzik388f3d02014-05-28 01:16:35 +000050 // Currently only read-only constants can be merged.
51 if (atom.permissions() == DefinedAtom::permR__)
52 return addByContent(atom);
53 // TODO: support mergeByContent of data atoms by comparing content & fixups.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000054 }
Rui Ueyama2a522512014-05-14 17:29:27 +000055 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000056}
57
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +000058const Atom *SymbolTable::findGroup(StringRef sym) {
59 NameToAtom::iterator pos = _groupTable.find(sym);
60 if (pos == _groupTable.end())
61 return nullptr;
62 return pos->second;
63}
64
65bool SymbolTable::addGroup(const DefinedAtom &da) {
66 StringRef name = da.name();
67 assert(!name.empty());
68 const Atom *existing = findGroup(name);
69 if (existing == nullptr) {
70 _groupTable[name] = &da;
71 return true;
72 }
73 _replacedAtoms[&da] = existing;
74 return false;
75}
76
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000077enum NameCollisionResolution {
78 NCR_First,
79 NCR_Second,
Nick Kledzik6bc04c62012-02-22 21:56:59 +000080 NCR_DupDef,
81 NCR_DupUndef,
82 NCR_DupShLib,
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000083 NCR_Error
84};
85
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000086static NameCollisionResolution cases[4][4] = {
87 //regular absolute undef sharedLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000088 {
89 // first is regular
Nick Kledzik6bc04c62012-02-22 21:56:59 +000090 NCR_DupDef, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000091 },
92 {
93 // first is absolute
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000094 NCR_Error, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000095 },
96 {
97 // first is undef
Nick Kledzik6bc04c62012-02-22 21:56:59 +000098 NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000099 },
100 {
101 // first is sharedLib
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000102 NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000103 }
104};
105
106static NameCollisionResolution collide(Atom::Definition first,
107 Atom::Definition second) {
108 return cases[first][second];
109}
110
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000111enum MergeResolution {
112 MCR_First,
113 MCR_Second,
114 MCR_Largest,
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000115 MCR_SameSize,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000116 MCR_Error
117};
118
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000119static MergeResolution mergeCases[][6] = {
120 // no tentative weak weakAddress sameNameAndSize largest
121 {MCR_Error, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // no
122 {MCR_Second, MCR_Largest, MCR_Second, MCR_Second, MCR_SameSize, MCR_Largest}, // tentative
123 {MCR_Second, MCR_First, MCR_First, MCR_Second, MCR_SameSize, MCR_Largest}, // weak
124 {MCR_Second, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // weakAddress
125 {MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize}, // sameSize
126 {MCR_Largest, MCR_Largest, MCR_Largest, MCR_Largest, MCR_SameSize, MCR_Largest}, // largest
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000127};
128
Michael J. Spencer765792d2012-04-03 18:40:27 +0000129static MergeResolution mergeSelect(DefinedAtom::Merge first,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000130 DefinedAtom::Merge second) {
Rui Ueyama7caea312014-03-08 00:44:01 +0000131 assert(first != DefinedAtom::mergeByContent);
132 assert(second != DefinedAtom::mergeByContent);
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000133 return mergeCases[first][second];
134}
135
Rui Ueyama331f4822014-04-04 18:34:40 +0000136static const DefinedAtom *followReference(const DefinedAtom *atom,
137 uint32_t kind) {
138 for (const Reference *r : *atom)
139 if (r->kindNamespace() == Reference::KindNamespace::all &&
140 r->kindArch() == Reference::KindArch::all &&
141 r->kindValue() == kind)
142 return cast<const DefinedAtom>(r->target());
143 return nullptr;
144}
145
146static uint64_t getSizeFollowReferences(const DefinedAtom *atom,
147 uint32_t kind) {
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000148 uint64_t size = 0;
Rui Ueyama331f4822014-04-04 18:34:40 +0000149 for (;;) {
150 atom = followReference(atom, kind);
151 if (!atom)
152 return size;
153 size += atom->size();
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000154 }
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000155}
156
157// Returns the size of the section containing the given atom. Atoms in the same
158// section are connected by layout-before and layout-after edges, so this
159// function traverses them to get the total size of atoms in the same section.
160static uint64_t sectionSize(const DefinedAtom *atom) {
161 return atom->size()
162 + getSizeFollowReferences(atom, lld::Reference::kindLayoutBefore)
163 + getSizeFollowReferences(atom, lld::Reference::kindLayoutAfter);
164}
165
Rui Ueyama2a522512014-05-14 17:29:27 +0000166bool SymbolTable::addByName(const Atom &newAtom) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000167 StringRef name = newAtom.name();
Nick Kledzik233f5372013-01-15 00:17:57 +0000168 assert(!name.empty());
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000169 const Atom *existing = findByName(name);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000170 if (existing == nullptr) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000171 // Name is not in symbol table yet, add it associate with this atom.
Nick Kledzikbb38f7b2014-08-20 20:46:28 +0000172 _context.notifySymbolTableAdd(&newAtom);
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000173 _nameTable[name] = &newAtom;
Rui Ueyama2a522512014-05-14 17:29:27 +0000174 return true;
Michael J. Spencer765792d2012-04-03 18:40:27 +0000175 }
Rui Ueyamaf347e752013-11-13 23:22:00 +0000176
Rui Ueyama2a522512014-05-14 17:29:27 +0000177 // Do nothing if the same object is added more than once.
178 if (existing == &newAtom)
179 return false;
180
Rui Ueyamaf347e752013-11-13 23:22:00 +0000181 // Name is already in symbol table and associated with another atom.
182 bool useNew = true;
183 switch (collide(existing->definition(), newAtom.definition())) {
184 case NCR_First:
185 useNew = false;
186 break;
187 case NCR_Second:
188 useNew = true;
189 break;
190 case NCR_DupDef:
Rui Ueyama0abf6132014-10-14 21:42:08 +0000191 switch (mergeSelect(cast<DefinedAtom>(existing)->merge(),
192 cast<DefinedAtom>(&newAtom)->merge())) {
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000193 case MCR_First:
194 useNew = false;
195 break;
196 case MCR_Second:
197 useNew = true;
198 break;
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000199 case MCR_Largest: {
200 uint64_t existingSize = sectionSize((DefinedAtom*)existing);
201 uint64_t newSize = sectionSize((DefinedAtom*)&newAtom);
202 useNew = (newSize >= existingSize);
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000203 break;
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000204 }
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000205 case MCR_SameSize: {
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000206 uint64_t existingSize = sectionSize((DefinedAtom*)existing);
207 uint64_t newSize = sectionSize((DefinedAtom*)&newAtom);
208 if (existingSize == newSize) {
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000209 useNew = true;
210 break;
211 }
212 llvm::errs() << "Size mismatch: "
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000213 << existing->name() << " (" << existingSize << ") "
214 << newAtom.name() << " (" << newSize << ")\n";
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000215 // fallthrough
216 }
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000217 case MCR_Error:
Rui Ueyamaa9a51292014-03-28 16:26:38 +0000218 if (!_context.getAllowDuplicates()) {
219 llvm::errs() << "Duplicate symbols: "
220 << existing->name()
221 << ":"
222 << existing->file().path()
223 << " and "
224 << newAtom.name()
225 << ":"
226 << newAtom.file().path()
227 << "\n";
228 llvm::report_fatal_error("duplicate symbol error");
229 }
230 useNew = false;
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000231 break;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000232 }
233 break;
234 case NCR_DupUndef: {
Rui Ueyama01cc7182014-04-04 18:21:51 +0000235 const UndefinedAtom* existingUndef = cast<UndefinedAtom>(existing);
236 const UndefinedAtom* newUndef = cast<UndefinedAtom>(&newAtom);
Rui Ueyamab4dca7f2013-11-15 03:12:24 +0000237
238 bool sameCanBeNull = (existingUndef->canBeNull() == newUndef->canBeNull());
239 if (!sameCanBeNull &&
240 _context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
241 llvm::errs() << "lld warning: undefined symbol "
242 << existingUndef->name()
243 << " has different weakness in "
244 << existingUndef->file().path()
245 << " and in " << newUndef->file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000246 }
Rui Ueyamab4dca7f2013-11-15 03:12:24 +0000247
248 const UndefinedAtom *existingFallback = existingUndef->fallback();
249 const UndefinedAtom *newFallback = newUndef->fallback();
250 bool hasDifferentFallback =
251 (existingFallback && newFallback &&
252 existingFallback->name() != newFallback->name());
253 if (hasDifferentFallback) {
254 llvm::errs() << "lld warning: undefined symbol "
255 << existingUndef->name() << " has different fallback: "
256 << existingFallback->name() << " in "
257 << existingUndef->file().path() << " and "
258 << newFallback->name() << " in "
259 << newUndef->file().path() << "\n";
260 }
261
262 bool hasNewFallback = newUndef->fallback();
263 if (sameCanBeNull)
264 useNew = hasNewFallback;
265 else
266 useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
Rui Ueyamaf347e752013-11-13 23:22:00 +0000267 break;
Rui Ueyamab4dca7f2013-11-15 03:12:24 +0000268 }
Rui Ueyamaf347e752013-11-13 23:22:00 +0000269 case NCR_DupShLib: {
Rui Ueyama01cc7182014-04-04 18:21:51 +0000270 const SharedLibraryAtom *curShLib = cast<SharedLibraryAtom>(existing);
271 const SharedLibraryAtom *newShLib = cast<SharedLibraryAtom>(&newAtom);
Rui Ueyamafb7936d2014-04-04 18:12:27 +0000272 bool sameNullness =
273 (curShLib->canBeNullAtRuntime() == newShLib->canBeNullAtRuntime());
274 bool sameName = curShLib->loadName().equals(newShLib->loadName());
Rui Ueyamae8af3e42014-04-04 18:21:53 +0000275 if (sameName && !sameNullness &&
276 _context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
277 // FIXME: need diagonstics interface for writing warning messages
278 llvm::errs() << "lld warning: shared library symbol "
279 << curShLib->name() << " has different weakness in "
280 << curShLib->file().path() << " and in "
281 << newShLib->file().path();
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000282 }
Rui Ueyamae8af3e42014-04-04 18:21:53 +0000283 if (!sameName && _context.warnIfCoalesableAtomsHaveDifferentLoadName()) {
284 // FIXME: need diagonstics interface for writing warning messages
285 llvm::errs() << "lld warning: shared library symbol "
286 << curShLib->name() << " has different load path in "
287 << curShLib->file().path() << " and in "
288 << newShLib->file().path();
289 }
290 useNew = false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000291 break;
Rui Ueyamafb7936d2014-04-04 18:12:27 +0000292 }
Rui Ueyamabcccb5d2013-11-13 23:23:38 +0000293 case NCR_Error:
294 llvm::errs() << "SymbolTable: error while merging " << name << "\n";
Rui Ueyama9310e012013-11-14 06:39:31 +0000295 llvm::report_fatal_error("duplicate symbol error");
296 break;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000297 }
Rui Ueyamabcccb5d2013-11-13 23:23:38 +0000298
Nick Kledzikbb38f7b2014-08-20 20:46:28 +0000299 // Give context a chance to change which is kept.
300 _context.notifySymbolTableCoalesce(existing, &newAtom, useNew);
301
Rui Ueyamaf347e752013-11-13 23:22:00 +0000302 if (useNew) {
303 // Update name table to use new atom.
304 _nameTable[name] = &newAtom;
305 // Add existing atom to replacement table.
306 _replacedAtoms[existing] = &newAtom;
307 } else {
308 // New atom is not being used. Add it to replacement table.
309 _replacedAtoms[&newAtom] = existing;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000310 }
Rui Ueyama2a522512014-05-14 17:29:27 +0000311 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000312}
313
Michael J. Spencer67f25272013-03-20 22:18:22 +0000314unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom *atom) {
315 auto content = atom->rawContent();
316 return llvm::hash_combine(atom->size(),
317 atom->contentType(),
318 llvm::hash_combine_range(content.begin(),
319 content.end()));
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000320}
321
Michael J. Spencer765792d2012-04-03 18:40:27 +0000322bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
Michael J. Spencer67f25272013-03-20 22:18:22 +0000323 const DefinedAtom * const r) {
Rui Ueyamaf347e752013-11-13 23:22:00 +0000324 if (l == r)
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000325 return true;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000326 if (l == getEmptyKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000327 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000328 if (r == getEmptyKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000329 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000330 if (l == getTombstoneKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000331 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000332 if (r == getTombstoneKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000333 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000334 if (l->contentType() != r->contentType())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000335 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000336 if (l->size() != r->size())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000337 return false;
Nick Kledzik32d0d092014-10-02 17:22:05 +0000338 if (l->sectionChoice() != r->sectionChoice())
339 return false;
340 if (l->sectionChoice() == DefinedAtom::sectionCustomRequired) {
341 if (!l->customSectionName().equals(r->customSectionName()))
342 return false;
343 }
Michael J. Spencere6203a52012-04-03 18:39:40 +0000344 ArrayRef<uint8_t> lc = l->rawContent();
345 ArrayRef<uint8_t> rc = r->rawContent();
Michael J. Spencer67f25272013-03-20 22:18:22 +0000346 return memcmp(lc.data(), rc.data(), lc.size()) == 0;
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000347}
348
Rui Ueyama2a522512014-05-14 17:29:27 +0000349bool SymbolTable::addByContent(const DefinedAtom &newAtom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000350 AtomContentSet::iterator pos = _contentTable.find(&newAtom);
Rui Ueyamaf347e752013-11-13 23:22:00 +0000351 if (pos == _contentTable.end()) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000352 _contentTable.insert(&newAtom);
Rui Ueyama2a522512014-05-14 17:29:27 +0000353 return true;
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000354 }
355 const Atom* existing = *pos;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000356 // New atom is not being used. Add it to replacement table.
357 _replacedAtoms[&newAtom] = existing;
Rui Ueyama2a522512014-05-14 17:29:27 +0000358 return false;
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000359}
360
Michael J. Spencere6203a52012-04-03 18:39:40 +0000361const Atom *SymbolTable::findByName(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000362 NameToAtom::iterator pos = _nameTable.find(sym);
363 if (pos == _nameTable.end())
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000364 return nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000365 return pos->second;
366}
367
Michael J. Spencere6203a52012-04-03 18:39:40 +0000368bool SymbolTable::isDefined(StringRef sym) {
Rui Ueyama71c02022014-04-03 22:43:42 +0000369 if (const Atom *atom = findByName(sym))
Rui Ueyama0abf6132014-10-14 21:42:08 +0000370 return !isa<UndefinedAtom>(atom);
Rui Ueyama71c02022014-04-03 22:43:42 +0000371 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000372}
373
Rui Ueyamae5416ec2013-09-12 19:14:05 +0000374void SymbolTable::addReplacement(const Atom *replaced,
375 const Atom *replacement) {
376 _replacedAtoms[replaced] = replacement;
377}
378
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000379const Atom *SymbolTable::replacement(const Atom *atom) {
Rui Ueyama1c3486a2014-04-03 22:58:41 +0000380 // Find the replacement for a given atom. Atoms in _replacedAtoms
381 // may be chained, so find the last one.
Rui Ueyama4f010d22014-04-03 22:36:55 +0000382 for (;;) {
383 AtomToAtom::iterator pos = _replacedAtoms.find(atom);
384 if (pos == _replacedAtoms.end())
385 return atom;
Rui Ueyama4f010d22014-04-03 22:36:55 +0000386 atom = pos->second;
387 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000388}
389
Rui Ueyama733b45f2014-06-05 07:37:29 +0000390bool SymbolTable::isCoalescedAway(const Atom *atom) {
391 return _replacedAtoms.count(atom) > 0;
392}
393
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000394unsigned int SymbolTable::size() {
395 return _nameTable.size();
396}
397
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000398std::vector<const UndefinedAtom *> SymbolTable::undefines() {
399 std::vector<const UndefinedAtom *> ret;
Rui Ueyama17e899c2013-11-25 17:09:27 +0000400 for (auto it : _nameTable) {
401 const Atom *atom = it.second;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000402 assert(atom != nullptr);
Rui Ueyama839fb2f2014-08-22 02:00:58 +0000403 if (const auto *undef = dyn_cast<const UndefinedAtom>(atom))
404 if (_replacedAtoms.count(undef) == 0)
405 ret.push_back(undef);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000406 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000407 return ret;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000408}
409
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000410std::vector<StringRef> SymbolTable::tentativeDefinitions() {
411 std::vector<StringRef> ret;
Nick Kledzik20e652d2012-04-20 01:24:37 +0000412 for (auto entry : _nameTable) {
413 const Atom *atom = entry.second;
414 StringRef name = entry.first;
415 assert(atom != nullptr);
Rui Ueyamaf347e752013-11-13 23:22:00 +0000416 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
417 if (defAtom->merge() == DefinedAtom::mergeAsTentative)
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000418 ret.push_back(name);
Nick Kledzik20e652d2012-04-20 01:24:37 +0000419 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000420 return ret;
Nick Kledzik20e652d2012-04-20 01:24:37 +0000421}
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000422
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000423} // namespace lld