c - How the linker calculate the size of .bss section? -


The

I want to know how Linkkrta .bss calculating section size?

I have a test program with two variables, one has to start at zero, the start of the second zero I hope .bss size 4 and it is.

  # cat test.c & amp; Amp; GCC-O Test O-C Test C & amp; LD-O Test Test O & amp; amp; Readelf -Ss test int _start = 0; Int a = 2; ...... Section Headings: [NR] Name Type Address Offset Size EntSize Flags Link Information Align [1]. Data PROGBITS 00000000006000b0 000000b0 0000000000000004 0000000000000000 WA 0 4 4 [2] .bss NOBITS 00000000006000b4 000000b4 0000000000000004 000000000000000004 000000000000000004 000000000000000000 WA 0 4 4 points :: value size of binding vis NDX name 6: 00000000006000b4 4 object global default 2 10 _start: ..... symbols include table '.symtab' 11 entries 00000000006000b0 4 object global default 1  < / Pre> 

Then I add another zero-initial value. This time I hope the size of .bss is 8, but this is 12 (0xc).

  # cat test.c & amp; Amp; GCC-O Test O-C Test C & amp; LD-O Test Test O & amp; amp; Readelf -Ss test int _start = 0; Int a = 2; Int b = 0; ...... Section Headings: [NR] Name Type Address Offset Size EntSize Flags Link Information Align [1]. Data PROGBITS 00000000006000b0 000000b0 0000000000000004 000000000000000000 WA 0 4 4 [2] .bss Nobbit 00000000006000b4 000000b4 000000000000000c 00000000000000000c 00000000000000000c 0000000000000000 WA 0 4 4 points :: value tied to the size of vis NDX name 6: 00000000006000b8 4 object global default 2B 7: 00000000006000b4 4 object global default 2 _start 11: 00000000006000b0 4 object global default ...... symbol table '.symtab' 12 entries included 1 one   

I initialazed the third zero- Continue adding and this time I hope .bss size 12 and it is.

  # cat test.c & amp; Amp; GCC-O Test O-C Test C & amp; LD-O Test Test O & amp; amp; Readelf -Ss test int _start = 0; Int a = 2; Int b = 0; Int c = 0; ...... section headers: [Nr] Name Type Address Offset Size EntSize align flag link information [0] zero 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [1] .data PROGBITS 00000000006000b0 000000b0 0000000000000004 -0000000000000000 Washington 0 0 4 [2 ] .bss NOBITS 00000000006000b4 000000b4 000000000000000 C 00000000000000000 C 000000000000000000 WA 0 4 4 ...... symbol table 13 entries '.symtab': number value size type bind vis Andiaks name 6: 00000000006000b8 4 object global default 2 B7: 00000000006000b4 4 Object global default 2 _start 8: 00000000006000 BC4Ab Global crop would keep the default 2 C 12: 00000000006000b0 4 object global default 1   

I continue to add the fourth zero-initial value. This time I hope the size of .bss is 16, but this is 20 (0x14).

  # cat test.c & amp; Amp; GCC-O Test O-C Test C & amp; LD-O Test Test O & amp; amp; Readelf -Ss test int _start = 0; Int a = 2; Int b = 0; Int c = 0; Int d = 0; ...... Section Headings: [NR] Name Type Address Offset Size EntSize Flags Link Information Align [1]. Data PROGBITS 00000000006000b0 000000b0 0000000000000004 0000000000000000 WA 0 4 4 [2] .bss NOBITS 00000000006000b4 000000b4 0000000000000014 000000000000000014 000000000000000014 0000000000000000 WA 0 4 4 points :: Value size Type bound by NDX name 6: 00000000006000b8 4 Object global default 2 b 7: 00000000006000b4 4 Object global default 2 _start 8: 00000000006000bc 4 Item global default ...... Symbol table '.symtab' 14 entries included 2C10: 00000000006000C4 Object Global DIFA T 2 D 13: 00000000006000b0 4 object global default 1 matches a   

Sometimes the results I expected, not sometimes. Does any other factor affect the output of the linker?

This is really alignment. The size of bss in your example is not the expected by 8-byte combinations, but that's because you have 4 bytes of data if you add a second initial variable, So you will see that the size of the bss segment is 8 bytes aligned.

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -