Removing dead code (GCC/LD)

Programming, for all ages and all languages.
Post Reply
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Removing dead code (GCC/LD)

Post by AndrewAPrice »

I have made a rather huge user library, and when I statically link my programs to it each one becomes at least 500kb in size. The thing is, each program only uses a very small subset of the library, so that's a good 400kb of dead code. This becomes a problem when I try to fit my OS onto a floppy image.

I'm using GCC (and GDC) for compiling, and LD for linking. I'm wondering how (if) I can enable dead code elimination, and would it be a linker switch (since only the linker has an overview of the entire project)?
My OS is Perception.
User avatar
JackScott
Member
Member
Posts: 1031
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Contact:

Re: Removing dead code (GCC/LD)

Post by JackScott »

How large are the individual object (and thus C) files making up the library? IIRC, the linker can only include the whole .o file, not a subset of it. So the smaller the object file (such as making the C file only have one function in it), the less the linker has to include.

I'll wait for Solar to come along and confirm before I would be confident in this though.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Removing dead code (GCC/LD)

Post by Solar »

JackScott wrote:I'll wait for Solar to come along and confirm before I would be confident in this though.
Confirmed.
Every good solution is obvious once you've found it.
User avatar
JackScott
Member
Member
Posts: 1031
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Contact:

Re: Removing dead code (GCC/LD)

Post by JackScott »

Solar wrote:Confirmed.
*pulls air, tennis player style*
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: Removing dead code (GCC/LD)

Post by AndrewAPrice »

Thanks, I just found --strip-all and --gc-sections. Instantly it stripped my kernel from 412kb to 64kb without corrupting anything so far!
My OS is Perception.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Removing dead code (GCC/LD)

Post by Solar »

--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
Every good solution is obvious once you've found it.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Removing dead code (GCC/LD)

Post by jal »

Solar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
lol

OT: I believe the Turbo Pascal linker did not include functions and procedures include in modules that weren't used. I've always failed to understand why a multiple-pass linker could not do the same for C, but perhaps that's just my limited indepth knowledge...


JAL
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Removing dead code (GCC/LD)

Post by Creature »

Solar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
I was wondering if debug information is even important in your kernel if you're not attempting to use GDB or something?
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Removing dead code (GCC/LD)

Post by qw »

berkus wrote:
jal wrote:OT: I believe the Turbo Pascal linker did not include functions and procedures include in modules that weren't used. I've always failed to understand why a multiple-pass linker could not do the same for C, but perhaps that's just my limited indepth knowledge...
It indeed could. I still fail to understand what analysis technique does LD miss that it cannot do the same. Need to look at a way Pascal units are organized, probably they contain way more metadata than ELF .o files.
There is some information about the TPU file format on wotsit.org.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Removing dead code (GCC/LD)

Post by jal »

berkus wrote:It indeed could. I still fail to understand what analysis technique does LD miss that it cannot do the same. Need to look at a way Pascal units are organized, probably they contain way more metadata than ELF .o files.
Well, since Pascal has the 'define first, use later' policy, there's no way to use a procedure before it is defined. And since declaring happens in the header of the module file, and the implementation necessarily in the same module, once you have included a module, you know the function exists. So TP (or any Pascal compiler) can issue unknown reference errors while compiling instead of linking. This as opposed to C, which allows declaration without definition, so the linker has to do all the works. ld would need at least one additional pass to be able to exclude certain code, but that should be relatively easy, compared to all the other complications of linking.


JAL
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Removing dead code (GCC/LD)

Post by JamesM »

Creature wrote:
Solar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
I was wondering if debug information is even important in your kernel if you're not attempting to use GDB or something?
I use the DWARF debug_frames section to implement cross-platform backtracing.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Removing dead code (GCC/LD)

Post by JohnnyTheDon »

Creature wrote:
Solar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
I was wondering if debug information is even important in your kernel if you're not attempting to use GDB or something?
Debugging isn't the only use for symbols. If you use the symbol table for locating functions (for example in a shared library) you need symbols. IIRC '--strip-debug' will remove your debug info and leave the symbol table intact. I've found that gcc/ld export WAY too many symbols though, which is important if you are making something closed source and you don't want people knowing the names and locations of all you functions.
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Re: Removing dead code (GCC/LD)

Post by leledumbo »

Free Pascal produces assembly files for each functions / procedures and variables, assembled them one by one so that each has its own .o file, then use ar to create a static lib. So, when calling ld it can simulate the smartlinking (or dead code elimination) technique.

This applies to systems where the internal linker not yet exists. The internal linker (currently, only Win32/64 has it) already knows how to smartlink, thus removing the need to create static lib with a bunch of .o files.
Post Reply