aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-02-27 18:41:59 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-02-27 18:41:59 +0000
commit574f2f43948bb21d6e4187936ba5a5afccba25f6 (patch)
tree0b39aca564149e5ad30b3cc791228655ff1b1827
parentfe66a0eca1bfeae0abc0fc1e7d3709f271e05e82 (diff)
downloadbusybox-w32-574f2f43948bb21d6e4187936ba5a5afccba25f6.tar.gz
busybox-w32-574f2f43948bb21d6e4187936ba5a5afccba25f6.tar.bz2
busybox-w32-574f2f43948bb21d6e4187936ba5a5afccba25f6.zip
*: add optimization barrier to all "G trick" locations
-rw-r--r--archival/gzip.c4
-rw-r--r--coreutils/test.c1
-rw-r--r--docs/keep_data_small.txt4
-rw-r--r--editors/awk.c7
-rw-r--r--editors/diff.c2
-rw-r--r--editors/ed.c2
-rw-r--r--editors/vi.c2
-rw-r--r--include/libbb.h10
-rw-r--r--libbb/Kbuild1
-rw-r--r--libbb/appletlib.c1
-rw-r--r--libbb/lineedit.c1
-rw-r--r--libbb/messages.c4
-rw-r--r--libbb/ptr_to_globals.c11
-rw-r--r--miscutils/less.c16
-rw-r--r--networking/httpd.c2
-rw-r--r--networking/ifenslave.c2
-rw-r--r--networking/nc_bloaty.c9
-rw-r--r--networking/sendmail.c2
-rw-r--r--networking/traceroute.c2
-rw-r--r--procps/nmeter.c10
-rw-r--r--runit/svlogd.c2
-rw-r--r--shell/Kbuild7
-rw-r--r--shell/ash.c24
-rw-r--r--shell/ash_ptr_hack.c16
-rw-r--r--shell/hush.c5
-rw-r--r--shell/msh.c2
-rw-r--r--sysklogd/syslogd.c2
-rw-r--r--util-linux/fdisk.c2
-rw-r--r--util-linux/fsck_minix.c2
-rw-r--r--util-linux/mkfs_minix.c2
-rw-r--r--util-linux/more.c2
-rw-r--r--util-linux/script.c2
32 files changed, 97 insertions, 64 deletions
diff --git a/archival/gzip.c b/archival/gzip.c
index 00299b17c..36502faf7 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -2042,8 +2042,8 @@ int gzip_main(int argc, char **argv)
2042 //if (opt & 0x4) // -v 2042 //if (opt & 0x4) // -v
2043 argv += optind; 2043 argv += optind;
2044 2044
2045 PTR_TO_GLOBALS = xzalloc(sizeof(struct globals) + sizeof(struct globals2)) 2045 SET_PTR_TO_GLOBALS(xzalloc(sizeof(struct globals) + sizeof(struct globals2))
2046 + sizeof(struct globals); 2046 + sizeof(struct globals));
2047 G2.l_desc.dyn_tree = G2.dyn_ltree; 2047 G2.l_desc.dyn_tree = G2.dyn_ltree;
2048 G2.l_desc.static_tree = G2.static_ltree; 2048 G2.l_desc.static_tree = G2.static_ltree;
2049 G2.l_desc.extra_bits = extra_lbits; 2049 G2.l_desc.extra_bits = extra_lbits;
diff --git a/coreutils/test.c b/coreutils/test.c
index 22dadac0e..2f5b6b8a1 100644
--- a/coreutils/test.c
+++ b/coreutils/test.c
@@ -180,6 +180,7 @@ static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
180 180
181#define INIT_S() do { \ 181#define INIT_S() do { \
182 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \ 182 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
183 barrier(); \
183} while (0) 184} while (0)
184#define DEINIT_S() do { \ 185#define DEINIT_S() do { \
185 free(ptr_to_statics); \ 186 free(ptr_to_statics); \
diff --git a/docs/keep_data_small.txt b/docs/keep_data_small.txt
index 3ddbd81a4..2ddbefa10 100644
--- a/docs/keep_data_small.txt
+++ b/docs/keep_data_small.txt
@@ -99,9 +99,9 @@ and then declare that ptr_to_globals is a pointer to it:
99 99
100ptr_to_globals is declared as constant pointer. 100ptr_to_globals is declared as constant pointer.
101This helps gcc understand that it won't change, resulting in noticeably 101This helps gcc understand that it won't change, resulting in noticeably
102smaller code. In order to assign it, use PTR_TO_GLOBALS macro: 102smaller code. In order to assign it, use SET_PTR_TO_GLOBALS macro:
103 103
104 PTR_TO_GLOBALS = xzalloc(sizeof(G)); 104 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G)));
105 105
106Typically it is done in <applet>_main(). 106Typically it is done in <applet>_main().
107 107
diff --git a/editors/awk.c b/editors/awk.c
index 983b31116..f04ea5ced 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -394,7 +394,8 @@ static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
394/* Globals. Split in two parts so that first one is addressed 394/* Globals. Split in two parts so that first one is addressed
395 * with (mostly short) negative offsets */ 395 * with (mostly short) negative offsets */
396struct globals { 396struct globals {
397 chain beginseq, mainseq, endseq, *seq; 397 chain beginseq, mainseq, endseq;
398 chain *seq;
398 node *break_ptr, *continue_ptr; 399 node *break_ptr, *continue_ptr;
399 rstream *iF; 400 rstream *iF;
400 xhash *vhash, *ahash, *fdhash, *fnhash; 401 xhash *vhash, *ahash, *fdhash, *fnhash;
@@ -445,7 +446,7 @@ struct globals2 {
445 tsplitter fsplitter, rsplitter; 446 tsplitter fsplitter, rsplitter;
446}; 447};
447#define G1 (ptr_to_globals[-1]) 448#define G1 (ptr_to_globals[-1])
448#define G (*(struct globals2 *const)ptr_to_globals) 449#define G (*(struct globals2 *)ptr_to_globals)
449/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */ 450/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
450/* char G1size[sizeof(G1)]; - 0x6c */ 451/* char G1size[sizeof(G1)]; - 0x6c */
451/* char Gsize[sizeof(G)]; - 0x1cc */ 452/* char Gsize[sizeof(G)]; - 0x1cc */
@@ -485,7 +486,7 @@ struct globals2 {
485#define fsplitter (G.fsplitter ) 486#define fsplitter (G.fsplitter )
486#define rsplitter (G.rsplitter ) 487#define rsplitter (G.rsplitter )
487#define INIT_G() do { \ 488#define INIT_G() do { \
488 PTR_TO_GLOBALS = xzalloc(sizeof(G1) + sizeof(G)) + sizeof(G1); \ 489 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G1) + sizeof(G)) + sizeof(G1)); \
489 G.next_token__ltclass = TC_OPTERM; \ 490 G.next_token__ltclass = TC_OPTERM; \
490 G.evaluate__seed = 1; \ 491 G.evaluate__seed = 1; \
491} while (0) 492} while (0)
diff --git a/editors/diff.c b/editors/diff.c
index 64b7daa11..8844ec1c0 100644
--- a/editors/diff.c
+++ b/editors/diff.c
@@ -151,7 +151,7 @@ struct globals {
151#define stb1 (G.stb1 ) 151#define stb1 (G.stb1 )
152#define stb2 (G.stb2 ) 152#define stb2 (G.stb2 )
153#define INIT_G() do { \ 153#define INIT_G() do { \
154 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 154 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
155 context = 3; \ 155 context = 3; \
156 max_context = 64; \ 156 max_context = 64; \
157} while (0) 157} while (0)
diff --git a/editors/ed.c b/editors/ed.c
index a569788ad..15f0147be 100644
--- a/editors/ed.c
+++ b/editors/ed.c
@@ -51,7 +51,7 @@ struct globals {
51#define lines (G.lines ) 51#define lines (G.lines )
52#define marks (G.marks ) 52#define marks (G.marks )
53#define INIT_G() do { \ 53#define INIT_G() do { \
54 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 54 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
55} while (0) 55} while (0)
56 56
57 57
diff --git a/editors/vi.c b/editors/vi.c
index 6f4bd06f2..d2d267036 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -233,7 +233,7 @@ struct globals {
233#define readbuffer (G.readbuffer ) 233#define readbuffer (G.readbuffer )
234#define scr_out_buf (G.scr_out_buf ) 234#define scr_out_buf (G.scr_out_buf )
235#define INIT_G() do { \ 235#define INIT_G() do { \
236 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 236 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
237} while (0) 237} while (0)
238 238
239static int init_text_buffer(char *); // init from file or create new 239static int init_text_buffer(char *); // init from file or create new
diff --git a/include/libbb.h b/include/libbb.h
index 707e8d69b..8305e59fc 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1208,10 +1208,14 @@ extern char bb_common_bufsiz1[COMMON_BUFSIZE];
1208struct globals; 1208struct globals;
1209/* '*const' ptr makes gcc optimize code much better. 1209/* '*const' ptr makes gcc optimize code much better.
1210 * Magic prevents ptr_to_globals from going into rodata. 1210 * Magic prevents ptr_to_globals from going into rodata.
1211 * If you want to assign a value, use PTR_TO_GLOBALS = xxx */ 1211 * If you want to assign a value, use SET_PTR_TO_GLOBALS(x) */
1212extern struct globals *const ptr_to_globals; 1212extern struct globals *const ptr_to_globals;
1213#define PTR_TO_GLOBALS (*(struct globals**)&ptr_to_globals) 1213/* At least gcc 3.4.6 on mipsel system needs optimization barrier */
1214 1214#define barrier() asm volatile("":::"memory")
1215#define SET_PTR_TO_GLOBALS(x) do { \
1216 (*(struct globals**)&ptr_to_globals) = (x); \
1217 barrier(); \
1218} while (0)
1215 1219
1216/* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it, 1220/* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
1217 * use bb_default_login_shell and following defines. 1221 * use bb_default_login_shell and following defines.
diff --git a/libbb/Kbuild b/libbb/Kbuild
index fc87f625b..fd366559a 100644
--- a/libbb/Kbuild
+++ b/libbb/Kbuild
@@ -69,6 +69,7 @@ lib-y += pidfile.o
69lib-y += printable.o 69lib-y += printable.o
70lib-y += process_escape_sequence.o 70lib-y += process_escape_sequence.o
71lib-y += procps.o 71lib-y += procps.o
72lib-y += ptr_to_globals.o
72lib-y += read.o 73lib-y += read.o
73lib-y += recursive_action.o 74lib-y += recursive_action.o
74lib-y += remove_file.o 75lib-y += remove_file.o
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 793e3dc96..fec99c5ce 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -127,6 +127,7 @@ void lbb_prepare(const char *applet, char **argv)
127{ 127{
128#ifdef __GLIBC__ 128#ifdef __GLIBC__
129 (*(int **)&bb_errno) = __errno_location(); 129 (*(int **)&bb_errno) = __errno_location();
130 barrier();
130#endif 131#endif
131 applet_name = applet; 132 applet_name = applet;
132 133
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index c6aa45c93..d716169d4 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -146,6 +146,7 @@ static struct statics *const ptr_to_statics __attribute__ ((section (".data")));
146 146
147#define INIT_S() do { \ 147#define INIT_S() do { \
148 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \ 148 (*(struct statics**)&ptr_to_statics) = xzalloc(sizeof(S)); \
149 barrier(); \
149 cmdedit_termw = 80; \ 150 cmdedit_termw = 80; \
150 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \ 151 USE_FEATURE_EDITING_FANCY_PROMPT(num_ok_lines = 1;) \
151 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \ 152 USE_FEATURE_GETUSERNAME_AND_HOMEDIR(home_pwd_buf = (char*)null_str;) \
diff --git a/libbb/messages.c b/libbb/messages.c
index 2a011f815..74a070c49 100644
--- a/libbb/messages.c
+++ b/libbb/messages.c
@@ -71,7 +71,3 @@ const char bb_path_wtmp_file[] ALIGN1 =
71 * Since gcc insists on aligning struct global's members, it would be a pity 71 * Since gcc insists on aligning struct global's members, it would be a pity
72 * (and an alignment fault on some CPUs) to mess it up. */ 72 * (and an alignment fault on some CPUs) to mess it up. */
73char bb_common_bufsiz1[COMMON_BUFSIZE] __attribute__(( aligned(sizeof(long long)) )); 73char bb_common_bufsiz1[COMMON_BUFSIZE] __attribute__(( aligned(sizeof(long long)) ));
74
75struct globals;
76/* Make it reside in R/W memory: */
77struct globals *const ptr_to_globals __attribute__ ((section (".data")));
diff --git a/libbb/ptr_to_globals.c b/libbb/ptr_to_globals.c
new file mode 100644
index 000000000..f8ccbf142
--- /dev/null
+++ b/libbb/ptr_to_globals.c
@@ -0,0 +1,11 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2008 by Denys Vlasenko <vda.linux@googlemail.com>
4 *
5 * Licensed under GPLv2, see file LICENSE in this tarball for details.
6 */
7
8/* We cheat here. It is declared as const ptr in libbb.h,
9 * but here we make it live in R/W memory */
10struct globals;
11struct globals *ptr_to_globals;
diff --git a/miscutils/less.c b/miscutils/less.c
index da595f428..8d4f7ecd4 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -153,14 +153,14 @@ struct globals {
153#define term_orig (G.term_orig ) 153#define term_orig (G.term_orig )
154#define term_less (G.term_less ) 154#define term_less (G.term_less )
155#define INIT_G() do { \ 155#define INIT_G() do { \
156 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 156 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
157 less_gets_pos = -1; \ 157 less_gets_pos = -1; \
158 empty_line_marker = "~"; \ 158 empty_line_marker = "~"; \
159 num_files = 1; \ 159 num_files = 1; \
160 current_file = 1; \ 160 current_file = 1; \
161 eof_error = 1; \ 161 eof_error = 1; \
162 terminated = 1; \ 162 terminated = 1; \
163 } while (0) 163} while (0)
164 164
165/* Reset terminal input to normal */ 165/* Reset terminal input to normal */
166static void set_tty_cooked(void) 166static void set_tty_cooked(void)
diff --git a/networking/httpd.c b/networking/httpd.c
index 5be53179f..de84ccaa2 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -322,7 +322,7 @@ struct globals {
322#define http_error_page (G.http_error_page ) 322#define http_error_page (G.http_error_page )
323#define proxy (G.proxy ) 323#define proxy (G.proxy )
324#define INIT_G() do { \ 324#define INIT_G() do { \
325 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 325 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
326 USE_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \ 326 USE_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \
327 bind_addr_or_port = "80"; \ 327 bind_addr_or_port = "80"; \
328 file_size = -1; \ 328 file_size = -1; \
diff --git a/networking/ifenslave.c b/networking/ifenslave.c
index 774d7c2d2..76aaa7614 100644
--- a/networking/ifenslave.c
+++ b/networking/ifenslave.c
@@ -129,7 +129,7 @@ struct globals {
129#define master (G.master ) 129#define master (G.master )
130#define slave (G.slave ) 130#define slave (G.slave )
131#define INIT_G() do { \ 131#define INIT_G() do { \
132 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 132 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
133} while (0) 133} while (0)
134 134
135 135
diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c
index dd62e5df7..cd014781b 100644
--- a/networking/nc_bloaty.c
+++ b/networking/nc_bloaty.c
@@ -97,7 +97,6 @@ struct globals {
97}; 97};
98 98
99#define G (*ptr_to_globals) 99#define G (*ptr_to_globals)
100
101#define wrote_out (G.wrote_out ) 100#define wrote_out (G.wrote_out )
102#define wrote_net (G.wrote_net ) 101#define wrote_net (G.wrote_net )
103#define ouraddr (G.ouraddr ) 102#define ouraddr (G.ouraddr )
@@ -115,6 +114,10 @@ struct globals {
115#else 114#else
116#define o_interval 0 115#define o_interval 0
117#endif 116#endif
117#define INIT_G() do { \
118 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
119} while (0)
120
118 121
119/* Must match getopt32 call! */ 122/* Must match getopt32 call! */
120enum { 123enum {
@@ -678,9 +681,7 @@ int nc_main(int argc, char **argv)
678 int x; 681 int x;
679 unsigned o_lport = 0; 682 unsigned o_lport = 0;
680 683
681 /* I was in this barbershop quartet in Skokie IL ... */ 684 INIT_G();
682 /* round up the usual suspects, i.e. malloc up all the stuff we need */
683 PTR_TO_GLOBALS = xzalloc(sizeof(G));
684 685
685 /* catch a signal or two for cleanup */ 686 /* catch a signal or two for cleanup */
686 bb_signals(0 687 bb_signals(0
diff --git a/networking/sendmail.c b/networking/sendmail.c
index 3a6078f79..eb356dcf5 100644
--- a/networking/sendmail.c
+++ b/networking/sendmail.c
@@ -77,7 +77,7 @@ struct globals {
77#define xargs (G.xargs ) 77#define xargs (G.xargs )
78#define fargs (G.fargs ) 78#define fargs (G.fargs )
79#define INIT_G() do { \ 79#define INIT_G() do { \
80 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 80 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
81 xargs[0] = "openssl"; \ 81 xargs[0] = "openssl"; \
82 xargs[1] = "s_client"; \ 82 xargs[1] = "s_client"; \
83 xargs[2] = "-quiet"; \ 83 xargs[2] = "-quiet"; \
diff --git a/networking/traceroute.c b/networking/traceroute.c
index 2ba558fc2..582840af6 100644
--- a/networking/traceroute.c
+++ b/networking/traceroute.c
@@ -378,7 +378,7 @@ struct globals {
378#define wherefrom (G.wherefrom) 378#define wherefrom (G.wherefrom)
379#define gwlist (G.gwlist ) 379#define gwlist (G.gwlist )
380#define INIT_G() do { \ 380#define INIT_G() do { \
381 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 381 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
382 maxpacket = 32 * 1024; \ 382 maxpacket = 32 * 1024; \
383 port = 32768 + 666; \ 383 port = 32768 + 666; \
384 waittime = 5; \ 384 waittime = 5; \
diff --git a/procps/nmeter.c b/procps/nmeter.c
index b8ba3facb..573052921 100644
--- a/procps/nmeter.c
+++ b/procps/nmeter.c
@@ -75,11 +75,11 @@ struct globals {
75#define proc_diskstats (G.proc_diskstats ) 75#define proc_diskstats (G.proc_diskstats )
76#define proc_sys_fs_filenr (G.proc_sys_fs_filenr) 76#define proc_sys_fs_filenr (G.proc_sys_fs_filenr)
77#define INIT_G() do { \ 77#define INIT_G() do { \
78 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 78 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
79 cur_outbuf = outbuf; \ 79 cur_outbuf = outbuf; \
80 final_str = "\n"; \ 80 final_str = "\n"; \
81 deltanz = delta = 1000000; \ 81 deltanz = delta = 1000000; \
82 } while (0) 82} while (0)
83 83
84// We depend on this being a char[], not char* - we take sizeof() of it 84// We depend on this being a char[], not char* - we take sizeof() of it
85#define outbuf bb_common_bufsiz1 85#define outbuf bb_common_bufsiz1
diff --git a/runit/svlogd.c b/runit/svlogd.c
index 73570dad0..d7da18093 100644
--- a/runit/svlogd.c
+++ b/runit/svlogd.c
@@ -106,7 +106,7 @@ struct globals {
106#define fl_flag_0 (G.fl_flag_0 ) 106#define fl_flag_0 (G.fl_flag_0 )
107#define dirn (G.dirn ) 107#define dirn (G.dirn )
108#define INIT_G() do { \ 108#define INIT_G() do { \
109 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 109 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
110 linemax = 1000; \ 110 linemax = 1000; \
111 /*buflen = 1024;*/ \ 111 /*buflen = 1024;*/ \
112 linecomplete = 1; \ 112 linecomplete = 1; \
diff --git a/shell/Kbuild b/shell/Kbuild
index 36a8ffd3a..deedc24f3 100644
--- a/shell/Kbuild
+++ b/shell/Kbuild
@@ -5,8 +5,7 @@
5# Licensed under the GPL v2, see the file LICENSE in this tarball. 5# Licensed under the GPL v2, see the file LICENSE in this tarball.
6 6
7lib-y:= 7lib-y:=
8lib-$(CONFIG_ASH) += ash.o 8lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o
9lib-$(CONFIG_HUSH) += hush.o 9lib-$(CONFIG_HUSH) += hush.o
10lib-$(CONFIG_MSH) += msh.o 10lib-$(CONFIG_MSH) += msh.o
11
12lib-$(CONFIG_CTTYHACK) += cttyhack.o 11lib-$(CONFIG_CTTYHACK) += cttyhack.o
diff --git a/shell/ash.c b/shell/ash.c
index 9c762e2ed..9b1a73024 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -202,9 +202,8 @@ struct globals_misc {
202 /* indicates specified signal received */ 202 /* indicates specified signal received */
203 char gotsig[NSIG - 1]; 203 char gotsig[NSIG - 1];
204}; 204};
205/* Make it reside in writable memory, yet make compiler understand that it is not going to change. */ 205extern struct globals_misc *const ash_ptr_to_globals_misc;
206static struct globals_misc *const ptr_to_globals_misc __attribute__ ((section (".data"))); 206#define G_misc (*ash_ptr_to_globals_misc)
207#define G_misc (*ptr_to_globals_misc)
208#define rootpid (G_misc.rootpid ) 207#define rootpid (G_misc.rootpid )
209#define shlvl (G_misc.shlvl ) 208#define shlvl (G_misc.shlvl )
210#define minusc (G_misc.minusc ) 209#define minusc (G_misc.minusc )
@@ -223,7 +222,8 @@ static struct globals_misc *const ptr_to_globals_misc __attribute__ ((section ("
223#define sigmode (G_misc.sigmode ) 222#define sigmode (G_misc.sigmode )
224#define gotsig (G_misc.gotsig ) 223#define gotsig (G_misc.gotsig )
225#define INIT_G_misc() do { \ 224#define INIT_G_misc() do { \
226 (*(struct globals_misc**)&ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ 225 (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
226 barrier(); \
227 curdir = nullstr; \ 227 curdir = nullstr; \
228 physdir = nullstr; \ 228 physdir = nullstr; \
229} while (0) 229} while (0)
@@ -1147,9 +1147,8 @@ struct globals_memstack {
1147 int herefd; // = -1; 1147 int herefd; // = -1;
1148 struct stack_block stackbase; 1148 struct stack_block stackbase;
1149}; 1149};
1150/* Make it reside in writable memory, yet make compiler understand that it is not going to change. */ 1150extern struct globals_memstack *const ash_ptr_to_globals_memstack;
1151static struct globals_memstack *const ptr_to_globals_memstack __attribute__ ((section (".data"))); 1151#define G_memstack (*ash_ptr_to_globals_memstack)
1152#define G_memstack (*ptr_to_globals_memstack)
1153#define g_stackp (G_memstack.g_stackp ) 1152#define g_stackp (G_memstack.g_stackp )
1154#define markp (G_memstack.markp ) 1153#define markp (G_memstack.markp )
1155#define g_stacknxt (G_memstack.g_stacknxt ) 1154#define g_stacknxt (G_memstack.g_stacknxt )
@@ -1158,7 +1157,8 @@ static struct globals_memstack *const ptr_to_globals_memstack __attribute__ ((se
1158#define herefd (G_memstack.herefd ) 1157#define herefd (G_memstack.herefd )
1159#define stackbase (G_memstack.stackbase ) 1158#define stackbase (G_memstack.stackbase )
1160#define INIT_G_memstack() do { \ 1159#define INIT_G_memstack() do { \
1161 (*(struct globals_memstack**)&ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ 1160 (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \
1161 barrier(); \
1162 g_stackp = &stackbase; \ 1162 g_stackp = &stackbase; \
1163 g_stacknxt = stackbase.space; \ 1163 g_stacknxt = stackbase.space; \
1164 g_stacknleft = MINSIZE; \ 1164 g_stacknleft = MINSIZE; \
@@ -1778,9 +1778,8 @@ struct globals_var {
1778 struct var *vartab[VTABSIZE]; 1778 struct var *vartab[VTABSIZE];
1779 struct var varinit[ARRAY_SIZE(varinit_data)]; 1779 struct var varinit[ARRAY_SIZE(varinit_data)];
1780}; 1780};
1781/* Make it reside in writable memory, yet make compiler understand that it is not going to change. */ 1781extern struct globals_var *const ash_ptr_to_globals_var;
1782static struct globals_var *const ptr_to_globals_var __attribute__ ((section (".data"))); 1782#define G_var (*ash_ptr_to_globals_var)
1783#define G_var (*ptr_to_globals_var)
1784#define shellparam (G_var.shellparam ) 1783#define shellparam (G_var.shellparam )
1785#define redirlist (G_var.redirlist ) 1784#define redirlist (G_var.redirlist )
1786#define g_nullredirs (G_var.g_nullredirs ) 1785#define g_nullredirs (G_var.g_nullredirs )
@@ -1789,7 +1788,8 @@ static struct globals_var *const ptr_to_globals_var __attribute__ ((section (".d
1789#define varinit (G_var.varinit ) 1788#define varinit (G_var.varinit )
1790#define INIT_G_var() do { \ 1789#define INIT_G_var() do { \
1791 int i; \ 1790 int i; \
1792 (*(struct globals_var**)&ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ 1791 (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \
1792 barrier(); \
1793 for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ 1793 for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
1794 varinit[i].flags = varinit_data[i].flags; \ 1794 varinit[i].flags = varinit_data[i].flags; \
1795 varinit[i].text = varinit_data[i].text; \ 1795 varinit[i].text = varinit_data[i].text; \
diff --git a/shell/ash_ptr_hack.c b/shell/ash_ptr_hack.c
new file mode 100644
index 000000000..490b73b6d
--- /dev/null
+++ b/shell/ash_ptr_hack.c
@@ -0,0 +1,16 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2008 by Denys Vlasenko <vda.linux@googlemail.com>
4 *
5 * Licensed under GPLv2, see file LICENSE in this tarball for details.
6 */
7
8/* We cheat here. They are declared as const ptr in ash.c,
9 * but here we make them live in R/W memory */
10struct globals_misc;
11struct globals_memstack;
12struct globals_var;
13
14struct globals_misc *ash_ptr_to_globals_misc;
15struct globals_memstack *ash_ptr_to_globals_memstack;
16struct globals_var *ash_ptr_to_globals_var;
diff --git a/shell/hush.c b/shell/hush.c
index d9ef2390e..b44f35bdd 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -460,6 +460,9 @@ enum { run_list_level = 0 };
460#endif 460#endif
461#define charmap (G.charmap ) 461#define charmap (G.charmap )
462#define user_input_buf (G.user_input_buf ) 462#define user_input_buf (G.user_input_buf )
463#define INIT_G() do { \
464 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
465} while (0)
463 466
464 467
465#define B_CHUNK 100 468#define B_CHUNK 100
@@ -3778,7 +3781,7 @@ int hush_main(int argc, char **argv)
3778 char **e; 3781 char **e;
3779 struct variable *cur_var; 3782 struct variable *cur_var;
3780 3783
3781 PTR_TO_GLOBALS = xzalloc(sizeof(G)); 3784 INIT_G();
3782 3785
3783 /* Deal with HUSH_VERSION */ 3786 /* Deal with HUSH_VERSION */
3784 shell_ver = const_shell_ver; /* copying struct here */ 3787 shell_ver = const_shell_ver; /* copying struct here */
diff --git a/shell/msh.c b/shell/msh.c
index 917b08a1e..569011bbd 100644
--- a/shell/msh.c
+++ b/shell/msh.c
@@ -705,7 +705,7 @@ struct globals {
705#define child_cmd (G.child_cmd ) 705#define child_cmd (G.child_cmd )
706#define iostack (G.iostack ) 706#define iostack (G.iostack )
707#define INIT_G() do { \ 707#define INIT_G() do { \
708 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 708 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
709 global_env.linep = line; \ 709 global_env.linep = line; \
710 global_env.iobase = iostack; \ 710 global_env.iobase = iostack; \
711 global_env.iop = iostack - 1; \ 711 global_env.iop = iostack - 1; \
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index e54ade7fd..596984e3f 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -136,7 +136,7 @@ static const struct init_globals init_data = {
136 136
137#define G (*ptr_to_globals) 137#define G (*ptr_to_globals)
138#define INIT_G() do { \ 138#define INIT_G() do { \
139 PTR_TO_GLOBALS = memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data)); \ 139 SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \
140} while (0) 140} while (0)
141 141
142 142
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index c98a74fc0..827ea21f3 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -309,7 +309,7 @@ struct globals {
309#define MBRbuffer (G.MBRbuffer) 309#define MBRbuffer (G.MBRbuffer)
310#define ptes (G.ptes) 310#define ptes (G.ptes)
311#define INIT_G() do { \ 311#define INIT_G() do { \
312 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 312 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
313 sector_size = DEFAULT_SECTOR_SIZE; \ 313 sector_size = DEFAULT_SECTOR_SIZE; \
314 sector_offset = 1; \ 314 sector_offset = 1; \
315 g_partitions = 4; \ 315 g_partitions = 4; \
diff --git a/util-linux/fsck_minix.c b/util-linux/fsck_minix.c
index deb82f75b..62e4f08b6 100644
--- a/util-linux/fsck_minix.c
+++ b/util-linux/fsck_minix.c
@@ -190,7 +190,7 @@ struct globals {
190#define check_file_blk (G.check_file_blk ) 190#define check_file_blk (G.check_file_blk )
191#define current_name (G.current_name ) 191#define current_name (G.current_name )
192#define INIT_G() do { \ 192#define INIT_G() do { \
193 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 193 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
194 dirsize = 16; \ 194 dirsize = 16; \
195 namelen = 14; \ 195 namelen = 14; \
196 current_name[0] = '/'; \ 196 current_name[0] = '/'; \
diff --git a/util-linux/mkfs_minix.c b/util-linux/mkfs_minix.c
index 118b95efb..a784b72f4 100644
--- a/util-linux/mkfs_minix.c
+++ b/util-linux/mkfs_minix.c
@@ -122,7 +122,7 @@ struct globals {
122}; 122};
123#define G (*ptr_to_globals) 123#define G (*ptr_to_globals)
124#define INIT_G() do { \ 124#define INIT_G() do { \
125 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 125 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
126} while (0) 126} while (0)
127 127
128static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n) 128static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
diff --git a/util-linux/more.c b/util-linux/more.c
index eeeea509e..2d5535991 100644
--- a/util-linux/more.c
+++ b/util-linux/more.c
@@ -28,9 +28,7 @@ struct globals {
28 struct termios new_settings; 28 struct termios new_settings;
29}; 29};
30#define G (*(struct globals*)bb_common_bufsiz1) 30#define G (*(struct globals*)bb_common_bufsiz1)
31//#define G (*ptr_to_globals)
32#define INIT_G() ((void)0) 31#define INIT_G() ((void)0)
33//#define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G))
34#define initial_settings (G.initial_settings) 32#define initial_settings (G.initial_settings)
35#define new_settings (G.new_settings ) 33#define new_settings (G.new_settings )
36#define cin_fileno (G.cin_fileno ) 34#define cin_fileno (G.cin_fileno )
diff --git a/util-linux/script.c b/util-linux/script.c
index fda726ed9..e6dbb1aa2 100644
--- a/util-linux/script.c
+++ b/util-linux/script.c
@@ -25,7 +25,7 @@ struct globals {
25#define tt (G.tt ) 25#define tt (G.tt )
26#define fname (G.fname ) 26#define fname (G.fname )
27#define INIT_G() do { \ 27#define INIT_G() do { \
28 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \ 28 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
29 fname = "typescript"; \ 29 fname = "typescript"; \
30} while (0) 30} while (0)
31 31