From c1e1c17c0af0960694dc7a98c08785c3b2937713 Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Sun, 12 Jul 2020 10:55:40 +0200 Subject: [PATCH] Move bound functions to tccgen.c Move gen_bounded_ptr_add() and gen_bounded_ptr_deref() code to tccgen.c No functional changes. --- arm-gen.c | 51 -------------------------------------------------- arm64-gen.c | 51 -------------------------------------------------- i386-gen.c | 50 ------------------------------------------------- riscv64-gen.c | 52 +-------------------------------------------------- tcc-doc.texi | 4 ---- tcc.h | 5 ----- tccgen.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++ x86_64-gen.c | 51 -------------------------------------------------- 8 files changed, 49 insertions(+), 263 deletions(-) diff --git a/arm-gen.c b/arm-gen.c index 11b258ad..784b2369 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -791,57 +791,6 @@ static void gen_bounds_call(int v) o(0xebfffffe); } -/* generate a bounded pointer addition */ -ST_FUNC void gen_bounded_ptr_add(void) -{ - vpush_global_sym(&func_old_type, TOK___bound_ptr_add); - vrott(3); - gfunc_call(2); - vpushi(0); - /* returned pointer is in REG_IRET */ - vtop->r = REG_IRET | VT_BOUNDED; - if (nocode_wanted) - return; - /* relocation offset of the bounding function call point */ - vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel)); -} - -/* patch pointer addition in vtop so that pointer dereferencing is - also tested */ -ST_FUNC void gen_bounded_ptr_deref(void) -{ - addr_t func; - int size, align; - Elf32_Rel *rel; - Sym *sym; - - if (nocode_wanted) - return; - - size = type_size(&vtop->type, &align); - switch(size) { - case 1: func = TOK___bound_ptr_indir1; break; - case 2: func = TOK___bound_ptr_indir2; break; - case 4: func = TOK___bound_ptr_indir4; break; - case 8: func = TOK___bound_ptr_indir8; break; - case 12: func = TOK___bound_ptr_indir12; break; - case 16: func = TOK___bound_ptr_indir16; break; - default: - /* may happen with struct member access */ - return; - //tcc_error("unhandled size when dereferencing bounded pointer"); - //func = 0; - //break; - } - sym = external_global_sym(func, &func_old_type); - if (!sym->c) - put_extern_sym(sym, NULL, 0, 0); - /* patch relocation */ - /* XXX: find a better solution ? */ - rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i); - rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info)); -} - static void gen_bounds_prolog(void) { /* leave some room for bound checking code */ diff --git a/arm64-gen.c b/arm64-gen.c index 737c8264..429c4f04 100644 --- a/arm64-gen.c +++ b/arm64-gen.c @@ -625,57 +625,6 @@ static void gen_bounds_call(int v) o(0x94000000); // bl } -/* generate a bounded pointer addition */ -ST_FUNC void gen_bounded_ptr_add(void) -{ - vpush_global_sym(&func_old_type, TOK___bound_ptr_add); - vrott(3); - gfunc_call(2); - vpushi(0); - /* returned pointer is in REG_IRET */ - vtop->r = REG_IRET | VT_BOUNDED; - if (nocode_wanted) - return; - /* relocation offset of the bounding function call point */ - vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW(Rela))); -} - -/* patch pointer addition in vtop so that pointer dereferencing is - also tested */ -ST_FUNC void gen_bounded_ptr_deref(void) -{ - addr_t func; - int size, align; - ElfW(Rela) *rel; - Sym *sym; - - if (nocode_wanted) - return; - - size = type_size(&vtop->type, &align); - switch(size) { - case 1: func = TOK___bound_ptr_indir1; break; - case 2: func = TOK___bound_ptr_indir2; break; - case 4: func = TOK___bound_ptr_indir4; break; - case 8: func = TOK___bound_ptr_indir8; break; - case 12: func = TOK___bound_ptr_indir12; break; - case 16: func = TOK___bound_ptr_indir16; break; - default: - /* may happen with struct member access */ - return; - //tcc_error("unhandled size when dereferencing bounded pointer"); - //func = 0; - //break; - } - sym = external_global_sym(func, &func_old_type); - if (!sym->c) - put_extern_sym(sym, NULL, 0, 0); - /* patch relocation */ - /* XXX: find a better solution ? */ - rel = (ElfW(Rela) *)(cur_text_section->reloc->data + vtop->c.i); - rel->r_info = ELF64_R_INFO(sym->c, ELF64_R_TYPE(rel->r_info)); -} - static void gen_bounds_prolog(void) { /* leave some room for bound checking code */ diff --git a/i386-gen.c b/i386-gen.c index dbf0fda2..8a0fb355 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -1027,56 +1027,6 @@ ST_FUNC void ggoto(void) /* bound check support functions */ #ifdef CONFIG_TCC_BCHECK -/* generate a bounded pointer addition */ -ST_FUNC void gen_bounded_ptr_add(void) -{ - vpush_global_sym(&func_old_type, TOK___bound_ptr_add); - vrott(3); - gfunc_call(2); - vpushi(0); - /* returned pointer is in eax */ - vtop->r = TREG_EAX | VT_BOUNDED; - if (nocode_wanted) - return; - /* relocation offset of the bounding function call point */ - vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel)); -} - -/* patch pointer addition in vtop so that pointer dereferencing is - also tested */ -ST_FUNC void gen_bounded_ptr_deref(void) -{ - addr_t func; - int size, align; - Elf32_Rel *rel; - Sym *sym; - - if (nocode_wanted) - return; - - size = type_size(&vtop->type, &align); - switch(size) { - case 1: func = TOK___bound_ptr_indir1; break; - case 2: func = TOK___bound_ptr_indir2; break; - case 4: func = TOK___bound_ptr_indir4; break; - case 8: func = TOK___bound_ptr_indir8; break; - case 12: func = TOK___bound_ptr_indir12; break; - case 16: func = TOK___bound_ptr_indir16; break; - default: - /* may happen with struct member access */ - return; - //tcc_error("unhandled size when dereferencing bounded pointer"); - //func = 0; - //break; - } - sym = external_global_sym(func, &func_old_type); - if (!sym->c) - put_extern_sym(sym, NULL, 0, 0); - /* patch relocation */ - /* XXX: find a better solution ? */ - rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i); - rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info)); -} static void gen_bounds_prolog(void) { diff --git a/riscv64-gen.c b/riscv64-gen.c index 8c3486df..beb0a46c 100644 --- a/riscv64-gen.c +++ b/riscv64-gen.c @@ -409,57 +409,6 @@ static void gen_bounds_call(int v) EI(0x67, 0, 1, 1, 0); // jalr TR, r(TR) } -/* generate a bounded pointer addition */ -ST_FUNC void gen_bounded_ptr_add(void) -{ - vpush_global_sym(&func_old_type, TOK___bound_ptr_add); - vrott(3); - gfunc_call(2); - vpushi(0); - /* returned pointer is in REG_IRET */ - vtop->r = REG_IRET | VT_BOUNDED; - if (nocode_wanted) - return; - /* relocation offset of the bounding function call point */ - vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW(Rela))); -} - -/* patch pointer addition in vtop so that pointer dereferencing is - also tested */ -ST_FUNC void gen_bounded_ptr_deref(void) -{ - addr_t func; - int size, align; - ElfW(Rela) *rel; - Sym *sym; - - if (nocode_wanted) - return; - - size = type_size(&vtop->type, &align); - switch(size) { - case 1: func = TOK___bound_ptr_indir1; break; - case 2: func = TOK___bound_ptr_indir2; break; - case 4: func = TOK___bound_ptr_indir4; break; - case 8: func = TOK___bound_ptr_indir8; break; - case 12: func = TOK___bound_ptr_indir12; break; - case 16: func = TOK___bound_ptr_indir16; break; - default: - /* may happen with struct member access */ - return; - //tcc_error("unhandled size when dereferencing bounded pointer"); - //func = 0; - //break; - } - sym = external_global_sym(func, &func_old_type); - if (!sym->c) - put_extern_sym(sym, NULL, 0, 0); - /* patch relocation */ - /* XXX: find a better solution ? */ - rel = (ElfW(Rela) *)(cur_text_section->reloc->data + vtop->c.i); - rel->r_info = ELF64_R_INFO(sym->c, ELF64_R_TYPE(rel->r_info)); -} - static void gen_bounds_prolog(void) { /* leave some room for bound checking code */ @@ -522,6 +471,7 @@ static void gen_bounds_epilog(void) o(0x61052542); /* fld fa0,16(sp) addi sp,sp,32 */ } #endif + static void reg_pass_rec(CType *type, int *rc, int *fieldofs, int ofs) { if ((type->t & VT_BTYPE) == VT_STRUCT) { diff --git a/tcc-doc.texi b/tcc-doc.texi index 4cc2a7b7..390e764e 100644 --- a/tcc-doc.texi +++ b/tcc-doc.texi @@ -1354,10 +1354,6 @@ floating point to integer conversion. @item gen_cvt_ftof() floating point to floating point of different size conversion. -@item gen_bounded_ptr_add() -@item gen_bounded_ptr_deref() -are only used for bounds checking. - @end table @section Optimizations done diff --git a/tcc.h b/tcc.h index bca6f65d..321ca57f 100644 --- a/tcc.h +++ b/tcc.h @@ -1649,11 +1649,6 @@ ST_FUNC void gen_addrpc32(int r, Sym *sym, int c); ST_FUNC void gen_cvt_csti(int t); #endif -#ifdef CONFIG_TCC_BCHECK -ST_FUNC void gen_bounded_ptr_add(void); -ST_FUNC void gen_bounded_ptr_deref(void); -#endif - /* ------------ x86_64-gen.c ------------ */ #ifdef TCC_TARGET_X86_64 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c); diff --git a/tccgen.c b/tccgen.c index 77a71eeb..5f124cbb 100644 --- a/tccgen.c +++ b/tccgen.c @@ -1866,6 +1866,54 @@ ST_FUNC void gaddrof(void) } #ifdef CONFIG_TCC_BCHECK +/* generate a bounded pointer addition */ +static void gen_bounded_ptr_add(void) +{ + vpush_global_sym(&func_old_type, TOK___bound_ptr_add); + vrott(3); + gfunc_call(2); + vpushi(0); + /* returned pointer is in REG_IRET */ + vtop->r = REG_IRET | VT_BOUNDED; + if (nocode_wanted) + return; + /* relocation offset of the bounding function call point */ + vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW_Rel)); +} + +/* patch pointer addition in vtop so that pointer dereferencing is + also tested */ +static void gen_bounded_ptr_deref(void) +{ + addr_t func; + int size, align; + ElfW_Rel *rel; + Sym *sym; + + if (nocode_wanted) + return; + + size = type_size(&vtop->type, &align); + switch(size) { + case 1: func = TOK___bound_ptr_indir1; break; + case 2: func = TOK___bound_ptr_indir2; break; + case 4: func = TOK___bound_ptr_indir4; break; + case 8: func = TOK___bound_ptr_indir8; break; + case 12: func = TOK___bound_ptr_indir12; break; + case 16: func = TOK___bound_ptr_indir16; break; + default: + /* may happen with struct member access */ + return; + } + sym = external_global_sym(func, &func_old_type); + if (!sym->c) + put_extern_sym(sym, NULL, 0, 0); + /* patch relocation */ + /* XXX: find a better solution ? */ + rel = (ElfW_Rel *)(cur_text_section->reloc->data + vtop->c.i); + rel->r_info = ELFW(R_INFO)(sym->c, ELFW(R_TYPE)(rel->r_info)); +} + /* generate lvalue bound code */ static void gbound(void) { diff --git a/x86_64-gen.c b/x86_64-gen.c index 208669f1..e68ef947 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -662,57 +662,6 @@ static void gen_bounds_call(int v) #endif } -/* generate a bounded pointer addition */ -ST_FUNC void gen_bounded_ptr_add(void) -{ - vpush_global_sym(&func_old_type, TOK___bound_ptr_add); - vrott(3); - gfunc_call(2); - vpushi(0); - /* returned pointer is in rax */ - vtop->r = TREG_RAX | VT_BOUNDED; - if (nocode_wanted) - return; - /* relocation offset of the bounding function call point */ - vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW(Rela))); -} - -/* patch pointer addition in vtop so that pointer dereferencing is - also tested */ -ST_FUNC void gen_bounded_ptr_deref(void) -{ - addr_t func; - int size, align; - ElfW(Rela) *rel; - Sym *sym; - - if (nocode_wanted) - return; - - size = type_size(&vtop->type, &align); - switch(size) { - case 1: func = TOK___bound_ptr_indir1; break; - case 2: func = TOK___bound_ptr_indir2; break; - case 4: func = TOK___bound_ptr_indir4; break; - case 8: func = TOK___bound_ptr_indir8; break; - case 12: func = TOK___bound_ptr_indir12; break; - case 16: func = TOK___bound_ptr_indir16; break; - default: - /* may happen with struct member access */ - return; - //tcc_error("unhandled size when dereferencing bounded pointer"); - //func = 0; - //break; - } - sym = external_global_sym(func, &func_old_type); - if (!sym->c) - put_extern_sym(sym, NULL, 0, 0); - /* patch relocation */ - /* XXX: find a better solution ? */ - rel = (ElfW(Rela) *)(cur_text_section->reloc->data + vtop->c.i); - rel->r_info = ELF64_R_INFO(sym->c, ELF64_R_TYPE(rel->r_info)); -} - #ifdef TCC_TARGET_PE # define TREG_FASTCALL_1 TREG_RCX #else