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 Path::GetLLVMConfigDir() {
45 if (result.set(LLVM_ETCDIR))
48 return GetLLVMDefaultConfigDir();
52 sys::IdentifyFileType(const char *magic, unsigned length) {
53 assert(magic && "Invalid magic number string");
54 assert(length >=4 && "Invalid magic number length");
55 switch ((unsigned char)magic[0]) {
56 case 0xDE: // 0x0B17C0DE = BC wraper
57 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
58 magic[3] == (char)0x0B)
59 return Bitcode_FileType;
62 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
63 return Bitcode_FileType;
67 if (memcmp(magic,"!<arch>\n",8) == 0)
68 return Archive_FileType;
72 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
73 if (length >= 18 && magic[17] == 0)
76 case 1: return ELF_Relocatable_FileType;
77 case 2: return ELF_Executable_FileType;
78 case 3: return ELF_SharedObject_FileType;
79 case 4: return ELF_Core_FileType;
85 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
86 magic[3] == char(0xBE)) {
87 // This is complicated by an overlap with Java class files.
88 // See the Mach-O section in /usr/share/file/magic for details.
89 if (length >= 8 && magic[7] < 43)
90 // FIXME: Universal Binary of any type.
91 return Mach_O_DynamicallyLinkedSharedLib_FileType;
98 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
99 magic[2] == char(0xFA) && magic[3] == char(0xCE)) {
101 if (length >= 16) type = magic[14] << 8 | magic[15];
102 } else if (magic[0] == char(0xCE) && magic[1] == char(0xFA) &&
103 magic[2] == char(0xED) && magic[3] == char(0xFE)) {
105 if (length >= 14) type = magic[13] << 8 | magic[12];
109 case 1: return Mach_O_Object_FileType;
110 case 2: return Mach_O_Executable_FileType;
111 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
112 case 4: return Mach_O_Core_FileType;
113 case 5: return Mach_O_PreloadExecutable_FileType;
114 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
115 case 7: return Mach_O_DynamicLinker_FileType;
116 case 8: return Mach_O_Bundle_FileType;
117 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
118 case 10: break; // FIXME: MH_DSYM companion file with only debug.
122 case 0xF0: // PowerPC Windows
123 case 0x83: // Alpha 32-bit
124 case 0x84: // Alpha 64-bit
125 case 0x66: // MPS R4000 Windows
127 case 0x4c: // 80386 Windows
128 if (magic[1] == 0x01)
129 return COFF_FileType;
131 case 0x90: // PA-RISC Windows
132 case 0x68: // mc68K Windows
133 if (magic[1] == 0x02)
134 return COFF_FileType;
137 case 0x4d: // Possible MS-DOS stub on Windows PE file
138 if (magic[1] == 0x5a) {
139 uint32_t off = *reinterpret_cast<const ulittle32_t *>(magic + 0x3c);
140 // PE/COFF file, either EXE or DLL.
141 if (off < length && memcmp(magic + off, "PE\0\0",4) == 0)
142 return COFF_FileType;
146 case 0x64: // x86-64 Windows.
147 if (magic[1] == char(0x86))
148 return COFF_FileType;
154 return Unknown_FileType;
158 Path::isArchive() const {
160 if (fs::identify_magic(str(), type))
162 return type == Archive_FileType;
166 Path::isDynamicLibrary() const {
168 if (fs::identify_magic(str(), type))
171 default: return false;
172 case Mach_O_FixedVirtualMemorySharedLib_FileType:
173 case Mach_O_DynamicallyLinkedSharedLib_FileType:
174 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
175 case ELF_SharedObject_FileType:
176 case COFF_FileType: return true;
181 Path::isObjectFile() const {
183 if (fs::identify_magic(str(), type) || type == Unknown_FileType)
189 Path::FindLibrary(std::string& name) {
190 std::vector<sys::Path> LibPaths;
191 GetSystemLibraryPaths(LibPaths);
192 for (unsigned i = 0; i < LibPaths.size(); ++i) {
193 sys::Path FullPath(LibPaths[i]);
194 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
195 if (FullPath.isDynamicLibrary())
197 FullPath.eraseSuffix();
198 FullPath.appendSuffix("a");
199 if (FullPath.isArchive())
205 StringRef Path::GetDLLSuffix() {
206 return &(LTDL_SHLIB_EXT[1]);
210 Path::appendSuffix(StringRef suffix) {
211 if (!suffix.empty()) {
218 Path::isBitcodeFile() const {
220 if (fs::identify_magic(str(), type))
222 return type == Bitcode_FileType;
225 bool Path::hasMagicNumber(StringRef Magic) const {
226 std::string actualMagic;
227 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
228 return Magic == actualMagic;
232 static void getPathList(const char*path, std::vector<Path>& Paths) {
233 const char* at = path;
234 const char* delim = strchr(at, PathSeparator);
237 std::string tmp(at, size_t(delim-at));
238 if (tmpPath.set(tmp))
239 if (tmpPath.canRead())
240 Paths.push_back(tmpPath);
242 delim = strchr(at, PathSeparator);
246 if (tmpPath.set(std::string(at)))
247 if (tmpPath.canRead())
248 Paths.push_back(tmpPath);
251 static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
252 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
253 "Sep must be a 1-character string literal.");
257 // If the path is all slashes, return a single slash.
258 // Otherwise, remove all trailing slashes.
260 signed pos = static_cast<signed>(path.size()) - 1;
262 while (pos >= 0 && path[pos] == Sep[0])
266 return path[0] == Sep[0] ? Sep : ".";
271 while (i < pos && path[i] != Sep[0])
274 if (i == pos) // No slashes? Return "."
277 // There is at least one slash left. Remove all trailing non-slashes.
278 while (pos >= 0 && path[pos] != Sep[0])
281 // Remove any trailing slashes.
282 while (pos >= 0 && path[pos] == Sep[0])
286 return path[0] == Sep[0] ? Sep : ".";
288 return path.substr(0, pos+1);
291 // Include the truly platform-specific parts of this class.
292 #if defined(LLVM_ON_UNIX)
293 #include "Unix/Path.inc"
295 #if defined(LLVM_ON_WIN32)
296 #include "Windows/Path.inc"