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

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

问题描述

我正在尝试从混合了 c、c++ 和程序集的 .so 中删除所有文本重定位.对于 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 (?).

所以我们的修复是:

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

幸运的是,我们的数组虽然是在运行时生成的静态数据,但可以在汇编时转换为已知的常量.但是对于那些数组或内存是动态的并且必须从 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 - 将地址加载到寄存器中

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

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