ESP编译时如何将没有使用的文件加入链接

toliqing
Posts: 2
Joined: Wed Sep 05, 2018 4:23 pm

ESP编译时如何将没有使用的文件加入链接

Postby toliqing » Wed Sep 05, 2018 4:37 pm

我在使用esp-idf遇到了这样的问题:

新建了一个C文件test.c,里面实现了几个函数,func1,func2...这样子,使用gcc的关键字将需要的函数声明为特殊的section:

Code: Select all

int myfunc0(void) __attribute__((section(.myfunc))) 
{
	return 0;
}

int testfunc(void)
{
	return 0;
}
然后在链接脚本中esp32.common.ld中添加了KEEP(*(.myfunc)),外部没有调用该文件中任何一个函数时,生成的map中,该文件所有函数被移除掉了;如果外部调用了testfunc函数,则myfunc0不会被移除。

我需要怎么配置才能在没有外部调用的情况下,myfunc0不会被移除掉?(去掉--gc-sections会保留其他不需要的代码,不是我想要的)

谢谢了!

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP编译时如何将没有使用的文件加入链接

Postby ESP_Sprite » Thu Sep 06, 2018 3:00 am

That should work just fine. Are you sure your C code is correct, by the way? For me, it wouldn't compile until I changed the first line to

Code: Select all

int __attribute__((section(".myfunc"))) myfunc0(void) 

toliqing
Posts: 2
Joined: Wed Sep 05, 2018 4:23 pm

Re: ESP编译时如何将没有使用的文件加入链接

Postby toliqing » Thu Sep 06, 2018 10:33 am

ESP_Sprite wrote:That should work just fine. Are you sure your C code is correct, by the way? For me, it wouldn't compile until I changed the first line to

Code: Select all

int __attribute__((section(".myfunc"))) myfunc0(void) 
谢谢你的回复 :D
对的,我上面的代码写错了,应该修改成你的写法才能正常编译,上面我只是用一个demo描述了问题。
你知道怎么解决我遇到的问题吗?
“没有显示的调用该文件中的函数,怎么让链接器保留该section? ”
期待你的解答 :)

ESP_Sprite
Posts: 8926
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP编译时如何将没有使用的文件加入链接

Postby ESP_Sprite » Fri Sep 07, 2018 2:12 am

So I think I get what's happening here. My assumption is that you have myfunc() and testfunc() in a separate file, without any other functions, yes? It seems the KEEP() statement in the ld file is enough to not garbage-collect the function that would normally be garbage-collected by --gc-sections... however, ld seems to do a separate, per-file garbage collection that discards the entire .o file if nothing in it is referenced anywhere else.

The easiest way to let ld think it's referenced (without actually referencing the function in code) is to add a -u line to the ld commandline. In esp-idf, you can do that by adding

Code: Select all

COMPONENT_ADD_LDFLAGS += -u myfunc0
to the component.mk file of the component the myfunc0 is defined in.

Who is online

Users browsing this forum: No registered users and 156 guests