Skip to content
systemcoding.dev
Assembly

Read Cortex-M Disassembly With arm-none-eabi-objdump

Disassemble a Cortex-M ELF with arm-none-eabi-objdump: pick sections and symbols, read the encoding columns, and spot literal pools and the vector table.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Arm, GNU and AMD logos.

When a Cortex-M program misbehaves, the source tells you what you meant and the disassembly tells you what the core is doing. arm-none-eabi-objdump from the Arm GNU Toolchain 14 (binutils 2.42) turns an ELF file into that second view. Reading it is a skill with a small number of moving parts: knowing which flags to pass, what each column means, where data hides inside code, and how to find one function in a firmware image that may hold ten thousand. This post walks through a tiny example end to end.

The program being inspected

Take a small C file built for a Cortex-M4 with -O1:

add.cc
#include <stdint.h>

uint32_t add3(uint32_t x)
{
    return x + 3;
}

uint32_t scale(uint32_t x)
{
    return x * 1000;
}
Shell
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O1 -c add.c -o add.o
arm-none-eabi-objdump -d add.o

The -d flag disassembles sections that contain code. For the file above it prints:

Text
add.o:     file format elf32-littlearm


Disassembly of section .text:

00000000 <add3>:
   0:	3003      	adds	r0, #3
   2:	4770      	bx	lr

00000004 <scale>:
   4:	f44f 737a 	mov.w	r3, #1000	@ 0x3e8
   8:	fb00 f003 	mul.w	r0, r0, r3
   c:	4770      	bx	lr

Each line has four parts. The first is the address of the instruction within the section. The second is the raw encoding, in halfwords, each shown in hex: 3003 is one 16-bit instruction, f44f 737a is a 32-bit instruction made of two. The third is the mnemonic and operands. The fourth, after an @, is a comment the disassembler adds, often the decoded immediate.

Look at what the compiler chose. adds r0, #3 is the 16-bit form, two bytes. x * 1000 needs a constant that does not fit the 16-bit movs range of 0 to 255, so it uses the 32-bit mov.w with a modified immediate, then mul.w. The .w suffix is objdump telling you the instruction is a wide encoding; the matching .n for narrow is printed only on branches and in a few ambiguous cases.

Choosing what to disassemble

On a real firmware ELF, -d alone prints a megabyte. Narrow it:

FlagEffect
-dDisassemble code sections only
-DDisassemble all sections, including data. Useful for raw regions that the tool treats as data
--disassemble=mainOnly the symbol main. Available since binutils 2.32
-j .textRestrict to one section
-SIntermix source lines. The ELF needs -g
-CDemangle C++ names
-hPrint the section headers: addresses, sizes, flags
-tPrint the symbol table
-s -j .isr_vectorHex dump the contents of a section, for example the vector table

For one function with its source:

Shell
arm-none-eabi-objdump -d -S --disassemble=scale add.o

With -g in the compile command, this interleaves the lines return x * 1000; above the instructions that implement them, which is the fastest way to learn what a given C construct costs.

The Thumb bit in symbols

Symbol tables for Thumb code can confuse you. When you print -t from a linked ELF, a function's value is shown with the low bit set, for example 08000141, while the instruction is really at 08000140. That is the Thumb bit, and it is the same bit that you need set in any address you branch to. The disassembly listing uses the real address, 08000140. If you compare an address from the -t list with one from a fault dump, subtract the low bit first.

Data hiding in code: literal pools and mapping symbols

Compilers put constants that do not fit an instruction into a literal pool right after the code that uses them. A disassembler must not decode those as instructions. objdump knows where they are through mapping symbols, tiny local symbols named $t for Thumb code and $d for data, which the assembler emits. Print them with -t and you see lines such as 00000010 l .text 00000000 $d.

A function that loads a 32-bit address looks like this:

Text
00000000 <get_reg>:
   0:	4b01      	ldr	r3, [pc, #4]	@ (8 <get_reg+0x8>)
   2:	6818      	ldr	r0, [r3, #0]
   4:	4770      	bx	lr
   6:	bf00      	nop
   8:	40021000 	.word	0x40021000

The ldr r3, [pc, #4] reads from address 8, 4 bytes past the aligned pc value; objdump helps by printing the target in the comment. The word at 8 is shown as .word 0x40021000, not as an instruction, thanks to the $d mapping symbol. The nop at 6 is padding to get the pool word-aligned. If you see a nonsense instruction sequence in a listing, you are probably looking at data that the tool missed, which happens when the mapping symbols were stripped.

Reading the vector table

The first sixteen words of flash on a Cortex-M are not code. They are the initial stack pointer and the exception handler addresses. Disassembling them gives garbage, so dump them as data:

Shell
arm-none-eabi-objdump -s -j .isr_vector firmware.elf
Text
Contents of section .isr_vector:
 8000000 00500020 41010008 51010008 53010008  .P. A...Q...S...
 8000010 55010008 57010008 59010008 00000000  U...W...Y.......

Each word is little-endian. The first, 00500020, is 0x20005000: the initial stack pointer. The second, 41010008, is 0x08000141, the reset handler with its Thumb bit set. Match those against -t output to name the handlers.

Common checks

Three habits earn their keep:

  1. After changing an optimization flag, disassemble the function again. If instructions you wrote in assembly vanish, the compiler had inlined or removed them.

  2. To check a hot loop, count its instructions between the label and the backward branch. Timing depends on the core, the clock and the flash wait states, so do not convert instructions to nanoseconds from the listing alone. Read cycle counts for the instructions in the core's technical reference manual.

  3. Compare a stripped release image's size against arm-none-eabi-size. If .text grew, the listing shows which function did.

Annotating the listing with source and symbols

Two more flags make a listing readable on a large image. -l adds file names and line numbers between instructions, which is cheaper than -S when you only need to know which line a faulting address came from. --start-address=0x08000140 --stop-address=0x08000180 limits the output to a range, which is how you look at the dozen instructions around a fault. When you only have an address from a crash log, this is the shortest route: pass the range and read the instruction at that address, then look backward to find which register it used.

For a linked image with symbols, also run arm-none-eabi-nm -S --size-sort -r firmware.elf | head to see the biggest functions first. If the one you are chasing is not there, look for a name starting with __ that the toolchain generated, such as a libgcc division helper. These appear in listings as bl __aeabi_idiv on a core without hardware divide, and they explain a good share of "mystery" calls in M0 firmware.

Cross-checking with the map file

The linker map, produced with -Wl,-Map=firmware.map, tells you which object file each function came from and the section it landed in. Use it together with the listing: the listing says what the instructions are, the map says why they are there. If a function appears twice, once in your code and once in a library, the map shows which copy the linker picked and why. That saves long hunts through a disassembly that turns out to be of the wrong copy.

Limits of reading disassembly

A listing shows the code that exists, not the code that runs. Interrupt handlers called through the vector table never appear as callees in the listing, so you will not find SysTick_Handler by following bl instructions from main. Function pointers and jump tables hide edges. And optimized code reorders and merges lines, so a source-interleaved view with -S can jump around. Use it as a map, and use a debugger with real registers to check what the core actually did.

Also inToolchains
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.