From faeab33375a694c8ade4163866853df2971d1d39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 20 Jan 2023 19:39:54 +0100 Subject: [PATCH] Allow for ELF files without program headers e.g. unlinked object files. --- src/elfpatch.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/elfpatch.c b/src/elfpatch.c index 3d4b9ae..4248c00 100644 --- a/src/elfpatch.c +++ b/src/elfpatch.c @@ -290,6 +290,12 @@ static int elf_patch_read_program_headers(elfpatch_handle_t *ep) return -1; } + if (header_count == 0) { + /* No program headers found. This ELF file is probably not linked */ + ep->program_headers_count = 0; + return 0; + } + ep->program_headers = (GElf_Phdr *)malloc(header_count * sizeof(GElf_Phdr)); if (!ep->program_headers) { /* Mem error. Abort. Program will crash eventually */ @@ -335,9 +341,11 @@ static void resolve_section_lmas(elfpatch_handle_t *ep) if (!sec) continue; + /* By default each sections LMA is assumed to be its LMA as well */ + sec->lma = (uint64_t)sec->section_header.sh_addr; + if (sec->section_header.sh_type == SHT_NOBITS) { /* Section does not contain data. It may be allocated but is not loaded. Therefore, LMA=VMA. */ - sec->lma = (uint64_t)sec->section_header.sh_addr; continue; }