Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 1 | //===-- lib/CodeGen/ELFCodeEmitter.cpp ------------------------------------===// |
| 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 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "elfce" |
| 11 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 12 | #include "ELFCodeEmitter.h" |
| 13 | #include "llvm/Constants.h" |
| 14 | #include "llvm/DerivedTypes.h" |
| 15 | #include "llvm/Function.h" |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/BinaryObject.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 18 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // ELFCodeEmitter Implementation |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
| 29 | /// startFunction - This callback is invoked when a new machine function is |
| 30 | /// about to be emitted. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 31 | void ELFCodeEmitter::startFunction(MachineFunction &MF) { |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 32 | // Get the ELF Section that this function belongs in. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 33 | ES = &EW.getTextSection(); |
| 34 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 35 | DOUT << "processing function: " << MF.getFunction()->getName() << "\n"; |
| 36 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 37 | // FIXME: better memory management, this will be replaced by BinaryObjects |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 38 | BinaryData &BD = ES->getData(); |
| 39 | BD.reserve(4096); |
| 40 | BufferBegin = &BD[0]; |
| 41 | BufferEnd = BufferBegin + BD.capacity(); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 42 | |
Bruno Cardoso Lopes | 3d62a41 | 2009-07-02 02:13:13 +0000 | [diff] [blame^] | 43 | // Get the function alignment in bytes |
| 44 | unsigned Align = (1 << MF.getAlignment()); |
| 45 | |
| 46 | // Align the section size with the function alignment, so the function can |
| 47 | // start in a aligned offset, also update the section alignment if needed. |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 48 | if (ES->Align < Align) ES->Align = Align; |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 49 | ES->Size = (ES->Size + (Align-1)) & (-Align); |
| 50 | |
| 51 | // Snaity check on allocated space for text section |
| 52 | assert( ES->Size < 4096 && "no more space in TextSection" ); |
| 53 | |
| 54 | // FIXME: Using ES->Size directly here instead of calculating it from the |
| 55 | // output buffer size (impossible because the code emitter deals only in raw |
| 56 | // bytes) forces us to manually synchronize size and write padding zero bytes |
| 57 | // to the output buffer for all non-text sections. For text sections, we do |
| 58 | // not synchonize the output buffer, and we just blow up if anyone tries to |
| 59 | // write non-code to it. An assert should probably be added to |
| 60 | // AddSymbolToSection to prevent calling it on the text section. |
| 61 | CurBufferPtr = BufferBegin + ES->Size; |
| 62 | |
| 63 | // Record function start address relative to BufferBegin |
| 64 | FnStartPtr = CurBufferPtr; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /// finishFunction - This callback is invoked after the function is completely |
| 68 | /// finished. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 69 | bool ELFCodeEmitter::finishFunction(MachineFunction &MF) { |
| 70 | // Add a symbol to represent the function. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 71 | ELFSym FnSym(MF.getFunction()); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 72 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 73 | // Update Section Size |
| 74 | ES->Size = CurBufferPtr - BufferBegin; |
| 75 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 76 | // Set the symbol type as a function |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 77 | FnSym.setType(ELFSym::STT_FUNC); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 78 | FnSym.SectionIdx = ES->SectionIdx; |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 79 | FnSym.Size = CurBufferPtr-FnStartPtr; |
| 80 | |
| 81 | // Offset from start of Section |
| 82 | FnSym.Value = FnStartPtr-BufferBegin; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 83 | |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 84 | // Figure out the binding (linkage) of the symbol. |
| 85 | switch (MF.getFunction()->getLinkage()) { |
| 86 | default: |
| 87 | // appending linkage is illegal for functions. |
| 88 | assert(0 && "Unknown linkage type!"); |
| 89 | case GlobalValue::ExternalLinkage: |
| 90 | FnSym.setBind(ELFSym::STB_GLOBAL); |
| 91 | EW.SymbolList.push_back(FnSym); |
| 92 | break; |
| 93 | case GlobalValue::LinkOnceAnyLinkage: |
| 94 | case GlobalValue::LinkOnceODRLinkage: |
| 95 | case GlobalValue::WeakAnyLinkage: |
| 96 | case GlobalValue::WeakODRLinkage: |
| 97 | FnSym.setBind(ELFSym::STB_WEAK); |
| 98 | EW.SymbolList.push_back(FnSym); |
| 99 | break; |
| 100 | case GlobalValue::PrivateLinkage: |
| 101 | assert (0 && "PrivateLinkage should not be in the symbol table."); |
| 102 | case GlobalValue::InternalLinkage: |
| 103 | FnSym.setBind(ELFSym::STB_LOCAL); |
| 104 | EW.SymbolList.push_front(FnSym); |
| 105 | break; |
| 106 | } |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 107 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 108 | // Emit constant pool to appropriate section(s) |
| 109 | emitConstantPool(MF.getConstantPool()); |
| 110 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 111 | // Relocations |
| 112 | // ----------- |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 113 | // If we have emitted any relocations to function-specific objects such as |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 114 | // basic blocks, constant pools entries, or jump tables, record their |
| 115 | // addresses now so that we can rewrite them with the correct addresses |
| 116 | // later. |
| 117 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 118 | MachineRelocation &MR = Relocations[i]; |
| 119 | intptr_t Addr; |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 120 | if (MR.isGlobalValue()) { |
| 121 | EW.PendingGlobals.insert(MR.getGlobalValue()); |
| 122 | } else if (MR.isBasicBlock()) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 123 | Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); |
| 124 | MR.setConstantVal(ES->SectionIdx); |
| 125 | MR.setResultPointer((void*)Addr); |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 126 | } else if (MR.isConstantPoolIndex()) { |
| 127 | Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
| 128 | MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]); |
| 129 | MR.setResultPointer((void*)Addr); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 130 | } else { |
| 131 | assert(0 && "Unhandled relocation type"); |
| 132 | } |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 133 | ES->addRelocation(MR); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 134 | } |
| 135 | Relocations.clear(); |
| 136 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 140 | /// emitConstantPool - For each constant pool entry, figure out which section |
| 141 | /// the constant should live in and emit the constant |
| 142 | void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) { |
| 143 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
| 144 | if (CP.empty()) return; |
| 145 | |
| 146 | // TODO: handle PIC codegen |
| 147 | assert(TM.getRelocationModel() != Reloc::PIC_ && |
| 148 | "PIC codegen not yet handled for elf constant pools!"); |
| 149 | |
| 150 | const TargetAsmInfo *TAI = TM.getTargetAsmInfo(); |
| 151 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 152 | MachineConstantPoolEntry CPE = CP[i]; |
| 153 | |
| 154 | // Get the right ELF Section for this constant pool entry |
| 155 | std::string CstPoolName = |
| 156 | TAI->SelectSectionForMachineConst(CPE.getType())->getName(); |
| 157 | ELFSection &CstPoolSection = |
| 158 | EW.getConstantPoolSection(CstPoolName, CPE.getAlignment()); |
| 159 | |
| 160 | // Record the constant pool location and the section index |
| 161 | CPLocations.push_back(CstPoolSection.size()); |
| 162 | CPSections.push_back(CstPoolSection.SectionIdx); |
| 163 | |
| 164 | if (CPE.isMachineConstantPoolEntry()) |
| 165 | assert("CPE.isMachineConstantPoolEntry not supported yet"); |
| 166 | |
| 167 | // Emit the constant to constant pool section |
| 168 | EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection); |
| 169 | } |
| 170 | } |
| 171 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 172 | } // end namespace llvm |