From 9c7fb2280125645c10e53df0a1d9f057ad66d7cb Mon Sep 17 00:00:00 2001 From: Zhou Kang Date: Wed, 9 Aug 2023 13:38:15 +0000 Subject: [PATCH] fix NULL sec and dynamic addr --- src/elf_link_elf.c | 13 +++++++++++-- src/elf_write_elf.c | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/elf_link_elf.c b/src/elf_link_elf.c index d8a2cb1..459be9e 100644 --- a/src/elf_link_elf.c +++ b/src/elf_link_elf.c @@ -706,6 +706,8 @@ static void dynamic_copy_dyn(elf_link_t *elf_link, elf_file_t *src_ef, Elf64_Dyn case DT_VERDEF: case DT_VERNEED: case DT_VERSYM: + case DT_GNU_HASH: + case DT_SYMTAB: dst_dyn->d_un.d_val = get_new_addr_by_old_addr(elf_link, src_ef, src_dyn->d_un.d_val); break; case DT_VERDEFNUM: @@ -746,6 +748,8 @@ static unsigned long libc_dt_arr[] = { DT_VERNEED, DT_VERNEEDNUM, DT_VERSYM, + DT_GNU_HASH, + DT_SYMTAB, }; #define LIBC_DT_ARR_LEN (sizeof(libc_dt_arr) / sizeof(libc_dt_arr[0])) @@ -824,11 +828,16 @@ static int dynamic_copy_obj(elf_link_t *elf_link, Elf64_Dyn *begin_dyn, int len) // fix name index new_d_val = get_new_name_offset(elf_link, ef, ef->dynstr_sec, dyn->d_un.d_val); break; + case DT_GNU_HASH: + case DT_SYMTAB: + if (is_static_nold_mode(elf_link)) { + // have done before + continue; + } + fallthrough; case DT_INIT: case DT_FINI: - case DT_GNU_HASH: case DT_STRTAB: - case DT_SYMTAB: case DT_PLTGOT: case DT_RELA: new_d_val = get_new_addr_by_old_addr(elf_link, ef, dyn->d_un.d_val); diff --git a/src/elf_write_elf.c b/src/elf_write_elf.c index cd8ec89..ade6a22 100644 --- a/src/elf_write_elf.c +++ b/src/elf_write_elf.c @@ -440,6 +440,9 @@ static void merge_section(elf_link_t *elf_link, Elf64_Shdr *dst_sec, elf_file_t static Elf64_Shdr *merge_ef_section_by_name(elf_link_t *elf_link, elf_file_t *ef, const char *sec_name) { Elf64_Shdr *sec = elf_find_section_by_name(ef, sec_name); + if (sec == NULL) { + return NULL; + } Elf64_Shdr *dst_sec = add_tmp_section(elf_link, ef, sec); if (dst_sec == NULL) { return NULL; -- Gitee