blob: 417442de0f23c72e48571979f632970d529b7c05 [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
Shankar Easwarana96f3a32013-10-07 02:47:09 +000032void Resolver::handleFile(const File &file) {
Rui Ueyama2a522512014-05-14 17:29:27 +000033 bool undefAdded = false;
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000034 for (const DefinedAtom *atom : file.defined())
Shankar Easwarana96f3a32013-10-07 02:47:09 +000035 doDefinedAtom(*atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +000036 for (const UndefinedAtom *atom : file.undefined())
Rui Ueyama2a522512014-05-14 17:29:27 +000037 if (doUndefinedAtom(*atom))
38 undefAdded = true;
Rui Ueyama94575102014-04-03 20:54:47 +000039 for (const SharedLibraryAtom *atom : file.sharedLibrary())
40 doSharedLibraryAtom(*atom);
41 for (const AbsoluteAtom *atom : file.absolute())
42 doAbsoluteAtom(*atom);
Rui Ueyamaedd5c0a2014-04-02 21:02:44 +000043
Rui Ueyama6aa91cf2014-04-03 21:00:03 +000044 // Notify the input file manager of the fact that we have made some progress
45 // on linking using the current input file. It may want to know the fact for
46 // --start-group/--end-group.
Rui Ueyama2a522512014-05-14 17:29:27 +000047 if (undefAdded) {
48 _context.getInputGraph().notifyProgress();
49 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000050}
51
Rui Ueyama0b8e0532014-04-04 00:39:37 +000052void Resolver::forEachUndefines(bool searchForOverrides,
53 UndefCallback callback) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000054 // Handle normal archives
Rui Ueyama992fdc02014-04-04 01:22:51 +000055 unsigned undefineGenCount = 0;
Shankar Easwarana96f3a32013-10-07 02:47:09 +000056 do {
57 undefineGenCount = _symbolTable.size();
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000058 for (const UndefinedAtom *undefAtom : _symbolTable.undefines()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000059 StringRef undefName = undefAtom->name();
60 // load for previous undefine may also have loaded this undefine
Rui Ueyama3429d012013-11-15 04:58:54 +000061 if (!_symbolTable.isDefined(undefName))
62 callback(undefName, false);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000063 }
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000064
Shankar Easwarana96f3a32013-10-07 02:47:09 +000065 // search libraries for overrides of common symbols
Rui Ueyama3429d012013-11-15 04:58:54 +000066 if (searchForOverrides) {
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +000067 for (StringRef tentDefName : _symbolTable.tentativeDefinitions()) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000068 // Load for previous tentative may also have loaded
69 // something that overrode this tentative, so always check.
70 const Atom *curAtom = _symbolTable.findByName(tentDefName);
71 assert(curAtom != nullptr);
72 if (const DefinedAtom *curDefAtom = dyn_cast<DefinedAtom>(curAtom)) {
Rui Ueyama3429d012013-11-15 04:58:54 +000073 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative)
74 callback(tentDefName, true);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000075 }
76 }
77 }
78 } while (undefineGenCount != _symbolTable.size());
79}
80
Rui Ueyama3429d012013-11-15 04:58:54 +000081void Resolver::handleArchiveFile(const File &file) {
Rui Ueyama0ca88e32014-04-02 06:54:43 +000082 const ArchiveLibraryFile *archiveFile = cast<ArchiveLibraryFile>(&file);
Rui Ueyama0b8e0532014-04-04 00:39:37 +000083 bool searchForOverrides =
84 _context.searchArchivesToOverrideTentativeDefinitions();
85 forEachUndefines(searchForOverrides,
86 [&](StringRef undefName, bool dataSymbolOnly) {
Rui Ueyama3429d012013-11-15 04:58:54 +000087 if (const File *member = archiveFile->find(undefName, dataSymbolOnly)) {
88 member->setOrdinal(_context.getNextOrdinalAndIncrement());
89 handleFile(*member);
90 }
Rui Ueyama0b8e0532014-04-04 00:39:37 +000091 });
Rui Ueyama3429d012013-11-15 04:58:54 +000092}
Shankar Easwarana96f3a32013-10-07 02:47:09 +000093
Rui Ueyama3429d012013-11-15 04:58:54 +000094void Resolver::handleSharedLibrary(const File &file) {
Shankar Easwarana96f3a32013-10-07 02:47:09 +000095 // Add all the atoms from the shared library
Rui Ueyama0ca88e32014-04-02 06:54:43 +000096 const SharedLibraryFile *sharedLibrary = cast<SharedLibraryFile>(&file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +000097 handleFile(*sharedLibrary);
Rui Ueyama3429d012013-11-15 04:58:54 +000098 bool searchForOverrides =
99 _context.searchSharedLibrariesToOverrideTentativeDefinitions();
Rui Ueyama0b8e0532014-04-04 00:39:37 +0000100 forEachUndefines(searchForOverrides,
101 [&](StringRef undefName, bool dataSymbolOnly) {
102 if (const SharedLibraryAtom *atom =
103 sharedLibrary->exports(undefName, dataSymbolOnly))
104 doSharedLibraryAtom(*atom);
105 });
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000106}
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000107
Rui Ueyama2a522512014-05-14 17:29:27 +0000108bool Resolver::doUndefinedAtom(const UndefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000109 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
110 << " UndefinedAtom: "
111 << llvm::format("0x%09lX", &atom)
Rui Ueyama70625fb2014-04-03 22:21:59 +0000112 << ", name=" << atom.name() << "\n");
Nick Kledzikabb69812012-05-31 22:34:00 +0000113
Rui Ueyamafd133b32013-11-21 23:41:53 +0000114 // add to list of known atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000115 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000116
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000117 // tell symbol table
Rui Ueyama2a522512014-05-14 17:29:27 +0000118 bool newUndefAdded = _symbolTable.add(atom);
Rui Ueyama70625fb2014-04-03 22:21:59 +0000119
120 // If the undefined symbol has an alternative name, try to resolve the
121 // symbol with the name to give it a second chance. This feature is used
122 // for COFF "weak external" symbol.
123 if (!_symbolTable.isDefined(atom.name())) {
124 if (const UndefinedAtom *fallbackAtom = atom.fallback()) {
125 doUndefinedAtom(*fallbackAtom);
126 _symbolTable.addReplacement(&atom, fallbackAtom);
127 }
128 }
Rui Ueyama2a522512014-05-14 17:29:27 +0000129 return newUndefAdded;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000130}
131
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000132/// \brief Add the section group and the group-child reference members.
Rui Ueyama52edc492014-06-05 07:37:25 +0000133void Resolver::maybeAddSectionGroupOrGnuLinkOnce(const DefinedAtom &atom) {
Rui Ueyamafb44f5e2014-04-03 21:06:23 +0000134 // First time adding a group?
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000135 bool isFirstTime = _symbolTable.addGroup(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000136
137 if (!isFirstTime) {
138 // If duplicate symbols are allowed, select the first group.
139 if (_context.getAllowDuplicates())
Rui Ueyama52edc492014-06-05 07:37:25 +0000140 return;
141 auto *prevGroup = dyn_cast<DefinedAtom>(_symbolTable.findGroup(atom.name()));
Shankar Easwaran9316c402014-04-01 03:49:55 +0000142 assert(prevGroup &&
143 "Internal Error: The group atom could only be a defined atom");
144 // The atoms should be of the same content type, reject invalid group
145 // resolution behaviors.
Rui Ueyama52edc492014-06-05 07:37:25 +0000146 if (atom.contentType() == prevGroup->contentType())
147 return;
148 llvm::errs() << "SymbolTable: error while merging " << atom.name()
149 << "\n";
150 llvm::report_fatal_error("duplicate symbol error");
151 return;
Shankar Easwaran9316c402014-04-01 03:49:55 +0000152 }
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000153
154 for (const Reference *r : atom) {
Rui Ueyama52edc492014-06-05 07:37:25 +0000155 if (r->kindNamespace() == lld::Reference::KindNamespace::all &&
156 r->kindValue() == lld::Reference::kindGroupChild) {
Rui Ueyamab461b1c2014-04-02 06:54:46 +0000157 const DefinedAtom *target = dyn_cast<DefinedAtom>(r->target());
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000158 assert(target && "Internal Error: kindGroupChild references need to "
159 "be associated with Defined Atoms only");
160 _atoms.push_back(target);
161 _symbolTable.add(*target);
162 }
163 }
164}
165
Rui Ueyama2a522512014-05-14 17:29:27 +0000166// Called on each atom when a file is added. Returns true if a given
167// atom is added to the symbol table.
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000168void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000169 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
170 << " DefinedAtom: "
171 << llvm::format("0x%09lX", &atom)
Nick Kledzik36293f62013-01-23 22:32:56 +0000172 << ", file=#"
173 << atom.file().ordinal()
174 << ", atom=#"
175 << atom.ordinal()
Nick Kledzikabb69812012-05-31 22:34:00 +0000176 << ", name="
177 << atom.name()
178 << "\n");
179
Nick Kledzik36293f62013-01-23 22:32:56 +0000180 // Verify on zero-size atoms are pinned to start or end of section.
Rui Ueyama71c02022014-04-03 22:43:42 +0000181 if (atom.sectionPosition() == DefinedAtom::sectionPositionStart ||
182 atom.sectionPosition() == DefinedAtom::sectionPositionEnd) {
Rui Ueyamae20474d2013-11-13 03:30:29 +0000183 assert(atom.size() == 0);
Nick Kledzik36293f62013-01-23 22:32:56 +0000184 }
185
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000186 // add to list of known atoms
187 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000188
Shankar Easwaran9316c402014-04-01 03:49:55 +0000189 if (atom.isGroupParent()) {
Rui Ueyama52edc492014-06-05 07:37:25 +0000190 maybeAddSectionGroupOrGnuLinkOnce(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000191 } else {
Shankar Easwaran7ac2a3d2014-03-26 16:37:13 +0000192 _symbolTable.add(atom);
Shankar Easwaran9316c402014-04-01 03:49:55 +0000193 }
Nick Kledzikabb69812012-05-31 22:34:00 +0000194
Rui Ueyama83e6acc2014-04-03 21:11:22 +0000195 // An atom that should never be dead-stripped is a dead-strip root.
196 if (_context.deadStrip() && atom.deadStrip() == DefinedAtom::deadStripNever) {
197 _deadStripRoots.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000198 }
199}
200
Rui Ueyamafd133b32013-11-21 23:41:53 +0000201void Resolver::doSharedLibraryAtom(const SharedLibraryAtom &atom) {
202 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000203 << " SharedLibraryAtom: "
204 << llvm::format("0x%09lX", &atom)
205 << ", name="
206 << atom.name()
207 << "\n");
208
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000209 // add to list of known atoms
210 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000211
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000212 // tell symbol table
213 _symbolTable.add(atom);
214}
Michael J. Spencer765792d2012-04-03 18:40:27 +0000215
Rui Ueyamafd133b32013-11-21 23:41:53 +0000216void Resolver::doAbsoluteAtom(const AbsoluteAtom &atom) {
217 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
Nick Kledzikabb69812012-05-31 22:34:00 +0000218 << " AbsoluteAtom: "
219 << llvm::format("0x%09lX", &atom)
220 << ", name="
Rui Ueyamafd133b32013-11-21 23:41:53 +0000221 << atom.name()
Nick Kledzikabb69812012-05-31 22:34:00 +0000222 << "\n");
223
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000224 // add to list of known atoms
225 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000226
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000227 // tell symbol table
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000228 if (atom.scope() != Atom::scopeTranslationUnit)
Hemant Kulkarnif8286132012-11-05 19:13:54 +0000229 _symbolTable.add(atom);
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000230}
231
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000232// utility to add a vector of atoms
Rui Ueyamafd133b32013-11-21 23:41:53 +0000233void Resolver::addAtoms(const std::vector<const DefinedAtom *> &newAtoms) {
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000234 for (const DefinedAtom *newAtom : newAtoms)
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000235 doDefinedAtom(*newAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000236}
237
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000238// Keep adding atoms until _context.getNextFile() returns an error. This
239// function is where undefined atoms are resolved.
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000240bool Resolver::resolveUndefines() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000241 ScopedTask task(getDefaultDomain(), "resolveUndefines");
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000242
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000243 for (;;) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000244 ErrorOr<File &> file = _context.getInputGraph().getNextFile();
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000245 std::error_code ec = file.getError();
Rafael Espindolad28918b2014-01-08 22:00:09 +0000246 if (ec == InputGraphError::no_more_files)
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000247 return true;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000248 if (!file) {
Rui Ueyama8bd093b2014-04-04 00:14:04 +0000249 llvm::errs() << "Error occurred in getNextFile: " << ec.message() << "\n";
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000250 return false;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000251 }
252
253 switch (file->kind()) {
254 case File::kindObject:
255 assert(!file->hasOrdinal());
256 file->setOrdinal(_context.getNextOrdinalAndIncrement());
257 handleFile(*file);
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000258 break;
Rui Ueyamad542b9e2013-10-11 03:48:06 +0000259 case File::kindArchiveLibrary:
260 if (!file->hasOrdinal())
261 file->setOrdinal(_context.getNextOrdinalAndIncrement());
262 handleArchiveFile(*file);
263 break;
264 case File::kindSharedLibrary:
265 if (!file->hasOrdinal())
266 file->setOrdinal(_context.getNextOrdinalAndIncrement());
267 handleSharedLibrary(*file);
268 break;
Shankar Easwaran1d3c48f2013-10-09 05:23:23 +0000269 }
Shankar Easwarana96f3a32013-10-07 02:47:09 +0000270 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000271}
272
273// switch all references to undefined or coalesced away atoms
274// to the new defined atom
275void Resolver::updateReferences() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000276 ScopedTask task(getDefaultDomain(), "updateReferences");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000277 for (const Atom *atom : _atoms) {
278 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000279 for (const Reference *ref : *defAtom) {
Rui Ueyama61d7f972014-06-17 16:19:33 +0000280 // A reference of type kindAssociate should't be updated.
281 // Instead, an atom having such reference will be removed
282 // if the target atom is coalesced away, so that they will
283 // go away as a group.
284 if (ref->kindNamespace() == lld::Reference::KindNamespace::all &&
285 ref->kindValue() == lld::Reference::kindAssociate) {
286 if (_symbolTable.isCoalescedAway(atom))
287 _deadAtoms.insert(ref->target());
288 continue;
289 }
Rui Ueyamafd133b32013-11-21 23:41:53 +0000290 const Atom *newTarget = _symbolTable.replacement(ref->target());
291 const_cast<Reference *>(ref)->setTarget(newTarget);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000292 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000293 }
294 }
295}
296
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000297// For dead code stripping, recursively mark atoms "live"
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000298void Resolver::markLive(const Atom *atom) {
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000299 // Mark the atom is live. If it's already marked live, then stop recursion.
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000300 auto exists = _liveAtoms.insert(atom);
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000301 if (!exists.second)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000302 return;
303
Rui Ueyamae5531fa2013-12-26 07:13:28 +0000304 // Mark all atoms it references as live
Rui Ueyama23487e82014-06-03 01:59:02 +0000305 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom)) {
Rui Ueyamac481b5b2013-12-26 07:02:33 +0000306 for (const Reference *ref : *defAtom)
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000307 markLive(ref->target());
Rui Ueyamad20d44f2014-09-26 23:21:10 +0000308 for (const Atom *target : _reverseRef.lookup(defAtom))
309 markLive(target);
Rui Ueyama23487e82014-06-03 01:59:02 +0000310 }
311}
312
313static bool isBackref(const Reference *ref) {
Rui Ueyama9aee0502014-06-03 03:07:49 +0000314 if (ref->kindNamespace() != lld::Reference::KindNamespace::all)
315 return false;
316 return (ref->kindValue() == lld::Reference::kindLayoutBefore ||
317 ref->kindValue() == lld::Reference::kindGroupChild);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000318}
319
320// remove all atoms not actually used
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000321void Resolver::deadStripOptimize() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000322 ScopedTask task(getDefaultDomain(), "deadStripOptimize");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000323 // only do this optimization with -dead_strip
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000324 if (!_context.deadStrip())
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000325 return;
Rui Ueyama23487e82014-06-03 01:59:02 +0000326
327 // Some type of references prevent referring atoms to be dead-striped.
328 // Make a reverse map of such references before traversing the graph.
329 for (const Atom *atom : _atoms)
330 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
331 for (const Reference *ref : *defAtom)
332 if (isBackref(ref))
333 _reverseRef[ref->target()].insert(atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000334
Nick Kledzikbb963df2012-04-18 21:55:06 +0000335 // By default, shared libraries are built with all globals as dead strip roots
Rui Ueyama517f0d92014-04-03 21:16:37 +0000336 if (_context.globalsAreDeadStripRoots())
337 for (const Atom *atom : _atoms)
338 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom))
339 if (defAtom->scope() == DefinedAtom::scopeGlobal)
340 _deadStripRoots.insert(defAtom);
Nick Kledzikabb69812012-05-31 22:34:00 +0000341
Joerg Sonnenberger03ed4cf2014-01-03 18:43:32 +0000342 // Or, use list of names that are dead strip roots.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000343 for (const StringRef &name : _context.deadStripRoots()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000344 const Atom *symAtom = _symbolTable.findByName(name);
Rui Ueyamaf3630fe2013-10-16 19:21:50 +0000345 assert(symAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000346 _deadStripRoots.insert(symAtom);
347 }
348
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000349 // mark all roots as live, and recursively all atoms they reference
Rui Ueyamac53b3b02013-12-10 06:19:09 +0000350 for (const Atom *dsrAtom : _deadStripRoots)
Rui Ueyama6ffe42e2014-06-02 08:06:57 +0000351 markLive(dsrAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000352
353 // now remove all non-live atoms from _atoms
Rui Ueyama450d9872014-04-03 22:24:40 +0000354 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), [&](const Atom *a) {
355 return _liveAtoms.count(a) == 0;
356 }),
357 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000358}
359
360// error out if some undefines remain
Rui Ueyama43a589c2014-04-02 06:38:46 +0000361bool Resolver::checkUndefines() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000362 // build vector of remaining undefined symbols
Rui Ueyama8dc9f0a2014-04-04 00:15:52 +0000363 std::vector<const UndefinedAtom *> undefinedAtoms = _symbolTable.undefines();
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000364 if (_context.deadStrip()) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000365 // When dead code stripping, we don't care if dead atoms are undefined.
Rui Ueyama450d9872014-04-03 22:24:40 +0000366 undefinedAtoms.erase(
367 std::remove_if(undefinedAtoms.begin(), undefinedAtoms.end(),
368 [&](const Atom *a) { return _liveAtoms.count(a) == 0; }),
369 undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000370 }
371
Nick Kledzikbb963df2012-04-18 21:55:06 +0000372 // error message about missing symbols
Nick Kledzikc314b462013-04-04 18:59:24 +0000373 if (!undefinedAtoms.empty()) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000374 // FIXME: need diagnostics interface for writing error messages
Nick Kledzikc314b462013-04-04 18:59:24 +0000375 bool foundUndefines = false;
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000376 for (const UndefinedAtom *undefAtom : undefinedAtoms) {
Shankar Easwaraneeee23e2013-04-11 02:56:30 +0000377 const File &f = undefAtom->file();
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000378
379 // Skip over a weak symbol.
380 if (undefAtom->canBeNull() != UndefinedAtom::canBeNullNever)
381 continue;
382
383 // If this is a library and undefined symbols are allowed on the
384 // target platform, skip over it.
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000385 if (isa<SharedLibraryFile>(f) && _context.allowShlibUndefines())
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000386 continue;
387
Rui Ueyama6f3254962013-09-12 21:42:52 +0000388 // If the undefine is coalesced away, skip over it.
Rui Ueyama733b45f2014-06-05 07:37:29 +0000389 if (_symbolTable.isCoalescedAway(undefAtom))
Rui Ueyama6f3254962013-09-12 21:42:52 +0000390 continue;
391
Michael J. Spencer9d70cef2013-04-24 19:00:26 +0000392 // Seems like this symbol is undefined. Warn that.
393 foundUndefines = true;
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000394 if (_context.printRemainingUndefines()) {
Rui Ueyama8d7a6f22014-01-18 00:57:40 +0000395 llvm::errs() << "Undefined symbol: " << undefAtom->file().path()
Nick Kledzikbe43d7e2014-09-30 23:15:39 +0000396 << ": " << _context.demangle(undefAtom->name())
397 << "\n";
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000398 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000399 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000400 if (foundUndefines) {
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000401 if (_context.printRemainingUndefines())
Nick Kledzikc314b462013-04-04 18:59:24 +0000402 llvm::errs() << "symbol(s) not found\n";
403 return true;
404 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000405 }
Nick Kledzikc314b462013-04-04 18:59:24 +0000406 return false;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000407}
408
409// remove from _atoms all coaleseced away atoms
410void Resolver::removeCoalescedAwayAtoms() {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000411 ScopedTask task(getDefaultDomain(), "removeCoalescedAwayAtoms");
Rui Ueyama733b45f2014-06-05 07:37:29 +0000412 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), [&](const Atom *a) {
Rui Ueyama61d7f972014-06-17 16:19:33 +0000413 return _symbolTable.isCoalescedAway(a) || _deadAtoms.count(a);
Rui Ueyama733b45f2014-06-05 07:37:29 +0000414 }),
Rui Ueyamafd133b32013-11-21 23:41:53 +0000415 _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000416}
417
Nick Kledzikc314b462013-04-04 18:59:24 +0000418bool Resolver::resolve() {
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000419 if (!resolveUndefines())
Rui Ueyama2ad117d2013-10-11 06:26:16 +0000420 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000421 updateReferences();
422 deadStripOptimize();
423 if (checkUndefines())
Rui Ueyama0ca149f2013-08-06 22:31:59 +0000424 if (!_context.allowRemainingUndefines())
Rui Ueyamaee366042013-10-11 06:16:33 +0000425 return false;
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000426 removeCoalescedAwayAtoms();
427 _result->addAtoms(_atoms);
Rui Ueyamaee366042013-10-11 06:16:33 +0000428 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000429}
430
Rui Ueyamafd133b32013-11-21 23:41:53 +0000431void Resolver::MergedFile::addAtom(const Atom &atom) {
Rui Ueyama251e68b2014-01-18 00:38:44 +0000432 if (auto *def = dyn_cast<DefinedAtom>(&atom)) {
433 _definedAtoms._atoms.push_back(def);
434 } else if (auto *undef = dyn_cast<UndefinedAtom>(&atom)) {
435 _undefinedAtoms._atoms.push_back(undef);
436 } else if (auto *shared = dyn_cast<SharedLibraryAtom>(&atom)) {
437 _sharedLibraryAtoms._atoms.push_back(shared);
438 } else if (auto *abs = dyn_cast<AbsoluteAtom>(&atom)) {
439 _absoluteAtoms._atoms.push_back(abs);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000440 } else {
Nick Kledzik751eb3d2012-06-15 20:37:24 +0000441 llvm_unreachable("atom has unknown definition kind");
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000442 }
443}
444
Nick Kledzik36293f62013-01-23 22:32:56 +0000445MutableFile::DefinedAtomRange Resolver::MergedFile::definedAtoms() {
Rui Ueyamafd133b32013-11-21 23:41:53 +0000446 return range<std::vector<const DefinedAtom *>::iterator>(
447 _definedAtoms._atoms.begin(), _definedAtoms._atoms.end());
Nick Kledzik36293f62013-01-23 22:32:56 +0000448}
449
Rui Ueyamafd133b32013-11-21 23:41:53 +0000450void Resolver::MergedFile::addAtoms(std::vector<const Atom *> &all) {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000451 ScopedTask task(getDefaultDomain(), "addAtoms");
Nick Kledzikabb69812012-05-31 22:34:00 +0000452 DEBUG_WITH_TYPE("resolver", llvm::dbgs() << "Resolver final atom list:\n");
Rui Ueyamafd133b32013-11-21 23:41:53 +0000453 for (const Atom *atom : all) {
Nick Kledzikabb69812012-05-31 22:34:00 +0000454 DEBUG_WITH_TYPE("resolver", llvm::dbgs()
455 << llvm::format(" 0x%09lX", atom)
456 << ", name="
457 << atom->name()
458 << "\n");
Rui Ueyama2b2ac912014-04-03 18:13:14 +0000459 addAtom(*atom);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000460 }
461}
462
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000463} // namespace lld