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 | #include <rld-rtems.h> |
---|
47 | |
---|
48 | #ifndef HAVE_KILL |
---|
49 | #define kill(p,s) raise(s) |
---|
50 | #endif |
---|
51 | |
---|
52 | /** |
---|
53 | * Header text. |
---|
54 | */ |
---|
55 | static const char* c_header[] = |
---|
56 | { |
---|
57 | "/*", |
---|
58 | " * RTEMS Global Symbol Table", |
---|
59 | " * Automatically generated. Do not edit..", |
---|
60 | " */", |
---|
61 | "", |
---|
62 | "extern const unsigned char rtems__rtl_base_globals[];", |
---|
63 | "extern const unsigned int rtems__rtl_base_globals_size;", |
---|
64 | "", |
---|
65 | "void rtems_rtl_base_sym_global_add (const unsigned char* , unsigned int );", |
---|
66 | "", |
---|
67 | "asm(\".section \\\".rodata\\\"\");", |
---|
68 | "", |
---|
69 | "asm(\" .align 4\");", |
---|
70 | "asm(\" .local rtems__rtl_base_globals\");", |
---|
71 | "asm(\"rtems__rtl_base_globals:\");", |
---|
72 | "#if __mips__", |
---|
73 | " asm(\" .align 0\");", |
---|
74 | "#else", |
---|
75 | " asm(\" .balign 1\");", |
---|
76 | "#endif", |
---|
77 | 0 |
---|
78 | }; |
---|
79 | |
---|
80 | static const char* c_trailer[] = |
---|
81 | { |
---|
82 | "asm(\" .byte 0\");", |
---|
83 | "asm(\" .ascii \\\"\\xde\\xad\\xbe\\xef\\\"\");", |
---|
84 | #if BROKEN_ON_SOME_ASSEMBLERS |
---|
85 | "asm(\" .type rtems__rtl_base_globals, #object\");", |
---|
86 | "asm(\" .size rtems__rtl_base_globals, . - rtems__rtl_base_globals\");", |
---|
87 | #endif |
---|
88 | "", |
---|
89 | "/*", |
---|
90 | " * Symbol table size.", |
---|
91 | " */", |
---|
92 | "asm(\" .align 4\");", |
---|
93 | "asm(\" .local rtems__rtl_base_globals_size\");", |
---|
94 | #if BROKEN_ON_SOME_ASSEMBLERS |
---|
95 | "asm(\" .type rtems__rtl_base_globals_size, #object\");", |
---|
96 | "asm(\" .size rtems__rtl_base_globals_size, 4\");", |
---|
97 | #endif |
---|
98 | "asm(\"rtems__rtl_base_globals_size:\");", |
---|
99 | "asm(\" .long rtems__rtl_base_globals_size - rtems__rtl_base_globals\");", |
---|
100 | "", |
---|
101 | 0 |
---|
102 | }; |
---|
103 | |
---|
104 | static const char* c_rtl_call_body[] = |
---|
105 | { |
---|
106 | "{", |
---|
107 | " rtems_rtl_base_sym_global_add (&rtems__rtl_base_globals[0],", |
---|
108 | " rtems__rtl_base_globals_size);", |
---|
109 | "}", |
---|
110 | 0 |
---|
111 | }; |
---|
112 | |
---|
113 | /** |
---|
114 | * Paint the data to the temporary file. |
---|
115 | */ |
---|
116 | static void |
---|
117 | temporary_file_paint (rld::process::tempfile& t, const char* lines[]) |
---|
118 | { |
---|
119 | for (int l = 0; lines[l]; ++l) |
---|
120 | t.write_line (lines[l]); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * The constructor trailer. |
---|
125 | */ |
---|
126 | static void |
---|
127 | c_constructor_trailer (rld::process::tempfile& c) |
---|
128 | { |
---|
129 | c.write_line ("static void init(void) __attribute__ ((constructor));"); |
---|
130 | c.write_line ("static void init(void)"); |
---|
131 | temporary_file_paint (c, c_rtl_call_body); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * The embedded trailer. |
---|
136 | */ |
---|
137 | static void |
---|
138 | c_embedded_trailer (rld::process::tempfile& c) |
---|
139 | { |
---|
140 | c.write_line ("void rtems_rtl_base_global_syms_init(void);"); |
---|
141 | c.write_line ("void rtems_rtl_base_global_syms_init(void)"); |
---|
142 | temporary_file_paint (c, c_rtl_call_body); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Generate the symbol map object file for loading or linking into |
---|
147 | * a running RTEMS machine. |
---|
148 | */ |
---|
149 | static void |
---|
150 | generate_c (rld::process::tempfile& c, |
---|
151 | rld::symbols::table& symbols, |
---|
152 | bool embed) |
---|
153 | { |
---|
154 | temporary_file_paint (c, c_header); |
---|
155 | |
---|
156 | for (rld::symbols::symtab::const_iterator si = symbols.globals ().begin (); |
---|
157 | si != symbols.globals ().end (); |
---|
158 | ++si) |
---|
159 | { |
---|
160 | const rld::symbols::symbol& sym = *((*si).second); |
---|
161 | |
---|
162 | c.write_line ("asm(\" .asciz \\\"" + sym.name () + "\\\"\");"); |
---|
163 | |
---|
164 | if (embed) |
---|
165 | { |
---|
166 | c.write_line ("asm(\" .long " + sym.name () + "\");"); |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | std::stringstream oss; |
---|
171 | oss << std::hex << std::setfill ('0') << std::setw (8) << sym.value (); |
---|
172 | c.write_line ("asm(\" .long 0x" + oss.str () + "\");"); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | temporary_file_paint (c, c_trailer); |
---|
177 | |
---|
178 | if (embed) |
---|
179 | c_embedded_trailer (c); |
---|
180 | else |
---|
181 | c_constructor_trailer (c); |
---|
182 | } |
---|
183 | |
---|
184 | static void |
---|
185 | generate_symmap (rld::process::tempfile& c, |
---|
186 | const std::string& output, |
---|
187 | rld::symbols::table& symbols, |
---|
188 | bool embed) |
---|
189 | { |
---|
190 | c.open (true); |
---|
191 | |
---|
192 | if (rld::verbose ()) |
---|
193 | std::cout << "symbol C file: " << c.name () << std::endl; |
---|
194 | |
---|
195 | generate_c (c, symbols, embed); |
---|
196 | |
---|
197 | if (rld::verbose ()) |
---|
198 | std::cout << "symbol O file: " << output << std::endl; |
---|
199 | |
---|
200 | rld::process::arg_container args; |
---|
201 | |
---|
202 | rld::cc::make_cc_command (args); |
---|
203 | rld::cc::append_flags (rld::cc::ft_cflags, args); |
---|
204 | |
---|
205 | args.push_back ("-O2"); |
---|
206 | args.push_back ("-c"); |
---|
207 | args.push_back ("-o"); |
---|
208 | args.push_back (output); |
---|
209 | args.push_back (c.name ()); |
---|
210 | |
---|
211 | rld::process::tempfile out; |
---|
212 | rld::process::tempfile err; |
---|
213 | rld::process::status status; |
---|
214 | |
---|
215 | status = rld::process::execute (rld::cc::get_cc (), |
---|
216 | args, |
---|
217 | out.name (), |
---|
218 | err.name ()); |
---|
219 | |
---|
220 | |
---|
221 | if ((status.type != rld::process::status::normal) || |
---|
222 | (status.code != 0)) |
---|
223 | { |
---|
224 | err.output (rld::cc::get_cc (), std::cout); |
---|
225 | throw rld::error ("Compiler error", "compiling wrapper"); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * RTEMS Symbols options. |
---|
231 | */ |
---|
232 | static struct option rld_opts[] = { |
---|
233 | { "help", no_argument, NULL, 'h' }, |
---|
234 | { "version", no_argument, NULL, 'V' }, |
---|
235 | { "verbose", no_argument, NULL, 'v' }, |
---|
236 | { "warn", no_argument, NULL, 'w' }, |
---|
237 | { "keep", no_argument, NULL, 'k' }, |
---|
238 | { "embed", no_argument, NULL, 'e' }, |
---|
239 | { "symc", required_argument, NULL, 'S' }, |
---|
240 | { "output", required_argument, NULL, 'o' }, |
---|
241 | { "map", required_argument, NULL, 'm' }, |
---|
242 | { "cc", required_argument, NULL, 'C' }, |
---|
243 | { "exec-prefix", required_argument, NULL, 'E' }, |
---|
244 | { "cflags", required_argument, NULL, 'c' }, |
---|
245 | { NULL, 0, NULL, 0 } |
---|
246 | }; |
---|
247 | |
---|
248 | void |
---|
249 | usage (int exit_code) |
---|
250 | { |
---|
251 | std::cout << "rtems-syms [options] kernel" << std::endl |
---|
252 | << "Options and arguments:" << std::endl |
---|
253 | << " -h : help (also --help)" << std::endl |
---|
254 | << " -V : print linker version number and exit (also --version)" << std::endl |
---|
255 | << " -v : verbose (trace import parts), can supply multiple times" << std::endl |
---|
256 | << " to increase verbosity (also --verbose)" << std::endl |
---|
257 | << " -w : generate warnings (also --warn)" << std::endl |
---|
258 | << " -k : keep temporary files (also --keep)" << std::endl |
---|
259 | << " -e : embedded symbol table (also --embed)" << std::endl |
---|
260 | << " -S : symbol's C file (also --symc)" << std::endl |
---|
261 | << " -o file : output object file (also --output)" << std::endl |
---|
262 | << " -m file : output a map file (also --map)" << std::endl |
---|
263 | << " -C file : execute file as the target C compiler (also --cc)" << std::endl |
---|
264 | << " -E prefix : the RTEMS tool prefix (also --exec-prefix)" << std::endl |
---|
265 | << " -c cflags : C compiler flags (also --cflags)" << std::endl; |
---|
266 | ::exit (exit_code); |
---|
267 | } |
---|
268 | |
---|
269 | static void |
---|
270 | fatal_signal (int signum) |
---|
271 | { |
---|
272 | signal (signum, SIG_DFL); |
---|
273 | |
---|
274 | rld::process::temporaries_clean_up (); |
---|
275 | |
---|
276 | /* |
---|
277 | * Get the same signal again, this time not handled, so its normal effect |
---|
278 | * occurs. |
---|
279 | */ |
---|
280 | kill (getpid (), signum); |
---|
281 | } |
---|
282 | |
---|
283 | static void |
---|
284 | setup_signals (void) |
---|
285 | { |
---|
286 | if (signal (SIGINT, SIG_IGN) != SIG_IGN) |
---|
287 | signal (SIGINT, fatal_signal); |
---|
288 | #ifdef SIGHUP |
---|
289 | if (signal (SIGHUP, SIG_IGN) != SIG_IGN) |
---|
290 | signal (SIGHUP, fatal_signal); |
---|
291 | #endif |
---|
292 | if (signal (SIGTERM, SIG_IGN) != SIG_IGN) |
---|
293 | signal (SIGTERM, fatal_signal); |
---|
294 | #ifdef SIGPIPE |
---|
295 | if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) |
---|
296 | signal (SIGPIPE, fatal_signal); |
---|
297 | #endif |
---|
298 | #ifdef SIGCHLD |
---|
299 | signal (SIGCHLD, SIG_DFL); |
---|
300 | #endif |
---|
301 | } |
---|
302 | |
---|
303 | int |
---|
304 | main (int argc, char* argv[]) |
---|
305 | { |
---|
306 | int ec = 0; |
---|
307 | |
---|
308 | setup_signals (); |
---|
309 | |
---|
310 | try |
---|
311 | { |
---|
312 | rld::files::cache kernel; |
---|
313 | rld::symbols::table symbols; |
---|
314 | std::string kernel_name; |
---|
315 | std::string output; |
---|
316 | std::string map; |
---|
317 | std::string cc; |
---|
318 | std::string symc; |
---|
319 | bool embed = 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 | << ", RTEMS revision " << rld::rtems::version () |
---|
334 | << std::endl; |
---|
335 | ::exit (0); |
---|
336 | break; |
---|
337 | |
---|
338 | case 'v': |
---|
339 | rld::verbose_inc (); |
---|
340 | break; |
---|
341 | |
---|
342 | case 'w': |
---|
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 | } |
---|