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