GCC Howto get structure listing - Linux

This is a discussion on GCC Howto get structure listing - Linux ; I'm looking for a way to get a listing of all structure fields in a C source with the size and the offset of the fields. Is such an output available from gcc? I did not find it until now. ...

+ Reply to Thread
Results 1 to 16 of 16

Thread: GCC Howto get structure listing

  1. GCC Howto get structure listing

    I'm looking for a way to get a listing of all structure fields in a C
    source with the size and the offset of the fields.

    Is such an output available from gcc? I did not find it until now.

    I know, I could check the assembler code, but this would be not very
    handy. What I'm targetting is (manually) aligning or checking
    structures definitions. Some of the are to complex or nested to count
    bytes by hand. (And as these structure are at least partially part of
    an external interface automatic alignment is no choice).

    Thanks for any hint
    Klaus

  2. Re: GCC Howto get structure listing

    Klaus Klingenbusch writes:
    > I'm looking for a way to get a listing of all structure fields in a C
    > source with the size and the offset of the fields.
    >
    > Is such an output available from gcc? I did not find it until now.
    >
    > I know, I could check the assembler code, but this would be not very
    > handy. What I'm targetting is (manually) aligning or checking
    > structures definitions. Some of the are to complex or nested to count
    > bytes by hand. (And as these structure are at least partially part of
    > an external interface automatic alignment is no choice).


    The easiest way to achieve this would be to define the structure with
    'suitable padding' to avoid misalignment. gcc will respect that[*],
    because otherwise, it couldn't compile Linux anymore, which would
    presumably too much of an issue even for the gcc-team's well-known
    desire to break third party code by changing the code-generator
    uselessly. __attribute__((aligned) could be helpful, too.
    [*] for C, that is. The g++-'band of maniacs' is an entirely
    different problem: For as long as these people have not
    managed to release at least three subsequent compilers without
    changing the language accepted by any of them, using C++ for
    anything serious with gcc as a compiler is basically a mistake.

  3. Re: GCC Howto get structure listing

    On 4 Apr., 10:35, Rainer Weikusat wrote:

    > The easiest way to achieve this would be to define the structure with
    > 'suitable padding' to avoid misalignment. gcc will respect that[*],
    > because otherwise, it couldn't compile Linux anymore, which would
    > presumably too much of an issue even for the gcc-team's well-known
    > desire to break third party code by changing the code-generator
    > uselessly. __attribute__((aligned) could be helpful, too.


    Thanks for the info. In the special case I cannot use automatic
    padding
    (by GCC) because some of the structures are to be specified as an
    external interface. I must pad them manually to avoid any unnotices
    change
    in future.

    I'm looking for a sort of listung like

    struct sss{
    char name[80];
    int32_t count;
    };


    structure sss listing
    fieldname size offset within structure requ.
    alignment
    name 80 0 1
    count 4 80 4


  4. Re: GCC Howto get structure listing


    > structure sss listing
    > fieldname size offset within structure requ.
    > alignment
    > name 80 0 1
    > count 4 80 4
    >


    Is
    man 3 offsetof
    of help?

  5. Re: GCC Howto get structure listing

    Klaus Klingenbusch writes:
    > On 4 Apr., 10:35, Rainer Weikusat wrote:
    >> The easiest way to achieve this would be to define the structure with
    >> 'suitable padding' to avoid misalignment. gcc will respect that[*],
    >> because otherwise, it couldn't compile Linux anymore, which would
    >> presumably too much of an issue even for the gcc-team's well-known
    >> desire to break third party code by changing the code-generator
    >> uselessly. __attribute__((aligned) could be helpful, too.

    >
    > Thanks for the info. In the special case I cannot use automatic
    > padding
    > (by GCC) because some of the structures are to be specified as an
    > external interface.


    You already wrote that. What makes you think that I didn't understand
    that because you didn't understand my answer to your question?

    Quoting from the gcc documentation:

    ,----
    | `aligned (ALIGNMENT)'
    | This attribute specifies a minimum alignment (in bytes) for
    | variables of the specified type. For example, the declarations:
    |
    | struct S { short f[3]; } __attribute__ ((aligned (8)));
    | typedef int more_aligned_int __attribute__ ((aligned (8)));
    |
    | force the compiler to insure (as far as it can) that each variable
    | whose type is `struct S' or `more_aligned_int' will be allocated
    | and aligned _at least_ on a 8-byte boundary.
    `----
    (5.23)

    [...]

    > struct sss{
    > char name[80];
    > int32_t count;
    > };


    Assuming the following definition:

    struct sss sss;

    &sss == &sss.name is required to be true (ISO/IEC 9899:1999, 6.7.2.1|13).
    Because the size of the first member is evenly divisible by four, the
    count field will be suitably aligned without any padding. As I already
    wrote, the Linux-kernel uses this method to define structures
    representing various network data formats, eg IP- or UDP-headers (=>
    /usr/include/linux/ip.h), it is therefore reasonably safe to assume
    that it will work and continue to work.

    You should probably check the ABI-definitions for any target
    platform(s) you care about, but for 'most cases' (all I personally
    know of), gcc defaults to natural alignment, ie allocate at addresses
    evenly divisible by the size of the type to allocate (for simple
    types, eg int).

  6. Re: GCC Howto get structure listing

    Rainer Weikusat writes:
    >> Thanks for the info. In the special case I cannot use automatic
    >> padding (by GCC) because some of the structures are to be specified as an
    >> external interface.


    [...]

    > Quoting from the gcc documentation:
    >
    > ,----


    [ alignment of types]

    > `----
    > (5.23)


    This was the wrong section. The correct one would be 'variable
    attributes': It is possible to specify an alignment for structure
    members, too, eg

    struct x {
    char tag[3];
    unint32_t v __attribute__ ((aligned (8)));
    };

    would insert suitable padding into the structure to cause v to be
    aligned on an 8-byte-boundary. I would nevertheless usually prefer

    struct x {
    char tag[3], pad[5];
    uint32_t v;
    }

    to achieve the same purpose (both depends on properties of the
    implementation)

  7. Re: GCC Howto get structure listing

    On 4 Apr., 14:42, Rainer Weikusat wrote:
    > would insert suitable padding into the structure to cause v to be
    > aligned on an 8-byte-boundary. I would nevertheless usually prefer
    >
    > * * * * struct x {
    > * * * * * * * *char tag[3], pad[5];
    > * * * * * * * *uint32_t v;
    > * * * * }
    >
    > to achieve the same purpose (both depends on properties of the
    > implementation)


    Sorry, I did not want to critism you in any way. I'm happy to get any
    help.
    I know, that I could use manual padding (as in the example above). But
    I do NOT know how much padding is
    required without digging into nested includes. char tag[3] uses 3
    bytes, thats clear. But

    struct x {
    abc_txpe elem1;
    xyz_type elem2;
    uint32_t v;}

    is not as clear. I can - of course - use offsetof, sizeof etc to get
    the infos. But what I am looking for is
    a way that the gcc directly loggs this information. (The compiler
    should know all this Info - so why to write
    a soft of helperprogram ?)

    But I guess, GCC does not provide this sort of listing. Well, then I
    must go some other way and count bytes

    Thanks for all responses.

    Klaus


  8. Re: GCC Howto get structure listing

    Klaus Klingenbusch writes:
    > On 4 Apr., 14:42, Rainer Weikusat wrote:
    >> would insert suitable padding into the structure to cause v to be
    >> aligned on an 8-byte-boundary. I would nevertheless usually prefer
    >>
    >> * * * * struct x {
    >> * * * * * * * *char tag[3], pad[5];
    >> * * * * * * * *uint32_t v;
    >> * * * * }
    >>
    >> to achieve the same purpose (both depends on properties of the
    >> implementation)

    >
    > Sorry, I did not want to critism you in any way. I'm happy to get any
    > help.
    > I know, that I could use manual padding (as in the example above). But
    > I do NOT know how much padding is required without digging into
    > nested includes.


    When you want to force a binary layout different from the one the
    compiler would pick on its own, you will have to 'dig into the nested
    includes'.


  9. Re: GCC Howto get structure listing

    Klaus Klingenbusch wrote in news:8b3ea195-d831-4cc8-bd0a-
    ce0845f3a346@y21g2000hsf.googlegroups.com:

    > I'm looking for a way to get a listing of all structure fields in a C
    > source with the size and the offset of the fields.
    >
    > Is such an output available from gcc? I did not find it until now.
    >
    > I know, I could check the assembler code, but this would be not very
    > handy. What I'm targetting is (manually) aligning or checking
    > structures definitions. Some of the are to complex or nested to count
    > bytes by hand. (And as these structure are at least partially part of
    > an external interface automatic alignment is no choice).


    There is a program called pstruct shipped in many linux distributions
    that may be useful. You first run your code through gcc with certain
    options to produce assembly output annotated with debugging information,
    then you run pstruct on the assembly.

    For example:
    gcc -gstabs -S s.c -o s.s
    pstruct s.s

    It produces output that looks like this:
    [other stuff snipped]
    struct xyz {
    char xyz.x 0 1
    struct abc xyz.xa 4 16
    char xyz.xa.a 4 1
    int xyz.xa.b 8 4
    char xyz.xa.a2 12 1
    float xyz.xa.c 16 4
    char xyz.y 20 1
    struct abc xyz.xb 24 16
    char xyz.xb.a 24 1
    int xyz.xb.b 28 4
    char xyz.xb.a2 32 1
    float xyz.xb.c 36 4
    char xyz.z 40 1
    struct abc xyz.xc 44 16
    char xyz.xc.a 44 1
    int xyz.xc.b 48 4
    char xyz.xc.a2 52 1
    float xyz.xc.c 56 4
    }

    The third column is the offset within the outer structure, the fourth is
    the member size. The output was produced from these structure
    definitions:

    struct abc {
    char a;
    int b;
    char a2;
    float c;
    };

    struct xyz {
    char x;
    struct abc xa;
    char y;
    struct abc xb;
    char z;
    struct abc xc;
    };

    GH

  10. Re: GCC Howto get structure listing

    Gil Hamilton writes:
    > Klaus Klingenbusch wrote in news:8b3ea195-d831-4cc8-bd0a-
    > ce0845f3a346@y21g2000hsf.googlegroups.com:
    >
    >> I'm looking for a way to get a listing of all structure fields in a C
    >> source with the size and the offset of the fields.
    >>
    >> Is such an output available from gcc? I did not find it until now.
    >>
    >> I know, I could check the assembler code, but this would be not very
    >> handy. What I'm targetting is (manually) aligning or checking
    >> structures definitions. Some of the are to complex or nested to count
    >> bytes by hand. (And as these structure are at least partially part of
    >> an external interface automatic alignment is no choice).

    >
    > There is a program called pstruct shipped in many linux distributions
    > that may be useful.


    When I run this on a moderately non-trivial source file, it prints
    complete nonsense or goes into an endless loop printing nonsense (when
    using -O2). Even if it didn't, all structures and structure members
    would still need to be massaged (insofar necessary) from lowest to
    highest level.

  11. Re: GCC Howto get structure listing

    Rainer Weikusat wrote in
    news:87hceh36jl.fsf@fever.mssgmbh.com:

    > Gil Hamilton writes:
    >> Klaus Klingenbusch wrote in news:8b3ea195-d831-4cc8-

    bd0a-
    >> ce0845f3a346@y21g2000hsf.googlegroups.com:
    >>
    >>> I'm looking for a way to get a listing of all structure fields in a

    C
    >>> source with the size and the offset of the fields.
    >>>
    >>> Is such an output available from gcc? I did not find it until now.
    >>>
    >>> I know, I could check the assembler code, but this would be not very
    >>> handy. What I'm targetting is (manually) aligning or checking
    >>> structures definitions. Some of the are to complex or nested to

    count
    >>> bytes by hand. (And as these structure are at least partially part

    of
    >>> an external interface automatic alignment is no choice).

    >>
    >> There is a program called pstruct shipped in many linux distributions
    >> that may be useful.

    >
    > When I run this on a moderately non-trivial source file, it prints
    > complete nonsense or goes into an endless loop printing nonsense (when
    > using -O2). Even if it didn't, all structures and structure members
    > would still need to be massaged (insofar necessary) from lowest to
    > highest level.


    Thanks for the bug report, but I'm not the author or maintainer of
    pstruct. I merely mentioned a tool I had stumbled onto recently. If it
    isn't useful for you, then obviously you shouldn't use it.

    For the record, I just ran it against a significantly non-trivial driver
    module that includes a healthy portion of the kernel headers and it
    seemed to work just fine.

    (I don't know whether the -O2 has something to do with it, but I'm not
    sure why you'd be optimizing at all anyway since the point of the
    exercise is to display the alignment of data structures not to produce
    running code.)

    GH

  12. Re: GCC Howto get structure listing

    On Apr 4, 9:53 am, Rainer Weikusat wrote:

    > When I run this on a moderately non-trivial source file, it prints
    > complete nonsense


    That's a bug.

    > or goes into an endless loop printing nonsense (when
    > using -O2). Even if it didn't, all structures and structure members


    That's user error. The documentation specifies the flags to pass to
    the compiler and '-O2' is not one of them.

    > would still need to be massaged (insofar necessary) from lowest to
    > highest level.


    I'm not quite sure what you mean by that. Are you talking about
    structures that include other structures?

    DS

  13. Re: GCC Howto get structure listing

    > I'm looking for a way to get a listing of all structure fields in a C
    > source with the size and the offset of the fields.


    For the size, then use 'sizeof'.

    For the offset, then you can do it by hand using a construct such as:
    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
    found in .
    Also see '__alignof__' which is analogous to 'sizeof'. Consult "info gcc".

    --

  14. Re: GCC Howto get structure listing

    David Schwartz writes:
    > On Apr 4, 9:53 am, Rainer Weikusat wrote:
    >> When I run this on a moderately non-trivial source file, it prints
    >> complete nonsense

    >
    > That's a bug.
    >
    >> or goes into an endless loop printing nonsense (when
    >> using -O2). Even if it didn't, all structures and structure members

    >
    > That's user error. The documentation specifies the flags to pass to
    > the compiler and '-O2' is not one of them.


    It doesn't:

    All you need to be able to use this is a C compiler than generates
    BSD/GCC-style stabs. The -g option on native BSD compilers and GCC
    should get this for you.

    And neither should it. The structure definitions don't change
    because of a certain optimization setting.

    >> would still need to be massaged (insofar necessary) from lowest to
    >> highest level.

    >
    > I'm not quite sure what you mean by that. Are you talking about
    > structures that include other structures?


    THe OP wanted to enforce a specific binary layout by means of C
    structure definitions and implementation dependent hacks (instead of,
    eg, defining a specific binary format and write encoding and decoding
    routines for it, which would be more portable and may be faster,
    because the code operating on the data can be generated to use a
    binary layout which is easy to process on the target architecture
    instead of one which may necessitate more complicated access
    procdures, eg reading a value byte-by-byte and assembling those bytes
    into machine words). This means that he would have to force a layout
    for every structure, starting with the ones which consist only of
    primitive types and working his way up from there. I would consider it
    to be easier to use knowledge about the ABI to determine modifications
    which may be necessary for any particular structure definition instead
    of generating a large listing of all structure definitions and using
    the numbers 'blindly', ie without really knowing why they have the
    values they have. OTOH, I read pretty fast ...

  15. Re: GCC Howto get structure listing

    On Apr 6, 1:06 am, Rainer Weikusat wrote:

    > > That's user error. The documentation specifies the flags to pass to
    > > the compiler and '-O2' is not one of them.


    > It doesn't:


    Mine does:

    .... as generated from "cc -g -S" stabs

    > All you need to be able to use this is a C compiler than generates
    > BSD/GCC-style stabs. The -g option on native BSD compilers and GCC
    > should get this for you.
    >
    > And neither should it. The structure definitions don't change
    > because of a certain optimization setting.


    But the ability of a simple tool to parse them does.

    DS

  16. Re: GCC Howto get structure listing

    David Schwartz writes:
    > On Apr 6, 1:06 am, Rainer Weikusat wrote:
    >
    >> > That's user error. The documentation specifies the flags to pass to
    >> > the compiler and '-O2' is not one of them.

    >
    >> It doesn't:

    >
    > Mine does:
    >
    > ... as generated from "cc -g -S" stabs


    Your's doesn't. That's an example from the introductory
    line and the text specifically does not require it,
    see quote below.

    >> All you need to be able to use this is a C compiler than generates
    >> BSD/GCC-style stabs.


+ Reply to Thread