b6c389523f4b1481a240ee86edd5a631aac9ea3d
[oota-llvm.git] / lib / Archive / ArchiveReader.cpp
1 //===- ReadArchive.cpp - Code to read LLVM bytecode from .a files ---------===//
2 //
3 // This file implements the ReadArchiveFile interface, which allows a linker to
4 // read all of the LLVM bytecode files contained in a .a file.  This file
5 // understands the standard system .a file format.  This can only handle the .a
6 // variant prevelant on linux systems so far, but may be extended.  See
7 // information in this source file for more information:
8 //   http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/bfd/archive.c?cvsroot=src
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Bytecode/Reader.h"
13 #include "llvm/Module.h"
14 #include <sys/stat.h>
15 #include <sys/mman.h>
16 #include <fcntl.h>
17
18 namespace {
19   struct ar_hdr {
20     char name[16];
21     char date[12];
22     char uid[6];
23     char gid[6];
24     char mode[8];
25     char size[10];
26     char fmag[2];          // Always equal to '`\n'
27   };
28
29   enum ObjectType {
30     UserObject,            // A user .o/.bc file
31     Unknown,               // Unknown file, just ignore it
32     SVR4LongFilename,      // a "//" section used for long file names
33   };
34 }
35
36
37 // getObjectType - Determine the type of object that this header represents.
38 // This is capable of parsing the variety of special sections used for various
39 // purposes.
40 static enum ObjectType getObjectType(ar_hdr *H, unsigned Size) {
41   // Check for sections with special names...
42   if (!memcmp(H->name, "//              ", 16))
43     return SVR4LongFilename;
44
45   // Check to see if it looks like an llvm object file...
46   if (Size >= 4 && !memcmp(H+1, "llvm", 4))
47     return UserObject;
48
49   return Unknown;
50 }
51
52
53 static inline bool Error(std::string *ErrorStr, const char *Message) {
54   if (ErrorStr) *ErrorStr = Message;
55   return true;
56 }
57
58 static bool ParseLongFilenameSection(unsigned char *Buffer, unsigned Size,
59                                      std::vector<std::string> &LongFilenames,
60                                      std::string *S) {
61   if (!LongFilenames.empty())
62     return Error(S, "archive file contains multiple long filename entries");
63                  
64   while (Size) {
65     // Long filename entries are newline delimited to keep the archive readable.
66     unsigned char *Ptr = (unsigned char*)memchr(Buffer, '\n', Size);
67     if (Ptr == 0)
68       return Error(S, "archive long filename entry doesn't end with newline!");
69     assert(*Ptr == '\n');
70
71     if (Ptr == Buffer) break;  // Last entry contains just a newline.
72
73     unsigned char *End = Ptr;
74     if (End[-1] == '/') --End; // Remove trailing / from name
75     
76     LongFilenames.push_back(std::string(Buffer, End));
77     Size -= Ptr-Buffer+1;
78     Buffer = Ptr+1;
79   }
80   
81   return false;
82 }
83
84
85 static bool ReadArchiveBuffer(unsigned char *Buffer, unsigned Length,
86                               std::vector<Module*> &Objects,
87                               std::string *ErrorStr) {
88   if (Length < 8 || memcmp(Buffer, "!<arch>\n", 8))
89     return Error(ErrorStr, "signature incorrect for an archive file!");
90   Buffer += 8;  Length -= 8; // Skip the magic string.
91
92   std::vector<std::string> LongFilenames;
93
94   while (Length >= sizeof(ar_hdr)) {
95     ar_hdr *Hdr = (ar_hdr*)Buffer;
96     unsigned Size = atoi(Hdr->size);
97     if (Size+sizeof(ar_hdr) > Length)
98       return Error(ErrorStr, "invalid record length in archive file!");
99
100     switch (getObjectType(Hdr, Size)) {
101     case SVR4LongFilename:
102       // If this is a long filename section, read all of the file names into the
103       // LongFilenames vector.
104       //
105       if (ParseLongFilenameSection(Buffer+sizeof(ar_hdr), Size,
106                                    LongFilenames, ErrorStr))
107         return true;
108       break;
109     case UserObject: {
110       Module *M = ParseBytecodeBuffer(Buffer+sizeof(ar_hdr), Size, ErrorStr);
111       if (!M) return true;
112       Objects.push_back(M);
113       break;
114     }
115     case Unknown:
116       std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: ";
117       std::cerr << std::string(Hdr->name, Hdr->name+sizeof(Hdr->name+1)) <<"\n";
118       break;   // Just ignore unknown files.
119     }
120
121     // Round Size up to an even number...
122     Size = (Size+1)/2*2;
123     Buffer += sizeof(ar_hdr)+Size;   // Move to the next entry
124     Length -= sizeof(ar_hdr)+Size;
125   }
126
127   return Length != 0;
128 }
129
130
131 // ReadArchiveFile - Read bytecode files from the specfied .a file, returning
132 // true on error, or false on success.  This does not support reading files from
133 // standard input.
134 //
135 bool ReadArchiveFile(const std::string &Filename, std::vector<Module*> &Objects,
136                      std::string *ErrorStr) {
137   int FD = open(Filename.c_str(), O_RDONLY);
138   if (FD == -1)
139     return Error(ErrorStr, "Error opening file!");
140   
141   // Stat the file to get its length...
142   struct stat StatBuf;
143   if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
144     return Error(ErrorStr, "Error stat'ing file!");
145   
146     // mmap in the file all at once...
147   int Length = StatBuf.st_size;
148   unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ, 
149                                                MAP_PRIVATE, FD, 0);
150   if (Buffer == (unsigned char*)MAP_FAILED)
151     return Error(ErrorStr, "Error mmapping file!");
152   
153   // Parse the archive files we mmap'ped in
154   bool Result = ReadArchiveBuffer(Buffer, Length, Objects, ErrorStr);
155   
156   // Unmmap the archive...
157   munmap((char*)Buffer, Length);
158
159   if (Result)    // Free any loaded objects
160     while (!Objects.empty()) {
161       delete Objects.back();
162       Objects.pop_back();
163     }
164   
165   return Result;
166 }