History

text relocation for netbsd

netbsd does not allow text relocations in text segment. tcc.h: - Add data_ro_section - Fix typo rela.plt tccelf.c: - Add data_ro_section - Make bounds_section/lbounds_section rw - Add GNU_RELRO section for data_ro_section/bounds_section/lbounds_section - Fix relocation for __dso_handle in atexit() tccgen.c: - Use data_ro_section x86_64-gen.c: - Use R_X86_64_PC32 instead of R_X86_64_64 for bounds checking tests/Makefile, tests/tests2/Makefile - Enable dll tests for netbsd

herman ten brugge committed 5 years ago

Commit 08219401 parent c13c4346 files · +101 −44Browse files

Unified · Split

6 files
  1. tcc.h +3 −2
  2. tccelf.c +86 −23
  3. tccgen.c +4 −2
  4. tests/Makefile +1 −7
  5. tests/tests2/Makefile +0 −3
  6. x86_64-gen.c +7 −7
tcc.h+3−2
@@ -409,7 +409,7 @@ extern long double strtold (const char *__nptr, char **__endptr); · 408 unmodified lines
409409 # define ElfW_Rel ElfW(Rela)
410410 # define SHT_RELX SHT_RELA
411411 # define REL_SECTION_FMT ".rela%s"
412−# define RELPLT_SECTION_FMT ".rel.plt"
412+# define RELPLT_SECTION_FMT ".rela.plt"
413413 #else
414414 # define ELFCLASSW ELFCLASS32
415415 # define ElfW(type) Elf##32##_##type
@@ -872,7 +872,7 @@ struct TCCState { · 456 unmodified lines
872872 Section *plt;
873873
874874 /* predefined sections */
875− Section *text_section, *data_section, *bss_section;
875+ Section *text_section, *data_section, *data_ro_section, *bss_section;
876876 Section *common_section;
877877 Section *cur_text_section; /* current section where function code is generated */
878878 #ifdef CONFIG_TCC_BCHECK
@@ -1817,6 +1817,7 @@ ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename) · 938 unmodified lines
18171817
18181818 #define text_section TCC_STATE_VAR(text_section)
18191819 #define data_section TCC_STATE_VAR(data_section)
1820+#define data_ro_section TCC_STATE_VAR(data_ro_section)
18201821 #define bss_section TCC_STATE_VAR(bss_section)
18211822 #define common_section TCC_STATE_VAR(common_section)
18221823 #define cur_text_section TCC_STATE_VAR(cur_text_section)
tccelf.c+86−23
@@ -58,6 +58,8 @@ ST_FUNC void tccelf_new(TCCState *s) · 57 unmodified lines
5858 /* create standard sections */
5959 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
6060 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
61+ /* create ro data section (make ro after relocation done with GNU_RELRO) */
62+ data_ro_section = new_section(s, ".data.ro", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
6163 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
6264 common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE);
6365 common_section->sh_num = SHN_COMMON;
@@ -79,11 +81,11 @@ ST_FUNC void tccelf_new(TCCState *s) · 15 unmodified lines
7981 ST_FUNC void tccelf_bounds_new(TCCState *s)
8082 {
8183 TCCState *s1 = s;
82− /* create bounds sections */
84+ /* create bounds sections (make ro after relocation done with GNU_RELRO) */
8385 bounds_section = new_section(s, ".bounds",
84− SHT_PROGBITS, SHF_ALLOC);
86+ SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
8587 lbounds_section = new_section(s, ".lbounds",
86− SHT_PROGBITS, SHF_ALLOC);
88+ SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
8789 }
8890 #endif
8991
@@ -1043,6 +1045,16 @@ static int prepare_dynamic_rel(TCCState *s1, Section *sr) · 953 unmodified lines
10431045 case R_386_PC32:
10441046 #elif defined(TCC_TARGET_X86_64)
10451047 case R_X86_64_PC32:
1048+ {
1049+ ElfW(Sym) *sym = &((ElfW(Sym) *)symtab_section->data)[sym_index];
1050+
1051+ /* support __dso_handle in atexit() */
1052+ if (sym->st_shndx != SHN_UNDEF &&
1053+ ELFW(ST_VISIBILITY)(sym->st_other) == STV_HIDDEN) {
1054+ rel->r_info = ELFW(R_INFO)(sym_index, R_X86_64_PLT32);
1055+ break;
1056+ }
1057+ }
10461058 #elif defined(TCC_TARGET_ARM64)
10471059 case R_AARCH64_PREL32:
10481060 #endif
@@ -1828,11 +1840,20 @@ struct dyn_inf { · 779 unmodified lines
18281840 addr_t rel_size;
18291841 };
18301842
1843+/* Info for GNU_RELRO */
1844+struct ro_inf {
1845+ addr_t sh_offset;
1846+ addr_t sh_addr;
1847+ addr_t sh_size;
1848+};
1849+
18311850 /* Assign sections to segments and decide how are sections laid out when loaded
18321851 in memory. This function also fills corresponding program headers. */
1833−static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum,
1852+static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr,
1853+ int phnum, int phfill,
18341854 Section *interp, Section* strsec,
1835− struct dyn_inf *dyninf, int *sec_order)
1855+ struct dyn_inf *dyninf, struct ro_inf *roinf,
1856+ int *sec_order)
18361857 {
18371858 int i, sh_order_index, file_offset;
18381859 Section *s;
@@ -1883,7 +1904,10 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum, · 44 unmodified lines
18831904 /* dynamic relocation table information, for .dynamic section */
18841905 dyninf->rel_addr = dyninf->rel_size = 0;
18851906
1886− for(j = 0; j < (phnum == 6 ? 3 : 2); j++) {
1907+ /* read only segment mapping for GNU_RELRO */
1908+ roinf->sh_offset = roinf->sh_addr = roinf->sh_size = 0;
1909+
1910+ for(j = 0; j < phfill; j++) {
18871911 Section *relocplt = s1->got ? s1->got->relocplt : NULL;
18881912
18891913 ph->p_type = j == 2 ? PT_TLS : PT_LOAD;
@@ -1898,7 +1922,7 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum, · 8 unmodified lines
18981922 info about the layout. We do the following ordering: interp,
18991923 symbol tables, relocations, progbits, nobits */
19001924 /* XXX: do faster and simpler sorting */
1901− for(k = 0; k < 6; k++) {
1925+ for(k = 0; k < 7; k++) {
19021926 for(i = 1; i < s1->nb_sections; i++) {
19031927 s = s1->sections[i];
19041928 /* compute if section should be included */
@@ -1930,12 +1954,17 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum, · 25 unmodified lines
19301954 else if (k != 3 && s == relocplt)
19311955 continue;
19321956 } else if (s->sh_type == SHT_NOBITS) {
1933− if (k != 5)
1957+ if (k != 6)
19341958 continue;
1935− } else {
1959+ } else if (s == data_ro_section ||
1960+ s == bounds_section ||
1961+ s == lbounds_section) {
19361962 if (k != 4)
19371963 continue;
1938− }
1964+ } else {
1965+ if (k != 5)
1966+ continue;
1967+ }
19391968 sec_order[sh_order_index++] = i;
19401969
19411970 /* section matches: we align it and add its size */
@@ -1956,8 +1985,17 @@ static int layout_sections(TCCState *s1, ElfW(Phdr) *phdr, int phnum, · 14 unmodified lines
19561985 if (s->sh_type == SHT_RELX && s != relocplt) {
19571986 if (dyninf->rel_size == 0)
19581987 dyninf->rel_addr = addr;
1959− dyninf->rel_size += s->sh_size;
1988+ dyninf->rel_size = (addr - dyninf->rel_addr) + s->sh_size;
19601989 }
1990+ if (s == data_ro_section ||
1991+ s == bounds_section ||
1992+ s == lbounds_section) {
1993+ if (roinf->sh_size == 0) {
1994+ roinf->sh_offset = s->sh_offset;
1995+ roinf->sh_addr = s->sh_addr;
1996+ }
1997+ roinf->sh_size = (addr - roinf->sh_addr) + s->sh_size;
1998+ }
19611999 addr += s->sh_size;
19622000 if (s->sh_type != SHT_NOBITS)
19632001 file_offset += s->sh_size;
@@ -2018,7 +2056,7 @@ static void put_dt(Section *dynamic, int dt, addr_t val) · 54 unmodified lines
20182056 }
20192057
20202058 static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp,
2021− Section *dynamic, Section *note)
2059+ Section *dynamic, Section *note, struct ro_inf *roinf)
20222060 {
20232061 ElfW(Phdr) *ph;
20242062
@@ -2046,7 +2084,7 @@ static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp, · 21 unmodified lines
20462084 }
20472085
20482086 if (note) {
2049− ph = &phdr[phnum - 2];
2087+ ph = &phdr[phnum - 2 - (roinf != NULL)];
20502088
20512089 ph->p_type = PT_NOTE;
20522090 ph->p_offset = note->sh_offset;
@@ -2060,7 +2098,7 @@ static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp, · 7 unmodified lines
20602098
20612099 /* if dynamic section, then add corresponding program header */
20622100 if (dynamic) {
2063− ph = &phdr[phnum - 1];
2101+ ph = &phdr[phnum - 1 - (roinf != NULL)];
20642102
20652103 ph->p_type = PT_DYNAMIC;
20662104 ph->p_offset = dynamic->sh_offset;
@@ -2071,6 +2109,19 @@ static void fill_unloadable_phdr(ElfW(Phdr) *phdr, int phnum, Section *interp, · 4 unmodified lines
20712109 ph->p_flags = PF_R | PF_W;
20722110 ph->p_align = dynamic->sh_addralign;
20732111 }
2112+
2113+ if (roinf) {
2114+ ph = &phdr[phnum - 1];
2115+
2116+ ph->p_type = PT_GNU_RELRO;
2117+ ph->p_offset = roinf->sh_offset;
2118+ ph->p_vaddr = roinf->sh_addr;
2119+ ph->p_paddr = ph->p_vaddr;
2120+ ph->p_filesz = roinf->sh_size;
2121+ ph->p_memsz = roinf->sh_size;
2122+ ph->p_flags = PF_R;
2123+ ph->p_align = 1;
2124+ }
20742125 }
20752126
20762127 /* Fill the dynamic section with tags describing the address and size of
@@ -2440,8 +2491,9 @@ static Section *create_bsd_note_section(TCCState *s1, · 363 unmodified lines
24402491 /* XXX: suppress unneeded sections */
24412492 static int elf_output_file(TCCState *s1, const char *filename)
24422493 {
2443− int ret, phnum, shnum, file_type, file_offset, *sec_order;
2494+ int i, ret, phnum, phfill, shnum, file_type, file_offset, *sec_order;
24442495 struct dyn_inf dyninf = {0};
2496+ struct ro_inf roinf, *roinf_use = NULL;
24452497 ElfW(Phdr) *phdr;
24462498 Section *strsec, *interp, *dynamic, *dynstr, *note = NULL;
24472499
@@ -2489,6 +2541,8 @@ static int elf_output_file(TCCState *s1, const char *filename) · 41 unmodified lines
24892541 s1->dynsym = new_symtab(s1, ".dynsym", SHT_DYNSYM, SHF_ALLOC,
24902542 ".dynstr",
24912543 ".hash", SHF_ALLOC);
2544+ /* Number of local symbols (readelf complains if not set) */
2545+ s1->dynsym->sh_info = 1;
24922546 dynstr = s1->dynsym->link;
24932547 /* add dynamic section */
24942548 dynamic = new_section(s1, ".dynamic", SHT_DYNAMIC,
@@ -2556,22 +2610,31 @@ static int elf_output_file(TCCState *s1, const char *filename) · 61 unmodified lines
25562610 }
25572611 #endif
25582612
2613+ for (i = 1; i < s1->nb_sections &&
2614+ !(s1->sections[i]->sh_flags & SHF_TLS); i++);
2615+ phfill = 2 + (i < s1->nb_sections);
2616+
25592617 /* compute number of program headers */
25602618 if (file_type == TCC_OUTPUT_OBJ)
2561− phnum = 0;
2619+ phnum = phfill = 0;
25622620 else if (file_type == TCC_OUTPUT_DLL)
25632621 phnum = 3;
25642622 else if (s1->static_link)
25652623 phnum = 2;
25662624 else {
2567− int i;
2568− for (i = 1; i < s1->nb_sections &&
2569− !(s1->sections[i]->sh_flags & SHF_TLS); i++);
2570− phnum = i < s1->nb_sections ? 6 : 5;
2625+ phnum = 5 + (i < s1->nb_sections);
25712626 }
25722627
25732628 phnum += note != NULL;
25742629
2630+#if !TARGETOS_FreeBSD && !TARGETOS_NetBSD && !defined(__APPLE__) && !defined(_WIN32)
2631+ /* GNU_RELRO */
2632+ if (file_type != TCC_OUTPUT_OBJ) {
2633+ phnum++;
2634+ roinf_use = &roinf;
2635+ }
2636+#endif
2637+
25752638 /* allocate program segment headers */
25762639 phdr = tcc_mallocz(phnum * sizeof(ElfW(Phdr)));
25772640
@@ -2583,14 +2646,14 @@ static int elf_output_file(TCCState *s1, const char *filename) · 5 unmodified lines
25832646 sec_order[0] = 0;
25842647
25852648 /* compute section to program header mapping */
2586− file_offset = layout_sections(s1, phdr, phnum, interp, strsec, &dyninf,
2587− sec_order);
2649+ file_offset = layout_sections(s1, phdr, phnum, phfill, interp, strsec,
2650+ &dyninf, &roinf, sec_order);
25882651
25892652 #ifndef ELF_OBJ_ONLY
25902653 /* Fill remaining program header and finalize relocation related to dynamic
25912654 linking. */
25922655 if (file_type != TCC_OUTPUT_OBJ) {
2593− fill_unloadable_phdr(phdr, phnum, interp, dynamic, note);
2656+ fill_unloadable_phdr(phdr, phnum, interp, dynamic, note, roinf_use);
25942657 if (dynamic) {
25952658 ElfW(Sym) *sym;
25962659 dynamic->data_offset = dyninf.data_offset;
tccgen.c+4−2
@@ -8120,8 +8120,10 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 8119 unmodified lines
81208120 /* allocate symbol in corresponding section */
81218121 sec = ad->section;
81228122 if (!sec) {
8123− if (has_init)
8124− sec = data_section;
8123+ if (type->t & VT_CONSTANT)
8124+ sec = data_ro_section;
8125+ else if (has_init)
8126+ sec = data_section;
81258127 else if (tcc_state->nocommon)
81268128 sec = bss_section;
81278129 }
tests/Makefile+1−7
@@ -64,17 +64,11 @@ endif · 63 unmodified lines
6464 ifeq ($(TARGETOS),OpenBSD)
6565 dlltest: CFLAGS+=-fno-stack-protector
6666 endif
67−ifeq ($(TARGETOS),FreeBSD)
67+ifneq (,$(filter FreeBSD NetBSD,$(TARGETOS)))
6868 # test3 has dlsym problems
6969 TESTS := $(filter-out test3,$(TESTS))
7070 TESTS += test1
7171 endif
72−ifeq ($(TARGETOS),NetBSD)
73− # test3 has dlsym problems
74− # dlltest does not allow text relocations
75− TESTS := $(filter-out test3 dlltest,$(TESTS))
76− TESTS += test1
77−endif
7872
7973 RUN_TCC = $(NATIVE_DEFINES) -run $(TOPSRC)/tcc.c $(TCCFLAGS)
8074 DISAS = objdump -d
tests/tests2/Makefile+0−3
@@ -53,9 +53,6 @@ ifneq (,$(filter OpenBSD FreeBSD NetBSD,$(TARGETOS))) · 52 unmodified lines
5353 SKIP += 114_bound_signal.test # libc problem signal/fork
5454 SKIP += 116_bound_setjmp2.test # No TLS_FUNC/TLS_VAR in bcheck.c
5555 endif
56−ifeq ($(TARGETOS),NetBSD)
57− SKIP += 113_btdll.test # text relocations
58−endif
5956
6057 # Some tests might need arguments
6158 ARGS =
x86_64-gen.c+7−7
@@ -674,8 +674,8 @@ static void gen_bounds_prolog(void) · 673 unmodified lines
674674 func_bound_offset = lbounds_section->data_offset;
675675 func_bound_ind = ind;
676676 func_bound_add_epilog = 0;
677− o(0xb848 + TREG_FASTCALL_1 * 0x100); /*lbound section pointer */
678− gen_le64 (0);
677+ o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /*lbound section pointer */
678+ gen_le32 (0);
679679 oad(0xb8, 0); /* call to function */
680680 }
681681
@@ -700,17 +700,17 @@ static void gen_bounds_epilog(void) · 18 unmodified lines
700700 if (offset_modified) {
701701 saved_ind = ind;
702702 ind = func_bound_ind;
703− greloca(cur_text_section, sym_data, ind + 2, R_X86_64_64, 0);
704− ind = ind + 10;
703+ greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
704+ ind = ind + 7;
705705 gen_bounds_call(TOK___bound_local_new);
706706 ind = saved_ind;
707707 }
708708
709709 /* generate bound check local freeing */
710710 o(0x5250); /* save returned value, if any */
711− greloca(cur_text_section, sym_data, ind + 2, R_X86_64_64, 0);
712− o(0xb848 + TREG_FASTCALL_1 * 0x100); /* mov xxx, %rcx/di */
713− gen_le64 (0);
711+ greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
712+ o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /* lea xxx(%rip), %rcx/rdi */
713+ gen_le32 (0);
714714 gen_bounds_call(TOK___bound_local_delete);
715715 o(0x585a); /* restore returned value, if any */
716716 }

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