source: rtems-tools/linkers/rtems-exeinfo.cpp @ 1d4edfb

Last change on this file since 1d4edfb was 1d4edfb, checked in by Ryan Long <ryan.long@…>, on 08/18/21 at 13:34:49

rtems-exeinfo.cpp: Initialize byteorder

CID 1471637: Uninitialized scalar field

Closes #4499

  • Property mode set to 100644
File size: 32.6 KB
RevLine 
[c81066f]1/*
[8dd3803]2 * Copyright (c) 2016-2018, Chris Johns <chrisj@rtems.org>
[c81066f]3 *
4 * RTEMS Tools Project (http://www.rtems.org/)
5 * This file is part of the RTEMS Tools package in 'rtems-tools'.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19/**
20 * @file
21 *
22 * @ingroup rtems_rld
23 *
24 * @brief RTEMS Init dumps the initialisation section data in a format we can
25 *        read.
26 *
27 */
28
29#if HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include <iostream>
34#include <iomanip>
35
36#include <cxxabi.h>
37#include <signal.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
42#include <getopt.h>
43
44#include <rld.h>
45#include <rld-buffer.h>
[1e21ea7]46#include <rld-dwarf.h>
[c81066f]47#include <rld-files.h>
48#include <rld-process.h>
49#include <rld-rtems.h>
[e146927]50#include <rtems-utils.h>
[c81066f]51
52#ifndef HAVE_KILL
53#define kill(p,s) raise(s)
54#endif
55
56namespace rld
57{
58  namespace exeinfo
59  {
60    /**
[78bbe4c]61     * Default section list.
[c81066f]62     */
[78bbe4c]63    const char* default_init[] =
[c81066f]64    {
65      ".rtemsroset",
66      ".ctors",
[78bbe4c]67      ".init",
[c81066f]68      0
69    };
70
[78bbe4c]71    const char* default_fini[] =
[c81066f]72    {
73      ".dtors",
[78bbe4c]74      ".fini",
75      0
76    };
77
78    /**
79     * ARM section list.
80     */
81    const char* arm_init[] =
82    {
83      ".rtemsroset",
84      ".init_array",
85      0
86    };
87
88    const char* arm_fini[] =
89    {
90      ".fini_array",
[c81066f]91      0
92    };
93
94    /**
95     * An executable section's address, offset, size and alignment.
96     */
97    struct section
98    {
[78bbe4c]99      const files::section& sec;        //< The executable's section.
100      buffer::buffer        data;       //< The section's data.
101      files::byteorder      byteorder;  //< The image's byteorder.
[c81066f]102
103      /**
104       * Construct the section.
105       */
[78bbe4c]106      section (const files::section& sec, files::byteorder byteorder);
[c81066f]107
108      /**
109       * Copy construct.
110       */
111      section (const section& orig);
112
113      /**
114       * Clean up the section's memory.
115       */
116      ~section ();
117
118    private:
119      /**
120       * Default constructor.
121       */
122      section ();
123    };
124
125    /**
126     * Container of sections. Order is the address in memory.
127     */
128    typedef std::list < section > sections;
129
130    /**
131     * The kernel image.
132     */
133    struct image
134    {
[78bbe4c]135      files::object    exe;         //< The object file that is the executable.
[1e21ea7]136      dwarf::file      debug;       //< The executable's DWARF details.
[78bbe4c]137      symbols::table   symbols;     //< The synbols for a map.
138      symbols::addrtab addresses;   //< The symbols keyed by address.
139      files::sections  secs;        //< The sections in the executable.
140      const char**     init;        //< The init section's list for the machinetype.
141      const char**     fini;        //< The fini section's list for the machinetype.
142
[c81066f]143
144      /**
145       * Load the executable file.
146       */
147      image (const std::string exe_name);
148
149      /**
150       * Clean up.
151       */
152      ~image ();
153
[1e21ea7]154      /*
155       * Check the compiler and flags match.
156       */
[4638313]157      void output_compilation_unit (bool objects, bool full_flags);
[1e21ea7]158
[c81066f]159      /*
160       * Output the sections.
161       */
162      void output_sections ();
163
164      /*
165       * Output the init sections.
166       */
167      void output_init ();
168
169      /*
170       * Output the fini sections.
171       */
172      void output_fini ();
173
174      /*
175       * Output init/fini worker.
176       */
177      void output_init_fini (const char* label, const char** names);
[f450227]178
[4638313]179      /*
180       * Output the configuration.
181       */
182      void output_config ();
183
[f86a0ce]184      /*
185       * Output the TLS data.
186       */
187      void output_tls ();
188
[f450227]189      /*
190       * Output the inlined functions.
191       */
192      void output_inlined ();
193
194      /*
195       * Output the DWARF data.
196       */
197      void output_dwarf ();
[4638313]198
199    private:
200
201      void config (const std::string name);
[c81066f]202    };
203
[78bbe4c]204    section::section (const files::section& sec, files::byteorder byteorder)
[c81066f]205      : sec (sec),
[8dd3803]206        data (sec.size, byteorder == rld::files::little_endian),
[78bbe4c]207        byteorder (byteorder)
[c81066f]208    {
209    }
210
211    section::section (const section& orig)
212      : sec (orig.sec),
[1d4edfb]213        data (orig.data),
214        byteorder (orig.byteorder)
[c81066f]215    {
216    }
217
218    section::~section ()
219    {
220    }
221
222    /**
223     * Helper for for_each to filter and load the sections we wish to
224     * dump.
225     */
226    class section_loader:
227      public std::unary_function < const files::section, void >
228    {
229    public:
230
231      section_loader (image& img, sections& secs, const char* names[]);
232
233      ~section_loader ();
234
235      void operator () (const files::section& fsec);
236
237    private:
238
239      image&       img;
240      sections&    secs;
241      const char** names;
242    };
243
244    section_loader::section_loader (image&      img,
245                                    sections&   secs,
246                                    const char* names[])
247      : img (img),
248        secs (secs),
249        names (names)
250    {
251    }
252
253    section_loader::~section_loader ()
254    {
255    }
256
257    void
258    section_loader::operator () (const files::section& fsec)
259    {
260      if (rld::verbose () >= RLD_VERBOSE_DETAILS)
261        std::cout << "init:section-loader: " << fsec.name
262                  << " address=" << std::hex << fsec.address << std::dec
263                  << " relocs=" << fsec.relocs.size ()
264                  << " fsec.size=" << fsec.size
265                  << " fsec.alignment=" << fsec.alignment
266                  << " fsec.rela=" << fsec.rela
267                  << std::endl;
268
269      for (int n = 0; names[n] != 0; ++n)
270      {
271        if (fsec.name == names[n])
272        {
273          if (rld::verbose () >= RLD_VERBOSE_DETAILS)
274            std::cout << "init:section-loader: " << fsec.name
275                      << " added" << std::endl;
[6c94148]276
[78bbe4c]277          section sec (fsec, img.exe.get_byteorder ());
[c81066f]278          img.exe.seek (fsec.offset);
279          sec.data.read (img.exe, fsec.size);
280          secs.push_back (sec);
281          break;
282        }
283      }
284    }
285
286    image::image (const std::string exe_name)
[78bbe4c]287      : exe (exe_name),
288        init (0),
289        fini (0)
[c81066f]290    {
291      /*
292       * Open the executable file and begin the session on it.
293       */
294      exe.open ();
295      exe.begin ();
[1e21ea7]296      debug.begin (exe.elf ());
[c81066f]297
298      if (!exe.valid ())
299        throw rld::error ("Not valid: " + exe.name ().full (),
300                          "init::image");
301
[78bbe4c]302      /*
303       * Set up the section lists for the machiner type.
304       */
305      switch (exe.elf ().machinetype ())
306      {
307        case EM_ARM:
308          init = arm_init;
309          fini = arm_fini;
310          break;
311        default:
312          init  = default_init;
313          fini  = default_fini;
314          break;
315      }
316
[c81066f]317      /*
318       * Load the symbols and sections.
319       */
320      exe.load_symbols (symbols, true);
[1e21ea7]321      debug.load_debug ();
[d8eef0a]322      debug.load_types ();
[4638313]323      debug.load_variables ();
[f450227]324      debug.load_functions ();
[c81066f]325      symbols.globals (addresses);
326      symbols.weaks (addresses);
327      symbols.locals (addresses);
328      exe.get_sections (secs);
329    }
330
331    image::~image ()
332    {
[1e21ea7]333    }
334
335    void
[4638313]336    image::output_compilation_unit (bool objects, bool full_flags)
[1e21ea7]337    {
338      dwarf::compilation_units& cus = debug.get_cus ();
339
340      std::cout << "Compilation: " << std::endl;
341
342      rld::strings flag_exceptions = { "-O",
343                                       "-g",
344                                       "-mtune=",
345                                       "-fno-builtin",
346                                       "-fno-inline",
347                                       "-fexceptions",
348                                       "-fnon-call-exceptions",
349                                       "-fvisibility=",
350                                       "-fno-stack-protector",
351                                       "-fbuilding-libgcc",
352                                       "-fno-implicit-templates",
[d8eef0a]353                                       "-fimplicit-templates",
[1e21ea7]354                                       "-ffunction-sections",
355                                       "-fdata-sections",
356                                       "-frandom-seed=",
357                                       "-fno-common",
358                                       "-fno-keep-inline-functions" };
359
360      dwarf::producer_sources producers;
361
362      debug.get_producer_sources (producers);
363
364      /*
365       * Find which flags are common to the building of all source. We are only
366       * interested in files that have any flags. This filters out things like
367       * the assembler which does not have flags.
368       */
369
370      rld::strings all_flags;
[f04f507]371      ::rtems::utils::ostream_guard old_state( std::cout );
[1e21ea7]372
373      size_t source_max = 0;
374
375      for (auto& p : producers)
376      {
377        dwarf::source_flags_compare compare;
378        std::sort (p.sources.begin (), p.sources.end (), compare);
379
380        for (auto& s : p.sources)
381        {
382          size_t len = rld::path::basename (s.source).length ();
383          if (len > source_max)
384            source_max = len;
385
386          if (!s.flags.empty ())
387          {
388            for (auto& f : s.flags)
389            {
390              bool add = true;
391              for (auto& ef : flag_exceptions)
392              {
393                if (rld::starts_with (f, ef))
394                {
395                  add = false;
396                  break;
397                }
398              }
399              if (add)
400              {
401                for (auto& af : all_flags)
402                {
403                  if (f == af)
404                  {
405                    add = false;
406                    break;
407                  }
408                }
409                if (add)
410                  all_flags.push_back (f);
411              }
412            }
413          }
414        }
415      }
416
417      rld::strings common_flags;
418
419      for (auto& flag : all_flags)
420      {
421        bool found_in_all = true;
422        for (auto& p : producers)
423        {
424          for (auto& s : p.sources)
425          {
426            if (!s.flags.empty ())
427            {
428              bool flag_found = false;
429              for (auto& f : s.flags)
430              {
431                if (flag == f)
432                {
433                  flag_found = true;
434                  break;
435                }
436              }
437              if (!flag_found)
438              {
439                found_in_all = false;
440                break;
441              }
442            }
443            if (!found_in_all)
444              break;
445          }
446        }
447        if (found_in_all)
448          common_flags.push_back (flag);
449      }
450
451      std::cout << " Producers: " << producers.size () << std::endl;
452
453      for (auto& p : producers)
454      {
455        std::cout << "  | " << p.producer
456                  << ": " << p.sources.size () << " objects" << std::endl;
457      }
458
459      std::cout << " Common flags: " << common_flags.size () << std::endl
460                << "  |";
461
462      for (auto& f : common_flags)
463        std::cout << ' ' << f;
464      std::cout << std::endl;
465
466      if (objects)
467      {
468        std::cout << " Object files: " << cus.size () << std::endl;
469
470        rld::strings filter_flags = common_flags;
471        filter_flags.insert (filter_flags.end (),
472                             flag_exceptions.begin (),
473                             flag_exceptions.end());
474
475        for (auto& p : producers)
476        {
477          std::cout << ' ' << p.producer
478                    << ": " << p.sources.size () << " objects" << std::endl;
479          for (auto& s : p.sources)
480          {
481            std::cout << "   | "
482                      << std::setw (source_max + 1) << std::left
483                      << rld::path::basename (s.source);
484            if (!s.flags.empty ())
485            {
486              bool first = true;
487              for (auto& f : s.flags)
488              {
489                bool present = false;
[4638313]490                if (!full_flags)
[1e21ea7]491                {
[4638313]492                  for (auto& ff : filter_flags)
[1e21ea7]493                  {
[4638313]494                    if (rld::starts_with(f, ff))
495                    {
496                      present = true;
497                      break;
498                    }
[1e21ea7]499                  }
500                }
501                if (!present)
502                {
503                  if (first)
504                  {
505                    std::cout << ':';
506                    first = false;
507                  }
508                  std::cout << ' ' << f;
509                }
510              }
511            }
512            std::cout << std::endl;
513          }
514        }
515      }
516
517      std::cout << std::endl;
[c81066f]518    }
519
520    void
521    image::output_sections ()
522    {
523      std::cout << "Sections: " << secs.size () << std::endl;
524
[78bbe4c]525      size_t max_section_name = 0;
526
527      for (files::sections::const_iterator si = secs.begin ();
528           si != secs.end ();
529           ++si)
530      {
531        const files::section& sec = *si;
532        if (sec.name.length() > max_section_name)
533          max_section_name = sec.name.length();
534      }
535
[c81066f]536      for (files::sections::const_iterator si = secs.begin ();
537           si != secs.end ();
538           ++si)
539      {
540        const files::section& sec = *si;
541
542        #define SF(f, i, c) if (sec.flags & (f)) flags[i] = c
543
544        std::string flags ("--------------");
545
546        SF (SHF_WRITE,            0, 'W');
547        SF (SHF_ALLOC,            1, 'A');
548        SF (SHF_EXECINSTR,        2, 'E');
549        SF (SHF_MERGE,            3, 'M');
550        SF (SHF_STRINGS,          4, 'S');
551        SF (SHF_INFO_LINK,        5, 'I');
552        SF (SHF_LINK_ORDER,       6, 'L');
553        SF (SHF_OS_NONCONFORMING, 7, 'N');
554        SF (SHF_GROUP,            8, 'G');
555        SF (SHF_TLS,              9, 'T');
556        SF (SHF_AMD64_LARGE,     10, 'a');
557        SF (SHF_ENTRYSECT,       11, 'e');
558        SF (SHF_COMDEF,          12, 'c');
559        SF (SHF_ORDERED,         13, 'O');
560
561        std::cout << "  " << std::left
[78bbe4c]562                  << std::setw (max_section_name) << sec.name
[c81066f]563                  << " " << flags
564                  << std::right << std::hex << std::setfill ('0')
[78bbe4c]565                  << " addr: 0x" << std::setw (8) << sec.address
[c81066f]566                  << " 0x" << std::setw (8) << sec.address + sec.size
567                  << std::dec << std::setfill (' ')
[78bbe4c]568                  << " size: " << std::setw (10) << sec.size
[c81066f]569                  << " align: " << std::setw (3) << sec.alignment
[78bbe4c]570                  << " relocs: " << std::setw (6) << sec.relocs.size ()
[c81066f]571                  << std::endl;
572      }
573
574      std::cout << std::endl;
575    }
576
577    void
578    image::output_init ()
579    {
[78bbe4c]580      output_init_fini ("Init", init);
[c81066f]581    }
582
583    void
584    image::output_fini ()
585    {
[78bbe4c]586      output_init_fini ("Fini", fini);
[c81066f]587    }
588
589    void
590    image::output_init_fini (const char* label, const char** names)
591    {
592      /*
593       * Load the sections.
594       */
595      sections ifsecs;
596      std::for_each (secs.begin (), secs.end (),
597                     section_loader (*this, ifsecs, names));
598
599      std::cout << label << " sections: " << ifsecs.size () << std::endl;
600
[8dd3803]601      for (auto& sec : ifsecs)
[c81066f]602      {
[8dd3803]603        const size_t machine_size = exe.elf ().machine_size ();
[c81066f]604        const int    count = sec.data.level () / machine_size;
605
606        std::cout << " " << sec.sec.name << std::endl;
607
608        for (int i = 0; i < count; ++i)
609        {
610          uint32_t         address;
611          symbols::symbol* sym;
612          sec.data >> address;
613          sym = addresses[address];
614          std::cout << "  "
615                    << std::hex << std::setfill ('0')
[6c94148]616                    << "0x" << std::setw (8) << address
617                    << std::dec << std::setfill ('0');
[c81066f]618          if (sym)
[6c94148]619          {
620            std::string label = sym->name ();
621            if (rld::symbols::is_cplusplus (label))
622              rld::symbols::demangle_name (label, label);
623            std::cout << " " << label;
624          }
[c81066f]625          else
[6c94148]626          {
[c81066f]627            std::cout << " no symbol";
[6c94148]628          }
629          std::cout << std::endl;
[c81066f]630        }
631      }
632
633      std::cout << std::endl;
634    }
[f450227]635
[f86a0ce]636    void image::output_tls ()
637    {
[f04f507]638      ::rtems::utils::ostream_guard old_state( std::cout );
[e146927]639
[f86a0ce]640      symbols::symbol* tls_data_begin = symbols.find_global("_TLS_Data_begin");
641      symbols::symbol* tls_data_end = symbols.find_global("_TLS_Data_end");
642      symbols::symbol* tls_data_size = symbols.find_global("_TLS_Data_size");
643      symbols::symbol* tls_bss_begin = symbols.find_global("_TLS_BSS_begin");
644      symbols::symbol* tls_bss_end = symbols.find_global("_TLS_BSS_end");
645      symbols::symbol* tls_bss_size = symbols.find_global("_TLS_BSS_size");
646      symbols::symbol* tls_size = symbols.find_global("_TLS_Size");
647      symbols::symbol* tls_alignment = symbols.find_global("_TLS_Alignment");
[4638313]648      symbols::symbol* tls_max_size = symbols.find_global("_Thread_Maximum_TLS_size");
[f86a0ce]649
650      if (tls_data_begin == nullptr ||
651          tls_data_end == nullptr ||
652          tls_data_size == nullptr ||
653          tls_bss_begin == nullptr ||
654          tls_bss_end == nullptr ||
655          tls_bss_size == nullptr ||
656          tls_size == nullptr ||
657          tls_alignment == nullptr)
658      {
659        if (tls_data_begin == nullptr &&
660            tls_data_end == nullptr &&
661            tls_data_size == nullptr &&
662            tls_bss_begin == nullptr &&
663            tls_bss_end == nullptr &&
664            tls_bss_size == nullptr &&
665            tls_size == nullptr &&
666            tls_alignment == nullptr)
667        {
668            std::cout << "No TLS data found" << std::endl;
669            return;
670        }
671        std::cout << "TLS environment is INVALID (please report):" << std::endl
[4638313]672                  << " _TLS_Data_begin          : "
[f86a0ce]673                  << (char*) (tls_data_begin == nullptr ? "not-found" : "found")
674                  << std::endl
[4638313]675                  << " _TLS_Data_end            : "
[f86a0ce]676                  << (char*) (tls_data_end == nullptr ? "not-found" : "found")
677                  << std::endl
[4638313]678                  << " _TLS_Data_size           : "
[f86a0ce]679                  << (char*) (tls_data_size == nullptr ? "not-found" : "found")
680                  << std::endl
[4638313]681                  << " _TLS_BSS_begin           : "
[f86a0ce]682                  << (char*) (tls_bss_begin == nullptr ? "not-found" : "found")
683                  << std::endl
[4638313]684                  << " _TLS_BSS_end             : "
[f86a0ce]685                  << (char*) (tls_bss_end == nullptr ? "not-found" : "found")
686                  << std::endl
[4638313]687                  << " _TLS_BSS_Size            : "
[f86a0ce]688                  << (char*) (tls_bss_size == nullptr ? "not-found" : "found")
689                  << std::endl
[4638313]690                  << " _TLS_Size                : "
[f86a0ce]691                  << (char*) (tls_size == nullptr ? "not-found" : "found")
692                  << std::endl
[4638313]693                  << " _TLS_Alignment           : "
[f86a0ce]694                  << (char*) (tls_alignment == nullptr ? "not-found" : "found")
695                  << std::endl
[4638313]696                  << " _Thread_Maximum_TLS_size : "
697                  << (char*) (tls_max_size == nullptr ? "not-found" : "found")
698                  << std::endl
[f86a0ce]699                  << std::endl;
700        return;
701      }
702
703      std::cout << "TLS size      : " << tls_size->value () << std::endl
[4638313]704                << "     max size : ";
705      if (tls_max_size == nullptr)
706          std::cout << "not found" << std::endl;
707      else
708          std::cout << tls_max_size->value () << std::endl;
709      std::cout << "    data size : " << tls_data_size->value () << std::endl
[f86a0ce]710                << "     bss size : " << tls_bss_size->value () << std::endl
711                << "    alignment : " << tls_alignment->value () << std::endl
712                << std::right << std::hex << std::setfill ('0')
713                << "    data addr : 0x" << std::setw (8) << tls_data_begin->value ()
714                << std::endl
715                << std::dec << std::setfill (' ')
716                << std::endl;
717    }
718
[4638313]719    void image::config(const std::string name)
720    {
721      std::string table_name = "_" + name + "_Information";
722      symbols::symbol* table = symbols.find_global(table_name);
723
724      if (table != nullptr)
725        std::cout << " " << name << std::endl;
726    }
727
728    void image::output_config()
729    {
730      std::cout << "Configurations:" << std::endl;
731      config("Thread");
732      config("Barrier");
733      config("Extension");
734      config("Message_queue");
735      config("Partition");
736      config("Rate_monotonic");
737      config("Dual_ported_memory");
738      config("Region");
739      config("Semaphore");
740      config("Timer");
741      config("RTEMS_tasks");
742    }
743
[f450227]744    struct func_count
745    {
746      std::string name;
747      int         count;
748      size_t      size;
749
750      func_count (std::string name, size_t size)
751        : name (name),
752          count (1),
753          size (size) {
754      }
755    };
756    typedef std::vector < func_count > func_counts;
757
758    void image::output_inlined ()
759    {
760      size_t           total = 0;
761      size_t           total_size = 0;
762      size_t           inlined_size = 0;
[7d3b8ac]763      double           percentage;
764      double           percentage_size;
[2950fd4]765      dwarf::functions funcs_inlined;
766      dwarf::functions funcs_not_inlined;
[f450227]767      func_counts      counts;
768
769      for (auto& cu : debug.get_cus ())
770      {
771        for (auto& f : cu.get_functions ())
772        {
773          if (f.size () > 0 && f.has_machine_code ())
774          {
[2950fd4]775            bool counted;
[f450227]776            ++total;
777            total_size += f.size ();
[2950fd4]778            switch (f.get_inlined ())
[f450227]779            {
[2950fd4]780              case dwarf::function::inl_inline:
781              case dwarf::function::inl_declared_inlined:
782                inlined_size += f.size ();
783                counted = false;
784                for (auto& c : counts)
[f450227]785                {
[2950fd4]786                  if (c.name == f.name ())
787                  {
788                    ++c.count;
789                    c.size += f.size ();
790                    counted = true;
791                    break;
792                  }
[f450227]793                }
[2950fd4]794                if (!counted)
795                  counts.push_back (func_count (f.name (), f.size ()));
796                funcs_inlined.push_back (f);
797                break;
798              case dwarf::function::inl_declared_not_inlined:
799                funcs_not_inlined.push_back (f);
800                break;
801              default:
802                break;
[f450227]803            }
804          }
805        }
806      }
807
[7d3b8ac]808      if ( total == 0 ) {
809        percentage = 0;
810      } else {
811        percentage = (double) ( funcs_inlined.size() * 100 ) / total;
812      }
813
814      if ( total_size == 0 ) {
815        percentage_size = 0;
816      } else {
817        percentage_size = (double) ( inlined_size * 100 ) / total_size;
818      }
819
[2950fd4]820      std::cout << "inlined funcs   : " << funcs_inlined.size () << std::endl
[f450227]821                << "    total funcs : " << total << std::endl
[7d3b8ac]822                << " % inline funcs : " << percentage << '%' << std::endl
[f450227]823                << "     total size : " << total_size << std::endl
824                << "    inline size : " << inlined_size << std::endl
[7d3b8ac]825                << "  % inline size : " << percentage_size << '%' << std::endl;
[f450227]826
827      auto count_compare = [](func_count const & a, func_count const & b) {
828        return a.size != b.size?  a.size < b.size : a.count > b.count;
829      };
830      std::sort (counts.begin (), counts.end (), count_compare);
831      std::reverse (counts.begin (), counts.end ());
832
833      std::cout  << std::endl << "inlined repeats : " << std::endl;
834      for (auto& c : counts)
835        if (c.count > 1)
836          std::cout << std::setw (6) << c.size << ' '
837                    << std::setw (4) << c.count << ' '
838                    << c.name << std::endl;
839
840      dwarf::function_compare compare (dwarf::function_compare::fc_by_size);
841
[2950fd4]842      std::sort (funcs_inlined.begin (), funcs_inlined.end (), compare);
843      std::reverse (funcs_inlined.begin (), funcs_inlined.end ());
844
845      std::cout << std::endl << "inline funcs : " << std::endl;
846      for (auto& f : funcs_inlined)
[f450227]847      {
[2950fd4]848        std::string flags;
849
[f450227]850        std::cout << std::setw (6) << f.size () << ' '
851                  << (char) (f.is_external () ? 'E' : ' ')
[2950fd4]852                  << (char) (f.get_inlined () == dwarf::function::inl_inline ? 'C' : ' ')
[f450227]853                  << std::hex << std::setfill ('0')
854                  << " 0x" << std::setw (8) << f.pc_low ()
855                  << std::dec << std::setfill (' ')
856                  << ' ' << f.name ()
857                  << std::endl;
858      }
[2950fd4]859
860      if (funcs_not_inlined.size () > 0)
861      {
862        std::sort (funcs_not_inlined.begin (), funcs_not_inlined.end (), compare);
863        std::reverse (funcs_not_inlined.begin (), funcs_not_inlined.end ());
864
865        std::cout << std::endl << "inline funcs not inlined: " << std::endl;
866        for (auto& f : funcs_not_inlined)
867        {
868          std::cout << std::setw (6) << f.size () << ' '
869                    << (char) (f.is_external () ? 'E' : ' ')
870                    << (char) (f.get_inlined () == dwarf::function::inl_inline ? 'C' : ' ')
871                    << std::hex << std::setfill ('0')
872                    << " 0x" << std::setw (8) << f.pc_low ()
873                    << std::dec << std::setfill (' ')
874                    << ' ' << f.name ()
875                    << std::endl;
876        }
877      }
[f450227]878    }
879
880    void image::output_dwarf ()
881    {
882      std::cout << "DWARF Data:" << std::endl;
883      debug.dump (std::cout);
884    }
[c81066f]885  }
886}
887
888/**
889 * RTEMS Exe Info options. This needs to be rewritten to be like cc where only
890 * a single '-' and long options is present.
891 */
892static struct option rld_opts[] = {
893  { "help",        no_argument,            NULL,           'h' },
894  { "version",     no_argument,            NULL,           'V' },
895  { "verbose",     no_argument,            NULL,           'v' },
896  { "map",         no_argument,            NULL,           'M' },
897  { "all",         no_argument,            NULL,           'a' },
898  { "sections",    no_argument,            NULL,           'S' },
899  { "init",        no_argument,            NULL,           'I' },
900  { "fini",        no_argument,            NULL,           'F' },
[3fab1f5]901  { "objects",     no_argument,            NULL,           'O' },
[4638313]902  { "full-flags",  no_argument,            NULL,           'A' },
903  { "config",      no_argument,            NULL,           'C' },
[f86a0ce]904  { "tls",         no_argument,            NULL,           'T' },
[f450227]905  { "inlined",     no_argument,            NULL,           'i' },
906  { "dwarf",       no_argument,            NULL,           'D' },
[c81066f]907  { NULL,          0,                      NULL,            0 }
908};
909
910void
911usage (int exit_code)
912{
913  std::cout << "rtems-exeinfo [options] objects" << std::endl
914            << "Options and arguments:" << std::endl
915            << " -h        : help (also --help)" << std::endl
916            << " -V        : print linker version number and exit (also --version)" << std::endl
917            << " -v        : verbose (trace import parts), can supply multiple times" << std::endl
918            << "             to increase verbosity (also --verbose)" << std::endl
919            << " -M        : generate map output (also --map)" << std::endl
[f450227]920            << " -a        : all output excluding the map and DAWRF (also --all)" << std::endl
[c81066f]921            << " -S        : show all section (also --sections)" << std::endl
922            << " -I        : show init section tables (also --init)" << std::endl
[1e21ea7]923            << " -F        : show fini section tables (also --fini)" << std::endl
[f450227]924            << " -O        : show object files (also --objects)" << std::endl
[4638313]925            << "           :  add --full-flags for compiler options" << std::endl
926            << " -C        : show configuration (also --config)" << std::endl
[f86a0ce]927            << " -T        : show thread local storage data (also --tls)" << std::endl
[f450227]928            << " -i        : show inlined code (also --inlined)" << std::endl
929            << " -D        : dump the DWARF data (also --dwarf)" << std::endl;
[c81066f]930  ::exit (exit_code);
931}
932
933static void
934fatal_signal (int signum)
935{
936  signal (signum, SIG_DFL);
937
938  rld::process::temporaries_clean_up ();
939
940  /*
941   * Get the same signal again, this time not handled, so its normal effect
942   * occurs.
943   */
944  kill (getpid (), signum);
945}
946
947static void
948setup_signals (void)
949{
950  if (signal (SIGINT, SIG_IGN) != SIG_IGN)
951    signal (SIGINT, fatal_signal);
952#ifdef SIGHUP
953  if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
954    signal (SIGHUP, fatal_signal);
955#endif
956  if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
957    signal (SIGTERM, fatal_signal);
958#ifdef SIGPIPE
959  if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
960    signal (SIGPIPE, fatal_signal);
961#endif
962#ifdef SIGCHLD
963  signal (SIGCHLD, SIG_DFL);
964#endif
965}
966
[1e21ea7]967void
968unhandled_exception (void)
969{
970  std::cerr << "error: exception handling error, please report" << std::endl;
971  exit (1);
972}
973
[c81066f]974int
975main (int argc, char* argv[])
976{
977  int ec = 0;
978
979  setup_signals ();
980
[1e21ea7]981  std::set_terminate(unhandled_exception);
982
[c81066f]983  try
984  {
985    std::string exe_name;
986    bool        map = false;
987    bool        all = false;
988    bool        sections = false;
989    bool        init = false;
990    bool        fini = false;
[1e21ea7]991    bool        objects = false;
[4638313]992    bool        full_flags = false;
993    bool        config = false;
[f86a0ce]994    bool        tls = false;
[f450227]995    bool        inlined = false;
996    bool        dwarf_data = false;
[c81066f]997
998    rld::set_cmdline (argc, argv);
999
1000    while (true)
1001    {
[4638313]1002      int opt = ::getopt_long (argc, argv, "hvVMaSIFOCTiD", rld_opts, NULL);
[c81066f]1003      if (opt < 0)
1004        break;
1005
1006      switch (opt)
1007      {
1008        case 'V':
1009          std::cout << "rtems-exeinfo (RTEMS Executable Info) " << rld::version ()
1010                    << ", RTEMS revision " << rld::rtems::version ()
1011                    << std::endl;
1012          ::exit (0);
1013          break;
1014
1015        case 'v':
1016          rld::verbose_inc ();
1017          break;
1018
1019        case 'M':
1020          map = true;
1021          break;
1022
1023        case 'a':
1024          all = true;
1025          break;
1026
1027        case 'I':
1028          init = true;
1029          break;
1030
1031        case 'F':
1032          fini = true;
1033          break;
1034
1035        case 'S':
1036          sections = true;
1037          break;
1038
[1e21ea7]1039        case 'O':
1040          objects = true;
1041          break;
1042
[4638313]1043        case 'A':
1044          full_flags = true;
1045          break;
1046
1047        case 'C':
1048          config = true;
1049          break;
1050
[f86a0ce]1051        case 'T':
1052          tls = true;
1053          break;
1054
[f450227]1055        case 'i':
1056          inlined = true;
1057          break;
1058
1059        case 'D':
1060          dwarf_data = true;
1061          break;
1062
[c81066f]1063        case '?':
1064          usage (3);
1065          break;
1066
1067        case 'h':
1068          usage (0);
1069          break;
1070      }
1071    }
1072
1073    /*
1074     * Set the program name.
1075     */
1076    rld::set_progname (argv[0]);
1077
1078    argc -= optind;
1079    argv += optind;
1080
1081    std::cout << "RTEMS Executable Info " << rld::version () << std::endl;
1082    std::cout << " " << rld::get_cmdline () << std::endl;
1083
1084    /*
1085     * All means all types of output.
1086     */
1087    if (all)
1088    {
1089      sections = true;
1090      init = true;
1091      fini = true;
[1e21ea7]1092      objects = true;
[4638313]1093      config = true;
[f86a0ce]1094      tls = true;
[f450227]1095      inlined = true;
[c81066f]1096    }
1097
1098    /*
1099     * If there is no executable there is nothing to convert.
1100     */
1101    if (argc == 0)
1102      throw rld::error ("no executable", "options");
1103    if (argc > 1)
1104      throw rld::error ("only a single executable", "options");
1105
1106    /*
1107     * The name of the executable.
1108     */
1109    exe_name = *argv;
1110
1111    if (rld::verbose ())
1112      std::cout << "exe-image: " << exe_name << std::endl;
1113
1114    /*
1115     * Open the executable and read the symbols.
1116     */
1117    rld::exeinfo::image exe (exe_name);
1118
[1e21ea7]1119    std::cout << "exe: " << exe.exe.name ().full () << std::endl
1120              << std::endl;
[c81066f]1121
1122    /*
1123     * Generate the output.
1124     */
[4638313]1125    exe.output_compilation_unit (objects, full_flags);
[c81066f]1126    if (sections)
1127      exe.output_sections ();
1128    if (init)
1129      exe.output_init ();
1130    if (fini)
1131      exe.output_fini ();
[4638313]1132    if (config)
1133      exe.output_config ();
[f86a0ce]1134    if (tls)
1135      exe.output_tls ();
[f450227]1136    if (inlined)
1137      exe.output_inlined ();
1138    if (dwarf_data)
1139      exe.output_dwarf ();
[c81066f]1140
1141    /*
1142     * Map ?
1143     */
1144    if (map)
1145      rld::symbols::output (std::cout, exe.symbols);
1146  }
1147  catch (rld::error re)
1148  {
1149    std::cerr << "error: "
1150              << re.where << ": " << re.what
1151              << std::endl;
1152    ec = 10;
1153  }
[3d2db56]1154  catch (std::exception& e)
[c81066f]1155  {
1156    int   status;
1157    char* realname;
1158    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
1159    std::cerr << "error: exception: " << realname << " [";
1160    ::free (realname);
1161    const std::type_info &ti = typeid (e);
1162    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
1163    std::cerr << realname << "] " << e.what () << std::endl << std::flush;
1164    ::free (realname);
1165    ec = 11;
1166  }
1167  catch (...)
1168  {
1169    /*
1170     * Helps to know if this happens.
1171     */
1172    std::cerr << "error: unhandled exception" << std::endl;
1173    ec = 12;
1174  }
1175
1176  return ec;
1177}
Note: See TracBrowser for help on using the repository browser.