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