How to use the command wasm-objdump (with examples)
The wasm-objdump
command is used to display information from WebAssembly binaries. It can be used to view the section headers, disassembled output, and details of each section of a given binary file.
Use case 1: Display the section headers of a given binary
Code:
wasm-objdump -h file.wasm
Motivation: This use case is useful when you want to quickly view the section headers of a WebAssembly binary file. Section headers provide information about the different sections present in the binary, such as the import section, function section, memory section, etc.
Explanation:
The -h
option is used to specify that we want to display the section headers. The argument file.wasm
is the name of the binary file we want to analyze.
Example output:
Sections:
- type = 1
- import = 2
- function = 3
- table = 4
- memory = 5
- global = 6
- export = 7
- start = 8
- elem = 9
- code = 10
- data = 11
Use case 2: Display the entire disassembled output of a given binary
Code:
wasm-objdump -d file.wasm
Motivation: Sometimes, it’s necessary to analyze a WebAssembly binary in a more human-readable format. This use case is useful when you want to view the disassembled output of all the instructions in the binary, making it easier to understand the functionality of the code.
Explanation:
The -d
option is used to specify that we want to display the disassembled output. The argument file.wasm
is the name of the binary file we want to analyze.
Example output:
0000039b | 20 00 | get_local 0
0000039d | 7f | i32.eqz
0000039e | 04 | if
0000039f | 41 0c | i32.const 12
000003a1 | 0b | else
000003a2 | 20 01 | get_local 1
000003a4 | 41 03 | i32.const 3
...
Use case 3: Display the details of each section
Code:
wasm-objdump --details file.wasm
Motivation: To get a more in-depth understanding of a WebAssembly binary, it can be helpful to view additional information about each section. This use case provides detailed information about each section, including the start offset, size, and content.
Explanation:
The --details
option is used to specify that we want to display the details of each section. The argument file.wasm
is the name of the binary file we want to analyze.
Example output:
Section Details:
- type: Offset = 8, Size = 6, Content = 01 07 01
- import: Offset = 14, Size = 24, Content = 02 0b 07 00 5f 5f 65 6e 74 72 79 02 5f 5f 64 75 6d 6d 79 00 02 0a 07 00 5f 5f 64 61 74 61 00
- function: Offset = 38, Size = 2, Content = 01 00
...
Use case 4: Display the details of a given section
Code:
wasm-objdump --section 'import' --details file.wasm
Motivation: When you want to analyze the contents of a specific section in a WebAssembly binary, this use case allows you to display the details of that particular section. It can be useful for understanding the imported functions, tables, and memories used by the binary.
Explanation:
The --section 'import'
option is used to specify the section name we want to display details for. The --details
option is used to indicate that we want to view the details. The argument file.wasm
is the name of the binary file we want to analyze.
Example output:
Section 'import' Details:
- Offset: 14
- Size: 24
- Content: 02 0b 07 00 5f 5f 65 6e 74 72 79 02 5f 5f 64 75 6d 6d 79 00 02 0a 07 00 5f 5f 64 61 74 61 00
Conclusion:
The wasm-objdump
command is a powerful tool for analyzing WebAssembly binaries. It allows you to gain insights into the structure and functionality of the code by displaying section headers, disassembled output, and section details. Whether you’re a developer or a security researcher, understanding the inner workings of WebAssembly binaries can help you in various scenarios, such as debugging, performance analysis, or vulnerability assessment.