3
0

trylink 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 -Wl,--sort-section option is not supported by older versions of ld
  79. SORT_SECTION=`check_cc "-Wl,--sort-section -Wl,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_SECTION=`(
  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_SECTION \
  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. for one in $LDLIBS; do
  118. without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
  119. # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
  120. l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  121. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
  122. $debug && echo "Trying -l options: '$l_list'"
  123. try $CC $CFLAGS $LDFLAGS \
  124. -o $EXE \
  125. -Wl,--sort-common \
  126. $SORT_SECTION \
  127. $GC_SECTION \
  128. -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
  129. $l_list
  130. if test $? = 0; then
  131. echo " Library $one is not needed"
  132. LDLIBS="$without_one"
  133. all_needed=false
  134. else
  135. echo " Library $one is needed"
  136. fi
  137. done
  138. # All libs were needed, can't remove any
  139. $all_needed && break
  140. # If there is no space char, the list has just one lib.
  141. # I'm not sure that in this case lib really is 100% needed.
  142. # Let's try linking without it anyway... thus commented out.
  143. #{ echo "$LDLIBS" | grep -q ' '; } || break
  144. done
  145. # Make the binary with final, minimal list of libs
  146. echo "Final link with: ${LDLIBS:-<none>}"
  147. l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  148. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
  149. # --verbose gives us gobs of info to stdout (e.g. linker script used)
  150. if ! test -f busybox_ldscript; then
  151. try $CC $CFLAGS $LDFLAGS \
  152. -o $EXE \
  153. -Wl,--sort-common \
  154. $SORT_SECTION \
  155. $GC_SECTION \
  156. -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
  157. $l_list \
  158. -Wl,--warn-common \
  159. -Wl,-Map -Wl,$EXE.map \
  160. -Wl,--verbose \
  161. || {
  162. cat $EXE.out
  163. exit 1
  164. }
  165. else
  166. echo "Custom linker script 'busybox_ldscript' found, using it"
  167. # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
  168. # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
  169. # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
  170. # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
  171. # This will eliminate most of the padding (~3kb).
  172. # Hmm, "ld --sort-section alignment" should do it too.
  173. try $CC $CFLAGS $LDFLAGS \
  174. -o $EXE \
  175. -Wl,--sort-common \
  176. $SORT_SECTION \
  177. $GC_SECTION \
  178. -Wl,-T -Wl,busybox_ldscript \
  179. -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
  180. $l_list \
  181. -Wl,--warn-common \
  182. -Wl,-Map -Wl,$EXE.map \
  183. -Wl,--verbose \
  184. || {
  185. cat $EXE.out
  186. exit 1
  187. }
  188. fi
  189. . ./.config
  190. sharedlib_dir="0_lib"
  191. if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
  192. mkdir "$sharedlib_dir" 2>/dev/null
  193. test -d "$sharedlib_dir" || {
  194. echo "Cannot make directory $sharedlib_dir"
  195. exit 1
  196. }
  197. ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
  198. EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
  199. try $CC $CFLAGS $LDFLAGS \
  200. -o $EXE \
  201. -shared -fPIC \
  202. -Wl,--enable-new-dtags \
  203. -Wl,-z,combreloc \
  204. -Wl,-soname="libbusybox.so.$BB_VER" \
  205. -Wl,--undefined=lbb_main \
  206. -Wl,--sort-common \
  207. $SORT_SECTION \
  208. -Wl,--start-group $A_FILES -Wl,--end-group \
  209. $l_list \
  210. -Wl,--warn-common \
  211. -Wl,-Map -Wl,$EXE.map \
  212. -Wl,--verbose \
  213. || {
  214. echo "Linking $EXE failed"
  215. cat $EXE.out
  216. exit 1
  217. }
  218. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
  219. chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
  220. echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
  221. fi
  222. if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
  223. EXE="$sharedlib_dir/busybox_unstripped"
  224. try $CC $CFLAGS $LDFLAGS \
  225. -o $EXE \
  226. -Wl,--sort-common \
  227. $SORT_SECTION \
  228. $GC_SECTION \
  229. -Wl,--start-group $O_FILES -Wl,--end-group \
  230. -L"$sharedlib_dir" -lbusybox \
  231. -Wl,--warn-common \
  232. -Wl,-Map -Wl,$EXE.map \
  233. -Wl,--verbose \
  234. || {
  235. echo "Linking $EXE failed"
  236. cat $EXE.out
  237. exit 1
  238. }
  239. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
  240. echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
  241. fi
  242. if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
  243. echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
  244. gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
  245. | grep -v "^#" \
  246. | grep -v "^$" \
  247. > applet_lst.tmp
  248. while read name main junk; do
  249. echo "\
  250. void lbb_prepare(const char *applet, char **argv);
  251. int $main(int argc, char **argv);
  252. int main(int argc, char **argv)
  253. {
  254. lbb_prepare(\"$name\", argv);
  255. return $main(argc, argv);
  256. }
  257. " >"$sharedlib_dir/applet.c"
  258. EXE="$sharedlib_dir/$name"
  259. try $CC $CFLAGS $LDFLAGS "$sharedlib_dir/applet.c" \
  260. -o $EXE \
  261. -Wl,--sort-common \
  262. $SORT_SECTION \
  263. $GC_SECTION \
  264. -L"$sharedlib_dir" -lbusybox \
  265. -Wl,--warn-common \
  266. || {
  267. echo "Linking $EXE failed"
  268. cat $EXE.out
  269. exit 1
  270. }
  271. rm -- "$sharedlib_dir/applet.c" $EXE.out
  272. $STRIP -s --remove-section=.note --remove-section=.comment $EXE
  273. done <applet_lst.tmp
  274. fi
  275. # libbusybox.so is needed only for -lbusybox at link time,
  276. # it is not needed at runtime. Deleting to reduce confusion.
  277. rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
  278. exit 0 # or else we may confuse make