1 // Utility for libstdc++ ABI analysis -*- C++ -*-
3 // Copyright (C) 2002 Free Software Foundation, Inc.
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)
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.
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,
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.
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.
35 #include <ext/hash_map>
41 #include <stdlib.h> // for system(3)
42 #include <unistd.h> // for access(2)
46 enum category { none, function, object, error };
49 std::string name_demangled;
53 symbol_info() : type(none), size(0) { }
55 symbol_info(const symbol_info& other)
56 : type(other.type), name(other.name), name_demangled(other.name_demangled),
57 version(other.version), size(other.size) { }
61 check_compatible(const symbol_info& lhs, const symbol_info& rhs,
66 const char tab = '\t';
68 // Check to see if symbol_infos are compatible.
69 if (lhs.type != rhs.type)
74 cout << tab << "incompatible types" << endl;
78 if (lhs.name != rhs.name)
83 cout << tab << "incompatible names" << endl;
87 if (lhs.size != rhs.size)
92 cout << tab << "incompatible sizes" << endl;
96 // Expect something more sophisticated eventually.
97 if (lhs.version != rhs.version)
102 cout << tab << "incompatible versions" << endl;
109 template<typename _CharT, typename _Traits>
110 std::basic_ostream<_CharT, _Traits>&
111 operator<<(std::basic_ostream<_CharT, _Traits>& os, symbol_info& si)
114 os << si.type << endl;
115 os << si.name << endl;
116 os << si.name_demangled << endl;
117 os << si.version << endl;
118 os << si.size << endl;
123 demangle(const std::string& mangled)
126 if (mangled[0] != '_' && mangled[1] != 'Z')
128 // This is not a mangled symbol, thus has "C" linkage.
129 name = mangled.c_str();
133 // Use __cxa_demangle to demangle.
135 name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
141 name = "error code = 0: success";
144 name = "error code = -1: memory allocation failure";
147 name = "error code = -2: invalid mangled name";
150 name = "error code = -3: invalid arguments";
153 name = "error code unknown - who knows what happened";
161 line_to_symbol_info(std::string& input, symbol_info& output)
164 const char delim = ':';
165 const char version_delim = '@';
166 const string::size_type npos = string::npos;
167 string::size_type n = 0;
170 if (input.find("FUNC") == 0)
171 output.type = symbol_info::function;
172 else if (input.find("OBJECT") == 0)
173 output.type = symbol_info::object;
175 output.type = symbol_info::error;
176 n = input.find_first_of(delim);
178 input.erase(input.begin(), input.begin() + n + 1);
180 // Iff object, get size info.
181 if (output.type == symbol_info::object)
183 n = input.find_first_of(delim);
186 string size(input.begin(), input.begin() + n);
187 istringstream iss(size);
192 input.erase(input.begin(), input.begin() + n + 1);
197 n = input.find_first_of(version_delim);
200 // Found version string.
201 output.name = string(input.begin(), input.begin() + n);
202 n = input.find_last_of(version_delim);
203 input.erase(input.begin(), input.begin() + n + 1);
206 output.version = input;
210 // No versioning info.
211 output.name = string(input.begin(), input.end());
212 input.erase(input.begin(), input.end());
215 // Set the demangled name.
216 output.name_demangled = demangle(output.name);
219 typedef std::deque<std::string> symbol_names;
220 typedef __gnu_cxx::hash_map<const char*, symbol_info> symbol_infos;
223 create_symbol_data(const char* file, symbol_infos& symbols,
226 // Parse list of symbols in file into vectors of symbol_info.
227 // For 3.2.0 on x86/linux, this usually is
228 // 947 non-weak symbols
234 // Organize input into container of symbol_info objects.
237 while (getline(ifs, line).good())
240 line_to_symbol_info(line, symbol);
241 symbols[symbol.name.c_str()] = symbol;
242 names.push_back(symbol.name);
249 report_symbol_info(const symbol_info& symbol, std::size_t n)
252 const char tab = '\t';
253 cout << tab << n << endl;
254 cout << tab << "symbol"<< endl;
255 cout << tab << symbol.name << endl;
257 // Add any other information to display here.
258 cout << tab << "demangled symbol"<< endl;
259 cout << tab << symbol.name_demangled << endl;
266 main(int argc, char** argv)
270 // Get arguments. (Heading towards getopt_long, I can feel it.)
272 if (argc < 4 || (string("--help") == (argv1 = argv[1])))
274 cerr << "Usage: abi_check --check cur baseline\n"
276 "Where CUR is a file containing the current results from\n"
277 "extract_symvers, and BASELINE is one from config/abi.\n"
282 const char* test_file = argv[2];
283 const char* baseline_file = argv[3];
285 // Quick sanity/setup check
286 if (access(test_file, R_OK) != 0)
288 cerr << "Cannot read symbols file " << test_file
289 << ", did you forget to build first?" << endl;
292 if (access(baseline_file, R_OK) != 0)
294 cerr << "Cannot read baseline file " << baseline_file << endl;
298 // Input both lists of symbols into container.
299 symbol_infos baseline_symbols;
300 symbol_names baseline_names;
301 symbol_infos test_symbols;
302 symbol_names test_names;
303 create_symbol_data(baseline_file, baseline_symbols, baseline_names);
304 create_symbol_data(test_file, test_symbols, test_names);
306 // More sanity checking.
307 const symbol_names::size_type baseline_size = baseline_names.size();
308 const symbol_names::size_type test_size = test_names.size();
309 if (!baseline_size || !test_size)
311 cerr << "Problems parsing the list of exported symbols." << endl;
316 // Assuming baseline_names, test_names are both unique w/ no duplicates.
318 // The pairs of names in shared_names are needed to do lookups on
319 // the hash tables of common symbols to do compares.
321 // The names added to missing_names are baseline_names not found in
323 // -> symbols that have been deleted.
325 // The names left in test_names are names not in baseline_names
326 // -> symbols that have been added.
327 typedef pair<string, string> string_pair;
328 vector<string_pair> shared_names;
329 symbol_names missing_names;
330 for (size_t i = 0; i < baseline_size; ++i)
332 symbol_names::iterator end = test_names.end();
333 symbol_names::iterator it = find(test_names.begin(), end,
338 shared_names.push_back(string_pair(baseline_names[i], *it));
339 test_names.erase(it);
342 missing_names.push_back(baseline_names[i]);
345 // Check common names for detailed compatibility.
346 const vector<string_pair>::size_type shared_size = shared_names.size();
347 typedef pair<symbol_info, symbol_info> symbol_pair;
348 vector<symbol_pair> incompatible;
349 for (size_t i = 0; i < shared_size; ++i)
351 symbol_info base = baseline_symbols[shared_names[i].first.c_str()];
352 symbol_info test = test_symbols[shared_names[i].second.c_str()];
353 if (!check_compatible(base, test))
354 incompatible.push_back(symbol_pair(base, test));
358 cout << test_names.size() << " added symbols " << endl;
359 for (size_t j = 0; j < test_names.size() ; ++j)
360 report_symbol_info(test_symbols[test_names[j].c_str()], j + 1);
362 cout << missing_names.size() << " missing symbols " << endl;
363 for (size_t j = 0; j < missing_names.size() ; ++j)
364 report_symbol_info(baseline_symbols[missing_names[j].c_str()], j + 1);
366 cout << incompatible.size() << " incompatible symbols " << endl;
367 for (size_t j = 0; j < incompatible.size() ; ++j)
369 // First, report name.
370 const symbol_info& base = incompatible[j].first;
371 const symbol_info& test = incompatible[j].second;
372 report_symbol_info(test, j + 1);
374 // Second, report reason or reasons incompatible.
375 check_compatible(base, test, true);