GCC + C:在二进制文件中按顺序保持功能 [英] GCC + C: Keeping Functions in order in the binary file

查看:76
本文介绍了GCC + C:在二进制文件中按顺序保持功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为微控制器编写一些代码,这些微控制器需要根据功能指针查找数据(这些数据和功能将存储在ROM中).

I'm doing some code for microcontrollers where I need to look up data based on function pointers (this data and the functions will be in ROM).

我希望能够相对快速地做到这一点,并且无需在查找表中使用RAM.我认为最简单的方法就是进行二进制搜索.

I'd like to be able to do this relatively quickly, and without using RAM for a lookup table. The easiest way I can think to do this would be to do a binary search.

我只是以示例为例,所以它可能会被破坏,但希望您能理解:

I just hacked this up as an example, so it might be broken but hopefully you get the idea:

void myFunction1() {}
void myFunction2() {}
void myFunction3() {}
void myFunction4() {}
// ...

typedef struct {
  void *functionPtr;
  int myData[10];
} SearchData;

const SearchData mySearchData[] = {
 { &myFunction1, ... },
 { &myFunction2, ... },
 { &myFunction3, ... },
 { &myFunction4, ... },
 // ...
};

void doBinarySearch(void *func, int min, int max) {
  if (max<min) return;
  int mid = (min+max)/2;
  if (func < mySearchData[mid].functionPtr)
    doBinarySearch(func, min, mid-1);
  else if (func > mySearchData[mid].functionPtr)
    doBinarySearch(func, mid+1, max);
  else {
    // we found it!
  }
}

void realBinarySearch(void *func) {
  doBinarySearch(func, 0, (sizeof(mySearchData)/sizeof(SearchData))-1);
}

但是,要使此方法起作用,需要在内存中一个接一个地放置 myFunction1 myFunction2 等(尽管不一定彼此紧挨着).我需要使用 -Os 编译所有内容以使其适合可用的ROM,所以如何确保功能实际上保持正确的顺序?

However for this to work, myFunction1, myFunction2, etc need to be laid out one after the other in memory (although not necessarily right next to each other). I need to compile everything with -Os to fit it in available ROM, so how do I ensure that the functions actually stay in the correct order?

...或者失败了,如何重新排序 mySearchData ,使其与功能的顺序匹配?

...or failing that, how could I re-order mySearchData such that it matches the order of the functions?

关于我现在唯一能想到的是:

About the only things I can think of right now are:

  • 构建整个对象,然后执行一个后处理步骤,将二进制文件内的数组排序-虽然非常不可移植.
  • 构建一次,查看列表文件以找出函数的真实顺序,重新编写 mySearchData 并再次编译-但不能保证编译器是确定性的
  • build the whole thing and then have a post-processing step that sorts the array inside the binary - very un-portable though.
  • build once, look at the list file to figure out the real order of functions, re-write mySearchData and compile again - but there's no guarantee the compiler is deterministic

这些听起来都不是很好.

Neither of those sound great though.

推荐答案

好的.我真的不明白为什么您需要一个指向SEARCH的函数指针才能使用某个函数...将数据放入函数中要简单得多,在这种情况下,数据也将被重新排序.但是您要的就是这个.

OK,. I did not really understand why you need a function pointer to SEARCH for a function... It is much simpler to put the data to the function which in that case will also be reordered. But you are asking for that.

好的,在C代码中给出了gcc:

OK, gcc in C code is given:

您可以通过添加以下内容将每个函数放在其自己的部分中:

You can place every function in its own section by adding:

void f1() __attribute__((section(".text.1")));
void f1()
{
}

void f2() __attribute__((section(".text.2")));
void f2()
{
}

void f3() __attribute__((section(".text.3")));
void f3()
{
}

然后您可以使用经过修改的链接描述文件,如下所示:

And you then use a modified linker script like this:

 .text   :
  {
   ....
   *(.text.1)
   *(.text.2)
   *(.text.3)
   ... continues here ...

链接脚本中还有很多可能性可以对输入节进行排序.寻找LD说明以了解SORT!

Also there are a lot more possibilities in linker script to sort input sections. Look ld description for SORT!

从ld手册中:通常,链接器将按通配符在链接期间看到的顺序放置文件和节.您可以使用SORT关键字更改此关键字,该关键字出现在括号中的通配符模式之前(例如SORT(.text *)).使用SORT关键字时,链接器会将文件或节按名称升序排序,然后再将它们放置在输出文件中.

但是,我要说的是,您有一个XY问题.我认为您会做一些事情和排序功能,处理二进制搜索是您的解决方案.我相信没有这种黑客攻击就可以解决起源问题!

But as a remark, I believe you have a XY-problem. I think you would do something and sorting functions and dealing with binary search is your solution for it. I believe that the origin problem can be solved without such hacks!

最好解决您的原始问题!

It would be nice to get your original problem!

这篇关于GCC + C:在二进制文件中按顺序保持功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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