1//===- ELFWriter.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_WRITER_H
10#define MCLD_ELF_WRITER_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14
15#include <llvm/Support/ELF.h>
16#include <mcld/MC/MCLDOutput.h>
17
18namespace mcld
19{
20
21class MCLDInfo;
22class Layout;
23class GNULDBackend;
24class Relocation;
25class LDSection;
26class SectionData;
27
28/** \class ELFWriter
29 *  \brief ELFWriter provides basic functions to write ELF sections, symbols,
30 *  and so on.
31 */
32class ELFWriter
33{
34public:
35  typedef uint64_t FileOffset;
36
37protected:
38  ELFWriter(GNULDBackend& pBackend)
39  : f_Backend(pBackend) {
40  }
41
42public:
43  virtual ~ELFWriter() { }
44
45  GNULDBackend& target()
46  { return f_Backend; }
47
48  const GNULDBackend& target() const
49  { return f_Backend; }
50
51  virtual void writeELF32Header(const MCLDInfo& pInfo,
52                                const Layout& pLayout,
53                                const GNULDBackend& pBackend,
54                                Output& pOutput) const;
55
56  virtual void writeELF64Header(const MCLDInfo& pInfo,
57                                const Layout& pLayout,
58                                const GNULDBackend& pBackend,
59                                Output& pOutput) const;
60
61  virtual uint64_t getEntryPoint(const MCLDInfo& pInfo,
62                                 const Layout& pLayout,
63                                 const GNULDBackend& pBackend,
64                                 const Output& pOutput) const;
65
66protected:
67  void emitELF32SectionHeader(Output& pOutput, MCLinker& pLinker) const;
68
69  void emitELF64SectionHeader(Output& pOutput, MCLinker& pLinker) const;
70
71  void emitELF32ProgramHeader(Output& pOutput,
72                              const GNULDBackend& pBackend) const;
73
74  void emitELF64ProgramHeader(Output& pOutput,
75                              const GNULDBackend& pBackend) const;
76
77  // emitShStrTab - emit .shstrtab
78  void emitELF32ShStrTab(Output& pOutput, MCLinker& pLinker) const;
79
80  void emitELF64ShStrTab(Output& pOutput, MCLinker& pLinker) const;
81
82  void emitSectionData(const Layout& pLayout,
83                       const LDSection& pSection,
84                       MemoryRegion& pRegion) const;
85
86  void emitRelocation(const Layout& pLayout,
87                      const Output& pOutput,
88                      const LDSection& pSection,
89                      MemoryRegion& pRegion) const;
90
91  void emitRel(const Layout& pLayout,
92               const Output& pOutput,
93               const SectionData& pSectionData,
94               MemoryRegion& pRegion) const;
95
96  void emitRela(const Layout& pLayout,
97                const Output& pOutput,
98                const SectionData& pSectionData,
99                MemoryRegion& pRegion) const;
100
101private:
102  // getSectEntrySize - compute ElfXX_Shdr::sh_entsize
103  uint64_t getELF32SectEntrySize(const LDSection& pSection) const;
104
105  // getSectEntrySize - compute ElfXX_Shdr::sh_entsize
106  uint64_t getELF64SectEntrySize(const LDSection& pSection) const;
107
108  // getSectEntrySize - compute ElfXX_Shdr::sh_link
109  uint64_t getSectLink(const LDSection& pSection, const Output& pOutput) const;
110
111  // getSectEntrySize - compute ElfXX_Shdr::sh_info
112  uint64_t getSectInfo(const LDSection& pSection, const Output& pOutput) const;
113
114  uint64_t getELF32LastStartOffset(const Output& pOutput) const;
115
116  uint64_t getELF64LastStartOffset(const Output& pOutput) const;
117
118protected:
119  GNULDBackend& f_Backend;
120};
121
122} // namespace of mcld
123
124#endif
125
126