You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

共享库中R_386_32重定位机制咨询:跨库全局变量引用场景

Understanding the Relocation Process for Your Shared Library Scenario

Let’s walk through exactly how the dynamic linker handles relocation for libbigshr.so and its dependency on libfunlib.so—this is core shared library linking behavior.

1. Initial Library Loading

When your program (or another library) tries to load libbigshr.so, the dynamic linker (e.g., ld-linux.so on Linux) first parses its dynamic section and discovers it depends on libfunlib.so. The linker:

  • Loads libfunlib.so into the process’s virtual address space (at a random base address thanks to ASLR, Address Space Layout Randomization).
  • Resolves libfunlib.so’s own relocations first (if any) so its symbols—including the global variable foo—have valid memory addresses.

2. Symbol Resolution for foo

Next, the linker turns its attention to libbigshr.so’s .rel.dyn section, which lists all data symbols that need relocation. Let’s break down each entry in your .rel.dyn output:

  • Offset: The memory offset within libbigshr.so’s loaded image where the reference to foo lives (e.g., 000005a5).
  • Info: A 32-bit value split into two parts: the upper 16 bits are the index of foo in libbigshr.so’s dynamic symbol table (.dynsym), and the lower 16 bits specify the relocation type (01 = R_386_32).
  • Type (R_386_32): This tells the linker to write the 32-bit absolute address of foo directly into the memory location at the given offset.
  • Sym.Value: 00000000 here is a placeholder—this was the "fake" address used at link time, since foo wasn’t defined in libbigshr.so.
  • Sym.Name: foo, the symbol the linker needs to find.

The linker looks up foo in the dependency chain: it checks libfunlib.so’s dynamic symbol table (.dynsym) and retrieves the actual virtual address of foo (now set because libfunlib.so was loaded).

3. Executing the Relocations

For each of the three foo entries in .rel.dyn:

  1. Calculate the full memory address to modify: libbigshr.so’s loaded base address + the entry’s offset (e.g., base + 0x5a5).
  2. Write the actual 32-bit address of foo (from libfunlib.so) into that memory location.
  3. Repeat this for 0x5ab and 0x5c7—each offset corresponds to a separate place in libbigshr.so where foo is referenced (e.g., three different variable assignments or code lines that use foo).

Key Notes

  • The three entries mean libbigshr.so has three distinct direct references to foo’s absolute address—each requires its own relocation.
  • .rel.dyn is used for data symbols (like global variables) because these are typically resolved immediately at load time, unlike function symbols which often use .rel.plt for lazy (on-first-call) binding.
  • ASLR ensures that each library loads at a unique address every time the program runs, making dynamic relocation mandatory—static linking wouldn’t need this because addresses are fixed at compile time.

内容的提问来源于stack exchange,提问作者ultimate cause

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:57:57