llvm.org GIT mirror llvm / c0e3340
Move three methods only used by MCJIT to MCJIT. These methods are only used by MCJIT and are very specific to it. In fact, they are also fairly specific to the fact that we have a dynamic linker of relocatable objects. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223964 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
7 changed file(s) with 64 addition(s) and 98 deletion(s). Raw diff Collapse all Expand all
592592 bool isSectionData(DataRefImpl Sec) const override;
593593 bool isSectionBSS(DataRefImpl Sec) const override;
594594 bool isSectionVirtual(DataRefImpl Sec) const override;
595 bool isSectionZeroInit(DataRefImpl Sec) const override;
596 bool isSectionReadOnlyData(DataRefImpl Sec) const override;
597 bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
598595 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
599596 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
600597 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
4747 virtual std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
4848 bool &IsDefault) const = 0;
4949
50 virtual uint64_t getSectionFlags(SectionRef Sec) const = 0;
51 virtual uint32_t getSectionType(SectionRef Sec) const = 0;
52
5053 static inline bool classof(const Binary *v) { return v->isELF(); }
5154 };
5255
9699 bool isSectionText(DataRefImpl Sec) const override;
97100 bool isSectionData(DataRefImpl Sec) const override;
98101 bool isSectionBSS(DataRefImpl Sec) const override;
99 bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
100102 bool isSectionVirtual(DataRefImpl Sec) const override;
101 bool isSectionZeroInit(DataRefImpl Sec) const override;
102 bool isSectionReadOnlyData(DataRefImpl Sec) const override;
103103 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
104104 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
105105 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
200200 std::error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
201201 bool &IsDefault) const override;
202202
203 uint64_t getSectionFlags(SectionRef Sec) const override;
204 uint32_t getSectionType(SectionRef Sec) const override;
205
203206 uint8_t getBytesInAddress() const override;
204207 StringRef getFileFormatName() const override;
205208 unsigned getArch() const override;
258261 return Ver.getError();
259262 Version = *Ver;
260263 return object_error::success;
264 }
265
266 template
267 uint64_t ELFObjectFile::getSectionFlags(SectionRef Sec) const {
268 DataRefImpl DRI = Sec.getRawDataRefImpl();
269 return toELFShdrIter(DRI)->sh_flags;
270 }
271
272 template
273 uint32_t ELFObjectFile::getSectionType(SectionRef Sec) const {
274 DataRefImpl DRI = Sec.getRawDataRefImpl();
275 return toELFShdrIter(DRI)->sh_type;
261276 }
262277
263278 template
451466 }
452467
453468 template
454 bool ELFObjectFile::isSectionRequiredForExecution(DataRefImpl Sec) const {
455 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
456 }
457
458 template
459469 bool ELFObjectFile::isSectionVirtual(DataRefImpl Sec) const {
460470 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
461 }
462
463 template
464 bool ELFObjectFile::isSectionZeroInit(DataRefImpl Sec) const {
465 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
466 }
467
468 template
469 bool ELFObjectFile::isSectionReadOnlyData(DataRefImpl Sec) const {
470 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
471 return !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
472471 }
473472
474473 template
199199
200200 // MachO specific.
201201 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
202 unsigned getSectionType(SectionRef Sec) const;
202203
203204 std::error_code getSymbolAddress(DataRefImpl Symb,
204205 uint64_t &Res) const override;
222223 bool isSectionText(DataRefImpl Sec) const override;
223224 bool isSectionData(DataRefImpl Sec) const override;
224225 bool isSectionBSS(DataRefImpl Sec) const override;
225 bool isSectionRequiredForExecution(DataRefImpl Sec) const override;
226226 bool isSectionVirtual(DataRefImpl Sec) const override;
227 bool isSectionZeroInit(DataRefImpl Sec) const override;
228 bool isSectionReadOnlyData(DataRefImpl Sec) const override;
229227 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
230228 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
231229 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
104104 bool isText() const;
105105 bool isData() const;
106106 bool isBSS() const;
107 bool isRequiredForExecution() const;
108107 bool isVirtual() const;
109 bool isZeroInit() const;
110 bool isReadOnlyData() const;
111108
112109 bool containsSymbol(SymbolRef S) const;
113110
120117 section_iterator getRelocatedSection() const;
121118
122119 DataRefImpl getRawDataRefImpl() const;
120 const ObjectFile *getObject() const;
123121 };
124122
125123 /// SymbolRef - This is a value type class that represents a single symbol in
232230 virtual bool isSectionText(DataRefImpl Sec) const = 0;
233231 virtual bool isSectionData(DataRefImpl Sec) const = 0;
234232 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
235 virtual bool isSectionRequiredForExecution(DataRefImpl Sec) const = 0;
236233 // A section is 'virtual' if its contents aren't present in the object image.
237234 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
238 virtual bool isSectionZeroInit(DataRefImpl Sec) const = 0;
239 virtual bool isSectionReadOnlyData(DataRefImpl Sec) const = 0;
240235 virtual bool sectionContainsSymbol(DataRefImpl Sec,
241236 DataRefImpl Symb) const = 0;
242237 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
416411 return OwningObject->isSectionBSS(SectionPimpl);
417412 }
418413
419 inline bool SectionRef::isRequiredForExecution() const {
420 return OwningObject->isSectionRequiredForExecution(SectionPimpl);
421 }
422
423414 inline bool SectionRef::isVirtual() const {
424415 return OwningObject->isSectionVirtual(SectionPimpl);
425 }
426
427 inline bool SectionRef::isZeroInit() const {
428 return OwningObject->isSectionZeroInit(SectionPimpl);
429 }
430
431 inline bool SectionRef::isReadOnlyData() const {
432 return OwningObject->isSectionReadOnlyData(SectionPimpl);
433416 }
434417
435418 inline bool SectionRef::containsSymbol(SymbolRef S) const {
451434
452435 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
453436 return SectionPimpl;
437 }
438
439 inline const ObjectFile *SectionRef::getObject() const {
440 return OwningObject;
454441 }
455442
456443 /// RelocationRef
1515 #include "RuntimeDyldELF.h"
1616 #include "RuntimeDyldImpl.h"
1717 #include "RuntimeDyldMachO.h"
18 #include "llvm/Object/ELF.h"
18 #include "llvm/Object/ELFObjectFile.h"
1919 #include "llvm/Support/MathExtras.h"
2020 #include "llvm/Support/MutexGuard.h"
2121
262262 return TotalSize;
263263 }
264264
265 static bool isRequiredForExecution(const SectionRef &Section) {
266 const ObjectFile *Obj = Section.getObject();
267 if (auto *ELFObj = dyn_cast(Obj))
268 return ELFObj->getSectionFlags(Section) & ELF::SHF_ALLOC;
269 assert(isa(Obj));
270 return true;
271 }
272
273 static bool isReadOnlyData(const SectionRef &Section) {
274 const ObjectFile *Obj = Section.getObject();
275 if (auto *ELFObj = dyn_cast(Obj))
276 return !(ELFObj->getSectionFlags(Section) &
277 (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
278 assert(isa(Obj));
279 return false;
280 }
281
282 static bool isZeroInit(const SectionRef &Section) {
283 const ObjectFile *Obj = Section.getObject();
284 if (auto *ELFObj = dyn_cast(Obj))
285 return ELFObj->getSectionType(Section) == ELF::SHT_NOBITS;
286
287 auto *MachO = cast(Obj);
288 unsigned SectionType = MachO->getSectionType(Section);
289 return SectionType == MachO::S_ZEROFILL ||
290 SectionType == MachO::S_GB_ZEROFILL;
291 }
292
265293 // Compute an upper bound of the memory size that is required to load all
266294 // sections
267295 void RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
280308 SI != SE; ++SI) {
281309 const SectionRef &Section = *SI;
282310
283 bool IsRequired = Section.isRequiredForExecution();
311 bool IsRequired = isRequiredForExecution(Section);
284312
285313 // Consider only the sections that are required to be loaded for execution
286314 if (IsRequired) {
288316 uint64_t DataSize = Section.getSize();
289317 uint64_t Alignment64 = Section.getAlignment();
290318 bool IsCode = Section.isText();
291 bool IsReadOnly = Section.isReadOnlyData();
319 bool IsReadOnly = isReadOnlyData(Section);
292320 Check(Section.getName(Name));
293321 unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
294322
461489 unsigned PaddingSize = 0;
462490 unsigned StubBufSize = 0;
463491 StringRef Name;
464 bool IsRequired = Section.isRequiredForExecution();
492 bool IsRequired = isRequiredForExecution(Section);
465493 bool IsVirtual = Section.isVirtual();
466 bool IsZeroInit = Section.isZeroInit();
467 bool IsReadOnly = Section.isReadOnlyData();
494 bool IsZeroInit = isZeroInit(Section);
495 bool IsReadOnly = isReadOnlyData(Section);
468496 uint64_t DataSize = Section.getSize();
469497 Check(Section.getName(Name));
470498
361361 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
362362 }
363363
364 bool COFFObjectFile::isSectionRequiredForExecution(DataRefImpl Ref) const {
365 // Sections marked 'Info', 'Remove', or 'Discardable' aren't required for
366 // execution.
367 const coff_section *Sec = toSec(Ref);
368 return !(Sec->Characteristics &
369 (COFF::IMAGE_SCN_LNK_INFO | COFF::IMAGE_SCN_LNK_REMOVE |
370 COFF::IMAGE_SCN_MEM_DISCARDABLE));
371 }
372
373364 bool COFFObjectFile::isSectionVirtual(DataRefImpl Ref) const {
374365 const coff_section *Sec = toSec(Ref);
375366 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
376 }
377
378 bool COFFObjectFile::isSectionZeroInit(DataRefImpl Ref) const {
379 const coff_section *Sec = toSec(Ref);
380 return Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
381 }
382
383 bool COFFObjectFile::isSectionReadOnlyData(DataRefImpl Ref) const {
384 const coff_section *Sec = toSec(Ref);
385 // Check if it's any sort of data section.
386 if (!(Sec->Characteristics & (COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA |
387 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)))
388 return false;
389 // If it's writable or executable or contains code, it isn't read-only data.
390 if (Sec->Characteristics &
391 (COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE |
392 COFF::IMAGE_SCN_MEM_WRITE))
393 return false;
394 return true;
395367 }
396368
397369 bool COFFObjectFile::sectionContainsSymbol(DataRefImpl SecRef,
315315 return object_error::success;
316316 }
317317
318 unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
319 DataRefImpl DRI = Sec.getRawDataRefImpl();
320 uint32_t Flags = getSectionFlags(this, DRI);
321 return Flags & MachO::SECTION_TYPE;
322 }
323
318324 // getIndirectName() returns the name of the alias'ed symbol who's string table
319325 // index is in the n_value field.
320326 std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
574580 SectionType == MachO::S_GB_ZEROFILL);
575581 }
576582
577 bool MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sect) const {
578 // FIXME: Unimplemented.
579 return true;
580 }
581
582583 bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
583 // FIXME: Unimplemented.
584 return false;
585 }
586
587 bool MachOObjectFile::isSectionZeroInit(DataRefImpl Sec) const {
588 uint32_t Flags = getSectionFlags(this, Sec);
589 unsigned SectionType = Flags & MachO::SECTION_TYPE;
590 return SectionType == MachO::S_ZEROFILL ||
591 SectionType == MachO::S_GB_ZEROFILL;
592 }
593
594 bool MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec) const {
595 // Consider using the code from isSectionText to look for __const sections.
596 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
597 // to use section attributes to distinguish code from data.
598
599584 // FIXME: Unimplemented.
600585 return false;
601586 }