c3c07d8588a36f6f711fb238d0bf4f8cafb3322f
[oota-llvm.git] / include / llvm / Bitcode / Archive.h
1 //===-- llvm/Bitcode/Archive.h - LLVM Bitcode Archive -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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
17 #ifndef LLVM_BITCODE_ARCHIVE_H
18 #define LLVM_BITCODE_ARCHIVE_H
19
20 #include "llvm/ADT/ilist.h"
21 #include "llvm/ADT/ilist_node.h"
22 #include "llvm/Support/Path.h"
23 #include <map>
24 #include <set>
25
26 namespace llvm {
27   class MemoryBuffer;
28   class raw_ostream;
29
30 // Forward declare classes
31 class Module;              // From VMCore
32 class Archive;             // Declared below
33 class ArchiveMemberHeader; // Internal implementation class
34 class LLVMContext;         // Global data
35
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.
43 class ArchiveMember : public ilist_node<ArchiveMember> {
44   /// @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 {
51       CompressedFlag = 1,          ///< Member is a normal compressed file
52       SVR4SymbolTableFlag = 2,     ///< Member is a SVR4 symbol table
53       BSD4SymbolTableFlag = 4,     ///< Member is a BSD4 symbol table
54       LLVMSymbolTableFlag = 8,     ///< Member is an LLVM symbol table
55       BitcodeFlag = 16,            ///< Member is bitcode
56       HasPathFlag = 64,            ///< Member has a full or partial path
57       HasLongFilenameFlag = 128,   ///< Member uses the long filename syntax
58       StringTableFlag = 256        ///< Member is an ar(1) format string table
59     };
60
61   /// @}
62   /// @name Accessors
63   /// @{
64   public:
65     /// @returns the parent Archive instance
66     /// @brief Get the archive associated with this member
67     Archive* getArchive() const          { return parent; }
68
69     /// @returns the path to the Archive's file
70     /// @brief Get the path to the archive member
71     const sys::Path& getPath() const     { return path; }
72
73     /// The "user" is the owner of the file per Unix security. This may not
74     /// have any applicability on non-Unix systems but is a required component
75     /// of the "ar" file format.
76     /// @brief Get the user associated with this archive member.
77     unsigned getUser() const             { return info.getUser(); }
78
79     /// The "group" is the owning group of the file per Unix security. This
80     /// may not have any applicability on non-Unix systems but is a required
81     /// component of the "ar" file format.
82     /// @brief Get the group associated with this archive member.
83     unsigned getGroup() const            { return info.getGroup(); }
84
85     /// The "mode" specifies the access permissions for the file per Unix
86     /// security. This may not have any applicability on non-Unix systems but is
87     /// a required component of the "ar" file format.
88     /// @brief Get the permission mode associated with this archive member.
89     unsigned getMode() const             { return info.getMode(); }
90
91     /// This method returns the time at which the archive member was last
92     /// modified when it was not in the archive.
93     /// @brief Get the time of last modification of the archive member.
94     sys::TimeValue getModTime() const    { return info.getTimestamp(); }
95
96     /// @returns the size of the archive member in bytes.
97     /// @brief Get the size of the archive member.
98     uint64_t getSize() const             { return info.getSize(); }
99
100     /// This method returns the total size of the archive member as it
101     /// appears on disk. This includes the file content, the header, the
102     /// long file name if any, and the padding.
103     /// @brief Get total on-disk member size.
104     unsigned getMemberSize() const;
105
106     /// This method will return a pointer to the in-memory content of the
107     /// archive member, if it is available. If the data has not been loaded
108     /// into memory, the return value will be null.
109     /// @returns a pointer to the member's data.
110     /// @brief Get the data content of the archive member
111     const char* getData() const { return data; }
112
113     /// This method determines if the member is a regular compressed file.
114     /// @returns true iff the archive member is a compressed regular file.
115     /// @brief Determine if the member is a compressed regular file.
116     bool isCompressed() const { return flags&CompressedFlag; }
117
118     /// @returns true iff the member is a SVR4 (non-LLVM) symbol table
119     /// @brief Determine if this member is a SVR4 symbol table.
120     bool isSVR4SymbolTable() const { return flags&SVR4SymbolTableFlag; }
121
122     /// @returns true iff the member is a BSD4.4 (non-LLVM) symbol table
123     /// @brief Determine if this member is a BSD4.4 symbol table.
124     bool isBSD4SymbolTable() const { return flags&BSD4SymbolTableFlag; }
125
126     /// @returns true iff the archive member is the LLVM symbol table
127     /// @brief Determine if this member is the LLVM symbol table.
128     bool isLLVMSymbolTable() const { return flags&LLVMSymbolTableFlag; }
129
130     /// @returns true iff the archive member is the ar(1) string table
131     /// @brief Determine if this member is the ar(1) string table.
132     bool isStringTable() const { return flags&StringTableFlag; }
133
134     /// @returns true iff the archive member is a bitcode file.
135     /// @brief Determine if this member is a bitcode file.
136     bool isBitcode() const { return flags&BitcodeFlag; }
137
138     /// @returns true iff the file name contains a path (directory) component.
139     /// @brief Determine if the member has a path
140     bool hasPath() const { return flags&HasPathFlag; }
141
142     /// Long filenames are an artifact of the ar(1) file format which allows
143     /// up to sixteen characters in its header and doesn't allow a path
144     /// separator character (/). To avoid this, a "long format" member name is
145     /// allowed that doesn't have this restriction. This method determines if
146     /// that "long format" is used for this member.
147     /// @returns true iff the file name uses the long form
148     /// @brief Determine if the member has a long file name
149     bool hasLongFilename() const { return flags&HasLongFilenameFlag; }
150
151     /// This method returns the status info (like Unix stat(2)) for the archive
152     /// member. The status info provides the file's size, permissions, and
153     /// modification time. The contents of the Path::StatusInfo structure, other
154     /// than the size and modification time, may not have utility on non-Unix
155     /// systems.
156     /// @returns the status info for the archive member
157     /// @brief Obtain the status info for the archive member
158     const sys::FileStatus &getFileStatus() const { return info; }
159
160     /// This method causes the archive member to be replaced with the contents
161     /// of the file specified by \p File. The contents of \p this will be
162     /// updated to reflect the new data from \p File. The \p File must exist and
163     /// be readable on entry to this method.
164     /// @returns true if an error occurred, false otherwise
165     /// @brief Replace contents of archive member with a new file.
166     bool replaceWith(const sys::Path &aFile, std::string* ErrMsg);
167
168   /// @}
169   /// @name Data
170   /// @{
171   private:
172     Archive*            parent;   ///< Pointer to parent archive
173     sys::PathWithStatus path;     ///< Path of file containing the member
174     sys::FileStatus     info;     ///< Status info (size,mode,date)
175     unsigned            flags;    ///< Flags about the archive member
176     const char*         data;     ///< Data for the member
177
178   /// @}
179   /// @name Constructors
180   /// @{
181   public:
182     /// The default constructor is only used by the Archive's iplist when it
183     /// constructs the list's sentry node.
184     ArchiveMember();
185
186   private:
187     /// Used internally by the Archive class to construct an ArchiveMember.
188     /// The contents of the ArchiveMember are filled out by the Archive class.
189     explicit ArchiveMember(Archive *PAR);
190
191     // So Archive can construct an ArchiveMember
192     friend class llvm::Archive;
193   /// @}
194 };
195
196 /// This class defines the interface to LLVM Archive files. The Archive class
197 /// presents the archive file as an ilist of ArchiveMember objects. The members
198 /// can be rearranged in any fashion either by directly editing the ilist or by
199 /// using editing methods on the Archive class (recommended). The Archive
200 /// class also provides several ways of accessing the archive file for various
201 /// purposes such as editing and linking.  Full symbol table support is provided
202 /// for loading only those files that resolve symbols. Note that read
203 /// performance of this library is _crucial_ for performance of JIT type
204 /// applications and the linkers. Consequently, the implementation of the class
205 /// is optimized for reading.
206 class Archive {
207
208   /// @name Types
209   /// @{
210   public:
211     /// This is the ilist type over which users may iterate to examine
212     /// the contents of the archive
213     /// @brief The ilist type of ArchiveMembers that Archive contains.
214     typedef iplist<ArchiveMember> MembersList;
215
216     /// @brief Forward mutable iterator over ArchiveMember
217     typedef MembersList::iterator iterator;
218
219     /// @brief Forward immutable iterator over ArchiveMember
220     typedef MembersList::const_iterator const_iterator;
221
222     /// @brief Reverse mutable iterator over ArchiveMember
223     typedef std::reverse_iterator<iterator> reverse_iterator;
224
225     /// @brief Reverse immutable iterator over ArchiveMember
226     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
227
228     /// @brief The in-memory version of the symbol table
229     typedef std::map<std::string,unsigned> SymTabType;
230
231   /// @}
232   /// @name ilist accessor methods
233   /// @{
234   public:
235     inline iterator               begin()        { return members.begin();  }
236     inline const_iterator         begin()  const { return members.begin();  }
237     inline iterator               end  ()        { return members.end();    }
238     inline const_iterator         end  ()  const { return members.end();    }
239
240     inline reverse_iterator       rbegin()       { return members.rbegin(); }
241     inline const_reverse_iterator rbegin() const { return members.rbegin(); }
242     inline reverse_iterator       rend  ()       { return members.rend();   }
243     inline const_reverse_iterator rend  () const { return members.rend();   }
244
245     inline size_t                 size()   const { return members.size();   }
246     inline bool                   empty()  const { return members.empty();  }
247     inline const ArchiveMember&   front()  const { return members.front();  }
248     inline       ArchiveMember&   front()        { return members.front();  }
249     inline const ArchiveMember&   back()   const { return members.back();   }
250     inline       ArchiveMember&   back()         { return members.back();   }
251
252   /// @}
253   /// @name ilist mutator methods
254   /// @{
255   public:
256     /// This method splices a \p src member from an archive (possibly \p this),
257     /// to a position just before the member given by \p dest in \p this. When
258     /// the archive is written, \p src will be written in its new location.
259     /// @brief Move a member to a new location
260     inline void splice(iterator dest, Archive& arch, iterator src)
261       { return members.splice(dest,arch.members,src); }
262
263     /// This method erases a \p target member from the archive. When the
264     /// archive is written, it will no longer contain \p target. The associated
265     /// ArchiveMember is deleted.
266     /// @brief Erase a member.
267     inline iterator erase(iterator target) { return members.erase(target); }
268
269   /// @}
270   /// @name Constructors
271   /// @{
272   public:
273     /// Create an empty archive file and associate it with the \p Filename. This
274     /// method does not actually create the archive disk file. It creates an
275     /// empty Archive object. If the writeToDisk method is called, the archive
276     /// file \p Filename will be created at that point, with whatever content
277     /// the returned Archive object has at that time.
278     /// @returns An Archive* that represents the new archive file.
279     /// @brief Create an empty Archive.
280     static Archive* CreateEmpty(
281       const sys::Path& Filename,///< Name of the archive to (eventually) create.
282       LLVMContext& C            ///< Context to use for global information
283     );
284
285     /// Open an existing archive and load its contents in preparation for
286     /// editing. After this call, the member ilist is completely populated based
287     /// on the contents of the archive file. You should use this form of open if
288     /// you intend to modify the archive or traverse its contents (e.g. for
289     /// printing).
290     /// @brief Open and load an archive file
291     static Archive* OpenAndLoad(
292       const sys::Path& filePath,  ///< The file path to open and load
293       LLVMContext& C,       ///< The context to use for global information
294       std::string* ErrorMessage   ///< An optional error string
295     );
296
297     /// This method opens an existing archive file from \p Filename and reads in
298     /// its symbol table without reading in any of the archive's members. This
299     /// reduces both I/O and cpu time in opening the archive if it is to be used
300     /// solely for symbol lookup (e.g. during linking).  The \p Filename must
301     /// exist and be an archive file or an error will be returned. This form
302     /// of opening the archive is intended for read-only operations that need to
303     /// locate members via the symbol table for link editing.  Since the archve
304     /// members are not read by this method, the archive will appear empty upon
305     /// return. If editing operations are performed on the archive, they will
306     /// completely replace the contents of the archive! It is recommended that
307     /// if this form of opening the archive is used that only the symbol table
308     /// lookup methods (getSymbolTable, findModuleDefiningSymbol, and
309     /// findModulesDefiningSymbols) be used.
310     /// @returns an Archive* that represents the archive file, or null on error.
311     /// @brief Open an existing archive and load its symbols.
312     static Archive* OpenAndLoadSymbols(
313       const sys::Path& Filename,   ///< Name of the archive file to open
314       LLVMContext& C,              ///< The context to use for global info
315       std::string* ErrorMessage=0  ///< An optional error string
316     );
317
318     /// This destructor cleans up the Archive object, releases all memory, and
319     /// closes files. It does nothing with the archive file on disk. If you
320     /// haven't used the writeToDisk method by the time the destructor is
321     /// called, all changes to the archive will be lost.
322     /// @brief Destruct in-memory archive
323     ~Archive();
324
325   /// @}
326   /// @name Accessors
327   /// @{
328   public:
329     /// @returns the path to the archive file.
330     /// @brief Get the archive path.
331     const sys::Path& getPath() { return archPath; }
332
333     /// This method is provided so that editing methods can be invoked directly
334     /// on the Archive's iplist of ArchiveMember. However, it is recommended
335     /// that the usual STL style iterator interface be used instead.
336     /// @returns the iplist of ArchiveMember
337     /// @brief Get the iplist of the members
338     MembersList& getMembers() { return members; }
339
340     /// This method allows direct query of the Archive's symbol table. The
341     /// symbol table is a std::map of std::string (the symbol) to unsigned (the
342     /// file offset). Note that for efficiency reasons, the offset stored in
343     /// the symbol table is not the actual offset. It is the offset from the
344     /// beginning of the first "real" file member (after the symbol table). Use
345     /// the getFirstFileOffset() to obtain that offset and add this value to the
346     /// offset in the symbol table to obtain the real file offset. Note that
347     /// there is purposefully no interface provided by Archive to look up
348     /// members by their offset. Use the findModulesDefiningSymbols and
349     /// findModuleDefiningSymbol methods instead.
350     /// @returns the Archive's symbol table.
351     /// @brief Get the archive's symbol table
352     const SymTabType& getSymbolTable() { return symTab; }
353
354     /// This method returns the offset in the archive file to the first "real"
355     /// file member. Archive files, on disk, have a signature and might have a
356     /// symbol table that precedes the first actual file member. This method
357     /// allows you to determine what the size of those fields are.
358     /// @returns the offset to the first "real" file member  in the archive.
359     /// @brief Get the offset to the first "real" file member  in the archive.
360     unsigned getFirstFileOffset() { return firstFileOffset; }
361
362     /// This method will scan the archive for bitcode modules, interpret them
363     /// and return a vector of the instantiated modules in \p Modules. If an
364     /// error occurs, this method will return true. If \p ErrMessage is not null
365     /// and an error occurs, \p *ErrMessage will be set to a string explaining
366     /// the error that occurred.
367     /// @returns true if an error occurred
368     /// @brief Instantiate all the bitcode modules located in the archive
369     bool getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage);
370
371     /// This accessor looks up the \p symbol in the archive's symbol table and
372     /// returns the associated module that defines that symbol. This method can
373     /// be called as many times as necessary. This is handy for linking the
374     /// archive into another module based on unresolved symbols. Note that the
375     /// Module returned by this accessor should not be deleted by the caller. It
376     /// is managed internally by the Archive class. It is possible that multiple
377     /// calls to this accessor will return the same Module instance because the
378     /// associated module defines multiple symbols.
379     /// @returns The Module* found or null if the archive does not contain a
380     /// module that defines the \p symbol.
381     /// @brief Look up a module by symbol name.
382     Module* findModuleDefiningSymbol(
383       const std::string& symbol,  ///< Symbol to be sought
384       std::string* ErrMessage     ///< Error message storage, if non-zero
385     );
386
387     /// This method is similar to findModuleDefiningSymbol but allows lookup of
388     /// more than one symbol at a time. If \p symbols contains a list of
389     /// undefined symbols in some module, then calling this method is like
390     /// making one complete pass through the archive to resolve symbols but is
391     /// more efficient than looking at the individual members. Note that on
392     /// exit, the symbols resolved by this method will be removed from \p
393     /// symbols to ensure they are not re-searched on a subsequent call. If
394     /// you need to retain the list of symbols, make a copy.
395     /// @brief Look up multiple symbols in the archive.
396     bool findModulesDefiningSymbols(
397       std::set<std::string>& symbols,     ///< Symbols to be sought
398       std::set<Module*>& modules,         ///< The modules matching \p symbols
399       std::string* ErrMessage             ///< Error msg storage, if non-zero
400     );
401
402     /// This method determines whether the archive is a properly formed llvm
403     /// bitcode archive.  It first makes sure the symbol table has been loaded
404     /// and has a non-zero size.  If it does, then it is an archive.  If not,
405     /// then it tries to load all the bitcode modules of the archive.  Finally,
406     /// it returns whether it was successful.
407     /// @returns true if the archive is a proper llvm bitcode archive
408     /// @brief Determine whether the archive is a proper llvm bitcode archive.
409     bool isBitcodeArchive();
410
411   /// @}
412   /// @name Mutators
413   /// @{
414   public:
415     /// This method is the only way to get the archive written to disk. It
416     /// creates or overwrites the file specified when \p this was created
417     /// or opened. The arguments provide options for writing the archive. If
418     /// \p CreateSymbolTable is true, the archive is scanned for bitcode files
419     /// and a symbol table of the externally visible function and global
420     /// variable names is created. If \p TruncateNames is true, the names of the
421     /// archive members will have their path component stripped and the file
422     /// name will be truncated at 15 characters. If \p Compress is specified,
423     /// all archive members will be compressed before being written. If
424     /// \p PrintSymTab is true, the symbol table will be printed to std::cout.
425     /// @returns true if an error occurred, \p error set to error message
426     /// @returns false if the writing succeeded.
427     /// @brief Write (possibly modified) archive contents to disk
428     bool writeToDisk(
429       bool CreateSymbolTable=false,   ///< Create Symbol table
430       bool TruncateNames=false,       ///< Truncate the filename to 15 chars
431       bool Compress=false,            ///< Compress files
432       std::string* ErrMessage=0       ///< If non-null, where error msg is set
433     );
434
435     /// This method adds a new file to the archive. The \p filename is examined
436     /// to determine just enough information to create an ArchiveMember object
437     /// which is then inserted into the Archive object's ilist at the location
438     /// given by \p where.
439     /// @returns true if an error occured, false otherwise
440     /// @brief Add a file to the archive.
441     bool addFileBefore(
442       const sys::Path& filename, ///< The file to be added
443       iterator where,            ///< Insertion point
444       std::string* ErrMsg        ///< Optional error message location
445     );
446
447   /// @}
448   /// @name Implementation
449   /// @{
450   protected:
451     /// @brief Construct an Archive for \p filename and optionally  map it
452     /// into memory.
453     explicit Archive(const sys::Path& filename, LLVMContext& C);
454
455     /// @param data The symbol table data to be parsed
456     /// @param len  The length of the symbol table data
457     /// @param error Set to address of a std::string to get error messages
458     /// @returns false on error
459     /// @brief Parse the symbol table at \p data.
460     bool parseSymbolTable(const void* data,unsigned len,std::string* error);
461
462     /// @returns A fully populated ArchiveMember or 0 if an error occurred.
463     /// @brief Parse the header of a member starting at \p At
464     ArchiveMember* parseMemberHeader(
465       const char*&At,    ///< The pointer to the location we're parsing
466       const char*End,    ///< The pointer to the end of the archive
467       std::string* error ///< Optional error message catcher
468     );
469
470     /// @param ErrMessage Set to address of a std::string to get error messages
471     /// @returns false on error
472     /// @brief Check that the archive signature is correct
473     bool checkSignature(std::string* ErrMessage);
474
475     /// @param ErrMessage Set to address of a std::string to get error messages
476     /// @returns false on error
477     /// @brief Load the entire archive.
478     bool loadArchive(std::string* ErrMessage);
479
480     /// @param ErrMessage Set to address of a std::string to get error messages
481     /// @returns false on error
482     /// @brief Load just the symbol table.
483     bool loadSymbolTable(std::string* ErrMessage);
484
485     /// @brief Write the symbol table to an ofstream.
486     void writeSymbolTable(raw_ostream& ARFile);
487
488     /// Writes one ArchiveMember to an ofstream. If an error occurs, returns
489     /// false, otherwise true. If an error occurs and error is non-null then
490     /// it will be set to an error message.
491     /// @returns false Writing member succeeded
492     /// @returns true Writing member failed, \p error set to error message
493     bool writeMember(
494       const ArchiveMember& member, ///< The member to be written
495       raw_ostream& ARFile,       ///< The file to write member onto
496       bool CreateSymbolTable,      ///< Should symbol table be created?
497       bool TruncateNames,          ///< Should names be truncated to 11 chars?
498       bool ShouldCompress,         ///< Should the member be compressed?
499       std::string* ErrMessage      ///< If non-null, place were error msg is set
500     );
501
502     /// @brief Fill in an ArchiveMemberHeader from ArchiveMember.
503     bool fillHeader(const ArchiveMember&mbr,
504                     ArchiveMemberHeader& hdr,int sz, bool TruncateNames) const;
505
506     /// @brief Maps archive into memory
507     bool mapToMemory(std::string* ErrMsg);
508
509     /// @brief Frees all the members and unmaps the archive file.
510     void cleanUpMemory();
511
512     /// This type is used to keep track of bitcode modules loaded from the
513     /// symbol table. It maps the file offset to a pair that consists of the
514     /// associated ArchiveMember and the Module.
515     /// @brief Module mapping type
516     typedef std::map<unsigned,std::pair<Module*,ArchiveMember*> >
517       ModuleMap;
518
519
520   /// @}
521   /// @name Data
522   /// @{
523   protected:
524     sys::Path archPath;       ///< Path to the archive file we read/write
525     MembersList members;      ///< The ilist of ArchiveMember
526     MemoryBuffer *mapfile;    ///< Raw Archive contents mapped into memory
527     const char* base;         ///< Base of the memory mapped file data
528     SymTabType symTab;        ///< The symbol table
529     std::string strtab;       ///< The string table for long file names
530     unsigned symTabSize;      ///< Size in bytes of symbol table
531     unsigned firstFileOffset; ///< Offset to first normal file.
532     ModuleMap modules;        ///< The modules loaded via symbol lookup.
533     ArchiveMember* foreignST; ///< This holds the foreign symbol table.
534     LLVMContext& Context;     ///< This holds global data.
535   /// @}
536   /// @name Hidden
537   /// @{
538   private:
539     Archive();                          ///< Do not implement
540     Archive(const Archive&);            ///< Do not implement
541     Archive& operator=(const Archive&); ///< Do not implement
542   /// @}
543 };
544
545 } // End llvm namespace
546
547 #endif