Implement elf_load_inplace() to apply dynamic relocations to an ELF binary that is already loaded in memory. Unlike elf_load(), this function does not allocate memory or copy segments - it only modifies the existing image in place.
This is useful for self-relocating loaders or when the ELF has been loaded by external means (e.g., firmware or another bootloader). For ET_DYN (position-independent) binaries, the relocation offset is calculated relative to the first executable PT_LOAD segment (.text section), taking into account the difference between the segment's virtual address and its file offset. The entry point is also adjusted to point to the relocated image. Signed-off-by: Sascha Hauer <[email protected]> Co-Authored-By: Claude Sonnet 4.5 <[email protected]> --- common/elf.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/elf.h | 8 ++++ 2 files changed, 160 insertions(+) diff --git a/common/elf.c b/common/elf.c index fc2949c285ebb0c0740c68c551926da8d0bb8637..565b283b694773727ef77917cfd8c1d4ee83a8d1 100644 --- a/common/elf.c +++ b/common/elf.c @@ -531,3 +531,155 @@ void elf_close(struct elf_image *elf) free(elf); } + +static void *elf_find_dynamic_inplace(struct elf_image *elf) +{ + void *buf = elf->hdr_buf; + void *phdr = buf + elf_hdr_e_phoff(elf, buf); + int i; + + for (i = 0; i < elf_hdr_e_phnum(elf, buf); i++) { + if (elf_phdr_p_type(elf, phdr) == PT_DYNAMIC) { + u64 offset = elf_phdr_p_offset(elf, phdr); + /* For in-place binary, PT_DYNAMIC is at hdr_buf + offset */ + return elf->hdr_buf + offset; + } + phdr += elf_size_of_phdr(elf); + } + + return NULL; /* No PT_DYNAMIC segment */ +} + +/** + * elf_load_inplace() - Apply dynamic relocations to an ELF binary in place + * @elf: ELF image previously opened with elf_open_binary() + * + * This function applies dynamic relocations to an ELF binary that is already + * loaded at its target address in memory. Unlike elf_load(), this does not + * allocate memory or copy segments - it only modifies the existing image. + * + * This is useful for self-relocating loaders or when the ELF has been loaded + * by external means (e.g., loaded by firmware or another bootloader). + * + * The ELF image must have been previously opened with elf_open_binary(). + * + * For ET_DYN (position-independent) binaries, the relocation offset is + * calculated relative to the first executable PT_LOAD segment (.text section). + * + * For ET_EXEC binaries, no relocation is applied as they are expected to + * be at their link-time addresses. + * + * Returns: 0 on success, negative error code on failure + */ +int elf_load_inplace(struct elf_image *elf) +{ + void *dyn_seg; + void *buf, *phdr; + void *elf_buf; + int i, ret; + + buf = elf->hdr_buf; + elf_buf = elf->hdr_buf; + + /* + * First pass: Clear BSS segments (p_memsz > p_filesz). + * This must be done before relocations as uninitialized data + * must be zeroed per C standard. + */ + phdr = buf + elf_hdr_e_phoff(elf, buf); + for (i = 0; i < elf_hdr_e_phnum(elf, buf); i++) { + if (elf_phdr_p_type(elf, phdr) == PT_LOAD) { + u64 p_offset = elf_phdr_p_offset(elf, phdr); + u64 p_filesz = elf_phdr_p_filesz(elf, phdr); + u64 p_memsz = elf_phdr_p_memsz(elf, phdr); + + /* Clear BSS (uninitialized data) */ + if (p_filesz < p_memsz) { + void *bss_start = elf_buf + p_offset + p_filesz; + size_t bss_size = p_memsz - p_filesz; + memset(bss_start, 0x00, bss_size); + } + } + phdr += elf_size_of_phdr(elf); + } + + /* + * Calculate relocation offset for the in-place binary. + * For ET_DYN, we need to find the first executable PT_LOAD segment + * (.text section) and use it as the relocation base. + */ + if (elf->type == ET_DYN) { + u64 text_vaddr = 0; + u64 text_offset = 0; + bool found_text = false; + + /* Find first executable PT_LOAD segment (.text) */ + phdr = buf + elf_hdr_e_phoff(elf, buf); + for (i = 0; i < elf_hdr_e_phnum(elf, buf); i++) { + if (elf_phdr_p_type(elf, phdr) == PT_LOAD) { + u32 flags = elf_phdr_p_flags(elf, phdr); + /* Check if segment is executable (PF_X = 0x1) */ + if (flags & PF_X) { + text_vaddr = elf_phdr_p_vaddr(elf, phdr); + text_offset = elf_phdr_p_offset(elf, phdr); + found_text = true; + break; + } + } + phdr += elf_size_of_phdr(elf); + } + + if (!found_text) { + pr_err("No executable PT_LOAD segment found\n"); + ret = -EINVAL; + goto out; + } + + /* + * Calculate relocation offset relative to .text section: + * - .text is at file offset text_offset, so in memory at: elf_buf + text_offset + * - .text has virtual address text_vaddr + * - reloc_offset = (actual .text address) - (virtual .text address) + */ + elf->reloc_offset = ((unsigned long)elf_buf + text_offset) - text_vaddr; + + pr_debug("In-place ELF relocation: text_vaddr=0x%llx, text_offset=0x%llx, " + "load_addr=%p, offset=0x%08lx\n", + text_vaddr, text_offset, elf_buf, elf->reloc_offset); + + /* Adjust entry point to point to relocated image */ + elf->entry += elf->reloc_offset; + } else { + /* + * ET_EXEC binaries are at their link-time addresses, + * no relocation needed + */ + elf->reloc_offset = 0; + } + + /* Find PT_DYNAMIC segment */ + dyn_seg = elf_find_dynamic_inplace(elf); + if (!dyn_seg) { + /* + * No PT_DYNAMIC segment found. + * This is fine for statically-linked binaries or + * binaries without relocations. + */ + pr_debug("No PT_DYNAMIC segment found\n"); + ret = 0; + goto out; + } + + /* Apply architecture-specific relocations */ + ret = elf_apply_relocations(elf, dyn_seg); + if (ret) { + pr_err("In-place relocation failed: %d\n", ret); + goto out; + } + + pr_debug("In-place ELF relocation completed successfully\n"); + return 0; + +out: + return ret; +} diff --git a/include/elf.h b/include/elf.h index 4b19c917639d0d655d9f7530f0311afa599f1a98..5b1d75c14ce198292b052159de310ac14c3bd2d9 100644 --- a/include/elf.h +++ b/include/elf.h @@ -422,6 +422,14 @@ int elf_load(struct elf_image *elf); */ void elf_set_load_address(struct elf_image *elf, void *addr); +/* + * Apply dynamic relocations to an ELF binary already loaded in memory. + * This modifies the ELF image in place without allocating new memory. + * Useful for self-relocating loaders or externally loaded binaries. + * The elf parameter must have been previously opened with elf_open_binary(). + */ +int elf_load_inplace(struct elf_image *elf); + /* * Architecture-specific relocation handler. * Returns 0 on success, -ENOSYS if architecture doesn't support relocations, -- 2.47.3
