X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FArchive%2FArchiveReader.cpp;h=1d8530fc785838e023fdb5a5f1831b886d35820f;hb=21ab22e47592d8a4046cfdac844d76b2cb76d711;hp=b147cb3344e04270a395690f0d6736bf79785db3;hpb=9059158b5613f9d87d2571ea67cef83d5f879106;p=oota-llvm.git diff --git a/lib/Archive/ArchiveReader.cpp b/lib/Archive/ArchiveReader.cpp index b147cb3344e..1d8530fc785 100644 --- a/lib/Archive/ArchiveReader.cpp +++ b/lib/Archive/ArchiveReader.cpp @@ -18,12 +18,10 @@ #include "llvm/Bytecode/Reader.h" #include "llvm/Module.h" -#include "Config/sys/stat.h" -#include "Config/sys/mman.h" -#include "Config/fcntl.h" +#include "Support/FileUtilities.h" #include - -namespace llvm { +#include +using namespace llvm; namespace { struct ar_hdr { @@ -48,14 +46,12 @@ namespace { /// This is capable of parsing the variety of special sections used for various /// purposes. /// -static enum ObjectType getObjectType(ar_hdr *H, unsigned char *MemberData, - unsigned Size) { +static enum ObjectType getObjectType(ar_hdr *H, std::string MemberName, + unsigned char *MemberData, unsigned Size) { // Check for sections with special names... - if (!memcmp(H->name, "__.SYMDEF ", 16)) - return ArchiveSymbolTable; - if (!memcmp(H->name, "__.SYMDEF SORTED", 16)) + if (MemberName == "__.SYMDEF " || MemberName == "__.SYMDEF SORTED") return ArchiveSymbolTable; - if (!memcmp(H->name, "// ", 16)) + else if (MemberName == "// ") return SVR4LongFilename; // Check to see if it looks like an llvm object file... @@ -114,6 +110,11 @@ static bool ReadArchiveBuffer(const std::string &ArchiveName, && "SVR4-style long filename for archive member not found"); startp = &LongFilenames[NameIndex]; endp = strchr (startp, '/'); + } 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)]; } if (!endp) { // 4.4BSD/MacOSX *short* filenames are not guaranteed to have a @@ -125,7 +126,7 @@ static bool ReadArchiveBuffer(const std::string &ArchiveName, std::string MemberName (startp, endp); std::string FullMemberName = ArchiveName + "(" + MemberName + ")"; - switch (getObjectType(Hdr, MemberData, MemberSize)) { + switch (getObjectType(Hdr, MemberName, MemberData, MemberSize)) { case SVR4LongFilename: // If this is a long filename section, read all of the file names into the // LongFilenames vector. @@ -162,29 +163,23 @@ static bool ReadArchiveBuffer(const std::string &ArchiveName, // true on error, or false on success. This does not support reading files from // standard input. // -bool ReadArchiveFile(const std::string &Filename, std::vector &Objects, - std::string *ErrorStr) { - int FD = open(Filename.c_str(), O_RDONLY); - if (FD == -1) - return Error(ErrorStr, "Error opening file!"); - - // Stat the file to get its length... - struct stat StatBuf; - if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0) - return Error(ErrorStr, "Error stat'ing file!"); - +bool llvm::ReadArchiveFile(const std::string &Filename, + std::vector &Objects,std::string *ErrorStr){ + unsigned Length; + // mmap in the file all at once... - int Length = StatBuf.st_size; - unsigned char *Buffer = (unsigned char*)mmap(0, Length, PROT_READ, - MAP_PRIVATE, FD, 0); - if (Buffer == (unsigned char*)MAP_FAILED) - return Error(ErrorStr, "Error mmapping file!"); + unsigned char *Buffer = + (unsigned char*)ReadFileIntoAddressSpace(Filename, Length); + if (Buffer == 0) { + if (ErrorStr) *ErrorStr = "Error reading file '" + Filename + "'!"; + return true; + } // Parse the archive files we mmap'ped in bool Result = ReadArchiveBuffer(Filename, Buffer, Length, Objects, ErrorStr); // Unmmap the archive... - munmap((char*)Buffer, Length); + UnmapFileFromAddressSpace(Buffer, Length); if (Result) // Free any loaded objects while (!Objects.empty()) { @@ -194,5 +189,3 @@ bool ReadArchiveFile(const std::string &Filename, std::vector &Objects, return Result; } - -} // End llvm namespace