blob: 4f041c27375866b7f43b9f65962da84db9cc3f89 [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"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.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"
Duraid Madina2e096c12005-12-28 06:29:02 +000041#include <iostream>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000042using namespace llvm;
43
Chris Lattner0e180502005-07-11 06:34:30 +000044//===----------------------------------------------------------------------===//
45// ELFCodeEmitter Implementation
46//===----------------------------------------------------------------------===//
47
Chris Lattneraa507db2005-07-11 05:17:18 +000048namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000049 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
50 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000051 class ELFCodeEmitter : public MachineCodeEmitter {
52 ELFWriter &EW;
Chris Lattner5f48ff72005-07-16 08:01:13 +000053 ELFWriter::ELFSection *ES; // Section to write to.
54 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000055 size_t FnStart;
56 public:
Chris Lattner5f48ff72005-07-16 08:01:13 +000057 ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000058
Chris Lattner0e180502005-07-11 06:34:30 +000059 void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +000060 bool finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000061
Chris Lattneraa507db2005-07-11 05:17:18 +000062 void addRelocation(const MachineRelocation &MR) {
63 assert(0 && "relo not handled yet!");
64 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000065
66 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
67 }
68
69 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
Chris Lattneraa507db2005-07-11 05:17:18 +000070 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000071 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000072 }
Chris Lattnerb4432f32006-05-03 17:10:41 +000073 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman37efe672006-04-22 18:53:45 +000074 assert(0 && "JT not implementated yet!");
75 return 0;
76 }
Chris Lattnerf75f9be2006-05-02 23:22:24 +000077
Chris Lattnerb4432f32006-05-03 17:10:41 +000078 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
79 assert(0 && "JT not implementated yet!");
80 return 0;
81 }
Andrew Lenharthfe660392005-07-28 18:13:59 +000082
Chris Lattner0e180502005-07-11 06:34:30 +000083 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Chris Lattneraa507db2005-07-11 05:17:18 +000084 void startFunctionStub(unsigned StubSize) {
85 assert(0 && "JIT specific function called!");
86 abort();
87 }
88 void *finishFunctionStub(const Function *F) {
89 assert(0 && "JIT specific function called!");
90 abort();
91 return 0;
92 }
93 };
94}
95
Chris Lattner0e180502005-07-11 06:34:30 +000096/// startFunction - This callback is invoked when a new machine function is
97/// about to be emitted.
98void ELFCodeEmitter::startFunction(MachineFunction &F) {
99 // Align the output buffer to the appropriate alignment.
100 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +0000101 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +0000102 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
103 ELFWriter::ELFSection::SHF_EXECINSTR |
104 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000105 OutBuffer = &ES->SectionData;
Chris Lattner43b429b2006-05-02 18:27:26 +0000106 std::cerr << "FIXME: This code needs to be updated for changes in the"
107 << " CodeEmitter interfaces. In particular, this should set "
108 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
109 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000110
Chris Lattner0e180502005-07-11 06:34:30 +0000111 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000112 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000113
Chris Lattner0e180502005-07-11 06:34:30 +0000114 // Add padding zeros to the end of the buffer to make sure that the
115 // function will start on the correct byte alignment within the section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000116 size_t SectionOff = OutBuffer->size();
117 ELFWriter::align(*OutBuffer, Align);
Jeff Cohen00b168892005-07-27 06:12:32 +0000118
Chris Lattner5f48ff72005-07-16 08:01:13 +0000119 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000120}
121
122/// finishFunction - This callback is invoked after the function is completely
123/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000124bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000125 // We now know the size of the function, add a symbol to represent it.
126 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000127
Chris Lattner0e180502005-07-11 06:34:30 +0000128 // 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 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000144
145 ES->Size = OutBuffer->size();
146
Chris Lattner0e180502005-07-11 06:34:30 +0000147 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000148 FnSym.SectionIdx = ES->SectionIdx;
149 FnSym.Value = FnStart; // Value = Offset from start of Section.
150 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000151
Chris Lattner0e180502005-07-11 06:34:30 +0000152 // Finally, add it to the symtab.
153 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000154 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000155}
156
157//===----------------------------------------------------------------------===//
158// ELFWriter Implementation
159//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000160
Chris Lattner35f0a4f2005-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
Owen Andersona69571c2006-05-03 01:29:57 +0000165 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
166 isLittleEndian = TM.getTargetData()->isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000167
168 // Create the machine code emitter object for this target.
169 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000170 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000171}
172
173ELFWriter::~ELFWriter() {
174 delete MCE;
Chris Lattner35f0a4f2005-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 Lattner5acd1202005-07-11 03:11:47 +0000180 Mang = new Mangler(M);
181
Chris Lattner5f48ff72005-07-16 08:01:13 +0000182 // Local alias to shortenify coming code.
183 std::vector<unsigned char> &FH = FileHeader;
Jeff Cohen00b168892005-07-27 06:12:32 +0000184
Chris Lattner5f48ff72005-07-16 08:01:13 +0000185 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.
Jeff Cohen00b168892005-07-27 06:12:32 +0000193
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000194 // This should change for shared objects.
Chris Lattner5f48ff72005-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 Lattner35f0a4f2005-06-27 06:29:00 +0000200
Chris Lattner5f48ff72005-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 Lattner35f0a4f2005-06-27 06:29:00 +0000204
Chris Lattner5f48ff72005-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 Lattner35f0a4f2005-06-27 06:29:00 +0000209
Jeff Cohen00b168892005-07-27 06:12:32 +0000210
Chris Lattner5f48ff72005-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 Lattner35f0a4f2005-06-27 06:29:00 +0000215
Chris Lattner5f48ff72005-07-16 08:01:13 +0000216 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000217 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000218
Chris Lattner80ed8fa2005-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 Lattner35f0a4f2005-06-27 06:29:00 +0000222
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000223 return false;
224}
225
Chris Lattner05895232005-07-16 17:41:06 +0000226void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000227 // If this is an external global, emit it now. TODO: Note that it would be
228 // better to ignore the symbol here and only add it to the symbol table if
229 // referenced.
230 if (!GV->hasInitializer()) {
231 ELFSym ExternalSym(GV);
232 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
233 ExternalSym.SetType(ELFSym::STT_NOTYPE);
234 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
235 SymbolTable.push_back(ExternalSym);
236 return;
237 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000238
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000239 const Type *GVType = (const Type*)GV->getType();
Owen Andersona69571c2006-05-03 01:29:57 +0000240 unsigned Align = TM.getTargetData()->getTypeAlignment(GVType);
241 unsigned Size = TM.getTargetData()->getTypeSize(GVType);
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000242
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000243 // If this global has a zero initializer, it is part of the .bss or common
244 // section.
245 if (GV->getInitializer()->isNullValue()) {
246 // If this global is part of the common block, add it now. Variables are
247 // part of the common block if they are zero initialized and allowed to be
248 // merged with other symbols.
249 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
250 ELFSym CommonSym(GV);
251 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000252 CommonSym.Value = Align;
253 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000254 CommonSym.SetBind(ELFSym::STB_GLOBAL);
255 CommonSym.SetType(ELFSym::STT_OBJECT);
256 // TODO SOMEDAY: add ELF visibility.
257 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
258 SymbolTable.push_back(CommonSym);
259 return;
260 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000261
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000262 // Otherwise, this symbol is part of the .bss section. Emit it now.
263
264 // Handle alignment. Ensure section is aligned at least as much as required
265 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000266 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000267 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 Lattner5f48ff72005-07-16 08:01:13 +0000292 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-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 Lattner80ed8fa2005-07-07 07:02:20 +0000297 return;
298 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000299
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000300 // FIXME: handle .rodata
301 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000302
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000303 // FIXME: handle .data
304 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000305}
306
307
308bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000309 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-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 Lattner80ed8fa2005-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 Lattner5f48ff72005-07-16 08:01:13 +0000318 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
319 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000320 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000321
322 // Emit the symbol table now, if non-empty.
323 EmitSymbolTable();
324
325 // FIXME: Emit the relocations now.
326
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000327 // Emit the string table for the sections in the ELF file we have.
328 EmitSectionTableStringTable();
329
Chris Lattner5f48ff72005-07-16 08:01:13 +0000330 // Emit the sections to the .o file, and emit the section table for the file.
331 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000332
Chris Lattner5f48ff72005-07-16 08:01:13 +0000333 // We are done with the abstract symbols.
334 SectionList.clear();
335 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000336
337 // Release the name mangler object.
338 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000339 return false;
340}
341
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000342/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
343/// table for it and then the symbol table itself.
344void ELFWriter::EmitSymbolTable() {
345 if (SymbolTable.size() == 1) return; // Only the null entry.
346
347 // FIXME: compact all local symbols to the start of the symtab.
348 unsigned FirstNonLocalSymbol = 1;
349
Chris Lattner00033952005-07-16 17:36:04 +0000350 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000351 StrTab.Align = 1;
352
Chris Lattner5f48ff72005-07-16 08:01:13 +0000353 DataBuffer &StrTabBuf = StrTab.SectionData;
354
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000355 // Set the zero'th symbol to a null byte, as required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000356 outbyte(StrTabBuf, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000357 SymbolTable[0].NameIdx = 0;
358 unsigned Index = 1;
359 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000360 // Use the name mangler to uniquify the LLVM symbol.
361 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000362
363 if (Name.empty()) {
364 SymbolTable[i].NameIdx = 0;
365 } else {
366 SymbolTable[i].NameIdx = Index;
367
368 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000369 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000370
371 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000372 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000373
374 // Keep track of the number of bytes emitted to this section.
375 Index += Name.size()+1;
376 }
377 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000378 assert(Index == StrTabBuf.size());
379 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000380
381 // Now that we have emitted the string table and know the offset into the
382 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000383 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000384 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000385 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000386 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
387 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000388 DataBuffer &SymTabBuf = SymTab.SectionData;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000389
Chris Lattner46c53052005-07-12 06:57:52 +0000390 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
391 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
392 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000393 outword(SymTabBuf, Sym.NameIdx);
394 outaddr32(SymTabBuf, Sym.Value);
395 outword(SymTabBuf, Sym.Size);
396 outbyte(SymTabBuf, Sym.Info);
397 outbyte(SymTabBuf, Sym.Other);
398 outhalf(SymTabBuf, Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000399 }
400 } else {
401 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
402 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000403 outword(SymTabBuf, Sym.NameIdx);
404 outbyte(SymTabBuf, Sym.Info);
405 outbyte(SymTabBuf, Sym.Other);
406 outhalf(SymTabBuf, Sym.SectionIdx);
407 outaddr64(SymTabBuf, Sym.Value);
408 outxword(SymTabBuf, Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000409 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000410 }
411
Chris Lattner5f48ff72005-07-16 08:01:13 +0000412 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000413}
414
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000415/// EmitSectionTableStringTable - This method adds and emits a section for the
416/// ELF Section Table string table: the string table that holds all of the
417/// section names.
418void ELFWriter::EmitSectionTableStringTable() {
419 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000420 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000421
422 // Now that we know which section number is the .shstrtab section, update the
423 // e_shstrndx entry in the ELF header.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000424 fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000425
426 // Set the NameIdx of each section in the string table and emit the bytes for
427 // the string table.
428 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000429 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000430
Chris Lattner5f48ff72005-07-16 08:01:13 +0000431 for (std::list<ELFSection>::iterator I = SectionList.begin(),
432 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000433 // Set the index into the table. Note if we have lots of entries with
434 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000435 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000436
437 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000438 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
439
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000440 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000441 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000442
443 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000444 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000445 }
446
447 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000448 assert(Index == Buf.size());
449 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000450}
451
Chris Lattner5f48ff72005-07-16 08:01:13 +0000452/// OutputSectionsAndSectionTable - Now that we have constructed the file header
453/// and all of the sections, emit these to the ostream destination and emit the
454/// SectionTable.
455void ELFWriter::OutputSectionsAndSectionTable() {
456 // Pass #1: Compute the file offset for each section.
457 size_t FileOff = FileHeader.size(); // File header first.
458
459 // Emit all of the section data in order.
460 for (std::list<ELFSection>::iterator I = SectionList.begin(),
461 E = SectionList.end(); I != E; ++I) {
462 // Align FileOff to whatever the alignment restrictions of the section are.
463 if (I->Align)
464 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
465 I->Offset = FileOff;
466 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000467 }
468
Chris Lattner5f48ff72005-07-16 08:01:13 +0000469 // Align Section Header.
470 unsigned TableAlign = is64Bit ? 8 : 4;
471 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
472
473 // Now that we know where all of the sections will be emitted, set the e_shnum
474 // entry in the ELF header.
475 fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000476
Chris Lattner5f48ff72005-07-16 08:01:13 +0000477 // Now that we know the offset in the file of the section table, update the
478 // e_shoff address in the ELF header.
479 fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000480
Chris Lattner5f48ff72005-07-16 08:01:13 +0000481 // Now that we know all of the data in the file header, emit it and all of the
482 // sections!
483 O.write((char*)&FileHeader[0], FileHeader.size());
484 FileOff = FileHeader.size();
485 DataBuffer().swap(FileHeader);
486
487 DataBuffer Table;
488
489 // Emit all of the section data and build the section table itself.
490 while (!SectionList.empty()) {
491 const ELFSection &S = *SectionList.begin();
492
493 // Align FileOff to whatever the alignment restrictions of the section are.
494 if (S.Align)
495 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
496 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000497 O.put((char)0xAB);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000498 O.write((char*)&S.SectionData[0], S.SectionData.size());
499 FileOff += S.SectionData.size();
500
501 outword(Table, S.NameIdx); // sh_name - Symbol table name idx
502 outword(Table, S.Type); // sh_type - Section contents & semantics
503 outword(Table, S.Flags); // sh_flags - Section flags.
504 outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in.
505 outaddr(Table, S.Offset); // sh_offset - Offset from the file start.
506 outword(Table, S.Size); // sh_size - The section size.
507 outword(Table, S.Link); // sh_link - Section header table index link.
508 outword(Table, S.Info); // sh_info - Auxillary information.
509 outword(Table, S.Align); // sh_addralign - Alignment of section.
510 outword(Table, S.EntSize); // sh_entsize - Size of entries in the section.
511
512 SectionList.pop_front();
513 }
514
515 // Align output for the section table.
516 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
517 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000518 O.put((char)0xAB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000519
Chris Lattner5f48ff72005-07-16 08:01:13 +0000520 // Emit the section table itself.
521 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000522}