binutils Newspaper
JUNE 15, 2026
ld Proposed

Ld: Fix calls to strchr that discard or use wrong const qualifiers

The linker is updated to avoid discarding const qualifiers when calling strchr in several places.

This patch updates the linker (ld) to ensure that the return values of strchr are correctly assigned to const char * variables, avoiding the discarding of const qualifiers. This change affects multiple files under the ld/emultempl/ directory and ld/ldelf.c. The patch addresses potential issues related to const correctness in the linker code.

In the Thread 1 participant
  1. Alan Modra <amodra@gmail.com> proposer

    Acknowledges the previous patch and indicates approval.

    “OK.”

In Details

This patch modifies linker scripts and C code to ensure const correctness when using strchr. Specifically, it addresses cases where the return value of strchr (a const char *) was being assigned to a non-const char *, potentially leading to undefined behavior. The changes affect files within the ld/emultempl/ directory, which contain emulation templates for different target architectures, and ld/ldelf.c, which handles ELF-specific linking operations.

For Context

The strchr function searches for the first occurrence of a character within a string. When working with constant strings (strings that shouldn't be modified), strchr returns a pointer to a constant character (const char *). This patch ensures that the code correctly handles these constant pointers in the linker, a tool that combines compiled code into an executable. Incorrect handling can lead to unexpected behavior when the program tries to modify a string that should not be changed.

Part of a Series

Filed Under: ldstrchrconst correctnesslinker