blob: 28683469f3c184c51cc78ce32a5a51e04b632f30 [file] [log] [blame]
Reid Spencer04f54122004-12-13 03:13:18 +00001//===- lib/Linker/LinkFiles.cpp - Link LLVM bytecode files ---------------===//
Reid Spencer3ece6392004-12-13 02:59:41 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencer04f54122004-12-13 03:13:18 +00005// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Reid Spencer3ece6392004-12-13 02:59:41 +00007//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer04f54122004-12-13 03:13:18 +000010// This file contains routines to handle linking together LLVM bytecode files.
Reid Spencer3ece6392004-12-13 02:59:41 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Linker.h"
15#include "llvm/Module.h"
Reid Spencer3ece6392004-12-13 02:59:41 +000016
17using namespace llvm;
18
19/// LinkInFile - opens a bytecode file and links in all objects which
20/// provide symbols that are currently undefined.
21///
22/// Inputs:
Reid Spencer04f54122004-12-13 03:13:18 +000023/// File - The pathname of the bytecode file.
Reid Spencer3ece6392004-12-13 02:59:41 +000024///
25/// Outputs:
26/// ErrorMessage - A C++ string detailing what error occurred, if any.
27///
28/// Return Value:
29/// TRUE - An error occurred.
30/// FALSE - No errors.
31///
32bool
33Linker::LinkInFile(const sys::Path &File)
34{
35 // Make sure we can at least read the file
36 if (!File.readable())
37 return error("Cannot find linker input '" + File.toString() + "'");
38
39 // A user may specify an ar archive without -l, perhaps because it
40 // is not installed as a library. Detect that and link the library.
41 if (File.isArchive()) {
42 if (LinkInArchive(File))
43 return error("Cannot link archive '" + File.toString() + "'");
44 } else if (File.isBytecodeFile()) {
45 verbose("Linking bytecode file '" + File.toString() + "'");
46
47 std::auto_ptr<Module> M(LoadObject(File));
48 if (M.get() == 0)
49 return error("Cannot load file '" + File.toString() + "'" + Error);
50 if (LinkInModule(M.get()))
51 return error("Cannot link file '" + File.toString() + "'" + Error);
52
53 verbose("Linked in file '" + File.toString() + "'");
54 } else {
55 return warning("File of unknown type '" + File.toString() + "' ignored.");
56 }
57 return false;
58}
59
60/// LinkFiles - takes a module and a list of files and links them all together.
61/// It locates the file either in the current directory, as its absolute
62/// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH.
63///
64/// Inputs:
Reid Spencer04f54122004-12-13 03:13:18 +000065/// Files - A vector of sys::Path indicating the LLVM bytecode filenames
Reid Spencer3ece6392004-12-13 02:59:41 +000066/// to be linked. The names can refer to a mixture of pure LLVM
67/// bytecode files and archive (ar) formatted files.
68///
Reid Spencer3ece6392004-12-13 02:59:41 +000069/// Return value:
70/// FALSE - No errors.
71/// TRUE - Some error occurred.
72///
73bool
74Linker::LinkInFiles(const std::vector<sys::Path> &Files)
75{
76 for (unsigned i = 0; i < Files.size(); ++i) {
77 if (LinkInFile(Files[i]))
78 return true;
79 }
80 return false;
81}