3
0

trylink 11 KB

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