Share a quick way to load a Dex file

Source: Internet
Author: User
There is a Dexclassloader class on the Android system that can dynamically load DEX files, but there is a flaw in this class, which is that the first time you start and load a Dex file, you perform a dex2oat or dexopt operation, which normally doesn't feel like it's deficient, However, if you use it for reinforcement, you will have a very long initial startup time problem, for which you can use the following methods to increase the speed of the first boot:
The first is art mode, art mode to support the interpretation of the implementation of Dex file, do not need to compile, and do not need oat file, so direct hook execv function, so that the process of invoking dex2oat directly exit the good,
The hook callback function code is as follows:
int hook_execv (const char *name, char **argv) {
int ret = 0;
Char tmp[512];
if (! Isdex2oat (name)) {
Return Ori_execv (name, argv);
}
Exit (0);
}
Hook Way is got hook, the code is as follows:
void* Gothook (Soinfo *si, Char *target_name, void* hook_callback) {
Elf32_rel *rel = (elf32_rel*) si->plt_rela;
int count = si->plt_rela_count;
void *target_addr = 0;

int idx = 0;
for (idx = 0; idx<count; ++idx, ++rel) {
unsigned type = Elf32_r_type (Rel->r_info);
unsigned sym = Elf32_r_sym (Rel->r_info);

Elf32_addr reloc = (elf32_addr) (Rel->r_offset + (U4) si->base);
Elf32_addr sym_addr = 0;
Const char* sym_name = 0;


if (type = = 0) {
Continue
}

Elf32_sym *s = 0;
struct Soinfo_mine *lsi = 0;

if (sym!= 0) {
Sym_name = get_string ((elf32_sym*) si->symtab) [Sym].st_name, si];

printf ("%d sym_name=%s\n", IDX, sym_name);

if (strcmp (Sym_name, target_name))
Continue

printf ("%d sym_name=%s\n", IDX, sym_name);

TARGET_ADDR = * (void**) reloc;
if (hook_callback!= 0) {
Elf32_addr Seg_page_start = Page_start (reloc);
Elf32_addr seg_page_end = page_end (reloc + 4);
errno=0;
int ret = Mprotect ((void*) Seg_page_start, Seg_page_end-seg_page_start,
Prot_read | Prot_write);

* (void**) reloc = Hook_callback;
}
}
}


return target_addr;
}

int Jni_onload () {
...
Libart = (soinfo*) dlopen ("libart.so", Rtld_now);
ORI_EXECV = (FNEXECV) gothook ((soinfo*) Libart, "Execv", (void*) hook_execv);
...
}

The structure used in it can be taken from the Android linker source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.