C – Packed struct – different behavior in different compilers

I’ve wrote a function that writes file-info into another file (in bin-format).
I’ve built it for WINDOWS executable, in two different environments:
eclipse, using mingw gcc:

GCC: (MinGW.org GCC-6.3.0-1) 6.3.0

linux, using cross-compiler (x86_64-w64-mingw32-gcc-win32):

GCC: (GNU) 10-win32 20220113

The file info is a packed struct, sized 128bytes, as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#define STR_SIZE 32
typedef char string_t[STR_SIZE];
typedef union
{
struct
{
uint32_t a : 15;
uint32_t b : 1;
uint32_t c : 8;
uint32_t d : 8;
} __attribute__((packed)) fields;
uint32_t raw;
} general_t;
typedef struct
{
uint8_t hour;
uint8_t minutes;
uint8_t day;
uint8_t month;
uint16_t year;
} __attribute__((packed)) full_date_t;
typedef struct
{
general_t general;
uint32_t size;
} __attribute__((packed)) info_t;
typedef struct
{ /* starting from offset 4 */
full_date_t date; /* 6 bytes - offset 4: 9 */
string_t my_str; /* 32 bytes - offset 10:41 */
info_t v1; /* 8 bytes - offset 42:49 */
info_t v2; /* 8 bytes - offset 50:57 */
info_t v3; /* 8 bytes - offset 58:65 */
info_t v4; /* 8 bytes - offset 66:73 */
info_t v5; /* 8 bytes - offset 74:81 */
uint32_t crc; /* 4 bytes - offset 82:85 */
} __attribute__((packed)) fields_t;
typedef union
{
struct
{
uint32_t crc1;
fields_t fields;
uint8_t reserved[TOTAL_SIZE - sizeof(fields_t) - sizeof(uint32_t)];
} __attribute__((packed)) fields;
uint8_t raw[TOTAL_SIZE];
} full_info_t;
</code>
<code>#define STR_SIZE 32 typedef char string_t[STR_SIZE]; typedef union { struct { uint32_t a : 15; uint32_t b : 1; uint32_t c : 8; uint32_t d : 8; } __attribute__((packed)) fields; uint32_t raw; } general_t; typedef struct { uint8_t hour; uint8_t minutes; uint8_t day; uint8_t month; uint16_t year; } __attribute__((packed)) full_date_t; typedef struct { general_t general; uint32_t size; } __attribute__((packed)) info_t; typedef struct { /* starting from offset 4 */ full_date_t date; /* 6 bytes - offset 4: 9 */ string_t my_str; /* 32 bytes - offset 10:41 */ info_t v1; /* 8 bytes - offset 42:49 */ info_t v2; /* 8 bytes - offset 50:57 */ info_t v3; /* 8 bytes - offset 58:65 */ info_t v4; /* 8 bytes - offset 66:73 */ info_t v5; /* 8 bytes - offset 74:81 */ uint32_t crc; /* 4 bytes - offset 82:85 */ } __attribute__((packed)) fields_t; typedef union { struct { uint32_t crc1; fields_t fields; uint8_t reserved[TOTAL_SIZE - sizeof(fields_t) - sizeof(uint32_t)]; } __attribute__((packed)) fields; uint8_t raw[TOTAL_SIZE]; } full_info_t; </code>
#define STR_SIZE            32
typedef char string_t[STR_SIZE];

typedef union
{
    struct
    {
        uint32_t    a   : 15;
        uint32_t    b   : 1;
        uint32_t    c   : 8;
        uint32_t    d   : 8;
    } __attribute__((packed)) fields;
    uint32_t raw;
} general_t;

typedef struct
{
    uint8_t     hour;
    uint8_t     minutes;
    uint8_t     day;
    uint8_t     month;
    uint16_t    year;
} __attribute__((packed)) full_date_t;

typedef struct
{
    general_t     general;
    uint32_t      size;
} __attribute__((packed)) info_t;

typedef struct 
{ /* starting from offset 4 */
    full_date_t    date;     /*  6 bytes - offset  4: 9 */
    string_t     my_str;    /* 32 bytes - offset 10:41 */
    info_t   v1;             /*  8 bytes - offset 42:49 */
    info_t   v2;             /*  8 bytes - offset 50:57 */
    info_t   v3;             /*  8 bytes - offset 58:65 */
    info_t   v4;             /*  8 bytes - offset 66:73 */
    info_t   v5;             /*  8 bytes - offset 74:81 */
    uint32_t crc;            /*  4 bytes - offset 82:85 */
} __attribute__((packed)) fields_t;

typedef union
{
    struct
    {
        uint32_t  crc1;
        fields_t  fields;
        uint8_t   reserved[TOTAL_SIZE - sizeof(fields_t) - sizeof(uint32_t)];
    } __attribute__((packed)) fields;
    uint8_t raw[TOTAL_SIZE];
} full_info_t;

After building the code, and running it to build the info-file, I get different results in the bin file (besides the date – I hardcoded it all to 0x12345678).
the last CRC field, inside the ‘fields_t’ type, is located differently:

on the left you can see the linux built result, and on the right the windows built result (both built for exe executable in windows).
The correct offset of the CRC, if the linux built executable, while the windows built adds 2 bytes of padding.

What can cause the difference?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật