History

riscv: GOT loads, signed remainder, ELF flags

* support loading sym addresses from GOT: important for weak syms, fixes 104_inline. This is still incomplete, it only works for taking the sym address, not for directly loading/storing into such symbols (i.e. not for VT_LVAL) * another op: '%' * ELF flags: add EF_RISCV_FLOAT_ABI_DOUBLE, which is our ABI.

Michael Matz committed 7 years ago

Commit 31ecaa71 parent b0329ac3 files · +28 −11Browse files

Unified · Split

3 files
  1. riscv64-gen.c +22 −7
  2. riscv64-link.c +4 −4
  3. tccelf.c +2 −0
riscv64-gen.c+22−7
@@ -189,12 +189,22 @@ ST_FUNC void load(int r, SValue *sv) · 188 unmodified lines
189 tcc_error("unimp: load(non-local lval)");189 tcc_error("unimp: load(non-local lval)");
190 }190 }
191 } else if (v == VT_CONST) {191 } else if (v == VT_CONST) {
192 int rb = 0, do32bit = 8;192 int rb = 0, do32bit = 8, doload = 0;
193 assert(!is_float(sv->type.t) && is_ireg(r));193 assert(!is_float(sv->type.t) && is_ireg(r));
194 if (fr & VT_SYM) {194 if (fr & VT_SYM) {
195 static Sym label;195 static Sym label;
196 greloca(cur_text_section, sv->sym, ind,196 if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
197 R_RISCV_PCREL_HI20, sv->c.i);197 greloca(cur_text_section, sv->sym, ind,
198 R_RISCV_PCREL_HI20, sv->c.i);
199 fc = 0;
200 sv->c.i = 0;
201 } else {
202 if (((unsigned)fc + (1 << 11)) >> 12)
203 tcc_error("unimp: large addend for global address");
204 greloca(cur_text_section, sv->sym, ind,
205 R_RISCV_GOT_HI20, 0);
206 doload = 1;
207 }
198 if (!label.v) {208 if (!label.v) {
199 label.v = tok_alloc(".L0 ", 4)->tok;209 label.v = tok_alloc(".L0 ", 4)->tok;
200 label.type.t = VT_VOID | VT_STATIC;210 label.type.t = VT_VOID | VT_STATIC;
@@ -205,8 +215,6 @@ ST_FUNC void load(int r, SValue *sv) · 4 unmodified lines
205 greloca(cur_text_section, &label, ind,215 greloca(cur_text_section, &label, ind,
206 R_RISCV_PCREL_LO12_I, 0);216 R_RISCV_PCREL_LO12_I, 0);
207 rb = rr;217 rb = rr;
208 fc = 0;
209 sv->c.i = 0;
210 do32bit = 0;218 do32bit = 0;
211 }219 }
212 if (is_float(sv->type.t))220 if (is_float(sv->type.t))
@@ -244,7 +252,12 @@ ST_FUNC void load(int r, SValue *sv) · 31 unmodified lines
244 }252 }
245 if (((unsigned)fc + (1 << 11)) >> 12)253 if (((unsigned)fc + (1 << 11)) >> 12)
246 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)254 o(0x37 | (rr << 7) | ((0x800 + fc) & 0xfffff000)), rb = rr; //lui RR, upper(fc)
247 EI(0x13 | do32bit, 0, rr, rb, fc << 20 >> 20); // addi[w] R, x0|R, FC255 if (doload) {
256 EI(0x03, 3, rr, rr, 0); // ld RR, 0(RR)
257 if (fc)
258 EI(0x13 | do32bit, 0, rr, rr, fc << 20 >> 20); // addi[w] R, x0|R, FC
259 } else
260 EI(0x13 | do32bit, 0, rr, rb, fc << 20 >> 20); // addi[w] R, x0|R, FC
248 } else if (v == VT_LOCAL) {261 } else if (v == VT_LOCAL) {
249 assert(is_ireg(r));262 assert(is_ireg(r));
250 if (((unsigned)fc + (1 << 11)) >> 12)263 if (((unsigned)fc + (1 << 11)) >> 12)
@@ -636,7 +649,6 @@ static void gen_opil(int op, int ll) · 385 unmodified lines
636 d = ireg(d);649 d = ireg(d);
637 ll = ll ? 0 : 8;650 ll = ll ? 0 : 8;
638 switch (op) {651 switch (op) {
639 case '%':
640 case TOK_PDIV:652 case TOK_PDIV:
641 default:653 default:
642 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));654 tcc_error("implement me: %s(%s)", __FUNCTION__, get_tok_str(op, NULL));
@@ -671,6 +683,9 @@ static void gen_opil(int op, int ll) · 28 unmodified lines
671 case '|':683 case '|':
672 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (6 << 12)); // or d, a, b684 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (6 << 12)); // or d, a, b
673 break;685 break;
686 case '%':
687 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (6 << 12)); //rem d, a, b
688 break;
674 case TOK_UMOD:689 case TOK_UMOD:
675 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (7 << 12)); //remu d, a, b690 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (7 << 12)); //remu d, a, b
676 break;691 break;
riscv64-link.c+4−4
@@ -19,6 +19,7 @@ · 18 unmodified lines
19 19
20#else /* !TARGET_DEFS_ONLY */20#else /* !TARGET_DEFS_ONLY */
21 21
22//#define DEBUG_RELOC
22#include "tcc.h"23#include "tcc.h"
23 24
24/* Returns 1 for a code relocation, 0 for a data relocation. For unknown25/* Returns 1 for a code relocation, 0 for a data relocation. For unknown
@@ -177,9 +178,7 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, · 152 unmodified lines
177 uint64_t off64;178 uint64_t off64;
178 uint32_t off32;179 uint32_t off32;
179 int sym_index = ELFW(R_SYM)(rel->r_info);180 int sym_index = ELFW(R_SYM)(rel->r_info);
180#ifdef DEBUG_RELOC
181 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];181 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
182#endif
183 182
184 switch(type) {183 switch(type) {
185 case R_RISCV_ALIGN:184 case R_RISCV_ALIGN:
@@ -223,8 +222,9 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, · 37 unmodified lines
223#endif222#endif
224 off64 = (int64_t)(val - addr + 0x800) >> 12;223 off64 = (int64_t)(val - addr + 0x800) >> 12;
225 if ((off64 + ((uint64_t)1 << 20)) >> 21)224 if ((off64 + ((uint64_t)1 << 20)) >> 21)
226 tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx",225 tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx sym=%s",
227 off64, ((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21));226 off64, ((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21),
227 symtab_section->link->data + sym->st_name);
228 write32le(ptr, (read32le(ptr) & 0xfff)228 write32le(ptr, (read32le(ptr) & 0xfff)
229 | ((off64 & 0xfffff) << 12));229 | ((off64 & 0xfffff) << 12));
230 last_hi.addr = addr;230 last_hi.addr = addr;
tccelf.c+2−0
@@ -1910,6 +1910,8 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr, · 1909 unmodified lines
1910#else1910#else
1911 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;1911 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
1912#endif1912#endif
1913#elif defined TCC_TARGET_RISCV64
1914 ehdr.e_flags = EF_RISCV_FLOAT_ABI_DOUBLE;
1913#endif1915#endif
1914 switch(file_type) {1916 switch(file_type) {
1915 default:1917 default:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t