source: rtems-tools/linkers/rtems-ld.cpp @ 6fb1409

4.104.115
Last change on this file since 6fb1409 was 6506aa1, checked in by Chris Johns <chrisj@…>, on 09/08/14 at 06:06:48

RTEMS trace linker builds trace applications.

The trace linker builds the both_hello example in examples-v2.

Move the various string support functions into a C++ file and stop being
inlined. Make them return const std::string.

Add ld support to rld-cc.

Add search path support to rld-config so installed common files can be used.

Fix the path bugs.

Add an absolute path function to rld-path.

  • Property mode set to 100644
File size: 16.9 KB
Line 
1/*
2 * Copyright (c) 2011-2012, Chris Johns <chrisj@rtems.org>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16/**
17 * @file
18 *
19 * @ingroup rtems_rld
20 *
21 * @brief RTEMS Linker Main manages opions, sequence of operations and exceptions.
22 *
23 */
24
25#if HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <iostream>
30
31#include <cxxabi.h>
32#include <signal.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36
37#include <getopt.h>
38
39#include <rld.h>
40#include <rld-cc.h>
41#include <rld-rap.h>
42#include <rld-outputter.h>
43#include <rld-process.h>
44#include <rld-resolver.h>
45#include <rld-rtems.h>
46
47#ifndef HAVE_KILL
48#define kill(p,s) raise(s)
49#endif
50
51/**
52 * RTEMS Linker options. This needs to be rewritten to be like cc where only a
53 * single '-' and long options is present.
54 */
55static struct option rld_opts[] = {
56  { "help",        no_argument,            NULL,           'h' },
57  { "version",     no_argument,            NULL,           'V' },
58  { "verbose",     no_argument,            NULL,           'v' },
59  { "warn",        no_argument,            NULL,           'w' },
60  { "map",         no_argument,            NULL,           'M' },
61  { "output",      required_argument,      NULL,           'o' },
62  { "out-format",  required_argument,      NULL,           'O' },
63  { "lib-path",    required_argument,      NULL,           'L' },
64  { "lib",         required_argument,      NULL,           'l' },
65  { "no-stdlibs",  no_argument,            NULL,           'n' },
66  { "entry",       required_argument,      NULL,           'e' },
67  { "define",      required_argument,      NULL,           'd' },
68  { "undefined",   required_argument,      NULL,           'u' },
69  { "base",        required_argument,      NULL,           'b' },
70  { "cc",          required_argument,      NULL,           'C' },
71  { "exec-prefix", required_argument,      NULL,           'E' },
72  { "cflags",      required_argument,      NULL,           'c' },
73  { "rap-strip",   no_argument,            NULL,           'S' },
74  { "rpath",       required_argument,      NULL,           'R' },
75  { "runtime-lib", required_argument,      NULL,           'P' },
76  { "one-file",    no_argument,            NULL,           's' },
77  { "rtems",       required_argument,      NULL,           'r' },
78  { "rtems-bsp",   required_argument,      NULL,           'B' },
79  { NULL,          0,                      NULL,            0 }
80};
81
82#if TO_BE_USED_FOR_THE_UNDEFINES
83void
84split_on_equals (const std::string& opt, std::string& left, std::string& right)
85{
86  std::string::size_type eq = opt.find_first_of('=');
87}
88#endif
89
90void
91usage (int exit_code)
92{
93  std::cout << "rtems-ld [options] objects" << std::endl
94            << "Options and arguments:" << std::endl
95            << " -h        : help (also --help)" << std::endl
96            << " -V        : print linker version number and exit (also --version)" << std::endl
97            << " -v        : verbose (trace import parts), can supply multiple times" << std::endl
98            << "             to increase verbosity (also --verbose)" << std::endl
99            << " -w        : generate warnings (also --warn)" << std::endl
100            << " -M        : generate map output (also --map)" << std::endl
101            << " -o file   : linker output is written to file (also --output)" << std::endl
102            << " -O format : linker output format, default is 'rap' (also --out-format)" << std::endl
103            << " -L path   : path to a library, add multiple for more than" << std::endl
104            << "             one path (also --lib-path)" << std::endl
105            << " -l lib    : add lib to the libraries searched, add multiple" << std::endl
106            << "             for more than one library (also --lib)" << std::endl
107            << " -n        : do not search standard libraries (also --no-stdlibs)" << std::endl
108            << " -e entry  : entry point symbol (also --entry)" << std::endl
109            << " -d sym    : add the symbol definition, add multiple with" << std::endl
110            << "             more than one define (also --define)" << std::endl
111            << " -u sym    : add the undefined symbol definition, add multiple" << std::endl
112            << "             for more than one undefined symbol (also --undefined)" << std::endl
113            << " -b elf    : read the ELF file symbols as the base RTEMS kernel" << std::endl
114            << "             image (also --base)" << std::endl
115            << " -C file   : execute file as the target C compiler (also --cc)" << std::endl
116            << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl
117            << " -c cflags : C compiler flags (also --cflags)" << std::endl
118            << " -S        : do not include file details (also --rap-strip)" << std::endl
119            << " -R        : include file paths (also --rpath)" << std::endl
120            << " -P        : place objects from archives (also --runtime-lib)" << std::endl
121            << " -s        : Include archive elf object files (also --one-file)" << std::endl
122            << " -Wl,opts  : link compatible flags, ignored" << std::endl
123            << " -r path   : RTEMS path (also --rtems)" << std::endl
124            << " -B bsp    : RTEMS arch/bsp (also --rtems-bsp)" << std::endl
125            << "Output Formats:" << std::endl
126            << " rap     - RTEMS application (LZ77, single image)" << std::endl
127            << " elf     - ELF application (script, ELF files)" << std::endl
128            << " script  - Script format (list of object files)" << std::endl
129            << " archive - Archive format (collection of ELF files)" << std::endl;
130  ::exit (exit_code);
131}
132
133static void
134fatal_signal (int signum)
135{
136  signal (signum, SIG_DFL);
137
138  rld::process::temporaries_clean_up ();
139
140  /*
141   * Get the same signal again, this time not handled, so its normal effect
142   * occurs.
143   */
144  kill (getpid (), signum);
145}
146
147static void
148setup_signals (void)
149{
150  if (signal (SIGINT, SIG_IGN) != SIG_IGN)
151    signal (SIGINT, fatal_signal);
152#ifdef SIGHUP
153  if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
154    signal (SIGHUP, fatal_signal);
155#endif
156  if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
157    signal (SIGTERM, fatal_signal);
158#ifdef SIGPIPE
159  if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
160    signal (SIGPIPE, fatal_signal);
161#endif
162#ifdef SIGCHLD
163  signal (SIGCHLD, SIG_DFL);
164#endif
165}
166
167int
168main (int argc, char* argv[])
169{
170  int ec = 0;
171
172  setup_signals ();
173
174  try
175  {
176    rld::files::cache    cache;
177    rld::files::cache    base;
178    rld::files::cache    cachera;
179    rld::path::paths     libpaths;
180    rld::path::paths     libs;
181    rld::path::paths     objects;
182    rld::path::paths     libraries;
183    rld::symbols::bucket defines;
184    rld::symbols::bucket undefines;
185    rld::symbols::table  base_symbols;
186    rld::symbols::table  symbols;
187    rld::symbols::symtab undefined;
188    std::string          rtems_path;
189    std::string          rtems_arch_bsp;
190    std::string          entry = "rtems";
191    std::string          exit;
192    std::string          output = "a.out";
193    std::string          outra;
194    std::string          base_name;
195    std::string          output_type = "rap";
196    bool                 standard_libs = true;
197    bool                 map = false;
198    bool                 warnings = false;
199    bool                 one_file = false;
200
201    libpaths.push_back (".");
202
203    while (true)
204    {
205      int opt = ::getopt_long (argc, argv, "hvwVMnsSb:E:o:O:L:l:c:e:d:u:C:W:R:P:r:B:", rld_opts, NULL);
206      if (opt < 0)
207        break;
208
209      switch (opt)
210      {
211        case 'V':
212          std::cout << "rtems-ld (RTEMS Linker) " << rld::version ()
213                    << std::endl;
214          ::exit (0);
215          break;
216
217        case 'v':
218          rld::verbose_inc ();
219          break;
220
221        case 'M':
222          map = true;
223          break;
224
225        case 'w':
226          warnings = true;
227          break;
228
229        case 'o':
230          if (output != "a.out")
231            std::cerr << "warning: output already set" << std::endl;
232          output = optarg;
233          break;
234
235        case 'O':
236          output_type = optarg;
237          break;
238
239        case 'l':
240          /*
241           * The order is important. It is the search order.
242           */
243          libs.push_back (optarg);
244          break;
245
246        case 'P':
247          if (!outra.empty ())
248            std::cerr << "warning: output ra alreay set" << std::endl;
249          outra = "lib";
250          outra += optarg;
251          outra += ".ra";
252          break;
253
254        case 's':
255          one_file = true;
256          break;
257
258        case 'L':
259          if ((optarg[::strlen (optarg) - 1] == '/') ||
260              (optarg[::strlen (optarg) - 1] == '\\'))
261            optarg[::strlen (optarg) - 1] = '\0';
262          libpaths.push_back (optarg);
263          break;
264
265        case 'n':
266          standard_libs = false;
267          break;
268
269        case 'C':
270          if (rld::cc::is_exec_prefix_set ())
271            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
272          rld::cc::set_cc (optarg);
273          break;
274
275        case 'E':
276          if (rld::cc::is_cc_set ())
277            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
278          rld::cc::set_exec_prefix (optarg);
279          break;
280
281        case 'c':
282          rld::cc::set_flags (optarg, rld::cc::ft_cflags);
283          break;
284
285        case 'e':
286          entry = optarg;
287          break;
288
289        case 'd':
290          defines.push_back (rld::symbols::symbol (optarg));
291          break;
292
293        case 'u':
294          undefines.push_back (rld::symbols::symbol (optarg));
295          break;
296
297        case 'b':
298          base_name = optarg;
299          break;
300
301        case 'S':
302          rld::rap::add_obj_details = false;
303          break;
304
305        case 'R':
306          rld::rap::rpath += optarg;
307          rld::rap::rpath += '\0';
308          break;
309
310        case 'W':
311          /* ignore linker compatiable flags */
312          break;
313
314        case 'r':
315          rtems_path = optarg;
316          break;
317
318        case 'B':
319          rtems_arch_bsp = optarg;
320          break;
321
322        case '?':
323          usage (3);
324          break;
325
326        case 'h':
327          usage (0);
328          break;
329      }
330    }
331
332    argc -= optind;
333    argv += optind;
334
335    if (rld::verbose () || map)
336      std::cout << "RTEMS Linker " << rld::version () << std::endl;
337
338    /*
339     * If there are no object files there is nothing to link.
340     */
341    if ((argc == 0) && !map)
342      throw rld::error ("no object files", "options");
343
344    /*
345     * Check the output format is valid.
346     */
347    if ((output_type != "rap") &&
348        (output_type != "elf") &&
349        (output_type != "script") &&
350        (output_type != "archive"))
351      throw rld::error ("invalid output format", "options");
352
353    /*
354     * Load the arch/bsp value if provided.
355     */
356    if (!rtems_arch_bsp.empty ())
357    {
358      if (rtems_path.empty ())
359        throw rld::error ("No RTEMS path provide with arch/bsp", "options");
360      rld::rtems::set_path (rtems_path);
361      rld::rtems::set_arch_bsp (rtems_arch_bsp);
362    }
363
364    /*
365     * Load the remaining command line arguments into the cache as object
366     * files.
367     */
368    while (argc--)
369      objects.push_back (*argv++);
370
371    /*
372     * The 'entry' point symbol needs to be added to the undefines so it is
373     * resolved.
374     */
375    undefines.push_back (rld::symbols::symbol (entry));
376
377    /*
378     * Load the symbol table with the defined symbols from the defines bucket.
379     */
380    rld::symbols::load (defines, symbols);
381
382    /*
383     * Load the undefined table with the undefined symbols from the undefines
384     * bucket.
385     */
386    rld::symbols::load (undefines, undefined);
387
388    /*
389     * Add the object files to the cache.
390     */
391    cache.add (objects);
392
393    /*
394     * Open the cache.
395     */
396    cache.open ();
397
398    /*
399     * If the full path to CC is not provided and the exec-prefix is not set by
400     * the command line see if it can be detected from the object file
401     * types. This must be after we have added the object files because they
402     * are used when detecting.
403     */
404    if (!rld::cc::is_cc_set () && !rld::cc::is_exec_prefix_set ())
405      rld::cc::set_exec_prefix (rld::elf::machine_type ());
406
407    /*
408     * If we have a base image add it.
409     */
410    if (base_name.length ())
411    {
412      if (rld::verbose ())
413        std::cout << "base-image: " << base_name << std::endl;
414      base.open ();
415      base.add (base_name);
416      base.load_symbols (base_symbols, true);
417    }
418
419    /*
420     * Get the standard library paths
421     */
422    if (standard_libs)
423      rld::cc::get_standard_libpaths (libpaths);
424
425    /*
426     * Get the command line libraries.
427     */
428    rld::files::find_libraries (libraries, libpaths, libs);
429
430    /*
431     * Are we to load standard libraries ?
432     */
433    if (standard_libs)
434      rld::cc::get_standard_libs (libraries, libpaths);
435
436    /*
437     * Load the library to the cache.
438     */
439    cache.add_libraries (libraries);
440
441    /*
442     * Begin the archive session. This opens the archives and leaves them open
443     * while we the symbol table is being used. The symbols reference object
444     * files and the object files may reference archives and it is assumed they
445     * are open and available. It is also assumed the number of library
446     * archives being managed is less than the maximum file handles this
447     * process can have open at any one time. If this is not the case this
448     * approach would need to be reconsidered and the overhead of opening and
449     * closing archives added.
450     */
451    try
452    {
453      cache.archives_begin ();
454
455      /*
456       * Load the symbol table.
457       */
458      cache.load_symbols (symbols);
459
460      /*
461       * Map ?
462       */
463      if (map)
464      {
465        if (base_name.length ())
466          rld::map (base, base_symbols);
467        rld::map (cache, symbols);
468      }
469
470      if (cache.path_count ())
471      {
472        /*
473         * This structure allows us to add different operations with the same
474         * structure.
475         */
476        rld::files::object_list dependents;
477        rld::resolver::resolve (dependents, cache,
478                                base_symbols, symbols, undefined);
479
480        /**
481         * Output the file.
482         */
483        if (output_type == "script")
484          rld::outputter::script (output, entry, exit, dependents, cache);
485        else if (output_type == "archive")
486          rld::outputter::archive (output, entry, exit, dependents, cache);
487        else if (output_type == "elf")
488          rld::outputter::elf_application (output, entry, exit,
489                                           dependents, cache);
490        else if (output_type == "rap")
491        {
492          rld::outputter::application (output, entry, exit,
493                                       dependents, cache, symbols,
494                                       one_file);
495          if (!outra.empty ())
496          {
497            rld::path::paths ra_libs;
498            bool ra_exist = false;
499
500            /**
501             * If exist, search it, else create a new one.
502             */
503            if ((ra_exist = ::access (outra.c_str (), 0)) == 0)
504            {
505              ra_libs.push_back (outra);
506              cachera.open ();
507              cachera.add_libraries (ra_libs);
508              cachera.archives_begin ();
509            }
510
511            rld::outputter::archivera (outra, dependents, cachera,
512                                       !ra_exist, false);
513          }
514        }
515        else
516          throw rld::error ("invalid output type", "output");
517
518        /**
519         * Check for warnings.
520         */
521        if (warnings)
522        {
523          rld::warn_unused_externals (dependents);
524        }
525      }
526    }
527    catch (...)
528    {
529      cache.archives_end ();
530      throw;
531    }
532
533    cache.archives_end ();
534  }
535  catch (rld::error re)
536  {
537    std::cerr << "error: "
538              << re.where << ": " << re.what
539              << std::endl;
540    ec = 10;
541  }
542  catch (std::exception e)
543  {
544    int   status;
545    char* realname;
546    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
547    std::cerr << "error: exception: " << realname << " [";
548    ::free (realname);
549    const std::type_info &ti = typeid (e);
550    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
551    std::cerr << realname << "] " << e.what () << std::endl << std::flush;
552    ::free (realname);
553    ec = 11;
554  }
555  catch (...)
556  {
557    /*
558     * Helps to know if this happens.
559     */
560    std::cerr << "error: unhandled exception" << std::endl;
561    ec = 12;
562  }
563
564  return ec;
565}
Note: See TracBrowser for help on using the repository browser.