source: rtems-tools/linkers/rtems-ld.cpp @ 32cd4fc

4.104.115
Last change on this file since 32cd4fc was 8807135, checked in by Chris Johns <chrisj@…>, on 09/06/14 at 10:19:45

Refactor the CC flags. Fix the various linkers. The trace linker is compiling.

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