blob: 1baaff289e3ca609d9b1e84a5099ac0296133efb [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"
John Criswell7a73b802003-06-30 21:59:07 +000021#include "Config/sys/stat.h"
22#include "Config/sys/mman.h"
23#include "Config/fcntl.h"
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000024#include <cstdlib>
Chris Lattner968cfd02003-04-19 21:45:34 +000025
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
Chris Lattner968cfd02003-04-19 21:45:34 +000028namespace {
29 struct ar_hdr {
30 char name[16];
31 char date[12];
32 char uid[6];
33 char gid[6];
34 char mode[8];
35 char size[10];
36 char fmag[2]; // Always equal to '`\n'
37 };
38
39 enum ObjectType {
40 UserObject, // A user .o/.bc file
41 Unknown, // Unknown file, just ignore it
42 SVR4LongFilename, // a "//" section used for long file names
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000043 ArchiveSymbolTable, // Symbol table produced by ranlib.
Chris Lattner968cfd02003-04-19 21:45:34 +000044 };
45}
46
Brian Gaeke3c096362003-12-11 00:38:04 +000047/// getObjectType - Determine the type of object that this header represents.
48/// This is capable of parsing the variety of special sections used for various
49/// purposes.
50///
51static enum ObjectType getObjectType(ar_hdr *H, unsigned char *MemberData,
52 unsigned Size) {
Chris Lattner968cfd02003-04-19 21:45:34 +000053 // Check for sections with special names...
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000054 if (!memcmp(H->name, "__.SYMDEF ", 16))
55 return ArchiveSymbolTable;
Brian Gaeke6ca5b8f2003-12-17 00:18:18 +000056 if (!memcmp(H->name, "__.SYMDEF SORTED", 16))
57 return ArchiveSymbolTable;
Chris Lattner968cfd02003-04-19 21:45:34 +000058 if (!memcmp(H->name, "// ", 16))
59 return SVR4LongFilename;
60
61 // Check to see if it looks like an llvm object file...
Brian Gaeke3c096362003-12-11 00:38:04 +000062 if (Size >= 4 && !memcmp(MemberData, "llvm", 4))
Chris Lattner968cfd02003-04-19 21:45:34 +000063 return UserObject;
64
65 return Unknown;
66}
67
Chris Lattner968cfd02003-04-19 21:45:34 +000068static inline bool Error(std::string *ErrorStr, const char *Message) {
69 if (ErrorStr) *ErrorStr = Message;
70 return true;
71}
72
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000073static bool ParseSymbolTableSection(unsigned char *Buffer, unsigned Size,
74 std::string *S) {
75 // Currently not supported (succeeds without doing anything)
76 return false;
77}
Chris Lattner968cfd02003-04-19 21:45:34 +000078
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000079static bool ReadArchiveBuffer(const std::string &ArchiveName,
Chris Lattner75f20532003-04-22 18:02:52 +000080 unsigned char *Buffer, unsigned Length,
Chris Lattner968cfd02003-04-19 21:45:34 +000081 std::vector<Module*> &Objects,
82 std::string *ErrorStr) {
83 if (Length < 8 || memcmp(Buffer, "!<arch>\n", 8))
84 return Error(ErrorStr, "signature incorrect for an archive file!");
85 Buffer += 8; Length -= 8; // Skip the magic string.
86
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000087 std::vector<char> LongFilenames;
Chris Lattner968cfd02003-04-19 21:45:34 +000088
89 while (Length >= sizeof(ar_hdr)) {
90 ar_hdr *Hdr = (ar_hdr*)Buffer;
Brian Gaeke3c096362003-12-11 00:38:04 +000091 unsigned SizeFromHeader = atoi(Hdr->size);
92 if (SizeFromHeader + sizeof(ar_hdr) > Length)
Chris Lattner968cfd02003-04-19 21:45:34 +000093 return Error(ErrorStr, "invalid record length in archive file!");
94
Brian Gaeke3c096362003-12-11 00:38:04 +000095 unsigned char *MemberData = Buffer + sizeof(ar_hdr);
96 unsigned MemberSize = SizeFromHeader;
Brian Gaeke2c61d7b2003-11-16 23:08:48 +000097 // Get name of archive member.
98 char *startp = Hdr->name;
Brian Gaeke3c096362003-12-11 00:38:04 +000099 char *endp = (char *) memchr (startp, '/', sizeof(ar_hdr));
100 if (memcmp (Hdr->name, "#1/", 3) == 0) {
101 // 4.4BSD/MacOSX long filenames are abbreviated as "#1/L", where L is an
102 // ASCII-coded decimal number representing the length of the name buffer,
103 // which is prepended to the archive member's contents.
104 unsigned NameLength = atoi (&Hdr->name[3]);
105 startp = (char *) MemberData;
106 endp = startp + NameLength;
107 MemberData += NameLength;
108 MemberSize -= NameLength;
109 } else if (startp == endp && isdigit (Hdr->name[1])) {
110 // SVR4 long filenames are abbreviated as "/I", where I is
111 // an ASCII-coded decimal index into the LongFilenames vector.
112 unsigned NameIndex = atoi (&Hdr->name[1]);
113 assert (LongFilenames.size () > NameIndex
114 && "SVR4-style long filename for archive member not found");
115 startp = &LongFilenames[NameIndex];
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000116 endp = strchr (startp, '/');
117 }
Brian Gaeke3c096362003-12-11 00:38:04 +0000118 if (!endp) {
119 // 4.4BSD/MacOSX *short* filenames are not guaranteed to have a
120 // terminator. Start at the end of the field and backtrack over spaces.
121 endp = startp + sizeof(Hdr->name);
122 while (endp[-1] == ' ')
123 --endp;
124 }
John Criswell1118d0f2003-12-20 22:37:29 +0000125
126 //
127 // We now have the beginning and the end of the object name.
128 // Convert this into a dynamically allocated std::string to pass
129 // to the routines that create the Module object. We do this
130 // (I think) because the created Module object will outlive this function,
131 // but statically declared std::string's won't.
132 //
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000133 std::string MemberName (startp, endp);
John Criswell1118d0f2003-12-20 22:37:29 +0000134 std::string * FullMemberName;
135 FullMemberName = new std::string (ArchiveName + "(" + MemberName + ")");
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000136
Brian Gaeke3c096362003-12-11 00:38:04 +0000137 switch (getObjectType(Hdr, MemberData, MemberSize)) {
Chris Lattner968cfd02003-04-19 21:45:34 +0000138 case SVR4LongFilename:
139 // If this is a long filename section, read all of the file names into the
140 // LongFilenames vector.
Brian Gaeke3c096362003-12-11 00:38:04 +0000141 LongFilenames.assign (MemberData, MemberData + MemberSize);
Chris Lattner968cfd02003-04-19 21:45:34 +0000142 break;
143 case UserObject: {
Brian Gaeke3c096362003-12-11 00:38:04 +0000144 Module *M = ParseBytecodeBuffer(MemberData, MemberSize,
John Criswell1118d0f2003-12-20 22:37:29 +0000145 *(FullMemberName), ErrorStr);
Chris Lattner968cfd02003-04-19 21:45:34 +0000146 if (!M) return true;
147 Objects.push_back(M);
148 break;
149 }
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000150 case ArchiveSymbolTable:
Brian Gaeke3c096362003-12-11 00:38:04 +0000151 if (ParseSymbolTableSection(MemberData, MemberSize, ErrorStr))
Brian Gaeke2c61d7b2003-11-16 23:08:48 +0000152 return true;
153 break;
154 default:
155 std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: "
John Criswell1118d0f2003-12-20 22:37:29 +0000156 << *(FullMemberName) << "\n";
Chris Lattner968cfd02003-04-19 21:45:34 +0000157 break; // Just ignore unknown files.
158 }
159
Brian Gaeke3c096362003-12-11 00:38:04 +0000160 // Round SizeFromHeader up to an even number...
161 SizeFromHeader = (SizeFromHeader+1)/2*2;
162 Buffer += sizeof(ar_hdr)+SizeFromHeader; // Move to the next entry
163 Length -= sizeof(ar_hdr)+SizeFromHeader;
Chris Lattner968cfd02003-04-19 21:45:34 +0000164 }
165
166 return Length != 0;
167}
168
169
Misha Brukman37f92e22003-09-11 22:34:13 +0000170// ReadArchiveFile - Read bytecode files from the specified .a file, returning
Chris Lattner968cfd02003-04-19 21:45:34 +0000171// true on error, or false on success. This does not support reading files from
172// standard input.
173//
174bool ReadArchiveFile(const std::string &Filename, std::vector<Module*> &Objects,
175 std::string *ErrorStr) {
176 int FD = open(Filename.c_str(), O_RDONLY);
177 if (FD == -1)
178 return Error(ErrorStr, "Error opening file!");
179
180 // Stat the file to get its length...
181 struct stat StatBuf;
182 if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
183 return Error(ErrorStr, "Error stat'ing file!");
184
185 // mmap in the file all at once...
186 int Length = StatBuf.st_size;
187 unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ,
188 MAP_PRIVATE, FD, 0);
189 if (Buffer == (unsigned char*)MAP_FAILED)
190 return Error(ErrorStr, "Error mmapping file!");
191
192 // Parse the archive files we mmap'ped in
Chris Lattner75f20532003-04-22 18:02:52 +0000193 bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr);
Chris Lattner968cfd02003-04-19 21:45:34 +0000194
195 // Unmmap the archive...
196 munmap((char*)Buffer, Length);
197
198 if (Result) // Free any loaded objects
199 while (!Objects.empty()) {
200 delete Objects.back();
201 Objects.pop_back();
202 }
203
204 return Result;
205}
Brian Gaeked0fde302003-11-11 22:41:34 +0000206
207} // End llvm namespace