www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - __attribute__((used)) and druntime

reply "w0rp" <devw0rp gmail.com> writes:
I've been looking into compiling smaller executables, and the 
trick I learned from watching sessions from the last DConf is to 
use the KEEP symbol in a linker script with something like 
gc-sections. So at the compilation stage, symbols are put in 
different sections, a linker script marks some symbols to be kept 
if they are not referenced, and then unreferenced symbols are 
removed, resulting in sometimes very small executables.

The problem is that if you don't KEEP some symbols for druntime, 
or in your own program, you can run into errors because symbols 
which weren't apparently referenced now get used and your program 
breaks. I was just thinking, "There must be a GCC extension for 
doing this without a linker script," I *think* there might be.

 From what I just read, __attribute__((used)) in GCC should do the 
job, and I think it should be usable from GDC with its attribute 
pragmas. I believe there's also an attribute for putting symbols 
in particular sections. I'm wondering, supposing I'm not 
completely wrong and this actually works, would it behoove us to 
add some version() blocks for GDC (Also LDC?) in druntime and 
flag all of the symbols like this appropriately, so that creating 
executables with gc-sections will Just Work in some cases without 
a linker script?

What do people think? Am I on to something, or I am just making 
fanciful ideas which won't work?
Apr 11 2015
next sibling parent reply "David Nadlinger" <code klickverbot.at> writes:
On Saturday, 11 April 2015 at 15:35:47 UTC, w0rp wrote:
 […] (Also LDC?) in druntime and flag all of the symbols like 
 this appropriately, so that creating executables with 
 gc-sections will Just Work in some cases without a linker 
 script?
LDC is shipping with --gc-sections enabled by default since a couple of releases, without a custom linker script. — David
Apr 11 2015
parent "w0rp" <devw0rp gmail.com> writes:
On Saturday, 11 April 2015 at 15:59:53 UTC, David Nadlinger wrote:
 On Saturday, 11 April 2015 at 15:35:47 UTC, w0rp wrote:
 […] (Also LDC?) in druntime and flag all of the symbols like 
 this appropriately, so that creating executables with 
 gc-sections will Just Work in some cases without a linker 
 script?
LDC is shipping with --gc-sections enabled by default since a couple of releases, without a custom linker script. — David
Ah. I was also wondering if this was now essentially a solved problem.
Apr 11 2015
prev sibling next sibling parent "Jens Bauer" <doctor who.no> writes:
On Saturday, 11 April 2015 at 15:35:47 UTC, w0rp wrote:
 From what I just read, __attribute__((used)) in GCC should do 
 the job, and I think it should be usable from GDC with its 
 attribute pragmas.
I wouldn't mind having this functionality. For a long time, I've wanted it in C in those cases where I do not have a linker-script or do not have access to a linker-script. In addition to the above, I think it would also be beneficial for those who write hardware/driver libraries for microcontrollers: Just drop in the files that you want to use, then they're linked when found. One could even just work with stand-alone object files marked 'used'.
Apr 11 2015
prev sibling parent "Mike" <none none.com> writes:
On Saturday, 11 April 2015 at 15:35:47 UTC, w0rp wrote:
 I've been looking into compiling smaller executables, and the 
 trick I learned from watching sessions from the last DConf is 
 to use the KEEP symbol in a linker script with something like 
 gc-sections. So at the compilation stage, symbols are put in 
 different sections, a linker script marks some symbols to be 
 kept if they are not referenced, and then unreferenced symbols 
 are removed, resulting in sometimes very small executables.

 The problem is that if you don't KEEP some symbols for 
 druntime, or in your own program, you can run into errors 
 because symbols which weren't apparently referenced now get 
 used and your program breaks. I was just thinking, "There must 
 be a GCC extension for doing this without a linker script," I 
 *think* there might be.

 From what I just read, __attribute__((used)) in GCC should do 
 the job, and I think it should be usable from GDC with its 
 attribute pragmas. I believe there's also an attribute for 
 putting symbols in particular sections. I'm wondering, 
 supposing I'm not completely wrong and this actually works, 
 would it behoove us to add some version() blocks for GDC (Also 
 LDC?) in druntime and flag all of the symbols like this 
 appropriately, so that creating executables with gc-sections 
 will Just Work in some cases without a linker script?

 What do people think? Am I on to something, or I am just making 
 fanciful ideas which won't work?
I tried to elicit some direction on this in the past, but I wasn't able to get very far. The following forum threads may interest you: "Dead code removal" http://forum.dlang.org/post/fqocnakibszciyptihpi forum.dlang.org "--gc-section and GDC] http://forum.dlang.org/post/cqzazaqxpwezignixuds forum.dlang.org "Difference in perpective between LDC and GDC" http://forum.dlang.org/post/xuosrmyumtitahinzrfi forum.dlang.org I get the impression from those discussions that the binutils implementation for D is incomplete but it's still not clear to me whether dead code removal, or retainining "used" code, is a job for the compiler or the linker. It may require some orchestration between the two. I proposed an idea to move declarations for certain runtime hooks out of the compiler and into druntime as *.di files. That would allow for features of the runtime to be decorated in the platform's port with "used", "weak", "section", etc., but although I proposed it, I'm not convinced it's the way forward. You can see the thread here: http://forum.dlang.org/post/psssnzurlzeqeneagora forum.dlang.org Mike
Apr 13 2015