current position:Home>Several key functions of parsing DEX file in Android source code

Several key functions of parsing DEX file in Android source code

2022-01-27 03:50:52 Trail safety

dex brief introduction

dex File as android Main format of , It can be directly in Dalvik Load running files in the virtual machine . dex Documents can be divided into 3 A module , The header file (header)、 Index area (xxxx_ids)、 Data area (data). We're working on android Reinforcement and shelling are required dex Dealing with file formats . Its definition in the system is defined in /art/runtime/dex_file In the document . Following pair dex Several key functions of file format are analyzed .

CheckMagicAndVersion function

// Judge dex The magic value in the file dex The following version 
bool DexFile::CheckMagicAndVersion(std::string* error_msg) const {
 //dex The file magic value exists in the header file header Medium , All through header You can get magic value 
 if (!IsMagicValid(header_->magic_)) {
    std::ostringstream oss;
    oss << "Unrecognized magic number in "  << GetLocation() << ":"
            << " " << header_->magic_[0]
            << " " << header_->magic_[1]
            << " " << header_->magic_[2]
            << " " << header_->magic_[3];
    *error_msg = oss.str();
    return false;
  }
  // Judge whether the obtained magic value is valid 
  if (!IsVersionValid(header_->magic_)) {
    std::ostringstream oss;
    oss << "Unrecognized version number in "  << GetLocation() << ":"
            << " " << header_->magic_[4]
            << " " << header_->magic_[5]
            << " " << header_->magic_[6]
            << " " << header_->magic_[7];
    *error_msg = oss.str();
    return false;
  }
  return true;
}
 Copy code 

DexFile function

// Get parsing dex File format 
DexFile::DexFile(const byte* base, size_t size,
                 const std::string& location,
                 uint32_t location_checksum,
                 MemMap* mem_map)
            
    : begin_(base),
      size_(size),
      location_(location),
      location_checksum_(location_checksum),
      mem_map_(mem_map),
      header_(reinterpret_cast<const Header*>(base)),
      string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
      type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
      field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
      method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
      proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
      class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
      find_class_def_misses_(0),
      class_def_index_(nullptr),
      build_class_def_index_mutex_("DexFile index creation mutex") {
  CHECK(begin_ != NULL) << GetLocation();
  CHECK_GT(size_, 0U) << GetLocation();
}
 Copy code 

FindClassDef function

// lookup dex In the document class_ids data 
const DexFile::ClassDef* DexFile::FindClassDef(uint16_t type_idx) const {
 // obtain class The number of 
 size_t num_class_defs = NumClassDefs();
  for (size_t i = 0; i < num_class_defs; ++i) {
    const ClassDef& class_def = GetClassDef(i);
    if (class_def.class_idx_ == type_idx) {
      return &class_def;
    }
  }
  return NULL;
}
 Copy code 

FindFieldId function

// lookup dex In the document fileId
const DexFile::FieldId* DexFile::FindFieldId(const DexFile::TypeId& declaring_klass, const DexFile::StringId& name, const DexFile::TypeId& type) const {
  // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
  const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
  const uint32_t name_idx = GetIndexForStringId(name);
  const uint16_t type_idx = GetIndexForTypeId(type);
  int32_t lo = 0;
  int32_t hi = NumFieldIds() - 1;
  while (hi >= lo) {
    int32_t mid = (hi + lo) / 2;
    // obtain FileId data 
    const DexFile::FieldId& field = GetFieldId(mid);
    if (class_idx > field.class_idx_) {
      lo = mid + 1;
    } else if (class_idx < field.class_idx_) {
      hi = mid - 1;
    } else {
      if (name_idx > field.name_idx_) {
        lo = mid + 1;
      } else if (name_idx < field.name_idx_) {
        hi = mid - 1;
      } else {
        if (type_idx > field.type_idx_) {
          lo = mid + 1;
        } else if (type_idx < field.type_idx_) {
          hi = mid - 1;
        } else {
        // Successfully return the obtained fileId data 
          return &field;
        }
      }
    }
  }
  return NULL;
}
 Copy code 

FindMethodId function

This function corresponds to dex In the document Method table data

// lookup dex In the document FindMethodId data 
const DexFile::MethodId* DexFile::FindMethodId(const DexFile::TypeId& declaring_klass, const DexFile::StringId& name, const DexFile::ProtoId& signature) const {
  // Binary search MethodIds knowing that they are sorted by class_idx, name_idx then proto_idx
  const uint16_t class_idx = GetIndexForTypeId(declaring_klass);
  const uint32_t name_idx = GetIndexForStringId(name);
  const uint16_t proto_idx = GetIndexForProtoId(signature);
  int32_t lo = 0;
  // obtain dex All of the documents MethodIds The number of 
  int32_t hi = NumMethodIds() - 1;
  // When the quantity is greater than 0
  while (hi >= lo) {
  // Go in half and get one by one 
    int32_t mid = (hi + lo) / 2;
    const DexFile::MethodId& method = GetMethodId(mid);
    if (class_idx > method.class_idx_) {
      lo = mid + 1;
    } else if (class_idx < method.class_idx_) {
      hi = mid - 1;
    } else {
      if (name_idx > method.name_idx_) {
        lo = mid + 1;
      } else if (name_idx < method.name_idx_) {
        hi = mid - 1;
      } else {
        if (proto_idx > method.proto_idx_) {
          lo = mid + 1;
        } else if (proto_idx < method.proto_idx_) {
          hi = mid - 1;
        } else {
          return &method;
        }
      }
    }
  }
  return NULL;
}
 Copy code 

FindStringId function

This function corresponds to dex In the document String table On

// lookup dex In the document StringId data 
const DexFile::StringId* DexFile::FindStringId(const char* string) const {
  int32_t lo = 0;
  // obtain dex All of the documents StringId It's quantity 
  int32_t hi = NumStringIds() - 1;
  while (hi >= lo) {
    int32_t mid = (hi + lo) / 2;
    const DexFile::StringId& str_id = GetStringId(mid);
    const char* str = GetStringData(str_id);
    int compare = CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(string, str);
    if (compare > 0) {
      lo = mid + 1;
    } else if (compare < 0) {
      hi = mid - 1;
    } else {
      return &str_id;
    }
  }
  return NULL;
}
 Copy code 

copyright notice
author[Trail safety],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270350475890.html

Random recommended