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