1 | /* |
---|
2 | * Copyright (c) 2016, 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-files.h> |
---|
47 | #include <rld-process.h> |
---|
48 | #include <rld-rtems.h> |
---|
49 | |
---|
50 | #ifndef HAVE_KILL |
---|
51 | #define kill(p,s) raise(s) |
---|
52 | #endif |
---|
53 | |
---|
54 | namespace rld |
---|
55 | { |
---|
56 | namespace exeinfo |
---|
57 | { |
---|
58 | /** |
---|
59 | * Sections we decode. |
---|
60 | */ |
---|
61 | const char* init_sections[] = |
---|
62 | { |
---|
63 | ".rtemsroset", |
---|
64 | ".ctors", |
---|
65 | 0 |
---|
66 | }; |
---|
67 | |
---|
68 | const char* fini_sections[] = |
---|
69 | { |
---|
70 | ".dtors", |
---|
71 | 0 |
---|
72 | }; |
---|
73 | |
---|
74 | /** |
---|
75 | * An executable section's address, offset, size and alignment. |
---|
76 | */ |
---|
77 | struct section |
---|
78 | { |
---|
79 | const files::section& sec; //< The executable's section. |
---|
80 | buffer::buffer data; //< The section's data. |
---|
81 | |
---|
82 | /** |
---|
83 | * Construct the section. |
---|
84 | */ |
---|
85 | section (const files::section& sec); |
---|
86 | |
---|
87 | /** |
---|
88 | * Copy construct. |
---|
89 | */ |
---|
90 | section (const section& orig); |
---|
91 | |
---|
92 | /** |
---|
93 | * Clean up the section's memory. |
---|
94 | */ |
---|
95 | ~section (); |
---|
96 | |
---|
97 | private: |
---|
98 | /** |
---|
99 | * Default constructor. |
---|
100 | */ |
---|
101 | section (); |
---|
102 | }; |
---|
103 | |
---|
104 | /** |
---|
105 | * Container of sections. Order is the address in memory. |
---|
106 | */ |
---|
107 | typedef std::list < section > sections; |
---|
108 | |
---|
109 | /** |
---|
110 | * The kernel image. |
---|
111 | */ |
---|
112 | struct image |
---|
113 | { |
---|
114 | files::object exe; //< The object file that is the executable. |
---|
115 | symbols::table symbols; //< The synbols for a map. |
---|
116 | symbols::addrtab addresses; //< The symbols keyed by address. |
---|
117 | files::sections secs; //< The sections in the executable. |
---|
118 | |
---|
119 | /** |
---|
120 | * Load the executable file. |
---|
121 | */ |
---|
122 | image (const std::string exe_name); |
---|
123 | |
---|
124 | /** |
---|
125 | * Clean up. |
---|
126 | */ |
---|
127 | ~image (); |
---|
128 | |
---|
129 | /* |
---|
130 | * Output the sections. |
---|
131 | */ |
---|
132 | void output_sections (); |
---|
133 | |
---|
134 | /* |
---|
135 | * Output the init sections. |
---|
136 | */ |
---|
137 | void output_init (); |
---|
138 | |
---|
139 | /* |
---|
140 | * Output the fini sections. |
---|
141 | */ |
---|
142 | void output_fini (); |
---|
143 | |
---|
144 | /* |
---|
145 | * Output init/fini worker. |
---|
146 | */ |
---|
147 | void output_init_fini (const char* label, const char** names); |
---|
148 | }; |
---|
149 | |
---|
150 | section::section (const files::section& sec) |
---|
151 | : sec (sec), |
---|
152 | data (sec.size) |
---|
153 | { |
---|
154 | } |
---|
155 | |
---|
156 | section::section (const section& orig) |
---|
157 | : sec (orig.sec), |
---|
158 | data (orig.data) |
---|
159 | { |
---|
160 | } |
---|
161 | |
---|
162 | section::~section () |
---|
163 | { |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Helper for for_each to filter and load the sections we wish to |
---|
168 | * dump. |
---|
169 | */ |
---|
170 | class section_loader: |
---|
171 | public std::unary_function < const files::section, void > |
---|
172 | { |
---|
173 | public: |
---|
174 | |
---|
175 | section_loader (image& img, sections& secs, const char* names[]); |
---|
176 | |
---|
177 | ~section_loader (); |
---|
178 | |
---|
179 | void operator () (const files::section& fsec); |
---|
180 | |
---|
181 | private: |
---|
182 | |
---|
183 | image& img; |
---|
184 | sections& secs; |
---|
185 | const char** names; |
---|
186 | }; |
---|
187 | |
---|
188 | section_loader::section_loader (image& img, |
---|
189 | sections& secs, |
---|
190 | const char* names[]) |
---|
191 | : img (img), |
---|
192 | secs (secs), |
---|
193 | names (names) |
---|
194 | { |
---|
195 | } |
---|
196 | |
---|
197 | section_loader::~section_loader () |
---|
198 | { |
---|
199 | } |
---|
200 | |
---|
201 | void |
---|
202 | section_loader::operator () (const files::section& fsec) |
---|
203 | { |
---|
204 | if (rld::verbose () >= RLD_VERBOSE_DETAILS) |
---|
205 | std::cout << "init:section-loader: " << fsec.name |
---|
206 | << " address=" << std::hex << fsec.address << std::dec |
---|
207 | << " relocs=" << fsec.relocs.size () |
---|
208 | << " fsec.size=" << fsec.size |
---|
209 | << " fsec.alignment=" << fsec.alignment |
---|
210 | << " fsec.rela=" << fsec.rela |
---|
211 | << std::endl; |
---|
212 | |
---|
213 | for (int n = 0; names[n] != 0; ++n) |
---|
214 | { |
---|
215 | if (fsec.name == names[n]) |
---|
216 | { |
---|
217 | if (rld::verbose () >= RLD_VERBOSE_DETAILS) |
---|
218 | std::cout << "init:section-loader: " << fsec.name |
---|
219 | << " added" << std::endl; |
---|
220 | |
---|
221 | section sec (fsec); |
---|
222 | img.exe.seek (fsec.offset); |
---|
223 | sec.data.read (img.exe, fsec.size); |
---|
224 | secs.push_back (sec); |
---|
225 | break; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | image::image (const std::string exe_name) |
---|
231 | : exe (exe_name) |
---|
232 | { |
---|
233 | /* |
---|
234 | * Open the executable file and begin the session on it. |
---|
235 | */ |
---|
236 | exe.open (); |
---|
237 | exe.begin (); |
---|
238 | |
---|
239 | if (!exe.valid ()) |
---|
240 | throw rld::error ("Not valid: " + exe.name ().full (), |
---|
241 | "init::image"); |
---|
242 | |
---|
243 | /* |
---|
244 | * Load the symbols and sections. |
---|
245 | */ |
---|
246 | exe.load_symbols (symbols, true); |
---|
247 | symbols.globals (addresses); |
---|
248 | symbols.weaks (addresses); |
---|
249 | symbols.locals (addresses); |
---|
250 | exe.get_sections (secs); |
---|
251 | } |
---|
252 | |
---|
253 | image::~image () |
---|
254 | { |
---|
255 | exe.close (); |
---|
256 | } |
---|
257 | |
---|
258 | void |
---|
259 | image::output_sections () |
---|
260 | { |
---|
261 | std::cout << "Sections: " << secs.size () << std::endl; |
---|
262 | |
---|
263 | for (files::sections::const_iterator si = secs.begin (); |
---|
264 | si != secs.end (); |
---|
265 | ++si) |
---|
266 | { |
---|
267 | const files::section& sec = *si; |
---|
268 | |
---|
269 | #define SF(f, i, c) if (sec.flags & (f)) flags[i] = c |
---|
270 | |
---|
271 | std::string flags ("--------------"); |
---|
272 | |
---|
273 | SF (SHF_WRITE, 0, 'W'); |
---|
274 | SF (SHF_ALLOC, 1, 'A'); |
---|
275 | SF (SHF_EXECINSTR, 2, 'E'); |
---|
276 | SF (SHF_MERGE, 3, 'M'); |
---|
277 | SF (SHF_STRINGS, 4, 'S'); |
---|
278 | SF (SHF_INFO_LINK, 5, 'I'); |
---|
279 | SF (SHF_LINK_ORDER, 6, 'L'); |
---|
280 | SF (SHF_OS_NONCONFORMING, 7, 'N'); |
---|
281 | SF (SHF_GROUP, 8, 'G'); |
---|
282 | SF (SHF_TLS, 9, 'T'); |
---|
283 | SF (SHF_AMD64_LARGE, 10, 'a'); |
---|
284 | SF (SHF_ENTRYSECT, 11, 'e'); |
---|
285 | SF (SHF_COMDEF, 12, 'c'); |
---|
286 | SF (SHF_ORDERED, 13, 'O'); |
---|
287 | |
---|
288 | std::cout << " " << std::left |
---|
289 | << std::setw (15) << sec.name |
---|
290 | << " " << flags |
---|
291 | << std::right << std::hex << std::setfill ('0') |
---|
292 | << " address: 0x" << std::setw (8) << sec.address |
---|
293 | << " 0x" << std::setw (8) << sec.address + sec.size |
---|
294 | << std::dec << std::setfill (' ') |
---|
295 | << " size: " << std::setw (7) << sec.size |
---|
296 | << " align: " << std::setw (3) << sec.alignment |
---|
297 | << " relocs: " << std::setw (4) << sec.relocs.size () |
---|
298 | << std::endl; |
---|
299 | } |
---|
300 | |
---|
301 | std::cout << std::endl; |
---|
302 | } |
---|
303 | |
---|
304 | void |
---|
305 | image::output_init () |
---|
306 | { |
---|
307 | output_init_fini ("Init", init_sections); |
---|
308 | } |
---|
309 | |
---|
310 | void |
---|
311 | image::output_fini () |
---|
312 | { |
---|
313 | output_init_fini ("Fini", fini_sections); |
---|
314 | } |
---|
315 | |
---|
316 | void |
---|
317 | image::output_init_fini (const char* label, const char** names) |
---|
318 | { |
---|
319 | /* |
---|
320 | * Load the sections. |
---|
321 | */ |
---|
322 | sections ifsecs; |
---|
323 | std::for_each (secs.begin (), secs.end (), |
---|
324 | section_loader (*this, ifsecs, names)); |
---|
325 | |
---|
326 | std::cout << label << " sections: " << ifsecs.size () << std::endl; |
---|
327 | |
---|
328 | for (sections::iterator ii = ifsecs.begin (); |
---|
329 | ii != ifsecs.end (); |
---|
330 | ++ii) |
---|
331 | { |
---|
332 | section& sec = *ii; |
---|
333 | const size_t machine_size = sizeof (uint32_t); |
---|
334 | const int count = sec.data.level () / machine_size; |
---|
335 | |
---|
336 | std::cout << " " << sec.sec.name << std::endl; |
---|
337 | |
---|
338 | for (int i = 0; i < count; ++i) |
---|
339 | { |
---|
340 | uint32_t address; |
---|
341 | symbols::symbol* sym; |
---|
342 | sec.data >> address; |
---|
343 | sym = addresses[address]; |
---|
344 | std::cout << " " |
---|
345 | << std::hex << std::setfill ('0') |
---|
346 | << "0x" << std::setw (8) << address |
---|
347 | << std::dec << std::setfill ('0'); |
---|
348 | if (sym) |
---|
349 | { |
---|
350 | /* |
---|
351 | * C++ is adding '_GLOBAL__sub_I_' to the label. If present, strip |
---|
352 | * and check the label. |
---|
353 | */ |
---|
354 | std::string label = sym->name (); |
---|
355 | if (rld::starts_with (label, "_GLOBAL__sub_I_")) |
---|
356 | label = rld::find_replace (label, "_GLOBAL__sub_I_", ""); |
---|
357 | if (rld::symbols::is_cplusplus (label)) |
---|
358 | rld::symbols::demangle_name (label, label); |
---|
359 | std::cout << " " << label; |
---|
360 | } |
---|
361 | else |
---|
362 | { |
---|
363 | std::cout << " no symbol"; |
---|
364 | } |
---|
365 | std::cout << std::endl; |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | std::cout << std::endl; |
---|
370 | } |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | /** |
---|
375 | * RTEMS Exe Info options. This needs to be rewritten to be like cc where only |
---|
376 | * a single '-' and long options is present. |
---|
377 | */ |
---|
378 | static struct option rld_opts[] = { |
---|
379 | { "help", no_argument, NULL, 'h' }, |
---|
380 | { "version", no_argument, NULL, 'V' }, |
---|
381 | { "verbose", no_argument, NULL, 'v' }, |
---|
382 | { "map", no_argument, NULL, 'M' }, |
---|
383 | { "all", no_argument, NULL, 'a' }, |
---|
384 | { "sections", no_argument, NULL, 'S' }, |
---|
385 | { "init", no_argument, NULL, 'I' }, |
---|
386 | { "fini", no_argument, NULL, 'F' }, |
---|
387 | { NULL, 0, NULL, 0 } |
---|
388 | }; |
---|
389 | |
---|
390 | void |
---|
391 | usage (int exit_code) |
---|
392 | { |
---|
393 | std::cout << "rtems-exeinfo [options] objects" << std::endl |
---|
394 | << "Options and arguments:" << std::endl |
---|
395 | << " -h : help (also --help)" << std::endl |
---|
396 | << " -V : print linker version number and exit (also --version)" << std::endl |
---|
397 | << " -v : verbose (trace import parts), can supply multiple times" << std::endl |
---|
398 | << " to increase verbosity (also --verbose)" << std::endl |
---|
399 | << " -M : generate map output (also --map)" << std::endl |
---|
400 | << " -a : all output excluding the map (also --all)" << std::endl |
---|
401 | << " -S : show all section (also --sections)" << std::endl |
---|
402 | << " -I : show init section tables (also --init)" << std::endl |
---|
403 | << " -F : show fini section tables (also --fini)" << std::endl; |
---|
404 | ::exit (exit_code); |
---|
405 | } |
---|
406 | |
---|
407 | static void |
---|
408 | fatal_signal (int signum) |
---|
409 | { |
---|
410 | signal (signum, SIG_DFL); |
---|
411 | |
---|
412 | rld::process::temporaries_clean_up (); |
---|
413 | |
---|
414 | /* |
---|
415 | * Get the same signal again, this time not handled, so its normal effect |
---|
416 | * occurs. |
---|
417 | */ |
---|
418 | kill (getpid (), signum); |
---|
419 | } |
---|
420 | |
---|
421 | static void |
---|
422 | setup_signals (void) |
---|
423 | { |
---|
424 | if (signal (SIGINT, SIG_IGN) != SIG_IGN) |
---|
425 | signal (SIGINT, fatal_signal); |
---|
426 | #ifdef SIGHUP |
---|
427 | if (signal (SIGHUP, SIG_IGN) != SIG_IGN) |
---|
428 | signal (SIGHUP, fatal_signal); |
---|
429 | #endif |
---|
430 | if (signal (SIGTERM, SIG_IGN) != SIG_IGN) |
---|
431 | signal (SIGTERM, fatal_signal); |
---|
432 | #ifdef SIGPIPE |
---|
433 | if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) |
---|
434 | signal (SIGPIPE, fatal_signal); |
---|
435 | #endif |
---|
436 | #ifdef SIGCHLD |
---|
437 | signal (SIGCHLD, SIG_DFL); |
---|
438 | #endif |
---|
439 | } |
---|
440 | |
---|
441 | int |
---|
442 | main (int argc, char* argv[]) |
---|
443 | { |
---|
444 | int ec = 0; |
---|
445 | |
---|
446 | setup_signals (); |
---|
447 | |
---|
448 | try |
---|
449 | { |
---|
450 | std::string exe_name; |
---|
451 | bool map = false; |
---|
452 | bool all = false; |
---|
453 | bool sections = false; |
---|
454 | bool init = false; |
---|
455 | bool fini = false; |
---|
456 | |
---|
457 | rld::set_cmdline (argc, argv); |
---|
458 | |
---|
459 | while (true) |
---|
460 | { |
---|
461 | int opt = ::getopt_long (argc, argv, "hvVMaSIF", rld_opts, NULL); |
---|
462 | if (opt < 0) |
---|
463 | break; |
---|
464 | |
---|
465 | switch (opt) |
---|
466 | { |
---|
467 | case 'V': |
---|
468 | std::cout << "rtems-exeinfo (RTEMS Executable Info) " << rld::version () |
---|
469 | << ", RTEMS revision " << rld::rtems::version () |
---|
470 | << std::endl; |
---|
471 | ::exit (0); |
---|
472 | break; |
---|
473 | |
---|
474 | case 'v': |
---|
475 | rld::verbose_inc (); |
---|
476 | break; |
---|
477 | |
---|
478 | case 'M': |
---|
479 | map = true; |
---|
480 | break; |
---|
481 | |
---|
482 | case 'a': |
---|
483 | all = true; |
---|
484 | break; |
---|
485 | |
---|
486 | case 'I': |
---|
487 | init = true; |
---|
488 | break; |
---|
489 | |
---|
490 | case 'F': |
---|
491 | fini = true; |
---|
492 | break; |
---|
493 | |
---|
494 | case 'S': |
---|
495 | sections = true; |
---|
496 | break; |
---|
497 | |
---|
498 | case '?': |
---|
499 | usage (3); |
---|
500 | break; |
---|
501 | |
---|
502 | case 'h': |
---|
503 | usage (0); |
---|
504 | break; |
---|
505 | } |
---|
506 | } |
---|
507 | |
---|
508 | /* |
---|
509 | * Set the program name. |
---|
510 | */ |
---|
511 | rld::set_progname (argv[0]); |
---|
512 | |
---|
513 | argc -= optind; |
---|
514 | argv += optind; |
---|
515 | |
---|
516 | std::cout << "RTEMS Executable Info " << rld::version () << std::endl; |
---|
517 | std::cout << " " << rld::get_cmdline () << std::endl; |
---|
518 | |
---|
519 | /* |
---|
520 | * All means all types of output. |
---|
521 | */ |
---|
522 | if (all) |
---|
523 | { |
---|
524 | sections = true; |
---|
525 | init = true; |
---|
526 | fini = true; |
---|
527 | } |
---|
528 | |
---|
529 | /* |
---|
530 | * If there is no executable there is nothing to convert. |
---|
531 | */ |
---|
532 | if (argc == 0) |
---|
533 | throw rld::error ("no executable", "options"); |
---|
534 | if (argc > 1) |
---|
535 | throw rld::error ("only a single executable", "options"); |
---|
536 | |
---|
537 | /* |
---|
538 | * The name of the executable. |
---|
539 | */ |
---|
540 | exe_name = *argv; |
---|
541 | |
---|
542 | if (rld::verbose ()) |
---|
543 | std::cout << "exe-image: " << exe_name << std::endl; |
---|
544 | |
---|
545 | /* |
---|
546 | * Open the executable and read the symbols. |
---|
547 | */ |
---|
548 | rld::exeinfo::image exe (exe_name); |
---|
549 | |
---|
550 | std::cout << "exe: " << exe.exe.name ().full () << std::endl; |
---|
551 | |
---|
552 | /* |
---|
553 | * Generate the output. |
---|
554 | */ |
---|
555 | if (sections) |
---|
556 | exe.output_sections (); |
---|
557 | if (init) |
---|
558 | exe.output_init (); |
---|
559 | if (fini) |
---|
560 | exe.output_fini (); |
---|
561 | |
---|
562 | /* |
---|
563 | * Map ? |
---|
564 | */ |
---|
565 | if (map) |
---|
566 | rld::symbols::output (std::cout, exe.symbols); |
---|
567 | } |
---|
568 | catch (rld::error re) |
---|
569 | { |
---|
570 | std::cerr << "error: " |
---|
571 | << re.where << ": " << re.what |
---|
572 | << std::endl; |
---|
573 | ec = 10; |
---|
574 | } |
---|
575 | catch (std::exception e) |
---|
576 | { |
---|
577 | int status; |
---|
578 | char* realname; |
---|
579 | realname = abi::__cxa_demangle (e.what(), 0, 0, &status); |
---|
580 | std::cerr << "error: exception: " << realname << " ["; |
---|
581 | ::free (realname); |
---|
582 | const std::type_info &ti = typeid (e); |
---|
583 | realname = abi::__cxa_demangle (ti.name(), 0, 0, &status); |
---|
584 | std::cerr << realname << "] " << e.what () << std::endl << std::flush; |
---|
585 | ::free (realname); |
---|
586 | ec = 11; |
---|
587 | } |
---|
588 | catch (...) |
---|
589 | { |
---|
590 | /* |
---|
591 | * Helps to know if this happens. |
---|
592 | */ |
---|
593 | std::cerr << "error: unhandled exception" << std::endl; |
---|
594 | ec = 12; |
---|
595 | } |
---|
596 | |
---|
597 | return ec; |
---|
598 | } |
---|