| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | |
| 3 | #debug=true |
| 4 | debug=false |
| 5 | |
| 6 | # Linker flags used: |
| 7 | # |
| 8 | # Informational: |
| 9 | # --warn-common |
| 10 | # -Map $EXE.map |
| 11 | # --verbose |
| 12 | # |
| 13 | # Optimizations: |
| 14 | # --sort-common reduces padding |
| 15 | # --sort-section alignment reduces padding |
| 16 | # --gc-sections throws out unused sections, |
| 17 | # does not work for shared libs |
| 18 | # -On Not used, maybe useful? |
| 19 | # |
| 20 | # List of files to link: |
| 21 | # $l_list == --start-group -llib1 -llib2 --end-group |
| 22 | # --start-group $O_FILES $A_FILES --end-group |
| 23 | # |
| 24 | # Shared library link: |
| 25 | # -shared self-explanatory |
| 26 | # -fPIC position-independent code |
| 27 | # --enable-new-dtags ? |
| 28 | # -z,combreloc ? |
| 29 | # -soname="libbusybox.so.$BB_VER" |
| 30 | # --undefined=lbb_main Seed name to start pulling from |
| 31 | # (otherwise we'll need --whole-archive) |
| 32 | # -static Not used, but may be useful! manpage: |
| 33 | # "... This option can be used with -shared. |
| 34 | # Doing so means that a shared library |
| 35 | # is being created but that all of the library's |
| 36 | # external references must be resolved by pulling |
| 37 | # in entries from static libraries." |
| 38 | |
| 39 | |
| 40 | tryscip-shell shell shell-project 0.0.0 try().() { |
| 41 | printfscip-shell system . . printf. "%s\n" "Output of:" >$EXE.out |
| 42 | printfscip-shell system . . printf. "%s\n" "$*" >>$EXE.out |
| 43 | printfscip-shell system . . printf. "%s\n" "==========" >>$EXE.out |
| 44 | $debug && echoscip-shell system . . echo. "Trying: $*" |
| 45 | $@ >>$EXE.out 2>&1 |
| 46 | return $? |
| 47 | } |
| 48 | |
| 49 | check_ccscip-shell shell shell-project 0.0.0 check_cc().() { |
| 50 | tempname="$(mktempscip-shell system . . mktemp. tmp.XXXXXXXXXX)" |
| 51 | echoscip-shell system . . echo. "int main(int argc,char**argv){return argv?argc:0;}" >"$tempname".c |
| 52 | # Can use "-o /dev/null", but older gcc tend to *unlink it* on failure! :( |
| 53 | # Was using "-xc /dev/null", but we need a valid C program. |
| 54 | $CC $CFLAGS $LDFLAGS $1 "$tempname".c -o "$tempname" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 55 | exitcode=$? |
| 56 | rmscip-shell system . . rm. -f "$tempname" "$tempname".c "$tempname".o |
| 57 | return $exitcode |
| 58 | } |
| 59 | |
| 60 | check_libc_is_glibcscip-shell shell shell-project 0.0.0 check_libc_is_glibc().() { |
| 61 | tempname="$(mktempscip-shell system . . mktemp. tmp.XXXXXXXXXX)" |
| 62 | echoscip-shell system . . echo. "\ |
| 63 | #include <stdlib.h> |
| 64 | /* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */ |
| 65 | #if defined(__GLIBC__) && !defined(__UCLIBC__) |
| 66 | syntax error here |
| 67 | #endif |
| 68 | " >"$tempname".c |
| 69 | ! $CC $CFLAGS "$tempname".c -c -o "$tempname".o >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 70 | exitcode=$? |
| 71 | rmscip-shell system . . rm. -f "$tempname" "$tempname".c "$tempname".o |
| 72 | return $exitcode |
| 73 | } |
| 74 | |
| 75 | EXE="$1" |
| 76 | CC="$2" |
| 77 | CFLAGS="$3" |
| 78 | LDFLAGS="$4" |
| 79 | O_FILES="$5" |
| 80 | A_FILES="$6" |
| 81 | # We try to drop libraries from LDLIBS if build works without them, |
| 82 | # but ones from CONFIG_EXTRA_LDLIBS are always linked in. |
| 83 | # (For example, musl has stub utmp implementation, and if you link with |
| 84 | # a real utmp library in LDLIBS, dropping it "works" but resulting binary |
| 85 | # does not work properly). |
| 86 | LDLIBS="$7" |
| 87 | CONFIG_EXTRA_LDLIBS="$8" |
| 88 | |
| 89 | # The --sort-section option is not supported by older versions of ld |
| 90 | SORT_SECTION="-Wl,--sort-section,alignment" |
| 91 | if ! check_ccscip-shell shell shell-project 0.0.0 check_cc(). "-Wl,--sort-section,alignment"; then |
| 92 | echoscip-shell system . . echo. "Your linker does not support --sort-section,alignment" |
| 93 | SORT_SECTION="" |
| 94 | fi |
| 95 | |
| 96 | START_GROUP="-Wl,--start-group" |
| 97 | END_GROUP="-Wl,--end-group" |
| 98 | if ! check_ccscip-shell shell shell-project 0.0.0 check_cc(). "-Wl,--start-group -Wl,--end-group"; then |
| 99 | echoscip-shell system . . echo. "Your linker does not support --start-group" |
| 100 | START_GROUP="" |
| 101 | END_GROUP="" |
| 102 | fi |
| 103 | |
| 104 | WARN_COMMON="-Wl,--warn-common" |
| 105 | if ! check_ccscip-shell shell shell-project 0.0.0 check_cc(). "-Wl,--warn-common"; then |
| 106 | echoscip-shell system . . echo. "Your linker does not support --warn-common" |
| 107 | WARN_COMMON="" |
| 108 | fi |
| 109 | MAP_OPT="-Wl,-Map,$EXE.map" |
| 110 | if ! check_ccscip-shell shell shell-project 0.0.0 check_cc(). "-Wl,-Map,/dev/null"; then |
| 111 | echoscip-shell system . . echo. "Your linker does not support -Map" |
| 112 | MAP_OPT="" |
| 113 | fi |
| 114 | VERBOSE_OPT="-Wl,--verbose" |
| 115 | if ! check_ccscip-shell shell shell-project 0.0.0 check_cc(). "-Wl,--verbose"; then |
| 116 | echoscip-shell system . . echo. "Your linker does not support --verbose" |
| 117 | VERBOSE_OPT="" |
| 118 | fi |
| 119 | INFO_OPTSscip-shell shell shell-project 0.0.0 INFO_OPTS().() { |
| 120 | echoscip-shell system . . echo. "$WARN_COMMON $MAP_OPT $VERBOSE_OPT" |
| 121 | } |
| 122 | |
| 123 | # gold may not support --sort-common (yet) |
| 124 | SORT_COMMON="-Wl,--sort-common" |
| 125 | if ! check_ccscip-shell shell shell-project 0.0.0 check_cc(). "-Wl,--sort-common"; then |
| 126 | echoscip-shell system . . echo. "Your linker does not support --sort-common" |
| 127 | SORT_COMMON="" |
| 128 | fi |
| 129 | |
| 130 | # Static linking against glibc produces buggy executables |
| 131 | # (glibc does not cope well with ld --gc-sections). |
| 132 | # See sources.redhat.com/bugzilla/show_bug.cgi?id=3400 |
| 133 | # Note that glibc is unsuitable for static linking anyway. |
| 134 | # We are removing -Wl,--gc-sections from link command line. |
| 135 | GC_SECTIONS="-Wl,--gc-sections" |
| 136 | if (. ./.configscip-shell source . . `./.config`. && testscip-shell system . . test. x"$CONFIG_STATIC" = x"y") then |
| 137 | if check_libc_is_glibcscip-shell shell shell-project 0.0.0 check_libc_is_glibc().; then |
| 138 | echoscip-shell system . . echo. "Static linking against glibc, can't use --gc-sections" |
| 139 | GC_SECTIONS="" |
| 140 | fi |
| 141 | fi |
| 142 | # The --gc-sections option is not supported by older versions of ld |
| 143 | if testscip-shell system . . test. -n "$GC_SECTIONS"; then |
| 144 | if ! check_ccscip-shell shell shell-project 0.0.0 check_cc(). "$GC_SECTIONS"; then |
| 145 | echoscip-shell system . . echo. "Your linker does not support $GC_SECTIONS" |
| 146 | GC_SECTIONS="" |
| 147 | fi |
| 148 | fi |
| 149 | |
| 150 | # Sanitize lib list (dups, extra spaces etc) |
| 151 | LDLIBS=`echoscip-shell system . . echo. "$LDLIBS" | xargsscip-shell system . . xargs. -n1 | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. | xargsscip-shell system . . xargs.` |
| 152 | |
| 153 | # First link with all libs. If it fails, bail out |
| 154 | echoscip-shell system . . echo. "Trying libraries: $LDLIBS" |
| 155 | # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3" |
| 156 | l_list=`echoscip-shell system . . echo. " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sedscip-shell system . . sed. -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'` |
| 157 | testscip-shell system . . test. x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP" |
| 158 | tryscip-shell shell shell-project 0.0.0 try(). $CC $CFLAGS $LDFLAGS \ |
| 159 | -o $EXE \ |
| 160 | $SORT_COMMON \ |
| 161 | $SORT_SECTION \ |
| 162 | $GC_SECTIONS \ |
| 163 | $START_GROUP $O_FILES $A_FILES $END_GROUP \ |
| 164 | $l_list \ |
| 165 | || { |
| 166 | echoscip-shell system . . echo. "Failed: $l_list" |
| 167 | catscip-shell system . . cat. $EXE.out |
| 168 | echoscip-shell system . . echo. 'Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.' |
| 169 | echoscip-shell system . . echo. 'Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"' |
| 170 | exit 1 |
| 171 | } |
| 172 | |
| 173 | # Now try to remove each lib and build without it. |
| 174 | # Stop when no lib can be removed. |
| 175 | while testscip-shell system . . test. "$LDLIBS"; do |
| 176 | $debug && echoscip-shell system . . echo. "Trying libraries: $LDLIBS" |
| 177 | dropped_non_first_lib=false |
| 178 | first_lib=true |
| 179 | for one in $LDLIBS; do |
| 180 | without_one=`echoscip-shell system . . echo. " $LDLIBS " | sedscip-shell system . . sed. "s/ $one / /g" | xargsscip-shell system . . xargs.` |
| 181 | # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3" |
| 182 | l_list=`echoscip-shell system . . echo. " $without_one $CONFIG_EXTRA_LDLIBS " | sedscip-shell system . . sed. -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'` |
| 183 | testscip-shell system . . test. x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP" |
| 184 | $debug && echoscip-shell system . . echo. "Trying -l options: '$l_list'" |
| 185 | tryscip-shell shell shell-project 0.0.0 try(). $CC $CFLAGS $LDFLAGS \ |
| 186 | -o $EXE \ |
| 187 | $SORT_COMMON \ |
| 188 | $SORT_SECTION \ |
| 189 | $GC_SECTIONS \ |
| 190 | $START_GROUP $O_FILES $A_FILES $END_GROUP \ |
| 191 | $l_list |
| 192 | if testscip-shell system . . test. $? = 0; then |
| 193 | echoscip-shell system . . echo. " Library $one is not needed, excluding it" |
| 194 | LDLIBS="$without_one" |
| 195 | $first_lib || dropped_non_first_lib=true |
| 196 | else |
| 197 | echoscip-shell system . . echo. " Library $one is needed, can't exclude it (yet)" |
| 198 | first_lib=false |
| 199 | fi |
| 200 | done |
| 201 | # We can stop trying to drop libs if either all libs were needed, |
| 202 | # or we excluded only the _first_ few. |
| 203 | # (else: we dropped some intermediate lib(s), maybe now we can succeed |
| 204 | # in dropping some of the preceding ones) |
| 205 | $dropped_non_first_lib || break |
| 206 | done |
| 207 | |
| 208 | # Make the binary with final, minimal list of libs |
| 209 | echoscip-shell system . . echo. "Final link with: ${LDLIBS:-<none>}" |
| 210 | l_list=`echoscip-shell system . . echo. " $LDLIBS $CONFIG_EXTRA_LDLIBS " | sedscip-shell system . . sed. -e 's: \([^- ][^ ]*\): -l\1:g' -e 's/^ *//'` |
| 211 | testscip-shell system . . test. x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP" |
| 212 | # --verbose gives us gobs of info to stdout (e.g. linker script used) |
| 213 | if ! testscip-shell system . . test. -f busybox_ldscript; then |
| 214 | tryscip-shell shell shell-project 0.0.0 try(). $CC $CFLAGS $LDFLAGS \ |
| 215 | -o $EXE \ |
| 216 | $SORT_COMMON \ |
| 217 | $SORT_SECTION \ |
| 218 | $GC_SECTIONS \ |
| 219 | $START_GROUP $O_FILES $A_FILES $END_GROUP \ |
| 220 | $l_list scip-shell shell shell-project 0.0.0 INFO_OPTS().\ |
| 221 | `INFO_OPTS` \ |
| 222 | || { |
| 223 | catscip-shell system . . cat. $EXE.out |
| 224 | exit 1 |
| 225 | } |
| 226 | else |
| 227 | echoscip-shell system . . echo. "Custom linker script 'busybox_ldscript' found, using it" |
| 228 | # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out): |
| 229 | # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) } |
| 230 | # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*) |
| 231 | # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*) |
| 232 | # This will eliminate most of the padding (~3kb). |
| 233 | # Hmm, "ld --sort-section alignment" should do it too. |
| 234 | # |
| 235 | # There is a ld hack which is meant to decrease disk usage |
| 236 | # at the cost of more RAM usage (??!!) in standard ld script: |
| 237 | # /* Adjust the address for the data segment. We want to adjust up to |
| 238 | # the same address within the page on the next page up. */ |
| 239 | # . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000); |
| 240 | # Replace it with: |
| 241 | # . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000); |
| 242 | # to unconditionally align .data to the next page boundary, |
| 243 | # instead of "next page, plus current offset in this page" |
| 244 | tryscip-shell shell shell-project 0.0.0 try(). $CC $CFLAGS $LDFLAGS \ |
| 245 | -o $EXE \ |
| 246 | $SORT_COMMON \ |
| 247 | $SORT_SECTION \ |
| 248 | $GC_SECTIONS \ |
| 249 | -Wl,-T,busybox_ldscript \ |
| 250 | $START_GROUP $O_FILES $A_FILES $END_GROUP \ |
| 251 | $l_list scip-shell shell shell-project 0.0.0 INFO_OPTS().\ |
| 252 | `INFO_OPTS` \ |
| 253 | || { |
| 254 | catscip-shell system . . cat. $EXE.out |
| 255 | exit 1 |
| 256 | } |
| 257 | fi |
| 258 | |
| 259 | . ./.configscip-shell source . . `./.config`. |
| 260 | |
| 261 | sharedlib_dir="0_lib" |
| 262 | |
| 263 | if testscip-shell system . . test. "$CONFIG_BUILD_LIBBUSYBOX" = y; then |
| 264 | mkdirscip-shell system . . mkdir. "$sharedlib_dir" 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 265 | testscip-shell system . . test. -d "$sharedlib_dir" || { |
| 266 | echoscip-shell system . . echo. "Cannot make directory $sharedlib_dir" |
| 267 | exit 1 |
| 268 | } |
| 269 | lnscip-shell system . . ln. -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 270 | |
| 271 | # Yes, "ld -shared -static" is a thing. It's a shared library which is itself static. |
| 272 | LBB_STATIC="" |
| 273 | testscip-shell system . . test. "$CONFIG_FEATURE_LIBBUSYBOX_STATIC" = y && LBB_STATIC="-Wl,-static" |
| 274 | |
| 275 | EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped" |
| 276 | tryscip-shell shell shell-project 0.0.0 try(). $CC $CFLAGS $LDFLAGS \ |
| 277 | -o $EXE \ |
| 278 | -shared -fPIC $LBB_STATIC \ |
| 279 | -Wl,--enable-new-dtags \ |
| 280 | -Wl,-z,combreloc \ |
| 281 | -Wl,-soname="libbusybox.so.$BB_VER" \ |
| 282 | -Wl,--undefined=lbb_main \ |
| 283 | $SORT_COMMON \ |
| 284 | $SORT_SECTION \ |
| 285 | $START_GROUP $A_FILES $END_GROUP \ |
| 286 | $l_list scip-shell shell shell-project 0.0.0 INFO_OPTS().\ |
| 287 | `INFO_OPTS` \ |
| 288 | || { |
| 289 | echoscip-shell system . . echo. "Linking $EXE failed" |
| 290 | catscip-shell system . . cat. $EXE.out |
| 291 | exit 1 |
| 292 | } |
| 293 | $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER" |
| 294 | chmodscip-shell system . . chmod. a+x "$sharedlib_dir/libbusybox.so.$BB_VER" |
| 295 | echoscip-shell system . . echo. "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER" |
| 296 | fi |
| 297 | |
| 298 | if testscip-shell system . . test. "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then |
| 299 | EXE="$sharedlib_dir/busybox_unstripped" |
| 300 | tryscip-shell shell shell-project 0.0.0 try(). $CC $CFLAGS $LDFLAGS \ |
| 301 | -o $EXE \ |
| 302 | $SORT_COMMON \ |
| 303 | $SORT_SECTION \ |
| 304 | $GC_SECTIONS \ |
| 305 | $START_GROUP $O_FILES $END_GROUP \ |
| 306 | -L"$sharedlib_dir" -lbusybox \ |
| 307 | $l_list scip-shell shell shell-project 0.0.0 INFO_OPTS().\ |
| 308 | `INFO_OPTS` \ |
| 309 | || { |
| 310 | echoscip-shell system . . echo. "Linking $EXE failed" |
| 311 | catscip-shell system . . cat. $EXE.out |
| 312 | exit 1 |
| 313 | } |
| 314 | $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox" |
| 315 | echoscip-shell system . . echo. "busybox linked against libbusybox: $sharedlib_dir/busybox" |
| 316 | fi |
| 317 | |
| 318 | if testscip-shell system . . test. "$CONFIG_FEATURE_INDIVIDUAL" = y; then |
| 319 | echoscip-shell system . . echo. "Linking individual applets against libbusybox (see $sharedlib_dir/*)" |
| 320 | gccscip-shell system . . gcc. -DNAME_MAIN -E -include include/autoconf.h include/applets.h \ |
| 321 | | grepscip-shell system . . grep. -v "^#" \ |
| 322 | | grepscip-shell system . . grep. -v "^ *$" \ |
| 323 | > applet_lst.tmp |
| 324 | while read name main junk; do |
| 325 | |
| 326 | echoscip-shell system . . echo. "\ |
| 327 | void lbb_prepare(const char *applet, char **argv); |
| 328 | int $main(int argc, char **argv); |
| 329 | |
| 330 | int main(int argc, char **argv) |
| 331 | { |
| 332 | lbb_prepare(\"$name\", argv); |
| 333 | return $main(argc, argv); |
| 334 | } |
| 335 | " >"$sharedlib_dir/applet.c" |
| 336 | |
| 337 | EXE="$sharedlib_dir/$name" |
| 338 | tryscip-shell shell shell-project 0.0.0 try(). $CC $CFLAGS $LDFLAGS "$sharedlib_dir/applet.c" \ |
| 339 | -o $EXE \ |
| 340 | $SORT_COMMON \ |
| 341 | $SORT_SECTION \ |
| 342 | $GC_SECTIONS \ |
| 343 | -L"$sharedlib_dir" -lbusybox \ |
| 344 | -Wl,--warn-common \ |
| 345 | || { |
| 346 | echoscip-shell system . . echo. "Linking $EXE failed" |
| 347 | catscip-shell system . . cat. $EXE.out |
| 348 | exit 1 |
| 349 | } |
| 350 | rmscip-shell system . . rm. -- "$sharedlib_dir/applet.c" $EXE.out |
| 351 | $STRIP -s --remove-section=.note --remove-section=.comment $EXE |
| 352 | # Let user see that we do something - list the names of created binaries: |
| 353 | echoscip-shell system . . echo. "$EXE" |
| 354 | |
| 355 | done <applet_lst.tmp |
| 356 | fi |
| 357 | |
| 358 | # libbusybox.so is needed only for -lbusybox at link time, |
| 359 | # it is not needed at runtime. Deleting to reduce confusion. |
| 360 | rmscip-shell system . . rm. "$sharedlib_dir"/libbusybox.so 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 361 | exit 0 # or else we may confuse make |