blob: 13180175c2ded87fef12e38138131450213d6c13 [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
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000033#include "ELF.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000034#include "ELFWriter.h"
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000035#include "ELFCodeEmitter.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"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000043#include "llvm/CodeGen/ObjectCodeEmitter.h"
44#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattneraa507db2005-07-11 05:17:18 +000045#include "llvm/CodeGen/MachineConstantPool.h"
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000046#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000047#include "llvm/Target/TargetData.h"
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +000048#include "llvm/Target/TargetELFWriterInfo.h"
Chris Lattner35f0a4f2005-06-27 06:29:00 +000049#include "llvm/Target/TargetMachine.h"
Chris Lattner5acd1202005-07-11 03:11:47 +000050#include "llvm/Support/Mangler.h"
Bill Wendlingbdc679d2006-11-29 00:39:47 +000051#include "llvm/Support/Streams.h"
Owen Andersoncb371882008-08-21 00:14:44 +000052#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000053#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000054#include "llvm/Support/ErrorHandling.h"
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000055
Chris Lattner35f0a4f2005-06-27 06:29:00 +000056using namespace llvm;
57
Devang Patel19974732007-05-03 01:11:54 +000058char ELFWriter::ID = 0;
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000059
60/// AddELFWriter - Add the ELF writer to the function pass manager
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000061ObjectCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000062 raw_ostream &O,
63 TargetMachine &TM) {
Bill Wendling8f84f1f2007-02-08 01:35:27 +000064 ELFWriter *EW = new ELFWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000065 PM.add(EW);
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000066 return EW->getObjectCodeEmitter();
Bill Wendling8f84f1f2007-02-08 01:35:27 +000067}
68
Chris Lattner0e180502005-07-11 06:34:30 +000069//===----------------------------------------------------------------------===//
Chris Lattner0e180502005-07-11 06:34:30 +000070// ELFWriter Implementation
71//===----------------------------------------------------------------------===//
Chris Lattneraa507db2005-07-11 05:17:18 +000072
Bruno Cardoso Lopes4cb31432009-06-03 17:47:27 +000073ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000074 : MachineFunctionPass(&ID), O(o), TM(tm),
75 is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64),
76 isLittleEndian(TM.getTargetData()->isLittleEndian()),
77 ElfHdr(isLittleEndian, is64Bit) {
Chris Lattneraa507db2005-07-11 05:17:18 +000078
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +000079 TAI = TM.getTargetAsmInfo();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000080 TEW = TM.getELFWriterInfo();
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +000081
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000082 // Create the object code emitter object for this target.
83 ElfCE = new ELFCodeEmitter(*this);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +000084
85 // Inital number of sections
Chris Lattner5f48ff72005-07-16 08:01:13 +000086 NumSections = 0;
Chris Lattneraa507db2005-07-11 05:17:18 +000087}
88
89ELFWriter::~ELFWriter() {
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +000090 delete ElfCE;
Chris Lattner35f0a4f2005-06-27 06:29:00 +000091}
92
93// doInitialization - Emit the file header and all of the global variables for
94// the module to the ELF file.
95bool ELFWriter::doInitialization(Module &M) {
Chris Lattner5acd1202005-07-11 03:11:47 +000096 Mang = new Mangler(M);
97
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +000098 // ELF Header
99 // ----------
100 // Fields e_shnum e_shstrndx are only known after all section have
101 // been emitted. They locations in the ouput buffer are recorded so
102 // to be patched up later.
103 //
104 // Note
105 // ----
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000106 // emitWord method behaves differently for ELF32 and ELF64, writing
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000107 // 4 bytes in the former and 8 in the last for *_off and *_addr elf types
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000108
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000109 ElfHdr.emitByte(0x7f); // e_ident[EI_MAG0]
110 ElfHdr.emitByte('E'); // e_ident[EI_MAG1]
111 ElfHdr.emitByte('L'); // e_ident[EI_MAG2]
112 ElfHdr.emitByte('F'); // e_ident[EI_MAG3]
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000113
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000114 ElfHdr.emitByte(TEW->getEIClass()); // e_ident[EI_CLASS]
115 ElfHdr.emitByte(TEW->getEIData()); // e_ident[EI_DATA]
116 ElfHdr.emitByte(EV_CURRENT); // e_ident[EI_VERSION]
117 ElfHdr.emitAlignment(16); // e_ident[EI_NIDENT-EI_PAD]
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000118
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000119 ElfHdr.emitWord16(ET_REL); // e_type
120 ElfHdr.emitWord16(TEW->getEMachine()); // e_machine = target
121 ElfHdr.emitWord32(EV_CURRENT); // e_version
122 ElfHdr.emitWord(0); // e_entry, no entry point in .o file
123 ElfHdr.emitWord(0); // e_phoff, no program header for .o
124 ELFHdr_e_shoff_Offset = ElfHdr.size();
125 ElfHdr.emitWord(0); // e_shoff = sec hdr table off in bytes
126 ElfHdr.emitWord32(TEW->getEFlags()); // e_flags = whatever the target wants
127 ElfHdr.emitWord16(TEW->getHdrSize()); // e_ehsize = ELF header size
128 ElfHdr.emitWord16(0); // e_phentsize = prog header entry size
129 ElfHdr.emitWord16(0); // e_phnum = # prog header entries = 0
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000130
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000131 // e_shentsize = Section header entry size
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000132 ElfHdr.emitWord16(TEW->getSHdrSize());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000133
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000134 // e_shnum = # of section header ents
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000135 ELFHdr_e_shnum_Offset = ElfHdr.size();
136 ElfHdr.emitWord16(0); // Placeholder
Jeff Cohen00b168892005-07-27 06:12:32 +0000137
Bruno Cardoso Lopesf5b0c5a2009-06-06 03:56:29 +0000138 // e_shstrndx = Section # of '.shstrtab'
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000139 ELFHdr_e_shstrndx_Offset = ElfHdr.size();
140 ElfHdr.emitWord16(0); // Placeholder
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000141
Chris Lattner5f48ff72005-07-16 08:01:13 +0000142 // Add the null section, which is required to be first in the file.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000143 getNullSection();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000144
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000145 return false;
146}
147
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000148unsigned ELFWriter::getGlobalELFVisibility(const GlobalValue *GV) {
149 switch (GV->getVisibility()) {
150 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000151 LLVM_UNREACHABLE("unknown visibility type");
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000152 case GlobalValue::DefaultVisibility:
153 return ELFSym::STV_DEFAULT;
154 case GlobalValue::HiddenVisibility:
155 return ELFSym::STV_HIDDEN;
156 case GlobalValue::ProtectedVisibility:
157 return ELFSym::STV_PROTECTED;
158 }
159
160 return 0;
161}
162
163unsigned ELFWriter::getGlobalELFLinkage(const GlobalValue *GV) {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000164 if (GV->hasInternalLinkage())
165 return ELFSym::STB_LOCAL;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000166
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000167 if (GV->hasWeakLinkage())
168 return ELFSym::STB_WEAK;
169
170 return ELFSym::STB_GLOBAL;
171}
172
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000173// getElfSectionFlags - Get the ELF Section Header based on the
174// flags defined in ELFTargetAsmInfo.
175unsigned ELFWriter::getElfSectionFlags(unsigned Flags) {
176 unsigned ElfSectionFlags = ELFSection::SHF_ALLOC;
177
178 if (Flags & SectionFlags::Code)
179 ElfSectionFlags |= ELFSection::SHF_EXECINSTR;
180 if (Flags & SectionFlags::Writeable)
181 ElfSectionFlags |= ELFSection::SHF_WRITE;
182 if (Flags & SectionFlags::Mergeable)
183 ElfSectionFlags |= ELFSection::SHF_MERGE;
184 if (Flags & SectionFlags::TLS)
185 ElfSectionFlags |= ELFSection::SHF_TLS;
186 if (Flags & SectionFlags::Strings)
187 ElfSectionFlags |= ELFSection::SHF_STRINGS;
188
189 return ElfSectionFlags;
190}
191
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000192// For global symbols without a section, return the Null section as a
193// placeholder
194ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV,
195 ELFSym &Sym) {
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000196 // If this is a declaration, the symbol does not have a section.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000197 if (!GV->hasInitializer()) {
198 Sym.SectionIdx = ELFSection::SHN_UNDEF;
199 return getNullSection();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000200 }
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000201
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000202 // Get the name and flags of the section for the global
203 const Section *S = TAI->SectionForGlobal(GV);
204 unsigned SectionType = ELFSection::SHT_PROGBITS;
205 unsigned SectionFlags = getElfSectionFlags(S->getFlags());
206 DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n";
207
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000208 const TargetData *TD = TM.getTargetData();
209 unsigned Align = TD->getPreferredAlignment(GV);
210 Constant *CV = GV->getInitializer();
211
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000212 // If this global has a zero initializer, go to .bss or common section.
213 // Variables are part of the common block if they are zero initialized
214 // and allowed to be merged with other symbols.
215 if (CV->isNullValue() || isa<UndefValue>(CV)) {
216 SectionType = ELFSection::SHT_NOBITS;
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000217 ELFSection &ElfS = getSection(S->getName(), SectionType, SectionFlags);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000218 if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
219 GV->hasCommonLinkage()) {
220 Sym.SectionIdx = ELFSection::SHN_COMMON;
221 Sym.IsCommon = true;
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000222 ElfS.Align = 1;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000223 return ElfS;
224 }
225 Sym.IsBss = true;
226 Sym.SectionIdx = ElfS.SectionIdx;
227 if (Align) ElfS.Size = (ElfS.Size + Align-1) & ~(Align-1);
228 ElfS.Align = std::max(ElfS.Align, Align);
229 return ElfS;
230 }
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000231
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000232 Sym.IsConstant = true;
Bruno Cardoso Lopes0b1308f2009-07-03 04:36:26 +0000233 ELFSection &ElfS = getSection(S->getName(), SectionType, SectionFlags);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000234 Sym.SectionIdx = ElfS.SectionIdx;
235 ElfS.Align = std::max(ElfS.Align, Align);
236 return ElfS;
237}
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000238
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000239void ELFWriter::EmitFunctionDeclaration(const Function *F) {
240 ELFSym GblSym(F);
241 GblSym.setBind(ELFSym::STB_GLOBAL);
242 GblSym.setType(ELFSym::STT_NOTYPE);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000243 GblSym.setVisibility(ELFSym::STV_DEFAULT);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000244 GblSym.SectionIdx = ELFSection::SHN_UNDEF;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000245 SymbolList.push_back(GblSym);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000246}
247
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000248void ELFWriter::EmitGlobalVar(const GlobalVariable *GV) {
249 unsigned SymBind = getGlobalELFLinkage(GV);
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000250 unsigned Align=0, Size=0;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000251 ELFSym GblSym(GV);
252 GblSym.setBind(SymBind);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000253 GblSym.setVisibility(getGlobalELFVisibility(GV));
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000254
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000255 if (GV->hasInitializer()) {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000256 GblSym.setType(ELFSym::STT_OBJECT);
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000257 const TargetData *TD = TM.getTargetData();
258 Align = TD->getPreferredAlignment(GV);
259 Size = TD->getTypeAllocSize(GV->getInitializer()->getType());
260 GblSym.Size = Size;
261 } else {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000262 GblSym.setType(ELFSym::STT_NOTYPE);
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000263 }
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000264
265 ELFSection &GblSection = getGlobalSymELFSection(GV, GblSym);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000266
267 if (GblSym.IsCommon) {
268 GblSym.Value = Align;
269 } else if (GblSym.IsBss) {
270 GblSym.Value = GblSection.Size;
271 GblSection.Size += Size;
272 } else if (GblSym.IsConstant){
273 // GblSym.Value should contain the symbol index inside the section,
274 // and all symbols should start on their required alignment boundary
275 GblSym.Value = (GblSection.size() + (Align-1)) & (-Align);
276 GblSection.emitAlignment(Align);
277 EmitGlobalConstant(GV->getInitializer(), GblSection);
278 }
279
280 // Local symbols should come first on the symbol table.
281 if (!GV->hasPrivateLinkage()) {
282 if (SymBind == ELFSym::STB_LOCAL)
283 SymbolList.push_front(GblSym);
284 else
285 SymbolList.push_back(GblSym);
286 }
287}
288
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000289void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000290 ELFSection &GblS) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000291
292 // Print the fields in successive locations. Pad to align if needed!
293 const TargetData *TD = TM.getTargetData();
294 unsigned Size = TD->getTypeAllocSize(CVS->getType());
295 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
296 uint64_t sizeSoFar = 0;
297 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
298 const Constant* field = CVS->getOperand(i);
299
300 // Check if padding is needed and insert one or more 0s.
301 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
302 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
303 - cvsLayout->getElementOffset(i)) - fieldSize;
304 sizeSoFar += fieldSize + padSize;
305
306 // Now print the actual field value.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000307 EmitGlobalConstant(field, GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000308
309 // Insert padding - this may include padding to increase the size of the
310 // current field up to the ABI size (if the struct is not packed) as well
311 // as padding to ensure that the next field starts at the right offset.
312 for (unsigned p=0; p < padSize; p++)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000313 GblS.emitByte(0);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000314 }
315 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
316 "Layout of constant struct may be incorrect!");
317}
318
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000319void ELFWriter::EmitGlobalConstant(const Constant *CV, ELFSection &GblS) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000320 const TargetData *TD = TM.getTargetData();
321 unsigned Size = TD->getTypeAllocSize(CV->getType());
322
323 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
324 if (CVA->isString()) {
325 std::string GblStr = CVA->getAsString();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000326 GblStr.resize(GblStr.size()-1);
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000327 GblS.emitString(GblStr);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000328 } else { // Not a string. Print the values in successive locations
329 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000330 EmitGlobalConstant(CVA->getOperand(i), GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000331 }
332 return;
333 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000334 EmitGlobalConstantStruct(CVS, GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000335 return;
336 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
337 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
338 if (CFP->getType() == Type::DoubleTy)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000339 GblS.emitWord64(Val);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000340 else if (CFP->getType() == Type::FloatTy)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000341 GblS.emitWord32(Val);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000342 else if (CFP->getType() == Type::X86_FP80Ty) {
Torok Edwinc25e7582009-07-11 20:10:48 +0000343 LLVM_UNREACHABLE("X86_FP80Ty global emission not implemented");
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000344 } else if (CFP->getType() == Type::PPC_FP128Ty)
Torok Edwinc25e7582009-07-11 20:10:48 +0000345 LLVM_UNREACHABLE("PPC_FP128Ty global emission not implemented");
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000346 return;
347 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
348 if (Size == 4)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000349 GblS.emitWord32(CI->getZExtValue());
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000350 else if (Size == 8)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000351 GblS.emitWord64(CI->getZExtValue());
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000352 else
Torok Edwinc25e7582009-07-11 20:10:48 +0000353 LLVM_UNREACHABLE("LargeInt global emission not implemented");
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000354 return;
355 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
356 const VectorType *PTy = CP->getType();
357 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000358 EmitGlobalConstant(CP->getOperand(I), GblS);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000359 return;
360 }
Torok Edwinc25e7582009-07-11 20:10:48 +0000361 LLVM_UNREACHABLE("unknown global constant");
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000362}
363
364
365bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
Bruno Cardoso Lopes6933d3e2009-07-06 09:26:48 +0000366 // Nothing to do here, this is all done through the ElfCE object above.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000367 return false;
368}
369
370/// doFinalization - Now that the module has been completely processed, emit
371/// the ELF file to 'O'.
372bool ELFWriter::doFinalization(Module &M) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000373 // Emit .data section placeholder
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000374 getDataSection();
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000375
376 // Emit .bss section placeholder
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000377 getBSSSection();
378
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000379 // Build and emit data, bss and "common" sections.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000380 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000381 I != E; ++I) {
382 EmitGlobalVar(I);
383 GblSymLookup[I] = 0;
384 }
385
386 // Emit all pending globals
387 // TODO: this should be done only for referenced symbols
388 for (SetVector<GlobalValue*>::const_iterator I = PendingGlobals.begin(),
389 E = PendingGlobals.end(); I != E; ++I) {
390
391 // No need to emit the symbol again
392 if (GblSymLookup.find(*I) != GblSymLookup.end())
393 continue;
394
395 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
396 EmitGlobalVar(GV);
397 } else if (Function *F = dyn_cast<Function>(*I)) {
398 // If function is not in GblSymLookup, it doesn't have a body,
399 // so emit the symbol as a function declaration (no section associated)
400 EmitFunctionDeclaration(F);
401 } else {
402 assert("unknown howto handle pending global");
403 }
404 GblSymLookup[*I] = 0;
405 }
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000406
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000407 // Emit non-executable stack note
408 if (TAI->getNonexecutableStackDirective())
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000409 getNonExecStackSection();
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000410
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000411 // Emit a symbol for each section created until now
412 for (std::map<std::string, ELFSection*>::iterator I = SectionLookup.begin(),
413 E = SectionLookup.end(); I != E; ++I) {
414 ELFSection *ES = I->second;
415
416 // Skip null section
417 if (ES->SectionIdx == 0) continue;
418
419 ELFSym SectionSym(0);
420 SectionSym.SectionIdx = ES->SectionIdx;
421 SectionSym.Size = 0;
422 SectionSym.setBind(ELFSym::STB_LOCAL);
423 SectionSym.setType(ELFSym::STT_SECTION);
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000424 SectionSym.setVisibility(ELFSym::STV_DEFAULT);
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000425
426 // Local symbols go in the list front
427 SymbolList.push_front(SectionSym);
428 }
429
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000430 // Emit string table
431 EmitStringTable();
432
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000433 // Emit the symbol table now, if non-empty.
434 EmitSymbolTable();
435
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000436 // Emit the relocation sections.
437 EmitRelocations();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000438
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000439 // Emit the sections string table.
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000440 EmitSectionTableStringTable();
441
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000442 // Dump the sections and section table to the .o file.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000443 OutputSectionsAndSectionTable();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000444
Chris Lattner5f48ff72005-07-16 08:01:13 +0000445 // We are done with the abstract symbols.
446 SectionList.clear();
447 NumSections = 0;
Chris Lattner5acd1202005-07-11 03:11:47 +0000448
449 // Release the name mangler object.
450 delete Mang; Mang = 0;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000451 return false;
452}
453
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000454/// EmitRelocations - Emit relocations
455void ELFWriter::EmitRelocations() {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000456
457 // Create Relocation sections for each section which needs it.
458 for (std::list<ELFSection>::iterator I = SectionList.begin(),
459 E = SectionList.end(); I != E; ++I) {
460
461 // This section does not have relocations
462 if (!I->hasRelocations()) continue;
463
464 // Get the relocation section for section 'I'
465 bool HasRelA = TEW->hasRelocationAddend();
Bruno Cardoso Lopes45f5d642009-07-02 18:29:24 +0000466 ELFSection &RelSec = getRelocSection(I->getName(), HasRelA,
467 TEW->getPrefELFAlignment());
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000468
469 // 'Link' - Section hdr idx of the associated symbol table
470 // 'Info' - Section hdr idx of the section to which the relocation applies
471 ELFSection &SymTab = getSymbolTableSection();
472 RelSec.Link = SymTab.SectionIdx;
473 RelSec.Info = I->SectionIdx;
474 RelSec.EntSize = TEW->getRelocationEntrySize();
475
476 // Get the relocations from Section
477 std::vector<MachineRelocation> Relos = I->getRelocations();
478 for (std::vector<MachineRelocation>::iterator MRI = Relos.begin(),
479 MRE = Relos.end(); MRI != MRE; ++MRI) {
480 MachineRelocation &MR = *MRI;
481
482 // Offset from the start of the section containing the symbol
483 unsigned Offset = MR.getMachineCodeOffset();
484
485 // Symbol index in the symbol table
486 unsigned SymIdx = 0;
487
488 // Target specific ELF relocation type
489 unsigned RelType = TEW->getRelocationType(MR.getRelocationType());
490
491 // Constant addend used to compute the value to be stored
492 // into the relocatable field
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000493 int64_t Addend = 0;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000494
495 // There are several machine relocations types, and each one of
496 // them needs a different approach to retrieve the symbol table index.
497 if (MR.isGlobalValue()) {
498 const GlobalValue *G = MR.getGlobalValue();
499 SymIdx = GblSymLookup[G];
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000500 Addend = TEW->getAddendForRelTy(RelType);
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000501 } else {
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000502 unsigned SectionIdx = MR.getConstantVal();
503 // TODO: use a map for this.
504 for (std::list<ELFSym>::iterator I = SymbolList.begin(),
505 E = SymbolList.end(); I != E; ++I)
506 if ((SectionIdx == I->SectionIdx) &&
507 (I->getType() == ELFSym::STT_SECTION)) {
508 SymIdx = I->SymTabIdx;
509 break;
510 }
511 Addend = (uint64_t)MR.getResultPointer();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000512 }
513
514 // Get the relocation entry and emit to the relocation section
515 ELFRelocation Rel(Offset, SymIdx, RelType, HasRelA, Addend);
516 EmitRelocation(RelSec, Rel, HasRelA);
517 }
518 }
519}
520
521/// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel'
522void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel,
523 bool HasRelA) {
524 RelSec.emitWord(Rel.getOffset());
525 RelSec.emitWord(Rel.getInfo(is64Bit));
526 if (HasRelA)
527 RelSec.emitWord(Rel.getAddend());
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000528}
529
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000530/// EmitSymbol - Write symbol 'Sym' to the symbol table 'SymbolTable'
531void ELFWriter::EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000532 if (is64Bit) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000533 SymbolTable.emitWord32(Sym.NameIdx);
534 SymbolTable.emitByte(Sym.Info);
535 SymbolTable.emitByte(Sym.Other);
536 SymbolTable.emitWord16(Sym.SectionIdx);
537 SymbolTable.emitWord64(Sym.Value);
538 SymbolTable.emitWord64(Sym.Size);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000539 } else {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000540 SymbolTable.emitWord32(Sym.NameIdx);
541 SymbolTable.emitWord32(Sym.Value);
542 SymbolTable.emitWord32(Sym.Size);
543 SymbolTable.emitByte(Sym.Info);
544 SymbolTable.emitByte(Sym.Other);
545 SymbolTable.emitWord16(Sym.SectionIdx);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000546 }
547}
548
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000549/// EmitSectionHeader - Write section 'Section' header in 'SHdrTab'
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000550/// Section Header Table
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000551void ELFWriter::EmitSectionHeader(BinaryObject &SHdrTab,
552 const ELFSection &SHdr) {
553 SHdrTab.emitWord32(SHdr.NameIdx);
554 SHdrTab.emitWord32(SHdr.Type);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000555 if (is64Bit) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000556 SHdrTab.emitWord64(SHdr.Flags);
557 SHdrTab.emitWord(SHdr.Addr);
558 SHdrTab.emitWord(SHdr.Offset);
559 SHdrTab.emitWord64(SHdr.Size);
560 SHdrTab.emitWord32(SHdr.Link);
561 SHdrTab.emitWord32(SHdr.Info);
562 SHdrTab.emitWord64(SHdr.Align);
563 SHdrTab.emitWord64(SHdr.EntSize);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000564 } else {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000565 SHdrTab.emitWord32(SHdr.Flags);
566 SHdrTab.emitWord(SHdr.Addr);
567 SHdrTab.emitWord(SHdr.Offset);
568 SHdrTab.emitWord32(SHdr.Size);
569 SHdrTab.emitWord32(SHdr.Link);
570 SHdrTab.emitWord32(SHdr.Info);
571 SHdrTab.emitWord32(SHdr.Align);
572 SHdrTab.emitWord32(SHdr.EntSize);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000573 }
574}
575
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000576/// EmitStringTable - If the current symbol table is non-empty, emit the string
577/// table for it
578void ELFWriter::EmitStringTable() {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000579 if (!SymbolList.size()) return; // Empty symbol table.
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000580 ELFSection &StrTab = getStringTableSection();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000581
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000582 // Set the zero'th symbol to a null byte, as required.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000583 StrTab.emitByte(0);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000584
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000585 // Walk on the symbol list and write symbol names into the
586 // string table.
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000587 unsigned Index = 1;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000588 for (std::list<ELFSym>::iterator I = SymbolList.begin(),
589 E = SymbolList.end(); I != E; ++I) {
590
Chris Lattner5acd1202005-07-11 03:11:47 +0000591 // Use the name mangler to uniquify the LLVM symbol.
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000592 std::string Name;
593 if (I->GV) Name.append(Mang->getValueName(I->GV));
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000594
595 if (Name.empty()) {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000596 I->NameIdx = 0;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000597 } else {
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000598 I->NameIdx = Index;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000599 StrTab.emitString(Name);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000600
601 // Keep track of the number of bytes emitted to this section.
602 Index += Name.size()+1;
603 }
604 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000605 assert(Index == StrTab.size());
Chris Lattner5f48ff72005-07-16 08:01:13 +0000606 StrTab.Size = Index;
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000607}
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000608
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000609/// EmitSymbolTable - Emit the symbol table itself.
610void ELFWriter::EmitSymbolTable() {
611 if (!SymbolList.size()) return; // Empty symbol table.
612
613 unsigned FirstNonLocalSymbol = 1;
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000614 // Now that we have emitted the string table and know the offset into the
615 // string table of each symbol, emit the symbol table itself.
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000616 ELFSection &SymTab = getSymbolTableSection();
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000617 SymTab.Align = TEW->getPrefELFAlignment();
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000618
619 // Section Index of .strtab.
620 SymTab.Link = getStringTableSection().SectionIdx;
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000621
622 // Size of each symtab entry.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000623 SymTab.EntSize = TEW->getSymTabEntrySize();
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000624
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000625 // The first entry in the symtab is the null symbol
626 ELFSym NullSym = ELFSym(0);
627 EmitSymbol(SymTab, NullSym);
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000628
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000629 // Emit all the symbols to the symbol table. Skip the null
630 // symbol, cause it's emitted already
Bruno Cardoso Lopesc236a342009-06-22 19:29:56 +0000631 unsigned Index = 1;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000632 for (std::list<ELFSym>::iterator I = SymbolList.begin(),
633 E = SymbolList.end(); I != E; ++I, ++Index) {
634 // Keep track of the first non-local symbol
635 if (I->getBind() == ELFSym::STB_LOCAL)
636 FirstNonLocalSymbol++;
637
638 // Emit symbol to the symbol table
639 EmitSymbol(SymTab, *I);
640
641 // Record the symbol table index for each global value
Bruno Cardoso Lopesa5e0abd2009-06-25 07:36:24 +0000642 if (I->GV)
643 GblSymLookup[I->GV] = Index;
644
645 // Keep track on the symbol index into the symbol table
646 I->SymTabIdx = Index;
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000647 }
648
649 SymTab.Info = FirstNonLocalSymbol;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000650 SymTab.Size = SymTab.size();
Chris Lattner80ed8fa2005-07-07 07:02:20 +0000651}
652
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000653/// EmitSectionTableStringTable - This method adds and emits a section for the
654/// ELF Section Table string table: the string table that holds all of the
655/// section names.
656void ELFWriter::EmitSectionTableStringTable() {
657 // First step: add the section for the string table to the list of sections:
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000658 ELFSection &SHStrTab = getSectionHeaderStringTableSection();
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000659
660 // Now that we know which section number is the .shstrtab section, update the
661 // e_shstrndx entry in the ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000662 ElfHdr.fixWord16(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset);
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000663
664 // Set the NameIdx of each section in the string table and emit the bytes for
665 // the string table.
666 unsigned Index = 0;
667
Chris Lattner5f48ff72005-07-16 08:01:13 +0000668 for (std::list<ELFSection>::iterator I = SectionList.begin(),
669 E = SectionList.end(); I != E; ++I) {
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000670 // Set the index into the table. Note if we have lots of entries with
671 // common suffixes, we could memoize them here if we cared.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000672 I->NameIdx = Index;
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000673 SHStrTab.emitString(I->getName());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000674
675 // Keep track of the number of bytes emitted to this section.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000676 Index += I->getName().size()+1;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000677 }
678
679 // Set the size of .shstrtab now that we know what it is.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000680 assert(Index == SHStrTab.size());
Chris Lattner5f48ff72005-07-16 08:01:13 +0000681 SHStrTab.Size = Index;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000682}
683
Chris Lattner5f48ff72005-07-16 08:01:13 +0000684/// OutputSectionsAndSectionTable - Now that we have constructed the file header
685/// and all of the sections, emit these to the ostream destination and emit the
686/// SectionTable.
687void ELFWriter::OutputSectionsAndSectionTable() {
688 // Pass #1: Compute the file offset for each section.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000689 size_t FileOff = ElfHdr.size(); // File header first.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000690
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000691 // Adjust alignment of all section if needed.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000692 for (std::list<ELFSection>::iterator I = SectionList.begin(),
693 E = SectionList.end(); I != E; ++I) {
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000694
695 // Section idx 0 has 0 offset
696 if (!I->SectionIdx)
697 continue;
698
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000699 if (!I->size()) {
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000700 I->Offset = FileOff;
701 continue;
702 }
703
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000704 // Update Section size
705 if (!I->Size)
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000706 I->Size = I->size();
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000707
Chris Lattner5f48ff72005-07-16 08:01:13 +0000708 // Align FileOff to whatever the alignment restrictions of the section are.
709 if (I->Align)
710 FileOff = (FileOff+I->Align-1) & ~(I->Align-1);
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000711
Chris Lattner5f48ff72005-07-16 08:01:13 +0000712 I->Offset = FileOff;
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000713 FileOff += I->Size;
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000714 }
715
Chris Lattner5f48ff72005-07-16 08:01:13 +0000716 // Align Section Header.
Bruno Cardoso Lopes0d3193e2009-06-22 19:16:16 +0000717 unsigned TableAlign = TEW->getPrefELFAlignment();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000718 FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
719
720 // Now that we know where all of the sections will be emitted, set the e_shnum
721 // entry in the ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000722 ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000723
Chris Lattner5f48ff72005-07-16 08:01:13 +0000724 // Now that we know the offset in the file of the section table, update the
725 // e_shoff address in the ELF header.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000726 ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset);
Jeff Cohen00b168892005-07-27 06:12:32 +0000727
Chris Lattner5f48ff72005-07-16 08:01:13 +0000728 // Now that we know all of the data in the file header, emit it and all of the
729 // sections!
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000730 O.write((char *)&ElfHdr.getData()[0], ElfHdr.size());
731 FileOff = ElfHdr.size();
Chris Lattner5f48ff72005-07-16 08:01:13 +0000732
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000733 // Section Header Table blob
734 BinaryObject SHdrTable(isLittleEndian, is64Bit);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000735
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000736 // Emit all of sections to the file and build the section header table.
Chris Lattner5f48ff72005-07-16 08:01:13 +0000737 while (!SectionList.empty()) {
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000738 ELFSection &S = *SectionList.begin();
739 DOUT << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName()
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000740 << ", Size: " << S.Size << ", Offset: " << S.Offset
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000741 << ", SectionData Size: " << S.size() << "\n";
Chris Lattner5f48ff72005-07-16 08:01:13 +0000742
743 // Align FileOff to whatever the alignment restrictions of the section are.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000744 if (S.size()) {
Bruno Cardoso Lopese39493e2009-06-23 04:39:27 +0000745 if (S.Align) {
746 for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
747 FileOff != NewFileOff; ++FileOff)
748 O << (char)0xAB;
749 }
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000750 O.write((char *)&S.getData()[0], S.Size);
Bruno Cardoso Lopesc997d452009-06-11 19:16:03 +0000751 FileOff += S.Size;
752 }
Bruno Cardoso Lopesa029a272009-06-07 21:22:38 +0000753
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000754 EmitSectionHeader(SHdrTable, S);
Chris Lattner5f48ff72005-07-16 08:01:13 +0000755 SectionList.pop_front();
756 }
757
758 // Align output for the section table.
759 for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
760 FileOff != NewFileOff; ++FileOff)
Owen Andersoncb371882008-08-21 00:14:44 +0000761 O << (char)0xAB;
Jeff Cohen00b168892005-07-27 06:12:32 +0000762
Chris Lattner5f48ff72005-07-16 08:01:13 +0000763 // Emit the section table itself.
Bruno Cardoso Lopesae9163f2009-06-14 07:53:21 +0000764 O.write((char *)&SHdrTable.getData()[0], SHdrTable.size());
Chris Lattner35f0a4f2005-06-27 06:29:00 +0000765}