Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
bmax committed Mar 26, 2024
2 parents 652c482 + 4ab1c3a commit a7df5f6
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 95 deletions.
2 changes: 1 addition & 1 deletion kernel/patch/android/userd.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ static void after_execveat(hook_fargs5_t *args, void *udata)
handle_after_execve(&args->local);
}

#define ORIGIN_RC_FILE "/init.environ.rc"
#define ORIGIN_RC_FILE "/system/etc/init/atrace.rc"
#define REPLACE_RC_FILE "/dev/anduser.rc"

static const char user_rc_data[] = { //
Expand Down
15 changes: 4 additions & 11 deletions tools/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,9 @@ typedef struct

int32_t get_kernel_info(kernel_info_t *kinfo, const char *img, int32_t imglen)
{
kinfo->img_offset = 0;

if (!strncmp("UNCOMPRESSED_IMG", img, strlen("UNCOMPRESSED_IMG"))) {
kinfo->img_offset = 0x14;
tools_logw("kernel image with UNCOMPRESSED_IMG header\n");
}

kinfo->is_be = 0;

arm64_hdr_t *khdr = (arm64_hdr_t *)(img + kinfo->img_offset);
arm64_hdr_t *khdr = (arm64_hdr_t *)img;
if (strncmp(khdr->magic, KERNEL_MAGIC, strlen(KERNEL_MAGIC))) {
tools_loge_exit("kernel image magic error: %s\n", khdr->magic);
}
Expand All @@ -76,10 +69,10 @@ int32_t get_kernel_info(kernel_info_t *kinfo, const char *img, int32_t imglen)
uint32_t b_stext_insn_offset;
if (kinfo->uefi) {
b_primary_entry_insn = khdr->hdr.efi.b_insn;
b_stext_insn_offset = 4 + kinfo->img_offset;
b_stext_insn_offset = 4;
} else {
b_primary_entry_insn = khdr->hdr.nefi.b_insn;
b_stext_insn_offset = 0 + kinfo->img_offset;
b_stext_insn_offset = 0;
}
kinfo->b_stext_insn_offset = b_stext_insn_offset;

Expand Down Expand Up @@ -122,7 +115,7 @@ int32_t get_kernel_info(kernel_info_t *kinfo, const char *img, int32_t imglen)

int32_t kernel_resize(kernel_info_t *kinfo, char *img, int32_t size)
{
arm64_hdr_t *khdr = (arm64_hdr_t *)(img + kinfo->img_offset);
arm64_hdr_t *khdr = (arm64_hdr_t *)img;
uint64_t ksize = size;
if (is_be() ^ kinfo->is_be) ksize = u64swp(size);
khdr->kernel_size_le = ksize;
Expand Down
1 change: 0 additions & 1 deletion tools/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ typedef struct
{
int8_t is_be; // 0: little, 1: big
int8_t uefi; //
int32_t img_offset;
int32_t load_offset;
int32_t kernel_size;
int32_t page_shift;
Expand Down
79 changes: 57 additions & 22 deletions tools/kallsym.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ static int try_find_arm64_relo_table(kallsym_t *info, char *img, int32_t imglen)
tools_logi("find arm64 relocation table range: [0x%08x, 0x%08x), count: 0x%08x\n", cand_start, cand_end, rela_num);

// apply relocations
int32_t max_offset = imglen - 8;
int32_t apply_num = 0;
for (cand = cand_start; cand < cand_end; cand += 24) {
uint64_t r_offset = uint_unpack(img + cand, 8, info->is_be);
Expand All @@ -264,11 +265,14 @@ static int try_find_arm64_relo_table(kallsym_t *info, char *img, int32_t imglen)
// tools_logw("warn ignore arm64 relocation r_offset: 0x%08lx at 0x%08x\n", r_offset, cand);
continue;
}

int32_t offset = r_offset - kernel_va;
if (offset >= imglen) {
// tools_logw("apply relocations error\n");
continue;
if (offset < 0 || offset >= max_offset) {
tools_logw("bad rela offset: 0x%" PRIx64 "\n", r_offset);
info->try_relo = 0;
return -1;
}

uint64_t value = uint_unpack(img + offset, 8, info->is_be);
if (value == r_addend) continue;
*(uint64_t *)(img + offset) = value + r_addend;
Expand Down Expand Up @@ -445,7 +449,7 @@ static int find_num_syms(kallsym_t *info, char *img, int32_t imglen)
return 0;
}

static int find_markers(kallsym_t *info, char *img, int32_t imglen)
static int find_markers_1(kallsym_t *info, char *img, int32_t imglen)
{
int32_t elem_size = get_markers_elem_size(info);
int32_t cand = info->kallsyms_token_table_offset - elem_size;
Expand All @@ -472,6 +476,48 @@ static int find_markers(kallsym_t *info, char *img, int32_t imglen)
return 0;
}

static int find_markers_2(kallsym_t *info, char *img, int32_t imglen)
{
int32_t elem_size = get_markers_elem_size(info);
int32_t cand = info->kallsyms_token_table_offset - KSYM_MIN_MARKER * elem_size;

int64_t marker, last_marker = 0x7fffffff;
int count = 0;
while (cand > 0x1000) {
marker = int_unpack(img + cand, elem_size, info->is_be);
if (last_marker > marker) {
count++;
if (!marker && count > KSYM_MIN_MARKER) break;
} else {
count = 0;
last_marker = 0x7fffffff;
}

last_marker = marker;
cand -= elem_size;
}

if (count < KSYM_MIN_MARKER) {
tools_logw("find kallsyms_markers error\n");
return -1;
}

int32_t marker_end = cand + count * elem_size + elem_size;
info->kallsyms_markers_offset = cand;
info->_marker_num = count;

tools_logi("kallsyms_markers range: [0x%08x, 0x%08x), count: 0x%08x\n", cand, marker_end, count);
return 0;
}

static inline int find_markers(kallsym_t *info, char *img, int32_t imglen)
{
// todo: remove one
int rc = find_markers_1(info, img, imglen);
if (!rc) return rc;
return find_markers_2(info, img, imglen);
}

static int decompress_symbol_name(kallsym_t *info, char *img, int32_t *pos_to_next, char *out_type, char *out_symbol)
{
int32_t pos = *pos_to_next;
Expand Down Expand Up @@ -774,13 +820,6 @@ int analyze_kallsym_info(kallsym_t *info, char *img, int32_t imglen, enum arch_t
if (arch == ARM64) info->try_relo = 1;
if (is_64) info->asm_PTR_size = 8;

info->img_offset = 0;
if (!strncmp("UNCOMPRESSED_IMG", img, strlen("UNCOMPRESSED_IMG"))) {
info->img_offset = 0x14;
}
img += info->img_offset;
imglen -= info->img_offset;

int rc = -1;
static int32_t (*base_funcs[])(kallsym_t *, char *, int32_t) = {
find_linux_banner,
Expand All @@ -799,11 +838,17 @@ int analyze_kallsym_info(kallsym_t *info, char *img, int32_t imglen, enum arch_t
if (!rc) goto out;

// 2nd
if (!info->try_relo) {
memcpy(copied_img, img, imglen);
rc = retry_relo_retry(info, copied_img, imglen);
if (!rc) goto out;
}

// 3rd
if (info->elf64_kernel_base != ELF64_KERNEL_MIN_VA) {
info->elf64_kernel_base = ELF64_KERNEL_MIN_VA;
memcpy(copied_img, img, imglen);
rc = retry_relo_retry(info, copied_img, imglen);
if (!rc) goto out;
}

out:
Expand All @@ -814,8 +859,6 @@ int analyze_kallsym_info(kallsym_t *info, char *img, int32_t imglen, enum arch_t

int32_t get_symbol_index_offset(kallsym_t *info, char *img, int32_t index)
{
img = img + info->img_offset;

int32_t elem_size;
int32_t pos;
if (info->has_relative_base) {
Expand All @@ -832,8 +875,6 @@ int32_t get_symbol_index_offset(kallsym_t *info, char *img, int32_t index)

int get_symbol_offset_and_size(kallsym_t *info, char *img, char *symbol, int32_t *size)
{
img = img + info->img_offset;

char decomp[KSYM_SYMBOL_LEN] = { '\0' };
char type = 0;
*size = 0;
Expand Down Expand Up @@ -862,8 +903,6 @@ int get_symbol_offset_and_size(kallsym_t *info, char *img, char *symbol, int32_t

int get_symbol_offset(kallsym_t *info, char *img, char *symbol)
{
img = img + info->img_offset;

char decomp[KSYM_SYMBOL_LEN] = { '\0' };
char type = 0;
char **tokens = info->kallsyms_token_table;
Expand All @@ -883,8 +922,6 @@ int get_symbol_offset(kallsym_t *info, char *img, char *symbol)

int dump_all_symbols(kallsym_t *info, char *img)
{
img = img + info->img_offset;

char symbol[KSYM_SYMBOL_LEN] = { '\0' };
char type = 0;
char **tokens = info->kallsyms_token_table;
Expand All @@ -901,8 +938,6 @@ int dump_all_symbols(kallsym_t *info, char *img)
int on_each_symbol(kallsym_t *info, char *img, void *userdata,
int32_t (*fn)(int32_t index, char type, const char *symbol, int32_t offset, void *userdata))
{
img = img + info->img_offset;

char symbol[KSYM_SYMBOL_LEN] = { '\0' };
char type = 0;
char **tokens = info->kallsyms_token_table;
Expand Down
2 changes: 0 additions & 2 deletions tools/kallsym.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ typedef struct
int32_t is_64;
int32_t is_be;

int32_t img_offset;

struct
{
uint8_t _;
Expand Down
Loading

0 comments on commit a7df5f6

Please sign in to comment.