[v1,0/1] Enable arch-specific CFI directives and DWARF instructions only when required by the target (part 1)

Message ID 20250409103611.407140-1-matthieu.longo@arm.com
Headers
Series Enable arch-specific CFI directives and DWARF instructions only when required by the target (part 1) |

Message

Matthieu Longo April 9, 2025, 10:36 a.m. UTC
  This patch is related to a comment from another patch series (see [1]), and tries to propose a solution for the raised issue while changing the approach proposed in a previous RFC [2] based on the feedback.

## Context

DWARF 5 standard provides a space for vendor-specific DWARF directives. Binutils currently defines GNU-specific DWARF directives and vendor-specific ones for AArch64, SGI/MIPS, and Sparc architectures.
The architecture-specific code is not guarded. This has caused confusion in the past, and .cfi_window_save (a Sparc-specific directive) was used on AArch64 in place of .cfi_negate_ra_state, the defined directive by the "DWARF extensions on AArch64" document (see [3]).

In [1], Jan Beulich rightly pointed out the issue and proposed to introduce an `#ifdef TC_AARCH64` around the AArch64 directives in **gas (only)**. Gas only supports one target architecture (the one selected with --target), whereas others tools like readelf, objdump, etc. deduce the target architecture from the input object at runtime. Hence, using TC_<arch> definitions to enable/disable DWARF directives at compilation time outside of gas is excluded.

## Solution

Since Gas is target-specific, the idea consists in detecting whether dwarf2.def is included from Gas or somewhere else. To do so, a new define MODULE_GAS was added in configure.ac to allow to detect the inclusion provenance, and the two behaviors can be observed in dwarf2.def:
- if MODULE_GAS is defined (included from gas folder), the architecture-specific DWARF instructions are enabled/disabled via TC_<arch> defines
- if MODULE_GAS is undefined (included from bfd and binutils folders), all the DWARF instructions are defined (as it is today).
In bfd, the architecture of the input object is detected at runtime using bfd_get_arch, and only the architecture-specific instructions matching the target of the object are considered during the processing.

This patch deliberately excluded from the scope binutils/dwarf.c that also requires a check of the architecture at runtime. This part will be addressed in a follow-up patch.

Tested on aarch64-none-linux-gnu, and no regression found.

Regards,
Matthieu.
  
[1]: https://inbox.sourceware.org/binutils/eaedfc6c-5666-4a05-a676-d25bf52b76c1@suse.com/
[2]: https://inbox.sourceware.org/binutils/20241211120553.1391850-1-matthieu.longo@arm.com/
[3]: https://github.com/ARM-software/abi-aa/blob/main/aadwarf64/aadwarf64.rst#id1

Matthieu Longo (1):
  Enable arch-specific CFI directives and DWARF instructions only when required by the target (part 1)

 bfd/elf-eh-frame.c | 90 +++++++++++++++++++++++++++++++++++++++-------
 gas/config.in      |  3 ++
 gas/configure      |  4 +++
 gas/configure.ac   |  2 ++
 gas/dw2gencfi.c    | 33 ++++++++++++++---
 gas/gen-sframe.c   | 35 ++++--------------
 include/dwarf2.def | 38 ++++++++++++++++----
 7 files changed, 154 insertions(+), 51 deletions(-)