blob: 34fe2f124565dda39862b0c8561bb772051bb62e [file] [log] [blame]
Chris Lattnera58d2be2003-09-30 03:24:28 +00001//===- Linker.cpp - Link together LLVM objects and libraries --------------===//
John Criswell71478b72003-09-19 20:24:23 +00002//
Chris Lattnera58d2be2003-09-30 03:24:28 +00003// This file contains routines to handle linking together LLVM bytecode files,
4// and to handle annoying things like static libraries.
John Criswell71478b72003-09-19 20:24:23 +00005//
6//===----------------------------------------------------------------------===//
7
Misha Brukman17dc4ce2003-09-29 22:16:43 +00008#include "gccld.h"
John Criswell71478b72003-09-19 20:24:23 +00009#include "llvm/Module.h"
10#include "llvm/PassManager.h"
11#include "llvm/Bytecode/Reader.h"
12#include "llvm/Bytecode/WriteBytecodePass.h"
13#include "llvm/Target/TargetData.h"
14#include "llvm/Transforms/IPO.h"
15#include "llvm/Transforms/Scalar.h"
Misha Brukman17dc4ce2003-09-29 22:16:43 +000016#include "llvm/Transforms/Utils/Linker.h"
John Criswell71478b72003-09-19 20:24:23 +000017#include "Support/CommandLine.h"
Misha Brukman17dc4ce2003-09-29 22:16:43 +000018#include "Support/FileUtilities.h"
John Criswell71478b72003-09-19 20:24:23 +000019#include "Support/Signals.h"
Misha Brukman17dc4ce2003-09-29 22:16:43 +000020#include "Support/SystemUtils.h"
John Criswell71478b72003-09-19 20:24:23 +000021#include "Config/stdlib.h"
Misha Brukman17dc4ce2003-09-29 22:16:43 +000022#include <algorithm>
John Criswell71478b72003-09-19 20:24:23 +000023#include <fstream>
24#include <memory>
25#include <set>
John Criswell71478b72003-09-19 20:24:23 +000026
27//
28// Function: FileExists ()
29//
30// Description:
31// Determine if the specified filename exists and is readable.
32//
33// Inputs:
34// FN - The name of the file.
35//
36// Outputs:
37// None.
38//
39// Return Value:
40// TRUE - The file exists and is readable.
41// FALSE - The file does not exist or is unreadable.
42//
Misha Brukman17dc4ce2003-09-29 22:16:43 +000043static inline bool FileExists(const std::string &FN) {
John Criswell71478b72003-09-19 20:24:23 +000044 return access(FN.c_str(), R_OK | F_OK) != -1;
45}
46
47//
48// Function: IsArchive ()
49//
50// Description:
51// Determine if the specified file is an ar archive. It determines this by
52// checking the magic string at the beginning of the file.
53//
54// Inputs:
55// filename - A C++ string containing the name of the file.
56//
57// Outputs:
58// None.
59//
60// Return value:
61// TRUE - The file is an archive.
62// FALSE - The file is not an archive.
63//
Misha Brukmane6763132003-09-29 22:26:24 +000064static inline bool IsArchive(const std::string &filename)
John Criswell71478b72003-09-19 20:24:23 +000065{
66 std::string ArchiveMagic("!<arch>\012");
67 char buf[1 + ArchiveMagic.size()];
John Criswell71478b72003-09-19 20:24:23 +000068 std::ifstream f(filename.c_str());
69 f.read(buf, ArchiveMagic.size());
70 buf[ArchiveMagic.size()] = '\0';
71 return ArchiveMagic == buf;
72}
73
74//
75// Function: FindLib ()
76//
77// Description:
78// This function locates a particular library. It will prepend and append
79// various directories, prefixes, and suffixes until it can find the library.
80//
81// Inputs:
82// Filename - Name of the file to find.
83// Paths - List of directories to search.
84//
85// Outputs:
86// None.
87//
88// Return value:
89// The name of the file is returned.
90// If the file is not found, an empty string is returned.
91//
92static std::string
Misha Brukmane6763132003-09-29 22:26:24 +000093FindLib(const std::string &Filename, const std::vector<std::string> &Paths)
John Criswell71478b72003-09-19 20:24:23 +000094{
John Criswell71478b72003-09-19 20:24:23 +000095 // Determine if the pathname can be found as it stands.
Misha Brukmane6763132003-09-29 22:26:24 +000096 if (FileExists(Filename))
John Criswell71478b72003-09-19 20:24:23 +000097 return Filename;
John Criswell71478b72003-09-19 20:24:23 +000098
John Criswell71478b72003-09-19 20:24:23 +000099 // If that doesn't work, convert the name into a library name.
John Criswell71478b72003-09-19 20:24:23 +0000100 std::string LibName = "lib" + Filename;
101
John Criswell71478b72003-09-19 20:24:23 +0000102 // Iterate over the directories in Paths to see if we can find the library
103 // there.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000104 for (unsigned Index = 0; Index != Paths.size(); ++Index) {
John Criswell71478b72003-09-19 20:24:23 +0000105 std::string Directory = Paths[Index] + "/";
106
Misha Brukmane6763132003-09-29 22:26:24 +0000107 if (FileExists(Directory + LibName + ".bc"))
John Criswell71478b72003-09-19 20:24:23 +0000108 return Directory + LibName + ".bc";
John Criswell71478b72003-09-19 20:24:23 +0000109
Misha Brukmane6763132003-09-29 22:26:24 +0000110 if (FileExists(Directory + LibName + ".so"))
John Criswell71478b72003-09-19 20:24:23 +0000111 return Directory + LibName + ".so";
John Criswell71478b72003-09-19 20:24:23 +0000112
Misha Brukmane6763132003-09-29 22:26:24 +0000113 if (FileExists(Directory + LibName + ".a"))
John Criswell71478b72003-09-19 20:24:23 +0000114 return Directory + LibName + ".a";
John Criswell71478b72003-09-19 20:24:23 +0000115 }
116
John Criswell71478b72003-09-19 20:24:23 +0000117 // One last hope: Check LLVM_LIB_SEARCH_PATH.
John Criswell71478b72003-09-19 20:24:23 +0000118 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
119 if (SearchPath == NULL)
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000120 return std::string();
John Criswell71478b72003-09-19 20:24:23 +0000121
122 LibName = std::string(SearchPath) + "/" + LibName;
Misha Brukmane6763132003-09-29 22:26:24 +0000123 if (FileExists(LibName))
John Criswell71478b72003-09-19 20:24:23 +0000124 return LibName;
John Criswell71478b72003-09-19 20:24:23 +0000125
126 return std::string();
127}
128
129//
130// Function: GetAllDefinedSymbols ()
131//
132// Description:
133// Find all of the defined symbols in the specified module.
134//
135// Inputs:
136// M - The module in which to find defined symbols.
137//
138// Outputs:
139// DefinedSymbols - A set of C++ strings that will contain the name of all
140// defined symbols.
141//
142// Return value:
143// None.
144//
145void
Misha Brukmane6763132003-09-29 22:26:24 +0000146GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols)
John Criswell71478b72003-09-19 20:24:23 +0000147{
148 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
149 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
150 DefinedSymbols.insert(I->getName());
151 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
152 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
153 DefinedSymbols.insert(I->getName());
154}
155
156//
157// Function: GetAllUndefinedSymbols ()
158//
159// Description:
160// This calculates the set of undefined symbols that still exist in an LLVM
161// module. This is a bit tricky because there may be two symbols with the
162// same name but different LLVM types that will be resolved to each other but
163// aren't currently (thus we need to treat it as resolved).
164//
165// Inputs:
166// M - The module in which to find undefined symbols.
167//
168// Outputs:
169// UndefinedSymbols - A set of C++ strings containing the name of all
170// undefined symbols.
171//
172// Return value:
173// None.
174//
175void
176GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols)
177{
178 std::set<std::string> DefinedSymbols;
179 UndefinedSymbols.clear(); // Start out empty
180
181 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
182 if (I->hasName()) {
183 if (I->isExternal())
184 UndefinedSymbols.insert(I->getName());
185 else if (!I->hasInternalLinkage())
186 DefinedSymbols.insert(I->getName());
187 }
188 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
189 if (I->hasName()) {
190 if (I->isExternal())
191 UndefinedSymbols.insert(I->getName());
192 else if (!I->hasInternalLinkage())
193 DefinedSymbols.insert(I->getName());
194 }
195
196 // Prune out any defined symbols from the undefined symbols set...
197 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
198 I != UndefinedSymbols.end(); )
199 if (DefinedSymbols.count(*I))
200 UndefinedSymbols.erase(I++); // This symbol really is defined!
201 else
202 ++I; // Keep this symbol in the undefined symbols list
203}
204
205
206//
207// Function: LoadObject ()
208//
209// Description:
210// Read the specified bytecode object file.
211//
212// Inputs:
213// FN - The name of the file to load.
214//
215// Outputs:
216// OutErrorMessage - The error message to give back to the caller.
217//
218// Return Value:
219// A pointer to a module represening the bytecode file is returned.
220// If an error occurs, the pointer is 0.
221//
222std::auto_ptr<Module>
Misha Brukmane6763132003-09-29 22:26:24 +0000223LoadObject(const std::string & FN, std::string &OutErrorMessage) {
John Criswell71478b72003-09-19 20:24:23 +0000224 std::string ErrorMessage;
225 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
226 if (Result) return std::auto_ptr<Module>(Result);
John Criswell71478b72003-09-19 20:24:23 +0000227 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
228 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
229 return std::auto_ptr<Module>();
230}
231
232//
233// Function: LinkInArchive ()
234//
235// Description:
236// This function will open an archive library and link in all objects which
237// provide symbols that are currently undefined.
238//
239// Inputs:
240// M - The module in which to link the archives.
241// Filename - The pathname of the archive.
242// Verbose - Flags whether verbose messages should be printed.
243//
244// Outputs:
245// ErrorMessage - A C++ string detailing what error occurred, if any.
246//
247// Return Value:
248// TRUE - An error occurred.
249// FALSE - No errors.
250//
Misha Brukmane6763132003-09-29 22:26:24 +0000251static bool LinkInArchive(Module *M,
252 const std::string &Filename,
253 std::string &ErrorMessage,
254 bool Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000255{
256 //
257 // Find all of the symbols currently undefined in the bytecode program.
258 // If all the symbols are defined, the program is complete, and there is
259 // no reason to link in any archive files.
260 //
261 std::set<std::string> UndefinedSymbols;
Misha Brukmane6763132003-09-29 22:26:24 +0000262 GetAllUndefinedSymbols(M, UndefinedSymbols);
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000263 if (UndefinedSymbols.empty()) {
John Criswell71478b72003-09-19 20:24:23 +0000264 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
265 return false; // No need to link anything in!
266 }
267
268 //
269 // Load in the archive objects.
270 //
271 if (Verbose) std::cerr << " Loading '" << Filename << "'\n";
272 std::vector<Module*> Objects;
Misha Brukmane6763132003-09-29 22:26:24 +0000273 if (ReadArchiveFile(Filename, Objects, &ErrorMessage))
John Criswell71478b72003-09-19 20:24:23 +0000274 return true;
John Criswell71478b72003-09-19 20:24:23 +0000275
276 //
277 // Figure out which symbols are defined by all of the modules in the archive.
278 //
279 std::vector<std::set<std::string> > DefinedSymbols;
Misha Brukmane6763132003-09-29 22:26:24 +0000280 DefinedSymbols.resize(Objects.size());
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000281 for (unsigned i = 0; i != Objects.size(); ++i) {
John Criswell71478b72003-09-19 20:24:23 +0000282 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
283 }
284
285 // While we are linking in object files, loop.
286 bool Linked = true;
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000287 while (Linked) {
John Criswell71478b72003-09-19 20:24:23 +0000288 Linked = false;
289
290 for (unsigned i = 0; i != Objects.size(); ++i) {
291 // Consider whether we need to link in this module... we only need to
292 // link it in if it defines some symbol which is so far undefined.
293 //
294 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
295
296 bool ObjectRequired = false;
297 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
298 E = UndefinedSymbols.end(); I != E; ++I)
299 if (DefSymbols.count(*I)) {
300 if (Verbose)
301 std::cerr << " Found object providing symbol '" << *I << "'...\n";
302 ObjectRequired = true;
303 break;
304 }
305
306 // We DO need to link this object into the program...
307 if (ObjectRequired) {
308 if (LinkModules(M, Objects[i], &ErrorMessage))
309 return true; // Couldn't link in the right object file...
310
311 // Since we have linked in this object, delete it from the list of
312 // objects to consider in this archive file.
313 std::swap(Objects[i], Objects.back());
314 std::swap(DefinedSymbols[i], DefinedSymbols.back());
315 Objects.pop_back();
316 DefinedSymbols.pop_back();
317 --i; // Do not skip an entry
318
319 // The undefined symbols set should have shrunk.
320 GetAllUndefinedSymbols(M, UndefinedSymbols);
321 Linked = true; // We have linked something in!
322 }
323 }
324 }
325
326 return false;
327}
328
329//
330// Function: LinkInFile ()
331//
332// Description:
333// This function will open an archive library and link in all objects which
334// provide symbols that are currently undefined.
335//
336// Inputs:
337// HeadModule - The module in which to link the archives.
338// Filename - The pathname of the archive.
339// Verbose - Flags whether verbose messages should be printed.
340//
341// Outputs:
342// ErrorMessage - A C++ string detailing what error occurred, if any.
343//
344// Return Value:
345// TRUE - An error occurred.
346// FALSE - No errors.
347//
Misha Brukmane6763132003-09-29 22:26:24 +0000348static bool LinkInFile(Module *HeadModule,
349 const std::string &Filename,
350 std::string &ErrorMessage,
351 bool Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000352{
353 std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000354 if (M.get() == 0) return true;
John Criswell71478b72003-09-19 20:24:23 +0000355 if (Verbose) std::cerr << "Linking in '" << Filename << "'\n";
Misha Brukmane6763132003-09-29 22:26:24 +0000356 return LinkModules(HeadModule, M.get(), &ErrorMessage);
John Criswell71478b72003-09-19 20:24:23 +0000357}
358
359//
360// Function: LinkFiles ()
361//
362// Description:
363// This function takes a module and a list of files and links them all
364// together. It locates the file either in the current directory, as it's
365// absolute or relative pathname, or as a file somewhere in
366// LLVM_LIB_SEARCH_PATH.
367//
368// Inputs:
369// progname - The name of the program (infamous argv[0]).
370// HeadModule - The module under which all files will be linked.
371// Files - A vector of C++ strings indicating the LLVM bytecode filenames
372// to be linked. The names can refer to a mixture of pure LLVM
373// bytecode files and archive (ar) formatted files.
374// Verbose - Flags whether verbose output should be printed while linking.
375//
376// Outputs:
377// HeadModule - The module will have the specified LLVM bytecode files linked
378// in.
379//
380// Return value:
381// FALSE - No errors.
382// TRUE - Some error occurred.
383//
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000384bool LinkFiles(const char *progname,
385 Module *HeadModule,
386 const std::vector<std::string> &Files,
387 bool Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000388{
389 // String in which to receive error messages.
390 std::string ErrorMessage;
391
392 // Full pathname of the file
393 std::string Pathname;
394
395 // Get the library search path from the environment
396 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
397
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000398 for (unsigned i = 1; i < Files.size(); ++i) {
John Criswell71478b72003-09-19 20:24:23 +0000399 // Determine where this file lives.
Misha Brukmane6763132003-09-29 22:26:24 +0000400 if (FileExists(Files[i])) {
John Criswell71478b72003-09-19 20:24:23 +0000401 Pathname = Files[i];
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000402 } else {
403 if (SearchPath == NULL) {
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000404 std::cerr << "Cannot find linker input file '" << Files[i] << "'";
John Criswell71478b72003-09-19 20:24:23 +0000405 return true;
406 }
407
408 Pathname = std::string(SearchPath)+"/"+Files[i];
Misha Brukmane6763132003-09-29 22:26:24 +0000409 if (!FileExists(Pathname)) {
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000410 std::cerr << "Cannot find linker input file '" << Files[i] << "'";
John Criswell71478b72003-09-19 20:24:23 +0000411 return true;
412 }
413 }
414
John Criswell71478b72003-09-19 20:24:23 +0000415 // A user may specify an ar archive without -l, perhaps because it
416 // is not installed as a library. Detect that and link the library.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000417 if (IsArchive(Pathname)) {
John Criswell71478b72003-09-19 20:24:23 +0000418 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000419 std::cerr << "Linking archive '" << Files[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000420
Misha Brukmane6763132003-09-29 22:26:24 +0000421 if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000422 PrintAndReturn(progname, ErrorMessage,
Misha Brukmane6763132003-09-29 22:26:24 +0000423 ": Error linking in '" + Files[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000424 return true;
425 }
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000426 } else {
John Criswell71478b72003-09-19 20:24:23 +0000427 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000428 std::cerr << "Linking file '" << Files[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000429
Misha Brukmane6763132003-09-29 22:26:24 +0000430 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000431 PrintAndReturn(progname, ErrorMessage,
Misha Brukmane6763132003-09-29 22:26:24 +0000432 ": error linking in '" + Files[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000433 return true;
434 }
435 }
436 }
437
438 return false;
439}
440
441//
442// Function: LinkLibraries ()
443//
444// Description:
445// This function takes the specified library files and links them into the
446// main bytecode object file.
447//
448// Inputs:
449// progname - The name of the program (infamous argv[0]).
450// HeadModule - The module into which all necessary libraries will be linked.
451// Libraries - The list of libraries to link into the module.
452// LibPaths - The list of library paths in which to find libraries.
453// Verbose - Flags whether verbose messages should be printed.
454// Native - Flags whether native code is being generated.
455//
456// Outputs:
457// HeadModule - The module will have all necessary libraries linked in.
458//
459// Return value:
460// FALSE - No error.
461// TRUE - Error.
462//
Misha Brukmane6763132003-09-29 22:26:24 +0000463bool LinkLibraries(const char *progname,
464 Module *HeadModule,
465 const std::vector<std::string> &Libraries,
466 const std::vector<std::string> &LibPaths,
467 bool Verbose,
468 bool Native)
John Criswell71478b72003-09-19 20:24:23 +0000469{
470 // String in which to receive error messages.
471 std::string ErrorMessage;
472
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000473 for (unsigned i = 1; i < Libraries.size(); ++i) {
John Criswell71478b72003-09-19 20:24:23 +0000474 // Determine where this library lives.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000475 std::string Pathname = FindLib(Libraries[i], LibPaths);
476 if (Pathname.empty()) {
John Criswell71478b72003-09-19 20:24:23 +0000477 // If the pathname does not exist, then continue to the next one if
478 // we're doing a native link and give an error if we're doing a bytecode
479 // link.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000480 if (!Native) {
481 PrintAndReturn(progname, "Cannot find " + Libraries[i]);
John Criswell71478b72003-09-19 20:24:23 +0000482 return true;
483 }
484 }
485
John Criswell71478b72003-09-19 20:24:23 +0000486 // A user may specify an ar archive without -l, perhaps because it
487 // is not installed as a library. Detect that and link the library.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000488 if (IsArchive(Pathname)) {
John Criswell71478b72003-09-19 20:24:23 +0000489 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000490 std::cerr << "Linking archive '" << Libraries[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000491
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000492 if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000493 PrintAndReturn(progname, ErrorMessage,
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000494 ": Error linking in '" + Libraries[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000495 return true;
496 }
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000497 } else {
John Criswell71478b72003-09-19 20:24:23 +0000498 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000499 std::cerr << "Linking file '" << Libraries[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000500
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000501 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000502 PrintAndReturn(progname, ErrorMessage,
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000503 ": error linking in '" + Libraries[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000504 return true;
505 }
506 }
507 }
508
509 return false;
510}