blob: e43346897d9ab74bace73a80123eee587788423b [file] [log] [blame]
Chris Lattner386b1512005-06-27 06:29:00 +00001//===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner363964e2005-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 Lattner386b1512005-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 Lattner1932f5c2005-07-07 07:02:20 +000014// #2. '.text' section
15// #3. '.data' section
16// #4. '.bss' section (conceptual position in file)
Chris Lattner386b1512005-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 Lattner1932f5c2005-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 Lattner386b1512005-06-27 06:29:00 +000026// ...
27// #N. ".shstrtab" entry - String table for the section names.
Chris Lattner386b1512005-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 Lattner2244f732005-07-11 05:17:18 +000036#include "llvm/CodeGen/MachineCodeEmitter.h"
37#include "llvm/CodeGen/MachineConstantPool.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
Chris Lattner386b1512005-06-27 06:29:00 +000039#include "llvm/Target/TargetMachine.h"
Chris Lattnerdfe33bc2005-07-11 03:11:47 +000040#include "llvm/Support/Mangler.h"
Bill Wendlingcd9c1f02007-01-17 22:22:31 +000041#include "llvm/Support/OutputBuffer.h"
Bill Wendling5c3966a2006-11-29 00:39:47 +000042#include "llvm/Support/Streams.h"
Chris Lattner386b1512005-06-27 06:29:00 +000043using namespace llvm;
44
Chris Lattner449e07f2005-07-11 06:34:30 +000045//===----------------------------------------------------------------------===//
46// ELFCodeEmitter Implementation
47//===----------------------------------------------------------------------===//
48
Chris Lattner2244f732005-07-11 05:17:18 +000049namespace llvm {
Chris Lattner449e07f2005-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 Lattner2244f732005-07-11 05:17:18 +000052 class ELFCodeEmitter : public MachineCodeEmitter {
53 ELFWriter &EW;
Bill Wendlingcd9c1f02007-01-17 22:22:31 +000054 TargetMachine &TM;
Chris Lattnerfd445002005-07-16 08:01:13 +000055 ELFWriter::ELFSection *ES; // Section to write to.
56 std::vector<unsigned char> *OutBuffer;
Chris Lattner2244f732005-07-11 05:17:18 +000057 size_t FnStart;
58 public:
Bill Wendlingcd9c1f02007-01-17 22:22:31 +000059 ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
Chris Lattner2244f732005-07-11 05:17:18 +000060
Chris Lattner449e07f2005-07-11 06:34:30 +000061 void startFunction(MachineFunction &F);
Chris Lattnerc9aa3712006-05-02 18:27:26 +000062 bool finishFunction(MachineFunction &F);
Chris Lattner5bacb002005-07-11 06:17:35 +000063
Chris Lattner2244f732005-07-11 05:17:18 +000064 void addRelocation(const MachineRelocation &MR) {
65 assert(0 && "relo not handled yet!");
66 }
Chris Lattner1d8ee1f2006-05-03 17:10:41 +000067
68 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
69 }
70
71 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
Chris Lattner2244f732005-07-11 05:17:18 +000072 assert(0 && "CP not implementated yet!");
Jeff Cohen33b82322005-07-12 02:53:33 +000073 return 0;
Chris Lattner2244f732005-07-11 05:17:18 +000074 }
Chris Lattner1d8ee1f2006-05-03 17:10:41 +000075 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman4ca2ea52006-04-22 18:53:45 +000076 assert(0 && "JT not implementated yet!");
77 return 0;
78 }
Chris Lattnerb8065a92006-05-02 23:22:24 +000079
Chris Lattner1d8ee1f2006-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 Lenharth3faa8222005-07-28 18:13:59 +000084
Chris Lattner449e07f2005-07-11 06:34:30 +000085 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Evan Cheng9fd73b82006-11-16 20:04:04 +000086 void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
Chris Lattner2244f732005-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 Lattner449e07f2005-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 Lattnerfd445002005-07-16 08:01:13 +0000103 // Get the ELF Section that this function belongs in.
Chris Lattner363964e2005-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 Lattnerfd445002005-07-16 08:01:13 +0000107 OutBuffer = &ES->SectionData;
Bill Wendlingcd9c1f02007-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 Wendlingf3baad32006-12-07 01:30:32 +0000110 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000111 abort();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000112
Chris Lattner449e07f2005-07-11 06:34:30 +0000113 // Upgrade the section alignment if required.
Chris Lattnerfd445002005-07-16 08:01:13 +0000114 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000115
Chris Lattner449e07f2005-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 Wendlingcd9c1f02007-01-17 22:22:31 +0000118 OutputBuffer OB(TM, *OutBuffer);
119 OB.align(Align);
Chris Lattnerfd445002005-07-16 08:01:13 +0000120 FnStart = OutBuffer->size();
Chris Lattner449e07f2005-07-11 06:34:30 +0000121}
122
123/// finishFunction - This callback is invoked after the function is completely
124/// finished.
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000125bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner449e07f2005-07-11 06:34:30 +0000126 // We now know the size of the function, add a symbol to represent it.
127 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000128
Chris Lattner449e07f2005-07-11 06:34:30 +0000129 // Figure out the binding (linkage) of the symbol.
130 switch (F.getFunction()->getLinkage()) {
131 default:
132 // appending linkage is illegal for functions.
133 assert(0 && "Unknown linkage type!");
134 case GlobalValue::ExternalLinkage:
135 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
136 break;
137 case GlobalValue::LinkOnceLinkage:
138 case GlobalValue::WeakLinkage:
139 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
140 break;
141 case GlobalValue::InternalLinkage:
142 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
143 break;
144 }
Chris Lattnerfd445002005-07-16 08:01:13 +0000145
146 ES->Size = OutBuffer->size();
147
Chris Lattner449e07f2005-07-11 06:34:30 +0000148 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattnerfd445002005-07-16 08:01:13 +0000149 FnSym.SectionIdx = ES->SectionIdx;
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000150 FnSym.Value = FnStart; // Value = Offset from start of Section.
Chris Lattnerfd445002005-07-16 08:01:13 +0000151 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000152
Chris Lattner449e07f2005-07-11 06:34:30 +0000153 // Finally, add it to the symtab.
154 EW.SymbolTable.push_back(FnSym);
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000155 return false;
Chris Lattner449e07f2005-07-11 06:34:30 +0000156}
157
158//===----------------------------------------------------------------------===//
159// ELFWriter Implementation
160//===----------------------------------------------------------------------===//
Chris Lattner2244f732005-07-11 05:17:18 +0000161
Chris Lattner386b1512005-06-27 06:29:00 +0000162ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
163 e_machine = 0; // e_machine defaults to 'No Machine'
164 e_flags = 0; // e_flags defaults to 0, no flags.
165
Owen Anderson20a631f2006-05-03 01:29:57 +0000166 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
167 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattner2244f732005-07-11 05:17:18 +0000168
169 // Create the machine code emitter object for this target.
Bill Wendling38ee6512007-01-17 09:06:13 +0000170 MCE = new ELFCodeEmitter(*this);
Chris Lattnerfd445002005-07-16 08:01:13 +0000171 NumSections = 0;
Chris Lattner2244f732005-07-11 05:17:18 +0000172}
173
174ELFWriter::~ELFWriter() {
175 delete MCE;
Chris Lattner386b1512005-06-27 06:29:00 +0000176}
177
178// doInitialization - Emit the file header and all of the global variables for
179// the module to the ELF file.
180bool ELFWriter::doInitialization(Module &M) {
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000181 Mang = new Mangler(M);
182
Chris Lattnerfd445002005-07-16 08:01:13 +0000183 // Local alias to shortenify coming code.
184 std::vector<unsigned char> &FH = FileHeader;
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000185 OutputBuffer FHOut(TM, FH);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000186
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000187 FHOut.outbyte(0x7F); // EI_MAG0
188 FHOut.outbyte('E'); // EI_MAG1
189 FHOut.outbyte('L'); // EI_MAG2
190 FHOut.outbyte('F'); // EI_MAG3
191 FHOut.outbyte(is64Bit ? 2 : 1); // EI_CLASS
192 FHOut.outbyte(isLittleEndian ? 1 : 2); // EI_DATA
193 FHOut.outbyte(1); // EI_VERSION
Bill Wendling38ee6512007-01-17 09:06:13 +0000194 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000195
Chris Lattner386b1512005-06-27 06:29:00 +0000196 // This should change for shared objects.
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000197 FHOut.outhalf(1); // e_type = ET_REL
198 FHOut.outhalf(e_machine); // e_machine = whatever the target wants
199 FHOut.outword(1); // e_version = 1
200 FHOut.outaddr(0); // e_entry = 0 -> no entry point in .o file
201 FHOut.outaddr(0); // e_phoff = 0 -> no program header for .o
Chris Lattner386b1512005-06-27 06:29:00 +0000202
Chris Lattnerfd445002005-07-16 08:01:13 +0000203 ELFHeader_e_shoff_Offset = FH.size();
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000204 FHOut.outaddr(0); // e_shoff
205 FHOut.outword(e_flags); // e_flags = whatever the target wants
Chris Lattner386b1512005-06-27 06:29:00 +0000206
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000207 FHOut.outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
208 FHOut.outhalf(0); // e_phentsize = prog header entry size
209 FHOut.outhalf(0); // e_phnum = # prog header entries = 0
210 FHOut.outhalf(is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner386b1512005-06-27 06:29:00 +0000211
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000212
Chris Lattnerfd445002005-07-16 08:01:13 +0000213 ELFHeader_e_shnum_Offset = FH.size();
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000214 FHOut.outhalf(0); // e_shnum = # of section header ents
Chris Lattnerfd445002005-07-16 08:01:13 +0000215 ELFHeader_e_shstrndx_Offset = FH.size();
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000216 FHOut.outhalf(0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner386b1512005-06-27 06:29:00 +0000217
Chris Lattnerfd445002005-07-16 08:01:13 +0000218 // Add the null section, which is required to be first in the file.
Chris Lattner363964e2005-07-16 17:36:04 +0000219 getSection("", 0, 0);
Chris Lattner386b1512005-06-27 06:29:00 +0000220
Chris Lattner1932f5c2005-07-07 07:02:20 +0000221 // Start up the symbol table. The first entry in the symtab is the null
222 // entry.
223 SymbolTable.push_back(ELFSym(0));
Chris Lattner386b1512005-06-27 06:29:00 +0000224
Chris Lattner386b1512005-06-27 06:29:00 +0000225 return false;
226}
227
Chris Lattnera5998ce2005-07-16 17:41:06 +0000228void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner748de6e2005-07-08 05:47:00 +0000229 // If this is an external global, emit it now. TODO: Note that it would be
230 // better to ignore the symbol here and only add it to the symbol table if
231 // referenced.
232 if (!GV->hasInitializer()) {
233 ELFSym ExternalSym(GV);
234 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
235 ExternalSym.SetType(ELFSym::STT_NOTYPE);
236 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
237 SymbolTable.push_back(ExternalSym);
238 return;
239 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000240
Chris Lattner748de6e2005-07-08 05:47:00 +0000241 const Type *GVType = (const Type*)GV->getType();
Owen Anderson20a631f2006-05-03 01:29:57 +0000242 unsigned Align = TM.getTargetData()->getTypeAlignment(GVType);
243 unsigned Size = TM.getTargetData()->getTypeSize(GVType);
Chris Lattner748de6e2005-07-08 05:47:00 +0000244
Chris Lattner1932f5c2005-07-07 07:02:20 +0000245 // If this global has a zero initializer, it is part of the .bss or common
246 // section.
247 if (GV->getInitializer()->isNullValue()) {
248 // If this global is part of the common block, add it now. Variables are
249 // part of the common block if they are zero initialized and allowed to be
250 // merged with other symbols.
251 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
252 ELFSym CommonSym(GV);
253 // Value for common symbols is the alignment required.
Chris Lattner748de6e2005-07-08 05:47:00 +0000254 CommonSym.Value = Align;
255 CommonSym.Size = Size;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000256 CommonSym.SetBind(ELFSym::STB_GLOBAL);
257 CommonSym.SetType(ELFSym::STT_OBJECT);
258 // TODO SOMEDAY: add ELF visibility.
259 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
260 SymbolTable.push_back(CommonSym);
261 return;
262 }
Chris Lattner386b1512005-06-27 06:29:00 +0000263
Chris Lattner748de6e2005-07-08 05:47:00 +0000264 // Otherwise, this symbol is part of the .bss section. Emit it now.
265
266 // Handle alignment. Ensure section is aligned at least as much as required
267 // by this symbol.
Chris Lattnera5998ce2005-07-16 17:41:06 +0000268 ELFSection &BSSSection = getBSSSection();
Chris Lattner748de6e2005-07-08 05:47:00 +0000269 BSSSection.Align = std::max(BSSSection.Align, Align);
270
271 // Within the section, emit enough virtual padding to get us to an alignment
272 // boundary.
273 if (Align)
274 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
275
276 ELFSym BSSSym(GV);
277 BSSSym.Value = BSSSection.Size;
278 BSSSym.Size = Size;
279 BSSSym.SetType(ELFSym::STT_OBJECT);
280
281 switch (GV->getLinkage()) {
282 default: // weak/linkonce handled above
283 assert(0 && "Unexpected linkage type!");
284 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
285 case GlobalValue::ExternalLinkage:
286 BSSSym.SetBind(ELFSym::STB_GLOBAL);
287 break;
288 case GlobalValue::InternalLinkage:
289 BSSSym.SetBind(ELFSym::STB_LOCAL);
290 break;
291 }
292
293 // Set the idx of the .bss section
Chris Lattnerfd445002005-07-16 08:01:13 +0000294 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner748de6e2005-07-08 05:47:00 +0000295 SymbolTable.push_back(BSSSym);
296
297 // Reserve space in the .bss section for this symbol.
298 BSSSection.Size += Size;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000299 return;
300 }
Chris Lattner386b1512005-06-27 06:29:00 +0000301
Chris Lattner1932f5c2005-07-07 07:02:20 +0000302 // FIXME: handle .rodata
303 //assert(!GV->isConstant() && "unimp");
Chris Lattner386b1512005-06-27 06:29:00 +0000304
Chris Lattner1932f5c2005-07-07 07:02:20 +0000305 // FIXME: handle .data
306 //assert(0 && "unimp");
Chris Lattner386b1512005-06-27 06:29:00 +0000307}
308
309
310bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner2244f732005-07-11 05:17:18 +0000311 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner386b1512005-06-27 06:29:00 +0000312 return false;
313}
314
315/// doFinalization - Now that the module has been completely processed, emit
316/// the ELF file to 'O'.
317bool ELFWriter::doFinalization(Module &M) {
Chris Lattner1932f5c2005-07-07 07:02:20 +0000318 // Okay, the ELF header and .text sections have been completed, build the
319 // .data, .bss, and "common" sections next.
Chris Lattnerfd445002005-07-16 08:01:13 +0000320 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
321 I != E; ++I)
Chris Lattnera5998ce2005-07-16 17:41:06 +0000322 EmitGlobal(I);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000323
324 // Emit the symbol table now, if non-empty.
325 EmitSymbolTable();
326
327 // FIXME: Emit the relocations now.
328
Chris Lattner386b1512005-06-27 06:29:00 +0000329 // Emit the string table for the sections in the ELF file we have.
330 EmitSectionTableStringTable();
331
Chris Lattnerfd445002005-07-16 08:01:13 +0000332 // Emit the sections to the .o file, and emit the section table for the file.
333 OutputSectionsAndSectionTable();
Chris Lattner386b1512005-06-27 06:29:00 +0000334
Chris Lattnerfd445002005-07-16 08:01:13 +0000335 // We are done with the abstract symbols.
336 SectionList.clear();
337 NumSections = 0;
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000338
339 // Release the name mangler object.
340 delete Mang; Mang = 0;
Chris Lattner386b1512005-06-27 06:29:00 +0000341 return false;
342}
343
Chris Lattner1932f5c2005-07-07 07:02:20 +0000344/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
345/// table for it and then the symbol table itself.
346void ELFWriter::EmitSymbolTable() {
347 if (SymbolTable.size() == 1) return; // Only the null entry.
348
349 // FIXME: compact all local symbols to the start of the symtab.
350 unsigned FirstNonLocalSymbol = 1;
351
Chris Lattner363964e2005-07-16 17:36:04 +0000352 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000353 StrTab.Align = 1;
354
Chris Lattnerfd445002005-07-16 08:01:13 +0000355 DataBuffer &StrTabBuf = StrTab.SectionData;
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000356 OutputBuffer StrTabOut(TM, StrTabBuf);
Chris Lattnerfd445002005-07-16 08:01:13 +0000357
Chris Lattner1932f5c2005-07-07 07:02:20 +0000358 // Set the zero'th symbol to a null byte, as required.
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000359 StrTabOut.outbyte(0);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000360 SymbolTable[0].NameIdx = 0;
361 unsigned Index = 1;
362 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000363 // Use the name mangler to uniquify the LLVM symbol.
364 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000365
366 if (Name.empty()) {
367 SymbolTable[i].NameIdx = 0;
368 } else {
369 SymbolTable[i].NameIdx = Index;
370
371 // Add the name to the output buffer, including the null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000372 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner1932f5c2005-07-07 07:02:20 +0000373
374 // Add a null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000375 StrTabBuf.push_back(0);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000376
377 // Keep track of the number of bytes emitted to this section.
378 Index += Name.size()+1;
379 }
380 }
Chris Lattnerfd445002005-07-16 08:01:13 +0000381 assert(Index == StrTabBuf.size());
382 StrTab.Size = Index;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000383
384 // Now that we have emitted the string table and know the offset into the
385 // string table of each symbol, emit the symbol table itself.
Chris Lattner363964e2005-07-16 17:36:04 +0000386 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner298ac692005-07-12 06:57:52 +0000387 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattnerfd445002005-07-16 08:01:13 +0000388 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner1932f5c2005-07-07 07:02:20 +0000389 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
390 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattnerfd445002005-07-16 08:01:13 +0000391 DataBuffer &SymTabBuf = SymTab.SectionData;
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000392 OutputBuffer SymTabOut(TM, SymTabBuf);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000393
Chris Lattner298ac692005-07-12 06:57:52 +0000394 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
395 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
396 ELFSym &Sym = SymbolTable[i];
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000397 SymTabOut.outword(Sym.NameIdx);
398 SymTabOut.outaddr32(Sym.Value);
399 SymTabOut.outword(Sym.Size);
400 SymTabOut.outbyte(Sym.Info);
401 SymTabOut.outbyte(Sym.Other);
402 SymTabOut.outhalf(Sym.SectionIdx);
Chris Lattner298ac692005-07-12 06:57:52 +0000403 }
404 } else {
405 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
406 ELFSym &Sym = SymbolTable[i];
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000407 SymTabOut.outword(Sym.NameIdx);
408 SymTabOut.outbyte(Sym.Info);
409 SymTabOut.outbyte(Sym.Other);
410 SymTabOut.outhalf(Sym.SectionIdx);
411 SymTabOut.outaddr64(Sym.Value);
412 SymTabOut.outxword(Sym.Size);
Chris Lattner298ac692005-07-12 06:57:52 +0000413 }
Chris Lattner1932f5c2005-07-07 07:02:20 +0000414 }
415
Chris Lattnerfd445002005-07-16 08:01:13 +0000416 SymTab.Size = SymTabBuf.size();
Chris Lattner1932f5c2005-07-07 07:02:20 +0000417}
418
Chris Lattner386b1512005-06-27 06:29:00 +0000419/// EmitSectionTableStringTable - This method adds and emits a section for the
420/// ELF Section Table string table: the string table that holds all of the
421/// section names.
422void ELFWriter::EmitSectionTableStringTable() {
423 // First step: add the section for the string table to the list of sections:
Chris Lattner363964e2005-07-16 17:36:04 +0000424 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner386b1512005-06-27 06:29:00 +0000425
426 // Now that we know which section number is the .shstrtab section, update the
427 // e_shstrndx entry in the ELF header.
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000428 OutputBuffer FHOut(TM, FileHeader);
429 FHOut.fixhalf(SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner386b1512005-06-27 06:29:00 +0000430
431 // Set the NameIdx of each section in the string table and emit the bytes for
432 // the string table.
433 unsigned Index = 0;
Chris Lattnerfd445002005-07-16 08:01:13 +0000434 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner386b1512005-06-27 06:29:00 +0000435
Chris Lattnerfd445002005-07-16 08:01:13 +0000436 for (std::list<ELFSection>::iterator I = SectionList.begin(),
437 E = SectionList.end(); I != E; ++I) {
Chris Lattner386b1512005-06-27 06:29:00 +0000438 // Set the index into the table. Note if we have lots of entries with
439 // common suffixes, we could memoize them here if we cared.
Chris Lattnerfd445002005-07-16 08:01:13 +0000440 I->NameIdx = Index;
Chris Lattner386b1512005-06-27 06:29:00 +0000441
442 // Add the name to the output buffer, including the null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000443 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
444
Chris Lattner386b1512005-06-27 06:29:00 +0000445 // Add a null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000446 Buf.push_back(0);
Chris Lattner386b1512005-06-27 06:29:00 +0000447
448 // Keep track of the number of bytes emitted to this section.
Chris Lattnerfd445002005-07-16 08:01:13 +0000449 Index += I->Name.size()+1;
Chris Lattner386b1512005-06-27 06:29:00 +0000450 }
451
452 // Set the size of .shstrtab now that we know what it is.
Chris Lattnerfd445002005-07-16 08:01:13 +0000453 assert(Index == Buf.size());
454 SHStrTab.Size = Index;
Chris Lattner386b1512005-06-27 06:29:00 +0000455}
456
Chris Lattnerfd445002005-07-16 08:01:13 +0000457/// OutputSectionsAndSectionTable - Now that we have constructed the file header
458/// and all of the sections, emit these to the ostream destination and emit the
459/// SectionTable.
460void ELFWriter::OutputSectionsAndSectionTable() {
461 // Pass #1: Compute the file offset for each section.
462 size_t FileOff = FileHeader.size(); // File header first.
463
464 // Emit all of the section data in order.
465 for (std::list<ELFSection>::iterator I = SectionList.begin(),
466 E = SectionList.end(); I != E; ++I) {
467 // Align FileOff to whatever the alignment restrictions of the section are.
468 if (I->Align)
469 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
470 I->Offset = FileOff;
471 FileOff += I->SectionData.size();
Chris Lattner386b1512005-06-27 06:29:00 +0000472 }
473
Chris Lattnerfd445002005-07-16 08:01:13 +0000474 // Align Section Header.
475 unsigned TableAlign = is64Bit ? 8 : 4;
476 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
477
478 // Now that we know where all of the sections will be emitted, set the e_shnum
479 // entry in the ELF header.
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000480 OutputBuffer FHOut(TM, FileHeader);
481 FHOut.fixhalf(NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000482
Chris Lattnerfd445002005-07-16 08:01:13 +0000483 // Now that we know the offset in the file of the section table, update the
484 // e_shoff address in the ELF header.
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000485 FHOut.fixaddr(FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000486
Chris Lattnerfd445002005-07-16 08:01:13 +0000487 // Now that we know all of the data in the file header, emit it and all of the
488 // sections!
489 O.write((char*)&FileHeader[0], FileHeader.size());
490 FileOff = FileHeader.size();
491 DataBuffer().swap(FileHeader);
492
493 DataBuffer Table;
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000494 OutputBuffer TableOut(TM, Table);
Chris Lattnerfd445002005-07-16 08:01:13 +0000495
496 // Emit all of the section data and build the section table itself.
497 while (!SectionList.empty()) {
498 const ELFSection &S = *SectionList.begin();
499
500 // Align FileOff to whatever the alignment restrictions of the section are.
501 if (S.Align)
502 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
503 FileOff != NewFileOff; ++FileOff)
Jeff Cohen486e36c2005-08-19 16:19:21 +0000504 O.put((char)0xAB);
Chris Lattnerfd445002005-07-16 08:01:13 +0000505 O.write((char*)&S.SectionData[0], S.SectionData.size());
506 FileOff += S.SectionData.size();
507
Bill Wendlingcd9c1f02007-01-17 22:22:31 +0000508 TableOut.outword(S.NameIdx); // sh_name - Symbol table name idx
509 TableOut.outword(S.Type); // sh_type - Section contents & semantics
510 TableOut.outword(S.Flags); // sh_flags - Section flags.
511 TableOut.outaddr(S.Addr); // sh_addr - The mem addr this section is in.
512 TableOut.outaddr(S.Offset); // sh_offset - Offset from the file start.
513 TableOut.outword(S.Size); // sh_size - The section size.
514 TableOut.outword(S.Link); // sh_link - Section header table index link.
515 TableOut.outword(S.Info); // sh_info - Auxillary information.
516 TableOut.outword(S.Align); // sh_addralign - Alignment of section.
517 TableOut.outword(S.EntSize); // sh_entsize - Size of entries in the section
Chris Lattnerfd445002005-07-16 08:01:13 +0000518
519 SectionList.pop_front();
520 }
521
522 // Align output for the section table.
523 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
524 FileOff != NewFileOff; ++FileOff)
Jeff Cohen486e36c2005-08-19 16:19:21 +0000525 O.put((char)0xAB);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000526
Chris Lattnerfd445002005-07-16 08:01:13 +0000527 // Emit the section table itself.
528 O.write((char*)&Table[0], Table.size());
Chris Lattner386b1512005-06-27 06:29:00 +0000529}