简体   繁体   中英

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? 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.

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

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.

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:

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.

#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.

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.

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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