By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

skip compilation of unused functions

P: n/a
Hello,

I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is
there any way to throw them out of the final executable? Is there maybe
a program that cuts them away from the source code of the dietlibc?
I am using gcc.

Joris
Aug 15 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Joris Dolderer <no-spamwrote:
I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is
there any way to throw them out of the final executable? Is there maybe
a program that cuts them away from the source code of the dietlibc?
I am using gcc.
Sorry, but that's nothing related to C (or compiling) but solely
depends on how clever the linker is that assembles the final pro-
gram from the object files and the libraries. Perhaps it already
does what you want automatically, especially when you do static
linking. But you will have to ask in some group where the inner
workings of the linker you use are on-topic

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 15 '07 #2

P: n/a
"Joris Dolderer" <no-spamwrote in message
news:46**********************@newsspool3.arcor-online.net...
Hello,

I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is there
any way to throw them out of the final executable? Is there maybe a
program that cuts them away from the source code of the dietlibc?
I am using gcc.
Linking is off-topic on clc.

<OT>
Any decent, modern linker will do exactly what you ask when statically
linking. If yours doesn't, replace it. GCC just uses whatever your
platform's standard linker is, so you'll need to ask for details on a
newsgroup specific to your platform if you're not sure how smart your linker
is.
</OT>

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Aug 15 '07 #3

P: n/a
Stephen Sprunk wrote:
Linking is off-topic on clc.
Which is the appropriate newsgroup
for discussing what the C standard says about linking?

--
pete
Aug 15 '07 #4

P: n/a
pete said:
Stephen Sprunk wrote:
>Linking is off-topic on clc.

Which is the appropriate newsgroup
for discussing what the C standard says about linking?
Well, of course it's comp.lang.c - but the C Standard actually says
precious little about linking. At best, it's a nod towards the
existence of linkers as an integral part of the build process in
compiler-based implementations.

The nitty-gritty of linkers - how to run them and how to tweak them - is
an inherently implementation-specific subject, best dealt with by
newsgroups devoted to the implementations concerned.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 15 '07 #5

P: n/a
"Stephen Sprunk" wrote:
>"Joris Dolderer" wrote:
>I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is there
any way to throw them out of the final executable? Is there maybe a
program that cuts them away from the source code of the dietlibc?
...
Any decent, modern linker will do exactly what you ask when statically
linking. If yours doesn't, replace it.
Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
All object code generated from the compilation of a single source file
is "atomic" as far as the linker is concerned, and must be included in
its totality in the final executable if a single reference is resolved
from that object code.
And, of course, a single file may contain more than one function, and
some of them may not be used.
The only way I know to force a "vanilla" linker to do this, is by
splitting the source code into one file per function before creating
the library. (I had to do some maintenance work on a project that did
just that, and the huge number of source files turned it into an
unmanageable mess.)
Even then, global variables can still create unwanted dependencies
between object modules.

To the OP: There are code analysis tool (I am using PC-Lint) that will
provide a list of unused functions and variables. It may be possible
to use it to drive other tools to remove these unused functions from
the source files (or surround them with #ifdef...#endif,) creating a
customized version of the library that includes no more than what is
actually used. (Hopefully no less either, it may be difficult to know
if a function is called indirectly.)

The details would be, of course, platform specific.

Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Aug 16 '07 #6

P: n/a
Roberto Waltman wrote:
<snip>
Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
<snip>
A counter-example: IAR's xlink does function-level linking. If I
remember correctly, Microsoft's cl has a switch that allows objects to
be linked with function granularity, too.
I suppose, logically function-level linking is possible and not even
that hard to implement as long as the compiler doesn't generate
cross-function jumps for the sake of size optimization.
-- Ark
Aug 16 '07 #7

P: n/a
Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
i think the units are not object files but named sections like .text .bss
etc withing the object file. compilers like TASKING genererate a seperate
section for every function in an object file.

All object code generated from the compilation of a single source file
is "atomic" as far as the linker is concerned, and must be included in
its totality in the final executable if a single reference is resolved
from that object code.
And, of course, a single file may contain more than one function, and
some of them may not be used.
The only way I know to force a "vanilla" linker to do this, is by
splitting the source code into one file per function before creating
the library. (I had to do some maintenance work on a project that did
just that, and the huge number of source files turned it into an
unmanageable mess.)
Even then, global variables can still create unwanted dependencies
between object modules.

To the OP: There are code analysis tool (I am using PC-Lint) that will
provide a list of unused functions and variables. It may be possible
to use it to drive other tools to remove these unused functions from
the source files (or surround them with #ifdef...#endif,) creating a
customized version of the library that includes no more than what is
actually used. (Hopefully no less either, it may be difficult to know
if a function is called indirectly.)
In Code Warrior IDE, the linker gives a list of functions that do not have
references and names them as UNUSED. But it does not remove them. It can be
done manually by the developer.

Aug 16 '07 #8

P: n/a
Roberto Waltman <us****@rwaltman.netwrites:
"Stephen Sprunk" wrote:
>>"Joris Dolderer" wrote:
>>I want to compile and link a program statically against the dietlibc. Of
course, the dietlibc includes functions my program never needs. Is there
any way to throw them out of the final executable? Is there maybe a
program that cuts them away from the source code of the dietlibc?
...
Any decent, modern linker will do exactly what you ask when statically
linking. If yours doesn't, replace it.

Not exactly that. In every C environment I have worked on, the
granularity of the units that the linker can extract from a library is
quite coarse: "translation units"
<OT>The gcc tools on my system have options that allow a linking
granularity of a "section". This, together with an option that make
the compiler put each function into a separate section would give the
desired result. Of course, it may not work on the OP's platform and
how to do it requires a question in a group aimed at the OP's tool
set</OT>

--
Ben.
Aug 16 '07 #9

P: n/a
On 15 Aug, 23:23, pete <pfil...@mindspring.comwrote:
Stephen Sprunk wrote:
Linking is off-topic on clc.

Which is the appropriate newsgroup
for discussing what the C standard says about linking?

--
pete
I found, there is almost none that you can ask such questions. You
could try comp.unix.programmer, linux.development.apps but these are
too generic for linking. Might try gnu.gcc.help; even though you
usually get an answer, what you ask is most likely off-topic there,
too. Might try the binutils list, but then that's not a newbie list
and you might get ignored.

The best thing to do is read the book linkers & loaders, and read the
GCC manuals, especially as and ld manuals. They come as pdfs. If your
linker is not ld, good luck.

Thanks,
Bahadir

Aug 16 '07 #10

P: n/a
Bahadir <Bi*************@gmail.comwrites:
[...]
The best thing to do is read the book linkers & loaders, and read the
GCC manuals, especially as and ld manuals. They come as pdfs. If your
linker is not ld, good luck.
That would be the book "Linkers and Loaders" by John R. Levine
(moderator of comp.compilers), right? To be clear, that book is
available only on paper as far as I know.

The GNU as and ld manuals are primarily available in "info" format
("info as" or "info ld" if the command is available), but I suppose
you can also get them in pdf.

(This isn't really topical; I'm just clarifying the redirection.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 16 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.