Just as the text utilities are meant for working with text files, the GNU binary utilities are meant for performing various tasks on binary files. Some of these utilities, such as ar, as, and ld, are used when building and managing object files that are generated when source files are compiled. A number of other binary utilities enable you to examine the contents of binary files. For example, the strings command prints all strings of printable characters in a file. Here is what the strings command displays for a simple C program that prints Hello, World! (the name of the binary executable is a.out):
strings a.out /lib/ld-linux.so.2 libc.so.6 printf _IO_stdin_used __libc_start_main __gmon_start__ GLIBC_2.0 PTRh QVh( Hello, World!
Notice that the output includes the Hello World! string, as well names of libraries (/lib/ld-linux.so.2 and libc.so.6) and C functions (printf).
You can use the size command to look at the number of bytes that various sections (such as the code size, data area, and stack space) of a program would need. Here is the output of size for the a.out file that contains the Hello, World! program:
size a.out text data bss dec hex filename 790 256 4 1050 41a a.out
In this case, the program requires 790 bytes for the code (called text, but it’s the binary executable code of a program), 256 bytes for the data, and 4 bytes for stack. Thus, the program requires a total of 1,050 bytes of memory. Note that the actual size of the a.out file is about 10KB, which is much larger than 1,050 bytes because other information, such as symbols, is included in the file. Programmers would find the output of the size command useful because it tells them about the memory required to load and run a program.
Table 8-5 briefly describes the programs in the GNU binary utilities package. You can try some of these programs on any binary file in the system. For example, here’s the result of running size on /bin/bash—the executable for the Bash shell:
size /bin/bash text data bss dec hex filename 602431 22200 17044 641675 9ca8b /bin/bash
Program |
Description |
---|---|
addr2line |
This uses debugging information in an executable file to translate program addresses into filenames and line numbers |
ar |
This creates and modifies archives and extracts from archives. (An archive is a library holding the object code of commonly needed subroutines.) |
as |
This is the portable GNU assembler |
gasp |
This is a filter program to translate encoded C++ symbols |
gprof |
This is the GNU profiler, used to determine which parts of a program are taking most of the execution time |
ld |
This the GNU linker, used to combine a number of object and archive files and create executable files |
nm |
This lists symbols from object files |
objcopy |
This copies the contents of an object file to another (can also translate the format, if required) |
objdump |
This displays information from object files |
ranlib |
This generates an index to the contents of an archive |
readelf |
This displays information about one or more Executable and Linking Format (ELF) object files |
size |
This lists the section sizes of an object or archive file |
strings |
This lists printable strings from files |
strip |
This discards symbols from object files |