avoiding text relocations when mixing c/c++ and assembly in a .so

人走茶凉 提交于 2020-02-01 09:11:39

问题


I am trying to remove all text relocations from an .so that mixes c, c++ and assembly. For c/c++ -fpic takes care of PIC.

On Android ARM target, we are able to call exported asm functions from c/c++ without causing text relocations. But in our implementation we have arrays of data that must be accessible from both C++ and assembly. On C++ it's a plain old array i.e. extern "C" { __declspec(align(32)) int16_t myarray[256]; } and on asm side we use .global myarray.

The second we use such a symbol in asm side we see text relocation in the final .so which is visible via scanelf and readelf. The Android L loader in api mode 23 will flat out refuse to load such an .so.

Questions: - It this issue to be expected? - Is there some special declaration to be used on the C or asm side to ensure there are no text relocations?

Edit: Would a minimal example be useful?


回答1:


Thanks to all the comments. So to summarize for the good of everyone else fighting with Android M, we were able to solve some of our issues. My understanding now is as follows and please, correct me if I am wrong:

1) I could not in asm point to directly to an external globals (i.e. ldr) without causing a text relocation.

2) I could not in asm reference a symbol contained in data section even if not global, because the relative address from text to data is unknown at assembly time. Why the .so linker cannot resolve this at link time I am not sure, but I can only assume the relative address is too far or by nature of PIC the relative addresses of data/text aren't known at run-time (?).

So our fix was:

1) Pre-generate the arrays and add to the text section, as close as possible to the code using it (relative instructions have a limited distance). We found that putting the table too far from the code using it failed.

2) Use of 'adr' is preferred of 'ldr' when loading from a label as it seemed to ensure you're loading from the current section and helped us avoid some text relocations

Fortunately our arrays were static data though generated at runtime and could be converted to const known at assembly time. But for those where the array or memory is dynamic and must be passed from C to asm, I see no other choice put to pass it as a parameter to the assembly code as suggested by several people in the comments.

A useful read was: ARM - Loading addresses into registers



来源:https://stackoverflow.com/questions/33510601/avoiding-text-relocations-when-mixing-c-c-and-assembly-in-a-so

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!