blob: 34f82766c5863eb65b5394575e989f0538e9589a [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;
Owen Andersoncb371882008-08-21 00:14:44 +000036 class raw_ostream;
Chris Lattner32018152005-06-27 06:28:45 +000037
Bruno Cardoso Lopes115934e2009-07-16 06:26:41 +000038 typedef std::vector<ELFSym*>::iterator ELFSymIter;
39 typedef std::vector<ELFSection*>::iterator ELFSectionIter;
40
Chris Lattner32018152005-06-27 06:28:45 +000041 /// ELFWriter - This class implements the common target-independent code for
42 /// writing ELF files. Targets should derive a class from this to
43 /// parameterize the output format.
44 ///
45 class ELFWriter : public MachineFunctionPass {
Chris Lattner6871aed2005-07-11 05:15:32 +000046 friend class ELFCodeEmitter;
47 public:
Devang Patel19974732007-05-03 01:11:54 +000048 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000049
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000050 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
51 ObjectCodeEmitter *getObjectCodeEmitter() {
52 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
Chris Lattner6871aed2005-07-11 05:15:32 +000053 }
54
Owen Andersoncb371882008-08-21 00:14:44 +000055 ELFWriter(raw_ostream &O, TargetMachine &TM);
Chris Lattner6871aed2005-07-11 05:15:32 +000056 ~ELFWriter();
57
Chris Lattner32018152005-06-27 06:28:45 +000058 protected:
Chris Lattner32018152005-06-27 06:28:45 +000059 /// Output stream to send the resultant object file to.
Owen Andersoncb371882008-08-21 00:14:44 +000060 raw_ostream &O;
Chris Lattner32018152005-06-27 06:28:45 +000061
62 /// Target machine description.
Chris Lattner32018152005-06-27 06:28:45 +000063 TargetMachine &TM;
64
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000065 /// Target Elf Writer description.
66 const TargetELFWriterInfo *TEW;
67
Chris Lattner75bbdff2005-07-11 03:11:10 +000068 /// Mang - The object used to perform name mangling for this module.
Chris Lattner75bbdff2005-07-11 03:11:10 +000069 Mangler *Mang;
70
Chris Lattner6871aed2005-07-11 05:15:32 +000071 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
72 /// code for functions to the .o file.
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000073 ELFCodeEmitter *ElfCE;
Chris Lattner6871aed2005-07-11 05:15:32 +000074
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000075 /// TAI - Target Asm Info, provide information about section names for
76 /// globals and other target specific stuff.
77 const TargetAsmInfo *TAI;
78
Chris Lattner32018152005-06-27 06:28:45 +000079 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000080 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000081 //===------------------------------------------------------------------===//
Chris Lattner32018152005-06-27 06:28:45 +000082
83 /// is64Bit/isLittleEndian - This information is inferred from the target
84 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
85 bool is64Bit, isLittleEndian;
86
87 /// doInitialization - Emit the file header and all of the global variables
88 /// for the module to the ELF file.
89 bool doInitialization(Module &M);
Chris Lattner32018152005-06-27 06:28:45 +000090 bool runOnMachineFunction(MachineFunction &MF);
91
Chris Lattner32018152005-06-27 06:28:45 +000092 /// doFinalization - Now that the module has been completely processed, emit
93 /// the ELF file to 'O'.
94 bool doFinalization(Module &M);
95
96 private:
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +000097 /// Blob containing the Elf header
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000098 BinaryObject ElfHdr;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000099
Chris Lattner32018152005-06-27 06:28:45 +0000100 /// SectionList - This is the list of sections that we have emitted to the
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000101 /// file. Once the file has been completely built, the section header table
Chris Lattner32018152005-06-27 06:28:45 +0000102 /// is constructed from this info.
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000103 std::vector<ELFSection*> SectionList;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000104 unsigned NumSections; // Always = SectionList.size()
105
106 /// SectionLookup - This is a mapping from section name to section number in
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000107 /// the SectionList. Used to quickly gather the Section Index from TAI names
Chris Lattner5f48ff72005-07-16 08:01:13 +0000108 std::map<std::string, ELFSection*> SectionLookup;
109
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000110 /// GblSymLookup - This is a mapping from global value to a symbol index
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000111 /// in the symbol table or private symbols list. This is useful since reloc
112 /// symbol references must be quickly mapped to their indices on the lists
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000113 std::map<const GlobalValue*, uint32_t> GblSymLookup;
114
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000115 /// SymbolList - This is the list of symbols emitted to the symbol table.
116 /// When the SymbolList is finally built, local symbols must be placed in
117 /// the beginning while non-locals at the end.
118 std::vector<ELFSym*> SymbolList;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000119
Bruno Cardoso Lopes171375f2009-07-18 19:30:09 +0000120 /// PrivateSyms - Record private symbols, every symbol here must never be
121 /// present in the SymbolList.
122 std::vector<ELFSym*> PrivateSyms;
123
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000124 /// PendingGlobals - List of externally defined symbols that we have been
125 /// asked to emit, but have not seen a reference to. When a reference
126 /// is seen, the symbol will move from this list to the SymbolList.
127 SetVector<GlobalValue*> PendingGlobals;
128
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000129 // Remove tab from section name prefix. This is necessary becase TAI
130 // sometimes return a section name prefixed with elf unused chars. This is
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000131 // a little bit dirty. FIXME: find a better approach, maybe add more
132 // methods to TAI to get the clean name?
133 void fixNameForSection(std::string &Name) {
134 size_t Pos = Name.find("\t");
135 if (Pos != std::string::npos)
136 Name.erase(Pos, 1);
137
138 Pos = Name.find(".section ");
139 if (Pos != std::string::npos)
140 Name.erase(Pos, 9);
141
142 Pos = Name.find("\n");
143 if (Pos != std::string::npos)
144 Name.erase(Pos, 1);
145 }
146
Chris Lattner5f48ff72005-07-16 08:01:13 +0000147 /// getSection - Return the section with the specified name, creating a new
148 /// section if one does not already exist.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000149 ELFSection &getSection(const std::string &Name, unsigned Type,
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000150 unsigned Flags = 0, unsigned Align = 0) {
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000151 std::string SName(Name);
152 fixNameForSection(SName);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000153
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000154 ELFSection *&SN = SectionLookup[SName];
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000155 if (SN) return *SN;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000156
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000157 SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit));
158 SN = SectionList.back();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000159 SN->SectionIdx = NumSections++;
Chris Lattner5f9cb592005-07-16 17:35:26 +0000160 SN->Type = Type;
161 SN->Flags = Flags;
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000162 SN->Link = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000163 SN->Align = Align;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000164 return *SN;
165 }
Chris Lattner32018152005-06-27 06:28:45 +0000166
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000167 /// TODO: support mangled names here to emit the right .text section
168 /// for c++ object files.
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000169 ELFSection &getTextSection() {
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000170 return getSection(".text", ELFSection::SHT_PROGBITS,
171 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes5d419102009-06-05 00:22:10 +0000172 }
173
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000174 ELFSection &getNonExecStackSection() {
175 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
176 }
177
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000178 ELFSection &getSymbolTableSection() {
179 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
180 }
181
182 ELFSection &getStringTableSection() {
183 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
184 }
185
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000186 ELFSection &getSectionHeaderStringTableSection() {
187 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
188 }
189
Chris Lattner56c32612005-07-16 17:40:34 +0000190 ELFSection &getDataSection() {
191 return getSection(".data", ELFSection::SHT_PROGBITS,
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000192 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Chris Lattner56c32612005-07-16 17:40:34 +0000193 }
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000194
Chris Lattner56c32612005-07-16 17:40:34 +0000195 ELFSection &getBSSSection() {
196 return getSection(".bss", ELFSection::SHT_NOBITS,
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000197 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Chris Lattner56c32612005-07-16 17:40:34 +0000198 }
199
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000200 ELFSection &getNullSection() {
201 return getSection("", ELFSection::SHT_NULL, 0);
202 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000203
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000204 ELFSection &getJumpTableSection();
205 ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
Bruno Cardoso Lopes0a38dc52009-07-20 08:52:02 +0000206 ELFSection &getRelocSection(ELFSection &S);
Bruno Cardoso Lopese2b0ecd2009-07-18 23:24:01 +0000207
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000208 // Helpers for obtaining ELF specific info.
Bruno Cardoso Lopesd2910662009-07-13 22:40:39 +0000209 unsigned getGlobalELFBinding(const GlobalValue *GV);
210 unsigned getGlobalELFType(const GlobalValue *GV);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000211 unsigned getGlobalELFVisibility(const GlobalValue *GV);
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000212 unsigned getElfSectionFlags(unsigned Flags);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000213
Chris Lattner5f48ff72005-07-16 08:01:13 +0000214 // As we complete the ELF file, we need to update fields in the ELF header
215 // (e.g. the location of the section table). These members keep track of
216 // the offset in ELFHeader of these various pieces to update and other
217 // locations in the file.
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000218 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
219 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
220 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000221
Chris Lattner32018152005-06-27 06:28:45 +0000222 private:
Bruno Cardoso Lopesd2910662009-07-13 22:40:39 +0000223 void EmitGlobal(const GlobalValue *GV);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000224 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000225 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000226 ELFSection &GblS);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000227 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000228 void EmitRelocations();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000229 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000230 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Chris Lattner32018152005-06-27 06:28:45 +0000231 void EmitSectionTableStringTable();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000232 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000233 void EmitSymbolTable();
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000234 void EmitStringTable();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000235 void OutputSectionsAndSectionTable();
Bruno Cardoso Lopes0a38dc52009-07-20 08:52:02 +0000236 void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
237 unsigned Size);
Bruno Cardoso Lopes4b70fab2009-07-15 20:49:10 +0000238 unsigned SortSymbols();
Chris Lattner32018152005-06-27 06:28:45 +0000239 };
240}
241
242#endif