summaryrefslogtreecommitdiffstats
path: root/linkers/rtems-exeinfo.cpp
blob: e0d88327786fdb1b2c75cb8a06d03a6cbaff5b8f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
/*
 * Copyright (c) 2016-2018, Chris Johns <chrisj@rtems.org>
 *
 * RTEMS Tools Project (http://www.rtems.org/)
 * This file is part of the RTEMS Tools package in 'rtems-tools'.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/**
 * @file
 *
 * @ingroup rtems_rld
 *
 * @brief RTEMS Init dumps the initialisation section data in a format we can
 *        read.
 *
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <iostream>
#include <iomanip>

#include <cxxabi.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <getopt.h>

#include <rld.h>
#include <rld-buffer.h>
#include <rld-dwarf.h>
#include <rld-files.h>
#include <rld-process.h>
#include <rld-rtems.h>

#ifndef HAVE_KILL
#define kill(p,s) raise(s)
#endif

namespace rld
{
  namespace exeinfo
  {
    /**
     * Default section list.
     */
    const char* default_init[] =
    {
      ".rtemsroset",
      ".ctors",
      ".init",
      0
    };

    const char* default_fini[] =
    {
      ".dtors",
      ".fini",
      0
    };

    /**
     * ARM section list.
     */
    const char* arm_init[] =
    {
      ".rtemsroset",
      ".init_array",
      0
    };

    const char* arm_fini[] =
    {
      ".fini_array",
      0
    };

    /**
     * An executable section's address, offset, size and alignment.
     */
    struct section
    {
      const files::section& sec;        //< The executable's section.
      buffer::buffer        data;       //< The section's data.
      files::byteorder      byteorder;  //< The image's byteorder.

      /**
       * Construct the section.
       */
      section (const files::section& sec, files::byteorder byteorder);

      /**
       * Copy construct.
       */
      section (const section& orig);

      /**
       * Clean up the section's memory.
       */
      ~section ();

    private:
      /**
       * Default constructor.
       */
      section ();
    };

    /**
     * Container of sections. Order is the address in memory.
     */
    typedef std::list < section > sections;

    /**
     * The kernel image.
     */
    struct image
    {
      files::object    exe;         //< The object file that is the executable.
      dwarf::file      debug;       //< The executable's DWARF details.
      symbols::table   symbols;     //< The synbols for a map.
      symbols::addrtab addresses;   //< The symbols keyed by address.
      files::sections  secs;        //< The sections in the executable.
      const char**     init;        //< The init section's list for the machinetype.
      const char**     fini;        //< The fini section's list for the machinetype.


      /**
       * Load the executable file.
       */
      image (const std::string exe_name);

      /**
       * Clean up.
       */
      ~image ();

      /*
       * Check the compiler and flags match.
       */
      void output_compilation_unit (bool objects);

      /*
       * Output the sections.
       */
      void output_sections ();

      /*
       * Output the init sections.
       */
      void output_init ();

      /*
       * Output the fini sections.
       */
      void output_fini ();

      /*
       * Output init/fini worker.
       */
      void output_init_fini (const char* label, const char** names);

      /*
       * Output the inlined functions.
       */
      void output_inlined ();

      /*
       * Output the DWARF data.
       */
      void output_dwarf ();
    };

    section::section (const files::section& sec, files::byteorder byteorder)
      : sec (sec),
        data (sec.size, byteorder == rld::files::little_endian),
        byteorder (byteorder)
    {
    }

    section::section (const section& orig)
      : sec (orig.sec),
        data (orig.data)
    {
    }

    section::~section ()
    {
    }

    /**
     * Helper for for_each to filter and load the sections we wish to
     * dump.
     */
    class section_loader:
      public std::unary_function < const files::section, void >
    {
    public:

      section_loader (image& img, sections& secs, const char* names[]);

      ~section_loader ();

      void operator () (const files::section& fsec);

    private:

      image&       img;
      sections&    secs;
      const char** names;
    };

    section_loader::section_loader (image&      img,
                                    sections&   secs,
                                    const char* names[])
      : img (img),
        secs (secs),
        names (names)
    {
    }

    section_loader::~section_loader ()
    {
    }

    void
    section_loader::operator () (const files::section& fsec)
    {
      if (rld::verbose () >= RLD_VERBOSE_DETAILS)
        std::cout << "init:section-loader: " << fsec.name
                  << " address=" << std::hex << fsec.address << std::dec
                  << " relocs=" << fsec.relocs.size ()
                  << " fsec.size=" << fsec.size
                  << " fsec.alignment=" << fsec.alignment
                  << " fsec.rela=" << fsec.rela
                  << std::endl;

      for (int n = 0; names[n] != 0; ++n)
      {
        if (fsec.name == names[n])
        {
          if (rld::verbose () >= RLD_VERBOSE_DETAILS)
            std::cout << "init:section-loader: " << fsec.name
                      << " added" << std::endl;

          section sec (fsec, img.exe.get_byteorder ());
          img.exe.seek (fsec.offset);
          sec.data.read (img.exe, fsec.size);
          secs.push_back (sec);
          break;
        }
      }
    }

    image::image (const std::string exe_name)
      : exe (exe_name),
        init (0),
        fini (0)
    {
      /*
       * Open the executable file and begin the session on it.
       */
      exe.open ();
      exe.begin ();
      debug.begin (exe.elf ());

      if (!exe.valid ())
        throw rld::error ("Not valid: " + exe.name ().full (),
                          "init::image");

      /*
       * Set up the section lists for the machiner type.
       */
      switch (exe.elf ().machinetype ())
      {
        case EM_ARM:
          init = arm_init;
          fini = arm_fini;
          break;
        default:
          init  = default_init;
          fini  = default_fini;
          break;
      }

      /*
       * Load the symbols and sections.
       */
      exe.load_symbols (symbols, true);
      debug.load_debug ();
      debug.load_types ();
      debug.load_functions ();
      symbols.globals (addresses);
      symbols.weaks (addresses);
      symbols.locals (addresses);
      exe.get_sections (secs);
    }

    image::~image ()
    {
    }

    void
    image::output_compilation_unit (bool objects)
    {
      dwarf::compilation_units& cus = debug.get_cus ();

      std::cout << "Compilation: " << std::endl;

      rld::strings flag_exceptions = { "-O",
                                       "-g",
                                       "-mtune=",
                                       "-fno-builtin",
                                       "-fno-inline",
                                       "-fexceptions",
                                       "-fnon-call-exceptions",
                                       "-fvisibility=",
                                       "-fno-stack-protector",
                                       "-fbuilding-libgcc",
                                       "-fno-implicit-templates",
                                       "-fimplicit-templates",
                                       "-ffunction-sections",
                                       "-fdata-sections",
                                       "-frandom-seed=",
                                       "-fno-common",
                                       "-fno-keep-inline-functions" };

      dwarf::producer_sources producers;

      debug.get_producer_sources (producers);

      /*
       * Find which flags are common to the building of all source. We are only
       * interested in files that have any flags. This filters out things like
       * the assembler which does not have flags.
       */

      rld::strings all_flags;

      size_t source_max = 0;

      for (auto& p : producers)
      {
        dwarf::source_flags_compare compare;
        std::sort (p.sources.begin (), p.sources.end (), compare);

        for (auto& s : p.sources)
        {
          size_t len = rld::path::basename (s.source).length ();
          if (len > source_max)
            source_max = len;

          if (!s.flags.empty ())
          {
            for (auto& f : s.flags)
            {
              bool add = true;
              for (auto& ef : flag_exceptions)
              {
                if (rld::starts_with (f, ef))
                {
                  add = false;
                  break;
                }
              }
              if (add)
              {
                for (auto& af : all_flags)
                {
                  if (f == af)
                  {
                    add = false;
                    break;
                  }
                }
                if (add)
                  all_flags.push_back (f);
              }
            }
          }
        }
      }

      rld::strings common_flags;

      for (auto& flag : all_flags)
      {
        bool found_in_all = true;
        for (auto& p : producers)
        {
          for (auto& s : p.sources)
          {
            if (!s.flags.empty ())
            {
              bool flag_found = false;
              for (auto& f : s.flags)
              {
                if (flag == f)
                {
                  flag_found = true;
                  break;
                }
              }
              if (!flag_found)
              {
                found_in_all = false;
                break;
              }
            }
            if (!found_in_all)
              break;
          }
        }
        if (found_in_all)
          common_flags.push_back (flag);
      }

      std::cout << " Producers: " << producers.size () << std::endl;

      for (auto& p : producers)
      {
        std::cout << "  | " << p.producer
                  << ": " << p.sources.size () << " objects" << std::endl;
      }

      std::cout << " Common flags: " << common_flags.size () << std::endl
                << "  |";

      for (auto& f : common_flags)
        std::cout << ' ' << f;
      std::cout << std::endl;

      if (objects)
      {
        std::cout << " Object files: " << cus.size () << std::endl;

        rld::strings filter_flags = common_flags;
        filter_flags.insert (filter_flags.end (),
                             flag_exceptions.begin (),
                             flag_exceptions.end());

        for (auto& p : producers)
        {
          std::cout << ' ' << p.producer
                    << ": " << p.sources.size () << " objects" << std::endl;
          for (auto& s : p.sources)
          {
            std::cout << "   | "
                      << std::setw (source_max + 1) << std::left
                      << rld::path::basename (s.source);
            if (!s.flags.empty ())
            {
              bool first = true;
              for (auto& f : s.flags)
              {
                bool present = false;
                for (auto& ff : filter_flags)
                {
                  if (rld::starts_with(f, ff))
                  {
                    present = true;
                    break;
                  }
                }
                if (!present)
                {
                  if (first)
                  {
                    std::cout << ':';
                    first = false;
                  }
                  std::cout << ' ' << f;
                }
              }
            }
            std::cout << std::endl;
          }
        }
      }

      std::cout << std::endl;
    }

    void
    image::output_sections ()
    {
      std::cout << "Sections: " << secs.size () << std::endl;

      size_t max_section_name = 0;

      for (files::sections::const_iterator si = secs.begin ();
           si != secs.end ();
           ++si)
      {
        const files::section& sec = *si;
        if (sec.name.length() > max_section_name)
          max_section_name = sec.name.length();
      }

      for (files::sections::const_iterator si = secs.begin ();
           si != secs.end ();
           ++si)
      {
        const files::section& sec = *si;

        #define SF(f, i, c) if (sec.flags & (f)) flags[i] = c

        std::string flags ("--------------");

        SF (SHF_WRITE,            0, 'W');
        SF (SHF_ALLOC,            1, 'A');
        SF (SHF_EXECINSTR,        2, 'E');
        SF (SHF_MERGE,            3, 'M');
        SF (SHF_STRINGS,          4, 'S');
        SF (SHF_INFO_LINK,        5, 'I');
        SF (SHF_LINK_ORDER,       6, 'L');
        SF (SHF_OS_NONCONFORMING, 7, 'N');
        SF (SHF_GROUP,            8, 'G');
        SF (SHF_TLS,              9, 'T');
        SF (SHF_AMD64_LARGE,     10, 'a');
        SF (SHF_ENTRYSECT,       11, 'e');
        SF (SHF_COMDEF,          12, 'c');
        SF (SHF_ORDERED,         13, 'O');

        std::cout << "  " << std::left
                  << std::setw (max_section_name) << sec.name
                  << " " << flags
                  << std::right << std::hex << std::setfill ('0')
                  << " addr: 0x" << std::setw (8) << sec.address
                  << " 0x" << std::setw (8) << sec.address + sec.size
                  << std::dec << std::setfill (' ')
                  << " size: " << std::setw (10) << sec.size
                  << " align: " << std::setw (3) << sec.alignment
                  << " relocs: " << std::setw (6) << sec.relocs.size ()
                  << std::endl;
      }

      std::cout << std::endl;
    }

    void
    image::output_init ()
    {
      output_init_fini ("Init", init);
    }

    void
    image::output_fini ()
    {
      output_init_fini ("Fini", fini);
    }

    void
    image::output_init_fini (const char* label, const char** names)
    {
      /*
       * Load the sections.
       */
      sections ifsecs;
      std::for_each (secs.begin (), secs.end (),
                     section_loader (*this, ifsecs, names));

      std::cout << label << " sections: " << ifsecs.size () << std::endl;

      for (auto& sec : ifsecs)
      {
        const size_t machine_size = exe.elf ().machine_size ();
        const int    count = sec.data.level () / machine_size;

        std::cout << " " << sec.sec.name << std::endl;

        for (int i = 0; i < count; ++i)
        {
          uint32_t         address;
          symbols::symbol* sym;
          sec.data >> address;
          sym = addresses[address];
          std::cout << "  "
                    << std::hex << std::setfill ('0')
                    << "0x" << std::setw (8) << address
                    << std::dec << std::setfill ('0');
          if (sym)
          {
            std::string label = sym->name ();
            if (rld::symbols::is_cplusplus (label))
              rld::symbols::demangle_name (label, label);
            std::cout << " " << label;
          }
          else
          {
            std::cout << " no symbol";
          }
          std::cout << std::endl;
        }
      }

      std::cout << std::endl;
    }

    struct func_count
    {
      std::string name;
      int         count;
      size_t      size;

      func_count (std::string name, size_t size)
        : name (name),
          count (1),
          size (size) {
      }
    };
    typedef std::vector < func_count > func_counts;

    void image::output_inlined ()
    {
      size_t           total = 0;
      size_t           total_size = 0;
      size_t           inlined_size = 0;
      dwarf::functions funcs_inlined;
      dwarf::functions funcs_not_inlined;
      func_counts      counts;

      for (auto& cu : debug.get_cus ())
      {
        for (auto& f : cu.get_functions ())
        {
          if (f.size () > 0 && f.has_machine_code ())
          {
            bool counted;
            ++total;
            total_size += f.size ();
            switch (f.get_inlined ())
            {
              case dwarf::function::inl_inline:
              case dwarf::function::inl_declared_inlined:
                inlined_size += f.size ();
                counted = false;
                for (auto& c : counts)
                {
                  if (c.name == f.name ())
                  {
                    ++c.count;
                    c.size += f.size ();
                    counted = true;
                    break;
                  }
                }
                if (!counted)
                  counts.push_back (func_count (f.name (), f.size ()));
                funcs_inlined.push_back (f);
                break;
              case dwarf::function::inl_declared_not_inlined:
                funcs_not_inlined.push_back (f);
                break;
              default:
                break;
            }
          }
        }
      }

      std::cout << "inlined funcs   : " << funcs_inlined.size () << std::endl
                << "    total funcs : " << total << std::endl
                << " % inline funcs : " << (funcs_inlined.size () * 100) / total << '%'
                << std::endl
                << "     total size : " << total_size << std::endl
                << "    inline size : " << inlined_size << std::endl
                << "  % inline size : " << (inlined_size * 100) / total_size << '%'
                << std::endl;

      auto count_compare = [](func_count const & a, func_count const & b) {
        return a.size != b.size?  a.size < b.size : a.count > b.count;
      };
      std::sort (counts.begin (), counts.end (), count_compare);
      std::reverse (counts.begin (), counts.end ());

      std::cout  << std::endl << "inlined repeats : " << std::endl;
      for (auto& c : counts)
        if (c.count > 1)
          std::cout << std::setw (6) << c.size << ' '
                    << std::setw (4) << c.count << ' '
                    << c.name << std::endl;

      dwarf::function_compare compare (dwarf::function_compare::fc_by_size);

      std::sort (funcs_inlined.begin (), funcs_inlined.end (), compare);
      std::reverse (funcs_inlined.begin (), funcs_inlined.end ());

      std::cout << std::endl << "inline funcs : " << std::endl;
      for (auto& f : funcs_inlined)
      {
        std::string flags;

        std::cout << std::setw (6) << f.size () << ' '
                  << (char) (f.is_external () ? 'E' : ' ')
                  << (char) (f.get_inlined () == dwarf::function::inl_inline ? 'C' : ' ')
                  << std::hex << std::setfill ('0')
                  << " 0x" << std::setw (8) << f.pc_low ()
                  << std::dec << std::setfill (' ')
                  << ' ' << f.name ()
                  << std::endl;
      }

      if (funcs_not_inlined.size () > 0)
      {
        std::sort (funcs_not_inlined.begin (), funcs_not_inlined.end (), compare);
        std::reverse (funcs_not_inlined.begin (), funcs_not_inlined.end ());

        std::cout << std::endl << "inline funcs not inlined: " << std::endl;
        for (auto& f : funcs_not_inlined)
        {
          std::cout << std::setw (6) << f.size () << ' '
                    << (char) (f.is_external () ? 'E' : ' ')
                    << (char) (f.get_inlined () == dwarf::function::inl_inline ? 'C' : ' ')
                    << std::hex << std::setfill ('0')
                    << " 0x" << std::setw (8) << f.pc_low ()
                    << std::dec << std::setfill (' ')
                    << ' ' << f.name ()
                    << std::endl;
        }
      }
    }

    void image::output_dwarf ()
    {
      std::cout << "DWARF Data:" << std::endl;
      debug.dump (std::cout);
    }
  }
}

/**
 * RTEMS Exe Info options. This needs to be rewritten to be like cc where only
 * a single '-' and long options is present.
 */
static struct option rld_opts[] = {
  { "help",        no_argument,            NULL,           'h' },
  { "version",     no_argument,            NULL,           'V' },
  { "verbose",     no_argument,            NULL,           'v' },
  { "map",         no_argument,            NULL,           'M' },
  { "all",         no_argument,            NULL,           'a' },
  { "sections",    no_argument,            NULL,           'S' },
  { "init",        no_argument,            NULL,           'I' },
  { "fini",        no_argument,            NULL,           'F' },
  { "inlined",     no_argument,            NULL,           'i' },
  { "dwarf",       no_argument,            NULL,           'D' },
  { NULL,          0,                      NULL,            0 }
};

void
usage (int exit_code)
{
  std::cout << "rtems-exeinfo [options] objects" << std::endl
            << "Options and arguments:" << std::endl
            << " -h        : help (also --help)" << std::endl
            << " -V        : print linker version number and exit (also --version)" << std::endl
            << " -v        : verbose (trace import parts), can supply multiple times" << std::endl
            << "             to increase verbosity (also --verbose)" << std::endl
            << " -M        : generate map output (also --map)" << std::endl
            << " -a        : all output excluding the map and DAWRF (also --all)" << std::endl
            << " -S        : show all section (also --sections)" << std::endl
            << " -I        : show init section tables (also --init)" << std::endl
            << " -F        : show fini section tables (also --fini)" << std::endl
            << " -O        : show object files (also --objects)" << std::endl
            << " -i        : show inlined code (also --inlined)" << std::endl
            << " -D        : dump the DWARF data (also --dwarf)" << std::endl;
  ::exit (exit_code);
}

static void
fatal_signal (int signum)
{
  signal (signum, SIG_DFL);

  rld::process::temporaries_clean_up ();

  /*
   * Get the same signal again, this time not handled, so its normal effect
   * occurs.
   */
  kill (getpid (), signum);
}

static void
setup_signals (void)
{
  if (signal (SIGINT, SIG_IGN) != SIG_IGN)
    signal (SIGINT, fatal_signal);
#ifdef SIGHUP
  if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
    signal (SIGHUP, fatal_signal);
#endif
  if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
    signal (SIGTERM, fatal_signal);
#ifdef SIGPIPE
  if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
    signal (SIGPIPE, fatal_signal);
#endif
#ifdef SIGCHLD
  signal (SIGCHLD, SIG_DFL);
#endif
}

void
unhandled_exception (void)
{
  std::cerr << "error: exception handling error, please report" << std::endl;
  exit (1);
}

int
main (int argc, char* argv[])
{
  int ec = 0;

  setup_signals ();

  std::set_terminate(unhandled_exception);

  try
  {
    std::string exe_name;
    bool        map = false;
    bool        all = false;
    bool        sections = false;
    bool        init = false;
    bool        fini = false;
    bool        objects = false;
    bool        inlined = false;
    bool        dwarf_data = false;

    rld::set_cmdline (argc, argv);

    while (true)
    {
      int opt = ::getopt_long (argc, argv, "hvVMaSIFiD", rld_opts, NULL);
      if (opt < 0)
        break;

      switch (opt)
      {
        case 'V':
          std::cout << "rtems-exeinfo (RTEMS Executable Info) " << rld::version ()
                    << ", RTEMS revision " << rld::rtems::version ()
                    << std::endl;
          ::exit (0);
          break;

        case 'v':
          rld::verbose_inc ();
          break;

        case 'M':
          map = true;
          break;

        case 'a':
          all = true;
          break;

        case 'I':
          init = true;
          break;

        case 'F':
          fini = true;
          break;

        case 'S':
          sections = true;
          break;

        case 'O':
          objects = true;
          break;

        case 'i':
          inlined = true;
          break;

        case 'D':
          dwarf_data = true;
          break;

        case '?':
          usage (3);
          break;

        case 'h':
          usage (0);
          break;
      }
    }

    /*
     * Set the program name.
     */
    rld::set_progname (argv[0]);

    argc -= optind;
    argv += optind;

    std::cout << "RTEMS Executable Info " << rld::version () << std::endl;
    std::cout << " " << rld::get_cmdline () << std::endl;

    /*
     * All means all types of output.
     */
    if (all)
    {
      sections = true;
      init = true;
      fini = true;
      objects = true;
      inlined = true;
    }

    /*
     * If there is no executable there is nothing to convert.
     */
    if (argc == 0)
      throw rld::error ("no executable", "options");
    if (argc > 1)
      throw rld::error ("only a single executable", "options");

    /*
     * The name of the executable.
     */
    exe_name = *argv;

    if (rld::verbose ())
      std::cout << "exe-image: " << exe_name << std::endl;

    /*
     * Open the executable and read the symbols.
     */
    rld::exeinfo::image exe (exe_name);

    std::cout << "exe: " << exe.exe.name ().full () << std::endl
              << std::endl;

    /*
     * Generate the output.
     */
    exe.output_compilation_unit (objects);
    if (sections)
      exe.output_sections ();
    if (init)
      exe.output_init ();
    if (fini)
      exe.output_fini ();
    if (inlined)
      exe.output_inlined ();
    if (dwarf_data)
      exe.output_dwarf ();

    /*
     * Map ?
     */
    if (map)
      rld::symbols::output (std::cout, exe.symbols);
  }
  catch (rld::error re)
  {
    std::cerr << "error: "
              << re.where << ": " << re.what
              << std::endl;
    ec = 10;
  }
  catch (std::exception e)
  {
    int   status;
    char* realname;
    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
    std::cerr << "error: exception: " << realname << " [";
    ::free (realname);
    const std::type_info &ti = typeid (e);
    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
    std::cerr << realname << "] " << e.what () << std::endl << std::flush;
    ::free (realname);
    ec = 11;
  }
  catch (...)
  {
    /*
     * Helps to know if this happens.
     */
    std::cerr << "error: unhandled exception" << std::endl;
    ec = 12;
  }

  return ec;
}