blob: 63fee3a8d74dc871d177c5faf610431c85b50599 [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"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000047#include <list>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000048using namespace llvm;
49
Devang Patel19974732007-05-03 01:11:54 +000050char ELFWriter::ID = 0;
Bill Wendling8f84f1f2007-02-08 01:35:27 +000051/// AddELFWriter - Concrete function to add the ELF writer to the function pass
52/// manager.
Dan Gohmanbfae8312008-03-11 22:29:46 +000053MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000054 std::ostream &O,
55 TargetMachine &TM) {
56 ELFWriter *EW = new ELFWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000057 PM.add(EW);
Bill Wendling8f84f1f2007-02-08 01:35:27 +000058 return &EW->getMachineCodeEmitter();
59}
60
Chris Lattner0e180502005-07-11 06:34:30 +000061//===----------------------------------------------------------------------===//
62// ELFCodeEmitter Implementation
63//===----------------------------------------------------------------------===//
64
Chris Lattneraa507db2005-07-11 05:17:18 +000065namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000066 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
67 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000068 class ELFCodeEmitter : public MachineCodeEmitter {
69 ELFWriter &EW;
Bill Wendling203d3e42007-01-17 22:22:31 +000070 TargetMachine &TM;
Chris Lattner5f48ff72005-07-16 08:01:13 +000071 ELFWriter::ELFSection *ES; // Section to write to.
72 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000073 size_t FnStart;
74 public:
Dan Gohmanded2b0d2007-12-14 15:41:34 +000075 explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000076
Chris Lattner0e180502005-07-11 06:34:30 +000077 void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +000078 bool finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000079
Chris Lattneraa507db2005-07-11 05:17:18 +000080 void addRelocation(const MachineRelocation &MR) {
81 assert(0 && "relo not handled yet!");
82 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000083
84 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
85 }
86
87 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
Chris Lattneraa507db2005-07-11 05:17:18 +000088 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000089 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000090 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000091 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +000092 assert(0 && "JT not implementated yet!");
93 return 0;
94 }
Chris Lattnerf75f9be2006-05-02 23:22:24 +000095
Chris Lattnerb4432f32006-05-03 17:10:41 +000096 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
97 assert(0 && "JT not implementated yet!");
98 return 0;
99 }
Andrew Lenharthfe660392005-07-28 18:13:59 +0000100
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000101 virtual intptr_t getLabelAddress(uint64_t Label) const {
102 assert(0 && "Label address not implementated yet!");
103 abort();
104 return 0;
105 }
106
107 virtual void emitLabel(uint64_t LabelID) {
108 assert(0 && "emit Label not implementated yet!");
109 abort();
110 }
111
112
113 virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
114
115
Chris Lattner0e180502005-07-11 06:34:30 +0000116 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000117 void startFunctionStub(const GlobalValue* F, unsigned StubSize,
118 unsigned Alignment = 1) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000119 assert(0 && "JIT specific function called!");
120 abort();
121 }
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000122 void *finishFunctionStub(const GlobalValue *F) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000123 assert(0 && "JIT specific function called!");
124 abort();
125 return 0;
126 }
127 };
128}
129
Chris Lattner0e180502005-07-11 06:34:30 +0000130/// startFunction - This callback is invoked when a new machine function is
131/// about to be emitted.
132void ELFCodeEmitter::startFunction(MachineFunction &F) {
133 // Align the output buffer to the appropriate alignment.
134 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +0000135 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +0000136 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
137 ELFWriter::ELFSection::SHF_EXECINSTR |
138 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000139 OutBuffer = &ES->SectionData;
Bill Wendling203d3e42007-01-17 22:22:31 +0000140 cerr << "FIXME: This code needs to be updated for changes in the "
141 << "CodeEmitter interfaces. In particular, this should set "
Bill Wendlinge8156192006-12-07 01:30:32 +0000142 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
Chris Lattner43b429b2006-05-02 18:27:26 +0000143 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000144
Chris Lattner0e180502005-07-11 06:34:30 +0000145 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000146 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000147
Chris Lattner0e180502005-07-11 06:34:30 +0000148 // Add padding zeros to the end of the buffer to make sure that the
149 // function will start on the correct byte alignment within the section.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000150 OutputBuffer OB(*OutBuffer,
151 TM.getTargetData()->getPointerSizeInBits() == 64,
152 TM.getTargetData()->isLittleEndian());
Bill Wendling203d3e42007-01-17 22:22:31 +0000153 OB.align(Align);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000154 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000155}
156
157/// finishFunction - This callback is invoked after the function is completely
158/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000159bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000160 // We now know the size of the function, add a symbol to represent it.
161 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000162
Chris Lattner0e180502005-07-11 06:34:30 +0000163 // Figure out the binding (linkage) of the symbol.
164 switch (F.getFunction()->getLinkage()) {
165 default:
166 // appending linkage is illegal for functions.
167 assert(0 && "Unknown linkage type!");
168 case GlobalValue::ExternalLinkage:
169 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
170 break;
171 case GlobalValue::LinkOnceLinkage:
172 case GlobalValue::WeakLinkage:
173 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
174 break;
175 case GlobalValue::InternalLinkage:
176 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
177 break;
178 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000179
180 ES->Size = OutBuffer->size();
181
Chris Lattner0e180502005-07-11 06:34:30 +0000182 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000183 FnSym.SectionIdx = ES->SectionIdx;
Bill Wendling203d3e42007-01-17 22:22:31 +0000184 FnSym.Value = FnStart; // Value = Offset from start of Section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000185 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000186
Chris Lattner0e180502005-07-11 06:34:30 +0000187 // Finally, add it to the symtab.
188 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000189 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000190}
191
192//===----------------------------------------------------------------------===//
193// ELFWriter Implementation
194//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000195
Devang Patel794fd752007-05-01 21:15:47 +0000196ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm)
197 : MachineFunctionPass((intptr_t)&ID), O(o), TM(tm) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000198 e_flags = 0; // e_flags defaults to 0, no flags.
199
Owen Andersona69571c2006-05-03 01:29:57 +0000200 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
201 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000202
203 // Create the machine code emitter object for this target.
Bill Wendlinge9116152007-01-17 09:06:13 +0000204 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000205 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000206}
207
208ELFWriter::~ELFWriter() {
209 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000210}
211
212// doInitialization - Emit the file header and all of the global variables for
213// the module to the ELF file.
214bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000215 Mang = new Mangler(M);
216
Chris Lattner5f48ff72005-07-16 08:01:13 +0000217 // Local alias to shortenify coming code.
218 std::vector<unsigned char> &FH = FileHeader;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000219 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Jeff Cohen00b168892005-07-27 06:12:32 +0000220
Bill Wendling203d3e42007-01-17 22:22:31 +0000221 FHOut.outbyte(0x7F); // EI_MAG0
222 FHOut.outbyte('E'); // EI_MAG1
223 FHOut.outbyte('L'); // EI_MAG2
224 FHOut.outbyte('F'); // EI_MAG3
225 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
226 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
227 FHOut.outbyte(1); // EI_VERSION
Bill Wendlinge9116152007-01-17 09:06:13 +0000228 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000229
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000230 // This should change for shared objects.
Bill Wendling203d3e42007-01-17 22:22:31 +0000231 FHOut.outhalf(1); // e_type = ET_REL
Dan Gohman87c3d412008-05-05 16:48:32 +0000232 FHOut.outhalf(TM.getELFWriterInfo()->getEMachine()); // target-defined
Bill Wendling203d3e42007-01-17 22:22:31 +0000233 FHOut.outword(1); // e_version = 1
234 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
235 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000236
Chris Lattner5f48ff72005-07-16 08:01:13 +0000237 ELFHeader_e_shoff_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000238 FHOut.outaddr(0); // e_shoff
239 FHOut.outword(e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000240
Bill Wendling203d3e42007-01-17 22:22:31 +0000241 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
242 FHOut.outhalf(0); // e_phentsize = prog header entry size
243 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
244 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000245
Jeff Cohen00b168892005-07-27 06:12:32 +0000246
Chris Lattner5f48ff72005-07-16 08:01:13 +0000247 ELFHeader_e_shnum_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000248 FHOut.outhalf(0); // e_shnum = # of section header ents
Chris Lattner5f48ff72005-07-16 08:01:13 +0000249 ELFHeader_e_shstrndx_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000250 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000251
Chris Lattner5f48ff72005-07-16 08:01:13 +0000252 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000253 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000254
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000255 // Start up the symbol table. The first entry in the symtab is the null
256 // entry.
257 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000258
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000259 return false;
260}
261
Chris Lattner05895232005-07-16 17:41:06 +0000262void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000263 // If this is an external global, emit it now. TODO: Note that it would be
264 // better to ignore the symbol here and only add it to the symbol table if
265 // referenced.
266 if (!GV->hasInitializer()) {
267 ELFSym ExternalSym(GV);
268 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
269 ExternalSym.SetType(ELFSym::STT_NOTYPE);
270 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
271 SymbolTable.push_back(ExternalSym);
272 return;
273 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000274
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000275 const Type *GVType = (const Type*)GV->getType();
Duncan Sandsd1025932008-01-29 06:23:44 +0000276 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
Duncan Sandsca0ed742007-11-05 00:04:43 +0000277 unsigned Size = TM.getTargetData()->getABITypeSize(GVType);
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000278
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000279 // If this global has a zero initializer, it is part of the .bss or common
280 // section.
281 if (GV->getInitializer()->isNullValue()) {
282 // If this global is part of the common block, add it now. Variables are
283 // part of the common block if they are zero initialized and allowed to be
284 // merged with other symbols.
285 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
286 ELFSym CommonSym(GV);
287 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000288 CommonSym.Value = Align;
289 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000290 CommonSym.SetBind(ELFSym::STB_GLOBAL);
291 CommonSym.SetType(ELFSym::STT_OBJECT);
292 // TODO SOMEDAY: add ELF visibility.
293 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
294 SymbolTable.push_back(CommonSym);
295 return;
296 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000297
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000298 // Otherwise, this symbol is part of the .bss section. Emit it now.
299
300 // Handle alignment. Ensure section is aligned at least as much as required
301 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000302 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000303 BSSSection.Align = std::max(BSSSection.Align, Align);
304
305 // Within the section, emit enough virtual padding to get us to an alignment
306 // boundary.
307 if (Align)
308 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
309
310 ELFSym BSSSym(GV);
311 BSSSym.Value = BSSSection.Size;
312 BSSSym.Size = Size;
313 BSSSym.SetType(ELFSym::STT_OBJECT);
314
315 switch (GV->getLinkage()) {
316 default: // weak/linkonce handled above
317 assert(0 && "Unexpected linkage type!");
318 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
319 case GlobalValue::ExternalLinkage:
320 BSSSym.SetBind(ELFSym::STB_GLOBAL);
321 break;
322 case GlobalValue::InternalLinkage:
323 BSSSym.SetBind(ELFSym::STB_LOCAL);
324 break;
325 }
326
327 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000328 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000329 SymbolTable.push_back(BSSSym);
330
331 // Reserve space in the .bss section for this symbol.
332 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000333 return;
334 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000335
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000336 // FIXME: handle .rodata
337 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000338
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000339 // FIXME: handle .data
340 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000341}
342
343
344bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000345 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000346 return false;
347}
348
349/// doFinalization - Now that the module has been completely processed, emit
350/// the ELF file to 'O'.
351bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000352 // Okay, the ELF header and .text sections have been completed, build the
353 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000354 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
355 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000356 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000357
358 // Emit the symbol table now, if non-empty.
359 EmitSymbolTable();
360
361 // FIXME: Emit the relocations now.
362
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000363 // Emit the string table for the sections in the ELF file we have.
364 EmitSectionTableStringTable();
365
Chris Lattner5f48ff72005-07-16 08:01:13 +0000366 // Emit the sections to the .o file, and emit the section table for the file.
367 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000368
Chris Lattner5f48ff72005-07-16 08:01:13 +0000369 // We are done with the abstract symbols.
370 SectionList.clear();
371 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000372
373 // Release the name mangler object.
374 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000375 return false;
376}
377
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000378/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
379/// table for it and then the symbol table itself.
380void ELFWriter::EmitSymbolTable() {
381 if (SymbolTable.size() == 1) return; // Only the null entry.
382
383 // FIXME: compact all local symbols to the start of the symtab.
384 unsigned FirstNonLocalSymbol = 1;
385
Chris Lattner00033952005-07-16 17:36:04 +0000386 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000387 StrTab.Align = 1;
388
Chris Lattner5f48ff72005-07-16 08:01:13 +0000389 DataBuffer &StrTabBuf = StrTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000390 OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000391
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000392 // Set the zero'th symbol to a null byte, as required.
Bill Wendling203d3e42007-01-17 22:22:31 +0000393 StrTabOut.outbyte(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000394 SymbolTable[0].NameIdx = 0;
395 unsigned Index = 1;
396 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000397 // Use the name mangler to uniquify the LLVM symbol.
398 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000399
400 if (Name.empty()) {
401 SymbolTable[i].NameIdx = 0;
402 } else {
403 SymbolTable[i].NameIdx = Index;
404
405 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000406 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000407
408 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000409 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000410
411 // Keep track of the number of bytes emitted to this section.
412 Index += Name.size()+1;
413 }
414 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000415 assert(Index == StrTabBuf.size());
416 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000417
418 // Now that we have emitted the string table and know the offset into the
419 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000420 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000421 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000422 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000423 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
424 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000425 DataBuffer &SymTabBuf = SymTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000426 OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000427
Chris Lattner46c53052005-07-12 06:57:52 +0000428 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
429 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
430 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000431 SymTabOut.outword(Sym.NameIdx);
432 SymTabOut.outaddr32(Sym.Value);
433 SymTabOut.outword(Sym.Size);
434 SymTabOut.outbyte(Sym.Info);
435 SymTabOut.outbyte(Sym.Other);
436 SymTabOut.outhalf(Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000437 }
438 } else {
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.outbyte(Sym.Info);
443 SymTabOut.outbyte(Sym.Other);
444 SymTabOut.outhalf(Sym.SectionIdx);
445 SymTabOut.outaddr64(Sym.Value);
446 SymTabOut.outxword(Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000447 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000448 }
449
Chris Lattner5f48ff72005-07-16 08:01:13 +0000450 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000451}
452
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000453/// EmitSectionTableStringTable - This method adds and emits a section for the
454/// ELF Section Table string table: the string table that holds all of the
455/// section names.
456void ELFWriter::EmitSectionTableStringTable() {
457 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000458 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000459
460 // Now that we know which section number is the .shstrtab section, update the
461 // e_shstrndx entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000462 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000463 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000464
465 // Set the NameIdx of each section in the string table and emit the bytes for
466 // the string table.
467 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000468 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000469
Chris Lattner5f48ff72005-07-16 08:01:13 +0000470 for (std::list<ELFSection>::iterator I = SectionList.begin(),
471 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000472 // Set the index into the table. Note if we have lots of entries with
473 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000474 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000475
476 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000477 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
478
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000479 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000480 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000481
482 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000483 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000484 }
485
486 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000487 assert(Index == Buf.size());
488 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000489}
490
Chris Lattner5f48ff72005-07-16 08:01:13 +0000491/// OutputSectionsAndSectionTable - Now that we have constructed the file header
492/// and all of the sections, emit these to the ostream destination and emit the
493/// SectionTable.
494void ELFWriter::OutputSectionsAndSectionTable() {
495 // Pass #1: Compute the file offset for each section.
496 size_t FileOff = FileHeader.size(); // File header first.
497
498 // Emit all of the section data in order.
499 for (std::list<ELFSection>::iterator I = SectionList.begin(),
500 E = SectionList.end(); I != E; ++I) {
501 // Align FileOff to whatever the alignment restrictions of the section are.
502 if (I->Align)
503 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
504 I->Offset = FileOff;
505 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000506 }
507
Chris Lattner5f48ff72005-07-16 08:01:13 +0000508 // Align Section Header.
509 unsigned TableAlign = is64Bit ? 8 : 4;
510 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
511
512 // Now that we know where all of the sections will be emitted, set the e_shnum
513 // entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000514 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000515 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000516
Chris Lattner5f48ff72005-07-16 08:01:13 +0000517 // Now that we know the offset in the file of the section table, update the
518 // e_shoff address in the ELF header.
Bill Wendling203d3e42007-01-17 22:22:31 +0000519 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000520
Chris Lattner5f48ff72005-07-16 08:01:13 +0000521 // Now that we know all of the data in the file header, emit it and all of the
522 // sections!
523 O.write((char*)&FileHeader[0], FileHeader.size());
524 FileOff = FileHeader.size();
525 DataBuffer().swap(FileHeader);
526
527 DataBuffer Table;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000528 OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000529
530 // Emit all of the section data and build the section table itself.
531 while (!SectionList.empty()) {
532 const ELFSection &S = *SectionList.begin();
533
534 // Align FileOff to whatever the alignment restrictions of the section are.
535 if (S.Align)
536 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
537 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000538 O.put((char)0xAB);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000539 O.write((char*)&S.SectionData[0], S.SectionData.size());
540 FileOff += S.SectionData.size();
541
Bill Wendling203d3e42007-01-17 22:22:31 +0000542 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
543 TableOut.outword(S.Type); // sh_type - Section contents & semantics
544 TableOut.outword(S.Flags); // sh_flags - Section flags.
545 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
546 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
547 TableOut.outword(S.Size); // sh_size - The section size.
548 TableOut.outword(S.Link); // sh_link - Section header table index link.
549 TableOut.outword(S.Info); // sh_info - Auxillary information.
550 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
551 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000552
553 SectionList.pop_front();
554 }
555
556 // Align output for the section table.
557 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
558 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000559 O.put((char)0xAB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000560
Chris Lattner5f48ff72005-07-16 08:01:13 +0000561 // Emit the section table itself.
562 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000563}