trylink 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/sh
  2. debug=false
  3. try() {
  4. added="$1"
  5. shift
  6. $debug && echo "Trying: $* $added"
  7. "$@" $added 2>busybox_ld.err
  8. }
  9. # Sanitize lib list (dups, extra spaces etc)
  10. #echo "BBOX_LIB_LIST=$BBOX_LIB_LIST"
  11. BBOX_LIB_LIST=`echo "$BBOX_LIB_LIST" | xargs -n1 | sort | uniq | xargs`
  12. # First link with all libs. If it fails, bail out
  13. echo "Trying libraries: $BBOX_LIB_LIST"
  14. l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  15. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
  16. try "$l_list" "$@" \
  17. || {
  18. echo "Failed: $* -Wl,--start-group $l_list -Wl,--end-group"
  19. cat busybox_ld.err
  20. exit 1
  21. }
  22. # Now try to remove each lib and build without it.
  23. # Stop when no lib can be removed.
  24. while test "$BBOX_LIB_LIST"; do
  25. $debug && echo "Trying libraries: $BBOX_LIB_LIST"
  26. all_needed=true
  27. for one in $BBOX_LIB_LIST; do
  28. without_one=`echo " $BBOX_LIB_LIST " | sed "s/ $one / /g" | xargs`
  29. l_list=`echo "$without_one" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  30. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group"
  31. $debug && echo "Trying -l options: '$l_list'"
  32. if try "$l_list" "$@"; then
  33. echo "Library $one is not needed"
  34. BBOX_LIB_LIST="$without_one"
  35. all_needed=false
  36. else
  37. echo "Library $one is needed"
  38. fi
  39. done
  40. # All libs were needed, can't remove any
  41. $all_needed && break
  42. # If there is no space char, the list has just one lib.
  43. # I'm not sure that in this case lib really is 100% needed.
  44. # Let's try linking without it anyway... thus commented out.
  45. #{ echo "$BBOX_LIB_LIST" | grep -q ' '; } || break
  46. done
  47. # Make the binary with final, minimal list of libs
  48. echo "Final link with: $BBOX_LIB_LIST"
  49. l_list=`echo "$BBOX_LIB_LIST" | sed -e 's/ / -l/g' -e 's/^/-l/' -e 's/^-l$//'`
  50. test "x$l_list" != "x" && l_list="-Wl,--start-group $l_list -Wl,--end-group -Wl,--verbose"
  51. # --verbose gives us gobs of info to stdout (e.g. linker script used)
  52. if ! test -f busybox_ldscript; then
  53. try "$l_list -Wl,--verbose" "$@" >busybox_ld.out
  54. else
  55. echo "Custom linker script 'busybox_ldscript' found, using it"
  56. # Add SORT_BY_ALIGNMENT to linker script (found in busybox_ld.out):
  57. # .rodata : { *(.rodata SORT_BY_ALIGNMENT(.rodata.*) .gnu.linkonce.r.*) }
  58. # *(.data SORT_BY_ALIGNMENT(.data.*) .gnu.linkonce.d.*)
  59. # *(.bss SORT_BY_ALIGNMENT(.bss.*) .gnu.linkonce.b.*)
  60. # This will eliminate most of the data padding (~3kb).
  61. try "$l_list -Wl,--verbose -Wl,-T -Wl,busybox_ldscript" "$@" >busybox_ld.out
  62. fi