blob: 4be24198864dba389efaaf4f2c981c8bb4512313 [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"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000038#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000039#include "llvm/Support/Mangler.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000040#include <iostream>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000041using namespace llvm;
42
Chris Lattner0e180502005-07-11 06:34:30 +000043//===----------------------------------------------------------------------===//
44// ELFCodeEmitter Implementation
45//===----------------------------------------------------------------------===//
46
Chris Lattneraa507db2005-07-11 05:17:18 +000047namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000048 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
49 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000050 class ELFCodeEmitter : public MachineCodeEmitter {
51 ELFWriter &EW;
Chris Lattner5f48ff72005-07-16 08:01:13 +000052 ELFWriter::ELFSection *ES; // Section to write to.
53 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000054 size_t FnStart;
55 public:
Chris Lattner5f48ff72005-07-16 08:01:13 +000056 ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000057
Chris Lattner0e180502005-07-11 06:34:30 +000058 void startFunction(MachineFunction &F);
59 void finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000060
Chris Lattneraa507db2005-07-11 05:17:18 +000061 void emitConstantPool(MachineConstantPool *MCP) {
62 if (MCP->isEmpty()) return;
63 assert(0 && "unimp");
64 }
65 virtual void emitByte(unsigned char B) {
Chris Lattner5f48ff72005-07-16 08:01:13 +000066 OutBuffer->push_back(B);
Chris Lattneraa507db2005-07-11 05:17:18 +000067 }
68 virtual void emitWordAt(unsigned W, unsigned *Ptr) {
69 assert(0 && "ni");
70 }
71 virtual void emitWord(unsigned W) {
72 assert(0 && "ni");
73 }
74 virtual uint64_t getCurrentPCValue() {
Chris Lattner5f48ff72005-07-16 08:01:13 +000075 return OutBuffer->size();
Chris Lattneraa507db2005-07-11 05:17:18 +000076 }
77 virtual uint64_t getCurrentPCOffset() {
Chris Lattner5f48ff72005-07-16 08:01:13 +000078 return OutBuffer->size()-FnStart;
Chris Lattneraa507db2005-07-11 05:17:18 +000079 }
80 void addRelocation(const MachineRelocation &MR) {
81 assert(0 && "relo not handled yet!");
82 }
83 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) {
84 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000085 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000086 }
Nate Begeman37efe672006-04-22 18:53:45 +000087 virtual uint64_t getJumpTableEntryAddress(unsigned Index) {
88 assert(0 && "JT not implementated yet!");
89 return 0;
90 }
Andrew Lenharthfe660392005-07-28 18:13:59 +000091 virtual unsigned char* allocateGlobal(unsigned size, unsigned alignment) {
92 assert(0 && "Globals not implemented yet!");
93 return 0;
94 }
95
Chris Lattner0e180502005-07-11 06:34:30 +000096 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Chris Lattneraa507db2005-07-11 05:17:18 +000097 void startFunctionStub(unsigned StubSize) {
98 assert(0 && "JIT specific function called!");
99 abort();
100 }
101 void *finishFunctionStub(const Function *F) {
102 assert(0 && "JIT specific function called!");
103 abort();
104 return 0;
105 }
106 };
107}
108
Chris Lattner0e180502005-07-11 06:34:30 +0000109/// startFunction - This callback is invoked when a new machine function is
110/// about to be emitted.
111void ELFCodeEmitter::startFunction(MachineFunction &F) {
112 // Align the output buffer to the appropriate alignment.
113 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +0000114 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +0000115 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
116 ELFWriter::ELFSection::SHF_EXECINSTR |
117 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000118 OutBuffer = &ES->SectionData;
Jeff Cohen00b168892005-07-27 06:12:32 +0000119
Chris Lattner0e180502005-07-11 06:34:30 +0000120 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000121 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000122
Chris Lattner0e180502005-07-11 06:34:30 +0000123 // Add padding zeros to the end of the buffer to make sure that the
124 // function will start on the correct byte alignment within the section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000125 size_t SectionOff = OutBuffer->size();
126 ELFWriter::align(*OutBuffer, Align);
Jeff Cohen00b168892005-07-27 06:12:32 +0000127
Chris Lattner5f48ff72005-07-16 08:01:13 +0000128 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000129}
130
131/// finishFunction - This callback is invoked after the function is completely
132/// finished.
133void ELFCodeEmitter::finishFunction(MachineFunction &F) {
134 // We now know the size of the function, add a symbol to represent it.
135 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000136
Chris Lattner0e180502005-07-11 06:34:30 +0000137 // Figure out the binding (linkage) of the symbol.
138 switch (F.getFunction()->getLinkage()) {
139 default:
140 // appending linkage is illegal for functions.
141 assert(0 && "Unknown linkage type!");
142 case GlobalValue::ExternalLinkage:
143 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
144 break;
145 case GlobalValue::LinkOnceLinkage:
146 case GlobalValue::WeakLinkage:
147 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
148 break;
149 case GlobalValue::InternalLinkage:
150 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
151 break;
152 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000153
154 ES->Size = OutBuffer->size();
155
Chris Lattner0e180502005-07-11 06:34:30 +0000156 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000157 FnSym.SectionIdx = ES->SectionIdx;
158 FnSym.Value = FnStart; // Value = Offset from start of Section.
159 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000160
Chris Lattner0e180502005-07-11 06:34:30 +0000161 // Finally, add it to the symtab.
162 EW.SymbolTable.push_back(FnSym);
163}
164
165//===----------------------------------------------------------------------===//
166// ELFWriter Implementation
167//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000168
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000169ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
170 e_machine = 0; // e_machine defaults to 'No Machine'
171 e_flags = 0; // e_flags defaults to 0, no flags.
172
Jeff Cohen00b168892005-07-27 06:12:32 +0000173 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000174 isLittleEndian = TM.getTargetData().isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000175
176 // Create the machine code emitter object for this target.
177 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000178 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000179}
180
181ELFWriter::~ELFWriter() {
182 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000183}
184
185// doInitialization - Emit the file header and all of the global variables for
186// the module to the ELF file.
187bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000188 Mang = new Mangler(M);
189
Chris Lattner5f48ff72005-07-16 08:01:13 +0000190 // Local alias to shortenify coming code.
191 std::vector<unsigned char> &FH = FileHeader;
Jeff Cohen00b168892005-07-27 06:12:32 +0000192
Chris Lattner5f48ff72005-07-16 08:01:13 +0000193 outbyte(FH, 0x7F); // EI_MAG0
194 outbyte(FH, 'E'); // EI_MAG1
195 outbyte(FH, 'L'); // EI_MAG2
196 outbyte(FH, 'F'); // EI_MAG3
197 outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS
198 outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA
199 outbyte(FH, 1); // EI_VERSION
200 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000201
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000202 // This should change for shared objects.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000203 outhalf(FH, 1); // e_type = ET_REL
204 outhalf(FH, e_machine); // e_machine = whatever the target wants
205 outword(FH, 1); // e_version = 1
206 outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file
207 outaddr(FH, 0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000208
Chris Lattner5f48ff72005-07-16 08:01:13 +0000209 ELFHeader_e_shoff_Offset = FH.size();
210 outaddr(FH, 0); // e_shoff
211 outword(FH, e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000212
Chris Lattner5f48ff72005-07-16 08:01:13 +0000213 outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
214 outhalf(FH, 0); // e_phentsize = prog header entry size
215 outhalf(FH, 0); // e_phnum = # prog header entries = 0
216 outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000217
Jeff Cohen00b168892005-07-27 06:12:32 +0000218
Chris Lattner5f48ff72005-07-16 08:01:13 +0000219 ELFHeader_e_shnum_Offset = FH.size();
220 outhalf(FH, 0); // e_shnum = # of section header ents
221 ELFHeader_e_shstrndx_Offset = FH.size();
222 outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000223
Chris Lattner5f48ff72005-07-16 08:01:13 +0000224 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000225 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000226
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000227 // Start up the symbol table. The first entry in the symtab is the null
228 // entry.
229 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000230
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000231 return false;
232}
233
Chris Lattner05895232005-07-16 17:41:06 +0000234void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000235 // If this is an external global, emit it now. TODO: Note that it would be
236 // better to ignore the symbol here and only add it to the symbol table if
237 // referenced.
238 if (!GV->hasInitializer()) {
239 ELFSym ExternalSym(GV);
240 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
241 ExternalSym.SetType(ELFSym::STT_NOTYPE);
242 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
243 SymbolTable.push_back(ExternalSym);
244 return;
245 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000246
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000247 const Type *GVType = (const Type*)GV->getType();
248 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
249 unsigned Size = TM.getTargetData().getTypeSize(GVType);
250
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000251 // If this global has a zero initializer, it is part of the .bss or common
252 // section.
253 if (GV->getInitializer()->isNullValue()) {
254 // If this global is part of the common block, add it now. Variables are
255 // part of the common block if they are zero initialized and allowed to be
256 // merged with other symbols.
257 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
258 ELFSym CommonSym(GV);
259 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000260 CommonSym.Value = Align;
261 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000262 CommonSym.SetBind(ELFSym::STB_GLOBAL);
263 CommonSym.SetType(ELFSym::STT_OBJECT);
264 // TODO SOMEDAY: add ELF visibility.
265 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
266 SymbolTable.push_back(CommonSym);
267 return;
268 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000269
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000270 // Otherwise, this symbol is part of the .bss section. Emit it now.
271
272 // Handle alignment. Ensure section is aligned at least as much as required
273 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000274 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000275 BSSSection.Align = std::max(BSSSection.Align, Align);
276
277 // Within the section, emit enough virtual padding to get us to an alignment
278 // boundary.
279 if (Align)
280 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
281
282 ELFSym BSSSym(GV);
283 BSSSym.Value = BSSSection.Size;
284 BSSSym.Size = Size;
285 BSSSym.SetType(ELFSym::STT_OBJECT);
286
287 switch (GV->getLinkage()) {
288 default: // weak/linkonce handled above
289 assert(0 && "Unexpected linkage type!");
290 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
291 case GlobalValue::ExternalLinkage:
292 BSSSym.SetBind(ELFSym::STB_GLOBAL);
293 break;
294 case GlobalValue::InternalLinkage:
295 BSSSym.SetBind(ELFSym::STB_LOCAL);
296 break;
297 }
298
299 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000300 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000301 SymbolTable.push_back(BSSSym);
302
303 // Reserve space in the .bss section for this symbol.
304 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000305 return;
306 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000307
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000308 // FIXME: handle .rodata
309 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000310
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000311 // FIXME: handle .data
312 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000313}
314
315
316bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000317 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000318 return false;
319}
320
321/// doFinalization - Now that the module has been completely processed, emit
322/// the ELF file to 'O'.
323bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000324 // Okay, the ELF header and .text sections have been completed, build the
325 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000326 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
327 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000328 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000329
330 // Emit the symbol table now, if non-empty.
331 EmitSymbolTable();
332
333 // FIXME: Emit the relocations now.
334
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000335 // Emit the string table for the sections in the ELF file we have.
336 EmitSectionTableStringTable();
337
Chris Lattner5f48ff72005-07-16 08:01:13 +0000338 // Emit the sections to the .o file, and emit the section table for the file.
339 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000340
Chris Lattner5f48ff72005-07-16 08:01:13 +0000341 // We are done with the abstract symbols.
342 SectionList.clear();
343 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000344
345 // Release the name mangler object.
346 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000347 return false;
348}
349
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000350/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
351/// table for it and then the symbol table itself.
352void ELFWriter::EmitSymbolTable() {
353 if (SymbolTable.size() == 1) return; // Only the null entry.
354
355 // FIXME: compact all local symbols to the start of the symtab.
356 unsigned FirstNonLocalSymbol = 1;
357
Chris Lattner00033952005-07-16 17:36:04 +0000358 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000359 StrTab.Align = 1;
360
Chris Lattner5f48ff72005-07-16 08:01:13 +0000361 DataBuffer &StrTabBuf = StrTab.SectionData;
362
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000363 // Set the zero'th symbol to a null byte, as required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000364 outbyte(StrTabBuf, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000365 SymbolTable[0].NameIdx = 0;
366 unsigned Index = 1;
367 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000368 // Use the name mangler to uniquify the LLVM symbol.
369 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000370
371 if (Name.empty()) {
372 SymbolTable[i].NameIdx = 0;
373 } else {
374 SymbolTable[i].NameIdx = Index;
375
376 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000377 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000378
379 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000380 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000381
382 // Keep track of the number of bytes emitted to this section.
383 Index += Name.size()+1;
384 }
385 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000386 assert(Index == StrTabBuf.size());
387 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000388
389 // Now that we have emitted the string table and know the offset into the
390 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000391 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000392 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000393 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000394 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
395 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000396 DataBuffer &SymTabBuf = SymTab.SectionData;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000397
Chris Lattner46c53052005-07-12 06:57:52 +0000398 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
399 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
400 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000401 outword(SymTabBuf, Sym.NameIdx);
402 outaddr32(SymTabBuf, Sym.Value);
403 outword(SymTabBuf, Sym.Size);
404 outbyte(SymTabBuf, Sym.Info);
405 outbyte(SymTabBuf, Sym.Other);
406 outhalf(SymTabBuf, Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000407 }
408 } else {
409 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
410 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000411 outword(SymTabBuf, Sym.NameIdx);
412 outbyte(SymTabBuf, Sym.Info);
413 outbyte(SymTabBuf, Sym.Other);
414 outhalf(SymTabBuf, Sym.SectionIdx);
415 outaddr64(SymTabBuf, Sym.Value);
416 outxword(SymTabBuf, Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000417 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000418 }
419
Chris Lattner5f48ff72005-07-16 08:01:13 +0000420 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000421}
422
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000423/// EmitSectionTableStringTable - This method adds and emits a section for the
424/// ELF Section Table string table: the string table that holds all of the
425/// section names.
426void ELFWriter::EmitSectionTableStringTable() {
427 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000428 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000429
430 // Now that we know which section number is the .shstrtab section, update the
431 // e_shstrndx entry in the ELF header.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000432 fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000433
434 // Set the NameIdx of each section in the string table and emit the bytes for
435 // the string table.
436 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000437 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000438
Chris Lattner5f48ff72005-07-16 08:01:13 +0000439 for (std::list<ELFSection>::iterator I = SectionList.begin(),
440 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000441 // Set the index into the table. Note if we have lots of entries with
442 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000443 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000444
445 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000446 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
447
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000448 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000449 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000450
451 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000452 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000453 }
454
455 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000456 assert(Index == Buf.size());
457 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000458}
459
Chris Lattner5f48ff72005-07-16 08:01:13 +0000460/// OutputSectionsAndSectionTable - Now that we have constructed the file header
461/// and all of the sections, emit these to the ostream destination and emit the
462/// SectionTable.
463void ELFWriter::OutputSectionsAndSectionTable() {
464 // Pass #1: Compute the file offset for each section.
465 size_t FileOff = FileHeader.size(); // File header first.
466
467 // Emit all of the section data in order.
468 for (std::list<ELFSection>::iterator I = SectionList.begin(),
469 E = SectionList.end(); I != E; ++I) {
470 // Align FileOff to whatever the alignment restrictions of the section are.
471 if (I->Align)
472 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
473 I->Offset = FileOff;
474 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000475 }
476
Chris Lattner5f48ff72005-07-16 08:01:13 +0000477 // Align Section Header.
478 unsigned TableAlign = is64Bit ? 8 : 4;
479 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
480
481 // Now that we know where all of the sections will be emitted, set the e_shnum
482 // entry in the ELF header.
483 fixhalf(FileHeader, 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.
487 fixaddr(FileHeader, 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;
496
497 // Emit all of the section data and build the section table itself.
498 while (!SectionList.empty()) {
499 const ELFSection &S = *SectionList.begin();
500
501 // Align FileOff to whatever the alignment restrictions of the section are.
502 if (S.Align)
503 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
504 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000505 O.put((char)0xAB);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000506 O.write((char*)&S.SectionData[0], S.SectionData.size());
507 FileOff += S.SectionData.size();
508
509 outword(Table, S.NameIdx); // sh_name - Symbol table name idx
510 outword(Table, S.Type); // sh_type - Section contents & semantics
511 outword(Table, S.Flags); // sh_flags - Section flags.
512 outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in.
513 outaddr(Table, S.Offset); // sh_offset - Offset from the file start.
514 outword(Table, S.Size); // sh_size - The section size.
515 outword(Table, S.Link); // sh_link - Section header table index link.
516 outword(Table, S.Info); // sh_info - Auxillary information.
517 outword(Table, S.Align); // sh_addralign - Alignment of section.
518 outword(Table, S.EntSize); // sh_entsize - Size of entries in the section.
519
520 SectionList.pop_front();
521 }
522
523 // Align output for the section table.
524 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
525 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000526 O.put((char)0xAB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000527
Chris Lattner5f48ff72005-07-16 08:01:13 +0000528 // Emit the section table itself.
529 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000530}