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 prevalent 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(const std::string &Filename,
86 unsigned char *Buffer, unsigned Length,
87 std::vector<Module*> &Objects,
88 std::string *ErrorStr) {
89 if (Length < 8 || memcmp(Buffer, "!<arch>\n", 8))
90 return Error(ErrorStr, "signature incorrect for an archive file!");
91 Buffer += 8; Length -= 8; // Skip the magic string.
93 std::vector<std::string> LongFilenames;
95 while (Length >= sizeof(ar_hdr)) {
96 ar_hdr *Hdr = (ar_hdr*)Buffer;
97 unsigned Size = atoi(Hdr->size);
98 if (Size+sizeof(ar_hdr) > Length)
99 return Error(ErrorStr, "invalid record length in archive file!");
101 switch (getObjectType(Hdr, Size)) {
102 case SVR4LongFilename:
103 // If this is a long filename section, read all of the file names into the
104 // LongFilenames vector.
106 if (ParseLongFilenameSection(Buffer+sizeof(ar_hdr), Size,
107 LongFilenames, ErrorStr))
111 Module *M = ParseBytecodeBuffer(Buffer+sizeof(ar_hdr), Size,
112 Filename+":somefile", ErrorStr);
114 Objects.push_back(M);
118 std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: ";
119 std::cerr << std::string(Hdr->name, Hdr->name+sizeof(Hdr->name+1)) <<"\n";
120 break; // Just ignore unknown files.
123 // Round Size up to an even number...
125 Buffer += sizeof(ar_hdr)+Size; // Move to the next entry
126 Length -= sizeof(ar_hdr)+Size;
133 // ReadArchiveFile - Read bytecode files from the specfied .a file, returning
134 // true on error, or false on success. This does not support reading files from
137 bool ReadArchiveFile(const std::string &Filename, std::vector<Module*> &Objects,
138 std::string *ErrorStr) {
139 int FD = open(Filename.c_str(), O_RDONLY);
141 return Error(ErrorStr, "Error opening file!");
143 // Stat the file to get its length...
145 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
146 return Error(ErrorStr, "Error stat'ing file!");
148 // mmap in the file all at once...
149 int Length = StatBuf.st_size;
150 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
152 if (Buffer == (unsigned char*)MAP_FAILED)
153 return Error(ErrorStr, "Error mmapping file!");
155 // Parse the archive files we mmap'ped in
156 bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr);
158 // Unmmap the archive...
159 munmap((char*)Buffer, Length);
161 if (Result) // Free any loaded objects
162 while (!Objects.empty()) {
163 delete Objects.back();