History

Allow atexit to be used with -run

Add atexit code to tccrun.c and added testcase 128

herman ten brugge committed 3 years ago

Commit ac0604a1 parent 065b4013 files · +51 −0Browse files

Unified · Split

3 files
  1. tccrun.c +29 −0
  2. tests/tests2/128_run_atexit.c +20 −0
  3. tests/tests2/128_run_atexit.expect +2 −0
tccrun.c+29−0
@@ -54,6 +54,9 @@ static rt_context g_rtctxt; · 53 unmodified lines
5454 static void set_exception_handler(void);
5555 static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);
5656 static void rt_exit(int code);
57+static void init_atexit(void);
58+static void run_atexit(void);
59+static int rt_atexit(void (*function)(void));
5760 #endif /* CONFIG_TCC_BACKTRACE */
5861
5962 /* defined when included from lib/bt-exe.c */
@@ -169,6 +172,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 109 unmodified lines
169172 if (s1->do_debug)
170173 tcc_add_symbol(s1, "exit", rt_exit);
171174 #endif
175+ tcc_add_symbol(s1, "atexit", rt_atexit);
172176 if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
173177 return -1;
174178 prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1);
@@ -215,6 +219,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 40 unmodified lines
215219 errno = 0; /* clean errno value */
216220 fflush(stdout);
217221 fflush(stderr);
222+ init_atexit();
218223 /* These aren't C symbols, so don't need leading underscore handling. */
219224 run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);
220225 #ifdef CONFIG_TCC_BACKTRACE
@@ -224,6 +229,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 3 unmodified lines
224229 ret = prog_main(argc, argv, envp);
225230 }
226231 run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);
232+ run_atexit();
227233 if ((s1->dflag & 16) && ret)
228234 fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
229235 return ret;
@@ -1022,6 +1028,29 @@ static void rt_exit(int code) · 792 unmodified lines
10221028 exit(code);
10231029 }
10241030
1031+#define NR_AT_EXIT 32
1032+
1033+static int nr_atexit = 0;
1034+static void (*at_exitfunc[NR_AT_EXIT])(void);
1035+
1036+static void init_atexit(void)
1037+{
1038+ nr_atexit = 0;
1039+}
1040+
1041+static void run_atexit(void)
1042+{
1043+ while (nr_atexit)
1044+ at_exitfunc[--nr_atexit]();
1045+}
1046+
1047+static int rt_atexit(void (*function)(void))
1048+{
1049+ if (nr_atexit < NR_AT_EXIT)
1050+ at_exitfunc[nr_atexit++] = function;
1051+ return 1;
1052+}
1053+
10251054 /* ------------------------------------------------------------- */
10261055
10271056 #ifndef _WIN32
tests/tests2/128_run_atexit.c+20−0
@@ -0,0 +1,20 @@
1+#include <stdio.h>
2+#include <stdlib.h>
3+
4+void cleanup1(void)
5+{
6+ printf ("cleanup1\n");
7+}
8+
9+void cleanup2(void)
10+{
11+ printf ("cleanup2\n");
12+}
13+
14+int main(int argc, char **argv)
15+{
16+ atexit(cleanup1);
17+ atexit(cleanup2);
18+ return 0;
19+}
20+
tests/tests2/128_run_atexit.expect+2−0
@@ -0,0 +1,2 @@
1+cleanup2
2+cleanup1

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