1 //===- ReadArchive.cpp - Code to read LLVM bytecode from .a files ---------===//
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
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Bytecode/Reader.h"
13 #include "llvm/Module.h"
26 char fmag[2]; // Always equal to '`\n'
30 UserObject, // A user .o/.bc file
31 Unknown, // Unknown file, just ignore it
32 SVR4LongFilename, // a "//" section used for long file names
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
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;
45 // Check to see if it looks like an llvm object file...
46 if (Size >= 4 && !memcmp(H+1, "llvm", 4))
53 static inline bool Error(std::string *ErrorStr, const char *Message) {
54 if (ErrorStr) *ErrorStr = Message;
58 static bool ParseLongFilenameSection(unsigned char *Buffer, unsigned Size,
59 std::vector<std::string> &LongFilenames,
61 if (!LongFilenames.empty())
62 return Error(S, "archive file contains multiple long filename entries");
65 // Long filename entries are newline delimited to keep the archive readable.
66 unsigned char *Ptr = (unsigned char*)memchr(Buffer, '\n', Size);
68 return Error(S, "archive long filename entry doesn't end with newline!");
71 if (Ptr == Buffer) break; // Last entry contains just a newline.
73 unsigned char *End = Ptr;
74 if (End[-1] == '/') --End; // Remove trailing / from name
76 LongFilenames.push_back(std::string(Buffer, End));
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.
92 std::vector<std::string> LongFilenames;
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!");
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.
105 if (ParseLongFilenameSection(Buffer+sizeof(ar_hdr), Size,
106 LongFilenames, ErrorStr))
110 Module *M = ParseBytecodeBuffer(Buffer+sizeof(ar_hdr), Size, ErrorStr);
112 Objects.push_back(M);
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.
121 // Round Size up to an even number...
123 Buffer += sizeof(ar_hdr)+Size; // Move to the next entry
124 Length -= sizeof(ar_hdr)+Size;
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
135 bool ReadArchiveFile(const std::string &Filename, std::vector<Module*> &Objects,
136 std::string *ErrorStr) {
137 int FD = open(Filename.c_str(), O_RDONLY);
139 return Error(ErrorStr, "Error opening file!");
141 // Stat the file to get its length...
143 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
144 return Error(ErrorStr, "Error stat'ing file!");
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,
150 if (Buffer == (unsigned char*)MAP_FAILED)
151 return Error(ErrorStr, "Error mmapping file!");
153 // Parse the archive files we mmap'ped in
154 bool Result = ReadArchiveBuffer(Buffer, Length, Objects, ErrorStr);
156 // Unmmap the archive...
157 munmap((char*)Buffer, Length);
159 if (Result) // Free any loaded objects
160 while (!Objects.empty()) {
161 delete Objects.back();