trylink 10 KB

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