blob: 0389185f1db4f7cbc316d0b6c17e5d5d99c45a3b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +000018#include "ELF.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include <list>
Dan Gohman249ddbf2008-03-21 23:51:57 +000020#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021
22namespace llvm {
23 class GlobalVariable;
24 class Mangler;
25 class MachineCodeEmitter;
26 class ELFCodeEmitter;
Owen Anderson847b99b2008-08-21 00:14:44 +000027 class raw_ostream;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
29 /// ELFWriter - This class implements the common target-independent code for
30 /// writing ELF files. Targets should derive a class from this to
31 /// parameterize the output format.
32 ///
33 class ELFWriter : public MachineFunctionPass {
34 friend class ELFCodeEmitter;
35 public:
36 static char ID;
37
38 MachineCodeEmitter &getMachineCodeEmitter() const {
39 return *(MachineCodeEmitter*)MCE;
40 }
41
Owen Anderson847b99b2008-08-21 00:14:44 +000042 ELFWriter(raw_ostream &O, TargetMachine &TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 ~ELFWriter();
44
45 typedef std::vector<unsigned char> DataBuffer;
46
47 protected:
48 /// Output stream to send the resultant object file to.
49 ///
Owen Anderson847b99b2008-08-21 00:14:44 +000050 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 /// Target machine description.
53 ///
54 TargetMachine &TM;
55
56 /// Mang - The object used to perform name mangling for this module.
57 ///
58 Mangler *Mang;
59
60 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
61 /// code for functions to the .o file.
62 ELFCodeEmitter *MCE;
63
64 //===------------------------------------------------------------------===//
65 // Properties to be set by the derived class ctor, used to configure the
66 // ELFWriter.
67
68 // e_machine - This field is the target specific value to emit as the
69 // e_machine member of the ELF header.
70 unsigned short e_machine;
71
72 // e_flags - The machine flags for the target. This defaults to zero.
73 unsigned e_flags;
74
75 //===------------------------------------------------------------------===//
76 // Properties inferred automatically from the target machine.
77 //
78
79 /// is64Bit/isLittleEndian - This information is inferred from the target
80 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
81 bool is64Bit, isLittleEndian;
82
83 /// doInitialization - Emit the file header and all of the global variables
84 /// for the module to the ELF file.
85 bool doInitialization(Module &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 bool runOnMachineFunction(MachineFunction &MF);
87
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 /// doFinalization - Now that the module has been completely processed, emit
89 /// the ELF file to 'O'.
90 bool doFinalization(Module &M);
91
92 private:
93 // The buffer we accumulate the file header into. Note that this should be
94 // changed into something much more efficient later (and the bitcode writer
95 // as well!).
96 DataBuffer FileHeader;
97
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 /// SectionList - This is the list of sections that we have emitted to the
99 /// file. Once the file has been completely built, the section header table
100 /// is constructed from this info.
101 std::list<ELFSection> SectionList;
102 unsigned NumSections; // Always = SectionList.size()
103
104 /// SectionLookup - This is a mapping from section name to section number in
105 /// the SectionList.
106 std::map<std::string, ELFSection*> SectionLookup;
107
108 /// getSection - Return the section with the specified name, creating a new
109 /// section if one does not already exist.
110 ELFSection &getSection(const std::string &Name,
111 unsigned Type, unsigned Flags = 0) {
112 ELFSection *&SN = SectionLookup[Name];
113 if (SN) return *SN;
114
115 SectionList.push_back(Name);
116 SN = &SectionList.back();
117 SN->SectionIdx = NumSections++;
118 SN->Type = Type;
119 SN->Flags = Flags;
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000120 SN->Link = ELFSection::SHN_UNDEF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 return *SN;
122 }
123
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000124 ELFSection &getTextSection() {
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000125 return getSection(".text", ELFSection::SHT_PROGBITS,
126 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000127 }
128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 ELFSection &getDataSection() {
130 return getSection(".data", ELFSection::SHT_PROGBITS,
131 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
132 }
133 ELFSection &getBSSSection() {
134 return getSection(".bss", ELFSection::SHT_NOBITS,
135 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
136 }
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 /// SymbolTable - This is the list of symbols we have emitted to the file.
139 /// This actually gets rearranged before emission to the file (to put the
140 /// local symbols first in the list).
141 std::vector<ELFSym> SymbolTable;
142
143 // As we complete the ELF file, we need to update fields in the ELF header
144 // (e.g. the location of the section table). These members keep track of
145 // the offset in ELFHeader of these various pieces to update and other
146 // locations in the file.
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000147 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
148 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
149 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 private:
151 void EmitGlobal(GlobalVariable *GV);
152
153 void EmitSymbolTable();
154
155 void EmitSectionTableStringTable();
156 void OutputSectionsAndSectionTable();
157 };
158}
159
160#endif