EEVblog Electronics Community Forum

Products => Computers => Programming => Topic started by: peter-h on July 24, 2022, 11:47:17 am

Title: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: peter-h on July 24, 2022, 11:47:17 am
The context is here
https://www.eevblog.com/forum/microcontrollers/32f4-hard-fault-trap-how-to-track-this-down/msg4318204/#msg4318204 (https://www.eevblog.com/forum/microcontrollers/32f4-hard-fault-trap-how-to-track-this-down/msg4318204/#msg4318204)
and
https://gist.github.com/thomask77/3a2d54a482c294beec5d87730e163bdd (https://gist.github.com/thomask77/3a2d54a482c294beec5d87730e163bdd)

I 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 I do want to get a multiple definition error.
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: langwadt on July 24, 2022, 01:20:23 pm
https://stackoverflow.com/questions/46444052/how-to-wrap-functions-with-the-wrap-option-correctly ?
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: Nominal Animal on July 24, 2022, 01:22:36 pm
You can use objcopy -W (https://www.man7.org/linux/man-pages/man1/objcopy.1.html) to weaken the symbol in the object file (dynamic or static library) so you can override it effortlessly.  Or you can use objcopy --redefine-sym oldname=newname to redefine one, in case you want to interpose a symbol with your own code and need access to the old symbol too.
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: ejeffrey on July 24, 2022, 05:56:18 pm
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.
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: peter-h on July 24, 2022, 06:46:44 pm
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.

(https://peter-ftp.co.uk/screenshots/202207243115104719.jpg)
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: ejeffrey on July 24, 2022, 07:33:36 pm
.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.
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: peter-h on July 24, 2022, 08:00:39 pm
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 sed :)

I think I have found the C source
https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/stdio/vfprintf.c;h=c1483c0ac62b7e74d087760f085da6ccde6b2d41;hb=65879410256b5f477ff56b0fdab7007fdfe0ec7f
Can't be sure but it contains the mutex stuff I recognise. ST compiled it with those functions containing just a return.
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: peter-h on July 26, 2022, 05:52:25 am
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.a

Code: [Select]
 --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_mutex

objdump shows

Code: [Select]
00000000  w    F .text.__retarget_lock_init	00000004 __retarget_lock_init
00000000  w    F .text.__retarget_lock_init_recursive 00000004 __retarget_lock_init_recursive
00000000  w    F .text.__retarget_lock_close 00000004 __retarget_lock_close
00000000  w    F .text.__retarget_lock_close_recursive 00000004 __retarget_lock_close_recursive
00000000  w    F .text.__retarget_lock_acquire 00000004 __retarget_lock_acquire
00000000  w    F .text.__retarget_lock_acquire_recursive 00000004 __retarget_lock_acquire_recursive
00000000  w    F .text.__retarget_lock_try_acquire 00000008 __retarget_lock_try_acquire
00000000  w    F .text.__retarget_lock_try_acquire_recursive 00000008 __retarget_lock_try_acquire_recursive
00000000  w    F .text.__retarget_lock_release 00000004 __retarget_lock_release
00000000  w    F .text.__retarget_lock_release_recursive 00000004 __retarget_lock_release_recursive
00000000  w    O .bss.__lock___arc4random_mutex 00000001 __lock___arc4random_mutex
00000000  w    O .bss.__lock___dd_hash_mutex 00000001 __lock___dd_hash_mutex
00000000  w    O .bss.__lock___tz_mutex 00000001 __lock___tz_mutex
00000000  w    O .bss.__lock___env_recursive_mutex 00000001 __lock___env_recursive_mutex
00000000  w    O .bss.__lock___malloc_recursive_mutex 00000001 __lock___malloc_recursive_mutex
00000000  w    O .bss.__lock___at_quick_exit_mutex 00000001 __lock___at_quick_exit_mutex
00000000  w    O .bss.__lock___atexit_recursive_mutex 00000001 __lock___atexit_recursive_mutex
00000000  w    O .bss.__lock___sfp_recursive_mutex 00000001 __lock___sfp_recursive_mutex
00000000  w    O .bss.__lock___sinit_recursive_mutex 00000001 __lock___sinit_recursive_mutex

Now 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 correct

arm-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-group

but it doesn't like it

c:\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.a

Unfortunately I have no other entries in the Libraries box which I could copy.
Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: peter-h on July 27, 2022, 06:24:10 am
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.1

(https://peter-ftp.co.uk/screenshots/20220727444681907.jpg)

That 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 here
https://www.eevblog.com/forum/programming/a-question-on-mutexes-normal-v-recursive-and-printf/msg4323412/#msg4323412 (https://www.eevblog.com/forum/programming/a-question-on-mutexes-normal-v-recursive-and-printf/msg4323412/#msg4323412)

Title: Re: ST Cube GCC: how to override a function not defined as weak (no sources)
Post by: peter-h on July 27, 2022, 04:28:48 pm
I have basically solved this issue - here
https://www.eevblog.com/forum/programming/a-question-on-mutexes-normal-v-recursive-and-printf/msg4324273/#msg4324273 (https://www.eevblog.com/forum/programming/a-question-on-mutexes-normal-v-recursive-and-printf/msg4324273/#msg4324273)