-/// finishFunction - This callback is invoked after the function is completely
-/// finished.
-bool MachOCodeEmitter::finishFunction(MachineFunction &F) {
- // Get the Mach-O Section that this function belongs in.
- MachOWriter::MachOSection *MOS = MOW.getTextSection();
-
- MOS->size += CurBufferPtr - BufferBegin;
-
- // Get a symbol for the function to add to the symbol table
- const GlobalValue *FuncV = F.getFunction();
- MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index, MOW.TM);
-
- // Emit constant pool to appropriate section(s)
- emitConstantPool(F.getConstantPool());
-
- // Emit jump tables to appropriate section
- emitJumpTables(F.getJumpTableInfo());
-
- // If we have emitted any relocations to function-specific objects such as
- // basic blocks, constant pools entries, or jump tables, record their
- // addresses now so that we can rewrite them with the correct addresses
- // later.
- for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
- MachineRelocation &MR = Relocations[i];
- intptr_t Addr;
-
- if (MR.isBasicBlock()) {
- Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
- MR.setConstantVal(MOS->Index);
- MR.setResultPointer((void*)Addr);
- } else if (MR.isJumpTableIndex()) {
- Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
- MR.setConstantVal(MOW.getJumpTableSection()->Index);
- MR.setResultPointer((void*)Addr);
- } else if (MR.isConstantPoolIndex()) {
- Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
- MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
- MR.setResultPointer((void*)Addr);
- } else if (!MR.isGlobalValue()) {
- assert(0 && "Unhandled relocation type");
- }
- MOS->Relocations.push_back(MR);
- }
- Relocations.clear();
-
- // Finally, add it to the symtab.
- MOW.SymbolTable.push_back(FnSym);
+bool MachOWriter::doInitialization(Module &M) {
+ // Set the magic value, now that we know the pointer size and endianness
+ Header.setMagic(isLittleEndian, is64Bit);
+
+ // Set the file type
+ // FIXME: this only works for object files, we do not support the creation
+ // of dynamic libraries or executables at this time.
+ Header.filetype = MachOHeader::MH_OBJECT;
+
+ Mang = new Mangler(M);