shijun zhao shijun zhao - 6 months ago 23
C Question

Why C static functions doesn't go to text section but to rodata section?

I define some static functions in a C source file. After compiling it, I use nm tool to display all the symbols in the .o file, and find that all of my defined static function is in the rodata section, and their symbols name are func.xxxx. Shouldn't the function be located in the text function?

Following is the results of nm command displaying the static functions. The 'r' before func.xxxx means that the function is stored in rodata section.

00000000 t desc_to_mattr
U __do_panic
00000000 r __func__.5546
00000000 r __func__.5554
00000000 r __func__.5560
00000000 r __func__.5565
00000000 r __func__.5604
00000000 r __func__.5638
00000000 r __func__.5698
00000000 r __func__.5710
00000000 r __func__.5719
00000000 r __func__.5729
00000000 r __func__.5758

Following is my gcc option:

arm-linux-gnueabihf-gcc -std=gnu99 -Werror -fdiagnostics-show-option -Wall -Wcast-align -Werror-implicit-function-declaration -Wextra -Wfloat-equal -Wformat-nonliteral -Wformat-security -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wwrite-strings -Wno-missing-field-initializers -Wno-format-zero-length -Waggregate-return -Wredundant-decls -Wold-style-definition -Wstrict-aliasing=2 -Wundef -pedantic -Wdeclaration-after-statement -Os -g -ffunction-sections -fdata-sections -pipe -g3 -mcpu=cortex-a9 -mfloat-abi=soft -funwind-tables -mthumb -mthumb-interwork -fno-short-enums -fno-common -mno-unaligned-access -MD -MF

Ctx Ctx
Answer Source

The symbols you observe do not point to the function, but to the names of the functions. You probably use the macro __func__ inside these static functions, which leads to creation of those symbols.

For example:

printf("%s: %s\n", __func__, somemsg);

would lead to the creation of those symbols. It may be obscured by a macro like

#define FUNC_ENTRY printf("%s entered\n", __func__);

or similar, so it might not be directly visible.

The static functions themselves are usually not visible in the symbol table since they cannot be linked externally.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download