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