From e6a2f4cc5a47d3022bdf5ca2cacbaa5a8c5baf7a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 16:26:30 +0200 Subject: libbb: make bb_common_bufsiz1 1 kbyte, add capability to use bss tail for it The config item is FEATURE_USE_BSS_TAIL. When it is off (default): function old new delta read_config 210 228 +18 doCommands 2279 2294 +15 ipneigh_list_or_flush 763 772 +9 ipaddr_list_or_flush 1256 1261 +5 display_process_list 1301 1306 +5 conspy_main 1378 1383 +5 do_lzo_compress 352 355 +3 do_lzo_decompress 565 567 +2 push 46 44 -2 inetd_main 2136 2134 -2 uevent_main 421 418 -3 addLines 97 92 -5 bb_common_bufsiz1 8193 1024 -7169 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 8/5 up/down: 62/-7181) Total: -7119 bytes text data bss dec hex filename 829850 4086 9080 843016 cdd08 busybox_old 829901 4086 1904 835891 cc133 busybox_unstripped FEATURE_USE_BSS_TAIL=y: read_config 210 228 +18 doCommands 2279 2294 +15 ipneigh_list_or_flush 763 772 +9 ipaddr_list_or_flush 1256 1261 +5 display_process_list 1301 1306 +5 conspy_main 1378 1383 +5 do_lzo_compress 352 355 +3 do_lzo_decompress 565 567 +2 inetd_main 2136 2134 -2 bb_common_bufsiz1 8193 - -8193 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 8/1 up/down: 62/-8195) Total: -8133 bytes text data bss dec hex filename 829850 4086 9080 843016 cdd08 busybox_old 829911 4086 880 834877 cbd3d busybox_unstripped FIXME: setup_common_bufsiz() calls are missing. Signed-off-by: Denys Vlasenko --- scripts/generate_BUFSIZ.sh | 114 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100755 scripts/generate_BUFSIZ.sh (limited to 'scripts') diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh new file mode 100755 index 000000000..afe9eee1e --- /dev/null +++ b/scripts/generate_BUFSIZ.sh @@ -0,0 +1,114 @@ +#!/bin/sh +# Called from top-level directory a-la +# +# scripts/generate_BUFSIZ.sh include/common_bufsiz.h + +. ./.config || exit 1 + +debug=false + +common_bufsiz_h=$1 + +test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm" +test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc" + +regenerate() { + cat >"$1.$$" + test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return + mv "$1.$$" "$1" +} + +generate_std_and_exit() { + $debug && echo "Default: bb_common_bufsiz1[] in bss" + { + echo "enum { COMMON_BUFSIZE = 1024 };" + echo "extern char bb_common_bufsiz1[];" + echo "#define setup_common_bufsiz() ((void)0)" + } | regenerate "$common_bufsiz_h" + exit 0 +} + +# User does not want any funky stuff? +test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit + +test -f busybox_unstripped || { + # We did not try anything yet + $debug && echo "Will try to fit bb_common_bufsiz1[] into _end[]" + { + echo "enum { COMMON_BUFSIZE = 1024 };" + echo "extern char _end[]; /* linker-provided label */" + echo "#define bb_common_bufsiz1 _end" + echo "#define setup_common_bufsiz() ((void)0)" + } | regenerate "$common_bufsiz_h" + echo 1024 >"$common_bufsiz_h.BUFSIZE" + exit 0 +} + +# Get _end address +END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1` +test x"$END" = x"" && generate_std_and_exit +$debug && echo "END:0x$END $((0x$END))" +END=$((0x$END)) + +# Get PAGE_SIZE +echo "\ +#include +#if defined(PAGE_SIZE) && PAGE_SIZE > 0 +char page_size[PAGE_SIZE]; +#else +char page_size[1]; +#endif +" >page_size_$$.c +$CC -c "page_size_$$.c" || generate_std_and_exit +PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1` +rm "page_size_$$.c" "page_size_$$.o" +test x"$PAGE_SIZE" = x"" && generate_std_and_exit +$debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))" +PAGE_SIZE=$((0x$PAGE_SIZE)) +test $PAGE_SIZE -lt 1024 && generate_std_and_exit + +# How much space between _end[] and next page? +PAGE_MASK=$((PAGE_SIZE-1)) +REM=$(( (-END) & PAGE_MASK )) +$debug && echo "REM:$REM" + +if test $REM -lt 1024; then + # _end[] has no enough space for bb_common_bufsiz1[], + # users will need to malloc it. + { + echo "enum { COMMON_BUFSIZE = 1024 };" + echo "extern char *bb_common_bufsiz1;" + echo "void setup_common_bufsiz(void);" + } | regenerate "$common_bufsiz_h" + # Check that we aren't left with a buggy binary: + if test -f "$common_bufsiz_h.BUFSIZE"; then + rm "$common_bufsiz_h.BUFSIZE" + echo "Warning! Space in _end[] is too small ($REM bytes)!" + echo "Rerun make to build a binary which doesn't use it!" + exit 1 + fi + exit 0 +fi + +# _end[] has REM bytes for bb_common_bufsiz1[] +OLD=1024 +test -f "$common_bufsiz_h.BUFSIZE" && OLD=`cat "$common_bufsiz_h.BUFSIZE"` +$debug && echo "OLD:$OLD" +{ +echo "enum { COMMON_BUFSIZE = $REM };" +echo "extern char _end[]; /* linker-provided label */" +echo "#define bb_common_bufsiz1 _end" +echo "#define setup_common_bufsiz() ((void)0)" +} | regenerate "$common_bufsiz_h" +echo $REM >"$common_bufsiz_h.BUFSIZE" + +# Check that code did not grow too much and thus _end[] did not shink: +if test $OLD -gt $REM; then + echo "Warning! Space in _end[] has decreased from $OLD to $REM bytes!" + echo "Rerun make!" + exit 1 +fi + +if test $OLD != $REM; then + echo "Space in _end[] is $REM bytes. Rerun make to use larger COMMON_BUFSIZE." +fi -- cgit v1.2.3-55-g6feb From 47cfbf32fd66563f8c4e09ad6cced6abfbe2fad5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 18:18:48 +0200 Subject: *: add most of the required setup_common_bufsiz() calls Signed-off-by: Denys Vlasenko --- archival/cpio.c | 1 + archival/lzop.c | 2 +- archival/rpm.c | 2 +- console-tools/resize.c | 3 +++ coreutils/dd.c | 1 + coreutils/du.c | 2 +- coreutils/expr.c | 2 +- coreutils/ls.c | 1 + coreutils/od_bloaty.c | 1 + coreutils/tail.c | 2 +- debianutils/run_parts.c | 2 +- debianutils/start_stop_daemon.c | 1 + e2fsprogs/fsck.c | 1 + editors/sed.c | 1 + findutils/find.c | 1 + findutils/grep.c | 1 + findutils/xargs.c | 1 + init/bootchartd.c | 2 +- loginutils/login.c | 2 +- miscutils/crond.c | 1 + miscutils/dc.c | 1 + miscutils/hdparm.c | 1 + networking/arp.c | 1 + networking/arping.c | 1 + networking/ftpd.c | 1 + networking/ftpgetput.c | 1 + networking/ifupdown.c | 2 +- networking/inetd.c | 1 + networking/ping.c | 3 ++- networking/slattach.c | 2 +- networking/tc.c | 1 + networking/tcpudp.c | 1 + networking/telnet.c | 1 + networking/telnetd.c | 1 + networking/tftp.c | 1 + networking/udhcp/dhcprelay.c | 3 +++ networking/zcip.c | 2 +- procps/free.c | 2 +- procps/fuser.c | 1 + procps/ps.c | 2 +- procps/top.c | 1 + runit/runsv.c | 1 + runit/runsvdir.c | 2 +- runit/sv.c | 2 +- scripts/generate_BUFSIZ.sh | 2 ++ selinux/setfiles.c | 1 + sysklogd/logread.c | 1 + util-linux/mdev.c | 1 + util-linux/mkswap.c | 3 +++ util-linux/more.c | 2 +- util-linux/mount.c | 2 +- util-linux/swaponoff.c | 2 +- util-linux/uevent.c | 3 +++ 53 files changed, 63 insertions(+), 19 deletions(-) (limited to 'scripts') diff --git a/archival/cpio.c b/archival/cpio.c index a3036e1ab..3b1550720 100644 --- a/archival/cpio.c +++ b/archival/cpio.c @@ -174,6 +174,7 @@ struct globals { #define G (*(struct globals*)bb_common_bufsiz1) void BUG_cpio_globals_too_big(void); #define INIT_G() do { \ + setup_common_bufsiz(); \ G.owner_ugid.uid = -1L; \ G.owner_ugid.gid = -1L; \ } while (0) diff --git a/archival/lzop.c b/archival/lzop.c index 1371c9751..4afa21889 100644 --- a/archival/lzop.c +++ b/archival/lzop.c @@ -445,7 +445,7 @@ struct globals { chksum_t chksum_out; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) //#define G (*ptr_to_globals) //#define INIT_G() do { // SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); diff --git a/archival/rpm.c b/archival/rpm.c index 079b7a95b..83160f975 100644 --- a/archival/rpm.c +++ b/archival/rpm.c @@ -95,7 +95,7 @@ struct globals { int tagcount; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void extract_cpio(int fd, const char *source_rpm) { diff --git a/console-tools/resize.c b/console-tools/resize.c index ed80aa082..a3342a195 100644 --- a/console-tools/resize.c +++ b/console-tools/resize.c @@ -19,6 +19,7 @@ #define ESC "\033" #define old_termios_p ((struct termios*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void onintr(int sig UNUSED_PARAM) @@ -34,6 +35,8 @@ int resize_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) struct winsize w = { 0, 0, 0, 0 }; int ret; + INIT_G(); + /* We use _stderr_ in order to make resize usable * in shell backticks (those redirect stdout away from tty). * NB: other versions of resize open "/dev/tty" diff --git a/coreutils/dd.c b/coreutils/dd.c index a5b8882a0..4dc302926 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c @@ -111,6 +111,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ /* we have to zero it out because of NOEXEC */ \ memset(&G, 0, sizeof(G)); \ } while (0) diff --git a/coreutils/du.c b/coreutils/du.c index 3d6777670..1240bcbbc 100644 --- a/coreutils/du.c +++ b/coreutils/du.c @@ -87,7 +87,7 @@ struct globals { dev_t dir_dev; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void print(unsigned long long size, const char *filename) diff --git a/coreutils/expr.c b/coreutils/expr.c index 59a66d9c5..ce6b2d189 100644 --- a/coreutils/expr.c +++ b/coreutils/expr.c @@ -101,7 +101,7 @@ struct globals { char **args; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) /* forward declarations */ static VALUE *eval(void); diff --git a/coreutils/ls.c b/coreutils/ls.c index e8c3e0490..344b4e61e 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -368,6 +368,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ /* we have to zero it out because of NOEXEC */ \ memset(&G, 0, sizeof(G)); \ IF_FEATURE_AUTOWIDTH(G_terminal_width = TERMINAL_WIDTH;) \ diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c index 1e252caf0..c8a654165 100644 --- a/coreutils/od_bloaty.c +++ b/coreutils/od_bloaty.c @@ -217,6 +217,7 @@ enum { G_pseudo_offset = 0 }; #endif #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ G.bytes_per_block = 32; \ } while (0) diff --git a/coreutils/tail.c b/coreutils/tail.c index cdc9fb66a..39f87679e 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -56,7 +56,7 @@ struct globals { bool exitcode; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void tail_xprint_header(const char *fmt, const char *filename) { diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c index a5e53576c..c671b9252 100644 --- a/debianutils/run_parts.c +++ b/debianutils/run_parts.c @@ -100,7 +100,7 @@ struct globals { #define names (G.names) #define cur (G.cur ) #define cmd (G.cmd ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 }; diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c index 6b8d53b13..3625ffee8 100644 --- a/debianutils/start_stop_daemon.c +++ b/debianutils/start_stop_daemon.c @@ -200,6 +200,7 @@ struct globals { #define user_id (G.user_id ) #define signal_nr (G.signal_nr ) #define INIT_G() do { \ + setup_common_bufsiz(); \ user_id = -1; \ signal_nr = 15; \ } while (0) diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c index b534568c2..59514a1a6 100644 --- a/e2fsprogs/fsck.c +++ b/e2fsprogs/fsck.c @@ -172,6 +172,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/editors/sed.c b/editors/sed.c index 330190e78..ed48de17f 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -164,6 +164,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ G.sed_cmd_tail = &G.sed_cmd_head; \ } while (0) diff --git a/findutils/find.c b/findutils/find.c index 32d830337..d71c69782 100644 --- a/findutils/find.c +++ b/findutils/find.c @@ -424,6 +424,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ /* we have to zero it out because of NOEXEC */ \ memset(&G, 0, sizeof(G)); \ diff --git a/findutils/grep.c b/findutils/grep.c index a669ac80b..b072cd441 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -204,6 +204,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) #define max_matches (G.max_matches ) diff --git a/findutils/xargs.c b/findutils/xargs.c index bfbd94960..ae01a49be 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -103,6 +103,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \ IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \ IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \ diff --git a/init/bootchartd.c b/init/bootchartd.c index 5101b28ae..7f511e650 100644 --- a/init/bootchartd.c +++ b/init/bootchartd.c @@ -117,7 +117,7 @@ struct globals { char jiffy_line[COMMON_BUFSIZE]; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void dump_file(FILE *fp, const char *filename) { diff --git a/loginutils/login.c b/loginutils/login.c index ea7c5a23d..94b6c45db 100644 --- a/loginutils/login.c +++ b/loginutils/login.c @@ -140,7 +140,7 @@ struct globals { struct termios tty_attrs; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #if ENABLE_FEATURE_NOLOGIN diff --git a/miscutils/crond.c b/miscutils/crond.c index 8536d43c5..f96c96ee7 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -143,6 +143,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.log_level = 8; \ G.crontab_dir_name = CRONTABS; \ } while (0) diff --git a/miscutils/dc.c b/miscutils/dc.c index 3fbb89f5b..4d92bc3d0 100644 --- a/miscutils/dc.c +++ b/miscutils/dc.c @@ -53,6 +53,7 @@ enum { STACK_SIZE = (COMMON_BUFSIZE - offsetof(struct globals, stack)) / sizeof( #define base (G.base ) #define stack (G.stack ) #define INIT_G() do { \ + setup_common_bufsiz(); \ base = 10; \ } while (0) diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c index 9e141de2f..b4c5876d4 100644 --- a/miscutils/hdparm.c +++ b/miscutils/hdparm.c @@ -432,6 +432,7 @@ struct globals { #define hwif_ctrl (G.hwif_ctrl ) #define hwif_irq (G.hwif_irq ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/arp.c b/networking/arp.c index 5f7818663..9381eb53a 100644 --- a/networking/arp.c +++ b/networking/arp.c @@ -76,6 +76,7 @@ struct globals { #define device (G.device ) #define hw_set (G.hw_set ) #define INIT_G() do { \ + setup_common_bufsiz(); \ device = ""; \ } while (0) diff --git a/networking/arping.c b/networking/arping.c index 52f5ba51f..6b0de4de2 100644 --- a/networking/arping.c +++ b/networking/arping.c @@ -77,6 +77,7 @@ struct globals { #define brd_recv (G.brd_recv ) #define req_recv (G.req_recv ) #define INIT_G() do { \ + setup_common_bufsiz(); \ count = -1; \ } while (0) diff --git a/networking/ftpd.c b/networking/ftpd.c index 8553a28f5..360d1e6be 100644 --- a/networking/ftpd.c +++ b/networking/ftpd.c @@ -126,6 +126,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ /* Moved to main */ \ /*strcpy(G.msg_ok + 4, MSG_OK );*/ \ /*strcpy(G.msg_err + 4, MSG_ERR);*/ \ diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index 61bc45c4e..91fb4569a 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c @@ -71,6 +71,7 @@ enum { BUFSZ = COMMON_BUFSIZE - offsetof(struct globals, buf) }; #define do_continue (G.do_continue ) #define buf (G.buf ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/ifupdown.c b/networking/ifupdown.c index 399ff6b5d..25b04c9d7 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c @@ -131,7 +131,7 @@ struct globals { char *shell; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static const char keywords_up_down[] ALIGN1 = diff --git a/networking/inetd.c b/networking/inetd.c index aa35ffa2b..8d44b5198 100644 --- a/networking/inetd.c +++ b/networking/inetd.c @@ -350,6 +350,7 @@ enum { LINE_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line) }; #define allsock (G.allsock ) #define line (G.line ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ rlim_ofile_cur = OPEN_MAX; \ global_queuelen = 128; \ diff --git a/networking/ping.c b/networking/ping.c index 761660979..cfe682646 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -188,7 +188,7 @@ struct globals { char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void noresp(int ign UNUSED_PARAM) { @@ -398,6 +398,7 @@ struct globals { #define pingaddr (G.pingaddr ) #define rcvd_tbl (G.rcvd_tbl ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ datalen = DEFDATALEN; \ timeout = MAXWAIT; \ diff --git a/networking/slattach.c b/networking/slattach.c index d9d8fe7b8..2d1305e32 100644 --- a/networking/slattach.c +++ b/networking/slattach.c @@ -39,7 +39,7 @@ struct globals { #define handle (G.handle ) #define saved_disc (G.saved_disc ) #define saved_state (G.saved_state ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) /* diff --git a/networking/tc.c b/networking/tc.c index 1372ca081..d0bcbdeaa 100644 --- a/networking/tc.c +++ b/networking/tc.c @@ -71,6 +71,7 @@ struct globals { #define filter_prio (G.filter_prio) #define filter_proto (G.filter_proto) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/tcpudp.c b/networking/tcpudp.c index 624973042..31bc70459 100644 --- a/networking/tcpudp.c +++ b/networking/tcpudp.c @@ -101,6 +101,7 @@ struct globals { #define env_cur (G.env_cur ) #define env_var (G.env_var ) #define INIT_G() do { \ + setup_common_bufsiz(); \ cmax = 30; \ env_cur = &env_var[0]; \ } while (0) diff --git a/networking/telnet.c b/networking/telnet.c index 2946bc831..d2daf5c8c 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -111,6 +111,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/telnetd.c b/networking/telnetd.c index 13d5a8f64..13c36aa46 100644 --- a/networking/telnetd.c +++ b/networking/telnetd.c @@ -85,6 +85,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.loginpath = "/bin/login"; \ G.issuefile = "/etc/issue.net"; \ } while (0) diff --git a/networking/tftp.c b/networking/tftp.c index 8aeb79aca..e879c4674 100644 --- a/networking/tftp.c +++ b/networking/tftp.c @@ -131,6 +131,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ } while (0) diff --git a/networking/udhcp/dhcprelay.c b/networking/udhcp/dhcprelay.c index 1722a85de..f52a0cf88 100644 --- a/networking/udhcp/dhcprelay.c +++ b/networking/udhcp/dhcprelay.c @@ -34,6 +34,7 @@ struct xid_item { } FIX_ALIASING; #define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client) { @@ -257,6 +258,8 @@ int dhcprelay_main(int argc, char **argv) int num_sockets, max_socket; uint32_t our_nip; + INIT_G(); + server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); server_addr.sin_port = htons(SERVER_PORT); diff --git a/networking/zcip.c b/networking/zcip.c index 79643458c..47f3216a0 100644 --- a/networking/zcip.c +++ b/networking/zcip.c @@ -92,7 +92,7 @@ struct globals { uint32_t localnet_ip; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) /** diff --git a/procps/free.c b/procps/free.c index 9fde64b64..fca9a2242 100644 --- a/procps/free.c +++ b/procps/free.c @@ -37,7 +37,7 @@ struct globals { #endif } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static unsigned long long scale(unsigned long d) diff --git a/procps/fuser.c b/procps/fuser.c index 2cda0f9d7..6dac852ed 100644 --- a/procps/fuser.c +++ b/procps/fuser.c @@ -46,6 +46,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ G.mypid = getpid(); \ G.killsig = SIGKILL; \ } while (0) diff --git a/procps/ps.c b/procps/ps.c index 65d62e256..08dfce12e 100644 --- a/procps/ps.c +++ b/procps/ps.c @@ -153,7 +153,7 @@ struct globals { #define buffer (G.buffer ) #define terminal_width (G.terminal_width ) #define kernel_HZ (G.kernel_HZ ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #if ENABLE_FEATURE_PS_TIME /* for ELF executables, notes are pushed before environment and args */ diff --git a/procps/top.c b/procps/top.c index 1c42b249c..640bcdc6d 100644 --- a/procps/top.c +++ b/procps/top.c @@ -202,6 +202,7 @@ enum { LINE_BUF_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line_buf) }; #define total_pcpu (G.total_pcpu ) #define line_buf (G.line_buf ) #define INIT_G() do { \ + setup_common_bufsiz(); \ BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ BUILD_BUG_ON(LINE_BUF_SIZE <= 80); \ } while (0) diff --git a/runit/runsv.c b/runit/runsv.c index 8833f4c96..e0e31508a 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -115,6 +115,7 @@ struct globals { #define dir (G.dir ) #define svd (G.svd ) #define INIT_G() do { \ + setup_common_bufsiz(); \ pidchanged = 1; \ } while (0) diff --git a/runit/runsvdir.c b/runit/runsvdir.c index 49c8f5b48..2b7927542 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c @@ -93,7 +93,7 @@ struct globals { #define logpipe (G.logpipe ) #define pfd (G.pfd ) #define stamplog (G.stamplog ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) static void fatal2_cannot(const char *m1, const char *m2) { diff --git a/runit/sv.c b/runit/sv.c index e83a29781..2a256a6b4 100644 --- a/runit/sv.c +++ b/runit/sv.c @@ -207,7 +207,7 @@ struct globals { #define tstart (G.tstart ) #define tnow (G.tnow ) #define svstatus (G.svstatus ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #define str_equal(s,t) (!strcmp((s), (t))) diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index afe9eee1e..bb0738641 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -111,4 +111,6 @@ fi if test $OLD != $REM; then echo "Space in _end[] is $REM bytes. Rerun make to use larger COMMON_BUFSIZE." +else + echo "COMMON_BUFSIZE = $REM bytes" fi diff --git a/selinux/setfiles.c b/selinux/setfiles.c index 441345ae9..51a7e63bd 100644 --- a/selinux/setfiles.c +++ b/selinux/setfiles.c @@ -80,6 +80,7 @@ struct globals { #define G (*(struct globals*)bb_common_bufsiz1) void BUG_setfiles_globals_too_big(void); #define INIT_G() do { \ + setup_common_bufsiz(); \ if (sizeof(G) > COMMON_BUFSIZE) \ BUG_setfiles_globals_too_big(); \ /* memset(&G, 0, sizeof(G)); - already is */ \ diff --git a/sysklogd/logread.c b/sysklogd/logread.c index ebd7f8b2c..5b999730a 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c @@ -73,6 +73,7 @@ struct globals { #define SMrdn (G.SMrdn) #define shbuf (G.shbuf) #define INIT_G() do { \ + setup_common_bufsiz(); \ memcpy(SMrup, init_sem, sizeof(init_sem)); \ } while (0) diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 7473b1855..37514eb54 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c @@ -288,6 +288,7 @@ struct globals { } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) #define INIT_G() do { \ + setup_common_bufsiz(); \ IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.maj = -1;) \ IF_NOT_FEATURE_MDEV_CONF(G.cur_rule.mode = 0660;) \ } while (0) diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c index f9451792b..dcb53f008 100644 --- a/util-linux/mkswap.c +++ b/util-linux/mkswap.c @@ -76,6 +76,7 @@ struct swap_header_v1 { #define NWORDS 129 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) struct BUG_sizes { char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1]; @@ -93,6 +94,8 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv) off_t len; const char *label = ""; + INIT_G(); + opt_complementary = "-1"; /* at least one param */ /* TODO: -p PAGESZ, -U UUID */ getopt32(argv, "L:", &label); diff --git a/util-linux/more.c b/util-linux/more.c index 58be3ac3b..95cbdd994 100644 --- a/util-linux/more.c +++ b/util-linux/more.c @@ -33,10 +33,10 @@ struct globals { struct termios new_settings; } FIX_ALIASING; #define G (*(struct globals*)bb_common_bufsiz1) -#define INIT_G() ((void)0) #define initial_settings (G.initial_settings) #define new_settings (G.new_settings ) #define cin_fileno (G.cin_fileno ) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #define setTermSettings(fd, argp) \ do { \ diff --git a/util-linux/mount.c b/util-linux/mount.c index e5c85feff..244f4fa27 100644 --- a/util-linux/mount.c +++ b/util-linux/mount.c @@ -457,7 +457,7 @@ enum { GETMNTENT_BUFSIZE = COMMON_BUFSIZE - offsetof(struct globals, getmntent_b #endif #define fslist (G.fslist ) #define getmntent_buf (G.getmntent_buf ) -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #if ENABLE_FEATURE_MTAB_SUPPORT /* diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c index 43228a6ba..6713852e5 100644 --- a/util-linux/swaponoff.c +++ b/util-linux/swaponoff.c @@ -72,7 +72,7 @@ struct globals { #define save_g_flags() ((void)0) #define restore_g_flags() ((void)0) #endif -#define INIT_G() do { } while (0) +#define INIT_G() do { setup_common_bufsiz(); } while (0) #define do_swapoff (applet_name[5] == 'f') diff --git a/util-linux/uevent.c b/util-linux/uevent.c index 58668fa5d..b98fe6160 100644 --- a/util-linux/uevent.c +++ b/util-linux/uevent.c @@ -31,6 +31,7 @@ #define BUFFER_SIZE 16*1024 #define env ((char **)bb_common_bufsiz1) +#define INIT_G() do { setup_common_bufsiz(); } while (0) enum { MAX_ENV = COMMON_BUFSIZE / sizeof(env[0]) - 1, }; @@ -46,6 +47,8 @@ int uevent_main(int argc UNUSED_PARAM, char **argv) struct sockaddr_nl sa; int fd; + INIT_G(); + argv++; // Subscribe for UEVENT kernel messages -- cgit v1.2.3-55-g6feb From f56fb5eb1120a92bdfb6d0ce64b3430b42a2efa0 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 21:03:51 +0200 Subject: libbb: make "COMMON_BUFSIZE = 1024 bytes, the buffer will be malloced" work Signed-off-by: Denys Vlasenko --- libbb/common_bufsiz.c | 6 +++--- scripts/generate_BUFSIZ.sh | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'scripts') diff --git a/libbb/common_bufsiz.c b/libbb/common_bufsiz.c index c16c361c9..26faafcbb 100644 --- a/libbb/common_bufsiz.c +++ b/libbb/common_bufsiz.c @@ -55,14 +55,14 @@ char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long)); # ifndef setup_common_bufsiz /* - * It is not a "((void)0)" macro. It means we have to provide this function. + * It is not defined as a dummy macro. + * It means we have to provide this function. */ char* bb_common_bufsiz1; -char* setup_common_bufsiz(void) +void setup_common_bufsiz(void) { if (!bb_common_bufsiz1) bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE); - return bb_common_bufsiz1; } # else # ifndef bb_common_bufsiz1 diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index bb0738641..d54142597 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -87,7 +87,8 @@ if test $REM -lt 1024; then echo "Rerun make to build a binary which doesn't use it!" exit 1 fi - exit 0 + echo "COMMON_BUFSIZE = 1024 bytes, the buffer will be malloced" + exit 0 fi # _end[] has REM bytes for bb_common_bufsiz1[] @@ -102,7 +103,7 @@ echo "#define setup_common_bufsiz() ((void)0)" } | regenerate "$common_bufsiz_h" echo $REM >"$common_bufsiz_h.BUFSIZE" -# Check that code did not grow too much and thus _end[] did not shink: +# Check that code did not grow too much and thus _end[] did not shrink: if test $OLD -gt $REM; then echo "Warning! Space in _end[] has decreased from $OLD to $REM bytes!" echo "Rerun make!" -- cgit v1.2.3-55-g6feb From 93e1aaa1c7e5ed6d2704262700ec28837bdfc9b7 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 21:47:45 +0200 Subject: libbb: constify *bb_common_bufsiz1 (if it is compiled to be a pointer) This lets gcc optimize much better: text data bss dec hex filename 922846 910 13056 936812 e4b6c busybox_unstripped.nonconst 920255 910 13056 934221 e414d busybox_unstripped Signed-off-by: Denys Vlasenko --- libbb/common_bufsiz.c | 4 ++-- scripts/generate_BUFSIZ.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'scripts') diff --git a/libbb/common_bufsiz.c b/libbb/common_bufsiz.c index 26faafcbb..1a3585169 100644 --- a/libbb/common_bufsiz.c +++ b/libbb/common_bufsiz.c @@ -58,11 +58,11 @@ char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long)); * It is not defined as a dummy macro. * It means we have to provide this function. */ -char* bb_common_bufsiz1; +char *const bb_common_bufsiz1 __attribute__ ((section (".data"))); void setup_common_bufsiz(void) { if (!bb_common_bufsiz1) - bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE); + *(char**)&bb_common_bufsiz1 = xzalloc(COMMON_BUFSIZE); } # else # ifndef bb_common_bufsiz1 diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index d54142597..1914fa0f5 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -77,7 +77,7 @@ if test $REM -lt 1024; then # users will need to malloc it. { echo "enum { COMMON_BUFSIZE = 1024 };" - echo "extern char *bb_common_bufsiz1;" + echo "extern char *const bb_common_bufsiz1;" echo "void setup_common_bufsiz(void);" } | regenerate "$common_bufsiz_h" # Check that we aren't left with a buggy binary: -- cgit v1.2.3-55-g6feb From d7b502c05911e1cf1d4fc31be71f3abccd0927a5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 21 Apr 2016 23:52:35 +0200 Subject: build system: fix generate_BUFSIZ.sh to not alternate 1k and malloc builds Signed-off-by: Denys Vlasenko --- Makefile | 2 +- scripts/generate_BUFSIZ.sh | 180 +++++++++++++++++++++++++++------------------ 2 files changed, 109 insertions(+), 73 deletions(-) (limited to 'scripts') diff --git a/Makefile b/Makefile index 75a33c1fb..d5950230d 100644 --- a/Makefile +++ b/Makefile @@ -611,7 +611,7 @@ quiet_cmd_busybox__ ?= LINK $@ "$(core-y)" \ "$(libs-y)" \ "$(LDLIBS)" \ - && $(srctree)/scripts/generate_BUFSIZ.sh include/common_bufsiz.h + && $(srctree)/scripts/generate_BUFSIZ.sh --post include/common_bufsiz.h # Generate System.map quiet_cmd_sysmap = SYSMAP diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index 1914fa0f5..8aa0174a6 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -7,11 +7,16 @@ debug=false +postcompile=false +test x"$1" = x"--post" && { postcompile=true; shift; } + common_bufsiz_h=$1 test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm" test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc" +exitcmd="exit 0" + regenerate() { cat >"$1.$$" test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return @@ -19,99 +24,130 @@ regenerate() { } generate_std_and_exit() { - $debug && echo "Default: bb_common_bufsiz1[] in bss" + $debug && echo "Configuring: bb_common_bufsiz1[] in bss" { echo "enum { COMMON_BUFSIZE = 1024 };" echo "extern char bb_common_bufsiz1[];" echo "#define setup_common_bufsiz() ((void)0)" } | regenerate "$common_bufsiz_h" - exit 0 + echo "std" >"$common_bufsiz_h.method" + $exitcmd } -# User does not want any funky stuff? -test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit - -test -f busybox_unstripped || { - # We did not try anything yet - $debug && echo "Will try to fit bb_common_bufsiz1[] into _end[]" +generate_big_and_exit() { + $debug && echo "Configuring: bb_common_bufsiz1[] in _end[], COMMON_BUFSIZE = $1" { - echo "enum { COMMON_BUFSIZE = 1024 };" + echo "enum { COMMON_BUFSIZE = $1 };" echo "extern char _end[]; /* linker-provided label */" echo "#define bb_common_bufsiz1 _end" echo "#define setup_common_bufsiz() ((void)0)" } | regenerate "$common_bufsiz_h" - echo 1024 >"$common_bufsiz_h.BUFSIZE" - exit 0 + echo "$2" >"$common_bufsiz_h.method" + $exitcmd } -# Get _end address -END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1` -test x"$END" = x"" && generate_std_and_exit -$debug && echo "END:0x$END $((0x$END))" -END=$((0x$END)) - -# Get PAGE_SIZE -echo "\ -#include -#if defined(PAGE_SIZE) && PAGE_SIZE > 0 -char page_size[PAGE_SIZE]; -#else -char page_size[1]; -#endif -" >page_size_$$.c -$CC -c "page_size_$$.c" || generate_std_and_exit -PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1` -rm "page_size_$$.c" "page_size_$$.o" -test x"$PAGE_SIZE" = x"" && generate_std_and_exit -$debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))" -PAGE_SIZE=$((0x$PAGE_SIZE)) -test $PAGE_SIZE -lt 1024 && generate_std_and_exit - -# How much space between _end[] and next page? -PAGE_MASK=$((PAGE_SIZE-1)) -REM=$(( (-END) & PAGE_MASK )) -$debug && echo "REM:$REM" - -if test $REM -lt 1024; then - # _end[] has no enough space for bb_common_bufsiz1[], - # users will need to malloc it. +generate_1k_and_exit() { + generate_big_and_exit 1024 "1k" +} + + +generate_malloc_and_exit() { + $debug && echo "Configuring: bb_common_bufsiz1[] is malloced" { echo "enum { COMMON_BUFSIZE = 1024 };" echo "extern char *const bb_common_bufsiz1;" echo "void setup_common_bufsiz(void);" } | regenerate "$common_bufsiz_h" - # Check that we aren't left with a buggy binary: - if test -f "$common_bufsiz_h.BUFSIZE"; then - rm "$common_bufsiz_h.BUFSIZE" - echo "Warning! Space in _end[] is too small ($REM bytes)!" - echo "Rerun make to build a binary which doesn't use it!" - exit 1 + echo "malloc" >"$common_bufsiz_h.method" + $exitcmd +} + +# User does not want any funky stuff? +test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit + +# The script is run two times: before compilation, when it needs to +# (re)generate $common_bufsiz_h, and directly after successful build, +# when it needs to assess whether the build is ok to use at all (not buggy), +# and (re)generate $common_bufsiz_h for a future build. + +if $postcompile; then + # Postcompile needs to create/delete OK/FAIL files + + test -f busybox_unstripped || exit 1 + test -f "$common_bufsiz_h.method" || exit 1 + + # How the build was done? + method=`cat -- "$common_bufsiz_h.method"` + + # Get _end address + END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1` + test x"$END" = x"" && generate_std_and_exit + $debug && echo "END:0x$END $((0x$END))" + END=$((0x$END)) + + # Get PAGE_SIZE + { + echo "#include " + echo "#if defined(PAGE_SIZE) && PAGE_SIZE > 0" + echo "char page_size[PAGE_SIZE];" + echo "#endif" + } >page_size_$$.c + $CC -c "page_size_$$.c" || exit 1 + PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1` + rm "page_size_$$.c" "page_size_$$.o" + test x"$PAGE_SIZE" = x"" && exit 1 + $debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))" + PAGE_SIZE=$((0x$PAGE_SIZE)) + test $PAGE_SIZE -lt 512 && exit 1 + + # How much space between _end[] and next page? + PAGE_MASK=$((PAGE_SIZE-1)) + COMMON_BUFSIZE=$(( (-END) & PAGE_MASK )) + echo "COMMON_BUFSIZE = $COMMON_BUFSIZE bytes" + + if test x"$method" = x"1k"; then + if test $COMMON_BUFSIZE -lt 1024; then + # _end[] has no enough space for bb_common_bufsiz1[] + rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null + { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" + echo "Warning! Space in _end[] is too small ($COMMON_BUFSIZE bytes)!" + echo "Rerun make to build a binary which doesn't use it!" + rm busybox_unstripped + exitcmd="exit 1" + else + rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null + echo $COMMON_BUFSIZE >"$common_bufsiz_h.1k.OK" + test $COMMON_BUFSIZE -gt $((1024+32)) && echo "Rerun make to use larger COMMON_BUFSIZE" + fi fi - echo "COMMON_BUFSIZE = 1024 bytes, the buffer will be malloced" - exit 0 fi -# _end[] has REM bytes for bb_common_bufsiz1[] -OLD=1024 -test -f "$common_bufsiz_h.BUFSIZE" && OLD=`cat "$common_bufsiz_h.BUFSIZE"` -$debug && echo "OLD:$OLD" -{ -echo "enum { COMMON_BUFSIZE = $REM };" -echo "extern char _end[]; /* linker-provided label */" -echo "#define bb_common_bufsiz1 _end" -echo "#define setup_common_bufsiz() ((void)0)" -} | regenerate "$common_bufsiz_h" -echo $REM >"$common_bufsiz_h.BUFSIZE" - -# Check that code did not grow too much and thus _end[] did not shrink: -if test $OLD -gt $REM; then - echo "Warning! Space in _end[] has decreased from $OLD to $REM bytes!" - echo "Rerun make!" - exit 1 -fi +# Based on past success/fail of 1k build, decide next build type -if test $OLD != $REM; then - echo "Space in _end[] is $REM bytes. Rerun make to use larger COMMON_BUFSIZE." -else - echo "COMMON_BUFSIZE = $REM bytes" +if test -f "$common_bufsiz_h.1k.OK"; then + # Previous build succeeded fitting 1k into _end[]. + # Try bigger COMMON_BUFSIZE if possible. + COMMON_BUFSIZE=`cat -- "$common_bufsiz_h.1k.OK"` + # Round down a bit + COMMON_BUFSIZE=$(( (COMMON_BUFSIZE-32) & 0xfffffe0 )) + COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) + test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit + generate_big_and_exit $COMMON_BUFSIZE "big" +fi +if test -f "$common_bufsiz_h.1k.FAIL"; then + # Previous build FAILED to fit 1k into _end[]. + # Was it with same .config? + oldcfg=`cat -- "$common_bufsiz_h.1k.FAIL"` + curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config` + # If yes, then build a "malloced" version + if test x"$oldcfg" = x"$curcfg"; then + echo "Will not try 1k build, it failed before. Touch .config to override" + generate_malloc_and_exit + fi + # else: try 1k version + echo "New .config, will try 1k build" + rm -- "$common_bufsiz_h.1k.FAIL" + generate_1k_and_exit fi +# There was no 1k build yet. Try it. +generate_1k_and_exit -- cgit v1.2.3-55-g6feb From 7ff24bd5fb37c58d9e41743a910df147357dda61 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 22 Apr 2016 00:24:53 +0200 Subject: generate_BUFSIZ.sh: catch BUFSIZE < 1024 also on "big" builds Signed-off-by: Denys Vlasenko --- scripts/generate_BUFSIZ.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'scripts') diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index 8aa0174a6..750fedbef 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -105,14 +105,14 @@ if $postcompile; then COMMON_BUFSIZE=$(( (-END) & PAGE_MASK )) echo "COMMON_BUFSIZE = $COMMON_BUFSIZE bytes" - if test x"$method" = x"1k"; then + if test x"$method" != x"malloc"; then if test $COMMON_BUFSIZE -lt 1024; then # _end[] has no enough space for bb_common_bufsiz1[] rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" echo "Warning! Space in _end[] is too small ($COMMON_BUFSIZE bytes)!" echo "Rerun make to build a binary which doesn't use it!" - rm busybox_unstripped + rm busybox_unstripped busybox exitcmd="exit 1" else rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null -- cgit v1.2.3-55-g6feb From 663d1da1e68b15397c00d6a094f78c2cf08358ea Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 22 Apr 2016 02:00:04 +0200 Subject: scripts/trylink: document DATA_SEGMENT_ALIGN() hack Signed-off-by: Denys Vlasenko --- scripts/trylink | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'scripts') diff --git a/scripts/trylink b/scripts/trylink index 15435f009..129570a60 100755 --- a/scripts/trylink +++ b/scripts/trylink @@ -209,6 +209,16 @@ else # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) # This will eliminate most of the padding (~3kb). # Hmm, "ld --sort-section alignment" should do it too. + # + # There is a ld hack which is meant to decrease disk usage + # at the cost of more RAM usage (??!!) in standard ld script: + # /* Adjust the address for the data segment. We want to adjust up to + # the same address within the page on the next page up. */ + # . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000); + # Replace it with: + # . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000); + # to unconditionally align .data to the next page boundary, + # instead of "next page, plus current offset in this page" try $CC $CFLAGS $LDFLAGS \ -o $EXE \ $SORT_COMMON \ -- cgit v1.2.3-55-g6feb From 4c8fa34417fd2ccdda6a8ea508a3f1e7fb1d4ceb Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 24 Apr 2016 14:13:35 +0200 Subject: generate_BUFSIZ.sh: yet another tweak Signed-off-by: Denys Vlasenko --- include/.gitignore | 1 + scripts/generate_BUFSIZ.sh | 54 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 13 deletions(-) (limited to 'scripts') diff --git a/include/.gitignore b/include/.gitignore index 9d9b6c499..75afff9ca 100644 --- a/include/.gitignore +++ b/include/.gitignore @@ -8,3 +8,4 @@ /NUM_APPLETS.h /usage_compressed.h /usage.h +/common_bufsiz.h* diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh index 750fedbef..844261906 100755 --- a/scripts/generate_BUFSIZ.sh +++ b/scripts/generate_BUFSIZ.sh @@ -6,6 +6,7 @@ . ./.config || exit 1 debug=false +#debug=true postcompile=false test x"$1" = x"--post" && { postcompile=true; shift; } @@ -62,6 +63,11 @@ generate_malloc_and_exit() { $exitcmd } +round_down_COMMON_BUFSIZE() { + COMMON_BUFSIZE=$(( ($1-32) & 0xfffffe0 )) + COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) +} + # User does not want any funky stuff? test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit @@ -102,22 +108,43 @@ if $postcompile; then # How much space between _end[] and next page? PAGE_MASK=$((PAGE_SIZE-1)) - COMMON_BUFSIZE=$(( (-END) & PAGE_MASK )) - echo "COMMON_BUFSIZE = $COMMON_BUFSIZE bytes" + TAIL_SIZE=$(( (-END) & PAGE_MASK )) + $debug && echo "TAIL_SIZE:$TAIL_SIZE bytes" - if test x"$method" != x"malloc"; then - if test $COMMON_BUFSIZE -lt 1024; then + if test x"$method" = x"1k" || test x"$method" = x"big"; then + if test $TAIL_SIZE -lt 1024; then # _end[] has no enough space for bb_common_bufsiz1[] + echo "Warning! Space in _end[] is too small ($TAIL_SIZE bytes)!" + echo "Rerun make to build a binary which doesn't use it!" rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" - echo "Warning! Space in _end[] is too small ($COMMON_BUFSIZE bytes)!" - echo "Rerun make to build a binary which doesn't use it!" - rm busybox_unstripped busybox + rm busybox_unstripped busybox 2>/dev/null +# Note: here we can do either a "malloc" or "std" build. +# "malloc" gives a bit bigger code: +# text bss filename +# 804355 5385 busybox.std +# 804618 4361 busybox.malloc +# but may have a smaller .bss (not guaranteed!). Use "pmap -x" to verify. exitcmd="exit 1" + generate_malloc_and_exit else + PREV_SIZE=1024 + test x"$method" = x"big" && PREV_SIZE=`cat -- "$common_bufsiz_h.1k.OK"` + round_down_COMMON_BUFSIZE $PREV_SIZE + PREV_BUFSIZE=$COMMON_BUFSIZE + rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null - echo $COMMON_BUFSIZE >"$common_bufsiz_h.1k.OK" - test $COMMON_BUFSIZE -gt $((1024+32)) && echo "Rerun make to use larger COMMON_BUFSIZE" + echo $TAIL_SIZE >"$common_bufsiz_h.1k.OK" + round_down_COMMON_BUFSIZE $TAIL_SIZE + # emit message only if COMMON_BUFSIZE is indeed larger + test $COMMON_BUFSIZE -gt $PREV_BUFSIZE \ + && echo "Rerun make to use larger COMMON_BUFSIZE ($COMMON_BUFSIZE)" +#TODO: test $PREV_BUFSIZE -lt $TAIL_SIZE && PANIC!!! +#Code size with COMMON_BUFSIZE > 1024 may be bigger than code with COMMON_BUFSIZE = 1024! +#(currently we just hope "-32 and round down to 32" saves us) + + test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit + generate_big_and_exit $COMMON_BUFSIZE "big" fi fi fi @@ -127,13 +154,12 @@ fi if test -f "$common_bufsiz_h.1k.OK"; then # Previous build succeeded fitting 1k into _end[]. # Try bigger COMMON_BUFSIZE if possible. - COMMON_BUFSIZE=`cat -- "$common_bufsiz_h.1k.OK"` - # Round down a bit - COMMON_BUFSIZE=$(( (COMMON_BUFSIZE-32) & 0xfffffe0 )) - COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) + TAIL_SIZE=`cat -- "$common_bufsiz_h.1k.OK"` + round_down_COMMON_BUFSIZE $TAIL_SIZE test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit generate_big_and_exit $COMMON_BUFSIZE "big" fi + if test -f "$common_bufsiz_h.1k.FAIL"; then # Previous build FAILED to fit 1k into _end[]. # Was it with same .config? @@ -142,6 +168,7 @@ if test -f "$common_bufsiz_h.1k.FAIL"; then # If yes, then build a "malloced" version if test x"$oldcfg" = x"$curcfg"; then echo "Will not try 1k build, it failed before. Touch .config to override" +# Note: here we can do either a "malloc" or "std" build. generate_malloc_and_exit fi # else: try 1k version @@ -149,5 +176,6 @@ if test -f "$common_bufsiz_h.1k.FAIL"; then rm -- "$common_bufsiz_h.1k.FAIL" generate_1k_and_exit fi + # There was no 1k build yet. Try it. generate_1k_and_exit -- cgit v1.2.3-55-g6feb