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