summaryrefslogtreecommitdiff
path: root/include/mcld/LD/ELFWriter.h
blob: 51834d16d127286d80bb058f1c3b2645db823c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//===- ELFWriter.h --------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef MCLD_ELF_WRITER_H
#define MCLD_ELF_WRITER_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif

#include <llvm/Support/ELF.h>
#include <mcld/MC/MCLDOutput.h>

namespace llvm {
class MCSectionData;
}

namespace mcld
{

class MCLDInfo;
class Layout;
class GNULDBackend;
class Relocation;
class LDSection;

/** \class ELFWriter
 *  \brief ELFWriter provides basic functions to write ELF sections, symbols,
 *  and so on.
 */
class ELFWriter
{
public:
  typedef uint64_t FileOffset;

protected:
  ELFWriter(GNULDBackend& pBackend)
  : f_Backend(pBackend) {
  }

public:
  virtual ~ELFWriter() { }

  GNULDBackend& target()
  { return f_Backend; }

  const GNULDBackend& target() const
  { return f_Backend; }

  virtual void writeELF32Header(const MCLDInfo& pInfo,
                                const Layout& pLayout,
                                const GNULDBackend& pBackend,
                                Output& pOutput) const;

  virtual void writeELF64Header(const MCLDInfo& pInfo,
                                const Layout& pLayout,
                                const GNULDBackend& pBackend,
                                Output& pOutput) const;

  virtual uint64_t getEntryPoint(const MCLDInfo& pInfo,
                                 const Layout& pLayout,
                                 const GNULDBackend& pBackend,
                                 const Output& pOutput) const;

protected:
  void emitELF32SectionHeader(Output& pOutput, MCLinker& pLinker) const;

  void emitELF64SectionHeader(Output& pOutput, MCLinker& pLinker) const;

  // emitShStrTab - emit .shstrtab
  void emitELF32ShStrTab(Output& pOutput, MCLinker& pLinker) const;

  void emitELF64ShStrTab(Output& pOutput, MCLinker& pLinker) const;

  void emitSectionData(const Layout& pLayout,
                       const LDSection& pSection,
                       MemoryRegion& pRegion) const;

  void emitRelocation(const Layout& pLayout,
                      const Output& pOutput,
                      const LDSection& pSection,
                      MemoryRegion& pRegion) const;

  void emitRel(const Layout& pLayout,
               const Output& pOutput,
               const llvm::MCSectionData& pSectionData,
               MemoryRegion& pRegion) const;

  void emitRela(const Layout& pLayout,
                const Output& pOutput,
                const llvm::MCSectionData& pSectionData,
                MemoryRegion& pRegion) const;

private:
  // getSectEntrySize - compute ElfXX_Shdr::sh_entsize
  uint64_t getELF32SectEntrySize(const LDSection& pSection) const;

  // getSectEntrySize - compute ElfXX_Shdr::sh_entsize
  uint64_t getELF64SectEntrySize(const LDSection& pSection) const;

  // getSectEntrySize - compute ElfXX_Shdr::sh_link
  uint64_t getSectLink(const LDSection& pSection, const Output& pOutput) const;

  // getSectEntrySize - compute ElfXX_Shdr::sh_info
  uint64_t getSectInfo(const LDSection& pSection, const Output& pOutput) const;

  uint64_t getELF32LastStartOffset(const Output& pOutput) const;

  uint64_t getELF64LastStartOffset(const Output& pOutput) const;

protected:
  GNULDBackend& f_Backend;
};

} // namespace of mcld

#endif