3
0

trylink 10 KB

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