----- Ursprüngliche Nachricht -----
Von: Grant Edwards
Gesendet am: 21 Jun 2010 16:54:37

>> If I understood the linker scripts correctly, the linker will put all
>> .text and .text.* compiler segments into the linker text segment.

> After the unreferenced .text.* sections are discarded, the remaining
> ones will all be placed into the .text section in the output file.

As I undestood you, the linker should do this for any (complete) object file
anyway, independently of the gc-sections setting.
Yet it doesn't. Any object file is put into the binary, whether referenced
or not.

>> So there are no empty segments to be discarded by -gc-sections.

>I'm not sure what "empty segments" you're talking about.

segments -> = sections. I think I got the 'empty' by browsing the ld
option description. Or i dedutcted it from your expression of
discarded unused content.

> Are you _sure_ you passed the -gc-sections flag to the linker?
no (see below)


> First, both -ffunction-sections and -gc-sections flags are commented
> out in your makefile.

I tried with and without. The version I posted was the last test (without both).

> Second, it's '--gc-sections' not '-gc-sections'

I tried both :)

> Third, if you uncomment them and add the second hyphen, you're still
> not passing --gc-sections flag to the linker, you're passing it to the
> compiler (which should have resulted in a error message).

> If you want to pass the --gc-sections flag to the linker via the gcc
> command line, you need to use either "-W,--gc-sections" or "-Xlinker 
> --gc-sections"

That's the point. (or nearly, according to the GC manual, it is -Wl,x (which 
works)

I read the ld documentation and missted the fact that the compiler gets
the options first, even if added to LDFLAGS.

Nevertheless, I don't understand why foo() is added to the binary by the
linker default, while it is not referenced and in its own object file.

With the use of -Wl, I come to this conclusion:

1) without --gc-sections, the linker keeps ALL code passed to it in an 
object file, no matter whether anything in an object file is referenced
or nothing at all.

2) with use of --gc-sections, the linker will ignore any object files content, 
if
nothing in this object file is referenced from outside

3) using -ffunction-sections AND --gc-sections, the compiler will put any
function in its own .text.* (sub)section and the linker will discard any of
them which is not referenced, even if in the same object file as other,
referenced code.

Since 1) is the default, binary files may be unnecessary large by default.

Thanks for your clarifications. It makes life a bit easier for me
(I can skip some #ifdefs now in the modules), now that I know (I think) 
how it really works.

JMGross

Reply via email to