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