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" |
David Greene | 0d036d2 | 2009-08-19 22:02:07 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRelocation.h" |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Bruno Cardoso Lopes | 171375f | 2009-07-18 19:30:09 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetELFWriterInfo.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetAsmInfo.h" |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // ELFCodeEmitter Implementation |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| 35 | namespace llvm { |
| 36 | |
| 37 | /// startFunction - This callback is invoked when a new machine function is |
| 38 | /// about to be emitted. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 39 | void ELFCodeEmitter::startFunction(MachineFunction &MF) { |
Bruno Cardoso Lopes | 50bc204 | 2009-07-28 19:25:33 +0000 | [diff] [blame] | 40 | DEBUG(errs() << "processing function: " |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 41 | << MF.getFunction()->getName() << "\n"); |
Bruno Cardoso Lopes | 6933d3e | 2009-07-06 09:26:48 +0000 | [diff] [blame] | 42 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 43 | // Get the ELF Section that this function belongs in. |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 44 | ES = &EW.getTextSection(MF.getFunction()); |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 45 | |
Bruno Cardoso Lopes | 6933d3e | 2009-07-06 09:26:48 +0000 | [diff] [blame] | 46 | // Set the desired binary object to be used by the code emitters |
| 47 | setBinaryObject(ES); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 48 | |
Bruno Cardoso Lopes | 3d62a41 | 2009-07-02 02:13:13 +0000 | [diff] [blame] | 49 | // Get the function alignment in bytes |
| 50 | unsigned Align = (1 << MF.getAlignment()); |
| 51 | |
Bruno Cardoso Lopes | 6933d3e | 2009-07-06 09:26:48 +0000 | [diff] [blame] | 52 | // The function must start on its required alignment |
| 53 | ES->emitAlignment(Align); |
| 54 | |
| 55 | // Update the section alignment if needed. |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 56 | ES->Align = std::max(ES->Align, Align); |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 57 | |
Bruno Cardoso Lopes | 6933d3e | 2009-07-06 09:26:48 +0000 | [diff] [blame] | 58 | // Record the function start offset |
| 59 | FnStartOff = ES->getCurrentPCOffset(); |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 60 | |
| 61 | // Emit constant pool and jump tables to their appropriate sections. |
| 62 | // They need to be emitted before the function because in some targets |
| 63 | // the later may reference JT or CP entry address. |
| 64 | emitConstantPool(MF.getConstantPool()); |
| 65 | emitJumpTables(MF.getJumpTableInfo()); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// finishFunction - This callback is invoked after the function is completely |
| 69 | /// finished. |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 70 | bool ELFCodeEmitter::finishFunction(MachineFunction &MF) { |
Bruno Cardoso Lopes | 45f5d64 | 2009-07-02 18:29:24 +0000 | [diff] [blame] | 71 | // Add a symbol to represent the function. |
| 72 | const Function *F = MF.getFunction(); |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 73 | ELFSym *FnSym = ELFSym::getGV(F, EW.getGlobalELFBinding(F), ELFSym::STT_FUNC, |
| 74 | EW.getGlobalELFVisibility(F)); |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 75 | FnSym->SectionIdx = ES->SectionIdx; |
| 76 | FnSym->Size = ES->getCurrentPCOffset()-FnStartOff; |
Bruno Cardoso Lopes | d163e8b | 2009-08-13 21:25:27 +0000 | [diff] [blame] | 77 | EW.AddPendingGlobalSymbol(F, true); |
Bruno Cardoso Lopes | 68491c1 | 2009-07-21 06:51:32 +0000 | [diff] [blame] | 78 | |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 79 | // Offset from start of Section |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 80 | FnSym->Value = FnStartOff; |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 81 | |
Bruno Cardoso Lopes | 4b70fab | 2009-07-15 20:49:10 +0000 | [diff] [blame] | 82 | if (!F->hasPrivateLinkage()) |
| 83 | EW.SymbolList.push_back(FnSym); |
Bruno Cardoso Lopes | 5d41910 | 2009-06-05 00:22:10 +0000 | [diff] [blame] | 84 | |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 85 | // Patch up Jump Table Section relocations to use the real MBBs offsets |
| 86 | // now that the MBB label offsets inside the function are known. |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 87 | if (!MF.getJumpTableInfo()->isEmpty()) { |
| 88 | ELFSection &JTSection = EW.getJumpTableSection(); |
| 89 | for (std::vector<MachineRelocation>::iterator MRI = JTRelocations.begin(), |
| 90 | MRE = JTRelocations.end(); MRI != MRE; ++MRI) { |
| 91 | MachineRelocation &MR = *MRI; |
| 92 | unsigned MBBOffset = getMachineBasicBlockAddress(MR.getBasicBlock()); |
| 93 | MR.setResultPointer((void*)MBBOffset); |
| 94 | MR.setConstantVal(ES->SectionIdx); |
| 95 | JTSection.addRelocation(MR); |
| 96 | } |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 97 | } |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 98 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 99 | // 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] | 100 | // basic blocks, constant pools entries, or jump tables, record their |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 101 | // addresses now so that we can rewrite them with the correct addresses later |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 102 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 103 | MachineRelocation &MR = Relocations[i]; |
| 104 | intptr_t Addr; |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 105 | if (MR.isGlobalValue()) { |
Bruno Cardoso Lopes | d163e8b | 2009-08-13 21:25:27 +0000 | [diff] [blame] | 106 | EW.AddPendingGlobalSymbol(MR.getGlobalValue()); |
Bruno Cardoso Lopes | 746e3bb | 2009-07-27 18:54:47 +0000 | [diff] [blame] | 107 | } else if (MR.isExternalSymbol()) { |
Bruno Cardoso Lopes | d163e8b | 2009-08-13 21:25:27 +0000 | [diff] [blame] | 108 | EW.AddPendingExternalSymbol(MR.getExternalSymbol()); |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 109 | } else if (MR.isBasicBlock()) { |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 110 | Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); |
| 111 | MR.setConstantVal(ES->SectionIdx); |
| 112 | MR.setResultPointer((void*)Addr); |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 113 | } else if (MR.isConstantPoolIndex()) { |
| 114 | Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
| 115 | MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]); |
| 116 | MR.setResultPointer((void*)Addr); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 117 | } else if (MR.isJumpTableIndex()) { |
Bruno Cardoso Lopes | 52d0851 | 2009-08-05 06:57:03 +0000 | [diff] [blame] | 118 | ELFSection &JTSection = EW.getJumpTableSection(); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 119 | Addr = getJumpTableEntryAddress(MR.getJumpTableIndex()); |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 120 | MR.setConstantVal(JTSection.SectionIdx); |
Bruno Cardoso Lopes | e2b0ecd | 2009-07-18 23:24:01 +0000 | [diff] [blame] | 121 | MR.setResultPointer((void*)Addr); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 122 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 123 | llvm_unreachable("Unhandled relocation type"); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 124 | } |
Bruno Cardoso Lopes | ae9163f | 2009-06-14 07:53:21 +0000 | [diff] [blame] | 125 | ES->addRelocation(MR); |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 126 | } |
Bruno Cardoso Lopes | a029a27 | 2009-06-07 21:22:38 +0000 | [diff] [blame] | 127 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 128 | // Clear per-function data structures. |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 129 | JTRelocations.clear(); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 130 | Relocations.clear(); |
| 131 | CPLocations.clear(); |
| 132 | CPSections.clear(); |
| 133 | JTLocations.clear(); |
| 134 | MBBLocations.clear(); |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 138 | /// emitConstantPool - For each constant pool entry, figure out which section |
| 139 | /// the constant should live in and emit the constant |
| 140 | void ELFCodeEmitter::emitConstantPool(MachineConstantPool *MCP) { |
| 141 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
| 142 | if (CP.empty()) return; |
| 143 | |
| 144 | // TODO: handle PIC codegen |
| 145 | assert(TM.getRelocationModel() != Reloc::PIC_ && |
| 146 | "PIC codegen not yet handled for elf constant pools!"); |
| 147 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 148 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 149 | MachineConstantPoolEntry CPE = CP[i]; |
| 150 | |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 151 | // Record the constant pool location and the section index |
Bruno Cardoso Lopes | e2b0ecd | 2009-07-18 23:24:01 +0000 | [diff] [blame] | 152 | ELFSection &CstPool = EW.getConstantPoolSection(CPE); |
| 153 | CPLocations.push_back(CstPool.size()); |
| 154 | CPSections.push_back(CstPool.SectionIdx); |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 155 | |
| 156 | if (CPE.isMachineConstantPoolEntry()) |
| 157 | assert("CPE.isMachineConstantPoolEntry not supported yet"); |
| 158 | |
| 159 | // Emit the constant to constant pool section |
Bruno Cardoso Lopes | e2b0ecd | 2009-07-18 23:24:01 +0000 | [diff] [blame] | 160 | EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool); |
Bruno Cardoso Lopes | a5e0abd | 2009-06-25 07:36:24 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 164 | /// emitJumpTables - Emit all the jump tables for a given jump table info |
| 165 | /// record to the appropriate section. |
| 166 | void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) { |
| 167 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 168 | if (JT.empty()) return; |
| 169 | |
| 170 | // FIXME: handle PIC codegen |
| 171 | assert(TM.getRelocationModel() != Reloc::PIC_ && |
| 172 | "PIC codegen not yet handled for elf jump tables!"); |
| 173 | |
Bruno Cardoso Lopes | 171375f | 2009-07-18 19:30:09 +0000 | [diff] [blame] | 174 | const TargetELFWriterInfo *TEW = TM.getELFWriterInfo(); |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 175 | unsigned EntrySize = MJTI->getEntrySize(); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 176 | |
| 177 | // Get the ELF Section to emit the jump table |
Bruno Cardoso Lopes | e2b0ecd | 2009-07-18 23:24:01 +0000 | [diff] [blame] | 178 | ELFSection &JTSection = EW.getJumpTableSection(); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 179 | |
| 180 | // For each JT, record its offset from the start of the section |
| 181 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 182 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
| 183 | |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 184 | // Record JT 'i' offset in the JT section |
| 185 | JTLocations.push_back(JTSection.size()); |
| 186 | |
| 187 | // Each MBB entry in the Jump table section has a relocation entry |
| 188 | // against the current text section. |
| 189 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
Bruno Cardoso Lopes | 617dd7b | 2009-07-18 20:52:11 +0000 | [diff] [blame] | 190 | unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy(); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 191 | MachineRelocation MR = |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 192 | MachineRelocation::getBB(JTSection.size(), MachineRelTy, MBBs[mi]); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 193 | |
| 194 | // Add the relocation to the Jump Table section |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 195 | JTRelocations.push_back(MR); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 196 | |
| 197 | // Output placeholder for MBB in the JT section |
Bruno Cardoso Lopes | 82a70cc | 2009-07-21 23:13:26 +0000 | [diff] [blame] | 198 | for (unsigned s=0; s < EntrySize; ++s) |
| 199 | JTSection.emitByte(0); |
Bruno Cardoso Lopes | 0b1308f | 2009-07-03 04:36:26 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Bruno Cardoso Lopes | 4cb3143 | 2009-06-03 17:47:27 +0000 | [diff] [blame] | 204 | } // end namespace llvm |