blob: 00778d99834481bc6fb9f2dcd61970050e23a7fe [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
17#include "llvm/ModuleProvider.h"
18#include "llvm/Module.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/System/Process.h"
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000021#include <memory>
22#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattnerb1aa85b2009-08-23 22:45:37 +000034 result += path.str().length() + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohman2fcbc7e2008-07-28 21:51:04 +000046 : parent(0), path("--invalid--"), flags(0), data(0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohman2fcbc7e2008-07-28 21:51:04 +000061 : parent(PAR), path(), flags(0), data(0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattnerb1aa85b2009-08-23 22:45:37 +000079 if (path.str() == ARFILE_SVR4_SYMTAB_NAME)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 flags |= SVR4SymbolTableFlag;
81 else
82 flags &= ~SVR4SymbolTableFlag;
83
84 // BSD4.4 symbol tables have a special name
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000085 if (path.str() == ARFILE_BSD4_SYMTAB_NAME)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 flags |= BSD4SymbolTableFlag;
87 else
88 flags &= ~BSD4SymbolTableFlag;
89
90 // LLVM symbol tables have a very specific name
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000091 if (path.str() == ARFILE_LLVM_SYMTAB_NAME)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 flags |= LLVMSymbolTableFlag;
93 else
94 flags &= ~LLVMSymbolTableFlag;
95
96 // String table name
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000097 if (path.str() == ARFILE_STRTAB_NAME)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 flags |= StringTableFlag;
99 else
100 flags &= ~StringTableFlag;
101
102 // If it has a slash then it has a path
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000103 bool hasSlash = path.str().find('/') != std::string::npos;
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattnerb1aa85b2009-08-23 22:45:37 +0000110 if (hasSlash || path.str().length() > 15)
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Dunbar4dad4b62009-07-13 05:29:34 +0000129 // Determine what kind of file it is.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 switch (sys::IdentifyFileType(signature,4)) {
Daniel Dunbar4dad4b62009-07-13 05:29:34 +0000131 case sys::Bitcode_FileType:
132 flags |= BitcodeFlag;
133 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 default:
135 flags &= ~BitcodeFlag;
136 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 Anderson92adb182009-07-01 23:13:44 +0000144Archive::Archive(const sys::Path& filename, LLVMContext& C)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
Owen Anderson25209b42009-07-01 16:58:40 +0000146 symTabSize(0), firstFileOffset(0), modules(), foreignST(0), Context(C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147}
148
149bool
Chris Lattner54d19d42008-04-01 04:26:46 +0000150Archive::mapToMemory(std::string* ErrMsg) {
Chris Lattnerfc003612008-04-01 18:04:03 +0000151 mapfile = MemoryBuffer::getFile(archPath.c_str(), ErrMsg);
Chris Lattner54d19d42008-04-01 04:26:46 +0000152 if (mapfile == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 return true;
Chris Lattner54d19d42008-04-01 04:26:46 +0000154 base = mapfile->getBufferStart();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 return false;
156}
157
158void Archive::cleanUpMemory() {
159 // Shutdown the file mapping
Chris Lattner54d19d42008-04-01 04:26:46 +0000160 delete mapfile;
161 mapfile = 0;
162 base = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
164 // Forget the entire symbol table
165 symTab.clear();
166 symTabSize = 0;
167
168 firstFileOffset = 0;
169
170 // Free the foreign symbol table member
171 if (foreignST) {
172 delete foreignST;
173 foreignST = 0;
174 }
175
176 // Delete any ModuleProviders and ArchiveMember's we've allocated as a result
177 // of symbol table searches.
178 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
179 delete I->second.first;
180 delete I->second.second;
181 }
182}
183
184// Archive destructor - just clean up memory
185Archive::~Archive() {
186 cleanUpMemory();
187}
188
189
190
191static void getSymbols(Module*M, std::vector<std::string>& symbols) {
192 // Loop over global variables
193 for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000194 if (!GI->isDeclaration() && !GI->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 if (!GI->getName().empty())
196 symbols.push_back(GI->getName());
197
Anton Korobeynikovb39d3682008-03-04 20:15:35 +0000198 // Loop over functions
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000200 if (!FI->isDeclaration() && !FI->hasLocalLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 if (!FI->getName().empty())
202 symbols.push_back(FI->getName());
Anton Korobeynikovb39d3682008-03-04 20:15:35 +0000203
204 // Loop over aliases
205 for (Module::alias_iterator AI = M->alias_begin(), AE = M->alias_end();
206 AI != AE; ++AI) {
Anton Korobeynikov574c0f82008-03-11 00:24:53 +0000207 if (AI->hasName())
208 symbols.push_back(AI->getName());
Anton Korobeynikovb39d3682008-03-04 20:15:35 +0000209 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210}
211
212// Get just the externally visible defined symbols from the bitcode
213bool llvm::GetBitcodeSymbols(const sys::Path& fName,
Owen Anderson92adb182009-07-01 23:13:44 +0000214 LLVMContext& Context,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 std::vector<std::string>& symbols,
216 std::string* ErrMsg) {
217 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattnerfc003612008-04-01 18:04:03 +0000218 MemoryBuffer::getFileOrSTDIN(fName.c_str()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 if (!Buffer.get()) {
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000220 if (ErrMsg) *ErrMsg = "Could not open file '" + fName.str() + "'";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 return true;
222 }
223
Owen Anderson25209b42009-07-01 16:58:40 +0000224 ModuleProvider *MP = getBitcodeModuleProvider(Buffer.get(), Context, ErrMsg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 if (!MP)
226 return true;
227
228 // Get the module from the provider
229 Module* M = MP->materializeModule();
230 if (M == 0) {
231 delete MP;
232 return true;
233 }
234
235 // Get the symbols
236 getSymbols(M, symbols);
237
238 // Done with the module.
239 delete MP;
240 return true;
241}
242
243ModuleProvider*
244llvm::GetBitcodeSymbols(const unsigned char *BufPtr, unsigned Length,
245 const std::string& ModuleID,
Owen Anderson92adb182009-07-01 23:13:44 +0000246 LLVMContext& Context,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 std::vector<std::string>& symbols,
248 std::string* ErrMsg) {
249 // Get the module provider
250 MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(Length, ModuleID.c_str());
251 memcpy((char*)Buffer->getBufferStart(), BufPtr, Length);
252
Owen Anderson25209b42009-07-01 16:58:40 +0000253 ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, ErrMsg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (!MP)
255 return 0;
256
257 // Get the module from the provider
258 Module* M = MP->materializeModule();
259 if (M == 0) {
260 delete MP;
261 return 0;
262 }
263
264 // Get the symbols
265 getSymbols(M, symbols);
266
267 // Done with the module. Note that ModuleProvider will delete the
268 // Module when it is deleted. Also note that its the caller's responsibility
269 // to delete the ModuleProvider.
270 return MP;
271}