An object or executable file in Linux consists of several sections, such as text and data. To examine the size of each section, you can use the size command—a handy command-line utility. In this guide, we will delve into the essential features of this tool with examples that are easy to follow.
Note: The examples in this article have been tested on Ubuntu 24.04 LTS.
Understanding the Linux Size Command
The size command provides the section sizes and the total size for the given object file(s). Below is the command syntax:
size [-A|-B|--format=compatibility] [--help] [-d|-o|-x|--radix=number] [--common] [-t|--totals] [--target=bfdname] [-V|--version] [objfile...]
The GNU size utility lists the section sizes and the total size for each object or archive file specified as arguments. By default, one line of output is generated for each object file or module in an archive.
If no object files are specified, the default file “a.out” will be used.
Examples
1. How to Use the Size Command
Using the size command is straightforward. Provide the name of the object or executable file as an argument. Here’s an example:
size apl
The output for the above command is:
The output displays the sizes of the text, data, and bss sections, followed by the total size in decimal and hexadecimal, and finally the filename.
2. Changing Output Formats
The default output format resembles Berkeley’s format. To switch to the System V convention, use the –format option with the SysV value:
size apl --format=SysV
Here’s the output using this option:
3. Displaying Section Sizes in Different Units
The default is decimal, but sizes can be shown in octal or hexadecimal using the -o and -x options:
About these options, the manual states:
-d -o -x --radix=number Using one of these options, you can control whether the size of each section is given in decimal (-d, or --radix=10); octal (-o, or --radix=8); or hexadecimal (-x, or --radix=16). In --radix=number, only the values 8, 10, and 16 are supported. The total size is always given in two radices; decimal and hexadecimal for -d or -x output, or octal and hexadecimal if you're using -o.
4. Summing Section Sizes Across Files
When examining multiple files, you can use the -t option to sum the sizes of all sections:
size -t [file1] [file2] ...
The last row in the output is the sum of all sections, as displayed by the -t option:
5. Displaying Common Symbols in Files
To make the size command print the total size of common symbols for each file, utilize the –common option:
size --common [file1] [file2] ...
Note: When using Berkeley format, common symbols are included in the bss size.
6. Additional Command-Line Options
Besides the options covered above, the size command includes:
- -v for version information
- -h for a summary of options
You can instruct size to read options from a file via the @file option. Options in the file are space-separated. The file may also contain additional @file options, processed recursively.
The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed. Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes. Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options; any such options will be processed recursively.
Conclusion
The size command is specialized, directed at those working with the structure of object or executable files in Linux. If you work in this area, practice the options discussed. For additional details, refer to its man page.
Frequently Asked Questions (FAQ)
What is the size command used for?
The size command is used to examine the size of various sections of an object or executable file, alongside providing the total size.
Can I use the size command on files other than .a.out?
Yes, you can specify any object file whose section sizes you wish to examine, not just .a.out.
How can I see the output in hexadecimal format?
Use the -x option to view section sizes in hexadecimal format.
How do I switch the output to the System V format?
Use the –format=SysV option to switch the output to System V format.