blob: 3ce7fbdc948da90489455bd93a809618a788c656 [file] [log] [blame]
Chris Lattner5b183222007-05-06 19:49:28 +00001//===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner5b183222007-05-06 19:49:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the Archive and ArchiveMember
11// classes that is common to both reading and writing archives..
12//
13//===----------------------------------------------------------------------===//
14
15#include "ArchiveInternals.h"
16#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner5b183222007-05-06 19:49:28 +000017#include "llvm/Module.h"
18#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000019#include "llvm/Support/Process.h"
Michael J. Spencer7b6fef82010-12-09 17:36:48 +000020#include "llvm/Support/system_error.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000021#include <memory>
22#include <cstring>
Chris Lattner5b183222007-05-06 19:49:28 +000023using namespace llvm;
24
25// getMemberSize - compute the actual physical size of the file member as seen
26// on disk. This isn't the size of member's payload. Use getSize() for that.
27unsigned
28ArchiveMember::getMemberSize() const {
29 // Basically its the file size plus the header size
30 unsigned result = info.fileSize + sizeof(ArchiveMemberHeader);
31
32 // If it has a long filename, include the name length
33 if (hasLongFilename())
Chris Lattnerc521f542009-08-23 22:45:37 +000034 result += path.str().length() + 1;
Chris Lattner5b183222007-05-06 19:49:28 +000035
36 // If its now odd lengthed, include the padding byte
37 if (result % 2 != 0 )
38 result++;
39
40 return result;
41}
42
43// This default constructor is only use by the ilist when it creates its
44// sentry node. We give it specific static values to make it stand out a bit.
45ArchiveMember::ArchiveMember()
Dan Gohman804c95d2008-07-28 21:51:04 +000046 : parent(0), path("--invalid--"), flags(0), data(0)
Chris Lattner5b183222007-05-06 19:49:28 +000047{
48 info.user = sys::Process::GetCurrentUserId();
49 info.group = sys::Process::GetCurrentGroupId();
50 info.mode = 0777;
51 info.fileSize = 0;
52 info.modTime = sys::TimeValue::now();
53}
54
55// This is the constructor that the Archive class uses when it is building or
56// reading an archive. It just defaults a few things and ensures the parent is
57// set for the iplist. The Archive class fills in the ArchiveMember's data.
58// This is required because correctly setting the data may depend on other
59// things in the Archive.
60ArchiveMember::ArchiveMember(Archive* PAR)
Dan Gohman804c95d2008-07-28 21:51:04 +000061 : parent(PAR), path(), flags(0), data(0)
Chris Lattner5b183222007-05-06 19:49:28 +000062{
63}
64
65// This method allows an ArchiveMember to be replaced with the data for a
66// different file, presumably as an update to the member. It also makes sure
67// the flags are reset correctly.
68bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
69 if (!newFile.exists()) {
70 if (ErrMsg)
71 *ErrMsg = "Can not replace an archive member with a non-existent file";
72 return true;
73 }
74
75 data = 0;
76 path = newFile;
77
78 // SVR4 symbol tables have an empty name
Chris Lattnerc521f542009-08-23 22:45:37 +000079 if (path.str() == ARFILE_SVR4_SYMTAB_NAME)
Chris Lattner5b183222007-05-06 19:49:28 +000080 flags |= SVR4SymbolTableFlag;
81 else
82 flags &= ~SVR4SymbolTableFlag;
83
84 // BSD4.4 symbol tables have a special name
Chris Lattnerc521f542009-08-23 22:45:37 +000085 if (path.str() == ARFILE_BSD4_SYMTAB_NAME)
Chris Lattner5b183222007-05-06 19:49:28 +000086 flags |= BSD4SymbolTableFlag;
87 else
88 flags &= ~BSD4SymbolTableFlag;
89
90 // LLVM symbol tables have a very specific name
Chris Lattnerc521f542009-08-23 22:45:37 +000091 if (path.str() == ARFILE_LLVM_SYMTAB_NAME)
Chris Lattner5b183222007-05-06 19:49:28 +000092 flags |= LLVMSymbolTableFlag;
93 else
94 flags &= ~LLVMSymbolTableFlag;
95
96 // String table name
Chris Lattnerc521f542009-08-23 22:45:37 +000097 if (path.str() == ARFILE_STRTAB_NAME)
Chris Lattner5b183222007-05-06 19:49:28 +000098 flags |= StringTableFlag;
99 else
100 flags &= ~StringTableFlag;
101
102 // If it has a slash then it has a path
Chris Lattnerc521f542009-08-23 22:45:37 +0000103 bool hasSlash = path.str().find('/') != std::string::npos;
Chris Lattner5b183222007-05-06 19:49:28 +0000104 if (hasSlash)
105 flags |= HasPathFlag;
106 else
107 flags &= ~HasPathFlag;
108
109 // If it has a slash or its over 15 chars then its a long filename format
Chris Lattnerc521f542009-08-23 22:45:37 +0000110 if (hasSlash || path.str().length() > 15)
Chris Lattner5b183222007-05-06 19:49:28 +0000111 flags |= HasLongFilenameFlag;
112 else
113 flags &= ~HasLongFilenameFlag;
114
115 // Get the signature and status info
116 const char* signature = (const char*) data;
117 std::string magic;
118 if (!signature) {
119 path.getMagicNumber(magic,4);
120 signature = magic.c_str();
121 std::string err;
122 const sys::FileStatus *FSinfo = path.getFileStatus(false, ErrMsg);
123 if (FSinfo)
124 info = *FSinfo;
125 else
126 return true;
127 }
128
Daniel Dunbar78be93a2009-07-13 05:29:34 +0000129 // Determine what kind of file it is.
Chris Lattner5b183222007-05-06 19:49:28 +0000130 switch (sys::IdentifyFileType(signature,4)) {
Daniel Dunbar78be93a2009-07-13 05:29:34 +0000131 case sys::Bitcode_FileType:
132 flags |= BitcodeFlag;
133 break;
Chris Lattner5b183222007-05-06 19:49:28 +0000134 default:
Gabor Greif3d3fc322007-07-06 13:38:17 +0000135 flags &= ~BitcodeFlag;
Chris Lattner5b183222007-05-06 19:49:28 +0000136 break;
137 }
138 return false;
139}
140
141// Archive constructor - this is the only constructor that gets used for the
142// Archive class. Everything else (default,copy) is deprecated. This just
143// initializes and maps the file into memory, if requested.
Owen Anderson2a154432009-07-01 23:13:44 +0000144Archive::Archive(const sys::Path& filename, LLVMContext& C)
Chris Lattner5b183222007-05-06 19:49:28 +0000145 : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
Owen Anderson6773d382009-07-01 16:58:40 +0000146 symTabSize(0), firstFileOffset(0), modules(), foreignST(0), Context(C) {
Chris Lattner5b183222007-05-06 19:49:28 +0000147}
148
149bool
Chris Lattnerd4310a22008-04-01 04:26:46 +0000150Archive::mapToMemory(std::string* ErrMsg) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000151 error_code ec;
152 mapfile = MemoryBuffer::getFile(archPath.c_str(), ec);
153 if (mapfile == 0) {
154 if (ErrMsg)
155 *ErrMsg = ec.message();
Chris Lattner5b183222007-05-06 19:49:28 +0000156 return true;
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000157 }
Chris Lattnerd4310a22008-04-01 04:26:46 +0000158 base = mapfile->getBufferStart();
Chris Lattner5b183222007-05-06 19:49:28 +0000159 return false;
160}
161
162void Archive::cleanUpMemory() {
163 // Shutdown the file mapping
Chris Lattnerd4310a22008-04-01 04:26:46 +0000164 delete mapfile;
165 mapfile = 0;
166 base = 0;
Chris Lattner5b183222007-05-06 19:49:28 +0000167
168 // Forget the entire symbol table
169 symTab.clear();
170 symTabSize = 0;
171
172 firstFileOffset = 0;
173
174 // Free the foreign symbol table member
175 if (foreignST) {
176 delete foreignST;
177 foreignST = 0;
178 }
179
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000180 // Delete any Modules and ArchiveMember's we've allocated as a result of
181 // symbol table searches.
Chris Lattner5b183222007-05-06 19:49:28 +0000182 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
183 delete I->second.first;
184 delete I->second.second;
185 }
186}
187
188// Archive destructor - just clean up memory
189Archive::~Archive() {
190 cleanUpMemory();
191}
192
193
194
195static void getSymbols(Module*M, std::vector<std::string>& symbols) {
196 // Loop over global variables
197 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Rafael Espindola6de96a12009-01-15 20:18:42 +0000198 if (!GI->isDeclaration() && !GI->hasLocalLinkage())
Chris Lattner5b183222007-05-06 19:49:28 +0000199 if (!GI->getName().empty())
200 symbols.push_back(GI->getName());
201
Anton Korobeynikovd72ade32008-03-04 20:15:35 +0000202 // Loop over functions
Chris Lattner5b183222007-05-06 19:49:28 +0000203 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Rafael Espindola6de96a12009-01-15 20:18:42 +0000204 if (!FI->isDeclaration() && !FI->hasLocalLinkage())
Chris Lattner5b183222007-05-06 19:49:28 +0000205 if (!FI->getName().empty())
206 symbols.push_back(FI->getName());
Anton Korobeynikovd72ade32008-03-04 20:15:35 +0000207
208 // Loop over aliases
209 for (Module::alias_iterator AI = M->alias_begin(), AE = M->alias_end();
210 AI != AE; ++AI) {
Anton Korobeynikov2591afc2008-03-11 00:24:53 +0000211 if (AI->hasName())
212 symbols.push_back(AI->getName());
Anton Korobeynikovd72ade32008-03-04 20:15:35 +0000213 }
Chris Lattner5b183222007-05-06 19:49:28 +0000214}
215
Gabor Greife16561c2007-07-05 17:07:56 +0000216// Get just the externally visible defined symbols from the bitcode
217bool llvm::GetBitcodeSymbols(const sys::Path& fName,
Owen Anderson2a154432009-07-01 23:13:44 +0000218 LLVMContext& Context,
Gabor Greife16561c2007-07-05 17:07:56 +0000219 std::vector<std::string>& symbols,
220 std::string* ErrMsg) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000221 error_code ec;
Chris Lattner5b183222007-05-06 19:49:28 +0000222 std::auto_ptr<MemoryBuffer> Buffer(
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000223 MemoryBuffer::getFileOrSTDIN(fName.c_str(), ec));
Chris Lattner5b183222007-05-06 19:49:28 +0000224 if (!Buffer.get()) {
Michael J. Spencer7b6fef82010-12-09 17:36:48 +0000225 if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'" + ": "
226 + ec.message();
Chris Lattner5b183222007-05-06 19:49:28 +0000227 return true;
228 }
229
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000230 Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
231 if (!M)
Chris Lattner5b183222007-05-06 19:49:28 +0000232 return true;
233
Chris Lattner5b183222007-05-06 19:49:28 +0000234 // Get the symbols
235 getSymbols(M, symbols);
236
237 // Done with the module.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000238 delete M;
Chris Lattner5b183222007-05-06 19:49:28 +0000239 return true;
240}
241
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000242Module*
Benjamin Kramer3576b742010-04-19 16:15:31 +0000243llvm::GetBitcodeSymbols(const char *BufPtr, unsigned Length,
Gabor Greife16561c2007-07-05 17:07:56 +0000244 const std::string& ModuleID,
Owen Anderson2a154432009-07-01 23:13:44 +0000245 LLVMContext& Context,
Gabor Greife16561c2007-07-05 17:07:56 +0000246 std::vector<std::string>& symbols,
247 std::string* ErrMsg) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000248 // Get the module.
249 std::auto_ptr<MemoryBuffer> Buffer(
Benjamin Kramer3576b742010-04-19 16:15:31 +0000250 MemoryBuffer::getMemBufferCopy(StringRef(BufPtr, Length),ModuleID.c_str()));
Chris Lattner5b183222007-05-06 19:49:28 +0000251
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000252 Module *M = ParseBitcodeFile(Buffer.get(), Context, ErrMsg);
253 if (!M)
Chris Lattner5b183222007-05-06 19:49:28 +0000254 return 0;
255
Chris Lattner5b183222007-05-06 19:49:28 +0000256 // Get the symbols
257 getSymbols(M, symbols);
258
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000259 // Done with the module. Note that it's the caller's responsibility to delete
260 // the Module.
261 return M;
Chris Lattner5b183222007-05-06 19:49:28 +0000262}