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

4.104.115
Last change on this file since b938414 was 7338811, checked in by Chris Johns <chrisj@…>, on 01/20/15 at 23:52:54

Remove warning on Linux.

  • Property mode set to 100644
File size: 13.1 KB
Line 
1/*
2 * Copyright (c) 2011-2014, 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 Symbols 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 <fstream>
30#include <iomanip>
31#include <iostream>
32
33#include <cxxabi.h>
34#include <signal.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38
39#include <getopt.h>
40
41#include <rld.h>
42#include <rld-cc.h>
43#include <rld-outputter.h>
44#include <rld-process.h>
45#include <rld-symbols.h>
46
47#ifndef HAVE_KILL
48#define kill(p,s) raise(s)
49#endif
50
51/**
52 * Header text.
53 */
54static const char* c_header[] =
55{
56  "/*",
57  " * RTEMS Global Symbol Table",
58  " *  Automatically generated. Do not edit..",
59  " */",
60  "",
61  "extern const unsigned char rtems__rtl_base_globals[];",
62  "extern const unsigned int rtems__rtl_base_globals_size;",
63  "",
64  "void rtems_rtl_base_sym_global_add (const unsigned char* , unsigned int );",
65  "",
66  "asm(\".section \\\".rodata\\\"\");",
67  "",
68  "asm(\"  .align   4\");",
69  "asm(\"  .local   rtems__rtl_base_globals\");",
70  "asm(\"rtems__rtl_base_globals:\");",
71  "#if __mips__",
72  " asm(\"  .align 0\");",
73  "#else",
74  " asm(\"  .balign 1\");",
75  "#endif",
76  0
77};
78
79static const char* c_trailer[] =
80{
81  "asm(\"  .byte    0\");",
82  "asm(\"  .ascii   \\\"\\xde\\xad\\xbe\\xef\\\"\");",
83#if BROKEN_ON_SOME_ASSEMBLERS
84  "asm(\"  .type    rtems__rtl_base_globals, #object\");",
85  "asm(\"  .size    rtems__rtl_base_globals, . - rtems__rtl_base_globals\");",
86#endif
87  "",
88  "/*",
89  " * Symbol table size.",
90  " */",
91  "asm(\"  .align   4\");",
92  "asm(\"  .local   rtems__rtl_base_globals_size\");",
93#if BROKEN_ON_SOME_ASSEMBLERS
94  "asm(\"  .type    rtems__rtl_base_globals_size, #object\");",
95  "asm(\"  .size    rtems__rtl_base_globals_size, 4\");",
96#endif
97  "asm(\"rtems__rtl_base_globals_size:\");",
98  "asm(\"  .long rtems__rtl_base_globals_size - rtems__rtl_base_globals\");",
99  "",
100  0
101};
102
103static const char* c_rtl_call_body[] =
104{
105  "{",
106  "  rtems_rtl_base_sym_global_add ((const unsigned char*) &rtems__rtl_base_globals,",
107  "                                 rtems__rtl_base_globals_size);",
108  "}",
109  0
110};
111
112/**
113 * Paint the data to the temporary file.
114 */
115static void
116temporary_file_paint (rld::process::tempfile& t, const char* lines[])
117{
118  for (int l = 0; lines[l]; ++l)
119    t.write_line (lines[l]);
120}
121
122/**
123 * The constructor trailer.
124 */
125static void
126c_constructor_trailer (rld::process::tempfile& c)
127{
128  c.write_line ("static void init(void) __attribute__ ((constructor));");
129  c.write_line ("static void init(void)");
130  temporary_file_paint (c, c_rtl_call_body);
131}
132
133/**
134 * The embedded trailer.
135 */
136static void
137c_embedded_trailer (rld::process::tempfile& c)
138{
139  c.write_line ("void rtems_rtl_base_global_syms_init(void);");
140  c.write_line ("void rtems_rtl_base_global_syms_init(void)");
141  temporary_file_paint (c, c_rtl_call_body);
142}
143
144/**
145 * Generate the symbol map object file for loading or linking into
146 * a running RTEMS machine.
147 */
148static void
149generate_c (rld::process::tempfile& c,
150              rld::symbols::table&  symbols,
151              bool                  embed)
152{
153  temporary_file_paint (c, c_header);
154
155  for (rld::symbols::symtab::const_iterator si = symbols.globals ().begin ();
156       si != symbols.globals ().end ();
157       ++si)
158  {
159    const rld::symbols::symbol& sym = *((*si).second);
160
161    c.write_line ("asm(\"  .asciz \\\"" + sym.name () + "\\\"\");");
162
163    if (embed)
164    {
165      c.write_line ("asm(\"  .long " + sym.name () + "\");");
166    }
167    else
168    {
169      std::stringstream oss;
170      oss << std::hex << std::setfill ('0') << std::setw (8) << sym.value ();
171      c.write_line ("asm(\"  .long 0x" + oss.str () + "\");");
172    }
173  }
174
175  temporary_file_paint (c, c_trailer);
176
177  if (embed)
178    c_embedded_trailer (c);
179  else
180    c_constructor_trailer (c);
181}
182
183static void
184generate_symmap (rld::process::tempfile& c,
185                 const std::string&      output,
186                 rld::symbols::table&    symbols,
187                 bool                    embed)
188{
189  c.open (true);
190
191  if (rld::verbose ())
192    std::cout << "symbol C file: " << c.name () << std::endl;
193
194  generate_c (c, symbols, embed);
195
196  if (rld::verbose ())
197    std::cout << "symbol O file: " << output << std::endl;
198
199  rld::process::arg_container args;
200
201  rld::cc::make_cc_command (args);
202  rld::cc::append_flags (rld::cc::ft_cflags, args);
203
204  args.push_back ("-O2");
205  args.push_back ("-c");
206  args.push_back ("-o");
207  args.push_back (output);
208  args.push_back (c.name ());
209
210  rld::process::tempfile out;
211  rld::process::tempfile err;
212  rld::process::status   status;
213
214  status = rld::process::execute (rld::cc::get_cc (),
215                                  args,
216                                  out.name (),
217                                  err.name ());
218
219
220  if ((status.type != rld::process::status::normal) ||
221      (status.code != 0))
222  {
223    err.output (rld::cc::get_cc (), std::cout);
224    throw rld::error ("Compiler error", "compiling wrapper");
225  }
226}
227
228/**
229 * RTEMS Symbols options.
230 */
231static struct option rld_opts[] = {
232  { "help",        no_argument,            NULL,           'h' },
233  { "version",     no_argument,            NULL,           'V' },
234  { "verbose",     no_argument,            NULL,           'v' },
235  { "warn",        no_argument,            NULL,           'w' },
236  { "keep",        no_argument,            NULL,           'k' },
237  { "embed",       no_argument,            NULL,           'e' },
238  { "symc",        required_argument,      NULL,           'S' },
239  { "output",      required_argument,      NULL,           'o' },
240  { "map",         required_argument,      NULL,           'm' },
241  { "cc",          required_argument,      NULL,           'C' },
242  { "exec-prefix", required_argument,      NULL,           'E' },
243  { "cflags",      required_argument,      NULL,           'c' },
244  { NULL,          0,                      NULL,            0 }
245};
246
247void
248usage (int exit_code)
249{
250  std::cout << "rtems-syms [options] kernel" << std::endl
251            << "Options and arguments:" << std::endl
252            << " -h        : help (also --help)" << std::endl
253            << " -V        : print linker version number and exit (also --version)" << std::endl
254            << " -v        : verbose (trace import parts), can supply multiple times" << std::endl
255            << "             to increase verbosity (also --verbose)" << std::endl
256            << " -w        : generate warnings (also --warn)" << std::endl
257            << " -k        : keep temporary files (also --keep)" << std::endl
258            << " -e        : embedded symbol table (also --embed)" << std::endl
259            << " -S        : symbol's C file (also --symc)" << std::endl
260            << " -o file   : output object file (also --output)" << std::endl
261            << " -m file   : output a map file (also --map)" << std::endl
262            << " -C file   : execute file as the target C compiler (also --cc)" << std::endl
263            << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl
264            << " -c cflags : C compiler flags (also --cflags)" << std::endl;
265  ::exit (exit_code);
266}
267
268static void
269fatal_signal (int signum)
270{
271  signal (signum, SIG_DFL);
272
273  rld::process::temporaries_clean_up ();
274
275  /*
276   * Get the same signal again, this time not handled, so its normal effect
277   * occurs.
278   */
279  kill (getpid (), signum);
280}
281
282static void
283setup_signals (void)
284{
285  if (signal (SIGINT, SIG_IGN) != SIG_IGN)
286    signal (SIGINT, fatal_signal);
287#ifdef SIGHUP
288  if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
289    signal (SIGHUP, fatal_signal);
290#endif
291  if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
292    signal (SIGTERM, fatal_signal);
293#ifdef SIGPIPE
294  if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
295    signal (SIGPIPE, fatal_signal);
296#endif
297#ifdef SIGCHLD
298  signal (SIGCHLD, SIG_DFL);
299#endif
300}
301
302int
303main (int argc, char* argv[])
304{
305  int ec = 0;
306
307  setup_signals ();
308
309  try
310  {
311    rld::files::cache   kernel;
312    rld::symbols::table symbols;
313    std::string         kernel_name;
314    std::string         output;
315    std::string         map;
316    std::string         cc;
317    std::string         symc;
318    bool                embed = false;
319
320    rld::set_cmdline (argc, argv);
321
322    while (true)
323    {
324      int opt = ::getopt_long (argc, argv, "hvVwkef:S:o:m:E:c:C:", rld_opts, NULL);
325      if (opt < 0)
326        break;
327
328      switch (opt)
329      {
330        case 'V':
331          std::cout << "rtems-syms (RTEMS Symbols) " << rld::version ()
332                    << std::endl;
333          ::exit (0);
334          break;
335
336        case 'v':
337          rld::verbose_inc ();
338          break;
339
340        case 'w':
341          break;
342
343        case 'k':
344          rld::process::set_keep_temporary_files ();
345          break;
346
347        case 'e':
348          embed = true;
349          break;
350
351        case 'o':
352          output = optarg;
353          break;
354
355        case 'm':
356          map = optarg;
357          break;
358
359        case 'C':
360          if (rld::cc::is_exec_prefix_set ())
361            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
362          rld::cc::set_cc (optarg);
363          break;
364
365        case 'E':
366          if (rld::cc::is_cc_set ())
367            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
368          rld::cc::set_exec_prefix (optarg);
369          break;
370
371        case 'c':
372          rld::cc::set_flags (optarg, rld::cc::ft_cflags);
373          break;
374
375        case 'S':
376          symc = optarg;
377          break;
378
379        case '?':
380          usage (3);
381          break;
382
383        case 'h':
384          usage (0);
385          break;
386      }
387    }
388
389    /*
390     * Set the program name.
391     */
392    rld::set_progname (argv[0]);
393
394    argc -= optind;
395    argv += optind;
396
397    if (rld::verbose ())
398      std::cout << "RTEMS Kernel Symbols " << rld::version () << std::endl;
399
400    /*
401     * If there are no object files there is nothing to link.
402     */
403    if (argc == 0)
404      throw rld::error ("no kernel file", "options");
405    if (argc != 1)
406      throw rld::error ("only one kernel file", "options");
407    if (output.empty () && map.empty ())
408      throw rld::error ("no output or map", "options");
409
410    kernel_name = *argv;
411
412    if (rld::verbose ())
413      std::cout << "kernel: " << kernel_name << std::endl;
414
415    /*
416     * Load the symbols from the kernel.
417     */
418    try
419    {
420      /*
421       * Load the kernel ELF file symbol table.
422       */
423      kernel.open ();
424      kernel.add (kernel_name);
425      kernel.load_symbols (symbols, true);
426
427      /*
428       * If the full path to CC is not provided and the exec-prefix is not set
429       * by the command line see if it can be detected from the object file
430       * types. This must be after we have added the object files because they
431       * are used when detecting.
432       */
433      if (!cc.empty ())
434        rld::cc::set_cc (cc);
435      if (!rld::cc::is_cc_set () && !rld::cc::is_exec_prefix_set ())
436        rld::cc::set_exec_prefix (rld::elf::machine_type ());
437
438      /*
439       * Create a map file if asked too.
440       */
441      if (!map.empty ())
442      {
443        std::ofstream mout;
444        mout.open (map.c_str());
445        if (!mout.is_open ())
446          throw rld::error ("map file open failed", "map");
447        mout << "RTEMS Kernel Symbols Map" << std::endl
448             << " kernel: " << kernel_name << std::endl
449             << std::endl;
450        rld::symbols::output (mout, symbols);
451        mout.close ();
452      }
453
454      /*
455       * Create an output file if asked too.
456       */
457      if (!output.empty ())
458      {
459        rld::process::tempfile c (".c");
460
461        if (!symc.empty ())
462        {
463          c.override (symc);
464          c.keep ();
465        }
466
467        /*
468         * Generate and compile the symbol map.
469         */
470        generate_symmap (c, output, symbols, embed);
471      }
472
473      kernel.close ();
474    }
475    catch (...)
476    {
477      kernel.close ();
478      throw;
479    }
480
481    kernel.close ();
482  }
483  catch (rld::error re)
484  {
485    std::cerr << "error: "
486              << re.where << ": " << re.what
487              << std::endl;
488    ec = 10;
489  }
490  catch (std::exception e)
491  {
492    int   status;
493    char* realname;
494    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
495    std::cerr << "error: exception: " << realname << " [";
496    ::free (realname);
497    const std::type_info &ti = typeid (e);
498    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
499    std::cerr << realname << "] " << e.what () << std::endl;
500    ::free (realname);
501    ec = 11;
502  }
503  catch (...)
504  {
505    /*
506     * Helps to know if this happens.
507     */
508    std::cout << "error: unhandled exception" << std::endl;
509    ec = 12;
510  }
511
512  return ec;
513}
Note: See TracBrowser for help on using the repository browser.