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