blob: e6d4bae29da27e0bebdadd0ef7e96e33a0fbc38b [file] [log] [blame]
Reid Spencer362cbf02004-11-06 08:51:45 +00001//===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Reid Spencer362cbf02004-11-06 08:51:45 +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 Brukman2b37d7c2005-04-21 21:13:18 +00007//
Reid Spencer362cbf02004-11-06 08:51:45 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencercf6afc62004-11-14 21:56:59 +000010// This file contains the implementation of the Archive and ArchiveMember
11// classes that is common to both reading and writing archives..
Reid Spencer362cbf02004-11-06 08:51:45 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Bitcode/Archive.h"
Reid Spencer362cbf02004-11-06 08:51:45 +000016#include "ArchiveInternals.h"
Chris Lattnerc1d56242007-05-06 09:28:33 +000017#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000019#include "llvm/Support/FileSystem.h"
Chris Lattnerc1d56242007-05-06 09:28:33 +000020#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000021#include "llvm/Support/Process.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000022#include "llvm/Support/system_error.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000023#include <cstring>
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include <memory>
Reid Spencer362cbf02004-11-06 08:51:45 +000025using namespace llvm;
26
Reid Spencercf6afc62004-11-14 21:56:59 +000027// getMemberSize - compute the actual physical size of the file member as seen
28// on disk. This isn't the size of member's payload. Use getSize() for that.
29unsigned
30ArchiveMember::getMemberSize() const {
31 // Basically its the file size plus the header size
32 unsigned result = info.fileSize + sizeof(ArchiveMemberHeader);
33
34 // If it has a long filename, include the name length
35 if (hasLongFilename())
Chris Lattner74382b72009-08-23 22:45:37 +000036 result += path.str().length() + 1;
Reid Spencercf6afc62004-11-14 21:56:59 +000037
38 // If its now odd lengthed, include the padding byte
Misha Brukman2b37d7c2005-04-21 21:13:18 +000039 if (result % 2 != 0 )
Reid Spencercf6afc62004-11-14 21:56:59 +000040 result++;
41
42 return result;
Reid Spencer362cbf02004-11-06 08:51:45 +000043}
44
Reid Spencercf6afc62004-11-14 21:56:59 +000045// This default constructor is only use by the ilist when it creates its
46// sentry node. We give it specific static values to make it stand out a bit.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000047ArchiveMember::ArchiveMember()
Dan Gohmanfed90b62008-07-28 21:51:04 +000048 : parent(0), path("--invalid--"), flags(0), data(0)
Reid Spencercf6afc62004-11-14 21:56:59 +000049{
Reid Spencer8e827e82005-04-21 17:49:57 +000050 info.user = sys::Process::GetCurrentUserId();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000051 info.group = sys::Process::GetCurrentGroupId();
52 info.mode = 0777;
53 info.fileSize = 0;
Reid Spencercf6afc62004-11-14 21:56:59 +000054 info.modTime = sys::TimeValue::now();
55}
56
57// This is the constructor that the Archive class uses when it is building or
58// reading an archive. It just defaults a few things and ensures the parent is
Misha Brukman2b37d7c2005-04-21 21:13:18 +000059// set for the iplist. The Archive class fills in the ArchiveMember's data.
60// This is required because correctly setting the data may depend on other
Reid Spencercf6afc62004-11-14 21:56:59 +000061// things in the Archive.
62ArchiveMember::ArchiveMember(Archive* PAR)
Dan Gohmanfed90b62008-07-28 21:51:04 +000063 : parent(PAR), path(), flags(0), data(0)
Reid Spencercf6afc62004-11-14 21:56:59 +000064{
65}
66
Misha Brukman2b37d7c2005-04-21 21:13:18 +000067// This method allows an ArchiveMember to be replaced with the data for a
Reid Spencercf6afc62004-11-14 21:56:59 +000068// different file, presumably as an update to the member. It also makes sure
69// the flags are reset correctly.
Reid Spencer0ff2d312006-08-24 23:45:08 +000070bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
Michael J. Spencer54453f22011-01-10 02:34:23 +000071 bool Exists;
72 if (sys::fs::exists(newFile.str(), Exists) || !Exists) {
73 if (ErrMsg)
Reid Spencercd5561a2006-12-15 19:44:51 +000074 *ErrMsg = "Can not replace an archive member with a non-existent file";
75 return true;
76 }
77
Reid Spencercf6afc62004-11-14 21:56:59 +000078 data = 0;
79 path = newFile;
80
Reid Spencer9a29db42004-11-20 07:29:40 +000081 // SVR4 symbol tables have an empty name
Chris Lattner74382b72009-08-23 22:45:37 +000082 if (path.str() == ARFILE_SVR4_SYMTAB_NAME)
Reid Spencer9a29db42004-11-20 07:29:40 +000083 flags |= SVR4SymbolTableFlag;
Reid Spencercf6afc62004-11-14 21:56:59 +000084 else
Reid Spencer9a29db42004-11-20 07:29:40 +000085 flags &= ~SVR4SymbolTableFlag;
86
87 // BSD4.4 symbol tables have a special name
Chris Lattner74382b72009-08-23 22:45:37 +000088 if (path.str() == ARFILE_BSD4_SYMTAB_NAME)
Reid Spencer9a29db42004-11-20 07:29:40 +000089 flags |= BSD4SymbolTableFlag;
90 else
91 flags &= ~BSD4SymbolTableFlag;
Reid Spencercf6afc62004-11-14 21:56:59 +000092
Reid Spencercf6afc62004-11-14 21:56:59 +000093 // String table name
Chris Lattner74382b72009-08-23 22:45:37 +000094 if (path.str() == ARFILE_STRTAB_NAME)
Reid Spencercf6afc62004-11-14 21:56:59 +000095 flags |= StringTableFlag;
96 else
97 flags &= ~StringTableFlag;
98
99 // If it has a slash then it has a path
Chris Lattner74382b72009-08-23 22:45:37 +0000100 bool hasSlash = path.str().find('/') != std::string::npos;
Reid Spencercf6afc62004-11-14 21:56:59 +0000101 if (hasSlash)
102 flags |= HasPathFlag;
103 else
104 flags &= ~HasPathFlag;
105
106 // If it has a slash or its over 15 chars then its a long filename format
Chris Lattner74382b72009-08-23 22:45:37 +0000107 if (hasSlash || path.str().length() > 15)
Reid Spencercf6afc62004-11-14 21:56:59 +0000108 flags |= HasLongFilenameFlag;
109 else
110 flags &= ~HasLongFilenameFlag;
111
112 // Get the signature and status info
Reid Spencercf6afc62004-11-14 21:56:59 +0000113 const char* signature = (const char*) data;
Michael J. Spencer3c98e142011-01-16 21:13:51 +0000114 SmallString<4> magic;
Reid Spencercf6afc62004-11-14 21:56:59 +0000115 if (!signature) {
Michael J. Spencer3c98e142011-01-16 21:13:51 +0000116 sys::fs::get_magic(path.str(), magic.capacity(), magic);
Reid Spencercf6afc62004-11-14 21:56:59 +0000117 signature = magic.c_str();
Reid Spencera021d5d2007-04-07 19:51:45 +0000118 const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg);
Reid Spencer8475ec02007-03-29 19:05:44 +0000119 if (FSinfo)
120 info = *FSinfo;
121 else
Reid Spencer0ff2d312006-08-24 23:45:08 +0000122 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000123 }
124
Daniel Dunbar4892cb72009-07-13 05:29:34 +0000125 // Determine what kind of file it is.
Rafael Espindola98ee2f92013-06-11 15:09:43 +0000126 if (sys::fs::identify_magic(StringRef(signature, 4)) ==
127 sys::fs::file_magic::bitcode)
128 flags |= BitcodeFlag;
129 else
130 flags &= ~BitcodeFlag;
131
Reid Spencer0ff2d312006-08-24 23:45:08 +0000132 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000133}
134
135// Archive constructor - this is the only constructor that gets used for the
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000136// Archive class. Everything else (default,copy) is deprecated. This just
Reid Spencercf6afc62004-11-14 21:56:59 +0000137// initializes and maps the file into memory, if requested.
Owen Anderson4434ed42009-07-01 23:13:44 +0000138Archive::Archive(const sys::Path& filename, LLVMContext& C)
Reid Spencer1f465802004-11-16 06:47:07 +0000139 : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
Owen Anderson8b477ed2009-07-01 16:58:40 +0000140 symTabSize(0), firstFileOffset(0), modules(), foreignST(0), Context(C) {
Reid Spencer0ff2d312006-08-24 23:45:08 +0000141}
142
143bool
Chris Lattner7f6b4472008-04-01 04:26:46 +0000144Archive::mapToMemory(std::string* ErrMsg) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000145 OwningPtr<MemoryBuffer> File;
146 if (error_code ec = MemoryBuffer::getFile(archPath.c_str(), File)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000147 if (ErrMsg)
148 *ErrMsg = ec.message();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000149 return true;
Michael J. Spencer333fb042010-12-09 17:36:48 +0000150 }
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000151 mapfile = File.take();
Chris Lattner7f6b4472008-04-01 04:26:46 +0000152 base = mapfile->getBufferStart();
Reid Spencer0ff2d312006-08-24 23:45:08 +0000153 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000154}
155
Reid Spencer6ff72402005-11-30 05:21:10 +0000156void Archive::cleanUpMemory() {
Reid Spencercf6afc62004-11-14 21:56:59 +0000157 // Shutdown the file mapping
Chris Lattner7f6b4472008-04-01 04:26:46 +0000158 delete mapfile;
159 mapfile = 0;
160 base = 0;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000161
Reid Spencer6ff72402005-11-30 05:21:10 +0000162 // Forget the entire symbol table
163 symTab.clear();
164 symTabSize = 0;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000165
Reid Spencer6ff72402005-11-30 05:21:10 +0000166 firstFileOffset = 0;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000167
Reid Spencer6ff72402005-11-30 05:21:10 +0000168 // Free the foreign symbol table member
169 if (foreignST) {
170 delete foreignST;
171 foreignST = 0;
172 }
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000173
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000174 // Delete any Modules and ArchiveMember's we've allocated as a result of
175 // symbol table searches.
Reid Spencercf6afc62004-11-14 21:56:59 +0000176 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
177 delete I->second.first;
178 delete I->second.second;
179 }
Reid Spencer362cbf02004-11-06 08:51:45 +0000180}
181
Reid Spencer6ff72402005-11-30 05:21:10 +0000182// Archive destructor - just clean up memory
183Archive::~Archive() {
184 cleanUpMemory();
185}
186
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000187
188
189static void getSymbols(Module*M, std::vector<std::string>& symbols) {
190 // Loop over global variables
191 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Rafael Espindolabb46f522009-01-15 20:18:42 +0000192 if (!GI->isDeclaration() && !GI->hasLocalLinkage())
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000193 if (!GI->getName().empty())
194 symbols.push_back(GI->getName());
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000195
Anton Korobeynikovd58ceb22008-03-04 20:15:35 +0000196 // Loop over functions
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000197 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Rafael Espindolabb46f522009-01-15 20:18:42 +0000198 if (!FI->isDeclaration() && !FI->hasLocalLinkage())
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000199 if (!FI->getName().empty())
200 symbols.push_back(FI->getName());
Anton Korobeynikovd58ceb22008-03-04 20:15:35 +0000201
202 // Loop over aliases
203 for (Module::alias_iterator AI = M->alias_begin(), AE = M->alias_end();
204 AI != AE; ++AI) {
Anton Korobeynikov7fcb6b62008-03-11 00:24:53 +0000205 if (AI->hasName())
206 symbols.push_back(AI->getName());
Anton Korobeynikovd58ceb22008-03-04 20:15:35 +0000207 }
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000208}
209
Gabor Greifa99be512007-07-05 17:07:56 +0000210// Get just the externally visible defined symbols from the bitcode
211bool llvm::GetBitcodeSymbols(const sys::Path& fName,
Owen Anderson4434ed42009-07-01 23:13:44 +0000212 LLVMContext& Context,
Gabor Greifa99be512007-07-05 17:07:56 +0000213 std::vector<std::string>& symbols,
214 std::string* ErrMsg) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000215 OwningPtr<MemoryBuffer> Buffer;
216 if (error_code ec = MemoryBuffer::getFileOrSTDIN(fName.c_str(), Buffer)) {
Michael J. Spencer333fb042010-12-09 17:36:48 +0000217 if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'" + ": "
218 + ec.message();
Chris Lattnerc1d56242007-05-06 09:28:33 +0000219 return true;
220 }
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000221
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000222 Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
223 if (!M)
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000224 return true;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000225
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000226 // Get the symbols
227 getSymbols(M, symbols);
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000228
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000229 // Done with the module.
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000230 delete M;
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000231 return true;
232}
233
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000234Module*
Benjamin Kramer9d44e702010-04-19 16:15:31 +0000235llvm::GetBitcodeSymbols(const char *BufPtr, unsigned Length,
Gabor Greifa99be512007-07-05 17:07:56 +0000236 const std::string& ModuleID,
Owen Anderson4434ed42009-07-01 23:13:44 +0000237 LLVMContext& Context,
Gabor Greifa99be512007-07-05 17:07:56 +0000238 std::vector<std::string>& symbols,
239 std::string* ErrMsg) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000240 // Get the module.
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000241 OwningPtr<MemoryBuffer> Buffer(
Benjamin Kramer9d44e702010-04-19 16:15:31 +0000242 MemoryBuffer::getMemBufferCopy(StringRef(BufPtr, Length),ModuleID.c_str()));
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000243
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000244 Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
245 if (!M)
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000246 return 0;
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000247
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000248 // Get the symbols
249 getSymbols(M, symbols);
Michael J. Spencer83a113b2011-01-10 02:34:40 +0000250
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000251 // Done with the module. Note that it's the caller's responsibility to delete
252 // the Module.
253 return M;
Chris Lattnerf36c7b82007-02-07 23:53:17 +0000254}