Skip to content

Commit 4e2d707

Browse files
committed
C backend: fix #858 - internal structure size mismatch due to padding differences
A structure which decreases its alignment (using FIELD = n) is annotated with both the "packed" and the "alignment(n)" attributes. That leads to a problem if such a structure is nested inside another structure that uses a greater alignment than the child structure. In that case the field representing the nested structure is aligned with the same properties as the nested structure. So the alignment has to be increased explicitly if the nested structure is packed with a smaller alignment than the parent structure. That is accomplished by adding the "alignment(n)" attribute to those fields.
1 parent 5ee8f12 commit 4e2d707

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/compiler/ir-hlc.bas

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,23 @@ private sub hEmitStructWithFields( byval s as FBSYMBOL ptr )
10381038
ln += " __attribute__((packed, aligned(" + str( align ) + ")))"
10391039
end if
10401040
end if
1041+
1042+
'' The alignment of nested structures which are packed with a
1043+
'' smaller alignment than the natural or specified alignment of the
1044+
'' parent structure has to be specified explicitly,
1045+
'' otherwise the field will be packed too.
1046+
if ( align <= 0 orelse skip ) then
1047+
if( typeGet( dtype ) = FB_DATATYPE_STRUCT ) then
1048+
var effectivealign = typeCalcNaturalAlign( dtype, subtype )
1049+
if ( align > 0 andalso align < effectivealign ) then
1050+
effectivealign = align
1051+
end if
1052+
var fieldudtalign = symbGetUDTAlign( subtype )
1053+
if( fieldudtalign > 0 andalso effectivealign > fieldudtalign ) then
1054+
ln += " __attribute__((aligned(" + str( effectivealign ) + ")))"
1055+
end if
1056+
end if
1057+
end if
10411058

10421059
ln += ";"
10431060
hWriteLine( ln, TRUE )

0 commit comments

Comments
 (0)