Files
binutils-gdb/include/sframe-api.h
Indu Bhagat 982b494403 [SFrame-V3] include: libsframe: bfd: gas: testsuite: support for signed 64-bit offset in SFrame FDE
This change enables support text > 2 GiB in SFrame format.

Each SFrame FDE needs to hold information about the start PC of the
function it pertains to.  Currently, the field 'sfde_func_start_address'
in SFrame FDE is encoded as a 32-bit offset to the start PC of the
function from the field itself.

In SFrame V2, this offset was a signed 32-bit offset.  The signedness
gives the flexibility of having .sframe ELF section before or after the
.text* sections.  But the size of 32-bit puts the limitation that
.sframe togther with the .text* sections must fit the 2 GiB range.

Currently, if the linker sees the offset not representable as 32-bit
signed offset, it issues an error (not seen in the wild, simulated by
forcing a function to align via an '.align  2147483648' directive):

  test.o:(.sframe+0x1c): relocation truncated to fit: R_X86_64_PC32 against `.text'
  make: *** [Makefile:7: all] Error 1

ATM, EH Frame also suffers with the same issue.

Moving forward, some cloud applications have been shown to be nearing
1.5 GiB threshold.  Extending the offset to int64_t now seems to be good
choice to make now for future-proof'ing the sections.

The use of int64_t offset is done for all SFrame V3 sections.  This
bump from int32_t to int64_t should not lead to an increase in the size
of SFrame sections, because of the following additional changes to the
SFrame FDE specification:
  - Reduce the type of sfde_func_num_fres (from uint32_t to uint16_t)
  - Remove the 2 bytes of padding (sfde_func_padding2). These served the
    two-fold purpose of keeping FDE data aligned _and_ unused space for
    future needs.

Now that the offset is int64_t, start using the
sframe_decoder_get_funcdesc_v3 () instead of
sframe_decoder_get_funcdesc_v2 () in GNU ld.

This patch changes the offset type in the SFrame FDE definition to an
int64_t.  No further changes in gas are necessary because the code
already accounts for writing out as per the size of the member of the
struct:

  emit_expr (&exp, sizeof_member (sframe_func_desc_entry,
                                  sfde_func_start_offset));

bfd/
	* elf-sframe.c (sframe_read_value): Signed offset for start PC
	is 8-bytes now.
	(_bfd_elf_merge_section_sframe): Likewise.
	* elf64-s390.c (_bfd_s390_elf_create_sframe_plt): Use V3 API.
	(elf_s390_finish_dynamic_sections): Signed offset for start PC
	is 8-bytes now.
	* elfxx-x86.c (_bfd_x86_elf_create_sframe_plt): Use V3 API.
	(_bfd_x86_elf_finish_dynamic_sections): Signed offset for start
	PC is 8-bytes now.
gas/
	* sframe.c (output_sframe_funcdesc): Rename to
	sfde_func_start_offset.
libsframe/
	* libsframe/sframe.c (sframe_fde_tbl_init): Rename to
	sfde_func_start_offset.
	(flip_fde): Likewise.
	(sframe_decoder_get_secrel_func_start_addr): Use int64_t.
	(sframe_fre_check_range_p): Likewise.
	(sframe_decoder_get_offsetof_fde_start_addr): Rename to
	sfde_func_start_offset.
	(sframe_get_funcdesc_with_addr_internal): Use int64_t.
	(sframe_find_fre): Likewise.
	(sframe_encoder_get_offsetof_fde_start_addr): Rename to
	sfde_func_start_offset.
	(sframe_encoder_add_funcdesc_internal): Use int64_t.
	(sframe_encoder_add_funcdesc): Likewise.  And rename to
	sfde_func_start_offset.
	(sframe_encoder_write_fde): Rename to sfde_func_start_offset.
libsframe/testsuite/
	* libsframe.decode/DATA2: Regenerate the data file.
	* libsframe.encode/encode-1.c: Use int64_t for start pc offset.
	* libsframe.find/findfre-1.c: Likewise.
	* libsframe.find/findfunc-1.c: Likewise.
	* libsframe.find/plt-findfre-1.c: Likewise.
	* libsframe.find/plt-findfre-2.c: Likewise.
include/
	* sframe-api.h (sframe_find_fre): Update arg type to int64_t.
	(sframe_encoder_add_funcdesc): Likewise.
	* sframe.h: Change data type to int64_t.
2026-01-15 17:02:24 -08:00

333 lines
12 KiB
C

/* Public API to SFrame.
Copyright (C) 2022-2026 Free Software Foundation, Inc.
This file is part of libsframe.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _SFRAME_API_H
#define _SFRAME_API_H
#include <sframe.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct sframe_decoder_ctx sframe_decoder_ctx;
typedef struct sframe_encoder_ctx sframe_encoder_ctx;
#define MAX_NUM_STACK_OFFSETS 3
#define MAX_OFFSET_BYTES \
((SFRAME_FRE_OFFSET_4B * 2 * MAX_NUM_STACK_OFFSETS))
/* User interfacing SFrame Row Entry.
An abstraction provided by libsframe so the consumer is decoupled from
the binary format representation of the same.
The members are best ordered such that they are aligned at their natural
boundaries. This helps avoid usage of undesirable misaligned memory
accesses. See PR libsframe/29856. */
typedef struct sframe_frame_row_entry
{
uint32_t fre_start_addr;
unsigned char fre_offsets[MAX_OFFSET_BYTES];
unsigned char fre_info;
} sframe_frame_row_entry;
#define SFRAME_ERR ((int) -1)
/* This macro holds information about all the available SFrame
errors. It is used to form both an enum holding all the error
constants, and also the error strings themselves. To use, define
_SFRAME_FIRST and _SFRAME_ITEM to expand as you like, then
mention the macro name. See the enum after this for an example. */
#define _SFRAME_ERRORS \
_SFRAME_FIRST (SFRAME_ERR_VERSION_INVAL, "SFrame version not supported.") \
_SFRAME_ITEM (SFRAME_ERR_NOMEM, "Out of Memory.") \
_SFRAME_ITEM (SFRAME_ERR_INVAL, "Corrupt SFrame.") \
_SFRAME_ITEM (SFRAME_ERR_BUF_INVAL, "Buffer does not contain SFrame data.") \
_SFRAME_ITEM (SFRAME_ERR_DCTX_INVAL, "Corrupt SFrame decoder.") \
_SFRAME_ITEM (SFRAME_ERR_ECTX_INVAL, "Corrupt SFrame encoder.") \
_SFRAME_ITEM (SFRAME_ERR_FDE_INVAL, "Corrput FDE.") \
_SFRAME_ITEM (SFRAME_ERR_FRE_INVAL, "Corrupt FRE.") \
_SFRAME_ITEM (SFRAME_ERR_FDE_NOTFOUND,"FDE not found.") \
_SFRAME_ITEM (SFRAME_ERR_FDE_NOTSORTED, "FDEs not sorted.") \
_SFRAME_ITEM (SFRAME_ERR_FRE_NOTFOUND,"FRE not found.") \
_SFRAME_ITEM (SFRAME_ERR_FREOFFSET_NOPRESENT,"FRE offset not present.")
#define SFRAME_ERR_BASE 2000 /* Base value for libsframe errnos. */
enum
{
#define _SFRAME_FIRST(NAME, STR) NAME = SFRAME_ERR_BASE
#define _SFRAME_ITEM(NAME, STR) , NAME
_SFRAME_ERRORS
#undef _SFRAME_ITEM
#undef _SFRAME_FIRST
};
/* Count of SFrame errors. */
#define SFRAME_ERR_NERR (SFRAME_ERR_FREOFFSET_NOPRESENT - SFRAME_ERR_BASE + 1)
/* Get the error message string. */
extern const char *
sframe_errmsg (int error);
/* Create an FDE function info bye given an FRE_TYPE and an FDE_TYPE. */
extern unsigned char
sframe_fde_create_func_info (uint32_t fre_type, uint32_t fde_type);
/* Gather the FRE type given the function size. */
extern uint32_t
sframe_calc_fre_type (size_t func_size);
/* The SFrame Decoder. */
/* Decode the specified SFrame buffer SF_BUF of size SF_SIZE and return the
new SFrame decoder context. Sets ERRP for the caller if any error. */
extern sframe_decoder_ctx *
sframe_decode (const char *sf_buf, size_t sf_size, int *errp);
/* Free the decoder context. */
extern void
sframe_decoder_free (sframe_decoder_ctx **dctx);
/* Get the size of the SFrame header from the decoder context DCTX. */
extern unsigned int
sframe_decoder_get_hdr_size (const sframe_decoder_ctx *dctx);
/* Get the SFrame's abi/arch info. */
extern uint8_t
sframe_decoder_get_abi_arch (const sframe_decoder_ctx *dctx);
/* Get the format version from the SFrame decoder context DCTX. */
extern uint8_t
sframe_decoder_get_version (const sframe_decoder_ctx *dctx);
/* Get the section flags from the SFrame decoder context DCTX. */
extern uint8_t
sframe_decoder_get_flags (const sframe_decoder_ctx *dctx);
/* Get the offset of the sfde_func_start_address field (from the start of the
on-disk layout of the SFrame section) of the FDE at FUNC_IDX in the decoder
context DCTX.
If FUNC_IDX is more than the number of SFrame FDEs in the section, sets
error code in ERRP, but returns the (hypothetical) offset. This is useful
for the linker when arranging input FDEs into the output section to be
emitted. */
uint32_t
sframe_decoder_get_offsetof_fde_start_addr (const sframe_decoder_ctx *dctx,
uint32_t func_idx, int *errp);
/* Return the number of function descriptor entries in the SFrame decoder
DCTX. */
extern uint32_t
sframe_decoder_get_num_fidx (const sframe_decoder_ctx *dctx);
/* Get the fixed FP offset from the decoder context DCTX. */
extern int8_t
sframe_decoder_get_fixed_fp_offset (const sframe_decoder_ctx *dctx);
/* Get the fixed RA offset from the decoder context DCTX. */
extern int8_t
sframe_decoder_get_fixed_ra_offset (const sframe_decoder_ctx *dctx);
/* Find the SFrame Frame Row Entry which contains the PC. Returns
SFRAME_ERR if failure. */
extern int
sframe_find_fre (const sframe_decoder_ctx *ctx, int64_t pc,
sframe_frame_row_entry *frep);
/* Get the FRE_IDX'th FRE of the function at FUNC_IDX'th function
index entry in the SFrame decoder CTX. Returns error code as
applicable. */
extern int
sframe_decoder_get_fre (const sframe_decoder_ctx *ctx,
unsigned int func_idx,
unsigned int fre_idx,
sframe_frame_row_entry *fre);
/* Get the data (NUM_FRES, FUNC_SIZE, FUNC_START_ADDRESS, FUNC_INFO,
REP_BLOCK_SIZE) from the function descriptor entry at index I'th
in the decoder CTX. If failed, return error code.
This API is only available from SFRAME_VERSION_2. */
extern int
sframe_decoder_get_funcdesc_v2 (const sframe_decoder_ctx *ctx,
unsigned int i,
uint32_t *num_fres,
uint32_t *func_size,
int32_t *func_start_address,
unsigned char *func_info,
uint8_t *rep_block_size);
/* Get the data (NUM_FRES, FUNC_SIZE, START_PC_OFFSET, FUNC_INFO,
REP_BLOCK_SIZE) from the SFrame function descriptor entry at the I'th index
in the decoder object DCTX. Return SFRAME_ERR on failure. */
extern int
sframe_decoder_get_funcdesc_v3 (const sframe_decoder_ctx *dctx,
unsigned int i,
uint32_t *num_fres,
uint32_t *func_size,
int64_t *start_pc_offset,
unsigned char *func_info,
uint8_t *rep_block_size);
/* SFrame textual dump. */
extern void
dump_sframe (const sframe_decoder_ctx *decoder, uint64_t addr);
/* Get the base reg id from the FRE info. Sets errp if fails. */
extern uint8_t
sframe_fre_get_base_reg_id (const sframe_frame_row_entry *fre, int *errp);
/* Get the CFA offset from the FRE. If the offset is invalid, sets errp. */
extern int32_t
sframe_fre_get_cfa_offset (const sframe_decoder_ctx *dtcx,
const sframe_frame_row_entry *fre, int *errp);
/* Get the FP offset from the FRE. If the offset is invalid, sets errp.
For s390x the offset may be an encoded register number, indicated by
LSB set to one, which is only valid in the topmost frame. */
extern int32_t
sframe_fre_get_fp_offset (const sframe_decoder_ctx *dctx,
const sframe_frame_row_entry *fre, int *errp);
/* Get the RA offset from the FRE. If the offset is invalid, sets errp.
For s390x an RA offset value of SFRAME_FRE_RA_OFFSET_INVALID indicates
that the RA is not saved, which is only valid in the topmost frame.
For s390x the offset may be an encoded register number, indicated by
LSB set to one, which is only valid in the topmost frame. */
extern int32_t
sframe_fre_get_ra_offset (const sframe_decoder_ctx *dctx,
const sframe_frame_row_entry *fre, int *errp);
/* Get whether the RA is mangled. */
extern bool
sframe_fre_get_ra_mangled_p (const sframe_decoder_ctx *dctx,
const sframe_frame_row_entry *fre, int *errp);
/* Get whether the RA is undefined (i.e. outermost frame). */
bool
sframe_fre_get_ra_undefined_p (const sframe_decoder_ctx *dctx ATTRIBUTE_UNUSED,
const sframe_frame_row_entry *fre, int *errp);
/* The SFrame Encoder. */
/* Create an encoder context with the given SFrame format version VER, FLAGS
and ABI information. Sets errp if failure. */
extern sframe_encoder_ctx *
sframe_encode (uint8_t ver, uint8_t flags, uint8_t abi_arch,
int8_t fixed_fp_offset, int8_t fixed_ra_offset, int *errp);
/* Free the encoder context ECTXP. */
extern void
sframe_encoder_free (sframe_encoder_ctx **ectxp);
/* Get the size of the SFrame header from the encoder context ECTX. */
extern unsigned int
sframe_encoder_get_hdr_size (sframe_encoder_ctx *ectx);
/* Get the SFrame abi/arch info from the encoder context ECTX. */
extern uint8_t
sframe_encoder_get_abi_arch (sframe_encoder_ctx *ectx);
/* Get the SFrame format version from the encoder context ECTX. */
extern uint8_t
sframe_encoder_get_version (sframe_encoder_ctx *ectx);
/* Get the SFrame flags from the encoder context ECTX. */
extern uint8_t
sframe_encoder_get_flags (sframe_encoder_ctx *ectx);
/* Get the offset of the sfde_func_start_address field (from the start of the
on-disk layout of the SFrame section) of the FDE at FUNC_IDX in the encoder
context ECTX.
If FUNC_IDX is more than the number of SFrame FDEs in the section, sets
error code in ERRP, but returns the (hypothetical) offset. This is useful
for the linker when arranging input FDEs into the output section to be
emitted. */
uint32_t
sframe_encoder_get_offsetof_fde_start_addr (sframe_encoder_ctx *ectx,
uint32_t func_idx, int *errp);
/* Return the number of SFrame function descriptor entries in the encoder
context ECTX. */
extern uint32_t
sframe_encoder_get_num_fidx (sframe_encoder_ctx *ectx);
/* Add an SFrame FRE to function at FUNC_IDX'th function descriptor entry in
the encoder context ECTX. */
extern int
sframe_encoder_add_fre (sframe_encoder_ctx *ectx,
unsigned int func_idx,
sframe_frame_row_entry *frep);
/* Add a new SFrame function descriptor entry with START_ADDR and FUNC_SIZE to
the encoder context ECTX. */
extern int
sframe_encoder_add_funcdesc (sframe_encoder_ctx *ectx,
int64_t start_addr,
uint32_t func_size);
/* Add a new SFrame function descriptor entry with START_ADDR, FUNC_SIZE,
FUNC_INFO and REP_BLOCK_SIZE to the encoder context ECTX. This API is valid
only for SFrame format version 2. */
extern int
sframe_encoder_add_funcdesc_v2 (sframe_encoder_ctx *ectx,
int32_t start_addr,
uint32_t func_size,
unsigned char func_info,
uint8_t rep_block_size,
uint32_t num_fres);
/* Add a new SFrame function descriptor entry with START_PC_OFFSET, FUNC_SIZE,
FUNC_INFO and REP_BLOCK_SIZE to the encoder context ECTX. Return error
code on failure. */
extern int
sframe_encoder_add_funcdesc_v3 (sframe_encoder_ctx *ectx,
int64_t start_pc_offset,
uint32_t func_size,
unsigned char func_info,
uint8_t rep_block_size,
uint32_t num_fres);
/* Serialize the contents of the encoder context ECTX and return the buffer.
Sort the SFrame FDEs on start PC if SORT_FDE_P is true. ENCODED_SIZE is
updated to the size of the buffer. Sets ERRP if failure. */
extern char *
sframe_encoder_write (sframe_encoder_ctx *ectx, size_t *encoded_size,
bool sort_fde_p, int *errp);
#ifdef __cplusplus
}
#endif
#endif /* _SFRAME_API_H */