你如何获得指向.text部分的指针?

时间:2023-01-28 07:29:54

How do you get a pointer to the .text section of memory for a program from within that program? I also need the length of the section to do a "Flash to Memory" compare as part of a continuous selftest that runs in the background.

如何从该程序中获取指向程序内存的.text部分的指针?我还需要该部分的长度来进行“Flash to Memory”比较,作为在后台运行的连续自测试的一部分。

The toolset automatically generates the linker .cmd file for the tools I'm using, and the Board Support Package for the board I'm using requires I use the generated .cmd file instead of making my own. (No make file either to add a script to muck with it afterwords.)

工具集自动为我正在使用的工具生成链接器.cmd文件,而我正在使用的板的Board Support Package要求我使用生成的.cmd文件而不是自己生成。 (没有make文件要么在后面添加一个脚本来粘贴它。)

Edit: I'm working with a TI TMS 6713 DSP using the code composer 3.1 environment. The card I'm using was contracted by our customer and produced by another organization so I can't really point you to any info on it. However the BSP is dependant upon TI's "DSP BIOS" config tool, and I can't really fudge the settings too much without digging into an out of scope effort.

编辑:我正在使用代码编辑器3.1环境的TI TMS 6713 DSP。我正在使用的卡是由我们的客户签约并由另一个组织生产的,因此我无法真正指出您的任何信息。然而,BSP依赖于TI的“DSP BIOS”配置工具,如果不深入研究范围之外的工作,我无法真正捏造设置。

4 个解决方案

#1


4  

You need to put "variables" in the linker script.

您需要在链接描述文件中添加“变量”。

In one of my projects I have this in one of my sections:

在我的一个项目中,我在我的一个部分中有这个:

  __FlashStart = .;

In the C program I have this:

在C程序中我有这个:

extern unsigned long int _FlashStart;
unsigned long int address = (unsigned long int)&_FlashStart;

#2


4  

It would definitely be easier if you could modify the linker script. Since you cannot, it is possible to extract section names, addresses, and sizes from the program binary. For example, here is how one would use libbfd to examine all code sections.

如果您可以修改链接描述文件肯定会更容易。由于您不能,可以从程序二进制文件中提取节名称,地址和大小。例如,以下是如何使用libbfd检查所有代码段。

#include <bfd.h>

bfd *abfd;
asection *p;
char *filename = "/path/to/my/file";

if ((abfd = bfd_openr(filename, NULL)) == NULL) {
    /* ... error handling */
}

if (!bfd_check_format (abfd, bfd_object)) {
    /* ... error handling */
}

for (p = abfd->sections; p != NULL; p = p->next) {
    bfd_vma  base_addr = bfd_section_vma(abfd, p);
    bfd_size_type size = bfd_section_size (abfd, p);
    const char   *name = bfd_section_name(abfd, p);
    flagword     flags = bfd_get_section_flags(abfd, p);

    if (flags & SEC_CODE) {
        printf("%s: addr=%p size=%d\n", name, base_addr, size);
    }
}

If you only want to look at the .text segment, you'd strcmp against the section name.

如果您只想查看.text段,则可以查看节名称。

The downside of this approach? Libbfd is licensed under the GPL, so your entire project would be encumbered with the GPL. For a commercial project, this might be a non-starter.

这种方法的缺点是什么? Libbfd根据GPL许可,因此您的整个项目将受到GPL的限制。对于商业项目,这可能是一个非首发。

If your binary is in ELF format, you could use libelf instead. I'm not familiar with how the libelf APIs work, so I can't provide sample code. The Linux libelf is also GPL, but I believe the BSD projects have their own libelf which you could use.

如果您的二进制文件是ELF格式,则可以使用libelf。我不熟悉libelf API的工作方式,因此我无法提供示例代码。 Linux libelf也是GPL,但我相信BSD项目有自己的libelf,你可以使用它。

Edit: as you're working on a DSP in a minimal real-time OS environment, this answer isn't going to work. Sorry, I tried.

编辑:当您在最小的实时操作系统环境中处理DSP时,这个答案是行不通的。对不起,我试过了。

#3


1  

Could you clarify which tool chain and architecture you are interested in.

您能澄清一下您感兴趣的工具链和架构吗?

On the compiler I am using right now (IAR ARM C/C++) there are operators built into the compiler which return the segment begin address __sfb(...), segment end address __sfe(...), and segment size __sfs(...)

在我现在使用的编译器(IAR ARM C / C ++)上,编译器内置了运算符,它返回段开始地址__sfb(...),段结束地址__sfe(...)和段大小__sfs( ...)

#4


1  

The symbols you're looking for are __text__ and __etext__ which point to the start and end of the .text section, respectively.

您正在寻找的符号分别是__text__和__etext__,它们分别指向.text部分的开头和结尾。

You may find the generated .map file useful, as it lists all the symbols and sections defined in your application.

您可能会发现生成的.map文件很有用,因为它列出了应用程序中定义的所有符号和部分。

#1


4  

You need to put "variables" in the linker script.

您需要在链接描述文件中添加“变量”。

In one of my projects I have this in one of my sections:

在我的一个项目中,我在我的一个部分中有这个:

  __FlashStart = .;

In the C program I have this:

在C程序中我有这个:

extern unsigned long int _FlashStart;
unsigned long int address = (unsigned long int)&_FlashStart;

#2


4  

It would definitely be easier if you could modify the linker script. Since you cannot, it is possible to extract section names, addresses, and sizes from the program binary. For example, here is how one would use libbfd to examine all code sections.

如果您可以修改链接描述文件肯定会更容易。由于您不能,可以从程序二进制文件中提取节名称,地址和大小。例如,以下是如何使用libbfd检查所有代码段。

#include <bfd.h>

bfd *abfd;
asection *p;
char *filename = "/path/to/my/file";

if ((abfd = bfd_openr(filename, NULL)) == NULL) {
    /* ... error handling */
}

if (!bfd_check_format (abfd, bfd_object)) {
    /* ... error handling */
}

for (p = abfd->sections; p != NULL; p = p->next) {
    bfd_vma  base_addr = bfd_section_vma(abfd, p);
    bfd_size_type size = bfd_section_size (abfd, p);
    const char   *name = bfd_section_name(abfd, p);
    flagword     flags = bfd_get_section_flags(abfd, p);

    if (flags & SEC_CODE) {
        printf("%s: addr=%p size=%d\n", name, base_addr, size);
    }
}

If you only want to look at the .text segment, you'd strcmp against the section name.

如果您只想查看.text段,则可以查看节名称。

The downside of this approach? Libbfd is licensed under the GPL, so your entire project would be encumbered with the GPL. For a commercial project, this might be a non-starter.

这种方法的缺点是什么? Libbfd根据GPL许可,因此您的整个项目将受到GPL的限制。对于商业项目,这可能是一个非首发。

If your binary is in ELF format, you could use libelf instead. I'm not familiar with how the libelf APIs work, so I can't provide sample code. The Linux libelf is also GPL, but I believe the BSD projects have their own libelf which you could use.

如果您的二进制文件是ELF格式,则可以使用libelf。我不熟悉libelf API的工作方式,因此我无法提供示例代码。 Linux libelf也是GPL,但我相信BSD项目有自己的libelf,你可以使用它。

Edit: as you're working on a DSP in a minimal real-time OS environment, this answer isn't going to work. Sorry, I tried.

编辑:当您在最小的实时操作系统环境中处理DSP时,这个答案是行不通的。对不起,我试过了。

#3


1  

Could you clarify which tool chain and architecture you are interested in.

您能澄清一下您感兴趣的工具链和架构吗?

On the compiler I am using right now (IAR ARM C/C++) there are operators built into the compiler which return the segment begin address __sfb(...), segment end address __sfe(...), and segment size __sfs(...)

在我现在使用的编译器(IAR ARM C / C ++)上,编译器内置了运算符,它返回段开始地址__sfb(...),段结束地址__sfe(...)和段大小__sfs( ...)

#4


1  

The symbols you're looking for are __text__ and __etext__ which point to the start and end of the .text section, respectively.

您正在寻找的符号分别是__text__和__etext__,它们分别指向.text部分的开头和结尾。

You may find the generated .map file useful, as it lists all the symbols and sections defined in your application.

您可能会发现生成的.map文件很有用,因为它列出了应用程序中定义的所有符号和部分。