• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ELFObjectWriter.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_ELFOBJWRITER_H
10 #define MCLD_LD_ELFOBJWRITER_H
11 #include <mcld/LD/ObjectWriter.h>
12 #include <cassert>
13 
14 #include <mcld/Support/FileOutputBuffer.h>
15 
16 namespace mcld {
17 
18 class EhFrame;
19 class Module;
20 class LinkerConfig;
21 class GNULDBackend;
22 class FragmentLinker;
23 class Relocation;
24 class LDSection;
25 class SectionData;
26 class RelocData;
27 class Output;
28 
29 /** \class ELFObjectWriter
30  *  \brief ELFObjectWriter writes the target-independent parts of object files.
31  *  ELFObjectWriter reads a MCLDFile and writes into raw_ostream
32  *
33  */
34 class ELFObjectWriter : public ObjectWriter
35 {
36 public:
37   ELFObjectWriter(GNULDBackend& pBackend, const LinkerConfig& pConfig);
38 
39   ~ELFObjectWriter();
40 
41   std::error_code writeObject(Module& pModule, FileOutputBuffer& pOutput);
42 
43   size_t getOutputSize(const Module& pModule) const;
44 
45 private:
46   void writeSection(Module& pModule,
47                     FileOutputBuffer& pOutput, LDSection *section);
48 
target()49   GNULDBackend&       target()        { return m_Backend; }
50 
target()51   const GNULDBackend& target() const  { return m_Backend; }
52 
53   // writeELFHeader - emit ElfXX_Ehdr
54   template<size_t SIZE>
55   void writeELFHeader(const LinkerConfig& pConfig,
56                       const Module& pModule,
57                       FileOutputBuffer& pOutput) const;
58 
59   uint64_t getEntryPoint(const LinkerConfig& pConfig,
60                          const Module& pModule) const;
61 
62   // emitSectionHeader - emit ElfXX_Shdr
63   template<size_t SIZE>
64   void emitSectionHeader(const Module& pModule,
65                          const LinkerConfig& pConfig,
66                          FileOutputBuffer& pOutput) const;
67 
68   // emitProgramHeader - emit ElfXX_Phdr
69   template<size_t SIZE>
70   void emitProgramHeader(FileOutputBuffer& pOutput) const;
71 
72   // emitShStrTab - emit .shstrtab
73   void emitShStrTab(const LDSection& pShStrTab,
74                     const Module& pModule,
75                     FileOutputBuffer& pOutput);
76 
77   void emitSectionData(const LDSection& pSection, MemoryRegion& pRegion) const;
78 
79   void emitEhFrame(Module& pModule,
80                    EhFrame& pFrame, MemoryRegion& pRegion) const;
81 
82   void emitRelocation(const LinkerConfig& pConfig,
83                       const LDSection& pSection,
84                       MemoryRegion& pRegion) const;
85 
86   // emitRel - emit ElfXX_Rel
87   template<size_t SIZE>
88   void emitRel(const LinkerConfig& pConfig,
89                const RelocData& pRelocData,
90                MemoryRegion& pRegion) const;
91 
92   // emitRela - emit ElfXX_Rela
93   template<size_t SIZE>
94   void emitRela(const LinkerConfig& pConfig,
95                 const RelocData& pRelocData,
96                 MemoryRegion& pRegion) const;
97 
98   // getSectEntrySize - compute ElfXX_Shdr::sh_entsize
99   template<size_t SIZE>
100   uint64_t getSectEntrySize(const LDSection& pSection) const;
101 
102   // getSectLink - compute ElfXX_Shdr::sh_link
103   uint64_t getSectLink(const LDSection& pSection,
104                        const LinkerConfig& pConfig) const;
105 
106   // getSectInfo - compute ElfXX_Shdr::sh_info
107   uint64_t getSectInfo(const LDSection& pSection) const;
108 
109   template<size_t SIZE>
getLastStartOffset(const Module & pModule)110   uint64_t getLastStartOffset(const Module& pModule) const
111   {
112     assert(0 && "Call invalid ELFObjectWriter::getLastStartOffset");
113     return 0;
114   }
115 
116   void emitSectionData(const SectionData& pSD, MemoryRegion& pRegion) const;
117 
118 private:
119   GNULDBackend& m_Backend;
120 
121   const LinkerConfig& m_Config;
122 };
123 
124 template<>
125 uint64_t ELFObjectWriter::getLastStartOffset<32>(const Module& pModule) const;
126 
127 template<>
128 uint64_t ELFObjectWriter::getLastStartOffset<64>(const Module& pModule) const;
129 
130 } // namespace of mcld
131 
132 #endif
133 
134