Solved

use of variable attribute

Posted on 2013-12-02
4
232 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

776 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