Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system Path concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/System/Path.h" |
| 15 | #include "llvm/Config/config.h" |
Daniel Dunbar | bf8e871 | 2009-07-12 20:23:56 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include <cassert> |
Duncan Sands | fca2014 | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 18 | #include <cstring> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include <ostream> |
| 20 | using namespace llvm; |
| 21 | using namespace sys; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 25 | //=== independent code. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Bill Wendling | 0df675d | 2008-05-21 21:20:07 +0000 | [diff] [blame] | 28 | bool Path::operator==(const Path &that) const { |
| 29 | return path == that.path; |
| 30 | } |
| 31 | |
Bill Wendling | 0df675d | 2008-05-21 21:20:07 +0000 | [diff] [blame] | 32 | bool Path::operator<(const Path& that) const { |
| 33 | return path < that.path; |
| 34 | } |
| 35 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | Path |
| 37 | Path::GetLLVMConfigDir() { |
| 38 | Path result; |
| 39 | #ifdef LLVM_ETCDIR |
| 40 | if (result.set(LLVM_ETCDIR)) |
| 41 | return result; |
| 42 | #endif |
| 43 | return GetLLVMDefaultConfigDir(); |
| 44 | } |
| 45 | |
| 46 | LLVMFileType |
Chris Lattner | 65b13ff | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 47 | sys::IdentifyFileType(const char *magic, unsigned length) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | assert(magic && "Invalid magic number string"); |
| 49 | assert(length >=4 && "Invalid magic number length"); |
Edwin Török | b2d71d7 | 2009-04-25 10:25:12 +0000 | [diff] [blame] | 50 | switch ((unsigned char)magic[0]) { |
Chris Lattner | 65b13ff | 2008-07-09 05:14:23 +0000 | [diff] [blame] | 51 | case 0xDE: // 0x0B17C0DE = BC wraper |
| 52 | if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 && |
| 53 | magic[3] == (char)0x0B) |
| 54 | return Bitcode_FileType; |
| 55 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | case 'B': |
| 57 | if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE) |
| 58 | return Bitcode_FileType; |
| 59 | break; |
| 60 | case '!': |
| 61 | if (length >= 8) |
| 62 | if (memcmp(magic,"!<arch>\n",8) == 0) |
| 63 | return Archive_FileType; |
| 64 | break; |
| 65 | |
| 66 | case '\177': |
| 67 | if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') { |
| 68 | if (length >= 18 && magic[17] == 0) |
| 69 | switch (magic[16]) { |
| 70 | default: break; |
| 71 | case 1: return ELF_Relocatable_FileType; |
| 72 | case 2: return ELF_Executable_FileType; |
| 73 | case 3: return ELF_SharedObject_FileType; |
| 74 | case 4: return ELF_Core_FileType; |
| 75 | } |
| 76 | } |
| 77 | break; |
| 78 | |
| 79 | case 0xCA: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 80 | if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && |
| 81 | magic[3] == char(0xBE)) { |
Chris Lattner | 59ca909 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 82 | // This is complicated by an overlap with Java class files. |
| 83 | // See the Mach-O section in /usr/share/file/magic for details. |
| 84 | if (length >= 8 && magic[7] < 43) |
| 85 | // FIXME: Universal Binary of any type. |
| 86 | return Mach_O_DynamicallyLinkedSharedLib_FileType; |
| 87 | } |
| 88 | break; |
| 89 | |
| 90 | case 0xFE: |
Bill Wendling | e5a33b2 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 91 | case 0xCE: { |
| 92 | uint16_t type = 0; |
Chris Lattner | 59ca909 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 93 | if (magic[0] == char(0xFE) && magic[1] == char(0xED) && |
| 94 | magic[2] == char(0xFA) && magic[3] == char(0xCE)) { |
| 95 | /* Native endian */ |
| 96 | if (length >= 16) type = magic[14] << 8 | magic[15]; |
| 97 | } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) && |
| 98 | magic[2] == char(0xED) && magic[3] == char(0xFE)) { |
| 99 | /* Reverse endian */ |
| 100 | if (length >= 14) type = magic[13] << 8 | magic[12]; |
Bill Wendling | e5a33b2 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | 59ca909 | 2008-06-26 05:17:18 +0000 | [diff] [blame] | 102 | switch (type) { |
| 103 | default: break; |
| 104 | case 1: return Mach_O_Object_FileType; |
| 105 | case 2: return Mach_O_Executable_FileType; |
| 106 | case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType; |
| 107 | case 4: return Mach_O_Core_FileType; |
| 108 | case 5: return Mach_O_PreloadExectuable_FileType; |
| 109 | case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType; |
| 110 | case 7: return Mach_O_DynamicLinker_FileType; |
| 111 | case 8: return Mach_O_Bundle_FileType; |
| 112 | case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType; |
| 113 | case 10: break; // FIXME: MH_DSYM companion file with only debug. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 114 | } |
| 115 | break; |
Bill Wendling | e5a33b2 | 2008-06-26 08:32:05 +0000 | [diff] [blame] | 116 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 117 | case 0xF0: // PowerPC Windows |
| 118 | case 0x83: // Alpha 32-bit |
| 119 | case 0x84: // Alpha 64-bit |
| 120 | case 0x66: // MPS R4000 Windows |
| 121 | case 0x50: // mc68K |
| 122 | case 0x4c: // 80386 Windows |
| 123 | if (magic[1] == 0x01) |
| 124 | return COFF_FileType; |
| 125 | |
| 126 | case 0x90: // PA-RISC Windows |
| 127 | case 0x68: // mc68K Windows |
| 128 | if (magic[1] == 0x02) |
| 129 | return COFF_FileType; |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | return Unknown_FileType; |
| 136 | } |
| 137 | |
| 138 | bool |
| 139 | Path::isArchive() const { |
| 140 | if (canRead()) |
| 141 | return hasMagicNumber("!<arch>\012"); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | bool |
| 146 | Path::isDynamicLibrary() const { |
| 147 | if (canRead()) { |
| 148 | std::string Magic; |
| 149 | if (getMagicNumber(Magic, 64)) |
Evan Cheng | 591bfc8 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 150 | switch (IdentifyFileType(Magic.c_str(), |
| 151 | static_cast<unsigned>(Magic.length()))) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | default: return false; |
| 153 | case Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 154 | case Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 155 | case Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 156 | case ELF_SharedObject_FileType: |
| 157 | case COFF_FileType: return true; |
| 158 | } |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | Path |
| 164 | Path::FindLibrary(std::string& name) { |
| 165 | std::vector<sys::Path> LibPaths; |
| 166 | GetSystemLibraryPaths(LibPaths); |
| 167 | for (unsigned i = 0; i < LibPaths.size(); ++i) { |
| 168 | sys::Path FullPath(LibPaths[i]); |
| 169 | FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT); |
| 170 | if (FullPath.isDynamicLibrary()) |
| 171 | return FullPath; |
| 172 | FullPath.eraseSuffix(); |
| 173 | FullPath.appendSuffix("a"); |
| 174 | if (FullPath.isArchive()) |
| 175 | return FullPath; |
| 176 | } |
| 177 | return sys::Path(); |
| 178 | } |
| 179 | |
| 180 | std::string Path::GetDLLSuffix() { |
| 181 | return LTDL_SHLIB_EXT; |
| 182 | } |
| 183 | |
| 184 | bool |
| 185 | Path::isBitcodeFile() const { |
| 186 | std::string actualMagic; |
| 187 | if (!getMagicNumber(actualMagic, 4)) |
| 188 | return false; |
Devang Patel | 86a79b9 | 2008-07-22 18:00:36 +0000 | [diff] [blame] | 189 | LLVMFileType FT = |
| 190 | IdentifyFileType(actualMagic.c_str(), |
| 191 | static_cast<unsigned>(actualMagic.length())); |
| 192 | return FT == Bitcode_FileType; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool Path::hasMagicNumber(const std::string &Magic) const { |
| 196 | std::string actualMagic; |
Evan Cheng | 591bfc8 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 197 | if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size()))) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 198 | return Magic == actualMagic; |
| 199 | return false; |
| 200 | } |
| 201 | |
Chris Lattner | 4b8f1c6 | 2008-02-27 06:17:10 +0000 | [diff] [blame] | 202 | static void getPathList(const char*path, std::vector<Path>& Paths) { |
| 203 | const char* at = path; |
| 204 | const char* delim = strchr(at, PathSeparator); |
| 205 | Path tmpPath; |
| 206 | while (delim != 0) { |
| 207 | std::string tmp(at, size_t(delim-at)); |
| 208 | if (tmpPath.set(tmp)) |
| 209 | if (tmpPath.canRead()) |
| 210 | Paths.push_back(tmpPath); |
| 211 | at = delim + 1; |
| 212 | delim = strchr(at, PathSeparator); |
| 213 | } |
| 214 | |
| 215 | if (*at != 0) |
| 216 | if (tmpPath.set(std::string(at))) |
| 217 | if (tmpPath.canRead()) |
| 218 | Paths.push_back(tmpPath); |
| 219 | } |
| 220 | |
Ted Kremenek | b169dfa | 2008-04-07 22:01:32 +0000 | [diff] [blame] | 221 | static std::string getDirnameCharSep(const std::string& path, char Sep) { |
Ted Kremenek | 4a4f5ed | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 222 | |
| 223 | if (path.empty()) |
| 224 | return "."; |
| 225 | |
| 226 | // If the path is all slashes, return a single slash. |
| 227 | // Otherwise, remove all trailing slashes. |
| 228 | |
Evan Cheng | 591bfc8 | 2008-05-05 18:30:58 +0000 | [diff] [blame] | 229 | signed pos = static_cast<signed>(path.size()) - 1; |
Ted Kremenek | 4a4f5ed | 2008-04-07 21:53:57 +0000 | [diff] [blame] | 230 | |
| 231 | while (pos >= 0 && path[pos] == Sep) |
| 232 | --pos; |
| 233 | |
| 234 | if (pos < 0) |
| 235 | return path[0] == Sep ? std::string(1, Sep) : std::string("."); |
| 236 | |
| 237 | // Any slashes left? |
| 238 | signed i = 0; |
| 239 | |
| 240 | while (i < pos && path[i] != Sep) |
| 241 | ++i; |
| 242 | |
| 243 | if (i == pos) // No slashes? Return "." |
| 244 | return "."; |
| 245 | |
| 246 | // There is at least one slash left. Remove all trailing non-slashes. |
| 247 | while (pos >= 0 && path[pos] != Sep) |
| 248 | --pos; |
| 249 | |
| 250 | // Remove any trailing slashes. |
| 251 | while (pos >= 0 && path[pos] == Sep) |
| 252 | --pos; |
| 253 | |
| 254 | if (pos < 0) |
| 255 | return path[0] == Sep ? std::string(1, Sep) : std::string("."); |
| 256 | |
| 257 | return path.substr(0, pos+1); |
| 258 | } |
| 259 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | // Include the truly platform-specific parts of this class. |
| 261 | #if defined(LLVM_ON_UNIX) |
| 262 | #include "Unix/Path.inc" |
| 263 | #endif |
| 264 | #if defined(LLVM_ON_WIN32) |
| 265 | #include "Win32/Path.inc" |
| 266 | #endif |
| 267 | |