Skip to content

Commit

Permalink
kptools: Skip relocation when trying again (#76)
Browse files Browse the repository at this point in the history
Signed-off-by: sekaiacg <[email protected]>
  • Loading branch information
sekaiacg authored Mar 25, 2024
1 parent 5d2f2a7 commit 4ab1c3a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 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%08lx\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 @@ -834,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 Down

0 comments on commit 4ab1c3a

Please sign in to comment.