Debian Codegraph @

libsframe/sframe.c

raw
1 /* sframe.c - SFrame decoder/encoder.
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 "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <stddef.h>
26 #include "sframe-impl.h"
27 #include "swap.h"
28
29 struct sf_fde_tbl
30 {
31 unsigned int count;
32 unsigned int alloced;
33 sframe_func_desc_entry entry[1];
34 };
35
36 struct sf_fre_tbl
37 {
38 unsigned int count;
39 unsigned int alloced;
40 sframe_frame_row_entry entry[1];
41 };
42
43 #define _sf_printflike_(string_index,first_to_check) \
44 __attribute__ ((__format__ (__printf__, (string_index), (first_to_check))))
45
46 static void debug_printf (const char *, ...);
47
48 static int _sframe_debug; /* Control for printing out debug info. */
49 static int number_of_entries = 64;
50
51 static void
52 sframe_init_debug (void)
53 {
54 static int inited;
55
56 if (!inited)
57 {
58 _sframe_debug = getenv ("SFRAME_DEBUG") != NULL;
59 inited = 1;
60 }
61 }
62
63 _sf_printflike_ (1, 2)
64 static void debug_printf (const char *format, ...)
65 {
66 if (_sframe_debug)
67 {
68 va_list args;
69
70 va_start (args, format);
71 vfprintf (stderr, format, args);
72 va_end (args);
73 }
74 }
75
76 /* Generate bitmask of given size in bytes. This is used for
77 some checks on the FRE start address.
78 SFRAME_FRE_TYPE_ADDR1 => 1 byte => [ bitmask = 0xff ]
79 SFRAME_FRE_TYPE_ADDR2 => 2 byte => [ bitmask = 0xffff ]
80 SFRAME_FRE_TYPE_ADDR4 => 4 byte => [ bitmask = 0xffffffff ]. */
81 #define SFRAME_BITMASK_OF_SIZE(size_in_bytes) \
82 (((uint64_t)1 << (size_in_bytes*8)) - 1)
83
84 /* Store the specified error code into errp if it is non-NULL.
85 Return SFRAME_ERR. */
86
87 static int
88 sframe_set_errno (int *errp, int error)
89 {
90 if (errp != NULL)
91 *errp = error;
92 return SFRAME_ERR;
93 }
94
95 /* Store the specified error code into errp if it is non-NULL.
96 Return NULL. */
97
98 static void *
99 sframe_ret_set_errno (int *errp, int error)
100 {
101 if (errp != NULL)
102 *errp = error;
103 return NULL;
104 }
105
106 /* Get the SFrame header size. */
107
108 static uint32_t
109 sframe_get_hdr_size (sframe_header *sfh)
110 {
111 return SFRAME_V1_HDR_SIZE (*sfh);
112 }
113
114 /* Access functions for frame row entry data. */
115
116 static uint8_t
117 sframe_fre_get_offset_count (uint8_t fre_info)
118 {
119 return SFRAME_V1_FRE_OFFSET_COUNT (fre_info);
120 }
121
122 static uint8_t
123 sframe_fre_get_offset_size (uint8_t fre_info)
124 {
125 return SFRAME_V1_FRE_OFFSET_SIZE (fre_info);
126 }
127
128 static bool
129 sframe_get_fre_ra_mangled_p (uint8_t fre_info)
130 {
131 return SFRAME_V1_FRE_MANGLED_RA_P (fre_info);
132 }
133
134 /* Access functions for info from function descriptor entry. */
135
136 static uint32_t
137 sframe_get_fre_type (sframe_func_desc_entry *fdep)
138 {
139 uint32_t fre_type = 0;
140 if (fdep)
141 fre_type = SFRAME_V1_FUNC_FRE_TYPE (fdep->sfde_func_info);
142 return fre_type;
143 }
144
145 static uint32_t
146 sframe_get_fde_type (sframe_func_desc_entry *fdep)
147 {
148 uint32_t fde_type = 0;
149 if (fdep)
150 fde_type = SFRAME_V1_FUNC_FDE_TYPE (fdep->sfde_func_info);
151 return fde_type;
152 }
153
154 /* Check if flipping is needed, based on ENDIAN. */
155
156 static int
157 need_swapping (int endian)
158 {
159 unsigned int ui = 1;
160 char *c = (char *)&ui;
161 int is_little = (int)*c;
162
163 switch (endian)
164 {
165 case SFRAME_ABI_AARCH64_ENDIAN_LITTLE:
166 case SFRAME_ABI_AMD64_ENDIAN_LITTLE:
167 return !is_little;
168 case SFRAME_ABI_AARCH64_ENDIAN_BIG:
169 case SFRAME_ABI_S390X_ENDIAN_BIG:
170 return is_little;
171 default:
172 break;
173 }
174
175 return 0;
176 }
177
178 /* Flip the endianness of the SFrame header. */
179
180 static void
181 flip_header (sframe_header *sfheader)
182 {
183 swap_thing (sfheader->sfh_preamble.sfp_magic);
184 swap_thing (sfheader->sfh_preamble.sfp_version);
185 swap_thing (sfheader->sfh_preamble.sfp_flags);
186 swap_thing (sfheader->sfh_cfa_fixed_fp_offset);
187 swap_thing (sfheader->sfh_cfa_fixed_ra_offset);
188 swap_thing (sfheader->sfh_num_fdes);
189 swap_thing (sfheader->sfh_num_fres);
190 swap_thing (sfheader->sfh_fre_len);
191 swap_thing (sfheader->sfh_fdeoff);
192 swap_thing (sfheader->sfh_freoff);
193 }
194
195 static void
196 flip_fde (sframe_func_desc_entry *fdep)
197 {
198 swap_thing (fdep->sfde_func_start_address);
199 swap_thing (fdep->sfde_func_size);
200 swap_thing (fdep->sfde_func_start_fre_off);
201 swap_thing (fdep->sfde_func_num_fres);
202 }
203
204 /* Check if SFrame header has valid data. */
205
206 static bool
207 sframe_header_sanity_check_p (sframe_header *hp)
208 {
209 /* Check preamble is valid. */
210 if (hp->sfh_preamble.sfp_magic != SFRAME_MAGIC
211 || (hp->sfh_preamble.sfp_version != SFRAME_VERSION_1
212 && hp->sfh_preamble.sfp_version != SFRAME_VERSION_2)
213 || (hp->sfh_preamble.sfp_flags & ~SFRAME_V2_F_ALL_FLAGS))
214 return false;
215
216 /* Check offsets are valid. */
217 if (hp->sfh_fdeoff > hp->sfh_freoff)
218 return false;
219
220 return true;
221 }
222
223 /* Flip the start address pointed to by FP. */
224
225 static void
226 flip_fre_start_address (char *addr, uint32_t fre_type)
227 {
228 if (fre_type == SFRAME_FRE_TYPE_ADDR2)
229 {
230 uint16_t *start_addr = (uint16_t *)addr;
231 swap_thing (*start_addr);
232 }
233 else if (fre_type == SFRAME_FRE_TYPE_ADDR4)
234 {
235 uint32_t *start_addr = (uint32_t *)addr;
236 swap_thing (*start_addr);
237 }
238 }
239
240 static void
241 flip_fre_stack_offsets (char *offsets, uint8_t offset_size, uint8_t offset_cnt)
242 {
243 int j;
244
245 if (offset_size == SFRAME_FRE_OFFSET_2B)
246 {
247 uint16_t *ust = (uint16_t *)offsets;
248 for (j = offset_cnt; j > 0; ust++, j--)
249 swap_thing (*ust);
250 }
251 else if (offset_size == SFRAME_FRE_OFFSET_4B)
252 {
253 uint32_t *uit = (uint32_t *)offsets;
254 for (j = offset_cnt; j > 0; uit++, j--)
255 swap_thing (*uit);
256 }
257 }
258
259 /* Get the FRE start address size, given the FRE_TYPE. */
260
261 static size_t
262 sframe_fre_start_addr_size (uint32_t fre_type)
263 {
264 size_t addr_size = 0;
265 switch (fre_type)
266 {
267 case SFRAME_FRE_TYPE_ADDR1:
268 addr_size = 1;
269 break;
270 case SFRAME_FRE_TYPE_ADDR2:
271 addr_size = 2;
272 break;
273 case SFRAME_FRE_TYPE_ADDR4:
274 addr_size = 4;
275 break;
276 default:
277 /* No other value is expected. */
278 sframe_assert (0);
279 break;
280 }
281 return addr_size;
282 }
283
284 /* Check if the FREP has valid data. */
285
286 static bool
287 sframe_fre_sanity_check_p (sframe_frame_row_entry *frep)
288 {
289 uint8_t offset_size, offset_cnt;
290 uint8_t fre_info;
291
292 if (frep == NULL)
293 return false;
294
295 fre_info = frep->fre_info;
296 offset_size = sframe_fre_get_offset_size (fre_info);
297
298 if (offset_size != SFRAME_FRE_OFFSET_1B
299 && offset_size != SFRAME_FRE_OFFSET_2B
300 && offset_size != SFRAME_FRE_OFFSET_4B)
301 return false;
302
303 offset_cnt = sframe_fre_get_offset_count (fre_info);
304 if (offset_cnt > MAX_NUM_STACK_OFFSETS)
305 return false;
306
307 return true;
308 }
309
310 /* Get FRE_INFO's offset size in bytes. */
311
312 static size_t
313 sframe_fre_offset_bytes_size (uint8_t fre_info)
314 {
315 uint8_t offset_size, offset_cnt;
316
317 offset_size = sframe_fre_get_offset_size (fre_info);
318
319 debug_printf ("offset_size = %u\n", offset_size);
320
321 offset_cnt = sframe_fre_get_offset_count (fre_info);
322
323 if (offset_size == SFRAME_FRE_OFFSET_2B
324 || offset_size == SFRAME_FRE_OFFSET_4B) /* 2 or 4 bytes. */
325 return (offset_cnt * (offset_size * 2));
326
327 return (offset_cnt);
328 }
329
330 /* Get total size in bytes to represent FREP in the binary format. This
331 includes the starting address, FRE info, and all the offsets. */
332
333 static size_t
334 sframe_fre_entry_size (sframe_frame_row_entry *frep, uint32_t fre_type)
335 {
336 if (frep == NULL)
337 return 0;
338
339 uint8_t fre_info = frep->fre_info;
340 size_t addr_size = sframe_fre_start_addr_size (fre_type);
341
342 return (addr_size + sizeof (frep->fre_info)
343 + sframe_fre_offset_bytes_size (fre_info));
344 }
345
346 /* Get the function descriptor entry at index FUNC_IDX in the decoder
347 context CTX. */
348
349 static sframe_func_desc_entry *
350 sframe_decoder_get_funcdesc_at_index (sframe_decoder_ctx *ctx,
351 uint32_t func_idx)
352 {
353 sframe_func_desc_entry *fdep;
354 uint32_t num_fdes;
355 int err;
356
357 num_fdes = sframe_decoder_get_num_fidx (ctx);
358 if (num_fdes == 0
359 || func_idx >= num_fdes
360 || ctx->sfd_funcdesc == NULL)
361 return sframe_ret_set_errno (&err, SFRAME_ERR_DCTX_INVAL);
362
363 fdep = &ctx->sfd_funcdesc[func_idx];
364 return fdep;
365 }
366
367 /* Get the offset of the start PC of the SFrame FDE at FUNC_IDX from the start
368 of the SFrame section. This section-relative offset is used within
369 libsframe for sorting the SFrame FDEs, and also information lookup routines
370 like sframe_find_fre.
371
372 If FUNC_IDX is not a valid index in the given decoder object, returns 0. */
373
374 static int32_t
375 sframe_decoder_get_secrel_func_start_addr (sframe_decoder_ctx *dctx,
376 uint32_t func_idx)
377 {
378 int err = 0;
379 int32_t offsetof_fde_in_sec
380 = sframe_decoder_get_offsetof_fde_start_addr (dctx, func_idx, &err);
381 /* If func_idx is not a valid index, return 0. */
382 if (err)
383 return 0;
384
385 int32_t func_start_addr = dctx->sfd_funcdesc[func_idx].sfde_func_start_address;
386
387 return func_start_addr + offsetof_fde_in_sec;
388 }
389
390 /* Check whether for the given FDEP, the SFrame Frame Row Entry identified via
391 the START_IP_OFFSET and the END_IP_OFFSET, provides the stack trace
392 information for the PC. */
393
394 static bool
395 sframe_fre_check_range_p (sframe_decoder_ctx *dctx, uint32_t func_idx,
396 uint32_t start_ip_offset, uint32_t end_ip_offset,
397 int32_t pc)
398 {
399 sframe_func_desc_entry *fdep;
400 int32_t func_start_addr;
401 uint8_t rep_block_size;
402 uint32_t fde_type;
403 uint32_t pc_offset;
404 bool mask_p;
405
406 fdep = &dctx->sfd_funcdesc[func_idx];
407 func_start_addr = sframe_decoder_get_secrel_func_start_addr (dctx, func_idx);
408 fde_type = sframe_get_fde_type (fdep);
409 mask_p = (fde_type == SFRAME_FDE_TYPE_PCMASK);
410 rep_block_size = fdep->sfde_func_rep_size;
411
412 if (func_start_addr > pc)
413 return false;
414
415 /* Given func_start_addr <= pc, pc - func_start_addr must be positive. */
416 pc_offset = pc - func_start_addr;
417 /* For SFrame FDEs encoding information for repetitive pattern of insns,
418 masking with the rep_block_size is necessary to find the matching FRE. */
419 if (mask_p)
420 pc_offset = pc_offset % rep_block_size;
421
422 return (start_ip_offset <= pc_offset) && (end_ip_offset >= pc_offset);
423 }
424
425 static int
426 flip_fre (char *fp, uint32_t fre_type, size_t *fre_size)
427 {
428 uint8_t fre_info;
429 uint8_t offset_size, offset_cnt;
430 size_t addr_size, fre_info_size = 0;
431 int err = 0;
432
433 if (fre_size == NULL)
434 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
435
436 flip_fre_start_address (fp, fre_type);
437
438 /* Advance the buffer pointer to where the FRE info is. */
439 addr_size = sframe_fre_start_addr_size (fre_type);
440 fp += addr_size;
441
442 /* FRE info is uint8_t. No need to flip. */
443 fre_info = *(uint8_t*)fp;
444 offset_size = sframe_fre_get_offset_size (fre_info);
445 offset_cnt = sframe_fre_get_offset_count (fre_info);
446
447 /* Advance the buffer pointer to where the stack offsets are. */
448 fre_info_size = sizeof (uint8_t);
449 fp += fre_info_size;
450 flip_fre_stack_offsets (fp, offset_size, offset_cnt);
451
452 *fre_size
453 = addr_size + fre_info_size + sframe_fre_offset_bytes_size (fre_info);
454
455 return 0;
456 }
457
458 /* Endian flip the contents of FRAME_BUF of size BUF_SIZE.
459 The SFrame header in the FRAME_BUF must be endian flipped prior to
460 calling flip_sframe.
461
462 Endian flipping at decode time vs encode time have different needs. At
463 encode time, the frame_buf is in host endianness, and hence, values should
464 be read up before the buffer is changed to foreign endianness. This change
465 of behaviour is specified via TO_FOREIGN arg.
466
467 If an error code is returned, the buffer should not be used. */
468
469 static int
470 flip_sframe (char *frame_buf, size_t buf_size, uint32_t to_foreign)
471 {
472 unsigned int i, j, prev_frep_index;
473 sframe_header *ihp;
474 char *fdes;
475 char *fp = NULL;
476 sframe_func_desc_entry *fdep;
477 unsigned int num_fdes = 0;
478 unsigned int num_fres = 0;
479 uint32_t fre_type = 0;
480 uint32_t fre_offset = 0;
481 size_t esz = 0;
482 size_t hdrsz = 0;
483 int err = 0;
484 /* For error checking. */
485 size_t bytes_flipped = 0;
486
487 /* Header must be in host endianness at this time. */
488 ihp = (sframe_header *)frame_buf;
489
490 if (!sframe_header_sanity_check_p (ihp))
491 return sframe_set_errno (&err, SFRAME_ERR_BUF_INVAL);
492
493 /* The contents of the SFrame header are safe to read. Get the number of
494 FDEs and the first FDE in the buffer. */
495 hdrsz = sframe_get_hdr_size (ihp);
496 num_fdes = ihp->sfh_num_fdes;
497 fdes = frame_buf + hdrsz + ihp->sfh_fdeoff;
498 fdep = (sframe_func_desc_entry *)fdes;
499
500 j = 0;
501 prev_frep_index = 0;
502 for (i = 0; i < num_fdes; fdep++, i++)
503 {
504 if ((char*)fdep >= (frame_buf + buf_size))
505 goto bad;
506
507 if (to_foreign)
508 {
509 num_fres = fdep->sfde_func_num_fres;
510 fre_type = sframe_get_fre_type (fdep);
511 fre_offset = fdep->sfde_func_start_fre_off;
512 }
513
514 flip_fde (fdep);
515 bytes_flipped += sizeof (sframe_func_desc_entry);
516
517 if (!to_foreign)
518 {
519 num_fres = fdep->sfde_func_num_fres;
520 fre_type = sframe_get_fre_type (fdep);
521 fre_offset = fdep->sfde_func_start_fre_off;
522 }
523
524 fp = frame_buf + hdrsz + ihp->sfh_freoff;
525 fp += fre_offset;
526 for (; j < prev_frep_index + num_fres; j++)
527 {
528 if (flip_fre (fp, fre_type, &esz))
529 goto bad;
530 bytes_flipped += esz;
531
532 if (esz == 0 || esz > buf_size)
533 goto bad;
534 fp += esz;
535 }
536 prev_frep_index = j;
537 }
538 /* All FDEs and FREs must have been endian flipped by now. */
539 if ((j != ihp->sfh_num_fres) || (bytes_flipped > (buf_size - hdrsz)))
540 goto bad;
541 /* Optional trailing section padding. */
542 for (fp = frame_buf + hdrsz + bytes_flipped; fp < frame_buf + buf_size; fp++)
543 if (*fp != '\0')
544 goto bad;
545
546 /* Success. */
547 return 0;
548 bad:
549 return SFRAME_ERR;
550 }
551
552 /* The SFrame Decoder. */
553
554 /* Get SFrame header from the given decoder context DCTX. */
555
556 static sframe_header *
557 sframe_decoder_get_header (sframe_decoder_ctx *dctx)
558 {
559 sframe_header *hp = NULL;
560 if (dctx != NULL)
561 hp = &dctx->sfd_header;
562 return hp;
563 }
564
565 /* Compare function for qsort'ing the FDE table. */
566
567 static int
568 fde_func (const void *p1, const void *p2)
569 {
570 const sframe_func_desc_entry *aa = p1;
571 const sframe_func_desc_entry *bb = p2;
572
573 if (aa->sfde_func_start_address < bb->sfde_func_start_address)
574 return -1;
575 else if (aa->sfde_func_start_address > bb->sfde_func_start_address)
576 return 1;
577 return 0;
578 }
579
580 /* Get IDX'th offset from FRE. Set errp as applicable. */
581
582 static int32_t
583 sframe_get_fre_offset (sframe_frame_row_entry *fre, int idx, int *errp)
584 {
585 uint8_t offset_cnt, offset_size;
586
587 if (fre == NULL || !sframe_fre_sanity_check_p (fre))
588 return sframe_set_errno (errp, SFRAME_ERR_FRE_INVAL);
589
590 offset_cnt = sframe_fre_get_offset_count (fre->fre_info);
591 offset_size = sframe_fre_get_offset_size (fre->fre_info);
592
593 if (offset_cnt < idx + 1)
594 return sframe_set_errno (errp, SFRAME_ERR_FREOFFSET_NOPRESENT);
595
596 if (errp)
597 *errp = 0; /* Offset Valid. */
598
599 if (offset_size == SFRAME_FRE_OFFSET_1B)
600 {
601 int8_t *sp = (int8_t *)fre->fre_offsets;
602 return sp[idx];
603 }
604 else if (offset_size == SFRAME_FRE_OFFSET_2B)
605 {
606 int16_t *sp = (int16_t *)fre->fre_offsets;
607 return sp[idx];
608 }
609 else
610 {
611 int32_t *ip = (int32_t *)fre->fre_offsets;
612 return ip[idx];
613 }
614 }
615
616 /* Free the decoder context. */
617
618 void
619 sframe_decoder_free (sframe_decoder_ctx **dctxp)
620 {
621 if (dctxp != NULL)
622 {
623 sframe_decoder_ctx *dctx = *dctxp;
624 if (dctx == NULL)
625 return;
626
627 if (dctx->sfd_funcdesc != NULL)
628 {
629 free (dctx->sfd_funcdesc);
630 dctx->sfd_funcdesc = NULL;
631 }
632 if (dctx->sfd_fres != NULL)
633 {
634 free (dctx->sfd_fres);
635 dctx->sfd_fres = NULL;
636 }
637 if (dctx->sfd_buf != NULL)
638 {
639 free (dctx->sfd_buf);
640 dctx->sfd_buf = NULL;
641 }
642
643 free (*dctxp);
644 *dctxp = NULL;
645 }
646 }
647
648 /* Create an FDE function info byte given an FRE_TYPE and an FDE_TYPE. */
649 /* FIXME API for linker. Revisit if its better placed somewhere else? */
650
651 unsigned char
652 sframe_fde_create_func_info (uint32_t fre_type,
653 uint32_t fde_type)
654 {
655 unsigned char func_info;
656 sframe_assert (fre_type == SFRAME_FRE_TYPE_ADDR1
657 || fre_type == SFRAME_FRE_TYPE_ADDR2
658 || fre_type == SFRAME_FRE_TYPE_ADDR4);
659 sframe_assert (fde_type == SFRAME_FDE_TYPE_PCINC
660 || fde_type == SFRAME_FDE_TYPE_PCMASK);
661 func_info = SFRAME_V1_FUNC_INFO (fde_type, fre_type);
662 return func_info;
663 }
664
665 /* Get the FRE type given the function size. */
666 /* FIXME API for linker. Revisit if its better placed somewhere else? */
667
668 uint32_t
669 sframe_calc_fre_type (size_t func_size)
670 {
671 uint32_t fre_type = 0;
672 if (func_size < SFRAME_FRE_TYPE_ADDR1_LIMIT)
673 fre_type = SFRAME_FRE_TYPE_ADDR1;
674 else if (func_size < SFRAME_FRE_TYPE_ADDR2_LIMIT)
675 fre_type = SFRAME_FRE_TYPE_ADDR2;
676 /* Adjust the check a bit so that it remains warning-free but meaningful
677 on 32-bit systems. */
678 else if (func_size <= (size_t) (SFRAME_FRE_TYPE_ADDR4_LIMIT - 1))
679 fre_type = SFRAME_FRE_TYPE_ADDR4;
680 return fre_type;
681 }
682
683 /* Get the base reg id from the FRE info. Set errp if failure. */
684
685 uint8_t
686 sframe_fre_get_base_reg_id (sframe_frame_row_entry *fre, int *errp)
687 {
688 if (fre == NULL)
689 return sframe_set_errno (errp, SFRAME_ERR_FRE_INVAL);
690
691 uint8_t fre_info = fre->fre_info;
692 return SFRAME_V1_FRE_CFA_BASE_REG_ID (fre_info);
693 }
694
695 /* Get the CFA offset from the FRE. If the offset is invalid, sets errp. */
696
697 int32_t
698 sframe_fre_get_cfa_offset (sframe_decoder_ctx *dctx,
699 sframe_frame_row_entry *fre, int *errp)
700 {
701 int32_t offset = sframe_get_fre_offset (fre, SFRAME_FRE_CFA_OFFSET_IDX, errp);
702
703 /* For s390x undo adjustment of CFA offset (to enable 8-bit offsets). */
704 if (sframe_decoder_get_abi_arch (dctx) == SFRAME_ABI_S390X_ENDIAN_BIG)
705 offset = SFRAME_V2_S390X_CFA_OFFSET_DECODE (offset);
706
707 return offset;
708 }
709
710 /* Get the FP offset from the FRE. If the offset is invalid, sets errp.
711
712 For s390x the offset may be an encoded register number, indicated by
713 LSB set to one, which is only valid in the topmost frame. */
714
715 int32_t
716 sframe_fre_get_fp_offset (sframe_decoder_ctx *dctx,
717 sframe_frame_row_entry *fre, int *errp)
718 {
719 uint32_t fp_offset_idx = 0;
720 int8_t fp_offset = sframe_decoder_get_fixed_fp_offset (dctx);
721 /* If the FP offset is not being tracked, return the fixed FP offset
722 from the SFrame header. */
723 if (fp_offset != SFRAME_CFA_FIXED_FP_INVALID)
724 {
725 if (errp)
726 *errp = 0;
727 return fp_offset;
728 }
729
730 /* In some ABIs, the stack offset to recover RA (using the CFA) from is
731 fixed (like AMD64). In such cases, the stack offset to recover FP will
732 appear at the second index. */
733 fp_offset_idx = ((sframe_decoder_get_fixed_ra_offset (dctx)
734 != SFRAME_CFA_FIXED_RA_INVALID)
735 ? SFRAME_FRE_RA_OFFSET_IDX
736 : SFRAME_FRE_FP_OFFSET_IDX);
737 return sframe_get_fre_offset (fre, fp_offset_idx, errp);
738 }
739
740 /* Get the RA offset from the FRE. If the offset is invalid, sets errp.
741
742 For s390x an RA offset value of SFRAME_FRE_RA_OFFSET_INVALID indicates
743 that the RA is not saved, which is only valid in the topmost frame.
744 For s390x the offset may be an encoded register number, indicated by
745 LSB set to one, which is only valid in the topmost frame. */
746
747 int32_t
748 sframe_fre_get_ra_offset (sframe_decoder_ctx *dctx,
749 sframe_frame_row_entry *fre, int *errp)
750 {
751 int8_t ra_offset = sframe_decoder_get_fixed_ra_offset (dctx);
752 /* If the RA offset was not being tracked, return the fixed RA offset
753 from the SFrame header. */
754 if (ra_offset != SFRAME_CFA_FIXED_RA_INVALID)
755 {
756 if (errp)
757 *errp = 0;
758 return ra_offset;
759 }
760
761 /* Otherwise, get the RA offset from the FRE. */
762 return sframe_get_fre_offset (fre, SFRAME_FRE_RA_OFFSET_IDX, errp);
763 }
764
765 /* Get whether the RA is mangled. */
766
767 bool
768 sframe_fre_get_ra_mangled_p (sframe_decoder_ctx *dctx ATTRIBUTE_UNUSED,
769 sframe_frame_row_entry *fre, int *errp)
770 {
771 if (fre == NULL || !sframe_fre_sanity_check_p (fre))
772 return sframe_set_errno (errp, SFRAME_ERR_FRE_INVAL);
773
774 return sframe_get_fre_ra_mangled_p (fre->fre_info);
775 }
776
777 static int
778 sframe_frame_row_entry_copy (sframe_frame_row_entry *dst,
779 sframe_frame_row_entry *src)
780 {
781 int err = 0;
782
783 if (dst == NULL || src == NULL)
784 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
785
786 memcpy (dst, src, sizeof (sframe_frame_row_entry));
787 return 0;
788 }
789
790 /* Decode the SFrame FRE start address offset value from FRE_BUF in on-disk
791 binary format, given the FRE_TYPE. Updates the FRE_START_ADDR.
792
793 Returns 0 on success, SFRAME_ERR otherwise. */
794
795 static int
796 sframe_decode_fre_start_address (const char *fre_buf,
797 uint32_t *fre_start_addr,
798 uint32_t fre_type)
799 {
800 uint32_t saddr = 0;
801 int err = 0;
802 size_t addr_size = 0;
803
804 addr_size = sframe_fre_start_addr_size (fre_type);
805
806 if (fre_type == SFRAME_FRE_TYPE_ADDR1)
807 {
808 uint8_t *uc = (uint8_t *)fre_buf;
809 saddr = (uint32_t)*uc;
810 }
811 else if (fre_type == SFRAME_FRE_TYPE_ADDR2)
812 {
813 uint16_t *ust = (uint16_t *)fre_buf;
814 /* SFrame is an unaligned on-disk format. Using memcpy helps avoid the
815 use of undesirable unaligned loads. See PR libsframe/29856. */
816 uint16_t tmp = 0;
817 memcpy (&tmp, ust, addr_size);
818 saddr = (uint32_t)tmp;
819 }
820 else if (fre_type == SFRAME_FRE_TYPE_ADDR4)
821 {
822 uint32_t *uit = (uint32_t *)fre_buf;
823 uint32_t tmp = 0;
824 memcpy (&tmp, uit, addr_size);
825 saddr = (uint32_t)tmp;
826 }
827 else
828 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
829
830 *fre_start_addr = saddr;
831 return 0;
832 }
833
834 /* Decode a frame row entry FRE which starts at location FRE_BUF. The function
835 updates ESZ to the size of the FRE as stored in the binary format.
836
837 This function works closely with the SFrame binary format.
838
839 Returns SFRAME_ERR if failure. */
840
841 static int
842 sframe_decode_fre (const char *fre_buf, sframe_frame_row_entry *fre,
843 uint32_t fre_type, size_t *esz)
844 {
845 int err = 0;
846 const char *stack_offsets = NULL;
847 size_t stack_offsets_sz;
848 size_t addr_size;
849 size_t fre_size;
850
851 if (fre_buf == NULL || fre == NULL || esz == NULL)
852 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
853
854 /* Copy over the FRE start address. */
855 sframe_decode_fre_start_address (fre_buf, &fre->fre_start_addr, fre_type);
856
857 addr_size = sframe_fre_start_addr_size (fre_type);
858 fre->fre_info = *(uint8_t *)(fre_buf + addr_size);
859 /* Sanity check as the API works closely with the binary format. */
860 sframe_assert (sizeof (fre->fre_info) == sizeof (uint8_t));
861
862 /* Cleanup the space for fre_offsets first, then copy over the valid
863 bytes. */
864 memset (fre->fre_offsets, 0, MAX_OFFSET_BYTES);
865 /* Get offsets size. */
866 stack_offsets_sz = sframe_fre_offset_bytes_size (fre->fre_info);
867 stack_offsets = fre_buf + addr_size + sizeof (fre->fre_info);
868 memcpy (fre->fre_offsets, stack_offsets, stack_offsets_sz);
869
870 /* The FRE has been decoded. Use it to perform one last sanity check. */
871 fre_size = sframe_fre_entry_size (fre, fre_type);
872 sframe_assert (fre_size == (addr_size + sizeof (fre->fre_info)
873 + stack_offsets_sz));
874 *esz = fre_size;
875
876 return 0;
877 }
878
879 /* Decode the specified SFrame buffer SF_BUF of size SF_SIZE and return the
880 new SFrame decoder context.
881
882 Sets ERRP for the caller if any error. Frees up the allocated memory in
883 case of error. */
884
885 sframe_decoder_ctx *
886 sframe_decode (const char *sf_buf, size_t sf_size, int *errp)
887 {
888 const sframe_preamble *sfp;
889 size_t hdrsz;
890 sframe_header *sfheaderp;
891 sframe_decoder_ctx *dctx;
892 char *frame_buf;
893 char *tempbuf = NULL;
894
895 int fidx_size;
896 uint32_t fre_bytes;
897 int foreign_endian = 0;
898
899 sframe_init_debug ();
900
901 if ((sf_buf == NULL) || (!sf_size))
902 return sframe_ret_set_errno (errp, SFRAME_ERR_INVAL);
903 else if (sf_size < sizeof (sframe_header))
904 return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
905
906 sfp = (const sframe_preamble *) sf_buf;
907
908 debug_printf ("sframe_decode: magic=0x%x version=%u flags=%u\n",
909 sfp->sfp_magic, sfp->sfp_version, sfp->sfp_flags);
910
911 /* Check for foreign endianness. */
912 if (sfp->sfp_magic != SFRAME_MAGIC)
913 {
914 if (sfp->sfp_magic == bswap_16 (SFRAME_MAGIC))
915 foreign_endian = 1;
916 else
917 return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
918 }
919
920 /* Initialize a new decoder context. */
921 if ((dctx = malloc (sizeof (sframe_decoder_ctx))) == NULL)
922 return sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
923 memset (dctx, 0, sizeof (sframe_decoder_ctx));
924
925 if (foreign_endian)
926 {
927 /* Allocate a new buffer and initialize it. */
928 tempbuf = (char *) malloc (sf_size * sizeof (char));
929 if (tempbuf == NULL)
930 return sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
931 memcpy (tempbuf, sf_buf, sf_size);
932
933 /* Flip the header. */
934 sframe_header *ihp = (sframe_header *) tempbuf;
935 flip_header (ihp);
936 /* Flip the rest of the SFrame section data buffer. */
937 if (flip_sframe (tempbuf, sf_size, 0))
938 {
939 free (tempbuf);
940 return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
941 }
942 frame_buf = tempbuf;
943 /* This buffer is malloc'd when endian flipping the contents of the input
944 buffer are needed. Keep a reference to it so it can be free'd up
945 later in sframe_decoder_free (). */
946 dctx->sfd_buf = tempbuf;
947 }
948 else
949 frame_buf = (char *)sf_buf;
950
951 /* Handle the SFrame header. */
952 dctx->sfd_header = *(sframe_header *) frame_buf;
953 /* Validate the contents of SFrame header. */
954 sfheaderp = &dctx->sfd_header;
955 if (!sframe_header_sanity_check_p (sfheaderp))
956 {
957 sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
958 goto decode_fail_free;
959 }
960 hdrsz = sframe_get_hdr_size (sfheaderp);
961 frame_buf += hdrsz;
962
963 /* Handle the SFrame Function Descriptor Entry section. */
964 fidx_size
965 = sfheaderp->sfh_num_fdes * sizeof (sframe_func_desc_entry);
966 dctx->sfd_funcdesc = malloc (fidx_size);
967 if (dctx->sfd_funcdesc == NULL)
968 {
969 sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
970 goto decode_fail_free;
971 }
972 /* SFrame FDEs are at an offset of sfh_fdeoff from SFrame header end. */
973 memcpy (dctx->sfd_funcdesc, frame_buf + sfheaderp->sfh_fdeoff, fidx_size);
974
975 debug_printf ("%u total fidx size\n", fidx_size);
976
977 /* Handle the SFrame Frame Row Entry section. */
978 dctx->sfd_fres = (char *) malloc (sfheaderp->sfh_fre_len);
979 if (dctx->sfd_fres == NULL)
980 {
981 sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
982 goto decode_fail_free;
983 }
984 /* SFrame FREs are at an offset of sfh_freoff from SFrame header end. */
985 memcpy (dctx->sfd_fres,
986 frame_buf + sfheaderp->sfh_freoff,
987 sfheaderp->sfh_fre_len);
988
989 fre_bytes = sfheaderp->sfh_fre_len;
990 dctx->sfd_fre_nbytes = fre_bytes;
991
992 debug_printf ("%u total fre bytes\n", fre_bytes);
993
994 return dctx;
995
996 decode_fail_free:
997 if (foreign_endian && tempbuf != NULL)
998 free (tempbuf);
999 sframe_decoder_free (&dctx);
1000 dctx = NULL;
1001 return dctx;
1002 }
1003
1004 /* Get the size of the SFrame header from the decoder context CTX. */
1005
1006 unsigned int
1007 sframe_decoder_get_hdr_size (sframe_decoder_ctx *ctx)
1008 {
1009 sframe_header *dhp;
1010 dhp = sframe_decoder_get_header (ctx);
1011 return sframe_get_hdr_size (dhp);
1012 }
1013
1014 /* Get the SFrame's abi/arch info given the decoder context DCTX. */
1015
1016 uint8_t
1017 sframe_decoder_get_abi_arch (sframe_decoder_ctx *dctx)
1018 {
1019 sframe_header *sframe_header;
1020 sframe_header = sframe_decoder_get_header (dctx);
1021 return sframe_header->sfh_abi_arch;
1022 }
1023
1024 /* Get the format version from the SFrame decoder context DCTX. */
1025
1026 uint8_t
1027 sframe_decoder_get_version (sframe_decoder_ctx *dctx)
1028 {
1029 sframe_header *dhp;
1030 dhp = sframe_decoder_get_header (dctx);
1031 return dhp->sfh_preamble.sfp_version;
1032 }
1033
1034 /* Get the section flags from the SFrame decoder context DCTX. */
1035
1036 uint8_t
1037 sframe_decoder_get_flags (sframe_decoder_ctx *dctx)
1038 {
1039 const sframe_header *dhp = sframe_decoder_get_header (dctx);
1040 return dhp->sfh_preamble.sfp_flags;
1041 }
1042
1043 /* Get the SFrame's fixed FP offset given the decoder context CTX. */
1044 int8_t
1045 sframe_decoder_get_fixed_fp_offset (sframe_decoder_ctx *ctx)
1046 {
1047 sframe_header *dhp;
1048 dhp = sframe_decoder_get_header (ctx);
1049 return dhp->sfh_cfa_fixed_fp_offset;
1050 }
1051
1052 /* Get the SFrame's fixed RA offset given the decoder context CTX. */
1053 int8_t
1054 sframe_decoder_get_fixed_ra_offset (sframe_decoder_ctx *ctx)
1055 {
1056 sframe_header *dhp;
1057 dhp = sframe_decoder_get_header (ctx);
1058 return dhp->sfh_cfa_fixed_ra_offset;
1059 }
1060
1061 /* Get the offset of the sfde_func_start_address field (from the start of the
1062 on-disk layout of the SFrame section) of the FDE at FUNC_IDX in the decoder
1063 context DCTX.
1064
1065 If FUNC_IDX is more than the number of SFrame FDEs in the section, sets
1066 error code in ERRP, but returns the (hypothetical) offset. This is useful
1067 for the linker when arranging input FDEs into the output section to be
1068 emitted. */
1069
1070 uint32_t
1071 sframe_decoder_get_offsetof_fde_start_addr (sframe_decoder_ctx *dctx,
1072 uint32_t func_idx, int *errp)
1073 {
1074 if (func_idx >= sframe_decoder_get_num_fidx (dctx))
1075 sframe_ret_set_errno (errp, SFRAME_ERR_FDE_NOTFOUND);
1076 else if (errp)
1077 *errp = 0;
1078
1079 return (sframe_decoder_get_hdr_size (dctx)
1080 + func_idx * sizeof (sframe_func_desc_entry)
1081 + offsetof (sframe_func_desc_entry, sfde_func_start_address));
1082 }
1083
1084 /* Find the function descriptor entry starting which contains the specified
1085 address ADDR. */
1086
1087 static sframe_func_desc_entry *
1088 sframe_get_funcdesc_with_addr_internal (sframe_decoder_ctx *ctx, int32_t addr,
1089 int *errp, uint32_t *func_idx)
1090 {
1091 sframe_header *dhp;
1092 sframe_func_desc_entry *fdp;
1093 int low, high;
1094
1095 if (ctx == NULL)
1096 return sframe_ret_set_errno (errp, SFRAME_ERR_INVAL);
1097
1098 dhp = sframe_decoder_get_header (ctx);
1099
1100 if (dhp == NULL || dhp->sfh_num_fdes == 0 || ctx->sfd_funcdesc == NULL)
1101 return sframe_ret_set_errno (errp, SFRAME_ERR_DCTX_INVAL);
1102 /* If the FDE sub-section is not sorted on PCs, skip the lookup because
1103 binary search cannot be used. */
1104 if ((sframe_decoder_get_flags (ctx) & SFRAME_F_FDE_SORTED) == 0)
1105 return sframe_ret_set_errno (errp, SFRAME_ERR_FDE_NOTSORTED);
1106
1107 /* Do the binary search. */
1108 fdp = (sframe_func_desc_entry *) ctx->sfd_funcdesc;
1109 low = 0;
1110 high = dhp->sfh_num_fdes - 1;
1111 while (low <= high)
1112 {
1113 int mid = low + (high - low) / 2;
1114
1115 /* Given sfde_func_start_address <= addr,
1116 addr - sfde_func_start_address must be positive. */
1117 if (sframe_decoder_get_secrel_func_start_addr (ctx, mid) <= addr
1118 && ((uint32_t)(addr - sframe_decoder_get_secrel_func_start_addr (ctx,
1119 mid))
1120 < fdp[mid].sfde_func_size))
1121 {
1122 *func_idx = mid;
1123 return fdp + mid;
1124 }
1125
1126 if (sframe_decoder_get_secrel_func_start_addr (ctx, mid) < addr)
1127 low = mid + 1;
1128 else
1129 high = mid - 1;
1130 }
1131
1132 return sframe_ret_set_errno (errp, SFRAME_ERR_FDE_NOTFOUND);
1133 }
1134
1135 /* Get the end IP offset for the FRE at index i in the FDEP. The buffer FRES
1136 is the starting location for the FRE. */
1137
1138 static uint32_t
1139 sframe_fre_get_end_ip_offset (sframe_func_desc_entry *fdep, unsigned int i,
1140 const char *fres)
1141 {
1142 uint32_t end_ip_offset;
1143 uint32_t fre_type;
1144
1145 fre_type = sframe_get_fre_type (fdep);
1146
1147 /* Get the start address of the next FRE in sequence. */
1148 if (i < fdep->sfde_func_num_fres - 1)
1149 {
1150 sframe_decode_fre_start_address (fres, &end_ip_offset, fre_type);
1151 end_ip_offset -= 1;
1152 }
1153 else
1154 /* The end IP offset for the FRE needs to be deduced from the function
1155 size. */
1156 end_ip_offset = fdep->sfde_func_size - 1;
1157
1158 return end_ip_offset;
1159 }
1160
1161 /* Find the SFrame Row Entry which contains the PC. Returns
1162 SFRAME_ERR if failure. */
1163
1164 int
1165 sframe_find_fre (sframe_decoder_ctx *ctx, int32_t pc,
1166 sframe_frame_row_entry *frep)
1167 {
1168 sframe_frame_row_entry cur_fre;
1169 sframe_func_desc_entry *fdep;
1170 uint32_t func_idx;
1171 uint32_t fre_type, i;
1172 int32_t func_start_addr;
1173 uint32_t start_ip_offset, end_ip_offset;
1174 const char *fres;
1175 size_t size = 0;
1176 int err = 0;
1177
1178 if ((ctx == NULL) || (frep == NULL))
1179 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1180
1181 /* Find the FDE which contains the PC, then scan its fre entries. */
1182 fdep = sframe_get_funcdesc_with_addr_internal (ctx, pc, &err, &func_idx);
1183 if (fdep == NULL || ctx->sfd_fres == NULL)
1184 return sframe_set_errno (&err, SFRAME_ERR_DCTX_INVAL);
1185
1186 fre_type = sframe_get_fre_type (fdep);
1187
1188 fres = ctx->sfd_fres + fdep->sfde_func_start_fre_off;
1189 func_start_addr = sframe_decoder_get_secrel_func_start_addr (ctx, func_idx);
1190
1191 for (i = 0; i < fdep->sfde_func_num_fres; i++)
1192 {
1193 err = sframe_decode_fre (fres, &cur_fre, fre_type, &size);
1194 if (err)
1195 return sframe_set_errno (&err, SFRAME_ERR_FRE_INVAL);
1196
1197 start_ip_offset = cur_fre.fre_start_addr;
1198 end_ip_offset = sframe_fre_get_end_ip_offset (fdep, i, fres + size);
1199
1200 /* Stop search if FRE's start_ip is greater than pc. Given
1201 func_start_addr <= pc, pc - func_start_addr must be positive. */
1202 if (start_ip_offset > (uint32_t)(pc - func_start_addr))
1203 return sframe_set_errno (&err, SFRAME_ERR_FRE_INVAL);
1204
1205 if (sframe_fre_check_range_p (ctx, func_idx, start_ip_offset,
1206 end_ip_offset, pc))
1207 {
1208 sframe_frame_row_entry_copy (frep, &cur_fre);
1209 return 0;
1210 }
1211 fres += size;
1212 }
1213 return sframe_set_errno (&err, SFRAME_ERR_FDE_INVAL);
1214 }
1215
1216 /* Return the number of function descriptor entries in the SFrame decoder
1217 DCTX. */
1218
1219 uint32_t
1220 sframe_decoder_get_num_fidx (sframe_decoder_ctx *ctx)
1221 {
1222 uint32_t num_fdes = 0;
1223 sframe_header *dhp = NULL;
1224 dhp = sframe_decoder_get_header (ctx);
1225 if (dhp)
1226 num_fdes = dhp->sfh_num_fdes;
1227 return num_fdes;
1228 }
1229
1230 /* Get the data (NUM_FRES, FUNC_START_ADDRESS) from the function
1231 descriptor entry at index I'th in the decoder CTX. If failed,
1232 return error code. */
1233 /* FIXME - consolidate the args and return a
1234 sframe_func_desc_index_elem rather? */
1235
1236 int
1237 sframe_decoder_get_funcdesc (sframe_decoder_ctx *ctx,
1238 unsigned int i,
1239 uint32_t *num_fres,
1240 uint32_t *func_size,
1241 int32_t *func_start_address,
1242 unsigned char *func_info)
1243 {
1244 sframe_func_desc_entry *fdp;
1245 int err = 0;
1246
1247 if (ctx == NULL || func_start_address == NULL || num_fres == NULL
1248 || func_size == NULL)
1249 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1250
1251 fdp = sframe_decoder_get_funcdesc_at_index (ctx, i);
1252
1253 if (fdp == NULL)
1254 return sframe_set_errno (&err, SFRAME_ERR_FDE_NOTFOUND);
1255
1256 *num_fres = fdp->sfde_func_num_fres;
1257 *func_start_address = fdp->sfde_func_start_address;
1258 *func_size = fdp->sfde_func_size;
1259 *func_info = fdp->sfde_func_info;
1260
1261 return 0;
1262 }
1263
1264 int
1265 sframe_decoder_get_funcdesc_v2 (sframe_decoder_ctx *dctx,
1266 unsigned int i,
1267 uint32_t *num_fres,
1268 uint32_t *func_size,
1269 int32_t *func_start_address,
1270 unsigned char *func_info,
1271 uint8_t *rep_block_size)
1272 {
1273 sframe_func_desc_entry *fdp;
1274 int err = 0;
1275
1276 if (dctx == NULL || func_start_address == NULL
1277 || num_fres == NULL || func_size == NULL
1278 || sframe_decoder_get_version (dctx) == SFRAME_VERSION_1)
1279 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1280
1281 fdp = sframe_decoder_get_funcdesc_at_index (dctx, i);
1282
1283 if (fdp == NULL)
1284 return sframe_set_errno (&err, SFRAME_ERR_FDE_NOTFOUND);
1285
1286 *num_fres = fdp->sfde_func_num_fres;
1287 *func_start_address = fdp->sfde_func_start_address;
1288 *func_size = fdp->sfde_func_size;
1289 *func_info = fdp->sfde_func_info;
1290 *rep_block_size = fdp->sfde_func_rep_size;
1291
1292 return 0;
1293 }
1294 /* Get the FRE_IDX'th FRE of the function at FUNC_IDX'th function
1295 descriptor entry in the SFrame decoder CTX. Returns error code as
1296 applicable. */
1297
1298 int
1299 sframe_decoder_get_fre (sframe_decoder_ctx *ctx,
1300 unsigned int func_idx,
1301 unsigned int fre_idx,
1302 sframe_frame_row_entry *fre)
1303 {
1304 sframe_func_desc_entry *fdep;
1305 sframe_frame_row_entry ifre;
1306 const char *fres;
1307 uint32_t i;
1308 uint32_t fre_type;
1309 size_t esz = 0;
1310 int err = 0;
1311
1312 if (ctx == NULL || fre == NULL)
1313 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1314
1315 /* Get function descriptor entry at index func_idx. */
1316 fdep = sframe_decoder_get_funcdesc_at_index (ctx, func_idx);
1317
1318 if (fdep == NULL)
1319 return sframe_set_errno (&err, SFRAME_ERR_FDE_NOTFOUND);
1320
1321 fre_type = sframe_get_fre_type (fdep);
1322 /* Now scan the FRE entries. */
1323 fres = ctx->sfd_fres + fdep->sfde_func_start_fre_off;
1324 for (i = 0; i < fdep->sfde_func_num_fres; i++)
1325 {
1326 /* Decode the FRE at the current position. Return it if valid. */
1327 err = sframe_decode_fre (fres, &ifre, fre_type, &esz);
1328 if (i == fre_idx)
1329 {
1330 if (!sframe_fre_sanity_check_p (&ifre))
1331 return sframe_set_errno (&err, SFRAME_ERR_FRE_INVAL);
1332
1333 /* Although a stricter sanity check on fre_start_addr like:
1334 if (fdep->sfde_func_size)
1335 sframe_assert (frep->fre_start_addr < fdep->sfde_func_size);
1336 is more suitable, some code has been seen to not abide by it. See
1337 PR libsframe/33131. */
1338 sframe_assert (ifre.fre_start_addr <= fdep->sfde_func_size);
1339
1340 sframe_frame_row_entry_copy (fre, &ifre);
1341
1342 return 0;
1343 }
1344 /* Next FRE. */
1345 fres += esz;
1346 }
1347
1348 return sframe_set_errno (&err, SFRAME_ERR_FDE_NOTFOUND);
1349 }
1350
1351
1352 /* SFrame Encoder. */
1353
1354 /* Get a reference to the ENCODER's SFrame header. */
1355
1356 static sframe_header *
1357 sframe_encoder_get_header (sframe_encoder_ctx *encoder)
1358 {
1359 sframe_header *hp = NULL;
1360 if (encoder)
1361 hp = &encoder->sfe_header;
1362 return hp;
1363 }
1364
1365 static sframe_func_desc_entry *
1366 sframe_encoder_get_funcdesc_at_index (sframe_encoder_ctx *encoder,
1367 uint32_t func_idx)
1368 {
1369 sframe_func_desc_entry *fde = NULL;
1370 if (func_idx < sframe_encoder_get_num_fidx (encoder))
1371 {
1372 sf_fde_tbl *func_tbl = encoder->sfe_funcdesc;
1373 fde = func_tbl->entry + func_idx;
1374 }
1375 return fde;
1376 }
1377
1378 /* Create an encoder context with the given SFrame format version VER, FLAGS
1379 and ABI information. Uses the ABI specific FIXED_FP_OFFSET and
1380 FIXED_RA_OFFSET values as provided. Sets errp if failure. */
1381
1382 sframe_encoder_ctx *
1383 sframe_encode (uint8_t ver, uint8_t flags, uint8_t abi_arch,
1384 int8_t fixed_fp_offset, int8_t fixed_ra_offset, int *errp)
1385 {
1386 sframe_header *hp;
1387 sframe_encoder_ctx *encoder;
1388
1389 if (ver != SFRAME_VERSION)
1390 return sframe_ret_set_errno (errp, SFRAME_ERR_VERSION_INVAL);
1391
1392 if ((encoder = malloc (sizeof (sframe_encoder_ctx))) == NULL)
1393 return sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
1394
1395 memset (encoder, 0, sizeof (sframe_encoder_ctx));
1396
1397 /* Get the SFrame header and update it. */
1398 hp = sframe_encoder_get_header (encoder);
1399 hp->sfh_preamble.sfp_version = ver;
1400 hp->sfh_preamble.sfp_magic = SFRAME_MAGIC;
1401 hp->sfh_preamble.sfp_flags = flags;
1402
1403 /* Implementation in the SFrame encoder APIs, e.g.,
1404 sframe_encoder_write_sframe assume flag SFRAME_F_FDE_FUNC_START_PCREL
1405 set. */
1406 if (!(flags & SFRAME_F_FDE_FUNC_START_PCREL))
1407 return sframe_ret_set_errno (errp, SFRAME_ERR_ECTX_INVAL);
1408
1409 hp->sfh_abi_arch = abi_arch;
1410 hp->sfh_cfa_fixed_fp_offset = fixed_fp_offset;
1411 hp->sfh_cfa_fixed_ra_offset = fixed_ra_offset;
1412
1413 return encoder;
1414 }
1415
1416 /* Free the encoder context. */
1417
1418 void
1419 sframe_encoder_free (sframe_encoder_ctx **encoder)
1420 {
1421 if (encoder != NULL)
1422 {
1423 sframe_encoder_ctx *ectx = *encoder;
1424 if (ectx == NULL)
1425 return;
1426
1427 if (ectx->sfe_funcdesc != NULL)
1428 {
1429 free (ectx->sfe_funcdesc);
1430 ectx->sfe_funcdesc = NULL;
1431 }
1432 if (ectx->sfe_fres != NULL)
1433 {
1434 free (ectx->sfe_fres);
1435 ectx->sfe_fres = NULL;
1436 }
1437 if (ectx->sfe_data != NULL)
1438 {
1439 free (ectx->sfe_data);
1440 ectx->sfe_data = NULL;
1441 }
1442
1443 free (*encoder);
1444 *encoder = NULL;
1445 }
1446 }
1447
1448 /* Get the size of the SFrame header from the encoder ctx ENCODER. */
1449
1450 unsigned int
1451 sframe_encoder_get_hdr_size (sframe_encoder_ctx *encoder)
1452 {
1453 sframe_header *ehp;
1454 ehp = sframe_encoder_get_header (encoder);
1455 return sframe_get_hdr_size (ehp);
1456 }
1457
1458 /* Get the abi/arch info from the SFrame encoder context ENCODER. */
1459
1460 uint8_t
1461 sframe_encoder_get_abi_arch (sframe_encoder_ctx *encoder)
1462 {
1463 uint8_t abi_arch = 0;
1464 sframe_header *ehp;
1465 ehp = sframe_encoder_get_header (encoder);
1466 if (ehp)
1467 abi_arch = ehp->sfh_abi_arch;
1468 return abi_arch;
1469 }
1470
1471 /* Get the format version from the SFrame encoder context ENCODER. */
1472
1473 uint8_t
1474 sframe_encoder_get_version (sframe_encoder_ctx *encoder)
1475 {
1476 sframe_header *ehp;
1477 ehp = sframe_encoder_get_header (encoder);
1478 return ehp->sfh_preamble.sfp_version;
1479 }
1480
1481 /* Get the section flags from the SFrame encoder context ENCODER. */
1482
1483 uint8_t
1484 sframe_encoder_get_flags (sframe_encoder_ctx *encoder)
1485 {
1486 const sframe_header *ehp = sframe_encoder_get_header (encoder);
1487 return ehp->sfh_preamble.sfp_flags;
1488 }
1489
1490 /* Return the number of function descriptor entries in the SFrame encoder
1491 ENCODER. */
1492
1493 uint32_t
1494 sframe_encoder_get_num_fidx (sframe_encoder_ctx *encoder)
1495 {
1496 uint32_t num_fdes = 0;
1497 sframe_header *ehp = NULL;
1498 ehp = sframe_encoder_get_header (encoder);
1499 if (ehp)
1500 num_fdes = ehp->sfh_num_fdes;
1501 return num_fdes;
1502 }
1503
1504 /* Get the offset of the sfde_func_start_address field (from the start of the
1505 on-disk layout of the SFrame section) of the FDE at FUNC_IDX in the encoder
1506 context ENCODER.
1507
1508 If FUNC_IDX is more than the number of SFrame FDEs in the section, sets
1509 error code in ERRP, but returns the (hypothetical) offset. This is useful
1510 for the linker when arranging input FDEs into the output section to be
1511 emitted. */
1512
1513 uint32_t
1514 sframe_encoder_get_offsetof_fde_start_addr (sframe_encoder_ctx *encoder,
1515 uint32_t func_idx, int *errp)
1516 {
1517 if (func_idx >= sframe_encoder_get_num_fidx (encoder))
1518 sframe_ret_set_errno (errp, SFRAME_ERR_FDE_INVAL);
1519 else if (errp)
1520 *errp = 0;
1521
1522 return (sframe_encoder_get_hdr_size (encoder)
1523 + func_idx * sizeof (sframe_func_desc_entry)
1524 + offsetof (sframe_func_desc_entry, sfde_func_start_address));
1525 }
1526
1527 /* Add an FRE to function at FUNC_IDX'th function descriptor entry in
1528 the encoder context. */
1529
1530 int
1531 sframe_encoder_add_fre (sframe_encoder_ctx *encoder,
1532 unsigned int func_idx,
1533 sframe_frame_row_entry *frep)
1534 {
1535 sframe_header *ehp;
1536 sframe_func_desc_entry *fdep;
1537 sframe_frame_row_entry *ectx_frep;
1538 size_t offsets_sz, esz;
1539 uint32_t fre_type;
1540 size_t fre_tbl_sz;
1541 int err = 0;
1542
1543 if (encoder == NULL || frep == NULL)
1544 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1545 if (!sframe_fre_sanity_check_p (frep))
1546 return sframe_set_errno (&err, SFRAME_ERR_FRE_INVAL);
1547
1548 /* Use func_idx to gather the function descriptor entry. */
1549 fdep = sframe_encoder_get_funcdesc_at_index (encoder, func_idx);
1550
1551 if (fdep == NULL)
1552 return sframe_set_errno (&err, SFRAME_ERR_FDE_NOTFOUND);
1553
1554 fre_type = sframe_get_fre_type (fdep);
1555 sf_fre_tbl *fre_tbl = encoder->sfe_fres;
1556
1557 if (fre_tbl == NULL)
1558 {
1559 fre_tbl_sz = (sizeof (sf_fre_tbl)
1560 + (number_of_entries * sizeof (sframe_frame_row_entry)));
1561 fre_tbl = malloc (fre_tbl_sz);
1562
1563 if (fre_tbl == NULL)
1564 {
1565 sframe_set_errno (&err, SFRAME_ERR_NOMEM);
1566 goto bad; /* OOM. */
1567 }
1568 memset (fre_tbl, 0, fre_tbl_sz);
1569 fre_tbl->alloced = number_of_entries;
1570 }
1571 else if (fre_tbl->count == fre_tbl->alloced)
1572 {
1573 fre_tbl_sz = (sizeof (sf_fre_tbl)
1574 + ((fre_tbl->alloced + number_of_entries)
1575 * sizeof (sframe_frame_row_entry)));
1576 fre_tbl = realloc (fre_tbl, fre_tbl_sz);
1577 if (fre_tbl == NULL)
1578 {
1579 sframe_set_errno (&err, SFRAME_ERR_NOMEM);
1580 goto bad; /* OOM. */
1581 }
1582
1583 memset (&fre_tbl->entry[fre_tbl->alloced], 0,
1584 number_of_entries * sizeof (sframe_frame_row_entry));
1585 fre_tbl->alloced += number_of_entries;
1586 }
1587
1588 ectx_frep = &fre_tbl->entry[fre_tbl->count];
1589 ectx_frep->fre_start_addr
1590 = frep->fre_start_addr;
1591 ectx_frep->fre_info = frep->fre_info;
1592
1593 /* Although a stricter sanity check on fre_start_addr like:
1594 if (fdep->sfde_func_size)
1595 sframe_assert (frep->fre_start_addr < fdep->sfde_func_size);
1596 is more suitable, some code has been seen to not abide by it. See PR
1597 libsframe/33131. */
1598 sframe_assert (frep->fre_start_addr <= fdep->sfde_func_size);
1599
1600 /* frep has already been sanity check'd. Get offsets size. */
1601 offsets_sz = sframe_fre_offset_bytes_size (frep->fre_info);
1602 memcpy (&ectx_frep->fre_offsets, &frep->fre_offsets, offsets_sz);
1603
1604 esz = sframe_fre_entry_size (frep, fre_type);
1605 fre_tbl->count++;
1606
1607 encoder->sfe_fres = fre_tbl;
1608 encoder->sfe_fre_nbytes += esz;
1609
1610 ehp = sframe_encoder_get_header (encoder);
1611 ehp->sfh_num_fres = fre_tbl->count;
1612
1613 /* Update the value of the number of FREs for the function. */
1614 fdep->sfde_func_num_fres++;
1615
1616 return 0;
1617
1618 bad:
1619 if (fre_tbl != NULL)
1620 free (fre_tbl);
1621 encoder->sfe_fres = NULL;
1622 encoder->sfe_fre_nbytes = 0;
1623 return -1;
1624 }
1625
1626 /* Add a new function descriptor entry with START_ADDR, FUNC_SIZE and NUM_FRES
1627 to the encoder. */
1628
1629 int
1630 sframe_encoder_add_funcdesc (sframe_encoder_ctx *encoder,
1631 int32_t start_addr,
1632 uint32_t func_size,
1633 unsigned char func_info,
1634 uint32_t num_fres ATTRIBUTE_UNUSED)
1635 {
1636 sframe_header *ehp;
1637 sf_fde_tbl *fd_info;
1638 size_t fd_tbl_sz;
1639 int err = 0;
1640
1641 /* FIXME book-keep num_fres for error checking. */
1642 if (encoder == NULL)
1643 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1644
1645 fd_info = encoder->sfe_funcdesc;
1646 ehp = sframe_encoder_get_header (encoder);
1647
1648 if (fd_info == NULL)
1649 {
1650 fd_tbl_sz = (sizeof (sf_fde_tbl)
1651 + (number_of_entries * sizeof (sframe_func_desc_entry)));
1652 fd_info = malloc (fd_tbl_sz);
1653 if (fd_info == NULL)
1654 {
1655 sframe_set_errno (&err, SFRAME_ERR_NOMEM);
1656 goto bad; /* OOM. */
1657 }
1658 memset (fd_info, 0, fd_tbl_sz);
1659 fd_info->alloced = number_of_entries;
1660 }
1661 else if (fd_info->count == fd_info->alloced)
1662 {
1663 fd_tbl_sz = (sizeof (sf_fde_tbl)
1664 + ((fd_info->alloced + number_of_entries)
1665 * sizeof (sframe_func_desc_entry)));
1666 fd_info = realloc (fd_info, fd_tbl_sz);
1667 if (fd_info == NULL)
1668 {
1669 sframe_set_errno (&err, SFRAME_ERR_NOMEM);
1670 goto bad; /* OOM. */
1671 }
1672
1673 memset (&fd_info->entry[fd_info->alloced], 0,
1674 number_of_entries * sizeof (sframe_func_desc_entry));
1675 fd_info->alloced += number_of_entries;
1676 }
1677
1678 fd_info->entry[fd_info->count].sfde_func_start_address = start_addr;
1679 /* Num FREs is updated as FREs are added for the function later via
1680 sframe_encoder_add_fre. */
1681 fd_info->entry[fd_info->count].sfde_func_size = func_size;
1682 fd_info->entry[fd_info->count].sfde_func_start_fre_off
1683 = encoder->sfe_fre_nbytes;
1684 #if 0
1685 // Linker optimization test code cleanup later ibhagat TODO FIXME
1686 uint32_t fre_type = sframe_calc_fre_type (func_size);
1687
1688 fd_info->entry[fd_info->count].sfde_func_info
1689 = sframe_fde_func_info (fre_type);
1690 #endif
1691 fd_info->entry[fd_info->count].sfde_func_info = func_info;
1692 fd_info->count++;
1693 encoder->sfe_funcdesc = fd_info;
1694 ehp->sfh_num_fdes++;
1695 return 0;
1696
1697 bad:
1698 if (fd_info != NULL)
1699 free (fd_info);
1700 encoder->sfe_funcdesc = NULL;
1701 ehp->sfh_num_fdes = 0;
1702 return -1;
1703 }
1704
1705 /* Add a new function descriptor entry with START_ADDR, FUNC_SIZE, FUNC_INFO
1706 and REP_BLOCK_SIZE to the encoder.
1707
1708 This API is valid only for SFrame format version 2. */
1709
1710 int
1711 sframe_encoder_add_funcdesc_v2 (sframe_encoder_ctx *encoder,
1712 int32_t start_addr,
1713 uint32_t func_size,
1714 unsigned char func_info,
1715 uint8_t rep_block_size,
1716 uint32_t num_fres ATTRIBUTE_UNUSED)
1717 {
1718 sf_fde_tbl *fd_info;
1719 int err;
1720
1721 if (encoder == NULL
1722 || sframe_encoder_get_version (encoder) == SFRAME_VERSION_1)
1723 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1724
1725 err = sframe_encoder_add_funcdesc (encoder, start_addr, func_size, func_info,
1726 num_fres);
1727 if (err)
1728 return SFRAME_ERR;
1729
1730 fd_info = encoder->sfe_funcdesc;
1731 fd_info->entry[fd_info->count-1].sfde_func_rep_size = rep_block_size;
1732
1733 return 0;
1734 }
1735
1736 static int
1737 sframe_sort_funcdesc (sframe_encoder_ctx *encoder)
1738 {
1739 sframe_header *ehp = sframe_encoder_get_header (encoder);
1740
1741 /* Sort and write out the FDE table. */
1742 sf_fde_tbl *fd_info = encoder->sfe_funcdesc;
1743 if (fd_info)
1744 {
1745 /* The new encoding of sfde_func_start_address means the distances are
1746 not from the same anchor, so cannot be sorted directly. At the moment
1747 we adress this by manual value adjustments before and after sorting.
1748 FIXME - qsort_r may be more optimal. */
1749
1750 for (unsigned int i = 0; i < fd_info->count; i++)
1751 fd_info->entry[i].sfde_func_start_address
1752 += sframe_encoder_get_offsetof_fde_start_addr (encoder, i, NULL);
1753
1754 qsort (fd_info->entry, fd_info->count,
1755 sizeof (sframe_func_desc_entry), fde_func);
1756
1757 for (unsigned int i = 0; i < fd_info->count; i++)
1758 fd_info->entry[i].sfde_func_start_address
1759 -= sframe_encoder_get_offsetof_fde_start_addr (encoder, i, NULL);
1760
1761 /* Update preamble's flags. */
1762 ehp->sfh_preamble.sfp_flags |= SFRAME_F_FDE_SORTED;
1763 }
1764 return 0;
1765 }
1766
1767 /* Write the SFrame FRE start address from the in-memory FRE_START_ADDR
1768 to the buffer CONTENTS (on-disk format), given the FRE_TYPE and
1769 FRE_START_ADDR_SZ. */
1770
1771 static int
1772 sframe_encoder_write_fre_start_addr (char *contents,
1773 uint32_t fre_start_addr,
1774 uint32_t fre_type,
1775 size_t fre_start_addr_sz)
1776 {
1777 int err = 0;
1778
1779 if (fre_type == SFRAME_FRE_TYPE_ADDR1)
1780 {
1781 uint8_t uc = fre_start_addr;
1782 memcpy (contents, &uc, fre_start_addr_sz);
1783 }
1784 else if (fre_type == SFRAME_FRE_TYPE_ADDR2)
1785 {
1786 uint16_t ust = fre_start_addr;
1787 memcpy (contents, &ust, fre_start_addr_sz);
1788 }
1789 else if (fre_type == SFRAME_FRE_TYPE_ADDR4)
1790 {
1791 uint32_t uit = fre_start_addr;
1792 memcpy (contents, &uit, fre_start_addr_sz);
1793 }
1794 else
1795 return sframe_set_errno (&err, SFRAME_ERR_INVAL);
1796
1797 return 0;
1798 }
1799
1800 /* Write a frame row entry pointed to by FREP into the buffer CONTENTS. The
1801 size in bytes written out are updated in ESZ.
1802
1803 This function works closely with the SFrame binary format.
1804
1805 Returns SFRAME_ERR if failure. */
1806
1807 static int
1808 sframe_encoder_write_fre (char *contents, sframe_frame_row_entry *frep,
1809 uint32_t fre_type, size_t *esz)
1810 {
1811 size_t fre_sz;
1812 size_t fre_start_addr_sz;
1813 size_t fre_stack_offsets_sz;
1814 int err = 0;
1815
1816 if (!sframe_fre_sanity_check_p (frep))
1817 return sframe_set_errno (&err, SFRAME_ERR_FRE_INVAL);
1818
1819 fre_start_addr_sz = sframe_fre_start_addr_size (fre_type);
1820 fre_stack_offsets_sz = sframe_fre_offset_bytes_size (frep->fre_info);
1821
1822 /* The FRE start address must be encodable in the available number of
1823 bytes. */
1824 uint64_t bitmask = SFRAME_BITMASK_OF_SIZE (fre_start_addr_sz);
1825 sframe_assert ((uint64_t)frep->fre_start_addr <= bitmask);
1826
1827 sframe_encoder_write_fre_start_addr (contents, frep->fre_start_addr,
1828 fre_type, fre_start_addr_sz);
1829 contents += fre_start_addr_sz;
1830
1831 memcpy (contents, &frep->fre_info, sizeof (frep->fre_info));
1832 contents += sizeof (frep->fre_info);
1833
1834 memcpy (contents, frep->fre_offsets, fre_stack_offsets_sz);
1835 contents+= fre_stack_offsets_sz;
1836
1837 fre_sz = sframe_fre_entry_size (frep, fre_type);
1838 /* Sanity checking. */
1839 sframe_assert ((fre_start_addr_sz
1840 + sizeof (frep->fre_info)
1841 + fre_stack_offsets_sz) == fre_sz);
1842
1843 *esz = fre_sz;
1844
1845 return 0;
1846 }
1847
1848 /* Serialize the core contents of the SFrame section and write out to the
1849 output buffer held in the ENCODER. Return SFRAME_ERR if failure. */
1850
1851 static int
1852 sframe_encoder_write_sframe (sframe_encoder_ctx *encoder)
1853 {
1854 char *contents;
1855 size_t buf_size;
1856 size_t hdr_size;
1857 size_t all_fdes_size;
1858 size_t fre_size;
1859 size_t esz = 0;
1860 sframe_header *ehp;
1861 sf_fde_tbl *fd_info;
1862 sf_fre_tbl *fr_info;
1863 uint32_t i, num_fdes;
1864 uint32_t j, num_fres;
1865 sframe_func_desc_entry *fdep;
1866 sframe_frame_row_entry *frep;
1867
1868 uint32_t fre_type;
1869 int err = 0;
1870
1871 contents = encoder->sfe_data;
1872 buf_size = encoder->sfe_data_size;
1873 num_fdes = sframe_encoder_get_num_fidx (encoder);
1874 all_fdes_size = num_fdes * sizeof (sframe_func_desc_entry);
1875 ehp = sframe_encoder_get_header (encoder);
1876 hdr_size = sframe_get_hdr_size (ehp);
1877
1878 fd_info = encoder->sfe_funcdesc;
1879 fr_info = encoder->sfe_fres;
1880
1881 /* Sanity checks:
1882 - buffers must be malloc'd by the caller. */
1883 if ((contents == NULL) || (buf_size < hdr_size))
1884 return sframe_set_errno (&err, SFRAME_ERR_BUF_INVAL);
1885 if (fr_info == NULL)
1886 return sframe_set_errno (&err, SFRAME_ERR_FRE_INVAL);
1887
1888 /* Write out the FRE table first.
1889
1890 Recall that read/write of FREs needs information from the corresponding
1891 FDE; the latter stores the information about the FRE type record used for
1892 the function. Also note that sorting of FDEs does NOT impact the order
1893 in which FREs are stored in the SFrame's FRE sub-section. This means
1894 that writing out FREs after sorting of FDEs will need some additional
1895 book-keeping. At this time, we can afford to avoid it by writing out
1896 the FREs first to the output buffer. */
1897 fre_size = 0;
1898 uint32_t global = 0;
1899 uint32_t fre_index = 0;
1900
1901 contents += hdr_size + all_fdes_size;
1902 for (i = 0; i < num_fdes; i++)
1903 {
1904 fdep = &fd_info->entry[i];
1905 fre_type = sframe_get_fre_type (fdep);
1906 num_fres = fdep->sfde_func_num_fres;
1907
1908 for (j = 0; j < num_fres; j++)
1909 {
1910 fre_index = global + j;
1911 frep = &fr_info->entry[fre_index];
1912
1913 sframe_encoder_write_fre (contents, frep, fre_type, &esz);
1914 contents += esz;
1915 fre_size += esz; /* For debugging only. */
1916 }
1917 global += j;
1918 }
1919
1920 sframe_assert (fre_size == ehp->sfh_fre_len);
1921 sframe_assert (global == ehp->sfh_num_fres);
1922 sframe_assert ((size_t)(contents - encoder->sfe_data) == buf_size);
1923
1924 /* Sort the FDE table */
1925 sframe_sort_funcdesc (encoder);
1926
1927 /* Sanity checks:
1928 - the FDE section must have been sorted by now on the start address
1929 of each function. */
1930 if (!(sframe_encoder_get_flags (encoder) & SFRAME_F_FDE_SORTED)
1931 || (fd_info == NULL))
1932 return sframe_set_errno (&err, SFRAME_ERR_FDE_INVAL);
1933
1934 contents = encoder->sfe_data;
1935 /* Write out the SFrame header. The SFrame header in the encoder
1936 object has already been updated with correct offsets by the caller. */
1937 memcpy (contents, ehp, hdr_size);
1938 contents += hdr_size;
1939
1940 /* Write out the FDE table sorted on funtion start address. */
1941 memcpy (contents, fd_info->entry, all_fdes_size);
1942 contents += all_fdes_size;
1943
1944 return 0;
1945 }
1946
1947 /* Serialize the contents of the encoder and return the buffer. ENCODED_SIZE
1948 is updated to the size of the buffer. */
1949
1950 char *
1951 sframe_encoder_write (sframe_encoder_ctx *encoder,
1952 size_t *encoded_size, int *errp)
1953 {
1954 sframe_header *ehp;
1955 size_t hdrsize, fsz, fresz, bufsize;
1956 int foreign_endian;
1957
1958 /* Initialize the encoded_size to zero. This makes it simpler to just
1959 return from the function in case of failure. Free'ing up of
1960 encoder->sfe_data is the responsibility of the caller. */
1961 *encoded_size = 0;
1962
1963 if (encoder == NULL || encoded_size == NULL || errp == NULL)
1964 return sframe_ret_set_errno (errp, SFRAME_ERR_INVAL);
1965
1966 ehp = sframe_encoder_get_header (encoder);
1967 hdrsize = sframe_get_hdr_size (ehp);
1968 fsz = sframe_encoder_get_num_fidx (encoder)
1969 * sizeof (sframe_func_desc_entry);
1970 fresz = encoder->sfe_fre_nbytes;
1971
1972 /* The total size of buffer is the sum of header, SFrame Function Descriptor
1973 Entries section and the FRE section. */
1974 bufsize = hdrsize + fsz + fresz;
1975 encoder->sfe_data = (char *) malloc (bufsize);
1976 if (encoder->sfe_data == NULL)
1977 return sframe_ret_set_errno (errp, SFRAME_ERR_NOMEM);
1978 encoder->sfe_data_size = bufsize;
1979
1980 /* Update the information in the SFrame header. */
1981 /* SFrame FDE section follows immediately after the header. */
1982 ehp->sfh_fdeoff = 0;
1983 /* SFrame FRE section follows immediately after the SFrame FDE section. */
1984 ehp->sfh_freoff = fsz;
1985 ehp->sfh_fre_len = fresz;
1986
1987 foreign_endian = need_swapping (ehp->sfh_abi_arch);
1988
1989 /* Write out the FDE Index and the FRE table in the sfe_data. */
1990 if (sframe_encoder_write_sframe (encoder))
1991 return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
1992
1993 /* Endian flip the contents if necessary. */
1994 if (foreign_endian)
1995 {
1996 if (flip_sframe (encoder->sfe_data, bufsize, 1))
1997 return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
1998 flip_header ((sframe_header*)encoder->sfe_data);
1999 }
2000
2001 *encoded_size = bufsize;
2002 return encoder->sfe_data;
2003 }