« 论文中的制图 | Main | Upgrading to Fedora Core 4 »
December 13, 2005
how "size" works
I'm talking about the utility "size" of binutils-2.16.1.
It's confusing that it can display the size of a binary in two formats:
System V size (--format=sysv), or Berkeley size (--format=berkeley, the default option).
The sysv format is easier to understand, the output is basically the same as that of "readelf -S": the size of every section is listed.
However berkeley format is more useful when you measure the size of data, code, and bss. Then it comes to the problem how you sum up those sections?
Check out the function berkeley_sum and you'll understand how datasize, bsssize, and textsize are calculated:
static void
berkeley_sum (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
void *ignore ATTRIBUTE_UNUSED)
{
flagword flags;
bfd_size_type size;
flags = bfd_get_section_flags (abfd, sec);
if ((flags & SEC_ALLOC) == 0)
return;
size = bfd_get_section_size (sec);
if ((flags & SEC_CODE) != 0 || (flags & SEC_READONLY) != 0)
textsize += size;
else if ((flags & SEC_HAS_CONTENTS) != 0)
datasize += size;
else
bsssize += size;
}
bfd is documented here.
#define bfd_get_section_flags(bfd, sec_ptr ptr) ((ptr)->flags + 0)
typedef struct bfd_section *sec_ptr;
Hmm, this is still too confusing.
Well, I created a utility to dump all the sections: size
Posted by Roy at December 13, 2005 04:47 PM