1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system Path concept.
12 //===----------------------------------------------------------------------===//
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"
25 using support::ulittle32_t;
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only TRULY operating system
30 //=== independent code.
31 //===----------------------------------------------------------------------===//
33 bool Path::operator==(const Path &that) const {
34 return path == that.path;
37 bool Path::operator<(const Path& that) const {
38 return path < that.path;
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;
52 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
53 return Bitcode_FileType;
57 if (memcmp(magic,"!<arch>\n",8) == 0)
58 return Archive_FileType;
62 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
63 if (length >= 18 && magic[17] == 0)
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;
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;
85 // The two magic numbers for mach-o are:
86 // 0xfeedface - 32-bit mach-o
87 // 0xfeedfacf - 64-bit mach-o
92 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
93 magic[2] == char(0xFA) &&
94 (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
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)) {
101 if (length >= 14) type = magic[13] << 8 | magic[12];
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;
118 case 0xF0: // PowerPC Windows
119 case 0x83: // Alpha 32-bit
120 case 0x84: // Alpha 64-bit
121 case 0x66: // MPS R4000 Windows
123 case 0x4c: // 80386 Windows
124 if (magic[1] == 0x01)
125 return COFF_FileType;
127 case 0x90: // PA-RISC Windows
128 case 0x68: // mc68K Windows
129 if (magic[1] == 0x02)
130 return COFF_FileType;
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;
142 case 0x64: // x86-64 Windows.
143 if (magic[1] == char(0x86))
144 return COFF_FileType;
150 return Unknown_FileType;
154 Path::isArchive() const {
156 if (fs::identify_magic(str(), type))
158 return type == fs::file_magic::archive;
162 Path::isDynamicLibrary() const {
164 if (fs::identify_magic(str(), 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;
177 Path::isObjectFile() const {
179 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
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())
193 FullPath.eraseSuffix();
194 FullPath.appendSuffix("a");
195 if (FullPath.isArchive())
201 StringRef Path::GetDLLSuffix() {
202 return &(LTDL_SHLIB_EXT[1]);
206 Path::appendSuffix(StringRef suffix) {
207 if (!suffix.empty()) {
214 Path::isBitcodeFile() const {
216 if (fs::identify_magic(str(), type))
218 return type == fs::file_magic::bitcode;
221 bool Path::hasMagicNumber(StringRef Magic) const {
222 std::string actualMagic;
223 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
224 return Magic == actualMagic;
228 static void getPathList(const char*path, std::vector<Path>& Paths) {
229 const char* at = path;
230 const char* delim = strchr(at, PathSeparator);
233 std::string tmp(at, size_t(delim-at));
234 if (tmpPath.set(tmp))
235 if (tmpPath.canRead())
236 Paths.push_back(tmpPath);
238 delim = strchr(at, PathSeparator);
242 if (tmpPath.set(std::string(at)))
243 if (tmpPath.canRead())
244 Paths.push_back(tmpPath);
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.");
253 // If the path is all slashes, return a single slash.
254 // Otherwise, remove all trailing slashes.
256 signed pos = static_cast<signed>(path.size()) - 1;
258 while (pos >= 0 && path[pos] == Sep[0])
262 return path[0] == Sep[0] ? Sep : ".";
267 while (i < pos && path[i] != Sep[0])
270 if (i == pos) // No slashes? Return "."
273 // There is at least one slash left. Remove all trailing non-slashes.
274 while (pos >= 0 && path[pos] != Sep[0])
277 // Remove any trailing slashes.
278 while (pos >= 0 && path[pos] == Sep[0])
282 return path[0] == Sep[0] ? Sep : ".";
284 return path.substr(0, pos+1);
287 // Include the truly platform-specific parts of this class.
288 #if defined(LLVM_ON_UNIX)
289 #include "Unix/Path.inc"
291 #if defined(LLVM_ON_WIN32)
292 #include "Windows/Path.inc"