gpt4 book ai didi

gcc - 在 GCC 中禁用 GOT

转载 作者:行者123 更新时间:2023-12-01 18:18:28 26 4
gpt4 key购买 nike

全局偏移表(GOT):用于重新定位ELF符号(实现GCC),它有助于共享相同的二进制文件,而无需为每个进程进行任何特定链接。从而减少内存中相同二进制图像的副本。

我的问题是,有没有办法禁用可重定位 ELF 镜像中的 R_386_GOT32R_386_GOTOFF 类型重定位条目?我的意思是,我可以强制 GCC 使用 R_386_PC32R_386_32 类型重定位而不是 GOT 类型重定位吗?

如果没有的话,能解释一下GOT的实现方式吗?我正在为 ELF 编写一个动态链接和加载库。

编辑:
引用链接
https://docs.oracle.com/cd/E23824_01/html/819-0690/chapter6-74186.html
http://man7.org/linux/man-pages/man8/ld.so.8.html
http://wiki.osdev.org/ELF

最佳答案

我终于破解了它!
不,不可能限制 GCC 输出非 GOT 类型重定位。
现在如何解决GOT类型重定位问题?
GOT是由动态链接器分配的固定128KB内存块(它的工作原理是写时复制),其中包含用于重定位的条目。
仅当 ELF 二进制文件中存在任何类型的(如下所列)GOT 重定位时,动态链接器才会分配 GOT。

R_386_GOTOFF (== 0x9)
此重定位类型计算符号值与全局偏移表地址之间的差异。它还指示链接编辑器创建全局偏移表。

R_386_GOTPC (== 0xA)
此重定位类型类似于 R_386_PC32,只不过它在计算中使用全局偏移表的地址。

如何实现?
注意:以下代码片段属于 Atom OS受闭源许可证保护的源代码。但我(Atom 开发者)特此声明此代码片段可以免费使用:)

    uint GOT = Heap.kmalloc(1024 * 128); // 128 KB
...
private static void Relocate(Elf_Header* aHeader, Elf_Shdr* aShdr, uint GOT)
{
uint BaseAddress = (uint)aHeader;
Elf32_Rel* Reloc = (Elf32_Rel*)aShdr->sh_addr;
Elf_Shdr* TargetSection = (Elf_Shdr*)(BaseAddress + aHeader->e_shoff) + aShdr->sh_info;

uint RelocCount = aShdr->sh_size / aShdr->sh_entsize;

uint SymIdx, SymVal, RelocType;
for (int i = 0; i < RelocCount; i++, Reloc++)
{
SymVal = 0;
SymIdx = (Reloc->r_info >> 8);
RelocType = Reloc->r_info & 0xFF;

if (SymIdx != SHN_UNDEF)
{
if (RelocType == R_386_GOTPC)
SymVal = GOT;
else
SymVal = GetSymValue(aHeader, TargetSection->sh_link, SymIdx);
}

uint* add_ref = (uint*)(TargetSection->sh_addr + Reloc->r_offset);
switch(RelocType)
{
case R_386_32:
*add_ref = SymVal + *add_ref; // S + A
break;
case R_386_GOTOFF:
*add_ref = SymVal + *add_ref - GOT; // S + A - GOT
break;
case R_386_PLT32: // L + A - P
case R_386_PC32: // S + A - P
case R_386_GOTPC: // GOT + A - P
*add_ref = SymVal + *add_ref - (uint)add_ref;
break;
default:
throw new Exception("[ELF]: Unsupported Relocation type");
}
}
}

关于gcc - 在 GCC 中禁用 GOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37902940/

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