关于链接脚本(ld)的使用中段(section)被优化的问题
Posted: Wed Nov 09, 2022 5:43 am
使用方式如下:
链接脚本:
a.h:
b.h:
b.c:
编译后得到的map文件:
问题是编译之后user_section_tab段里的内容是空的,请问这个问题怎么处理呢?
链接脚本:
Code: Select all
...
. = ALIGN(4);
user_section_tab_local_start = ABSOLUTE(.);
KEEP (*(SORT(*user_section_tab*)))
user_section_tab_local_end = ABSOLUTE(.);
...
Code: Select all
#define USER_SECTION_NAME __attribute__((section("user_section_name_tab")))
#define USER_SECTION __attribute__((section("user_section_tab")))
extern void *user_section_tab_local_start;
extern void *user_section_tab_local_end;
const static uint32_t user_section_tab_local_start_addr = (uint32_t)&user_section_tab_local_start;
const static uint32_t user_section_tab_local_end_addr = (uint32_t)&user_section_tab_local_end;
Code: Select all
typedef int (*user_func_t)(void *);
struct user_cmd_object {
user_func_t func;
const char *breif;
uint32_t *const param;
};
#define USER_CMD_EXPORT(name, f, c, p) \
uint32_t _USER_CMD_##name##_param = (uint32_t)p; \
const char _USER_CMD_##name##_name[] SECTION_A_NAME = #c; \
__attribute__((used)) const struct user_cmd_object _USER_CMD_##name##_obj USER_SECTION = \
{ \
.func = f, \
.breif = _USER_CMD_##name##_name, \
.param = &_USER_CMD_##name##_param \
}
Code: Select all
#include "b.h"
//at+version
static int func_version(void *param)
{
printf("%s\r\n", "V1.0.0");
return 0;
}
USER_CMD_EXPORT(VERSION, func_version, AT+VERSION, 0);
//at+uid
static int func_uid(void *param)
{
printf("%08x\r\n", u_devid_get());
return 0;
}
USER_CMD_EXPORT(UID, func_uid, AT+UID, 0);
Code: Select all
0x000000003c029614 . = ALIGN (0x4)
0x000000003c029614 user_section_tab_local_start = ABSOLUTE (.)
*(SORT_BY_NAME(*user_section_tab*))
0x000000003c029614 user_section_tab_local_end = ABSOLUTE (.)