Traditional behaviour on x86-64 is to encode the relocation addend in r_addend, not in the relocated field (after all, that's the reason to use RELA relocs to begin with). Our linker can deal with both, other linkers as well. But using e.g. the GNU assembler one can detect differences (equivalent code in the end, but still a difference). Now there's only a trivial difference in tests/asmtest.S (having to do with ordering of prefixes).
Unified · Split
| @@ -927,7 +927,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 926 unmodified lines | |||
| 927 | qrel->r_offset = rel->r_offset; | 927 | qrel->r_offset = rel->r_offset; |
| 928 | qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32); | 928 | qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32); |
| 929 | /* Use sign extension! */ | 929 | /* Use sign extension! */ |
| 930 | qrel->r_addend = (int)read32le(ptr); | 930 | qrel->r_addend = (int)read32le(ptr) + rel->r_addend; |
| 931 | qrel++; | 931 | qrel++; |
| 932 | break; | 932 | break; |
| 933 | } | 933 | } |
| @@ -256,7 +256,7 @@ ST_FUNC int oad(int c, int s) · 255 unmodified lines | |||
| 256 | ST_FUNC void gen_addr32(int r, Sym *sym, int c) | 256 | ST_FUNC void gen_addr32(int r, Sym *sym, int c) |
| 257 | { | 257 | { |
| 258 | if (r & VT_SYM) | 258 | if (r & VT_SYM) |
| 259 | greloc(cur_text_section, sym, ind, R_X86_64_32); | 259 | greloca(cur_text_section, sym, ind, R_X86_64_32, c), c=0; |
| 260 | gen_le32(c); | 260 | gen_le32(c); |
| 261 | } | 261 | } |
| 262 | 262 | ||
| @@ -264,7 +264,7 @@ ST_FUNC void gen_addr32(int r, Sym *sym, int c) · 1 unmodified line | |||
| 264 | ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c) | 264 | ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c) |
| 265 | { | 265 | { |
| 266 | if (r & VT_SYM) | 266 | if (r & VT_SYM) |
| 267 | greloc(cur_text_section, sym, ind, R_X86_64_64); | 267 | greloca(cur_text_section, sym, ind, R_X86_64_64, c), c=0; |
| 268 | gen_le64(c); | 268 | gen_le64(c); |
| 269 | } | 269 | } |
| 270 | 270 | ||
| @@ -272,7 +272,7 @@ ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c) · 1 unmodified line | |||
| 272 | ST_FUNC void gen_addrpc32(int r, Sym *sym, int c) | 272 | ST_FUNC void gen_addrpc32(int r, Sym *sym, int c) |
| 273 | { | 273 | { |
| 274 | if (r & VT_SYM) | 274 | if (r & VT_SYM) |
| 275 | greloc(cur_text_section, sym, ind, R_X86_64_PC32); | 275 | greloca(cur_text_section, sym, ind, R_X86_64_PC32, c-4), c=4; |
| 276 | gen_le32(c-4); | 276 | gen_le32(c-4); |
| 277 | } | 277 | } |
| 278 | 278 | ||
| @@ -280,12 +280,7 @@ ST_FUNC void gen_addrpc32(int r, Sym *sym, int c) · 1 unmodified line | |||
| 280 | static void gen_gotpcrel(int r, Sym *sym, int c) | 280 | static void gen_gotpcrel(int r, Sym *sym, int c) |
| 281 | { | 281 | { |
| 282 | #ifndef TCC_TARGET_PE | 282 | #ifndef TCC_TARGET_PE |
| 283 | Section *sr; | 283 | greloca(cur_text_section, sym, ind, R_X86_64_GOTPCREL, -4); |
| 284 | ElfW(Rela) *rel; | ||
| 285 | greloc(cur_text_section, sym, ind, R_X86_64_GOTPCREL); | ||
| 286 | sr = cur_text_section->reloc; | ||
| 287 | rel = (ElfW(Rela) *)(sr->data + sr->data_offset - sizeof(ElfW(Rela))); | ||
| 288 | rel->r_addend = -4; | ||
| 289 | #else | 284 | #else |
| 290 | tcc_error("internal error: no GOT on PE: %s %x %x | %02x %02x %02x\n", | 285 | tcc_error("internal error: no GOT on PE: %s %x %x | %02x %02x %02x\n", |
| 291 | get_tok_str(sym->v, NULL), c, r, | 286 | get_tok_str(sym->v, NULL), c, r, |
| @@ -608,16 +603,16 @@ static void gcall_or_jmp(int is_jmp) · 316 unmodified lines | |||
| 608 | if (vtop->r & VT_SYM) { | 603 | if (vtop->r & VT_SYM) { |
| 609 | /* relocation case */ | 604 | /* relocation case */ |
| 610 | #ifdef TCC_TARGET_PE | 605 | #ifdef TCC_TARGET_PE |
| 611 | greloc(cur_text_section, vtop->sym, ind + 1, R_X86_64_PC32); | 606 | greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PC32, (int)(vtop->c.i-4)); |
| 612 | #else | 607 | #else |
| 613 | greloc(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32); | 608 | greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4)); |
| 614 | #endif | 609 | #endif |
| 615 | } else { | 610 | } else { |
| 616 | /* put an empty PC32 relocation */ | 611 | /* put an empty PC32 relocation */ |
| 617 | put_elf_reloc(symtab_section, cur_text_section, | 612 | put_elf_reloca(symtab_section, cur_text_section, |
| 618 | ind + 1, R_X86_64_PC32, 0); | 613 | ind + 1, R_X86_64_PC32, 0, (int)(vtop->c.i-4)); |
| 619 | } | 614 | } |
| 620 | oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */ | 615 | oad(0xe8 + is_jmp, 0); /* call/jmp im */ |
| 621 | } else { | 616 | } else { |
| 622 | /* otherwise, indirect call */ | 617 | /* otherwise, indirect call */ |
| 623 | r = TREG_R11; | 618 | r = TREG_R11; |
| @@ -637,8 +632,8 @@ static unsigned long func_bound_ind; · 13 unmodified lines | |||
| 637 | static void gen_static_call(int v) | 632 | static void gen_static_call(int v) |
| 638 | { | 633 | { |
| 639 | Sym *sym = external_global_sym(v, &func_old_type, 0); | 634 | Sym *sym = external_global_sym(v, &func_old_type, 0); |
| 640 | oad(0xe8, -4); | 635 | oad(0xe8, 0); |
| 641 | greloc(cur_text_section, sym, ind-4, R_X86_64_PC32); | 636 | greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4); |
| 642 | } | 637 | } |
| 643 | 638 | ||
| 644 | /* generate a bounded pointer addition */ | 639 | /* generate a bounded pointer addition */ |
| @@ -991,8 +986,8 @@ void gfunc_epilog(void) · 346 unmodified lines | |||
| 991 | if (v >= 4096) { | 986 | if (v >= 4096) { |
| 992 | Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0); | 987 | Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0); |
| 993 | oad(0xb8, v); /* mov stacksize, %eax */ | 988 | oad(0xb8, v); /* mov stacksize, %eax */ |
| 994 | oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */ | 989 | oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */ |
| 995 | greloc(cur_text_section, sym, ind-4, R_X86_64_PC32); | 990 | greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4); |
| 996 | o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */ | 991 | o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */ |
| 997 | } else { | 992 | } else { |
| 998 | o(0xe5894855); /* push %rbp, mov %rsp, %rbp */ | 993 | o(0xe5894855); /* push %rbp, mov %rsp, %rbp */ |