Add the Object Code Emitter class. Original patch by Aaron Gray, I did some
[oota-llvm.git] / lib / CodeGen / MachOWriter.cpp
1 //===-- MachOWriter.cpp - Target-independent Mach-O 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 Mach-O writer.  This file writes
11 // out the Mach-O file in the following order:
12 //
13 //  #1 FatHeader (universal-only)
14 //  #2 FatArch (universal-only, 1 per universal arch)
15 //  Per arch:
16 //    #3 Header
17 //    #4 Load Commands
18 //    #5 Sections
19 //    #6 Relocations
20 //    #7 Symbols
21 //    #8 Strings
22 //
23 //===----------------------------------------------------------------------===//
24
25 #include "MachOWriter.h"
26 #include "MachOCodeEmitter.h"
27 #include "llvm/Constants.h"
28 #include "llvm/DerivedTypes.h"
29 #include "llvm/Module.h"
30 #include "llvm/PassManager.h"
31 #include "llvm/CodeGen/FileWriters.h"
32 #include "llvm/CodeGen/MachineCodeEmitter.h"
33 #include "llvm/CodeGen/MachineConstantPool.h"
34 #include "llvm/CodeGen/MachineJumpTableInfo.h"
35 #include "llvm/Target/TargetAsmInfo.h"
36 #include "llvm/Target/TargetJITInfo.h"
37 #include "llvm/Support/Mangler.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/OutputBuffer.h"
40 #include "llvm/Support/Streams.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include <algorithm>
43 #include <cstring>
44
45 namespace llvm {
46
47 /// AddMachOWriter - Concrete function to add the Mach-O writer to the function
48 /// pass manager.
49 ObjectCodeEmitter *AddMachOWriter(PassManagerBase &PM,
50                                          raw_ostream &O,
51                                          TargetMachine &TM) {
52   MachOWriter *MOW = new MachOWriter(O, TM);
53   PM.add(MOW);
54   return MOW->getObjectCodeEmitter();
55 }
56
57 //===----------------------------------------------------------------------===//
58 //                          MachOWriter Implementation
59 //===----------------------------------------------------------------------===//
60
61 char MachOWriter::ID = 0;
62
63 MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm) 
64   : MachineFunctionPass(&ID), O(o), TM(tm)
65   {
66   is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
67   isLittleEndian = TM.getTargetData()->isLittleEndian();
68
69   TAI = TM.getTargetAsmInfo();
70
71   // Create the machine code emitter object for this target.
72
73   MachOCE = new MachOCodeEmitter(*this, *getTextSection(true));
74 }
75
76 MachOWriter::~MachOWriter() {
77   delete MachOCE;
78 }
79
80 bool MachOWriter::doInitialization(Module &M) {
81   // Set the magic value, now that we know the pointer size and endianness
82   Header.setMagic(isLittleEndian, is64Bit);
83
84   // Set the file type
85   // FIXME: this only works for object files, we do not support the creation
86   //        of dynamic libraries or executables at this time.
87   Header.filetype = MachOHeader::MH_OBJECT;
88
89   Mang = new Mangler(M);
90   return false;
91 }
92
93 bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
94   return false;
95 }
96
97 /// doFinalization - Now that the module has been completely processed, emit
98 /// the Mach-O file to 'O'.
99 bool MachOWriter::doFinalization(Module &M) {
100   // FIXME: we don't handle debug info yet, we should probably do that.
101   // Okay, the.text section has been completed, build the .data, .bss, and 
102   // "common" sections next.
103
104   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
105        I != E; ++I)
106     EmitGlobal(I);
107   
108   // Emit the header and load commands.
109   EmitHeaderAndLoadCommands();
110
111   // Emit the various sections and their relocation info.
112   EmitSections();
113   EmitRelocations();
114
115   // Write the symbol table and the string table to the end of the file.
116   O.write((char*)&SymT[0], SymT.size());
117   O.write((char*)&StrT[0], StrT.size());
118
119   // We are done with the abstract symbols.
120   SectionList.clear();
121   SymbolTable.clear();
122   DynamicSymbolTable.clear();
123
124   // Release the name mangler object.
125   delete Mang; Mang = 0;
126   return false;
127 }
128
129 void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
130   const Type *Ty = GV->getType()->getElementType();
131   unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
132   unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
133
134   // Reserve space in the .bss section for this symbol while maintaining the
135   // desired section alignment, which must be at least as much as required by
136   // this symbol.
137   OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
138
139   if (Align) {
140     Align = Log2_32(Align);
141     Sec->align = std::max(unsigned(Sec->align), Align);
142
143     Sec->emitAlignment(Sec->align);
144   }
145   // Globals without external linkage apparently do not go in the symbol table.
146   if (!GV->hasLocalLinkage()) {
147     MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TAI);
148     Sym.n_value = Sec->size();
149     SymbolTable.push_back(Sym);
150   }
151
152   // Record the offset of the symbol, and then allocate space for it.
153   // FIXME: remove when we have unified size + output buffer
154   
155   // Now that we know what section the GlovalVariable is going to be emitted 
156   // into, update our mappings.
157   // FIXME: We may also need to update this when outputting non-GlobalVariable
158   // GlobalValues such as functions.
159
160   GVSection[GV] = Sec;
161   GVOffset[GV] = Sec->size();
162   
163   // Allocate space in the section for the global.
164   for (unsigned i = 0; i < Size; ++i)
165     SecDataOut.outbyte(0);
166 }
167
168 void MachOWriter::EmitGlobal(GlobalVariable *GV) {
169   const Type *Ty = GV->getType()->getElementType();
170   unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
171   bool NoInit = !GV->hasInitializer();
172   
173   // If this global has a zero initializer, it is part of the .bss or common
174   // section.
175   if (NoInit || GV->getInitializer()->isNullValue()) {
176     // If this global is part of the common block, add it now.  Variables are
177     // part of the common block if they are zero initialized and allowed to be
178     // merged with other symbols.
179     if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
180         GV->hasCommonLinkage()) {
181       MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV), MachOSym::NO_SECT, TAI);
182       // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
183       // bytes of the symbol.
184       ExtOrCommonSym.n_value = Size;
185       SymbolTable.push_back(ExtOrCommonSym);
186       // Remember that we've seen this symbol
187       GVOffset[GV] = Size;
188       return;
189     }
190     // Otherwise, this symbol is part of the .bss section.
191     MachOSection *BSS = getBSSSection();
192     AddSymbolToSection(BSS, GV);
193     return;
194   }
195   
196   // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
197   // 16 bytes, or a cstring.  Other read only data goes into a regular const
198   // section.  Read-write data goes in the data section.
199   MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) : 
200                                          getDataSection();
201   AddSymbolToSection(Sec, GV);
202   InitMem(GV->getInitializer(), GVOffset[GV], TM.getTargetData(), Sec);
203 }
204
205
206
207 void MachOWriter::EmitHeaderAndLoadCommands() {
208   // Step #0: Fill in the segment load command size, since we need it to figure
209   //          out the rest of the header fields
210
211   MachOSegment SEG("", is64Bit);
212   SEG.nsects  = SectionList.size();
213   SEG.cmdsize = SEG.cmdSize(is64Bit) + 
214                 SEG.nsects * SectionList[0]->cmdSize(is64Bit);
215   
216   // Step #1: calculate the number of load commands.  We always have at least
217   //          one, for the LC_SEGMENT load command, plus two for the normal
218   //          and dynamic symbol tables, if there are any symbols.
219   Header.ncmds = SymbolTable.empty() ? 1 : 3;
220   
221   // Step #2: calculate the size of the load commands
222   Header.sizeofcmds = SEG.cmdsize;
223   if (!SymbolTable.empty())
224     Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
225     
226   // Step #3: write the header to the file
227   // Local alias to shortenify coming code.
228   DataBuffer &FH = Header.HeaderData;
229   OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
230
231   FHOut.outword(Header.magic);
232   FHOut.outword(TM.getMachOWriterInfo()->getCPUType());
233   FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType());
234   FHOut.outword(Header.filetype);
235   FHOut.outword(Header.ncmds);
236   FHOut.outword(Header.sizeofcmds);
237   FHOut.outword(Header.flags);
238   if (is64Bit)
239     FHOut.outword(Header.reserved);
240   
241   // Step #4: Finish filling in the segment load command and write it out
242   for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
243          E = SectionList.end(); I != E; ++I)
244     SEG.filesize += (*I)->size();
245
246   SEG.vmsize = SEG.filesize;
247   SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
248   
249   FHOut.outword(SEG.cmd);
250   FHOut.outword(SEG.cmdsize);
251   FHOut.outstring(SEG.segname, 16);
252   FHOut.outaddr(SEG.vmaddr);
253   FHOut.outaddr(SEG.vmsize);
254   FHOut.outaddr(SEG.fileoff);
255   FHOut.outaddr(SEG.filesize);
256   FHOut.outword(SEG.maxprot);
257   FHOut.outword(SEG.initprot);
258   FHOut.outword(SEG.nsects);
259   FHOut.outword(SEG.flags);
260   
261   // Step #5: Finish filling in the fields of the MachOSections 
262   uint64_t currentAddr = 0;
263   for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
264          E = SectionList.end(); I != E; ++I) {
265     MachOSection *MOS = *I;
266     MOS->addr = currentAddr;
267     MOS->offset = currentAddr + SEG.fileoff;
268     // FIXME: do we need to do something with alignment here?
269     currentAddr += MOS->size();
270   }
271   
272   // Step #6: Emit the symbol table to temporary buffers, so that we know the
273   // size of the string table when we write the next load command.  This also
274   // sorts and assigns indices to each of the symbols, which is necessary for
275   // emitting relocations to externally-defined objects.
276   BufferSymbolAndStringTable();
277   
278   // Step #7: Calculate the number of relocations for each section and write out
279   // the section commands for each section
280   currentAddr += SEG.fileoff;
281   for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
282          E = SectionList.end(); I != E; ++I) {
283     MachOSection *MOS = *I;
284
285     // Convert the relocations to target-specific relocations, and fill in the
286     // relocation offset for this section.
287     CalculateRelocations(*MOS);
288     MOS->reloff = MOS->nreloc ? currentAddr : 0;
289     currentAddr += MOS->nreloc * 8;
290     
291     // write the finalized section command to the output buffer
292     FHOut.outstring(MOS->sectname, 16);
293     FHOut.outstring(MOS->segname, 16);
294     FHOut.outaddr(MOS->addr);
295     FHOut.outaddr(MOS->size());
296     FHOut.outword(MOS->offset);
297     FHOut.outword(MOS->align);
298     FHOut.outword(MOS->reloff);
299     FHOut.outword(MOS->nreloc);
300     FHOut.outword(MOS->flags);
301     FHOut.outword(MOS->reserved1);
302     FHOut.outword(MOS->reserved2);
303     if (is64Bit)
304       FHOut.outword(MOS->reserved3);
305   }
306   
307   // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands
308   SymTab.symoff  = currentAddr;
309   SymTab.nsyms   = SymbolTable.size();
310   SymTab.stroff  = SymTab.symoff + SymT.size();
311   SymTab.strsize = StrT.size();
312   FHOut.outword(SymTab.cmd);
313   FHOut.outword(SymTab.cmdsize);
314   FHOut.outword(SymTab.symoff);
315   FHOut.outword(SymTab.nsyms);
316   FHOut.outword(SymTab.stroff);
317   FHOut.outword(SymTab.strsize);
318
319   // FIXME: set DySymTab fields appropriately
320   // We should probably just update these in BufferSymbolAndStringTable since
321   // thats where we're partitioning up the different kinds of symbols.
322   FHOut.outword(DySymTab.cmd);
323   FHOut.outword(DySymTab.cmdsize);
324   FHOut.outword(DySymTab.ilocalsym);
325   FHOut.outword(DySymTab.nlocalsym);
326   FHOut.outword(DySymTab.iextdefsym);
327   FHOut.outword(DySymTab.nextdefsym);
328   FHOut.outword(DySymTab.iundefsym);
329   FHOut.outword(DySymTab.nundefsym);
330   FHOut.outword(DySymTab.tocoff);
331   FHOut.outword(DySymTab.ntoc);
332   FHOut.outword(DySymTab.modtaboff);
333   FHOut.outword(DySymTab.nmodtab);
334   FHOut.outword(DySymTab.extrefsymoff);
335   FHOut.outword(DySymTab.nextrefsyms);
336   FHOut.outword(DySymTab.indirectsymoff);
337   FHOut.outword(DySymTab.nindirectsyms);
338   FHOut.outword(DySymTab.extreloff);
339   FHOut.outword(DySymTab.nextrel);
340   FHOut.outword(DySymTab.locreloff);
341   FHOut.outword(DySymTab.nlocrel);
342   
343   O.write((char*)&FH[0], FH.size());
344 }
345
346 /// EmitSections - Now that we have constructed the file header and load
347 /// commands, emit the data for each section to the file.
348 void MachOWriter::EmitSections() {
349   for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
350          E = SectionList.end(); I != E; ++I)
351     // Emit the contents of each section
352     if ((*I)->size())
353       O.write((char*)&(*I)->getData()[0], (*I)->size());
354 }
355
356 /// EmitRelocations - emit relocation data from buffer.
357 void MachOWriter::EmitRelocations() {
358   for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
359          E = SectionList.end(); I != E; ++I)
360     // Emit the relocation entry data for each section.
361     if ((*I)->RelocBuffer.size())
362       O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
363 }
364
365 /// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
366 /// each a string table index so that they appear in the correct order in the
367 /// output file.
368 void MachOWriter::BufferSymbolAndStringTable() {
369   // The order of the symbol table is:
370   // 1. local symbols
371   // 2. defined external symbols (sorted by name)
372   // 3. undefined external symbols (sorted by name)
373   
374   // Before sorting the symbols, check the PendingGlobals for any undefined
375   // globals that need to be put in the symbol table.
376   for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(),
377          E = PendingGlobals.end(); I != E; ++I) {
378     if (GVOffset[*I] == 0 && GVSection[*I] == 0) {
379       MachOSym UndfSym(*I, Mang->getValueName(*I), MachOSym::NO_SECT, TAI);
380       SymbolTable.push_back(UndfSym);
381       GVOffset[*I] = -1;
382     }
383   }
384
385   // Sort the symbols by name, so that when we partition the symbols by scope
386   // of definition, we won't have to sort by name within each partition.
387   std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp());
388
389   // Parition the symbol table entries so that all local symbols come before 
390   // all symbols with external linkage. { 1 | 2 3 }
391   std::partition(SymbolTable.begin(), SymbolTable.end(), MachOSym::PartitionByLocal);
392   
393   // Advance iterator to beginning of external symbols and partition so that
394   // all external symbols defined in this module come before all external
395   // symbols defined elsewhere. { 1 | 2 | 3 }
396   for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
397          E = SymbolTable.end(); I != E; ++I) {
398     if (!MachOSym::PartitionByLocal(*I)) {
399       std::partition(I, E, MachOSym::PartitionByDefined);
400       break;
401     }
402   }
403
404   // Calculate the starting index for each of the local, extern defined, and 
405   // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB
406   // load command.
407   for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
408          E = SymbolTable.end(); I != E; ++I) {
409     if (MachOSym::PartitionByLocal(*I)) {
410       ++DySymTab.nlocalsym;
411       ++DySymTab.iextdefsym;
412       ++DySymTab.iundefsym;
413     } else if (MachOSym::PartitionByDefined(*I)) {
414       ++DySymTab.nextdefsym;
415       ++DySymTab.iundefsym;
416     } else {
417       ++DySymTab.nundefsym;
418     }
419   }
420   
421   // Write out a leading zero byte when emitting string table, for n_strx == 0
422   // which means an empty string.
423   OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
424   StrTOut.outbyte(0);
425
426   // The order of the string table is:
427   // 1. strings for external symbols
428   // 2. strings for local symbols
429   // Since this is the opposite order from the symbol table, which we have just
430   // sorted, we can walk the symbol table backwards to output the string table.
431   for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(),
432         E = SymbolTable.rend(); I != E; ++I) {
433     if (I->GVName == "") {
434       I->n_strx = 0;
435     } else {
436       I->n_strx = StrT.size();
437       StrTOut.outstring(I->GVName, I->GVName.length()+1);
438     }
439   }
440
441   OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian);
442
443   unsigned index = 0;
444   for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
445          E = SymbolTable.end(); I != E; ++I, ++index) {
446     // Add the section base address to the section offset in the n_value field
447     // to calculate the full address.
448     // FIXME: handle symbols where the n_value field is not the address
449     GlobalValue *GV = const_cast<GlobalValue*>(I->GV);
450     if (GV && GVSection[GV])
451       I->n_value += GVSection[GV]->addr;
452     if (GV && (GVOffset[GV] == -1))
453       GVOffset[GV] = index;
454          
455     // Emit nlist to buffer
456     SymTOut.outword(I->n_strx);
457     SymTOut.outbyte(I->n_type);
458     SymTOut.outbyte(I->n_sect);
459     SymTOut.outhalf(I->n_desc);
460     SymTOut.outaddr(I->n_value);
461   }
462 }
463
464 /// CalculateRelocations - For each MachineRelocation in the current section,
465 /// calculate the index of the section containing the object to be relocated,
466 /// and the offset into that section.  From this information, create the
467 /// appropriate target-specific MachORelocation type and add buffer it to be
468 /// written out after we are finished writing out sections.
469 void MachOWriter::CalculateRelocations(MachOSection &MOS) {
470   std::vector<MachineRelocation> Relocations =  MOS.getRelocations();
471   for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
472     MachineRelocation &MR = Relocations[i];
473     unsigned TargetSection = MR.getConstantVal();
474     unsigned TargetAddr = 0;
475     unsigned TargetIndex = 0;
476
477     // This is a scattered relocation entry if it points to a global value with
478     // a non-zero offset.
479     bool Scattered = false;
480     bool Extern = false;
481
482     // Since we may not have seen the GlobalValue we were interested in yet at
483     // the time we emitted the relocation for it, fix it up now so that it
484     // points to the offset into the correct section.
485     if (MR.isGlobalValue()) {
486       GlobalValue *GV = MR.getGlobalValue();
487       MachOSection *MOSPtr = GVSection[GV];
488       intptr_t Offset = GVOffset[GV];
489       
490       // If we have never seen the global before, it must be to a symbol
491       // defined in another module (N_UNDF).
492       if (!MOSPtr) {
493         // FIXME: need to append stub suffix
494         Extern = true;
495         TargetAddr = 0;
496         TargetIndex = GVOffset[GV];
497       } else {
498         Scattered = TargetSection != 0;
499         TargetSection = MOSPtr->Index;
500       }
501       MR.setResultPointer((void*)Offset);
502     }
503     
504     // If the symbol is locally defined, pass in the address of the section and
505     // the section index to the code which will generate the target relocation.
506     if (!Extern) {
507         MachOSection &To = *SectionList[TargetSection - 1];
508         TargetAddr = To.addr;
509         TargetIndex = To.Index;
510     }
511
512     OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian);
513     OutputBuffer SecOut(MOS.getData(), is64Bit, isLittleEndian);
514
515     MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex,
516                                       RelocOut, SecOut, Scattered, Extern);
517   }
518 }
519
520 // InitMem - Write the value of a Constant to the specified memory location,
521 // converting it into bytes and relocations.
522 void MachOWriter::InitMem(const Constant *C, uintptr_t Offset, 
523                           const TargetData *TD, MachOSection* mos) {
524   typedef std::pair<const Constant*, intptr_t> CPair;
525   std::vector<CPair> WorkList;
526   uint8_t *Addr = &mos->getData()[0];
527   
528   WorkList.push_back(CPair(C,(intptr_t)Addr + Offset));
529   
530   intptr_t ScatteredOffset = 0;
531   
532   while (!WorkList.empty()) {
533     const Constant *PC = WorkList.back().first;
534     intptr_t PA = WorkList.back().second;
535     WorkList.pop_back();
536     
537     if (isa<UndefValue>(PC)) {
538       continue;
539     } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) {
540       unsigned ElementSize =
541         TD->getTypeAllocSize(CP->getType()->getElementType());
542       for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
543         WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize));
544     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) {
545       //
546       // FIXME: Handle ConstantExpression.  See EE::getConstantValue()
547       //
548       switch (CE->getOpcode()) {
549       case Instruction::GetElementPtr: {
550         SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
551         ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(),
552                                                &Indices[0], Indices.size());
553         WorkList.push_back(CPair(CE->getOperand(0), PA));
554         break;
555       }
556       case Instruction::Add:
557       default:
558         cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
559         abort();
560         break;
561       }
562     } else if (PC->getType()->isSingleValueType()) {
563       unsigned char *ptr = (unsigned char *)PA;
564       switch (PC->getType()->getTypeID()) {
565       case Type::IntegerTyID: {
566         unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth();
567         uint64_t val = cast<ConstantInt>(PC)->getZExtValue();
568         if (NumBits <= 8)
569           ptr[0] = val;
570         else if (NumBits <= 16) {
571           if (TD->isBigEndian())
572             val = ByteSwap_16(val);
573           ptr[0] = val;
574           ptr[1] = val >> 8;
575         } else if (NumBits <= 32) {
576           if (TD->isBigEndian())
577             val = ByteSwap_32(val);
578           ptr[0] = val;
579           ptr[1] = val >> 8;
580           ptr[2] = val >> 16;
581           ptr[3] = val >> 24;
582         } else if (NumBits <= 64) {
583           if (TD->isBigEndian())
584             val = ByteSwap_64(val);
585           ptr[0] = val;
586           ptr[1] = val >> 8;
587           ptr[2] = val >> 16;
588           ptr[3] = val >> 24;
589           ptr[4] = val >> 32;
590           ptr[5] = val >> 40;
591           ptr[6] = val >> 48;
592           ptr[7] = val >> 56;
593         } else {
594           assert(0 && "Not implemented: bit widths > 64");
595         }
596         break;
597       }
598       case Type::FloatTyID: {
599         uint32_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
600                         getZExtValue();
601         if (TD->isBigEndian())
602           val = ByteSwap_32(val);
603         ptr[0] = val;
604         ptr[1] = val >> 8;
605         ptr[2] = val >> 16;
606         ptr[3] = val >> 24;
607         break;
608       }
609       case Type::DoubleTyID: {
610         uint64_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
611                          getZExtValue();
612         if (TD->isBigEndian())
613           val = ByteSwap_64(val);
614         ptr[0] = val;
615         ptr[1] = val >> 8;
616         ptr[2] = val >> 16;
617         ptr[3] = val >> 24;
618         ptr[4] = val >> 32;
619         ptr[5] = val >> 40;
620         ptr[6] = val >> 48;
621         ptr[7] = val >> 56;
622         break;
623       }
624       case Type::PointerTyID:
625         if (isa<ConstantPointerNull>(PC))
626           memset(ptr, 0, TD->getPointerSize());
627         else if (const GlobalValue* GV = dyn_cast<GlobalValue>(PC)) {
628           // FIXME: what about function stubs?
629           mos->addRelocation(MachineRelocation::getGV(PA-(intptr_t)Addr, 
630                                                  MachineRelocation::VANILLA,
631                                                  const_cast<GlobalValue*>(GV),
632                                                  ScatteredOffset));
633           ScatteredOffset = 0;
634         } else
635           assert(0 && "Unknown constant pointer type!");
636         break;
637       default:
638         cerr << "ERROR: Constant unimp for type: " << *PC->getType() << "\n";
639         abort();
640       }
641     } else if (isa<ConstantAggregateZero>(PC)) {
642       memset((void*)PA, 0, (size_t)TD->getTypeAllocSize(PC->getType()));
643     } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) {
644       unsigned ElementSize =
645         TD->getTypeAllocSize(CPA->getType()->getElementType());
646       for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
647         WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize));
648     } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) {
649       const StructLayout *SL =
650         TD->getStructLayout(cast<StructType>(CPS->getType()));
651       for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
652         WorkList.push_back(CPair(CPS->getOperand(i),
653                                  PA+SL->getElementOffset(i)));
654     } else {
655       cerr << "Bad Type: " << *PC->getType() << "\n";
656       assert(0 && "Unknown constant type to initialize memory with!");
657     }
658   }
659 }
660
661 //===----------------------------------------------------------------------===//
662 //                          MachOSym Implementation
663 //===----------------------------------------------------------------------===//
664
665 MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
666                    const TargetAsmInfo *TAI) :
667   GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect),
668   n_desc(0), n_value(0) {
669
670   switch (GV->getLinkage()) {
671   default:
672     assert(0 && "Unexpected linkage type!");
673     break;
674   case GlobalValue::WeakAnyLinkage:
675   case GlobalValue::WeakODRLinkage:
676   case GlobalValue::LinkOnceAnyLinkage:
677   case GlobalValue::LinkOnceODRLinkage:
678   case GlobalValue::CommonLinkage:
679     assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
680   case GlobalValue::ExternalLinkage:
681     GVName = TAI->getGlobalPrefix() + name;
682     n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT;
683     break;
684   case GlobalValue::PrivateLinkage:
685     GVName = TAI->getPrivateGlobalPrefix() + name;
686     break;
687   case GlobalValue::InternalLinkage:
688     GVName = TAI->getGlobalPrefix() + name;
689     break;
690   }
691 }
692
693 } // end namespace llvm
694