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"
20 // getMemberSize - compute the actual physical size of the file member as seen
21 // on disk. This isn't the size of member's payload. Use getSize() for that.
23 ArchiveMember::getMemberSize() const {
24 // Basically its the file size plus the header size
25 unsigned result = info.fileSize + sizeof(ArchiveMemberHeader);
27 // If it has a long filename, include the name length
28 if (hasLongFilename())
29 result += path.get().length() + 1;
31 // If its now odd lengthed, include the padding byte
38 // This default constructor is only use by the ilist when it creates its
39 // sentry node. We give it specific static values to make it stand out a bit.
40 ArchiveMember::ArchiveMember()
41 : next(0), prev(0), parent(0), path("<invalid>"), flags(0), data(0)
47 info.modTime = sys::TimeValue::now();
50 // This is the constructor that the Archive class uses when it is building or
51 // reading an archive. It just defaults a few things and ensures the parent is
52 // set for the iplist. The Archive class fills in the ArchiveMember's data.
53 // This is required because correctly setting the data may depend on other
54 // things in the Archive.
55 ArchiveMember::ArchiveMember(Archive* PAR)
56 : next(0), prev(0), parent(PAR), path(), flags(0), data(0)
60 // This method allows an ArchiveMember to be replaced with the data for a
61 // different file, presumably as an update to the member. It also makes sure
62 // the flags are reset correctly.
63 void ArchiveMember::replaceWith(const sys::Path& newFile) {
64 assert(newFile.exists() && "Can't replace with a non-existent file");
68 // Foreign symbol tables have an empty name
69 if (path.get() == ARFILE_SYMTAB_NAME)
70 flags |= ForeignSymbolTableFlag;
72 flags &= ~ForeignSymbolTableFlag;
74 // LLVM symbol tables have a very specific name
75 if (path.get() == ARFILE_LLVM_SYMTAB_NAME)
76 flags |= LLVMSymbolTableFlag;
78 flags &= ~LLVMSymbolTableFlag;
81 if (path.get() == ARFILE_STRTAB_NAME)
82 flags |= StringTableFlag;
84 flags &= ~StringTableFlag;
86 // If it has a slash then it has a path
87 bool hasSlash = path.get().find('/') != std::string::npos;
91 flags &= ~HasPathFlag;
93 // If it has a slash or its over 15 chars then its a long filename format
94 if (hasSlash || path.get().length() > 15)
95 flags |= HasLongFilenameFlag;
97 flags &= ~HasLongFilenameFlag;
99 // Get the signature and status info
101 const char* signature = (const char*) data;
103 path.getMagicNumber(magic,4);
104 signature = magic.c_str();
105 path.getStatusInfo(info);
108 // Determine what kind of file it is
109 switch (sys::IdentifyFileType(signature,4)) {
110 case sys::BytecodeFileType:
111 flags |= BytecodeFlag;
113 case sys::CompressedBytecodeFileType:
114 flags |= CompressedBytecodeFlag;
115 flags &= ~CompressedFlag;
118 flags &= ~(BytecodeFlag|CompressedBytecodeFlag);
123 // Archive constructor - this is the only constructor that gets used for the
124 // Archive class. Everything else (default,copy) is deprecated. This just
125 // initializes and maps the file into memory, if requested.
126 Archive::Archive(const sys::Path& filename, bool map )
127 : archPath(filename), members(), mapfile(0), base(0), symTab(), symTabSize(0)
130 mapfile = new sys::MappedFile(filename);
131 base = (char*) mapfile->map();
135 // Archive destructor - just clean up memory
136 Archive::~Archive() {
137 // Shutdown the file mapping
142 // Delete any ModuleProviders and ArchiveMember's we've allocated as a result
143 // of symbol table searches.
144 for (ModuleMap::iterator I=modules.begin(), E=modules.end(); I != E; ++I ) {
145 delete I->second.first;
146 delete I->second.second;