This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How do I guarantee data structure alignment equivalence between CCS/GCC and LLVM/MSVC?

I am maintaining a C6727-based product which involves streaming data from a host PC to the DSP memory space. This data may include arbitrarily complex C/C++ data structures. I must guarantee that the data structures which are generated and initialized on the host PC match the data structures which the compiled C6727 instructions expect to receive - for example, all data member alignment must match across the two platforms. Possible host-side compilers include GCC, LLVM/clang, and MSVC.

I know that the CCS compiler uses 8-byte alignment, so we have some wrapping code around the definitions of all data structures which may be transmitted to the DSP. This wrapping code sets #pragma pack(push,8) / #pragma pack(pop) around each structure. This seems to work just fine for ensuring 8-byte alignment of the structures.

In addition to the 8-byte packing, this wrapping code also includes #pragma ms_struct on / #pragma ms_struct off when using GCC or LLVM/clang. Here is some background on that pragma according to the GCC documentation:

"If packed is used on a structure, or if bit-fields are used it may be that the Microsoft ABI packs them differently than GCC normally packs them."
https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html

Since I inherited this code, I have to assume that the reason this pragma is used is that the CCS compiler follows the same packing rules as MSVC.

As of Xcode 6, however, clang has added a warning about use of ms_struct with data structures that include base structures or virtual functions:

#pragma ms_struct can not be used with dynamic classes or structures
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20140224/100153.html

Apparently, although this warning was only recently added, GCC and LLVM/clang do not, and never have, guaranteed that data alignment for data structures that include either base structures or virtual functions will be equivalent with MSVC, even when using the ms_struct pragma.

My questions are:

1) Am I correct that the CCS compiler follows the same alignment rules as MSVC with #pragma pack(8)?
2) Does the CCS compiler similarly guarantee identical data alignment with MSVC for data structures with base structures and/or virtual functions?
3) Is there any way to guarantee that such structures will be compatible (i.e. identical) between LLVM/clang and the CCS compiler?

  • Moving this from the C6000 Single Core DSP Forum (for device related questions) to the TI C/C++ Compiler Forum (for compiler related questions).
  • Rob Majors said:
    I must guarantee that the data structures which are generated and initialized on the host PC match the data structures which the compiled C6727 instructions expect to receive

    That is not a good idea.  See this FAQ (not from TI) for one take on this.  That said, there is also this ...

    Rob Majors said:
    I inherited this code

    So my warnings to not work this way are useless to you.  I only hope that others benefit from your experience.

    Generally speaking, my answer is that you are on your own. We do no verification or testing to insure that data layout within structures matches that of any other C compiler.  And that includes MSVC and LLVM/clang compilers.  I think #pragma pack(8) gets you most of what you need.  But it is up to you to verify that.

    Thanks and regards,

    -George