Enable Semihosting in ARM DS 2024, ARM Compiler

Hi

I want to enable semihosting on ARM DS 2024 so I can create an output file. 
Can you tell me how to do that?

I am using Arm Compiler for Embeded 6 toolchain and Gnu Make Builder.

Warm regards


  • Hi again

    The C/C++ libraries that are provided with Arm Compiler for Embedded 6 are semihosted by default, so all the usual C stdio functions will just work.  See

    https://developer.arm.com/documentation/101470/2024-1/Controlling-Target-Execution/Using-semihosting-to-access-resources-on-the-host-computer

    For example:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
        const char *filename = "/home/stheobal/test/out.txt";

        FILE* fp = fopen(filename, "wt");
        if (!fp) {
            printf("Could not open file %s for writing", filename);
        }
        else {
            printf("fopen success\n");
        }

        char str[] = "Hello World!\n";

        fprintf(fp, "%s", str);
        fflush(fp);
    }

    Be aware that semihosting allows code running on a target platform to access files and execute system commands on a host machine, potentially allowing sensitive data on the host to be exposed.  Before you use semihosting, you must ensure that you assess and mitigate the security implications.  For example, a semihosted application running on an FVP model may access files on its host machine, unless semihosting is explicitly disabled in the FVP model.  Similarly, a semihosted application running on a hardware target may access files on a debugger host that is connected by a debug agent such as DSTREAM-ST.  For the latter case, the debugger has new controls to configure the semihosting security policy.  In Arm DS 2024.1 and later, by default, if semihosting is enabled in the debugger, the debugger prevents semihosting from accessing files and executing system commands on the debugger host. The policy can be adjusted with the "set semihosting policy warn|allow|block" command.  See:

    https://developer.arm.com/documentation/101471/6-4-0armds/Arm-Debugger-commands/Arm-Debugger-commands-listed-in-alphabetical-order/set-semihosting

    Hope this helps

    Stephen

  • Hi Stephen and thank you for the information.

    I tried similar code, and I tried yours, too.

    Although I enabled Semihosting and Policy, 

    set semihosting enabled on
    set semihosting policy allow

    It still cannot read and write the file, I get the error:
    Could not open file C:\test\out.txt for writing

    Is there anything that should be changed in ARM DS or in the code?

  • Hi again

    Apologies, the example I gave yesterday contains a couple of errors:

    1) For the fopen mode, the non-ANSI option 't' is not supported.  So "wt" should be just "w" (or "wb" for a binary file). See

    https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst

    2) The fprintf call should (obviously!) have been inside the else clause

    Here's the corrected version, which works for me on Windows, when the debugger is connected to real hardware, and "set semihosting policy allow":

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
        const char *filename = "C:\\Temp\\semihosting\\out.txt";

        FILE* fp = fopen(filename, "w");
        if (!fp) {
            printf("Could not open file %s for writing", filename);
        }
        else {
            printf("fopen success\n");
            char str[] = "Hello World!\n";

            fprintf(fp, "%s", str);
            fflush(fp);
        }
    }

    You shouldn't need to "set semihosting enabled on" explicitly when using the Arm Compiler for Embedded 6 toolchain, however, it is harmless to do so.  The Debugger enables semihosting automatically if either symbols __auto_semihosting or __semihosting_library_function are present in an image. Arm Compiler for Embedded 6 adds __semihosting_library_function automatically to an image at link time if that image uses any semihosting-using functions. If compiling with GCC or LLVM, use an alias symbol definition such as void __auto_semihosting(void) __attribute__ ((alias("main"))); or turn on semihosting support in the Debugger explicitly with "set semihosting enabled on".

    Hope this helps

    Stephen

  • Hi Stephen

    I noticed "wt" and changed it to "w" before I run the file.
    However, I started debugging using your code and another files that I had been created.
    Although I get "fopen success 
    Hello World!" print in the app console, I can see no folder is created, and even if I create the folder and the file, it would not write anything on that.

    Is there any options that I have to check in ARM DS? (for example in debugging section)

    As you know I am using Arm Compiler for Embedded 6 and I did these changes on ARM DS commands:
    set semihosting enabled on
    set semihosting policy allow


    Thank you in advance for your kind cooperation.