diff options
author | Julius Werner <jwerner@chromium.org> | 2021-02-12 17:37:27 -0800 |
---|---|---|
committer | Julius Werner <jwerner@chromium.org> | 2021-02-18 02:32:06 +0000 |
commit | 84446e6e54fa9df42fcbb8450c9a071feaf1f8d0 (patch) | |
tree | d300069dd3ff9f2cb59e86f9f320ce9018653196 /util/cbfstool | |
parent | a2642d0ffe3ecba201a0bfe6b219e35b52c399ae (diff) |
rmodtool: Make memlayout symbols absolute and do not relocate them
Memlayout is a mechanism to define memory areas outside the normal
program segment constructed by the linker. Therefore, it generally
doesn't make sense to relocate memlayout symbols when the program is
relocated. They tend to refer to things that are always in one specific
spot, independent of where the program is loaded.
This hasn't really hurt us in the past because the use case we have for
rmodules (ramstage on x86) just happens to not really need to refer to
any memlayout-defined areas at the moment. But that use case may come up
in the future so it's still worth fixing.
This patch declares all memlayout-defined symbols as ABSOLUTE() in the
linker, which is then reflected in the symbol table of the generated
ELF. We can then use that distinction to have rmodtool skip them when
generating the relocation table for an rmodule. (Also rearrange rmodtool
a little to make the primary string table more easily accessible to the
rest of the code, so we can refer to symbol names in debug output.)
A similar problem can come up with userspace unit tests, but we cannot
modify the userspace relocation toolchain (and for unfortunate
historical reasons, it tries to relocate even absolute symbols). We'll
just disable PIC and make those binaries fully static to avoid that
issue.
Signed-off-by: Julius Werner <jwerner@chromium.org>
Change-Id: Ic51d9add3dc463495282b365c1b6d4a9bf11dbf2
Reviewed-on: https://review.coreboot.org/c/coreboot/+/50629
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Diffstat (limited to 'util/cbfstool')
-rw-r--r-- | util/cbfstool/rmodule.c | 70 | ||||
-rw-r--r-- | util/cbfstool/rmodule.h | 2 |
2 files changed, 43 insertions, 29 deletions
diff --git a/util/cbfstool/rmodule.c b/util/cbfstool/rmodule.c index 429bbf37fb..258a4d8803 100644 --- a/util/cbfstool/rmodule.c +++ b/util/cbfstool/rmodule.c @@ -72,7 +72,7 @@ static int valid_reloc_arm(Elf64_Rela *rel) /* Only these 6 relocations are expected to be found. */ return (type == R_ARM_ABS32 || type == R_ARM_THM_PC22 || - type == R_ARM_THM_JUMP24 || type == R_ARM_V4BX || + type == R_ARM_THM_JUMP24 || type == R_ARM_V4BX || type == R_ARM_CALL || type == R_ARM_JUMP24); } @@ -137,6 +137,19 @@ static const struct arch_ops reloc_ops[] = { }, }; +static int relocation_for_absolute_symbol(struct rmod_context *ctx, Elf64_Rela *r) +{ + Elf64_Sym *s = &ctx->pelf.syms[ELF64_R_SYM(r->r_info)]; + + if (s->st_shndx == SHN_ABS) { + DEBUG("Omitting relocation for absolute symbol: %s\n", + &ctx->strtab[s->st_name]); + return 1; + } + + return 0; +} + /* * Relocation processing loops. */ @@ -172,6 +185,9 @@ static int for_each_reloc(struct rmod_context *ctx, struct reloc_filter *f, return -1; } + if (relocation_for_absolute_symbol(ctx, r)) + continue; + /* Allow the provided filter to have precedence. */ if (f != NULL) { filter_emit = f->filter(f, r); @@ -341,7 +357,7 @@ int rmodule_collect_relocations(struct rmod_context *ctx, static int populate_sym(struct rmod_context *ctx, const char *sym_name, Elf64_Addr *addr, - int nsyms, const char *strtab, int optional) + int nsyms, int optional) { int i; Elf64_Sym *syms; @@ -351,7 +367,7 @@ populate_sym(struct rmod_context *ctx, const char *sym_name, Elf64_Addr *addr, for (i = 0; i < nsyms; i++) { if (syms[i].st_name == 0) continue; - if (strcmp(sym_name, &strtab[syms[i].st_name])) + if (strcmp(sym_name, &ctx->strtab[syms[i].st_name])) continue; DEBUG("%s -> 0x%llx\n", sym_name, (long long)syms[i].st_value); *addr = syms[i].st_value; @@ -371,7 +387,6 @@ populate_sym(struct rmod_context *ctx, const char *sym_name, Elf64_Addr *addr, static int populate_rmodule_info(struct rmod_context *ctx) { int i; - const char *strtab; struct parsed_elf *pelf; Elf64_Ehdr *ehdr; int nsyms; @@ -379,23 +394,6 @@ static int populate_rmodule_info(struct rmod_context *ctx) pelf = &ctx->pelf; ehdr = &pelf->ehdr; - /* Obtain the string table. */ - strtab = NULL; - for (i = 0; i < ehdr->e_shnum; i++) { - if (ctx->pelf.strtabs[i] == NULL) - continue; - /* Don't use the section headers' string table. */ - if (i == ehdr->e_shstrndx) - continue; - strtab = buffer_get(ctx->pelf.strtabs[i]); - break; - } - - if (strtab == NULL) { - ERROR("No string table found.\n"); - return -1; - } - /* Determine number of symbols. */ nsyms = 0; for (i = 0; i < ehdr->e_shnum; i++) { @@ -406,18 +404,16 @@ static int populate_rmodule_info(struct rmod_context *ctx) break; } - if (populate_sym(ctx, "_rmodule_params", &ctx->parameters_begin, - nsyms, strtab, 1)) + if (populate_sym(ctx, "_rmodule_params", &ctx->parameters_begin, nsyms, 1)) return -1; - if (populate_sym(ctx, "_ermodule_params", &ctx->parameters_end, - nsyms, strtab, 1)) + if (populate_sym(ctx, "_ermodule_params", &ctx->parameters_end, nsyms, 1)) return -1; - if (populate_sym(ctx, "_bss", &ctx->bss_begin, nsyms, strtab, 0)) + if (populate_sym(ctx, "_bss", &ctx->bss_begin, nsyms, 0)) return -1; - if (populate_sym(ctx, "_ebss", &ctx->bss_end, nsyms, strtab, 0)) + if (populate_sym(ctx, "_ebss", &ctx->bss_end, nsyms, 0)) return -1; return 0; @@ -425,7 +421,7 @@ static int populate_rmodule_info(struct rmod_context *ctx) static int add_section(struct elf_writer *ew, struct buffer *data, const char *name, - Elf64_Addr addr, Elf64_Word size) + Elf64_Addr addr, Elf64_Word size) { Elf64_Shdr shdr; int ret; @@ -452,7 +448,7 @@ add_section(struct elf_writer *ew, struct buffer *data, const char *name, static int write_elf(const struct rmod_context *ctx, const struct buffer *in, - struct buffer *out) + struct buffer *out) { int ret; int bit64; @@ -658,6 +654,22 @@ int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin) else ctx->xdr = &xdr_le; + /* Obtain the string table. */ + for (i = 0; i < pelf->ehdr.e_shnum; i++) { + if (pelf->strtabs[i] == NULL) + continue; + /* Don't use the section headers' string table. */ + if (i == pelf->ehdr.e_shstrndx) + continue; + ctx->strtab = buffer_get(pelf->strtabs[i]); + break; + } + + if (ctx->strtab == NULL) { + ERROR("No string table found.\n"); + return -1; + } + if (find_program_segment(ctx)) goto out; diff --git a/util/cbfstool/rmodule.h b/util/cbfstool/rmodule.h index a62562b98f..ec0971e27e 100644 --- a/util/cbfstool/rmodule.h +++ b/util/cbfstool/rmodule.h @@ -29,6 +29,8 @@ struct rmod_context { struct parsed_elf pelf; /* Program segment. */ Elf64_Phdr *phdr; + /* Symbol string table. */ + char *strtab; /* Collection of relocation addresses fixup in the module. */ Elf64_Xword nrelocs; |