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
54static void set_exception_handler(void);54static void set_exception_handler(void);
55static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);55static int _rt_error(void *fp, void *ip, const char *fmt, va_list ap);
56static void rt_exit(int code);56static void rt_exit(int code);
57static void init_atexit(void);
58static void run_atexit(void);
59static int rt_atexit(void (*function)(void));
57#endif /* CONFIG_TCC_BACKTRACE */60#endif /* CONFIG_TCC_BACKTRACE */
58 61
59/* defined when included from lib/bt-exe.c */62/* 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
169 if (s1->do_debug)172 if (s1->do_debug)
170 tcc_add_symbol(s1, "exit", rt_exit);173 tcc_add_symbol(s1, "exit", rt_exit);
171#endif174#endif
175 tcc_add_symbol(s1, "atexit", rt_atexit);
172 if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)176 if (tcc_relocate(s1, TCC_RELOCATE_AUTO) < 0)
173 return -1;177 return -1;
174 prog_main = (void*)get_sym_addr(s1, s1->runtime_main, 1, 1);178 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
215 errno = 0; /* clean errno value */219 errno = 0; /* clean errno value */
216 fflush(stdout);220 fflush(stdout);
217 fflush(stderr);221 fflush(stderr);
222 init_atexit();
218 /* These aren't C symbols, so don't need leading underscore handling. */223 /* These aren't C symbols, so don't need leading underscore handling. */
219 run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);224 run_cdtors(s1, "__init_array_start", "__init_array_end", argc, argv, envp);
220#ifdef CONFIG_TCC_BACKTRACE225#ifdef CONFIG_TCC_BACKTRACE
@@ -224,6 +229,7 @@ LIBTCCAPI int tcc_run(TCCState *s1, int argc, char **argv) · 3 unmodified lines
224 ret = prog_main(argc, argv, envp);229 ret = prog_main(argc, argv, envp);
225 }230 }
226 run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);231 run_cdtors(s1, "__fini_array_start", "__fini_array_end", 0, NULL, NULL);
232 run_atexit();
227 if ((s1->dflag & 16) && ret)233 if ((s1->dflag & 16) && ret)
228 fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);234 fprintf(s1->ppfp, "[returns %d]\n", ret), fflush(s1->ppfp);
229 return ret;235 return ret;
@@ -1022,6 +1028,29 @@ static void rt_exit(int code) · 792 unmodified lines
1022 exit(code);1028 exit(code);
1023}1029}
1024 1030
1031#define NR_AT_EXIT 32
1032
1033static int nr_atexit = 0;
1034static void (*at_exitfunc[NR_AT_EXIT])(void);
1035
1036static void init_atexit(void)
1037{
1038 nr_atexit = 0;
1039}
1040
1041static void run_atexit(void)
1042{
1043 while (nr_atexit)
1044 at_exitfunc[--nr_atexit]();
1045}
1046
1047static 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
1025/* ------------------------------------------------------------- */1054/* ------------------------------------------------------------- */
1026 1055
1027#ifndef _WIN321056#ifndef _WIN32
tests/tests2/128_run_atexit.c+20−0
@@ -0,0 +1,20 @@
1#include <stdio.h>
2#include <stdlib.h>
3
4void cleanup1(void)
5{
6 printf ("cleanup1\n");
7}
8
9void cleanup2(void)
10{
11 printf ("cleanup2\n");
12}
13
14int 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 @@
1cleanup2
2cleanup1

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