blob: 232a5c23d2a488493dbad0c8495125235a7af226 [file] [log] [blame]
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +00001//===-- llvm/Bitcode/Archive.h - LLVM Bitcode Archive -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnere9cc7422007-12-29 19:59:42 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header file declares the Archive and ArchiveMember classes that provide
11// manipulation of LLVM Archive files. The implementation is provided by the
12// lib/Bitcode/Archive library. This library is used to read and write
13// archive (*.a) files that contain LLVM bitcode files (or others).
14//
15//===----------------------------------------------------------------------===//
16
Rafael Espindolaac5c1b02013-06-17 15:47:20 +000017#ifndef TOOLS_LLVM_AR_ARCHIVE_H
18#define TOOLS_LLVM_AR_ARCHIVE_H
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000019
Anton Korobeynikovcf3acae2008-05-29 17:41:17 +000020#include "llvm/ADT/ilist.h"
Dan Gohman804c95d2008-07-28 21:51:04 +000021#include "llvm/ADT/ilist_node.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000022#include "llvm/Support/Path.h"
Rafael Espindolafca03892013-06-19 21:13:59 +000023#include "llvm/Support/TimeValue.h"
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000024#include <map>
25#include <set>
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000026
27namespace llvm {
Chris Lattnerd4310a22008-04-01 04:26:46 +000028 class MemoryBuffer;
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000029
30// Forward declare classes
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000031class Module; // From VMCore
32class Archive; // Declared below
33class ArchiveMemberHeader; // Internal implementation class
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000034class LLVMContext; // Global data
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000035
36/// This class is the main class manipulated by users of the Archive class. It
37/// holds information about one member of the Archive. It is also the element
38/// stored by the Archive's ilist, the Archive's main abstraction. Because of
39/// the special requirements of archive files, users are not permitted to
40/// construct ArchiveMember instances. You should obtain them from the methods
41/// of the Archive class instead.
42/// @brief This class represents a single archive member.
Dan Gohman804c95d2008-07-28 21:51:04 +000043class ArchiveMember : public ilist_node<ArchiveMember> {
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000044 /// @name Types
45 /// @{
46 public:
47 /// These flags are used internally by the archive member to specify various
48 /// characteristics of the member. The various "is" methods below provide
49 /// access to the flags. The flags are not user settable.
50 enum Flags {
Rafael Espindola740a6bc2012-08-10 01:57:52 +000051 SVR4SymbolTableFlag = 1, ///< Member is a SVR4 symbol table
52 BSD4SymbolTableFlag = 2, ///< Member is a BSD4 symbol table
Rafael Espindola668c6422013-06-14 23:25:53 +000053 BitcodeFlag = 4, ///< Member is bitcode
54 HasPathFlag = 8, ///< Member has a full or partial path
55 HasLongFilenameFlag = 16, ///< Member uses the long filename syntax
56 StringTableFlag = 32 ///< Member is an ar(1) format string table
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000057 };
58
59 /// @}
60 /// @name Accessors
61 /// @{
62 public:
63 /// @returns the parent Archive instance
64 /// @brief Get the archive associated with this member
65 Archive* getArchive() const { return parent; }
66
67 /// @returns the path to the Archive's file
68 /// @brief Get the path to the archive member
Rafael Espindola8417a782013-06-19 15:45:37 +000069 StringRef getPath() const { return path; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000070
71 /// The "user" is the owner of the file per Unix security. This may not
72 /// have any applicability on non-Unix systems but is a required component
73 /// of the "ar" file format.
74 /// @brief Get the user associated with this archive member.
Rafael Espindolafca03892013-06-19 21:13:59 +000075 unsigned getUser() const { return User; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000076
77 /// The "group" is the owning group of the file per Unix security. This
78 /// may not have any applicability on non-Unix systems but is a required
79 /// component of the "ar" file format.
80 /// @brief Get the group associated with this archive member.
Rafael Espindolafca03892013-06-19 21:13:59 +000081 unsigned getGroup() const { return Group; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000082
83 /// The "mode" specifies the access permissions for the file per Unix
Michael J. Spencer53dcdc72011-01-15 21:43:45 +000084 /// security. This may not have any applicability on non-Unix systems but is
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000085 /// a required component of the "ar" file format.
86 /// @brief Get the permission mode associated with this archive member.
Rafael Espindolafca03892013-06-19 21:13:59 +000087 unsigned getMode() const { return Mode; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000088
89 /// This method returns the time at which the archive member was last
90 /// modified when it was not in the archive.
91 /// @brief Get the time of last modification of the archive member.
Rafael Espindolafca03892013-06-19 21:13:59 +000092 sys::TimeValue getModTime() const { return ModTime; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000093
94 /// @returns the size of the archive member in bytes.
95 /// @brief Get the size of the archive member.
Rafael Espindolafca03892013-06-19 21:13:59 +000096 uint64_t getSize() const { return Size; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +000097
98 /// This method returns the total size of the archive member as it
99 /// appears on disk. This includes the file content, the header, the
100 /// long file name if any, and the padding.
101 /// @brief Get total on-disk member size.
102 unsigned getMemberSize() const;
103
104 /// This method will return a pointer to the in-memory content of the
105 /// archive member, if it is available. If the data has not been loaded
106 /// into memory, the return value will be null.
107 /// @returns a pointer to the member's data.
108 /// @brief Get the data content of the archive member
Benjamin Kramer3576b742010-04-19 16:15:31 +0000109 const char* getData() const { return data; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000110
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000111 /// @returns true iff the member is a SVR4 (non-LLVM) symbol table
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000112 /// @brief Determine if this member is a SVR4 symbol table.
113 bool isSVR4SymbolTable() const { return flags&SVR4SymbolTableFlag; }
114
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000115 /// @returns true iff the member is a BSD4.4 (non-LLVM) symbol table
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000116 /// @brief Determine if this member is a BSD4.4 symbol table.
117 bool isBSD4SymbolTable() const { return flags&BSD4SymbolTableFlag; }
118
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000119 /// @returns true iff the archive member is the ar(1) string table
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000120 /// @brief Determine if this member is the ar(1) string table.
121 bool isStringTable() const { return flags&StringTableFlag; }
122
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000123 /// @returns true iff the archive member is a bitcode file.
Gabor Greif24027b52007-07-06 20:28:40 +0000124 /// @brief Determine if this member is a bitcode file.
Gabor Greif3d3fc322007-07-06 13:38:17 +0000125 bool isBitcode() const { return flags&BitcodeFlag; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000126
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000127 /// @returns true iff the file name contains a path (directory) component.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000128 /// @brief Determine if the member has a path
129 bool hasPath() const { return flags&HasPathFlag; }
130
131 /// Long filenames are an artifact of the ar(1) file format which allows
132 /// up to sixteen characters in its header and doesn't allow a path
133 /// separator character (/). To avoid this, a "long format" member name is
134 /// allowed that doesn't have this restriction. This method determines if
135 /// that "long format" is used for this member.
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000136 /// @returns true iff the file name uses the long form
Michael J. Spencer53dcdc72011-01-15 21:43:45 +0000137 /// @brief Determine if the member has a long file name
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000138 bool hasLongFilename() const { return flags&HasLongFilenameFlag; }
139
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000140 /// This method causes the archive member to be replaced with the contents
141 /// of the file specified by \p File. The contents of \p this will be
142 /// updated to reflect the new data from \p File. The \p File must exist and
143 /// be readable on entry to this method.
144 /// @returns true if an error occurred, false otherwise
145 /// @brief Replace contents of archive member with a new file.
Rafael Espindola351a88f2013-06-19 17:49:07 +0000146 bool replaceWith(StringRef aFile, std::string* ErrMsg);
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000147
148 /// @}
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000149 /// @name Data
150 /// @{
151 private:
Rafael Espindolafca03892013-06-19 21:13:59 +0000152 Archive *parent; ///< Pointer to parent archive
153 std::string path; ///< Path of file containing the member
154 uint32_t User;
155 uint32_t Group;
156 uint32_t Mode;
157 sys::TimeValue ModTime;
158 uint64_t Size;
159 unsigned flags; ///< Flags about the archive member
160 const char *data; ///< Data for the member
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000161
162 /// @}
163 /// @name Constructors
164 /// @{
165 public:
166 /// The default constructor is only used by the Archive's iplist when it
167 /// constructs the list's sentry node.
168 ArchiveMember();
169
170 private:
171 /// Used internally by the Archive class to construct an ArchiveMember.
172 /// The contents of the ArchiveMember are filled out by the Archive class.
Dan Gohman13ab93e2007-10-08 15:08:41 +0000173 explicit ArchiveMember(Archive *PAR);
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000174
175 // So Archive can construct an ArchiveMember
176 friend class llvm::Archive;
177 /// @}
178};
179
180/// This class defines the interface to LLVM Archive files. The Archive class
181/// presents the archive file as an ilist of ArchiveMember objects. The members
182/// can be rearranged in any fashion either by directly editing the ilist or by
183/// using editing methods on the Archive class (recommended). The Archive
184/// class also provides several ways of accessing the archive file for various
185/// purposes such as editing and linking. Full symbol table support is provided
186/// for loading only those files that resolve symbols. Note that read
187/// performance of this library is _crucial_ for performance of JIT type
188/// applications and the linkers. Consequently, the implementation of the class
189/// is optimized for reading.
190class Archive {
Misha Brukman9e6fb742009-02-20 23:04:06 +0000191
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000192 /// @name Types
193 /// @{
194 public:
195 /// This is the ilist type over which users may iterate to examine
196 /// the contents of the archive
197 /// @brief The ilist type of ArchiveMembers that Archive contains.
198 typedef iplist<ArchiveMember> MembersList;
199
200 /// @brief Forward mutable iterator over ArchiveMember
201 typedef MembersList::iterator iterator;
202
203 /// @brief Forward immutable iterator over ArchiveMember
204 typedef MembersList::const_iterator const_iterator;
205
206 /// @brief Reverse mutable iterator over ArchiveMember
207 typedef std::reverse_iterator<iterator> reverse_iterator;
208
209 /// @brief Reverse immutable iterator over ArchiveMember
210 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
211
212 /// @brief The in-memory version of the symbol table
213 typedef std::map<std::string,unsigned> SymTabType;
214
215 /// @}
216 /// @name ilist accessor methods
217 /// @{
218 public:
219 inline iterator begin() { return members.begin(); }
220 inline const_iterator begin() const { return members.begin(); }
221 inline iterator end () { return members.end(); }
222 inline const_iterator end () const { return members.end(); }
223
224 inline reverse_iterator rbegin() { return members.rbegin(); }
225 inline const_reverse_iterator rbegin() const { return members.rbegin(); }
226 inline reverse_iterator rend () { return members.rend(); }
227 inline const_reverse_iterator rend () const { return members.rend(); }
228
Evan Cheng86cb3182008-05-05 18:30:58 +0000229 inline size_t size() const { return members.size(); }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000230 inline bool empty() const { return members.empty(); }
231 inline const ArchiveMember& front() const { return members.front(); }
232 inline ArchiveMember& front() { return members.front(); }
233 inline const ArchiveMember& back() const { return members.back(); }
234 inline ArchiveMember& back() { return members.back(); }
235
236 /// @}
237 /// @name ilist mutator methods
238 /// @{
239 public:
240 /// This method splices a \p src member from an archive (possibly \p this),
241 /// to a position just before the member given by \p dest in \p this. When
242 /// the archive is written, \p src will be written in its new location.
243 /// @brief Move a member to a new location
244 inline void splice(iterator dest, Archive& arch, iterator src)
245 { return members.splice(dest,arch.members,src); }
246
247 /// This method erases a \p target member from the archive. When the
248 /// archive is written, it will no longer contain \p target. The associated
249 /// ArchiveMember is deleted.
250 /// @brief Erase a member.
251 inline iterator erase(iterator target) { return members.erase(target); }
252
253 /// @}
254 /// @name Constructors
255 /// @{
256 public:
257 /// Create an empty archive file and associate it with the \p Filename. This
258 /// method does not actually create the archive disk file. It creates an
259 /// empty Archive object. If the writeToDisk method is called, the archive
260 /// file \p Filename will be created at that point, with whatever content
261 /// the returned Archive object has at that time.
262 /// @returns An Archive* that represents the new archive file.
263 /// @brief Create an empty Archive.
Rafael Espindola351a88f2013-06-19 17:49:07 +0000264 static Archive *CreateEmpty(
265 StringRef Filename, ///< Name of the archive to (eventually) create.
266 LLVMContext &C ///< Context to use for global information
267 );
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000268
269 /// Open an existing archive and load its contents in preparation for
270 /// editing. After this call, the member ilist is completely populated based
271 /// on the contents of the archive file. You should use this form of open if
272 /// you intend to modify the archive or traverse its contents (e.g. for
273 /// printing).
274 /// @brief Open and load an archive file
Rafael Espindola351a88f2013-06-19 17:49:07 +0000275 static Archive *OpenAndLoad(
276 StringRef filePath, ///< The file path to open and load
277 LLVMContext &C, ///< The context to use for global information
278 std::string *ErrorMessage ///< An optional error string
279 );
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000280
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000281 /// This destructor cleans up the Archive object, releases all memory, and
282 /// closes files. It does nothing with the archive file on disk. If you
283 /// haven't used the writeToDisk method by the time the destructor is
284 /// called, all changes to the archive will be lost.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000285 /// @brief Destruct in-memory archive
286 ~Archive();
287
288 /// @}
289 /// @name Accessors
290 /// @{
291 public:
292 /// @returns the path to the archive file.
293 /// @brief Get the archive path.
Rafael Espindola351a88f2013-06-19 17:49:07 +0000294 StringRef getPath() { return archPath; }
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000295
296 /// This method is provided so that editing methods can be invoked directly
297 /// on the Archive's iplist of ArchiveMember. However, it is recommended
298 /// that the usual STL style iterator interface be used instead.
299 /// @returns the iplist of ArchiveMember
300 /// @brief Get the iplist of the members
301 MembersList& getMembers() { return members; }
302
303 /// This method allows direct query of the Archive's symbol table. The
304 /// symbol table is a std::map of std::string (the symbol) to unsigned (the
305 /// file offset). Note that for efficiency reasons, the offset stored in
306 /// the symbol table is not the actual offset. It is the offset from the
307 /// beginning of the first "real" file member (after the symbol table). Use
308 /// the getFirstFileOffset() to obtain that offset and add this value to the
309 /// offset in the symbol table to obtain the real file offset. Note that
310 /// there is purposefully no interface provided by Archive to look up
311 /// members by their offset. Use the findModulesDefiningSymbols and
312 /// findModuleDefiningSymbol methods instead.
313 /// @returns the Archive's symbol table.
314 /// @brief Get the archive's symbol table
315 const SymTabType& getSymbolTable() { return symTab; }
316
317 /// This method returns the offset in the archive file to the first "real"
318 /// file member. Archive files, on disk, have a signature and might have a
319 /// symbol table that precedes the first actual file member. This method
320 /// allows you to determine what the size of those fields are.
321 /// @returns the offset to the first "real" file member in the archive.
322 /// @brief Get the offset to the first "real" file member in the archive.
323 unsigned getFirstFileOffset() { return firstFileOffset; }
324
Gabor Greife16561c2007-07-05 17:07:56 +0000325 /// This method will scan the archive for bitcode modules, interpret them
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000326 /// and return a vector of the instantiated modules in \p Modules. If an
327 /// error occurs, this method will return true. If \p ErrMessage is not null
328 /// and an error occurs, \p *ErrMessage will be set to a string explaining
329 /// the error that occurred.
330 /// @returns true if an error occurred
Gabor Greife16561c2007-07-05 17:07:56 +0000331 /// @brief Instantiate all the bitcode modules located in the archive
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000332 bool getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage);
333
334 /// This accessor looks up the \p symbol in the archive's symbol table and
335 /// returns the associated module that defines that symbol. This method can
336 /// be called as many times as necessary. This is handy for linking the
337 /// archive into another module based on unresolved symbols. Note that the
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000338 /// Module returned by this accessor should not be deleted by the caller. It
339 /// is managed internally by the Archive class. It is possible that multiple
340 /// calls to this accessor will return the same Module instance because the
341 /// associated module defines multiple symbols.
342 /// @returns The Module* found or null if the archive does not contain a
343 /// module that defines the \p symbol.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000344 /// @brief Look up a module by symbol name.
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000345 Module* findModuleDefiningSymbol(
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000346 const std::string& symbol, ///< Symbol to be sought
347 std::string* ErrMessage ///< Error message storage, if non-zero
348 );
349
350 /// This method is similar to findModuleDefiningSymbol but allows lookup of
351 /// more than one symbol at a time. If \p symbols contains a list of
352 /// undefined symbols in some module, then calling this method is like
353 /// making one complete pass through the archive to resolve symbols but is
354 /// more efficient than looking at the individual members. Note that on
355 /// exit, the symbols resolved by this method will be removed from \p
356 /// symbols to ensure they are not re-searched on a subsequent call. If
357 /// you need to retain the list of symbols, make a copy.
358 /// @brief Look up multiple symbols in the archive.
359 bool findModulesDefiningSymbols(
360 std::set<std::string>& symbols, ///< Symbols to be sought
Rafael Espindolaabf456e2012-01-23 03:41:53 +0000361 SmallVectorImpl<Module*>& modules, ///< The modules matching \p symbols
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000362 std::string* ErrMessage ///< Error msg storage, if non-zero
363 );
364
365 /// This method determines whether the archive is a properly formed llvm
Gabor Greife16561c2007-07-05 17:07:56 +0000366 /// bitcode archive. It first makes sure the symbol table has been loaded
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000367 /// and has a non-zero size. If it does, then it is an archive. If not,
Gabor Greife16561c2007-07-05 17:07:56 +0000368 /// then it tries to load all the bitcode modules of the archive. Finally,
Michael J. Spencer53dcdc72011-01-15 21:43:45 +0000369 /// it returns whether it was successful.
Gabor Greife16561c2007-07-05 17:07:56 +0000370 /// @returns true if the archive is a proper llvm bitcode archive
371 /// @brief Determine whether the archive is a proper llvm bitcode archive.
372 bool isBitcodeArchive();
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000373
374 /// @}
375 /// @name Mutators
376 /// @{
377 public:
378 /// This method is the only way to get the archive written to disk. It
379 /// creates or overwrites the file specified when \p this was created
380 /// or opened. The arguments provide options for writing the archive. If
Gabor Greife16561c2007-07-05 17:07:56 +0000381 /// \p CreateSymbolTable is true, the archive is scanned for bitcode files
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000382 /// and a symbol table of the externally visible function and global
383 /// variable names is created. If \p TruncateNames is true, the names of the
384 /// archive members will have their path component stripped and the file
385 /// name will be truncated at 15 characters. If \p Compress is specified,
386 /// all archive members will be compressed before being written. If
387 /// \p PrintSymTab is true, the symbol table will be printed to std::cout.
Dmitri Gribenko65340a62012-08-23 16:54:08 +0000388 /// @returns true if an error occurred, \p error set to error message;
389 /// returns false if the writing succeeded.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000390 /// @brief Write (possibly modified) archive contents to disk
391 bool writeToDisk(
392 bool CreateSymbolTable=false, ///< Create Symbol table
393 bool TruncateNames=false, ///< Truncate the filename to 15 chars
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000394 std::string* ErrMessage=0 ///< If non-null, where error msg is set
395 );
396
397 /// This method adds a new file to the archive. The \p filename is examined
398 /// to determine just enough information to create an ArchiveMember object
399 /// which is then inserted into the Archive object's ilist at the location
400 /// given by \p where.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000401 /// @returns true if an error occurred, false otherwise
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000402 /// @brief Add a file to the archive.
Rafael Espindola351a88f2013-06-19 17:49:07 +0000403 bool addFileBefore(StringRef filename, ///< The file to be added
404 iterator where, ///< Insertion point
405 std::string *ErrMsg ///< Optional error message location
406 );
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000407
408 /// @}
409 /// @name Implementation
410 /// @{
411 protected:
412 /// @brief Construct an Archive for \p filename and optionally map it
413 /// into memory.
Rafael Espindola351a88f2013-06-19 17:49:07 +0000414 explicit Archive(StringRef filename, LLVMContext& C);
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000415
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000416 /// @returns A fully populated ArchiveMember or 0 if an error occurred.
417 /// @brief Parse the header of a member starting at \p At
418 ArchiveMember* parseMemberHeader(
419 const char*&At, ///< The pointer to the location we're parsing
420 const char*End, ///< The pointer to the end of the archive
421 std::string* error ///< Optional error message catcher
422 );
423
Reid Spencer446282a2007-08-05 20:06:04 +0000424 /// @param ErrMessage Set to address of a std::string to get error messages
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000425 /// @returns false on error
426 /// @brief Check that the archive signature is correct
427 bool checkSignature(std::string* ErrMessage);
428
Reid Spencer446282a2007-08-05 20:06:04 +0000429 /// @param ErrMessage Set to address of a std::string to get error messages
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000430 /// @returns false on error
431 /// @brief Load the entire archive.
432 bool loadArchive(std::string* ErrMessage);
433
Reid Spencer446282a2007-08-05 20:06:04 +0000434 /// @param ErrMessage Set to address of a std::string to get error messages
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000435 /// @returns false on error
436 /// @brief Load just the symbol table.
437 bool loadSymbolTable(std::string* ErrMessage);
438
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000439 /// Writes one ArchiveMember to an ofstream. If an error occurs, returns
Misha Brukman9e6fb742009-02-20 23:04:06 +0000440 /// false, otherwise true. If an error occurs and error is non-null then
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000441 /// it will be set to an error message.
Dmitri Gribenko65340a62012-08-23 16:54:08 +0000442 /// @returns false if writing member succeeded,
443 /// returns true if writing member failed, \p error set to error message.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000444 bool writeMember(
445 const ArchiveMember& member, ///< The member to be written
Dan Gohmana9c23e22011-03-01 19:50:55 +0000446 std::ofstream& ARFile, ///< The file to write member onto
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000447 bool CreateSymbolTable, ///< Should symbol table be created?
448 bool TruncateNames, ///< Should names be truncated to 11 chars?
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000449 std::string* ErrMessage ///< If non-null, place were error msg is set
450 );
451
452 /// @brief Fill in an ArchiveMemberHeader from ArchiveMember.
453 bool fillHeader(const ArchiveMember&mbr,
454 ArchiveMemberHeader& hdr,int sz, bool TruncateNames) const;
455
456 /// @brief Maps archive into memory
457 bool mapToMemory(std::string* ErrMsg);
458
459 /// @brief Frees all the members and unmaps the archive file.
460 void cleanUpMemory();
461
Gabor Greife16561c2007-07-05 17:07:56 +0000462 /// This type is used to keep track of bitcode modules loaded from the
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000463 /// symbol table. It maps the file offset to a pair that consists of the
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000464 /// associated ArchiveMember and the Module.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000465 /// @brief Module mapping type
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000466 typedef std::map<unsigned,std::pair<Module*,ArchiveMember*> >
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000467 ModuleMap;
468
469
470 /// @}
471 /// @name Data
472 /// @{
473 protected:
Rafael Espindola351a88f2013-06-19 17:49:07 +0000474 std::string archPath; ///< Path to the archive file we read/write
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000475 MembersList members; ///< The ilist of ArchiveMember
Chris Lattnerd4310a22008-04-01 04:26:46 +0000476 MemoryBuffer *mapfile; ///< Raw Archive contents mapped into memory
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000477 const char* base; ///< Base of the memory mapped file data
478 SymTabType symTab; ///< The symbol table
479 std::string strtab; ///< The string table for long file names
480 unsigned symTabSize; ///< Size in bytes of symbol table
481 unsigned firstFileOffset; ///< Offset to first normal file.
482 ModuleMap modules; ///< The modules loaded via symbol lookup.
483 ArchiveMember* foreignST; ///< This holds the foreign symbol table.
Owen Anderson2a154432009-07-01 23:13:44 +0000484 LLVMContext& Context; ///< This holds global data.
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000485 /// @}
486 /// @name Hidden
487 /// @{
488 private:
Craig Topperb1d83e82012-09-18 02:01:41 +0000489 Archive() LLVM_DELETED_FUNCTION;
490 Archive(const Archive&) LLVM_DELETED_FUNCTION;
491 Archive& operator=(const Archive&) LLVM_DELETED_FUNCTION;
Chris Lattnerc9c5d3e2007-05-06 09:14:53 +0000492 /// @}
493};
494
495} // End llvm namespace
496
497#endif