diff --git a/src/elf_check_elf.h b/src/elf_check_elf.h index ae8eaa37d757553a61cb0609c6fa025412aab11d..2e06f1a701265ecda74a6eab638505be71b667e5 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 4265db9e9ed6762a242ac5953d83aacecc78dadc..43f553df816b352140cd45dfa73b77936be36f8b 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 27c7de5c8bc0d7439474318c04a2811f239ceb55..3380b7354281ec20e640364e3157725c0ae59ecf 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 eb8b1b9cbb62f6e7e37cd8f659b8a72b3569986a..9a1b2ed0935c2dce687f7d5c88bd06196f130ee6 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 84d97abecd2a78c7b0181a778dc2c529cce778fb..56dca8faa46b561bcd92ac4c299bb199f20ecb70 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 05a248f3d344a4fa1d69d658d8d425318c360073..b066aade0b68dac0ece14745bb3370846653e287 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 97e884c902b333c9cabe667315b28ab1cd77611a..d4b09fc36cc8ebce37825a636f85850fee229cb0 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 dbf5ed018ff8016051cfa866717e3a6942f7a0eb..c0e5fbfbe3cf87fa3cf02974d9462256d11857eb 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 2a7e92223395b07f55ccbb908cfae7973a47f403..41945be1843634b85c2ffa63add2fa30ef989d8e 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 9cf65be2b9a6470aec69d94f948f0543febe2d5d..dc6d2d7644577dd001c065556717dc3a0a006b89 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 2974f6de58505c38855b66a1a422b87c0c139465..250328ee4cd8d1b158345d927e40c2929e5a55a8 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 b59660f3a56936c6ea4cc33a000b6c0d4dde772d..9b623ec4afa100e68f05c4b6209f931ad608f9af 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 cc8854167c8ac7cb42fcc196dec37b327ebdcf1d..2f68e147b9d1ef858370923917b66b6a347fa970 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 3467340c2bbfe7fbf3ab5dc04bc2db5f4ec1d693..f53bbcd8df4ea18aa7ff39c58fd71774fb146128 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