About semihosting

Semihosting enables code running on a target system, the model, to interface with a debugger running on a host system, the computer, and to use its input and output (I/O) facilities. This means that you can interact with a model or microcontroller that may not possess I/O functionality.

In Building your first embedded image, we used a printf() call in the code to display the "Hello World" message. This printf() call triggers a request to a connected debugger through the library function _sys_write. To see how this works, you can use fromelf to inspect the compiled code, as shown in the following instruction:

$ fromelf --text -c __image.axf --output=disasm.txt

This command generates a disassembly of __image.axf in the file disasm.txt. Within the disassembly, look at _sys_write, which contains a HLT instruction:

_sys_write
    0x00003a74:    d100c3ff    ....    SUB      sp,sp,#0x30
    0x00003a78:    a9027bfd    .{..    STP      x29,x30,[sp,#0x20]
    ...
    0x00003a9c:    d45e0000    ..^.    HLT      #0xf000
    ...
    0x00003aa8:    d65f03c0    .._.    RET

The debugger detects this halt as a semihosting operation, and interprets the _sys_write as a request to output to the console.

You can check if you are using semihosting by adding __asm(".global __use_no_semihosting\n\t"); to main(). Linking the image will now throw an error for any functions that use semihosting.

Previous Next