3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 3, or (at
9 // your option) any later version.
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Benjamin Kosnik <bkoz@redhat.com>
23 #include "testsuite_abi.h"
34 symbol::init(string& data)
36 const char delim = ':';
37 const char version_delim = '@';
38 const string::size_type npos = string::npos;
39 string::size_type n = 0;
42 if (data.find("FUNC") == 0)
43 type = symbol::function;
44 else if (data.find("OBJECT") == 0)
45 type = symbol::object;
47 n = data.find_first_of(delim);
49 data.erase(data.begin(), data.begin() + n + 1);
51 // Iff object, get size info.
52 if (type == symbol::object)
54 n = data.find_first_of(delim);
57 string objectsize(data.begin(), data.begin() + n);
58 istringstream iss(objectsize);
63 data.erase(data.begin(), data.begin() + n + 1);
67 // Set the name and raw_name.
68 raw_name = string(data.begin(), data.end());
69 n = data.find_first_of(version_delim);
72 // Found version string.
73 name = string(data.begin(), data.begin() + n);
74 n = data.find_last_of(version_delim);
75 data.erase(data.begin(), data.begin() + n + 1);
82 // No versioning info.
83 name = string(data.begin(), data.end());
84 version_status = symbol::none;
87 // Set the demangled name.
88 demangled_name = demangle(name);
94 const char tab = '\t';
97 if (demangled_name != name)
98 cout << demangled_name << endl;
101 switch (version_status)
110 vers = "incompatible";
113 vers = "unversioned";
118 cout << "version status: " << vers << endl;
120 if (version_name.size()
121 && (version_status == compatible || version_status == incompatible))
122 cout << version_name << endl;
128 type_string = "function";
131 type_string = "object";
134 type_string = "uncategorized";
137 type_string = "<default>";
139 cout << "type: " << type_string << endl;
142 cout << "type size: " << size << endl;
144 string status_string;
148 status_string = "added";
151 status_string = "subtracted";
154 status_string = "undesignated";
157 status_string = "<default>";
159 cout << "status: " << status_string << endl;
166 check_version(symbol& test, bool added)
168 // Construct list of compatible versions.
169 typedef std::vector<std::string> compat_list;
170 static compat_list known_versions;
171 if (known_versions.empty())
173 // NB: First version here must be the default version for this
174 // version of DT_SONAME.
175 known_versions.push_back("GLIBCXX_3.4");
176 known_versions.push_back("GLIBCXX_3.4.1");
177 known_versions.push_back("GLIBCXX_3.4.2");
178 known_versions.push_back("GLIBCXX_3.4.3");
179 known_versions.push_back("GLIBCXX_3.4.4");
180 known_versions.push_back("GLIBCXX_3.4.5");
181 known_versions.push_back("GLIBCXX_3.4.6");
182 known_versions.push_back("GLIBCXX_3.4.7");
183 known_versions.push_back("GLIBCXX_3.4.8");
184 known_versions.push_back("GLIBCXX_3.4.9");
185 known_versions.push_back("GLIBCXX_3.4.10");
186 known_versions.push_back("GLIBCXX_3.4.11");
187 known_versions.push_back("GLIBCXX_3.4.12");
188 known_versions.push_back("GLIBCXX_3.4.13");
189 known_versions.push_back("GLIBCXX_3.4.14");
190 known_versions.push_back("GLIBCXX_3.4.15");
191 known_versions.push_back("GLIBCXX_LDBL_3.4");
192 known_versions.push_back("GLIBCXX_LDBL_3.4.7");
193 known_versions.push_back("GLIBCXX_LDBL_3.4.10");
194 known_versions.push_back("CXXABI_1.3");
195 known_versions.push_back("CXXABI_1.3.1");
196 known_versions.push_back("CXXABI_1.3.2");
197 known_versions.push_back("CXXABI_1.3.3");
198 known_versions.push_back("CXXABI_1.3.4");
199 known_versions.push_back("CXXABI_1.3.5");
200 known_versions.push_back("CXXABI_LDBL_1.3");
202 compat_list::iterator begin = known_versions.begin();
203 compat_list::iterator end = known_versions.end();
205 // Check for compatible version.
206 if (test.version_name.size())
208 compat_list::iterator it1 = find(begin, end, test.version_name);
209 compat_list::iterator it2 = find(begin, end, test.name);
211 test.version_status = symbol::compatible;
213 test.version_status = symbol::incompatible;
215 // Check that added symbols aren't added in the base version.
216 if (added && test.version_name == known_versions[0])
217 test.version_status = symbol::incompatible;
219 // Check that long double compatibility symbols demangled as
220 // __float128 are put into some _LDBL_ version name.
221 if (added && test.demangled_name.find("__float128") != std::string::npos)
223 // Has to be in _LDBL_ version name.
224 if (test.version_name.find("_LDBL_") == std::string::npos)
225 test.version_status = symbol::incompatible;
228 // Check for weak label.
229 if (it1 == end && it2 == end)
230 test.version_status = symbol::incompatible;
235 // version as compatible
242 // New version labels are ok. The rest are not.
243 compat_list::iterator it2 = find(begin, end, test.name);
245 test.version_status = symbol::compatible;
247 test.version_status = symbol::incompatible;
250 return test.version_status == symbol::compatible;
254 check_compatible(symbol& lhs, symbol& rhs, bool verbose)
257 const char tab = '\t';
259 // Check to see if symbol_objects are compatible.
260 if (lhs.type != rhs.type)
264 cout << tab << "incompatible types" << endl;
267 if (lhs.name != rhs.name)
271 cout << tab << "incompatible names" << endl;
274 if (lhs.size != rhs.size)
279 cout << tab << "incompatible sizes" << endl;
280 cout << tab << lhs.size << endl;
281 cout << tab << rhs.size << endl;
285 if (lhs.version_name != rhs.version_name
286 && !check_version(lhs) && !check_version(rhs))
291 cout << tab << "incompatible versions" << endl;
292 cout << tab << lhs.version_name << endl;
293 cout << tab << rhs.version_name << endl;
305 has_symbol(const string& name, const symbols& s) throw()
306 { return s.find(name) != s.end(); }
309 get_symbol(const string& name, const symbols& s)
311 symbols::const_iterator i = s.find(name);
319 os << "get_symbol failed for symbol " << name;
320 __throw_logic_error(os.str().c_str());
325 examine_symbol(const char* name, const char* file)
329 symbols s = create_symbols(file);
330 const symbol& sym = get_symbol(name, s);
334 { __throw_exception_again; }
338 compare_symbols(const char* baseline_file, const char* test_file,
341 // Input both lists of symbols into container.
342 symbols baseline = create_symbols(baseline_file);
343 symbols test = create_symbols(test_file);
345 // Sanity check results.
346 if (!baseline.size() || !test.size())
348 cerr << "Problems parsing the list of exported symbols." << endl;
352 // Check to see if any long double compatibility symbols are produced.
353 bool ld_version_found(false);
354 symbols::iterator li(test.begin());
355 while (!ld_version_found && li != test.end())
357 if (li->second.version_name.find("_LDBL_") != std::string::npos)
358 ld_version_found = true;
363 // Assuming all baseline names and test names are both unique w/ no
366 // The names added to missing_names are baseline names not found in
368 // -> symbols that have been deleted.
370 // The names added to added_names are test names not in
372 // -> symbols that have been added.
373 typedef std::vector<std::string> symbol_names;
374 symbol_names shared_names;
375 symbol_names missing_names;
376 symbol_names added_names;
377 for (li = test.begin(); li != test.end(); ++li)
378 added_names.push_back(li->first);
380 for (symbols::iterator i = baseline.begin(); i != baseline.end(); ++i)
382 string name(i->first);
383 symbol_names::iterator end = added_names.end();
384 symbol_names::iterator it = find(added_names.begin(), end, name);
388 shared_names.push_back(name);
389 added_names.erase(it);
393 // Iff no test long double compatibility symbols at all and the symbol
394 // missing is a baseline long double compatibility symbol, skip.
395 string version_name(i->second.version_name);
396 bool base_ld(version_name.find("_LDBL_") != std::string::npos);
397 if (!base_ld || base_ld && ld_version_found)
398 missing_names.push_back(name);
402 // Fill out list of incompatible symbols.
403 typedef pair<symbol, symbol> symbol_pair;
404 vector<symbol_pair> incompatible;
406 // Check missing names for compatibility.
407 for (size_t j = 0; j < missing_names.size(); ++j)
409 symbol& sbase = baseline[missing_names[j]];
410 sbase.status = symbol::subtracted;
411 incompatible.push_back(symbol_pair(sbase, sbase));
414 // Check shared names for compatibility.
415 const symbol_names::size_type shared_size = shared_names.size();
416 for (size_t k = 0; k < shared_size; ++k)
418 symbol& sbase = baseline[shared_names[k]];
419 symbol& stest = test[shared_names[k]];
420 stest.status = symbol::existing;
421 if (!check_compatible(sbase, stest))
422 incompatible.push_back(symbol_pair(sbase, stest));
425 // Check added names for compatibility.
426 const symbol_names::size_type added_size = added_names.size();
427 for (size_t l = 0; l < added_size; ++l)
429 symbol& stest = test[added_names[l]];
430 stest.status = symbol::added;
431 if (!check_version(stest, true))
432 incompatible.push_back(symbol_pair(stest, stest));
436 if (verbose && added_names.size())
438 cout << endl << added_names.size() << " added symbols " << endl;
439 for (size_t j = 0; j < added_names.size() ; ++j)
442 test[added_names[j]].print();
446 if (verbose && missing_names.size())
448 cout << endl << missing_names.size() << " missing symbols " << endl;
449 for (size_t j = 0; j < missing_names.size() ; ++j)
452 baseline[missing_names[j]].print();
456 if (verbose && incompatible.size())
458 cout << endl << incompatible.size() << " incompatible symbols " << endl;
459 for (size_t j = 0; j < incompatible.size() ; ++j)
461 // First, print index.
464 // Second, report name.
465 symbol& sbase = incompatible[j].first;
466 symbol& stest = incompatible[j].second;
469 // Second, report reason or reasons incompatible.
470 check_compatible(sbase, stest, true);
474 cout << "\n\t\t=== libstdc++-v3 check-abi Summary ===" << endl;
476 cout << "# of added symbols:\t\t " << added_names.size() << endl;
477 cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
478 cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
480 cout << "using: " << baseline_file << endl;
482 return !(missing_names.size() || incompatible.size());
487 create_symbols(const char* file)
493 // Organize file data into an associated container (symbols) of symbol
494 // objects mapped to mangled names without versioning
498 while (getline(ifs, line).good())
509 os << "create_symbols failed for file " << file;
510 __throw_runtime_error(os.str().c_str());
517 demangle(const std::string& mangled)
520 if (mangled[0] != '_' || mangled[1] != 'Z')
522 // This is not a mangled symbol, thus has "C" linkage.
523 name = mangled.c_str();
527 // Use __cxa_demangle to demangle.
529 name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
535 name = "error code = 0: success";
538 name = "error code = -1: memory allocation failure";
541 name = "error code = -2: invalid mangled name";
544 name = "error code = -3: invalid arguments";
547 name = "error code unknown - who knows what happened";