trylink 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. if $CC $1 -shared -o /dev/null -xc /dev/null > /dev/null 2>&1; then
  46. echo "$1";
  47. else
  48. echo "$2";
  49. fi
  50. }
  51. EXE="$1"
  52. CC="$2"
  53. LDFLAGS="$3"
  54. O_FILES="$4"
  55. A_FILES="$5"
  56. LDLIBS="$6"
  57. # The -Wl,--sort-section option is not supported by older versions of ld
  58. SORT_SECTION=`check_cc "-Wl,--sort-section -Wl,alignment" ""`
  59. # Sanitize lib list (dups, extra spaces etc)
  60. LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
  61. # First link with all libs. If it fails, bail out
  62. echo "Trying libraries: $LDLIBS"
  63. # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
  64. l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  65. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
  66. try $CC $LDFLAGS \
  67. -o $EXE \
  68. -Wl,--sort-common \
  69. $SORT_SECTION \
  70. -Wl,--gc-sections \
  71. -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
  72. $l_list \
  73. || {
  74. echo "Failed: $* $l_list"
  75. cat $EXE.out
  76. exit 1
  77. }
  78. # Now try to remove each lib and build without it.
  79. # Stop when no lib can be removed.
  80. while test "$LDLIBS"; do
  81. $debug && echo "Trying libraries: $LDLIBS"
  82. all_needed=true
  83. for one in $LDLIBS; do
  84. without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
  85. # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
  86. l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  87. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
  88. $debug && echo "Trying -l options: '$l_list'"
  89. try $CC $LDFLAGS \
  90. -o $EXE \
  91. -Wl,--sort-common \
  92. $SORT_SECTION \
  93. -Wl,--gc-sections \
  94. -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
  95. $l_list
  96. if test $? = 0; then
  97. echo "Library $one is not needed"
  98. LDLIBS="$without_one"
  99. all_needed=false
  100. else
  101. echo "Library $one is needed"
  102. fi
  103. done
  104. # All libs were needed, can't remove any
  105. $all_needed && break
  106. # If there is no space char, the list has just one lib.
  107. # I'm not sure that in this case lib really is 100% needed.
  108. # Let's try linking without it anyway... thus commented out.
  109. #{ echo "$LDLIBS" | grep -q ' '; } || break
  110. done
  111. # Make the binary with final, minimal list of libs
  112. echo "Final link with: $LDLIBS"
  113. l_list=`echo "$LDLIBS" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  114. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
  115. # --verbose gives us gobs of info to stdout (e.g. linker script used)
  116. if ! test -f busybox_ldscript; then
  117. try $CC $LDFLAGS \
  118. -o $EXE \
  119. -Wl,--sort-common \
  120. $SORT_SECTION \
  121. -Wl,--gc-sections \
  122. -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
  123. $l_list \
  124. -Wl,--warn-common \
  125. -Wl,-Map -Wl,$EXE.map \
  126. -Wl,--verbose \
  127. || {
  128. cat $EXE.out
  129. exit 1
  130. }
  131. else
  132. echo "Custom linker script 'busybox_ldscript' found, using it"
  133. # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
  134. # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
  135. # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
  136. # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
  137. # This will eliminate most of the padding (~3kb).
  138. # Hmm, "ld --sort-section alignment" should do it too.
  139. try $CC $LDFLAGS \
  140. -o $EXE \
  141. -Wl,--sort-common \
  142. $SORT_SECTION \
  143. -Wl,--gc-sections \
  144. -Wl,-T -Wl,busybox_ldscript \
  145. -Wl,--start-group $O_FILES $A_FILES -Wl,--end-group \
  146. $l_list \
  147. -Wl,--warn-common \
  148. -Wl,-Map -Wl,$EXE.map \
  149. -Wl,--verbose \
  150. || {
  151. cat $EXE.out
  152. exit 1
  153. }
  154. fi
  155. . ./.config
  156. sharedlib_dir="0_lib"
  157. if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
  158. mkdir "$sharedlib_dir" 2>/dev/null
  159. test -d "$sharedlib_dir" || {
  160. echo "Cannot make directory $sharedlib_dir"
  161. exit 1
  162. }
  163. ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
  164. EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
  165. try $CC $LDFLAGS \
  166. -o $EXE \
  167. -shared -fPIC \
  168. -Wl,--enable-new-dtags \
  169. -Wl,-z,combreloc \
  170. -Wl,-soname="libbusybox.so.$BB_VER" \
  171. -Wl,--undefined=lbb_main \
  172. -Wl,--sort-common \
  173. $SORT_SECTION \
  174. -Wl,--start-group $A_FILES -Wl,--end-group \
  175. $l_list \
  176. -Wl,--warn-common \
  177. -Wl,-Map -Wl,$EXE.map \
  178. -Wl,--verbose \
  179. || {
  180. echo "Linking $EXE failed"
  181. cat $EXE.out
  182. exit 1
  183. }
  184. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
  185. chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
  186. echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
  187. fi
  188. if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
  189. EXE="$sharedlib_dir/busybox_unstripped"
  190. try $CC $LDFLAGS \
  191. -o $EXE \
  192. -Wl,--sort-common \
  193. $SORT_SECTION \
  194. -Wl,--gc-sections \
  195. -Wl,--start-group $O_FILES -Wl,--end-group \
  196. -L"$sharedlib_dir" -lbusybox \
  197. -Wl,--warn-common \
  198. -Wl,-Map -Wl,$EXE.map \
  199. -Wl,--verbose \
  200. || {
  201. echo "Linking $EXE failed"
  202. cat $EXE.out
  203. exit 1
  204. }
  205. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
  206. echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
  207. fi
  208. if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
  209. echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
  210. gcc -DNAME_MAIN_CNAME -E -include include/autoconf.h include/applets.h \
  211. | grep -v "^#" \
  212. | grep -v "^$" \
  213. > applet_lst.tmp
  214. while read name main junk; do
  215. echo "\
  216. void lbb_prepare(const char *applet, char **argv);
  217. int $main(int argc, char **argv);
  218. int main(int argc, char **argv)
  219. {
  220. lbb_prepare(\"$name\", argv);
  221. return $main(argc, argv);
  222. }
  223. " >"$sharedlib_dir/applet.c"
  224. EXE="$sharedlib_dir/$name"
  225. try $CC $LDFLAGS "$sharedlib_dir/applet.c" \
  226. -o $EXE \
  227. -Wl,--sort-common \
  228. $SORT_SECTION \
  229. -Wl,--gc-sections \
  230. -L"$sharedlib_dir" -lbusybox \
  231. -Wl,--warn-common \
  232. || {
  233. echo "Linking $EXE failed"
  234. cat $EXE.out
  235. exit 1
  236. }
  237. rm -- "$sharedlib_dir/applet.c" $EXE.out
  238. $STRIP -s --remove-section=.note --remove-section=.comment $EXE
  239. done <applet_lst.tmp
  240. fi
  241. # libbusybox.so is needed only for -lbusybox at link time,
  242. # it is not needed at runtime. Deleting to reduce confusion.
  243. rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
  244. exit 0 # or else we may confuse make