Allow globals to be of different const'nesses when we link.
[oota-llvm.git] / lib / Linker / LinkFiles.cpp
1 //===- lib/Linker/LinkFiles.cpp - Link LLVM bytecode files  ---------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains routines to handle linking together LLVM bytecode files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Linker.h"
15 #include "llvm/Module.h"
16
17 using 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:
23 ///  File - The pathname of the bytecode file.
24 ///
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 ///
32 bool 
33 Linker::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:
65 ///  Files      - A vector of sys::Path indicating the LLVM bytecode filenames
66 ///               to be linked.  The names can refer to a mixture of pure LLVM
67 ///               bytecode files and archive (ar) formatted files.
68 ///
69 /// Return value:
70 ///  FALSE - No errors.
71 ///  TRUE  - Some error occurred.
72 ///
73 bool 
74 Linker::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 }