71 Commits

Author SHA1 Message Date
Indu Bhagat
a806da18ad libsframe: bugfix in flip_sframe
A previous commit bdb0d62281 to make flip_fde version aware added a new
argument for passing the remaning buffer size to the involved functions.
Fix the passed value to the intended.

libsframe/
        * sframe.c (flip_sframe): Correct the passed value of buf size.
2025-11-25 20:46:08 -08:00
Indu Bhagat
b91966e2df libsframe: rename encoder to ectx for readability
Addressing (an old) review comment suggesting this housekeeping item.
Use consistent naming style in libsframe.  sframe_decoder_ctx objects
are named 'dctx', so use 'ectx' for sframe_encoder_ctx objects.

Make necessary changes in all the applicable declarations and definitions.

Reviewed-by: Jens Remus <jremus@linux.ibm.com>
2025-11-09 00:34:27 -08:00
Indu Bhagat
b263aca0a5 libsframe: fix checks in flip_fde
Adjust the sanity checks for flip_fde workflow and optional trailing
section padding to account for the case of ihp->sfh_fdeoff != 0 or
ihp->sfh_freoff != total FDEs size.

Reviewed-by: Jens Remus <jremus@linux.ibm.com>

libsframe/
        * sframe.c (flip_sframe): Fix checks in flip_fde to accommodate
	cases when sfh_fdeoff != 0 or when SFrame FREs are placed after
	a gap from SFrame FDEs.
2025-11-08 23:33:22 -08:00
Jan Dubiec
84402f09dd sframe: Minor format string fix in sframe_decode
The type of fidx_size is size_t so the proper length modifier is not "l"
but "z".

libsframe/
    * sframe.c (sframe_decode): Fix format string (length modifier)
    for fidx_size.

Signed-off-by: Jan Dubiec <jdx@o2.pl>
2025-11-06 14:44:16 -08:00
Indu Bhagat
3ae28d0776 libsframe: use sf_fde_tbl data structure internally for the decoder
Instead of the current sframe_func_desc_entry (on-disk format
representation) data structure.

The decoder context in libsframe, so far, has been internally directly
tied to the sframe_func_desc_entry (on-disk format representation) data
structure.  While this allows libsframe to avoid some operations, this
is not desirable anymore as the format evolves: we will need to support
reading in of older version(s) of SFrame FDE, as well as a newer on-disk
representations for SFrame FDE.

Use sf_fde_tbl internally in the decoder context.  Note that libsframe
already does _not_ use sframe_func_desc_entry in any external-facing,
user-visible APIs.

Note that this commit is simply preparatory in nature.  At the moment,
the 'sf_fde_tbl' internally uses the sframe_func_desc_entry (on-disk
format representation).  When need arises (as SFrame FDE evolves), we
may change sf_fde_tbl to use an alternative (but still libsframe
internal) definition of SFrame FDE.

lisbframe/
        * sframe-impl.h (sf_fde_tbl, sf_fre_tbl): Move definition before use.
	Use sf_fde_tbl instead of sframe_func_desc_entry in struct
        sframe_decoder_ctx.
        * sframe.c (sframe_fde_tbl_alloc): New internal definition.
        (sframe_fde_tbl_init): Likewise.
        (sframe_decoder_get_funcdesc_at_index): Adjust for sf_fde_tbl
	usage.
        (sframe_decoder_get_secrel_func_start_addr): Likewise.
        (sframe_fre_check_range_p): Likewise.
        (sframe_decode): Likewise.
        (sframe_get_funcdesc_with_addr_internal): Likewise.
2025-11-01 01:47:29 -07:00
Indu Bhagat
f264ead9fb libsframe: make flip_header version aware
Future versions of the format may have alternative representation of an
FDE.  As the format evolves, endian flipping of the SFrame header may
need to be version aware.

flip_header () now takes the SFrame version as argument and also returns
SFRAME_ERR in case of error.  Currently the SFrame version as argument
remains unused.

SFrame encoder, at the momemnt, writes the SFrame data in the most recent
format version by default.

libsframe/
        * sframe.c (flip_header): Make version aware.
        (sframe_decode): Adjust usage of flip_header.
        (sframe_encoder_write): Likewise.
2025-11-01 01:42:02 -07:00
Indu Bhagat
bdb0d62281 libsframe: make flip_fde version aware
Future versions of the format may have a different representation of an
SFrame FDE.  As the format evolves, endian flipping will need to be version
aware.

Refactor flip_fde a bit by carving out an internal sframe_decode_fde API
which can read information from an on-disk SFrame FDE.

libsframe/
        * sframe.c (flip_fde): Make version aware.
        (sframe_decode_fde): New internal definition.
        (flip_sframe): Use the new definitions.
2025-11-01 01:35:34 -07:00
Indu Bhagat
76d2c7d416 libsframe: use const qualifier for sframe_header object
...where applicable.

For the static functions that do not modify the sframe_header object,
use const.  Ditto for local vars.

Reviewed-by: Jens Remus <jremus@linux.ibm.com>

libsframe/
        * sframe.c (sframe_get_hdr_size): Use const qualifier.
        (sframe_header_sanity_check_p): Likewise.
        (flip_sframe): Use const for local var.
        (sframe_decode): Likewise.  While at it, use similar looking var
	name.
        (sframe_decoder_get_hdr_size): Use const for local var.
        (sframe_decoder_get_abi_arch): Likewise.
        (sframe_decoder_get_version): Likewise.
        (sframe_decoder_get_fixed_fp_offset): Likewise.
        (sframe_decoder_get_fixed_ra_offset): Likewise.
        (sframe_get_funcdesc_with_addr_internal): Likewise.
        (sframe_decoder_get_num_fidx): Likewise.
        (sframe_encoder_get_hdr_size): Likewise.
        (sframe_encoder_get_abi_arch): Likewise.
        (sframe_encoder_get_version): Likewise.
        (sframe_encoder_get_num_fidx): Likewise.
2025-10-19 21:12:20 -07:00
Indu Bhagat
ffe711c381 libsframe: fix warning about argument of sframe_fre_sanity_check_p
Recent commit (6ca8915c) added a new API sframe_fre_get_ra_undefined_p
(). It has a 'const sframe_frame_row_entry *fre' argument, causing a
warning in function ‘sframe_fre_get_ra_undefined_p’:

libsframe/sframe.c:794:50: warning: passing argument 1 of
‘sframe_fre_sanity_check_p’ discards ‘const’ qualifier from pointer
target type [-Wdiscarded-qualifiers]
  794 |   if (fre == NULL || !sframe_fre_sanity_check_p (fre))
      |                                                  ^~~
libsframe/sframe.c:293:52: note: expected ‘sframe_frame_row_entry *’ but
argument is of type ‘const sframe_frame_row_entry *’
  293 | sframe_fre_sanity_check_p (sframe_frame_row_entry *frep)
      |                            ~~~~~~~~~~~~~~~~~~~~~~~~^~~~

Fix it by using const qualifier.

libsframe/
	* sframe.c (sframe_fre_sanity_check_p): Use const.
2025-10-16 11:21:02 -07:00
Jens Remus
5079bf8959 gas: sframe: Represent .cfi_undefined RA as FRE without offsets
In DWARF CFI an "undefined" register rule for the return address (RA)
register indicates that there is no return address and the stack trace
is complete.

Represent DW_CFA_undefined as SFrame FRE without any offsets, so that a
stack tracer implementation can use this as indication that an outermost
frame has been reached and the stack trace is complete.

This representation is backward compatible, as existing stack tracers
should already deal with the case, that an SFrame FRE a so far invalid
offset count of zero and stop the trace.

include/
	* sframe.h (SFRAME_V2_FRE_RA_UNDEFINED_P): New macro to test
	FRE info word for RA undefined (FRE without any offsets).

binutils/
	* NEWS: Mention SFrame can represent an undefined RA as FRE
	without	any offsets.

gas/
	* gen-sframe.h (struct sframe_row_entry): Add ra_undefined_p
	flag.
	* gen-sframe.c (sframe_row_entry_new): Initialize ra_undefined_p
	flag to not set.
	(sframe_row_entry_initialize): Treat ra_undefined_p flag as
	sticky.
	(sframe_fre_set_ra_track): Reset ra_undefined_p flag.
	(sframe_xlate_do_restore): Reset ra_undefined_p flag to saved
	state.
	(sframe_xlate_do_same_value): Reset ra_undefined_p flag.
	(sframe_xlate_do_cfi_undefined): For RA set ra_undefined_p flag.
	(output_sframe_row_entry): Represent RA undefined as SFrame FRE
	without any offsets and FRE info word fields zeroed.
	* NEWS: Mention assembler represents .cfi_undefined RA in SFrame
	as FRE without any offsets.

libsframe/
	* doc/sframe-spec.texi (Changes from Version 1 to Version 2):
	Mention that a SFrame FRE without any offsets flag indicates an
	outermost frame with an undefined RA.
	(fre_offset_count): Document that a FRE offset count of zero
	indicates an outermost frame with an undefined RA.
	* sframe.c (sframe_get_fre_ra_undefined_p): Use macro
	SFRAME_V2_FRE_RA_UNDEFINED_P.
	(sframe_fre_get_fp_offset, sframe_fre_get_ra_offset): Do not
	return fixed FP/RA offset if RA undefined.
	* sframe-dump.c (dump_sframe_func_with_fres): Show FRE without
	any offsets as "RA undefined".

gas/testsuite/
	* gas/cfi-sframe/cfi-sframe.exp: Run tests for .cfi_undefined RA
	on AArch64, s390x, and x86-64.
	* gas/cfi-sframe/cfi-sframe-aarch64-ra-undefined-1.d: Add test
	for .cfi_undefined RA on AArch64.
	* gas/cfi-sframe/cfi-sframe-aarch64-ra-undefined-1.s: Likewise.
	* as/cfi-sframe/cfi-sframe-s390x-ra-undefined-1.d: Add test
	for .cfi_undefined RA on s390x.
	* gas/cfi-sframe/cfi-sframe-s390x-ra-undefined-1.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-ra-undefined-1.d: Add test
	for .cfi_undefined RA on x86-64.
	* gas/cfi-sframe/cfi-sframe-x86_64-ra-undefined-1.s: Likewise.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-10-15 17:45:48 +02:00
Jens Remus
6ca8915c61 include: libsframe: Add API to get RA undefined
SFrame FREs without any offsets will later be used to represent an
undefined return address (RA) in SFrame.  This API can then be used,
for instance by libsframe when dumping SFrame stack trace information
(e.g. in objdump and readelf), to test for RA undefined.  Other users
of libsframe need the same capability.

include/
	* sframe-api.h (sframe_fre_get_ra_undefined_p): New declaration.

libsframe/
	* libsframe.ver (sframe_fre_get_ra_undefined_p): List new API.
	* sframe.c (sframe_fre_get_ra_undefined_p): New definition.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-10-15 17:45:48 +02:00
Jens Remus
52308aeca8 libsframe: s390: No further decode if sframe_get_fre_offset returns err
SFrame FREs without any offsets will later be used to represent
.cfi_undefined RA in SFrame.

As a result the API to get the CFA offset can return an error value, if
there are no offsets.  Do not apply the s390x-specific decoding of CFA
offset on the error return value.

libsframe/
	* sframe.c (sframe_fre_get_cfa_offset): Do not apply s390x-
	specific decoding to error return value.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-10-15 17:45:48 +02:00
Jens Remus
c47ec3b44a gas: ld: libsframe: Support for SFrame FDEs without any FREs
Allow SFrame sections without any FREs, that can occur if they solely
contain FDEs without any FREs.  For FDEs without and FREs set the
offset to the first FRE to zero.

libsframe/
	* sframe.c (sframe_encoder_write_sframe): Allow SFrame sections
	without any FREs.  For FDEs without any FREs set the offset to
	the first FRE to zero.

gas/
	* gen-sframe.c (output_sframe_funcdesc): For FDEs without any
	FREs set the offset to the first FRE to zero.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-10-15 17:45:48 +02:00
Indu Bhagat
0d90e0ce80 libsframe: use offsets to FDE and FRE sub-sections
In SFrame format, the SFrame FDEs begin at an offset 'sfh_fdeoff' from
the end of the SFrame header.  Similarly, the SFrame FREs begin at an
offset 'sfh_freoff' from the end of the SFrame header.

While the GNU assembler generates these subsections such that sfd_fdeoff
is zero, the correct way to decode the SFrame section (conforming with
the specification) is to use both sfh_fdeoff and sfh_freoff in
sframe_decode.

libsframe/
	* sframe.c (sframe_decode): Use offsets to SFrame FDE and FRE
	sub-sections as applicable.
2025-08-23 12:10:31 -07:00
Indu Bhagat
387efef5fe libsframe: relax the assertion limit for fre_start_addr
Fix PR ld/33131 Failed assertion when linking gccgo

Make amendments in both sframe_decoder_get_fre and
sframe_encoder_add_fre.

Since GNU as and the dw2gencfi code generally accepts such CFI, its best
to allow in SFrame FREs too.

libsframe/
	PR ld/33131.
	* sframe.c (sframe_decoder_get_fre): Relax the assertion a bit.
	(sframe_encoder_add_fre): Likewise.
2025-07-14 17:08:21 -07:00
Indu Bhagat
6a959b1270 libsframe: bump version to 2.0
Remove LIBSFRAME_1.1, LIBSFRAME_1.0 nodes and add a new LIBSFRAME_2.0
node (non-inheritance version) to create new global versioned symbols.
Also announce libsframe.so.2 in NEWS.

New APIs:
     sframe_decoder_get_flags;
     sframe_decoder_get_offsetof_fde_start_addr;
     sframe_encoder_get_flags;
     sframe_encoder_get_offsetof_fde_start_addr;

Removed APIs: (already deprecated since X-2 release)
     sframe_get_funcdesc_with_addr;

APIs with changed semantics:
     sframe_decoder_get_funcdesc_v2;
     sframe_encoder_add_funcdesc_v2;
     sframe_encoder_write;

lisbframe/
	* libsframe.ver: Define new LIBSFRAME_2.0.
	* libtool-version: Bump the 'current' numeral to indicate a binary
	incompatible release.
include/
	* sframe-api.h (sframe_get_funcdesc_with_addr): Remove
	deprecated interface.
libsframe/
	* sframe.c (sframe_get_funcdesc_with_addr): Likewise.
binutils/
	* NEWS: Announce new versioned release of libsframe.
2025-07-12 01:09:17 -07:00
Indu Bhagat
ddcac08407 libsframe: fixup comment and minor style issues
Also, use ATTRIBUTE_UNUSED consistently.

libsframe/
	* sframe.c (sframe_encoder_add_funcdesc): Fix function-level
	comment and use ATTRIBUTE_UNUSED consistently.
	(sframe_encoder_add_funcdesc_v2): Use ATTRIBUTE_UNUSED
	consistently.
2025-07-12 00:10:36 -07:00
Jens Remus
c1056133a3 s390: Store SFrame CFA offset adjusted
In SFrame V2 the size of the offsets following an SFrame FRE can be
either signed 8-bit, 16-bit, or 32-bit integer, with the largest offset
determining their size:
  1. CFA offset from CFA base register
  2. RA (stack save slot) offset from CFA, usually -48 on s390x if saved
  3. FP (stack save slot) offset from CFA, usually -72 on s390x if saved
The FP and RA offsets from CFA, when FP/RA saved on the stack, usually
have fixed values that fit into signed 8-bit SFrame offsets.  Likewise
the DWARF register numbers on s390x of general registers (GR; 0-15) and
floating-point registers (FPR; 16-31), when FP/RA saved in registers.
With that the CFA offset from CFA base register has the greatest impact
on the signed SFrame offset size.

The s390x ELF ABI [1] defines the CFA as stack pointer (SP) at call
site +160. [2]  Therefore the minimum CFA offset from CFA base register
on s390x is 160.  This does not fit into a signed 8-bit integer and
therefore effectively prevents any use of signed 8-bit SFrame offsets
on s390x.

For s390x store the CFA offset from CFA base register adjusted by -160
to enable the use of signed 8-bit SFrame offsets.

[1]: s390x ELF ABI, https://github.com/IBM/s390x-abi/releases
[2]: s390x ELF ABI, commit 4e38ad9c8a88 ("Document the CFA"),
     https://github.com/IBM/s390x-abi/commit/4e38ad9c8a88

include/
	* sframe.h (SFRAME_S390X_CFA_OFFSET_ADJUSTMENT): Define
	s390x-specific CFA offset adjustment.
	(SFRAME_V2_S390X_CFA_OFFSET_ENCODE,
	SFRAME_V2_S390X_CFA_OFFSET_DECODE): New s390x-specific
	macros.  Use SFRAME_S390X_CFA_OFFSET_ADJUSTMENT to en-/decode
	CFA offset.

bfd/
	* elf64-s390.c (elf_s390x_sframe_plt_fre): Use
	SFRAME_V2_S390X_CFA_OFFSET_ENCODE on CFA offset to store it
	adjusted and switch to 8-bit offsets.

gas/
	* gen-sframe.c (sframe_fre_set_cfa_offset): For s390x use
	SFRAME_V2_S390X_CFA_OFFSET_ENCODE on CFA offset to store it
	adjusted.
	(sframe_fre_get_cfa_offset): New helper.  For s390x use
	SFRAME_V2_S390X_CFA_OFFSET_DECODE on CFA offset to undo its
	adjustment.
	(sframe_xlate_do_def_cfa_register): Use new helper
	sframe_fre_get_cfa_offset.

libsframe/
	* sframe.c (sframe_fre_get_cfa_offset): For s390x use
	SFRAME_V2_S390X_CFA_OFFSET_DECODE on CFA offset to undo its
	adjustment.
	* doc/sframe-spec.texi (s390x,
	SFRAME_S390X_CFA_OFFSET_ADJUSTMENT,
	SFRAME_V2_S390X_CFA_OFFSET_ENCODE,
	SFRAME_V2_S390X_CFA_OFFSET_DECODE): Document s390x-specific
	adjustment of CFA offset.

libsframe/testsuite/
	* libsframe.find/plt-findfre-2.c (add_plt0_fde, add_pltn_fde):
	Use SFRAME_V2_S390X_CFA_OFFSET_ENCODE to enable use of 1-byte
	SFrame offsets.

Suggested-by: Indu Bhagat <indu.bhagat@oracle.com>
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-07-11 10:29:40 +02:00
Jens Remus
955570f097 s390: Represent FP without RA saved in SFrame
If an architecture uses both SFrame RA and FP tracking SFrame assumes
that the RA offset is the 2nd offset and the FP offset is the 3rd offset
following a SFrame FRE.  An architecture does not necessarily need to
save both on the stack (or in register) at the same time or even at all.
SFrame cannot represent FP without RA saved on stack (or in a register),
since it cannot distinguish whether the 2nd offset is the RA or FP
offset.

For s390x use an invalid SFrame RA offset from CFA value of zero as
padding to represent the FP being saved when the RA is not saved.  This
aligns with the existing invalid SFrame fixed RA offset from CFA value
of zero.  In a stack tracer this then also naturally falls into place,
as it can skip restoring the RA in the topmost frame, if both the fixed
RA offset (from SFrame header) and the RA offset (from FDE) are zero,
without any need to test architecture-specific flags.

include/
	* sframe.h (SFRAME_FRE_RA_OFFSET_INVALID): New define.  Used as
	padding offset.
	* sframe-api.h (sframe_fre_get_ra_offset): Add comment that for
	s390x an offset value of SFRAME_FRE_RA_OFFSET_INVALID indicates
	that the RA is not saved.

gas/
	* gen-sframe.c (get_fre_num_offsets): For s390x account padding
	RA offset, if FP without RA saved.
	(sframe_get_fre_offset_size): Likewise.
	(output_sframe_row_entry): For s390x write a padding RA offset,
	if FP without RA needs to be represented.
	(sframe_do_fde): Enable FP without RA saved to be represented
	on s390x.

libsframe/
	* sframe.c (sframe_fre_get_ra_offset): Add comment that for
	s390x an offset value of SFRAME_FRE_RA_OFFSET_INVALID indicates
	that the RA is not saved.
	* sframe-dump.c (dump_sframe_func_with_fres): Treat invalid
	RA offsets as if they were undefined.  Display them as "U"
	to distinguish them.
	* doc/sframe-spec.texi (s390x): Document s390x-specific use of
	SFRAME_FRE_RA_OFFSET_INVALID to represent FP without RA saved.

gas/testsuite/
	* gas/cfi-sframe/cfi-sframe.exp: Rename s390x-specific tests.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-offset-err-1.s: Rename
	to ...
	* cfi-sframe/cfi-sframe-s390x-fpra-offset-err-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-offset-2.s: This.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-offset-2.d: Likewise.
	Update test verification pattern accordingly.
	* cfi-sframe/cfi-sframe-s390x-fpra-register-err-1.s: Rename
	to ...
	* cfi-sframe/cfi-sframe-s390x-fpra-register-err-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-2.s: This.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-2.d: Likewise.
	Update test verification pattern accordingly.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-07-11 10:29:40 +02:00
Jens Remus
61b808e087 s390: Represent FP/RA saved in register in SFrame
GCC on s390x, when in a leaf function, can be observed to save the
frame pointer (FP) and/or return address (RA) register in a floating-
point registers (FPR) instead of on the stack.  This is declared using
the following CFI directive:

  .cfi_register <fp/ra-regnum>, <fpr-regnum>

SFrame cannot represent the FP and/or RA being saved in another
register.  It does only track the CFA base register (SP/FP), CFA offset
from CFA base register, and FP and RA save area offsets from CFA.

On s390x the FP and/or RA are only saved in another FPR when in a leaf
function.  That is a function that does not call any other function.
Therefore it can ever only be the topmost function in a call chain.
An unwinder by default has access to all registers of the function that
is the topmost on the call stack.  Therefore no further information
is required to restore FP/RA from the FPR.

Represent FP/RA saved in another register on s390x, by encoding the
DWARF register number shifted by one to the left with the least-
significant bit set in the offset as follows:

  offset = (regnum << 1) | 1

The use of the least-significant bit of the offset as indication is
possible, as the stack pointer (SP), the CFA, and any register save
area slots are 8-byte aligned according to the s390x ELF ABI:
- The stack pointer (SP) "shall maintain an 8-byte alignment". [1]
- The CFA is defined as SP at call site +160. [2]
- Pointers and 8-byte integers, such as general register values, must
  be 8-byte aligned. [3]
SFrame FP and RA stack offsets must therefore always be a multiple of
8 on s390x.  Note that for the same reason the DWARF data alignment
factor is -8 on s390x (see DWARF2_CIE_DATA_ALIGNMENT).

Add s390x-specific SFrame (error) tests for FP/RA saved in FPRs in leaf
function.

[1]: s390x ELF ABI, sections "Register Roles" and "Stack Frame
     Allocation", https://github.com/IBM/s390x-abi/releases
[2]: s390x ELF ABI, commit 4e38ad9c8a88 ("Document the CFA"),
     https://github.com/IBM/s390x-abi/commit/4e38ad9c8a88
[3]: s390x ELF ABI, section "Fundamental Types", table "Scalar types",
     https://github.com/IBM/s390x-abi/releases

include/
	* sframe.h (SFRAME_V2_S390X_OFFSET_IS_REGNUM): New s390x-
	specific macro to test whether an SFrame FP/RA offset is a DWARF
	register number.
	(SFRAME_V2_S390X_OFFSET_ENCODE_REGNUM): New s390x-specific macro
	to encode a DWARF register number into an SFrame FP/RA offset.
	(SFRAME_V2_S390X_OFFSET_DECODE_REGNUM): New s390x-specific macro
	to decode an SFrame FP/RA offset into a DWARF register number.
	* sframe-api.h (sframe_fre_get_fp_offset,
	sframe_fre_get_fp_offset): Add comment that for s390x the offset
	may be an encoded register number.

gas/
	* gen-sframe.c (s390_sframe_xlate_do_register): New S390-
	specific function.  Uses SFRAME_V2_S390X_OFFSET_ENCODE_REGNUM to
	represent FP/RA saved in another register on s390x.
	(sframe_xlate_do_register): Invoke s390_sframe_xlate_do_register
	on s390x.

libsframe/
	* sframe.c (sframe_fre_get_fp_offset, sframe_fre_get_fp_offset):
	Add comment that for s390x the offset may be an encoded register
	number.
	* sframe-dump.c (is_sframe_abi_arch_s390x): New helper to test
	whether ABI/arch is s390x.
	(dump_sframe_func_with_fres): Use
	SFRAME_V2_S390X_OFFSET_IS_REGNUM and
	SFRAME_V2_S390X_OFFSET_DECODE_REGNUM to dump FP/RA saved in
	another register on s390x.
	* doc/sframe-spec.texi (s390x): Document s390x-specific
	representation of FP/RA saved in another register.

gas/testsuite/
	* gas/cfi-sframe/cfi-sframe.exp: Update s390x-specific SFrame
	(error) tests.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-err-2.s: Rename
	to ...
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-err-2.d:
	Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-1.s: This.  Test
	no longer triggers a warning, as SFrame can represent FP and RA
	saved in registers.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-err-1.d: Test
	now triggers a different warning, as SFrame can represent FP and
	RA saved in registers, but not FP without RA saved in register.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-07-11 10:29:40 +02:00
Jens Remus
d27d82f560 s390: Initial support to generate .sframe from CFI directives in assembler
This introduces initial support to generate .sframe from CFI directives
in assembler on s390 64-bit (s390x).  Due to SFrame V2 format
limitations it has the following limitations, some of them getting
addressed by subsequent patches, which cause generation of SFrame FDE
to be skipped:

- SFrame FP/RA tracking only supports register contents being saved on
  the stack (i.e. .cfi_offset).  It does not support FP/RA register
  contents being saved in other registers (i.e. .cfi_register).  GCC on
  s390x can be observed to save the FP/RA register contents in floating-
  point registers, but only in leaf functions.
  This issue is detailed further and resolved in the subsequent commit
  "s390: Represent FP/RA saved in register in SFrame".

- SFrame FP/RA tracking cannot represent FP without RA saved.  This is
  because the format assumes SFrame FDE offset2 to be the RA offset, if
  there are two offsets, and offset3 to be the FP offset, if there are
  three offsets.  There is no mean to distinguish whether offset2 is the
  RA or FP offset, if there are only two offsets.
  This issue is detailed further and resolved in the subsequent commit
  "s390: Represent FP without RA saved in SFrame".

- SFrame assumes a dedicated FP register number.  The s390x ELF ABI [1]
  does only designate register 11 as preferred FP register number.  In
  general GCC and Clang on s390x use register 11 as frame pointer.
  GCC on s390x can be observed to use register 14 as frame pointer in
  the stack clash protector in the function prologue.
  glibc on s390x contains hand-written assembler code that uses
  register 12 as frame pointer.

This s390x support is largely based on the AArch64 support from commit
b52c4ee466 ("gas: generate .sframe from CFI directives").

The SFrame ABI/arch identifier SFRAME_ABI_S390X_ENDIAN_BIG is introduced
for s390x and added to the SFrame format specification.

The s390x ELF ABI [1] specifies the following C calling conventions for
s390x architecture:
- Register 15 is the stack pointer (SP).
- Register 14 contains the return address (RA) at function entry.
- There is no dedicated frame pointer register.  Register 11 is the
  preferred frame pointer (FP). [2]  GCC and Clang in general use
  register 11 as frame pointer.
- The CFA is defined as SP at call site +160. [3]  The SP at call site
  can therefore be derived from the CFA using a SP value offset from CFA
  of -160.

The s390x ELF ABI [1] does not assign any standard save slot to each
register in the register save area of a stack frame.  Neither the
return address (RA, r14) nor preferred frame pointer (FP, r11)
necessarily need to be saved.  Therefore SFrame RA and FP tracking is
used.

Support for SFrame on s390 is only enabled for the 64-bit s390x ELF ABI
(z/Architecture with 64-bit addressing mode).  It is disabled for the
32-bit s390 ELF ABI (ESA/390 or z/Architecture with 32-bit addressing
mode).

s390x-specific SFrame assembler and linker tests are added, including
error tests for use of a non-preferred frame pointer (FP) register and
specification of a non-default return address (RA) register.

[1]: s390x ELF ABI, https://github.com/IBM/s390x-abi/releases
[2]: s390x ELF ABI, commit f00421825979 ("Add information about the frame
     pointer register"),
     https://github.com/IBM/s390x-abi/commit/f00421825979
[3]: s390x ELF ABI, commit 4e38ad9c8a88 ("Document the CFA"),
     https://github.com/IBM/s390x-abi/commit/4e38ad9c8a88

include/
	* sframe.h: Add reference to s390x architecture in comments.
	(SFRAME_ABI_S390X_ENDIAN_BIG): Define SFrame ABI/arch identifier
	for s390x.
	(SFRAME_S390X_SP_VAL_OFFSET): Define s390x-specific SP value
	offset from CFA.

libsframe/
	* sframe.c (need_swapping): Add SFRAME_ABI_S390X_ENDIAN_BIG.
	* doc/sframe-spec.texi (SFRAME_ABI_S390X_ENDIAN_BIG, s390x,
	SFRAME_S390X_SP_VAL_OFFSET): Document SFrame ABI/arch identifier
	for s390x, add references to s390x architecture, and document
	s390x-specifics, such as the SP value offset from CFA of -160.

gas/
	* config/tc-s390.h: s390x support to generate .sframe from CFI
	directives in assembler.
	(support_sframe_p): Define.
	(SFRAME_CFA_SP_REG, SFRAME_CFA_FP_REG, SFRAME_CFA_RA_REG):
	Define.
	(sframe_ra_tracking_p): Define.
	(sframe_cfa_ra_offset): Define.
	(sframe_get_abi_arch): Define.
	* config/tc-s390.c: s390x support to generate .sframe from CFI
	directives in assembler.
	(s390_sframe_cfa_sp_reg, s390_sframe_cfa_fp_reg,
	s390_sframe_cfa_ra_reg): New.  Initialize to DWARF register
	numbers of stack pointer (SP, r15), preferred frame pointer
	(FP, r11), and return address (RA, r14) registers.
	(s390_support_sframe_p): New function.  Return true if s390x.
	(s390_sframe_ra_tracking_p): New function.  Return true.
	(s390_sframe_cfa_ra_offset): New function.  Return
	SFRAME_CFA_FIXED_RA_INVALID.
	(s390_sframe_get_abi_arch): New function.  Return
	SFRAME_ABI_S390X_ENDIAN_BIG if s390x, otherwise zero.
	* gen-sframe.c: Add reference to s390x architecture in comments.
	(sframe_xlate_do_val_offset): Add support for s390x-specific
	SFRAME_S390X_SP_VAL_OFFSET.
	* NEWS: Add news entry.

gas/testsuite/
	* gas/cfi-sframe/cfi-sframe.exp: Enable common SFrame tests for
	s390x.  Add s390x-specific SFrame (error) tests.
	* gas/cfi-sframe/cfi-sframe-s390x-1.d: New s390x-specific SFrame
	test.
	* gas/cfi-sframe/cfi-sframe-s390x-1.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-2.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-err-1.d: New s390x-specific
	SFrame error test that uses a non-default frame-pointer register
	as CFA base register.
	* gas/cfi-sframe/cfi-sframe-s390x-err-1.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-err-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-err-2.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-err-3.d: New s390x-specific
	SFrame error test that uses a non-default return address
	register.
	* gas/cfi-sframe/cfi-sframe-s390x-err-3.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-offset-1.d: New s390x-
	specific SFrame test that saves RA and FP individually on the
	stack.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-offset-1.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-offset-err-1.d: New
	s390x-specific SFrame error test that saves FP and RA
	individually, to trigger FP without RA saved.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-offset-err-1.s: Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-err-1.d: New
	s390x-specific SFrame error test that saves FP and RA
	individually in registers.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-err-1.s:
	Likewise.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-err-2.d: New
	s390x-specific SFrame error test that saves RA and FP
	individually in registers.
	* gas/cfi-sframe/cfi-sframe-s390x-fpra-register-err-2.s:
	Likewise.

ld/testsuite/
	* ld-s390/s390.exp: Add simple SFrame test.
	* ld-s390/sframe-simple-1.d: New simple SFrame test.
	* ld-s390/sframe-bar.s: Likewise.
	* ld-s390/sframe-foo.s: Likewise.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-07-11 10:29:40 +02:00
Jens Remus
b36a8e57ea sframe: Ignore section padding when converting endianness
The .sframe section may have a trailing padding due to the architecture-
specific default section alignment.  Do not treat this padding as error
when converting between target and host endianness.

This can be observed when building Binutils with SFrame s390x support on
x86-64 for s390x using configure option "--target=s390x-ibm-linux-gnu"
and running the GAS test suite.

While at it reuse the determined SFrame section header size.

libsframe/
	* sframe.c (flip_sframe): Ignore .sframe section padding.  Reuse
	SFrame header size.

Reported-by: Indu Bhagat <indu.bhagat@oracle.com>
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-07-11 10:29:40 +02:00
Indu Bhagat
0b1bf2fc98 bfd: gas: ld: libsframe: adopt new encoding for FDE func start addr field
This patch convenes a set of changes in bfd, gas, ld, libsframe towards
moving to the new encoding for the 'sfde_func_start_address' field in
SFrame FDE.

First, gas must now mark all SFrame sections with the new flag
SFRAME_F_FDE_FUNC_START_PCREL.  gas was already emitting the field
in the said encoding.

	* gas/gen-sframe.c (output_sframe_internal): Emit the flag
	SFRAME_F_FDE_FUNC_START_PCREL.

Similarly for ld, adopt the new semantics of sfde_func_start_address
consistently.  This means:
  - When merging SFrame sections, check that all input SFrame sections
    have the SFRAME_F_FDE_FUNC_START_PCREL flag set.  If the check
    fails, ld errors out.
  - When merging SFrame sections, keep even the in-memory contents of
    the FDE function start address (buffer passed to libsframe
    sframe_encoder_write () for writing out) encoded in the new
    semantics.  While it is, in theory, possible that instead of doing this
    change here, we adjust the value of sfde_func_start_address at the final
    write (sframe_encoder_write) time.  But latter is not favorable for
    maintenanance and may be generally confusing for developers.
  - When creating SFrame for PLT entries, emit flag
    SFRAME_F_FDE_FUNC_START_PCREL.

include/
        * sframe-api.h (SFRAME_F_LD_MUSTHAVE_FLAGS): New definition.
bfd/
	* elf-sframe.c (_bfd_elf_merge_section_sframe): Check for flag
	combinatation SFRAME_F_LD_MUSTHAVE_FLAGS set for all input and
	output SFrame sections.  If not, error out.  Also, adopt the new
        semantics of function start address encoding.
	* bfd/elfxx-x86.c (_bfd_x86_elf_create_sframe_plt): Emit flag
	SFRAME_F_FDE_FUNC_START_PCREL.

Next, for dumping SFrame sections, now that we are emitting the same
encoding in GAS, non-relocatable and relocatable SFrame links, it is the
time to set relocate to TRUE in debug_displays[].

binutils/
	* dwarf.c (struct dwarf_section_display): Allow sframe sections
	  to now be relocated.
gas/testsuite/
	* gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d: Update the
	test.  Relocatable SFrame sections now display non-zero value
	(appropriate function start address).

Now, as the SFrame sections on-disk and in-memory use the new semantics of
sfde_func_start_address encoding (i.e., function start address is the
offset from the sfde_func_start_address field to the start PC), the
calculation to make it human readable (i.e., relatable to the addresses
in .text sections) needs adjustment.

libsframe/
	* sframe-dump.c (dump_sframe_func_with_fres): Adjust the
	function start address for dumping.

Now that both the emission of the new encoding, and the relocation of
sections before dumping them is in place, it is time to adjust the
testcases.

gas/testsuite/
	* gas/cfi-sframe/cfi-sframe-aarch64-1.d: Update expected output
	to include SFRAME_F_FDE_FUNC_START_PCREL instead of NONE.
	* gas/cfi-sframe/cfi-sframe-aarch64-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-aarch64-3.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-aarch64-4.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-10.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-11.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-3.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-4.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-5.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-6.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-7.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-8.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-9.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-3.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-empty-4.d: Likewise.
	* gas/cfi-sframe/common-empty-1.d: Likewise.
	* gas/cfi-sframe/common-empty-2.d: Likewise.
	* gas/cfi-sframe/common-empty-3.d: Likewise.
	* gas/scfi/x86_64/scfi-cfi-sections-1.d: Likewise.
	* gas/scfi/x86_64/scfi-dyn-stack-1.d: Likewise.
ld/testsuite/
	* ld-aarch64/sframe-simple-1.d: Update expected output to
	include SFRAME_F_FDE_FUNC_START_PCREL.
	* ld-x86-64/sframe-ibt-plt-1.d: Likewise.
	* ld-x86-64/sframe-plt-1.d: Likewise.
	* ld-x86-64/sframe-pltgot-1.d: Likewise.
	* ld-x86-64/sframe-pltgot-2.d: Likewise.
	* ld-x86-64/sframe-simple-1.d: Likewise.

Naturally, the change of semantics for 'SFrame FDE function start address'
has consequences on the implementation in libsframe.  As per the new
semantics:
  - Function start address in the SFrame FDE (sfde_func_start_address)
    is an offset from the FDE function start address field to the start
    PC of the associated function.

Note that, the libsframe library brings the SFrame section contents into
its own memory to create a sframe_decoder_ctx object via sframe_decode
().  Many internal and user-interfacing APIs then may use
sframe_decoder_ctx object to interact and fulfill the work.

In context of changing semantics for sfde_func_start_address, following
relevant examples may help understand the impact:
  - sframe_find_fre () finds a the SFrame stack trace data (SFrame FRE)
    given a lookup offset (offset of lookup_pc from the start of SFrame
    section).  Now that the sfde_func_start_address includes the
    distance from the sfde_func_start_address field to the start of
    SFrame section itself, the comparison checks of
    sfde_func_start_address with the incoming lookup offset need
    adjustment.
  - Some internal functions (sframe_get_funcdesc_with_addr_internal ()
    finds SFrame FDE by using binary seach comparing
    sfde_func_start_address fields, etc.) need adjustments.
  - sframe_encoder_write () sorts the SFrame FDEs before writing out
    the SFrame data.  Sorting of SFrame FDE via the internal function
    sframe_sort_funcdesc() needs adjustments: the new encoding of
    sfde_func_start_address means the distances are not from the same
    anchor, so cannot be sorted directly.

This patch takes the approach of adding a new internal function:
  - sframe_decoder_get_secrel_func_start_addr (): This function returns
    the offset of the start PC of the function from the start of SFrame
    section, i.e., it gives a section-relative offset.

As the sframe_decoder_get_secrel_func_start_addr () API needs the value
of the function index in the FDE list, another internal API needs
sframe_fre_check_range_p () adjustments too.

Sorting the FDEs (via sframe_sort_funcdesc ()) is done by first bringing
all offsets in sfde_func_start_address relative to start of SFrame
section, followed by sorting, and then readjusting the offsets accroding
to the new position in the FDE list.

libsframe/
	* sframe.c (sframe_decoder_get_secrel_func_start_addr): New
	static function.
        (sframe_fre_check_range_p): Adjust the interface a bit.
	(sframe_get_funcdesc_with_addr_internal): Use
	sframe_decoder_get_secrel_func_start_addr () when comparing
	sfde_func_start_address with user input offset.
        (sframe_find_fre): Adopt the new semantics.
        (sframe_sort_funcdesc): Likewise.

For the libsframe testsuite, use the new encoding for FDE func start
addr: distance between the FDE sfde_func_start_address field and the
start PC of the function itself.

Use SFRAME_F_FDE_FUNC_START_PCREL flag, though the sframe_encode ()
interface in libsframe applies no sanity checks for the encoding itself.

libsframe/testsuite/
	* libsframe.find/findfre-1.c: Adjust to use the new
	SFRAME_F_FDE_FUNC_START_PCREL specific encoding.
	* libsframe.find/findfunc-1.c: Likewise.
	* libsframe.find/plt-findfre-1.c: Likewise.
	* libsframe/testsuite/libsframe.decode/DATA2: Update data file
	due to usage of new SFRAME_F_FDE_FUNC_START_PCREL flag.
	* libsframe/testsuite/libsframe.encode/encode-1.c: Use flag
	SFRAME_F_FDE_FUNC_START_PCREL.
2025-07-06 12:53:03 -07:00
Indu Bhagat
dcb0cf7bb2 include: sframe: doc: define new flag SFRAME_F_FDE_FUNC_START_PCREL
Add a new flag SFRAME_F_FDE_FUNC_START_PCREL to SFrame stack trace
format.  If set, this flag indicates that the function start address
field (sfde_func_start_address) is the offset to the function start
address from the SFrame FDE function start address field itself.

Such an encoding is friendlier to the exisitng PC-REL relocations
available in the ABIs supported in SFrame: AMD64 (R_X86_64_PC32) and
AArch64 (R_AARCH64_PREL32).  In subsequent patches, we will make the
implementation in gas and ld to both:
  - emit the values in the same (above-mentioned) encoding uniformly.
  - set the flag SFRAME_F_FDE_FUNC_START_PCREL in the SFrame header
    for consumers to be able to distinguish.

Define SFRAME_V2_F_ALL_FLAGS in sframe.h to help keep the implementation
less error-prone by keeping a set of all defined flags at a central
place.  Adjust the check in sframe_header_sanity_check_p () to use the
SFRAME_V2_F_ALL_FLAGS instead.

Add documentation for SFRAME_F_FDE_FUNC_START_PCREL.  Update the
documentation about the encoding of the sfde_func_start_address field.

Also, update the section "Changes from Version 1 to Version 2" to
include the specification of the new flag SFRAME_F_FDE_FUNC_START_PCREL
as an erratum to the SFrame Version 2 specification.

include/
        * sframe.h (SFRAME_F_FDE_FUNC_START_PCREL): New definition.
        (SFRAME_V2_F_ALL_FLAGS): Likewise.
libsframe/
	* sframe-dump.c (dump_sframe_header_flags): Update to include
	the new flag SFRAME_F_FDE_FUNC_START_PCREL.
	* sframe.c (sframe_header_sanity_check_p): Use
	SFRAME_V2_F_ALL_FLAGS.
libsframe/doc/
	* sframe-spec.texi: Add details about the new flag.  Also update
	the defails about the sfde_func_start_address encoding.
2025-07-06 12:53:03 -07:00
Indu Bhagat
72dac98050 include: libsframe: add APIs for offsetof FDE func start addr field
These APIs will be later used by the linker to arrange SFrame FDEs in
the output SFrame section.

include/
        * sframe-api.h (sframe_decoder_get_offsetof_fde_start_addr): New
	declaration.
        (sframe_encoder_get_offsetof_fde_start_addr): Likewise.

libsframe/
        * libsframe.ver: List the new APIs.
        * sframe.c (sframe_decoder_get_offsetof_fde_start_addr): New
	definition.
        (sframe_encoder_get_offsetof_fde_start_addr): Likewise.
2025-07-06 12:53:03 -07:00
Indu Bhagat
251c6789b3 include: libsframe: add APIs for SFrame header flags
Add new APIs, one each for getting flags from the SFrame decoder and
SFrame encoder context objects respectively.

These will later be used by the linker to uniformly access the flags,
given the SFrame decoder and SFrame encoder objects.

Use the new API, where applicable, within libsframe.

include/
        * sframe-api.h (sframe_decoder_get_flags): New declaration.
        (sframe_encoder_get_flags): Likewise.
libsframe/
	* libsframe.ver: List new APIs.
        * sframe.c (sframe_decoder_get_flags): New definition.
	(sframe_encoder_get_flags): Likewise.
        (sframe_get_funcdesc_with_addr_internal): Use the new API.
        (sframe_encoder_get_flags): Likewise.
        (sframe_encoder_write_sframe): Likewise.
2025-07-06 12:53:03 -07:00
Indu Bhagat
87f5e2edca libsframe: fix error code in sframe_decode
When sanity check of SFrame header fails, set error code to
SFRAME_ERR_BUF_INVAL instead of the current SFRAME_ERR_NOMEM.
2025-07-05 19:11:15 -07:00
Indu Bhagat
d984b08bb7 sframe: fix PR libsframe/33051
Fix PR libsframe/Bug 33051 - ASAN: heap-buffer-overflow
../../src/libsframe/sframe.c:1054 in
sframe_get_funcdesc_with_addr_internal

The previous commit 9d2a24349e (libsframe: correct binary search for
SFrame FDE) adapted the binary search logic in
sframe_get_funcdesc_with_addr_internal.  Adjusting the upper end of the
search index was missed.

The search must only be done for FDEs starting at index 0 and up until
num_fdes - 1.  Prior logic of searching (before commit 9d2a24349e) was
a bit different.

libsframe/
	* sframe.c: Use the correct high index.
2025-06-03 23:10:46 -07:00
Jens Remus
2adbf167ca libsframe: handle SFrame FRE start/end IP offsets as unsigned
The SFrame FRE start address (fre_start_addr) is defined as unsigned
32-bit integer, as it is an offset from SFrame FDE function start
address (sfde_func_start_address) and functions only grow upwards
(towards higher addresses).

The SFrame FRE start IP offset is a synonym to the SFrame FRE start
address.  The SFrame FRE end IP offset is either the value of the
subsequent FDE start address minus one, if that exists, or the FDE
function size minus one otherwise.  Both should therefore be handled
as unsigned 32-bit integer.

In libsframe the "lookup PC" (pc) and SFrame FDE function start address
(sfde_func_start_address) are both signed integers, as they are actually
offsets from the SFrame section.  The unsigned FDE start/end IP offsets
may therefore only be safely compared against the offset of the lookup
PC from FDE function start address if the FDE function start address is
lower or equal to the lookup PC, as this guarantees the offset to be
always positive:

Given:

  lookup_pc = pc - sframe_addr

  sfde_func_start_address = func_start_addr - sframe_addr

If the FDE function start address is lower or equal than the lookup PC,
which both are signed offsets from SFrame section, then the function
start address is also lower or equal to the PC, which are both unsigned:

  sfde_func_start_address <= lookup_pc
  func_start_addr - sframe_addr <= pc - sframe_addr
  func_start_addr <= pc

With that the offset of the lookup PC from FDE function start address
(lookup_pc - sfde_func_start_address) must always be positive, if
FDE function start address is lower or equal to the lookup PC:

  lookup_pc - sfde_func_start_address
  = pc - sframe_addr - (func_start_addr - sframe_addr)
  = pc - func_start_addr

libsframe/
	* sframe.c (sframe_find_fre): Define and handle start_ip_offset
	and end_ip_offset as unsigned (same as FRE fre_start_addr).
	(sframe_fre_check_range_p): Likewise.  Define PC offset (from
	function start address) as unsigned.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-05-26 11:02:47 -07:00
Jens Remus
ada5c6fa08 libsframe: stop search for SFrame FRE if its start IP is greater than PC
The SFrame FREs for an SFrame FDE are sorted on their start address.
Therefore the linear search for a matching SFrame FRE can be stopped,
if its start address is greater than the searched for PC.

libsframe/
	* sframe.c (sframe_find_fre): Stop search if FRE's start IP is
	greater than PC.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-05-26 11:02:29 -07:00
Jens Remus
9d2a24349e libsframe: correct binary search for SFrame FDE
sframe_get_funcdesc_with_addr_internal erroneously returns the last FDE,
if its function start address is lower than the searched for address.

Simplify the binary search for a SFrame FDE for a given address.  Only
return an FDE, if the searched for address is within the bounds of the
FDE function start address and function size.

libsframe/
	* sframe.c (sframe_get_funcdesc_with_addr_internal): Correct
	binary search for SFrame FDE.

libsframe/testsuite/
	* libsframe.find/plt-findfre-1.c: Add test for out of range
	PLT6.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
2025-05-26 11:01:14 -07:00
Indu Bhagat
4e94f00756 libsframe: fix issue finding FRE in PCMASK type SFrame FDEs
SFrame FDEs of type SFRAME_FDE_TYPE_PCMASK are used for repetitive code
patterns, e.g., pltN entries.  For SFrame FDEs of type
SFRAME_FDE_TYPE_PCMASK, sframe_fre_check_range_p erroneously tested the
given PC instead of the masked PC offset from function start address.
Therefore it only worked correctly by chance, e.g., if the function start
address was aligned on the repetition block size.

For regular SFrame FDEs the PC offset from function start address must
be within a SFrame FRE's start IP offset and end IP offset.  For SFrame
FDEs of type SFRAME_FDE_TYPE_PCMASK, the masked PC offset must be within
that range.

SFrame FRE start/end IP offsets are relative to the SFrame FDE function
start address. For regular SFrame FDEs, the PC offset from function
start address must be within a SFrame FRE's start IP offset and end IP
offset.  For SFRAME_FDE_TYPE_PCMASK type FDEs, the masked PC offset must
be within that range.

Exercise the testcase for a variety of placements; without the fix some
of these tests will fail.  Also, make the testcase itself easier to
follow by adding appropriate vars where applicable.

libsframe/
	* sframe.c (sframe_fre_check_range_p): Fix logic for
	SFRAME_FDE_TYPE_PCMASK type FDE.
libsframe/testsuite/
	* libsframe.find/plt-findfre-1.c: Adjust the test for a variety
	of placements of .sframe and .plt.

Co-Authored-by: Jens Remus <jremus@linux.ibm.com>
2025-05-26 10:54:06 -07:00
Alan Modra
e8e7cf2abe Update year range in copyright notice of binutils files 2025-01-01 18:29:57 +10:30
Alan Modra
fd67aa1129 Update year range in copyright notice of binutils files
Adds two new external authors to etc/update-copyright.py to cover
bfd/ax_tls.m4, and adds gprofng to dirs handled automatically, then
updates copyright messages as follows:

1) Update cgen/utils.scm emitted copyrights.
2) Run "etc/update-copyright.py --this-year" with an extra external
   author I haven't committed, 'Kalray SA.', to cover gas testsuite
   files (which should have their copyright message removed).
3) Build with --enable-maintainer-mode --enable-cgen-maint=yes.
4) Check out */po/*.pot which we don't update frequently.
2024-01-04 22:58:12 +10:30
Indu Bhagat
ce9a87252c sframe: bfd: gas: ld: format bump to SFrame version 2
SFrame version 2 encodes the size of repetitive insn block explicitly
in the format.  Add information in the SFrame FDE to convey the size
of the block of repeating instructions.  This information is used only
for SFrame FDEs of type SFRAME_FDE_TYPE_PCMASK.

Introduce two extra bytes for padding: this ensures that the memory
accesses to the members of the SFrame Frame Descriptor Entry (FDE) are
naturally aligned.

gas generates SFrame section with version SFRAME_VERSION_2 by default.

libsframe provides two new APIs to:
  - get an SFrame FDE data from the decoder context, and
  - add an SFrame FDE to the encoder context.
The additional argument (for rep_block_size) is useful for SFrame FDEs
where FDE type is SFRAME_FDE_TYPE_PCMASK.

The linker will generate the output SFrame sections in the
SFRAME_VERSION_2 format.  If the input sections offered to the linker
are not all in the SFRAME_VERSION_2 format, the linker issues an error
to the user.

objdump/readelf will show the following message to the user if .sframe
section in SFRAME_VERSION_1 format is seen:

 "No further information can be displayed.  SFrame version not
 supported."

In other words, like the rest of the binutils, only the current SFrame
format version, i.e., SFRAME_VERSION_2 is supported by the textual dump
facilities.

bfd/
	* elf-sframe.c (_bfd_elf_merge_section_sframe): Generate an
	output SFrame section with version SFRAME_VERSION_2.  Also,
	error out if the SFrame sections do not all have
	SFRAME_VERSION_2.
	* elfxx-x86.c (_bfd_x86_elf_create_sframe_plt): Generate SFrame
	section for plt entries with version SFRAME_VERSION_2.
gas/
	* gen-sframe.c (sframe_set_version): Update to SFRAME_VERSION_2.
	(output_sframe): Likewise.
gas/testsuite/
	* gas/cfi-sframe/cfi-sframe-aarch64-1.d: Use SFRAME_VERSION_2.
	* gas/cfi-sframe/cfi-sframe-aarch64-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-aarch64-pac-ab-key-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-1.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-2.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-3.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-4.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-5.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-6.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-7.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-common-8.d: Likewise.
	* gas/cfi-sframe/cfi-sframe-x86_64-1.d: Likewise.
	* gas/cfi-sframe/common-empty-1.d: Likewise.
	* gas/cfi-sframe/common-empty-2.d: Likewise.
	* gas/cfi-sframe/common-empty-3.d: Likewise.
ld/testsuite/
	* ld-aarch64/sframe-simple-1.d: Adjust for SFRAME_VERSION_2.
	* ld-x86-64/sframe-plt-1.d: Likewise.
	* ld-x86-64/sframe-simple-1.d: Likewise.
libsframe/
	* libsframe.ver: Add the new APIs.
	* sframe.c (sframe_decoder_get_funcdesc_v2): New definition.
	(sframe_encoder_add_funcdesc_v2): Likewise.
	(sframe_header_sanity_check_p): Include SFRAME_VERSION_2.
	(sframe_fre_check_range_p): Get rep_block_size info from SFrame
	FDE.
	* sframe-dump.c (dump_sframe_header): Add support for
	SFRAME_VERSION_2.
	(dump_sframe): Inform user if SFrame section in SFRAME_VERSION_1
	format is seen.
libsframe/testsuite/
	* libsframe.decode/DATA-BE: Regenerated data file.
	* libsframe.decode/DATA1: Likewise.
	* libsframe.decode/DATA2: Likewise.
	* libsframe.find/plt-findfre-1.c: Use new API in the testcase.
include/
	* sframe.h: Add member to encode size of the code block of
	repeating instructions.  Add 2 bytes of padding.
	* sframe-api.h (sframe_decoder_get_funcdesc_v2): New
	declaration.
	(sframe_encoder_add_funcdesc_v2): Likewise.
2023-06-29 16:31:58 -07:00
Indu Bhagat
9f71b60bd4 libsframe: add new APIs to get SFrame version
While the SFrame preamble is guaranteed to not change between versions,
providing these access APIs from the SFrame decoder and encoder APIs is
for convenience only.  The linker may want to use these APIs as the
format evolves.

include/
	* sframe-api.h (sframe_decoder_get_version): New declaration.
	(sframe_encoder_get_version): Likewise.

libsframe/
	* libsframe/libsframe.ver: Add new APIs.
	* libsframe/sframe.c (sframe_decoder_get_version): New
	definition.
	(sframe_encoder_get_version): Likewise.
2023-06-29 16:28:56 -07:00
Indu Bhagat
3169b734cf libsframe: fix sframe_find_fre for pltN entries
For a toy application on x86_64, for example, following is the SFrame
stack trace information for the 3 pltN entries of 16 bytes each:

   func idx [1]: pc = 0x401030, size = 48 bytes
   STARTPC[m]      CFA       FP        RA
   0000000000000000  sp+8      u         u
   000000000000000b  sp+16     u         u

The data in first column is the start_ip_offset.  Also note that the FDE
is of type SFRAME_FDE_TYPE_PCMASK (denoted by the [m] on LHS).

Where each pltN (note: excluding plt0 entry) entry looks like:

  401030: jmp    *0x2fca(%rip)
  401036: push   $0x0
  40103b: jmp    401020<_init+0x20>

  401040: jmp    *0x2fc2(%rip)
  401046: push   $0x1
  40104b: jmp    401020<_init+0x20>

  401050: jmp    *0x2fba(%rip)
  401056: push   $0x2
  40105b: jmp    401020<_init+0x20>

Now, to find SFrame stack trace information from an FDE of type
SFRAME_FDE_TYPE_PCMASK, sframe_find_fre () was doing an operation
like,
  (start_ip_offset & 0xf) >= (pc & 0xf)

This works for pltN entry of size, say, less than 16 bytes.  But if the
pltN entries or similar code stubs (for which SFrame FDE of type
SFRAME_FDE_TYPE_PCMASK may be used), evolve to be of size > 16 bytes,
this will cease to work.

To match the range covered by the SFrame FRE, one should instead perform
a modulo operation.  The constant for the modulo operation must be the
size of the pltN entry.  Further, this constant should ideally be
encoded in the format, as it may be different for each ABI.

In SFrame Version 2 of the format, we will move towards encoding it
explicitly in the SFrame FDE.  For now, fix up the logic to at least
move towards modulo operation.

libsframe/
	* sframe.c (sframe_fre_check_range_p): New definition.
	(sframe_find_fre): Refactor a bit and use the new definition
	above.
include/
	* sframe.h (SFRAME_FDE_TYPE_PCMASK): Update comment.
libsframe/doc/
	* sframe-spec.texi: Fix the text for SFRAME_FDE_TYPE_PCMASK FDE
	type.
2023-06-29 11:03:32 -07:00
Indu Bhagat
df6f1afb6d libsframe: bfd: use uint32_t for return type of get_num_fidx APIs
Keep the data types usage in libsframe look consistent.

bfd/
	* elf-sframe.c (_bfd_elf_merge_section_sframe): Use uint32_t
	type alias.
	* libsframe/sframe.c (sframe_decoder_get_funcdesc_at_index):
	Likewise.
	(sframe_decoder_get_num_fidx): Likewise.
	(sframe_encoder_get_num_fidx): Likewise.
include/
	* sframe-api.h (sframe_decoder_get_num_fidx): Likewise.
	(sframe_encoder_get_num_fidx): Likewise.
2023-06-27 12:01:56 -07:00
Indu Bhagat
3412dcec61 libsframe: use appropriate data types for args of sframe_encode
include/
	* sframe-api.h (sframe_encode): Use of uint8_t is more
	appropriate.
libsframe/
	* sframe.c (sframe_encode): Likewise.
2023-06-27 12:01:56 -07:00
Indu Bhagat
a9f1da26da libsframe: use uint8_t for return type of sframe_fre_get_base_reg_id
Use a more appropriate data type.

include/
	* sframe-api.h (sframe_fre_get_base_reg_id): Use uint8_t as
	return type.
libsframe/
	* sframe-dump.c (dump_sframe_func_with_fres): Use uint8_t type
	for base reg id.
	* sframe.c (sframe_fre_get_base_reg_id): Use uin8_t as return
	type.
2023-06-27 12:01:56 -07:00
Indu Bhagat
de4879feca libsframe: use uint8_t instead of unsigned char for abi_arch
Use uint8_t consistently for identifying ABI/arch in SFrame format.

bfd/
	* elf-sframe.c (_bfd_elf_merge_section_sframe):
libsframe/
	* sframe-dump.c (is_sframe_abi_arch_aarch64): Use uint8_t for
	local variable.
	* sframe.c (sframe_decoder_get_abi_arch): Update return type to
	uint8_t.
	(sframe_encoder_get_abi_arch): Likewise.
include/
	* sframe-api.h (sframe_decoder_get_abi_arch): Likewise.
	(sframe_encoder_get_abi_arch): Likewise.
2023-06-27 12:01:56 -07:00
Indu Bhagat
49e4485cba libsframe: bfd: use uint32_t for return type of sframe_calc_fre_type
Use uint32_t type alias consistently for all APIs in libsframe.

bfd/
	* elfxx-x86.c (_bfd_x86_elf_create_sframe_plt): Adjust for the
	changed return type.
libsframe/
	* sframe.c (sframe_calc_fre_type): Use uint32_t for return type.
include/
	* sframe-api.h (sframe_calc_fre_type): Likewise.
2023-06-27 12:01:56 -07:00
Indu Bhagat
100d405dae libsframe: use uint32_t for fre_type and fde_type function args
The API sframe_fde_create_func_info is provided by libsframe.  Current
users are the bfd linker.  Adjust the argument type for the variables
carrying the SFrame FRE type and SFrame FDE type to consistenly use
uint32_t type alias.

include/
	* sframe-api.h (sframe_fde_create_func_info): Use uint32_t
	instead of unsigned int.
libsframe/
	* sframe.c (sframe_get_fre_type): Likewise.
	(sframe_get_fde_type): Likewise.
	(flip_fre_start_address): Likewise.
	(sframe_fre_start_addr_size): Likewise.
	(sframe_fre_entry_size): Likewise.
	(flip_fre): Likewise.
	(flip_sframe): Likewise.
	(sframe_fde_create_func_info): Likewise.
	(sframe_calc_fre_type): Likewise.
	(sframe_decode_fre_start_address): Likewise.
	(sframe_decode_fre): Likewise.
	(sframe_find_fre): Likewise.
	(sframe_decoder_get_fre): Likewise.
	(sframe_encoder_add_fre): Likewise.
	(sframe_encoder_write_fre_start_addr): Likewise.
	(sframe_encoder_write_fre): Likewise.
	(sframe_encoder_write_sframe): Likewise.
2023-06-27 12:01:56 -07:00
Indu Bhagat
526960c912 libsframe: update the semantics of sframe_fre_get_fp_offset
Until now, sframe_fre_get_fp_offset () would return
SFRAME_ERR_FREOFFSET_NOPRESENT if the ABI uses fixed FP offset.  A stack
tracer, then, would call an explicit sframe_decoder_get_fixed_fp_offset ()
to get the FP offset.

On second look, it appears to make sense to hide these details of
whether the FP offset is fixed or not in an ABI from the consumer.  Now,
with the changed semantics, the call to sframe_fre_get_fp_offset () will
fetch the fixed FP offset if applicable, or get the FP offset from FRE
when there is no fixed FP offset.

This patch changes the behavior of sframe_fre_get_fp_offset (): it turns
an error into non-error.  This change will be included with the next
release of libsframe, where all the exposed symbols will be versioned
with version node LIBSFRAME_1.0 for the first time.

libsframe/
	* sframe.c (sframe_fre_get_fp_offset): Return the fixed offset, if
	applicable. Else return the FP offset from the FRE.
2023-06-27 12:01:56 -07:00
Indu Bhagat
36aecb4197 libsframe: update the semantics of sframe_fre_get_ra_offset
Until now, sframe_fre_get_ra_offset () would return
SFRAME_ERR_FREOFFSET_NOPRESENT if the ABI uses fixed RA offset (e.g.,
AMD64).  A stack tracer, then, will call an explicit
sframe_decoder_get_fixed_ra_offset () to get the RA offset.

On second look, it appears to make sense to hide these details of
whether the RA offset is fixed or not from the consumer.  Now, with the
changed semantics, the call to sframe_fre_get_ra_offset () will fetch
the fixed RA offset if applicable, or get the RA offset from FRE when
there is no fixed RA offset.

Adjustments need to be made to ensure the textual dump remains the same
as preivous.  Currently, e.g., if RA is not being tracked per FRE,
following is seen with objdump --sframe:

    STARTPC         CFA       FP        RA
    000000000000NNNN  sp+X      u         u

This patch changes the behavior of sframe_fre_get_ra_offset: it turns an
error into non-error.  This change will be included with the next
release of libsframe, where all exposed symbols will be versioned for
the first time.

libsframe/
	* sframe.c (sframe_fre_get_ra_offset): Return the fixed offset,
	if applicable.  Else return the RA offset from the FRE.
	* sframe-dump.c (dump_sframe_func_with_fres): Make adjustments
	to keep the textual dump same as previous.
2023-06-27 12:01:56 -07:00
Indu Bhagat
852bb8c10d libsframe: remove sframe_get_funcdesc_with_addr API
This is an incompatible ABI change in libsframe.

The interface provided by this function is not a healthy abstraction to
expose: the return type sframe_func_desc_entry, which is defined in
include/sframe.h (the SFrame binary format definition).  This ties up
the library in a undesirable way.  Most importantly, this function
should technically not be directly necessary for a stack tracer.  A
stack tracer will likely only need to do a sframe_find_fre ().

Rename the API to continue to use the functionality internally in the
library.  bfd/linker does not use this function.

Change the return type of the previous definition and make a note about
its planned deprecation.

include/
	* sframe-api.h:  Change return type of sframe_get_funcdesc_with_addr.
	Add comment for intention to deprecate.
libsframe/
	*sframe.c (sframe_get_funcdesc_with_addr): Change return type
	and set error code. This API is deprecated.
        (sframe_get_funcdesc_with_addr_internal): New definition for
	internal use.
	(sframe_find_fre): Use sframe_get_funcdesc_with_addr_internal
	instead.
2023-06-27 12:01:56 -07:00
Indu Bhagat
937c461e41 libsframe: fix sframe_find_fre for pltN entries
To find SFrame stack trace information from an FDE of type
SFRAME_FDE_TYPE_PCMASK, sframe_find_fre () was doing an operation
like,
  (start_ip_offset & 0xff) >= (pc & 0xff), etc.

This is buggy and needs correction.  The mask 0xff should be 0xf (to
work for a pltN entry of size say, 16 bytes).

At this time, the size of the pltN entry is implicitly assumed to be 16
bytes by libsframe.  In next version of the SFrame format, we can encode
this information explicitly in the SFrame FDE.

For now, we should fix the code to at least behave correctly for the
generated code and the generated SFrame stack trace information for the
pltN entries on x86_64.

libsframe/
	* sframe.c (sframe_find_fre): Correct the bitmask used for
	SFrame FDEs of type SFRAME_FDE_TYPE_PCMASK.
2023-06-09 11:14:05 -07:00
Indu Bhagat
676cb9d2e0 libsframe: reuse static function sframe_decoder_get_funcdesc_at_index
sframe_decoder_get_funcdesc_at_index () is the function to access SFrame
FDEs in the SFrame decoder context.  Use it consistently.

Avoid unnecessary type cast and include minor enhancements as the code
is moved around.

libsframe/
	* sframe.c (sframe_decoder_get_funcdesc_at_index): Move some
	checks here.  Move the static function definition before the new
	use.
	(sframe_decoder_get_funcdesc): Use
	sframe_decoder_get_funcdesc_at_index instead.
2023-06-07 15:14:39 -07:00
Indu Bhagat
f4af42724b libsframe: fix cosmetic issues and typos
include/
	* sframe-api.h (sframe_decoder_get_num_fidx): Use extern.
libsframe/
	* sframe-dump.c (dump_sframe_func_with_fres): Fix line length.
	* sframe.c (sframe_frame_row_entry_copy): Likewise.
	(sframe_decode_fre_start_address): Use the intended type uint32_t.
2023-06-06 10:50:07 -07:00
Indu Bhagat
1466e49f7d libsframe: avoid unnecessary type casts
Change the data type of some of the members of the sframe_decoder_ctx
and sframe_encoder_ctx data structures to use the applicable data types
explicitly. Current implementation in libsframe does type casts, which
seem unnecessary.

libsframe/
	* libsframe/sframe-impl.h (struct sframe_decoder_ctx): Use
	applicable data type explicitly.
	(struct sframe_encoder_ctx): Likewise. Use same style of
	comments consistently.
	* libsframe/sframe.c (struct sf_fde_tbl): Define without
	typedef.
	(struct sf_fre_tbl): Likewise.
	(sframe_decode): Remove unnecessary type casts.
	(sframe_encoder_get_funcdesc_at_index): Likewise.
	(sframe_encoder_add_fre): Likewise.
	(sframe_encoder_add_funcdesc): Likewise.
	(sframe_sort_funcdesc): Likewise.
	(sframe_encoder_write_sframe): Likewise.
2023-06-05 14:17:15 -07:00