From 845d08ee3a258c852dcc42c6b29d01ce05c2d4e3 Mon Sep 17 00:00:00 2001 From: flysand7 Date: Fri, 4 Aug 2023 05:46:05 +1100 Subject: [PATCH] Hide dbg_printfs behind a define --- build.py | 2 +- src/loader/loader-self-reloc.c | 70 ++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/build.py b/build.py index 58f0ed7..57fcfe1 100755 --- a/build.py +++ b/build.py @@ -273,7 +273,7 @@ if target == 'linux': print_step(f'Compiling {dl_lib}\n') assemble('src/loader/loader-entry.asm', 'bin/loader-entry.o') compile(['bin/loader-entry.o', 'src/loader/loader-self-reloc.c', 'src/loader/loader.c'], dl_lib, - '-shared -nostdlib -Wl,-e,_dlstart -Wl,--sort-section,alignment -Wl,--sort-common -Wl,--gc-sections -Wl,--hash-style=both -Wl,--no-undefined -Wl,--exclude-libs=ALL -fno-stack-protector') + '-ffreestanding -shared -nostdlib -Wl,-e,_dlstart -Wl,--sort-section,alignment -Wl,--sort-common -Wl,--gc-sections -Wl,--hash-style=both -Wl,--no-undefined -Wl,--exclude-libs=ALL -fno-stack-protector') print_step(f'Compiling {crt_file}\n') if target == 'linux': diff --git a/src/loader/loader-self-reloc.c b/src/loader/loader-self-reloc.c index cff616d..0a71c0f 100644 --- a/src/loader/loader-self-reloc.c +++ b/src/loader/loader-self-reloc.c @@ -16,30 +16,27 @@ #include #include #include +#include #define AUX_CNT 32 #define DYN_CNT 37 -#include -static void _dbg_print_char(char c) { - sys_write(STDOUT_FILENO, &c, 1); +static void print_string_n(char *str, u64 len) { + sys_write(STDOUT_FILENO, str, len); } - -static void _dbg_print_string(char *str) { +static void print_char(char c) { + print_string_n(&c, 1); +} +static void print_string(char *str) { int str_len = 0; while(str[str_len] != 0) { str_len += 1; } - sys_write(STDOUT_FILENO, str, str_len); + print_string_n(str, str_len); } - -static void _dbg_print_string_n(char *str, u64 len) { - sys_write(STDOUT_FILENO, str, len); -} - -static void _dbg_print_int(i64 number) { +static void print_int(i64 number) { if(number < 0) { - _dbg_print_char('-'); + print_char('-'); number = -number; } char buf[20]; @@ -49,10 +46,9 @@ static void _dbg_print_int(i64 number) { *--p = (number%10) + '0'; number /= 10; } while(number > 0); - _dbg_print_string(p); + print_string(p); } - -static void _dbg_print_uint(u64 number) { +static void print_uint(u64 number) { char buf[20]; buf[19] = 0; char *p = buf + sizeof buf - 1; @@ -60,11 +56,10 @@ static void _dbg_print_uint(u64 number) { *--p = (number%10) + '0'; number /= 10; } while(number > 0); - _dbg_print_string(p); + print_string(p); } - -static void _dbg_print_hex(u64 number) { - // _dbg_print_string("0x"); +static void print_hex(u64 number) { + // print_string("0x"); char digits[] = "0123456789abcdef"; char buf[20]; buf[19] = 0; @@ -77,10 +72,9 @@ static void _dbg_print_hex(u64 number) { char digit = digits[bits]; *--p = digit; } - _dbg_print_string(p); + print_string(p); } - -static void _dbg_printf(char *fmt, ...) { +static void printf(char *fmt, ...) { va_list args; va_start(args, fmt); char str_buf[256]; @@ -92,29 +86,29 @@ static void _dbg_printf(char *fmt, ...) { fmt++; } str_buf[buf_i] = 0; - _dbg_print_string_n(str_buf, buf_i); + print_string_n(str_buf, buf_i); buf_i = 0; if(*fmt == '%') { ++fmt; if(*fmt == 'd') { i64 number = va_arg(args, i64); - _dbg_print_int(number); + print_int(number); } else if(*fmt == 'u') { u64 number = va_arg(args, u64); - _dbg_print_uint(number); + print_uint(number); } else if(*fmt == 'x') { u64 number = va_arg(args, u64); - _dbg_print_hex(number); + print_hex(number); } else if(*fmt == 's') { char *str = va_arg(args, char *); - _dbg_print_string(str); + print_string(str); } else if(*fmt == 'c') { int c = va_arg(args, int); - _dbg_print_char(c); + print_char(c); } ++fmt; } @@ -122,6 +116,24 @@ static void _dbg_printf(char *fmt, ...) { va_end(args); } +#if defined(_DEBUG) + #define _dbg_print_char(c) print_char(c) + #define _dbg_print_string(s) print_string(s) + #define _dbg_print_string_n(s,n) print_string_n(s,n) + #define _dbg_print_int(d) print_int(d) + #define _dbg_print_uint(u) print_uint(u) + #define _dbg_print_hex(x) print_hex(x) + #define _dbg_printf(fmt, ...) printf(fmt, ##__VA_ARGS__) +#else + #define _dbg_print_char(c) do {} while(0) + #define _dbg_print_string(s) do {} while(0) + #define _dbg_print_string_n(s,n) do {} while(0) + #define _dbg_print_int(d) do {} while(0) + #define _dbg_print_uint(u) do {} while(0) + #define _dbg_print_hex(x) do {} while(0) + #define _dbg_printf(fmt, ...) do {} while(0) +#endif + extern void loader_entry(u64 *sp, u64 *dyn, u64 *aux); void _dlstart_reloc_c(u64 *sp, Elf64_Dyn *dynv) {