From db9c19faa4e3c1f6432e251b2153a91061403132 Mon Sep 17 00:00:00 2001 From: l00580197 Date: Fri, 7 Jul 2023 19:40:22 +0800 Subject: [PATCH] fix codecheck for elf_relocation elf_write Signed-off-by: l00580197 --- src/elf_relocation.c | 20 ++++++++++++++++---- src/elf_relocation.h | 13 ++++++++++++- src/elf_relocation_aarch64.c | 18 ++++++++++++------ src/elf_relocation_x86_64.c | 31 ++++++++++++++++++++++++------- src/elf_write_elf.c | 19 ++++++++++++++++--- src/elf_write_elf.h | 13 ++++++++++++- src/main.c | 13 ++++++++++++- 7 files changed, 104 insertions(+), 23 deletions(-) diff --git a/src/elf_relocation.c b/src/elf_relocation.c index 040125b..a5fb245 100644 --- a/src/elf_relocation.c +++ b/src/elf_relocation.c @@ -1,4 +1,15 @@ -/* SPDX-License-Identifier: MulanPSL-2.0 */ +// Copyright (c) 2023 Huawei Technologies Co.,Ltd. All rights reserved. +// +// sysboost is licensed under Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan +// PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +// See the Mulan PSL v2 for more details. + #include #include #include @@ -9,11 +20,12 @@ #include #include -#include "elf_link_common.h" -#include "elf_relocation.h" #include #include +#include "elf_link_common.h" +#include "elf_relocation.h" + #define BYTES_NOP1 0x90 #define INDIRECT_CALL_INSN_OP_SIZE 2 @@ -25,7 +37,7 @@ #define JMP32_INSN_OPCODE 0xE9 #define MAX_INSN_OFFSET 2147483647L -#define MIN_INSN_OFFSET -2147483648L +#define MIN_INSN_OFFSET (-2147483648L) static void modify_local_call_sec(elf_link_t *elf_link, elf_file_t *ef, Elf64_Shdr *sec) { diff --git a/src/elf_relocation.h b/src/elf_relocation.h index 5e09c25..184ede6 100644 --- a/src/elf_relocation.h +++ b/src/elf_relocation.h @@ -1,4 +1,15 @@ -/* SPDX-License-Identifier: MulanPSL-2.0 */ +// Copyright (c) 2023 Huawei Technologies Co.,Ltd. All rights reserved. +// +// sysboost is licensed under Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan +// PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +// See the Mulan PSL v2 for more details. + #ifndef _ELF_RELOCATION_H #define _ELF_RELOCATION_H diff --git a/src/elf_relocation_aarch64.c b/src/elf_relocation_aarch64.c index ad95d0b..acd21e3 100644 --- a/src/elf_relocation_aarch64.c +++ b/src/elf_relocation_aarch64.c @@ -1026,8 +1026,9 @@ void correct_stop_libc_atexit(elf_link_t *elf_link) } old_ldr_addr = rela->r_offset; } - if (!old_ldr_addr) + if (!old_ldr_addr) { si_panic("%s, didn't find __stop___libc_atexit symbol\n", __func__); + } unsigned binary = elf_read_u32(template_ef, old_ldr_addr); unsigned ldr_Rn = get_ldr_Rn(binary); @@ -1035,13 +1036,16 @@ void correct_stop_libc_atexit(elf_link_t *elf_link) unsigned long old_adrp_addr = 0; for (unsigned long addr = start; addr < end; addr += ARM64_INSN_LEN) { binary = elf_read_u32(template_ef, addr); - if (!is_adrp_instruction(binary)) + if (!is_adrp_instruction(binary)) { continue; + } unsigned adrp_Rd = get_adrp_Rd(binary); - if (adrp_Rd != ldr_Rn) + if (adrp_Rd != ldr_Rn) { continue; - if (old_adrp_addr) + } + if (old_adrp_addr) { si_panic("%s, found 2 matched adrp in __run_exit_handlers()\n", __func__); + } old_adrp_addr = addr; } if (!old_adrp_addr) @@ -1063,14 +1067,16 @@ void correct_stop_libc_atexit(elf_link_t *elf_link) bool found = false; for (unsigned i = 0; i < len; i++) { Elf64_Rela *rela = &relas[i]; - if (rela->r_offset != got_addr) + if (rela->r_offset != got_addr) { continue; + } Elf64_Sym *sym = elf_find_symbol_by_name(out_ef, "__stop___libc_atexit"); rela->r_addend = sym->st_value; SI_LOG_DEBUG("change .rela.dyn 0x%lx's value to 0x%lx\n", rela->r_offset, sym->st_value); found = true; } - if (!found) + if (!found) { si_panic("didn't find corresponding rela entry in .rela.dyn\n"); + } } diff --git a/src/elf_relocation_x86_64.c b/src/elf_relocation_x86_64.c index 42b74f0..bbcbb86 100644 --- a/src/elf_relocation_x86_64.c +++ b/src/elf_relocation_x86_64.c @@ -1,4 +1,15 @@ -/* SPDX-License-Identifier: MulanPSL-2.0 */ +// Copyright (c) 2023 Huawei Technologies Co.,Ltd. All rights reserved. +// +// sysboost is licensed under Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan +// PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +// See the Mulan PSL v2 for more details. + #include #include #include @@ -28,6 +39,8 @@ #define POKE_MAX_OPCODE_SIZE 10 +#define INDEX_FIVE 5 + union text_poke_insn { unsigned char text[POKE_MAX_OPCODE_SIZE]; struct { @@ -81,7 +94,7 @@ static int modify_insn_direct_jmp(elf_link_t *elf_link, elf_file_t *ef, Elf64_Re } insn->opcode = CALL_INSN_OPCODE; insn->disp = disp; - insn->text[5] = BYTES_NOP1; + insn->text[INDEX_FIVE] = BYTES_NOP1; return 0; } @@ -294,8 +307,9 @@ int modify_local_call_rela(elf_link_t *elf_link, elf_file_t *ef, Elf64_Rela *rel // call func use got, change to direct jump // ff 15 00 00 00 00 callq *0x00(%rip) ret = modify_insn_direct_jmp(elf_link, ef, rela, sym); - if (ret == 0) + if (ret == 0) { break; + } // data var, just change offset // 48 83 3d d2 fe 5f 00 cmpq $0x0,0x5ffed2(%rip) @@ -339,6 +353,8 @@ static void clear_rela(Elf64_Rela *dst_rela) // TODO: bug, R_X86_64_NONE can not in .rela.plt } +#define ADDRESS_OF_FOUR_BYTES 4 +#define ADDRESS_OF_SIX_BYTES 6 void modify_rela_plt(elf_link_t *elf_link, si_array_t *arr) { int len = arr->len; @@ -383,13 +399,13 @@ void modify_rela_plt(elf_link_t *elf_link, si_array_t *arr) // 68 00 00 00 00 pushq $0x0 // e9 e0 ff ff ff jmpq 200020 <.plt> // change jmp insn offset to new - modify_insn_offset(elf_link, new_plt_addr - 4, (unsigned long)dst_rela->r_offset, -4); + modify_insn_offset(elf_link, new_plt_addr - ADDRESS_OF_FOUR_BYTES, (unsigned long)dst_rela->r_offset, -1 * ADDRESS_OF_FOUR_BYTES); // change sym index, pushq has 1 Byte cmd // index of .rela.plt elf_write_value(out_ef, new_plt_addr + 1, &i, sizeof(unsigned int)); // relative jump to begin of .plt // pushq has 5 Byte, jmpq has 1 Byte cmd - elf_write_jmp_addr(out_ef, new_plt_addr + 6, new_plt_start_addr); + elf_write_jmp_addr(out_ef, new_plt_addr + ADDRESS_OF_SIX_BYTES, new_plt_start_addr); } if (is_share_mode(elf_link) == false) @@ -410,13 +426,14 @@ void modify_plt_got(elf_link_t *elf_link) // ff 25 82 ff 5f 00 jmp *0x5fff82(%rip) # 7ffff8 <__cxa_finalize> Elf64_Shdr *sec = elf_find_section_by_name(ef, ".plt.got"); - if (!sec) + if (!sec) { return; + } unsigned long loc = sec->sh_offset; // insn have 2 op code, direct value have 4 Byte loc = loc + 2; - modify_insn_data_offset(elf_link, ef, loc, -4); + modify_insn_data_offset(elf_link, ef, loc, -1 * ADDRESS_OF_FOUR_BYTES); } void correct_stop_libc_atexit(elf_link_t *elf_link) diff --git a/src/elf_write_elf.c b/src/elf_write_elf.c index 22577a6..10bb3ac 100644 --- a/src/elf_write_elf.c +++ b/src/elf_write_elf.c @@ -1,4 +1,15 @@ -/* SPDX-License-Identifier: MulanPSL-2.0 */ +// Copyright (c) 2023 Huawei Technologies Co.,Ltd. All rights reserved. +// +// sysboost is licensed under Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan +// PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +// See the Mulan PSL v2 for more details. + #include #include #include @@ -129,8 +140,9 @@ int create_elf_file(char *file_name, elf_file_t *elf_file) elf_file->file_name = strdup(file_name); lseek(fd, len - 1, SEEK_SET); ret = write(fd, "", 1); - if (ret == -1UL) + if (ret == -1UL) { si_panic("%s write fail\n", __func__); + } elf_file->hdr = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (elf_file->hdr == MAP_FAILED) { @@ -147,6 +159,7 @@ void truncate_elf_file(elf_link_t *elf_link) { elf_file_t *out_ef = &elf_link->out_ef; int ret = ftruncate(out_ef->fd, elf_link->next_file_offset); - if (ret == -1) + if (ret == -1) { si_panic("%s ftruncate fail\n", __func__); + } } diff --git a/src/elf_write_elf.h b/src/elf_write_elf.h index 0a145cb..f3ffd2d 100644 --- a/src/elf_write_elf.h +++ b/src/elf_write_elf.h @@ -1,4 +1,15 @@ -/* SPDX-License-Identifier: MulanPSL-2.0 */ +// Copyright (c) 2023 Huawei Technologies Co.,Ltd. All rights reserved. +// +// sysboost is licensed under Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan +// PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +// See the Mulan PSL v2 for more details. + #ifndef _ELF_WRITE_ELF_H #define _ELF_WRITE_ELF_H diff --git a/src/main.c b/src/main.c index cde1e5a..5ff7037 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,15 @@ -/* SPDX-License-Identifier: MulanPSL-2.0 */ +// Copyright (c) 2023 Huawei Technologies Co.,Ltd. All rights reserved. +// +// sysboost is licensed under Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan +// PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +// See the Mulan PSL v2 for more details. + #include #include #include -- Gitee