1 /* This testcase is part of GDB, the GNU debugger. 2 3 Copyright 2018-2020 Free Software Foundation, Inc. 4 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. 9 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. 14 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/>. */ 17 18 /* This file is used for testing GDBs ability to pass structures to, and 19 return structures from, functions. All of the structures in this test 20 are special in that they are small structures containing from 1 up to 5 21 scalar fields, the fields can be inside nested structures, and there can 22 be empty structures around too. 23 24 When compiled for C++ this file also tests structures containing static 25 members (which live in global memory). In addition, empty structures in C++ 26 have a size of 1 (compared to 0 in GNU C), which can effect structure 27 padding. 28 29 This test is specifically written for RiscV and Aarch64, which both have 30 special ABI rules for structures like these, however, there should be no harm 31 in running these tests on other targets, though in many cases the 32 structures will treated no differently to the structures already covered 33 in the structs.exp test script. */ 34 35 #include <string.h> 36 37 /* Useful abreviations. */ 38 typedef char tc; 39 typedef short ts; 40 typedef int ti; 41 typedef long tl; 42 typedef long long tll; 43 typedef float tf; 44 typedef double td; 45 typedef long double tld; 46 47 #ifdef TEST_COMPLEX 48 typedef float _Complex tfc; 49 typedef double _Complex tdc; 50 typedef long double _Complex tldc; 51 #endif /* TEST_COMPLEX */ 52 53 #define MAKE_CHECK_FUNCS(TYPE) \ 54 int __attribute__((noinline,noclone)) \ 55 check_arg_ ## TYPE (struct TYPE arg) \ 56 { \ 57 return cmp_ ## TYPE (arg, ref_val_ ## TYPE); \ 58 } \ 59 \ 60 struct TYPE __attribute__((noinline,noclone)) \ 61 rtn_str_ ## TYPE (void) \ 62 { \ 63 return (ref_val_ ## TYPE); \ 64 } 65 66 #define REF_VAL(NAME) struct NAME ref_val_ ## NAME 67 #define ES(NAME) struct { } NAME 68 69 /* Test is either for a single type or two differing types. */ 70 #if defined tA && ! defined tB 71 #define tB tA 72 #endif 73 #if ! defined tB 74 #error "Incorrect configuration of tA and tB defines" 75 #endif 76 77 /* Structures with a single field nested to various depths, along with 78 some empty structures. */ 79 struct struct_01_01 { ES(es1); struct { struct { tA a; } s1; } s2; }; 80 struct struct_01_02 { tA a; struct { struct { ES(es1); } s1; } s2; }; 81 struct struct_01_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4;}; 82 struct struct_01_04 { ES(es1); ES(es2); tA a; ES(es3); }; 83 84 /* Structures with two fields nested to various depths, along with 85 some empty structures. */ 86 struct struct_02_01 { ES(es1); struct { struct { tA a; tB b; } s1; } s2; }; 87 struct struct_02_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; }; 88 struct struct_02_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6;}; 89 struct struct_02_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; }; 90 91 /* Structures with four fields nested to various depths, along with 92 some empty structures. */ 93 struct struct_04_01 { ES(es1); struct { struct { tA a; tB b; tA c; tB d; } s1; } s2; }; 94 struct struct_04_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; tA c; struct { struct { ES(es2); } s4; } s5; tB d;}; 95 struct struct_04_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6; struct { struct { tA c; } s7; } s8; struct { struct { tB d; } s9; } s10;}; 96 struct struct_04_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); tB d; }; 97 98 /* Structures with five fields nested to various depths, along with 99 some empty structures. */ 100 struct struct_05_01 { ES(es1); struct { struct { tA a; tB b; tA c; tB d; tA e; } s1; } s2; }; 101 struct struct_05_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; tA c; struct { struct { ES(es2); } s4; } s5; tB d; struct { struct { ES(es2); } s6; } s7; tB e;}; 102 struct struct_05_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct { struct { tB b; } s5; } s6; struct { struct { tA c; } s7; } s8; struct { struct { tB d; } s9; } s10; struct { struct { tA e; } s11; } s12;}; 103 struct struct_05_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); tB d; ES(es6); tA e; }; 104 105 /* Only C++ allows structures to have static members. */ 106 #ifdef __cplusplus 107 108 /* Structures with two fields nested to various depths, one of which is static. 109 Some include empty structures. */ 110 struct struct_static_02_01 { struct sa { struct sb { tA a; static tB b; } s1; } s2; }; 111 struct struct_static_02_02 { static tA a; struct { struct { ES(es1); } s1; } s2; tB b; }; 112 struct struct_static_02_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct { struct { tA a; } s3; } s4; struct sa { struct sb { static tB b; } s5; } s6;}; 113 struct struct_static_02_04 { static tA a; tB b; }; 114 115 /* Structures with four fields nested to various depths, some of which are 116 static. Some include empty structures. */ 117 struct struct_static_04_01 { struct sa { struct sb { static tA a; tB b; tA c; tB d; } s1; } s2; }; 118 struct struct_static_04_02 { tA a; struct { struct { ES(es1); } s1; } s2; tB b; struct { struct { ES(es1); } s2; } s3; static tA c; struct { struct { ES(es2); } s4; } s5; static tB d;}; 119 struct struct_static_04_03 { struct sa { struct sb { static tA a; } s3; } s4; struct sc { struct sd { static tB b; } s5; } s6; struct se { struct sf { static tA c; } s7; } s8; struct sg { struct sh { static tB d; } s9; } s10;}; 120 struct struct_static_04_04 { ES(es1); ES(es2); tA a; ES(es3); tB b; ES(es4); tA c; ES(es5); static tB d; }; 121 122 /* Structures with six fields nested to various depths, some of which are 123 static. Some include empty structures. */ 124 struct struct_static_06_01 { struct sa { struct sb { tA a; static tB b; tA c; tB d; tA e; } s1; } s2; tB f; }; 125 struct struct_static_06_02 { tA a; static tB b; static tA c; tB d; tB e; tA f;}; 126 struct struct_static_06_03 { struct { struct { ES(es1); } s1; } s2; ES(es1); struct sa { struct sb { static tA a; } s3; } s4; struct sc { struct sd { tB b; } s5; } s6; struct se { struct sf { static tA c; } s7; } s8; struct sg { struct sh { static tB d; } s9; } s10; struct { struct { tA e; tB f; } s11; } s12;}; 127 struct struct_static_06_04 { ES(es1); ES(es2); static tA a; ES(es3); static tB b; ES(es4); static tA c; ES(es5); static tB d; ES(es6); static tA e; ES(es7); tB f; }; 128 129 #endif 130 131 int __attribute__((noinline,noclone)) 132 cmp_struct_01_01 (struct struct_01_01 a, struct struct_01_01 b) 133 { return a.s2.s1.a == b.s2.s1.a; } 134 135 int __attribute__((noinline,noclone)) 136 cmp_struct_01_02 (struct struct_01_02 a, struct struct_01_02 b) 137 { return a.a == b.a; } 138 139 int __attribute__((noinline,noclone)) 140 cmp_struct_01_03 (struct struct_01_03 a, struct struct_01_03 b) 141 { return a.s4.s3.a == b.s4.s3.a; } 142 143 int __attribute__((noinline,noclone)) 144 cmp_struct_01_04 (struct struct_01_04 a, struct struct_01_04 b) 145 { return a.a == b.a; } 146 147 int __attribute__((noinline,noclone)) 148 cmp_struct_02_01 (struct struct_02_01 a, struct struct_02_01 b) 149 { return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b; } 150 151 int __attribute__((noinline,noclone)) 152 cmp_struct_02_02 (struct struct_02_02 a, struct struct_02_02 b) 153 { return a.a == b.a && a.b == b.b; } 154 155 int __attribute__((noinline,noclone)) 156 cmp_struct_02_03 (struct struct_02_03 a, struct struct_02_03 b) 157 { return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b; } 158 159 int __attribute__((noinline,noclone)) 160 cmp_struct_02_04 (struct struct_02_04 a, struct struct_02_04 b) 161 { return a.a == b.a && a.b == b.b; } 162 163 int __attribute__((noinline,noclone)) 164 cmp_struct_04_01 (struct struct_04_01 a, struct struct_04_01 b) 165 { return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b 166 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d; } 167 168 int __attribute__((noinline,noclone)) 169 cmp_struct_04_02 (struct struct_04_02 a, struct struct_04_02 b) 170 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; } 171 172 int __attribute__((noinline,noclone)) 173 cmp_struct_04_03 (struct struct_04_03 a, struct struct_04_03 b) 174 { return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b 175 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d; } 176 177 int __attribute__((noinline,noclone)) 178 cmp_struct_04_04 (struct struct_04_04 a, struct struct_04_04 b) 179 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; } 180 181 int __attribute__((noinline,noclone)) 182 cmp_struct_05_01 (struct struct_05_01 a, struct struct_05_01 b) 183 { return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b 184 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d 185 && a.s2.s1.e == b.s2.s1.e; } 186 187 int __attribute__((noinline,noclone)) 188 cmp_struct_05_02 (struct struct_05_02 a, struct struct_05_02 b) 189 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e; } 190 191 int __attribute__((noinline,noclone)) 192 cmp_struct_05_03 (struct struct_05_03 a, struct struct_05_03 b) 193 { return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b 194 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d 195 && a.s12.s11.e == b.s12.s11.e; } 196 197 int __attribute__((noinline,noclone)) 198 cmp_struct_05_04 (struct struct_05_04 a, struct struct_05_04 b) 199 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e; } 200 201 #ifdef __cplusplus 202 203 int __attribute__((noinline,noclone)) 204 cmp_struct_static_02_01 (struct struct_static_02_01 a, 205 struct struct_static_02_01 b) 206 { return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b; } 207 208 int __attribute__((noinline,noclone)) 209 cmp_struct_static_02_02 (struct struct_static_02_02 a, 210 struct struct_static_02_02 b) 211 { return a.a == b.a && a.b == b.b; } 212 213 int __attribute__((noinline,noclone)) 214 cmp_struct_static_02_03 (struct struct_static_02_03 a, 215 struct struct_static_02_03 b) 216 { return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b; } 217 218 int __attribute__((noinline,noclone)) 219 cmp_struct_static_02_04 (struct struct_static_02_04 a, 220 struct struct_static_02_04 b) 221 { return a.a == b.a && a.b == b.b; } 222 223 int __attribute__((noinline,noclone)) 224 cmp_struct_static_04_01 (struct struct_static_04_01 a, 225 struct struct_static_04_01 b) 226 { return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b 227 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d; } 228 229 int __attribute__((noinline,noclone)) 230 cmp_struct_static_04_02 (struct struct_static_04_02 a, 231 struct struct_static_04_02 b) 232 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; } 233 234 int __attribute__((noinline,noclone)) 235 cmp_struct_static_04_03 (struct struct_static_04_03 a, 236 struct struct_static_04_03 b) 237 { return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b 238 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d; } 239 240 int __attribute__((noinline,noclone)) 241 cmp_struct_static_04_04 (struct struct_static_04_04 a, 242 struct struct_static_04_04 b) 243 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d; } 244 245 int __attribute__((noinline,noclone)) 246 cmp_struct_static_06_01 (struct struct_static_06_01 a, 247 struct struct_static_06_01 b) 248 { return a.s2.s1.a == b.s2.s1.a && a.s2.s1.b == b.s2.s1.b 249 && a.s2.s1.c == b.s2.s1.c && a.s2.s1.d == b.s2.s1.d 250 && a.s2.s1.e == b.s2.s1.e && a.f == b.f; } 251 252 int __attribute__((noinline,noclone)) 253 cmp_struct_static_06_02 (struct struct_static_06_02 a, 254 struct struct_static_06_02 b) 255 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e 256 && a.f == b.f; } 257 258 int __attribute__((noinline,noclone)) 259 cmp_struct_static_06_03 (struct struct_static_06_03 a, 260 struct struct_static_06_03 b) 261 { return a.s4.s3.a == b.s4.s3.a && a.s6.s5.b == b.s6.s5.b 262 && a.s8.s7.c == b.s8.s7.c && a.s10.s9.d == b.s10.s9.d 263 && a.s12.s11.e == b.s12.s11.e && a.s12.s11.f == b.s12.s11.f; } 264 265 int __attribute__((noinline,noclone)) 266 cmp_struct_static_06_04 (struct struct_static_06_04 a, 267 struct struct_static_06_04 b) 268 { return a.a == b.a && a.b == b.b && a.c == b.c && a.d == b.d && a.e == b.e 269 && a.f == b.f; } 270 271 #endif 272 273 REF_VAL(struct_01_01) = { {}, { { 'a' } } }; 274 REF_VAL(struct_01_02) = { 'a', { { {} } } }; 275 REF_VAL(struct_01_03) = { { { {} } }, {}, { { 'a' } } }; 276 REF_VAL(struct_01_04) = { {}, {}, 'a', {} }; 277 278 REF_VAL(struct_02_01) = { {}, { { 'a', 'b' } } }; 279 REF_VAL(struct_02_02) = { 'a', { { {} } }, 'b' }; 280 REF_VAL(struct_02_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } } }; 281 REF_VAL(struct_02_04) = { {}, {}, 'a', {}, 'b' }; 282 283 REF_VAL(struct_04_01) = { {}, { { 'a', 'b', 'c', 'd' } } }; 284 REF_VAL(struct_04_02) = { 'a', { { {} } }, 'b', { { {} } }, 'c', { { {} } }, 'd' }; 285 REF_VAL(struct_04_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } }, { { 'c' } }, { { 'd' } } }; 286 REF_VAL(struct_04_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {}, 'd' }; 287 288 REF_VAL(struct_05_01) = { {}, { { 'a', 'b', 'c', 'd', 'e' } } }; 289 REF_VAL(struct_05_02) = { 'a', { { {} } }, 'b', { { {} } }, 'c', { { {} } }, 'd', { { {} } }, 'e' }; 290 REF_VAL(struct_05_03) = { { { {} } }, {}, { { 'a' } }, { { 'b' } }, { { 'c' } }, { { 'd' } }, { { 'e' } } }; 291 REF_VAL(struct_05_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {}, 'd', {}, 'e' }; 292 293 #ifdef __cplusplus 294 295 /* Initialise static members. */ 296 tB struct_static_02_01::sa::sb::b = '1'; 297 tA struct_static_02_02::a = '2'; 298 tB struct_static_02_03::sa::sb::b = '3'; 299 tA struct_static_02_04::a = '4'; 300 tA struct_static_04_01::sa::sb::a = '5'; 301 tA struct_static_04_02::c = '6'; 302 tB struct_static_04_02::d = '7'; 303 tA struct_static_04_03::sa::sb::a = '8'; 304 tB struct_static_04_03::sc::sd::b = '9'; 305 tA struct_static_04_03::se::sf::c = '0'; 306 tB struct_static_04_03::sg::sh::d = 'A'; 307 tB struct_static_04_04::d = 'B'; 308 tB struct_static_06_01::sa::sb::b = 'C'; 309 tB struct_static_06_02::b = 'D'; 310 tA struct_static_06_02::c = 'E'; 311 tA struct_static_06_03::sa::sb::a = 'F'; 312 tA struct_static_06_03::se::sf::c = 'G'; 313 tB struct_static_06_03::sg::sh::d = 'H'; 314 tA struct_static_06_04::a = 'I'; 315 tB struct_static_06_04::b = 'J'; 316 tA struct_static_06_04::c = 'K'; 317 tB struct_static_06_04::d = 'L'; 318 tA struct_static_06_04::e = 'M'; 319 320 REF_VAL(struct_static_02_01) = { { { 'a' } } }; 321 REF_VAL(struct_static_02_02) = { { { {} } }, 'b' }; 322 REF_VAL(struct_static_02_03) = { { { {} } }, {}, { { 'a' } }, { { } } }; 323 REF_VAL(struct_static_02_04) = { 'b' }; 324 REF_VAL(struct_static_04_01) = { { { 'b', 'c', 'd' } } }; 325 REF_VAL(struct_static_04_02) = { 'a', { { {} } }, 'b', { { {} } }, { { {} } } }; 326 REF_VAL(struct_static_04_03) = {}; 327 REF_VAL(struct_static_04_04) = { {}, {}, 'a', {}, 'b', {}, 'c', {} }; 328 REF_VAL(struct_static_06_01) = { { { 'a', 'c', 'd', 'e' } }, 'f' }; 329 REF_VAL(struct_static_06_02) = { 'a', 'd', 'e', 'f' }; 330 REF_VAL(struct_static_06_03) = { { { {} } }, {}, {}, { { 'b' } }, {}, /*{ { 'e', 'f' } }*/ }; 331 REF_VAL(struct_static_06_04) = { {}, {}, {}, {}, {}, {}, {}, 'f' }; 332 333 #endif 334 335 /* Create all of the functions GDB will call to check functionality. */ 336 MAKE_CHECK_FUNCS(struct_01_01) 337 MAKE_CHECK_FUNCS(struct_01_02) 338 MAKE_CHECK_FUNCS(struct_01_03) 339 MAKE_CHECK_FUNCS(struct_01_04) 340 MAKE_CHECK_FUNCS(struct_02_01) 341 MAKE_CHECK_FUNCS(struct_02_02) 342 MAKE_CHECK_FUNCS(struct_02_03) 343 MAKE_CHECK_FUNCS(struct_02_04) 344 MAKE_CHECK_FUNCS(struct_04_01) 345 MAKE_CHECK_FUNCS(struct_04_02) 346 MAKE_CHECK_FUNCS(struct_04_03) 347 MAKE_CHECK_FUNCS(struct_04_04) 348 MAKE_CHECK_FUNCS(struct_05_01) 349 MAKE_CHECK_FUNCS(struct_05_02) 350 MAKE_CHECK_FUNCS(struct_05_03) 351 MAKE_CHECK_FUNCS(struct_05_04) 352 #ifdef __cplusplus 353 MAKE_CHECK_FUNCS(struct_static_02_01) 354 MAKE_CHECK_FUNCS(struct_static_02_02) 355 MAKE_CHECK_FUNCS(struct_static_02_03) 356 MAKE_CHECK_FUNCS(struct_static_02_04) 357 MAKE_CHECK_FUNCS(struct_static_04_01) 358 MAKE_CHECK_FUNCS(struct_static_04_02) 359 MAKE_CHECK_FUNCS(struct_static_04_03) 360 MAKE_CHECK_FUNCS(struct_static_04_04) 361 MAKE_CHECK_FUNCS(struct_static_06_01) 362 MAKE_CHECK_FUNCS(struct_static_06_02) 363 MAKE_CHECK_FUNCS(struct_static_06_03) 364 MAKE_CHECK_FUNCS(struct_static_06_04) 365 #endif 366 367 #define CALL_LINE(NAME) val += check_arg_ ## NAME (rtn_str_ ## NAME ()) 368 369 int __attribute__((noinline,noclone)) 370 call_all () 371 { 372 int val = 0; 373 374 CALL_LINE(struct_01_01); 375 CALL_LINE(struct_01_02); 376 CALL_LINE(struct_01_03); 377 CALL_LINE(struct_01_04); 378 CALL_LINE(struct_02_01); 379 CALL_LINE(struct_02_02); 380 CALL_LINE(struct_02_03); 381 CALL_LINE(struct_02_04); 382 CALL_LINE(struct_04_01); 383 CALL_LINE(struct_04_02); 384 CALL_LINE(struct_04_03); 385 CALL_LINE(struct_04_04); 386 CALL_LINE(struct_05_01); 387 CALL_LINE(struct_05_02); 388 CALL_LINE(struct_05_03); 389 CALL_LINE(struct_05_04); 390 #ifdef __cplusplus 391 CALL_LINE(struct_static_02_01); 392 CALL_LINE(struct_static_02_02); 393 CALL_LINE(struct_static_02_03); 394 CALL_LINE(struct_static_02_04); 395 CALL_LINE(struct_static_04_01); 396 CALL_LINE(struct_static_04_02); 397 CALL_LINE(struct_static_04_03); 398 CALL_LINE(struct_static_04_04); 399 CALL_LINE(struct_static_06_01); 400 CALL_LINE(struct_static_06_02); 401 CALL_LINE(struct_static_06_03); 402 CALL_LINE(struct_static_06_04); 403 #endif 404 405 return val; 406 } 407 408 int volatile v = 1; 409 410 void __attribute__((noinline, noclone)) 411 breakpt (void) 412 { 413 v++; 414 } 415 416 int 417 main () 418 { 419 int res; 420 421 res = call_all (); 422 breakpt (); /* Break Here. */ 423 return res; 424 } 425