Move bound functions to tccgen.c

Move gen_bounded_ptr_add() and gen_bounded_ptr_deref() code to tccgen.c
No functional changes.
This commit is contained in:
herman ten brugge 2020-07-12 10:55:40 +02:00
parent 8314119662
commit c1e1c17c0a
8 changed files with 49 additions and 263 deletions

View File

@ -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 */

View File

@ -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 */

View File

@ -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)
{

View File

@ -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) {

View File

@ -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

5
tcc.h
View File

@ -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);

View File

@ -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)
{

View File

@ -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