retdec
elf_image.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LOADER_RETDEC_LOADER_ELF_ELF_IMAGE_H
8 #define RETDEC_LOADER_RETDEC_LOADER_ELF_ELF_IMAGE_H
9 
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13 
15 
16 namespace retdec {
17 namespace loader {
18 
19 class ElfImage : public Image
20 {
29  {
30  SectionMapInfo(const retdec::fileformat::Section* section_, std::uint64_t offset_, std::uint64_t size_) :
31  section(section_), offset(offset_), size(size_) {}
32 
33  SectionMapInfo(const SectionMapInfo& mapInfo) :
34  section(mapInfo.section), offset(mapInfo.offset), size(mapInfo.size) {}
35 
37  std::uint64_t offset;
38  std::uint64_t size;
39  };
40 
41  using SectionList = std::vector<const retdec::fileformat::ElfSection*>;
42  using SegmentToSectionsTable = std::unordered_map<const retdec::fileformat::ElfSegment*, SectionList>;
43 
44 public:
45  ElfImage(const std::shared_ptr<retdec::fileformat::FileFormat>& fileFormat);
46 
47  virtual bool load() override;
48 
49  const std::unordered_map<std::string, std::uint64_t>& getExternFncTable() const
50  {
51  return _externFncTable;
52  }
53 
54 protected:
55  bool loadExecutableFile();
56  bool loadRelocatableFile();
57  bool canLoadSections(const std::vector<retdec::fileformat::Section*>& sections) const;
58  void fixBssSegments();
59  void createExternSegment();
60  void applyRelocations();
62 
64  const Segment* addSegment(const retdec::fileformat::SecSeg* secSeg, std::uint64_t address, std::uint64_t memSize);
65 
66 private:
67 
69  std::vector<std::uint8_t> _externFncData {};
70  // Mapping between extern symbols and their address in extern segment
71  std::unordered_map<std::string, std::uint64_t> _externFncTable {};
72 };
73 
74 } // namespace loader
75 } // namespace retdec
76 
77 #endif
Definition: elf_segment.h:19
Definition: relocation.h:20
Definition: sec_seg.h:24
Definition: section.h:19
Definition: symbol.h:19
Definition: elf_image.h:20
void fixBssSegments()
Definition: elf_image.cpp:502
ElfImage(const std::shared_ptr< retdec::fileformat::FileFormat > &fileFormat)
Definition: elf_image.cpp:30
retdec::fileformat::ElfSegment _extern_segment
Definition: elf_image.h:68
std::vector< const retdec::fileformat::ElfSection * > SectionList
Definition: elf_image.h:41
virtual bool load() override
Definition: elf_image.cpp:40
void createExternSegment()
Definition: elf_image.cpp:76
SegmentToSectionsTable createSegmentToSectionsTable()
Definition: elf_image.cpp:284
std::unordered_map< const retdec::fileformat::ElfSegment *, SectionList > SegmentToSectionsTable
Definition: elf_image.h:42
const Segment * addSegment(const retdec::fileformat::SecSeg *secSeg, std::uint64_t address, std::uint64_t memSize)
Definition: elf_image.cpp:382
bool loadExecutableFile()
Definition: elf_image.cpp:147
std::vector< std::uint8_t > _externFncData
Definition: elf_image.h:69
bool loadRelocatableFile()
Definition: elf_image.cpp:208
std::unordered_map< std::string, std::uint64_t > _externFncTable
Definition: elf_image.h:71
void resolveRelocation(const retdec::fileformat::Relocation &rel, const retdec::fileformat::Symbol &sym)
Definition: elf_image.cpp:598
bool canLoadSections(const std::vector< retdec::fileformat::Section * > &sections) const
Definition: elf_image.cpp:473
void applyRelocations()
Definition: elf_image.cpp:567
const std::unordered_map< std::string, std::uint64_t > & getExternFncTable() const
Definition: elf_image.h:49
Definition: image.h:22
Definition: segment.h:25
Declaration of loadable image class.
Generic loader.
Definition: archive_wrapper.h:19
std::uint64_t size
Definition: elf_image.h:38
const retdec::fileformat::Section * section
Definition: elf_image.h:36
SectionMapInfo(const SectionMapInfo &mapInfo)
Definition: elf_image.h:33
SectionMapInfo(const retdec::fileformat::Section *section_, std::uint64_t offset_, std::uint64_t size_)
Definition: elf_image.h:30
std::uint64_t offset
Definition: elf_image.h:37