Hide dbg_printfs behind a define

This commit is contained in:
flysand7 2023-08-04 05:46:05 +11:00
parent a812a18033
commit 845d08ee3a
2 changed files with 42 additions and 30 deletions

View File

@ -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':

View File

@ -16,30 +16,27 @@
#include <cia-def.h>
#include <bin/elf.h>
#include <sys/syscall.h>
#include <stdarg.h>
#define AUX_CNT 32
#define DYN_CNT 37
#include <stdarg.h>
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) {