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