Solved

use of variable attribute

Posted on 2013-12-02
4
228 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 30

Accepted Solution

by:
Zoppo earned 300 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now