current commit
260929.11 · deps: update lief to 1.0.0
/* Copyright 2021 - 2026 R. Thomas
 * Copyright 2021 - 2026 Quarkslab
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "logging.hpp"

#include "LIEF/BinaryStream/VectorStream.hpp"

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

#include "LIEF/VDEX/utils.hpp"
#include "LIEF/VDEX/Parser.hpp"

#include "OAT/Structures.hpp"

#include "Parser.tcc"


namespace LIEF::OAT {

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


std::unique_ptr<Binary> Parser::parse(const std::string& oat_file) {
  if (!is_oat(oat_file)) {
    LIEF_ERR("'{}' is not an OAT file", oat_file);
    return nullptr;
  }

  Parser parser{oat_file};
  parser.init();

  std::unique_ptr<Binary> oat_binary{
      static_cast<Binary*>(parser.binary_.release())
  };
  return oat_binary;
}


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

  if (!VDEX::is_vdex(vdex_file)) {
    return nullptr;
  }

  Parser parser{oat_file};
  if (std::unique_ptr<VDEX::File> vdex = VDEX::Parser::parse(vdex_file)) {
    parser.vdex_file_ = std::move(vdex);
  } else {
    LIEF_WARN("Failed to parse VDEX file '{}'", vdex_file);
  }
  parser.init();
  std::unique_ptr<Binary> oat_binary{
      static_cast<Binary*>(parser.binary_.release())
  };
  return oat_binary;
}

std::unique_ptr<Binary> Parser::parse(std::vector<uint8_t> data) {
  Parser parser{std::move(data)};
  parser.init();
  std::unique_ptr<Binary> oat_binary{
      static_cast<Binary*>(parser.binary_.release())
  };
  return oat_binary;
}


Parser::Parser(std::vector<uint8_t> data) {
  stream_ = std::make_unique<VectorStream>(std::move(data));
  binary_ = std::unique_ptr<Binary>(new Binary{});
  config_.count_mtd = ELF::ParserConfig::DYNSYM_COUNT::AUTO;
}

Parser::Parser(const std::string& file) {
  if (auto s = VectorStream::from_file(file)) {
    stream_ = std::make_unique<VectorStream>(std::move(*s));
  }
  binary_ = std::unique_ptr<Binary>(new Binary{});
  config_.count_mtd = ELF::ParserConfig::DYNSYM_COUNT::AUTO;
}


result<uint64_t> Parser::oat_data_exec_gap() const {
  static constexpr uint64_t MAX_GAP = 64_MB;

  const uint64_t data_end = data_address_ + data_size_;
  if (data_end < data_address_ || exec_start_ < data_end) {
    return make_error_code(lief_errors::corrupted);
  }

  const uint64_t gap = exec_start_ - data_end;
  if (gap > MAX_GAP) {
    return make_error_code(lief_errors::corrupted);
  }

  return gap;
}

bool Parser::has_vdex() const {
  return vdex_file_ != nullptr;
}

void Parser::set_vdex(std::unique_ptr<VDEX::File> file) {
  vdex_file_ = std::move(file);
}


void Parser::init() {
  ELF::Parser::init();
  oat_version_t version = OAT::version(oat_binary());
  Binary& oat_bin = oat_binary();
  oat_bin.vdex_ = std::move(vdex_file_);

  if (!oat_bin.has_vdex() && version > details::OAT_088::oat_version) {
    LIEF_INFO("No VDEX provided, OAT parsing will be incomplete");
  }

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

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

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

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

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

  if (version <= details::OAT_138::oat_version) {
    return parse_binary<details::OAT138_t>();
  }
}

} // namespace LIEF::OAT