]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/lib/CodeGen/ELFWriter.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / lib / CodeGen / ELFWriter.cpp
1 //===-- ELFWriter.cpp - Target-independent ELF Writer code ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the target-independent ELF writer.  This file writes out
11 // the ELF file in the following order:
12 //
13 //  #1. ELF Header
14 //  #2. '.text' section
15 //  #3. '.data' section
16 //  #4. '.bss' section  (conceptual position in file)
17 //  ...
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]
23 //  #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 ]
26 //  ...
27 //  #N. ".shstrtab" entry - String table for the section names.
28 //
29 //===----------------------------------------------------------------------===//
30
31 #define DEBUG_TYPE "elfwriter"
32 #include "ELF.h"
33 #include "ELFWriter.h"
34 #include "ELFCodeEmitter.h"
35 #include "llvm/Constants.h"
36 #include "llvm/Module.h"
37 #include "llvm/PassManager.h"
38 #include "llvm/DerivedTypes.h"
39 #include "llvm/CodeGen/BinaryObject.h"
40 #include "llvm/CodeGen/MachineCodeEmitter.h"
41 #include "llvm/CodeGen/ObjectCodeEmitter.h"
42 #include "llvm/CodeGen/MachineCodeEmitter.h"
43 #include "llvm/CodeGen/MachineConstantPool.h"
44 #include "llvm/MC/MCContext.h"
45 #include "llvm/MC/MCSectionELF.h"
46 #include "llvm/MC/MCAsmInfo.h"
47 #include "llvm/Target/Mangler.h"
48 #include "llvm/Target/TargetData.h"
49 #include "llvm/Target/TargetELFWriterInfo.h"
50 #include "llvm/Target/TargetLowering.h"
51 #include "llvm/Target/TargetLoweringObjectFile.h"
52 #include "llvm/Target/TargetMachine.h"
53 #include "llvm/Target/TargetRegisterInfo.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/ErrorHandling.h"
56 #include "llvm/Support/raw_ostream.h"
57 #include "llvm/ADT/SmallString.h"
58 using namespace llvm;
59
60 char ELFWriter::ID = 0;
61
62 //===----------------------------------------------------------------------===//
63 //                          ELFWriter Implementation
64 //===----------------------------------------------------------------------===//
65
66 ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm)
67   : MachineFunctionPass(ID), O(o), TM(tm),
68     OutContext(*new MCContext(*TM.getMCAsmInfo(), *TM.getRegisterInfo(),
69                               &TM.getTargetLowering()->getObjFileLowering())),
70     TLOF(TM.getTargetLowering()->getObjFileLowering()),
71     is64Bit(TM.getTargetData()->getPointerSizeInBits() == 64),
72     isLittleEndian(TM.getTargetData()->isLittleEndian()),
73     ElfHdr(isLittleEndian, is64Bit) {
74
75   MAI = TM.getMCAsmInfo();
76   TEW = TM.getELFWriterInfo();
77
78   // Create the object code emitter object for this target.
79   ElfCE = new ELFCodeEmitter(*this);
80
81   // Initial number of sections
82   NumSections = 0;
83 }
84
85 ELFWriter::~ELFWriter() {
86   delete ElfCE;
87   delete &OutContext;
88
89   while(!SymbolList.empty()) {
90     delete SymbolList.back(); 
91     SymbolList.pop_back();
92   }
93
94   while(!PrivateSyms.empty()) {
95     delete PrivateSyms.back(); 
96     PrivateSyms.pop_back();
97   }
98
99   while(!SectionList.empty()) {
100     delete SectionList.back(); 
101     SectionList.pop_back();
102   }
103
104   // Release the name mangler object.
105   delete Mang; Mang = 0;
106 }
107
108 // doInitialization - Emit the file header and all of the global variables for
109 // the module to the ELF file.
110 bool ELFWriter::doInitialization(Module &M) {
111   // Initialize TargetLoweringObjectFile.
112   const_cast<TargetLoweringObjectFile&>(TLOF).Initialize(OutContext, TM);
113   
114   Mang = new Mangler(OutContext, *TM.getTargetData());
115
116   // ELF Header
117   // ----------
118   // Fields e_shnum e_shstrndx are only known after all section have
119   // been emitted. They locations in the ouput buffer are recorded so
120   // to be patched up later.
121   //
122   // Note
123   // ----
124   // emitWord method behaves differently for ELF32 and ELF64, writing
125   // 4 bytes in the former and 8 in the last for *_off and *_addr elf types
126
127   ElfHdr.emitByte(0x7f); // e_ident[EI_MAG0]
128   ElfHdr.emitByte('E');  // e_ident[EI_MAG1]
129   ElfHdr.emitByte('L');  // e_ident[EI_MAG2]
130   ElfHdr.emitByte('F');  // e_ident[EI_MAG3]
131
132   ElfHdr.emitByte(TEW->getEIClass()); // e_ident[EI_CLASS]
133   ElfHdr.emitByte(TEW->getEIData());  // e_ident[EI_DATA]
134   ElfHdr.emitByte(ELF::EV_CURRENT);   // e_ident[EI_VERSION]
135   ElfHdr.emitAlignment(16);           // e_ident[EI_NIDENT-EI_PAD]
136
137   ElfHdr.emitWord16(ELF::ET_REL);        // e_type
138   ElfHdr.emitWord16(TEW->getEMachine()); // e_machine = target
139   ElfHdr.emitWord32(ELF::EV_CURRENT);    // e_version
140   ElfHdr.emitWord(0);                    // e_entry, no entry point in .o file
141   ElfHdr.emitWord(0);                    // e_phoff, no program header for .o
142   ELFHdr_e_shoff_Offset = ElfHdr.size();
143   ElfHdr.emitWord(0);                    // e_shoff = sec hdr table off in bytes
144   ElfHdr.emitWord32(TEW->getEFlags());   // e_flags = whatever the target wants
145   ElfHdr.emitWord16(TEW->getHdrSize());  // e_ehsize = ELF header size
146   ElfHdr.emitWord16(0);                  // e_phentsize = prog header entry size
147   ElfHdr.emitWord16(0);                  // e_phnum = # prog header entries = 0
148
149   // e_shentsize = Section header entry size
150   ElfHdr.emitWord16(TEW->getSHdrSize());
151
152   // e_shnum     = # of section header ents
153   ELFHdr_e_shnum_Offset = ElfHdr.size();
154   ElfHdr.emitWord16(0); // Placeholder
155
156   // e_shstrndx  = Section # of '.shstrtab'
157   ELFHdr_e_shstrndx_Offset = ElfHdr.size();
158   ElfHdr.emitWord16(0); // Placeholder
159
160   // Add the null section, which is required to be first in the file.
161   getNullSection();
162
163   // The first entry in the symtab is the null symbol and the second
164   // is a local symbol containing the module/file name
165   SymbolList.push_back(new ELFSym());
166   SymbolList.push_back(ELFSym::getFileSym());
167
168   return false;
169 }
170
171 // AddPendingGlobalSymbol - Add a global to be processed and to
172 // the global symbol lookup, use a zero index because the table
173 // index will be determined later.
174 void ELFWriter::AddPendingGlobalSymbol(const GlobalValue *GV, 
175                                        bool AddToLookup /* = false */) {
176   PendingGlobals.insert(GV);
177   if (AddToLookup) 
178     GblSymLookup[GV] = 0;
179 }
180
181 // AddPendingExternalSymbol - Add the external to be processed
182 // and to the external symbol lookup, use a zero index because
183 // the symbol table index will be determined later.
184 void ELFWriter::AddPendingExternalSymbol(const char *External) {
185   PendingExternals.insert(External);
186   ExtSymLookup[External] = 0;
187 }
188
189 ELFSection &ELFWriter::getDataSection() {
190   const MCSectionELF *Data = (const MCSectionELF *)TLOF.getDataSection();
191   return getSection(Data->getSectionName(), Data->getType(), 
192                     Data->getFlags(), 4);
193 }
194
195 ELFSection &ELFWriter::getBSSSection() {
196   const MCSectionELF *BSS = (const MCSectionELF *)TLOF.getBSSSection();
197   return getSection(BSS->getSectionName(), BSS->getType(), BSS->getFlags(), 4);
198 }
199
200 // getCtorSection - Get the static constructor section
201 ELFSection &ELFWriter::getCtorSection() {
202   const MCSectionELF *Ctor = (const MCSectionELF *)TLOF.getStaticCtorSection();
203   return getSection(Ctor->getSectionName(), Ctor->getType(), Ctor->getFlags()); 
204 }
205
206 // getDtorSection - Get the static destructor section
207 ELFSection &ELFWriter::getDtorSection() {
208   const MCSectionELF *Dtor = (const MCSectionELF *)TLOF.getStaticDtorSection();
209   return getSection(Dtor->getSectionName(), Dtor->getType(), Dtor->getFlags());
210 }
211
212 // getTextSection - Get the text section for the specified function
213 ELFSection &ELFWriter::getTextSection(const Function *F) {
214   const MCSectionELF *Text = 
215     (const MCSectionELF *)TLOF.SectionForGlobal(F, Mang, TM);
216   return getSection(Text->getSectionName(), Text->getType(), Text->getFlags());
217 }
218
219 // getJumpTableSection - Get a read only section for constants when 
220 // emitting jump tables. TODO: add PIC support
221 ELFSection &ELFWriter::getJumpTableSection() {
222   const MCSectionELF *JT = 
223     (const MCSectionELF *)TLOF.getSectionForConstant(SectionKind::getReadOnly());
224   return getSection(JT->getSectionName(), JT->getType(), JT->getFlags(),
225                     TM.getTargetData()->getPointerABIAlignment());
226 }
227
228 // getConstantPoolSection - Get a constant pool section based on the machine 
229 // constant pool entry type and relocation info.
230 ELFSection &ELFWriter::getConstantPoolSection(MachineConstantPoolEntry &CPE) {
231   SectionKind Kind;
232   switch (CPE.getRelocationInfo()) {
233   default: llvm_unreachable("Unknown section kind");
234   case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
235   case 1:
236     Kind = SectionKind::getReadOnlyWithRelLocal();
237     break;
238   case 0:
239     switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
240     case 4:  Kind = SectionKind::getMergeableConst4(); break;
241     case 8:  Kind = SectionKind::getMergeableConst8(); break;
242     case 16: Kind = SectionKind::getMergeableConst16(); break;
243     default: Kind = SectionKind::getMergeableConst(); break;
244     }
245   }
246
247   const MCSectionELF *CPSect = 
248     (const MCSectionELF *)TLOF.getSectionForConstant(Kind);
249   return getSection(CPSect->getSectionName(), CPSect->getType(), 
250                     CPSect->getFlags(), CPE.getAlignment());
251 }
252
253 // getRelocSection - Return the relocation section of section 'S'. 'RelA' 
254 // is true if the relocation section contains entries with addends.
255 ELFSection &ELFWriter::getRelocSection(ELFSection &S) {
256   unsigned SectionType = TEW->hasRelocationAddend() ?
257                 ELF::SHT_RELA : ELF::SHT_REL;
258
259   std::string SectionName(".rel");
260   if (TEW->hasRelocationAddend())
261     SectionName.append("a");
262   SectionName.append(S.getName());
263
264   return getSection(SectionName, SectionType, 0, TEW->getPrefELFAlignment());
265 }
266
267 // getGlobalELFVisibility - Returns the ELF specific visibility type
268 unsigned ELFWriter::getGlobalELFVisibility(const GlobalValue *GV) {
269   switch (GV->getVisibility()) {
270   default:
271     llvm_unreachable("unknown visibility type");
272   case GlobalValue::DefaultVisibility:
273     return ELF::STV_DEFAULT;
274   case GlobalValue::HiddenVisibility:
275     return ELF::STV_HIDDEN;
276   case GlobalValue::ProtectedVisibility:
277     return ELF::STV_PROTECTED;
278   }
279   return 0;
280 }
281
282 // getGlobalELFBinding - Returns the ELF specific binding type
283 unsigned ELFWriter::getGlobalELFBinding(const GlobalValue *GV) {
284   if (GV->hasInternalLinkage())
285     return ELF::STB_LOCAL;
286
287   if (GV->isWeakForLinker() && !GV->hasCommonLinkage())
288     return ELF::STB_WEAK;
289
290   return ELF::STB_GLOBAL;
291 }
292
293 // getGlobalELFType - Returns the ELF specific type for a global
294 unsigned ELFWriter::getGlobalELFType(const GlobalValue *GV) {
295   if (GV->isDeclaration())
296     return ELF::STT_NOTYPE;
297
298   if (isa<Function>(GV))
299     return ELF::STT_FUNC;
300
301   return ELF::STT_OBJECT;
302 }
303
304 // IsELFUndefSym - True if the global value must be marked as a symbol
305 // which points to a SHN_UNDEF section. This means that the symbol has
306 // no definition on the module.
307 static bool IsELFUndefSym(const GlobalValue *GV) {
308   return GV->isDeclaration() || (isa<Function>(GV));
309 }
310
311 // AddToSymbolList - Update the symbol lookup and If the symbol is 
312 // private add it to PrivateSyms list, otherwise to SymbolList. 
313 void ELFWriter::AddToSymbolList(ELFSym *GblSym) {
314   assert(GblSym->isGlobalValue() && "Symbol must be a global value");
315
316   const GlobalValue *GV = GblSym->getGlobalValue(); 
317   if (GV->hasPrivateLinkage()) {
318     // For a private symbols, keep track of the index inside 
319     // the private list since it will never go to the symbol 
320     // table and won't be patched up later.
321     PrivateSyms.push_back(GblSym);
322     GblSymLookup[GV] = PrivateSyms.size()-1;
323   } else {
324     // Non private symbol are left with zero indices until 
325     // they are patched up during the symbol table emition 
326     // (where the indicies are created).
327     SymbolList.push_back(GblSym);
328     GblSymLookup[GV] = 0;
329   }
330 }
331
332 /// HasCommonSymbols - True if this section holds common symbols, this is
333 /// indicated on the ELF object file by a symbol with SHN_COMMON section
334 /// header index.
335 static bool HasCommonSymbols(const MCSectionELF &S) {
336   // FIXME: this is wrong, a common symbol can be in .data for example.
337   if (StringRef(S.getSectionName()).startswith(".gnu.linkonce."))
338     return true;
339
340   return false;
341 }
342
343
344 // EmitGlobal - Choose the right section for global and emit it
345 void ELFWriter::EmitGlobal(const GlobalValue *GV) {
346
347   // Check if the referenced symbol is already emitted
348   if (GblSymLookup.find(GV) != GblSymLookup.end())
349     return;
350
351   // Handle ELF Bind, Visibility and Type for the current symbol
352   unsigned SymBind = getGlobalELFBinding(GV);
353   unsigned SymType = getGlobalELFType(GV);
354   bool IsUndefSym = IsELFUndefSym(GV);
355
356   ELFSym *GblSym = IsUndefSym ? ELFSym::getUndefGV(GV, SymBind)
357     : ELFSym::getGV(GV, SymBind, SymType, getGlobalELFVisibility(GV));
358
359   if (!IsUndefSym) {
360     assert(isa<GlobalVariable>(GV) && "GV not a global variable!");
361     const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
362
363     // Handle special llvm globals
364     if (EmitSpecialLLVMGlobal(GVar))
365       return;
366
367     // Get the ELF section where this global belongs from TLOF
368     const MCSectionELF *S = 
369       (const MCSectionELF *)TLOF.SectionForGlobal(GV, Mang, TM);
370     ELFSection &ES = 
371       getSection(S->getSectionName(), S->getType(), S->getFlags());
372     SectionKind Kind = S->getKind();
373
374     // The symbol align should update the section alignment if needed
375     const TargetData *TD = TM.getTargetData();
376     unsigned Align = TD->getPreferredAlignment(GVar);
377     unsigned Size = TD->getTypeAllocSize(GVar->getInitializer()->getType());
378     GblSym->Size = Size;
379
380     if (HasCommonSymbols(*S)) { // Symbol must go to a common section
381       GblSym->SectionIdx = ELF::SHN_COMMON;
382
383       // A new linkonce section is created for each global in the
384       // common section, the default alignment is 1 and the symbol
385       // value contains its alignment.
386       ES.Align = 1;
387       GblSym->Value = Align;
388
389     } else if (Kind.isBSS() || Kind.isThreadBSS()) { // Symbol goes to BSS.
390       GblSym->SectionIdx = ES.SectionIdx;
391
392       // Update the size with alignment and the next object can
393       // start in the right offset in the section
394       if (Align) ES.Size = (ES.Size + Align-1) & ~(Align-1);
395       ES.Align = std::max(ES.Align, Align);
396
397       // GblSym->Value should contain the virtual offset inside the section.
398       // Virtual because the BSS space is not allocated on ELF objects
399       GblSym->Value = ES.Size;
400       ES.Size += Size;
401
402     } else { // The symbol must go to some kind of data section
403       GblSym->SectionIdx = ES.SectionIdx;
404
405       // GblSym->Value should contain the symbol offset inside the section,
406       // and all symbols should start on their required alignment boundary
407       ES.Align = std::max(ES.Align, Align);
408       ES.emitAlignment(Align);
409       GblSym->Value = ES.size();
410
411       // Emit the global to the data section 'ES'
412       EmitGlobalConstant(GVar->getInitializer(), ES);
413     }
414   }
415
416   AddToSymbolList(GblSym);
417 }
418
419 void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
420                                          ELFSection &GblS) {
421
422   // Print the fields in successive locations. Pad to align if needed!
423   const TargetData *TD = TM.getTargetData();
424   unsigned Size = TD->getTypeAllocSize(CVS->getType());
425   const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
426   uint64_t sizeSoFar = 0;
427   for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
428     const Constant* field = CVS->getOperand(i);
429
430     // Check if padding is needed and insert one or more 0s.
431     uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
432     uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
433                         - cvsLayout->getElementOffset(i)) - fieldSize;
434     sizeSoFar += fieldSize + padSize;
435
436     // Now print the actual field value.
437     EmitGlobalConstant(field, GblS);
438
439     // Insert padding - this may include padding to increase the size of the
440     // current field up to the ABI size (if the struct is not packed) as well
441     // as padding to ensure that the next field starts at the right offset.
442     GblS.emitZeros(padSize);
443   }
444   assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
445          "Layout of constant struct may be incorrect!");
446 }
447
448 void ELFWriter::EmitGlobalConstant(const Constant *CV, ELFSection &GblS) {
449   const TargetData *TD = TM.getTargetData();
450   unsigned Size = TD->getTypeAllocSize(CV->getType());
451
452   if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
453     for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
454       EmitGlobalConstant(CVA->getOperand(i), GblS);
455     return;
456   } else if (isa<ConstantAggregateZero>(CV)) {
457     GblS.emitZeros(Size);
458     return;
459   } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
460     EmitGlobalConstantStruct(CVS, GblS);
461     return;
462   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
463     APInt Val = CFP->getValueAPF().bitcastToAPInt();
464     if (CFP->getType()->isDoubleTy())
465       GblS.emitWord64(Val.getZExtValue());
466     else if (CFP->getType()->isFloatTy())
467       GblS.emitWord32(Val.getZExtValue());
468     else if (CFP->getType()->isX86_FP80Ty()) {
469       unsigned PadSize = TD->getTypeAllocSize(CFP->getType())-
470                          TD->getTypeStoreSize(CFP->getType());
471       GblS.emitWordFP80(Val.getRawData(), PadSize);
472     } else if (CFP->getType()->isPPC_FP128Ty())
473       llvm_unreachable("PPC_FP128Ty global emission not implemented");
474     return;
475   } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
476     if (Size == 1)
477       GblS.emitByte(CI->getZExtValue());
478     else if (Size == 2) 
479       GblS.emitWord16(CI->getZExtValue());
480     else if (Size == 4)
481       GblS.emitWord32(CI->getZExtValue());
482     else 
483       EmitGlobalConstantLargeInt(CI, GblS);
484     return;
485   } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
486     VectorType *PTy = CP->getType();
487     for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
488       EmitGlobalConstant(CP->getOperand(I), GblS);
489     return;
490   } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
491     // Resolve a constant expression which returns a (Constant, Offset)
492     // pair. If 'Res.first' is a GlobalValue, emit a relocation with 
493     // the offset 'Res.second', otherwise emit a global constant like
494     // it is always done for not contant expression types.
495     CstExprResTy Res = ResolveConstantExpr(CE);
496     const Constant *Op = Res.first;
497
498     if (isa<GlobalValue>(Op))
499       EmitGlobalDataRelocation(cast<const GlobalValue>(Op), 
500                                TD->getTypeAllocSize(Op->getType()), 
501                                GblS, Res.second);
502     else
503       EmitGlobalConstant(Op, GblS);
504
505     return;
506   } else if (CV->getType()->getTypeID() == Type::PointerTyID) {
507     // Fill the data entry with zeros or emit a relocation entry
508     if (isa<ConstantPointerNull>(CV))
509       GblS.emitZeros(Size);
510     else 
511       EmitGlobalDataRelocation(cast<const GlobalValue>(CV), 
512                                Size, GblS);
513     return;
514   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
515     // This is a constant address for a global variable or function and
516     // therefore must be referenced using a relocation entry.
517     EmitGlobalDataRelocation(GV, Size, GblS);
518     return;
519   }
520
521   std::string msg;
522   raw_string_ostream ErrorMsg(msg);
523   ErrorMsg << "Constant unimp for type: " << *CV->getType();
524   report_fatal_error(ErrorMsg.str());
525 }
526
527 // ResolveConstantExpr - Resolve the constant expression until it stop
528 // yielding other constant expressions.
529 CstExprResTy ELFWriter::ResolveConstantExpr(const Constant *CV) {
530   const TargetData *TD = TM.getTargetData();
531   
532   // There ins't constant expression inside others anymore
533   if (!isa<ConstantExpr>(CV))
534     return std::make_pair(CV, 0);
535
536   const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
537   switch (CE->getOpcode()) {
538   case Instruction::BitCast:
539     return ResolveConstantExpr(CE->getOperand(0));
540   
541   case Instruction::GetElementPtr: {
542     const Constant *ptrVal = CE->getOperand(0);
543     SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
544     int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec);
545     return std::make_pair(ptrVal, Offset);
546   }
547   case Instruction::IntToPtr: {
548     Constant *Op = CE->getOperand(0);
549     Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
550                                       false/*ZExt*/);
551     return ResolveConstantExpr(Op);
552   }
553   case Instruction::PtrToInt: {
554     Constant *Op = CE->getOperand(0);
555     Type *Ty = CE->getType();
556
557     // We can emit the pointer value into this slot if the slot is an
558     // integer slot greater or equal to the size of the pointer.
559     if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
560       return ResolveConstantExpr(Op);
561
562     llvm_unreachable("Integer size less then pointer size");
563   }
564   case Instruction::Add:
565   case Instruction::Sub: {
566     // Only handle cases where there's a constant expression with GlobalValue
567     // as first operand and ConstantInt as second, which are the cases we can
568     // solve direclty using a relocation entry. GlobalValue=Op0, CstInt=Op1
569     // 1)  Instruction::Add  => (global) + CstInt
570     // 2)  Instruction::Sub  => (global) + -CstInt
571     const Constant *Op0 = CE->getOperand(0); 
572     const Constant *Op1 = CE->getOperand(1); 
573     assert(isa<ConstantInt>(Op1) && "Op1 must be a ConstantInt");
574
575     CstExprResTy Res = ResolveConstantExpr(Op0);
576     assert(isa<GlobalValue>(Res.first) && "Op0 must be a GlobalValue");
577
578     const APInt &RHS = cast<ConstantInt>(Op1)->getValue();
579     switch (CE->getOpcode()) {
580     case Instruction::Add: 
581       return std::make_pair(Res.first, RHS.getSExtValue());
582     case Instruction::Sub:
583       return std::make_pair(Res.first, (-RHS).getSExtValue());
584     }
585   }
586   }
587
588   report_fatal_error(CE->getOpcodeName() +
589                      StringRef(": Unsupported ConstantExpr type"));
590
591   return std::make_pair(CV, 0); // silence warning
592 }
593
594 void ELFWriter::EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size,
595                                          ELFSection &GblS, int64_t Offset) {
596   // Create the relocation entry for the global value
597   MachineRelocation MR =
598     MachineRelocation::getGV(GblS.getCurrentPCOffset(),
599                              TEW->getAbsoluteLabelMachineRelTy(),
600                              const_cast<GlobalValue*>(GV),
601                              Offset);
602
603   // Fill the data entry with zeros
604   GblS.emitZeros(Size);
605
606   // Add the relocation entry for the current data section
607   GblS.addRelocation(MR);
608 }
609
610 void ELFWriter::EmitGlobalConstantLargeInt(const ConstantInt *CI, 
611                                            ELFSection &S) {
612   const TargetData *TD = TM.getTargetData();
613   unsigned BitWidth = CI->getBitWidth();
614   assert(isPowerOf2_32(BitWidth) &&
615          "Non-power-of-2-sized integers not handled!");
616
617   const uint64_t *RawData = CI->getValue().getRawData();
618   uint64_t Val = 0;
619   for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
620     Val = (TD->isBigEndian()) ? RawData[e - i - 1] : RawData[i];
621     S.emitWord64(Val);
622   }
623 }
624
625 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a
626 /// special global used by LLVM.  If so, emit it and return true, otherwise
627 /// do nothing and return false.
628 bool ELFWriter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
629   if (GV->getName() == "llvm.used")
630     llvm_unreachable("not implemented yet");
631
632   // Ignore debug and non-emitted data.  This handles llvm.compiler.used.
633   if (GV->getSection() == "llvm.metadata" ||
634       GV->hasAvailableExternallyLinkage())
635     return true;
636   
637   if (!GV->hasAppendingLinkage()) return false;
638
639   assert(GV->hasInitializer() && "Not a special LLVM global!");
640   
641   const TargetData *TD = TM.getTargetData();
642   unsigned Align = TD->getPointerPrefAlignment();
643   if (GV->getName() == "llvm.global_ctors") {
644     ELFSection &Ctor = getCtorSection();
645     Ctor.emitAlignment(Align);
646     EmitXXStructorList(GV->getInitializer(), Ctor);
647     return true;
648   } 
649   
650   if (GV->getName() == "llvm.global_dtors") {
651     ELFSection &Dtor = getDtorSection();
652     Dtor.emitAlignment(Align);
653     EmitXXStructorList(GV->getInitializer(), Dtor);
654     return true;
655   }
656   
657   return false;
658 }
659
660 /// EmitXXStructorList - Emit the ctor or dtor list.  This just emits out the 
661 /// function pointers, ignoring the init priority.
662 void ELFWriter::EmitXXStructorList(const Constant *List, ELFSection &Xtor) {
663   // Should be an array of '{ i32, void ()* }' structs.  The first value is the
664   // init priority, which we ignore.
665   if (List->isNullValue()) return;
666   const ConstantArray *InitList = cast<ConstantArray>(List);
667   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
668     if (InitList->getOperand(i)->isNullValue())
669       continue;
670     ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(i));
671
672     if (CS->getOperand(1)->isNullValue())
673       continue;
674
675     // Emit the function pointer.
676     EmitGlobalConstant(CS->getOperand(1), Xtor);
677   }
678 }
679
680 bool ELFWriter::runOnMachineFunction(MachineFunction &MF) {
681   // Nothing to do here, this is all done through the ElfCE object above.
682   return false;
683 }
684
685 /// doFinalization - Now that the module has been completely processed, emit
686 /// the ELF file to 'O'.
687 bool ELFWriter::doFinalization(Module &M) {
688   // Emit .data section placeholder
689   getDataSection();
690
691   // Emit .bss section placeholder
692   getBSSSection();
693
694   // Build and emit data, bss and "common" sections.
695   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
696        I != E; ++I)
697     EmitGlobal(I);
698
699   // Emit all pending globals
700   for (PendingGblsIter I = PendingGlobals.begin(), E = PendingGlobals.end();
701        I != E; ++I)
702     EmitGlobal(*I);
703
704   // Emit all pending externals
705   for (PendingExtsIter I = PendingExternals.begin(), E = PendingExternals.end();
706        I != E; ++I)
707     SymbolList.push_back(ELFSym::getExtSym(*I));
708
709   // Emit a symbol for each section created until now, skip null section
710   for (unsigned i = 1, e = SectionList.size(); i < e; ++i) {
711     ELFSection &ES = *SectionList[i];
712     ELFSym *SectionSym = ELFSym::getSectionSym();
713     SectionSym->SectionIdx = ES.SectionIdx;
714     SymbolList.push_back(SectionSym);
715     ES.Sym = SymbolList.back();
716   }
717
718   // Emit string table
719   EmitStringTable(M.getModuleIdentifier());
720
721   // Emit the symbol table now, if non-empty.
722   EmitSymbolTable();
723
724   // Emit the relocation sections.
725   EmitRelocations();
726
727   // Emit the sections string table.
728   EmitSectionTableStringTable();
729
730   // Dump the sections and section table to the .o file.
731   OutputSectionsAndSectionTable();
732
733   return false;
734 }
735
736 // RelocateField - Patch relocatable field with 'Offset' in 'BO'
737 // using a 'Value' of known 'Size'
738 void ELFWriter::RelocateField(BinaryObject &BO, uint32_t Offset,
739                               int64_t Value, unsigned Size) {
740   if (Size == 32)
741     BO.fixWord32(Value, Offset);
742   else if (Size == 64)
743     BO.fixWord64(Value, Offset);
744   else
745     llvm_unreachable("don't know howto patch relocatable field");
746 }
747
748 /// EmitRelocations - Emit relocations
749 void ELFWriter::EmitRelocations() {
750
751   // True if the target uses the relocation entry to hold the addend,
752   // otherwise the addend is written directly to the relocatable field.
753   bool HasRelA = TEW->hasRelocationAddend();
754
755   // Create Relocation sections for each section which needs it.
756   for (unsigned i=0, e=SectionList.size(); i != e; ++i) {
757     ELFSection &S = *SectionList[i];
758
759     // This section does not have relocations
760     if (!S.hasRelocations()) continue;
761     ELFSection &RelSec = getRelocSection(S);
762
763     // 'Link' - Section hdr idx of the associated symbol table
764     // 'Info' - Section hdr idx of the section to which the relocation applies
765     ELFSection &SymTab = getSymbolTableSection();
766     RelSec.Link = SymTab.SectionIdx;
767     RelSec.Info = S.SectionIdx;
768     RelSec.EntSize = TEW->getRelocationEntrySize();
769
770     // Get the relocations from Section
771     std::vector<MachineRelocation> Relos = S.getRelocations();
772     for (std::vector<MachineRelocation>::iterator MRI = Relos.begin(),
773          MRE = Relos.end(); MRI != MRE; ++MRI) {
774       MachineRelocation &MR = *MRI;
775
776       // Relocatable field offset from the section start
777       unsigned RelOffset = MR.getMachineCodeOffset();
778
779       // Symbol index in the symbol table
780       unsigned SymIdx = 0;
781
782       // Target specific relocation field type and size
783       unsigned RelType = TEW->getRelocationType(MR.getRelocationType());
784       unsigned RelTySize = TEW->getRelocationTySize(RelType);
785       int64_t Addend = 0;
786
787       // There are several machine relocations types, and each one of
788       // them needs a different approach to retrieve the symbol table index.
789       if (MR.isGlobalValue()) {
790         const GlobalValue *G = MR.getGlobalValue();
791         int64_t GlobalOffset = MR.getConstantVal();
792         SymIdx = GblSymLookup[G];
793         if (G->hasPrivateLinkage()) {
794           // If the target uses a section offset in the relocation:
795           // SymIdx + Addend = section sym for global + section offset
796           unsigned SectionIdx = PrivateSyms[SymIdx]->SectionIdx;
797           Addend = PrivateSyms[SymIdx]->Value + GlobalOffset;
798           SymIdx = SectionList[SectionIdx]->getSymbolTableIndex();
799         } else {
800           Addend = TEW->getDefaultAddendForRelTy(RelType, GlobalOffset);
801         }
802       } else if (MR.isExternalSymbol()) {
803         const char *ExtSym = MR.getExternalSymbol();
804         SymIdx = ExtSymLookup[ExtSym];
805         Addend = TEW->getDefaultAddendForRelTy(RelType);
806       } else {
807         // Get the symbol index for the section symbol
808         unsigned SectionIdx = MR.getConstantVal();
809         SymIdx = SectionList[SectionIdx]->getSymbolTableIndex();
810
811         // The symbol offset inside the section
812         int64_t SymOffset = (int64_t)MR.getResultPointer();
813
814         // For pc relative relocations where symbols are defined in the same
815         // section they are referenced, ignore the relocation entry and patch
816         // the relocatable field with the symbol offset directly.
817         if (S.SectionIdx == SectionIdx && TEW->isPCRelativeRel(RelType)) {
818           int64_t Value = TEW->computeRelocation(SymOffset, RelOffset, RelType);
819           RelocateField(S, RelOffset, Value, RelTySize);
820           continue;
821         }
822
823         Addend = TEW->getDefaultAddendForRelTy(RelType, SymOffset);
824       }
825
826       // The target without addend on the relocation symbol must be
827       // patched in the relocation place itself to contain the addend
828       // otherwise write zeros to make sure there is no garbage there
829       RelocateField(S, RelOffset, HasRelA ? 0 : Addend, RelTySize);
830
831       // Get the relocation entry and emit to the relocation section
832       ELFRelocation Rel(RelOffset, SymIdx, RelType, HasRelA, Addend);
833       EmitRelocation(RelSec, Rel, HasRelA);
834     }
835   }
836 }
837
838 /// EmitRelocation - Write relocation 'Rel' to the relocation section 'Rel'
839 void ELFWriter::EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel,
840                                bool HasRelA) {
841   RelSec.emitWord(Rel.getOffset());
842   RelSec.emitWord(Rel.getInfo(is64Bit));
843   if (HasRelA)
844     RelSec.emitWord(Rel.getAddend());
845 }
846
847 /// EmitSymbol - Write symbol 'Sym' to the symbol table 'SymbolTable'
848 void ELFWriter::EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym) {
849   if (is64Bit) {
850     SymbolTable.emitWord32(Sym.NameIdx);
851     SymbolTable.emitByte(Sym.Info);
852     SymbolTable.emitByte(Sym.Other);
853     SymbolTable.emitWord16(Sym.SectionIdx);
854     SymbolTable.emitWord64(Sym.Value);
855     SymbolTable.emitWord64(Sym.Size);
856   } else {
857     SymbolTable.emitWord32(Sym.NameIdx);
858     SymbolTable.emitWord32(Sym.Value);
859     SymbolTable.emitWord32(Sym.Size);
860     SymbolTable.emitByte(Sym.Info);
861     SymbolTable.emitByte(Sym.Other);
862     SymbolTable.emitWord16(Sym.SectionIdx);
863   }
864 }
865
866 /// EmitSectionHeader - Write section 'Section' header in 'SHdrTab'
867 /// Section Header Table
868 void ELFWriter::EmitSectionHeader(BinaryObject &SHdrTab,
869                                   const ELFSection &SHdr) {
870   SHdrTab.emitWord32(SHdr.NameIdx);
871   SHdrTab.emitWord32(SHdr.Type);
872   if (is64Bit) {
873     SHdrTab.emitWord64(SHdr.Flags);
874     SHdrTab.emitWord(SHdr.Addr);
875     SHdrTab.emitWord(SHdr.Offset);
876     SHdrTab.emitWord64(SHdr.Size);
877     SHdrTab.emitWord32(SHdr.Link);
878     SHdrTab.emitWord32(SHdr.Info);
879     SHdrTab.emitWord64(SHdr.Align);
880     SHdrTab.emitWord64(SHdr.EntSize);
881   } else {
882     SHdrTab.emitWord32(SHdr.Flags);
883     SHdrTab.emitWord(SHdr.Addr);
884     SHdrTab.emitWord(SHdr.Offset);
885     SHdrTab.emitWord32(SHdr.Size);
886     SHdrTab.emitWord32(SHdr.Link);
887     SHdrTab.emitWord32(SHdr.Info);
888     SHdrTab.emitWord32(SHdr.Align);
889     SHdrTab.emitWord32(SHdr.EntSize);
890   }
891 }
892
893 /// EmitStringTable - If the current symbol table is non-empty, emit the string
894 /// table for it
895 void ELFWriter::EmitStringTable(const std::string &ModuleName) {
896   if (!SymbolList.size()) return;  // Empty symbol table.
897   ELFSection &StrTab = getStringTableSection();
898
899   // Set the zero'th symbol to a null byte, as required.
900   StrTab.emitByte(0);
901
902   // Walk on the symbol list and write symbol names into the string table.
903   unsigned Index = 1;
904   for (ELFSymIter I=SymbolList.begin(), E=SymbolList.end(); I != E; ++I) {
905     ELFSym &Sym = *(*I);
906
907     std::string Name;
908     if (Sym.isGlobalValue()) {
909       SmallString<40> NameStr;
910       Mang->getNameWithPrefix(NameStr, Sym.getGlobalValue(), false);
911       Name.append(NameStr.begin(), NameStr.end());
912     } else if (Sym.isExternalSym())
913       Name.append(Sym.getExternalSymbol());
914     else if (Sym.isFileType())
915       Name.append(ModuleName);
916
917     if (Name.empty()) {
918       Sym.NameIdx = 0;
919     } else {
920       Sym.NameIdx = Index;
921       StrTab.emitString(Name);
922
923       // Keep track of the number of bytes emitted to this section.
924       Index += Name.size()+1;
925     }
926   }
927   assert(Index == StrTab.size());
928   StrTab.Size = Index;
929 }
930
931 // SortSymbols - On the symbol table local symbols must come before
932 // all other symbols with non-local bindings. The return value is
933 // the position of the first non local symbol.
934 unsigned ELFWriter::SortSymbols() {
935   unsigned FirstNonLocalSymbol;
936   std::vector<ELFSym*> LocalSyms, OtherSyms;
937
938   for (ELFSymIter I=SymbolList.begin(), E=SymbolList.end(); I != E; ++I) {
939     if ((*I)->isLocalBind())
940       LocalSyms.push_back(*I);
941     else
942       OtherSyms.push_back(*I);
943   }
944   SymbolList.clear();
945   FirstNonLocalSymbol = LocalSyms.size();
946
947   for (unsigned i = 0; i < FirstNonLocalSymbol; ++i)
948     SymbolList.push_back(LocalSyms[i]);
949
950   for (ELFSymIter I=OtherSyms.begin(), E=OtherSyms.end(); I != E; ++I)
951     SymbolList.push_back(*I);
952
953   LocalSyms.clear();
954   OtherSyms.clear();
955
956   return FirstNonLocalSymbol;
957 }
958
959 /// EmitSymbolTable - Emit the symbol table itself.
960 void ELFWriter::EmitSymbolTable() {
961   if (!SymbolList.size()) return;  // Empty symbol table.
962
963   // Now that we have emitted the string table and know the offset into the
964   // string table of each symbol, emit the symbol table itself.
965   ELFSection &SymTab = getSymbolTableSection();
966   SymTab.Align = TEW->getPrefELFAlignment();
967
968   // Section Index of .strtab.
969   SymTab.Link = getStringTableSection().SectionIdx;
970
971   // Size of each symtab entry.
972   SymTab.EntSize = TEW->getSymTabEntrySize();
973
974   // Reorder the symbol table with local symbols first!
975   unsigned FirstNonLocalSymbol = SortSymbols();
976
977   // Emit all the symbols to the symbol table.
978   for (unsigned i = 0, e = SymbolList.size(); i < e; ++i) {
979     ELFSym &Sym = *SymbolList[i];
980
981     // Emit symbol to the symbol table
982     EmitSymbol(SymTab, Sym);
983
984     // Record the symbol table index for each symbol
985     if (Sym.isGlobalValue())
986       GblSymLookup[Sym.getGlobalValue()] = i;
987     else if (Sym.isExternalSym())
988       ExtSymLookup[Sym.getExternalSymbol()] = i;
989
990     // Keep track on the symbol index into the symbol table
991     Sym.SymTabIdx = i;
992   }
993
994   // One greater than the symbol table index of the last local symbol
995   SymTab.Info = FirstNonLocalSymbol;
996   SymTab.Size = SymTab.size();
997 }
998
999 /// EmitSectionTableStringTable - This method adds and emits a section for the
1000 /// ELF Section Table string table: the string table that holds all of the
1001 /// section names.
1002 void ELFWriter::EmitSectionTableStringTable() {
1003   // First step: add the section for the string table to the list of sections:
1004   ELFSection &SHStrTab = getSectionHeaderStringTableSection();
1005
1006   // Now that we know which section number is the .shstrtab section, update the
1007   // e_shstrndx entry in the ELF header.
1008   ElfHdr.fixWord16(SHStrTab.SectionIdx, ELFHdr_e_shstrndx_Offset);
1009
1010   // Set the NameIdx of each section in the string table and emit the bytes for
1011   // the string table.
1012   unsigned Index = 0;
1013
1014   for (ELFSectionIter I=SectionList.begin(), E=SectionList.end(); I != E; ++I) {
1015     ELFSection &S = *(*I);
1016     // Set the index into the table.  Note if we have lots of entries with
1017     // common suffixes, we could memoize them here if we cared.
1018     S.NameIdx = Index;
1019     SHStrTab.emitString(S.getName());
1020
1021     // Keep track of the number of bytes emitted to this section.
1022     Index += S.getName().size()+1;
1023   }
1024
1025   // Set the size of .shstrtab now that we know what it is.
1026   assert(Index == SHStrTab.size());
1027   SHStrTab.Size = Index;
1028 }
1029
1030 /// OutputSectionsAndSectionTable - Now that we have constructed the file header
1031 /// and all of the sections, emit these to the ostream destination and emit the
1032 /// SectionTable.
1033 void ELFWriter::OutputSectionsAndSectionTable() {
1034   // Pass #1: Compute the file offset for each section.
1035   size_t FileOff = ElfHdr.size();   // File header first.
1036
1037   // Adjust alignment of all section if needed, skip the null section.
1038   for (unsigned i=1, e=SectionList.size(); i < e; ++i) {
1039     ELFSection &ES = *SectionList[i];
1040     if (!ES.size()) {
1041       ES.Offset = FileOff;
1042       continue;
1043     }
1044
1045     // Update Section size
1046     if (!ES.Size)
1047       ES.Size = ES.size();
1048
1049     // Align FileOff to whatever the alignment restrictions of the section are.
1050     if (ES.Align)
1051       FileOff = (FileOff+ES.Align-1) & ~(ES.Align-1);
1052
1053     ES.Offset = FileOff;
1054     FileOff += ES.Size;
1055   }
1056
1057   // Align Section Header.
1058   unsigned TableAlign = TEW->getPrefELFAlignment();
1059   FileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
1060
1061   // Now that we know where all of the sections will be emitted, set the e_shnum
1062   // entry in the ELF header.
1063   ElfHdr.fixWord16(NumSections, ELFHdr_e_shnum_Offset);
1064
1065   // Now that we know the offset in the file of the section table, update the
1066   // e_shoff address in the ELF header.
1067   ElfHdr.fixWord(FileOff, ELFHdr_e_shoff_Offset);
1068
1069   // Now that we know all of the data in the file header, emit it and all of the
1070   // sections!
1071   O.write((char *)&ElfHdr.getData()[0], ElfHdr.size());
1072   FileOff = ElfHdr.size();
1073
1074   // Section Header Table blob
1075   BinaryObject SHdrTable(isLittleEndian, is64Bit);
1076
1077   // Emit all of sections to the file and build the section header table.
1078   for (ELFSectionIter I=SectionList.begin(), E=SectionList.end(); I != E; ++I) {
1079     ELFSection &S = *(*I);
1080     DEBUG(dbgs() << "SectionIdx: " << S.SectionIdx << ", Name: " << S.getName()
1081                  << ", Size: " << S.Size << ", Offset: " << S.Offset
1082                  << ", SectionData Size: " << S.size() << "\n");
1083
1084     // Align FileOff to whatever the alignment restrictions of the section are.
1085     if (S.size()) {
1086       if (S.Align)  {
1087         for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
1088              FileOff != NewFileOff; ++FileOff)
1089           O << (char)0xAB;
1090       }
1091       O.write((char *)&S.getData()[0], S.Size);
1092       FileOff += S.Size;
1093     }
1094
1095     EmitSectionHeader(SHdrTable, S);
1096   }
1097
1098   // Align output for the section table.
1099   for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
1100        FileOff != NewFileOff; ++FileOff)
1101     O << (char)0xAB;
1102
1103   // Emit the section table itself.
1104   O.write((char *)&SHdrTable.getData()[0], SHdrTable.size());
1105 }