2

I am currently writing a C project that includes a separate test build.

The tests are different C processes that uses the source code to test every defined function in my code (TDD).

I want monkey patching in those tests. I took some time to see what it could be done and I think the easiest option is to use gcc attribute feature: https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html

For the test build, I would like all of my functions to be weak (IE, I want the same effect as if I would have written __attribute__ (( weak)) in front of any function declaration of my C file.)

I would like to know if there is a way of passing this weak attribute at weak by default as a compilation option rather than doing it manually each time I need it. Right now I am using a macro that test if the build is in test and if so add this line before every function I want. I would rather avoid complexifing the source code just to allow some test features.

This topic on google is polluted by the repetition of the same the same trivial attribute usage, but I can not find any way of setting attribute defaults.

However, I found this book https://link.springer.com/chapter/10.1007%2F978-1-4302-0704-7_4 that may contain the solution but I wont pay 30 buck just in hope for that. So here I am asking the question.

Some may think it's a bad idea but in my scenario it is fine I think. Remember that my test suite is composed of a lot of mono C file linked aginst my code and remember that the normal build wont be impacted by the new test build option.

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
ninjaconcombre
  • 456
  • 4
  • 15
  • FYI, I got the book by Wall and von Hagen. Searching for `weak` doesn't reveal anything relevant to your question. – Nate Eldredge Apr 06 '20 at 18:28
  • Thanks you, did you tried to look for the attribute keyword? – ninjaconcombre Apr 06 '20 at 18:38
  • Nothing useful there either. – Nate Eldredge Apr 06 '20 at 18:44
  • 1
    Thanks again. I have done some more research and if it's not doable throught normal gcc this probably could be done writing a gcc plugin that could set default attributes for functions. But this look like a complexe task. Also I cannot find example on gcc plugin that add line of code. – ninjaconcombre Apr 06 '20 at 18:59

1 Answers1

2

How about following?

  1. You make separate file (lets call it weak.h) with all symbols need to be converted to weak in the form

#pragma weak func1

#pragma weak func2

#pragma weak func3

...

  1. During test compilation you add default include following this

gcc -include weak.h ...

I never tried this approach myself, but doing that your code won't be affected, which seems what you want most

PS

just tried it, for couple of symbols source file seems to work on Ubuntu 18.04 x64

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • Hey, thanks you for this approach :). This is indeed better than manually modyfing the source code, but this is not as good as I wish because I still need to tediously list my monkeypatched functions. So still I wont accept to see if people know other ways. – ninjaconcombre Apr 06 '20 at 18:37
  • Yes, there might be a better way, so lets see if someone could come with better answer. But why do you want to list it manually? You should build list of functions/symbols which goes into your final build, and generate weak.h with simple script. Here is the link to question which might be of interest to you https://stackoverflow.com/questions/60869800/gcc-linker-how-to-generate-a-report-of-per-file-contribution-on-output-section/. You could ask this guy, @EricSun, for his script and reuse it. – Severin Pappadeux Apr 06 '20 at 21:35
  • 1
    @ninjaconcombre By the way, Eric shared his script – Severin Pappadeux Jun 29 '20 at 22:07