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