Support adding relocations for data sections, handling the cases where
[oota-llvm.git] / lib / CodeGen / ELFCodeEmitter.cpp
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
10 #define DEBUG_TYPE "elfce"
11
12 #include "ELF.h"
13 #include "ELFWriter.h"
14 #include "ELFCodeEmitter.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/CodeGen/BinaryObject.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineJumpTableInfo.h"
21 #include "llvm/CodeGen/MachineRelocation.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetELFWriterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28
29 //===----------------------------------------------------------------------===//
30 //                       ELFCodeEmitter Implementation
31 //===----------------------------------------------------------------------===//
32
33 namespace llvm {
34
35 /// startFunction - This callback is invoked when a new machine function is
36 /// about to be emitted.
37 void ELFCodeEmitter::startFunction(MachineFunction &MF) {
38   DOUT << "processing function: " << MF.getFunction()->getName() << "\n";
39
40   // Get the ELF Section that this function belongs in.
41   ES = &EW.getTextSection();
42
43   // Set the desired binary object to be used by the code emitters
44   setBinaryObject(ES);
45
46   // Get the function alignment in bytes
47   unsigned Align = (1 << MF.getAlignment());
48
49   // The function must start on its required alignment
50   ES->emitAlignment(Align);
51
52   // Update the section alignment if needed.
53   if (ES->Align < Align) ES->Align = Align;
54
55   // Record the function start offset
56   FnStartOff = ES->getCurrentPCOffset();
57 }
58
59 /// finishFunction - This callback is invoked after the function is completely
60 /// finished.
61 bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
62   // Add a symbol to represent the function.
63   const Function *F = MF.getFunction();
64   ELFSym *FnSym = new ELFSym(F);
65   FnSym->setType(ELFSym::STT_FUNC);
66   FnSym->setBind(EW.getGlobalELFBinding(F));
67   FnSym->setVisibility(EW.getGlobalELFVisibility(F));
68   FnSym->SectionIdx = ES->SectionIdx;
69   FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
70
71   // keep track of the emitted function leaving its symbol index as zero
72   // to be patched up later when emitting the symbol table
73   EW.setGlobalSymLookup(F, 0);
74
75   // Offset from start of Section
76   FnSym->Value = FnStartOff;
77
78   if (!F->hasPrivateLinkage())
79     EW.SymbolList.push_back(FnSym);
80
81   // Emit constant pool to appropriate section(s)
82   emitConstantPool(MF.getConstantPool());
83
84   // Emit jump tables to appropriate section
85   emitJumpTables(MF.getJumpTableInfo());
86
87   // Relocations
88   // -----------
89   // If we have emitted any relocations to function-specific objects such as
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;
96     if (MR.isGlobalValue()) {
97       EW.PendingGlobals.insert(MR.getGlobalValue());
98     } else if (MR.isBasicBlock()) {
99       Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
100       MR.setConstantVal(ES->SectionIdx);
101       MR.setResultPointer((void*)Addr);
102     } else if (MR.isConstantPoolIndex()) {
103       Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
104       MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
105       MR.setResultPointer((void*)Addr);
106     } else if (MR.isJumpTableIndex()) {
107       Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
108       MR.setConstantVal(JumpTableSectionIdx);
109       MR.setResultPointer((void*)Addr);
110     } else {
111       llvm_unreachable("Unhandled relocation type");
112     }
113     ES->addRelocation(MR);
114   }
115
116   // Clear per-function data structures.
117   Relocations.clear();
118   CPLocations.clear();
119   CPSections.clear();
120   JTLocations.clear();
121   MBBLocations.clear();
122   return false;
123 }
124
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   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
136     MachineConstantPoolEntry CPE = CP[i];
137
138     // Record the constant pool location and the section index
139     ELFSection &CstPool = EW.getConstantPoolSection(CPE);
140     CPLocations.push_back(CstPool.size());
141     CPSections.push_back(CstPool.SectionIdx);
142
143     if (CPE.isMachineConstantPoolEntry())
144       assert("CPE.isMachineConstantPoolEntry not supported yet");
145
146     // Emit the constant to constant pool section
147     EW.EmitGlobalConstant(CPE.Val.ConstVal, CstPool);
148   }
149 }
150
151 /// emitJumpTables - Emit all the jump tables for a given jump table info
152 /// record to the appropriate section.
153 void ELFCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
154   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
155   if (JT.empty()) return;
156
157   // FIXME: handle PIC codegen
158   assert(TM.getRelocationModel() != Reloc::PIC_ &&
159          "PIC codegen not yet handled for elf jump tables!");
160
161   const TargetELFWriterInfo *TEW = TM.getELFWriterInfo();
162
163   // Get the ELF Section to emit the jump table
164   ELFSection &JTSection = EW.getJumpTableSection();
165   JumpTableSectionIdx = JTSection.SectionIdx;
166
167   // Entries in the JT Section are relocated against the text section
168   ELFSection &TextSection = EW.getTextSection();
169
170   // For each JT, record its offset from the start of the section
171   for (unsigned i = 0, e = JT.size(); i != e; ++i) {
172     const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
173
174     DOUT << "JTSection.size(): " << JTSection.size() << "\n";
175     DOUT << "JTLocations.size: " << JTLocations.size() << "\n";
176
177     // Record JT 'i' offset in the JT section
178     JTLocations.push_back(JTSection.size());
179
180     // Each MBB entry in the Jump table section has a relocation entry
181     // against the current text section.
182     for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
183       unsigned MachineRelTy = TEW->getAbsoluteLabelMachineRelTy();
184       MachineRelocation MR =
185         MachineRelocation::getBB(JTSection.size(),
186                                  MachineRelTy,
187                                  MBBs[mi]);
188
189       // Offset of JT 'i' in JT section
190       MR.setResultPointer((void*)getMachineBasicBlockAddress(MBBs[mi]));
191       MR.setConstantVal(TextSection.SectionIdx);
192
193       // Add the relocation to the Jump Table section
194       JTSection.addRelocation(MR);
195
196       // Output placeholder for MBB in the JT section
197       JTSection.emitWord(0);
198     }
199   }
200 }
201
202 } // end namespace llvm