Skip to content

Commit 9611914

Browse files
committed
Problems solved on for libtcc, pie, riscv64, arm64
I found some problems with a testcase from mailing list. On x86_64 an overflow on reloc R_X86_64_32 occurred that was not reported when using -run -ltcc. The problem could be solved by compiling tcc with -fPIE, -pie or --disable-static. Makefile, configure, libtcc.c, x86_64-link.c: - add --config-pie to configure help. Ignore -pie in libtcc.c and check reloc overflow in x86_64-link.c arm64-gen.c: - Fix reading from constant like '*(int *)0x7fffb7f1280c' elf.h, riscv64-link.c: - fix for -run -ltcc. Ignore reloc R_RISCV_SET_ULEB128 and R_RISCV_SUB_ULEB128 that are used in .debug_loclists section.
1 parent 34eed88 commit 9611914

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ CFLAGS += $(CPPFLAGS)
2424
VPATH = $(TOPSRC)
2525
-LTCC = $(TOP)/$(LIBTCC)
2626

27+
ifeq ($(CONFIG_pie),yes)
28+
CFLAGS += -fPIE
29+
LDFLAGS += -pie
30+
endif
31+
2732
ifdef CONFIG_WIN32
2833
CFG = -win
2934
ifneq ($(CONFIG_static),yes)

arm64-gen.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,17 +503,19 @@ ST_FUNC void load(int r, SValue *sv)
503503
}
504504

505505
if (svr == (VT_CONST | VT_LVAL)) {
506+
uint64_t i = sv->c.i;
507+
506508
if (sv->sym)
507509
arm64_sym(30, sv->sym, // use x30 for address
508-
arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
510+
arm64_check_offset(0, arm64_type_size(svtt), i));
509511
else
510-
arm64_movimm (30, sv->c.i);
512+
arm64_movimm (30, i), i = 0;
511513
if (IS_FREG(r))
512514
arm64_ldrv(arm64_type_size(svtt), fltr(r), 30,
513-
arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
515+
arm64_check_offset(1, arm64_type_size(svtt), i));
514516
else
515517
arm64_ldrx(!(svtt&VT_UNSIGNED), arm64_type_size(svtt), intr(r), 30,
516-
arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
518+
arm64_check_offset(1, arm64_type_size(svtt), i));
517519
return;
518520
}
519521

@@ -621,17 +623,19 @@ ST_FUNC void store(int r, SValue *sv)
621623
}
622624

623625
if (svr == (VT_CONST | VT_LVAL)) {
626+
uint64_t i = sv->c.i;
627+
624628
if (sv->sym)
625629
arm64_sym(30, sv->sym, // use x30 for address
626-
arm64_check_offset(0, arm64_type_size(svtt), sv->c.i));
630+
arm64_check_offset(0, arm64_type_size(svtt), i));
627631
else
628-
arm64_movimm (30, sv->c.i);
632+
arm64_movimm (30, i), i = 0;
629633
if (IS_FREG(r))
630634
arm64_strv(arm64_type_size(svtt), fltr(r), 30,
631-
arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
635+
arm64_check_offset(1, arm64_type_size(svtt), i));
632636
else
633637
arm64_strx(arm64_type_size(svtt), intr(r), 30,
634-
arm64_check_offset(1, arm64_type_size(svtt), sv->c.i));
638+
arm64_check_offset(1, arm64_type_size(svtt), i));
635639
return;
636640
}
637641

configure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Advanced options (experts only):
230230
--config-new_dtags=yes use new ELF DTAGs (DT_RUNPATH instead of DT_RPATH)
231231
--config-codesign=no do not use codesign on apple to sign executables
232232
--config-dwarf=x use dwarf debug info instead of stabs (x=2..5)
233+
--config-pie compile with pie
233234
234235
Cross build options (experimental):
235236
--cpu=CPU target CPU [$cpu]

elf.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3313,8 +3313,12 @@ typedef Elf32_Addr Elf32_Conflict;
33133313
#define R_RISCV_SET16 55
33143314
#define R_RISCV_SET32 56
33153315
#define R_RISCV_32_PCREL 57
3316+
#define R_RISCV_IRELATIVE 58
3317+
#define R_RISCV_PLT32 59
3318+
#define R_RISCV_SET_ULEB128 60
3319+
#define R_RISCV_SUB_ULEB128 61
33163320

3317-
#define R_RISCV_NUM 58
3321+
#define R_RISCV_NUM 62
33183322

33193323

33203324
#endif /* elf.h */

libtcc.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ static const TCCOption tcc_options[] = {
16741674
{ "C", 0, 0 },
16751675
{ "-param", 0, TCC_OPTION_HAS_ARG },
16761676
{ "pedantic", 0, 0 },
1677+
{ "pie", 0, 0 },
16771678
{ "pipe", 0, 0 },
16781679
{ "s", 0, 0 },
16791680
{ "traditional", 0, 0 },

riscv64-link.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ ST_FUNC int code_reloc (int reloc_type)
5151
case R_RISCV_SUB64:
5252
case R_RISCV_32:
5353
case R_RISCV_64:
54+
case R_RISCV_SET_ULEB128:
55+
case R_RISCV_SUB_ULEB128:
5456
return 0;
5557

5658
case R_RISCV_CALL_PLT:
@@ -77,6 +79,8 @@ ST_FUNC int gotplt_entry_type (int reloc_type)
7779
case R_RISCV_ADD16:
7880
case R_RISCV_SUB8:
7981
case R_RISCV_SUB16:
82+
case R_RISCV_SET_ULEB128:
83+
case R_RISCV_SUB_ULEB128:
8084
return NO_GOTPLT_ENTRY;
8185

8286
case R_RISCV_BRANCH:
@@ -367,6 +371,10 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
367371
}
368372
add32le(ptr, val - addr);
369373
return;
374+
case R_RISCV_SET_ULEB128:
375+
case R_RISCV_SUB_ULEB128:
376+
/* ignore. used in section .debug_loclists */
377+
return;
370378
case R_RISCV_COPY:
371379
/* XXX */
372380
return;

x86_64-link.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
221221
qrel->r_addend = (int)read32le(ptr) + val;
222222
qrel++;
223223
}
224+
if ((type == R_X86_64_32 && (unsigned long long)val > 4294967295ULL) ||
225+
(type == R_X86_64_32S &&
226+
((long long)val < -2147483648LL || (long long)val > 2147483647LL)))
227+
tcc_error_noabort("internal error: relocation %d failed", type);
224228
add32le(ptr, val);
225229
break;
226230

@@ -251,7 +255,7 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr,
251255
/* ignore overflow with undefined weak symbols */
252256
if (((ElfW(Sym)*)symtab_section->data)[sym_index].st_shndx != SHN_UNDEF)
253257
#endif
254-
tcc_error_noabort("internal error: relocation failed");
258+
tcc_error_noabort("internal error: relocation %d failed", type);
255259
}
256260
add32le(ptr, diff);
257261
}

0 commit comments

Comments
 (0)