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 | #include <rtems-utils.h> |
---|
51 | |
---|
52 | #ifndef HAVE_KILL |
---|
53 | #define kill(p,s) raise(s) |
---|
54 | #endif |
---|
55 | |
---|
56 | namespace rld |
---|
57 | { |
---|
58 | namespace exeinfo |
---|
59 | { |
---|
60 | /** |
---|
61 | * Default section list. |
---|
62 | */ |
---|
63 | const char* default_init[] = |
---|
64 | { |
---|
65 | ".rtemsroset", |
---|
66 | ".ctors", |
---|
67 | ".init", |
---|
68 | 0 |
---|
69 | }; |
---|
70 | |
---|
71 | const char* default_fini[] = |
---|
72 | { |
---|
73 | ".dtors", |
---|
74 | ".fini", |
---|
75 | 0 |
---|
76 | }; |
---|
77 | |
---|
78 | /** |
---|
79 | * ARM section list. |
---|
80 | */ |
---|
81 | const char* arm_init[] = |
---|
82 | { |
---|
83 | ".rtemsroset", |
---|
84 | ".init_array", |
---|
85 | 0 |
---|
86 | }; |
---|
87 | |
---|
88 | const char* arm_fini[] = |
---|
89 | { |
---|
90 | ".fini_array", |
---|
91 | 0 |
---|
92 | }; |
---|
93 | |
---|
94 | /** |
---|
95 | * An executable section's address, offset, size and alignment. |
---|
96 | */ |
---|
97 | struct section |
---|
98 | { |
---|
99 | const files::section& sec; //< The executable's section. |
---|
100 | buffer::buffer data; //< The section's data. |
---|
101 | files::byteorder byteorder; //< The image's byteorder. |
---|
102 | |
---|
103 | /** |
---|
104 | * Construct the section. |
---|
105 | */ |
---|
106 | section (const files::section& sec, files::byteorder byteorder); |
---|
107 | |
---|
108 | /** |
---|
109 | * Copy construct. |
---|
110 | */ |
---|
111 | section (const section& orig); |
---|
112 | |
---|
113 | /** |
---|
114 | * Clean up the section's memory. |
---|
115 | */ |
---|
116 | ~section (); |
---|
117 | |
---|
118 | private: |
---|
119 | /** |
---|
120 | * Default constructor. |
---|
121 | */ |
---|
122 | section (); |
---|
123 | }; |
---|
124 | |
---|
125 | /** |
---|
126 | * Container of sections. Order is the address in memory. |
---|
127 | */ |
---|
128 | typedef std::list < section > sections; |
---|
129 | |
---|
130 | /** |
---|
131 | * The kernel image. |
---|
132 | */ |
---|
133 | struct image |
---|
134 | { |
---|
135 | files::object exe; //< The object file that is the executable. |
---|
136 | dwarf::file debug; //< The executable's DWARF details. |
---|
137 | symbols::table symbols; //< The synbols for a map. |
---|
138 | symbols::addrtab addresses; //< The symbols keyed by address. |
---|
139 | files::sections secs; //< The sections in the executable. |
---|
140 | const char** init; //< The init section's list for the machinetype. |
---|
141 | const char** fini; //< The fini section's list for the machinetype. |
---|
142 | |
---|
143 | |
---|
144 | /** |
---|
145 | * Load the executable file. |
---|
146 | */ |
---|
147 | image (const std::string exe_name, bool load_functions); |
---|
148 | |
---|
149 | /** |
---|
150 | * Clean up. |
---|
151 | */ |
---|
152 | ~image (); |
---|
153 | |
---|
154 | /* |
---|
155 | * Check the compiler and flags match. |
---|
156 | */ |
---|
157 | void output_compilation_unit (bool objects, bool full_flags); |
---|
158 | |
---|
159 | /* |
---|
160 | * Output the sections. |
---|
161 | */ |
---|
162 | void output_sections (); |
---|
163 | |
---|
164 | /* |
---|
165 | * Output the init sections. |
---|
166 | */ |
---|
167 | void output_init (); |
---|
168 | |
---|
169 | /* |
---|
170 | * Output the fini sections. |
---|
171 | */ |
---|
172 | void output_fini (); |
---|
173 | |
---|
174 | /* |
---|
175 | * Output init/fini worker. |
---|
176 | */ |
---|
177 | void output_init_fini (const char* label, const char** names); |
---|
178 | |
---|
179 | /* |
---|
180 | * Output the configuration. |
---|
181 | */ |
---|
182 | void output_config (); |
---|
183 | |
---|
184 | /* |
---|
185 | * Output the TLS data. |
---|
186 | */ |
---|
187 | void output_tls (); |
---|
188 | |
---|
189 | /* |
---|
190 | * Output the inlined functions. |
---|
191 | */ |
---|
192 | void output_inlined (); |
---|
193 | |
---|
194 | /* |
---|
195 | * Output the DWARF data. |
---|
196 | */ |
---|
197 | void output_dwarf (); |
---|
198 | |
---|
199 | private: |
---|
200 | |
---|
201 | void config (const std::string name); |
---|
202 | }; |
---|
203 | |
---|
204 | section::section (const files::section& sec, files::byteorder byteorder) |
---|
205 | : sec (sec), |
---|
206 | data (sec.size, byteorder == rld::files::little_endian), |
---|
207 | byteorder (byteorder) |
---|
208 | { |
---|
209 | } |
---|
210 | |
---|
211 | section::section (const section& orig) |
---|
212 | : sec (orig.sec), |
---|
213 | data (orig.data), |
---|
214 | byteorder (orig.byteorder) |
---|
215 | { |
---|
216 | } |
---|
217 | |
---|
218 | section::~section () |
---|
219 | { |
---|
220 | } |
---|
221 | |
---|
222 | /** |
---|
223 | * Helper for for_each to filter and load the sections we wish to |
---|
224 | * dump. |
---|
225 | */ |
---|
226 | class section_loader: |
---|
227 | public std::unary_function < const files::section, void > |
---|
228 | { |
---|
229 | public: |
---|
230 | |
---|
231 | section_loader (image& img, sections& secs, const char* names[]); |
---|
232 | |
---|
233 | ~section_loader (); |
---|
234 | |
---|
235 | void operator () (const files::section& fsec); |
---|
236 | |
---|
237 | private: |
---|
238 | |
---|
239 | image& img; |
---|
240 | sections& secs; |
---|
241 | const char** names; |
---|
242 | }; |
---|
243 | |
---|
244 | section_loader::section_loader (image& img, |
---|
245 | sections& secs, |
---|
246 | const char* names[]) |
---|
247 | : img (img), |
---|
248 | secs (secs), |
---|
249 | names (names) |
---|
250 | { |
---|
251 | } |
---|
252 | |
---|
253 | section_loader::~section_loader () |
---|
254 | { |
---|
255 | } |
---|
256 | |
---|
257 | void |
---|
258 | section_loader::operator () (const files::section& fsec) |
---|
259 | { |
---|
260 | if (rld::verbose () >= RLD_VERBOSE_DETAILS) |
---|
261 | std::cout << "init:section-loader: " << fsec.name |
---|
262 | << " address=" << std::hex << fsec.address << std::dec |
---|
263 | << " relocs=" << fsec.relocs.size () |
---|
264 | << " fsec.size=" << fsec.size |
---|
265 | << " fsec.alignment=" << fsec.alignment |
---|
266 | << " fsec.rela=" << fsec.rela |
---|
267 | << std::endl; |
---|
268 | |
---|
269 | for (int n = 0; names[n] != 0; ++n) |
---|
270 | { |
---|
271 | if (fsec.name == names[n]) |
---|
272 | { |
---|
273 | if (rld::verbose () >= RLD_VERBOSE_DETAILS) |
---|
274 | std::cout << "init:section-loader: " << fsec.name |
---|
275 | << " added" << std::endl; |
---|
276 | |
---|
277 | section sec (fsec, img.exe.get_byteorder ()); |
---|
278 | img.exe.seek (fsec.offset); |
---|
279 | sec.data.read (img.exe, fsec.size); |
---|
280 | secs.push_back (sec); |
---|
281 | break; |
---|
282 | } |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | image::image (const std::string exe_name, bool load_functions) |
---|
287 | : exe (exe_name), |
---|
288 | init (0), |
---|
289 | fini (0) |
---|
290 | { |
---|
291 | /* |
---|
292 | * Open the executable file and begin the session on it. |
---|
293 | */ |
---|
294 | exe.open (); |
---|
295 | exe.begin (); |
---|
296 | debug.begin (exe.elf ()); |
---|
297 | |
---|
298 | if (!exe.valid ()) |
---|
299 | throw rld::error ("Not valid: " + exe.name ().full (), |
---|
300 | "init::image"); |
---|
301 | |
---|
302 | /* |
---|
303 | * Set up the section lists for the machiner type. |
---|
304 | */ |
---|
305 | switch (exe.elf ().machinetype ()) |
---|
306 | { |
---|
307 | case EM_ARM: |
---|
308 | init = arm_init; |
---|
309 | fini = arm_fini; |
---|
310 | break; |
---|
311 | default: |
---|
312 | init = default_init; |
---|
313 | fini = default_fini; |
---|
314 | break; |
---|
315 | } |
---|
316 | |
---|
317 | /* |
---|
318 | * Load the symbols and sections. |
---|
319 | */ |
---|
320 | exe.load_symbols (symbols, true); |
---|
321 | debug.load_debug (); |
---|
322 | debug.load_types (); |
---|
323 | debug.load_variables (); |
---|
324 | if (load_functions) |
---|
325 | { |
---|
326 | std::cout << "May take a while ..." << std::endl; |
---|
327 | debug.load_functions (); |
---|
328 | } |
---|
329 | symbols.globals (addresses); |
---|
330 | symbols.weaks (addresses); |
---|
331 | symbols.locals (addresses); |
---|
332 | exe.get_sections (secs); |
---|
333 | } |
---|
334 | |
---|
335 | image::~image () |
---|
336 | { |
---|
337 | } |
---|
338 | |
---|
339 | void |
---|
340 | image::output_compilation_unit (bool objects, bool full_flags) |
---|
341 | { |
---|
342 | dwarf::compilation_units& cus = debug.get_cus (); |
---|
343 | |
---|
344 | std::cout << "Compilation: " << std::endl; |
---|
345 | |
---|
346 | rld::strings flag_exceptions = { "-O", |
---|
347 | "-g", |
---|
348 | "-mtune=", |
---|
349 | "-fno-builtin", |
---|
350 | "-fno-inline", |
---|
351 | "-fexceptions", |
---|
352 | "-fnon-call-exceptions", |
---|
353 | "-fvisibility=", |
---|
354 | "-fno-stack-protector", |
---|
355 | "-fbuilding-libgcc", |
---|
356 | "-fno-implicit-templates", |
---|
357 | "-fimplicit-templates", |
---|
358 | "-ffunction-sections", |
---|
359 | "-fdata-sections", |
---|
360 | "-frandom-seed=", |
---|
361 | "-fno-common", |
---|
362 | "-fno-keep-inline-functions" }; |
---|
363 | |
---|
364 | dwarf::producer_sources producers; |
---|
365 | |
---|
366 | debug.get_producer_sources (producers); |
---|
367 | |
---|
368 | /* |
---|
369 | * Find which flags are common to the building of all source. We are only |
---|
370 | * interested in files that have any flags. This filters out things like |
---|
371 | * the assembler which does not have flags. |
---|
372 | */ |
---|
373 | |
---|
374 | rld::strings all_flags; |
---|
375 | ::rtems::utils::ostream_guard old_state( std::cout ); |
---|
376 | |
---|
377 | size_t source_max = 0; |
---|
378 | |
---|
379 | for (auto& p : producers) |
---|
380 | { |
---|
381 | dwarf::source_flags_compare compare; |
---|
382 | std::sort (p.sources.begin (), p.sources.end (), compare); |
---|
383 | |
---|
384 | for (auto& s : p.sources) |
---|
385 | { |
---|
386 | size_t len = rld::path::basename (s.source).length (); |
---|
387 | if (len > source_max) |
---|
388 | source_max = len; |
---|
389 | |
---|
390 | if (!s.flags.empty ()) |
---|
391 | { |
---|
392 | for (auto& f : s.flags) |
---|
393 | { |
---|
394 | bool add = true; |
---|
395 | for (auto& ef : flag_exceptions) |
---|
396 | { |
---|
397 | if (rld::starts_with (f, ef)) |
---|
398 | { |
---|
399 | add = false; |
---|
400 | break; |
---|
401 | } |
---|
402 | } |
---|
403 | if (add) |
---|
404 | { |
---|
405 | for (auto& af : all_flags) |
---|
406 | { |
---|
407 | if (f == af) |
---|
408 | { |
---|
409 | add = false; |
---|
410 | break; |
---|
411 | } |
---|
412 | } |
---|
413 | if (add) |
---|
414 | all_flags.push_back (f); |
---|
415 | } |
---|
416 | } |
---|
417 | } |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | rld::strings common_flags; |
---|
422 | |
---|
423 | for (auto& flag : all_flags) |
---|
424 | { |
---|
425 | bool found_in_all = true; |
---|
426 | for (auto& p : producers) |
---|
427 | { |
---|
428 | for (auto& s : p.sources) |
---|
429 | { |
---|
430 | if (!s.flags.empty ()) |
---|
431 | { |
---|
432 | bool flag_found = false; |
---|
433 | for (auto& f : s.flags) |
---|
434 | { |
---|
435 | if (flag == f) |
---|
436 | { |
---|
437 | flag_found = true; |
---|
438 | break; |
---|
439 | } |
---|
440 | } |
---|
441 | if (!flag_found) |
---|
442 | { |
---|
443 | found_in_all = false; |
---|
444 | break; |
---|
445 | } |
---|
446 | } |
---|
447 | if (!found_in_all) |
---|
448 | break; |
---|
449 | } |
---|
450 | } |
---|
451 | if (found_in_all) |
---|
452 | common_flags.push_back (flag); |
---|
453 | } |
---|
454 | |
---|
455 | std::cout << " Producers: " << producers.size () << std::endl; |
---|
456 | |
---|
457 | for (auto& p : producers) |
---|
458 | { |
---|
459 | std::cout << " | " << p.producer |
---|
460 | << ": " << p.sources.size () << " objects" << std::endl; |
---|
461 | } |
---|
462 | |
---|
463 | std::cout << " Common flags: " << common_flags.size () << std::endl |
---|
464 | << " |"; |
---|
465 | |
---|
466 | for (auto& f : common_flags) |
---|
467 | std::cout << ' ' << f; |
---|
468 | std::cout << std::endl; |
---|
469 | |
---|
470 | if (objects) |
---|
471 | { |
---|
472 | std::cout << " Object files: " << cus.size () << std::endl; |
---|
473 | |
---|
474 | rld::strings filter_flags = common_flags; |
---|
475 | filter_flags.insert (filter_flags.end (), |
---|
476 | flag_exceptions.begin (), |
---|
477 | flag_exceptions.end()); |
---|
478 | |
---|
479 | for (auto& p : producers) |
---|
480 | { |
---|
481 | std::cout << ' ' << p.producer |
---|
482 | << ": " << p.sources.size () << " objects" << std::endl; |
---|
483 | for (auto& s : p.sources) |
---|
484 | { |
---|
485 | std::cout << " | " |
---|
486 | << std::setw (source_max + 1) << std::left |
---|
487 | << rld::path::basename (s.source); |
---|
488 | if (!s.flags.empty ()) |
---|
489 | { |
---|
490 | bool first = true; |
---|
491 | for (auto& f : s.flags) |
---|
492 | { |
---|
493 | bool present = false; |
---|
494 | if (!full_flags) |
---|
495 | { |
---|
496 | for (auto& ff : filter_flags) |
---|
497 | { |
---|
498 | if (rld::starts_with(f, ff)) |
---|
499 | { |
---|
500 | present = true; |
---|
501 | break; |
---|
502 | } |
---|
503 | } |
---|
504 | } |
---|
505 | if (!present) |
---|
506 | { |
---|
507 | if (first) |
---|
508 | { |
---|
509 | std::cout << ':'; |
---|
510 | first = false; |
---|
511 | } |
---|
512 | std::cout << ' ' << f; |
---|
513 | } |
---|
514 | } |
---|
515 | } |
---|
516 | std::cout << std::endl; |
---|
517 | } |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | std::cout << std::endl; |
---|
522 | } |
---|
523 | |
---|
524 | void |
---|
525 | image::output_sections () |
---|
526 | { |
---|
527 | std::cout << "Sections: " << secs.size () << std::endl; |
---|
528 | |
---|
529 | size_t max_section_name = 0; |
---|
530 | |
---|
531 | for (files::sections::const_iterator si = secs.begin (); |
---|
532 | si != secs.end (); |
---|
533 | ++si) |
---|
534 | { |
---|
535 | const files::section& sec = *si; |
---|
536 | if (sec.name.length() > max_section_name) |
---|
537 | max_section_name = sec.name.length(); |
---|
538 | } |
---|
539 | |
---|
540 | for (files::sections::const_iterator si = secs.begin (); |
---|
541 | si != secs.end (); |
---|
542 | ++si) |
---|
543 | { |
---|
544 | const files::section& sec = *si; |
---|
545 | |
---|
546 | #define SF(f, i, c) if (sec.flags & (f)) flags[i] = c |
---|
547 | |
---|
548 | std::string flags ("--------------"); |
---|
549 | |
---|
550 | SF (SHF_WRITE, 0, 'W'); |
---|
551 | SF (SHF_ALLOC, 1, 'A'); |
---|
552 | SF (SHF_EXECINSTR, 2, 'E'); |
---|
553 | SF (SHF_MERGE, 3, 'M'); |
---|
554 | SF (SHF_STRINGS, 4, 'S'); |
---|
555 | SF (SHF_INFO_LINK, 5, 'I'); |
---|
556 | SF (SHF_LINK_ORDER, 6, 'L'); |
---|
557 | SF (SHF_OS_NONCONFORMING, 7, 'N'); |
---|
558 | SF (SHF_GROUP, 8, 'G'); |
---|
559 | SF (SHF_TLS, 9, 'T'); |
---|
560 | SF (SHF_AMD64_LARGE, 10, 'a'); |
---|
561 | SF (SHF_ENTRYSECT, 11, 'e'); |
---|
562 | SF (SHF_COMDEF, 12, 'c'); |
---|
563 | SF (SHF_ORDERED, 13, 'O'); |
---|
564 | |
---|
565 | std::cout << " " << std::left |
---|
566 | << std::setw (max_section_name) << sec.name |
---|
567 | << " " << flags |
---|
568 | << std::right << std::hex << std::setfill ('0') |
---|
569 | << " addr: 0x" << std::setw (8) << sec.address |
---|
570 | << " 0x" << std::setw (8) << sec.address + sec.size |
---|
571 | << std::dec << std::setfill (' ') |
---|
572 | << " size: " << std::setw (10) << sec.size |
---|
573 | << " align: " << std::setw (3) << sec.alignment |
---|
574 | << " relocs: " << std::setw (6) << sec.relocs.size () |
---|
575 | << std::endl; |
---|
576 | } |
---|
577 | |
---|
578 | std::cout << std::endl; |
---|
579 | } |
---|
580 | |
---|
581 | void |
---|
582 | image::output_init () |
---|
583 | { |
---|
584 | output_init_fini ("Init", init); |
---|
585 | } |
---|
586 | |
---|
587 | void |
---|
588 | image::output_fini () |
---|
589 | { |
---|
590 | output_init_fini ("Fini", fini); |
---|
591 | } |
---|
592 | |
---|
593 | void |
---|
594 | image::output_init_fini (const char* label, const char** names) |
---|
595 | { |
---|
596 | /* |
---|
597 | * Load the sections. |
---|
598 | */ |
---|
599 | sections ifsecs; |
---|
600 | std::for_each (secs.begin (), secs.end (), |
---|
601 | section_loader (*this, ifsecs, names)); |
---|
602 | |
---|
603 | std::cout << label << " sections: " << ifsecs.size () << std::endl; |
---|
604 | |
---|
605 | for (auto& sec : ifsecs) |
---|
606 | { |
---|
607 | const size_t machine_size = exe.elf ().machine_size (); |
---|
608 | const int count = sec.data.level () / machine_size; |
---|
609 | |
---|
610 | std::cout << " " << sec.sec.name << std::endl; |
---|
611 | |
---|
612 | for (int i = 0; i < count; ++i) |
---|
613 | { |
---|
614 | uint32_t address; |
---|
615 | symbols::symbol* sym; |
---|
616 | sec.data >> address; |
---|
617 | if (address != 0) |
---|
618 | { |
---|
619 | sym = addresses[address]; |
---|
620 | std::cout << " " |
---|
621 | << std::hex << std::setfill ('0') |
---|
622 | << "0x" << std::setw (8) << address |
---|
623 | << std::dec << std::setfill ('0'); |
---|
624 | if (sym) |
---|
625 | { |
---|
626 | std::string label = sym->name (); |
---|
627 | if (rld::symbols::is_cplusplus (label)) |
---|
628 | rld::symbols::demangle_name (label, label); |
---|
629 | std::cout << " " << label; |
---|
630 | } |
---|
631 | else |
---|
632 | { |
---|
633 | std::cout << " no symbol (maybe static to a module)"; |
---|
634 | } |
---|
635 | std::cout << std::endl; |
---|
636 | } |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | std::cout << std::endl; |
---|
641 | } |
---|
642 | |
---|
643 | void image::output_tls () |
---|
644 | { |
---|
645 | ::rtems::utils::ostream_guard old_state( std::cout ); |
---|
646 | |
---|
647 | symbols::symbol* tls_data_begin = symbols.find_global("_TLS_Data_begin"); |
---|
648 | symbols::symbol* tls_data_end = symbols.find_global("_TLS_Data_end"); |
---|
649 | symbols::symbol* tls_data_size = symbols.find_global("_TLS_Data_size"); |
---|
650 | symbols::symbol* tls_bss_begin = symbols.find_global("_TLS_BSS_begin"); |
---|
651 | symbols::symbol* tls_bss_end = symbols.find_global("_TLS_BSS_end"); |
---|
652 | symbols::symbol* tls_bss_size = symbols.find_global("_TLS_BSS_size"); |
---|
653 | symbols::symbol* tls_size = symbols.find_global("_TLS_Size"); |
---|
654 | symbols::symbol* tls_alignment = symbols.find_global("_TLS_Alignment"); |
---|
655 | symbols::symbol* tls_max_size = symbols.find_global("_Thread_Maximum_TLS_size"); |
---|
656 | |
---|
657 | if (tls_data_begin == nullptr || |
---|
658 | tls_data_end == nullptr || |
---|
659 | tls_data_size == nullptr || |
---|
660 | tls_bss_begin == nullptr || |
---|
661 | tls_bss_end == nullptr || |
---|
662 | tls_bss_size == nullptr || |
---|
663 | tls_size == nullptr || |
---|
664 | tls_alignment == nullptr) |
---|
665 | { |
---|
666 | if (tls_data_begin == nullptr && |
---|
667 | tls_data_end == nullptr && |
---|
668 | tls_data_size == nullptr && |
---|
669 | tls_bss_begin == nullptr && |
---|
670 | tls_bss_end == nullptr && |
---|
671 | tls_bss_size == nullptr && |
---|
672 | tls_size == nullptr && |
---|
673 | tls_alignment == nullptr) |
---|
674 | { |
---|
675 | std::cout << "No TLS data found" << std::endl; |
---|
676 | return; |
---|
677 | } |
---|
678 | std::cout << "TLS environment is INVALID (please report):" << std::endl |
---|
679 | << " _TLS_Data_begin : " |
---|
680 | << (char*) (tls_data_begin == nullptr ? "not-found" : "found") |
---|
681 | << std::endl |
---|
682 | << " _TLS_Data_end : " |
---|
683 | << (char*) (tls_data_end == nullptr ? "not-found" : "found") |
---|
684 | << std::endl |
---|
685 | << " _TLS_Data_size : " |
---|
686 | << (char*) (tls_data_size == nullptr ? "not-found" : "found") |
---|
687 | << std::endl |
---|
688 | << " _TLS_BSS_begin : " |
---|
689 | << (char*) (tls_bss_begin == nullptr ? "not-found" : "found") |
---|
690 | << std::endl |
---|
691 | << " _TLS_BSS_end : " |
---|
692 | << (char*) (tls_bss_end == nullptr ? "not-found" : "found") |
---|
693 | << std::endl |
---|
694 | << " _TLS_BSS_Size : " |
---|
695 | << (char*) (tls_bss_size == nullptr ? "not-found" : "found") |
---|
696 | << std::endl |
---|
697 | << " _TLS_Size : " |
---|
698 | << (char*) (tls_size == nullptr ? "not-found" : "found") |
---|
699 | << std::endl |
---|
700 | << " _TLS_Alignment : " |
---|
701 | << (char*) (tls_alignment == nullptr ? "not-found" : "found") |
---|
702 | << std::endl |
---|
703 | << " _Thread_Maximum_TLS_size : " |
---|
704 | << (char*) (tls_max_size == nullptr ? "not-found" : "found") |
---|
705 | << std::endl |
---|
706 | << std::endl; |
---|
707 | return; |
---|
708 | } |
---|
709 | |
---|
710 | std::cout << "TLS size : " << tls_size->value () << std::endl |
---|
711 | << " max size : "; |
---|
712 | if (tls_max_size == nullptr) |
---|
713 | std::cout << "not found" << std::endl; |
---|
714 | else |
---|
715 | std::cout << tls_max_size->value () << std::endl; |
---|
716 | std::cout << " data size : " << tls_data_size->value () << std::endl |
---|
717 | << " bss size : " << tls_bss_size->value () << std::endl |
---|
718 | << " alignment : " << tls_alignment->value () << std::endl |
---|
719 | << std::right << std::hex << std::setfill ('0') |
---|
720 | << " data addr : 0x" << std::setw (8) << tls_data_begin->value () |
---|
721 | << std::endl |
---|
722 | << std::dec << std::setfill (' ') |
---|
723 | << std::endl; |
---|
724 | } |
---|
725 | |
---|
726 | void image::config(const std::string name) |
---|
727 | { |
---|
728 | std::string table_name = "_" + name + "_Information"; |
---|
729 | symbols::symbol* table = symbols.find_global(table_name); |
---|
730 | |
---|
731 | if (table != nullptr) |
---|
732 | std::cout << " " << name << std::endl; |
---|
733 | } |
---|
734 | |
---|
735 | void image::output_config() |
---|
736 | { |
---|
737 | std::cout << "Configurations:" << std::endl; |
---|
738 | config("Thread"); |
---|
739 | config("Barrier"); |
---|
740 | config("Extension"); |
---|
741 | config("Message_queue"); |
---|
742 | config("Partition"); |
---|
743 | config("Rate_monotonic"); |
---|
744 | config("Dual_ported_memory"); |
---|
745 | config("Region"); |
---|
746 | config("Semaphore"); |
---|
747 | config("Timer"); |
---|
748 | config("RTEMS_tasks"); |
---|
749 | } |
---|
750 | |
---|
751 | struct func_count |
---|
752 | { |
---|
753 | std::string name; |
---|
754 | int count; |
---|
755 | size_t size; |
---|
756 | |
---|
757 | func_count (std::string name, size_t size) |
---|
758 | : name (name), |
---|
759 | count (1), |
---|
760 | size (size) { |
---|
761 | } |
---|
762 | }; |
---|
763 | typedef std::vector < func_count > func_counts; |
---|
764 | |
---|
765 | void image::output_inlined () |
---|
766 | { |
---|
767 | size_t total = 0; |
---|
768 | size_t total_size = 0; |
---|
769 | size_t inlined_size = 0; |
---|
770 | double percentage; |
---|
771 | double percentage_size; |
---|
772 | dwarf::functions funcs_inlined; |
---|
773 | dwarf::functions funcs_not_inlined; |
---|
774 | func_counts counts; |
---|
775 | |
---|
776 | for (auto& cu : debug.get_cus ()) |
---|
777 | { |
---|
778 | for (auto& f : cu.get_functions ()) |
---|
779 | { |
---|
780 | if (f.size () > 0 && f.has_machine_code ()) |
---|
781 | { |
---|
782 | bool counted; |
---|
783 | ++total; |
---|
784 | total_size += f.size (); |
---|
785 | switch (f.get_inlined ()) |
---|
786 | { |
---|
787 | case dwarf::function::inl_inline: |
---|
788 | case dwarf::function::inl_declared_inlined: |
---|
789 | inlined_size += f.size (); |
---|
790 | counted = false; |
---|
791 | for (auto& c : counts) |
---|
792 | { |
---|
793 | if (c.name == f.name ()) |
---|
794 | { |
---|
795 | ++c.count; |
---|
796 | c.size += f.size (); |
---|
797 | counted = true; |
---|
798 | break; |
---|
799 | } |
---|
800 | } |
---|
801 | if (!counted) |
---|
802 | counts.push_back (func_count (f.name (), f.size ())); |
---|
803 | funcs_inlined.push_back (f); |
---|
804 | break; |
---|
805 | case dwarf::function::inl_declared_not_inlined: |
---|
806 | funcs_not_inlined.push_back (f); |
---|
807 | break; |
---|
808 | default: |
---|
809 | break; |
---|
810 | } |
---|
811 | } |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | if ( total == 0 ) { |
---|
816 | percentage = 0; |
---|
817 | } else { |
---|
818 | percentage = (double) ( funcs_inlined.size() * 100 ) / total; |
---|
819 | } |
---|
820 | |
---|
821 | if ( total_size == 0 ) { |
---|
822 | percentage_size = 0; |
---|
823 | } else { |
---|
824 | percentage_size = (double) ( inlined_size * 100 ) / total_size; |
---|
825 | } |
---|
826 | |
---|
827 | std::cout << "inlined funcs : " << funcs_inlined.size () << std::endl |
---|
828 | << " total funcs : " << total << std::endl |
---|
829 | << " % inline funcs : " << percentage << '%' << std::endl |
---|
830 | << " total size : " << total_size << std::endl |
---|
831 | << " inline size : " << inlined_size << std::endl |
---|
832 | << " % inline size : " << percentage_size << '%' << std::endl; |
---|
833 | |
---|
834 | auto count_compare = [](func_count const & a, func_count const & b) { |
---|
835 | return a.size != b.size? a.size < b.size : a.count > b.count; |
---|
836 | }; |
---|
837 | std::sort (counts.begin (), counts.end (), count_compare); |
---|
838 | std::reverse (counts.begin (), counts.end ()); |
---|
839 | |
---|
840 | std::cout << std::endl << "inlined repeats : " << std::endl; |
---|
841 | for (auto& c : counts) |
---|
842 | if (c.count > 1) |
---|
843 | std::cout << std::setw (6) << c.size << ' ' |
---|
844 | << std::setw (4) << c.count << ' ' |
---|
845 | << c.name << std::endl; |
---|
846 | |
---|
847 | dwarf::function_compare compare (dwarf::function_compare::fc_by_size); |
---|
848 | |
---|
849 | std::sort (funcs_inlined.begin (), funcs_inlined.end (), compare); |
---|
850 | std::reverse (funcs_inlined.begin (), funcs_inlined.end ()); |
---|
851 | |
---|
852 | std::cout << std::endl << "inline funcs : " << std::endl; |
---|
853 | for (auto& f : funcs_inlined) |
---|
854 | { |
---|
855 | std::string flags; |
---|
856 | |
---|
857 | std::cout << std::setw (6) << f.size () << ' ' |
---|
858 | << (char) (f.is_external () ? 'E' : ' ') |
---|
859 | << (char) (f.get_inlined () == dwarf::function::inl_inline ? 'C' : ' ') |
---|
860 | << std::hex << std::setfill ('0') |
---|
861 | << " 0x" << std::setw (8) << f.pc_low () |
---|
862 | << std::dec << std::setfill (' ') |
---|
863 | << ' ' << f.name () |
---|
864 | << std::endl; |
---|
865 | } |
---|
866 | |
---|
867 | if (funcs_not_inlined.size () > 0) |
---|
868 | { |
---|
869 | std::sort (funcs_not_inlined.begin (), funcs_not_inlined.end (), compare); |
---|
870 | std::reverse (funcs_not_inlined.begin (), funcs_not_inlined.end ()); |
---|
871 | |
---|
872 | std::cout << std::endl << "inline funcs not inlined: " << std::endl; |
---|
873 | for (auto& f : funcs_not_inlined) |
---|
874 | { |
---|
875 | std::cout << std::setw (6) << f.size () << ' ' |
---|
876 | << (char) (f.is_external () ? 'E' : ' ') |
---|
877 | << (char) (f.get_inlined () == dwarf::function::inl_inline ? 'C' : ' ') |
---|
878 | << std::hex << std::setfill ('0') |
---|
879 | << " 0x" << std::setw (8) << f.pc_low () |
---|
880 | << std::dec << std::setfill (' ') |
---|
881 | << ' ' << f.name () |
---|
882 | << std::endl; |
---|
883 | } |
---|
884 | } |
---|
885 | } |
---|
886 | |
---|
887 | void image::output_dwarf () |
---|
888 | { |
---|
889 | std::cout << "DWARF Data:" << std::endl; |
---|
890 | debug.dump (std::cout); |
---|
891 | } |
---|
892 | } |
---|
893 | } |
---|
894 | |
---|
895 | /** |
---|
896 | * RTEMS Exe Info options. This needs to be rewritten to be like cc where only |
---|
897 | * a single '-' and long options is present. |
---|
898 | */ |
---|
899 | static struct option rld_opts[] = { |
---|
900 | { "help", no_argument, NULL, 'h' }, |
---|
901 | { "version", no_argument, NULL, 'V' }, |
---|
902 | { "verbose", no_argument, NULL, 'v' }, |
---|
903 | { "map", no_argument, NULL, 'M' }, |
---|
904 | { "all", no_argument, NULL, 'a' }, |
---|
905 | { "sections", no_argument, NULL, 'S' }, |
---|
906 | { "init", no_argument, NULL, 'I' }, |
---|
907 | { "fini", no_argument, NULL, 'F' }, |
---|
908 | { "objects", no_argument, NULL, 'O' }, |
---|
909 | { "full-flags", no_argument, NULL, 'A' }, |
---|
910 | { "config", no_argument, NULL, 'C' }, |
---|
911 | { "tls", no_argument, NULL, 'T' }, |
---|
912 | { "inlined", no_argument, NULL, 'i' }, |
---|
913 | { "dwarf", no_argument, NULL, 'D' }, |
---|
914 | { NULL, 0, NULL, 0 } |
---|
915 | }; |
---|
916 | |
---|
917 | void |
---|
918 | usage (int exit_code) |
---|
919 | { |
---|
920 | std::cout << "rtems-exeinfo [options] objects" << std::endl |
---|
921 | << "Options and arguments:" << std::endl |
---|
922 | << " -h : help (also --help)" << std::endl |
---|
923 | << " -V : print linker version number and exit (also --version)" << std::endl |
---|
924 | << " -v : verbose (trace import parts), can supply multiple times" << std::endl |
---|
925 | << " to increase verbosity (also --verbose)" << std::endl |
---|
926 | << " -M : generate map output (also --map)" << std::endl |
---|
927 | << " -a : all output excluding the map and DAWRF (also --all)" << std::endl |
---|
928 | << " -S : show all section (also --sections)" << std::endl |
---|
929 | << " -I : show init section tables (also --init)" << std::endl |
---|
930 | << " -F : show fini section tables (also --fini)" << std::endl |
---|
931 | << " -O : show object files (also --objects)" << std::endl |
---|
932 | << " : add --full-flags for compiler options" << std::endl |
---|
933 | << " -C : show configuration (also --config)" << std::endl |
---|
934 | << " -T : show thread local storage data (also --tls)" << std::endl |
---|
935 | << " -i : show inlined code (also --inlined)" << std::endl |
---|
936 | << " -D : dump the DWARF data (also --dwarf)" << std::endl; |
---|
937 | ::exit (exit_code); |
---|
938 | } |
---|
939 | |
---|
940 | static void |
---|
941 | fatal_signal (int signum) |
---|
942 | { |
---|
943 | signal (signum, SIG_DFL); |
---|
944 | |
---|
945 | rld::process::temporaries_clean_up (); |
---|
946 | |
---|
947 | /* |
---|
948 | * Get the same signal again, this time not handled, so its normal effect |
---|
949 | * occurs. |
---|
950 | */ |
---|
951 | kill (getpid (), signum); |
---|
952 | } |
---|
953 | |
---|
954 | static void |
---|
955 | setup_signals (void) |
---|
956 | { |
---|
957 | if (signal (SIGINT, SIG_IGN) != SIG_IGN) |
---|
958 | signal (SIGINT, fatal_signal); |
---|
959 | #ifdef SIGHUP |
---|
960 | if (signal (SIGHUP, SIG_IGN) != SIG_IGN) |
---|
961 | signal (SIGHUP, fatal_signal); |
---|
962 | #endif |
---|
963 | if (signal (SIGTERM, SIG_IGN) != SIG_IGN) |
---|
964 | signal (SIGTERM, fatal_signal); |
---|
965 | #ifdef SIGPIPE |
---|
966 | if (signal (SIGPIPE, SIG_IGN) != SIG_IGN) |
---|
967 | signal (SIGPIPE, fatal_signal); |
---|
968 | #endif |
---|
969 | #ifdef SIGCHLD |
---|
970 | signal (SIGCHLD, SIG_DFL); |
---|
971 | #endif |
---|
972 | } |
---|
973 | |
---|
974 | void |
---|
975 | unhandled_exception (void) |
---|
976 | { |
---|
977 | std::cerr << "error: exception handling error, please report" << std::endl; |
---|
978 | exit (1); |
---|
979 | } |
---|
980 | |
---|
981 | int |
---|
982 | main (int argc, char* argv[]) |
---|
983 | { |
---|
984 | int ec = 0; |
---|
985 | |
---|
986 | setup_signals (); |
---|
987 | |
---|
988 | std::set_terminate(unhandled_exception); |
---|
989 | |
---|
990 | try |
---|
991 | { |
---|
992 | std::string exe_name; |
---|
993 | bool map = false; |
---|
994 | bool all = false; |
---|
995 | bool sections = false; |
---|
996 | bool init = false; |
---|
997 | bool fini = false; |
---|
998 | bool objects = false; |
---|
999 | bool full_flags = false; |
---|
1000 | bool config = false; |
---|
1001 | bool tls = false; |
---|
1002 | bool inlined = false; |
---|
1003 | bool dwarf_data = false; |
---|
1004 | |
---|
1005 | rld::set_cmdline (argc, argv); |
---|
1006 | |
---|
1007 | while (true) |
---|
1008 | { |
---|
1009 | int opt = ::getopt_long (argc, argv, "hvVMaSIFOCTiD", rld_opts, NULL); |
---|
1010 | if (opt < 0) |
---|
1011 | break; |
---|
1012 | |
---|
1013 | switch (opt) |
---|
1014 | { |
---|
1015 | case 'V': |
---|
1016 | std::cout << "rtems-exeinfo (RTEMS Executable Info) " << rld::version () |
---|
1017 | << ", RTEMS revision " << rld::rtems::version () |
---|
1018 | << std::endl; |
---|
1019 | ::exit (0); |
---|
1020 | break; |
---|
1021 | |
---|
1022 | case 'v': |
---|
1023 | rld::verbose_inc (); |
---|
1024 | break; |
---|
1025 | |
---|
1026 | case 'M': |
---|
1027 | map = true; |
---|
1028 | break; |
---|
1029 | |
---|
1030 | case 'a': |
---|
1031 | all = true; |
---|
1032 | break; |
---|
1033 | |
---|
1034 | case 'I': |
---|
1035 | init = true; |
---|
1036 | break; |
---|
1037 | |
---|
1038 | case 'F': |
---|
1039 | fini = true; |
---|
1040 | break; |
---|
1041 | |
---|
1042 | case 'S': |
---|
1043 | sections = true; |
---|
1044 | break; |
---|
1045 | |
---|
1046 | case 'O': |
---|
1047 | objects = true; |
---|
1048 | break; |
---|
1049 | |
---|
1050 | case 'A': |
---|
1051 | full_flags = true; |
---|
1052 | break; |
---|
1053 | |
---|
1054 | case 'C': |
---|
1055 | config = true; |
---|
1056 | break; |
---|
1057 | |
---|
1058 | case 'T': |
---|
1059 | tls = true; |
---|
1060 | break; |
---|
1061 | |
---|
1062 | case 'i': |
---|
1063 | inlined = true; |
---|
1064 | break; |
---|
1065 | |
---|
1066 | case 'D': |
---|
1067 | dwarf_data = true; |
---|
1068 | break; |
---|
1069 | |
---|
1070 | case '?': |
---|
1071 | usage (3); |
---|
1072 | break; |
---|
1073 | |
---|
1074 | case 'h': |
---|
1075 | usage (0); |
---|
1076 | break; |
---|
1077 | } |
---|
1078 | } |
---|
1079 | |
---|
1080 | /* |
---|
1081 | * Set the program name. |
---|
1082 | */ |
---|
1083 | rld::set_progname (argv[0]); |
---|
1084 | |
---|
1085 | argc -= optind; |
---|
1086 | argv += optind; |
---|
1087 | |
---|
1088 | std::cout << "RTEMS Executable Info " << rld::version () << std::endl; |
---|
1089 | std::cout << " " << rld::get_cmdline () << std::endl; |
---|
1090 | |
---|
1091 | /* |
---|
1092 | * All means all types of output. |
---|
1093 | */ |
---|
1094 | if (all) |
---|
1095 | { |
---|
1096 | sections = true; |
---|
1097 | init = true; |
---|
1098 | fini = true; |
---|
1099 | objects = true; |
---|
1100 | config = true; |
---|
1101 | tls = true; |
---|
1102 | inlined = true; |
---|
1103 | } |
---|
1104 | |
---|
1105 | /* |
---|
1106 | * If there is no executable there is nothing to convert. |
---|
1107 | */ |
---|
1108 | if (argc == 0) |
---|
1109 | throw rld::error ("no executable", "options"); |
---|
1110 | if (argc > 1) |
---|
1111 | throw rld::error ("only a single executable", "options"); |
---|
1112 | |
---|
1113 | /* |
---|
1114 | * The name of the executable. |
---|
1115 | */ |
---|
1116 | exe_name = *argv; |
---|
1117 | |
---|
1118 | if (rld::verbose ()) |
---|
1119 | std::cout << "exe-image: " << exe_name << std::endl; |
---|
1120 | |
---|
1121 | /* |
---|
1122 | * Open the executable and read the symbols. |
---|
1123 | */ |
---|
1124 | rld::exeinfo::image exe (exe_name, inlined | dwarf_data); |
---|
1125 | |
---|
1126 | std::cout << "exe: " << exe.exe.name ().full () << std::endl |
---|
1127 | << std::endl; |
---|
1128 | |
---|
1129 | /* |
---|
1130 | * Generate the output. |
---|
1131 | */ |
---|
1132 | exe.output_compilation_unit (objects, full_flags); |
---|
1133 | if (sections) |
---|
1134 | exe.output_sections (); |
---|
1135 | if (init) |
---|
1136 | exe.output_init (); |
---|
1137 | if (fini) |
---|
1138 | exe.output_fini (); |
---|
1139 | if (config) |
---|
1140 | exe.output_config (); |
---|
1141 | if (tls) |
---|
1142 | exe.output_tls (); |
---|
1143 | if (inlined) |
---|
1144 | exe.output_inlined (); |
---|
1145 | if (dwarf_data) |
---|
1146 | exe.output_dwarf (); |
---|
1147 | |
---|
1148 | /* |
---|
1149 | * Map ? |
---|
1150 | */ |
---|
1151 | if (map) |
---|
1152 | rld::symbols::output (std::cout, exe.symbols); |
---|
1153 | } |
---|
1154 | catch (rld::error re) |
---|
1155 | { |
---|
1156 | std::cerr << "error: " |
---|
1157 | << re.where << ": " << re.what |
---|
1158 | << std::endl; |
---|
1159 | ec = 10; |
---|
1160 | } |
---|
1161 | catch (std::exception& e) |
---|
1162 | { |
---|
1163 | int status; |
---|
1164 | char* realname; |
---|
1165 | realname = abi::__cxa_demangle (e.what(), 0, 0, &status); |
---|
1166 | std::cerr << "error: exception: " << realname << " ["; |
---|
1167 | ::free (realname); |
---|
1168 | const std::type_info &ti = typeid (e); |
---|
1169 | realname = abi::__cxa_demangle (ti.name(), 0, 0, &status); |
---|
1170 | std::cerr << realname << "] " << e.what () << std::endl << std::flush; |
---|
1171 | ::free (realname); |
---|
1172 | ec = 11; |
---|
1173 | } |
---|
1174 | catch (...) |
---|
1175 | { |
---|
1176 | /* |
---|
1177 | * Helps to know if this happens. |
---|
1178 | */ |
---|
1179 | std::cerr << "error: unhandled exception" << std::endl; |
---|
1180 | ec = 12; |
---|
1181 | } |
---|
1182 | |
---|
1183 | return ec; |
---|
1184 | } |
---|