Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 1 | //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system Path concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 13 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 14 | #include "llvm/System/Path.h" |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Alkis Evlogimenos | 98bc8ed | 2004-11-14 22:37:42 +0000 | [diff] [blame] | 16 | #include <cassert> |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 17 | #include <ostream> |
| 18 | using namespace llvm; |
Reid Spencer | 8e66595 | 2004-08-29 05:24:01 +0000 | [diff] [blame] | 19 | using namespace sys; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 23 | //=== independent code. |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 26 | std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) { |
| 27 | strm << aPath.toString(); |
| 28 | return strm; |
| 29 | } |
| 30 | |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 31 | Path |
| 32 | Path::GetLLVMConfigDir() { |
| 33 | Path result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 34 | #ifdef LLVM_ETCDIR |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 35 | if (result.set(LLVM_ETCDIR)) |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 36 | return result; |
Jeff Cohen | ab68df0 | 2004-12-15 04:08:15 +0000 | [diff] [blame] | 37 | #endif |
Reid Spencer | c29befb | 2004-12-15 01:50:13 +0000 | [diff] [blame] | 38 | return GetLLVMDefaultConfigDir(); |
| 39 | } |
| 40 | |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 41 | LLVMFileType |
Reid Spencer | a2a6221 | 2004-11-14 23:26:18 +0000 | [diff] [blame] | 42 | sys::IdentifyFileType(const char*magic, unsigned length) { |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 43 | assert(magic && "Invalid magic number string"); |
| 44 | assert(length >=4 && "Invalid magic number length"); |
| 45 | switch (magic[0]) { |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 46 | case 'B': |
| 47 | if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE) |
| 48 | return Bitcode_FileType; |
| 49 | break; |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 50 | case '!': |
| 51 | if (length >= 8) |
| 52 | if (memcmp(magic,"!<arch>\n",8) == 0) |
| 53 | return Archive_FileType; |
| 54 | break; |
| 55 | |
| 56 | case '\177': |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 57 | if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') { |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 58 | if (length >= 18 && magic[17] == 0) |
| 59 | switch (magic[16]) { |
| 60 | default: break; |
| 61 | case 1: return ELF_Relocatable_FileType; |
| 62 | case 2: return ELF_Executable_FileType; |
| 63 | case 3: return ELF_SharedObject_FileType; |
| 64 | case 4: return ELF_Core_FileType; |
| 65 | } |
Chris Lattner | 24eac6c | 2007-05-03 18:15:56 +0000 | [diff] [blame] | 66 | } |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 67 | break; |
| 68 | |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 69 | case 0xCA: |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 70 | // This is complicated by an overlap with Java class files. |
| 71 | // See the Mach-O section in /usr/share/file/magic for details. |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 72 | if (magic[1] == char(0xFE) && magic[2] == char(0xBA) && |
| 73 | magic[3] == char(0xBE)) { |
| 74 | return Mach_O_DynamicallyLinkedSharedLib_FileType; |
| 75 | |
| 76 | // FIXME: How does this work? |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 77 | if (length >= 14 && magic[13] == 0) |
| 78 | switch (magic[12]) { |
| 79 | default: break; |
| 80 | case 1: return Mach_O_Object_FileType; |
| 81 | case 2: return Mach_O_Executable_FileType; |
| 82 | case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType; |
| 83 | case 4: return Mach_O_Core_FileType; |
| 84 | case 5: return Mach_O_PreloadExectuable_FileType; |
| 85 | case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType; |
| 86 | case 7: return Mach_O_DynamicLinker_FileType; |
| 87 | case 8: return Mach_O_Bundle_FileType; |
| 88 | case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType; |
| 89 | } |
Chris Lattner | ade7592 | 2007-04-11 03:15:35 +0000 | [diff] [blame] | 90 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 91 | break; |
| 92 | |
| 93 | case 0xF0: // PowerPC Windows |
| 94 | case 0x83: // Alpha 32-bit |
| 95 | case 0x84: // Alpha 64-bit |
| 96 | case 0x66: // MPS R4000 Windows |
| 97 | case 0x50: // mc68K |
| 98 | case 0x4c: // 80386 Windows |
| 99 | if (magic[1] == 0x01) |
| 100 | return COFF_FileType; |
| 101 | |
| 102 | case 0x90: // PA-RISC Windows |
| 103 | case 0x68: // mc68K Windows |
| 104 | if (magic[1] == 0x02) |
| 105 | return COFF_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 106 | break; |
| 107 | |
| 108 | default: |
| 109 | break; |
| 110 | } |
Reid Spencer | 8bb5fd1 | 2007-04-04 06:30:26 +0000 | [diff] [blame] | 111 | return Unknown_FileType; |
Reid Spencer | f37ce99 | 2004-11-14 22:05:32 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 114 | bool |
| 115 | Path::isArchive() const { |
Reid Spencer | c7f0832 | 2005-07-07 18:21:42 +0000 | [diff] [blame] | 116 | if (canRead()) |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 117 | return hasMagicNumber("!<arch>\012"); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | bool |
| 122 | Path::isDynamicLibrary() const { |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 123 | if (canRead()) { |
| 124 | std::string Magic; |
| 125 | if (getMagicNumber(Magic, 64)) |
| 126 | switch (IdentifyFileType(Magic.c_str(), Magic.length())) { |
| 127 | default: return false; |
Reid Spencer | 947aa7d | 2007-04-11 02:02:09 +0000 | [diff] [blame] | 128 | case Mach_O_FixedVirtualMemorySharedLib_FileType: |
| 129 | case Mach_O_DynamicallyLinkedSharedLib_FileType: |
| 130 | case Mach_O_DynamicallyLinkedSharedLibStub_FileType: |
| 131 | case ELF_SharedObject_FileType: |
Reid Spencer | 410aa02 | 2007-04-11 00:49:39 +0000 | [diff] [blame] | 132 | case COFF_FileType: return true; |
| 133 | } |
| 134 | } |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | Path |
| 139 | Path::FindLibrary(std::string& name) { |
| 140 | std::vector<sys::Path> LibPaths; |
| 141 | GetSystemLibraryPaths(LibPaths); |
| 142 | for (unsigned i = 0; i < LibPaths.size(); ++i) { |
| 143 | sys::Path FullPath(LibPaths[i]); |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 144 | FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 145 | if (FullPath.isDynamicLibrary()) |
| 146 | return FullPath; |
Reid Spencer | dd04df0 | 2005-07-07 23:21:43 +0000 | [diff] [blame] | 147 | FullPath.eraseSuffix(); |
Reid Spencer | ccb23a1 | 2004-12-13 03:00:39 +0000 | [diff] [blame] | 148 | FullPath.appendSuffix("a"); |
| 149 | if (FullPath.isArchive()) |
| 150 | return FullPath; |
| 151 | } |
| 152 | return sys::Path(); |
| 153 | } |
| 154 | |
Chris Lattner | c67dc45 | 2006-07-07 18:11:32 +0000 | [diff] [blame] | 155 | std::string Path::GetDLLSuffix() { |
Reid Spencer | 79fc924 | 2004-12-13 18:41:28 +0000 | [diff] [blame] | 156 | return LTDL_SHLIB_EXT; |
| 157 | } |
| 158 | |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 159 | bool |
Chris Lattner | f283a5e | 2007-05-06 05:32:21 +0000 | [diff] [blame] | 160 | Path::isBitcodeFile() const { |
| 161 | std::string actualMagic; |
| 162 | if (!getMagicNumber(actualMagic, 4)) |
| 163 | return false; |
| 164 | return actualMagic == "BC\xC0\xDE"; |
| 165 | } |
| 166 | |
| 167 | bool Path::hasMagicNumber(const std::string &Magic) const { |
| 168 | std::string actualMagic; |
| 169 | if (getMagicNumber(actualMagic, Magic.size())) |
| 170 | return Magic == actualMagic; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 176 | // Include the truly platform-specific parts of this class. |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 177 | #if defined(LLVM_ON_UNIX) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 178 | #include "Unix/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 179 | #endif |
| 180 | #if defined(LLVM_ON_WIN32) |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 181 | #include "Win32/Path.inc" |
Reid Spencer | dafe55f | 2004-12-24 06:29:17 +0000 | [diff] [blame] | 182 | #endif |
Reid Spencer | 23dd332 | 2006-07-26 16:55:39 +0000 | [diff] [blame] | 183 | |