blob: e4a1b53cc334f3f628c4e05b58999b4c41b7de34 [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"
Michael J. Spencerbd66d042013-05-28 18:55:39 +000013#include "lld/Core/Instrumentation.h"
Michael J. Spencere6203a52012-04-03 18:39:40 +000014#include "lld/Core/LLVM.h"
Rui Ueyama0ca149f2013-08-06 22:31:59 +000015#include "lld/Core/LinkingContext.h"
Shankar Easwaran2b67fca2014-10-18 05:33:55 +000016#include "lld/Core/Resolver.h"
17#include "lld/Core/SharedLibraryFile.h"
18#include "lld/Core/SymbolTable.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000019#include "lld/Core/UndefinedAtom.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000020#include "llvm/Support/Debug.h"
Michael J. Spencer4586fbc2013-01-22 20:49:42 +000021#include "llvm/Support/ErrorHandling.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000022#include "llvm/Support/Format.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000023#include "llvm/Support/raw_ostream.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000024#include <algorithm>
25#include <cassert>
26#include <vector>
27
28namespace lld {
29
Shankar Easwarana96f3a32013-10-07 02:47:09 +000030void Resolver::handleFile(const File &file) {
Rui Ueyama2a522512014-05-14 17:29:27 +000031 bool undefAdded = false;
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000032 for (const DefinedAtom *atom : file.defined())
Shankar Easwarana96f3a32013-10-07 02:47:09 +000033 doDefinedAtom(*atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +000034 for (const UndefinedAtom *atom : file.undefined())
Rui Ueyama2a522512014-05-14 17:29:27 +000035 if (doUndefinedAtom(*atom))
36 undefAdded = true;
Rui Ueyama94575102014-04-03 20:54:47 +000037 for (const SharedLibraryAtom *atom : file.sharedLibrary())
38 doSharedLibraryAtom(*atom);
39 for (const AbsoluteAtom *atom : file.absolute())
40 doAbsoluteAtom(*atom);
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000041
Rui Ueyama6aa91cf2014-04-03 21:00:03 +000042 // Notify the input file manager of the fact that we have made some progress
43 // on linking using the current input file. It may want to know the fact for
44 // --start-group/--end-group.
Rui Ueyama2a522512014-05-14 17:29:27 +000045 if (undefAdded) {
46 _context.getInputGraph().notifyProgress();
47 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000048}
49
Rui Ueyama0b8e0532014-04-04 00:39:37 +000050void Resolver::forEachUndefines(bool searchForOverrides,
51 UndefCallback callback) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000052 // Handle normal archives
Rui Ueyama992fdc02014-04-04 01:22:51 +000053 unsigned undefineGenCount = 0;
Shankar Easwarana96f3a32013-10-07 02:47:09 +000054 do {
55 undefineGenCount = _symbolTable.size();
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000056 for (const UndefinedAtom *undefAtom : _symbolTable.undefines()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000057 StringRef undefName = undefAtom->name();
58 // load for previous undefine may also have loaded this undefine
Rui Ueyama3429d012013-11-15 04:58:54 +000059 if (!_symbolTable.isDefined(undefName))
60 callback(undefName, false);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000061 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000062
Shankar Easwarana96f3a32013-10-07 02:47:09 +000063 // search libraries for overrides of common symbols
Rui Ueyama3429d012013-11-15 04:58:54 +000064 if (searchForOverrides) {
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000065 for (StringRef tentDefName : _symbolTable.tentativeDefinitions()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000066 // Load for previous tentative may also have loaded
67 // something that overrode this tentative, so always check.
68 const Atom *curAtom = _symbolTable.findByName(tentDefName);
69 assert(curAtom != nullptr);
70 if (const DefinedAtom *curDefAtom = dyn_cast<DefinedAtom>(curAtom)) {
Rui Ueyama3429d012013-11-15 04:58:54 +000071 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative)
72 callback(tentDefName, true);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000073 }
74 }
75 }
76 } while (undefineGenCount != _symbolTable.size());
77}
78
Rui Ueyama3429d012013-11-15 04:58:54 +000079void Resolver::handleArchiveFile(const File &file) {
Rui Ueyama0ca88e32014-04-02 06:54:43 +000080 const ArchiveLibraryFile *archiveFile = cast<ArchiveLibraryFile>(&file);
Rui Ueyama0b8e0532014-04-04 00:39:37 +000081 bool searchForOverrides =
82 _context.searchArchivesToOverrideTentativeDefinitions();
83 forEachUndefines(searchForOverrides,
84 [&](StringRef undefName, bool dataSymbolOnly) {
Rui Ueyama3429d012013-11-15 04:58:54 +000085 if (const File *member = archiveFile->find(undefName, dataSymbolOnly)) {
86 member->setOrdinal(_context.getNextOrdinalAndIncrement());
87 handleFile(*member);
88 }
Rui Ueyama0b8e0532014-04-04 00:39:37 +000089 });
Rui Ueyama3429d012013-11-15 04:58:54 +000090}
Shankar Easwarana96f3a32013-10-07 02:47:09 +000091
Rui Ueyama3429d012013-11-15 04:58:54 +000092void Resolver::handleSharedLibrary(const File &file) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000093 // Add all the atoms from the shared library
Rui Ueyama0ca88e32014-04-02 06:54:43 +000094 const SharedLibraryFile *sharedLibrary = cast<SharedLibraryFile>(&file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000095 handleFile(*sharedLibrary);
Rui Ueyama3429d012013-11-15 04:58:54 +000096 bool searchForOverrides =
97 _context.searchSharedLibrariesToOverrideTentativeDefinitions();
Rui Ueyama0b8e0532014-04-04 00:39:37 +000098 forEachUndefines(searchForOverrides,
99 [&](StringRef undefName, bool dataSymbolOnly) {
100 if (const SharedLibraryAtom *atom =
101 sharedLibrary->exports(undefName, dataSymbolOnly))
102 doSharedLibraryAtom(*atom);
103 });
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000104}
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000105
Rui Ueyama2a522512014-05-14 17:29:27 +0000106bool Resolver::doUndefinedAtom(const UndefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000107 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
108 << " UndefinedAtom: "
109 << llvm::format("0x%09lX", &atom)
Rui Ueyama70625fb2014-04-03 22:21:59 +0000110 << ", name=" << atom.name() << "\n");
Nick Kledzikabb69812012-05-31 22:34:00 +0000111
Rui Ueyamafd133b32013-11-21 23:41:53 +0000112 // add to list of known atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000113 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000114
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000115 // tell symbol table
Rui Ueyama2a522512014-05-14 17:29:27 +0000116 bool newUndefAdded = _symbolTable.add(atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +0000117
118 // If the undefined symbol has an alternative name, try to resolve the
119 // symbol with the name to give it a second chance. This feature is used
120 // for COFF "weak external" symbol.
121 if (!_symbolTable.isDefined(atom.name())) {
122 if (const UndefinedAtom *fallbackAtom = atom.fallback()) {
123 doUndefinedAtom(*fallbackAtom);
124 _symbolTable.addReplacement(&atom, fallbackAtom);
125 }
126 }
Rui Ueyama2a522512014-05-14 17:29:27 +0000127 return newUndefAdded;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000128}
129
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000130/// \brief Add the section group and the group-child reference members.
Rui Ueyama52edc492014-06-05 07:37:25 +0000131void Resolver::maybeAddSectionGroupOrGnuLinkOnce(const DefinedAtom &atom) {
Rui Ueyamafb44f5e2014-04-03 21:06:23 +0000132 // First time adding a group?
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000133 bool isFirstTime = _symbolTable.addGroup(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000134
135 if (!isFirstTime) {
136 // If duplicate symbols are allowed, select the first group.
137 if (_context.getAllowDuplicates())
Rui Ueyama52edc492014-06-05 07:37:25 +0000138 return;
139 auto *prevGroup = dyn_cast<DefinedAtom>(_symbolTable.findGroup(atom.name()));
Shankar Easwaran9316c402014-04-01 03:49:55 +0000140 assert(prevGroup &&
141 "Internal Error: The group atom could only be a defined atom");
142 // The atoms should be of the same content type, reject invalid group
143 // resolution behaviors.
Rui Ueyama52edc492014-06-05 07:37:25 +0000144 if (atom.contentType() == prevGroup->contentType())
145 return;
146 llvm::errs() << "SymbolTable: error while merging " << atom.name()
147 << "\n";
148 llvm::report_fatal_error("duplicate symbol error");
149 return;
Shankar Easwaran9316c402014-04-01 03:49:55 +0000150 }
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000151
152 for (const Reference *r : atom) {
Rui Ueyama52edc492014-06-05 07:37:25 +0000153 if (r->kindNamespace() == lld::Reference::KindNamespace::all &&
154 r->kindValue() == lld::Reference::kindGroupChild) {
Rui Ueyamab461b1c2014-04-02 06:54:46 +0000155 const DefinedAtom *target = dyn_cast<DefinedAtom>(r->target());
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000156 assert(target && "Internal Error: kindGroupChild references need to "
157 "be associated with Defined Atoms only");
158 _atoms.push_back(target);
159 _symbolTable.add(*target);
160 }
161 }
162}
163
Rui Ueyama2a522512014-05-14 17:29:27 +0000164// Called on each atom when a file is added. Returns true if a given
165// atom is added to the symbol table.
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000166void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000167 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
168 << " DefinedAtom: "
169 << llvm::format("0x%09lX", &atom)
Nick Kledzik36293f62013-01-23 22:32:56 +0000170 << ", file=#"
171 << atom.file().ordinal()
172 << ", atom=#"
173 << atom.ordinal()
Nick Kledzikabb69812012-05-31 22:34:00 +0000174 << ", name="
175 << atom.name()
176 << "\n");
177
Nick Kledzik36293f62013-01-23 22:32:56 +0000178 // Verify on zero-size atoms are pinned to start or end of section.
Rui Ueyama71c02022014-04-03 22:43:42 +0000179 if (atom.sectionPosition() == DefinedAtom::sectionPositionStart ||
180 atom.sectionPosition() == DefinedAtom::sectionPositionEnd) {
Rui Ueyamae20474d2013-11-13 03:30:29 +0000181 assert(atom.size() == 0);
Nick Kledzik36293f62013-01-23 22:32:56 +0000182 }
183
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000184 // add to list of known atoms
185 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000186
Shankar Easwaran9316c402014-04-01 03:49:55 +0000187 if (atom.isGroupParent()) {
Rui Ueyama52edc492014-06-05 07:37:25 +0000188 maybeAddSectionGroupOrGnuLinkOnce(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000189 } else {
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000190 _symbolTable.add(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000191 }
Nick Kledzikabb69812012-05-31 22:34:00 +0000192
Rui Ueyama83e6acc2014-04-03 21:11:22 +0000193 // An atom that should never be dead-stripped is a dead-strip root.
194 if (_context.deadStrip() && atom.deadStrip() == DefinedAtom::deadStripNever) {
195 _deadStripRoots.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000196 }
197}
198
Rui Ueyamafd133b32013-11-21 23:41:53 +0000199void Resolver::doSharedLibraryAtom(const SharedLibraryAtom &atom) {
200 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000201 << " SharedLibraryAtom: "
202 << llvm::format("0x%09lX", &atom)
203 << ", name="
204 << atom.name()
205 << "\n");
206
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000207 // add to list of known atoms
208 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000209
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000210 // tell symbol table
211 _symbolTable.add(atom);
212}
Michael J. Spencer765792d2012-04-03 18:40:27 +0000213
Rui Ueyamafd133b32013-11-21 23:41:53 +0000214void Resolver::doAbsoluteAtom(const AbsoluteAtom &atom) {
215 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000216 << " AbsoluteAtom: "
217 << llvm::format("0x%09lX", &atom)
218 << ", name="
Rui Ueyamafd133b32013-11-21 23:41:53 +0000219 << atom.name()
Nick Kledzikabb69812012-05-31 22:34:00 +0000220 << "\n");
221
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000222 // add to list of known atoms
223 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000224
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000225 // tell symbol table
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000226 if (atom.scope() != Atom::scopeTranslationUnit)
Hemant Kulkarnif8286132012-11-05 19:13:54 +0000227 _symbolTable.add(atom);
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000228}
229
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000230// utility to add a vector of atoms
Rui Ueyamafd133b32013-11-21 23:41:53 +0000231void Resolver::addAtoms(const std::vector<const DefinedAtom *> &newAtoms) {
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000232 for (const DefinedAtom *newAtom : newAtoms)
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000233 doDefinedAtom(*newAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000234}
235
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000236// Keep adding atoms until _context.getNextFile() returns an error. This
237// function is where undefined atoms are resolved.
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000238bool Resolver::resolveUndefines() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000239 ScopedTask task(getDefaultDomain(), "resolveUndefines");
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000240
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000241 for (;;) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000242 ErrorOr<File &> file = _context.getInputGraph().getNextFile();
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000243 std::error_code ec = file.getError();
Rafael Espindolad28918b2014-01-08 22:00:09 +0000244 if (ec == InputGraphError::no_more_files)
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000245 return true;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000246 if (!file) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000247 llvm::errs() << "Error occurred in getNextFile: " << ec.message() << "\n";
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000248 return false;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000249 }
250
251 switch (file->kind()) {
252 case File::kindObject:
253 assert(!file->hasOrdinal());
254 file->setOrdinal(_context.getNextOrdinalAndIncrement());
255 handleFile(*file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000256 break;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000257 case File::kindArchiveLibrary:
258 if (!file->hasOrdinal())
259 file->setOrdinal(_context.getNextOrdinalAndIncrement());
260 handleArchiveFile(*file);
261 break;
262 case File::kindSharedLibrary:
263 if (!file->hasOrdinal())
264 file->setOrdinal(_context.getNextOrdinalAndIncrement());
265 handleSharedLibrary(*file);
266 break;
Shankar Easwaran1d3c48f2013-10-09 05:23:23 +0000267 }
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000268 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000269}
270
271// switch all references to undefined or coalesced away atoms
272// to the new defined atom
273void Resolver::updateReferences() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000274 ScopedTask task(getDefaultDomain(), "updateReferences");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000275 for (const Atom *atom : _atoms) {
276 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000277 for (const Reference *ref : *defAtom) {
Rui Ueyama61d7f972014-06-17 16:19:33 +0000278 // A reference of type kindAssociate should't be updated.
279 // Instead, an atom having such reference will be removed
280 // if the target atom is coalesced away, so that they will
281 // go away as a group.
282 if (ref->kindNamespace() == lld::Reference::KindNamespace::all &&
283 ref->kindValue() == lld::Reference::kindAssociate) {
284 if (_symbolTable.isCoalescedAway(atom))
285 _deadAtoms.insert(ref->target());
286 continue;
287 }
Rui Ueyamafd133b32013-11-21 23:41:53 +0000288 const Atom *newTarget = _symbolTable.replacement(ref->target());
289 const_cast<Reference *>(ref)->setTarget(newTarget);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000290 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000291 }
292 }
293}
294
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000295// For dead code stripping, recursively mark atoms "live"
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000296void Resolver::markLive(const Atom *atom) {
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000297 // Mark the atom is live. If it's already marked live, then stop recursion.
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000298 auto exists = _liveAtoms.insert(atom);
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000299 if (!exists.second)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000300 return;
301
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000302 // Mark all atoms it references as live
Rui Ueyama23487e82014-06-03 01:59:02 +0000303 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Rui Ueyamac481b5b2013-12-26 07:02:33 +0000304 for (const Reference *ref : *defAtom)
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000305 markLive(ref->target());
Rui Ueyamad20d44f2014-09-26 23:21:10 +0000306 for (const Atom *target : _reverseRef.lookup(defAtom))
307 markLive(target);
Rui Ueyama23487e82014-06-03 01:59:02 +0000308 }
309}
310
311static bool isBackref(const Reference *ref) {
Rui Ueyama9aee0502014-06-03 03:07:49 +0000312 if (ref->kindNamespace() != lld::Reference::KindNamespace::all)
313 return false;
314 return (ref->kindValue() == lld::Reference::kindLayoutBefore ||
315 ref->kindValue() == lld::Reference::kindGroupChild);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000316}
317
318// remove all atoms not actually used
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000319void Resolver::deadStripOptimize() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000320 ScopedTask task(getDefaultDomain(), "deadStripOptimize");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000321 // only do this optimization with -dead_strip
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000322 if (!_context.deadStrip())
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000323 return;
Rui Ueyama23487e82014-06-03 01:59:02 +0000324
325 // Some type of references prevent referring atoms to be dead-striped.
326 // Make a reverse map of such references before traversing the graph.
327 for (const Atom *atom : _atoms)
328 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
329 for (const Reference *ref : *defAtom)
330 if (isBackref(ref))
331 _reverseRef[ref->target()].insert(atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000332
Nick Kledzikbb963df2012-04-18 21:55:06 +0000333 // By default, shared libraries are built with all globals as dead strip roots
Rui Ueyama517f0d92014-04-03 21:16:37 +0000334 if (_context.globalsAreDeadStripRoots())
335 for (const Atom *atom : _atoms)
336 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
337 if (defAtom->scope() == DefinedAtom::scopeGlobal)
338 _deadStripRoots.insert(defAtom);
Nick Kledzikabb69812012-05-31 22:34:00 +0000339
Joerg Sonnenberger03ed4cf2014-01-03 18:43:32 +0000340 // Or, use list of names that are dead strip roots.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000341 for (const StringRef &name : _context.deadStripRoots()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000342 const Atom *symAtom = _symbolTable.findByName(name);
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000343 assert(symAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000344 _deadStripRoots.insert(symAtom);
345 }
346
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000347 // mark all roots as live, and recursively all atoms they reference
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000348 for (const Atom *dsrAtom : _deadStripRoots)
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000349 markLive(dsrAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000350
351 // now remove all non-live atoms from _atoms
Rui Ueyama450d9872014-04-03 22:24:40 +0000352 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), [&](const Atom *a) {
353 return _liveAtoms.count(a) == 0;
354 }),
355 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000356}
357
358// error out if some undefines remain
Rui Ueyama43a589c2014-04-02 06:38:46 +0000359bool Resolver::checkUndefines() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000360 // build vector of remaining undefined symbols
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000361 std::vector<const UndefinedAtom *> undefinedAtoms = _symbolTable.undefines();
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000362 if (_context.deadStrip()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000363 // When dead code stripping, we don't care if dead atoms are undefined.
Rui Ueyama450d9872014-04-03 22:24:40 +0000364 undefinedAtoms.erase(
365 std::remove_if(undefinedAtoms.begin(), undefinedAtoms.end(),
366 [&](const Atom *a) { return _liveAtoms.count(a) == 0; }),
367 undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000368 }
369
Nick Kledzikbb963df2012-04-18 21:55:06 +0000370 // error message about missing symbols
Nick Kledzikc314b462013-04-04 18:59:24 +0000371 if (!undefinedAtoms.empty()) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000372 // FIXME: need diagnostics interface for writing error messages
Nick Kledzikc314b462013-04-04 18:59:24 +0000373 bool foundUndefines = false;
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000374 for (const UndefinedAtom *undefAtom : undefinedAtoms) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000375 const File &f = undefAtom->file();
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000376
377 // Skip over a weak symbol.
378 if (undefAtom->canBeNull() != UndefinedAtom::canBeNullNever)
379 continue;
380
381 // If this is a library and undefined symbols are allowed on the
382 // target platform, skip over it.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000383 if (isa<SharedLibraryFile>(f) && _context.allowShlibUndefines())
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000384 continue;
385
Rui Ueyama6f3254962013-09-12 21:42:52 +0000386 // If the undefine is coalesced away, skip over it.
Rui Ueyama733b45f2014-06-05 07:37:29 +0000387 if (_symbolTable.isCoalescedAway(undefAtom))
Rui Ueyama6f3254962013-09-12 21:42:52 +0000388 continue;
389
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000390 // Seems like this symbol is undefined. Warn that.
391 foundUndefines = true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000392 if (_context.printRemainingUndefines()) {
Rui Ueyama8d7a6f22014-01-18 00:57:40 +0000393 llvm::errs() << "Undefined symbol: " << undefAtom->file().path()
Nick Kledzikbe43d7e2014-09-30 23:15:39 +0000394 << ": " << _context.demangle(undefAtom->name())
395 << "\n";
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000396 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000397 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000398 if (foundUndefines) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000399 if (_context.printRemainingUndefines())
Nick Kledzikc314b462013-04-04 18:59:24 +0000400 llvm::errs() << "symbol(s) not found\n";
401 return true;
402 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000403 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000404 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000405}
406
407// remove from _atoms all coaleseced away atoms
408void Resolver::removeCoalescedAwayAtoms() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000409 ScopedTask task(getDefaultDomain(), "removeCoalescedAwayAtoms");
Rui Ueyama733b45f2014-06-05 07:37:29 +0000410 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), [&](const Atom *a) {
Rui Ueyama61d7f972014-06-17 16:19:33 +0000411 return _symbolTable.isCoalescedAway(a) || _deadAtoms.count(a);
Rui Ueyama733b45f2014-06-05 07:37:29 +0000412 }),
Rui Ueyamafd133b32013-11-21 23:41:53 +0000413 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000414}
415
Nick Kledzikc314b462013-04-04 18:59:24 +0000416bool Resolver::resolve() {
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000417 if (!resolveUndefines())
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000418 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000419 updateReferences();
420 deadStripOptimize();
421 if (checkUndefines())
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000422 if (!_context.allowRemainingUndefines())
Rui Ueyamaee366042013-10-11 06:16:33 +0000423 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000424 removeCoalescedAwayAtoms();
425 _result->addAtoms(_atoms);
Rui Ueyamaee366042013-10-11 06:16:33 +0000426 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000427}
428
Rui Ueyamafd133b32013-11-21 23:41:53 +0000429void Resolver::MergedFile::addAtom(const Atom &atom) {
Rui Ueyama251e68b2014-01-18 00:38:44 +0000430 if (auto *def = dyn_cast<DefinedAtom>(&atom)) {
431 _definedAtoms._atoms.push_back(def);
432 } else if (auto *undef = dyn_cast<UndefinedAtom>(&atom)) {
433 _undefinedAtoms._atoms.push_back(undef);
434 } else if (auto *shared = dyn_cast<SharedLibraryAtom>(&atom)) {
435 _sharedLibraryAtoms._atoms.push_back(shared);
436 } else if (auto *abs = dyn_cast<AbsoluteAtom>(&atom)) {
437 _absoluteAtoms._atoms.push_back(abs);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000438 } else {
Nick Kledzik751eb3d2012-06-15 20:37:24 +0000439 llvm_unreachable("atom has unknown definition kind");
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000440 }
441}
442
Nick Kledzik36293f62013-01-23 22:32:56 +0000443MutableFile::DefinedAtomRange Resolver::MergedFile::definedAtoms() {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000444 return range<std::vector<const DefinedAtom *>::iterator>(
445 _definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
Nick Kledzik36293f62013-01-23 22:32:56 +0000446}
447
Tim Northoverf98b1c92014-11-04 21:57:32 +0000448void Resolver::MergedFile::removeDefinedAtomsIf(
449 std::function<bool(const DefinedAtom *)> pred) {
450 auto &atoms = _definedAtoms._atoms;
451 auto newEnd = std::remove_if(atoms.begin(), atoms.end(), pred);
452 atoms.erase(newEnd, atoms.end());
453}
454
Rui Ueyamafd133b32013-11-21 23:41:53 +0000455void Resolver::MergedFile::addAtoms(std::vector<const Atom *> &all) {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000456 ScopedTask task(getDefaultDomain(), "addAtoms");
Nick Kledzikabb69812012-05-31 22:34:00 +0000457 DEBUG_WITH_TYPE("resolver", llvm::dbgs() << "Resolver final atom list:\n");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000458 for (const Atom *atom : all) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000459 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
460 << llvm::format(" 0x%09lX", atom)
461 << ", name="
462 << atom->name()
463 << "\n");
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000464 addAtom(*atom);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000465 }
466}
467
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000468} // namespace lld