This is supposed to be a preorder numbering of the dominator tree, not the CFG.
[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 is distributed under the University of Illinois Open Source
6 // 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/ADT/SetVector.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Support/OutputBuffer.h"
20 #include "llvm/Target/TargetAsmInfo.h"
21 #include "llvm/Target/TargetELFWriterInfo.h"
22 #include "ELF.h"
23 #include <list>
24 #include <map>
25
26 namespace llvm {
27   class ConstantStruct;
28   class ELFCodeEmitter;
29   class GlobalVariable;
30   class Mangler;
31   class MachineCodeEmitter;
32   class raw_ostream;
33
34   /// ELFWriter - This class implements the common target-independent code for
35   /// writing ELF files.  Targets should derive a class from this to
36   /// parameterize the output format.
37   ///
38   class ELFWriter : public MachineFunctionPass {
39     friend class ELFCodeEmitter;
40   public:
41     static char ID;
42
43     MachineCodeEmitter &getMachineCodeEmitter() const {
44       return *(MachineCodeEmitter*)MCE;
45     }
46
47     ELFWriter(raw_ostream &O, TargetMachine &TM);
48     ~ELFWriter();
49
50     typedef std::vector<unsigned char> DataBuffer;
51
52   protected:
53     /// Output stream to send the resultant object file to.
54     raw_ostream &O;
55
56     /// Target machine description.
57     TargetMachine &TM;
58
59     /// Mang - The object used to perform name mangling for this module.
60     Mangler *Mang;
61
62     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
63     /// code for functions to the .o file.
64     ELFCodeEmitter *MCE;
65
66     /// TAI - Target Asm Info, provide information about section names for
67     /// globals and other target specific stuff.
68     const TargetAsmInfo *TAI;
69
70     //===------------------------------------------------------------------===//
71     // Properties inferred automatically from the target machine.
72     //===------------------------------------------------------------------===//
73
74     /// is64Bit/isLittleEndian - This information is inferred from the target
75     /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
76     bool is64Bit, isLittleEndian;
77
78     /// doInitialization - Emit the file header and all of the global variables
79     /// for the module to the ELF file.
80     bool doInitialization(Module &M);
81     bool runOnMachineFunction(MachineFunction &MF);
82
83     /// doFinalization - Now that the module has been completely processed, emit
84     /// the ELF file to 'O'.
85     bool doFinalization(Module &M);
86
87   private:
88     // The buffer we accumulate the file header into.  Note that this should be
89     // changed into something much more efficient later (and the bitcode writer
90     // as well!).
91     DataBuffer FileHeader;
92
93     /// ElfHdr - Hold information about the ELF Header
94     ELFHeader *ElfHdr;
95
96     /// SectionList - This is the list of sections that we have emitted to the
97     /// file.  Once the file has been completely built, the section header table
98     /// is constructed from this info.
99     std::list<ELFSection> SectionList;
100     unsigned NumSections;   // Always = SectionList.size()
101
102     /// SectionLookup - This is a mapping from section name to section number in
103     /// the SectionList.
104     std::map<std::string, ELFSection*> SectionLookup;
105
106     /// getSection - Return the section with the specified name, creating a new
107     /// section if one does not already exist.
108     ELFSection &getSection(const std::string &Name, unsigned Type, 
109                            unsigned Flags = 0, unsigned Align = 0) {
110       ELFSection *&SN = SectionLookup[Name];
111       if (SN) return *SN;
112
113       SectionList.push_back(Name);
114       SN = &SectionList.back();
115       SN->SectionIdx = NumSections++;
116       SN->Type = Type;
117       SN->Flags = Flags;
118       SN->Link = ELFSection::SHN_UNDEF;
119       SN->Align = Align;
120       return *SN;
121     }
122
123     ELFSection &getTextSection() {
124       return getSection(".text", ELFSection::SHT_PROGBITS,
125                         ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
126     }
127
128     ELFSection &getSymbolTableSection() {
129       return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
130     }
131
132     ELFSection &getStringTableSection() {
133       return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
134     }
135
136     ELFSection &getDataSection() {
137       return getSection(".data", ELFSection::SHT_PROGBITS,
138                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
139     }
140
141     ELFSection &getBSSSection() {
142       return getSection(".bss", ELFSection::SHT_NOBITS,
143                         ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
144     }
145
146     /// SymbolTable - This is the list of symbols we have emitted to the file.
147     /// This actually gets rearranged before emission to the file (to put the
148     /// local symbols first in the list).
149     std::vector<ELFSym> SymbolTable;
150
151     /// PendingSyms - This is a list of externally defined symbols that we have
152     /// been asked to emit, but have not seen a reference to.  When a reference
153     /// is seen, the symbol will move from this list to the SymbolTable.
154     SetVector<GlobalValue*> PendingGlobals;
155
156     // As we complete the ELF file, we need to update fields in the ELF header
157     // (e.g. the location of the section table).  These members keep track of
158     // the offset in ELFHeader of these various pieces to update and other
159     // locations in the file.
160     unsigned ELFHdr_e_shoff_Offset;     // e_shoff    in ELF header.
161     unsigned ELFHdr_e_shstrndx_Offset;  // e_shstrndx in ELF header.
162     unsigned ELFHdr_e_shnum_Offset;     // e_shnum    in ELF header.
163   private:
164     void EmitGlobal(GlobalVariable *GV);
165     void EmitGlobalConstant(const Constant *C, OutputBuffer &GblCstTab);
166     void EmitGlobalConstantStruct(const ConstantStruct *CVS,
167                                   OutputBuffer &GblCstTab);
168     void EmitRelocations();
169     void EmitSectionHeader(OutputBuffer &TableOut, const ELFSection &Section);
170     void EmitSectionTableStringTable();
171     void EmitSymbol(OutputBuffer &SymTabOut, ELFSym &Sym);
172     void EmitSymbolTable();
173     void OutputSectionsAndSectionTable();
174   };
175 }
176
177 #endif