]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - libsframe/sframe-dump.c
libsframe: add symbol versioning
[thirdparty/binutils-gdb.git] / libsframe / sframe-dump.c
CommitLineData
42b6953b
IB
1/* sframe-dump.c - Textual dump of .sframe.
2
d87bef3a 3 Copyright (C) 2022-2023 Free Software Foundation, Inc.
42b6953b 4
f4af4272 5 This file is part of libsframe.
42b6953b
IB
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#define SFRAME_HEADER_FLAGS_STR_MAX_LEN 50
27
95e829af
IB
28/* Return TRUE if the SFrame section is associated with the aarch64 ABIs. */
29
30static bool
31is_sframe_abi_arch_aarch64 (sframe_decoder_ctx *sfd_ctx)
32{
33 bool aarch64_p = false;
34
35 unsigned char abi_arch = sframe_decoder_get_abi_arch (sfd_ctx);
36 if ((abi_arch == SFRAME_ABI_AARCH64_ENDIAN_BIG)
37 || (abi_arch == SFRAME_ABI_AARCH64_ENDIAN_LITTLE))
38 aarch64_p = true;
39
40 return aarch64_p;
41}
42
42b6953b
IB
43static void
44dump_sframe_header (sframe_decoder_ctx *sfd_ctx)
45{
46 const char *verstr = NULL;
47 const sframe_header *header = &(sfd_ctx->sfd_header);
48
49 /* Prepare SFrame section version string. */
50 const char *version_names[]
51 = { "NULL",
52 "SFRAME_VERSION_1" };
53 unsigned char ver = header->sfh_preamble.sfp_version;
54 if (ver <= SFRAME_VERSION)
55 verstr = version_names[ver];
56
57 /* Prepare SFrame section flags string. */
58 unsigned char flags = header->sfh_preamble.sfp_flags;
59 char *flags_str
60 = (char*) calloc (sizeof (char), SFRAME_HEADER_FLAGS_STR_MAX_LEN);
61 if (flags)
62 {
63 const char *flag_names[]
64 = { "SFRAME_F_FDE_SORTED",
65 "SFRAME_F_FRAME_POINTER" };
66 unsigned char flags = header->sfh_preamble.sfp_flags;
67 if (flags & SFRAME_F_FDE_SORTED)
68 strcpy (flags_str, flag_names[0]);
69 if (flags & SFRAME_F_FRAME_POINTER)
70 {
71 if (strlen (flags_str) > 0)
72 strcpy (flags_str, ",");
73 strcpy (flags_str, flag_names[1]);
74 }
75 }
76 else
77 strcpy (flags_str, "NONE");
78
79 const char* subsec_name = "Header";
80 printf ("\n");
81 printf (" %s :\n", subsec_name);
82 printf ("\n");
83 printf (" Version: %s\n", verstr);
84 printf (" Flags: %s\n", flags_str);
85 printf (" Num FDEs: %d\n", header->sfh_num_fdes);
86 printf (" Num FREs: %d\n", header->sfh_num_fres);
87
88 free (flags_str);
89}
90
91static void
92dump_sframe_func_with_fres (sframe_decoder_ctx *sfd_ctx,
93 unsigned int funcidx,
94 uint64_t sec_addr)
95{
96 uint32_t j = 0;
97 uint32_t num_fres = 0;
98 uint32_t func_size = 0;
99 int32_t func_start_address = 0;
100 unsigned char func_info = 0;
101
102 uint64_t func_start_pc_vma = 0;
103 uint64_t fre_start_pc_vma = 0;
104 const char *base_reg_str[] = {"fp", "sp"};
105 int32_t cfa_offset = 0;
106 int32_t fp_offset = 0;
107 int32_t ra_offset = 0;
108 unsigned int base_reg_id = 0;
109 int err[3] = {0, 0, 0};
110
111 sframe_frame_row_entry fre;
112
113 /* Get the SFrame function descriptor. */
114 sframe_decoder_get_funcdesc (sfd_ctx, funcidx, &num_fres,
115 &func_size, &func_start_address, &func_info);
116 /* Calculate the virtual memory address for function start pc. */
117 func_start_pc_vma = func_start_address + sec_addr;
118
119 /* Mark FDEs with [m] where the FRE start address is interpreted as a
120 mask. */
121 int fde_type_addrmask_p = (SFRAME_V1_FUNC_FDE_TYPE (func_info)
122 == SFRAME_FDE_TYPE_PCMASK);
123 const char *fde_type_marker
124 = (fde_type_addrmask_p ? "[m]" : " ");
125
126 printf ("\n func idx [%d]: pc = 0x%"PRIx64 ", size = %d bytes",
127 funcidx,
128 func_start_pc_vma,
129 func_size);
130
95e829af
IB
131 if (is_sframe_abi_arch_aarch64 (sfd_ctx)
132 && (SFRAME_V1_FUNC_PAUTH_KEY (func_info) == SFRAME_AARCH64_PAUTH_KEY_B))
133 printf (", pauth = B key");
134
42b6953b 135 char temp[100];
42b6953b 136
f4af4272
IB
137 printf ("\n %-7s%-8s %-10s%-10s%-13s",
138 "STARTPC", fde_type_marker, "CFA", "FP", "RA");
42b6953b
IB
139 for (j = 0; j < num_fres; j++)
140 {
141 sframe_decoder_get_fre (sfd_ctx, funcidx, j, &fre);
142
143 fre_start_pc_vma = (fde_type_addrmask_p
144 ? fre.fre_start_addr
145 : func_start_pc_vma + fre.fre_start_addr);
146
147 /* FIXME - fixup the err caching in array.
148 assert no error for base reg id. */
149 base_reg_id = sframe_fre_get_base_reg_id (&fre, &err[0]);
150 cfa_offset = sframe_fre_get_cfa_offset (sfd_ctx, &fre, &err[0]);
151 fp_offset = sframe_fre_get_fp_offset (sfd_ctx, &fre, &err[1]);
152 ra_offset = sframe_fre_get_ra_offset (sfd_ctx, &fre, &err[2]);
153
154 /* Dump CFA info. */
155 printf ("\n");
156 printf (" %016"PRIx64, fre_start_pc_vma);
157 sprintf (temp, "%s+%d", base_reg_str[base_reg_id], cfa_offset);
158 printf (" %-10s", temp);
159
160 /* Dump SP/FP info. */
42b6953b
IB
161 if (err[1] == 0)
162 sprintf (temp, "c%+d", fp_offset);
163 else
164 strcpy (temp, "u");
165 printf ("%-10s", temp);
166
167 /* Dump RA info. */
42b6953b
IB
168 if (err[2] == 0)
169 sprintf (temp, "c%+d", ra_offset);
170 else
171 strcpy (temp, "u");
49948bce
IB
172 /* Mark SFrame FRE's RA information with "[s]" if the RA is mangled
173 with signature bits. */
174 const char *ra_mangled_p_str
175 = ((sframe_fre_get_ra_mangled_p (sfd_ctx, &fre, &err[2]))
176 ? "[s]" : " ");
68e0003e 177 strcat (temp, ra_mangled_p_str);
49948bce 178 printf ("%-13s", temp);
42b6953b
IB
179 }
180}
181
182static void
183dump_sframe_functions (sframe_decoder_ctx *sfd_ctx, uint64_t sec_addr)
184{
185 uint32_t i;
186 uint32_t num_fdes;
187
188 const char* subsec_name = "Function Index";
189 printf ("\n %s :\n", subsec_name);
190
191 num_fdes = sframe_decoder_get_num_fidx (sfd_ctx);
192 for (i = 0; i < num_fdes; i++)
193 {
194 dump_sframe_func_with_fres (sfd_ctx, i, sec_addr);
195 printf ("\n");
196 }
197}
198
199void
200dump_sframe (sframe_decoder_ctx *sfd_ctx, uint64_t sec_addr)
201{
202 dump_sframe_header (sfd_ctx);
203 dump_sframe_functions (sfd_ctx, sec_addr);
204}