This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Section garbage collection for libraries?


Hi linker gurus,

I need your advice again.

SEED_O="built-in.o"
FILE_A="archival/lib.a archival/libunarchive/lib.a ..."

I link an executable like this:

gcc -o busybox -Wl,--sort-common -Wl,--gc-sections \
    -Wl,--start-group $SEED_O $FILE_A -Wl,--end-group \
    -Wl,--start-group -lcrypt -lm -Wl,--end-group \

busybox is built ok and executes ok.
--gc-sections removes rather lot of unused stuff. I'm happy.

However, people which need to run on architectures which do not
allow for code/rodata reuse (some NOMMU architectures), need
to move bulk of executable into shared library.

I am trying to make it work on i386 first:

gcc -o libLIBRARY.so -shared -fPIC -Wl,--enable-new-dtags \
    -Wl,--start-group -Wl,--whole-archive $FILE_A -Wl,--no-whole-archive -Wl,--end-group \
    -Wl,--start-group -lcrypt -lm -Wl,--end-group \
    -Wl,-soname=libLIBRARY.so -Wl,-z,combreloc \

gcc -o busybox_SHARED -Wl,--sort-common -Wl,--gc-sections \
    -Wl,--start-group $SEED_O -Wl,--end-group -L. -lLIBRARY \


It works (links and runs), but libLIBRARY.so is much bigger
than BINARY from the first example due to lack of --gc-sections.

I try to cheat and convince ld to do GC collection
for libLIBRARY.so. Something like:

ld -o busybox_CHEAT \
    \
    --entry=main \
    --unresolved-symbols=ignore-all \
    \
    --sort-common \
    --gc-sections \
    --start-group \
    $SEED_O $FILE_A \
    --end-group \

ld -o libCHEAT.so -r busybox_CHEAT

gcc -o busybox_SHARED2 \
    \
    -Wl,--allow-multiple-definition \
    \
    -Wl,--sort-common \
    -Wl,--gc-sections \
    -Wl,--start-group \
    $SEED_O \
    -Wl,--end-group -L. -lCHEAT \

it produces busybox_SHARED2, but it doesn't work. It segfaults after it loads libc.so.6.

A bit of reasons what I'm trying to achieve:
Sizes:
$ size * 2>/dev/null
   text    data     bss     dec     hex filename
   2669       0       9    2678     a76 built-in.o
 669240    2640   12200  684080   a7030 busybox
 643224     772   12128  656124   a02fc busybox_CHEAT
   6547     428    3260   10235    27fb busybox_SHARED
 660817    1128   12152  674097   a4931 busybox_SHARED2
 643224     772   12128  656124   a02fc libCHEAT.so
 909619    1076   12108  922803   e14b3 libLIBRARY.so

Basically, I want to make GC collected, smaller libLIBRARY.so.
libCHEAT.so is a result of that attempt.
I'd say it's *too* small. It's broken, and as a result busybox_SHARED2
is (a) not small at all [should be roughly like busybox_SHARED]
and (b) broken too.

Full set of needed *.o and *.a files, together with shell script
which does everything described above, can be downloaded here:

http://busybox.net/~vda/ld_testcase.tar.bz2

I ma also attaching shell script to this message.

Versions used:
$ ld -v
GNU ld (GNU Binutils) 2.18
$ gcc -v
...
gcc version 4.2.1

What I am doing wrong? Is it at all possible to make it work?
--
vda

Attachment: 0cheat.sh
Description: application/shellscript


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]