blob: a4d49dc494096bc6db8fc38f63b222e9a7df120f [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
Misha Brukmanf976c852005-04-21 22:55:34 +000041LLVMFileType
Chris Lattner6fa6a322008-07-09 05:14:23 +000042sys::IdentifyFileType(const char *magic, unsigned length) {
Reid Spencerf37ce992004-11-14 22:05:32 +000043 assert(magic && "Invalid magic number string");
44 assert(length >=4 && "Invalid magic number length");
Torok Edwin4cdc44c2009-04-25 10:25:12 +000045 switch ((unsigned char)magic[0]) {
Chris Lattner6fa6a322008-07-09 05:14:23 +000046 case 0xDE: // 0x0B17C0DE = BC wraper
47 if (magic[1] == (char)0xC0 && magic[2] == (char)0x17 &&
48 magic[3] == (char)0x0B)
49 return Bitcode_FileType;
50 break;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000051 case 'B':
52 if (magic[1] == 'C' && magic[2] == (char)0xC0 && magic[3] == (char)0xDE)
53 return Bitcode_FileType;
54 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000055 case '!':
56 if (length >= 8)
57 if (memcmp(magic,"!<arch>\n",8) == 0)
58 return Archive_FileType;
59 break;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000060
Reid Spencer8bb5fd12007-04-04 06:30:26 +000061 case '\177':
Chris Lattner24eac6c2007-05-03 18:15:56 +000062 if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F') {
Reid Spencer947aa7d2007-04-11 02:02:09 +000063 if (length >= 18 && magic[17] == 0)
64 switch (magic[16]) {
65 default: break;
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;
70 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000071 }
Reid Spencerf37ce992004-11-14 22:05:32 +000072 break;
73
Chris Lattnerade75922007-04-11 03:15:35 +000074 case 0xCA:
Michael J. Spencer4a295d32010-08-31 06:36:46 +000075 if (magic[1] == char(0xFE) && magic[2] == char(0xBA) &&
Chris Lattnerade75922007-04-11 03:15:35 +000076 magic[3] == char(0xBE)) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +000077 // This is complicated by an overlap with Java class files.
Chris Lattner65215492008-06-26 05:17:18 +000078 // See the Mach-O section in /usr/share/file/magic for details.
Michael J. Spencer4a295d32010-08-31 06:36:46 +000079 if (length >= 8 && magic[7] < 43)
Chris Lattner65215492008-06-26 05:17:18 +000080 // FIXME: Universal Binary of any type.
81 return Mach_O_DynamicallyLinkedSharedLib_FileType;
82 }
83 break;
84
Eric Christopherf0c3af62011-04-22 03:50:19 +000085 // The two magic numbers for mach-o are:
86 // 0xfeedface - 32-bit mach-o
87 // 0xfeedfacf - 64-bit mach-o
Chris Lattner65215492008-06-26 05:17:18 +000088 case 0xFE:
Eric Christopherf0c3af62011-04-22 03:50:19 +000089 case 0xCE:
90 case 0xCF: {
Bill Wendlingfc1fd542008-06-26 08:32:05 +000091 uint16_t type = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000092 if (magic[0] == char(0xFE) && magic[1] == char(0xED) &&
Eric Christopherf0c3af62011-04-22 03:50:19 +000093 magic[2] == char(0xFA) &&
94 (magic[3] == char(0xCE) || magic[3] == char(0xCF))) {
Chris Lattner65215492008-06-26 05:17:18 +000095 /* Native endian */
96 if (length >= 16) type = magic[14] << 8 | magic[15];
Eric Christopherf0c3af62011-04-22 03:50:19 +000097 } else if ((magic[0] == char(0xCE) || magic[0] == char(0xCF)) &&
98 magic[1] == char(0xFA) && magic[2] == char(0xED) &&
99 magic[3] == char(0xFE)) {
Chris Lattner65215492008-06-26 05:17:18 +0000100 /* Reverse endian */
101 if (length >= 14) type = magic[13] << 8 | magic[12];
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000102 }
Chris Lattner65215492008-06-26 05:17:18 +0000103 switch (type) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000104 default: break;
105 case 1: return Mach_O_Object_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000106 case 2: return Mach_O_Executable_FileType;
107 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
108 case 4: return Mach_O_Core_FileType;
Peter Collingbourneb1a33c42010-11-17 00:43:43 +0000109 case 5: return Mach_O_PreloadExecutable_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000110 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;
Benjamin Kramer83662342011-09-14 00:39:22 +0000114 case 10: return Mach_O_DSYMCompanion_FileType;
Chris Lattnerade75922007-04-11 03:15:35 +0000115 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000116 break;
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000117 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000118 case 0xF0: // PowerPC Windows
119 case 0x83: // Alpha 32-bit
120 case 0x84: // Alpha 64-bit
121 case 0x66: // MPS R4000 Windows
122 case 0x50: // mc68K
123 case 0x4c: // 80386 Windows
124 if (magic[1] == 0x01)
125 return COFF_FileType;
126
127 case 0x90: // PA-RISC Windows
128 case 0x68: // mc68K Windows
129 if (magic[1] == 0x02)
130 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000131 break;
Eric Christopher539d8d82011-04-03 22:53:19 +0000132
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;
139 }
140 break;
141
Michael J. Spencer7e7d01d2010-09-15 23:04:14 +0000142 case 0x64: // x86-64 Windows.
143 if (magic[1] == char(0x86))
144 return COFF_FileType;
145 break;
Reid Spencerf37ce992004-11-14 22:05:32 +0000146
147 default:
148 break;
149 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000150 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000151}
152
Reid Spencerccb23a12004-12-13 03:00:39 +0000153bool
154Path::isArchive() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000155 LLVMFileType type;
156 if (fs::identify_magic(str(), type))
157 return false;
158 return type == Archive_FileType;
Reid Spencerccb23a12004-12-13 03:00:39 +0000159}
160
161bool
162Path::isDynamicLibrary() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000163 LLVMFileType type;
164 if (fs::identify_magic(str(), type))
165 return false;
166 switch (type) {
167 default: return false;
168 case Mach_O_FixedVirtualMemorySharedLib_FileType:
169 case Mach_O_DynamicallyLinkedSharedLib_FileType:
170 case Mach_O_DynamicallyLinkedSharedLibStub_FileType:
171 case ELF_SharedObject_FileType:
172 case COFF_FileType: return true;
173 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000174}
175
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000176bool
177Path::isObjectFile() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000178 LLVMFileType type;
179 if (fs::identify_magic(str(), type) || type == Unknown_FileType)
180 return false;
181 return true;
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000182}
183
Reid Spencerccb23a12004-12-13 03:00:39 +0000184Path
185Path::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]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000190 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000191 if (FullPath.isDynamicLibrary())
192 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000193 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000194 FullPath.appendSuffix("a");
195 if (FullPath.isArchive())
196 return FullPath;
197 }
198 return sys::Path();
199}
200
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000201StringRef Path::GetDLLSuffix() {
Mikhail Glushenkovc8aef4b2010-11-02 20:32:59 +0000202 return &(LTDL_SHLIB_EXT[1]);
Reid Spencer79fc9242004-12-13 18:41:28 +0000203}
204
Dan Gohman552a3c22010-12-01 02:46:41 +0000205void
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000206Path::appendSuffix(StringRef suffix) {
207 if (!suffix.empty()) {
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000208 path.append(".");
209 path.append(suffix);
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000210 }
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000211}
212
213bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000214Path::isBitcodeFile() const {
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000215 LLVMFileType type;
216 if (fs::identify_magic(str(), type))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000217 return false;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000218 return type == Bitcode_FileType;
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000219}
220
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000221bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000222 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000223 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000224 return Magic == actualMagic;
225 return false;
226}
227
Chris Lattnere1b332a2008-02-27 06:17:10 +0000228static void getPathList(const char*path, std::vector<Path>& Paths) {
229 const char* at = path;
230 const char* delim = strchr(at, PathSeparator);
231 Path tmpPath;
232 while (delim != 0) {
233 std::string tmp(at, size_t(delim-at));
234 if (tmpPath.set(tmp))
235 if (tmpPath.canRead())
236 Paths.push_back(tmpPath);
237 at = delim + 1;
238 delim = strchr(at, PathSeparator);
239 }
240
241 if (*at != 0)
242 if (tmpPath.set(std::string(at)))
243 if (tmpPath.canRead())
244 Paths.push_back(tmpPath);
245}
246
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000247static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
248 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
249 "Sep must be a 1-character string literal.");
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000250 if (path.empty())
251 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000252
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000253 // If the path is all slashes, return a single slash.
254 // Otherwise, remove all trailing slashes.
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000255
Evan Cheng34cd4a42008-05-05 18:30:58 +0000256 signed pos = static_cast<signed>(path.size()) - 1;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000257
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000258 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000259 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000260
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000261 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000262 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000263
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000264 // Any slashes left?
265 signed i = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000266
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000267 while (i < pos && path[i] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000268 ++i;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000269
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000270 if (i == pos) // No slashes? Return "."
271 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000272
273 // There is at least one slash left. Remove all trailing non-slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000274 while (pos >= 0 && path[pos] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000275 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000276
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000277 // Remove any trailing slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000278 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000279 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000280
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000281 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000282 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000283
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000284 return path.substr(0, pos+1);
285}
286
Reid Spencerb89a2232004-08-25 06:20:07 +0000287// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000288#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000289#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000290#endif
291#if defined(LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000292#include "Windows/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000293#endif