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