From a1652e96ea898836478cad898427ac9ed93546b8 Mon Sep 17 00:00:00 2001 From: Tie Liu Date: Tue, 13 Jun 2023 15:56:47 +0800 Subject: [PATCH] use getopt_long --- src/elf_check_elf.h | 2 +- src/elf_hugepage.c | 4 +- src/elf_link_common.c | 6 +- src/elf_link_common.h | 6 +- src/elf_link_elf.c | 4 +- src/elf_link_elf.h | 4 +- src/elf_read_elf.c | 2 +- src/elf_relocation.c | 4 +- src/elf_relocation_aarch64.c | 4 +- src/elf_relocation_x86_64.c | 4 +- src/elf_write_elf.c | 6 +- src/libhook/meson.build | 3 +- src/main.c | 108 ++++++++++++++++++----------------- src/meson.build | 9 +-- 14 files changed, 86 insertions(+), 80 deletions(-) diff --git a/src/elf_check_elf.h b/src/elf_check_elf.h index ae8eaa3..2e06f1a 100644 --- a/src/elf_check_elf.h +++ b/src/elf_check_elf.h @@ -6,7 +6,7 @@ #include #include "elf_link_common.h" -#include "si_common.h" +#include void elf_check_elf(elf_link_t *elf_link); diff --git a/src/elf_hugepage.c b/src/elf_hugepage.c index 4265db9..43f553d 100644 --- a/src/elf_hugepage.c +++ b/src/elf_hugepage.c @@ -11,8 +11,8 @@ #include #include "elf_link_common.h" -#include "si_debug.h" -#include "si_log.h" +#include +#include #ifndef PF_HUGEPAGE #define PF_HUGEPAGE (0x01000000) diff --git a/src/elf_link_common.c b/src/elf_link_common.c index 27c7de5..3380b73 100644 --- a/src/elf_link_common.c +++ b/src/elf_link_common.c @@ -12,9 +12,9 @@ #include #include "elf_link_common.h" -#include "si_common.h" -#include "si_debug.h" -#include "si_log.h" +#include +#include +#include static char *special_dynsyms[] = { "__pointer_chk_guard", diff --git a/src/elf_link_common.h b/src/elf_link_common.h index eb8b1b9..9a1b2ed 100644 --- a/src/elf_link_common.h +++ b/src/elf_link_common.h @@ -7,9 +7,9 @@ #include #include "elf_read_elf.h" -#include "si_array.h" -#include "si_common.h" -#include "si_debug.h" +#include +#include +#include #define ELF_SEGMENT_ALIGN (0x200000) diff --git a/src/elf_link_elf.c b/src/elf_link_elf.c index 84d97ab..56dca8f 100644 --- a/src/elf_link_elf.c +++ b/src/elf_link_elf.c @@ -16,8 +16,8 @@ #include "elf_write_elf.h" #include "elf_link_elf.h" #include "si_array.h" -#include "si_debug.h" -#include "si_log.h" +#include +#include #ifdef __aarch64__ #define LD_SO_STATIC_TEMPLATE "/lib/ld-linux-aarch64.so.1" diff --git a/src/elf_link_elf.h b/src/elf_link_elf.h index 05a248f..b066aad 100644 --- a/src/elf_link_elf.h +++ b/src/elf_link_elf.h @@ -4,8 +4,8 @@ #include "elf_link_common.h" #include "elf_read_elf.h" -#include "si_array.h" -#include "si_common.h" +#include +#include elf_link_t *elf_link_new(void); int elf_link_set_mode(elf_link_t *elf_link, unsigned int mode); diff --git a/src/elf_read_elf.c b/src/elf_read_elf.c index 97e884c..d4b09fc 100644 --- a/src/elf_read_elf.c +++ b/src/elf_read_elf.c @@ -13,7 +13,7 @@ #include #include "elf_read_elf.h" -#include "si_common.h" +#include #include #include diff --git a/src/elf_relocation.c b/src/elf_relocation.c index dbf5ed0..c0e5fbf 100644 --- a/src/elf_relocation.c +++ b/src/elf_relocation.c @@ -11,8 +11,8 @@ #include "elf_link_common.h" #include "elf_relocation.h" -#include "si_debug.h" -#include "si_log.h" +#include +#include #define BYTES_NOP1 0x90 diff --git a/src/elf_relocation_aarch64.c b/src/elf_relocation_aarch64.c index 2a7e922..41945be 100644 --- a/src/elf_relocation_aarch64.c +++ b/src/elf_relocation_aarch64.c @@ -11,8 +11,8 @@ #include "elf_link_common.h" #include "elf_write_elf.h" -#include "si_debug.h" -#include "si_log.h" +#include +#include #define unlikely(x) __builtin_expect((x), 0) diff --git a/src/elf_relocation_x86_64.c b/src/elf_relocation_x86_64.c index 9cf65be..dc6d2d7 100644 --- a/src/elf_relocation_x86_64.c +++ b/src/elf_relocation_x86_64.c @@ -10,8 +10,8 @@ #include #include "elf_link_common.h" -#include "si_debug.h" -#include "si_log.h" +#include +#include #define BYTES_NOP1 0x90 diff --git a/src/elf_write_elf.c b/src/elf_write_elf.c index 2974f6d..250328e 100644 --- a/src/elf_write_elf.c +++ b/src/elf_write_elf.c @@ -14,9 +14,9 @@ #include "elf_link_common.h" #include "elf_read_elf.h" -#include "si_common.h" -#include "si_debug.h" -#include "si_log.h" +#include +#include +#include unsigned int elf_align_file(elf_link_t *elf_link, unsigned int align) { diff --git a/src/libhook/meson.build b/src/libhook/meson.build index b59660f..9b623ec 100644 --- a/src/libhook/meson.build +++ b/src/libhook/meson.build @@ -3,7 +3,7 @@ sources = files('dlsym_hook.c') cflags += ['-v'] -ldflags += ['-ldl'] +ldflags += ['-ldl', '-Wl,--whole-archive', '/usr/lib64/libsi_sys.a', '-Wl,--no-whole-archive'] includes = [] deps = [] @@ -12,6 +12,5 @@ libhook = shared_library('hook', sources, install: true, c_args: cflags, link_args: ldflags, - link_with: link_libs, dependencies: deps, include_directories: includes) diff --git a/src/main.c b/src/main.c index cc88541..2f68e14 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,10 @@ /* SPDX-License-Identifier: MulanPSL-2.0 */ #include #include +#include #include +#include +#include #include #include #include @@ -10,91 +13,93 @@ #include #include -#include "elf_read_elf.h" -#include "elf_link_elf.h" -#include "si_debug.h" -#include "si_log.h" #include "elf_hugepage.h" +#include "elf_link_elf.h" +#include "elf_read_elf.h" int main(int argc, char *argv[]) { int ret = 0; char tmp[PATH_MAX]; elf_link_t *elf_link = elf_link_new(); - int cur_arg; char *str_ret; + bool debug = false; + bool static_mode = false; + bool static_nolibc_mode = false; - if (!elf_link) { - SI_LOG_ERR("malloc fail\n"); - return -1; - } + static struct option long_options[] = { + {"debug", no_argument, NULL, 'd'}, + {"set", required_argument, NULL, 's'}, + {"unset", required_argument, NULL, 'u'}, + {"hook", no_argument, NULL, 'h'}, + {"static", no_argument, NULL, 'S'}, + {"static-nolibc", no_argument, NULL, 'N'}, + {NULL, 0, NULL, 0}}; - if (argc == 1) { - SI_LOG_ERR("nothing to do\n"); - return -1; + int option_index = 0; + int c; + while ((c = getopt_long(argc, argv, "ds:u:hSN", long_options, &option_index)) != -1) { + switch (c) { + case 'd': + debug = true; + break; + case 's': + str_ret = realpath(optarg, tmp); + if (!str_ret) { + SI_LOG_ERR("get realpath fail: %s\n", optarg); + return -1; + } + return elf_set_aot(tmp, true); + case 'u': + str_ret = realpath(optarg, tmp); + if (!str_ret) { + SI_LOG_ERR("get realpath fail: %s\n", optarg); + return -1; + } + return elf_set_aot(tmp, false); + case 'h': + elf_link->hook_func = true; + SI_LOG_INFO("hook func\n"); + break; + case 'S': + static_mode = true; + break; + case 'N': + static_nolibc_mode = true; + break; + default: + return -1; + } } - // arg0 is program name, parameter is from arg1 - cur_arg = 1; - - if (strcmp(argv[cur_arg], "-debug") == 0) { + if (debug) { si_log_set_global_level(SI_LOG_LEVEL_DEBUG); - cur_arg++; } else { si_log_set_global_level(SI_LOG_LEVEL_INFO); } - if ((strcmp(argv[cur_arg], "-set") == 0) || (strcmp(argv[cur_arg], "-unset") == 0)) { - if (cur_arg + 1 >= argc) { - SI_LOG_ERR("need file path\n"); - return -1; - } - str_ret = realpath(argv[cur_arg + 1], tmp); - if (!str_ret) { - SI_LOG_ERR("get realpath fail: %s\n", argv[cur_arg + 1]); - return -1; - } - - bool state = true; - if ((strcmp(argv[cur_arg], "-unset") == 0)) { - state = false; - } - - return elf_set_aot(tmp, state); - } - - if (strcmp(argv[cur_arg], "--hook") == 0) { - elf_link->hook_func = true; - cur_arg++; - SI_LOG_INFO("hook func\n"); - } - - // -static parameter is used to determine whether static file generated - if (strcmp(argv[cur_arg], "-static") == 0) { + if (static_mode) { ret = elf_link_set_mode(elf_link, ELF_LINK_STATIC); if (ret < 0) { return -1; } - cur_arg++; SI_LOG_INFO("static mode\n"); - } else if (strcmp(argv[cur_arg], "-static-nolibc") == 0) { + } else if (static_nolibc_mode) { ret = elf_link_set_mode(elf_link, ELF_LINK_STATIC_NOLIBC); if (ret < 0) { return -1; } - cur_arg++; SI_LOG_INFO("static-nolibc mode\n"); } - for (int i = cur_arg; i < argc; i++) { - if (*argv[i] == '\0') { + for (int i = optind; i < argc; i++) { + if (*argv[i] == '0') { continue; } - str_ret = realpath(argv[i], tmp); if (!str_ret) { SI_LOG_ERR("get realpath fail: %s\n", argv[i]); - return -1; + return -1; } elf_file_t *ef = elf_link_add_infile(elf_link, tmp); if (ef == NULL) { @@ -111,3 +116,4 @@ int main(int argc, char *argv[]) SI_LOG_INFO("OK\n"); return 0; } + diff --git a/src/meson.build b/src/meson.build index 3467340..f53bbcd 100644 --- a/src/meson.build +++ b/src/meson.build @@ -15,8 +15,7 @@ default_cflags = machine_args + ['-Wno-pointer-arith', '-Wno-pedantic'] default_ldflags = lk_args ext_deps = [] -deps = static_link_libraries + ['libsi_sys.a', 'libsi_array.a'] -link_libs = si_static_libraries +deps = static_link_libraries if get_option('buildtype').contains('debug') default_cflags += [ '-pedantic', '-DPEDANTIC', '-Og', '-g', '-rdynamic' ] @@ -39,6 +38,9 @@ foreach tool:tools subdir(name) endforeach +default_ldflags += ['-Wl,--whole-archive', '/usr/lib64/libsi_sys.a'] +default_ldflags += ['-Wl,--whole-archive', '/usr/lib64/libsi_array.a'] +default_ldflags += ['-Wl,--whole-archive', '/usr/lib64/liblzma.a', '-Wl,--no-whole-archive'] core_sources = files([ 'main.c', @@ -58,7 +60,6 @@ cflags += ['-fpic', '-pie'] executable('sysboost', core_sources, install: true, c_args: default_cflags, - link_args: default_ldflags + ['-Wl,--whole-archive', '/usr/lib64/liblzma.a', '-Wl,--no-whole-archive'], - link_with: link_libs, + link_args: default_ldflags, dependencies: deps, include_directories: includes) \ No newline at end of file -- Gitee