3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
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.
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.
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/>.
20 // Benjamin Kosnik <bkoz@redhat.com>
22 #include "testsuite_abi.h"
33 symbol::init(string& data)
35 const char delim = ':';
36 const char version_delim = '@';
37 const string::size_type npos = string::npos;
38 string::size_type n = 0;
41 if (data.find("FUNC") == 0)
42 type = symbol::function;
43 else if (data.find("OBJECT") == 0)
44 type = symbol::object;
46 n = data.find_first_of(delim);
48 data.erase(data.begin(), data.begin() + n + 1);
50 // Iff object, get size info.
51 if (type == symbol::object)
53 n = data.find_first_of(delim);
56 string objectsize(data.begin(), data.begin() + n);
57 istringstream iss(objectsize);
62 data.erase(data.begin(), data.begin() + n + 1);
66 // Set the name and raw_name.
67 raw_name = string(data.begin(), data.end());
68 n = data.find_first_of(version_delim);
71 // Found version string.
72 name = string(data.begin(), data.begin() + n);
73 n = data.find_last_of(version_delim);
74 data.erase(data.begin(), data.begin() + n + 1);
81 // No versioning info.
82 name = string(data.begin(), data.end());
83 version_status = symbol::none;
86 // Set the demangled name.
87 demangled_name = demangle(name);
93 const char tab = '\t';
96 if (demangled_name != name)
97 cout << demangled_name << endl;
100 switch (version_status)
109 vers = "incompatible";
112 vers = "unversioned";
117 cout << "version status: " << vers << endl;
119 if (version_name.size()
120 && (version_status == compatible || version_status == incompatible))
121 cout << version_name << endl;
127 type_string = "function";
130 type_string = "object";
133 type_string = "uncategorized";
136 type_string = "<default>";
138 cout << "type: " << type_string << endl;
141 cout << "type size: " << size << endl;
143 string status_string;
147 status_string = "added";
150 status_string = "subtracted";
153 status_string = "undesignated";
156 status_string = "<default>";
158 cout << "status: " << status_string << endl;
165 check_version(symbol& test, bool added)
167 // Construct list of compatible versions.
168 typedef std::vector<std::string> compat_list;
169 static compat_list known_versions;
170 if (known_versions.empty())
172 // NB: First version here must be the default version for this
173 // version of DT_SONAME.
174 known_versions.push_back("GLIBCXX_3.4");
175 known_versions.push_back("GLIBCXX_3.4.1");
176 known_versions.push_back("GLIBCXX_3.4.2");
177 known_versions.push_back("GLIBCXX_3.4.3");
178 known_versions.push_back("GLIBCXX_3.4.4");
179 known_versions.push_back("GLIBCXX_3.4.5");
180 known_versions.push_back("GLIBCXX_3.4.6");
181 known_versions.push_back("GLIBCXX_3.4.7");
182 known_versions.push_back("GLIBCXX_3.4.8");
183 known_versions.push_back("GLIBCXX_3.4.9");
184 known_versions.push_back("GLIBCXX_3.4.10");
185 known_versions.push_back("GLIBCXX_3.4.11");
186 known_versions.push_back("GLIBCXX_3.4.12");
187 known_versions.push_back("GLIBCXX_3.4.13");
188 known_versions.push_back("GLIBCXX_3.4.14");
189 known_versions.push_back("GLIBCXX_LDBL_3.4");
190 known_versions.push_back("GLIBCXX_LDBL_3.4.7");
191 known_versions.push_back("GLIBCXX_LDBL_3.4.10");
192 known_versions.push_back("CXXABI_1.3");
193 known_versions.push_back("CXXABI_1.3.1");
194 known_versions.push_back("CXXABI_1.3.2");
195 known_versions.push_back("CXXABI_1.3.3");
196 known_versions.push_back("CXXABI_1.3.4");
197 known_versions.push_back("CXXABI_1.3.5");
198 known_versions.push_back("CXXABI_LDBL_1.3");
200 compat_list::iterator begin = known_versions.begin();
201 compat_list::iterator end = known_versions.end();
203 // Check for compatible version.
204 if (test.version_name.size())
206 compat_list::iterator it1 = find(begin, end, test.version_name);
207 compat_list::iterator it2 = find(begin, end, test.name);
209 test.version_status = symbol::compatible;
211 test.version_status = symbol::incompatible;
213 // Check that added symbols aren't added in the base version.
214 if (added && test.version_name == known_versions[0])
215 test.version_status = symbol::incompatible;
217 // Check that long double compatibility symbols demangled as
218 // __float128 are put into some _LDBL_ version name.
219 if (added && test.demangled_name.find("__float128") != std::string::npos)
221 // Has to be in _LDBL_ version name.
222 if (test.version_name.find("_LDBL_") == std::string::npos)
223 test.version_status = symbol::incompatible;
226 // Check for weak label.
227 if (it1 == end && it2 == end)
228 test.version_status = symbol::incompatible;
233 // version as compatible
240 // New version labels are ok. The rest are not.
241 compat_list::iterator it2 = find(begin, end, test.name);
243 test.version_status = symbol::compatible;
245 test.version_status = symbol::incompatible;
248 return test.version_status == symbol::compatible;
252 check_compatible(symbol& lhs, symbol& rhs, bool verbose)
255 const char tab = '\t';
257 // Check to see if symbol_objects are compatible.
258 if (lhs.type != rhs.type)
262 cout << tab << "incompatible types" << endl;
265 if (lhs.name != rhs.name)
269 cout << tab << "incompatible names" << endl;
272 if (lhs.size != rhs.size)
277 cout << tab << "incompatible sizes" << endl;
278 cout << tab << lhs.size << endl;
279 cout << tab << rhs.size << endl;
283 if (lhs.version_name != rhs.version_name
284 && !check_version(lhs) && !check_version(rhs))
289 cout << tab << "incompatible versions" << endl;
290 cout << tab << lhs.version_name << endl;
291 cout << tab << rhs.version_name << endl;
303 has_symbol(const string& name, const symbols& s) throw()
304 { return s.find(name) != s.end(); }
307 get_symbol(const string& name, const symbols& s)
309 symbols::const_iterator i = s.find(name);
317 os << "get_symbol failed for symbol " << name;
318 __throw_logic_error(os.str().c_str());
323 examine_symbol(const char* name, const char* file)
327 symbols s = create_symbols(file);
328 const symbol& sym = get_symbol(name, s);
332 { __throw_exception_again; }
336 compare_symbols(const char* baseline_file, const char* test_file,
339 // Input both lists of symbols into container.
340 symbols baseline = create_symbols(baseline_file);
341 symbols test = create_symbols(test_file);
343 // Sanity check results.
344 if (!baseline.size() || !test.size())
346 cerr << "Problems parsing the list of exported symbols." << endl;
350 // Check to see if any long double compatibility symbols are produced.
351 bool ld_version_found(false);
352 symbols::iterator li(test.begin());
353 while (!ld_version_found && li != test.end())
355 if (li->second.version_name.find("_LDBL_") != std::string::npos)
356 ld_version_found = true;
361 // Assuming all baseline names and test names are both unique w/ no
364 // The names added to missing_names are baseline names not found in
366 // -> symbols that have been deleted.
368 // The names added to added_names are test names not in
370 // -> symbols that have been added.
371 typedef std::vector<std::string> symbol_names;
372 symbol_names shared_names;
373 symbol_names missing_names;
374 symbol_names added_names;
375 for (li = test.begin(); li != test.end(); ++li)
376 added_names.push_back(li->first);
378 for (symbols::iterator i = baseline.begin(); i != baseline.end(); ++i)
380 string name(i->first);
381 symbol_names::iterator end = added_names.end();
382 symbol_names::iterator it = find(added_names.begin(), end, name);
386 shared_names.push_back(name);
387 added_names.erase(it);
391 // Iff no test long double compatibility symbols at all and the symbol
392 // missing is a baseline long double compatibility symbol, skip.
393 string version_name(i->second.version_name);
394 bool base_ld(version_name.find("_LDBL_") != std::string::npos);
395 if (!base_ld || base_ld && ld_version_found)
396 missing_names.push_back(name);
400 // Fill out list of incompatible symbols.
401 typedef pair<symbol, symbol> symbol_pair;
402 vector<symbol_pair> incompatible;
404 // Check missing names for compatibility.
405 for (size_t j = 0; j < missing_names.size(); ++j)
407 symbol& sbase = baseline[missing_names[j]];
408 sbase.status = symbol::subtracted;
409 incompatible.push_back(symbol_pair(sbase, sbase));
412 // Check shared names for compatibility.
413 const symbol_names::size_type shared_size = shared_names.size();
414 for (size_t k = 0; k < shared_size; ++k)
416 symbol& sbase = baseline[shared_names[k]];
417 symbol& stest = test[shared_names[k]];
418 stest.status = symbol::existing;
419 if (!check_compatible(sbase, stest))
420 incompatible.push_back(symbol_pair(sbase, stest));
423 // Check added names for compatibility.
424 const symbol_names::size_type added_size = added_names.size();
425 for (size_t l = 0; l < added_size; ++l)
427 symbol& stest = test[added_names[l]];
428 stest.status = symbol::added;
429 if (!check_version(stest, true))
430 incompatible.push_back(symbol_pair(stest, stest));
434 if (verbose && added_names.size())
436 cout << endl << added_names.size() << " added symbols " << endl;
437 for (size_t j = 0; j < added_names.size() ; ++j)
440 test[added_names[j]].print();
444 if (verbose && missing_names.size())
446 cout << endl << missing_names.size() << " missing symbols " << endl;
447 for (size_t j = 0; j < missing_names.size() ; ++j)
450 baseline[missing_names[j]].print();
454 if (verbose && incompatible.size())
456 cout << endl << incompatible.size() << " incompatible symbols " << endl;
457 for (size_t j = 0; j < incompatible.size() ; ++j)
459 // First, print index.
462 // Second, report name.
463 symbol& sbase = incompatible[j].first;
464 symbol& stest = incompatible[j].second;
467 // Second, report reason or reasons incompatible.
468 check_compatible(sbase, stest, true);
472 cout << "\n\t\t=== libstdc++-v3 check-abi Summary ===" << endl;
474 cout << "# of added symbols:\t\t " << added_names.size() << endl;
475 cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
476 cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
478 cout << "using: " << baseline_file << endl;
480 return !(missing_names.size() || incompatible.size());
485 create_symbols(const char* file)
491 // Organize file data into an associated container (symbols) of symbol
492 // objects mapped to mangled names without versioning
496 while (getline(ifs, line).good())
507 os << "create_symbols failed for file " << file;
508 __throw_runtime_error(os.str().c_str());
515 demangle(const std::string& mangled)
518 if (mangled[0] != '_' || mangled[1] != 'Z')
520 // This is not a mangled symbol, thus has "C" linkage.
521 name = mangled.c_str();
525 // Use __cxa_demangle to demangle.
527 name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
533 name = "error code = 0: success";
536 name = "error code = -1: memory allocation failure";
539 name = "error code = -2: invalid mangled name";
542 name = "error code = -3: invalid arguments";
545 name = "error code unknown - who knows what happened";