blob: 8186e7b4d18514486d8be7f1766aaa21250bbc93 [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
Misha Brukman5208ba12003-09-30 18:09:32 +000026/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
27/// exist in an LLVM module. This is a bit tricky because there may be two
28/// symbols with the same name but different LLVM types that will be resolved to
29/// each other but aren't currently (thus we need to treat it as resolved).
30///
31/// Inputs:
32/// M - The module in which to find undefined symbols.
33///
34/// Outputs:
35/// UndefinedSymbols - A set of C++ strings containing the name of all
36/// undefined symbols.
37///
Reid Spencer7dde0e32004-12-13 02:59:29 +000038static void
39GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000040 std::set<std::string> DefinedSymbols;
Reid Spencer3cf2c322004-11-19 03:13:25 +000041 UndefinedSymbols.clear();
Chris Lattnerc72e5732005-03-15 23:03:34 +000042
43 // If the program doesn't define a main, try pulling one in from a .a file.
44 // This is needed for programs where the main function is defined in an
45 // archive, such f2c'd programs.
Reid Spencer688b0492007-02-05 21:19:13 +000046 Function *Main = M->getFunction("main");
Reid Spencer5cbf9852007-01-30 20:08:39 +000047 if (Main == 0 || Main->isDeclaration())
Chris Lattnerc72e5732005-03-15 23:03:34 +000048 UndefinedSymbols.insert("main");
Misha Brukmanf976c852005-04-21 22:55:34 +000049
John Criswell71478b72003-09-19 20:24:23 +000050 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
51 if (I->hasName()) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000052 if (I->isDeclaration())
John Criswell71478b72003-09-19 20:24:23 +000053 UndefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000054 else if (!I->hasInternalLinkage()) {
55 assert(!I->hasDLLImportLinkage()
56 && "Found dllimported non-external symbol!");
John Criswell71478b72003-09-19 20:24:23 +000057 DefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000058 }
John Criswell71478b72003-09-19 20:24:23 +000059 }
Chris Lattnerc72e5732005-03-15 23:03:34 +000060 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
61 I != E; ++I)
John Criswell71478b72003-09-19 20:24:23 +000062 if (I->hasName()) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000063 if (I->isDeclaration())
John Criswell71478b72003-09-19 20:24:23 +000064 UndefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000065 else if (!I->hasInternalLinkage()) {
66 assert(!I->hasDLLImportLinkage()
67 && "Found dllimported non-external symbol!");
John Criswell71478b72003-09-19 20:24:23 +000068 DefinedSymbols.insert(I->getName());
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000069 }
John Criswell71478b72003-09-19 20:24:23 +000070 }
Misha Brukmanf976c852005-04-21 22:55:34 +000071
John Criswell71478b72003-09-19 20:24:23 +000072 // Prune out any defined symbols from the undefined symbols set...
73 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
74 I != UndefinedSymbols.end(); )
75 if (DefinedSymbols.count(*I))
76 UndefinedSymbols.erase(I++); // This symbol really is defined!
77 else
78 ++I; // Keep this symbol in the undefined symbols list
79}
80
Misha Brukman5208ba12003-09-30 18:09:32 +000081/// LinkInArchive - opens an archive library and link in all objects which
82/// provide symbols that are currently undefined.
83///
84/// Inputs:
Misha Brukman5208ba12003-09-30 18:09:32 +000085/// Filename - The pathname of the archive.
Misha Brukman5208ba12003-09-30 18:09:32 +000086///
87/// Return Value:
88/// TRUE - An error occurred.
89/// FALSE - No errors.
Misha Brukmanf976c852005-04-21 22:55:34 +000090bool
Reid Spencer7dde0e32004-12-13 02:59:29 +000091Linker::LinkInArchive(const sys::Path &Filename) {
92
93 // Make sure this is an archive file we're dealing with
94 if (!Filename.isArchive())
95 return error("File '" + Filename.toString() + "' is not an archive.");
96
97 // Open the archive file
98 verbose("Linking archive file '" + Filename.toString() + "'");
99
John Criswell71478b72003-09-19 20:24:23 +0000100 // Find all of the symbols currently undefined in the bytecode program.
101 // If all the symbols are defined, the program is complete, and there is
102 // no reason to link in any archive files.
John Criswell71478b72003-09-19 20:24:23 +0000103 std::set<std::string> UndefinedSymbols;
Reid Spencer7dde0e32004-12-13 02:59:29 +0000104 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Misha Brukmanf976c852005-04-21 22:55:34 +0000105
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000106 if (UndefinedSymbols.empty()) {
Misha Brukmanf976c852005-04-21 22:55:34 +0000107 verbose("No symbols undefined, skipping library '" +
Reid Spencer7dde0e32004-12-13 02:59:29 +0000108 Filename.toString() + "'");
John Criswell71478b72003-09-19 20:24:23 +0000109 return false; // No need to link anything in!
110 }
111
Reid Spencer7dde0e32004-12-13 02:59:29 +0000112 std::string ErrMsg;
Reid Spencer99d36042004-11-16 06:47:41 +0000113 std::auto_ptr<Archive> AutoArch (
Reid Spencer7dde0e32004-12-13 02:59:29 +0000114 Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
Reid Spencer99d36042004-11-16 06:47:41 +0000115
116 Archive* arch = AutoArch.get();
John Criswell71478b72003-09-19 20:24:23 +0000117
Reid Spencer7dde0e32004-12-13 02:59:29 +0000118 if (!arch)
Misha Brukmanf976c852005-04-21 22:55:34 +0000119 return error("Cannot read archive '" + Filename.toString() +
Reid Spencer7dde0e32004-12-13 02:59:29 +0000120 "': " + ErrMsg);
121
Reid Spencer3cf2c322004-11-19 03:13:25 +0000122 // Save a set of symbols that are not defined by the archive. Since we're
123 // entering a loop, there's no point searching for these multiple times. This
124 // variable is used to "set_subtract" from the set of undefined symbols.
125 std::set<std::string> NotDefinedByArchive;
126
Reid Spencer49521432006-11-11 11:54:25 +0000127 // Save the current set of undefined symbols, because we may have to make
128 // multiple passes over the archive:
129 std::set<std::string> CurrentlyUndefinedSymbols;
130
131 do {
132 CurrentlyUndefinedSymbols = UndefinedSymbols;
Reid Spencer3cf2c322004-11-19 03:13:25 +0000133
134 // Find the modules we need to link into the target module
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000135 std::set<ModuleProvider*> Modules;
Reid Spencer8d8a7ff2006-07-07 20:56:50 +0000136 if (!arch->findModulesDefiningSymbols(UndefinedSymbols, Modules, &ErrMsg))
137 return error("Cannot find symbols in '" + Filename.toString() +
138 "': " + ErrMsg);
John Criswell71478b72003-09-19 20:24:23 +0000139
Misha Brukmanf976c852005-04-21 22:55:34 +0000140 // If we didn't find any more modules to link this time, we are done
Reid Spencer3cf2c322004-11-19 03:13:25 +0000141 // searching this archive.
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000142 if (Modules.empty())
143 break;
John Criswell71478b72003-09-19 20:24:23 +0000144
Reid Spencer3cf2c322004-11-19 03:13:25 +0000145 // Any symbols remaining in UndefinedSymbols after
146 // findModulesDefiningSymbols are ones that the archive does not define. So
147 // we add them to the NotDefinedByArchive variable now.
148 NotDefinedByArchive.insert(UndefinedSymbols.begin(),
Reid Spencerb9371ce2004-11-19 03:27:05 +0000149 UndefinedSymbols.end());
Reid Spencer3cf2c322004-11-19 03:13:25 +0000150
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000151 // Loop over all the ModuleProviders that we got back from the archive
152 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
153 I != E; ++I) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000154
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000155 // Get the module we must link in.
Reid Spencer49521432006-11-11 11:54:25 +0000156 std::string moduleErrorMsg;
157 std::auto_ptr<Module> AutoModule((*I)->releaseModule( &moduleErrorMsg ));
Reid Spencer99d36042004-11-16 06:47:41 +0000158 Module* aModule = AutoModule.get();
159
Reid Spencer49521432006-11-11 11:54:25 +0000160 if (aModule != NULL) {
161 verbose(" Linking in module: " + aModule->getModuleIdentifier());
Chris Lattner50c301b2005-02-13 15:26:14 +0000162
Reid Spencer49521432006-11-11 11:54:25 +0000163 // Link it in
164 if (LinkInModule(aModule, &moduleErrorMsg)) {
165 return error("Cannot link in module '" +
166 aModule->getModuleIdentifier() + "': " + moduleErrorMsg);
167 }
Reid Spencer0288d182006-11-11 20:27:49 +0000168 }
John Criswell71478b72003-09-19 20:24:23 +0000169 }
Reid Spencer49521432006-11-11 11:54:25 +0000170
Reid Spencer3cf2c322004-11-19 03:13:25 +0000171 // Get the undefined symbols from the aggregate module. This recomputes the
172 // symbols we still need after the new modules have been linked in.
Reid Spencer7dde0e32004-12-13 02:59:29 +0000173 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencer3cf2c322004-11-19 03:13:25 +0000174
175 // At this point we have two sets of undefined symbols: UndefinedSymbols
Misha Brukmanf976c852005-04-21 22:55:34 +0000176 // which holds the undefined symbols from all the modules, and
Reid Spencer3cf2c322004-11-19 03:13:25 +0000177 // NotDefinedByArchive which holds symbols we know the archive doesn't
178 // define. There's no point searching for symbols that we won't find in the
179 // archive so we subtract these sets.
Chris Lattner12945ac2005-02-13 17:50:16 +0000180 set_subtract(UndefinedSymbols, NotDefinedByArchive);
Misha Brukmanf976c852005-04-21 22:55:34 +0000181
Reid Spencer3cf2c322004-11-19 03:13:25 +0000182 // If there's no symbols left, no point in continuing to search the
183 // archive.
184 if (UndefinedSymbols.empty())
185 break;
Reid Spencer49521432006-11-11 11:54:25 +0000186 } while (CurrentlyUndefinedSymbols != UndefinedSymbols);
Misha Brukmanf976c852005-04-21 22:55:34 +0000187
John Criswell71478b72003-09-19 20:24:23 +0000188 return false;
189}