D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7083 - variables with static/private storage create global symbols
Summary: variables with static/private storage create global symbols
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other FreeBSD
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-08 17:28 UTC by Martin Nowak
Modified: 2022-03-03 15:43 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Martin Nowak 2011-12-08 17:28:27 UTC
----
module bug;

static
{
    __gshared int sa;
    extern(C) __gshared int sb;
}

private
{
    __gshared int pa;
    extern(C) __gshared int pb;
}

----
dmd -c bug.d
nm -c bug.o

output:
0000000000000008 B _D3bug2pai
0000000000000000 B _D3bug2sai
000000000000000c B pb
0000000000000004 B sb

expected output:
0000000000000008 b _D3bug2pai
0000000000000000 b _D3bug2sai
000000000000000c b pb
0000000000000004 b sb
----------------

Either static or private should provide the C's static semantic
of defining a local symbol, i.e. one that can only be accessed
from within the same object file. Probably only useful for
extern(C) as D declarations are scoped by the mangling but
for D it would still shrink the symbol table.
Comment 1 Walter Bright 2011-12-08 20:56:19 UTC
This is done deliberately as the code/data for one module can be distributed among several object files (unlike C) and the only way they can communicate is via a global name.
Comment 2 Martin Nowak 2013-03-27 13:07:58 UTC
It would be good to reconsider this for PIC code where every access to global symbols is indirect through the GOT table.

> as the code/data for one module can be distributed among several object files

Do you mean the multilib support?
Comment 3 Martin Nowak 2013-04-03 19:55:13 UTC
(In reply to comment #2)
> It would be good to reconsider this for PIC code where every access to global
> symbols is indirect through the GOT table.
> 
This could be achieved by changing the default visibility to hidden.
Similar to Windows only symbols with the export attribute would be visible in a shared library thus allowing link-time optimizations for non-exported symbols.

http://gcc.gnu.org/wiki/Visibility
Comment 4 kinke 2022-03-03 15:43:53 UTC
(In reply to Martin Nowak from comment #2)
> > as the code/data for one module can be distributed among several object files
> 
> Do you mean the multilib support?

```
private __gshared int something;

int getSomething(T)() { …; return something; }
```

If the (public) template is instantiated and emitted into another object file / binary, it still needs to be able to access the private symbol.