How can I peek at the assembly code generated by GCC -- Structure padding.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arunraj2002in
    New Member
    • Jun 2007
    • 22

    How can I peek at the assembly code generated by GCC -- Structure padding.

    gcc -O2 -S -c b.c gives the answer but if we declare a structure say
    struct test_t {
    int a;
    char b;
    int c;
    };
    main()
    {
    struct test_t test= { 13,30, 40};
    }

    Now in command prompt if we type gcc -O2 -S -c b.c will give output in b.s file as
    30 .align 4
    31 .LC0:
    32 0000 0D000000 .long 13
    33 0004 1E .byte 30
    34 0005 000000 .zero 3 ( structure padding of 3 Bytes)
    35 0008 28000000 .long 40

    with the help of gcc -S command we can see the padding done by compiler but if we don't

    declare the structure say:

    struct test_t {
    int a;
    char b;
    int c;
    };
    main()
    {
    struct test_t test;/*= { 13,30, 40};*/
    }

    here the output is:
    .LC0:
    31 0000 256400 .string "%d"
    32 .text
    33 .align 4
    35 .globl main
    37 main:..


    Here i can't see the structure, only if i assign the structure with a value then only "gcc

    -S" is effective. Is their any other way to find padding done by structure.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    If you want to look at the assembly, you could use GDB - the GNU Debugger.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You can calculate the pad yourself.
      [code=c]
      struct MyStruct
      {
      int a;
      char b;
      int c;
      };
      [/code]

      Here the &a + (sizeof int) should be &b. If not, the difference between &a + (sizeof int) and &b is the pad.

      You should find three pad bytes after b.

      By &a + (sizeof int) I mean the &a plus (sizeof int) bytes.

      You will need to convert your addresses to int to do the calculations.

      Comment

      Working...