__attribute__((used)) not working..

Bug #1868064 reported by Dag Midling Larsen
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
GNU Arm Embedded Toolchain
New
Undecided
Unassigned

Bug Description

Hi

This could be related to bugs:
#1403649
#1747966
#1788156

I'm using toolchain Version 9-2019-q4-major, binary package on Windows 10. Sorry I don't have a "preprocessed testcase" for you.

I'm building more than one application for one board (different use-cases). Say I'm building:
third_party_lib_a,
third_party_lib_b,
third_party_lib_c,
user_lib,
user_app_a,
user_app_b,
user_app_c

A function is declared as weak in one of the third party lib's.
I try to "override" the weak function in user_lib, and have added __attribute__((used)).
Still in the user_app's the weak function is linked in.

If I instead move the module to all user_app's linking, the correct function is used.

At the same I could mention that if a module containing e.g. the function _kill is put in user_lib the applications don't link. _kill is referenced by the tool chain libraries, but seems to be omitted before this "is realized" in the linking process. Adding __attribute__((used)) doesn't help. The module must be in all the app projects.

Best Regards,
Dag

Revision history for this message
David Brown (davidbrown) wrote :

The "used" attribute does not do quite what you seem to think it does. It tells the compiler that the function might be used even if the compiler doesn't see it (such as from assembly) - so the compiler will generate the code for the function even if it can't see it being used. If your code already calls the function name somewhere, the "used" attribute is redundant.

Your issue is not with compilation, and can't be solved by compile-time attributes. It is in linking. I think what you are seeing is that a weak symbol definition in one library is not being overridden by a symbol definition in another library - but it does get overridden by a definition in an object code file. Definitions in libraries are always considered a bit lower priority than definitions in object code files, and resolution of symbols in libraries and object code files is done in different orderings.

I am not sure of the exact rules here. The binutils "ld" manual might give you ideas. As a starting point, you might change your linker file to load the user_lib library first, before the third_party_lib files.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.