public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* In need of help to understand the program behaviour while loading shared libraries and at linking time.
@ 2024-04-28  8:21 ShriHari
  2024-05-03  8:56 ` Florian Weimer
  0 siblings, 1 reply; 2+ messages in thread
From: ShriHari @ 2024-04-28  8:21 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1616 bytes --]

Dear GCC community,

I was working on making an interception library; which I pre-load before
I run the application that I need to use it for. This is intended to
perform some redirection for logging purposes.
In my library, I have the *__attribute__ constructor(()) *to set up the
logging environment that is needed to be run *before *any resolutions to
the calls defined in my library are made.
Now, my assumption was that at runtime, this constructor will be called
before any resolutions have been made to the functions that I have defined
in this library. For example, let's say that I was intercepting the
*ioctl *function.
My assumption was that the dynamic linker would only resolve any calls (and
call them) ONLY after the *__attribute__ constructor(())* has run; or
at-least run the *ctor *when it is found. But to my surprise, it happens to
be the case that the *ctor *is delayed (17-30 milliseconds).
I have made attempts to understand the stuff that happens between the
program getting loaded, but it was challenging to come to any conclusion. I
did watch a cppCon talk by Matt Godbolt which helped me understand the
specifics at this low-level, but I am afraid that I do not understand this
totally. I am a newbie into low-level systems programming, and I am loving
to work on this. Could I please have resources to understand this behaviour
from the community? It would also be great if there is any resolution (or
is it UB) to the behaviour that I asked about.

Thank you so much for your time, and for making the world a better place
with GCC. I truly appreciate it!

Thanks and Regards,
Shrihari H

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: In need of help to understand the program behaviour while loading shared libraries and at linking time.
  2024-04-28  8:21 In need of help to understand the program behaviour while loading shared libraries and at linking time ShriHari
@ 2024-05-03  8:56 ` Florian Weimer
  0 siblings, 0 replies; 2+ messages in thread
From: Florian Weimer @ 2024-05-03  8:56 UTC (permalink / raw)
  To: ShriHari; +Cc: gcc-help

> Now, my assumption was that at runtime, this constructor will be called
> before any resolutions have been made to the functions that I have defined
> in this library. For example, let's say that I was intercepting the
> *ioctl *function.

I'll assume your question is about GNU/Linux and therefore glibc.

That is not how this works.  Relocation completes for all objects, and
only then ELF constructors are invoked.  This two-pass approach is
necessary because otherwise, it would be more likely that unrelocated
code is executed if symbol interposition or underlinking is in play.

If you want to run your own code before relocation processing, you
should look at LD_AUDIT and audit modules.  The Solaris documentation
mostly applies to glibc as well, at least as far as the basics are
concerned.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-03  8:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-28  8:21 In need of help to understand the program behaviour while loading shared libraries and at linking time ShriHari
2024-05-03  8:56 ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).