2

For unit testing I'd like to replace a function from "outside". Normally, I'm using the wrapping mechanism - but unfortunately this does not work for calls to the function from within the same compilation unit.

My idea was to mark the function as "weak" so I am able to reimplement it in a testing application. Generally this works using the following code:

File myfunctions.c (this is the code under test):

#include "myfunctions.h"
int weakFunction(int param) __attribute__((weak));

int weakFunction(int param)
{
  return 2*param;
}

int myfunction(int param)
{
  int result = weakFunction(param);
  return (result == (2*param)) ? 1:0;
}

File main.c

#include "myfunctions.h"
int weakFunction(int param)
{
  return 3*param;
}

int main()
{
  return myfunction(5);
}

This example works as expected - when I remove weakFunction from main.c, the program returns 1, when I add weakFunction the program returns 0. Looks good at this point.

But as soon as I change the order within myfunctions.c as follows, the resulting program crashes with a segmentation fault:

File myfunctions.c (modified order):

#include "myfunctions.h"
int weakFunction(int param) __attribute__((weak));

int myfunction(int param)
{
  int result = weakFunction(param);
  return (result == (2*param)) ? 1:0;
}

int weakFunction(int param)
{
  return 2*param;
}

Any idea? What could be the reason for the crash?

I am using GCC 4.8.1 (MinGW w64 build) on Windows 7.

Thanks for any help! Florian

  • 1
    a bit unclear what you're doing. do you compile both `myfunctions.c` and `main.c` ? if you have two functions with same names in different modules, at least one of them should be static, otherwise you'd get compile error – mangusta Apr 10 '14 at 07:05
  • run it with gdb, it will tell you where it crashes. – Jabberwocky Apr 10 '14 at 07:09
  • GDB shows the crash at the call to weakFunction (within myfunction()). @mangusta: That's why the function is "weak", it allows to have the same symbol "overwritten" with a strong symbol which is used during likage. – trapperjohn Apr 10 '14 at 07:14
  • 1
    Cannot reproduce the problem on Linux (Debian 7) with GCC 4.7.2 or Fedora 19 with GCC 4.8.2. So I guess the code itself is OK. – Lee Duhem Apr 10 '14 at 07:15
  • Thanks for inspiration... actually I cannot reproduce this myself on Ubuntu with GCC 4.6.3 – trapperjohn Apr 10 '14 at 07:21
  • Just tried to run the code from a Cygwin installation (GCC 4.5.3) : no problems. So it seems that it is MinGW's fault :( – trapperjohn Apr 10 '14 at 08:36
  • Try adding the weak attribute in the function definition. – cup Apr 10 '14 at 09:33
  • I was too quick - Cygwin has the same problem. Actually when reading the "weak" attribute documentation, it seems that Windows targets are generally not supported: > "Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker." (okay, comments cannot be formatted?) – trapperjohn Apr 10 '14 at 10:26
  • Getting segfault on a weak symbol using an ubuntu docker container. tried gcc 5,6,7, and 8. No help. – Todd Flanders Feb 06 '20 at 16:32

0 Answers0