blob: fe726524d32e2bd0ec3f95d187ecfb77778a4b4a [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 Lopesc997d452009-06-11 19:16:03 +000024 class ConstantStruct;
25 class ELFCodeEmitter;
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000026 class ELFRelocation;
27 class ELFSection;
Daniel Dunbar89e12a12009-07-13 06:00:13 +000028 struct ELFSym;
Chris Lattner32018152005-06-27 06:28:45 +000029 class GlobalVariable;
Chris Lattner75bbdff2005-07-11 03:11:10 +000030 class Mangler;
Chris Lattner6871aed2005-07-11 05:15:32 +000031 class MachineCodeEmitter;
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +000032 class MachineConstantPoolEntry;
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000033 class ObjectCodeEmitter;
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000034 class TargetAsmInfo;
35 class TargetELFWriterInfo;
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000036 class TargetLoweringObjectFile;
Owen Andersoncb371882008-08-21 00:14:44 +000037 class raw_ostream;
Chris Lattner5fe575f2009-07-27 05:32:16 +000038 class SectionKind;
Chris Lattnerf26e03b2009-07-31 17:42:42 +000039 class MCContext;
Chris Lattner32018152005-06-27 06:28:45 +000040
Bruno Cardoso Lopes115934e2009-07-16 06:26:41 +000041 typedef std::vector<ELFSym*>::iterator ELFSymIter;
42 typedef std::vector<ELFSection*>::iterator ELFSectionIter;
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +000043 typedef SetVector<const GlobalValue*>::const_iterator PendingGblsIter;
44 typedef SetVector<const char *>::const_iterator PendingExtsIter;
Bruno Cardoso Lopes115934e2009-07-16 06:26:41 +000045
Chris Lattner32018152005-06-27 06:28:45 +000046 /// ELFWriter - This class implements the common target-independent code for
47 /// writing ELF files. Targets should derive a class from this to
48 /// parameterize the output format.
49 ///
50 class ELFWriter : public MachineFunctionPass {
Chris Lattner6871aed2005-07-11 05:15:32 +000051 friend class ELFCodeEmitter;
52 public:
Devang Patel19974732007-05-03 01:11:54 +000053 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000054
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000055 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
56 ObjectCodeEmitter *getObjectCodeEmitter() {
57 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
Chris Lattner6871aed2005-07-11 05:15:32 +000058 }
59
Owen Andersoncb371882008-08-21 00:14:44 +000060 ELFWriter(raw_ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000061 ~ELFWriter();
62
Chris Lattner32018152005-06-27 06:28:45 +000063 protected:
Chris Lattner32018152005-06-27 06:28:45 +000064 /// Output stream to send the resultant object file to.
Owen Andersoncb371882008-08-21 00:14:44 +000065 raw_ostream &O;
Chris Lattner32018152005-06-27 06:28:45 +000066
67 /// Target machine description.
Chris Lattner32018152005-06-27 06:28:45 +000068 TargetMachine &TM;
69
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000070 /// Context object for machine code objects.
Chris Lattnerf26e03b2009-07-31 17:42:42 +000071 MCContext &OutContext;
72
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000073 /// Target Elf Writer description.
74 const TargetELFWriterInfo *TEW;
75
Chris Lattner75bbdff2005-07-11 03:11:10 +000076 /// Mang - The object used to perform name mangling for this module.
Chris Lattner75bbdff2005-07-11 03:11:10 +000077 Mangler *Mang;
78
Chris Lattner6871aed2005-07-11 05:15:32 +000079 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
80 /// code for functions to the .o file.
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000081 ELFCodeEmitter *ElfCE;
Chris Lattner6871aed2005-07-11 05:15:32 +000082
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +000083 /// TLOF - Target Lowering Object File, provide section names for globals
84 /// and other object file specific stuff
85 const TargetLoweringObjectFile &TLOF;
86
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000087 /// TAI - Target Asm Info, provide information about section names for
88 /// globals and other target specific stuff.
89 const TargetAsmInfo *TAI;
90
Chris Lattner32018152005-06-27 06:28:45 +000091 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000092 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000093 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000094
95 /// is64Bit/isLittleEndian - This information is inferred from the target
96 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
97 bool is64Bit, isLittleEndian;
98
99 /// doInitialization - Emit the file header and all of the global variables
100 /// for the module to the ELF file.
101 bool doInitialization(Module &M);
Chris Lattner32018152005-06-27 06:28:45 +0000102 bool runOnMachineFunction(MachineFunction &MF);
103
Chris Lattner32018152005-06-27 06:28:45 +0000104 /// doFinalization - Now that the module has been completely processed, emit
105 /// the ELF file to 'O'.
106 bool doFinalization(Module &M);
107
108 private:
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000109 /// Blob containing the Elf header
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000110 BinaryObject ElfHdr;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000111
Chris Lattner32018152005-06-27 06:28:45 +0000112 /// SectionList - This is the list of sections that we have emitted to the
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000113 /// file. Once the file has been completely built, the section header table
Chris Lattner32018152005-06-27 06:28:45 +0000114 /// is constructed from this info.
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000115 std::vector<ELFSection*> SectionList;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000116 unsigned NumSections; // Always = SectionList.size()
117
118 /// SectionLookup - This is a mapping from section name to section number in
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000119 /// the SectionList. Used to quickly gather the Section Index from TAI names
Chris Lattner5f48ff72005-07-16 08:01:13 +0000120 std::map<std::string, ELFSection*> SectionLookup;
121
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000122 /// PendingGlobals - Globals not processed as symbols yet.
123 SetVector<const GlobalValue*> PendingGlobals;
124
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000125 /// GblSymLookup - This is a mapping from global value to a symbol index
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000126 /// in the symbol table or private symbols list. This is useful since reloc
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000127 /// symbol references must be quickly mapped to their indices on the lists.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000128 std::map<const GlobalValue*, uint32_t> GblSymLookup;
129
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000130 /// PendingExternals - Externals not processed as symbols yet.
131 SetVector<const char *> PendingExternals;
132
133 /// ExtSymLookup - This is a mapping from externals to a symbol index
134 /// in the symbol table list. This is useful since reloc symbol references
135 /// must be quickly mapped to their symbol table indices.
136 std::map<const char *, uint32_t> ExtSymLookup;
137
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000138 /// SymbolList - This is the list of symbols emitted to the symbol table.
139 /// When the SymbolList is finally built, local symbols must be placed in
140 /// the beginning while non-locals at the end.
141 std::vector<ELFSym*> SymbolList;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000142
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000143 /// PrivateSyms - Record private symbols, every symbol here must never be
144 /// present in the SymbolList.
145 std::vector<ELFSym*> PrivateSyms;
146
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000147 // Remove tab from section name prefix. This is necessary becase TAI
148 // sometimes return a section name prefixed with elf unused chars. This is
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000149 // a little bit dirty. FIXME: find a better approach, maybe add more
150 // methods to TAI to get the clean name?
151 void fixNameForSection(std::string &Name) {
152 size_t Pos = Name.find("\t");
153 if (Pos != std::string::npos)
154 Name.erase(Pos, 1);
155
156 Pos = Name.find(".section ");
157 if (Pos != std::string::npos)
158 Name.erase(Pos, 9);
159
160 Pos = Name.find("\n");
161 if (Pos != std::string::npos)
162 Name.erase(Pos, 1);
163 }
164
Chris Lattner5f48ff72005-07-16 08:01:13 +0000165 /// getSection - Return the section with the specified name, creating a new
166 /// section if one does not already exist.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000167 ELFSection &getSection(const std::string &Name, unsigned Type,
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000168 unsigned Flags = 0, unsigned Align = 0) {
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000169 std::string SName(Name);
170 fixNameForSection(SName);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000171
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000172 ELFSection *&SN = SectionLookup[SName];
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000173 if (SN) return *SN;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000174
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000175 SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit));
176 SN = SectionList.back();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000177 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000178 SN->Type = Type;
179 SN->Flags = Flags;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000180 SN->Link = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000181 SN->Align = Align;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000182 return *SN;
183 }
Chris Lattner32018152005-06-27 06:28:45 +0000184
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000185 ELFSection &getNonExecStackSection() {
186 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
187 }
188
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000189 ELFSection &getSymbolTableSection() {
190 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
191 }
192
193 ELFSection &getStringTableSection() {
194 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
195 }
196
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000197 ELFSection &getSectionHeaderStringTableSection() {
198 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
199 }
200
Chris Lattner56c32612005-07-16 17:40:34 +0000201 ELFSection &getDataSection() {
202 return getSection(".data", ELFSection::SHT_PROGBITS,
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000203 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Chris Lattner56c32612005-07-16 17:40:34 +0000204 }
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000205
Chris Lattner56c32612005-07-16 17:40:34 +0000206 ELFSection &getBSSSection() {
207 return getSection(".bss", ELFSection::SHT_NOBITS,
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000208 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Chris Lattner56c32612005-07-16 17:40:34 +0000209 }
210
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000211 ELFSection &getNullSection() {
212 return getSection("", ELFSection::SHT_NULL, 0);
213 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000214
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000215 ELFSection &getCtorSection();
216 ELFSection &getDtorSection();
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000217 ELFSection &getJumpTableSection();
218 ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000219 ELFSection &getTextSection(Function *F);
Bruno Cardoso Lopes0a38dc52009-07-20 08:52:02 +0000220 ELFSection &getRelocSection(ELFSection &S);
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000221
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000222 // Helpers for obtaining ELF specific info.
Bruno Cardoso Lopesd2910662009-07-13 22:40:39 +0000223 unsigned getGlobalELFBinding(const GlobalValue *GV);
224 unsigned getGlobalELFType(const GlobalValue *GV);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000225 unsigned getGlobalELFVisibility(const GlobalValue *GV);
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000226 unsigned getElfSectionFlags(SectionKind Kind, bool IsAlloc = true);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000227
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000228 // addGlobalSymbol - Add a global to be processed and to
229 // the global symbol lookup, use a zero index because the table
230 // index will be determined later.
231 void addGlobalSymbol(const GlobalValue *GV, bool AddToLookup = false);
232
Bruno Cardoso Lopes746e3bb2009-07-27 18:54:47 +0000233 // addExternalSymbol - Add the external to be processed and to the
234 // external symbol lookup, use a zero index because the symbol
235 // table index will be determined later
236 void addExternalSymbol(const char *External);
Bruno Cardoso Lopes68491c12009-07-21 06:51:32 +0000237
Chris Lattner5f48ff72005-07-16 08:01:13 +0000238 // As we complete the ELF file, we need to update fields in the ELF header
239 // (e.g. the location of the section table). These members keep track of
240 // the offset in ELFHeader of these various pieces to update and other
241 // locations in the file.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000242 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
243 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
244 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000245
Chris Lattner32018152005-06-27 06:28:45 +0000246 private:
Bruno Cardoso Lopesd2910662009-07-13 22:40:39 +0000247 void EmitGlobal(const GlobalValue *GV);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000248 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000249 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000250 ELFSection &GblS);
Bruno Cardoso Lopes52d08512009-08-05 06:57:03 +0000251 void emitGlobalDataRelocation(const GlobalValue *GV, unsigned Size,
252 ELFSection &GblS);
253 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
254 void EmitXXStructorList(Constant *List, ELFSection &Xtor);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000255 void EmitRelocations();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000256 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000257 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Chris Lattner32018152005-06-27 06:28:45 +0000258 void EmitSectionTableStringTable();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000259 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000260 void EmitSymbolTable();
Bruno Cardoso Lopesdf0b6502009-07-27 19:32:57 +0000261 void EmitStringTable(const std::string &ModuleName);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000262 void OutputSectionsAndSectionTable();
Bruno Cardoso Lopes0a38dc52009-07-20 08:52:02 +0000263 void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
264 unsigned Size);
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000265 unsigned SortSymbols();
Chris Lattner32018152005-06-27 06:28:45 +0000266 };
267}
268
269#endif