Skip to content

Commit 1bcec04

Browse files
author
grischka
committed
TLS bits
tccelf.c: - set_elf_sym() check NOBITS instead of just .bss - sort_sections() must have common alignment for tls sections fix BSD relro tccpe.c: - sort .tbss after .tdata tccrun.c: - TLS not supported with -run - arm64: FlushInstructionCache on win32 too lib/bt-exe.c: - use less unused bss memory (from global vars in tcc.h) arm64-gen.c: - arm64_sym(): direct access (no GOT) for 'static' vars relocate with 'addend' unless GOT - reserve X16/x17 for internal usage (specifically to avoid their usage as value register passed to store())
1 parent 2ba12e8 commit 1bcec04

5 files changed

Lines changed: 99 additions & 93 deletions

File tree

‎arm64-gen.c‎

Lines changed: 51 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,10 @@ ST_DATA const int reg_classes[NB_REGS] = {
7777
RC_INT | RC_R(13),
7878
RC_INT | RC_R(14),
7979
RC_INT | RC_R(15),
80-
RC_INT | RC_R(16),
81-
RC_INT | RC_R(17),
82-
#ifdef TCC_TARGET_PE
83-
RC_R(18), /* (x18 reserved on Windows) */
84-
#else
85-
RC_INT | RC_R(18),
86-
#endif
87-
RC_R30, // not in RC_INT as we make special use of x30
80+
RC_R(16), /* reserved for generator (arm64_sym()) */
81+
RC_R(17), /* reserved for generator (arm64_sym()) */
82+
RC_R(18), /* reserved on Windows/OSX */
83+
RC_R30, /* not in RC_INT as we make special use of x30 */
8884
RC_FLOAT | RC_F(0),
8985
RC_FLOAT | RC_F(1),
9086
RC_FLOAT | RC_F(2),
@@ -298,6 +294,7 @@ static void arm64_spoff(int reg, uint64_t off)
298294

299295
static uint64_t arm64_check_offset(int sz_, uint64_t off)
300296
{
297+
#ifndef TCC_TARGET_PE
301298
uint32_t sz = sz_;
302299
uint64_t scaled_mask = 0xffful << sz;
303300
if (!(off & ~scaled_mask) || (off < 256 || -off <= 256))
@@ -306,6 +303,7 @@ static uint64_t arm64_check_offset(int sz_, uint64_t off)
306303
return scaled_mask;
307304
if (off & 0x1fful)
308305
return 0x1fful;
306+
#endif
309307
return 0;
310308
}
311309

@@ -463,12 +461,17 @@ static void arm64_strv(int sz_, int dst, int bas, uint64_t off)
463461
}
464462
}
465463

464+
static void arm64_reloca(Sym *sym, int reloc_type, uint64_t addend)
465+
{
466+
greloca(cur_text_section, sym, ind, reloc_type, addend);
467+
}
468+
466469
static void arm64_sym(int r, Sym *sym, addr_t addend)
467470
{
468471
if (sym->type.t & VT_TLS) {
469-
#if TCC_TARGET_PE
472+
#ifdef TCC_TARGET_PE
470473
Sym *s2 = external_global_sym(TOK___tls_index, &int_type);
471-
int r2 = get_reg(RC_INT);
474+
int r2 = get_reg(RC_R(16));
472475
arm64_sym(30, s2, 0);
473476
o(0xb94003de); // ldr w30, [x30]
474477
o(0xf9402e40 | r2); // ldr r2, [x18, #88]
@@ -477,46 +480,35 @@ static void arm64_sym(int r, Sym *sym, addr_t addend)
477480
#else
478481
o(0xd53bd040 | r); /* mrs xr, tpidr_el0 */
479482
#endif
480-
greloca(cur_text_section, sym, ind, R_AARCH64_TLSLE_ADD_TPREL_HI12, 0);
481-
/* add xr, xr, #0, lsl #12 */
482-
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_SH(1) | ARM64_RN(r) | ARM64_RD(r));
483-
greloca(cur_text_section, sym, ind, R_AARCH64_TLSLE_ADD_TPREL_LO12, 0);
484-
/* add xr, xr, #0 */
485-
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RN(r) | ARM64_RD(r));
486-
goto add_addend;
487-
}
488-
#ifdef TCC_TARGET_PE
489-
/* PE links symbol addresses directly; there is no ELF-style GOT here. */
490-
greloca(cur_text_section, sym, ind, R_AARCH64_ADR_PREL_PG_HI21, 0);
491-
o(ARM64_ADRP | r); // adrp xr, #sym
492-
greloca(cur_text_section, sym, ind, R_AARCH64_ADD_ABS_LO12_NC, 0);
493-
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RN(r) | r); // add xr, xr, #sym
494-
#else
495-
greloca(cur_text_section, sym, ind, R_AARCH64_ADR_GOT_PAGE, 0);
496-
o(ARM64_ADRP | r); // adrp xr, #sym
497-
greloca(cur_text_section, sym, ind, R_AARCH64_LD64_GOT_LO12_NC, 0);
498-
o(ARM64_LDR_X | ARM64_RN(r) | r); // ld xr,[xr, #sym]
499-
#endif
500-
add_addend:
501-
if (addend) {
502-
// add xr, xr, #addend
503-
if (addend & 0xffful)
504-
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RN(r) | r |
505-
(addend & 0xfff) << 10);
506-
if (addend > 0xffful) {
507-
// add xr, xr, #addend, lsl #12
508-
if (addend & 0xfff000ul)
509-
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_SH(1) |
510-
ARM64_RN(r) | r | ((addend >> 12) & 0xfff) << 10);
511-
if (addend > 0xfffffful) {
512-
/* very unlikely */
513-
int t = r ? 0 : 1;
514-
o(ARM64_STR_X_PRE | 0x001F0FE0U | t); /* str xt, [sp, #-16]! */
515-
arm64_movimm(t, addend & ~0xffffffull); // use xt for addent
516-
o(ARM64_ADD_REG | ARM64_SF(1) | ARM64_RM(t) | ARM64_RN(r) | r); /* add xr, xr, xt */
517-
o(ARM64_LDR_X_POST | 0x000107E0U | t); /* ldr xt, [sp], #16 */
518-
}
483+
arm64_reloca(sym, R_AARCH64_TLSLE_ADD_TPREL_HI12, addend);
484+
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_SH(1) | ARM64_RN(r) | ARM64_RD(r)); /* add xr, xr, #0, lsl #12 */
485+
arm64_reloca(sym, R_AARCH64_TLSLE_ADD_TPREL_LO12, addend);
486+
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RN(r) | ARM64_RD(r)); /* add xr, xr, #0 */
487+
#ifndef TCC_TARGET_PE /* no elf-style GOT on Windows */
488+
} else if (!(sym->type.t & VT_STATIC)) {
489+
arm64_reloca(sym, R_AARCH64_ADR_GOT_PAGE, 0);
490+
o(ARM64_ADRP | r); /* adrp xr, #sym */
491+
arm64_reloca(sym, R_AARCH64_LD64_GOT_LO12_NC, 0);
492+
o(ARM64_LDR_X | ARM64_RN(r) | r); /* ld xr,[xr, #sym] */
493+
if (addend > 0xffffff) {
494+
int t = get_reg(RC_R(16));
495+
arm64_movimm(t, addend); // use xt for addent
496+
o(ARM64_ADD_REG | ARM64_SF(1) | ARM64_RM(t) | ARM64_RN(r) | r); /* add xr, xr, xt */
497+
} else {
498+
if (addend & 0xfff) /* add xr, xr, #addend */
499+
o(ARM64_ADD_IMM | ARM64_SF(1)
500+
| ARM64_RN(r) | r | (addend & 0xfff) << 10);
501+
addend >>= 12;
502+
if (addend & 0xfff) /* add xr, xr, #addend, lsl #12 */
503+
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_SH(1)
504+
| ARM64_RN(r) | r | (addend & 0xfff) << 10);
519505
}
506+
#endif
507+
} else {
508+
arm64_reloca(sym, R_AARCH64_ADR_PREL_PG_HI21, addend);
509+
o(ARM64_ADRP | r); /* adrp xr, #sym */
510+
arm64_reloca(sym, R_AARCH64_ADD_ABS_LO12_NC, addend);
511+
o(ARM64_ADD_IMM | ARM64_SF(1) | ARM64_RN(r) | r); /* add xr, xr, #sym */
520512
}
521513
}
522514

@@ -678,8 +670,8 @@ ST_FUNC void store(int r, SValue *sv)
678670

679671
static void arm64_gen_bl_or_b(int b)
680672
{
681-
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
682-
greloca(cur_text_section, vtop->sym, ind,
673+
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM)) {
674+
arm64_reloca(vtop->sym,
683675
b ? R_AARCH64_JUMP26 : R_AARCH64_CALL26, 0);
684676
o(b ? ARM64_B : ARM64_BL); // b/bl .
685677
}
@@ -691,16 +683,16 @@ static void arm64_gen_bl_or_b(int b)
691683
}
692684
}
693685

694-
#if defined(CONFIG_TCC_BCHECK)
695-
696-
static void gen_bounds_call(int v)
686+
#if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
687+
static void gen_static_call(int v)
697688
{
698689
Sym *sym = external_helper_sym(v);
699-
700-
greloca(cur_text_section, sym, ind, R_AARCH64_CALL26, 0);
690+
arm64_reloca(sym, R_AARCH64_CALL26, 0);
701691
o(ARM64_BL); // bl
702692
}
693+
#endif
703694

695+
#if defined(CONFIG_TCC_BCHECK)
704696
static void gen_bounds_prolog(void)
705697
{
706698
/* leave some room for bound checking code */
@@ -735,15 +727,15 @@ static void gen_bounds_epilog(void)
735727
saved_ind = ind;
736728
ind = func_bound_ind;
737729
arm64_sym(0, sym_data, 0);
738-
gen_bounds_call(TOK___bound_local_new);
730+
gen_static_call(TOK___bound_local_new);
739731
ind = saved_ind;
740732
}
741733

742734
/* generate bound check local freeing */
743735
o(0xa9bf07e0); /* stp x0, x1, [sp, #-16]! */
744736
o(0x3c9f0fe0); /* str q0, [sp, #-16]! */
745737
arm64_sym(0, sym_data, 0);
746-
gen_bounds_call(TOK___bound_local_delete);
738+
gen_static_call(TOK___bound_local_delete);
747739
o(0x3cc107e0); /* ldr q0, [sp], #16 */
748740
o(0xa8c107e0); /* ldp x0, x1, [sp], #16 */
749741
}
@@ -1004,11 +996,8 @@ static void arm64_sub_sp(uint64_t diff)
1004996
return;
1005997
#ifdef TCC_TARGET_PE
1006998
if (diff >= 4096) {
1007-
Sym *sym = external_helper_sym(TOK___chkstk);
1008-
1009999
arm64_movimm(15, diff >> 4);
1010-
greloca(cur_text_section, sym, ind, R_AARCH64_CALL26, 0);
1011-
o(ARM64_BL); // bl __chkstk
1000+
gen_static_call(TOK___chkstk);
10121001
o(0xcb2f73ff); // sub sp,sp,x15,lsl #4
10131002
return;
10141003
}

‎lib/bt-exe.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from tccrun.c into executables. */
44

55
#define CONFIG_TCC_BACKTRACE_ONLY
6-
#define ONE_SOURCE 1
6+
#define ONE_SOURCE 0
77
#define pstrcpy tcc_pstrcpy
88
#include "../tccrun.c"
99

‎tccelf.c‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ version_add (TCCState *s1)
693693
}
694694
#endif /* ndef ELF_OBJ_ONLY */
695695

696+
/* catch .tbss also */
697+
static int IS_BSS(TCCState *s1, int ndx)
698+
{
699+
return ndx == SHN_COMMON
700+
|| (ndx < s1->nb_sections
701+
&& s1->sections[ndx]->sh_type == SHT_NOBITS);
702+
}
703+
696704
/* add an elf symbol : check if it is already defined and patch
697705
it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
698706
ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
@@ -744,13 +752,10 @@ ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
744752
/* ignore hidden symbols after */
745753
} else if (s->sh_flags & SHF_DYNSYM) {
746754
/* we accept that two DLL define the same symbol */
747-
} else if ((esym->st_shndx == SHN_COMMON
748-
|| esym->st_shndx == bss_section->sh_num)
749-
&& (shndx < SHN_LORESERVE
750-
&& shndx != bss_section->sh_num)) {
755+
} else if (!IS_BSS(s1, shndx) && IS_BSS(s1, esym->st_shndx)) {
751756
/* data symbol gets precedence over common/bss */
752757
goto do_patch;
753-
} else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
758+
} else if (IS_BSS(s1, shndx)) {
754759
/* data symbol keeps precedence over common/bss */
755760
} else if (esym->st_other & ST_ASM_SET) {
756761
/* If the existing symbol came from an asm .set
@@ -2259,6 +2264,7 @@ static int sort_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
22592264
int i, j, k, f, f0, n;
22602265
int nb_sections = s1->nb_sections;
22612266
int *sec_cls = sec_order + nb_sections;
2267+
int tls_align = 0; /* need common alignment for all tls sections */
22622268

22632269
for (i = 1; i < nb_sections; i++) {
22642270
s = s1->sections[i];
@@ -2294,6 +2300,8 @@ static int sort_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
22942300
k = 0x60;
22952301
/* RELRO sections --> */
22962302
} else if (s->sh_flags & SHF_TLS) {
2303+
if (s->sh_addralign > tls_align)
2304+
tls_align = s->sh_addralign;
22972305
k = 0x40 + (s->sh_type == SHT_NOBITS);
22982306
} else if (s->sh_type == SHT_PREINIT_ARRAY) {
22992307
k = 0x42;
@@ -2302,11 +2310,11 @@ static int sort_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
23022310
} else if (s->sh_type == SHT_FINI_ARRAY) {
23032311
k = 0x44;
23042312
} else if (s->sh_type == SHT_DYNAMIC) {
2305-
k = 0x46;
2313+
k = 0x48;
23062314
} else if (s == s1->got) {
2307-
k = 0x47; /* .got as RELRO needs BIND_NOW in DT_FLAGS */
2315+
k = 0x49; /* .got as RELRO needs BIND_NOW in DT_FLAGS */
23082316
} else if (s->reloc && (s->reloc->sh_flags & SHF_ALLOC) && j == 0x100) {
2309-
k = 0x45;
2317+
k = 0x45; /* +1 for data.ro */
23102318
/* <-- */
23112319
} else if (s->sh_type == SHT_NOTE) {
23122320
k = 0x08, d->notes = 1;
@@ -2349,15 +2357,18 @@ static int sort_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
23492357
/* NetBSD only supports 2 PT_LOAD sections.
23502358
See: https://blog.netbsd.org/tnf/entry/the_first_report_on_lld */
23512359
if ((f & SHF_WRITE) == 0)
2352-
f |= SHF_EXECINSTR, k = 0; /* no relro */
2360+
f |= SHF_EXECINSTR;
2361+
k = 0; /* no relro */
23532362
#endif
2363+
if ((k & 0xfff0) == 0x240) /* RELRO sections */
2364+
d->relro = 1, f |= SHFX_RELRO;
23542365
/* start new header when flags changed, but avoid zero memsz */
23552366
if (f != f0 && s->sh_size)
23562367
f0 = f, ++n, f |= SHFX_NEWPH;
2357-
if ((s->sh_flags & SHF_TLS) && s->sh_size)
2368+
if ((s->sh_flags & SHF_TLS) && s->sh_size) {
2369+
s->sh_addralign = tls_align;
23582370
d->tls = 1, f |= SHF_TLS;
2359-
if ((k & 0xfff0) == 0x240) /* RELRO sections */
2360-
d->relro = 1, f |= SHFX_RELRO;
2371+
}
23612372
}
23622373
sec_cls[i] = f;
23632374
//printf("ph %d sec %02d : %3X %3X %8.2X %04X %s\n", (f>0) * n, i, f, k, s->sh_type, (int)s->sh_size, s->name);
@@ -2484,7 +2495,7 @@ static int layout_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
24842495
s->sh_offset = file_offset;
24852496
s->sh_addr = addr;
24862497

2487-
//printf("%d : %08x %08x %04x %03x %s\n", (int)(ph - d->phdr), (int)file_offset, (int)addr, (int)s->sh_size, s->sh_type, s->name);
2498+
//printf("%d : %08x %08x %04x %03x %s %d\n", n, (int)file_offset, (int)addr, (int)s->sh_size, s->sh_type, s->name, align + 1);
24882499
addr += s->sh_size;
24892500
if (s->sh_type != SHT_NOBITS)
24902501
file_offset += s->sh_size;
@@ -2516,8 +2527,6 @@ static int layout_sections(TCCState *s1, int *sec_order, struct dyn_inf *d)
25162527
}
25172528
if (f & SHF_TLS) {
25182529
ph2 = update_phdr(&d->phdr[d->tls], PT_TLS, s, addr, file_offset);
2519-
if (s->sh_addralign > ph2->p_align)
2520-
ph2->p_align = s->sh_addralign;
25212530
if (s->sh_type == SHT_NOBITS)
25222531
addr -= s->sh_size;
25232532
/* for xxx-link.c:relocate() */

0 commit comments

Comments
 (0)