tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] TCC modification simple boundschecking for windows


From: Simon Lehmayr
Subject: [Tinycc-devel] TCC modification simple boundschecking for windows
Date: Tue, 2 Dec 2008 11:58:33 +0100 (CET)

Hello,

I currently try to enable a mild boundschecking for the windows version. I 
already found out that malloc hooks are required to check allocated memory. But 
I don't use malloc in my programs. So I wanted a mild bounds-checking version 
working for me.
I also modified the make batch file to use tcc to compile itself. I use mingw 
ar.exe. tcc0 is the tcc compiled of previous run.
New batch file:
@rem ----------------------------------------------------
@rem batch file to build tcc using gcc and ar from mingw
@rem ----------------------------------------------------
:
@echo>..\config.h #define TCC_VERSION "0.9.24"
@echo>>..\config.h #define TCC_TARGET_PE 1
@echo>>..\config.h #define CONFIG_TCCDIR "."
:
tcc0 -fno-strict-aliasing ../tcc.c -o tcc.exe
tcc0 -fno-strict-aliasing ../tcc.c -D LIBTCC -c -o libtcc.o
tcc0 -fno-strict-aliasing ../bcheck.c -c
tcc0 tools/tiny_impdef.c -o tiny_impdef.exe
tcc0 tools/tiny_libmaker.c -o tiny_libmaker.exe
mkdir libtcc
ar rcs libtcc/libtcc.a libtcc.o
rem del libtcc.o
copy ..\libtcc.h libtcc
:
tcc0 -c lib/crt1.c
tcc0 -c lib/wincrt1.c
tcc0 -c lib/dllcrt1.c
tcc0 -c lib/dllmain.c
tcc0 -c lib/chkstk.S
tcc0 -c ../libtcc1.c
tcc0 -c ../alloca86.S
tcc0 -c ../alloca86-bt.S
ar rcs lib/libtcc1.a crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o libtcc1.o 
alloca86.o alloca86-bt.o
@rem del *.o
del tcc0.exe
copy tcc.exe tcc0.exe
@pause

But I have the problem that I can't fix: My compiled test program won't run. It 
crashes with a memory access violation.

Test c program:
#include <stdio.h>
void main()
{
        volatile char a[9];
        a[8]=0; // should work fine
//      a[9]=0; // should cause exception
        printf("hello world\n");
}

I modified bcheck.c to include boundschecking in windows builds and added 
#undef CONFIG_TCC_MALLOC_HOOKS to remove the malloc hooks
I defined #define CONFIG_TCC_BCHECK in all .c files

I modified tccpe.c to add the required sections for bcheck. I think here is my 
error:
function pe_add_runtime_ex:
ST_FN void pe_add_runtime_ex(TCCState *s1, struct pe_info *pe)
{
    const char *start_symbol;
    unsigned long addr = 0;
    int pe_type = 0;

// ------------------------- new ----------------------------
#ifdef CONFIG_TCC_BCHECK
    if (do_bounds_check) {
        unsigned long *ptr;
        Section *init_section;
        unsigned char *pinit;
        int sym_index;

        /* XXX: add an object file to do that */
        ptr = section_ptr_add(bounds_section, sizeof(unsigned long));
        *ptr = 0;
        add_elf_sym(symtab_section, 0, 0,
                    ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
                    bounds_section->sh_num, "__bounds_start");
        /* add bound check code */
//        snprintf(buf, sizeof(buf), "%s/%s", tcc_lib_path, "bcheck.o");
//        tcc_add_file(s1, buf);
#ifdef TCC_TARGET_I386
        if (s1->output_type != TCC_OUTPUT_MEMORY) {
            /* add 'call __bound_init()' in .init section */
            init_section = find_section(s1, ".init");
            pinit = section_ptr_add(init_section, 5);
            pinit[0] = 0xe8;
            put32(pinit + 1, -4);
            sym_index = find_elf_sym(symtab_section, "__bound_init");
            put_elf_reloc(symtab_section, init_section,
                          init_section->data_offset - 4, R_386_PC32, sym_index);
        }
#endif
    }
#endif
// ------------------------- end new ----------------------------

    if (find_elf_sym(symtab_section, "address@hidden"))
        pe_type = PE_GUI;
    else
    if (TCC_OUTPUT_DLL == s1->output_type) {
        pe_type = PE_DLL;
        /* need this for 'tccelf.c:relocate_section()' */
        s1->output_type = TCC_OUTPUT_EXE;
    }

    start_symbol =
        TCC_OUTPUT_MEMORY == s1->output_type
        ? PE_GUI == pe_type ? "_runwinmain" : NULL
        : PE_DLL == pe_type ? "address@hidden"
        : PE_GUI == pe_type ? "_winstart" : "_start"
        ;

    /* grab the startup code from libtcc1 */
    if (start_symbol)
        add_elf_sym(symtab_section,
            0, 0,
            ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
            SHN_UNDEF, start_symbol);

    if (0 == s1->nostdlib) {
        tcc_add_library(s1, "tcc1");
#ifdef __CYGWIN__
        tcc_add_library(s1, "cygwin1");
#else
        tcc_add_library(s1, "msvcrt");
#endif
        tcc_add_library(s1, "kernel32");
        if (PE_DLL == pe_type || PE_GUI == pe_type) {
            tcc_add_library(s1, "user32");
            tcc_add_library(s1, "gdi32");
        }
    }

    if (start_symbol) {
        addr = (unsigned long)tcc_get_symbol_err(s1, start_symbol);
        if (s1->output_type == TCC_OUTPUT_MEMORY && addr)
            /* for -run GUI's, put '_runwinmain' instead of 'main' */
            add_elf_sym(symtab_section,
                    addr, 0,
                    ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
                    text_section->sh_num, "main");
    }

    if (pe) {
        pe->type = pe_type;
        pe->start_addr = addr;
    }
}

I hope you can help me here. I think I'm just a little step away from the goal.
Simon


Jetzt komfortabel bei Arcor-Digital TV einsteigen: Mehr Happy Ends, mehr 
Herzschmerz, mehr Fernsehen! Erleben Sie 50 digitale TV Programme und optional 
60 Pay TV Sender, einen elektronischen Programmführer mit Movie Star 
Bewertungen von TV Movie. Außerdem, aktuelle Filmhits und spannende Dokus in 
der Arcor-Videothek. Infos unter www.arcor.de/tv




reply via email to

[Prev in Thread] Current Thread [Next in Thread]