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