diff --git a/compiler/src/dmd/backend/elpicpie.d b/compiler/src/dmd/backend/elpicpie.d index 6ad189cafdd1..706cd0740986 100644 --- a/compiler/src/dmd/backend/elpicpie.d +++ b/compiler/src/dmd/backend/elpicpie.d @@ -46,12 +46,26 @@ elem * el_var(Symbol *s) if (config.exe & EX_posix) { if (config.flags3 & CFG3pie && - s.Stype.Tty & mTYthread) - return el_pievar(s); // Position Independent Executable + s.Stype.Tty & mTYthread && + (s.Sclass == SC.global || + s.Sclass == SC.static_ || + s.Sclass == SC.locstat)) + { + } + else + { + if (config.flags3 & CFG3pie && + s.Stype.Tty & mTYthread) + { + return el_pievar(s); // Position Independent Executable + } - if (config.flags3 & CFG3pic && - !tyfunc(s.ty())) - return el_picvar(s); // Position Independent Code + if (config.flags3 & CFG3pic && + !tyfunc(s.ty())) + { + return el_picvar(s); // Position Independent Code + } + } } if (config.exe & (EX_OSX | EX_OSX64)) @@ -128,7 +142,9 @@ else if (config.exe & EX_posix) Obj.refGOTsym(); elem *e1 = el_calloc(); e1.EV.Vsym = s; - if (s.Sclass == SC.static_ || s.Sclass == SC.locstat) + if (s.Sclass == SC.global || + s.Sclass == SC.static_ || + s.Sclass == SC.locstat) { e1.Eoper = OPrelconst; e1.Ety = TYnptr; @@ -739,7 +755,7 @@ private elem *el_pievar(Symbol *s) case SC.static_: case SC.locstat: case SC.global: - break; + assert(0); case SC.comdat: case SC.comdef: @@ -767,7 +783,7 @@ private elem *el_pievar(Symbol *s) case SC.static_: case SC.locstat: case SC.global: - break; + assert(0); case SC.comdat: case SC.comdef: diff --git a/compiler/test/runnable/issue24168.d b/compiler/test/runnable/issue24168.d new file mode 100644 index 000000000000..c21a35bc9d46 --- /dev/null +++ b/compiler/test/runnable/issue24168.d @@ -0,0 +1,31 @@ +/* +REQUIRED_ARGS: -fPIE +DISABLED: win32 win64 +*/ + +//https://issues.dlang.org/show_bug.cgi?id=24168 + +int i = 42; + +bool foo(ref int a) +{ + return a == 42; +} + +ref int bar() +{ + return i; +} + +bool baz() +{ + static int i = 42; + return foo(i); +} + +void main() +{ + assert(foo(i)); + assert(bar() == 42); + assert(baz()); +}