Update year range in copyright notice of binutils files
[binutils-gdb.git] / libsframe / testsuite / libsframe.decode / be-flipping.c
blobe7c7143482f88405e0649415b9e64ee79468006c
1 /* be-flipping.c -- Test for handling different endianness in libsframe.
3 Copyright (C) 2022-2024 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "config.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/stat.h>
24 #include "sframe-api.h"
26 /* DejaGnu should not use gnulib's vsnprintf replacement here. */
27 #undef vsnprintf
28 #include <dejagnu.h>
30 /* SFrame info from the following source (1 fde 5 fres):
31 static int cnt;
32 extern void foo (void);
34 int bar()
36 cnt++;
37 if (cnt == 3)
38 foo();
39 return (cnt);
41 gcc -mbig-endian -Wa,--gsframe -c -O3 t.c
42 objcopy --dump-section .sframe=DATA-BE t.o
44 #define DATA "DATA-BE"
46 int
47 main (void)
49 sframe_decoder_ctx *dctx = NULL;
50 uint32_t nfres, fsize;
51 int32_t fstart;
52 unsigned char finfo;
53 int err = 0;
54 FILE *fp;
55 struct stat st;
56 char *sf_buf;
57 size_t sf_size;
59 #define TEST(name, cond) \
60 do \
61 { \
62 if (cond) \
63 pass (name); \
64 else \
65 fail (name); \
66 } \
67 while (0)
69 /* Test setup. */
70 fp = fopen (DATA, "r");
71 if (fp == NULL)
72 goto setup_fail;
73 if (fstat (fileno (fp), &st) < 0)
75 perror ("fstat");
76 fclose (fp);
77 goto setup_fail;
79 sf_buf = malloc (st.st_size);
80 if (sf_buf == NULL)
82 perror ("malloc");
83 goto setup_fail;
85 sf_size = fread (sf_buf, 1, st.st_size, fp);
86 fclose (fp);
87 if (sf_size == 0)
89 fprintf (stderr, "Decode: Read buffer failed\n");
90 goto setup_fail;
93 /* Execute tests. */
95 /* Call to sframe_decode will endian flip the input buffer (big-endian) if
96 the host running the test is a little-endian system. This endian-flipped
97 copy of the buffer is kept internally in dctx. */
98 dctx = sframe_decode (sf_buf, sf_size, &err);
99 TEST ("be-flipping: Decoder setup", dctx != NULL);
101 unsigned int fde_cnt = sframe_decoder_get_num_fidx (dctx);
102 TEST ("be-flipping: Decoder FDE count", fde_cnt == 1);
104 err = sframe_decoder_get_funcdesc (dctx, 0, &nfres, &fsize, &fstart, &finfo);
105 TEST ("be-flipping: Decoder get FDE", err == 0);
106 TEST ("be-flipping: Decoder FRE count", nfres == 5);
108 free (sf_buf);
109 sf_buf = NULL;
111 sframe_decoder_free (&dctx);
112 return 0;
114 setup_fail:
115 sframe_decoder_free (&dctx);
116 fail ("be-flipping: Test setup");
117 return 1;