Implement a simple target-independent CFG cleanup pass
[oota-llvm.git] / lib / Archive / ArchiveReader.cpp
index b147cb3344e04270a395690f0d6736bf79785db3..1d8530fc785838e023fdb5a5f1831b886d35820f 100644 (file)
 
 #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 <cstdlib>
-
-namespace llvm {
+#include <iostream>
+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<Module*> &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<Module*> &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<Module*> &Objects,
   
   return Result;
 }
-
-} // End llvm namespace