History

feat(arm): add TLS Local Exec code generation and linker relocations

Meng Zhuo committed 6 months ago

Commit 841ce0d1 parent 8abaf103 files · +54 −6Browse files

Unified · Split

3 files
  1. arm-gen.c +20 −4
  2. arm-link.c +32 −0
  3. tests/tests2/Makefile +2 −2
arm-gen.c+20−4
@@ -572,7 +572,7 @@ static void load_value(SValue *sv, int r) · 571 unmodified lines
572572 void load(int r, SValue *sv)
573573 {
574574 int v, ft, fc, fr, sign;
575− uint32_t op;
575+ uint32_t op, base;
576576 SValue v1;
577577
578578 fr = sv->r;
@@ -588,7 +588,15 @@ void load(int r, SValue *sv) · 9 unmodified lines
588588
589589 v = fr & VT_VALMASK;
590590 if (fr & VT_LVAL) {
591− uint32_t base = 0xB; // fp
591+ if ((fr & VT_SYM) && sv->sym->type.t & VT_TLS) {
592+ uint32_t op;
593+ o(0xee1d0fe0); /* mrc p15, 0, lr, c13, c0, 3 */
594+ op = 0xe510e000; /* ldr r, [lr, #0] */
595+ greloca(cur_text_section, sv->sym, ind, R_ARM_TLS_LE32, 0);
596+ o(op | (intr(r) << 12));
597+ return;
598+ }
599+ base = 0xB; // fp
592600 if(v == VT_LLOCAL) {
593601 v1.type.t = VT_PTR;
594602 v1.r = VT_LOCAL | VT_LVAL;
@@ -703,7 +711,7 @@ void store(int r, SValue *sv) · 108 unmodified lines
703711 {
704712 SValue v1;
705713 int v, ft, fc, fr, sign;
706− uint32_t op;
714+ uint32_t op, base;
707715
708716 fr = sv->r;
709717 ft = sv->type.t;
@@ -718,7 +726,15 @@ void store(int r, SValue *sv) · 8 unmodified lines
718726
719727 v = fr & VT_VALMASK;
720728 if (fr & VT_LVAL || fr == VT_LOCAL) {
721− uint32_t base = 0xb; /* fp */
729+ if ((fr & VT_SYM) && sv->sym->type.t & VT_TLS) {
730+ uint32_t op;
731+ o(0xee1d0fe0); /* mrc p15, 0, lr, c13, c0, 3 */
732+ op = 0xe500e000; /* str r, [lr, #0] */
733+ greloca(cur_text_section, sv->sym, ind, R_ARM_TLS_LE32, 0);
734+ o(op | (intr(r) << 12));
735+ return;
736+ }
737+ base = 0xb; /* fp */
722738 if(v < VT_CONST) {
723739 base=intr(v);
724740 v=VT_LOCAL;
arm-link.c+32−0
@@ -44,6 +44,7 @@ ST_FUNC int code_reloc (int reloc_type) · 43 unmodified lines
4444 case R_ARM_TARGET1:
4545 case R_ARM_MOVT_PREL:
4646 case R_ARM_MOVW_PREL_NC:
47+ case R_ARM_TLS_LE32:
4748 return 0;
4849
4950 case R_ARM_PC24:
@@ -70,6 +71,7 @@ ST_FUNC int gotplt_entry_type (int reloc_type) · 20 unmodified lines
7071 case R_ARM_COPY:
7172 case R_ARM_GLOB_DAT:
7273 case R_ARM_JUMP_SLOT:
74+ case R_ARM_TLS_LE32:
7375 return NO_GOTPLT_ENTRY;
7476
7577 case R_ARM_PC24:
@@ -430,6 +432,36 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, · 354 unmodified lines
430432 #endif
431433 /* do nothing */
432434 return;
435+ case R_ARM_TLS_LE32:
436+ {
437+ ElfW(Sym) *sym;
438+ Section *sec;
439+ int32_t x;
440+ addr_t tls_start = 0, tls_end = 0, tls_align = 1;
441+ int i;
442+
443+ sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
444+ sec = s1->sections[sym->st_shndx];
445+
446+ for (i = 1; i < s1->nb_sections; i++) {
447+ Section *s = s1->sections[i];
448+ if (s->sh_flags & SHF_TLS && s->sh_size) {
449+ if (!tls_start || s->sh_addr < tls_start)
450+ tls_start = s->sh_addr;
451+ if (s->sh_addr + s->sh_size > tls_end)
452+ tls_end = s->sh_addr + s->sh_size;
453+ if (s->sh_addralign > tls_align)
454+ tls_align = s->sh_addralign;
455+ }
456+ }
457+ if (tls_end > tls_start) {
458+ x = val - tls_start + 8;
459+ } else {
460+ x = val - sec->sh_addr - sec->data_offset + 8;
461+ }
462+ add32le(ptr, x);
463+ }
464+ return;
433465 default:
434466 fprintf(stderr,"FIXME: handle reloc type %d at %x [%p] to %x\n",
435467 type, (unsigned)addr, ptr, (unsigned)val);
tests/tests2/Makefile+2−2
@@ -19,8 +19,8 @@ ifeq (,$(filter i386 x86_64,$(ARCH))) · 18 unmodified lines
1919 SKIP += 85_asm-outside-function.test # x86 asm
2020 SKIP += 127_asm_goto.test # hardcodes x86 asm
2121 endif
22−ifeq (,$(filter x86_64 riscv64 arm64,$(ARCH)))
23− SKIP += 144_tls.test # TLS only implemented on x86_64 so far
22+ifeq (,$(filter x86_64 riscv64 arm64 arm,$(ARCH)))
23+ SKIP += 144_tls.test # TLS only implemented on these architectures so far
2424 endif
2525 ifeq ($(CONFIG_backtrace),no)
2626 SKIP += 113_btdll.test

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