Reorder aligned objects to minimize space usage

I have a data structure that needs to be aligned with 4-KiB. I can apply this with __attribute__ ((aligned (4096))).

The problem is that this alignment requirement causes memory to be lost. This is how the linker puts the characters ( pg_dir- aligned data structure):

00011000 <cursor_loc>:
00012000 <pg_dir>:
00013000 <idt>:

cursor_lochas a size of only four bytes. That would be better:

00011000 <pg_dir>:
00012000 <cursor_loc>:
00012008 <idt>:

( idtmust be aligned by 8 bytes.)


You can play it using several files:

test1.c:

char aligned[4096] __attribute__ ((aligned (4096)));
int i;

test2.c:

int j;

int main(void) { }

Then build it with

gcc test1.c test2.c

and objdump -D a.outprints this:

0000000000602004 <j>:
        ...

0000000000603000 <aligned>:
        ...

0000000000604000 <i>:

How can I move GNU ld to reorder characters for minimal waste? I really wonder why this is not done automatically.

+6
1

, , , ( , , - ), . , - :

gcc -fno-common -fdata-sections -Wl,--sort-section=alignment test1.c test2.c

-fno-common , -fdata-sections , -Wl,--sort-section=alignment - .

+2

All Articles