blob: 5b34c5eab6843ce0c8ec3a1d68e0e738393e48c9 [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"
Reid Spencer79fc9242004-12-13 18:41:28 +000015#include "llvm/Config/config.h"
Eric Christopher539d8d82011-04-03 22:53:19 +000016#include "llvm/Support/Endian.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/Support/FileSystem.h"
Alkis Evlogimenos98bc8ed2004-11-14 22:37:42 +000018#include <cassert>
Duncan Sandsf52e32a2008-01-09 19:42:09 +000019#include <cstring>
Chris Lattnerc67dc452006-07-07 18:11:32 +000020#include <ostream>
21using namespace llvm;
Reid Spencer8e665952004-08-29 05:24:01 +000022using namespace sys;
Eric Christopher539d8d82011-04-03 22:53:19 +000023namespace {
24using support::ulittle32_t;
25}
Reid Spencerb89a2232004-08-25 06:20:07 +000026
27//===----------------------------------------------------------------------===//
28//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukmanf976c852005-04-21 22:55:34 +000029//=== independent code.
Reid Spencerb89a2232004-08-25 06:20:07 +000030//===----------------------------------------------------------------------===//
31
Bill Wendling40db5d42008-05-21 21:20:07 +000032bool Path::operator==(const Path &that) const {
33 return path == that.path;
34}
35
Bill Wendling40db5d42008-05-21 21:20:07 +000036bool Path::operator<(const Path& that) const {
37 return path < that.path;
38}
39
Misha Brukmanf976c852005-04-21 22:55:34 +000040LLVMFileType
Rafael Espindola91de80a2013-06-10 14:56:16 +000041sys::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]) {
Chris Lattner6fa6a322008-07-09 05:14:23 +000045 case 0xDE: // 0x0B17C0DE = BC wraper
Rafael Espindola91de80a2013-06-10 14:56:16 +000046 if (Magic[1] == (char)0xC0 && Magic[2] == (char)0x17 &&
47 Magic[3] == (char)0x0B)
Chris Lattner6fa6a322008-07-09 05:14:23 +000048 return Bitcode_FileType;
49 break;
Chris Lattnerf283a5e2007-05-06 05:32:21 +000050 case 'B':
Rafael Espindola91de80a2013-06-10 14:56:16 +000051 if (Magic[1] == 'C' && Magic[2] == (char)0xC0 && Magic[3] == (char)0xDE)
Chris Lattnerf283a5e2007-05-06 05:32:21 +000052 return Bitcode_FileType;
53 break;
Reid Spencer8bb5fd12007-04-04 06:30:26 +000054 case '!':
Rafael Espindola91de80a2013-06-10 14:56:16 +000055 if (Length >= 8)
56 if (memcmp(Magic,"!<arch>\n",8) == 0)
Reid Spencer8bb5fd12007-04-04 06:30:26 +000057 return Archive_FileType;
58 break;
Michael J. Spencer4a295d32010-08-31 06:36:46 +000059
Reid Spencer8bb5fd12007-04-04 06:30:26 +000060 case '\177':
Rafael Espindola91de80a2013-06-10 14:56:16 +000061 if (Magic[1] == 'E' && Magic[2] == 'L' && Magic[3] == 'F') {
62 bool Data2MSB = Magic[5] == 2;
Meador Ingeb935cd12012-06-25 14:48:43 +000063 unsigned high = Data2MSB ? 16 : 17;
64 unsigned low = Data2MSB ? 17 : 16;
Rafael Espindola91de80a2013-06-10 14:56:16 +000065 if (Length >= 18 && Magic[high] == 0)
66 switch (Magic[low]) {
Reid Spencer947aa7d2007-04-11 02:02:09 +000067 default: break;
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;
72 }
Chris Lattner24eac6c2007-05-03 18:15:56 +000073 }
Reid Spencerf37ce992004-11-14 22:05:32 +000074 break;
75
Chris Lattnerade75922007-04-11 03:15:35 +000076 case 0xCA:
Rafael Espindola91de80a2013-06-10 14:56:16 +000077 if (Magic[1] == char(0xFE) && Magic[2] == char(0xBA) &&
78 Magic[3] == char(0xBE)) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +000079 // This is complicated by an overlap with Java class files.
Chris Lattner65215492008-06-26 05:17:18 +000080 // See the Mach-O section in /usr/share/file/magic for details.
Rafael Espindola91de80a2013-06-10 14:56:16 +000081 if (Length >= 8 && Magic[7] < 43)
Chris Lattner65215492008-06-26 05:17:18 +000082 // FIXME: Universal Binary of any type.
83 return Mach_O_DynamicallyLinkedSharedLib_FileType;
84 }
85 break;
86
Eric Christopherf0c3af62011-04-22 03:50:19 +000087 // The two magic numbers for mach-o are:
88 // 0xfeedface - 32-bit mach-o
89 // 0xfeedfacf - 64-bit mach-o
Chris Lattner65215492008-06-26 05:17:18 +000090 case 0xFE:
Eric Christopherf0c3af62011-04-22 03:50:19 +000091 case 0xCE:
92 case 0xCF: {
Bill Wendlingfc1fd542008-06-26 08:32:05 +000093 uint16_t type = 0;
Rafael Espindola91de80a2013-06-10 14:56:16 +000094 if (Magic[0] == char(0xFE) && Magic[1] == char(0xED) &&
95 Magic[2] == char(0xFA) &&
96 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
Chris Lattner65215492008-06-26 05:17:18 +000097 /* Native endian */
Rafael Espindola91de80a2013-06-10 14:56:16 +000098 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)) {
Chris Lattner65215492008-06-26 05:17:18 +0000102 /* Reverse endian */
Rafael Espindola91de80a2013-06-10 14:56:16 +0000103 if (Length >= 14) type = Magic[13] << 8 | Magic[12];
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000104 }
Chris Lattner65215492008-06-26 05:17:18 +0000105 switch (type) {
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000106 default: break;
107 case 1: return Mach_O_Object_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000108 case 2: return Mach_O_Executable_FileType;
109 case 3: return Mach_O_FixedVirtualMemorySharedLib_FileType;
110 case 4: return Mach_O_Core_FileType;
Peter Collingbourneb1a33c42010-11-17 00:43:43 +0000111 case 5: return Mach_O_PreloadExecutable_FileType;
Chris Lattner65215492008-06-26 05:17:18 +0000112 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;
Benjamin Kramer83662342011-09-14 00:39:22 +0000116 case 10: return Mach_O_DSYMCompanion_FileType;
Chris Lattnerade75922007-04-11 03:15:35 +0000117 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000118 break;
Bill Wendlingfc1fd542008-06-26 08:32:05 +0000119 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000120 case 0xF0: // PowerPC Windows
121 case 0x83: // Alpha 32-bit
122 case 0x84: // Alpha 64-bit
123 case 0x66: // MPS R4000 Windows
124 case 0x50: // mc68K
125 case 0x4c: // 80386 Windows
Rafael Espindola91de80a2013-06-10 14:56:16 +0000126 if (Magic[1] == 0x01)
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000127 return COFF_FileType;
128
129 case 0x90: // PA-RISC Windows
130 case 0x68: // mc68K Windows
Rafael Espindola91de80a2013-06-10 14:56:16 +0000131 if (Magic[1] == 0x02)
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000132 return COFF_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000133 break;
Eric Christopher539d8d82011-04-03 22:53:19 +0000134
135 case 0x4d: // Possible MS-DOS stub on Windows PE file
Rafael Espindola91de80a2013-06-10 14:56:16 +0000136 if (Magic[1] == 0x5a) {
137 uint32_t off =
138 *reinterpret_cast<const ulittle32_t *>(Magic + 0x3c);
Eric Christopher539d8d82011-04-03 22:53:19 +0000139 // PE/COFF file, either EXE or DLL.
Rafael Espindola91de80a2013-06-10 14:56:16 +0000140 if (off < Length && memcmp(Magic + off, "PE\0\0",4) == 0)
Eric Christopher539d8d82011-04-03 22:53:19 +0000141 return COFF_FileType;
142 }
143 break;
144
Michael J. Spencer7e7d01d2010-09-15 23:04:14 +0000145 case 0x64: // x86-64 Windows.
Rafael Espindola91de80a2013-06-10 14:56:16 +0000146 if (Magic[1] == char(0x86))
Michael J. Spencer7e7d01d2010-09-15 23:04:14 +0000147 return COFF_FileType;
148 break;
Reid Spencerf37ce992004-11-14 22:05:32 +0000149
150 default:
151 break;
152 }
Reid Spencer8bb5fd12007-04-04 06:30:26 +0000153 return Unknown_FileType;
Reid Spencerf37ce992004-11-14 22:05:32 +0000154}
155
Reid Spencerccb23a12004-12-13 03:00:39 +0000156bool
157Path::isArchive() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000158 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000159 if (fs::identify_magic(str(), type))
160 return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000161 return type == fs::file_magic::archive;
Reid Spencerccb23a12004-12-13 03:00:39 +0000162}
163
164bool
165Path::isDynamicLibrary() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000166 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000167 if (fs::identify_magic(str(), type))
168 return false;
169 switch (type) {
170 default: return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000171 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;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000176 }
Reid Spencerccb23a12004-12-13 03:00:39 +0000177}
178
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000179bool
180Path::isObjectFile() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000181 fs::file_magic type;
182 if (fs::identify_magic(str(), type) || type == fs::file_magic::unknown)
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000183 return false;
184 return true;
Michael J. Spencer8a26f812010-09-15 22:45:45 +0000185}
186
Reid Spencerccb23a12004-12-13 03:00:39 +0000187Path
188Path::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]);
Reid Spencerdd04df02005-07-07 23:21:43 +0000193 FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
Reid Spencerccb23a12004-12-13 03:00:39 +0000194 if (FullPath.isDynamicLibrary())
195 return FullPath;
Reid Spencerdd04df02005-07-07 23:21:43 +0000196 FullPath.eraseSuffix();
Reid Spencerccb23a12004-12-13 03:00:39 +0000197 FullPath.appendSuffix("a");
198 if (FullPath.isArchive())
199 return FullPath;
200 }
201 return sys::Path();
202}
203
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000204StringRef Path::GetDLLSuffix() {
Mikhail Glushenkovc8aef4b2010-11-02 20:32:59 +0000205 return &(LTDL_SHLIB_EXT[1]);
Reid Spencer79fc9242004-12-13 18:41:28 +0000206}
207
Dan Gohman552a3c22010-12-01 02:46:41 +0000208void
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000209Path::appendSuffix(StringRef suffix) {
210 if (!suffix.empty()) {
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000211 path.append(".");
212 path.append(suffix);
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000213 }
Mikhail Glushenkovbd6e0322010-11-02 22:18:37 +0000214}
215
216bool
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000217Path::isBitcodeFile() const {
Michael J. Spencer5b082302011-12-13 23:17:12 +0000218 fs::file_magic type;
Michael J. Spencer28f0ed52011-01-15 20:39:36 +0000219 if (fs::identify_magic(str(), type))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000220 return false;
Michael J. Spencer5b082302011-12-13 23:17:12 +0000221 return type == fs::file_magic::bitcode;
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000222}
223
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000224bool Path::hasMagicNumber(StringRef Magic) const {
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000225 std::string actualMagic;
Evan Cheng34cd4a42008-05-05 18:30:58 +0000226 if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
Chris Lattnerf283a5e2007-05-06 05:32:21 +0000227 return Magic == actualMagic;
228 return false;
229}
230
Chris Lattnere1b332a2008-02-27 06:17:10 +0000231static void getPathList(const char*path, std::vector<Path>& Paths) {
232 const char* at = path;
233 const char* delim = strchr(at, PathSeparator);
234 Path tmpPath;
235 while (delim != 0) {
236 std::string tmp(at, size_t(delim-at));
237 if (tmpPath.set(tmp))
238 if (tmpPath.canRead())
239 Paths.push_back(tmpPath);
240 at = delim + 1;
241 delim = strchr(at, PathSeparator);
242 }
243
244 if (*at != 0)
245 if (tmpPath.set(std::string(at)))
246 if (tmpPath.canRead())
247 Paths.push_back(tmpPath);
248}
249
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000250static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
251 assert(Sep[0] != '\0' && Sep[1] == '\0' &&
252 "Sep must be a 1-character string literal.");
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000253 if (path.empty())
254 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000255
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000256 // If the path is all slashes, return a single slash.
257 // Otherwise, remove all trailing slashes.
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000258
Evan Cheng34cd4a42008-05-05 18:30:58 +0000259 signed pos = static_cast<signed>(path.size()) - 1;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000260
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000261 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000262 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000263
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000264 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000265 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000266
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000267 // Any slashes left?
268 signed i = 0;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000269
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000270 while (i < pos && path[i] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000271 ++i;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000272
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000273 if (i == pos) // No slashes? Return "."
274 return ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000275
276 // There is at least one slash left. Remove all trailing non-slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000277 while (pos >= 0 && path[pos] != Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000278 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000279
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000280 // Remove any trailing slashes.
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000281 while (pos >= 0 && path[pos] == Sep[0])
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000282 --pos;
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000283
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000284 if (pos < 0)
Jeffrey Yasskin88cd3582009-12-17 21:02:39 +0000285 return path[0] == Sep[0] ? Sep : ".";
Michael J. Spencer4a295d32010-08-31 06:36:46 +0000286
Ted Kremenekcf55c8e2008-04-07 21:53:57 +0000287 return path.substr(0, pos+1);
288}
289
Reid Spencerb89a2232004-08-25 06:20:07 +0000290// Include the truly platform-specific parts of this class.
Reid Spencerdafe55f2004-12-24 06:29:17 +0000291#if defined(LLVM_ON_UNIX)
Reid Spencerbccc8ab2005-01-09 23:29:00 +0000292#include "Unix/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000293#endif
294#if defined(LLVM_ON_WIN32)
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000295#include "Windows/Path.inc"
Reid Spencerdafe55f2004-12-24 06:29:17 +0000296#endif