创建一个C函数,在文本段给定大小 [英] creating a C function with a given size in the text segment

查看:169
本文介绍了创建一个C函数,在文本段给定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程嵌入式的PowerPC问题 32系统具有32千字节的8路组相联的L2指令缓存。为了避免缓存反覆我们的方式在单独的缓存设置,这样一组被称为在高频功能的文本(想想中断code)结束对齐功能。我们根据需要插入虚拟功能做到这一点,例如。

I'm programming an embedded powerpc 32 system with a 32 kbyte 8-way set associative L2 instruction cache. To avoid cache thrashing we align functions in a way such that the text of a set of functions called at a high frequency (think interrupt code) ends up in separate cache sets. We do this by inserting dummy functions as needed, e.g.

void high_freq1(void)
{
   ...
}

void dummy(void)
{
   __asm__(/* Silly opcodes to fill ~100 to ~1000 bytes of text segment */);
}

void high_freq2(void)
{
   ...
}

这令我丑陋和不理想。我想要做的就是

This strikes me as ugly and suboptimal. What I'd like to do is


  • 避免 __ __ ASM 完全和使用纯C89(也许C99)

  • 找到一个方法来创建所需的假人()垫片,海湾合作委员会的优化不碰

  • 假人的尺寸()间隔应该是可配置为4字节的倍数。典型的间隔是260到1000个字节。

  • 应为一组的约50个函数在总共500功能的可行

  • avoid __asm__ entirely and use pure C89 (maybe C99)
  • find a way to create the needed dummy() spacer that the GCC optimizer does not touch
  • the size of the dummy() spacer should be configurable as a multiple of 4 bytes. Typical spacers are 260 to 1000 bytes.
  • should be feasible for a set of about 50 functions out of a total of 500 functions

我也愿意探讨放置的一组中选择的功能的方式,以便它们不会被映射到相同的高速缓存行的全新的技术。一个链接脚本可以做到这一点?

I'm also willing to explore entirely new techniques of placing a set of selected functions in a way so they aren't mapped to the same cache lines. Can a linker script do this?

推荐答案

也许 连接器脚本 是要走的路。 GNU链接器可以使用这些我想......我用LD文件用于AVR和MQX这两者,我们采用基于GCC编译器...可以帮助...

Maybe linker scripts are the way to go. The GNU linker can use these I think... I've used LD files for the AVR and on MQX both of which we using GCC based compilers... might help...

您可以定义你的内存段等什么去哪里......每一次我来到一个写了这么久,因为最后我要再次阅读起来......

You can define your memory sections etc and what goes where... Each time I come to write one its been so long since the last I have to read up again...

有SVR3式的命令文件宝石了搜索。

Have a search for SVR3-style command files to gem up.

免责声明:以下示例为一个非常具体的编译器...但SVR3般的格式是pretty一般... ...你必须阅读起来为你的系统

DISCLAIMER: Following example for a very specific compiler... but the SVR3-like format is pretty general... you'll have to read up for your system

例如,你可以用这样的命令......

For example you can use commands like...

ApplicationStart = 0x...;
MemoryBlockSize = 0x...;
ApplicationDataSize  = 0x...;
ApplicationLength    = MemoryBlockSize - ApplicationDataSize;

MEMORY {
    RAM: ORIGIN = 0x...                LENGTH = 1M
    ROM: ORIGIN = ApplicationStart     LENGTH = ApplicationLength   
}

这定义了连接三个存储部分。然后,你可以说这样的话

This defines three memory sections for the linker. Then you can say things like

SECTIONS
{
    GROUP :
    {       
        .text :
        {
            * (.text)
            * (.init , '.init$*')
            * (.fini , '.fini$*')
        }

        .my_special_text ALIGN(32): 
        {
            * (.my_special_text)
        } 

        .initdat ALIGN(4):
        // Blah blah
    } > ROM
    // SNIP
}

命令告知链接器如何将输入段映射到输出段,以及如何放置输出段在内存......这里,我们所说的是进入ROM的输出部分,这是我们在内存上面定义的定义。该位可能你的兴趣是 .my_special_text 。在您的code然后你可以做这样的事情...

The SECTIONS command tells the linker how to map input sections into output sections, and how to place the output sections in memory... Here we're saying what is going into the ROM output section, which we defined in the MEMORY definition above. The bit possible of interest to you is .my_special_text. In your code you can then do things like...

__attribute__ ((section(".my_special_text")))
void MySpecialFunction(...)
{
    ....
}

链接器将放在由 __属性__ 语句到 my_special_text 部分pceded任何函数$ P $。在上面的例子,这是文本部分后放置到ROM中的下一个4字节对齐边界,但你可以把它无论如何你喜欢。所以,你可以做几节,每一个的你描述的功能,并确保该地址不会造成冲突......

The linker will put any function preceded by the __attribute__ statement into the my_special_text section. In the above example this is placed into ROM on the next 4 byte aligned boundary after the text section, but you can put it anyway you like. So you could make a few sections, one for each of the functions you describe, and make sure the addresses won't cause clashes...

您可以使用以下格式的链接器定义的变量这一节的大小和存储位置

You can the size and memory location of the section using linker defined variables of the form

extern char_fsection_name[]; // Set to the address of the start of section_name
extern char_esection_name[]; // Set to the first byte following section_name

因此​​,对于这个例子...

So for this example...

extern char _fmy_special_text[]; // Set to the address of the start of section_name
extern char _emy_special_text[]; // Set to the first byte following section_name

这篇关于创建一个C函数,在文本段给定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆