On Tue, Nov 17, 2026 at 16:02:36 +0100, Richard Biener via Gcc wrote:
> On Tue, Nov 17, 2026 at 11:42 AM Basile STARYNKEVITCH
> <[email protected]> wrote:
> > Assume a recent GCC (15 or later) on a 64 bits processor and Linux
> > system (eg ARM64 or AMD64)
> >
> > Assume compilation of a single C or C++ file (foo.c or foo.cxx) into a
> > shared (dlopenable) object foo.so (so with options -O2 -fPIC -shared)
> >
> > Assume also that no function is declared with attributes (other than
> > format).
> >
> > Is there in practice an order of the addresses (as obtained by dlsym)
> > of the external functions of foo.so?
> >
> > This could be useful when using dladdr and dlsym
> >
> > I was guessing that in practice if an external function f is defined
> > textually before function g then its dlsym-ed address is less than the
> > dlsym-ed address of g
> >
> > In other words, when does the GCC compiler reorder the external
> > functions in a shared object
>
> GCC orders in reverse topological order IIRC. You can use
> -fno-toplevel-reorder to avoid this.
However, optimizers like BOLT can jumble things up as necessary:
https://github.com/llvm/llvm-project/blob/main/bolt/README.md
Though I suppose `-ffunction-sections` can be *not* provided to certain
objects to ensure they move as a group.
--Ben