blob: e64a9c946b6db354e5be7b162b2746f03ce6b321 [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:
33 MachineCodeEmitter &getMachineCodeEmitter() const {
34 return *(MachineCodeEmitter*)MCE;
35 }
36
Bill Wendling4b2ca1a2007-02-08 01:30:50 +000037 ELFWriter(std::ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000038 ~ELFWriter();
39
Chris Lattner5f48ff72005-07-16 08:01:13 +000040 typedef std::vector<unsigned char> DataBuffer;
41
Chris Lattner32018152005-06-27 06:28:45 +000042 protected:
Chris Lattner32018152005-06-27 06:28:45 +000043 /// Output stream to send the resultant object file to.
44 ///
45 std::ostream &O;
46
47 /// Target machine description.
48 ///
49 TargetMachine &TM;
50
Chris Lattner75bbdff2005-07-11 03:11:10 +000051 /// Mang - The object used to perform name mangling for this module.
52 ///
53 Mangler *Mang;
54
Chris Lattner6871aed2005-07-11 05:15:32 +000055 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
56 /// code for functions to the .o file.
57 ELFCodeEmitter *MCE;
58
Chris Lattner32018152005-06-27 06:28:45 +000059 //===------------------------------------------------------------------===//
60 // Properties to be set by the derived class ctor, used to configure the
61 // ELFWriter.
62
63 // e_machine - This field is the target specific value to emit as the
64 // e_machine member of the ELF header.
65 unsigned short e_machine;
66
67 // e_flags - The machine flags for the target. This defaults to zero.
68 unsigned e_flags;
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
82 bool runOnMachineFunction(MachineFunction &MF);
83
84
85 /// doFinalization - Now that the module has been completely processed, emit
86 /// the ELF file to 'O'.
87 bool doFinalization(Module &M);
88
89 private:
Chris Lattner5f48ff72005-07-16 08:01:13 +000090 // The buffer we accumulate the file header into. Note that this should be
Chris Lattner32018152005-06-27 06:28:45 +000091 // changed into something much more efficient later (and the bytecode writer
92 // as well!).
Chris Lattner5f48ff72005-07-16 08:01:13 +000093 DataBuffer FileHeader;
Chris Lattner32018152005-06-27 06:28:45 +000094
95 /// ELFSection - This struct contains information about each section that is
Chris Lattner5f48ff72005-07-16 08:01:13 +000096 /// emitted to the file. This is eventually turned into the section header
97 /// table at the end of the file.
Chris Lattner32018152005-06-27 06:28:45 +000098 struct ELFSection {
99 std::string Name; // Name of the section.
100 unsigned NameIdx; // Index in .shstrtab of name, once emitted.
101 unsigned Type;
102 unsigned Flags;
103 uint64_t Addr;
104 unsigned Offset;
105 unsigned Size;
106 unsigned Link;
107 unsigned Info;
108 unsigned Align;
109 unsigned EntSize;
Chris Lattnere578ab92005-07-07 07:00:37 +0000110
Chris Lattner5f48ff72005-07-16 08:01:13 +0000111 /// SectionIdx - The number of the section in the Section Table.
112 ///
113 unsigned short SectionIdx;
114
115 /// SectionData - The actual data for this section which we are building
116 /// up for emission to the file.
117 DataBuffer SectionData;
118
Chris Lattnere578ab92005-07-07 07:00:37 +0000119 enum { SHT_NULL = 0, SHT_PROGBITS = 1, SHT_SYMTAB = 2, SHT_STRTAB = 3,
120 SHT_RELA = 4, SHT_HASH = 5, SHT_DYNAMIC = 6, SHT_NOTE = 7,
121 SHT_NOBITS = 8, SHT_REL = 9, SHT_SHLIB = 10, SHT_DYNSYM = 11 };
122 enum { SHN_UNDEF = 0, SHN_ABS = 0xFFF1, SHN_COMMON = 0xFFF2 };
Chris Lattnerf5d507e2005-07-12 06:40:29 +0000123 enum { // SHF - ELF Section Header Flags
124 SHF_WRITE = 1 << 0, // Writable
125 SHF_ALLOC = 1 << 1, // Mapped into the process addr space
126 SHF_EXECINSTR = 1 << 2, // Executable
127 SHF_MERGE = 1 << 4, // Might be merged if equal
128 SHF_STRINGS = 1 << 5, // Contains null-terminated strings
129 SHF_INFO_LINK = 1 << 6, // 'sh_info' contains SHT index
130 SHF_LINK_ORDER = 1 << 7, // Preserve order after combining
131 SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
132 SHF_GROUP = 1 << 9, // Section is a member of a group
Chris Lattner410354f2006-02-22 16:23:43 +0000133 SHF_TLS = 1 << 10 // Section holds thread-local data
Chris Lattnerf5d507e2005-07-12 06:40:29 +0000134 };
Chris Lattnere578ab92005-07-07 07:00:37 +0000135
Chris Lattner5f48ff72005-07-16 08:01:13 +0000136 ELFSection(const std::string &name)
137 : Name(name), Type(0), Flags(0), Addr(0), Offset(0), Size(0),
Chris Lattner32018152005-06-27 06:28:45 +0000138 Link(0), Info(0), Align(0), EntSize(0) {
139 }
140 };
141
142 /// SectionList - This is the list of sections that we have emitted to the
143 /// file. Once the file has been completely built, the section header table
144 /// is constructed from this info.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000145 std::list<ELFSection> SectionList;
146 unsigned NumSections; // Always = SectionList.size()
147
148 /// SectionLookup - This is a mapping from section name to section number in
149 /// the SectionList.
150 std::map<std::string, ELFSection*> SectionLookup;
151
152 /// getSection - Return the section with the specified name, creating a new
153 /// section if one does not already exist.
Chris Lattner5f9cb592005-07-16 17:35:26 +0000154 ELFSection &getSection(const std::string &Name,
155 unsigned Type, unsigned Flags = 0) {
Chris Lattner5f48ff72005-07-16 08:01:13 +0000156 ELFSection *&SN = SectionLookup[Name];
157 if (SN) return *SN;
158
159 SectionList.push_back(Name);
160 SN = &SectionList.back();
161 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000162 SN->Type = Type;
163 SN->Flags = Flags;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000164 return *SN;
165 }
Chris Lattner32018152005-06-27 06:28:45 +0000166
Chris Lattner56c32612005-07-16 17:40:34 +0000167 ELFSection &getDataSection() {
168 return getSection(".data", ELFSection::SHT_PROGBITS,
169 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
170 }
171 ELFSection &getBSSSection() {
172 return getSection(".bss", ELFSection::SHT_NOBITS,
173 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
174 }
175
Chris Lattnere578ab92005-07-07 07:00:37 +0000176 /// ELFSym - This struct contains information about each symbol that is
177 /// added to logical symbol table for the module. This is eventually
178 /// turned into a real symbol table in the file.
179 struct ELFSym {
Chris Lattnerf0cf1b72005-07-11 06:16:24 +0000180 const GlobalValue *GV; // The global value this corresponds to.
181 unsigned NameIdx; // Index in .strtab of name, once emitted.
Chris Lattnere578ab92005-07-07 07:00:37 +0000182 uint64_t Value;
183 unsigned Size;
184 unsigned char Info;
185 unsigned char Other;
186 unsigned short SectionIdx;
187
188 enum { STB_LOCAL = 0, STB_GLOBAL = 1, STB_WEAK = 2 };
189 enum { STT_NOTYPE = 0, STT_OBJECT = 1, STT_FUNC = 2, STT_SECTION = 3,
190 STT_FILE = 4 };
Chris Lattnerf0cf1b72005-07-11 06:16:24 +0000191 ELFSym(const GlobalValue *gv) : GV(gv), Value(0), Size(0), Info(0),
192 Other(0), SectionIdx(0) {}
Chris Lattnere578ab92005-07-07 07:00:37 +0000193
194 void SetBind(unsigned X) {
195 assert(X == (X & 0xF) && "Bind value out of range!");
196 Info = (Info & 0x0F) | (X << 4);
197 }
198 void SetType(unsigned X) {
199 assert(X == (X & 0xF) && "Type value out of range!");
200 Info = (Info & 0xF0) | X;
201 }
202 };
203
204 /// SymbolTable - This is the list of symbols we have emitted to the file.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000205 /// This actually gets rearranged before emission to the file (to put the
206 /// local symbols first in the list).
Chris Lattnere578ab92005-07-07 07:00:37 +0000207 std::vector<ELFSym> SymbolTable;
208
Chris Lattner5f48ff72005-07-16 08:01:13 +0000209 // As we complete the ELF file, we need to update fields in the ELF header
210 // (e.g. the location of the section table). These members keep track of
211 // the offset in ELFHeader of these various pieces to update and other
212 // locations in the file.
Chris Lattner32018152005-06-27 06:28:45 +0000213 unsigned ELFHeader_e_shoff_Offset; // e_shoff in ELF header.
214 unsigned ELFHeader_e_shstrndx_Offset; // e_shstrndx in ELF header.
215 unsigned ELFHeader_e_shnum_Offset; // e_shnum in ELF header.
Chris Lattner32018152005-06-27 06:28:45 +0000216 private:
Chris Lattner56c32612005-07-16 17:40:34 +0000217 void EmitGlobal(GlobalVariable *GV);
Chris Lattnere578ab92005-07-07 07:00:37 +0000218
219 void EmitSymbolTable();
Chris Lattner32018152005-06-27 06:28:45 +0000220
221 void EmitSectionTableStringTable();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000222 void OutputSectionsAndSectionTable();
Chris Lattner32018152005-06-27 06:28:45 +0000223 };
224}
225
226#endif