简体   繁体   English

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

[英]How do you get a pointer to the .text section?

How do you get a pointer to the .text section of memory for a program from within that program? 如何从该程序中获取指向程序内存的.text部分的指针? 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. 我还需要该部分的长度来进行“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. 工具集自动为我正在使用的工具生成链接器.cmd文件,而我正在使用的板的Board Support Package要求我使用生成的.cmd文件而不是自己生成。 (No make file either to add a script to muck with it afterwords.) (没有make文件要么在后面添加一个脚本来粘贴它。)

Edit: I'm working with a TI TMS 6713 DSP using the code composer 3.1 environment. 编辑:我正在使用代码编辑器3.1环境的TI TMS 6713 DSP。 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. 然而,BSP依赖于TI的“DSP BIOS”配置工具,如果不进行超出范围的工作,我无法真正捏造设置太多。

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;

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段,则可以对段名进行strcmp。

The downside of this approach? 这种方法的缺点是什么? Libbfd is licensed under the GPL, so your entire project would be encumbered with the GPL. Libbfd是根据GPL许可的,因此您的整个项目将受到GPL的限制。 For a commercial project, this might be a non-starter. 对于商业项目,这可能是一个非首发。

If your binary is in ELF format, you could use libelf instead. 如果您的二进制文件是ELF格式,则可以使用libelf。 I'm not familiar with how the libelf APIs work, so I can't provide sample code. 我不熟悉libelf API的工作方式,所以我无法提供示例代码。 The Linux libelf is also GPL, but I believe the BSD projects have their own libelf which you could use. 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. 编辑:当您在最小的实时操作系统环境中处理DSP时,这个答案是行不通的。 Sorry, I tried. 对不起,我试过了。

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(...)

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文件很有用,因为它列出了应用程序中定义的所有符号和部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM