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

4.104.115
Last change on this file since a916fa4 was 31bf375, checked in by Chris Johns <chrisj@…>, on 09/05/14 at 08:18:11

Remove march/mcpu and add RTEMS BSP and cflags support.

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