?
Solved

use of variable attribute

Posted on 2013-12-02
4
Medium Priority
?
246 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 1200 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 800 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 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

771 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