blob: 7cca74b1fb14acb44e0aa569ad3870de7cfe95ad [file] [log] [blame]
Chris Lattner35f0a4f2005-06-27 06:29:00 +00001//===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
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 Lattner35f0a4f2005-06-27 06:29:00 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the target-independent ELF writer. This file writes out
11// the ELF file in the following order:
12//
13// #1. ELF Header
Chris Lattner80ed8fa2005-07-07 07:02:20 +000014// #2. '.text' section
15// #3. '.data' section
16// #4. '.bss' section (conceptual position in file)
Chris Lattner35f0a4f2005-06-27 06:29:00 +000017// ...
18// #X. '.shstrtab' section
19// #Y. Section Table
20//
21// The entries in the section table are laid out as:
22// #0. Null entry [required]
Chris Lattner80ed8fa2005-07-07 07:02:20 +000023// #1. ".text" entry - the program code
24// #2. ".data" entry - global variables with initializers. [ if needed ]
25// #3. ".bss" entry - global variables without initializers. [ if needed ]
Chris Lattner35f0a4f2005-06-27 06:29:00 +000026// ...
27// #N. ".shstrtab" entry - String table for the section names.
Chris Lattner35f0a4f2005-06-27 06:29:00 +000028//
29// NOTE: This code should eventually be extended to support 64-bit ELF (this
30// won't be hard), but we haven't done so yet!
31//
32//===----------------------------------------------------------------------===//
33
Bill Wendling8f84f1f2007-02-08 01:35:27 +000034#include "ELFWriter.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000035#include "llvm/Module.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000036#include "llvm/PassManager.h"
Chris Lattner02b73ab2009-01-04 20:19:20 +000037#include "llvm/DerivedTypes.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000038#include "llvm/CodeGen/FileWriters.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000039#include "llvm/CodeGen/MachineCodeEmitter.h"
40#include "llvm/CodeGen/MachineConstantPool.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000041#include "llvm/CodeGen/MachineFunctionPass.h"
Owen Anderson07000c62006-05-12 06:33:49 +000042#include "llvm/Target/TargetData.h"
Bill Wendling5d73a2a2007-01-27 02:55:44 +000043#include "llvm/Target/TargetELFWriterInfo.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000044#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000045#include "llvm/Support/Mangler.h"
Bill Wendling203d3e42007-01-17 22:22:31 +000046#include "llvm/Support/OutputBuffer.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000047#include "llvm/Support/Streams.h"
Owen Andersoncb371882008-08-21 00:14:44 +000048#include "llvm/Support/raw_ostream.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000049#include <list>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000050using namespace llvm;
51
Devang Patel19974732007-05-03 01:11:54 +000052char ELFWriter::ID = 0;
Bill Wendling8f84f1f2007-02-08 01:35:27 +000053/// AddELFWriter - Concrete function to add the ELF writer to the function pass
54/// manager.
Dan Gohmanbfae8312008-03-11 22:29:46 +000055MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000056 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000057 TargetMachine &TM) {
58 ELFWriter *EW = new ELFWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000059 PM.add(EW);
Bill Wendling8f84f1f2007-02-08 01:35:27 +000060 return &EW->getMachineCodeEmitter();
61}
62
Chris Lattner0e180502005-07-11 06:34:30 +000063//===----------------------------------------------------------------------===//
64// ELFCodeEmitter Implementation
65//===----------------------------------------------------------------------===//
66
Chris Lattneraa507db2005-07-11 05:17:18 +000067namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000068 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
69 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000070 class ELFCodeEmitter : public MachineCodeEmitter {
71 ELFWriter &EW;
Bill Wendling203d3e42007-01-17 22:22:31 +000072 TargetMachine &TM;
Chris Lattner5f48ff72005-07-16 08:01:13 +000073 ELFWriter::ELFSection *ES; // Section to write to.
74 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000075 size_t FnStart;
76 public:
Dan Gohmanded2b0d2007-12-14 15:41:34 +000077 explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000078
Chris Lattner0e180502005-07-11 06:34:30 +000079 void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +000080 bool finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000081
Chris Lattneraa507db2005-07-11 05:17:18 +000082 void addRelocation(const MachineRelocation &MR) {
83 assert(0 && "relo not handled yet!");
84 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000085
86 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
87 }
88
Evan Cheng5788d1a2008-12-10 02:32:19 +000089 virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
Chris Lattneraa507db2005-07-11 05:17:18 +000090 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000091 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000092 }
Evan Cheng5788d1a2008-12-10 02:32:19 +000093 virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +000094 assert(0 && "JT not implementated yet!");
95 return 0;
96 }
Chris Lattnerf75f9be2006-05-02 23:22:24 +000097
Evan Cheng5788d1a2008-12-10 02:32:19 +000098 virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
Chris Lattnerb4432f32006-05-03 17:10:41 +000099 assert(0 && "JT not implementated yet!");
100 return 0;
101 }
Andrew Lenharthfe660392005-07-28 18:13:59 +0000102
Evan Cheng5788d1a2008-12-10 02:32:19 +0000103 virtual uintptr_t getLabelAddress(uint64_t Label) const {
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000104 assert(0 && "Label address not implementated yet!");
105 abort();
106 return 0;
107 }
108
109 virtual void emitLabel(uint64_t LabelID) {
110 assert(0 && "emit Label not implementated yet!");
111 abort();
112 }
113
114
115 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
116
117
Chris Lattner0e180502005-07-11 06:34:30 +0000118 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Evan Chengce4a70b2008-11-08 08:02:53 +0000119 void startGVStub(const GlobalValue* F, unsigned StubSize,
120 unsigned Alignment = 1) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000121 assert(0 && "JIT specific function called!");
122 abort();
123 }
Nate Begemand6b7a242009-02-18 08:31:02 +0000124 void startGVStub(const GlobalValue* F, void *Buffer, unsigned StubSize) {
125 assert(0 && "JIT specific function called!");
126 abort();
127 }
Evan Chengce4a70b2008-11-08 08:02:53 +0000128 void *finishGVStub(const GlobalValue *F) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000129 assert(0 && "JIT specific function called!");
130 abort();
131 return 0;
132 }
133 };
134}
135
Chris Lattner0e180502005-07-11 06:34:30 +0000136/// startFunction - This callback is invoked when a new machine function is
137/// about to be emitted.
138void ELFCodeEmitter::startFunction(MachineFunction &F) {
139 // Align the output buffer to the appropriate alignment.
140 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +0000141 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +0000142 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
143 ELFWriter::ELFSection::SHF_EXECINSTR |
144 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000145 OutBuffer = &ES->SectionData;
Bill Wendling203d3e42007-01-17 22:22:31 +0000146 cerr << "FIXME: This code needs to be updated for changes in the "
147 << "CodeEmitter interfaces. In particular, this should set "
Bill Wendlinge8156192006-12-07 01:30:32 +0000148 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
Chris Lattner43b429b2006-05-02 18:27:26 +0000149 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000150
Chris Lattner0e180502005-07-11 06:34:30 +0000151 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000152 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000153
Chris Lattner0e180502005-07-11 06:34:30 +0000154 // Add padding zeros to the end of the buffer to make sure that the
155 // function will start on the correct byte alignment within the section.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000156 OutputBuffer OB(*OutBuffer,
157 TM.getTargetData()->getPointerSizeInBits() == 64,
158 TM.getTargetData()->isLittleEndian());
Bill Wendling203d3e42007-01-17 22:22:31 +0000159 OB.align(Align);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000160 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000161}
162
163/// finishFunction - This callback is invoked after the function is completely
164/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000165bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000166 // We now know the size of the function, add a symbol to represent it.
167 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000168
Chris Lattner0e180502005-07-11 06:34:30 +0000169 // Figure out the binding (linkage) of the symbol.
170 switch (F.getFunction()->getLinkage()) {
171 default:
172 // appending linkage is illegal for functions.
173 assert(0 && "Unknown linkage type!");
174 case GlobalValue::ExternalLinkage:
175 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
176 break;
177 case GlobalValue::LinkOnceLinkage:
178 case GlobalValue::WeakLinkage:
179 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
180 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000181 case GlobalValue::PrivateLinkage:
182 assert (0 && "PrivateLinkage should not be in the symbol table.");
Chris Lattner0e180502005-07-11 06:34:30 +0000183 case GlobalValue::InternalLinkage:
184 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
185 break;
186 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000187
188 ES->Size = OutBuffer->size();
189
Chris Lattner0e180502005-07-11 06:34:30 +0000190 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000191 FnSym.SectionIdx = ES->SectionIdx;
Bill Wendling203d3e42007-01-17 22:22:31 +0000192 FnSym.Value = FnStart; // Value = Offset from start of Section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000193 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000194
Chris Lattner0e180502005-07-11 06:34:30 +0000195 // Finally, add it to the symtab.
196 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000197 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000198}
199
200//===----------------------------------------------------------------------===//
201// ELFWriter Implementation
202//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000203
Owen Andersoncb371882008-08-21 00:14:44 +0000204ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
Dan Gohmanae73dc12008-09-04 17:05:41 +0000205 : MachineFunctionPass(&ID), O(o), TM(tm) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000206 e_flags = 0; // e_flags defaults to 0, no flags.
207
Owen Andersona69571c2006-05-03 01:29:57 +0000208 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
209 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000210
211 // Create the machine code emitter object for this target.
Bill Wendlinge9116152007-01-17 09:06:13 +0000212 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000213 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000214}
215
216ELFWriter::~ELFWriter() {
217 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000218}
219
220// doInitialization - Emit the file header and all of the global variables for
221// the module to the ELF file.
222bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000223 Mang = new Mangler(M);
224
Chris Lattner5f48ff72005-07-16 08:01:13 +0000225 // Local alias to shortenify coming code.
226 std::vector<unsigned char> &FH = FileHeader;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000227 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Bill Wendling203d3e42007-01-17 22:22:31 +0000229 FHOut.outbyte(0x7F); // EI_MAG0
230 FHOut.outbyte('E'); // EI_MAG1
231 FHOut.outbyte('L'); // EI_MAG2
232 FHOut.outbyte('F'); // EI_MAG3
233 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
234 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
235 FHOut.outbyte(1); // EI_VERSION
Bill Wendlinge9116152007-01-17 09:06:13 +0000236 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000237
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000238 // This should change for shared objects.
Bill Wendling203d3e42007-01-17 22:22:31 +0000239 FHOut.outhalf(1); // e_type = ET_REL
Dan Gohman87c3d412008-05-05 16:48:32 +0000240 FHOut.outhalf(TM.getELFWriterInfo()->getEMachine()); // target-defined
Bill Wendling203d3e42007-01-17 22:22:31 +0000241 FHOut.outword(1); // e_version = 1
242 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
243 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000244
Chris Lattner5f48ff72005-07-16 08:01:13 +0000245 ELFHeader_e_shoff_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000246 FHOut.outaddr(0); // e_shoff
247 FHOut.outword(e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000248
Bill Wendling203d3e42007-01-17 22:22:31 +0000249 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
250 FHOut.outhalf(0); // e_phentsize = prog header entry size
251 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
252 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000253
Jeff Cohen00b168892005-07-27 06:12:32 +0000254
Chris Lattner5f48ff72005-07-16 08:01:13 +0000255 ELFHeader_e_shnum_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000256 FHOut.outhalf(0); // e_shnum = # of section header ents
Chris Lattner5f48ff72005-07-16 08:01:13 +0000257 ELFHeader_e_shstrndx_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000258 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000259
Chris Lattner5f48ff72005-07-16 08:01:13 +0000260 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000261 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000262
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000263 // Start up the symbol table. The first entry in the symtab is the null
264 // entry.
265 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000266
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000267 return false;
268}
269
Chris Lattner05895232005-07-16 17:41:06 +0000270void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000271 // If this is an external global, emit it now. TODO: Note that it would be
272 // better to ignore the symbol here and only add it to the symbol table if
273 // referenced.
274 if (!GV->hasInitializer()) {
275 ELFSym ExternalSym(GV);
276 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
277 ExternalSym.SetType(ELFSym::STT_NOTYPE);
278 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
279 SymbolTable.push_back(ExternalSym);
280 return;
281 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000282
Duncan Sandsd1025932008-01-29 06:23:44 +0000283 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
Chris Lattner02b73ab2009-01-04 20:19:20 +0000284 unsigned Size =
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000285 TM.getTargetData()->getTypePaddedSize(GV->getType()->getElementType());
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000286
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000287 // If this global has a zero initializer, it is part of the .bss or common
288 // section.
289 if (GV->getInitializer()->isNullValue()) {
290 // If this global is part of the common block, add it now. Variables are
291 // part of the common block if they are zero initialized and allowed to be
292 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000293 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
294 GV->hasCommonLinkage()) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000295 ELFSym CommonSym(GV);
296 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000297 CommonSym.Value = Align;
298 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000299 CommonSym.SetBind(ELFSym::STB_GLOBAL);
300 CommonSym.SetType(ELFSym::STT_OBJECT);
301 // TODO SOMEDAY: add ELF visibility.
302 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
303 SymbolTable.push_back(CommonSym);
304 return;
305 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000306
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000307 // Otherwise, this symbol is part of the .bss section. Emit it now.
308
309 // Handle alignment. Ensure section is aligned at least as much as required
310 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000311 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000312 BSSSection.Align = std::max(BSSSection.Align, Align);
313
314 // Within the section, emit enough virtual padding to get us to an alignment
315 // boundary.
316 if (Align)
317 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
318
319 ELFSym BSSSym(GV);
320 BSSSym.Value = BSSSection.Size;
321 BSSSym.Size = Size;
322 BSSSym.SetType(ELFSym::STT_OBJECT);
323
324 switch (GV->getLinkage()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000325 default: // weak/linkonce/common handled above
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000326 assert(0 && "Unexpected linkage type!");
327 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
328 case GlobalValue::ExternalLinkage:
329 BSSSym.SetBind(ELFSym::STB_GLOBAL);
330 break;
331 case GlobalValue::InternalLinkage:
332 BSSSym.SetBind(ELFSym::STB_LOCAL);
333 break;
334 }
335
336 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000337 BSSSym.SectionIdx = BSSSection.SectionIdx;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000338 if (!GV->hasPrivateLinkage())
339 SymbolTable.push_back(BSSSym);
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000340
341 // Reserve space in the .bss section for this symbol.
342 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000343 return;
344 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000345
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000346 // FIXME: handle .rodata
347 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000348
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000349 // FIXME: handle .data
350 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000351}
352
353
354bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000355 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000356 return false;
357}
358
359/// doFinalization - Now that the module has been completely processed, emit
360/// the ELF file to 'O'.
361bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000362 // Okay, the ELF header and .text sections have been completed, build the
363 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000364 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
365 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000366 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000367
368 // Emit the symbol table now, if non-empty.
369 EmitSymbolTable();
370
371 // FIXME: Emit the relocations now.
372
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000373 // Emit the string table for the sections in the ELF file we have.
374 EmitSectionTableStringTable();
375
Chris Lattner5f48ff72005-07-16 08:01:13 +0000376 // Emit the sections to the .o file, and emit the section table for the file.
377 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000378
Chris Lattner5f48ff72005-07-16 08:01:13 +0000379 // We are done with the abstract symbols.
380 SectionList.clear();
381 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000382
383 // Release the name mangler object.
384 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000385 return false;
386}
387
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000388/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
389/// table for it and then the symbol table itself.
390void ELFWriter::EmitSymbolTable() {
391 if (SymbolTable.size() == 1) return; // Only the null entry.
392
393 // FIXME: compact all local symbols to the start of the symtab.
394 unsigned FirstNonLocalSymbol = 1;
395
Chris Lattner00033952005-07-16 17:36:04 +0000396 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000397 StrTab.Align = 1;
398
Chris Lattner5f48ff72005-07-16 08:01:13 +0000399 DataBuffer &StrTabBuf = StrTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000400 OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000401
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000402 // Set the zero'th symbol to a null byte, as required.
Bill Wendling203d3e42007-01-17 22:22:31 +0000403 StrTabOut.outbyte(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000404 SymbolTable[0].NameIdx = 0;
405 unsigned Index = 1;
406 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000407 // Use the name mangler to uniquify the LLVM symbol.
408 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000409
410 if (Name.empty()) {
411 SymbolTable[i].NameIdx = 0;
412 } else {
413 SymbolTable[i].NameIdx = Index;
414
415 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000416 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000417
418 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000419 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000420
421 // Keep track of the number of bytes emitted to this section.
422 Index += Name.size()+1;
423 }
424 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000425 assert(Index == StrTabBuf.size());
426 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000427
428 // Now that we have emitted the string table and know the offset into the
429 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000430 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000431 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000432 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000433 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
434 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000435 DataBuffer &SymTabBuf = SymTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000436 OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000437
Chris Lattner46c53052005-07-12 06:57:52 +0000438 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
439 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
440 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000441 SymTabOut.outword(Sym.NameIdx);
442 SymTabOut.outaddr32(Sym.Value);
443 SymTabOut.outword(Sym.Size);
444 SymTabOut.outbyte(Sym.Info);
445 SymTabOut.outbyte(Sym.Other);
446 SymTabOut.outhalf(Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000447 }
448 } else {
449 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
450 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000451 SymTabOut.outword(Sym.NameIdx);
452 SymTabOut.outbyte(Sym.Info);
453 SymTabOut.outbyte(Sym.Other);
454 SymTabOut.outhalf(Sym.SectionIdx);
455 SymTabOut.outaddr64(Sym.Value);
456 SymTabOut.outxword(Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000457 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000458 }
459
Chris Lattner5f48ff72005-07-16 08:01:13 +0000460 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000461}
462
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000463/// EmitSectionTableStringTable - This method adds and emits a section for the
464/// ELF Section Table string table: the string table that holds all of the
465/// section names.
466void ELFWriter::EmitSectionTableStringTable() {
467 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000468 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000469
470 // Now that we know which section number is the .shstrtab section, update the
471 // e_shstrndx entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000472 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000473 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000474
475 // Set the NameIdx of each section in the string table and emit the bytes for
476 // the string table.
477 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000478 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000479
Chris Lattner5f48ff72005-07-16 08:01:13 +0000480 for (std::list<ELFSection>::iterator I = SectionList.begin(),
481 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000482 // Set the index into the table. Note if we have lots of entries with
483 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000484 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000485
486 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000487 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
488
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000489 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000490 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000491
492 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000493 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000494 }
495
496 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000497 assert(Index == Buf.size());
498 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000499}
500
Chris Lattner5f48ff72005-07-16 08:01:13 +0000501/// OutputSectionsAndSectionTable - Now that we have constructed the file header
502/// and all of the sections, emit these to the ostream destination and emit the
503/// SectionTable.
504void ELFWriter::OutputSectionsAndSectionTable() {
505 // Pass #1: Compute the file offset for each section.
506 size_t FileOff = FileHeader.size(); // File header first.
507
508 // Emit all of the section data in order.
509 for (std::list<ELFSection>::iterator I = SectionList.begin(),
510 E = SectionList.end(); I != E; ++I) {
511 // Align FileOff to whatever the alignment restrictions of the section are.
512 if (I->Align)
513 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
514 I->Offset = FileOff;
515 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000516 }
517
Chris Lattner5f48ff72005-07-16 08:01:13 +0000518 // Align Section Header.
519 unsigned TableAlign = is64Bit ? 8 : 4;
520 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
521
522 // Now that we know where all of the sections will be emitted, set the e_shnum
523 // entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000524 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000525 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000526
Chris Lattner5f48ff72005-07-16 08:01:13 +0000527 // Now that we know the offset in the file of the section table, update the
528 // e_shoff address in the ELF header.
Bill Wendling203d3e42007-01-17 22:22:31 +0000529 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000530
Chris Lattner5f48ff72005-07-16 08:01:13 +0000531 // Now that we know all of the data in the file header, emit it and all of the
532 // sections!
533 O.write((char*)&FileHeader[0], FileHeader.size());
534 FileOff = FileHeader.size();
535 DataBuffer().swap(FileHeader);
536
537 DataBuffer Table;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000538 OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000539
540 // Emit all of the section data and build the section table itself.
541 while (!SectionList.empty()) {
542 const ELFSection &S = *SectionList.begin();
543
544 // Align FileOff to whatever the alignment restrictions of the section are.
545 if (S.Align)
546 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
547 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000548 O << (char)0xAB;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000549 O.write((char*)&S.SectionData[0], S.SectionData.size());
550 FileOff += S.SectionData.size();
551
Bill Wendling203d3e42007-01-17 22:22:31 +0000552 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
553 TableOut.outword(S.Type); // sh_type - Section contents & semantics
554 TableOut.outword(S.Flags); // sh_flags - Section flags.
555 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
556 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
557 TableOut.outword(S.Size); // sh_size - The section size.
558 TableOut.outword(S.Link); // sh_link - Section header table index link.
559 TableOut.outword(S.Info); // sh_info - Auxillary information.
560 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
561 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000562
563 SectionList.pop_front();
564 }
565
566 // Align output for the section table.
567 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
568 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000569 O << (char)0xAB;
Jeff Cohen00b168892005-07-27 06:12:32 +0000570
Chris Lattner5f48ff72005-07-16 08:01:13 +0000571 // Emit the section table itself.
572 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000573}