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
189189 tcc_error("unimp: load(non-local lval)");
190190 }
191191 } else if (v == VT_CONST) {
192− int rb = 0, do32bit = 8;
192+ int rb = 0, do32bit = 8, doload = 0;
193193 assert(!is_float(sv->type.t) && is_ireg(r));
194194 if (fr & VT_SYM) {
195195 static Sym label;
196− greloca(cur_text_section, sv->sym, ind,
197− R_RISCV_PCREL_HI20, sv->c.i);
196+ if (sv->sym->type.t & VT_STATIC) { // XXX do this per linker relax
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+ }
198208 if (!label.v) {
199209 label.v = tok_alloc(".L0 ", 4)->tok;
200210 label.type.t = VT_VOID | VT_STATIC;
@@ -205,8 +215,6 @@ ST_FUNC void load(int r, SValue *sv) · 4 unmodified lines
205215 greloca(cur_text_section, &label, ind,
206216 R_RISCV_PCREL_LO12_I, 0);
207217 rb = rr;
208− fc = 0;
209− sv->c.i = 0;
210218 do32bit = 0;
211219 }
212220 if (is_float(sv->type.t))
@@ -244,7 +252,12 @@ ST_FUNC void load(int r, SValue *sv) · 31 unmodified lines
244252 }
245253 if (((unsigned)fc + (1 << 11)) >> 12)
246254 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, FC
255+ 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
248261 } else if (v == VT_LOCAL) {
249262 assert(is_ireg(r));
250263 if (((unsigned)fc + (1 << 11)) >> 12)
@@ -636,7 +649,6 @@ static void gen_opil(int op, int ll) · 385 unmodified lines
636649 d = ireg(d);
637650 ll = ll ? 0 : 8;
638651 switch (op) {
639− case '%':
640652 case TOK_PDIV:
641653 default:
642654 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
671683 case '|':
672684 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (6 << 12)); // or d, a, b
673685 break;
686+ case '%':
687+ o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (6 << 12)); //rem d, a, b
688+ break;
674689 case TOK_UMOD:
675690 o(0x33 | (d << 7) | (a << 15) | (b << 20) | (0x01 << 25) | (7 << 12)); //remu d, a, b
676691 break;
riscv64-link.c+4−4
@@ -19,6 +19,7 @@ · 18 unmodified lines
1919
2020 #else /* !TARGET_DEFS_ONLY */
2121
22+//#define DEBUG_RELOC
2223 #include "tcc.h"
2324
2425 /* 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
177178 uint64_t off64;
178179 uint32_t off32;
179180 int sym_index = ELFW(R_SYM)(rel->r_info);
180−#ifdef DEBUG_RELOC
181181 ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
182−#endif
183182
184183 switch(type) {
185184 case R_RISCV_ALIGN:
@@ -223,8 +222,9 @@ void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, · 37 unmodified lines
223222 #endif
224223 off64 = (int64_t)(val - addr + 0x800) >> 12;
225224 if ((off64 + ((uint64_t)1 << 20)) >> 21)
226− tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx",
227− off64, ((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21));
225+ tcc_error("R_RISCV_PCREL_HI20 relocation failed: off=%lx cond=%lx sym=%s",
226+ off64, ((int64_t)(off64 + ((uint64_t)1 << 20)) >> 21),
227+ symtab_section->link->data + sym->st_name);
228228 write32le(ptr, (read32le(ptr) & 0xfff)
229229 | ((off64 & 0xfffff) << 12));
230230 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
19101910 #else
19111911 ehdr.e_ident[EI_OSABI] = ELFOSABI_ARM;
19121912 #endif
1913+#elif defined TCC_TARGET_RISCV64
1914+ ehdr.e_flags = EF_RISCV_FLOAT_ABI_DOUBLE;
19131915 #endif
19141916 switch(file_type) {
19151917 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