LoongArch: Use canonical PLT entries for read-only function pointers
Enables successful linking with `-z text` for non-PIE executables using read-only function pointers to shared library functions.
This patch addresses an issue where non-PIE executables storing shared library function addresses in read-only data caused linker failures with -z text. The relocation scan would increment the PLT reference count but not set needs_plt, leaving a dynamic relocation in .rodata. The fix resolves this by making the linker use canonical PLT entries for such read-only function pointers, allowing -z text links to succeed while maintaining proper dynamic relocation behavior.
- proposer
Submits a patch to resolve linker errors with `-z text` on non-PIE executables by using canonical PLT entries for read-only function pointers to shared library functions.
“When a non-PIE executable stores the address of a shared library function in read-only data, the relocation scan increments the PLT reference count but does not set needs_plt. If the function is only referenced through the pointer, the linker leaves an R_LARCH_64 dynamic relocation in .rodata and the link fails with -z text.”
In Details
This patch modifies bfd/elfnn-loongarch.c and adds test cases to address PR ld/35006. For non-PIE executables, if a function pointer in read-only data (.rodata) points to a shared library function, the existing logic incorrectly handles the PLT (Procedure Linkage Table) reference. Specifically, needs_plt is not set, leading to a dynamic relocation (R_LARCH_64) in .rodata. This causes a failure when linking with -z text (which prohibits writable text segments). The fix adds a condition to set h->needs_plt = 1 when a function entry (h->type == STT_FUNC) is dynamic, regular, non-…
- PLT
- Procedure Linkage Table. A table used in dynamically linked executables to resolve addresses of functions in shared libraries at runtime.
- non-PIE executable
- An executable file that is not position-independent. Its code is loaded at a fixed address in memory.
- -z text
- A linker option that prohibits writable segments in the output, enforcing that the text (code) section remains read-only.
- dynamic relocation
- A relocation entry that needs to be processed by the dynamic linker at runtime, typically for shared library symbol resolution.
- canonical PLT entries
- Standard or pre-defined entries within the Procedure Linkage Table that are used for resolving function calls.