blob: 89ab00c397dbe3e7e4599b395294e04681bb623b [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
Bruno Cardoso Lopesf4f4dcd2009-07-16 06:26:41 +000037 typedef std::vector<ELFSym*>::iterator ELFSymIter;
38 typedef std::vector<ELFSection*>::iterator ELFSectionIter;
39
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 /// ELFWriter - This class implements the common target-independent code for
41 /// writing ELF files. Targets should derive a class from this to
42 /// parameterize the output format.
43 ///
44 class ELFWriter : public MachineFunctionPass {
45 friend class ELFCodeEmitter;
46 public:
47 static char ID;
48
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000049 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
50 ObjectCodeEmitter *getObjectCodeEmitter() {
51 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 }
53
Owen Anderson847b99b2008-08-21 00:14:44 +000054 ELFWriter(raw_ostream &O, TargetMachine &TM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 ~ELFWriter();
56
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 protected:
58 /// Output stream to send the resultant object file to.
Owen Anderson847b99b2008-08-21 00:14:44 +000059 raw_ostream &O;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61 /// Target machine description.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 TargetMachine &TM;
63
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000064 /// Target Elf Writer description.
65 const TargetELFWriterInfo *TEW;
66
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 /// Mang - The object used to perform name mangling for this module.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 Mangler *Mang;
69
70 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
71 /// code for functions to the .o file.
Bruno Cardoso Lopes2bcbd2e2009-07-06 09:26:48 +000072 ELFCodeEmitter *ElfCE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +000074 /// TAI - Target Asm Info, provide information about section names for
75 /// globals and other target specific stuff.
76 const TargetAsmInfo *TAI;
77
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 // Properties inferred automatically from the target machine.
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000080 //===------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
82 /// is64Bit/isLittleEndian - This information is inferred from the target
83 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
84 bool is64Bit, isLittleEndian;
85
86 /// doInitialization - Emit the file header and all of the global variables
87 /// for the module to the ELF file.
88 bool doInitialization(Module &M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 bool runOnMachineFunction(MachineFunction &MF);
90
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 /// doFinalization - Now that the module has been completely processed, emit
92 /// the ELF file to 'O'.
93 bool doFinalization(Module &M);
94
95 private:
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +000096 /// Blob containing the Elf header
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +000097 BinaryObject ElfHdr;
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +000098
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 /// SectionList - This is the list of sections that we have emitted to the
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000100 /// file. Once the file has been completely built, the section header table
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 /// is constructed from this info.
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000102 std::vector<ELFSection*> SectionList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 unsigned NumSections; // Always = SectionList.size()
104
105 /// SectionLookup - This is a mapping from section name to section number in
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000106 /// the SectionList. Used to quickly gather the Section Index from TAI names
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 std::map<std::string, ELFSection*> SectionLookup;
108
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000109 /// GblSymLookup - This is a mapping from global value to a symbol index
Bruno Cardoso Lopes8c203922009-07-18 19:30:09 +0000110 /// in the symbol table or private symbols list. This is useful since reloc
111 /// symbol references must be quickly mapped to their indices on the lists
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000112 std::map<const GlobalValue*, uint32_t> GblSymLookup;
113
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000114 /// SymbolList - This is the list of symbols emitted to the symbol table.
115 /// When the SymbolList is finally built, local symbols must be placed in
116 /// the beginning while non-locals at the end.
117 std::vector<ELFSym*> SymbolList;
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000118
Bruno Cardoso Lopes8c203922009-07-18 19:30:09 +0000119 /// PrivateSyms - Record private symbols, every symbol here must never be
120 /// present in the SymbolList.
121 std::vector<ELFSym*> PrivateSyms;
122
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000123 /// PendingGlobals - List of externally defined symbols that we have been
124 /// asked to emit, but have not seen a reference to. When a reference
125 /// is seen, the symbol will move from this list to the SymbolList.
126 SetVector<GlobalValue*> PendingGlobals;
127
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000128 // Remove tab from section name prefix. This is necessary becase TAI
129 // sometimes return a section name prefixed with elf unused chars. This is
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000130 // a little bit dirty. FIXME: find a better approach, maybe add more
131 // methods to TAI to get the clean name?
132 void fixNameForSection(std::string &Name) {
133 size_t Pos = Name.find("\t");
134 if (Pos != std::string::npos)
135 Name.erase(Pos, 1);
136
137 Pos = Name.find(".section ");
138 if (Pos != std::string::npos)
139 Name.erase(Pos, 9);
140
141 Pos = Name.find("\n");
142 if (Pos != std::string::npos)
143 Name.erase(Pos, 1);
144 }
145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 /// getSection - Return the section with the specified name, creating a new
147 /// section if one does not already exist.
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000148 ELFSection &getSection(const std::string &Name, unsigned Type,
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000149 unsigned Flags = 0, unsigned Align = 0) {
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000150 std::string SName(Name);
151 fixNameForSection(SName);
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000152
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000153 ELFSection *&SN = SectionLookup[SName];
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000154 if (SN) return *SN;
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000155
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000156 SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit));
157 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 Lopes6d4086b2009-06-22 19:16:16 +0000166 /// TODO: support mangled names here to emit the right .text section
167 /// for c++ object files.
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000168 ELFSection &getTextSection() {
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000169 return getSection(".text", ELFSection::SHT_PROGBITS,
170 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
Bruno Cardoso Lopes23dfc8f2009-06-05 00:22:10 +0000171 }
172
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000173 /// Get jump table section on the section name returned by TAI
174 ELFSection &getJumpTableSection(std::string SName, unsigned Align) {
175 return getSection(SName, ELFSection::SHT_PROGBITS,
176 ELFSection::SHF_ALLOC, Align);
177 }
178
Bruno Cardoso Lopes179f3a32009-06-25 07:36:24 +0000179 /// Get a constant pool section based on the section name returned by TAI
180 ELFSection &getConstantPoolSection(std::string SName, unsigned Align) {
181 return getSection(SName, ELFSection::SHT_PROGBITS,
182 ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC, Align);
183 }
184
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000185 /// Return the relocation section of section 'S'. 'RelA' is true
186 /// if the relocation section contains entries with addends.
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000187 ELFSection &getRelocSection(std::string SName, bool RelA, unsigned Align) {
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000188 std::string RelSName(".rel");
189 unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
190
191 if (RelA) RelSName.append("a");
192 RelSName.append(SName);
193
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000194 return getSection(RelSName, SHdrTy, 0, Align);
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000195 }
196
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000197 ELFSection &getNonExecStackSection() {
198 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
199 }
200
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000201 ELFSection &getSymbolTableSection() {
202 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
203 }
204
205 ELFSection &getStringTableSection() {
206 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
207 }
208
Bruno Cardoso Lopesb53d5c02009-06-23 04:39:27 +0000209 ELFSection &getSectionHeaderStringTableSection() {
210 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
211 }
212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 ELFSection &getDataSection() {
214 return getSection(".data", ELFSection::SHT_PROGBITS,
Bruno Cardoso Lopesb53d5c02009-06-23 04:39:27 +0000215 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 }
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 ELFSection &getBSSSection() {
219 return getSection(".bss", ELFSection::SHT_NOBITS,
Bruno Cardoso Lopesb53d5c02009-06-23 04:39:27 +0000220 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 }
222
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000223 ELFSection &getNullSection() {
224 return getSection("", ELFSection::SHT_NULL, 0);
225 }
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000226
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000227 // Helpers for obtaining ELF specific info.
Bruno Cardoso Lopesaf939d82009-07-13 22:40:39 +0000228 unsigned getGlobalELFBinding(const GlobalValue *GV);
229 unsigned getGlobalELFType(const GlobalValue *GV);
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000230 unsigned getGlobalELFVisibility(const GlobalValue *GV);
Bruno Cardoso Lopes79ed6712009-07-03 04:36:26 +0000231 unsigned getElfSectionFlags(unsigned Flags);
Bruno Cardoso Lopes9dcbc862009-07-02 18:29:24 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // As we complete the ELF file, we need to update fields in the ELF header
234 // (e.g. the location of the section table). These members keep track of
235 // the offset in ELFHeader of these various pieces to update and other
236 // locations in the file.
Bruno Cardoso Lopes04066de2009-06-06 03:56:29 +0000237 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
238 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
239 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 private:
Bruno Cardoso Lopesaf939d82009-07-13 22:40:39 +0000242 void EmitGlobal(const GlobalValue *GV);
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000243 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000244 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000245 ELFSection &GblS);
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000246 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym);
Bruno Cardoso Lopes6b5788e2009-06-07 21:22:38 +0000247 void EmitRelocations();
Bruno Cardoso Lopes6d4086b2009-06-22 19:16:16 +0000248 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000249 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 void EmitSectionTableStringTable();
Bruno Cardoso Lopesfee73a32009-06-14 07:53:21 +0000251 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
Bruno Cardoso Lopes8c25df12009-06-11 19:16:03 +0000252 void EmitSymbolTable();
Bruno Cardoso Lopes5d7a1eb2009-06-22 19:29:56 +0000253 void EmitStringTable();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 void OutputSectionsAndSectionTable();
Bruno Cardoso Lopesa412a422009-07-15 20:49:10 +0000255 unsigned SortSymbols();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 };
257}
258
259#endif