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