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