]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/abi_check.cc
Move from CPP to CXX.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / abi_check.cc
1 // Utility for libstdc++ ABI analysis -*- C++ -*-
2
3 // Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // Benjamin Kosnik <bkoz@redhat.com>
31 // Blame subsequent hacks on Loren J. Rittle <ljrittle@acm.org>, Phil
32 // Edwards <pme@gcc.gnu.org>, and a cast of dozens at libstdc++@gcc.gnu.org.
33
34 #include <string>
35 #include <ext/hash_map>
36 #include <deque>
37 #include <sstream>
38 #include <fstream>
39 #include <iostream>
40 #include <cxxabi.h>
41 #include <stdlib.h> // for system(3)
42 #include <unistd.h> // for access(2)
43
44 struct symbol_info
45 {
46 enum category { none, function, object, error };
47 category type;
48 std::string name;
49 std::string demangled_name;
50 int size;
51 std::string version_name;
52
53 symbol_info() : type(none), size(0) { }
54
55 symbol_info(const symbol_info& other)
56 : type(other.type), name(other.name), demangled_name(other.demangled_name),
57 size(other.size), version_name(other.version_name) { }
58 };
59
60 namespace __gnu_cxx
61 {
62 using namespace std;
63
64 template<>
65 struct hash<string>
66 {
67 size_t operator()(const string& s) const
68 {
69 const collate<char>& c = use_facet<collate<char> >(locale::classic());
70 return c.hash(s.c_str(), s.c_str() + s.size());
71 }
72 };
73 }
74
75 typedef std::deque<std::string> symbol_names;
76 typedef __gnu_cxx::hash_map<std::string, symbol_info> symbol_infos;
77
78
79 bool
80 check_version(const symbol_info& test, bool added = false)
81 {
82 typedef std::vector<std::string> compat_list;
83 static compat_list known_versions;
84 if (known_versions.empty())
85 {
86 known_versions.push_back("GLIBCPP_3.2"); // base version
87 known_versions.push_back("GLIBCPP_3.2.1");
88 known_versions.push_back("GLIBCPP_3.2.2");
89 known_versions.push_back("GLIBCPP_3.2.3"); // gcc-3.3.0
90 known_versions.push_back("GLIBCXX_3.4");
91 known_versions.push_back("CXXABI_1.2");
92 known_versions.push_back("CXXABI_1.2.1");
93 known_versions.push_back("CXXABI_1.3");
94 }
95 compat_list::iterator begin = known_versions.begin();
96 compat_list::iterator end = known_versions.end();
97
98 // Check version names for compatibility...
99 compat_list::iterator it1 = find(begin, end, test.version_name);
100
101 // Check for weak label.
102 compat_list::iterator it2 = find(begin, end, test.name);
103
104 // Check that added symbols aren't added in the base version.
105 bool compat = true;
106 if (added && test.version_name == known_versions[0])
107 compat = false;
108
109 if (it1 == end && it2 == end)
110 compat = false;
111
112 return compat;
113 }
114
115 bool
116 check_compatible(const symbol_info& lhs, const symbol_info& rhs,
117 bool verbose = false)
118 {
119 using namespace std;
120 bool ret = true;
121 const char tab = '\t';
122
123 // Check to see if symbol_infos are compatible.
124 if (lhs.type != rhs.type)
125 {
126 ret = false;
127 if (verbose)
128 {
129 cout << tab << "incompatible types" << endl;
130 }
131 }
132
133 if (lhs.name != rhs.name)
134 {
135 ret = false;
136 if (verbose)
137 {
138 cout << tab << "incompatible names" << endl;
139 }
140 }
141
142 if (lhs.size != rhs.size)
143 {
144 ret = false;
145 if (verbose)
146 {
147 cout << tab << "incompatible sizes" << endl;
148 cout << tab << lhs.size << endl;
149 cout << tab << rhs.size << endl;
150 }
151 }
152
153 if (lhs.version_name != rhs.version_name
154 && !check_version(lhs) && !check_version(rhs))
155 {
156 ret = false;
157 if (verbose)
158 {
159 cout << tab << "incompatible versions" << endl;
160 cout << tab << lhs.version_name << endl;
161 cout << tab << rhs.version_name << endl;
162 }
163 }
164
165 if (verbose)
166 cout << endl;
167
168 return ret;
169 }
170
171 const char*
172 demangle(const std::string& mangled)
173 {
174 const char* name;
175 if (mangled[0] != '_' || mangled[1] != 'Z')
176 {
177 // This is not a mangled symbol, thus has "C" linkage.
178 name = mangled.c_str();
179 }
180 else
181 {
182 // Use __cxa_demangle to demangle.
183 int status = 0;
184 name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
185 if (!name)
186 {
187 switch (status)
188 {
189 case 0:
190 name = "error code = 0: success";
191 break;
192 case -1:
193 name = "error code = -1: memory allocation failure";
194 break;
195 case -2:
196 name = "error code = -2: invalid mangled name";
197 break;
198 case -3:
199 name = "error code = -3: invalid arguments";
200 break;
201 default:
202 name = "error code unknown - who knows what happened";
203 }
204 }
205 }
206 return name;
207 }
208
209 void
210 line_to_symbol_info(std::string& input, symbol_info& output)
211 {
212 using namespace std;
213 const char delim = ':';
214 const char version_delim = '@';
215 const string::size_type npos = string::npos;
216 string::size_type n = 0;
217
218 // Set the type.
219 if (input.find("FUNC") == 0)
220 output.type = symbol_info::function;
221 else if (input.find("OBJECT") == 0)
222 output.type = symbol_info::object;
223 else
224 output.type = symbol_info::error;
225 n = input.find_first_of(delim);
226 if (n != npos)
227 input.erase(input.begin(), input.begin() + n + 1);
228
229 // Iff object, get size info.
230 if (output.type == symbol_info::object)
231 {
232 n = input.find_first_of(delim);
233 if (n != npos)
234 {
235 string size(input.begin(), input.begin() + n);
236 istringstream iss(size);
237 int x;
238 iss >> x;
239 if (!iss.fail())
240 output.size = x;
241 input.erase(input.begin(), input.begin() + n + 1);
242 }
243 }
244
245 // Set the name.
246 n = input.find_first_of(version_delim);
247 if (n != npos)
248 {
249 // Found version string.
250 output.name = string(input.begin(), input.begin() + n);
251 n = input.find_last_of(version_delim);
252 input.erase(input.begin(), input.begin() + n + 1);
253
254 // Set version name.
255 output.version_name = input;
256 }
257 else
258 {
259 // No versioning info.
260 output.name = string(input.begin(), input.end());
261 input.erase(input.begin(), input.end());
262 }
263
264 // Set the demangled name.
265 output.demangled_name = demangle(output.name);
266 }
267
268 void
269 create_symbol_data(const char* file, symbol_infos& symbols,
270 symbol_names& names)
271 {
272 // Parse list of symbols in file into vectors of symbol_info.
273 // For 3.2.0 on x86/linux, this usually is
274 // 947 non-weak symbols
275 // 2084 weak symbols
276 using namespace std;
277 ifstream ifs(file);
278 if (ifs.is_open())
279 {
280 // Organize input into container of symbol_info objects.
281 const string empty;
282 string line = empty;
283 while (getline(ifs, line).good())
284 {
285 symbol_info symbol;
286 line_to_symbol_info(line, symbol);
287 symbols[symbol.name] = symbol;
288 names.push_back(symbol.name);
289 line = empty;
290 }
291 }
292 }
293
294 void
295 report_symbol_info(const symbol_info& symbol, std::size_t n, bool ret = true)
296 {
297 using namespace std;
298 const char tab = '\t';
299
300 // Add any other information to display here.
301 cout << tab << symbol.demangled_name << endl;
302 cout << tab << symbol.name << endl;
303 cout << tab << symbol.version_name << endl;
304
305 if (ret)
306 cout << endl;
307 }
308
309
310 int
311 main(int argc, char** argv)
312 {
313 using namespace std;
314
315 // Get arguments. (Heading towards getopt_long, I can feel it.)
316 bool verbose = false;
317 string argv1 = argc > 1 ? argv[1] : "";
318 if (argv1 == "--help" || argc < 4)
319 {
320 cerr << "usage: abi_check --check current baseline\n"
321 " --check-verbose current baseline\n"
322 " --help\n\n"
323 "Where CURRENT is a file containing the current results from\n"
324 "extract_symvers, and BASELINE is one from config/abi.\n"
325 << endl;
326 exit(1);
327 }
328 else if (argv1 == "--check-verbose")
329 verbose = true;
330
331 // Quick sanity/setup check for arguments.
332 const char* test_file = argv[2];
333 const char* baseline_file = argv[3];
334 if (access(test_file, R_OK) != 0)
335 {
336 cerr << "Cannot read symbols file " << test_file
337 << ", did you forget to build first?" << endl;
338 exit(1);
339 }
340 if (access(baseline_file, R_OK) != 0)
341 {
342 cerr << "Cannot read baseline file " << baseline_file << endl;
343 exit(1);
344 }
345
346 // Input both lists of symbols into container.
347 symbol_infos baseline_symbols;
348 symbol_names baseline_names;
349 symbol_infos test_symbols;
350 symbol_names test_names;
351 create_symbol_data(baseline_file, baseline_symbols, baseline_names);
352 create_symbol_data(test_file, test_symbols, test_names);
353
354 // Sanity check results.
355 const symbol_names::size_type baseline_size = baseline_names.size();
356 const symbol_names::size_type test_size = test_names.size();
357 if (!baseline_size || !test_size)
358 {
359 cerr << "Problems parsing the list of exported symbols." << endl;
360 exit(2);
361 }
362
363 // Sort out names.
364 // Assuming baseline_names, test_names are both unique w/ no duplicates.
365 //
366 // The names added to missing_names are baseline_names not found in
367 // test_names
368 // -> symbols that have been deleted.
369 //
370 // The names added to added_names are test_names are names not in
371 // baseline_names
372 // -> symbols that have been added.
373 symbol_names shared_names;
374 symbol_names missing_names;
375 symbol_names added_names = test_names;
376 for (size_t i = 0; i < baseline_size; ++i)
377 {
378 string what(baseline_names[i]);
379 symbol_names::iterator end = added_names.end();
380 symbol_names::iterator it = find(added_names.begin(), end, what);
381 if (it != end)
382 {
383 // Found.
384 shared_names.push_back(what);
385 added_names.erase(it);
386 }
387 else
388 missing_names.push_back(what);
389 }
390
391 // Check missing names for compatibility.
392 typedef pair<symbol_info, symbol_info> symbol_pair;
393 vector<symbol_pair> incompatible;
394 for (size_t i = 0; i < missing_names.size(); ++i)
395 {
396 symbol_info base = baseline_symbols[missing_names[i]];
397 incompatible.push_back(symbol_pair(base, base));
398 }
399
400 // Check shared names for compatibility.
401 for (size_t i = 0; i < shared_names.size(); ++i)
402 {
403 symbol_info base = baseline_symbols[shared_names[i]];
404 symbol_info test = test_symbols[shared_names[i]];
405 if (!check_compatible(base, test))
406 incompatible.push_back(symbol_pair(base, test));
407 }
408
409 // Check added names for compatibility.
410 for (size_t i = 0; i < added_names.size(); ++i)
411 {
412 symbol_info test = test_symbols[added_names[i]];
413 if (!check_version(test, true))
414 incompatible.push_back(symbol_pair(test, test));
415 }
416
417 // Report results.
418 if (verbose && added_names.size())
419 {
420 cout << added_names.size() << " added symbols " << endl;
421 for (size_t j = 0; j < added_names.size() ; ++j)
422 report_symbol_info(test_symbols[added_names[j]], j + 1);
423 }
424
425 if (verbose && missing_names.size())
426 {
427 cout << missing_names.size() << " missing symbols " << endl;
428 for (size_t j = 0; j < missing_names.size() ; ++j)
429 report_symbol_info(baseline_symbols[missing_names[j]], j + 1);
430 }
431
432 if (verbose && incompatible.size())
433 {
434 cout << incompatible.size() << " incompatible symbols " << endl;
435 for (size_t j = 0; j < incompatible.size() ; ++j)
436 {
437 // First, report name.
438 const symbol_info& base = incompatible[j].first;
439 const symbol_info& test = incompatible[j].second;
440 report_symbol_info(test, j + 1, false);
441
442 // Second, report reason or reasons incompatible.
443 check_compatible(base, test, true);
444 }
445 }
446
447 cout << "\n\t\t=== libstdc++-v3 check-abi Summary ===" << endl;
448 cout << endl;
449 cout << "# of added symbols:\t\t " << added_names.size() << endl;
450 cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
451 cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
452 cout << endl;
453 cout << "using: " << baseline_file << endl;
454
455 return 0;
456 }