From d7a7c3769d0a1dcb8400258cd8adf78a8566de61 Mon Sep 17 00:00:00 2001 From: mob Date: Sun, 26 Feb 2012 19:02:51 -0600 Subject: [PATCH] Multiple fixes for 64 bit sections This changeset attempts to fix a few problems when giving using the high 32bits of a 64bit section offset. There are likely more issues (or perhaps regressions) lurking in the muck here. In general, this moves a few data type declarations to use uplong. Also, add support for 64bit mingw32 building under cygwin. Because native types are used for 64 bit offsets, this won't fix challenges with cross compiling from 32bit -> 64bit. Tested under cygwin, against binary compiled with -Wl,-Ttext=0xffffff8000000000 Signed-off-by: Andrew Mulbrook --- Makefile | 3 +++ configure | 12 +++++++++--- libtcc.c | 9 ++++----- tcc.h | 11 ++++++----- tccelf.c | 21 +++++++++++++-------- tccrun.c | 9 ++++++--- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index ab9a698..fb2f721 100644 --- a/Makefile +++ b/Makefile @@ -318,9 +318,12 @@ install: $(PROGS) $(TCCLIBS) $(TCCDOCS) ifdef CONFIG_CROSS mkdir -p "$(tccdir)/lib/32" mkdir -p "$(tccdir)/lib/64" +ifeq ($(ARCH),x86-64) -$(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/lib/32" +else -$(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/lib/64" endif +endif uninstall: rm -rfv "$(tccdir)/*" diff --git a/configure b/configure index 188c360..ed6ca6f 100755 --- a/configure +++ b/configure @@ -154,6 +154,8 @@ for opt do ;; --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; cpu=x86 ;; + --enable-cygwin64) mingw32="yes" ; cygwin="yes" ; cross_prefix="x86_64-w64-mingw32-" ; cpu="x86-64" + ;; --enable-cross) build_cross="yes" ;; --disable-static) disable_static="yes" @@ -431,14 +433,18 @@ if test "$noldl" = "yes" ; then echo "CONFIG_NOLDL=yes" >> config.mak fi if test "$mingw32" = "yes" ; then - echo "CONFIG_WIN32=yes" >> config.mak - echo "#define CONFIG_WIN32 1" >> $TMPH + if test "$cpu" = "x86-64" ; then + echo "CONFIG_WIN64=yes" >> config.mak + echo "#define CONFIG_WIN64 1" >> $TMPH + else + echo "CONFIG_WIN32=yes" >> config.mak + echo "#define CONFIG_WIN32 1" >> $TMPH + fi fi if test "$cygwin" = "yes" ; then echo "#ifndef _WIN32" >> $TMPH echo "#define _WIN32" >> $TMPH echo "#endif" >> $TMPH - echo "AR=ar" >> config.mak fi if test "$bigendian" = "yes" ; then echo "WORDS_BIGENDIAN=yes" >> config.mak diff --git a/libtcc.c b/libtcc.c index 8f5cd0e..159e23b 100644 --- a/libtcc.c +++ b/libtcc.c @@ -425,7 +425,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, - unsigned long value, unsigned long size, + uplong value, unsigned long size, int can_add_underscore) { int sym_type, sym_bind, sh_num, info, other; @@ -529,7 +529,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, } ST_FUNC void put_extern_sym(Sym *sym, Section *section, - unsigned long value, unsigned long size) + uplong value, unsigned long size) { put_extern_sym2(sym, section, value, size, 1); } @@ -1493,7 +1493,7 @@ PUB_FUNC const char * tcc_set_linker(TCCState *s, char *option, int multi) if (s->warn_unsupported) tcc_warning("ignoring -fini %s", p); } else if (link_option(option, "image-base=", &p)) { - s->text_addr = strtoul(p, &end, 16); + s->text_addr = strtoull(p, &end, 16); s->has_text_addr = 1; } else if (link_option(option, "init=", &p)) { s->init_symbol = p; @@ -1562,9 +1562,8 @@ PUB_FUNC const char * tcc_set_linker(TCCState *s, char *option, int multi) #endif } else if (link_option(option, "Ttext=", &p)) { - s->text_addr = strtoul(p, &end, 16); + s->text_addr = strtoull(p, &end, 16); s->has_text_addr = 1; - } else { return option; } diff --git a/tcc.h b/tcc.h index d158829..18a6b91 100644 --- a/tcc.h +++ b/tcc.h @@ -313,8 +313,8 @@ typedef struct Section { int sh_addralign; /* elf section alignment */ int sh_entsize; /* elf entry size */ unsigned long sh_size; /* section size (only used during output) */ - unsigned long sh_addr; /* address at which the section is relocated */ - unsigned long sh_offset; /* file offset */ + uplong sh_addr; /* address at which the section is relocated */ + unsigned long sh_offset; /* file offset */ int nb_hashed_syms; /* used to resize the hash table */ struct Section *link; /* link to another section */ struct Section *reloc; /* corresponding section for relocation, if any */ @@ -531,7 +531,7 @@ struct TCCState { int alacarte_link; /* address of text section */ - unsigned long text_addr; + uplong text_addr; int has_text_addr; /* symbols to call at load-time / unload-time */ @@ -872,6 +872,7 @@ enum tcc_token { #define strtold (long double)strtod #define strtof (float)strtod #define strtoll (long long)strtol + #define strtoull (unsigned long long)strtoull #endif #else /* XXX: need to define this to use them in non ISOC99 context */ @@ -992,8 +993,8 @@ ST_FUNC void *section_ptr_add(Section *sec, unsigned long size); ST_FUNC void section_reserve(Section *sec, unsigned long size); ST_FUNC Section *find_section(TCCState *s1, const char *name); -ST_FUNC void put_extern_sym2(Sym *sym, Section *section, unsigned long value, unsigned long size, int can_add_underscore); -ST_FUNC void put_extern_sym(Sym *sym, Section *section, unsigned long value, unsigned long size); +ST_FUNC void put_extern_sym2(Sym *sym, Section *section, uplong value, unsigned long size, int can_add_underscore); +ST_FUNC void put_extern_sym(Sym *sym, Section *section, uplong value, unsigned long size); ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type); ST_INLN void sym_free(Sym *sym); diff --git a/tccelf.c b/tccelf.c index 40c88ab..2b4b0ab 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1153,7 +1153,7 @@ ST_FUNC Section *new_symtab(TCCState *s1, } /* put dynamic tag */ -static void put_dt(Section *dynamic, int dt, unsigned long val) +static void put_dt(Section *dynamic, int dt, uplong val) { ElfW(Dyn) *dyn; dyn = section_ptr_add(dynamic, sizeof(ElfW(Dyn))); @@ -1381,7 +1381,7 @@ ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel) section_reserve(s1->got, offset + PTR_SIZE); #ifdef TCC_TARGET_X86_64 /* only works for x86-64 */ - put32(s1->got->data + offset, sym->st_value >> 32); + put32(s1->got->data + offset + 4, sym->st_value >> 32); #endif put32(s1->got->data + offset, sym->st_value & 0xffffffff); } @@ -1421,8 +1421,9 @@ static int elf_output_file(TCCState *s1, const char *filename) FILE *f; int fd, mode, ret; int *section_order; - int shnum, i, phnum, file_offset, offset, size, j, tmp, sh_order_index, k; - unsigned long addr; + int shnum, i, phnum, file_offset, offset, size, j, sh_order_index, k; + long long tmp; + uplong addr; Section *strsec, *s; ElfW(Shdr) shdr, *sh; ElfW(Phdr) *phdr, *ph; @@ -1430,9 +1431,9 @@ static int elf_output_file(TCCState *s1, const char *filename) unsigned long saved_dynamic_data_offset; ElfW(Sym) *sym; int type, file_type; - unsigned long rel_addr, rel_size; + uplong rel_addr, rel_size; #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) - unsigned long bss_addr, bss_size; + uplong bss_addr, bss_size; #endif file_type = s1->output_type; @@ -1747,7 +1748,7 @@ static int elf_output_file(TCCState *s1, const char *filename) addr = s1->text_addr; /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset % ELF_PAGE_SIZE */ - a_offset = addr & (s1->section_align - 1); + 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; @@ -1821,7 +1822,7 @@ static int elf_output_file(TCCState *s1, const char *filename) tmp = addr; addr = (addr + s->sh_addralign - 1) & ~(s->sh_addralign - 1); - file_offset += addr - tmp; + file_offset += (int) ( addr - tmp ); s->sh_offset = file_offset; s->sh_addr = addr; @@ -1915,8 +1916,12 @@ static int elf_output_file(TCCState *s1, const char *filename) ph->p_align = dynamic->sh_addralign; /* put GOT dynamic section address */ +#if defined(TCC_TARGET_X86_64) + put32(s1->got->data + 4, dynamic->sh_addr >> 32 ); +#endif put32(s1->got->data, dynamic->sh_addr); + /* relocate the PLT */ if (file_type == TCC_OUTPUT_EXE #if defined(TCC_OUTPUT_DLL_WITH_PLT) diff --git a/tccrun.c b/tccrun.c index de0ef89..2ea3c49 100644 --- a/tccrun.c +++ b/tccrun.c @@ -36,10 +36,11 @@ static int rt_get_caller_pc(uplong *paddr, ucontext_t *uc, int level); static void rt_error(ucontext_t *uc, const char *fmt, ...); static int tcc_relocate_ex(TCCState *s1, void *ptr); -#ifdef _WIN64 +#if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE static void win64_add_function_table(TCCState *s1); #endif + /* ------------------------------------------------------------- */ /* Do all relocations (needed before using tcc_get_symbol()) Returns -1 on error. */ @@ -193,7 +194,7 @@ static int tcc_relocate_ex(TCCState *s1, void *ptr) s1->runtime_plt_and_got_offset); #endif -#ifdef _WIN64 +#if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE win64_add_function_table(s1); #endif return 0; @@ -621,7 +622,8 @@ static void set_exception_handler(void) SetUnhandledExceptionFilter(cpu_exception_handler); } -#ifdef _WIN64 +#ifdef TCC_TARGET_PE +#ifdef TCC_TARGET_X86_64 static void win64_add_function_table(TCCState *s1) { RtlAddFunctionTable( @@ -631,6 +633,7 @@ static void win64_add_function_table(TCCState *s1) ); } #endif +#endif /* return the PC at frame level 'level'. Return non zero if not found */ static int rt_get_caller_pc(uplong *paddr, CONTEXT *uc, int level)