簡體   English   中英

GCC + C:在二進制文件中按順序保持功能

[英]GCC + C: Keeping Functions in order in the binary file

我正在為微控制器編寫一些代碼,這些微控制器需要根據功能指針查找數據(這些數據和功能將存儲在ROM中)。

我希望能夠相對快速地執行此操作,並且無需在查找表中使用RAM。 我可以想到的最簡單的方法是進行二進制搜索。

我只是以示例為例,所以它可能會被破壞,但希望您能明白:

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此功能起作用,需要在內存中一個接一個地布局myFunction1myFunction2等(盡管不一定彼此緊挨着)。 我需要使用-Os編譯所有內容以使其適合可用的ROM,那么如何確保功能實際上保持正確的順序?

...或者失敗了,如何重新排序mySearchData ,使其與功能的順序匹配?

我現在唯一能想到的是:

  • 構建整個對象,然后執行一個后處理步驟, 對二進制文件中的數組進行排序-雖然非常不可移植。
  • 一次構建,查看列表文件以找出函數的真實順序,重新編寫mySearchData並再次編譯-但不能保證編譯器是確定性的

這些聽起來都不是很好。

對問題的書面回答:
如何使用gcc工具鏈強制執行二進制函數的順序?
此鏈接http://sourceware.org/binutils/docs-2.21/ld/Scripts.html#Scripts
這是SCARY,但有效

編輯:
方法2分區查找表:
通過半字節分區,它非常有效。您只需要一個初始化步驟。 分配16個字節讀取數組中的所有元素。 用0x0F“&”函數指針的第一個字節,並在所得偏移量中增加插槽。 分配16個具有您剛剛計算的長度的列表。 重新讀取數組中的所有元素,這一次將它們插入分配的列表中,並使用16個字節作為計數器來放置指針。
順便說一句,您有動態內存分配嗎? 我才意識到你說的是8KiB。

好,。 我真的不明白為什么您需要一個指向SEARCH的函數指針才能使用函數...將數據放入函數中要簡單得多,在這種情況下,數據也將重新排序。 但是您要的是。

OK,給出了C代碼中的gcc:

您可以通過添加以下內容將每個函數放在其自己的部分中:

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

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

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

然后,使用修改后的鏈接描述文件,如下所示:

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

鏈接描述文件中還有很多可能性可以對輸入節進行排序。 尋找LD說明以了解SORT!

從ld手冊中: Normally, the linker will place files and sections matched by wildcards in the order in which they are seen during the link. You can change this by using the SORT keyword, which appears before a wildcard pattern in parentheses (eg, SORT(.text*)). When the SORT keyword is used, the linker will sort the files or sections into ascending order by name before placing them in the output file. Normally, the linker will place files and sections matched by wildcards in the order in which they are seen during the link. You can change this by using the SORT keyword, which appears before a wildcard pattern in parentheses (eg, SORT(.text*)). When the SORT keyword is used, the linker will sort the files or sections into ascending order by name before placing them in the output file.

但是,我要說的是,您有一個XY問題。 我認為您會做一些事情,排序功能,處理二進制搜索是您的解決方案。 我相信,沒有此類黑客攻擊就可以解決起源問題!

最好解決您的原始問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM