History

x86_64: Use addend on relocs

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).

Michael Matz committed 10 years ago

Commit 9645b621 parent 5e47b082 files · +14 −19Browse files

Unified · Split

2 files
  1. tccelf.c +1 −1
  2. x86_64-gen.c +13 −18
tccelf.c+1−1
@@ -927,7 +927,7 @@ ST_FUNC void relocate_section(TCCState *s1, Section *s) · 926 unmodified lines
927927 qrel->r_offset = rel->r_offset;
928928 qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
929929 /* Use sign extension! */
930− qrel->r_addend = (int)read32le(ptr);
930+ qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
931931 qrel++;
932932 break;
933933 }
x86_64-gen.c+13−18
@@ -256,7 +256,7 @@ ST_FUNC int oad(int c, int s) · 255 unmodified lines
256256 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
257257 {
258258 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;
260260 gen_le32(c);
261261 }
262262
@@ -264,7 +264,7 @@ ST_FUNC void gen_addr32(int r, Sym *sym, int c) · 1 unmodified line
264264 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c)
265265 {
266266 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;
268268 gen_le64(c);
269269 }
270270
@@ -272,7 +272,7 @@ ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c) · 1 unmodified line
272272 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
273273 {
274274 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;
276276 gen_le32(c-4);
277277 }
278278
@@ -280,12 +280,7 @@ ST_FUNC void gen_addrpc32(int r, Sym *sym, int c) · 1 unmodified line
280280 static void gen_gotpcrel(int r, Sym *sym, int c)
281281 {
282282 #ifndef TCC_TARGET_PE
283− Section *sr;
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;
283+ greloca(cur_text_section, sym, ind, R_X86_64_GOTPCREL, -4);
289284 #else
290285 tcc_error("internal error: no GOT on PE: %s %x %x | %02x %02x %02x\n",
291286 get_tok_str(sym->v, NULL), c, r,
@@ -608,16 +603,16 @@ static void gcall_or_jmp(int is_jmp) · 316 unmodified lines
608603 if (vtop->r & VT_SYM) {
609604 /* relocation case */
610605 #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));
612607 #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));
614609 #endif
615610 } else {
616611 /* put an empty PC32 relocation */
617− put_elf_reloc(symtab_section, cur_text_section,
618− ind + 1, R_X86_64_PC32, 0);
612+ put_elf_reloca(symtab_section, cur_text_section,
613+ ind + 1, R_X86_64_PC32, 0, (int)(vtop->c.i-4));
619614 }
620− oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
615+ oad(0xe8 + is_jmp, 0); /* call/jmp im */
621616 } else {
622617 /* otherwise, indirect call */
623618 r = TREG_R11;
@@ -637,8 +632,8 @@ static unsigned long func_bound_ind; · 13 unmodified lines
637632 static void gen_static_call(int v)
638633 {
639634 Sym *sym = external_global_sym(v, &func_old_type, 0);
640− oad(0xe8, -4);
641− greloc(cur_text_section, sym, ind-4, R_X86_64_PC32);
635+ oad(0xe8, 0);
636+ greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
642637 }
643638
644639 /* generate a bounded pointer addition */
@@ -991,8 +986,8 @@ void gfunc_epilog(void) · 346 unmodified lines
991986 if (v >= 4096) {
992987 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
993988 oad(0xb8, v); /* mov stacksize, %eax */
994− oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
995− greloc(cur_text_section, sym, ind-4, R_X86_64_PC32);
989+ oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */
990+ greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
996991 o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
997992 } else {
998993 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */

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