blob: a661a4e992a449cdaa0edc9364b789f5fe911cd2 [file] [log] [blame]
Reid Spencer362cbf02004-11-06 08:51:45 +00001//===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Reid Spencer362cbf02004-11-06 08:51:45 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer362cbf02004-11-06 08:51:45 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Reid Spencer362cbf02004-11-06 08:51:45 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencercf6afc62004-11-14 21:56:59 +000010// This file contains the implementation of the Archive and ArchiveMember
11// classes that is common to both reading and writing archives..
Reid Spencer362cbf02004-11-06 08:51:45 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "ArchiveInternals.h"
Reid Spencercf6afc62004-11-14 21:56:59 +000016#include "llvm/ModuleProvider.h"
Reid Spencer8e827e82005-04-21 17:49:57 +000017#include "llvm/System/Process.h"
Reid Spencer362cbf02004-11-06 08:51:45 +000018
19using namespace llvm;
20
Reid Spencercf6afc62004-11-14 21:56:59 +000021// getMemberSize - compute the actual physical size of the file member as seen
22// on disk. This isn't the size of member's payload. Use getSize() for that.
23unsigned
24ArchiveMember::getMemberSize() const {
25 // Basically its the file size plus the header size
26 unsigned result = info.fileSize + sizeof(ArchiveMemberHeader);
27
28 // If it has a long filename, include the name length
29 if (hasLongFilename())
Reid Spencer1fce0912004-12-11 00:14:15 +000030 result += path.toString().length() + 1;
Reid Spencercf6afc62004-11-14 21:56:59 +000031
32 // If its now odd lengthed, include the padding byte
Misha Brukman2b37d7c2005-04-21 21:13:18 +000033 if (result % 2 != 0 )
Reid Spencercf6afc62004-11-14 21:56:59 +000034 result++;
35
36 return result;
Reid Spencer362cbf02004-11-06 08:51:45 +000037}
38
Reid Spencercf6afc62004-11-14 21:56:59 +000039// This default constructor is only use by the ilist when it creates its
40// sentry node. We give it specific static values to make it stand out a bit.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000041ArchiveMember::ArchiveMember()
Jeff Cohen943b9b62006-05-06 23:25:53 +000042 : next(0), prev(0), parent(0), path("--invalid--"), flags(0), data(0)
Reid Spencercf6afc62004-11-14 21:56:59 +000043{
Reid Spencer8e827e82005-04-21 17:49:57 +000044 info.user = sys::Process::GetCurrentUserId();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000045 info.group = sys::Process::GetCurrentGroupId();
46 info.mode = 0777;
47 info.fileSize = 0;
Reid Spencercf6afc62004-11-14 21:56:59 +000048 info.modTime = sys::TimeValue::now();
49}
50
51// This is the constructor that the Archive class uses when it is building or
52// reading an archive. It just defaults a few things and ensures the parent is
Misha Brukman2b37d7c2005-04-21 21:13:18 +000053// set for the iplist. The Archive class fills in the ArchiveMember's data.
54// This is required because correctly setting the data may depend on other
Reid Spencercf6afc62004-11-14 21:56:59 +000055// things in the Archive.
56ArchiveMember::ArchiveMember(Archive* PAR)
57 : next(0), prev(0), parent(PAR), path(), flags(0), data(0)
58{
59}
60
Misha Brukman2b37d7c2005-04-21 21:13:18 +000061// This method allows an ArchiveMember to be replaced with the data for a
Reid Spencercf6afc62004-11-14 21:56:59 +000062// different file, presumably as an update to the member. It also makes sure
63// the flags are reset correctly.
Reid Spencer0ff2d312006-08-24 23:45:08 +000064bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
Reid Spencercf6afc62004-11-14 21:56:59 +000065 assert(newFile.exists() && "Can't replace with a non-existent file");
66 data = 0;
67 path = newFile;
68
Reid Spencer9a29db42004-11-20 07:29:40 +000069 // SVR4 symbol tables have an empty name
Reid Spencer1fce0912004-12-11 00:14:15 +000070 if (path.toString() == ARFILE_SVR4_SYMTAB_NAME)
Reid Spencer9a29db42004-11-20 07:29:40 +000071 flags |= SVR4SymbolTableFlag;
Reid Spencercf6afc62004-11-14 21:56:59 +000072 else
Reid Spencer9a29db42004-11-20 07:29:40 +000073 flags &= ~SVR4SymbolTableFlag;
74
75 // BSD4.4 symbol tables have a special name
Reid Spencer1fce0912004-12-11 00:14:15 +000076 if (path.toString() == ARFILE_BSD4_SYMTAB_NAME)
Reid Spencer9a29db42004-11-20 07:29:40 +000077 flags |= BSD4SymbolTableFlag;
78 else
79 flags &= ~BSD4SymbolTableFlag;
Reid Spencercf6afc62004-11-14 21:56:59 +000080
81 // LLVM symbol tables have a very specific name
Reid Spencer1fce0912004-12-11 00:14:15 +000082 if (path.toString() == ARFILE_LLVM_SYMTAB_NAME)
Reid Spencercf6afc62004-11-14 21:56:59 +000083 flags |= LLVMSymbolTableFlag;
84 else
85 flags &= ~LLVMSymbolTableFlag;
86
87 // String table name
Reid Spencer1fce0912004-12-11 00:14:15 +000088 if (path.toString() == ARFILE_STRTAB_NAME)
Reid Spencercf6afc62004-11-14 21:56:59 +000089 flags |= StringTableFlag;
90 else
91 flags &= ~StringTableFlag;
92
93 // If it has a slash then it has a path
Reid Spencer1fce0912004-12-11 00:14:15 +000094 bool hasSlash = path.toString().find('/') != std::string::npos;
Reid Spencercf6afc62004-11-14 21:56:59 +000095 if (hasSlash)
96 flags |= HasPathFlag;
97 else
98 flags &= ~HasPathFlag;
99
100 // If it has a slash or its over 15 chars then its a long filename format
Reid Spencer1fce0912004-12-11 00:14:15 +0000101 if (hasSlash || path.toString().length() > 15)
Reid Spencercf6afc62004-11-14 21:56:59 +0000102 flags |= HasLongFilenameFlag;
103 else
104 flags &= ~HasLongFilenameFlag;
105
106 // Get the signature and status info
Reid Spencercf6afc62004-11-14 21:56:59 +0000107 const char* signature = (const char*) data;
Chris Lattner252ad032006-07-28 22:03:44 +0000108 std::string magic;
Reid Spencercf6afc62004-11-14 21:56:59 +0000109 if (!signature) {
110 path.getMagicNumber(magic,4);
111 signature = magic.c_str();
Chris Lattner252ad032006-07-28 22:03:44 +0000112 std::string err;
Reid Spencer0ff2d312006-08-24 23:45:08 +0000113 if (path.getFileStatus(info, ErrMsg))
114 return true;
Reid Spencercf6afc62004-11-14 21:56:59 +0000115 }
116
117 // Determine what kind of file it is
118 switch (sys::IdentifyFileType(signature,4)) {
119 case sys::BytecodeFileType:
120 flags |= BytecodeFlag;
121 break;
122 case sys::CompressedBytecodeFileType:
123 flags |= CompressedBytecodeFlag;
124 flags &= ~CompressedFlag;
125 break;
126 default:
127 flags &= ~(BytecodeFlag|CompressedBytecodeFlag);
128 break;
129 }
Reid Spencer0ff2d312006-08-24 23:45:08 +0000130 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000131}
132
133// Archive constructor - this is the only constructor that gets used for the
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000134// Archive class. Everything else (default,copy) is deprecated. This just
Reid Spencercf6afc62004-11-14 21:56:59 +0000135// initializes and maps the file into memory, if requested.
Reid Spencer0ff2d312006-08-24 23:45:08 +0000136Archive::Archive(const sys::Path& filename)
Reid Spencer1f465802004-11-16 06:47:07 +0000137 : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
138 symTabSize(0), firstFileOffset(0), modules(), foreignST(0)
Reid Spencercf6afc62004-11-14 21:56:59 +0000139{
Reid Spencer0ff2d312006-08-24 23:45:08 +0000140}
141
142bool
143Archive::mapToMemory(std::string* ErrMsg)
144{
145 mapfile = new sys::MappedFile();
146 if (mapfile->open(archPath, sys::MappedFile::READ_ACCESS, ErrMsg))
147 return true;
148 if (!(base = (char*) mapfile->map(ErrMsg)))
149 return true;
150 return false;
Reid Spencercf6afc62004-11-14 21:56:59 +0000151}
152
Reid Spencer6ff72402005-11-30 05:21:10 +0000153void Archive::cleanUpMemory() {
Reid Spencercf6afc62004-11-14 21:56:59 +0000154 // Shutdown the file mapping
155 if (mapfile) {
Jeff Cohend19d89a2005-01-28 01:17:07 +0000156 mapfile->close();
Reid Spencercf6afc62004-11-14 21:56:59 +0000157 delete mapfile;
Reid Spencer6ff72402005-11-30 05:21:10 +0000158
159 mapfile = 0;
160 base = 0;
Reid Spencercf6afc62004-11-14 21:56:59 +0000161 }
Reid Spencer6ff72402005-11-30 05:21:10 +0000162
163 // Forget the entire symbol table
164 symTab.clear();
165 symTabSize = 0;
166
167 firstFileOffset = 0;
168
169 // Free the foreign symbol table member
170 if (foreignST) {
171 delete foreignST;
172 foreignST = 0;
173 }
174
Reid Spencercf6afc62004-11-14 21:56:59 +0000175 // Delete any ModuleProviders and ArchiveMember's we've allocated as a result
176 // of symbol table searches.
177 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
178 delete I->second.first;
179 delete I->second.second;
180 }
Reid Spencer362cbf02004-11-06 08:51:45 +0000181}
182
Reid Spencer6ff72402005-11-30 05:21:10 +0000183// Archive destructor - just clean up memory
184Archive::~Archive() {
185 cleanUpMemory();
186}
187