[SOLVED] Error building project with nested function as parameter in GCC

  • Good afternoon! I am developing a microcontroller project on ARM Cortex-M4, GCC compiler (Release 6.22a). The project includes a library that uses a nested function and is passed as a parameter to another function. When building, the project crashes on this function with the message:

    undefined reference to `__clear_cache'

    I wrote a pseudo-program with a similar implementation, it is built on GCC under Windows, it works, but on GCC ARM the same problem.


    Tell me, where could be the problem?

  • You may need to specify that libgcc gets linked in.


    tl;dr details:


    Nested functions (or rather pointers to nested functions) use a technique called "trampolines" which allocate a small bit of code and data that allows a call through a nested function pointer to do some dynamic fixup (in essence set up the local data for the function) before jumping to the actual function's code. See https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html where it says "GCC implements taking the address of a nested function using a technique called trampolines. This technique was described in Lexical Closures for C++ (Thomas M. Breuel, USENIX C++ Conference Proceedings, October 17-21, 1988)"


    Also see http://fpl.cs.depaul.edu/jriely/447/ass…icles/lexic.pdf


    Anyway, since trampolines are dynamic code, they can require that the instruction cache be flushed, which is handled by the libgcc function `__clear_cache()` (https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html)


    All that said, I'd suggest avoiding nested functions if possible. They are non-standard, easy to misuse, and rarely necessary.

  • Good afternoon!
    I completely agree that it is better not to use nested functions, unfortunately this is part of the library.
    Please tell me which Linker Options is needed to connect libgcc?

  • From https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html:



    You might want to post the details of the options you are currently using (copy/paste the linker command line being used by the IDE) as there might be interactions.

    Do a search for "libgcc" on the linked web page for other details that might be important.

    This information might also be helpful in understanding what the GNU linker is doing:

    https://stackoverflow.com/a/11481258/12711

    > The `-v` option to `gcc` will cause it to dump information about the default options it will use including the library paths and default libraries and object files that will be linked in.
    >
    > If you give the `-Wl,--verbose` option, gcc will pass the `--verbose` to the linker which will dump exactly where it's looking for libraries, including both failed and successful searches.
    >
    > Combine both options, and you'll see exactly what libraries are linked in, and why they're being linked in.
    >
    > gcc -v foo.c -Wl,--verbose

    Edited once, last by mwb1100 (May 5, 2022 at 6:09 PM).

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!