trylink 10 KB

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