gpt4 book ai didi

c - 使用位置无关代码而不使用 GOT

转载 作者:行者123 更新时间:2023-11-30 15:02:14 25 4
gpt4 key购买 nike

我在没有全局偏移表的情况下编译多个 PIC 文件时遇到问题。

使用以下参数编译第一个文件 onefile.c 时,程序集不包含对 GOT 的引用:

arm-none-eabi-gcc.exe first.c -nosdlib -fPIE -march=armv7-a -o first.o

第一个.c

void function();
int main();

int main()
{
void* function_pointer;
function_pointer = &function;
}

void function()
{

}

组装:

.text:00008000 ; int __cdecl main(int argc, const char **argv, const char **envp)
.text:00008000 EXPORT main
.text:00008000 main
.text:00008000
.text:00008000 var_8 = -8
.text:00008000 var_s0 = 0
.text:00008000
.text:00008000 STR R11, [SP,#-4+var_s0]!
.text:00008004 ADD R11, SP, #0
.text:00008008 SUB SP, SP, #0xC
.text:0000800C LDR R3, =(function - 0x8018)
.text:00008010 ADD R3, PC, R3 ; function
.text:00008014 STR R3, [R11,#var_8]
.text:00008018 MOV R3, #0
.text:0000801C MOV R0, R3
.text:00008020 SUB SP, R11, #0
.text:00008024 LDR R11, [SP+var_s0],#4
.text:00008028 BX LR
.text:00008028 ; End of function main

注意寄存器 R3 在地址 0x8010 中如何在不使用 GOT 的情况下获得函数“function”的地址。

但是当将文件拆分为多个 .c 和 .h 文件时,结果会发生变化

arm-none-eabi-gcc.exe twofiles1.c twofiles2.c -nostdlib -fPIE -march=armv7-a -o twofiles.o

twofiles1.c:

#include "twofiles1.h"

int main()
{
void* function_pointer;
function_pointer = &function;
}

twofiles1.h:

#include "twofiles2.h"

int main();

twofiles2.c:

void function()
{

}

twofiles2.h:

void function();

组装:

.text:00008000 ; int __cdecl main(int argc, const char **argv, const char **envp)
.text:00008000 EXPORT main
.text:00008000 main
.text:00008000
.text:00008000 var_8 = -8
.text:00008000 var_s0 = 0
.text:00008000
.text:00008000 STR R11, [SP,#-4+var_s0]!
.text:00008004 ADD R11, SP, #0
.text:00008008 SUB SP, SP, #0xC
.text:0000800C LDR R2, =(_GLOBAL_OFFSET_TABLE_ - 0x8018)
.text:00008010 ADD R2, PC, R2 ; _GLOBAL_OFFSET_TABLE_
.text:00008014 LDR R3, =(function_ptr - 0x18054)
.text:00008018 LDR R3, [R2,R3] ; function
.text:0000801C STR R3, [R11,#var_8]
.text:00008020 MOV R3, #0
.text:00008024 MOV R0, R3
.text:00008028 SUB SP, R11, #0
.text:0000802C LDR R11, [SP+var_s0],#4
.text:00008030 BX LR
.text:00008030 ; End of function main

请注意如何使用 GOT 计算函数“function”的地址。这就是我想避免的有没有办法实现与第一个结果相同的组装?

最佳答案

编译器一次只能看到一个模块,因此不同的编译单元要求使用 GOT。

关于PIC代码GCC man

-fpic

Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.

强调我的

关于c - 使用位置无关代码而不使用 GOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41122258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com