Remove remaining bits of the old LLVM specific symtab handling.
[oota-llvm.git] / tools / llvm-ar / Archive.cpp
1 //===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the implementation of the Archive and ArchiveMember
11 // classes that is common to both reading and writing archives..
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "Archive.h"
16 #include "ArchiveInternals.h"
17 #include "llvm/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/PathV1.h"
22 #include "llvm/Support/Process.h"
23 #include "llvm/Support/system_error.h"
24 #include <cstring>
25 #include <memory>
26 using namespace llvm;
27
28 // getMemberSize - compute the actual physical size of the file member as seen
29 // on disk. This isn't the size of member's payload. Use getSize() for that.
30 unsigned
31 ArchiveMember::getMemberSize() const {
32   // Basically its the file size plus the header size
33   unsigned result = Size + sizeof(ArchiveMemberHeader);
34
35   // If it has a long filename, include the name length
36   if (hasLongFilename())
37     result += path.length() + 1;
38
39   // If its now odd lengthed, include the padding byte
40   if (result % 2 != 0 )
41     result++;
42
43   return result;
44 }
45
46 // This default constructor is only use by the ilist when it creates its
47 // sentry node. We give it specific static values to make it stand out a bit.
48 ArchiveMember::ArchiveMember()
49   : parent(0), path("--invalid--"), flags(0), data(0)
50 {
51   User = sys::Process::GetCurrentUserId();
52   Group = sys::Process::GetCurrentGroupId();
53   Mode = 0777;
54   Size = 0;
55   ModTime = sys::TimeValue::now();
56 }
57
58 // This is the constructor that the Archive class uses when it is building or
59 // reading an archive. It just defaults a few things and ensures the parent is
60 // set for the iplist. The Archive class fills in the ArchiveMember's data.
61 // This is required because correctly setting the data may depend on other
62 // things in the Archive.
63 ArchiveMember::ArchiveMember(Archive* PAR)
64   : parent(PAR), path(), flags(0), data(0)
65 {
66 }
67
68 // This method allows an ArchiveMember to be replaced with the data for a
69 // different file, presumably as an update to the member. It also makes sure
70 // the flags are reset correctly.
71 bool ArchiveMember::replaceWith(StringRef newFile, std::string* ErrMsg) {
72   bool Exists;
73   if (sys::fs::exists(newFile.str(), Exists) || !Exists) {
74     if (ErrMsg)
75       *ErrMsg = "Can not replace an archive member with a non-existent file";
76     return true;
77   }
78
79   data = 0;
80   path = newFile.str();
81
82   // SVR4 symbol tables have an empty name
83   if (path == ARFILE_SVR4_SYMTAB_NAME)
84     flags |= SVR4SymbolTableFlag;
85   else
86     flags &= ~SVR4SymbolTableFlag;
87
88   // BSD4.4 symbol tables have a special name
89   if (path == ARFILE_BSD4_SYMTAB_NAME)
90     flags |= BSD4SymbolTableFlag;
91   else
92     flags &= ~BSD4SymbolTableFlag;
93
94   // String table name
95   if (path == ARFILE_STRTAB_NAME)
96     flags |= StringTableFlag;
97   else
98     flags &= ~StringTableFlag;
99
100   // If it has a slash then it has a path
101   bool hasSlash = path.find('/') != std::string::npos;
102   if (hasSlash)
103     flags |= HasPathFlag;
104   else
105     flags &= ~HasPathFlag;
106
107   // If it has a slash or its over 15 chars then its a long filename format
108   if (hasSlash || path.length() > 15)
109     flags |= HasLongFilenameFlag;
110   else
111     flags &= ~HasLongFilenameFlag;
112
113   // Get the signature and status info
114   const char* signature = (const char*) data;
115   SmallString<4> magic;
116   if (!signature) {
117     sys::fs::get_magic(path, magic.capacity(), magic);
118     signature = magic.c_str();
119     sys::PathWithStatus PWS(path);
120     const sys::FileStatus *FSinfo = PWS.getFileStatus(false, ErrMsg);
121     if (!FSinfo)
122       return true;
123     User = FSinfo->getUser();
124     Group = FSinfo->getGroup();
125     Mode = FSinfo->getMode();
126     ModTime = FSinfo->getTimestamp();
127     Size = FSinfo->getSize();
128   }
129
130   // Determine what kind of file it is.
131   if (sys::fs::identify_magic(StringRef(signature, 4)) ==
132       sys::fs::file_magic::bitcode)
133     flags |= BitcodeFlag;
134   else
135     flags &= ~BitcodeFlag;
136
137   return false;
138 }
139
140 // Archive constructor - this is the only constructor that gets used for the
141 // Archive class. Everything else (default,copy) is deprecated. This just
142 // initializes and maps the file into memory, if requested.
143 Archive::Archive(StringRef filename, LLVMContext &C)
144     : archPath(filename), members(), mapfile(0), base(0), strtab(),
145       firstFileOffset(0), modules(), Context(C) {}
146
147 bool
148 Archive::mapToMemory(std::string* ErrMsg) {
149   OwningPtr<MemoryBuffer> File;
150   if (error_code ec = MemoryBuffer::getFile(archPath.c_str(), File)) {
151     if (ErrMsg)
152       *ErrMsg = ec.message();
153     return true;
154   }
155   mapfile = File.take();
156   base = mapfile->getBufferStart();
157   return false;
158 }
159
160 void Archive::cleanUpMemory() {
161   // Shutdown the file mapping
162   delete mapfile;
163   mapfile = 0;
164   base = 0;
165
166   firstFileOffset = 0;
167
168   // Delete any Modules and ArchiveMember's we've allocated as a result of
169   // symbol table searches.
170   for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
171     delete I->second.first;
172     delete I->second.second;
173   }
174 }
175
176 // Archive destructor - just clean up memory
177 Archive::~Archive() {
178   cleanUpMemory();
179 }
180