Thread overview
Alignment of symbol is not kept during linking
Aug 09
Kagamin
6 days ago
Kagamin
6 days ago
Yuxuan Shui
August 09
I'm trying to build LDC with dmd and Musl, but the result ldc will always crash. I track that down to an unalignment SIMD access to a global variable. Apparently C++ compiler thinks the variable should be aligned to 16 bytes, but it's only aligned to 8 bytes.

After some more digging, I find out dmd does try to do the right thing here. In the .o file, the offending symbol is indeed aligned to 16 bytes. But, after linking, that symbol got bumped 8 bytes for some reason.

I searched around, and there seems to be no way to specify alignment on symbol, so I don't think the linker is in the wrong here.

C compilers would not put variables like that in .data, instead they use SHN_COMMON, and specify alignment there. LDC will just put every symbol into their own section, which also supports alignment. dmd should probably do the same.
August 09
Clarifications:

On Thursday, 9 August 2018 at 13:30:38 UTC, Yuxuan Shui wrote:
> I'm trying to build LDC with dmd and Musl, but the result ldc will always crash. I track that down to an unalignment SIMD access to a global variable. Apparently C++ compiler thinks the variable should be aligned to 16 bytes, but it's only aligned to 8 bytes.

SIMD is generated by the C++ compiler, the unaligned read happens in C++ code.

The accessed variable is a __gshared global variable.


August 09
On Thursday, 9 August 2018 at 13:30:38 UTC, Yuxuan Shui wrote:
> I searched around, and there seems to be no way to specify alignment on symbol, so I don't think the linker is in the wrong here.

The alignment should be specified for section.
August 09
On Thursday, 9 August 2018 at 13:50:00 UTC, Kagamin wrote:
> On Thursday, 9 August 2018 at 13:30:38 UTC, Yuxuan Shui wrote:
>> I searched around, and there seems to be no way to specify alignment on symbol, so I don't think the linker is in the wrong here.
>
> The alignment should be specified for section.

The alignment is specified for the section, and it apparently kept. problem is individual symbols in the section don't have a alignment.
6 days ago
On Thursday, 9 August 2018 at 14:15:31 UTC, Yuxuan Shui wrote:
> The alignment is specified for the section, and it apparently kept. problem is individual symbols in the section don't have a alignment.

If the section is aligned at 16 bytes and the symbol is aligned at 16 bytes in the section, then the symbol will be aligned at 16 bytes, I don't thunk this can go wrong.
6 days ago
On Friday, 10 August 2018 at 08:46:46 UTC, Kagamin wrote:
> On Thursday, 9 August 2018 at 14:15:31 UTC, Yuxuan Shui wrote:
>> The alignment is specified for the section, and it apparently kept. problem is individual symbols in the section don't have a alignment.
>
> If the section is aligned at 16 bytes and the symbol is aligned at 16 bytes in the section, then the symbol will be aligned at 16 bytes, I don't thunk this can go wrong.

I think you are correct. And I finally found out what is the culprit: The section is not actually aligned.

Bug report: https://issues.dlang.org/show_bug.cgi?id=19148