Skip to content

Commit

Permalink
elf: export rela_type_string() and apply it to x86_64 relocation
Browse files Browse the repository at this point in the history
Print example in relocation:

	overflow in relocation type R_X86_64_32S(11) val 556ddb198110

Signed-off-by: Rong Tao <[email protected]>
  • Loading branch information
Rtoax committed Mar 2, 2024
1 parent 8709638 commit 0a3c106
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/arch/x86_64/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int apply_relocate_add(const struct load_info *info, GElf_Shdr *sechdrs,
Elf64_Sym *sym;
void *loc;
uint64_t val;
int r_type = 0;

ldebug("Applying relocate section %u to %u\n", relsec, sechdrs[relsec].sh_info);

Expand All @@ -90,15 +91,15 @@ int apply_relocate_add(const struct load_info *info, GElf_Shdr *sechdrs,
+ ELF64_R_SYM(rel[i].r_info);

const char *symname = strtab + sym->st_name;

r_type = (int)ELF64_R_TYPE(rel[i].r_info);
val = sym->st_value + rel[i].r_addend;

ldebug("RELA: %s, st_name %d, type %d, st_value %lx, r_addend %lx, loc %lx, val %lx\n",
symname, sym->st_name,
(int)ELF64_R_TYPE(rel[i].r_info),
ldebug("RELA: %s, st_name %d, type %d, st_value %lx, "
"r_addend %lx, loc %lx, val %lx\n",
symname, sym->st_name, r_type,
sym->st_value, rel[i].r_addend, (uint64_t)loc, val);

switch (ELF64_R_TYPE(rel[i].r_info)) {
switch (r_type) {

case R_X86_64_NONE:
lwarning("Handle R_X86_64_NONE\n");
Expand All @@ -116,8 +117,11 @@ int apply_relocate_add(const struct load_info *info, GElf_Shdr *sechdrs,
if (*(uint32_t *)loc != 0)
goto invalid_relocation;
write_func(loc, &val, 4);
if (val != *(uint32_t *)loc)
if (val != *(uint32_t *)loc) {
lerror("R_X86_64_32 overflow val(%lx) != loc(%x)\n",
val, *(uint32_t *)loc);
goto overflow;
}
break;

case R_X86_64_32S:
Expand Down Expand Up @@ -180,8 +184,8 @@ int apply_relocate_add(const struct load_info *info, GElf_Shdr *sechdrs,
break;

default:
lerror("Unknown rela relocation: %lu\n",
ELF64_R_TYPE(rel[i].r_info));
lerror("Unknown rela relocation: %s\n",
rela_type_string(r_type));
return -ENOEXEC;
}
}
Expand All @@ -190,13 +194,13 @@ int apply_relocate_add(const struct load_info *info, GElf_Shdr *sechdrs,

invalid_relocation:
lerror("x86: Skipping invalid relocation target, "
"existing value is nonzero for type %d, loc %p, val %lx\n",
(int)ELF64_R_TYPE(rel[i].r_info), loc, val);
"existing value is nonzero for type %s(%d), loc %p, val %lx\n",
rela_type_string(r_type), r_type, loc, val);
return -ENOEXEC;

overflow:
lerror("overflow in relocation type %d val %lx\n",
(int)ELF64_R_TYPE(rel[i].r_info), val);
lerror("overflow in relocation type %s(%d) val %lx\n",
rela_type_string(r_type), r_type, val);
lerror("likely not compiled with -mcmodel=kernel.\n");
return -ENOEXEC;
}
Expand Down
2 changes: 2 additions & 0 deletions src/elf/elf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ const char *strbuildid(uint8_t *bid, size_t descsz, char *buf, size_t buf_len);

/* ELF Rela api */
int handle_relocs(struct elf_file *elf, GElf_Shdr *shdr, Elf_Scn *scn);
const char *rela_type_string(int r);
void print_rela(GElf_Rela *rela);

/* ELF Auxv api */
int auxv_type_info(GElf_Xword a_type, const char **name, const char **format);
Expand Down

0 comments on commit 0a3c106

Please sign in to comment.