Skip to content

Commit 306342f

Browse files
committed
main: Add NetBSD support
Signed-off-by: Akira Moroo <[email protected]>
1 parent b589b91 commit 306342f

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ OBJS = $(C_SRCS:.c=.o)
3131
all: $(PROGS)
3232

3333
$(PROGS): $(OBJS)
34-
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
34+
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LDFLAGS)
3535

3636
clean:
3737
-@rm -rf $(CLEANFILES)

main.c

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <sys/stat.h>
1919
#include <unistd.h>
2020

21-
#ifdef __FreeBSD__
21+
#if defined(__FreeBSD__) || defined(__NetBSD__)
2222
#define PROCFS_MAP "/proc/self/map"
2323
#else
2424
#define PROCFS_MAP "/proc/self/maps"
@@ -120,7 +120,7 @@ void ____asm_impl(void) {
120120
"adrp x6, :got:syscall_table \n\t"
121121
"ldr x6, [x6, #:got_lo12:syscall_table] \n\t"
122122
"ldr x6, [x6] \n\t"
123-
"add x6, x6, xzr, lsl #3 \n\t"
123+
"add x6, x6, x8, lsl #3 \n\t"
124124
"br x6 \n\t");
125125

126126
/*
@@ -189,7 +189,9 @@ void ____asm_impl(void) {
189189

190190
/* arguments for syscall_hook */
191191
"mov x7, x14 \n\t" /* return address */
192+
#if !defined(__NetBSD__)
192193
"mov x6, x8 \n\t" /* syscall NR */
194+
#endif /* !defined(__NetBSD__) */
193195

194196
"bl syscall_hook \n\t"
195197

@@ -508,7 +510,7 @@ static void scan_exec_code(char *code, size_t code_size, int mem_prot,
508510
close(fd);
509511
}
510512

511-
#ifdef __FreeBSD__
513+
#if defined(__FreeBSD__)
512514
/* entry point for binary scanning on FreeBSD */
513515
static void scan_code(void) {
514516
LIST_INIT(&head);
@@ -559,6 +561,57 @@ static void scan_code(void) {
559561
}
560562
fclose(fp);
561563
}
564+
#elif defined(__NetBSD__)
565+
/* entry point for binary scanning on NetBSD */
566+
static void scan_code(void) {
567+
LIST_INIT(&head);
568+
569+
FILE *fp = NULL;
570+
/* get memory mapping information from procfs */
571+
assert((fp = fopen(PROCFS_MAP, "r")) != NULL);
572+
char buf[4096];
573+
while (fgets(buf, sizeof(buf), fp) != NULL) {
574+
/* we do not touch stack memory */
575+
if (strstr(buf, "[stack]") != NULL) {
576+
continue;
577+
}
578+
int mem_prot = 0;
579+
int i = 0;
580+
char from_addr[65] = {0};
581+
char to_addr[65] = {0};
582+
char *c = strtok(buf, " ");
583+
while (c != NULL) {
584+
switch (i) {
585+
case 0:
586+
strncpy(from_addr, c, sizeof(from_addr) - 1);
587+
break;
588+
case 1:
589+
strncpy(to_addr, c, sizeof(to_addr) - 1);
590+
break;
591+
case 2:
592+
for (size_t j = 0; j < strlen(c); j++) {
593+
if (c[j] == 'r') mem_prot |= PROT_READ;
594+
if (c[j] == 'w') mem_prot |= PROT_WRITE;
595+
if (c[j] == 'x') mem_prot |= PROT_EXEC;
596+
}
597+
break;
598+
case 4:
599+
if (strncmp(c, "COW", 3) == 0) {
600+
int64_t from = strtol(&from_addr[0], NULL, 16);
601+
int64_t to = strtol(&to_addr[0], NULL, 16);
602+
if (mem_prot & PROT_EXEC) {
603+
scan_exec_code((char *)from, (size_t)to - from, mem_prot, NULL);
604+
}
605+
}
606+
break;
607+
}
608+
if (i == 4) break;
609+
c = strtok(NULL, " ");
610+
i++;
611+
}
612+
}
613+
fclose(fp);
614+
}
562615
#else
563616
/* entry point for binary scanning on Linux */
564617
static void scan_code(void) {

0 commit comments

Comments
 (0)