source: rtems-tools/linkers/rtems-exeinfo.cpp @ a248471

5
Last change on this file since a248471 was 78bbe4c, checked in by Chris Johns <chrisj@…>, on 08/16/17 at 08:09:59

linkers/exe-info Support ARM static constructors.

Note, ARM destructors are registered at runtime and currently not
easly found.

Update libiberty to get a newer demangler.

Closes #3102.

  • Property mode set to 100644
File size: 15.5 KB
Line 
1/*
2 * Copyright (c) 2016, Chris Johns <chrisj@rtems.org>
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>
46#include <rld-files.h>
47#include <rld-process.h>
48#include <rld-rtems.h>
49
50#ifndef HAVE_KILL
51#define kill(p,s) raise(s)
52#endif
53
54namespace rld
55{
56  namespace exeinfo
57  {
58    /**
59     * Default section list.
60     */
61    const char* default_init[] =
62    {
63      ".rtemsroset",
64      ".ctors",
65      ".init",
66      0
67    };
68
69    const char* default_fini[] =
70    {
71      ".dtors",
72      ".fini",
73      0
74    };
75
76    /**
77     * ARM section list.
78     */
79    const char* arm_init[] =
80    {
81      ".rtemsroset",
82      ".init_array",
83      0
84    };
85
86    const char* arm_fini[] =
87    {
88      ".fini_array",
89      0
90    };
91
92    /**
93     * An executable section's address, offset, size and alignment.
94     */
95    struct section
96    {
97      const files::section& sec;        //< The executable's section.
98      buffer::buffer        data;       //< The section's data.
99      files::byteorder      byteorder;  //< The image's byteorder.
100
101      /**
102       * Construct the section.
103       */
104      section (const files::section& sec, files::byteorder byteorder);
105
106      /**
107       * Copy construct.
108       */
109      section (const section& orig);
110
111      /**
112       * Clean up the section's memory.
113       */
114      ~section ();
115
116    private:
117      /**
118       * Default constructor.
119       */
120      section ();
121    };
122
123    /**
124     * Container of sections. Order is the address in memory.
125     */
126    typedef std::list < section > sections;
127
128    /**
129     * The kernel image.
130     */
131    struct image
132    {
133      files::object    exe;         //< The object file that is the executable.
134      symbols::table   symbols;     //< The synbols for a map.
135      symbols::addrtab addresses;   //< The symbols keyed by address.
136      files::sections  secs;        //< The sections in the executable.
137      const char**     init;        //< The init section's list for the machinetype.
138      const char**     fini;        //< The fini section's list for the machinetype.
139
140
141      /**
142       * Load the executable file.
143       */
144      image (const std::string exe_name);
145
146      /**
147       * Clean up.
148       */
149      ~image ();
150
151      /*
152       * Output the sections.
153       */
154      void output_sections ();
155
156      /*
157       * Output the init sections.
158       */
159      void output_init ();
160
161      /*
162       * Output the fini sections.
163       */
164      void output_fini ();
165
166      /*
167       * Output init/fini worker.
168       */
169      void output_init_fini (const char* label, const char** names);
170    };
171
172    section::section (const files::section& sec, files::byteorder byteorder)
173      : sec (sec),
174        data (sec.size),
175        byteorder (byteorder)
176    {
177    }
178
179    section::section (const section& orig)
180      : sec (orig.sec),
181        data (orig.data)
182    {
183    }
184
185    section::~section ()
186    {
187    }
188
189    /**
190     * Helper for for_each to filter and load the sections we wish to
191     * dump.
192     */
193    class section_loader:
194      public std::unary_function < const files::section, void >
195    {
196    public:
197
198      section_loader (image& img, sections& secs, const char* names[]);
199
200      ~section_loader ();
201
202      void operator () (const files::section& fsec);
203
204    private:
205
206      image&       img;
207      sections&    secs;
208      const char** names;
209    };
210
211    section_loader::section_loader (image&      img,
212                                    sections&   secs,
213                                    const char* names[])
214      : img (img),
215        secs (secs),
216        names (names)
217    {
218    }
219
220    section_loader::~section_loader ()
221    {
222    }
223
224    void
225    section_loader::operator () (const files::section& fsec)
226    {
227      if (rld::verbose () >= RLD_VERBOSE_DETAILS)
228        std::cout << "init:section-loader: " << fsec.name
229                  << " address=" << std::hex << fsec.address << std::dec
230                  << " relocs=" << fsec.relocs.size ()
231                  << " fsec.size=" << fsec.size
232                  << " fsec.alignment=" << fsec.alignment
233                  << " fsec.rela=" << fsec.rela
234                  << std::endl;
235
236      for (int n = 0; names[n] != 0; ++n)
237      {
238        if (fsec.name == names[n])
239        {
240          if (rld::verbose () >= RLD_VERBOSE_DETAILS)
241            std::cout << "init:section-loader: " << fsec.name
242                      << " added" << std::endl;
243
244          section sec (fsec, img.exe.get_byteorder ());
245          img.exe.seek (fsec.offset);
246          sec.data.read (img.exe, fsec.size);
247          secs.push_back (sec);
248          break;
249        }
250      }
251    }
252
253    image::image (const std::string exe_name)
254      : exe (exe_name),
255        init (0),
256        fini (0)
257    {
258      /*
259       * Open the executable file and begin the session on it.
260       */
261      exe.open ();
262      exe.begin ();
263
264      if (!exe.valid ())
265        throw rld::error ("Not valid: " + exe.name ().full (),
266                          "init::image");
267
268      /*
269       * Set up the section lists for the machiner type.
270       */
271      switch (exe.elf ().machinetype ())
272      {
273        case EM_ARM:
274          init = arm_init;
275          fini = arm_fini;
276          break;
277        default:
278          init  = default_init;
279          fini  = default_fini;
280          break;
281      }
282
283      /*
284       * Load the symbols and sections.
285       */
286      exe.load_symbols (symbols, true);
287      symbols.globals (addresses);
288      symbols.weaks (addresses);
289      symbols.locals (addresses);
290      exe.get_sections (secs);
291    }
292
293    image::~image ()
294    {
295      exe.close ();
296    }
297
298    void
299    image::output_sections ()
300    {
301      std::cout << "Sections: " << secs.size () << std::endl;
302
303      size_t max_section_name = 0;
304
305      for (files::sections::const_iterator si = secs.begin ();
306           si != secs.end ();
307           ++si)
308      {
309        const files::section& sec = *si;
310        if (sec.name.length() > max_section_name)
311          max_section_name = sec.name.length();
312      }
313
314      for (files::sections::const_iterator si = secs.begin ();
315           si != secs.end ();
316           ++si)
317      {
318        const files::section& sec = *si;
319
320        #define SF(f, i, c) if (sec.flags & (f)) flags[i] = c
321
322        std::string flags ("--------------");
323
324        SF (SHF_WRITE,            0, 'W');
325        SF (SHF_ALLOC,            1, 'A');
326        SF (SHF_EXECINSTR,        2, 'E');
327        SF (SHF_MERGE,            3, 'M');
328        SF (SHF_STRINGS,          4, 'S');
329        SF (SHF_INFO_LINK,        5, 'I');
330        SF (SHF_LINK_ORDER,       6, 'L');
331        SF (SHF_OS_NONCONFORMING, 7, 'N');
332        SF (SHF_GROUP,            8, 'G');
333        SF (SHF_TLS,              9, 'T');
334        SF (SHF_AMD64_LARGE,     10, 'a');
335        SF (SHF_ENTRYSECT,       11, 'e');
336        SF (SHF_COMDEF,          12, 'c');
337        SF (SHF_ORDERED,         13, 'O');
338
339        std::cout << "  " << std::left
340                  << std::setw (max_section_name) << sec.name
341                  << " " << flags
342                  << std::right << std::hex << std::setfill ('0')
343                  << " addr: 0x" << std::setw (8) << sec.address
344                  << " 0x" << std::setw (8) << sec.address + sec.size
345                  << std::dec << std::setfill (' ')
346                  << " size: " << std::setw (10) << sec.size
347                  << " align: " << std::setw (3) << sec.alignment
348                  << " relocs: " << std::setw (6) << sec.relocs.size ()
349                  << std::endl;
350      }
351
352      std::cout << std::endl;
353    }
354
355    void
356    image::output_init ()
357    {
358      output_init_fini ("Init", init);
359    }
360
361    void
362    image::output_fini ()
363    {
364      output_init_fini ("Fini", fini);
365    }
366
367    void
368    image::output_init_fini (const char* label, const char** names)
369    {
370      /*
371       * Load the sections.
372       */
373      sections ifsecs;
374      std::for_each (secs.begin (), secs.end (),
375                     section_loader (*this, ifsecs, names));
376
377      std::cout << label << " sections: " << ifsecs.size () << std::endl;
378
379      for (sections::iterator ii = ifsecs.begin ();
380           ii != ifsecs.end ();
381           ++ii)
382      {
383        section&     sec = *ii;
384        const size_t machine_size = sizeof (uint32_t);
385        const int    count = sec.data.level () / machine_size;
386
387        std::cout << " " << sec.sec.name << std::endl;
388
389        for (int i = 0; i < count; ++i)
390        {
391          uint32_t         address;
392          symbols::symbol* sym;
393          sec.data >> address;
394          sym = addresses[address];
395          std::cout << "  "
396                    << std::hex << std::setfill ('0')
397                    << "0x" << std::setw (8) << address
398                    << std::dec << std::setfill ('0');
399          if (sym)
400          {
401            std::string label = sym->name ();
402            if (rld::symbols::is_cplusplus (label))
403              rld::symbols::demangle_name (label, label);
404            std::cout << " " << label;
405          }
406          else
407          {
408            std::cout << " no symbol";
409          }
410          std::cout << std::endl;
411        }
412      }
413
414      std::cout << std::endl;
415    }
416  }
417}
418
419/**
420 * RTEMS Exe Info options. This needs to be rewritten to be like cc where only
421 * a single '-' and long options is present.
422 */
423static struct option rld_opts[] = {
424  { "help",        no_argument,            NULL,           'h' },
425  { "version",     no_argument,            NULL,           'V' },
426  { "verbose",     no_argument,            NULL,           'v' },
427  { "map",         no_argument,            NULL,           'M' },
428  { "all",         no_argument,            NULL,           'a' },
429  { "sections",    no_argument,            NULL,           'S' },
430  { "init",        no_argument,            NULL,           'I' },
431  { "fini",        no_argument,            NULL,           'F' },
432  { NULL,          0,                      NULL,            0 }
433};
434
435void
436usage (int exit_code)
437{
438  std::cout << "rtems-exeinfo [options] objects" << std::endl
439            << "Options and arguments:" << std::endl
440            << " -h        : help (also --help)" << std::endl
441            << " -V        : print linker version number and exit (also --version)" << std::endl
442            << " -v        : verbose (trace import parts), can supply multiple times" << std::endl
443            << "             to increase verbosity (also --verbose)" << std::endl
444            << " -M        : generate map output (also --map)" << std::endl
445            << " -a        : all output excluding the map (also --all)" << std::endl
446            << " -S        : show all section (also --sections)" << std::endl
447            << " -I        : show init section tables (also --init)" << std::endl
448            << " -F        : show fini section tables (also --fini)" << std::endl;
449  ::exit (exit_code);
450}
451
452static void
453fatal_signal (int signum)
454{
455  signal (signum, SIG_DFL);
456
457  rld::process::temporaries_clean_up ();
458
459  /*
460   * Get the same signal again, this time not handled, so its normal effect
461   * occurs.
462   */
463  kill (getpid (), signum);
464}
465
466static void
467setup_signals (void)
468{
469  if (signal (SIGINT, SIG_IGN) != SIG_IGN)
470    signal (SIGINT, fatal_signal);
471#ifdef SIGHUP
472  if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
473    signal (SIGHUP, fatal_signal);
474#endif
475  if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
476    signal (SIGTERM, fatal_signal);
477#ifdef SIGPIPE
478  if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
479    signal (SIGPIPE, fatal_signal);
480#endif
481#ifdef SIGCHLD
482  signal (SIGCHLD, SIG_DFL);
483#endif
484}
485
486int
487main (int argc, char* argv[])
488{
489  int ec = 0;
490
491  setup_signals ();
492
493  try
494  {
495    std::string exe_name;
496    bool        map = false;
497    bool        all = false;
498    bool        sections = false;
499    bool        init = false;
500    bool        fini = false;
501
502    rld::set_cmdline (argc, argv);
503
504    while (true)
505    {
506      int opt = ::getopt_long (argc, argv, "hvVMaSIF", rld_opts, NULL);
507      if (opt < 0)
508        break;
509
510      switch (opt)
511      {
512        case 'V':
513          std::cout << "rtems-exeinfo (RTEMS Executable Info) " << rld::version ()
514                    << ", RTEMS revision " << rld::rtems::version ()
515                    << std::endl;
516          ::exit (0);
517          break;
518
519        case 'v':
520          rld::verbose_inc ();
521          break;
522
523        case 'M':
524          map = true;
525          break;
526
527        case 'a':
528          all = true;
529          break;
530
531        case 'I':
532          init = true;
533          break;
534
535        case 'F':
536          fini = true;
537          break;
538
539        case 'S':
540          sections = true;
541          break;
542
543        case '?':
544          usage (3);
545          break;
546
547        case 'h':
548          usage (0);
549          break;
550      }
551    }
552
553    /*
554     * Set the program name.
555     */
556    rld::set_progname (argv[0]);
557
558    argc -= optind;
559    argv += optind;
560
561    std::cout << "RTEMS Executable Info " << rld::version () << std::endl;
562    std::cout << " " << rld::get_cmdline () << std::endl;
563
564    /*
565     * All means all types of output.
566     */
567    if (all)
568    {
569      sections = true;
570      init = true;
571      fini = true;
572    }
573
574    /*
575     * If there is no executable there is nothing to convert.
576     */
577    if (argc == 0)
578      throw rld::error ("no executable", "options");
579    if (argc > 1)
580      throw rld::error ("only a single executable", "options");
581
582    /*
583     * The name of the executable.
584     */
585    exe_name = *argv;
586
587    if (rld::verbose ())
588      std::cout << "exe-image: " << exe_name << std::endl;
589
590    /*
591     * Open the executable and read the symbols.
592     */
593    rld::exeinfo::image exe (exe_name);
594
595    std::cout << "exe: " << exe.exe.name ().full () << std::endl;
596
597    /*
598     * Generate the output.
599     */
600    if (sections)
601      exe.output_sections ();
602    if (init)
603      exe.output_init ();
604    if (fini)
605      exe.output_fini ();
606
607    /*
608     * Map ?
609     */
610    if (map)
611      rld::symbols::output (std::cout, exe.symbols);
612  }
613  catch (rld::error re)
614  {
615    std::cerr << "error: "
616              << re.where << ": " << re.what
617              << std::endl;
618    ec = 10;
619  }
620  catch (std::exception e)
621  {
622    int   status;
623    char* realname;
624    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
625    std::cerr << "error: exception: " << realname << " [";
626    ::free (realname);
627    const std::type_info &ti = typeid (e);
628    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
629    std::cerr << realname << "] " << e.what () << std::endl << std::flush;
630    ::free (realname);
631    ec = 11;
632  }
633  catch (...)
634  {
635    /*
636     * Helps to know if this happens.
637     */
638    std::cerr << "error: unhandled exception" << std::endl;
639    ec = 12;
640  }
641
642  return ec;
643}
Note: See TracBrowser for help on using the repository browser.