Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Called from top-level directory a-la |
| 3 | # |
| 4 | # scripts/generate_BUFSIZ.sh include/common_bufsiz.h |
| 5 | |
| 6 | . ./.config || exit 1 |
| 7 | |
| 8 | debug=false |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 9 | #debug=true |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 10 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 11 | postcompile=false |
| 12 | test x"$1" = x"--post" && { postcompile=true; shift; } |
| 13 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 14 | common_bufsiz_h=$1 |
| 15 | |
| 16 | test x"$NM" = x"" && NM="${CONFIG_CROSS_COMPILER_PREFIX}nm" |
| 17 | test x"$CC" = x"" && CC="${CONFIG_CROSS_COMPILER_PREFIX}gcc" |
| 18 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 19 | exitcmd="exit 0" |
| 20 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 21 | regenerate() { |
| 22 | cat >"$1.$$" |
| 23 | test -f "$1" && diff "$1.$$" "$1" >/dev/null && rm "$1.$$" && return |
| 24 | mv "$1.$$" "$1" |
| 25 | } |
| 26 | |
| 27 | generate_std_and_exit() { |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 28 | $debug && echo "Configuring: bb_common_bufsiz1[] in bss" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 29 | { |
| 30 | echo "enum { COMMON_BUFSIZE = 1024 };" |
| 31 | echo "extern char bb_common_bufsiz1[];" |
| 32 | echo "#define setup_common_bufsiz() ((void)0)" |
| 33 | } | regenerate "$common_bufsiz_h" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 34 | echo "std" >"$common_bufsiz_h.method" |
| 35 | $exitcmd |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 38 | generate_big_and_exit() { |
| 39 | $debug && echo "Configuring: bb_common_bufsiz1[] in _end[], COMMON_BUFSIZE = $1" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 40 | { |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 41 | echo "enum { COMMON_BUFSIZE = $1 };" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 42 | echo "extern char _end[]; /* linker-provided label */" |
| 43 | echo "#define bb_common_bufsiz1 _end" |
| 44 | echo "#define setup_common_bufsiz() ((void)0)" |
| 45 | } | regenerate "$common_bufsiz_h" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 46 | echo "$2" >"$common_bufsiz_h.method" |
| 47 | $exitcmd |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 50 | generate_1k_and_exit() { |
| 51 | generate_big_and_exit 1024 "1k" |
| 52 | } |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 53 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 54 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 55 | generate_malloc_and_exit() { |
| 56 | $debug && echo "Configuring: bb_common_bufsiz1[] is malloced" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 57 | { |
| 58 | echo "enum { COMMON_BUFSIZE = 1024 };" |
Denys Vlasenko | 93e1aaa | 2016-04-21 21:47:45 +0200 | [diff] [blame] | 59 | echo "extern char *const bb_common_bufsiz1;" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 60 | echo "void setup_common_bufsiz(void);" |
| 61 | } | regenerate "$common_bufsiz_h" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 62 | echo "malloc" >"$common_bufsiz_h.method" |
| 63 | $exitcmd |
| 64 | } |
| 65 | |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 66 | round_down_COMMON_BUFSIZE() { |
| 67 | COMMON_BUFSIZE=$(( ($1-32) & 0xfffffe0 )) |
| 68 | COMMON_BUFSIZE=$(( COMMON_BUFSIZE < 1024 ? 1024 : COMMON_BUFSIZE )) |
| 69 | } |
| 70 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 71 | # User does not want any funky stuff? |
| 72 | test x"$CONFIG_FEATURE_USE_BSS_TAIL" = x"y" || generate_std_and_exit |
| 73 | |
| 74 | # The script is run two times: before compilation, when it needs to |
| 75 | # (re)generate $common_bufsiz_h, and directly after successful build, |
| 76 | # when it needs to assess whether the build is ok to use at all (not buggy), |
| 77 | # and (re)generate $common_bufsiz_h for a future build. |
| 78 | |
| 79 | if $postcompile; then |
| 80 | # Postcompile needs to create/delete OK/FAIL files |
| 81 | |
| 82 | test -f busybox_unstripped || exit 1 |
| 83 | test -f "$common_bufsiz_h.method" || exit 1 |
| 84 | |
| 85 | # How the build was done? |
| 86 | method=`cat -- "$common_bufsiz_h.method"` |
| 87 | |
| 88 | # Get _end address |
| 89 | END=`$NM busybox_unstripped | grep ' . _end$'| cut -d' ' -f1` |
| 90 | test x"$END" = x"" && generate_std_and_exit |
| 91 | $debug && echo "END:0x$END $((0x$END))" |
| 92 | END=$((0x$END)) |
| 93 | |
| 94 | # Get PAGE_SIZE |
| 95 | { |
| 96 | echo "#include <sys/user.h>" |
| 97 | echo "#if defined(PAGE_SIZE) && PAGE_SIZE > 0" |
| 98 | echo "char page_size[PAGE_SIZE];" |
| 99 | echo "#endif" |
| 100 | } >page_size_$$.c |
| 101 | $CC -c "page_size_$$.c" || exit 1 |
| 102 | PAGE_SIZE=`$NM --size-sort "page_size_$$.o" | cut -d' ' -f1` |
| 103 | rm "page_size_$$.c" "page_size_$$.o" |
| 104 | test x"$PAGE_SIZE" = x"" && exit 1 |
| 105 | $debug && echo "PAGE_SIZE:0x$PAGE_SIZE $((0x$PAGE_SIZE))" |
| 106 | PAGE_SIZE=$((0x$PAGE_SIZE)) |
| 107 | test $PAGE_SIZE -lt 512 && exit 1 |
| 108 | |
| 109 | # How much space between _end[] and next page? |
| 110 | PAGE_MASK=$((PAGE_SIZE-1)) |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 111 | TAIL_SIZE=$(( (-END) & PAGE_MASK )) |
| 112 | $debug && echo "TAIL_SIZE:$TAIL_SIZE bytes" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 113 | |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 114 | if test x"$method" = x"1k" || test x"$method" = x"big"; then |
| 115 | if test $TAIL_SIZE -lt 1024; then |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 116 | # _end[] has no enough space for bb_common_bufsiz1[] |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 117 | echo "Warning! Space in _end[] is too small ($TAIL_SIZE bytes)!" |
| 118 | echo "Rerun make to build a binary which doesn't use it!" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 119 | rm -- "$common_bufsiz_h.1k.OK" 2>/dev/null |
| 120 | { md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config; } >"$common_bufsiz_h.1k.FAIL" |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 121 | rm busybox_unstripped busybox 2>/dev/null |
| 122 | # Note: here we can do either a "malloc" or "std" build. |
| 123 | # "malloc" gives a bit bigger code: |
| 124 | # text bss filename |
| 125 | # 804355 5385 busybox.std |
| 126 | # 804618 4361 busybox.malloc |
| 127 | # but may have a smaller .bss (not guaranteed!). Use "pmap -x" to verify. |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 128 | exitcmd="exit 1" |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 129 | generate_malloc_and_exit |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 130 | else |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 131 | PREV_SIZE=1024 |
| 132 | test x"$method" = x"big" && PREV_SIZE=`cat -- "$common_bufsiz_h.1k.OK"` |
| 133 | round_down_COMMON_BUFSIZE $PREV_SIZE |
| 134 | PREV_BUFSIZE=$COMMON_BUFSIZE |
| 135 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 136 | rm -- "$common_bufsiz_h.1k.FAIL" 2>/dev/null |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 137 | echo $TAIL_SIZE >"$common_bufsiz_h.1k.OK" |
| 138 | round_down_COMMON_BUFSIZE $TAIL_SIZE |
| 139 | # emit message only if COMMON_BUFSIZE is indeed larger |
| 140 | test $COMMON_BUFSIZE -gt $PREV_BUFSIZE \ |
| 141 | && echo "Rerun make to use larger COMMON_BUFSIZE ($COMMON_BUFSIZE)" |
| 142 | #TODO: test $PREV_BUFSIZE -lt $TAIL_SIZE && PANIC!!! |
| 143 | #Code size with COMMON_BUFSIZE > 1024 may be bigger than code with COMMON_BUFSIZE = 1024! |
| 144 | #(currently we just hope "-32 and round down to 32" saves us) |
| 145 | |
| 146 | test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit |
| 147 | generate_big_and_exit $COMMON_BUFSIZE "big" |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 148 | fi |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 149 | fi |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 150 | fi |
| 151 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 152 | # Based on past success/fail of 1k build, decide next build type |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 153 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 154 | if test -f "$common_bufsiz_h.1k.OK"; then |
| 155 | # Previous build succeeded fitting 1k into _end[]. |
| 156 | # Try bigger COMMON_BUFSIZE if possible. |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 157 | TAIL_SIZE=`cat -- "$common_bufsiz_h.1k.OK"` |
| 158 | round_down_COMMON_BUFSIZE $TAIL_SIZE |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 159 | test $COMMON_BUFSIZE = 1024 && generate_1k_and_exit |
| 160 | generate_big_and_exit $COMMON_BUFSIZE "big" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 161 | fi |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 162 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 163 | if test -f "$common_bufsiz_h.1k.FAIL"; then |
| 164 | # Previous build FAILED to fit 1k into _end[]. |
| 165 | # Was it with same .config? |
| 166 | oldcfg=`cat -- "$common_bufsiz_h.1k.FAIL"` |
| 167 | curcfg=`md5sum <.config | cut -d' ' -f1; stat -c "%Y" .config` |
| 168 | # If yes, then build a "malloced" version |
| 169 | if test x"$oldcfg" = x"$curcfg"; then |
| 170 | echo "Will not try 1k build, it failed before. Touch .config to override" |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 171 | # Note: here we can do either a "malloc" or "std" build. |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 172 | generate_malloc_and_exit |
| 173 | fi |
| 174 | # else: try 1k version |
| 175 | echo "New .config, will try 1k build" |
| 176 | rm -- "$common_bufsiz_h.1k.FAIL" |
| 177 | generate_1k_and_exit |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 178 | fi |
Denys Vlasenko | 4c8fa34 | 2016-04-24 14:13:35 +0200 | [diff] [blame^] | 179 | |
Denys Vlasenko | d7b502c | 2016-04-21 23:52:35 +0200 | [diff] [blame] | 180 | # There was no 1k build yet. Try it. |
| 181 | generate_1k_and_exit |