blob: 3661e2aeadd3a03210d6d5eb8db107fcd323c473 [file] [log] [blame]
Reid Spencerc408c452004-11-12 20:34:32 +00001//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
John Criswell71478b72003-09-19 20:24:23 +00009//
Chris Lattnera58d2be2003-09-30 03:24:28 +000010// This file contains routines to handle linking together LLVM bytecode files,
11// and to handle annoying things like static libraries.
John Criswell71478b72003-09-19 20:24:23 +000012//
13//===----------------------------------------------------------------------===//
14
Reid Spencer8bbb17a2004-11-14 22:02:27 +000015#include "llvm/Linker.h"
John Criswell71478b72003-09-19 20:24:23 +000016#include "llvm/Module.h"
Reid Spencer8bbb17a2004-11-14 22:02:27 +000017#include "llvm/ModuleProvider.h"
Reid Spencer3cf2c322004-11-19 03:13:25 +000018#include "llvm/ADT/SetOperations.h"
John Criswell71478b72003-09-19 20:24:23 +000019#include "llvm/Bytecode/Reader.h"
Reid Spencer8bbb17a2004-11-14 22:02:27 +000020#include "llvm/Bytecode/Archive.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Config/config.h"
Reid Spencer7dde0e32004-12-13 02:59:29 +000022//#include "llvm/Support/SystemUtils.h"
23//#include <algorithm>
24//#include <memory>
John Criswell71478b72003-09-19 20:24:23 +000025#include <set>
Reid Spencer7dde0e32004-12-13 02:59:29 +000026#include <iostream>
27
Chris Lattner6cc8ca92003-11-28 07:44:09 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Brian Gaeke3b3640a2003-11-05 22:12:52 +000030/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
31/// name of each externally-visible symbol defined in M.
Misha Brukman5208ba12003-09-30 18:09:32 +000032///
Reid Spencer7dde0e32004-12-13 02:59:29 +000033static void
34GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000035 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
36 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
37 DefinedSymbols.insert(I->getName());
38 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
39 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
40 DefinedSymbols.insert(I->getName());
41}
42
Misha Brukman5208ba12003-09-30 18:09:32 +000043/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
44/// exist in an LLVM module. This is a bit tricky because there may be two
45/// symbols with the same name but different LLVM types that will be resolved to
46/// each other but aren't currently (thus we need to treat it as resolved).
47///
48/// Inputs:
49/// M - The module in which to find undefined symbols.
50///
51/// Outputs:
52/// UndefinedSymbols - A set of C++ strings containing the name of all
53/// undefined symbols.
54///
Reid Spencer7dde0e32004-12-13 02:59:29 +000055static void
56GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000057 std::set<std::string> DefinedSymbols;
Reid Spencer3cf2c322004-11-19 03:13:25 +000058 UndefinedSymbols.clear();
John Criswell71478b72003-09-19 20:24:23 +000059
60 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
61 if (I->hasName()) {
62 if (I->isExternal())
63 UndefinedSymbols.insert(I->getName());
64 else if (!I->hasInternalLinkage())
65 DefinedSymbols.insert(I->getName());
66 }
67 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
68 if (I->hasName()) {
69 if (I->isExternal())
70 UndefinedSymbols.insert(I->getName());
71 else if (!I->hasInternalLinkage())
72 DefinedSymbols.insert(I->getName());
73 }
74
75 // Prune out any defined symbols from the undefined symbols set...
76 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
77 I != UndefinedSymbols.end(); )
78 if (DefinedSymbols.count(*I))
79 UndefinedSymbols.erase(I++); // This symbol really is defined!
80 else
81 ++I; // Keep this symbol in the undefined symbols list
82}
83
Misha Brukman5208ba12003-09-30 18:09:32 +000084/// LinkInArchive - opens an archive library and link in all objects which
85/// provide symbols that are currently undefined.
86///
87/// Inputs:
Misha Brukman5208ba12003-09-30 18:09:32 +000088/// Filename - The pathname of the archive.
Misha Brukman5208ba12003-09-30 18:09:32 +000089///
90/// Return Value:
91/// TRUE - An error occurred.
92/// FALSE - No errors.
Reid Spencer7dde0e32004-12-13 02:59:29 +000093bool
94Linker::LinkInArchive(const sys::Path &Filename) {
95
96 // Make sure this is an archive file we're dealing with
97 if (!Filename.isArchive())
98 return error("File '" + Filename.toString() + "' is not an archive.");
99
100 // Open the archive file
101 verbose("Linking archive file '" + Filename.toString() + "'");
102
John Criswell71478b72003-09-19 20:24:23 +0000103 // Find all of the symbols currently undefined in the bytecode program.
104 // If all the symbols are defined, the program is complete, and there is
105 // no reason to link in any archive files.
John Criswell71478b72003-09-19 20:24:23 +0000106 std::set<std::string> UndefinedSymbols;
Reid Spencer7dde0e32004-12-13 02:59:29 +0000107 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencer3cf2c322004-11-19 03:13:25 +0000108
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000109 if (UndefinedSymbols.empty()) {
Reid Spencer7dde0e32004-12-13 02:59:29 +0000110 verbose("No symbols undefined, skipping library '" +
111 Filename.toString() + "'");
John Criswell71478b72003-09-19 20:24:23 +0000112 return false; // No need to link anything in!
113 }
114
Reid Spencer7dde0e32004-12-13 02:59:29 +0000115 std::string ErrMsg;
Reid Spencer99d36042004-11-16 06:47:41 +0000116 std::auto_ptr<Archive> AutoArch (
Reid Spencer7dde0e32004-12-13 02:59:29 +0000117 Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
Reid Spencer99d36042004-11-16 06:47:41 +0000118
119 Archive* arch = AutoArch.get();
John Criswell71478b72003-09-19 20:24:23 +0000120
Reid Spencer7dde0e32004-12-13 02:59:29 +0000121 if (!arch)
122 return error("Cannot read archive '" + Filename.toString() +
123 "': " + ErrMsg);
124
Reid Spencer3cf2c322004-11-19 03:13:25 +0000125 // Save a set of symbols that are not defined by the archive. Since we're
126 // entering a loop, there's no point searching for these multiple times. This
127 // variable is used to "set_subtract" from the set of undefined symbols.
128 std::set<std::string> NotDefinedByArchive;
129
John Criswell71478b72003-09-19 20:24:23 +0000130 // While we are linking in object files, loop.
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000131 while (true) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000132
133 // Find the modules we need to link into the target module
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000134 std::set<ModuleProvider*> Modules;
Reid Spencer99d36042004-11-16 06:47:41 +0000135 arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
John Criswell71478b72003-09-19 20:24:23 +0000136
Reid Spencer3cf2c322004-11-19 03:13:25 +0000137 // If we didn't find any more modules to link this time, we are done
138 // searching this archive.
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000139 if (Modules.empty())
140 break;
John Criswell71478b72003-09-19 20:24:23 +0000141
Reid Spencer3cf2c322004-11-19 03:13:25 +0000142 // Any symbols remaining in UndefinedSymbols after
143 // findModulesDefiningSymbols are ones that the archive does not define. So
144 // we add them to the NotDefinedByArchive variable now.
145 NotDefinedByArchive.insert(UndefinedSymbols.begin(),
Reid Spencerb9371ce2004-11-19 03:27:05 +0000146 UndefinedSymbols.end());
Reid Spencer3cf2c322004-11-19 03:13:25 +0000147
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000148 // Loop over all the ModuleProviders that we got back from the archive
149 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
150 I != E; ++I) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000151
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000152 // Get the module we must link in.
Reid Spencer99d36042004-11-16 06:47:41 +0000153 std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
Reid Spencer99d36042004-11-16 06:47:41 +0000154 Module* aModule = AutoModule.get();
155
156 // Link it in
Reid Spencer7dde0e32004-12-13 02:59:29 +0000157 if (this->LinkInModule(aModule))
158 return error("Cannot link in module '" +
159 aModule->getModuleIdentifier() + "': " + Error);
John Criswell71478b72003-09-19 20:24:23 +0000160 }
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000161
Reid Spencer3cf2c322004-11-19 03:13:25 +0000162 // Get the undefined symbols from the aggregate module. This recomputes the
163 // symbols we still need after the new modules have been linked in.
Reid Spencer7dde0e32004-12-13 02:59:29 +0000164 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencer3cf2c322004-11-19 03:13:25 +0000165
166 // At this point we have two sets of undefined symbols: UndefinedSymbols
167 // which holds the undefined symbols from all the modules, and
168 // NotDefinedByArchive which holds symbols we know the archive doesn't
169 // define. There's no point searching for symbols that we won't find in the
170 // archive so we subtract these sets.
171 set_subtract<std::set<std::string>,std::set<std::string> >(
172 UndefinedSymbols,NotDefinedByArchive);
173
174 // If there's no symbols left, no point in continuing to search the
175 // archive.
176 if (UndefinedSymbols.empty())
177 break;
John Criswell71478b72003-09-19 20:24:23 +0000178 }
179
180 return false;
181}