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, unsigned char *MemberData,
52 // Check for sections with special names...
53 if (!memcmp(H->name, "__.SYMDEF ", 16))
54 return ArchiveSymbolTable;
55 if (!memcmp(H->name, "__.SYMDEF SORTED", 16))
56 return ArchiveSymbolTable;
57 if (!memcmp(H->name, "// ", 16))
58 return SVR4LongFilename;
60 // Check to see if it looks like an llvm object file...
61 if (Size >= 4 && !memcmp(MemberData, "llvm", 4))
67 static inline bool Error(std::string *ErrorStr, const char *Message) {
68 if (ErrorStr) *ErrorStr = Message;
72 static bool ParseSymbolTableSection(unsigned char *Buffer, unsigned Size,
74 // Currently not supported (succeeds without doing anything)
78 static bool ReadArchiveBuffer(const std::string &ArchiveName,
79 unsigned char *Buffer, unsigned Length,
80 std::vector<Module*> &Objects,
81 std::string *ErrorStr) {
82 if (Length < 8 || memcmp(Buffer, "!<arch>\n", 8))
83 return Error(ErrorStr, "signature incorrect for an archive file!");
84 Buffer += 8; Length -= 8; // Skip the magic string.
86 std::vector<char> LongFilenames;
88 while (Length >= sizeof(ar_hdr)) {
89 ar_hdr *Hdr = (ar_hdr*)Buffer;
90 unsigned SizeFromHeader = atoi(Hdr->size);
91 if (SizeFromHeader + sizeof(ar_hdr) > Length)
92 return Error(ErrorStr, "invalid record length in archive file!");
94 unsigned char *MemberData = Buffer + sizeof(ar_hdr);
95 unsigned MemberSize = SizeFromHeader;
96 // Get name of archive member.
97 char *startp = Hdr->name;
98 char *endp = (char *) memchr (startp, '/', sizeof(ar_hdr));
99 if (memcmp (Hdr->name, "#1/", 3) == 0) {
100 // 4.4BSD/MacOSX long filenames are abbreviated as "#1/L", where L is an
101 // ASCII-coded decimal number representing the length of the name buffer,
102 // which is prepended to the archive member's contents.
103 unsigned NameLength = atoi (&Hdr->name[3]);
104 startp = (char *) MemberData;
105 endp = startp + NameLength;
106 MemberData += NameLength;
107 MemberSize -= NameLength;
108 } else if (startp == endp && isdigit (Hdr->name[1])) {
109 // SVR4 long filenames are abbreviated as "/I", where I is
110 // an ASCII-coded decimal index into the LongFilenames vector.
111 unsigned NameIndex = atoi (&Hdr->name[1]);
112 assert (LongFilenames.size () > NameIndex
113 && "SVR4-style long filename for archive member not found");
114 startp = &LongFilenames[NameIndex];
115 endp = strchr (startp, '/');
118 // 4.4BSD/MacOSX *short* filenames are not guaranteed to have a
119 // terminator. Start at the end of the field and backtrack over spaces.
120 endp = startp + sizeof(Hdr->name);
121 while (endp[-1] == ' ')
124 std::string MemberName (startp, endp);
125 std::string FullMemberName = ArchiveName + "(" + MemberName + ")";
127 switch (getObjectType(Hdr, MemberData, MemberSize)) {
128 case SVR4LongFilename:
129 // If this is a long filename section, read all of the file names into the
130 // LongFilenames vector.
131 LongFilenames.assign (MemberData, MemberData + MemberSize);
134 Module *M = ParseBytecodeBuffer(MemberData, MemberSize,
135 FullMemberName, ErrorStr);
137 Objects.push_back(M);
140 case ArchiveSymbolTable:
141 if (ParseSymbolTableSection(MemberData, MemberSize, ErrorStr))
145 std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: "
146 << FullMemberName << "\n";
147 break; // Just ignore unknown files.
150 // Round SizeFromHeader up to an even number...
151 SizeFromHeader = (SizeFromHeader+1)/2*2;
152 Buffer += sizeof(ar_hdr)+SizeFromHeader; // Move to the next entry
153 Length -= sizeof(ar_hdr)+SizeFromHeader;
160 // ReadArchiveFile - Read bytecode files from the specified .a file, returning
161 // true on error, or false on success. This does not support reading files from
164 bool llvm::ReadArchiveFile(const std::string &Filename,
165 std::vector<Module*> &Objects,std::string *ErrorStr){
166 int Length = getFileSize(Filename);
168 return Error(ErrorStr, "Error getting file length!");
170 int FD = open(Filename.c_str(), O_RDONLY);
172 return Error(ErrorStr, "Error opening file!");
174 // mmap in the file all at once...
175 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
177 if (Buffer == (unsigned char*)MAP_FAILED)
178 return Error(ErrorStr, "Error mmapping file!");
180 // Parse the archive files we mmap'ped in
181 bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr);
183 // Unmmap the archive...
184 munmap((char*)Buffer, Length);
186 if (Result) // Free any loaded objects
187 while (!Objects.empty()) {
188 delete Objects.back();