blob: 03db65699a2a0e63a0eb675bc8a924e587befbfe [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 Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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//
Chris Lattner35f0a4f2005-06-27 06:29:00 +000029//===----------------------------------------------------------------------===//
30
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000031#define DEBUG_TYPE "elfwriter"
32
Bill Wendling8f84f1f2007-02-08 01:35:27 +000033#include "ELFWriter.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000034#include "ELFCodeEmitter.h"
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000035#include "ELF.h"
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000036#include "llvm/Constants.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000037#include "llvm/Module.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000038#include "llvm/PassManager.h"
Chris Lattner02b73ab2009-01-04 20:19:20 +000039#include "llvm/DerivedTypes.h"
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000040#include "llvm/CodeGen/BinaryObject.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000041#include "llvm/CodeGen/FileWriters.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000042#include "llvm/CodeGen/MachineCodeEmitter.h"
43#include "llvm/CodeGen/MachineConstantPool.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000044#include "llvm/CodeGen/MachineFunctionPass.h"
Owen Anderson07000c62006-05-12 06:33:49 +000045#include "llvm/Target/TargetData.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000046#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000047#include "llvm/Support/Mangler.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000048#include "llvm/Support/Streams.h"
Owen Andersoncb371882008-08-21 00:14:44 +000049#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000050#include "llvm/Support/Debug.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000051#include <list>
Chris Lattner35f0a4f2005-06-27 06:29:00 +000052using namespace llvm;
53
Devang Patel19974732007-05-03 01:11:54 +000054char ELFWriter::ID = 0;
Bill Wendling8f84f1f2007-02-08 01:35:27 +000055/// AddELFWriter - Concrete function to add the ELF writer to the function pass
56/// manager.
Dan Gohmanbfae8312008-03-11 22:29:46 +000057MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000058 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000059 TargetMachine &TM) {
60 ELFWriter *EW = new ELFWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000061 PM.add(EW);
Bill Wendling8f84f1f2007-02-08 01:35:27 +000062 return &EW->getMachineCodeEmitter();
63}
64
Chris Lattner0e180502005-07-11 06:34:30 +000065//===----------------------------------------------------------------------===//
Chris Lattner0e180502005-07-11 06:34:30 +000066// ELFWriter Implementation
67//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +000068
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000069ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000070 : MachineFunctionPass(&ID), O(o), TM(tm),
71 is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64),
72 isLittleEndian(TM.getTargetData()->isLittleEndian()),
73 ElfHdr(isLittleEndian, is64Bit) {
Chris Lattneraa507db2005-07-11 05:17:18 +000074
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000075 TAI = TM.getTargetAsmInfo();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000076 TEW = TM.getELFWriterInfo();
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000077
Chris Lattneraa507db2005-07-11 05:17:18 +000078 // Create the machine code emitter object for this target.
Bill Wendlinge9116152007-01-17 09:06:13 +000079 MCE = new ELFCodeEmitter(*this);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000080
81 // Inital number of sections
Chris Lattner5f48ff72005-07-16 08:01:13 +000082 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000083}
84
85ELFWriter::~ELFWriter() {
86 delete MCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +000087}
88
89// doInitialization - Emit the file header and all of the global variables for
90// the module to the ELF file.
91bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +000092 Mang = new Mangler(M);
93
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000094 // ELF Header
95 // ----------
96 // Fields e_shnum e_shstrndx are only known after all section have
97 // been emitted. They locations in the ouput buffer are recorded so
98 // to be patched up later.
99 //
100 // Note
101 // ----
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000102 // emitWord method behaves differently for ELF32 and ELF64, writing
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000103 // 4 bytes in the former and 8 in the last for *_off and *_addr elf types
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000104
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000105 ElfHdr.emitByte(0x7f); // e_ident[EI_MAG0]
106 ElfHdr.emitByte('E'); // e_ident[EI_MAG1]
107 ElfHdr.emitByte('L'); // e_ident[EI_MAG2]
108 ElfHdr.emitByte('F'); // e_ident[EI_MAG3]
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000109
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000110 ElfHdr.emitByte(TEW->getEIClass()); // e_ident[EI_CLASS]
111 ElfHdr.emitByte(TEW->getEIData()); // e_ident[EI_DATA]
112 ElfHdr.emitByte(EV_CURRENT); // e_ident[EI_VERSION]
113 ElfHdr.emitAlignment(16); // e_ident[EI_NIDENT-EI_PAD]
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000114
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000115 ElfHdr.emitWord16(ET_REL); // e_type
116 ElfHdr.emitWord16(TEW->getEMachine()); // e_machine = target
117 ElfHdr.emitWord32(EV_CURRENT); // e_version
118 ElfHdr.emitWord(0); // e_entry, no entry point in .o file
119 ElfHdr.emitWord(0); // e_phoff, no program header for .o
120 ELFHdr_e_shoff_Offset = ElfHdr.size();
121 ElfHdr.emitWord(0); // e_shoff = sec hdr table off in bytes
122 ElfHdr.emitWord32(TEW->getEFlags()); // e_flags = whatever the target wants
123 ElfHdr.emitWord16(TEW->getHdrSize()); // e_ehsize = ELF header size
124 ElfHdr.emitWord16(0); // e_phentsize = prog header entry size
125 ElfHdr.emitWord16(0); // e_phnum = # prog header entries = 0
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000126
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000127 // e_shentsize = Section header entry size
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000128 ElfHdr.emitWord16(TEW->getSHdrSize());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000129
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000130 // e_shnum = # of section header ents
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000131 ELFHdr_e_shnum_Offset = ElfHdr.size();
132 ElfHdr.emitWord16(0); // Placeholder
Jeff Cohen00b168892005-07-27 06:12:32 +0000133
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000134 // e_shstrndx = Section # of '.shstrtab'
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000135 ELFHdr_e_shstrndx_Offset = ElfHdr.size();
136 ElfHdr.emitWord16(0); // Placeholder
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000137
Chris Lattner5f48ff72005-07-16 08:01:13 +0000138 // Add the null section, which is required to be first in the file.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000139 getNullSection();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000140
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000141 return false;
142}
143
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000144unsigned ELFWriter::getGlobalELFLinkage(const GlobalVariable *GV) {
145 if (GV->hasInternalLinkage())
146 return ELFSym::STB_LOCAL;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000147
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000148 if (GV->hasWeakLinkage())
149 return ELFSym::STB_WEAK;
150
151 return ELFSym::STB_GLOBAL;
152}
153
154// For global symbols without a section, return the Null section as a
155// placeholder
156ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV,
157 ELFSym &Sym) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000158 const Section *S = TAI->SectionForGlobal(GV);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000159 unsigned Flags = S->getFlags();
160 unsigned SectionType = ELFSection::SHT_PROGBITS;
161 unsigned SHdrFlags = ELFSection::SHF_ALLOC;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000162 DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n";
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000163
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000164 // If this is an external global, the symbol does not have a section.
165 if (!GV->hasInitializer()) {
166 Sym.SectionIdx = ELFSection::SHN_UNDEF;
167 return getNullSection();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000168 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000169
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000170 const TargetData *TD = TM.getTargetData();
171 unsigned Align = TD->getPreferredAlignment(GV);
172 Constant *CV = GV->getInitializer();
173
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000174 if (Flags & SectionFlags::Code)
175 SHdrFlags |= ELFSection::SHF_EXECINSTR;
176 if (Flags & SectionFlags::Writeable)
177 SHdrFlags |= ELFSection::SHF_WRITE;
178 if (Flags & SectionFlags::Mergeable)
179 SHdrFlags |= ELFSection::SHF_MERGE;
180 if (Flags & SectionFlags::TLS)
181 SHdrFlags |= ELFSection::SHF_TLS;
182 if (Flags & SectionFlags::Strings)
183 SHdrFlags |= ELFSection::SHF_STRINGS;
184
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000185 // If this global has a zero initializer, go to .bss or common section.
186 // Variables are part of the common block if they are zero initialized
187 // and allowed to be merged with other symbols.
188 if (CV->isNullValue() || isa<UndefValue>(CV)) {
189 SectionType = ELFSection::SHT_NOBITS;
190 ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags);
191 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
192 GV->hasCommonLinkage()) {
193 Sym.SectionIdx = ELFSection::SHN_COMMON;
194 Sym.IsCommon = true;
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000195 ElfS.Align = 1;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000196 return ElfS;
197 }
198 Sym.IsBss = true;
199 Sym.SectionIdx = ElfS.SectionIdx;
200 if (Align) ElfS.Size = (ElfS.Size + Align-1) & ~(Align-1);
201 ElfS.Align = std::max(ElfS.Align, Align);
202 return ElfS;
203 }
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000204
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000205 Sym.IsConstant = true;
206 ELFSection &ElfS = getSection(S->getName(), SectionType, SHdrFlags);
207 Sym.SectionIdx = ElfS.SectionIdx;
208 ElfS.Align = std::max(ElfS.Align, Align);
209 return ElfS;
210}
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000211
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000212void ELFWriter::EmitFunctionDeclaration(const Function *F) {
213 ELFSym GblSym(F);
214 GblSym.setBind(ELFSym::STB_GLOBAL);
215 GblSym.setType(ELFSym::STT_NOTYPE);
216 GblSym.SectionIdx = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000217 SymbolList.push_back(GblSym);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000218}
219
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000220void ELFWriter::EmitGlobalVar(const GlobalVariable *GV) {
221 unsigned SymBind = getGlobalELFLinkage(GV);
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000222 unsigned Align=0, Size=0;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000223 ELFSym GblSym(GV);
224 GblSym.setBind(SymBind);
225
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000226 if (GV->hasInitializer()) {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000227 GblSym.setType(ELFSym::STT_OBJECT);
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000228 const TargetData *TD = TM.getTargetData();
229 Align = TD->getPreferredAlignment(GV);
230 Size = TD->getTypeAllocSize(GV->getInitializer()->getType());
231 GblSym.Size = Size;
232 } else {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000233 GblSym.setType(ELFSym::STT_NOTYPE);
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000234 }
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000235
236 ELFSection &GblSection = getGlobalSymELFSection(GV, GblSym);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000237
238 if (GblSym.IsCommon) {
239 GblSym.Value = Align;
240 } else if (GblSym.IsBss) {
241 GblSym.Value = GblSection.Size;
242 GblSection.Size += Size;
243 } else if (GblSym.IsConstant){
244 // GblSym.Value should contain the symbol index inside the section,
245 // and all symbols should start on their required alignment boundary
246 GblSym.Value = (GblSection.size() + (Align-1)) & (-Align);
247 GblSection.emitAlignment(Align);
248 EmitGlobalConstant(GV->getInitializer(), GblSection);
249 }
250
251 // Local symbols should come first on the symbol table.
252 if (!GV->hasPrivateLinkage()) {
253 if (SymBind == ELFSym::STB_LOCAL)
254 SymbolList.push_front(GblSym);
255 else
256 SymbolList.push_back(GblSym);
257 }
258}
259
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000260void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000261 ELFSection &GblS) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000262
263 // Print the fields in successive locations. Pad to align if needed!
264 const TargetData *TD = TM.getTargetData();
265 unsigned Size = TD->getTypeAllocSize(CVS->getType());
266 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
267 uint64_t sizeSoFar = 0;
268 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
269 const Constant* field = CVS->getOperand(i);
270
271 // Check if padding is needed and insert one or more 0s.
272 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
273 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
274 - cvsLayout->getElementOffset(i)) - fieldSize;
275 sizeSoFar += fieldSize + padSize;
276
277 // Now print the actual field value.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000278 EmitGlobalConstant(field, GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000279
280 // Insert padding - this may include padding to increase the size of the
281 // current field up to the ABI size (if the struct is not packed) as well
282 // as padding to ensure that the next field starts at the right offset.
283 for (unsigned p=0; p < padSize; p++)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000284 GblS.emitByte(0);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000285 }
286 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
287 "Layout of constant struct may be incorrect!");
288}
289
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000290void ELFWriter::EmitGlobalConstant(const Constant *CV, ELFSection &GblS) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000291 const TargetData *TD = TM.getTargetData();
292 unsigned Size = TD->getTypeAllocSize(CV->getType());
293
294 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
295 if (CVA->isString()) {
296 std::string GblStr = CVA->getAsString();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000297 GblStr.resize(GblStr.size()-1);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000298 GblS.emitString(GblStr);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000299 } else { // Not a string. Print the values in successive locations
300 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000301 EmitGlobalConstant(CVA->getOperand(i), GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000302 }
303 return;
304 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000305 EmitGlobalConstantStruct(CVS, GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000306 return;
307 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
308 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
309 if (CFP->getType() == Type::DoubleTy)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000310 GblS.emitWord64(Val);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000311 else if (CFP->getType() == Type::FloatTy)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000312 GblS.emitWord32(Val);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000313 else if (CFP->getType() == Type::X86_FP80Ty) {
314 assert(0 && "X86_FP80Ty global emission not implemented");
315 } else if (CFP->getType() == Type::PPC_FP128Ty)
316 assert(0 && "PPC_FP128Ty global emission not implemented");
317 return;
318 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
319 if (Size == 4)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000320 GblS.emitWord32(CI->getZExtValue());
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000321 else if (Size == 8)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000322 GblS.emitWord64(CI->getZExtValue());
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000323 else
324 assert(0 && "LargeInt global emission not implemented");
325 return;
326 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
327 const VectorType *PTy = CP->getType();
328 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000329 EmitGlobalConstant(CP->getOperand(I), GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000330 return;
331 }
332 assert(0 && "unknown global constant");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000333}
334
335
336bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattneraa507db2005-07-11 05:17:18 +0000337 // Nothing to do here, this is all done through the MCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000338 return false;
339}
340
341/// doFinalization - Now that the module has been completely processed, emit
342/// the ELF file to 'O'.
343bool ELFWriter::doFinalization(Module &M) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000344 /// FIXME: This should be removed when moving to ObjectCodeEmiter. Since the
345 /// current ELFCodeEmiter uses CurrBuff, ... it doesn't update S.Data
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000346 /// vector size for .text sections, so this is a quick dirty fix
347 ELFSection &TS = getTextSection();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000348 if (TS.Size) {
349 BinaryData &BD = TS.getData();
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000350 for (unsigned e=0; e<TS.Size; ++e)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000351 BD.push_back(BD[e]);
352 }
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000353
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000354 // Emit .data section placeholder
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000355 getDataSection();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000356
357 // Emit .bss section placeholder
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000358 getBSSSection();
359
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000360 // Build and emit data, bss and "common" sections.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000361 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000362 I != E; ++I) {
363 EmitGlobalVar(I);
364 GblSymLookup[I] = 0;
365 }
366
367 // Emit all pending globals
368 // TODO: this should be done only for referenced symbols
369 for (SetVector<GlobalValue*>::const_iterator I = PendingGlobals.begin(),
370 E = PendingGlobals.end(); I != E; ++I) {
371
372 // No need to emit the symbol again
373 if (GblSymLookup.find(*I) != GblSymLookup.end())
374 continue;
375
376 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
377 EmitGlobalVar(GV);
378 } else if (Function *F = dyn_cast<Function>(*I)) {
379 // If function is not in GblSymLookup, it doesn't have a body,
380 // so emit the symbol as a function declaration (no section associated)
381 EmitFunctionDeclaration(F);
382 } else {
383 assert("unknown howto handle pending global");
384 }
385 GblSymLookup[*I] = 0;
386 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000387
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000388 // Emit non-executable stack note
389 if (TAI->getNonexecutableStackDirective())
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000390 getNonExecStackSection();
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000391
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000392 // Emit string table
393 EmitStringTable();
394
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000395 // Emit the symbol table now, if non-empty.
396 EmitSymbolTable();
397
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000398 // Emit the relocation sections.
399 EmitRelocations();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000400
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000401 // Emit the sections string table.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000402 EmitSectionTableStringTable();
403
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000404 // Dump the sections and section table to the .o file.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000405 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000406
Chris Lattner5f48ff72005-07-16 08:01:13 +0000407 // We are done with the abstract symbols.
408 SectionList.clear();
409 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000410
411 // Release the name mangler object.
412 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000413 return false;
414}
415
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000416/// EmitRelocations - Emit relocations
417void ELFWriter::EmitRelocations() {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000418
419 // Create Relocation sections for each section which needs it.
420 for (std::list<ELFSection>::iterator I = SectionList.begin(),
421 E = SectionList.end(); I != E; ++I) {
422
423 // This section does not have relocations
424 if (!I->hasRelocations()) continue;
425
426 // Get the relocation section for section 'I'
427 bool HasRelA = TEW->hasRelocationAddend();
428 ELFSection &RelSec = getRelocSection(I->getName(), HasRelA);
429
430 // 'Link' - Section hdr idx of the associated symbol table
431 // 'Info' - Section hdr idx of the section to which the relocation applies
432 ELFSection &SymTab = getSymbolTableSection();
433 RelSec.Link = SymTab.SectionIdx;
434 RelSec.Info = I->SectionIdx;
435 RelSec.EntSize = TEW->getRelocationEntrySize();
436
437 // Get the relocations from Section
438 std::vector<MachineRelocation> Relos = I->getRelocations();
439 for (std::vector<MachineRelocation>::iterator MRI = Relos.begin(),
440 MRE = Relos.end(); MRI != MRE; ++MRI) {
441 MachineRelocation &MR = *MRI;
442
443 // Offset from the start of the section containing the symbol
444 unsigned Offset = MR.getMachineCodeOffset();
445
446 // Symbol index in the symbol table
447 unsigned SymIdx = 0;
448
449 // Target specific ELF relocation type
450 unsigned RelType = TEW->getRelocationType(MR.getRelocationType());
451
452 // Constant addend used to compute the value to be stored
453 // into the relocatable field
454 int64_t Addend = TEW->getAddendForRelTy(RelType);
455
456 // There are several machine relocations types, and each one of
457 // them needs a different approach to retrieve the symbol table index.
458 if (MR.isGlobalValue()) {
459 const GlobalValue *G = MR.getGlobalValue();
460 SymIdx = GblSymLookup[G];
461 } else {
462 assert(0 && "dunno how to handle other relocation types");
463 }
464
465 // Get the relocation entry and emit to the relocation section
466 ELFRelocation Rel(Offset, SymIdx, RelType, HasRelA, Addend);
467 EmitRelocation(RelSec, Rel, HasRelA);
468 }
469 }
470}
471
472/// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel'
473void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel,
474 bool HasRelA) {
475 RelSec.emitWord(Rel.getOffset());
476 RelSec.emitWord(Rel.getInfo(is64Bit));
477 if (HasRelA)
478 RelSec.emitWord(Rel.getAddend());
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000479}
480
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000481/// EmitSymbol - Write symbol 'Sym' to the symbol table 'SymbolTable'
482void ELFWriter::EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000483 if (is64Bit) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000484 SymbolTable.emitWord32(Sym.NameIdx);
485 SymbolTable.emitByte(Sym.Info);
486 SymbolTable.emitByte(Sym.Other);
487 SymbolTable.emitWord16(Sym.SectionIdx);
488 SymbolTable.emitWord64(Sym.Value);
489 SymbolTable.emitWord64(Sym.Size);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000490 } else {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000491 SymbolTable.emitWord32(Sym.NameIdx);
492 SymbolTable.emitWord32(Sym.Value);
493 SymbolTable.emitWord32(Sym.Size);
494 SymbolTable.emitByte(Sym.Info);
495 SymbolTable.emitByte(Sym.Other);
496 SymbolTable.emitWord16(Sym.SectionIdx);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000497 }
498}
499
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000500/// EmitSectionHeader - Write section 'Section' header in 'SHdrTab'
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000501/// Section Header Table
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000502void ELFWriter::EmitSectionHeader(BinaryObject &SHdrTab,
503 const ELFSection &SHdr) {
504 SHdrTab.emitWord32(SHdr.NameIdx);
505 SHdrTab.emitWord32(SHdr.Type);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000506 if (is64Bit) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000507 SHdrTab.emitWord64(SHdr.Flags);
508 SHdrTab.emitWord(SHdr.Addr);
509 SHdrTab.emitWord(SHdr.Offset);
510 SHdrTab.emitWord64(SHdr.Size);
511 SHdrTab.emitWord32(SHdr.Link);
512 SHdrTab.emitWord32(SHdr.Info);
513 SHdrTab.emitWord64(SHdr.Align);
514 SHdrTab.emitWord64(SHdr.EntSize);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000515 } else {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000516 SHdrTab.emitWord32(SHdr.Flags);
517 SHdrTab.emitWord(SHdr.Addr);
518 SHdrTab.emitWord(SHdr.Offset);
519 SHdrTab.emitWord32(SHdr.Size);
520 SHdrTab.emitWord32(SHdr.Link);
521 SHdrTab.emitWord32(SHdr.Info);
522 SHdrTab.emitWord32(SHdr.Align);
523 SHdrTab.emitWord32(SHdr.EntSize);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000524 }
525}
526
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000527/// EmitStringTable - If the current symbol table is non-empty, emit the string
528/// table for it
529void ELFWriter::EmitStringTable() {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000530 if (!SymbolList.size()) return; // Empty symbol table.
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000531 ELFSection &StrTab = getStringTableSection();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000532
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000533 // Set the zero'th symbol to a null byte, as required.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000534 StrTab.emitByte(0);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000535
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000536 // Walk on the symbol list and write symbol names into the
537 // string table.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000538 unsigned Index = 1;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000539 for (std::list<ELFSym>::iterator I = SymbolList.begin(),
540 E = SymbolList.end(); I != E; ++I) {
541
Chris Lattner5acd1202005-07-11 03:11:47 +0000542 // Use the name mangler to uniquify the LLVM symbol.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000543 std::string Name = Mang->getValueName(I->GV);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000544
545 if (Name.empty()) {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000546 I->NameIdx = 0;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000547 } else {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000548 I->NameIdx = Index;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000549 StrTab.emitString(Name);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000550
551 // Keep track of the number of bytes emitted to this section.
552 Index += Name.size()+1;
553 }
554 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000555 assert(Index == StrTab.size());
Chris Lattner5f48ff72005-07-16 08:01:13 +0000556 StrTab.Size = Index;
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000557}
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000558
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000559/// EmitSymbolTable - Emit the symbol table itself.
560void ELFWriter::EmitSymbolTable() {
561 if (!SymbolList.size()) return; // Empty symbol table.
562
563 unsigned FirstNonLocalSymbol = 1;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000564 // Now that we have emitted the string table and know the offset into the
565 // string table of each symbol, emit the symbol table itself.
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000566 ELFSection &SymTab = getSymbolTableSection();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000567 SymTab.Align = TEW->getPrefELFAlignment();
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000568
569 // Section Index of .strtab.
570 SymTab.Link = getStringTableSection().SectionIdx;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000571
572 // Size of each symtab entry.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000573 SymTab.EntSize = TEW->getSymTabEntrySize();
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000574
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000575 // The first entry in the symtab is the null symbol
576 ELFSym NullSym = ELFSym(0);
577 EmitSymbol(SymTab, NullSym);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000578
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000579 // Emit all the symbols to the symbol table. Skip the null
580 // symbol, cause it's emitted already
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000581 unsigned Index = 1;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000582 for (std::list<ELFSym>::iterator I = SymbolList.begin(),
583 E = SymbolList.end(); I != E; ++I, ++Index) {
584 // Keep track of the first non-local symbol
585 if (I->getBind() == ELFSym::STB_LOCAL)
586 FirstNonLocalSymbol++;
587
588 // Emit symbol to the symbol table
589 EmitSymbol(SymTab, *I);
590
591 // Record the symbol table index for each global value
592 GblSymLookup[I->GV] = Index;
593 }
594
595 SymTab.Info = FirstNonLocalSymbol;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000596 SymTab.Size = SymTab.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000597}
598
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000599/// EmitSectionTableStringTable - This method adds and emits a section for the
600/// ELF Section Table string table: the string table that holds all of the
601/// section names.
602void ELFWriter::EmitSectionTableStringTable() {
603 // First step: add the section for the string table to the list of sections:
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000604 ELFSection &SHStrTab = getSectionHeaderStringTableSection();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000605
606 // Now that we know which section number is the .shstrtab section, update the
607 // e_shstrndx entry in the ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000608 ElfHdr.fixWord16(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000609
610 // Set the NameIdx of each section in the string table and emit the bytes for
611 // the string table.
612 unsigned Index = 0;
613
Chris Lattner5f48ff72005-07-16 08:01:13 +0000614 for (std::list<ELFSection>::iterator I = SectionList.begin(),
615 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000616 // Set the index into the table. Note if we have lots of entries with
617 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000618 I->NameIdx = Index;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000619 SHStrTab.emitString(I->getName());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000620
621 // Keep track of the number of bytes emitted to this section.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000622 Index += I->getName().size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000623 }
624
625 // Set the size of .shstrtab now that we know what it is.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000626 assert(Index == SHStrTab.size());
Chris Lattner5f48ff72005-07-16 08:01:13 +0000627 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000628}
629
Chris Lattner5f48ff72005-07-16 08:01:13 +0000630/// OutputSectionsAndSectionTable - Now that we have constructed the file header
631/// and all of the sections, emit these to the ostream destination and emit the
632/// SectionTable.
633void ELFWriter::OutputSectionsAndSectionTable() {
634 // Pass #1: Compute the file offset for each section.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000635 size_t FileOff = ElfHdr.size(); // File header first.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000636
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000637 // Adjust alignment of all section if needed.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000638 for (std::list<ELFSection>::iterator I = SectionList.begin(),
639 E = SectionList.end(); I != E; ++I) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000640
641 // Section idx 0 has 0 offset
642 if (!I->SectionIdx)
643 continue;
644
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000645 if (!I->size()) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000646 I->Offset = FileOff;
647 continue;
648 }
649
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000650 // Update Section size
651 if (!I->Size)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000652 I->Size = I->size();
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000653
Chris Lattner5f48ff72005-07-16 08:01:13 +0000654 // Align FileOff to whatever the alignment restrictions of the section are.
655 if (I->Align)
656 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000657
Chris Lattner5f48ff72005-07-16 08:01:13 +0000658 I->Offset = FileOff;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000659 FileOff += I->Size;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000660 }
661
Chris Lattner5f48ff72005-07-16 08:01:13 +0000662 // Align Section Header.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000663 unsigned TableAlign = TEW->getPrefELFAlignment();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000664 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
665
666 // Now that we know where all of the sections will be emitted, set the e_shnum
667 // entry in the ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000668 ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000669
Chris Lattner5f48ff72005-07-16 08:01:13 +0000670 // Now that we know the offset in the file of the section table, update the
671 // e_shoff address in the ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000672 ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000673
Chris Lattner5f48ff72005-07-16 08:01:13 +0000674 // Now that we know all of the data in the file header, emit it and all of the
675 // sections!
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000676 O.write((char *)&ElfHdr.getData()[0], ElfHdr.size());
677 FileOff = ElfHdr.size();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000678
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000679 // Section Header Table blob
680 BinaryObject SHdrTable(isLittleEndian, is64Bit);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000681
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000682 // Emit all of sections to the file and build the section header table.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000683 while (!SectionList.empty()) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000684 ELFSection &S = *SectionList.begin();
685 DOUT << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName()
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000686 << ", Size: " << S.Size << ", Offset: " << S.Offset
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000687 << ", SectionData Size: " << S.size() << "\n";
Chris Lattner5f48ff72005-07-16 08:01:13 +0000688
689 // Align FileOff to whatever the alignment restrictions of the section are.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000690 if (S.size()) {
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000691 if (S.Align) {
692 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
693 FileOff != NewFileOff; ++FileOff)
694 O << (char)0xAB;
695 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000696 O.write((char *)&S.getData()[0], S.Size);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000697 FileOff += S.Size;
698 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000699
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000700 EmitSectionHeader(SHdrTable, S);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000701 SectionList.pop_front();
702 }
703
704 // Align output for the section table.
705 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
706 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000707 O << (char)0xAB;
Jeff Cohen00b168892005-07-27 06:12:32 +0000708
Chris Lattner5f48ff72005-07-16 08:01:13 +0000709 // Emit the section table itself.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000710 O.write((char *)&SHdrTable.getData()[0], SHdrTable.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000711}