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 "llvm/Support/FileUtilities.h"
34 char fmag[2]; // Always equal to '`\n'
38 UserObject, // A user .o/.bc file
39 Unknown, // Unknown file, just ignore it
40 SVR4LongFilename, // a "//" section used for long file names
41 ArchiveSymbolTable, // Symbol table produced by ranlib.
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
49 static enum ObjectType getObjectType(ar_hdr *H, std::string MemberName,
50 unsigned char *MemberData, unsigned Size) {
51 // Check for sections with special names...
52 if (MemberName == "__.SYMDEF " || MemberName == "__.SYMDEF SORTED")
53 return ArchiveSymbolTable;
54 else if (MemberName == "// ")
55 return SVR4LongFilename;
57 // Check to see if it looks like an llvm object file...
58 if (Size >= 4 && !memcmp(MemberData, "llvm", 4))
61 if (Size >= 4 && !memcmp(MemberData, "llvc", 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, '/');
116 } else if (startp == endp && Hdr->name[1] == '/') {
117 // This is for the SVR4 long filename table (there might be other
118 // names starting with // but I don't know about them). Make sure that
119 // getObjectType sees it.
120 endp = &Hdr->name[sizeof (Hdr->name)];
123 // 4.4BSD/MacOSX *short* filenames are not guaranteed to have a
124 // terminator. Start at the end of the field and backtrack over spaces.
125 endp = startp + sizeof(Hdr->name);
126 while (endp[-1] == ' ')
129 std::string MemberName (startp, endp);
130 std::string FullMemberName = ArchiveName + "(" + MemberName + ")";
132 switch (getObjectType(Hdr, MemberName, MemberData, MemberSize)) {
133 case SVR4LongFilename:
134 // If this is a long filename section, read all of the file names into the
135 // LongFilenames vector.
136 LongFilenames.assign (MemberData, MemberData + MemberSize);
139 Module *M = ParseBytecodeBuffer(MemberData, MemberSize,
140 FullMemberName, ErrorStr);
142 Objects.push_back(M);
145 case ArchiveSymbolTable:
146 if (ParseSymbolTableSection(MemberData, MemberSize, ErrorStr))
150 std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: "
151 << FullMemberName << "\n";
152 break; // Just ignore unknown files.
155 // Round SizeFromHeader up to an even number...
156 SizeFromHeader = (SizeFromHeader+1)/2*2;
157 Buffer += sizeof(ar_hdr)+SizeFromHeader; // Move to the next entry
158 Length -= sizeof(ar_hdr)+SizeFromHeader;
165 // ReadArchiveFile - Read bytecode files from the specified .a file, returning
166 // true on error, or false on success. This does not support reading files from
169 bool llvm::ReadArchiveFile(const std::string &Filename,
170 std::vector<Module*> &Objects,std::string *ErrorStr){
173 // mmap in the file all at once...
174 unsigned char *Buffer =
175 (unsigned char*)ReadFileIntoAddressSpace(Filename, Length);
177 if (ErrorStr) *ErrorStr = "Error reading file '" + Filename + "'!";
181 // Parse the archive files we mmap'ped in
182 bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr);
184 // Unmmap the archive...
185 UnmapFileFromAddressSpace(Buffer, Length);
187 if (Result) // Free any loaded objects
188 while (!Objects.empty()) {
189 delete Objects.back();