blob: 04cd1773c16115769fa2eb9725a6c8e4e64031df [file] [log] [blame]
Chris Lattner32018152005-06-27 06:28:45 +00001//===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner56c32612005-07-16 17:40:34 +00005// This file was developed by Chris Lattner and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Chris Lattner32018152005-06-27 06:28:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ELFWriter class.
11//
12//===----------------------------------------------------------------------===//
13
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000014#ifndef ELFWRITER_H
15#define ELFWRITER_H
Chris Lattner32018152005-06-27 06:28:45 +000016
17#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner5f48ff72005-07-16 08:01:13 +000018#include <list>
Chris Lattner32018152005-06-27 06:28:45 +000019
20namespace llvm {
21 class GlobalVariable;
Chris Lattner75bbdff2005-07-11 03:11:10 +000022 class Mangler;
Chris Lattner6871aed2005-07-11 05:15:32 +000023 class MachineCodeEmitter;
24 class ELFCodeEmitter;
Chris Lattner32018152005-06-27 06:28:45 +000025
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 {
Chris Lattner6871aed2005-07-11 05:15:32 +000031 friend class ELFCodeEmitter;
32 public:
Devang Patel19974732007-05-03 01:11:54 +000033 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000034
Chris Lattner6871aed2005-07-11 05:15:32 +000035 MachineCodeEmitter &getMachineCodeEmitter() const {
36 return *(MachineCodeEmitter*)MCE;
37 }
38
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000039 ELFWriter(std::ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000040 ~ELFWriter();
41
Chris Lattner5f48ff72005-07-16 08:01:13 +000042 typedef std::vector<unsigned char> DataBuffer;
43
Chris Lattner32018152005-06-27 06:28:45 +000044 protected:
Chris Lattner32018152005-06-27 06:28:45 +000045 /// Output stream to send the resultant object file to.
46 ///
47 std::ostream &O;
48
49 /// Target machine description.
50 ///
51 TargetMachine &TM;
52
Chris Lattner75bbdff2005-07-11 03:11:10 +000053 /// Mang - The object used to perform name mangling for this module.
54 ///
55 Mangler *Mang;
56
Chris Lattner6871aed2005-07-11 05:15:32 +000057 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
58 /// code for functions to the .o file.
59 ELFCodeEmitter *MCE;
60
Chris Lattner32018152005-06-27 06:28:45 +000061 //===------------------------------------------------------------------===//
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:
Chris Lattner5f48ff72005-07-16 08:01:13 +000092 // The buffer we accumulate the file header into. Note that this should be
Chris Lattner32018152005-06-27 06:28:45 +000093 // changed into something much more efficient later (and the bytecode writer
94 // as well!).
Chris Lattner5f48ff72005-07-16 08:01:13 +000095 DataBuffer FileHeader;
Chris Lattner32018152005-06-27 06:28:45 +000096
97 /// ELFSection - This struct contains information about each section that is
Chris Lattner5f48ff72005-07-16 08:01:13 +000098 /// emitted to the file. This is eventually turned into the section header
99 /// table at the end of the file.
Chris Lattner32018152005-06-27 06:28:45 +0000100 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;
Chris Lattnere578ab92005-07-07 07:00:37 +0000112
Chris Lattner5f48ff72005-07-16 08:01:13 +0000113 /// 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
Chris Lattnere578ab92005-07-07 07:00:37 +0000121 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 };
Chris Lattnerf5d507e2005-07-12 06:40:29 +0000125 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
Chris Lattner410354f2006-02-22 16:23:43 +0000135 SHF_TLS = 1 << 10 // Section holds thread-local data
Chris Lattnerf5d507e2005-07-12 06:40:29 +0000136 };
Chris Lattnere578ab92005-07-07 07:00:37 +0000137
Chris Lattner5f48ff72005-07-16 08:01:13 +0000138 ELFSection(const std::string &name)
139 : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
Chris Lattner32018152005-06-27 06:28:45 +0000140 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.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000147 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.
Chris Lattner5f9cb592005-07-16 17:35:26 +0000156 ELFSection &getSection(const std::string &Name,
157 unsigned Type, unsigned Flags = 0) {
Chris Lattner5f48ff72005-07-16 08:01:13 +0000158 ELFSection *&SN = SectionLookup[Name];
159 if (SN) return *SN;
160
161 SectionList.push_back(Name);
162 SN = &SectionList.back();
163 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000164 SN->Type = Type;
165 SN->Flags = Flags;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000166 return *SN;
167 }
Chris Lattner32018152005-06-27 06:28:45 +0000168
Chris Lattner56c32612005-07-16 17:40:34 +0000169 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
Chris Lattnere578ab92005-07-07 07:00:37 +0000178 /// 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 {
Chris Lattnerf0cf1b72005-07-11 06:16:24 +0000182 const GlobalValue *GV; // The global value this corresponds to.
183 unsigned NameIdx; // Index in .strtab of name, once emitted.
Chris Lattnere578ab92005-07-07 07:00:37 +0000184 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 };
Chris Lattnerf0cf1b72005-07-11 06:16:24 +0000193 ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0),
194 Other(0), SectionIdx(0) {}
Chris Lattnere578ab92005-07-07 07:00:37 +0000195
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.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000207 /// This actually gets rearranged before emission to the file (to put the
208 /// local symbols first in the list).
Chris Lattnere578ab92005-07-07 07:00:37 +0000209 std::vector<ELFSym> SymbolTable;
210
Chris Lattner5f48ff72005-07-16 08:01:13 +0000211 // 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.
Chris Lattner32018152005-06-27 06:28:45 +0000215 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.
Chris Lattner32018152005-06-27 06:28:45 +0000218 private:
Chris Lattner56c32612005-07-16 17:40:34 +0000219 void EmitGlobal(GlobalVariable *GV);
Chris Lattnere578ab92005-07-07 07:00:37 +0000220
221 void EmitSymbolTable();
Chris Lattner32018152005-06-27 06:28:45 +0000222
223 void EmitSectionTableStringTable();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000224 void OutputSectionsAndSectionTable();
Chris Lattner32018152005-06-27 06:28:45 +0000225 };
226}
227
228#endif