Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux objdump command

Linux objdump command

Linux Command Reference Linux Command Reference


objdump is an important command-line tool in the GNU Binutils suite, used to display various information about object files and executable files. It is a powerful tool for binary analysis, reverse engineering, and debugging on Linux.

The main functions of objdump include:


Basic Syntax

The basic command format of objdump is as follows:

objdump [选项] 文件名

If no options are specified, objdump displays the section header information of the file.


Details of common options

Disassembly-related options

Example

-d,

--disassemble

       

Disassemble sections that contain code

-D,

--disassemble-all

   

Disassemble all sections

-S,

--source

           

Intermix source code and assembly code (requires compiling with the -g option)

--prefix-addresses

     

Show full addresses when disassembling

--no-addresses

         

Do not display address information

Section information options

Example

-h,

--section-headers

   

Display section header information

-j,

--section

=name      

Display only the contents of the specified section

Symbol table options

Example

-t,

--syms

             

Display the symbol table

-T,

--dynamic-syms

     

Display the dynamic symbol table

File header information

-f, --file-headers       # 显示文件头部信息

Other useful options

Example

-l,

--line-numbers

     

Display line number information (requires debug information)

-r,

--reloc

             

Display relocation entries

-R,

--dynamic-reloc

     

Display dynamic relocation entries

-s,

--full-contents

     

Display the complete contents of all sections


Practical Examples

Example 1: View the structure of an executable file

objdump -h /bin/ls

Example output:

/bin/ls:     file format elf64-x86-64



Sections:

Idx Name          Size      VMA               LMA               File off  Algn

  0 .interp       0000001c  0000000000400238  0000000000400238  00000238  2**0

                  CONTENTS, ALLOC, LOAD, READONLY, DATA

  1 .note.ABI-tag 00000020  0000000000400254  0000000000400254  00000254  2**2

                  CONTENTS, ALLOC, LOAD, READONLY, DATA

  ...

Example 2: Disassemble an executable file

objdump -d /bin/ls

Sample output (partial):

0000000000405a50 :

  405a50:       31 ed                   xor    %ebp,%ebp

  405a52:       49 89 d1                mov    %rdx,%r9

  405a55:       5e                      pop    %rsi

  405a56:       48 89 e2                mov    %rsp,%rdx

  405a59:       48 83 e4 f0             and    $0xfffffffffffffff0,%rsp

  ...

Example 3: View the symbol table

objdump -t myprogram.o

Example output:

myprogram.o:     file format elf64-x86-64



SYMBOL TABLE:

0000000000000000 l    df *ABS*  0000000000000000 myprogram.c

0000000000000000 l    d  .text  0000000000000000 .text

0000000000000000 g     F .text  0000000000000015 main

0000000000000000         *UND*  0000000000000000 printf

Example 4: Intermix source code and assembly code

objdump -S myprogram

Example output:

Example

int

main

(

)

{

 

400526

:

     

55

                     push  

%

rbp

 

400527

:

     

48

89

e5                mov    

%

rsp

,%

rbp

   

printf

(

"Hello, World!n"

)

;

  40052a

:

      bf d4

05

40

00

         mov    $

0x4005d4

,%

edi

 

40052f

:

      e8 cc fe ff ff          callq  

400400

   

return

0

;

 

400534

:

      b8

00

00

00

00

         mov    $

0x0

,%

eax

}


Practical Application Scenarios

Scenario 1: Debugging a program crash

When a program crashes, you can use objdump to view the code near the crash address:

objdump -d --start-address=0x400526 --stop-address=0x400536 myprogram

Scenario 2: Analyzing library function calls

See which dynamic library functions a program calls:

objdump -T myprogram | grep UND

Scenario 3: Learning assembly language

Learn assembly language by disassembling a simple C program:

Example

gcc

-o

simple simple.c

objdump

-d

simple


Notes

  1. Debug information: To obtain source-level information, you need to add the option when compiling -g Option
  2. Optimization impact: Compiler optimization affects the generated assembly code, so take care when analyzing
  3. Architecture differences: Different CPU architectures have different assembly instructions; make sure to use the correct disassembly options
  4. Permission issues: Analyzing system files may require root privileges
  5. File format: objdump mainly targets ELF format files; other formats may require special handling

Advanced Tips

Use in combination with other tools

Example

Use grep to filter for specific functions

objdump

-d

myprogram

|

grep

-A20

"main>:"

Count function sizes

objdump

-d

myprogram

|

awk

'/^[0-9a-f]+ :/ {print $1,$2}'

Create a disassembly script

Example

#!/bin/bash

Example disassembly script

if

[

$#

-ne

1

]

;

then

   

echo

"Usage: $0 "

   

exit

1

fi

echo

"=== File Header Info ==="

objdump

-f

$1

echo

-e

"n=== Section Info ==="

objdump

-h

$1

echo

-e

"n=== Disassembly Code ==="

objdump

-d

$1


Summary

objdump is a powerful binary analysis tool on Linux. Mastering it lets you:

Through the basic usage and practical examples in this article, you should now be able to start using objdump for basic binary file analysis. As you gain experience, you will discover more of its uses in system programming and debugging.


Linux Command Reference Linux Command Reference