Removing dead code (GCC/LD)
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Removing dead code (GCC/LD)
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)?
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.
Re: Removing dead code (GCC/LD)
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.
I'll wait for Solar to come along and confirm before I would be confident in this though.
Re: Removing dead code (GCC/LD)
Confirmed.JackScott wrote:I'll wait for Solar to come along and confirm before I would be confident in this though.
Every good solution is obvious once you've found it.
Re: Removing dead code (GCC/LD)
*pulls air, tennis player style*Solar wrote:Confirmed.
- AndrewAPrice
- Member
- Posts: 2299
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
Re: Removing dead code (GCC/LD)
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.
Re: Removing dead code (GCC/LD)
--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.
Re: Removing dead code (GCC/LD)
lolSolar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
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
Re: Removing dead code (GCC/LD)
I was wondering if debug information is even important in your kernel if you're not attempting to use GDB or something?Solar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Re: Removing dead code (GCC/LD)
There is some information about the TPU file format on wotsit.org.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.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...
Re: Removing dead code (GCC/LD)
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.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.
JAL
Re: Removing dead code (GCC/LD)
I use the DWARF debug_frames section to implement cross-platform backtracing.Creature wrote:I was wondering if debug information is even important in your kernel if you're not attempting to use GDB or something?Solar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
-
- Member
- Posts: 524
- Joined: Sun Nov 09, 2008 2:55 am
- Location: Pennsylvania, USA
Re: Removing dead code (GCC/LD)
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.Creature wrote:I was wondering if debug information is even important in your kernel if you're not attempting to use GDB or something?Solar wrote:--strip-all is a completely different feature - it removes symbol (i.e., debug) information from your binary.
Re: Removing dead code (GCC/LD)
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.
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.