LLVM API Documentation

Path.cpp

Go to the documentation of this file.
00001 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This header file implements the operating system Path concept.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/System/Path.h"
00015 #include "llvm/Config/config.h"
00016 #include "llvm/Support/ErrorHandling.h"
00017 #include <cassert>
00018 #include <cstring>
00019 #include <ostream>
00020 using namespace llvm;
00021 using namespace sys;
00022 
00023 //===----------------------------------------------------------------------===//
00024 //=== WARNING: Implementation here must contain only TRULY operating system
00025 //===          independent code.
00026 //===----------------------------------------------------------------------===//
00027 
00028 bool Path::operator==(const Path &that) const {
00029   return path == that.path;
00030 }
00031 
00032 bool Path::operator!=(const Path &that) const {
00033   return path != that.path;
00034 }
00035 
00036 bool Path::operator<(const Path& that) const {
00037   return path < that.path;
00038 }
00039 
00040 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
00041   strm << aPath.toString();
00042   return strm;
00043 }
00044 
00045 Path
00046 Path::GetLLVMConfigDir() {
00047   Path result;
00048 #ifdef LLVM_ETCDIR
00049   if (result.set(LLVM_ETCDIR))
00050     return result;
00051 #endif
00052   return GetLLVMDefaultConfigDir();
00053 }
00054 
00055 LLVMFileType
00056 sys::IdentifyFileType(const char *magic, unsigned length) {
00057   assert(magic && "Invalid magic number string");
00058   assert(length >=4 && "Invalid magic number length");
00059   switch ((unsigned char)magic[0]) {
00060     case 0xDE:  // 0x0B17C0DE = BC wraper
00061       if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
00062           magic[3] == (char)0x0B)
00063         return Bitcode_FileType;
00064       break;
00065     case 'B':
00066       if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
00067         return Bitcode_FileType;
00068       break;
00069     case '!':
00070       if (length >= 8)
00071         if (memcmp(magic,"!<arch>\n",8) == 0)
00072           return Archive_FileType;
00073       break;
00074       
00075     case '\177':
00076       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
00077         if (length >= 18 && magic[17] == 0)
00078           switch (magic[16]) {
00079             default: break;
00080             case 1: return ELF_Relocatable_FileType;
00081             case 2: return ELF_Executable_FileType;
00082             case 3: return ELF_SharedObject_FileType;
00083             case 4: return ELF_Core_FileType;
00084           }
00085       }
00086       break;
00087 
00088     case 0xCA:
00089       if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && 
00090           magic[3] == char(0xBE)) {
00091         // This is complicated by an overlap with Java class files. 
00092         // See the Mach-O section in /usr/share/file/magic for details.
00093         if (length >= 8 && magic[7] < 43) 
00094           // FIXME: Universal Binary of any type.
00095           return Mach_O_DynamicallyLinkedSharedLib_FileType;
00096       }
00097       break;
00098 
00099     case 0xFE:
00100     case 0xCE: {
00101       uint16_t type = 0;
00102       if (magic[0] == char(0xFE) && magic[1] == char(0xED) && 
00103           magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
00104         /* Native endian */
00105         if (length >= 16) type = magic[14] << 8 | magic[15];
00106       } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) && 
00107                  magic[2] == char(0xED) && magic[3] == char(0xFE)) {
00108         /* Reverse endian */
00109         if (length >= 14) type = magic[13] << 8 | magic[12];
00110       }
00111       switch (type) {
00112         default: break;      
00113         case 1: return Mach_O_Object_FileType; 
00114         case 2: return Mach_O_Executable_FileType;
00115         case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
00116         case 4: return Mach_O_Core_FileType;
00117         case 5: return Mach_O_PreloadExectuable_FileType;
00118         case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
00119         case 7: return Mach_O_DynamicLinker_FileType;
00120         case 8: return Mach_O_Bundle_FileType;
00121         case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
00122         case 10: break; // FIXME: MH_DSYM companion file with only debug.
00123       }
00124       break;
00125     }
00126     case 0xF0: // PowerPC Windows
00127     case 0x83: // Alpha 32-bit
00128     case 0x84: // Alpha 64-bit
00129     case 0x66: // MPS R4000 Windows
00130     case 0x50: // mc68K
00131     case 0x4c: // 80386 Windows
00132       if (magic[1] == 0x01)
00133         return COFF_FileType;
00134 
00135     case 0x90: // PA-RISC Windows
00136     case 0x68: // mc68K Windows
00137       if (magic[1] == 0x02)
00138         return COFF_FileType;
00139       break;
00140 
00141     default:
00142       break;
00143   }
00144   return Unknown_FileType;
00145 }
00146 
00147 bool
00148 Path::isArchive() const {
00149   if (canRead())
00150     return hasMagicNumber("!<arch>\012");
00151   return false;
00152 }
00153 
00154 bool
00155 Path::isDynamicLibrary() const {
00156   if (canRead()) {
00157     std::string Magic;
00158     if (getMagicNumber(Magic, 64))
00159       switch (IdentifyFileType(Magic.c_str(),
00160                                static_cast<unsigned>(Magic.length()))) {
00161         default: return false;
00162         case Mach_O_FixedVirtualMemorySharedLib_FileType:
00163         case Mach_O_DynamicallyLinkedSharedLib_FileType:
00164         case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
00165         case ELF_SharedObject_FileType:
00166         case COFF_FileType:  return true;
00167       }
00168   }
00169   return false;
00170 }
00171 
00172 Path
00173 Path::FindLibrary(std::string& name) {
00174   std::vector<sys::Path> LibPaths;
00175   GetSystemLibraryPaths(LibPaths);
00176   for (unsigned i = 0; i < LibPaths.size(); ++i) {
00177     sys::Path FullPath(LibPaths[i]);
00178     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
00179     if (FullPath.isDynamicLibrary())
00180       return FullPath;
00181     FullPath.eraseSuffix();
00182     FullPath.appendSuffix("a");
00183     if (FullPath.isArchive())
00184       return FullPath;
00185   }
00186   return sys::Path();
00187 }
00188 
00189 std::string Path::GetDLLSuffix() {
00190   return LTDL_SHLIB_EXT;
00191 }
00192 
00193 bool
00194 Path::isBitcodeFile() const {
00195   std::string actualMagic;
00196   if (!getMagicNumber(actualMagic, 4))
00197     return false;
00198   LLVMFileType FT =
00199     IdentifyFileType(actualMagic.c_str(),
00200                      static_cast<unsigned>(actualMagic.length()));
00201   return FT == Bitcode_FileType;
00202 }
00203 
00204 bool Path::hasMagicNumber(const std::string &Magic) const {
00205   std::string actualMagic;
00206   if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
00207     return Magic == actualMagic;
00208   return false;
00209 }
00210 
00211 static void getPathList(const char*path, std::vector<Path>& Paths) {
00212   const char* at = path;
00213   const char* delim = strchr(at, PathSeparator);
00214   Path tmpPath;
00215   while (delim != 0) {
00216     std::string tmp(at, size_t(delim-at));
00217     if (tmpPath.set(tmp))
00218       if (tmpPath.canRead())
00219         Paths.push_back(tmpPath);
00220     at = delim + 1;
00221     delim = strchr(at, PathSeparator);
00222   }
00223 
00224   if (*at != 0)
00225     if (tmpPath.set(std::string(at)))
00226       if (tmpPath.canRead())
00227         Paths.push_back(tmpPath);
00228 }
00229 
00230 static std::string getDirnameCharSep(const std::string& path, char Sep) {
00231   
00232   if (path.empty())
00233     return ".";
00234   
00235   // If the path is all slashes, return a single slash.
00236   // Otherwise, remove all trailing slashes.
00237   
00238   signed pos = static_cast<signed>(path.size()) - 1;
00239   
00240   while (pos >= 0 && path[pos] == Sep)
00241     --pos;
00242   
00243   if (pos < 0)
00244     return path[0] == Sep ? std::string(1, Sep) : std::string(".");
00245   
00246   // Any slashes left?
00247   signed i = 0;
00248   
00249   while (i < pos && path[i] != Sep)
00250     ++i;
00251   
00252   if (i == pos) // No slashes?  Return "."
00253     return ".";
00254   
00255   // There is at least one slash left.  Remove all trailing non-slashes.  
00256   while (pos >= 0 && path[pos] != Sep)
00257     --pos;
00258   
00259   // Remove any trailing slashes.
00260   while (pos >= 0 && path[pos] == Sep)
00261     --pos;
00262   
00263   if (pos < 0)
00264     return path[0] == Sep ? std::string(1, Sep) : std::string(".");
00265   
00266   return path.substr(0, pos+1);
00267 }
00268 
00269 // Include the truly platform-specific parts of this class.
00270 #if defined(LLVM_ON_UNIX)
00271 #include "Unix/Path.inc"
00272 #endif
00273 #if defined(LLVM_ON_WIN32)
00274 #include "Win32/Path.inc"
00275 #endif
00276