blob: 780ba543cdc3122a9fe75ca76842e26447415303 [file] [log] [blame]
Chris Lattner35f0a4f2005-06-27 06:29:00 +00001//===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner00033952005-07-16 17:36:04 +00005// This file was developed by Chris Lattner and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
Chris Lattner35f0a4f2005-06-27 06:29:00 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the target-independent ELF writer. This file writes out
11// the ELF file in the following order:
12//
13// #1. ELF Header
Chris Lattner80ed8fa2005-07-07 07:02:20 +000014// #2. '.text' section
15// #3. '.data' section
16// #4. '.bss' section (conceptual position in file)
Chris Lattner35f0a4f2005-06-27 06:29:00 +000017// ...
18// #X. '.shstrtab' section
19// #Y. Section Table
20//
21// The entries in the section table are laid out as:
22// #0. Null entry [required]
Chris Lattner80ed8fa2005-07-07 07:02:20 +000023// #1. ".text" entry - the program code
24// #2. ".data" entry - global variables with initializers. [ if needed ]
25// #3. ".bss" entry - global variables without initializers. [ if needed ]
Chris Lattner35f0a4f2005-06-27 06:29:00 +000026// ...
27// #N. ".shstrtab" entry - String table for the section names.
Chris Lattner35f0a4f2005-06-27 06:29:00 +000028//
29// NOTE: This code should eventually be extended to support 64-bit ELF (this
30// won't be hard), but we haven't done so yet!
31//
32//===----------------------------------------------------------------------===//
33
34#include "llvm/CodeGen/ELFWriter.h"
35#include "llvm/Module.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000036#include "llvm/CodeGen/MachineCodeEmitter.h"
37#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000038#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000039#include "llvm/Support/Mangler.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000040#include <iostream>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000041using namespace llvm;
42
Chris Lattner0e180502005-07-11 06:34:30 +000043//===----------------------------------------------------------------------===//
44// ELFCodeEmitter Implementation
45//===----------------------------------------------------------------------===//
46
Chris Lattneraa507db2005-07-11 05:17:18 +000047namespace llvm {
Chris Lattner0e180502005-07-11 06:34:30 +000048 /// ELFCodeEmitter - This class is used by the ELFWriter to emit the code for
49 /// functions to the ELF file.
Chris Lattneraa507db2005-07-11 05:17:18 +000050 class ELFCodeEmitter : public MachineCodeEmitter {
51 ELFWriter &EW;
Chris Lattner5f48ff72005-07-16 08:01:13 +000052 ELFWriter::ELFSection *ES; // Section to write to.
53 std::vector<unsigned char> *OutBuffer;
Chris Lattneraa507db2005-07-11 05:17:18 +000054 size_t FnStart;
55 public:
Chris Lattner5f48ff72005-07-16 08:01:13 +000056 ELFCodeEmitter(ELFWriter &ew) : EW(ew), OutBuffer(0) {}
Chris Lattneraa507db2005-07-11 05:17:18 +000057
Chris Lattner0e180502005-07-11 06:34:30 +000058 void startFunction(MachineFunction &F);
Chris Lattner43b429b2006-05-02 18:27:26 +000059 bool finishFunction(MachineFunction &F);
Chris Lattner5fe7b6e2005-07-11 06:17:35 +000060
Chris Lattneraa507db2005-07-11 05:17:18 +000061 void addRelocation(const MachineRelocation &MR) {
62 assert(0 && "relo not handled yet!");
63 }
64 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) {
65 assert(0 && "CP not implementated yet!");
Jeff Cohen44213c92005-07-12 02:53:33 +000066 return 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000067 }
Nate Begeman37efe672006-04-22 18:53:45 +000068 virtual uint64_t getJumpTableEntryAddress(unsigned Index) {
69 assert(0 && "JT not implementated yet!");
70 return 0;
71 }
Chris Lattnerf75f9be2006-05-02 23:22:24 +000072
73 virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
Chris Lattneraf1563f2006-05-03 00:32:55 +000074 std::vector<uint64_t> &MBBM) {
Chris Lattnerf75f9be2006-05-02 23:22:24 +000075 assert(0 && "JT not implementated yet!");
76 }
77
Andrew Lenharthfe660392005-07-28 18:13:59 +000078
Chris Lattner0e180502005-07-11 06:34:30 +000079 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Chris Lattneraa507db2005-07-11 05:17:18 +000080 void startFunctionStub(unsigned StubSize) {
81 assert(0 && "JIT specific function called!");
82 abort();
83 }
84 void *finishFunctionStub(const Function *F) {
85 assert(0 && "JIT specific function called!");
86 abort();
87 return 0;
88 }
89 };
90}
91
Chris Lattner0e180502005-07-11 06:34:30 +000092/// startFunction - This callback is invoked when a new machine function is
93/// about to be emitted.
94void ELFCodeEmitter::startFunction(MachineFunction &F) {
95 // Align the output buffer to the appropriate alignment.
96 unsigned Align = 16; // FIXME: GENERICIZE!!
Chris Lattner5f48ff72005-07-16 08:01:13 +000097 // Get the ELF Section that this function belongs in.
Chris Lattner00033952005-07-16 17:36:04 +000098 ES = &EW.getSection(".text", ELFWriter::ELFSection::SHT_PROGBITS,
99 ELFWriter::ELFSection::SHF_EXECINSTR |
100 ELFWriter::ELFSection::SHF_ALLOC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000101 OutBuffer = &ES->SectionData;
Chris Lattner43b429b2006-05-02 18:27:26 +0000102 std::cerr << "FIXME: This code needs to be updated for changes in the"
103 << " CodeEmitter interfaces. In particular, this should set "
104 << "BufferBegin/BufferEnd/CurBufferPtr, not deal with OutBuffer!";
105 abort();
Jeff Cohen00b168892005-07-27 06:12:32 +0000106
Chris Lattner0e180502005-07-11 06:34:30 +0000107 // Upgrade the section alignment if required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000108 if (ES->Align < Align) ES->Align = Align;
Jeff Cohen00b168892005-07-27 06:12:32 +0000109
Chris Lattner0e180502005-07-11 06:34:30 +0000110 // 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.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000112 size_t SectionOff = OutBuffer->size();
113 ELFWriter::align(*OutBuffer, Align);
Jeff Cohen00b168892005-07-27 06:12:32 +0000114
Chris Lattner5f48ff72005-07-16 08:01:13 +0000115 FnStart = OutBuffer->size();
Chris Lattner0e180502005-07-11 06:34:30 +0000116}
117
118/// finishFunction - This callback is invoked after the function is completely
119/// finished.
Chris Lattner43b429b2006-05-02 18:27:26 +0000120bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
Chris Lattner0e180502005-07-11 06:34:30 +0000121 // We now know the size of the function, add a symbol to represent it.
122 ELFWriter::ELFSym FnSym(F.getFunction());
Jeff Cohen00b168892005-07-27 06:12:32 +0000123
Chris Lattner0e180502005-07-11 06:34:30 +0000124 // Figure out the binding (linkage) of the symbol.
125 switch (F.getFunction()->getLinkage()) {
126 default:
127 // appending linkage is illegal for functions.
128 assert(0 && "Unknown linkage type!");
129 case GlobalValue::ExternalLinkage:
130 FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL);
131 break;
132 case GlobalValue::LinkOnceLinkage:
133 case GlobalValue::WeakLinkage:
134 FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
135 break;
136 case GlobalValue::InternalLinkage:
137 FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
138 break;
139 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000140
141 ES->Size = OutBuffer->size();
142
Chris Lattner0e180502005-07-11 06:34:30 +0000143 FnSym.SetType(ELFWriter::ELFSym::STT_FUNC);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000144 FnSym.SectionIdx = ES->SectionIdx;
145 FnSym.Value = FnStart; // Value = Offset from start of Section.
146 FnSym.Size = OutBuffer->size()-FnStart;
Jeff Cohen00b168892005-07-27 06:12:32 +0000147
Chris Lattner0e180502005-07-11 06:34:30 +0000148 // Finally, add it to the symtab.
149 EW.SymbolTable.push_back(FnSym);
Chris Lattner43b429b2006-05-02 18:27:26 +0000150 return false;
Chris Lattner0e180502005-07-11 06:34:30 +0000151}
152
153//===----------------------------------------------------------------------===//
154// ELFWriter Implementation
155//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +0000156
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000157ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
158 e_machine = 0; // e_machine defaults to 'No Machine'
159 e_flags = 0; // e_flags defaults to 0, no flags.
160
Jeff Cohen00b168892005-07-27 06:12:32 +0000161 is64Bit = TM.getTargetData().getPointerSizeInBits() == 64;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000162 isLittleEndian = TM.getTargetData().isLittleEndian();
Chris Lattneraa507db2005-07-11 05:17:18 +0000163
164 // Create the machine code emitter object for this target.
165 MCE = new ELFCodeEmitter(*this);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000166 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +0000167}
168
169ELFWriter::~ELFWriter() {
170 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000171}
172
173// doInitialization - Emit the file header and all of the global variables for
174// the module to the ELF file.
175bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000176 Mang = new Mangler(M);
177
Chris Lattner5f48ff72005-07-16 08:01:13 +0000178 // Local alias to shortenify coming code.
179 std::vector<unsigned char> &FH = FileHeader;
Jeff Cohen00b168892005-07-27 06:12:32 +0000180
Chris Lattner5f48ff72005-07-16 08:01:13 +0000181 outbyte(FH, 0x7F); // EI_MAG0
182 outbyte(FH, 'E'); // EI_MAG1
183 outbyte(FH, 'L'); // EI_MAG2
184 outbyte(FH, 'F'); // EI_MAG3
185 outbyte(FH, is64Bit ? 2 : 1); // EI_CLASS
186 outbyte(FH, isLittleEndian ? 1 : 2); // EI_DATA
187 outbyte(FH, 1); // EI_VERSION
188 FH.resize(16); // EI_PAD up to 16 bytes.
Jeff Cohen00b168892005-07-27 06:12:32 +0000189
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000190 // This should change for shared objects.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000191 outhalf(FH, 1); // e_type = ET_REL
192 outhalf(FH, e_machine); // e_machine = whatever the target wants
193 outword(FH, 1); // e_version = 1
194 outaddr(FH, 0); // e_entry = 0 -> no entry point in .o file
195 outaddr(FH, 0); // e_phoff = 0 -> no program header for .o
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000196
Chris Lattner5f48ff72005-07-16 08:01:13 +0000197 ELFHeader_e_shoff_Offset = FH.size();
198 outaddr(FH, 0); // e_shoff
199 outword(FH, e_flags); // e_flags = whatever the target wants
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000200
Chris Lattner5f48ff72005-07-16 08:01:13 +0000201 outhalf(FH, is64Bit ? 64 : 52); // e_ehsize = ELF header size
202 outhalf(FH, 0); // e_phentsize = prog header entry size
203 outhalf(FH, 0); // e_phnum = # prog header entries = 0
204 outhalf(FH, is64Bit ? 64 : 40); // e_shentsize = sect hdr entry size
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000205
Jeff Cohen00b168892005-07-27 06:12:32 +0000206
Chris Lattner5f48ff72005-07-16 08:01:13 +0000207 ELFHeader_e_shnum_Offset = FH.size();
208 outhalf(FH, 0); // e_shnum = # of section header ents
209 ELFHeader_e_shstrndx_Offset = FH.size();
210 outhalf(FH, 0); // e_shstrndx = Section # of '.shstrtab'
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000211
Chris Lattner5f48ff72005-07-16 08:01:13 +0000212 // Add the null section, which is required to be first in the file.
Chris Lattner00033952005-07-16 17:36:04 +0000213 getSection("", 0, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000214
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000215 // Start up the symbol table. The first entry in the symtab is the null
216 // entry.
217 SymbolTable.push_back(ELFSym(0));
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000218
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000219 return false;
220}
221
Chris Lattner05895232005-07-16 17:41:06 +0000222void ELFWriter::EmitGlobal(GlobalVariable *GV) {
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000223 // If this is an external global, emit it now. TODO: Note that it would be
224 // better to ignore the symbol here and only add it to the symbol table if
225 // referenced.
226 if (!GV->hasInitializer()) {
227 ELFSym ExternalSym(GV);
228 ExternalSym.SetBind(ELFSym::STB_GLOBAL);
229 ExternalSym.SetType(ELFSym::STT_NOTYPE);
230 ExternalSym.SectionIdx = ELFSection::SHN_UNDEF;
231 SymbolTable.push_back(ExternalSym);
232 return;
233 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000234
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000235 const Type *GVType = (const Type*)GV->getType();
236 unsigned Align = TM.getTargetData().getTypeAlignment(GVType);
237 unsigned Size = TM.getTargetData().getTypeSize(GVType);
238
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000239 // If this global has a zero initializer, it is part of the .bss or common
240 // section.
241 if (GV->getInitializer()->isNullValue()) {
242 // If this global is part of the common block, add it now. Variables are
243 // part of the common block if they are zero initialized and allowed to be
244 // merged with other symbols.
245 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
246 ELFSym CommonSym(GV);
247 // Value for common symbols is the alignment required.
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000248 CommonSym.Value = Align;
249 CommonSym.Size = Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000250 CommonSym.SetBind(ELFSym::STB_GLOBAL);
251 CommonSym.SetType(ELFSym::STT_OBJECT);
252 // TODO SOMEDAY: add ELF visibility.
253 CommonSym.SectionIdx = ELFSection::SHN_COMMON;
254 SymbolTable.push_back(CommonSym);
255 return;
256 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000257
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000258 // Otherwise, this symbol is part of the .bss section. Emit it now.
259
260 // Handle alignment. Ensure section is aligned at least as much as required
261 // by this symbol.
Chris Lattner05895232005-07-16 17:41:06 +0000262 ELFSection &BSSSection = getBSSSection();
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000263 BSSSection.Align = std::max(BSSSection.Align, Align);
264
265 // Within the section, emit enough virtual padding to get us to an alignment
266 // boundary.
267 if (Align)
268 BSSSection.Size = (BSSSection.Size + Align - 1) & ~(Align-1);
269
270 ELFSym BSSSym(GV);
271 BSSSym.Value = BSSSection.Size;
272 BSSSym.Size = Size;
273 BSSSym.SetType(ELFSym::STT_OBJECT);
274
275 switch (GV->getLinkage()) {
276 default: // weak/linkonce handled above
277 assert(0 && "Unexpected linkage type!");
278 case GlobalValue::AppendingLinkage: // FIXME: This should be improved!
279 case GlobalValue::ExternalLinkage:
280 BSSSym.SetBind(ELFSym::STB_GLOBAL);
281 break;
282 case GlobalValue::InternalLinkage:
283 BSSSym.SetBind(ELFSym::STB_LOCAL);
284 break;
285 }
286
287 // Set the idx of the .bss section
Chris Lattner5f48ff72005-07-16 08:01:13 +0000288 BSSSym.SectionIdx = BSSSection.SectionIdx;
Chris Lattner4c47e3a2005-07-08 05:47:00 +0000289 SymbolTable.push_back(BSSSym);
290
291 // Reserve space in the .bss section for this symbol.
292 BSSSection.Size += Size;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000293 return;
294 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000295
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000296 // FIXME: handle .rodata
297 //assert(!GV->isConstant() && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000298
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000299 // FIXME: handle .data
300 //assert(0 && "unimp");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000301}
302
303
304bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000305 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000306 return false;
307}
308
309/// doFinalization - Now that the module has been completely processed, emit
310/// the ELF file to 'O'.
311bool ELFWriter::doFinalization(Module &M) {
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000312 // Okay, the ELF header and .text sections have been completed, build the
313 // .data, .bss, and "common" sections next.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000314 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
315 I != E; ++I)
Chris Lattner05895232005-07-16 17:41:06 +0000316 EmitGlobal(I);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000317
318 // Emit the symbol table now, if non-empty.
319 EmitSymbolTable();
320
321 // FIXME: Emit the relocations now.
322
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000323 // Emit the string table for the sections in the ELF file we have.
324 EmitSectionTableStringTable();
325
Chris Lattner5f48ff72005-07-16 08:01:13 +0000326 // Emit the sections to the .o file, and emit the section table for the file.
327 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000328
Chris Lattner5f48ff72005-07-16 08:01:13 +0000329 // We are done with the abstract symbols.
330 SectionList.clear();
331 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000332
333 // Release the name mangler object.
334 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000335 return false;
336}
337
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000338/// EmitSymbolTable - If the current symbol table is non-empty, emit the string
339/// table for it and then the symbol table itself.
340void ELFWriter::EmitSymbolTable() {
341 if (SymbolTable.size() == 1) return; // Only the null entry.
342
343 // FIXME: compact all local symbols to the start of the symtab.
344 unsigned FirstNonLocalSymbol = 1;
345
Chris Lattner00033952005-07-16 17:36:04 +0000346 ELFSection &StrTab = getSection(".strtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000347 StrTab.Align = 1;
348
Chris Lattner5f48ff72005-07-16 08:01:13 +0000349 DataBuffer &StrTabBuf = StrTab.SectionData;
350
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000351 // Set the zero'th symbol to a null byte, as required.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000352 outbyte(StrTabBuf, 0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000353 SymbolTable[0].NameIdx = 0;
354 unsigned Index = 1;
355 for (unsigned i = 1, e = SymbolTable.size(); i != e; ++i) {
Chris Lattner5acd1202005-07-11 03:11:47 +0000356 // Use the name mangler to uniquify the LLVM symbol.
357 std::string Name = Mang->getValueName(SymbolTable[i].GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000358
359 if (Name.empty()) {
360 SymbolTable[i].NameIdx = 0;
361 } else {
362 SymbolTable[i].NameIdx = Index;
363
364 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000365 StrTabBuf.insert(StrTabBuf.end(), Name.begin(), Name.end());
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000366
367 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000368 StrTabBuf.push_back(0);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000369
370 // Keep track of the number of bytes emitted to this section.
371 Index += Name.size()+1;
372 }
373 }
Chris Lattner5f48ff72005-07-16 08:01:13 +0000374 assert(Index == StrTabBuf.size());
375 StrTab.Size = Index;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000376
377 // Now that we have emitted the string table and know the offset into the
378 // string table of each symbol, emit the symbol table itself.
Chris Lattner00033952005-07-16 17:36:04 +0000379 ELFSection &SymTab = getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
Chris Lattner46c53052005-07-12 06:57:52 +0000380 SymTab.Align = is64Bit ? 8 : 4;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000381 SymTab.Link = SymTab.SectionIdx; // Section Index of .strtab.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000382 SymTab.Info = FirstNonLocalSymbol; // First non-STB_LOCAL symbol.
383 SymTab.EntSize = 16; // Size of each symtab entry. FIXME: wrong for ELF64
Chris Lattner5f48ff72005-07-16 08:01:13 +0000384 DataBuffer &SymTabBuf = SymTab.SectionData;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000385
Chris Lattner46c53052005-07-12 06:57:52 +0000386 if (!is64Bit) { // 32-bit and 64-bit formats are shuffled a bit.
387 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
388 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000389 outword(SymTabBuf, Sym.NameIdx);
390 outaddr32(SymTabBuf, Sym.Value);
391 outword(SymTabBuf, Sym.Size);
392 outbyte(SymTabBuf, Sym.Info);
393 outbyte(SymTabBuf, Sym.Other);
394 outhalf(SymTabBuf, Sym.SectionIdx);
Chris Lattner46c53052005-07-12 06:57:52 +0000395 }
396 } else {
397 for (unsigned i = 0, e = SymbolTable.size(); i != e; ++i) {
398 ELFSym &Sym = SymbolTable[i];
Chris Lattner5f48ff72005-07-16 08:01:13 +0000399 outword(SymTabBuf, Sym.NameIdx);
400 outbyte(SymTabBuf, Sym.Info);
401 outbyte(SymTabBuf, Sym.Other);
402 outhalf(SymTabBuf, Sym.SectionIdx);
403 outaddr64(SymTabBuf, Sym.Value);
404 outxword(SymTabBuf, Sym.Size);
Chris Lattner46c53052005-07-12 06:57:52 +0000405 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000406 }
407
Chris Lattner5f48ff72005-07-16 08:01:13 +0000408 SymTab.Size = SymTabBuf.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000409}
410
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000411/// EmitSectionTableStringTable - This method adds and emits a section for the
412/// ELF Section Table string table: the string table that holds all of the
413/// section names.
414void ELFWriter::EmitSectionTableStringTable() {
415 // First step: add the section for the string table to the list of sections:
Chris Lattner00033952005-07-16 17:36:04 +0000416 ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000417
418 // Now that we know which section number is the .shstrtab section, update the
419 // e_shstrndx entry in the ELF header.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000420 fixhalf(FileHeader, SHStrTab.SectionIdx, ELFHeader_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000421
422 // Set the NameIdx of each section in the string table and emit the bytes for
423 // the string table.
424 unsigned Index = 0;
Chris Lattner5f48ff72005-07-16 08:01:13 +0000425 DataBuffer &Buf = SHStrTab.SectionData;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000426
Chris Lattner5f48ff72005-07-16 08:01:13 +0000427 for (std::list<ELFSection>::iterator I = SectionList.begin(),
428 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000429 // Set the index into the table. Note if we have lots of entries with
430 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000431 I->NameIdx = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000432
433 // Add the name to the output buffer, including the null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000434 Buf.insert(Buf.end(), I->Name.begin(), I->Name.end());
435
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000436 // Add a null terminator.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000437 Buf.push_back(0);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000438
439 // Keep track of the number of bytes emitted to this section.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000440 Index += I->Name.size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000441 }
442
443 // Set the size of .shstrtab now that we know what it is.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000444 assert(Index == Buf.size());
445 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000446}
447
Chris Lattner5f48ff72005-07-16 08:01:13 +0000448/// OutputSectionsAndSectionTable - Now that we have constructed the file header
449/// and all of the sections, emit these to the ostream destination and emit the
450/// SectionTable.
451void ELFWriter::OutputSectionsAndSectionTable() {
452 // Pass #1: Compute the file offset for each section.
453 size_t FileOff = FileHeader.size(); // File header first.
454
455 // Emit all of the section data in order.
456 for (std::list<ELFSection>::iterator I = SectionList.begin(),
457 E = SectionList.end(); I != E; ++I) {
458 // Align FileOff to whatever the alignment restrictions of the section are.
459 if (I->Align)
460 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
461 I->Offset = FileOff;
462 FileOff += I->SectionData.size();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000463 }
464
Chris Lattner5f48ff72005-07-16 08:01:13 +0000465 // Align Section Header.
466 unsigned TableAlign = is64Bit ? 8 : 4;
467 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
468
469 // Now that we know where all of the sections will be emitted, set the e_shnum
470 // entry in the ELF header.
471 fixhalf(FileHeader, NumSections, ELFHeader_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000472
Chris Lattner5f48ff72005-07-16 08:01:13 +0000473 // Now that we know the offset in the file of the section table, update the
474 // e_shoff address in the ELF header.
475 fixaddr(FileHeader, FileOff, ELFHeader_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000476
Chris Lattner5f48ff72005-07-16 08:01:13 +0000477 // Now that we know all of the data in the file header, emit it and all of the
478 // sections!
479 O.write((char*)&FileHeader[0], FileHeader.size());
480 FileOff = FileHeader.size();
481 DataBuffer().swap(FileHeader);
482
483 DataBuffer Table;
484
485 // Emit all of the section data and build the section table itself.
486 while (!SectionList.empty()) {
487 const ELFSection &S = *SectionList.begin();
488
489 // Align FileOff to whatever the alignment restrictions of the section are.
490 if (S.Align)
491 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
492 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000493 O.put((char)0xAB);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000494 O.write((char*)&S.SectionData[0], S.SectionData.size());
495 FileOff += S.SectionData.size();
496
497 outword(Table, S.NameIdx); // sh_name - Symbol table name idx
498 outword(Table, S.Type); // sh_type - Section contents & semantics
499 outword(Table, S.Flags); // sh_flags - Section flags.
500 outaddr(Table, S.Addr); // sh_addr - The mem addr this section is in.
501 outaddr(Table, S.Offset); // sh_offset - Offset from the file start.
502 outword(Table, S.Size); // sh_size - The section size.
503 outword(Table, S.Link); // sh_link - Section header table index link.
504 outword(Table, S.Info); // sh_info - Auxillary information.
505 outword(Table, S.Align); // sh_addralign - Alignment of section.
506 outword(Table, S.EntSize); // sh_entsize - Size of entries in the section.
507
508 SectionList.pop_front();
509 }
510
511 // Align output for the section table.
512 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
513 FileOff != NewFileOff; ++FileOff)
Jeff Cohen23c73a12005-08-19 16:19:21 +0000514 O.put((char)0xAB);
Jeff Cohen00b168892005-07-27 06:12:32 +0000515
Chris Lattner5f48ff72005-07-16 08:01:13 +0000516 // Emit the section table itself.
517 O.write((char*)&Table[0], Table.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000518}