blob: 15d261e25bd8df77c9b138fcf10e229344a02bf6 [file] [log] [blame]
Reid Spencerc408c452004-11-12 20:34:32 +00001//===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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 Spencer4ca8e712004-12-20 04:15:44 +000022#include <memory>
John Criswell71478b72003-09-19 20:24:23 +000023#include <set>
Chris Lattner6cc8ca92003-11-28 07:44:09 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Brian Gaeke3b3640a2003-11-05 22:12:52 +000026/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
27/// name of each externally-visible symbol defined in M.
Misha Brukman5208ba12003-09-30 18:09:32 +000028///
Misha Brukmanf976c852005-04-21 22:55:34 +000029static void
Reid Spencer7dde0e32004-12-13 02:59:29 +000030GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000031 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
32 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
33 DefinedSymbols.insert(I->getName());
Chris Lattnerc72e5732005-03-15 23:03:34 +000034 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
35 I != E; ++I)
John Criswell71478b72003-09-19 20:24:23 +000036 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
37 DefinedSymbols.insert(I->getName());
38}
39
Misha Brukman5208ba12003-09-30 18:09:32 +000040/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
41/// exist in an LLVM module. This is a bit tricky because there may be two
42/// symbols with the same name but different LLVM types that will be resolved to
43/// each other but aren't currently (thus we need to treat it as resolved).
44///
45/// Inputs:
46/// M - The module in which to find undefined symbols.
47///
48/// Outputs:
49/// UndefinedSymbols - A set of C++ strings containing the name of all
50/// undefined symbols.
51///
Reid Spencer7dde0e32004-12-13 02:59:29 +000052static void
53GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000054 std::set<std::string> DefinedSymbols;
Reid Spencer3cf2c322004-11-19 03:13:25 +000055 UndefinedSymbols.clear();
Chris Lattnerc72e5732005-03-15 23:03:34 +000056
57 // If the program doesn't define a main, try pulling one in from a .a file.
58 // This is needed for programs where the main function is defined in an
59 // archive, such f2c'd programs.
60 Function *Main = M->getMainFunction();
61 if (Main == 0 || Main->isExternal())
62 UndefinedSymbols.insert("main");
Misha Brukmanf976c852005-04-21 22:55:34 +000063
John Criswell71478b72003-09-19 20:24:23 +000064 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
65 if (I->hasName()) {
66 if (I->isExternal())
67 UndefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000068 else if (!I->hasInternalLinkage()) {
69 assert(!I->hasDLLImportLinkage()
70 && "Found dllimported non-external symbol!");
John Criswell71478b72003-09-19 20:24:23 +000071 DefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000072 }
John Criswell71478b72003-09-19 20:24:23 +000073 }
Chris Lattnerc72e5732005-03-15 23:03:34 +000074 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
75 I != E; ++I)
John Criswell71478b72003-09-19 20:24:23 +000076 if (I->hasName()) {
77 if (I->isExternal())
78 UndefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000079 else if (!I->hasInternalLinkage()) {
80 assert(!I->hasDLLImportLinkage()
81 && "Found dllimported non-external symbol!");
John Criswell71478b72003-09-19 20:24:23 +000082 DefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000083 }
John Criswell71478b72003-09-19 20:24:23 +000084 }
Misha Brukmanf976c852005-04-21 22:55:34 +000085
John Criswell71478b72003-09-19 20:24:23 +000086 // Prune out any defined symbols from the undefined symbols set...
87 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
88 I != UndefinedSymbols.end(); )
89 if (DefinedSymbols.count(*I))
90 UndefinedSymbols.erase(I++); // This symbol really is defined!
91 else
92 ++I; // Keep this symbol in the undefined symbols list
93}
94
Misha Brukman5208ba12003-09-30 18:09:32 +000095/// LinkInArchive - opens an archive library and link in all objects which
96/// provide symbols that are currently undefined.
97///
98/// Inputs:
Misha Brukman5208ba12003-09-30 18:09:32 +000099/// Filename - The pathname of the archive.
Misha Brukman5208ba12003-09-30 18:09:32 +0000100///
101/// Return Value:
102/// TRUE - An error occurred.
103/// FALSE - No errors.
Misha Brukmanf976c852005-04-21 22:55:34 +0000104bool
Reid Spencer7dde0e32004-12-13 02:59:29 +0000105Linker::LinkInArchive(const sys::Path &Filename) {
106
107 // Make sure this is an archive file we're dealing with
108 if (!Filename.isArchive())
109 return error("File '" + Filename.toString() + "' is not an archive.");
110
111 // Open the archive file
112 verbose("Linking archive file '" + Filename.toString() + "'");
113
John Criswell71478b72003-09-19 20:24:23 +0000114 // Find all of the symbols currently undefined in the bytecode program.
115 // If all the symbols are defined, the program is complete, and there is
116 // no reason to link in any archive files.
John Criswell71478b72003-09-19 20:24:23 +0000117 std::set<std::string> UndefinedSymbols;
Reid Spencer7dde0e32004-12-13 02:59:29 +0000118 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Misha Brukmanf976c852005-04-21 22:55:34 +0000119
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000120 if (UndefinedSymbols.empty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000121 verbose("No symbols undefined, skipping library '" +
Reid Spencer7dde0e32004-12-13 02:59:29 +0000122 Filename.toString() + "'");
John Criswell71478b72003-09-19 20:24:23 +0000123 return false; // No need to link anything in!
124 }
125
Reid Spencer7dde0e32004-12-13 02:59:29 +0000126 std::string ErrMsg;
Reid Spencer99d36042004-11-16 06:47:41 +0000127 std::auto_ptr<Archive> AutoArch (
Reid Spencer7dde0e32004-12-13 02:59:29 +0000128 Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
Reid Spencer99d36042004-11-16 06:47:41 +0000129
130 Archive* arch = AutoArch.get();
John Criswell71478b72003-09-19 20:24:23 +0000131
Reid Spencer7dde0e32004-12-13 02:59:29 +0000132 if (!arch)
Misha Brukmanf976c852005-04-21 22:55:34 +0000133 return error("Cannot read archive '" + Filename.toString() +
Reid Spencer7dde0e32004-12-13 02:59:29 +0000134 "': " + ErrMsg);
135
Reid Spencer3cf2c322004-11-19 03:13:25 +0000136 // Save a set of symbols that are not defined by the archive. Since we're
137 // entering a loop, there's no point searching for these multiple times. This
138 // variable is used to "set_subtract" from the set of undefined symbols.
139 std::set<std::string> NotDefinedByArchive;
140
John Criswell71478b72003-09-19 20:24:23 +0000141 // While we are linking in object files, loop.
Misha Brukmanf976c852005-04-21 22:55:34 +0000142 while (true) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000143
144 // Find the modules we need to link into the target module
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000145 std::set<ModuleProvider*> Modules;
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000146 if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
147 return error("Cannot find symbols in '" + Filename.toString() +
148 "': " + ErrMsg);
John Criswell71478b72003-09-19 20:24:23 +0000149
Misha Brukmanf976c852005-04-21 22:55:34 +0000150 // If we didn't find any more modules to link this time, we are done
Reid Spencer3cf2c322004-11-19 03:13:25 +0000151 // searching this archive.
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000152 if (Modules.empty())
153 break;
John Criswell71478b72003-09-19 20:24:23 +0000154
Reid Spencer3cf2c322004-11-19 03:13:25 +0000155 // Any symbols remaining in UndefinedSymbols after
156 // findModulesDefiningSymbols are ones that the archive does not define. So
157 // we add them to the NotDefinedByArchive variable now.
158 NotDefinedByArchive.insert(UndefinedSymbols.begin(),
Reid Spencerb9371ce2004-11-19 03:27:05 +0000159 UndefinedSymbols.end());
Reid Spencer3cf2c322004-11-19 03:13:25 +0000160
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000161 // Loop over all the ModuleProviders that we got back from the archive
162 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
163 I != E; ++I) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000164
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000165 // Get the module we must link in.
Reid Spencer99d36042004-11-16 06:47:41 +0000166 std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
Reid Spencer99d36042004-11-16 06:47:41 +0000167 Module* aModule = AutoModule.get();
168
Chris Lattner50c301b2005-02-13 15:26:14 +0000169 verbose(" Linking in module: " + aModule->getModuleIdentifier());
170
Reid Spencer99d36042004-11-16 06:47:41 +0000171 // Link it in
Chris Lattner12945ac2005-02-13 17:50:16 +0000172 if (LinkInModule(aModule))
Misha Brukmanf976c852005-04-21 22:55:34 +0000173 return error("Cannot link in module '" +
Reid Spencer7dde0e32004-12-13 02:59:29 +0000174 aModule->getModuleIdentifier() + "': " + Error);
John Criswell71478b72003-09-19 20:24:23 +0000175 }
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000176
Reid Spencer3cf2c322004-11-19 03:13:25 +0000177 // Get the undefined symbols from the aggregate module. This recomputes the
178 // symbols we still need after the new modules have been linked in.
Reid Spencer7dde0e32004-12-13 02:59:29 +0000179 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencer3cf2c322004-11-19 03:13:25 +0000180
181 // At this point we have two sets of undefined symbols: UndefinedSymbols
Misha Brukmanf976c852005-04-21 22:55:34 +0000182 // which holds the undefined symbols from all the modules, and
Reid Spencer3cf2c322004-11-19 03:13:25 +0000183 // NotDefinedByArchive which holds symbols we know the archive doesn't
184 // define. There's no point searching for symbols that we won't find in the
185 // archive so we subtract these sets.
Chris Lattner12945ac2005-02-13 17:50:16 +0000186 set_subtract(UndefinedSymbols, NotDefinedByArchive);
Misha Brukmanf976c852005-04-21 22:55:34 +0000187
Reid Spencer3cf2c322004-11-19 03:13:25 +0000188 // If there's no symbols left, no point in continuing to search the
189 // archive.
190 if (UndefinedSymbols.empty())
191 break;
John Criswell71478b72003-09-19 20:24:23 +0000192 }
Misha Brukmanf976c852005-04-21 22:55:34 +0000193
John Criswell71478b72003-09-19 20:24:23 +0000194 return false;
195}