From 8a0ccbbd94d77f6dc453056be8d0b17a43249344 Mon Sep 17 00:00:00 2001 From: herman ten brugge Date: Sun, 3 Aug 2025 10:22:26 +0200 Subject: [PATCH] Another update for clang -fsanitize arm-link.c: remove some casts tccelf.c: code style and remove code tccpp.c: use correct pointer size so cross compiling works riscv64-gen.c: make code more readable --- arm-link.c | 14 +++++++------- riscv64-gen.c | 23 ++++++++++++----------- tccelf.c | 6 +++--- tccpp.c | 6 ++++-- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/arm-link.c b/arm-link.c index 4a35559c..16e1549d 100644 --- a/arm-link.c +++ b/arm-link.c @@ -204,7 +204,7 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, x = (unsigned) x << 2; blx_avail = (CONFIG_TCC_CPUVER >= 5); is_thumb = val & 1; - is_bl = ((unsigned) read32le(ptr)) >> 24 == 0xeb; + is_bl = read32le(ptr) >> 24 == 0xeb; is_call = (type == R_ARM_CALL || (type == R_ARM_PC24 && is_bl)); x += val - addr; #ifdef DEBUG_RELOC @@ -241,8 +241,8 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, return; /* Get initial offset */ - hi = (uint16_t) read16le(ptr); - lo = (uint16_t) read16le(ptr+2); + hi = read16le(ptr); + lo = read16le(ptr+2); s = (hi >> 10) & 1; j1 = (lo >> 13) & 1; j2 = (lo >> 11) & 1; @@ -313,11 +313,11 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, j2 = s ^ (i2 ^ 1); imm10 = (x >> 12) & 0x3ff; imm11 = (x >> 1) & 0x7ff; - write16le(ptr, (uint16_t) ((hi & 0xf800) | - (s << 10) | imm10)); - write16le(ptr+2, (uint16_t) ((lo & 0xc000) | + write16le(ptr, (hi & 0xf800) | + (s << 10) | imm10); + write16le(ptr+2, (lo & 0xc000) | (j1 << 13) | blx_bit | (j2 << 11) | - imm11)); + imm11); } return; case R_ARM_MOVT_ABS: diff --git a/riscv64-gen.c b/riscv64-gen.c index a3705666..7b27e5ff 100644 --- a/riscv64-gen.c +++ b/riscv64-gen.c @@ -36,6 +36,7 @@ #include #define UPPER(x) (((unsigned)(x) + 0x800u) & 0xfffff000) +#define LOW_OVERFLOW(x) UPPER(x) #define SIGN7(x) ((((x) & 0xff) ^ 0x80) - 0x80) #define SIGN11(x) ((((x) & 0xfff) ^ 0x800) - 0x800) @@ -135,14 +136,14 @@ static void ER(uint32_t opcode, uint32_t func3, static void EI(uint32_t opcode, uint32_t func3, uint32_t rd, uint32_t rs1, uint32_t imm) { - assert(! ((imm + (1 << 11)) >> 12)); + assert(! LOW_OVERFLOW(imm)); EIu(opcode, func3, rd, rs1, imm); } static void ES(uint32_t opcode, uint32_t func3, uint32_t rs1, uint32_t rs2, uint32_t imm) { - assert(! ((imm + (1 << 11)) >> 12)); + assert(! LOW_OVERFLOW(imm)); o(opcode | (func3 << 12) | ((imm & 0x1f) << 7) | (rs1 << 15) | (rs2 << 20) | ((imm >> 5) << 25)); } @@ -158,7 +159,7 @@ ST_FUNC void gsym_addr(int t_, int a_) uint32_t r = a - t, imm; if ((r + (1 << 21)) & ~((1U << 22) - 2)) tcc_error("out-of-range branch chain"); - imm = (((r >> 12) & 0xff) << 12) + imm = (((r >> 12) & 0xff) << 12) | (((r >> 11) & 1) << 20) | (((r >> 1) & 0x3ff) << 21) | (((r >> 20) & 1) << 31); @@ -179,7 +180,7 @@ static int load_symofs(int r, SValue *sv, int forstore, int *new_fc) R_RISCV_PCREL_HI20, sv->c.i); *new_fc = 0; } else { - if (((unsigned)fc + (1 << 11)) >> 12){ + if (LOW_OVERFLOW(fc)){ large_addend = 1; } greloca(cur_text_section, sv->sym, ind, @@ -206,7 +207,7 @@ static int load_symofs(int r, SValue *sv, int forstore, int *new_fc) rr = 8; // s0 if (fc != sv->c.i) tcc_error("unimp: store(giant local off) (0x%lx)", (long)sv->c.i); - if (((unsigned)fc + (1 << 11)) >> 12) { + if (LOW_OVERFLOW(fc)) { rr = is_ireg(r) ? ireg(r) : 5; // t0 o(0x37 | (rr << 7) | UPPER(fc)); //lui RR, upper(fc) ER(0x33, 0, rr, rr, 8, 0); // add RR, RR, s0 @@ -251,7 +252,7 @@ ST_FUNC void load(int r, SValue *sv) br = load_symofs(r, sv, 0, &fc); } else if (v < VT_CONST) { br = ireg(v); - /*if (((unsigned)fc + (1 << 11)) >> 12) + /*if (LOW_OVERFLOW(fc)) tcc_error("unimp: load(large addend) (0x%x)", fc);*/ fc = 0; // XXX store ofs in LVAL(reg) } else if (v == VT_LLOCAL) { @@ -297,7 +298,7 @@ ST_FUNC void load(int r, SValue *sv) zext = 1; } } - if (((unsigned)fc + (1 << 11)) >> 12) + if (LOW_OVERFLOW(fc)) o(0x37 | (rr << 7) | UPPER(fc)), rb = rr; //lui RR, upper(fc) if (fc || (rr != rb) || do32bit || (fr & VT_SYM)) EI(0x13 | do32bit, 0, rr, rb, SIGN11(fc)); // addi[w] R, x0|R, FC @@ -392,7 +393,7 @@ ST_FUNC void store(int r, SValue *sv) ptrreg = load_symofs(-1, sv, 1, &fc); } else if (fr < VT_CONST) { ptrreg = ireg(fr); - /*if (((unsigned)fc + (1 << 11)) >> 12) + /*if (LOW_OVERFLOW(fc)) tcc_error("unimp: store(large addend) (0x%x)", fc);*/ fc = 0; // XXX support offsets regs } else if (fr == VT_CONST) { @@ -1014,7 +1015,7 @@ static void gen_opil(int op, int ll) ll = ll ? 0 : 8; if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { int fc = vtop->c.i; - if (fc == vtop->c.i && !(((unsigned)fc + (1 << 11)) >> 12)) { + if (fc == vtop->c.i && !LOW_OVERFLOW(fc)) { int cll = 0; int m = ll ? 31 : 63; vswap(); @@ -1381,7 +1382,7 @@ ST_FUNC void ggoto(void) ST_FUNC void gen_vla_sp_save(int addr) { - if (((unsigned)addr + (1 << 11)) >> 12) { + if (LOW_OVERFLOW(addr)) { o(0x37 | (5 << 7) | UPPER(addr)); //lui t0,upper(addr) ER(0x33, 0, 5, 5, 8, 0); // add t0, t0, s0 ES(0x23, 3, 5, 2, SIGN11(addr)); // sd sp, fc(t0) @@ -1392,7 +1393,7 @@ ST_FUNC void gen_vla_sp_save(int addr) ST_FUNC void gen_vla_sp_restore(int addr) { - if (((unsigned)addr + (1 << 11)) >> 12) { + if (LOW_OVERFLOW(addr)) { o(0x37 | (5 << 7) | UPPER(addr)); //lui t0,upper(addr) ER(0x33, 0, 5, 5, 8, 0); // add t0, t0, s0 EI(0x03, 3, 2, 5, SIGN11(addr)); // ld sp, fc(t0) diff --git a/tccelf.c b/tccelf.c index 06d47658..a8232c8a 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1129,7 +1129,8 @@ static void relocate_section(TCCState *s1, Section *s, Section *sr) qrel = (ElfW_Rel *)sr->data; for_each_elem(sr, 0, rel, ElfW_Rel) { - if (s->data == NULL) continue; /* bss */ + if (s->data == NULL) /* bss */ + continue; ptr = s->data + rel->r_offset; sym_index = ELFW(R_SYM)(rel->r_info); sym = &((ElfW(Sym) *)symtab_section->data)[sym_index]; @@ -3310,7 +3311,6 @@ invalid: /* resolve symbols */ old_to_new_syms = tcc_mallocz(nb_syms * sizeof(int)); - if (nb_syms == 0) goto skip; sym = symtab + 1; for(i = 1; i < nb_syms; i++, sym++) { if (sym->st_shndx != SHN_UNDEF && @@ -3343,7 +3343,7 @@ invalid: sym->st_shndx, name); old_to_new_syms[i] = sym_index; } -skip: + /* third pass to patch relocation entries */ for(i = 1; i < ehdr.e_shnum; i++) { s = sm_table[i].s; diff --git a/tccpp.c b/tccpp.c index 6d28a766..8a496667 100644 --- a/tccpp.c +++ b/tccpp.c @@ -117,6 +117,8 @@ ST_FUNC void expect(const char *msg) #define USE_TAL +#define POINTER_SIZE sizeof(void *) + #ifndef USE_TAL #define tal_free(al, p) tcc_free(p) #define tal_realloc(al, p, size) tcc_realloc(p, size) @@ -157,7 +159,7 @@ typedef struct TinyAlloc { } TinyAlloc; typedef struct tal_header_t { - ALIGNED(PTR_SIZE) unsigned size; + ALIGNED(POINTER_SIZE) unsigned size; #ifdef TAL_DEBUG int line_num; /* negative line_num used for double free check */ char file_name[TAL_DEBUG_FILE_LEN + 1]; @@ -246,7 +248,7 @@ static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_ tal_header_t *header; void *ret; int is_own; - unsigned adj_size = (size + PTR_SIZE - 1) & -PTR_SIZE; + unsigned adj_size = (size + POINTER_SIZE - 1) & -POINTER_SIZE; TinyAlloc *al = *pal; tail_call: