1 //===- ArchiveReader.cpp - Code to read LLVM bytecode from .a files -------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Bytecode/Reader.h"
20 #include "llvm/Module.h"
21 #include "Support/FileUtilities.h"
22 #include "Config/sys/mman.h"
23 #include "Config/fcntl.h"
35 char fmag[2]; // Always equal to '`\n'
39 UserObject, // A user .o/.bc file
40 Unknown, // Unknown file, just ignore it
41 SVR4LongFilename, // a "//" section used for long file names
42 ArchiveSymbolTable, // Symbol table produced by ranlib.
46 /// getObjectType - Determine the type of object that this header represents.
47 /// This is capable of parsing the variety of special sections used for various
50 static enum ObjectType getObjectType(ar_hdr *H, std::string MemberName,
51 unsigned char *MemberData, unsigned Size) {
52 // Check for sections with special names...
53 if (MemberName == "__.SYMDEF " || MemberName == "__.SYMDEF SORTED")
54 return ArchiveSymbolTable;
55 else if (MemberName == "// ")
56 return SVR4LongFilename;
58 // Check to see if it looks like an llvm object file...
59 if (Size >= 4 && !memcmp(MemberData, "llvm", 4))
65 static inline bool Error(std::string *ErrorStr, const char *Message) {
66 if (ErrorStr) *ErrorStr = Message;
70 static bool ParseSymbolTableSection(unsigned char *Buffer, unsigned Size,
72 // Currently not supported (succeeds without doing anything)
76 static bool ReadArchiveBuffer(const std::string &ArchiveName,
77 unsigned char *Buffer, unsigned Length,
78 std::vector<Module*> &Objects,
79 std::string *ErrorStr) {
80 if (Length < 8 || memcmp(Buffer, "!<arch>\n", 8))
81 return Error(ErrorStr, "signature incorrect for an archive file!");
82 Buffer += 8; Length -= 8; // Skip the magic string.
84 std::vector<char> LongFilenames;
86 while (Length >= sizeof(ar_hdr)) {
87 ar_hdr *Hdr = (ar_hdr*)Buffer;
88 unsigned SizeFromHeader = atoi(Hdr->size);
89 if (SizeFromHeader + sizeof(ar_hdr) > Length)
90 return Error(ErrorStr, "invalid record length in archive file!");
92 unsigned char *MemberData = Buffer + sizeof(ar_hdr);
93 unsigned MemberSize = SizeFromHeader;
94 // Get name of archive member.
95 char *startp = Hdr->name;
96 char *endp = (char *) memchr (startp, '/', sizeof(ar_hdr));
97 if (memcmp (Hdr->name, "#1/", 3) == 0) {
98 // 4.4BSD/MacOSX long filenames are abbreviated as "#1/L", where L is an
99 // ASCII-coded decimal number representing the length of the name buffer,
100 // which is prepended to the archive member's contents.
101 unsigned NameLength = atoi (&Hdr->name[3]);
102 startp = (char *) MemberData;
103 endp = startp + NameLength;
104 MemberData += NameLength;
105 MemberSize -= NameLength;
106 } else if (startp == endp && isdigit (Hdr->name[1])) {
107 // SVR4 long filenames are abbreviated as "/I", where I is
108 // an ASCII-coded decimal index into the LongFilenames vector.
109 unsigned NameIndex = atoi (&Hdr->name[1]);
110 assert (LongFilenames.size () > NameIndex
111 && "SVR4-style long filename for archive member not found");
112 startp = &LongFilenames[NameIndex];
113 endp = strchr (startp, '/');
114 } else if (startp == endp && Hdr->name[1] == '/') {
115 // This is for the SVR4 long filename table (there might be other
116 // names starting with // but I don't know about them). Make sure that
117 // getObjectType sees it.
118 endp = &Hdr->name[sizeof (Hdr->name)];
121 // 4.4BSD/MacOSX *short* filenames are not guaranteed to have a
122 // terminator. Start at the end of the field and backtrack over spaces.
123 endp = startp + sizeof(Hdr->name);
124 while (endp[-1] == ' ')
127 std::string MemberName (startp, endp);
128 std::string FullMemberName = ArchiveName + "(" + MemberName + ")";
130 switch (getObjectType(Hdr, MemberName, MemberData, MemberSize)) {
131 case SVR4LongFilename:
132 // If this is a long filename section, read all of the file names into the
133 // LongFilenames vector.
134 LongFilenames.assign (MemberData, MemberData + MemberSize);
137 Module *M = ParseBytecodeBuffer(MemberData, MemberSize,
138 FullMemberName, ErrorStr);
140 Objects.push_back(M);
143 case ArchiveSymbolTable:
144 if (ParseSymbolTableSection(MemberData, MemberSize, ErrorStr))
148 std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: "
149 << FullMemberName << "\n";
150 break; // Just ignore unknown files.
153 // Round SizeFromHeader up to an even number...
154 SizeFromHeader = (SizeFromHeader+1)/2*2;
155 Buffer += sizeof(ar_hdr)+SizeFromHeader; // Move to the next entry
156 Length -= sizeof(ar_hdr)+SizeFromHeader;
163 // ReadArchiveFile - Read bytecode files from the specified .a file, returning
164 // true on error, or false on success. This does not support reading files from
167 bool llvm::ReadArchiveFile(const std::string &Filename,
168 std::vector<Module*> &Objects,std::string *ErrorStr){
169 int Length = getFileSize(Filename);
171 return Error(ErrorStr, "Error getting file length!");
173 int FD = open(Filename.c_str(), O_RDONLY);
175 return Error(ErrorStr, "Error opening file!");
177 // mmap in the file all at once...
178 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
180 if (Buffer == (unsigned char*)MAP_FAILED)
181 return Error(ErrorStr, "Error mmapping file!");
183 // Parse the archive files we mmap'ped in
184 bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr);
186 // Unmmap the archive...
187 munmap((char*)Buffer, Length);
189 if (Result) // Free any loaded objects
190 while (!Objects.empty()) {
191 delete Objects.back();