blob: 3b9653740cc02d47419f3b286f5342569902fc83 [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
34#include "llvm/CodeGen/ELFWriter.h"
35#include "llvm/Module.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000036#include "llvm/CodeGen/MachineCodeEmitter.h"
37#include "llvm/CodeGen/MachineConstantPool.h"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000039#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000040#include "llvm/Support/Mangler.h"
Bill Wendling203d3e42007-01-17 22:22:31 +000041#include "llvm/Support/OutputBuffer.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000042#include "llvm/Support/Streams.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000043using namespace llvm;
44
Chris Lattner0e180502005-07-11 06:34:30 +000045//===----------------------------------------------------------------------===//
46// ELFCodeEmitter Implementation
47//===----------------------------------------------------------------------===//
48
Chris Lattneraa507db2005-07-11 05:17:18 +000049namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000050 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
51 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000052 class ELFCodeEmitter : public MachineCodeEmitter {
53 ELFWriter &EW;
Bill Wendling203d3e42007-01-17 22:22:31 +000054 TargetMachine &TM;
Chris Lattner5f48ff72005-07-16 08:01:13 +000055 ELFWriter::ELFSection *ES; // Section to write to.
56 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000057 size_t FnStart;
58 public:
Bill Wendling203d3e42007-01-17 22:22:31 +000059 ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000060
Chris Lattner0e180502005-07-11 06:34:30 +000061 void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +000062 bool finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000063
Chris Lattneraa507db2005-07-11 05:17:18 +000064 void addRelocation(const MachineRelocation &MR) {
65 assert(0 && "relo not handled yet!");
66 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000067
68 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
69 }
70
71 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
Chris Lattneraa507db2005-07-11 05:17:18 +000072 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000073 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000074 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000075 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +000076 assert(0 && "JT not implementated yet!");
77 return 0;
78 }
Chris Lattnerf75f9be2006-05-02 23:22:24 +000079
Chris Lattnerb4432f32006-05-03 17:10:41 +000080 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
81 assert(0 && "JT not implementated yet!");
82 return 0;
83 }
Andrew Lenharthfe660392005-07-28 18:13:59 +000084
Chris Lattner0e180502005-07-11 06:34:30 +000085 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Evan Chengce9a5762006-11-16 20:04:04 +000086 void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
Chris Lattneraa507db2005-07-11 05:17:18 +000087 assert(0 && "JIT specific function called!");
88 abort();
89 }
90 void *finishFunctionStub(const Function *F) {
91 assert(0 && "JIT specific function called!");
92 abort();
93 return 0;
94 }
95 };
96}
97
Chris Lattner0e180502005-07-11 06:34:30 +000098/// startFunction - This callback is invoked when a new machine function is
99/// about to be emitted.
100void ELFCodeEmitter::startFunction(MachineFunction &F) {
101 // Align the output buffer to the appropriate alignment.
102 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +0000103 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +0000104 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
105 ELFWriter::ELFSection::SHF_EXECINSTR |
106 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000107 OutBuffer = &ES->SectionData;
Bill Wendling203d3e42007-01-17 22:22:31 +0000108 cerr << "FIXME: This code needs to be updated for changes in the "
109 << "CodeEmitter interfaces. In particular, this should set "
Bill Wendlinge8156192006-12-07 01:30:32 +0000110 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
Chris Lattner43b429b2006-05-02 18:27:26 +0000111 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000112
Chris Lattner0e180502005-07-11 06:34:30 +0000113 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000114 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000115
Chris Lattner0e180502005-07-11 06:34:30 +0000116 // Add padding zeros to the end of the buffer to make sure that the
117 // function will start on the correct byte alignment within the section.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000118 OutputBuffer OB(*OutBuffer,
119 TM.getTargetData()->getPointerSizeInBits() == 64,
120 TM.getTargetData()->isLittleEndian());
Bill Wendling203d3e42007-01-17 22:22:31 +0000121 OB.align(Align);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000122 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000123}
124
125/// finishFunction - This callback is invoked after the function is completely
126/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000127bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000128 // We now know the size of the function, add a symbol to represent it.
129 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000130
Chris Lattner0e180502005-07-11 06:34:30 +0000131 // Figure out the binding (linkage) of the symbol.
132 switch (F.getFunction()->getLinkage()) {
133 default:
134 // appending linkage is illegal for functions.
135 assert(0 && "Unknown linkage type!");
136 case GlobalValue::ExternalLinkage:
137 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
138 break;
139 case GlobalValue::LinkOnceLinkage:
140 case GlobalValue::WeakLinkage:
141 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
142 break;
143 case GlobalValue::InternalLinkage:
144 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
145 break;
146 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000147
148 ES->Size = OutBuffer->size();
149
Chris Lattner0e180502005-07-11 06:34:30 +0000150 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000151 FnSym.SectionIdx = ES->SectionIdx;
Bill Wendling203d3e42007-01-17 22:22:31 +0000152 FnSym.Value = FnStart; // Value = Offset from start of Section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000153 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000154
Chris Lattner0e180502005-07-11 06:34:30 +0000155 // Finally, add it to the symtab.
156 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000157 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000158}
159
160//===----------------------------------------------------------------------===//
161// ELFWriter Implementation
162//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000163
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000164ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
165 e_machine = 0; // e_machine defaults to 'No Machine'
166 e_flags = 0; // e_flags defaults to 0, no flags.
167
Owen Andersona69571c2006-05-03 01:29:57 +0000168 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
169 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000170
171 // Create the machine code emitter object for this target.
Bill Wendlinge9116152007-01-17 09:06:13 +0000172 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000173 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000174}
175
176ELFWriter::~ELFWriter() {
177 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000178}
179
180// doInitialization - Emit the file header and all of the global variables for
181// the module to the ELF file.
182bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000183 Mang = new Mangler(M);
184
Chris Lattner5f48ff72005-07-16 08:01:13 +0000185 // Local alias to shortenify coming code.
186 std::vector<unsigned char> &FH = FileHeader;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000187 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Jeff Cohen00b168892005-07-27 06:12:32 +0000188
Bill Wendling203d3e42007-01-17 22:22:31 +0000189 FHOut.outbyte(0x7F); // EI_MAG0
190 FHOut.outbyte('E'); // EI_MAG1
191 FHOut.outbyte('L'); // EI_MAG2
192 FHOut.outbyte('F'); // EI_MAG3
193 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
194 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
195 FHOut.outbyte(1); // EI_VERSION
Bill Wendlinge9116152007-01-17 09:06:13 +0000196 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000197
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000198 // This should change for shared objects.
Bill Wendling203d3e42007-01-17 22:22:31 +0000199 FHOut.outhalf(1); // e_type = ET_REL
200 FHOut.outhalf(e_machine); // e_machine = whatever the target wants
201 FHOut.outword(1); // e_version = 1
202 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
203 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000204
Chris Lattner5f48ff72005-07-16 08:01:13 +0000205 ELFHeader_e_shoff_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000206 FHOut.outaddr(0); // e_shoff
207 FHOut.outword(e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000208
Bill Wendling203d3e42007-01-17 22:22:31 +0000209 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
210 FHOut.outhalf(0); // e_phentsize = prog header entry size
211 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
212 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000213
Jeff Cohen00b168892005-07-27 06:12:32 +0000214
Chris Lattner5f48ff72005-07-16 08:01:13 +0000215 ELFHeader_e_shnum_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000216 FHOut.outhalf(0); // e_shnum = # of section header ents
Chris Lattner5f48ff72005-07-16 08:01:13 +0000217 ELFHeader_e_shstrndx_Offset = FH.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000218 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000219
Chris Lattner5f48ff72005-07-16 08:01:13 +0000220 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000221 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000222
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000223 // Start up the symbol table. The first entry in the symtab is the null
224 // entry.
225 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000226
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000227 return false;
228}
229
Chris Lattner05895232005-07-16 17:41:06 +0000230void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000231 // If this is an external global, emit it now. TODO: Note that it would be
232 // better to ignore the symbol here and only add it to the symbol table if
233 // referenced.
234 if (!GV->hasInitializer()) {
235 ELFSym ExternalSym(GV);
236 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
237 ExternalSym.SetType(ELFSym::STT_NOTYPE);
238 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
239 SymbolTable.push_back(ExternalSym);
240 return;
241 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000242
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000243 const Type *GVType = (const Type*)GV->getType();
Chris Lattner58092e32007-01-20 22:35:55 +0000244 unsigned Align = TM.getTargetData()->getTypeAlignmentPref(GVType);
Owen Andersona69571c2006-05-03 01:29:57 +0000245 unsigned Size = TM.getTargetData()->getTypeSize(GVType);
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000246
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000247 // If this global has a zero initializer, it is part of the .bss or common
248 // section.
249 if (GV->getInitializer()->isNullValue()) {
250 // If this global is part of the common block, add it now. Variables are
251 // part of the common block if they are zero initialized and allowed to be
252 // merged with other symbols.
253 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
254 ELFSym CommonSym(GV);
255 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000256 CommonSym.Value = Align;
257 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000258 CommonSym.SetBind(ELFSym::STB_GLOBAL);
259 CommonSym.SetType(ELFSym::STT_OBJECT);
260 // TODO SOMEDAY: add ELF visibility.
261 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
262 SymbolTable.push_back(CommonSym);
263 return;
264 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000265
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000266 // Otherwise, this symbol is part of the .bss section. Emit it now.
267
268 // Handle alignment. Ensure section is aligned at least as much as required
269 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000270 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000271 BSSSection.Align = std::max(BSSSection.Align, Align);
272
273 // Within the section, emit enough virtual padding to get us to an alignment
274 // boundary.
275 if (Align)
276 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
277
278 ELFSym BSSSym(GV);
279 BSSSym.Value = BSSSection.Size;
280 BSSSym.Size = Size;
281 BSSSym.SetType(ELFSym::STT_OBJECT);
282
283 switch (GV->getLinkage()) {
284 default: // weak/linkonce handled above
285 assert(0 && "Unexpected linkage type!");
286 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
287 case GlobalValue::ExternalLinkage:
288 BSSSym.SetBind(ELFSym::STB_GLOBAL);
289 break;
290 case GlobalValue::InternalLinkage:
291 BSSSym.SetBind(ELFSym::STB_LOCAL);
292 break;
293 }
294
295 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000296 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000297 SymbolTable.push_back(BSSSym);
298
299 // Reserve space in the .bss section for this symbol.
300 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000301 return;
302 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000303
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000304 // FIXME: handle .rodata
305 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000306
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000307 // FIXME: handle .data
308 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000309}
310
311
312bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000313 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000314 return false;
315}
316
317/// doFinalization - Now that the module has been completely processed, emit
318/// the ELF file to 'O'.
319bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000320 // Okay, the ELF header and .text sections have been completed, build the
321 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000322 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
323 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000324 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000325
326 // Emit the symbol table now, if non-empty.
327 EmitSymbolTable();
328
329 // FIXME: Emit the relocations now.
330
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000331 // Emit the string table for the sections in the ELF file we have.
332 EmitSectionTableStringTable();
333
Chris Lattner5f48ff72005-07-16 08:01:13 +0000334 // Emit the sections to the .o file, and emit the section table for the file.
335 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000336
Chris Lattner5f48ff72005-07-16 08:01:13 +0000337 // We are done with the abstract symbols.
338 SectionList.clear();
339 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000340
341 // Release the name mangler object.
342 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000343 return false;
344}
345
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000346/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
347/// table for it and then the symbol table itself.
348void ELFWriter::EmitSymbolTable() {
349 if (SymbolTable.size() == 1) return; // Only the null entry.
350
351 // FIXME: compact all local symbols to the start of the symtab.
352 unsigned FirstNonLocalSymbol = 1;
353
Chris Lattner00033952005-07-16 17:36:04 +0000354 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000355 StrTab.Align = 1;
356
Chris Lattner5f48ff72005-07-16 08:01:13 +0000357 DataBuffer &StrTabBuf = StrTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000358 OutputBuffer StrTabOut(StrTabBuf, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000359
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000360 // Set the zero'th symbol to a null byte, as required.
Bill Wendling203d3e42007-01-17 22:22:31 +0000361 StrTabOut.outbyte(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000362 SymbolTable[0].NameIdx = 0;
363 unsigned Index = 1;
364 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000365 // Use the name mangler to uniquify the LLVM symbol.
366 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000367
368 if (Name.empty()) {
369 SymbolTable[i].NameIdx = 0;
370 } else {
371 SymbolTable[i].NameIdx = Index;
372
373 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000374 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000375
376 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000377 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000378
379 // Keep track of the number of bytes emitted to this section.
380 Index += Name.size()+1;
381 }
382 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000383 assert(Index == StrTabBuf.size());
384 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000385
386 // Now that we have emitted the string table and know the offset into the
387 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000388 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000389 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000390 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000391 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
392 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000393 DataBuffer &SymTabBuf = SymTab.SectionData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000394 OutputBuffer SymTabOut(SymTabBuf, is64Bit, isLittleEndian);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000395
Chris Lattner46c53052005-07-12 06:57:52 +0000396 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
397 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
398 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000399 SymTabOut.outword(Sym.NameIdx);
400 SymTabOut.outaddr32(Sym.Value);
401 SymTabOut.outword(Sym.Size);
402 SymTabOut.outbyte(Sym.Info);
403 SymTabOut.outbyte(Sym.Other);
404 SymTabOut.outhalf(Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000405 }
406 } else {
407 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
408 ELFSym &Sym = SymbolTable[i];
Bill Wendling203d3e42007-01-17 22:22:31 +0000409 SymTabOut.outword(Sym.NameIdx);
410 SymTabOut.outbyte(Sym.Info);
411 SymTabOut.outbyte(Sym.Other);
412 SymTabOut.outhalf(Sym.SectionIdx);
413 SymTabOut.outaddr64(Sym.Value);
414 SymTabOut.outxword(Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000415 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000416 }
417
Chris Lattner5f48ff72005-07-16 08:01:13 +0000418 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000419}
420
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000421/// EmitSectionTableStringTable - This method adds and emits a section for the
422/// ELF Section Table string table: the string table that holds all of the
423/// section names.
424void ELFWriter::EmitSectionTableStringTable() {
425 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000426 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000427
428 // Now that we know which section number is the .shstrtab section, update the
429 // e_shstrndx entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000430 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000431 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000432
433 // Set the NameIdx of each section in the string table and emit the bytes for
434 // the string table.
435 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000436 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000437
Chris Lattner5f48ff72005-07-16 08:01:13 +0000438 for (std::list<ELFSection>::iterator I = SectionList.begin(),
439 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000440 // Set the index into the table. Note if we have lots of entries with
441 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000442 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000443
444 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000445 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
446
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000447 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000448 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000449
450 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000451 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000452 }
453
454 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000455 assert(Index == Buf.size());
456 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000457}
458
Chris Lattner5f48ff72005-07-16 08:01:13 +0000459/// OutputSectionsAndSectionTable - Now that we have constructed the file header
460/// and all of the sections, emit these to the ostream destination and emit the
461/// SectionTable.
462void ELFWriter::OutputSectionsAndSectionTable() {
463 // Pass #1: Compute the file offset for each section.
464 size_t FileOff = FileHeader.size(); // File header first.
465
466 // Emit all of the section data in order.
467 for (std::list<ELFSection>::iterator I = SectionList.begin(),
468 E = SectionList.end(); I != E; ++I) {
469 // Align FileOff to whatever the alignment restrictions of the section are.
470 if (I->Align)
471 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
472 I->Offset = FileOff;
473 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000474 }
475
Chris Lattner5f48ff72005-07-16 08:01:13 +0000476 // Align Section Header.
477 unsigned TableAlign = is64Bit ? 8 : 4;
478 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
479
480 // Now that we know where all of the sections will be emitted, set the e_shnum
481 // entry in the ELF header.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000482 OutputBuffer FHOut(FileHeader, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000483 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000484
Chris Lattner5f48ff72005-07-16 08:01:13 +0000485 // Now that we know the offset in the file of the section table, update the
486 // e_shoff address in the ELF header.
Bill Wendling203d3e42007-01-17 22:22:31 +0000487 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000488
Chris Lattner5f48ff72005-07-16 08:01:13 +0000489 // Now that we know all of the data in the file header, emit it and all of the
490 // sections!
491 O.write((char*)&FileHeader[0], FileHeader.size());
492 FileOff = FileHeader.size();
493 DataBuffer().swap(FileHeader);
494
495 DataBuffer Table;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000496 OutputBuffer TableOut(Table, is64Bit, isLittleEndian);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000497
498 // Emit all of the section data and build the section table itself.
499 while (!SectionList.empty()) {
500 const ELFSection &S = *SectionList.begin();
501
502 // Align FileOff to whatever the alignment restrictions of the section are.
503 if (S.Align)
504 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
505 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000506 O.put((char)0xAB);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000507 O.write((char*)&S.SectionData[0], S.SectionData.size());
508 FileOff += S.SectionData.size();
509
Bill Wendling203d3e42007-01-17 22:22:31 +0000510 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
511 TableOut.outword(S.Type); // sh_type - Section contents & semantics
512 TableOut.outword(S.Flags); // sh_flags - Section flags.
513 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
514 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
515 TableOut.outword(S.Size); // sh_size - The section size.
516 TableOut.outword(S.Link); // sh_link - Section header table index link.
517 TableOut.outword(S.Info); // sh_info - Auxillary information.
518 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
519 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000520
521 SectionList.pop_front();
522 }
523
524 // Align output for the section table.
525 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
526 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000527 O.put((char)0xAB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000528
Chris Lattner5f48ff72005-07-16 08:01:13 +0000529 // Emit the section table itself.
530 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000531}