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