blob: 3cc50fe83697c1d6174f78ba28f0c02e5daf444d [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 Lopes8c25df12009-06-11 19:16:03 +000024 class ConstantStruct;
25 class ELFCodeEmitter;
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000026 class ELFRelocation;
27 class ELFSection;
Daniel Dunbard653ac82009-07-13 06:00:13 +000028 struct ELFSym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029 class GlobalVariable;
30 class Mangler;
31 class MachineCodeEmitter;
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000032 class ObjectCodeEmitter;
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +000033 class TargetAsmInfo;
34 class TargetELFWriterInfo;
Owen Anderson847b99b2008-08-21 00:14:44 +000035 class raw_ostream;
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 {
42 friend class ELFCodeEmitter;
43 public:
44 static char ID;
45
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000046 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
47 ObjectCodeEmitter *getObjectCodeEmitter() {
48 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 }
50
Owen Anderson847b99b2008-08-21 00:14:44 +000051 ELFWriter(raw_ostream &O, TargetMachine &TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 ~ELFWriter();
53
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 protected:
55 /// Output stream to send the resultant object file to.
Owen Anderson847b99b2008-08-21 00:14:44 +000056 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 /// Target machine description.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 TargetMachine &TM;
60
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000061 /// Target Elf Writer description.
62 const TargetELFWriterInfo *TEW;
63
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 /// Mang - The object used to perform name mangling for this module.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 Mangler *Mang;
66
67 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
68 /// code for functions to the .o file.
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000069 ELFCodeEmitter *ElfCE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000071 /// TAI - Target Asm Info, provide information about section names for
72 /// globals and other target specific stuff.
73 const TargetAsmInfo *TAI;
74
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000077 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 /// is64Bit/isLittleEndian - This information is inferred from the target
80 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
81 bool is64Bit, isLittleEndian;
82
83 /// doInitialization - Emit the file header and all of the global variables
84 /// for the module to the ELF file.
85 bool doInitialization(Module &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 bool runOnMachineFunction(MachineFunction &MF);
87
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 /// doFinalization - Now that the module has been completely processed, emit
89 /// the ELF file to 'O'.
90 bool doFinalization(Module &M);
91
92 private:
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +000093 /// Blob containing the Elf header
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000094 BinaryObject ElfHdr;
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000095
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 /// SectionList - This is the list of sections that we have emitted to the
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +000097 /// file. Once the file has been completely built, the section header table
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 /// is constructed from this info.
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +000099 std::vector<ELFSection*> SectionList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 unsigned NumSections; // Always = SectionList.size()
101
102 /// SectionLookup - This is a mapping from section name to section number in
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000103 /// the SectionList. Used to quickly gather the Section Index from TAI names
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 std::map<std::string, ELFSection*> SectionLookup;
105
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000106 /// GblSymLookup - This is a mapping from global value to a symbol index
107 /// in the symbol table. This is useful since relocations symbol references
108 /// must be quickly mapped to a symbol table index
109 std::map<const GlobalValue*, uint32_t> GblSymLookup;
110
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000111 /// SymbolList - This is the list of symbols emitted to the symbol table.
112 /// When the SymbolList is finally built, local symbols must be placed in
113 /// the beginning while non-locals at the end.
114 std::vector<ELFSym*> SymbolList;
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000115
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
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000121 // Remove tab from section name prefix. This is necessary becase TAI
122 // sometimes return a section name prefixed with elf unused chars. This is
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000123 // a little bit dirty. FIXME: find a better approach, maybe add more
124 // methods to TAI to get the clean name?
125 void fixNameForSection(std::string &Name) {
126 size_t Pos = Name.find("\t");
127 if (Pos != std::string::npos)
128 Name.erase(Pos, 1);
129
130 Pos = Name.find(".section ");
131 if (Pos != std::string::npos)
132 Name.erase(Pos, 9);
133
134 Pos = Name.find("\n");
135 if (Pos != std::string::npos)
136 Name.erase(Pos, 1);
137 }
138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 /// getSection - Return the section with the specified name, creating a new
140 /// section if one does not already exist.
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000141 ELFSection &getSection(const std::string &Name, unsigned Type,
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000142 unsigned Flags = 0, unsigned Align = 0) {
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000143 std::string SName(Name);
144 fixNameForSection(SName);
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000145
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000146 ELFSection *&SN = SectionLookup[SName];
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000147 if (SN) return *SN;
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000148
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000149 SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit));
150 SN = SectionList.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 SN->SectionIdx = NumSections++;
152 SN->Type = Type;
153 SN->Flags = Flags;
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000154 SN->Link = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000155 SN->Align = Align;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 return *SN;
157 }
158
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000159 /// TODO: support mangled names here to emit the right .text section
160 /// for c++ object files.
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000161 ELFSection &getTextSection() {
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000162 return getSection(".text", ELFSection::SHT_PROGBITS,
163 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000164 }
165
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000166 /// Get jump table section on the section name returned by TAI
167 ELFSection &getJumpTableSection(std::string SName, unsigned Align) {
168 return getSection(SName, ELFSection::SHT_PROGBITS,
169 ELFSection::SHF_ALLOC, Align);
170 }
171
Bruno Cardoso Lopes179f3a32009-06-25 07:36:24 +0000172 /// Get a constant pool section based on the section name returned by TAI
173 ELFSection &getConstantPoolSection(std::string SName, unsigned Align) {
174 return getSection(SName, ELFSection::SHT_PROGBITS,
175 ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC, Align);
176 }
177
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000178 /// Return the relocation section of section 'S'. 'RelA' is true
179 /// if the relocation section contains entries with addends.
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000180 ELFSection &getRelocSection(std::string SName, bool RelA, unsigned Align) {
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000181 std::string RelSName(".rel");
182 unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
183
184 if (RelA) RelSName.append("a");
185 RelSName.append(SName);
186
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000187 return getSection(RelSName, SHdrTy, 0, Align);
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000188 }
189
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000190 ELFSection &getNonExecStackSection() {
191 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
192 }
193
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000194 ELFSection &getSymbolTableSection() {
195 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
196 }
197
198 ELFSection &getStringTableSection() {
199 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
200 }
201
Bruno Cardoso Lopesb53d5c02009-06-23 04:39:27 +0000202 ELFSection &getSectionHeaderStringTableSection() {
203 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
204 }
205
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 ELFSection &getDataSection() {
207 return getSection(".data", ELFSection::SHT_PROGBITS,
Bruno Cardoso Lopesb53d5c02009-06-23 04:39:27 +0000208 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 }
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 ELFSection &getBSSSection() {
212 return getSection(".bss", ELFSection::SHT_NOBITS,
Bruno Cardoso Lopesb53d5c02009-06-23 04:39:27 +0000213 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 }
215
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000216 ELFSection &getNullSection() {
217 return getSection("", ELFSection::SHT_NULL, 0);
218 }
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000219
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000220 // Helpers for obtaining ELF specific info.
Bruno Cardoso Lopesaf939d82009-07-13 22:40:39 +0000221 unsigned getGlobalELFBinding(const GlobalValue *GV);
222 unsigned getGlobalELFType(const GlobalValue *GV);
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000223 unsigned getGlobalELFVisibility(const GlobalValue *GV);
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000224 unsigned getElfSectionFlags(unsigned Flags);
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 // As we complete the ELF file, we need to update fields in the ELF header
227 // (e.g. the location of the section table). These members keep track of
228 // the offset in ELFHeader of these various pieces to update and other
229 // locations in the file.
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000230 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
231 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
232 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000233
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 private:
Bruno Cardoso Lopesaf939d82009-07-13 22:40:39 +0000235 void EmitGlobal(const GlobalValue *GV);
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000236 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000237 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000238 ELFSection &GblS);
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000239 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000240 void EmitRelocations();
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000241 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000242 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 void EmitSectionTableStringTable();
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000244 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000245 void EmitSymbolTable();
Bruno Cardoso Lopes5d7a1eb2009-06-22 19:29:56 +0000246 void EmitStringTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 void OutputSectionsAndSectionTable();
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000248 unsigned SortSymbols();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 };
250}
251
252#endif