ELFReaderIf.h revision 6f75755c9204b1d8817ae5a65a2f7e5af0ec3f70
1//===- ELFReader.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_ELF_READER_INTERFACE_H
10#define MCLD_ELF_READER_INTERFACE_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(void* pELFHeader) const = 0;
50
51  /// isMyEndian - is this ELF file in the same endian to me?
52  virtual bool isMyEndian(void* pELFHeader) const = 0;
53
54  /// isMyMachine - is this ELF file generated for the same machine.
55  virtual bool isMyMachine(void* pELFHeader) const = 0;
56
57  /// fileType - the file type of this file
58  virtual Input::Type fileType(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, void* pELFHeader) const = 0;
67
68  /// readRegularSection - read a regular section and create fragments.
69  virtual bool readRegularSection(Input& pInput, SectionData& pSD) const = 0;
70
71  /// readSymbols - read ELF symbols and create LDSymbol
72  virtual bool readSymbols(Input& pInput,
73                           IRBuilder& pBuilder,
74                           const MemoryRegion& pRegion,
75                           const char* StrTab) const = 0;
76
77  /// readSignature - read a symbol from the given Input and index in symtab
78  /// This is used to get the signature of a group section.
79  virtual ResolveInfo* readSignature(Input& pInput,
80                                     LDSection& pSymTab,
81                                     uint32_t pSymIdx) const = 0;
82
83  /// readRela - read ELF rela and create Relocation
84  virtual bool readRela(Input& pInput,
85                        LDSection& pSection,
86                        const MemoryRegion& pRegion) const = 0;
87
88  /// readRel - read ELF rel and create Relocation
89  virtual bool readRel(Input& pInput,
90                       LDSection& pSection,
91                       const MemoryRegion& pRegion) const = 0;
92
93  /// readDynamic - read ELF .dynamic in input dynobj
94  virtual bool readDynamic(Input& pInput) const = 0;
95
96protected:
97  /// LinkInfo - some section needs sh_link and sh_info, remember them.
98  struct LinkInfo {
99    LDSection* section;
100    uint32_t sh_link;
101    uint32_t sh_info;
102  };
103
104  typedef std::vector<LinkInfo> LinkInfoList;
105
106protected:
107  ResolveInfo::Type getSymType(uint8_t pInfo, uint16_t pShndx) const;
108
109  ResolveInfo::Desc getSymDesc(uint16_t pShndx, const Input& pInput) const;
110
111  ResolveInfo::Binding getSymBinding(uint8_t pBinding,
112                                     uint16_t pShndx,
113                                     uint8_t pVisibility) const;
114
115  uint64_t getSymValue(uint64_t pValue,
116                       uint16_t pShndx,
117                       const Input& pInput) const;
118
119  FragmentRef* getSymFragmentRef(Input& pInput,
120                                 uint16_t pShndx,
121                                 uint32_t pOffset) const;
122
123  ResolveInfo::Visibility getSymVisibility(uint8_t pVis) const;
124
125protected:
126  GNULDBackend& m_Backend;
127};
128
129} // namespace of mcld
130
131#endif
132
133