Author Topic: ST Cube GCC: how to override a function not defined as weak (no sources)  (Read 3723 times)

0 Members and 1 Guest are viewing this topic.

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 4096
  • Country: gb
  • Doing electronics since the 1960s...
The context is here
https://www.eevblog.com/forum/microcontrollers/32f4-hard-fault-trap-how-to-track-this-down/msg4318204/#msg4318204
and
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.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4698
  • Country: dk
 
The following users thanked this post: peter-h

Online Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6841
  • Country: fi
    • My home page and email address
You can use objcopy -W 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.
 
The following users thanked this post: peter-h, SiliconWizard

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 3886
  • Country: us
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.
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 4096
  • Country: gb
  • Doing electronics since the 1960s...
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.


Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 3886
  • Country: us
.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.
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 4096
  • Country: gb
  • Doing electronics since the 1960s...
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.
« Last Edit: July 25, 2022, 09:36:36 am by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 4096
  • Country: gb
  • Doing electronics since the 1960s...
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:/KDE485/Project1/LIBC/libc-weakened.a

Unfortunately I have no other entries in the Libraries box which I could copy.
« Last Edit: July 26, 2022, 12:42:18 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 4096
  • Country: gb
  • Doing electronics since the 1960s...
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



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

Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 4096
  • Country: gb
  • Doing electronics since the 1960s...
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf