2003-09-22 23:42:00 +00:00
|
|
|
//===- ArchiveReader.cpp - Code to read LLVM bytecode from .a files -------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-04-19 21:45:34 +00:00
|
|
|
//
|
|
|
|
// This file implements the ReadArchiveFile interface, which allows a linker to
|
|
|
|
// read all of the LLVM bytecode files contained in a .a file. This file
|
|
|
|
// understands the standard system .a file format. This can only handle the .a
|
2003-04-23 02:59:05 +00:00
|
|
|
// variant prevalent on Linux systems so far, but may be extended. See
|
2003-04-19 21:45:34 +00:00
|
|
|
// information in this source file for more information:
|
|
|
|
// http://sources.redhat.com/cgi-bin/cvsweb.cgi/src/bfd/archive.c?cvsroot=src
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Module.h"
|
2003-12-30 07:40:35 +00:00
|
|
|
#include "Support/FileUtilities.h"
|
2003-11-16 23:08:48 +00:00
|
|
|
#include <cstdlib>
|
2004-01-10 19:00:15 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-04-19 21:45:34 +00:00
|
|
|
namespace {
|
|
|
|
struct ar_hdr {
|
|
|
|
char name[16];
|
|
|
|
char date[12];
|
|
|
|
char uid[6];
|
|
|
|
char gid[6];
|
|
|
|
char mode[8];
|
|
|
|
char size[10];
|
|
|
|
char fmag[2]; // Always equal to '`\n'
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ObjectType {
|
|
|
|
UserObject, // A user .o/.bc file
|
|
|
|
Unknown, // Unknown file, just ignore it
|
|
|
|
SVR4LongFilename, // a "//" section used for long file names
|
2003-11-16 23:08:48 +00:00
|
|
|
ArchiveSymbolTable, // Symbol table produced by ranlib.
|
2003-04-19 21:45:34 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-12-11 00:38:04 +00:00
|
|
|
/// getObjectType - Determine the type of object that this header represents.
|
|
|
|
/// This is capable of parsing the variety of special sections used for various
|
|
|
|
/// purposes.
|
|
|
|
///
|
2004-03-31 19:51:00 +00:00
|
|
|
static enum ObjectType getObjectType(ar_hdr *H, std::string MemberName,
|
|
|
|
unsigned char *MemberData, unsigned Size) {
|
2003-04-19 21:45:34 +00:00
|
|
|
// Check for sections with special names...
|
2004-03-31 19:51:00 +00:00
|
|
|
if (MemberName == "__.SYMDEF " || MemberName == "__.SYMDEF SORTED")
|
2003-11-16 23:08:48 +00:00
|
|
|
return ArchiveSymbolTable;
|
2004-03-31 19:51:00 +00:00
|
|
|
else if (MemberName == "// ")
|
2003-04-19 21:45:34 +00:00
|
|
|
return SVR4LongFilename;
|
|
|
|
|
|
|
|
// Check to see if it looks like an llvm object file...
|
2003-12-11 00:38:04 +00:00
|
|
|
if (Size >= 4 && !memcmp(MemberData, "llvm", 4))
|
2003-04-19 21:45:34 +00:00
|
|
|
return UserObject;
|
|
|
|
|
|
|
|
return Unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool Error(std::string *ErrorStr, const char *Message) {
|
|
|
|
if (ErrorStr) *ErrorStr = Message;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-11-16 23:08:48 +00:00
|
|
|
static bool ParseSymbolTableSection(unsigned char *Buffer, unsigned Size,
|
|
|
|
std::string *S) {
|
|
|
|
// Currently not supported (succeeds without doing anything)
|
|
|
|
return false;
|
|
|
|
}
|
2003-04-19 21:45:34 +00:00
|
|
|
|
2003-11-16 23:08:48 +00:00
|
|
|
static bool ReadArchiveBuffer(const std::string &ArchiveName,
|
2003-04-22 18:02:52 +00:00
|
|
|
unsigned char *Buffer, unsigned Length,
|
2003-04-19 21:45:34 +00:00
|
|
|
std::vector<Module*> &Objects,
|
|
|
|
std::string *ErrorStr) {
|
|
|
|
if (Length < 8 || memcmp(Buffer, "!<arch>\n", 8))
|
|
|
|
return Error(ErrorStr, "signature incorrect for an archive file!");
|
|
|
|
Buffer += 8; Length -= 8; // Skip the magic string.
|
|
|
|
|
2003-11-16 23:08:48 +00:00
|
|
|
std::vector<char> LongFilenames;
|
2003-04-19 21:45:34 +00:00
|
|
|
|
|
|
|
while (Length >= sizeof(ar_hdr)) {
|
|
|
|
ar_hdr *Hdr = (ar_hdr*)Buffer;
|
2003-12-11 00:38:04 +00:00
|
|
|
unsigned SizeFromHeader = atoi(Hdr->size);
|
|
|
|
if (SizeFromHeader + sizeof(ar_hdr) > Length)
|
2003-04-19 21:45:34 +00:00
|
|
|
return Error(ErrorStr, "invalid record length in archive file!");
|
|
|
|
|
2003-12-11 00:38:04 +00:00
|
|
|
unsigned char *MemberData = Buffer + sizeof(ar_hdr);
|
|
|
|
unsigned MemberSize = SizeFromHeader;
|
2003-11-16 23:08:48 +00:00
|
|
|
// Get name of archive member.
|
|
|
|
char *startp = Hdr->name;
|
2003-12-11 00:38:04 +00:00
|
|
|
char *endp = (char *) memchr (startp, '/', sizeof(ar_hdr));
|
|
|
|
if (memcmp (Hdr->name, "#1/", 3) == 0) {
|
|
|
|
// 4.4BSD/MacOSX long filenames are abbreviated as "#1/L", where L is an
|
|
|
|
// ASCII-coded decimal number representing the length of the name buffer,
|
|
|
|
// which is prepended to the archive member's contents.
|
|
|
|
unsigned NameLength = atoi (&Hdr->name[3]);
|
|
|
|
startp = (char *) MemberData;
|
|
|
|
endp = startp + NameLength;
|
|
|
|
MemberData += NameLength;
|
|
|
|
MemberSize -= NameLength;
|
|
|
|
} else if (startp == endp && isdigit (Hdr->name[1])) {
|
|
|
|
// SVR4 long filenames are abbreviated as "/I", where I is
|
|
|
|
// an ASCII-coded decimal index into the LongFilenames vector.
|
|
|
|
unsigned NameIndex = atoi (&Hdr->name[1]);
|
|
|
|
assert (LongFilenames.size () > NameIndex
|
|
|
|
&& "SVR4-style long filename for archive member not found");
|
|
|
|
startp = &LongFilenames[NameIndex];
|
2003-11-16 23:08:48 +00:00
|
|
|
endp = strchr (startp, '/');
|
2004-03-31 19:51:00 +00:00
|
|
|
} else if (startp == endp && Hdr->name[1] == '/') {
|
|
|
|
// This is for the SVR4 long filename table (there might be other
|
|
|
|
// names starting with // but I don't know about them). Make sure that
|
|
|
|
// getObjectType sees it.
|
|
|
|
endp = &Hdr->name[sizeof (Hdr->name)];
|
2003-11-16 23:08:48 +00:00
|
|
|
}
|
2003-12-11 00:38:04 +00:00
|
|
|
if (!endp) {
|
|
|
|
// 4.4BSD/MacOSX *short* filenames are not guaranteed to have a
|
|
|
|
// terminator. Start at the end of the field and backtrack over spaces.
|
|
|
|
endp = startp + sizeof(Hdr->name);
|
|
|
|
while (endp[-1] == ' ')
|
|
|
|
--endp;
|
|
|
|
}
|
2003-11-16 23:08:48 +00:00
|
|
|
std::string MemberName (startp, endp);
|
2003-12-22 16:22:49 +00:00
|
|
|
std::string FullMemberName = ArchiveName + "(" + MemberName + ")";
|
2003-11-16 23:08:48 +00:00
|
|
|
|
2004-03-31 19:51:00 +00:00
|
|
|
switch (getObjectType(Hdr, MemberName, MemberData, MemberSize)) {
|
2003-04-19 21:45:34 +00:00
|
|
|
case SVR4LongFilename:
|
|
|
|
// If this is a long filename section, read all of the file names into the
|
|
|
|
// LongFilenames vector.
|
2003-12-11 00:38:04 +00:00
|
|
|
LongFilenames.assign (MemberData, MemberData + MemberSize);
|
2003-04-19 21:45:34 +00:00
|
|
|
break;
|
|
|
|
case UserObject: {
|
2003-12-11 00:38:04 +00:00
|
|
|
Module *M = ParseBytecodeBuffer(MemberData, MemberSize,
|
2003-12-22 16:22:49 +00:00
|
|
|
FullMemberName, ErrorStr);
|
2003-04-19 21:45:34 +00:00
|
|
|
if (!M) return true;
|
|
|
|
Objects.push_back(M);
|
|
|
|
break;
|
|
|
|
}
|
2003-11-16 23:08:48 +00:00
|
|
|
case ArchiveSymbolTable:
|
2003-12-11 00:38:04 +00:00
|
|
|
if (ParseSymbolTableSection(MemberData, MemberSize, ErrorStr))
|
2003-11-16 23:08:48 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cerr << "ReadArchiveBuffer: WARNING: Skipping unknown file: "
|
2003-12-22 16:22:49 +00:00
|
|
|
<< FullMemberName << "\n";
|
2003-04-19 21:45:34 +00:00
|
|
|
break; // Just ignore unknown files.
|
|
|
|
}
|
|
|
|
|
2003-12-11 00:38:04 +00:00
|
|
|
// Round SizeFromHeader up to an even number...
|
|
|
|
SizeFromHeader = (SizeFromHeader+1)/2*2;
|
|
|
|
Buffer += sizeof(ar_hdr)+SizeFromHeader; // Move to the next entry
|
|
|
|
Length -= sizeof(ar_hdr)+SizeFromHeader;
|
2003-04-19 21:45:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Length != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-11 22:34:13 +00:00
|
|
|
// ReadArchiveFile - Read bytecode files from the specified .a file, returning
|
2003-04-19 21:45:34 +00:00
|
|
|
// true on error, or false on success. This does not support reading files from
|
|
|
|
// standard input.
|
|
|
|
//
|
2004-01-10 19:00:15 +00:00
|
|
|
bool llvm::ReadArchiveFile(const std::string &Filename,
|
|
|
|
std::vector<Module*> &Objects,std::string *ErrorStr){
|
2004-05-28 00:24:41 +00:00
|
|
|
unsigned Length;
|
2003-12-30 07:40:35 +00:00
|
|
|
|
2003-04-19 21:45:34 +00:00
|
|
|
// mmap in the file all at once...
|
2004-05-28 00:24:41 +00:00
|
|
|
unsigned char *Buffer =
|
|
|
|
(unsigned char*)ReadFileIntoAddressSpace(Filename, Length);
|
|
|
|
if (Buffer == 0) {
|
|
|
|
if (ErrorStr) *ErrorStr = "Error reading file '" + Filename + "'!";
|
|
|
|
return true;
|
|
|
|
}
|
2003-04-19 21:45:34 +00:00
|
|
|
|
|
|
|
// Parse the archive files we mmap'ped in
|
2003-04-22 18:02:52 +00:00
|
|
|
bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr);
|
2003-04-19 21:45:34 +00:00
|
|
|
|
|
|
|
// Unmmap the archive...
|
2004-05-28 00:24:41 +00:00
|
|
|
UnmapFileFromAddressSpace(Buffer, Length);
|
2003-04-19 21:45:34 +00:00
|
|
|
|
|
|
|
if (Result) // Free any loaded objects
|
|
|
|
while (!Objects.empty()) {
|
|
|
|
delete Objects.back();
|
|
|
|
Objects.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|