www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alignment of symbol is not kept during linking

reply Yuxuan Shui <yshuiv7 gmail.com> writes:
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.
Aug 09 2018
next sibling parent Yuxuan Shui <yshuiv7 gmail.com> writes:
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.
Aug 09 2018
prev sibling parent reply Kagamin <spam here.lot> writes:
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.
Aug 09 2018
parent reply Yuxuan Shui <yshuiv7 gmail.com> writes:
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.
Aug 09 2018
parent reply Kagamin <spam here.lot> writes:
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.
Aug 10 2018
parent Yuxuan Shui <yshuiv7 gmail.com> writes:
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
Aug 10 2018