blob: b73d80adc7039f2f10f4517ad12e19894c0c27d9 [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 Ueyama2a522512014-05-14 17:29:27 +000051 bool undefAdded = false;
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000052 for (const DefinedAtom *atom : file.defined())
Shankar Easwarana96f3a32013-10-07 02:47:09 +000053 doDefinedAtom(*atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +000054 for (const UndefinedAtom *atom : file.undefined())
Rui Ueyama2a522512014-05-14 17:29:27 +000055 if (doUndefinedAtom(*atom))
56 undefAdded = true;
Rui Ueyama94575102014-04-03 20:54:47 +000057 for (const SharedLibraryAtom *atom : file.sharedLibrary())
58 doSharedLibraryAtom(*atom);
59 for (const AbsoluteAtom *atom : file.absolute())
60 doAbsoluteAtom(*atom);
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000061
Rui Ueyama6aa91cf2014-04-03 21:00:03 +000062 // Notify the input file manager of the fact that we have made some progress
63 // on linking using the current input file. It may want to know the fact for
64 // --start-group/--end-group.
Rui Ueyama2a522512014-05-14 17:29:27 +000065 if (undefAdded) {
66 _context.getInputGraph().notifyProgress();
67 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000068}
69
Rui Ueyama0b8e0532014-04-04 00:39:37 +000070void Resolver::forEachUndefines(bool searchForOverrides,
71 UndefCallback callback) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000072 // Handle normal archives
Rui Ueyama992fdc02014-04-04 01:22:51 +000073 unsigned undefineGenCount = 0;
Shankar Easwarana96f3a32013-10-07 02:47:09 +000074 do {
75 undefineGenCount = _symbolTable.size();
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000076 for (const UndefinedAtom *undefAtom : _symbolTable.undefines()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000077 StringRef undefName = undefAtom->name();
78 // load for previous undefine may also have loaded this undefine
Rui Ueyama3429d012013-11-15 04:58:54 +000079 if (!_symbolTable.isDefined(undefName))
80 callback(undefName, false);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000081 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000082
Shankar Easwarana96f3a32013-10-07 02:47:09 +000083 // search libraries for overrides of common symbols
Rui Ueyama3429d012013-11-15 04:58:54 +000084 if (searchForOverrides) {
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000085 for (StringRef tentDefName : _symbolTable.tentativeDefinitions()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000086 // Load for previous tentative may also have loaded
87 // something that overrode this tentative, so always check.
88 const Atom *curAtom = _symbolTable.findByName(tentDefName);
89 assert(curAtom != nullptr);
90 if (const DefinedAtom *curDefAtom = dyn_cast<DefinedAtom>(curAtom)) {
Rui Ueyama3429d012013-11-15 04:58:54 +000091 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative)
92 callback(tentDefName, true);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000093 }
94 }
95 }
96 } while (undefineGenCount != _symbolTable.size());
97}
98
Rui Ueyama3429d012013-11-15 04:58:54 +000099void Resolver::handleArchiveFile(const File &file) {
Rui Ueyama0ca88e32014-04-02 06:54:43 +0000100 const ArchiveLibraryFile *archiveFile = cast<ArchiveLibraryFile>(&file);
Rui Ueyama0b8e0532014-04-04 00:39:37 +0000101 bool searchForOverrides =
102 _context.searchArchivesToOverrideTentativeDefinitions();
103 forEachUndefines(searchForOverrides,
104 [&](StringRef undefName, bool dataSymbolOnly) {
Rui Ueyama3429d012013-11-15 04:58:54 +0000105 if (const File *member = archiveFile->find(undefName, dataSymbolOnly)) {
106 member->setOrdinal(_context.getNextOrdinalAndIncrement());
107 handleFile(*member);
108 }
Rui Ueyama0b8e0532014-04-04 00:39:37 +0000109 });
Rui Ueyama3429d012013-11-15 04:58:54 +0000110}
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000111
Rui Ueyama3429d012013-11-15 04:58:54 +0000112void Resolver::handleSharedLibrary(const File &file) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000113 // Add all the atoms from the shared library
Rui Ueyama0ca88e32014-04-02 06:54:43 +0000114 const SharedLibraryFile *sharedLibrary = cast<SharedLibraryFile>(&file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000115 handleFile(*sharedLibrary);
Rui Ueyama3429d012013-11-15 04:58:54 +0000116 bool searchForOverrides =
117 _context.searchSharedLibrariesToOverrideTentativeDefinitions();
Rui Ueyama0b8e0532014-04-04 00:39:37 +0000118 forEachUndefines(searchForOverrides,
119 [&](StringRef undefName, bool dataSymbolOnly) {
120 if (const SharedLibraryAtom *atom =
121 sharedLibrary->exports(undefName, dataSymbolOnly))
122 doSharedLibraryAtom(*atom);
123 });
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000124}
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000125
Rui Ueyama2a522512014-05-14 17:29:27 +0000126bool Resolver::doUndefinedAtom(const UndefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000127 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
128 << " UndefinedAtom: "
129 << llvm::format("0x%09lX", &atom)
Rui Ueyama70625fb2014-04-03 22:21:59 +0000130 << ", name=" << atom.name() << "\n");
Nick Kledzikabb69812012-05-31 22:34:00 +0000131
Rui Ueyamafd133b32013-11-21 23:41:53 +0000132 // add to list of known atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000133 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000134
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000135 // tell symbol table
Rui Ueyama2a522512014-05-14 17:29:27 +0000136 bool newUndefAdded = _symbolTable.add(atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +0000137
138 // If the undefined symbol has an alternative name, try to resolve the
139 // symbol with the name to give it a second chance. This feature is used
140 // for COFF "weak external" symbol.
141 if (!_symbolTable.isDefined(atom.name())) {
142 if (const UndefinedAtom *fallbackAtom = atom.fallback()) {
143 doUndefinedAtom(*fallbackAtom);
144 _symbolTable.addReplacement(&atom, fallbackAtom);
145 }
146 }
Rui Ueyama2a522512014-05-14 17:29:27 +0000147 return newUndefAdded;
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.
Rui Ueyama20822d92014-05-02 22:32:01 +0000165 return atom.contentType() == prevGroup->contentType();
Shankar Easwaran9316c402014-04-01 03:49:55 +0000166 }
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000167
168 for (const Reference *r : atom) {
169 if ((r->kindNamespace() == lld::Reference::KindNamespace::all) &&
170 (r->kindValue() == lld::Reference::kindGroupChild)) {
Rui Ueyamab461b1c2014-04-02 06:54:46 +0000171 const DefinedAtom *target = dyn_cast<DefinedAtom>(r->target());
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000172 assert(target && "Internal Error: kindGroupChild references need to "
173 "be associated with Defined Atoms only");
174 _atoms.push_back(target);
175 _symbolTable.add(*target);
176 }
177 }
Shankar Easwaran9316c402014-04-01 03:49:55 +0000178 return true;
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000179}
180
Rui Ueyama2a522512014-05-14 17:29:27 +0000181// Called on each atom when a file is added. Returns true if a given
182// atom is added to the symbol table.
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000183void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000184 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
185 << " DefinedAtom: "
186 << llvm::format("0x%09lX", &atom)
Nick Kledzik36293f62013-01-23 22:32:56 +0000187 << ", file=#"
188 << atom.file().ordinal()
189 << ", atom=#"
190 << atom.ordinal()
Nick Kledzikabb69812012-05-31 22:34:00 +0000191 << ", name="
192 << atom.name()
193 << "\n");
194
Nick Kledzik36293f62013-01-23 22:32:56 +0000195 // Verify on zero-size atoms are pinned to start or end of section.
Rui Ueyama71c02022014-04-03 22:43:42 +0000196 if (atom.sectionPosition() == DefinedAtom::sectionPositionStart ||
197 atom.sectionPosition() == DefinedAtom::sectionPositionEnd) {
Rui Ueyamae20474d2013-11-13 03:30:29 +0000198 assert(atom.size() == 0);
Nick Kledzik36293f62013-01-23 22:32:56 +0000199 }
200
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000201 // add to list of known atoms
202 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000203
Shankar Easwaran9316c402014-04-01 03:49:55 +0000204 if (atom.isGroupParent()) {
205 // Raise error if there exists a similar gnu linkonce section.
206 if (!maybeAddSectionGroupOrGnuLinkOnce(atom)) {
207 llvm::errs() << "SymbolTable: error while merging " << atom.name()
208 << "\n";
209 llvm::report_fatal_error("duplicate symbol error");
210 }
211 } else {
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000212 _symbolTable.add(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000213 }
Nick Kledzikabb69812012-05-31 22:34:00 +0000214
Rui Ueyama83e6acc2014-04-03 21:11:22 +0000215 // An atom that should never be dead-stripped is a dead-strip root.
216 if (_context.deadStrip() && atom.deadStrip() == DefinedAtom::deadStripNever) {
217 _deadStripRoots.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000218 }
219}
220
Rui Ueyamafd133b32013-11-21 23:41:53 +0000221void Resolver::doSharedLibraryAtom(const SharedLibraryAtom &atom) {
222 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000223 << " SharedLibraryAtom: "
224 << llvm::format("0x%09lX", &atom)
225 << ", name="
226 << atom.name()
227 << "\n");
228
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000229 // add to list of known atoms
230 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000231
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000232 // tell symbol table
233 _symbolTable.add(atom);
234}
Michael J. Spencer765792d2012-04-03 18:40:27 +0000235
Rui Ueyamafd133b32013-11-21 23:41:53 +0000236void Resolver::doAbsoluteAtom(const AbsoluteAtom &atom) {
237 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000238 << " AbsoluteAtom: "
239 << llvm::format("0x%09lX", &atom)
240 << ", name="
Rui Ueyamafd133b32013-11-21 23:41:53 +0000241 << atom.name()
Nick Kledzikabb69812012-05-31 22:34:00 +0000242 << "\n");
243
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000244 // add to list of known atoms
245 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000246
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000247 // tell symbol table
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000248 if (atom.scope() != Atom::scopeTranslationUnit)
Hemant Kulkarnif8286132012-11-05 19:13:54 +0000249 _symbolTable.add(atom);
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000250}
251
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000252// utility to add a vector of atoms
Rui Ueyamafd133b32013-11-21 23:41:53 +0000253void Resolver::addAtoms(const std::vector<const DefinedAtom *> &newAtoms) {
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000254 for (const DefinedAtom *newAtom : newAtoms)
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000255 doDefinedAtom(*newAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000256}
257
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000258// Keep adding atoms until _context.getNextFile() returns an error. This
259// function is where undefined atoms are resolved.
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000260bool Resolver::resolveUndefines() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000261 ScopedTask task(getDefaultDomain(), "resolveUndefines");
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000262
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000263 for (;;) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000264 ErrorOr<File &> file = _context.getInputGraph().getNextFile();
Rafael Espindolad28918b2014-01-08 22:00:09 +0000265 error_code ec = file.getError();
266 if (ec == InputGraphError::no_more_files)
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000267 return true;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000268 if (!file) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000269 llvm::errs() << "Error occurred in getNextFile: " << ec.message() << "\n";
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000270 return false;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000271 }
272
273 switch (file->kind()) {
274 case File::kindObject:
275 assert(!file->hasOrdinal());
276 file->setOrdinal(_context.getNextOrdinalAndIncrement());
277 handleFile(*file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000278 break;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000279 case File::kindArchiveLibrary:
280 if (!file->hasOrdinal())
281 file->setOrdinal(_context.getNextOrdinalAndIncrement());
282 handleArchiveFile(*file);
283 break;
284 case File::kindSharedLibrary:
285 if (!file->hasOrdinal())
286 file->setOrdinal(_context.getNextOrdinalAndIncrement());
287 handleSharedLibrary(*file);
288 break;
Shankar Easwaran1d3c48f2013-10-09 05:23:23 +0000289 }
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000290 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000291}
292
293// switch all references to undefined or coalesced away atoms
294// to the new defined atom
295void Resolver::updateReferences() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000296 ScopedTask task(getDefaultDomain(), "updateReferences");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000297 for (const Atom *atom : _atoms) {
298 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000299 for (const Reference *ref : *defAtom) {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000300 const Atom *newTarget = _symbolTable.replacement(ref->target());
301 const_cast<Reference *>(ref)->setTarget(newTarget);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000302 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000303 }
304 }
305}
306
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000307// For dead code stripping, recursively mark atoms "live"
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000308void Resolver::markLive(const Atom *atom) {
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000309 // Mark the atom is live. If it's already marked live, then stop recursion.
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000310 auto exists = _liveAtoms.insert(atom);
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000311 if (!exists.second)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000312 return;
313
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000314 // Mark all atoms it references as live
Rui Ueyama23487e82014-06-03 01:59:02 +0000315 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Rui Ueyamac481b5b2013-12-26 07:02:33 +0000316 for (const Reference *ref : *defAtom)
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000317 markLive(ref->target());
Rui Ueyama23487e82014-06-03 01:59:02 +0000318 for (const Atom *target : _reverseRef[defAtom])
319 markLive(target);
320 }
321}
322
323static bool isBackref(const Reference *ref) {
324 return ref->kindNamespace() == lld::Reference::KindNamespace::all &&
325 ref->kindValue() == lld::Reference::kindLayoutBefore;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000326}
327
328// remove all atoms not actually used
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000329void Resolver::deadStripOptimize() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000330 ScopedTask task(getDefaultDomain(), "deadStripOptimize");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000331 // only do this optimization with -dead_strip
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000332 if (!_context.deadStrip())
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000333 return;
Rui Ueyama23487e82014-06-03 01:59:02 +0000334
335 // Some type of references prevent referring atoms to be dead-striped.
336 // Make a reverse map of such references before traversing the graph.
337 for (const Atom *atom : _atoms)
338 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
339 for (const Reference *ref : *defAtom)
340 if (isBackref(ref))
341 _reverseRef[ref->target()].insert(atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000342
Nick Kledzikbb963df2012-04-18 21:55:06 +0000343 // By default, shared libraries are built with all globals as dead strip roots
Rui Ueyama517f0d92014-04-03 21:16:37 +0000344 if (_context.globalsAreDeadStripRoots())
345 for (const Atom *atom : _atoms)
346 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
347 if (defAtom->scope() == DefinedAtom::scopeGlobal)
348 _deadStripRoots.insert(defAtom);
Nick Kledzikabb69812012-05-31 22:34:00 +0000349
Joerg Sonnenberger03ed4cf2014-01-03 18:43:32 +0000350 // Or, use list of names that are dead strip roots.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000351 for (const StringRef &name : _context.deadStripRoots()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000352 const Atom *symAtom = _symbolTable.findByName(name);
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000353 assert(symAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000354 _deadStripRoots.insert(symAtom);
355 }
356
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000357 // mark all roots as live, and recursively all atoms they reference
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000358 for (const Atom *dsrAtom : _deadStripRoots)
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000359 markLive(dsrAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000360
361 // now remove all non-live atoms from _atoms
Rui Ueyama450d9872014-04-03 22:24:40 +0000362 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), [&](const Atom *a) {
363 return _liveAtoms.count(a) == 0;
364 }),
365 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000366}
367
368// error out if some undefines remain
Rui Ueyama43a589c2014-04-02 06:38:46 +0000369bool Resolver::checkUndefines() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000370 // build vector of remaining undefined symbols
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000371 std::vector<const UndefinedAtom *> undefinedAtoms = _symbolTable.undefines();
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000372 if (_context.deadStrip()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000373 // When dead code stripping, we don't care if dead atoms are undefined.
Rui Ueyama450d9872014-04-03 22:24:40 +0000374 undefinedAtoms.erase(
375 std::remove_if(undefinedAtoms.begin(), undefinedAtoms.end(),
376 [&](const Atom *a) { return _liveAtoms.count(a) == 0; }),
377 undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000378 }
379
Nick Kledzikbb963df2012-04-18 21:55:06 +0000380 // error message about missing symbols
Nick Kledzikc314b462013-04-04 18:59:24 +0000381 if (!undefinedAtoms.empty()) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000382 // FIXME: need diagnostics interface for writing error messages
Nick Kledzikc314b462013-04-04 18:59:24 +0000383 bool foundUndefines = false;
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000384 for (const UndefinedAtom *undefAtom : undefinedAtoms) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000385 const File &f = undefAtom->file();
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000386
387 // Skip over a weak symbol.
388 if (undefAtom->canBeNull() != UndefinedAtom::canBeNullNever)
389 continue;
390
391 // If this is a library and undefined symbols are allowed on the
392 // target platform, skip over it.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000393 if (isa<SharedLibraryFile>(f) && _context.allowShlibUndefines())
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000394 continue;
395
Rui Ueyama6f3254962013-09-12 21:42:52 +0000396 // If the undefine is coalesced away, skip over it.
397 if (_symbolTable.replacement(undefAtom) != undefAtom)
398 continue;
399
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000400 // Seems like this symbol is undefined. Warn that.
401 foundUndefines = true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000402 if (_context.printRemainingUndefines()) {
Rui Ueyama8d7a6f22014-01-18 00:57:40 +0000403 llvm::errs() << "Undefined symbol: " << undefAtom->file().path()
404 << ": " << undefAtom->name() << "\n";
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000405 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000406 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000407 if (foundUndefines) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000408 if (_context.printRemainingUndefines())
Nick Kledzikc314b462013-04-04 18:59:24 +0000409 llvm::errs() << "symbol(s) not found\n";
410 return true;
411 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000412 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000413 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000414}
415
416// remove from _atoms all coaleseced away atoms
417void Resolver::removeCoalescedAwayAtoms() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000418 ScopedTask task(getDefaultDomain(), "removeCoalescedAwayAtoms");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000419 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
Rui Ueyamafd133b32013-11-21 23:41:53 +0000420 AtomCoalescedAway(_symbolTable)),
421 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000422}
423
Nick Kledzikc314b462013-04-04 18:59:24 +0000424bool Resolver::resolve() {
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000425 if (!resolveUndefines())
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000426 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000427 updateReferences();
428 deadStripOptimize();
429 if (checkUndefines())
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000430 if (!_context.allowRemainingUndefines())
Rui Ueyamaee366042013-10-11 06:16:33 +0000431 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000432 removeCoalescedAwayAtoms();
433 _result->addAtoms(_atoms);
Rui Ueyamaee366042013-10-11 06:16:33 +0000434 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000435}
436
Rui Ueyamafd133b32013-11-21 23:41:53 +0000437void Resolver::MergedFile::addAtom(const Atom &atom) {
Rui Ueyama251e68b2014-01-18 00:38:44 +0000438 if (auto *def = dyn_cast<DefinedAtom>(&atom)) {
439 _definedAtoms._atoms.push_back(def);
440 } else if (auto *undef = dyn_cast<UndefinedAtom>(&atom)) {
441 _undefinedAtoms._atoms.push_back(undef);
442 } else if (auto *shared = dyn_cast<SharedLibraryAtom>(&atom)) {
443 _sharedLibraryAtoms._atoms.push_back(shared);
444 } else if (auto *abs = dyn_cast<AbsoluteAtom>(&atom)) {
445 _absoluteAtoms._atoms.push_back(abs);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000446 } else {
Nick Kledzik751eb3d2012-06-15 20:37:24 +0000447 llvm_unreachable("atom has unknown definition kind");
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000448 }
449}
450
Nick Kledzik36293f62013-01-23 22:32:56 +0000451MutableFile::DefinedAtomRange Resolver::MergedFile::definedAtoms() {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000452 return range<std::vector<const DefinedAtom *>::iterator>(
453 _definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
Nick Kledzik36293f62013-01-23 22:32:56 +0000454}
455
Rui Ueyamafd133b32013-11-21 23:41:53 +0000456void Resolver::MergedFile::addAtoms(std::vector<const Atom *> &all) {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000457 ScopedTask task(getDefaultDomain(), "addAtoms");
Nick Kledzikabb69812012-05-31 22:34:00 +0000458 DEBUG_WITH_TYPE("resolver", llvm::dbgs() << "Resolver final atom list:\n");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000459 for (const Atom *atom : all) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000460 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
461 << llvm::format(" 0x%09lX", atom)
462 << ", name="
463 << atom->name()
464 << "\n");
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000465 addAtom(*atom);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000466 }
467}
468
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000469} // namespace lld