1 | /* |
---|
2 | * Copyright (c) 2016-2018, Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * RTEMS Tools Project (http://www.rtems.org/) |
---|
5 | * This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
6 | * |
---|
7 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
8 | * purpose with or without fee is hereby granted, provided that the above |
---|
9 | * copyright notice and this permission notice appear in all copies. |
---|
10 | * |
---|
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
18 | */ |
---|
19 | /** |
---|
20 | * @file |
---|
21 | * |
---|
22 | * @ingroup rtems_rld |
---|
23 | * |
---|
24 | * @brief RTEMS Init dumps the initialisation section data in a format we can |
---|
25 | * read. |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #if HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <iostream> |
---|
34 | #include <iomanip> |
---|
35 | |
---|
36 | #include <cxxabi.h> |
---|
37 | #include <signal.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <string.h> |
---|
40 | #include <unistd.h> |
---|
41 | |
---|
42 | #include <getopt.h> |
---|
43 | |
---|
44 | #include <rld.h> |
---|
45 | #include <rld-buffer.h> |
---|
46 | #include <rld-dwarf.h> |
---|
47 | #include <rld-files.h> |
---|
48 | #include <rld-process.h> |
---|
49 | #include <rld-rtems.h> |
---|
50 | |
---|
51 | #ifndef HAVE_KILL |
---|
52 | #define kill(p,s) raise(s) |
---|
53 | #endif |
---|
54 | |
---|
55 | namespace rld |
---|
56 | { |
---|
57 | namespace exeinfo |
---|
58 | { |
---|
59 | /** |
---|
60 | * Default section list. |
---|
61 | */ |
---|
62 | const char* default_init[] = |
---|
63 | { |
---|
64 | ".rtemsroset", |
---|
65 | ".ctors", |
---|
66 | ".init", |
---|
67 | 0 |
---|
68 | }; |
---|
69 | |
---|
70 | const char* default_fini[] = |
---|
71 | { |
---|
72 | ".dtors", |
---|
73 | ".fini", |
---|
74 | 0 |
---|
75 | }; |
---|
76 | |
---|
77 | /** |
---|
78 | * ARM section list. |
---|
79 | */ |
---|
80 | const char* arm_init[] = |
---|
81 | { |
---|
82 | ".rtemsroset", |
---|
83 | ".init_array", |
---|
84 | 0 |
---|
85 | }; |
---|
86 | |
---|
87 | const char* arm_fini[] = |
---|
88 | { |
---|
89 | ".fini_array", |
---|
90 | 0 |
---|
91 | }; |
---|
92 | |
---|
93 | /** |
---|
94 | * An executable section's address, offset, size and alignment. |
---|
95 | */ |
---|
96 | struct section |
---|
97 | { |
---|
98 | const files::section& sec; //< The executable's section. |
---|
99 | buffer::buffer data; //< The section's data. |
---|
100 | files::byteorder byteorder; //< The image's byteorder. |
---|
101 | |
---|
102 | /** |
---|
103 | * Construct the section. |
---|
104 | */ |
---|
105 | section (const files::section& sec, files::byteorder byteorder); |
---|
106 | |
---|
107 | /** |
---|
108 | * Copy construct. |
---|
109 | */ |
---|
110 | section (const section& orig); |
---|
111 | |
---|
112 | /** |
---|
113 | * Clean up the section's memory. |
---|
114 | */ |
---|
115 | ~section (); |
---|
116 | |
---|
117 | private: |
---|
118 | /** |
---|
119 | * Default constructor. |
---|
120 | */ |
---|
121 | section (); |
---|
122 | }; |
---|
123 | |
---|
124 | /** |
---|
125 | * Container of sections. Order is the address in memory. |
---|
126 | */ |
---|
127 | typedef std::list < section > sections; |
---|
128 | |
---|
129 | /** |
---|
130 | * The kernel image. |
---|
131 | */ |
---|
132 | struct image |
---|
133 | { |
---|
134 | files::object exe; //< The object file that is the executable. |
---|
135 | dwarf::file debug; //< The executable's DWARF details. |
---|
136 | symbols::table symbols; //< The synbols for a map. |
---|
137 | symbols::addrtab addresses; //< The symbols keyed by address. |
---|
138 | files::sections secs; //< The sections in the executable. |
---|
139 | const char** init; //< The init section's list for the machinetype. |
---|
140 | const char** fini; //< The fini section's list for the machinetype. |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | * Load the executable file. |
---|
145 | */ |
---|
146 | image (const std::string exe_name); |
---|
147 | |
---|
148 | /** |
---|
149 | * Clean up. |
---|
150 | */ |
---|
151 | ~image (); |
---|
152 | |
---|
153 | /* |
---|
154 | * Check the compiler and flags match. |
---|
155 | */ |
---|
156 | void output_compilation_unit (bool objects); |
---|
157 | |
---|
158 | /* |
---|
159 | * Output the sections. |
---|
160 | */ |
---|
161 | void output_sections (); |
---|
162 | |
---|
163 | /* |
---|
164 | * Output the init sections. |
---|
165 | */ |
---|
166 | void output_init (); |
---|
167 | |
---|
168 | /* |
---|
169 | * Output the fini sections. |
---|
170 | */ |
---|
171 | void output_fini (); |
---|
172 | |
---|
173 | /* |
---|
174 | * Output init/fini worker. |
---|
175 | */ |
---|
176 | void output_init_fini (const char* label, const char** names); |
---|
177 | }; |
---|
178 | |
---|
179 | section::section (const files::section& sec, files::byteorder byteorder) |
---|
180 | : sec (sec), |
---|
181 | data (sec.size, byteorder == rld::files::little_endian), |
---|
182 | byteorder (byteorder) |
---|
183 | { |
---|
184 | } |
---|
185 | |
---|
186 | section::section (const section& orig) |
---|
187 | : sec (orig.sec), |
---|
188 | data (orig.data) |
---|
189 | { |
---|
190 | } |
---|
191 | |
---|
192 | section::~section () |
---|
193 | { |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * Helper for for_each to filter and load the sections we wish to |
---|
198 | * dump. |
---|
199 | */ |
---|
200 | class section_loader: |
---|
201 | public std::unary_function < const files::section, void > |
---|
202 | { |
---|
203 | public: |
---|
204 | |
---|
205 | section_loader (image& img, sections& secs, const char* names[]); |
---|
206 | |
---|
207 | ~section_loader (); |
---|
208 | |
---|
209 | void operator () (const files::section& fsec); |
---|
210 | |
---|
211 | private: |
---|
212 | |
---|
213 | image& img; |
---|
214 | sections& secs; |
---|
215 | const char** names; |
---|
216 | }; |
---|
217 | |
---|
218 | section_loader::section_loader (image& img, |
---|
219 | sections& secs, |
---|
220 | const char* names[]) |
---|
221 | : img (img), |
---|
222 | secs (secs), |
---|
223 | names (names) |
---|
224 | { |
---|
225 | } |
---|
226 | |
---|
227 | section_loader::~section_loader () |
---|
228 | { |
---|
229 | } |
---|
230 | |
---|
231 | void |
---|
232 | section_loader::operator () (const files::section& fsec) |
---|
233 | { |
---|
234 | if (rld::verbose () >= RLD_VERBOSE_DETAILS) |
---|
235 | std::cout << "init:section-loader: " << fsec.name |
---|
236 | << " address=" << std::hex << fsec.address << std::dec |
---|
237 | << " relocs=" << fsec.relocs.size () |
---|
238 | << " fsec.size=" << fsec.size |
---|
239 | << " fsec.alignment=" << fsec.alignment |
---|
240 | << " fsec.rela=" << fsec.rela |
---|
241 | << std::endl; |
---|
242 | |
---|
243 | for (int n = 0; names[n] != 0; ++n) |
---|
244 | { |
---|
245 | if (fsec.name == names[n]) |
---|
246 | { |
---|
247 | if (rld::verbose () >= RLD_VERBOSE_DETAILS) |
---|
248 | std::cout << "init:section-loader: " << fsec.name |
---|
249 | << " added" << std::endl; |
---|
250 | |
---|
251 | section sec (fsec, img.exe.get_byteorder ()); |
---|
252 | img.exe.seek (fsec.offset); |
---|
253 | sec.data.read (img.exe, fsec.size); |
---|
254 | secs.push_back (sec); |
---|
255 | break; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | image::image (const std::string exe_name) |
---|
261 | : exe (exe_name), |
---|
262 | init (0), |
---|
263 | fini (0) |
---|
264 | { |
---|
265 | /* |
---|
266 | * Open the executable file and begin the session on it. |
---|
267 | */ |
---|
268 | exe.open (); |
---|
269 | exe.begin (); |
---|
270 | debug.begin (exe.elf ()); |
---|
271 | |
---|
272 | if (!exe.valid ()) |
---|
273 | throw rld::error ("Not valid: " + exe.name ().full (), |
---|
274 | "init::image"); |
---|
275 | |
---|
276 | /* |
---|
277 | * Set up the section lists for the machiner type. |
---|
278 | */ |
---|
279 | switch (exe.elf ().machinetype ()) |
---|
280 | { |
---|
281 | case EM_ARM: |
---|
282 | init = arm_init; |
---|
283 | fini = arm_fini; |
---|
284 | break; |
---|
285 | default: |
---|
286 | init = default_init; |
---|
287 | fini = default_fini; |
---|
288 | break; |
---|
289 | } |
---|
290 | |
---|
291 | /* |
---|
292 | * Load the symbols and sections. |
---|
293 | */ |
---|
294 | exe.load_symbols (symbols, true); |
---|
295 | debug.load_debug (); |
---|
296 | symbols.globals (addresses); |
---|
297 | symbols.weaks (addresses); |
---|
298 | symbols.locals (addresses); |
---|
299 | exe.get_sections (secs); |
---|
300 | } |
---|
301 | |
---|
302 | image::~image () |
---|
303 | { |
---|
304 | } |
---|
305 | |
---|
306 | void |
---|
307 | image::output_compilation_unit (bool objects) |
---|
308 | { |
---|
309 | dwarf::compilation_units& cus = debug.get_cus (); |
---|
310 | |
---|
311 | std::cout << "Compilation: " << std::endl; |
---|
312 | |
---|
313 | rld::strings flag_exceptions = { "-O", |
---|
314 | "-g", |
---|
315 | "-mtune=", |
---|
316 | "-fno-builtin", |
---|
317 | "-fno-inline", |
---|
318 | "-fexceptions", |
---|
319 | "-fnon-call-exceptions", |
---|
320 | "-fvisibility=", |
---|
321 | "-fno-stack-protector", |
---|
322 | "-fbuilding-libgcc", |
---|
323 | "-fno-implicit-templates", |
---|
324 | "-ffunction-sections", |
---|
325 | "-fdata-sections", |
---|
326 | "-frandom-seed=", |
---|
327 | "-fno-common", |
---|
328 | "-fno-keep-inline-functions" }; |
---|
329 | |
---|
330 | dwarf::producer_sources producers; |
---|
331 | |
---|
332 | debug.get_producer_sources (producers); |
---|
333 | |
---|
334 | /* |
---|
335 | * Find which flags are common to the building of all source. We are only |
---|
336 | * interested in files that have any flags. This filters out things like |
---|
337 | * the assembler which does not have flags. |
---|
338 | */ |
---|
339 | |
---|
340 | rld::strings all_flags; |
---|
341 | |
---|
342 | size_t source_max = 0; |
---|
343 | |
---|
344 | for (auto& p : producers) |
---|
345 | { |
---|
346 | dwarf::source_flags_compare compare; |
---|
347 | std::sort (p.sources.begin (), p.sources.end (), compare); |
---|
348 | |
---|
349 | for (auto& s : p.sources) |
---|
350 | { |
---|
351 | size_t len = rld::path::basename (s.source).length (); |
---|
352 | if (len > source_max) |
---|
353 | source_max = len; |
---|
354 | |
---|
355 | if (!s.flags.empty ()) |
---|
356 | { |
---|
357 | for (auto& f : s.flags) |
---|
358 | { |
---|
359 | bool add = true; |
---|
360 | for (auto& ef : flag_exceptions) |
---|
361 | { |
---|
362 | if (rld::starts_with (f, ef)) |
---|
363 | { |
---|
364 | add = false; |
---|
365 | break; |
---|
366 | } |
---|
367 | } |
---|
368 | if (add) |
---|
369 | { |
---|
370 | for (auto& af : all_flags) |
---|
371 | { |
---|
372 | if (f == af) |
---|
373 | { |
---|
374 | add = false; |
---|
375 | break; |
---|
376 | } |
---|
377 | } |
---|
378 | if (add) |
---|
379 | all_flags.push_back (f); |
---|
380 | } |
---|
381 | } |
---|
382 | } |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | rld::strings common_flags; |
---|
387 | |
---|
388 | for (auto& flag : all_flags) |
---|
389 | { |
---|
390 | bool found_in_all = true; |
---|
391 | for (auto& p : producers) |
---|
392 | { |
---|
393 | for (auto& s : p.sources) |
---|
394 | { |
---|
395 | if (!s.flags.empty ()) |
---|
396 | { |
---|
397 | bool flag_found = false; |
---|
398 | for (auto& f : s.flags) |
---|
399 | { |
---|
400 | if (flag == f) |
---|
401 | { |
---|
402 | flag_found = true; |
---|
403 | break; |
---|
404 | } |
---|
405 | } |
---|
406 | if (!flag_found) |
---|
407 | { |
---|
408 | found_in_all = false; |
---|
409 | break; |
---|
410 | } |
---|
411 | } |
---|
412 | if (!found_in_all) |
---|
413 | break; |
---|
414 | } |
---|
415 | } |
---|
416 | if (found_in_all) |
---|
417 | common_flags.push_back (flag); |
---|
418 | } |
---|
419 | |
---|
420 | std::cout << " Producers: " << producers.size () << std::endl; |
---|
421 | |
---|
422 | for (auto& p : producers) |
---|
423 | { |
---|
424 | std::cout << " | " << p.producer |
---|
425 | << ": " << p.sources.size () << " objects" << std::endl; |
---|
426 | } |
---|
427 | |
---|
428 | std::cout << " Common flags: " << common_flags.size () << std::endl |
---|
429 | << " |"; |
---|
430 | |
---|
431 | for (auto& f : common_flags) |
---|
432 | std::cout << ' ' << f; |
---|
433 | std::cout << std::endl; |
---|
434 | |
---|
435 | if (objects) |
---|
436 | { |
---|
437 | std::cout << " Object files: " << cus.size () << std::endl; |
---|
438 | |
---|
439 | rld::strings filter_flags = common_flags; |
---|
440 | filter_flags.insert (filter_flags.end (), |
---|
441 | flag_exceptions.begin (), |
---|
442 | flag_exceptions.end()); |
---|
443 | |
---|
444 | for (auto& p : producers) |
---|
445 | { |
---|
446 | std::cout << ' ' << p.producer |
---|
447 | << ": " << p.sources.size () << " objects" << std::endl; |
---|
448 | for (auto& s : p.sources) |
---|
449 | { |
---|
450 | std::cout << " | " |
---|
451 | << std::setw (source_max + 1) << std::left |
---|
452 | << rld::path::basename (s.source); |
---|
453 | if (!s.flags.empty ()) |
---|
454 | { |
---|
455 | bool first = true; |
---|
456 | for (auto& f : s.flags) |
---|
457 | { |
---|
458 | bool present = false; |
---|
459 | for (auto& ff : filter_flags) |
---|
460 | { |
---|
461 | if (rld::starts_with(f, ff)) |
---|
462 | { |
---|
463 | present = true; |
---|
464 | break; |
---|
465 | } |
---|
466 | } |
---|
467 | if (!present) |
---|
468 | { |
---|
469 | if (first) |
---|
470 | { |
---|
471 | std::cout << ':'; |
---|
472 | first = false; |
---|
473 | } |
---|
474 | std::cout << ' ' << f; |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | std::cout << std::endl; |
---|
479 | } |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | std::cout << std::endl; |
---|
484 | } |
---|
485 | |
---|
486 | void |
---|
487 | image::output_sections () |
---|
488 | { |
---|
489 | std::cout << "Sections: " << secs.size () << std::endl; |
---|
490 | |
---|
491 | size_t max_section_name = 0; |
---|
492 | |
---|
493 | for (files::sections::const_iterator si = secs.begin (); |
---|
494 | si != secs.end (); |
---|
495 | ++si) |
---|
496 | { |
---|
497 | const files::section& sec = *si; |
---|
498 | if (sec.name.length() > max_section_name) |
---|
499 | max_section_name = sec.name.length(); |
---|
500 | } |
---|
501 | |
---|
502 | for (files::sections::const_iterator si = secs.begin (); |
---|
503 | si != secs.end (); |
---|
504 | ++si) |
---|
505 | { |
---|
506 | const files::section& sec = *si; |
---|
507 | |
---|
508 | #define SF(f, i, c) if (sec.flags & (f)) flags[i] = c |
---|
509 | |
---|
510 | std::string flags ("--------------"); |
---|
511 | |
---|
512 | SF (SHF_WRITE, 0, 'W'); |
---|
513 | SF (SHF_ALLOC, 1, 'A'); |
---|
514 | SF (SHF_EXECINSTR, 2, 'E'); |
---|
515 | SF (SHF_MERGE, 3, 'M'); |
---|
516 | SF (SHF_STRINGS, 4, 'S'); |
---|
517 | SF (SHF_INFO_LINK, 5, 'I'); |
---|
518 | SF (SHF_LINK_ORDER, 6, 'L'); |
---|
519 | SF (SHF_OS_NONCONFORMING, 7, 'N'); |
---|
520 | SF (SHF_GROUP, 8, 'G'); |
---|
521 | SF (SHF_TLS, 9, 'T'); |
---|
522 | SF (SHF_AMD64_LARGE, 10, 'a'); |
---|
523 | SF (SHF_ENTRYSECT, 11, 'e'); |
---|
524 | SF (SHF_COMDEF, 12, 'c'); |
---|
525 | SF (SHF_ORDERED, 13, 'O'); |
---|
526 | |
---|
527 | std::cout << " " << std::left |
---|
528 | << std::setw (max_section_name) << sec.name |
---|
529 | << " " << flags |
---|
530 | << std::right << std::hex << std::setfill ('0') |
---|
531 | << " addr: 0x" << std::setw (8) << sec.address |
---|
532 | << " 0x" << std::setw (8) << sec.address + sec.size |
---|
533 | << std::dec << std::setfill (' ') |
---|
534 | << " size: " << std::setw (10) << sec.size |
---|
535 | << " align: " << std::setw (3) << sec.alignment |
---|
536 | << " relocs: " << std::setw (6) << sec.relocs.size () |
---|
537 | << std::endl; |
---|
538 | } |
---|
539 | |
---|
540 | std::cout << std::endl; |
---|
541 | } |
---|
542 | |
---|
543 | void |
---|
544 | image::output_init () |
---|
545 | { |
---|
546 | output_init_fini ("Init", init); |
---|
547 | } |
---|
548 | |
---|
549 | void |
---|
550 | image::output_fini () |
---|
551 | { |
---|
552 | output_init_fini ("Fini", fini); |
---|
553 | } |
---|
554 | |
---|
555 | void |
---|
556 | image::output_init_fini (const char* label, const char** names) |
---|
557 | { |
---|
558 | /* |
---|
559 | * Load the sections. |
---|
560 | */ |
---|
561 | sections ifsecs; |
---|
562 | std::for_each (secs.begin (), secs.end (), |
---|
563 | section_loader (*this, ifsecs, names)); |
---|
564 | |
---|
565 | std::cout << label << " sections: " << ifsecs.size () << std::endl; |
---|
566 | |
---|
567 | for (auto& sec : ifsecs) |
---|
568 | { |
---|
569 | const size_t machine_size = exe.elf ().machine_size (); |
---|
570 | const int count = sec.data.level () / machine_size; |
---|
571 | |
---|
572 | std::cout << " " << sec.sec.name << std::endl; |
---|
573 | |
---|
574 | for (int i = 0; i < count; ++i) |
---|
575 | { |
---|
576 | uint32_t address; |
---|
577 | symbols::symbol* sym; |
---|
578 | sec.data >> address; |
---|
579 | sym = addresses[address]; |
---|
580 | std::cout << " " |
---|
581 | << std::hex << std::setfill ('0') |
---|
582 | << "0x" << std::setw (8) << address |
---|
583 | << std::dec << std::setfill ('0'); |
---|
584 | if (sym) |
---|
585 | { |
---|
586 | std::string label = sym->name (); |
---|
587 | if (rld::symbols::is_cplusplus (label)) |
---|
588 | rld::symbols::demangle_name (label, label); |
---|
589 | std::cout << " " << label; |
---|
590 | } |
---|
591 | else |
---|
592 | { |
---|
593 | std::cout << " no symbol"; |
---|
594 | } |
---|
595 | std::cout << std::endl; |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | std::cout << std::endl; |
---|
600 | } |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | /** |
---|
605 | * RTEMS Exe Info options. This needs to be rewritten to be like cc where only |
---|
606 | * a single '-' and long options is present. |
---|
607 | */ |
---|
608 | static struct option rld_opts[] = { |
---|
609 | { "help", no_argument, NULL, 'h' }, |
---|
610 | { "version", no_argument, NULL, 'V' }, |
---|
611 | { "verbose", no_argument, NULL, 'v' }, |
---|
612 | { "map", no_argument, NULL, 'M' }, |
---|
613 | { "all", no_argument, NULL, 'a' }, |
---|
614 | { "sections", no_argument, NULL, 'S' }, |
---|
615 | { "init", no_argument, NULL, 'I' }, |
---|
616 | { "fini", no_argument, NULL, 'F' }, |
---|
617 | { NULL, 0, NULL, 0 } |
---|
618 | }; |
---|
619 | |
---|
620 | void |
---|
621 | usage (int exit_code) |
---|
622 | { |
---|
623 | std::cout << "rtems-exeinfo [options] objects" << std::endl |
---|
624 | << "Options and arguments:" << std::endl |
---|
625 | << " -h : help (also --help)" << std::endl |
---|
626 | << " -V : print linker version number and exit (also --version)" << std::endl |
---|
627 | << " -v : verbose (trace import parts), can supply multiple times" << std::endl |
---|
628 | << " to increase verbosity (also --verbose)" << std::endl |
---|
629 | << " -M : generate map output (also --map)" << std::endl |
---|
630 | << " -a : all output excluding the map (also --all)" << std::endl |
---|
631 | << " -S : show all section (also --sections)" << std::endl |
---|
632 | << " -I : show init section tables (also --init)" << std::endl |
---|
633 | << " -F : show fini section tables (also --fini)" << std::endl |
---|
634 | << " -O : show object files (also --objects)" << std::endl; |
---|
635 | ::exit (exit_code); |
---|
636 | } |
---|
637 | |
---|
638 | static void |
---|
639 | fatal_signal (int signum) |
---|
640 | { |
---|
641 | signal (signum, SIG_DFL); |
---|
642 | |
---|
643 | rld::process::temporaries_clean_up (); |
---|
644 | |
---|
645 | /* |
---|
646 | * Get the same signal again, this time not handled, so its normal effect |
---|
647 | * occurs. |
---|
648 | */ |
---|
649 | kill (getpid (), signum); |
---|
650 | } |
---|
651 | |
---|
652 | static void |
---|
653 | setup_signals (void) |
---|
654 | { |
---|
655 | if (signal (SIGINT, SIG_IGN) != SIG_IGN) |
---|
656 | signal (SIGINT, fatal_signal); |
---|
657 | #ifdef SIGHUP |
---|
658 | if (signal (SIGHUP, SIG_IGN) != SIG_IGN) |
---|
659 | signal (SIGHUP, fatal_signal); |
---|
660 | #endif |
---|
661 | if (signal (SIGTERM, SIG_IGN) != SIG_IGN) |
---|
662 | signal (SIGTERM, fatal_signal); |
---|
663 | #ifdef SIGPIPE |
---|
664 | if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) |
---|
665 | signal (SIGPIPE, fatal_signal); |
---|
666 | #endif |
---|
667 | #ifdef SIGCHLD |
---|
668 | signal (SIGCHLD, SIG_DFL); |
---|
669 | #endif |
---|
670 | } |
---|
671 | |
---|
672 | void |
---|
673 | unhandled_exception (void) |
---|
674 | { |
---|
675 | std::cerr << "error: exception handling error, please report" << std::endl; |
---|
676 | exit (1); |
---|
677 | } |
---|
678 | |
---|
679 | int |
---|
680 | main (int argc, char* argv[]) |
---|
681 | { |
---|
682 | int ec = 0; |
---|
683 | |
---|
684 | setup_signals (); |
---|
685 | |
---|
686 | std::set_terminate(unhandled_exception); |
---|
687 | |
---|
688 | try |
---|
689 | { |
---|
690 | std::string exe_name; |
---|
691 | bool map = false; |
---|
692 | bool all = false; |
---|
693 | bool sections = false; |
---|
694 | bool init = false; |
---|
695 | bool fini = false; |
---|
696 | bool objects = false; |
---|
697 | |
---|
698 | rld::set_cmdline (argc, argv); |
---|
699 | |
---|
700 | while (true) |
---|
701 | { |
---|
702 | int opt = ::getopt_long (argc, argv, "hvVMaSIF", rld_opts, NULL); |
---|
703 | if (opt < 0) |
---|
704 | break; |
---|
705 | |
---|
706 | switch (opt) |
---|
707 | { |
---|
708 | case 'V': |
---|
709 | std::cout << "rtems-exeinfo (RTEMS Executable Info) " << rld::version () |
---|
710 | << ", RTEMS revision " << rld::rtems::version () |
---|
711 | << std::endl; |
---|
712 | ::exit (0); |
---|
713 | break; |
---|
714 | |
---|
715 | case 'v': |
---|
716 | rld::verbose_inc (); |
---|
717 | break; |
---|
718 | |
---|
719 | case 'M': |
---|
720 | map = true; |
---|
721 | break; |
---|
722 | |
---|
723 | case 'a': |
---|
724 | all = true; |
---|
725 | break; |
---|
726 | |
---|
727 | case 'I': |
---|
728 | init = true; |
---|
729 | break; |
---|
730 | |
---|
731 | case 'F': |
---|
732 | fini = true; |
---|
733 | break; |
---|
734 | |
---|
735 | case 'S': |
---|
736 | sections = true; |
---|
737 | break; |
---|
738 | |
---|
739 | case 'O': |
---|
740 | objects = true; |
---|
741 | break; |
---|
742 | |
---|
743 | case '?': |
---|
744 | usage (3); |
---|
745 | break; |
---|
746 | |
---|
747 | case 'h': |
---|
748 | usage (0); |
---|
749 | break; |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | /* |
---|
754 | * Set the program name. |
---|
755 | */ |
---|
756 | rld::set_progname (argv[0]); |
---|
757 | |
---|
758 | argc -= optind; |
---|
759 | argv += optind; |
---|
760 | |
---|
761 | std::cout << "RTEMS Executable Info " << rld::version () << std::endl; |
---|
762 | std::cout << " " << rld::get_cmdline () << std::endl; |
---|
763 | |
---|
764 | /* |
---|
765 | * All means all types of output. |
---|
766 | */ |
---|
767 | if (all) |
---|
768 | { |
---|
769 | sections = true; |
---|
770 | init = true; |
---|
771 | fini = true; |
---|
772 | objects = true; |
---|
773 | } |
---|
774 | |
---|
775 | /* |
---|
776 | * If there is no executable there is nothing to convert. |
---|
777 | */ |
---|
778 | if (argc == 0) |
---|
779 | throw rld::error ("no executable", "options"); |
---|
780 | if (argc > 1) |
---|
781 | throw rld::error ("only a single executable", "options"); |
---|
782 | |
---|
783 | /* |
---|
784 | * The name of the executable. |
---|
785 | */ |
---|
786 | exe_name = *argv; |
---|
787 | |
---|
788 | if (rld::verbose ()) |
---|
789 | std::cout << "exe-image: " << exe_name << std::endl; |
---|
790 | |
---|
791 | /* |
---|
792 | * Open the executable and read the symbols. |
---|
793 | */ |
---|
794 | rld::exeinfo::image exe (exe_name); |
---|
795 | |
---|
796 | std::cout << "exe: " << exe.exe.name ().full () << std::endl |
---|
797 | << std::endl; |
---|
798 | |
---|
799 | /* |
---|
800 | * Generate the output. |
---|
801 | */ |
---|
802 | exe.output_compilation_unit (objects); |
---|
803 | if (sections) |
---|
804 | exe.output_sections (); |
---|
805 | if (init) |
---|
806 | exe.output_init (); |
---|
807 | if (fini) |
---|
808 | exe.output_fini (); |
---|
809 | |
---|
810 | /* |
---|
811 | * Map ? |
---|
812 | */ |
---|
813 | if (map) |
---|
814 | rld::symbols::output (std::cout, exe.symbols); |
---|
815 | } |
---|
816 | catch (rld::error re) |
---|
817 | { |
---|
818 | std::cerr << "error: " |
---|
819 | << re.where << ": " << re.what |
---|
820 | << std::endl; |
---|
821 | ec = 10; |
---|
822 | } |
---|
823 | catch (std::exception e) |
---|
824 | { |
---|
825 | int status; |
---|
826 | char* realname; |
---|
827 | realname = abi::__cxa_demangle (e.what(), 0, 0, &status); |
---|
828 | std::cerr << "error: exception: " << realname << " ["; |
---|
829 | ::free (realname); |
---|
830 | const std::type_info &ti = typeid (e); |
---|
831 | realname = abi::__cxa_demangle (ti.name(), 0, 0, &status); |
---|
832 | std::cerr << realname << "] " << e.what () << std::endl << std::flush; |
---|
833 | ::free (realname); |
---|
834 | ec = 11; |
---|
835 | } |
---|
836 | catch (...) |
---|
837 | { |
---|
838 | /* |
---|
839 | * Helps to know if this happens. |
---|
840 | */ |
---|
841 | std::cerr << "error: unhandled exception" << std::endl; |
---|
842 | ec = 12; |
---|
843 | } |
---|
844 | |
---|
845 | return ec; |
---|
846 | } |
---|