source: rtems-tools/linkers/rtems-ld.cpp @ b938414

4.104.115
Last change on this file since b938414 was 2d5de2e, checked in by Chris Johns <chrisj@…>, on 09/10/14 at 01:16:26

Fix the error message.

  • Property mode set to 100644
File size: 17.2 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    rld::set_cmdline (argc, argv);
202
203    libpaths.push_back (".");
204
205    while (true)
206    {
207      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);
208      if (opt < 0)
209        break;
210
211      switch (opt)
212      {
213        case 'V':
214          std::cout << "rtems-ld (RTEMS Linker) " << rld::version ()
215                    << std::endl;
216          ::exit (0);
217          break;
218
219        case 'v':
220          rld::verbose_inc ();
221          break;
222
223        case 'M':
224          map = true;
225          break;
226
227        case 'w':
228          warnings = true;
229          break;
230
231        case 'o':
232          if (output != "a.out")
233            std::cerr << "warning: output already set" << std::endl;
234          output = optarg;
235          break;
236
237        case 'O':
238          output_type = optarg;
239          break;
240
241        case 'l':
242          /*
243           * The order is important. It is the search order.
244           */
245          libs.push_back (optarg);
246          break;
247
248        case 'P':
249          if (!outra.empty ())
250            std::cerr << "warning: output ra alreay set" << std::endl;
251          outra = "lib";
252          outra += optarg;
253          outra += ".ra";
254          break;
255
256        case 's':
257          one_file = true;
258          break;
259
260        case 'L':
261          if ((optarg[::strlen (optarg) - 1] == '/') ||
262              (optarg[::strlen (optarg) - 1] == '\\'))
263            optarg[::strlen (optarg) - 1] = '\0';
264          libpaths.push_back (optarg);
265          break;
266
267        case 'n':
268          standard_libs = false;
269          break;
270
271        case 'C':
272          if (rld::cc::is_exec_prefix_set ())
273            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
274          rld::cc::set_cc (optarg);
275          break;
276
277        case 'E':
278          if (rld::cc::is_cc_set ())
279            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
280          rld::cc::set_exec_prefix (optarg);
281          break;
282
283        case 'c':
284          rld::cc::set_flags (optarg, rld::cc::ft_cflags);
285          break;
286
287        case 'e':
288          entry = optarg;
289          break;
290
291        case 'd':
292          defines.push_back (rld::symbols::symbol (optarg));
293          break;
294
295        case 'u':
296          undefines.push_back (rld::symbols::symbol (optarg));
297          break;
298
299        case 'b':
300          base_name = optarg;
301          break;
302
303        case 'S':
304          rld::rap::add_obj_details = false;
305          break;
306
307        case 'R':
308          rld::rap::rpath += optarg;
309          rld::rap::rpath += '\0';
310          break;
311
312        case 'W':
313          /* ignore linker compatiable flags */
314          break;
315
316        case 'r':
317          rtems_path = optarg;
318          break;
319
320        case 'B':
321          rtems_arch_bsp = optarg;
322          break;
323
324        case '?':
325          usage (3);
326          break;
327
328        case 'h':
329          usage (0);
330          break;
331      }
332    }
333
334    /*
335     * Set the program name.
336     */
337    rld::set_progname (argv[0]);
338
339    argc -= optind;
340    argv += optind;
341
342    if (rld::verbose () || map)
343    {
344      std::cout << "RTEMS Linker " << rld::version () << std::endl;
345      std::cout << " " << rld::get_cmdline () << std::endl;
346    }
347
348    /*
349     * If there are no object files there is nothing to link.
350     */
351    if ((argc == 0) && !map)
352      throw rld::error ("no object files", "options");
353
354    /*
355     * Check the output format is valid.
356     */
357    if ((output_type != "rap") &&
358        (output_type != "elf") &&
359        (output_type != "script") &&
360        (output_type != "archive"))
361      throw rld::error ("invalid output format", "options");
362
363    /*
364     * Load the arch/bsp value if provided.
365     */
366    if (!rtems_arch_bsp.empty ())
367    {
368      const std::string& prefix = rld::get_prefix ();
369      if (rtems_path.empty () && prefix.empty ())
370        throw rld::error ("No RTEMS path provided with arch/bsp", "options");
371      if (!rtems_path.empty ())
372        rld::rtems::set_path (rtems_path);
373      else
374        rld::rtems::set_path (prefix);
375      rld::rtems::set_arch_bsp (rtems_arch_bsp);
376    }
377
378    /*
379     * Load the remaining command line arguments into the cache as object
380     * files.
381     */
382    while (argc--)
383      objects.push_back (*argv++);
384
385    /*
386     * The 'entry' point symbol needs to be added to the undefines so it is
387     * resolved.
388     */
389    undefines.push_back (rld::symbols::symbol (entry));
390
391    /*
392     * Load the symbol table with the defined symbols from the defines bucket.
393     */
394    rld::symbols::load (defines, symbols);
395
396    /*
397     * Load the undefined table with the undefined symbols from the undefines
398     * bucket.
399     */
400    rld::symbols::load (undefines, undefined);
401
402    /*
403     * Add the object files to the cache.
404     */
405    cache.add (objects);
406
407    /*
408     * Open the cache.
409     */
410    cache.open ();
411
412    /*
413     * If the full path to CC is not provided and the exec-prefix is not set by
414     * the command line see if it can be detected from the object file
415     * types. This must be after we have added the object files because they
416     * are used when detecting.
417     */
418    if (!rld::cc::is_cc_set () && !rld::cc::is_exec_prefix_set ())
419      rld::cc::set_exec_prefix (rld::elf::machine_type ());
420
421    /*
422     * If we have a base image add it.
423     */
424    if (base_name.length ())
425    {
426      if (rld::verbose ())
427        std::cout << "base-image: " << base_name << std::endl;
428      base.open ();
429      base.add (base_name);
430      base.load_symbols (base_symbols, true);
431    }
432
433    /*
434     * Get the standard library paths
435     */
436    if (standard_libs)
437      rld::cc::get_standard_libpaths (libpaths);
438
439    /*
440     * Get the command line libraries.
441     */
442    rld::files::find_libraries (libraries, libpaths, libs);
443
444    /*
445     * Are we to load standard libraries ?
446     */
447    if (standard_libs)
448      rld::cc::get_standard_libs (libraries, libpaths);
449
450    /*
451     * Load the library to the cache.
452     */
453    cache.add_libraries (libraries);
454
455    /*
456     * Begin the archive session. This opens the archives and leaves them open
457     * while we the symbol table is being used. The symbols reference object
458     * files and the object files may reference archives and it is assumed they
459     * are open and available. It is also assumed the number of library
460     * archives being managed is less than the maximum file handles this
461     * process can have open at any one time. If this is not the case this
462     * approach would need to be reconsidered and the overhead of opening and
463     * closing archives added.
464     */
465    try
466    {
467      cache.archives_begin ();
468
469      /*
470       * Load the symbol table.
471       */
472      cache.load_symbols (symbols);
473
474      /*
475       * Map ?
476       */
477      if (map)
478      {
479        if (base_name.length ())
480          rld::map (base, base_symbols);
481        rld::map (cache, symbols);
482      }
483
484      if (cache.path_count ())
485      {
486        /*
487         * This structure allows us to add different operations with the same
488         * structure.
489         */
490        rld::files::object_list dependents;
491        rld::resolver::resolve (dependents, cache,
492                                base_symbols, symbols, undefined);
493
494        /**
495         * Output the file.
496         */
497        if (output_type == "script")
498          rld::outputter::script (output, entry, exit, dependents, cache);
499        else if (output_type == "archive")
500          rld::outputter::archive (output, entry, exit, dependents, cache);
501        else if (output_type == "elf")
502          rld::outputter::elf_application (output, entry, exit,
503                                           dependents, cache);
504        else if (output_type == "rap")
505        {
506          rld::outputter::application (output, entry, exit,
507                                       dependents, cache, symbols,
508                                       one_file);
509          if (!outra.empty ())
510          {
511            rld::path::paths ra_libs;
512            bool ra_exist = false;
513
514            /**
515             * If exist, search it, else create a new one.
516             */
517            if ((ra_exist = ::access (outra.c_str (), 0)) == 0)
518            {
519              ra_libs.push_back (outra);
520              cachera.open ();
521              cachera.add_libraries (ra_libs);
522              cachera.archives_begin ();
523            }
524
525            rld::outputter::archivera (outra, dependents, cachera,
526                                       !ra_exist, false);
527          }
528        }
529        else
530          throw rld::error ("invalid output type", "output");
531
532        /**
533         * Check for warnings.
534         */
535        if (warnings)
536        {
537          rld::warn_unused_externals (dependents);
538        }
539      }
540    }
541    catch (...)
542    {
543      cache.archives_end ();
544      throw;
545    }
546
547    cache.archives_end ();
548  }
549  catch (rld::error re)
550  {
551    std::cerr << "error: "
552              << re.where << ": " << re.what
553              << std::endl;
554    ec = 10;
555  }
556  catch (std::exception e)
557  {
558    int   status;
559    char* realname;
560    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
561    std::cerr << "error: exception: " << realname << " [";
562    ::free (realname);
563    const std::type_info &ti = typeid (e);
564    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
565    std::cerr << realname << "] " << e.what () << std::endl << std::flush;
566    ::free (realname);
567    ec = 11;
568  }
569  catch (...)
570  {
571    /*
572     * Helps to know if this happens.
573     */
574    std::cerr << "error: unhandled exception" << std::endl;
575    ec = 12;
576  }
577
578  return ec;
579}
Note: See TracBrowser for help on using the repository browser.