gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
exec_ecoff.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 The Regents of the University of California
3  * All rights reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Authors: Bobby R. Bruce
29  */
30 
31 
32 #include <gtest/gtest.h>
33 
34 #include "base/loader/exec_ecoff.h"
35 
36 #define OMAGIC_STRUCT (*((const ecoff_exechdr *)omagic))
37 #define NMAGIC_STRUCT (*((const ecoff_exechdr *)nmagic))
38 #define ZMAGIC_STRUCT (*((const ecoff_exechdr *)zmagic))
39 #define NO_MAGIC_STRUCT (*((const ecoff_exechdr *)no_magic))
40 
41 #if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN))\
42  && BYTE_ORDER==BIG_ENDIAN
43 const uint8_t zmagic[] = {
44  // ecoff_filehdr
45  0x01, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 
49  // ecoff_aouthdr
50  0x01, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51  0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00,
52  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59  0x00, 0x00, 0x00, 0x00, 0x00, 0x00
60 };
61 
62 const uint8_t nmagic[] = {
63  //ecoff_filehdr
64  0x01, 0x08, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00,
65  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
67 
68  //ecoff_aouthdr
69  0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70  0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00,
71  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78  0x00, 0x00, 0x00, 0x00, 0x00, 0x00
79 };
80 
81 const uint8_t omagic[] = {
82  //ecoff_filehdr
83  0x01, 0x07, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00,
84  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 
87  //ecoff_aouthdr
88  0x01, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89  0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
90  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97  0x00, 0x00, 0x00, 0x00, 0x00, 0x00
98 };
99 #elif defined(_LITTLE_ENDIAN) || BYTE_ORDER==LITTLE_ENDIAN
100 const uint8_t zmagic[] = {
101  // ecoff_filehdr
102  0x0B, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 
106  // ecoff_aouthdr
107  0x0B, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108  0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116  0x00, 0x00, 0x00, 0x00, 0x00, 0x00
117 };
118 
119 const uint8_t nmagic[] = {
120  //ecoff_filehdr
121  0x08, 0x01, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00,
122  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
124 
125  //ecoff_aouthdr
126  0x08, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127  0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135  0x00, 0x00, 0x00, 0x00, 0x00, 0x00
136 };
137 
138 const uint8_t omagic[] = {
139  //ecoff_filehdr
140  0x07, 0x01, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00,
141  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 
144  //ecoff_aouthdr
145  0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146  0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154  0x00, 0x00, 0x00, 0x00, 0x00, 0x00
155 };
156 #else
157  #error Invalid Endianess
158 #endif
159 
160 
161 
162 TEST(ExecEcoffTest, Round)
163 {
164  /*
165  * Note: ECOFF_ROUND always rounds up.
166  */
167  EXPECT_EQ((1 << 10) + (1 << 8) + (1 << 6),
168  ECOFF_ROUND((1 << 10) + (1 << 8) + (1 << 5) + (1 << 4) + (1 << 1),
169  (1 << 5)));
170 
171  EXPECT_EQ((1 << 10) + (1 << 8) + (1 << 5),
172  ECOFF_ROUND((1 << 10) + (1 << 8) + (1 << 4) + (1 << 1),
173  (1 << 5)));
174 
175  EXPECT_EQ((1 << 10) + (1 << 8) + (1 << 6),
176  ECOFF_ROUND((1 << 10) + (1 << 8) + (1 << 5) + (1 << 1),
177  (1 << 5)));
178 }
179 
180 TEST(ExecEcoffTest, NoRounding)
181 {
182  EXPECT_EQ((1 << 10) + (1 << 8) + (1 << 5),
183  ECOFF_ROUND((1 << 10) + (1 << 8) + (1 << 5),
184  (1 << 5)));
185 }
186 
187 TEST(ExecEcoffTest, BlockAlignWithoutZmagic)
188 {
189  /*
190  * ECOFF_BLOCK_ALIGN will return "value" if "ep->a.magic" is not
191  * ECOFF_ZMAGIC.
192  */
193  u_int64_t value = 0XFFFF;
194 
195  EXPECT_EQ(value, ECOFF_BLOCK_ALIGN(&OMAGIC_STRUCT, value));
196  EXPECT_EQ(value, ECOFF_BLOCK_ALIGN(&OMAGIC_STRUCT, value));
197 }
198 
199 TEST(ExecEcoffTest, BlockAlignWithZmagic)
200 {
201  /*
202  * ECOFF_BLOCK_ALIGN will return "value" rounded ECOFF_LDPGSZ (1 << 12)
203  * if "ep->a.magic" is ECOFF_ZMAGIC. This uses ECOFF_ROUND, so will only
204  * round up.
205  */
206  u_int64_t value1 = (1 << 14) + (1 << 13) + (1 << 10) + (1 << 8);
207  u_int64_t expected_output1 = (1 << 14) + (1 << 13) + (1 << 12);
208  EXPECT_EQ(expected_output1, ECOFF_BLOCK_ALIGN(&ZMAGIC_STRUCT, value1));
209 
210  u_int64_t value2 = (1 << 10) + (1 << 8);
211  u_int64_t expected_output2 = (1 << 12) ;
212  EXPECT_EQ(expected_output2, ECOFF_BLOCK_ALIGN(&ZMAGIC_STRUCT, value2));
213 }
214 
215 TEST(ExecEcoffTest, SegmentAlignWithZmagic)
216 {
217  /*
218  * ECOFF_SEGMENT_ALIGN will round "value" rounded by ECOFF_LDPGZ (1 << 12)
219  * if "ep->amagic" is ECOFF_ZMAGIC.
220  */
221  u_int64_t value1 = (1 << 14) + (1 << 13) + (1 << 10) + (1 << 8);
222  u_int64_t expected_output1 = (1 << 14) + (1 << 13) + (1 << 12);
223  EXPECT_EQ(expected_output1, ECOFF_SEGMENT_ALIGN(&ZMAGIC_STRUCT, value1));
224 
225  u_int64_t value2 = (1 << 10) + (1 << 8);
226  u_int64_t expected_output2 = (1 << 12) ;
227  EXPECT_EQ(expected_output2, ECOFF_SEGMENT_ALIGN(&ZMAGIC_STRUCT, value2));
228 }
229 
230 TEST(ExecEcoffTest, SegmentAlignWithoutZmagic)
231 {
232  /*
233  * ECOFF_SEGMENT_ALIGN will return value rounded by the
234  * ECOFF_SEGMENT_ALIGNMENT of ep (ECOFF_SEGMENT_ALIGNMENT is defined in
235  * "arch/alpha/ecoff_machdep.h") if "ep->amagic" is not ECOFF_ZMAGIC.
236  *
237  * ECOFF_SEGMENT_ALIGNMENT(ep) will return 8 if the 2nd LSB in
238  * "(ep)->f.f_flags" is 0 and 16 if it is 1.
239  *
240  * "(nmagic)->f.f_flags" is 0x2, and therefore ECOFF_SEGMENT_ALIGN will
241  * round value by (1 << 4). "(omagic)->f.f_flags" is 0x0 and, in this
242  * case, ECOFF_SEGMENT_ALIGN will round value by (1 << 3).
243  */
244 
245  u_int64_t value = (1 << 14) + (1 << 13) + (1 << 8) + (1 << 4) + (1 << 2)
246  + (1 << 1);
247 
248  EXPECT_EQ((1 << 14) + (1 << 13) + (1 << 8) + (1 << 5),
249  ECOFF_SEGMENT_ALIGN(&NMAGIC_STRUCT, value)); // Round by (1 << 4).
250  EXPECT_EQ((1 << 14) + (1 << 13) + (1 << 8) + (1 << 4) + (1 << 3),
251  ECOFF_SEGMENT_ALIGN(&OMAGIC_STRUCT, value)); // Round by (1 << 3).
252 }
253 
254 TEST(ExecEcoffTest, TxtOffWithMagic)
255 {
256  /*
257  * ECOFF_TXTOFF will return 0 if "(ep)->a.magic" is ECOFF_ZMAGIC
258  */
260 }
261 
262 TEST(ExecEcoffTest, TxtOffWithoutMagic)
263 {
264  /*
265  * If "(ep)->a.magic" is not ECOFF_ZMAGIC, ECOFF_TXTOFF will return
266  * "(ep)->f.f_nscns" multiplied by ECOFF_HDR_SIZE (sizeof ecoff_scnhdr)
267  * plus the size of ecoff_exechdr, roundeded by ECOFF_SEGMENT_ALIGNMENT.
268  * The value of ECOFF_SEGMENT_ALIGNMENT will return (1 << 3) if the 2nd
269  * LSB in "(ep)->f.f_flags" is 0 and (1 << 4) if it's one.
270  *
271  * In our setup (omagic)->f.f_flags is 0x0 and (nmagic)->f.f_flags is 0x2.
272  * (omagic)->f.f_nscns is 0xA, and (nmagic)->f.f_nscns is 0xF.
273  */
274 
275  /*
276  * (sizeof omagic) * 0xA + (sizeof omagic), rounded by (1 << 3).
277  */
278  u_int64_t expected_output_omagic = ECOFF_ROUND(
279  (sizeof(struct ecoff_scnhdr)) * 0xA
280  + (sizeof(struct ecoff_exechdr)),
281  (1 << 3));
282  EXPECT_EQ(expected_output_omagic, ECOFF_TXTOFF(&OMAGIC_STRUCT));
283 
284  /*
285  * (sizeof nmagic) * 0xF + (sizeof nmagic), rounded by (1 << 4).
286  */
287  u_int64_t expected_output_nmagic = ECOFF_ROUND(
288  (sizeof(struct ecoff_scnhdr) * 0xF
289  + (sizeof(struct ecoff_exechdr))),
290  (1 << 4));
291  EXPECT_EQ(expected_output_nmagic, ECOFF_TXTOFF(&NMAGIC_STRUCT));
292 }
293 
294 TEST(ExecEcoffTest, DatOffWithMagic)
295 {
296  /*
297  * f (ep)->a.magic is ECOFF_ZMAGIC, ECOFF_DATOFF will return
298  * (ep)->a.tsize plus ECOFF_TXT(ep), rounded by ECOFF_LDPGSZ (1 << 12).
299  *
300  * (zmagic)->a.tsize is 0xF.
301  */
302 
303  u_int64_t expected_output = ECOFF_ROUND(
304  0xF + ECOFF_TXTOFF(&ZMAGIC_STRUCT), (1 << 12));
305  EXPECT_EQ(expected_output, ECOFF_DATOFF(&ZMAGIC_STRUCT));
306 }
307 
308 TEST(ExecEcoffTest, DatOffWithoutMagic)
309 {
310  /*
311  * ECOFF_DATOFF will return (ep)->a.tsize plus ECOFF_TXTOFF(ep) if
312  * (ep)->a.magic is not ECOFF_ZMAGIC.
313  *
314  * (nmagic)->a.tsize is 0xA.
315  * (omagic)->a.tsize is 0x1.
316  */
317 
318  u_int64_t expected_output_nmagic = 0xA + ECOFF_TXTOFF(&NMAGIC_STRUCT);
319  EXPECT_EQ(expected_output_nmagic, ECOFF_DATOFF(&NMAGIC_STRUCT));
320 
321  u_int64_t expected_output_omagic = 0x1 + ECOFF_TXTOFF(&OMAGIC_STRUCT);
322  EXPECT_EQ(expected_output_omagic, ECOFF_DATOFF(&OMAGIC_STRUCT));
323 }
#define ECOFF_SEGMENT_ALIGN(ep, value)
Definition: exec_ecoff.h:105
#define ZMAGIC_STRUCT
#define OMAGIC_STRUCT
TEST(ExecEcoffTest, Round)
#define ECOFF_DATOFF(ep)
Definition: exec_ecoff.h:102
const uint8_t zmagic[]
#define NMAGIC_STRUCT
#define ECOFF_BLOCK_ALIGN(ep, value)
Definition: exec_ecoff.h:93
const uint8_t nmagic[]
#define ECOFF_TXTOFF(ep)
Definition: exec_ecoff.h:97
#define ECOFF_ROUND(value, by)
Definition: exec_ecoff.h:90
const uint8_t omagic[]
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:112

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13