如何将 C 风格的字符串(函数参数)放在特定的部分?

How to put c-style strings (function arguments) in the specific section?

我想将一些函数放在名为“.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”部分,而不是 整个文件。

在地图文件中,foo()的文本放在右边, 但是字符串 "hello world\n" (.rodata.str1.1) 被放置在另一个 seciton.

 *(.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](GNU Tools for Arm 嵌入式处理器 7-2017-q4-major)

function text is placed in ".xip" section, but the string "hello world\n" is not put in the same section.

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

如果你想控制只读数据到哪个部分,你需要自己做。这样的事情应该有效:

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

void foo(void)
{
  printf(my_xip_data);
}