llvm.org GIT mirror llvm / d9182d6
Removing a file that's no longer being used after the recent refactorings git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153825 91177308-0d34-0410-b5e6-96231b3b80d8 Eli Bendersky 8 years ago
1 changed file(s) with 0 addition(s) and 388 deletion(s). Raw diff Collapse all Expand all
+0
-388
lib/ExecutionEngine/RuntimeDyld/DyldELFObject.h less more
None //===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used
10 // to represent a loadable ELF image.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
15 #define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
16
17 #include "llvm/Object/ELF.h"
18
19
20 namespace llvm {
21
22 using support::endianness;
23 using namespace llvm::object;
24
25 template
26 class DyldELFObject : public ELFObjectFile {
27 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
28
29 typedef Elf_Shdr_Impl Elf_Shdr;
30 typedef Elf_Sym_Impl Elf_Sym;
31 typedef Elf_Rel_Impl Elf_Rel;
32 typedef Elf_Rel_Impl Elf_Rela;
33
34 typedef typename ELFObjectFile::
35 Elf_Ehdr Elf_Ehdr;
36 Elf_Ehdr *Header;
37
38 // Update section headers according to the current location in memory
39 virtual void rebaseObject(std::vector *MemoryMap);
40 // Record memory addresses for cleanup
41 virtual void saveAddress(std::vector *MemoryMap, uint8_t *addr);
42
43 protected:
44 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
45
46 public:
47 DyldELFObject(MemoryBuffer *Object, std::vector *MemoryMap,
48 error_code &ec);
49
50 // Methods for type inquiry through isa, cast, and dyn_cast
51 static inline bool classof(const Binary *v) {
52 return (isa >(v)
53 && classof(cast >(v)));
54 }
55 static inline bool classof(
56 const ELFObjectFile *v) {
57 return v->isDyldType();
58 }
59 static inline bool classof(const DyldELFObject *v) {
60 return true;
61 }
62 };
63
64 template
65 DyldELFObject::DyldELFObject(MemoryBuffer *Object,
66 std::vector *MemoryMap, error_code &ec)
67 : ELFObjectFile(Object, ec)
68 , Header(0) {
69 this->isDyldELFObject = true;
70 Header = const_cast(
71 reinterpret_cast(this->base()));
72 if (Header->e_shoff == 0)
73 return;
74
75 // Mark the image as a dynamic shared library
76 Header->e_type = ELF::ET_DYN;
77
78 rebaseObject(MemoryMap);
79 }
80
81 // Walk through the ELF headers, updating virtual addresses to reflect where
82 // the object is currently loaded in memory
83 template
84 void DyldELFObject::rebaseObject(
85 std::vector *MemoryMap) {
86 typedef typename ELFDataTypeTypedefHelper<
87 target_endianness, is64Bits>::value_type addr_type;
88
89 uint8_t *base_p = const_cast(this->base());
90 Elf_Shdr *sectionTable =
91 reinterpret_cast(base_p + Header->e_shoff);
92 uint64_t numSections = this->getNumSections();
93
94 // Allocate memory space for NOBITS sections (such as .bss), which only exist
95 // in memory, but don't occupy space in the object file.
96 // Update the address in the section headers to reflect this allocation.
97 for (uint64_t index = 0; index < numSections; index++) {
98 Elf_Shdr *sec = reinterpret_cast(
99 reinterpret_cast(sectionTable) + index * Header->e_shentsize);
100
101 // Only update sections that are meant to be present in program memory
102 if (sec->sh_flags & ELF::SHF_ALLOC) {
103 uint8_t *addr = base_p + sec->sh_offset;
104 if (sec->sh_type == ELF::SHT_NOBITS) {
105 addr = static_cast(calloc(sec->sh_size, 1));
106 saveAddress(MemoryMap, addr);
107 }
108 else {
109 // FIXME: Currently memory with RWX permissions is allocated. In the
110 // future, make sure that permissions are as necessary
111 if (sec->sh_flags & ELF::SHF_WRITE) {
112 // see FIXME above
113 }
114 if (sec->sh_flags & ELF::SHF_EXECINSTR) {
115 // see FIXME above
116 }
117 }
118 assert(sizeof(addr_type) == sizeof(intptr_t) &&
119 "Cross-architecture ELF dy-load is not supported!");
120 sec->sh_addr = static_cast(intptr_t(addr));
121 }
122 }
123
124 // Now allocate actual space for COMMON symbols, which also don't occupy
125 // space in the object file.
126 // We want to allocate space for all COMMON symbols at once, so the flow is:
127 // 1. Go over all symbols, find those that are in COMMON. For each such
128 // symbol, record its size and the value field in its symbol header in a
129 // special vector.
130 // 2. Allocate memory for all COMMON symbols in one fell swoop.
131 // 3. Using the recorded information from (1), update the address fields in
132 // the symbol headers of the COMMON symbols to reflect their allocated
133 // address.
134 uint64_t TotalSize = 0;
135 std::vector > SymbAddrInfo;
136 error_code ec = object_error::success;
137 for (symbol_iterator si = this->begin_symbols(),
138 se = this->end_symbols(); si != se; si.increment(ec)) {
139 uint64_t Size = 0;
140 ec = si->getSize(Size);
141 Elf_Sym* symb = const_cast(
142 this->getSymbol(si->getRawDataRefImpl()));
143 if (ec == object_error::success &&
144 this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
145 SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
146 TotalSize += Size;
147 }
148 }
149
150 uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
151 saveAddress(MemoryMap, SectionPtr);
152
153 typedef typename std::vector >::iterator
154 AddrInfoIterator;
155 AddrInfoIterator EndIter = SymbAddrInfo.end();
156 for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
157 AddrIter != EndIter; ++AddrIter) {
158 assert(sizeof(addr_type) == sizeof(intptr_t) &&
159 "Cross-architecture ELF dy-load is not supported!");
160 *(AddrIter->first) = static_cast(intptr_t(SectionPtr));
161 SectionPtr += AddrIter->second;
162 }
163 }
164
165 // Record memory addresses for callers
166 template
167 void DyldELFObject::saveAddress(
168 std::vector *MemoryMap, uint8_t* addr) {
169 if (MemoryMap)
170 MemoryMap->push_back(addr);
171 else
172 errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
173 }
174
175 template
176 error_code DyldELFObject::getSymbolAddress(
177 DataRefImpl Symb, uint64_t &Result) const {
178 this->validateSymbol(Symb);
179 const Elf_Sym *symb = this->getSymbol(Symb);
180 if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
181 Result = symb->st_value;
182 return object_error::success;
183 }
184 else {
185 return ELFObjectFile::getSymbolAddress(
186 Symb, Result);
187 }
188 }
189
190 }
191
192 #endif
193
194 //===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===//
195 //
196 // The LLVM Compiler Infrastructure
197 //
198 // This file is distributed under the University of Illinois Open Source
199 // License. See LICENSE.TXT for details.
200 //
201 //===----------------------------------------------------------------------===//
202 //
203 // Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used
204 // to represent a loadable ELF image.
205 //
206 //===----------------------------------------------------------------------===//
207
208 #ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
209 #define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
210
211 #include "llvm/Object/ELF.h"
212
213
214 namespace llvm {
215
216 using support::endianness;
217 using namespace llvm::object;
218
219 template
220 class DyldELFObject : public ELFObjectFile {
221 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
222
223 typedef Elf_Shdr_Impl Elf_Shdr;
224 typedef Elf_Sym_Impl Elf_Sym;
225 typedef Elf_Rel_Impl Elf_Rel;
226 typedef Elf_Rel_Impl Elf_Rela;
227
228 typedef typename ELFObjectFile::
229 Elf_Ehdr Elf_Ehdr;
230 Elf_Ehdr *Header;
231
232 // Update section headers according to the current location in memory
233 virtual void rebaseObject(std::vector *MemoryMap);
234 // Record memory addresses for cleanup
235 virtual void saveAddress(std::vector *MemoryMap, uint8_t *addr);
236
237 protected:
238 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
239
240 public:
241 DyldELFObject(MemoryBuffer *Object, std::vector *MemoryMap,
242 error_code &ec);
243
244 // Methods for type inquiry through isa, cast, and dyn_cast
245 static inline bool classof(const Binary *v) {
246 return (isa >(v)
247 && classof(cast >(v)));
248 }
249 static inline bool classof(
250 const ELFObjectFile *v) {
251 return v->isDyldType();
252 }
253 static inline bool classof(const DyldELFObject *v) {
254 return true;
255 }
256 };
257
258 template
259 DyldELFObject::DyldELFObject(MemoryBuffer *Object,
260 std::vector *MemoryMap, error_code &ec)
261 : ELFObjectFile(Object, ec)
262 , Header(0) {
263 this->isDyldELFObject = true;
264 Header = const_cast(
265 reinterpret_cast(this->base()));
266 if (Header->e_shoff == 0)
267 return;
268
269 // Mark the image as a dynamic shared library
270 Header->e_type = ELF::ET_DYN;
271
272 rebaseObject(MemoryMap);
273 }
274
275 // Walk through the ELF headers, updating virtual addresses to reflect where
276 // the object is currently loaded in memory
277 template
278 void DyldELFObject::rebaseObject(
279 std::vector *MemoryMap) {
280 typedef typename ELFDataTypeTypedefHelper<
281 target_endianness, is64Bits>::value_type addr_type;
282
283 uint8_t *base_p = const_cast(this->base());
284 Elf_Shdr *sectionTable =
285 reinterpret_cast(base_p + Header->e_shoff);
286 uint64_t numSections = this->getNumSections();
287
288 // Allocate memory space for NOBITS sections (such as .bss), which only exist
289 // in memory, but don't occupy space in the object file.
290 // Update the address in the section headers to reflect this allocation.
291 for (uint64_t index = 0; index < numSections; index++) {
292 Elf_Shdr *sec = reinterpret_cast(
293 reinterpret_cast(sectionTable) + index * Header->e_shentsize);
294
295 // Only update sections that are meant to be present in program memory
296 if (sec->sh_flags & ELF::SHF_ALLOC) {
297 uint8_t *addr = base_p + sec->sh_offset;
298 if (sec->sh_type == ELF::SHT_NOBITS) {
299 addr = static_cast(calloc(sec->sh_size, 1));
300 saveAddress(MemoryMap, addr);
301 }
302 else {
303 // FIXME: Currently memory with RWX permissions is allocated. In the
304 // future, make sure that permissions are as necessary
305 if (sec->sh_flags & ELF::SHF_WRITE) {
306 // see FIXME above
307 }
308 if (sec->sh_flags & ELF::SHF_EXECINSTR) {
309 // see FIXME above
310 }
311 }
312 assert(sizeof(addr_type) == sizeof(intptr_t) &&
313 "Cross-architecture ELF dy-load is not supported!");
314 sec->sh_addr = static_cast(intptr_t(addr));
315 }
316 }
317
318 // Now allocate actual space for COMMON symbols, which also don't occupy
319 // space in the object file.
320 // We want to allocate space for all COMMON symbols at once, so the flow is:
321 // 1. Go over all symbols, find those that are in COMMON. For each such
322 // symbol, record its size and the value field in its symbol header in a
323 // special vector.
324 // 2. Allocate memory for all COMMON symbols in one fell swoop.
325 // 3. Using the recorded information from (1), update the address fields in
326 // the symbol headers of the COMMON symbols to reflect their allocated
327 // address.
328 uint64_t TotalSize = 0;
329 std::vector > SymbAddrInfo;
330 error_code ec = object_error::success;
331 for (symbol_iterator si = this->begin_symbols(),
332 se = this->end_symbols(); si != se; si.increment(ec)) {
333 uint64_t Size = 0;
334 ec = si->getSize(Size);
335 Elf_Sym* symb = const_cast(
336 this->getSymbol(si->getRawDataRefImpl()));
337 if (ec == object_error::success &&
338 this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
339 SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
340 TotalSize += Size;
341 }
342 }
343
344 uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
345 saveAddress(MemoryMap, SectionPtr);
346
347 typedef typename std::vector >::iterator
348 AddrInfoIterator;
349 AddrInfoIterator EndIter = SymbAddrInfo.end();
350 for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
351 AddrIter != EndIter; ++AddrIter) {
352 assert(sizeof(addr_type) == sizeof(intptr_t) &&
353 "Cross-architecture ELF dy-load is not supported!");
354 *(AddrIter->first) = static_cast(intptr_t(SectionPtr));
355 SectionPtr += AddrIter->second;
356 }
357 }
358
359 // Record memory addresses for callers
360 template
361 void DyldELFObject::saveAddress(
362 std::vector *MemoryMap, uint8_t* addr) {
363 if (MemoryMap)
364 MemoryMap->push_back(addr);
365 else
366 errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
367 }
368
369 template
370 error_code DyldELFObject::getSymbolAddress(
371 DataRefImpl Symb, uint64_t &Result) const {
372 this->validateSymbol(Symb);
373 const Elf_Sym *symb = this->getSymbol(Symb);
374 if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
375 Result = symb->st_value;
376 return object_error::success;
377 }
378 else {
379 return ELFObjectFile::getSymbolAddress(
380 Symb, Result);
381 }
382 }
383
384 }
385
386 #endif
387