Home | History | Annotate | Download | only in LD
      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_GNU_ARCHIVE_READER_H
     10 #define MCLD_GNU_ARCHIVE_READER_H
     11 #ifdef ENABLE_UNITTEST
     12 #include <gtest.h>
     13 #endif
     14 
     15 #include "mcld/LD/ArchiveReader.h"
     16 #include "mcld/Support/Path.h"
     17 #include <llvm/ADT/OwningPtr.h>
     18 
     19 #include <vector>
     20 #include <string>
     21 
     22 namespace llvm
     23 {
     24 class MemoryBuffer;
     25 
     26 }
     27 
     28 namespace mcld
     29 {
     30 class MCLDInfo;
     31 class Input;
     32 class InputTree;
     33 
     34 /** \class GNUArchiveReader
     35  *  \brief GNUArchiveReader reads GNU archive files.
     36  */
     37 class GNUArchiveReader : public ArchiveReader
     38 {
     39 private:
     40   struct ArchiveMemberHeader;
     41   struct SymbolTableEntry;
     42 
     43 public:
     44   explicit GNUArchiveReader(MCLDInfo &pLDInfo, LDReader::Endian endian)
     45   : m_pLDInfo(pLDInfo),
     46     m_endian(endian)
     47   { }
     48 
     49   ~GNUArchiveReader()
     50   { }
     51 
     52   /// Read an archive and extract each member in.
     53   /// Construct the coresponding Input for each member.
     54   InputTree *readArchive(Input &input);
     55 
     56   bool isMyFormat(Input &input) const;
     57 
     58   LDReader::Endian endian(Input& pFile) const;
     59 
     60 private:
     61   /// set up the archive, including
     62   /// first, read symbol table
     63   /// second, read extended file name which is used in thin archive
     64   InputTree *setupNewArchive(Input &pInput, size_t off);
     65 
     66   /// parse the archive header, and return the member size
     67   size_t parseMemberHeader(llvm::OwningPtr<llvm::MemoryBuffer> &mapFile,
     68                    off_t off,
     69                    std::string *p_Name,
     70                    off_t *nestedOff,
     71                    std::string &p_ExtendedName);
     72 
     73   void readSymbolTable(llvm::OwningPtr<llvm::MemoryBuffer> &mapFile,
     74                       std::vector<SymbolTableEntry> &pSymbolTable,
     75                       off_t start,
     76                       size_t size);
     77 
     78 private:
     79   MCLDInfo &m_pLDInfo;
     80   LDReader::Endian m_endian;
     81 };
     82 
     83 } // namespace of mcld
     84 
     85 #endif
     86