diff -ur tcc-0.9.14_org/Makefile tcc-0.9.14/Makefile --- tcc-0.9.14_org/Makefile Sun Nov 24 16:56:44 2002 +++ tcc-0.9.14/Makefile Sun Dec 8 01:43:57 2002 @@ -1,11 +1,11 @@ # # Tiny C Compiler Makefile # -prefix=/usr/local +prefix=/opt/tcc/0.9.14 manpath=$(prefix)/man CFLAGS=-O2 -g -Wall -LIBS=-ldl +LIBS= CFLAGS_P=$(CFLAGS) -pg -static -DCONFIG_TCC_STATIC LIBS_P= diff -ur tcc-0.9.14_org/bcheck.c tcc-0.9.14/bcheck.c --- tcc-0.9.14_org/bcheck.c Sun Nov 24 16:56:44 2002 +++ tcc-0.9.14/bcheck.c Sat Dec 7 21:49:20 2002 @@ -21,7 +21,9 @@ #include #include #include +#ifndef __FreeBSD__ #include +#endif //#define BOUND_DEBUG @@ -31,6 +33,10 @@ /* use malloc hooks. Currently the code cannot be reliable if no hooks */ #define CONFIG_TCC_MALLOC_HOOKS +#ifdef __FreeBSD__ +#warning A comment just above this line says CONFIG_TCC_MALLOC_HOOKS is required, but it doesnt work on FreeBSD. Sorry. +#undef CONFIG_TCC_MALLOC_HOOKS +#endif #define BOUND_T1_BITS 13 #define BOUND_T2_BITS 11 @@ -713,10 +719,16 @@ restore_malloc_hooks(); +#ifdef __FreeBSD__ + /* FreeBSD doesn't have memalign. But it certainly aligns to 4, possibly more. */ + if (align > 4) return NULL; + ptr = malloc (size + 1); +#else /* we allocate one more byte to ensure the regions will be separated by at least one byte. With the glibc malloc, it may be in fact not necessary */ ptr = memalign(size + 1, align); +#endif install_malloc_hooks(); @@ -762,7 +774,7 @@ { void *ptr; size = size * nmemb; - ptr = __bound_malloc(size); + ptr = __bound_malloc(size, NULL); if (!ptr) return NULL; memset(ptr, 0, size); diff -ur tcc-0.9.14_org/ex4.c tcc-0.9.14/ex4.c --- tcc-0.9.14_org/ex4.c Sun Nov 24 16:56:44 2002 +++ tcc-0.9.14/ex4.c Sun Dec 8 03:27:44 2002 @@ -1,4 +1,4 @@ -#!/usr/local/bin/tcc -lX11 +#!/opt/tcc/c/bin/tcc -lX11 #include /* Yes, TCC can use X11 too ! */ #include diff -ur tcc-0.9.14_org/tcc.c tcc-0.9.14/tcc.c --- tcc-0.9.14_org/tcc.c Sun Nov 24 16:56:44 2002 +++ tcc-0.9.14/tcc.c Mon Dec 9 04:04:20 2002 @@ -35,11 +35,14 @@ #include #include #endif -#include "elf.h" -#include "stab.h" +#include +#include #ifndef CONFIG_TCC_STATIC #include #endif +#ifdef __FreeBSD__ +#include +#endif #include "libtcc.h" @@ -534,7 +537,7 @@ #define vsnprintf _vsnprintf #endif -#if defined(WIN32) || defined(TCC_UCLIBC) +#if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__) /* currently incorrect */ long double strtold(const char *nptr, char **endptr) { @@ -8345,16 +8348,24 @@ /* return the PC at frame level 'level'. Return non zero if not found */ static int rt_get_caller_pc(unsigned long *paddr, - struct ucontext *uc, int level) + ucontext_t *uc, int level) { unsigned long fp; int i; if (level == 0) { +#ifdef __FreeBSD__ + *paddr = uc->uc_mcontext.mc_eip; +#else *paddr = uc->uc_mcontext.gregs[EIP]; +#endif return 0; } else { +#ifdef __FreeBSD__ + fp = uc->uc_mcontext.mc_ebp; +#else fp = uc->uc_mcontext.gregs[EBP]; +#endif for(i=1;i= 0xc0000000) @@ -8370,7 +8381,7 @@ #endif /* emit a run time error at position 'pc' */ -void rt_error(struct ucontext *uc, const char *fmt, ...) +void rt_error(ucontext_t *uc, const char *fmt, ...) { va_list ap; unsigned long pc; @@ -8396,7 +8407,7 @@ /* signal handler for fatal errors */ static void sig_error(int signum, siginfo_t *siginf, void *puc) { - struct ucontext *uc = puc; + ucontext_t *uc = puc; switch(signum) { case SIGFPE: @@ -8485,7 +8496,7 @@ struct sigaction sigact; /* install TCC signal handlers to print debug info on fatal runtime errors */ - sigact.sa_flags = SA_SIGINFO | SA_ONESHOT; + sigact.sa_flags = SA_SIGINFO | SA_RESETHAND; sigact.sa_sigaction = sig_error; sigemptyset(&sigact.sa_mask); sigaction(SIGFPE, &sigact, NULL); @@ -8830,6 +8841,7 @@ put_stabs("", 0, 0, 0, 0); } +#ifndef __FreeBSD__ /* add libc crt1/crti objects */ if (output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) { @@ -8837,6 +8849,7 @@ tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o"); tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o"); } +#endif return 0; } diff -ur tcc-0.9.14_org/tccelf.c tcc-0.9.14/tccelf.c --- tcc-0.9.14_org/tccelf.c Sun Nov 24 16:56:44 2002 +++ tcc-0.9.14/tccelf.c Mon Dec 9 04:06:59 2002 @@ -825,7 +825,11 @@ } /* name of ELF interpreter */ +#ifdef __FreeBSD__ +static char elf_interp[] = "/usr/libexec/ld-elf.so.1"; +#else static char elf_interp[] = "/lib/ld-linux.so.2"; +#endif #define ELF_START_ADDR 0x08048000 #define ELF_PAGE_SIZE 0x1000 @@ -848,10 +852,24 @@ Elf32_Sym *sym; int type, file_type; unsigned long rel_addr, rel_size; +#ifdef __FreeBSD__ + int tempfd = -1; + char *tempfn = NULL; +#endif file_type = s1->output_type; s1->nb_errors = 0; +#ifdef __FreeBSD__ + if (file_type == TCC_OUTPUT_EXE) { + file_type = TCC_OUTPUT_OBJ; + tempfn = strdup ("/tmp/tcctmpXXXXXXXX.o"); + if (tempfn == NULL) error("no memory"); + tempfd = mkstemps (tempfn, 2); + if (tempfd == -1) error("could not create tempfile"); + } +#endif + if (file_type != TCC_OUTPUT_OBJ) tcc_add_runtime(s1); @@ -1324,6 +1342,10 @@ ehdr.e_ident[4] = ELFCLASS32; ehdr.e_ident[5] = ELFDATA2LSB; ehdr.e_ident[6] = EV_CURRENT; +#ifdef __FreeBSD__ + ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; + ehdr.e_ident[EI_ABIVERSION] = 0; +#endif switch(file_type) { default: case TCC_OUTPUT_EXE: @@ -1349,7 +1371,12 @@ mode = 0666; else mode = 0777; +#ifdef __FreeBSD__ + if (tempfd != -1) fd = tempfd; + else fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode); +#else fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode); +#endif if (fd < 0) { error_noabort("could not write '%s'", filename); goto fail; @@ -1396,6 +1423,35 @@ fwrite(sh, 1, sizeof(Elf32_Shdr), f); } fclose(f); + +#ifdef __FreeBSD__ + if (tempfd != -1) { + char *av [6]; + char buf [128]; + pid_t pid; + int status; + av [0] = "gcc"; + av [1] = "-o"; + av [2] = (char *) filename; /* mumble mumble warnings.. */ + av [3] = tempfn; + av [4] = buf; + av [5] = NULL; + snprintf (buf, sizeof (buf), "%s/libtcc1.o", tcc_lib_path); + pid = fork (); + if (pid == 0) { + execv ("/usr/bin/gcc", av); + error ("Failed to exec gcc"); + } else { + if (pid == -1) { + unlink (tempfn); + error ("Failed to fork"); + } else { + waitpid (pid, &status, 0); + unlink (tempfn); + } + } + } +#endif ret = 0; the_end: