blob: 0b6e0b1b1031ecfda853ff7f26adb5f13e42e83b [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"
37#include "llvm/CodeGen/FileWriters.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000038#include "llvm/CodeGen/MachineCodeEmitter.h"
39#include "llvm/CodeGen/MachineConstantPool.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000040#include "llvm/CodeGen/MachineFunctionPass.h"
Owen Anderson07000c62006-05-12 06:33:49 +000041#include "llvm/Target/TargetData.h"
Bill Wendling5d73a2a2007-01-27 02:55:44 +000042#include "llvm/Target/TargetELFWriterInfo.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000043#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000044#include "llvm/Support/Mangler.h"
Bill Wendling203d3e42007-01-17 22:22:31 +000045#include "llvm/Support/OutputBuffer.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000046#include "llvm/Support/Streams.h"
Owen Andersoncb371882008-08-21 00:14:44 +000047#include "llvm/Support/raw_ostream.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000048#include <list>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000049using namespace llvm;
50
Devang Patel19974732007-05-03 01:11:54 +000051char ELFWriter::ID = 0;
Bill Wendling8f84f1f2007-02-08 01:35:27 +000052/// AddELFWriter - Concrete function to add the ELF writer to the function pass
53/// manager.
Dan Gohmanbfae8312008-03-11 22:29:46 +000054MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000055 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000056 TargetMachine &TM) {
57 ELFWriter *EW = new ELFWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000058 PM.add(EW);
Bill Wendling8f84f1f2007-02-08 01:35:27 +000059 return &EW->getMachineCodeEmitter();
60}
61
Chris Lattner0e180502005-07-11 06:34:30 +000062//===----------------------------------------------------------------------===//
63// ELFCodeEmitter Implementation
64//===----------------------------------------------------------------------===//
65
Chris Lattneraa507db2005-07-11 05:17:18 +000066namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000067 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
68 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000069 class ELFCodeEmitter : public MachineCodeEmitter {
70 ELFWriter &EW;
Bill Wendling203d3e42007-01-17 22:22:31 +000071 TargetMachine &TM;
Chris Lattner5f48ff72005-07-16 08:01:13 +000072 ELFWriter::ELFSection *ES; // Section to write to.
73 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000074 size_t FnStart;
75 public:
Dan Gohmanded2b0d2007-12-14 15:41:34 +000076 explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000077
Chris Lattner0e180502005-07-11 06:34:30 +000078 void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +000079 bool finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000080
Chris Lattneraa507db2005-07-11 05:17:18 +000081 void addRelocation(const MachineRelocation &MR) {
82 assert(0 && "relo not handled yet!");
83 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000084
85 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
86 }
87
88 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
Chris Lattneraa507db2005-07-11 05:17:18 +000089 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000090 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000091 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000092 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +000093 assert(0 && "JT not implementated yet!");
94 return 0;
95 }
Chris Lattnerf75f9be2006-05-02 23:22:24 +000096
Chris Lattnerb4432f32006-05-03 17:10:41 +000097 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
98 assert(0 && "JT not implementated yet!");
99 return 0;
100 }
Andrew Lenharthfe660392005-07-28 18:13:59 +0000101
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000102 virtual intptr_t getLabelAddress(uint64_t Label) const {
103 assert(0 && "Label address not implementated yet!");
104 abort();
105 return 0;
106 }
107
108 virtual void emitLabel(uint64_t LabelID) {
109 assert(0 && "emit Label not implementated yet!");
110 abort();
111 }
112
113
114 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
115
116
Chris Lattner0e180502005-07-11 06:34:30 +0000117 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000118 void startFunctionStub(const GlobalValue* F, unsigned StubSize,
119 unsigned Alignment = 1) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000120 assert(0 && "JIT specific function called!");
121 abort();
122 }
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000123 void *finishFunctionStub(const GlobalValue *F) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000124 assert(0 && "JIT specific function called!");
125 abort();
126 return 0;
127 }
128 };
129}
130
Chris Lattner0e180502005-07-11 06:34:30 +0000131/// startFunction - This callback is invoked when a new machine function is
132/// about to be emitted.
133void ELFCodeEmitter::startFunction(MachineFunction &F) {
134 // Align the output buffer to the appropriate alignment.
135 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +0000136 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +0000137 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
138 ELFWriter::ELFSection::SHF_EXECINSTR |
139 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000140 OutBuffer = &ES->SectionData;
Bill Wendling203d3e42007-01-17 22:22:31 +0000141 cerr << "FIXME: This code needs to be updated for changes in the "
142 << "CodeEmitter interfaces. In particular, this should set "
Bill Wendlinge8156192006-12-07 01:30:32 +0000143 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
Chris Lattner43b429b2006-05-02 18:27:26 +0000144 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000145
Chris Lattner0e180502005-07-11 06:34:30 +0000146 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000147 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000148
Chris Lattner0e180502005-07-11 06:34:30 +0000149 // Add padding zeros to the end of the buffer to make sure that the
150 // function will start on the correct byte alignment within the section.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000151 OutputBuffer OB(*OutBuffer,
152 TM.getTargetData()->getPointerSizeInBits() == 64,
153 TM.getTargetData()->isLittleEndian());
Bill Wendling203d3e42007-01-17 22:22:31 +0000154 OB.align(Align);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000155 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000156}
157
158/// finishFunction - This callback is invoked after the function is completely
159/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000160bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000161 // We now know the size of the function, add a symbol to represent it.
162 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000163
Chris Lattner0e180502005-07-11 06:34:30 +0000164 // Figure out the binding (linkage) of the symbol.
165 switch (F.getFunction()->getLinkage()) {
166 default:
167 // appending linkage is illegal for functions.
168 assert(0 && "Unknown linkage type!");
169 case GlobalValue::ExternalLinkage:
170 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
171 break;
172 case GlobalValue::LinkOnceLinkage:
173 case GlobalValue::WeakLinkage:
174 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
175 break;
176 case GlobalValue::InternalLinkage:
177 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
178 break;
179 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000180
181 ES->Size = OutBuffer->size();
182
Chris Lattner0e180502005-07-11 06:34:30 +0000183 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000184 FnSym.SectionIdx = ES->SectionIdx;
Bill Wendling203d3e42007-01-17 22:22:31 +0000185 FnSym.Value = FnStart; // Value = Offset from start of Section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000186 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000187
Chris Lattner0e180502005-07-11 06:34:30 +0000188 // Finally, add it to the symtab.
189 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000190 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000191}
192
193//===----------------------------------------------------------------------===//
194// ELFWriter Implementation
195//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000196
Owen Andersoncb371882008-08-21 00:14:44 +0000197ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
Dan Gohmanae73dc12008-09-04 17:05:41 +0000198 : MachineFunctionPass(&ID), O(o), TM(tm) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000199 e_flags = 0; // e_flags defaults to 0, no flags.
200
Owen Andersona69571c2006-05-03 01:29:57 +0000201 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
202 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000203
204 // Create the machine code emitter object for this target.
Bill Wendlinge9116152007-01-17 09:06:13 +0000205 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000206 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000207}
208
209ELFWriter::~ELFWriter() {
210 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000211}
212
213// doInitialization - Emit the file header and all of the global variables for
214// the module to the ELF file.
215bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000216 Mang = new Mangler(M);
217
Chris Lattner5f48ff72005-07-16 08:01:13 +0000218 // Local alias to shortenify coming code.
219 std::vector<unsigned char> &FH = FileHeader;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000220 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Jeff Cohen00b168892005-07-27 06:12:32 +0000221
Bill Wendling203d3e42007-01-17 22:22:31 +0000222 FHOut.outbyte(0x7F); // EI_MAG0
223 FHOut.outbyte('E'); // EI_MAG1
224 FHOut.outbyte('L'); // EI_MAG2
225 FHOut.outbyte('F'); // EI_MAG3
226 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
227 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
228 FHOut.outbyte(1); // EI_VERSION
Bill Wendlinge9116152007-01-17 09:06:13 +0000229 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000230
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000231 // This should change for shared objects.
Bill Wendling203d3e42007-01-17 22:22:31 +0000232 FHOut.outhalf(1); // e_type = ET_REL
Dan Gohman87c3d412008-05-05 16:48:32 +0000233 FHOut.outhalf(TM.getELFWriterInfo()->getEMachine()); // target-defined
Bill Wendling203d3e42007-01-17 22:22:31 +0000234 FHOut.outword(1); // e_version = 1
235 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
236 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000237
Chris Lattner5f48ff72005-07-16 08:01:13 +0000238 ELFHeader_e_shoff_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000239 FHOut.outaddr(0); // e_shoff
240 FHOut.outword(e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000241
Bill Wendling203d3e42007-01-17 22:22:31 +0000242 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
243 FHOut.outhalf(0); // e_phentsize = prog header entry size
244 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
245 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000246
Jeff Cohen00b168892005-07-27 06:12:32 +0000247
Chris Lattner5f48ff72005-07-16 08:01:13 +0000248 ELFHeader_e_shnum_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000249 FHOut.outhalf(0); // e_shnum = # of section header ents
Chris Lattner5f48ff72005-07-16 08:01:13 +0000250 ELFHeader_e_shstrndx_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000251 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000252
Chris Lattner5f48ff72005-07-16 08:01:13 +0000253 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000254 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000255
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000256 // Start up the symbol table. The first entry in the symtab is the null
257 // entry.
258 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000259
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000260 return false;
261}
262
Chris Lattner05895232005-07-16 17:41:06 +0000263void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000264 // If this is an external global, emit it now. TODO: Note that it would be
265 // better to ignore the symbol here and only add it to the symbol table if
266 // referenced.
267 if (!GV->hasInitializer()) {
268 ELFSym ExternalSym(GV);
269 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
270 ExternalSym.SetType(ELFSym::STT_NOTYPE);
271 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
272 SymbolTable.push_back(ExternalSym);
273 return;
274 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000275
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000276 const Type *GVType = (const Type*)GV->getType();
Duncan Sandsd1025932008-01-29 06:23:44 +0000277 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
Duncan Sandsca0ed742007-11-05 00:04:43 +0000278 unsigned Size = TM.getTargetData()->getABITypeSize(GVType);
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000279
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000280 // If this global has a zero initializer, it is part of the .bss or common
281 // section.
282 if (GV->getInitializer()->isNullValue()) {
283 // If this global is part of the common block, add it now. Variables are
284 // part of the common block if they are zero initialized and allowed to be
285 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000286 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
287 GV->hasCommonLinkage()) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000288 ELFSym CommonSym(GV);
289 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000290 CommonSym.Value = Align;
291 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000292 CommonSym.SetBind(ELFSym::STB_GLOBAL);
293 CommonSym.SetType(ELFSym::STT_OBJECT);
294 // TODO SOMEDAY: add ELF visibility.
295 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
296 SymbolTable.push_back(CommonSym);
297 return;
298 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000299
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000300 // Otherwise, this symbol is part of the .bss section. Emit it now.
301
302 // Handle alignment. Ensure section is aligned at least as much as required
303 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000304 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000305 BSSSection.Align = std::max(BSSSection.Align, Align);
306
307 // Within the section, emit enough virtual padding to get us to an alignment
308 // boundary.
309 if (Align)
310 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
311
312 ELFSym BSSSym(GV);
313 BSSSym.Value = BSSSection.Size;
314 BSSSym.Size = Size;
315 BSSSym.SetType(ELFSym::STT_OBJECT);
316
317 switch (GV->getLinkage()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000318 default: // weak/linkonce/common handled above
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000319 assert(0 && "Unexpected linkage type!");
320 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
321 case GlobalValue::ExternalLinkage:
322 BSSSym.SetBind(ELFSym::STB_GLOBAL);
323 break;
324 case GlobalValue::InternalLinkage:
325 BSSSym.SetBind(ELFSym::STB_LOCAL);
326 break;
327 }
328
329 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000330 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000331 SymbolTable.push_back(BSSSym);
332
333 // Reserve space in the .bss section for this symbol.
334 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000335 return;
336 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000337
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000338 // FIXME: handle .rodata
339 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000340
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000341 // FIXME: handle .data
342 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000343}
344
345
346bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000347 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000348 return false;
349}
350
351/// doFinalization - Now that the module has been completely processed, emit
352/// the ELF file to 'O'.
353bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000354 // Okay, the ELF header and .text sections have been completed, build the
355 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000356 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
357 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000358 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000359
360 // Emit the symbol table now, if non-empty.
361 EmitSymbolTable();
362
363 // FIXME: Emit the relocations now.
364
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000365 // Emit the string table for the sections in the ELF file we have.
366 EmitSectionTableStringTable();
367
Chris Lattner5f48ff72005-07-16 08:01:13 +0000368 // Emit the sections to the .o file, and emit the section table for the file.
369 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000370
Chris Lattner5f48ff72005-07-16 08:01:13 +0000371 // We are done with the abstract symbols.
372 SectionList.clear();
373 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000374
375 // Release the name mangler object.
376 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000377 return false;
378}
379
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000380/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
381/// table for it and then the symbol table itself.
382void ELFWriter::EmitSymbolTable() {
383 if (SymbolTable.size() == 1) return; // Only the null entry.
384
385 // FIXME: compact all local symbols to the start of the symtab.
386 unsigned FirstNonLocalSymbol = 1;
387
Chris Lattner00033952005-07-16 17:36:04 +0000388 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000389 StrTab.Align = 1;
390
Chris Lattner5f48ff72005-07-16 08:01:13 +0000391 DataBuffer &StrTabBuf = StrTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000392 OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000393
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000394 // Set the zero'th symbol to a null byte, as required.
Bill Wendling203d3e42007-01-17 22:22:31 +0000395 StrTabOut.outbyte(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000396 SymbolTable[0].NameIdx = 0;
397 unsigned Index = 1;
398 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000399 // Use the name mangler to uniquify the LLVM symbol.
400 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000401
402 if (Name.empty()) {
403 SymbolTable[i].NameIdx = 0;
404 } else {
405 SymbolTable[i].NameIdx = Index;
406
407 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000408 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000409
410 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000411 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000412
413 // Keep track of the number of bytes emitted to this section.
414 Index += Name.size()+1;
415 }
416 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000417 assert(Index == StrTabBuf.size());
418 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000419
420 // Now that we have emitted the string table and know the offset into the
421 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000422 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000423 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000424 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000425 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
426 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000427 DataBuffer &SymTabBuf = SymTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000428 OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000429
Chris Lattner46c53052005-07-12 06:57:52 +0000430 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
431 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
432 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000433 SymTabOut.outword(Sym.NameIdx);
434 SymTabOut.outaddr32(Sym.Value);
435 SymTabOut.outword(Sym.Size);
436 SymTabOut.outbyte(Sym.Info);
437 SymTabOut.outbyte(Sym.Other);
438 SymTabOut.outhalf(Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000439 }
440 } else {
441 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
442 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000443 SymTabOut.outword(Sym.NameIdx);
444 SymTabOut.outbyte(Sym.Info);
445 SymTabOut.outbyte(Sym.Other);
446 SymTabOut.outhalf(Sym.SectionIdx);
447 SymTabOut.outaddr64(Sym.Value);
448 SymTabOut.outxword(Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000449 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000450 }
451
Chris Lattner5f48ff72005-07-16 08:01:13 +0000452 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000453}
454
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000455/// EmitSectionTableStringTable - This method adds and emits a section for the
456/// ELF Section Table string table: the string table that holds all of the
457/// section names.
458void ELFWriter::EmitSectionTableStringTable() {
459 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000460 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000461
462 // Now that we know which section number is the .shstrtab section, update the
463 // e_shstrndx entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000464 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000465 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000466
467 // Set the NameIdx of each section in the string table and emit the bytes for
468 // the string table.
469 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000470 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000471
Chris Lattner5f48ff72005-07-16 08:01:13 +0000472 for (std::list<ELFSection>::iterator I = SectionList.begin(),
473 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000474 // Set the index into the table. Note if we have lots of entries with
475 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000476 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000477
478 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000479 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
480
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000481 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000482 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000483
484 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000485 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000486 }
487
488 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000489 assert(Index == Buf.size());
490 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000491}
492
Chris Lattner5f48ff72005-07-16 08:01:13 +0000493/// OutputSectionsAndSectionTable - Now that we have constructed the file header
494/// and all of the sections, emit these to the ostream destination and emit the
495/// SectionTable.
496void ELFWriter::OutputSectionsAndSectionTable() {
497 // Pass #1: Compute the file offset for each section.
498 size_t FileOff = FileHeader.size(); // File header first.
499
500 // Emit all of the section data in order.
501 for (std::list<ELFSection>::iterator I = SectionList.begin(),
502 E = SectionList.end(); I != E; ++I) {
503 // Align FileOff to whatever the alignment restrictions of the section are.
504 if (I->Align)
505 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
506 I->Offset = FileOff;
507 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000508 }
509
Chris Lattner5f48ff72005-07-16 08:01:13 +0000510 // Align Section Header.
511 unsigned TableAlign = is64Bit ? 8 : 4;
512 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
513
514 // Now that we know where all of the sections will be emitted, set the e_shnum
515 // entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000516 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000517 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000518
Chris Lattner5f48ff72005-07-16 08:01:13 +0000519 // Now that we know the offset in the file of the section table, update the
520 // e_shoff address in the ELF header.
Bill Wendling203d3e42007-01-17 22:22:31 +0000521 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000522
Chris Lattner5f48ff72005-07-16 08:01:13 +0000523 // Now that we know all of the data in the file header, emit it and all of the
524 // sections!
525 O.write((char*)&FileHeader[0], FileHeader.size());
526 FileOff = FileHeader.size();
527 DataBuffer().swap(FileHeader);
528
529 DataBuffer Table;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000530 OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000531
532 // Emit all of the section data and build the section table itself.
533 while (!SectionList.empty()) {
534 const ELFSection &S = *SectionList.begin();
535
536 // Align FileOff to whatever the alignment restrictions of the section are.
537 if (S.Align)
538 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
539 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000540 O << (char)0xAB;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000541 O.write((char*)&S.SectionData[0], S.SectionData.size());
542 FileOff += S.SectionData.size();
543
Bill Wendling203d3e42007-01-17 22:22:31 +0000544 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
545 TableOut.outword(S.Type); // sh_type - Section contents & semantics
546 TableOut.outword(S.Flags); // sh_flags - Section flags.
547 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
548 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
549 TableOut.outword(S.Size); // sh_size - The section size.
550 TableOut.outword(S.Link); // sh_link - Section header table index link.
551 TableOut.outword(S.Info); // sh_info - Auxillary information.
552 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
553 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000554
555 SectionList.pop_front();
556 }
557
558 // Align output for the section table.
559 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
560 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000561 O << (char)0xAB;
Jeff Cohen00b168892005-07-27 06:12:32 +0000562
Chris Lattner5f48ff72005-07-16 08:01:13 +0000563 // Emit the section table itself.
564 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000565}