Simone Atzeni via llvm-dev
2017-Jan-08 05:34 UTC
[llvm-dev] Weak symbol function in shared library and strong symbol function in main problem
Hi, I have a pass that add a function in the module where the “main” is. Then, I have the same function declared as weak symbol in a shared library that I load at runtime with LD_PRELOAD. When I run a program the weak symbol function gets called over the function was added by the pass. Is it a correct behavior? Is there a way to call the strong symbol function when it’s present in the module? In other words, when I don’t apply the pass I want the program to call the weak symbol function, otherwise when I apply the pass I want the program to call the function added by the pass. Thanks! Best, Simone
Mehdi Amini via llvm-dev
2017-Jan-08 06:31 UTC
[llvm-dev] Weak symbol function in shared library and strong symbol function in main problem
> On Jan 7, 2017, at 9:34 PM, Simone Atzeni via llvm-dev <llvm-dev at lists.llvm.org> wrote: > > Hi, > > I have a pass that add a function in the module where the “main” is. Then, I have the same function declared as weak symbol in a shared library that I load at runtime with LD_PRELOAD. > When I run a program the weak symbol function gets called over the function was added by the pass. > > Is it a correct behavior? Is there a way to call the strong symbol function when it’s present in the module?I don’t expect this, but there are possible subtleties: for instance who is calling the function? Is the call site in the dylib? Is it in the same translation unit as the definition? (It could have been inlined for instance). Is the library built with protected/hidden visibility? Which platform are you on? Also, that’s not really directly related to LLVM or the fact that you add the symbol in a pass. You should be able to reproduce with two trivial files: main.c that defines main() and foo(), and lib.c that defines a weak foo() and is built as a library. Can you reproduce with such setup? Otherwise something else is going on in your build… — Mehdi> In other words, when I don’t apply the pass I want the program to call the weak symbol function, otherwise when I apply the pass I want the program to call the function added by the pass. > > Thanks! > Best, > Simone > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reid Kleckner via llvm-dev
2017-Jan-08 18:57 UTC
[llvm-dev] Weak symbol function in shared library and strong symbol function in main problem
ELF dynamic loaders generally do not distinguish between weak and strong symbols. They only look at the visibility, which can be internal, hidden, default, or protected. If you're trying to allow the main program to customize some aspect of your LD_PRELOAD'ed tool, you probably want to use an extern weak symbol. I forget the details on how to do this, but it looks something like this: extern void __attribute__((weak)) myhook(void); ... if (&myhook) { myhook(); } On Sat, Jan 7, 2017 at 9:34 PM, Simone Atzeni via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > I have a pass that add a function in the module where the “main” is. Then, > I have the same function declared as weak symbol in a shared library that I > load at runtime with LD_PRELOAD. > When I run a program the weak symbol function gets called over the > function was added by the pass. > > Is it a correct behavior? Is there a way to call the strong symbol > function when it’s present in the module? > In other words, when I don’t apply the pass I want the program to call the > weak symbol function, otherwise when I apply the pass I want the program to > call the function added by the pass. > > Thanks! > Best, > Simone > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170108/db8dcea3/attachment.html>
Mehdi Amini via llvm-dev
2017-Jan-08 20:10 UTC
[llvm-dev] Weak symbol function in shared library and strong symbol function in main problem
DiOn Jan 8, 2017, at 10:57 AM, Reid Kleckner via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > ELF dynamic loaders generally do not distinguish between weak and strong symbols. They only look at the visibility, which can be internal, hidden, default, or protected.Was it always the case? The only thing I find in the doc is the description of the environment variable: LD_DYNAMIC_WEAK "(glibc since 2.1.91) Allow weak symbols to be overridden (reverting to old glibc behaviour)." Just curious… — Mehdi> > If you're trying to allow the main program to customize some aspect of your LD_PRELOAD'ed tool, you probably want to use an extern weak symbol. I forget the details on how to do this, but it looks something like this: > > extern void __attribute__((weak)) myhook(void); > ... > if (&myhook) { > myhook(); > } > > On Sat, Jan 7, 2017 at 9:34 PM, Simone Atzeni via llvm-dev <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > Hi, > > I have a pass that add a function in the module where the “main” is. Then, I have the same function declared as weak symbol in a shared library that I load at runtime with LD_PRELOAD. > When I run a program the weak symbol function gets called over the function was added by the pass. > > Is it a correct behavior? Is there a way to call the strong symbol function when it’s present in the module? > In other words, when I don’t apply the pass I want the program to call the weak symbol function, otherwise when I apply the pass I want the program to call the function added by the pass. > > Thanks! > Best, > Simone > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev> > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170108/544b23b1/attachment.html>