1 //===- ELFReaderIf.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_ELFREADERIF_H_
10 #define MCLD_LD_ELFREADERIF_H_
11 
12 #include "mcld/LinkerConfig.h"
13 #include "mcld/Support/MsgHandling.h"
14 #include "mcld/Target/GNULDBackend.h"
15 
16 #include <llvm/ADT/StringRef.h>
17 #include <llvm/Support/ELF.h>
18 #include <llvm/Support/Host.h>
19 
20 namespace mcld {
21 
22 class IRBuilder;
23 class FragmentRef;
24 class LDSection;
25 class SectionData;
26 
27 /** \class ELFReaderIF
28  *  \brief ELFReaderIF provides common interface for all kind of ELF readers.
29  */
30 class ELFReaderIF {
31  public:
ELFReaderIF(GNULDBackend & pBackend)32   explicit ELFReaderIF(GNULDBackend& pBackend) : m_Backend(pBackend) {}
33 
~ELFReaderIF()34   virtual ~ELFReaderIF() {}
35 
36   /// ELFHeaderSize - return the size of the ELFHeader
37   virtual size_t getELFHeaderSize() const = 0;
38 
39   /// isELF - is this a ELF file
40   virtual bool isELF(const void* pELFHeader) const = 0;
41 
42   /// isMyEndian - is this ELF file in the same endian to me?
43   virtual bool isMyEndian(const void* pELFHeader) const = 0;
44 
45   /// isMyMachine - is this ELF file generated for the same machine.
46   virtual bool isMyMachine(const void* pELFHeader) const = 0;
47 
48   /// fileType - the file type of this file
49   virtual Input::Type fileType(const void* pELFHeader) const = 0;
50 
51   /// target - the target backend
target()52   const GNULDBackend& target() const { return m_Backend; }
target()53   GNULDBackend& target() { return m_Backend; }
54 
55   /// readSectionHeaders - read ELF section header table and create LDSections
56   virtual bool readSectionHeaders(Input& pInput,
57                                   const void* pELFHeader) const = 0;
58 
59   /// readRegularSection - read a regular section and create fragments.
60   virtual bool readRegularSection(Input& pInput, SectionData& pSD) const = 0;
61 
62   /// readSymbols - read ELF symbols and create LDSymbol
63   virtual bool readSymbols(Input& pInput,
64                            IRBuilder& pBuilder,
65                            llvm::StringRef pRegion,
66                            const char* StrTab) const = 0;
67 
68   /// readSignature - read a symbol from the given Input and index in symtab
69   /// This is used to get the signature of a group section.
70   virtual ResolveInfo* readSignature(Input& pInput,
71                                      LDSection& pSymTab,
72                                      uint32_t pSymIdx) const = 0;
73 
74   /// readRela - read ELF rela and create Relocation
75   virtual bool readRela(Input& pInput,
76                         LDSection& pSection,
77                         llvm::StringRef pRegion) const = 0;
78 
79   /// readRel - read ELF rel and create Relocation
80   virtual bool readRel(Input& pInput,
81                        LDSection& pSection,
82                        llvm::StringRef pRegion) const = 0;
83 
84   /// readDynamic - read ELF .dynamic in input dynobj
85   virtual bool readDynamic(Input& pInput) const = 0;
86 
87  protected:
88   /// LinkInfo - some section needs sh_link and sh_info, remember them.
89   struct LinkInfo {
90     LDSection* section;
91     uint32_t sh_link;
92     uint32_t sh_info;
93   };
94 
95   typedef std::vector<LinkInfo> LinkInfoList;
96 
97  protected:
98   ResolveInfo::Type getSymType(uint8_t pInfo, uint16_t pShndx) const;
99 
100   ResolveInfo::Desc getSymDesc(uint16_t pShndx, const Input& pInput) const;
101 
102   ResolveInfo::Binding getSymBinding(uint8_t pBinding,
103                                      uint16_t pShndx,
104                                      uint8_t pVisibility) const;
105 
106   uint64_t getSymValue(uint64_t pValue,
107                        uint16_t pShndx,
108                        const Input& pInput) const;
109 
110   FragmentRef* getSymFragmentRef(Input& pInput,
111                                  uint16_t pShndx,
112                                  uint32_t pOffset) const;
113 
114   ResolveInfo::Visibility getSymVisibility(uint8_t pVis) const;
115 
116  protected:
117   GNULDBackend& m_Backend;
118 };
119 
120 }  // namespace mcld
121 
122 #endif  // MCLD_LD_ELFREADERIF_H_
123