This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

#pragma WEAK not working as expected

Other Parts Discussed in Thread: HALCOGEN

I have a project where I use code generated by HalCoGen, and I'm only able to override some of the weakly defined functions. I posted in the HalCoGen forum, but it looks like there's a compiler issue here. https://e2e.ti.com/support/microcontrollers/hercules/f/312/p/510723/1853529

I'm using the TI compiler 5.2.0 and HalCoGen 4.05.02. The problem is illustrated with 3 files :

sys_selftest.c (generated by HalcoGen)

#pragma WEAK(selftestFailNotification)
void selftestFailNotification(uint32 flag) { }

void ccmSelfCheck(void)
{
[code] selftestFailNotification(CCMSELFCHECK_FAIL1);
[code] }

notification.c (generated by HalCoGen):

 #pragma WEAK(spiNotification)
 void spiNotification(spiBASE_t *spi, uint32 flags) { }

myFile.c (written by me)

void selftestFailNotification(uint32 flag) { }
void spiNotification(spiBASE_t *spi, uint32 flags) { }

[call spi and self-test functions]

In this setup, my overridden spiNotification() gets called, but selftestFailNotification does not. If I move selftestFailNotification to notification.c, then my overridden one gets called. It appears that calls to selftestFailNotification from the same file as the definition, causes it to be resolved immediately, not at link time, like with spiNotification. 

According to the manual: A weak definition does not change the rules by which object files are selected from libraries. However, if a link set contains both a weak definition and a non-weak definition, the non-weak definition is always used. 

In my case the non-weak definition is not used for calls from sys_selftest.c. While trying things out, I discovered that calls to selftestFailNotification from outside of sys_selftest.c will use the definition in myFile.c, even while calls from within sys_selftest.c use the one in the same file.


  • This is the same issue as e2e.ti.com/.../510739. We were both entering it at the same time.
  • You have what appears to be an extra, strong definition of selftestFailNotification in myFile.c. Remove the body of the function and change it to a prototype. Now do you get the results you expect?
  • The selftestFailNotification is the one that I want to be called. The weak one in sys_selftest.c should be overridden, so that ccmSelfCheck calls the one in myFile.c, not the one in sys_selftest.c.
  • You need to make sure you are using the option --gen_func_subsections=on
  • That worked. It's exactly what I needed. Thanks.
  • Okay, now that we've verified that was the problem, I know what's going on. The full story is a long one, so I'll give you the short version. Before TI ARM compiler version 15.12.0.LTS, the assembler would always prefer a call to a function in the same subsection, regardless of whether it was weak. So, if you defined a weak function in the same section as a function that called it, you'd always get a call to the weak function. One way to work around this is to use --gen_func_subsections to make the compiler keep those functions in different subsections. It can be argued that the assembler should not have preferred the weak function. This failing is more of a systemic design flaw than a bug we can easily pin down to either the compiler or assembler.

    Enhancement request SDSCM00052445, implemented in 15.12.0.LTS, changes the behavior of the assembler in a way that this problem doesn't happen. This behavior was not back-ported to earlier compiler tools versions, and I doubt it ever will be.