在编译时将多个文件中的变量收集到单个连续的内存块中 [英] Gather variables from multiple files into a single contiguous block of memory at compile time

查看:164
本文介绍了在编译时将多个文件中的变量收集到单个连续的内存块中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义(并初始化)一系列* .c文件中的一些结构体的实例,但我希望它们在编译时收集到一个连续的数组中。我一直在研究使用一个自定义的部分,并使用该部分的开始和结束地址作为结构数组的开始和结束,但我还没有完全弄清楚细节,我宁愿不写一个自定义链接器脚本,如果我可以摆脱它。下面是我的第一个黑客的总结,它不起作用:

  // mystruct.h:
typedef struct {int a; int b; } mystruct;

// mycode1.c:
#includemystruct.h
mystruct instance1 = {1,2} __attribute __((section(。mysection)));

// mycode2.c:
#includemystruct.h
mystruct instance2 = {3,4} __attribute __((section(。mysection)));

// mystruct.c:
extern char __mysection_start;
extern char __mysection_end;
void myfunc(void){
mystruct * p =& __ mysection_start;
for(; p<& __ mysection_end; p ++){
//使用p-> a和p-> b
}
}


解决方案

为了使用自定义部分,您必须定义其起始地址在自定义链接脚本中。复制您设备的链接描述文件并将其添加到 SECTIONS 块中: c> / * in custom.gld * /
mysection 0x2000:
{
*(mysection);
>> data

为了让您的对象进入本节,请使用section属性:

  / * mycode1.c:* / 
#includemystruct.h
mystruct __attribute__ ((section(mysection)))instance1 = {1,2};
$ b $ * mycode2.c:* /
#includemystruct.h
mystruct __attribute __((section(mysection)))instance2 = {3,4} ;

现在,要获得自定义区域的边界,可以使用 .startof。(section_name) .sizeof。(section_name)汇编运算符:

  #includemystruct.h

char * mysection_start;
char * mysection_end;
size_t mysection_size;

int main(void)
{
asm(mov#.startof。(mysection),W12);
asm(mov#.sizeof。(mysection),W13);
asm(mov W12,_mysection_start);
asm(mov W13,_mysection_size);

mysection_end = mysection_start + mysection_size;

mystruct * p =(mystruct *)mysection_start; $;
for(;(char *)p {
//使用p-> a和p-> b
}
}


I'd like to define (and initialize) a number of instances of a struct across a number of *.c files, but I want them to gather at compile time into a single contiguous array. I've been looking into using a custom section and using the section's start and end address as the start and end of the array of structs, but I haven't quite figured out the details yet, and I'd rather not write a custom linker script if I can get away with it. Here's a summary of my first hack which didn't quite work:

// mystruct.h:
typedef struct { int a; int b; } mystruct;

// mycode1.c:
#include "mystruct.h"
mystruct instance1 = { 1, 2 } __attribute__((section(".mysection")));

// mycode2.c:
#include "mystruct.h"
mystruct instance2 = { 3, 4 } __attribute__((section(".mysection")));

// mystruct.c:
extern char __mysection_start;
extern char __mysection_end;
void myfunc(void) {
    mystruct * p = &__mysection_start;
    for ( ; p < &__mysection_end ; p++) {
        // do stuff using p->a and p->b
    }
}

解决方案

In order to use a custom section, you must define its start address in a custom linker script. Copy your device's linker script and add the new section into its SECTIONS block:

/* in custom.gld */
mysection 0x2000 :
{
    *(mysection);
} >data

To make your objects go into this section, use the section attribute:

/* mycode1.c: */
#include "mystruct.h"
mystruct __attribute__((section("mysection"))) instance1 = { 1, 2 };

/* mycode2.c: */
#include "mystruct.h"
mystruct __attribute__((section("mysection"))) instance2 = { 3, 4 };

Now, to get the boundaries of your custom section, you can use the .startof.(section_name) and .sizeof.(section_name) assembler operators:

#include "mystruct.h"

char *mysection_start;
char *mysection_end;
size_t mysection_size;

int main(void)
{
    asm("mov #.startof.(mysection), W12");
    asm("mov #.sizeof.(mysection), W13");
    asm("mov W12, _mysection_start");
    asm("mov W13, _mysection_size");

    mysection_end = mysection_start + mysection_size;

    mystruct *p = (mystruct *)mysection_start;
    for ( ; (char *)p < mysection_end ; p++)
    {
        // do stuff using p->a and p->b
    }
}

这篇关于在编译时将多个文件中的变量收集到单个连续的内存块中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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