1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This header file implements the operating system Path concept. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Support/Path.h" 15 #include "llvm/Support/FileSystem.h" 16 #include "llvm/Config/config.h" 17 #include "llvm/Support/FileSystem.h" 18 #include "llvm/Support/Endian.h" 19 #include <cassert> 20 #include <cstring> 21 #include <ostream> 22 using namespace llvm; 23 using namespace sys; 24 namespace { 25 using support::ulittle32_t; 26 } 27 28 //===----------------------------------------------------------------------===// 29 //=== WARNING: Implementation here must contain only TRULY operating system 30 //=== independent code. 31 //===----------------------------------------------------------------------===// 32 33 bool Path::operator==(const Path &that) const { 34 return path == that.path; 35 } 36 37 bool Path::operator<(const Path& that) const { 38 return path < that.path; 39 } 40 41 LLVMFileType 42 sys::IdentifyFileType(const char *magic, unsigned length) { 43 assert(magic && "Invalid magic number string"); 44 assert(length >=4 && "Invalid magic number length"); 45 switch ((unsigned char)magic[0]) { 46 case 0xDE: // 0x0B17C0DE = BC wraper 47 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 && 48 magic[3] == (char)0x0B) 49 return Bitcode_FileType; 50 break; 51 case 'B': 52 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE) 53 return Bitcode_FileType; 54 break; 55 case '!': 56 if (length >= 8) 57 if (memcmp(magic,"!<arch>\n",8) == 0) 58 return Archive_FileType; 59 break; 60 61 case '\177': 62 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') { 63 if (length >= 18 && magic[17] == 0) 64 switch (magic[16]) { 65 default: break; 66 case 1: return ELF_Relocatable_FileType; 67 case 2: return ELF_Executable_FileType; 68 case 3: return ELF_SharedObject_FileType; 69 case 4: return ELF_Core_FileType; 70 } 71 } 72 break; 73 74 case 0xCA: 75 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && 76 magic[3] == char(0xBE)) { 77 // This is complicated by an overlap with Java class files. 78 // See the Mach-O section in /usr/share/file/magic for details. 79 if (length >= 8 && magic[7] < 43) 80 // FIXME: Universal Binary of any type. 81 return Mach_O_DynamicallyLinkedSharedLib_FileType; 82 } 83 break; 84 85 // The two magic numbers for mach-o are: 86 // 0xfeedface - 32-bit mach-o 87 // 0xfeedfacf - 64-bit mach-o 88 case 0xFE: 89 case 0xCE: 90 case 0xCF: { 91 uint16_t type = 0; 92 if (magic[0] == char(0xFE) && magic[1] == char(0xED) && 93 magic[2] == char(0xFA) && 94 (magic[3] == char(0xCE) || magic[3] == char(0xCF))) { 95 /* Native endian */ 96 if (length >= 16) type = magic[14] << 8 | magic[15]; 97 } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) && 98 magic[1] == char(0xFA) && magic[2] == char(0xED) && 99 magic[3] == char(0xFE)) { 100 /* Reverse endian */ 101 if (length >= 14) type = magic[13] << 8 | magic[12]; 102 } 103 switch (type) { 104 default: break; 105 case 1: return Mach_O_Object_FileType; 106 case 2: return Mach_O_Executable_FileType; 107 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType; 108 case 4: return Mach_O_Core_FileType; 109 case 5: return Mach_O_PreloadExecutable_FileType; 110 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType; 111 case 7: return Mach_O_DynamicLinker_FileType; 112 case 8: return Mach_O_Bundle_FileType; 113 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType; 114 case 10: return Mach_O_DSYMCompanion_FileType; 115 } 116 break; 117 } 118 case 0xF0: // PowerPC Windows 119 case 0x83: // Alpha 32-bit 120 case 0x84: // Alpha 64-bit 121 case 0x66: // MPS R4000 Windows 122 case 0x50: // mc68K 123 case 0x4c: // 80386 Windows 124 if (magic[1] == 0x01) 125 return COFF_FileType; 126 127 case 0x90: // PA-RISC Windows 128 case 0x68: // mc68K Windows 129 if (magic[1] == 0x02) 130 return COFF_FileType; 131 break; 132 133 case 0x4d: // Possible MS-DOS stub on Windows PE file 134 if (magic[1] == 0x5a) { 135 uint32_t off = *reinterpret_cast<const ulittle32_t *>(magic + 0x3c); 136 // PE/COFF file, either EXE or DLL. 137 if (off < length && memcmp(magic + off, "PE\0\0",4) == 0) 138 return COFF_FileType; 139 } 140 break; 141 142 case 0x64: // x86-64 Windows. 143 if (magic[1] == char(0x86)) 144 return COFF_FileType; 145 break; 146 147 default: 148 break; 149 } 150 return Unknown_FileType; 151 } 152 153 bool 154 Path::isArchive() const { 155 fs::file_magic type; 156 if (fs::identify_magic(str(), type)) 157 return false; 158 return type == fs::file_magic::archive; 159 } 160 161 bool 162 Path::isDynamicLibrary() const { 163 fs::file_magic type; 164 if (fs::identify_magic(str(), type)) 165 return false; 166 switch (type) { 167 default: return false; 168 case fs::file_magic::macho_fixed_virtual_memory_shared_lib: 169 case fs::file_magic::macho_dynamically_linked_shared_lib: 170 case fs::file_magic::macho_dynamically_linked_shared_lib_stub: 171 case fs::file_magic::elf_shared_object: 172 case fs::file_magic::pecoff_executable: return true; 173 } 174 } 175 176 bool 177 Path::isObjectFile() const { 178 fs::file_magic type; 179 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown) 180 return false; 181 return true; 182 } 183 184 Path 185 Path::FindLibrary(std::string& name) { 186 std::vector<sys::Path> LibPaths; 187 GetSystemLibraryPaths(LibPaths); 188 for (unsigned i = 0; i < LibPaths.size(); ++i) { 189 sys::Path FullPath(LibPaths[i]); 190 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT); 191 if (FullPath.isDynamicLibrary()) 192 return FullPath; 193 FullPath.eraseSuffix(); 194 FullPath.appendSuffix("a"); 195 if (FullPath.isArchive()) 196 return FullPath; 197 } 198 return sys::Path(); 199 } 200 201 StringRef Path::GetDLLSuffix() { 202 return &(LTDL_SHLIB_EXT[1]); 203 } 204 205 void 206 Path::appendSuffix(StringRef suffix) { 207 if (!suffix.empty()) { 208 path.append("."); 209 path.append(suffix); 210 } 211 } 212 213 bool 214 Path::isBitcodeFile() const { 215 fs::file_magic type; 216 if (fs::identify_magic(str(), type)) 217 return false; 218 return type == fs::file_magic::bitcode; 219 } 220 221 bool Path::hasMagicNumber(StringRef Magic) const { 222 std::string actualMagic; 223 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size()))) 224 return Magic == actualMagic; 225 return false; 226 } 227 228 static void getPathList(const char*path, std::vector<Path>& Paths) { 229 const char* at = path; 230 const char* delim = strchr(at, PathSeparator); 231 Path tmpPath; 232 while (delim != 0) { 233 std::string tmp(at, size_t(delim-at)); 234 if (tmpPath.set(tmp)) 235 if (tmpPath.canRead()) 236 Paths.push_back(tmpPath); 237 at = delim + 1; 238 delim = strchr(at, PathSeparator); 239 } 240 241 if (*at != 0) 242 if (tmpPath.set(std::string(at))) 243 if (tmpPath.canRead()) 244 Paths.push_back(tmpPath); 245 } 246 247 static StringRef getDirnameCharSep(StringRef path, const char *Sep) { 248 assert(Sep[0] != '\0' && Sep[1] == '\0' && 249 "Sep must be a 1-character string literal."); 250 if (path.empty()) 251 return "."; 252 253 // If the path is all slashes, return a single slash. 254 // Otherwise, remove all trailing slashes. 255 256 signed pos = static_cast<signed>(path.size()) - 1; 257 258 while (pos >= 0 && path[pos] == Sep[0]) 259 --pos; 260 261 if (pos < 0) 262 return path[0] == Sep[0] ? Sep : "."; 263 264 // Any slashes left? 265 signed i = 0; 266 267 while (i < pos && path[i] != Sep[0]) 268 ++i; 269 270 if (i == pos) // No slashes? Return "." 271 return "."; 272 273 // There is at least one slash left. Remove all trailing non-slashes. 274 while (pos >= 0 && path[pos] != Sep[0]) 275 --pos; 276 277 // Remove any trailing slashes. 278 while (pos >= 0 && path[pos] == Sep[0]) 279 --pos; 280 281 if (pos < 0) 282 return path[0] == Sep[0] ? Sep : "."; 283 284 return path.substr(0, pos+1); 285 } 286 287 // Include the truly platform-specific parts of this class. 288 #if defined(LLVM_ON_UNIX) 289 #include "Unix/Path.inc" 290 #endif 291 #if defined(LLVM_ON_WIN32) 292 #include "Windows/Path.inc" 293 #endif 294