This is the mail archive of the 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.

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 -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, -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 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 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 -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

A bit of reasons what I'm trying to achieve:
$ 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
 909619    1076   12108  922803   e14b3

Basically, I want to make GC collected, smaller 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:

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?

Description: application/shellscript

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