Chris Lattner | 23219d1 | 2003-09-22 23:42:00 +0000 | [diff] [blame] | 1 | //===- ArchiveReader.cpp - Code to read LLVM bytecode from .a files -------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the ReadArchiveFile interface, which allows a linker to |
| 11 | // read all of the LLVM bytecode files contained in a .a file. This file |
| 12 | // understands the standard system .a file format. This can only handle the .a |
Misha Brukman | 09ff150 | 2003-04-23 02:59:05 +0000 | [diff] [blame] | 13 | // variant prevalent on Linux systems so far, but may be extended. See |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 14 | // information in this source file for more information: |
| 15 | // http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/bfd/archive.c?cvsroot=src |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "llvm/Bytecode/Reader.h" |
| 20 | #include "llvm/Module.h" |
John Criswell | 7a73b80 | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 21 | #include "Config/sys/stat.h" |
| 22 | #include "Config/sys/mman.h" |
| 23 | #include "Config/fcntl.h" |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 24 | #include <cstdlib> |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 25 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | struct ar_hdr { |
| 30 | char name[16]; |
| 31 | char date[12]; |
| 32 | char uid[6]; |
| 33 | char gid[6]; |
| 34 | char mode[8]; |
| 35 | char size[10]; |
| 36 | char fmag[2]; // Always equal to '`\n' |
| 37 | }; |
| 38 | |
| 39 | enum ObjectType { |
| 40 | UserObject, // A user .o/.bc file |
| 41 | Unknown, // Unknown file, just ignore it |
| 42 | SVR4LongFilename, // a "//" section used for long file names |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 43 | ArchiveSymbolTable, // Symbol table produced by ranlib. |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 44 | }; |
| 45 | } |
| 46 | |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 47 | // getObjectType - Determine the type of object that this header represents. |
| 48 | // This is capable of parsing the variety of special sections used for various |
| 49 | // purposes. |
| 50 | static enum ObjectType getObjectType(ar_hdr *H, unsigned Size) { |
| 51 | // Check for sections with special names... |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 52 | if (!memcmp(H->name, "__.SYMDEF ", 16)) |
| 53 | return ArchiveSymbolTable; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 54 | if (!memcmp(H->name, "// ", 16)) |
| 55 | return SVR4LongFilename; |
| 56 | |
| 57 | // Check to see if it looks like an llvm object file... |
| 58 | if (Size >= 4 && !memcmp(H+1, "llvm", 4)) |
| 59 | return UserObject; |
| 60 | |
| 61 | return Unknown; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static inline bool Error(std::string *ErrorStr, const char *Message) { |
| 66 | if (ErrorStr) *ErrorStr = Message; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | static bool ParseLongFilenameSection(unsigned char *Buffer, unsigned Size, |
| 71 | std::vector<std::string> &LongFilenames, |
| 72 | std::string *S) { |
| 73 | if (!LongFilenames.empty()) |
| 74 | return Error(S, "archive file contains multiple long filename entries"); |
| 75 | |
| 76 | while (Size) { |
| 77 | // Long filename entries are newline delimited to keep the archive readable. |
| 78 | unsigned char *Ptr = (unsigned char*)memchr(Buffer, '\n', Size); |
| 79 | if (Ptr == 0) |
| 80 | return Error(S, "archive long filename entry doesn't end with newline!"); |
| 81 | assert(*Ptr == '\n'); |
| 82 | |
| 83 | if (Ptr == Buffer) break; // Last entry contains just a newline. |
| 84 | |
| 85 | unsigned char *End = Ptr; |
| 86 | if (End[-1] == '/') --End; // Remove trailing / from name |
| 87 | |
| 88 | LongFilenames.push_back(std::string(Buffer, End)); |
| 89 | Size -= Ptr-Buffer+1; |
| 90 | Buffer = Ptr+1; |
| 91 | } |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 96 | static bool ParseSymbolTableSection(unsigned char *Buffer, unsigned Size, |
| 97 | std::string *S) { |
| 98 | // Currently not supported (succeeds without doing anything) |
| 99 | return false; |
| 100 | } |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 101 | |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 102 | static bool ReadArchiveBuffer(const std::string &ArchiveName, |
Chris Lattner | 75f2053 | 2003-04-22 18:02:52 +0000 | [diff] [blame] | 103 | unsigned char *Buffer, unsigned Length, |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 104 | std::vector<Module*> &Objects, |
| 105 | std::string *ErrorStr) { |
| 106 | if (Length < 8 || memcmp(Buffer, "!<arch>\n", 8)) |
| 107 | return Error(ErrorStr, "signature incorrect for an archive file!"); |
| 108 | Buffer += 8; Length -= 8; // Skip the magic string. |
| 109 | |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 110 | std::vector<char> LongFilenames; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 111 | |
| 112 | while (Length >= sizeof(ar_hdr)) { |
| 113 | ar_hdr *Hdr = (ar_hdr*)Buffer; |
| 114 | unsigned Size = atoi(Hdr->size); |
| 115 | if (Size+sizeof(ar_hdr) > Length) |
| 116 | return Error(ErrorStr, "invalid record length in archive file!"); |
| 117 | |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 118 | // Get name of archive member. |
| 119 | char *startp = Hdr->name; |
| 120 | char *endp = strchr (startp, '/'); |
| 121 | if (startp == endp && isdigit (Hdr->name[1])) { |
| 122 | // Long filenames are abbreviated as "/I", where I is a decimal |
| 123 | // index into the LongFilenames vector. |
| 124 | unsigned Index = atoi (&Hdr->name[1]); |
| 125 | assert (LongFilenames.size () > Index |
| 126 | && "Long filename for archive member not found"); |
| 127 | startp = &LongFilenames[Index]; |
| 128 | endp = strchr (startp, '/'); |
| 129 | } |
| 130 | std::string MemberName (startp, endp); |
| 131 | std::string FullMemberName = ArchiveName + "(" + MemberName + ")"; |
| 132 | |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 133 | switch (getObjectType(Hdr, Size)) { |
| 134 | case SVR4LongFilename: |
| 135 | // If this is a long filename section, read all of the file names into the |
| 136 | // LongFilenames vector. |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 137 | LongFilenames.assign (Buffer+sizeof(ar_hdr), Buffer+sizeof(ar_hdr)+Size); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 138 | break; |
| 139 | case UserObject: { |
Chris Lattner | 75f2053 | 2003-04-22 18:02:52 +0000 | [diff] [blame] | 140 | Module *M = ParseBytecodeBuffer(Buffer+sizeof(ar_hdr), Size, |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 141 | FullMemberName, ErrorStr); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 142 | if (!M) return true; |
| 143 | Objects.push_back(M); |
| 144 | break; |
| 145 | } |
Brian Gaeke | 2c61d7b | 2003-11-16 23:08:48 +0000 | [diff] [blame^] | 146 | case ArchiveSymbolTable: |
| 147 | if (ParseSymbolTableSection(Buffer+sizeof(ar_hdr), Size, ErrorStr)) |
| 148 | return true; |
| 149 | break; |
| 150 | default: |
| 151 | std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: " |
| 152 | << FullMemberName << "\n"; |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 153 | break; // Just ignore unknown files. |
| 154 | } |
| 155 | |
| 156 | // Round Size up to an even number... |
| 157 | Size = (Size+1)/2*2; |
| 158 | Buffer += sizeof(ar_hdr)+Size; // Move to the next entry |
| 159 | Length -= sizeof(ar_hdr)+Size; |
| 160 | } |
| 161 | |
| 162 | return Length != 0; |
| 163 | } |
| 164 | |
| 165 | |
Misha Brukman | 37f92e2 | 2003-09-11 22:34:13 +0000 | [diff] [blame] | 166 | // ReadArchiveFile - Read bytecode files from the specified .a file, returning |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 167 | // true on error, or false on success. This does not support reading files from |
| 168 | // standard input. |
| 169 | // |
| 170 | bool ReadArchiveFile(const std::string &Filename, std::vector<Module*> &Objects, |
| 171 | std::string *ErrorStr) { |
| 172 | int FD = open(Filename.c_str(), O_RDONLY); |
| 173 | if (FD == -1) |
| 174 | return Error(ErrorStr, "Error opening file!"); |
| 175 | |
| 176 | // Stat the file to get its length... |
| 177 | struct stat StatBuf; |
| 178 | if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0) |
| 179 | return Error(ErrorStr, "Error stat'ing file!"); |
| 180 | |
| 181 | // mmap in the file all at once... |
| 182 | int Length = StatBuf.st_size; |
| 183 | unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ, |
| 184 | MAP_PRIVATE, FD, 0); |
| 185 | if (Buffer == (unsigned char*)MAP_FAILED) |
| 186 | return Error(ErrorStr, "Error mmapping file!"); |
| 187 | |
| 188 | // Parse the archive files we mmap'ped in |
Chris Lattner | 75f2053 | 2003-04-22 18:02:52 +0000 | [diff] [blame] | 189 | bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr); |
Chris Lattner | 968cfd0 | 2003-04-19 21:45:34 +0000 | [diff] [blame] | 190 | |
| 191 | // Unmmap the archive... |
| 192 | munmap((char*)Buffer, Length); |
| 193 | |
| 194 | if (Result) // Free any loaded objects |
| 195 | while (!Objects.empty()) { |
| 196 | delete Objects.back(); |
| 197 | Objects.pop_back(); |
| 198 | } |
| 199 | |
| 200 | return Result; |
| 201 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 202 | |
| 203 | } // End llvm namespace |