blob: 950af90035bf676f6cb06b3501ddcd15d4c3a9e2 [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 Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000017#include "llvm/ADT/SetVector.h"
Chris Lattner32018152005-06-27 06:28:45 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner5f48ff72005-07-16 08:01:13 +000019#include <list>
Dan Gohmanc9235d22008-03-21 23:51:57 +000020#include <map>
Chris Lattner32018152005-06-27 06:28:45 +000021
22namespace llvm {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000023 class BinaryObject;
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000024 class Constant;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000025 class ConstantStruct;
26 class ELFCodeEmitter;
Chris Lattner32018152005-06-27 06:28:45 +000027 class GlobalVariable;
Chris Lattner75bbdff2005-07-11 03:11:10 +000028 class Mangler;
Chris Lattner6871aed2005-07-11 05:15:32 +000029 class MachineCodeEmitter;
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000030 class TargetAsmInfo;
31 class TargetELFWriterInfo;
Owen Andersoncb371882008-08-21 00:14:44 +000032 class raw_ostream;
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000033 class ELFSection;
34 class ELFSym;
35 class ELFRelocation;
Chris Lattner32018152005-06-27 06:28:45 +000036
37 /// ELFWriter - This class implements the common target-independent code for
38 /// writing ELF files. Targets should derive a class from this to
39 /// parameterize the output format.
40 ///
41 class ELFWriter : public MachineFunctionPass {
Chris Lattner6871aed2005-07-11 05:15:32 +000042 friend class ELFCodeEmitter;
43 public:
Devang Patel19974732007-05-03 01:11:54 +000044 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000045
Chris Lattner6871aed2005-07-11 05:15:32 +000046 MachineCodeEmitter &getMachineCodeEmitter() const {
47 return *(MachineCodeEmitter*)MCE;
48 }
49
Owen Andersoncb371882008-08-21 00:14:44 +000050 ELFWriter(raw_ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000051 ~ELFWriter();
52
Chris Lattner5f48ff72005-07-16 08:01:13 +000053 typedef std::vector<unsigned char> DataBuffer;
54
Chris Lattner32018152005-06-27 06:28:45 +000055 protected:
Chris Lattner32018152005-06-27 06:28:45 +000056 /// Output stream to send the resultant object file to.
Owen Andersoncb371882008-08-21 00:14:44 +000057 raw_ostream &O;
Chris Lattner32018152005-06-27 06:28:45 +000058
59 /// Target machine description.
Chris Lattner32018152005-06-27 06:28:45 +000060 TargetMachine &TM;
61
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000062 /// Target Elf Writer description.
63 const TargetELFWriterInfo *TEW;
64
Chris Lattner75bbdff2005-07-11 03:11:10 +000065 /// Mang - The object used to perform name mangling for this module.
Chris Lattner75bbdff2005-07-11 03:11:10 +000066 Mangler *Mang;
67
Chris Lattner6871aed2005-07-11 05:15:32 +000068 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
69 /// code for functions to the .o file.
70 ELFCodeEmitter *MCE;
71
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000072 /// TAI - Target Asm Info, provide information about section names for
73 /// globals and other target specific stuff.
74 const TargetAsmInfo *TAI;
75
Chris Lattner32018152005-06-27 06:28:45 +000076 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000077 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000078 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000079
80 /// is64Bit/isLittleEndian - This information is inferred from the target
81 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
82 bool is64Bit, isLittleEndian;
83
84 /// doInitialization - Emit the file header and all of the global variables
85 /// for the module to the ELF file.
86 bool doInitialization(Module &M);
Chris Lattner32018152005-06-27 06:28:45 +000087 bool runOnMachineFunction(MachineFunction &MF);
88
Chris Lattner32018152005-06-27 06:28:45 +000089 /// doFinalization - Now that the module has been completely processed, emit
90 /// the ELF file to 'O'.
91 bool doFinalization(Module &M);
92
93 private:
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000094 /// Blob containing the Elf header
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000095 BinaryObject ElfHdr;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000096
Chris Lattner32018152005-06-27 06:28:45 +000097 /// SectionList - This is the list of sections that we have emitted to the
98 /// file. Once the file has been completely built, the section header table
99 /// is constructed from this info.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000100 std::list<ELFSection> SectionList;
101 unsigned NumSections; // Always = SectionList.size()
102
103 /// SectionLookup - This is a mapping from section name to section number in
104 /// the SectionList.
105 std::map<std::string, ELFSection*> SectionLookup;
106
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000107 /// GblSymLookup - This is a mapping from global value to a symbol index
108 /// in the symbol table. This is useful since relocations symbol references
109 /// must be quickly mapped to a symbol table index
110 std::map<const GlobalValue*, uint32_t> GblSymLookup;
111
112 /// SymbolList - This is the list of symbols emitted to the symbol table
113 /// Local symbols go to the front and Globals to the back.
114 std::list<ELFSym> SymbolList;
115
116 /// PendingGlobals - List of externally defined symbols that we have been
117 /// asked to emit, but have not seen a reference to. When a reference
118 /// is seen, the symbol will move from this list to the SymbolList.
119 SetVector<GlobalValue*> PendingGlobals;
120
Chris Lattner5f48ff72005-07-16 08:01:13 +0000121 /// getSection - Return the section with the specified name, creating a new
122 /// section if one does not already exist.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000123 ELFSection &getSection(const std::string &Name, unsigned Type,
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000124 unsigned Flags = 0, unsigned Align = 0) {
Chris Lattner5f48ff72005-07-16 08:01:13 +0000125 ELFSection *&SN = SectionLookup[Name];
126 if (SN) return *SN;
127
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000128 // Remove tab from section name prefix. This is necessary becase TAI
129 // sometimes return a section name prefixed with a "\t" char.
130 std::string SectionName(Name);
131 size_t Pos = SectionName.find("\t");
132 if (Pos != std::string::npos)
133 SectionName.erase(Pos, 1);
134
135 SectionList.push_back(ELFSection(SectionName, isLittleEndian, is64Bit));
Chris Lattner5f48ff72005-07-16 08:01:13 +0000136 SN = &SectionList.back();
137 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000138 SN->Type = Type;
139 SN->Flags = Flags;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000140 SN->Link = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000141 SN->Align = Align;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000142 return *SN;
143 }
Chris Lattner32018152005-06-27 06:28:45 +0000144
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000145 /// TODO: support mangled names here to emit the right .text section
146 /// for c++ object files.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000147 ELFSection &getTextSection() {
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000148 return getSection(".text", ELFSection::SHT_PROGBITS,
149 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000150 }
151
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000152 /// Get a constant pool section based on the section name returned by TAI
153 ELFSection &getConstantPoolSection(std::string SName, unsigned Align) {
154 return getSection(SName, ELFSection::SHT_PROGBITS,
155 ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC, Align);
156 }
157
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000158 /// Return the relocation section of section 'S'. 'RelA' is true
159 /// if the relocation section contains entries with addends.
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000160 ELFSection &getRelocSection(std::string SName, bool RelA, unsigned Align) {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000161 std::string RelSName(".rel");
162 unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
163
164 if (RelA) RelSName.append("a");
165 RelSName.append(SName);
166
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000167 return getSection(RelSName, SHdrTy, 0, Align);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000168 }
169
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000170 ELFSection &getNonExecStackSection() {
171 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
172 }
173
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000174 ELFSection &getSymbolTableSection() {
175 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
176 }
177
178 ELFSection &getStringTableSection() {
179 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
180 }
181
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000182 ELFSection &getSectionHeaderStringTableSection() {
183 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
184 }
185
Chris Lattner56c32612005-07-16 17:40:34 +0000186 ELFSection &getDataSection() {
187 return getSection(".data", ELFSection::SHT_PROGBITS,
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000188 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Chris Lattner56c32612005-07-16 17:40:34 +0000189 }
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000190
Chris Lattner56c32612005-07-16 17:40:34 +0000191 ELFSection &getBSSSection() {
192 return getSection(".bss", ELFSection::SHT_NOBITS,
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000193 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Chris Lattner56c32612005-07-16 17:40:34 +0000194 }
195
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000196 ELFSection &getNullSection() {
197 return getSection("", ELFSection::SHT_NULL, 0);
198 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000199
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000200 // Helpers for obtaining ELF specific Linkage and Visibility info.
201 unsigned getGlobalELFLinkage(const GlobalValue *GV);
202 unsigned getGlobalELFVisibility(const GlobalValue *GV);
203
Chris Lattner5f48ff72005-07-16 08:01:13 +0000204 // As we complete the ELF file, we need to update fields in the ELF header
205 // (e.g. the location of the section table). These members keep track of
206 // the offset in ELFHeader of these various pieces to update and other
207 // locations in the file.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000208 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
209 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
210 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000211
Chris Lattner32018152005-06-27 06:28:45 +0000212 private:
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000213 void EmitFunctionDeclaration(const Function *F);
214 void EmitGlobalVar(const GlobalVariable *GV);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000215 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000216 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000217 ELFSection &GblS);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000218 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000219 void EmitRelocations();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000220 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000221 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Chris Lattner32018152005-06-27 06:28:45 +0000222 void EmitSectionTableStringTable();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000223 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000224 void EmitSymbolTable();
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000225 void EmitStringTable();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000226 void OutputSectionsAndSectionTable();
Chris Lattner32018152005-06-27 06:28:45 +0000227 };
228}
229
230#endif