trylink 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. tempname="$(mktemp tmp.XXXXXXXXXX)"
  45. echo "int main(int argc,char**argv){return argv?argc:0;}" >"$tempname".c
  46. # Can use "-o /dev/null", but older gcc tend to *unlink it* on failure! :(
  47. # Was using "-xc /dev/null", but we need a valid C program.
  48. $CC $CFLAGS $LDFLAGS $1 "$tempname".c -o "$tempname" >/dev/null 2>&1
  49. exitcode=$?
  50. rm -f "$tempname" "$tempname".c "$tempname".o
  51. return $exitcode
  52. }
  53. check_libc_is_glibc() {
  54. tempname="$(mktemp tmp.XXXXXXXXXX)"
  55. echo "\
  56. #include <stdlib.h>
  57. /* Apparently uclibc defines __GLIBC__ (compat trick?). Oh well. */
  58. #if defined(__GLIBC__) && !defined(__UCLIBC__)
  59. syntax error here
  60. #endif
  61. " >"$tempname".c
  62. ! $CC $CFLAGS "$tempname".c -c -o "$tempname".o >/dev/null 2>&1
  63. exitcode=$?
  64. rm -f "$tempname" "$tempname".c "$tempname".o
  65. return $exitcode
  66. }
  67. EXE="$1"
  68. CC="$2"
  69. CFLAGS="$3"
  70. LDFLAGS="$4"
  71. O_FILES="$5"
  72. A_FILES="$6"
  73. LDLIBS="$7"
  74. # The --sort-section option is not supported by older versions of ld
  75. SORT_SECTION="-Wl,--sort-section,alignment"
  76. if ! check_cc "-Wl,--sort-section,alignment"; then
  77. echo "Your linker does not support --sort-section,alignment"
  78. SORT_SECTION=""
  79. fi
  80. START_GROUP="-Wl,--start-group"
  81. END_GROUP="-Wl,--end-group"
  82. INFO_OPTS() {
  83. echo "-Wl,--warn-common -Wl,-Map,$EXE.map -Wl,--verbose"
  84. }
  85. # gold may not support --sort-common (yet)
  86. SORT_COMMON="-Wl,--sort-common"
  87. if ! check_cc "-Wl,--sort-common"; then
  88. echo "Your linker does not support --sort-common"
  89. SORT_COMMON=""
  90. fi
  91. # Static linking against glibc produces buggy executables
  92. # (glibc does not cope well with ld --gc-sections).
  93. # See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
  94. # Note that glibc is unsuitable for static linking anyway.
  95. # We are removing -Wl,--gc-sections from link command line.
  96. GC_SECTIONS="-Wl,--gc-sections"
  97. if (. ./.config && test x"$CONFIG_STATIC" = x"y") then
  98. if check_libc_is_glibc; then
  99. echo "Static linking against glibc, can't use --gc-sections"
  100. GC_SECTIONS=""
  101. fi
  102. fi
  103. # The --gc-sections option is not supported by older versions of ld
  104. if test -n "$GC_SECTIONS"; then
  105. if ! check_cc "$GC_SECTIONS"; then
  106. echo "Your linker does not support $GC_SECTIONS"
  107. GC_SECTIONS=""
  108. fi
  109. fi
  110. # Sanitize lib list (dups, extra spaces etc)
  111. LDLIBS=`echo "$LDLIBS" | xargs -n1 | sort | uniq | xargs`
  112. # First link with all libs. If it fails, bail out
  113. echo "Trying libraries: $LDLIBS"
  114. # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
  115. l_list=`echo " $LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g'`
  116. test "x$l_list" != "x" && l_list="$START_GROUP $l_list $END_GROUP"
  117. try $CC $CFLAGS $LDFLAGS \
  118. -o $EXE \
  119. $SORT_COMMON \
  120. $SORT_SECTION \
  121. $GC_SECTIONS \
  122. $START_GROUP $O_FILES $A_FILES $END_GROUP \
  123. $l_list \
  124. || {
  125. echo "Failed: $l_list"
  126. cat $EXE.out
  127. echo 'Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.'
  128. echo 'Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"'
  129. exit 1
  130. }
  131. # Now try to remove each lib and build without it.
  132. # Stop when no lib can be removed.
  133. while test "$LDLIBS"; do
  134. $debug && echo "Trying libraries: $LDLIBS"
  135. dropped_non_first_lib=false
  136. first_lib=true
  137. for one in $LDLIBS; do
  138. without_one=`echo " $LDLIBS " | sed "s/ $one / /g" | xargs`
  139. # "lib1 lib2 lib3" -> "-llib1 -llib2 -llib3"
  140. l_list=`echo " $without_one " | sed -e 's: \([^- ][^ ]*\): -l\1:g'`
  141. test x"$l_list" != x"" && l_list="$START_GROUP $l_list $END_GROUP"
  142. $debug && echo "Trying -l options: '$l_list'"
  143. try $CC $CFLAGS $LDFLAGS \
  144. -o $EXE \
  145. $SORT_COMMON \
  146. $SORT_SECTION \
  147. $GC_SECTIONS \
  148. $START_GROUP $O_FILES $A_FILES $END_GROUP \
  149. $l_list
  150. if test $? = 0; then
  151. echo " Library $one is not needed, excluding it"
  152. LDLIBS="$without_one"
  153. $first_lib || dropped_non_first_lib=true
  154. else
  155. echo " Library $one is needed, can't exclude it (yet)"
  156. first_lib=false
  157. fi
  158. done
  159. # We can stop trying to drop libs if either all libs were needed,
  160. # or we excluded only the _first_ few.
  161. # (else: we dropped some intermediate lib(s), maybe now we can succeed
  162. # in dropping some of the preceding ones)
  163. $dropped_non_first_lib || break
  164. done
  165. # Make the binary with final, minimal list of libs
  166. echo "Final link with: ${LDLIBS:-<none>}"
  167. l_list=`echo " $LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g'`
  168. test "x$l_list" != "x" && l_list="$START_GROUP $l_list $END_GROUP"
  169. # --verbose gives us gobs of info to stdout (e.g. linker script used)
  170. if ! test -f busybox_ldscript; then
  171. try $CC $CFLAGS $LDFLAGS \
  172. -o $EXE \
  173. $SORT_COMMON \
  174. $SORT_SECTION \
  175. $GC_SECTIONS \
  176. $START_GROUP $O_FILES $A_FILES $END_GROUP \
  177. $l_list \
  178. `INFO_OPTS` \
  179. || {
  180. cat $EXE.out
  181. exit 1
  182. }
  183. else
  184. echo "Custom linker script 'busybox_ldscript' found, using it"
  185. # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
  186. # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
  187. # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
  188. # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
  189. # This will eliminate most of the padding (~3kb).
  190. # Hmm, "ld --sort-section alignment" should do it too.
  191. #
  192. # There is a ld hack which is meant to decrease disk usage
  193. # at the cost of more RAM usage (??!!) in standard ld script:
  194. # /* Adjust the address for the data segment. We want to adjust up to
  195. # the same address within the page on the next page up. */
  196. # . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
  197. # Replace it with:
  198. # . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
  199. # to unconditionally align .data to the next page boundary,
  200. # instead of "next page, plus current offset in this page"
  201. try $CC $CFLAGS $LDFLAGS \
  202. -o $EXE \
  203. $SORT_COMMON \
  204. $SORT_SECTION \
  205. $GC_SECTIONS \
  206. -Wl,-T,busybox_ldscript \
  207. $START_GROUP $O_FILES $A_FILES $END_GROUP \
  208. $l_list \
  209. `INFO_OPTS` \
  210. || {
  211. cat $EXE.out
  212. exit 1
  213. }
  214. fi
  215. . ./.config
  216. sharedlib_dir="0_lib"
  217. if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
  218. mkdir "$sharedlib_dir" 2>/dev/null
  219. test -d "$sharedlib_dir" || {
  220. echo "Cannot make directory $sharedlib_dir"
  221. exit 1
  222. }
  223. ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
  224. # Yes, "ld -shared -static" is a thing. It's a shared library which is itself static.
  225. LBB_STATIC=""
  226. test "$CONFIG_FEATURE_LIBBUSYBOX_STATIC" = y && LBB_STATIC="-Wl,-static"
  227. EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
  228. try $CC $CFLAGS $LDFLAGS \
  229. -o $EXE \
  230. -shared -fPIC $LBB_STATIC \
  231. -Wl,--enable-new-dtags \
  232. -Wl,-z,combreloc \
  233. -Wl,-soname="libbusybox.so.$BB_VER" \
  234. -Wl,--undefined=lbb_main \
  235. $SORT_COMMON \
  236. $SORT_SECTION \
  237. $START_GROUP $A_FILES $END_GROUP \
  238. $l_list \
  239. `INFO_OPTS` \
  240. || {
  241. echo "Linking $EXE failed"
  242. cat $EXE.out
  243. exit 1
  244. }
  245. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
  246. chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
  247. echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
  248. fi
  249. if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
  250. EXE="$sharedlib_dir/busybox_unstripped"
  251. try $CC $CFLAGS $LDFLAGS \
  252. -o $EXE \
  253. $SORT_COMMON \
  254. $SORT_SECTION \
  255. $GC_SECTIONS \
  256. $START_GROUP $O_FILES $END_GROUP \
  257. -L"$sharedlib_dir" -lbusybox \
  258. $l_list \
  259. `INFO_OPTS` \
  260. || {
  261. echo "Linking $EXE failed"
  262. cat $EXE.out
  263. exit 1
  264. }
  265. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
  266. echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
  267. fi
  268. if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
  269. echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
  270. gcc -DNAME_MAIN -E -include include/autoconf.h include/applets.h \
  271. | grep -v "^#" \
  272. | grep -v "^ *$" \
  273. > applet_lst.tmp
  274. while read name main junk; do
  275. echo "\
  276. void lbb_prepare(const char *applet, char **argv);
  277. int $main(int argc, char **argv);
  278. int main(int argc, char **argv)
  279. {
  280. lbb_prepare(\"$name\", argv);
  281. return $main(argc, argv);
  282. }
  283. " >"$sharedlib_dir/applet.c"
  284. EXE="$sharedlib_dir/$name"
  285. try $CC $CFLAGS $LDFLAGS "$sharedlib_dir/applet.c" \
  286. -o $EXE \
  287. $SORT_COMMON \
  288. $SORT_SECTION \
  289. $GC_SECTIONS \
  290. -L"$sharedlib_dir" -lbusybox \
  291. -Wl,--warn-common \
  292. || {
  293. echo "Linking $EXE failed"
  294. cat $EXE.out
  295. exit 1
  296. }
  297. rm -- "$sharedlib_dir/applet.c" $EXE.out
  298. $STRIP -s --remove-section=.note --remove-section=.comment $EXE
  299. # Let user see that we do something - list the names of created binaries:
  300. echo "$EXE"
  301. done <applet_lst.tmp
  302. fi
  303. # libbusybox.so is needed only for -lbusybox at link time,
  304. # it is not needed at runtime. Deleting to reduce confusion.
  305. rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
  306. exit 0 # or else we may confuse make