混合c/c ++和.so中的汇编时避免文本重定位 [英] avoiding text relocations when mixing c/c++ and assembly in a .so

查看:135
本文介绍了混合c/c ++和.so中的汇编时避免文本重定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从.so中删除所有文本重定位,以便将c,c ++和汇编语言混合在一起.对于c/c ++,-fpic负责PIC.

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.

在Android ARM目标上,我们可以从c/c ++调用导出的asm函数,而不会引起文本重定位.但是,在我们的实现中,我们必须从C ++和汇编语言中都可以访问数据数组.在C ++上,它是一个普通的旧数组,即extern "C" { __declspec(align(32)) int16_t myarray[256]; },而在asm端,我们使用.global myarray.

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.

第二个我们在asm端使用这样的符号,我们在最后一个.so中看到文本重定位,这可以通过scanelfreadelf看到.处于api模式23的Android L加载程序将拒绝加载此类.so.

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.

问题: -这个问题是可以预期的吗? -在C或asm端是否要使用一些特殊的声明,以确保没有文本重定位?

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?

推荐答案

感谢所有评论.因此,总结一下与Android M对抗的所有人,我们能够解决一些问题.现在,我的理解如下,如果我错了,请纠正我:

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)我不能在asm中直接指向外部全局变量(即ldr),而不会引起文本重定位.

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

2)我无法在asm中引用数据节中包含的符号,即使它不是全局的也是如此,因为在组装时,从文本到数据的相对地址是未知的.为什么我不确定.so链接器为什么不能在链接时解决此问题,但是我只能假设相对地址太远,或者根据PIC的性质,在运行时不知道数据/文本的相对地址(?).

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)预生成数组,然后将其添加到文本部分,并尽可能靠近使用它的代码(相对指令的距离有限).我们发现将表与使用它的代码放置得太远了.

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)从标签加载时,使用'adr'优先于'ldr',因为这似乎可以确保您从当前部分加载,并帮助我们避免了某些文本重定位

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

幸运的是,我们的数组虽然是在运行时生成的,但它们是静态数据,可以转换为汇编时已知的const.但是对于那些数组或内存是动态的并且必须从C传递到asm的对象,我看不到其他选择,如一些人在评论中所建议的那样,将其作为参数传递给汇编代码.

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.

有用的读物​​是: ARM-将地址加载到寄存器中

这篇关于混合c/c ++和.so中的汇编时避免文本重定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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