【问题标题】:Change prefix to per-function sections generated by -ffunction-sections将前缀更改为由 -ffunction-sections 生成的每个功能部分
【发布时间】:2021-03-07 22:29:21
【问题描述】:

如果我有一个函数foo() 并使用-ffunction-sectionsgcc 将把foo() 放在它自己的.text.foo 部分中。是否可以更改.text 的前缀?这样我得到.customName.foo 而不是text.foo

【问题讨论】:

    标签: gcc


    【解决方案1】:

    我有同样的问题,我使用section 属性解决了它。

    以下解决方案是最简单的,但不会为每个函数创建一个部分(因为-ffunction-section 参数允许这样做)

    #define AT_FLASH_TEXT_SECTION(var) \
        __attribute__((section(".text.flash"))) var
    
    AT_FLASH_TEXT_SECTION(int myFunction(float param1, long param2));
    

    因此函数myFunction 将出现在.text.flash 部分中,而且所有其他使用宏AT_FLASH_TEXT_SECTION 声明的函数也将出现。



    为了获得所需的行为,我将宏修改如下:

    #define AT_FLASH_TEXT_SECTION_SYM(var, subsectionName) \
        __attribute__((section(".text.flash." #subsectionName))) var
    
    AT_FLASH_TEXT_SECTION_SYM(int myNewFunction(float param1, long param2), myNewFunction);
    

    这是迄今为止我找到的最佳解决方案。
    不幸的是,它很容易出错:函数名称必须在AT_FLASH_TEXT_SECTION_SYM 宏的subsectionName 参数中重复相同。
    此外,如果两个 c 模块包含两个同名的静态函数,它们将在同一个部分中发出,回到上一个问题。

    希望对您有所帮助,或许您可以从这里找到更好的解决方案。

    【讨论】:

      【解决方案2】:

      不,这似乎不可能。请参阅gcc/varasm.c(我还没有运行调试器,但我很确定这是计算节名称的代码。)

      void
      default_unique_section (tree decl, int reloc)
      {
        [...]
      
        switch (categorize_decl_for_section (decl, reloc))
          {
          case SECCAT_TEXT:
            prefix = one_only ? ".t" : ".text";
            break;
      
        [...]
      
        name = IDENTIFIER_POINTER (id);
        name = targetm.strip_name_encoding (name);
      
        [...]
      
        string = ACONCAT ((linkonce, prefix, ".", name, NULL));
      
        set_decl_section_name (decl, string);
      }
      

      此外,这可能是个坏主意,例如链接器脚本根据其名称处理节(请参阅ld --verbose)。 .text.customprefix.foo 之类的东西可能是更好的选择,但我不知道您为什么需要自定义前缀。


      作为一种解决方法,您可以使用 section 属性手动分配部分。

      'section ("SECTION-NAME")'
           Normally, the compiler places the code it generates in the 'text'
           section.  Sometimes, however, you need additional sections, or you
           need certain particular functions to appear in special sections.
           The 'section' attribute specifies that a function lives in a
           particular section.  For example, the declaration:
      
                extern void foobar (void) __attribute__ ((section ("bar")));
      
           puts the function 'foobar' in the 'bar' section.
      

      【讨论】:

      • 我有同样的问题,我可以解释为什么我需要更改前缀:在裸机固件项目(在具有闪存和小内存的微控制器上运行)中,初始化函数和其他函数执行是有意义的只有一次,在闪存中(通常较慢)以释放 SRAM 中的空间(通常更快),用于需要最高性能的功能。
      • @mastupristi 很有趣,谢谢。 section 函数属性不是为你解决了吗?
      • section 属性部分有帮助,正如我在回答中所解释的那样。
      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 2017-06-09
      • 2020-01-31
      • 2012-04-10
      • 2015-10-27
      相关资源
      最近更新 更多