blob: 8c6ed423f05ceb7956e2cdd91e856b1c4897d67c [file] [log] [blame]
Reid Spencer4bdf1c92004-12-05 19:14:55 +00001//===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencer4bdf1c92004-12-05 19:14:55 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencer4bdf1c92004-12-05 19:14:55 +00008//===----------------------------------------------------------------------===//
9//
Gabor Greifa99be512007-07-05 17:07:56 +000010// This file contains routines to handle linking together LLVM bitcode files,
Reid Spencer4bdf1c92004-12-05 19:14:55 +000011// and to handle annoying things like static libraries.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Linker.h"
Chris Lattner74382b72009-08-23 22:45:37 +000016#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Module.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000018#include "llvm/Support/ErrorHandling.h"
Reid Spencer53424ad2007-08-08 19:52:29 +000019#include "llvm/Support/MemoryBuffer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/Support/Path.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000021#include "llvm/Support/system_error.h"
Reid Spencer4bdf1c92004-12-05 19:14:55 +000022using namespace llvm;
23
Reid Spencerf4484f32006-01-10 03:14:40 +000024// LinkItems - This function is the main entry point into linking. It takes a
25// list of LinkItem which indicates the order the files should be linked and
26// how each file should be treated (plain file or with library search). The
Gabor Greifa99be512007-07-05 17:07:56 +000027// function only links bitcode and produces a result list of items that are
Reid Spencerf4484f32006-01-10 03:14:40 +000028// native objects.
Reid Spencere84de292004-12-13 02:59:52 +000029bool
Reid Spencerf4484f32006-01-10 03:14:40 +000030Linker::LinkInItems(const ItemList& Items, ItemList& NativeItems) {
31 // Clear the NativeItems just in case
32 NativeItems.clear();
33
Reid Spencer4bdf1c92004-12-05 19:14:55 +000034 // For each linkage item ...
Misha Brukmanf976c852005-04-21 22:55:34 +000035 for (ItemList::const_iterator I = Items.begin(), E = Items.end();
Reid Spencer4bdf1c92004-12-05 19:14:55 +000036 I != E; ++I) {
37 if (I->second) {
38 // Link in the library suggested.
Reid Spencerc07cfdd2007-04-04 06:44:18 +000039 bool is_native = false;
40 if (LinkInLibrary(I->first, is_native))
Reid Spencere84de292004-12-13 02:59:52 +000041 return true;
Reid Spencerc07cfdd2007-04-04 06:44:18 +000042 if (is_native)
Reid Spencerf4484f32006-01-10 03:14:40 +000043 NativeItems.push_back(*I);
Reid Spencer4bdf1c92004-12-05 19:14:55 +000044 } else {
Reid Spencerf4484f32006-01-10 03:14:40 +000045 // Link in the file suggested
Reid Spencerc8539732007-04-04 06:33:17 +000046 bool is_native = false;
47 if (LinkInFile(sys::Path(I->first), is_native))
Reid Spencere84de292004-12-13 02:59:52 +000048 return true;
Reid Spencerc8539732007-04-04 06:33:17 +000049 if (is_native)
50 NativeItems.push_back(*I);
Reid Spencer4bdf1c92004-12-05 19:14:55 +000051 }
52 }
53
Reid Spencere84de292004-12-13 02:59:52 +000054 return false;
Reid Spencer4bdf1c92004-12-05 19:14:55 +000055}
Chris Lattnerad988f32005-03-15 22:51:40 +000056
57
58/// LinkInLibrary - links one library into the HeadModule.
59///
Daniel Dunbar2928c832009-11-06 10:58:06 +000060bool Linker::LinkInLibrary(StringRef Lib, bool& is_native) {
Reid Spencerc8539732007-04-04 06:33:17 +000061 is_native = false;
Chris Lattnerad988f32005-03-15 22:51:40 +000062 // Determine where this library lives.
63 sys::Path Pathname = FindLib(Lib);
64 if (Pathname.isEmpty())
Daniel Dunbar92ccf702009-07-25 06:02:13 +000065 return error("Cannot find library '" + Lib.str() + "'");
Chris Lattnerad988f32005-03-15 22:51:40 +000066
67 // If its an archive, try to link it in
Reid Spencerf4484f32006-01-10 03:14:40 +000068 std::string Magic;
69 Pathname.getMagicNumber(Magic, 64);
70 switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
Torok Edwinc23197a2009-07-14 16:55:14 +000071 default: llvm_unreachable("Bad file type identification");
Reid Spencerc8539732007-04-04 06:33:17 +000072 case sys::Unknown_FileType:
Daniel Dunbar92ccf702009-07-25 06:02:13 +000073 return warning("Supposed library '" + Lib.str() + "' isn't a library.");
Reid Spencerc8539732007-04-04 06:33:17 +000074
Chris Lattner1a019e52007-05-06 06:02:13 +000075 case sys::Bitcode_FileType:
Reid Spencerf4484f32006-01-10 03:14:40 +000076 // LLVM ".so" file.
Reid Spencerc8539732007-04-04 06:33:17 +000077 if (LinkInFile(Pathname, is_native))
Reid Spencer49068bf2007-08-16 07:47:30 +000078 return true;
Reid Spencerf4484f32006-01-10 03:14:40 +000079 break;
Reid Spencerc8539732007-04-04 06:33:17 +000080
81 case sys::Archive_FileType:
Reid Spencerc9a83e42007-04-30 00:29:39 +000082 if (LinkInArchive(Pathname, is_native))
Chris Lattner74382b72009-08-23 22:45:37 +000083 return error("Cannot link archive '" + Pathname.str() + "'");
Reid Spencerf4484f32006-01-10 03:14:40 +000084 break;
Reid Spencerc8539732007-04-04 06:33:17 +000085
Reid Spencer18da0722007-04-11 02:44:20 +000086 case sys::ELF_Relocatable_FileType:
87 case sys::ELF_SharedObject_FileType:
88 case sys::Mach_O_Object_FileType:
89 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
90 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
91 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
Reid Spencerc8539732007-04-04 06:33:17 +000092 case sys::COFF_FileType:
93 is_native = true;
94 break;
Chris Lattnerad988f32005-03-15 22:51:40 +000095 }
96 return false;
97}
98
99/// LinkLibraries - takes the specified library files and links them into the
Gabor Greifa99be512007-07-05 17:07:56 +0000100/// main bitcode object file.
Chris Lattnerad988f32005-03-15 22:51:40 +0000101///
102/// Inputs:
103/// Libraries - The list of libraries to link into the module.
104///
105/// Return value:
106/// FALSE - No error.
107/// TRUE - Error.
108///
109bool Linker::LinkInLibraries(const std::vector<std::string> &Libraries) {
110
111 // Process the set of libraries we've been provided.
Reid Spencerc07cfdd2007-04-04 06:44:18 +0000112 bool is_native = false;
Chris Lattnerad988f32005-03-15 22:51:40 +0000113 for (unsigned i = 0; i < Libraries.size(); ++i)
Reid Spencerc07cfdd2007-04-04 06:44:18 +0000114 if (LinkInLibrary(Libraries[i], is_native))
Chris Lattnerad988f32005-03-15 22:51:40 +0000115 return true;
116
Chris Lattnerad988f32005-03-15 22:51:40 +0000117 return false;
118}
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000119
Gabor Greifa99be512007-07-05 17:07:56 +0000120/// LinkInFile - opens a bitcode file and links in all objects which
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000121/// provide symbols that are currently undefined.
122///
123/// Inputs:
Gabor Greifa99be512007-07-05 17:07:56 +0000124/// File - The pathname of the bitcode file.
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000125///
126/// Outputs:
127/// ErrorMessage - A C++ string detailing what error occurred, if any.
128///
129/// Return Value:
130/// TRUE - An error occurred.
131/// FALSE - No errors.
132///
Reid Spencerc8539732007-04-04 06:33:17 +0000133bool Linker::LinkInFile(const sys::Path &File, bool &is_native) {
134 is_native = false;
Reid Spencer53424ad2007-08-08 19:52:29 +0000135
136 // Check for a file of name "-", which means "read standard input"
Chris Lattner74382b72009-08-23 22:45:37 +0000137 if (File.str() == "-") {
Reid Spencer53424ad2007-08-08 19:52:29 +0000138 std::auto_ptr<Module> M;
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000139 OwningPtr<MemoryBuffer> Buffer;
Michael J. Spencer333fb042010-12-09 17:36:48 +0000140 error_code ec;
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000141 if (!(ec = MemoryBuffer::getSTDIN(Buffer))) {
Dan Gohman30377e72010-05-27 17:31:51 +0000142 if (!Buffer->getBufferSize()) {
Dan Gohman30377e72010-05-27 17:31:51 +0000143 Error = "standard input is empty";
144 } else {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000145 M.reset(ParseBitcodeFile(Buffer.get(), Context, &Error));
Dan Gohman30377e72010-05-27 17:31:51 +0000146 if (M.get())
147 if (!LinkInModule(M.get(), &Error))
148 return false;
149 }
Daniel Dunbard65267e2009-11-10 00:43:58 +0000150 }
Michael J. Spencer333fb042010-12-09 17:36:48 +0000151 return error("Cannot link stdin: " + ec.message());
Reid Spencer53424ad2007-08-08 19:52:29 +0000152 }
153
Dan Gohman95131fc2010-05-27 17:18:38 +0000154 // Determine what variety of file it is.
155 std::string Magic;
156 if (!File.getMagicNumber(Magic, 64))
Chris Lattner74382b72009-08-23 22:45:37 +0000157 return error("Cannot find linker input '" + File.str() + "'");
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000158
Reid Spencerc8539732007-04-04 06:33:17 +0000159 switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000160 default: llvm_unreachable("Bad file type identification");
Reid Spencerc8539732007-04-04 06:33:17 +0000161 case sys::Unknown_FileType:
Chris Lattner74382b72009-08-23 22:45:37 +0000162 return warning("Ignoring file '" + File.str() +
Reid Spencer49068bf2007-08-16 07:47:30 +0000163 "' because does not contain bitcode.");
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000164
Reid Spencerc8539732007-04-04 06:33:17 +0000165 case sys::Archive_FileType:
166 // A user may specify an ar archive without -l, perhaps because it
167 // is not installed as a library. Detect that and link the archive.
Reid Spencerc9a83e42007-04-30 00:29:39 +0000168 if (LinkInArchive(File, is_native))
Reid Spencer49068bf2007-08-16 07:47:30 +0000169 return true;
Reid Spencerc8539732007-04-04 06:33:17 +0000170 break;
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000171
Gabor Greife75ca3d2007-07-06 13:38:17 +0000172 case sys::Bitcode_FileType: {
Chris Lattner74382b72009-08-23 22:45:37 +0000173 verbose("Linking bitcode file '" + File.str() + "'");
Reid Spencerc8539732007-04-04 06:33:17 +0000174 std::auto_ptr<Module> M(LoadObject(File));
175 if (M.get() == 0)
Chris Lattner74382b72009-08-23 22:45:37 +0000176 return error("Cannot load file '" + File.str() + "': " + Error);
Reid Spenceraf11dc02007-08-16 07:23:37 +0000177 if (LinkInModule(M.get(), &Error))
Chris Lattner74382b72009-08-23 22:45:37 +0000178 return error("Cannot link file '" + File.str() + "': " + Error);
Reid Spencerc8539732007-04-04 06:33:17 +0000179
Chris Lattner74382b72009-08-23 22:45:37 +0000180 verbose("Linked in file '" + File.str() + "'");
Reid Spencerc8539732007-04-04 06:33:17 +0000181 break;
182 }
183
Reid Spencer18da0722007-04-11 02:44:20 +0000184 case sys::ELF_Relocatable_FileType:
185 case sys::ELF_SharedObject_FileType:
186 case sys::Mach_O_Object_FileType:
187 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
188 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
189 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
Reid Spencerc8539732007-04-04 06:33:17 +0000190 case sys::COFF_FileType:
191 is_native = true;
192 break;
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000193 }
194 return false;
195}
196
197/// LinkFiles - takes a module and a list of files and links them all together.
198/// It locates the file either in the current directory, as its absolute
199/// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH.
200///
201/// Inputs:
Gabor Greifa99be512007-07-05 17:07:56 +0000202/// Files - A vector of sys::Path indicating the LLVM bitcode filenames
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000203/// to be linked. The names can refer to a mixture of pure LLVM
Gabor Greifa99be512007-07-05 17:07:56 +0000204/// bitcode files and archive (ar) formatted files.
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000205///
206/// Return value:
207/// FALSE - No errors.
208/// TRUE - Some error occurred.
209///
210bool Linker::LinkInFiles(const std::vector<sys::Path> &Files) {
Reid Spencerc8539732007-04-04 06:33:17 +0000211 bool is_native;
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000212 for (unsigned i = 0; i < Files.size(); ++i)
Reid Spencerc8539732007-04-04 06:33:17 +0000213 if (LinkInFile(Files[i], is_native))
Chris Lattnerfc82ef62005-03-15 22:55:17 +0000214 return true;
215 return false;
216}