blob: b8bac5598ecfce2c4d01a4f42b603985888a27ba [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"
Dan Gohmanc9235d22008-03-21 23:51:57 +000019#include <map>
Chris Lattner32018152005-06-27 06:28:45 +000020
21namespace llvm {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000022 class BinaryObject;
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000023 class Constant;
Bruno Cardoso Lopes3e0094d2009-08-08 17:29:04 +000024 class ConstantInt;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000025 class ConstantStruct;
26 class ELFCodeEmitter;
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000027 class ELFRelocation;
28 class ELFSection;
Daniel Dunbar89e12a12009-07-13 06:00:13 +000029 struct ELFSym;
Chris Lattner32018152005-06-27 06:28:45 +000030 class GlobalVariable;
Reid Kleckner27632172009-09-20 23:52:43 +000031 class JITDebugRegisterer;
Chris Lattner75bbdff2005-07-11 03:11:10 +000032 class Mangler;
Chris Lattner6871aed2005-07-11 05:15:32 +000033 class MachineCodeEmitter;
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +000034 class MachineConstantPoolEntry;
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000035 class ObjectCodeEmitter;
Chris Lattneraf76e592009-08-22 20:48:53 +000036 class MCAsmInfo;
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000037 class TargetELFWriterInfo;
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000038 class TargetLoweringObjectFile;
Owen Andersoncb371882008-08-21 00:14:44 +000039 class raw_ostream;
Chris Lattner5fe575f2009-07-27 05:32:16 +000040 class SectionKind;
Chris Lattnerf26e03b2009-07-31 17:42:42 +000041 class MCContext;
Dan Gohman11112e02010-08-07 00:43:20 +000042 class TargetMachine;
Chris Lattner32018152005-06-27 06:28:45 +000043
Bruno Cardoso Lopes115934e2009-07-16 06:26:41 +000044 typedef std::vector<ELFSym*>::iterator ELFSymIter;
45 typedef std::vector<ELFSection*>::iterator ELFSectionIter;
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000046 typedef SetVector<const GlobalValue*>::const_iterator PendingGblsIter;
47 typedef SetVector<const char *>::const_iterator PendingExtsIter;
Bruno Cardoso Lopes64a6b392009-08-10 03:32:40 +000048 typedef std::pair<const Constant *, int64_t> CstExprResTy;
Bruno Cardoso Lopes115934e2009-07-16 06:26:41 +000049
Chris Lattner32018152005-06-27 06:28:45 +000050 /// ELFWriter - This class implements the common target-independent code for
51 /// writing ELF files. Targets should derive a class from this to
52 /// parameterize the output format.
53 ///
54 class ELFWriter : public MachineFunctionPass {
Chris Lattner6871aed2005-07-11 05:15:32 +000055 friend class ELFCodeEmitter;
Reid Kleckner27632172009-09-20 23:52:43 +000056 friend class JITDebugRegisterer;
Chris Lattner6871aed2005-07-11 05:15:32 +000057 public:
Devang Patel19974732007-05-03 01:11:54 +000058 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000059
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000060 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
61 ObjectCodeEmitter *getObjectCodeEmitter() {
62 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
Chris Lattner6871aed2005-07-11 05:15:32 +000063 }
64
Owen Andersoncb371882008-08-21 00:14:44 +000065 ELFWriter(raw_ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000066 ~ELFWriter();
67
Chris Lattner32018152005-06-27 06:28:45 +000068 protected:
Chris Lattner32018152005-06-27 06:28:45 +000069 /// Output stream to send the resultant object file to.
Owen Andersoncb371882008-08-21 00:14:44 +000070 raw_ostream &O;
Chris Lattner32018152005-06-27 06:28:45 +000071
72 /// Target machine description.
Chris Lattner32018152005-06-27 06:28:45 +000073 TargetMachine &TM;
74
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000075 /// Context object for machine code objects.
Chris Lattnerf26e03b2009-07-31 17:42:42 +000076 MCContext &OutContext;
77
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000078 /// Target Elf Writer description.
79 const TargetELFWriterInfo *TEW;
80
Chris Lattner75bbdff2005-07-11 03:11:10 +000081 /// Mang - The object used to perform name mangling for this module.
Chris Lattner75bbdff2005-07-11 03:11:10 +000082 Mangler *Mang;
83
Chris Lattner6871aed2005-07-11 05:15:32 +000084 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
85 /// code for functions to the .o file.
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000086 ELFCodeEmitter *ElfCE;
Chris Lattner6871aed2005-07-11 05:15:32 +000087
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000088 /// TLOF - Target Lowering Object File, provide section names for globals
89 /// and other object file specific stuff
90 const TargetLoweringObjectFile &TLOF;
91
Chris Lattner33adcfb2009-08-22 21:43:10 +000092 /// MAI - Target Asm Info, provide information about section names for
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000093 /// globals and other target specific stuff.
Chris Lattner33adcfb2009-08-22 21:43:10 +000094 const MCAsmInfo *MAI;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000095
Chris Lattner32018152005-06-27 06:28:45 +000096 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000097 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000098 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000099
100 /// is64Bit/isLittleEndian - This information is inferred from the target
101 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
102 bool is64Bit, isLittleEndian;
103
104 /// doInitialization - Emit the file header and all of the global variables
105 /// for the module to the ELF file.
106 bool doInitialization(Module &M);
Chris Lattner32018152005-06-27 06:28:45 +0000107 bool runOnMachineFunction(MachineFunction &MF);
108
Chris Lattner32018152005-06-27 06:28:45 +0000109 /// doFinalization - Now that the module has been completely processed, emit
110 /// the ELF file to 'O'.
111 bool doFinalization(Module &M);
112
113 private:
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000114 /// Blob containing the Elf header
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000115 BinaryObject ElfHdr;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000116
Chris Lattner32018152005-06-27 06:28:45 +0000117 /// SectionList - This is the list of sections that we have emitted to the
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000118 /// file. Once the file has been completely built, the section header table
Chris Lattner32018152005-06-27 06:28:45 +0000119 /// is constructed from this info.
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000120 std::vector<ELFSection*> SectionList;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000121 unsigned NumSections; // Always = SectionList.size()
122
123 /// SectionLookup - This is a mapping from section name to section number in
Chris Lattner33adcfb2009-08-22 21:43:10 +0000124 /// the SectionList. Used to quickly gather the Section Index from MAI names
Chris Lattner5f48ff72005-07-16 08:01:13 +0000125 std::map<std::string, ELFSection*> SectionLookup;
126
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000127 /// PendingGlobals - Globals not processed as symbols yet.
128 SetVector<const GlobalValue*> PendingGlobals;
129
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000130 /// GblSymLookup - This is a mapping from global value to a symbol index
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000131 /// in the symbol table or private symbols list. This is useful since reloc
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000132 /// symbol references must be quickly mapped to their indices on the lists.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000133 std::map<const GlobalValue*, uint32_t> GblSymLookup;
134
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000135 /// PendingExternals - Externals not processed as symbols yet.
136 SetVector<const char *> PendingExternals;
137
138 /// ExtSymLookup - This is a mapping from externals to a symbol index
139 /// in the symbol table list. This is useful since reloc symbol references
140 /// must be quickly mapped to their symbol table indices.
141 std::map<const char *, uint32_t> ExtSymLookup;
142
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000143 /// SymbolList - This is the list of symbols emitted to the symbol table.
144 /// When the SymbolList is finally built, local symbols must be placed in
145 /// the beginning while non-locals at the end.
146 std::vector<ELFSym*> SymbolList;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000147
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000148 /// PrivateSyms - Record private symbols, every symbol here must never be
149 /// present in the SymbolList.
150 std::vector<ELFSym*> PrivateSyms;
151
Chris Lattner5f48ff72005-07-16 08:01:13 +0000152 /// getSection - Return the section with the specified name, creating a new
153 /// section if one does not already exist.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000154 ELFSection &getSection(const std::string &Name, unsigned Type,
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000155 unsigned Flags = 0, unsigned Align = 0) {
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000156 ELFSection *&SN = SectionLookup[Name];
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000157 if (SN) return *SN;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000158
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000159 SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000160 SN = SectionList.back();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000161 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000162 SN->Type = Type;
163 SN->Flags = Flags;
Eli Friedman10bb4212010-07-16 07:53:29 +0000164 SN->Link = ELF::SHN_UNDEF;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000165 SN->Align = Align;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000166 return *SN;
167 }
Chris Lattner32018152005-06-27 06:28:45 +0000168
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000169 ELFSection &getNonExecStackSection() {
Eli Friedman10bb4212010-07-16 07:53:29 +0000170 return getSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0, 1);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000171 }
172
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000173 ELFSection &getSymbolTableSection() {
Eli Friedman10bb4212010-07-16 07:53:29 +0000174 return getSection(".symtab", ELF::SHT_SYMTAB, 0);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000175 }
176
177 ELFSection &getStringTableSection() {
Eli Friedman10bb4212010-07-16 07:53:29 +0000178 return getSection(".strtab", ELF::SHT_STRTAB, 0, 1);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000179 }
180
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000181 ELFSection &getSectionHeaderStringTableSection() {
Eli Friedman10bb4212010-07-16 07:53:29 +0000182 return getSection(".shstrtab", ELF::SHT_STRTAB, 0, 1);
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000183 }
184
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000185 ELFSection &getNullSection() {
Eli Friedman10bb4212010-07-16 07:53:29 +0000186 return getSection("", ELF::SHT_NULL, 0);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000187 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000188
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000189 ELFSection &getDataSection();
190 ELFSection &getBSSSection();
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000191 ELFSection &getCtorSection();
192 ELFSection &getDtorSection();
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000193 ELFSection &getJumpTableSection();
194 ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
Dan Gohmanae541aa2010-04-15 04:33:49 +0000195 ELFSection &getTextSection(const Function *F);
Bruno Cardoso Lopes0a38dc52009-07-20 08:52:02 +0000196 ELFSection &getRelocSection(ELFSection &S);
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000197
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000198 // Helpers for obtaining ELF specific info.
Bruno Cardoso Lopesd2910662009-07-13 22:40:39 +0000199 unsigned getGlobalELFBinding(const GlobalValue *GV);
200 unsigned getGlobalELFType(const GlobalValue *GV);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000201 unsigned getGlobalELFVisibility(const GlobalValue *GV);
202
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000203 // AddPendingGlobalSymbol - Add a global to be processed and to
204 // the global symbol lookup, use a zero index because the table
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000205 // index will be determined later.
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000206 void AddPendingGlobalSymbol(const GlobalValue *GV,
207 bool AddToLookup = false);
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000208
Bruno Cardoso Lopesd163e8b2009-08-13 21:25:27 +0000209 // AddPendingExternalSymbol - Add the external to be processed
210 // and to the external symbol lookup, use a zero index because
211 // the symbol table index will be determined later.
212 void AddPendingExternalSymbol(const char *External);
213
214 // AddToSymbolList - Update the symbol lookup and If the symbol is
215 // private add it to PrivateSyms list, otherwise to SymbolList.
216 void AddToSymbolList(ELFSym *GblSym);
Bruno Cardoso Lopes68491c12009-07-21 06:51:32 +0000217
Chris Lattner5f48ff72005-07-16 08:01:13 +0000218 // As we complete the ELF file, we need to update fields in the ELF header
219 // (e.g. the location of the section table). These members keep track of
220 // the offset in ELFHeader of these various pieces to update and other
221 // locations in the file.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000222 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
223 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
224 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000225
Chris Lattner32018152005-06-27 06:28:45 +0000226 private:
Bruno Cardoso Lopesd2910662009-07-13 22:40:39 +0000227 void EmitGlobal(const GlobalValue *GV);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000228 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000229 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000230 ELFSection &GblS);
Bruno Cardoso Lopes3e0094d2009-08-08 17:29:04 +0000231 void EmitGlobalConstantLargeInt(const ConstantInt *CI, ELFSection &S);
232 void EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size,
Bruno Cardoso Lopes64a6b392009-08-10 03:32:40 +0000233 ELFSection &GblS, int64_t Offset = 0);
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000234 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
235 void EmitXXStructorList(Constant *List, ELFSection &Xtor);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000236 void EmitRelocations();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000237 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000238 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Chris Lattner32018152005-06-27 06:28:45 +0000239 void EmitSectionTableStringTable();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000240 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000241 void EmitSymbolTable();
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000242 void EmitStringTable(const std::string &ModuleName);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000243 void OutputSectionsAndSectionTable();
Bruno Cardoso Lopes0a38dc52009-07-20 08:52:02 +0000244 void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
245 unsigned Size);
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000246 unsigned SortSymbols();
Bruno Cardoso Lopes64a6b392009-08-10 03:32:40 +0000247 CstExprResTy ResolveConstantExpr(const Constant *CV);
Chris Lattner32018152005-06-27 06:28:45 +0000248 };
249}
250
251#endif