Add a weak definition of mep_di to MeP's libgloss

Schwarz, Konrad
Wed Feb 4 09:11:00 GMT 2009


this is the second recent post which makes questionable use of "weak",
at least to my mind.

Traditional Unix static linkers link the object and library (archive)
files listed in the command line strictly from left to right.  GNU ld
follows this tradition.  On encountering an archive, the members of the
archive are scanned, again from left to right, and all members that
resolve a symbol are extracted and included in the link output.
Extracting members of the archive might create new unresolved symbols.
The process of scanning the archive is repeated until no new members can
be extracted.  However, once processed, this archive is not revisited,
even if later object files have unresolved symbols that could be
resolved by this library (unless the library is mentioned a second time
on the command line, to the right of these later object files).

As an aside, these semantics of processing of archive members from left
to right is not modified by the existence of the archive's symbol table.
This is why GNU ar still has the "b" (before), "a" (after), "i"
(insert), and "m" (move) keys.  The symbol table is merely an

If you wish to override the definition of a symbol, add an object
(possibly in an archive, if you are sure that an unresolved reference to
this symbol exists before the library is processed) defining that symbol
before the library containing the overridden definition.

Object files are included in a link integrally (with the exception of
segment garbage collection, which requires extra effort from both the
implementer of the module and from the invoker of the linker).  That is,
either all of the definitions are included or none of them are.  Thus,
adding weak definitions to existing members (object files) will bloat
the linker's output (perhaps only slightly, as in the case below) when
that member is extracted for other reasons.  

The correct solution is to add a separate archive member for each
"fall-back" definition.  These definitions do not profit from having the
"weak" attribute.


Konrad Schwarz

> -----Original Message-----
> From: 
> [] On Behalf Of Nick Clifton
> Sent: Wednesday, February 04, 2009 09:00
> To:
> Cc:
> Subject: RFA: Add a weak definition of mep_di to MeP's libgloss
> Hi DJ,
>   Whilst trying to compile some of the gcc testsuite for the MeP
>   target I ran into a problem with libgloss.  Specifically the
>   _handler_RESET function in libgloss/mep/h_reset.c references a
>   function called "mep_di" which does not seem to be defined
>   anywhere.  My guess was that this is a board specific function to
>   disable interrupts, but since I was only using the simulator I was
>   not really concerned with interrupt handling.
>   So I created the patch below to add a weak, empty definition of the
>   mep_di function.  With this patch applied I was able to compile and
>   link the gcc testsuite testcases.
>   What do you think.  Is the patch suitable for application to the
>   libgloss sources or should there be some other way of providing the
>   mep_di function ?
> Cheers
>   Nick
> libgloss/ChangeLog
> 2009-02-04  Nick Clifton  <>
> 	* mep/h_reset.c: Add a weak definition of mep_di.
> Index: libgloss/mep/h_reset.c
> ===================================================================
> RCS file: /cvs/src/src/libgloss/mep/h_reset.c,v
> retrieving revision 1.1
> diff -c -3 -p -r1.1 h_reset.c
> *** libgloss/mep/h_reset.c	8 Feb 2007 21:22:05 -0000	1.1
> --- libgloss/mep/h_reset.c	4 Feb 2009 07:58:53 -0000
> ***************
> *** 13,18 ****
> --- 13,22 ----
>    * the express permission of Red Hat, Inc.
>    */
> + #pragma weak mep_di
> + void mep_di (void) __attribute__((section(".hwinit")));
> + void mep_di (void) { }
> +   
>   #pragma weak _handler_RESET
>   void _handler_RESET(void) __attribute__((section(".hwinit")));

More information about the Newlib mailing list