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" |
| 16 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 17 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // ELFCodeEmitter Implementation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | namespace llvm { |
| 27 | |
| 28 | /// startFunction - This callback is invoked when a new machine function is |
| 29 | /// about to be emitted. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 30 | void ELFCodeEmitter::startFunction(MachineFunction &MF) { |
| 31 | const TargetData *TD = TM.getTargetData(); |
| 32 | const Function *F = MF.getFunction(); |
| 33 | |
| 34 | // Align the output buffer to the appropriate alignment, power of 2. |
| 35 | unsigned FnAlign = F->getAlignment(); |
| 36 | unsigned TDAlign = TD->getPrefTypeAlignment(F->getType()); |
| 37 | unsigned Align = std::max(FnAlign, TDAlign); |
| 38 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
| 39 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 40 | // Get the ELF Section that this function belongs in. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 41 | ES = &EW.getTextSection(); |
| 42 | |
| 43 | // FIXME: better memory management, this will be replaced by BinaryObjects |
| 44 | ES->SectionData.reserve(4096); |
| 45 | BufferBegin = &ES->SectionData[0]; |
| 46 | BufferEnd = BufferBegin + ES->SectionData.capacity(); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 47 | |
| 48 | // Upgrade the section alignment if required. |
| 49 | if (ES->Align < Align) ES->Align = Align; |
| 50 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 51 | // Round the size up to the correct alignment for starting the new function. |
| 52 | ES->Size = (ES->Size + (Align-1)) & (-Align); |
| 53 | |
| 54 | // Snaity check on allocated space for text section |
| 55 | assert( ES->Size < 4096 && "no more space in TextSection" ); |
| 56 | |
| 57 | // FIXME: Using ES->Size directly here instead of calculating it from the |
| 58 | // output buffer size (impossible because the code emitter deals only in raw |
| 59 | // bytes) forces us to manually synchronize size and write padding zero bytes |
| 60 | // to the output buffer for all non-text sections. For text sections, we do |
| 61 | // not synchonize the output buffer, and we just blow up if anyone tries to |
| 62 | // write non-code to it. An assert should probably be added to |
| 63 | // AddSymbolToSection to prevent calling it on the text section. |
| 64 | CurBufferPtr = BufferBegin + ES->Size; |
| 65 | |
| 66 | // Record function start address relative to BufferBegin |
| 67 | FnStartPtr = CurBufferPtr; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /// finishFunction - This callback is invoked after the function is completely |
| 71 | /// finished. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 72 | bool ELFCodeEmitter::finishFunction(MachineFunction &MF) { |
| 73 | // Add a symbol to represent the function. |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 74 | ELFSym FnSym(MF.getFunction()); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 75 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 76 | // Update Section Size |
| 77 | ES->Size = CurBufferPtr - BufferBegin; |
| 78 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 79 | // Figure out the binding (linkage) of the symbol. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 80 | switch (MF.getFunction()->getLinkage()) { |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 81 | default: |
| 82 | // appending linkage is illegal for functions. |
| 83 | assert(0 && "Unknown linkage type!"); |
| 84 | case GlobalValue::ExternalLinkage: |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 85 | FnSym.SetBind(ELFSym::STB_GLOBAL); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 86 | break; |
| 87 | case GlobalValue::LinkOnceAnyLinkage: |
| 88 | case GlobalValue::LinkOnceODRLinkage: |
| 89 | case GlobalValue::WeakAnyLinkage: |
| 90 | case GlobalValue::WeakODRLinkage: |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 91 | FnSym.SetBind(ELFSym::STB_WEAK); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 92 | break; |
| 93 | case GlobalValue::PrivateLinkage: |
| 94 | assert (0 && "PrivateLinkage should not be in the symbol table."); |
| 95 | case GlobalValue::InternalLinkage: |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 96 | FnSym.SetBind(ELFSym::STB_LOCAL); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 97 | break; |
| 98 | } |
| 99 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 100 | // Set the symbol type as a function |
Bruno Cardoso Lopes | f5b0c5a | 2009-06-06 03:56:29 +0000 | [diff] [blame] | 101 | FnSym.SetType(ELFSym::STT_FUNC); |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 102 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 103 | FnSym.SectionIdx = ES->SectionIdx; |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 104 | FnSym.Size = CurBufferPtr-FnStartPtr; |
| 105 | |
| 106 | // Offset from start of Section |
| 107 | FnSym.Value = FnStartPtr-BufferBegin; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 108 | |
| 109 | // Finally, add it to the symtab. |
| 110 | EW.SymbolTable.push_back(FnSym); |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 111 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 112 | // Relocations |
| 113 | // ----------- |
| 114 | // If we have emitted any relocations to function-specific objects such as |
| 115 | // basic blocks, constant pools entries, or jump tables, record their |
| 116 | // addresses now so that we can rewrite them with the correct addresses |
| 117 | // later. |
| 118 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 119 | MachineRelocation &MR = Relocations[i]; |
| 120 | intptr_t Addr; |
| 121 | |
| 122 | if (MR.isBasicBlock()) { |
| 123 | Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); |
| 124 | MR.setConstantVal(ES->SectionIdx); |
| 125 | MR.setResultPointer((void*)Addr); |
| 126 | } else if (MR.isGlobalValue()) { |
| 127 | EW.PendingGlobals.insert(MR.getGlobalValue()); |
| 128 | } else { |
| 129 | assert(0 && "Unhandled relocation type"); |
| 130 | } |
| 131 | ES->Relocations.push_back(MR); |
| 132 | } |
| 133 | Relocations.clear(); |
| 134 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | } // end namespace llvm |