From 7f898abb8226ac5852e9dc0a993c224673d2af59 Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Sat, 19 Dec 2020 20:55:52 +0100 Subject: [PATCH] bsd update Fix crtbegin/crtend Use dlsym on all bsd targets Check .eh_frame on all bsd targets Disable test3 on FreeBSD and NetBSD and use test1 instead because dlsym not working (WIP) Disable dlltest and 113_btdll on NetBSD because text relocations are not allowed Disable 115_bound_setjmp on NetBSD because longjmp is renamed into __longjmp14 --- libtcc.c | 14 ++++++++------ tccelf.c | 15 ++++++++------- tests/Makefile | 11 +++++++++++ tests/tests2/Makefile | 4 ++++ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/libtcc.c b/libtcc.c index 8b8f8b28..ed33c278 100644 --- a/libtcc.c +++ b/libtcc.c @@ -1105,23 +1105,25 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) /* add libc crt1/crti objects */ if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) && !s->nostdlib) { +#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD #if TARGETOS_OpenBSD if (output_type != TCC_OUTPUT_DLL) tcc_add_crt(s, "crt0.o"); - if (output_type == TCC_OUTPUT_DLL) - tcc_add_crt(s, "crtbeginS.o"); - else - tcc_add_crt(s, "crtbegin.o"); #elif TARGETOS_FreeBSD if (output_type != TCC_OUTPUT_DLL) tcc_add_crt(s, "crt1.o"); tcc_add_crt(s, "crti.o"); - tcc_add_crt(s, "crtbegin.o"); #elif TARGETOS_NetBSD if (output_type != TCC_OUTPUT_DLL) tcc_add_crt(s, "crt0.o"); tcc_add_crt(s, "crti.o"); - tcc_add_crt(s, "crtbegin.o"); +#endif + if (s->static_link) + tcc_add_crt(s, "crtbeginT.o"); + else if (output_type == TCC_OUTPUT_DLL) + tcc_add_crt(s, "crtbeginS.o"); + else + tcc_add_crt(s, "crtbegin.o"); #elif !TCC_TARGET_MACHO /* Mach-O with LC_MAIN doesn't need any crt startup code. */ if (output_type != TCC_OUTPUT_DLL) diff --git a/tccelf.c b/tccelf.c index 1b05b69e..03c670d7 100644 --- a/tccelf.c +++ b/tccelf.c @@ -915,7 +915,7 @@ ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve) #if defined TCC_IS_NATIVE && !defined TCC_TARGET_PE /* dlsym() needs the undecorated name. */ void *addr = dlsym(RTLD_DEFAULT, &name[s1->leading_underscore]); -#if TARGETOS_OpenBSD +#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD if (addr == NULL) { int i; for (i = 0; i < s1->nb_loaded_dlls; i++) @@ -1460,10 +1460,11 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) tcc_add_support(s1, TCC_LIBTCC1); #if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD /* add crt end if not memory output */ - if (s1->output_type == TCC_OUTPUT_DLL) - tcc_add_crt(s1, "crtendS.o"); - else if (s1->output_type != TCC_OUTPUT_MEMORY) { - tcc_add_crt(s1, "crtend.o"); + if (s1->output_type != TCC_OUTPUT_MEMORY) { + if (s1->output_type == TCC_OUTPUT_DLL) + tcc_add_crt(s1, "crtendS.o"); + else + tcc_add_crt(s1, "crtend.o"); #if TARGETOS_FreeBSD || TARGETOS_NetBSD tcc_add_crt(s1, "crtn.o"); #endif @@ -2856,8 +2857,8 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, sm_table[i].new_section = 1; found: if (sh->sh_type != s->sh_type) { -#if TARGETOS_OpenBSD - if (strcmp (s->name, ".eh_frame") || sh->sh_type != SHT_PROGBITS) +#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD + if (strcmp (s->name, ".eh_frame")) #endif { tcc_error_noabort("invalid section type"); diff --git a/tests/Makefile b/tests/Makefile index 8e516bf3..2aac24c0 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -64,6 +64,17 @@ endif ifeq ($(TARGETOS),OpenBSD) dlltest: CFLAGS+=-fno-stack-protector endif +ifeq ($(TARGETOS),FreeBSD) + # test3 has dlsym problems + TESTS := $(filter-out test3,$(TESTS)) + TESTS += test1 +endif +ifeq ($(TARGETOS),NetBSD) + # test3 has dlsym problems + # dlltest does not allow text relocations + TESTS := $(filter-out test3 dlltest,$(TESTS)) + TESTS += test1 +endif RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) DISAS = objdump -d diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index 49dc6ff6..40d7cce5 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -52,6 +52,10 @@ ifneq (,$(filter OpenBSD FreeBSD NetBSD,$(TARGETOS))) SKIP += 106_pthread.test # no pthread_condattr_setpshared SKIP += 114_bound_signal.test # libc problem signal/fork endif +ifeq ($(TARGETOS),NetBSD) + SKIP += 113_btdll.test # text relocations + SKIP += 115_bound_setjmp.test # longjmp renamed to __longjmp14 +endif # Some tests might need arguments ARGS =