blob: cc2146bffd2ba6248b51655d5b34ceafda28db5d [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"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000016#include "lld/Core/Resolver.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000017#include "lld/Core/SharedLibraryAtom.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000018#include "lld/Core/LinkingContext.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000019#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000020
Nick Kledzikbfedfc12012-01-09 20:18:15 +000021#include "llvm/ADT/ArrayRef.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000022#include "llvm/ADT/DenseMapInfo.h"
Michael J. Spencer67f25272013-03-20 22:18:22 +000023#include "llvm/ADT/Hashing.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000024#include "llvm/Support/ErrorHandling.h"
Nick Kledzikbb963df2012-04-18 21:55:06 +000025#include "llvm/Support/raw_ostream.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000026
27#include <algorithm>
28#include <cassert>
Michael J. Spencercfd029f2012-03-28 19:04:02 +000029#include <cstdlib>
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000030#include <vector>
31
32namespace lld {
Rui Ueyama0ca149f2013-08-06 22:31:59 +000033SymbolTable::SymbolTable(const LinkingContext &context) : _context(context) {}
Nick Kledzik38eec3d2011-12-22 02:38:01 +000034
Rui Ueyama2a522512014-05-14 17:29:27 +000035bool SymbolTable::add(const UndefinedAtom &atom) { return addByName(atom); }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000036
Rui Ueyama2a522512014-05-14 17:29:27 +000037bool SymbolTable::add(const SharedLibraryAtom &atom) { return addByName(atom); }
Michael J. Spencer765792d2012-04-03 18:40:27 +000038
Rui Ueyama2a522512014-05-14 17:29:27 +000039bool SymbolTable::add(const AbsoluteAtom &atom) { return addByName(atom); }
Nick Kledzik6bc04c62012-02-22 21:56:59 +000040
Rui Ueyama2a522512014-05-14 17:29:27 +000041bool SymbolTable::add(const DefinedAtom &atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +000042 if (!atom.name().empty() &&
Rui Ueyama8b08c372013-11-25 17:09:29 +000043 atom.scope() != DefinedAtom::scopeTranslationUnit) {
Nick Kledzik233f5372013-01-15 00:17:57 +000044 // Named atoms cannot be merged by content.
45 assert(atom.merge() != DefinedAtom::mergeByContent);
46 // Track named atoms that are not scoped to file (static).
Rui Ueyama2a522512014-05-14 17:29:27 +000047 return addByName(atom);
Rui Ueyama8b08c372013-11-25 17:09:29 +000048 }
49 if (atom.merge() == DefinedAtom::mergeByContent) {
Nick Kledzik233f5372013-01-15 00:17:57 +000050 // Named atoms cannot be merged by content.
51 assert(atom.name().empty());
Nick Kledzik388f3d02014-05-28 01:16:35 +000052 // Currently only read-only constants can be merged.
53 if (atom.permissions() == DefinedAtom::permR__)
54 return addByContent(atom);
55 // TODO: support mergeByContent of data atoms by comparing content & fixups.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000056 }
Rui Ueyama2a522512014-05-14 17:29:27 +000057 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000058}
59
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +000060const Atom *SymbolTable::findGroup(StringRef sym) {
61 NameToAtom::iterator pos = _groupTable.find(sym);
62 if (pos == _groupTable.end())
63 return nullptr;
64 return pos->second;
65}
66
67bool SymbolTable::addGroup(const DefinedAtom &da) {
68 StringRef name = da.name();
69 assert(!name.empty());
70 const Atom *existing = findGroup(name);
71 if (existing == nullptr) {
72 _groupTable[name] = &da;
73 return true;
74 }
75 _replacedAtoms[&da] = existing;
76 return false;
77}
78
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000079enum NameCollisionResolution {
80 NCR_First,
81 NCR_Second,
Nick Kledzik6bc04c62012-02-22 21:56:59 +000082 NCR_DupDef,
83 NCR_DupUndef,
84 NCR_DupShLib,
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000085 NCR_Error
86};
87
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000088static NameCollisionResolution cases[4][4] = {
89 //regular absolute undef sharedLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000090 {
91 // first is regular
Nick Kledzik6bc04c62012-02-22 21:56:59 +000092 NCR_DupDef, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000093 },
94 {
95 // first is absolute
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000096 NCR_Error, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000097 },
98 {
99 // first is undef
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000100 NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000101 },
102 {
103 // first is sharedLib
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000104 NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000105 }
106};
107
108static NameCollisionResolution collide(Atom::Definition first,
109 Atom::Definition second) {
110 return cases[first][second];
111}
112
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000113enum MergeResolution {
114 MCR_First,
115 MCR_Second,
116 MCR_Largest,
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000117 MCR_SameSize,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000118 MCR_Error
119};
120
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000121static MergeResolution mergeCases[][6] = {
122 // no tentative weak weakAddress sameNameAndSize largest
123 {MCR_Error, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // no
124 {MCR_Second, MCR_Largest, MCR_Second, MCR_Second, MCR_SameSize, MCR_Largest}, // tentative
125 {MCR_Second, MCR_First, MCR_First, MCR_Second, MCR_SameSize, MCR_Largest}, // weak
126 {MCR_Second, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // weakAddress
127 {MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize}, // sameSize
128 {MCR_Largest, MCR_Largest, MCR_Largest, MCR_Largest, MCR_SameSize, MCR_Largest}, // largest
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000129};
130
Michael J. Spencer765792d2012-04-03 18:40:27 +0000131static MergeResolution mergeSelect(DefinedAtom::Merge first,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000132 DefinedAtom::Merge second) {
Rui Ueyama7caea312014-03-08 00:44:01 +0000133 assert(first != DefinedAtom::mergeByContent);
134 assert(second != DefinedAtom::mergeByContent);
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000135 return mergeCases[first][second];
136}
137
Rui Ueyama331f4822014-04-04 18:34:40 +0000138static const DefinedAtom *followReference(const DefinedAtom *atom,
139 uint32_t kind) {
140 for (const Reference *r : *atom)
141 if (r->kindNamespace() == Reference::KindNamespace::all &&
142 r->kindArch() == Reference::KindArch::all &&
143 r->kindValue() == kind)
144 return cast<const DefinedAtom>(r->target());
145 return nullptr;
146}
147
148static uint64_t getSizeFollowReferences(const DefinedAtom *atom,
149 uint32_t kind) {
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000150 uint64_t size = 0;
Rui Ueyama331f4822014-04-04 18:34:40 +0000151 for (;;) {
152 atom = followReference(atom, kind);
153 if (!atom)
154 return size;
155 size += atom->size();
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000156 }
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000157}
158
159// Returns the size of the section containing the given atom. Atoms in the same
160// section are connected by layout-before and layout-after edges, so this
161// function traverses them to get the total size of atoms in the same section.
162static uint64_t sectionSize(const DefinedAtom *atom) {
163 return atom->size()
164 + getSizeFollowReferences(atom, lld::Reference::kindLayoutBefore)
165 + getSizeFollowReferences(atom, lld::Reference::kindLayoutAfter);
166}
167
Rui Ueyama2a522512014-05-14 17:29:27 +0000168bool SymbolTable::addByName(const Atom &newAtom) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000169 StringRef name = newAtom.name();
Nick Kledzik233f5372013-01-15 00:17:57 +0000170 assert(!name.empty());
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000171 const Atom *existing = findByName(name);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000172 if (existing == nullptr) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000173 // Name is not in symbol table yet, add it associate with this atom.
Nick Kledzikbb38f7b2014-08-20 20:46:28 +0000174 _context.notifySymbolTableAdd(&newAtom);
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000175 _nameTable[name] = &newAtom;
Rui Ueyama2a522512014-05-14 17:29:27 +0000176 return true;
Michael J. Spencer765792d2012-04-03 18:40:27 +0000177 }
Rui Ueyamaf347e752013-11-13 23:22:00 +0000178
Rui Ueyama2a522512014-05-14 17:29:27 +0000179 // Do nothing if the same object is added more than once.
180 if (existing == &newAtom)
181 return false;
182
Rui Ueyamaf347e752013-11-13 23:22:00 +0000183 // Name is already in symbol table and associated with another atom.
184 bool useNew = true;
185 switch (collide(existing->definition(), newAtom.definition())) {
186 case NCR_First:
187 useNew = false;
188 break;
189 case NCR_Second:
190 useNew = true;
191 break;
192 case NCR_DupDef:
193 assert(existing->definition() == Atom::definitionRegular);
194 assert(newAtom.definition() == Atom::definitionRegular);
195 switch (mergeSelect(((DefinedAtom*)existing)->merge(),
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000196 ((DefinedAtom*)&newAtom)->merge())) {
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000197 case MCR_First:
198 useNew = false;
199 break;
200 case MCR_Second:
201 useNew = true;
202 break;
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000203 case MCR_Largest: {
204 uint64_t existingSize = sectionSize((DefinedAtom*)existing);
205 uint64_t newSize = sectionSize((DefinedAtom*)&newAtom);
206 useNew = (newSize >= existingSize);
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000207 break;
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000208 }
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000209 case MCR_SameSize: {
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000210 uint64_t existingSize = sectionSize((DefinedAtom*)existing);
211 uint64_t newSize = sectionSize((DefinedAtom*)&newAtom);
212 if (existingSize == newSize) {
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000213 useNew = true;
214 break;
215 }
216 llvm::errs() << "Size mismatch: "
Rui Ueyama7ad72eb2014-03-18 19:37:50 +0000217 << existing->name() << " (" << existingSize << ") "
218 << newAtom.name() << " (" << newSize << ")\n";
Rui Ueyamac79dd2f2014-03-07 23:05:10 +0000219 // fallthrough
220 }
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000221 case MCR_Error:
Rui Ueyamaa9a51292014-03-28 16:26:38 +0000222 if (!_context.getAllowDuplicates()) {
223 llvm::errs() << "Duplicate symbols: "
224 << existing->name()
225 << ":"
226 << existing->file().path()
227 << " and "
228 << newAtom.name()
229 << ":"
230 << newAtom.file().path()
231 << "\n";
232 llvm::report_fatal_error("duplicate symbol error");
233 }
234 useNew = false;
Rui Ueyama5a3804f2013-11-25 17:09:25 +0000235 break;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000236 }
237 break;
238 case NCR_DupUndef: {
Rui Ueyama01cc7182014-04-04 18:21:51 +0000239 const UndefinedAtom* existingUndef = cast<UndefinedAtom>(existing);
240 const UndefinedAtom* newUndef = cast<UndefinedAtom>(&newAtom);
Rui Ueyamab4dca7f2013-11-15 03:12:24 +0000241
242 bool sameCanBeNull = (existingUndef->canBeNull() == newUndef->canBeNull());
243 if (!sameCanBeNull &&
244 _context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
245 llvm::errs() << "lld warning: undefined symbol "
246 << existingUndef->name()
247 << " has different weakness in "
248 << existingUndef->file().path()
249 << " and in " << newUndef->file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000250 }
Rui Ueyamab4dca7f2013-11-15 03:12:24 +0000251
252 const UndefinedAtom *existingFallback = existingUndef->fallback();
253 const UndefinedAtom *newFallback = newUndef->fallback();
254 bool hasDifferentFallback =
255 (existingFallback && newFallback &&
256 existingFallback->name() != newFallback->name());
257 if (hasDifferentFallback) {
258 llvm::errs() << "lld warning: undefined symbol "
259 << existingUndef->name() << " has different fallback: "
260 << existingFallback->name() << " in "
261 << existingUndef->file().path() << " and "
262 << newFallback->name() << " in "
263 << newUndef->file().path() << "\n";
264 }
265
266 bool hasNewFallback = newUndef->fallback();
267 if (sameCanBeNull)
268 useNew = hasNewFallback;
269 else
270 useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
Rui Ueyamaf347e752013-11-13 23:22:00 +0000271 break;
Rui Ueyamab4dca7f2013-11-15 03:12:24 +0000272 }
Rui Ueyamaf347e752013-11-13 23:22:00 +0000273 case NCR_DupShLib: {
Rui Ueyama01cc7182014-04-04 18:21:51 +0000274 const SharedLibraryAtom *curShLib = cast<SharedLibraryAtom>(existing);
275 const SharedLibraryAtom *newShLib = cast<SharedLibraryAtom>(&newAtom);
Rui Ueyamafb7936d2014-04-04 18:12:27 +0000276 bool sameNullness =
277 (curShLib->canBeNullAtRuntime() == newShLib->canBeNullAtRuntime());
278 bool sameName = curShLib->loadName().equals(newShLib->loadName());
Rui Ueyamae8af3e42014-04-04 18:21:53 +0000279 if (sameName && !sameNullness &&
280 _context.warnIfCoalesableAtomsHaveDifferentCanBeNull()) {
281 // FIXME: need diagonstics interface for writing warning messages
282 llvm::errs() << "lld warning: shared library symbol "
283 << curShLib->name() << " has different weakness in "
284 << curShLib->file().path() << " and in "
285 << newShLib->file().path();
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000286 }
Rui Ueyamae8af3e42014-04-04 18:21:53 +0000287 if (!sameName && _context.warnIfCoalesableAtomsHaveDifferentLoadName()) {
288 // FIXME: need diagonstics interface for writing warning messages
289 llvm::errs() << "lld warning: shared library symbol "
290 << curShLib->name() << " has different load path in "
291 << curShLib->file().path() << " and in "
292 << newShLib->file().path();
293 }
294 useNew = false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000295 break;
Rui Ueyamafb7936d2014-04-04 18:12:27 +0000296 }
Rui Ueyamabcccb5d2013-11-13 23:23:38 +0000297 case NCR_Error:
298 llvm::errs() << "SymbolTable: error while merging " << name << "\n";
Rui Ueyama9310e012013-11-14 06:39:31 +0000299 llvm::report_fatal_error("duplicate symbol error");
300 break;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000301 }
Rui Ueyamabcccb5d2013-11-13 23:23:38 +0000302
Nick Kledzikbb38f7b2014-08-20 20:46:28 +0000303 // Give context a chance to change which is kept.
304 _context.notifySymbolTableCoalesce(existing, &newAtom, useNew);
305
Rui Ueyamaf347e752013-11-13 23:22:00 +0000306 if (useNew) {
307 // Update name table to use new atom.
308 _nameTable[name] = &newAtom;
309 // Add existing atom to replacement table.
310 _replacedAtoms[existing] = &newAtom;
311 } else {
312 // New atom is not being used. Add it to replacement table.
313 _replacedAtoms[&newAtom] = existing;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000314 }
Rui Ueyama2a522512014-05-14 17:29:27 +0000315 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000316}
317
Michael J. Spencer67f25272013-03-20 22:18:22 +0000318unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom *atom) {
319 auto content = atom->rawContent();
320 return llvm::hash_combine(atom->size(),
321 atom->contentType(),
322 llvm::hash_combine_range(content.begin(),
323 content.end()));
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000324}
325
Michael J. Spencer765792d2012-04-03 18:40:27 +0000326bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
Michael J. Spencer67f25272013-03-20 22:18:22 +0000327 const DefinedAtom * const r) {
Rui Ueyamaf347e752013-11-13 23:22:00 +0000328 if (l == r)
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000329 return true;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000330 if (l == getEmptyKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000331 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000332 if (r == getEmptyKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000333 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000334 if (l == getTombstoneKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000335 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000336 if (r == getTombstoneKey())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000337 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000338 if (l->contentType() != r->contentType())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000339 return false;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000340 if (l->size() != r->size())
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000341 return false;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000342 ArrayRef<uint8_t> lc = l->rawContent();
343 ArrayRef<uint8_t> rc = r->rawContent();
Michael J. Spencer67f25272013-03-20 22:18:22 +0000344 return memcmp(lc.data(), rc.data(), lc.size()) == 0;
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000345}
346
Rui Ueyama2a522512014-05-14 17:29:27 +0000347bool SymbolTable::addByContent(const DefinedAtom &newAtom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000348 AtomContentSet::iterator pos = _contentTable.find(&newAtom);
Rui Ueyamaf347e752013-11-13 23:22:00 +0000349 if (pos == _contentTable.end()) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000350 _contentTable.insert(&newAtom);
Rui Ueyama2a522512014-05-14 17:29:27 +0000351 return true;
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000352 }
353 const Atom* existing = *pos;
Rui Ueyamaf347e752013-11-13 23:22:00 +0000354 // New atom is not being used. Add it to replacement table.
355 _replacedAtoms[&newAtom] = existing;
Rui Ueyama2a522512014-05-14 17:29:27 +0000356 return false;
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000357}
358
Michael J. Spencere6203a52012-04-03 18:39:40 +0000359const Atom *SymbolTable::findByName(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000360 NameToAtom::iterator pos = _nameTable.find(sym);
361 if (pos == _nameTable.end())
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000362 return nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000363 return pos->second;
364}
365
Michael J. Spencere6203a52012-04-03 18:39:40 +0000366bool SymbolTable::isDefined(StringRef sym) {
Rui Ueyama71c02022014-04-03 22:43:42 +0000367 if (const Atom *atom = findByName(sym))
368 return atom->definition() != Atom::definitionUndefined;
369 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000370}
371
Rui Ueyamae5416ec2013-09-12 19:14:05 +0000372void SymbolTable::addReplacement(const Atom *replaced,
373 const Atom *replacement) {
374 _replacedAtoms[replaced] = replacement;
375}
376
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000377const Atom *SymbolTable::replacement(const Atom *atom) {
Rui Ueyama1c3486a2014-04-03 22:58:41 +0000378 // Find the replacement for a given atom. Atoms in _replacedAtoms
379 // may be chained, so find the last one.
Rui Ueyama4f010d22014-04-03 22:36:55 +0000380 for (;;) {
381 AtomToAtom::iterator pos = _replacedAtoms.find(atom);
382 if (pos == _replacedAtoms.end())
383 return atom;
Rui Ueyama4f010d22014-04-03 22:36:55 +0000384 atom = pos->second;
385 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000386}
387
Rui Ueyama733b45f2014-06-05 07:37:29 +0000388bool SymbolTable::isCoalescedAway(const Atom *atom) {
389 return _replacedAtoms.count(atom) > 0;
390}
391
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000392unsigned int SymbolTable::size() {
393 return _nameTable.size();
394}
395
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000396std::vector<const UndefinedAtom *> SymbolTable::undefines() {
397 std::vector<const UndefinedAtom *> ret;
Rui Ueyama17e899c2013-11-25 17:09:27 +0000398 for (auto it : _nameTable) {
399 const Atom *atom = it.second;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000400 assert(atom != nullptr);
Rui Ueyamae5416ec2013-09-12 19:14:05 +0000401 if (const auto undef = dyn_cast<const UndefinedAtom>(atom)) {
402 AtomToAtom::iterator pos = _replacedAtoms.find(undef);
403 if (pos != _replacedAtoms.end())
404 continue;
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000405 ret.push_back(undef);
Rui Ueyamae5416ec2013-09-12 19:14:05 +0000406 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000407 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000408 return ret;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000409}
410
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000411std::vector<StringRef> SymbolTable::tentativeDefinitions() {
412 std::vector<StringRef> ret;
Nick Kledzik20e652d2012-04-20 01:24:37 +0000413 for (auto entry : _nameTable) {
414 const Atom *atom = entry.second;
415 StringRef name = entry.first;
416 assert(atom != nullptr);
Rui Ueyamaf347e752013-11-13 23:22:00 +0000417 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
418 if (defAtom->merge() == DefinedAtom::mergeAsTentative)
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000419 ret.push_back(name);
Nick Kledzik20e652d2012-04-20 01:24:37 +0000420 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000421 return ret;
Nick Kledzik20e652d2012-04-20 01:24:37 +0000422}
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000423
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000424} // namespace lld