Skip to content

Commit

Permalink
Merge pull request #7243 from WalterBright/fix7997
Browse files Browse the repository at this point in the history
fix Issue 7997 - Optlink issues 'Index Range' error with static zero …
  • Loading branch information
andralex committed Oct 26, 2017
2 parents f18bfe9 + d4883f2 commit 4a62e84
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/ddmd/toobj.d
Expand Up @@ -938,25 +938,31 @@ void toObjFile(Dsymbol ds, bool multiobj)
} while (parent);
s.Sfl = FLdata;

if (!sz && vd.type.toBasetype().ty != Tsarray)
assert(0); // this shouldn't be possible

scope dtb = new DtBuilder();
if (config.objfmt == OBJ_MACH && global.params.is64bit && (s.Stype.Tty & mTYLINK) == mTYthread)
{
scope dtb = new DtBuilder();
tlsToDt(vd, s, dtb);
s.Sdt = dtb.finish();
tlsToDt(vd, s, sz, dtb);
}
else if (!sz)
{
/* Give it a byte of data
* so we can take the 'address' of this symbol
* and avoid problematic behavior of object file format
*/
dtb.nzeros(1);
}

else if (vd._init)
{
scope dtb = new DtBuilder();
initializerToDt(vd, dtb);
s.Sdt = dtb.finish();
}
else
{
scope dtb = new DtBuilder();
Type_toDt(vd.type, dtb);
s.Sdt = dtb.finish();
}
s.Sdt = dtb.finish();

// See if we can convert a comdat to a comdef,
// which saves on exe file space.
Expand All @@ -969,17 +975,11 @@ void toObjFile(Dsymbol ds, bool multiobj)
dt2common(&s.Sdt);
}

if (!sz && vd.type.toBasetype().ty != Tsarray)
assert(0); // this shouldn't be possible

if (sz || objmod.allowZeroSize())
{
outdata(s);
if (vd.type.isMutable() || !vd._init)
write_pointers(vd.type, s, 0);
if (vd.isExport())
objmod.export_symbol(s, 0);
}
outdata(s);
if (vd.type.isMutable() || !vd._init)
write_pointers(vd.type, s, 0);
if (vd.isExport())
objmod.export_symbol(s, 0);
}

override void visit(EnumDeclaration ed)
Expand Down Expand Up @@ -1230,18 +1230,22 @@ void toObjFile(Dsymbol ds, bool multiobj)
* size_t offset;
* }
*
* Input:
* vd the variable declaration for the symbol
* s the symbol to output
* Params:
* vd = the variable declaration for the symbol
* s = the backend Symbol corresponsing to vd
* sz = data size of s
* dtb = where to put the data
*/
static void tlsToDt(VarDeclaration vd, Symbol *s, DtBuilder dtb)
static void tlsToDt(VarDeclaration vd, Symbol *s, uint sz, DtBuilder dtb)
{
assert(config.objfmt == OBJ_MACH && global.params.is64bit && (s.Stype.Tty & mTYLINK) == mTYthread);

Symbol *tlvInit = createTLVDataSymbol(vd, s);
scope tlvInitDtb = new DtBuilder();

if (vd._init)
if (sz == 0)
tlvInitDtb.nzeros(1);
else if (vd._init)
initializerToDt(vd, tlvInitDtb);
else
Type_toDt(vd.type, tlvInitDtb);
Expand Down
10 changes: 10 additions & 0 deletions test/runnable/test42.d
Expand Up @@ -6101,6 +6101,15 @@ void test252()
assert(0);
}

/***************************************************/
// https://issues.dlang.org/show_bug.cgi?id=7997

void test7997()
{
__gshared int[0] foos;
foreach (f; foos) {}
}

/***************************************************/

int main()
Expand Down Expand Up @@ -6401,6 +6410,7 @@ int main()
test16027();
test16530();
test252();
test7997();

writefln("Success");
return 0;
Expand Down

0 comments on commit 4a62e84

Please sign in to comment.