blob: 8fbaf2d42bf9f39c2c5994ff40f903e24cb0c78a [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencerb89a2232004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencerb89a2232004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system Path concept.
11//
12//===----------------------------------------------------------------------===//
Reid Spencer8e665952004-08-29 05:24:01 +000013
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000014#include "llvm/Support/Path.h"
Michael J. Spencer28f0ed52011-01-15 20:39:36 +000015#include "llvm/Support/FileSystem.h"
Reid Spencer79fc9242004-12-13 18:41:28 +000016#include "llvm/Config/config.h"
Michael J. Spencer54453f22011-01-10 02:34:23 +000017#include "llvm/Support/FileSystem.h"
Eric Christopher539d8d82011-04-03 22:53:19 +000018#include "llvm/Support/Endian.h"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000019#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000020#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000021#include <ostream>
22using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000023using namespace sys;
Eric Christopher539d8d82011-04-03 22:53:19 +000024namespace {
25using support::ulittle32_t;
26}
Reid Spencerb89a2232004-08-25 06:20:07 +000027
28//===----------------------------------------------------------------------===//
29//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000030//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000031//===----------------------------------------------------------------------===//
32
Bill Wendling40db5d42008-05-21 21:20:07 +000033bool Path::operator==(const Path &that) const {
34 return path == that.path;
35}
36
Bill Wendling40db5d42008-05-21 21:20:07 +000037bool Path::operator<(const Path& that) const {
38 return path < that.path;
39}
40
Reid Spencerc29befb2004-12-15 01:50:13 +000041Path
42Path::GetLLVMConfigDir() {
43 Path result;
Jeff Cohenab68df02004-12-15 04:08:15 +000044#ifdef LLVM_ETCDIR
Reid Spencerdd04df02005-07-07 23:21:43 +000045 if (result.set(LLVM_ETCDIR))
Reid Spencerc29befb2004-12-15 01:50:13 +000046 return result;
Jeff Cohenab68df02004-12-15 04:08:15 +000047#endif
Reid Spencerc29befb2004-12-15 01:50:13 +000048 return GetLLVMDefaultConfigDir();
49}
50
Misha Brukmanf976c852005-04-21 22:55:34 +000051LLVMFileType
Chris Lattner6fa6a322008-07-09 05:14:23 +000052sys::IdentifyFileType(const char *magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000053 assert(magic && "Invalid magic number string");
54 assert(length >=4 && "Invalid magic number length");
Torok Edwin4cdc44c2009-04-25 10:25:12 +000055 switch ((unsigned char)magic[0]) {
Chris Lattner6fa6a322008-07-09 05:14:23 +000056 case 0xDE: // 0x0B17C0DE = BC wraper
57 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
58 magic[3] == (char)0x0B)
59 return Bitcode_FileType;
60 break;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000061 case 'B':
62 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
63 return Bitcode_FileType;
64 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000065 case '!':
66 if (length >= 8)
67 if (memcmp(magic,"!<arch>\n",8) == 0)
68 return Archive_FileType;
69 break;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000070
Reid Spencer8bb5fd12007-04-04 06:30:26 +000071 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000072 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000073 if (length >= 18 && magic[17] == 0)
74 switch (magic[16]) {
75 default: break;
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;
80 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000081 }
Reid Spencerf37ce992004-11-14 22:05:32 +000082 break;
83
Chris Lattnerade75922007-04-11 03:15:35 +000084 case 0xCA:
Michael J. Spencer4a295d32010-08-31 06:36:46 +000085 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
Chris Lattnerade75922007-04-11 03:15:35 +000086 magic[3] == char(0xBE)) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +000087 // This is complicated by an overlap with Java class files.
Chris Lattner65215492008-06-26 05:17:18 +000088 // See the Mach-O section in /usr/share/file/magic for details.
Michael J. Spencer4a295d32010-08-31 06:36:46 +000089 if (length >= 8 && magic[7] < 43)
Chris Lattner65215492008-06-26 05:17:18 +000090 // FIXME: Universal Binary of any type.
91 return Mach_O_DynamicallyLinkedSharedLib_FileType;
92 }
93 break;
94
Eric Christopherf0c3af62011-04-22 03:50:19 +000095 // The two magic numbers for mach-o are:
96 // 0xfeedface - 32-bit mach-o
97 // 0xfeedfacf - 64-bit mach-o
Chris Lattner65215492008-06-26 05:17:18 +000098 case 0xFE:
Eric Christopherf0c3af62011-04-22 03:50:19 +000099 case 0xCE:
100 case 0xCF: {
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000101 uint16_t type = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000102 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
Eric Christopherf0c3af62011-04-22 03:50:19 +0000103 magic[2] == char(0xFA) &&
104 (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
Chris Lattner65215492008-06-26 05:17:18 +0000105 /* Native endian */
106 if (length >= 16) type = magic[14] << 8 | magic[15];
Eric Christopherf0c3af62011-04-22 03:50:19 +0000107 } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
108 magic[1] == char(0xFA) && magic[2] == char(0xED) &&
109 magic[3] == char(0xFE)) {
Chris Lattner65215492008-06-26 05:17:18 +0000110 /* Reverse endian */
111 if (length >= 14) type = magic[13] << 8 | magic[12];
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000112 }
Chris Lattner65215492008-06-26 05:17:18 +0000113 switch (type) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000114 default: break;
115 case 1: return Mach_O_Object_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000116 case 2: return Mach_O_Executable_FileType;
117 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
118 case 4: return Mach_O_Core_FileType;
Peter Collingbourneb1a33c42010-11-17 00:43:43 +0000119 case 5: return Mach_O_PreloadExecutable_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000120 case 6: return Mach_O_DynamicallyLinkedSharedLib_FileType;
121 case 7: return Mach_O_DynamicLinker_FileType;
122 case 8: return Mach_O_Bundle_FileType;
123 case 9: return Mach_O_DynamicallyLinkedSharedLibStub_FileType;
124 case 10: break; // FIXME: MH_DSYM companion file with only debug.
Chris Lattnerade75922007-04-11 03:15:35 +0000125 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000126 break;
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000127 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000128 case 0xF0: // PowerPC Windows
129 case 0x83: // Alpha 32-bit
130 case 0x84: // Alpha 64-bit
131 case 0x66: // MPS R4000 Windows
132 case 0x50: // mc68K
133 case 0x4c: // 80386 Windows
134 if (magic[1] == 0x01)
135 return COFF_FileType;
136
137 case 0x90: // PA-RISC Windows
138 case 0x68: // mc68K Windows
139 if (magic[1] == 0x02)
140 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000141 break;
Eric Christopher539d8d82011-04-03 22:53:19 +0000142
143 case 0x4d: // Possible MS-DOS stub on Windows PE file
144 if (magic[1] == 0x5a) {
145 uint32_t off = *reinterpret_cast<const ulittle32_t *>(magic + 0x3c);
146 // PE/COFF file, either EXE or DLL.
147 if (off < length && memcmp(magic + off, "PE\0\0",4) == 0)
148 return COFF_FileType;
149 }
150 break;
151
Michael J. Spencer7e7d01d2010-09-15 23:04:14 +0000152 case 0x64: // x86-64 Windows.
153 if (magic[1] == char(0x86))
154 return COFF_FileType;
155 break;
Reid Spencerf37ce992004-11-14 22:05:32 +0000156
157 default:
158 break;
159 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000160 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000161}
162
Reid Spencerccb23a12004-12-13 03:00:39 +0000163bool
164Path::isArchive() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000165 LLVMFileType type;
166 if (fs::identify_magic(str(), type))
167 return false;
168 return type == Archive_FileType;
Reid Spencerccb23a12004-12-13 03:00:39 +0000169}
170
171bool
172Path::isDynamicLibrary() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000173 LLVMFileType type;
174 if (fs::identify_magic(str(), type))
175 return false;
176 switch (type) {
177 default: return false;
178 case Mach_O_FixedVirtualMemorySharedLib_FileType:
179 case Mach_O_DynamicallyLinkedSharedLib_FileType:
180 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
181 case ELF_SharedObject_FileType:
182 case COFF_FileType: return true;
183 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000184}
185
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000186bool
187Path::isObjectFile() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000188 LLVMFileType type;
189 if (fs::identify_magic(str(), type) || type == Unknown_FileType)
190 return false;
191 return true;
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000192}
193
Reid Spencerccb23a12004-12-13 03:00:39 +0000194Path
195Path::FindLibrary(std::string& name) {
196 std::vector<sys::Path> LibPaths;
197 GetSystemLibraryPaths(LibPaths);
198 for (unsigned i = 0; i < LibPaths.size(); ++i) {
199 sys::Path FullPath(LibPaths[i]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000200 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000201 if (FullPath.isDynamicLibrary())
202 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000203 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000204 FullPath.appendSuffix("a");
205 if (FullPath.isArchive())
206 return FullPath;
207 }
208 return sys::Path();
209}
210
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000211StringRef Path::GetDLLSuffix() {
Mikhail Glushenkovc8aef4b2010-11-02 20:32:59 +0000212 return &(LTDL_SHLIB_EXT[1]);
Reid Spencer79fc9242004-12-13 18:41:28 +0000213}
214
Dan Gohman552a3c22010-12-01 02:46:41 +0000215void
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000216Path::appendSuffix(StringRef suffix) {
217 if (!suffix.empty()) {
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000218 path.append(".");
219 path.append(suffix);
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000220 }
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000221}
222
223bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000224Path::isBitcodeFile() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000225 LLVMFileType type;
226 if (fs::identify_magic(str(), type))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000227 return false;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000228 return type == Bitcode_FileType;
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000229}
230
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000231bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000232 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000233 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000234 return Magic == actualMagic;
235 return false;
236}
237
Chris Lattnere1b332a2008-02-27 06:17:10 +0000238static void getPathList(const char*path, std::vector<Path>& Paths) {
239 const char* at = path;
240 const char* delim = strchr(at, PathSeparator);
241 Path tmpPath;
242 while (delim != 0) {
243 std::string tmp(at, size_t(delim-at));
244 if (tmpPath.set(tmp))
245 if (tmpPath.canRead())
246 Paths.push_back(tmpPath);
247 at = delim + 1;
248 delim = strchr(at, PathSeparator);
249 }
250
251 if (*at != 0)
252 if (tmpPath.set(std::string(at)))
253 if (tmpPath.canRead())
254 Paths.push_back(tmpPath);
255}
256
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000257static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
258 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
259 "Sep must be a 1-character string literal.");
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000260 if (path.empty())
261 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000262
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000263 // If the path is all slashes, return a single slash.
264 // Otherwise, remove all trailing slashes.
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000265
Evan Cheng34cd4a42008-05-05 18:30:58 +0000266 signed pos = static_cast<signed>(path.size()) - 1;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000267
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000268 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000269 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000270
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000271 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000272 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000273
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000274 // Any slashes left?
275 signed i = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000276
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000277 while (i < pos && path[i] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000278 ++i;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000279
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000280 if (i == pos) // No slashes? Return "."
281 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000282
283 // There is at least one slash left. Remove all trailing non-slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000284 while (pos >= 0 && path[pos] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000285 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000286
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000287 // Remove any trailing slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000288 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000289 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000290
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000291 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000292 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000293
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000294 return path.substr(0, pos+1);
295}
296
Reid Spencerb89a2232004-08-25 06:20:07 +0000297// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000298#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000299#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000300#endif
301#if defined(LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000302#include "Windows/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000303#endif