trylink 8.9 KB

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