Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the usecase of gcc's used attribute?

Tags:

c++

c

gcc

clang

icc

#include <stdio.h>

// xyz will be emitted with -flto (or if it is static) even when
// the function is unused

__attribute__((__used__))
void xyz() {
  printf("Hello World!\n");
}

int main() {
  return 0;
}

What do I need this for?

Is there any way I could still reach xyz somehow besides directly calling the function, like some dlsym() like magic?

like image 407
Thomas Avatar asked Jul 26 '15 13:07

Thomas


People also ask

What is the use of __ attribute __ in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What is a GCC function?

GCC allows the declaration of nested functions (a function defined inside another function). The nested function's name will be local to the block where it is defined and can access all the variables of the containing function that are visible at the point of its definition.

What is an attribute function?

Function attributes are extensions implemented to enhance the portability of programs developed with GNU C. Specifiable attributes for functions provide explicit ways to help the compiler optimize function calls and to instruct it to check more aspects of the code.

What is naked function in C?

The naked storage-class attribute is a Microsoft-specific extension to the C language. For functions declared with the naked storage-class attribute, the compiler generates code without prolog and epilog code. You can use this feature to write your own prolog/epilog code sequences using inline assembler code.


2 Answers

Attribute used is helpful in situation when you want to force compiler to emit symbol, when normally it may be omitted. As GCC's documentation says (emphasis mine):

This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly.

For instance, if you have code as follows:

#include <iostream>

static int foo(int a, int b)
{
    return a + b;
}

int main()
{
   int result = 0;

   // some inline assembly that calls foo and updates result

   std::cout << result << std::endl;
}

you might notice, that no symbol foo is present with -O flag (optimization level -O1):

g++ -O -pedantic -Wall check.cpp -c
check.cpp:3: warning: ‘int foo(int, int)’ defined but not used
nm check.o | c++filt | grep foo

As a result you cannot reference foo within this (imaginary) inline assembly.

By adding:

__attribute__((__used__))

it turns into:

g++ -O -pedantic -Wall check.cpp -c
nm check.o | c++filt | grep foo
00000000 t foo(int, int)

thus now foo can be referenced within it.

You may also have spotted that gcc's warning is now gone, as you have tell you compiler that you are sure that foo is actually used "behind the scene".

like image 52
Grzegorz Szpetkowski Avatar answered Sep 28 '22 04:09

Grzegorz Szpetkowski


A particular usecase is for interrupt service routines in a static library. For example, a timer overflow interrupt:

void __attribute__((interrupt(TIMERA_VECTOR),used)) timera_isr(void)

This timera_isr is never called by any function in the user code, but it might form an essential part of a library. To ensure it is linked and there isn't a interrupt vector pointing to an empty section the keyword ensures the linker doesn't optimise it out.

like image 33
EdL Avatar answered Sep 28 '22 06:09

EdL