首页 文章

为什么C静态函数不会转到文本部分而是转到rodata部分?

提问于
浏览
4

我在C源文件中定义了一些静态函数 . 编译之后,我使用nm工具显示.o文件中的所有符号,并发现我所定义的所有静态函数都在rodata部分,它们的符号名称是 func .xxxx . 功能不应该位于文本功能中吗?

以下是显示静态函数的nm命令的结果 . func 之前的'r' .xxxx表示该函数存储在rodata部分中 .

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

以下是我的gcc选项:

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

1 回答

  • 6

    您观察到的符号不是指向函数,而是指向函数的名称 . 您可能在这些静态函数中使用宏 __func__ ,这会导致创建这些符号 .

    例如:

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

    会导致这些符号的产生 . 它可能会像宏一样模糊不清

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

    或类似的,所以它可能不是直接可见的 .

    静态函数本身通常在符号表中不可见,因为它们无法在外部链接 .

相关问题