GNUArchiveReader.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
1//===- GNUArchiveReader.h -------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_LD_GNUARCHIVEREADER_H
10#define MCLD_LD_GNUARCHIVEREADER_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <mcld/LD/ArchiveReader.h>
16#include <mcld/LD/Archive.h>
17
18namespace mcld {
19
20class Module;
21class Input;
22class ELFObjectReader;
23class Archive;
24class LinkerConfig;
25
26/** \class GNUArchiveReader
27 *  \brief GNUArchiveReader reads GNU archive files.
28 */
29class GNUArchiveReader : public ArchiveReader
30{
31public:
32  GNUArchiveReader(Module& pModule, ELFObjectReader& pELFObjectReader);
33
34  ~GNUArchiveReader();
35
36  /// readArchive - read an archive, include the needed members, and build up
37  /// the subtree
38  bool readArchive(const LinkerConfig& pConfig, Archive& pArchive);
39
40  /// isMyFormat
41  bool isMyFormat(Input& input, bool &pContinue) const;
42
43private:
44  /// isArchive
45  bool isArchive(const char* pStr) const;
46
47  /// isThinArchive
48  bool isThinArchive(const char* pStr) const;
49
50  /// isThinArchive
51  bool isThinArchive(Input& input) const;
52
53  /// readMemberHeader - read the header of a member in a archive file and then
54  /// return the corresponding archive member (it may be an input object or
55  /// another archive)
56  /// @param pArchiveRoot  - the archive root that holds the strtab (extended
57  ///                        name table)
58  /// @param pArchiveFile  - the archive that contains the needed object
59  /// @param pFileOffset   - file offset of the member header in the archive
60  /// @param pNestedOffset - used when we find a nested archive
61  /// @param pMemberSize   - the file size of this member
62  Input* readMemberHeader(Archive& pArchiveRoot,
63                          Input& pArchiveFile,
64                          uint32_t pFileOffset,
65                          uint32_t& pNestedOffset,
66                          size_t& pMemberSize);
67
68  /// readSymbolTable - read the archive symbol map (armap)
69  bool readSymbolTable(Archive& pArchive);
70
71  /// readStringTable - read the strtab for long file name of the archive
72  bool readStringTable(Archive& pArchive);
73
74  /// shouldIncludeSymbol - given a sym name from armap and check if we should
75  /// include the corresponding archive member, and then return the decision
76  enum Archive::Symbol::Status
77  shouldIncludeSymbol(const llvm::StringRef& pSymName) const;
78
79  /// includeMember - include the object member in the given file offset, and
80  /// return the size of the object
81  /// @param pConfig - LinkerConfig
82  /// @param pArchiveRoot - the archive root
83  /// @param pFileOffset  - file offset of the member header in the archive
84  size_t includeMember(const LinkerConfig& pConfig,
85                       Archive& pArchiveRoot,
86                       uint32_t pFileOffset);
87
88  /// includeAllMembers - include all object members. This is called if
89  /// --whole-archive is the attribute for this archive file.
90  bool includeAllMembers(const LinkerConfig& pConfig, Archive& pArchive);
91
92private:
93  Module& m_Module;
94  ELFObjectReader& m_ELFObjectReader;
95};
96
97} // namespace of mcld
98
99#endif
100
101