3
0

trylink 8.7 KB

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