blob: 4c4fd6edaef227a34b3b8f2ff3ca8e24596b401e [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 Brukman61087cc2003-09-30 17:51:20 +000064static inline bool IsArchive(const std::string &filename) {
John Criswell71478b72003-09-19 20:24:23 +000065 std::string ArchiveMagic("!<arch>\012");
66 char buf[1 + ArchiveMagic.size()];
John Criswell71478b72003-09-19 20:24:23 +000067 std::ifstream f(filename.c_str());
68 f.read(buf, ArchiveMagic.size());
69 buf[ArchiveMagic.size()] = '\0';
70 return ArchiveMagic == buf;
71}
72
73//
74// Function: FindLib ()
75//
76// Description:
77// This function locates a particular library. It will prepend and append
78// various directories, prefixes, and suffixes until it can find the library.
79//
80// Inputs:
81// Filename - Name of the file to find.
82// Paths - List of directories to search.
83//
84// Outputs:
85// None.
86//
87// Return value:
88// The name of the file is returned.
89// If the file is not found, an empty string is returned.
90//
91static std::string
Misha Brukman61087cc2003-09-30 17:51:20 +000092FindLib(const std::string &Filename, const std::vector<std::string> &Paths) {
John Criswell71478b72003-09-19 20:24:23 +000093 // Determine if the pathname can be found as it stands.
Misha Brukmane6763132003-09-29 22:26:24 +000094 if (FileExists(Filename))
John Criswell71478b72003-09-19 20:24:23 +000095 return Filename;
John Criswell71478b72003-09-19 20:24:23 +000096
John Criswell71478b72003-09-19 20:24:23 +000097 // If that doesn't work, convert the name into a library name.
John Criswell71478b72003-09-19 20:24:23 +000098 std::string LibName = "lib" + Filename;
99
John Criswell71478b72003-09-19 20:24:23 +0000100 // Iterate over the directories in Paths to see if we can find the library
101 // there.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000102 for (unsigned Index = 0; Index != Paths.size(); ++Index) {
John Criswell71478b72003-09-19 20:24:23 +0000103 std::string Directory = Paths[Index] + "/";
104
Misha Brukmane6763132003-09-29 22:26:24 +0000105 if (FileExists(Directory + LibName + ".bc"))
John Criswell71478b72003-09-19 20:24:23 +0000106 return Directory + LibName + ".bc";
John Criswell71478b72003-09-19 20:24:23 +0000107
Misha Brukmane6763132003-09-29 22:26:24 +0000108 if (FileExists(Directory + LibName + ".so"))
John Criswell71478b72003-09-19 20:24:23 +0000109 return Directory + LibName + ".so";
John Criswell71478b72003-09-19 20:24:23 +0000110
Misha Brukmane6763132003-09-29 22:26:24 +0000111 if (FileExists(Directory + LibName + ".a"))
John Criswell71478b72003-09-19 20:24:23 +0000112 return Directory + LibName + ".a";
John Criswell71478b72003-09-19 20:24:23 +0000113 }
114
John Criswell71478b72003-09-19 20:24:23 +0000115 // One last hope: Check LLVM_LIB_SEARCH_PATH.
John Criswell71478b72003-09-19 20:24:23 +0000116 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
117 if (SearchPath == NULL)
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000118 return std::string();
John Criswell71478b72003-09-19 20:24:23 +0000119
120 LibName = std::string(SearchPath) + "/" + LibName;
Misha Brukmane6763132003-09-29 22:26:24 +0000121 if (FileExists(LibName))
John Criswell71478b72003-09-19 20:24:23 +0000122 return LibName;
John Criswell71478b72003-09-19 20:24:23 +0000123
124 return std::string();
125}
126
127//
128// Function: GetAllDefinedSymbols ()
129//
130// Description:
131// Find all of the defined symbols in the specified module.
132//
133// Inputs:
134// M - The module in which to find defined symbols.
135//
136// Outputs:
137// DefinedSymbols - A set of C++ strings that will contain the name of all
138// defined symbols.
139//
140// Return value:
141// None.
142//
Misha Brukman61087cc2003-09-30 17:51:20 +0000143void GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +0000144 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
145 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
146 DefinedSymbols.insert(I->getName());
147 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
148 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
149 DefinedSymbols.insert(I->getName());
150}
151
152//
153// Function: GetAllUndefinedSymbols ()
154//
155// Description:
156// This calculates the set of undefined symbols that still exist in an LLVM
157// module. This is a bit tricky because there may be two symbols with the
158// same name but different LLVM types that will be resolved to each other but
159// aren't currently (thus we need to treat it as resolved).
160//
161// Inputs:
162// M - The module in which to find undefined symbols.
163//
164// Outputs:
165// UndefinedSymbols - A set of C++ strings containing the name of all
166// undefined symbols.
167//
168// Return value:
169// None.
170//
171void
Misha Brukman61087cc2003-09-30 17:51:20 +0000172GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
John Criswell71478b72003-09-19 20:24:23 +0000173 std::set<std::string> DefinedSymbols;
174 UndefinedSymbols.clear(); // Start out empty
175
176 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
177 if (I->hasName()) {
178 if (I->isExternal())
179 UndefinedSymbols.insert(I->getName());
180 else if (!I->hasInternalLinkage())
181 DefinedSymbols.insert(I->getName());
182 }
183 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
184 if (I->hasName()) {
185 if (I->isExternal())
186 UndefinedSymbols.insert(I->getName());
187 else if (!I->hasInternalLinkage())
188 DefinedSymbols.insert(I->getName());
189 }
190
191 // Prune out any defined symbols from the undefined symbols set...
192 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
193 I != UndefinedSymbols.end(); )
194 if (DefinedSymbols.count(*I))
195 UndefinedSymbols.erase(I++); // This symbol really is defined!
196 else
197 ++I; // Keep this symbol in the undefined symbols list
198}
199
200
201//
202// Function: LoadObject ()
203//
204// Description:
205// Read the specified bytecode object file.
206//
207// Inputs:
208// FN - The name of the file to load.
209//
210// Outputs:
211// OutErrorMessage - The error message to give back to the caller.
212//
213// Return Value:
214// A pointer to a module represening the bytecode file is returned.
215// If an error occurs, the pointer is 0.
216//
217std::auto_ptr<Module>
Misha Brukmane6763132003-09-29 22:26:24 +0000218LoadObject(const std::string & FN, std::string &OutErrorMessage) {
John Criswell71478b72003-09-19 20:24:23 +0000219 std::string ErrorMessage;
220 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
221 if (Result) return std::auto_ptr<Module>(Result);
John Criswell71478b72003-09-19 20:24:23 +0000222 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
223 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
224 return std::auto_ptr<Module>();
225}
226
227//
228// Function: LinkInArchive ()
229//
230// Description:
231// This function will open an archive library and link in all objects which
232// provide symbols that are currently undefined.
233//
234// Inputs:
235// M - The module in which to link the archives.
236// Filename - The pathname of the archive.
237// Verbose - Flags whether verbose messages should be printed.
238//
239// Outputs:
240// ErrorMessage - A C++ string detailing what error occurred, if any.
241//
242// Return Value:
243// TRUE - An error occurred.
244// FALSE - No errors.
245//
Misha Brukmane6763132003-09-29 22:26:24 +0000246static bool LinkInArchive(Module *M,
247 const std::string &Filename,
248 std::string &ErrorMessage,
249 bool Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000250{
John Criswell71478b72003-09-19 20:24:23 +0000251 // Find all of the symbols currently undefined in the bytecode program.
252 // If all the symbols are defined, the program is complete, and there is
253 // no reason to link in any archive files.
John Criswell71478b72003-09-19 20:24:23 +0000254 std::set<std::string> UndefinedSymbols;
Misha Brukmane6763132003-09-29 22:26:24 +0000255 GetAllUndefinedSymbols(M, UndefinedSymbols);
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000256 if (UndefinedSymbols.empty()) {
John Criswell71478b72003-09-19 20:24:23 +0000257 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
258 return false; // No need to link anything in!
259 }
260
John Criswell71478b72003-09-19 20:24:23 +0000261 // Load in the archive objects.
John Criswell71478b72003-09-19 20:24:23 +0000262 if (Verbose) std::cerr << " Loading '" << Filename << "'\n";
263 std::vector<Module*> Objects;
Misha Brukmane6763132003-09-29 22:26:24 +0000264 if (ReadArchiveFile(Filename, Objects, &ErrorMessage))
John Criswell71478b72003-09-19 20:24:23 +0000265 return true;
John Criswell71478b72003-09-19 20:24:23 +0000266
John Criswell71478b72003-09-19 20:24:23 +0000267 // Figure out which symbols are defined by all of the modules in the archive.
John Criswell71478b72003-09-19 20:24:23 +0000268 std::vector<std::set<std::string> > DefinedSymbols;
Misha Brukmane6763132003-09-29 22:26:24 +0000269 DefinedSymbols.resize(Objects.size());
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000270 for (unsigned i = 0; i != Objects.size(); ++i) {
John Criswell71478b72003-09-19 20:24:23 +0000271 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
272 }
273
274 // While we are linking in object files, loop.
275 bool Linked = true;
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000276 while (Linked) {
John Criswell71478b72003-09-19 20:24:23 +0000277 Linked = false;
278
279 for (unsigned i = 0; i != Objects.size(); ++i) {
280 // Consider whether we need to link in this module... we only need to
281 // link it in if it defines some symbol which is so far undefined.
282 //
283 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
284
285 bool ObjectRequired = false;
286 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
287 E = UndefinedSymbols.end(); I != E; ++I)
288 if (DefSymbols.count(*I)) {
289 if (Verbose)
290 std::cerr << " Found object providing symbol '" << *I << "'...\n";
291 ObjectRequired = true;
292 break;
293 }
294
295 // We DO need to link this object into the program...
296 if (ObjectRequired) {
297 if (LinkModules(M, Objects[i], &ErrorMessage))
298 return true; // Couldn't link in the right object file...
299
300 // Since we have linked in this object, delete it from the list of
301 // objects to consider in this archive file.
302 std::swap(Objects[i], Objects.back());
303 std::swap(DefinedSymbols[i], DefinedSymbols.back());
304 Objects.pop_back();
305 DefinedSymbols.pop_back();
306 --i; // Do not skip an entry
307
308 // The undefined symbols set should have shrunk.
309 GetAllUndefinedSymbols(M, UndefinedSymbols);
310 Linked = true; // We have linked something in!
311 }
312 }
313 }
314
315 return false;
316}
317
318//
319// Function: LinkInFile ()
320//
321// Description:
322// This function will open an archive library and link in all objects which
323// provide symbols that are currently undefined.
324//
325// Inputs:
326// HeadModule - The module in which to link the archives.
327// Filename - The pathname of the archive.
328// Verbose - Flags whether verbose messages should be printed.
329//
330// Outputs:
331// ErrorMessage - A C++ string detailing what error occurred, if any.
332//
333// Return Value:
334// TRUE - An error occurred.
335// FALSE - No errors.
336//
Misha Brukmane6763132003-09-29 22:26:24 +0000337static bool LinkInFile(Module *HeadModule,
338 const std::string &Filename,
339 std::string &ErrorMessage,
340 bool Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000341{
342 std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000343 if (M.get() == 0) return true;
John Criswell71478b72003-09-19 20:24:23 +0000344 if (Verbose) std::cerr << "Linking in '" << Filename << "'\n";
Misha Brukmane6763132003-09-29 22:26:24 +0000345 return LinkModules(HeadModule, M.get(), &ErrorMessage);
John Criswell71478b72003-09-19 20:24:23 +0000346}
347
348//
349// Function: LinkFiles ()
350//
351// Description:
352// This function takes a module and a list of files and links them all
353// together. It locates the file either in the current directory, as it's
354// absolute or relative pathname, or as a file somewhere in
355// LLVM_LIB_SEARCH_PATH.
356//
357// Inputs:
358// progname - The name of the program (infamous argv[0]).
359// HeadModule - The module under which all files will be linked.
360// Files - A vector of C++ strings indicating the LLVM bytecode filenames
361// to be linked. The names can refer to a mixture of pure LLVM
362// bytecode files and archive (ar) formatted files.
363// Verbose - Flags whether verbose output should be printed while linking.
364//
365// Outputs:
366// HeadModule - The module will have the specified LLVM bytecode files linked
367// in.
368//
369// Return value:
370// FALSE - No errors.
371// TRUE - Some error occurred.
372//
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000373bool LinkFiles(const char *progname,
374 Module *HeadModule,
375 const std::vector<std::string> &Files,
376 bool Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000377{
378 // String in which to receive error messages.
379 std::string ErrorMessage;
380
381 // Full pathname of the file
382 std::string Pathname;
383
384 // Get the library search path from the environment
385 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
386
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000387 for (unsigned i = 1; i < Files.size(); ++i) {
John Criswell71478b72003-09-19 20:24:23 +0000388 // Determine where this file lives.
Misha Brukmane6763132003-09-29 22:26:24 +0000389 if (FileExists(Files[i])) {
John Criswell71478b72003-09-19 20:24:23 +0000390 Pathname = Files[i];
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000391 } else {
392 if (SearchPath == NULL) {
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000393 std::cerr << "Cannot find linker input file '" << Files[i] << "'";
John Criswell71478b72003-09-19 20:24:23 +0000394 return true;
395 }
396
397 Pathname = std::string(SearchPath)+"/"+Files[i];
Misha Brukmane6763132003-09-29 22:26:24 +0000398 if (!FileExists(Pathname)) {
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000399 std::cerr << "Cannot find linker input file '" << Files[i] << "'";
John Criswell71478b72003-09-19 20:24:23 +0000400 return true;
401 }
402 }
403
John Criswell71478b72003-09-19 20:24:23 +0000404 // A user may specify an ar archive without -l, perhaps because it
405 // is not installed as a library. Detect that and link the library.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000406 if (IsArchive(Pathname)) {
John Criswell71478b72003-09-19 20:24:23 +0000407 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000408 std::cerr << "Linking archive '" << Files[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000409
Misha Brukmane6763132003-09-29 22:26:24 +0000410 if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000411 PrintAndReturn(progname, ErrorMessage,
Misha Brukmane6763132003-09-29 22:26:24 +0000412 ": Error linking in '" + Files[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000413 return true;
414 }
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000415 } else {
John Criswell71478b72003-09-19 20:24:23 +0000416 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000417 std::cerr << "Linking file '" << Files[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000418
Misha Brukmane6763132003-09-29 22:26:24 +0000419 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000420 PrintAndReturn(progname, ErrorMessage,
Misha Brukmane6763132003-09-29 22:26:24 +0000421 ": error linking in '" + Files[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000422 return true;
423 }
424 }
425 }
426
427 return false;
428}
429
430//
431// Function: LinkLibraries ()
432//
433// Description:
434// This function takes the specified library files and links them into the
435// main bytecode object file.
436//
437// Inputs:
438// progname - The name of the program (infamous argv[0]).
439// HeadModule - The module into which all necessary libraries will be linked.
440// Libraries - The list of libraries to link into the module.
441// LibPaths - The list of library paths in which to find libraries.
442// Verbose - Flags whether verbose messages should be printed.
443// Native - Flags whether native code is being generated.
444//
445// Outputs:
446// HeadModule - The module will have all necessary libraries linked in.
447//
448// Return value:
449// FALSE - No error.
450// TRUE - Error.
451//
Misha Brukmane6763132003-09-29 22:26:24 +0000452bool LinkLibraries(const char *progname,
453 Module *HeadModule,
454 const std::vector<std::string> &Libraries,
455 const std::vector<std::string> &LibPaths,
456 bool Verbose,
457 bool Native)
John Criswell71478b72003-09-19 20:24:23 +0000458{
459 // String in which to receive error messages.
460 std::string ErrorMessage;
461
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000462 for (unsigned i = 1; i < Libraries.size(); ++i) {
John Criswell71478b72003-09-19 20:24:23 +0000463 // Determine where this library lives.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000464 std::string Pathname = FindLib(Libraries[i], LibPaths);
465 if (Pathname.empty()) {
John Criswell71478b72003-09-19 20:24:23 +0000466 // If the pathname does not exist, then continue to the next one if
467 // we're doing a native link and give an error if we're doing a bytecode
468 // link.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000469 if (!Native) {
470 PrintAndReturn(progname, "Cannot find " + Libraries[i]);
John Criswell71478b72003-09-19 20:24:23 +0000471 return true;
472 }
473 }
474
John Criswell71478b72003-09-19 20:24:23 +0000475 // A user may specify an ar archive without -l, perhaps because it
476 // is not installed as a library. Detect that and link the library.
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000477 if (IsArchive(Pathname)) {
John Criswell71478b72003-09-19 20:24:23 +0000478 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000479 std::cerr << "Linking archive '" << Libraries[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000480
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000481 if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000482 PrintAndReturn(progname, ErrorMessage,
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000483 ": Error linking in '" + Libraries[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000484 return true;
485 }
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000486 } else {
John Criswell71478b72003-09-19 20:24:23 +0000487 if (Verbose)
John Criswell71478b72003-09-19 20:24:23 +0000488 std::cerr << "Linking file '" << Libraries[i] << "'\n";
John Criswell71478b72003-09-19 20:24:23 +0000489
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000490 if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
John Criswell71478b72003-09-19 20:24:23 +0000491 PrintAndReturn(progname, ErrorMessage,
Misha Brukman17dc4ce2003-09-29 22:16:43 +0000492 ": error linking in '" + Libraries[i] + "'");
John Criswell71478b72003-09-19 20:24:23 +0000493 return true;
494 }
495 }
496 }
497
498 return false;
499}