I'd like to know how to get this berkeley format output:
$ size --format=berkeley /bin/ls
text data bss dec hex filename
124042 4728 4832 133602 209e2 /bin/ls
From this sysv format output:
$ size --format=sysv /bin/ls
/bin/ls :
section size addr
.interp 28 568
.note.ABI-tag 32 596
.note.gnu.build-id 36 628
.gnu.hash 236 664
.dynsym 3576 904
.dynstr 1666 4480
.gnu.version 298 6146
.gnu.version_r 112 6448
.rela.dyn 4944 6560
.rela.plt 2664 11504
.init 23 14168
.plt 1792 14192
.plt.got 24 15984
.text 74969 16016
.fini 9 90988
.rodata 19997 91008
.eh_frame_hdr 2180 111008
.eh_frame 11456 113192
.init_array 8 2224112
.fini_array 8 2224120
.data.rel.ro 2616 2224128
.dynamic 512 2226744
.got 968 2227256
.data 616 2228224
.bss 4832 2228864
.gnu_debuglink 52 0
Total 133654
In other words, which of the little parts (sections) of the "sysv" format go into which of the big parts (text
, data
, and bss
sections), of the "berkeley" format?
I'm trying to guess here by seeing what sums to what.
In other words, I'd like to know:
? + ? + ? = text
? + ? + ? = data
? + ? + ? = bss
Here's the answer:
TLDR;
See also the image with yellow, blue, and red boxes at the end, for a quick visual summary.
Details:
First, let's print the berkeley size information in hex, with
size -x --format=berkeley /bin/ls
orsize -x /bin/ls
(same thing, since berkeley is the default format):And here's the sysv size output in hex, obtained with
size -x --format=sysv /bin/ls
:Next, if you run
objdump -h /bin/ls
, you get the following, which shows all output sections in the/bin/ls
object file, or executable. These output sections match the output from thesize -x --format=sysv /bin/ls
command, but have more-detailed information such as the VMA (Virtual Memory Address) and LMA (Load Memory Address), among other things:A Google search for "vma and lma meaning" brings me to this site, which has a useful quote from the GNU
ld
linker manual. Searching for that quote leads me here, which conveniently has the source for the quote. So let's just cite the quote directly from its original source:(Source: GNU linker script
ld
manual)This means that any output section shown by
objdump -h
which does NOT have a VMA is not part of the program. That eliminates the.gnu_debuglink
section.Next, we can see that the
.bss
section has the exact same size (0x12e0) as the berkeleybss
section, so that's a match:bss
contains the zero-initialized global and static variables.So, what about the
data
output section, which contains all NON-zero-initialized (ie: initialized with some non-zero value) global and static variables?And, what about the
text
output section, which contains all program code and constant (read only) static and global variables?Well, through logical deduction and analysis, and using my prior knowledge about which sections go into Flash vs RAM vs both on microcontrollers, I determined that all sections which are marked
READONLY
in theobjdump -h
output sections (which contains someDATA
(non-zero-initialized,const
(read-only) static and global variables) and someCODE
(the actual program logic) (also read-only)) are stored into thetext
output section.So:
You can confirm that in the math by summing all their sizes. In hex:
...which is the size of the
text
section shown in the berkeley size output.You can see them boxed in yellow in the image below.
So, the remainder, which are marked
DATA
and NOTREADONLY
, are thedata
sections:Again, the hex size summation confirms this:
...which is the size of the
data
section in the berkeley size output.You can see them boxed in blue in the image below.
In this image, you can see all 3 berkely output sections boxed in different colors:
text
output sections (read-only, program logic and const static and global variables) are boxed in yellow.data
output sections (non-zero-initialized [ie: other-than-zero initialized] static and global variables) are boxed in blue.bss
output sections (zero-initialized static and global variables) are boxed in red.In the case of looking at a microcontroller object file, such as for an STM32 mcu:
text
+data
, andbss
+data
.RAM_total - (bss + data)
.Primary References:
ld
) manual, section "3.1 Basic Linker Script Concepts": https://sourceware.org/binutils/docs/ld/Basic-Script-Concepts.html#Basic-Script-Concepts