File: Parser.cpp

package info (click to toggle)
lief 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 16,036 kB
  • sloc: cpp: 76,013; python: 6,167; ansic: 3,355; pascal: 404; sh: 98; makefile: 32
file content (124 lines) | stat: -rw-r--r-- 2,871 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include "LIEF/logging++.hpp"
#include "LIEF/filesystem/filesystem.h"

#include "LIEF/OAT/Parser.hpp"
#include "LIEF/OAT/utils.hpp"
#include "LIEF/OAT/Structures.hpp"

#include "LIEF/VDEX.hpp"

#include "Parser.tcc"

namespace LIEF {
namespace OAT {

Parser::~Parser(void) = default;
Parser::Parser(void)  = default;


std::unique_ptr<Binary> Parser::parse(const std::string& oat_file) {
  if (not is_oat(oat_file)) {
    LOG(FATAL) << "'" + oat_file + "' is not an OAT";
    return nullptr;
  }

  Parser parser{oat_file};
  parser.init(oat_file);
  return std::unique_ptr<Binary>{parser.oat_binary_};
}


std::unique_ptr<Binary> Parser::parse(const std::string& oat_file, const std::string& vdex_file) {
  if (not is_oat(oat_file)) {
    return nullptr;
  }

  if (not VDEX::is_vdex(vdex_file)) {
    return nullptr;
  }
  Parser parser{oat_file};
  parser.set_vdex(VDEX::Parser::parse(vdex_file).release());
  parser.init(oat_file);
  return std::unique_ptr<Binary>{parser.oat_binary_};

}

std::unique_ptr<Binary> Parser::parse(const std::vector<uint8_t>& data, const std::string& name) {
  Parser parser{data, name};
  parser.init(name);
  return std::unique_ptr<Binary>{parser.oat_binary_};
}


Parser::Parser(const std::vector<uint8_t>& data, const std::string& name) :
  oat_binary_{new Binary{}},
  stream_{nullptr}
{
  LIEF::ELF::Parser{data, name, LIEF::ELF::DYNSYM_COUNT_METHODS::COUNT_AUTO, this->oat_binary_};
}

Parser::Parser(const std::string& file) :
  LIEF::Parser{file},
  oat_binary_{new Binary{}},
  stream_{nullptr}
{
  LIEF::ELF::Parser{file, LIEF::ELF::DYNSYM_COUNT_METHODS::COUNT_AUTO, this->oat_binary_};
}


bool Parser::has_vdex(void) const {
  return this->vdex_file_ != nullptr;
}

void Parser::set_vdex(VDEX::File* file) {
  this->vdex_file_ = file;
}


void Parser::init(const std::string& name) {
  VLOG(VDEBUG) << "Parsing binary: " << name << std::endl;

  oat_version_t version = OAT::version(*this->oat_binary_);

  if (this->has_vdex()) {
    this->oat_binary_->vdex_ = this->vdex_file_;
  }

  if (not this->has_vdex() and version > OAT_088::oat_version) {
    LOG(WARNING) << "No VDEX provided with this OAT file. Parsing will be incomplete";
  }

  if (version <= OAT_064::oat_version) {
    return this->parse_binary<OAT64_t>();
  }

  if (version <= OAT_079::oat_version) {
    return this->parse_binary<OAT79_t>();
  }

  if (version <= OAT_088::oat_version) {
    return this->parse_binary<OAT88_t>();
  }

  if (version <= OAT_124::oat_version) {
    return this->parse_binary<OAT124_t>();
  }

  if (version <= OAT_131::oat_version) {
    return this->parse_binary<OAT131_t>();
  }

}


void Parser::bind_vdex(void) {
  CHECK_NE(this->vdex_file_, nullptr);
  for (DEX::File& dex_file : this->vdex_file_->dex_files()) {
    this->oat_binary_->dex_files_.push_back(&dex_file);
  }
}


} // namespace OAT
} // namespace LIEF