blob: ffc0a929f318d67ef2ac7dea84a44d2719c918be [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 Lattner00033952005-07-16 17:36:04 +00005// This file was developed by Chris Lattner and is distributed under the
6// University of Illinois Open Source 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
Bill Wendling8f84f1f2007-02-08 01:35:27 +000050/// AddELFWriter - Concrete function to add the ELF writer to the function pass
51/// manager.
52MachineCodeEmitter *llvm::AddELFWriter(FunctionPassManager &FPM,
53 std::ostream &O,
54 TargetMachine &TM) {
55 ELFWriter *EW = new ELFWriter(O, TM);
56 FPM.add(EW);
57 return &EW->getMachineCodeEmitter();
58}
59
Chris Lattner0e180502005-07-11 06:34:30 +000060//===----------------------------------------------------------------------===//
61// ELFCodeEmitter Implementation
62//===----------------------------------------------------------------------===//
63
Chris Lattneraa507db2005-07-11 05:17:18 +000064namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000065 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
66 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000067 class ELFCodeEmitter : public MachineCodeEmitter {
68 ELFWriter &EW;
Bill Wendling203d3e42007-01-17 22:22:31 +000069 TargetMachine &TM;
Chris Lattner5f48ff72005-07-16 08:01:13 +000070 ELFWriter::ELFSection *ES; // Section to write to.
71 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000072 size_t FnStart;
73 public:
Bill Wendling203d3e42007-01-17 22:22:31 +000074 ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000075
Chris Lattner0e180502005-07-11 06:34:30 +000076 void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +000077 bool finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000078
Chris Lattneraa507db2005-07-11 05:17:18 +000079 void addRelocation(const MachineRelocation &MR) {
80 assert(0 && "relo not handled yet!");
81 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000082
83 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
84 }
85
86 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
Chris Lattneraa507db2005-07-11 05:17:18 +000087 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000088 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000089 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000090 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +000091 assert(0 && "JT not implementated yet!");
92 return 0;
93 }
Chris Lattnerf75f9be2006-05-02 23:22:24 +000094
Chris Lattnerb4432f32006-05-03 17:10:41 +000095 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
96 assert(0 && "JT not implementated yet!");
97 return 0;
98 }
Andrew Lenharthfe660392005-07-28 18:13:59 +000099
Chris Lattner0e180502005-07-11 06:34:30 +0000100 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Evan Chengce9a5762006-11-16 20:04:04 +0000101 void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000102 assert(0 && "JIT specific function called!");
103 abort();
104 }
105 void *finishFunctionStub(const Function *F) {
106 assert(0 && "JIT specific function called!");
107 abort();
108 return 0;
109 }
110 };
111}
112
Chris Lattner0e180502005-07-11 06:34:30 +0000113/// startFunction - This callback is invoked when a new machine function is
114/// about to be emitted.
115void ELFCodeEmitter::startFunction(MachineFunction &F) {
116 // Align the output buffer to the appropriate alignment.
117 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +0000118 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +0000119 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
120 ELFWriter::ELFSection::SHF_EXECINSTR |
121 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000122 OutBuffer = &ES->SectionData;
Bill Wendling203d3e42007-01-17 22:22:31 +0000123 cerr << "FIXME: This code needs to be updated for changes in the "
124 << "CodeEmitter interfaces. In particular, this should set "
Bill Wendlinge8156192006-12-07 01:30:32 +0000125 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
Chris Lattner43b429b2006-05-02 18:27:26 +0000126 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000127
Chris Lattner0e180502005-07-11 06:34:30 +0000128 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000129 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000130
Chris Lattner0e180502005-07-11 06:34:30 +0000131 // Add padding zeros to the end of the buffer to make sure that the
132 // function will start on the correct byte alignment within the section.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000133 OutputBuffer OB(*OutBuffer,
134 TM.getTargetData()->getPointerSizeInBits() == 64,
135 TM.getTargetData()->isLittleEndian());
Bill Wendling203d3e42007-01-17 22:22:31 +0000136 OB.align(Align);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000137 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000138}
139
140/// finishFunction - This callback is invoked after the function is completely
141/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000142bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000143 // We now know the size of the function, add a symbol to represent it.
144 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000145
Chris Lattner0e180502005-07-11 06:34:30 +0000146 // Figure out the binding (linkage) of the symbol.
147 switch (F.getFunction()->getLinkage()) {
148 default:
149 // appending linkage is illegal for functions.
150 assert(0 && "Unknown linkage type!");
151 case GlobalValue::ExternalLinkage:
152 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
153 break;
154 case GlobalValue::LinkOnceLinkage:
155 case GlobalValue::WeakLinkage:
156 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
157 break;
158 case GlobalValue::InternalLinkage:
159 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
160 break;
161 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000162
163 ES->Size = OutBuffer->size();
164
Chris Lattner0e180502005-07-11 06:34:30 +0000165 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000166 FnSym.SectionIdx = ES->SectionIdx;
Bill Wendling203d3e42007-01-17 22:22:31 +0000167 FnSym.Value = FnStart; // Value = Offset from start of Section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000168 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000169
Chris Lattner0e180502005-07-11 06:34:30 +0000170 // Finally, add it to the symtab.
171 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000172 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000173}
174
175//===----------------------------------------------------------------------===//
176// ELFWriter Implementation
177//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000178
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000179ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000180 e_flags = 0; // e_flags defaults to 0, no flags.
181
Owen Andersona69571c2006-05-03 01:29:57 +0000182 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
183 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000184
185 // Create the machine code emitter object for this target.
Bill Wendlinge9116152007-01-17 09:06:13 +0000186 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000187 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000188}
189
190ELFWriter::~ELFWriter() {
191 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000192}
193
194// doInitialization - Emit the file header and all of the global variables for
195// the module to the ELF file.
196bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000197 Mang = new Mangler(M);
198
Chris Lattner5f48ff72005-07-16 08:01:13 +0000199 // Local alias to shortenify coming code.
200 std::vector<unsigned char> &FH = FileHeader;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000201 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Jeff Cohen00b168892005-07-27 06:12:32 +0000202
Bill Wendling203d3e42007-01-17 22:22:31 +0000203 FHOut.outbyte(0x7F); // EI_MAG0
204 FHOut.outbyte('E'); // EI_MAG1
205 FHOut.outbyte('L'); // EI_MAG2
206 FHOut.outbyte('F'); // EI_MAG3
207 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
208 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
209 FHOut.outbyte(1); // EI_VERSION
Bill Wendlinge9116152007-01-17 09:06:13 +0000210 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000211
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000212 // This should change for shared objects.
Bill Wendling203d3e42007-01-17 22:22:31 +0000213 FHOut.outhalf(1); // e_type = ET_REL
Bill Wendling5d73a2a2007-01-27 02:55:44 +0000214 FHOut.outword(TM.getELFWriterInfo()->getEMachine()); // target-defined
Bill Wendling203d3e42007-01-17 22:22:31 +0000215 FHOut.outword(1); // e_version = 1
216 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
217 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000218
Chris Lattner5f48ff72005-07-16 08:01:13 +0000219 ELFHeader_e_shoff_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000220 FHOut.outaddr(0); // e_shoff
221 FHOut.outword(e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000222
Bill Wendling203d3e42007-01-17 22:22:31 +0000223 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
224 FHOut.outhalf(0); // e_phentsize = prog header entry size
225 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
226 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000227
Jeff Cohen00b168892005-07-27 06:12:32 +0000228
Chris Lattner5f48ff72005-07-16 08:01:13 +0000229 ELFHeader_e_shnum_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000230 FHOut.outhalf(0); // e_shnum = # of section header ents
Chris Lattner5f48ff72005-07-16 08:01:13 +0000231 ELFHeader_e_shstrndx_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000232 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000233
Chris Lattner5f48ff72005-07-16 08:01:13 +0000234 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000235 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000236
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000237 // Start up the symbol table. The first entry in the symtab is the null
238 // entry.
239 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000240
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000241 return false;
242}
243
Chris Lattner05895232005-07-16 17:41:06 +0000244void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000245 // If this is an external global, emit it now. TODO: Note that it would be
246 // better to ignore the symbol here and only add it to the symbol table if
247 // referenced.
248 if (!GV->hasInitializer()) {
249 ELFSym ExternalSym(GV);
250 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
251 ExternalSym.SetType(ELFSym::STT_NOTYPE);
252 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
253 SymbolTable.push_back(ExternalSym);
254 return;
255 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000256
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000257 const Type *GVType = (const Type*)GV->getType();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000258 unsigned Align = TM.getTargetData()->getPrefTypeAlignment(GVType);
Owen Andersona69571c2006-05-03 01:29:57 +0000259 unsigned Size = TM.getTargetData()->getTypeSize(GVType);
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000260
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000261 // If this global has a zero initializer, it is part of the .bss or common
262 // section.
263 if (GV->getInitializer()->isNullValue()) {
264 // If this global is part of the common block, add it now. Variables are
265 // part of the common block if they are zero initialized and allowed to be
266 // merged with other symbols.
267 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
268 ELFSym CommonSym(GV);
269 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000270 CommonSym.Value = Align;
271 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000272 CommonSym.SetBind(ELFSym::STB_GLOBAL);
273 CommonSym.SetType(ELFSym::STT_OBJECT);
274 // TODO SOMEDAY: add ELF visibility.
275 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
276 SymbolTable.push_back(CommonSym);
277 return;
278 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000279
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000280 // Otherwise, this symbol is part of the .bss section. Emit it now.
281
282 // Handle alignment. Ensure section is aligned at least as much as required
283 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000284 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000285 BSSSection.Align = std::max(BSSSection.Align, Align);
286
287 // Within the section, emit enough virtual padding to get us to an alignment
288 // boundary.
289 if (Align)
290 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
291
292 ELFSym BSSSym(GV);
293 BSSSym.Value = BSSSection.Size;
294 BSSSym.Size = Size;
295 BSSSym.SetType(ELFSym::STT_OBJECT);
296
297 switch (GV->getLinkage()) {
298 default: // weak/linkonce handled above
299 assert(0 && "Unexpected linkage type!");
300 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
301 case GlobalValue::ExternalLinkage:
302 BSSSym.SetBind(ELFSym::STB_GLOBAL);
303 break;
304 case GlobalValue::InternalLinkage:
305 BSSSym.SetBind(ELFSym::STB_LOCAL);
306 break;
307 }
308
309 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000310 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000311 SymbolTable.push_back(BSSSym);
312
313 // Reserve space in the .bss section for this symbol.
314 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000315 return;
316 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000317
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000318 // FIXME: handle .rodata
319 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000320
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000321 // FIXME: handle .data
322 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000323}
324
325
326bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000327 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000328 return false;
329}
330
331/// doFinalization - Now that the module has been completely processed, emit
332/// the ELF file to 'O'.
333bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000334 // Okay, the ELF header and .text sections have been completed, build the
335 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000336 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
337 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000338 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000339
340 // Emit the symbol table now, if non-empty.
341 EmitSymbolTable();
342
343 // FIXME: Emit the relocations now.
344
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000345 // Emit the string table for the sections in the ELF file we have.
346 EmitSectionTableStringTable();
347
Chris Lattner5f48ff72005-07-16 08:01:13 +0000348 // Emit the sections to the .o file, and emit the section table for the file.
349 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000350
Chris Lattner5f48ff72005-07-16 08:01:13 +0000351 // We are done with the abstract symbols.
352 SectionList.clear();
353 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000354
355 // Release the name mangler object.
356 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000357 return false;
358}
359
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000360/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
361/// table for it and then the symbol table itself.
362void ELFWriter::EmitSymbolTable() {
363 if (SymbolTable.size() == 1) return; // Only the null entry.
364
365 // FIXME: compact all local symbols to the start of the symtab.
366 unsigned FirstNonLocalSymbol = 1;
367
Chris Lattner00033952005-07-16 17:36:04 +0000368 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000369 StrTab.Align = 1;
370
Chris Lattner5f48ff72005-07-16 08:01:13 +0000371 DataBuffer &StrTabBuf = StrTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000372 OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000373
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000374 // Set the zero'th symbol to a null byte, as required.
Bill Wendling203d3e42007-01-17 22:22:31 +0000375 StrTabOut.outbyte(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000376 SymbolTable[0].NameIdx = 0;
377 unsigned Index = 1;
378 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000379 // Use the name mangler to uniquify the LLVM symbol.
380 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000381
382 if (Name.empty()) {
383 SymbolTable[i].NameIdx = 0;
384 } else {
385 SymbolTable[i].NameIdx = Index;
386
387 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000388 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000389
390 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000391 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000392
393 // Keep track of the number of bytes emitted to this section.
394 Index += Name.size()+1;
395 }
396 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000397 assert(Index == StrTabBuf.size());
398 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000399
400 // Now that we have emitted the string table and know the offset into the
401 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000402 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000403 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000404 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000405 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
406 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000407 DataBuffer &SymTabBuf = SymTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000408 OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000409
Chris Lattner46c53052005-07-12 06:57:52 +0000410 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
411 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
412 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000413 SymTabOut.outword(Sym.NameIdx);
414 SymTabOut.outaddr32(Sym.Value);
415 SymTabOut.outword(Sym.Size);
416 SymTabOut.outbyte(Sym.Info);
417 SymTabOut.outbyte(Sym.Other);
418 SymTabOut.outhalf(Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000419 }
420 } else {
421 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
422 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000423 SymTabOut.outword(Sym.NameIdx);
424 SymTabOut.outbyte(Sym.Info);
425 SymTabOut.outbyte(Sym.Other);
426 SymTabOut.outhalf(Sym.SectionIdx);
427 SymTabOut.outaddr64(Sym.Value);
428 SymTabOut.outxword(Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000429 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000430 }
431
Chris Lattner5f48ff72005-07-16 08:01:13 +0000432 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000433}
434
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000435/// EmitSectionTableStringTable - This method adds and emits a section for the
436/// ELF Section Table string table: the string table that holds all of the
437/// section names.
438void ELFWriter::EmitSectionTableStringTable() {
439 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000440 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000441
442 // Now that we know which section number is the .shstrtab section, update the
443 // e_shstrndx entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000444 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000445 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000446
447 // Set the NameIdx of each section in the string table and emit the bytes for
448 // the string table.
449 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000450 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000451
Chris Lattner5f48ff72005-07-16 08:01:13 +0000452 for (std::list<ELFSection>::iterator I = SectionList.begin(),
453 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000454 // Set the index into the table. Note if we have lots of entries with
455 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000456 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000457
458 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000459 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
460
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000461 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000462 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000463
464 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000465 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000466 }
467
468 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000469 assert(Index == Buf.size());
470 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000471}
472
Chris Lattner5f48ff72005-07-16 08:01:13 +0000473/// OutputSectionsAndSectionTable - Now that we have constructed the file header
474/// and all of the sections, emit these to the ostream destination and emit the
475/// SectionTable.
476void ELFWriter::OutputSectionsAndSectionTable() {
477 // Pass #1: Compute the file offset for each section.
478 size_t FileOff = FileHeader.size(); // File header first.
479
480 // Emit all of the section data in order.
481 for (std::list<ELFSection>::iterator I = SectionList.begin(),
482 E = SectionList.end(); I != E; ++I) {
483 // Align FileOff to whatever the alignment restrictions of the section are.
484 if (I->Align)
485 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
486 I->Offset = FileOff;
487 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000488 }
489
Chris Lattner5f48ff72005-07-16 08:01:13 +0000490 // Align Section Header.
491 unsigned TableAlign = is64Bit ? 8 : 4;
492 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
493
494 // Now that we know where all of the sections will be emitted, set the e_shnum
495 // entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000496 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000497 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000498
Chris Lattner5f48ff72005-07-16 08:01:13 +0000499 // Now that we know the offset in the file of the section table, update the
500 // e_shoff address in the ELF header.
Bill Wendling203d3e42007-01-17 22:22:31 +0000501 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000502
Chris Lattner5f48ff72005-07-16 08:01:13 +0000503 // Now that we know all of the data in the file header, emit it and all of the
504 // sections!
505 O.write((char*)&FileHeader[0], FileHeader.size());
506 FileOff = FileHeader.size();
507 DataBuffer().swap(FileHeader);
508
509 DataBuffer Table;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000510 OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000511
512 // Emit all of the section data and build the section table itself.
513 while (!SectionList.empty()) {
514 const ELFSection &S = *SectionList.begin();
515
516 // Align FileOff to whatever the alignment restrictions of the section are.
517 if (S.Align)
518 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
519 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000520 O.put((char)0xAB);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000521 O.write((char*)&S.SectionData[0], S.SectionData.size());
522 FileOff += S.SectionData.size();
523
Bill Wendling203d3e42007-01-17 22:22:31 +0000524 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
525 TableOut.outword(S.Type); // sh_type - Section contents & semantics
526 TableOut.outword(S.Flags); // sh_flags - Section flags.
527 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
528 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
529 TableOut.outword(S.Size); // sh_size - The section size.
530 TableOut.outword(S.Link); // sh_link - Section header table index link.
531 TableOut.outword(S.Info); // sh_info - Auxillary information.
532 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
533 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000534
535 SectionList.pop_front();
536 }
537
538 // Align output for the section table.
539 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
540 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000541 O.put((char)0xAB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000542
Chris Lattner5f48ff72005-07-16 08:01:13 +0000543 // Emit the section table itself.
544 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000545}