f27d78f4f0131667d2ba7ecf29cbc9826773e73d
[oota-llvm.git] / lib / CodeGen / ELFWriter.h
1 //===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ELFWriter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef ELFWRITER_H
15 #define ELFWRITER_H
16
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include <list>
19
20 namespace llvm {
21   class GlobalVariable;
22   class Mangler;
23   class MachineCodeEmitter;
24   class ELFCodeEmitter;
25
26   /// ELFWriter - This class implements the common target-independent code for
27   /// writing ELF files.  Targets should derive a class from this to
28   /// parameterize the output format.
29   ///
30   class ELFWriter : public MachineFunctionPass {
31     friend class ELFCodeEmitter;
32   public:
33     static char ID;
34
35     MachineCodeEmitter &getMachineCodeEmitter() const {
36       return *(MachineCodeEmitter*)MCE;
37     }
38
39     ELFWriter(std::ostream &O, TargetMachine &TM);
40     ~ELFWriter();
41
42     typedef std::vector<unsigned char> DataBuffer;
43
44   protected:
45     /// Output stream to send the resultant object file to.
46     ///
47     std::ostream &O;
48
49     /// Target machine description.
50     ///
51     TargetMachine &TM;
52
53     /// Mang - The object used to perform name mangling for this module.
54     ///
55     Mangler *Mang;
56
57     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
58     /// code for functions to the .o file.
59     ELFCodeEmitter *MCE;
60
61     //===------------------------------------------------------------------===//
62     // Properties to be set by the derived class ctor, used to configure the
63     // ELFWriter.
64
65     // e_machine - This field is the target specific value to emit as the
66     // e_machine member of the ELF header.
67     unsigned short e_machine;
68
69     // e_flags - The machine flags for the target.  This defaults to zero.
70     unsigned e_flags;
71
72     //===------------------------------------------------------------------===//
73     // Properties inferred automatically from the target machine.
74     //
75
76     /// is64Bit/isLittleEndian - This information is inferred from the target
77     /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
78     bool is64Bit, isLittleEndian;
79
80     /// doInitialization - Emit the file header and all of the global variables
81     /// for the module to the ELF file.
82     bool doInitialization(Module &M);
83
84     bool runOnMachineFunction(MachineFunction &MF);
85
86
87     /// doFinalization - Now that the module has been completely processed, emit
88     /// the ELF file to 'O'.
89     bool doFinalization(Module &M);
90
91   private:
92     // The buffer we accumulate the file header into.  Note that this should be
93     // changed into something much more efficient later (and the bitcode writer
94     // as well!).
95     DataBuffer FileHeader;
96
97     /// ELFSection - This struct contains information about each section that is
98     /// emitted to the file.  This is eventually turned into the section header
99     /// table at the end of the file.
100     struct ELFSection {
101       std::string Name;       // Name of the section.
102       unsigned NameIdx;       // Index in .shstrtab of name, once emitted.
103       unsigned Type;
104       unsigned Flags;
105       uint64_t Addr;
106       unsigned Offset;
107       unsigned Size;
108       unsigned Link;
109       unsigned Info;
110       unsigned Align;
111       unsigned EntSize;
112
113       /// SectionIdx - The number of the section in the Section Table.
114       ///
115       unsigned short SectionIdx;
116
117       /// SectionData - The actual data for this section which we are building
118       /// up for emission to the file.
119       DataBuffer SectionData;
120
121       enum { SHT_NULL = 0, SHT_PROGBITS = 1, SHT_SYMTAB = 2, SHT_STRTAB = 3,
122              SHT_RELA = 4, SHT_HASH = 5, SHT_DYNAMIC = 6, SHT_NOTE = 7,
123              SHT_NOBITS = 8, SHT_REL = 9, SHT_SHLIB = 10, SHT_DYNSYM = 11 };
124       enum { SHN_UNDEF = 0, SHN_ABS = 0xFFF1, SHN_COMMON = 0xFFF2 };
125       enum {   // SHF - ELF Section Header Flags
126         SHF_WRITE            = 1 << 0, // Writable
127         SHF_ALLOC            = 1 << 1, // Mapped into the process addr space
128         SHF_EXECINSTR        = 1 << 2, // Executable
129         SHF_MERGE            = 1 << 4, // Might be merged if equal
130         SHF_STRINGS          = 1 << 5, // Contains null-terminated strings
131         SHF_INFO_LINK        = 1 << 6, // 'sh_info' contains SHT index
132         SHF_LINK_ORDER       = 1 << 7, // Preserve order after combining
133         SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
134         SHF_GROUP            = 1 << 9, // Section is a member of a group
135         SHF_TLS              = 1 << 10 // Section holds thread-local data
136       };
137
138       ELFSection(const std::string &name)
139         : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
140           Link(0), Info(0), Align(0), EntSize(0) {
141       }
142     };
143
144     /// SectionList - This is the list of sections that we have emitted to the
145     /// file.  Once the file has been completely built, the section header table
146     /// is constructed from this info.
147     std::list<ELFSection> SectionList;
148     unsigned NumSections;   // Always = SectionList.size()
149
150     /// SectionLookup - This is a mapping from section name to section number in
151     /// the SectionList.
152     std::map<std::string, ELFSection*> SectionLookup;
153
154     /// getSection - Return the section with the specified name, creating a new
155     /// section if one does not already exist.
156     ELFSection &getSection(const std::string &Name,
157                            unsigned Type, unsigned Flags = 0) {
158       ELFSection *&SN = SectionLookup[Name];
159       if (SN) return *SN;
160
161       SectionList.push_back(Name);
162       SN = &SectionList.back();
163       SN->SectionIdx = NumSections++;
164       SN->Type = Type;
165       SN->Flags = Flags;
166       return *SN;
167     }
168
169     ELFSection &getDataSection() {
170       return getSection(".data", ELFSection::SHT_PROGBITS,
171                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
172     }
173     ELFSection &getBSSSection() {
174       return getSection(".bss", ELFSection::SHT_NOBITS,
175                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
176     }
177
178     /// ELFSym - This struct contains information about each symbol that is
179     /// added to logical symbol table for the module.  This is eventually
180     /// turned into a real symbol table in the file.
181     struct ELFSym {
182       const GlobalValue *GV;    // The global value this corresponds to.
183       unsigned NameIdx;         // Index in .strtab of name, once emitted.
184       uint64_t Value;
185       unsigned Size;
186       unsigned char Info;
187       unsigned char Other;
188       unsigned short SectionIdx;
189
190       enum { STB_LOCAL = 0, STB_GLOBAL = 1, STB_WEAK = 2 };
191       enum { STT_NOTYPE = 0, STT_OBJECT = 1, STT_FUNC = 2, STT_SECTION = 3,
192              STT_FILE = 4 };
193       ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0),
194                                       Other(0), SectionIdx(0) {}
195
196       void SetBind(unsigned X) {
197         assert(X == (X & 0xF) && "Bind value out of range!");
198         Info = (Info & 0x0F) | (X << 4);
199       }
200       void SetType(unsigned X) {
201         assert(X == (X & 0xF) && "Type value out of range!");
202         Info = (Info & 0xF0) | X;
203       }
204     };
205
206     /// SymbolTable - This is the list of symbols we have emitted to the file.
207     /// This actually gets rearranged before emission to the file (to put the
208     /// local symbols first in the list).
209     std::vector<ELFSym> SymbolTable;
210
211     // As we complete the ELF file, we need to update fields in the ELF header
212     // (e.g. the location of the section table).  These members keep track of
213     // the offset in ELFHeader of these various pieces to update and other
214     // locations in the file.
215     unsigned ELFHeader_e_shoff_Offset;     // e_shoff    in ELF header.
216     unsigned ELFHeader_e_shstrndx_Offset;  // e_shstrndx in ELF header.
217     unsigned ELFHeader_e_shnum_Offset;     // e_shnum    in ELF header.
218   private:
219     void EmitGlobal(GlobalVariable *GV);
220
221     void EmitSymbolTable();
222
223     void EmitSectionTableStringTable();
224     void OutputSectionsAndSectionTable();
225   };
226 }
227
228 #endif