blob: afd6c3b05ca6978cedad97efb0006d6b8a86d90d [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.
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 Lattner2244f732005-07-11 05:17:18 +000037#include "llvm/CodeGen/MachineCodeEmitter.h"
38#include "llvm/CodeGen/MachineConstantPool.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"
Chris Lattner386b1512005-06-27 06:29:00 +000041using namespace llvm;
42
Chris Lattner449e07f2005-07-11 06:34:30 +000043//===----------------------------------------------------------------------===//
44// ELFCodeEmitter Implementation
45//===----------------------------------------------------------------------===//
46
Chris Lattner2244f732005-07-11 05:17:18 +000047namespace llvm {
Chris Lattner449e07f2005-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 Lattner2244f732005-07-11 05:17:18 +000050 class ELFCodeEmitter : public MachineCodeEmitter {
51 ELFWriter &EW;
Chris Lattnerfd445002005-07-16 08:01:13 +000052 ELFWriter::ELFSection *ES; // Section to write to.
53 std::vector<unsigned char> *OutBuffer;
Chris Lattner2244f732005-07-11 05:17:18 +000054 size_t FnStart;
55 public:
Chris Lattnerfd445002005-07-16 08:01:13 +000056 ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {}
Chris Lattner2244f732005-07-11 05:17:18 +000057
Chris Lattner449e07f2005-07-11 06:34:30 +000058 void startFunction(MachineFunction &F);
59 void finishFunction(MachineFunction &F);
Chris Lattner5bacb002005-07-11 06:17:35 +000060
Chris Lattner2244f732005-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 Lattnerfd445002005-07-16 08:01:13 +000066 OutBuffer->push_back(B);
Chris Lattner2244f732005-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 Lattnerfd445002005-07-16 08:01:13 +000075 return OutBuffer->size();
Chris Lattner2244f732005-07-11 05:17:18 +000076 }
77 virtual uint64_t getCurrentPCOffset() {
Chris Lattnerfd445002005-07-16 08:01:13 +000078 return OutBuffer->size()-FnStart;
Chris Lattner2244f732005-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 Cohen33b82322005-07-12 02:53:33 +000085 return 0;
Chris Lattner2244f732005-07-11 05:17:18 +000086 }
Chris Lattner449e07f2005-07-11 06:34:30 +000087
88 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Chris Lattner2244f732005-07-11 05:17:18 +000089 void startFunctionStub(unsigned StubSize) {
90 assert(0 && "JIT specific function called!");
91 abort();
92 }
93 void *finishFunctionStub(const Function *F) {
94 assert(0 && "JIT specific function called!");
95 abort();
96 return 0;
97 }
98 };
99}
100
Chris Lattner449e07f2005-07-11 06:34:30 +0000101/// startFunction - This callback is invoked when a new machine function is
102/// about to be emitted.
103void ELFCodeEmitter::startFunction(MachineFunction &F) {
104 // Align the output buffer to the appropriate alignment.
105 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattnerfd445002005-07-16 08:01:13 +0000106 // Get the ELF Section that this function belongs in.
Chris Lattner363964e2005-07-16 17:36:04 +0000107 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
108 ELFWriter::ELFSection::SHF_EXECINSTR |
109 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattnerfd445002005-07-16 08:01:13 +0000110 OutBuffer = &ES->SectionData;
Chris Lattner449e07f2005-07-11 06:34:30 +0000111
112 // Upgrade the section alignment if required.
Chris Lattnerfd445002005-07-16 08:01:13 +0000113 if (ES->Align < Align) ES->Align = Align;
Chris Lattner449e07f2005-07-11 06:34:30 +0000114
115 // Add padding zeros to the end of the buffer to make sure that the
116 // function will start on the correct byte alignment within the section.
Chris Lattnerfd445002005-07-16 08:01:13 +0000117 size_t SectionOff = OutBuffer->size();
118 ELFWriter::align(*OutBuffer, Align);
Chris Lattner449e07f2005-07-11 06:34:30 +0000119
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.
125void ELFCodeEmitter::finishFunction(MachineFunction &F) {
126 // We now know the size of the function, add a symbol to represent it.
127 ELFWriter::ELFSym FnSym(F.getFunction());
128
129 // 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;
150 FnSym.Value = FnStart; // Value = Offset from start of Section.
151 FnSym.Size = OutBuffer->size()-FnStart;
Chris Lattner449e07f2005-07-11 06:34:30 +0000152
153 // Finally, add it to the symtab.
154 EW.SymbolTable.push_back(FnSym);
155}
156
157//===----------------------------------------------------------------------===//
158// ELFWriter Implementation
159//===----------------------------------------------------------------------===//
Chris Lattner2244f732005-07-11 05:17:18 +0000160
Chris Lattner386b1512005-06-27 06:29:00 +0000161ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
162 e_machine = 0; // e_machine defaults to 'No Machine'
163 e_flags = 0; // e_flags defaults to 0, no flags.
164
165 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
166 isLittleEndian = TM.getTargetData().isLittleEndian();
Chris Lattner2244f732005-07-11 05:17:18 +0000167
168 // Create the machine code emitter object for this target.
169 MCE = new ELFCodeEmitter(*this);
Chris Lattnerfd445002005-07-16 08:01:13 +0000170 NumSections = 0;
Chris Lattner2244f732005-07-11 05:17:18 +0000171}
172
173ELFWriter::~ELFWriter() {
174 delete MCE;
Chris Lattner386b1512005-06-27 06:29:00 +0000175}
176
177// doInitialization - Emit the file header and all of the global variables for
178// the module to the ELF file.
179bool ELFWriter::doInitialization(Module &M) {
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000180 Mang = new Mangler(M);
181
Chris Lattnerfd445002005-07-16 08:01:13 +0000182 // Local alias to shortenify coming code.
183 std::vector<unsigned char> &FH = FileHeader;
184
185 outbyte(FH, 0x7F); // EI_MAG0
186 outbyte(FH, 'E'); // EI_MAG1
187 outbyte(FH, 'L'); // EI_MAG2
188 outbyte(FH, 'F'); // EI_MAG3
189 outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS
190 outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA
191 outbyte(FH, 1); // EI_VERSION
192 FH.resize(16); // EI_PAD up to 16 bytes.
Chris Lattner386b1512005-06-27 06:29:00 +0000193
194 // This should change for shared objects.
Chris Lattnerfd445002005-07-16 08:01:13 +0000195 outhalf(FH, 1); // e_type = ET_REL
196 outhalf(FH, e_machine); // e_machine = whatever the target wants
197 outword(FH, 1); // e_version = 1
198 outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file
199 outaddr(FH, 0); // e_phoff = 0 -> no program header for .o
Chris Lattner386b1512005-06-27 06:29:00 +0000200
Chris Lattnerfd445002005-07-16 08:01:13 +0000201 ELFHeader_e_shoff_Offset = FH.size();
202 outaddr(FH, 0); // e_shoff
203 outword(FH, e_flags); // e_flags = whatever the target wants
Chris Lattner386b1512005-06-27 06:29:00 +0000204
Chris Lattnerfd445002005-07-16 08:01:13 +0000205 outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
206 outhalf(FH, 0); // e_phentsize = prog header entry size
207 outhalf(FH, 0); // e_phnum = # prog header entries = 0
208 outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner386b1512005-06-27 06:29:00 +0000209
210
Chris Lattnerfd445002005-07-16 08:01:13 +0000211 ELFHeader_e_shnum_Offset = FH.size();
212 outhalf(FH, 0); // e_shnum = # of section header ents
213 ELFHeader_e_shstrndx_Offset = FH.size();
214 outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner386b1512005-06-27 06:29:00 +0000215
Chris Lattnerfd445002005-07-16 08:01:13 +0000216 // Add the null section, which is required to be first in the file.
Chris Lattner363964e2005-07-16 17:36:04 +0000217 getSection("", 0, 0);
Chris Lattner386b1512005-06-27 06:29:00 +0000218
Chris Lattner1932f5c2005-07-07 07:02:20 +0000219 // Start up the symbol table. The first entry in the symtab is the null
220 // entry.
221 SymbolTable.push_back(ELFSym(0));
Chris Lattner386b1512005-06-27 06:29:00 +0000222
Chris Lattner386b1512005-06-27 06:29:00 +0000223 return false;
224}
225
Chris Lattner1932f5c2005-07-07 07:02:20 +0000226void ELFWriter::EmitGlobal(GlobalVariable *GV, ELFSection &DataSection,
227 ELFSection &BSSSection) {
Chris Lattner748de6e2005-07-08 05:47:00 +0000228 // If this is an external global, emit it now. TODO: Note that it would be
229 // better to ignore the symbol here and only add it to the symbol table if
230 // referenced.
231 if (!GV->hasInitializer()) {
232 ELFSym ExternalSym(GV);
233 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
234 ExternalSym.SetType(ELFSym::STT_NOTYPE);
235 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
236 SymbolTable.push_back(ExternalSym);
237 return;
238 }
Chris Lattner1932f5c2005-07-07 07:02:20 +0000239
Chris Lattner748de6e2005-07-08 05:47:00 +0000240 const Type *GVType = (const Type*)GV->getType();
241 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
242 unsigned Size = TM.getTargetData().getTypeSize(GVType);
243
Chris Lattner1932f5c2005-07-07 07:02:20 +0000244 // If this global has a zero initializer, it is part of the .bss or common
245 // section.
246 if (GV->getInitializer()->isNullValue()) {
247 // If this global is part of the common block, add it now. Variables are
248 // part of the common block if they are zero initialized and allowed to be
249 // merged with other symbols.
250 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
251 ELFSym CommonSym(GV);
252 // Value for common symbols is the alignment required.
Chris Lattner748de6e2005-07-08 05:47:00 +0000253 CommonSym.Value = Align;
254 CommonSym.Size = Size;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000255 CommonSym.SetBind(ELFSym::STB_GLOBAL);
256 CommonSym.SetType(ELFSym::STT_OBJECT);
257 // TODO SOMEDAY: add ELF visibility.
258 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
259 SymbolTable.push_back(CommonSym);
260 return;
261 }
Chris Lattner386b1512005-06-27 06:29:00 +0000262
Chris Lattner748de6e2005-07-08 05:47:00 +0000263 // Otherwise, this symbol is part of the .bss section. Emit it now.
264
265 // Handle alignment. Ensure section is aligned at least as much as required
266 // by this symbol.
267 BSSSection.Align = std::max(BSSSection.Align, Align);
268
269 // Within the section, emit enough virtual padding to get us to an alignment
270 // boundary.
271 if (Align)
272 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
273
274 ELFSym BSSSym(GV);
275 BSSSym.Value = BSSSection.Size;
276 BSSSym.Size = Size;
277 BSSSym.SetType(ELFSym::STT_OBJECT);
278
279 switch (GV->getLinkage()) {
280 default: // weak/linkonce handled above
281 assert(0 && "Unexpected linkage type!");
282 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
283 case GlobalValue::ExternalLinkage:
284 BSSSym.SetBind(ELFSym::STB_GLOBAL);
285 break;
286 case GlobalValue::InternalLinkage:
287 BSSSym.SetBind(ELFSym::STB_LOCAL);
288 break;
289 }
290
291 // Set the idx of the .bss section
Chris Lattnerfd445002005-07-16 08:01:13 +0000292 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner748de6e2005-07-08 05:47:00 +0000293 SymbolTable.push_back(BSSSym);
294
295 // Reserve space in the .bss section for this symbol.
296 BSSSection.Size += Size;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000297 return;
298 }
Chris Lattner386b1512005-06-27 06:29:00 +0000299
Chris Lattner1932f5c2005-07-07 07:02:20 +0000300 // FIXME: handle .rodata
301 //assert(!GV->isConstant() && "unimp");
Chris Lattner386b1512005-06-27 06:29:00 +0000302
Chris Lattner1932f5c2005-07-07 07:02:20 +0000303 // FIXME: handle .data
304 //assert(0 && "unimp");
Chris Lattner386b1512005-06-27 06:29:00 +0000305}
306
307
308bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner2244f732005-07-11 05:17:18 +0000309 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner386b1512005-06-27 06:29:00 +0000310 return false;
311}
312
313/// doFinalization - Now that the module has been completely processed, emit
314/// the ELF file to 'O'.
315bool ELFWriter::doFinalization(Module &M) {
Chris Lattner1932f5c2005-07-07 07:02:20 +0000316 // Okay, the ELF header and .text sections have been completed, build the
317 // .data, .bss, and "common" sections next.
Chris Lattner363964e2005-07-16 17:36:04 +0000318 ELFSection &DataSection =
319 getSection(".data", ELFSection::SHT_PROGBITS,
320 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000321
Chris Lattner363964e2005-07-16 17:36:04 +0000322 ELFSection &BSSSection =
323 getSection(".bss", ELFSection::SHT_NOBITS,
324 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC);
Chris Lattnerfd445002005-07-16 08:01:13 +0000325
326 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
327 I != E; ++I)
328 EmitGlobal(I, DataSection, BSSSection);
Chris Lattner1932f5c2005-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 Lattner386b1512005-06-27 06:29:00 +0000335 // Emit the string table for the sections in the ELF file we have.
336 EmitSectionTableStringTable();
337
Chris Lattnerfd445002005-07-16 08:01:13 +0000338 // Emit the sections to the .o file, and emit the section table for the file.
339 OutputSectionsAndSectionTable();
Chris Lattner386b1512005-06-27 06:29:00 +0000340
Chris Lattnerfd445002005-07-16 08:01:13 +0000341 // We are done with the abstract symbols.
342 SectionList.clear();
343 NumSections = 0;
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000344
345 // Release the name mangler object.
346 delete Mang; Mang = 0;
Chris Lattner386b1512005-06-27 06:29:00 +0000347 return false;
348}
349
Chris Lattner1932f5c2005-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 Lattner363964e2005-07-16 17:36:04 +0000358 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000359 StrTab.Align = 1;
360
Chris Lattnerfd445002005-07-16 08:01:13 +0000361 DataBuffer &StrTabBuf = StrTab.SectionData;
362
Chris Lattner1932f5c2005-07-07 07:02:20 +0000363 // Set the zero'th symbol to a null byte, as required.
Chris Lattnerfd445002005-07-16 08:01:13 +0000364 outbyte(StrTabBuf, 0);
Chris Lattner1932f5c2005-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 Lattnerdfe33bc2005-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 Lattner1932f5c2005-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 Lattnerfd445002005-07-16 08:01:13 +0000377 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner1932f5c2005-07-07 07:02:20 +0000378
379 // Add a null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000380 StrTabBuf.push_back(0);
Chris Lattner1932f5c2005-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 Lattnerfd445002005-07-16 08:01:13 +0000386 assert(Index == StrTabBuf.size());
387 StrTab.Size = Index;
Chris Lattner1932f5c2005-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 Lattner363964e2005-07-16 17:36:04 +0000391 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner298ac692005-07-12 06:57:52 +0000392 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattnerfd445002005-07-16 08:01:13 +0000393 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner1932f5c2005-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 Lattnerfd445002005-07-16 08:01:13 +0000396 DataBuffer &SymTabBuf = SymTab.SectionData;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000397
Chris Lattner298ac692005-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 Lattnerfd445002005-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 Lattner298ac692005-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 Lattnerfd445002005-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 Lattner298ac692005-07-12 06:57:52 +0000417 }
Chris Lattner1932f5c2005-07-07 07:02:20 +0000418 }
419
Chris Lattnerfd445002005-07-16 08:01:13 +0000420 SymTab.Size = SymTabBuf.size();
Chris Lattner1932f5c2005-07-07 07:02:20 +0000421}
422
Chris Lattner386b1512005-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 Lattner363964e2005-07-16 17:36:04 +0000428 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner386b1512005-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 Lattnerfd445002005-07-16 08:01:13 +0000432 fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner386b1512005-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 Lattnerfd445002005-07-16 08:01:13 +0000437 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner386b1512005-06-27 06:29:00 +0000438
Chris Lattnerfd445002005-07-16 08:01:13 +0000439 for (std::list<ELFSection>::iterator I = SectionList.begin(),
440 E = SectionList.end(); I != E; ++I) {
Chris Lattner386b1512005-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 Lattnerfd445002005-07-16 08:01:13 +0000443 I->NameIdx = Index;
Chris Lattner386b1512005-06-27 06:29:00 +0000444
445 // Add the name to the output buffer, including the null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000446 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
447
Chris Lattner386b1512005-06-27 06:29:00 +0000448 // Add a null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000449 Buf.push_back(0);
Chris Lattner386b1512005-06-27 06:29:00 +0000450
451 // Keep track of the number of bytes emitted to this section.
Chris Lattnerfd445002005-07-16 08:01:13 +0000452 Index += I->Name.size()+1;
Chris Lattner386b1512005-06-27 06:29:00 +0000453 }
454
455 // Set the size of .shstrtab now that we know what it is.
Chris Lattnerfd445002005-07-16 08:01:13 +0000456 assert(Index == Buf.size());
457 SHStrTab.Size = Index;
Chris Lattner386b1512005-06-27 06:29:00 +0000458}
459
Chris Lattnerfd445002005-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 Lattner386b1512005-06-27 06:29:00 +0000475 }
476
Chris Lattnerfd445002005-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);
484
485 // 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);
488
489 // 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)
505 O.put(0xAB);
506 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)
526 O.put(0xAB);
527
528 // Emit the section table itself.
529 O.write((char*)&Table[0], Table.size());
Chris Lattner386b1512005-06-27 06:29:00 +0000530}