blob: c9d1ae4041c87ebe407accd0998a178b697eaa2a [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//
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 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.
107 ES = &EW.getSection(".text");
108 ES->Type = ELFWriter::ELFSection::SHT_PROGBITS;
109 ES->Flags = ELFWriter::ELFSection::SHF_EXECINSTR |
110 ELFWriter::ELFSection::SHF_ALLOC;
111 OutBuffer = &ES->SectionData;
Chris Lattner449e07f2005-07-11 06:34:30 +0000112
113 // Upgrade the section alignment if required.
Chris Lattnerfd445002005-07-16 08:01:13 +0000114 if (ES->Align < Align) ES->Align = Align;
Chris Lattner449e07f2005-07-11 06:34:30 +0000115
116 // Add padding zeros to the end of the buffer to make sure that the
117 // function will start on the correct byte alignment within the section.
Chris Lattnerfd445002005-07-16 08:01:13 +0000118 size_t SectionOff = OutBuffer->size();
119 ELFWriter::align(*OutBuffer, Align);
Chris Lattner449e07f2005-07-11 06:34:30 +0000120
Chris Lattnerfd445002005-07-16 08:01:13 +0000121 FnStart = OutBuffer->size();
Chris Lattner449e07f2005-07-11 06:34:30 +0000122}
123
124/// finishFunction - This callback is invoked after the function is completely
125/// finished.
126void ELFCodeEmitter::finishFunction(MachineFunction &F) {
127 // We now know the size of the function, add a symbol to represent it.
128 ELFWriter::ELFSym FnSym(F.getFunction());
129
130 // Figure out the binding (linkage) of the symbol.
131 switch (F.getFunction()->getLinkage()) {
132 default:
133 // appending linkage is illegal for functions.
134 assert(0 && "Unknown linkage type!");
135 case GlobalValue::ExternalLinkage:
136 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
137 break;
138 case GlobalValue::LinkOnceLinkage:
139 case GlobalValue::WeakLinkage:
140 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
141 break;
142 case GlobalValue::InternalLinkage:
143 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
144 break;
145 }
Chris Lattnerfd445002005-07-16 08:01:13 +0000146
147 ES->Size = OutBuffer->size();
148
Chris Lattner449e07f2005-07-11 06:34:30 +0000149 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattnerfd445002005-07-16 08:01:13 +0000150 FnSym.SectionIdx = ES->SectionIdx;
151 FnSym.Value = FnStart; // Value = Offset from start of Section.
152 FnSym.Size = OutBuffer->size()-FnStart;
Chris Lattner449e07f2005-07-11 06:34:30 +0000153
154 // Finally, add it to the symtab.
155 EW.SymbolTable.push_back(FnSym);
156}
157
158//===----------------------------------------------------------------------===//
159// ELFWriter Implementation
160//===----------------------------------------------------------------------===//
Chris Lattner2244f732005-07-11 05:17:18 +0000161
Chris Lattner386b1512005-06-27 06:29:00 +0000162ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
163 e_machine = 0; // e_machine defaults to 'No Machine'
164 e_flags = 0; // e_flags defaults to 0, no flags.
165
166 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
167 isLittleEndian = TM.getTargetData().isLittleEndian();
Chris Lattner2244f732005-07-11 05:17:18 +0000168
169 // Create the machine code emitter object for this target.
170 MCE = new ELFCodeEmitter(*this);
Chris Lattnerfd445002005-07-16 08:01:13 +0000171 NumSections = 0;
Chris Lattner2244f732005-07-11 05:17:18 +0000172}
173
174ELFWriter::~ELFWriter() {
175 delete MCE;
Chris Lattner386b1512005-06-27 06:29:00 +0000176}
177
178// doInitialization - Emit the file header and all of the global variables for
179// the module to the ELF file.
180bool ELFWriter::doInitialization(Module &M) {
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000181 Mang = new Mangler(M);
182
Chris Lattnerfd445002005-07-16 08:01:13 +0000183 // Local alias to shortenify coming code.
184 std::vector<unsigned char> &FH = FileHeader;
185
186 outbyte(FH, 0x7F); // EI_MAG0
187 outbyte(FH, 'E'); // EI_MAG1
188 outbyte(FH, 'L'); // EI_MAG2
189 outbyte(FH, 'F'); // EI_MAG3
190 outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS
191 outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA
192 outbyte(FH, 1); // EI_VERSION
193 FH.resize(16); // EI_PAD up to 16 bytes.
Chris Lattner386b1512005-06-27 06:29:00 +0000194
195 // This should change for shared objects.
Chris Lattnerfd445002005-07-16 08:01:13 +0000196 outhalf(FH, 1); // e_type = ET_REL
197 outhalf(FH, e_machine); // e_machine = whatever the target wants
198 outword(FH, 1); // e_version = 1
199 outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file
200 outaddr(FH, 0); // e_phoff = 0 -> no program header for .o
Chris Lattner386b1512005-06-27 06:29:00 +0000201
Chris Lattnerfd445002005-07-16 08:01:13 +0000202 ELFHeader_e_shoff_Offset = FH.size();
203 outaddr(FH, 0); // e_shoff
204 outword(FH, e_flags); // e_flags = whatever the target wants
Chris Lattner386b1512005-06-27 06:29:00 +0000205
Chris Lattnerfd445002005-07-16 08:01:13 +0000206 outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
207 outhalf(FH, 0); // e_phentsize = prog header entry size
208 outhalf(FH, 0); // e_phnum = # prog header entries = 0
209 outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner386b1512005-06-27 06:29:00 +0000210
211
Chris Lattnerfd445002005-07-16 08:01:13 +0000212 ELFHeader_e_shnum_Offset = FH.size();
213 outhalf(FH, 0); // e_shnum = # of section header ents
214 ELFHeader_e_shstrndx_Offset = FH.size();
215 outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner386b1512005-06-27 06:29:00 +0000216
Chris Lattnerfd445002005-07-16 08:01:13 +0000217 // Add the null section, which is required to be first in the file.
218 getSection("");
Chris Lattner386b1512005-06-27 06:29:00 +0000219
Chris Lattner1932f5c2005-07-07 07:02:20 +0000220 // Start up the symbol table. The first entry in the symtab is the null
221 // entry.
222 SymbolTable.push_back(ELFSym(0));
Chris Lattner386b1512005-06-27 06:29:00 +0000223
Chris Lattner386b1512005-06-27 06:29:00 +0000224 return false;
225}
226
Chris Lattner1932f5c2005-07-07 07:02:20 +0000227void ELFWriter::EmitGlobal(GlobalVariable *GV, ELFSection &DataSection,
228 ELFSection &BSSSection) {
Chris Lattner748de6e2005-07-08 05:47:00 +0000229 // If this is an external global, emit it now. TODO: Note that it would be
230 // better to ignore the symbol here and only add it to the symbol table if
231 // referenced.
232 if (!GV->hasInitializer()) {
233 ELFSym ExternalSym(GV);
234 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
235 ExternalSym.SetType(ELFSym::STT_NOTYPE);
236 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
237 SymbolTable.push_back(ExternalSym);
238 return;
239 }
Chris Lattner1932f5c2005-07-07 07:02:20 +0000240
Chris Lattner748de6e2005-07-08 05:47:00 +0000241 const Type *GVType = (const Type*)GV->getType();
242 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
243 unsigned Size = TM.getTargetData().getTypeSize(GVType);
244
Chris Lattner1932f5c2005-07-07 07:02:20 +0000245 // If this global has a zero initializer, it is part of the .bss or common
246 // section.
247 if (GV->getInitializer()->isNullValue()) {
248 // If this global is part of the common block, add it now. Variables are
249 // part of the common block if they are zero initialized and allowed to be
250 // merged with other symbols.
251 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
252 ELFSym CommonSym(GV);
253 // Value for common symbols is the alignment required.
Chris Lattner748de6e2005-07-08 05:47:00 +0000254 CommonSym.Value = Align;
255 CommonSym.Size = Size;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000256 CommonSym.SetBind(ELFSym::STB_GLOBAL);
257 CommonSym.SetType(ELFSym::STT_OBJECT);
258 // TODO SOMEDAY: add ELF visibility.
259 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
260 SymbolTable.push_back(CommonSym);
261 return;
262 }
Chris Lattner386b1512005-06-27 06:29:00 +0000263
Chris Lattner748de6e2005-07-08 05:47:00 +0000264 // Otherwise, this symbol is part of the .bss section. Emit it now.
265
266 // Handle alignment. Ensure section is aligned at least as much as required
267 // by this symbol.
268 BSSSection.Align = std::max(BSSSection.Align, Align);
269
270 // Within the section, emit enough virtual padding to get us to an alignment
271 // boundary.
272 if (Align)
273 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
274
275 ELFSym BSSSym(GV);
276 BSSSym.Value = BSSSection.Size;
277 BSSSym.Size = Size;
278 BSSSym.SetType(ELFSym::STT_OBJECT);
279
280 switch (GV->getLinkage()) {
281 default: // weak/linkonce handled above
282 assert(0 && "Unexpected linkage type!");
283 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
284 case GlobalValue::ExternalLinkage:
285 BSSSym.SetBind(ELFSym::STB_GLOBAL);
286 break;
287 case GlobalValue::InternalLinkage:
288 BSSSym.SetBind(ELFSym::STB_LOCAL);
289 break;
290 }
291
292 // Set the idx of the .bss section
Chris Lattnerfd445002005-07-16 08:01:13 +0000293 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner748de6e2005-07-08 05:47:00 +0000294 SymbolTable.push_back(BSSSym);
295
296 // Reserve space in the .bss section for this symbol.
297 BSSSection.Size += Size;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000298 return;
299 }
Chris Lattner386b1512005-06-27 06:29:00 +0000300
Chris Lattner1932f5c2005-07-07 07:02:20 +0000301 // FIXME: handle .rodata
302 //assert(!GV->isConstant() && "unimp");
Chris Lattner386b1512005-06-27 06:29:00 +0000303
Chris Lattner1932f5c2005-07-07 07:02:20 +0000304 // FIXME: handle .data
305 //assert(0 && "unimp");
Chris Lattner386b1512005-06-27 06:29:00 +0000306}
307
308
309bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner2244f732005-07-11 05:17:18 +0000310 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner386b1512005-06-27 06:29:00 +0000311 return false;
312}
313
314/// doFinalization - Now that the module has been completely processed, emit
315/// the ELF file to 'O'.
316bool ELFWriter::doFinalization(Module &M) {
Chris Lattner1932f5c2005-07-07 07:02:20 +0000317 // Okay, the ELF header and .text sections have been completed, build the
318 // .data, .bss, and "common" sections next.
Chris Lattnerfd445002005-07-16 08:01:13 +0000319 ELFSection &DataSection = getSection(".data");
Chris Lattner748de6e2005-07-08 05:47:00 +0000320 DataSection.Type = ELFSection::SHT_PROGBITS;
321 DataSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000322
Chris Lattnerfd445002005-07-16 08:01:13 +0000323 ELFSection &BSSSection = getSection(".bss");
Chris Lattner748de6e2005-07-08 05:47:00 +0000324 BSSSection.Type = ELFSection::SHT_NOBITS;
325 BSSSection.Flags = ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC;
Chris Lattnerfd445002005-07-16 08:01:13 +0000326
327 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
328 I != E; ++I)
329 EmitGlobal(I, DataSection, BSSSection);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000330
331 // Emit the symbol table now, if non-empty.
332 EmitSymbolTable();
333
334 // FIXME: Emit the relocations now.
335
Chris Lattner386b1512005-06-27 06:29:00 +0000336 // Emit the string table for the sections in the ELF file we have.
337 EmitSectionTableStringTable();
338
Chris Lattnerfd445002005-07-16 08:01:13 +0000339 // Emit the sections to the .o file, and emit the section table for the file.
340 OutputSectionsAndSectionTable();
Chris Lattner386b1512005-06-27 06:29:00 +0000341
Chris Lattnerfd445002005-07-16 08:01:13 +0000342 // We are done with the abstract symbols.
343 SectionList.clear();
344 NumSections = 0;
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000345
346 // Release the name mangler object.
347 delete Mang; Mang = 0;
Chris Lattner386b1512005-06-27 06:29:00 +0000348 return false;
349}
350
Chris Lattner1932f5c2005-07-07 07:02:20 +0000351/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
352/// table for it and then the symbol table itself.
353void ELFWriter::EmitSymbolTable() {
354 if (SymbolTable.size() == 1) return; // Only the null entry.
355
356 // FIXME: compact all local symbols to the start of the symtab.
357 unsigned FirstNonLocalSymbol = 1;
358
Chris Lattnerfd445002005-07-16 08:01:13 +0000359 ELFSection &StrTab = getSection(".strtab");
Chris Lattner1932f5c2005-07-07 07:02:20 +0000360 StrTab.Type = ELFSection::SHT_STRTAB;
361 StrTab.Align = 1;
362
Chris Lattnerfd445002005-07-16 08:01:13 +0000363 DataBuffer &StrTabBuf = StrTab.SectionData;
364
Chris Lattner1932f5c2005-07-07 07:02:20 +0000365 // Set the zero'th symbol to a null byte, as required.
Chris Lattnerfd445002005-07-16 08:01:13 +0000366 outbyte(StrTabBuf, 0);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000367 SymbolTable[0].NameIdx = 0;
368 unsigned Index = 1;
369 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattnerdfe33bc2005-07-11 03:11:47 +0000370 // Use the name mangler to uniquify the LLVM symbol.
371 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000372
373 if (Name.empty()) {
374 SymbolTable[i].NameIdx = 0;
375 } else {
376 SymbolTable[i].NameIdx = Index;
377
378 // Add the name to the output buffer, including the null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000379 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner1932f5c2005-07-07 07:02:20 +0000380
381 // Add a null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000382 StrTabBuf.push_back(0);
Chris Lattner1932f5c2005-07-07 07:02:20 +0000383
384 // Keep track of the number of bytes emitted to this section.
385 Index += Name.size()+1;
386 }
387 }
Chris Lattnerfd445002005-07-16 08:01:13 +0000388 assert(Index == StrTabBuf.size());
389 StrTab.Size = Index;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000390
391 // Now that we have emitted the string table and know the offset into the
392 // string table of each symbol, emit the symbol table itself.
Chris Lattnerfd445002005-07-16 08:01:13 +0000393 ELFSection &SymTab = getSection(".symtab");
Chris Lattner1932f5c2005-07-07 07:02:20 +0000394 SymTab.Type = ELFSection::SHT_SYMTAB;
Chris Lattner298ac692005-07-12 06:57:52 +0000395 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattnerfd445002005-07-16 08:01:13 +0000396 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner1932f5c2005-07-07 07:02:20 +0000397 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
398 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattnerfd445002005-07-16 08:01:13 +0000399 DataBuffer &SymTabBuf = SymTab.SectionData;
Chris Lattner1932f5c2005-07-07 07:02:20 +0000400
Chris Lattner298ac692005-07-12 06:57:52 +0000401 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
402 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
403 ELFSym &Sym = SymbolTable[i];
Chris Lattnerfd445002005-07-16 08:01:13 +0000404 outword(SymTabBuf, Sym.NameIdx);
405 outaddr32(SymTabBuf, Sym.Value);
406 outword(SymTabBuf, Sym.Size);
407 outbyte(SymTabBuf, Sym.Info);
408 outbyte(SymTabBuf, Sym.Other);
409 outhalf(SymTabBuf, Sym.SectionIdx);
Chris Lattner298ac692005-07-12 06:57:52 +0000410 }
411 } else {
412 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
413 ELFSym &Sym = SymbolTable[i];
Chris Lattnerfd445002005-07-16 08:01:13 +0000414 outword(SymTabBuf, Sym.NameIdx);
415 outbyte(SymTabBuf, Sym.Info);
416 outbyte(SymTabBuf, Sym.Other);
417 outhalf(SymTabBuf, Sym.SectionIdx);
418 outaddr64(SymTabBuf, Sym.Value);
419 outxword(SymTabBuf, Sym.Size);
Chris Lattner298ac692005-07-12 06:57:52 +0000420 }
Chris Lattner1932f5c2005-07-07 07:02:20 +0000421 }
422
Chris Lattnerfd445002005-07-16 08:01:13 +0000423 SymTab.Size = SymTabBuf.size();
Chris Lattner1932f5c2005-07-07 07:02:20 +0000424}
425
Chris Lattner386b1512005-06-27 06:29:00 +0000426/// EmitSectionTableStringTable - This method adds and emits a section for the
427/// ELF Section Table string table: the string table that holds all of the
428/// section names.
429void ELFWriter::EmitSectionTableStringTable() {
430 // First step: add the section for the string table to the list of sections:
Chris Lattnerfd445002005-07-16 08:01:13 +0000431 ELFSection &SHStrTab = getSection(".shstrtab");
432 SHStrTab.Type = ELFSection::SHT_STRTAB;
Chris Lattner386b1512005-06-27 06:29:00 +0000433
434 // Now that we know which section number is the .shstrtab section, update the
435 // e_shstrndx entry in the ELF header.
Chris Lattnerfd445002005-07-16 08:01:13 +0000436 fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner386b1512005-06-27 06:29:00 +0000437
438 // Set the NameIdx of each section in the string table and emit the bytes for
439 // the string table.
440 unsigned Index = 0;
Chris Lattnerfd445002005-07-16 08:01:13 +0000441 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner386b1512005-06-27 06:29:00 +0000442
Chris Lattnerfd445002005-07-16 08:01:13 +0000443 for (std::list<ELFSection>::iterator I = SectionList.begin(),
444 E = SectionList.end(); I != E; ++I) {
Chris Lattner386b1512005-06-27 06:29:00 +0000445 // Set the index into the table. Note if we have lots of entries with
446 // common suffixes, we could memoize them here if we cared.
Chris Lattnerfd445002005-07-16 08:01:13 +0000447 I->NameIdx = Index;
Chris Lattner386b1512005-06-27 06:29:00 +0000448
449 // Add the name to the output buffer, including the null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000450 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
451
Chris Lattner386b1512005-06-27 06:29:00 +0000452 // Add a null terminator.
Chris Lattnerfd445002005-07-16 08:01:13 +0000453 Buf.push_back(0);
Chris Lattner386b1512005-06-27 06:29:00 +0000454
455 // Keep track of the number of bytes emitted to this section.
Chris Lattnerfd445002005-07-16 08:01:13 +0000456 Index += I->Name.size()+1;
Chris Lattner386b1512005-06-27 06:29:00 +0000457 }
458
459 // Set the size of .shstrtab now that we know what it is.
Chris Lattnerfd445002005-07-16 08:01:13 +0000460 assert(Index == Buf.size());
461 SHStrTab.Size = Index;
Chris Lattner386b1512005-06-27 06:29:00 +0000462}
463
Chris Lattnerfd445002005-07-16 08:01:13 +0000464/// OutputSectionsAndSectionTable - Now that we have constructed the file header
465/// and all of the sections, emit these to the ostream destination and emit the
466/// SectionTable.
467void ELFWriter::OutputSectionsAndSectionTable() {
468 // Pass #1: Compute the file offset for each section.
469 size_t FileOff = FileHeader.size(); // File header first.
470
471 // Emit all of the section data in order.
472 for (std::list<ELFSection>::iterator I = SectionList.begin(),
473 E = SectionList.end(); I != E; ++I) {
474 // Align FileOff to whatever the alignment restrictions of the section are.
475 if (I->Align)
476 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
477 I->Offset = FileOff;
478 FileOff += I->SectionData.size();
Chris Lattner386b1512005-06-27 06:29:00 +0000479 }
480
Chris Lattnerfd445002005-07-16 08:01:13 +0000481 // Align Section Header.
482 unsigned TableAlign = is64Bit ? 8 : 4;
483 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
484
485 // Now that we know where all of the sections will be emitted, set the e_shnum
486 // entry in the ELF header.
487 fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset);
488
489 // Now that we know the offset in the file of the section table, update the
490 // e_shoff address in the ELF header.
491 fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset);
492
493 // Now that we know all of the data in the file header, emit it and all of the
494 // sections!
495 O.write((char*)&FileHeader[0], FileHeader.size());
496 FileOff = FileHeader.size();
497 DataBuffer().swap(FileHeader);
498
499 DataBuffer Table;
500
501 // Emit all of the section data and build the section table itself.
502 while (!SectionList.empty()) {
503 const ELFSection &S = *SectionList.begin();
504
505 // Align FileOff to whatever the alignment restrictions of the section are.
506 if (S.Align)
507 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
508 FileOff != NewFileOff; ++FileOff)
509 O.put(0xAB);
510 O.write((char*)&S.SectionData[0], S.SectionData.size());
511 FileOff += S.SectionData.size();
512
513 outword(Table, S.NameIdx); // sh_name - Symbol table name idx
514 outword(Table, S.Type); // sh_type - Section contents & semantics
515 outword(Table, S.Flags); // sh_flags - Section flags.
516 outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in.
517 outaddr(Table, S.Offset); // sh_offset - Offset from the file start.
518 outword(Table, S.Size); // sh_size - The section size.
519 outword(Table, S.Link); // sh_link - Section header table index link.
520 outword(Table, S.Info); // sh_info - Auxillary information.
521 outword(Table, S.Align); // sh_addralign - Alignment of section.
522 outword(Table, S.EntSize); // sh_entsize - Size of entries in the section.
523
524 SectionList.pop_front();
525 }
526
527 // Align output for the section table.
528 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
529 FileOff != NewFileOff; ++FileOff)
530 O.put(0xAB);
531
532 // Emit the section table itself.
533 O.write((char*)&Table[0], Table.size());
Chris Lattner386b1512005-06-27 06:29:00 +0000534}