ST Cube GCC: how to override a function not defined as weak (no sources) - Page 1
Forum section: electronics / general

author: peter-h
author_rank: Super Contributor
timestamp: July 24, 2022, 11:47:17 am
content: The context is herehttps://www.eevblog.com/forum/microcontrollers/32f4-hard-fault-trap-how-to-track-this-down/msg4318204/#msg4318204andhttps://gist.github.com/thomask77/3a2d54a482c294beec5d87730e163bddI need to replace empty mutex functions with real ones.I don't have the sources to the code making those calls (printf stuff, using the heap and using mutexes to make that thread-safe, except the mutexes were never implemented) so it is just the symbols that have been defined in the linker input and I need to override them.I would prefer to make this override specific to just the 10 or so functions, because in general Idowant to get a multiple definition error.

author: langwadt
author_rank: Super Contributor
timestamp: July 24, 2022, 01:20:23 pm
content: https://stackoverflow.com/questions/46444052/how-to-wrap-functions-with-the-wrap-option-correctly?

author: Nominal Animal
author_rank: Super Contributor
timestamp: July 24, 2022, 01:22:36 pm
content: You can useobjcopy -Wto weaken the symbol in the object file (dynamic or static library) so you can override it effortlessly. Or you can useobjcopy --redefine-symoldname=newnameto redefine one, in case you want to interpose a symbol with your own code and need access to the old symbol too.

author: ejeffrey
author_rank: Super Contributor
timestamp: July 24, 2022, 05:56:18 pm
content: Are you trying to change an already linked executable or do you have libraries? You can just omit the entire .o file with the lock functions you want to exclude.Objcopy would work if you only wanted to change a few symbols in a given translation unit. However functions within the same translation unit will usually call directly or inline the calls so this isn't a guarantee.

author: peter-h
author_rank: Super Contributor
timestamp: July 24, 2022, 06:46:44 pm
content: I have libs but don't have printf.o. I see many lib*.a files but can't find the config for selecting these. It is deep in there somewhere.

author: ejeffrey
author_rank: Super Contributor
timestamp: July 24, 2022, 07:33:36 pm
content: .a files are just ar archives of .o files. It's not printf.o you want to remove its the one with the lock primatives.

author: peter-h
author_rank: Super Contributor
timestamp: July 24, 2022, 08:00:39 pm
content: I think I need to remove those symbols from the symbol table.Well, that is how I used to do it in the Z80 days, with sedI think I have found the C sourcehttps://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/stdio/vfprintf.c;h=c1483c0ac62b7e74d087760f085da6ccde6b2d41;hb=65879410256b5f477ff56b0fdab7007fdfe0ec7fCan't be sure but it contains the mutex stuff I recognise. ST compiled it with those functions containing just a return.

author: peter-h
author_rank: Super Contributor
timestamp: July 26, 2022, 05:52:25 am
content: I have apparently managed to use objcopy to weaken the list of symbols involved, according to a dump from objdump.Used objcopy @cmdfile.txt libc.a libc-weakened.aCode:--weaken-symbol __retarget_lock_init--weaken-symbol __retarget_lock_init_recursive--weaken-symbol __retarget_lock_close--weaken-symbol __retarget_lock_close_recursive--weaken-symbol __retarget_lock_acquire--weaken-symbol __retarget_lock_acquire_recursive--weaken-symbol __retarget_lock_try_acquire--weaken-symbol __retarget_lock_try_acquire_recursive--weaken-symbol __retarget_lock_release--weaken-symbol __retarget_lock_release_recursive--weaken-symbol __lock___arc4random_mutex--weaken-symbol __lock___dd_hash_mutex--weaken-symbol __lock___tz_mutex--weaken-symbol __lock___env_recursive_mutex--weaken-symbol __lock___malloc_recursive_mutex--weaken-symbol __lock___at_quick_exit_mutex--weaken-symbol __lock___atexit_recursive_mutex--weaken-symbol __lock___sfp_recursive_mutex--weaken-symbol __lock___sinit_recursive_mutexobjdump showsCode:00000000 w F .text.__retarget_lock_init00000004 __retarget_lock_init00000000 w F .text.__retarget_lock_init_recursive00000004 __retarget_lock_init_recursive00000000 w F .text.__retarget_lock_close00000004 __retarget_lock_close00000000 w F .text.__retarget_lock_close_recursive00000004 __retarget_lock_close_recursive00000000 w F .text.__retarget_lock_acquire00000004 __retarget_lock_acquire00000000 w F .text.__retarget_lock_acquire_recursive00000004 __retarget_lock_acquire_recursive00000000 w F .text.__retarget_lock_try_acquire00000008 __retarget_lock_try_acquire00000000 w F .text.__retarget_lock_try_acquire_recursive00000008 __retarget_lock_try_acquire_recursive00000000 w F .text.__retarget_lock_release00000004 __retarget_lock_release00000000 w F .text.__retarget_lock_release_recursive00000004 __retarget_lock_release_recursive00000000 w O .bss.__lock___arc4random_mutex00000001 __lock___arc4random_mutex00000000 w O .bss.__lock___dd_hash_mutex00000001 __lock___dd_hash_mutex00000000 w O .bss.__lock___tz_mutex00000001 __lock___tz_mutex00000000 w O .bss.__lock___env_recursive_mutex00000001 __lock___env_recursive_mutex00000000 w O .bss.__lock___malloc_recursive_mutex00000001 __lock___malloc_recursive_mutex00000000 w O .bss.__lock___at_quick_exit_mutex00000001 __lock___at_quick_exit_mutex00000000 w O .bss.__lock___atexit_recursive_mutex00000001 __lock___atexit_recursive_mutex00000000 w O .bss.__lock___sfp_recursive_mutex00000001 __lock___sfp_recursive_mutex00000000 w O .bss.__lock___sinit_recursive_mutex00000001 __lock___sinit_recursive_mutexNow the Q is where to put the modified library. If I just replace the one in c:\ST\.... it will get overwritten next time Cube is installed. I have found a place:project / properties / c/c++ build / settings / mcu g++ linker / libraries, and add the path to "libraries"but, what a surprise, that doesn't work. The linker command line is correctarm-none-eabi-g++ -o "xxxxxx.elf" @"objects.list" -l"C:/xxxxxx/Project1/LIBC/libc-weakened.a" -mcpu=cortex-m4 -T"C:\xxxxxx\Project1\LinkerScript.ld" --specs=nosys.specs -Wl,-Map="xxxxxx.map" -Wl,--gc-sections -static -u _printf_float -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -lstdc++ -lsupc++ -Wl,--end-groupbut it doesn't like itc:\st\stm32cubeide_1.10.0\stm32cubeide\plugins\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127\tools\arm-none-eabi\bin\ld.exe: cannot find-lC:/xxxxx/Project1/LIBC/libc-weakened.aUnfortunately I have no other entries in the Libraries box which I could copy.

author: peter-h
author_rank: Super Contributor
timestamp: July 27, 2022, 06:24:10 am
content: Getting the filename onto the linker command line was unsuccessful but after a day's trial and error I managed to get this to work, in ST Cube v1.10.1That uses the "unix convention" that the file must start with "lib" and that gets stripped, and the filetype gets stripped, and what is left is what you specify.The other details are herehttps://www.eevblog.com/forum/programming/a-question-on-mutexes-normal-v-recursive-and-printf/msg4323412/#msg4323412

author: peter-h
author_rank: Super Contributor
timestamp: July 27, 2022, 04:28:48 pm
content: I have basically solved this issue - herehttps://www.eevblog.com/forum/programming/a-question-on-mutexes-normal-v-recursive-and-printf/msg4324273/#msg4324273