summaryrefslogtreecommitdiff
path: root/rtems-ra.cpp
blob: 7b7a98bef8b0d05cc0c231854d24fc1cc60b49a2 (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
/*
 * Copyright (c) 2011-2012, 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 RA Linker Main manages opions, sequence of operations and exceptions.
 *
 */

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

#include <iostream>

#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-rap.h>
#include <rld-outputter.h>
#include <rld-process.h>
#include <rld-resolver.h>

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

/**
 * RTEMS Linker 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' },
  { "output-path", required_argument,      NULL,           'p' },
  { "output",      required_argument,      NULL,           'o' },
  { "lib-path",    required_argument,      NULL,           'L' },
  { "lib",         required_argument,      NULL,           'l' },
  { "no-stdlibs",  no_argument,            NULL,           'n' },
  { "cc",          required_argument,      NULL,           'C' },
  { "exec-prefix", required_argument,      NULL,           'E' },
  { "march",       required_argument,      NULL,           'a' },
  { "mcpu",        required_argument,      NULL,           'c' },
  { "rap-strip",   no_argument,            NULL,           'S' },
  { "rpath",       required_argument,      NULL,           'R' },
  { "add-rap",     required_argument,      NULL,           'A' },
  { "replace-rap", required_argument,      NULL,           'r' },
  { "delete-rap",  required_argument,      NULL,           'd' },
  { NULL,          0,                      NULL,            0 }
};

#if TO_BE_USED_FOR_THE_UNDEFINES
void
split_on_equals (const std::string& opt, std::string& left, std::string& right)
{
  std::string::size_type eq = opt.find_first_of('=');
}
#endif

void
usage (int exit_code)
{
  std::cout << "rtems-ra [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
            << " -o name   : linker output, this options is just" << std::endl
            << "             for waf, it will not output to file (also --output)" << std::endl
            << " -p path   : output path (also --output-path)" << std::endl
            << " -L path   : path to a library, add multiple for more than" << std::endl
            << "             one path (also --lib-path)" << std::endl
            << " -l lib    : add lib to the libraries searched, add multiple" << std::endl
            << "             for more than one library (also --lib)" << std::endl
            << " -n        : do not search standard libraries (also --no-stdlibs)" << std::endl
            << " -C file   : execute file as the target C compiler (also --cc)" << std::endl
            << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl
            << " -c cflags : C compiler flags (also --cflags)" << std::endl
            << " -S        : do not include file details (also --rap-strip)" << std::endl
            << " -R        : include file paths (also --rpath)" << std::endl
            << " -A        : Add rap files (also --Add-rap)" << std::endl
            << " -r        : replace rap files (also --replace-rap)" << std::endl
            << " -d        : delete rap files (also --delete-rap)" << std::endl
            << " -Wl,opts  : link compatible flags, ignored" << std::endl
            << "Output Formats:" << std::endl
            << " ra      - RTEMS archive container of rap files" << 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::path::paths        libpaths;
    rld::path::paths        libs;
    rld::path::paths        libraries;
    rld::path::paths        outra;
    rld::path::paths        raps_add;
    rld::path::paths        raps_replace;
    rld::path::paths        raps_delete;
    std::string             cc_name;
    std::string             entry;
    std::string             exit;
    std::string             output_path = "./";
    std::string             output = "a.ra";
    bool                    standard_libs = true;
    bool                    convert = true;
    rld::files::object_list dependents;

    libpaths.push_back (".");
    dependents.clear ();

    while (true)
    {
      int opt = ::getopt_long (argc, argv, "hVvnSa:p:L:l:o:C:E:c:R:W:A:r:d:", rld_opts, NULL);
      if (opt < 0)
        break;

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

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

        case 'l':
          libs.push_back (optarg);
          break;

        case 'A':
          /*
           * Add rap files to ra file.
           */
          raps_add.push_back (optarg);
          convert = false;
          break;

        case 'r':
          /*
           * replace rap files to ra file.
           */
          raps_replace.push_back (optarg);
          convert = false;
          break;

        case 'd':
          /*
           * delete rap files to ra file.
           */
          raps_delete.push_back (optarg);
          convert = false;
          break;

        case 'L':
          if ((optarg[::strlen (optarg) - 1] == '/') ||
              (optarg[::strlen (optarg) - 1] == '\\'))
            optarg[::strlen (optarg) - 1] = '\0';
          libpaths.push_back (optarg);
          break;

        case 'n':
          standard_libs = false;
          break;

        case 'p':
          std::cout << "Output path: " << optarg << std::endl;
          output_path = optarg;
          break;

        case 'o':
          output = optarg;
          break;

        case 'C':
          if (rld::cc::is_exec_prefix_set ())
            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
          rld::cc::set_cc (optarg);
          break;

        case 'E':
          if (rld::cc::is_cc_set ())
            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
          rld::cc::set_exec_prefix (optarg);
          break;

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

        case 'S':
          rld::rap::add_obj_details = false;
          break;

        case 'R':
          rld::rap::rpath += optarg;
          rld::rap::rpath += '\0';
          break;

        case 'W':
          /* ignore linker compatiable flags */
          break;

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

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

    argc -= optind;
    argv += optind;

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

    /*
     * libs can be elf archive and rap archive
     */
    while (argc--)
      libs.push_back (*argv++);

    /*
     * If the full path to CC is not provided and the exec-prefix is not set by
     * the command line see if it can be detected from the object file
     * types. This must be after we have added the object files because they
     * are used when detecting.
     */
    if (!rld::cc::is_cc_set () && !rld::cc::is_exec_prefix_set ())
      rld::cc::set_exec_prefix (rld::elf::machine_type ());

    if (convert)
    {
      /*
       * Get the standard library paths
       */
      if (standard_libs)
        rld::cc::get_standard_libpaths (libpaths);
      /*
       * Get the command line libraries.
       */
      rld::files::find_libraries (libraries, libpaths, libs);

      /*
       * Are we to load standard libraries ?
       */
      if (standard_libs)
        rld::cc::get_standard_libs (libraries, libpaths);

     /*
      * Convert ar file to ra file
      */
      for (rld::path::paths::iterator p = libraries.begin (); p != libraries.end (); ++p)
      {
        rld::path::paths    library;
        rld::symbols::table symbols;
        rld::files::cache*  cache = new rld::files::cache ();

        library.clear ();
        library.push_back (*p);

        /*
        * Open the cache.
        */
        cache->open ();

        /*
         * Load the library to the cache.
         */
        cache->add_libraries (library);

        cache->load_symbols (symbols);

        try
        {

          rld::files::objects& objs = cache->get_objects ();
          rld::path::paths     raobjects;

          int pos = -1;
          std::string rap_name;
          for (rld::files::objects::iterator obi = objs.begin ();
              obi != objs.end ();
              ++obi)
          {
            rld::files::object* obj = (*obi).second;

            dependents.clear ();

            rap_name = obj->name ().oname ();

            pos = obj->name ().oname ().rfind ('.', rap_name.length ());
            if (pos != -1)
            {
              rap_name.erase (pos, rap_name.length ());
            }

            rap_name += ".rap";

            dependents.push_back (obj);

            raobjects.push_back (rap_name);

            /* Todo: include absolute name for rap_name */

            rld::outputter::application (rap_name, entry, exit,
                                         dependents, *cache, symbols,
                                         true);
          }

          dependents.clear ();
          for (rld::path::paths::iterator ni = raobjects.begin (); ni != raobjects.end (); ++ni)
          {
            rld::files::object* obj = new rld::files::object (*ni);
            dependents.push_back (obj);
          }

          bool              ra_rap = true;
          bool              ra_exist = false;
          rld::files::cache cachera;
          std::string       raname = *p;

          pos = -1;
          pos = raname.rfind ('/', raname.length ());
          if (pos != -1)
          {
            raname.erase (0, pos);
          }

          pos = -1;
          pos = raname.rfind ('.', raname.length ());
          if (pos != -1)
          {
            raname.erase (pos, raname.length ());
          }
          raname += ".ra";

          raname = output_path + raname;

          rld::outputter::archivera (raname, dependents, cachera,
                                     ra_exist, ra_rap);
          std::cout << "Generated: " << raname << std::endl;


          for (rld::files::object_list::iterator oi = dependents.begin ();
               oi != dependents.end ();
               ++oi)
          {
            rld::files::object& obj = *(*oi);
            ::unlink (obj.name ().oname ().c_str ());
          }
        }
        catch (...)
        {
          cache->archives_end ();
          throw;
        }

        cache->archives_end ();
        delete cache;
      }
    }
    else
    {
     /*
      * Add, replace, delete files from the ra file.
      */
      for (rld::path::paths::iterator pl = libs.begin (); pl != libs.end (); ++pl)
      {
        rld::path::paths   library;
        rld::files::cache* cache = new rld::files::cache ();

        library.clear ();
        library.push_back (*pl);

        /*
        * Open the cache.
        */
        cache->open ();

        /*
         * Load the library to the cache.
         */
        cache->add_libraries (library);

        rld::files::objects& objs = cache->get_objects ();
        rld::path::paths     raobjects;

        std::string rap_name;
        bool        rap_delete = false;

        dependents.clear ();
        /*
         * Delete rap files in ra file.
         */
        for (rld::files::objects::iterator obi = objs.begin ();
            obi != objs.end ();
            ++obi)
        {
          rld::files::object* obj = (*obi).second;

          rap_name = obj->name ().oname ();
          rap_delete = false;

          for (rld::path::paths::iterator pa = raps_delete.begin ();
               pa != raps_delete.end ();
               ++pa)
          {
            if (*pa == rap_name)
            {
              rap_delete = true;
              break;
            }
          }

          if (!rap_delete)
            dependents.push_back (obj);
        }

        /*
         * Add rap files into ra file, add supports replace.
         */
        rld::path::paths rap_objects;
        bool             rap_exist = false;

        for (rld::path::paths::iterator pa = raps_add.begin ();
             pa != raps_add.end ();
             ++pa)
        {
          rap_exist = false;

          for (rld::files::object_list::iterator oi = dependents.begin ();
               oi != dependents.end ();
               ++oi)
          {
            rld::files::object& obj = *(*oi);
            if (*pa == obj.name ().oname ())
            {
              rap_exist = true;
              raps_replace.push_back (*pa);
              break;
            }
          }

          if (!rap_exist)
            rap_objects.push_back (*pa);
        }

        for (rld::path::paths::iterator pa = rap_objects.begin ();
             pa != rap_objects.end ();
             ++pa)
        {
          rld::files::object* obj = new rld::files::object (*pa);
          if (!obj->name ().exists ())
          {
            delete obj;
            throw rld::error ("file not exist", "rap-add");
          }
          else dependents.push_back (obj);
        }

        /*
         * Replace rap files in ra file
         */
        rld::files::cache cachera;
        bool              rap_replace = false;

        rap_objects.clear ();
        cachera.open ();

        for (rld::path::paths::iterator pa = raps_replace.begin ();
             pa != raps_replace.end ();
             ++pa)
        {
          rap_replace = false;

          for (rld::files::object_list::iterator oi = dependents.begin ();
               oi != dependents.end ();
               )
          {
            rld::files::object& obj = *(*oi);
            if (*pa == obj.name ().oname ())
            {
              rap_replace = true;
              dependents.erase (oi++);
              break;
            }
            ++oi;
          }

          if (rap_replace)
            rap_objects.push_back (*pa);
        }

        for (rld::path::paths::iterator pa = rap_objects.begin ();
             pa != rap_objects.end ();
             ++pa)
        {
          rld::files::object* obj = new rld::files::object (*pa);
          if (!obj->name ().exists ())
          {
            delete obj;
            throw rld::error ("file not exist", "rap-add");
          }
          else dependents.push_back (obj);
        }

        rld::outputter::archivera (*pl, dependents, cachera,
                                   true, true);
        std::cout << "End" << std::endl;

        cache->archives_end ();
        delete cache;
      }
    }
  }
  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;
}