Link Order: Using Multiple Definitions with ARM GNU Linker and Eclipse


Sometimes I have ‘multiple definitions’ in my projects: this means that I have functions defined in one source files, and I need to ‘overwrite’ one or more with a version in another source file.  For example I have a source file with utility functions (Utility.c), and I want to overwrite some of these functions with a different implementation in a different file (MyUtility.c). How can I do this?

Link Order

Typically this is done with changing the link order: changing the order the linker ‘sees’ the object files while linking. Yes, this is possible with a pure make file approach where I have complete control. But not easily with an Eclipse build tools integration: the linker panels do not allow to change or affect the link order of files :-(.

Link Order Feature in CodeWarrior for MCU10.5

Well, only until I have realized that the Eclipse based CodeWarrior for MCU10.5 has this feature implemented: I can change the link order :-).

I can find this option in the linker settings as ‘Link Order’. Below is a screenshot for ARM GNU linker settings in CodeWarrior for MCU10.5:

Customize link input order

Customize link input order

If I enable that option, I can move up/down files in the order for the linker:

Customizing linker input order

Customizing linker input order

Placing a file above another one, means it takes precedence (priority). I remember it that way: The higher in the list, the higher the priority :-).

Error: Multiple Definitions

Having multiple definitions can end up in a ‘multiple definition of‘ error, like this one for ARM GNU linker:

Multiple Definition Error

Multiple Definition Error

The linker is right: I do have multiple definitions :-). So how to get rid of this? The answer is to add the

-z muldefs

option to the ARM GNU Linker flags:

Allow multiple definitions

Allow multiple definitions

💡 See this page for more linker options.

With this, the linker is happy :mrgreen:.

❗ There is just one (potential) issue: I see that the list of files is passed to the linker on the command line (and not in a separate argument file). This means that the current implementation might hit a wall if the command line length exceeds the limits of the host operating system (8196 characters for Windows as far as I can tell).

Summary

The new linker panels in CodeWarrior for MCU10.5 allow me to change and modify the link order. That way I have the possibility to overwrite functions or objects defined in other files.

Happy Link Ordering 🙂

3 thoughts on “Link Order: Using Multiple Definitions with ARM GNU Linker and Eclipse

  1. I believe there is a better approach:
    You can define the functions you want to override as weak. If the linker finds another implementation of this function it will replace it with the strong variant (which is the default if you do not declare it as weak). This way your source code contains a hint that this might be overridden and you can reuse it better as you no longer have to mangle with the linker order.

    Regards

    Matthias

    See [1] and [2] for more information.
    [1] http://en.wikipedia.org/wiki/Weak_symbol
    [2] http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

    Like

    • Hi,
      yes, good advise, and I’m using this approach from time to time too. But it only works if I can (and I’m allowed) to change the source to add that weak attribute.
      thanks again!

      Like

  2. Pingback: FreeRTOS, malloc() and SP check with GNU Tools | MCU on Eclipse

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s