1
1
/****************************************************************************
2
- * arch/sim/src/sim/up_macho_init.c
2
+ * arch/sim/src/sim/posix/sim_macho_init.c
3
+ *
4
+ * SPDX-License-Identifier: Apache-2.0
3
5
*
4
6
* Licensed to the Apache Software Foundation (ASF) under one or more
5
7
* contributor license agreements. See the NOTICE file distributed with
22
24
* Included Files
23
25
****************************************************************************/
24
26
27
+ #include <sys/mman.h>
28
+
25
29
#include <assert.h>
26
30
#include <stdlib.h>
31
+ #include <unistd.h>
27
32
28
33
/****************************************************************************
29
34
* Private Data
@@ -49,21 +54,51 @@ static const char **g_saved_argv;
49
54
static const char * * g_saved_envp ;
50
55
static const char * * g_saved_apple ;
51
56
57
+ static void
58
+ allow_write (const void * start , const void * end )
59
+ {
60
+ const size_t page_size = sysconf (_SC_PAGE_SIZE );
61
+ const size_t page_mask = ~(page_size - 1 );
62
+ void * p = (void * )((uintptr_t )start & page_mask );
63
+ size_t sz = ((uintptr_t )end - (uintptr_t )p + page_size - 1 ) & ~page_mask ;
64
+
65
+ /* It seems that Monterey (12.1) maps the section read-only.
66
+ * Make it writable as we want to patch it.
67
+ * This was not necessary for Mojave.
68
+ * Ignore failures as this might not be critical, depending on
69
+ * the OS version.
70
+ */
71
+
72
+ mprotect (p , sz , PROT_READ | PROT_WRITE );
73
+ }
74
+
52
75
__attribute__((constructor ))
53
76
static void save_and_replace_init_funcs (int argc , const char * argv [],
54
77
const char * envp [],
55
78
const char * apple [])
56
79
{
80
+ init_func_t * fp ;
81
+ unsigned int nfuncs = & mod_init_func_end - & mod_init_func_start ;
82
+
83
+ assert (nfuncs > 0 );
84
+ g_num_saved_init_funcs = nfuncs - 1 ;
85
+ if (g_num_saved_init_funcs == 0 )
86
+ {
87
+ /* This function is the only constructor in the binary.
88
+ * no need to apply the following hack.
89
+ */
90
+
91
+ return ;
92
+ }
93
+
57
94
g_saved_argc = argc ;
58
95
g_saved_argv = argv ;
59
96
g_saved_envp = envp ;
60
97
g_saved_apple = apple ;
61
- init_func_t * fp ;
62
- unsigned int nfuncs = & mod_init_func_end - & mod_init_func_start ;
63
- assert (nfuncs > 1 );
64
- g_num_saved_init_funcs = nfuncs - 1 ;
98
+
65
99
g_saved_init_funcs = malloc (g_num_saved_init_funcs *
66
100
sizeof (* g_saved_init_funcs ));
101
+ allow_write (& mod_init_func_start , & mod_init_func_end );
67
102
int i = 0 ;
68
103
for (fp = & mod_init_func_start ; fp < & mod_init_func_end ; fp ++ )
69
104
{
0 commit comments