Changeset 6e0f49a in rtems-tools


Ignore:
Timestamp:
Feb 11, 2019, 5:25:20 AM (5 days ago)
Author:
Chris Johns <chrisj@…>
Branches:
master
Children:
4828c19
Parents:
7f85a65
git-author:
Chris Johns <chrisj@…> (02/11/19 05:25:20)
git-committer:
Chris Johns <chrisj@…> (02/11/19 05:42:53)
Message:

linkers/syms: Add symbol filtering.

  • Filter symbols by regx.
  • Return the name of a symbol via demangled() even it is not a mangled C++ function.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • linkers/rtems-syms.cpp

    r7f85a65 r6e0f49a  
    3030#include <iomanip>
    3131#include <iostream>
     32#include <regex>
    3233
    3334#include <cxxabi.h>
     
    144145
    145146/**
     147 * Filter the symbols given a list of regx expressions.
     148 */
     149class symbol_filter
     150{
     151public:
     152  typedef std::vector < std::string > expressions;
     153
     154  symbol_filter ();
     155
     156  void load (const std::string& file);
     157  void add (const std::string& re);
     158
     159  void filter (const rld::symbols::symtab& symbols,
     160               rld::symbols::symtab&       filtered_symbols);
     161
     162private:
     163
     164  expressions expr;
     165};
     166
     167symbol_filter::symbol_filter ()
     168{
     169}
     170
     171void
     172symbol_filter::load (const std::string& file)
     173{
     174  std::ifstream in (file);
     175  std::string   re;
     176  while (in >> re)
     177    add (re);
     178}
     179
     180void
     181symbol_filter::add (const std::string& re)
     182{
     183  expr.push_back (re);
     184}
     185
     186void
     187symbol_filter::filter (const rld::symbols::symtab& symbols,
     188                       rld::symbols::symtab&       filtered_symbols)
     189{
     190  if (expr.size () > 0)
     191  {
     192    for (auto& re : expr)
     193    {
     194      const std::regex sym_re(re);
     195      for (const auto& sym : symbols)
     196      {
     197        std::smatch      m;
     198        if (std::regex_match (sym.second->demangled (), m, sym_re))
     199          filtered_symbols[sym.first] = sym.second;
     200      }
     201    }
     202  }
     203  else
     204  {
     205    for (const auto& sym : symbols)
     206      filtered_symbols[sym.first] = sym.second;
     207  }
     208}
     209
     210/**
    146211 * Generate the symbol map object file for loading or linking into
    147212 * a running RTEMS machine.
     
    193258static void
    194259generate_c (rld::process::tempfile& c,
    195             rld::symbols::table&    symbols,
     260            rld::symbols::symtab&   symbols,
    196261            bool                    embed)
    197262{
     
    199264
    200265  /*
    201    * Add the global symbols.
     266   * Add the symbols. The is the globals and the weak symbols that have been
     267   * linked into the base image. A weak symbols present in the base image is no
     268   * longer weak and should be consider a global symbol. You cannot link a
     269   * global symbol with the same in a dynamically loaded module.
    202270   */
    203   std::for_each (symbols.globals ().begin (),
    204                  symbols.globals ().end (),
     271  std::for_each (symbols.begin (),
     272                 symbols.end (),
    205273                 output_sym (c, embed, false));
    206 
    207   /*
    208    * Add the weak symbols that have been linked into the base image. A weak
    209    * symbols present in the base image is no longer weak and should be consider
    210    * a global symbol. You cannot link a global symbol with the same in a
    211    * dynamically loaded module.
    212    */
    213   std::for_each (symbols.weaks ().begin (),
    214                  symbols.weaks ().end (),
    215                  output_sym (c, embed, true));
    216274
    217275  temporary_file_paint (c, c_trailer);
     
    226284generate_symmap (rld::process::tempfile& c,
    227285                 const std::string&      output,
    228                  rld::symbols::table&    symbols,
     286                 rld::symbols::symtab&   symbols,
    229287                 bool                    embed)
    230288{
     
    284342  { "exec-prefix", required_argument,      NULL,           'E' },
    285343  { "cflags",      required_argument,      NULL,           'c' },
     344  { "filter",      required_argument,      NULL,           'f' },
     345  { "filter-re",   required_argument,      NULL,           'F' },
    286346  { NULL,          0,                      NULL,            0 }
    287347};
     
    302362            << " -o file   : output object file (also --output)" << std::endl
    303363            << " -m file   : output a map file (also --map)" << std::endl
    304             << " -C file   : execute file as the target C compiler (also --cc)" << std::endl
     364            << " -C file   : target C compiler executable (also --cc)" << std::endl
    305365            << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl
    306             << " -c cflags : C compiler flags (also --cflags)" << std::endl;
     366            << " -c cflags : C compiler flags (also --cflags)" << std::endl
     367            << " -f file   : file of symbol filters (also --filter)" << std::endl
     368            << " -F re     : filter regx expression (also --filter-re)" << std::endl;
    307369  ::exit (exit_code);
    308370}
     
    353415    rld::files::cache   kernel;
    354416    rld::symbols::table symbols;
     417    symbol_filter       filter;
    355418    std::string         kernel_name;
    356419    std::string         output;
     
    364427    while (true)
    365428    {
    366       int opt = ::getopt_long (argc, argv, "hvVwkef:S:o:m:E:c:C:", rld_opts, NULL);
     429      int opt = ::getopt_long (argc, argv, "hvVwkef:S:o:m:E:c:C:f:F:", rld_opts, NULL);
    367430      if (opt < 0)
    368431        break;
     
    418481        case 'S':
    419482          symc = optarg;
     483          break;
     484
     485        case 'f':
     486          filter.load (optarg);
     487          break;
     488
     489        case 'F':
     490          filter.add (optarg);
    420491          break;
    421492
     
    478549      if (!rld::cc::is_cc_set () && !rld::cc::is_exec_prefix_set ())
    479550        rld::cc::set_exec_prefix (rld::elf::machine_type ());
     551
     552      /*
     553       * Filter the symbols.
     554       */
     555      rld::symbols::symtab filter_symbols;
     556      filter.filter (symbols.globals (), filter_symbols);
     557      filter.filter (symbols.weaks (), filter_symbols);
     558      if (filter_symbols.size () == 0)
     559        throw rld::error ("no filtered symbols", "filter");
     560      if (rld::verbose ())
     561        std::cout << "Filtered symbols: " << filter_symbols.size () << std::endl;
    480562
    481563      /*
     
    491573             << " kernel: " << kernel_name << std::endl
    492574             << std::endl;
    493         rld::symbols::output (mout, symbols);
     575        rld::symbols::output (mout, filter_symbols);
    494576        mout.close ();
    495577      }
     
    511593         * Generate and compile the symbol map.
    512594         */
    513         generate_symmap (c, output, symbols, embed);
     595        generate_symmap (c, output, filter_symbols, embed);
    514596      }
    515597
  • rtemstoolkit/rld-symbols.cpp

    r7f85a65 r6e0f49a  
    8989      if (!object_)
    9090        throw rld_error_at ("object pointer is 0");
    91       demangle_name (name_, demangled_);
     91      if (!demangle_name (name_, demangled_))
     92        demangled_ = name_;
    9293    }
    9394
     
    101102        references_ (0)
    102103    {
    103       demangle_name (name_, demangled_);
     104      if (!demangle_name (name_, demangled_))
     105        demangled_ = name_;
    104106    }
    105107
Note: See TracChangeset for help on using the changeset viewer.