3
0

trylink 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. dropped_non_first_lib=false
  139. first_lib=true
  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. $first_lib || dropped_non_first_lib=true
  157. else
  158. echo " Library $one is needed, can't exclude it (yet)"
  159. first_lib=false
  160. fi
  161. done
  162. # We can stop trying to drop libs if either all libs were needed,
  163. # or we excluded only the _first_ few.
  164. # (else: we dropped some intermediate lib(s), maybe now we can succeed
  165. # in dropping some of the preceding ones)
  166. $dropped_non_first_lib || break
  167. done
  168. # Make the binary with final, minimal list of libs
  169. echo "Final link with: ${LDLIBS:-<none>}"
  170. l_list=`echo " $LDLIBS " | sed -e 's: \([^- ][^ ]*\): -l\1:g'`
  171. test "x$l_list" != "x" && l_list="$START_GROUP $l_list $END_GROUP"
  172. # --verbose gives us gobs of info to stdout (e.g. linker script used)
  173. if ! test -f busybox_ldscript; then
  174. try $CC $CFLAGS $LDFLAGS \
  175. -o $EXE \
  176. $SORT_COMMON \
  177. $SORT_SECTION \
  178. $GC_SECTIONS \
  179. $START_GROUP $O_FILES $A_FILES $END_GROUP \
  180. $l_list \
  181. `INFO_OPTS` \
  182. || {
  183. cat $EXE.out
  184. exit 1
  185. }
  186. else
  187. echo "Custom linker script 'busybox_ldscript' found, using it"
  188. # Add SORT_BY_ALIGNMENT to linker script (found in $EXE.out):
  189. # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
  190. # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
  191. # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
  192. # This will eliminate most of the padding (~3kb).
  193. # Hmm, "ld --sort-section alignment" should do it too.
  194. #
  195. # There is a ld hack which is meant to decrease disk usage
  196. # at the cost of more RAM usage (??!!) in standard ld script:
  197. # /* Adjust the address for the data segment. We want to adjust up to
  198. # the same address within the page on the next page up. */
  199. # . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1)); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
  200. # Replace it with:
  201. # . = ALIGN (0x1000); . = DATA_SEGMENT_ALIGN (0x1000, 0x1000);
  202. # to unconditionally align .data to the next page boundary,
  203. # instead of "next page, plus current offset in this page"
  204. try $CC $CFLAGS $LDFLAGS \
  205. -o $EXE \
  206. $SORT_COMMON \
  207. $SORT_SECTION \
  208. $GC_SECTIONS \
  209. -Wl,-T,busybox_ldscript \
  210. $START_GROUP $O_FILES $A_FILES $END_GROUP \
  211. $l_list \
  212. `INFO_OPTS` \
  213. || {
  214. cat $EXE.out
  215. exit 1
  216. }
  217. fi
  218. . ./.config
  219. sharedlib_dir="0_lib"
  220. if test "$CONFIG_BUILD_LIBBUSYBOX" = y; then
  221. mkdir "$sharedlib_dir" 2>/dev/null
  222. test -d "$sharedlib_dir" || {
  223. echo "Cannot make directory $sharedlib_dir"
  224. exit 1
  225. }
  226. ln -s "libbusybox.so.$BB_VER" "$sharedlib_dir"/libbusybox.so 2>/dev/null
  227. # Yes, "ld -shared -static" is a thing. It's a shared library which is itself static.
  228. LBB_STATIC=""
  229. test "$CONFIG_FEATURE_LIBBUSYBOX_STATIC" = y && LBB_STATIC="-Wl,-static"
  230. EXE="$sharedlib_dir/libbusybox.so.${BB_VER}_unstripped"
  231. try $CC $CFLAGS $LDFLAGS \
  232. -o $EXE \
  233. -shared -fPIC $LBB_STATIC \
  234. -Wl,--enable-new-dtags \
  235. -Wl,-z,combreloc \
  236. -Wl,-soname="libbusybox.so.$BB_VER" \
  237. -Wl,--undefined=lbb_main \
  238. $SORT_COMMON \
  239. $SORT_SECTION \
  240. $START_GROUP $A_FILES $END_GROUP \
  241. $l_list \
  242. `INFO_OPTS` \
  243. || {
  244. echo "Linking $EXE failed"
  245. cat $EXE.out
  246. exit 1
  247. }
  248. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/libbusybox.so.$BB_VER"
  249. chmod a+x "$sharedlib_dir/libbusybox.so.$BB_VER"
  250. echo "libbusybox: $sharedlib_dir/libbusybox.so.$BB_VER"
  251. fi
  252. if test "$CONFIG_FEATURE_SHARED_BUSYBOX" = y; then
  253. EXE="$sharedlib_dir/busybox_unstripped"
  254. try $CC $CFLAGS $LDFLAGS \
  255. -o $EXE \
  256. $SORT_COMMON \
  257. $SORT_SECTION \
  258. $GC_SECTIONS \
  259. $START_GROUP $O_FILES $END_GROUP \
  260. -L"$sharedlib_dir" -lbusybox \
  261. $l_list \
  262. `INFO_OPTS` \
  263. || {
  264. echo "Linking $EXE failed"
  265. cat $EXE.out
  266. exit 1
  267. }
  268. $STRIP -s --remove-section=.note --remove-section=.comment $EXE -o "$sharedlib_dir/busybox"
  269. echo "busybox linked against libbusybox: $sharedlib_dir/busybox"
  270. fi
  271. if test "$CONFIG_FEATURE_INDIVIDUAL" = y; then
  272. echo "Linking individual applets against libbusybox (see $sharedlib_dir/*)"
  273. gcc -DNAME_MAIN -E -include include/autoconf.h include/applets.h \
  274. | grep -v "^#" \
  275. | grep -v "^ *$" \
  276. > applet_lst.tmp
  277. while read name main junk; do
  278. echo "\
  279. void lbb_prepare(const char *applet, char **argv);
  280. int $main(int argc, char **argv);
  281. int main(int argc, char **argv)
  282. {
  283. lbb_prepare(\"$name\", argv);
  284. return $main(argc, argv);
  285. }
  286. " >"$sharedlib_dir/applet.c"
  287. EXE="$sharedlib_dir/$name"
  288. try $CC $CFLAGS $LDFLAGS "$sharedlib_dir/applet.c" \
  289. -o $EXE \
  290. $SORT_COMMON \
  291. $SORT_SECTION \
  292. $GC_SECTIONS \
  293. -L"$sharedlib_dir" -lbusybox \
  294. -Wl,--warn-common \
  295. || {
  296. echo "Linking $EXE failed"
  297. cat $EXE.out
  298. exit 1
  299. }
  300. rm -- "$sharedlib_dir/applet.c" $EXE.out
  301. $STRIP -s --remove-section=.note --remove-section=.comment $EXE
  302. # Let user see that we do something - list the names of created binaries:
  303. echo "$EXE"
  304. done <applet_lst.tmp
  305. fi
  306. # libbusybox.so is needed only for -lbusybox at link time,
  307. # it is not needed at runtime. Deleting to reduce confusion.
  308. rm "$sharedlib_dir"/libbusybox.so 2>/dev/null
  309. exit 0 # or else we may confuse make