Make isDynamicLibrary detect more than just an ELF file.
[oota-llvm.git] / lib / System / Path.cpp
1 //===-- Path.cpp - Implement OS Path Concept --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file implements the operating system Path concept.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/Path.h"
15 #include "llvm/Config/config.h"
16 #include <cassert>
17 #include <ostream>
18 using namespace llvm;
19 using namespace sys;
20
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //===          independent code.
24 //===----------------------------------------------------------------------===//
25
26 std::ostream& llvm::operator<<(std::ostream &strm, const sys::Path &aPath) {
27   strm << aPath.toString();
28   return strm;
29 }
30
31 Path
32 Path::GetLLVMConfigDir() {
33   Path result;
34 #ifdef LLVM_ETCDIR
35   if (result.set(LLVM_ETCDIR))
36     return result;
37 #endif
38   return GetLLVMDefaultConfigDir();
39 }
40
41 LLVMFileType
42 sys::IdentifyFileType(const char*magic, unsigned length) {
43   assert(magic && "Invalid magic number string");
44   assert(length >=4 && "Invalid magic number length");
45   switch (magic[0]) {
46     case 'l':
47       if (magic[1] == 'l' && magic[2] == 'v')
48         if (magic[3] == 'c')
49           return CompressedBytecode_FileType;
50         else if (magic[3] == 'm')
51           return Bytecode_FileType;
52       break;
53     case '!':
54       if (length >= 8)
55         if (memcmp(magic,"!<arch>\n",8) == 0)
56           return Archive_FileType;
57       break;
58       
59     case '\177':
60       if (magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F')
61         return ELF_FileType;
62       break;
63
64     case 0xCE:
65     case 0xCF:
66       // This is complicated by an overlap with Java class files. 
67       // See the Mach-O section in /usr/share/file/magic for details.
68       if (magic[1] == char(0xFA) && magic[2] == char(0xED) && 
69           magic[3] == char(0xFE))
70         if (length >= 15)
71           if (magic[15] == 1 || magic[15] == 3 || magic[15] == 6 || 
72               magic[15] == 9)
73             return Mach_O_FileType;
74       break;
75
76     case 0xF0: // PowerPC Windows
77     case 0x83: // Alpha 32-bit
78     case 0x84: // Alpha 64-bit
79     case 0x66: // MPS R4000 Windows
80     case 0x50: // mc68K
81     case 0x4c: // 80386 Windows
82       if (magic[1] == 0x01)
83         return COFF_FileType;
84
85     case 0x90: // PA-RISC Windows
86     case 0x68: // mc68K Windows
87       if (magic[1] == 0x02)
88         return COFF_FileType;
89       break;
90
91     default:
92       break;
93   }
94   return Unknown_FileType;
95 }
96
97 bool
98 Path::isArchive() const {
99   if (canRead())
100     return hasMagicNumber("!<arch>\012");
101   return false;
102 }
103
104 bool
105 Path::isDynamicLibrary() const {
106   if (canRead()) {
107     std::string Magic;
108     if (getMagicNumber(Magic, 64))
109       switch (IdentifyFileType(Magic.c_str(), Magic.length())) {
110         default: return false;
111         case ELF_FileType:
112         case Mach_O_FileType:
113         case COFF_FileType:  return true;
114       }
115   }
116   return false;
117 }
118
119 Path
120 Path::FindLibrary(std::string& name) {
121   std::vector<sys::Path> LibPaths;
122   GetSystemLibraryPaths(LibPaths);
123   for (unsigned i = 0; i < LibPaths.size(); ++i) {
124     sys::Path FullPath(LibPaths[i]);
125     FullPath.appendComponent("lib" + name + LTDL_SHLIB_EXT);
126     if (FullPath.isDynamicLibrary())
127       return FullPath;
128     FullPath.eraseSuffix();
129     FullPath.appendSuffix("a");
130     if (FullPath.isArchive())
131       return FullPath;
132   }
133   return sys::Path();
134 }
135
136 std::string Path::GetDLLSuffix() {
137   return LTDL_SHLIB_EXT;
138 }
139
140 // Include the truly platform-specific parts of this class.
141 #if defined(LLVM_ON_UNIX)
142 #include "Unix/Path.inc"
143 #endif
144 #if defined(LLVM_ON_WIN32)
145 #include "Win32/Path.inc"
146 #endif
147
148 DEFINING_FILE_FOR(SystemPath)