【问题标题】:How to put c-style strings (function arguments) in the specific section?如何将 c 风格的字符串(函数参数)放在特定部分?
【发布时间】:2018-10-01 19:20:22
【问题描述】:

我想将一些函数放在名为“.xip.text”的特定部分中, 但是只读数据不能放在那个部分。

以下是我的测试代码:

#include <stdio.h>

void foo(void) __attribute__((section (".xip.text")));

void foo(void)
{
    printf("hello world\n");
}

在链接器脚本文件中:

MEMORY
{
  RAM (rwx)  : ORIGIN = 0x00010000, LENGTH = 448K
  FLASH (rx) : ORIGIN = 0x10000000, LENGTH = 1024K
}

SECTIONS
{
    .xip :
    {
        *(.xip.text .xip.text.*)
        *(.xip.rodata .xip.rodata.*)
    } > FLASH

    .text :
    {
        *(.text*)
    } > RAM
}

链接后,函数文本放在“.xip”部分,但字符串 “hello world\n”没有放在同一个部分。如何解决?

我可以将整个文件放在“.xip”部分来解决这个问题。但是我 有很多文件,我只想把一些功能放到“.xip”部分,而不是 整个文件。

在map文件中,foo()的文本放在右边部分, 但是字符串 "hello world\n" (.rodata.str1.1) 被放在另一个部分中。

 *(.xip.text .xip.text.*)
 .xip.text      0x1002f7b0        0xc ../foo.o
                0x1002f7b0                foo
 *fill*         0x1002f7bc        0x4 


 .rodata.str1.1
                0x00025515        0xc ../foo.o
 *fill*         0x00025521        0x3 

拆机后,

1002f7b0 <foo>:

void foo(void) __attribute__((section (".xip.text")));

void foo(void)
{
    printf("hello world\n");
1002f7b0:   4801        ldr r0, [pc, #4]    ; (1002f7b8 <foo+0x8>)
1002f7b2:   f000 b95d   b.w 1002fa70 <__puts_veneer>
1002f7b6:   bf00        nop
1002f7b8:   00025515    .word   0x00025515
1002f7bc:   00000000    .word   0x00000000

gcc 版本:gcc-arm-none-eabi-7-2017-q4-major,gcc 版本 7.2.1 20170904(发布)[ARM/embedded-7-branch 修订版 255204](用于 Arm 嵌入式处理器的 GNU 工具 7 -2017-q4-专业)

【问题讨论】:

    标签: gcc linker linker-scripts


    【解决方案1】:

    函数文本放在“.xip”部分,但字符串“hello world\n”没有放在同一部分。

    只读数据不是.text,所以不应该放在同一个部分。

    如果您想控制只读数据进入哪个部分,您需要自己进行。这样的事情应该可以工作:

    __attribute__((section(".xip.rodata")))
    const char my_xip_data[] = "hello, world\n";
    
    void foo(void)
    {
      printf(my_xip_data);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多