1 //===-- Archive.cpp - Generic LLVM archive functions ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of the Archive and ArchiveMember
11 // classes that is common to both reading and writing archives..
13 //===----------------------------------------------------------------------===//
15 #include "ArchiveInternals.h"
16 #include "llvm/ModuleProvider.h"
17 #include "llvm/System/Process.h"
21 // 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.
24 ArchiveMember::getMemberSize() const {
25 // Basically its the file size plus the header size
26 unsigned result = info.fileSize + sizeof(ArchiveMemberHeader);
28 // If it has a long filename, include the name length
29 if (hasLongFilename())
30 result += path.toString().length() + 1;
32 // If its now odd lengthed, include the padding byte
39 // 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.
41 ArchiveMember::ArchiveMember()
42 : next(0), prev(0), parent(0), path("--invalid--"), flags(0), data(0)
44 info.user = sys::Process::GetCurrentUserId();
45 info.group = sys::Process::GetCurrentGroupId();
48 info.modTime = sys::TimeValue::now();
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
53 // 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
55 // things in the Archive.
56 ArchiveMember::ArchiveMember(Archive* PAR)
57 : next(0), prev(0), parent(PAR), path(), flags(0), data(0)
61 // This method allows an ArchiveMember to be replaced with the data for a
62 // different file, presumably as an update to the member. It also makes sure
63 // the flags are reset correctly.
64 bool ArchiveMember::replaceWith(const sys::Path& newFile, std::string* ErrMsg) {
65 if (!newFile.exists()) {
67 *ErrMsg = "Can not replace an archive member with a non-existent file";
74 // SVR4 symbol tables have an empty name
75 if (path.toString() == ARFILE_SVR4_SYMTAB_NAME)
76 flags |= SVR4SymbolTableFlag;
78 flags &= ~SVR4SymbolTableFlag;
80 // BSD4.4 symbol tables have a special name
81 if (path.toString() == ARFILE_BSD4_SYMTAB_NAME)
82 flags |= BSD4SymbolTableFlag;
84 flags &= ~BSD4SymbolTableFlag;
86 // LLVM symbol tables have a very specific name
87 if (path.toString() == ARFILE_LLVM_SYMTAB_NAME)
88 flags |= LLVMSymbolTableFlag;
90 flags &= ~LLVMSymbolTableFlag;
93 if (path.toString() == ARFILE_STRTAB_NAME)
94 flags |= StringTableFlag;
96 flags &= ~StringTableFlag;
98 // If it has a slash then it has a path
99 bool hasSlash = path.toString().find('/') != std::string::npos;
101 flags |= HasPathFlag;
103 flags &= ~HasPathFlag;
105 // If it has a slash or its over 15 chars then its a long filename format
106 if (hasSlash || path.toString().length() > 15)
107 flags |= HasLongFilenameFlag;
109 flags &= ~HasLongFilenameFlag;
111 // Get the signature and status info
112 const char* signature = (const char*) data;
115 path.getMagicNumber(magic,4);
116 signature = magic.c_str();
118 if (path.getFileStatus(info, ErrMsg))
122 // Determine what kind of file it is
123 switch (sys::IdentifyFileType(signature,4)) {
124 case sys::BytecodeFileType:
125 flags |= BytecodeFlag;
127 case sys::CompressedBytecodeFileType:
128 flags |= CompressedBytecodeFlag;
129 flags &= ~CompressedFlag;
132 flags &= ~(BytecodeFlag|CompressedBytecodeFlag);
138 // Archive constructor - this is the only constructor that gets used for the
139 // Archive class. Everything else (default,copy) is deprecated. This just
140 // initializes and maps the file into memory, if requested.
141 Archive::Archive(const sys::Path& filename)
142 : archPath(filename), members(), mapfile(0), base(0), symTab(), strtab(),
143 symTabSize(0), firstFileOffset(0), modules(), foreignST(0)
148 Archive::mapToMemory(std::string* ErrMsg)
150 mapfile = new sys::MappedFile();
151 if (mapfile->open(archPath, sys::MappedFile::READ_ACCESS, ErrMsg))
153 if (!(base = (char*) mapfile->map(ErrMsg)))
158 void Archive::cleanUpMemory() {
159 // Shutdown the file mapping
168 // Forget the entire symbol table
174 // Free the foreign symbol table member
180 // Delete any ModuleProviders and ArchiveMember's we've allocated as a result
181 // of symbol table searches.
182 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
183 delete I->second.first;
184 delete I->second.second;
188 // Archive destructor - just clean up memory
189 Archive::~Archive() {