“The objdump is a command-line utility used to display information about an object on Unix-like systems. The command, when used, gets an object file’s information even when you don’t have its source code. Therefore, it can be a debugging tool for object files, especially when working with compiler programs.”
This guide covers the different uses of the objdump command with examples. Check it out.
Uses of the Objdump Command
The objdump has 6 main purposes:
- To debug the object file
- Retrieving file headers
- Getting the bfdname
- Retrieving archive header
- Getting offsets of a file
- Disassembling an object file
Its basic syntax is:
There are many options to use with the objdump command, and you can view the help page to view them.
This article will use the /bin /echo as our object file for our examples. However, you can use other object files such as C programs. The usage is still the same.
Working With Objdump
1. Display File Headers of the Object File
The -f option, when used with the objdump command, retrieves all the file headers associated with a given object file, as shown below. Remember, we are using the /bin /echo, which is a binary executable file for the echo command as our object in this case.
The resulting output displaying the file headers will be:
You can note the object file’s format and headers from the output.
2. To Get the Object-Specific File Headers
If you need to display specific file headers for the object file you are using, use the -p option.
3. Display Section Header’s Contents
Each file has different section headers. If you need to display the contents of each of the section headers, use the -h option.
In this case, the output was:
From the output, you can notice that the different section headers are numbered from 1, and each section header has its size, VMA, LMA, File off, and Algn.
Size: represents the size of the section loaded.
VMA: represents the virtual memory address
LMA: represents the logical memory address
File off: represents the section’s offset from the start of the file.
Algn: represents the alignment of the section.
The other details, such as ALLOC, DATA, READONLY, and CONTENTS, are the different flags that represent if the section is READONLY or is LOADED.
4. Display Information of All Headers
You can also get all the information about the headers in the object file. To do this, use the -x option.
In this example, the partial output for the above command will be:
5. To Get the Executable Section’s Assembler Contents
To get the executable section’s assembler content, use the -d option.
In this example, the partial output is:
The output gets divided into executable sections, and for each, its assembler contents get displayed as shown. For instance, you can see the section “.init” and its corresponding assembler contents below it before proceeding to the next executable section of the object file.
6. Get Assembler Contents of Every Section
The -d displays the assembler contents of only the executable sections. However, use the- D option if you need the contents for all file sections.
7. To Get All Contents of All Sections
For this, use the -s option
The output for all sections and their contents will be:
8. Display Debug Information
The -g option displays all the available debug information of the object file.
9. Display Contents of Symbol Table
If the object file has a symbol table, you can display its contents using the -t option
An objdump command is a useful tool for programmers who deal with compilers. The command has multiple uses, and we’ve covered the most common example usage. You can also check its man page for more details on how you can use the tool. Furthermore, the command can get a bit overwhelming. So, keep practicing it more. All the best!