Support in-memory return values for s390
cover Support in-memory return values for s390
The s390 ABI returns any aggregate and scalars larger than 8 bytes, such as __int128, in a return value buffer allocated by the caller. This buffer's address is treated like a "hidden argument" and passed by the caller in %r2. The function's first parameter is passed in %r3.
Without this change pahole skips such functions and with "--verbose" prints a message like this:
<function> : skipping BTF encoding of function due to unexpected register usage for parameter
Set the first parameter register index (reg_idx) to the proper value. Move function's return type resolution before parameters are analyzed, because the analysis depends on the return type.
Although param_loc_supported is not set for s390, calculate the reg_idx in function__match_clang_parameter_locations in case the flag is enabled in the future.
Add test for in-memory return values.
The change affects only s390.
Maxim Khmelevskii (2):
dwarf_loader: Support in-memory return values for s390
tests: Add test for in-memory return values
dwarf_loader.c | 84 +++++++++++++++++++++++------- dwarves.h | 1 + tests/ret_in_memory.sh | 113 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 18 deletions(-) create mode 100755 tests/ret_in_memory.sh
Signature · 1 line
2.56.0
PATCH 1/2 dwarf_loader: Support in-memory return values for s390
The s390 ABI returns any aggregate and scalars larger than 8 bytes, such as __int128, in a return value buffer allocated by the caller. This buffer's address is treated like a "hidden argument" and passed by the caller in %r2. The function's first parameter is passed in %r3.
Without this change pahole skips such functions and with "--verbose" prints a message like this:
<function> : skipping BTF encoding of function due to unexpected register usage for parameter
Set the first parameter register index (reg_idx) to the proper value. Move function's return type resolution before parameters are analyzed, because the analysis depends on the return type.
Although param_loc_supported is not set for s390, calculate the reg_idx in function__match_clang_parameter_locations in case the flag is enabled in the future.
The change affects only s390.
--- dwarf_loader.c | 84 +++++++++++++++++++++++++++++++++++++++----------- dwarves.h | 1 + 2 files changed, 67 insertions(+), 18 deletions(-)
dwarf_loader.c +66 -18
@@ -4044,6 +4044,51 @@ static bool parameter__apply_true_sig_member(struct parameter *parm, struct cu *return true; } +static bool tag__is_aggregate (const struct tag *type) +{ +switch (type->tag) { +case DW_TAG_structure_type: +case DW_TAG_class_type: +case DW_TAG_union_type: +case DW_TAG_array_type: +return true; +default : +return false; + } +} + +static bool s390x__ret_in_memory (const struct tag *type,const struct cu *cu) +{ +if (tag__is_aggregate (type)) +return true; + +return tag__size (type, cu) >8 ; +} + +static bool ftype__ret_in_memory (const struct ftype *ftype,const struct cu *cu) +{ +switch (cu->e_machine) { +case EM_S390 : { +struct tag *type; + +if (ftype->tag.type ==0 ) +return false; + + type =tag__strip_typedefs_and_modifiers (&ftype->tag, cu); +if (type ==NULL ) +return false; +return s390x__ret_in_memory (type, cu); + }break ; +default : +return false; + } +} + +static int ftype__first_param_reg_idx (const struct ftype *ftype,const struct cu *cu) +{ +return ftype__ret_in_memory (ftype, cu) ?1 :0 ; +} +static bool parameter__reg_in_expected_window (const struct parameter *parm,int reg_idx,int slots,const struct cu *cu) {
@@ -4060,8 +4105,8 @@ static bool parameter__reg_in_expected_window(const struct parameter *parm, intstatic void function__match_clang_parameter_locations (struct ftype *ftype,struct cu *cu) { +int reg_idx =ftype__first_param_reg_idx (ftype, cu);struct parameter *pos; -int reg_idx =0 ;ftype__for_each_parameter (ftype, pos) {int slots =parameter__abi_slots (pos, cu);
@@ -4089,7 +4134,7 @@ static void function__analyze_parameter_locations(struct function *fn, struct cubool true_sig_enabled = cu->param_loc_supported && conf->true_signature && ftype->signature_changed;bool check_locations = !cu->producer_clang || ftype->signature_changed; -int reg_idx =0 ; +int reg_idx =ftype__first_param_reg_idx (ftype, cu);if (!check_locations) {/* Producer is clang and the signature was not changed: match
@@ -4448,6 +4493,24 @@ static int cu__resolve_func_ret_types_optimized(struct cu *cu, struct conf_loadcontinue ; fn =tag__function (tag); + +/* Set the function return type, it affects the register index associated +* with the first parameter of the function. If the return value is passed +* in memory the index is shifted. +*/ +if (tag->type ==0 && fn->abstract_origin) { +struct dwarf_tag *dtag =tag__dwarf (tag); +struct dwarf_tag *dfunc; + + dfunc =dwarf_cu__find_tag_by_ref (cu->priv, dtag, abstract_origin); +if (dfunc ==NULL ) { +tag__print_abstract_origin_not_found (tag); +return -1 ; + } + + tag->type =dtag__tag (dfunc)->type; + } +/* Abstract inline subprograms describe source signatures; their * concrete locations belong to DW_TAG_inlined_subroutine children. */
@@ -4475,22 +4538,6 @@ static int cu__resolve_func_ret_types_optimized(struct cu *cu, struct conf_load fn->proto.unexpected_reg =1 ; } } - -if (tag ==NULL || tag->type !=0 ) -continue ; - -if (!fn->abstract_origin) -continue ; - -struct dwarf_tag *dtag =tag__dwarf (tag); -struct dwarf_tag *dfunc; - dfunc =dwarf_cu__find_tag_by_ref (cu->priv, dtag, abstract_origin); -if (dfunc ==NULL ) { -tag__print_abstract_origin_not_found (tag); -return -1 ; - } - - tag->type =dtag__tag (dfunc)->type; }return 0 ; }
@@ -4805,6 +4852,7 @@ static int cu__set_common(struct cu *cu, struct conf_load *conf, cu->agg_use_two_regs =arch__agg_use_two_regs (&ehdr); cu->arg_align_two_regs =arch__arg_align_two_regs (&ehdr); cu->param_loc_supported =arch__param_loc_supported (&ehdr); + cu->e_machine = ehdr.e_machine;arch__set_register_params (&ehdr, cu);return 0 ; }
dwarves.h +1 -0
@@ -333,6 +333,7 @@ struct cu { uint8_t arg_align_two_regs:1; /* An over-aligned arg starts on an even register */ uint8_t param_loc_supported:1; /* Parameter location analysis is validated for this arch */uint8_t nr_register_params; +uint16_t e_machine;int register_params[ARCH_MAX_REGISTER_PARAMS ];int functions_saved;uint16_t language;
Signature · 1 line
2.56.0
PATCH 2/2 tests: Add test for in-memory return values
In reply to Maxim KhmelevskiiCheck that functions returning a value passed in memory have the same signature in BTF and DWARF.
--- tests/ret_in_memory.sh | 113 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100755 tests/ret_in_memory.sh
tests/ret_in_memory.sh new file +113 -0
@@ -0,0 +1,113 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +source "$(dirname "$0")/test_lib.sh" + +outdir=$(make_tmpdir) + +# Comment this out to save test data. +trap cleanup EXIT + +title_log "BTF encoding: functions returning values passed in memory" + +supported_arches=( + x86_64 + aarch64 + s390x +) + +CC=${CC:-gcc} +if ! command -v "${CC%% *}" > /dev/null 2>&1; then + info_log "skip: $CC not available" + test_skip +fi + +arch=$(uname -m) +if [[ " ${supported_arches[*]} " != *" $arch "* ]]; then + info_log "skip: test does not support $arch" + test_skip +fi + +run_cmd() +{ + verbose_log "$1" >&2 + eval "$1" +} + +src="${outdir}/ret_in_memory" +obj="${src}.o" +btf="${src}.btf" + +cat > ${src}.c << EOF2 +typedef long long i64; +struct s128 { i64 a, b; }; + +__attribute__((noinline)) __int128 ret_i128(i64 x, i64 y) +{ + return (__int128)x * y; +} + +__attribute__((noinline)) struct s128 ret_s128(i64 x, i64 y) +{ + struct s128 r = { x * y, x - y }; + return r; +} + +/* Check if DWARF abstract function works correctly */ +static struct s128 ret_origin(i64 x, i64 y) +{ + struct s128 r = { x, y }; + return r; +} +struct s128 (*fp)(i64, i64) = ret_origin; + +__int128 gi; +struct s128 gs; +i64 xx, yy; + +void entry(void) +{ + gi = ret_i128(xx, yy); + gs = ret_s128(xx, yy); + gs = ret_origin(xx, yy); +} +EOF2 + +if ! $CC -g -O2 -c -o $obj ${src}.c; then + error_log "$CC: could not compile ${src}.c" + test_fail +fi + +info_log "Generate $(basename $btf)" +if ! run_cmd "pahole -J --btf_encode_detached=$btf $obj"; then + error_log "could not encode BTF for $obj" + test_fail +fi + +expected_functions=( + ret_i128 + ret_s128 + ret_origin +) + +info_log "Check that each function in DWARF has a match in BTF" +for fn in "${expected_functions[@]}"; do + # DWARF lists ret_origin() twice: the abstract and the concrete instance, + # because of that sort is needed. + dwarf_sig=$(run_cmd "pfunct -P $obj | grep ' ${fn}(' | sort -u") + btf_sig=$(run_cmd "pfunct -P --format_path=btf $btf | grep ' ${fn}('") + if [[ -z "$dwarf_sig" ]]; then + error_log "${fn}() missing from DWARF" + test_fail + fi + if [[ -z "$btf_sig" ]]; then + error_log "${fn}() missing from BTF" + test_fail + fi + if [[ "$btf_sig" != "$dwarf_sig" ]]; then + error_log "${fn}() BTF signature '$btf_sig' differs from DWARF '$dwarf_sig'" + test_fail + fi +done + +test_pass
Signature · 1 line
2.56.0