diff --git a/.gitignore b/.gitignore index da0d094..348aba7 100644 --- a/.gitignore +++ b/.gitignore @@ -36,10 +36,13 @@ p2.c tcctest[1234] test[1234].out tests/tcclib.h -tests/tcctest.gcc +tests/tcctest.cc tests/weaktest.*.o.txt tests/tests2/fred.txt +tests/tests2/*.exe tests/hello +tests/abitest-*cc +tests/vla_test .gdb_history tcc.1 tcc.pod @@ -53,6 +56,7 @@ lib/x86_64 lib/i386 lib/x86_64-win32 lib/i386-win32 +lib/arm tcc-doc.info conftest* tiny_libmaker diff --git a/CMakeLists.txt b/CMakeLists.txt index fb0d968..df3c831 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,58 +195,53 @@ macro(make_tcc native_name cross_name cross_enabled definitions tcc_sources libt endif() endmacro() -make_tcc("Win32" i386-win32 TCC_BUILD_WIN32 +make_tcc("Win32" i386-w64-mingw32 TCC_BUILD_WIN32 "TCC_TARGET_I386;TCC_TARGET_PE" "${I386_SOURCES};tccpe.c" tiny_libmaker_32 "${LIBTCC1_I386_SOURCES};${LIBTCC1_WIN_SOURCES}" "win32/include;win32/include/winapi" ) -make_tcc("Win64" x86_64-win32 TCC_BUILD_WIN64 +make_tcc("Win64" x86_64-w64-mingw32 TCC_BUILD_WIN64 "TCC_TARGET_X86_64;TCC_TARGET_PE" "${X86_64_SOURCES};tccpe.c" tiny_libmaker_64 "lib/alloca86_64.S;${LIBTCC1_WIN_SOURCES}" "win32/include;win32/include/winapi" ) -make_tcc("WinCE" arm-win32 TCC_BUILD_WINCE +make_tcc("WinCE" arm-wince-mingw32ce TCC_BUILD_WINCE "TCC_TARGET_ARM;TCC_ARM_VERSION=${TCC_ARM_VERSION};TCC_TARGET_PE" "${ARM_SOURCES};tccpe.c" "" "" "" ) -make_tcc("i386" i386 TCC_BUILD_I386 +make_tcc("i386" i386-linux-gnu TCC_BUILD_I386 TCC_TARGET_I386 "${I386_SOURCES}" tiny_libmaker_32 "${LIBTCC1_I386_SOURCES}" "" ) -make_tcc("x86_64" x86_64 TCC_BUILD_X64 +make_tcc("x86_64" x86_64-linux-gnu TCC_BUILD_X64 TCC_TARGET_X86_64 "${X86_64_SOURCES}" tiny_libmaker_64 "lib/alloca86_64.S" "" ) set(ARM_DEFINITIONS TCC_TARGET_ARM TCC_ARM_VERSION=${TCC_ARM_VERSION}) -make_tcc("ARM" arm TCC_BUILD_ARM - "${ARM_DEFINITIONS};WITHOUT_LIBTCC" - "${ARM_SOURCES}" - "" "" "" -) -make_tcc("" arm-eabihf TCC_BUILD_ARM_EABIHF +make_tcc("" arm-linux-gnueabihf TCC_BUILD_ARM_EABIHF "${ARM_DEFINITIONS};TCC_ARM_EABI;TCC_ARM_HARDFLOAT" "${ARM_SOURCES}" "" "" "" ) -make_tcc("" arm-eabi TCC_BUILD_ARM_EABI +make_tcc("" arm-linux-gnueabi TCC_BUILD_ARM_EABI "${ARM_DEFINITIONS};TCC_ARM_EABI" "${ARM_SOURCES}" "" "" "" ) -make_tcc("" arm-fpa TCC_BUILD_ARM_FPA +make_tcc("" arm-linux-fpa TCC_BUILD_ARM_FPA "${ARM_DEFINITIONS}" "${ARM_SOURCES}" "" "" "" ) -make_tcc("" arm-fpa-ld TCC_BUILD_ARM_FPA_LD +make_tcc("" arm-linux-fpa-ld TCC_BUILD_ARM_FPA_LD "${ARM_DEFINITIONS};LDOUBLE_SIZE=12" "${ARM_SOURCES}" "" "" "" ) -make_tcc("" arm-vfp TCC_BUILD_ARM_VFP +make_tcc("" arm-linux-gnu TCC_BUILD_ARM_VFP "${ARM_DEFINITIONS};TCC_ARM_VFP" "${ARM_SOURCES}" "" "" "" diff --git a/Changelog b/Changelog index 2c03673..4370310 100644 --- a/Changelog +++ b/Changelog @@ -1,7 +1,126 @@ Version 0.9.27: +Licensing: + +- TinyCC partly relicensed to MIT license + +User interface: + +- define __STDC_HOSTED__ (Michael Matz, Urs Janssen) +- added support for CPATH, C_INCLUDE_PATH and LD_LIBRARY_PATH (Andrew Aladjev +and Urs Janssen) +- added option -norunsrc to control argv[0] with tcc -run (James Lyon) +- improve --with-libgcc configure help (grischka) +- improve error message when memory is full (grischka) +- improve wording about compiler switches in documentation (Thomas Preud'homme) +- use GNU triplet prefix for cross compiler names (Thomas Preud'homme) +- ignore unknown linker optimization and as-needed option (Austin English) + +Features: + +- added ABI tests with native compiler using libtcc (James Lyon) +- added CMake build system with support for cross-compilation (James Lyon) +- improved variable length array support (James Lyon) +- add the possibility to use noname functions by ordinal (YX Hao) +- add a install-strip target to install tcc (Thomas Preud'homme) +- add runtime selection of float ABI on ARM (Thomas Preud'homme) +- add shared lib support on x86-64 (Michael Matz) + Platforms: -- Support Debian GNU/kfreeBSD 64bit userspace (Thomas Preud'homme) +- support Debian GNU/kfreeBSD 64bit userspace (Thomas Preud'homme) +- fix GNU/Hurd interpreter path (Thomas Preud'homme) +- fix configure script for FreeBSD host (Thomas Preud'homme) +- make tcc -run work reliably on ARM by flushing caches (Thomas Preud'homme) +- many x86-64 ABI fixes incl. XMM register passing (James Lyon) +- improve compatibility with mingw's long double (James Lyon) +- avoid .stabstr section name to be truncated on win32 (Roy) +- add support for load/store of _Bool value (Thomas Preud'homme) +- detect instruction with incorrect operands on x86-64 (Thomas Preud'homme) +- improved relocations on ARM (Thomas Preud'homme) +- add va_* macro implementation for ARM (Thomas Preud'homme) +- define __ARM_EABI__, __ARMEL__ and __ARM_PCS_VFP (Thomas Preud'homme) +- provide a runtime library for ARM (Thomas Preud'homme) +- vastly improved support for ARM hard float calling convention +(Thomas Preud'homme, Daniel Glöckner) +- tcc can uses libtcc1 on ARM (Thomas Preud'homme) +- use __fixdfdi for all float to integer conversion (grischka) +- simplify startup code for unix platforms (grischka) +- improve ELF generated on ARM (Thomas Preud'homme) +- add support for thumb to ARM relocation (Thomas Preud'homme) +- fix globbing to match MSVC on Windows (Thomas Preud'homme) +- deprecate FPA and OABI support for ARM (Thomas Preud'homme) +- warn about softfloat not being supported on ARM (Thomas Preud'homme) + +Bug fixes: +- many code clean up (Urs Janssen, grischka) +- fixes of other's patches (grischka, Ramsay Jones, Michael Matz) +- fix documentation about __TINYC__ (Urs Janssen) +- improve build of documentation (Urs Janssen) +- improve build instructions (Jov) +- switch from texi2html to makeinfo --html to build tcc-doc.html (James Lyon) +- improve out of tree build (James Lyon) +- improved passing and returning of struct (James Lyon) +- fix CMake build on i386 and x86-64 (James Lyon) +- fix i386 calling convention issue (James Lyon) +- fix error in Windows build of tests (James Lyon) +- fix x86-64 long double passing (James Lyon) +- fix crash with undefined struct (grischka) +- normalize slashes on win32 to always use backslashes (grischka) +- use runtime function for float to int conversion on i386 (grischka) +- improved documentation for include and lib lookup on win32 (grischka) +- detect redefinition of function (Thomas Preud'homme) +- detect the use of array of functions (Thomas Preud'homme) +- detect use of enumerator with wrong enumeration (Thomas Preud'homme) +- detect redefinition of enumerator or enumeration (Thomas Preud'homme) +- set the user-defined library search paths first (Vittorio Giovara) +- detect usage of incomplete types inside struct/union (Amine Najahi) +- various macro bug fixes (Joseph Poirier) +- avoid wrong trigger of assert on x86-64 platform (Thomas Preud'homme) +- fix NaN comparison (Thomas Preud'homme) +- use libtcc for static linking with runtime library (Thomas Preud'homme) +- fix negation of 0.0 and -0.0 values (Thomas Preud'homme) +- fix use of long long as if condition (Thomas Preud'homme) +- disable bound check if libgcc is used (Thomas Preud'homme) +- error out when casting to void (grischka) +- remove circular dependency in Makefile (grischka) +- stop preventing gcc to do strict aliasing (grischka) +- fix Windows build of tcc (grischka) +- build runtime library for arm cross compiler (Thomas Preud'homme) +- fix installation of arm cross-compiler (Thomas Preud'homme) +- add basic test for cross-compiler (Thomas Preud'homme) +- fix failure when generating PE on x86-64 (Archidemon) +- fix floating point unary minus and plus (Michael Matz) +- add more tests for signed zero float (Michael Matz) +- fix precision of double on x86-64 (Vincent Lefevre) +- fix bound checking of argv with -run switch (Kirill Smelkov) +- work around a wine cmd bug when building tcc on Windows (Austin English) +- reenable some bound check tests (grischka) +- boundtest.c lookup honors VPATH (grischka) +- diff compared to CC in test[123]b? are now errors (grischka) +- fix test3 on Windows (grischka) +- prevent gcc from building (non functional) libtcc.a (grischka) +- fix warning related to PE file generation on x86-64 (grischka) +- stop mixing ordinary and implicit rule in Makefile (Iavael) +- fix integer to double conversion on ARM (Thomas Preud'homme) +- fix parameter passing of structure < 4 bytes on ARM (Thomas Preud'homme) +- disable builtin_frame_address test on ARM due to gcc bug (Thomas Preud'homme) +- fix initialization of struct on ARM (Thomas Preud'homme) +- fix parameter passing of (unsigned) long long bitfield (Thomas Preud'homme) +- improve float to integer tests (Thomas Preud'homme) +- fix relocation of Thumb branch to ARM function (Thomas Preud'homme) +- fix char wrong compatibility with [un]signed char (Thomas Preud'homme) +- choose the code to compile based on target in libtcc1 (Thomas Preud'homme) +- fix various clang warnings (Thomas Preud'homme) +- don't hardcode tcc in Makefile for tests (Thomas Preud'homme) +- fix relocation of __bound_init bound checking code (Thomas Preud'homme) +- accept only one basic type for a given variable (Thomas Preud'homme) +- fix error when using va_* with tcc using libgcc (Thomas Preud'homme) +- support GOT32 and PLT32 reloc on the same symbol (Thomas Preud'homme) +- fix memory leak due to symbol attributes (mingodad) +- partially fix bound checking of argv and arge (Thomas Preud'homme) +- fix possible dereference when getting name of symbol (grischka) +- fix va_list type definition on x86-64 (Daniel Glöckner) +- reduce number of scan-build false positive (mingodad) version 0.9.26: diff --git a/Makefile b/Makefile index 4a27364..4f18567 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,7 @@ 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_uClibc) += -DTCC_UCLIBC -NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM -DWITHOUT_LIBTCC +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 @@ -75,18 +75,30 @@ NATIVE_DEFINES += $(NATIVE_DEFINES_yes) ifeq ($(TOP),.) PROGS=tcc$(EXESUF) -I386_CROSS = i386-tcc$(EXESUF) -WIN32_CROSS = i386-win32-tcc$(EXESUF) -WIN64_CROSS = x86_64-win32-tcc$(EXESUF) -WINCE_CROSS = arm-win32-tcc$(EXESUF) -X64_CROSS = x86_64-tcc$(EXESUF) -ARM_FPA_CROSS = arm-fpa-tcc$(EXESUF) -ARM_FPA_LD_CROSS = arm-fpa-ld-tcc$(EXESUF) -ARM_VFP_CROSS = arm-vfp-tcc$(EXESUF) -ARM_EABI_CROSS = arm-eabi-tcc$(EXESUF) +I386_CROSS = i386-linux-gnu-tcc$(EXESUF) +WIN32_CROSS = i386-w64-mingw32-tcc$(EXESUF) +WIN64_CROSS = x86_64-w64-mingw32-tcc$(EXESUF) +WINCE_CROSS = arm-wince-mingw32ce-tcc$(EXESUF) +X64_CROSS = x86_64-linux-gnu-tcc$(EXESUF) +ARM_FPA_CROSS = arm-linux-fpa-tcc$(EXESUF) +ARM_FPA_LD_CROSS = arm-linux-fpa-ld-tcc$(EXESUF) +ARM_VFP_CROSS = arm-linux-gnu-tcc$(EXESUF) +ARM_EABI_CROSS = arm-linux-gnueabi-tcc$(EXESUF) +ARM_EABIHF_CROSS = arm-linux-gnueabihf-tcc$(EXESUF) ARM_CROSS = $(ARM_FPA_CROSS) $(ARM_FPA_LD_CROSS) $(ARM_VFP_CROSS) $(ARM_EABI_CROSS) C67_CROSS = c67-tcc$(EXESUF) +# Legacy symlinks for cross compilers +$(I386_CROSS)_LINK = i386-tcc$(EXESUF) +$(WIN32_CROSS)_LINK = i386-win32-tcc$(EXESUF) +$(WIN64_CROSS)_LINK = x86_64-win32-tcc$(EXESUF) +$(WINCE_CROSS)_LINK = arm-win32-tcc$(EXESUF) +$(X64_CROSS)_LINK = x86_64-tcc$(EXESUF) +$(ARM_FPA_CROSS)_LINK = arm-fpa-tcc$(EXESUF) +$(ARM_FPA_LD_CROSS)_LINK = arm-fpa-ld-tcc$(EXESUF) +$(ARM_VFP_CROSS)_LINK = arm-vfp-tcc$(EXESUF) +$(ARM_EABI_CROSS)_LINK = arm-eabi-tcc$(EXESUF) + CORE_FILES = tcc.c libtcc.c tccpp.c tccgen.c tccelf.c tccasm.c tccrun.c CORE_FILES += tcc.h config.h libtcc.h tcctok.h I386_FILES = $(CORE_FILES) i386-gen.c i386-asm.c i386-asm.h i386-tok.h @@ -122,16 +134,15 @@ LIBTCC1=libtcc1.a else ifeq ($(ARCH),arm) NATIVE_FILES=$(ARM_FILES) PROGS_CROSS=$(I386_CROSS) $(X64_CROSS) $(WIN32_CROSS) $(WIN64_CROSS) $(C67_CROSS) +LIBTCC1=libtcc1.a +LIBTCC1_CROSS=lib/i386-win32/libtcc1.a lib/x86_64-win32/libtcc1.a lib/i386/libtcc1.a endif +PROGS_CROSS_LINK=$(foreach PROG_CROSS,$(PROGS_CROSS),$($(PROG_CROSS)_LINK)) ifeq ($(TARGETOS),Darwin) PROGS+=tiny_libmaker$(EXESUF) endif -ifdef CONFIG_USE_LIBGCC -LIBTCC1= -endif - TCCLIBS = $(LIBTCC1) $(LIBTCC) $(LIBTCC_EXTRA) TCCDOCS = tcc.1 tcc-doc.html tcc-doc.info @@ -149,6 +160,7 @@ tcc$(EXESUF): tcc.o $(LIBTCC) # Cross Tiny C Compilers %-tcc$(EXESUF): tcc.c $(CC) -o $@ $< -DONE_SOURCE $(DEFINES) $(CPPFLAGS) $(CFLAGS) $(LIBS) $(LDFLAGS) + $(if $($@_LINK),ln -sf $@ $($@_LINK)) # profiling version tcc_p$(EXESUF): $(NATIVE_FILES) @@ -168,7 +180,7 @@ $(C67_CROSS): DEFINES = -DTCC_TARGET_C67 $(ARM_FPA_CROSS): DEFINES = -DTCC_TARGET_ARM $(ARM_FPA_LD_CROSS)$(EXESUF): DEFINES = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12 $(ARM_VFP_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_VFP -$(ARM_EABI_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_EABI +$(ARM_EABI_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_EABI -DTCC_ARM_VFP $(I386_CROSS): $(I386_FILES) $(X64_CROSS): $(X86_64_FILES) @@ -229,18 +241,21 @@ else INSTALLBIN=$(INSTALL) endif +install-strip: install + strip $(foreach PROG,$(PROGS),"$(bindir)"/$(PROG)) + ifndef CONFIG_WIN32 install: $(PROGS) $(TCCLIBS) $(TCCDOCS) mkdir -p "$(bindir)" ifeq ($(CC),tcc) - $(INSTALL) -m755 $(PROGS) "$(bindir)" + $(INSTALL) -m755 $(PROGS) $(PROGS_CROSS_LINK) "$(bindir)" else $(INSTALLBIN) -m755 $(PROGS) "$(bindir)" endif mkdir -p "$(mandir)/man1" - -$(INSTALL) tcc.1 "$(mandir)/man1" + -$(INSTALL) -m644 tcc.1 "$(mandir)/man1" mkdir -p "$(infodir)" - -$(INSTALL) tcc-doc.info "$(infodir)" + -$(INSTALL) -m644 tcc-doc.info "$(infodir)" mkdir -p "$(tccdir)" mkdir -p "$(tccdir)/include" ifneq ($(LIBTCC1),) @@ -248,7 +263,7 @@ ifneq ($(LIBTCC1),) endif $(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) $(top_srcdir)/tcclib.h "$(tccdir)/include" mkdir -p "$(libdir)" - $(INSTALL) -m755 $(LIBTCC) "$(libdir)" + $(INSTALL) -m644 $(LIBTCC) "$(libdir)" ifdef DISABLE_STATIC ln -sf "$(ln_libdir)/libtcc.so.1.0" "$(libdir)/libtcc.so.1" ln -sf "$(ln_libdir)/libtcc.so.1.0" "$(libdir)/libtcc.so" @@ -260,7 +275,7 @@ endif ifdef CONFIG_CROSS mkdir -p "$(tccdir)/win32/lib/32" mkdir -p "$(tccdir)/win32/lib/64" -ifeq ($(ARCH),x86-64) +ifneq ($(ARCH),i386) mkdir -p "$(tccdir)/i386" $(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386" cp -r "$(tccdir)/include" "$(tccdir)/i386" @@ -269,7 +284,7 @@ endif $(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/win32/lib/32" $(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/win32/lib/64" cp -r $(top_srcdir)/win32/include/. "$(tccdir)/win32/include" - cp -r $(top_srcdir)/include/. "$(tccdir)/win32/include" + cp -r "$(tccdir)/include" "$(tccdir)/win32" endif uninstall: @@ -282,7 +297,7 @@ uninstall: rm -fv "$(libdir)/libtcc.so*" rm -rf "$(tccdir)/win32" -rmdir $(tccdir)/include -ifeq ($(ARCH),x86-64) +ifneq ($(ARCH),i386) rm -rf "$(tccdir)/i386" endif else @@ -328,7 +343,7 @@ tcc-doc.info: tcc-doc.texi export LIBTCC1 %est: - $(MAKE) -C tests $@ + $(MAKE) -C tests $@ 'PROGS_CROSS=$(PROGS_CROSS)' clean: rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.exe libtcc_test$(EXESUF) @@ -358,9 +373,6 @@ tar: tcc-doc.html rm -rf $(TCC-VERSION) git reset -Makefile: $(top_srcdir)/Makefile - cp $< $@ - .PHONY: all clean tar distclean install uninstall FORCE endif # ifeq ($(TOP),.) diff --git a/RELICENSING b/RELICENSING index 1cffbdb..1e2d378 100644 --- a/RELICENSING +++ b/RELICENSING @@ -27,9 +27,10 @@ Author (name) I agree (YES/NO) Files/Features (optional) ------------------------------------------------------------------------------ - Daniel Glöckner ? arm-gen.c + Daniel Glöckner NO arm-gen.c + Daniel Glöckner YES not arm-gen.c Fabrice Bellard YES original author - Frédéric Feret ? x86 64/16 bit asm + Frédéric Féret YES x86 64/16 bit asm grischka YES tccpe.c Shinichiro Hamaji YES x86_64-gen.c Thomas Preud'homme YES arm-gen.c diff --git a/arm-gen.c b/arm-gen.c index eccfdd8..680a490 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -1,6 +1,6 @@ /* * ARMv4 code generator for TCC - * + * * Copyright (c) 2003 Daniel Glöckner * Copyright (c) 2012 Thomas Preud'homme * @@ -23,10 +23,8 @@ #ifdef TARGET_DEFS_ONLY -#ifdef TCC_ARM_EABI -#ifndef TCC_ARM_VFP /* Avoid useless warning */ -#define TCC_ARM_VFP -#endif +#if defined(TCC_ARM_EABI) && !defined(TCC_ARM_VFP) +#error "Currently TinyCC only supports float computation with VFP instructions" #endif /* number of available registers */ @@ -46,7 +44,7 @@ #define RC_INT 0x0001 /* generic integer register */ #define RC_FLOAT 0x0002 /* generic float register */ #define RC_R0 0x0004 -#define RC_R1 0x0008 +#define RC_R1 0x0008 #define RC_R2 0x0010 #define RC_R3 0x0020 #define RC_R12 0x0040 @@ -143,11 +141,18 @@ enum { #define ELF_START_ADDR 0x00008000 #define ELF_PAGE_SIZE 0x1000 +enum float_abi { + ARM_SOFTFP_FLOAT, + ARM_HARD_FLOAT, +}; + /******************************************************/ #else /* ! TARGET_DEFS_ONLY */ /******************************************************/ #include "tcc.h" +enum float_abi float_abi; + ST_DATA const int reg_classes[NB_REGS] = { /* r0 */ RC_INT | RC_R0, /* r1 */ RC_INT | RC_R1, @@ -171,7 +176,7 @@ static int leaffunc; #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) static CType float_type, double_type, func_float_type, func_double_type; -ST_FUNC void arm_init_types(void) +ST_FUNC void arm_init(struct TCCState *s) { float_type.t = VT_FLOAT; double_type.t = VT_DOUBLE; @@ -179,12 +184,27 @@ ST_FUNC void arm_init_types(void) func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD); func_double_type.t = VT_FUNC; func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD); + + float_abi = s->float_abi; +#ifndef TCC_ARM_HARDFLOAT + tcc_warning("soft float ABI currently not supported: default to softfp"); +#endif } #else #define func_float_type func_old_type #define func_double_type func_old_type #define func_ldouble_type func_old_type -ST_FUNC void arm_init_types(void) {} +ST_FUNC void arm_init(struct TCCState *s) +{ +#if !defined (TCC_ARM_VFP) + tcc_warning("Support for FPA is deprecated and will be removed in next" + " release"); +#endif +#if !defined (TCC_ARM_EABI) + tcc_warning("Support for OABI is deprecated and will be removed in next" + " release"); +#endif +} #endif static int two2mask(int a,int b) { @@ -197,6 +217,16 @@ static int regmask(int r) { /******************************************************/ +#ifdef TCC_ARM_EABI +char *default_elfinterp(struct TCCState *s) +{ + if (s->float_abi == ARM_HARD_FLOAT) + return "/lib/ld-linux-armhf.so.3"; + else + return "/lib/ld-linux.so.3"; +} +#endif + void o(uint32_t i) { /* this is a good place to start adding big-endian support*/ @@ -211,7 +241,7 @@ void o(uint32_t i) cur_text_section->data[ind++] = i&255; i>>=8; cur_text_section->data[ind++] = i&255; - i>>=8; + i>>=8; cur_text_section->data[ind++] = i&255; i>>=8; cur_text_section->data[ind++] = i; @@ -506,7 +536,7 @@ void load(int r, SValue *sv) sign=1; fc=-fc; } - + v = fr & VT_VALMASK; if (fr & VT_LVAL) { uint32_t base = 0xB; // fp @@ -645,8 +675,8 @@ void store(int r, SValue *sv) sign=1; fc=-fc; } - - v = fr & VT_VALMASK; + + v = fr & VT_VALMASK; if (fr & VT_LVAL || fr == VT_LOCAL) { uint32_t base = 0xb; if(v < VT_CONST) { @@ -660,16 +690,16 @@ void store(int r, SValue *sv) v1.sym=sv->sym; load(base=14, &v1); fc=sign=0; - v=VT_LOCAL; + v=VT_LOCAL; } if(v == VT_LOCAL) { if(is_float(ft)) { calcaddr(&base,&fc,&sign,1020,2); #ifdef TCC_ARM_VFP op=0xED000A00; /* fsts */ - if(!sign) - op|=0x800000; - if ((ft & VT_BTYPE) != VT_FLOAT) + if(!sign) + op|=0x800000; + if ((ft & VT_BTYPE) != VT_FLOAT) op|=0x100; /* fsts -> fstd */ o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16)); #else @@ -746,14 +776,18 @@ static void gcall_or_jmp(int is_jmp) } } -#ifdef TCC_ARM_HARDFLOAT -static int is_float_hgen_aggr(CType *type) +/* Return whether a structure is an homogeneous float aggregate or not. + The answer is true if all the elements of the structure are of the same + primitive float type and there is less than 4 elements. + + type: the type corresponding to the structure to be tested */ +static int is_hgen_float_aggr(CType *type) { if ((type->t & VT_BTYPE) == VT_STRUCT) { struct Sym *ref; int btype, nb_fields = 0; - ref = type->ref; + ref = type->ref->next; btype = ref->type.t & VT_BTYPE; if (btype == VT_FLOAT || btype == VT_DOUBLE) { for(; ref && btype == (ref->type.t & VT_BTYPE); ref = ref->next, nb_fields++); @@ -764,28 +798,36 @@ static int is_float_hgen_aggr(CType *type) } struct avail_regs { - /* worst case: f(float, double, 3 float struct, double, 3 float struct, double) */ - signed char avail[3]; - int first_hole; - int last_hole; - int first_free_reg; + signed char avail[3]; /* 3 holes max with only float and double alignments */ + int first_hole; /* first available hole */ + int last_hole; /* last available hole (none if equal to first_hole) */ + int first_free_reg; /* next free register in the sequence, hole excluded */ }; #define AVAIL_REGS_INITIALIZER (struct avail_regs) { { 0, 0, 0}, 0, 0, 0 } -/* Assign a register for a CPRC param with correct size and alignment - * size and align are in bytes, as returned by type_size */ -int assign_fpreg(struct avail_regs *avregs, int align, int size) +/* Find suitable registers for a VFP Co-Processor Register Candidate (VFP CPRC + param) according to the rules described in the procedure call standard for + the ARM architecture (AAPCS). If found, the registers are assigned to this + VFP CPRC parameter. Registers are allocated in sequence unless a hole exists + and the parameter is a single float. + + avregs: opaque structure to keep track of available VFP co-processor regs + align: alignment contraints for the param, as returned by type_size() + size: size of the parameter, as returned by type_size() */ +int assign_vfpreg(struct avail_regs *avregs, int align, int size) { int first_reg = 0; if (avregs->first_free_reg == -1) return -1; - if (align >> 3) { // alignment needed (base type: double) + if (align >> 3) { /* double alignment */ first_reg = avregs->first_free_reg; + /* alignment contraint not respected so use next reg and record hole */ if (first_reg & 1) avregs->avail[avregs->last_hole++] = first_reg++; - } else { + } else { /* no special alignment (float or array of float) */ + /* if single float and a hole is available, assign the param to it */ if (size == 4 && avregs->first_hole != avregs->last_hole) return avregs->avail[avregs->first_hole++]; else @@ -798,394 +840,443 @@ int assign_fpreg(struct avail_regs *avregs, int align, int size) avregs->first_free_reg = -1; return -1; } -#endif -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { -#if TCC_ARM_EABI +/* Returns whether all params need to be passed in core registers or not. + This is the case for function part of the runtime ABI. */ +int floats_in_core_regs(SValue *sval) +{ + if (!sval->sym) + return 0; + + switch (sval->sym->v) { + case TOK___floatundisf: + case TOK___floatundidf: + case TOK___fixunssfdi: + case TOK___fixunsdfdi: +#ifndef TCC_ARM_VFP + case TOK___fixunsxfdi: +#endif + case TOK___floatdisf: + case TOK___floatdidf: + case TOK___fixsfdi: + case TOK___fixdfdi: + return 1; + + default: + return 0; + } +} + +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { +#ifdef TCC_ARM_EABI int size, align; size = type_size(vt, &align); - if (size > 4) { - return 1; - } else { + if (float_abi == ARM_HARD_FLOAT && !variadic && + (is_float(vt->t) || is_hgen_float_aggr(vt))) { + *ret_align = 8; + ret->ref = NULL; + ret->t = VT_DOUBLE; + return (size + 7) >> 3; + } else if (size <= 4) { *ret_align = 4; ret->ref = NULL; ret->t = VT_INT; + return 1; + } else return 0; - } #else - return 1; + return 0; #endif } +/* Parameters are classified according to how they are copied to their final + destination for the function call. Because the copying is performed class + after class according to the order in the union below, it is important that + some constraints about the order of the members of this union are respected: + - CORE_STRUCT_CLASS must come after STACK_CLASS; + - CORE_CLASS must come after STACK_CLASS, CORE_STRUCT_CLASS and + VFP_STRUCT_CLASS; + - VFP_STRUCT_CLASS must come after VFP_CLASS. + See the comment for the main loop in copy_params() for the reason. */ +enum reg_class { + STACK_CLASS = 0, + CORE_STRUCT_CLASS, + VFP_CLASS, + VFP_STRUCT_CLASS, + CORE_CLASS, + NB_CLASSES +}; + +struct param_plan { + int start; /* first reg or addr used depending on the class */ + int end; /* last reg used or next free addr depending on the class */ + SValue *sval; /* pointer to SValue on the value stack */ + struct param_plan *prev; /* previous element in this class */ +}; + +struct plan { + struct param_plan *pplans; /* array of all the param plans */ + struct param_plan *clsplans[NB_CLASSES]; /* per class lists of param plans */ +}; + +#define add_param_plan(plan,pplan,class) \ + do { \ + pplan.prev = plan->clsplans[class]; \ + plan->pplans[plan ## _nb] = pplan; \ + plan->clsplans[class] = &plan->pplans[plan ## _nb++]; \ + } while(0) + +/* Assign parameters to registers and stack with alignment according to the + rules in the procedure call standard for the ARM architecture (AAPCS). + The overall assignment is recorded in an array of per parameter structures + called parameter plans. The parameter plans are also further organized in a + number of linked lists, one per class of parameter (see the comment for the + definition of union reg_class). + + nb_args: number of parameters of the function for which a call is generated + float_abi: float ABI in use for this function call + plan: the structure where the overall assignment is recorded + todo: a bitmap that record which core registers hold a parameter + + Returns the amount of stack space needed for parameter passing + + Note: this function allocated an array in plan->pplans with tcc_malloc. It + is the responsibility of the caller to free this array once used (ie not + before copy_params). */ +static int assign_regs(int nb_args, int float_abi, struct plan *plan, int *todo) +{ + int i, size, align; + int ncrn /* next core register number */, nsaa /* next stacked argument address*/; + int plan_nb = 0; + struct param_plan pplan; + struct avail_regs avregs = AVAIL_REGS_INITIALIZER; + + ncrn = nsaa = 0; + *todo = 0; + plan->pplans = tcc_malloc(nb_args * sizeof(*plan->pplans)); + memset(plan->clsplans, 0, sizeof(plan->clsplans)); + for(i = nb_args; i-- ;) { + int j, start_vfpreg = 0; + CType type = vtop[-i].type; + type.t &= ~VT_ARRAY; + size = type_size(&type, &align); + size = (size + 3) & ~3; + align = (align + 3) & ~3; + switch(vtop[-i].type.t & VT_BTYPE) { + case VT_STRUCT: + case VT_FLOAT: + case VT_DOUBLE: + case VT_LDOUBLE: + if (float_abi == ARM_HARD_FLOAT) { + int is_hfa = 0; /* Homogeneous float aggregate */ + + if (is_float(vtop[-i].type.t) + || (is_hfa = is_hgen_float_aggr(&vtop[-i].type))) { + int end_vfpreg; + + start_vfpreg = assign_vfpreg(&avregs, align, size); + end_vfpreg = start_vfpreg + ((size - 1) >> 2); + if (start_vfpreg >= 0) { + pplan = (struct param_plan) {start_vfpreg, end_vfpreg, &vtop[-i]}; + if (is_hfa) + add_param_plan(plan, pplan, VFP_STRUCT_CLASS); + else + add_param_plan(plan, pplan, VFP_CLASS); + continue; + } else + break; + } + } + ncrn = (ncrn + (align-1)/4) & ~((align/4) - 1); + if (ncrn + size/4 <= 4 || (ncrn < 4 && start_vfpreg != -1)) { + /* The parameter is allocated both in core register and on stack. As + * such, it can be of either class: it would either be the last of + * CORE_STRUCT_CLASS or the first of STACK_CLASS. */ + for (j = ncrn; j < 4 && j < ncrn + size / 4; j++) + *todo|=(1< 4) + nsaa = (ncrn - 4) * 4; + } else { + ncrn = 4; + break; + } + continue; + default: + if (ncrn < 4) { + int is_long = (vtop[-i].type.t & VT_BTYPE) == VT_LLONG; + + if (is_long) { + ncrn = (ncrn + 1) & -2; + if (ncrn == 4) + break; + } + pplan = (struct param_plan) {ncrn, ncrn, &vtop[-i]}; + ncrn++; + if (is_long) + pplan.end = ncrn++; + add_param_plan(plan, pplan, CORE_CLASS); + continue; + } + } + nsaa = (nsaa + (align - 1)) & ~(align - 1); + pplan = (struct param_plan) {nsaa, nsaa + size, &vtop[-i]}; + add_param_plan(plan, pplan, STACK_CLASS); + nsaa += size; /* size already rounded up before */ + } + return nsaa; +} + +#undef add_param_plan + +/* Copy parameters to their final destination (core reg, VFP reg or stack) for + function call. + + nb_args: number of parameters the function take + plan: the overall assignment plan for parameters + todo: a bitmap indicating what core reg will hold a parameter + + Returns the number of SValue added by this function on the value stack */ +static int copy_params(int nb_args, struct plan *plan, int todo) +{ + int size, align, r, i, nb_extra_sval = 0; + struct param_plan *pplan; + + /* Several constraints require parameters to be copied in a specific order: + - structures are copied to the stack before being loaded in a reg; + - floats loaded to an odd numbered VFP reg are first copied to the + preceding even numbered VFP reg and then moved to the next VFP reg. + + It is thus important that: + - structures assigned to core regs must be copied after parameters + assigned to the stack but before structures assigned to VFP regs because + a structure can lie partly in core registers and partly on the stack; + - parameters assigned to the stack and all structures be copied before + parameters assigned to a core reg since copying a parameter to the stack + require using a core reg; + - parameters assigned to VFP regs be copied before structures assigned to + VFP regs as the copy might use an even numbered VFP reg that already + holds part of a structure. */ + for(i = 0; i < NB_CLASSES; i++) { + for(pplan = plan->clsplans[i]; pplan; pplan = pplan->prev) { + vpushv(pplan->sval); + pplan->sval->r = pplan->sval->r2 = VT_CONST; /* disable entry */ + switch(i) { + case STACK_CLASS: + case CORE_STRUCT_CLASS: + case VFP_STRUCT_CLASS: + if ((pplan->sval->type.t & VT_BTYPE) == VT_STRUCT) { + int padding = 0; + size = type_size(&pplan->sval->type, &align); + /* align to stack align size */ + size = (size + 3) & ~3; + if (i == STACK_CLASS && pplan->prev) + padding = pplan->start - pplan->prev->end; + size += padding; /* Add padding if any */ + /* allocate the necessary size on stack */ + gadd_sp(-size); + /* generate structure store */ + r = get_reg(RC_INT); + o(0xE28D0000|(intr(r)<<12)|padding); /* add r, sp, padding */ + vset(&vtop->type, r | VT_LVAL, 0); + vswap(); + vstore(); /* memcpy to current sp + potential padding */ + + /* Homogeneous float aggregate are loaded to VFP registers + immediately since there is no way of loading data in multiple + non consecutive VFP registers as what is done for other + structures (see the use of todo). */ + if (i == VFP_STRUCT_CLASS) { + int first = pplan->start, nb = pplan->end - first + 1; + /* vpop.32 {pplan->start, ..., pplan->end} */ + o(0xECBD0A00|(first&1)<<22|(first>>1)<<12|nb); + /* No need to write the register used to a SValue since VFP regs + cannot be used for gcall_or_jmp */ + } + } else { + if (is_float(pplan->sval->type.t)) { +#ifdef TCC_ARM_VFP + r = vfpr(gv(RC_FLOAT)) << 12; + if ((pplan->sval->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else { + size = 8; + r |= 0x101; /* vpush.32 -> vpush.64 */ + } + o(0xED2D0A01 + r); /* vpush */ +#else + r = fpr(gv(RC_FLOAT)) << 12; + if ((pplan->sval->type.t & VT_BTYPE) == VT_FLOAT) + size = 4; + else if ((pplan->sval->type.t & VT_BTYPE) == VT_DOUBLE) + size = 8; + else + size = LDOUBLE_SIZE; + + if (size == 12) + r |= 0x400000; + else if(size == 8) + r|=0x8000; + + o(0xED2D0100|r|(size>>2)); /* some kind of vpush for FPA */ +#endif + } else { + /* simple type (currently always same size) */ + /* XXX: implicit cast ? */ + size=4; + if ((pplan->sval->type.t & VT_BTYPE) == VT_LLONG) { + lexpand_nr(); + size = 8; + r = gv(RC_INT); + o(0xE52D0004|(intr(r)<<12)); /* push r */ + vtop--; + } + r = gv(RC_INT); + o(0xE52D0004|(intr(r)<<12)); /* push r */ + } + if (i == STACK_CLASS && pplan->prev) + gadd_sp(pplan->prev->end - pplan->start); /* Add padding if any */ + } + break; + + case VFP_CLASS: + gv(regmask(TREG_F0 + (pplan->start >> 1))); + if (pplan->start & 1) { /* Must be in upper part of double register */ + o(0xEEF00A40|((pplan->start>>1)<<12)|(pplan->start>>1)); /* vmov.f32 s(n+1), sn */ + vtop->r = VT_CONST; /* avoid being saved on stack by gv for next float */ + } + break; + + case CORE_CLASS: + if ((pplan->sval->type.t & VT_BTYPE) == VT_LLONG) { + lexpand_nr(); + gv(regmask(pplan->end)); + pplan->sval->r2 = vtop->r; + vtop--; + } + gv(regmask(pplan->start)); + /* Mark register as used so that gcall_or_jmp use another one + (regs >=4 are free as never used to pass parameters) */ + pplan->sval->r = vtop->r; + break; + } + vtop--; + } + } + + /* Manually free remaining registers since next parameters are loaded + * manually, without the help of gv(int). */ + save_regs(nb_args); + + if(todo) { + o(0xE8BD0000|todo); /* pop {todo} */ + for(pplan = plan->clsplans[CORE_STRUCT_CLASS]; pplan; pplan = pplan->prev) { + int r; + pplan->sval->r = pplan->start; + /* An SValue can only pin 2 registers at best (r and r2) but a structure + can occupy more than 2 registers. Thus, we need to push on the value + stack some fake parameter to have on SValue for each registers used + by a structure (r2 is not used). */ + for (r = pplan->start + 1; r <= pplan->end; r++) { + if (todo & (1 << r)) { + nb_extra_sval++; + vpushi(0); + vtop->r = r; + } + } + } + } + return nb_extra_sval; +} + /* Generate function call. The function address is pushed first, then all the parameters in call order. This functions pops all the parameters and the function address. */ void gfunc_call(int nb_args) { - int size, align, r, args_size, i, ncrn, ncprn, argno, vfp_argno; - signed char plan[4][2]={{-1,-1},{-1,-1},{-1,-1},{-1,-1}}; - SValue *before_stack = NULL; /* SValue before first on stack argument */ - SValue *before_vfpreg_hfa = NULL; /* SValue before first in VFP reg hfa argument */ -#ifdef TCC_ARM_HARDFLOAT - struct avail_regs avregs = AVAIL_REGS_INITIALIZER; - signed char vfp_plan[16]; - int plan2[4+16]; - int variadic; -#else - int plan2[4]={0,0,0,0}; -#endif - int vfp_todo=0; - int todo=0, keep; + int r, args_size; + int variadic, def_float_abi = float_abi; + int todo; + struct plan plan; -#ifdef TCC_ARM_HARDFLOAT - memset(vfp_plan, -1, sizeof(vfp_plan)); - memset(plan2, 0, sizeof(plan2)); - variadic = (vtop[-nb_args].type.ref->c == FUNC_ELLIPSIS); +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT) { + variadic = (vtop[-nb_args].type.ref->c == FUNC_ELLIPSIS); + if (variadic || floats_in_core_regs(&vtop[-nb_args])) + float_abi = ARM_SOFTFP_FLOAT; + } #endif + /* cannot let cpu flags if other instruction are generated. Also avoid leaving + VT_JMP anywhere except on the top of the stack because it would complicate + the code generator. */ r = vtop->r & VT_VALMASK; if (r == VT_CMP || (r & ~1) == VT_JMP) gv(RC_INT); -#ifdef TCC_ARM_EABI - if((vtop[-nb_args].type.ref->type.t & VT_BTYPE) == VT_STRUCT - && type_size(&vtop[-nb_args].type.ref->type, &align) <= 4) { - SValue tmp; - tmp=vtop[-nb_args]; - vtop[-nb_args]=vtop[-nb_args+1]; - vtop[-nb_args+1]=tmp; - --nb_args; - } - - vpushi(0), nb_args++; - vtop->type.t = VT_LLONG; -#endif - ncrn = ncprn = argno = vfp_argno = args_size = 0; - /* Assign argument to registers and stack with alignment. - If, considering alignment constraints, enough registers of the correct type - (core or VFP) are free for the current argument, assign them to it, else - allocate on stack with correct alignment. Whenever a structure is allocated - in registers or on stack, it is always put on the stack at this stage. The - stack is divided in 3 zones. The zone are, from low addresses to high - addresses: structures to be loaded in core registers, structures to be - loaded in VFP registers, argument allocated to stack. SValue's representing - structures in the first zone are moved just after the SValue pointed by - before_vfpreg_hfa. SValue's representing structures in the second zone are - moved just after the SValue pointer by before_stack. */ - for(i = nb_args; i-- ;) { - int j, assigned_vfpreg = 0; - size = type_size(&vtop[-i].type, &align); - switch(vtop[-i].type.t & VT_BTYPE) { - case VT_STRUCT: - case VT_FLOAT: - case VT_DOUBLE: - case VT_LDOUBLE: -#ifdef TCC_ARM_HARDFLOAT - if (!variadic) { - int hfa = 0; /* Homogeneous float aggregate */ - if (is_float(vtop[-i].type.t) - || (hfa = is_float_hgen_aggr(&vtop[-i].type))) { - int end_reg; + args_size = assign_regs(nb_args, float_abi, &plan, &todo); - assigned_vfpreg = assign_fpreg(&avregs, align, size); - end_reg = assigned_vfpreg + (size - 1) / 4; - if (assigned_vfpreg >= 0) { - vfp_plan[vfp_argno++]=TREG_F0 + assigned_vfpreg/2; - if (hfa) { - /* before_stack can only have been set because all core registers - are assigned, so no need to care about before_vfpreg_hfa if - before_stack is set */ - if (before_stack) { - vrote(&vtop[-i], &vtop[-i] - before_stack); - before_stack++; - } else if (!before_vfpreg_hfa) - before_vfpreg_hfa = &vtop[-i-1]; - for (j = assigned_vfpreg; j <= end_reg; j++) - vfp_todo|=(1< 4) { - args_size = (ncrn - 4) * 4; - if (!before_stack) - before_stack = &vtop[-i-1]; - } - } - else { - ncrn = 4; - /* No need to set before_vfpreg_hfa if not set since there will no - longer be any structure assigned to core registers */ - if (!before_stack) - before_stack = &vtop[-i-1]; - break; - } - continue; - default: #ifdef TCC_ARM_EABI - if (!i) { - break; - } + if (args_size & 7) { /* Stack must be 8 byte aligned at fct call for EABI */ + args_size = (args_size + 7) & ~7; + o(0xE24DD004); /* sub sp, sp, #4 */ + } #endif - if (ncrn < 4) { - int is_long = (vtop[-i].type.t & VT_BTYPE) == VT_LLONG; - if (is_long) { - ncrn = (ncrn + 1) & -2; - if (ncrn == 4) { - argno++; - break; - } - } - plan[argno++][0]=ncrn++; - if (is_long) { - plan[argno-1][1]=ncrn++; - } - continue; - } - argno++; - } -#ifdef TCC_ARM_EABI - if(args_size & (align-1)) { - vpushi(0); - vtop->type.t = VT_VOID; /* padding */ - vrott(i+2); - args_size += 4; - nb_args++; - argno++; - } -#endif - args_size += (size + 3) & -4; - } -#ifdef TCC_ARM_EABI - vtop--, nb_args--; -#endif - args_size = keep = 0; - for(i = 0;i < nb_args; i++) { - vrotb(keep+1); - if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { - size = type_size(&vtop->type, &align); - /* align to stack align size */ - size = (size + 3) & -4; - /* allocate the necessary size on stack */ - gadd_sp(-size); - /* generate structure store */ - r = get_reg(RC_INT); - o(0xE1A0000D|(intr(r)<<12)); - vset(&vtop->type, r | VT_LVAL, 0); - vswap(); - vstore(); - vtop--; - args_size += size; - } else if (is_float(vtop->type.t)) { -#ifdef TCC_ARM_HARDFLOAT - if (!variadic && --vfp_argno<16 && vfp_plan[vfp_argno]!=-1) { - plan2[keep++]=vfp_plan[vfp_argno]; - continue; - } -#endif -#ifdef TCC_ARM_VFP - r=vfpr(gv(RC_FLOAT))<<12; - size=4; - if ((vtop->type.t & VT_BTYPE) != VT_FLOAT) - { - size=8; - r|=0x101; /* fstms -> fstmd */ - } - o(0xED2D0A01+r); -#else - r=fpr(gv(RC_FLOAT))<<12; - if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) - size = 4; - else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) - size = 8; - else - size = LDOUBLE_SIZE; - - if (size == 12) - r|=0x400000; - else if(size == 8) - r|=0x8000; + nb_args += copy_params(nb_args, &plan, todo); + tcc_free(plan.pplans); - o(0xED2D0100|r|(size>>2)); -#endif - vtop--; - args_size += size; - } else { - int s; - /* simple type (currently always same size) */ - /* XXX: implicit cast ? */ - size=4; - if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { - lexpand_nr(); - s=-1; - if(--argno<4 && plan[argno][1]!=-1) - s=plan[argno][1]; - argno++; - size = 8; - if(s==-1) { - r = gv(RC_INT); - o(0xE52D0004|(intr(r)<<12)); /* str r,[sp,#-4]! */ - vtop--; - } else { - size=0; - plan2[keep]=s; - keep++; - vswap(); - } - } - s=-1; - if(--argno<4 && plan[argno][0]!=-1) - s=plan[argno][0]; -#ifdef TCC_ARM_EABI - if(vtop->type.t == VT_VOID) { - if(s == -1) - o(0xE24DD004); /* sub sp,sp,#4 */ - vtop--; - } else -#endif - if(s == -1) { - r = gv(RC_INT); - o(0xE52D0004|(intr(r)<<12)); /* str r,[sp,#-4]! */ - vtop--; - } else { - size=0; - plan2[keep]=s; - keep++; - } - args_size += size; - } - } - for(i = 0; i < keep; i++) { - vrotb(keep); - gv(regmask(plan2[i])); -#ifdef TCC_ARM_HARDFLOAT - /* arg is in s(2d+1): plan2[i] alignment occured (ex f,d,f) */ - if (i < keep - 1 && is_float(vtop->type.t) && (plan2[i] <= plan2[i + 1])) { - o(0xEEF00A40|(vfpr(plan2[i])<<12)|vfpr(plan2[i])); - } -#endif - } -save_regs(keep); /* save used temporary registers */ - keep++; - if(ncrn) { - int nb_regs=0; - if (ncrn>4) - ncrn=4; - todo&=((1<r=i; - keep++; - nb_regs++; - } - } - args_size-=nb_regs*4; - } - if(vfp_todo) { - int nb_fregs=0; - - for(i=0;i<16;i++) - if(vfp_todo&(1<>1)<<12|nb_fregs); - vpushi(0); - /* There might be 2 floats in a double VFP reg but that doesn't seem - to matter */ - if (!(i%2)) - vtop->r=TREG_F0+i/2; - keep++; - nb_fregs++; - } - if (nb_fregs) { - gadd_sp(nb_fregs*4); - args_size-=nb_fregs*4; - } - } - vrotb(keep); + /* Move fct SValue on top as required by gcall_or_jmp */ + vrotb(nb_args + 1); gcall_or_jmp(0); if (args_size) - gadd_sp(args_size); -#ifdef TCC_ARM_EABI - if((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT - && type_size(&vtop->type.ref->type, &align) <= 4) - { - store(REG_IRET,vtop-keep); - ++keep; - } -#ifdef TCC_ARM_VFP -#ifdef TCC_ARM_HARDFLOAT - else if(variadic && is_float(vtop->type.ref->type.t)) { -#else - else if(is_float(vtop->type.ref->type.t)) { -#endif + gadd_sp(args_size); /* pop all parameters passed on the stack */ +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) + if(float_abi == ARM_SOFTFP_FLOAT && is_float(vtop->type.ref->type.t)) { if((vtop->type.ref->type.t & VT_BTYPE) == VT_FLOAT) { - o(0xEE000A10); /* fmsr s0,r0 */ + o(0xEE000A10); /*vmov s0, r0 */ } else { - o(0xEE000B10); /* fmdlr d0,r0 */ - o(0xEE201B10); /* fmdhr d0,r1 */ + o(0xEE000B10); /* vmov.32 d0[0], r0 */ + o(0xEE201B10); /* vmov.32 d0[1], r1 */ } } #endif -#endif - vtop-=keep; - leaffunc = 0; + vtop -= nb_args + 1; /* Pop all params and fct address from value stack */ + leaffunc = 0; /* we are calling a function, so we aren't in a leaf function */ + float_abi = def_float_abi; } /* generate function prolog of type 't' */ void gfunc_prolog(CType *func_type) { Sym *sym,*sym2; - int n,nf,size,align, variadic, struct_ret = 0; -#ifdef TCC_ARM_HARDFLOAT + int n, nf, size, align, struct_ret = 0; + int addr, pn, sn; /* pn=core, sn=stack */ struct avail_regs avregs = AVAIL_REGS_INITIALIZER; -#endif + CType ret_type; sym = func_type->ref; func_vt = sym->type; + func_var = (func_type->ref->c == FUNC_ELLIPSIS); n = nf = 0; - variadic = (func_type->ref->c == FUNC_ELLIPSIS); - if((func_vt.t & VT_BTYPE) == VT_STRUCT - && type_size(&func_vt,&align) > 4) + if ((func_vt.t & VT_BTYPE) == VT_STRUCT && + !gfunc_sret(&func_vt, func_var, &ret_type, &align)) { n++; struct_ret = 1; func_vc = 12; /* Offset from fp of the place to store the result */ } - for(sym2=sym->next;sym2 && (n<4 || nf<16);sym2=sym2->next) { + for(sym2 = sym->next; sym2 && (n < 4 || nf < 16); sym2 = sym2->next) { size = type_size(&sym2->type, &align); -#ifdef TCC_ARM_HARDFLOAT - if (!variadic && (is_float(sym2->type.t) - || is_float_hgen_aggr(&sym2->type))) { - int tmpnf = assign_fpreg(&avregs, align, size) + 1; +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT && !func_var && + (is_float(sym2->type.t) || is_hgen_float_aggr(&sym2->type))) { + int tmpnf = assign_vfpreg(&avregs, align, size); + tmpnf += (size + 3) / 4; nf = (tmpnf > nf) ? tmpnf : nf; } else #endif @@ -1193,9 +1284,9 @@ void gfunc_prolog(CType *func_type) n += (size + 3) / 4; } o(0xE1A0C00D); /* mov ip,sp */ - if(variadic) + if (func_var) n=4; - if(n) { + if (n) { if(n>4) n=4; #ifdef TCC_ARM_EABI @@ -1212,50 +1303,49 @@ void gfunc_prolog(CType *func_type) o(0xE92D5800); /* save fp, ip, lr */ o(0xE1A0B00D); /* mov fp, sp */ func_sub_sp_offset = ind; - o(0xE1A00000); /* nop, leave space for stack adjustment in epilogue */ - { - int addr, pn = struct_ret, sn = 0; /* pn=core, sn=stack */ + o(0xE1A00000); /* nop, leave space for stack adjustment in epilog */ -#ifdef TCC_ARM_HARDFLOAT +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT) { func_vc += nf * 4; avregs = AVAIL_REGS_INITIALIZER; + } #endif - while ((sym = sym->next)) { - CType *type; - type = &sym->type; - size = type_size(type, &align); - size = (size + 3) >> 2; - align = (align + 3) & ~3; -#ifdef TCC_ARM_HARDFLOAT - if (!variadic && (is_float(sym->type.t) - || is_float_hgen_aggr(&sym->type))) { - int fpn = assign_fpreg(&avregs, align, size << 2); - if (fpn >= 0) { - addr = fpn * 4; - } else - goto from_stack; - } else + pn = struct_ret, sn = 0; + while ((sym = sym->next)) { + CType *type; + type = &sym->type; + size = type_size(type, &align); + size = (size + 3) >> 2; + align = (align + 3) & ~3; +#ifdef TCC_ARM_EABI + if (float_abi == ARM_HARD_FLOAT && !func_var && (is_float(sym->type.t) + || is_hgen_float_aggr(&sym->type))) { + int fpn = assign_vfpreg(&avregs, align, size << 2); + if (fpn >= 0) + addr = fpn * 4; + else + goto from_stack; + } else #endif - if (pn < 4) { + if (pn < 4) { #ifdef TCC_ARM_EABI pn = (pn + (align-1)/4) & -(align/4); #endif - addr = (nf + pn) * 4; - pn += size; - if (!sn && pn > 4) - sn = (pn - 4); - } else { -#ifdef TCC_ARM_HARDFLOAT + addr = (nf + pn) * 4; + pn += size; + if (!sn && pn > 4) + sn = (pn - 4); + } else { from_stack: -#endif #ifdef TCC_ARM_EABI sn = (sn + (align-1)/4) & -(align/4); #endif - addr = (n + nf + sn) * 4; - sn += size; - } - sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | lvalue_type(type->t), addr+12); + addr = (n + nf + sn) * 4; + sn += size; } + sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | lvalue_type(type->t), + addr + 12); } last_itod_magic=0; leaffunc = 1; @@ -1267,10 +1357,10 @@ void gfunc_epilog(void) { uint32_t x; int diff; -#ifdef TCC_ARM_EABI - /* Useless but harmless copy of the float result into main register(s) in case - of variadic function in the hardfloat variant */ - if(is_float(func_vt.t)) { + /* Copy float return value to core register if base standard is used and + float computation is made with VFP */ +#if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP) + if ((float_abi == ARM_SOFTFP_FLOAT || func_var) && is_float(func_vt.t)) { if((func_vt.t & VT_BTYPE) == VT_FLOAT) o(0xEE100A10); /* fmrs r0, s0 */ else { @@ -1328,7 +1418,7 @@ int gtst(int inv, int t) op|=encbranch(r,t,1); o(op); t=r; - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ if ((v & 1) == inv) { if(!vtop->c.i) vtop->c.i=t; @@ -1350,29 +1440,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t)) { - r=gv(RC_FLOAT); -#ifdef TCC_ARM_VFP - o(0xEEB50A40|(vfpr(r)<<12)|T2CPR(vtop->type.t)); /* fcmpzX */ - o(0xEEF1FA10); /* fmstat */ -#else - o(0xEE90F118|(fpr(r)<<16)); -#endif - vtop->r = VT_CMP; - vtop->c.i = TOK_NE; - return gtst(inv, t); - } else if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { - /* constant jmp optimization */ - if ((vtop->c.i != 0) != inv) - t = gjmp(t); - } else { - v = gv(RC_INT); - o(0xE3300000|(intr(v)<<16)); - vtop->r = VT_CMP; - vtop->c.i = TOK_NE; - return gtst(inv, t); - } } vtop--; return t; @@ -1630,7 +1697,7 @@ void gen_opf(int op) case TOK_UGE: op=TOK_GE; break; case TOK_UGT: op=TOK_GT; break; } - + vtop->r = VT_CMP; vtop->c.i = op; return; @@ -1781,7 +1848,7 @@ void gen_opf(int op) r=fpr(gv(RC_FLOAT)); vswap(); r2=fpr(gv(RC_FLOAT)); - } + } break; default: if(op >= TOK_ULT && op <= TOK_GT) { @@ -1793,7 +1860,7 @@ void gen_opf(int op) case TOK_UGE: case TOK_ULE: case TOK_UGT: - tcc_error("unsigned comparision on floats?"); + tcc_error("unsigned comparison on floats?"); break; case TOK_LT: op=TOK_Nset; diff --git a/c67-gen.c b/c67-gen.c index 0d5e33f..a26dfaa 100644 --- a/c67-gen.c +++ b/c67-gen.c @@ -245,8 +245,8 @@ void gsym(int t) } // these are regs that tcc doesn't really know about, -// but asign them unique values so the mapping routines -// can distinquish them +// but assign them unique values so the mapping routines +// can distinguish them #define C67_A0 105 #define C67_SP 106 @@ -1879,10 +1879,11 @@ static void gcall_or_jmp(int is_jmp) } } -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { *ret_align = 1; // Never have to re-align return values for x86-64 - return 1; + return 0; } /* generate function call with address in (vtop->t, vtop->c) and free function @@ -1900,8 +1901,6 @@ void gfunc_call(int nb_args) for (i = 0; i < nb_args; i++) { if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { ALWAYS_ASSERT(FALSE); - } else if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { - ALWAYS_ASSERT(FALSE); } else { /* simple type (currently always same size) */ /* XXX: implicit cast ? */ @@ -1970,6 +1969,7 @@ void gfunc_prolog(CType * func_type) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->type; + func_var = (sym->c == FUNC_ELLIPSIS); if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { func_vc = addr; addr += 4; @@ -2102,7 +2102,7 @@ int gtst(int inv, int t) C67_NOP(5); t = ind1; //return where we need to patch - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -2128,37 +2128,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t)) { - vpushi(0); - gen_op(TOK_NE); - } - if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { - /* constant jmp optimization */ - if ((vtop->c.i != 0) != inv) - t = gjmp(t); - } else { - // I think we need to get the value on the stack - // into a register, test it, and generate a branch - // return the address of the branch, so it can be - // later patched - - v = gv(RC_INT); // get value into a reg - ind1 = ind; - C67_MVKL(C67_A0, t); //r=reg to load, constant - C67_MVKH(C67_A0, t); //r=reg to load, constant - - if (v != TREG_EAX && // check if not already in a conditional test reg - v != TREG_EDX && v != TREG_ST0 && v != C67_B2) { - C67_MV(v, C67_B2); - v = C67_B2; - } - - C67_IREG_B_REG(inv, v, C67_A0); // [!R] B.S2x A0 - C67_NOP(5); - t = ind1; //return where we need to patch - ind1 = ind; - } } vtop--; return t; diff --git a/configure b/configure index b513cc1..f6df9ad 100755 --- a/configure +++ b/configure @@ -56,6 +56,7 @@ case $targetos in DragonFly) noldl=yes;; OpenBSD) noldl=yes;; FreeBSD) noldl=yes;; + NetBSD) noldl=yes;; *) ;; esac @@ -70,46 +71,52 @@ if test -z "$source_path" -o "$source_path" = "." ; then source_path_used="no" fi -case "$cpu" in - i386|i486|i586|i686|i86pc|BePC|i686-AT386) - cpu="x86" - ;; - x86_64|amd64) - cpu="x86-64" - ;; - arm*) - case "$cpu" in - arm|armv4l) - cpuver=4 - ;; - armv5tel|armv5tejl) - cpuver=5 - ;; - armv6j|armv6l) - cpuver=6 - ;; - armv7a|armv7l) - cpuver=7 - ;; - esac - cpu="armv4l" - ;; - alpha) - cpu="alpha" - ;; - "Power Macintosh"|ppc|ppc64) - cpu="powerpc" - ;; - mips) - cpu="mips" - ;; - s390) - cpu="s390" - ;; - *) - cpu="unknown" - ;; -esac +classify_cpu () +{ + cpu="$1" + + case "$cpu" in + x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386) + cpu="x86" + ;; + x86_64|amd64) + cpu="x86-64" + ;; + arm*) + case "$cpu" in + arm|armv4l) + cpuver=4 + ;; + armv5tel|armv5tejl) + cpuver=5 + ;; + armv6j|armv6l) + cpuver=6 + ;; + armv7a|armv7l) + cpuver=7 + ;; + esac + cpu="armv4l" + ;; + alpha) + cpu="alpha" + ;; + "Power Macintosh"|ppc|ppc64) + cpu="powerpc" + ;; + mips) + cpu="mips" + ;; + s390) + cpu="s390" + ;; + *) + echo "Unsupported CPU: $cpu" + exit 1 + ;; + esac +} for opt do eval opt=\"$opt\" @@ -140,14 +147,6 @@ for opt do ;; --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2` ;; - --cc=*) cc=`echo $opt | cut -d '=' -f 2` - ;; - --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}" - ;; - --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}" - ;; - --extra-libs=*) extralibs=${opt#--extra-libs=} - ;; --sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2` ;; --libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2` @@ -156,7 +155,15 @@ for opt do ;; --elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2` ;; - --cpu=*) cpu=`echo $opt | cut -d '=' -f 2` + --cc=*) cc=`echo $opt | cut -d '=' -f 2` + ;; + --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}" + ;; + --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}" + ;; + --extra-libs=*) extralibs=${opt#--extra-libs=} + ;; + --cpu=*) build_cpu=`echo $opt | cut -d '=' -f 2` ;; --enable-gprof) gprof="yes" ;; @@ -185,6 +192,8 @@ for opt do esac done +classify_cpu "$cpu" + # Checking for CFLAGS if test -z "$CFLAGS"; then CFLAGS="-Wall -g -O2" @@ -270,10 +279,11 @@ Advanced options (experts only): --cc=CC use C compiler CC [$cc] --extra-cflags= specify compiler flags [$CFLAGS] --extra-ldflags= specify linker options [] + --cpu=CPU CPU [$cpu] --strip-binaries strip symbol tables from resulting binaries --disable-static make libtcc.so instead of libtcc.a --disable-rpath disable use of -rpath with the above - --with-libgcc use /lib/libgcc_s.so.1 instead of libtcc.a + --with-libgcc use libgcc_s.so.1 instead of libtcc1.a in dynamic link --enable-mingw32 build windows version on linux with mingw32 --enable-cygwin build windows version on windows with cygwin --enable-cross build cross compilers @@ -304,7 +314,7 @@ if test -z "$cross_prefix" ; then if test "$mingw32" = "no" ; then triplet="$($CONFTEST triplet)" if test -f "/usr/lib/$triplet/crti.o" ; then - tcc_lddir="lib/$triplet" + tcc_lddir="lib" multiarch_triplet="$triplet" elif test -f "/usr/lib64/crti.o" ; then tcc_lddir="lib64" @@ -355,8 +365,8 @@ Doc directory $docdir Target root prefix $sysroot Source path $source_path C compiler $cc +Build CPU $cpu Target OS $targetos -CPU $cpu Big Endian $bigendian gprof enabled $gprof cross compilers $build_cross diff --git a/conftest.c b/conftest.c index 53c181c..ddb7a20 100644 --- a/conftest.c +++ b/conftest.c @@ -37,6 +37,10 @@ # define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS "-" TRIPLET_ABI #endif +#if defined(_WIN32) +int _CRT_glob = 0; +#endif + int main(int argc, char *argv[]) { switch(argc == 2 ? argv[1][0] : 0) { diff --git a/elf.h b/elf.h index 2983c75..a3597f9 100644 --- a/elf.h +++ b/elf.h @@ -1,25 +1,23 @@ /* This file defines standard ELF types, structures, and macros. - Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc. + Copyright (C) 1995-2012 Free Software Foundation, Inc. This file is part of the GNU C Library. - Contributed by Ian Lance Taylor . The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. + Lesser General Public License for more details. - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ #ifndef _ELF_H -#define _ELF_H 1 +#define _ELF_H 1 #ifndef _WIN32 #include @@ -46,15 +44,15 @@ typedef uint16_t Elf64_Half; /* Types for signed and unsigned 32-bit quantities. */ typedef uint32_t Elf32_Word; -typedef int32_t Elf32_Sword; +typedef int32_t Elf32_Sword; typedef uint32_t Elf64_Word; -typedef int32_t Elf64_Sword; +typedef int32_t Elf64_Sword; /* Types for signed and unsigned 64-bit quantities. */ typedef uint64_t Elf32_Xword; -typedef int64_t Elf32_Sxword; +typedef int64_t Elf32_Sxword; typedef uint64_t Elf64_Xword; -typedef int64_t Elf64_Sxword; +typedef int64_t Elf64_Sxword; /* Type of addresses. */ typedef uint32_t Elf32_Addr; @@ -68,9 +66,9 @@ typedef uint64_t Elf64_Off; typedef uint16_t Elf32_Section; typedef uint16_t Elf64_Section; -/* Type of symbol indices. */ -typedef uint32_t Elf32_Symndx; -typedef uint64_t Elf64_Symndx; +/* Type for version symbol information. */ +typedef Elf32_Half Elf32_Versym; +typedef Elf64_Half Elf64_Versym; /* The ELF file header. This appears at the start of every ELF file. */ @@ -79,306 +77,336 @@ typedef uint64_t Elf64_Symndx; typedef struct { - unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ - Elf32_Half e_type; /* Object file type */ - Elf32_Half e_machine; /* Architecture */ - Elf32_Word e_version; /* Object file version */ - Elf32_Addr e_entry; /* Entry point virtual address */ - Elf32_Off e_phoff; /* Program header table file offset */ - Elf32_Off e_shoff; /* Section header table file offset */ - Elf32_Word e_flags; /* Processor-specific flags */ - Elf32_Half e_ehsize; /* ELF header size in bytes */ - Elf32_Half e_phentsize; /* Program header table entry size */ - Elf32_Half e_phnum; /* Program header table entry count */ - Elf32_Half e_shentsize; /* Section header table entry size */ - Elf32_Half e_shnum; /* Section header table entry count */ - Elf32_Half e_shstrndx; /* Section header string table index */ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf32_Half e_type; /* Object file type */ + Elf32_Half e_machine; /* Architecture */ + Elf32_Word e_version; /* Object file version */ + Elf32_Addr e_entry; /* Entry point virtual address */ + Elf32_Off e_phoff; /* Program header table file offset */ + Elf32_Off e_shoff; /* Section header table file offset */ + Elf32_Word e_flags; /* Processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size in bytes */ + Elf32_Half e_phentsize; /* Program header table entry size */ + Elf32_Half e_phnum; /* Program header table entry count */ + Elf32_Half e_shentsize; /* Section header table entry size */ + Elf32_Half e_shnum; /* Section header table entry count */ + Elf32_Half e_shstrndx; /* Section header string table index */ } Elf32_Ehdr; typedef struct { - unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ - Elf64_Half e_type; /* Object file type */ - Elf64_Half e_machine; /* Architecture */ - Elf64_Word e_version; /* Object file version */ - Elf64_Addr e_entry; /* Entry point virtual address */ - Elf64_Off e_phoff; /* Program header table file offset */ - Elf64_Off e_shoff; /* Section header table file offset */ - Elf64_Word e_flags; /* Processor-specific flags */ - Elf64_Half e_ehsize; /* ELF header size in bytes */ - Elf64_Half e_phentsize; /* Program header table entry size */ - Elf64_Half e_phnum; /* Program header table entry count */ - Elf64_Half e_shentsize; /* Section header table entry size */ - Elf64_Half e_shnum; /* Section header table entry count */ - Elf64_Half e_shstrndx; /* Section header string table index */ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ } Elf64_Ehdr; /* Fields in the e_ident array. The EI_* macros are indices into the array. The macros under each EI_* macro are the values the byte may have. */ -#define EI_MAG0 0 /* File identification byte 0 index */ -#define ELFMAG0 0x7f /* Magic number byte 0 */ +#define EI_MAG0 0 /* File identification byte 0 index */ +#define ELFMAG0 0x7f /* Magic number byte 0 */ -#define EI_MAG1 1 /* File identification byte 1 index */ -#define ELFMAG1 'E' /* Magic number byte 1 */ +#define EI_MAG1 1 /* File identification byte 1 index */ +#define ELFMAG1 'E' /* Magic number byte 1 */ -#define EI_MAG2 2 /* File identification byte 2 index */ -#define ELFMAG2 'L' /* Magic number byte 2 */ +#define EI_MAG2 2 /* File identification byte 2 index */ +#define ELFMAG2 'L' /* Magic number byte 2 */ -#define EI_MAG3 3 /* File identification byte 3 index */ -#define ELFMAG3 'F' /* Magic number byte 3 */ +#define EI_MAG3 3 /* File identification byte 3 index */ +#define ELFMAG3 'F' /* Magic number byte 3 */ /* Conglomeration of the identification bytes, for easy testing as a word. */ -#define ELFMAG "\177ELF" -#define SELFMAG 4 +#define ELFMAG "\177ELF" +#define SELFMAG 4 -#define EI_CLASS 4 /* File class byte index */ -#define ELFCLASSNONE 0 /* Invalid class */ -#define ELFCLASS32 1 /* 32-bit objects */ -#define ELFCLASS64 2 /* 64-bit objects */ -#define ELFCLASSNUM 3 +#define EI_CLASS 4 /* File class byte index */ +#define ELFCLASSNONE 0 /* Invalid class */ +#define ELFCLASS32 1 /* 32-bit objects */ +#define ELFCLASS64 2 /* 64-bit objects */ +#define ELFCLASSNUM 3 -#define EI_DATA 5 /* Data encoding byte index */ -#define ELFDATANONE 0 /* Invalid data encoding */ -#define ELFDATA2LSB 1 /* 2's complement, little endian */ -#define ELFDATA2MSB 2 /* 2's complement, big endian */ -#define ELFDATANUM 3 +#define EI_DATA 5 /* Data encoding byte index */ +#define ELFDATANONE 0 /* Invalid data encoding */ +#define ELFDATA2LSB 1 /* 2's complement, little endian */ +#define ELFDATA2MSB 2 /* 2's complement, big endian */ +#define ELFDATANUM 3 -#define EI_VERSION 6 /* File version byte index */ - /* Value must be EV_CURRENT */ +#define EI_VERSION 6 /* File version byte index */ + /* Value must be EV_CURRENT */ -#define EI_OSABI 7 /* OS ABI identification */ -#define ELFOSABI_SYSV 0 /* UNIX System V ABI */ -#define ELFOSABI_HPUX 1 /* HP-UX */ -#define ELFOSABI_FREEBSD 9 /* Free BSD */ -#define ELFOSABI_ARM 97 /* ARM */ -#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ +#define EI_OSABI 7 /* OS ABI identification */ +#define ELFOSABI_NONE 0 /* UNIX System V ABI */ +#define ELFOSABI_SYSV 0 /* Alias. */ +#define ELFOSABI_HPUX 1 /* HP-UX */ +#define ELFOSABI_NETBSD 2 /* NetBSD. */ +#define ELFOSABI_GNU 3 /* Object uses GNU ELF extensions. */ +#define ELFOSABI_LINUX ELFOSABI_GNU /* Compatibility alias. */ +#define ELFOSABI_SOLARIS 6 /* Sun Solaris. */ +#define ELFOSABI_AIX 7 /* IBM AIX. */ +#define ELFOSABI_IRIX 8 /* SGI Irix. */ +#define ELFOSABI_FREEBSD 9 /* FreeBSD. */ +#define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX. */ +#define ELFOSABI_MODESTO 11 /* Novell Modesto. */ +#define ELFOSABI_OPENBSD 12 /* OpenBSD. */ +#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */ +#define ELFOSABI_ARM 97 /* ARM */ +#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ -#define EI_ABIVERSION 8 /* ABI version */ +#define EI_ABIVERSION 8 /* ABI version */ -#define EI_PAD 9 /* Byte index of padding bytes */ +#define EI_PAD 9 /* Byte index of padding bytes */ /* Legal values for e_type (object file type). */ -#define ET_NONE 0 /* No file type */ -#define ET_REL 1 /* Relocatable file */ -#define ET_EXEC 2 /* Executable file */ -#define ET_DYN 3 /* Shared object file */ -#define ET_CORE 4 /* Core file */ -#define ET_NUM 5 /* Number of defined types */ -#define ET_LOPROC 0xff00 /* Processor-specific */ -#define ET_HIPROC 0xffff /* Processor-specific */ +#define ET_NONE 0 /* No file type */ +#define ET_REL 1 /* Relocatable file */ +#define ET_EXEC 2 /* Executable file */ +#define ET_DYN 3 /* Shared object file */ +#define ET_CORE 4 /* Core file */ +#define ET_NUM 5 /* Number of defined types */ +#define ET_LOOS 0xfe00 /* OS-specific range start */ +#define ET_HIOS 0xfeff /* OS-specific range end */ +#define ET_LOPROC 0xff00 /* Processor-specific range start */ +#define ET_HIPROC 0xffff /* Processor-specific range end */ /* Legal values for e_machine (architecture). */ -#define EM_NONE 0 /* No machine */ -#define EM_M32 1 /* AT&T WE 32100 */ -#define EM_SPARC 2 /* SUN SPARC */ -#define EM_386 3 /* Intel 80386 */ -#define EM_68K 4 /* Motorola m68k family */ -#define EM_88K 5 /* Motorola m88k family */ -#define EM_486 6 /* Intel 80486 */ -#define EM_860 7 /* Intel 80860 */ -#define EM_MIPS 8 /* MIPS R3000 big-endian */ -#define EM_S370 9 /* Amdahl */ -#define EM_MIPS_RS4_BE 10 /* MIPS R4000 big-endian */ -#define EM_RS6000 11 /* RS6000 */ +#define EM_NONE 0 /* No machine */ +#define EM_M32 1 /* AT&T WE 32100 */ +#define EM_SPARC 2 /* SUN SPARC */ +#define EM_386 3 /* Intel 80386 */ +#define EM_68K 4 /* Motorola m68k family */ +#define EM_88K 5 /* Motorola m88k family */ +#define EM_860 7 /* Intel 80860 */ +#define EM_MIPS 8 /* MIPS R3000 big-endian */ +#define EM_S370 9 /* IBM System/370 */ +#define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ -#define EM_PARISC 15 /* HPPA */ -#define EM_nCUBE 16 /* nCUBE */ -#define EM_VPP500 17 /* Fujitsu VPP500 */ -#define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ -#define EM_960 19 /* Intel 80960 */ -#define EM_PPC 20 /* PowerPC */ +#define EM_PARISC 15 /* HPPA */ +#define EM_VPP500 17 /* Fujitsu VPP500 */ +#define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ +#define EM_960 19 /* Intel 80960 */ +#define EM_PPC 20 /* PowerPC */ +#define EM_PPC64 21 /* PowerPC 64-bit */ +#define EM_S390 22 /* IBM S390 */ -#define EM_V800 36 /* NEC V800 series */ -#define EM_FR20 37 /* Fujitsu FR20 */ -#define EM_RH32 38 /* TRW RH32 */ -#define EM_RCE 39 /* Motorola RCE */ -#define EM_ARM 40 /* ARM */ -#define EM_FAKE_ALPHA 41 /* Digital Alpha */ -#define EM_SH 42 /* Hitachi SH */ -#define EM_SPARCV9 43 /* SPARC v9 64-bit */ -#define EM_TRICORE 44 /* Siemens Tricore */ -#define EM_ARC 45 /* Argonaut RISC Core */ -#define EM_H8_300 46 /* Hitachi H8/300 */ -#define EM_H8_300H 47 /* Hitachi H8/300H */ -#define EM_H8S 48 /* Hitachi H8S */ -#define EM_H8_500 49 /* Hitachi H8/500 */ -#define EM_IA_64 50 /* Intel Merced */ -#define EM_MIPS_X 51 /* Stanford MIPS-X */ -#define EM_COLDFIRE 52 /* Motorola Coldfire */ -#define EM_68HC12 53 /* Motorola M68HC12 */ -#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/ -#define EM_PCP 55 /* Siemens PCP */ -#define EM_NCPU 56 /* Sony nCPU embeeded RISC */ -#define EM_NDR1 57 /* Denso NDR1 microprocessor */ -#define EM_STARCORE 58 /* Motorola Start*Core processor */ -#define EM_ME16 59 /* Toyota ME16 processor */ -#define EM_ST100 60 /* STMicroelectronic ST100 processor */ -#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/ -#define EM_X86_64 62 /* AMD x86-64 architecture */ -#define EM_PDSP 63 /* Sony DSP Processor */ -#define EM_FX66 66 /* Siemens FX66 microcontroller */ -#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */ -#define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */ -#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */ -#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */ -#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */ -#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */ -#define EM_SVX 73 /* Silicon Graphics SVx */ -#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */ -#define EM_VAX 75 /* Digital VAX */ -#define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ -#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded processor */ -#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */ -#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */ -#define EM_MMIX 80 /* Donald Knuth's educational 64-bit processor */ -#define EM_HUANY 81 /* Harvard University machine-independent object files */ -#define EM_PRISM 82 /* SiTera Prism */ -#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */ -#define EM_FR30 84 /* Fujitsu FR30 */ -#define EM_D10V 85 /* Mitsubishi D10V */ -#define EM_D30V 86 /* Mitsubishi D30V */ -#define EM_V850 87 /* NEC v850 */ -#define EM_M32R 88 /* Mitsubishi M32R */ -#define EM_MN10300 89 /* Matsushita MN10300 */ -#define EM_MN10200 90 /* Matsushita MN10200 */ -#define EM_PJ 91 /* picoJava */ -#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ -#define EM_ARC_A5 93 /* ARC Cores Tangent-A5 */ -#define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ -#define EM_NUM 95 +#define EM_V800 36 /* NEC V800 series */ +#define EM_FR20 37 /* Fujitsu FR20 */ +#define EM_RH32 38 /* TRW RH-32 */ +#define EM_RCE 39 /* Motorola RCE */ +#define EM_ARM 40 /* ARM */ +#define EM_FAKE_ALPHA 41 /* Digital Alpha */ +#define EM_SH 42 /* Hitachi SH */ +#define EM_SPARCV9 43 /* SPARC v9 64-bit */ +#define EM_TRICORE 44 /* Siemens Tricore */ +#define EM_ARC 45 /* Argonaut RISC Core */ +#define EM_H8_300 46 /* Hitachi H8/300 */ +#define EM_H8_300H 47 /* Hitachi H8/300H */ +#define EM_H8S 48 /* Hitachi H8S */ +#define EM_H8_500 49 /* Hitachi H8/500 */ +#define EM_IA_64 50 /* Intel Merced */ +#define EM_MIPS_X 51 /* Stanford MIPS-X */ +#define EM_COLDFIRE 52 /* Motorola Coldfire */ +#define EM_68HC12 53 /* Motorola M68HC12 */ +#define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/ +#define EM_PCP 55 /* Siemens PCP */ +#define EM_NCPU 56 /* Sony nCPU embeeded RISC */ +#define EM_NDR1 57 /* Denso NDR1 microprocessor */ +#define EM_STARCORE 58 /* Motorola Start*Core processor */ +#define EM_ME16 59 /* Toyota ME16 processor */ +#define EM_ST100 60 /* STMicroelectronic ST100 processor */ +#define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/ +#define EM_X86_64 62 /* AMD x86-64 architecture */ +#define EM_PDSP 63 /* Sony DSP Processor */ + +#define EM_FX66 66 /* Siemens FX66 microcontroller */ +#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */ +#define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */ +#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */ +#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */ +#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */ +#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */ +#define EM_SVX 73 /* Silicon Graphics SVx */ +#define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */ +#define EM_VAX 75 /* Digital VAX */ +#define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ +#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded processor */ +#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */ +#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */ +#define EM_MMIX 80 /* Donald Knuth's educational 64-bit processor */ +#define EM_HUANY 81 /* Harvard University machine-independent object files */ +#define EM_PRISM 82 /* SiTera Prism */ +#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */ +#define EM_FR30 84 /* Fujitsu FR30 */ +#define EM_D10V 85 /* Mitsubishi D10V */ +#define EM_D30V 86 /* Mitsubishi D30V */ +#define EM_V850 87 /* NEC v850 */ +#define EM_M32R 88 /* Mitsubishi M32R */ +#define EM_MN10300 89 /* Matsushita MN10300 */ +#define EM_MN10200 90 /* Matsushita MN10200 */ +#define EM_PJ 91 /* picoJava */ +#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ +#define EM_ARC_A5 93 /* ARC Cores Tangent-A5 */ +#define EM_XTENSA 94 /* Tensilica Xtensa Architecture */ +#define EM_AARCH64 183 /* ARM AARCH64 */ +#define EM_TILEPRO 188 /* Tilera TILEPro */ +#define EM_TILEGX 191 /* Tilera TILE-Gx */ +#define EM_NUM 192 /* If it is necessary to assign new unofficial EM_* values, please pick large random numbers (0x8523, 0xa7f2, etc.) to minimize the chances of collision with official or non-GNU unofficial values. */ -#define EM_ALPHA 0x9026 -#define EM_C60 0x9c60 +#define EM_ALPHA 0x9026 +#define EM_C60 0x9c60 /* Legal values for e_version (version). */ -#define EV_NONE 0 /* Invalid ELF version */ -#define EV_CURRENT 1 /* Current version */ -#define EV_NUM 2 +#define EV_NONE 0 /* Invalid ELF version */ +#define EV_CURRENT 1 /* Current version */ +#define EV_NUM 2 /* Section header. */ typedef struct { - Elf32_Word sh_name; /* Section name (string tbl index) */ - Elf32_Word sh_type; /* Section type */ - Elf32_Word sh_flags; /* Section flags */ - Elf32_Addr sh_addr; /* Section virtual addr at execution */ - Elf32_Off sh_offset; /* Section file offset */ - Elf32_Word sh_size; /* Section size in bytes */ - Elf32_Word sh_link; /* Link to another section */ - Elf32_Word sh_info; /* Additional section information */ - Elf32_Word sh_addralign; /* Section alignment */ - Elf32_Word sh_entsize; /* Entry size if section holds table */ + Elf32_Word sh_name; /* Section name (string tbl index) */ + Elf32_Word sh_type; /* Section type */ + Elf32_Word sh_flags; /* Section flags */ + Elf32_Addr sh_addr; /* Section virtual addr at execution */ + Elf32_Off sh_offset; /* Section file offset */ + Elf32_Word sh_size; /* Section size in bytes */ + Elf32_Word sh_link; /* Link to another section */ + Elf32_Word sh_info; /* Additional section information */ + Elf32_Word sh_addralign; /* Section alignment */ + Elf32_Word sh_entsize; /* Entry size if section holds table */ } Elf32_Shdr; typedef struct { - Elf64_Word sh_name; /* Section name (string tbl index) */ - Elf64_Word sh_type; /* Section type */ - Elf64_Xword sh_flags; /* Section flags */ - Elf64_Addr sh_addr; /* Section virtual addr at execution */ - Elf64_Off sh_offset; /* Section file offset */ - Elf64_Xword sh_size; /* Section size in bytes */ - Elf64_Word sh_link; /* Link to another section */ - Elf64_Word sh_info; /* Additional section information */ - Elf64_Xword sh_addralign; /* Section alignment */ - Elf64_Xword sh_entsize; /* Entry size if section holds table */ + Elf64_Word sh_name; /* Section name (string tbl index) */ + Elf64_Word sh_type; /* Section type */ + Elf64_Xword sh_flags; /* Section flags */ + Elf64_Addr sh_addr; /* Section virtual addr at execution */ + Elf64_Off sh_offset; /* Section file offset */ + Elf64_Xword sh_size; /* Section size in bytes */ + Elf64_Word sh_link; /* Link to another section */ + Elf64_Word sh_info; /* Additional section information */ + Elf64_Xword sh_addralign; /* Section alignment */ + Elf64_Xword sh_entsize; /* Entry size if section holds table */ } Elf64_Shdr; /* Special section indices. */ -#define SHN_UNDEF 0 /* Undefined section */ -#define SHN_LORESERVE 0xff00 /* Start of reserved indices */ -#define SHN_LOPROC 0xff00 /* Start of processor-specific */ -#define SHN_HIPROC 0xff1f /* End of processor-specific */ -#define SHN_ABS 0xfff1 /* Associated symbol is absolute */ -#define SHN_COMMON 0xfff2 /* Associated symbol is common */ -#define SHN_HIRESERVE 0xffff /* End of reserved indices */ +#define SHN_UNDEF 0 /* Undefined section */ +#define SHN_LORESERVE 0xff00 /* Start of reserved indices */ +#define SHN_LOPROC 0xff00 /* Start of processor-specific */ +#define SHN_BEFORE 0xff00 /* Order section before all others + (Solaris). */ +#define SHN_AFTER 0xff01 /* Order section after all others + (Solaris). */ +#define SHN_HIPROC 0xff1f /* End of processor-specific */ +#define SHN_LOOS 0xff20 /* Start of OS-specific */ +#define SHN_HIOS 0xff3f /* End of OS-specific */ +#define SHN_ABS 0xfff1 /* Associated symbol is absolute */ +#define SHN_COMMON 0xfff2 /* Associated symbol is common */ +#define SHN_XINDEX 0xffff /* Index is in extra table. */ +#define SHN_HIRESERVE 0xffff /* End of reserved indices */ /* Legal values for sh_type (section type). */ -#define SHT_NULL 0 /* Section header table entry unused */ -#define SHT_PROGBITS 1 /* Program data */ -#define SHT_SYMTAB 2 /* Symbol table */ -#define SHT_STRTAB 3 /* String table */ -#define SHT_RELA 4 /* Relocation entries with addends */ -#define SHT_HASH 5 /* Symbol hash table */ -#define SHT_DYNAMIC 6 /* Dynamic linking information */ -#define SHT_NOTE 7 /* Notes */ -#define SHT_NOBITS 8 /* Program space with no data (bss) */ -#define SHT_REL 9 /* Relocation entries, no addends */ -#define SHT_SHLIB 10 /* Reserved */ -#define SHT_DYNSYM 11 /* Dynamic linker symbol table */ -#define SHT_INIT_ARRAY 14 /* Array of constructors */ -#define SHT_FINI_ARRAY 15 /* Array of destructors */ -#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ -#define SHT_GROUP 17 /* Section group */ -#define SHT_SYMTAB_SHNDX 18 /* Extended section indices */ -#define SHT_NUM 19 /* Number of defined types. */ -#define SHT_LOOS 0x60000000 /* Start OS-specific */ -#define SHT_LOSUNW 0x6ffffffb /* Sun-specific low bound. */ -#define SHT_SUNW_COMDAT 0x6ffffffb -#define SHT_SUNW_syminfo 0x6ffffffc -#define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */ -#define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */ -#define SHT_GNU_versym 0x6fffffff /* Version symbol table. */ -#define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */ -#define SHT_HIOS 0x6fffffff /* End OS-specific type */ -#define SHT_LOPROC 0x70000000 /* Start of processor-specific */ -#define SHT_ARM_EXIDX 0x70000001 /* Exception Index table */ -#define SHT_ARM_PREEMPTMAP 0x70000002 /* dynamic linking pre-emption map */ -#define SHT_ARM_ATTRIBUTES 0x70000003 /* Object file compatibility attrs */ -#define SHT_HIPROC 0x7fffffff /* End of processor-specific */ -#define SHT_LOUSER 0x80000000 /* Start of application-specific */ -#define SHT_HIUSER 0x8fffffff /* End of application-specific */ +#define SHT_NULL 0 /* Section header table entry unused */ +#define SHT_PROGBITS 1 /* Program data */ +#define SHT_SYMTAB 2 /* Symbol table */ +#define SHT_STRTAB 3 /* String table */ +#define SHT_RELA 4 /* Relocation entries with addends */ +#define SHT_HASH 5 /* Symbol hash table */ +#define SHT_DYNAMIC 6 /* Dynamic linking information */ +#define SHT_NOTE 7 /* Notes */ +#define SHT_NOBITS 8 /* Program space with no data (bss) */ +#define SHT_REL 9 /* Relocation entries, no addends */ +#define SHT_SHLIB 10 /* Reserved */ +#define SHT_DYNSYM 11 /* Dynamic linker symbol table */ +#define SHT_INIT_ARRAY 14 /* Array of constructors */ +#define SHT_FINI_ARRAY 15 /* Array of destructors */ +#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ +#define SHT_GROUP 17 /* Section group */ +#define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */ +#define SHT_NUM 19 /* Number of defined types. */ +#define SHT_LOOS 0x60000000 /* Start OS-specific. */ +#define SHT_GNU_ATTRIBUTES 0x6ffffff5 /* Object attributes. */ +#define SHT_GNU_HASH 0x6ffffff6 /* GNU-style hash table. */ +#define SHT_GNU_LIBLIST 0x6ffffff7 /* Prelink library list */ +#define SHT_CHECKSUM 0x6ffffff8 /* Checksum for DSO content. */ +#define SHT_LOSUNW 0x6ffffffa /* Sun-specific low bound. */ +#define SHT_SUNW_move 0x6ffffffa +#define SHT_SUNW_COMDAT 0x6ffffffb +#define SHT_SUNW_syminfo 0x6ffffffc +#define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */ +#define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */ +#define SHT_GNU_versym 0x6fffffff /* Version symbol table. */ +#define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */ +#define SHT_HIOS 0x6fffffff /* End OS-specific type */ +#define SHT_LOPROC 0x70000000 /* Start of processor-specific */ +#define SHT_HIPROC 0x7fffffff /* End of processor-specific */ +#define SHT_LOUSER 0x80000000 /* Start of application-specific */ +#define SHT_HIUSER 0x8fffffff /* End of application-specific */ /* Legal values for sh_flags (section flags). */ -#define SHF_WRITE (1 << 0) /* Writable */ -#define SHF_ALLOC (1 << 1) /* Occupies memory during execution */ -#define SHF_EXECINSTR (1 << 2) /* Executable */ -#define SHF_MASKPROC 0xf0000000 /* Processor-specific */ +#define SHF_WRITE (1 << 0) /* Writable */ +#define SHF_ALLOC (1 << 1) /* Occupies memory during execution */ +#define SHF_EXECINSTR (1 << 2) /* Executable */ +#define SHF_MERGE (1 << 4) /* Might be merged */ +#define SHF_STRINGS (1 << 5) /* Contains nul-terminated strings */ +#define SHF_INFO_LINK (1 << 6) /* `sh_info' contains SHT index */ +#define SHF_LINK_ORDER (1 << 7) /* Preserve order after combining */ +#define SHF_OS_NONCONFORMING (1 << 8) /* Non-standard OS specific handling + required */ +#define SHF_GROUP (1 << 9) /* Section is member of a group. */ +#define SHF_TLS (1 << 10) /* Section hold thread-local data. */ +#define SHF_MASKOS 0x0ff00000 /* OS-specific. */ +#define SHF_MASKPROC 0xf0000000 /* Processor-specific */ +#define SHF_ORDERED (1 << 30) /* Special ordering requirement + (Solaris). */ +#define SHF_EXCLUDE (1 << 31) /* Section is excluded unless + referenced or allocated (Solaris).*/ -#define SHF_MERGE 0x10 -#define SHF_STRINGS 0x20 -#define SHF_INFO_LINK 0x40 -#define SHF_LINK_ORDER 0x80 -#define SHF_OS_NONCONFORMING 0x100 -#define SHF_GROUP 0x200 -#define SHF_TLS 0x400 -#define SHF_MASKOS 0x0ff00000 -#define SHF_ORDERED 0x40000000 -#define SHF_EXCLUDE 0x80000000 +/* Section group handling. */ +#define GRP_COMDAT 0x1 /* Mark group as COMDAT. */ /* Symbol table entry. */ typedef struct { - Elf32_Word st_name; /* Symbol name (string tbl index) */ - Elf32_Addr st_value; /* Symbol value */ - Elf32_Word st_size; /* Symbol size */ - unsigned char st_info; /* Symbol type and binding */ - unsigned char st_other; /* No defined meaning, 0 */ - Elf32_Section st_shndx; /* Section index */ + Elf32_Word st_name; /* Symbol name (string tbl index) */ + Elf32_Addr st_value; /* Symbol value */ + Elf32_Word st_size; /* Symbol size */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf32_Section st_shndx; /* Section index */ } Elf32_Sym; typedef struct { - Elf64_Word st_name; /* Symbol name (string tbl index) */ - unsigned char st_info; /* Symbol type and binding */ - unsigned char st_other; /* No defined meaning, 0 */ - Elf64_Section st_shndx; /* Section index */ - Elf64_Addr st_value; /* Symbol value */ - Elf64_Xword st_size; /* Symbol size */ + Elf64_Word st_name; /* Symbol name (string tbl index) */ + unsigned char st_info; /* Symbol type and binding */ + unsigned char st_other; /* Symbol visibility */ + Elf64_Section st_shndx; /* Section index */ + Elf64_Addr st_value; /* Symbol value */ + Elf64_Xword st_size; /* Symbol size */ } Elf64_Sym; /* The syminfo section if available contains additional information about @@ -386,101 +414,100 @@ typedef struct typedef struct { - Elf32_Half si_boundto; /* Direct bindings, symbol bound to */ - Elf32_Half si_flags; /* Per symbol flags */ + Elf32_Half si_boundto; /* Direct bindings, symbol bound to */ + Elf32_Half si_flags; /* Per symbol flags */ } Elf32_Syminfo; typedef struct { - Elf64_Half si_boundto; /* Direct bindings, symbol bound to */ - Elf64_Half si_flags; /* Per symbol flags */ + Elf64_Half si_boundto; /* Direct bindings, symbol bound to */ + Elf64_Half si_flags; /* Per symbol flags */ } Elf64_Syminfo; /* Possible values for si_boundto. */ -#define SYMINFO_BT_SELF 0xffff /* Symbol bound to self */ -#define SYMINFO_BT_PARENT 0xfffe /* Symbol bound to parent */ -#define SYMINFO_BT_LOWRESERVE 0xff00 /* Beginning of reserved entries */ +#define SYMINFO_BT_SELF 0xffff /* Symbol bound to self */ +#define SYMINFO_BT_PARENT 0xfffe /* Symbol bound to parent */ +#define SYMINFO_BT_LOWRESERVE 0xff00 /* Beginning of reserved entries */ /* Possible bitmasks for si_flags. */ -#define SYMINFO_FLG_DIRECT 0x0001 /* Direct bound symbol */ -#define SYMINFO_FLG_PASSTHRU 0x0002 /* Pass-thru symbol for translator */ -#define SYMINFO_FLG_COPY 0x0004 /* Symbol is a copy-reloc */ -#define SYMINFO_FLG_LAZYLOAD 0x0008 /* Symbol bound to object to be lazy - loaded */ +#define SYMINFO_FLG_DIRECT 0x0001 /* Direct bound symbol */ +#define SYMINFO_FLG_PASSTHRU 0x0002 /* Pass-thru symbol for translator */ +#define SYMINFO_FLG_COPY 0x0004 /* Symbol is a copy-reloc */ +#define SYMINFO_FLG_LAZYLOAD 0x0008 /* Symbol bound to object to be lazy + loaded */ /* Syminfo version values. */ -#define SYMINFO_NONE 0 -#define SYMINFO_CURRENT 1 -#define SYMINFO_NUM 2 +#define SYMINFO_NONE 0 +#define SYMINFO_CURRENT 1 +#define SYMINFO_NUM 2 -/* Special section index. */ - -#define SHN_UNDEF 0 /* No section, undefined symbol. */ - /* How to extract and insert information held in the st_info field. */ -#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4) -#define ELF32_ST_TYPE(val) ((val) & 0xf) -#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) +#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4) +#define ELF32_ST_TYPE(val) ((val) & 0xf) +#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) /* Both Elf32_Sym and Elf64_Sym use the same one-byte st_info field. */ -#define ELF64_ST_BIND(val) ELF32_ST_BIND (val) -#define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val) -#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) +#define ELF64_ST_BIND(val) ELF32_ST_BIND (val) +#define ELF64_ST_TYPE(val) ELF32_ST_TYPE (val) +#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type)) /* Legal values for ST_BIND subfield of st_info (symbol binding). */ -#define STB_LOCAL 0 /* Local symbol */ -#define STB_GLOBAL 1 /* Global symbol */ -#define STB_WEAK 2 /* Weak symbol */ -#define STB_NUM 3 /* Number of defined types. */ -#define STB_LOOS 10 /* Start of OS-specific */ -#define STB_HIOS 12 /* End of OS-specific */ -#define STB_LOPROC 13 /* Start of processor-specific */ -#define STB_HIPROC 15 /* End of processor-specific */ +#define STB_LOCAL 0 /* Local symbol */ +#define STB_GLOBAL 1 /* Global symbol */ +#define STB_WEAK 2 /* Weak symbol */ +#define STB_NUM 3 /* Number of defined types. */ +#define STB_LOOS 10 /* Start of OS-specific */ +#define STB_GNU_UNIQUE 10 /* Unique symbol. */ +#define STB_HIOS 12 /* End of OS-specific */ +#define STB_LOPROC 13 /* Start of processor-specific */ +#define STB_HIPROC 15 /* End of processor-specific */ /* Legal values for ST_TYPE subfield of st_info (symbol type). */ -#define STT_NOTYPE 0 /* Symbol type is unspecified */ -#define STT_OBJECT 1 /* Symbol is a data object */ -#define STT_FUNC 2 /* Symbol is a code object */ -#define STT_SECTION 3 /* Symbol associated with a section */ -#define STT_FILE 4 /* Symbol's name is file name */ -#define STT_NUM 5 /* Number of defined types. */ -#define STT_GNU_IFUNC 10 /* Symbol is a indirect code object */ -#define STT_LOOS 11 /* Start of OS-specific */ -#define STT_HIOS 12 /* End of OS-specific */ -#define STT_LOPROC 13 /* Start of processor-specific */ -#define STT_HIPROC 15 /* End of processor-specific */ +#define STT_NOTYPE 0 /* Symbol type is unspecified */ +#define STT_OBJECT 1 /* Symbol is a data object */ +#define STT_FUNC 2 /* Symbol is a code object */ +#define STT_SECTION 3 /* Symbol associated with a section */ +#define STT_FILE 4 /* Symbol's name is file name */ +#define STT_COMMON 5 /* Symbol is a common data object */ +#define STT_TLS 6 /* Symbol is thread-local data object*/ +#define STT_NUM 7 /* Number of defined types. */ +#define STT_LOOS 10 /* Start of OS-specific */ +#define STT_GNU_IFUNC 10 /* Symbol is indirect code object */ +#define STT_HIOS 12 /* End of OS-specific */ +#define STT_LOPROC 13 /* Start of processor-specific */ +#define STT_HIPROC 15 /* End of processor-specific */ /* Symbol table indices are found in the hash buckets and chain table of a symbol hash table section. This special index value indicates the end of a chain, meaning no further symbols are found in that bucket. */ -#define STN_UNDEF 0 /* End of a chain. */ +#define STN_UNDEF 0 /* End of a chain. */ /* How to extract and insert information held in the st_other field. */ -#define ELF32_ST_VISIBILITY(o) ((o) & 0x03) +#define ELF32_ST_VISIBILITY(o) ((o) & 0x03) /* For ELF64 the definitions are the same. */ -#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) +#define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o) /* Symbol visibility specification encoded in the st_other field. */ -#define STV_DEFAULT 0 /* Default symbol visibility rules */ -#define STV_INTERNAL 1 /* Processor specific hidden class */ -#define STV_HIDDEN 2 /* Sym unavailable in other modules */ -#define STV_PROTECTED 3 /* Not preemptible, not exported */ +#define STV_DEFAULT 0 /* Default symbol visibility rules */ +#define STV_INTERNAL 1 /* Processor specific hidden class */ +#define STV_HIDDEN 2 /* Sym unavailable in other modules */ +#define STV_PROTECTED 3 /* Not preemptible, not exported */ /* Relocation table entry without addend (in section of type SHT_REL). */ typedef struct { - Elf32_Addr r_offset; /* Address */ - Elf32_Word r_info; /* Relocation type and symbol index */ + Elf32_Addr r_offset; /* Address */ + Elf32_Word r_info; /* Relocation type and symbol index */ } Elf32_Rel; /* I have seen two different definitions of the Elf64_Rel and @@ -490,266 +517,374 @@ typedef struct typedef struct { - Elf64_Addr r_offset; /* Address */ - Elf64_Xword r_info; /* Relocation type and symbol index */ + Elf64_Addr r_offset; /* Address */ + Elf64_Xword r_info; /* Relocation type and symbol index */ } Elf64_Rel; /* Relocation table entry with addend (in section of type SHT_RELA). */ typedef struct { - Elf32_Addr r_offset; /* Address */ - Elf32_Word r_info; /* Relocation type and symbol index */ - Elf32_Sword r_addend; /* Addend */ + Elf32_Addr r_offset; /* Address */ + Elf32_Word r_info; /* Relocation type and symbol index */ + Elf32_Sword r_addend; /* Addend */ } Elf32_Rela; typedef struct { - Elf64_Addr r_offset; /* Address */ - Elf64_Xword r_info; /* Relocation type and symbol index */ - Elf64_Sxword r_addend; /* Addend */ + Elf64_Addr r_offset; /* Address */ + Elf64_Xword r_info; /* Relocation type and symbol index */ + Elf64_Sxword r_addend; /* Addend */ } Elf64_Rela; /* How to extract and insert information held in the r_info field. */ -#define ELF32_R_SYM(val) ((val) >> 8) -#define ELF32_R_TYPE(val) ((val) & 0xff) -#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) +#define ELF32_R_SYM(val) ((val) >> 8) +#define ELF32_R_TYPE(val) ((val) & 0xff) +#define ELF32_R_INFO(sym, type) (((sym) << 8) + ((type) & 0xff)) -#define ELF64_R_SYM(i) ((i) >> 32) -#define ELF64_R_TYPE(i) ((i) & 0xffffffff) -#define ELF64_R_INFO(sym,type) ((((Elf64_Xword)(sym)) << 32) + (type)) +#define ELF64_R_SYM(i) ((i) >> 32) +#define ELF64_R_TYPE(i) ((i) & 0xffffffff) +#define ELF64_R_INFO(sym,type) ((((Elf64_Xword) (sym)) << 32) + (type)) /* Program segment header. */ typedef struct { - Elf32_Word p_type; /* Segment type */ - Elf32_Off p_offset; /* Segment file offset */ - Elf32_Addr p_vaddr; /* Segment virtual address */ - Elf32_Addr p_paddr; /* Segment physical address */ - Elf32_Word p_filesz; /* Segment size in file */ - Elf32_Word p_memsz; /* Segment size in memory */ - Elf32_Word p_flags; /* Segment flags */ - Elf32_Word p_align; /* Segment alignment */ + Elf32_Word p_type; /* Segment type */ + Elf32_Off p_offset; /* Segment file offset */ + Elf32_Addr p_vaddr; /* Segment virtual address */ + Elf32_Addr p_paddr; /* Segment physical address */ + Elf32_Word p_filesz; /* Segment size in file */ + Elf32_Word p_memsz; /* Segment size in memory */ + Elf32_Word p_flags; /* Segment flags */ + Elf32_Word p_align; /* Segment alignment */ } Elf32_Phdr; typedef struct { - Elf64_Word p_type; /* Segment type */ - Elf64_Word p_flags; /* Segment flags */ - Elf64_Off p_offset; /* Segment file offset */ - Elf64_Addr p_vaddr; /* Segment virtual address */ - Elf64_Addr p_paddr; /* Segment physical address */ - Elf64_Xword p_filesz; /* Segment size in file */ - Elf64_Xword p_memsz; /* Segment size in memory */ - Elf64_Xword p_align; /* Segment alignment */ + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ } Elf64_Phdr; +/* Special value for e_phnum. This indicates that the real number of + program headers is too large to fit into e_phnum. Instead the real + value is in the field sh_info of section 0. */ + +#define PN_XNUM 0xffff + /* Legal values for p_type (segment type). */ -#define PT_NULL 0 /* Program header table entry unused */ -#define PT_LOAD 1 /* Loadable program segment */ -#define PT_DYNAMIC 2 /* Dynamic linking information */ -#define PT_INTERP 3 /* Program interpreter */ -#define PT_NOTE 4 /* Auxiliary information */ -#define PT_SHLIB 5 /* Reserved */ -#define PT_PHDR 6 /* Entry for header table itself */ -#define PT_NUM 7 /* Number of defined types. */ -#define PT_LOOS 0x60000000 /* Start of OS-specific */ -#define PT_HIOS 0x6fffffff /* End of OS-specific */ -#define PT_LOPROC 0x70000000 /* Start of processor-specific */ -#define PT_HIPROC 0x7fffffff /* End of processor-specific */ +#define PT_NULL 0 /* Program header table entry unused */ +#define PT_LOAD 1 /* Loadable program segment */ +#define PT_DYNAMIC 2 /* Dynamic linking information */ +#define PT_INTERP 3 /* Program interpreter */ +#define PT_NOTE 4 /* Auxiliary information */ +#define PT_SHLIB 5 /* Reserved */ +#define PT_PHDR 6 /* Entry for header table itself */ +#define PT_TLS 7 /* Thread-local storage segment */ +#define PT_NUM 8 /* Number of defined types */ +#define PT_LOOS 0x60000000 /* Start of OS-specific */ +#define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */ +#define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */ +#define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */ +#define PT_LOSUNW 0x6ffffffa +#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */ +#define PT_SUNWSTACK 0x6ffffffb /* Stack segment */ +#define PT_HISUNW 0x6fffffff +#define PT_HIOS 0x6fffffff /* End of OS-specific */ +#define PT_LOPROC 0x70000000 /* Start of processor-specific */ +#define PT_HIPROC 0x7fffffff /* End of processor-specific */ /* Legal values for p_flags (segment flags). */ -#define PF_X (1 << 0) /* Segment is executable */ -#define PF_W (1 << 1) /* Segment is writable */ -#define PF_R (1 << 2) /* Segment is readable */ -#define PF_MASKPROC 0xf0000000 /* Processor-specific */ +#define PF_X (1 << 0) /* Segment is executable */ +#define PF_W (1 << 1) /* Segment is writable */ +#define PF_R (1 << 2) /* Segment is readable */ +#define PF_MASKOS 0x0ff00000 /* OS-specific */ +#define PF_MASKPROC 0xf0000000 /* Processor-specific */ /* Legal values for note segment descriptor types for core files. */ -#define NT_PRSTATUS 1 /* Contains copy of prstatus struct */ -#define NT_FPREGSET 2 /* Contains copy of fpregset struct */ -#define NT_PRPSINFO 3 /* Contains copy of prpsinfo struct */ -#define NT_PRXREG 4 /* Contains copy of prxregset struct */ -#define NT_PLATFORM 5 /* String from sysinfo(SI_PLATFORM) */ -#define NT_AUXV 6 /* Contains copy of auxv array */ -#define NT_GWINDOWS 7 /* Contains copy of gwindows struct */ -#define NT_PSTATUS 10 /* Contains copy of pstatus struct */ -#define NT_PSINFO 13 /* Contains copy of psinfo struct */ -#define NT_PRCRED 14 /* Contains copy of prcred struct */ -#define NT_UTSNAME 15 /* Contains copy of utsname struct */ -#define NT_LWPSTATUS 16 /* Contains copy of lwpstatus struct */ -#define NT_LWPSINFO 17 /* Contains copy of lwpinfo struct */ +#define NT_PRSTATUS 1 /* Contains copy of prstatus struct */ +#define NT_FPREGSET 2 /* Contains copy of fpregset struct */ +#define NT_PRPSINFO 3 /* Contains copy of prpsinfo struct */ +#define NT_PRXREG 4 /* Contains copy of prxregset struct */ +#define NT_TASKSTRUCT 4 /* Contains copy of task structure */ +#define NT_PLATFORM 5 /* String from sysinfo(SI_PLATFORM) */ +#define NT_AUXV 6 /* Contains copy of auxv array */ +#define NT_GWINDOWS 7 /* Contains copy of gwindows struct */ +#define NT_ASRS 8 /* Contains copy of asrset struct */ +#define NT_PSTATUS 10 /* Contains copy of pstatus struct */ +#define NT_PSINFO 13 /* Contains copy of psinfo struct */ +#define NT_PRCRED 14 /* Contains copy of prcred struct */ +#define NT_UTSNAME 15 /* Contains copy of utsname struct */ +#define NT_LWPSTATUS 16 /* Contains copy of lwpstatus struct */ +#define NT_LWPSINFO 17 /* Contains copy of lwpinfo struct */ +#define NT_PRFPXREG 20 /* Contains copy of fprxregset struct */ +#define NT_PRXFPREG 0x46e62b7f /* Contains copy of user_fxsr_struct */ +#define NT_PPC_VMX 0x100 /* PowerPC Altivec/VMX registers */ +#define NT_PPC_SPE 0x101 /* PowerPC SPE/EVR registers */ +#define NT_PPC_VSX 0x102 /* PowerPC VSX registers */ +#define NT_386_TLS 0x200 /* i386 TLS slots (struct user_desc) */ +#define NT_386_IOPERM 0x201 /* x86 io permission bitmap (1=deny) */ +#define NT_X86_XSTATE 0x202 /* x86 extended state using xsave */ +#define NT_S390_HIGH_GPRS 0x300 /* s390 upper register halves */ +#define NT_S390_TIMER 0x301 /* s390 timer register */ +#define NT_S390_TODCMP 0x302 /* s390 TOD clock comparator register */ +#define NT_S390_TODPREG 0x303 /* s390 TOD programmable register */ +#define NT_S390_CTRS 0x304 /* s390 control registers */ +#define NT_S390_PREFIX 0x305 /* s390 prefix register */ +#define NT_S390_LAST_BREAK 0x306 /* s390 breaking event address */ +#define NT_S390_SYSTEM_CALL 0x307 /* s390 system call restart data */ +#define NT_ARM_VFP 0x400 /* ARM VFP/NEON registers */ +#define NT_ARM_TLS 0x401 /* ARM TLS register */ +#define NT_ARM_HW_BREAK 0x402 /* ARM hardware breakpoint registers */ +#define NT_ARM_HW_WATCH 0x403 /* ARM hardware watchpoint registers */ -/* Legal values for the note segment descriptor types for object files. */ +/* Legal values for the note segment descriptor types for object files. */ -#define NT_VERSION 1 /* Contains a version string. */ +#define NT_VERSION 1 /* Contains a version string. */ /* Dynamic section entry. */ typedef struct { - Elf32_Sword d_tag; /* Dynamic entry type */ + Elf32_Sword d_tag; /* Dynamic entry type */ union { - Elf32_Word d_val; /* Integer value */ - Elf32_Addr d_ptr; /* Address value */ + Elf32_Word d_val; /* Integer value */ + Elf32_Addr d_ptr; /* Address value */ } d_un; } Elf32_Dyn; typedef struct { - Elf64_Sxword d_tag; /* Dynamic entry type */ + Elf64_Sxword d_tag; /* Dynamic entry type */ union { - Elf64_Xword d_val; /* Integer value */ - Elf64_Addr d_ptr; /* Address value */ + Elf64_Xword d_val; /* Integer value */ + Elf64_Addr d_ptr; /* Address value */ } d_un; } Elf64_Dyn; /* Legal values for d_tag (dynamic entry type). */ -#define DT_NULL 0 /* Marks end of dynamic section */ -#define DT_NEEDED 1 /* Name of needed library */ -#define DT_PLTRELSZ 2 /* Size in bytes of PLT relocs */ -#define DT_PLTGOT 3 /* Processor defined value */ -#define DT_HASH 4 /* Address of symbol hash table */ -#define DT_STRTAB 5 /* Address of string table */ -#define DT_SYMTAB 6 /* Address of symbol table */ -#define DT_RELA 7 /* Address of Rela relocs */ -#define DT_RELASZ 8 /* Total size of Rela relocs */ -#define DT_RELAENT 9 /* Size of one Rela reloc */ -#define DT_STRSZ 10 /* Size of string table */ -#define DT_SYMENT 11 /* Size of one symbol table entry */ -#define DT_INIT 12 /* Address of init function */ -#define DT_FINI 13 /* Address of termination function */ -#define DT_SONAME 14 /* Name of shared object */ -#define DT_RPATH 15 /* Library search path */ -#define DT_SYMBOLIC 16 /* Start symbol search here */ -#define DT_REL 17 /* Address of Rel relocs */ -#define DT_RELSZ 18 /* Total size of Rel relocs */ -#define DT_RELENT 19 /* Size of one Rel reloc */ -#define DT_PLTREL 20 /* Type of reloc in PLT */ -#define DT_DEBUG 21 /* For debugging; unspecified */ -#define DT_TEXTREL 22 /* Reloc might modify .text */ -#define DT_JMPREL 23 /* Address of PLT relocs */ -#define DT_BIND_NOW 24 /* Process relocations of object */ -#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */ -#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */ -#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */ -#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */ -#define DT_NUM 29 /* Number used */ -#define DT_LOOS 0x60000000 /* Start of OS-specific */ -#define DT_HIOS 0x6fffffff /* End of OS-specific */ -#define DT_LOPROC 0x70000000 /* Start of processor-specific */ -#define DT_HIPROC 0x7fffffff /* End of processor-specific */ -#define DT_PROCNUM DT_MIPS_NUM /* Most used by any processor */ +#define DT_NULL 0 /* Marks end of dynamic section */ +#define DT_NEEDED 1 /* Name of needed library */ +#define DT_PLTRELSZ 2 /* Size in bytes of PLT relocs */ +#define DT_PLTGOT 3 /* Processor defined value */ +#define DT_HASH 4 /* Address of symbol hash table */ +#define DT_STRTAB 5 /* Address of string table */ +#define DT_SYMTAB 6 /* Address of symbol table */ +#define DT_RELA 7 /* Address of Rela relocs */ +#define DT_RELASZ 8 /* Total size of Rela relocs */ +#define DT_RELAENT 9 /* Size of one Rela reloc */ +#define DT_STRSZ 10 /* Size of string table */ +#define DT_SYMENT 11 /* Size of one symbol table entry */ +#define DT_INIT 12 /* Address of init function */ +#define DT_FINI 13 /* Address of termination function */ +#define DT_SONAME 14 /* Name of shared object */ +#define DT_RPATH 15 /* Library search path (deprecated) */ +#define DT_SYMBOLIC 16 /* Start symbol search here */ +#define DT_REL 17 /* Address of Rel relocs */ +#define DT_RELSZ 18 /* Total size of Rel relocs */ +#define DT_RELENT 19 /* Size of one Rel reloc */ +#define DT_PLTREL 20 /* Type of reloc in PLT */ +#define DT_DEBUG 21 /* For debugging; unspecified */ +#define DT_TEXTREL 22 /* Reloc might modify .text */ +#define DT_JMPREL 23 /* Address of PLT relocs */ +#define DT_BIND_NOW 24 /* Process relocations of object */ +#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */ +#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */ +#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */ +#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */ +#define DT_RUNPATH 29 /* Library search path */ +#define DT_FLAGS 30 /* Flags for the object being loaded */ +#define DT_ENCODING 32 /* Start of encoded range */ +#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/ +#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */ +#define DT_NUM 34 /* Number used */ +#define DT_LOOS 0x6000000d /* Start of OS-specific */ +#define DT_HIOS 0x6ffff000 /* End of OS-specific */ +#define DT_LOPROC 0x70000000 /* Start of processor-specific */ +#define DT_HIPROC 0x7fffffff /* End of processor-specific */ +#define DT_PROCNUM DT_MIPS_NUM /* Most used by any processor */ /* DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the Dyn.d_un.d_val field of the Elf*_Dyn structure. This follows Sun's approach. */ -#define DT_VALRNGLO 0x6ffffd00 -#define DT_POSFLAG_1 0x6ffffdfd /* Flags for DT_* entries, effecting - the following DT_* entry. */ -#define DT_SYMINSZ 0x6ffffdfe /* Size of syminfo table (in bytes) */ -#define DT_SYMINENT 0x6ffffdff /* Entry size of syminfo */ -#define DT_VALRNGHI 0x6ffffdff +#define DT_VALRNGLO 0x6ffffd00 +#define DT_GNU_PRELINKED 0x6ffffdf5 /* Prelinking timestamp */ +#define DT_GNU_CONFLICTSZ 0x6ffffdf6 /* Size of conflict section */ +#define DT_GNU_LIBLISTSZ 0x6ffffdf7 /* Size of library list */ +#define DT_CHECKSUM 0x6ffffdf8 +#define DT_PLTPADSZ 0x6ffffdf9 +#define DT_MOVEENT 0x6ffffdfa +#define DT_MOVESZ 0x6ffffdfb +#define DT_FEATURE_1 0x6ffffdfc /* Feature selection (DTF_*). */ +#define DT_POSFLAG_1 0x6ffffdfd /* Flags for DT_* entries, effecting + the following DT_* entry. */ +#define DT_SYMINSZ 0x6ffffdfe /* Size of syminfo table (in bytes) */ +#define DT_SYMINENT 0x6ffffdff /* Entry size of syminfo */ +#define DT_VALRNGHI 0x6ffffdff +#define DT_VALTAGIDX(tag) (DT_VALRNGHI - (tag)) /* Reverse order! */ +#define DT_VALNUM 12 /* DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the Dyn.d_un.d_ptr field of the Elf*_Dyn structure. If any adjustment is made to the ELF object after it has been built these entries will need to be adjusted. */ -#define DT_ADDRRNGLO 0x6ffffe00 -#define DT_SYMINFO 0x6ffffeff /* syminfo table */ -#define DT_ADDRRNGHI 0x6ffffeff +#define DT_ADDRRNGLO 0x6ffffe00 +#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table. */ +#define DT_TLSDESC_PLT 0x6ffffef6 +#define DT_TLSDESC_GOT 0x6ffffef7 +#define DT_GNU_CONFLICT 0x6ffffef8 /* Start of conflict section */ +#define DT_GNU_LIBLIST 0x6ffffef9 /* Library list */ +#define DT_CONFIG 0x6ffffefa /* Configuration information. */ +#define DT_DEPAUDIT 0x6ffffefb /* Dependency auditing. */ +#define DT_AUDIT 0x6ffffefc /* Object auditing. */ +#define DT_PLTPAD 0x6ffffefd /* PLT padding. */ +#define DT_MOVETAB 0x6ffffefe /* Move table. */ +#define DT_SYMINFO 0x6ffffeff /* Syminfo table. */ +#define DT_ADDRRNGHI 0x6ffffeff +#define DT_ADDRTAGIDX(tag) (DT_ADDRRNGHI - (tag)) /* Reverse order! */ +#define DT_ADDRNUM 11 /* The versioning entry types. The next are defined as part of the GNU extension. */ -#define DT_VERSYM 0x6ffffff0 +#define DT_VERSYM 0x6ffffff0 + +#define DT_RELACOUNT 0x6ffffff9 +#define DT_RELCOUNT 0x6ffffffa /* These were chosen by Sun. */ -#define DT_FLAGS_1 0x6ffffffb /* State flags, see DF_1_* below. */ -#define DT_VERDEF 0x6ffffffc /* Address of version definition - table */ -#define DT_VERDEFNUM 0x6ffffffd /* Number of version definitions */ -#define DT_VERNEED 0x6ffffffe /* Address of table with needed - versions */ -#define DT_VERNEEDNUM 0x6fffffff /* Number of needed versions */ -#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */ +#define DT_FLAGS_1 0x6ffffffb /* State flags, see DF_1_* below. */ +#define DT_VERDEF 0x6ffffffc /* Address of version definition + table */ +#define DT_VERDEFNUM 0x6ffffffd /* Number of version definitions */ +#define DT_VERNEED 0x6ffffffe /* Address of table with needed + versions */ +#define DT_VERNEEDNUM 0x6fffffff /* Number of needed versions */ +#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */ #define DT_VERSIONTAGNUM 16 /* Sun added these machine-independent extensions in the "processor-specific" range. Be compatible. */ #define DT_AUXILIARY 0x7ffffffd /* Shared object to load before self */ #define DT_FILTER 0x7fffffff /* Shared object to get values from */ -#define DT_EXTRATAGIDX(tag) ((Elf32_Word)-((Elf32_Sword) (tag) <<1>>1)-1) -#define DT_EXTRANUM 3 +#define DT_EXTRATAGIDX(tag) ((Elf32_Word)-((Elf32_Sword) (tag) <<1>>1)-1) +#define DT_EXTRANUM 3 + +/* Values of `d_un.d_val' in the DT_FLAGS entry. */ +#define DF_ORIGIN 0x00000001 /* Object may use DF_ORIGIN */ +#define DF_SYMBOLIC 0x00000002 /* Symbol resolutions starts here */ +#define DF_TEXTREL 0x00000004 /* Object contains text relocations */ +#define DF_BIND_NOW 0x00000008 /* No lazy binding for this object */ +#define DF_STATIC_TLS 0x00000010 /* Module uses the static TLS model */ /* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1 entry in the dynamic section. */ -#define DF_1_NOW 0x00000001 /* Set RTLD_NOW for this object. */ -#define DF_1_GLOBAL 0x00000002 /* Set RTLD_GLOBAL for this object. */ -#define DF_1_GROUP 0x00000004 /* Set RTLD_GROUP for this object. */ -#define DF_1_NODELETE 0x00000008 /* Set RTLD_NODELETE for this object.*/ -#define DF_1_LOADFLTR 0x00000010 /* Trigger filtee loading at runtime.*/ -#define DF_1_INITFIRST 0x00000020 /* Set RTLD_INITFIRST for this object*/ -#define DF_1_NOOPEN 0x00000040 /* Set RTLD_NOOPEN for this object. */ +#define DF_1_NOW 0x00000001 /* Set RTLD_NOW for this object. */ +#define DF_1_GLOBAL 0x00000002 /* Set RTLD_GLOBAL for this object. */ +#define DF_1_GROUP 0x00000004 /* Set RTLD_GROUP for this object. */ +#define DF_1_NODELETE 0x00000008 /* Set RTLD_NODELETE for this object.*/ +#define DF_1_LOADFLTR 0x00000010 /* Trigger filtee loading at runtime.*/ +#define DF_1_INITFIRST 0x00000020 /* Set RTLD_INITFIRST for this object*/ +#define DF_1_NOOPEN 0x00000040 /* Set RTLD_NOOPEN for this object. */ +#define DF_1_ORIGIN 0x00000080 /* $ORIGIN must be handled. */ +#define DF_1_DIRECT 0x00000100 /* Direct binding enabled. */ +#define DF_1_TRANS 0x00000200 +#define DF_1_INTERPOSE 0x00000400 /* Object is used to interpose. */ +#define DF_1_NODEFLIB 0x00000800 /* Ignore default lib search path. */ +#define DF_1_NODUMP 0x00001000 /* Object can't be dldump'ed. */ +#define DF_1_CONFALT 0x00002000 /* Configuration alternative created.*/ +#define DF_1_ENDFILTEE 0x00004000 /* Filtee terminates filters search. */ +#define DF_1_DISPRELDNE 0x00008000 /* Disp reloc applied at build time. */ +#define DF_1_DISPRELPND 0x00010000 /* Disp reloc applied at run-time. */ +#define DF_1_NODIRECT 0x00020000 /* Object has no-direct binding. */ +#define DF_1_IGNMULDEF 0x00040000 +#define DF_1_NOKSYMS 0x00080000 +#define DF_1_NOHDR 0x00100000 +#define DF_1_EDITED 0x00200000 /* Object is modified after built. */ +#define DF_1_NORELOC 0x00400000 +#define DF_1_SYMINTPOSE 0x00800000 /* Object has individual interposers. */ +#define DF_1_GLOBAUDIT 0x01000000 /* Global auditin required. */ +#define DF_1_SINGLETON 0x02000000 /* Singleton symbols are used. */ + +/* Flags for the feature selection in DT_FEATURE_1. */ +#define DTF_1_PARINIT 0x00000001 +#define DTF_1_CONFEXP 0x00000002 + +/* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry. */ +#define DF_P1_LAZYLOAD 0x00000001 /* Lazyload following object. */ +#define DF_P1_GROUPPERM 0x00000002 /* Symbols from next object are not + generally available. */ /* Version definition sections. */ typedef struct { - Elf32_Half vd_version; /* Version revision */ - Elf32_Half vd_flags; /* Version information */ - Elf32_Half vd_ndx; /* Version Index */ - Elf32_Half vd_cnt; /* Number of associated aux entries */ - Elf32_Word vd_hash; /* Version name hash value */ - Elf32_Word vd_aux; /* Offset in bytes to verdaux array */ - Elf32_Word vd_next; /* Offset in bytes to next verdef - entry */ + Elf32_Half vd_version; /* Version revision */ + Elf32_Half vd_flags; /* Version information */ + Elf32_Half vd_ndx; /* Version Index */ + Elf32_Half vd_cnt; /* Number of associated aux entries */ + Elf32_Word vd_hash; /* Version name hash value */ + Elf32_Word vd_aux; /* Offset in bytes to verdaux array */ + Elf32_Word vd_next; /* Offset in bytes to next verdef + entry */ } Elf32_Verdef; typedef struct { - Elf64_Half vd_version; /* Version revision */ - Elf64_Half vd_flags; /* Version information */ - Elf64_Half vd_ndx; /* Version Index */ - Elf64_Half vd_cnt; /* Number of associated aux entries */ - Elf64_Word vd_hash; /* Version name hash value */ - Elf64_Word vd_aux; /* Offset in bytes to verdaux array */ - Elf64_Word vd_next; /* Offset in bytes to next verdef - entry */ + Elf64_Half vd_version; /* Version revision */ + Elf64_Half vd_flags; /* Version information */ + Elf64_Half vd_ndx; /* Version Index */ + Elf64_Half vd_cnt; /* Number of associated aux entries */ + Elf64_Word vd_hash; /* Version name hash value */ + Elf64_Word vd_aux; /* Offset in bytes to verdaux array */ + Elf64_Word vd_next; /* Offset in bytes to next verdef + entry */ } Elf64_Verdef; /* Legal values for vd_version (version revision). */ -#define VER_DEF_NONE 0 /* No version */ -#define VER_DEF_CURRENT 1 /* Current version */ -#define VER_DEF_NUM 2 /* Given version number */ +#define VER_DEF_NONE 0 /* No version */ +#define VER_DEF_CURRENT 1 /* Current version */ +#define VER_DEF_NUM 2 /* Given version number */ /* Legal values for vd_flags (version information flags). */ -#define VER_FLG_BASE 0x1 /* Version definition of file itself */ -#define VER_FLG_WEAK 0x2 /* Weak version identifier */ +#define VER_FLG_BASE 0x1 /* Version definition of file itself */ +#define VER_FLG_WEAK 0x2 /* Weak version identifier */ + +/* Versym symbol index values. */ +#define VER_NDX_LOCAL 0 /* Symbol is local. */ +#define VER_NDX_GLOBAL 1 /* Symbol is global. */ +#define VER_NDX_LORESERVE 0xff00 /* Beginning of reserved entries. */ +#define VER_NDX_ELIMINATE 0xff01 /* Symbol is to be eliminated. */ /* Auxialiary version information. */ typedef struct { - Elf32_Word vda_name; /* Version or dependency names */ - Elf32_Word vda_next; /* Offset in bytes to next verdaux - entry */ + Elf32_Word vda_name; /* Version or dependency names */ + Elf32_Word vda_next; /* Offset in bytes to next verdaux + entry */ } Elf32_Verdaux; typedef struct { - Elf64_Word vda_name; /* Version or dependency names */ - Elf64_Word vda_next; /* Offset in bytes to next verdaux - entry */ + Elf64_Word vda_name; /* Version or dependency names */ + Elf64_Word vda_next; /* Offset in bytes to next verdaux + entry */ } Elf64_Verdaux; @@ -757,57 +892,57 @@ typedef struct typedef struct { - Elf32_Half vn_version; /* Version of structure */ - Elf32_Half vn_cnt; /* Number of associated aux entries */ - Elf32_Word vn_file; /* Offset of filename for this - dependency */ - Elf32_Word vn_aux; /* Offset in bytes to vernaux array */ - Elf32_Word vn_next; /* Offset in bytes to next verneed - entry */ + Elf32_Half vn_version; /* Version of structure */ + Elf32_Half vn_cnt; /* Number of associated aux entries */ + Elf32_Word vn_file; /* Offset of filename for this + dependency */ + Elf32_Word vn_aux; /* Offset in bytes to vernaux array */ + Elf32_Word vn_next; /* Offset in bytes to next verneed + entry */ } Elf32_Verneed; typedef struct { - Elf64_Half vn_version; /* Version of structure */ - Elf64_Half vn_cnt; /* Number of associated aux entries */ - Elf64_Word vn_file; /* Offset of filename for this - dependency */ - Elf64_Word vn_aux; /* Offset in bytes to vernaux array */ - Elf64_Word vn_next; /* Offset in bytes to next verneed - entry */ + Elf64_Half vn_version; /* Version of structure */ + Elf64_Half vn_cnt; /* Number of associated aux entries */ + Elf64_Word vn_file; /* Offset of filename for this + dependency */ + Elf64_Word vn_aux; /* Offset in bytes to vernaux array */ + Elf64_Word vn_next; /* Offset in bytes to next verneed + entry */ } Elf64_Verneed; /* Legal values for vn_version (version revision). */ -#define VER_NEED_NONE 0 /* No version */ -#define VER_NEED_CURRENT 1 /* Current version */ -#define VER_NEED_NUM 2 /* Given version number */ +#define VER_NEED_NONE 0 /* No version */ +#define VER_NEED_CURRENT 1 /* Current version */ +#define VER_NEED_NUM 2 /* Given version number */ /* Auxiliary needed version information. */ typedef struct { - Elf32_Word vna_hash; /* Hash value of dependency name */ - Elf32_Half vna_flags; /* Dependency specific information */ - Elf32_Half vna_other; /* Unused */ - Elf32_Word vna_name; /* Dependency name string offset */ - Elf32_Word vna_next; /* Offset in bytes to next vernaux - entry */ + Elf32_Word vna_hash; /* Hash value of dependency name */ + Elf32_Half vna_flags; /* Dependency specific information */ + Elf32_Half vna_other; /* Unused */ + Elf32_Word vna_name; /* Dependency name string offset */ + Elf32_Word vna_next; /* Offset in bytes to next vernaux + entry */ } Elf32_Vernaux; typedef struct { - Elf64_Word vna_hash; /* Hash value of dependency name */ - Elf64_Half vna_flags; /* Dependency specific information */ - Elf64_Half vna_other; /* Unused */ - Elf64_Word vna_name; /* Dependency name string offset */ - Elf64_Word vna_next; /* Offset in bytes to next vernaux - entry */ + Elf64_Word vna_hash; /* Hash value of dependency name */ + Elf64_Half vna_flags; /* Dependency specific information */ + Elf64_Half vna_other; /* Unused */ + Elf64_Word vna_name; /* Dependency name string offset */ + Elf64_Word vna_next; /* Offset in bytes to next vernaux + entry */ } Elf64_Vernaux; /* Legal values for vna_flags. */ -#define VER_FLG_WEAK 0x2 /* Weak version identifier */ +#define VER_FLG_WEAK 0x2 /* Weak version identifier */ /* Auxiliary vector. */ @@ -821,84 +956,115 @@ typedef struct typedef struct { - int a_type; /* Entry type */ + uint32_t a_type; /* Entry type */ union { - long int a_val; /* Integer value */ - void *a_ptr; /* Pointer value */ - void (*a_fcn) (void); /* Function pointer value */ + uint32_t a_val; /* Integer value */ + /* We use to have pointer elements added here. We cannot do that, + though, since it does not work when using 32-bit definitions + on 64-bit platforms and vice versa. */ } a_un; } Elf32_auxv_t; typedef struct { - long int a_type; /* Entry type */ + uint64_t a_type; /* Entry type */ union { - long int a_val; /* Integer value */ - void *a_ptr; /* Pointer value */ - void (*a_fcn) (void); /* Function pointer value */ + uint64_t a_val; /* Integer value */ + /* We use to have pointer elements added here. We cannot do that, + though, since it does not work when using 32-bit definitions + on 64-bit platforms and vice versa. */ } a_un; } Elf64_auxv_t; /* Legal values for a_type (entry type). */ -#define AT_NULL 0 /* End of vector */ -#define AT_IGNORE 1 /* Entry should be ignored */ -#define AT_EXECFD 2 /* File descriptor of program */ -#define AT_PHDR 3 /* Program headers for program */ -#define AT_PHENT 4 /* Size of program header entry */ -#define AT_PHNUM 5 /* Number of program headers */ -#define AT_PAGESZ 6 /* System page size */ -#define AT_BASE 7 /* Base address of interpreter */ -#define AT_FLAGS 8 /* Flags */ -#define AT_ENTRY 9 /* Entry point of program */ -#define AT_NOTELF 10 /* Program is not ELF */ -#define AT_UID 11 /* Real uid */ -#define AT_EUID 12 /* Effective uid */ -#define AT_GID 13 /* Real gid */ -#define AT_EGID 14 /* Effective gid */ +#define AT_NULL 0 /* End of vector */ +#define AT_IGNORE 1 /* Entry should be ignored */ +#define AT_EXECFD 2 /* File descriptor of program */ +#define AT_PHDR 3 /* Program headers for program */ +#define AT_PHENT 4 /* Size of program header entry */ +#define AT_PHNUM 5 /* Number of program headers */ +#define AT_PAGESZ 6 /* System page size */ +#define AT_BASE 7 /* Base address of interpreter */ +#define AT_FLAGS 8 /* Flags */ +#define AT_ENTRY 9 /* Entry point of program */ +#define AT_NOTELF 10 /* Program is not ELF */ +#define AT_UID 11 /* Real uid */ +#define AT_EUID 12 /* Effective uid */ +#define AT_GID 13 /* Real gid */ +#define AT_EGID 14 /* Effective gid */ +#define AT_CLKTCK 17 /* Frequency of times() */ /* Some more special a_type values describing the hardware. */ -#define AT_PLATFORM 15 /* String identifying platform. */ -#define AT_HWCAP 16 /* Machine dependent hints about - processor capabilities. */ +#define AT_PLATFORM 15 /* String identifying platform. */ +#define AT_HWCAP 16 /* Machine dependent hints about + processor capabilities. */ /* This entry gives some information about the FPU initialization performed by the kernel. */ -#define AT_FPUCW 17 /* Used FPU control word. */ +#define AT_FPUCW 18 /* Used FPU control word. */ +/* Cache block sizes. */ +#define AT_DCACHEBSIZE 19 /* Data cache block size. */ +#define AT_ICACHEBSIZE 20 /* Instruction cache block size. */ +#define AT_UCACHEBSIZE 21 /* Unified cache block size. */ + +/* A special ignored value for PPC, used by the kernel to control the + interpretation of the AUXV. Must be > 16. */ +#define AT_IGNOREPPC 22 /* Entry should be ignored. */ + +#define AT_SECURE 23 /* Boolean, was exec setuid-like? */ + +#define AT_BASE_PLATFORM 24 /* String identifying real platforms.*/ + +#define AT_RANDOM 25 /* Address of 16 random bytes. */ + +#define AT_EXECFN 31 /* Filename of executable. */ + +/* Pointer to the global system page used for system calls and other + nice things. */ +#define AT_SYSINFO 32 +#define AT_SYSINFO_EHDR 33 + +/* Shapes of the caches. Bits 0-3 contains associativity; bits 4-7 contains + log2 of line size; mask those to get cache size. */ +#define AT_L1I_CACHESHAPE 34 +#define AT_L1D_CACHESHAPE 35 +#define AT_L2_CACHESHAPE 36 +#define AT_L3_CACHESHAPE 37 /* Note section contents. Each entry in the note section begins with a header of a fixed form. */ typedef struct { - Elf32_Word n_namesz; /* Length of the note's name. */ - Elf32_Word n_descsz; /* Length of the note's descriptor. */ - Elf32_Word n_type; /* Type of the note. */ + Elf32_Word n_namesz; /* Length of the note's name. */ + Elf32_Word n_descsz; /* Length of the note's descriptor. */ + Elf32_Word n_type; /* Type of the note. */ } Elf32_Nhdr; typedef struct { - Elf64_Word n_namesz; /* Length of the note's name. */ - Elf64_Word n_descsz; /* Length of the note's descriptor. */ - Elf64_Word n_type; /* Type of the note. */ + Elf64_Word n_namesz; /* Length of the note's name. */ + Elf64_Word n_descsz; /* Length of the note's descriptor. */ + Elf64_Word n_type; /* Type of the note. */ } Elf64_Nhdr; /* Known names of notes. */ /* Solaris entries in the note section have this name. */ -#define ELF_NOTE_SOLARIS "SUNW Solaris" +#define ELF_NOTE_SOLARIS "SUNW Solaris" /* Note entries for GNU systems have this name. */ -#define ELF_NOTE_GNU "GNU" +#define ELF_NOTE_GNU "GNU" /* Defined types of notes for Solaris. */ /* Value of descriptor (one word) is desired pagesize for the binary. */ -#define ELF_NOTE_PAGESIZE_HINT 1 +#define ELF_NOTE_PAGESIZE_HINT 1 /* Defined note types for GNU systems. */ @@ -909,141 +1075,1665 @@ typedef struct word 2: minor version of the ABI word 3: subminor version of the ABI */ -#define ELF_NOTE_ABI 1 +#define NT_GNU_ABI_TAG 1 +#define ELF_NOTE_ABI NT_GNU_ABI_TAG /* Old name. */ -/* Known OSes. These value can appear in word 0 of an ELF_NOTE_ABI - note section entry. */ -#define ELF_NOTE_OS_LINUX 0 -#define ELF_NOTE_OS_GNU 1 -#define ELF_NOTE_OS_SOLARIS2 2 +/* Known OSes. These values can appear in word 0 of an + NT_GNU_ABI_TAG note section entry. */ +#define ELF_NOTE_OS_LINUX 0 +#define ELF_NOTE_OS_GNU 1 +#define ELF_NOTE_OS_SOLARIS2 2 +#define ELF_NOTE_OS_FREEBSD 3 + +/* Synthetic hwcap information. The descriptor begins with two words: + word 0: number of entries + word 1: bitmask of enabled entries + Then follow variable-length entries, one byte followed by a + '\0'-terminated hwcap name string. The byte gives the bit + number to test if enabled, (1U << bit) & bitmask. */ +#define NT_GNU_HWCAP 2 + +/* Build ID bits as generated by ld --build-id. + The descriptor consists of any nonzero number of bytes. */ +#define NT_GNU_BUILD_ID 3 + +/* Version note generated by GNU gold containing a version string. */ +#define NT_GNU_GOLD_VERSION 4 + + +/* Move records. */ +typedef struct +{ + Elf32_Xword m_value; /* Symbol value. */ + Elf32_Word m_info; /* Size and index. */ + Elf32_Word m_poffset; /* Symbol offset. */ + Elf32_Half m_repeat; /* Repeat count. */ + Elf32_Half m_stride; /* Stride info. */ +} Elf32_Move; + +typedef struct +{ + Elf64_Xword m_value; /* Symbol value. */ + Elf64_Xword m_info; /* Size and index. */ + Elf64_Xword m_poffset; /* Symbol offset. */ + Elf64_Half m_repeat; /* Repeat count. */ + Elf64_Half m_stride; /* Stride info. */ +} Elf64_Move; + +/* Macro to construct move records. */ +#define ELF32_M_SYM(info) ((info) >> 8) +#define ELF32_M_SIZE(info) ((unsigned char) (info)) +#define ELF32_M_INFO(sym, size) (((sym) << 8) + (unsigned char) (size)) + +#define ELF64_M_SYM(info) ELF32_M_SYM (info) +#define ELF64_M_SIZE(info) ELF32_M_SIZE (info) +#define ELF64_M_INFO(sym, size) ELF32_M_INFO (sym, size) /* Motorola 68k specific definitions. */ +/* Values for Elf32_Ehdr.e_flags. */ +#define EF_CPU32 0x00810000 + /* m68k relocs. */ -#define R_68K_NONE 0 /* No reloc */ -#define R_68K_32 1 /* Direct 32 bit */ -#define R_68K_16 2 /* Direct 16 bit */ -#define R_68K_8 3 /* Direct 8 bit */ -#define R_68K_PC32 4 /* PC relative 32 bit */ -#define R_68K_PC16 5 /* PC relative 16 bit */ -#define R_68K_PC8 6 /* PC relative 8 bit */ -#define R_68K_GOT32 7 /* 32 bit PC relative GOT entry */ -#define R_68K_GOT16 8 /* 16 bit PC relative GOT entry */ -#define R_68K_GOT8 9 /* 8 bit PC relative GOT entry */ -#define R_68K_GOT32O 10 /* 32 bit GOT offset */ -#define R_68K_GOT16O 11 /* 16 bit GOT offset */ -#define R_68K_GOT8O 12 /* 8 bit GOT offset */ -#define R_68K_PLT32 13 /* 32 bit PC relative PLT address */ -#define R_68K_PLT16 14 /* 16 bit PC relative PLT address */ -#define R_68K_PLT8 15 /* 8 bit PC relative PLT address */ -#define R_68K_PLT32O 16 /* 32 bit PLT offset */ -#define R_68K_PLT16O 17 /* 16 bit PLT offset */ -#define R_68K_PLT8O 18 /* 8 bit PLT offset */ -#define R_68K_COPY 19 /* Copy symbol at runtime */ -#define R_68K_GLOB_DAT 20 /* Create GOT entry */ -#define R_68K_JMP_SLOT 21 /* Create PLT entry */ -#define R_68K_RELATIVE 22 /* Adjust by program base */ +#define R_68K_NONE 0 /* No reloc */ +#define R_68K_32 1 /* Direct 32 bit */ +#define R_68K_16 2 /* Direct 16 bit */ +#define R_68K_8 3 /* Direct 8 bit */ +#define R_68K_PC32 4 /* PC relative 32 bit */ +#define R_68K_PC16 5 /* PC relative 16 bit */ +#define R_68K_PC8 6 /* PC relative 8 bit */ +#define R_68K_GOT32 7 /* 32 bit PC relative GOT entry */ +#define R_68K_GOT16 8 /* 16 bit PC relative GOT entry */ +#define R_68K_GOT8 9 /* 8 bit PC relative GOT entry */ +#define R_68K_GOT32O 10 /* 32 bit GOT offset */ +#define R_68K_GOT16O 11 /* 16 bit GOT offset */ +#define R_68K_GOT8O 12 /* 8 bit GOT offset */ +#define R_68K_PLT32 13 /* 32 bit PC relative PLT address */ +#define R_68K_PLT16 14 /* 16 bit PC relative PLT address */ +#define R_68K_PLT8 15 /* 8 bit PC relative PLT address */ +#define R_68K_PLT32O 16 /* 32 bit PLT offset */ +#define R_68K_PLT16O 17 /* 16 bit PLT offset */ +#define R_68K_PLT8O 18 /* 8 bit PLT offset */ +#define R_68K_COPY 19 /* Copy symbol at runtime */ +#define R_68K_GLOB_DAT 20 /* Create GOT entry */ +#define R_68K_JMP_SLOT 21 /* Create PLT entry */ +#define R_68K_RELATIVE 22 /* Adjust by program base */ +#define R_68K_TLS_GD32 25 /* 32 bit GOT offset for GD */ +#define R_68K_TLS_GD16 26 /* 16 bit GOT offset for GD */ +#define R_68K_TLS_GD8 27 /* 8 bit GOT offset for GD */ +#define R_68K_TLS_LDM32 28 /* 32 bit GOT offset for LDM */ +#define R_68K_TLS_LDM16 29 /* 16 bit GOT offset for LDM */ +#define R_68K_TLS_LDM8 30 /* 8 bit GOT offset for LDM */ +#define R_68K_TLS_LDO32 31 /* 32 bit module-relative offset */ +#define R_68K_TLS_LDO16 32 /* 16 bit module-relative offset */ +#define R_68K_TLS_LDO8 33 /* 8 bit module-relative offset */ +#define R_68K_TLS_IE32 34 /* 32 bit GOT offset for IE */ +#define R_68K_TLS_IE16 35 /* 16 bit GOT offset for IE */ +#define R_68K_TLS_IE8 36 /* 8 bit GOT offset for IE */ +#define R_68K_TLS_LE32 37 /* 32 bit offset relative to + static TLS block */ +#define R_68K_TLS_LE16 38 /* 16 bit offset relative to + static TLS block */ +#define R_68K_TLS_LE8 39 /* 8 bit offset relative to + static TLS block */ +#define R_68K_TLS_DTPMOD32 40 /* 32 bit module number */ +#define R_68K_TLS_DTPREL32 41 /* 32 bit module-relative offset */ +#define R_68K_TLS_TPREL32 42 /* 32 bit TP-relative offset */ /* Keep this the last entry. */ -#define R_68K_NUM 23 +#define R_68K_NUM 43 /* Intel 80386 specific definitions. */ /* i386 relocs. */ -#define R_386_NONE 0 /* No reloc */ -#define R_386_32 1 /* Direct 32 bit */ -#define R_386_PC32 2 /* PC relative 32 bit */ -#define R_386_GOT32 3 /* 32 bit GOT entry */ -#define R_386_PLT32 4 /* 32 bit PLT address */ -#define R_386_COPY 5 /* Copy symbol at runtime */ -#define R_386_GLOB_DAT 6 /* Create GOT entry */ -#define R_386_JMP_SLOT 7 /* Create PLT entry */ -#define R_386_RELATIVE 8 /* Adjust by program base */ -#define R_386_GOTOFF 9 /* 32 bit offset to GOT */ -#define R_386_GOTPC 10 /* 32 bit PC relative offset to GOT */ +#define R_386_NONE 0 /* No reloc */ +#define R_386_32 1 /* Direct 32 bit */ +#define R_386_PC32 2 /* PC relative 32 bit */ +#define R_386_GOT32 3 /* 32 bit GOT entry */ +#define R_386_PLT32 4 /* 32 bit PLT address */ +#define R_386_COPY 5 /* Copy symbol at runtime */ +#define R_386_GLOB_DAT 6 /* Create GOT entry */ +#define R_386_JMP_SLOT 7 /* Create PLT entry */ +#define R_386_RELATIVE 8 /* Adjust by program base */ +#define R_386_GOTOFF 9 /* 32 bit offset to GOT */ +#define R_386_GOTPC 10 /* 32 bit PC relative offset to GOT */ +#define R_386_32PLT 11 +#define R_386_TLS_TPOFF 14 /* Offset in static TLS block */ +#define R_386_TLS_IE 15 /* Address of GOT entry for static TLS + block offset */ +#define R_386_TLS_GOTIE 16 /* GOT entry for static TLS block + offset */ +#define R_386_TLS_LE 17 /* Offset relative to static TLS + block */ +#define R_386_TLS_GD 18 /* Direct 32 bit for GNU version of + general dynamic thread local data */ +#define R_386_TLS_LDM 19 /* Direct 32 bit for GNU version of + local dynamic thread local data + in LE code */ +#define R_386_16 20 +#define R_386_PC16 21 +#define R_386_8 22 +#define R_386_PC8 23 +#define R_386_TLS_GD_32 24 /* Direct 32 bit for general dynamic + thread local data */ +#define R_386_TLS_GD_PUSH 25 /* Tag for pushl in GD TLS code */ +#define R_386_TLS_GD_CALL 26 /* Relocation for call to + __tls_get_addr() */ +#define R_386_TLS_GD_POP 27 /* Tag for popl in GD TLS code */ +#define R_386_TLS_LDM_32 28 /* Direct 32 bit for local dynamic + thread local data in LE code */ +#define R_386_TLS_LDM_PUSH 29 /* Tag for pushl in LDM TLS code */ +#define R_386_TLS_LDM_CALL 30 /* Relocation for call to + __tls_get_addr() in LDM code */ +#define R_386_TLS_LDM_POP 31 /* Tag for popl in LDM TLS code */ +#define R_386_TLS_LDO_32 32 /* Offset relative to TLS block */ +#define R_386_TLS_IE_32 33 /* GOT entry for negated static TLS + block offset */ +#define R_386_TLS_LE_32 34 /* Negated offset relative to static + TLS block */ +#define R_386_TLS_DTPMOD32 35 /* ID of module containing symbol */ +#define R_386_TLS_DTPOFF32 36 /* Offset in TLS block */ +#define R_386_TLS_TPOFF32 37 /* Negated offset in static TLS block */ +/* 38? */ +#define R_386_TLS_GOTDESC 39 /* GOT offset for TLS descriptor. */ +#define R_386_TLS_DESC_CALL 40 /* Marker of call through TLS + descriptor for + relaxation. */ +#define R_386_TLS_DESC 41 /* TLS descriptor containing + pointer to code and to + argument, returning the TLS + offset for the symbol. */ +#define R_386_IRELATIVE 42 /* Adjust indirectly by program base */ /* Keep this the last entry. */ -#define R_386_NUM 11 - -/* TCC-specific 16-bit relocs. */ -#define R_386_16 12 /* Direct 16 bit */ -#define R_386_PC16 13 /* PC relative 16 bit */ +#define R_386_NUM 43 /* SUN SPARC specific definitions. */ +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_SPARC_REGISTER 13 /* Global register reserved to app. */ + /* Values for Elf64_Ehdr.e_flags. */ -#define EF_SPARCV9_MM 3 -#define EF_SPARCV9_TSO 0 -#define EF_SPARCV9_PSO 1 -#define EF_SPARCV9_RMO 2 -#define EF_SPARC_EXT_MASK 0xFFFF00 -#define EF_SPARC_SUN_US1 0x000200 -#define EF_SPARC_HAL_R1 0x000400 +#define EF_SPARCV9_MM 3 +#define EF_SPARCV9_TSO 0 +#define EF_SPARCV9_PSO 1 +#define EF_SPARCV9_RMO 2 +#define EF_SPARC_LEDATA 0x800000 /* little endian data */ +#define EF_SPARC_EXT_MASK 0xFFFF00 +#define EF_SPARC_32PLUS 0x000100 /* generic V8+ features */ +#define EF_SPARC_SUN_US1 0x000200 /* Sun UltraSPARC1 extensions */ +#define EF_SPARC_HAL_R1 0x000400 /* HAL R1 extensions */ +#define EF_SPARC_SUN_US3 0x000800 /* Sun UltraSPARCIII extensions */ /* SPARC relocs. */ -#define R_SPARC_NONE 0 /* No reloc */ -#define R_SPARC_8 1 /* Direct 8 bit */ -#define R_SPARC_16 2 /* Direct 16 bit */ -#define R_SPARC_32 3 /* Direct 32 bit */ -#define R_SPARC_DISP8 4 /* PC relative 8 bit */ -#define R_SPARC_DISP16 5 /* PC relative 16 bit */ -#define R_SPARC_DISP32 6 /* PC relative 32 bit */ -#define R_SPARC_WDISP30 7 /* PC relative 30 bit shifted */ -#define R_SPARC_WDISP22 8 /* PC relative 22 bit shifted */ -#define R_SPARC_HI22 9 /* High 22 bit */ -#define R_SPARC_22 10 /* Direct 22 bit */ -#define R_SPARC_13 11 /* Direct 13 bit */ -#define R_SPARC_LO10 12 /* Truncated 10 bit */ -#define R_SPARC_GOT10 13 /* Truncated 10 bit GOT entry */ -#define R_SPARC_GOT13 14 /* 13 bit GOT entry */ -#define R_SPARC_GOT22 15 /* 22 bit GOT entry shifted */ -#define R_SPARC_PC10 16 /* PC relative 10 bit truncated */ -#define R_SPARC_PC22 17 /* PC relative 22 bit shifted */ -#define R_SPARC_WPLT30 18 /* 30 bit PC relative PLT address */ -#define R_SPARC_COPY 19 /* Copy symbol at runtime */ -#define R_SPARC_GLOB_DAT 20 /* Create GOT entry */ -#define R_SPARC_JMP_SLOT 21 /* Create PLT entry */ -#define R_SPARC_RELATIVE 22 /* Adjust by program base */ -#define R_SPARC_UA32 23 /* Direct 32 bit unaligned */ +#define R_SPARC_NONE 0 /* No reloc */ +#define R_SPARC_8 1 /* Direct 8 bit */ +#define R_SPARC_16 2 /* Direct 16 bit */ +#define R_SPARC_32 3 /* Direct 32 bit */ +#define R_SPARC_DISP8 4 /* PC relative 8 bit */ +#define R_SPARC_DISP16 5 /* PC relative 16 bit */ +#define R_SPARC_DISP32 6 /* PC relative 32 bit */ +#define R_SPARC_WDISP30 7 /* PC relative 30 bit shifted */ +#define R_SPARC_WDISP22 8 /* PC relative 22 bit shifted */ +#define R_SPARC_HI22 9 /* High 22 bit */ +#define R_SPARC_22 10 /* Direct 22 bit */ +#define R_SPARC_13 11 /* Direct 13 bit */ +#define R_SPARC_LO10 12 /* Truncated 10 bit */ +#define R_SPARC_GOT10 13 /* Truncated 10 bit GOT entry */ +#define R_SPARC_GOT13 14 /* 13 bit GOT entry */ +#define R_SPARC_GOT22 15 /* 22 bit GOT entry shifted */ +#define R_SPARC_PC10 16 /* PC relative 10 bit truncated */ +#define R_SPARC_PC22 17 /* PC relative 22 bit shifted */ +#define R_SPARC_WPLT30 18 /* 30 bit PC relative PLT address */ +#define R_SPARC_COPY 19 /* Copy symbol at runtime */ +#define R_SPARC_GLOB_DAT 20 /* Create GOT entry */ +#define R_SPARC_JMP_SLOT 21 /* Create PLT entry */ +#define R_SPARC_RELATIVE 22 /* Adjust by program base */ +#define R_SPARC_UA32 23 /* Direct 32 bit unaligned */ /* Additional Sparc64 relocs. */ -#define R_SPARC_PLT32 24 /* Direct 32 bit ref to PLT entry */ -#define R_SPARC_HIPLT22 25 /* High 22 bit PLT entry */ -#define R_SPARC_LOPLT10 26 /* Truncated 10 bit PLT entry */ -#define R_SPARC_PCPLT32 27 /* PC rel 32 bit ref to PLT entry */ -#define R_SPARC_PCPLT22 28 /* PC rel high 22 bit PLT entry */ -#define R_SPARC_PCPLT10 29 /* PC rel trunc 10 bit PLT entry */ -#define R_SPARC_10 30 /* Direct 10 bit */ -#define R_SPARC_11 31 /* Direct 11 bit */ -#define R_SPARC_64 32 /* Direct 64 bit */ -#define R_SPARC_OLO10 33 /* ?? */ -#define R_SPARC_HH22 34 /* Top 22 bits of direct 64 bit */ -#define R_SPARC_HM10 35 /* High middle 10 bits of ... */ -#define R_SPARC_LM22 36 /* Low middle 22 bits of ... */ -#define R_SPARC_PC_HH22 37 /* Top 22 bits of pc rel 64 bit */ -#define R_SPARC_PC_HM10 38 /* High middle 10 bit of ... */ -#define R_SPARC_PC_LM22 39 /* Low miggle 22 bits of ... */ -#define R_SPARC_WDISP16 40 /* PC relative 16 bit shifted */ -#define R_SPARC_WDISP19 41 /* PC relative 19 bit shifted */ -#define R_SPARC_7 43 /* Direct 7 bit */ -#define R_SPARC_5 44 /* Direct 5 bit */ -#define R_SPARC_6 45 /* Direct 6 bit */ -#define R_SPARC_DISP64 46 /* PC relative 64 bit */ -#define R_SPARC_PLT64 47 /* Direct 64 bit ref to PLT entry */ -#define R_SPARC_HIX22 48 /* High 22 bit complemented */ -#define R_SPARC_LOX10 49 /* Truncated 11 bit complemented */ -#define R_SPARC_H44 50 /* Direct high 12 of 44 bit */ -#define R_SPARC_M44 51 /* Direct mid 22 of 44 bit */ -#define R_SPARC_L44 52 /* Direct low 10 of 44 bit */ -#define R_SPARC_REGISTER 53 /* Global register usage */ -#define R_SPARC_UA64 54 /* Direct 64 bit unaligned */ -#define R_SPARC_UA16 55 /* Direct 16 bit unaligned */ +#define R_SPARC_PLT32 24 /* Direct 32 bit ref to PLT entry */ +#define R_SPARC_HIPLT22 25 /* High 22 bit PLT entry */ +#define R_SPARC_LOPLT10 26 /* Truncated 10 bit PLT entry */ +#define R_SPARC_PCPLT32 27 /* PC rel 32 bit ref to PLT entry */ +#define R_SPARC_PCPLT22 28 /* PC rel high 22 bit PLT entry */ +#define R_SPARC_PCPLT10 29 /* PC rel trunc 10 bit PLT entry */ +#define R_SPARC_10 30 /* Direct 10 bit */ +#define R_SPARC_11 31 /* Direct 11 bit */ +#define R_SPARC_64 32 /* Direct 64 bit */ +#define R_SPARC_OLO10 33 /* 10bit with secondary 13bit addend */ +#define R_SPARC_HH22 34 /* Top 22 bits of direct 64 bit */ +#define R_SPARC_HM10 35 /* High middle 10 bits of ... */ +#define R_SPARC_LM22 36 /* Low middle 22 bits of ... */ +#define R_SPARC_PC_HH22 37 /* Top 22 bits of pc rel 64 bit */ +#define R_SPARC_PC_HM10 38 /* High middle 10 bit of ... */ +#define R_SPARC_PC_LM22 39 /* Low miggle 22 bits of ... */ +#define R_SPARC_WDISP16 40 /* PC relative 16 bit shifted */ +#define R_SPARC_WDISP19 41 /* PC relative 19 bit shifted */ +#define R_SPARC_GLOB_JMP 42 /* was part of v9 ABI but was removed */ +#define R_SPARC_7 43 /* Direct 7 bit */ +#define R_SPARC_5 44 /* Direct 5 bit */ +#define R_SPARC_6 45 /* Direct 6 bit */ +#define R_SPARC_DISP64 46 /* PC relative 64 bit */ +#define R_SPARC_PLT64 47 /* Direct 64 bit ref to PLT entry */ +#define R_SPARC_HIX22 48 /* High 22 bit complemented */ +#define R_SPARC_LOX10 49 /* Truncated 11 bit complemented */ +#define R_SPARC_H44 50 /* Direct high 12 of 44 bit */ +#define R_SPARC_M44 51 /* Direct mid 22 of 44 bit */ +#define R_SPARC_L44 52 /* Direct low 10 of 44 bit */ +#define R_SPARC_REGISTER 53 /* Global register usage */ +#define R_SPARC_UA64 54 /* Direct 64 bit unaligned */ +#define R_SPARC_UA16 55 /* Direct 16 bit unaligned */ +#define R_SPARC_TLS_GD_HI22 56 +#define R_SPARC_TLS_GD_LO10 57 +#define R_SPARC_TLS_GD_ADD 58 +#define R_SPARC_TLS_GD_CALL 59 +#define R_SPARC_TLS_LDM_HI22 60 +#define R_SPARC_TLS_LDM_LO10 61 +#define R_SPARC_TLS_LDM_ADD 62 +#define R_SPARC_TLS_LDM_CALL 63 +#define R_SPARC_TLS_LDO_HIX22 64 +#define R_SPARC_TLS_LDO_LOX10 65 +#define R_SPARC_TLS_LDO_ADD 66 +#define R_SPARC_TLS_IE_HI22 67 +#define R_SPARC_TLS_IE_LO10 68 +#define R_SPARC_TLS_IE_LD 69 +#define R_SPARC_TLS_IE_LDX 70 +#define R_SPARC_TLS_IE_ADD 71 +#define R_SPARC_TLS_LE_HIX22 72 +#define R_SPARC_TLS_LE_LOX10 73 +#define R_SPARC_TLS_DTPMOD32 74 +#define R_SPARC_TLS_DTPMOD64 75 +#define R_SPARC_TLS_DTPOFF32 76 +#define R_SPARC_TLS_DTPOFF64 77 +#define R_SPARC_TLS_TPOFF32 78 +#define R_SPARC_TLS_TPOFF64 79 +#define R_SPARC_GOTDATA_HIX22 80 +#define R_SPARC_GOTDATA_LOX10 81 +#define R_SPARC_GOTDATA_OP_HIX22 82 +#define R_SPARC_GOTDATA_OP_LOX10 83 +#define R_SPARC_GOTDATA_OP 84 +#define R_SPARC_H34 85 +#define R_SPARC_SIZE32 86 +#define R_SPARC_SIZE64 87 +#define R_SPARC_WDISP10 88 +#define R_SPARC_JMP_IREL 248 +#define R_SPARC_IRELATIVE 249 +#define R_SPARC_GNU_VTINHERIT 250 +#define R_SPARC_GNU_VTENTRY 251 +#define R_SPARC_REV32 252 /* Keep this the last entry. */ -#define R_SPARC_NUM 56 +#define R_SPARC_NUM 253 + +/* For Sparc64, legal values for d_tag of Elf64_Dyn. */ + +#define DT_SPARC_REGISTER 0x70000001 +#define DT_SPARC_NUM 2 + +/* MIPS R3000 specific definitions. */ + +/* Legal values for e_flags field of Elf32_Ehdr. */ + +#define EF_MIPS_NOREORDER 1 /* A .noreorder directive was used */ +#define EF_MIPS_PIC 2 /* Contains PIC code */ +#define EF_MIPS_CPIC 4 /* Uses PIC calling sequence */ +#define EF_MIPS_XGOT 8 +#define EF_MIPS_64BIT_WHIRL 16 +#define EF_MIPS_ABI2 32 +#define EF_MIPS_ABI_ON32 64 +#define EF_MIPS_ARCH 0xf0000000 /* MIPS architecture level */ + +/* Legal values for MIPS architecture level. */ + +#define EF_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ +#define EF_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ +#define EF_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ +#define EF_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ +#define EF_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ +#define EF_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */ +#define EF_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */ + +/* The following are non-official names and should not be used. */ + +#define E_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ +#define E_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ +#define E_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ +#define E_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ +#define E_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ +#define E_MIPS_ARCH_32 0x60000000 /* MIPS32 code. */ +#define E_MIPS_ARCH_64 0x70000000 /* MIPS64 code. */ + +/* Special section indices. */ + +#define SHN_MIPS_ACOMMON 0xff00 /* Allocated common symbols */ +#define SHN_MIPS_TEXT 0xff01 /* Allocated test symbols. */ +#define SHN_MIPS_DATA 0xff02 /* Allocated data symbols. */ +#define SHN_MIPS_SCOMMON 0xff03 /* Small common symbols */ +#define SHN_MIPS_SUNDEFINED 0xff04 /* Small undefined symbols */ + +/* Legal values for sh_type field of Elf32_Shdr. */ + +#define SHT_MIPS_LIBLIST 0x70000000 /* Shared objects used in link */ +#define SHT_MIPS_MSYM 0x70000001 +#define SHT_MIPS_CONFLICT 0x70000002 /* Conflicting symbols */ +#define SHT_MIPS_GPTAB 0x70000003 /* Global data area sizes */ +#define SHT_MIPS_UCODE 0x70000004 /* Reserved for SGI/MIPS compilers */ +#define SHT_MIPS_DEBUG 0x70000005 /* MIPS ECOFF debugging information*/ +#define SHT_MIPS_REGINFO 0x70000006 /* Register usage information */ +#define SHT_MIPS_PACKAGE 0x70000007 +#define SHT_MIPS_PACKSYM 0x70000008 +#define SHT_MIPS_RELD 0x70000009 +#define SHT_MIPS_IFACE 0x7000000b +#define SHT_MIPS_CONTENT 0x7000000c +#define SHT_MIPS_OPTIONS 0x7000000d /* Miscellaneous options. */ +#define SHT_MIPS_SHDR 0x70000010 +#define SHT_MIPS_FDESC 0x70000011 +#define SHT_MIPS_EXTSYM 0x70000012 +#define SHT_MIPS_DENSE 0x70000013 +#define SHT_MIPS_PDESC 0x70000014 +#define SHT_MIPS_LOCSYM 0x70000015 +#define SHT_MIPS_AUXSYM 0x70000016 +#define SHT_MIPS_OPTSYM 0x70000017 +#define SHT_MIPS_LOCSTR 0x70000018 +#define SHT_MIPS_LINE 0x70000019 +#define SHT_MIPS_RFDESC 0x7000001a +#define SHT_MIPS_DELTASYM 0x7000001b +#define SHT_MIPS_DELTAINST 0x7000001c +#define SHT_MIPS_DELTACLASS 0x7000001d +#define SHT_MIPS_DWARF 0x7000001e /* DWARF debugging information. */ +#define SHT_MIPS_DELTADECL 0x7000001f +#define SHT_MIPS_SYMBOL_LIB 0x70000020 +#define SHT_MIPS_EVENTS 0x70000021 /* Event section. */ +#define SHT_MIPS_TRANSLATE 0x70000022 +#define SHT_MIPS_PIXIE 0x70000023 +#define SHT_MIPS_XLATE 0x70000024 +#define SHT_MIPS_XLATE_DEBUG 0x70000025 +#define SHT_MIPS_WHIRL 0x70000026 +#define SHT_MIPS_EH_REGION 0x70000027 +#define SHT_MIPS_XLATE_OLD 0x70000028 +#define SHT_MIPS_PDR_EXCEPTION 0x70000029 + +/* Legal values for sh_flags field of Elf32_Shdr. */ + +#define SHF_MIPS_GPREL 0x10000000 /* Must be part of global data area */ +#define SHF_MIPS_MERGE 0x20000000 +#define SHF_MIPS_ADDR 0x40000000 +#define SHF_MIPS_STRINGS 0x80000000 +#define SHF_MIPS_NOSTRIP 0x08000000 +#define SHF_MIPS_LOCAL 0x04000000 +#define SHF_MIPS_NAMES 0x02000000 +#define SHF_MIPS_NODUPE 0x01000000 + + +/* Symbol tables. */ + +/* MIPS specific values for `st_other'. */ +#define STO_MIPS_DEFAULT 0x0 +#define STO_MIPS_INTERNAL 0x1 +#define STO_MIPS_HIDDEN 0x2 +#define STO_MIPS_PROTECTED 0x3 +#define STO_MIPS_PLT 0x8 +#define STO_MIPS_SC_ALIGN_UNUSED 0xff + +/* MIPS specific values for `st_info'. */ +#define STB_MIPS_SPLIT_COMMON 13 + +/* Entries found in sections of type SHT_MIPS_GPTAB. */ + +typedef union +{ + struct + { + Elf32_Word gt_current_g_value; /* -G value used for compilation */ + Elf32_Word gt_unused; /* Not used */ + } gt_header; /* First entry in section */ + struct + { + Elf32_Word gt_g_value; /* If this value were used for -G */ + Elf32_Word gt_bytes; /* This many bytes would be used */ + } gt_entry; /* Subsequent entries in section */ +} Elf32_gptab; + +/* Entry found in sections of type SHT_MIPS_REGINFO. */ + +typedef struct +{ + Elf32_Word ri_gprmask; /* General registers used */ + Elf32_Word ri_cprmask[4]; /* Coprocessor registers used */ + Elf32_Sword ri_gp_value; /* $gp register value */ +} Elf32_RegInfo; + +/* Entries found in sections of type SHT_MIPS_OPTIONS. */ + +typedef struct +{ + unsigned char kind; /* Determines interpretation of the + variable part of descriptor. */ + unsigned char size; /* Size of descriptor, including header. */ + Elf32_Section section; /* Section header index of section affected, + 0 for global options. */ + Elf32_Word info; /* Kind-specific information. */ +} Elf_Options; + +/* Values for `kind' field in Elf_Options. */ + +#define ODK_NULL 0 /* Undefined. */ +#define ODK_REGINFO 1 /* Register usage information. */ +#define ODK_EXCEPTIONS 2 /* Exception processing options. */ +#define ODK_PAD 3 /* Section padding options. */ +#define ODK_HWPATCH 4 /* Hardware workarounds performed */ +#define ODK_FILL 5 /* record the fill value used by the linker. */ +#define ODK_TAGS 6 /* reserve space for desktop tools to write. */ +#define ODK_HWAND 7 /* HW workarounds. 'AND' bits when merging. */ +#define ODK_HWOR 8 /* HW workarounds. 'OR' bits when merging. */ + +/* Values for `info' in Elf_Options for ODK_EXCEPTIONS entries. */ + +#define OEX_FPU_MIN 0x1f /* FPE's which MUST be enabled. */ +#define OEX_FPU_MAX 0x1f00 /* FPE's which MAY be enabled. */ +#define OEX_PAGE0 0x10000 /* page zero must be mapped. */ +#define OEX_SMM 0x20000 /* Force sequential memory mode? */ +#define OEX_FPDBUG 0x40000 /* Force floating point debug mode? */ +#define OEX_PRECISEFP OEX_FPDBUG +#define OEX_DISMISS 0x80000 /* Dismiss invalid address faults? */ + +#define OEX_FPU_INVAL 0x10 +#define OEX_FPU_DIV0 0x08 +#define OEX_FPU_OFLO 0x04 +#define OEX_FPU_UFLO 0x02 +#define OEX_FPU_INEX 0x01 + +/* Masks for `info' in Elf_Options for an ODK_HWPATCH entry. */ + +#define OHW_R4KEOP 0x1 /* R4000 end-of-page patch. */ +#define OHW_R8KPFETCH 0x2 /* may need R8000 prefetch patch. */ +#define OHW_R5KEOP 0x4 /* R5000 end-of-page patch. */ +#define OHW_R5KCVTL 0x8 /* R5000 cvt.[ds].l bug. clean=1. */ + +#define OPAD_PREFIX 0x1 +#define OPAD_POSTFIX 0x2 +#define OPAD_SYMBOL 0x4 + +/* Entry found in `.options' section. */ + +typedef struct +{ + Elf32_Word hwp_flags1; /* Extra flags. */ + Elf32_Word hwp_flags2; /* Extra flags. */ +} Elf_Options_Hw; + +/* Masks for `info' in ElfOptions for ODK_HWAND and ODK_HWOR entries. */ + +#define OHWA0_R4KEOP_CHECKED 0x00000001 +#define OHWA1_R4KEOP_CLEAN 0x00000002 + +/* MIPS relocs. */ + +#define R_MIPS_NONE 0 /* No reloc */ +#define R_MIPS_16 1 /* Direct 16 bit */ +#define R_MIPS_32 2 /* Direct 32 bit */ +#define R_MIPS_REL32 3 /* PC relative 32 bit */ +#define R_MIPS_26 4 /* Direct 26 bit shifted */ +#define R_MIPS_HI16 5 /* High 16 bit */ +#define R_MIPS_LO16 6 /* Low 16 bit */ +#define R_MIPS_GPREL16 7 /* GP relative 16 bit */ +#define R_MIPS_LITERAL 8 /* 16 bit literal entry */ +#define R_MIPS_GOT16 9 /* 16 bit GOT entry */ +#define R_MIPS_PC16 10 /* PC relative 16 bit */ +#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */ +#define R_MIPS_GPREL32 12 /* GP relative 32 bit */ + +#define R_MIPS_SHIFT5 16 +#define R_MIPS_SHIFT6 17 +#define R_MIPS_64 18 +#define R_MIPS_GOT_DISP 19 +#define R_MIPS_GOT_PAGE 20 +#define R_MIPS_GOT_OFST 21 +#define R_MIPS_GOT_HI16 22 +#define R_MIPS_GOT_LO16 23 +#define R_MIPS_SUB 24 +#define R_MIPS_INSERT_A 25 +#define R_MIPS_INSERT_B 26 +#define R_MIPS_DELETE 27 +#define R_MIPS_HIGHER 28 +#define R_MIPS_HIGHEST 29 +#define R_MIPS_CALL_HI16 30 +#define R_MIPS_CALL_LO16 31 +#define R_MIPS_SCN_DISP 32 +#define R_MIPS_REL16 33 +#define R_MIPS_ADD_IMMEDIATE 34 +#define R_MIPS_PJUMP 35 +#define R_MIPS_RELGOT 36 +#define R_MIPS_JALR 37 +#define R_MIPS_TLS_DTPMOD32 38 /* Module number 32 bit */ +#define R_MIPS_TLS_DTPREL32 39 /* Module-relative offset 32 bit */ +#define R_MIPS_TLS_DTPMOD64 40 /* Module number 64 bit */ +#define R_MIPS_TLS_DTPREL64 41 /* Module-relative offset 64 bit */ +#define R_MIPS_TLS_GD 42 /* 16 bit GOT offset for GD */ +#define R_MIPS_TLS_LDM 43 /* 16 bit GOT offset for LDM */ +#define R_MIPS_TLS_DTPREL_HI16 44 /* Module-relative offset, high 16 bits */ +#define R_MIPS_TLS_DTPREL_LO16 45 /* Module-relative offset, low 16 bits */ +#define R_MIPS_TLS_GOTTPREL 46 /* 16 bit GOT offset for IE */ +#define R_MIPS_TLS_TPREL32 47 /* TP-relative offset, 32 bit */ +#define R_MIPS_TLS_TPREL64 48 /* TP-relative offset, 64 bit */ +#define R_MIPS_TLS_TPREL_HI16 49 /* TP-relative offset, high 16 bits */ +#define R_MIPS_TLS_TPREL_LO16 50 /* TP-relative offset, low 16 bits */ +#define R_MIPS_GLOB_DAT 51 +#define R_MIPS_COPY 126 +#define R_MIPS_JUMP_SLOT 127 +/* Keep this the last entry. */ +#define R_MIPS_NUM 128 + +/* Legal values for p_type field of Elf32_Phdr. */ + +#define PT_MIPS_REGINFO 0x70000000 /* Register usage information */ +#define PT_MIPS_RTPROC 0x70000001 /* Runtime procedure table. */ +#define PT_MIPS_OPTIONS 0x70000002 + +/* Special program header types. */ + +#define PF_MIPS_LOCAL 0x10000000 + +/* Legal values for d_tag field of Elf32_Dyn. */ + +#define DT_MIPS_RLD_VERSION 0x70000001 /* Runtime linker interface version */ +#define DT_MIPS_TIME_STAMP 0x70000002 /* Timestamp */ +#define DT_MIPS_ICHECKSUM 0x70000003 /* Checksum */ +#define DT_MIPS_IVERSION 0x70000004 /* Version string (string tbl index) */ +#define DT_MIPS_FLAGS 0x70000005 /* Flags */ +#define DT_MIPS_BASE_ADDRESS 0x70000006 /* Base address */ +#define DT_MIPS_MSYM 0x70000007 +#define DT_MIPS_CONFLICT 0x70000008 /* Address of CONFLICT section */ +#define DT_MIPS_LIBLIST 0x70000009 /* Address of LIBLIST section */ +#define DT_MIPS_LOCAL_GOTNO 0x7000000a /* Number of local GOT entries */ +#define DT_MIPS_CONFLICTNO 0x7000000b /* Number of CONFLICT entries */ +#define DT_MIPS_LIBLISTNO 0x70000010 /* Number of LIBLIST entries */ +#define DT_MIPS_SYMTABNO 0x70000011 /* Number of DYNSYM entries */ +#define DT_MIPS_UNREFEXTNO 0x70000012 /* First external DYNSYM */ +#define DT_MIPS_GOTSYM 0x70000013 /* First GOT entry in DYNSYM */ +#define DT_MIPS_HIPAGENO 0x70000014 /* Number of GOT page table entries */ +#define DT_MIPS_RLD_MAP 0x70000016 /* Address of run time loader map. */ +#define DT_MIPS_DELTA_CLASS 0x70000017 /* Delta C++ class definition. */ +#define DT_MIPS_DELTA_CLASS_NO 0x70000018 /* Number of entries in + DT_MIPS_DELTA_CLASS. */ +#define DT_MIPS_DELTA_INSTANCE 0x70000019 /* Delta C++ class instances. */ +#define DT_MIPS_DELTA_INSTANCE_NO 0x7000001a /* Number of entries in + DT_MIPS_DELTA_INSTANCE. */ +#define DT_MIPS_DELTA_RELOC 0x7000001b /* Delta relocations. */ +#define DT_MIPS_DELTA_RELOC_NO 0x7000001c /* Number of entries in + DT_MIPS_DELTA_RELOC. */ +#define DT_MIPS_DELTA_SYM 0x7000001d /* Delta symbols that Delta + relocations refer to. */ +#define DT_MIPS_DELTA_SYM_NO 0x7000001e /* Number of entries in + DT_MIPS_DELTA_SYM. */ +#define DT_MIPS_DELTA_CLASSSYM 0x70000020 /* Delta symbols that hold the + class declaration. */ +#define DT_MIPS_DELTA_CLASSSYM_NO 0x70000021 /* Number of entries in + DT_MIPS_DELTA_CLASSSYM. */ +#define DT_MIPS_CXX_FLAGS 0x70000022 /* Flags indicating for C++ flavor. */ +#define DT_MIPS_PIXIE_INIT 0x70000023 +#define DT_MIPS_SYMBOL_LIB 0x70000024 +#define DT_MIPS_LOCALPAGE_GOTIDX 0x70000025 +#define DT_MIPS_LOCAL_GOTIDX 0x70000026 +#define DT_MIPS_HIDDEN_GOTIDX 0x70000027 +#define DT_MIPS_PROTECTED_GOTIDX 0x70000028 +#define DT_MIPS_OPTIONS 0x70000029 /* Address of .options. */ +#define DT_MIPS_INTERFACE 0x7000002a /* Address of .interface. */ +#define DT_MIPS_DYNSTR_ALIGN 0x7000002b +#define DT_MIPS_INTERFACE_SIZE 0x7000002c /* Size of the .interface section. */ +#define DT_MIPS_RLD_TEXT_RESOLVE_ADDR 0x7000002d /* Address of rld_text_rsolve + function stored in GOT. */ +#define DT_MIPS_PERF_SUFFIX 0x7000002e /* Default suffix of dso to be added + by rld on dlopen() calls. */ +#define DT_MIPS_COMPACT_SIZE 0x7000002f /* (O32)Size of compact rel section. */ +#define DT_MIPS_GP_VALUE 0x70000030 /* GP value for aux GOTs. */ +#define DT_MIPS_AUX_DYNAMIC 0x70000031 /* Address of aux .dynamic. */ +/* The address of .got.plt in an executable using the new non-PIC ABI. */ +#define DT_MIPS_PLTGOT 0x70000032 +/* The base of the PLT in an executable using the new non-PIC ABI if that + PLT is writable. For a non-writable PLT, this is omitted or has a zero + value. */ +#define DT_MIPS_RWPLT 0x70000034 +#define DT_MIPS_NUM 0x35 + +/* Legal values for DT_MIPS_FLAGS Elf32_Dyn entry. */ + +#define RHF_NONE 0 /* No flags */ +#define RHF_QUICKSTART (1 << 0) /* Use quickstart */ +#define RHF_NOTPOT (1 << 1) /* Hash size not power of 2 */ +#define RHF_NO_LIBRARY_REPLACEMENT (1 << 2) /* Ignore LD_LIBRARY_PATH */ +#define RHF_NO_MOVE (1 << 3) +#define RHF_SGI_ONLY (1 << 4) +#define RHF_GUARANTEE_INIT (1 << 5) +#define RHF_DELTA_C_PLUS_PLUS (1 << 6) +#define RHF_GUARANTEE_START_INIT (1 << 7) +#define RHF_PIXIE (1 << 8) +#define RHF_DEFAULT_DELAY_LOAD (1 << 9) +#define RHF_REQUICKSTART (1 << 10) +#define RHF_REQUICKSTARTED (1 << 11) +#define RHF_CORD (1 << 12) +#define RHF_NO_UNRES_UNDEF (1 << 13) +#define RHF_RLD_ORDER_SAFE (1 << 14) + +/* Entries found in sections of type SHT_MIPS_LIBLIST. */ + +typedef struct +{ + Elf32_Word l_name; /* Name (string table index) */ + Elf32_Word l_time_stamp; /* Timestamp */ + Elf32_Word l_checksum; /* Checksum */ + Elf32_Word l_version; /* Interface version */ + Elf32_Word l_flags; /* Flags */ +} Elf32_Lib; + +typedef struct +{ + Elf64_Word l_name; /* Name (string table index) */ + Elf64_Word l_time_stamp; /* Timestamp */ + Elf64_Word l_checksum; /* Checksum */ + Elf64_Word l_version; /* Interface version */ + Elf64_Word l_flags; /* Flags */ +} Elf64_Lib; + + +/* Legal values for l_flags. */ + +#define LL_NONE 0 +#define LL_EXACT_MATCH (1 << 0) /* Require exact match */ +#define LL_IGNORE_INT_VER (1 << 1) /* Ignore interface version */ +#define LL_REQUIRE_MINOR (1 << 2) +#define LL_EXPORTS (1 << 3) +#define LL_DELAY_LOAD (1 << 4) +#define LL_DELTA (1 << 5) + +/* Entries found in sections of type SHT_MIPS_CONFLICT. */ + +typedef Elf32_Addr Elf32_Conflict; + + +/* HPPA specific definitions. */ + +/* Legal values for e_flags field of Elf32_Ehdr. */ + +#define EF_PARISC_TRAPNIL 0x00010000 /* Trap nil pointer dereference. */ +#define EF_PARISC_EXT 0x00020000 /* Program uses arch. extensions. */ +#define EF_PARISC_LSB 0x00040000 /* Program expects little endian. */ +#define EF_PARISC_WIDE 0x00080000 /* Program expects wide mode. */ +#define EF_PARISC_NO_KABP 0x00100000 /* No kernel assisted branch + prediction. */ +#define EF_PARISC_LAZYSWAP 0x00400000 /* Allow lazy swapping. */ +#define EF_PARISC_ARCH 0x0000ffff /* Architecture version. */ + +/* Defined values for `e_flags & EF_PARISC_ARCH' are: */ + +#define EFA_PARISC_1_0 0x020b /* PA-RISC 1.0 big-endian. */ +#define EFA_PARISC_1_1 0x0210 /* PA-RISC 1.1 big-endian. */ +#define EFA_PARISC_2_0 0x0214 /* PA-RISC 2.0 big-endian. */ + +/* Additional section indeces. */ + +#define SHN_PARISC_ANSI_COMMON 0xff00 /* Section for tenatively declared + symbols in ANSI C. */ +#define SHN_PARISC_HUGE_COMMON 0xff01 /* Common blocks in huge model. */ + +/* Legal values for sh_type field of Elf32_Shdr. */ + +#define SHT_PARISC_EXT 0x70000000 /* Contains product specific ext. */ +#define SHT_PARISC_UNWIND 0x70000001 /* Unwind information. */ +#define SHT_PARISC_DOC 0x70000002 /* Debug info for optimized code. */ + +/* Legal values for sh_flags field of Elf32_Shdr. */ + +#define SHF_PARISC_SHORT 0x20000000 /* Section with short addressing. */ +#define SHF_PARISC_HUGE 0x40000000 /* Section far from gp. */ +#define SHF_PARISC_SBP 0x80000000 /* Static branch prediction code. */ + +/* Legal values for ST_TYPE subfield of st_info (symbol type). */ + +#define STT_PARISC_MILLICODE 13 /* Millicode function entry point. */ + +#define STT_HP_OPAQUE (STT_LOOS + 0x1) +#define STT_HP_STUB (STT_LOOS + 0x2) + +/* HPPA relocs. */ + +#define R_PARISC_NONE 0 /* No reloc. */ +#define R_PARISC_DIR32 1 /* Direct 32-bit reference. */ +#define R_PARISC_DIR21L 2 /* Left 21 bits of eff. address. */ +#define R_PARISC_DIR17R 3 /* Right 17 bits of eff. address. */ +#define R_PARISC_DIR17F 4 /* 17 bits of eff. address. */ +#define R_PARISC_DIR14R 6 /* Right 14 bits of eff. address. */ +#define R_PARISC_PCREL32 9 /* 32-bit rel. address. */ +#define R_PARISC_PCREL21L 10 /* Left 21 bits of rel. address. */ +#define R_PARISC_PCREL17R 11 /* Right 17 bits of rel. address. */ +#define R_PARISC_PCREL17F 12 /* 17 bits of rel. address. */ +#define R_PARISC_PCREL14R 14 /* Right 14 bits of rel. address. */ +#define R_PARISC_DPREL21L 18 /* Left 21 bits of rel. address. */ +#define R_PARISC_DPREL14R 22 /* Right 14 bits of rel. address. */ +#define R_PARISC_GPREL21L 26 /* GP-relative, left 21 bits. */ +#define R_PARISC_GPREL14R 30 /* GP-relative, right 14 bits. */ +#define R_PARISC_LTOFF21L 34 /* LT-relative, left 21 bits. */ +#define R_PARISC_LTOFF14R 38 /* LT-relative, right 14 bits. */ +#define R_PARISC_SECREL32 41 /* 32 bits section rel. address. */ +#define R_PARISC_SEGBASE 48 /* No relocation, set segment base. */ +#define R_PARISC_SEGREL32 49 /* 32 bits segment rel. address. */ +#define R_PARISC_PLTOFF21L 50 /* PLT rel. address, left 21 bits. */ +#define R_PARISC_PLTOFF14R 54 /* PLT rel. address, right 14 bits. */ +#define R_PARISC_LTOFF_FPTR32 57 /* 32 bits LT-rel. function pointer. */ +#define R_PARISC_LTOFF_FPTR21L 58 /* LT-rel. fct ptr, left 21 bits. */ +#define R_PARISC_LTOFF_FPTR14R 62 /* LT-rel. fct ptr, right 14 bits. */ +#define R_PARISC_FPTR64 64 /* 64 bits function address. */ +#define R_PARISC_PLABEL32 65 /* 32 bits function address. */ +#define R_PARISC_PLABEL21L 66 /* Left 21 bits of fdesc address. */ +#define R_PARISC_PLABEL14R 70 /* Right 14 bits of fdesc address. */ +#define R_PARISC_PCREL64 72 /* 64 bits PC-rel. address. */ +#define R_PARISC_PCREL22F 74 /* 22 bits PC-rel. address. */ +#define R_PARISC_PCREL14WR 75 /* PC-rel. address, right 14 bits. */ +#define R_PARISC_PCREL14DR 76 /* PC rel. address, right 14 bits. */ +#define R_PARISC_PCREL16F 77 /* 16 bits PC-rel. address. */ +#define R_PARISC_PCREL16WF 78 /* 16 bits PC-rel. address. */ +#define R_PARISC_PCREL16DF 79 /* 16 bits PC-rel. address. */ +#define R_PARISC_DIR64 80 /* 64 bits of eff. address. */ +#define R_PARISC_DIR14WR 83 /* 14 bits of eff. address. */ +#define R_PARISC_DIR14DR 84 /* 14 bits of eff. address. */ +#define R_PARISC_DIR16F 85 /* 16 bits of eff. address. */ +#define R_PARISC_DIR16WF 86 /* 16 bits of eff. address. */ +#define R_PARISC_DIR16DF 87 /* 16 bits of eff. address. */ +#define R_PARISC_GPREL64 88 /* 64 bits of GP-rel. address. */ +#define R_PARISC_GPREL14WR 91 /* GP-rel. address, right 14 bits. */ +#define R_PARISC_GPREL14DR 92 /* GP-rel. address, right 14 bits. */ +#define R_PARISC_GPREL16F 93 /* 16 bits GP-rel. address. */ +#define R_PARISC_GPREL16WF 94 /* 16 bits GP-rel. address. */ +#define R_PARISC_GPREL16DF 95 /* 16 bits GP-rel. address. */ +#define R_PARISC_LTOFF64 96 /* 64 bits LT-rel. address. */ +#define R_PARISC_LTOFF14WR 99 /* LT-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF14DR 100 /* LT-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF16F 101 /* 16 bits LT-rel. address. */ +#define R_PARISC_LTOFF16WF 102 /* 16 bits LT-rel. address. */ +#define R_PARISC_LTOFF16DF 103 /* 16 bits LT-rel. address. */ +#define R_PARISC_SECREL64 104 /* 64 bits section rel. address. */ +#define R_PARISC_SEGREL64 112 /* 64 bits segment rel. address. */ +#define R_PARISC_PLTOFF14WR 115 /* PLT-rel. address, right 14 bits. */ +#define R_PARISC_PLTOFF14DR 116 /* PLT-rel. address, right 14 bits. */ +#define R_PARISC_PLTOFF16F 117 /* 16 bits LT-rel. address. */ +#define R_PARISC_PLTOFF16WF 118 /* 16 bits PLT-rel. address. */ +#define R_PARISC_PLTOFF16DF 119 /* 16 bits PLT-rel. address. */ +#define R_PARISC_LTOFF_FPTR64 120 /* 64 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR14WR 123 /* LT-rel. fct. ptr., right 14 bits. */ +#define R_PARISC_LTOFF_FPTR14DR 124 /* LT-rel. fct. ptr., right 14 bits. */ +#define R_PARISC_LTOFF_FPTR16F 125 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR16WF 126 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LTOFF_FPTR16DF 127 /* 16 bits LT-rel. function ptr. */ +#define R_PARISC_LORESERVE 128 +#define R_PARISC_COPY 128 /* Copy relocation. */ +#define R_PARISC_IPLT 129 /* Dynamic reloc, imported PLT */ +#define R_PARISC_EPLT 130 /* Dynamic reloc, exported PLT */ +#define R_PARISC_TPREL32 153 /* 32 bits TP-rel. address. */ +#define R_PARISC_TPREL21L 154 /* TP-rel. address, left 21 bits. */ +#define R_PARISC_TPREL14R 158 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_LTOFF_TP21L 162 /* LT-TP-rel. address, left 21 bits. */ +#define R_PARISC_LTOFF_TP14R 166 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP14F 167 /* 14 bits LT-TP-rel. address. */ +#define R_PARISC_TPREL64 216 /* 64 bits TP-rel. address. */ +#define R_PARISC_TPREL14WR 219 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_TPREL14DR 220 /* TP-rel. address, right 14 bits. */ +#define R_PARISC_TPREL16F 221 /* 16 bits TP-rel. address. */ +#define R_PARISC_TPREL16WF 222 /* 16 bits TP-rel. address. */ +#define R_PARISC_TPREL16DF 223 /* 16 bits TP-rel. address. */ +#define R_PARISC_LTOFF_TP64 224 /* 64 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP14WR 227 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP14DR 228 /* LT-TP-rel. address, right 14 bits.*/ +#define R_PARISC_LTOFF_TP16F 229 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP16WF 230 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_LTOFF_TP16DF 231 /* 16 bits LT-TP-rel. address. */ +#define R_PARISC_GNU_VTENTRY 232 +#define R_PARISC_GNU_VTINHERIT 233 +#define R_PARISC_TLS_GD21L 234 /* GD 21-bit left. */ +#define R_PARISC_TLS_GD14R 235 /* GD 14-bit right. */ +#define R_PARISC_TLS_GDCALL 236 /* GD call to __t_g_a. */ +#define R_PARISC_TLS_LDM21L 237 /* LD module 21-bit left. */ +#define R_PARISC_TLS_LDM14R 238 /* LD module 14-bit right. */ +#define R_PARISC_TLS_LDMCALL 239 /* LD module call to __t_g_a. */ +#define R_PARISC_TLS_LDO21L 240 /* LD offset 21-bit left. */ +#define R_PARISC_TLS_LDO14R 241 /* LD offset 14-bit right. */ +#define R_PARISC_TLS_DTPMOD32 242 /* DTP module 32-bit. */ +#define R_PARISC_TLS_DTPMOD64 243 /* DTP module 64-bit. */ +#define R_PARISC_TLS_DTPOFF32 244 /* DTP offset 32-bit. */ +#define R_PARISC_TLS_DTPOFF64 245 /* DTP offset 32-bit. */ +#define R_PARISC_TLS_LE21L R_PARISC_TPREL21L +#define R_PARISC_TLS_LE14R R_PARISC_TPREL14R +#define R_PARISC_TLS_IE21L R_PARISC_LTOFF_TP21L +#define R_PARISC_TLS_IE14R R_PARISC_LTOFF_TP14R +#define R_PARISC_TLS_TPREL32 R_PARISC_TPREL32 +#define R_PARISC_TLS_TPREL64 R_PARISC_TPREL64 +#define R_PARISC_HIRESERVE 255 + +/* Legal values for p_type field of Elf32_Phdr/Elf64_Phdr. */ + +#define PT_HP_TLS (PT_LOOS + 0x0) +#define PT_HP_CORE_NONE (PT_LOOS + 0x1) +#define PT_HP_CORE_VERSION (PT_LOOS + 0x2) +#define PT_HP_CORE_KERNEL (PT_LOOS + 0x3) +#define PT_HP_CORE_COMM (PT_LOOS + 0x4) +#define PT_HP_CORE_PROC (PT_LOOS + 0x5) +#define PT_HP_CORE_LOADABLE (PT_LOOS + 0x6) +#define PT_HP_CORE_STACK (PT_LOOS + 0x7) +#define PT_HP_CORE_SHM (PT_LOOS + 0x8) +#define PT_HP_CORE_MMF (PT_LOOS + 0x9) +#define PT_HP_PARALLEL (PT_LOOS + 0x10) +#define PT_HP_FASTBIND (PT_LOOS + 0x11) +#define PT_HP_OPT_ANNOT (PT_LOOS + 0x12) +#define PT_HP_HSL_ANNOT (PT_LOOS + 0x13) +#define PT_HP_STACK (PT_LOOS + 0x14) + +#define PT_PARISC_ARCHEXT 0x70000000 +#define PT_PARISC_UNWIND 0x70000001 + +/* Legal values for p_flags field of Elf32_Phdr/Elf64_Phdr. */ + +#define PF_PARISC_SBP 0x08000000 + +#define PF_HP_PAGE_SIZE 0x00100000 +#define PF_HP_FAR_SHARED 0x00200000 +#define PF_HP_NEAR_SHARED 0x00400000 +#define PF_HP_CODE 0x01000000 +#define PF_HP_MODIFY 0x02000000 +#define PF_HP_LAZYSWAP 0x04000000 +#define PF_HP_SBP 0x08000000 + + +/* Alpha specific definitions. */ + +/* Legal values for e_flags field of Elf64_Ehdr. */ + +#define EF_ALPHA_32BIT 1 /* All addresses must be < 2GB. */ +#define EF_ALPHA_CANRELAX 2 /* Relocations for relaxing exist. */ + +/* Legal values for sh_type field of Elf64_Shdr. */ + +/* These two are primerily concerned with ECOFF debugging info. */ +#define SHT_ALPHA_DEBUG 0x70000001 +#define SHT_ALPHA_REGINFO 0x70000002 + +/* Legal values for sh_flags field of Elf64_Shdr. */ + +#define SHF_ALPHA_GPREL 0x10000000 + +/* Legal values for st_other field of Elf64_Sym. */ +#define STO_ALPHA_NOPV 0x80 /* No PV required. */ +#define STO_ALPHA_STD_GPLOAD 0x88 /* PV only used for initial ldgp. */ + +/* Alpha relocs. */ + +#define R_ALPHA_NONE 0 /* No reloc */ +#define R_ALPHA_REFLONG 1 /* Direct 32 bit */ +#define R_ALPHA_REFQUAD 2 /* Direct 64 bit */ +#define R_ALPHA_GPREL32 3 /* GP relative 32 bit */ +#define R_ALPHA_LITERAL 4 /* GP relative 16 bit w/optimization */ +#define R_ALPHA_LITUSE 5 /* Optimization hint for LITERAL */ +#define R_ALPHA_GPDISP 6 /* Add displacement to GP */ +#define R_ALPHA_BRADDR 7 /* PC+4 relative 23 bit shifted */ +#define R_ALPHA_HINT 8 /* PC+4 relative 16 bit shifted */ +#define R_ALPHA_SREL16 9 /* PC relative 16 bit */ +#define R_ALPHA_SREL32 10 /* PC relative 32 bit */ +#define R_ALPHA_SREL64 11 /* PC relative 64 bit */ +#define R_ALPHA_GPRELHIGH 17 /* GP relative 32 bit, high 16 bits */ +#define R_ALPHA_GPRELLOW 18 /* GP relative 32 bit, low 16 bits */ +#define R_ALPHA_GPREL16 19 /* GP relative 16 bit */ +#define R_ALPHA_COPY 24 /* Copy symbol at runtime */ +#define R_ALPHA_GLOB_DAT 25 /* Create GOT entry */ +#define R_ALPHA_JMP_SLOT 26 /* Create PLT entry */ +#define R_ALPHA_RELATIVE 27 /* Adjust by program base */ +#define R_ALPHA_TLS_GD_HI 28 +#define R_ALPHA_TLSGD 29 +#define R_ALPHA_TLS_LDM 30 +#define R_ALPHA_DTPMOD64 31 +#define R_ALPHA_GOTDTPREL 32 +#define R_ALPHA_DTPREL64 33 +#define R_ALPHA_DTPRELHI 34 +#define R_ALPHA_DTPRELLO 35 +#define R_ALPHA_DTPREL16 36 +#define R_ALPHA_GOTTPREL 37 +#define R_ALPHA_TPREL64 38 +#define R_ALPHA_TPRELHI 39 +#define R_ALPHA_TPRELLO 40 +#define R_ALPHA_TPREL16 41 +/* Keep this the last entry. */ +#define R_ALPHA_NUM 46 + +/* Magic values of the LITUSE relocation addend. */ +#define LITUSE_ALPHA_ADDR 0 +#define LITUSE_ALPHA_BASE 1 +#define LITUSE_ALPHA_BYTOFF 2 +#define LITUSE_ALPHA_JSR 3 +#define LITUSE_ALPHA_TLS_GD 4 +#define LITUSE_ALPHA_TLS_LDM 5 + +/* Legal values for d_tag of Elf64_Dyn. */ +#define DT_ALPHA_PLTRO (DT_LOPROC + 0) +#define DT_ALPHA_NUM 1 + +/* PowerPC specific declarations */ + +/* Values for Elf32/64_Ehdr.e_flags. */ +#define EF_PPC_EMB 0x80000000 /* PowerPC embedded flag */ + +/* Cygnus local bits below */ +#define EF_PPC_RELOCATABLE 0x00010000 /* PowerPC -mrelocatable flag*/ +#define EF_PPC_RELOCATABLE_LIB 0x00008000 /* PowerPC -mrelocatable-lib + flag */ + +/* PowerPC relocations defined by the ABIs */ +#define R_PPC_NONE 0 +#define R_PPC_ADDR32 1 /* 32bit absolute address */ +#define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored. */ +#define R_PPC_ADDR16 3 /* 16bit absolute address */ +#define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */ +#define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */ +#define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */ +#define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */ +#define R_PPC_ADDR14_BRTAKEN 8 +#define R_PPC_ADDR14_BRNTAKEN 9 +#define R_PPC_REL24 10 /* PC relative 26 bit */ +#define R_PPC_REL14 11 /* PC relative 16 bit */ +#define R_PPC_REL14_BRTAKEN 12 +#define R_PPC_REL14_BRNTAKEN 13 +#define R_PPC_GOT16 14 +#define R_PPC_GOT16_LO 15 +#define R_PPC_GOT16_HI 16 +#define R_PPC_GOT16_HA 17 +#define R_PPC_PLTREL24 18 +#define R_PPC_COPY 19 +#define R_PPC_GLOB_DAT 20 +#define R_PPC_JMP_SLOT 21 +#define R_PPC_RELATIVE 22 +#define R_PPC_LOCAL24PC 23 +#define R_PPC_UADDR32 24 +#define R_PPC_UADDR16 25 +#define R_PPC_REL32 26 +#define R_PPC_PLT32 27 +#define R_PPC_PLTREL32 28 +#define R_PPC_PLT16_LO 29 +#define R_PPC_PLT16_HI 30 +#define R_PPC_PLT16_HA 31 +#define R_PPC_SDAREL16 32 +#define R_PPC_SECTOFF 33 +#define R_PPC_SECTOFF_LO 34 +#define R_PPC_SECTOFF_HI 35 +#define R_PPC_SECTOFF_HA 36 + +/* PowerPC relocations defined for the TLS access ABI. */ +#define R_PPC_TLS 67 /* none (sym+add)@tls */ +#define R_PPC_DTPMOD32 68 /* word32 (sym+add)@dtpmod */ +#define R_PPC_TPREL16 69 /* half16* (sym+add)@tprel */ +#define R_PPC_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */ +#define R_PPC_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */ +#define R_PPC_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */ +#define R_PPC_TPREL32 73 /* word32 (sym+add)@tprel */ +#define R_PPC_DTPREL16 74 /* half16* (sym+add)@dtprel */ +#define R_PPC_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */ +#define R_PPC_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */ +#define R_PPC_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */ +#define R_PPC_DTPREL32 78 /* word32 (sym+add)@dtprel */ +#define R_PPC_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */ +#define R_PPC_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */ +#define R_PPC_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */ +#define R_PPC_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */ +#define R_PPC_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */ +#define R_PPC_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */ +#define R_PPC_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */ +#define R_PPC_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */ +#define R_PPC_GOT_TPREL16 87 /* half16* (sym+add)@got@tprel */ +#define R_PPC_GOT_TPREL16_LO 88 /* half16 (sym+add)@got@tprel@l */ +#define R_PPC_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */ +#define R_PPC_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */ +#define R_PPC_GOT_DTPREL16 91 /* half16* (sym+add)@got@dtprel */ +#define R_PPC_GOT_DTPREL16_LO 92 /* half16* (sym+add)@got@dtprel@l */ +#define R_PPC_GOT_DTPREL16_HI 93 /* half16* (sym+add)@got@dtprel@h */ +#define R_PPC_GOT_DTPREL16_HA 94 /* half16* (sym+add)@got@dtprel@ha */ + +/* The remaining relocs are from the Embedded ELF ABI, and are not + in the SVR4 ELF ABI. */ +#define R_PPC_EMB_NADDR32 101 +#define R_PPC_EMB_NADDR16 102 +#define R_PPC_EMB_NADDR16_LO 103 +#define R_PPC_EMB_NADDR16_HI 104 +#define R_PPC_EMB_NADDR16_HA 105 +#define R_PPC_EMB_SDAI16 106 +#define R_PPC_EMB_SDA2I16 107 +#define R_PPC_EMB_SDA2REL 108 +#define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */ +#define R_PPC_EMB_MRKREF 110 +#define R_PPC_EMB_RELSEC16 111 +#define R_PPC_EMB_RELST_LO 112 +#define R_PPC_EMB_RELST_HI 113 +#define R_PPC_EMB_RELST_HA 114 +#define R_PPC_EMB_BIT_FLD 115 +#define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */ + +/* Diab tool relocations. */ +#define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */ +#define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */ +#define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */ +#define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */ +#define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */ +#define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */ + +/* GNU extension to support local ifunc. */ +#define R_PPC_IRELATIVE 248 + +/* GNU relocs used in PIC code sequences. */ +#define R_PPC_REL16 249 /* half16 (sym+add-.) */ +#define R_PPC_REL16_LO 250 /* half16 (sym+add-.)@l */ +#define R_PPC_REL16_HI 251 /* half16 (sym+add-.)@h */ +#define R_PPC_REL16_HA 252 /* half16 (sym+add-.)@ha */ + +/* This is a phony reloc to handle any old fashioned TOC16 references + that may still be in object files. */ +#define R_PPC_TOC16 255 + +/* PowerPC specific values for the Dyn d_tag field. */ +#define DT_PPC_GOT (DT_LOPROC + 0) +#define DT_PPC_NUM 1 + +/* PowerPC64 relocations defined by the ABIs */ +#define R_PPC64_NONE R_PPC_NONE +#define R_PPC64_ADDR32 R_PPC_ADDR32 /* 32bit absolute address */ +#define R_PPC64_ADDR24 R_PPC_ADDR24 /* 26bit address, word aligned */ +#define R_PPC64_ADDR16 R_PPC_ADDR16 /* 16bit absolute address */ +#define R_PPC64_ADDR16_LO R_PPC_ADDR16_LO /* lower 16bits of address */ +#define R_PPC64_ADDR16_HI R_PPC_ADDR16_HI /* high 16bits of address. */ +#define R_PPC64_ADDR16_HA R_PPC_ADDR16_HA /* adjusted high 16bits. */ +#define R_PPC64_ADDR14 R_PPC_ADDR14 /* 16bit address, word aligned */ +#define R_PPC64_ADDR14_BRTAKEN R_PPC_ADDR14_BRTAKEN +#define R_PPC64_ADDR14_BRNTAKEN R_PPC_ADDR14_BRNTAKEN +#define R_PPC64_REL24 R_PPC_REL24 /* PC-rel. 26 bit, word aligned */ +#define R_PPC64_REL14 R_PPC_REL14 /* PC relative 16 bit */ +#define R_PPC64_REL14_BRTAKEN R_PPC_REL14_BRTAKEN +#define R_PPC64_REL14_BRNTAKEN R_PPC_REL14_BRNTAKEN +#define R_PPC64_GOT16 R_PPC_GOT16 +#define R_PPC64_GOT16_LO R_PPC_GOT16_LO +#define R_PPC64_GOT16_HI R_PPC_GOT16_HI +#define R_PPC64_GOT16_HA R_PPC_GOT16_HA + +#define R_PPC64_COPY R_PPC_COPY +#define R_PPC64_GLOB_DAT R_PPC_GLOB_DAT +#define R_PPC64_JMP_SLOT R_PPC_JMP_SLOT +#define R_PPC64_RELATIVE R_PPC_RELATIVE + +#define R_PPC64_UADDR32 R_PPC_UADDR32 +#define R_PPC64_UADDR16 R_PPC_UADDR16 +#define R_PPC64_REL32 R_PPC_REL32 +#define R_PPC64_PLT32 R_PPC_PLT32 +#define R_PPC64_PLTREL32 R_PPC_PLTREL32 +#define R_PPC64_PLT16_LO R_PPC_PLT16_LO +#define R_PPC64_PLT16_HI R_PPC_PLT16_HI +#define R_PPC64_PLT16_HA R_PPC_PLT16_HA + +#define R_PPC64_SECTOFF R_PPC_SECTOFF +#define R_PPC64_SECTOFF_LO R_PPC_SECTOFF_LO +#define R_PPC64_SECTOFF_HI R_PPC_SECTOFF_HI +#define R_PPC64_SECTOFF_HA R_PPC_SECTOFF_HA +#define R_PPC64_ADDR30 37 /* word30 (S + A - P) >> 2 */ +#define R_PPC64_ADDR64 38 /* doubleword64 S + A */ +#define R_PPC64_ADDR16_HIGHER 39 /* half16 #higher(S + A) */ +#define R_PPC64_ADDR16_HIGHERA 40 /* half16 #highera(S + A) */ +#define R_PPC64_ADDR16_HIGHEST 41 /* half16 #highest(S + A) */ +#define R_PPC64_ADDR16_HIGHESTA 42 /* half16 #highesta(S + A) */ +#define R_PPC64_UADDR64 43 /* doubleword64 S + A */ +#define R_PPC64_REL64 44 /* doubleword64 S + A - P */ +#define R_PPC64_PLT64 45 /* doubleword64 L + A */ +#define R_PPC64_PLTREL64 46 /* doubleword64 L + A - P */ +#define R_PPC64_TOC16 47 /* half16* S + A - .TOC */ +#define R_PPC64_TOC16_LO 48 /* half16 #lo(S + A - .TOC.) */ +#define R_PPC64_TOC16_HI 49 /* half16 #hi(S + A - .TOC.) */ +#define R_PPC64_TOC16_HA 50 /* half16 #ha(S + A - .TOC.) */ +#define R_PPC64_TOC 51 /* doubleword64 .TOC */ +#define R_PPC64_PLTGOT16 52 /* half16* M + A */ +#define R_PPC64_PLTGOT16_LO 53 /* half16 #lo(M + A) */ +#define R_PPC64_PLTGOT16_HI 54 /* half16 #hi(M + A) */ +#define R_PPC64_PLTGOT16_HA 55 /* half16 #ha(M + A) */ + +#define R_PPC64_ADDR16_DS 56 /* half16ds* (S + A) >> 2 */ +#define R_PPC64_ADDR16_LO_DS 57 /* half16ds #lo(S + A) >> 2 */ +#define R_PPC64_GOT16_DS 58 /* half16ds* (G + A) >> 2 */ +#define R_PPC64_GOT16_LO_DS 59 /* half16ds #lo(G + A) >> 2 */ +#define R_PPC64_PLT16_LO_DS 60 /* half16ds #lo(L + A) >> 2 */ +#define R_PPC64_SECTOFF_DS 61 /* half16ds* (R + A) >> 2 */ +#define R_PPC64_SECTOFF_LO_DS 62 /* half16ds #lo(R + A) >> 2 */ +#define R_PPC64_TOC16_DS 63 /* half16ds* (S + A - .TOC.) >> 2 */ +#define R_PPC64_TOC16_LO_DS 64 /* half16ds #lo(S + A - .TOC.) >> 2 */ +#define R_PPC64_PLTGOT16_DS 65 /* half16ds* (M + A) >> 2 */ +#define R_PPC64_PLTGOT16_LO_DS 66 /* half16ds #lo(M + A) >> 2 */ + +/* PowerPC64 relocations defined for the TLS access ABI. */ +#define R_PPC64_TLS 67 /* none (sym+add)@tls */ +#define R_PPC64_DTPMOD64 68 /* doubleword64 (sym+add)@dtpmod */ +#define R_PPC64_TPREL16 69 /* half16* (sym+add)@tprel */ +#define R_PPC64_TPREL16_LO 70 /* half16 (sym+add)@tprel@l */ +#define R_PPC64_TPREL16_HI 71 /* half16 (sym+add)@tprel@h */ +#define R_PPC64_TPREL16_HA 72 /* half16 (sym+add)@tprel@ha */ +#define R_PPC64_TPREL64 73 /* doubleword64 (sym+add)@tprel */ +#define R_PPC64_DTPREL16 74 /* half16* (sym+add)@dtprel */ +#define R_PPC64_DTPREL16_LO 75 /* half16 (sym+add)@dtprel@l */ +#define R_PPC64_DTPREL16_HI 76 /* half16 (sym+add)@dtprel@h */ +#define R_PPC64_DTPREL16_HA 77 /* half16 (sym+add)@dtprel@ha */ +#define R_PPC64_DTPREL64 78 /* doubleword64 (sym+add)@dtprel */ +#define R_PPC64_GOT_TLSGD16 79 /* half16* (sym+add)@got@tlsgd */ +#define R_PPC64_GOT_TLSGD16_LO 80 /* half16 (sym+add)@got@tlsgd@l */ +#define R_PPC64_GOT_TLSGD16_HI 81 /* half16 (sym+add)@got@tlsgd@h */ +#define R_PPC64_GOT_TLSGD16_HA 82 /* half16 (sym+add)@got@tlsgd@ha */ +#define R_PPC64_GOT_TLSLD16 83 /* half16* (sym+add)@got@tlsld */ +#define R_PPC64_GOT_TLSLD16_LO 84 /* half16 (sym+add)@got@tlsld@l */ +#define R_PPC64_GOT_TLSLD16_HI 85 /* half16 (sym+add)@got@tlsld@h */ +#define R_PPC64_GOT_TLSLD16_HA 86 /* half16 (sym+add)@got@tlsld@ha */ +#define R_PPC64_GOT_TPREL16_DS 87 /* half16ds* (sym+add)@got@tprel */ +#define R_PPC64_GOT_TPREL16_LO_DS 88 /* half16ds (sym+add)@got@tprel@l */ +#define R_PPC64_GOT_TPREL16_HI 89 /* half16 (sym+add)@got@tprel@h */ +#define R_PPC64_GOT_TPREL16_HA 90 /* half16 (sym+add)@got@tprel@ha */ +#define R_PPC64_GOT_DTPREL16_DS 91 /* half16ds* (sym+add)@got@dtprel */ +#define R_PPC64_GOT_DTPREL16_LO_DS 92 /* half16ds (sym+add)@got@dtprel@l */ +#define R_PPC64_GOT_DTPREL16_HI 93 /* half16 (sym+add)@got@dtprel@h */ +#define R_PPC64_GOT_DTPREL16_HA 94 /* half16 (sym+add)@got@dtprel@ha */ +#define R_PPC64_TPREL16_DS 95 /* half16ds* (sym+add)@tprel */ +#define R_PPC64_TPREL16_LO_DS 96 /* half16ds (sym+add)@tprel@l */ +#define R_PPC64_TPREL16_HIGHER 97 /* half16 (sym+add)@tprel@higher */ +#define R_PPC64_TPREL16_HIGHERA 98 /* half16 (sym+add)@tprel@highera */ +#define R_PPC64_TPREL16_HIGHEST 99 /* half16 (sym+add)@tprel@highest */ +#define R_PPC64_TPREL16_HIGHESTA 100 /* half16 (sym+add)@tprel@highesta */ +#define R_PPC64_DTPREL16_DS 101 /* half16ds* (sym+add)@dtprel */ +#define R_PPC64_DTPREL16_LO_DS 102 /* half16ds (sym+add)@dtprel@l */ +#define R_PPC64_DTPREL16_HIGHER 103 /* half16 (sym+add)@dtprel@higher */ +#define R_PPC64_DTPREL16_HIGHERA 104 /* half16 (sym+add)@dtprel@highera */ +#define R_PPC64_DTPREL16_HIGHEST 105 /* half16 (sym+add)@dtprel@highest */ +#define R_PPC64_DTPREL16_HIGHESTA 106 /* half16 (sym+add)@dtprel@highesta */ + +/* GNU extension to support local ifunc. */ +#define R_PPC64_JMP_IREL 247 +#define R_PPC64_IRELATIVE 248 +#define R_PPC64_REL16 249 /* half16 (sym+add-.) */ +#define R_PPC64_REL16_LO 250 /* half16 (sym+add-.)@l */ +#define R_PPC64_REL16_HI 251 /* half16 (sym+add-.)@h */ +#define R_PPC64_REL16_HA 252 /* half16 (sym+add-.)@ha */ + +/* PowerPC64 specific values for the Dyn d_tag field. */ +#define DT_PPC64_GLINK (DT_LOPROC + 0) +#define DT_PPC64_OPD (DT_LOPROC + 1) +#define DT_PPC64_OPDSZ (DT_LOPROC + 2) +#define DT_PPC64_NUM 3 + + +/* ARM specific declarations */ + +/* Processor specific flags for the ELF header e_flags field. */ +#define EF_ARM_RELEXEC 0x01 +#define EF_ARM_HASENTRY 0x02 +#define EF_ARM_INTERWORK 0x04 +#define EF_ARM_APCS_26 0x08 +#define EF_ARM_APCS_FLOAT 0x10 +#define EF_ARM_PIC 0x20 +#define EF_ARM_ALIGN8 0x40 /* 8-bit structure alignment is in use */ +#define EF_ARM_NEW_ABI 0x80 +#define EF_ARM_OLD_ABI 0x100 +#define EF_ARM_SOFT_FLOAT 0x200 +#define EF_ARM_VFP_FLOAT 0x400 +#define EF_ARM_MAVERICK_FLOAT 0x800 + +#define EF_ARM_ABI_FLOAT_SOFT 0x200 /* NB conflicts with EF_ARM_SOFT_FLOAT */ +#define EF_ARM_ABI_FLOAT_HARD 0x400 /* NB conflicts with EF_ARM_VFP_FLOAT */ + + +/* Other constants defined in the ARM ELF spec. version B-01. */ +/* NB. These conflict with values defined above. */ +#define EF_ARM_SYMSARESORTED 0x04 +#define EF_ARM_DYNSYMSUSESEGIDX 0x08 +#define EF_ARM_MAPSYMSFIRST 0x10 +#define EF_ARM_EABIMASK 0XFF000000 + +/* Constants defined in AAELF. */ +#define EF_ARM_BE8 0x00800000 +#define EF_ARM_LE8 0x00400000 + +#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK) +#define EF_ARM_EABI_UNKNOWN 0x00000000 +#define EF_ARM_EABI_VER1 0x01000000 +#define EF_ARM_EABI_VER2 0x02000000 +#define EF_ARM_EABI_VER3 0x03000000 +#define EF_ARM_EABI_VER4 0x04000000 +#define EF_ARM_EABI_VER5 0x05000000 + +/* Additional symbol types for Thumb. */ +#define STT_ARM_TFUNC STT_LOPROC /* A Thumb function. */ +#define STT_ARM_16BIT STT_HIPROC /* A Thumb label. */ + +/* ARM-specific values for sh_flags */ +#define SHF_ARM_ENTRYSECT 0x10000000 /* Section contains an entry point */ +#define SHF_ARM_COMDEF 0x80000000 /* Section may be multiply defined + in the input to a link step. */ + +/* ARM-specific program header flags */ +#define PF_ARM_SB 0x10000000 /* Segment contains the location + addressed by the static base. */ +#define PF_ARM_PI 0x20000000 /* Position-independent segment. */ +#define PF_ARM_ABS 0x40000000 /* Absolute segment. */ + +/* Processor specific values for the Phdr p_type field. */ +#define PT_ARM_EXIDX (PT_LOPROC + 1) /* ARM unwind segment. */ + +/* Processor specific values for the Shdr sh_type field. */ +#define SHT_ARM_EXIDX (SHT_LOPROC + 1) /* ARM unwind section. */ +#define SHT_ARM_PREEMPTMAP (SHT_LOPROC + 2) /* Preemption details. */ +#define SHT_ARM_ATTRIBUTES (SHT_LOPROC + 3) /* ARM attributes section. */ + + +/* AArch64 relocs. */ + +#define R_AARCH64_NONE 0 /* No relocation. */ +#define R_AARCH64_ABS64 257 /* Direct 64 bit. */ +#define R_AARCH64_ABS32 258 /* Direct 32 bit. */ +#define R_AARCH64_COPY 1024 /* Copy symbol at runtime. */ +#define R_AARCH64_GLOB_DAT 1025 /* Create GOT entry. */ +#define R_AARCH64_JUMP_SLOT 1026 /* Create PLT entry. */ +#define R_AARCH64_RELATIVE 1027 /* Adjust by program base. */ +#define R_AARCH64_TLS_DTPMOD64 1028 /* Module number, 64 bit. */ +#define R_AARCH64_TLS_DTPREL64 1029 /* Module-relative offset, 64 bit. */ +#define R_AARCH64_TLS_TPREL64 1030 /* TP-relative offset, 64 bit. */ +#define R_AARCH64_TLSDESC 1031 /* TLS Descriptor. */ + +/* ARM relocs. */ + +#define R_ARM_NONE 0 /* No reloc */ +#define R_ARM_PC24 1 /* PC relative 26 bit branch */ +#define R_ARM_ABS32 2 /* Direct 32 bit */ +#define R_ARM_REL32 3 /* PC relative 32 bit */ +#define R_ARM_PC13 4 +#define R_ARM_ABS16 5 /* Direct 16 bit */ +#define R_ARM_ABS12 6 /* Direct 12 bit */ +#define R_ARM_THM_ABS5 7 +#define R_ARM_ABS8 8 /* Direct 8 bit */ +#define R_ARM_SBREL32 9 +#define R_ARM_THM_PC22 10 +#define R_ARM_THM_PC8 11 +#define R_ARM_AMP_VCALL9 12 +#define R_ARM_SWI24 13 /* Obsolete static relocation. */ +#define R_ARM_TLS_DESC 13 /* Dynamic relocation. */ +#define R_ARM_THM_SWI8 14 +#define R_ARM_XPC25 15 +#define R_ARM_THM_XPC22 16 +#define R_ARM_TLS_DTPMOD32 17 /* ID of module containing symbol */ +#define R_ARM_TLS_DTPOFF32 18 /* Offset in TLS block */ +#define R_ARM_TLS_TPOFF32 19 /* Offset in static TLS block */ +#define R_ARM_COPY 20 /* Copy symbol at runtime */ +#define R_ARM_GLOB_DAT 21 /* Create GOT entry */ +#define R_ARM_JUMP_SLOT 22 /* Create PLT entry */ +#define R_ARM_RELATIVE 23 /* Adjust by program base */ +#define R_ARM_GOTOFF 24 /* 32 bit offset to GOT */ +#define R_ARM_GOTPC 25 /* 32 bit PC relative offset to GOT */ +#define R_ARM_GOT32 26 /* 32 bit GOT entry */ +#define R_ARM_PLT32 27 /* 32 bit PLT address */ +#define R_ARM_CALL 28 +#define R_ARM_JUMP24 29 +#define R_ARM_THM_JUMP24 30 +#define R_ARM_ALU_PCREL_7_0 32 +#define R_ARM_ALU_PCREL_15_8 33 +#define R_ARM_ALU_PCREL_23_15 34 +#define R_ARM_LDR_SBREL_11_0 35 +#define R_ARM_ALU_SBREL_19_12 36 +#define R_ARM_ALU_SBREL_27_20 37 +#define R_ARM_V4BX 40 +#define R_ARM_PREL31 42 +#define R_ARM_MOVW_ABS_NC 43 +#define R_ARM_MOVT_ABS 44 +#define R_ARM_THM_MOVW_ABS_NC 47 +#define R_ARM_THM_MOVT_ABS 48 +#define R_ARM_TLS_GOTDESC 90 +#define R_ARM_TLS_CALL 91 +#define R_ARM_TLS_DESCSEQ 92 +#define R_ARM_THM_TLS_CALL 93 +#define R_ARM_GNU_VTENTRY 100 +#define R_ARM_GNU_VTINHERIT 101 +#define R_ARM_THM_PC11 102 /* thumb unconditional branch */ +#define R_ARM_THM_PC9 103 /* thumb conditional branch */ +#define R_ARM_TLS_GD32 104 /* PC-rel 32 bit for global dynamic + thread local data */ +#define R_ARM_TLS_LDM32 105 /* PC-rel 32 bit for local dynamic + thread local data */ +#define R_ARM_TLS_LDO32 106 /* 32 bit offset relative to TLS + block */ +#define R_ARM_TLS_IE32 107 /* PC-rel 32 bit for GOT entry of + static TLS block offset */ +#define R_ARM_TLS_LE32 108 /* 32 bit offset relative to static + TLS block */ +#define R_ARM_THM_TLS_DESCSEQ 129 +#define R_ARM_IRELATIVE 160 +#define R_ARM_RXPC25 249 +#define R_ARM_RSBREL32 250 +#define R_ARM_THM_RPC22 251 +#define R_ARM_RREL32 252 +#define R_ARM_RABS22 253 +#define R_ARM_RPC24 254 +#define R_ARM_RBASE 255 +/* Keep this the last entry. */ +#define R_ARM_NUM 256 + +/* TMS320C67xx specific declarations */ + +/* XXX: no ELF standard yet*/ + +/* TMS320C67xx relocs. */ +#define R_C60_32 1 +#define R_C60_GOT32 3 /* 32 bit GOT entry */ +#define R_C60_PLT32 4 /* 32 bit PLT address */ +#define R_C60_COPY 5 /* Copy symbol at runtime */ +#define R_C60_GLOB_DAT 6 /* Create GOT entry */ +#define R_C60_JMP_SLOT 7 /* Create PLT entry */ +#define R_C60_RELATIVE 8 /* Adjust by program base */ +#define R_C60_GOTOFF 9 /* 32 bit offset to GOT */ +#define R_C60_GOTPC 10 /* 32 bit PC relative offset to GOT */ + +#define R_C60HI16 0x55 /* high 16 bit MVKH embedded */ +#define R_C60LO16 0x54 /* low 16 bit MVKL embedded */ + +/* IA-64 specific declarations. */ + +/* Processor specific flags for the Ehdr e_flags field. */ +#define EF_IA_64_MASKOS 0x0000000f /* os-specific flags */ +#define EF_IA_64_ABI64 0x00000010 /* 64-bit ABI */ +#define EF_IA_64_ARCH 0xff000000 /* arch. version mask */ + +/* Processor specific values for the Phdr p_type field. */ +#define PT_IA_64_ARCHEXT (PT_LOPROC + 0) /* arch extension bits */ +#define PT_IA_64_UNWIND (PT_LOPROC + 1) /* ia64 unwind bits */ +#define PT_IA_64_HP_OPT_ANOT (PT_LOOS + 0x12) +#define PT_IA_64_HP_HSL_ANOT (PT_LOOS + 0x13) +#define PT_IA_64_HP_STACK (PT_LOOS + 0x14) + +/* Processor specific flags for the Phdr p_flags field. */ +#define PF_IA_64_NORECOV 0x80000000 /* spec insns w/o recovery */ + +/* Processor specific values for the Shdr sh_type field. */ +#define SHT_IA_64_EXT (SHT_LOPROC + 0) /* extension bits */ +#define SHT_IA_64_UNWIND (SHT_LOPROC + 1) /* unwind bits */ + +/* Processor specific flags for the Shdr sh_flags field. */ +#define SHF_IA_64_SHORT 0x10000000 /* section near gp */ +#define SHF_IA_64_NORECOV 0x20000000 /* spec insns w/o recovery */ + +/* Processor specific values for the Dyn d_tag field. */ +#define DT_IA_64_PLT_RESERVE (DT_LOPROC + 0) +#define DT_IA_64_NUM 1 + +/* IA-64 relocations. */ +#define R_IA64_NONE 0x00 /* none */ +#define R_IA64_IMM14 0x21 /* symbol + addend, add imm14 */ +#define R_IA64_IMM22 0x22 /* symbol + addend, add imm22 */ +#define R_IA64_IMM64 0x23 /* symbol + addend, mov imm64 */ +#define R_IA64_DIR32MSB 0x24 /* symbol + addend, data4 MSB */ +#define R_IA64_DIR32LSB 0x25 /* symbol + addend, data4 LSB */ +#define R_IA64_DIR64MSB 0x26 /* symbol + addend, data8 MSB */ +#define R_IA64_DIR64LSB 0x27 /* symbol + addend, data8 LSB */ +#define R_IA64_GPREL22 0x2a /* @gprel(sym + add), add imm22 */ +#define R_IA64_GPREL64I 0x2b /* @gprel(sym + add), mov imm64 */ +#define R_IA64_GPREL32MSB 0x2c /* @gprel(sym + add), data4 MSB */ +#define R_IA64_GPREL32LSB 0x2d /* @gprel(sym + add), data4 LSB */ +#define R_IA64_GPREL64MSB 0x2e /* @gprel(sym + add), data8 MSB */ +#define R_IA64_GPREL64LSB 0x2f /* @gprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF22 0x32 /* @ltoff(sym + add), add imm22 */ +#define R_IA64_LTOFF64I 0x33 /* @ltoff(sym + add), mov imm64 */ +#define R_IA64_PLTOFF22 0x3a /* @pltoff(sym + add), add imm22 */ +#define R_IA64_PLTOFF64I 0x3b /* @pltoff(sym + add), mov imm64 */ +#define R_IA64_PLTOFF64MSB 0x3e /* @pltoff(sym + add), data8 MSB */ +#define R_IA64_PLTOFF64LSB 0x3f /* @pltoff(sym + add), data8 LSB */ +#define R_IA64_FPTR64I 0x43 /* @fptr(sym + add), mov imm64 */ +#define R_IA64_FPTR32MSB 0x44 /* @fptr(sym + add), data4 MSB */ +#define R_IA64_FPTR32LSB 0x45 /* @fptr(sym + add), data4 LSB */ +#define R_IA64_FPTR64MSB 0x46 /* @fptr(sym + add), data8 MSB */ +#define R_IA64_FPTR64LSB 0x47 /* @fptr(sym + add), data8 LSB */ +#define R_IA64_PCREL60B 0x48 /* @pcrel(sym + add), brl */ +#define R_IA64_PCREL21B 0x49 /* @pcrel(sym + add), ptb, call */ +#define R_IA64_PCREL21M 0x4a /* @pcrel(sym + add), chk.s */ +#define R_IA64_PCREL21F 0x4b /* @pcrel(sym + add), fchkf */ +#define R_IA64_PCREL32MSB 0x4c /* @pcrel(sym + add), data4 MSB */ +#define R_IA64_PCREL32LSB 0x4d /* @pcrel(sym + add), data4 LSB */ +#define R_IA64_PCREL64MSB 0x4e /* @pcrel(sym + add), data8 MSB */ +#define R_IA64_PCREL64LSB 0x4f /* @pcrel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_FPTR22 0x52 /* @ltoff(@fptr(s+a)), imm22 */ +#define R_IA64_LTOFF_FPTR64I 0x53 /* @ltoff(@fptr(s+a)), imm64 */ +#define R_IA64_LTOFF_FPTR32MSB 0x54 /* @ltoff(@fptr(s+a)), data4 MSB */ +#define R_IA64_LTOFF_FPTR32LSB 0x55 /* @ltoff(@fptr(s+a)), data4 LSB */ +#define R_IA64_LTOFF_FPTR64MSB 0x56 /* @ltoff(@fptr(s+a)), data8 MSB */ +#define R_IA64_LTOFF_FPTR64LSB 0x57 /* @ltoff(@fptr(s+a)), data8 LSB */ +#define R_IA64_SEGREL32MSB 0x5c /* @segrel(sym + add), data4 MSB */ +#define R_IA64_SEGREL32LSB 0x5d /* @segrel(sym + add), data4 LSB */ +#define R_IA64_SEGREL64MSB 0x5e /* @segrel(sym + add), data8 MSB */ +#define R_IA64_SEGREL64LSB 0x5f /* @segrel(sym + add), data8 LSB */ +#define R_IA64_SECREL32MSB 0x64 /* @secrel(sym + add), data4 MSB */ +#define R_IA64_SECREL32LSB 0x65 /* @secrel(sym + add), data4 LSB */ +#define R_IA64_SECREL64MSB 0x66 /* @secrel(sym + add), data8 MSB */ +#define R_IA64_SECREL64LSB 0x67 /* @secrel(sym + add), data8 LSB */ +#define R_IA64_REL32MSB 0x6c /* data 4 + REL */ +#define R_IA64_REL32LSB 0x6d /* data 4 + REL */ +#define R_IA64_REL64MSB 0x6e /* data 8 + REL */ +#define R_IA64_REL64LSB 0x6f /* data 8 + REL */ +#define R_IA64_LTV32MSB 0x74 /* symbol + addend, data4 MSB */ +#define R_IA64_LTV32LSB 0x75 /* symbol + addend, data4 LSB */ +#define R_IA64_LTV64MSB 0x76 /* symbol + addend, data8 MSB */ +#define R_IA64_LTV64LSB 0x77 /* symbol + addend, data8 LSB */ +#define R_IA64_PCREL21BI 0x79 /* @pcrel(sym + add), 21bit inst */ +#define R_IA64_PCREL22 0x7a /* @pcrel(sym + add), 22bit inst */ +#define R_IA64_PCREL64I 0x7b /* @pcrel(sym + add), 64bit inst */ +#define R_IA64_IPLTMSB 0x80 /* dynamic reloc, imported PLT, MSB */ +#define R_IA64_IPLTLSB 0x81 /* dynamic reloc, imported PLT, LSB */ +#define R_IA64_COPY 0x84 /* copy relocation */ +#define R_IA64_SUB 0x85 /* Addend and symbol difference */ +#define R_IA64_LTOFF22X 0x86 /* LTOFF22, relaxable. */ +#define R_IA64_LDXMOV 0x87 /* Use of LTOFF22X. */ +#define R_IA64_TPREL14 0x91 /* @tprel(sym + add), imm14 */ +#define R_IA64_TPREL22 0x92 /* @tprel(sym + add), imm22 */ +#define R_IA64_TPREL64I 0x93 /* @tprel(sym + add), imm64 */ +#define R_IA64_TPREL64MSB 0x96 /* @tprel(sym + add), data8 MSB */ +#define R_IA64_TPREL64LSB 0x97 /* @tprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_TPREL22 0x9a /* @ltoff(@tprel(s+a)), imm2 */ +#define R_IA64_DTPMOD64MSB 0xa6 /* @dtpmod(sym + add), data8 MSB */ +#define R_IA64_DTPMOD64LSB 0xa7 /* @dtpmod(sym + add), data8 LSB */ +#define R_IA64_LTOFF_DTPMOD22 0xaa /* @ltoff(@dtpmod(sym + add)), imm22 */ +#define R_IA64_DTPREL14 0xb1 /* @dtprel(sym + add), imm14 */ +#define R_IA64_DTPREL22 0xb2 /* @dtprel(sym + add), imm22 */ +#define R_IA64_DTPREL64I 0xb3 /* @dtprel(sym + add), imm64 */ +#define R_IA64_DTPREL32MSB 0xb4 /* @dtprel(sym + add), data4 MSB */ +#define R_IA64_DTPREL32LSB 0xb5 /* @dtprel(sym + add), data4 LSB */ +#define R_IA64_DTPREL64MSB 0xb6 /* @dtprel(sym + add), data8 MSB */ +#define R_IA64_DTPREL64LSB 0xb7 /* @dtprel(sym + add), data8 LSB */ +#define R_IA64_LTOFF_DTPREL22 0xba /* @ltoff(@dtprel(s+a)), imm22 */ + +/* SH specific declarations */ + +/* Processor specific flags for the ELF header e_flags field. */ +#define EF_SH_MACH_MASK 0x1f +#define EF_SH_UNKNOWN 0x0 +#define EF_SH1 0x1 +#define EF_SH2 0x2 +#define EF_SH3 0x3 +#define EF_SH_DSP 0x4 +#define EF_SH3_DSP 0x5 +#define EF_SH4AL_DSP 0x6 +#define EF_SH3E 0x8 +#define EF_SH4 0x9 +#define EF_SH2E 0xb +#define EF_SH4A 0xc +#define EF_SH2A 0xd +#define EF_SH4_NOFPU 0x10 +#define EF_SH4A_NOFPU 0x11 +#define EF_SH4_NOMMU_NOFPU 0x12 +#define EF_SH2A_NOFPU 0x13 +#define EF_SH3_NOMMU 0x14 +#define EF_SH2A_SH4_NOFPU 0x15 +#define EF_SH2A_SH3_NOFPU 0x16 +#define EF_SH2A_SH4 0x17 +#define EF_SH2A_SH3E 0x18 + +/* SH relocs. */ +#define R_SH_NONE 0 +#define R_SH_DIR32 1 +#define R_SH_REL32 2 +#define R_SH_DIR8WPN 3 +#define R_SH_IND12W 4 +#define R_SH_DIR8WPL 5 +#define R_SH_DIR8WPZ 6 +#define R_SH_DIR8BP 7 +#define R_SH_DIR8W 8 +#define R_SH_DIR8L 9 +#define R_SH_SWITCH16 25 +#define R_SH_SWITCH32 26 +#define R_SH_USES 27 +#define R_SH_COUNT 28 +#define R_SH_ALIGN 29 +#define R_SH_CODE 30 +#define R_SH_DATA 31 +#define R_SH_LABEL 32 +#define R_SH_SWITCH8 33 +#define R_SH_GNU_VTINHERIT 34 +#define R_SH_GNU_VTENTRY 35 +#define R_SH_TLS_GD_32 144 +#define R_SH_TLS_LD_32 145 +#define R_SH_TLS_LDO_32 146 +#define R_SH_TLS_IE_32 147 +#define R_SH_TLS_LE_32 148 +#define R_SH_TLS_DTPMOD32 149 +#define R_SH_TLS_DTPOFF32 150 +#define R_SH_TLS_TPOFF32 151 +#define R_SH_GOT32 160 +#define R_SH_PLT32 161 +#define R_SH_COPY 162 +#define R_SH_GLOB_DAT 163 +#define R_SH_JMP_SLOT 164 +#define R_SH_RELATIVE 165 +#define R_SH_GOTOFF 166 +#define R_SH_GOTPC 167 +/* Keep this the last entry. */ +#define R_SH_NUM 256 + +/* S/390 specific definitions. */ + +/* Valid values for the e_flags field. */ + +#define EF_S390_HIGH_GPRS 0x00000001 /* High GPRs kernel facility needed. */ + +/* Additional s390 relocs */ + +#define R_390_NONE 0 /* No reloc. */ +#define R_390_8 1 /* Direct 8 bit. */ +#define R_390_12 2 /* Direct 12 bit. */ +#define R_390_16 3 /* Direct 16 bit. */ +#define R_390_32 4 /* Direct 32 bit. */ +#define R_390_PC32 5 /* PC relative 32 bit. */ +#define R_390_GOT12 6 /* 12 bit GOT offset. */ +#define R_390_GOT32 7 /* 32 bit GOT offset. */ +#define R_390_PLT32 8 /* 32 bit PC relative PLT address. */ +#define R_390_COPY 9 /* Copy symbol at runtime. */ +#define R_390_GLOB_DAT 10 /* Create GOT entry. */ +#define R_390_JMP_SLOT 11 /* Create PLT entry. */ +#define R_390_RELATIVE 12 /* Adjust by program base. */ +#define R_390_GOTOFF32 13 /* 32 bit offset to GOT. */ +#define R_390_GOTPC 14 /* 32 bit PC relative offset to GOT. */ +#define R_390_GOT16 15 /* 16 bit GOT offset. */ +#define R_390_PC16 16 /* PC relative 16 bit. */ +#define R_390_PC16DBL 17 /* PC relative 16 bit shifted by 1. */ +#define R_390_PLT16DBL 18 /* 16 bit PC rel. PLT shifted by 1. */ +#define R_390_PC32DBL 19 /* PC relative 32 bit shifted by 1. */ +#define R_390_PLT32DBL 20 /* 32 bit PC rel. PLT shifted by 1. */ +#define R_390_GOTPCDBL 21 /* 32 bit PC rel. GOT shifted by 1. */ +#define R_390_64 22 /* Direct 64 bit. */ +#define R_390_PC64 23 /* PC relative 64 bit. */ +#define R_390_GOT64 24 /* 64 bit GOT offset. */ +#define R_390_PLT64 25 /* 64 bit PC relative PLT address. */ +#define R_390_GOTENT 26 /* 32 bit PC rel. to GOT entry >> 1. */ +#define R_390_GOTOFF16 27 /* 16 bit offset to GOT. */ +#define R_390_GOTOFF64 28 /* 64 bit offset to GOT. */ +#define R_390_GOTPLT12 29 /* 12 bit offset to jump slot. */ +#define R_390_GOTPLT16 30 /* 16 bit offset to jump slot. */ +#define R_390_GOTPLT32 31 /* 32 bit offset to jump slot. */ +#define R_390_GOTPLT64 32 /* 64 bit offset to jump slot. */ +#define R_390_GOTPLTENT 33 /* 32 bit rel. offset to jump slot. */ +#define R_390_PLTOFF16 34 /* 16 bit offset from GOT to PLT. */ +#define R_390_PLTOFF32 35 /* 32 bit offset from GOT to PLT. */ +#define R_390_PLTOFF64 36 /* 16 bit offset from GOT to PLT. */ +#define R_390_TLS_LOAD 37 /* Tag for load insn in TLS code. */ +#define R_390_TLS_GDCALL 38 /* Tag for function call in general + dynamic TLS code. */ +#define R_390_TLS_LDCALL 39 /* Tag for function call in local + dynamic TLS code. */ +#define R_390_TLS_GD32 40 /* Direct 32 bit for general dynamic + thread local data. */ +#define R_390_TLS_GD64 41 /* Direct 64 bit for general dynamic + thread local data. */ +#define R_390_TLS_GOTIE12 42 /* 12 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_GOTIE32 43 /* 32 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_GOTIE64 44 /* 64 bit GOT offset for static TLS + block offset. */ +#define R_390_TLS_LDM32 45 /* Direct 32 bit for local dynamic + thread local data in LE code. */ +#define R_390_TLS_LDM64 46 /* Direct 64 bit for local dynamic + thread local data in LE code. */ +#define R_390_TLS_IE32 47 /* 32 bit address of GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_IE64 48 /* 64 bit address of GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_IEENT 49 /* 32 bit rel. offset to GOT entry for + negated static TLS block offset. */ +#define R_390_TLS_LE32 50 /* 32 bit negated offset relative to + static TLS block. */ +#define R_390_TLS_LE64 51 /* 64 bit negated offset relative to + static TLS block. */ +#define R_390_TLS_LDO32 52 /* 32 bit offset relative to TLS + block. */ +#define R_390_TLS_LDO64 53 /* 64 bit offset relative to TLS + block. */ +#define R_390_TLS_DTPMOD 54 /* ID of module containing symbol. */ +#define R_390_TLS_DTPOFF 55 /* Offset in TLS block. */ +#define R_390_TLS_TPOFF 56 /* Negated offset in static TLS + block. */ +#define R_390_20 57 /* Direct 20 bit. */ +#define R_390_GOT20 58 /* 20 bit GOT offset. */ +#define R_390_GOTPLT20 59 /* 20 bit offset to jump slot. */ +#define R_390_TLS_GOTIE20 60 /* 20 bit GOT offset for static TLS + block offset. */ +#define R_390_IRELATIVE 61 /* STT_GNU_IFUNC relocation. */ +/* Keep this the last entry. */ +#define R_390_NUM 62 + + +/* CRIS relocations. */ +#define R_CRIS_NONE 0 +#define R_CRIS_8 1 +#define R_CRIS_16 2 +#define R_CRIS_32 3 +#define R_CRIS_8_PCREL 4 +#define R_CRIS_16_PCREL 5 +#define R_CRIS_32_PCREL 6 +#define R_CRIS_GNU_VTINHERIT 7 +#define R_CRIS_GNU_VTENTRY 8 +#define R_CRIS_COPY 9 +#define R_CRIS_GLOB_DAT 10 +#define R_CRIS_JUMP_SLOT 11 +#define R_CRIS_RELATIVE 12 +#define R_CRIS_16_GOT 13 +#define R_CRIS_32_GOT 14 +#define R_CRIS_16_GOTPLT 15 +#define R_CRIS_32_GOTPLT 16 +#define R_CRIS_32_GOTREL 17 +#define R_CRIS_32_PLT_GOTREL 18 +#define R_CRIS_32_PLT_PCREL 19 + +#define R_CRIS_NUM 20 + /* AMD x86-64 relocations. */ #define R_X86_64_NONE 0 /* No reloc */ @@ -1074,658 +2764,353 @@ typedef struct #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset to GOT entry for IE symbol */ #define R_X86_64_TPOFF32 23 /* Offset in initial TLS block */ - -#define R_X86_64_NUM 24 - -/* For Sparc64, legal values for d_tag of Elf64_Dyn. */ - -#define DT_SPARC_REGISTER 0x70000001 -#define DT_SPARC_NUM 2 - -/* Bits present in AT_HWCAP, primarily for Sparc32. */ - -#define HWCAP_SPARC_FLUSH 1 /* The cpu supports flush insn. */ -#define HWCAP_SPARC_STBAR 2 -#define HWCAP_SPARC_SWAP 4 -#define HWCAP_SPARC_MULDIV 8 -#define HWCAP_SPARC_V9 16 /* The cpu is v9, so v8plus is ok. */ - -/* MIPS R3000 specific definitions. */ - -/* Legal values for e_flags field of Elf32_Ehdr. */ - -#define EF_MIPS_NOREORDER 1 /* A .noreorder directive was used */ -#define EF_MIPS_PIC 2 /* Contains PIC code */ -#define EF_MIPS_CPIC 4 /* Uses PIC calling sequence */ -#define EF_MIPS_XGOT 8 -#define EF_MIPS_64BIT_WHIRL 16 -#define EF_MIPS_ABI2 32 -#define EF_MIPS_ABI_ON32 64 -#define EF_MIPS_ARCH 0xf0000000 /* MIPS architecture level */ - -/* Legal values for MIPS architecture level. */ - -#define EF_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ -#define EF_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ -#define EF_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ -#define EF_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ -#define EF_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ - -/* The following are non-official names and should not be used. */ - -#define E_MIPS_ARCH_1 0x00000000 /* -mips1 code. */ -#define E_MIPS_ARCH_2 0x10000000 /* -mips2 code. */ -#define E_MIPS_ARCH_3 0x20000000 /* -mips3 code. */ -#define E_MIPS_ARCH_4 0x30000000 /* -mips4 code. */ -#define E_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ - -/* Special section indices. */ - -#define SHN_MIPS_ACOMMON 0xff00 /* Allocated common symbols */ -#define SHN_MIPS_TEXT 0xff01 /* Allocated test symbols. */ -#define SHN_MIPS_DATA 0xff02 /* Allocated data symbols. */ -#define SHN_MIPS_SCOMMON 0xff03 /* Small common symbols */ -#define SHN_MIPS_SUNDEFINED 0xff04 /* Small undefined symbols */ - -/* Legal values for sh_type field of Elf32_Shdr. */ - -#define SHT_MIPS_LIBLIST 0x70000000 /* Shared objects used in link */ -#define SHT_MIPS_MSYM 0x70000001 -#define SHT_MIPS_CONFLICT 0x70000002 /* Conflicting symbols */ -#define SHT_MIPS_GPTAB 0x70000003 /* Global data area sizes */ -#define SHT_MIPS_UCODE 0x70000004 /* Reserved for SGI/MIPS compilers */ -#define SHT_MIPS_DEBUG 0x70000005 /* MIPS ECOFF debugging information*/ -#define SHT_MIPS_REGINFO 0x70000006 /* Register usage information */ -#define SHT_MIPS_PACKAGE 0x70000007 -#define SHT_MIPS_PACKSYM 0x70000008 -#define SHT_MIPS_RELD 0x70000009 -#define SHT_MIPS_IFACE 0x7000000b -#define SHT_MIPS_CONTENT 0x7000000c -#define SHT_MIPS_OPTIONS 0x7000000d /* Miscellaneous options. */ -#define SHT_MIPS_SHDR 0x70000010 -#define SHT_MIPS_FDESC 0x70000011 -#define SHT_MIPS_EXTSYM 0x70000012 -#define SHT_MIPS_DENSE 0x70000013 -#define SHT_MIPS_PDESC 0x70000014 -#define SHT_MIPS_LOCSYM 0x70000015 -#define SHT_MIPS_AUXSYM 0x70000016 -#define SHT_MIPS_OPTSYM 0x70000017 -#define SHT_MIPS_LOCSTR 0x70000018 -#define SHT_MIPS_LINE 0x70000019 -#define SHT_MIPS_RFDESC 0x7000001a -#define SHT_MIPS_DELTASYM 0x7000001b -#define SHT_MIPS_DELTAINST 0x7000001c -#define SHT_MIPS_DELTACLASS 0x7000001d -#define SHT_MIPS_DWARF 0x7000001e /* DWARF debugging information. */ -#define SHT_MIPS_DELTADECL 0x7000001f -#define SHT_MIPS_SYMBOL_LIB 0x70000020 -#define SHT_MIPS_EVENTS 0x70000021 /* Event section. */ -#define SHT_MIPS_TRANSLATE 0x70000022 -#define SHT_MIPS_PIXIE 0x70000023 -#define SHT_MIPS_XLATE 0x70000024 -#define SHT_MIPS_XLATE_DEBUG 0x70000025 -#define SHT_MIPS_WHIRL 0x70000026 -#define SHT_MIPS_EH_REGION 0x70000027 -#define SHT_MIPS_XLATE_OLD 0x70000028 -#define SHT_MIPS_PDR_EXCEPTION 0x70000029 - -/* Legal values for sh_flags field of Elf32_Shdr. */ - -#define SHF_MIPS_GPREL 0x10000000 /* Must be part of global data area */ -#define SHF_MIPS_MERGE 0x20000000 -#define SHF_MIPS_ADDR 0x40000000 -#define SHF_MIPS_STRINGS 0x80000000 -#define SHF_MIPS_NOSTRIP 0x08000000 -#define SHF_MIPS_LOCAL 0x04000000 -#define SHF_MIPS_NAMES 0x02000000 -#define SHF_MIPS_NODUPE 0x01000000 - - -/* Symbol tables. */ - -/* MIPS specific values for `st_other'. */ -#define STO_MIPS_DEFAULT 0x0 -#define STO_MIPS_INTERNAL 0x1 -#define STO_MIPS_HIDDEN 0x2 -#define STO_MIPS_PROTECTED 0x3 -#define STO_MIPS_SC_ALIGN_UNUSED 0xff - -/* MIPS specific values for `st_info'. */ -#define STB_MIPS_SPLIT_COMMON 13 - -/* Entries found in sections of type SHT_MIPS_GPTAB. */ - -typedef union -{ - struct - { - Elf32_Word gt_current_g_value; /* -G value used for compilation */ - Elf32_Word gt_unused; /* Not used */ - } gt_header; /* First entry in section */ - struct - { - Elf32_Word gt_g_value; /* If this value were used for -G */ - Elf32_Word gt_bytes; /* This many bytes would be used */ - } gt_entry; /* Subsequent entries in section */ -} Elf32_gptab; - -/* Entry found in sections of type SHT_MIPS_REGINFO. */ - -typedef struct -{ - Elf32_Word ri_gprmask; /* General registers used */ - Elf32_Word ri_cprmask[4]; /* Coprocessor registers used */ - Elf32_Sword ri_gp_value; /* $gp register value */ -} Elf32_RegInfo; - -/* Entries found in sections of type SHT_MIPS_OPTIONS. */ - -typedef struct -{ - unsigned char kind; /* Determines interpretation of the - variable part of descriptor. */ - unsigned char size; /* Size of descriptor, including header. */ - Elf32_Section section; /* Section header index of section affected, - 0 for global options. */ - Elf32_Word info; /* Kind-specific information. */ -} Elf_Options; - -/* Values for `kind' field in Elf_Options. */ - -#define ODK_NULL 0 /* Undefined. */ -#define ODK_REGINFO 1 /* Register usage information. */ -#define ODK_EXCEPTIONS 2 /* Exception processing options. */ -#define ODK_PAD 3 /* Section padding options. */ -#define ODK_HWPATCH 4 /* Hardware workarounds performed */ -#define ODK_FILL 5 /* record the fill value used by the linker. */ -#define ODK_TAGS 6 /* reserve space for desktop tools to write. */ -#define ODK_HWAND 7 /* HW workarounds. 'AND' bits when merging. */ -#define ODK_HWOR 8 /* HW workarounds. 'OR' bits when merging. */ - -/* Values for `info' in Elf_Options for ODK_EXCEPTIONS entries. */ - -#define OEX_FPU_MIN 0x1f /* FPE's which MUST be enabled. */ -#define OEX_FPU_MAX 0x1f00 /* FPE's which MAY be enabled. */ -#define OEX_PAGE0 0x10000 /* page zero must be mapped. */ -#define OEX_SMM 0x20000 /* Force sequential memory mode? */ -#define OEX_FPDBUG 0x40000 /* Force floating point debug mode? */ -#define OEX_PRECISEFP OEX_FPDBUG -#define OEX_DISMISS 0x80000 /* Dismiss invalid address faults? */ - -#define OEX_FPU_INVAL 0x10 -#define OEX_FPU_DIV0 0x08 -#define OEX_FPU_OFLO 0x04 -#define OEX_FPU_UFLO 0x02 -#define OEX_FPU_INEX 0x01 - -/* Masks for `info' in Elf_Options for an ODK_HWPATCH entry. */ - -#define OHW_R4KEOP 0x1 /* R4000 end-of-page patch. */ -#define OHW_R8KPFETCH 0x2 /* may need R8000 prefetch patch. */ -#define OHW_R5KEOP 0x4 /* R5000 end-of-page patch. */ -#define OHW_R5KCVTL 0x8 /* R5000 cvt.[ds].l bug. clean=1. */ - -#define OPAD_PREFIX 0x1 -#define OPAD_POSTFIX 0x2 -#define OPAD_SYMBOL 0x4 - -/* Entry found in `.options' section. */ - -typedef struct -{ - Elf32_Word hwp_flags1; /* Extra flags. */ - Elf32_Word hwp_flags2; /* Extra flags. */ -} Elf_Options_Hw; - -/* Masks for `info' in ElfOptions for ODK_HWAND and ODK_HWOR entries. */ - -#define OHWA0_R4KEOP_CHECKED 0x00000001 -#define OHWA1_R4KEOP_CLEAN 0x00000002 - -/* MIPS relocs. */ - -#define R_MIPS_NONE 0 /* No reloc */ -#define R_MIPS_16 1 /* Direct 16 bit */ -#define R_MIPS_32 2 /* Direct 32 bit */ -#define R_MIPS_REL32 3 /* PC relative 32 bit */ -#define R_MIPS_26 4 /* Direct 26 bit shifted */ -#define R_MIPS_HI16 5 /* High 16 bit */ -#define R_MIPS_LO16 6 /* Low 16 bit */ -#define R_MIPS_GPREL16 7 /* GP relative 16 bit */ -#define R_MIPS_LITERAL 8 /* 16 bit literal entry */ -#define R_MIPS_GOT16 9 /* 16 bit GOT entry */ -#define R_MIPS_PC16 10 /* PC relative 16 bit */ -#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */ -#define R_MIPS_GPREL32 12 /* GP relative 32 bit */ - -#define R_MIPS_SHIFT5 16 -#define R_MIPS_SHIFT6 17 -#define R_MIPS_64 18 -#define R_MIPS_GOT_DISP 19 -#define R_MIPS_GOT_PAGE 20 -#define R_MIPS_GOT_OFST 21 -#define R_MIPS_GOT_HI16 22 -#define R_MIPS_GOT_LO16 23 -#define R_MIPS_SUB 24 -#define R_MIPS_INSERT_A 25 -#define R_MIPS_INSERT_B 26 -#define R_MIPS_DELETE 27 -#define R_MIPS_HIGHER 28 -#define R_MIPS_HIGHEST 29 -#define R_MIPS_CALL_HI16 30 -#define R_MIPS_CALL_LO16 31 -#define R_MIPS_SCN_DISP 32 -#define R_MIPS_REL16 33 -#define R_MIPS_ADD_IMMEDIATE 34 -#define R_MIPS_PJUMP 35 -#define R_MIPS_RELGOT 36 -#define R_MIPS_JALR 37 -/* Keep this the last entry. */ -#define R_MIPS_NUM 38 - -/* Legal values for p_type field of Elf32_Phdr. */ - -#define PT_MIPS_REGINFO 0x70000000 /* Register usage information */ -#define PT_MIPS_RTPROC 0x70000001 /* Runtime procedure table. */ -#define PT_MIPS_OPTIONS 0x70000002 - -/* Special program header types. */ - -#define PF_MIPS_LOCAL 0x10000000 - -/* Legal values for d_tag field of Elf32_Dyn. */ - -#define DT_MIPS_RLD_VERSION 0x70000001 /* Runtime linker interface version */ -#define DT_MIPS_TIME_STAMP 0x70000002 /* Timestamp */ -#define DT_MIPS_ICHECKSUM 0x70000003 /* Checksum */ -#define DT_MIPS_IVERSION 0x70000004 /* Version string (string tbl index) */ -#define DT_MIPS_FLAGS 0x70000005 /* Flags */ -#define DT_MIPS_BASE_ADDRESS 0x70000006 /* Base address */ -#define DT_MIPS_MSYM 0x70000007 -#define DT_MIPS_CONFLICT 0x70000008 /* Address of CONFLICT section */ -#define DT_MIPS_LIBLIST 0x70000009 /* Address of LIBLIST section */ -#define DT_MIPS_LOCAL_GOTNO 0x7000000a /* Number of local GOT entries */ -#define DT_MIPS_CONFLICTNO 0x7000000b /* Number of CONFLICT entries */ -#define DT_MIPS_LIBLISTNO 0x70000010 /* Number of LIBLIST entries */ -#define DT_MIPS_SYMTABNO 0x70000011 /* Number of DYNSYM entries */ -#define DT_MIPS_UNREFEXTNO 0x70000012 /* First external DYNSYM */ -#define DT_MIPS_GOTSYM 0x70000013 /* First GOT entry in DYNSYM */ -#define DT_MIPS_HIPAGENO 0x70000014 /* Number of GOT page table entries */ -#define DT_MIPS_RLD_MAP 0x70000016 /* Address of run time loader map. */ -#define DT_MIPS_DELTA_CLASS 0x70000017 /* Delta C++ class definition. */ -#define DT_MIPS_DELTA_CLASS_NO 0x70000018 /* Number of entries in - DT_MIPS_DELTA_CLASS. */ -#define DT_MIPS_DELTA_INSTANCE 0x70000019 /* Delta C++ class instances. */ -#define DT_MIPS_DELTA_INSTANCE_NO 0x7000001a /* Number of entries in - DT_MIPS_DELTA_INSTANCE. */ -#define DT_MIPS_DELTA_RELOC 0x7000001b /* Delta relocations. */ -#define DT_MIPS_DELTA_RELOC_NO 0x7000001c /* Number of entries in - DT_MIPS_DELTA_RELOC. */ -#define DT_MIPS_DELTA_SYM 0x7000001d /* Delta symbols that Delta - relocations refer to. */ -#define DT_MIPS_DELTA_SYM_NO 0x7000001e /* Number of entries in - DT_MIPS_DELTA_SYM. */ -#define DT_MIPS_DELTA_CLASSSYM 0x70000020 /* Delta symbols that hold the - class declaration. */ -#define DT_MIPS_DELTA_CLASSSYM_NO 0x70000021 /* Number of entries in - DT_MIPS_DELTA_CLASSSYM. */ -#define DT_MIPS_CXX_FLAGS 0x70000022 /* Flags indicating for C++ flavor. */ -#define DT_MIPS_PIXIE_INIT 0x70000023 -#define DT_MIPS_SYMBOL_LIB 0x70000024 -#define DT_MIPS_LOCALPAGE_GOTIDX 0x70000025 -#define DT_MIPS_LOCAL_GOTIDX 0x70000026 -#define DT_MIPS_HIDDEN_GOTIDX 0x70000027 -#define DT_MIPS_PROTECTED_GOTIDX 0x70000028 -#define DT_MIPS_OPTIONS 0x70000029 /* Address of .options. */ -#define DT_MIPS_INTERFACE 0x7000002a /* Address of .interface. */ -#define DT_MIPS_DYNSTR_ALIGN 0x7000002b -#define DT_MIPS_INTERFACE_SIZE 0x7000002c /* Size of the .interface section. */ -#define DT_MIPS_RLD_TEXT_RESOLVE_ADDR 0x7000002d /* Address of rld_text_rsolve - function stored in GOT. */ -#define DT_MIPS_PERF_SUFFIX 0x7000002e /* Default suffix of dso to be added - by rld on dlopen() calls. */ -#define DT_MIPS_COMPACT_SIZE 0x7000002f /* (O32)Size of compact rel section. */ -#define DT_MIPS_GP_VALUE 0x70000030 /* GP value for aux GOTs. */ -#define DT_MIPS_AUX_DYNAMIC 0x70000031 /* Address of aux .dynamic. */ -#define DT_MIPS_NUM 0x32 - -/* Legal values for DT_MIPS_FLAGS Elf32_Dyn entry. */ - -#define RHF_NONE 0 /* No flags */ -#define RHF_QUICKSTART (1 << 0) /* Use quickstart */ -#define RHF_NOTPOT (1 << 1) /* Hash size not power of 2 */ -#define RHF_NO_LIBRARY_REPLACEMENT (1 << 2) /* Ignore LD_LIBRARY_PATH */ -#define RHF_NO_MOVE (1 << 3) -#define RHF_SGI_ONLY (1 << 4) -#define RHF_GUARANTEE_INIT (1 << 5) -#define RHF_DELTA_C_PLUS_PLUS (1 << 6) -#define RHF_GUARANTEE_START_INIT (1 << 7) -#define RHF_PIXIE (1 << 8) -#define RHF_DEFAULT_DELAY_LOAD (1 << 9) -#define RHF_REQUICKSTART (1 << 10) -#define RHF_REQUICKSTARTED (1 << 11) -#define RHF_CORD (1 << 12) -#define RHF_NO_UNRES_UNDEF (1 << 13) -#define RHF_RLD_ORDER_SAFE (1 << 14) - -/* Entries found in sections of type SHT_MIPS_LIBLIST. */ - -typedef struct -{ - Elf32_Word l_name; /* Name (string table index) */ - Elf32_Word l_time_stamp; /* Timestamp */ - Elf32_Word l_checksum; /* Checksum */ - Elf32_Word l_version; /* Interface version */ - Elf32_Word l_flags; /* Flags */ -} Elf32_Lib; - -typedef struct -{ - Elf64_Word l_name; /* Name (string table index) */ - Elf64_Word l_time_stamp; /* Timestamp */ - Elf64_Word l_checksum; /* Checksum */ - Elf64_Word l_version; /* Interface version */ - Elf64_Word l_flags; /* Flags */ -} Elf64_Lib; - - -/* Legal values for l_flags. */ - -#define LL_NONE 0 -#define LL_EXACT_MATCH (1 << 0) /* Require exact match */ -#define LL_IGNORE_INT_VER (1 << 1) /* Ignore interface version */ -#define LL_REQUIRE_MINOR (1 << 2) -#define LL_EXPORTS (1 << 3) -#define LL_DELAY_LOAD (1 << 4) -#define LL_DELTA (1 << 5) - -/* Entries found in sections of type SHT_MIPS_CONFLICT. */ - -typedef Elf32_Addr Elf32_Conflict; - - -/* HPPA specific definitions. */ - -/* Legal values for e_flags field of Elf32_Ehdr. */ - -#define EF_PARISC_TRAPNL 1 /* Trap nil pointer dereference. */ -#define EF_PARISC_EXT 2 /* Program uses arch. extensions. */ -#define EF_PARISC_ARCH 0xffff0000 /* Architecture version. */ -/* Defined values are: - 0x020b PA-RISC 1.0 big-endian - 0x0210 PA-RISC 1.1 big-endian - 0x028b PA-RISC 1.0 little-endian - 0x0290 PA-RISC 1.1 little-endian -*/ - -/* Legal values for sh_type field of Elf32_Shdr. */ - -#define SHT_PARISC_GOT 0x70000000 /* GOT for external data. */ -#define SHT_PARISC_ARCH 0x70000001 /* Architecture extensions. */ -#define SHT_PARISC_GLOBAL 0x70000002 /* Definition of $global$. */ -#define SHT_PARISC_MILLI 0x70000003 /* Millicode routines. */ -#define SHT_PARISC_UNWIND 0x70000004 /* Unwind information. */ -#define SHT_PARISC_PLT 0x70000005 /* Procedure linkage table. */ -#define SHT_PARISC_SDATA 0x70000006 /* Short initialized data. */ -#define SHT_PARISC_SBSS 0x70000007 /* Short uninitialized data. */ -#define SHT_PARISC_SYMEXTN 0x70000008 /* Argument/relocation info. */ -#define SHT_PARISC_STUBS 0x70000009 /* Linker stubs. */ - -/* Legal values for sh_flags field of Elf32_Shdr. */ - -#define SHF_PARISC_GLOBAL 0x10000000 /* Section defines dp. */ -#define SHF_PARISC_SHORT 0x20000000 /* Section with short addressing. */ - -/* Legal values for ST_TYPE subfield of st_info (symbol type). */ - -#define STT_PARISC_MILLICODE 13 /* Millicode function entry point. */ - -/* HPPA relocs. */ - -#define R_PARISC_NONE 0 /* No reloc. */ -#define R_PARISC_DIR32 1 /* Direct 32-bit reference. */ -#define R_PARISC_DIR21L 2 /* Left 21 bits of eff. address. */ -#define R_PARISC_DIR17R 3 /* Right 17 bits of eff. address. */ -#define R_PARISC_DIR14R 4 /* Right 14 bits of eff. address. */ -#define R_PARISC_PCREL21L 5 /* PC-relative, left 21 bits. */ -#define R_PARISC_PCREL14R 6 /* PC-relative, right 14 bits. */ -#define R_PARISC_PCREL17C 7 /* Conditional PC-relative, ignore - if displacement > 17bits. */ -#define R_PARISC_PCREL17F 8 /* Conditional PC-relative, must - fit in 17bits. */ -#define R_PARISC_DPREL21L 9 /* DP-relative, left 21 bits. */ -#define R_PARISC_DPREL14R 10 /* DP-relative, right 14 bits. */ -#define R_PARISC_DPREL14F 11 /* DP-relative, must bit in 14 bits. */ -#define R_PARISC_DLTREL21L 12 /* DLT-relative, left 21 bits. */ -#define R_PARISC_DLTREL14R 13 /* DLT-relative, right 14 bits. */ -#define R_PARISC_DLTREL14F 14 /* DLT-relative, must fit in 14 bits.*/ -#define R_PARISC_DLTIND21L 15 /* DLT-relative indirect, left - 21 bits. */ -#define R_PARISC_DLTIND14R 16 /* DLT-relative indirect, right - 14 bits. */ -#define R_PARISC_DLTIND14F 17 /* DLT-relative indirect, must fit - int 14 bits. */ -#define R_PARISC_PLABEL32 18 /* Direct 32-bit reference to proc. */ - -/* Alpha specific definitions. */ - -/* Legal values for e_flags field of Elf64_Ehdr. */ - -#define EF_ALPHA_32BIT 1 /* All addresses must be < 2GB. */ -#define EF_ALPHA_CANRELAX 2 /* Relocations for relaxing exist. */ - -/* Legal values for sh_type field of Elf64_Shdr. */ - -/* These two are primerily concerned with ECOFF debugging info. */ -#define SHT_ALPHA_DEBUG 0x70000001 -#define SHT_ALPHA_REGINFO 0x70000002 - -/* Legal values for sh_flags field of Elf64_Shdr. */ - -#define SHF_ALPHA_GPREL 0x10000000 - -/* Legal values for st_other field of Elf64_Sym. */ -#define STO_ALPHA_NOPV 0x80 /* No PV required. */ -#define STO_ALPHA_STD_GPLOAD 0x88 /* PV only used for initial ldgp. */ - -/* Alpha relocs. */ - -#define R_ALPHA_NONE 0 /* No reloc */ -#define R_ALPHA_REFLONG 1 /* Direct 32 bit */ -#define R_ALPHA_REFQUAD 2 /* Direct 64 bit */ -#define R_ALPHA_GPREL32 3 /* GP relative 32 bit */ -#define R_ALPHA_LITERAL 4 /* GP relative 16 bit w/optimization */ -#define R_ALPHA_LITUSE 5 /* Optimization hint for LITERAL */ -#define R_ALPHA_GPDISP 6 /* Add displacement to GP */ -#define R_ALPHA_BRADDR 7 /* PC+4 relative 23 bit shifted */ -#define R_ALPHA_HINT 8 /* PC+4 relative 16 bit shifted */ -#define R_ALPHA_SREL16 9 /* PC relative 16 bit */ -#define R_ALPHA_SREL32 10 /* PC relative 32 bit */ -#define R_ALPHA_SREL64 11 /* PC relative 64 bit */ -#define R_ALPHA_OP_PUSH 12 /* OP stack push */ -#define R_ALPHA_OP_STORE 13 /* OP stack pop and store */ -#define R_ALPHA_OP_PSUB 14 /* OP stack subtract */ -#define R_ALPHA_OP_PRSHIFT 15 /* OP stack right shift */ -#define R_ALPHA_GPVALUE 16 -#define R_ALPHA_GPRELHIGH 17 -#define R_ALPHA_GPRELLOW 18 -#define R_ALPHA_IMMED_GP_16 19 -#define R_ALPHA_IMMED_GP_HI32 20 -#define R_ALPHA_IMMED_SCN_HI32 21 -#define R_ALPHA_IMMED_BR_HI32 22 -#define R_ALPHA_IMMED_LO32 23 -#define R_ALPHA_COPY 24 /* Copy symbol at runtime */ -#define R_ALPHA_GLOB_DAT 25 /* Create GOT entry */ -#define R_ALPHA_JMP_SLOT 26 /* Create PLT entry */ -#define R_ALPHA_RELATIVE 27 /* Adjust by program base */ -/* Keep this the last entry. */ -#define R_ALPHA_NUM 28 - - -/* PowerPC specific declarations */ - -/* PowerPC relocations defined by the ABIs */ -#define R_PPC_NONE 0 -#define R_PPC_ADDR32 1 /* 32bit absolute address */ -#define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored. */ -#define R_PPC_ADDR16 3 /* 16bit absolute address */ -#define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */ -#define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */ -#define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */ -#define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */ -#define R_PPC_ADDR14_BRTAKEN 8 -#define R_PPC_ADDR14_BRNTAKEN 9 -#define R_PPC_REL24 10 /* PC relative 26 bit */ -#define R_PPC_REL14 11 /* PC relative 16 bit */ -#define R_PPC_REL14_BRTAKEN 12 -#define R_PPC_REL14_BRNTAKEN 13 -#define R_PPC_GOT16 14 -#define R_PPC_GOT16_LO 15 -#define R_PPC_GOT16_HI 16 -#define R_PPC_GOT16_HA 17 -#define R_PPC_PLTREL24 18 -#define R_PPC_COPY 19 -#define R_PPC_GLOB_DAT 20 -#define R_PPC_JMP_SLOT 21 -#define R_PPC_RELATIVE 22 -#define R_PPC_LOCAL24PC 23 -#define R_PPC_UADDR32 24 -#define R_PPC_UADDR16 25 -#define R_PPC_REL32 26 -#define R_PPC_PLT32 27 -#define R_PPC_PLTREL32 28 -#define R_PPC_PLT16_LO 29 -#define R_PPC_PLT16_HI 30 -#define R_PPC_PLT16_HA 31 -#define R_PPC_SDAREL16 32 -#define R_PPC_SECTOFF 33 -#define R_PPC_SECTOFF_LO 34 -#define R_PPC_SECTOFF_HI 35 -#define R_PPC_SECTOFF_HA 36 -/* Keep this the last entry. */ -#define R_PPC_NUM 37 - -/* The remaining relocs are from the Embedded ELF ABI, and are not - in the SVR4 ELF ABI. */ -#define R_PPC_EMB_NADDR32 101 -#define R_PPC_EMB_NADDR16 102 -#define R_PPC_EMB_NADDR16_LO 103 -#define R_PPC_EMB_NADDR16_HI 104 -#define R_PPC_EMB_NADDR16_HA 105 -#define R_PPC_EMB_SDAI16 106 -#define R_PPC_EMB_SDA2I16 107 -#define R_PPC_EMB_SDA2REL 108 -#define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */ -#define R_PPC_EMB_MRKREF 110 -#define R_PPC_EMB_RELSEC16 111 -#define R_PPC_EMB_RELST_LO 112 -#define R_PPC_EMB_RELST_HI 113 -#define R_PPC_EMB_RELST_HA 114 -#define R_PPC_EMB_BIT_FLD 115 -#define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */ - -/* Diab tool relocations. */ -#define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */ -#define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */ -#define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */ -#define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */ -#define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */ -#define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */ - -/* This is a phony reloc to handle any old fashioned TOC16 references - that may still be in object files. */ -#define R_PPC_TOC16 255 - - -/* ARM specific declarations */ - -/* Processor specific flags for the ELF header e_flags field. */ -#define EF_ARM_RELEXEC 0x01 -#define EF_ARM_HASENTRY 0x02 -#define EF_ARM_INTERWORK 0x04 -#define EF_ARM_APCS_26 0x08 -#define EF_ARM_APCS_FLOAT 0x10 -#define EF_ARM_PIC 0x20 -#define EF_ALIGN8 0x40 /* 8-bit structure alignment is in use */ -#define EF_NEW_ABI 0x80 -#define EF_OLD_ABI 0x100 - -/* Additional symbol types for Thumb */ -#define STT_ARM_TFUNC 0xd - -/* ARM-specific values for sh_flags */ -#define SHF_ARM_ENTRYSECT 0x10000000 /* Section contains an entry point */ -#define SHF_ARM_COMDEF 0x80000000 /* Section may be multiply defined - in the input to a link step */ - -/* ARM-specific program header flags */ -#define PF_ARM_SB 0x10000000 /* Segment contains the location - addressed by the static base */ - -/* ARM relocs. */ -#define R_ARM_NONE 0 /* No reloc */ -#define R_ARM_PC24 1 /* PC relative 26 bit branch */ -#define R_ARM_ABS32 2 /* Direct 32 bit */ -#define R_ARM_REL32 3 /* PC relative 32 bit */ -#define R_ARM_PC13 4 -#define R_ARM_ABS16 5 /* Direct 16 bit */ -#define R_ARM_ABS12 6 /* Direct 12 bit */ -#define R_ARM_THM_ABS5 7 -#define R_ARM_ABS8 8 /* Direct 8 bit */ -#define R_ARM_SBREL32 9 -#define R_ARM_THM_CALL 10 -#define R_ARM_THM_PC8 11 -#define R_ARM_AMP_VCALL9 12 -#define R_ARM_SWI24 13 -#define R_ARM_THM_SWI8 14 -#define R_ARM_XPC25 15 -#define R_ARM_THM_XPC22 16 -#define R_ARM_COPY 20 /* Copy symbol at runtime */ -#define R_ARM_GLOB_DAT 21 /* Create GOT entry */ -#define R_ARM_JUMP_SLOT 22 /* Create PLT entry */ -#define R_ARM_RELATIVE 23 /* Adjust by program base */ -#define R_ARM_GOTOFF32 24 /* 32 bit offset to GOT */ -#define R_ARM_BASE_PREL 25 /* 32 bit PC relative offset to GOT */ -#define R_ARM_GOT_BREL 26 /* 32 bit GOT entry */ -#define R_ARM_PLT32 27 /* 32 bit PLT address */ -#define R_ARM_CALL 28 -#define R_ARM_JUMP24 29 -#define R_ARM_THM_JUMP24 30 -#define R_ARM_V4BX 40 -#define R_ARM_PREL31 42 -#define R_ARM_MOVW_ABS_NC 43 -#define R_ARM_MOVT_ABS 44 -#define R_ARM_THM_MOVW_ABS_NC 47 -#define R_ARM_THM_MOVT_ABS 48 -#define R_ARM_GNU_VTENTRY 100 -#define R_ARM_GNU_VTINHERIT 101 -#define R_ARM_THM_PC11 102 /* thumb unconditional branch */ -#define R_ARM_THM_PC9 103 /* thumb conditional branch */ -#define R_ARM_RXPC25 249 -#define R_ARM_RSBREL32 250 -#define R_ARM_THM_RPC22 251 -#define R_ARM_RREL32 252 -#define R_ARM_RABS22 253 -#define R_ARM_RPC24 254 -#define R_ARM_RBASE 255 -/* Keep this the last entry. */ -#define R_ARM_NUM 256 - -/* TMS320C67xx specific declarations */ -/* XXX: no ELF standard yet */ - -/* TMS320C67xx relocs. */ -#define R_C60_32 1 -#define R_C60_GOT32 3 /* 32 bit GOT entry */ -#define R_C60_PLT32 4 /* 32 bit PLT address */ -#define R_C60_COPY 5 /* Copy symbol at runtime */ -#define R_C60_GLOB_DAT 6 /* Create GOT entry */ -#define R_C60_JMP_SLOT 7 /* Create PLT entry */ -#define R_C60_RELATIVE 8 /* Adjust by program base */ -#define R_C60_GOTOFF 9 /* 32 bit offset to GOT */ -#define R_C60_GOTPC 10 /* 32 bit PC relative offset to GOT */ - -#define R_C60HI16 0x55 /* high 16 bit MVKH embedded */ -#define R_C60LO16 0x54 /* low 16 bit MVKL embedded */ - -#endif /* elf.h */ +#define R_X86_64_PC64 24 /* PC relative 64 bit */ +#define R_X86_64_GOTOFF64 25 /* 64 bit offset to GOT */ +#define R_X86_64_GOTPC32 26 /* 32 bit signed pc relative + offset to GOT */ +#define R_X86_64_GOT64 27 /* 64-bit GOT entry offset */ +#define R_X86_64_GOTPCREL64 28 /* 64-bit PC relative offset + to GOT entry */ +#define R_X86_64_GOTPC64 29 /* 64-bit PC relative offset to GOT */ +#define R_X86_64_GOTPLT64 30 /* like GOT64, says PLT entry needed */ +#define R_X86_64_PLTOFF64 31 /* 64-bit GOT relative offset + to PLT entry */ +#define R_X86_64_SIZE32 32 /* Size of symbol plus 32-bit addend */ +#define R_X86_64_SIZE64 33 /* Size of symbol plus 64-bit addend */ +#define R_X86_64_GOTPC32_TLSDESC 34 /* GOT offset for TLS descriptor. */ +#define R_X86_64_TLSDESC_CALL 35 /* Marker for call through TLS + descriptor. */ +#define R_X86_64_TLSDESC 36 /* TLS descriptor. */ +#define R_X86_64_IRELATIVE 37 /* Adjust indirectly by program base */ +#define R_X86_64_RELATIVE64 38 /* 64-bit adjust by program base */ + +#define R_X86_64_NUM 39 + + +/* AM33 relocations. */ +#define R_MN10300_NONE 0 /* No reloc. */ +#define R_MN10300_32 1 /* Direct 32 bit. */ +#define R_MN10300_16 2 /* Direct 16 bit. */ +#define R_MN10300_8 3 /* Direct 8 bit. */ +#define R_MN10300_PCREL32 4 /* PC-relative 32-bit. */ +#define R_MN10300_PCREL16 5 /* PC-relative 16-bit signed. */ +#define R_MN10300_PCREL8 6 /* PC-relative 8-bit signed. */ +#define R_MN10300_GNU_VTINHERIT 7 /* Ancient C++ vtable garbage... */ +#define R_MN10300_GNU_VTENTRY 8 /* ... collection annotation. */ +#define R_MN10300_24 9 /* Direct 24 bit. */ +#define R_MN10300_GOTPC32 10 /* 32-bit PCrel offset to GOT. */ +#define R_MN10300_GOTPC16 11 /* 16-bit PCrel offset to GOT. */ +#define R_MN10300_GOTOFF32 12 /* 32-bit offset from GOT. */ +#define R_MN10300_GOTOFF24 13 /* 24-bit offset from GOT. */ +#define R_MN10300_GOTOFF16 14 /* 16-bit offset from GOT. */ +#define R_MN10300_PLT32 15 /* 32-bit PCrel to PLT entry. */ +#define R_MN10300_PLT16 16 /* 16-bit PCrel to PLT entry. */ +#define R_MN10300_GOT32 17 /* 32-bit offset to GOT entry. */ +#define R_MN10300_GOT24 18 /* 24-bit offset to GOT entry. */ +#define R_MN10300_GOT16 19 /* 16-bit offset to GOT entry. */ +#define R_MN10300_COPY 20 /* Copy symbol at runtime. */ +#define R_MN10300_GLOB_DAT 21 /* Create GOT entry. */ +#define R_MN10300_JMP_SLOT 22 /* Create PLT entry. */ +#define R_MN10300_RELATIVE 23 /* Adjust by program base. */ +#define R_MN10300_TLS_GD 24 /* 32-bit offset for global dynamic. */ +#define R_MN10300_TLS_LD 25 /* 32-bit offset for local dynamic. */ +#define R_MN10300_TLS_LDO 26 /* Module-relative offset. */ +#define R_MN10300_TLS_GOTIE 27 /* GOT offset for static TLS block + offset. */ +#define R_MN10300_TLS_IE 28 /* GOT address for static TLS block + offset. */ +#define R_MN10300_TLS_LE 29 /* Offset relative to static TLS + block. */ +#define R_MN10300_TLS_DTPMOD 30 /* ID of module containing symbol. */ +#define R_MN10300_TLS_DTPOFF 31 /* Offset in module TLS block. */ +#define R_MN10300_TLS_TPOFF 32 /* Offset in static TLS block. */ +#define R_MN10300_SYM_DIFF 33 /* Adjustment for next reloc as needed + by linker relaxation. */ +#define R_MN10300_ALIGN 34 /* Alignment requirement for linker + relaxation. */ +#define R_MN10300_NUM 35 + + +/* M32R relocs. */ +#define R_M32R_NONE 0 /* No reloc. */ +#define R_M32R_16 1 /* Direct 16 bit. */ +#define R_M32R_32 2 /* Direct 32 bit. */ +#define R_M32R_24 3 /* Direct 24 bit. */ +#define R_M32R_10_PCREL 4 /* PC relative 10 bit shifted. */ +#define R_M32R_18_PCREL 5 /* PC relative 18 bit shifted. */ +#define R_M32R_26_PCREL 6 /* PC relative 26 bit shifted. */ +#define R_M32R_HI16_ULO 7 /* High 16 bit with unsigned low. */ +#define R_M32R_HI16_SLO 8 /* High 16 bit with signed low. */ +#define R_M32R_LO16 9 /* Low 16 bit. */ +#define R_M32R_SDA16 10 /* 16 bit offset in SDA. */ +#define R_M32R_GNU_VTINHERIT 11 +#define R_M32R_GNU_VTENTRY 12 +/* M32R relocs use SHT_RELA. */ +#define R_M32R_16_RELA 33 /* Direct 16 bit. */ +#define R_M32R_32_RELA 34 /* Direct 32 bit. */ +#define R_M32R_24_RELA 35 /* Direct 24 bit. */ +#define R_M32R_10_PCREL_RELA 36 /* PC relative 10 bit shifted. */ +#define R_M32R_18_PCREL_RELA 37 /* PC relative 18 bit shifted. */ +#define R_M32R_26_PCREL_RELA 38 /* PC relative 26 bit shifted. */ +#define R_M32R_HI16_ULO_RELA 39 /* High 16 bit with unsigned low */ +#define R_M32R_HI16_SLO_RELA 40 /* High 16 bit with signed low */ +#define R_M32R_LO16_RELA 41 /* Low 16 bit */ +#define R_M32R_SDA16_RELA 42 /* 16 bit offset in SDA */ +#define R_M32R_RELA_GNU_VTINHERIT 43 +#define R_M32R_RELA_GNU_VTENTRY 44 +#define R_M32R_REL32 45 /* PC relative 32 bit. */ + +#define R_M32R_GOT24 48 /* 24 bit GOT entry */ +#define R_M32R_26_PLTREL 49 /* 26 bit PC relative to PLT shifted */ +#define R_M32R_COPY 50 /* Copy symbol at runtime */ +#define R_M32R_GLOB_DAT 51 /* Create GOT entry */ +#define R_M32R_JMP_SLOT 52 /* Create PLT entry */ +#define R_M32R_RELATIVE 53 /* Adjust by program base */ +#define R_M32R_GOTOFF 54 /* 24 bit offset to GOT */ +#define R_M32R_GOTPC24 55 /* 24 bit PC relative offset to GOT */ +#define R_M32R_GOT16_HI_ULO 56 /* High 16 bit GOT entry with unsigned + low */ +#define R_M32R_GOT16_HI_SLO 57 /* High 16 bit GOT entry with signed + low */ +#define R_M32R_GOT16_LO 58 /* Low 16 bit GOT entry */ +#define R_M32R_GOTPC_HI_ULO 59 /* High 16 bit PC relative offset to + GOT with unsigned low */ +#define R_M32R_GOTPC_HI_SLO 60 /* High 16 bit PC relative offset to + GOT with signed low */ +#define R_M32R_GOTPC_LO 61 /* Low 16 bit PC relative offset to + GOT */ +#define R_M32R_GOTOFF_HI_ULO 62 /* High 16 bit offset to GOT + with unsigned low */ +#define R_M32R_GOTOFF_HI_SLO 63 /* High 16 bit offset to GOT + with signed low */ +#define R_M32R_GOTOFF_LO 64 /* Low 16 bit offset to GOT */ +#define R_M32R_NUM 256 /* Keep this the last entry. */ + + +/* TILEPro relocations. */ +#define R_TILEPRO_NONE 0 /* No reloc */ +#define R_TILEPRO_32 1 /* Direct 32 bit */ +#define R_TILEPRO_16 2 /* Direct 16 bit */ +#define R_TILEPRO_8 3 /* Direct 8 bit */ +#define R_TILEPRO_32_PCREL 4 /* PC relative 32 bit */ +#define R_TILEPRO_16_PCREL 5 /* PC relative 16 bit */ +#define R_TILEPRO_8_PCREL 6 /* PC relative 8 bit */ +#define R_TILEPRO_LO16 7 /* Low 16 bit */ +#define R_TILEPRO_HI16 8 /* High 16 bit */ +#define R_TILEPRO_HA16 9 /* High 16 bit, adjusted */ +#define R_TILEPRO_COPY 10 /* Copy relocation */ +#define R_TILEPRO_GLOB_DAT 11 /* Create GOT entry */ +#define R_TILEPRO_JMP_SLOT 12 /* Create PLT entry */ +#define R_TILEPRO_RELATIVE 13 /* Adjust by program base */ +#define R_TILEPRO_BROFF_X1 14 /* X1 pipe branch offset */ +#define R_TILEPRO_JOFFLONG_X1 15 /* X1 pipe jump offset */ +#define R_TILEPRO_JOFFLONG_X1_PLT 16 /* X1 pipe jump offset to PLT */ +#define R_TILEPRO_IMM8_X0 17 /* X0 pipe 8-bit */ +#define R_TILEPRO_IMM8_Y0 18 /* Y0 pipe 8-bit */ +#define R_TILEPRO_IMM8_X1 19 /* X1 pipe 8-bit */ +#define R_TILEPRO_IMM8_Y1 20 /* Y1 pipe 8-bit */ +#define R_TILEPRO_MT_IMM15_X1 21 /* X1 pipe mtspr */ +#define R_TILEPRO_MF_IMM15_X1 22 /* X1 pipe mfspr */ +#define R_TILEPRO_IMM16_X0 23 /* X0 pipe 16-bit */ +#define R_TILEPRO_IMM16_X1 24 /* X1 pipe 16-bit */ +#define R_TILEPRO_IMM16_X0_LO 25 /* X0 pipe low 16-bit */ +#define R_TILEPRO_IMM16_X1_LO 26 /* X1 pipe low 16-bit */ +#define R_TILEPRO_IMM16_X0_HI 27 /* X0 pipe high 16-bit */ +#define R_TILEPRO_IMM16_X1_HI 28 /* X1 pipe high 16-bit */ +#define R_TILEPRO_IMM16_X0_HA 29 /* X0 pipe high 16-bit, adjusted */ +#define R_TILEPRO_IMM16_X1_HA 30 /* X1 pipe high 16-bit, adjusted */ +#define R_TILEPRO_IMM16_X0_PCREL 31 /* X0 pipe PC relative 16 bit */ +#define R_TILEPRO_IMM16_X1_PCREL 32 /* X1 pipe PC relative 16 bit */ +#define R_TILEPRO_IMM16_X0_LO_PCREL 33 /* X0 pipe PC relative low 16 bit */ +#define R_TILEPRO_IMM16_X1_LO_PCREL 34 /* X1 pipe PC relative low 16 bit */ +#define R_TILEPRO_IMM16_X0_HI_PCREL 35 /* X0 pipe PC relative high 16 bit */ +#define R_TILEPRO_IMM16_X1_HI_PCREL 36 /* X1 pipe PC relative high 16 bit */ +#define R_TILEPRO_IMM16_X0_HA_PCREL 37 /* X0 pipe PC relative ha() 16 bit */ +#define R_TILEPRO_IMM16_X1_HA_PCREL 38 /* X1 pipe PC relative ha() 16 bit */ +#define R_TILEPRO_IMM16_X0_GOT 39 /* X0 pipe 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT 40 /* X1 pipe 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_LO 41 /* X0 pipe low 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_LO 42 /* X1 pipe low 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_HI 43 /* X0 pipe high 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_HI 44 /* X1 pipe high 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X0_GOT_HA 45 /* X0 pipe ha() 16-bit GOT offset */ +#define R_TILEPRO_IMM16_X1_GOT_HA 46 /* X1 pipe ha() 16-bit GOT offset */ +#define R_TILEPRO_MMSTART_X0 47 /* X0 pipe mm "start" */ +#define R_TILEPRO_MMEND_X0 48 /* X0 pipe mm "end" */ +#define R_TILEPRO_MMSTART_X1 49 /* X1 pipe mm "start" */ +#define R_TILEPRO_MMEND_X1 50 /* X1 pipe mm "end" */ +#define R_TILEPRO_SHAMT_X0 51 /* X0 pipe shift amount */ +#define R_TILEPRO_SHAMT_X1 52 /* X1 pipe shift amount */ +#define R_TILEPRO_SHAMT_Y0 53 /* Y0 pipe shift amount */ +#define R_TILEPRO_SHAMT_Y1 54 /* Y1 pipe shift amount */ +#define R_TILEPRO_DEST_IMM8_X1 55 /* X1 pipe destination 8-bit */ +/* Relocs 56-59 are currently not defined. */ +#define R_TILEPRO_TLS_GD_CALL 60 /* "jal" for TLS GD */ +#define R_TILEPRO_IMM8_X0_TLS_GD_ADD 61 /* X0 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_X1_TLS_GD_ADD 62 /* X1 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_Y0_TLS_GD_ADD 63 /* Y0 pipe "addi" for TLS GD */ +#define R_TILEPRO_IMM8_Y1_TLS_GD_ADD 64 /* Y1 pipe "addi" for TLS GD */ +#define R_TILEPRO_TLS_IE_LOAD 65 /* "lw_tls" for TLS IE */ +#define R_TILEPRO_IMM16_X0_TLS_GD 66 /* X0 pipe 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD 67 /* X1 pipe 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_LO 68 /* X0 pipe low 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_LO 69 /* X1 pipe low 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_HI 70 /* X0 pipe high 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_HI 71 /* X1 pipe high 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_GD_HA 72 /* X0 pipe ha() 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X1_TLS_GD_HA 73 /* X1 pipe ha() 16-bit TLS GD offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE 74 /* X0 pipe 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE 75 /* X1 pipe 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_LO 76 /* X0 pipe low 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_LO 77 /* X1 pipe low 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_HI 78 /* X0 pipe high 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_HI 79 /* X1 pipe high 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X0_TLS_IE_HA 80 /* X0 pipe ha() 16-bit TLS IE offset */ +#define R_TILEPRO_IMM16_X1_TLS_IE_HA 81 /* X1 pipe ha() 16-bit TLS IE offset */ +#define R_TILEPRO_TLS_DTPMOD32 82 /* ID of module containing symbol */ +#define R_TILEPRO_TLS_DTPOFF32 83 /* Offset in TLS block */ +#define R_TILEPRO_TLS_TPOFF32 84 /* Offset in static TLS block */ +#define R_TILEPRO_IMM16_X0_TLS_LE 85 /* X0 pipe 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE 86 /* X1 pipe 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_LO 87 /* X0 pipe low 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_LO 88 /* X1 pipe low 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_HI 89 /* X0 pipe high 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_HI 90 /* X1 pipe high 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X0_TLS_LE_HA 91 /* X0 pipe ha() 16-bit TLS LE offset */ +#define R_TILEPRO_IMM16_X1_TLS_LE_HA 92 /* X1 pipe ha() 16-bit TLS LE offset */ + +#define R_TILEPRO_GNU_VTINHERIT 128 /* GNU C++ vtable hierarchy */ +#define R_TILEPRO_GNU_VTENTRY 129 /* GNU C++ vtable member usage */ + +#define R_TILEPRO_NUM 130 + + +/* TILE-Gx relocations. */ +#define R_TILEGX_NONE 0 /* No reloc */ +#define R_TILEGX_64 1 /* Direct 64 bit */ +#define R_TILEGX_32 2 /* Direct 32 bit */ +#define R_TILEGX_16 3 /* Direct 16 bit */ +#define R_TILEGX_8 4 /* Direct 8 bit */ +#define R_TILEGX_64_PCREL 5 /* PC relative 64 bit */ +#define R_TILEGX_32_PCREL 6 /* PC relative 32 bit */ +#define R_TILEGX_16_PCREL 7 /* PC relative 16 bit */ +#define R_TILEGX_8_PCREL 8 /* PC relative 8 bit */ +#define R_TILEGX_HW0 9 /* hword 0 16-bit */ +#define R_TILEGX_HW1 10 /* hword 1 16-bit */ +#define R_TILEGX_HW2 11 /* hword 2 16-bit */ +#define R_TILEGX_HW3 12 /* hword 3 16-bit */ +#define R_TILEGX_HW0_LAST 13 /* last hword 0 16-bit */ +#define R_TILEGX_HW1_LAST 14 /* last hword 1 16-bit */ +#define R_TILEGX_HW2_LAST 15 /* last hword 2 16-bit */ +#define R_TILEGX_COPY 16 /* Copy relocation */ +#define R_TILEGX_GLOB_DAT 17 /* Create GOT entry */ +#define R_TILEGX_JMP_SLOT 18 /* Create PLT entry */ +#define R_TILEGX_RELATIVE 19 /* Adjust by program base */ +#define R_TILEGX_BROFF_X1 20 /* X1 pipe branch offset */ +#define R_TILEGX_JUMPOFF_X1 21 /* X1 pipe jump offset */ +#define R_TILEGX_JUMPOFF_X1_PLT 22 /* X1 pipe jump offset to PLT */ +#define R_TILEGX_IMM8_X0 23 /* X0 pipe 8-bit */ +#define R_TILEGX_IMM8_Y0 24 /* Y0 pipe 8-bit */ +#define R_TILEGX_IMM8_X1 25 /* X1 pipe 8-bit */ +#define R_TILEGX_IMM8_Y1 26 /* Y1 pipe 8-bit */ +#define R_TILEGX_DEST_IMM8_X1 27 /* X1 pipe destination 8-bit */ +#define R_TILEGX_MT_IMM14_X1 28 /* X1 pipe mtspr */ +#define R_TILEGX_MF_IMM14_X1 29 /* X1 pipe mfspr */ +#define R_TILEGX_MMSTART_X0 30 /* X0 pipe mm "start" */ +#define R_TILEGX_MMEND_X0 31 /* X0 pipe mm "end" */ +#define R_TILEGX_SHAMT_X0 32 /* X0 pipe shift amount */ +#define R_TILEGX_SHAMT_X1 33 /* X1 pipe shift amount */ +#define R_TILEGX_SHAMT_Y0 34 /* Y0 pipe shift amount */ +#define R_TILEGX_SHAMT_Y1 35 /* Y1 pipe shift amount */ +#define R_TILEGX_IMM16_X0_HW0 36 /* X0 pipe hword 0 */ +#define R_TILEGX_IMM16_X1_HW0 37 /* X1 pipe hword 0 */ +#define R_TILEGX_IMM16_X0_HW1 38 /* X0 pipe hword 1 */ +#define R_TILEGX_IMM16_X1_HW1 39 /* X1 pipe hword 1 */ +#define R_TILEGX_IMM16_X0_HW2 40 /* X0 pipe hword 2 */ +#define R_TILEGX_IMM16_X1_HW2 41 /* X1 pipe hword 2 */ +#define R_TILEGX_IMM16_X0_HW3 42 /* X0 pipe hword 3 */ +#define R_TILEGX_IMM16_X1_HW3 43 /* X1 pipe hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_LAST 44 /* X0 pipe last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST 45 /* X1 pipe last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST 46 /* X0 pipe last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST 47 /* X1 pipe last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST 48 /* X0 pipe last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST 49 /* X1 pipe last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_PCREL 50 /* X0 pipe PC relative hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_PCREL 51 /* X1 pipe PC relative hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_PCREL 52 /* X0 pipe PC relative hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_PCREL 53 /* X1 pipe PC relative hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_PCREL 54 /* X0 pipe PC relative hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_PCREL 55 /* X1 pipe PC relative hword 2 */ +#define R_TILEGX_IMM16_X0_HW3_PCREL 56 /* X0 pipe PC relative hword 3 */ +#define R_TILEGX_IMM16_X1_HW3_PCREL 57 /* X1 pipe PC relative hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_PCREL 58 /* X0 pipe PC-rel last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST_PCREL 59 /* X1 pipe PC-rel last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST_PCREL 60 /* X0 pipe PC-rel last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST_PCREL 61 /* X1 pipe PC-rel last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST_PCREL 62 /* X0 pipe PC-rel last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST_PCREL 63 /* X1 pipe PC-rel last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_GOT 64 /* X0 pipe hword 0 GOT offset */ +#define R_TILEGX_IMM16_X1_HW0_GOT 65 /* X1 pipe hword 0 GOT offset */ +#define R_TILEGX_IMM16_X0_HW0_PLT_PCREL 66 /* X0 pipe PC-rel PLT hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_PLT_PCREL 67 /* X1 pipe PC-rel PLT hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_PLT_PCREL 68 /* X0 pipe PC-rel PLT hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_PLT_PCREL 69 /* X1 pipe PC-rel PLT hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_PLT_PCREL 70 /* X0 pipe PC-rel PLT hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_PLT_PCREL 71 /* X1 pipe PC-rel PLT hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_GOT 72 /* X0 pipe last hword 0 GOT offset */ +#define R_TILEGX_IMM16_X1_HW0_LAST_GOT 73 /* X1 pipe last hword 0 GOT offset */ +#define R_TILEGX_IMM16_X0_HW1_LAST_GOT 74 /* X0 pipe last hword 1 GOT offset */ +#define R_TILEGX_IMM16_X1_HW1_LAST_GOT 75 /* X1 pipe last hword 1 GOT offset */ +#define R_TILEGX_IMM16_X0_HW3_PLT_PCREL 76 /* X0 pipe PC-rel PLT hword 3 */ +#define R_TILEGX_IMM16_X1_HW3_PLT_PCREL 77 /* X1 pipe PC-rel PLT hword 3 */ +#define R_TILEGX_IMM16_X0_HW0_TLS_GD 78 /* X0 pipe hword 0 TLS GD offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_GD 79 /* X1 pipe hword 0 TLS GD offset */ +#define R_TILEGX_IMM16_X0_HW0_TLS_LE 80 /* X0 pipe hword 0 TLS LE offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_LE 81 /* X1 pipe hword 0 TLS LE offset */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_LE 82 /* X0 pipe last hword 0 LE off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_LE 83 /* X1 pipe last hword 0 LE off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_LE 84 /* X0 pipe last hword 1 LE off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_LE 85 /* X1 pipe last hword 1 LE off */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_GD 86 /* X0 pipe last hword 0 GD off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_GD 87 /* X1 pipe last hword 0 GD off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_GD 88 /* X0 pipe last hword 1 GD off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_GD 89 /* X1 pipe last hword 1 GD off */ +/* Relocs 90-91 are currently not defined. */ +#define R_TILEGX_IMM16_X0_HW0_TLS_IE 92 /* X0 pipe hword 0 TLS IE offset */ +#define R_TILEGX_IMM16_X1_HW0_TLS_IE 93 /* X1 pipe hword 0 TLS IE offset */ +#define R_TILEGX_IMM16_X0_HW0_LAST_PLT_PCREL 94 /* X0 pipe PC-rel PLT last hword 0 */ +#define R_TILEGX_IMM16_X1_HW0_LAST_PLT_PCREL 95 /* X1 pipe PC-rel PLT last hword 0 */ +#define R_TILEGX_IMM16_X0_HW1_LAST_PLT_PCREL 96 /* X0 pipe PC-rel PLT last hword 1 */ +#define R_TILEGX_IMM16_X1_HW1_LAST_PLT_PCREL 97 /* X1 pipe PC-rel PLT last hword 1 */ +#define R_TILEGX_IMM16_X0_HW2_LAST_PLT_PCREL 98 /* X0 pipe PC-rel PLT last hword 2 */ +#define R_TILEGX_IMM16_X1_HW2_LAST_PLT_PCREL 99 /* X1 pipe PC-rel PLT last hword 2 */ +#define R_TILEGX_IMM16_X0_HW0_LAST_TLS_IE 100 /* X0 pipe last hword 0 IE off */ +#define R_TILEGX_IMM16_X1_HW0_LAST_TLS_IE 101 /* X1 pipe last hword 0 IE off */ +#define R_TILEGX_IMM16_X0_HW1_LAST_TLS_IE 102 /* X0 pipe last hword 1 IE off */ +#define R_TILEGX_IMM16_X1_HW1_LAST_TLS_IE 103 /* X1 pipe last hword 1 IE off */ +/* Relocs 104-105 are currently not defined. */ +#define R_TILEGX_TLS_DTPMOD64 106 /* 64-bit ID of symbol's module */ +#define R_TILEGX_TLS_DTPOFF64 107 /* 64-bit offset in TLS block */ +#define R_TILEGX_TLS_TPOFF64 108 /* 64-bit offset in static TLS block */ +#define R_TILEGX_TLS_DTPMOD32 109 /* 32-bit ID of symbol's module */ +#define R_TILEGX_TLS_DTPOFF32 110 /* 32-bit offset in TLS block */ +#define R_TILEGX_TLS_TPOFF32 111 /* 32-bit offset in static TLS block */ +#define R_TILEGX_TLS_GD_CALL 112 /* "jal" for TLS GD */ +#define R_TILEGX_IMM8_X0_TLS_GD_ADD 113 /* X0 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_X1_TLS_GD_ADD 114 /* X1 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_Y0_TLS_GD_ADD 115 /* Y0 pipe "addi" for TLS GD */ +#define R_TILEGX_IMM8_Y1_TLS_GD_ADD 116 /* Y1 pipe "addi" for TLS GD */ +#define R_TILEGX_TLS_IE_LOAD 117 /* "ld_tls" for TLS IE */ +#define R_TILEGX_IMM8_X0_TLS_ADD 118 /* X0 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_X1_TLS_ADD 119 /* X1 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_Y0_TLS_ADD 120 /* Y0 pipe "addi" for TLS GD/IE */ +#define R_TILEGX_IMM8_Y1_TLS_ADD 121 /* Y1 pipe "addi" for TLS GD/IE */ + +#define R_TILEGX_GNU_VTINHERIT 128 /* GNU C++ vtable hierarchy */ +#define R_TILEGX_GNU_VTENTRY 129 /* GNU C++ vtable member usage */ + +#define R_TILEGX_NUM 130 + + +#endif /* elf.h */ diff --git a/i386-asm.c b/i386-asm.c index f9c0fa7..a524658 100644 --- a/i386-asm.c +++ b/i386-asm.c @@ -27,6 +27,7 @@ #define TOK_ASM_first TOK_ASM_clc #define TOK_ASM_last TOK_ASM_emms +#define TOK_ASM_alllast TOK_ASM_pxor #define OPC_JMP 0x01 /* jmp operand */ #define OPC_B 0x02 /* only used with OPC_WL */ @@ -715,6 +716,9 @@ ST_FUNC void asm_opcode(TCCState *s1, int opcode) g(b >> 8); g(b); return; + } else if (opcode <= TOK_ASM_alllast) { + tcc_error("bad operand with opcode '%s'", + get_tok_str(opcode, NULL)); } else { tcc_error("unknown opcode '%s'", get_tok_str(opcode, NULL)); @@ -1378,7 +1382,7 @@ ST_FUNC void subst_asm_operand(CString *add_str, } } -/* generate prolog and epilog code for asm statment */ +/* generate prolog and epilog code for asm statement */ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, diff --git a/i386-asm.h b/i386-asm.h index 760c06d..a954afb 100644 --- a/i386-asm.h +++ b/i386-asm.h @@ -463,7 +463,7 @@ ALT(DEF_ASM_OP2(psrlq, 0x0f73, 2, OPC_MODRM, OPT_IM8, OPT_MMX )) DEF_ASM_OP2(punpcklbw, 0x0f60, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) DEF_ASM_OP2(punpcklwd, 0x0f61, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) DEF_ASM_OP2(punpckldq, 0x0f62, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) - DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) + DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) /* must be last !OP0 */ #undef ALT #undef DEF_ASM_OP0 diff --git a/i386-gen.c b/i386-gen.c index 844a482..ece054b 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -338,6 +338,15 @@ static void gadd_sp(int val) } } +static void gen_static_call(int v) +{ + Sym *sym; + + sym = external_global_sym(v, &func_old_type, 0); + oad(0xe8, -4); + greloc(cur_text_section, sym, ind-4, R_386_PC32); +} + /* 'is_jmp' is '1' if it is a jump */ static void gcall_or_jmp(int is_jmp) { @@ -365,8 +374,9 @@ static void gcall_or_jmp(int is_jmp) static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX }; static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX }; -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { #ifdef TCC_TARGET_PE int size, align; @@ -374,19 +384,19 @@ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) *ret_align = 1; // Never have to re-align return values for x86 size = type_size(vt, &align); if (size > 8) { - return 1; + return 0; } else if (size > 4) { ret->ref = NULL; ret->t = VT_LLONG; - return 0; + return 1; } else { ret->ref = NULL; ret->t = VT_INT; - return 0; + return 1; } #else *ret_align = 1; // Never have to re-align return values for x86 - return 1; + return 0; #endif } @@ -447,7 +457,7 @@ ST_FUNC void gfunc_call(int nb_args) } save_regs(0); /* save used temporary registers */ func_sym = vtop->type.ref; - func_call = FUNC_CALL(func_sym->r); + func_call = func_sym->a.func_call; /* fast call case */ if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) || func_call == FUNC_FASTCALLW) { @@ -495,7 +505,7 @@ ST_FUNC void gfunc_prolog(CType *func_type) CType *type; sym = func_type->ref; - func_call = FUNC_CALL(sym->r); + func_call = sym->a.func_call; addr = 8; loc = 0; func_vc = 0; @@ -517,6 +527,7 @@ ST_FUNC void gfunc_prolog(CType *func_type) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->type; + func_var = (sym->c == FUNC_ELLIPSIS); #ifdef TCC_TARGET_PE size = type_size(&func_vt,&align); if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) { @@ -582,7 +593,7 @@ ST_FUNC void gfunc_epilog(void) && func_bound_offset != lbounds_section->data_offset) { int saved_ind; int *bounds_ptr; - Sym *sym, *sym_data; + Sym *sym_data; /* add end of table info */ bounds_ptr = section_ptr_add(lbounds_section, sizeof(int)); *bounds_ptr = 0; @@ -594,20 +605,16 @@ ST_FUNC void gfunc_epilog(void) greloc(cur_text_section, sym_data, ind + 1, R_386_32); oad(0xb8, 0); /* mov %eax, xxx */ - sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0); - greloc(cur_text_section, sym, - ind + 1, R_386_PC32); - oad(0xe8, -4); + gen_static_call(TOK___bound_local_new); + ind = saved_ind; /* generate bound check local freeing */ o(0x5250); /* save returned value, if any */ greloc(cur_text_section, sym_data, ind + 1, R_386_32); oad(0xb8, 0); /* mov %eax, xxx */ - sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0); - greloc(cur_text_section, sym, - ind + 1, R_386_PC32); - oad(0xe8, -4); + gen_static_call(TOK___bound_local_delete); + o(0x585a); /* restore returned value, if any */ } #endif @@ -626,10 +633,8 @@ ST_FUNC void gfunc_epilog(void) ind = func_sub_sp_offset - FUNC_PROLOG_SIZE; #ifdef TCC_TARGET_PE if (v >= 4096) { - Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0); oad(0xb8, v); /* mov stacksize, %eax */ - oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */ - greloc(cur_text_section, sym, ind-4, R_386_PC32); + gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */ } else #endif { @@ -672,7 +677,7 @@ ST_FUNC int gtst(int inv, int t) /* fast case : can jump directly since flags are set */ g(0x0f); t = psym((vtop->c.i - 16) ^ inv, t); - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -685,23 +690,6 @@ ST_FUNC int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t) || - (vtop->type.t & VT_BTYPE) == VT_LLONG) { - vpushi(0); - gen_op(TOK_NE); - } - if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { - /* constant jmp optimization */ - if ((vtop->c.i != 0) != inv) - t = gjmp(t); - } else { - v = gv(RC_INT); - o(0x85); - o(0xc0 + v * 9); - g(0x0f); - t = psym(0x85 ^ inv, t); - } } vtop--; return t; @@ -885,7 +873,10 @@ ST_FUNC void gen_opf(int op) swapped = 0; if (swapped) o(0xc9d9); /* fxch %st(1) */ - o(0xe9da); /* fucompp */ + if (op == TOK_EQ || op == TOK_NE) + o(0xe9da); /* fucompp */ + else + o(0xd9de); /* fcompp */ o(0xe0df); /* fnstsw %ax */ if (op == TOK_EQ) { o(0x45e480); /* and $0x45, %ah */ @@ -989,55 +980,20 @@ ST_FUNC void gen_cvt_itof(int t) } /* convert fp to int 't' type */ -/* XXX: handle long long case */ ST_FUNC void gen_cvt_ftoi(int t) { - int r, r2, size; - Sym *sym; - CType ushort_type; - - ushort_type.t = VT_SHORT | VT_UNSIGNED; - ushort_type.ref = 0; - - gv(RC_FLOAT); - if (t != VT_INT) - size = 8; - else - size = 4; - - o(0x2dd9); /* ldcw xxx */ - sym = external_global_sym(TOK___tcc_int_fpu_control, - &ushort_type, VT_LVAL); - greloc(cur_text_section, sym, - ind, R_386_32); - gen_le32(0); - - oad(0xec81, size); /* sub $xxx, %esp */ - if (size == 4) - o(0x1cdb); /* fistpl */ + int bt = vtop->type.t & VT_BTYPE; + if (bt == VT_FLOAT) + vpush_global_sym(&func_old_type, TOK___fixsfdi); + else if (bt == VT_LDOUBLE) + vpush_global_sym(&func_old_type, TOK___fixxfdi); else - o(0x3cdf); /* fistpll */ - o(0x24); - o(0x2dd9); /* ldcw xxx */ - sym = external_global_sym(TOK___tcc_fpu_control, - &ushort_type, VT_LVAL); - greloc(cur_text_section, sym, - ind, R_386_32); - gen_le32(0); - - r = get_reg(RC_INT); - o(0x58 + r); /* pop r */ - if (size == 8) { - if (t == VT_LLONG) { - vtop->r = r; /* mark reg as used */ - r2 = get_reg(RC_INT); - o(0x58 + r2); /* pop r2 */ - vtop->r2 = r2; - } else { - o(0x04c483); /* add $4, %esp */ - } - } - vtop->r = r; + vpush_global_sym(&func_old_type, TOK___fixdfdi); + vswap(); + gfunc_call(1); + vpushi(0); + vtop->r = REG_IRET; + vtop->r2 = REG_LRET; } /* convert from one floating point type to another */ @@ -1060,18 +1016,13 @@ ST_FUNC void ggoto(void) /* generate a bounded pointer addition */ ST_FUNC void gen_bounded_ptr_add(void) { - Sym *sym; - /* prepare fast i386 function call (args in eax and edx) */ gv2(RC_EAX, RC_EDX); /* save all temporary registers */ vtop -= 2; save_regs(0); /* do a fast function call */ - sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0); - greloc(cur_text_section, sym, - ind + 1, R_386_PC32); - oad(0xe8, -4); + gen_static_call(TOK___bound_ptr_add); /* returned pointer is in eax */ vtop++; vtop->r = TREG_EAX | VT_BOUNDED; diff --git a/i386-tok.h b/i386-tok.h index d1e4bf3..6ba865d 100644 --- a/i386-tok.h +++ b/i386-tok.h @@ -191,7 +191,7 @@ DEF_FP(mul) DEF_ASM(fcom) - DEF_ASM(fcom_1) /* non existant op, just to have a regular table */ + DEF_ASM(fcom_1) /* non existent op, just to have a regular table */ DEF_FP1(com) DEF_FP(comp) diff --git a/il-gen.c b/il-gen.c index 170f436..9e1ec64 100644 --- a/il-gen.c +++ b/il-gen.c @@ -441,6 +441,7 @@ void gfunc_prolog(int t) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->t; + func_var = (sym->c == FUNC_ELLIPSIS); if ((func_vt & VT_BTYPE) == VT_STRUCT) { func_vc = addr; addr++; @@ -515,7 +516,7 @@ int gtst(int inv, int t) break; } t = out_opj(c, t); - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -528,19 +529,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->t)) { - vpushi(0); - gen_op(TOK_NE); - } - if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) { - /* constant jmp optimization */ - if ((vtop->c.i != 0) != inv) - t = gjmp(t); - } else { - v = gv(RC_INT); - t = out_opj(IL_OP_BRTRUE - inv, t); - } } vtop--; return t; diff --git a/include/float.h b/include/float.h index 5f1c6f7..f16f1f0 100644 --- a/include/float.h +++ b/include/float.h @@ -27,7 +27,7 @@ #define DBL_MAX_10_EXP 308 /* horrible intel long double */ -#ifdef __i386__ +#if defined __i386__ || defined __x86_64__ #define LDBL_MANT_DIG 64 #define LDBL_DIG 18 diff --git a/include/stdarg.h b/include/stdarg.h index efca0e5..5aa9d57 100644 --- a/include/stdarg.h +++ b/include/stdarg.h @@ -4,18 +4,28 @@ #ifdef __x86_64__ #ifndef _WIN64 -typedef void *va_list; +//This should be in sync with the declaration on our lib/libtcc1.c +/* GCC compatible definition of va_list. */ +typedef struct { + unsigned int gp_offset; + unsigned int fp_offset; + union { + unsigned int overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; +} __va_list_struct; -va_list __va_start(void *fp); -void *__va_arg(va_list ap, int arg_type, int size, int align); -va_list __va_copy(va_list src); -void __va_end(va_list ap); +typedef __va_list_struct va_list[1]; -#define va_start(ap, last) ((ap) = __va_start(__builtin_frame_address(0))) +void __va_start(__va_list_struct *ap, void *fp); +void *__va_arg(__va_list_struct *ap, int arg_type, int size, int align); + +#define va_start(ap, last) __va_start(ap, __builtin_frame_address(0)) #define va_arg(ap, type) \ (*(type *)(__va_arg(ap, __builtin_va_arg_types(type), sizeof(type), __alignof__(type)))) -#define va_copy(dest, src) ((dest) = __va_copy(src)) -#define va_end(ap) __va_end(ap) +#define va_copy(dest, src) (*(dest) = *(src)) +#define va_end(ap) #else /* _WIN64 */ typedef char *va_list; @@ -25,6 +35,17 @@ typedef char *va_list; #define va_end(ap) #endif +#elif __arm__ +typedef char *va_list; +#define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x) +#define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \ + & ~(_tcc_alignof(type) - 1)) +#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3) +#define va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \ + &~3), *(type *)(ap - ((sizeof(type)+3)&~3))) +#define va_copy(dest, src) (dest) = (src) +#define va_end(ap) + #else /* __i386__ */ typedef char *va_list; /* only correct for i386 */ diff --git a/include/stdbool.h b/include/stdbool.h index 6ed13a6..d2ee446 100644 --- a/include/stdbool.h +++ b/include/stdbool.h @@ -6,5 +6,6 @@ #define bool _Bool #define true 1 #define false 0 +#define __bool_true_false_are_defined 1 #endif /* _STDBOOL_H */ diff --git a/include/stddef.h b/include/stddef.h index fbc61fc..9e43de9 100644 --- a/include/stddef.h +++ b/include/stddef.h @@ -26,3 +26,18 @@ typedef unsigned long long int uint64_t; void *alloca(size_t size); #endif + +/* Older glibc require a wint_t from (when requested + by __need_wint_t, as otherwise stddef.h isn't allowed to + define this type). Note that this must be outside the normal + _STDDEF_H guard, so that it works even when we've included the file + already (without requiring wint_t). Some other libs define _WINT_T + if they've already provided that type, so we can use that as guard. + TCC defines __WINT_TYPE__ for us. */ +#if defined (__need_wint_t) +#ifndef _WINT_T +#define _WINT_T +typedef __WINT_TYPE__ wint_t; +#endif +#undef __need_wint_t +#endif diff --git a/lib/Makefile b/lib/Makefile index 300fa46..e9e12f1 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -6,7 +6,7 @@ TOP = .. include $(TOP)/Makefile VPATH = $(top_srcdir)/lib $(top_srcdir)/win32/lib -ifndef TARGET +ifndef TARGET # native library ifdef CONFIG_WIN64 TARGET = x86_64-win32 else @@ -24,6 +24,11 @@ ifndef TARGET ifneq ($(TARGETOS),Darwin) XCC = $(CC) endif + else + ifeq ($(ARCH),arm) + TARGET = arm + XCC = $(CC) + endif endif endif endif @@ -41,20 +46,27 @@ cross : TCC = $(TOP)/$(TARGET)-tcc$(EXESUF) I386_O = libtcc1.o alloca86.o alloca86-bt.o $(BCHECK_O) X86_64_O = libtcc1.o alloca86_64.o +ARM_O = libtcc1.o armeabi.o alloca-arm.o WIN32_O = $(I386_O) crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o WIN64_O = $(X86_64_O) crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o +# build TCC runtime library to contain PIC code, so it can be linked +# into shared libraries +PICFLAGS = -fPIC + ifeq "$(TARGET)" "i386-win32" OBJ = $(addprefix $(DIR)/,$(WIN32_O)) TGT = -DTCC_TARGET_I386 -DTCC_TARGET_PE XCC = $(TCC) -B$(top_srcdir)/win32 -I$(top_srcdir)/include XAR = $(DIR)/tiny_libmaker$(EXESUF) + PICFLAGS = else ifeq "$(TARGET)" "x86_64-win32" OBJ = $(addprefix $(DIR)/,$(WIN64_O)) TGT = -DTCC_TARGET_X86_64 -DTCC_TARGET_PE XCC = $(TCC) -B$(top_srcdir)/win32 -I$(top_srcdir)/include XAR = $(DIR)/tiny_libmaker$(EXESUF) + PICFLAGS = else ifeq "$(TARGET)" "i386" OBJ = $(addprefix $(DIR)/,$(I386_O)) @@ -65,14 +77,20 @@ ifeq "$(TARGET)" "x86_64" OBJ = $(addprefix $(DIR)/,$(X86_64_O)) TGT = -DTCC_TARGET_X86_64 XCC ?= $(TCC) -B$(TOP) +else +ifeq "$(TARGET)" "arm" + OBJ = $(addprefix $(DIR)/,$(ARM_O)) + TGT = -DTCC_TARGET_ARM + XCC ?= $(TCC) -B$(TOP) else $(error libtcc1.a not supported on target '$(TARGET)') endif endif endif endif +endif -XFLAGS = $(CPPFLAGS) $(CFLAGS) $(TGT) +XFLAGS = $(CPPFLAGS) $(CFLAGS) $(PICFLAGS) $(TGT) ifeq ($(TARGETOS),Darwin) XAR = $(DIR)/tiny_libmaker$(EXESUF) @@ -100,6 +118,3 @@ $(DIR)/exists : clean : rm -rfv i386-win32 x86_64-win32 i386 x86_64 - -Makefile: $(top_srcdir)/lib/Makefile - cp $< $@ diff --git a/lib/alloca-arm.S b/lib/alloca-arm.S new file mode 100644 index 0000000..9deae63 --- /dev/null +++ b/lib/alloca-arm.S @@ -0,0 +1,11 @@ + .text + .align 2 + .global alloca + .type alloca, %function +alloca: + rsb sp, r0, sp + bic sp, sp, #7 + mov r0, sp + mov pc, lr + .size alloca, .-alloca + .section .note.GNU-stack,"",%progbits diff --git a/lib/armeabi.c b/lib/armeabi.c new file mode 100644 index 0000000..ce5e232 --- /dev/null +++ b/lib/armeabi.c @@ -0,0 +1,468 @@ +#include + +/* We rely on the little endianness and EABI calling convention for this to + work */ + +typedef struct double_unsigned_struct { + unsigned low; + unsigned high; +} double_unsigned_struct; + +typedef struct unsigned_int_struct { + unsigned low; + int high; +} unsigned_int_struct; + +#define REGS_RETURN(name, type) \ + void name ## _return(type ret) {} + + +/* Float helper functions */ + +#define FLOAT_EXP_BITS 8 +#define FLOAT_FRAC_BITS 23 + +#define DOUBLE_EXP_BITS 11 +#define DOUBLE_FRAC_BITS 52 + +#define ONE_EXP(type) ((1 << (type ## _EXP_BITS - 1)) - 1) + +REGS_RETURN(unsigned_int_struct, unsigned_int_struct) +REGS_RETURN(double_unsigned_struct, double_unsigned_struct) + +/* float -> integer: (sign) 1.fraction x 2^(exponent - exp_for_one) */ + + +/* float to [unsigned] long long conversion */ +#define DEFINE__AEABI_F2XLZ(name, with_sign) \ +void __aeabi_ ## name(unsigned val) \ +{ \ + int exp, high_shift, sign; \ + double_unsigned_struct ret; \ + \ + /* compute sign */ \ + sign = val >> 31; \ + \ + /* compute real exponent */ \ + exp = val >> FLOAT_FRAC_BITS; \ + exp &= (1 << FLOAT_EXP_BITS) - 1; \ + exp -= ONE_EXP(FLOAT); \ + \ + /* undefined behavior if truncated value cannot be represented */ \ + if (with_sign) { \ + if (exp > 62) /* |val| too big, double cannot represent LLONG_MAX */ \ + return; \ + } else { \ + if ((sign && exp >= 0) || exp > 63) /* if val < 0 || val too big */ \ + return; \ + } \ + \ + val &= (1 << FLOAT_FRAC_BITS) - 1; \ + if (exp >= 32) { \ + ret.high = 1 << (exp - 32); \ + if (exp - 32 >= FLOAT_FRAC_BITS) { \ + ret.high |= val << (exp - 32 - FLOAT_FRAC_BITS); \ + ret.low = 0; \ + } else { \ + high_shift = FLOAT_FRAC_BITS - (exp - 32); \ + ret.high |= val >> high_shift; \ + ret.low = val << (32 - high_shift); \ + } \ + } else { \ + ret.high = 0; \ + ret.low = 1 << exp; \ + if (exp > FLOAT_FRAC_BITS) \ + ret.low |= val << (exp - FLOAT_FRAC_BITS); \ + else \ + ret.low |= val >> (FLOAT_FRAC_BITS - exp); \ + } \ + \ + /* encode negative integer using 2's complement */ \ + if (with_sign && sign) { \ + ret.low = ~ret.low; \ + ret.high = ~ret.high; \ + if (ret.low == UINT_MAX) { \ + ret.low = 0; \ + ret.high++; \ + } else \ + ret.low++; \ + } \ + \ + double_unsigned_struct_return(ret); \ +} + +/* float to unsigned long long conversion */ +DEFINE__AEABI_F2XLZ(f2ulz, 0) + +/* float to long long conversion */ +DEFINE__AEABI_F2XLZ(f2lz, 1) + +/* double to [unsigned] long long conversion */ +#define DEFINE__AEABI_D2XLZ(name, with_sign) \ +void __aeabi_ ## name(double_unsigned_struct val) \ +{ \ + int exp, high_shift, sign; \ + double_unsigned_struct ret; \ + \ + /* compute sign */ \ + sign = val.high >> 31; \ + \ + /* compute real exponent */ \ + exp = (val.high >> (DOUBLE_FRAC_BITS - 32)); \ + exp &= (1 << DOUBLE_EXP_BITS) - 1; \ + exp -= ONE_EXP(DOUBLE); \ + \ + /* undefined behavior if truncated value cannot be represented */ \ + if (with_sign) { \ + if (exp > 62) /* |val| too big, double cannot represent LLONG_MAX */ \ + return; \ + } else { \ + if ((sign && exp >= 0) || exp > 63) /* if val < 0 || val too big */ \ + return; \ + } \ + \ + val.high &= (1 << (DOUBLE_FRAC_BITS - 32)) - 1; \ + if (exp >= 32) { \ + ret.high = 1 << (exp - 32); \ + if (exp >= DOUBLE_FRAC_BITS) { \ + high_shift = exp - DOUBLE_FRAC_BITS; \ + ret.high |= val.high << high_shift; \ + ret.high |= val.low >> (32 - high_shift); \ + ret.low = val.low << high_shift; \ + } else { \ + high_shift = DOUBLE_FRAC_BITS - exp; \ + ret.high |= val.high >> high_shift; \ + ret.low = val.high << (32 - high_shift); \ + ret.low |= val.low >> high_shift; \ + } \ + } else { \ + ret.high = 0; \ + ret.low = 1 << exp; \ + if (exp > DOUBLE_FRAC_BITS - 32) { \ + high_shift = exp - DOUBLE_FRAC_BITS - 32; \ + ret.low |= val.high << high_shift; \ + ret.low |= val.low >> (32 - high_shift); \ + } else \ + ret.low |= val.high >> (DOUBLE_FRAC_BITS - 32 - exp); \ + } \ + \ + /* encode negative integer using 2's complement */ \ + if (with_sign && sign) { \ + ret.low = ~ret.low; \ + ret.high = ~ret.high; \ + if (ret.low == UINT_MAX) { \ + ret.low = 0; \ + ret.high++; \ + } else \ + ret.low++; \ + } \ + \ + double_unsigned_struct_return(ret); \ +} + +/* double to unsigned long long conversion */ +DEFINE__AEABI_D2XLZ(d2ulz, 0) + +/* double to long long conversion */ +DEFINE__AEABI_D2XLZ(d2lz, 1) + +/* long long to float conversion */ +#define DEFINE__AEABI_XL2F(name, with_sign) \ +unsigned __aeabi_ ## name(unsigned long long v) \ +{ \ + int s /* shift */, flb /* first lost bit */, sign = 0; \ + unsigned p = 0 /* power */, ret; \ + double_unsigned_struct val; \ + \ + /* fraction in negative float is encoded in 1's complement */ \ + if (with_sign && (v & (1ULL << 63))) { \ + sign = 1; \ + v = ~v + 1; \ + } \ + val.low = v; \ + val.high = v >> 32; \ + /* fill fraction bits */ \ + for (s = 31, p = 1 << 31; p && !(val.high & p); s--, p >>= 1); \ + if (p) { \ + ret = val.high & (p - 1); \ + if (s < FLOAT_FRAC_BITS) { \ + ret <<= FLOAT_FRAC_BITS - s; \ + ret |= val.low >> (32 - (FLOAT_FRAC_BITS - s)); \ + flb = (val.low >> (32 - (FLOAT_FRAC_BITS - s - 1))) & 1; \ + } else { \ + flb = (ret >> (s - FLOAT_FRAC_BITS - 1)) & 1; \ + ret >>= s - FLOAT_FRAC_BITS; \ + } \ + s += 32; \ + } else { \ + for (s = 31, p = 1 << 31; p && !(val.low & p); s--, p >>= 1); \ + if (p) { \ + ret = val.low & (p - 1); \ + if (s <= FLOAT_FRAC_BITS) { \ + ret <<= FLOAT_FRAC_BITS - s; \ + flb = 0; \ + } else { \ + flb = (ret >> (s - FLOAT_FRAC_BITS - 1)) & 1; \ + ret >>= s - FLOAT_FRAC_BITS; \ + } \ + } else \ + return 0; \ + } \ + if (flb) \ + ret++; \ + \ + /* fill exponent bits */ \ + ret |= (s + ONE_EXP(FLOAT)) << FLOAT_FRAC_BITS; \ + \ + /* fill sign bit */ \ + ret |= sign << 31; \ + \ + return ret; \ +} + +/* unsigned long long to float conversion */ +DEFINE__AEABI_XL2F(ul2f, 0) + +/* long long to float conversion */ +DEFINE__AEABI_XL2F(l2f, 1) + +/* long long to double conversion */ +#define __AEABI_XL2D(name, with_sign) \ +void __aeabi_ ## name(unsigned long long v) \ +{ \ + int s /* shift */, high_shift, sign = 0; \ + unsigned tmp, p = 0; \ + double_unsigned_struct val, ret; \ + \ + /* fraction in negative float is encoded in 1's complement */ \ + if (with_sign && (v & (1ULL << 63))) { \ + sign = 1; \ + v = ~v + 1; \ + } \ + val.low = v; \ + val.high = v >> 32; \ + \ + /* fill fraction bits */ \ + for (s = 31, p = 1 << 31; p && !(val.high & p); s--, p >>= 1); \ + if (p) { \ + tmp = val.high & (p - 1); \ + if (s < DOUBLE_FRAC_BITS - 32) { \ + high_shift = DOUBLE_FRAC_BITS - 32 - s; \ + ret.high = tmp << high_shift; \ + ret.high |= val.low >> (32 - high_shift); \ + ret.low = val.low << high_shift; \ + } else { \ + high_shift = s - (DOUBLE_FRAC_BITS - 32); \ + ret.high = tmp >> high_shift; \ + ret.low = tmp << (32 - high_shift); \ + ret.low |= val.low >> high_shift; \ + if ((val.low >> (high_shift - 1)) & 1) { \ + if (ret.low == UINT_MAX) { \ + ret.high++; \ + ret.low = 0; \ + } else \ + ret.low++; \ + } \ + } \ + s += 32; \ + } else { \ + for (s = 31, p = 1 << 31; p && !(val.low & p); s--, p >>= 1); \ + if (p) { \ + tmp = val.low & (p - 1); \ + if (s <= DOUBLE_FRAC_BITS - 32) { \ + high_shift = DOUBLE_FRAC_BITS - 32 - s; \ + ret.high = tmp << high_shift; \ + ret.low = 0; \ + } else { \ + high_shift = s - (DOUBLE_FRAC_BITS - 32); \ + ret.high = tmp >> high_shift; \ + ret.low = tmp << (32 - high_shift); \ + } \ + } else { \ + ret.high = ret.low = 0; \ + double_unsigned_struct_return(ret); \ + } \ + } \ + \ + /* fill exponent bits */ \ + ret.high |= (s + ONE_EXP(DOUBLE)) << (DOUBLE_FRAC_BITS - 32); \ + \ + /* fill sign bit */ \ + ret.high |= sign << 31; \ + \ + double_unsigned_struct_return(ret); \ +} + +/* unsigned long long to double conversion */ +__AEABI_XL2D(ul2d, 0) + +/* long long to double conversion */ +__AEABI_XL2D(l2d, 1) + + +/* Long long helper functions */ + +/* TODO: add error in case of den == 0 (see §4.3.1 and §4.3.2) */ + +#define define_aeabi_xdivmod_signed_type(basetype, type) \ +typedef struct type { \ + basetype quot; \ + unsigned basetype rem; \ +} type + +#define define_aeabi_xdivmod_unsigned_type(basetype, type) \ +typedef struct type { \ + basetype quot; \ + basetype rem; \ +} type + +#define AEABI_UXDIVMOD(name,type, rettype, typemacro) \ +static inline rettype aeabi_ ## name (type num, type den) \ +{ \ + rettype ret; \ + type quot = 0; \ + \ + /* Increase quotient while it is less than numerator */ \ + while (num >= den) { \ + type q = 1; \ + \ + /* Find closest power of two */ \ + while ((q << 1) * den <= num && q * den <= typemacro ## _MAX / 2) \ + q <<= 1; \ + \ + /* Compute difference between current quotient and numerator */ \ + num -= q * den; \ + quot += q; \ + } \ + ret.quot = quot; \ + ret.rem = num; \ + return ret; \ +} + +#define __AEABI_XDIVMOD(name, type, uiname, rettype, urettype, typemacro) \ +void __aeabi_ ## name(type numerator, type denominator) \ +{ \ + unsigned type num, den; \ + urettype uxdiv_ret; \ + rettype ret; \ + \ + if (numerator >= 0) \ + num = numerator; \ + else \ + num = 0 - numerator; \ + if (denominator >= 0) \ + den = denominator; \ + else \ + den = 0 - denominator; \ + uxdiv_ret = aeabi_ ## uiname(num, den); \ + /* signs differ */ \ + if ((numerator & typemacro ## _MIN) != (denominator & typemacro ## _MIN)) \ + ret.quot = 0 - uxdiv_ret.quot; \ + else \ + ret.quot = uxdiv_ret.quot; \ + if (numerator < 0) \ + ret.rem = 0 - uxdiv_ret.rem; \ + else \ + ret.rem = uxdiv_ret.rem; \ + \ + rettype ## _return(ret); \ +} + +define_aeabi_xdivmod_signed_type(long long, lldiv_t); +define_aeabi_xdivmod_unsigned_type(unsigned long long, ulldiv_t); +define_aeabi_xdivmod_signed_type(int, idiv_t); +define_aeabi_xdivmod_unsigned_type(unsigned, uidiv_t); + +REGS_RETURN(lldiv_t, lldiv_t) +REGS_RETURN(ulldiv_t, ulldiv_t) +REGS_RETURN(idiv_t, idiv_t) +REGS_RETURN(uidiv_t, uidiv_t) + +AEABI_UXDIVMOD(uldivmod, unsigned long long, ulldiv_t, ULONG) + +__AEABI_XDIVMOD(ldivmod, long long, uldivmod, lldiv_t, ulldiv_t, LLONG) + +void __aeabi_uldivmod(unsigned long long num, unsigned long long den) +{ + ulldiv_t_return(aeabi_uldivmod(num, den)); +} + +void __aeabi_llsl(double_unsigned_struct val, int shift) +{ + double_unsigned_struct ret; + + if (shift >= 32) { + val.high = val.low; + val.low = 0; + shift -= 32; + } + if (shift > 0) { + ret.low = val.low << shift; + ret.high = (val.high << shift) | (val.low >> (32 - shift)); + double_unsigned_struct_return(ret); + return; + } + double_unsigned_struct_return(val); +} + +#define aeabi_lsr(val, shift, fill, type) \ + type ## _struct ret; \ + \ + if (shift >= 32) { \ + val.low = val.high; \ + val.high = fill; \ + shift -= 32; \ + } \ + if (shift > 0) { \ + ret.high = val.high >> shift; \ + ret.low = (val.high << (32 - shift)) | (val.low >> shift); \ + type ## _struct_return(ret); \ + return; \ + } \ + type ## _struct_return(val); + +void __aeabi_llsr(double_unsigned_struct val, int shift) +{ + aeabi_lsr(val, shift, 0, double_unsigned); +} + +void __aeabi_lasr(unsigned_int_struct val, int shift) +{ + aeabi_lsr(val, shift, val.high >> 31, unsigned_int); +} + + +/* Integer division functions */ + +AEABI_UXDIVMOD(uidivmod, unsigned, uidiv_t, UINT) + +int __aeabi_idiv(int numerator, int denominator) +{ + unsigned num, den; + uidiv_t ret; + + if (numerator >= 0) + num = numerator; + else + num = 0 - numerator; + if (denominator >= 0) + den = denominator; + else + den = 0 - denominator; + ret = aeabi_uidivmod(num, den); + if ((numerator & INT_MIN) != (denominator & INT_MIN)) /* signs differ */ + ret.quot *= -1; + return ret.quot; +} + +unsigned __aeabi_uidiv(unsigned num, unsigned den) +{ + return aeabi_uidivmod(num, den).quot; +} + +__AEABI_XDIVMOD(idivmod, int, uidivmod, idiv_t, uidiv_t, INT) + +void __aeabi_uidivmod(unsigned num, unsigned den) +{ + uidiv_t_return(aeabi_uidivmod(num, den)); +} diff --git a/lib/bcheck.c b/lib/bcheck.c index 064fb5d..8ac7d4e 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -22,7 +22,7 @@ #include #include #if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) \ - && !defined(__DragonFly__) && !defined(__OpenBSD__) + && !defined(__DragonFly__) && !defined(__OpenBSD__) && !defined(__NetBSD__) #include #endif #if !defined(_WIN32) @@ -41,7 +41,7 @@ #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) \ || defined(__DragonFly__) || defined(__dietlibc__) \ - || defined(__UCLIBC__) || defined(__OpenBSD__) \ + || defined(__UCLIBC__) || defined(__OpenBSD__) || defined(__NetBSD__) \ || defined(_WIN32) || defined(TCC_UCLIBC) #warning Bound checking does not support malloc (etc.) in this environment. #undef CONFIG_TCC_MALLOC_HOOKS @@ -418,6 +418,13 @@ void __bound_init(void) } } +void __bound_main_arg(void **p) +{ + void *start = p; + while (*p++); + __bound_new_region(start, (void *) p - start); +} + void __bound_exit(void) { restore_malloc_hooks(); @@ -628,7 +635,7 @@ int __bound_delete_region(void *p) } /* return the size of the region starting at p, or EMPTY_SIZE if non - existant region. */ + existent region. */ static unsigned long get_region_size(void *p) { unsigned long addr = (unsigned long)p; diff --git a/lib/libtcc1.c b/lib/libtcc1.c index 53dbec4..284965e 100644 --- a/lib/libtcc1.c +++ b/lib/libtcc1.c @@ -28,6 +28,8 @@ the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include + #define W_TYPE_SIZE 32 #define BITS_PER_UNIT 8 @@ -107,10 +109,10 @@ union float_long { }; /* XXX: we don't support several builtin supports for now */ -#ifndef __x86_64__ +#if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM) /* XXX: use gcc/tcc intrinsic ? */ -#if defined(__i386__) +#if defined(TCC_TARGET_I386) #define sub_ddmmss(sh, sl, ah, al, bh, bl) \ __asm__ ("subl %5,%1\n\tsbbl %3,%0" \ : "=r" ((USItype) (sh)), \ @@ -478,13 +480,6 @@ long long __ashldi3(long long a, int b) #endif } -#if defined(__i386__) -/* FPU control word for rounding to nearest mode */ -unsigned short __tcc_fpu_control = 0x137f; -/* FPU control word for round to zero mode for int conversion */ -unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00; -#endif - #endif /* !__x86_64__ */ /* XXX: fix tcc's code generator to do this instead */ @@ -605,15 +600,38 @@ unsigned long long __fixunsxfdi (long double a1) return 0; } -#if defined(__x86_64__) && !defined(_WIN64) +long long __fixsfdi (float a1) +{ + long long ret; int s; + ret = __fixunssfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +long long __fixdfdi (double a1) +{ + long long ret; int s; + ret = __fixunsdfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +long long __fixxfdi (long double a1) +{ + long long ret; int s; + ret = __fixunsxfdi((s = a1 >= 0) ? a1 : -a1); + return s ? ret : -ret; +} + +#if defined(TCC_TARGET_X86_64) && !defined(_WIN64) #ifndef __TINYC__ #include #include +#include #else /* Avoid including stdlib.h because it is not easily available when cross compiling */ extern void *malloc(unsigned long long); +void *memset(void *s, int c, size_t n); extern void free(void*); extern void abort(void); #endif @@ -622,8 +640,9 @@ enum __va_arg_type { __va_gen_reg, __va_float_reg, __va_stack }; +//This should be in sync with the declaration on our include/stdarg.h /* GCC compatible definition of va_list. */ -struct __va_list_struct { +typedef struct { unsigned int gp_offset; unsigned int fp_offset; union { @@ -631,19 +650,22 @@ struct __va_list_struct { char *overflow_arg_area; }; char *reg_save_area; -}; +} __va_list_struct; -void *__va_start(void *fp) +#undef __va_start +#undef __va_arg +#undef __va_copy +#undef __va_end + +void __va_start(__va_list_struct *ap, void *fp) { - struct __va_list_struct *ap = - (struct __va_list_struct *)malloc(sizeof(struct __va_list_struct)); - *ap = *(struct __va_list_struct *)((char *)fp - 16); + memset(ap, 0, sizeof(__va_list_struct)); + *ap = *(__va_list_struct *)((char *)fp - 16); ap->overflow_arg_area = (char *)fp + ap->overflow_offset; ap->reg_save_area = (char *)fp - 176 - 16; - return ap; } -void *__va_arg(struct __va_list_struct *ap, +void *__va_arg(__va_list_struct *ap, enum __va_arg_type arg_type, int size, int align) { @@ -669,7 +691,7 @@ void *__va_arg(struct __va_list_struct *ap, case __va_stack: use_overflow_area: ap->overflow_arg_area += size; - ap->overflow_arg_area = (char*)((long long)(ap->overflow_arg_area + align - 1) & -(long long)align); + ap->overflow_arg_area = (char*)((intptr_t)(ap->overflow_arg_area + align - 1) & -(intptr_t)align); return ap->overflow_arg_area - size; default: @@ -680,28 +702,38 @@ void *__va_arg(struct __va_list_struct *ap, } } -void *__va_copy(struct __va_list_struct *src) -{ - struct __va_list_struct *dest = - (struct __va_list_struct *)malloc(sizeof(struct __va_list_struct)); - *dest = *src; - return dest; -} - -void __va_end(struct __va_list_struct *ap) -{ - free(ap); -} - #endif /* __x86_64__ */ /* Flushing for tccrun */ -#if defined(__x86_64__) || defined(__i386__) +#if defined(TCC_TARGET_X86_64) || defined(TCC_TARGET_I386) void __clear_cache(char *beginning, char *end) { } +#elif defined(TCC_TARGET_ARM) + +#define _GNU_SOURCE +#include +#include +#include + +void __clear_cache(char *beginning, char *end) +{ +/* __ARM_NR_cacheflush is kernel private and should not be used in user space. + * However, there is no ARM asm parser in tcc so we use it for now */ +#if 1 + syscall(__ARM_NR_cacheflush, beginning, end, 0); +#else + __asm__ ("push {r7}\n\t" + "mov r7, #0xf0002\n\t" + "mov r2, #0\n\t" + "swi 0\n\t" + "pop {r7}\n\t" + "ret"); +#endif +} + #else #warning __clear_cache not defined for this architecture, avoid using tcc -run #endif diff --git a/libtcc.c b/libtcc.c index f691218..deda7e6 100644 --- a/libtcc.c +++ b/libtcc.c @@ -1,6 +1,6 @@ /* * TCC - Tiny C Compiler - * + * * Copyright (c) 2001-2004 Fabrice Bellard * * This library is free software; you can redistribute it and/or @@ -78,21 +78,13 @@ ST_FUNC void asm_global_instr(void) #endif /********************************************************/ - #ifdef _WIN32 -// GCC appears to use '/' for relative paths and '\\' for absolute paths on Windows static char *normalize_slashes(char *path) { char *p; - if (path[1] == ':') { - for (p = path+2; *p; ++p) - if (*p == '/') - *p = '\\'; - } else { - for (p = path; *p; ++p) - if (*p == '\\') - *p = '/'; - } + for (p = path; *p; ++p) + if (*p == '\\') + *p = '/'; return path; } @@ -129,7 +121,7 @@ void dlclose(void *p) #endif #ifdef LIBTCC_AS_DLL -BOOL WINAPI DllMain (HANDLE hDll, DWORD dwReason, LPVOID lpReserved) +BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) { if (DLL_PROCESS_ATTACH == dwReason) tcc_module = hDll; @@ -164,7 +156,7 @@ PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s) { int len; len = strlen(buf); - if (len < buf_size) + if (len < buf_size) pstrcpy(buf + len, buf_size - len, s); return buf; } @@ -222,7 +214,7 @@ PUB_FUNC void *tcc_malloc(unsigned long size) void *ptr; ptr = malloc(size); if (!ptr && size) - tcc_error("memory full"); + tcc_error("memory full (malloc)"); #ifdef MEM_DEBUG mem_cur_size += malloc_usable_size(ptr); if (mem_cur_size > mem_max_size) @@ -247,7 +239,7 @@ PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size) #endif ptr1 = realloc(ptr, size); if (!ptr1 && size) - tcc_error("memory full"); + tcc_error("memory full (realloc)"); #ifdef MEM_DEBUG /* NOTE: count not correct if alloc error, but not critical */ mem_cur_size += malloc_usable_size(ptr1); @@ -283,7 +275,7 @@ ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data) { int nb, nb_alloc; void **pp; - + nb = *nb_ptr; pp = *ptab; /* every power of two we double array size */ @@ -379,7 +371,7 @@ ST_FUNC void section_realloc(Section *sec, unsigned long new_size) { unsigned long size; unsigned char *data; - + size = sec->data_allocated; if (size == 0) size = 1; @@ -422,7 +414,7 @@ ST_FUNC Section *find_section(TCCState *s1, const char *name) int i; for(i = 1; i < s1->nb_sections; i++) { sec = s1->sections[i]; - if (!strcmp(name, sec->name)) + if (!strcmp(name, sec->name)) return sec; } /* sections are created as PROGBITS */ @@ -431,7 +423,7 @@ ST_FUNC Section *find_section(TCCState *s1, const char *name) /* update sym->c so that it points to an external symbol in section 'section' with value 'value' */ -ST_FUNC void put_extern_sym2(Sym *sym, Section *section, +ST_FUNC void put_extern_sym2(Sym *sym, Section *section, addr_t value, unsigned long size, int can_add_underscore) { @@ -442,7 +434,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, if (section == NULL) sh_num = SHN_UNDEF; - else if (section == SECTION_ABS) + else if (section == SECTION_ABS) sh_num = SHN_ABS; else sh_num = section->sh_num; @@ -476,13 +468,13 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, switch(sym->v) { #ifdef TCC_TARGET_PE /* XXX: we rely only on malloc hooks */ - case TOK_malloc: - case TOK_free: - case TOK_realloc: - case TOK_memalign: - case TOK_calloc: + case TOK_malloc: + case TOK_free: + case TOK_realloc: + case TOK_memalign: + case TOK_calloc: #endif - case TOK_memcpy: + case TOK_memcpy: case TOK_memmove: case TOK_memset: case TOK_strlen: @@ -499,23 +491,26 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, #ifdef TCC_TARGET_PE if (sym->type.t & VT_EXPORT) - other |= 1; + other |= ST_PE_EXPORT; if (sym_type == STT_FUNC && sym->type.ref) { - int attr = sym->type.ref->r; - if (FUNC_EXPORT(attr)) - other |= 1; - if (FUNC_CALL(attr) == FUNC_STDCALL && can_add_underscore) { - sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr) * PTR_SIZE); + Sym *ref = sym->type.ref; + if (ref->a.func_export) + other |= ST_PE_EXPORT; + if (ref->a.func_call == FUNC_STDCALL && can_add_underscore) { + sprintf(buf1, "_%s@%d", name, ref->a.func_args * PTR_SIZE); name = buf1; - other |= 2; + other |= ST_PE_STDCALL; can_add_underscore = 0; } } else { if (find_elf_sym(tcc_state->dynsymtab_section, name)) - other |= 4; + other |= ST_PE_IMPORT; if (sym->type.t & VT_IMPORT) - other |= 4; + other |= ST_PE_IMPORT; } +#else + if (! (sym->type.t & VT_STATIC)) + other = (sym->type.t & VT_VIS_MASK) >> VT_VIS_SHIFT; #endif if (tcc_state->leading_underscore && can_add_underscore) { buf1[0] = '_'; @@ -535,7 +530,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, } } -ST_FUNC void put_extern_sym(Sym *sym, Section *section, +ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size) { put_extern_sym2(sym, section, value, size, 1); @@ -575,7 +570,7 @@ static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap) { char buf[2048]; BufferedFile **pf, *f; - + buf[0] = '\0'; /* use upper file if inline ":asm:" or token ":paste:" */ for (f = file; f && f->filename[0] == ':'; f = f->prev) @@ -726,28 +721,28 @@ static int tcc_compile(TCCState *s1) cur_text_section = NULL; funcname = ""; - anon_sym = SYM_FIRST_ANOM; + anon_sym = SYM_FIRST_ANOM; /* file info: full path + filename */ section_sym = 0; /* avoid warning */ if (s1->do_debug) { - section_sym = put_elf_sym(symtab_section, 0, 0, - ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0, + section_sym = put_elf_sym(symtab_section, 0, 0, + ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0, text_section->sh_num, NULL); getcwd(buf, sizeof(buf)); #ifdef _WIN32 normalize_slashes(buf); #endif pstrcat(buf, sizeof(buf), "/"); - put_stabs_r(buf, N_SO, 0, 0, + put_stabs_r(buf, N_SO, 0, 0, text_section->data_offset, text_section, section_sym); - put_stabs_r(file->filename, N_SO, 0, 0, + put_stabs_r(file->filename, N_SO, 0, 0, text_section->data_offset, text_section, section_sym); } /* an elf symbol of type STT_FILE must be put so that STB_LOCAL symbols can be safely used */ - put_elf_sym(symtab_section, 0, 0, - ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0, + put_elf_sym(symtab_section, 0, 0, + ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0, SHN_ABS, file->filename); /* define some often used types */ @@ -765,7 +760,7 @@ static int tcc_compile(TCCState *s1) func_old_type.t = VT_FUNC; func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD); #ifdef TCC_TARGET_ARM - arm_init_types(); + arm_init(s1); #endif #if 0 @@ -802,7 +797,7 @@ static int tcc_compile(TCCState *s1) /* end of translation unit info */ if (s1->do_debug) { - put_stabs_r(NULL, N_SO, 0, 0, + put_stabs_r(NULL, N_SO, 0, 0, text_section->data_offset, text_section, section_sym); } } @@ -811,7 +806,7 @@ static int tcc_compile(TCCState *s1) /* reset define stack, but leave -Dsymbols (may be incorrect if they are undefined) */ - free_defines(define_start); + free_defines(define_start); gen_inline_functions(); @@ -950,6 +945,16 @@ LIBTCCAPI TCCState *tcc_new(void) tcc_define_symbol(s, "__arm", NULL); tcc_define_symbol(s, "arm", NULL); tcc_define_symbol(s, "__APCS_32__", NULL); + tcc_define_symbol(s, "__ARMEL__", NULL); +#if defined(TCC_ARM_EABI) + tcc_define_symbol(s, "__ARM_EABI__", NULL); +#endif +#if defined(TCC_ARM_HARDFLOAT) + s->float_abi = ARM_HARD_FLOAT; + tcc_define_symbol(s, "__ARM_PCS_VFP", NULL); +#else + s->float_abi = ARM_SOFTFP_FLOAT; +#endif #endif #ifdef TCC_TARGET_PE @@ -986,16 +991,23 @@ LIBTCCAPI TCCState *tcc_new(void) #ifdef TCC_TARGET_PE tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short"); + tcc_define_symbol(s, "__WINT_TYPE__", "unsigned short"); #else tcc_define_symbol(s, "__WCHAR_TYPE__", "int"); + /* wint_t is unsigned int by default, but (signed) int on BSDs + and unsigned short on windows. Other OSes might have still + other conventions, sigh. */ +#if defined(__FreeBSD__) || defined (__FreeBSD_kernel__) + tcc_define_symbol(s, "__WINT_TYPE__", "int"); +#else + tcc_define_symbol(s, "__WINT_TYPE__", "unsigned int"); +#endif #endif #ifndef TCC_TARGET_PE /* glibc defines */ tcc_define_symbol(s, "__REDIRECT(name, proto, alias)", "name proto __asm__ (#alias)"); tcc_define_symbol(s, "__REDIRECT_NTH(name, proto, alias)", "name proto __asm__ (#alias) __THROW"); - /* default library paths */ - tcc_add_library_path(s, CONFIG_TCC_LIBPATHS); /* paths for crt objects */ tcc_split_path(s, (void ***)&s->crt_paths, &s->nb_crt_paths, CONFIG_TCC_CRTPREFIX); #endif @@ -1011,13 +1023,13 @@ LIBTCCAPI TCCState *tcc_new(void) /* symbols are always generated for linking stage */ symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0, ".strtab", - ".hashtab", SHF_PRIVATE); + ".hashtab", SHF_PRIVATE); strtab_section = symtab_section->link; s->symtab = symtab_section; - + /* private symbol table for dynamic symbols */ s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE, - ".dynstrtab", + ".dynstrtab", ".dynhashtab", SHF_PRIVATE); s->alacarte_link = 1; s->nocommon = 1; @@ -1032,6 +1044,9 @@ LIBTCCAPI TCCState *tcc_new(void) #endif #ifdef TCC_TARGET_I386 s->seg_size = 32; +#endif +#ifdef TCC_IS_NATIVE + s->runtime_main = "main"; #endif return s; } @@ -1050,7 +1065,7 @@ LIBTCCAPI void tcc_delete(TCCState *s1) for(i = 0; i < s1->nb_priv_sections; i++) free_section(s1->priv_sections[i]); dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections); - + /* free any loaded DLLs */ #ifdef TCC_IS_NATIVE for ( i = 0; i < s1->nb_loaded_dlls; i++) { @@ -1059,7 +1074,7 @@ LIBTCCAPI void tcc_delete(TCCState *s1) dlclose(ref->handle); } #endif - + /* free loaded dlls array */ dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls); @@ -1085,12 +1100,14 @@ LIBTCCAPI void tcc_delete(TCCState *s1) #ifdef TCC_IS_NATIVE # ifdef HAVE_SELINUX munmap (s1->write_mem, s1->mem_size); - munmap (s1->runtime_mem, s1->mem_size); + munmap (s1->runtime_mem, s1->mem_size); # else tcc_free(s1->runtime_mem); # endif #endif + if(s1->sym_attrs) tcc_free(s1->sym_attrs); + tcc_free(s1); } @@ -1192,7 +1209,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) #endif ret = 0; } else { - ret = tcc_load_dll(s1, fd, filename, + ret = tcc_load_dll(s1, fd, filename, (flags & AFF_REFERENCED_DLL) != 0); } goto the_end; @@ -1299,8 +1316,6 @@ LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val) So it is handled here as if it were in a DLL. */ pe_putimport(s, 0, name, (uintptr_t)val); #else - /* XXX: Same problem on linux but currently "solved" elsewhere - via the rather dirty 'runtime_plt_and_got' hack. */ add_elf_sym(symtab_section, (uintptr_t)val, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, SHN_ABS, name); @@ -1324,9 +1339,9 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) /* define symbol */ tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL); /* create bounds sections */ - bounds_section = new_section(s, ".bounds", + bounds_section = new_section(s, ".bounds", SHT_PROGBITS, SHF_ALLOC); - lbounds_section = new_section(s, ".lbounds", + lbounds_section = new_section(s, ".lbounds", SHT_PROGBITS, SHF_ALLOC); } #endif @@ -1347,8 +1362,8 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) put_stabs("", 0, 0, 0, 0); } -#ifdef TCC_TARGET_PE tcc_add_library_path(s, CONFIG_TCC_LIBPATHS); +#ifdef TCC_TARGET_PE # ifdef _WIN32 tcc_add_systemdir(s); # endif @@ -1557,6 +1572,10 @@ static int tcc_set_linker(TCCState *s, const char *option) } else goto err; + } else if (link_option(option, "as-needed", &p)) { + ignoring = 1; + } else if (link_option(option, "O", &p)) { + ignoring = 1; } else if (link_option(option, "rpath=", &p)) { s->rpath = copy_linker_arg(p); } else if (link_option(option, "section-alignment=", &p)) { @@ -1628,6 +1647,7 @@ enum { TCC_OPTION_b, TCC_OPTION_g, TCC_OPTION_c, + TCC_OPTION_float_abi, TCC_OPTION_static, TCC_OPTION_shared, TCC_OPTION_soname, @@ -1647,7 +1667,6 @@ enum { TCC_OPTION_pedantic, TCC_OPTION_pthread, TCC_OPTION_run, - TCC_OPTION_norunsrc, TCC_OPTION_v, TCC_OPTION_w, TCC_OPTION_pipe, @@ -1680,6 +1699,9 @@ static const TCCOption tcc_options[] = { #endif { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, { "c", TCC_OPTION_c, 0 }, +#ifdef TCC_TARGET_ARM + { "mfloat-abi", TCC_OPTION_float_abi, TCC_OPTION_HAS_ARG }, +#endif { "static", TCC_OPTION_static, 0 }, { "shared", TCC_OPTION_shared, 0 }, { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG }, @@ -1687,7 +1709,6 @@ static const TCCOption tcc_options[] = { { "pedantic", TCC_OPTION_pedantic, 0}, { "pthread", TCC_OPTION_pthread, 0}, { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, - { "norunsrc", TCC_OPTION_norunsrc, 0 }, { "rdynamic", TCC_OPTION_rdynamic, 0 }, { "r", TCC_OPTION_r, 0 }, { "s", TCC_OPTION_s, 0 }, @@ -1699,7 +1720,7 @@ static const TCCOption tcc_options[] = { { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG }, { "nostdinc", TCC_OPTION_nostdinc, 0 }, { "nostdlib", TCC_OPTION_nostdlib, 0 }, - { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 }, + { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 }, { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, { "w", TCC_OPTION_w, 0 }, { "pipe", TCC_OPTION_pipe, 0}, @@ -1726,7 +1747,6 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) const TCCOption *popt; const char *optarg, *r; int run = 0; - int norunsrc = 0; int pthread = 0; int optind = 0; @@ -1739,8 +1759,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) r = argv[optind++]; if (r[0] != '-' || r[1] == '\0') { /* add a new file */ - if (!run || !norunsrc) - dynarray_add((void ***)&s->files, &s->nb_files, tcc_strdup(r)); + dynarray_add((void ***)&s->files, &s->nb_files, tcc_strdup(r)); if (run) { optind--; /* argv[0] will be this file */ @@ -1817,6 +1836,18 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) case TCC_OPTION_c: s->output_type = TCC_OUTPUT_OBJ; break; +#ifdef TCC_TARGET_ARM + case TCC_OPTION_float_abi: + /* tcc doesn't support soft float yet */ + if (!strcmp(optarg, "softfp")) { + s->float_abi = ARM_SOFTFP_FLOAT; + tcc_undefine_symbol(s, "__ARM_PCS_VFP"); + } else if (!strcmp(optarg, "hard")) + s->float_abi = ARM_HARD_FLOAT; + else + tcc_error("unsupported float abi '%s'", optarg); + break; +#endif case TCC_OPTION_static: s->static_link = 1; break; @@ -1854,9 +1885,6 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) tcc_set_options(s, optarg); run = 1; break; - case TCC_OPTION_norunsrc: - norunsrc = 1; - break; case TCC_OPTION_v: do ++s->verbose; while (*optarg++ == 'v'); break; @@ -1865,7 +1893,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) goto unsupported_option; break; case TCC_OPTION_W: - if (tcc_set_warning(s, optarg, 1) < 0 && + if (tcc_set_warning(s, optarg, 1) < 0 && s->warn_unsupported) goto unsupported_option; break; @@ -1952,7 +1980,7 @@ PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time) tt = 0.001; if (total_bytes < 1) total_bytes = 1; - printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n", + printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n", tok_ident - TOK_IDENT, total_lines, total_bytes, tt, (int)(total_lines / tt), total_bytes / tt / 1000000.0); @@ -1961,7 +1989,7 @@ PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time) PUB_FUNC void tcc_set_environment(TCCState *s) { char * path; - + path = getenv("C_INCLUDE_PATH"); if(path != NULL) { tcc_add_include_path(s, path); diff --git a/tcc-doc.texi b/tcc-doc.texi index dbfbb4a..68bac65 100644 --- a/tcc-doc.texi +++ b/tcc-doc.texi @@ -176,6 +176,9 @@ In a script, it gives the following header: #!/usr/local/bin/tcc -run -L/usr/X11R6/lib -lX11 @end example +@item -mfloat-abi (ARM only) +Select the float ABI. Possible values: @code{softfp} and @code{hard} + @item -dumpversion Print only the compiler version and nothing else. @@ -217,7 +220,7 @@ Undefine preprocessor symbol @samp{sym}. Compilation flags: -Note: each of the following warning options has a negative form beginning with +Note: each of the following options has a negative form beginning with @option{-fno-}. @table @option @@ -338,7 +341,7 @@ Generate additional support code to check memory allocations and array/pointer bounds. @option{-g} is implied. Note that the generated code is slower and bigger in this case. -Note: @option{-b} is only available on i386 for the moment. +Note: @option{-b} is only available on i386 when using libtcc for the moment. @item -bt N Display N callers in stack traces. This is useful with @option{-g} or @@ -967,7 +970,7 @@ be the best solution. #define VT_BITFIELD 0x0040 /* bitfield modifier */ #define VT_CONSTANT 0x0800 /* const modifier */ #define VT_VOLATILE 0x1000 /* volatile modifier */ -#define VT_SIGNED 0x2000 /* signed type */ +#define VT_DEFSIGN 0x2000 /* signed type */ #define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */ @end example @@ -1077,7 +1080,7 @@ are used when bound checking is activated @item stab_section @itemx stabstr_section -are used when debugging is actived to store debug information +are used when debugging is active to store debug information @item symtab_section @itemx strtab_section diff --git a/tcc.c b/tcc.c index b223d39..392efca 100644 --- a/tcc.c +++ b/tcc.c @@ -69,7 +69,6 @@ static void help(void) " -Bdir use 'dir' as tcc internal library and include path\n" " -MD generate target dependencies for make\n" " -MF depfile put generated dependencies here\n" - " -norunsrc Do not compile the file which is the first argument after -run.\n" ); } @@ -79,7 +78,7 @@ static void help(void) #include static int execvp_win32(const char *prog, char **argv) { - int ret = spawnvp(P_NOWAIT, prog, (char const*const*)argv); + int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv); if (-1 == ret) return ret; cwait(&ret, ret, WAIT_CHILD); @@ -224,7 +223,7 @@ static void display_info(TCCState *s, int what) print_paths("crt", s->crt_paths, s->nb_crt_paths); print_paths("libraries", s->library_paths, s->nb_library_paths); print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths); - printf("elfinterp:\n %s\n", CONFIG_TCC_ELFINTERP); + printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s)); break; } } diff --git a/tcc.h b/tcc.h index 859d4fd..c93cedf 100644 --- a/tcc.h +++ b/tcc.h @@ -55,18 +55,27 @@ # ifndef CONFIG_TCC_STATIC # include # endif -#else +/* XXX: need to define this to use them in non ISOC99 context */ + extern float strtof (const char *__nptr, char **__endptr); + extern long double strtold (const char *__nptr, char **__endptr); +#else /* on _WIN32: */ # include # include # include /* open, close etc. */ # include /* getcwd */ # ifdef __GNUC__ # include -# else - typedef UINT_PTR uintptr_t; # endif # define inline __inline # define inp next_inp +# define snprintf _snprintf +# define vsnprintf _vsnprintf +# ifndef __GNUC__ +# define strtold (long double)strtod +# define strtof (float)strtod +# define strtoll _strtoi64 +# define strtoull _strtoui64 +# endif # ifdef LIBTCC_AS_DLL # define LIBTCCAPI __declspec(dllexport) # define PUB_FUNC LIBTCCAPI @@ -79,6 +88,30 @@ # define O_BINARY 0 #endif +#ifdef __GNUC__ +# define NORETURN __attribute__ ((noreturn)) +#elif defined _MSC_VER +# define NORETURN __declspec(noreturn) +#else +# define NORETURN +#endif + +#ifdef _WIN32 +# define IS_DIRSEP(c) (c == '/' || c == '\\') +# define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2]))) +# define PATHCMP stricmp +#else +# define IS_DIRSEP(c) (c == '/') +# define IS_ABSPATH(p) IS_DIRSEP(p[0]) +# define PATHCMP strcmp +#endif + +#ifdef TCC_TARGET_PE +#define PATHSEP ';' +#else +#define PATHSEP ':' +#endif + #include "elf.h" #ifdef TCC_TARGET_X86_64 # define ELFCLASSW ELFCLASS64 @@ -128,7 +161,8 @@ #endif #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \ - !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64) + !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64) && \ + !defined(CONFIG_USE_LIBGCC) #define CONFIG_TCC_BCHECK /* enable bound checking code */ #endif @@ -169,9 +203,17 @@ # define CONFIG_LDDIR "lib" #endif +#ifdef CONFIG_MULTIARCHDIR +# define USE_MUADIR(s) s "/" CONFIG_MULTIARCHDIR +# define ALSO_MUADIR(s) s "/" CONFIG_MULTIARCHDIR ":" s +#else +# define USE_MUADIR(s) s +# define ALSO_MUADIR(s) s +#endif + /* path to find crt1.o, crti.o and crtn.o */ #ifndef CONFIG_TCC_CRTPREFIX -# define CONFIG_TCC_CRTPREFIX CONFIG_SYSROOT "/usr/" CONFIG_LDDIR +# define CONFIG_TCC_CRTPREFIX USE_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) #endif /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */ @@ -180,17 +222,10 @@ #ifndef CONFIG_TCC_SYSINCLUDEPATHS # ifdef TCC_TARGET_PE # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi" -# elif defined CONFIG_MULTIARCHDIR -# define CONFIG_TCC_SYSINCLUDEPATHS \ - CONFIG_SYSROOT "/usr/local/include" \ - ":" CONFIG_SYSROOT "/usr/local/include/" CONFIG_MULTIARCHDIR \ - ":" CONFIG_SYSROOT "/usr/include" \ - ":" CONFIG_SYSROOT "/usr/include/" CONFIG_MULTIARCHDIR \ - ":" "{B}/include" # else # define CONFIG_TCC_SYSINCLUDEPATHS \ - CONFIG_SYSROOT "/usr/local/include" \ - ":" CONFIG_SYSROOT "/usr/include" \ + ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/include") \ + ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/include") \ ":" "{B}/include" # endif #endif @@ -198,12 +233,12 @@ /* library search paths */ #ifndef CONFIG_TCC_LIBPATHS # ifdef TCC_TARGET_PE -# define CONFIG_TCC_LIBPATHS "{B}/lib;{B}" +# define CONFIG_TCC_LIBPATHS "{B}/lib" # else # define CONFIG_TCC_LIBPATHS \ - CONFIG_SYSROOT "/usr/" CONFIG_LDDIR \ - ":" CONFIG_SYSROOT "/" CONFIG_LDDIR \ - ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR + ALSO_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \ + ":" ALSO_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) \ + ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR) # endif #endif @@ -217,25 +252,30 @@ # else # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1" # endif +# elif defined __DragonFly__ +# define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2" # elif defined __GNU__ # define CONFIG_TCC_ELFINTERP "/lib/ld.so" -# elif defined TCC_ARM_HARDFLOAT -# define CONFIG_TCC_ELFINTERP "/lib/ld-linux-armhf.so.3" -# elif defined TCC_ARM_EABI -# define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.3" # elif defined(TCC_TARGET_X86_64) # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2" # elif defined(TCC_UCLIBC) # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" # elif defined(TCC_TARGET_PE) # define CONFIG_TCC_ELFINTERP "-" -# else +# elif !defined(TCC_ARM_EABI) # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2" # endif #endif +/* var elf_interp dans *-gen.c */ +#ifdef CONFIG_TCC_ELFINTERP +# define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP +#else +# define DEFAULT_ELFINTERP(s) default_elfinterp(s) +#endif + /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */ -#define TCC_LIBGCC CONFIG_SYSROOT "/" CONFIG_LDDIR "/libgcc_s.so.1" +#define TCC_LIBGCC USE_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1" /* -------------------------------------------- */ /* include the target specific definitions */ @@ -310,7 +350,7 @@ typedef union CValue { long long ll; unsigned long long ull; struct CString *cstr; - void *ptr; + addr_t ptr_offset; int tab[LDOUBLE_SIZE/4]; } CValue; @@ -324,11 +364,36 @@ typedef struct SValue { struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */ } SValue; +struct Attribute { + unsigned + func_call : 3, /* calling convention (0..5), see below */ + aligned : 5, /* alignement (0..16) */ + packed : 1, + func_export : 1, + func_import : 1, + func_args : 5, + func_proto : 1, + mode : 4, + weak : 1, + visibility : 2, + fill : 8; // 8 bits left to fit well in union below +}; + +/* GNUC attribute definition */ +typedef struct AttributeDef { + struct Attribute a; + struct Section *section; + int alias_target; /* token */ +} AttributeDef; + /* symbol management */ typedef struct Sym { int v; /* symbol token */ char *asm_label; /* associated asm label */ - long r; /* associated register */ + union { + long r; /* associated register */ + struct Attribute a; + }; union { long c; /* associated number */ int *d; /* define token stream */ @@ -379,32 +444,6 @@ typedef struct DLLReference { char name[1]; } DLLReference; -/* GNUC attribute definition */ -typedef struct AttributeDef { - unsigned - func_call : 3, /* calling convention (0..5), see below */ - aligned : 5, /* alignement (0..16) */ - packed : 1, - func_export : 1, - func_import : 1, - func_args : 5, - mode : 4, - weak : 1, - fill : 11; - struct Section *section; - int alias_target; /* token */ -} AttributeDef; - -/* gr: wrappers for casting sym->r for other purposes */ -#define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call) -#define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export) -#define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import) -#define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args) -#define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned) -#define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed) -#define ATTR_MODE(r) (((AttributeDef*)&(r))->mode) -#define INT_ATTR(ad) (*(int*)(ad)) - /* -------------------------------------------------- */ #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ @@ -519,6 +558,7 @@ typedef struct ASMOperand { struct sym_attr { unsigned long got_offset; + unsigned long plt_offset; #ifdef TCC_TARGET_ARM unsigned char plt_thumb_stub:1; #endif @@ -561,6 +601,9 @@ struct TCCState { /* compile with built-in memory and bounds checker */ int do_bounds_check; #endif +#ifdef TCC_TARGET_ARM + enum float_abi float_abi; /* float ABI of the generated code*/ +#endif addr_t text_addr; /* address of text section */ int has_text_addr; @@ -665,18 +708,13 @@ struct TCCState { #endif #ifdef TCC_IS_NATIVE + const char *runtime_main; /* for tcc_relocate */ void *runtime_mem; # ifdef HAVE_SELINUX void *write_mem; unsigned long mem_size; # endif -# if !defined TCC_TARGET_PE && (defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM) - /* write PLT and GOT here */ - char *runtime_plt_and_got; - unsigned runtime_plt_and_got_offset; -# define TCC_HAS_RUNTIME_PLTGOT -# endif #endif /* used by main and tcc_parse_args only */ @@ -738,7 +776,7 @@ struct TCCState { #define VT_BITFIELD 0x0040 /* bitfield modifier */ #define VT_CONSTANT 0x0800 /* const modifier */ #define VT_VOLATILE 0x1000 /* volatile modifier */ -#define VT_SIGNED 0x2000 /* signed type */ +#define VT_DEFSIGN 0x2000 /* signed type */ #define VT_VLA 0x00020000 /* VLA type (also has VT_PTR and VT_ARRAY) */ /* storage */ @@ -749,11 +787,19 @@ struct TCCState { #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */ #define VT_EXPORT 0x00008000 /* win32: data exported from dll */ #define VT_WEAK 0x00010000 /* weak symbol */ +#define VT_TLS 0x00040000 /* thread-local storage */ +#define VT_VIS_SHIFT 19 /* shift for symbol visibility, overlapping + bitfield values, because bitfields never + have linkage and hence never have + visibility. */ +#define VT_VIS_SIZE 2 /* We have four visibilities. */ +#define VT_VIS_MASK (((1 << VT_VIS_SIZE)-1) << VT_VIS_SHIFT) + +#define VT_STRUCT_SHIFT 19 /* shift for bitfield shift values (max: 32 - 2*6) */ -#define VT_STRUCT_SHIFT 18 /* shift for bitfield shift values (max: 32 - 2*6) */ /* type mask (except storage) */ -#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK) +#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK | VT_VIS_MASK) #define VT_TYPE (~(VT_STORAGE)) /* token values */ @@ -774,35 +820,39 @@ struct TCCState { #define TOK_LAND 0xa0 #define TOK_LOR 0xa1 - #define TOK_DEC 0xa2 #define TOK_MID 0xa3 /* inc/dec, to void constant */ #define TOK_INC 0xa4 #define TOK_UDIV 0xb0 /* unsigned division */ #define TOK_UMOD 0xb1 /* unsigned modulo */ #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */ -#define TOK_CINT 0xb3 /* number in tokc */ -#define TOK_CCHAR 0xb4 /* char constant in tokc */ -#define TOK_STR 0xb5 /* pointer to string in tokc */ -#define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */ -#define TOK_LCHAR 0xb7 -#define TOK_LSTR 0xb8 -#define TOK_CFLOAT 0xb9 /* float constant */ -#define TOK_LINENUM 0xba /* line number info */ -#define TOK_CDOUBLE 0xc0 /* double constant */ -#define TOK_CLDOUBLE 0xc1 /* long double constant */ + +/* tokens that carry values (in additional token string space / tokc) --> */ +#define TOK_CCHAR 0xb3 /* char constant in tokc */ +#define TOK_LCHAR 0xb4 +#define TOK_CINT 0xb5 /* number in tokc */ +#define TOK_CUINT 0xb6 /* unsigned int constant */ +#define TOK_CLLONG 0xb7 /* long long constant */ +#define TOK_CULLONG 0xb8 /* unsigned long long constant */ +#define TOK_STR 0xb9 /* pointer to string in tokc */ +#define TOK_LSTR 0xba +#define TOK_CFLOAT 0xbb /* float constant */ +#define TOK_CDOUBLE 0xbc /* double constant */ +#define TOK_CLDOUBLE 0xbd /* long double constant */ +#define TOK_PPNUM 0xbe /* preprocessor number */ +#define TOK_LINENUM 0xbf /* line number info */ +/* <-- */ + +#define TOK_TWOSHARPS 0xc0 /* ## preprocessing token */ +#define TOK_PLCHLDR 0xc1 /* placeholder token as defined in C99 */ #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */ #define TOK_ADDC1 0xc3 /* add with carry generation */ #define TOK_ADDC2 0xc4 /* add with carry use */ #define TOK_SUBC1 0xc5 /* add with carry generation */ #define TOK_SUBC2 0xc6 /* add with carry use */ -#define TOK_CUINT 0xc8 /* unsigned int constant */ -#define TOK_CLLONG 0xc9 /* long long constant */ -#define TOK_CULLONG 0xca /* unsigned long long constant */ #define TOK_ARROW 0xcb #define TOK_DOTS 0xcc /* three dots */ #define TOK_SHR 0xcd /* unsigned shift right */ -#define TOK_PPNUM 0xce /* preprocessor number */ #define TOK_NOSUBST 0xcf /* means following token has already been pp'd */ #define TOK_SHL 0x01 /* shift left */ @@ -926,37 +976,6 @@ enum tcc_token { #define TOK_UIDENT TOK_DEFINE -#ifdef _WIN32 -#define snprintf _snprintf -#define vsnprintf _vsnprintf -#ifndef __GNUC__ -# define strtold (long double)strtod -# define strtof (float)strtod -# define strtoll _strtoi64 -# define strtoull _strtoui64 -#endif -#else -/* XXX: need to define this to use them in non ISOC99 context */ -extern float strtof (const char *__nptr, char **__endptr); -extern long double strtold (const char *__nptr, char **__endptr); -#endif - -#ifdef _WIN32 -#define IS_DIRSEP(c) (c == '/' || c == '\\') -#define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2]))) -#define PATHCMP stricmp -#else -#define IS_DIRSEP(c) (c == '/') -#define IS_ABSPATH(p) IS_DIRSEP(p[0]) -#define PATHCMP strcmp -#endif - -#ifdef TCC_TARGET_PE -#define PATHSEP ';' -#else -#define PATHSEP ':' -#endif - /* space exlcuding newline */ static inline int is_space(int ch) { @@ -1033,7 +1052,7 @@ PUB_FUNC char *tcc_strdup(const char *str); #define strdup(s) use_tcc_strdup(s) PUB_FUNC void tcc_memstats(void); PUB_FUNC void tcc_error_noabort(const char *fmt, ...); -PUB_FUNC void tcc_error(const char *fmt, ...); +PUB_FUNC NORETURN void tcc_error(const char *fmt, ...); PUB_FUNC void tcc_warning(const char *fmt, ...); /* other utilities */ @@ -1131,7 +1150,7 @@ ST_FUNC void preprocess_init(TCCState *s1); ST_FUNC void preprocess_new(void); ST_FUNC int tcc_preprocess(TCCState *s1); ST_FUNC void skip(int c); -ST_FUNC void expect(const char *msg); +ST_FUNC NORETURN void expect(const char *msg); /* ------------ tccgen.c ------------ */ @@ -1169,6 +1188,7 @@ ST_DATA int const_wanted; /* true if constant wanted */ ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */ ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */ ST_DATA CType func_vt; /* current function return type (used by return instruction) */ +ST_DATA int func_var; /* true if current function is variadic */ ST_DATA int func_vc; ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ ST_DATA char *funcname; @@ -1250,6 +1270,7 @@ ST_FUNC void put_stabd(int type, int other, int desc); ST_FUNC void relocate_common_syms(void); ST_FUNC void relocate_syms(TCCState *s1, int do_resolve); ST_FUNC void relocate_section(TCCState *s1, Section *s); +ST_FUNC void relocate_plt(TCCState *s1); ST_FUNC void tcc_add_linker_symbols(TCCState *s1); ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset); @@ -1260,7 +1281,7 @@ ST_FUNC void build_got_entries(TCCState *s1); ST_FUNC void tcc_add_runtime(TCCState *s1); ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err); -#ifdef TCC_IS_NATIVE +#if defined TCC_IS_NATIVE || defined TCC_TARGET_PE ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name); #endif @@ -1281,7 +1302,7 @@ ST_FUNC void gsym_addr(int t, int a); ST_FUNC void gsym(int t); ST_FUNC void load(int r, SValue *sv); ST_FUNC void store(int r, SValue *v); -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *align); +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align); ST_FUNC void gfunc_call(int nb_args); ST_FUNC void gfunc_prolog(CType *func_type); ST_FUNC void gfunc_epilog(void); @@ -1326,7 +1347,10 @@ ST_FUNC void gen_opl(int op); /* ------------ arm-gen.c ------------ */ #ifdef TCC_TARGET_ARM -ST_FUNC void arm_init_types(void); +#ifdef TCC_ARM_EABI +ST_FUNC char *default_elfinterp(struct TCCState *s); +#endif +ST_FUNC void arm_init(struct TCCState *s); ST_FUNC uint32_t encbranch(int pos, int addr, int fail); ST_FUNC void gen_cvt_itof1(int t); #endif @@ -1368,6 +1392,10 @@ ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2); #ifdef TCC_TARGET_X86_64 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack); #endif +/* symbol properties stored in Elf32_Sym->st_other */ +# define ST_PE_EXPORT 0x10 +# define ST_PE_IMPORT 0x20 +# define ST_PE_STDCALL 0x40 #endif /* ------------ tccrun.c ----------------- */ diff --git a/tccasm.c b/tccasm.c index 9c77960..38efe1c 100644 --- a/tccasm.c +++ b/tccasm.c @@ -232,7 +232,7 @@ static inline void asm_expr_sum(TCCState *s1, ExprValue *pe) } else { goto cannot_relocate; } - pe->sym = NULL; /* same symbols can be substracted to NULL */ + pe->sym = NULL; /* same symbols can be subtracted to NULL */ } else { cannot_relocate: tcc_error("invalid operation with label"); @@ -483,6 +483,7 @@ static void asm_parse_directive(TCCState *s1) case TOK_ASM_globl: case TOK_ASM_global: case TOK_ASM_weak: + case TOK_ASM_hidden: tok1 = tok; do { Sym *sym; @@ -493,9 +494,12 @@ static void asm_parse_directive(TCCState *s1) sym = label_push(&s1->asm_labels, tok, 0); sym->type.t = VT_VOID; } - sym->type.t &= ~VT_STATIC; + if (tok1 != TOK_ASM_hidden) + sym->type.t &= ~VT_STATIC; if (tok1 == TOK_ASM_weak) sym->type.t |= VT_WEAK; + else if (tok1 == TOK_ASM_hidden) + sym->type.t |= STV_HIDDEN << VT_VIS_SHIFT; next(); } while (tok == ','); break; @@ -588,12 +592,12 @@ static void asm_parse_directive(TCCState *s1) tcc_error("label not found: %s", get_tok_str(tok, NULL)); } - next(); - skip(','); /* XXX .size name,label2-label1 */ if (s1->warn_unsupported) tcc_warning("ignoring .size %s,*", get_tok_str(tok, NULL)); + next(); + skip(','); while (tok != '\n' && tok != CH_EOF) { next(); } @@ -622,7 +626,7 @@ static void asm_parse_directive(TCCState *s1) } if (!strcmp(newtype, "function") || !strcmp(newtype, "STT_FUNC")) { - sym->type.t = VT_FUNC; + sym->type.t = (sym->type.t & ~VT_BTYPE) | VT_FUNC; } else if (s1->warn_unsupported) tcc_warning("change type of '%s' from 0x%x to '%s' ignored", diff --git a/tccelf.c b/tccelf.c index 8af4bb6..f0ed22b 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1,6 +1,6 @@ /* * ELF file handling for TCC - * + * * Copyright (c) 2001-2004 Fabrice Bellard * * This library is free software; you can redistribute it and/or @@ -20,6 +20,10 @@ #include "tcc.h" +/* Define this to get some debug output during relocation processing. */ +#undef DEBUG_RELOC + +/* XXX: avoid static variable */ static int new_undef_sym = 0; /* Is there a new undefined sym since last new_undef_sym() */ ST_FUNC int put_elf_str(Section *s, const char *sym) @@ -38,7 +42,7 @@ ST_FUNC int put_elf_str(Section *s, const char *sym) static unsigned long elf_hash(const unsigned char *name) { unsigned long h = 0, g; - + while (*name) { h = (h << 4) + *name++; g = h & 0xf0000000; @@ -55,7 +59,7 @@ static void rebuild_hash(Section *s, unsigned int nb_buckets) { ElfW(Sym) *sym; int *ptr, *hash, nb_syms, sym_index, h; - char *strtab; + unsigned char *strtab; strtab = s->link->data; nb_syms = s->data_offset / sizeof(ElfW(Sym)); @@ -91,7 +95,7 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int nbuckets, h; ElfW(Sym) *sym; Section *hs; - + sym = section_ptr_add(s, sizeof(ElfW(Sym))); if (name) name_offset = put_elf_str(s->link, name); @@ -114,7 +118,7 @@ ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, if (ELFW(ST_BIND)(info) != STB_LOCAL) { /* add another hashing entry */ nbuckets = base[0]; - h = elf_hash(name) % nbuckets; + h = elf_hash((unsigned char *) name) % nbuckets; *ptr = base[2 + h]; base[2 + h] = sym_index; base[1]++; @@ -139,16 +143,16 @@ ST_FUNC int find_elf_sym(Section *s, const char *name) Section *hs; int nbuckets, sym_index, h; const char *name1; - + hs = s->hash; if (!hs) return 0; nbuckets = ((int *)hs->data)[0]; - h = elf_hash(name) % nbuckets; + h = elf_hash((unsigned char *) name) % nbuckets; sym_index = ((int *)hs->data)[2 + h]; while (sym_index != 0) { sym = &((ElfW(Sym) *)s->data)[sym_index]; - name1 = s->link->data + sym->st_name; + name1 = (char *) s->link->data + sym->st_name; if (!strcmp(name, name1)) return sym_index; sym_index = ((int *)hs->data)[2 + nbuckets + sym_index]; @@ -178,11 +182,11 @@ LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name) return (void*)(uintptr_t)get_elf_sym_addr(s, name, 0); } -#ifdef TCC_IS_NATIVE +#if defined TCC_IS_NATIVE || defined TCC_TARGET_PE /* return elf symbol value or error */ ST_FUNC void* tcc_get_symbol_err(TCCState *s, const char *name) { - return (void*)get_elf_sym_addr(s, name, 1); + return (void*)(uintptr_t)get_elf_sym_addr(s, name, 1); } #endif @@ -198,7 +202,7 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, sym_bind = ELFW(ST_BIND)(info); sym_type = ELFW(ST_TYPE)(info); sym_vis = ELFW(ST_VISIBILITY)(other); - + if (sym_bind != STB_LOCAL) { /* we search global or weak symbols */ sym_index = find_elf_sym(s, name); @@ -257,8 +261,8 @@ ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, } } else { do_def: - sym_index = put_elf_sym(s, value, size, - ELFW(ST_INFO)(sym_bind, sym_type), other, + sym_index = put_elf_sym(s, value, size, + ELFW(ST_INFO)(sym_bind, sym_type), other, sh_num, name); } return sym_index; @@ -311,11 +315,11 @@ ST_FUNC void put_stabs(const char *str, int type, int other, int desc, sym->n_value = value; } -ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, +ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index) { put_stabs(str, type, other, desc, value); - put_elf_reloc(symtab_section, stab_section, + put_elf_reloc(symtab_section, stab_section, stab_section->data_offset - sizeof(unsigned int), R_DATA_32, sym_index); } @@ -330,6 +334,12 @@ ST_FUNC void put_stabd(int type, int other, int desc) put_stabs(NULL, type, other, desc, 0); } +/* Browse each elem of type in section starting at elem + using variable */ +#define for_each_elem(sec, startoff, elem, type) \ + for (elem = (type *) sec->data + startoff; \ + elem < (type *) (sec->data + sec->data_offset); elem++) + /* In an ELF file symbol table, the local symbols must appear below the global and weak ones. Since TCC cannot sort it while generating the code, we must do it after. All the relocation tables are also @@ -340,7 +350,7 @@ static void sort_syms(TCCState *s1, Section *s) ElfW(Sym) *new_syms; int nb_syms, i; ElfW(Sym) *p, *q; - ElfW_Rel *rel, *rel_end; + ElfW_Rel *rel; Section *sr; int type, sym_index; @@ -370,7 +380,7 @@ static void sort_syms(TCCState *s1, Section *s) } p++; } - + /* we copy the new symbols to the old */ memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym))); tcc_free(new_syms); @@ -379,10 +389,7 @@ static void sort_syms(TCCState *s1, Section *s) for(i = 1; i < s1->nb_sections; i++) { sr = s1->sections[i]; if (sr->sh_type == SHT_RELX && sr->link == s) { - rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); - for(rel = (ElfW_Rel *)sr->data; - rel < rel_end; - rel++) { + for_each_elem(sr, 0, rel, ElfW_Rel) { sym_index = ELFW(R_SYM)(rel->r_info); type = ELFW(R_TYPE)(rel->r_info); sym_index = old_to_new_syms[sym_index]; @@ -390,20 +397,17 @@ static void sort_syms(TCCState *s1, Section *s) } } } - + tcc_free(old_to_new_syms); } /* relocate common symbols in the .bss section */ ST_FUNC void relocate_common_syms(void) { - ElfW(Sym) *sym, *sym_end; + ElfW(Sym) *sym; unsigned long offset, align; - - sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset); - for(sym = (ElfW(Sym) *)symtab_section->data + 1; - sym < sym_end; - sym++) { + + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { if (sym->st_shndx == SHN_COMMON) { /* align symbol */ align = sym->st_value; @@ -421,24 +425,25 @@ ST_FUNC void relocate_common_syms(void) true and output error if undefined symbol. */ ST_FUNC void relocate_syms(TCCState *s1, int do_resolve) { - ElfW(Sym) *sym, *esym, *sym_end; + ElfW(Sym) *sym, *esym; int sym_bind, sh_num, sym_index; const char *name; - sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset); - for(sym = (ElfW(Sym) *)symtab_section->data + 1; - sym < sym_end; - sym++) { + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { sh_num = sym->st_shndx; if (sh_num == SHN_UNDEF) { - name = strtab_section->data + sym->st_name; + name = (char *) strtab_section->data + sym->st_name; + /* Use ld.so to resolve symbol for us (for tcc -run) */ if (do_resolve) { #if defined TCC_IS_NATIVE && !defined _WIN32 void *addr; - name = symtab_section->link->data + sym->st_name; + name = (char *) symtab_section->link->data + sym->st_name; addr = resolve_sym(s1, name); if (addr) { sym->st_value = (addr_t)addr; +#ifdef DEBUG_RELOC + printf ("relocate_sym: %s -> 0x%x\n", name, sym->st_value); +#endif goto found; } #endif @@ -471,61 +476,22 @@ ST_FUNC void relocate_syms(TCCState *s1, int do_resolve) } } -#ifdef TCC_HAS_RUNTIME_PLTGOT -#ifdef TCC_TARGET_X86_64 -#define JMP_TABLE_ENTRY_SIZE 14 -static addr_t add_jmp_table(TCCState *s1, addr_t val) -{ - char *p = s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset; - s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE; - /* jmp *0x0(%rip) */ - p[0] = 0xff; - p[1] = 0x25; - *(int *)(p + 2) = 0; - *(addr_t *)(p + 6) = val; - return (addr_t)p; -} - -static addr_t add_got_table(TCCState *s1, addr_t val) -{ - addr_t *p = (addr_t *)(s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset); - s1->runtime_plt_and_got_offset += sizeof(addr_t); - *p = val; - return (addr_t)p; -} -#elif defined TCC_TARGET_ARM -#define JMP_TABLE_ENTRY_SIZE 8 -static addr_t add_jmp_table(TCCState *s1, int val) -{ - uint32_t *p = (uint32_t *)(s1->runtime_plt_and_got + s1->runtime_plt_and_got_offset); - s1->runtime_plt_and_got_offset += JMP_TABLE_ENTRY_SIZE; - /* ldr pc, [pc, #-4] */ - p[0] = 0xE51FF004; - p[1] = val; - return (addr_t)p; -} -#endif -#endif /* def TCC_HAS_RUNTIME_PLTGOT */ - -/* relocate a given section (CPU dependent) */ +/* relocate a given section (CPU dependent) by applying the relocations + in the associated relocation section */ ST_FUNC void relocate_section(TCCState *s1, Section *s) { - Section *sr; - ElfW_Rel *rel, *rel_end, *qrel; + Section *sr = s->reloc; + ElfW_Rel *rel; ElfW(Sym) *sym; int type, sym_index; unsigned char *ptr; addr_t val, addr; #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 + ElfW_Rel *qrel = (ElfW_Rel *) sr->data; /* ptr to next reloc entry reused */ int esym_index; #endif - sr = s->reloc; - rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); - qrel = (ElfW_Rel *)sr->data; - for(rel = qrel; - rel < rel_end; - rel++) { + for_each_elem(sr, 0, rel, ElfW_Rel) { ptr = s->data + rel->r_offset; sym_index = ELFW(R_SYM)(rel->r_info); @@ -588,13 +554,13 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) case R_386_16: if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) { output_file: - tcc_error("can only produce 16-bit binary files"); + tcc_error("can only produce 16-bit binary files"); } *(short *)ptr += val; break; case R_386_PC16: if (s1->output_format != TCC_OUTPUT_FORMAT_BINARY) - goto output_file; + goto output_file; *(short *)ptr += val - addr; break; #elif defined(TCC_TARGET_ARM) @@ -603,29 +569,30 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) case R_ARM_JUMP24: case R_ARM_PLT32: { - int x, is_thumb, is_call, h, blx_avail; - x = (*(int *)ptr)&0xffffff; + int x, is_thumb, is_call, h, blx_avail, is_bl, th_ko; + x = (*(int *) ptr) & 0xffffff; + if (sym->st_shndx == SHN_UNDEF) + val = s1->plt->sh_addr; +#ifdef DEBUG_RELOC + printf ("reloc %d: x=0x%x val=0x%x ", type, x, val); +#endif (*(int *)ptr) &= 0xff000000; if (x & 0x800000) x -= 0x1000000; x <<= 2; blx_avail = (TCC_ARM_VERSION >= 5); is_thumb = val & 1; - is_call = (type == R_ARM_CALL); + is_bl = (*(unsigned *) ptr) >> 24 == 0xeb; + is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl)); x += val - addr; - h = x & 2; -#ifdef TCC_HAS_RUNTIME_PLTGOT - if (s1->output_type == TCC_OUTPUT_MEMORY) { - if ((x & 3) || x >= 0x2000000 || x < -0x2000000) - if (!(x & 3) || !blx_avail || !is_call) { - x += add_jmp_table(s1, val) - val; /* add veneer */ - is_thumb = 0; /* Veneer uses ARM instructions */ - } - } +#ifdef DEBUG_RELOC + printf (" newx=0x%x name=%s\n", x, + (char *) symtab_section->link->data + sym->st_name); #endif - if ((x & 3) || x >= 0x2000000 || x < -0x2000000) - if (!(x & 3) || !blx_avail || !is_call) - tcc_error("can't relocate value at %x",addr); + h = x & 2; + th_ko = (x & 3) && (!blx_avail || !is_call); + if (th_ko || x >= 0x2000000 || x < -0x2000000) + tcc_error("can't relocate value at %x,%d",addr, type); x >>= 2; x &= 0xffffff; /* Only reached if blx is avail and it is a call */ @@ -633,15 +600,15 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) x |= h << 24; (*(int *)ptr) = 0xfa << 24; /* bl -> blx */ } - (*(int *)ptr) |= x; + (*(int *) ptr) |= x; } break; /* Since these relocations only concern Thumb-2 and blx instruction was introduced before Thumb-2, we can assume blx is available and not guard its use */ - case R_ARM_THM_CALL: + case R_ARM_THM_PC22: case R_ARM_THM_JUMP24: - { + { int x, hi, lo, s, j1, j2, i1, i2, imm10, imm11; int to_thumb, is_call, to_plt, blx_bit = 1 << 12; Section *plt; @@ -671,7 +638,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) plt = s1->plt; to_plt = (val >= plt->sh_addr) && (val < plt->sh_addr + plt->data_offset); - is_call = (type == R_ARM_THM_CALL); + is_call = (type == R_ARM_THM_PC22); /* Compute final offset */ if (to_plt && !is_call) /* Point to 1st instr of Thumb stub */ @@ -689,7 +656,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) - instruction must be a call (bl) or a jump to PLT */ if (!to_thumb || x >= 0x1000000 || x < -0x1000000) if (to_thumb || (val & 2) || (!is_call && !to_plt)) - tcc_error("can't relocate value at %x",addr); + tcc_error("can't relocate value at %x,%d",addr, type); /* Compute and store final offset */ s = (x >> 24) & 1; @@ -719,7 +686,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) *(int *)ptr |= x; else *(int *)ptr += x; - } + } break; case R_ARM_THM_MOVT_ABS: case R_ARM_THM_MOVW_ABS_NC: @@ -736,7 +703,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) *(int *)ptr |= x; else *(int *)ptr += x; - } + } break; case R_ARM_PREL31: { @@ -746,7 +713,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) x = (x * 2) / 2; x += val - addr; if((x^(x>>1))&0x40000000) - tcc_error("can't relocate value at %x",addr); + tcc_error("can't relocate value at %x,%d",addr, type); (*(int *)ptr) |= x & 0x7fffffff; } case R_ARM_ABS32: @@ -755,13 +722,13 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) case R_ARM_REL32: *(int *)ptr += val - addr; break; - case R_ARM_BASE_PREL: + case R_ARM_GOTPC: *(int *)ptr += s1->got->sh_addr - addr; break; - case R_ARM_GOTOFF32: + case R_ARM_GOTOFF: *(int *)ptr += val - s1->got->sh_addr; break; - case R_ARM_GOT_BREL: + case R_ARM_GOT32: /* we load the got offset */ *(int *)ptr += s1->sym_attrs[sym_index].got_offset; break; @@ -772,6 +739,14 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) if ((0x0ffffff0 & *(int*)ptr) == 0x012FFF10) *(int*)ptr ^= 0xE12FFF10 ^ 0xE1A0F000; /* BX Rm -> MOV PC, Rm */ break; + case R_ARM_GLOB_DAT: + case R_ARM_JUMP_SLOT: + *(addr_t *)ptr = val; + break; + case R_ARM_NONE: + /* Nothing to do. Normally used to indicate a dependency + on a certain symbol (like for exception handling under EABI). */ + break; default: fprintf(stderr,"FIXME: handle reloc type %x at %x [%p] to %x\n", type, (unsigned)addr, ptr, (unsigned)val); @@ -783,15 +758,15 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) case R_C60LO16: { uint32_t orig; - - /* put the low 16 bits of the absolute address */ - // add to what is already there - + + /* put the low 16 bits of the absolute address + add to what is already there */ + orig = ((*(int *)(ptr )) >> 7) & 0xffff; orig |= (((*(int *)(ptr+4)) >> 7) & 0xffff) << 16; - - //patch both at once - assumes always in pairs Low - High - + + /* patch both at once - assumes always in pairs Low - High */ + *(int *) ptr = (*(int *) ptr & (~(0xffff << 7)) ) | (((val+orig) & 0xffff) << 7); *(int *)(ptr+4) = (*(int *)(ptr+4) & (~(0xffff << 7)) ) | ((((val+orig)>>16) & 0xffff) << 7); } @@ -805,9 +780,18 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) #elif defined(TCC_TARGET_X86_64) case R_X86_64_64: if (s1->output_type == TCC_OUTPUT_DLL) { - qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE); - qrel->r_addend = *(long long *)ptr + val; - qrel++; + esym_index = s1->symtab_to_dynsym[sym_index]; + qrel->r_offset = rel->r_offset; + if (esym_index) { + qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64); + qrel->r_addend = rel->r_addend; + qrel++; + break; + } else { + qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE); + qrel->r_addend = *(long long *)ptr + val; + qrel++; + } } *(long long *)ptr += val; break; @@ -835,22 +819,21 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) break; } } - /* fall through */ - case R_X86_64_PLT32: { + goto plt32pc32; + + case R_X86_64_PLT32: + /* We've put the PLT slot offset into r_addend when generating + it, and that's what we must use as relocation value (adjusted + by section offset of course). */ + val = s1->plt->sh_addr + rel->r_addend; + /* fallthrough. */ + + plt32pc32: + { long long diff; diff = (long long)val - addr; if (diff <= -2147483647 || diff > 2147483647) { -#ifdef TCC_HAS_RUNTIME_PLTGOT - /* XXX: naive support for over 32bit jump */ - if (s1->output_type == TCC_OUTPUT_MEMORY) { - val = (add_jmp_table(s1, val - rel->r_addend) + - rel->r_addend); - diff = val - addr; - } -#endif - if (diff <= -2147483647 || diff > 2147483647) { - tcc_error("internal error: relocation failed"); - } + tcc_error("internal error: relocation failed"); } *(int *)ptr += diff; } @@ -858,16 +841,9 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) case R_X86_64_GLOB_DAT: case R_X86_64_JUMP_SLOT: /* They don't need addend */ - *(int *)ptr = val - rel->r_addend; + *(addr_t *)ptr = val - rel->r_addend; break; case R_X86_64_GOTPCREL: -#ifdef TCC_HAS_RUNTIME_PLTGOT - if (s1->output_type == TCC_OUTPUT_MEMORY) { - val = add_got_table(s1, val - rel->r_addend) + rel->r_addend; - *(int *)ptr += val - addr; - break; - } -#endif *(int *)ptr += (s1->got->sh_addr - addr + s1->sym_attrs[sym_index].got_offset - 4); break; @@ -892,27 +868,22 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) static void relocate_rel(TCCState *s1, Section *sr) { Section *s; - ElfW_Rel *rel, *rel_end; - + ElfW_Rel *rel; + s = s1->sections[sr->sh_info]; - rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); - for(rel = (ElfW_Rel *)sr->data; - rel < rel_end; - rel++) { + for_each_elem(sr, 0, rel, ElfW_Rel) rel->r_offset += s->sh_addr; - } } /* count the number of dynamic relocations so that we can reserve their space */ static int prepare_dynamic_rel(TCCState *s1, Section *sr) { - ElfW_Rel *rel, *rel_end; + ElfW_Rel *rel; int sym_index, esym_index, type, count; count = 0; - rel_end = (ElfW_Rel *)(sr->data + sr->data_offset); - for(rel = (ElfW_Rel *)sr->data; rel < rel_end; rel++) { + for_each_elem(sr, 0, rel, ElfW_Rel) { sym_index = ELFW(R_SYM)(rel->r_info); type = ELFW(R_TYPE)(rel->r_info); switch(type) { @@ -989,7 +960,7 @@ static void build_got(TCCState *s1) /* if no got, then create it */ s1->got = new_section(s1, ".got", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE); s1->got->sh_entsize = 4; - add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), + add_elf_sym(symtab_section, 0, 4, ELFW(ST_INFO)(STB_GLOBAL, STT_OBJECT), 0, s1->got->sh_num, "_GLOBAL_OFFSET_TABLE_"); ptr = section_ptr_add(s1->got, 3 * PTR_SIZE); #if PTR_SIZE == 4 @@ -1010,43 +981,64 @@ static void build_got(TCCState *s1) #endif } -/* put a got entry corresponding to a symbol in symtab_section. 'size' - and 'info' can be modifed if more precise info comes from the DLL */ -static void put_got_entry(TCCState *s1, - int reloc_type, unsigned long size, int info, - int sym_index) +/* put a got or plt entry corresponding to a symbol in symtab_section. 'size' + and 'info' can be modifed if more precise info comes from the DLL. + Returns offset of GOT or PLT slot. */ +static unsigned long put_got_entry(TCCState *s1, + int reloc_type, unsigned long size, int info, + int sym_index) { - int index; + int index, need_plt_entry; const char *name; ElfW(Sym) *sym; unsigned long offset; int *ptr; + struct sym_attr *symattr; if (!s1->got) build_got(s1); - /* if a got entry already exists for that symbol, no need to add one */ - if (sym_index < s1->nb_sym_attrs && - s1->sym_attrs[sym_index].got_offset) - return; - - alloc_sym_attr(s1, sym_index)->got_offset = s1->got->data_offset; - - if (s1->dynsym) { - sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; - name = symtab_section->link->data + sym->st_name; - offset = sym->st_value; -#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) - if (reloc_type == + need_plt_entry = #ifdef TCC_TARGET_X86_64 - R_X86_64_JUMP_SLOT + (reloc_type == R_X86_64_JUMP_SLOT); +#elif defined(TCC_TARGET_I386) + (reloc_type == R_386_JMP_SLOT); +#elif defined(TCC_TARGET_ARM) + (reloc_type == R_ARM_JUMP_SLOT); #else - R_386_JMP_SLOT + 0; #endif - ) { + + if (need_plt_entry && !s1->plt) { + /* add PLT */ + s1->plt = new_section(s1, ".plt", SHT_PROGBITS, + SHF_ALLOC | SHF_EXECINSTR); + s1->plt->sh_entsize = 4; + } + + /* If a got/plt entry already exists for that symbol, no need to add one */ + if (sym_index < s1->nb_sym_attrs) { + if (need_plt_entry && s1->sym_attrs[sym_index].plt_offset) + return s1->sym_attrs[sym_index].plt_offset; + else if (!need_plt_entry && s1->sym_attrs[sym_index].got_offset) + return s1->sym_attrs[sym_index].got_offset; + } + + symattr = alloc_sym_attr(s1, sym_index); + + /* Only store the GOT offset if it's not generated for the PLT entry. */ + if (!need_plt_entry) + symattr->got_offset = s1->got->data_offset; + + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + name = (char *) symtab_section->link->data + sym->st_name; + offset = sym->st_value; +#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64) + if (need_plt_entry) { Section *plt; uint8_t *p; int modrm; + unsigned long relofs; #if defined(TCC_OUTPUT_DLL_WITH_PLT) modrm = 0x25; @@ -1071,27 +1063,36 @@ static void put_got_entry(TCCState *s1, put32(p + 8, PTR_SIZE * 2); } + /* The PLT slot refers to the relocation entry it needs + via offset. The reloc entry is created below, so its + offset is the current data_offset. */ + relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0; + symattr->plt_offset = plt->data_offset; p = section_ptr_add(plt, 16); p[0] = 0xff; /* jmp *(got + x) */ p[1] = modrm; put32(p + 2, s1->got->data_offset); p[6] = 0x68; /* push $xxx */ - put32(p + 7, (plt->data_offset - 32) >> 1); +#ifdef TCC_TARGET_X86_64 + /* On x86-64, the relocation is referred to by _index_. */ + put32(p + 7, relofs / sizeof (ElfW_Rel)); +#else + put32(p + 7, relofs); +#endif p[11] = 0xe9; /* jmp plt_start */ put32(p + 12, -(plt->data_offset)); - /* the symbol is modified so that it will be relocated to - the PLT */ -#if !defined(TCC_OUTPUT_DLL_WITH_PLT) - if (s1->output_type == TCC_OUTPUT_EXE) -#endif - offset = plt->data_offset - 16; + /* If this was an UNDEF symbol set the offset in the + dynsymtab to the PLT slot, so that PC32 relocs to it + can be resolved. */ + if (sym->st_shndx == SHN_UNDEF) + offset = plt->data_offset - 16; } #elif defined(TCC_TARGET_ARM) - if (reloc_type == R_ARM_JUMP_SLOT) { + if (need_plt_entry) { Section *plt; uint8_t *p; - + /* if we build a DLL, we add a %ebx offset */ if (s1->output_type == TCC_OUTPUT_DLL) tcc_error("DLLs unimplemented!"); @@ -1101,50 +1102,68 @@ static void put_got_entry(TCCState *s1, if (plt->data_offset == 0) { /* first plt entry */ p = section_ptr_add(plt, 16); - put32(p , 0xe52de004); - put32(p + 4, 0xe59fe010); - put32(p + 8, 0xe08fe00e); - put32(p + 12, 0xe5bef008); + put32(p, 0xe52de004); /* push {lr} */ + put32(p+4, 0xe59fe010); /* ldr lr, [pc, #16] */ + put32(p+8, 0xe08fe00e); /* add lr, pc, lr */ + put32(p+12, 0xe5bef008); /* ldr pc, [lr, #8]! */ } - if (s1->sym_attrs[sym_index].plt_thumb_stub) { + symattr->plt_offset = plt->data_offset; + if (symattr->plt_thumb_stub) { p = section_ptr_add(plt, 20); - put32(p , 0x4778); // bx pc - put32(p+2, 0x46c0); // nop + put32(p, 0x4778); /* bx pc */ + put32(p+2, 0x46c0); /* nop */ p += 4; } else p = section_ptr_add(plt, 16); - put32(p , 0xe59fc004); // ldr ip, [pc, #4] // offset in GOT - put32(p+4, 0xe08fc00c); // add ip, pc, ip // absolute address or offset - put32(p+8, 0xe59cf000); // ldr pc, [ip] // load absolute address or load offset - put32(p+12, s1->got->data_offset); + put32(p, 0xe59fc004); /* ldr ip, [pc, #4] ; GOT entry offset */ + put32(p+4, 0xe08fc00c); /* add ip, pc, ip ; addr of GOT entry */ + put32(p+8, 0xe59cf000); /* ldr pc, [ip] ; jump to GOT entry */ + put32(p+12, s1->got->data_offset); /* GOT entry off once patched */ /* the symbol is modified so that it will be relocated to the PLT */ - if (s1->output_type == TCC_OUTPUT_EXE) + if (sym->st_shndx == SHN_UNDEF) offset = plt->data_offset - 16; } #elif defined(TCC_TARGET_C67) + if (s1->dynsym) { tcc_error("C67 got not implemented"); + } #else #error unsupported CPU #endif - index = put_elf_sym(s1->dynsym, offset, + if (s1->dynsym) { + /* XXX This might generate multiple syms for name. */ + index = put_elf_sym(s1->dynsym, offset, size, info, 0, sym->st_shndx, name); - /* put a got entry */ - put_elf_reloc(s1->dynsym, s1->got, - s1->got->data_offset, + /* Create the relocation (it's against the GOT for PLT + and GOT relocs). */ + put_elf_reloc(s1->dynsym, s1->got, + s1->got->data_offset, reloc_type, index); + } else { + /* Without .dynsym (i.e. static link or memory output) we + still need relocs against the generated got, so as to fill + the entries with the symbol values (determined later). */ + put_elf_reloc(symtab_section, s1->got, + s1->got->data_offset, + reloc_type, sym_index); } + /* And now create the GOT slot itself. */ ptr = section_ptr_add(s1->got, PTR_SIZE); *ptr = 0; + if (need_plt_entry) + return symattr->plt_offset; + else + return symattr->got_offset; } /* build GOT and PLT entries */ ST_FUNC void build_got_entries(TCCState *s1) { Section *s; - ElfW_Rel *rel, *rel_end; + ElfW_Rel *rel; ElfW(Sym) *sym; int i, type, reloc_type, sym_index; @@ -1155,10 +1174,7 @@ ST_FUNC void build_got_entries(TCCState *s1) /* no need to handle got relocations */ if (s->link != symtab_section) continue; - rel_end = (ElfW_Rel *)(s->data + s->data_offset); - for(rel = (ElfW_Rel *)s->data; - rel < rel_end; - rel++) { + for_each_elem(s, 0, rel, ElfW_Rel) { type = ELFW(R_TYPE)(rel->r_info); switch(type) { #if defined(TCC_TARGET_I386) @@ -1176,29 +1192,82 @@ ST_FUNC void build_got_entries(TCCState *s1) reloc_type = R_386_GLOB_DAT; else reloc_type = R_386_JMP_SLOT; - put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, + put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, sym_index); } break; #elif defined(TCC_TARGET_ARM) - case R_ARM_GOT_BREL: - case R_ARM_GOTOFF32: - case R_ARM_BASE_PREL: + case R_ARM_PC24: + case R_ARM_CALL: + case R_ARM_JUMP24: + case R_ARM_GOT32: + case R_ARM_GOTOFF: + case R_ARM_GOTPC: case R_ARM_PLT32: if (!s1->got) build_got(s1); - if (type == R_ARM_GOT_BREL || type == R_ARM_PLT32) { - sym_index = ELFW(R_SYM)(rel->r_info); - sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + sym_index = ELFW(R_SYM)(rel->r_info); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + if (type != R_ARM_GOTOFF && type != R_ARM_GOTPC + && sym->st_shndx == SHN_UNDEF) { + unsigned long ofs; /* look at the symbol got offset. If none, then add one */ - if (type == R_ARM_GOT_BREL) + if (type == R_ARM_GOT32) reloc_type = R_ARM_GLOB_DAT; else reloc_type = R_ARM_JUMP_SLOT; - put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, - sym_index); + ofs = put_got_entry(s1, reloc_type, sym->st_size, + sym->st_info, sym_index); +#ifdef DEBUG_RELOC + printf ("maybegot: %s, %d, %d --> ofs=0x%x\n", + (char *) symtab_section->link->data + sym->st_name, + type, sym->st_shndx, ofs); +#endif + if (type != R_ARM_GOT32) { + addr_t *ptr = (addr_t*)(s1->sections[s->sh_info]->data + + rel->r_offset); + /* x must be signed! */ + int x = *ptr & 0xffffff; + x = (x << 8) >> 8; + x <<= 2; + x += ofs; + x >>= 2; +#ifdef DEBUG_RELOC + printf ("insn=0x%x --> 0x%x (x==0x%x)\n", *ptr, + (*ptr & 0xff000000) | x, x); +#endif + *ptr = (*ptr & 0xff000000) | x; + } } break; + case R_ARM_THM_JUMP24: + sym_index = ELFW(R_SYM)(rel->r_info); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + /* We are relocating a jump from thumb code to arm code */ + if (sym->st_shndx != SHN_UNDEF && !(sym->st_value & 1)) { + int index; + uint8_t *p; + char *name, buf[1024]; + Section *text_section; + + name = (char *) symtab_section->link->data + sym->st_name; + text_section = s1->sections[sym->st_shndx]; + /* Modify reloc to target a thumb stub to switch to ARM */ + snprintf(buf, sizeof(buf), "%s_from_thumb", name); + index = put_elf_sym(symtab_section, + text_section->data_offset + 1, + sym->st_size, sym->st_info, 0, + sym->st_shndx, buf); + rel->r_info = ELFW(R_INFO)(index, type); + /* Create a thumb stub fonction to switch to ARM mode */ + put_elf_reloc(symtab_section, text_section, + text_section->data_offset + 4, R_ARM_JUMP24, + sym_index); + p = section_ptr_add(text_section, 8); + put32(p, 0x4778); /* bx pc */ + put32(p+2, 0x46c0); /* nop */ + put32(p+4, 0xeafffffe); /* b $sym */ + } #elif defined(TCC_TARGET_C67) case R_C60_GOT32: case R_C60_GOTOFF: @@ -1214,7 +1283,7 @@ ST_FUNC void build_got_entries(TCCState *s1) reloc_type = R_C60_GLOB_DAT; else reloc_type = R_C60_JMP_SLOT; - put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, + put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, sym_index); } break; @@ -1223,19 +1292,31 @@ ST_FUNC void build_got_entries(TCCState *s1) case R_X86_64_GOTTPOFF: case R_X86_64_GOTPCREL: case R_X86_64_PLT32: + sym_index = ELFW(R_SYM)(rel->r_info); + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + if (type == R_X86_64_PLT32 && + ELFW(ST_VISIBILITY)(sym->st_other) != STV_DEFAULT) + { + rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PC32); + break; + } + if (!s1->got) build_got(s1); if (type == R_X86_64_GOT32 || type == R_X86_64_GOTPCREL || type == R_X86_64_PLT32) { - sym_index = ELFW(R_SYM)(rel->r_info); - sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + unsigned long ofs; /* look at the symbol got offset. If none, then add one */ if (type == R_X86_64_GOT32 || type == R_X86_64_GOTPCREL) reloc_type = R_X86_64_GLOB_DAT; else reloc_type = R_X86_64_JUMP_SLOT; - put_got_entry(s1, reloc_type, sym->st_size, sym->st_info, - sym_index); + ofs = put_got_entry(s1, reloc_type, sym->st_size, + sym->st_info, sym_index); + if (type == R_X86_64_PLT32) + /* We store the place of the generated PLT slot + in our addend. */ + rel->r_addend += ofs; } break; #else @@ -1250,7 +1331,7 @@ ST_FUNC void build_got_entries(TCCState *s1) ST_FUNC Section *new_symtab(TCCState *s1, const char *symtab_name, int sh_type, int sh_flags, - const char *strtab_name, + const char *strtab_name, const char *hash_name, int hash_sh_flags) { Section *symtab, *strtab, *hash; @@ -1262,7 +1343,7 @@ ST_FUNC Section *new_symtab(TCCState *s1, put_elf_str(strtab, ""); symtab->link = strtab; put_elf_sym(symtab, 0, 0, 0, 0, 0, NULL); - + nb_buckets = 1; hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags); @@ -1292,7 +1373,7 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) long end_offset; char sym_start[1024]; char sym_end[1024]; - + snprintf(sym_start, sizeof(sym_start), "__%s_start", section_name + 1); snprintf(sym_end, sizeof(sym_end), "__%s_end", section_name + 1); @@ -1304,11 +1385,11 @@ static void add_init_array_defines(TCCState *s1, const char *section_name) end_offset = s->data_offset; } - add_elf_sym(symtab_section, + add_elf_sym(symtab_section, 0, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, s->sh_num, sym_start); - add_elf_sym(symtab_section, + add_elf_sym(symtab_section, end_offset, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, s->sh_num, sym_end); @@ -1356,16 +1437,26 @@ static inline int tcc_add_support(TCCState *s1, const char *filename) /* add tcc runtime libraries */ ST_FUNC void tcc_add_runtime(TCCState *s1) { - tcc_add_bcheck(s1); - /* add libc */ if (!s1->nostdlib) { tcc_add_library(s1, "c"); #ifdef CONFIG_USE_LIBGCC - tcc_add_file(s1, TCC_LIBGCC); -#elif !defined WITHOUT_LIBTCC + if (!s1->static_link) { + tcc_add_file(s1, TCC_LIBGCC); + tcc_add_support(s1, "libtcc1.a"); + } else + tcc_add_support(s1, "libtcc1.a"); +#else tcc_add_support(s1, "libtcc1.a"); #endif + } + + /* tcc_add_bcheck tries to relocate a call to __bound_init in _init so + libtcc1.a must be loaded before for __bound_init to be defined and + crtn.o must be loaded after to not finalize _init too early. */ + tcc_add_bcheck(s1); + + if (!s1->nostdlib) { /* add crt end if not memory output */ if (s1->output_type != TCC_OUTPUT_MEMORY) tcc_add_crt(s1, "crtn.o"); @@ -1381,15 +1472,15 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) int i; Section *s; - add_elf_sym(symtab_section, + add_elf_sym(symtab_section, text_section->data_offset, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, text_section->sh_num, "_etext"); - add_elf_sym(symtab_section, + add_elf_sym(symtab_section, data_section->data_offset, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, data_section->sh_num, "_edata"); - add_elf_sym(symtab_section, + add_elf_sym(symtab_section, bss_section->data_offset, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, bss_section->sh_num, "_end"); @@ -1397,7 +1488,7 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) add_init_array_defines(s1, ".preinit_array"); add_init_array_defines(s1, ".init_array"); add_init_array_defines(s1, ".fini_array"); - + /* add start and stop symbols for sections whose name can be expressed in C */ for(i = 1; i < s1->nb_sections; i++) { @@ -1418,7 +1509,7 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) p++; } snprintf(buf, sizeof(buf), "__start_%s", s->name); - add_elf_sym(symtab_section, + add_elf_sym(symtab_section, 0, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, s->sh_num, buf); @@ -1433,14 +1524,14 @@ ST_FUNC void tcc_add_linker_symbols(TCCState *s1) } static void tcc_output_binary(TCCState *s1, FILE *f, - const int *section_order) + const int *sec_order) { Section *s; int i, offset, size; offset = 0; for(i=1;inb_sections;i++) { - s = s1->sections[section_order[i]]; + s = s1->sections[sec_order[i]]; if (s->sh_type != SHT_NOBITS && (s->sh_flags & SHF_ALLOC)) { while (offset < s->sh_offset) { @@ -1455,36 +1546,34 @@ static void tcc_output_binary(TCCState *s1, FILE *f, } #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -#define HAVE_PHDR 1 -#define EXTRA_RELITEMS 14 +#define HAVE_PHDR 1 +#define EXTRA_RELITEMS 14 /* move the relocation value from .dynsym to .got */ void patch_dynsym_undef(TCCState *s1, Section *s) { uint32_t *gotd = (void *)s1->got->data; - ElfW(Sym) *sym, *sym_end; + ElfW(Sym) *sym; - gotd += 3; // dummy entries in .got + gotd += 3; /* dummy entries in .got */ /* relocate symbols in .dynsym */ - sym_end = (ElfW(Sym) *)(s->data + s->data_offset); - for (sym = (ElfW(Sym) *)s->data + 1; sym < sym_end; sym++) { - if (sym->st_shndx == SHN_UNDEF) { - *gotd++ = sym->st_value + 6; // XXX 6 is magic ? - sym->st_value = 0; - } + for_each_elem(s, 1, sym, ElfW(Sym)) { + if (sym->st_shndx == SHN_UNDEF) { + *gotd++ = sym->st_value + 6; /* XXX 6 is magic ? */ + sym->st_value = 0; + } } } #else -#define HAVE_PHDR 0 -#define EXTRA_RELITEMS 9 +#define HAVE_PHDR 1 +#define EXTRA_RELITEMS 9 /* zero plt offsets of weak symbols in .dynsym */ void patch_dynsym_undef(TCCState *s1, Section *s) { - ElfW(Sym) *sym, *sym_end; + ElfW(Sym) *sym; - sym_end = (ElfW(Sym) *)(s->data + s->data_offset); - for (sym = (ElfW(Sym) *)s->data + 1; sym < sym_end; sym++) + for_each_elem(s, 1, sym, ElfW(Sym)) if (sym->st_shndx == SHN_UNDEF && ELFW(ST_BIND)(sym->st_info) == STB_WEAK) sym->st_value = 0; } @@ -1492,281 +1581,786 @@ void patch_dynsym_undef(TCCState *s1, Section *s) ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel) { - int sym_index = ELFW(R_SYM) (rel->r_info); - ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index]; - unsigned long offset; + int sym_index = ELFW(R_SYM) (rel->r_info); + ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index]; + unsigned long offset; - if (sym_index >= s1->nb_sym_attrs) - return; - offset = s1->sym_attrs[sym_index].got_offset; - section_reserve(s1->got, offset + PTR_SIZE); + if (sym_index >= s1->nb_sym_attrs) + return; + offset = s1->sym_attrs[sym_index].got_offset; + section_reserve(s1->got, offset + PTR_SIZE); #ifdef TCC_TARGET_X86_64 - /* only works for x86-64 */ - put32(s1->got->data + offset + 4, sym->st_value >> 32); + /* only works for x86-64 */ + put32(s1->got->data + offset + 4, sym->st_value >> 32); #endif - put32(s1->got->data + offset, sym->st_value & 0xffffffff); + put32(s1->got->data + offset, sym->st_value & 0xffffffff); } +/* Perform relocation to GOT or PLT entries */ ST_FUNC void fill_got(TCCState *s1) { - Section *s; - ElfW_Rel *rel, *rel_end; - int i; + Section *s; + ElfW_Rel *rel; + int i; - for(i = 1; i < s1->nb_sections; i++) { - s = s1->sections[i]; - if (s->sh_type != SHT_RELX) - continue; - /* no need to handle got relocations */ - if (s->link != symtab_section) - continue; - rel_end = (ElfW_Rel *) (s->data + s->data_offset); - for(rel = (ElfW_Rel *) s->data; rel < rel_end; rel++) { - switch (ELFW(R_TYPE) (rel->r_info)) { - case R_X86_64_GOT32: - case R_X86_64_GOTPCREL: - case R_X86_64_PLT32: - fill_got_entry(s1, rel); - break; - } - } - } + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (s->sh_type != SHT_RELX) + continue; + /* no need to handle got relocations */ + if (s->link != symtab_section) + continue; + for_each_elem(s, 0, rel, ElfW_Rel) { + switch (ELFW(R_TYPE) (rel->r_info)) { + case R_X86_64_GOT32: + case R_X86_64_GOTPCREL: + case R_X86_64_PLT32: + fill_got_entry(s1, rel); + break; + } + } + } } +/* Bind symbols of executable: resolve undefined symbols from exported symbols + in shared libraries and export non local defined symbols to shared libraries + if -rdynamic switch was given on command line */ +static void bind_exe_dynsyms(TCCState *s1) +{ + const char *name; + int sym_index, index; + ElfW(Sym) *sym, *esym; + int type; -/* output an ELF file */ + /* Resolve undefined symbols from dynamic symbols. When there is a match: + - if STT_FUNC or STT_GNU_IFUNC symbol -> add it in PLT + - if STT_OBJECT symbol -> add it in .bss section with suitable reloc */ + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { + if (sym->st_shndx == SHN_UNDEF) { + name = (char *) symtab_section->link->data + sym->st_name; + sym_index = find_elf_sym(s1->dynsymtab_section, name); + if (sym_index) { + esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index]; + type = ELFW(ST_TYPE)(esym->st_info); + if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) { + /* Indirect functions shall have STT_FUNC type in executable + * dynsym section. Indeed, a dlsym call following a lazy + * resolution would pick the symbol value from the + * executable dynsym entry which would contain the address + * of the function wanted by the caller of dlsym instead of + * the address of the function that would return that + * address */ + put_got_entry(s1, R_JMP_SLOT, esym->st_size, + ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), + sym - (ElfW(Sym) *)symtab_section->data); + } else if (type == STT_OBJECT) { + unsigned long offset; + ElfW(Sym) *dynsym; + offset = bss_section->data_offset; + /* XXX: which alignment ? */ + offset = (offset + 16 - 1) & -16; + index = put_elf_sym(s1->dynsym, offset, esym->st_size, + esym->st_info, 0, bss_section->sh_num, + name); + /* Ensure R_COPY works for weak symbol aliases */ + if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) { + for_each_elem(s1->dynsymtab_section, 1, dynsym, ElfW(Sym)) { + if ((dynsym->st_value == esym->st_value) + && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) { + char *dynname = (char *) s1->dynsymtab_section->link->data + + dynsym->st_name; + put_elf_sym(s1->dynsym, offset, dynsym->st_size, + dynsym->st_info, 0, + bss_section->sh_num, dynname); + break; + } + } + } + put_elf_reloc(s1->dynsym, bss_section, + offset, R_COPY, index); + offset += esym->st_size; + bss_section->data_offset = offset; + } + } else { + /* STB_WEAK undefined symbols are accepted */ + /* XXX: _fp_hw seems to be part of the ABI, so we ignore it */ + if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK || + !strcmp(name, "_fp_hw")) { + } else { + tcc_error_noabort("undefined symbol '%s'", name); + } + } + } else if (s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + /* if -rdynamic option, then export all non local symbols */ + name = (char *) symtab_section->link->data + sym->st_name; + put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, sym->st_info, + 0, sym->st_shndx, name); + } + } +} + +/* Bind symbols of libraries: export non local symbols of executable that + resolve undefined symbols of shared libraries */ +static void bind_libs_dynsyms(TCCState *s1) +{ + const char *name; + int sym_index; + ElfW(Sym) *sym, *esym; + + /* now look at unresolved dynamic symbols and export + corresponding symbol */ + for_each_elem(s1->dynsymtab_section, 1, esym, ElfW(Sym)) { + if (esym->st_shndx == SHN_UNDEF) { + name = (char *) s1->dynsymtab_section->link->data + esym->st_name; + sym_index = find_elf_sym(symtab_section, name); + if (sym_index) { + /* XXX: avoid adding a symbol if already present because of + -rdynamic ? */ + sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; + put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, + sym->st_info, 0, sym->st_shndx, name); + } else { + /* weak symbols can stay undefined */ + if (ELFW(ST_BIND)(esym->st_info) != STB_WEAK) + tcc_warning("undefined dynamic symbol '%s'", name); + } + } + } +} + +/* Export all non local symbols (for shared libraries) */ +static void export_global_syms(TCCState *s1) +{ + int nb_syms, dynindex, index; + const char *name; + ElfW(Sym) *sym; + + nb_syms = symtab_section->data_offset / sizeof(ElfW(Sym)); + s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms); + for_each_elem(symtab_section, 1, sym, ElfW(Sym)) { + if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { + name = (char *) symtab_section->link->data + sym->st_name; + dynindex = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, + sym->st_info, 0, sym->st_shndx, name); + index = sym - (ElfW(Sym) *) symtab_section->data; + s1->symtab_to_dynsym[index] = dynindex; + } + } +} + +/* relocate the PLT: compute addresses and offsets in the PLT now that final + address for PLT and GOT are known (see fill_program_header) */ +ST_FUNC void relocate_plt(TCCState *s1) +{ + uint8_t *p, *p_end; + + if (!s1->plt) + return; + + p = s1->plt->data; + p_end = p + s1->plt->data_offset; + if (p < p_end) { +#if defined(TCC_TARGET_I386) + put32(p + 2, get32(p + 2) + s1->got->sh_addr); + put32(p + 8, get32(p + 8) + s1->got->sh_addr); + p += 16; + while (p < p_end) { + put32(p + 2, get32(p + 2) + s1->got->sh_addr); + p += 16; + } +#elif defined(TCC_TARGET_X86_64) + int x = s1->got->sh_addr - s1->plt->sh_addr - 6; + put32(p + 2, get32(p + 2) + x); + put32(p + 8, get32(p + 8) + x - 6); + p += 16; + while (p < p_end) { + put32(p + 2, get32(p + 2) + x + s1->plt->data - p); + p += 16; + } +#elif defined(TCC_TARGET_ARM) + int x; + x=s1->got->sh_addr - s1->plt->sh_addr - 12; + p += 16; + while (p < p_end) { + if (get32(p) == 0x46c04778) /* PLT Thumb stub present */ + p += 4; + put32(p + 12, x + get32(p + 12) + s1->plt->data - p); + p += 16; + } +#elif defined(TCC_TARGET_C67) + /* XXX: TODO */ +#else +#error unsupported CPU +#endif + } +} + +/* Allocate strings for section names and decide if an unallocated section + should be output. + + NOTE: the strsec section comes last, so its size is also correct ! */ +static void alloc_sec_names(TCCState *s1, int file_type, Section *strsec) +{ + int i; + Section *s; + + /* Allocate strings for section names */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + s->sh_name = put_elf_str(strsec, s->name); + /* when generating a DLL, we include relocations but we may + patch them */ + if (file_type == TCC_OUTPUT_DLL && + s->sh_type == SHT_RELX && + !(s->sh_flags & SHF_ALLOC)) { + /* gr: avoid bogus relocs for empty (debug) sections */ + if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) + prepare_dynamic_rel(s1, s); + else if (s1->do_debug) + s->sh_size = s->data_offset; + } else if (s1->do_debug || + file_type == TCC_OUTPUT_OBJ || + (s->sh_flags & SHF_ALLOC) || + i == (s1->nb_sections - 1)) { + /* we output all sections if debug or object file */ + s->sh_size = s->data_offset; + } + } +} + +/* Info to be copied in dynamic section */ +struct dyn_inf { + Section *dynamic; + Section *dynstr; + unsigned long dyn_rel_off; + addr_t rel_addr; + addr_t rel_size; +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + addr_t bss_addr; + addr_t bss_size; +#endif +}; + +/* Assign sections to segments and decide how are sections laid out when loaded + in memory. This function also fills corresponding program headers. */ +static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum, + Section *interp, struct dyn_inf *dyninf, + int *sec_order) +{ + int i, j, k, file_type, sh_order_index, file_offset; + long long tmp; + addr_t addr; + ElfW(Phdr) *ph; + Section *s; + + file_type = s1->output_type; + sh_order_index = 1; + if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) + file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr)); + else + file_offset = 0; + + if (phnum > 0) { + if (s1->has_text_addr) { + int a_offset, p_offset; + addr = s1->text_addr; + /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset % + ELF_PAGE_SIZE */ + a_offset = (int) (addr & (s1->section_align - 1)); + p_offset = file_offset & (s1->section_align - 1); + if (a_offset < p_offset) + a_offset += s1->section_align; + file_offset += (a_offset - p_offset); + } else { + if (file_type == TCC_OUTPUT_DLL) + addr = 0; + else + addr = ELF_START_ADDR; + /* compute address after headers */ + addr += (file_offset & (s1->section_align - 1)); + } + + ph = &phdr[0]; + /* Leave one program headers for the program interpreter and one for + the program header table itself if needed. These are done later as + they require section layout to be done first. */ + if (interp) + ph += 1 + HAVE_PHDR; + + /* dynamic relocation table information, for .dynamic section */ + dyninf->rel_addr = dyninf->rel_size = 0; +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + dyninf->bss_addr = dyninf->bss_size = 0; +#endif + + for(j = 0; j < 2; j++) { + ph->p_type = PT_LOAD; + if (j == 0) + ph->p_flags = PF_R | PF_X; + else + ph->p_flags = PF_R | PF_W; + ph->p_align = s1->section_align; + + /* Decide the layout of sections loaded in memory. This must + be done before program headers are filled since they contain + info about the layout. We do the following ordering: interp, + symbol tables, relocations, progbits, nobits */ + /* XXX: do faster and simpler sorting */ + for(k = 0; k < 5; k++) { + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + /* compute if section should be included */ + if (j == 0) { + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != + SHF_ALLOC) + continue; + } else { + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != + (SHF_ALLOC | SHF_WRITE)) + continue; + } + if (s == interp) { + if (k != 0) + continue; + } else if (s->sh_type == SHT_DYNSYM || + s->sh_type == SHT_STRTAB || + s->sh_type == SHT_HASH) { + if (k != 1) + continue; + } else if (s->sh_type == SHT_RELX) { + if (k != 2) + continue; + } else if (s->sh_type == SHT_NOBITS) { + if (k != 4) + continue; + } else { + if (k != 3) + continue; + } + sec_order[sh_order_index++] = i; + + /* section matches: we align it and add its size */ + tmp = addr; + addr = (addr + s->sh_addralign - 1) & + ~(s->sh_addralign - 1); + file_offset += (int) ( addr - tmp ); + s->sh_offset = file_offset; + s->sh_addr = addr; + + /* update program header infos */ + if (ph->p_offset == 0) { + ph->p_offset = file_offset; + ph->p_vaddr = addr; + ph->p_paddr = ph->p_vaddr; + } + /* update dynamic relocation infos */ + if (s->sh_type == SHT_RELX) { +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + if (!strcmp(strsec->data + s->sh_name, ".rel.got")) { + dyninf->rel_addr = addr; + dyninf->rel_size += s->sh_size; /* XXX only first rel. */ + } + if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) { + dyninf->bss_addr = addr; + dyninf->bss_size = s->sh_size; /* XXX only first rel. */ + } +#else + if (dyninf->rel_size == 0) + dyninf->rel_addr = addr; + dyninf->rel_size += s->sh_size; +#endif + } + addr += s->sh_size; + if (s->sh_type != SHT_NOBITS) + file_offset += s->sh_size; + } + } + if (j == 0) { + /* Make the first PT_LOAD segment include the program + headers itself (and the ELF header as well), it'll + come out with same memory use but will make various + tools like binutils strip work better. */ + ph->p_offset &= ~(ph->p_align - 1); + ph->p_vaddr &= ~(ph->p_align - 1); + ph->p_paddr &= ~(ph->p_align - 1); + } + ph->p_filesz = file_offset - ph->p_offset; + ph->p_memsz = addr - ph->p_vaddr; + ph++; + if (j == 0) { + if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) { + /* if in the middle of a page, we duplicate the page in + memory so that one copy is RX and the other is RW */ + if ((addr & (s1->section_align - 1)) != 0) + addr += s1->section_align; + } else { + addr = (addr + s1->section_align - 1) & ~(s1->section_align - 1); + file_offset = (file_offset + s1->section_align - 1) & + ~(s1->section_align - 1); + } + } + } + } + + /* all other sections come after */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (phnum > 0 && (s->sh_flags & SHF_ALLOC)) + continue; + sec_order[sh_order_index++] = i; + + file_offset = (file_offset + s->sh_addralign - 1) & + ~(s->sh_addralign - 1); + s->sh_offset = file_offset; + if (s->sh_type != SHT_NOBITS) + file_offset += s->sh_size; + } + + return file_offset; +} + +static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp, + Section *dynamic) +{ + ElfW(Phdr) *ph; + + /* if interpreter, then add corresponding program header */ + if (interp) { + ph = &phdr[0]; + + if (HAVE_PHDR) + { + int len = phnum * sizeof(ElfW(Phdr)); + + ph->p_type = PT_PHDR; + ph->p_offset = sizeof(ElfW(Ehdr)); + ph->p_vaddr = interp->sh_addr - len; + ph->p_paddr = ph->p_vaddr; + ph->p_filesz = ph->p_memsz = len; + ph->p_flags = PF_R | PF_X; + ph->p_align = 4; /* interp->sh_addralign; */ + ph++; + } + + ph->p_type = PT_INTERP; + ph->p_offset = interp->sh_offset; + ph->p_vaddr = interp->sh_addr; + ph->p_paddr = ph->p_vaddr; + ph->p_filesz = interp->sh_size; + ph->p_memsz = interp->sh_size; + ph->p_flags = PF_R; + ph->p_align = interp->sh_addralign; + } + + /* if dynamic section, then add corresponding program header */ + if (dynamic) { + ph = &phdr[phnum - 1]; + + ph->p_type = PT_DYNAMIC; + ph->p_offset = dynamic->sh_offset; + ph->p_vaddr = dynamic->sh_addr; + ph->p_paddr = ph->p_vaddr; + ph->p_filesz = dynamic->sh_size; + ph->p_memsz = dynamic->sh_size; + ph->p_flags = PF_R | PF_W; + ph->p_align = dynamic->sh_addralign; + } +} + +/* Fill the dynamic section with tags describing the address and size of + sections */ +static void fill_dynamic(TCCState *s1, struct dyn_inf *dyninf) +{ + Section *dynamic; + + dynamic = dyninf->dynamic; + + /* put dynamic section entries */ + dynamic->data_offset = dyninf->dyn_rel_off; + put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr); + put_dt(dynamic, DT_STRTAB, dyninf->dynstr->sh_addr); + put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr); + put_dt(dynamic, DT_STRSZ, dyninf->dynstr->data_offset); + put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym))); +#ifdef TCC_TARGET_X86_64 + put_dt(dynamic, DT_RELA, dyninf->rel_addr); + put_dt(dynamic, DT_RELASZ, dyninf->rel_size); + put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel)); +#else +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr); + put_dt(dynamic, DT_PLTRELSZ, dyninf->rel_size); + put_dt(dynamic, DT_JMPREL, dyninf->rel_addr); + put_dt(dynamic, DT_PLTREL, DT_REL); + put_dt(dynamic, DT_REL, dyninf->bss_addr); + put_dt(dynamic, DT_RELSZ, dyninf->bss_size); +#else + put_dt(dynamic, DT_REL, dyninf->rel_addr); + put_dt(dynamic, DT_RELSZ, dyninf->rel_size); + put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel)); +#endif +#endif + if (s1->do_debug) + put_dt(dynamic, DT_DEBUG, 0); + put_dt(dynamic, DT_NULL, 0); +} + +/* Relocate remaining sections and symbols (that is those not related to + dynamic linking) */ +static int final_sections_reloc(TCCState *s1) +{ + int i; + Section *s; + + relocate_syms(s1, 0); + + if (s1->nb_errors != 0) + return -1; + + /* relocate sections */ + /* XXX: ignore sections with allocated relocations ? */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if (s->reloc && s != s1->got) + relocate_section(s1, s); + } + + /* relocate relocation entries if the relocation tables are + allocated in the executable */ + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[i]; + if ((s->sh_flags & SHF_ALLOC) && + s->sh_type == SHT_RELX) { + relocate_rel(s1, s); + } + } + return 0; +} + +/* Create an ELF file on disk. + This function handle ELF specific layout requirements */ +static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr, + int file_offset, int *sec_order) +{ + int i, shnum, offset, size, file_type; + Section *s; + ElfW(Ehdr) ehdr; + ElfW(Shdr) shdr, *sh; + + file_type = s1->output_type; + shnum = s1->nb_sections; + + memset(&ehdr, 0, sizeof(ehdr)); + + if (phnum > 0) { + ehdr.e_phentsize = sizeof(ElfW(Phdr)); + ehdr.e_phnum = phnum; + ehdr.e_phoff = sizeof(ElfW(Ehdr)); + } + + /* align to 4 */ + file_offset = (file_offset + 3) & -4; + + /* fill header */ + ehdr.e_ident[0] = ELFMAG0; + ehdr.e_ident[1] = ELFMAG1; + ehdr.e_ident[2] = ELFMAG2; + ehdr.e_ident[3] = ELFMAG3; + ehdr.e_ident[4] = ELFCLASSW; + ehdr.e_ident[5] = ELFDATA2LSB; + ehdr.e_ident[6] = EV_CURRENT; +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; +#endif +#ifdef TCC_TARGET_ARM +#ifdef TCC_ARM_EABI + ehdr.e_ident[EI_OSABI] = 0; + ehdr.e_flags = EF_ARM_EABI_VER4; + if (file_type == TCC_OUTPUT_EXE || file_type == TCC_OUTPUT_DLL) + ehdr.e_flags |= EF_ARM_HASENTRY; + if (s1->float_abi == ARM_HARD_FLOAT) + ehdr.e_flags |= EF_ARM_VFP_FLOAT; + else + ehdr.e_flags |= EF_ARM_SOFT_FLOAT; +#else + ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM; +#endif +#endif + switch(file_type) { + default: + case TCC_OUTPUT_EXE: + ehdr.e_type = ET_EXEC; + ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1); + break; + case TCC_OUTPUT_DLL: + ehdr.e_type = ET_DYN; + ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */ + break; + case TCC_OUTPUT_OBJ: + ehdr.e_type = ET_REL; + break; + } + ehdr.e_machine = EM_TCC_TARGET; + ehdr.e_version = EV_CURRENT; + ehdr.e_shoff = file_offset; + ehdr.e_ehsize = sizeof(ElfW(Ehdr)); + ehdr.e_shentsize = sizeof(ElfW(Shdr)); + ehdr.e_shnum = shnum; + ehdr.e_shstrndx = shnum - 1; + + fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f); + fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f); + offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr)); + + sort_syms(s1, symtab_section); + for(i = 1; i < s1->nb_sections; i++) { + s = s1->sections[sec_order[i]]; + if (s->sh_type != SHT_NOBITS) { + if (s->sh_type == SHT_DYNSYM) + patch_dynsym_undef(s1, s); + while (offset < s->sh_offset) { + fputc(0, f); + offset++; + } + size = s->sh_size; + fwrite(s->data, 1, size, f); + offset += size; + } + } + + /* output section headers */ + while (offset < ehdr.e_shoff) { + fputc(0, f); + offset++; + } + + for(i = 0; i < s1->nb_sections; i++) { + sh = &shdr; + memset(sh, 0, sizeof(ElfW(Shdr))); + s = s1->sections[i]; + if (s) { + sh->sh_name = s->sh_name; + sh->sh_type = s->sh_type; + sh->sh_flags = s->sh_flags; + sh->sh_entsize = s->sh_entsize; + sh->sh_info = s->sh_info; + if (s->link) + sh->sh_link = s->link->sh_num; + sh->sh_addralign = s->sh_addralign; + sh->sh_addr = s->sh_addr; + sh->sh_offset = s->sh_offset; + sh->sh_size = s->sh_size; + } + fwrite(sh, 1, sizeof(ElfW(Shdr)), f); + } +} + +/* Write an elf, coff or "binary" file */ +static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum, + ElfW(Phdr) *phdr, int file_offset, int *sec_order) +{ + int fd, mode, file_type; + FILE *f; + + file_type = s1->output_type; + if (file_type == TCC_OUTPUT_OBJ) + mode = 0666; + else + mode = 0777; + unlink(filename); + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); + if (fd < 0) { + tcc_error_noabort("could not write '%s'", filename); + return -1; + } + f = fdopen(fd, "wb"); + if (s1->verbose) + printf("<- %s\n", filename); + +#ifdef TCC_TARGET_COFF + if (s1->output_format == TCC_OUTPUT_FORMAT_COFF) + tcc_output_coff(s1, f); + else +#endif + if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) + tcc_output_elf(s1, f, phnum, phdr, file_offset, sec_order); + else + tcc_output_binary(s1, f, sec_order); + fclose(f); + + return 0; +} + +/* Output an elf, coff or binary file */ /* XXX: suppress unneeded sections */ static int elf_output_file(TCCState *s1, const char *filename) { - ElfW(Ehdr) ehdr; - FILE *f; - int fd, mode, ret; - int *section_order; - int shnum, i, phnum, file_offset, offset, size, j, sh_order_index, k; - long long tmp; - addr_t addr; - Section *strsec, *s; - ElfW(Shdr) shdr, *sh; - ElfW(Phdr) *phdr, *ph; - Section *interp, *dynamic, *dynstr; - unsigned long saved_dynamic_data_offset; + int i, ret, phnum, shnum, file_type, file_offset, *sec_order; + struct dyn_inf dyninf; + ElfW(Phdr) *phdr; ElfW(Sym) *sym; - int type, file_type; - addr_t rel_addr, rel_size; -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - addr_t bss_addr, bss_size; -#endif + Section *strsec, *interp, *dynamic, *dynstr; file_type = s1->output_type; s1->nb_errors = 0; + /* if linking, also link in runtime libraries (libc, libgcc, etc.) */ if (file_type != TCC_OUTPUT_OBJ) { tcc_add_runtime(s1); } phdr = NULL; - section_order = NULL; - interp = NULL; - dynamic = NULL; - dynstr = NULL; /* avoid warning */ - saved_dynamic_data_offset = 0; /* avoid warning */ - + sec_order = NULL; + interp = dynamic = dynstr = NULL; /* avoid warning */ + dyninf.dyn_rel_off = 0; /* avoid warning */ + if (file_type != TCC_OUTPUT_OBJ) { relocate_common_syms(); tcc_add_linker_symbols(s1); if (!s1->static_link) { - const char *name; - int sym_index, index; - ElfW(Sym) *esym, *sym_end; - if (file_type == TCC_OUTPUT_EXE) { char *ptr; - /* allow override the dynamic loader */ - const char *elfint = getenv("LD_SO"); - if (elfint == NULL) - elfint = CONFIG_TCC_ELFINTERP; + /* allow override the dynamic loader */ + const char *elfint = getenv("LD_SO"); + if (elfint == NULL) + elfint = DEFAULT_ELFINTERP(s1); /* add interpreter section only if executable */ interp = new_section(s1, ".interp", SHT_PROGBITS, SHF_ALLOC); interp->sh_addralign = 1; - ptr = section_ptr_add(interp, 1+strlen(elfint)); + ptr = section_ptr_add(interp, 1 + strlen(elfint)); strcpy(ptr, elfint); } /* add dynamic symbol table */ s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC, - ".dynstr", + ".dynstr", ".hash", SHF_ALLOC); dynstr = s1->dynsym->link; - + /* add dynamic section */ - dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC, + dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE); dynamic->link = dynstr; dynamic->sh_entsize = sizeof(ElfW(Dyn)); - - /* add PLT */ - s1->plt = new_section(s1, ".plt", SHT_PROGBITS, - SHF_ALLOC | SHF_EXECINSTR); - s1->plt->sh_entsize = 4; build_got(s1); - /* scan for undefined symbols and see if they are in the - dynamic symbols. If a symbol STT_FUNC or STT_GNU_IFUNC - is found, then we add it in the PLT. If a symbol - STT_OBJECT is found, we add it in the .bss section with - a suitable relocation */ - sym_end = (ElfW(Sym) *)(symtab_section->data + - symtab_section->data_offset); if (file_type == TCC_OUTPUT_EXE) { - for(sym = (ElfW(Sym) *)symtab_section->data + 1; - sym < sym_end; - sym++) { - if (sym->st_shndx == SHN_UNDEF) { - name = symtab_section->link->data + sym->st_name; - sym_index = find_elf_sym(s1->dynsymtab_section, name); - if (sym_index) { - esym = &((ElfW(Sym) *)s1->dynsymtab_section->data)[sym_index]; - type = ELFW(ST_TYPE)(esym->st_info); - if ((type == STT_FUNC) || (type == STT_GNU_IFUNC)) { - /* Indirect functions shall have STT_FUNC type - * in executable dynsym section. Indeed, a dlsym - * call following a lazy resolution would pick - * the symbol value from the executable dynsym - * entry which would contain the address of the - * function wanted by the caller of dlsym - * instead of the address of the function that - * would return that address */ - put_got_entry(s1, R_JMP_SLOT, esym->st_size, - ELFW(ST_INFO)(STB_GLOBAL,STT_FUNC), - sym - (ElfW(Sym) *)symtab_section->data); - } else if (type == STT_OBJECT) { - unsigned long offset; - ElfW(Sym) *dynsym, *dynsym_end; - offset = bss_section->data_offset; - /* XXX: which alignment ? */ - offset = (offset + 16 - 1) & -16; - index = put_elf_sym(s1->dynsym, offset, esym->st_size, - esym->st_info, 0, - bss_section->sh_num, name); - // Ensure R_COPY works for weak symbol aliases - if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) { - dynsym_end = (ElfW(Sym) *) - (s1->dynsymtab_section->data + - s1->dynsymtab_section->data_offset); - for(dynsym = (ElfW(Sym) *)s1->dynsymtab_section->data + 1; - dynsym < dynsym_end; dynsym++) { - if ((dynsym->st_value == esym->st_value) - && (ELFW(ST_BIND)(dynsym->st_info) == STB_GLOBAL)) { - char *dynname; - dynname = s1->dynsymtab_section->link->data - + dynsym->st_name; - put_elf_sym(s1->dynsym, offset, - dynsym->st_size, - dynsym->st_info, 0, - bss_section->sh_num, - dynname); - break; - } - } - } - put_elf_reloc(s1->dynsym, bss_section, - offset, R_COPY, index); - offset += esym->st_size; - bss_section->data_offset = offset; - } - } else { - /* STB_WEAK undefined symbols are accepted */ - /* XXX: _fp_hw seems to be part of the ABI, so we ignore - it */ - if (ELFW(ST_BIND)(sym->st_info) == STB_WEAK || - !strcmp(name, "_fp_hw")) { - } else { - tcc_error_noabort("undefined symbol '%s'", name); - } - } - } else if (s1->rdynamic && - ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { - /* if -rdynamic option, then export all non - local symbols */ - name = symtab_section->link->data + sym->st_name; - put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, - sym->st_info, 0, - sym->st_shndx, name); - } - } - - if (s1->nb_errors) - goto fail; + bind_exe_dynsyms(s1); - /* now look at unresolved dynamic symbols and export - corresponding symbol */ - sym_end = (ElfW(Sym) *)(s1->dynsymtab_section->data + - s1->dynsymtab_section->data_offset); - for(esym = (ElfW(Sym) *)s1->dynsymtab_section->data + 1; - esym < sym_end; - esym++) { - if (esym->st_shndx == SHN_UNDEF) { - name = s1->dynsymtab_section->link->data + esym->st_name; - sym_index = find_elf_sym(symtab_section, name); - if (sym_index) { - /* XXX: avoid adding a symbol if already - present because of -rdynamic ? */ - sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; - put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, - sym->st_info, 0, - sym->st_shndx, name); - } else { - if (ELFW(ST_BIND)(esym->st_info) == STB_WEAK) { - /* weak symbols can stay undefined */ - } else { - tcc_warning("undefined dynamic symbol '%s'", name); - } - } - } + if (s1->nb_errors) { + ret = -1; + goto the_end; } - } else { - int nb_syms; - /* shared library case : we simply export all the global symbols */ - nb_syms = symtab_section->data_offset / sizeof(ElfW(Sym)); - s1->symtab_to_dynsym = tcc_mallocz(sizeof(int) * nb_syms); - for(sym = (ElfW(Sym) *)symtab_section->data + 1; - sym < sym_end; - sym++) { - if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { -#if defined(TCC_OUTPUT_DLL_WITH_PLT) - if ((ELFW(ST_TYPE)(sym->st_info) == STT_FUNC || - ELFW(ST_TYPE)(sym->st_info) == STT_GNU_IFUNC) - && sym->st_shndx == SHN_UNDEF) { - int visibility = ELFW(ST_BIND)(sym->st_info); - put_got_entry(s1, R_JMP_SLOT, sym->st_size, - ELFW(ST_INFO)(visibility,STT_FUNC), - sym - (ElfW(Sym) *)symtab_section->data); - } - else if (ELFW(ST_TYPE)(sym->st_info) == STT_OBJECT) { - put_got_entry(s1, R_X86_64_GLOB_DAT, sym->st_size, - sym->st_info, - sym - (ElfW(Sym) *)symtab_section->data); - } - else -#endif - { - name = symtab_section->link->data + sym->st_name; - index = put_elf_sym(s1->dynsym, sym->st_value, sym->st_size, - sym->st_info, 0, - sym->st_shndx, name); - s1->symtab_to_dynsym[sym - - (ElfW(Sym) *)symtab_section->data] = - index; - } - } - } - } + + bind_libs_dynsyms(s1); + } else /* shared library case: simply export all global symbols */ + export_global_syms(s1); build_got_entries(s1); - + /* add a list of needed dlls */ for(i = 0; i < s1->nb_loaded_dlls; i++) { DLLReference *dllref = s1->loaded_dlls[i]; @@ -1789,7 +2383,7 @@ static int elf_output_file(TCCState *s1, const char *filename) put_dt(dynamic, DT_SYMBOLIC, 0); /* add necessary space for other entries */ - saved_dynamic_data_offset = dynamic->data_offset; + dyninf.dyn_rel_off = dynamic->data_offset; dynamic->data_offset += sizeof(ElfW(Dyn)) * EXTRA_RELITEMS; } else { /* still need to build got entries in case of static link */ @@ -1797,20 +2391,17 @@ static int elf_output_file(TCCState *s1, const char *filename) } } - memset(&ehdr, 0, sizeof(ehdr)); - /* we add a section for symbols */ strsec = new_section(s1, ".shstrtab", SHT_STRTAB, 0); put_elf_str(strsec, ""); - + /* compute number of sections */ shnum = s1->nb_sections; /* this array is used to reorder sections in the output file */ - section_order = tcc_malloc(sizeof(int) * shnum); - section_order[0] = 0; - sh_order_index = 1; - + sec_order = tcc_malloc(sizeof(int) * shnum); + sec_order[0] = 0; + /* compute number of program headers */ switch(file_type) { default: @@ -1828,280 +2419,38 @@ static int elf_output_file(TCCState *s1, const char *filename) break; } - /* allocate strings for section names and decide if an unallocated - section should be output */ - /* NOTE: the strsec section comes last, so its size is also - correct ! */ - for(i = 1; i < s1->nb_sections; i++) { - s = s1->sections[i]; - s->sh_name = put_elf_str(strsec, s->name); -#if 0 /* gr */ - printf("section: f=%08x t=%08x i=%08x %s %s\n", - s->sh_flags, - s->sh_type, - s->sh_info, - s->name, - s->reloc ? s->reloc->name : "n" - ); -#endif - /* when generating a DLL, we include relocations but we may - patch them */ - if (file_type == TCC_OUTPUT_DLL && - s->sh_type == SHT_RELX && - !(s->sh_flags & SHF_ALLOC)) { - /* //gr: avoid bogus relocs for empty (debug) sections */ - if (s1->sections[s->sh_info]->sh_flags & SHF_ALLOC) - prepare_dynamic_rel(s1, s); - else if (s1->do_debug) - s->sh_size = s->data_offset; - } else if (s1->do_debug || - file_type == TCC_OUTPUT_OBJ || - (s->sh_flags & SHF_ALLOC) || - i == (s1->nb_sections - 1)) { - /* we output all sections if debug or object file */ - s->sh_size = s->data_offset; - } - } + /* Allocate strings for section names */ + alloc_sec_names(s1, file_type, strsec); /* allocate program segment headers */ phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr))); - - if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) { - file_offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr)); - } else { - file_offset = 0; - } + + /* compute section to program header mapping */ + file_offset = layout_sections(s1, phdr, phnum, interp, &dyninf, sec_order); + + /* Fill remaining program header and finalize relocation related to dynamic + linking. */ if (phnum > 0) { - /* compute section to program header mapping */ - if (s1->has_text_addr) { - int a_offset, p_offset; - addr = s1->text_addr; - /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset % - ELF_PAGE_SIZE */ - a_offset = (int) (addr & (s1->section_align - 1)); - p_offset = file_offset & (s1->section_align - 1); - if (a_offset < p_offset) - a_offset += s1->section_align; - file_offset += (a_offset - p_offset); - } else { - if (file_type == TCC_OUTPUT_DLL) - addr = 0; - else - addr = ELF_START_ADDR; - /* compute address after headers */ - addr += (file_offset & (s1->section_align - 1)); - } - - /* dynamic relocation table information, for .dynamic section */ - rel_size = 0; - rel_addr = 0; - -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - bss_addr = bss_size = 0; -#endif - /* leave one program header for the program interpreter */ - ph = &phdr[0]; - if (interp) - ph += 1 + HAVE_PHDR; - - for(j = 0; j < 2; j++) { - ph->p_type = PT_LOAD; - if (j == 0) - ph->p_flags = PF_R | PF_X; - else - ph->p_flags = PF_R | PF_W; - ph->p_align = s1->section_align; - - /* we do the following ordering: interp, symbol tables, - relocations, progbits, nobits */ - /* XXX: do faster and simpler sorting */ - for(k = 0; k < 5; k++) { - for(i = 1; i < s1->nb_sections; i++) { - s = s1->sections[i]; - /* compute if section should be included */ - if (j == 0) { - if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != - SHF_ALLOC) - continue; - } else { - if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != - (SHF_ALLOC | SHF_WRITE)) - continue; - } - if (s == interp) { - if (k != 0) - continue; - } else if (s->sh_type == SHT_DYNSYM || - s->sh_type == SHT_STRTAB || - s->sh_type == SHT_HASH) { - if (k != 1) - continue; - } else if (s->sh_type == SHT_RELX) { - if (k != 2) - continue; - } else if (s->sh_type == SHT_NOBITS) { - if (k != 4) - continue; - } else { - if (k != 3) - continue; - } - section_order[sh_order_index++] = i; - - /* section matches: we align it and add its size */ - tmp = addr; - addr = (addr + s->sh_addralign - 1) & - ~(s->sh_addralign - 1); - file_offset += (int) ( addr - tmp ); - s->sh_offset = file_offset; - s->sh_addr = addr; - - /* update program header infos */ - if (ph->p_offset == 0) { - ph->p_offset = file_offset; - ph->p_vaddr = addr; - ph->p_paddr = ph->p_vaddr; - } - /* update dynamic relocation infos */ - if (s->sh_type == SHT_RELX) { -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - if (!strcmp(strsec->data + s->sh_name, ".rel.got")) { // rel_size == 0) { - rel_addr = addr; - rel_size += s->sh_size; // XXX only first rel. - } - if (!strcmp(strsec->data + s->sh_name, ".rel.bss")) { // rel_size == 0) { - bss_addr = addr; - bss_size = s->sh_size; // XXX only first rel. - } -#else - if (rel_size == 0) - rel_addr = addr; - rel_size += s->sh_size; -#endif - } - addr += s->sh_size; - if (s->sh_type != SHT_NOBITS) - file_offset += s->sh_size; - } - } - ph->p_filesz = file_offset - ph->p_offset; - ph->p_memsz = addr - ph->p_vaddr; - ph++; - if (j == 0) { - if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) { - /* if in the middle of a page, we duplicate the page in - memory so that one copy is RX and the other is RW */ - if ((addr & (s1->section_align - 1)) != 0) - addr += s1->section_align; - } else { - addr = (addr + s1->section_align - 1) & ~(s1->section_align - 1); - file_offset = (file_offset + s1->section_align - 1) & - ~(s1->section_align - 1); - } - } - } - - /* if interpreter, then add corresponing program header */ - if (interp) { - ph = &phdr[0]; - -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - { - int len = phnum * sizeof(ElfW(Phdr)); - - ph->p_type = PT_PHDR; - ph->p_offset = sizeof(ElfW(Ehdr)); - ph->p_vaddr = interp->sh_addr - len; - ph->p_paddr = ph->p_vaddr; - ph->p_filesz = ph->p_memsz = len; - ph->p_flags = PF_R | PF_X; - ph->p_align = 4; // interp->sh_addralign; - ph++; - } -#endif - - ph->p_type = PT_INTERP; - ph->p_offset = interp->sh_offset; - ph->p_vaddr = interp->sh_addr; - ph->p_paddr = ph->p_vaddr; - ph->p_filesz = interp->sh_size; - ph->p_memsz = interp->sh_size; - ph->p_flags = PF_R; - ph->p_align = interp->sh_addralign; - } - - /* if dynamic section, then add corresponing program header */ + fill_unloadable_phdr(phdr, phnum, interp, dynamic); if (dynamic) { - ElfW(Sym) *sym_end; + dyninf.dynamic = dynamic; + dyninf.dynstr = dynstr; - ph = &phdr[phnum - 1]; - - ph->p_type = PT_DYNAMIC; - ph->p_offset = dynamic->sh_offset; - ph->p_vaddr = dynamic->sh_addr; - ph->p_paddr = ph->p_vaddr; - ph->p_filesz = dynamic->sh_size; - ph->p_memsz = dynamic->sh_size; - ph->p_flags = PF_R | PF_W; - ph->p_align = dynamic->sh_addralign; + fill_dynamic(s1, &dyninf); - /* put GOT dynamic section address */ + /* put in GOT the dynamic section address and relocate PLT */ put32(s1->got->data, dynamic->sh_addr); - - /* relocate the PLT */ if (file_type == TCC_OUTPUT_EXE #if defined(TCC_OUTPUT_DLL_WITH_PLT) || file_type == TCC_OUTPUT_DLL #endif - ) { - uint8_t *p, *p_end; + ) + relocate_plt(s1); - p = s1->plt->data; - p_end = p + s1->plt->data_offset; - if (p < p_end) { -#if defined(TCC_TARGET_I386) - put32(p + 2, get32(p + 2) + s1->got->sh_addr); - put32(p + 8, get32(p + 8) + s1->got->sh_addr); - p += 16; - while (p < p_end) { - put32(p + 2, get32(p + 2) + s1->got->sh_addr); - p += 16; - } -#elif defined(TCC_TARGET_X86_64) - int x = s1->got->sh_addr - s1->plt->sh_addr - 6; - put32(p + 2, get32(p + 2) + x); - put32(p + 8, get32(p + 8) + x - 6); - p += 16; - while (p < p_end) { - put32(p + 2, get32(p + 2) + x + s1->plt->data - p); - p += 16; - } -#elif defined(TCC_TARGET_ARM) - int x; - x=s1->got->sh_addr - s1->plt->sh_addr - 12; - p += 16; - while (p < p_end) { - if (get32(p) == 0x46c04778) /* PLT Thumb stub present */ - p += 4; - put32(p + 12, x + get32(p + 12) + s1->plt->data - p); - p += 16; - } -#elif defined(TCC_TARGET_C67) - /* XXX: TODO */ -#else -#error unsupported CPU -#endif - } - } - - /* relocate symbols in .dynsym */ - sym_end = (ElfW(Sym) *)(s1->dynsym->data + s1->dynsym->data_offset); - for(sym = (ElfW(Sym) *)s1->dynsym->data + 1; - sym < sym_end; - sym++) { + /* relocate symbols in .dynsym now that final addresses are known */ + for_each_elem(s1->dynsym, 1, sym, ElfW(Sym)) { + /* relocate to PLT if symbol corresponds to a PLT entry */ if (sym->st_shndx == SHN_UNDEF) { - /* relocate to the PLT if the symbol corresponds - to a PLT entry */ if (sym->st_value) sym->st_value += s1->plt->sh_addr; } else if (sym->st_shndx < SHN_LORESERVE) { @@ -2109,214 +2458,29 @@ static int elf_output_file(TCCState *s1, const char *filename) sym->st_value += s1->sections[sym->st_shndx]->sh_addr; } } - - /* put dynamic section entries */ - dynamic->data_offset = saved_dynamic_data_offset; - put_dt(dynamic, DT_HASH, s1->dynsym->hash->sh_addr); - put_dt(dynamic, DT_STRTAB, dynstr->sh_addr); - put_dt(dynamic, DT_SYMTAB, s1->dynsym->sh_addr); - put_dt(dynamic, DT_STRSZ, dynstr->data_offset); - put_dt(dynamic, DT_SYMENT, sizeof(ElfW(Sym))); -#ifdef TCC_TARGET_X86_64 - put_dt(dynamic, DT_RELA, rel_addr); - put_dt(dynamic, DT_RELASZ, rel_size); - put_dt(dynamic, DT_RELAENT, sizeof(ElfW_Rel)); -#else -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - put_dt(dynamic, DT_PLTGOT, s1->got->sh_addr); - put_dt(dynamic, DT_PLTRELSZ, rel_size); - put_dt(dynamic, DT_JMPREL, rel_addr); - put_dt(dynamic, DT_PLTREL, DT_REL); - put_dt(dynamic, DT_REL, bss_addr); - put_dt(dynamic, DT_RELSZ, bss_size); -#else - put_dt(dynamic, DT_REL, rel_addr); - put_dt(dynamic, DT_RELSZ, rel_size); - put_dt(dynamic, DT_RELENT, sizeof(ElfW_Rel)); -#endif -#endif - if (s1->do_debug) - put_dt(dynamic, DT_DEBUG, 0); - put_dt(dynamic, DT_NULL, 0); } - - ehdr.e_phentsize = sizeof(ElfW(Phdr)); - ehdr.e_phnum = phnum; - ehdr.e_phoff = sizeof(ElfW(Ehdr)); } - /* all other sections come after */ - for(i = 1; i < s1->nb_sections; i++) { - s = s1->sections[i]; - if (phnum > 0 && (s->sh_flags & SHF_ALLOC)) - continue; - section_order[sh_order_index++] = i; - - file_offset = (file_offset + s->sh_addralign - 1) & - ~(s->sh_addralign - 1); - s->sh_offset = file_offset; - if (s->sh_type != SHT_NOBITS) - file_offset += s->sh_size; - } - /* if building executable or DLL, then relocate each section except the GOT which is already relocated */ if (file_type != TCC_OUTPUT_OBJ) { - relocate_syms(s1, 0); - - if (s1->nb_errors != 0) { - fail: - ret = -1; + ret = final_sections_reloc(s1); + if (ret) goto the_end; - } - - /* relocate sections */ - /* XXX: ignore sections with allocated relocations ? */ - for(i = 1; i < s1->nb_sections; i++) { - s = s1->sections[i]; - if (s->reloc && s != s1->got) - relocate_section(s1, s); - } - - /* relocate relocation entries if the relocation tables are - allocated in the executable */ - for(i = 1; i < s1->nb_sections; i++) { - s = s1->sections[i]; - if ((s->sh_flags & SHF_ALLOC) && - s->sh_type == SHT_RELX) { - relocate_rel(s1, s); - } - } - - /* get entry point address */ - if (file_type == TCC_OUTPUT_EXE) - ehdr.e_entry = get_elf_sym_addr(s1, "_start", 1); - else - ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */ } + + /* Perform relocation to GOT or PLT entries */ if (file_type == TCC_OUTPUT_EXE && s1->static_link) fill_got(s1); - /* write elf file */ - if (file_type == TCC_OUTPUT_OBJ) - mode = 0666; - else - mode = 0777; - unlink(filename); - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); - if (fd < 0) { - tcc_error_noabort("could not write '%s'", filename); - goto fail; - } - f = fdopen(fd, "wb"); - if (s1->verbose) - printf("<- %s\n", filename); - -#ifdef TCC_TARGET_COFF - if (s1->output_format == TCC_OUTPUT_FORMAT_COFF) { - tcc_output_coff(s1, f); - } else -#endif - if (s1->output_format == TCC_OUTPUT_FORMAT_ELF) { - sort_syms(s1, symtab_section); - - /* align to 4 */ - file_offset = (file_offset + 3) & -4; - - /* fill header */ - ehdr.e_ident[0] = ELFMAG0; - ehdr.e_ident[1] = ELFMAG1; - ehdr.e_ident[2] = ELFMAG2; - ehdr.e_ident[3] = ELFMAG3; - ehdr.e_ident[4] = ELFCLASSW; - ehdr.e_ident[5] = ELFDATA2LSB; - ehdr.e_ident[6] = EV_CURRENT; -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; -#endif -#ifdef TCC_TARGET_ARM -#ifdef TCC_ARM_EABI - ehdr.e_ident[EI_OSABI] = 0; - ehdr.e_flags = 4 << 24; -#else - ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM; -#endif -#endif - switch(file_type) { - default: - case TCC_OUTPUT_EXE: - ehdr.e_type = ET_EXEC; - break; - case TCC_OUTPUT_DLL: - ehdr.e_type = ET_DYN; - break; - case TCC_OUTPUT_OBJ: - ehdr.e_type = ET_REL; - break; - } - ehdr.e_machine = EM_TCC_TARGET; - ehdr.e_version = EV_CURRENT; - ehdr.e_shoff = file_offset; - ehdr.e_ehsize = sizeof(ElfW(Ehdr)); - ehdr.e_shentsize = sizeof(ElfW(Shdr)); - ehdr.e_shnum = shnum; - ehdr.e_shstrndx = shnum - 1; - - fwrite(&ehdr, 1, sizeof(ElfW(Ehdr)), f); - fwrite(phdr, 1, phnum * sizeof(ElfW(Phdr)), f); - offset = sizeof(ElfW(Ehdr)) + phnum * sizeof(ElfW(Phdr)); - - for(i=1;inb_sections;i++) { - s = s1->sections[section_order[i]]; - if (s->sh_type != SHT_NOBITS) { - if (s->sh_type == SHT_DYNSYM) - patch_dynsym_undef(s1, s); - while (offset < s->sh_offset) { - fputc(0, f); - offset++; - } - size = s->sh_size; - fwrite(s->data, 1, size, f); - offset += size; - } - } - - /* output section headers */ - while (offset < ehdr.e_shoff) { - fputc(0, f); - offset++; - } - - for(i=0;inb_sections;i++) { - sh = &shdr; - memset(sh, 0, sizeof(ElfW(Shdr))); - s = s1->sections[i]; - if (s) { - sh->sh_name = s->sh_name; - sh->sh_type = s->sh_type; - sh->sh_flags = s->sh_flags; - sh->sh_entsize = s->sh_entsize; - sh->sh_info = s->sh_info; - if (s->link) - sh->sh_link = s->link->sh_num; - sh->sh_addralign = s->sh_addralign; - sh->sh_addr = s->sh_addr; - sh->sh_offset = s->sh_offset; - sh->sh_size = s->sh_size; - } - fwrite(sh, 1, sizeof(ElfW(Shdr)), f); - } - } else { - tcc_output_binary(s1, f, section_order); - } - fclose(f); - - ret = 0; + /* Create the ELF file with name 'filename' */ + ret = tcc_write_elf_file(s1, filename, phnum, phdr, file_offset, sec_order); the_end: tcc_free(s1->symtab_to_dynsym); - tcc_free(section_order); + tcc_free(sec_order); tcc_free(phdr); tcc_free(s1->sym_attrs); + s1->sym_attrs = NULL; return ret; } @@ -2328,9 +2492,7 @@ LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename) ret = pe_output_file(s, filename); } else #endif - { ret = elf_output_file(s, filename); - } return ret; } @@ -2353,9 +2515,9 @@ typedef struct SectionMergeInfo { /* load an object file and merge it with current files */ /* XXX: handle correctly stab (debug) info */ -ST_FUNC int tcc_load_object_file(TCCState *s1, +ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset) -{ +{ ElfW(Ehdr) ehdr; ElfW(Shdr) *shdr, *sh; int size, i, j, offset, offseti, nb_syms, sym_index, ret; @@ -2364,7 +2526,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, char *sh_name, *name; SectionMergeInfo *sm_table, *sm; ElfW(Sym) *sym, *symtab; - ElfW_Rel *rel, *rel_end; + ElfW_Rel *rel; Section *s; int stab_index; @@ -2390,10 +2552,10 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, return -1; } /* read sections */ - shdr = load_data(fd, file_offset + ehdr.e_shoff, + shdr = load_data(fd, file_offset + ehdr.e_shoff, sizeof(ElfW(Shdr)) * ehdr.e_shnum); sm_table = tcc_mallocz(sizeof(SectionMergeInfo) * ehdr.e_shnum); - + /* load section names */ sh = &shdr[ehdr.e_shstrndx]; strsec = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); @@ -2421,7 +2583,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, strtab = load_data(fd, file_offset + sh->sh_offset, sh->sh_size); } } - + /* now examine each section and try to merge its content with the ones in memory */ for(i = 1; i < ehdr.e_shnum; i++) { @@ -2429,14 +2591,14 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, if (i == ehdr.e_shstrndx) continue; sh = &shdr[i]; - sh_name = strsec + sh->sh_name; + sh_name = (char *) strsec + sh->sh_name; /* ignore sections types we do not handle */ if (sh->sh_type != SHT_PROGBITS && - sh->sh_type != SHT_RELX && + sh->sh_type != SHT_RELX && #ifdef TCC_ARM_EABI sh->sh_type != SHT_ARM_EXIDX && #endif - sh->sh_type != SHT_NOBITS && + sh->sh_type != SHT_NOBITS && sh->sh_type != SHT_PREINIT_ARRAY && sh->sh_type != SHT_INIT_ARRAY && sh->sh_type != SHT_FINI_ARRAY && @@ -2449,7 +2611,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, for(j = 1; j < s1->nb_sections;j++) { s = s1->sections[j]; if (!strcmp(s->name, sh_name)) { - if (!strncmp(sh_name, ".gnu.linkonce", + 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 @@ -2508,7 +2670,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, next: ; } - /* //gr relocate stab strings */ + /* gr relocate stab strings */ if (stab_index && stabstr_index) { Stab_Sym *a, *b; unsigned o; @@ -2516,7 +2678,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, a = (Stab_Sym *)(s->data + sm_table[stab_index].offset); b = (Stab_Sym *)(s->data + s->data_offset); o = sm_table[stabstr_index].offset; - while (a < b) + while (a < b) a->n_strx += o, a++; } @@ -2550,7 +2712,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, already defined symbol. It is very important to get correct relocations */ if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { - name = strtab + sym->st_name; + name = (char *) strtab + sym->st_name; sym_index = find_elf_sym(symtab_section, name); if (sym_index) old_to_new_syms[i] = sym_index; @@ -2566,9 +2728,9 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, sym->st_value += sm->offset; } /* add symbol */ - name = strtab + sym->st_name; - sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size, - sym->st_info, sym->st_other, + name = (char *) strtab + sym->st_name; + sym_index = add_elf_sym(symtab_section, sym->st_value, sym->st_size, + sym->st_info, sym->st_other, sym->st_shndx, name); old_to_new_syms[i] = sym_index; } @@ -2584,10 +2746,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, case SHT_RELX: /* take relocation offset information */ offseti = sm_table[sh->sh_info].offset; - rel_end = (ElfW_Rel *)(s->data + s->data_offset); - for(rel = (ElfW_Rel *)(s->data + offset); - rel < rel_end; - rel++) { + for_each_elem(s, (offset / sizeof(*rel)), rel, ElfW_Rel) { int type; unsigned sym_index; /* convert symbol index */ @@ -2618,8 +2777,8 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, handled by converting these instructions into blx instructions. Other case of instructions referencing a PLT entry require to add a Thumb stub before the PLT entry to - switch to ARM mode. We set bit 0 of the got offset of a - symbol to indicate such a case. */ + switch to ARM mode. We set bit plt_thumb_stub of the + attribute of a symbol to indicate such a case. */ if (type == R_ARM_THM_JUMP24) alloc_sym_attr(s1, sym_index)->plt_thumb_stub = 1; #endif @@ -2629,7 +2788,7 @@ ST_FUNC int tcc_load_object_file(TCCState *s1, break; } } - + ret = 0; the_end: tcc_free(symtab); @@ -2670,7 +2829,7 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size) goto fail; nsyms = get_be32(data); ar_index = data + 4; - ar_names = ar_index + nsyms * 4; + ar_names = (char *) ar_index + nsyms * 4; do { bound = 0; @@ -2680,9 +2839,6 @@ static int tcc_load_alacarte(TCCState *s1, int fd, int size) sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; if(sym->st_shndx == SHN_UNDEF) { off = get_be32(ar_index + i * 4) + sizeof(ArchiveHeader); -#if 0 - printf("%5d\t%s\t%08x\n", i, p, sym->st_shndx); -#endif ++bound; lseek(fd, off, SEEK_SET); if(tcc_load_object_file(s1, fd, off) < 0) { @@ -2712,7 +2868,7 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) /* skip magic which was already checked */ read(fd, magic, sizeof(magic)); - + for(;;) { len = read(fd, &hdr, sizeof(hdr)); if (len == 0) @@ -2730,7 +2886,6 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) break; } ar_name[i + 1] = '\0'; - // printf("name='%s' size=%d %s\n", ar_name, size, ar_size); file_offset = lseek(fd, 0, SEEK_CUR); /* align to even */ size = (size + 1) & ~1; @@ -2757,7 +2912,7 @@ ST_FUNC int tcc_load_archive(TCCState *s1, int fd) is referenced by the user (so it should be added as DT_NEEDED in the generated ELF file) */ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) -{ +{ ElfW(Ehdr) ehdr; ElfW(Shdr) *shdr, *sh, *sh1; int i, j, nb_syms, nb_dts, sym_bind, ret; @@ -2766,7 +2921,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) unsigned char *dynstr; const char *name, *soname; DLLReference *dllref; - + read(fd, &ehdr, sizeof(ehdr)); /* test CPU specific stuff */ @@ -2801,13 +2956,13 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) break; } } - + /* compute the real library name */ soname = tcc_basename(filename); - + for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) { if (dt->d_tag == DT_SONAME) { - soname = dynstr + dt->d_un.d_val; + soname = (char *) dynstr + dt->d_un.d_val; } } @@ -2822,8 +2977,6 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) goto the_end; } } - - // printf("loading dll '%s'\n", soname); /* add the dll and its level */ dllref = tcc_mallocz(sizeof(DLLReference) + strlen(soname)); @@ -2836,7 +2989,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) sym_bind = ELFW(ST_BIND)(sym->st_info); if (sym_bind == STB_LOCAL) continue; - name = dynstr + sym->st_name; + name = (char *) dynstr + sym->st_name; add_elf_sym(s1->dynsymtab_section, sym->st_value, sym->st_size, sym->st_info, sym->st_other, sym->st_shndx, name); } @@ -2845,7 +2998,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level) for(i = 0, dt = dynamic; i < nb_dts; i++, dt++) { switch(dt->d_tag) { case DT_NEEDED: - name = dynstr + dt->d_un.d_val; + name = (char *) dynstr + dt->d_un.d_val; for(j = 0; j < s1->nb_loaded_dlls; j++) { dllref = s1->loaded_dlls[j]; if (!strcmp(name, dllref->name)) @@ -2983,11 +3136,6 @@ static int ld_next(TCCState *s1, char *name, int name_size) inp(); break; } -#if 0 - printf("tok=%c %d\n", c, c); - if (c == LD_TOK_NAME) - printf(" name=%s\n", name); -#endif return c; } @@ -3091,7 +3239,7 @@ ST_FUNC int tcc_load_ldscript(TCCState *s1) char cmd[64]; char filename[1024]; int t, ret; - + ch = file->buf_ptr[0]; ch = handle_eob(); for(;;) { @@ -3126,4 +3274,4 @@ ST_FUNC int tcc_load_ldscript(TCCState *s1) } return 0; } -#endif /* ndef TCC_TARGET_PE */ +#endif /* !TCC_TARGET_PE */ diff --git a/tccgen.c b/tccgen.c index 14a5a54..1a89d4a 100644 --- a/tccgen.c +++ b/tccgen.c @@ -66,6 +66,7 @@ ST_DATA int const_wanted; /* true if constant wanted */ ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */ ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */ ST_DATA CType func_vt; /* current function return type (used by return instruction) */ +ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */ ST_DATA int func_vc; ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ ST_DATA char *funcname; @@ -89,6 +90,10 @@ static void vla_runtime_type_size(CType *type, int *a); static void vla_sp_save(void); static int is_compatible_parameter_types(CType *type1, CType *type2); static void expr_type(CType *type); +ST_FUNC void vpush64(int ty, unsigned long long v); +ST_FUNC void vpush(CType *type); +ST_FUNC int gvtst(int inv, int t); +ST_FUNC int is_btype_size(int bt); ST_INLN int is_float(int t) { @@ -102,7 +107,8 @@ ST_INLN int is_float(int t) /* XXX: endianness dependent */ ST_FUNC int ieee_finite(double d) { - int *p = (int *)&d; + int p[4]; + memcpy(p, &d, sizeof(double)); return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31; } @@ -160,7 +166,6 @@ ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c) tcc_error("incompatible types for redefinition of '%s'", get_tok_str(v, NULL)); } - s = *ps; s = sym_malloc(); s->asm_label = NULL; s->v = v; @@ -184,6 +189,8 @@ ST_FUNC Sym *sym_find2(Sym *s, int v) while (s) { if (s->v == v) return s; + else if (s->v == -1) + return NULL; s = s->prev; } return NULL; @@ -293,6 +300,28 @@ static void weaken_symbol(Sym *sym) } } +static void apply_visibility(Sym *sym, CType *type) +{ + int vis = sym->type.t & VT_VIS_MASK; + int vis2 = type->t & VT_VIS_MASK; + if (vis == (STV_DEFAULT << VT_VIS_SHIFT)) + vis = vis2; + else if (vis2 == (STV_DEFAULT << VT_VIS_SHIFT)) + ; + else + vis = (vis < vis2) ? vis : vis2; + sym->type.t &= ~VT_VIS_MASK; + sym->type.t |= vis; + + if (sym->c > 0) { + ElfW(Sym) *esym; + + esym = &((ElfW(Sym) *)symtab_section->data)[sym->c]; + vis >>= VT_VIS_SHIFT; + esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) | vis; + } +} + /* ------------------------------------------------------------------------- */ ST_FUNC void swap(int *p, int *q) @@ -308,7 +337,7 @@ static void vsetc(CType *type, int r, CValue *vc) int v; if (vtop >= vstack + (VSTACK_SIZE - 1)) - tcc_error("memory full"); + tcc_error("memory full (vstack)"); /* cannot let cpu flags if other instruction are generated. Also avoid leaving VT_JMP anywhere except on the top of the stack because it would complicate the code generator. */ @@ -325,7 +354,7 @@ static void vsetc(CType *type, int r, CValue *vc) } /* push constant of type "type" with useless value */ -void vpush(CType *type) +ST_FUNC void vpush(CType *type) { CValue cval; vsetc(type, VT_CONST, &cval); @@ -340,18 +369,15 @@ ST_FUNC void vpushi(int v) } /* push a pointer sized constant */ -static void vpushs(long long v) +static void vpushs(addr_t v) { CValue cval; - if (PTR_SIZE == 4) - cval.i = (int)v; - else - cval.ull = v; + cval.ptr_offset = v; vsetc(&size_type, VT_CONST, &cval); } /* push arbitrary 64bit constant */ -void vpush64(int ty, unsigned long long v) +ST_FUNC void vpush64(int ty, unsigned long long v) { CValue cval; CType ctype; @@ -367,6 +393,15 @@ static inline void vpushll(long long v) vpush64(VT_LLONG, v); } +/* push a symbol value of TYPE */ +static inline void vpushsym(CType *type, Sym *sym) +{ + CValue cval; + cval.ptr_offset = 0; + vsetc(type, VT_CONST | VT_SYM, &cval); + vtop->sym = sym; +} + /* Return a static symbol pointing to a section */ ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size) { @@ -384,11 +419,7 @@ ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsign /* push a reference to a section offset by adding a dummy symbol */ static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size) { - CValue cval; - - cval.ul = 0; - vsetc(type, VT_CONST | VT_SYM, &cval); - vtop->sym = get_sym_ref(type, sec, offset, size); + vpushsym(type, get_sym_ref(type, sec, offset, size)); } /* define a new external reference to a symbol 'v' of type 'u' */ @@ -427,19 +458,20 @@ static Sym *external_sym(int v, CType *type, int r, char *asm_label) tcc_error("incompatible types for redefinition of '%s'", get_tok_str(v, NULL)); } + /* Merge some storage attributes. */ + if (type->t & VT_WEAK) + weaken_symbol(s); + + if (type->t & VT_VIS_MASK) + apply_visibility(s, type); + return s; } /* push a reference to global symbol v */ ST_FUNC void vpush_global_sym(CType *type, int v) { - Sym *sym; - CValue cval; - - sym = external_global_sym(v, type, 0); - cval.ul = 0; - vsetc(type, VT_CONST | VT_SYM, &cval); - vtop->sym = sym; + vpushsym(type, external_global_sym(v, type, 0)); } ST_FUNC void vset(CType *type, int r, int v) @@ -482,7 +514,7 @@ ST_FUNC void vswap(void) ST_FUNC void vpushv(SValue *v) { if (vtop >= vstack + (VSTACK_SIZE - 1)) - tcc_error("memory full"); + tcc_error("memory full (vstack)"); vtop++; *vtop = *v; } @@ -762,7 +794,7 @@ ST_FUNC int gv(int rc) sym = get_sym_ref(&vtop->type, data_section, offset, size << 2); vtop->r |= VT_LVAL | VT_SYM; vtop->sym = sym; - vtop->c.ul = 0; + vtop->c.ptr_offset = 0; } #ifdef CONFIG_TCC_BCHECK if (vtop->r & VT_MUSTBOUND) @@ -800,9 +832,9 @@ ST_FUNC int gv(int rc) #else if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { int addr_type = VT_INT, load_size = 4, load_type = VT_INT; + unsigned long long ll; #endif int r2, original_type; - unsigned long long ll; original_type = vtop->type.t; /* two register type load : expand to two words temporarily */ @@ -947,7 +979,7 @@ static void lexpand(void) { int u; - u = vtop->type.t & VT_UNSIGNED; + u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED); gv(RC_INT); vdup(); vtop[0].r = vtop[-1].r2; @@ -963,7 +995,7 @@ ST_FUNC void lexpand_nr(void) { int u,v; - u = vtop->type.t & VT_UNSIGNED; + u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED); vdup(); vtop->r2 = VT_CONST; vtop->type.t = VT_INT | u; @@ -1095,6 +1127,26 @@ static void gv_dup(void) } } +/* Generate value test + * + * Generate a test for any value (jump, comparison and integers) */ +ST_FUNC int gvtst(int inv, int t) +{ + int v = vtop->r & VT_VALMASK; + if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) { + vpushi(0); + gen_op(TOK_NE); + } + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { + /* constant jmp optimization */ + if ((vtop->c.i != 0) != inv) + t = gjmp(t); + vtop--; + return t; + } + return gtst(inv, t); +} + #ifndef TCC_TARGET_X86_64 /* generate CPU independent (unsigned) long long operations */ static void gen_opl(int op) @@ -1293,13 +1345,13 @@ static void gen_opl(int op) b = 0; gen_op(op1); if (op1 != TOK_NE) { - a = gtst(1, 0); + a = gvtst(1, 0); } if (op != TOK_EQ) { /* generate non equal test */ /* XXX: NOT PORTABLE yet */ if (a == 0) { - b = gtst(0, 0); + b = gvtst(0, 0); } else { #if defined(TCC_TARGET_I386) b = psym(0x850f, 0); @@ -1324,7 +1376,7 @@ static void gen_opl(int op) else if (op1 == TOK_GE) op1 = TOK_UGE; gen_op(op1); - a = gtst(1, a); + a = gvtst(1, a); gsym(b); vseti(VT_JMPI, a); break; @@ -1553,7 +1605,7 @@ static inline int is_null_pointer(SValue *p) return 0; return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) || ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) || - ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0); + ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr_offset == 0); } static inline int is_integer_btype(int bt) @@ -1562,7 +1614,7 @@ static inline int is_integer_btype(int bt) bt == VT_INT || bt == VT_LLONG); } -/* check types for comparison or substraction of pointers */ +/* check types for comparison or subtraction of pointers */ static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op) { CType *type1, *type2, tmp_type1, tmp_type2; @@ -1599,8 +1651,8 @@ static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op) return; tmp_type1 = *type1; tmp_type2 = *type2; - tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); - tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); if (!is_compatible_types(&tmp_type1, &tmp_type2)) { /* gcc-like error if '-' is used */ if (op == '-') @@ -1927,13 +1979,16 @@ static void gen_cast(CType *type) vtop->c.ull = vtop->c.ll; else if (dbt == VT_BOOL) vtop->c.i = (vtop->c.ll != 0); +#ifdef TCC_TARGET_X86_64 + else if (dbt == VT_PTR) + ; +#endif else if (dbt != VT_LLONG) { int s = 0; if ((dbt & VT_BTYPE) == VT_BYTE) s = 24; else if ((dbt & VT_BTYPE) == VT_SHORT) s = 16; - if(dbt & VT_UNSIGNED) vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s; else @@ -2151,7 +2206,7 @@ static int is_compatible_func(CType *type1, CType *type2) if (!is_compatible_types(&s1->type, &s2->type)) return 0; /* check func_call */ - if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r)) + if (s1->a.func_call != s2->a.func_call) return 0; /* XXX: not complete */ if (s1->c == FUNC_OLD || s2->c == FUNC_OLD) @@ -2187,6 +2242,11 @@ static int compare_types(CType *type1, CType *type2, int unqualified) t1 &= ~(VT_CONSTANT | VT_VOLATILE); t2 &= ~(VT_CONSTANT | VT_VOLATILE); } + /* Default Vs explicit signedness only matters for char */ + if ((t1 & VT_BTYPE) != VT_BYTE) { + t1 &= ~VT_DEFSIGN; + t2 &= ~VT_DEFSIGN; + } /* XXX: bitfields ? */ if (t1 != t2) return 0; @@ -2239,8 +2299,10 @@ static void type_to_str(char *buf, int buf_size, pstrcat(buf, buf_size, "const "); if (t & VT_VOLATILE) pstrcat(buf, buf_size, "volatile "); - if (t & VT_UNSIGNED) + if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED)) pstrcat(buf, buf_size, "unsigned "); + else if (t & VT_DEFSIGN) + pstrcat(buf, buf_size, "signed "); switch(bt) { case VT_VOID: tstr = "void"; @@ -2327,8 +2389,8 @@ static void gen_assign_cast(CType *dt) st = &vtop->type; /* source type */ dbt = dt->t & VT_BTYPE; sbt = st->t & VT_BTYPE; - if (sbt == VT_VOID) - tcc_error("Cannot assign void value"); + if (sbt == VT_VOID || dbt == VT_VOID) + tcc_error("cannot cast from/to void"); if (dt->t & VT_CONSTANT) tcc_warning("assignment of read-only location"); switch(dbt) { @@ -2360,8 +2422,10 @@ static void gen_assign_cast(CType *dt) /* exact type match, except for unsigned */ tmp_type1 = *type1; tmp_type2 = *type2; - tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); - tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | + VT_VOLATILE); + tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | + VT_VOLATILE); if (!is_compatible_types(&tmp_type1, &tmp_type2)) tcc_warning("assignment from incompatible pointer type"); } @@ -2627,6 +2691,24 @@ static void parse_attribute(AttributeDef *ad) next(); skip(')'); break; + case TOK_VISIBILITY1: + case TOK_VISIBILITY2: + skip('('); + if (tok != TOK_STR) + expect("visibility(\"default|hidden|internal|protected\")"); + if (!strcmp (tokc.cstr->data, "default")) + ad->a.visibility = STV_DEFAULT; + else if (!strcmp (tokc.cstr->data, "hidden")) + ad->a.visibility = STV_HIDDEN; + else if (!strcmp (tokc.cstr->data, "internal")) + ad->a.visibility = STV_INTERNAL; + else if (!strcmp (tokc.cstr->data, "protected")) + ad->a.visibility = STV_PROTECTED; + else + expect("visibility(\"default|hidden|internal|protected\")"); + next(); + skip(')'); + break; case TOK_ALIGNED1: case TOK_ALIGNED2: if (tok == '(') { @@ -2638,15 +2720,15 @@ static void parse_attribute(AttributeDef *ad) } else { n = MAX_ALIGN; } - ad->aligned = n; + ad->a.aligned = n; break; case TOK_PACKED1: case TOK_PACKED2: - ad->packed = 1; + ad->a.packed = 1; break; case TOK_WEAK1: case TOK_WEAK2: - ad->weak = 1; + ad->a.weak = 1; break; case TOK_UNUSED1: case TOK_UNUSED2: @@ -2661,12 +2743,12 @@ static void parse_attribute(AttributeDef *ad) case TOK_CDECL1: case TOK_CDECL2: case TOK_CDECL3: - ad->func_call = FUNC_CDECL; + ad->a.func_call = FUNC_CDECL; break; case TOK_STDCALL1: case TOK_STDCALL2: case TOK_STDCALL3: - ad->func_call = FUNC_STDCALL; + ad->a.func_call = FUNC_STDCALL; break; #ifdef TCC_TARGET_I386 case TOK_REGPARM1: @@ -2678,26 +2760,26 @@ static void parse_attribute(AttributeDef *ad) else if (n < 0) n = 0; if (n > 0) - ad->func_call = FUNC_FASTCALL1 + n - 1; + ad->a.func_call = FUNC_FASTCALL1 + n - 1; skip(')'); break; case TOK_FASTCALL1: case TOK_FASTCALL2: case TOK_FASTCALL3: - ad->func_call = FUNC_FASTCALLW; + ad->a.func_call = FUNC_FASTCALLW; break; #endif case TOK_MODE: skip('('); switch(tok) { case TOK_MODE_DI: - ad->mode = VT_LLONG + 1; + ad->a.mode = VT_LLONG + 1; break; case TOK_MODE_HI: - ad->mode = VT_SHORT + 1; + ad->a.mode = VT_SHORT + 1; break; case TOK_MODE_SI: - ad->mode = VT_INT + 1; + ad->a.mode = VT_INT + 1; break; default: tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL)); @@ -2707,10 +2789,10 @@ static void parse_attribute(AttributeDef *ad) skip(')'); break; case TOK_DLLEXPORT: - ad->func_export = 1; + ad->a.func_export = 1; break; case TOK_DLLIMPORT: - ad->func_import = 1; + ad->a.func_import = 1; break; default: if (tcc_state->warn_unsupported) @@ -2738,9 +2820,9 @@ static void parse_attribute(AttributeDef *ad) } /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */ -static void struct_decl(CType *type, int u) +static void struct_decl(CType *type, int u, int tdef) { - int a, v, size, align, maxalign, c, offset; + int a, v, size, align, maxalign, c, offset, flexible; int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt; Sym *s, *ss, *ass, **ps; AttributeDef ad; @@ -2759,11 +2841,13 @@ static void struct_decl(CType *type, int u) if (s->type.t != a) tcc_error("invalid type"); goto do_decl; - } + } else if (tok >= TOK_IDENT && !tdef) + tcc_error("unknown struct/union/enum"); } else { v = anon_sym++; } type1.t = a; + type1.ref = NULL; /* we put an undefined size for struct/union */ s = sym_push(v | SYM_STRUCT, &type1, 0, -1); s->r = 0; /* default alignment is zero as gcc */ @@ -2784,6 +2868,10 @@ static void struct_decl(CType *type, int u) v = tok; if (v < TOK_UIDENT) expect("identifier"); + ss = sym_find(v); + if (ss && !local_stack) + tcc_error("redefinition of enumerator '%s'", + get_tok_str(v, NULL)); next(); if (tok == '=') { next(); @@ -2800,6 +2888,7 @@ static void struct_decl(CType *type, int u) if (tok == '}') break; } + s->c = type_size(&int_type, &align); skip('}'); } else { maxalign = 1; @@ -2807,9 +2896,13 @@ static void struct_decl(CType *type, int u) prevbt = VT_INT; bit_pos = 0; offset = 0; + flexible = 0; while (tok != '}') { parse_btype(&btype, &ad); while (1) { + if (flexible) + tcc_error("flexible array member '%s' not at the end of struct", + get_tok_str(v, NULL)); bit_size = -1; v = 0; type1 = btype; @@ -2817,6 +2910,13 @@ static void struct_decl(CType *type, int u) type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT); if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT) expect("identifier"); + if (type_size(&type1, &align) < 0) { + if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c) + flexible = 1; + else + tcc_error("field '%s' has incomplete type", + get_tok_str(v, NULL)); + } if ((type1.t & VT_BTYPE) == VT_FUNC || (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE))) tcc_error("invalid type for '%s'", @@ -2834,10 +2934,10 @@ static void struct_decl(CType *type, int u) get_tok_str(v, NULL)); } size = type_size(&type1, &align); - if (ad.aligned) { - if (align < ad.aligned) - align = ad.aligned; - } else if (ad.packed) { + if (ad.a.aligned) { + if (align < ad.a.aligned) + align = ad.a.aligned; + } else if (ad.a.packed) { align = 1; } else if (*tcc_state->pack_stack_ptr) { if (align > *tcc_state->pack_stack_ptr) @@ -2937,19 +3037,25 @@ static void struct_decl(CType *type, int u) } } +/* return 1 if basic type is a type size (short, long, long long) */ +ST_FUNC int is_btype_size(int bt) +{ + return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG; +} + /* return 0 if no type declaration. otherwise, return the basic type and skip it. */ static int parse_btype(CType *type, AttributeDef *ad) { - int t, u, type_found, typespec_found, typedef_found; + int t, u, bt_size, complete, type_found, typespec_found; Sym *s; CType type1; memset(ad, 0, sizeof(AttributeDef)); + complete = 0; type_found = 0; typespec_found = 0; - typedef_found = 0; t = 0; while(1) { switch(tok) { @@ -2964,9 +3070,12 @@ static int parse_btype(CType *type, AttributeDef *ad) basic_type: next(); basic_type1: - if ((t & VT_BTYPE) != 0) + if (complete) tcc_error("too many basic types"); t |= u; + bt_size = is_btype_size (u & VT_BTYPE); + if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF))) + complete = 1; typespec_found = 1; break; case TOK_VOID: @@ -2976,9 +3085,8 @@ static int parse_btype(CType *type, AttributeDef *ad) u = VT_SHORT; goto basic_type; case TOK_INT: - next(); - typespec_found = 1; - break; + u = VT_INT; + goto basic_type; case TOK_LONG: next(); if ((t & VT_BTYPE) == VT_DOUBLE) { @@ -3012,14 +3120,14 @@ static int parse_btype(CType *type, AttributeDef *ad) } break; case TOK_ENUM: - struct_decl(&type1, VT_ENUM); + struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF); basic_type2: u = type1.t; type->ref = type1.ref; goto basic_type1; case TOK_STRUCT: case TOK_UNION: - struct_decl(&type1, VT_STRUCT); + struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF); goto basic_type2; /* type modifiers */ @@ -3038,8 +3146,10 @@ static int parse_btype(CType *type, AttributeDef *ad) case TOK_SIGNED1: case TOK_SIGNED2: case TOK_SIGNED3: + if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED)) + tcc_error("signed and unsigned modifier"); typespec_found = 1; - t |= VT_SIGNED; + t |= VT_DEFSIGN; next(); break; case TOK_REGISTER: @@ -3050,7 +3160,9 @@ static int parse_btype(CType *type, AttributeDef *ad) next(); break; case TOK_UNSIGNED: - t |= VT_UNSIGNED; + if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN) + tcc_error("signed and unsigned modifier"); + t |= VT_DEFSIGN | VT_UNSIGNED; next(); typespec_found = 1; break; @@ -3079,8 +3191,8 @@ static int parse_btype(CType *type, AttributeDef *ad) case TOK_ATTRIBUTE1: case TOK_ATTRIBUTE2: parse_attribute(ad); - if (ad->mode) { - u = ad->mode -1; + if (ad->a.mode) { + u = ad->a.mode -1; t = (t & ~VT_BTYPE) | u; } break; @@ -3094,21 +3206,20 @@ static int parse_btype(CType *type, AttributeDef *ad) type1.t &= ~(VT_STORAGE&~VT_TYPEDEF); goto basic_type2; default: - if (typespec_found || typedef_found) + if (typespec_found) goto the_end; s = sym_find(tok); if (!s || !(s->type.t & VT_TYPEDEF)) goto the_end; - typedef_found = 1; t |= (s->type.t & ~VT_TYPEDEF); type->ref = s->type.ref; if (s->r) { /* get attributes from typedef */ - if (0 == ad->aligned) - ad->aligned = FUNC_ALIGN(s->r); - if (0 == ad->func_call) - ad->func_call = FUNC_CALL(s->r); - ad->packed |= FUNC_PACKED(s->r); + if (0 == ad->a.aligned) + ad->a.aligned = s->a.aligned; + if (0 == ad->a.func_call) + ad->a.func_call = s->a.func_call; + ad->a.packed |= s->a.packed; } next(); typespec_found = 1; @@ -3117,13 +3228,10 @@ static int parse_btype(CType *type, AttributeDef *ad) type_found = 1; } the_end: - if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED)) - tcc_error("signed and unsigned modifier"); if (tcc_state->char_is_unsigned) { - if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE) + if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE) t |= VT_UNSIGNED; } - t &= ~VT_SIGNED; /* long is never used as type */ if ((t & VT_BTYPE) == VT_LONG) @@ -3248,8 +3356,9 @@ static void post_type(CType *type, AttributeDef *ad) type->t |= VT_PTR; } /* we push a anonymous symbol which will contain the function prototype */ - ad->func_args = arg_size; - s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l); + ad->a.func_args = arg_size; + s = sym_push(SYM_FIELD, type, 0, l); + s->a = ad->a; s->next = first; type->t = VT_FUNC; type->ref = s; @@ -3277,6 +3386,8 @@ static void post_type(CType *type, AttributeDef *ad) skip(']'); /* parse next post type */ post_type(type, ad); + if (type->t == VT_FUNC) + tcc_error("declaration of an array of functions"); t1 |= type->t & VT_VLA; if (t1 & VT_VLA) { @@ -3444,6 +3555,9 @@ static void gfunc_param_typed(Sym *func, Sym *arg) if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) { type.t = VT_DOUBLE; gen_cast(&type); + } else if (vtop->type.t & VT_BITFIELD) { + type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED); + gen_cast(&type); } } else if (arg == NULL) { tcc_error("too many arguments to function"); @@ -3645,7 +3759,7 @@ ST_FUNC void unary(void) vtop->c.i = vtop->c.i ^ 1; else { save_regs(1); - vseti(VT_JMP, gtst(1, 0)); + vseti(VT_JMP, gvtst(1, 0)); } break; case '~': @@ -3656,12 +3770,16 @@ ST_FUNC void unary(void) break; case '+': next(); - /* in order to force cast, we add zero */ unary(); if ((vtop->type.t & VT_BTYPE) == VT_PTR) tcc_error("pointer not accepted for unary plus"); - vpushi(0); - gen_op('+'); + /* In order to force cast, we add zero, except for floating point + where we really need an noop (otherwise -0.0 will be transformed + into +0.0). */ + if (!is_float(vtop->type.t)) { + vpushi(0); + gen_op('+'); + } break; case TOK_SIZEOF: case TOK_ALIGNOF1: @@ -3756,7 +3874,6 @@ ST_FUNC void unary(void) case TOK_builtin_va_arg_types: { CType type; - int bt; next(); skip('('); parse_type(&type); @@ -3775,9 +3892,22 @@ ST_FUNC void unary(void) break; case '-': next(); - vpushi(0); unary(); - gen_op('-'); + t = vtop->type.t & VT_BTYPE; + if (is_float(t)) { + /* In IEEE negate(x) isn't subtract(0,x), but rather + subtract(-0, x). */ + vpush(&vtop->type); + if (t == VT_FLOAT) + vtop->c.f = -0.0f; + else if (t == VT_DOUBLE) + vtop->c.d = -0.0; + else + vtop->c.ld = -0.0; + } else + vpushi(0); + vswap(); + gen_op('-'); break; case TOK_LAND: if (!gnu_ext) @@ -3798,8 +3928,7 @@ ST_FUNC void unary(void) mk_pointer(&s->type); s->type.t |= VT_STATIC; } - vset(&s->type, VT_CONST | VT_SYM, 0); - vtop->sym = s; + vpushsym(&s->type, s); next(); break; @@ -3825,13 +3954,19 @@ ST_FUNC void unary(void) expect("identifier"); s = sym_find(t); if (!s) { + const char *name = get_tok_str(t, NULL); if (tok != '(') - tcc_error("'%s' undeclared", get_tok_str(t, NULL)); + tcc_error("'%s' undeclared", name); /* for simple function calls, we tolerate undeclared external reference to int() function */ - if (tcc_state->warn_implicit_function_declaration) - tcc_warning("implicit declaration of function '%s'", - get_tok_str(t, NULL)); + if (tcc_state->warn_implicit_function_declaration +#ifdef TCC_TARGET_PE + /* people must be warned about using undeclared WINAPI functions + (which usually start with uppercase letter) */ + || (name[0] >= 'A' && name[0] <= 'Z') +#endif + ) + tcc_warning("implicit declaration of function '%s'", name); s = external_global_sym(t, &func_old_type, 0); } if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) == @@ -3851,7 +3986,7 @@ ST_FUNC void unary(void) /* if forward reference, we must point to s */ if (vtop->r & VT_SYM) { vtop->sym = s; - vtop->c.ul = 0; + vtop->c.ptr_offset = 0; } break; } @@ -3908,7 +4043,7 @@ ST_FUNC void unary(void) } else if (tok == '(') { SValue ret; Sym *sa; - int nb_args, sret; + int nb_args, ret_nregs, ret_align, variadic; /* function call */ if ((vtop->type.t & VT_BTYPE) != VT_FUNC) { @@ -3932,9 +4067,10 @@ ST_FUNC void unary(void) ret.r2 = VT_CONST; /* compute first implicit argument if a structure is returned */ if ((s->type.t & VT_BTYPE) == VT_STRUCT) { - int ret_align; - sret = gfunc_sret(&s->type, &ret.type, &ret_align); - if (sret) { + variadic = (s->c == FUNC_ELLIPSIS); + ret_nregs = gfunc_sret(&s->type, variadic, &ret.type, + &ret_align); + if (!ret_nregs) { /* get some space for the returned structure */ size = type_size(&s->type, &align); loc = (loc - size) & -align; @@ -3947,11 +4083,11 @@ ST_FUNC void unary(void) nb_args++; } } else { - sret = 0; + ret_nregs = 1; ret.type = s->type; } - if (!sret) { + if (ret_nregs) { /* return in register */ if (is_float(ret.type.t)) { ret.r = reg_fret(ret.type.t); @@ -3990,19 +4126,31 @@ ST_FUNC void unary(void) } else { vtop -= (nb_args + 1); } + /* return value */ - vsetc(&ret.type, ret.r, &ret.c); - vtop->r2 = ret.r2; + for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) { + vsetc(&ret.type, r, &ret.c); + vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */ + } + /* handle packed struct return */ - if (((s->type.t & VT_BTYPE) == VT_STRUCT) && !sret) { - int addr; + if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) { + int addr, offset; + size = type_size(&s->type, &align); loc = (loc - size) & -align; addr = loc; - vset(&ret.type, VT_LOCAL | VT_LVAL, addr); - vswap(); - vstore(); - vtop--; + offset = 0; + for (;;) { + vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset); + vswap(); + vstore(); + vtop--; + if (--ret_nregs == 0) + break; + /* XXX: compatible with arm only: ret_align == register_size */ + offset += ret_align; + } vset(&s->type, VT_LOCAL | VT_LVAL, addr); } } else { @@ -4139,7 +4287,7 @@ static void expr_land(void) t = 0; save_regs(1); for(;;) { - t = gtst(1, t); + t = gvtst(1, t); if (tok != TOK_LAND) { vseti(VT_JMPI, t); break; @@ -4159,7 +4307,7 @@ static void expr_lor(void) t = 0; save_regs(1); for(;;) { - t = gtst(0, t); + t = gvtst(0, t); if (tok != TOK_LOR) { vseti(VT_JMP, t); break; @@ -4221,9 +4369,9 @@ static void expr_cond(void) } if (tok == ':' && gnu_ext) { gv_dup(); - tt = gtst(1, 0); + tt = gvtst(1, 0); } else { - tt = gtst(1, 0); + tt = gvtst(1, 0); gexpr(); } type1 = vtop->type; @@ -4469,7 +4617,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, skip('('); gexpr(); skip(')'); - a = gtst(1, 0); + a = gvtst(1, 0); block(bsym, csym, case_sym, def_sym, case_reg, 0); c = tok; if (c == TOK_ELSE) { @@ -4486,7 +4634,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, skip('('); gexpr(); skip(')'); - a = gtst(1, 0); + a = gvtst(1, 0); b = 0; block(&a, &b, case_sym, def_sym, case_reg, 0); gjmp_addr(d); @@ -4573,8 +4721,10 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, gen_assign_cast(&func_vt); if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { CType type, ret_type; - int ret_align; - if (gfunc_sret(&func_vt, &ret_type, &ret_align)) { + int ret_align, ret_nregs; + ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type, + &ret_align); + if (0 == ret_nregs) { /* if returning structure, must copy it to implicit first pointer arg location */ type = func_vt; @@ -4586,7 +4736,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, vstore(); } else { /* returning structure packed into registers */ - int size, addr, align; + int r, size, addr, align; size = type_size(&func_vt,&align); if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1))) && (align & (ret_align-1))) { @@ -4600,9 +4750,22 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, } vtop->type = ret_type; if (is_float(ret_type.t)) - gv(rc_fret(ret_type.t)); + r = rc_fret(ret_type.t); else - gv(RC_IRET); + r = RC_IRET; + + for (;;) { + gv(r); + if (--ret_nregs == 0) + break; + /* We assume that when a structure is returned in multiple + registers, their classes are consecutive values of the + suite s(n) = 2^n */ + r <<= 1; + /* XXX: compatible with arm only: ret_align == register_size */ + vtop->c.i += ret_align; + vtop->r = VT_LOCAL | VT_LVAL; + } } } else if (is_float(func_vt.t)) { gv(rc_fret(func_vt.t)); @@ -4650,7 +4813,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, b = 0; if (tok != ';') { gexpr(); - a = gtst(1, 0); + a = gvtst(1, 0); } skip(';'); if (tok != ')') { @@ -4679,7 +4842,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, skip('('); gsym(b); gexpr(); - c = gtst(0, 0); + c = gvtst(0, 0); gsym_addr(c, d); skip(')'); gsym(a); @@ -5037,6 +5200,12 @@ static void init_putv(CType *type, Section *sec, unsigned long c, case VT_LLONG: *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos; break; + case VT_PTR: + if (vtop->r & VT_SYM) { + greloc(sec, vtop->sym, c, R_DATA_PTR); + } + *(addr_t *)ptr |= (vtop->c.ptr_offset & bit_mask) << bit_pos; + break; default: if (vtop->r & VT_SYM) { greloc(sec, vtop->sym, c, R_DATA_PTR); @@ -5061,8 +5230,13 @@ static void init_putz(CType *t, Section *sec, unsigned long c, int size) } else { vpush_global_sym(&func_old_type, TOK_memset); vseti(VT_LOCAL, c); +#ifdef TCC_TARGET_ARM + vpushs(size); + vpushi(0); +#else vpushi(0); vpushs(size); +#endif gfunc_call(3); } } @@ -5337,12 +5511,13 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, flexible_array = NULL; if ((type->t & VT_BTYPE) == VT_STRUCT) { - Sym *field; - field = type->ref; - while (field && field->next) - field = field->next; - if (field->type.t & VT_ARRAY && field->type.ref->c < 0) - flexible_array = field; + Sym *field = type->ref->next; + if (field) { + while (field->next) + field = field->next; + if (field->type.t & VT_ARRAY && field->type.ref->c < 0) + flexible_array = field; + } } size = type_size(type, &align); @@ -5402,10 +5577,10 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, if (flexible_array) size += flexible_array->type.ref->c * pointed_size(&flexible_array->type); /* take into account specified alignment if bigger */ - if (ad->aligned) { - if (ad->aligned > align) - align = ad->aligned; - } else if (ad->packed) { + if (ad->a.aligned) { + if (ad->a.aligned > align) + align = ad->a.aligned; + } else if (ad->a.packed) { align = 1; } if ((r & VT_VALMASK) == VT_LOCAL) { @@ -5452,7 +5627,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, if (sym->type.t & VT_EXTERN) { /* if the variable is extern, it was not allocated */ sym->type.t &= ~VT_EXTERN; - /* set array size if it was ommited in extern + /* set array size if it was omitted in extern declaration */ if ((sym->type.t & VT_ARRAY) && sym->type.ref->c < 0 && @@ -5521,17 +5696,14 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, esym->st_shndx = SHN_COMMON; } } else { - CValue cval; - /* push global reference */ sym = get_sym_ref(type, sec, addr, size); - cval.ul = 0; - vsetc(type, VT_CONST | VT_SYM, &cval); - vtop->sym = sym; + vpushsym(type, sym); } /* patch symbol weakness */ if (type->t & VT_WEAK) weaken_symbol(sym); + apply_visibility(sym, type); #ifdef CONFIG_TCC_BCHECK /* handles bounds now because the symbol must be defined before for the relocation */ @@ -5646,6 +5818,21 @@ static void gen_function(Sym *sym) /* push a dummy symbol to enable local sym storage */ sym_push2(&local_stack, SYM_FIELD, 0, 0); gfunc_prolog(&sym->type); +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bounds_check + && !strcmp(get_tok_str(sym->v, NULL), "main")) { + int i; + + sym = local_stack; + for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) { + if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD) + break; + vpush_global_sym(&func_old_type, TOK___bound_main_arg); + vset(&sym->type, sym->r, sym->c); + gfunc_call(1); + } + } +#endif rsym = 0; block(NULL, NULL, NULL, NULL, 0, 0); gsym(rsym); @@ -5662,6 +5849,7 @@ static void gen_function(Sym *sym) /* patch symbol weakness (this definition overrules any prototype) */ if (sym->type.t & VT_WEAK) weaken_symbol(sym); + apply_visibility(sym, &sym->type); if (tcc_state->do_debug) { put_stabn(N_FUN, 0, 0, ind - func_ind); } @@ -5669,6 +5857,7 @@ static void gen_function(Sym *sym) cur_text_section = NULL; funcname = ""; /* for safety */ func_vt.t = VT_VOID; /* for safety */ + func_var = 0; /* for safety */ ind = 0; /* for safety */ nocode_wanted = saved_nocode_wanted; } @@ -5786,14 +5975,16 @@ static int decl0(int l, int is_for_loop_init) parse_attribute(&ad); } - if (ad.weak) + if (ad.a.weak) type.t |= VT_WEAK; #ifdef TCC_TARGET_PE - if (ad.func_import) + if (ad.a.func_import) type.t |= VT_IMPORT; - if (ad.func_export) + if (ad.a.func_export) type.t |= VT_EXPORT; #endif + type.t |= ad.a.visibility << VT_VIS_SHIFT; + if (tok == '{') { if (l == VT_LOCAL) tcc_error("cannot use local functions"); @@ -5812,28 +6003,38 @@ static int decl0(int l, int is_for_loop_init) sym = sym_find(v); if (sym) { + Sym *ref; if ((sym->type.t & VT_BTYPE) != VT_FUNC) goto func_error1; - r = sym->type.ref->r; + ref = sym->type.ref; + if (0 == ref->a.func_proto) + tcc_error("redefinition of '%s'", get_tok_str(v, NULL)); + /* use func_call from prototype if not defined */ - if (FUNC_CALL(r) != FUNC_CDECL - && FUNC_CALL(type.ref->r) == FUNC_CDECL) - FUNC_CALL(type.ref->r) = FUNC_CALL(r); + if (ref->a.func_call != FUNC_CDECL + && type.ref->a.func_call == FUNC_CDECL) + type.ref->a.func_call = ref->a.func_call; /* use export from prototype */ - if (FUNC_EXPORT(r)) - FUNC_EXPORT(type.ref->r) = 1; + if (ref->a.func_export) + type.ref->a.func_export = 1; /* use static from prototype */ if (sym->type.t & VT_STATIC) type.t = (type.t & ~VT_EXTERN) | VT_STATIC; + /* If the definition has no visibility use the + one from prototype. */ + if (! (type.t & VT_VIS_MASK)) + type.t |= sym->type.t & VT_VIS_MASK; + if (!is_compatible_types(&sym->type, &type)) { func_error1: tcc_error("incompatible types for redefinition of '%s'", get_tok_str(v, NULL)); } + type.ref->a.func_proto = 0; /* if symbol is already defined, then put complete type */ sym->type = type; } else { @@ -5892,14 +6093,16 @@ static int decl0(int l, int is_for_loop_init) if (btype.t & VT_TYPEDEF) { /* save typedefed type */ /* XXX: test storage specifiers ? */ - sym = sym_push(v, &type, INT_ATTR(&ad), 0); + sym = sym_push(v, &type, 0, 0); + sym->a = ad.a; sym->type.t |= VT_TYPEDEF; } else { r = 0; if ((type.t & VT_BTYPE) == VT_FUNC) { /* external function definition */ /* specific case for func_call attribute */ - type.ref->r = INT_ATTR(&ad); + ad.a.func_proto = 1; + type.ref->a = ad.a; } else if (!(type.t & VT_ARRAY)) { /* not lvalue if array */ r |= lvalue_type(type.t); @@ -5916,9 +6119,6 @@ static int decl0(int l, int is_for_loop_init) extern */ sym = external_sym(v, &type, r, asm_label); - if (type.t & VT_WEAK) - weaken_symbol(sym); - if (ad.alias_target) { Section tsec; Elf32_Sym *esym; @@ -5950,7 +6150,7 @@ static int decl0(int l, int is_for_loop_init) } next(); } - ad.aligned = 0; + ad.a.aligned = 0; } } return 0; diff --git a/tccpe.c b/tccpe.c index f7a200c..b972d75 100644 --- a/tccpe.c +++ b/tccpe.c @@ -363,7 +363,7 @@ struct pe_info { static const char *pe_export_name(TCCState *s1, ElfW(Sym) *sym) { const char *name = symtab_section->link->data + sym->st_name; - if (s1->leading_underscore && name[0] == '_' && !(sym->st_other & 2)) + if (s1->leading_underscore && name[0] == '_' && !(sym->st_other & ST_PE_STDCALL)) return name + 1; return name; } @@ -378,7 +378,7 @@ static int pe_find_import(TCCState * s1, ElfW(Sym) *sym) s = pe_export_name(s1, sym); if (n) { /* second try: */ - if (sym->st_other & 2) { + if (sym->st_other & ST_PE_STDCALL) { /* try w/0 stdcall deco (windows API convention) */ p = strrchr(s, '@'); if (!p || s[0] != '_') @@ -663,7 +663,7 @@ static int pe_write(struct pe_info *pe) } } - pstrcpy((char*)psh->Name, sizeof psh->Name, sh_name); + strncpy((char*)psh->Name, sh_name, sizeof psh->Name); psh->Characteristics = pe_sec_flags[si->cls]; psh->VirtualAddress = addr; @@ -819,27 +819,39 @@ static void pe_build_imports(struct pe_info *pe) 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 = 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; - v = pe->thunk->data_offset + rva_base; - section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */ - put_elf_str(pe->thunk, name); + + if (dllref) + v = 0, ordinal = imp_sym->st_value; /* ordinal from pe_load_def */ + else + ordinal = 0, v = imp_sym->st_value; /* address from tcc_add_symbol() */ + #ifdef TCC_IS_NATIVE if (pe->type == PE_RUN) { - v = imp_sym->st_value; if (dllref) { if ( !dllref->handle ) dllref->handle = LoadLibrary(dllref->name); - v = (ADDR3264)GetProcAddress(dllref->handle, name); + v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(LPCSTR)NULL+ordinal:name); } if (!v) - tcc_error_noabort("undefined symbol '%s'", name); - } + tcc_error_noabort("can't build symbol '%s'", name); + } else #endif + if (ordinal) { + v = ordinal | (ADDR3264)1 << (sizeof(ADDR3264)*8 - 1); + } else { + v = pe->thunk->data_offset + rva_base; + section_ptr_add(pe->thunk, sizeof(WORD)); /* hint, not used */ + put_elf_str(pe->thunk, name); + } + } else { v = 0; /* last entry is zero */ } + *(ADDR3264*)(pe->thunk->data+thk_ptr) = *(ADDR3264*)(pe->thunk->data+ent_ptr) = v; thk_ptr += sizeof (ADDR3264); @@ -887,7 +899,7 @@ static void pe_build_exports(struct pe_info *pe) for (sym_index = 1; sym_index < sym_end; ++sym_index) { sym = (ElfW(Sym)*)symtab_section->data + sym_index; name = pe_export_name(pe->s1, sym); - if ((sym->st_other & 1) + if ((sym->st_other & ST_PE_EXPORT) /* export only symbols from actually written sections */ && pe->s1->sections[sym->st_shndx]->sh_addr) { p = tcc_malloc(sizeof *p); @@ -896,9 +908,9 @@ static void pe_build_exports(struct pe_info *pe) dynarray_add((void***)&sorted, &sym_count, p); } #if 0 - if (sym->st_other & 1) + if (sym->st_other & ST_PE_EXPORT) printf("export: %s\n", name); - if (sym->st_other & 2) + if (sym->st_other & ST_PE_STDCALL) printf("stdcall: %s\n", name); #endif } @@ -1270,7 +1282,7 @@ static int pe_check_symbols(struct pe_info *pe) /* patch the original symbol */ sym->st_value = offset; sym->st_shndx = text_section->sh_num; - sym->st_other &= ~1; /* do not export */ + sym->st_other &= ~ST_PE_EXPORT; /* do not export */ continue; } @@ -1289,7 +1301,7 @@ static int pe_check_symbols(struct pe_info *pe) } else if (pe->s1->rdynamic && ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) { /* if -rdynamic option, then export all non local symbols */ - sym->st_other |= 1; + sym->st_other |= ST_PE_EXPORT; } } return ret; @@ -1451,7 +1463,7 @@ ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2) if (!sym->c) put_extern_sym(sym, NULL, 0, 0); esym = &((ElfW(Sym) *)symtab_section->data)[sym->c]; - if (!(esym->st_other & 4)) + if (!(esym->st_other & ST_PE_IMPORT)) return sv; // printf("import %04x %04x %04x %s\n", sv->type.t, sym->type.t, sv->r, get_tok_str(sv->sym->v, NULL)); @@ -1571,30 +1583,20 @@ static char *trimback(char *a, char *e) return a; } -static char *get_line(char *line, int size, int fd) -{ - int n; - for (n = 0; n < size - 1; ) - if (read(fd, line + n, 1) < 1 || line[n++] == '\n') - break; - if (0 == n) - return NULL; - trimback(line, line + n); - return trimfront(line); -} - /* ------------------------------------------------------------- */ static int pe_load_def(TCCState *s1, int fd) { - int state = 0, ret = -1, dllindex = 0; - char line[400], dllname[80], *p; + int state = 0, ret = -1, dllindex = 0, ord; + char line[400], dllname[80], *p, *x; + FILE *fp; - for (;;) { - p = get_line(line, sizeof line, fd); - if (NULL == p) - break; + fp = fdopen(dup(fd), "rb"); + while (fgets(line, sizeof line, fp)) + { + p = trimfront(trimback(line, strchr(line, 0))); if (0 == *p || ';' == *p) continue; + switch (state) { case 0: if (0 != strnicmp(p, "LIBRARY", 7)) @@ -1612,14 +1614,27 @@ static int pe_load_def(TCCState *s1, int fd) case 2: dllindex = add_dllref(s1, dllname); ++state; - + /* fall through */ default: - pe_putimport(s1, dllindex, p, 0); + /* get ordinal and will store in sym->st_value */ + ord = 0; + x = strchr(p, ' '); + if (x) { + *x = 0, x = strrchr(x + 1, '@'); + if (x) { + char *d; + ord = (int)strtol(x + 1, &d, 10); + if (*d) + ord = 0; + } + } + pe_putimport(s1, dllindex, p, ord); continue; } } ret = 0; quit: + fclose(fp); return ret; } @@ -1726,7 +1741,6 @@ ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack) static void pe_add_runtime(TCCState *s1, struct pe_info *pe) { const char *start_symbol; - ADDR3264 addr = 0; int pe_type = 0; if (find_elf_sym(symtab_section, PE_STDSYM("WinMain","@16"))) @@ -1747,14 +1761,11 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) : PE_GUI == pe_type ? "__winstart" : "__start" ; - if (!s1->leading_underscore || strchr(start_symbol, '@')) { + if (!s1->leading_underscore || strchr(start_symbol, '@')) ++start_symbol; - if (start_symbol[0] != '_') - start_symbol = NULL; - } /* grab the startup code from libtcc1 */ - if (start_symbol) + if (TCC_OUTPUT_MEMORY != s1->output_type || PE_GUI == pe_type) add_elf_sym(symtab_section, 0, 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, @@ -1776,21 +1787,16 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) } } - if (TCC_OUTPUT_MEMORY == s1->output_type) + if (TCC_OUTPUT_MEMORY == s1->output_type) { pe_type = PE_RUN; - - if (start_symbol) { - addr = get_elf_sym_addr(s1, start_symbol, 1); - if (PE_RUN == pe_type && addr) - /* for -run GUI's, put '_runwinmain' instead of 'main' */ - add_elf_sym(symtab_section, - addr, 0, - ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, - text_section->sh_num, "main"); +#ifdef TCC_IS_NATIVE + s1->runtime_main = start_symbol; +#endif + } else { + pe->start_addr = (DWORD)(uintptr_t)tcc_get_symbol_err(s1, start_symbol); } pe->type = pe_type; - pe->start_addr = addr; } ST_FUNC int pe_output_file(TCCState * s1, const char *filename) diff --git a/tccpp.c b/tccpp.c index aeaf6be..732c5ea 100644 --- a/tccpp.c +++ b/tccpp.c @@ -58,7 +58,6 @@ static const int *unget_saved_macro_ptr; static int unget_saved_buffer[TOK_MAX_SIZE + 1]; static int unget_buffer_enabled; static TokenSym *hash_ident[TOK_HASH_SIZE]; -static char token_buf[STRING_MAX_SIZE + 1]; /* true if isid(c) || isnum(c) */ static unsigned char isidnum_table[256-CH_EOF]; @@ -70,8 +69,33 @@ static const char tcc_keywords[] = /* WARNING: the content of this string encodes token numbers */ static const unsigned char tok_two_chars[] = +/* outdated -- gr "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253" "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266"; +*/{ + '<','=', TOK_LE, + '>','=', TOK_GE, + '!','=', TOK_NE, + '&','&', TOK_LAND, + '|','|', TOK_LOR, + '+','+', TOK_INC, + '-','-', TOK_DEC, + '=','=', TOK_EQ, + '<','<', TOK_SHL, + '>','>', TOK_SAR, + '+','=', TOK_A_ADD, + '-','=', TOK_A_SUB, + '*','=', TOK_A_MUL, + '/','=', TOK_A_DIV, + '%','=', TOK_A_MOD, + '&','=', TOK_A_AND, + '^','=', TOK_A_XOR, + '|','=', TOK_A_OR, + '-','>', TOK_ARROW, + '.','.', 0xa8, // C++ token ? + '#','#', TOK_TWOSHARPS, + 0 +}; struct macro_level { struct macro_level *prev; @@ -197,7 +221,7 @@ static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len) int i; if (tok_ident >= SYM_FIRST_ANOM) - tcc_error("memory full"); + tcc_error("memory full (symbols)"); /* expand token table if needed */ i = tok_ident - TOK_IDENT; @@ -264,6 +288,10 @@ ST_FUNC char *get_tok_str(int v, CValue *cv) cstr_buf.size_allocated = sizeof(buf); p = buf; +/* just an explanation, should never happen: + if (v <= TOK_LINENUM && v >= TOK_CINT && cv == NULL) + tcc_error("internal error: get_tok_str"); */ + switch(v) { case TOK_CINT: case TOK_CUINT: @@ -276,7 +304,7 @@ ST_FUNC char *get_tok_str(int v, CValue *cv) #ifdef _WIN32 sprintf(p, "%u", (unsigned)cv->ull); #else - sprintf(p, "%Lu", cv->ull); + sprintf(p, "%llu", cv->ull); #endif break; case TOK_LCHAR: @@ -311,6 +339,15 @@ ST_FUNC char *get_tok_str(int v, CValue *cv) cstr_ccat(&cstr_buf, '\"'); cstr_ccat(&cstr_buf, '\0'); break; + + case TOK_CFLOAT: + case TOK_CDOUBLE: + case TOK_CLDOUBLE: + case TOK_LINENUM: + return NULL; /* should not happen */ + + /* above tokens have value, the ones below don't */ + case TOK_LT: v = '<'; goto addv; @@ -1175,9 +1212,9 @@ static void tok_print(int *str) ST_FUNC void parse_define(void) { Sym *s, *first, **ps; - int v, t, varg, is_vaargs, spc; + int v, t, varg, is_vaargs, spc, ptok, macro_list_start; TokenString str; - + v = tok; if (v < TOK_IDENT) tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc)); @@ -1216,8 +1253,13 @@ ST_FUNC void parse_define(void) tok_str_new(&str); spc = 2; /* EOF testing necessary for '-D' handling */ + ptok = 0; + macro_list_start = 1; while (tok != TOK_LINEFEED && tok != TOK_EOF) { - /* remove spaces around ## and after '#' */ + if (!macro_list_start && spc == 2 && tok == TOK_TWOSHARPS) + tcc_error("'##' invalid at start of macro"); + ptok = tok; + /* remove spaces around ## and after '#' */ if (TOK_TWOSHARPS == tok) { if (1 == spc) --str.len; @@ -1230,7 +1272,10 @@ ST_FUNC void parse_define(void) tok_str_add2(&str, tok, &tokc); skip: next_nomacro_spc(); + macro_list_start = 0; } + if (ptok == TOK_TWOSHARPS) + tcc_error("'##' invalid at end of macro"); if (spc == 1) --str.len; /* remove trailing space */ tok_str_add(&str, 0); @@ -1247,7 +1292,7 @@ static inline int hash_cached_include(const char *filename) unsigned int h; h = TOK_HASH_INIT; - s = filename; + s = (unsigned char *) filename; while (*s) { h = TOK_HASH_FUNC(h, *s); s++; @@ -1528,7 +1573,7 @@ include_done: c = (define_find(tok) != 0) ^ c; do_if: if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE) - tcc_error("memory full"); + tcc_error("memory full (ifdef)"); *s1->ifdef_stack_ptr++ = c; goto test_skip; case TOK_ELSE: @@ -1744,261 +1789,156 @@ static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long cstr_wccat(outstr, '\0'); } -/* we use 64 bit numbers */ -#define BN_SIZE 2 - -/* bn = (bn << shift) | or_val */ -static void bn_lshift(unsigned int *bn, int shift, int or_val) -{ - int i; - unsigned int v; - for(i=0;i> (32 - shift); - } -} - -static void bn_zero(unsigned int *bn) -{ - int i; - for(i=0;i= 'a' && ch <= 'f') - t = ch - 'a' + 10; - else if (ch >= 'A' && ch <= 'F') - t = ch - 'A' + 10; - else if (isnum(ch)) - t = ch - '0'; - else - break; - if (t >= b) - break; - if (q >= token_buf + STRING_MAX_SIZE) { - num_too_long: - tcc_error("number too long"); - } - *q++ = ch; - ch = *p++; - } - if (ch == '.' || - ((ch == 'e' || ch == 'E') && b == 10) || - ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) { - if (b != 10) { - /* NOTE: strtox should support that for hexa numbers, but - non ISOC99 libcs do not support it, so we prefer to do - it by hand */ - /* hexadecimal or binary floats */ - /* XXX: handle overflows */ - *q = '\0'; - if (b == 16) - shift = 4; - else - shift = 2; - bn_zero(bn); - q = token_buf; - while (1) { - t = *q++; - if (t == '\0') { - break; - } else if (t >= 'a') { - t = t - 'a' + 10; - } else if (t >= 'A') { - t = t - 'A' + 10; - } else { - t = t - '0'; - } - bn_lshift(bn, shift, t); - } - frac_bits = 0; - if (ch == '.') { - ch = *p++; - while (1) { - t = ch; - if (t >= 'a' && t <= 'f') { - t = t - 'a' + 10; - } else if (t >= 'A' && t <= 'F') { - t = t - 'A' + 10; - } else if (t >= '0' && t <= '9') { - t = t - '0'; - } else { - break; - } - if (t >= b) - tcc_error("invalid digit"); - bn_lshift(bn, shift, t); - frac_bits += shift; - ch = *p++; - } - } - if (ch != 'p' && ch != 'P') - expect("exponent"); - ch = *p++; - s = 1; - exp_val = 0; - if (ch == '+') { - ch = *p++; - } else if (ch == '-') { - s = -1; - ch = *p++; - } - if (ch < '0' || ch > '9') - expect("exponent digits"); - while (ch >= '0' && ch <= '9') { - exp_val = exp_val * 10 + ch - '0'; - ch = *p++; - } - exp_val = exp_val * s; - - /* now we can generate the number */ - /* XXX: should patch directly float number */ - d = (double)bn[1] * 4294967296.0 + (double)bn[0]; - d = ldexp(d, exp_val - frac_bits); - t = toup(ch); - if (t == 'F') { - ch = *p++; - tok = TOK_CFLOAT; - /* float : should handle overflow */ - tokc.f = (float)d; - } else if (t == 'L') { - ch = *p++; -#ifdef TCC_TARGET_PE - tok = TOK_CDOUBLE; - tokc.d = d; -#else - tok = TOK_CLDOUBLE; - /* XXX: not large enough */ - tokc.ld = (long double)d; -#endif - } else { - tok = TOK_CDOUBLE; - tokc.d = d; - } - } else { - /* decimal floats */ - if (ch == '.') { - if (q >= token_buf + STRING_MAX_SIZE) - goto num_too_long; - *q++ = ch; - ch = *p++; - float_frac_parse: - while (ch >= '0' && ch <= '9') { - if (q >= token_buf + STRING_MAX_SIZE) - goto num_too_long; - *q++ = ch; - ch = *p++; - } - } - if (ch == 'e' || ch == 'E') { - if (q >= token_buf + STRING_MAX_SIZE) - goto num_too_long; - *q++ = ch; - ch = *p++; - if (ch == '-' || ch == '+') { - if (q >= token_buf + STRING_MAX_SIZE) - goto num_too_long; - *q++ = ch; - ch = *p++; - } - if (ch < '0' || ch > '9') - expect("exponent digits"); - while (ch >= '0' && ch <= '9') { - if (q >= token_buf + STRING_MAX_SIZE) - goto num_too_long; - *q++ = ch; - ch = *p++; - } - } - *q = '\0'; - t = toup(ch); - errno = 0; - if (t == 'F') { - ch = *p++; - tok = TOK_CFLOAT; - tokc.f = strtof(token_buf, NULL); - } else if (t == 'L') { - ch = *p++; -#ifdef TCC_TARGET_PE - tok = TOK_CDOUBLE; - tokc.d = strtod(token_buf, NULL); -#else - tok = TOK_CLDOUBLE; - tokc.ld = strtold(token_buf, NULL); -#endif - } else { - tok = TOK_CDOUBLE; - tokc.d = strtod(token_buf, NULL); - } - } - } else { - unsigned long long n, n1; - int lcount, ucount; + c = *p++; + }else{ + --p; + } + }else + --p; + if(strchr(p , '.') || (b == 10 && (strchr(p,'e') || strchr(p,'E'))) || + ((b == 2 || b == 16)&& (strchr(p,'p') || strchr(p,'P')))){ + long double ld, sh, fb; + int exp; + /* NOTE: strtox should support that for hexa numbers, but + non ISOC99 libcs do not support it, so we prefer to do + it by hand */ + /* hexadecimal or binary floats */ + /* XXX: handle overflows */ +float_frac_parse: + fb = 1.0L/b; + sh = b; + ld = 0.0; - /* integer number */ - *q = '\0'; - q = token_buf; - if (b == 10 && *q == '0') { + while(1){ + if (c == '\0') + break; + if (c >= 'a' && c <= 'f') + t = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + t = c - 'A' + 10; + else if(isnum(c)) + t = c - '0'; + else + break; + if (t >= b) + tcc_error("invalid digit"); + ld = ld * b + t; + c = *p++; + } + if (c == '.'){ + c = *p++; + sh = fb; + while (1){ + if (c == '\0') + break; + if (c >= 'a' && c <= 'f') + t = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + t = c - 'A' + 10; + else if (isnum(c)) + t =c - '0'; + else + break; + if (t >= b){ + if(b == 10 && (c == 'e' || c == 'E' || c == 'f' || c == 'F')) + break; + tcc_error("invalid digit"); + } + ld += sh*t; + sh*=fb; + c = *p++; + } + } + if ((b == 16 || b == 2) && c != 'p' && c != 'P') + expect("exponent"); + if(((c == 'e' || c == 'E') && b == 10) || + ((c == 'p' || c == 'P') && (b == 16 || b == 2))){ + c = *p++; + if(c == '+' || c == '-'){ + if (c == '-') + sh = fb; + c = *p++; + }else + sh = b; + if (!isnum(c)) + expect("exponent digits"); + exp = 0; + do{ + exp = exp * 10 + c - '0'; + c = *p++; + }while(isnum(c)); + while (exp != 0){ + if (exp & 1) + ld *= sh; + exp >>= 1; + sh *= sh; + } + } + t = toup(c); + if (t == 'F') { + c = *p++; + tok = TOK_CFLOAT; + tokc.f = (float)ld; + } else if (t == 'L') { + c = *p++; +#ifdef TCC_TARGET_PE + tok = TOK_CDOUBLE; + tokc.d = (double)ld; +#else + tok = TOK_CLDOUBLE; + tokc.ld = ld; +#endif + } else { + tok = TOK_CDOUBLE; + tokc.d = (double)ld; + } + } else { + uint64_t n = 0, n1; + int warn = 1; + int lcount, ucount; + if (b == 10 && c == '0') { b = 8; - q++; } - n = 0; - while(1) { - t = *q++; - /* no need for checks except for base 10 / 8 errors */ - if (t == '\0') { - break; - } else if (t >= 'a') { - t = t - 'a' + 10; - } else if (t >= 'A') { - t = t - 'A' + 10; - } else { - t = t - '0'; - if (t >= b) - tcc_error("invalid digit"); - } - n1 = n; - n = n * b + t; - /* detect overflow */ - /* XXX: this test is not reliable */ - if (n < n1) - tcc_error("integer constant overflow"); - } - + while(1){ + if (c == '\0') + break; + if (c >= 'a' && c <= 'f') + t = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + t = c - 'A' + 10; + else if(isnum(c)) + t = c - '0'; + else + break; + if (t >= b) + tcc_error("invalid digit"); + n1 = n; + n = n * b + t; + if (n < n1 && warn){ + tcc_warning("integer constant overflow"); + warn = 0; + } + c = *p++; + } /* XXX: not exactly ANSI compliant */ if ((n & 0xffffffff00000000LL) != 0) { if ((n >> 63) != 0) @@ -2013,7 +1953,7 @@ static void parse_number(const char *p) lcount = 0; ucount = 0; for(;;) { - t = toup(ch); + t = toup(c); if (t == 'L') { if (lcount >= 2) tcc_error("three 'l's in integer constant"); @@ -2028,7 +1968,7 @@ static void parse_number(const char *p) #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE } #endif - ch = *p++; + c = *p++; } else if (t == 'U') { if (ucount >= 1) tcc_error("two 'u's in integer constant"); @@ -2037,7 +1977,7 @@ static void parse_number(const char *p) tok = TOK_CUINT; else if (tok == TOK_CLLONG) tok = TOK_CULLONG; - ch = *p++; + c = *p++; } else { break; } @@ -2047,7 +1987,7 @@ static void parse_number(const char *p) else tokc.ull = n; } - if (ch) + if (c) tcc_error("invalid number\n"); } @@ -2222,7 +2162,7 @@ maybe_newline: goto token_found; pts = &(ts->hash_next); } - ts = tok_alloc_new(pts, p1, len); + ts = tok_alloc_new(pts, (char *) p1, len); token_found: ; } else { /* slower case */ @@ -2519,7 +2459,8 @@ ST_FUNC void next_nomacro(void) } while (is_space(tok)); } -/* substitute args in macro_str and return allocated string */ +/* substitute arguments in replacement lists in macro_str by the values in + args (field d) and return allocated string */ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) { int last_tok, t, spc; @@ -2576,7 +2517,7 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) if (gnu_ext && s->type.t && last_tok == TOK_TWOSHARPS && str.len >= 2 && str.str[str.len - 2] == ',') { - if (*st == 0) { + if (*st == TOK_PLCHLDR) { /* suppress ',' '##' */ str.len -= 2; } else { @@ -2747,6 +2688,8 @@ static int macro_subst_tok(TokenString *tok_str, tok_str_add2(&str, tok, &tokc); next_nomacro_spc(); } + if (!str.len) + tok_str_add(&str, TOK_PLCHLDR); str.len -= spc; tok_str_add(&str, 0); sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0); @@ -2839,9 +2782,11 @@ static inline int *macro_twosharps(const int *macro_str) TOK_GET(&t, &ptr, &cval); /* We concatenate the two tokens */ cstr_new(&cstr); - cstr_cat(&cstr, get_tok_str(tok, &tokc)); + if (tok != TOK_PLCHLDR) + cstr_cat(&cstr, get_tok_str(tok, &tokc)); n = cstr.size; - cstr_cat(&cstr, get_tok_str(t, &cval)); + if (t != TOK_PLCHLDR || tok == TOK_PLCHLDR) + cstr_cat(&cstr, get_tok_str(t, &cval)); cstr_ccat(&cstr, '\0'); tcc_open_bf(tcc_state, ":paste:", cstr.size); @@ -2858,8 +2803,11 @@ static inline int *macro_twosharps(const int *macro_str) cstr_free(&cstr); } } - if (tok != TOK_NOSUBST) + if (tok != TOK_NOSUBST) { + tok_str_add2(¯o_str1, tok, &tokc); + tok = ' '; start_of_nosubsts = -1; + } tok_str_add2(¯o_str1, tok, &tokc); } tok_str_add(¯o_str1, 0); diff --git a/tccrun.c b/tccrun.c index d858ae6..13c2012 100644 --- a/tccrun.c +++ b/tccrun.c @@ -97,7 +97,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0) return -1; - prog_main = tcc_get_symbol_err(s1, "main"); + prog_main = tcc_get_symbol_err(s1, s1->runtime_main); #ifdef CONFIG_TCC_BACKTRACE if (s1->do_debug) { @@ -110,13 +110,30 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) if (s1->do_bounds_check) { void (*bound_init)(void); void (*bound_exit)(void); + void (*bound_new_region)(void *p, unsigned long size); + int (*bound_delete_region)(void *p); + int i; + /* set error function */ rt_bound_error_msg = tcc_get_symbol_err(s1, "__bound_error_msg"); /* XXX: use .init section so that it also work in binary ? */ bound_init = tcc_get_symbol_err(s1, "__bound_init"); bound_exit = tcc_get_symbol_err(s1, "__bound_exit"); + bound_new_region = tcc_get_symbol_err(s1, "__bound_new_region"); + bound_delete_region = tcc_get_symbol_err(s1, "__bound_delete_region"); bound_init(); + /* mark argv area as valid */ + bound_new_region(argv, argc*sizeof(argv[0])); + for (i=0; inb_errors) return -1; -#ifdef TCC_HAS_RUNTIME_PLTGOT - s1->runtime_plt_and_got_offset = 0; - s1->runtime_plt_and_got = (char *)(mem + offset); - /* double the size of the buffer for got and plt entries - XXX: calculate exact size for them? */ - offset *= 2; -#endif - if (0 == mem) return offset; @@ -180,6 +189,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) if (s->reloc) relocate_section(s1, s); } + relocate_plt(s1); for(i = 1; i < s1->nb_sections; i++) { s = s1->sections[i]; @@ -197,11 +207,6 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) set_pages_executable(ptr, length); } -#ifdef TCC_HAS_RUNTIME_PLTGOT - set_pages_executable(s1->runtime_plt_and_got, - s1->runtime_plt_and_got_offset); -#endif - #ifdef _WIN64 win64_add_function_table(s1); #endif @@ -217,6 +222,7 @@ static void set_pages_executable(void *ptr, unsigned long length) unsigned long old_protect; VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect); #else + extern void __clear_cache(char *beginning, char *end); #ifndef PAGESIZE # define PAGESIZE 4096 #endif @@ -254,7 +260,7 @@ static addr_t rt_printline(addr_t wanted_pc, const char *msg) if (stab_section) { stab_len = stab_section->data_offset; stab_sym = (Stab_Sym *)stab_section->data; - stab_str = stabstr_section->data; + stab_str = (char *) stabstr_section->data; } func_name[0] = '\0'; @@ -347,7 +353,7 @@ no_stabs: if (wanted_pc >= sym->st_value && wanted_pc < sym->st_value + sym->st_size) { pstrcpy(last_func_name, sizeof(last_func_name), - strtab_section->data + sym->st_name); + (char *) strtab_section->data + sym->st_name); func_addr = sym->st_value; goto found; } @@ -480,10 +486,12 @@ static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) if (level == 0) { #if defined(__APPLE__) *paddr = uc->uc_mcontext->__ss.__eip; -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) *paddr = uc->uc_mcontext.mc_eip; #elif defined(__dietlibc__) *paddr = uc->uc_mcontext.eip; +#elif defined(__NetBSD__) + *paddr = uc->uc_mcontext.__gregs[_REG_EIP]; #else *paddr = uc->uc_mcontext.gregs[REG_EIP]; #endif @@ -491,10 +499,12 @@ static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) } else { #if defined(__APPLE__) fp = uc->uc_mcontext->__ss.__ebp; -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) fp = uc->uc_mcontext.mc_ebp; #elif defined(__dietlibc__) fp = uc->uc_mcontext.ebp; +#elif defined(__NetBSD__) + fp = uc->uc_mcontext.__gregs[_REG_EBP]; #else fp = uc->uc_mcontext.gregs[REG_EBP]; #endif @@ -522,8 +532,10 @@ static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) /* XXX: only support linux */ #if defined(__APPLE__) *paddr = uc->uc_mcontext->__ss.__rip; -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) *paddr = uc->uc_mcontext.mc_rip; +#elif defined(__NetBSD__) + *paddr = uc->uc_mcontext.__gregs[_REG_RIP]; #else *paddr = uc->uc_mcontext.gregs[REG_RIP]; #endif @@ -531,8 +543,10 @@ static int rt_get_caller_pc(addr_t *paddr, ucontext_t *uc, int level) } else { #if defined(__APPLE__) fp = uc->uc_mcontext->__ss.__rbp; -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) fp = uc->uc_mcontext.mc_rbp; +#elif defined(__NetBSD__) + fp = uc->uc_mcontext.__gregs[_REG_RBP]; #else fp = uc->uc_mcontext.gregs[REG_RBP]; #endif diff --git a/tcctok.h b/tcctok.h index fde13dd..735ccdd 100644 --- a/tcctok.h +++ b/tcctok.h @@ -121,6 +121,8 @@ DEF(TOK_DLLIMPORT, "dllimport") DEF(TOK_NORETURN1, "noreturn") DEF(TOK_NORETURN2, "__noreturn__") + DEF(TOK_VISIBILITY1, "visibility") + DEF(TOK_VISIBILITY2, "__visibility__") DEF(TOK_builtin_types_compatible_p, "__builtin_types_compatible_p") DEF(TOK_builtin_constant_p, "__builtin_constant_p") DEF(TOK_builtin_frame_address, "__builtin_frame_address") @@ -143,23 +145,34 @@ #endif /* builtin functions or variables */ -#ifdef TCC_ARM_EABI - DEF(TOK_memcpy, "__aeabi_memcpy") - DEF(TOK_memcpy4, "__aeabi_memcpy4") - DEF(TOK_memcpy8, "__aeabi_memcpy8") - DEF(TOK_memset, "__aeabi_memset") - DEF(TOK___aeabi_ldivmod, "__aeabi_ldivmod") - DEF(TOK___aeabi_uldivmod, "__aeabi_uldivmod") -#else +#ifndef TCC_ARM_EABI DEF(TOK_memcpy, "memcpy") DEF(TOK_memset, "memset") DEF(TOK___divdi3, "__divdi3") DEF(TOK___moddi3, "__moddi3") DEF(TOK___udivdi3, "__udivdi3") DEF(TOK___umoddi3, "__umoddi3") + DEF(TOK___ashrdi3, "__ashrdi3") + DEF(TOK___lshrdi3, "__lshrdi3") + DEF(TOK___ashldi3, "__ashldi3") + DEF(TOK___floatundisf, "__floatundisf") + DEF(TOK___floatundidf, "__floatundidf") +# ifndef TCC_ARM_VFP + DEF(TOK___floatundixf, "__floatundixf") + DEF(TOK___fixunsxfdi, "__fixunsxfdi") +# endif + DEF(TOK___fixunssfdi, "__fixunssfdi") + DEF(TOK___fixunsdfdi, "__fixunsdfdi") #endif -#if defined(TCC_TARGET_ARM) -#ifdef TCC_ARM_EABI + +#if defined TCC_TARGET_ARM +# ifdef TCC_ARM_EABI + DEF(TOK_memcpy, "__aeabi_memcpy") + DEF(TOK_memcpy4, "__aeabi_memcpy4") + DEF(TOK_memcpy8, "__aeabi_memcpy8") + DEF(TOK_memset, "__aeabi_memset") + DEF(TOK___aeabi_ldivmod, "__aeabi_ldivmod") + DEF(TOK___aeabi_uldivmod, "__aeabi_uldivmod") DEF(TOK___aeabi_idivmod, "__aeabi_idivmod") DEF(TOK___aeabi_uidivmod, "__aeabi_uidivmod") DEF(TOK___divsi3, "__aeabi_idiv") @@ -168,36 +181,6 @@ DEF(TOK___floatdidf, "__aeabi_l2d") DEF(TOK___fixsfdi, "__aeabi_f2lz") DEF(TOK___fixdfdi, "__aeabi_d2lz") -#else - DEF(TOK___modsi3, "__modsi3") - DEF(TOK___umodsi3, "__umodsi3") - DEF(TOK___divsi3, "__divsi3") - DEF(TOK___udivsi3, "__udivsi3") - DEF(TOK___floatdisf, "__floatdisf") - DEF(TOK___floatdidf, "__floatdidf") -#ifndef TCC_ARM_VFP - DEF(TOK___floatdixf, "__floatdixf") - DEF(TOK___fixunssfsi, "__fixunssfsi") - DEF(TOK___fixunsdfsi, "__fixunsdfsi") - DEF(TOK___fixunsxfsi, "__fixunsxfsi") - DEF(TOK___fixxfdi, "__fixxfdi") -#endif - DEF(TOK___fixsfdi, "__fixsfdi") - DEF(TOK___fixdfdi, "__fixdfdi") -#endif -#elif defined(TCC_TARGET_C67) - DEF(TOK__divi, "_divi") - DEF(TOK__divu, "_divu") - DEF(TOK__divf, "_divf") - DEF(TOK__divd, "_divd") - DEF(TOK__remi, "_remi") - DEF(TOK__remu, "_remu") -#endif -#ifdef TCC_TARGET_I386 - DEF(TOK___tcc_int_fpu_control, "__tcc_int_fpu_control") - DEF(TOK___tcc_fpu_control, "__tcc_fpu_control") -#endif -#ifdef TCC_ARM_EABI DEF(TOK___ashrdi3, "__aeabi_lasr") DEF(TOK___lshrdi3, "__aeabi_llsr") DEF(TOK___ashldi3, "__aeabi_llsl") @@ -205,20 +188,45 @@ DEF(TOK___floatundidf, "__aeabi_ul2d") DEF(TOK___fixunssfdi, "__aeabi_f2ulz") DEF(TOK___fixunsdfdi, "__aeabi_d2ulz") -#else - DEF(TOK___ashrdi3, "__ashrdi3") - DEF(TOK___lshrdi3, "__lshrdi3") - DEF(TOK___ashldi3, "__ashldi3") - DEF(TOK___floatundisf, "__floatundisf") - DEF(TOK___floatundidf, "__floatundidf") -#ifndef TCC_ARM_VFP - DEF(TOK___floatundixf, "__floatundixf") - DEF(TOK___fixunsxfdi, "__fixunsxfdi") +# else + DEF(TOK___modsi3, "__modsi3") + DEF(TOK___umodsi3, "__umodsi3") + DEF(TOK___divsi3, "__divsi3") + DEF(TOK___udivsi3, "__udivsi3") + DEF(TOK___floatdisf, "__floatdisf") + DEF(TOK___floatdidf, "__floatdidf") +# ifndef TCC_ARM_VFP + DEF(TOK___floatdixf, "__floatdixf") + DEF(TOK___fixunssfsi, "__fixunssfsi") + DEF(TOK___fixunsdfsi, "__fixunsdfsi") + DEF(TOK___fixunsxfsi, "__fixunsxfsi") + DEF(TOK___fixxfdi, "__fixxfdi") +# endif + DEF(TOK___fixsfdi, "__fixsfdi") + DEF(TOK___fixdfdi, "__fixdfdi") +# endif #endif - DEF(TOK___fixunssfdi, "__fixunssfdi") - DEF(TOK___fixunsdfdi, "__fixunsdfdi") + +#if defined TCC_TARGET_C67 + DEF(TOK__divi, "_divi") + DEF(TOK__divu, "_divu") + DEF(TOK__divf, "_divf") + DEF(TOK__divd, "_divd") + DEF(TOK__remi, "_remi") + DEF(TOK__remu, "_remu") #endif -#ifdef TCC_TARGET_PE + +#if defined TCC_TARGET_I386 + DEF(TOK___fixsfdi, "__fixsfdi") + DEF(TOK___fixdfdi, "__fixdfdi") + DEF(TOK___fixxfdi, "__fixxfdi") +#endif + +#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 + DEF(TOK_alloca, "alloca") +#endif + +#if defined TCC_TARGET_PE DEF(TOK___chkstk, "__chkstk") #endif @@ -231,22 +239,20 @@ DEF(TOK___bound_ptr_indir8, "__bound_ptr_indir8") DEF(TOK___bound_ptr_indir12, "__bound_ptr_indir12") DEF(TOK___bound_ptr_indir16, "__bound_ptr_indir16") + DEF(TOK___bound_main_arg, "__bound_main_arg") DEF(TOK___bound_local_new, "__bound_local_new") DEF(TOK___bound_local_delete, "__bound_local_delete") -#ifdef TCC_TARGET_PE +# ifdef TCC_TARGET_PE DEF(TOK_malloc, "malloc") DEF(TOK_free, "free") DEF(TOK_realloc, "realloc") DEF(TOK_memalign, "memalign") DEF(TOK_calloc, "calloc") -#endif +# endif DEF(TOK_memmove, "memmove") DEF(TOK_strlen, "strlen") DEF(TOK_strcpy, "strcpy") #endif -#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 - DEF(TOK_alloca, "alloca") -#endif /* Tiny Assembler */ DEF_ASM(byte) @@ -260,6 +266,7 @@ DEF_ASM(file) DEF_ASM(globl) DEF_ASM(global) + DEF_ASM(hidden) DEF_ASM(ident) DEF_ASM(size) DEF_ASM(type) diff --git a/tests/Makefile b/tests/Makefile index 08dfa42..e3824ba 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -5,7 +5,7 @@ TOP = .. include $(TOP)/Makefile SRCDIR = $(top_srcdir)/tests -VPATH = $(SRCDIR) +VPATH = $(SRCDIR) $(top_srcdir) # what tests to run TESTS = \ @@ -13,24 +13,30 @@ TESTS = \ hello-run \ libtest \ test3 \ + $(BTESTS) \ abitest \ vla_test-run \ moretests +BTESTS = test1b test3b btest + +ifdef CONFIG_CROSS + TESTS += hello-cross +endif + # test4 -- problem with -static # asmtest -- minor differences with gcc # btest -- works on i386 (including win32) -# test3 -- win32 does not know how to printf long doubles # bounds-checking is supported only on i386 ifneq ($(ARCH),i386) - TESTS := $(filter-out btest,$(TESTS)) + TESTS := $(filter-out $(BTESTS),$(TESTS)) endif ifdef CONFIG_WIN32 - TESTS := $(filter-out test3,$(TESTS)) + TESTS := w32-prep $(filter-out $(BTESTS),$(TESTS)) endif ifeq ($(TARGETOS),Darwin) - TESTS := $(filter-out hello-exe test3 btest,$(TESTS)) + TESTS := $(filter-out hello-exe test3 $(BTESTS),$(TESTS)) endif ifeq ($(ARCH),i386) else ifneq ($(ARCH),x86-64) @@ -52,6 +58,7 @@ TCCFLAGS = -B$(TOP) -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include ifdef CONFIG_WIN32 TCCFLAGS = -B$(top_srcdir)/win32 -I$(top_srcdir) -I$(top_srcdir)/include -I$(TOP) -L$(TOP) endif +XTCCFLAGS = -B$(TOP) -B$(top_srcdir)/win32 -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include TCC = $(TOP)/tcc $(TCCFLAGS) RUN_TCC = $(NATIVE_DEFINES) -DONE_SOURCE -run $(top_srcdir)/tcc.c $(TCCFLAGS) @@ -69,6 +76,15 @@ hello-exe: ../examples/ex1.c @echo ------------ $@ ------------ $(TCC) $< -o hello$(EXESUF) || ($(TOP)/tcc -vv; exit 1) && ./hello$(EXESUF) +hello-cross: ../examples/ex1.c + @echo ------------ $@ ------------ + for XTCC in $(PROGS_CROSS) ; \ + do echo -n "Test of $$XTCC... "; \ + out=$$($(TOP)/$$XTCC $(XTCCFLAGS) -c $< 2>&1); \ + test $$? -ne 0 && { echo "Failed\n$$out\n" ; $(TOP)/$$XTCC -vv; exit 1; } ; \ + echo "Success"; \ + done + hello-run: ../examples/ex1.c @echo ------------ $@ ------------ $(TCC) -run $< @@ -84,48 +100,52 @@ moretests: @echo ------------ $@ ------------ $(MAKE) -C tests2 -# test.ref - generate using gcc -# copy only tcclib.h so GCC's stddef and stdarg will be used +w32-prep: + cp ../libtcc1.a ../lib + +# test.ref - generate using cc test.ref: tcctest.c - gcc -o tcctest.gcc $< -I$(top_srcdir) $(CPPFLAGS) -w $(CFLAGS) $(NATIVE_DEFINES) -std=gnu99 -O0 -fno-omit-frame-pointer $(LDFLAGS) - ./tcctest.gcc > $@ + $(CC) -o tcctest.cc $< -I$(top_srcdir) $(CPPFLAGS) -w $(CFLAGS) $(NATIVE_DEFINES) -std=gnu99 -O0 -fno-omit-frame-pointer $(LDFLAGS) + ./tcctest.cc > $@ # auto test -test1: test.ref +test1 test1b: tcctest.c test.ref @echo ------------ $@ ------------ - $(TCC) -run $(SRCDIR)/tcctest.c > test.out1 - @if diff -u test.ref test.out1 ; then echo "Auto Test OK"; fi + $(TCC) -run $< > test.out1 + @diff -u test.ref test.out1 && echo "Auto Test OK" # iterated test2 (compile tcc then compile tcctest.c !) -test2: test.ref +test2 test2b: tcctest.c test.ref @echo ------------ $@ ------------ - $(TCC) $(RUN_TCC) $(RUN_TCC) -run $(SRCDIR)/tcctest.c > test.out2 - @if diff -u test.ref test.out2 ; then echo "Auto Test2 OK"; fi + $(TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out2 + @diff -u test.ref test.out2 && echo "Auto Test2 OK" # iterated test3 (compile tcc then compile tcc then compile tcctest.c !) -test3: test.ref +test3 test3b: tcctest.c test.ref @echo ------------ $@ ------------ - $(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -run $(SRCDIR)/tcctest.c > test.out3 - @if diff -u test.ref test.out3 ; then echo "Auto Test3 OK"; fi + $(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out3 + @diff -u test.ref test.out3 && echo "Auto Test3 OK" + +test%b : TCCFLAGS += -b # binary output test -test4: test.ref +test4: tcctest.c test.ref @echo ------------ $@ ------------ # object + link output - $(TCC) -c -o tcctest3.o $(SRCDIR)/tcctest.c + $(TCC) -c -o tcctest3.o $< $(TCC) -o tcctest3 tcctest3.o ./tcctest3 > test3.out @if diff -u test.ref test3.out ; then echo "Object Auto Test OK"; fi # dynamic output - $(TCC) -o tcctest1 $(SRCDIR)/tcctest.c + $(TCC) -o tcctest1 $< ./tcctest1 > test1.out @if diff -u test.ref test1.out ; then echo "Dynamic Auto Test OK"; fi # dynamic output + bound check - $(TCC) -b -o tcctest4 $(SRCDIR)/tcctest.c + $(TCC) -b -o tcctest4 $< ./tcctest4 > test4.out @if diff -u test.ref test4.out ; then echo "BCheck Auto Test OK"; fi # static output - $(TCC) -static -o tcctest2 $(SRCDIR)/tcctest.c + $(TCC) -static -o tcctest2 $< ./tcctest2 > test2.out @if diff -u test.ref test2.out ; then echo "Static Auto Test OK"; fi @@ -137,15 +157,15 @@ btest: boundtest.c @echo ------------ $@ ------------ @for i in $(BOUNDS_OK); do \ echo ; echo --- boundtest $$i ---; \ - if $(TCC) -b -run boundtest.c $$i ; then \ - echo succeded as expected; \ + if $(TCC) -b -run $< $$i ; then \ + echo succeeded as expected; \ else\ echo Failed positive test $$i ; exit 1 ; \ fi ;\ done ;\ for i in $(BOUNDS_FAIL); do \ echo ; echo --- boundtest $$i ---; \ - if $(TCC) -b -run boundtest.c $$i ; then \ + if $(TCC) -b -run $< $$i ; then \ echo Failed negative test $$i ; exit 1 ;\ else\ echo failed as expected; \ @@ -161,12 +181,12 @@ speedtest: ex2 ex3 time ./ex3 35 time $(TCC) -run $(top_srcdir)/examples/ex3.c 35 -weaktest: test.ref - $(TCC) -c tcctest.c -o weaktest.tcc.o $(CPPFLAGS) $(CFLAGS) - $(CC) -c tcctest.c -o weaktest.gcc.o -I. $(CPPFLAGS) -w $(CFLAGS) +weaktest: tcctest.c test.ref + $(TCC) -c $< -o weaktest.tcc.o $(CPPFLAGS) $(CFLAGS) + $(CC) -c $< -o weaktest.cc.o -I. $(CPPFLAGS) -w $(CFLAGS) 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" + objdump -t weaktest.cc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.cc.o.txt + diff weaktest.cc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK" ex%: $(top_srcdir)/examples/ex%.c $(CC) -o $@ $< $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) @@ -187,8 +207,8 @@ asmtest: asmtest.ref abitest-cc$(EXESUF): abitest.c $(top_builddir)/$(LIBTCC) $(CC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(NATIVE_DEFINES) $(LIBS) $(LINK_LIBTCC) $(LDFLAGS) -I$(top_srcdir) -abitest-tcc$(EXESUF): abitest.c $(top_builddir)/$(LIBTCC) - $(TCC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(NATIVE_DEFINES) $(LIBS) $(LINK_LIBTCC) $(LDFLAGS) -I$(top_srcdir) +abitest-tcc$(EXESUF): abitest.c libtcc.c + $(TCC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(NATIVE_DEFINES) -DONE_SOURCE $(LIBS) $(LDFLAGS) -I$(top_srcdir) abitest: abitest-cc$(EXESUF) abitest-tcc$(EXESUF) @echo ------------ $@ ------------ @@ -219,8 +239,7 @@ cache: tcc_g # clean clean: $(MAKE) -C tests2 $@ - rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.gcc *.exe \ - hello libtcc_test tcctest[1234] ex? tcc_g tcclib.h - -Makefile: $(SRCDIR)/Makefile - cp $< $@ + rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc \ + *-cc *-tcc *.exe \ + hello libtcc_test vla_test tcctest[1234] ex? tcc_g tcclib.h \ + ../lib/libtcc1.a diff --git a/tests/abitest.c b/tests/abitest.c index 7b12144..3ad707a 100644 --- a/tests/abitest.c +++ b/tests/abitest.c @@ -88,7 +88,7 @@ static int ret_2float_test_callback(void *ptr) { ret_2float_test_type a = {10, 35}; ret_2float_test_type r; r = f(a); - return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; + return ((r.x == a.x*5) && (r.y == a.y*3) && (f(a).x == a.x*5) && (f(a).y == a.y*3)) ? 0 : -1; } static int ret_2float_test(void) { @@ -116,7 +116,7 @@ static int ret_2double_test_callback(void *ptr) { ret_2double_test_type a = {10, 35}; ret_2double_test_type r; r = f(a); - return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; + return ((r.x == a.x*5) && (r.y == a.y*3) && (f(a).x == a.x*5) && (f(a).y == a.y*3)) ? 0 : -1; } static int ret_2double_test(void) { @@ -130,6 +130,50 @@ static int ret_2double_test(void) { return run_callback(src, ret_2double_test_callback); } +typedef struct ret_longdouble_test_type_s2 {LONG_DOUBLE x;} ret_longdouble_test_type; +typedef ret_longdouble_test_type (*ret_longdouble_test_function_type) (ret_longdouble_test_type); + +static int ret_longdouble_test_callback2(void *ptr) { + ret_longdouble_test_function_type f = (ret_longdouble_test_function_type)ptr; + ret_longdouble_test_type a = {10}; + ret_longdouble_test_type r; + r = f(a); + return ((r.x == a.x*5) && (f(a).x == a.x*5)) ? 0 : -1; +} + +static int ret_longdouble_test2(void) { + const char *src = + "typedef struct ret_longdouble_test_type_s2 {long double x;} ret_longdouble_test_type;" + "ret_longdouble_test_type f(ret_longdouble_test_type a) {\n" + " ret_longdouble_test_type r = {a.x*5};\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_longdouble_test_callback2); +} + +typedef struct ret_longlong_test_type_s2 {int x[4];} ret_longlong_test_type; +typedef ret_longlong_test_type (*ret_longlong_test_function_type) (ret_longlong_test_type); + +static int ret_longlong_test_callback2(void *ptr) { + ret_longlong_test_function_type f = (ret_longlong_test_function_type)ptr; + ret_longlong_test_type a = {{10,11,12,13}}; + ret_longlong_test_type r; + r = f(a); + return ((r.x[2] == a.x[2]*5) && (f(a).x[2] == a.x[2]*5)) ? 0 : -1; +} + +static int ret_longlong_test2(void) { + const char *src = + "typedef struct ret_longlong_test_type_s2 {int x[4];} ret_longlong_test_type;" + "ret_longlong_test_type f(ret_longlong_test_type a) {\n" + " ret_longlong_test_type r = {.x[2] = a.x[2]*5};\n" + " return r;\n" + "}\n"; + + return run_callback(src, ret_longlong_test_callback2); +} + /* * reg_pack_test: return a small struct which should be packed into * registers (Win32) during return. @@ -142,7 +186,7 @@ static int reg_pack_test_callback(void *ptr) { reg_pack_test_type a = {10, 35}; reg_pack_test_type r; r = f(a); - return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; + return ((r.x == a.x*5) && (r.y == a.y*3) && (f(a).x == a.x*5) && (f(a).y == a.y*3)) ? 0 : -1; } static int reg_pack_test(void) { @@ -168,7 +212,7 @@ static int reg_pack_longlong_test_callback(void *ptr) { reg_pack_longlong_test_type a = {10, 35}; reg_pack_longlong_test_type r; r = f(a); - return ((r.x == a.x*5) && (r.y == a.y*3)) ? 0 : -1; + return ((r.x == a.x*5) && (r.y == a.y*3) && (f(a).x == a.x*5) && (f(a).y == a.y*3)) ? 0 : -1; } static int reg_pack_longlong_test(void) { @@ -248,7 +292,7 @@ static int two_member_union_test_callback(void *ptr) { two_member_union_test_type a, b; a.x = 34; b = f(a); - return (b.x == a.x*2) ? 0 : -1; + return ((b.x == a.x*2) && (f(a).x == a.x*2)) ? 0 : -1; } static int two_member_union_test(void) { @@ -339,8 +383,8 @@ static int stdarg_test(void) { "#include \n" "typedef struct {long long a, b, c;} stdarg_test_struct_type;\n" "void f(int n_int, int n_float, int n_struct, ...) {\n" - " int i, ti;\n" - " double td;\n" + " int i, ti = 0;\n" + " double td = 0.0;\n" " stdarg_test_struct_type ts = {0,0,0}, tmp;\n" " va_list ap;\n" " va_start(ap, n_struct);\n" @@ -441,6 +485,8 @@ int main(int argc, char **argv) { RUN_TEST(ret_longdouble_test); RUN_TEST(ret_2float_test); RUN_TEST(ret_2double_test); + RUN_TEST(ret_longlong_test2); + RUN_TEST(ret_longdouble_test2); RUN_TEST(reg_pack_test); RUN_TEST(reg_pack_longlong_test); RUN_TEST(sret_test); diff --git a/tests/tcctest.c b/tests/tcctest.c index c5a3e73..cc8ffd8 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -59,6 +59,7 @@ #include "tcclib.h" +void intdiv_test(); void string_test(); void expr_test(); void macro_test(); @@ -167,6 +168,71 @@ int qq(int x) #define wq_spin_lock spin_lock #define TEST2() wq_spin_lock(a) +#define UINT_MAX ((unsigned) -1) + +void intdiv_test(void) +{ + printf("18/21=%u\n", 18/21); + printf("18%21=%u\n", 18%21); + printf("41/21=%u\n", 41/21); + printf("41%21=%u\n", 41%21); + printf("42/21=%u\n", 42/21); + printf("42%21=%u\n", 42%21); + printf("43/21=%u\n", 43/21); + printf("43%21=%u\n", 43%21); + printf("126/21=%u\n", 126/21); + printf("12%/21=%u\n", 126%21); + printf("131/21=%u\n", 131/21); + printf("131%21=%u\n", 131%21); + printf("(UINT_MAX/2+3)/2=%u\n", (UINT_MAX/2+3)/2); + printf("(UINT_MAX/2+3)%2=%u\n", (UINT_MAX/2+3)%2); + + printf("18/-21=%u\n", 18/-21); + printf("18%-21=%u\n", 18%-21); + printf("41/-21=%u\n", 41/-21); + printf("41%-21=%u\n", 41%-21); + printf("42/-21=%u\n", 42/-21); + printf("42%-21=%u\n", 42%-21); + printf("43/-21=%u\n", 43/-21); + printf("43%-21=%u\n", 43%-21); + printf("126/-21=%u\n", 126/-21); + printf("12%/-21=%u\n", 126%-21); + printf("131/-21=%u\n", 131/-21); + printf("131%-21=%u\n", 131%-21); + printf("(UINT_MAX/2+3)/-2=%u\n", (UINT_MAX/2+3)/-2); + printf("(UINT_MAX/2+3)%-2=%u\n", (UINT_MAX/2+3)%-2); + + printf("-18/21=%u\n", -18/21); + printf("-18%21=%u\n", -18%21); + printf("-41/21=%u\n", -41/21); + printf("-41%21=%u\n", -41%21); + printf("-42/21=%u\n", -42/21); + printf("-42%21=%u\n", -42%21); + printf("-43/21=%u\n", -43/21); + printf("-43%21=%u\n", -43%21); + printf("-126/21=%u\n", -126/21); + printf("-12%/21=%u\n", -126%21); + printf("-131/21=%u\n", -131/21); + printf("-131%21=%u\n", -131%21); + printf("-(UINT_MAX/2+3)/2=%u\n", (0-(UINT_MAX/2+3))/2); + printf("-(UINT_MAX/2+3)%2=%u\n", (0-(UINT_MAX/2+3))%2); + + printf("-18/-21=%u\n", -18/-21); + printf("-18%-21=%u\n", -18%-21); + printf("-41/-21=%u\n", -41/-21); + printf("-41%-21=%u\n", -41%-21); + printf("-42/-21=%u\n", -42/-21); + printf("-42%-21=%u\n", -42%-21); + printf("-43/-21=%u\n", -43/-21); + printf("-43%-21=%u\n", -43%-21); + printf("-126/-21=%u\n", -126/-21); + printf("-12%/-21=%u\n", -126%-21); + printf("-131/-21=%u\n", -131/-21); + printf("-131%-21=%u\n", -131%-21); + printf("-(UINT_MAX/2+3)/-2=%u\n", (0-(UINT_MAX/2+3))/-2); + printf("-(UINT_MAX/2+3)%-2=%u\n", (0-(UINT_MAX/2+3))%-2); +} + void macro_test(void) { printf("macro:\n"); @@ -619,6 +685,7 @@ int main(int argc, char **argv) math_cmp_test(); callsave_test(); builtin_frame_address_test(); + intdiv_test(); return 0; } @@ -1603,21 +1670,32 @@ void prefix ## fcast(type a)\ double da;\ LONG_DOUBLE la;\ int ia;\ + long long llia;\ unsigned int ua;\ + unsigned long long llua;\ type b;\ fa = a;\ da = a;\ la = a;\ printf("ftof: %f %f %Lf\n", fa, da, la);\ ia = (int)a;\ + llia = (long long)a;\ + a = (a >= 0) ? a : -a;\ ua = (unsigned int)a;\ - printf("ftoi: %d %u\n", ia, ua);\ + llua = (unsigned long long)a;\ + printf("ftoi: %d %u %lld %llu\n", ia, ua, llia, llua);\ ia = -1234;\ ua = 0x81234500;\ + llia = -0x123456789012345LL;\ + llua = 0xf123456789012345LLU;\ b = ia;\ printf("itof: " fmt "\n", b);\ b = ua;\ printf("utof: " fmt "\n", b);\ + b = llia;\ + printf("lltof: " fmt "\n", b);\ + b = llua;\ + printf("ulltof: " fmt "\n", b);\ }\ \ float prefix ## retf(type a) { return a; }\ @@ -1632,6 +1710,35 @@ void prefix ## call(void)\ printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\ }\ \ +void prefix ## signed_zeros(void) \ +{\ + type x = 0.0, y = -0.0, n, p;\ + if (x == y)\ + printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / y);\ + else\ + printf ("x != y; this is wrong!\n");\ +\ + n = -x;\ + if (x == n)\ + printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / n);\ + else\ + printf ("x != -x; this is wrong!\n");\ +\ + p = +y;\ + if (x == p)\ + printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / p);\ + else\ + printf ("x != +y; this is wrong!\n");\ + p = -y;\ + if (x == p)\ + printf ("Test 1.0 / x != 1.0 / -y returns %d (should be 0).\n",\ + 1.0 / x != 1.0 / p);\ + else\ + printf ("x != -y; this is wrong!\n");\ +}\ void prefix ## test(void)\ {\ printf("testing '%s'\n", #typename);\ @@ -1641,6 +1748,7 @@ void prefix ## test(void)\ prefix ## fcast(234.6);\ prefix ## fcast(-2334.6);\ prefix ## call();\ + prefix ## signed_zeros();\ } FTEST(f, float, float, "%f") @@ -2113,7 +2221,7 @@ void old_style_function(void) void alloca_test() { -#if defined __i386__ || defined __x86_64__ +#if defined __i386__ || defined __x86_64__ || defined __arm__ char *p = alloca(16); strcpy(p,"123456789012345"); printf("alloca: p is %s\n", p); @@ -2146,7 +2254,7 @@ void c99_vla_test(int size1, int size2) printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED"); tab1_ptr = tab1; tab2_ptr = tab2; - printf("Test C99 VLA 2 (ptrs substract): "); + printf("Test C99 VLA 2 (ptrs subtract): "); printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED"); printf("Test C99 VLA 3 (ptr add): "); printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED"); @@ -2686,7 +2794,7 @@ double get100 () { return 100.0; } void callsave_test(void) { -#if defined __i386__ || defined __x86_64__ +#if defined __i386__ || defined __x86_64__ || defined __arm__ int i, s; double *d; double t; s = sizeof (double); printf ("callsavetest: %d\n", s); @@ -2715,16 +2823,17 @@ void bfa2(ptrdiff_t str_offset) void bfa1(ptrdiff_t str_offset) { printf("bfa1: %s\n", (char *)__builtin_frame_address(1) + str_offset); -#if defined(__arm__) && !defined(__GNUC__) bfa2(str_offset); -#endif } void builtin_frame_address_test(void) { +/* builtin_frame_address fails on ARM with gcc which make test3 fail */ +#ifndef __arm__ char str[] = "__builtin_frame_address"; char *fp0 = __builtin_frame_address(0); printf("str: %s\n", str); bfa1(str-fp0); +#endif } diff --git a/tests/tests2/03_struct.c b/tests/tests2/03_struct.c index df0d3e7..c5d48c5 100644 --- a/tests/tests2/03_struct.c +++ b/tests/tests2/03_struct.c @@ -6,7 +6,7 @@ struct fred int natasha; }; -void main() +int main() { struct fred bloggs; diff --git a/tests/tests2/30_hanoi.c b/tests/tests2/30_hanoi.c index b1a1181..7c0893b 100644 --- a/tests/tests2/30_hanoi.c +++ b/tests/tests2/30_hanoi.c @@ -68,7 +68,7 @@ void PrintAll() /* Returns the value moved (not used.) */ int Move(int *source, int *dest) { - int i,j; + int i = 0, j = 0; while (i +#include /* * grep @@ -25,14 +26,13 @@ * See below for more information. */ -#if 0 char *documentation[] = { "grep searches a file for a given pattern. Execute by", " grep [flags] regular_expression file_list\n", - "Flags are single characters preceeded by '-':", + "Flags are single characters preceded by '-':", " -c Only a count of matching lines is printed", " -f Print file name for matching lines switch, see below", - " -n Each line is preceeded by its line number", + " -n Each line is preceded by its line number", " -v Only print non-matching lines\n", "The file_list is a list of files (wildcards are acceptable on RSX modes).", "\nThe file name is normally printed if there is a file given.", @@ -54,10 +54,10 @@ char *patdoc[] = { "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and", "': ' other control characters, such as new-line.", "'*' An expression followed by an asterisk matches zero or more", - " occurrances of that expression: \"fo*\" matches \"f\", \"fo\"", + " occurrences of that expression: \"fo*\" matches \"f\", \"fo\"", " \"foo\", etc.", "'+' An expression followed by a plus sign matches one or more", - " occurrances of that expression: \"fo+\" matches \"fo\", etc.", + " occurrences of that expression: \"fo+\" matches \"fo\", etc.", "'-' An expression followed by a minus sign optionally matches", " the expression.", "'[]' A string enclosed in square brackets matches any character in", @@ -70,7 +70,6 @@ char *patdoc[] = { " [a-z] matches alphabetics, while [z-a] never matches.", "The concatenation of regular expressions is a regular expression.", 0}; -#endif #define LMAX 512 #define PMAX 256 @@ -97,6 +96,10 @@ char *pp, lbuf[LMAX], pbuf[PMAX]; char *cclass(); char *pmatch(); +void store(int); +void error(char *); +void badpat(char *, char *, char *); +int match(void); /*** Display a file name *******************************/ @@ -153,7 +156,7 @@ void compile(char *source) o == STAR || o == PLUS || o == MINUS) - badpat("Illegal occurrance op.", source, s); + badpat("Illegal occurrence op.", source, s); store(ENDPAT); store(ENDPAT); spp = pp; /* Save pattern end */ @@ -300,7 +303,7 @@ void badpat(char *message, char *source, char *stop) /* char *stop; // Pattern end */ { fprintf(stderr, "-GREP-E-%s, pattern is\"%s\"\n", message, source); - fprintf(stderr, "-GREP-E-Stopped at byte %d, '%c'\n", + fprintf(stderr, "-GREP-E-Stopped at byte %ld, '%c'\n", stop-source, stop[-1]); error("?GREP-E-Bad pattern\n"); } @@ -338,7 +341,7 @@ void grep(FILE *fp, char *fn) } /*** Match line (lbuf) with pattern (pbuf) return 1 if match ***/ -void match() +int match() { char *l; /* Line pointer */ @@ -368,7 +371,7 @@ char *pmatch(char *line, char *pattern) p = pattern; while ((op = *p++) != ENDPAT) { if (debug > 1) - printf("byte[%d] = 0%o, '%c', op = 0%o\n", + printf("byte[%ld] = 0%o, '%c', op = 0%o\n", l-line, *l, *l, op); switch(op) { diff --git a/tests/tests2/46_grep.expect b/tests/tests2/46_grep.expect new file mode 100644 index 0000000..e8a6791 --- /dev/null +++ b/tests/tests2/46_grep.expect @@ -0,0 +1,3 @@ +File 46_grep.c: +/* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/ + diff --git a/tests/tests2/56_btype_excess-1.c b/tests/tests2/56_btype_excess-1.c new file mode 100644 index 0000000..06eabe7 --- /dev/null +++ b/tests/tests2/56_btype_excess-1.c @@ -0,0 +1 @@ +struct A {} int i; diff --git a/tests/tests2/56_btype_excess-1.expect b/tests/tests2/56_btype_excess-1.expect new file mode 100644 index 0000000..4e6d2d7 --- /dev/null +++ b/tests/tests2/56_btype_excess-1.expect @@ -0,0 +1 @@ +56_btype_excess-1.c:1: error: too many basic types diff --git a/tests/tests2/57_btype_excess-2.c b/tests/tests2/57_btype_excess-2.c new file mode 100644 index 0000000..ab95c3e --- /dev/null +++ b/tests/tests2/57_btype_excess-2.c @@ -0,0 +1 @@ +char int i; diff --git a/tests/tests2/57_btype_excess-2.expect b/tests/tests2/57_btype_excess-2.expect new file mode 100644 index 0000000..c12ef81 --- /dev/null +++ b/tests/tests2/57_btype_excess-2.expect @@ -0,0 +1 @@ +57_btype_excess-2.c:1: error: too many basic types diff --git a/tests/tests2/58_function_redefinition.c b/tests/tests2/58_function_redefinition.c new file mode 100644 index 0000000..33f16ee --- /dev/null +++ b/tests/tests2/58_function_redefinition.c @@ -0,0 +1,9 @@ +int f(void) +{ + return 0; +} + +int f(void) +{ + return 1; +} diff --git a/tests/tests2/58_function_redefinition.expect b/tests/tests2/58_function_redefinition.expect new file mode 100644 index 0000000..a95a3f0 --- /dev/null +++ b/tests/tests2/58_function_redefinition.expect @@ -0,0 +1 @@ +58_function_redefinition.c:7: error: redefinition of 'f' diff --git a/tests/tests2/59_function_array.c b/tests/tests2/59_function_array.c new file mode 100644 index 0000000..9fcc12d --- /dev/null +++ b/tests/tests2/59_function_array.c @@ -0,0 +1 @@ +int (*fct)[42](int x); diff --git a/tests/tests2/59_function_array.expect b/tests/tests2/59_function_array.expect new file mode 100644 index 0000000..bf62c6e --- /dev/null +++ b/tests/tests2/59_function_array.expect @@ -0,0 +1 @@ +59_function_array.c:1: error: declaration of an array of functions diff --git a/tests/tests2/60_enum_redefinition.c b/tests/tests2/60_enum_redefinition.c new file mode 100644 index 0000000..2601560 --- /dev/null +++ b/tests/tests2/60_enum_redefinition.c @@ -0,0 +1,4 @@ +enum color {RED, GREEN, BLUE}; +enum color {R, G, B}; + +enum color c; diff --git a/tests/tests2/60_enum_redefinition.expect b/tests/tests2/60_enum_redefinition.expect new file mode 100644 index 0000000..5cb41bc --- /dev/null +++ b/tests/tests2/60_enum_redefinition.expect @@ -0,0 +1 @@ +60_enum_redefinition.c:2: error: struct/union/enum already defined diff --git a/tests/tests2/61_undefined_enum.c b/tests/tests2/61_undefined_enum.c new file mode 100644 index 0000000..bc7c6ea --- /dev/null +++ b/tests/tests2/61_undefined_enum.c @@ -0,0 +1 @@ +enum rgb c = 42; diff --git a/tests/tests2/61_undefined_enum.expect b/tests/tests2/61_undefined_enum.expect new file mode 100644 index 0000000..7ccdeca --- /dev/null +++ b/tests/tests2/61_undefined_enum.expect @@ -0,0 +1 @@ +61_undefined_enum.c:1: error: unknown struct/union/enum diff --git a/tests/tests2/62_enumerator_redefinition.c b/tests/tests2/62_enumerator_redefinition.c new file mode 100644 index 0000000..3da85ae --- /dev/null +++ b/tests/tests2/62_enumerator_redefinition.c @@ -0,0 +1,4 @@ +enum color {RED, GREEN, BLUE}; +enum rgb {RED, G, B}; + +enum color c = RED; diff --git a/tests/tests2/62_enumerator_redefinition.expect b/tests/tests2/62_enumerator_redefinition.expect new file mode 100644 index 0000000..3d0e879 --- /dev/null +++ b/tests/tests2/62_enumerator_redefinition.expect @@ -0,0 +1 @@ +62_enumerator_redefinition.c:2: error: redefinition of enumerator 'RED' diff --git a/tests/tests2/63_local_enumerator_redefinition.c b/tests/tests2/63_local_enumerator_redefinition.c new file mode 100644 index 0000000..dd4d8e0 --- /dev/null +++ b/tests/tests2/63_local_enumerator_redefinition.c @@ -0,0 +1,14 @@ +enum { + FOO, + BAR +}; + +int main(void) +{ + enum { + FOO = 2, + BAR + }; + + return BAR - FOO; +} diff --git a/tests/tests2/63_local_enumerator_redefinition.expect b/tests/tests2/63_local_enumerator_redefinition.expect new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests2/64_macro_nesting.c b/tests/tests2/64_macro_nesting.c new file mode 100644 index 0000000..44b582f --- /dev/null +++ b/tests/tests2/64_macro_nesting.c @@ -0,0 +1,10 @@ +#define CAT2(a,b) a##b +#define CAT(a,b) CAT2(a,b) +#define AB(x) CAT(x,y) + +int main(void) +{ + int xy = 42; + printf("%d\n", CAT(A,B)(x)); + return 0; +} diff --git a/tests/tests2/64_macro_nesting.expect b/tests/tests2/64_macro_nesting.expect new file mode 100644 index 0000000..d81cc07 --- /dev/null +++ b/tests/tests2/64_macro_nesting.expect @@ -0,0 +1 @@ +42 diff --git a/tests/tests2/65_macro_concat_start.c b/tests/tests2/65_macro_concat_start.c new file mode 100644 index 0000000..d63d67a --- /dev/null +++ b/tests/tests2/65_macro_concat_start.c @@ -0,0 +1,2 @@ +#define paste(A,B) ##A B +paste(x,y) diff --git a/tests/tests2/65_macro_concat_start.expect b/tests/tests2/65_macro_concat_start.expect new file mode 100644 index 0000000..88ed6c5 --- /dev/null +++ b/tests/tests2/65_macro_concat_start.expect @@ -0,0 +1 @@ +65_macro_concat_start.c:1: error: '##' invalid at start of macro diff --git a/tests/tests2/66_macro_concat_end.c b/tests/tests2/66_macro_concat_end.c new file mode 100644 index 0000000..55dcaef --- /dev/null +++ b/tests/tests2/66_macro_concat_end.c @@ -0,0 +1,2 @@ +#define paste(A,B) A B## +paste(x,y) diff --git a/tests/tests2/66_macro_concat_end.expect b/tests/tests2/66_macro_concat_end.expect new file mode 100644 index 0000000..224e5c9 --- /dev/null +++ b/tests/tests2/66_macro_concat_end.expect @@ -0,0 +1 @@ +66_macro_concat_end.c:2: error: '##' invalid at end of macro diff --git a/tests/tests2/67_macro_concat.c b/tests/tests2/67_macro_concat.c new file mode 100644 index 0000000..c580d3a --- /dev/null +++ b/tests/tests2/67_macro_concat.c @@ -0,0 +1,14 @@ +#include + +#define P(A,B) A ## B ; bob +#define Q(A,B) A ## B+ + +int main(void) +{ + int bob, jim = 21; + bob = P(jim,) *= 2; + printf("jim: %d, bob: %d\n", jim, bob); + jim = 60 Q(+,)3; + printf("jim: %d\n", jim); + return 0; +} diff --git a/tests/tests2/67_macro_concat.expect b/tests/tests2/67_macro_concat.expect new file mode 100644 index 0000000..8386c2d --- /dev/null +++ b/tests/tests2/67_macro_concat.expect @@ -0,0 +1,2 @@ +jim: 21, bob: 42 +jim: 63 diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index 369ed47..c47fe0a 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -1,10 +1,10 @@ TOP = ../.. include $(TOP)/Makefile -VPATH = $(top_srcdir)/tests/tests2 -TCCFLAGS = -B$(TOP) -I$(top_srcdir)/include ifdef CONFIG_WIN32 TCCFLAGS = -B$(top_srcdir)/win32 -I$(top_srcdir)/include -L$(TOP) +else + TCCFLAGS = -B$(TOP) -I$(top_srcdir)/include -lm endif ifeq ($(TARGETOS),Darwin) @@ -46,9 +46,11 @@ TESTS = \ 27_sizeof.test \ 28_strings.test \ 29_array_address.test \ + 30_hanoi.test \ 31_args.test \ 32_led.test \ 33_ternary_op.test \ + 34_array_assignment.test \ 35_sizeof.test \ 36_array_initialisers.test \ 37_sprintf.test \ @@ -60,6 +62,7 @@ TESTS = \ 43_void_param.test \ 44_scoped_declarations.test \ 45_empty_for.test \ + 46_grep.test \ 47_switch_return.test \ 48_nested_break.test \ 49_bracket_evaluation.test \ @@ -67,35 +70,48 @@ TESTS = \ 51_static.test \ 52_unnamed_enum.test \ 54_goto.test \ - 55_lshift_type.test + 55_lshift_type.test \ + 56_btype_excess-1.test \ + 57_btype_excess-2.test \ + 58_function_redefinition.test \ + 59_function_array.test \ + 60_enum_redefinition.test \ + 61_undefined_enum.test \ + 62_enumerator_redefinition.test \ + 63_local_enumerator_redefinition.test \ + 64_macro_nesting.test \ + 65_macro_concat_start.test \ + 66_macro_concat_end.test \ + 67_macro_concat.test -# 30_hanoi.test -- seg fault in the code, gcc as well # 34_array_assignment.test -- array assignment is not in C standard -# 46_grep.test -- does not compile even with gcc + +SKIP = 34_array_assignment.test # some tests do not pass on all platforms, remove them for now ifeq ($(TARGETOS),Darwin) - TESTS := $(filter-out 40_stdio.test,$(TESTS)) + SKIP += 40_stdio.test endif ifdef CONFIG_WIN32 - TESTS := $(filter-out 24_math_library.test 28_strings.test,$(TESTS)) + SKIP += 24_math_library.test # don't have round() + SKIP += 28_strings.test # don't have r/index() / strings.h endif +# Some tests might need arguments +ARGS = +31_args.test : ARGS = arg1 arg2 arg3 arg4 arg5 +46_grep.test : ARGS = '[^* ]*[:a:d: ]+\:\*-/: $$' 46_grep.c + +all test: $(filter-out $(SKIP),$(TESTS)) + %.test: %.c %.expect @echo Test: $*... - @if [ "x`echo $* | grep args`" != "x" ]; \ - then $(TCC) $< -norunsrc -run $(notdir $<) - arg1 arg2 arg3 arg4 >$*.output; \ - else $(TCC) -run $< >$*.output; \ - fi - @if diff -bu $(<:.c=.expect) $*.output ; \ - then rm -f $*.output; \ - else exit 1; \ - fi -all test: $(TESTS) + @$(TCC) -run $< $(ARGS) >$*.output 2>&1 || true + @diff -bu $*.expect $*.output && rm -f $*.output + + @($(TCC) $< -o $*.exe && ./$*.exe $(ARGS)) >$*.output2 2>&1 || true + @diff -bu $*.expect $*.output2 && rm -f $*.output2 $*.exe clean: - rm -vf fred.txt *.output - -Makefile: $(top_srcdir)/tests/tests2/Makefile - cp $< $@ + rm -vf fred.txt *.output* *.exe diff --git a/win32/build-tcc.bat b/win32/build-tcc.bat index 5bc5585..f5c414a 100644 --- a/win32/build-tcc.bat +++ b/win32/build-tcc.bat @@ -5,7 +5,7 @@ @set /p VERSION= < ..\VERSION echo>..\config.h #define TCC_VERSION "%VERSION%" -@if _%PROCESSOR_ARCHITEW6432%_==_AMD64_ goto x86_64 +@rem @if _%PROCESSOR_ARCHITEW6432%_==_AMD64_ goto x86_64 @if _%PROCESSOR_ARCHITECTURE%_==_AMD64_ goto x86_64 @set target=-DTCC_TARGET_PE -DTCC_TARGET_I386 @@ -25,7 +25,7 @@ echo>..\config.h #define TCC_VERSION "%VERSION%" %CC% %target% tools/tiny_libmaker.c -o tiny_libmaker.exe :libtcc -if not exist libtcc\nul mkdir libtcc +if not exist libtcc mkdir libtcc copy ..\libtcc.h libtcc\libtcc.h %CC% %target% -shared -DLIBTCC_AS_DLL -DONE_SOURCE ../libtcc.c -o libtcc.dll -Wl,-out-implib,libtcc/libtcc.a tiny_impdef libtcc.dll -o libtcc/libtcc.def @@ -58,3 +58,10 @@ tiny_libmaker lib/libtcc1.a libtcc1.o alloca86_64.o crt1.o wincrt1.o dllcrt1.o d :the_end del *.o + +:makedoc +echo>..\config.texi @set VERSION %VERSION% +if not exist doc md doc +makeinfo --html --no-split -o doc\tcc-doc.html ../tcc-doc.texi +copy tcc-win32.txt doc +copy ..\tests\libtcc_test.c examples diff --git a/win32/include/math.h b/win32/include/math.h index 984a717..4fe64e7 100644 --- a/win32/include/math.h +++ b/win32/include/math.h @@ -666,6 +666,7 @@ extern "C" { extern long double __cdecl fmal (long double, long double, long double); +#if 0 // gr: duplicate, see below /* 7.12.14 */ /* * With these functions, comparisons involving quiet NaNs set the FP @@ -708,6 +709,7 @@ extern "C" { & 0x4500) == 0x4500) #endif +#endif //0 #endif /* __STDC_VERSION__ >= 199901L */ diff --git a/win32/include/process.h b/win32/include/process.h index f679442..dadaf2b 100644 --- a/win32/include/process.h +++ b/win32/include/process.h @@ -153,20 +153,20 @@ extern "C" { stupid warnings, define them in POSIX way. This is save, because those methods do not return in success case, so that the return value is not really dependent to its scalar width. */ - int __cdecl execv(const char *_Filename,char *const _ArgList[]); - int __cdecl execve(const char *_Filename,char *const _ArgList[],char *const _Env[]); - int __cdecl execvp(const char *_Filename,char *const _ArgList[]); - int __cdecl execvpe(const char *_Filename,char *const _ArgList[],char *const _Env[]); + int __cdecl execv(const char *_Filename,const char *const _ArgList[]); + int __cdecl execve(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); + int __cdecl execvp(const char *_Filename,const char *const _ArgList[]); + int __cdecl execvpe(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); #else - intptr_t __cdecl execv(const char *_Filename,char *const _ArgList[]); - intptr_t __cdecl execve(const char *_Filename,char *const _ArgList[],char *const _Env[]); - intptr_t __cdecl execvp(const char *_Filename,char *const _ArgList[]); - intptr_t __cdecl execvpe(const char *_Filename,char *const _ArgList[],char *const _Env[]); + intptr_t __cdecl execv(const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl execve(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); + intptr_t __cdecl execvp(const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl execvpe(const char *_Filename,const char *const _ArgList[],const char *const _Env[]); #endif - intptr_t __cdecl spawnv(int,const char *_Filename,char *const _ArgList[]); - intptr_t __cdecl spawnve(int,const char *_Filename,char *const _ArgList[],char *const _Env[]); - intptr_t __cdecl spawnvp(int,const char *_Filename,char *const _ArgList[]); - intptr_t __cdecl spawnvpe(int,const char *_Filename,char *const _ArgList[],char *const _Env[]); + intptr_t __cdecl spawnv(int,const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl spawnve(int,const char *_Filename,const char *const _ArgList[],const char *const _Env[]); + intptr_t __cdecl spawnvp(int,const char *_Filename,const char *const _ArgList[]); + intptr_t __cdecl spawnvpe(int,const char *_Filename,const char *const _ArgList[],char *const _Env[]); #endif #ifdef __cplusplus diff --git a/win32/include/winapi/winbase.h b/win32/include/winapi/winbase.h index a5d9b17..4a38006 100644 --- a/win32/include/winapi/winbase.h +++ b/win32/include/winapi/winbase.h @@ -968,15 +968,15 @@ extern "C" { LONG64 InterlockedExchangeAdd64(LONG64 volatile *Addend,LONG64 Value); LONG64 InterlockedCompareExchange64(LONG64 volatile *Destination,LONG64 ExChange,LONG64 Comperand); #else - LONG InterlockedIncrement(LONG volatile *lpAddend); - LONG InterlockedDecrement(LONG volatile *lpAddend); - LONG InterlockedExchange(LONG volatile *Target,LONG Value); + LONG WINAPI InterlockedIncrement(LONG volatile *lpAddend); + LONG WINAPI InterlockedDecrement(LONG volatile *lpAddend); + LONG WINAPI InterlockedExchange(LONG volatile *Target,LONG Value); #define InterlockedExchangePointer(Target,Value) (PVOID)InterlockedExchange((PLONG)(Target),(LONG)(Value)) - LONG InterlockedExchangeAdd(LONG volatile *Addend,LONG Value); - LONG InterlockedCompareExchange(LONG volatile *Destination,LONG Exchange,LONG Comperand); - LONGLONG InterlockedCompareExchange64(LONGLONG volatile *Destination,LONGLONG Exchange,LONGLONG Comperand); + LONG WINAPI InterlockedExchangeAdd(LONG volatile *Addend,LONG Value); + LONG WINAPI InterlockedCompareExchange(LONG volatile *Destination,LONG Exchange,LONG Comperand); + LONGLONG WINAPI InterlockedCompareExchange64(LONGLONG volatile *Destination,LONGLONG Exchange,LONGLONG Comperand); __CRT_INLINE LONGLONG InterlockedAnd64 (LONGLONG volatile *Destination,LONGLONG Value) { LONGLONG Old; diff --git a/win32/lib/wincrt1.c b/win32/lib/wincrt1.c index 77e74b8..663fd33 100644 --- a/win32/lib/wincrt1.c +++ b/win32/lib/wincrt1.c @@ -59,6 +59,6 @@ int _runwinmain(int argc, char **argv) szCmd = ""; else if (szCmd > p && szCmd[-1] == '\"') --szCmd; + _controlfp(0x10000, 0x30000); return WinMain(GetModuleHandle(NULL), NULL, szCmd, SW_SHOWDEFAULT); } - diff --git a/win32/tcc-win32.txt b/win32/tcc-win32.txt index dc06b8f..1cb35c5 100644 --- a/win32/tcc-win32.txt +++ b/win32/tcc-win32.txt @@ -18,6 +18,12 @@ system PATH. + Include and library search paths + -------------------------------- + On windows, the standard "include" and "lib" directories are searched + relatively from the location of the executables (tcc.exe, libtcc.dll). + + Examples: --------- Open a console window (DOS box) and 'cd' to the examples directory. diff --git a/win32/tools/tiny_impdef.c b/win32/tools/tiny_impdef.c index 1739549..d12c502 100644 --- a/win32/tools/tiny_impdef.c +++ b/win32/tools/tiny_impdef.c @@ -226,7 +226,7 @@ found: for (l = 0;;) { if (n+1 >= n0) p = tcc_realloc(p, n0 = n0 ? n0 * 2 : 256); - if (!read_mem(fd, ptr - ref + l, p + n, 1) || ++l >= 80) { + if (!read_mem(fd, ptr - ref + l++, p + n, 1)) { tcc_free(p), p = NULL; goto the_end; } diff --git a/x86_64-asm.h b/x86_64-asm.h index 69cd77c..31a7b38 100644 --- a/x86_64-asm.h +++ b/x86_64-asm.h @@ -438,7 +438,7 @@ ALT(DEF_ASM_OP2(psrlq, 0x0f73, 2, OPC_MODRM, OPT_IM8, OPT_MMX )) DEF_ASM_OP2(punpcklbw, 0x0f60, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) DEF_ASM_OP2(punpcklwd, 0x0f61, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) DEF_ASM_OP2(punpckldq, 0x0f62, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) - DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) + DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX ) /* must be last !OP0 */ #undef ALT #undef DEF_ASM_OP0 diff --git a/x86_64-gen.c b/x86_64-gen.c index d1bf75c..ae65328 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -112,8 +112,8 @@ enum { #define R_JMP_SLOT R_X86_64_JUMP_SLOT #define R_COPY R_X86_64_COPY -#define ELF_START_ADDR 0x08048000 -#define ELF_PAGE_SIZE 0x1000 +#define ELF_START_ADDR 0x400000 +#define ELF_PAGE_SIZE 0x200000 /******************************************************/ #else /* ! TARGET_DEFS_ONLY */ @@ -239,13 +239,6 @@ static int is64_type(int t) (t & VT_BTYPE) == VT_LLONG); } -static int is_sse_float(int t) { - int bt; - bt = t & VT_BTYPE; - return bt == VT_DOUBLE || bt == VT_FLOAT; -} - - /* instruction + 4 bytes data. Return the address of the data */ ST_FUNC int oad(int c, int s) { @@ -373,7 +366,7 @@ void load(int r, SValue *sv) #endif fr = sv->r; - ft = sv->type.t; + ft = sv->type.t & ~VT_DEFSIGN; fc = sv->c.ul; #ifndef TCC_TARGET_PE @@ -508,7 +501,7 @@ void load(int r, SValue *sv) o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8); } } else if (r == TREG_ST0) { - assert((v >= TREG_XMM0) || (v <= TREG_XMM7)); + assert((v >= TREG_XMM0) && (v <= TREG_XMM7)); /* gen_cvt_ftof(VT_LDOUBLE); */ /* movsd %xmmN,-0x10(%rsp) */ o(0x110ff2); @@ -611,7 +604,7 @@ static void gcall_or_jmp(int is_jmp) if (vtop->r & VT_SYM) { /* relocation case */ greloc(cur_text_section, vtop->sym, - ind + 1, R_X86_64_PC32); + ind + 1, R_X86_64_PLT32); } else { /* put an empty PC32 relocation */ put_elf_reloc(symtab_section, cur_text_section, @@ -663,30 +656,37 @@ void gen_offs_sp(int b, int r, int d) } } -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { int size, align; *ret_align = 1; // Never have to re-align return values for x86-64 size = type_size(vt, &align); ret->ref = NULL; if (size > 8) { - return 1; + return 0; } else if (size > 4) { ret->t = VT_LLONG; - return 0; + return 1; } else if (size > 2) { ret->t = VT_INT; - return 0; + return 1; } else if (size > 1) { ret->t = VT_SHORT; - return 0; + return 1; } else { ret->t = VT_BYTE; - return 0; + return 1; } } +static int is_sse_float(int t) { + int bt; + bt = t & VT_BTYPE; + return bt == VT_DOUBLE || bt == VT_FLOAT; +} + int gfunc_arg_size(CType *type) { int align; if (type->t & (VT_ARRAY|VT_BITFIELD)) @@ -833,6 +833,7 @@ void gfunc_prolog(CType *func_type) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->type; + func_var = (sym->c == FUNC_ELLIPSIS); size = gfunc_arg_size(&func_vt); if (size > 8) { gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr); @@ -932,7 +933,8 @@ typedef enum X86_64_Mode { x86_64_mode_x87 } X86_64_Mode; -static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) { +static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) +{ if (a == b) return a; else if (a == x86_64_mode_none) @@ -949,7 +951,8 @@ static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) { return x86_64_mode_sse; } -static X86_64_Mode classify_x86_64_inner(CType *ty) { +static X86_64_Mode classify_x86_64_inner(CType *ty) +{ X86_64_Mode mode; Sym *f; @@ -978,7 +981,7 @@ static X86_64_Mode classify_x86_64_inner(CType *ty) { return x86_64_mode_memory; mode = x86_64_mode_none; - for (; f; f = f->next) + for (f = f->next; f; f = f->next) mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type)); return mode; @@ -987,12 +990,14 @@ static X86_64_Mode classify_x86_64_inner(CType *ty) { assert(0); } -static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count) { +static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count) +{ X86_64_Mode mode; - int size, align, ret_t; + int size, align, ret_t = 0; if (ty->t & (VT_BITFIELD|VT_ARRAY)) { *psize = 8; + *palign = 8; *reg_count = 1; ret_t = ty->t; mode = x86_64_mode_integer; @@ -1030,6 +1035,7 @@ static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *p ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT; } break; + default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/ } } } @@ -1042,7 +1048,8 @@ static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *p return mode; } -ST_FUNC int classify_x86_64_va_arg(CType *ty) { +ST_FUNC int classify_x86_64_va_arg(CType *ty) +{ /* This definition must be synced with stdarg.h */ enum __va_arg_type { __va_gen_reg, __va_float_reg, __va_stack @@ -1056,11 +1063,13 @@ ST_FUNC int classify_x86_64_va_arg(CType *ty) { } } -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ -int gfunc_sret(CType *vt, CType *ret, int *ret_align) { +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) +{ int size, align, reg_count; *ret_align = 1; // Never have to re-align return values for x86-64 - return (classify_x86_64_arg(vt, ret, &size, &align, ®_count) == x86_64_mode_memory); + return (classify_x86_64_arg(vt, ret, &size, &align, ®_count) != x86_64_mode_memory); } #define REGN 6 @@ -1083,7 +1092,7 @@ void gfunc_call(int nb_args) { X86_64_Mode mode; CType type; - int size, align, r, args_size, stack_adjust, run_start, run_end, i, j, reg_count; + int size, align, r, args_size, stack_adjust, run_start, run_end, i, reg_count; int nb_reg_args = 0; int nb_sse_args = 0; int sse_reg, gen_reg; @@ -1133,6 +1142,7 @@ void gfunc_call(int nb_args) gen_reg -= reg_count; if (gen_reg + reg_count > REGN) goto stack_arg; break; + default: break; /* nothing to be done for x86_64_mode_none */ } } @@ -1262,7 +1272,7 @@ void gfunc_call(int nb_args) g(0x00); args_size += size; } else { - assert(mode == x86_64_mode_memory); + //assert(mode == x86_64_mode_memory); /* allocate the necessary size on stack */ o(0x48); @@ -1366,7 +1376,7 @@ void gfunc_prolog(CType *func_type) { X86_64_Mode mode; int i, addr, align, size, reg_count; - int param_addr, reg_param_index, sse_param_index; + int param_addr = 0, reg_param_index, sse_param_index; Sym *sym; CType *type; @@ -1499,6 +1509,7 @@ void gfunc_prolog(CType *func_type) } break; } + default: break; /* nothing to be done for x86_64_mode_none */ } sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, param_addr); @@ -1577,7 +1588,7 @@ int gtst(int inv, int t) } g(0x0f); t = psym((vtop->c.i - 16) ^ inv, t); - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -1590,23 +1601,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t) || - (vtop->type.t & VT_BTYPE) == VT_LLONG) { - vpushi(0); - gen_op(TOK_NE); - } - if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { - /* constant jmp optimization */ - if ((vtop->c.i != 0) != inv) - t = gjmp(t); - } else { - v = gv(RC_INT); - orex(0,v,v,0x85); - o(0xc0 + REG_VALUE(v) * 9); - g(0x0f); - t = psym(0x85 ^ inv, t); - } } vtop--; return t; @@ -1788,7 +1782,10 @@ void gen_opf(int op) swapped = 0; if (swapped) o(0xc9d9); /* fxch %st(1) */ - o(0xe9da); /* fucompp */ + if (op == TOK_EQ || op == TOK_NE) + o(0xe9da); /* fucompp */ + else + o(0xd9de); /* fcompp */ o(0xe0df); /* fnstsw %ax */ if (op == TOK_EQ) { o(0x45e480); /* and $0x45, %ah */ @@ -1872,8 +1869,11 @@ void gen_opf(int op) if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) o(0x66); - o(0x2e0f); /* ucomisd */ - + if (op == TOK_EQ || op == TOK_NE) + o(0x2e0f); /* ucomisd */ + else + o(0x2f0f); /* comisd */ + if (vtop->r & VT_LVAL) { gen_modrm(vtop[-1].r, r, vtop->sym, fc); } else {