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 | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 12 | #include "ELF.h" |
| 13 | #include "ELFWriter.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 14 | #include "ELFCodeEmitter.h" |
| 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
| 17 | #include "llvm/Function.h" |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/BinaryObject.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 20 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineRelocation.h" |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetAsmInfo.h" |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // ELFCodeEmitter Implementation |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | namespace llvm { |
| 32 | |
| 33 | /// startFunction - This callback is invoked when a new machine function is |
| 34 | /// about to be emitted. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 35 | void ELFCodeEmitter::startFunction(MachineFunction &MF) { |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 36 | // Get the ELF Section that this function belongs in. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 37 | ES = &EW.getTextSection(); |
| 38 | |
Bruno Cardoso Lopes | c997d45 | 2009-06-11 19:16:03 +0000 | [diff] [blame] | 39 | DOUT << "processing function: " << MF.getFunction()->getName() << "\n"; |
| 40 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 41 | // FIXME: better memory management, this will be replaced by BinaryObjects |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 42 | BinaryData &BD = ES->getData(); |
| 43 | BD.reserve(4096); |
| 44 | BufferBegin = &BD[0]; |
| 45 | BufferEnd = BufferBegin + BD.capacity(); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 46 | |
Bruno Cardoso Lopes | 3d62a41 | 2009-07-02 02:13:13 +0000 | [diff] [blame] | 47 | // Get the function alignment in bytes |
| 48 | unsigned Align = (1 << MF.getAlignment()); |
| 49 | |
| 50 | // Align the section size with the function alignment, so the function can |
| 51 | // 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] | 52 | if (ES->Align < Align) ES->Align = Align; |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 53 | ES->Size = (ES->Size + (Align-1)) & (-Align); |
| 54 | |
| 55 | // Snaity check on allocated space for text section |
| 56 | assert( ES->Size < 4096 && "no more space in TextSection" ); |
| 57 | |
| 58 | // FIXME: Using ES->Size directly here instead of calculating it from the |
| 59 | // output buffer size (impossible because the code emitter deals only in raw |
| 60 | // bytes) forces us to manually synchronize size and write padding zero bytes |
| 61 | // to the output buffer for all non-text sections. For text sections, we do |
| 62 | // not synchonize the output buffer, and we just blow up if anyone tries to |
| 63 | // write non-code to it. An assert should probably be added to |
| 64 | // AddSymbolToSection to prevent calling it on the text section. |
| 65 | CurBufferPtr = BufferBegin + ES->Size; |
| 66 | |
| 67 | // Record function start address relative to BufferBegin |
| 68 | FnStartPtr = CurBufferPtr; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /// finishFunction - This callback is invoked after the function is completely |
| 72 | /// finished. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 73 | bool ELFCodeEmitter::finishFunction(MachineFunction &MF) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 74 | // Update Section Size |
| 75 | ES->Size = CurBufferPtr - BufferBegin; |
| 76 | |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 77 | // Add a symbol to represent the function. |
| 78 | const Function *F = MF.getFunction(); |
| 79 | ELFSym FnSym(F); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 80 | FnSym.setType(ELFSym::STT_FUNC); |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 81 | FnSym.setBind(EW.getGlobalELFLinkage(F)); |
| 82 | FnSym.setVisibility(EW.getGlobalELFVisibility(F)); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 83 | FnSym.SectionIdx = ES->SectionIdx; |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 84 | FnSym.Size = CurBufferPtr-FnStartPtr; |
| 85 | |
| 86 | // Offset from start of Section |
| 87 | FnSym.Value = FnStartPtr-BufferBegin; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 88 | |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 89 | // Locals should go on the symbol list front |
| 90 | if (!F->hasPrivateLinkage()) { |
| 91 | if (FnSym.getBind() == ELFSym::STB_LOCAL) |
| 92 | EW.SymbolList.push_front(FnSym); |
| 93 | else |
| 94 | EW.SymbolList.push_back(FnSym); |
Bruno Cardoso Lopes | 0d3193e | 2009-06-22 19:16:16 +0000 | [diff] [blame] | 95 | } |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 96 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 97 | // Emit constant pool to appropriate section(s) |
| 98 | emitConstantPool(MF.getConstantPool()); |
| 99 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 100 | // Emit jump tables to appropriate section |
| 101 | emitJumpTables(MF.getJumpTableInfo()); |
| 102 | |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 103 | // Relocations |
| 104 | // ----------- |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 105 | // 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] | 106 | // basic blocks, constant pools entries, or jump tables, record their |
| 107 | // addresses now so that we can rewrite them with the correct addresses |
| 108 | // later. |
| 109 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 110 | MachineRelocation &MR = Relocations[i]; |
| 111 | intptr_t Addr; |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 112 | if (MR.isGlobalValue()) { |
| 113 | EW.PendingGlobals.insert(MR.getGlobalValue()); |
| 114 | } else if (MR.isBasicBlock()) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 115 | Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); |
| 116 | MR.setConstantVal(ES->SectionIdx); |
| 117 | MR.setResultPointer((void*)Addr); |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 118 | } else if (MR.isConstantPoolIndex()) { |
| 119 | Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
| 120 | MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]); |
| 121 | MR.setResultPointer((void*)Addr); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 122 | } else if (MR.isJumpTableIndex()) { |
| 123 | Addr = getJumpTableEntryAddress(MR.getJumpTableIndex()); |
| 124 | MR.setResultPointer((void*)Addr); |
| 125 | MR.setConstantVal(JumpTableSectionIdx); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 126 | } else { |
| 127 | assert(0 && "Unhandled relocation type"); |
| 128 | } |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 129 | ES->addRelocation(MR); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 130 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 131 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 132 | // Clear per-function data structures. |
| 133 | Relocations.clear(); |
| 134 | CPLocations.clear(); |
| 135 | CPSections.clear(); |
| 136 | JTLocations.clear(); |
| 137 | MBBLocations.clear(); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 141 | /// emitConstantPool - For each constant pool entry, figure out which section |
| 142 | /// the constant should live in and emit the constant |
| 143 | void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) { |
| 144 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
| 145 | if (CP.empty()) return; |
| 146 | |
| 147 | // TODO: handle PIC codegen |
| 148 | assert(TM.getRelocationModel() != Reloc::PIC_ && |
| 149 | "PIC codegen not yet handled for elf constant pools!"); |
| 150 | |
| 151 | const TargetAsmInfo *TAI = TM.getTargetAsmInfo(); |
| 152 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 153 | MachineConstantPoolEntry CPE = CP[i]; |
| 154 | |
| 155 | // Get the right ELF Section for this constant pool entry |
| 156 | std::string CstPoolName = |
| 157 | TAI->SelectSectionForMachineConst(CPE.getType())->getName(); |
| 158 | ELFSection &CstPoolSection = |
| 159 | EW.getConstantPoolSection(CstPoolName, CPE.getAlignment()); |
| 160 | |
| 161 | // Record the constant pool location and the section index |
| 162 | CPLocations.push_back(CstPoolSection.size()); |
| 163 | CPSections.push_back(CstPoolSection.SectionIdx); |
| 164 | |
| 165 | if (CPE.isMachineConstantPoolEntry()) |
| 166 | assert("CPE.isMachineConstantPoolEntry not supported yet"); |
| 167 | |
| 168 | // Emit the constant to constant pool section |
| 169 | EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPoolSection); |
| 170 | } |
| 171 | } |
| 172 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 173 | /// emitJumpTables - Emit all the jump tables for a given jump table info |
| 174 | /// record to the appropriate section. |
| 175 | void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) { |
| 176 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 177 | if (JT.empty()) return; |
| 178 | |
| 179 | // FIXME: handle PIC codegen |
| 180 | assert(TM.getRelocationModel() != Reloc::PIC_ && |
| 181 | "PIC codegen not yet handled for elf jump tables!"); |
| 182 | |
| 183 | const TargetAsmInfo *TAI = TM.getTargetAsmInfo(); |
| 184 | |
| 185 | // Get the ELF Section to emit the jump table |
| 186 | unsigned Align = TM.getTargetData()->getPointerABIAlignment(); |
| 187 | std::string JTName(TAI->getJumpTableDataSection()); |
| 188 | ELFSection &JTSection = EW.getJumpTableSection(JTName, Align); |
| 189 | JumpTableSectionIdx = JTSection.SectionIdx; |
| 190 | |
| 191 | // Entries in the JT Section are relocated against the text section |
| 192 | ELFSection &TextSection = EW.getTextSection(); |
| 193 | |
| 194 | // For each JT, record its offset from the start of the section |
| 195 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 196 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 197 | |
| 198 | DOUT << "JTSection.size(): " << JTSection.size() << "\n"; |
| 199 | DOUT << "JTLocations.size: " << JTLocations.size() << "\n"; |
| 200 | |
| 201 | // Record JT 'i' offset in the JT section |
| 202 | JTLocations.push_back(JTSection.size()); |
| 203 | |
| 204 | // Each MBB entry in the Jump table section has a relocation entry |
| 205 | // against the current text section. |
| 206 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
| 207 | MachineRelocation MR = |
| 208 | MachineRelocation::getBB(JTSection.size(), |
| 209 | MachineRelocation::VANILLA, |
| 210 | MBBs[mi]); |
| 211 | |
| 212 | // Offset of JT 'i' in JT section |
| 213 | MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi])); |
| 214 | MR.setConstantVal(TextSection.SectionIdx); |
| 215 | |
| 216 | // Add the relocation to the Jump Table section |
| 217 | JTSection.addRelocation(MR); |
| 218 | |
| 219 | // Output placeholder for MBB in the JT section |
| 220 | JTSection.emitWord(0); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 225 | } // end namespace llvm |