History

Call __bound_main_arg at startup

This uses a glibc feature present since constructor/destructor support was added. Modify tccrun.c to call constructor with argc, argcv, envp. In lib/bcheck.c use these values to register them in the splay tree. Remove HAS_ENVIRON is lib/bcheck.c as it is not needed any more. Modify win32/lib/crt1.c/win32/lib/dllcrt1.c/win32/lib/wincrt1.c to also call constructor with argc, argcv, envp. While implementing I saw that tccrun did nog call main with envp. Fixed it. Also fix fetch_and_add_arm.S to make it work on armv6 (raspberry pi default).

herman ten brugge committed 6 years ago

Commit 039e4ec1 parent 0262f0e6 files · +57 −55Browse files

Unified · Split

6 files
  1. lib/bcheck.c +28 −35
  2. lib/fetch_and_add_arm.S +5 −5
  3. tccrun.c +7 −6
  4. win32/lib/crt1.c +7 −3
  5. win32/lib/dllcrt1.c +3 −3
  6. win32/lib/wincrt1.c +7 −3
lib/bcheck.c+28−35
@@ -70,7 +70,6 @@ · 69 unmodified lines
7070 #define WAIT_SEM()
7171 #define POST_SEM()
7272 #define HAVE_MEMALIGN (0)
73−#define HAS_ENVIRON (0)
7473 #define MALLOC_REDIR (0)
7574 #define HAVE_PTHREAD_CREATE (0)
7675 #define HAVE_CTYPE (0)
@@ -85,7 +84,6 @@ static CRITICAL_SECTION bounds_sem; · 8 unmodified lines
8584 #define WAIT_SEM() EnterCriticalSection(&bounds_sem)
8685 #define POST_SEM() LeaveCriticalSection(&bounds_sem)
8786 #define HAVE_MEMALIGN (0)
88−#define HAS_ENVIRON (0)
8987 #define MALLOC_REDIR (0)
9088 #define HAVE_PTHREAD_CREATE (0)
9189 #define HAVE_CTYPE (0)
@@ -123,7 +121,6 @@ static pthread_spinlock_t bounds_spin; · 31 unmodified lines
123121 #define POST_SEM() if (use_sem) pthread_spin_unlock (&bounds_spin)
124122 #endif
125123 #define HAVE_MEMALIGN (1)
126−#define HAS_ENVIRON (1)
127124 #define MALLOC_REDIR (1)
128125 #define HAVE_PTHREAD_CREATE (1)
129126 #define HAVE_CTYPE (1)
@@ -207,7 +204,7 @@ DLL_EXPORT void * __bound_ptr_indir16(void *p, size_t offset); · 77 unmodified lines
207204 DLL_EXPORT void FASTCALL __bound_local_new(void *p1);
208205 DLL_EXPORT void FASTCALL __bound_local_delete(void *p1);
209206 void __bound_init(size_t *, int);
210−void __bound_main_arg(char **p);
207+void __bound_main_arg(int argc, char **argv, char **envp);
211208 void __bound_exit(void);
212209 #if !defined(_WIN32)
213210 void *__bound_mmap (void *start, size_t size, int prot, int flags, int fd,
@@ -934,61 +931,57 @@ no_bounds: · 720 unmodified lines
934931 dprintf(stderr, "%s, %s(): end\n\n", __FILE__, __FUNCTION__);
935932 }
936933
937−void __bound_main_arg(char **p)
934+void
935+#if (defined(__GLIBC__) && (__GLIBC_MINOR__ >= 4)) || defined(_WIN32)
936+__attribute__((constructor))
937+#endif
938+__bound_main_arg(int argc, char **argv, char **envp)
938939 {
939− char *start = (char *) p;
940+ __bound_init (0, -1);
941+ if (argc && argv) {
942+ int i;
940943
941− WAIT_SEM ();
942− while (*p) {
943− tree = splay_insert((size_t) *p, strlen (*p) + 1, tree);
944− ++p;
945− }
946− tree = splay_insert((size_t) start, (char *) p - start, tree);
947− POST_SEM ();
944+ WAIT_SEM ();
945+ for (i = 0; i < argc; i++)
946+ tree = splay_insert((size_t) argv[i], strlen (argv[i]) + 1, tree);
947+ tree = splay_insert((size_t) argv, argc * sizeof(char *), tree);
948+ POST_SEM ();
948949 #if BOUND_DEBUG
949− if (print_calls) {
950− p = (char **) start;
951− while (*p) {
952− dprintf(stderr, "%s, %s(): %p 0x%lx\n",
953− __FILE__, __FUNCTION__,
954− *p, (unsigned long)(strlen (*p) + 1));
955− ++p;
950+ if (print_calls) {
951+ for (i = 0; i < argc; i++)
952+ dprintf(stderr, "%s, %s(): arg %p 0x%lx\n",
953+ __FILE__, __FUNCTION__,
954+ argv[i], (unsigned long)(strlen (argv[i]) + 1));
955+ dprintf(stderr, "%s, %s(): argv %p 0x%lx\n",
956+ __FILE__, __FUNCTION__, argv, argc * sizeof(char *));
956957 }
957− dprintf(stderr, "%s, %s(): argv %p 0x%lx\n",
958− __FILE__, __FUNCTION__,
959− start, (unsigned long)((char *) p - start));
960− }
961958 #endif
959+ }
962960
963−#if HAS_ENVIRON
964− {
965− extern char **environ;
961+ if (envp && *envp) {
962+ char **p = envp;
966963
967964 WAIT_SEM ();
968− p = environ;
969− start = (char *) p;
970965 while (*p) {
971966 tree = splay_insert((size_t) *p, strlen (*p) + 1, tree);
972967 ++p;
973968 }
974− tree = splay_insert((size_t) start, (char *) p - start, tree);
969+ tree = splay_insert((size_t) envp, p - envp, tree);
975970 POST_SEM ();
976971 #if BOUND_DEBUG
977972 if (print_calls) {
978− p = environ;
973+ p = envp;
979974 while (*p) {
980− dprintf(stderr, "%s, %s(): %p 0x%lx\n",
975+ dprintf(stderr, "%s, %s(): env %p 0x%lx\n",
981976 __FILE__, __FUNCTION__,
982977 *p, (unsigned long)(strlen (*p) + 1));
983978 ++p;
984979 }
985980 dprintf(stderr, "%s, %s(): environ %p 0x%lx\n",
986− __FILE__, __FUNCTION__,
987− start, (unsigned long)((char *) p - start));
981+ __FILE__, __FUNCTION__, envp, p - envp);
988982 }
989983 #endif
990984 }
991−#endif
992985 }
993986
994987 void __attribute__((destructor)) __bound_exit(void)
lib/fetch_and_add_arm.S+5−5
@@ -4,25 +4,25 @@ · 3 unmodified lines
44 .type fetch_and_add_arm, %function
55 fetch_and_add_arm:
66 #ifdef __TINYC__
7− .int 0xf57ff05b
7+ .int 0xee070fba
88 .int 0xe1903f9f
99 .int 0xe0833001
1010 .int 0xe1802f93
1111 .int 0xe3520000
1212 .int 0x1afffffa
13− .int 0xf57ff05b
13+ .int 0xee070fba
1414 .int 0xe12fff1e
1515 #else
16− .arch armv7-a
16+ .arch armv6
1717
18− dmb ish
18+ mcr p15, 0, r0, c7, c10, 5
1919 .L0:
2020 ldrex r3, [r0]
2121 add r3, r3, r1
2222 strex r2, r3, [r0]
2323 cmp r2, #0
2424 bne .L0
25− dmb ish
25+ mcr p15, 0, r0, c7, c10, 5
2626 bx lr
2727 #endif
2828 .size fetch_and_add_arm, .-fetch_and_add_arm
tccrun.c+7−6
@@ -124,18 +124,19 @@ ST_FUNC void tcc_run_free(TCCState *s1) · 123 unmodified lines
124124 tcc_free(s1->runtime_mem);
125125 }
126126
127−static void run_cdtors(TCCState *s1, const char *start, const char *end)
127+static void run_cdtors(TCCState *s1, const char *start, const char *end,
128+ int argc, char **argv, char **envp)
128129 {
129130 void **a = (void **)get_sym_addr(s1, start, 0, 0);
130131 void **b = (void **)get_sym_addr(s1, end, 0, 0);
131132 while (a != b)
132− ((void(*)(void))*a++)();
133+ ((void(*)(int, char **, char **))*a++)(argc, argv, envp);
133134 }
134135
135136 /* launch the compiled program with the given arguments */
136137 LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv)
137138 {
138− int (*prog_main)(int, char **), ret;
139+ int (*prog_main)(int, char **, char **), ret;
139140 #ifdef CONFIG_TCC_BACKTRACE
140141 rt_context *rc = &g_rtctxt;
141142 #endif
@@ -183,14 +184,14 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 41 unmodified lines
183184 fflush(stdout);
184185 fflush(stderr);
185186 /* These aren't C symbols, so don't need leading underscore handling. */
186− run_cdtors(s1, "__init_array_start", "__init_array_end");
187+ run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, environ);
187188 #ifdef CONFIG_TCC_BACKTRACE
188189 if (!rc->do_jmp || !(ret = setjmp(rc->jmp_buf)))
189190 #endif
190191 {
191− ret = prog_main(argc, argv);
192+ ret = prog_main(argc, argv, environ);
192193 }
193− run_cdtors(s1, "__fini_array_start", "__fini_array_end");
194+ run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);
194195 if ((s1->dflag & 16) && ret)
195196 fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
196197 return ret;
win32/lib/crt1.c+7−3
@@ -34,8 +34,8 @@ int __cdecl __tgetmainargs(int *pargc, _TCHAR ***pargv, _TCHAR ***penv, int glob · 33 unmodified lines
3434 void __cdecl __set_app_type(int apptype);
3535 unsigned int __cdecl _controlfp(unsigned int new_value, unsigned int mask);
3636 extern int _tmain(int argc, _TCHAR * argv[], _TCHAR * env[]);
37−extern void (*__init_array_start[]) (void);
38−extern void (*__init_array_end[]) (void);
37+extern void (*__init_array_start[]) (int argc, char **argv, char **envp);
38+extern void (*__init_array_end[]) (int argc, char **argv, char **envp);
3939 extern void (*__fini_array_start[]) (void);
4040 extern void (*__fini_array_end[]) (void);
4141
@@ -46,7 +46,11 @@ static int do_main (int argc, _TCHAR * argv[], _TCHAR * env[]) · 4 unmodified lines
4646
4747 i = 0;
4848 while (&__init_array_start[i] != __init_array_end) {
49− (*__init_array_start[i++])();
49+#ifdef UNICODE
50+ (*__init_array_start[i++])(0, NULL, NULL);
51+#else
52+ (*__init_array_start[i++])(argc, argv, env);
53+#endif
5054 }
5155 retval = _tmain(__argc, __targv, _tenviron);
5256 i = 0;
win32/lib/dllcrt1.c+3−3
@@ -2,8 +2,8 @@ · 1 unmodified line
22
33 #include <windows.h>
44
5−extern void (*__init_array_start[]) (void);
6−extern void (*__init_array_end[]) (void);
5+extern void (*__init_array_start[]) (int argc, char **argv, char **envp);
6+extern void (*__init_array_end[]) (int argc, char **argv, char **envp);
77 extern void (*__fini_array_start[]) (void);
88 extern void (*__fini_array_end[]) (void);
99
@@ -17,7 +17,7 @@ BOOL WINAPI _dllstart(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) · 7 unmodified lines
1717 if (dwReason == DLL_PROCESS_ATTACH) { /* ignore DLL_THREAD_ATTACH */
1818 i = 0;
1919 while (&__init_array_start[i] != __init_array_end) {
20− (*__init_array_start[i++])();
20+ (*__init_array_start[i++])(0, NULL, NULL);
2121 }
2222 }
2323 if (dwReason == DLL_PROCESS_DETACH) { /* ignore DLL_THREAD_DETACH */
win32/lib/wincrt1.c+7−3
@@ -23,8 +23,8 @@ int APIENTRY wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int); · 22 unmodified lines
2323 #define _runtwinmain _runwinmain
2424 #endif
2525
26−extern void (*__init_array_start[]) (void);
27−extern void (*__init_array_end[]) (void);
26+extern void (*__init_array_start[]) (int argc, char **argv, char **envp);
27+extern void (*__init_array_end[]) (int argc, char **argv, char **envp);
2828 extern void (*__fini_array_start[]) (void);
2929 extern void (*__fini_array_end[]) (void);
3030
@@ -57,7 +57,11 @@ static int go_winmain(TCHAR *arg1) · 26 unmodified lines
5757 #endif
5858 i = 0;
5959 while (&__init_array_start[i] != __init_array_end) {
60− (*__init_array_start[i++])();
60+#ifdef UNICODE
61+ (*__init_array_start[i++])(0, NULL, NULL);
62+#else
63+ (*__init_array_start[i++])(__argc, __targv, _tenviron);
64+#endif
6165 }
6266 retval = _tWinMain(GetModuleHandle(NULL), NULL, szCmd, fShow);
6367 i = 0;

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