blob: 768c05a32fcb48b837df173be65f2de1af44f706 [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 Ueyamafd133b32013-11-21 23:41:53 +000071void Resolver::forEachUndefines(UndefCallback callback,
72 bool searchForOverrides) {
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 Ueyama3429d012013-11-15 04:58:54 +0000102 auto callback = [&](StringRef undefName, bool dataSymbolOnly) {
103 if (const File *member = archiveFile->find(undefName, dataSymbolOnly)) {
104 member->setOrdinal(_context.getNextOrdinalAndIncrement());
105 handleFile(*member);
106 }
107 };
Rui Ueyamafd133b32013-11-21 23:41:53 +0000108 bool searchForOverrides =
109 _context.searchArchivesToOverrideTentativeDefinitions();
Rui Ueyama3429d012013-11-15 04:58:54 +0000110 forEachUndefines(callback, searchForOverrides);
111}
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
118 auto callback = [&](StringRef undefName, bool dataSymbolOnly) {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000119 if (const SharedLibraryAtom *shAtom =
120 sharedLibrary->exports(undefName, dataSymbolOnly))
Rui Ueyama3429d012013-11-15 04:58:54 +0000121 doSharedLibraryAtom(*shAtom);
122 };
123 bool searchForOverrides =
124 _context.searchSharedLibrariesToOverrideTentativeDefinitions();
125 forEachUndefines(callback, searchForOverrides);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000126}
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000127
Rui Ueyamafd133b32013-11-21 23:41:53 +0000128void Resolver::doUndefinedAtom(const UndefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000129 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
130 << " UndefinedAtom: "
131 << llvm::format("0x%09lX", &atom)
Rui Ueyama70625fb2014-04-03 22:21:59 +0000132 << ", name=" << atom.name() << "\n");
Nick Kledzikabb69812012-05-31 22:34:00 +0000133
Rui Ueyamafd133b32013-11-21 23:41:53 +0000134 // add to list of known atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000135 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000136
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000137 // tell symbol table
138 _symbolTable.add(atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +0000139
140 // If the undefined symbol has an alternative name, try to resolve the
141 // symbol with the name to give it a second chance. This feature is used
142 // for COFF "weak external" symbol.
143 if (!_symbolTable.isDefined(atom.name())) {
144 if (const UndefinedAtom *fallbackAtom = atom.fallback()) {
145 doUndefinedAtom(*fallbackAtom);
146 _symbolTable.addReplacement(&atom, fallbackAtom);
147 }
148 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000149}
150
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000151/// \brief Add the section group and the group-child reference members.
Shankar Easwaran9316c402014-04-01 03:49:55 +0000152bool Resolver::maybeAddSectionGroupOrGnuLinkOnce(const DefinedAtom &atom) {
Rui Ueyamafb44f5e2014-04-03 21:06:23 +0000153 // First time adding a group?
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000154 bool isFirstTime = _symbolTable.addGroup(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000155
156 if (!isFirstTime) {
157 // If duplicate symbols are allowed, select the first group.
158 if (_context.getAllowDuplicates())
159 return true;
160 const DefinedAtom *prevGroup =
Rui Ueyamab461b1c2014-04-02 06:54:46 +0000161 dyn_cast<DefinedAtom>(_symbolTable.findGroup(atom.name()));
Shankar Easwaran9316c402014-04-01 03:49:55 +0000162 assert(prevGroup &&
163 "Internal Error: The group atom could only be a defined atom");
164 // The atoms should be of the same content type, reject invalid group
165 // resolution behaviors.
166 if (atom.contentType() != prevGroup->contentType())
167 return false;
168 return true;
169 }
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000170
171 for (const Reference *r : atom) {
172 if ((r->kindNamespace() == lld::Reference::KindNamespace::all) &&
173 (r->kindValue() == lld::Reference::kindGroupChild)) {
Rui Ueyamab461b1c2014-04-02 06:54:46 +0000174 const DefinedAtom *target = dyn_cast<DefinedAtom>(r->target());
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000175 assert(target && "Internal Error: kindGroupChild references need to "
176 "be associated with Defined Atoms only");
177 _atoms.push_back(target);
178 _symbolTable.add(*target);
179 }
180 }
Shankar Easwaran9316c402014-04-01 03:49:55 +0000181 return true;
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000182}
183
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000184// called on each atom when a file is added
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000185void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000186 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
187 << " DefinedAtom: "
188 << llvm::format("0x%09lX", &atom)
Nick Kledzik36293f62013-01-23 22:32:56 +0000189 << ", file=#"
190 << atom.file().ordinal()
191 << ", atom=#"
192 << atom.ordinal()
Nick Kledzikabb69812012-05-31 22:34:00 +0000193 << ", name="
194 << atom.name()
195 << "\n");
196
Nick Kledzik36293f62013-01-23 22:32:56 +0000197 // Verify on zero-size atoms are pinned to start or end of section.
Rui Ueyama71c02022014-04-03 22:43:42 +0000198 if (atom.sectionPosition() == DefinedAtom::sectionPositionStart ||
199 atom.sectionPosition() == DefinedAtom::sectionPositionEnd) {
Rui Ueyamae20474d2013-11-13 03:30:29 +0000200 assert(atom.size() == 0);
Nick Kledzik36293f62013-01-23 22:32:56 +0000201 }
202
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000203 // add to list of known atoms
204 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000205
Shankar Easwaran9316c402014-04-01 03:49:55 +0000206 if (atom.isGroupParent()) {
207 // Raise error if there exists a similar gnu linkonce section.
208 if (!maybeAddSectionGroupOrGnuLinkOnce(atom)) {
209 llvm::errs() << "SymbolTable: error while merging " << atom.name()
210 << "\n";
211 llvm::report_fatal_error("duplicate symbol error");
212 }
213 } else {
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000214 _symbolTable.add(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000215 }
Nick Kledzikabb69812012-05-31 22:34:00 +0000216
Rui Ueyama83e6acc2014-04-03 21:11:22 +0000217 // An atom that should never be dead-stripped is a dead-strip root.
218 if (_context.deadStrip() && atom.deadStrip() == DefinedAtom::deadStripNever) {
219 _deadStripRoots.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000220 }
221}
222
Rui Ueyamafd133b32013-11-21 23:41:53 +0000223void Resolver::doSharedLibraryAtom(const SharedLibraryAtom &atom) {
224 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000225 << " SharedLibraryAtom: "
226 << llvm::format("0x%09lX", &atom)
227 << ", name="
228 << atom.name()
229 << "\n");
230
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000231 // add to list of known atoms
232 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000233
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000234 // tell symbol table
235 _symbolTable.add(atom);
236}
Michael J. Spencer765792d2012-04-03 18:40:27 +0000237
Rui Ueyamafd133b32013-11-21 23:41:53 +0000238void Resolver::doAbsoluteAtom(const AbsoluteAtom &atom) {
239 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000240 << " AbsoluteAtom: "
241 << llvm::format("0x%09lX", &atom)
242 << ", name="
Rui Ueyamafd133b32013-11-21 23:41:53 +0000243 << atom.name()
Nick Kledzikabb69812012-05-31 22:34:00 +0000244 << "\n");
245
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000246 // add to list of known atoms
247 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000248
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000249 // tell symbol table
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000250 if (atom.scope() != Atom::scopeTranslationUnit)
Hemant Kulkarnif8286132012-11-05 19:13:54 +0000251 _symbolTable.add(atom);
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000252}
253
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000254// utility to add a vector of atoms
Rui Ueyamafd133b32013-11-21 23:41:53 +0000255void Resolver::addAtoms(const std::vector<const DefinedAtom *> &newAtoms) {
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000256 for (const DefinedAtom *newAtom : newAtoms)
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000257 doDefinedAtom(*newAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000258}
259
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000260// Keep adding atoms until _context.getNextFile() returns an error. This
261// function is where undefined atoms are resolved.
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000262bool Resolver::resolveUndefines() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000263 ScopedTask task(getDefaultDomain(), "resolveUndefines");
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000264
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000265 for (;;) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000266 ErrorOr<File &> file = _context.getInputGraph().getNextFile();
Rafael Espindolad28918b2014-01-08 22:00:09 +0000267 error_code ec = file.getError();
268 if (ec == InputGraphError::no_more_files)
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000269 return true;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000270 if (!file) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000271 llvm::errs() << "Error occurred in getNextFile: " << ec.message() << "\n";
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000272 return false;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000273 }
274
275 switch (file->kind()) {
276 case File::kindObject:
277 assert(!file->hasOrdinal());
278 file->setOrdinal(_context.getNextOrdinalAndIncrement());
279 handleFile(*file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000280 break;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000281 case File::kindArchiveLibrary:
282 if (!file->hasOrdinal())
283 file->setOrdinal(_context.getNextOrdinalAndIncrement());
284 handleArchiveFile(*file);
285 break;
286 case File::kindSharedLibrary:
287 if (!file->hasOrdinal())
288 file->setOrdinal(_context.getNextOrdinalAndIncrement());
289 handleSharedLibrary(*file);
290 break;
Shankar Easwaran1d3c48f2013-10-09 05:23:23 +0000291 }
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000292 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000293}
294
295// switch all references to undefined or coalesced away atoms
296// to the new defined atom
297void Resolver::updateReferences() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000298 ScopedTask task(getDefaultDomain(), "updateReferences");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000299 for (const Atom *atom : _atoms) {
300 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000301 for (const Reference *ref : *defAtom) {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000302 const Atom *newTarget = _symbolTable.replacement(ref->target());
303 const_cast<Reference *>(ref)->setTarget(newTarget);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000304 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000305 }
306 }
307}
308
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000309// For dead code stripping, recursively mark atoms "live"
Nick Kledzikbb963df2012-04-18 21:55:06 +0000310void Resolver::markLive(const Atom &atom) {
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000311 // Mark the atom is live. If it's already marked live, then stop recursion.
312 auto exists = _liveAtoms.insert(&atom);
313 if (!exists.second)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000314 return;
315
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000316 // Mark all atoms it references as live
Rui Ueyamac481b5b2013-12-26 07:02:33 +0000317 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(&atom))
318 for (const Reference *ref : *defAtom)
319 if (const Atom *target = ref->target())
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000320 markLive(*target);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000321}
322
323// remove all atoms not actually used
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000324void Resolver::deadStripOptimize() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000325 ScopedTask task(getDefaultDomain(), "deadStripOptimize");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000326 // only do this optimization with -dead_strip
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000327 if (!_context.deadStrip())
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000328 return;
Rui Ueyama841016a2013-12-26 08:11:06 +0000329 assert(_liveAtoms.empty());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000330
Nick Kledzikbb963df2012-04-18 21:55:06 +0000331 // By default, shared libraries are built with all globals as dead strip roots
Rui Ueyama517f0d92014-04-03 21:16:37 +0000332 if (_context.globalsAreDeadStripRoots())
333 for (const Atom *atom : _atoms)
334 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
335 if (defAtom->scope() == DefinedAtom::scopeGlobal)
336 _deadStripRoots.insert(defAtom);
Nick Kledzikabb69812012-05-31 22:34:00 +0000337
Joerg Sonnenberger03ed4cf2014-01-03 18:43:32 +0000338 // Or, use list of names that are dead strip roots.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000339 for (const StringRef &name : _context.deadStripRoots()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000340 const Atom *symAtom = _symbolTable.findByName(name);
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000341 assert(symAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000342 _deadStripRoots.insert(symAtom);
343 }
344
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000345 // mark all roots as live, and recursively all atoms they reference
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000346 for (const Atom *dsrAtom : _deadStripRoots)
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000347 markLive(*dsrAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000348
349 // now remove all non-live atoms from _atoms
Rui Ueyama450d9872014-04-03 22:24:40 +0000350 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), [&](const Atom *a) {
351 return _liveAtoms.count(a) == 0;
352 }),
353 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000354}
355
356// error out if some undefines remain
Rui Ueyama43a589c2014-04-02 06:38:46 +0000357bool Resolver::checkUndefines() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000358 // build vector of remaining undefined symbols
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000359 std::vector<const UndefinedAtom *> undefinedAtoms = _symbolTable.undefines();
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000360 if (_context.deadStrip()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000361 // When dead code stripping, we don't care if dead atoms are undefined.
Rui Ueyama450d9872014-04-03 22:24:40 +0000362 undefinedAtoms.erase(
363 std::remove_if(undefinedAtoms.begin(), undefinedAtoms.end(),
364 [&](const Atom *a) { return _liveAtoms.count(a) == 0; }),
365 undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000366 }
367
Nick Kledzikbb963df2012-04-18 21:55:06 +0000368 // error message about missing symbols
Nick Kledzikc314b462013-04-04 18:59:24 +0000369 if (!undefinedAtoms.empty()) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000370 // FIXME: need diagnostics interface for writing error messages
Nick Kledzikc314b462013-04-04 18:59:24 +0000371 bool foundUndefines = false;
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000372 for (const UndefinedAtom *undefAtom : undefinedAtoms) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000373 const File &f = undefAtom->file();
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000374
375 // Skip over a weak symbol.
376 if (undefAtom->canBeNull() != UndefinedAtom::canBeNullNever)
377 continue;
378
379 // If this is a library and undefined symbols are allowed on the
380 // target platform, skip over it.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000381 if (isa<SharedLibraryFile>(f) && _context.allowShlibUndefines())
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000382 continue;
383
Rui Ueyama6f3254962013-09-12 21:42:52 +0000384 // If the undefine is coalesced away, skip over it.
385 if (_symbolTable.replacement(undefAtom) != undefAtom)
386 continue;
387
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000388 // Seems like this symbol is undefined. Warn that.
389 foundUndefines = true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000390 if (_context.printRemainingUndefines()) {
Rui Ueyama8d7a6f22014-01-18 00:57:40 +0000391 llvm::errs() << "Undefined symbol: " << undefAtom->file().path()
392 << ": " << undefAtom->name() << "\n";
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000393 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000394 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000395 if (foundUndefines) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000396 if (_context.printRemainingUndefines())
Nick Kledzikc314b462013-04-04 18:59:24 +0000397 llvm::errs() << "symbol(s) not found\n";
398 return true;
399 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000400 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000401 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000402}
403
404// remove from _atoms all coaleseced away atoms
405void Resolver::removeCoalescedAwayAtoms() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000406 ScopedTask task(getDefaultDomain(), "removeCoalescedAwayAtoms");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000407 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
Rui Ueyamafd133b32013-11-21 23:41:53 +0000408 AtomCoalescedAway(_symbolTable)),
409 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000410}
411
Nick Kledzikc314b462013-04-04 18:59:24 +0000412bool Resolver::resolve() {
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000413 if (!resolveUndefines())
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000414 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000415 updateReferences();
416 deadStripOptimize();
417 if (checkUndefines())
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000418 if (!_context.allowRemainingUndefines())
Rui Ueyamaee366042013-10-11 06:16:33 +0000419 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000420 removeCoalescedAwayAtoms();
421 _result->addAtoms(_atoms);
Rui Ueyamaee366042013-10-11 06:16:33 +0000422 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000423}
424
Rui Ueyamafd133b32013-11-21 23:41:53 +0000425void Resolver::MergedFile::addAtom(const Atom &atom) {
Rui Ueyama251e68b2014-01-18 00:38:44 +0000426 if (auto *def = dyn_cast<DefinedAtom>(&atom)) {
427 _definedAtoms._atoms.push_back(def);
428 } else if (auto *undef = dyn_cast<UndefinedAtom>(&atom)) {
429 _undefinedAtoms._atoms.push_back(undef);
430 } else if (auto *shared = dyn_cast<SharedLibraryAtom>(&atom)) {
431 _sharedLibraryAtoms._atoms.push_back(shared);
432 } else if (auto *abs = dyn_cast<AbsoluteAtom>(&atom)) {
433 _absoluteAtoms._atoms.push_back(abs);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000434 } else {
Nick Kledzik751eb3d2012-06-15 20:37:24 +0000435 llvm_unreachable("atom has unknown definition kind");
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000436 }
437}
438
Nick Kledzik36293f62013-01-23 22:32:56 +0000439MutableFile::DefinedAtomRange Resolver::MergedFile::definedAtoms() {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000440 return range<std::vector<const DefinedAtom *>::iterator>(
441 _definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
Nick Kledzik36293f62013-01-23 22:32:56 +0000442}
443
Rui Ueyamafd133b32013-11-21 23:41:53 +0000444void Resolver::MergedFile::addAtoms(std::vector<const Atom *> &all) {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000445 ScopedTask task(getDefaultDomain(), "addAtoms");
Nick Kledzikabb69812012-05-31 22:34:00 +0000446 DEBUG_WITH_TYPE("resolver", llvm::dbgs() << "Resolver final atom list:\n");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000447 for (const Atom *atom : all) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000448 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
449 << llvm::format(" 0x%09lX", atom)
450 << ", name="
451 << atom->name()
452 << "\n");
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000453 addAtom(*atom);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000454 }
455}
456
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000457} // namespace lld