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/Support/Path.h"
00015 #include "llvm/Config/config.h"
00016 #include "llvm/Support/Endian.h"
00017 #include "llvm/Support/FileSystem.h"
00018 #include <cassert>
00019 #include <cstring>
00020 #include <ostream>
00021 using namespace llvm;
00022 using namespace sys;
00023 namespace {
00024 using support::ulittle32_t;
00025 }
00026 
00027 //===----------------------------------------------------------------------===//
00028 //=== WARNING: Implementation here must contain only TRULY operating system
00029 //===          independent code.
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 LLVMFileType
00041 sys::IdentifyFileType(const char *magic, unsigned length) {
00042   assert(magic && "Invalid magic number string");
00043   assert(length >=4 && "Invalid magic number length");
00044   switch ((unsigned char)magic[0]) {
00045     case 0xDE:  // 0x0B17C0DE = BC wraper
00046       if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
00047           magic[3] == (char)0x0B)
00048         return Bitcode_FileType;
00049       break;
00050     case 'B':
00051       if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
00052         return Bitcode_FileType;
00053       break;
00054     case '!':
00055       if (length >= 8)
00056         if (memcmp(magic,"!<arch>\n",8) == 0)
00057           return Archive_FileType;
00058       break;
00059 
00060     case '\177':
00061       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
00062         bool Data2MSB = magic[5] == 2;
00063         unsigned high = Data2MSB ? 16 : 17;
00064         unsigned low  = Data2MSB ? 17 : 16;
00065         if (length >= 18 && magic[high] == 0)
00066           switch (magic[low]) {
00067             default: break;
00068             case 1: return ELF_Relocatable_FileType;
00069             case 2: return ELF_Executable_FileType;
00070             case 3: return ELF_SharedObject_FileType;
00071             case 4: return ELF_Core_FileType;
00072           }
00073       }
00074       break;
00075 
00076     case 0xCA:
00077       if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
00078           magic[3] == char(0xBE)) {
00079         // This is complicated by an overlap with Java class files.
00080         // See the Mach-O section in /usr/share/file/magic for details.
00081         if (length >= 8 && magic[7] < 43)
00082           // FIXME: Universal Binary of any type.
00083           return Mach_O_DynamicallyLinkedSharedLib_FileType;
00084       }
00085       break;
00086 
00087       // The two magic numbers for mach-o are:
00088       // 0xfeedface - 32-bit mach-o
00089       // 0xfeedfacf - 64-bit mach-o
00090     case 0xFE:
00091     case 0xCE:
00092     case 0xCF: {
00093       uint16_t type = 0;
00094       if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
00095           magic[2] == char(0xFA) &&
00096           (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
00097         /* Native endian */
00098         if (length >= 16) type = magic[14] << 8 | magic[15];
00099       } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
00100                  magic[1] == char(0xFA) && magic[2] == char(0xED) &&
00101                  magic[3] == char(0xFE)) {
00102         /* Reverse endian */
00103         if (length >= 14) type = magic[13] << 8 | magic[12];
00104       }
00105       switch (type) {
00106         default: break;
00107         case 1: return Mach_O_Object_FileType;
00108         case 2: return Mach_O_Executable_FileType;
00109         case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
00110         case 4: return Mach_O_Core_FileType;
00111         case 5: return Mach_O_PreloadExecutable_FileType;
00112         case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
00113         case 7: return Mach_O_DynamicLinker_FileType;
00114         case 8: return Mach_O_Bundle_FileType;
00115         case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
00116         case 10: return Mach_O_DSYMCompanion_FileType;
00117       }
00118       break;
00119     }
00120     case 0xF0: // PowerPC Windows
00121     case 0x83: // Alpha 32-bit
00122     case 0x84: // Alpha 64-bit
00123     case 0x66: // MPS R4000 Windows
00124     case 0x50: // mc68K
00125     case 0x4c: // 80386 Windows
00126       if (magic[1] == 0x01)
00127         return COFF_FileType;
00128 
00129     case 0x90: // PA-RISC Windows
00130     case 0x68: // mc68K Windows
00131       if (magic[1] == 0x02)
00132         return COFF_FileType;
00133       break;
00134 
00135     case 0x4d: // Possible MS-DOS stub on Windows PE file
00136       if (magic[1] == 0x5a) {
00137         uint32_t off = *reinterpret_cast<const ulittle32_t *>(magic + 0x3c);
00138         // PE/COFF file, either EXE or DLL.
00139         if (off < length && memcmp(magic + off, "PE\0\0",4) == 0)
00140           return COFF_FileType;
00141       }
00142       break;
00143 
00144     case 0x64: // x86-64 Windows.
00145       if (magic[1] == char(0x86))
00146         return COFF_FileType;
00147       break;
00148 
00149     default:
00150       break;
00151   }
00152   return Unknown_FileType;
00153 }
00154 
00155 bool
00156 Path::isArchive() const {
00157   fs::file_magic type;
00158   if (fs::identify_magic(str(), type))
00159     return false;
00160   return type == fs::file_magic::archive;
00161 }
00162 
00163 bool
00164 Path::isDynamicLibrary() const {
00165   fs::file_magic type;
00166   if (fs::identify_magic(str(), type))
00167     return false;
00168   switch (type) {
00169     default: return false;
00170     case fs::file_magic::macho_fixed_virtual_memory_shared_lib:
00171     case fs::file_magic::macho_dynamically_linked_shared_lib:
00172     case fs::file_magic::macho_dynamically_linked_shared_lib_stub:
00173     case fs::file_magic::elf_shared_object:
00174     case fs::file_magic::pecoff_executable:  return true;
00175   }
00176 }
00177 
00178 bool
00179 Path::isObjectFile() const {
00180   fs::file_magic type;
00181   if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
00182     return false;
00183   return true;
00184 }
00185 
00186 Path
00187 Path::FindLibrary(std::string& name) {
00188   std::vector<sys::Path> LibPaths;
00189   GetSystemLibraryPaths(LibPaths);
00190   for (unsigned i = 0; i < LibPaths.size(); ++i) {
00191     sys::Path FullPath(LibPaths[i]);
00192     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
00193     if (FullPath.isDynamicLibrary())
00194       return FullPath;
00195     FullPath.eraseSuffix();
00196     FullPath.appendSuffix("a");
00197     if (FullPath.isArchive())
00198       return FullPath;
00199   }
00200   return sys::Path();
00201 }
00202 
00203 StringRef Path::GetDLLSuffix() {
00204   return &(LTDL_SHLIB_EXT[1]);
00205 }
00206 
00207 void
00208 Path::appendSuffix(StringRef suffix) {
00209   if (!suffix.empty()) {
00210     path.append(".");
00211     path.append(suffix);
00212   }
00213 }
00214 
00215 bool
00216 Path::isBitcodeFile() const {
00217   fs::file_magic type;
00218   if (fs::identify_magic(str(), type))
00219     return false;
00220   return type == fs::file_magic::bitcode;
00221 }
00222 
00223 bool Path::hasMagicNumber(StringRef Magic) const {
00224   std::string actualMagic;
00225   if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
00226     return Magic == actualMagic;
00227   return false;
00228 }
00229 
00230 static void getPathList(const char*path, std::vector<Path>& Paths) {
00231   const char* at = path;
00232   const char* delim = strchr(at, PathSeparator);
00233   Path tmpPath;
00234   while (delim != 0) {
00235     std::string tmp(at, size_t(delim-at));
00236     if (tmpPath.set(tmp))
00237       if (tmpPath.canRead())
00238         Paths.push_back(tmpPath);
00239     at = delim + 1;
00240     delim = strchr(at, PathSeparator);
00241   }
00242 
00243   if (*at != 0)
00244     if (tmpPath.set(std::string(at)))
00245       if (tmpPath.canRead())
00246         Paths.push_back(tmpPath);
00247 }
00248 
00249 static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
00250   assert(Sep[0] != '\0' && Sep[1] == '\0' &&
00251          "Sep must be a 1-character string literal.");
00252   if (path.empty())
00253     return ".";
00254 
00255   // If the path is all slashes, return a single slash.
00256   // Otherwise, remove all trailing slashes.
00257 
00258   signed pos = static_cast<signed>(path.size()) - 1;
00259 
00260   while (pos >= 0 && path[pos] == Sep[0])
00261     --pos;
00262 
00263   if (pos < 0)
00264     return path[0] == Sep[0] ? Sep : ".";
00265 
00266   // Any slashes left?
00267   signed i = 0;
00268 
00269   while (i < pos && path[i] != Sep[0])
00270     ++i;
00271 
00272   if (i == pos) // No slashes?  Return "."
00273     return ".";
00274 
00275   // There is at least one slash left.  Remove all trailing non-slashes.
00276   while (pos >= 0 && path[pos] != Sep[0])
00277     --pos;
00278 
00279   // Remove any trailing slashes.
00280   while (pos >= 0 && path[pos] == Sep[0])
00281     --pos;
00282 
00283   if (pos < 0)
00284     return path[0] == Sep[0] ? Sep : ".";
00285 
00286   return path.substr(0, pos+1);
00287 }
00288 
00289 // Include the truly platform-specific parts of this class.
00290 #if defined(LLVM_ON_UNIX)
00291 #include "Unix/Path.inc"
00292 #endif
00293 #if defined(LLVM_ON_WIN32)
00294 #include "Windows/Path.inc"
00295 #endif