GCC Hwoto tell the objects are not aligned - Linux

This is a discussion on GCC Hwoto tell the objects are not aligned - Linux ; The following testprogram has some integers (u1, u2) intentionally misaligned. When passing references to these integers into a function by reference an access in the function causes an alignment trap on IA64. Ok, the variables should be better aligned. But ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: GCC Hwoto tell the objects are not aligned

  1. GCC Hwoto tell the objects are not aligned

    The following testprogram has some integers (u1, u2) intentionally
    misaligned. When passing
    references to these integers into a function by reference an access in
    the function causes an
    alignment trap on IA64.

    Ok, the variables should be better aligned.
    But due to requirements from the external protocol, the cannot be
    aligned.

    Is there any way to tell gcc that the uint32_t references by p1 and p2
    are possible not aligned so that
    gcc generates more secure code for TestCmp ?

    Placing an __attribute__(align(1))) after the paramwter definition
    leads to an syntax error, so this try did not succeed.

    Klaus

    void TestCmp (uint32_t *p1, uint32_t *p2)
    {
    fprintf (stdout, "%d / %d\n", *p1, *p2 );
    if ( *p1 == *p2 ) { fprintf (stdout, "are equal\n" ); };
    }

    int main ( void )
    {
    #pragma pack(1)
    typedef struct {
    char ch;
    uint32_t u1; // force misalignment
    uint32_t u2;
    } test_t;

    test_t tt = { ' ', 0, 0 };

    TestCmp( &tt.u1, &tt.u2 );
    }

    IA alignment tests:
    TypeTest(29914): unaligned access to 0x60000fffffffa171,
    ip=0x4000000000000b91
    TypeTest(29914): unaligned access to 0x60000fffffffa175,
    ip=0x4000000000000ba0
    0 / 0
    TypeTest(29914): unaligned access to 0x60000fffffffa171,
    ip=0x4000000000000bb1
    TypeTest(29914): unaligned access to 0x60000fffffffa175,
    ip=0x4000000000000bc0
    are equal


  2. Re: GCC Hwoto tell the objects are not aligned

    > The following testprogram has some integers (u1, u2) intentionally
    > misaligned. When passing
    > references to these integers into a function by reference an access in
    > the function causes an
    > alignment trap on IA64.
    >
    > Ok, the variables should be better aligned.
    > But due to requirements from the external protocol, the cannot be
    > aligned.


    Then you cannot require a C compiler to generate good code for any
    reference to such a variable except via something like "(char *)&ptr->u1".

    Some compilers have extensions to support unknown alignment. For instance,
    the MIPS R3000 architecture has hardware support for arbitrary-aligned
    integers (instructions: lwl, lwr, swl, swr), and it is simple,
    and it costs no more than twice as much as a naturally-aligned reference.
    So some compilers have a command-line option to request "Use unaligned
    access wherever you cannot prove that the data is aligned."

    For portable access to possibly-unaligned integers, then you must
    code it explicitly yourself. Functions such as
    int get32(char const *);
    set32(char *, int);
    are commonly used.

    In C++ it is possible with some effort (such as clever jiggering of
    namespaces and types, overloading of '->', etc.) to insert the right
    calls "automatically". But based on your original question and how
    you ask it, then probably this effort is too complex for your use.

    --

  3. Re: GCC Hwoto tell the objects are not aligned

    On Apr 5, 2:08 pm, John Reiser wrote:

    > In C++ it is possible with some effort (such as clever jiggering of
    > namespaces and types, overloading of '->', etc.) to insert the right
    > calls "automatically". But based on your original question and how
    > you ask it, then probably this effort is too complex for your use.


    You can cast through a packed union in GCC. It's still ugly. See, for
    example:
    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35653#c12

    DS

+ Reply to Thread