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>
33 #include <ext/hash_map>
42 enum category { none, function, object, error };
45 std::string name_demangled;
49 symbol_info() : type(none), size(0) { }
51 symbol_info(const symbol_info& other)
52 : type(other.type), name(other.name), name_demangled(other.name_demangled),
53 version(other.version), size(other.size) { }
57 operator==(const symbol_info& lhs, const symbol_info& rhs)
61 // Check to see if symbol_infos are compatible.
62 ret &= lhs.type == rhs.type;
63 ret &= lhs.name == rhs.name;
64 ret &= lhs.size == rhs.size;
66 // Expect something more sophisticated eventually.
67 ret &= lhs.version == rhs.version;
72 operator!=(const symbol_info& lhs, const symbol_info& rhs)
73 { return !(lhs == rhs); }
75 template<typename _CharT, typename _Traits>
76 std::basic_ostream<_CharT, _Traits>&
77 operator<<(std::basic_ostream<_CharT, _Traits>& os, symbol_info& si)
80 os << si.type << endl;
81 os << si.name << endl;
82 os << si.name_demangled << endl;
83 os << si.version << endl;
84 os << si.size << endl;
89 demangle(const std::string& mangled)
92 if (mangled[0] != '_' && mangled[1] != 'Z')
94 // This is not a mangled symbol, thus has "C" linkage.
95 name = mangled.c_str();
99 // Use __cxa_demangle to demangle.
101 name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
107 name = "error code = 0: success";
110 name = "error code = -1: memory allocation failure";
113 name = "error code = -2: invalid mangled name";
116 name = "error code = -3: invalid arguments";
119 name = "error code unknown - who knows what happened";
127 line_to_symbol_info(std::string& input, symbol_info& output)
130 const char delim = ':';
131 const char version_delim = '@';
132 const string::size_type npos = string::npos;
133 string::size_type n = 0;
136 if (input.find("FUNC") == 0)
137 output.type = symbol_info::function;
138 else if (input.find("OBJECT") == 0)
139 output.type = symbol_info::object;
141 output.type = symbol_info::error;
142 n = input.find_first_of(delim);
144 input.erase(input.begin(), input.begin() + n + 1);
146 // Iff object, get size info.
147 if (output.type == symbol_info::object)
149 n = input.find_first_of(delim);
152 string size(input.begin(), input.begin() + n);
153 istringstream iss(size);
158 input.erase(input.begin(), input.begin() + n + 1);
163 n = input.find_first_of(version_delim);
166 // Found version string.
167 output.name = string(input.begin(), input.begin() + n);
168 n = input.find_last_of(version_delim);
169 input.erase(input.begin(), input.begin() + n + 1);
172 output.version = input;
176 // No versioning info.
177 output.name = string(input.begin(), input.end());
178 input.erase(input.begin(), input.end());
181 // Set the demangled name.
182 output.name_demangled = demangle(output.name);
185 typedef std::deque<std::string> symbol_names;
186 typedef __gnu_cxx::hash_map<const char*, symbol_info> symbol_infos;
189 collect_symbol_data(const char* file, symbol_infos& symbols,
192 // Parse list of symbols in file into vectors of symbol_info.
193 // For 3.2.0 on x86/linux, this usually is
194 // 947 non-weak symbols
200 // Organize input into container of symbol_info objects.
203 while (getline(ifs, line).good())
206 line_to_symbol_info(line, symbol);
207 symbols[symbol.name.c_str()] = symbol;
208 names.push_back(symbol.name);
215 int main(int argc, char** argv)
222 cerr << "Usage: abi_check baseline_file" << endl;
225 const char* baseline_file = argv[1];
226 const char* test_file = "current_symbols.txt";
227 const char* test_lib = "../src/.libs/libstdc++.so";
229 // Get list of symbols.
230 // Assume external symbol list computed "as if" by
232 readelf -s -W libstdc++.so | sed '/\.dynsym/,/^$/p;d' | egrep -v
233 ' (LOCAL|UND) ' | awk '{ if ($4 == "FUNC" || $4 == "NOTYPE") printf
234 "%s:%s\n", $4, $8; else if ($4 == "OBJECT") printf "%s:%s:%s\n", $4,
235 $3, $8;}' | sort >& current_symbols.txt
237 const char quote = '"';
238 const char bslash = '\\';
240 cmd << "readelf -s -W " << test_lib << " | sed '/" << bslash
241 << ".dynsym/,/^$/p;d' | egrep -v ' (LOCAL|UND) ' | "
242 << "awk '{ if ($4 == " << quote << "FUNC" << quote << "|| $4 == "
243 << quote << "NOTYPE" << quote << ") printf " << quote << "%s:%s"
244 << bslash << "n" << quote << ", $4, $8; else if ($4 == "
245 << quote << "OBJECT" << quote << ") printf " << quote
246 << "%s:%s:%s" << bslash << "n" << quote << ", $4, $3, $8;}' | "
247 << "sort >& " << test_file;
248 if (system(cmd.str().c_str()) != 0)
250 cerr << "Unable to generate the list of exported symbols." << endl;
254 // Input both list of symbols into container.
255 symbol_infos baseline_symbols;
256 symbol_names baseline_names;
257 symbol_infos test_symbols;
258 symbol_names test_names;
259 collect_symbol_data(baseline_file, baseline_symbols, baseline_names);
260 collect_symbol_data(test_file, test_symbols, test_names);
262 // Basic sanity check. (Was: error checking, what's that?)
263 const symbol_names::size_type baseline_size = baseline_names.size();
264 const symbol_names::size_type test_size = test_names.size();
265 if (!baseline_size || !test_size)
267 cerr << "Problems parsing the list of exported symbols." << endl;
272 // Assuming baseline_names, test_names are both unique w/ no duplicates.
274 // The pairs of names in shared_names are needed to do lookups on
275 // the hash tables of common symbols to do compares.
277 // The names added to missing_names are baseline_names not found in
279 // -> symbols that have been deleted.
281 // The names left in test_names are names not in baseline_names
282 // -> symbols that have been added.
283 typedef pair<string, string> string_pair;
284 vector<string_pair> shared_names;
285 symbol_names missing_names;
286 for (size_t i = 0; i < baseline_size; ++i)
288 symbol_names::iterator end = test_names.end();
289 symbol_names::iterator it = find(test_names.begin(), end,
294 shared_names.push_back(string_pair(baseline_names[i], *it));
295 test_names.erase(it);
298 missing_names.push_back(baseline_names[i]);
301 // Check common names for detailed compatibility.
302 const vector<string_pair>::size_type shared_size = shared_names.size();
303 typedef pair<symbol_info, symbol_info> symbol_pair;
304 vector<symbol_pair> incompatible;
305 for (size_t i = 0; i < shared_size; ++i)
307 symbol_info binfo = baseline_symbols[shared_names[i].first.c_str()];
308 symbol_info tinfo = test_symbols[shared_names[i].second.c_str()];
310 incompatible.push_back(symbol_pair(binfo, tinfo));
314 cout << test_names.size() << " added symbols " << endl;
315 for (size_t j = 0; j < test_names.size() ; ++j)
316 cout << '\t' << test_names[j] << endl;
318 cout << missing_names.size() << " missing symbols " << endl;
319 for (size_t j = 0; j < missing_names.size() ; ++j)
320 cout << '\t' << missing_names[j] << endl;
322 cout << incompatible.size() << " incompatible symbols " << endl;
323 for (size_t j = 0; j < incompatible.size() ; ++j)
325 cout << "baseline symbol_info:" << endl;
326 cout << incompatible[j].first << endl;
327 cout << "test symbol_info:" << endl;
328 cout << incompatible[j].second << endl;