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/Config/config.h"
16 #include "llvm/Support/Endian.h"
17 #include "llvm/Support/FileSystem.h"
24 using support::ulittle32_t;
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only TRULY operating system
29 //=== independent code.
30 //===----------------------------------------------------------------------===//
32 bool Path::operator==(const Path &that) const {
33 return path == that.path;
36 bool Path::operator<(const Path& that) const {
37 return path < that.path;
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;
51 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
52 return Bitcode_FileType;
56 if (memcmp(Magic,"!<arch>\n",8) == 0)
57 return Archive_FileType;
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)
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;
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;
87 // The two magic numbers for mach-o are:
88 // 0xfeedface - 32-bit mach-o
89 // 0xfeedfacf - 64-bit mach-o
94 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
95 Magic[2] == char(0xFA) &&
96 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
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)) {
103 if (Length >= 14) type = Magic[13] << 8 | Magic[12];
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;
120 case 0xF0: // PowerPC Windows
121 case 0x83: // Alpha 32-bit
122 case 0x84: // Alpha 64-bit
123 case 0x66: // MPS R4000 Windows
125 case 0x4c: // 80386 Windows
126 if (Magic[1] == 0x01)
127 return COFF_FileType;
129 case 0x90: // PA-RISC Windows
130 case 0x68: // mc68K Windows
131 if (Magic[1] == 0x02)
132 return COFF_FileType;
135 case 0x4d: // Possible MS-DOS stub on Windows PE file
136 if (Magic[1] == 0x5a) {
138 *reinterpret_cast<const ulittle32_t *>(Magic + 0x3c);
139 // PE/COFF file, either EXE or DLL.
140 if (off < Length && memcmp(Magic + off, "PE\0\0",4) == 0)
141 return COFF_FileType;
145 case 0x64: // x86-64 Windows.
146 if (Magic[1] == char(0x86))
147 return COFF_FileType;
153 return Unknown_FileType;
157 Path::isArchive() const {
159 if (fs::identify_magic(str(), type))
161 return type == fs::file_magic::archive;
165 Path::isDynamicLibrary() const {
167 if (fs::identify_magic(str(), type))
170 default: return false;
171 case fs::file_magic::macho_fixed_virtual_memory_shared_lib:
172 case fs::file_magic::macho_dynamically_linked_shared_lib:
173 case fs::file_magic::macho_dynamically_linked_shared_lib_stub:
174 case fs::file_magic::elf_shared_object:
175 case fs::file_magic::pecoff_executable: return true;
180 Path::isObjectFile() const {
182 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
188 Path::FindLibrary(std::string& name) {
189 std::vector<sys::Path> LibPaths;
190 GetSystemLibraryPaths(LibPaths);
191 for (unsigned i = 0; i < LibPaths.size(); ++i) {
192 sys::Path FullPath(LibPaths[i]);
193 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
194 if (FullPath.isDynamicLibrary())
196 FullPath.eraseSuffix();
197 FullPath.appendSuffix("a");
198 if (FullPath.isArchive())
204 StringRef Path::GetDLLSuffix() {
205 return &(LTDL_SHLIB_EXT[1]);
209 Path::appendSuffix(StringRef suffix) {
210 if (!suffix.empty()) {
217 Path::isBitcodeFile() const {
219 if (fs::identify_magic(str(), type))
221 return type == fs::file_magic::bitcode;
224 bool Path::hasMagicNumber(StringRef Magic) const {
225 std::string actualMagic;
226 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
227 return Magic == actualMagic;
231 static void getPathList(const char*path, std::vector<Path>& Paths) {
232 const char* at = path;
233 const char* delim = strchr(at, PathSeparator);
236 std::string tmp(at, size_t(delim-at));
237 if (tmpPath.set(tmp))
238 if (tmpPath.canRead())
239 Paths.push_back(tmpPath);
241 delim = strchr(at, PathSeparator);
245 if (tmpPath.set(std::string(at)))
246 if (tmpPath.canRead())
247 Paths.push_back(tmpPath);
250 static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
251 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
252 "Sep must be a 1-character string literal.");
256 // If the path is all slashes, return a single slash.
257 // Otherwise, remove all trailing slashes.
259 signed pos = static_cast<signed>(path.size()) - 1;
261 while (pos >= 0 && path[pos] == Sep[0])
265 return path[0] == Sep[0] ? Sep : ".";
270 while (i < pos && path[i] != Sep[0])
273 if (i == pos) // No slashes? Return "."
276 // There is at least one slash left. Remove all trailing non-slashes.
277 while (pos >= 0 && path[pos] != Sep[0])
280 // Remove any trailing slashes.
281 while (pos >= 0 && path[pos] == Sep[0])
285 return path[0] == Sep[0] ? Sep : ".";
287 return path.substr(0, pos+1);
290 // Include the truly platform-specific parts of this class.
291 #if defined(LLVM_ON_UNIX)
292 #include "Unix/Path.inc"
294 #if defined(LLVM_ON_WIN32)
295 #include "Windows/Path.inc"