blob: 2f8f625d96723d3cda0a3c6f80462a9747d258a0 [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
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000017#include "llvm/ADT/SetVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
Dan Gohman249ddbf2008-03-21 23:51:57 +000019#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020
21namespace llvm {
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000022 class BinaryObject;
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +000023 class Constant;
Bruno Cardoso Lopesd5b21c12009-08-08 17:29:04 +000024 class ConstantInt;
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000025 class ConstantStruct;
26 class ELFCodeEmitter;
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000027 class ELFRelocation;
28 class ELFSection;
Daniel Dunbard653ac82009-07-13 06:00:13 +000029 struct ELFSym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 class GlobalVariable;
31 class Mangler;
32 class MachineCodeEmitter;
Bruno Cardoso Lopes2c1fe1a2009-07-18 23:24:01 +000033 class MachineConstantPoolEntry;
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000034 class ObjectCodeEmitter;
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +000035 class TargetAsmInfo;
36 class TargetELFWriterInfo;
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +000037 class TargetLoweringObjectFile;
Owen Anderson847b99b2008-08-21 00:14:44 +000038 class raw_ostream;
Chris Lattnerd8310522009-07-27 05:32:16 +000039 class SectionKind;
Chris Lattner01316272009-07-31 17:42:42 +000040 class MCContext;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Bruno Cardoso Lopesf4f4dcd2009-07-16 06:26:41 +000042 typedef std::vector<ELFSym*>::iterator ELFSymIter;
43 typedef std::vector<ELFSection*>::iterator ELFSectionIter;
Bruno Cardoso Lopes5436d9f2009-07-27 18:54:47 +000044 typedef SetVector<const GlobalValue*>::const_iterator PendingGblsIter;
45 typedef SetVector<const char *>::const_iterator PendingExtsIter;
Bruno Cardoso Lopes63c4e062009-08-10 03:32:40 +000046 typedef std::pair<const Constant *, int64_t> CstExprResTy;
Bruno Cardoso Lopesf4f4dcd2009-07-16 06:26:41 +000047
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 /// ELFWriter - This class implements the common target-independent code for
49 /// writing ELF files. Targets should derive a class from this to
50 /// parameterize the output format.
51 ///
52 class ELFWriter : public MachineFunctionPass {
53 friend class ELFCodeEmitter;
54 public:
55 static char ID;
56
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000057 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
58 ObjectCodeEmitter *getObjectCodeEmitter() {
59 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 }
61
Owen Anderson847b99b2008-08-21 00:14:44 +000062 ELFWriter(raw_ostream &O, TargetMachine &TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 ~ELFWriter();
64
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 protected:
66 /// Output stream to send the resultant object file to.
Owen Anderson847b99b2008-08-21 00:14:44 +000067 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
69 /// Target machine description.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 TargetMachine &TM;
71
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +000072 /// Context object for machine code objects.
Chris Lattner01316272009-07-31 17:42:42 +000073 MCContext &OutContext;
74
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000075 /// Target Elf Writer description.
76 const TargetELFWriterInfo *TEW;
77
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 /// Mang - The object used to perform name mangling for this module.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 Mangler *Mang;
80
81 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
82 /// code for functions to the .o file.
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000083 ELFCodeEmitter *ElfCE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +000085 /// TLOF - Target Lowering Object File, provide section names for globals
86 /// and other object file specific stuff
87 const TargetLoweringObjectFile &TLOF;
88
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000089 /// TAI - Target Asm Info, provide information about section names for
90 /// globals and other target specific stuff.
91 const TargetAsmInfo *TAI;
92
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000095 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 /// is64Bit/isLittleEndian - This information is inferred from the target
98 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
99 bool is64Bit, isLittleEndian;
100
101 /// doInitialization - Emit the file header and all of the global variables
102 /// for the module to the ELF file.
103 bool doInitialization(Module &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 bool runOnMachineFunction(MachineFunction &MF);
105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 /// doFinalization - Now that the module has been completely processed, emit
107 /// the ELF file to 'O'.
108 bool doFinalization(Module &M);
109
110 private:
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000111 /// Blob containing the Elf header
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000112 BinaryObject ElfHdr;
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 /// SectionList - This is the list of sections that we have emitted to the
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000115 /// file. Once the file has been completely built, the section header table
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 /// is constructed from this info.
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000117 std::vector<ELFSection*> SectionList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 unsigned NumSections; // Always = SectionList.size()
119
120 /// SectionLookup - This is a mapping from section name to section number in
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000121 /// the SectionList. Used to quickly gather the Section Index from TAI names
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 std::map<std::string, ELFSection*> SectionLookup;
123
Bruno Cardoso Lopes5436d9f2009-07-27 18:54:47 +0000124 /// PendingGlobals - Globals not processed as symbols yet.
125 SetVector<const GlobalValue*> PendingGlobals;
126
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000127 /// GblSymLookup - This is a mapping from global value to a symbol index
Bruno Cardoso Lopes8c203922009-07-18 19:30:09 +0000128 /// in the symbol table or private symbols list. This is useful since reloc
Bruno Cardoso Lopes5436d9f2009-07-27 18:54:47 +0000129 /// symbol references must be quickly mapped to their indices on the lists.
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000130 std::map<const GlobalValue*, uint32_t> GblSymLookup;
131
Bruno Cardoso Lopes5436d9f2009-07-27 18:54:47 +0000132 /// PendingExternals - Externals not processed as symbols yet.
133 SetVector<const char *> PendingExternals;
134
135 /// ExtSymLookup - This is a mapping from externals to a symbol index
136 /// in the symbol table list. This is useful since reloc symbol references
137 /// must be quickly mapped to their symbol table indices.
138 std::map<const char *, uint32_t> ExtSymLookup;
139
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000140 /// SymbolList - This is the list of symbols emitted to the symbol table.
141 /// When the SymbolList is finally built, local symbols must be placed in
142 /// the beginning while non-locals at the end.
143 std::vector<ELFSym*> SymbolList;
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000144
Bruno Cardoso Lopes8c203922009-07-18 19:30:09 +0000145 /// PrivateSyms - Record private symbols, every symbol here must never be
146 /// present in the SymbolList.
147 std::vector<ELFSym*> PrivateSyms;
148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 /// getSection - Return the section with the specified name, creating a new
150 /// section if one does not already exist.
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000151 ELFSection &getSection(const std::string &Name, unsigned Type,
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000152 unsigned Flags = 0, unsigned Align = 0) {
Bruno Cardoso Lopesdfa78d12009-08-13 21:25:27 +0000153 ELFSection *&SN = SectionLookup[Name];
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000154 if (SN) return *SN;
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000155
Bruno Cardoso Lopesdfa78d12009-08-13 21:25:27 +0000156 SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000157 SN = SectionList.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 SN->SectionIdx = NumSections++;
159 SN->Type = Type;
160 SN->Flags = Flags;
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000161 SN->Link = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000162 SN->Align = Align;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 return *SN;
164 }
165
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000166 ELFSection &getNonExecStackSection() {
167 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
168 }
169
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000170 ELFSection &getSymbolTableSection() {
171 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
172 }
173
174 ELFSection &getStringTableSection() {
175 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
176 }
177
Bruno Cardoso Lopesb53d5c02009-06-23 04:39:27 +0000178 ELFSection &getSectionHeaderStringTableSection() {
179 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
180 }
181
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000182 ELFSection &getNullSection() {
183 return getSection("", ELFSection::SHT_NULL, 0);
184 }
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000185
Bruno Cardoso Lopesdfa78d12009-08-13 21:25:27 +0000186 ELFSection &getDataSection();
187 ELFSection &getBSSSection();
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +0000188 ELFSection &getCtorSection();
189 ELFSection &getDtorSection();
Bruno Cardoso Lopes2c1fe1a2009-07-18 23:24:01 +0000190 ELFSection &getJumpTableSection();
191 ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +0000192 ELFSection &getTextSection(Function *F);
Bruno Cardoso Lopesc4d2daf2009-07-20 08:52:02 +0000193 ELFSection &getRelocSection(ELFSection &S);
Bruno Cardoso Lopes2c1fe1a2009-07-18 23:24:01 +0000194
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000195 // Helpers for obtaining ELF specific info.
Bruno Cardoso Lopesaf939d82009-07-13 22:40:39 +0000196 unsigned getGlobalELFBinding(const GlobalValue *GV);
197 unsigned getGlobalELFType(const GlobalValue *GV);
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000198 unsigned getGlobalELFVisibility(const GlobalValue *GV);
199
Bruno Cardoso Lopesdfa78d12009-08-13 21:25:27 +0000200 // AddPendingGlobalSymbol - Add a global to be processed and to
201 // the global symbol lookup, use a zero index because the table
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +0000202 // index will be determined later.
Bruno Cardoso Lopesdfa78d12009-08-13 21:25:27 +0000203 void AddPendingGlobalSymbol(const GlobalValue *GV,
204 bool AddToLookup = false);
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +0000205
Bruno Cardoso Lopesdfa78d12009-08-13 21:25:27 +0000206 // AddPendingExternalSymbol - Add the external to be processed
207 // and to the external symbol lookup, use a zero index because
208 // the symbol table index will be determined later.
209 void AddPendingExternalSymbol(const char *External);
210
211 // AddToSymbolList - Update the symbol lookup and If the symbol is
212 // private add it to PrivateSyms list, otherwise to SymbolList.
213 void AddToSymbolList(ELFSym *GblSym);
Bruno Cardoso Lopes88ac2e42009-07-21 06:51:32 +0000214
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 // As we complete the ELF file, we need to update fields in the ELF header
216 // (e.g. the location of the section table). These members keep track of
217 // the offset in ELFHeader of these various pieces to update and other
218 // locations in the file.
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000219 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
220 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
221 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 private:
Bruno Cardoso Lopesaf939d82009-07-13 22:40:39 +0000224 void EmitGlobal(const GlobalValue *GV);
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000225 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000226 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000227 ELFSection &GblS);
Bruno Cardoso Lopesd5b21c12009-08-08 17:29:04 +0000228 void EmitGlobalConstantLargeInt(const ConstantInt *CI, ELFSection &S);
229 void EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size,
Bruno Cardoso Lopes63c4e062009-08-10 03:32:40 +0000230 ELFSection &GblS, int64_t Offset = 0);
Bruno Cardoso Lopesb4be2152009-08-05 06:57:03 +0000231 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
232 void EmitXXStructorList(Constant *List, ELFSection &Xtor);
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000233 void EmitRelocations();
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000234 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000235 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 void EmitSectionTableStringTable();
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000237 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000238 void EmitSymbolTable();
Bruno Cardoso Lopes2be60ac2009-07-27 19:32:57 +0000239 void EmitStringTable(const std::string &ModuleName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 void OutputSectionsAndSectionTable();
Bruno Cardoso Lopesc4d2daf2009-07-20 08:52:02 +0000241 void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
242 unsigned Size);
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000243 unsigned SortSymbols();
Bruno Cardoso Lopes63c4e062009-08-10 03:32:40 +0000244 CstExprResTy ResolveConstantExpr(const Constant *CV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 };
246}
247
248#endif