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