Use weak functions in the RTEMS core?

Jonathan Brandmeyer jbrandmeyer at planetiq.com
Wed Apr 8 17:34:11 UTC 2020


On Wed, Apr 8, 2020 at 10:30 AM Sebastian Huber <
sebastian.huber at embedded-brains.de> wrote:

> On 08/04/2020 17:57, Jonathan Brandmeyer wrote:
>
> > There are a couple more disadvantages.
> >
> > Which definition is pulled into the final link depends on the order
> > that the object files are listed on the command-line.  If the weak one
> > is seen first, then the linker will resolve the symbol against the
> > weak definition and it won't even search into subsequent archives for
> > the strong one.  It will merely suppress multiple-definition errors at
> > link time.
> >
> > We ran into this issue in our project when we over-rode some RTEMS
> > weak symbols in one of our static archives.  Since the dependency
> > graph seen by the linker was from one object in librtems.a to another
> > object in librtems.a, the only way we could reliably pull in our
> > override was by listing our archive as an object to be linked instead
> > of as an archive to be searched (ie, named as libour_runtime.a instead
> > of -lour_runtime on the link step command).
>
> For the linker weak functions are like ordinary functions during the
> symbol resolving phase. If the linker finds the weak one first, then it
> is happy and stops searching.


Correct, that's why it behaves this way.


> If you encounter problems like this, then
> weak functions are used for the wrong thing.
>

Exhibit A: zynq_setup_mmu_and_cache.  It is referred to only by the BSP's
startup sequence.  So it is a reference from librtemsbsp to librtemsbsp by
default.  I don't think it's being used for the wrong thing.


>
> I would like to use weak functions with one level of indirection. For
> example an application can use two features A and B. Both use an
> interface C. If only A is used, then C can be implemented via D or E. If
> B is used, then C must be implemented via E. For this you can use a weak
> implementation D of interface C in module of A and a strong
> implementation E in module of B.
>

In the proposed case of using weak references for the heap, which
implementation gets pulled in depends on whether or not the object files
listed on the command line reference the heap or not.  If the only
references are within static archives, then the application will have
difficulty choosing exactly which implementation of the heap gets pulled
in.  librtemsbsp both depends-on and provides implementations of malloc and
free.

It's pretty easy to provide a definition of Init() or POSIX_Init() that
doesn't directly call malloc or free, for example.

Instead of treating weak references as a single level of indirection, I
think you have to treat them as a single overridable interface.  In a
dynamically-linked application, we might try to perform an override using
the LD_PRELOAD feature.  But in a statically-linked application we have to
do it differently.  The overriding archive must be named in full, and it
must be named as an object to be linked instead of a library to be
searched.  Furthermore, an interface can have only one override that ends
up in the linked application.

-- 
Jonathan Brandmeyer
PlanetiQ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20200408/40a3649b/attachment.html>


More information about the devel mailing list