| 1 |
/* sframe-dump.c - Textual dump of .sframe. |
| 2 |
|
| 3 |
Copyright (C) 2022-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of libsframe. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include <string.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <inttypes.h> |
| 24 |
#include "sframe-impl.h" |
| 25 |
|
| 26 |
/* Return TRUE if the SFrame section is associated with the aarch64 ABIs. */ |
| 27 |
|
| 28 |
static bool |
| 29 |
is_sframe_abi_arch_aarch64 (sframe_decoder_ctx *sfd_ctx) |
| 30 |
{ |
| 31 |
bool aarch64_p = false; |
| 32 |
|
| 33 |
uint8_t abi_arch = sframe_decoder_get_abi_arch (sfd_ctx); |
| 34 |
if (abi_arch == SFRAME_ABI_AARCH64_ENDIAN_BIG |
| 35 |
|| abi_arch == SFRAME_ABI_AARCH64_ENDIAN_LITTLE) |
| 36 |
aarch64_p = true; |
| 37 |
|
| 38 |
return aarch64_p; |
| 39 |
} |
| 40 |
|
| 41 |
/* Return TRUE if the SFrame section is associated with the s390x ABI. */ |
| 42 |
|
| 43 |
static bool |
| 44 |
is_sframe_abi_arch_s390x (sframe_decoder_ctx *sfd_ctx) |
| 45 |
{ |
| 46 |
return sframe_decoder_get_abi_arch (sfd_ctx) == SFRAME_ABI_S390X_ENDIAN_BIG; |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
dump_sframe_header_flags (sframe_decoder_ctx *sfd_ctx) |
| 51 |
{ |
| 52 |
uint8_t flags; |
| 53 |
const char *prefix = "Flags: "; |
| 54 |
|
| 55 |
flags = sframe_decoder_get_flags (sfd_ctx); |
| 56 |
if (!flags) |
| 57 |
{ |
| 58 |
printf ("%11sNONE\n", prefix); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
#define PRINT_FLAG(x) \ |
| 63 |
if (flags & (x)) \ |
| 64 |
{ flags = (flags & ~(x)); \ |
| 65 |
printf ("%11s%s%s\n", prefix, #x, flags ? "," : ""); \ |
| 66 |
prefix = " "; \ |
| 67 |
} |
| 68 |
|
| 69 |
PRINT_FLAG (SFRAME_F_FDE_SORTED); |
| 70 |
PRINT_FLAG (SFRAME_F_FRAME_POINTER); |
| 71 |
PRINT_FLAG (SFRAME_F_FDE_FUNC_START_PCREL); |
| 72 |
#undef PRINT_FLAG |
| 73 |
|
| 74 |
/* Print any residual flags, should this implementation be out of sync when |
| 75 |
new flags are added. */ |
| 76 |
if (flags) |
| 77 |
printf ("%11s%d\n", prefix, flags); |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
dump_sframe_header (sframe_decoder_ctx *sfd_ctx) |
| 82 |
{ |
| 83 |
uint8_t ver; |
| 84 |
const char *ver_str = NULL; |
| 85 |
int8_t cfa_fixed_fp_offset; |
| 86 |
int8_t cfa_fixed_ra_offset; |
| 87 |
const sframe_header *header = &(sfd_ctx->sfd_header); |
| 88 |
|
| 89 |
/* Prepare SFrame section version string. */ |
| 90 |
const char *version_names[] |
| 91 |
= { "NULL", |
| 92 |
"SFRAME_VERSION_1", |
| 93 |
"SFRAME_VERSION_2" }; |
| 94 |
|
| 95 |
ver = sframe_decoder_get_version (sfd_ctx); |
| 96 |
if (ver <= SFRAME_VERSION) |
| 97 |
ver_str = version_names[ver]; |
| 98 |
|
| 99 |
/* CFA fixed FP and RA offsets. */ |
| 100 |
cfa_fixed_fp_offset = header->sfh_cfa_fixed_fp_offset; |
| 101 |
cfa_fixed_ra_offset = header->sfh_cfa_fixed_ra_offset; |
| 102 |
|
| 103 |
const char* subsec_name = "Header"; |
| 104 |
printf ("\n"); |
| 105 |
printf (" %s :\n", subsec_name); |
| 106 |
printf ("\n"); |
| 107 |
printf (" Version: %s\n", ver_str); |
| 108 |
|
| 109 |
dump_sframe_header_flags (sfd_ctx); |
| 110 |
|
| 111 |
if (cfa_fixed_fp_offset != SFRAME_CFA_FIXED_FP_INVALID) |
| 112 |
printf (" CFA fixed FP offset: %d\n", cfa_fixed_fp_offset); |
| 113 |
if (cfa_fixed_ra_offset != SFRAME_CFA_FIXED_RA_INVALID) |
| 114 |
printf (" CFA fixed RA offset: %d\n", cfa_fixed_ra_offset); |
| 115 |
printf (" Num FDEs: %d\n", sframe_decoder_get_num_fidx (sfd_ctx)); |
| 116 |
printf (" Num FREs: %d\n", header->sfh_num_fres); |
| 117 |
} |
| 118 |
|
| 119 |
static void |
| 120 |
dump_sframe_func_with_fres (sframe_decoder_ctx *sfd_ctx, |
| 121 |
unsigned int funcidx, |
| 122 |
uint64_t sec_addr) |
| 123 |
{ |
| 124 |
uint32_t j = 0; |
| 125 |
uint32_t num_fres = 0; |
| 126 |
uint32_t func_size = 0; |
| 127 |
int32_t func_start_address = 0; |
| 128 |
unsigned char func_info = 0; |
| 129 |
|
| 130 |
uint64_t func_start_pc_vma = 0; |
| 131 |
uint64_t fre_start_pc_vma = 0; |
| 132 |
const char *base_reg_str[] = {"fp", "sp"}; |
| 133 |
int32_t cfa_offset = 0; |
| 134 |
int32_t fp_offset = 0; |
| 135 |
int32_t ra_offset = 0; |
| 136 |
uint8_t base_reg_id = 0; |
| 137 |
int err[3] = {0, 0, 0}; |
| 138 |
|
| 139 |
sframe_frame_row_entry fre; |
| 140 |
|
| 141 |
/* Get the SFrame function descriptor. */ |
| 142 |
sframe_decoder_get_funcdesc (sfd_ctx, funcidx, &num_fres, |
| 143 |
&func_size, &func_start_address, &func_info); |
| 144 |
/* Calculate the virtual memory address for function start pc. Some older |
| 145 |
SFrame V2 sections in ET_DYN or ET_EXEC may still have the |
| 146 |
SFRAME_F_FDE_FUNC_START_PCREL flag unset, and hence may be using the |
| 147 |
old encoding. Continue to support dumping the sections at least. */ |
| 148 |
func_start_pc_vma = func_start_address + sec_addr; |
| 149 |
if (sframe_decoder_get_flags (sfd_ctx) & SFRAME_F_FDE_FUNC_START_PCREL) |
| 150 |
func_start_pc_vma += sframe_decoder_get_offsetof_fde_start_addr (sfd_ctx, |
| 151 |
funcidx, |
| 152 |
NULL); |
| 153 |
|
| 154 |
/* Mark FDEs with [m] where the FRE start address is interpreted as a |
| 155 |
mask. */ |
| 156 |
int fde_type_addrmask_p = (SFRAME_V1_FUNC_FDE_TYPE (func_info) |
| 157 |
== SFRAME_FDE_TYPE_PCMASK); |
| 158 |
const char *fde_type_marker |
| 159 |
= (fde_type_addrmask_p ? "[m]" : " "); |
| 160 |
|
| 161 |
printf ("\n func idx [%d]: pc = 0x%"PRIx64 ", size = %d bytes", |
| 162 |
funcidx, |
| 163 |
func_start_pc_vma, |
| 164 |
func_size); |
| 165 |
|
| 166 |
if (is_sframe_abi_arch_aarch64 (sfd_ctx) |
| 167 |
&& (SFRAME_V1_FUNC_PAUTH_KEY (func_info) == SFRAME_AARCH64_PAUTH_KEY_B)) |
| 168 |
printf (", pauth = B key"); |
| 169 |
|
| 170 |
char temp[100]; |
| 171 |
|
| 172 |
printf ("\n %-7s%-8s %-10s%-10s%-13s", |
| 173 |
"STARTPC", fde_type_marker, "CFA", "FP", "RA"); |
| 174 |
for (j = 0; j < num_fres; j++) |
| 175 |
{ |
| 176 |
sframe_decoder_get_fre (sfd_ctx, funcidx, j, &fre); |
| 177 |
|
| 178 |
fre_start_pc_vma = (fde_type_addrmask_p |
| 179 |
? fre.fre_start_addr |
| 180 |
: func_start_pc_vma + fre.fre_start_addr); |
| 181 |
|
| 182 |
/* FIXME - fixup the err caching in array. |
| 183 |
assert no error for base reg id. */ |
| 184 |
base_reg_id = sframe_fre_get_base_reg_id (&fre, &err[0]); |
| 185 |
cfa_offset = sframe_fre_get_cfa_offset (sfd_ctx, &fre, &err[0]); |
| 186 |
fp_offset = sframe_fre_get_fp_offset (sfd_ctx, &fre, &err[1]); |
| 187 |
ra_offset = sframe_fre_get_ra_offset (sfd_ctx, &fre, &err[2]); |
| 188 |
|
| 189 |
/* Dump CFA info. */ |
| 190 |
printf ("\n"); |
| 191 |
printf (" %016"PRIx64, fre_start_pc_vma); |
| 192 |
sprintf (temp, "%s+%d", base_reg_str[base_reg_id], cfa_offset); |
| 193 |
printf (" %-10s", temp); |
| 194 |
|
| 195 |
/* Dump SP/FP info. */ |
| 196 |
if (err[1] == 0) |
| 197 |
{ |
| 198 |
if (is_sframe_abi_arch_s390x (sfd_ctx) |
| 199 |
&& SFRAME_V2_S390X_OFFSET_IS_REGNUM (fp_offset)) |
| 200 |
sprintf (temp, "r%d", SFRAME_V2_S390X_OFFSET_DECODE_REGNUM (fp_offset)); |
| 201 |
else |
| 202 |
sprintf (temp, "c%+d", fp_offset); |
| 203 |
} |
| 204 |
else |
| 205 |
strcpy (temp, "u"); |
| 206 |
printf ("%-10s", temp); |
| 207 |
|
| 208 |
/* Dump RA info. |
| 209 |
If an ABI does not track RA offset, e.g., AMD64, display 'f', |
| 210 |
else display the offset d as 'c+-d'. */ |
| 211 |
if (sframe_decoder_get_fixed_ra_offset (sfd_ctx) |
| 212 |
!= SFRAME_CFA_FIXED_RA_INVALID) |
| 213 |
strcpy (temp, "f"); |
| 214 |
/* If an ABI does track RA offset, e.g. s390x, it can be a padding |
| 215 |
to represent FP without RA being saved on stack. */ |
| 216 |
else if (err[2] == 0 && ra_offset == SFRAME_FRE_RA_OFFSET_INVALID) |
| 217 |
sprintf (temp, "U"); |
| 218 |
else if (err[2] == 0) |
| 219 |
{ |
| 220 |
if (is_sframe_abi_arch_s390x (sfd_ctx) |
| 221 |
&& SFRAME_V2_S390X_OFFSET_IS_REGNUM (ra_offset)) |
| 222 |
sprintf (temp, "r%d", SFRAME_V2_S390X_OFFSET_DECODE_REGNUM (ra_offset)); |
| 223 |
else |
| 224 |
sprintf (temp, "c%+d", ra_offset); |
| 225 |
} |
| 226 |
else |
| 227 |
strcpy (temp, "u"); |
| 228 |
|
| 229 |
/* Mark SFrame FRE's RA information with "[s]" if the RA is mangled |
| 230 |
with signature bits. */ |
| 231 |
const char *ra_mangled_p_str |
| 232 |
= ((sframe_fre_get_ra_mangled_p (sfd_ctx, &fre, &err[2])) |
| 233 |
? "[s]" : " "); |
| 234 |
strcat (temp, ra_mangled_p_str); |
| 235 |
printf ("%-13s", temp); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
dump_sframe_functions (sframe_decoder_ctx *sfd_ctx, uint64_t sec_addr) |
| 241 |
{ |
| 242 |
uint32_t i; |
| 243 |
uint32_t num_fdes; |
| 244 |
|
| 245 |
const char* subsec_name = "Function Index"; |
| 246 |
printf ("\n %s :\n", subsec_name); |
| 247 |
|
| 248 |
num_fdes = sframe_decoder_get_num_fidx (sfd_ctx); |
| 249 |
for (i = 0; i < num_fdes; i++) |
| 250 |
{ |
| 251 |
dump_sframe_func_with_fres (sfd_ctx, i, sec_addr); |
| 252 |
printf ("\n"); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
void |
| 257 |
dump_sframe (sframe_decoder_ctx *sfd_ctx, uint64_t sec_addr) |
| 258 |
{ |
| 259 |
uint8_t ver; |
| 260 |
|
| 261 |
dump_sframe_header (sfd_ctx); |
| 262 |
|
| 263 |
ver = sframe_decoder_get_version (sfd_ctx); |
| 264 |
if (ver == SFRAME_VERSION) |
| 265 |
dump_sframe_functions (sfd_ctx, sec_addr); |
| 266 |
else |
| 267 |
printf ("\n No further information can be displayed. %s", |
| 268 |
"SFrame version not supported\n"); |
| 269 |
} |