blob: b8bb914eba764ac560591255067f8abb7a93858e [file] [log] [blame]
Chris Lattner23219d12003-09-22 23:42:00 +00001//===- ArchiveReader.cpp - Code to read LLVM bytecode from .a files -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner968cfd02003-04-19 21:45:34 +00009//
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
Misha Brukman09ff1502003-04-23 02:59:05 +000013// variant prevalent on Linux systems so far, but may be extended. See
Chris Lattner968cfd02003-04-19 21:45:34 +000014// information in this source file for more information:
15// http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/bfd/archive.c?cvsroot=src
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Bytecode/Reader.h"
20#include "llvm/Module.h"
Chris Lattnerb70abe12003-12-30 07:40:35 +000021#include "Support/FileUtilities.h"
John Criswell7a73b802003-06-30 21:59:07 +000022#include "Config/sys/mman.h"
23#include "Config/fcntl.h"
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000024#include <cstdlib>
Chris Lattner3446ae82004-01-10 19:00:15 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner968cfd02003-04-19 21:45:34 +000027namespace {
28 struct ar_hdr {
29 char name[16];
30 char date[12];
31 char uid[6];
32 char gid[6];
33 char mode[8];
34 char size[10];
35 char fmag[2]; // Always equal to '`\n'
36 };
37
38 enum ObjectType {
39 UserObject, // A user .o/.bc file
40 Unknown, // Unknown file, just ignore it
41 SVR4LongFilename, // a "//" section used for long file names
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000042 ArchiveSymbolTable, // Symbol table produced by ranlib.
Chris Lattner968cfd02003-04-19 21:45:34 +000043 };
44}
45
Brian Gaeke3c096362003-12-11 00:38:04 +000046/// getObjectType - Determine the type of object that this header represents.
47/// This is capable of parsing the variety of special sections used for various
48/// purposes.
49///
50static enum ObjectType getObjectType(ar_hdr *H, unsigned char *MemberData,
51 unsigned Size) {
Chris Lattner968cfd02003-04-19 21:45:34 +000052 // Check for sections with special names...
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000053 if (!memcmp(H->name, "__.SYMDEF ", 16))
54 return ArchiveSymbolTable;
Brian Gaeke6ca5b8f2003-12-17 00:18:18 +000055 if (!memcmp(H->name, "__.SYMDEF SORTED", 16))
56 return ArchiveSymbolTable;
Chris Lattner968cfd02003-04-19 21:45:34 +000057 if (!memcmp(H->name, "// ", 16))
58 return SVR4LongFilename;
59
60 // Check to see if it looks like an llvm object file...
Brian Gaeke3c096362003-12-11 00:38:04 +000061 if (Size >= 4 && !memcmp(MemberData, "llvm", 4))
Chris Lattner968cfd02003-04-19 21:45:34 +000062 return UserObject;
63
64 return Unknown;
65}
66
Chris Lattner968cfd02003-04-19 21:45:34 +000067static inline bool Error(std::string *ErrorStr, const char *Message) {
68 if (ErrorStr) *ErrorStr = Message;
69 return true;
70}
71
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000072static bool ParseSymbolTableSection(unsigned char *Buffer, unsigned Size,
73 std::string *S) {
74 // Currently not supported (succeeds without doing anything)
75 return false;
76}
Chris Lattner968cfd02003-04-19 21:45:34 +000077
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000078static bool ReadArchiveBuffer(const std::string &ArchiveName,
Chris Lattner75f20532003-04-22 18:02:52 +000079 unsigned char *Buffer, unsigned Length,
Chris Lattner968cfd02003-04-19 21:45:34 +000080 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.
85
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000086 std::vector<char> LongFilenames;
Chris Lattner968cfd02003-04-19 21:45:34 +000087
88 while (Length >= sizeof(ar_hdr)) {
89 ar_hdr *Hdr = (ar_hdr*)Buffer;
Brian Gaeke3c096362003-12-11 00:38:04 +000090 unsigned SizeFromHeader = atoi(Hdr->size);
91 if (SizeFromHeader + sizeof(ar_hdr) > Length)
Chris Lattner968cfd02003-04-19 21:45:34 +000092 return Error(ErrorStr, "invalid record length in archive file!");
93
Brian Gaeke3c096362003-12-11 00:38:04 +000094 unsigned char *MemberData = Buffer + sizeof(ar_hdr);
95 unsigned MemberSize = SizeFromHeader;
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000096 // Get name of archive member.
97 char *startp = Hdr->name;
Brian Gaeke3c096362003-12-11 00:38:04 +000098 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];
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000115 endp = strchr (startp, '/');
116 }
Brian Gaeke3c096362003-12-11 00:38:04 +0000117 if (!endp) {
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] == ' ')
122 --endp;
123 }
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000124 std::string MemberName (startp, endp);
John Criswell90591582003-12-22 16:22:49 +0000125 std::string FullMemberName = ArchiveName + "(" + MemberName + ")";
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000126
Brian Gaeke3c096362003-12-11 00:38:04 +0000127 switch (getObjectType(Hdr, MemberData, MemberSize)) {
Chris Lattner968cfd02003-04-19 21:45:34 +0000128 case SVR4LongFilename:
129 // If this is a long filename section, read all of the file names into the
130 // LongFilenames vector.
Brian Gaeke3c096362003-12-11 00:38:04 +0000131 LongFilenames.assign (MemberData, MemberData + MemberSize);
Chris Lattner968cfd02003-04-19 21:45:34 +0000132 break;
133 case UserObject: {
Brian Gaeke3c096362003-12-11 00:38:04 +0000134 Module *M = ParseBytecodeBuffer(MemberData, MemberSize,
John Criswell90591582003-12-22 16:22:49 +0000135 FullMemberName, ErrorStr);
Chris Lattner968cfd02003-04-19 21:45:34 +0000136 if (!M) return true;
137 Objects.push_back(M);
138 break;
139 }
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000140 case ArchiveSymbolTable:
Brian Gaeke3c096362003-12-11 00:38:04 +0000141 if (ParseSymbolTableSection(MemberData, MemberSize, ErrorStr))
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000142 return true;
143 break;
144 default:
145 std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: "
John Criswell90591582003-12-22 16:22:49 +0000146 << FullMemberName << "\n";
Chris Lattner968cfd02003-04-19 21:45:34 +0000147 break; // Just ignore unknown files.
148 }
149
Brian Gaeke3c096362003-12-11 00:38:04 +0000150 // 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;
Chris Lattner968cfd02003-04-19 21:45:34 +0000154 }
155
156 return Length != 0;
157}
158
159
Misha Brukman37f92e22003-09-11 22:34:13 +0000160// ReadArchiveFile - Read bytecode files from the specified .a file, returning
Chris Lattner968cfd02003-04-19 21:45:34 +0000161// true on error, or false on success. This does not support reading files from
162// standard input.
163//
Chris Lattner3446ae82004-01-10 19:00:15 +0000164bool llvm::ReadArchiveFile(const std::string &Filename,
165 std::vector<Module*> &Objects,std::string *ErrorStr){
Chris Lattnerb70abe12003-12-30 07:40:35 +0000166 int Length = getFileSize(Filename);
167 if (Length == -1)
168 return Error(ErrorStr, "Error getting file length!");
169
Chris Lattner968cfd02003-04-19 21:45:34 +0000170 int FD = open(Filename.c_str(), O_RDONLY);
171 if (FD == -1)
172 return Error(ErrorStr, "Error opening file!");
173
Chris Lattner968cfd02003-04-19 21:45:34 +0000174 // mmap in the file all at once...
Chris Lattner968cfd02003-04-19 21:45:34 +0000175 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
176 MAP_PRIVATE, FD, 0);
177 if (Buffer == (unsigned char*)MAP_FAILED)
178 return Error(ErrorStr, "Error mmapping file!");
179
180 // Parse the archive files we mmap'ped in
Chris Lattner75f20532003-04-22 18:02:52 +0000181 bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr);
Chris Lattner968cfd02003-04-19 21:45:34 +0000182
183 // Unmmap the archive...
184 munmap((char*)Buffer, Length);
185
186 if (Result) // Free any loaded objects
187 while (!Objects.empty()) {
188 delete Objects.back();
189 Objects.pop_back();
190 }
191
192 return Result;
193}