2

With arm gcc cross compiler for aarch64, the following structure:

struct lock {
    uint32_t lk;
};

always got compiled to an address which is aligned with 8 bytes. When I try to put the structure onto a 4-byte aligned address, gcc will fill 4 empty bytes before the structure to make it on 8-bytes aligned address.

Is this a convention? or I can config it to be 4 bytes aligned.

ldscript:

SECTIONS {
.init.data : {
         *(.init.data.v);
         *(.init.data.l);
 } > INIT_DATA
} 

There is a uint32_t put in .init.data.v, then a struct lock variable put in .init.data.l, but between them there's a 4-byte fill. If code has a uint32_t in .init.data.v, then another uint32_t in .init.data.l, there's NO 4-byte fill.

So only struct variable causes this 4-byte fill.

https://pastebin.com/kRsmPwrS

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Leslie Li
  • 407
  • 7
  • 14
  • And what is your linker script? – 0___________ Sep 15 '17 at 08:04
  • I put the structure in a user defined section in linker script, start at 8bytes aligned address, the first variable is a uint32_t, then followed by this struct, but between them, there is a 4 bytes empty fill – Leslie Li Sep 15 '17 at 08:13
  • I would try to set it in the linker script. I do not have any similar project now - so cant try and have no time to create a new one. – 0___________ Sep 15 '17 at 13:37
  • why would you want to change the alignment? – old_timer Sep 21 '17 at 17:15
  • some guy from Linaro gave me below link, which talks about the same issue, and he encouraged to report a bug of aligned attribute absence: https://gcc.gnu.org/ml/gcc-patches/2017-06/msg00317.html – Leslie Li Sep 26 '17 at 08:58

3 Answers3

1

I guess, it happen in order to improve data access performance - you are talking about 64-bit CPU.

Dima
  • 1,253
  • 3
  • 21
  • 31
1

There are many questions on this on this site.

You're probably going to need to pass -munaligned-access to the compiler to make it do this, depending on your exact target processor. This does beg the question of why here.

Also see

Does AArch64 support unaligned access?

Linaro g++ aarch64 compilation cause unalignment fault

camelccc
  • 2,847
  • 8
  • 26
  • 52
0
asm(".global _start; _start: b .");


struct lock {
    unsigned int lk;
};

struct lock x;
unsigned char a;
unsigned short b;
struct lock y;


void centry ( void )
{
}

Gcc is just trying for alignment

Disassembly of section .text:

0000000000001000 <_start>:
    1000:   14000000    b   1000 <_start>
    1004:   d503201f    nop

0000000000001008 <centry>:
    1008:   d65f03c0    ret

Disassembly of section .bss:

0000000000002000 <b>:
    ...

0000000000002008 <x>:
    ...

0000000000002010 <y>:
    2010:   00000000    .inst   0x00000000 ; undefined

0000000000002014 <a>:
    2014:   00000000    .inst   0x00000000 ; undefined

you really shouldnt fight that if you can avoid it. if you are trying to point structures across compile domains then you are asking for trouble anyway.

asm(".global _start; _start: b .");


struct lock {
    unsigned int lk;
};

struct lock x;
unsigned char a;
unsigned short b;
unsigned int c;
unsigned int d;
struct lock y;
unsigned int e;

void centry ( void )
{
}

that worked

Disassembly of section .text:

0000000000001000 <_start>:
    1000:   14000000    b   1000 <_start>
    1004:   d503201f    nop

0000000000001008 <centry>:
    1008:   d65f03c0    ret

Disassembly of section .bss:

0000000000002000 <b>:
    ...

0000000000002008 <x>:
    2008:   00000000    .inst   0x00000000 ; undefined

000000000000200c <c>:
    200c:   00000000    .inst   0x00000000 ; undefined

0000000000002010 <y>:
    2010:   00000000    .inst   0x00000000 ; undefined

0000000000002014 <d>:
    2014:   00000000    .inst   0x00000000 ; undefined

0000000000002018 <a>:
    2018:   00000000    .inst   0x00000000 ; undefined

000000000000201c <e>:
    201c:   00000000    .inst   0x00000000 ; undefined

I wouldnt rely on these settings or how they arrange things, you let the compiler set the alignment and packing on the structure so that it ideally doesnt create alignment faults or performance hits. Just asking for trouble if you start to mess with that, may take days or years for the trouble to hit but it will eventually.

old_timer
  • 69,149
  • 8
  • 89
  • 168