]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_abi.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_abi.cc
1 // -*- C++ -*-
2
3 // Copyright (C) 2004-2019 Free Software Foundation, Inc.
4
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3, or (at
8 // your option) any later version.
9
10 // This library is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19
20 // Benjamin Kosnik <bkoz@redhat.com>
21
22 #include "testsuite_abi.h"
23 #include <cstdlib>
24 #include <sstream>
25 #include <fstream>
26 #include <iostream>
27 #include <vector>
28 #include <algorithm>
29
30 using namespace std;
31
32 void
33 symbol::init(string& data)
34 {
35 const char delim = ':';
36 const char version_delim = '@';
37 const string::size_type npos = string::npos;
38 string::size_type n = 0;
39
40 // Set the type.
41 if (data.find("FUNC") == 0)
42 type = symbol::function;
43 else if (data.find("OBJECT") == 0)
44 type = symbol::object;
45 else if (data.find("TLS") == 0)
46 type = symbol::tls;
47
48 n = data.find_first_of(delim);
49 if (n != npos)
50 data.erase(data.begin(), data.begin() + n + 1);
51
52 // Iff object or TLS, get size info.
53 if (type == symbol::object || type == symbol::tls)
54 {
55 n = data.find_first_of(delim);
56 if (n != npos)
57 {
58 string objectsize(data.begin(), data.begin() + n);
59 istringstream iss(objectsize);
60 int x;
61 iss >> x;
62 if (!iss.fail())
63 size = x;
64 data.erase(data.begin(), data.begin() + n + 1);
65 }
66 }
67
68 // Set the name and raw_name.
69 raw_name = string(data.begin(), data.end());
70 n = data.find_first_of(version_delim);
71 if (n != npos)
72 {
73 // Found version string.
74 name = string(data.begin(), data.begin() + n);
75 n = data.find_last_of(version_delim);
76 data.erase(data.begin(), data.begin() + n + 1);
77
78 // Set version name.
79 version_name = data;
80 }
81 else
82 {
83 // No versioning info.
84 name = string(data.begin(), data.end());
85 version_status = symbol::none;
86 }
87
88 // Set the demangled name.
89 demangled_name = demangle(name);
90 }
91
92 void
93 symbol::print() const
94 {
95 const char tab = '\t';
96 cout << name << endl;
97
98 if (demangled_name != name)
99 cout << demangled_name << endl;
100
101 string vers;
102 switch (version_status)
103 {
104 case none:
105 vers = "none";
106 break;
107 case compatible:
108 vers = "compatible";
109 break;
110 case incompatible:
111 vers = "incompatible";
112 break;
113 case unversioned:
114 vers = "unversioned";
115 break;
116 default:
117 vers = "<default>";
118 }
119 cout << "version status: " << vers << endl;
120
121 if (version_name.size()
122 && (version_status == compatible || version_status == incompatible))
123 cout << version_name << endl;
124
125 string type_string;
126 switch (type)
127 {
128 case function:
129 type_string = "function";
130 break;
131 case object:
132 type_string = "object";
133 break;
134 case tls:
135 type_string = "tls";
136 break;
137 case uncategorized:
138 type_string = "uncategorized";
139 break;
140 default:
141 type_string = "<default>";
142 }
143 cout << "type: " << type_string << endl;
144
145 if (type == object || type == tls)
146 cout << "type size: " << size << endl;
147
148 string status_string;
149 switch (status)
150 {
151 case added:
152 status_string = "added";
153 break;
154 case subtracted:
155 status_string = "subtracted";
156 break;
157 case undesignated:
158 status_string = "undesignated";
159 break;
160 default:
161 status_string = "<default>";
162 }
163 cout << "status: " << status_string << endl;
164
165 cout << endl;
166 }
167
168
169 bool
170 check_version(symbol& test, bool added)
171 {
172 // Construct list of compatible versions.
173 typedef std::vector<std::string> compat_list;
174 static compat_list known_versions;
175 if (known_versions.empty())
176 {
177 // NB: First version here must be the default version for this
178 // version of DT_SONAME.
179 known_versions.push_back("GLIBCXX_3.4");
180 known_versions.push_back("GLIBCXX_LDBL_3.4");
181 known_versions.push_back("GLIBCXX_3.4.1");
182 known_versions.push_back("GLIBCXX_3.4.2");
183 known_versions.push_back("GLIBCXX_3.4.3");
184 known_versions.push_back("GLIBCXX_3.4.4");
185 known_versions.push_back("GLIBCXX_3.4.5");
186 known_versions.push_back("GLIBCXX_3.4.6");
187 known_versions.push_back("GLIBCXX_3.4.7");
188 known_versions.push_back("GLIBCXX_LDBL_3.4.7");
189 known_versions.push_back("GLIBCXX_3.4.8");
190 known_versions.push_back("GLIBCXX_3.4.9");
191 known_versions.push_back("GLIBCXX_3.4.10");
192 known_versions.push_back("GLIBCXX_LDBL_3.4.10");
193 known_versions.push_back("GLIBCXX_3.4.11");
194 known_versions.push_back("GLIBCXX_3.4.12");
195 known_versions.push_back("GLIBCXX_3.4.13");
196 known_versions.push_back("GLIBCXX_3.4.14");
197 known_versions.push_back("GLIBCXX_3.4.15");
198 known_versions.push_back("GLIBCXX_3.4.16");
199 known_versions.push_back("GLIBCXX_3.4.17");
200 known_versions.push_back("GLIBCXX_3.4.18");
201 known_versions.push_back("GLIBCXX_3.4.19");
202 known_versions.push_back("GLIBCXX_3.4.20");
203 known_versions.push_back("GLIBCXX_3.4.21");
204 known_versions.push_back("GLIBCXX_LDBL_3.4.21");
205 known_versions.push_back("GLIBCXX_3.4.22");
206 known_versions.push_back("GLIBCXX_3.4.23");
207 known_versions.push_back("GLIBCXX_3.4.24");
208 known_versions.push_back("GLIBCXX_3.4.25");
209 known_versions.push_back("GLIBCXX_3.4.26");
210 known_versions.push_back("CXXABI_1.3");
211 known_versions.push_back("CXXABI_LDBL_1.3");
212 known_versions.push_back("CXXABI_1.3.1");
213 known_versions.push_back("CXXABI_1.3.2");
214 known_versions.push_back("CXXABI_1.3.3");
215 known_versions.push_back("CXXABI_1.3.4");
216 known_versions.push_back("CXXABI_1.3.5");
217 known_versions.push_back("CXXABI_1.3.6");
218 known_versions.push_back("CXXABI_1.3.7");
219 known_versions.push_back("CXXABI_1.3.8");
220 known_versions.push_back("CXXABI_1.3.9");
221 known_versions.push_back("CXXABI_1.3.10");
222 known_versions.push_back("CXXABI_1.3.11");
223 known_versions.push_back("CXXABI_TM_1");
224 known_versions.push_back("CXXABI_FLOAT128");
225 }
226 compat_list::iterator begin = known_versions.begin();
227 compat_list::iterator end = known_versions.end();
228
229 // Check for compatible version.
230 if (test.version_name.size())
231 {
232 compat_list::iterator it1 = find(begin, end, test.version_name);
233 compat_list::iterator it2 = find(begin, end, test.name);
234 if (it1 != end)
235 test.version_status = symbol::compatible;
236 else
237 test.version_status = symbol::incompatible;
238
239 // Check that added symbols are added in the latest pre-release version.
240 bool latestp = (test.version_name == "GLIBCXX_3.4.26"
241 || test.version_name == "CXXABI_1.3.11"
242 || test.version_name == "CXXABI_FLOAT128"
243 || test.version_name == "CXXABI_TM_1");
244 if (added && !latestp)
245 test.version_status = symbol::incompatible;
246
247 // Check that long double compatibility symbols demangled as
248 // __float128 and regular __float128 symbols are put into some _LDBL_
249 // or _FLOAT128 version name.
250 if (added && test.demangled_name.find("__float128") != std::string::npos
251 && test.demangled_name.find("std::__cxx11::") != 0)
252 {
253 if (test.version_name.find("_LDBL_") == std::string::npos
254 && test.version_name.find("_FLOAT128") == std::string::npos)
255 test.version_status = symbol::incompatible;
256 }
257
258 // Check for weak label.
259 if (it1 == end && it2 == end)
260 test.version_status = symbol::incompatible;
261
262 // Check that
263 // GLIBCXX_3.4
264 // GLIBCXX_3.4.5
265 // version as compatible
266 // XXX
267 }
268 else
269 {
270 if (added)
271 {
272 // New version labels are ok. The rest are not.
273 compat_list::iterator it2 = find(begin, end, test.name);
274 if (it2 != end)
275 test.version_status = symbol::compatible;
276 else
277 test.version_status = symbol::incompatible;
278 }
279 }
280 return test.version_status == symbol::compatible;
281 }
282
283 bool
284 check_compatible(symbol& lhs, symbol& rhs, bool verbose)
285 {
286 bool ret = true;
287 const char tab = '\t';
288
289 // Check to see if symbol_objects are compatible.
290 if (lhs.type != rhs.type)
291 {
292 ret = false;
293 if (verbose)
294 cout << tab << "incompatible types" << endl;
295 }
296
297 if (lhs.name != rhs.name)
298 {
299 ret = false;
300 if (verbose)
301 cout << tab << "incompatible names" << endl;
302 }
303
304 if (lhs.size != rhs.size)
305 {
306 ret = false;
307 if (verbose)
308 {
309 cout << tab << "incompatible sizes" << endl;
310 cout << tab << lhs.size << endl;
311 cout << tab << rhs.size << endl;
312 }
313 }
314
315 if (lhs.version_name != rhs.version_name
316 && !check_version(lhs) && !check_version(rhs))
317 {
318 ret = false;
319 if (verbose)
320 {
321 cout << tab << "incompatible versions" << endl;
322 cout << tab << lhs.version_name << endl;
323 cout << tab << rhs.version_name << endl;
324 }
325 }
326
327 if (verbose)
328 cout << endl;
329
330 return ret;
331 }
332
333
334 inline bool
335 has_symbol(const string& name, const symbols& s) throw()
336 { return s.find(name) != s.end(); }
337
338 const symbol&
339 get_symbol(const string& name, const symbols& s)
340 {
341 symbols::const_iterator i = s.find(name);
342 if (i != s.end())
343 {
344 return i->second;
345 }
346 else
347 {
348 ostringstream os;
349 os << "get_symbol failed for symbol " << name;
350 __throw_logic_error(os.str().c_str());
351 }
352 }
353
354 void
355 examine_symbol(const char* name, const char* file)
356 {
357 try
358 {
359 symbols s = create_symbols(file);
360 const symbol& sym = get_symbol(name, s);
361 sym.print();
362 }
363 catch(...)
364 { __throw_exception_again; }
365 }
366
367 int
368 compare_symbols(const char* baseline_file, const char* test_file,
369 bool verbose)
370 {
371 // Input both lists of symbols into container.
372 symbols baseline = create_symbols(baseline_file);
373 symbols test = create_symbols(test_file);
374
375 // Sanity check results.
376 if (!baseline.size() || !test.size())
377 {
378 cerr << "Problems parsing the list of exported symbols." << endl;
379 exit(2);
380 }
381
382 // Check to see if any long double compatibility symbols are produced.
383 bool ld_version_found(false);
384 symbols::iterator li(test.begin());
385 while (!ld_version_found && li != test.end())
386 {
387 if (li->second.version_name.find("_LDBL_") != std::string::npos)
388 ld_version_found = true;
389 ++li;
390 }
391
392 // Sort out names.
393 // Assuming all baseline names and test names are both unique w/ no
394 // duplicates.
395 //
396 // The names added to missing_names are baseline names not found in
397 // test names
398 // -> symbols that have been deleted.
399 //
400 // The names added to added_names are test names not in
401 // baseline names
402 // -> symbols that have been added.
403 typedef std::vector<std::string> symbol_names;
404 symbol_names shared_names;
405 symbol_names missing_names;
406 symbol_names added_names;
407 for (li = test.begin(); li != test.end(); ++li)
408 added_names.push_back(li->first);
409
410 for (symbols::iterator i = baseline.begin(); i != baseline.end(); ++i)
411 {
412 string name(i->first);
413 symbol_names::iterator end = added_names.end();
414 symbol_names::iterator it = find(added_names.begin(), end, name);
415 if (it != end)
416 {
417 // Found.
418 shared_names.push_back(name);
419 added_names.erase(it);
420 }
421 else
422 {
423 // Iff no test long double compatibility symbols at all and the symbol
424 // missing is a baseline long double compatibility symbol, skip.
425 string version_name(i->second.version_name);
426 bool base_ld(version_name.find("_LDBL_") != std::string::npos);
427 if (!base_ld || base_ld && ld_version_found)
428 missing_names.push_back(name);
429 }
430 }
431
432 // Fill out list of incompatible symbols.
433 typedef pair<symbol, symbol> symbol_pair;
434 vector<symbol_pair> incompatible;
435
436 // Fill out list of undesignated symbols.
437 vector<symbol> undesignated;
438
439 // Check missing names for compatibility.
440 for (size_t j = 0; j < missing_names.size(); ++j)
441 {
442 symbol& sbase = baseline[missing_names[j]];
443 sbase.status = symbol::subtracted;
444 incompatible.push_back(symbol_pair(sbase, sbase));
445 }
446
447 // Check shared names for compatibility.
448 const symbol_names::size_type shared_size = shared_names.size();
449 for (size_t k = 0; k < shared_size; ++k)
450 {
451 symbol& sbase = baseline[shared_names[k]];
452 symbol& stest = test[shared_names[k]];
453 stest.status = symbol::existing;
454 if (!check_compatible(sbase, stest))
455 incompatible.push_back(symbol_pair(sbase, stest));
456 }
457
458 // Check added names for compatibility.
459 const symbol_names::size_type added_size = added_names.size();
460 for (size_t l = 0; l < added_size; ++l)
461 {
462 symbol& stest = test[added_names[l]];
463
464 // Mark TLS as undesignated, remove from added.
465 if (stest.type == symbol::tls)
466 {
467 stest.status = symbol::undesignated;
468 if (!check_version(stest, false))
469 incompatible.push_back(symbol_pair(stest, stest));
470 else
471 undesignated.push_back(stest);
472 }
473 else
474 {
475 stest.status = symbol::added;
476 if (!check_version(stest, true))
477 incompatible.push_back(symbol_pair(stest, stest));
478 }
479 }
480
481 // Normalize added names and undesignated names.
482 const size_t undesignated_size = undesignated.size();
483 for (size_t l = 0; l < undesignated_size; ++l)
484 {
485 symbol& sundes = undesignated[l];
486 symbol_names::iterator end = added_names.end();
487 symbol_names::iterator it = find(added_names.begin(), end, sundes.name);
488 if (it != end)
489 {
490 // Found.
491 added_names.erase(it);
492 }
493 else
494 __throw_runtime_error(sundes.name.c_str());
495 }
496
497
498 // Report results.
499 if (verbose && added_names.size())
500 {
501 cout << endl << added_names.size() << " added symbols " << endl;
502 for (size_t j = 0; j < added_names.size() ; ++j)
503 {
504 cout << j << endl;
505 test[added_names[j]].print();
506 }
507 }
508
509 if (verbose && missing_names.size())
510 {
511 cout << endl << missing_names.size() << " missing symbols " << endl;
512 for (size_t j = 0; j < missing_names.size() ; ++j)
513 {
514 cout << j << endl;
515 baseline[missing_names[j]].print();
516 }
517 }
518
519 if (verbose && undesignated.size())
520 {
521 cout << endl << undesignated.size() << " undesignated symbols " << endl;
522 for (size_t j = 0; j < undesignated.size() ; ++j)
523 {
524 // First, print index.
525 cout << j << endl;
526
527 // Second, report name.
528 symbol& s = undesignated[j];
529 s.print();
530 }
531 }
532
533 if (verbose && incompatible.size())
534 {
535 cout << endl << incompatible.size() << " incompatible symbols " << endl;
536 for (size_t j = 0; j < incompatible.size() ; ++j)
537 {
538 // First, print index.
539 cout << j << endl;
540
541 // Second, report name.
542 symbol& sbase = incompatible[j].first;
543 symbol& stest = incompatible[j].second;
544 stest.print();
545
546 // Third, report reason or reasons incompatible.
547 check_compatible(sbase, stest, true);
548 }
549 }
550
551 cout << "\n\t\t==== libstdc++-v3 check-abi Summary ====" << endl;
552 cout << endl;
553 cout << "# of added symbols:\t\t " << added_names.size() << endl;
554 cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
555 cout << "# of undesignated symbols:\t " << undesignated.size() << endl;
556 cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
557 cout << endl;
558 cout << "using: " << baseline_file << endl;
559
560 return !(missing_names.size() || incompatible.size());
561 }
562
563
564 symbols
565 create_symbols(const char* file)
566 {
567 symbols s;
568 ifstream ifs(file);
569 if (ifs.is_open())
570 {
571 // Organize file data into an associated container (symbols) of symbol
572 // objects mapped to mangled names without versioning
573 // information.
574 const string empty;
575 string line = empty;
576 while (getline(ifs, line).good())
577 {
578 symbol tmp;
579 tmp.init(line);
580 s[tmp.name] = tmp;
581 line = empty;
582 }
583 }
584 else
585 {
586 ostringstream os;
587 os << "create_symbols failed for file " << file;
588 __throw_runtime_error(os.str().c_str());
589 }
590 return s;
591 }
592
593
594 std::string
595 demangle(const std::string& mangled)
596 {
597 std::string name;
598 if (mangled[0] != '_' || mangled[1] != 'Z')
599 {
600 // This is not a mangled symbol, thus has "C" linkage.
601 name = mangled;
602 }
603 else
604 {
605 // Use __cxa_demangle to demangle.
606 int status = 0;
607 char* ptr = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
608 if (ptr)
609 {
610 name = ptr;
611 free(ptr);
612 }
613 else
614 {
615 switch (status)
616 {
617 case 0:
618 name = "error code = 0: success";
619 break;
620 case -1:
621 name = "error code = -1: memory allocation failure";
622 break;
623 case -2:
624 name = "error code = -2: invalid mangled name";
625 break;
626 case -3:
627 name = "error code = -3: invalid arguments";
628 break;
629 default:
630 name = "error code unknown - who knows what happened";
631 }
632 }
633 }
634 return name;
635 }