make: make shorter command lines

Put former NATIVE_DEFINES into config.h.  Such tcc can be run
and tested directly from source more easily, like for example:

    tcc -run tcc.c -B. -run test.c

Also:
- tccelf.c: cleanup
- tccpp.c: avoid stupid clang warning
- configure: reduce -Wno- switches
- tcc.h: inline wait/post_sem()
- tccpe.c: simplify import (assume STT_NOTYPE is function)
This commit is contained in:
grischka 2024-11-21 12:03:32 +01:00
parent 4b0402825e
commit 729918ef35
21 changed files with 379 additions and 472 deletions

3
.gitignore vendored
View file

@ -48,6 +48,9 @@ win32/include/stddef.h
win32/include/stdnoreturn.h
win32/include/varargs.h
win32/include/tcclib.h
win32/include/tccdefs.h
win32/include/stdatomic.h
win32/include/tgmath.h
tests/tcctest[1234]
tests/tcctest.gcc

View file

@ -20,18 +20,15 @@ LIBTCC = libtcc.a
LIBTCC1 = libtcc1.a
LINK_LIBTCC =
LIBS =
CFLAGS += -I$(TOP)
CFLAGS += $(CPPFLAGS)
VPATH = $(TOPSRC)
-LTCC = $(TOP)/$(LIBTCC)
ifdef CONFIG_WIN32
CFG = -win
ifneq ($(CONFIG_static),yes)
LIBTCC = libtcc$(DLLSUF)
LIBTCCDEF = libtcc.def
-LTCC = $(bindir)/libtcc.dll
else
-LTCC = -ltcc -L$(libdir)
endif
ifneq ($(CONFIG_debug),yes)
LDFLAGS += -s
@ -76,7 +73,6 @@ else
endif
export MACOSX_DEPLOYMENT_TARGET := 10.6
endif
-LTCC = -ltcc
endif
# run local version of tcc with local libraries and includes
@ -89,42 +85,15 @@ TCC = $(TCC_LOCAL) $(TCCFLAGS)
# run tests with the installed tcc instead
ifdef TESTINSTALL
TCC_LOCAL = $(bindir)/tcc
TCCFLAGS-unx = -I..
TCCFLAGS-win = -I.. -B$(bindir)
LIBTCC =
LIBS += $(-LTCC)
TCCFLAGS-unx = -I$(TOP)
TCCFLAGS-win = -B$(bindir) -I$(TOP)
-LTCC = $(libdir)/$(LIBTCC) $(LINK_LIBTCC)
endif
CFLAGS_P = $(CFLAGS) -pg -static -DCONFIG_TCC_STATIC -DTCC_PROFILE
LIBS_P = $(LIBS)
LDFLAGS_P = $(LDFLAGS)
CONFIG_$(ARCH) = yes
NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386
NATIVE_DEFINES_$(CONFIG_x86_64) += -DTCC_TARGET_X86_64
NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE
NATIVE_DEFINES_$(CONFIG_OSX) += -DTCC_TARGET_MACHO
NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC
NATIVE_DEFINES_$(CONFIG_musl) += -DTCC_MUSL
NATIVE_DEFINES_$(CONFIG_libgcc) += -DCONFIG_USE_LIBGCC
NATIVE_DEFINES_$(CONFIG_selinux) += -DHAVE_SELINUX
NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM
NATIVE_DEFINES_$(CONFIG_arm_eabihf) += -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT
NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI
NATIVE_DEFINES_$(CONFIG_arm_vfp) += -DTCC_ARM_VFP
NATIVE_DEFINES_$(CONFIG_arm64) += -DTCC_TARGET_ARM64
NATIVE_DEFINES_$(CONFIG_riscv64) += -DTCC_TARGET_RISCV64
NATIVE_DEFINES_$(CONFIG_BSD) += -DTARGETOS_$(TARGETOS)
NATIVE_DEFINES_$(CONFIG_Android) += -DTARGETOS_ANDROID
NATIVE_DEFINES_$(CONFIG_pie) += -DCONFIG_TCC_PIE
NATIVE_DEFINES_$(CONFIG_pic) += -DCONFIG_TCC_PIC
NATIVE_DEFINES_no_$(CONFIG_new_macho) += -DCONFIG_NEW_MACHO=0
NATIVE_DEFINES_$(CONFIG_codesign) += -DCONFIG_CODESIGN
NATIVE_DEFINES_$(CONFIG_new-dtags) += -DCONFIG_NEW_DTAGS
NATIVE_DEFINES_no_$(CONFIG_bcheck) += -DCONFIG_TCC_BCHECK=0
NATIVE_DEFINES_no_$(CONFIG_backtrace) += -DCONFIG_TCC_BACKTRACE=0
NATIVE_DEFINES += $(NATIVE_DEFINES_yes) $(NATIVE_DEFINES_no_no)
DEF-i386 = -DTCC_TARGET_I386
DEF-i386-win32 = -DTCC_TARGET_I386 -DTCC_TARGET_PE
DEF-i386-OpenBSD = $(DEF-i386) -DTARGETOS_OpenBSD
@ -150,8 +119,6 @@ DEF-x86_64-FreeBSD = $(DEF-x86_64) -DTARGETOS_FreeBSD
DEF-x86_64-NetBSD = $(DEF-x86_64) -DTARGETOS_NetBSD
DEF-x86_64-OpenBSD = $(DEF-x86_64) -DTARGETOS_OpenBSD
DEF-$(NATIVE_TARGET) = $(NATIVE_DEFINES)
ifeq ($(INCLUDED),no)
# --------------------------------------------------------------------------
# running top Makefile
@ -192,7 +159,7 @@ endif
T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
DEFINES += $(DEF-$T) $(DEF-all)
DEFINES += $(DEF-$T)
DEFINES += $(if $(ROOT-$T),-DCONFIG_SYSROOT="\"$(ROOT-$T)\"")
DEFINES += $(if $(CRT-$T),-DCONFIG_TCC_CRTPREFIX="\"$(CRT-$T)\"")
DEFINES += $(if $(LIB-$T),-DCONFIG_TCC_LIBPATHS="\"$(LIB-$T)\"")
@ -202,10 +169,15 @@ DEFINES += $(DEF-$(or $(findstring win,$T),unx))
ifneq ($(X),)
$(if $(DEF-$T),,$(error error: unknown target: '$T'))
DEF-all += -DCONFIG_TCC_CROSSPREFIX="\"$X\""
DEF-$T += -DCONFIG_TCC_CROSS
DEF-$(NATIVE_TARGET) =
DEF-$T += -DCONFIG_TCC_CROSSPREFIX="\"$X\""
ifneq ($(CONFIG_WIN32),yes)
DEF-win += -DCONFIG_TCCDIR="\"$(tccdir)/win32\""
endif
else
# using values from config.h
DEF-$(NATIVE_TARGET) =
endif
# include custom configuration (see make help)
@ -255,15 +227,16 @@ ifeq ($(ONE_SOURCE),yes)
LIBTCC_OBJ = $(X)libtcc.o
LIBTCC_INC = $($T_FILES)
TCC_FILES = $(X)tcc.o
tcc.o : DEFINES += -DONE_SOURCE=0
$(X)tcc.o $(X)libtcc.o : $(TCCDEFS_H)
$(X)tcc.o $(X)libtcc.o : $(TCCDEFS_H)
else
LIBTCC_OBJ = $(patsubst %.c,$(X)%.o,$(LIBTCC_SRC))
LIBTCC_INC = $(filter %.h %-gen.c %-link.c,$($T_FILES))
TCC_FILES = $(X)tcc.o $(LIBTCC_OBJ)
$(TCC_FILES) : DEFINES += -DONE_SOURCE=0
$(X)tccpp.o : $(TCCDEFS_H)
$(X)libtcc.o : DEFINES += -DONE_SOURCE=0
endif
tcc.o : DEFINES += -DONE_SOURCE=0
DEFINES += -I$(TOP)
GITHASH:=$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo no)
ifneq ($(GITHASH),no)
@ -283,7 +256,7 @@ endif
# target specific object rule
$(X)%.o : %.c $(LIBTCC_INC)
$S$(CC) -o $@ -c $< $(DEFINES) $(CFLAGS)
$S$(CC) -o $@ -c $< $(addsuffix ,$(DEFINES) $(CFLAGS))
# additional dependencies
$(X)tcc.o : tcctools.c
@ -291,7 +264,7 @@ $(X)tcc.o : DEFINES += $(DEF_GITHASH)
# Host Tiny C Compiler
tcc$(EXESUF): tcc.o $(LIBTCC)
$S$(CC) -o $@ $^ $(LIBS) $(LDFLAGS) $(LINK_LIBTCC)
$S$(CC) -o $@ $^ $(addsuffix ,$(LIBS) $(LDFLAGS) $(LINK_LIBTCC))
# Cross Tiny C Compilers
# (the TCCDEFS_H dependency is only necessary for parallel makes,
@ -318,8 +291,8 @@ libtcc.a: $(LIBTCC_OBJ)
libtcc.so: $(LIBTCC_OBJ)
$S$(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LIBS) $(LDFLAGS)
libtcc.so: CFLAGS+=-fPIC
libtcc.so: LDFLAGS+=-fPIC
libtcc.so: override CFLAGS += -fPIC
libtcc.so: override LDFLAGS += -fPIC
# OSX dynamic libtcc library
libtcc.dylib: $(LIBTCC_OBJ)
@ -529,13 +502,12 @@ help:
@echo "make test-install"
@echo " run tests with the installed tcc"
@echo "Other supported make targets:"
@echo " install install-strip doc clean tags ETAGS tar distclean help"
@echo " install install-strip uninstall doc [dist]clean tags ETAGS tar help"
@echo "Custom configuration:"
@echo " The makefile includes a file 'config-extra.mak' if it is present."
@echo " This file may contain some custom configuration. For example:"
@echo " NATIVE_DEFINES += -D..."
@echo " Or for example to configure the search paths for a cross-compiler"
@echo " assuming the support files in /usr/i686-linux-gnu:"
@echo " This file may contain some custom configuration. For example to"
@echo " configure the search paths for a cross-compiler, assuming the"
@echo " support files in /usr/i686-linux-gnu:"
@echo " ROOT-i386 = /usr/i686-linux-gnu"
@echo " CRT-i386 = {R}/lib"
@echo " LIB-i386 = {B}:{R}/lib"

View file

@ -34,8 +34,8 @@
#define NB_REGS 9
#endif
#ifndef TCC_CPU_VERSION
# define TCC_CPU_VERSION 5
#ifndef CONFIG_TCC_CPUVER
# define CONFIG_TCC_CPUVER 5
#endif
/* a register can belong to several classes. The classes must be

View file

@ -202,7 +202,7 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
if (x & 0x800000)
x -= 0x1000000;
x <<= 2;
blx_avail = (TCC_CPU_VERSION >= 5);
blx_avail = (CONFIG_TCC_CPUVER >= 5);
is_thumb = val & 1;
is_bl = (*(unsigned *) ptr) >> 24 == 0xeb;
is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl));

216
configure vendored
View file

@ -117,7 +117,7 @@ for opt do
;;
--cpu=*) cpu=`echo $opt | cut -d '=' -f 2-`
;;
--dwarf=*) dwarf=`echo $opt | cut -d '=' -f 2-`
--dwarf=*) confvars="$confvars dwarf=${opt#*=}"
;;
--enable-cross) confvars="$confvars cross"
;;
@ -193,9 +193,10 @@ Advanced options (experts only):
--config-backtrace=no disable stack backtraces (with -run or -bt)
--config-bcheck=no disable bounds checker (-b)
--config-predefs=no do not compile tccdefs.h, instead just include
--config-new_macho=no|yes Force apple object format (autodetect osx <= 10)
--config-new_macho=no|yes force apple object format (autodetect osx <= 10)
--config-new_dtags=yes use new ELF DTAGs (DT_RUNPATH instead of DT_RPATH)
--config-codesign=no do not use codesign on apple to sign executables
--dwarf=x Use dwarf debug info instead of stabs (x=2..5)
--config-dwarf=x use dwarf debug info instead of stabs (x=2..5)
Cross build options (experimental):
--cpu=CPU target CPU [$cpu]
@ -209,7 +210,7 @@ default() # set variable unless already set
{
local v
eval v=\"\$$1\"
test -z "$v" && eval $1=\"$2\"
test -z "$v" -a -n "$2" && eval $1=\"$2\"
}
default_conf() # add to confvars unless already present
@ -251,10 +252,9 @@ else
fi
default cpu "$cpu_sys"
cpu_set="$cpu"
# check for crpss build
if test "$cpu_set" != "$cpu_sys" \
if test "$cpu" != "$cpu_sys" \
-o "$targetos" != "$buildos" \
-o -n "$cross_prefix" ; then
build_cross="yes"
@ -322,8 +322,7 @@ esac
case $targetos in
Darwin)
dwarf=4
confvars="$confvars OSX"
confvars="$confvars OSX dwarf=4"
default_conf "codesign"
DLLSUF=".dylib"
if test -z "$build_cross"; then
@ -354,14 +353,14 @@ case $targetos in
default sysroot "/usr"
fi
default prefix "${sysroot}"
confvars="$confvars Android new-dtags rpath=no"
confvars="$confvars Android new_dtags rpath=no"
test "${cpu}" != "i386" && confvars="$confvars pie"
default_conf "static=no"
if test "${cpu}" = "arm"; then
default triplet "arm-linux-androideabi"
cpuver=7
else
default triplet "${cpu_set}-linux-android"
default triplet "${cpu}-linux-android"
fi
test "${cpu%64}" != "${cpu}" && S="64" || S=""
default tcc_sysincludepaths "{B}/include:{R}/include:{R}/include/${triplet}"
@ -372,13 +371,16 @@ case $targetos in
;;
WIN32)
mingw32="yes"
confvars="$confvars WIN32"
confvars="WIN32 $confvars"
default prefix "C:/Program Files/tcc"
default tccdir "${prefix}"
default bindir "${tccdir}"
default docdir "${tccdir}/doc"
default libdir "${tccdir}/libtcc"
# set tccdir at runtime from executable path
test "$tccdir" = "$bindir" && tccdir_auto="yes"
# avoid mingw dependencies such as 'libgcc_s_dw2-1.dll'
test "$cc_name" = "gcc" && default LDFLAGS "-static"
LIBSUF=".lib"
EXESUF=".exe"
DLLSUF=".dll"
@ -435,7 +437,7 @@ if test -z "$build_cross"; then
if test -f "/lib/ld-uClibc.so.0" ; then
echo "Perhaps you want ./configure --config-uClibc"
fi
if test -f "/lib/ld-musl-$cpu.so.1"; then
if test -f "/lib/ld-musl-${cpu}.so.1"; then
echo "Perhaps you want ./configure --config-musl"
fi
fi
@ -452,7 +454,7 @@ else
esac
case $targetos in
Linux)
default triplet "$cpu_set-linux-gnu"
default triplet "${cpu}-linux-gnu"
esac
fi
@ -460,11 +462,6 @@ if test "$bigendian" = "yes" ; then
confvars="$confvars BIGENDIAN"
fi
if test "$mingw32" = "yes" -a "$cc_name" = "gcc"; then
# avoid mingw dependencies such as 'libgcc_s_dw2-1.dll'
default LDFLAGS "-static"
fi
if test "$cpu" = "arm"; then
if test "${triplet%eabihf}" != "$triplet" ; then
confvars="$confvars arm_eabihf arm_vfp"
@ -481,19 +478,14 @@ if test "$cpu" = "arm"; then
fi
fi
if test -n "$dwarf"; then
confvars="$confvars dwarf=$dwarf"
fi
# a final configuration tuning
if test "$cc_name" != "tcc"; then
OPT1="-Wdeclaration-after-statement -fno-strict-aliasing"
OPT1="-Wdeclaration-after-statement" #-fno-strict-aliasing
# we want -Wno- but gcc does not always reject unknown -Wno- options
OPT2="-Wpointer-sign -Wsign-compare -Wunused-result -Wformat-truncation"
OPT2="$OPT2 -Wstringop-truncation"
if test "$cc_name" = "clang"; then
OPT1="$OPT1 -fheinous-gnu-extensions"
OPT2="$OPT2 -Wstring-plus-int -Wdeprecated-declarations"
OPT2= #"-Wstring-plus-int"
else
OPT2="-Wunused-result"
fi
$cc $OPT1 $OPT2 -o a.out -c -xc - < /dev/null > cc_msg.txt 2>&1
for o in $OPT1; do # enable these options
@ -534,6 +526,8 @@ fcho "Switches " "$tcc_switches"
fcho "Config " "${confvars# }"
echo "Creating config.mak and config.h"
version=`head $source_path/VERSION`
cat >config.mak <<EOF
# Automatically generated by configure - do not modify
prefix=$prefix
@ -549,66 +543,17 @@ CC_NAME=$cc_name
GCC_MAJOR=$gcc_major
GCC_MINOR=$gcc_minor
AR=$ar
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
LIBSUF=$LIBSUF
EXESUF=$EXESUF
DLLSUF=$DLLSUF
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
ARCH=$cpu
TARGETOS=$targetos
BUILDOS=$buildos
VERSION=$version
EOF
print_inc() {
local v="$2"
if test -n "$v"; then
test "$3" = "num" || v="\"$v\""
echo "#ifndef $1" >> $TMPH
echo "# define $1 $v" >> $TMPH
echo "#endif" >> $TMPH
fi
}
print_mak() {
local v="$2"
if test -n "$v"; then
test "$3" = "num" || v="\"\\\"$v\\\"\""
echo "NATIVE_DEFINES+=-D$1=$v" >> config.mak
fi
}
echo "/* Automatically generated by configure - do not modify */" > $TMPH
print_inc CONFIG_SYSROOT "$sysroot"
test "$tccdir_auto" = "yes" || print_inc CONFIG_TCCDIR "$tccdir"
print_inc DWARF_VERSION "$dwarf" num
print_mak CONFIG_USR_INCLUDE "$tcc_usrinclude"
print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"
print_mak CONFIG_TCC_CRTPREFIX "$tcc_crtprefix"
print_mak CONFIG_TCC_ELFINTERP "$tcc_elfinterp"
print_mak CONFIG_TCC_SWITCHES "$tcc_switches"
print_mak CONFIG_LDDIR "$tcc_lddir"
print_mak CONFIG_TRIPLET "$triplet"
print_mak TCC_CPU_VERSION "$cpuver" num
echo "ARCH=$cpu" >> config.mak
echo "TARGETOS=$targetos" >> config.mak
echo "BUILDOS=$buildos" >> config.mak
predefs="1"
for v in $confvars ; do
test "$v" = "predefs=no" && predefs=""
if test "${v%=*}" = "$v"; then
echo "CONFIG_$v=yes" >> config.mak
else
echo "CONFIG_$v" >> config.mak
fi
done
print_inc CONFIG_TCC_PREDEFS "$predefs" num
version=`head $source_path/VERSION`
echo "VERSION = $version" >> config.mak
echo "#define TCC_VERSION \"$version\"" >> $TMPH
echo "@set VERSION $version" > config.texi
if test "$source_path_used" = "yes" ; then
case $source_path in
/*) echo "TOPSRC=$source_path";;
@ -617,12 +562,113 @@ if test "$source_path_used" = "yes" ; then
else
echo 'TOPSRC=$(TOP)' >>config.mak
fi
cat >>$TMPH <<EOF
print_str() {
local v="$2"
if test -n "$v"; then
test -n "$3" && echo "#ifndef $1" >> $TMPH
echo "#define $1 \"$v\"" >> $TMPH
test -n "$3" && echo "#endif" >> $TMPH
fi
}
print_num() {
local v="$2"
if test -n "$v"; then
test -n "$3" && echo "#ifndef $1" >> $TMPH
echo "#define $1 $v" >> $TMPH
test -n "$3" && echo "#endif" >> $TMPH
fi
}
cat >$TMPH <<EOF
/* Automatically generated by configure - do not modify */
#define TCC_VERSION "$version"
#define CC_NAME CC_$cc_name
#define GCC_MAJOR $gcc_major
#define GCC_MINOR $gcc_minor
#define CC_NAME CC_${cc_name}
#if !CONFIG_TCC_CROSS
EOF
predefs=1
# options that are applied only to the native tcc
for v in $cpu $confvars ; do
if test "${v%=*}" = "$v"; then
v="$v=yes"
fi
R="CONFIG_$v"
echo "$R" >> config.mak
case "$R" in
# CPU
CONFIG_i386=yes) print_num TCC_TARGET_I386 1 ;;
CONFIG_x86_64=yes) print_num TCC_TARGET_X86_64 1 ;;
CONFIG_arm64=yes) print_num TCC_TARGET_ARM64 1 ;;
CONFIG_riscv64=yes) print_num TCC_TARGET_RISCV64 1 ;;
CONFIG_arm=yes) print_num TCC_TARGET_ARM 1
print_num CONFIG_TCC_CPUVER "$cpuver" ;;
CONFIG_arm_eabihf=yes) print_num TCC_ARM_EABI 1
print_num TCC_ARM_HARDFLOAT 1 ;;
CONFIG_arm_eabi=yes) print_num TCC_ARM_EABI 1 ;;
CONFIG_arm_vfp=yes) print_num TCC_ARM_VFP 1 ;;
# OS
CONFIG_WIN32=yes) print_num TCC_TARGET_PE 1 ;;
CONFIG_OSX=yes) print_num TCC_TARGET_MACHO 1 ;;
CONFIG_Android=yes) print_num TARGETOS_ANDROID 1 ;;
CONFIG_BSD=yes) print_num TARGETOS_$targetos 1
case "$targetos" in
FreeBSD) default tcc_elfinterp "/libexec/ld-elf.so.1";;
FreeBSD_kernel)
case "$cpu" in
x86_64) default tcc_elfinterp "/lib/ld-kfreebsd-x86-64.so.1";;
*) default tcc_elfinterp "/lib/ld.so.1";;
esac ;;
DragonFly) default tcc_elfinterp "/usr/libexec/ld-elf.so.2";;
NetBSD) default tcc_elfinterp "/usr/libexec/ld.elf_so";;
OpenBSD) default tcc_elfinterp "/usr/libexec/ld.so";;
esac
;;
CONFIG_uClibc=yes) print_num CONFIG_TCC_UCLIBC 1
default tcc_elfinterp "/lib/ld-uClibc.so.0"
;;
CONFIG_musl=yes) print_num CONFIG_TCC_MUSL 1
case "$cpu" in
arm64) default tcc_elfinterp "/lib/ld-musl-aarch64.so.1";;
*) default tcc_elfinterp "/lib/ld-musl-${cpu}.so.1";;
esac
;;
# other
CONFIG_libgcc=yes) print_num CONFIG_USE_LIBGCC 1 ;;
CONFIG_selinux=yes) print_num CONFIG_SELINUX 1 ;;
CONFIG_pie=yes) print_num CONFIG_TCC_PIE 1 ;;
CONFIG_pic=yes) print_num CONFIG_TCC_PIC 1 ;;
CONFIG_new_dtags=yes) print_num CONFIG_NEW_DTAGS 1 ;;
CONFIG_codesign=yes) print_num CONFIG_CODESIGN 1 ;;
CONFIG_new_macho=no) print_num CONFIG_NEW_MACHO 0 ;;
CONFIG_bcheck=no) print_num CONFIG_TCC_BCHECK 0 ;;
CONFIG_backtrace=no) print_num CONFIG_TCC_BACKTRACE 0 ;;
CONFIG_dwarf=*) print_num CONFIG_DWARF_VERSION ${R#*=} ;;
CONFIG_semlock=*) print_num CONFIG_TCC_SEMLOCK ${R#*=} ;;
CONFIG_predefs=no) predefs=0 ;;
esac
done
print_str CONFIG_USR_INCLUDE "$tcc_usrinclude"
print_str CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
print_str CONFIG_TCC_LIBPATHS "$tcc_libpaths"
print_str CONFIG_TCC_CRTPREFIX "$tcc_crtprefix"
print_str CONFIG_TCC_ELFINTERP "$tcc_elfinterp"
print_str CONFIG_TCC_SWITCHES "$tcc_switches"
print_str CONFIG_LDDIR "$tcc_lddir"
print_str CONFIG_TRIPLET "$triplet"
echo "#endif" >> $TMPH && echo >> $TMPH
print_str CONFIG_SYSROOT "$sysroot" x
test "$tccdir_auto" = "yes" || print_str CONFIG_TCCDIR "$tccdir" x
print_num CONFIG_TCC_PREDEFS "$predefs"
diff $TMPH config.h >/dev/null 2>&1
if test $? -ne 0 ; then
mv -f $TMPH config.h
@ -630,6 +676,8 @@ else
echo "config.h is unchanged"
fi
echo "@set VERSION $version" > config.texi
rm -f $TMPN* $CONFTEST
# ---------------------------------------------------------------------------

View file

@ -218,11 +218,6 @@ ST_FUNC void load(int r, SValue *sv)
int v, t, ft, fc, fr;
SValue v1;
#ifdef TCC_TARGET_PE
SValue v2;
sv = pe_getimport(sv, &v2);
#endif
fr = sv->r;
ft = sv->type.t & ~VT_DEFSIGN;
fc = sv->c.i;
@ -297,11 +292,6 @@ ST_FUNC void store(int r, SValue *v)
{
int fr, bt, ft, fc;
#ifdef TCC_TARGET_PE
SValue v2;
v = pe_getimport(v, &v2);
#endif
ft = v->type.t;
fc = v->c.i;
fr = v->r & VT_VALMASK;

View file

@ -91,7 +91,7 @@ $(X)%.o : %.S $(TCC)
$(TOP)/%.o : %.c $(TCC)
$S$(XCC) -c $< -o $@ $(XFLAGS)
$(TOP)/bcheck.o : XFLAGS += $(BFLAGS) $(if $(CONFIG_musl),-DTCC_MUSL)
$(TOP)/bcheck.o : XFLAGS += $(BFLAGS)
$(TOP)/bt-exe.o : $(TOP)/tccrun.c
$(X)crt1w.o : crt1.c

View file

@ -37,6 +37,8 @@
#include <sys/syscall.h>
#endif
#include "config.h"
#define BOUND_DEBUG (1)
#define BOUND_STATISTIC (1)
@ -161,7 +163,7 @@ static pthread_spinlock_t bounds_spin;
#define HAVE_TLS_FUNC (1)
#define HAVE_TLS_VAR (0)
#endif
#if defined TCC_MUSL || defined __ANDROID__
#if defined CONFIG_TCC_MUSL || defined __ANDROID__
# undef HAVE_CTYPE
#endif
#endif
@ -1187,7 +1189,7 @@ void __attribute__((destructor)) __bound_exit(void)
dprintf(stderr, "%s, %s():\n", __FILE__, __FUNCTION__);
if (inited) {
#if !defined(_WIN32) && !defined(__APPLE__) && !defined TCC_MUSL && \
#if !defined(_WIN32) && !defined(__APPLE__) && !defined CONFIG_TCC_MUSL && \
!defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
!defined(__ANDROID__)
if (print_heap) {

View file

@ -5,7 +5,6 @@
#define CONFIG_TCC_BACKTRACE_ONLY
#define ONE_SOURCE 1
#define pstrcpy tcc_pstrcpy
#define TCC_SEM_IMPL 1
#include "../tccrun.c"
#ifndef _WIN32

View file

@ -18,7 +18,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#if !defined ONE_SOURCE || ONE_SOURCE
#ifndef ONE_SOURCE
# define ONE_SOURCE 1
#endif
#if ONE_SOURCE
#include "tccpp.c"
#include "tccgen.c"
#include "tccdbg.c"
@ -60,7 +64,6 @@
#endif
#endif /* ONE_SOURCE */
#define TCC_SEM_IMPL 1
#include "tcc.h"
/********************************************************/
@ -1189,12 +1192,14 @@ ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags)
}
/* find [cross-]libtcc1.a and tcc helper objects in library path */
ST_FUNC void tcc_add_support(TCCState *s1, const char *filename)
ST_FUNC int tcc_add_support(TCCState *s1, const char *filename)
{
#if CONFIG_TCC_CROSS
char buf[100];
if (CONFIG_TCC_CROSSPREFIX[0])
filename = strcat(strcpy(buf, CONFIG_TCC_CROSSPREFIX), filename);
tcc_add_dll(s1, filename, AFF_PRINT_ERROR);
#endif
return tcc_add_dll(s1, filename, AFF_PRINT_ERROR);
}
#if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
@ -1229,6 +1234,11 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
return ret;
++pp;
}
#if CONFIG_TCC_CROSS
/* hack for '<cross>-tcc -nostdlib -ltcc1' to find <cross>-libtcc1.a */
if (0 == strcmp(libraryname, "tcc1"))
return tcc_add_support(s, TCC_LIBTCC1);
#endif
return tcc_add_dll(s, libraryname, AFF_PRINT_ERROR);
}
@ -1750,7 +1760,7 @@ static const char dumpmachine_str[] =
"openbsd"
#elif TARGETOS_NetBSD
"netbsd"
#elif TCC_MUSL
#elif CONFIG_TCC_MUSL
"linux-musl"
#else
"linux-gnu"
@ -1941,7 +1951,7 @@ dorun:
enable_backtrace:
s->do_backtrace = 1;
s->do_debug = s->do_debug ? s->do_debug : 1;
s->dwarf = DWARF_VERSION;
s->dwarf = CONFIG_DWARF_VERSION;
break;
#ifdef CONFIG_TCC_BCHECK
case TCC_OPTION_b:
@ -1951,7 +1961,7 @@ dorun:
#endif
case TCC_OPTION_g:
s->do_debug = 2;
s->dwarf = DWARF_VERSION;
s->dwarf = CONFIG_DWARF_VERSION;
if (strstart("dwarf", &optarg)) {
s->dwarf = (*optarg) ? (0 - atoi(optarg)) : DEFAULT_DWARF_VERSION;
} else if (isnum(*optarg)) {

View file

@ -1614,7 +1614,7 @@ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands,
/* push */
/* addi sp, sp, -offset */
gen_le32((4 << 2) | 3 |
ENCODE_RD(2) | ENCODE_RS1(2) | -8 << 20);
ENCODE_RD(2) | ENCODE_RS1(2) | (unsigned)-8 << 20);
if (REG_IS_FLOAT(reg)){
/* fsd reg, offset(sp) */
gen_le32( 0x27 | (3 << 12) |

4
tcc.c
View file

@ -18,6 +18,10 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef ONE_SOURCE
# define ONE_SOURCE 1
#endif
#include "tcc.h"
#if ONE_SOURCE
# include "libtcc.c"

144
tcc.h
View file

@ -212,7 +212,7 @@ extern long double strtold (const char *__nptr, char **__endptr);
|| defined TARGETOS_FreeBSD_kernel
# define TARGETOS_BSD 1
#elif !(defined TCC_TARGET_PE || defined TCC_TARGET_MACHO)
# define TARGETOS_Linux 1
# define TARGETOS_Linux 1 /* for tccdefs_.h */
#endif
#if defined TCC_TARGET_PE || defined TCC_TARGET_MACHO
@ -231,6 +231,11 @@ extern long double strtold (const char *__nptr, char **__endptr);
# define CONFIG_TCC_PIC 1
#endif
/* support using libtcc from threads */
#ifndef CONFIG_TCC_SEMLOCK
# define CONFIG_TCC_SEMLOCK 1
#endif
/* ------------ path configuration ------------ */
#ifndef CONFIG_SYSROOT
@ -288,58 +293,26 @@ extern long double strtold (const char *__nptr, char **__endptr);
/* name of ELF interpreter */
#ifndef CONFIG_TCC_ELFINTERP
# if TARGETOS_FreeBSD
# define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
# elif TARGETOS_FreeBSD_kernel
# if defined(TCC_TARGET_X86_64)
# define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
# else
# define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
# endif
# elif TARGETOS_DragonFly
# define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2"
# elif TARGETOS_NetBSD
# define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
# elif TARGETOS_OpenBSD
# define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.so"
# elif defined __GNU__
# if defined __GNU__
# define CONFIG_TCC_ELFINTERP "/lib/ld.so"
# elif defined(TCC_TARGET_PE)
# define CONFIG_TCC_ELFINTERP "-"
# elif defined(TCC_UCLIBC)
# define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */
# elif defined TCC_TARGET_ARM64
# if defined(TCC_MUSL)
# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-aarch64.so.1"
# else
# define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
# endif
# define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
# elif defined(TCC_TARGET_X86_64)
# if defined(TCC_MUSL)
# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-x86_64.so.1"
# else
# define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
# endif
# define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
# elif defined(TCC_TARGET_RISCV64)
# define CONFIG_TCC_ELFINTERP "/lib/ld-linux-riscv64-lp64d.so.1"
# elif !defined(TCC_ARM_EABI)
# if defined(TCC_MUSL)
# if defined(TCC_TARGET_I386)
# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-i386.so.1"
# else
# define CONFIG_TCC_ELFINTERP "/lib/ld-musl-arm.so.1"
# endif
# else
# define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
# endif
# elif defined(TCC_ARM_EABI)
# define DEFAULT_ELFINTERP(s) default_elfinterp(s)
# else
# define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
# endif
#endif
/* var elf_interp dans *-gen.c */
#ifdef CONFIG_TCC_ELFINTERP
#ifndef DEFAULT_ELFINTERP
# define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP
#else
# define DEFAULT_ELFINTERP(s) default_elfinterp(s)
#endif
/* (target specific) libtcc1.a */
@ -347,15 +320,16 @@ extern long double strtold (const char *__nptr, char **__endptr);
# define TCC_LIBTCC1 "libtcc1.a"
#endif
#ifndef CONFIG_TCC_CROSSPREFIX
# define CONFIG_TCC_CROSSPREFIX ""
#endif
/* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
#if defined CONFIG_USE_LIBGCC && !defined TCC_LIBGCC
#define TCC_LIBGCC USE_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
#endif
/* <cross-prefix-to->libtcc1.a */
#ifndef CONFIG_TCC_CROSSPREFIX
# define CONFIG_TCC_CROSSPREFIX ""
#endif
/* -------------------------------------------- */
#include "libtcc.h"
@ -370,12 +344,7 @@ extern long double strtold (const char *__nptr, char **__endptr);
#endif
#ifndef ONE_SOURCE
# define ONE_SOURCE 1
#endif
/* support using libtcc from threads */
#ifndef CONFIG_TCC_SEMLOCK
# define CONFIG_TCC_SEMLOCK 1
# define ONE_SOURCE 0
#endif
#if ONE_SOURCE
@ -1310,7 +1279,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
#endif
ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
ST_FUNC void tcc_add_support(TCCState *s1, const char *filename);
ST_FUNC int tcc_add_support(TCCState *s1, const char *filename);
#ifdef CONFIG_TCC_BCHECK
ST_FUNC void tcc_add_bcheck(TCCState *s1);
#endif
@ -1821,7 +1790,6 @@ ST_FUNC int pe_load_file(struct TCCState *s1, int fd, const char *filename);
ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
#endif
#ifdef TCC_TARGET_X86_64
ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
@ -1924,8 +1892,8 @@ ST_FUNC void tcc_tcov_reset_ind(TCCState *s1);
#endif
/* default dwarf version for "-g". use 0 to emit stab debug infos */
#ifndef DWARF_VERSION
# define DWARF_VERSION 0 //DEFAULT_DWARF_VERSION
#ifndef CONFIG_DWARF_VERSION
# define CONFIG_DWARF_VERSION 0
#endif
#if defined TCC_TARGET_PE
@ -1939,16 +1907,38 @@ ST_FUNC void tcc_tcov_reset_ind(TCCState *s1);
/********************************************************/
#if CONFIG_TCC_SEMLOCK
#if defined _WIN32
typedef struct { int init; CRITICAL_SECTION cr; } TCCSem;
typedef struct { int init; CRITICAL_SECTION cs; } TCCSem;
static inline void wait_sem(TCCSem *p) {
if (!p->init)
InitializeCriticalSection(&p->cs), p->init = 1;
EnterCriticalSection(&p->cs);
}
static inline void post_sem(TCCSem *p) {
LeaveCriticalSection(&p->cs);
}
#elif defined __APPLE__
#include <dispatch/dispatch.h>
typedef struct { int init; dispatch_semaphore_t sem; } TCCSem;
static inline void wait_sem(TCCSem *p) {
if (!p->init)
p->sem = dispatch_semaphore_create(1), p->init = 1;
dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
}
static inline void post_sem(TCCSem *p) {
dispatch_semaphore_signal(p->sem);
}
#else
#include <semaphore.h>
typedef struct { int init; sem_t sem; } TCCSem;
static inline void wait_sem(TCCSem *p) {
if (!p->init)
sem_init(&p->sem, 0, 1), p->init = 1;
while (sem_wait(&p->sem) < 0 && errno == EINTR);
}
static inline void post_sem(TCCSem *p) {
sem_post(&p->sem);
}
#endif
ST_FUNC void wait_sem(TCCSem *p);
ST_FUNC void post_sem(TCCSem *p);
#define TCC_SEM(s) TCCSem s
#define WAIT_SEM wait_sem
#define POST_SEM post_sem
@ -2009,41 +1999,3 @@ PUB_FUNC void tcc_exit_state(TCCState *s1);
# define TCC_SET_STATE(fn) (tcc_enter_state(s1),fn)
# define _tcc_error use_tcc_error_noabort
#endif
#if CONFIG_TCC_SEMLOCK && TCC_SEM_IMPL
#undef TCC_SEM_IMPL
#if defined _WIN32
ST_FUNC void wait_sem(TCCSem *p)
{
if (!p->init)
InitializeCriticalSection(&p->cr), p->init = 1;
EnterCriticalSection(&p->cr);
}
ST_FUNC void post_sem(TCCSem *p)
{
LeaveCriticalSection(&p->cr);
}
#elif defined __APPLE__
ST_FUNC void wait_sem(TCCSem *p)
{
if (!p->init)
p->sem = dispatch_semaphore_create(1), p->init = 1;
dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
}
ST_FUNC void post_sem(TCCSem *p)
{
dispatch_semaphore_signal(p->sem);
}
#else
ST_FUNC void wait_sem(TCCSem *p)
{
if (!p->init)
sem_init(&p->sem, 0, 1), p->init = 1;
while (sem_wait(&p->sem) < 0 && errno == EINTR);
}
ST_FUNC void post_sem(TCCSem *p)
{
sem_post(&p->sem);
}
#endif
#endif

View file

@ -826,6 +826,7 @@ static void asm_parse_directive(TCCState *s1, int global)
{
Sym *sym;
const char *newtype;
int st_type;
next();
sym = get_asm_sym(tok, NULL);
@ -840,11 +841,17 @@ static void asm_parse_directive(TCCState *s1, int global)
}
if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) {
sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC;
if (IS_ASM_SYM(sym))
sym->type.t = (sym->type.t & ~VT_ASM) | VT_ASM_FUNC;
st_type = STT_FUNC;
set_st_type:
if (sym->c) {
ElfSym *esym = elfsym(sym);
esym->st_info = ELFW(ST_INFO)(ELFW(ST_BIND)(esym->st_info), STT_FUNC);
esym->st_info = ELFW(ST_INFO)(ELFW(ST_BIND)(esym->st_info), st_type);
}
} else if (!strcmp(newtype, "object") || !strcmp(newtype, "STT_OBJECT")) {
st_type = STT_OBJECT;
goto set_st_type;
} else
tcc_warning_c(warn_unsupported)("change type of '%s' from 0x%x to '%s' ignored",
get_tok_str(sym->v, NULL), sym->type.t, newtype);
@ -1381,7 +1388,7 @@ ST_FUNC void asm_global_instr(void)
expect("';'");
#ifdef ASM_DEBUG
printf("asm_global: \"%s\"\n", (char *)astr.data);
printf("asm_global: \"%s\"\n", (char *)astr->data);
#endif
cur_text_section = text_section;
ind = cur_text_section->data_offset;

View file

@ -90,6 +90,8 @@ ST_FUNC void tccelf_new(TCCState *s)
}
#if TCC_EH_FRAME
if (s->output_format != TCC_OUTPUT_FORMAT_ELF)
s->unwind_tables = 0;
tcc_eh_frame_start(s);
#endif
@ -101,6 +103,13 @@ ST_FUNC void tccelf_new(TCCState *s)
lbounds_section = new_section(s, ".lbounds", SHT_PROGBITS, shf_RELRO);
}
#endif
#ifdef TCC_TARGET_PE
/* to make sure that -ltcc1 -Wl,-e,_start will grab the startup code
from libtcc1.a (unless _start defined) */
if (s->elf_entryname)
set_global_sym(s, s->elf_entryname, NULL, 0); /* SHN_UNDEF */
#endif
}
ST_FUNC void free_section(Section *s)
@ -2157,10 +2166,8 @@ static int sort_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
} else {
j = 0x700;
}
if (s1->output_format != TCC_OUTPUT_FORMAT_ELF) {
if (j >= 0x700 || 0 == strncmp(s->name, ".eh_frame", 9))
s->sh_size = 0, j = 0x900;
}
if (j >= 0x700 && s1->output_format != TCC_OUTPUT_FORMAT_ELF)
s->sh_size = 0, j = 0x900;
if (s->sh_type == SHT_SYMTAB || s->sh_type == SHT_DYNSYM) {
k = 0x10;
@ -3149,23 +3156,24 @@ invalid:
if (sh->sh_type == SHT_RELX)
sh = &shdr[sh->sh_info];
/* ignore sections types we do not handle (plus relocs to those) */
if (0 == strncmp(strsec + sh->sh_name, ".debug_", 7)
|| 0 == strncmp(strsec + sh->sh_name, ".stab", 5)) {
sh_name = strsec + sh->sh_name;
if (0 == strncmp(sh_name, ".debug_", 7)
|| 0 == strncmp(sh_name, ".stab", 5)) {
if (!s1->do_debug || seencompressed)
continue;
} else if (0 == strncmp(sh_name, ".eh_frame", 9)) {
if (NULL == eh_frame_section)
continue;
} else
if (sh->sh_type != SHT_PROGBITS &&
#ifdef TCC_ARM_EABI
sh->sh_type != SHT_ARM_EXIDX &&
#endif
#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD
sh->sh_type != SHT_X86_64_UNWIND &&
#endif
sh->sh_type != SHT_NOTE &&
sh->sh_type != SHT_NOBITS &&
sh->sh_type != SHT_PREINIT_ARRAY &&
sh->sh_type != SHT_INIT_ARRAY &&
sh->sh_type != SHT_FINI_ARRAY
#ifdef TCC_ARM_EABI
&& sh->sh_type != SHT_ARM_EXIDX
#endif
)
continue;
@ -3176,24 +3184,29 @@ invalid:
/* find corresponding section, if any */
for(j = 1; j < s1->nb_sections;j++) {
s = s1->sections[j];
if (!strcmp(s->name, sh_name)) {
if (!strncmp(sh_name, ".gnu.linkonce",
sizeof(".gnu.linkonce") - 1)) {
/* if a 'linkonce' section is already present, we
do not add it again. It is a little tricky as
symbols can still be defined in
it. */
sm_table[i].link_once = 1;
goto next;
}
if (stab_section) {
if (s == stab_section)
stab_index = i;
if (s == stab_section->link)
stabstr_index = i;
}
goto found;
if (strcmp(s->name, sh_name))
continue;
if (sh->sh_type != s->sh_type
&& s != eh_frame_section
) {
tcc_error_noabort("section type conflict: %s %02x <> %02x", s->name, sh->sh_type, s->sh_type);
goto the_end;
}
if (!strncmp(sh_name, ".gnu.linkonce", 13)) {
/* if a 'linkonce' section is already present, we
do not add it again. It is a little tricky as
symbols can still be defined in
it. */
sm_table[i].link_once = 1;
goto next;
}
if (stab_section) {
if (s == stab_section)
stab_index = i;
if (s == stab_section->link)
stabstr_index = i;
}
goto found;
}
/* not found: create new section */
s = new_section(s1, sh_name, sh->sh_type, sh->sh_flags & ~SHF_GROUP);
@ -3203,14 +3216,6 @@ invalid:
s->sh_entsize = sh->sh_entsize;
sm_table[i].new_section = 1;
found:
if (sh->sh_type != s->sh_type
#if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD
&& strcmp (s->name, ".eh_frame")
#endif
) {
tcc_error_noabort("invalid section type");
goto the_end;
}
/* align start of section */
s->data_offset += -s->data_offset & (sh->sh_addralign - 1);
if (sh->sh_addralign > s->sh_addralign)
@ -3907,7 +3912,7 @@ static int ld_add_file(TCCState *s1, const char filename[])
static int ld_add_file_list(TCCState *s1, const char *cmd, int as_needed)
{
char filename[1024], libname[1024];
char filename[1024], libname[1016];
int t, group, nblibs = 0, ret = 0;
char **libs = NULL;

View file

@ -157,6 +157,7 @@ static void gv_dup(void);
static int get_temp_local_var(int size,int align,int *r2);
static void cast_error(CType *st, CType *dt);
static void end_switch(void);
static void do_Static_assert(void);
/* ------------------------------------------------------------------------- */
/* Automagical code suppression */
@ -4340,8 +4341,6 @@ static void struct_layout(CType *type, AttributeDef *ad)
}
}
static void do_Static_assert(void);
/* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
static void struct_decl(CType *type, int u)
{
@ -6027,6 +6026,13 @@ special_math_val:
if (r & VT_SYM) {
vtop->c.i = 0;
#ifdef TCC_TARGET_PE
if (s->a.dllimport) {
mk_pointer(&vtop->type);
vtop->r |= VT_LVAL;
indir();
}
#endif
} else if (r == VT_CONST && IS_ENUM_VAL(s->type.t)) {
vtop->c.i = s->enum_val;
}

178
tccpe.c
View file

@ -384,47 +384,6 @@ static const char *pe_export_name(TCCState *s1, ElfW(Sym) *sym)
return name;
}
static int pe_find_import(TCCState * s1, ElfW(Sym) *sym)
{
char buffer[200];
const char *s, *p;
int sym_index = 0, n = 0;
int a, err = 0;
do {
s = pe_export_name(s1, sym);
a = 0;
if (n) {
/* second try: */
if (sym->st_other & ST_PE_STDCALL) {
/* try w/0 stdcall deco (windows API convention) */
p = strrchr(s, '@');
if (!p || s[0] != '_')
break;
strcpy(buffer, s+1)[p-s-1] = 0;
} else if (s[0] != '_') { /* try non-ansi function */
buffer[0] = '_', strcpy(buffer + 1, s);
} else if (0 == memcmp(s, "__imp_", 6)) { /* mingw 2.0 */
strcpy(buffer, s + 6), a = 1;
} else if (0 == memcmp(s, "_imp__", 6)) { /* mingw 3.7 */
strcpy(buffer, s + 6), a = 1;
} else {
continue;
}
s = buffer;
}
sym_index = find_elf_sym(s1->dynsymtab_section, s);
// printf("find (%d) %d %s\n", n, sym_index, s);
if (sym_index
&& ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT
&& 0 == (sym->st_other & ST_PE_IMPORT)
&& 0 == a
) err = -1, sym_index = 0;
} while (0 == sym_index && ++n < 2);
return n == 2 ? err : sym_index;
}
/*----------------------------------------------------------------------------*/
static int dynarray_assoc(void **pp, int n, int key)
{
@ -905,12 +864,16 @@ static void pe_build_imports(struct pe_info *pe)
int iat_index = p->symbols[k]->iat_index;
int sym_index = p->symbols[k]->sym_index;
ElfW(Sym) *imp_sym = (ElfW(Sym) *)pe->s1->dynsymtab_section->data + sym_index;
ElfW(Sym) *org_sym = (ElfW(Sym) *)symtab_section->data + iat_index;
const char *name = (char*)pe->s1->dynsymtab_section->link->data + imp_sym->st_name;
int ordinal;
org_sym->st_value = thk_ptr;
org_sym->st_shndx = pe->thunk->sh_num;
/* patch symbol (and possibly its underscored alias) */
do {
ElfW(Sym) *esym = (ElfW(Sym) *)symtab_section->data + iat_index;
iat_index = esym->st_value;
esym->st_value = thk_ptr;
esym->st_shndx = pe->thunk->sh_num;
} while (iat_index);
if (dllref)
v = 0, ordinal = imp_sym->st_value; /* ordinal from pe_load_def */
@ -1276,34 +1239,9 @@ add_section:
return 0;
}
/*----------------------------------------------------------------------------*/
static int pe_isafunc(TCCState *s1, int sym_index)
{
Section *sr = text_section->reloc;
ElfW_Rel *rel, *rel_end;
ElfW(Addr)info = ELFW(R_INFO)(sym_index, R_XXX_FUNCCALL);
#ifdef R_XXX_FUNCCALL2
ElfW(Addr)info2 = ELFW(R_INFO)(sym_index, R_XXX_FUNCCALL2);
#endif
if (!sr)
return 0;
rel_end = (ElfW_Rel *)(sr->data + sr->data_offset);
for (rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) {
if (rel->r_info == info)
return 1;
#ifdef R_XXX_FUNCCALL2
if (rel->r_info == info2)
return 1;
#endif
}
return 0;
}
/*----------------------------------------------------------------------------*/
static int pe_check_symbols(struct pe_info *pe)
{
ElfW(Sym) *sym;
int sym_index, sym_end;
int ret = 0;
TCCState *s1 = pe->s1;
@ -1312,34 +1250,54 @@ static int pe_check_symbols(struct pe_info *pe)
sym_end = symtab_section->data_offset / sizeof(ElfW(Sym));
for (sym_index = 1; sym_index < sym_end; ++sym_index) {
sym = (ElfW(Sym) *)symtab_section->data + sym_index;
ElfW(Sym) *sym = (ElfW(Sym) *)symtab_section->data + sym_index;
if (sym->st_shndx == SHN_UNDEF) {
const char *name = (char*)symtab_section->link->data + sym->st_name;
unsigned type = ELFW(ST_TYPE)(sym->st_info);
int imp_sym = pe_find_import(pe->s1, sym);
int imp_sym;
struct import_symbol *is;
if (imp_sym <= 0)
goto not_found;
int _imp_, n;
char buffer[200];
const char *s, *p;
if (type == STT_NOTYPE) {
/* symbols from assembler have no type, find out which */
if (pe_isafunc(s1, sym_index))
type = STT_FUNC;
else
type = STT_OBJECT;
}
n = _imp_ = 0;
do {
s = pe_export_name(s1, sym);
if (n) {
/* second try: */
if (sym->st_other & ST_PE_STDCALL) {
/* try w/0 stdcall deco (windows API convention) */
p = strrchr(s, '@');
if (!p || s[0] != '_')
break;
strcpy(buffer, s+1)[p-s-1] = 0, s = buffer;
} else if (s[0] != '_') { /* try non-ansi function */
buffer[0] = '_', strcpy(buffer + 1, s), s = buffer;
} else if (0 == memcmp(s, "_imp__", 6)) { /* mingw 3.7 */
s += 6, _imp_ = 1;
} else if (0 == memcmp(s, "__imp_", 6)) { /* mingw 2.0 */
s += 6, _imp_ = 1;
} else {
break;
}
}
imp_sym = find_elf_sym(s1->dynsymtab_section, s);
} while (0 == imp_sym && ++n < 2);
//printf("pe_find_export (%d) %4x %s\n", n, imp_sym, name);
if (0 == imp_sym)
continue; /* will throw the 'undefined' error in relocate_syms() */
is = pe_add_import(pe, imp_sym);
if (type == STT_FUNC) {
if (type == STT_FUNC
/* symbols from assembler often have no type */
|| type == STT_NOTYPE) {
unsigned offset = is->thk_offset;
if (offset) {
/* got aliased symbol, like stricmp and _stricmp */
} else {
char buffer[100];
unsigned char *p;
/* add a helper symbol, will be patched later in
@ -1372,29 +1330,19 @@ static int pe_check_symbols(struct pe_info *pe)
}
/* tcc_realloc might have altered sym's address */
sym = (ElfW(Sym) *)symtab_section->data + sym_index;
/* patch the original symbol */
sym->st_value = offset;
sym->st_shndx = text_section->sh_num;
sym->st_other &= ~ST_PE_EXPORT; /* do not export */
continue;
}
if (type == STT_OBJECT) { /* data, ptr to that should be */
if (0 == is->iat_index) {
/* original symbol will be patched later in pe_build_imports */
is->iat_index = sym_index;
continue;
}
} else { /* STT_OBJECT */
if (0 == _imp_ && 0 == (sym->st_other & ST_PE_IMPORT))
ret = tcc_error_noabort("symbol '%s' is missing __declspec(dllimport)", name);
/* original symbol will be patched later in pe_build_imports */
sym->st_value = is->iat_index; /* chain potential alias */
is->iat_index = sym_index;
}
not_found:
if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK)
/* STB_WEAK undefined symbols are accepted */
continue;
ret = tcc_error_noabort("undefined symbol '%s'%s", name,
imp_sym < 0 ? ", missing __declspec(dllimport)?":"");
} else if (pe->s1->rdynamic
&& ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
/* if -rdynamic option, then export all non local symbols */
@ -1544,36 +1492,6 @@ static void pe_print_sections(TCCState *s1, const char *fname)
#endif
/* ------------------------------------------------------------- */
/* helper function for load/store to insert one more indirection */
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2)
{
int r2;
if ((sv->r & (VT_VALMASK|VT_SYM)) != (VT_CONST|VT_SYM) || (sv->r2 != VT_CONST))
return sv;
if (!sv->sym->a.dllimport)
return sv;
// printf("import %04x %04x %04x %s\n", sv->type.t, sv->sym->type.t, sv->r, get_tok_str(sv->sym->v, NULL));
memset(v2, 0, sizeof *v2);
v2->type.t = VT_PTR;
v2->r = VT_CONST | VT_SYM | VT_LVAL;
v2->sym = sv->sym;
r2 = get_reg(RC_INT);
load(r2, v2);
v2->r = r2;
if ((uint32_t)sv->c.i) {
vpushv(v2);
vpushi(sv->c.i);
gen_opi('+');
*v2 = *vtop--;
}
v2->type.t = sv->type.t;
v2->r |= sv->r & VT_LVAL;
return v2;
}
#endif
ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value)
{

13
tccpp.c
View file

@ -1927,15 +1927,18 @@ ST_FUNC void preprocess(int is_bof)
tok_flags |= TOK_FLAG_ENDIF;
}
break;
case TOK_LINE:
next_nomacro();
if (tok != TOK_PPNUM) {
_line_err:
if (parse_flags & PARSE_FLAG_ASM_FILE)
goto ignore;
tcc_error("wrong #line format");
}
goto _line_num;
case TOK_PPNUM:
if (parse_flags & PARSE_FLAG_ASM_FILE)
goto ignore;
_line_num:
for (n = 0, q = tokc.str.data; *q; ++q) {
if (!isnum(*q))
goto _line_err;
@ -1948,9 +1951,7 @@ ST_FUNC void preprocess(int is_bof)
tccpp_putfile(tokc.str.data + 1);
} else
goto _line_err;
next_nomacro();
} else if (parse_flags & PARSE_FLAG_ASM_FILE)
goto ignore;
}
if (file->fd > 0)
total_lines += file->line_num - n;
file->line_num = n;
@ -3586,7 +3587,7 @@ static void putdefs(CString *cs, const char *p)
static void tcc_predefs(TCCState *s1, CString *cs, int is_asm)
{
cstr_printf(cs, "#define __TINYC__ 9%.2s\n", TCC_VERSION + 4);
cstr_printf(cs, "#define __TINYC__ 9%.2s\n", *& TCC_VERSION + 4);
putdefs(cs, target_machine_defs);
putdefs(cs, target_os_defs);

View file

@ -105,14 +105,14 @@ static void win64_del_function_table(void *);
#define PAGEALIGN(n) ((addr_t)n + (-(addr_t)n & (PAGESIZE-1)))
#if !_WIN32 && !__APPLE__
//#define HAVE_SELINUX 1
//#define CONFIG_SELINUX 1
#endif
static int rt_mem(TCCState *s1, int size)
{
void *ptr;
int ptr_diff = 0;
#ifdef HAVE_SELINUX
#ifdef CONFIG_SELINUX
/* Using mmap instead of malloc */
void *prw;
char tmpfname[] = "/tmp/.tccrunXXXXXX";
@ -185,7 +185,7 @@ ST_FUNC void tcc_run_free(TCCState *s1)
return;
st_unlink(s1);
size = s1->run_size;
#ifdef HAVE_SELINUX
#ifdef CONFIG_SELINUX
munmap(ptr, size);
#else
/* unprotect memory to make it usable for malloc again */
@ -381,7 +381,7 @@ redo:
if (copy == 2) { /* set permissions */
if (n == 0) /* no data */
continue;
#ifdef HAVE_SELINUX
#ifdef CONFIG_SELINUX
if (k == 0) /* SHF_EXECINSTR has its own mapping */
continue;
#endif

View file

@ -4,8 +4,8 @@
TOP = ..
include $(TOP)/Makefile
VPATH = $(TOPSRC)/tests $(TOPSRC) $(TOP)
CFLAGS := $(filter-out -g% -O%,$(CFLAGS)) -I$(TOPSRC) $(LDFLAGS)
VPATH = $(TOPSRC)/tests $(TOPSRC)
CFLAGS := $(filter-out -g% -O%,$(CFLAGS)) -I$(TOPSRC) -I$(TOP) $(LDFLAGS)
# what tests to run
TESTS = \
@ -69,7 +69,7 @@ ifneq (,$(filter FreeBSD NetBSD,$(TARGETOS)))
TESTS += test1
endif
RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS)
RUN_TCC = -run $(TOPSRC)/tcc.c $(TCCFLAGS)
DISAS = objdump -d
ifdef CONFIG_OSX
DUMPTCC = (set -x; $(TOP)/tcc -vv; otool -L $(TOP)/tcc; exit 1)
@ -95,13 +95,13 @@ hello-run: ../examples/ex1.c
libtes%: libtcc_tes%$(EXESUF)
@echo ------------ $@ ------------
./libtcc_tes$*$(EXESUF) $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES)
./libtcc_tes$*$(EXESUF) $(TOPSRC)/tcc.c $(TCCFLAGS)
libtcc_test$(EXESUF): libtcc_test.c $(LIBTCC)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
libtcc_test$(EXESUF): libtcc_test.c
$(CC) -o $@ $< $(CFLAGS) $(-LTCC) $(LIBS)
libtcc_test_mt$(EXESUF): libtcc_test_mt.c $(LIBTCC)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
libtcc_test_mt$(EXESUF): libtcc_test_mt.c
$(CC) -o $@ $< $(CFLAGS) $(-LTCC) $(LIBS)
%-dir:
@echo ------------ $@ ------------
@ -109,7 +109,7 @@ libtcc_test_mt$(EXESUF): libtcc_test_mt.c $(LIBTCC)
# test.ref - generate using cc
test.ref: tcctest.c
$(CC) -o tcctest.gcc $< $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
$(CC) -o tcctest.gcc $< $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
./tcctest.gcc > $@
# auto test
@ -161,23 +161,23 @@ test4_static: tcctest.c test.ref
# use tcc to create libtcc.so/.dll and the tcc(.exe) frontend and run them
dlltest:
@echo ------------ $@ ------------
$(TCC) $(NATIVE_DEFINES) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF)
$(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
$(TCC) -DLIBTCC_AS_DLL $(TOPSRC)/libtcc.c $(LIBS) -shared -o libtcc2$(DLLSUF)
$(TCC) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
ifeq (,$(filter Darwin WIN32,$(TARGETOS)))
@echo ------------ $@ with PIC ------------
$(CC) $(CFLAGS) -fPIC $(NATIVE_DEFINES) -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
$(CC) $(CFLAGS) -fPIC -DLIBTCC_AS_DLL -c $(TOPSRC)/libtcc.c
$(TCC) libtcc.o $(LIBS) -shared -o libtcc2$(DLLSUF)
$(TCC) $(NATIVE_DEFINES) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
$(TCC) -DONE_SOURCE=0 $(TOPSRC)/tcc.c libtcc2$(DLLSUF) $(LIBS) -Wl,-rpath=. -o tcc2$(EXESUF)
./tcc2$(EXESUF) $(TCCFLAGS) $(RUN_TCC) -run $(TOPSRC)/examples/ex1.c
endif
@rm tcc2$(EXESUF) libtcc2$(DLLSUF)
memtest:
@echo ------------ $@ ------------
$(CC) $(CFLAGS) $(NATIVE_DEFINES) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) $(TOPSRC)/tcc.c $(LIBS)
./memtest-tcc$(EXESUF) $(TCCFLAGS) $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS) -w $(TOPSRC)/tests/tcctest.c
$(CC) $(CFLAGS) -DMEM_DEBUG=2 $(TOPSRC)/tcc.c $(LIBS) -o memtest-tcc$(EXESUF)
./memtest-tcc$(EXESUF) $(TCCFLAGS) $(TOPSRC)/tcc.c $(LIBS)
./memtest-tcc$(EXESUF) $(TCCFLAGS) -run $(TOPSRC)/tcc.c $(TCCFLAGS) -w $(TOPSRC)/tests/tcctest.c
@echo OK
# memory and bound check auto test
@ -204,9 +204,9 @@ btest: boundtest.c
tccb:
@echo ------------ $@ ------------
$(TCC) -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o tccb1.exe
$(TCC) -b $(TOPSRC)/tcc.c $(TCCFLAGS) -o tccb1.exe
mv tccb1.exe tccb2.exe
./tccb2.exe -b $(TOPSRC)/tcc.c $(TCCFLAGS) $(NATIVE_DEFINES) -o tccb1.exe
./tccb2.exe -b $(TOPSRC)/tcc.c $(TCCFLAGS) -o tccb1.exe
cmp -s tccb1.exe tccb2.exe && echo "Exe Bound-Test OK"
@ -221,7 +221,7 @@ speedtest: ex2 ex3
weaktest: tcctest.c test.ref
@echo ------------ $@ ------------
$(TCC) -c $< -o weaktest.tcc.o
$(CC) -c $< -o weaktest.gcc.o $(NATIVE_DEFINES) $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
$(CC) -c $< -o weaktest.gcc.o $(CFLAGS) -w -O0 -std=gnu99 -fno-omit-frame-pointer
objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt
objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt
diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK"
@ -250,11 +250,11 @@ asmtest2: MAYBE_RUN_TCC = $(RUN_TCC)
# Check that code generated by libtcc is binary compatible with
# that generated by CC
abitest-cc.exe: abitest.c $(LIBTCC)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) -w
abitest-cc.exe: abitest.c
$(CC) -o $@ $^ $(CFLAGS) $(-LTCC) $(LIBS) -w
abitest-tcc.exe: abitest.c libtcc.c
$(TCC) -o $@ $^ $(NATIVE_DEFINES) $(LIBS)
$(TCC) -o $@ $^ $(LIBS)
abitest-% : abitest-%.exe
@echo ------------ $@ ------------
@ -318,7 +318,7 @@ CROSS-TGTS = \
define CROSS-COMPILE
@echo " . $(1)"
$(TCC) $(DEF-$1) -DTCC_CROSS_TEST -run $(TOPSRC)/tcc.c \
$(TCC) $(DEF-$1) -DCONFIG_TCC_CROSS -DTCC_CROSS_TEST -run $(TOPSRC)/tcc.c \
-c $(if $(findstring c67,$1),$(filter %/ex3.c,$^),$<) -w $(TCCFLAGS)
endef

View file

@ -364,11 +364,6 @@ void load(int r, SValue *sv)
int v, t, ft, fc, fr;
SValue v1;
#ifdef TCC_TARGET_PE
SValue v2;
sv = pe_getimport(sv, &v2);
#endif
fr = sv->r;
ft = sv->type.t & ~VT_DEFSIGN;
fc = sv->c.i;
@ -573,11 +568,6 @@ void store(int r, SValue *v)
/* store the REX prefix in this variable when PIC is enabled */
int pic = 0;
#ifdef TCC_TARGET_PE
SValue v2;
v = pe_getimport(v, &v2);
#endif
fr = v->r & VT_VALMASK;
ft = v->type.t;
fc = v->c.i;