blob: f914a8edd18cad5ad3ea24df76273a7d3e510197 [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001//===- Core/Resolver.cpp - Resolves Atom References -----------------------===//
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
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000010#include "lld/Core/Atom.h"
Shankar Easwarana96f3a32013-10-07 02:47:09 +000011#include "lld/Core/ArchiveLibraryFile.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000012#include "lld/Core/File.h"
Shankar Easwarana96f3a32013-10-07 02:47:09 +000013#include "lld/Core/SharedLibraryFile.h"
Michael J. Spencerbd66d042013-05-28 18:55:39 +000014#include "lld/Core/Instrumentation.h"
Michael J. Spencere6203a52012-04-03 18:39:40 +000015#include "lld/Core/LLVM.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000016#include "lld/Core/Resolver.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000017#include "lld/Core/SymbolTable.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000018#include "lld/Core/LinkingContext.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000019#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000020
Nick Kledzikabb69812012-05-31 22:34:00 +000021#include "llvm/Support/Debug.h"
Michael J. Spencer4586fbc2013-01-22 20:49:42 +000022#include "llvm/Support/ErrorHandling.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000023#include "llvm/Support/Format.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000024#include "llvm/Support/raw_ostream.h"
25
26#include <algorithm>
27#include <cassert>
28#include <vector>
29
30namespace lld {
31
Nick Kledzik80d83082013-05-08 21:34:11 +000032namespace {
33
Michael J. Spencer765792d2012-04-03 18:40:27 +000034/// This is used as a filter function to std::remove_if to coalesced atoms.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000035class AtomCoalescedAway {
36public:
Nick Kledzik80d83082013-05-08 21:34:11 +000037 explicit AtomCoalescedAway(SymbolTable &sym) : _symbolTable(sym) {}
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000038
39 bool operator()(const Atom *atom) const {
40 const Atom *rep = _symbolTable.replacement(atom);
41 return rep != atom;
42 }
43
44private:
45 SymbolTable &_symbolTable;
46};
47
Nick Kledzik80d83082013-05-08 21:34:11 +000048} // namespace
49
Shankar Easwarana96f3a32013-10-07 02:47:09 +000050void Resolver::handleFile(const File &file) {
Rui Ueyama70625fb2014-04-03 22:21:59 +000051 bool isEmpty = file.defined().empty() && file.undefined().empty() &&
52 file.sharedLibrary().empty() && file.absolute().empty();
Rui Ueyama6aa91cf2014-04-03 21:00:03 +000053 if (isEmpty)
54 return;
55
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000056 for (const DefinedAtom *atom : file.defined())
Shankar Easwarana96f3a32013-10-07 02:47:09 +000057 doDefinedAtom(*atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +000058 for (const UndefinedAtom *atom : file.undefined())
59 doUndefinedAtom(*atom);
Rui Ueyama94575102014-04-03 20:54:47 +000060 for (const SharedLibraryAtom *atom : file.sharedLibrary())
61 doSharedLibraryAtom(*atom);
62 for (const AbsoluteAtom *atom : file.absolute())
63 doAbsoluteAtom(*atom);
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000064
Rui Ueyama6aa91cf2014-04-03 21:00:03 +000065 // Notify the input file manager of the fact that we have made some progress
66 // on linking using the current input file. It may want to know the fact for
67 // --start-group/--end-group.
Rui Ueyama8bd093b2014-04-04 00:14:04 +000068 _context.getInputGraph().notifyProgress();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000069}
70
Rui Ueyama0b8e0532014-04-04 00:39:37 +000071void Resolver::forEachUndefines(bool searchForOverrides,
72 UndefCallback callback) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000073 // Handle normal archives
74 int64_t undefineGenCount = 0;
75 do {
76 undefineGenCount = _symbolTable.size();
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000077 for (const UndefinedAtom *undefAtom : _symbolTable.undefines()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000078 StringRef undefName = undefAtom->name();
79 // load for previous undefine may also have loaded this undefine
Rui Ueyama3429d012013-11-15 04:58:54 +000080 if (!_symbolTable.isDefined(undefName))
81 callback(undefName, false);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000082 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000083
Shankar Easwarana96f3a32013-10-07 02:47:09 +000084 // search libraries for overrides of common symbols
Rui Ueyama3429d012013-11-15 04:58:54 +000085 if (searchForOverrides) {
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000086 for (StringRef tentDefName : _symbolTable.tentativeDefinitions()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000087 // Load for previous tentative may also have loaded
88 // something that overrode this tentative, so always check.
89 const Atom *curAtom = _symbolTable.findByName(tentDefName);
90 assert(curAtom != nullptr);
91 if (const DefinedAtom *curDefAtom = dyn_cast<DefinedAtom>(curAtom)) {
Rui Ueyama3429d012013-11-15 04:58:54 +000092 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative)
93 callback(tentDefName, true);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000094 }
95 }
96 }
97 } while (undefineGenCount != _symbolTable.size());
98}
99
Rui Ueyama3429d012013-11-15 04:58:54 +0000100void Resolver::handleArchiveFile(const File &file) {
Rui Ueyama0ca88e32014-04-02 06:54:43 +0000101 const ArchiveLibraryFile *archiveFile = cast<ArchiveLibraryFile>(&file);
Rui Ueyama0b8e0532014-04-04 00:39:37 +0000102 bool searchForOverrides =
103 _context.searchArchivesToOverrideTentativeDefinitions();
104 forEachUndefines(searchForOverrides,
105 [&](StringRef undefName, bool dataSymbolOnly) {
Rui Ueyama3429d012013-11-15 04:58:54 +0000106 if (const File *member = archiveFile->find(undefName, dataSymbolOnly)) {
107 member->setOrdinal(_context.getNextOrdinalAndIncrement());
108 handleFile(*member);
109 }
Rui Ueyama0b8e0532014-04-04 00:39:37 +0000110 });
Rui Ueyama3429d012013-11-15 04:58:54 +0000111}
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000112
Rui Ueyama3429d012013-11-15 04:58:54 +0000113void Resolver::handleSharedLibrary(const File &file) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000114 // Add all the atoms from the shared library
Rui Ueyama0ca88e32014-04-02 06:54:43 +0000115 const SharedLibraryFile *sharedLibrary = cast<SharedLibraryFile>(&file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000116 handleFile(*sharedLibrary);
Rui Ueyama3429d012013-11-15 04:58:54 +0000117 bool searchForOverrides =
118 _context.searchSharedLibrariesToOverrideTentativeDefinitions();
Rui Ueyama0b8e0532014-04-04 00:39:37 +0000119 forEachUndefines(searchForOverrides,
120 [&](StringRef undefName, bool dataSymbolOnly) {
121 if (const SharedLibraryAtom *atom =
122 sharedLibrary->exports(undefName, dataSymbolOnly))
123 doSharedLibraryAtom(*atom);
124 });
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000125}
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000126
Rui Ueyamafd133b32013-11-21 23:41:53 +0000127void Resolver::doUndefinedAtom(const UndefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000128 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
129 << " UndefinedAtom: "
130 << llvm::format("0x%09lX", &atom)
Rui Ueyama70625fb2014-04-03 22:21:59 +0000131 << ", name=" << atom.name() << "\n");
Nick Kledzikabb69812012-05-31 22:34:00 +0000132
Rui Ueyamafd133b32013-11-21 23:41:53 +0000133 // add to list of known atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000134 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000135
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000136 // tell symbol table
137 _symbolTable.add(atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +0000138
139 // If the undefined symbol has an alternative name, try to resolve the
140 // symbol with the name to give it a second chance. This feature is used
141 // for COFF "weak external" symbol.
142 if (!_symbolTable.isDefined(atom.name())) {
143 if (const UndefinedAtom *fallbackAtom = atom.fallback()) {
144 doUndefinedAtom(*fallbackAtom);
145 _symbolTable.addReplacement(&atom, fallbackAtom);
146 }
147 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000148}
149
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000150/// \brief Add the section group and the group-child reference members.
Shankar Easwaran9316c402014-04-01 03:49:55 +0000151bool Resolver::maybeAddSectionGroupOrGnuLinkOnce(const DefinedAtom &atom) {
Rui Ueyamafb44f5e2014-04-03 21:06:23 +0000152 // First time adding a group?
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000153 bool isFirstTime = _symbolTable.addGroup(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000154
155 if (!isFirstTime) {
156 // If duplicate symbols are allowed, select the first group.
157 if (_context.getAllowDuplicates())
158 return true;
159 const DefinedAtom *prevGroup =
Rui Ueyamab461b1c2014-04-02 06:54:46 +0000160 dyn_cast<DefinedAtom>(_symbolTable.findGroup(atom.name()));
Shankar Easwaran9316c402014-04-01 03:49:55 +0000161 assert(prevGroup &&
162 "Internal Error: The group atom could only be a defined atom");
163 // The atoms should be of the same content type, reject invalid group
164 // resolution behaviors.
165 if (atom.contentType() != prevGroup->contentType())
166 return false;
167 return true;
168 }
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000169
170 for (const Reference *r : atom) {
171 if ((r->kindNamespace() == lld::Reference::KindNamespace::all) &&
172 (r->kindValue() == lld::Reference::kindGroupChild)) {
Rui Ueyamab461b1c2014-04-02 06:54:46 +0000173 const DefinedAtom *target = dyn_cast<DefinedAtom>(r->target());
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000174 assert(target && "Internal Error: kindGroupChild references need to "
175 "be associated with Defined Atoms only");
176 _atoms.push_back(target);
177 _symbolTable.add(*target);
178 }
179 }
Shankar Easwaran9316c402014-04-01 03:49:55 +0000180 return true;
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000181}
182
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000183// called on each atom when a file is added
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000184void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000185 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
186 << " DefinedAtom: "
187 << llvm::format("0x%09lX", &atom)
Nick Kledzik36293f62013-01-23 22:32:56 +0000188 << ", file=#"
189 << atom.file().ordinal()
190 << ", atom=#"
191 << atom.ordinal()
Nick Kledzikabb69812012-05-31 22:34:00 +0000192 << ", name="
193 << atom.name()
194 << "\n");
195
Nick Kledzik36293f62013-01-23 22:32:56 +0000196 // Verify on zero-size atoms are pinned to start or end of section.
Rui Ueyama71c02022014-04-03 22:43:42 +0000197 if (atom.sectionPosition() == DefinedAtom::sectionPositionStart ||
198 atom.sectionPosition() == DefinedAtom::sectionPositionEnd) {
Rui Ueyamae20474d2013-11-13 03:30:29 +0000199 assert(atom.size() == 0);
Nick Kledzik36293f62013-01-23 22:32:56 +0000200 }
201
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000202 // add to list of known atoms
203 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000204
Shankar Easwaran9316c402014-04-01 03:49:55 +0000205 if (atom.isGroupParent()) {
206 // Raise error if there exists a similar gnu linkonce section.
207 if (!maybeAddSectionGroupOrGnuLinkOnce(atom)) {
208 llvm::errs() << "SymbolTable: error while merging " << atom.name()
209 << "\n";
210 llvm::report_fatal_error("duplicate symbol error");
211 }
212 } else {
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000213 _symbolTable.add(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000214 }
Nick Kledzikabb69812012-05-31 22:34:00 +0000215
Rui Ueyama83e6acc2014-04-03 21:11:22 +0000216 // An atom that should never be dead-stripped is a dead-strip root.
217 if (_context.deadStrip() && atom.deadStrip() == DefinedAtom::deadStripNever) {
218 _deadStripRoots.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000219 }
220}
221
Rui Ueyamafd133b32013-11-21 23:41:53 +0000222void Resolver::doSharedLibraryAtom(const SharedLibraryAtom &atom) {
223 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000224 << " SharedLibraryAtom: "
225 << llvm::format("0x%09lX", &atom)
226 << ", name="
227 << atom.name()
228 << "\n");
229
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000230 // add to list of known atoms
231 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000232
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000233 // tell symbol table
234 _symbolTable.add(atom);
235}
Michael J. Spencer765792d2012-04-03 18:40:27 +0000236
Rui Ueyamafd133b32013-11-21 23:41:53 +0000237void Resolver::doAbsoluteAtom(const AbsoluteAtom &atom) {
238 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000239 << " AbsoluteAtom: "
240 << llvm::format("0x%09lX", &atom)
241 << ", name="
Rui Ueyamafd133b32013-11-21 23:41:53 +0000242 << atom.name()
Nick Kledzikabb69812012-05-31 22:34:00 +0000243 << "\n");
244
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000245 // add to list of known atoms
246 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000247
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000248 // tell symbol table
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000249 if (atom.scope() != Atom::scopeTranslationUnit)
Hemant Kulkarnif8286132012-11-05 19:13:54 +0000250 _symbolTable.add(atom);
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000251}
252
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000253// utility to add a vector of atoms
Rui Ueyamafd133b32013-11-21 23:41:53 +0000254void Resolver::addAtoms(const std::vector<const DefinedAtom *> &newAtoms) {
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000255 for (const DefinedAtom *newAtom : newAtoms)
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000256 doDefinedAtom(*newAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000257}
258
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000259// Keep adding atoms until _context.getNextFile() returns an error. This
260// function is where undefined atoms are resolved.
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000261bool Resolver::resolveUndefines() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000262 ScopedTask task(getDefaultDomain(), "resolveUndefines");
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000263
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000264 for (;;) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000265 ErrorOr<File &> file = _context.getInputGraph().getNextFile();
Rafael Espindolad28918b2014-01-08 22:00:09 +0000266 error_code ec = file.getError();
267 if (ec == InputGraphError::no_more_files)
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000268 return true;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000269 if (!file) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000270 llvm::errs() << "Error occurred in getNextFile: " << ec.message() << "\n";
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000271 return false;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000272 }
273
274 switch (file->kind()) {
275 case File::kindObject:
276 assert(!file->hasOrdinal());
277 file->setOrdinal(_context.getNextOrdinalAndIncrement());
278 handleFile(*file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000279 break;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000280 case File::kindArchiveLibrary:
281 if (!file->hasOrdinal())
282 file->setOrdinal(_context.getNextOrdinalAndIncrement());
283 handleArchiveFile(*file);
284 break;
285 case File::kindSharedLibrary:
286 if (!file->hasOrdinal())
287 file->setOrdinal(_context.getNextOrdinalAndIncrement());
288 handleSharedLibrary(*file);
289 break;
Shankar Easwaran1d3c48f2013-10-09 05:23:23 +0000290 }
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000291 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000292}
293
294// switch all references to undefined or coalesced away atoms
295// to the new defined atom
296void Resolver::updateReferences() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000297 ScopedTask task(getDefaultDomain(), "updateReferences");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000298 for (const Atom *atom : _atoms) {
299 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000300 for (const Reference *ref : *defAtom) {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000301 const Atom *newTarget = _symbolTable.replacement(ref->target());
302 const_cast<Reference *>(ref)->setTarget(newTarget);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000303 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000304 }
305 }
306}
307
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000308// For dead code stripping, recursively mark atoms "live"
Nick Kledzikbb963df2012-04-18 21:55:06 +0000309void Resolver::markLive(const Atom &atom) {
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000310 // Mark the atom is live. If it's already marked live, then stop recursion.
311 auto exists = _liveAtoms.insert(&atom);
312 if (!exists.second)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000313 return;
314
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000315 // Mark all atoms it references as live
Rui Ueyamac481b5b2013-12-26 07:02:33 +0000316 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&atom))
317 for (const Reference *ref : *defAtom)
318 if (const Atom *target = ref->target())
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000319 markLive(*target);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000320}
321
322// remove all atoms not actually used
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000323void Resolver::deadStripOptimize() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000324 ScopedTask task(getDefaultDomain(), "deadStripOptimize");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000325 // only do this optimization with -dead_strip
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000326 if (!_context.deadStrip())
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000327 return;
Rui Ueyama841016a2013-12-26 08:11:06 +0000328 assert(_liveAtoms.empty());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000329
Nick Kledzikbb963df2012-04-18 21:55:06 +0000330 // By default, shared libraries are built with all globals as dead strip roots
Rui Ueyama517f0d92014-04-03 21:16:37 +0000331 if (_context.globalsAreDeadStripRoots())
332 for (const Atom *atom : _atoms)
333 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
334 if (defAtom->scope() == DefinedAtom::scopeGlobal)
335 _deadStripRoots.insert(defAtom);
Nick Kledzikabb69812012-05-31 22:34:00 +0000336
Joerg Sonnenberger03ed4cf2014-01-03 18:43:32 +0000337 // Or, use list of names that are dead strip roots.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000338 for (const StringRef &name : _context.deadStripRoots()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000339 const Atom *symAtom = _symbolTable.findByName(name);
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000340 assert(symAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000341 _deadStripRoots.insert(symAtom);
342 }
343
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000344 // mark all roots as live, and recursively all atoms they reference
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000345 for (const Atom *dsrAtom : _deadStripRoots)
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000346 markLive(*dsrAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000347
348 // now remove all non-live atoms from _atoms
Rui Ueyama450d9872014-04-03 22:24:40 +0000349 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), [&](const Atom *a) {
350 return _liveAtoms.count(a) == 0;
351 }),
352 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000353}
354
355// error out if some undefines remain
Rui Ueyama43a589c2014-04-02 06:38:46 +0000356bool Resolver::checkUndefines() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000357 // build vector of remaining undefined symbols
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000358 std::vector<const UndefinedAtom *> undefinedAtoms = _symbolTable.undefines();
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000359 if (_context.deadStrip()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000360 // When dead code stripping, we don't care if dead atoms are undefined.
Rui Ueyama450d9872014-04-03 22:24:40 +0000361 undefinedAtoms.erase(
362 std::remove_if(undefinedAtoms.begin(), undefinedAtoms.end(),
363 [&](const Atom *a) { return _liveAtoms.count(a) == 0; }),
364 undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000365 }
366
Nick Kledzikbb963df2012-04-18 21:55:06 +0000367 // error message about missing symbols
Nick Kledzikc314b462013-04-04 18:59:24 +0000368 if (!undefinedAtoms.empty()) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000369 // FIXME: need diagnostics interface for writing error messages
Nick Kledzikc314b462013-04-04 18:59:24 +0000370 bool foundUndefines = false;
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000371 for (const UndefinedAtom *undefAtom : undefinedAtoms) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000372 const File &f = undefAtom->file();
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000373
374 // Skip over a weak symbol.
375 if (undefAtom->canBeNull() != UndefinedAtom::canBeNullNever)
376 continue;
377
378 // If this is a library and undefined symbols are allowed on the
379 // target platform, skip over it.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000380 if (isa<SharedLibraryFile>(f) && _context.allowShlibUndefines())
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000381 continue;
382
Rui Ueyama6f3254962013-09-12 21:42:52 +0000383 // If the undefine is coalesced away, skip over it.
384 if (_symbolTable.replacement(undefAtom) != undefAtom)
385 continue;
386
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000387 // Seems like this symbol is undefined. Warn that.
388 foundUndefines = true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000389 if (_context.printRemainingUndefines()) {
Rui Ueyama8d7a6f22014-01-18 00:57:40 +0000390 llvm::errs() << "Undefined symbol: " << undefAtom->file().path()
391 << ": " << undefAtom->name() << "\n";
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000392 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000393 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000394 if (foundUndefines) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000395 if (_context.printRemainingUndefines())
Nick Kledzikc314b462013-04-04 18:59:24 +0000396 llvm::errs() << "symbol(s) not found\n";
397 return true;
398 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000399 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000400 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000401}
402
403// remove from _atoms all coaleseced away atoms
404void Resolver::removeCoalescedAwayAtoms() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000405 ScopedTask task(getDefaultDomain(), "removeCoalescedAwayAtoms");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000406 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
Rui Ueyamafd133b32013-11-21 23:41:53 +0000407 AtomCoalescedAway(_symbolTable)),
408 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000409}
410
Nick Kledzikc314b462013-04-04 18:59:24 +0000411bool Resolver::resolve() {
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000412 if (!resolveUndefines())
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000413 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000414 updateReferences();
415 deadStripOptimize();
416 if (checkUndefines())
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000417 if (!_context.allowRemainingUndefines())
Rui Ueyamaee366042013-10-11 06:16:33 +0000418 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000419 removeCoalescedAwayAtoms();
420 _result->addAtoms(_atoms);
Rui Ueyamaee366042013-10-11 06:16:33 +0000421 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000422}
423
Rui Ueyamafd133b32013-11-21 23:41:53 +0000424void Resolver::MergedFile::addAtom(const Atom &atom) {
Rui Ueyama251e68b2014-01-18 00:38:44 +0000425 if (auto *def = dyn_cast<DefinedAtom>(&atom)) {
426 _definedAtoms._atoms.push_back(def);
427 } else if (auto *undef = dyn_cast<UndefinedAtom>(&atom)) {
428 _undefinedAtoms._atoms.push_back(undef);
429 } else if (auto *shared = dyn_cast<SharedLibraryAtom>(&atom)) {
430 _sharedLibraryAtoms._atoms.push_back(shared);
431 } else if (auto *abs = dyn_cast<AbsoluteAtom>(&atom)) {
432 _absoluteAtoms._atoms.push_back(abs);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000433 } else {
Nick Kledzik751eb3d2012-06-15 20:37:24 +0000434 llvm_unreachable("atom has unknown definition kind");
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000435 }
436}
437
Nick Kledzik36293f62013-01-23 22:32:56 +0000438MutableFile::DefinedAtomRange Resolver::MergedFile::definedAtoms() {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000439 return range<std::vector<const DefinedAtom *>::iterator>(
440 _definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
Nick Kledzik36293f62013-01-23 22:32:56 +0000441}
442
Rui Ueyamafd133b32013-11-21 23:41:53 +0000443void Resolver::MergedFile::addAtoms(std::vector<const Atom *> &all) {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000444 ScopedTask task(getDefaultDomain(), "addAtoms");
Nick Kledzikabb69812012-05-31 22:34:00 +0000445 DEBUG_WITH_TYPE("resolver", llvm::dbgs() << "Resolver final atom list:\n");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000446 for (const Atom *atom : all) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000447 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
448 << llvm::format(" 0x%09lX", atom)
449 << ", name="
450 << atom->name()
451 << "\n");
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000452 addAtom(*atom);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000453 }
454}
455
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000456} // namespace lld