retdec
elf_upx_stub.h
Go to the documentation of this file.
1 
7 #ifndef UNPACKERTOOL_PLUGINS_UPX_ELF_ELF_UPX_STUB_H
8 #define UNPACKERTOOL_PLUGINS_UPX_ELF_ELF_UPX_STUB_H
9 
10 #include <vector>
11 
15 
16 using namespace retdec::utils;
17 
18 // Foroward declarations
19 namespace ELFIO {
20  struct Elf32_Ehdr;
21  struct Elf32_Phdr;
22  struct Elf64_Ehdr;
23  struct Elf64_Phdr;
24 }
25 
26 namespace retdec {
27 namespace unpackertool {
28 namespace upx {
29 
33 template <int /*bits*/> struct ElfUpxStubTraits {};
34 
38 template <> struct ElfUpxStubTraits<32>
39 {
41  using AddressType = std::uint32_t;
43  using ElfHeaderType = ELFIO::Elf32_Ehdr;
45  using ProgHeaderType = ELFIO::Elf32_Phdr;
46 
48  static const AddressType FirstBlockOffset = 0x18;
49 
51  static const AddressType ElfHeaderEhsizeOffset = 40;
53  static const AddressType ElfHeaderPhnumOffset = 44;
54 
56  static const AddressType ProgHeaderOffsetOffset = 4;
58  static const AddressType ProgHeaderFileszOffset = 16;
60  static const AddressType ProgHeaderSize = 32;
61 };
62 
66 template <> struct ElfUpxStubTraits<64>
67 {
69  using AddressType = std::uint64_t;
71  using ElfHeaderType = ELFIO::Elf64_Ehdr;
73  using ProgHeaderType = ELFIO::Elf64_Phdr;
74 
76  static const AddressType FirstBlockOffset = 0x18;
77 
79  static const AddressType ElfHeaderEhsizeOffset = 52;
81  static const AddressType ElfHeaderPhnumOffset = 56;
82 
84  static const AddressType ProgHeaderOffsetOffset = 8;
86  static const AddressType ProgHeaderFileszOffset = 32;
88  static const AddressType ProgHeaderSize = 56;
89 };
90 
98 template <int bits> class ElfUpxStub : public UpxStub
99 {
100 public:
104 
105  ElfUpxStub(
106  retdec::loader::Image* inputFile,
107  const UpxStubData* stubData,
108  const DynamicBuffer& stubCapturedData,
109  std::unique_ptr<Decompressor> decompressor,
110  const UpxMetadata& metadata
111  );
112 
113  virtual void unpack(const std::string& outputFile) override;
114  virtual void cleanup() override;
115 
116  void setupPackingMethod(std::uint8_t packingMethod);
117  void decompress(DynamicBuffer& packedData, DynamicBuffer& unpackedData);
118 
119 private:
120  std::uint32_t getFirstBlockOffset();
121  bool validBlock(const DynamicBuffer& block);
122  void unpackBlock(
123  DynamicBuffer& unpackedData,
124  AddressType fileOffset,
125  AddressType& readFromBuffer,
126  std::uint32_t sizeHint = 0
127  );
128  void unpackBlock(
129  DynamicBuffer& unpackedData,
130  DynamicBuffer& packedBlock,
131  AddressType& readFromBuffer,
132  std::uint32_t sizeHint = 0
133  );
134  AddressType nextLoadSegmentGap(
135  const std::vector<ProgHeaderType>& phdrs,
136  std::uint32_t currentLoadSegmentIndex
137  );
138  void unfilterBlock(
139  const DynamicBuffer& packedBlock,
140  DynamicBuffer& unpackedData
141  );
142 
144 };
145 
146 } // namespace upx
147 } // namespace unpackertool
148 } // namespace retdec
149 
150 #endif
Bit getters for NRV decompression algorithms.
Definition: image.h:22
Definition: bit_parsers.h:19
Definition: elf_upx_stub.h:99
typename ElfUpxStubTraits< bits >::AddressType AddressType
Definition: elf_upx_stub.h:101
typename ElfUpxStubTraits< bits >::ElfHeaderType ElfHeaderType
Definition: elf_upx_stub.h:102
retdec::unpacker::BitParser * _bitParser
Associated NRV bit parser.
Definition: elf_upx_stub.h:143
typename ElfUpxStubTraits< bits >::ProgHeaderType ProgHeaderType
Definition: elf_upx_stub.h:103
Definition: upx_stub.h:111
The class for dynamic buffered data manipulation taking the endianness of the data in account.
Definition: dynamic_buffer.h:36
Declaration of class for buffered data mainpulation.
Definition: elf_upx_stub.h:19
Definition: unpacking_stub.h:16
Definition: archive_wrapper.h:19
void cleanup(ProgramOptions &po)
Definition: retdec-decompiler.cpp:910
std::uint32_t AddressType
Type with default word size.
Definition: elf_upx_stub.h:41
ELFIO::Elf32_Ehdr ElfHeaderType
Type of ELF header.
Definition: elf_upx_stub.h:43
ELFIO::Elf32_Phdr ProgHeaderType
Type of ELF program headers.
Definition: elf_upx_stub.h:45
ELFIO::Elf64_Phdr ProgHeaderType
Type of ELF program headers.
Definition: elf_upx_stub.h:73
std::uint64_t AddressType
Type with default word size.
Definition: elf_upx_stub.h:69
ELFIO::Elf64_Ehdr ElfHeaderType
Type of ELF header.
Definition: elf_upx_stub.h:71
Definition: elf_upx_stub.h:33
Definition: upx_stub_signatures.h:25
Declaration of abstract UPX stub class that represents the unpacking procedure itself.