blob: 483c49059c43b0361967c55f707217203bb026e1 [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);
Chris Lattner43b429b2006-05-02 18:27:26 +000059 bool 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 }
Chris Lattneraa507db2005-07-11 05:17:18 +000065 void addRelocation(const MachineRelocation &MR) {
66 assert(0 && "relo not handled yet!");
67 }
68 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) {
69 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000070 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000071 }
Nate Begeman37efe672006-04-22 18:53:45 +000072 virtual uint64_t getJumpTableEntryAddress(unsigned Index) {
73 assert(0 && "JT not implementated yet!");
74 return 0;
75 }
Andrew Lenharthfe660392005-07-28 18:13:59 +000076
Chris Lattner0e180502005-07-11 06:34:30 +000077 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Chris Lattneraa507db2005-07-11 05:17:18 +000078 void startFunctionStub(unsigned StubSize) {
79 assert(0 && "JIT specific function called!");
80 abort();
81 }
82 void *finishFunctionStub(const Function *F) {
83 assert(0 && "JIT specific function called!");
84 abort();
85 return 0;
86 }
87 };
88}
89
Chris Lattner0e180502005-07-11 06:34:30 +000090/// startFunction - This callback is invoked when a new machine function is
91/// about to be emitted.
92void ELFCodeEmitter::startFunction(MachineFunction &F) {
93 // Align the output buffer to the appropriate alignment.
94 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +000095 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +000096 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
97 ELFWriter::ELFSection::SHF_EXECINSTR |
98 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +000099 OutBuffer = &ES->SectionData;
Chris Lattner43b429b2006-05-02 18:27:26 +0000100 std::cerr << "FIXME: This code needs to be updated for changes in the"
101 << " CodeEmitter interfaces. In particular, this should set "
102 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
103 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000104
Chris Lattner0e180502005-07-11 06:34:30 +0000105 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000106 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000107
Chris Lattner0e180502005-07-11 06:34:30 +0000108 // Add padding zeros to the end of the buffer to make sure that the
109 // function will start on the correct byte alignment within the section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000110 size_t SectionOff = OutBuffer->size();
111 ELFWriter::align(*OutBuffer, Align);
Jeff Cohen00b168892005-07-27 06:12:32 +0000112
Chris Lattner5f48ff72005-07-16 08:01:13 +0000113 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000114}
115
116/// finishFunction - This callback is invoked after the function is completely
117/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000118bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000119 // We now know the size of the function, add a symbol to represent it.
120 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000121
Chris Lattner0e180502005-07-11 06:34:30 +0000122 // Figure out the binding (linkage) of the symbol.
123 switch (F.getFunction()->getLinkage()) {
124 default:
125 // appending linkage is illegal for functions.
126 assert(0 && "Unknown linkage type!");
127 case GlobalValue::ExternalLinkage:
128 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
129 break;
130 case GlobalValue::LinkOnceLinkage:
131 case GlobalValue::WeakLinkage:
132 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
133 break;
134 case GlobalValue::InternalLinkage:
135 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
136 break;
137 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000138
139 ES->Size = OutBuffer->size();
140
Chris Lattner0e180502005-07-11 06:34:30 +0000141 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000142 FnSym.SectionIdx = ES->SectionIdx;
143 FnSym.Value = FnStart; // Value = Offset from start of Section.
144 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000145
Chris Lattner0e180502005-07-11 06:34:30 +0000146 // Finally, add it to the symtab.
147 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000148 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000149}
150
151//===----------------------------------------------------------------------===//
152// ELFWriter Implementation
153//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000154
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000155ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
156 e_machine = 0; // e_machine defaults to 'No Machine'
157 e_flags = 0; // e_flags defaults to 0, no flags.
158
Jeff Cohen00b168892005-07-27 06:12:32 +0000159 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000160 isLittleEndian = TM.getTargetData().isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000161
162 // Create the machine code emitter object for this target.
163 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000164 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000165}
166
167ELFWriter::~ELFWriter() {
168 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000169}
170
171// doInitialization - Emit the file header and all of the global variables for
172// the module to the ELF file.
173bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000174 Mang = new Mangler(M);
175
Chris Lattner5f48ff72005-07-16 08:01:13 +0000176 // Local alias to shortenify coming code.
177 std::vector<unsigned char> &FH = FileHeader;
Jeff Cohen00b168892005-07-27 06:12:32 +0000178
Chris Lattner5f48ff72005-07-16 08:01:13 +0000179 outbyte(FH, 0x7F); // EI_MAG0
180 outbyte(FH, 'E'); // EI_MAG1
181 outbyte(FH, 'L'); // EI_MAG2
182 outbyte(FH, 'F'); // EI_MAG3
183 outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS
184 outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA
185 outbyte(FH, 1); // EI_VERSION
186 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000187
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000188 // This should change for shared objects.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000189 outhalf(FH, 1); // e_type = ET_REL
190 outhalf(FH, e_machine); // e_machine = whatever the target wants
191 outword(FH, 1); // e_version = 1
192 outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file
193 outaddr(FH, 0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000194
Chris Lattner5f48ff72005-07-16 08:01:13 +0000195 ELFHeader_e_shoff_Offset = FH.size();
196 outaddr(FH, 0); // e_shoff
197 outword(FH, e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000198
Chris Lattner5f48ff72005-07-16 08:01:13 +0000199 outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
200 outhalf(FH, 0); // e_phentsize = prog header entry size
201 outhalf(FH, 0); // e_phnum = # prog header entries = 0
202 outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000203
Jeff Cohen00b168892005-07-27 06:12:32 +0000204
Chris Lattner5f48ff72005-07-16 08:01:13 +0000205 ELFHeader_e_shnum_Offset = FH.size();
206 outhalf(FH, 0); // e_shnum = # of section header ents
207 ELFHeader_e_shstrndx_Offset = FH.size();
208 outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000209
Chris Lattner5f48ff72005-07-16 08:01:13 +0000210 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000211 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000212
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000213 // Start up the symbol table. The first entry in the symtab is the null
214 // entry.
215 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000216
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000217 return false;
218}
219
Chris Lattner05895232005-07-16 17:41:06 +0000220void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000221 // If this is an external global, emit it now. TODO: Note that it would be
222 // better to ignore the symbol here and only add it to the symbol table if
223 // referenced.
224 if (!GV->hasInitializer()) {
225 ELFSym ExternalSym(GV);
226 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
227 ExternalSym.SetType(ELFSym::STT_NOTYPE);
228 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
229 SymbolTable.push_back(ExternalSym);
230 return;
231 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000232
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000233 const Type *GVType = (const Type*)GV->getType();
234 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
235 unsigned Size = TM.getTargetData().getTypeSize(GVType);
236
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000237 // If this global has a zero initializer, it is part of the .bss or common
238 // section.
239 if (GV->getInitializer()->isNullValue()) {
240 // If this global is part of the common block, add it now. Variables are
241 // part of the common block if they are zero initialized and allowed to be
242 // merged with other symbols.
243 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
244 ELFSym CommonSym(GV);
245 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000246 CommonSym.Value = Align;
247 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000248 CommonSym.SetBind(ELFSym::STB_GLOBAL);
249 CommonSym.SetType(ELFSym::STT_OBJECT);
250 // TODO SOMEDAY: add ELF visibility.
251 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
252 SymbolTable.push_back(CommonSym);
253 return;
254 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000255
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000256 // Otherwise, this symbol is part of the .bss section. Emit it now.
257
258 // Handle alignment. Ensure section is aligned at least as much as required
259 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000260 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000261 BSSSection.Align = std::max(BSSSection.Align, Align);
262
263 // Within the section, emit enough virtual padding to get us to an alignment
264 // boundary.
265 if (Align)
266 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
267
268 ELFSym BSSSym(GV);
269 BSSSym.Value = BSSSection.Size;
270 BSSSym.Size = Size;
271 BSSSym.SetType(ELFSym::STT_OBJECT);
272
273 switch (GV->getLinkage()) {
274 default: // weak/linkonce handled above
275 assert(0 && "Unexpected linkage type!");
276 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
277 case GlobalValue::ExternalLinkage:
278 BSSSym.SetBind(ELFSym::STB_GLOBAL);
279 break;
280 case GlobalValue::InternalLinkage:
281 BSSSym.SetBind(ELFSym::STB_LOCAL);
282 break;
283 }
284
285 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000286 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000287 SymbolTable.push_back(BSSSym);
288
289 // Reserve space in the .bss section for this symbol.
290 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000291 return;
292 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000293
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000294 // FIXME: handle .rodata
295 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000296
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000297 // FIXME: handle .data
298 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000299}
300
301
302bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000303 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000304 return false;
305}
306
307/// doFinalization - Now that the module has been completely processed, emit
308/// the ELF file to 'O'.
309bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000310 // Okay, the ELF header and .text sections have been completed, build the
311 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000312 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
313 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000314 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000315
316 // Emit the symbol table now, if non-empty.
317 EmitSymbolTable();
318
319 // FIXME: Emit the relocations now.
320
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000321 // Emit the string table for the sections in the ELF file we have.
322 EmitSectionTableStringTable();
323
Chris Lattner5f48ff72005-07-16 08:01:13 +0000324 // Emit the sections to the .o file, and emit the section table for the file.
325 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000326
Chris Lattner5f48ff72005-07-16 08:01:13 +0000327 // We are done with the abstract symbols.
328 SectionList.clear();
329 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000330
331 // Release the name mangler object.
332 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000333 return false;
334}
335
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000336/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
337/// table for it and then the symbol table itself.
338void ELFWriter::EmitSymbolTable() {
339 if (SymbolTable.size() == 1) return; // Only the null entry.
340
341 // FIXME: compact all local symbols to the start of the symtab.
342 unsigned FirstNonLocalSymbol = 1;
343
Chris Lattner00033952005-07-16 17:36:04 +0000344 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000345 StrTab.Align = 1;
346
Chris Lattner5f48ff72005-07-16 08:01:13 +0000347 DataBuffer &StrTabBuf = StrTab.SectionData;
348
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000349 // Set the zero'th symbol to a null byte, as required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000350 outbyte(StrTabBuf, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000351 SymbolTable[0].NameIdx = 0;
352 unsigned Index = 1;
353 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000354 // Use the name mangler to uniquify the LLVM symbol.
355 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000356
357 if (Name.empty()) {
358 SymbolTable[i].NameIdx = 0;
359 } else {
360 SymbolTable[i].NameIdx = Index;
361
362 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000363 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000364
365 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000366 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000367
368 // Keep track of the number of bytes emitted to this section.
369 Index += Name.size()+1;
370 }
371 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000372 assert(Index == StrTabBuf.size());
373 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000374
375 // Now that we have emitted the string table and know the offset into the
376 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000377 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000378 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000379 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000380 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
381 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000382 DataBuffer &SymTabBuf = SymTab.SectionData;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000383
Chris Lattner46c53052005-07-12 06:57:52 +0000384 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
385 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
386 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000387 outword(SymTabBuf, Sym.NameIdx);
388 outaddr32(SymTabBuf, Sym.Value);
389 outword(SymTabBuf, Sym.Size);
390 outbyte(SymTabBuf, Sym.Info);
391 outbyte(SymTabBuf, Sym.Other);
392 outhalf(SymTabBuf, Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000393 }
394 } else {
395 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
396 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000397 outword(SymTabBuf, Sym.NameIdx);
398 outbyte(SymTabBuf, Sym.Info);
399 outbyte(SymTabBuf, Sym.Other);
400 outhalf(SymTabBuf, Sym.SectionIdx);
401 outaddr64(SymTabBuf, Sym.Value);
402 outxword(SymTabBuf, Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000403 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000404 }
405
Chris Lattner5f48ff72005-07-16 08:01:13 +0000406 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000407}
408
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000409/// EmitSectionTableStringTable - This method adds and emits a section for the
410/// ELF Section Table string table: the string table that holds all of the
411/// section names.
412void ELFWriter::EmitSectionTableStringTable() {
413 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000414 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000415
416 // Now that we know which section number is the .shstrtab section, update the
417 // e_shstrndx entry in the ELF header.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000418 fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000419
420 // Set the NameIdx of each section in the string table and emit the bytes for
421 // the string table.
422 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000423 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000424
Chris Lattner5f48ff72005-07-16 08:01:13 +0000425 for (std::list<ELFSection>::iterator I = SectionList.begin(),
426 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000427 // Set the index into the table. Note if we have lots of entries with
428 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000429 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000430
431 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000432 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
433
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000434 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000435 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000436
437 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000438 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000439 }
440
441 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000442 assert(Index == Buf.size());
443 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000444}
445
Chris Lattner5f48ff72005-07-16 08:01:13 +0000446/// OutputSectionsAndSectionTable - Now that we have constructed the file header
447/// and all of the sections, emit these to the ostream destination and emit the
448/// SectionTable.
449void ELFWriter::OutputSectionsAndSectionTable() {
450 // Pass #1: Compute the file offset for each section.
451 size_t FileOff = FileHeader.size(); // File header first.
452
453 // Emit all of the section data in order.
454 for (std::list<ELFSection>::iterator I = SectionList.begin(),
455 E = SectionList.end(); I != E; ++I) {
456 // Align FileOff to whatever the alignment restrictions of the section are.
457 if (I->Align)
458 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
459 I->Offset = FileOff;
460 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000461 }
462
Chris Lattner5f48ff72005-07-16 08:01:13 +0000463 // Align Section Header.
464 unsigned TableAlign = is64Bit ? 8 : 4;
465 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
466
467 // Now that we know where all of the sections will be emitted, set the e_shnum
468 // entry in the ELF header.
469 fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000470
Chris Lattner5f48ff72005-07-16 08:01:13 +0000471 // Now that we know the offset in the file of the section table, update the
472 // e_shoff address in the ELF header.
473 fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000474
Chris Lattner5f48ff72005-07-16 08:01:13 +0000475 // Now that we know all of the data in the file header, emit it and all of the
476 // sections!
477 O.write((char*)&FileHeader[0], FileHeader.size());
478 FileOff = FileHeader.size();
479 DataBuffer().swap(FileHeader);
480
481 DataBuffer Table;
482
483 // Emit all of the section data and build the section table itself.
484 while (!SectionList.empty()) {
485 const ELFSection &S = *SectionList.begin();
486
487 // Align FileOff to whatever the alignment restrictions of the section are.
488 if (S.Align)
489 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
490 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000491 O.put((char)0xAB);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000492 O.write((char*)&S.SectionData[0], S.SectionData.size());
493 FileOff += S.SectionData.size();
494
495 outword(Table, S.NameIdx); // sh_name - Symbol table name idx
496 outword(Table, S.Type); // sh_type - Section contents & semantics
497 outword(Table, S.Flags); // sh_flags - Section flags.
498 outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in.
499 outaddr(Table, S.Offset); // sh_offset - Offset from the file start.
500 outword(Table, S.Size); // sh_size - The section size.
501 outword(Table, S.Link); // sh_link - Section header table index link.
502 outword(Table, S.Info); // sh_info - Auxillary information.
503 outword(Table, S.Align); // sh_addralign - Alignment of section.
504 outword(Table, S.EntSize); // sh_entsize - Size of entries in the section.
505
506 SectionList.pop_front();
507 }
508
509 // Align output for the section table.
510 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
511 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000512 O.put((char)0xAB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000513
Chris Lattner5f48ff72005-07-16 08:01:13 +0000514 // Emit the section table itself.
515 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000516}