source: rtems-tools/linkers/rtems-syms.cpp @ 69c955d

4.104.115
Last change on this file since 69c955d was b9c0a04, checked in by Chris Johns <chrisj@…>, on 11/06/14 at 01:05:38

linkers: Disable .type statements in symbol code.

Some of the assemblers do not support this statement.

  • Property mode set to 100644
File size: 13.2 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    bool                warnings = false;
320
321    rld::set_cmdline (argc, argv);
322
323    while (true)
324    {
325      int opt = ::getopt_long (argc, argv, "hvVwkef:S:o:m:E:c:C:", rld_opts, NULL);
326      if (opt < 0)
327        break;
328
329      switch (opt)
330      {
331        case 'V':
332          std::cout << "rtems-syms (RTEMS Symbols) " << rld::version ()
333                    << std::endl;
334          ::exit (0);
335          break;
336
337        case 'v':
338          rld::verbose_inc ();
339          break;
340
341        case 'w':
342          warnings = true;
343          break;
344
345        case 'k':
346          rld::process::set_keep_temporary_files ();
347          break;
348
349        case 'e':
350          embed = true;
351          break;
352
353        case 'o':
354          output = optarg;
355          break;
356
357        case 'm':
358          map = optarg;
359          break;
360
361        case 'C':
362          if (rld::cc::is_exec_prefix_set ())
363            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
364          rld::cc::set_cc (optarg);
365          break;
366
367        case 'E':
368          if (rld::cc::is_cc_set ())
369            std::cerr << "warning: exec-prefix ignored when CC provided" << std::endl;
370          rld::cc::set_exec_prefix (optarg);
371          break;
372
373        case 'c':
374          rld::cc::set_flags (optarg, rld::cc::ft_cflags);
375          break;
376
377        case 'S':
378          symc = optarg;
379          break;
380
381        case '?':
382          usage (3);
383          break;
384
385        case 'h':
386          usage (0);
387          break;
388      }
389    }
390
391    /*
392     * Set the program name.
393     */
394    rld::set_progname (argv[0]);
395
396    argc -= optind;
397    argv += optind;
398
399    if (rld::verbose ())
400      std::cout << "RTEMS Kernel Symbols " << rld::version () << std::endl;
401
402    /*
403     * If there are no object files there is nothing to link.
404     */
405    if (argc == 0)
406      throw rld::error ("no kernel file", "options");
407    if (argc != 1)
408      throw rld::error ("only one kernel file", "options");
409    if (output.empty () && map.empty ())
410      throw rld::error ("no output or map", "options");
411
412    kernel_name = *argv;
413
414    if (rld::verbose ())
415      std::cout << "kernel: " << kernel_name << std::endl;
416
417    /*
418     * Load the symbols from the kernel.
419     */
420    try
421    {
422      /*
423       * Load the kernel ELF file symbol table.
424       */
425      kernel.open ();
426      kernel.add (kernel_name);
427      kernel.load_symbols (symbols, true);
428
429      /*
430       * If the full path to CC is not provided and the exec-prefix is not set
431       * by the command line see if it can be detected from the object file
432       * types. This must be after we have added the object files because they
433       * are used when detecting.
434       */
435      if (!cc.empty ())
436        rld::cc::set_cc (cc);
437      if (!rld::cc::is_cc_set () && !rld::cc::is_exec_prefix_set ())
438        rld::cc::set_exec_prefix (rld::elf::machine_type ());
439
440      /*
441       * Create a map file if asked too.
442       */
443      if (!map.empty ())
444      {
445        std::ofstream mout;
446        mout.open (map.c_str());
447        if (!mout.is_open ())
448          throw rld::error ("map file open failed", "map");
449        mout << "RTEMS Kernel Symbols Map" << std::endl
450             << " kernel: " << kernel_name << std::endl
451             << std::endl;
452        rld::symbols::output (mout, symbols);
453        mout.close ();
454      }
455
456      /*
457       * Create an output file if asked too.
458       */
459      if (!output.empty ())
460      {
461        rld::process::tempfile c (".c");
462
463        if (!symc.empty ())
464        {
465          c.override (symc);
466          c.keep ();
467        }
468
469        /*
470         * Generate and compile the symbol map.
471         */
472        generate_symmap (c, output, symbols, embed);
473      }
474
475      kernel.close ();
476    }
477    catch (...)
478    {
479      kernel.close ();
480      throw;
481    }
482
483    kernel.close ();
484  }
485  catch (rld::error re)
486  {
487    std::cerr << "error: "
488              << re.where << ": " << re.what
489              << std::endl;
490    ec = 10;
491  }
492  catch (std::exception e)
493  {
494    int   status;
495    char* realname;
496    realname = abi::__cxa_demangle (e.what(), 0, 0, &status);
497    std::cerr << "error: exception: " << realname << " [";
498    ::free (realname);
499    const std::type_info &ti = typeid (e);
500    realname = abi::__cxa_demangle (ti.name(), 0, 0, &status);
501    std::cerr << realname << "] " << e.what () << std::endl;
502    ::free (realname);
503    ec = 11;
504  }
505  catch (...)
506  {
507    /*
508     * Helps to know if this happens.
509     */
510    std::cout << "error: unhandled exception" << std::endl;
511    ec = 12;
512  }
513
514  return ec;
515}
Note: See TracBrowser for help on using the repository browser.