blob: fcd2cf8b999900f3de34986b1346cd22aa7b12ee [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 Spencer4ca8e712004-12-20 04:15:44 +000022#include <memory>
John Criswell71478b72003-09-19 20:24:23 +000023#include <set>
Reid Spencer7dde0e32004-12-13 02:59:29 +000024#include <iostream>
25
Chris Lattner6cc8ca92003-11-28 07:44:09 +000026using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Brian Gaeke3b3640a2003-11-05 22:12:52 +000028/// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
29/// name of each externally-visible symbol defined in M.
Misha Brukman5208ba12003-09-30 18:09:32 +000030///
Reid Spencer7dde0e32004-12-13 02:59:29 +000031static void
32GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000033 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
34 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
35 DefinedSymbols.insert(I->getName());
Chris Lattnere4d5c442005-03-15 04:54:21 +000036 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
John Criswell71478b72003-09-19 20:24:23 +000037 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
38 DefinedSymbols.insert(I->getName());
39}
40
Misha Brukman5208ba12003-09-30 18:09:32 +000041/// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
42/// exist in an LLVM module. This is a bit tricky because there may be two
43/// symbols with the same name but different LLVM types that will be resolved to
44/// each other but aren't currently (thus we need to treat it as resolved).
45///
46/// Inputs:
47/// M - The module in which to find undefined symbols.
48///
49/// Outputs:
50/// UndefinedSymbols - A set of C++ strings containing the name of all
51/// undefined symbols.
52///
Reid Spencer7dde0e32004-12-13 02:59:29 +000053static void
54GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +000055 std::set<std::string> DefinedSymbols;
Reid Spencer3cf2c322004-11-19 03:13:25 +000056 UndefinedSymbols.clear();
John Criswell71478b72003-09-19 20:24:23 +000057
58 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
59 if (I->hasName()) {
60 if (I->isExternal())
61 UndefinedSymbols.insert(I->getName());
62 else if (!I->hasInternalLinkage())
63 DefinedSymbols.insert(I->getName());
64 }
Chris Lattnere4d5c442005-03-15 04:54:21 +000065 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I)
John Criswell71478b72003-09-19 20:24:23 +000066 if (I->hasName()) {
67 if (I->isExternal())
68 UndefinedSymbols.insert(I->getName());
69 else if (!I->hasInternalLinkage())
70 DefinedSymbols.insert(I->getName());
71 }
72
73 // Prune out any defined symbols from the undefined symbols set...
74 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
75 I != UndefinedSymbols.end(); )
76 if (DefinedSymbols.count(*I))
77 UndefinedSymbols.erase(I++); // This symbol really is defined!
78 else
79 ++I; // Keep this symbol in the undefined symbols list
80}
81
Misha Brukman5208ba12003-09-30 18:09:32 +000082/// LinkInArchive - opens an archive library and link in all objects which
83/// provide symbols that are currently undefined.
84///
85/// Inputs:
Misha Brukman5208ba12003-09-30 18:09:32 +000086/// Filename - The pathname of the archive.
Misha Brukman5208ba12003-09-30 18:09:32 +000087///
88/// Return Value:
89/// TRUE - An error occurred.
90/// FALSE - No errors.
Reid Spencer7dde0e32004-12-13 02:59:29 +000091bool
92Linker::LinkInArchive(const sys::Path &Filename) {
93
94 // Make sure this is an archive file we're dealing with
95 if (!Filename.isArchive())
96 return error("File '" + Filename.toString() + "' is not an archive.");
97
98 // Open the archive file
99 verbose("Linking archive file '" + Filename.toString() + "'");
100
John Criswell71478b72003-09-19 20:24:23 +0000101 // Find all of the symbols currently undefined in the bytecode program.
102 // If all the symbols are defined, the program is complete, and there is
103 // no reason to link in any archive files.
John Criswell71478b72003-09-19 20:24:23 +0000104 std::set<std::string> UndefinedSymbols;
Reid Spencer7dde0e32004-12-13 02:59:29 +0000105 GetAllUndefinedSymbols(Composite, UndefinedSymbols);
Reid Spencer3cf2c322004-11-19 03:13:25 +0000106
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000107 if (UndefinedSymbols.empty()) {
Reid Spencer7dde0e32004-12-13 02:59:29 +0000108 verbose("No symbols undefined, skipping library '" +
109 Filename.toString() + "'");
John Criswell71478b72003-09-19 20:24:23 +0000110 return false; // No need to link anything in!
111 }
112
Reid Spencer7dde0e32004-12-13 02:59:29 +0000113 std::string ErrMsg;
Reid Spencer99d36042004-11-16 06:47:41 +0000114 std::auto_ptr<Archive> AutoArch (
Reid Spencer7dde0e32004-12-13 02:59:29 +0000115 Archive::OpenAndLoadSymbols(Filename,&ErrMsg));
Reid Spencer99d36042004-11-16 06:47:41 +0000116
117 Archive* arch = AutoArch.get();
John Criswell71478b72003-09-19 20:24:23 +0000118
Reid Spencer7dde0e32004-12-13 02:59:29 +0000119 if (!arch)
120 return error("Cannot read archive '" + Filename.toString() +
121 "': " + ErrMsg);
122
Reid Spencer3cf2c322004-11-19 03:13:25 +0000123 // Save a set of symbols that are not defined by the archive. Since we're
124 // entering a loop, there's no point searching for these multiple times. This
125 // variable is used to "set_subtract" from the set of undefined symbols.
126 std::set<std::string> NotDefinedByArchive;
127
John Criswell71478b72003-09-19 20:24:23 +0000128 // While we are linking in object files, loop.
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000129 while (true) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000130
131 // Find the modules we need to link into the target module
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000132 std::set<ModuleProvider*> Modules;
Reid Spencer99d36042004-11-16 06:47:41 +0000133 arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
John Criswell71478b72003-09-19 20:24:23 +0000134
Reid Spencer3cf2c322004-11-19 03:13:25 +0000135 // If we didn't find any more modules to link this time, we are done
136 // searching this archive.
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000137 if (Modules.empty())
138 break;
John Criswell71478b72003-09-19 20:24:23 +0000139
Reid Spencer3cf2c322004-11-19 03:13:25 +0000140 // Any symbols remaining in UndefinedSymbols after
141 // findModulesDefiningSymbols are ones that the archive does not define. So
142 // we add them to the NotDefinedByArchive variable now.
143 NotDefinedByArchive.insert(UndefinedSymbols.begin(),
Reid Spencerb9371ce2004-11-19 03:27:05 +0000144 UndefinedSymbols.end());
Reid Spencer3cf2c322004-11-19 03:13:25 +0000145
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000146 // Loop over all the ModuleProviders that we got back from the archive
147 for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
148 I != E; ++I) {
Reid Spencer3cf2c322004-11-19 03:13:25 +0000149
Reid Spencer8bbb17a2004-11-14 22:02:27 +0000150 // Get the module we must link in.
Reid Spencer99d36042004-11-16 06:47:41 +0000151 std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
Reid Spencer99d36042004-11-16 06:47:41 +0000152 Module* aModule = AutoModule.get();
153
Chris Lattner50c301b2005-02-13 15:26:14 +0000154 verbose(" Linking in module: " + aModule->getModuleIdentifier());
155
Reid Spencer99d36042004-11-16 06:47:41 +0000156 // Link it in
Chris Lattner12945ac2005-02-13 17:50:16 +0000157 if (LinkInModule(aModule))
Reid Spencer7dde0e32004-12-13 02:59:29 +0000158 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.
Chris Lattner12945ac2005-02-13 17:50:16 +0000171 set_subtract(UndefinedSymbols, NotDefinedByArchive);
Reid Spencer3cf2c322004-11-19 03:13:25 +0000172
173 // If there's no symbols left, no point in continuing to search the
174 // archive.
175 if (UndefinedSymbols.empty())
176 break;
John Criswell71478b72003-09-19 20:24:23 +0000177 }
178
179 return false;
180}