Solved

use of variable attribute

Posted on 2013-12-02
4
236 Views
Last Modified: 2013-12-03
I saw at some places in my code the functions were defined in this way

#define USED_ATTR __attribute__ ((used))
return_type  func_name(parameters) const  USED_ATTR  { }

I tried to read this but cannot understand the real use case of this
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348c/BABCJJID.html

What exactly is    __attribute__ ((used))  doing underneath
What does it mean by retain in object file
0
Comment
Question by:perlperl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 300 total points
ID: 39689843
Hi perlperl,

at http://www.keil.com/support/man/docs/armccref/armccref_BABCIGHE.htm you I found this:
Functions marked with __attribute__((used)) are tagged in the object file to avoid removal by linker unused section removal.
Further at http://www.keil.com/support/man/docs/ARMLINK/ARMLINK_Cchhhghb.htm you can find this:
This causes armcc to generate the symbol __tagsym$$used for each of the functions and variables, and ensures that the function or variable is not removed by the linker.
So this means the functions tagged with this attribute aren't removed from the binary by the linkter even if they are not referenced (i.o.w. used) anywhere in the code. I guess this is mostly useful when implementing libraries, but I'm not sure about this, I have no experience with ARM as target platform.

Hope this helps,

ZOPPO
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 39689852
>>What does it mean by retain in object file

The original quote is

This variable attribute informs the compiler that a static variable is to be retained in the object file, even if it is unreferenced.

Usually, the compiler would remove symbols (i.e. variables or functions) that aren't used and marked as 'local' ('static') to save space and avoid redundancy (whether that happens when explicitly not optimizing is up to the compiler, but seems to be the case here). So, by explicitly marking them as 'used', the compiler is instructed to leave them in the created module, regardless if they are references (== used) ur not.
0
 

Author Comment

by:perlperl
ID: 39689967
so its strictly for optimization/performance use case and nothing to do with the symbol defined elsewhere in the library?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 39689972
Such as the docs suggest: Yes. Yet it's not 'optimization', I'd call the category 'Code Generation', since it manipulates the way the compiler creates the output.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question