blob: cc8895df09d4482ca374e951da74451dda9812c4 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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.
28
29//
30// NOTE: This code should eventually be extended to support 64-bit ELF (this
31// won't be hard), but we haven't done so yet!
32//
33//===----------------------------------------------------------------------===//
34
35#include "llvm/CodeGen/ELFWriter.h"
36#include "llvm/Module.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000037#include "llvm/CodeGen/MachineCodeEmitter.h"
38#include "llvm/CodeGen/MachineConstantPool.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"
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;
52 std::vector<unsigned char> &OutputBuffer;
53 size_t FnStart;
54 public:
55 ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutputBuffer(EW.OutputBuffer) {}
56
Chris Lattner0e180502005-07-11 06:34:30 +000057 void startFunction(MachineFunction &F);
58 void finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000059
Chris Lattneraa507db2005-07-11 05:17:18 +000060 void emitConstantPool(MachineConstantPool *MCP) {
61 if (MCP->isEmpty()) return;
62 assert(0 && "unimp");
63 }
64 virtual void emitByte(unsigned char B) {
65 OutputBuffer.push_back(B);
66 }
67 virtual void emitWordAt(unsigned W, unsigned *Ptr) {
68 assert(0 && "ni");
69 }
70 virtual void emitWord(unsigned W) {
71 assert(0 && "ni");
72 }
73 virtual uint64_t getCurrentPCValue() {
74 return OutputBuffer.size();
75 }
76 virtual uint64_t getCurrentPCOffset() {
77 return OutputBuffer.size()-FnStart;
78 }
79 void addRelocation(const MachineRelocation &MR) {
80 assert(0 && "relo not handled yet!");
81 }
82 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) {
83 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000084 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000085 }
Chris Lattner0e180502005-07-11 06:34:30 +000086
87 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Chris Lattneraa507db2005-07-11 05:17:18 +000088 void startFunctionStub(unsigned StubSize) {
89 assert(0 && "JIT specific function called!");
90 abort();
91 }
92 void *finishFunctionStub(const Function *F) {
93 assert(0 && "JIT specific function called!");
94 abort();
95 return 0;
96 }
97 };
98}
99
Chris Lattner0e180502005-07-11 06:34:30 +0000100/// startFunction - This callback is invoked when a new machine function is
101/// about to be emitted.
102void ELFCodeEmitter::startFunction(MachineFunction &F) {
103 // Align the output buffer to the appropriate alignment.
104 unsigned Align = 16; // FIXME: GENERICIZE!!
105 ELFWriter::ELFSection &TextSection = EW.SectionList.back();
106
107 // Upgrade the section alignment if required.
108 if (TextSection.Align < Align) TextSection.Align = Align;
109
110 // Add padding zeros to the end of the buffer to make sure that the
111 // function will start on the correct byte alignment within the section.
112 size_t SectionOff = OutputBuffer.size()-TextSection.Offset;
113 if (SectionOff & (Align-1)) {
114 // Add padding to get alignment to the correct place.
115 size_t Pad = Align-(SectionOff & (Align-1));
116 OutputBuffer.resize(OutputBuffer.size()+Pad);
117 }
118
119 FnStart = OutputBuffer.size();
120}
121
122/// finishFunction - This callback is invoked after the function is completely
123/// finished.
124void ELFCodeEmitter::finishFunction(MachineFunction &F) {
125 // We now know the size of the function, add a symbol to represent it.
126 ELFWriter::ELFSym FnSym(F.getFunction());
127
128 // Figure out the binding (linkage) of the symbol.
129 switch (F.getFunction()->getLinkage()) {
130 default:
131 // appending linkage is illegal for functions.
132 assert(0 && "Unknown linkage type!");
133 case GlobalValue::ExternalLinkage:
134 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
135 break;
136 case GlobalValue::LinkOnceLinkage:
137 case GlobalValue::WeakLinkage:
138 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
139 break;
140 case GlobalValue::InternalLinkage:
141 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
142 break;
143 }
144
145 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
146 FnSym.SectionIdx = EW.SectionList.size()-1; // .text section.
147 // Value = Offset from start of .text
148 FnSym.Value = FnStart - EW.SectionList.back().Offset;
149 FnSym.Size = OutputBuffer.size()-FnStart;
150
151 // Finally, add it to the symtab.
152 EW.SymbolTable.push_back(FnSym);
153}
154
155//===----------------------------------------------------------------------===//
156// ELFWriter Implementation
157//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000158
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000159ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
160 e_machine = 0; // e_machine defaults to 'No Machine'
161 e_flags = 0; // e_flags defaults to 0, no flags.
162
163 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
164 isLittleEndian = TM.getTargetData().isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000165
166 // Create the machine code emitter object for this target.
167 MCE = new ELFCodeEmitter(*this);
168}
169
170ELFWriter::~ELFWriter() {
171 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000172}
173
174// doInitialization - Emit the file header and all of the global variables for
175// the module to the ELF file.
176bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000177 Mang = new Mangler(M);
178
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000179 outbyte(0x7F); // EI_MAG0
180 outbyte('E'); // EI_MAG1
181 outbyte('L'); // EI_MAG2
182 outbyte('F'); // EI_MAG3
183 outbyte(is64Bit ? 2 : 1); // EI_CLASS
184 outbyte(isLittleEndian ? 1 : 2); // EI_DATA
185 outbyte(1); // EI_VERSION
186 for (unsigned i = OutputBuffer.size(); i != 16; ++i)
187 outbyte(0); // EI_PAD up to 16 bytes.
188
189 // This should change for shared objects.
190 outhalf(1); // e_type = ET_REL
191 outhalf(e_machine); // e_machine = whatever the target wants
192 outword(1); // e_version = 1
193 outaddr(0); // e_entry = 0 -> no entry point in .o file
194 outaddr(0); // e_phoff = 0 -> no program header for .o
195
196 ELFHeader_e_shoff_Offset = OutputBuffer.size();
197 outaddr(0); // e_shoff
198 outword(e_flags); // e_flags = whatever the target wants
199
Chris Lattner46c53052005-07-12 06:57:52 +0000200 outhalf(is64Bit ? 64 : 52); // e_ehsize = ELF header size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000201 outhalf(0); // e_phentsize = prog header entry size
202 outhalf(0); // e_phnum = # prog header entries = 0
Chris Lattner46c53052005-07-12 06:57:52 +0000203 outhalf(is64Bit ? 64 : 40); // e_shentsize = sect header entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000204
205
206 ELFHeader_e_shnum_Offset = OutputBuffer.size();
207 outhalf(0); // e_shnum = # of section header ents
208 ELFHeader_e_shstrndx_Offset = OutputBuffer.size();
209 outhalf(0); // e_shstrndx = Section # of '.shstrtab'
210
211 // Add the null section.
212 SectionList.push_back(ELFSection());
213
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000214 // Start up the symbol table. The first entry in the symtab is the null
215 // entry.
216 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000217
Chris Lattneraa507db2005-07-11 05:17:18 +0000218 SectionList.push_back(ELFSection(".text", OutputBuffer.size()));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000219
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000220 return false;
221}
222
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000223void ELFWriter::EmitGlobal(GlobalVariable *GV, ELFSection &DataSection,
224 ELFSection &BSSSection) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000225 // If this is an external global, emit it now. TODO: Note that it would be
226 // better to ignore the symbol here and only add it to the symbol table if
227 // referenced.
228 if (!GV->hasInitializer()) {
229 ELFSym ExternalSym(GV);
230 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
231 ExternalSym.SetType(ELFSym::STT_NOTYPE);
232 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
233 SymbolTable.push_back(ExternalSym);
234 return;
235 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000236
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000237 const Type *GVType = (const Type*)GV->getType();
238 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
239 unsigned Size = TM.getTargetData().getTypeSize(GVType);
240
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000241 // If this global has a zero initializer, it is part of the .bss or common
242 // section.
243 if (GV->getInitializer()->isNullValue()) {
244 // If this global is part of the common block, add it now. Variables are
245 // part of the common block if they are zero initialized and allowed to be
246 // merged with other symbols.
247 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
248 ELFSym CommonSym(GV);
249 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000250 CommonSym.Value = Align;
251 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000252 CommonSym.SetBind(ELFSym::STB_GLOBAL);
253 CommonSym.SetType(ELFSym::STT_OBJECT);
254 // TODO SOMEDAY: add ELF visibility.
255 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
256 SymbolTable.push_back(CommonSym);
257 return;
258 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000259
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000260 // Otherwise, this symbol is part of the .bss section. Emit it now.
261
262 // Handle alignment. Ensure section is aligned at least as much as required
263 // by this symbol.
264 BSSSection.Align = std::max(BSSSection.Align, Align);
265
266 // Within the section, emit enough virtual padding to get us to an alignment
267 // boundary.
268 if (Align)
269 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
270
271 ELFSym BSSSym(GV);
272 BSSSym.Value = BSSSection.Size;
273 BSSSym.Size = Size;
274 BSSSym.SetType(ELFSym::STT_OBJECT);
275
276 switch (GV->getLinkage()) {
277 default: // weak/linkonce handled above
278 assert(0 && "Unexpected linkage type!");
279 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
280 case GlobalValue::ExternalLinkage:
281 BSSSym.SetBind(ELFSym::STB_GLOBAL);
282 break;
283 case GlobalValue::InternalLinkage:
284 BSSSym.SetBind(ELFSym::STB_LOCAL);
285 break;
286 }
287
288 // Set the idx of the .bss section
289 BSSSym.SectionIdx = &BSSSection-&SectionList[0];
290 SymbolTable.push_back(BSSSym);
291
292 // Reserve space in the .bss section for this symbol.
293 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000294 return;
295 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000296
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000297 // FIXME: handle .rodata
298 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000299
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000300 // FIXME: handle .data
301 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000302}
303
304
305bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000306 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000307 return false;
308}
309
310/// doFinalization - Now that the module has been completely processed, emit
311/// the ELF file to 'O'.
312bool ELFWriter::doFinalization(Module &M) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000313 // Okay, the .text section has now been finalized. If it contains nothing, do
314 // not emit it.
315 uint64_t TextSize = OutputBuffer.size() - SectionList.back().Offset;
316 if (TextSize == 0) {
317 SectionList.pop_back();
318 } else {
319 ELFSection &Text = SectionList.back();
320 Text.Size = TextSize;
321 Text.Type = ELFSection::SHT_PROGBITS;
322 Text.Flags = ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC;
323 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000324
325 // Okay, the ELF header and .text sections have been completed, build the
326 // .data, .bss, and "common" sections next.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000327 SectionList.push_back(ELFSection(".data", OutputBuffer.size()));
328 SectionList.push_back(ELFSection(".bss"));
329 ELFSection &DataSection = *(SectionList.end()-2);
330 ELFSection &BSSSection = SectionList.back();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000331 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
332 I != E; ++I)
333 EmitGlobal(I, DataSection, BSSSection);
334
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000335 // Finish up the data section.
336 DataSection.Type = ELFSection::SHT_PROGBITS;
337 DataSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000338
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000339 // The BSS Section logically starts at the end of the Data Section (adjusted
340 // to the required alignment of the BSSSection).
341 BSSSection.Offset = DataSection.Offset+DataSection.Size;
342 BSSSection.Type = ELFSection::SHT_NOBITS;
343 BSSSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC;
344 if (BSSSection.Align)
345 BSSSection.Offset = (BSSSection.Offset+BSSSection.Align-1) &
346 ~(BSSSection.Align-1);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000347
348 // Emit the symbol table now, if non-empty.
349 EmitSymbolTable();
350
351 // FIXME: Emit the relocations now.
352
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000353 // Emit the string table for the sections in the ELF file we have.
354 EmitSectionTableStringTable();
355
356 // Emit the .o file section table.
357 EmitSectionTable();
358
359 // Emit the .o file to the specified stream.
360 O.write((char*)&OutputBuffer[0], OutputBuffer.size());
361
362 // Free the output buffer.
363 std::vector<unsigned char>().swap(OutputBuffer);
Chris Lattner5acd1202005-07-11 03:11:47 +0000364
365 // Release the name mangler object.
366 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000367 return false;
368}
369
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000370/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
371/// table for it and then the symbol table itself.
372void ELFWriter::EmitSymbolTable() {
373 if (SymbolTable.size() == 1) return; // Only the null entry.
374
375 // FIXME: compact all local symbols to the start of the symtab.
376 unsigned FirstNonLocalSymbol = 1;
377
378 SectionList.push_back(ELFSection(".strtab", OutputBuffer.size()));
379 ELFSection &StrTab = SectionList.back();
380 StrTab.Type = ELFSection::SHT_STRTAB;
381 StrTab.Align = 1;
382
383 // Set the zero'th symbol to a null byte, as required.
384 outbyte(0);
385 SymbolTable[0].NameIdx = 0;
386 unsigned Index = 1;
387 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000388 // Use the name mangler to uniquify the LLVM symbol.
389 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000390
391 if (Name.empty()) {
392 SymbolTable[i].NameIdx = 0;
393 } else {
394 SymbolTable[i].NameIdx = Index;
395
396 // Add the name to the output buffer, including the null terminator.
397 OutputBuffer.insert(OutputBuffer.end(), Name.begin(), Name.end());
398
399 // Add a null terminator.
400 OutputBuffer.push_back(0);
401
402 // Keep track of the number of bytes emitted to this section.
403 Index += Name.size()+1;
404 }
405 }
406
407 StrTab.Size = OutputBuffer.size()-StrTab.Offset;
408
409 // Now that we have emitted the string table and know the offset into the
410 // string table of each symbol, emit the symbol table itself.
Chris Lattner46c53052005-07-12 06:57:52 +0000411 align(is64Bit ? 8 : 4);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000412
413 SectionList.push_back(ELFSection(".symtab", OutputBuffer.size()));
414 ELFSection &SymTab = SectionList.back();
415 SymTab.Type = ELFSection::SHT_SYMTAB;
Chris Lattner46c53052005-07-12 06:57:52 +0000416 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000417 SymTab.Link = SectionList.size()-2; // Section Index of .strtab.
418 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
419 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
420
Chris Lattner46c53052005-07-12 06:57:52 +0000421 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
422 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
423 ELFSym &Sym = SymbolTable[i];
424 outword(Sym.NameIdx);
425 outaddr32(Sym.Value);
426 outword(Sym.Size);
427 outbyte(Sym.Info);
428 outbyte(Sym.Other);
429 outhalf(Sym.SectionIdx);
430 }
431 } else {
432 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
433 ELFSym &Sym = SymbolTable[i];
434 outword(Sym.NameIdx);
435 outbyte(Sym.Info);
436 outbyte(Sym.Other);
437 outhalf(Sym.SectionIdx);
438 outaddr64(Sym.Value);
439 outxword(Sym.Size);
440 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000441 }
442
443 SymTab.Size = OutputBuffer.size()-SymTab.Offset;
444}
445
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000446/// EmitSectionTableStringTable - This method adds and emits a section for the
447/// ELF Section Table string table: the string table that holds all of the
448/// section names.
449void ELFWriter::EmitSectionTableStringTable() {
450 // First step: add the section for the string table to the list of sections:
451 SectionList.push_back(ELFSection(".shstrtab", OutputBuffer.size()));
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000452 SectionList.back().Type = ELFSection::SHT_STRTAB;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000453
454 // Now that we know which section number is the .shstrtab section, update the
455 // e_shstrndx entry in the ELF header.
456 fixhalf(SectionList.size()-1, ELFHeader_e_shstrndx_Offset);
457
458 // Set the NameIdx of each section in the string table and emit the bytes for
459 // the string table.
460 unsigned Index = 0;
461
462 for (unsigned i = 0, e = SectionList.size(); i != e; ++i) {
463 // Set the index into the table. Note if we have lots of entries with
464 // common suffixes, we could memoize them here if we cared.
465 SectionList[i].NameIdx = Index;
466
467 // Add the name to the output buffer, including the null terminator.
468 OutputBuffer.insert(OutputBuffer.end(), SectionList[i].Name.begin(),
469 SectionList[i].Name.end());
470 // Add a null terminator.
471 OutputBuffer.push_back(0);
472
473 // Keep track of the number of bytes emitted to this section.
474 Index += SectionList[i].Name.size()+1;
475 }
476
477 // Set the size of .shstrtab now that we know what it is.
478 SectionList.back().Size = Index;
479}
480
481/// EmitSectionTable - Now that we have emitted the entire contents of the file
482/// (all of the sections), emit the section table which informs the reader where
483/// the boundaries are.
484void ELFWriter::EmitSectionTable() {
485 // Now that all of the sections have been emitted, set the e_shnum entry in
486 // the ELF header.
487 fixhalf(SectionList.size(), ELFHeader_e_shnum_Offset);
488
489 // Now that we know the offset in the file of the section table (which we emit
490 // next), update the e_shoff address in the ELF header.
491 fixaddr(OutputBuffer.size(), ELFHeader_e_shoff_Offset);
492
493 // Emit all of the section table entries.
494 for (unsigned i = 0, e = SectionList.size(); i != e; ++i) {
495 const ELFSection &S = SectionList[i];
496 outword(S.NameIdx); // sh_name - Symbol table name idx
497 outword(S.Type); // sh_type - Section contents & semantics
498 outword(S.Flags); // sh_flags - Section flags.
499 outaddr(S.Addr); // sh_addr - The mem address this section appears in.
500 outaddr(S.Offset); // sh_offset - The offset from the start of the file.
501 outword(S.Size); // sh_size - The section size.
502 outword(S.Link); // sh_link - Section header table index link.
503 outword(S.Info); // sh_info - Auxillary information.
504 outword(S.Align); // sh_addralign - Alignment of section.
505 outword(S.EntSize); // sh_entsize - Size of each entry in the section.
506 }
507
508 // Release the memory allocated for the section list.
509 std::vector<ELFSection>().swap(SectionList);
510}