Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12)

Delta Between Two Patch Sets: lib/CodeGen/ELFWriter.h

Issue 91042: Implement LLVM JIT side of GDB JIT debugging interface (Closed) SVN Base: http://llvm.org/svn/llvm-project/llvm/trunk/
Left Patch Set: Added hooks for GDB to automatically add the symbol file. Created 4 months, 4 weeks ago
Right Patch Set: Synced with TOT. Created 2 months, 2 weeks ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 //===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===// 1 //===-- ELFWriter.h - Target-independent ELF writer support -----*- C++ -*-===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file defines the ELFWriter class. 10 // This file defines the ELFWriter class.
11 // 11 //
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 #ifndef ELFWRITER_H 14 #ifndef ELFWRITER_H
15 #define ELFWRITER_H 15 #define ELFWRITER_H
16 16
17 #include "llvm/ADT/SetVector.h" 17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h" 18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include <list>
20 #include <map> 19 #include <map>
21 20
22 namespace llvm { 21 namespace llvm {
23 class BinaryObject; 22 class BinaryObject;
24 class Constant; 23 class Constant;
24 class ConstantInt;
25 class ConstantStruct; 25 class ConstantStruct;
26 class ELFCodeEmitter; 26 class ELFCodeEmitter;
27 class ELFRelocation; 27 class ELFRelocation;
28 class ELFSection; 28 class ELFSection;
29 class ELFSym; 29 struct ELFSym;
30 class GlobalVariable; 30 class GlobalVariable;
31 class JITDebugRegisterer; 31 class JITDebugRegisterer;
32 class Mangler; 32 class Mangler;
33 class MachineCodeEmitter; 33 class MachineCodeEmitter;
34 class MachineConstantPoolEntry;
34 class ObjectCodeEmitter; 35 class ObjectCodeEmitter;
35 class TargetAsmInfo; 36 class MCAsmInfo;
36 class TargetELFWriterInfo; 37 class TargetELFWriterInfo;
38 class TargetLoweringObjectFile;
37 class raw_ostream; 39 class raw_ostream;
40 class SectionKind;
41 class MCContext;
42
43 typedef std::vector<ELFSym*>::iterator ELFSymIter;
44 typedef std::vector<ELFSection*>::iterator ELFSectionIter;
45 typedef SetVector<const GlobalValue*>::const_iterator PendingGblsIter;
46 typedef SetVector<const char *>::const_iterator PendingExtsIter;
47 typedef std::pair<const Constant *, int64_t> CstExprResTy;
38 48
39 /// ELFWriter - This class implements the common target-independent code for 49 /// ELFWriter - This class implements the common target-independent code for
40 /// writing ELF files. Targets should derive a class from this to 50 /// writing ELF files. Targets should derive a class from this to
41 /// parameterize the output format. 51 /// parameterize the output format.
42 /// 52 ///
43 class ELFWriter : public MachineFunctionPass { 53 class ELFWriter : public MachineFunctionPass {
44 friend class ELFCodeEmitter; 54 friend class ELFCodeEmitter;
45 friend class JITDebugRegisterer; 55 friend class JITDebugRegisterer;
46 public: 56 public:
47 static char ID; 57 static char ID;
48 58
49 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter 59 /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
50 ObjectCodeEmitter *getObjectCodeEmitter() { 60 ObjectCodeEmitter *getObjectCodeEmitter() {
51 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE); 61 return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
52 } 62 }
53 63
54 ELFWriter(raw_ostream &O, TargetMachine &TM); 64 ELFWriter(raw_ostream &O, TargetMachine &TM);
55 ~ELFWriter(); 65 ~ELFWriter();
56 66
57 protected: 67 protected:
58 /// Output stream to send the resultant object file to. 68 /// Output stream to send the resultant object file to.
59 raw_ostream &O; 69 raw_ostream &O;
60 70
61 /// Target machine description. 71 /// Target machine description.
62 TargetMachine &TM; 72 TargetMachine &TM;
63 73
74 /// Context object for machine code objects.
75 MCContext &OutContext;
76
64 /// Target Elf Writer description. 77 /// Target Elf Writer description.
65 const TargetELFWriterInfo *TEW; 78 const TargetELFWriterInfo *TEW;
66 79
67 /// Mang - The object used to perform name mangling for this module. 80 /// Mang - The object used to perform name mangling for this module.
68 Mangler *Mang; 81 Mangler *Mang;
69 82
70 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine 83 /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
71 /// code for functions to the .o file. 84 /// code for functions to the .o file.
72 ELFCodeEmitter *ElfCE; 85 ELFCodeEmitter *ElfCE;
73 86
74 /// TAI - Target Asm Info, provide information about section names for 87 /// TLOF - Target Lowering Object File, provide section names for globals
88 /// and other object file specific stuff
89 const TargetLoweringObjectFile &TLOF;
90
91 /// MAI - Target Asm Info, provide information about section names for
75 /// globals and other target specific stuff. 92 /// globals and other target specific stuff.
76 const TargetAsmInfo *TAI; 93 const MCAsmInfo *MAI;
77 94
78 //===------------------------------------------------------------------===// 95 //===------------------------------------------------------------------===//
79 // Properties inferred automatically from the target machine. 96 // Properties inferred automatically from the target machine.
80 //===------------------------------------------------------------------===// 97 //===------------------------------------------------------------------===//
81 98
82 /// is64Bit/isLittleEndian - This information is inferred from the target 99 /// is64Bit/isLittleEndian - This information is inferred from the target
83 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file. 100 /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
84 bool is64Bit, isLittleEndian; 101 bool is64Bit, isLittleEndian;
85 102
86 /// doInitialization - Emit the file header and all of the global variables 103 /// doInitialization - Emit the file header and all of the global variables
87 /// for the module to the ELF file. 104 /// for the module to the ELF file.
88 bool doInitialization(Module &M); 105 bool doInitialization(Module &M);
89 bool runOnMachineFunction(MachineFunction &MF); 106 bool runOnMachineFunction(MachineFunction &MF);
90 107
91 /// doFinalization - Now that the module has been completely processed, emit 108 /// doFinalization - Now that the module has been completely processed, emit
92 /// the ELF file to 'O'. 109 /// the ELF file to 'O'.
93 bool doFinalization(Module &M); 110 bool doFinalization(Module &M);
94 111
95 private: 112 private:
96 /// Blob containing the Elf header 113 /// Blob containing the Elf header
97 BinaryObject ElfHdr; 114 BinaryObject ElfHdr;
98 115
99 /// SectionList - This is the list of sections that we have emitted to the 116 /// SectionList - This is the list of sections that we have emitted to the
100 /// file. Once the file has been completely built, the section header table 117 /// file. Once the file has been completely built, the section header table
101 /// is constructed from this info. 118 /// is constructed from this info.
102 std::list<ELFSection> SectionList; 119 std::vector<ELFSection*> SectionList;
103 unsigned NumSections; // Always = SectionList.size() 120 unsigned NumSections; // Always = SectionList.size()
104 121
105 /// SectionLookup - This is a mapping from section name to section number in 122 /// SectionLookup - This is a mapping from section name to section number in
106 /// the SectionList. 123 /// the SectionList. Used to quickly gather the Section Index from MAI names
107 std::map<std::string, ELFSection*> SectionLookup; 124 std::map<std::string, ELFSection*> SectionLookup;
108 125
126 /// PendingGlobals - Globals not processed as symbols yet.
127 SetVector<const GlobalValue*> PendingGlobals;
128
109 /// GblSymLookup - This is a mapping from global value to a symbol index 129 /// GblSymLookup - This is a mapping from global value to a symbol index
110 /// in the symbol table. This is useful since relocations symbol references 130 /// in the symbol table or private symbols list. This is useful since reloc
111 /// must be quickly mapped to a symbol table index 131 /// symbol references must be quickly mapped to their indices on the lists.
112 std::map<const GlobalValue*, uint32_t> GblSymLookup; 132 std::map<const GlobalValue*, uint32_t> GblSymLookup;
113 133
114 /// SymbolList - This is the list of symbols emitted to the symbol table 134 /// PendingExternals - Externals not processed as symbols yet.
115 /// Local symbols go to the front and Globals to the back. 135 SetVector<const char *> PendingExternals;
116 std::list<ELFSym> SymbolList; 136
117 137 /// ExtSymLookup - This is a mapping from externals to a symbol index
118 /// PendingGlobals - List of externally defined symbols that we have been 138 /// in the symbol table list. This is useful since reloc symbol references
119 /// asked to emit, but have not seen a reference to. When a reference 139 /// must be quickly mapped to their symbol table indices.
120 /// is seen, the symbol will move from this list to the SymbolList. 140 std::map<const char *, uint32_t> ExtSymLookup;
121 SetVector<GlobalValue*> PendingGlobals; 141
122 142 /// SymbolList - This is the list of symbols emitted to the symbol table.
123 // Remove tab from section name prefix. This is necessary becase TAI 143 /// When the SymbolList is finally built, local symbols must be placed in
124 // sometimes return a section name prefixed with a "\t" char. This is 144 /// the beginning while non-locals at the end.
125 // a little bit dirty. FIXME: find a better approach, maybe add more 145 std::vector<ELFSym*> SymbolList;
126 // methods to TAI to get the clean name? 146
127 void fixNameForSection(std::string &Name) { 147 /// PrivateSyms - Record private symbols, every symbol here must never be
128 size_t Pos = Name.find("\t"); 148 /// present in the SymbolList.
129 if (Pos != std::string::npos) 149 std::vector<ELFSym*> PrivateSyms;
130 Name.erase(Pos, 1);
131
132 Pos = Name.find(".section ");
133 if (Pos != std::string::npos)
134 Name.erase(Pos, 9);
135
136 Pos = Name.find("\n");
137 if (Pos != std::string::npos)
138 Name.erase(Pos, 1);
139 }
140 150
141 /// getSection - Return the section with the specified name, creating a new 151 /// getSection - Return the section with the specified name, creating a new
142 /// section if one does not already exist. 152 /// section if one does not already exist.
143 ELFSection &getSection(const std::string &Name, unsigned Type, 153 ELFSection &getSection(const std::string &Name, unsigned Type,
144 unsigned Flags = 0, unsigned Align = 0) { 154 unsigned Flags = 0, unsigned Align = 0) {
145 std::string SectionName(Name); 155 ELFSection *&SN = SectionLookup[Name];
146 fixNameForSection(SectionName);
147
148 ELFSection *&SN = SectionLookup[SectionName];
149 if (SN) return *SN; 156 if (SN) return *SN;
150 157
151 SectionList.push_back(ELFSection(SectionName, isLittleEndian, is64Bit)); 158 SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
152 SN = &SectionList.back(); 159 SN = SectionList.back();
153 SN->SectionIdx = NumSections++; 160 SN->SectionIdx = NumSections++;
154 SN->Type = Type; 161 SN->Type = Type;
155 SN->Flags = Flags; 162 SN->Flags = Flags;
156 SN->Link = ELFSection::SHN_UNDEF; 163 SN->Link = ELFSection::SHN_UNDEF;
157 SN->Align = Align; 164 SN->Align = Align;
158 return *SN; 165 return *SN;
159 } 166 }
160 167
161 /// TODO: support mangled names here to emit the right .text section
162 /// for c++ object files.
163 ELFSection &getTextSection() {
164 return getSection(".text", ELFSection::SHT_PROGBITS,
165 ELFSection::SHF_EXECINSTR | ELFSection::SHF_ALLOC);
166 }
167
168 /// Get jump table section on the section name returned by TAI
169 ELFSection &getJumpTableSection(std::string SName, unsigned Align) {
170 return getSection(SName, ELFSection::SHT_PROGBITS,
171 ELFSection::SHF_ALLOC, Align);
172 }
173
174 /// Get a constant pool section based on the section name returned by TAI
175 ELFSection &getConstantPoolSection(std::string SName, unsigned Align) {
176 return getSection(SName, ELFSection::SHT_PROGBITS,
177 ELFSection::SHF_MERGE | ELFSection::SHF_ALLOC, Align);
178 }
179
180 /// Return the relocation section of section 'S'. 'RelA' is true
181 /// if the relocation section contains entries with addends.
182 ELFSection &getRelocSection(std::string SName, bool RelA, unsigned Align) {
183 std::string RelSName(".rel");
184 unsigned SHdrTy = RelA ? ELFSection::SHT_RELA : ELFSection::SHT_REL;
185
186 if (RelA) RelSName.append("a");
187 RelSName.append(SName);
188
189 return getSection(RelSName, SHdrTy, 0, Align);
190 }
191
192 ELFSection &getNonExecStackSection() { 168 ELFSection &getNonExecStackSection() {
193 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1); 169 return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
194 } 170 }
195 171
196 ELFSection &getSymbolTableSection() { 172 ELFSection &getSymbolTableSection() {
197 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0); 173 return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
198 } 174 }
199 175
200 ELFSection &getStringTableSection() { 176 ELFSection &getStringTableSection() {
201 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1); 177 return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
202 } 178 }
203 179
204 ELFSection &getSectionHeaderStringTableSection() { 180 ELFSection &getSectionHeaderStringTableSection() {
205 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1); 181 return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
206 } 182 }
207 183
208 ELFSection &getDataSection() {
209 return getSection(".data", ELFSection::SHT_PROGBITS,
210 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
211 }
212
213 ELFSection &getBSSSection() {
214 return getSection(".bss", ELFSection::SHT_NOBITS,
215 ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
216 }
217
218 ELFSection &getNullSection() { 184 ELFSection &getNullSection() {
219 return getSection("", ELFSection::SHT_NULL, 0); 185 return getSection("", ELFSection::SHT_NULL, 0);
220 } 186 }
221 187
188 ELFSection &getDataSection();
189 ELFSection &getBSSSection();
190 ELFSection &getCtorSection();
191 ELFSection &getDtorSection();
192 ELFSection &getJumpTableSection();
193 ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
194 ELFSection &getTextSection(Function *F);
195 ELFSection &getRelocSection(ELFSection &S);
196
222 // Helpers for obtaining ELF specific info. 197 // Helpers for obtaining ELF specific info.
223 unsigned getGlobalELFLinkage(const GlobalValue *GV); 198 unsigned getGlobalELFBinding(const GlobalValue *GV);
199 unsigned getGlobalELFType(const GlobalValue *GV);
224 unsigned getGlobalELFVisibility(const GlobalValue *GV); 200 unsigned getGlobalELFVisibility(const GlobalValue *GV);
225 unsigned getElfSectionFlags(unsigned Flags); 201
202 // AddPendingGlobalSymbol - Add a global to be processed and to
203 // the global symbol lookup, use a zero index because the table
204 // index will be determined later.
205 void AddPendingGlobalSymbol(const GlobalValue *GV,
206 bool AddToLookup = false);
207
208 // AddPendingExternalSymbol - Add the external to be processed
209 // and to the external symbol lookup, use a zero index because
210 // the symbol table index will be determined later.
211 void AddPendingExternalSymbol(const char *External);
212
213 // AddToSymbolList - Update the symbol lookup and If the symbol is
214 // private add it to PrivateSyms list, otherwise to SymbolList.
215 void AddToSymbolList(ELFSym *GblSym);
226 216
227 // As we complete the ELF file, we need to update fields in the ELF header 217 // As we complete the ELF file, we need to update fields in the ELF header
228 // (e.g. the location of the section table). These members keep track of 218 // (e.g. the location of the section table). These members keep track of
229 // the offset in ELFHeader of these various pieces to update and other 219 // the offset in ELFHeader of these various pieces to update and other
230 // locations in the file. 220 // locations in the file.
231 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header. 221 unsigned ELFHdr_e_shoff_Offset; // e_shoff in ELF header.
232 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header. 222 unsigned ELFHdr_e_shstrndx_Offset; // e_shstrndx in ELF header.
233 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header. 223 unsigned ELFHdr_e_shnum_Offset; // e_shnum in ELF header.
234 224
235 private: 225 private:
236 void EmitFunctionDeclaration(const Function *F); 226 void EmitGlobal(const GlobalValue *GV);
237 void EmitGlobalVar(const GlobalVariable *GV);
238 void EmitGlobalConstant(const Constant *C, ELFSection &GblS); 227 void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
239 void EmitGlobalConstantStruct(const ConstantStruct *CVS, 228 void EmitGlobalConstantStruct(const ConstantStruct *CVS,
240 ELFSection &GblS); 229 ELFSection &GblS);
241 ELFSection &getGlobalSymELFSection(const GlobalVariable *GV, ELFSym &Sym); 230 void EmitGlobalConstantLargeInt(const ConstantInt *CI, ELFSection &S);
231 void EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size,
232 ELFSection &GblS, int64_t Offset = 0);
233 bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
234 void EmitXXStructorList(Constant *List, ELFSection &Xtor);
242 void EmitRelocations(); 235 void EmitRelocations();
243 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA); 236 void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
244 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr); 237 void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
245 void EmitSectionTableStringTable(); 238 void EmitSectionTableStringTable();
246 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym); 239 void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
247 void EmitSymbolTable(); 240 void EmitSymbolTable();
248 void EmitStringTable(); 241 void EmitStringTable(const std::string &ModuleName);
249 void OutputSectionsAndSectionTable(); 242 void OutputSectionsAndSectionTable();
243 void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
244 unsigned Size);
245 unsigned SortSymbols();
246 CstExprResTy ResolveConstantExpr(const Constant *CV);
250 }; 247 };
251 } 248 }
252 249
253 #endif 250 #endif
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497