summaryrefslogtreecommitdiff
path: root/rtems-tld.cpp
blob: 08616745a6cab15e95dc9dc943d7d53d066db366 (plain)
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
/*
 * Copyright (c) 2014, Chris Johns <chrisj@rtems.org>
 *
 * 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 Trace Linker manages creating a tracable RTEMS executable.
 *
 */

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

#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <locale>
#include <sstream>

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

#include <getopt.h>

#include <rld.h>
#include <rld-cc.h>
#include <rld-config.h>
#include <rld-process.h>
#include <rld-rtems.h>

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

namespace rld
{
  /**
   * RTEMS Trace Linker.
   */
  namespace trace
  {
    /**
     * A container of arguments.
     */
    typedef std::vector < std::string > function_args;

    /**
     * The return value.
     */
    typedef std::string function_return;

    /**
     * A function's signature.
     */
    struct signature
    {
      std::string     name; /**< The function's name. */
      function_args   args; /**< The function's list of arguments. */
      function_return ret;  /**< The fuctions return value. */

      /**
       * The default constructor.
       */
      signature ();

      /**
       * Construct the signature loading it from the configuration.
       */
      signature (const rld::config::record& record);

      /**
       * Return the function's declaration.
       */
      const std::string decl () const;
    };

    /**
     * A container of signatures.
     */
    typedef std::map < std::string, signature > signatures;

    /**
     * A function is list of function signatures headers and defines that allow
     * a function to be wrapped.
     */
    struct function
    {
      std::string  name;        /**< The name of this wrapper. */
      rld::strings headers;     /**< Include statements. */
      rld::strings defines;     /**< Define statements. */
      signatures   signatures_; /**< Signatures in this function. */

      /**
       * Load the function.
       */
      function (rld::config::config& config,
                const std::string&   name);

      /**
       * Dump the function.
       */
      void dump (std::ostream& out) const;
    };

    /**
     * A container of functions.
     */
    typedef std::vector < function > functions;

    /**
     * A generator and that contains the functions used to trace arguments and
     * return values. It also provides the implementation of those functions.
     */
    struct generator
    {
      std::string  name;            /**< The name of this wrapper. */
      rld::strings headers;         /**< Include statements. */
      rld::strings defines;         /**< Define statements. */
      std::string  map_sym_prefix;  /**< Mapping symbol prefix. */
      std::string  arg_trace;       /**< Code template to trace an argument. */
      std::string  ret_trace;       /**< Code template to trace the return value. */
      rld::strings code;            /**< Code block inserted before the trace code. */

      /**
       * Default constructor.
       */
      generator ();

      /**
       * Load the generator.
       */
      generator (rld::config::config& config,
                 const std::string&   name);

      /**
       * Dump the generator.
       */
      void dump (std::ostream& out) const;
    };

    /**
     * Tracer.
     */
    class tracer
    {
    public:
      tracer ();

      /**
       * Load the user's configuration.
       */
      void load (rld::config::config& config,
                 const std::string&   section);

      /**
       * The the functions for the trace.
       */
      void load_functions (rld::config::config&        config,
                           const rld::config::section& section);

      /**
       * The the traces for the tracer.
       */
      void load_traces (rld::config::config&        config,
                        const rld::config::section& section);

      /**
       * Generate the wrapper object file.
       */
      const std::string generate ();

      /**
       * Generate the trace functions.
       */
      void generate_traces (rld::process::tempfile& c);

      /**
       * Dump the wrapper.
       */
      void dump (std::ostream& out) const;

    private:

      std::string  name;       /**< The name of the trace. */
      std::string  bsp;        /**< The BSP we are linking to. */
      rld::strings traces;     /**< The functions to trace. */
      functions    functions_; /**< The functions that can be traced. */
      generator    generator_; /**< The tracer's generator. */
    };

    /**
     * Trace Linker.
     */
    class linker
    {
    public:
      linker ();

      /**
       * Load the user's configuration.
       */
      void load_config (const std::string& path,
                        const std::string& trace);

      /**
       * Generate the C file.
       */
      void generate_wrapper ();

      /**
       * Compile the C file.
       */
      void compile_wrapper ();

      /**
       * Dump the linker.
       */
      void dump (std::ostream& out) const;

    private:

      rld::config::config config;     /**< User configuration. */
      tracer              tracer_;    /**< The tracer */
      std::string         wrapper_c;  /**< Wrapper C source file. */
      std::string         wrapper_o;  /**< Wrapper object file. */
    };

    /**
     * Recursive parser for strings.
     */
    void
    parse (rld::config::config&        config,
           const rld::config::section& section,
           const std::string&          sec_name,
           const std::string&          rec_name,
           rld::strings&               items,
           bool                        split = true,
           int                         depth = 0)
    {
      if (depth > 32)
        throw rld::error ("too deep", "parsing: " + sec_name + '/' + rec_name);

      rld::config::parse_items (section, rec_name, items, false, false, split);

      rld::strings sl;

      rld::config::parse_items (section, sec_name, sl);

      for (rld::strings::iterator sli = sl.begin ();
           sli != sl.end ();
           ++sli)
      {
        const rld::config::section& sec = config.get_section (*sli);
        parse (config, sec, sec_name, rec_name, items, split, depth + 1);
      }

      /*
       * Make the items unique.
       */
      rld::strings::iterator ii;
      ii = std::unique (items.begin (), items.end ());
      items.resize (std::distance (items.begin (), ii));
    }

    signature::signature ()
    {
    }

    signature::signature (const rld::config::record& record)
    {
      /*
       * There can only be one function signature in the configuration.
       */
      if (!record.single ())
        throw rld::error ("duplicate", "signature: " + record.name);

      name = record.name;

      /*
       * Signatures are defined as the return value then the arguments
       * delimited by a comma and white space. No checking is made of the
       * return value or arguments.
       */
      rld::strings si;
      rld::config::parse_items (record, si);

      if (si.size () == 0)
        throw rld::error ("no return value", "signature: " + record.name);
      if (si.size () == 1)
          throw rld::error ("no arguments", "signature: " + record.name);

      ret = si[0];
      args.resize (si.size () - 1);
      std::copy (si.begin ()  + 1, si.end (), args.begin ());
    }

    const std::string
    signature::decl () const
    {
      std::string ds = ret + ' ' + name + '(';
      int         arg = 0;
      for (function_args::const_iterator ai = args.begin ();
           ai != args.end ();
           ++ai)
        {
          if (ai != args.begin ())
            ds += ", ";
          ds += (*ai) + " a" + rld::to_string (++arg);
        }
      ds += ')';
      return ds;
    }

    function::function (rld::config::config& config,
                        const std::string&   name)
      : name (name)
    {
      /*
       * A function section optionally contain one or more records of:
       *
       * # headers     A list of sections containing headers or header records.
       * # header      A list of include string that are single or double quoted.
       * # defines     A list of sections containing defines or define record.
       * # defines     A list of define string that are single or double quoted.
       * # signatures  A list of section names of signatures.
       * # includes    A list of files to include.
       *
       * @note The quoting and list spliting is a little weak because a delimiter
       *       in a quote should not be seen as a delimiter.
       */
      const rld::config::section& section = config.get_section (name);

      config.includes (section);

      parse (config, section, "headers", "header", headers);
      parse (config, section, "defines", "define", defines);

      rld::strings sig_list;
      section.get_record_items ("signatures", sig_list);

      for (rld::strings::const_iterator sli = sig_list.begin ();
           sli != sig_list.end ();
           ++sli)
      {
        const rld::config::section& sig_sec = config.get_section (*sli);
        for (rld::config::records::const_iterator si = sig_sec.recs.begin ();
             si != sig_sec.recs.end ();
             ++si)
        {
          signature sig (*si);
          signatures_[sig.name] = sig;
        }
      }
    }

    void
    function::dump (std::ostream& out) const
    {
      out << "   Function: " << name << std::endl
          << "    Headers: " << headers.size () << std::endl;
      for (rld::strings::const_iterator hi = headers.begin ();
           hi != headers.end ();
           ++hi)
      {
        out << "     " << (*hi) << std::endl;
      }
      out << "   Defines: " << defines.size () << std::endl;
      for (rld::strings::const_iterator di = defines.begin ();
           di != defines.end ();
           ++di)
      {
        out << "     " << (*di) << std::endl;
      }
      out << "   Signatures: " << signatures_.size () << std::endl;
      for (signatures::const_iterator si = signatures_.begin ();
           si != signatures_.end ();
           ++si)
      {
        const signature& sig = (*si).second;
        out << "     " << sig.name << ": " << sig.decl () << ';' << std::endl;
      }
    }

    generator::generator ()
    {
    }

    generator::generator (rld::config::config& config,
                          const std::string&   name)
      : name (name)
    {
      /*
       * A generator section optionally contain one or more records of:
       *
       * # headers     A list of sections containing headers or header records.
       * # header      A list of include string that are single or double quoted.
       * # defines     A list of sections containing defines or define record.
       * # defines     A list of define string that are single or double quoted.
       * # code-blocks A list of section names of code blocks.
       * # includes    A list of files to include.
       *
       * @note The quoting and list spliting is a little weak because a delimiter
       *       in a quote should not be seen as a delimiter.
       */
      const rld::config::section& section = config.get_section (name);

      config.includes (section);

      parse (config, section, "headers",     "header", headers);
      parse (config, section, "defines",     "define", defines);
      parse (config, section, "code-blocks", "code",   code, false);

      map_sym_prefix = section.get_record_item ("map-sym-prefix");
      arg_trace = rld::dequote (section.get_record_item ("arg-trace"));
      ret_trace = rld::dequote (section.get_record_item ("ret-trace"));
    }

    void
    generator::dump (std::ostream& out) const
    {
      out << "  Generator: " << name << std::endl
          << "   Headers: " << headers.size () << std::endl;
      for (rld::strings::const_iterator hi = headers.begin ();
           hi != headers.end ();
           ++hi)
      {
        out << "    " << (*hi) << std::endl;
      }
      out << "   Defines: " << defines.size () << std::endl;
      for (rld::strings::const_iterator di = defines.begin ();
           di != defines.end ();
           ++di)
      {
        out << "    " << (*di) << std::endl;
      }
      out << "   Mapping Symbol Prefix: " << map_sym_prefix << std::endl
          << "   Arg Trace Code: " << arg_trace << std::endl
          << "   Return Trace Code: " << ret_trace << std::endl
          << "   Code blocks: " << std::endl;
      for (rld::strings::const_iterator ci = code.begin ();
           ci != code.end ();
           ++ci)
      {
        out << "      > "
            << rld::find_replace (*ci, "\n", "\n      | ") << std::endl;
      }
    }

    tracer::tracer ()
    {
    }

    void
    tracer::load (rld::config::config& config,
                  const std::string&   tname)
    {
      /*
       * The configuration must contain a "section" section. This is the top level
       * configuration and may contain:
       *
       *  # name      The name of trace being linked.
       *  # bsp       The architecture/bsp name of the BSP.
       *  # options   A list of options as per the long command line args.
       *  # traces    The list of sections containing function lists to trace.
       *  # functions The list of sections containing function details.
       *  # include   The list of files to include.
       *
       * The following records are required:
       *
       *  # name
       *  # bsp
       *  # trace
       *  # functions
       */
      const rld::config::section& section = config.get_section (tname);

      config.includes (section);

      name = section.get_record_item ("name");
      bsp  = section.get_record_item ("bsp");

      load_functions (config, section);
      load_traces (config, section);
    }

    void
    tracer::load_functions (rld::config::config&        config,
                            const rld::config::section& section)
    {
      rld::strings fl;
      rld::config::parse_items (section, "functions", fl, true);
      for (rld::strings::const_iterator fli = fl.begin ();
           fli != fl.end ();
           ++fli)
      {
        functions_.push_back (function (config, *fli));
      }
    }

    void
    tracer::load_traces (rld::config::config&        config,
                         const rld::config::section& section)
    {
      parse (config, section, "traces", "trace", traces);

      rld::strings gens;
      std::string  gen;

      parse (config, section, "traces", "generator", gens);

      if (gens.size () > 1)
        throw rld::error ("duplicate generators", "tracer: " + section.name);

      if (gens.size () == 0)
      {
        gen =
          config.get_section ("default-generator").get_record_item ("generator");
      }
      else
      {
        gen = gens[0];
      }

      generator_ = generator (config, gen);
    }

    const std::string
    tracer::generate ()
    {
      rld::process::tempfile c (".c");

      c.open (true);

      if (rld::verbose ())
        std::cout << "wrapper C file: " << c.name () << std::endl;

      try
      {
        c.write_line ("/*");
        c.write_line (" * RTEMS Trace Linker Wrapper");
        c.write_line (" *  Automatically generated.");
        c.write_line (" */");

        c.write_line ("");
        c.write_line ("/*");
        c.write_line (" * Generator: " + generator_.name);
        c.write_line (" */");
        c.write_lines (generator_.defines);
        c.write_lines (generator_.headers);
        c.write_line ("");
        c.write_lines (generator_.code);

        generate_traces (c);
      }
      catch (...)
      {
        c.close ();
        throw;
      }

      c.close ();

      return c.name ();
    }

    void
    tracer::generate_traces (rld::process::tempfile& c)
    {
      for (functions::const_iterator fi = functions_.begin ();
           fi != functions_.end ();
           ++fi)
      {
        const function& funcs = *fi;

        for (rld::strings::const_iterator ti = traces.begin ();
             ti != traces.end ();
             ++ti)
        {
          const std::string&         trace = *ti;
          signatures::const_iterator si = funcs.signatures_.find (trace);

          if (si != funcs.signatures_.end ())
          {
            c.write_line ("");
            c.write_line ("/*");
            c.write_line (" * Function: " + funcs.name);
            c.write_line (" */");
            c.write_lines (funcs.defines);
            c.write_lines (funcs.headers);
            break;
          }
        }
      }

      c.write_line ("");
      c.write_line ("/*");
      c.write_line (" * Wrappers.");
      c.write_line (" */");

      for (rld::strings::const_iterator ti = traces.begin ();
           ti != traces.end ();
           ++ti)
      {
        const std::string& trace = *ti;
        bool               found = false;

        for (functions::const_iterator fi = functions_.begin ();
             fi != functions_.end ();
             ++fi)
        {
          const function&            funcs = *fi;
          signatures::const_iterator si = funcs.signatures_.find (trace);

          if (si != funcs.signatures_.end ())
          {
            found = true;

            const signature& sig = (*si).second;

            c.write_line("");
            c.write_line(sig.decl ());
            c.write_line("{");

            std::string l;

            /*
             * @todo Need to define as part of the function signature if ret
             *       processing is required.
             */
            if (sig.ret != "void")
            {
              c.write_line(" " + sig.ret + " ret;");
              l = " ret =";
            }

            l += " " + generator_.map_sym_prefix + sig.name + '(';
            for (size_t a = 0; a < sig.args.size (); ++a)
            {
              if (a)
                l += ", ";
              l += "a" + rld::to_string ((int) (a + 1));
            }
            l += ");";
            c.write_line(l);

            if (sig.ret != "void")
            {
              c.write_line(" return ret;");
            }

            c.write_line("}");
          }
        }

        if (!found)
          throw rld::error ("not found", "trace function: " + trace);
      }
    }

    void
    tracer::dump (std::ostream& out) const
    {
      out << " Tracer: " << name << std::endl
          << "  BSP: " << bsp << std::endl
          << "  Traces: " << traces.size () << std::endl;
      for (rld::strings::const_iterator ti = traces.begin ();
           ti != traces.end ();
           ++ti)
      {
        out << "   " << (*ti) << std::endl;
      }
      out << "  Functions: " << functions_.size () << std::endl;
      for (functions::const_iterator fi = functions_.begin ();
           fi != functions_.end ();
           ++fi)
      {
        (*fi).dump (out);
      }
      out << "  Generator: " << std::endl;
      generator_.dump (out);
    }

    linker::linker ()
    {
    }

    void
    linker::load_config (const std::string& path,
                         const std::string& trace)
    {
      config.clear ();
      config.load (path);
      tracer_.load (config, trace);
    }

    void
    linker::generate_wrapper ()
    {
      wrapper_c = tracer_.generate ();
    }

    void
    linker::compile_wrapper ()
    {
     rld::process::arg_container args;

      rld::process::tempfile o (".o");

      if (rld::verbose ())
        std::cout << "wrapper O file: " << o.name () << std::endl;

      rld::cc::make_cc_command (args);
      rld::cc::append_flags (rld::cc::ft_cflags, args);

      args.push_back ("-c");
      args.push_back ("-o ");
      args.push_back (o.name ());
      args.push_back (wrapper_c);

      rld::process::tempfile out;
      rld::process::tempfile err;
      rld::process::status   status;

      status = rld::process::execute (rld::cc::get_cc (),
                                      args,
                                      out.name (),
                                      err.name ());

      if ((status.type != rld::process::status::normal) ||
          (status.code != 0))
      {
        err.output (rld::cc::get_cc (), std::cout);
        throw rld::error ("Compiler error", "compiling wrapper");
      }

      wrapper_o = o.name ();
    }

    void
    linker::dump (std::ostream& out) const
    {
      const rld::config::paths& cpaths = config.get_paths ();
      out << " Configuration Files: " << cpaths.size () << std::endl;
      for (rld::config::paths::const_iterator pi = cpaths.begin ();
           pi != cpaths.end ();
           ++pi)
      {
        out << "  " << (*pi) << std::endl;
      }

      tracer_.dump (out);
    }
  }
}

/**
 * RTEMS Trace Linker options. This needs to be rewritten to be like cc where
 * only a single '-' and long options is present. Anything after '--' is passed
 * to RTEMS's real linker.
 */
static struct option rld_opts[] = {
  { "help",        no_argument,            NULL,           'h' },
  { "version",     no_argument,            NULL,           'V' },
  { "verbose",     no_argument,            NULL,           'v' },
  { "warn",        no_argument,            NULL,           'w' },
  { "keep",        no_argument,            NULL,           'k' },
  { "exec-prefix", required_argument,      NULL,           'E' },
  { "cflags",      required_argument,      NULL,           'c' },
  { "rtems",       required_argument,      NULL,           'r' },
  { "rtems-bsp",   required_argument,      NULL,           'B' },
  { "config",      required_argument,      NULL,           'C' },
  { NULL,          0,                      NULL,            0 }
};

void
usage (int exit_code)
{
  std::cout << "rtems-trace-ld [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
            << " -w        : generate warnings (also --warn)" << std::endl
            << " -k        : keep temporary files (also --keep)" << std::endl
            << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl
            << " -c cflags : C compiler flags (also --cflags)" << std::endl
            << " -r path   : RTEMS path (also --rtems)" << std::endl
            << " -B bsp    : RTEMS arch/bsp (also --rtems-bsp)" << std::endl
            << " -C ini    : user configuration INI file (also --config)" << 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
}

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

  setup_signals ();

  try
  {
    rld::trace::linker linker;
    std::string        ld_cmd;
    std::string        configuration;
    std::string        trace = "tracer";
    bool               arch_bsp_load = false;

    while (true)
    {
      int opt = ::getopt_long (argc, argv, "hvwkVE:c:C:r:B:", rld_opts, NULL);
      if (opt < 0)
        break;

      switch (opt)
      {
        case 'V':
          std::cout << "rtems-trace-ld (RTEMS Trace Linker) " << rld::version ()
                    << std::endl;
          ::exit (0);
          break;

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

        case 'w':
#if HAVE_WARNINGS
          warnings = true;
#endif
          break;

        case 'k':
          rld::process::set_keep_temporary_files ();
          break;

        case 'E':
          rld::cc::set_exec_prefix (optarg);
          break;

        case 'c':
          rld::cc::append_flags (optarg, rld::cc::ft_cflags);
          break;

        case 'r':
          rld::rtems::path = optarg;
          break;

        case 'B':
          rld::rtems::arch_bsp = optarg;
          arch_bsp_load = true;
          break;

        case 'C':
          configuration = optarg;
          break;

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

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

    argc -= optind;
    argv += optind;

    if (rld::verbose ())
      std::cout << "RTEMS Trace Linker " << rld::version () << std::endl;

    /*
     * Load the arch/bsp value if provided.
     */
    if (arch_bsp_load)
      rld::rtems::load_cc ();

    /*
     * Load the remaining command line arguments into the linker command line.
     */
    while (argc--)
    {
      if (ld_cmd.length () != 0)
        ld_cmd += " ";
      ld_cmd += *argv++;
    }

    /*
     * If there are no object files there is nothing to link.
     */
    if (ld_cmd.empty ())
      throw rld::error ("no trace linker options", "options");

    /*
     * Perform a trace link.
     */
    try
    {
      linker.load_config (configuration, trace);
      linker.generate_wrapper ();
      linker.compile_wrapper ();

      if (rld::verbose ())
        linker.dump (std::cout);
    }
    catch (...)
    {
      throw;
    }

  }
  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;
    ::free (realname);
    ec = 11;
  }
  catch (...)
  {
    /*
     * Helps to know if this happens.
     */
    std::cout << "error: unhandled exception" << std::endl;
    ec = 12;
  }

  return ec;
}