• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Révision365da5ab379a3e38e196cad370695c4b300cfe51 (tree)
l'heure2022-09-07 15:32:47
Auteurlinted <linted@user...>
CommiterWaldemar Brodkorb

Message de Log

Added support for creation of Static Position-Independent Executables (PIE) on mips

Updated config to allow compilation of rcrt1.o for mips and modified it's crt1.S to perform relocates in start.

The mips architecture performs relocations differently then most other architectures. reloc_static_pie was rewritten, taking code from dl-startup.c, in order to perfrom the additional relocations. Modifications were made to mips' dl-startup.h to allow for the use of contained macros without including _start definition.

Signed-off-by: linted <linted@users.noreply.github.com>

Change Summary

Modification

--- a/extra/Configs/Config.in
+++ b/extra/Configs/Config.in
@@ -324,7 +324,7 @@ config DOPIC
324324 config STATIC_PIE
325325 bool "Add support for Static Position Independent Executables (PIE)"
326326 default n
327- depends on DOPIC && !UCLIBC_FORMAT_FDPIC_ELF && (TARGET_arm || TARGET_i386 || TARGET_x86_64 || TARGET_aarch64)
327+ depends on DOPIC && !UCLIBC_FORMAT_FDPIC_ELF && (TARGET_arm || TARGET_i386 || TARGET_x86_64 || TARGET_aarch64 || TARGET_mips)
328328
329329 config ARCH_HAS_NO_SHARED
330330 bool
--- a/ldso/ldso/mips/dl-startup.h
+++ b/ldso/ldso/mips/dl-startup.h
@@ -7,6 +7,7 @@
77
88
99 #include <sgidefs.h>
10+#ifndef L_rcrt1
1011 __asm__(""
1112 " .text\n"
1213 " .globl _start\n"
@@ -114,6 +115,7 @@ __asm__(""
114115 "\n\n"
115116 ".previous\n"
116117 );
118+#endif
117119
118120 /*
119121 * Get a pointer to the argv array. On many platforms this can be just
@@ -191,6 +193,5 @@ do { \
191193 case R_MIPS_NONE: \
192194 break; \
193195 default: \
194- SEND_STDERR("Aiieeee!"); \
195196 _dl_exit(1); \
196197 }
--- a/libc/misc/internals/Makefile.in
+++ b/libc/misc/internals/Makefile.in
@@ -17,7 +17,7 @@ MISC_INTERNALS_SRC := $(patsubst %.c,$(MISC_INTERNALS_DIR)/%.c,$(CSRC-y))
1717 MISC_INTERNALS_OBJ := $(patsubst %.c,$(MISC_INTERNALS_OUT)/%.o,$(CSRC-y))
1818
1919 CFLAGS-__uClibc_main.c := $(SSP_DISABLE_FLAGS)
20-CFLAGS-reloc_static_pie.c := $(SSP_DISABLE_FLAGS)
20+CFLAGS-reloc_static_pie.c := $(SSP_DISABLE_FLAGS) -DL_rcrt1
2121
2222 libc-y += $(MISC_INTERNALS_OBJ)
2323 ifneq ($(UCLIBC_FORMAT_SHARED_FLAT),y)
--- a/libc/misc/internals/reloc_static_pie.c
+++ b/libc/misc/internals/reloc_static_pie.c
@@ -15,33 +15,96 @@
1515 You should have received a copy of the GNU Lesser General Public
1616 License along with the GNU C Library; if not, see
1717 <https://www.gnu.org/licenses/>. */
18-
18+#define IS_IN_rtld // force inline function calls
1919 #include <link.h>
2020 #include <elf.h>
2121 #include <dl-elf.h>
2222
23+#include <ldso.h>
24+#ifdef __mips__
25+#include <dl-startup.h>
26+#endif
27+
2328 ElfW(Addr) _dl_load_base = NULL;
2429
2530 void
2631 reloc_static_pie (ElfW(Addr) load_addr);
2732
2833 void
29-reloc_static_pie (ElfW(Addr) load_addr)
34+reloc_static_pie(ElfW(Addr) load_addr)
3035 {
31- ElfW(Word) relative_count = 0;
32- ElfW(Addr) rel_addr = 0;
33- ElfW(Dyn) * dyn_addr = NULL;
34- unsigned long dynamic_info[DYNAMIC_SIZE] = {0};
36+ int indx;
37+ ElfW(Addr) got;
38+ ElfW(Dyn) *dpnt;
39+ struct elf_resolve tpnt_tmp;
40+ struct elf_resolve *tpnt = &tpnt_tmp;
41+
42+ DL_BOOT_COMPUTE_GOT(got);
43+ DL_BOOT_COMPUTE_DYN(dpnt, got, (DL_LOADADDR_TYPE)load_addr);
44+
45+ _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
46+ tpnt->loadaddr = load_addr;
47+ tpnt->dynamic_addr = dpnt;
48+
49+ __dl_parse_dynamic_info(dpnt, tpnt->dynamic_info, NULL, load_addr);
50+
51+#if defined(PERFORM_BOOTSTRAP_GOT)
52+ /* some arches (like MIPS) we have to tweak the GOT before relocations */
53+ PERFORM_BOOTSTRAP_GOT(tpnt);
54+#endif
55+
56+
57+#if defined(ELF_MACHINE_PLTREL_OVERLAP)
58+# define INDX_MAX 1
59+#else
60+# define INDX_MAX 2
61+#endif
62+
63+ for (indx = 0; indx < INDX_MAX; indx++) {
64+ unsigned long rel_addr, rel_size;
65+ ElfW(Word) relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
66+
67+ rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] :
68+ tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]);
69+ rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] :
70+ tpnt->dynamic_info[DT_RELOC_TABLE_SIZE]);
71+
72+ if (!rel_addr)
73+ continue;
3574
36- /* Read our own dynamic section and fill in the info array. */
37- dyn_addr = ((void *) load_addr + elf_machine_dynamic ());
75+ if((0 == indx) && relative_count) {
76+ rel_size -= relative_count * sizeof(ELF_RELOC);
77+ elf_machine_relative(load_addr, rel_addr, relative_count);
78+ rel_addr += relative_count * sizeof(ELF_RELOC);
79+ }
3880
39- /* Use the underlying function to avoid TLS access before initialization */
40- __dl_parse_dynamic_info(dyn_addr, dynamic_info, NULL, load_addr);
81+#ifdef ARCH_NEEDS_BOOTSTRAP_RELOCS
82+ {
83+ ELF_RELOC *rpnt;
84+ unsigned int i;
85+ ElfW(Sym) *sym;
86+ unsigned long symbol_addr;
87+ int symtab_index;
88+ unsigned long *reloc_addr;
4189
42- /* Perform relocations */
43- relative_count = dynamic_info[DT_RELCONT_IDX];
44- rel_addr = dynamic_info[DT_RELOC_TABLE_ADDR];
45- elf_machine_relative(load_addr, rel_addr, relative_count);
90+ /* Now parse the relocation information */
91+ rpnt = (ELF_RELOC *) rel_addr;
92+ for (i = 0; i < rel_size; i += sizeof(ELF_RELOC), rpnt++) {
93+ reloc_addr = (unsigned long *) DL_RELOC_ADDR(load_addr, (unsigned long)rpnt->r_offset);
94+ symtab_index = ELF_R_SYM(rpnt->r_info);
95+ symbol_addr = 0;
96+ sym = NULL;
97+ if (symtab_index) {
98+ ElfW(Sym) *symtab;
99+ symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
100+ sym = &symtab[symtab_index];
101+ symbol_addr = (unsigned long) DL_RELOC_ADDR(load_addr, sym->st_value);
102+ }
103+ /* Use this machine-specific macro to perform the actual relocation. */
104+ PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr, sym);
105+ }
106+ }
107+#endif
108+ }
46109 _dl_load_base = load_addr;
47-}
110+}
\ No newline at end of file
--- a/libc/sysdeps/linux/mips/crt1.S
+++ b/libc/sysdeps/linux/mips/crt1.S
@@ -78,6 +78,10 @@
7878 .weak _init
7979 .weak _fini
8080 #endif
81+#ifdef L_rcrt1
82+ .type reloc_static_pie,@function
83+ .hidden .L0
84+#endif
8185 .type main,@function
8286 .type __uClibc_main,@function
8387 .ent __start
@@ -90,6 +94,25 @@ __start:
9094 PTR_LA $28, _gp /* Setup GP correctly if we're non-PIC. */
9195 move $31, $0
9296 #endif
97+#ifdef L_rcrt1
98+ PTR_LA $4, _DYNAMIC /* Place _DYNAMIC into the GOT */
99+ REG_S $4, -0x7ff0($28) /* offset to GOT stolen from dl-startup */
100+ jal .L0 /* Get the current $pc address */
101+.L0:
102+ PTR_SUBU $4, $31, $25 /* Calculate load addr */
103+ move $31, $0 /* Clear ra */
104+ and $29, -2 * SZREG /* Ensure stack is aligned */
105+ PTR_ADDIU $29, (-2 * SZREG) /* Allocate 2 register spaces on stack */
106+ REG_S $2, SZREG($29) /* Store atexit in case it exists */
107+ PTR_LA $5, reloc_static_pie /* function calls before relocation
108+ don't work unless we set $t9 manually */
109+ PTR_ADDU $25, $4, $5 /* store reloc_static_pie in $t9 */
110+ jalr $25 /* call reloc_static_pie */
111+ nop /* delay slot, just in case */
112+ REG_L $2, SZREG($29) /* cleanup stack */
113+ PTR_ADDIU $29, $29, (2 * SZREG)
114+
115+#endif
93116
94117 PTR_LA $4, main /* main */
95118 PTR_L $5, 0($29) /* argc */