incremental-dump.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // incremental.cc -- incremental linking test/debug tool
  2. // Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. // Written by Rafael Avila de Espindola <rafael.espindola@gmail.com>
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. // This file is a (still incomplete) test/debug tool that should display
  18. // all information available in the incremental linking sections in a
  19. // format that is easy to read.
  20. // Once the format is a bit more stable, this should probably be moved to
  21. // readelf. Because of that, the use of gold's data structures and functions
  22. // is just a short term convenience and not a design decision.
  23. #include "gold.h"
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <time.h>
  27. #include "incremental.h"
  28. namespace gold
  29. {
  30. class Output_file;
  31. }
  32. using namespace gold;
  33. template<int size, bool big_endian>
  34. static typename Incremental_inputs_reader<size, big_endian>::
  35. Incremental_input_entry_reader
  36. find_input_containing_global(
  37. Incremental_inputs_reader<size, big_endian>& incremental_inputs,
  38. unsigned int offset,
  39. unsigned int* symndx)
  40. {
  41. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  42. static const unsigned int global_sym_entry_size =
  43. Incremental_inputs_reader<size, big_endian>::global_sym_entry_size;
  44. for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
  45. {
  46. typename Inputs_reader::Incremental_input_entry_reader input_file =
  47. incremental_inputs.input_file(i);
  48. if (input_file.type() != INCREMENTAL_INPUT_OBJECT
  49. && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
  50. continue;
  51. unsigned int nsyms = input_file.get_global_symbol_count();
  52. if (offset >= input_file.get_symbol_offset(0)
  53. && offset < input_file.get_symbol_offset(nsyms))
  54. {
  55. *symndx = ((offset - input_file.get_symbol_offset(0))
  56. / global_sym_entry_size);
  57. return input_file;
  58. }
  59. }
  60. gold_unreachable();
  61. }
  62. template<int size, bool big_endian>
  63. static void
  64. dump_incremental_inputs(const char* argv0, const char* filename,
  65. Sized_incremental_binary<size, big_endian>* inc)
  66. {
  67. typedef Incremental_binary::Location Location;
  68. typedef Incremental_binary::View View;
  69. typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
  70. typedef typename Inputs_reader::Incremental_input_entry_reader Entry_reader;
  71. if (!inc->has_incremental_info())
  72. {
  73. fprintf(stderr, "%s: %s: no .gnu_incremental_inputs section\n", argv0,
  74. filename);
  75. exit(1);
  76. }
  77. // Create a reader object for the .gnu_incremental_inputs section.
  78. Incremental_inputs_reader<size, big_endian>
  79. incremental_inputs(inc->inputs_reader());
  80. if (incremental_inputs.version() != 2)
  81. {
  82. fprintf(stderr, "%s: %s: unknown incremental version %d\n", argv0,
  83. filename, incremental_inputs.version());
  84. exit(1);
  85. }
  86. const char* command_line = incremental_inputs.command_line();
  87. if (command_line == NULL)
  88. {
  89. fprintf(stderr,
  90. "%s: %s: failed to get link command line\n",
  91. argv0, filename);
  92. exit(1);
  93. }
  94. printf("Link command line: %s\n", command_line);
  95. printf("\nInput files:\n");
  96. for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
  97. {
  98. Entry_reader input_file = incremental_inputs.input_file(i);
  99. const char* objname = input_file.filename();
  100. if (objname == NULL)
  101. {
  102. fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
  103. argv0, filename, i);
  104. exit(1);
  105. }
  106. printf("[%d] %s\n", i, objname);
  107. Timespec mtime = input_file.get_mtime();
  108. printf(" Timestamp: %llu.%09d %s",
  109. static_cast<unsigned long long>(mtime.seconds),
  110. mtime.nanoseconds,
  111. ctime(&mtime.seconds));
  112. printf(" Serial Number: %d\n", input_file.arg_serial());
  113. printf(" In System Directory: %s\n",
  114. input_file.is_in_system_directory() ? "true" : "false");
  115. Incremental_input_type input_type = input_file.type();
  116. printf(" Type: ");
  117. switch (input_type)
  118. {
  119. case INCREMENTAL_INPUT_OBJECT:
  120. case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
  121. printf("%s\n", (input_type == INCREMENTAL_INPUT_OBJECT
  122. ? "Object" : "Archive member"));
  123. printf(" Input section count: %d\n",
  124. input_file.get_input_section_count());
  125. printf(" Global symbol count: %d\n",
  126. input_file.get_global_symbol_count());
  127. printf(" Local symbol offset: %d\n",
  128. input_file.get_local_symbol_offset());
  129. printf(" Local symbol count: %d\n",
  130. input_file.get_local_symbol_count());
  131. printf(" First dynamic reloc: %d\n",
  132. input_file.get_first_dyn_reloc());
  133. printf(" Dynamic reloc count: %d\n",
  134. input_file.get_dyn_reloc_count());
  135. printf(" COMDAT group count: %d\n",
  136. input_file.get_comdat_group_count());
  137. break;
  138. case INCREMENTAL_INPUT_ARCHIVE:
  139. printf("Archive\n");
  140. printf(" Member count: %d\n", input_file.get_member_count());
  141. printf(" Unused symbol count: %d\n",
  142. input_file.get_unused_symbol_count());
  143. break;
  144. case INCREMENTAL_INPUT_SHARED_LIBRARY:
  145. printf("Shared library\n");
  146. printf(" As needed: %s\n",
  147. input_file.as_needed() ? "true" : "false");
  148. printf(" soname: %s\n",
  149. input_file.get_soname());
  150. printf(" Symbol count: %d\n",
  151. input_file.get_global_symbol_count());
  152. break;
  153. case INCREMENTAL_INPUT_SCRIPT:
  154. printf("Linker script\n");
  155. printf(" Object count: %d\n", input_file.get_object_count());
  156. break;
  157. default:
  158. fprintf(stderr, "%s: invalid file type for object %u: %d\n",
  159. argv0, i, input_type);
  160. exit(1);
  161. }
  162. }
  163. printf("\nInput sections:\n");
  164. for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
  165. {
  166. Entry_reader input_file(incremental_inputs.input_file(i));
  167. if (input_file.type() != INCREMENTAL_INPUT_OBJECT
  168. && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
  169. continue;
  170. const char* objname = input_file.filename();
  171. if (objname == NULL)
  172. {
  173. fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
  174. argv0, filename, i);
  175. exit(1);
  176. }
  177. printf("[%d] %s\n", i, objname);
  178. printf(" %3s %6s %8s %8s %s\n",
  179. "n", "outndx", "offset", "size", "name");
  180. unsigned int nsections = input_file.get_input_section_count();
  181. for (unsigned int shndx = 0; shndx < nsections; ++shndx)
  182. {
  183. typename Entry_reader::Input_section_info info(
  184. input_file.get_input_section(shndx));
  185. printf(" %3d %6d %8lld %8lld %s\n", shndx + 1,
  186. info.output_shndx,
  187. static_cast<long long>(info.sh_offset),
  188. static_cast<long long>(info.sh_size),
  189. info.name);
  190. }
  191. unsigned int ncomdat = input_file.get_comdat_group_count();
  192. for (unsigned int i = 0; i < ncomdat; ++i)
  193. printf(" Comdat group: %s\n",
  194. input_file.get_comdat_group_signature(i));
  195. }
  196. // Get a view of the .symtab section.
  197. elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file(inc);
  198. unsigned int symtab_shndx = elf_file.find_section_by_type(elfcpp::SHT_SYMTAB);
  199. if (symtab_shndx == elfcpp::SHN_UNDEF) // Not found.
  200. {
  201. fprintf(stderr, "%s: %s: no symbol table section\n", argv0, filename);
  202. exit(1);
  203. }
  204. Location symtab_location(elf_file.section_contents(symtab_shndx));
  205. View symtab_view(inc->view(symtab_location));
  206. // Get a view of the .strtab section.
  207. unsigned int strtab_shndx = elf_file.section_link(symtab_shndx);
  208. if (strtab_shndx == elfcpp::SHN_UNDEF
  209. || strtab_shndx > elf_file.shnum()
  210. || elf_file.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
  211. {
  212. fprintf(stderr, "%s: %s: no string table section\n", argv0, filename);
  213. exit(1);
  214. }
  215. Location strtab_location(elf_file.section_contents(strtab_shndx));
  216. View strtab_view(inc->view(strtab_location));
  217. elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
  218. // The .gnu_incremental_symtab section contains entries that parallel
  219. // the global symbols of the main symbol table. The sh_info field
  220. // of the main symbol table's section header tells us how many global
  221. // symbols there are, but that count does not include any global
  222. // symbols that were forced local during the link. Therefore, we
  223. // use the size of the .gnu_incremental_symtab section to deduce
  224. // the number of global symbols + forced-local symbols there are
  225. // in the symbol table.
  226. Incremental_symtab_reader<big_endian> isymtab(inc->symtab_reader());
  227. Incremental_relocs_reader<size, big_endian> irelocs(inc->relocs_reader());
  228. unsigned int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  229. unsigned int nsyms = symtab_location.data_size / sym_size;
  230. unsigned int nglobals = isymtab.symbol_count();
  231. unsigned int first_global = nsyms - nglobals;
  232. unsigned const char* sym_p;
  233. printf("\nGlobal symbols per input file:\n");
  234. for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
  235. {
  236. Entry_reader input_file(incremental_inputs.input_file(i));
  237. if (input_file.type() != INCREMENTAL_INPUT_OBJECT
  238. && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
  239. && input_file.type() != INCREMENTAL_INPUT_SHARED_LIBRARY)
  240. continue;
  241. const char* objname = input_file.filename();
  242. if (objname == NULL)
  243. {
  244. fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
  245. argv0, filename, i);
  246. exit(1);
  247. }
  248. printf("[%d] %s\n", i, objname);
  249. unsigned int nsyms = input_file.get_global_symbol_count();
  250. if (nsyms > 0)
  251. printf(" %6s %6s %8s %8s %8s %8s\n",
  252. "outndx", "shndx", "offset", "chain", "#relocs", "rbase");
  253. if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
  254. {
  255. for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
  256. {
  257. bool is_def;
  258. bool is_copy;
  259. unsigned int output_symndx =
  260. input_file.get_output_symbol_index(symndx, &is_def, &is_copy);
  261. sym_p = symtab_view.data() + output_symndx * sym_size;
  262. elfcpp::Sym<size, big_endian> sym(sym_p);
  263. const char* symname;
  264. if (!strtab.get_c_string(sym.get_st_name(), &symname))
  265. symname = "<unknown>";
  266. printf(" %6d %6s %8s %8s %8s %8s %-5s %s\n",
  267. output_symndx,
  268. "", "", "", "", "",
  269. is_copy ? "COPY" : (is_def ? "DEF" : "UNDEF"),
  270. symname);
  271. }
  272. }
  273. else
  274. {
  275. for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
  276. {
  277. Incremental_global_symbol_reader<big_endian> info(
  278. input_file.get_global_symbol_reader(symndx));
  279. unsigned int output_symndx = info.output_symndx();
  280. sym_p = symtab_view.data() + output_symndx * sym_size;
  281. elfcpp::Sym<size, big_endian> sym(sym_p);
  282. const char* symname;
  283. if (!strtab.get_c_string(sym.get_st_name(), &symname))
  284. symname = "<unknown>";
  285. printf(" %6d %6d %8d %8d %8d %8d %-5s %s\n",
  286. output_symndx,
  287. info.shndx() == -1U ? -1 : info.shndx(),
  288. input_file.get_symbol_offset(symndx),
  289. info.next_offset(),
  290. info.reloc_count(),
  291. info.reloc_offset(),
  292. (info.shndx() == -1U
  293. ? "BASE"
  294. : info.shndx() == 0 ? "UNDEF" : "DEF"),
  295. symname);
  296. }
  297. }
  298. }
  299. sym_p = symtab_view.data() + first_global * sym_size;
  300. printf("\nGlobal symbol table:\n");
  301. for (unsigned int i = 0; i < nglobals; i++)
  302. {
  303. elfcpp::Sym<size, big_endian> sym(sym_p);
  304. const char* symname;
  305. if (!strtab.get_c_string(sym.get_st_name(), &symname))
  306. symname = "<unknown>";
  307. printf("[%d] %s\n", first_global + i, symname);
  308. unsigned int offset = isymtab.get_list_head(i);
  309. while (offset > 0)
  310. {
  311. unsigned int sym_ndx;
  312. Entry_reader input_file =
  313. find_input_containing_global<size, big_endian>(incremental_inputs,
  314. offset, &sym_ndx);
  315. Incremental_global_symbol_reader<big_endian> sym_info(
  316. input_file.get_global_symbol_reader(sym_ndx));
  317. printf(" %s (first reloc: %d, reloc count: %d)",
  318. input_file.filename(), sym_info.reloc_offset(),
  319. sym_info.reloc_count());
  320. if (sym_info.output_symndx() != first_global + i)
  321. printf(" ** wrong output symndx (%d) **", sym_info.output_symndx());
  322. printf("\n");
  323. // Dump the relocations from this input file for this symbol.
  324. unsigned int r_off = sym_info.reloc_offset();
  325. for (unsigned int j = 0; j < sym_info.reloc_count(); j++)
  326. {
  327. printf(" %4d relocation type %3d shndx %2d"
  328. " offset %016llx addend %016llx %s\n",
  329. r_off,
  330. irelocs.get_r_type(r_off),
  331. irelocs.get_r_shndx(r_off),
  332. static_cast<long long>(irelocs.get_r_offset(r_off)),
  333. static_cast<long long>(irelocs.get_r_addend(r_off)),
  334. symname);
  335. r_off += irelocs.reloc_size;
  336. }
  337. offset = sym_info.next_offset();
  338. }
  339. sym_p += sym_size;
  340. }
  341. Incremental_got_plt_reader<big_endian> igot_plt(inc->got_plt_reader());
  342. unsigned int ngot = igot_plt.get_got_entry_count();
  343. unsigned int nplt = igot_plt.get_plt_entry_count();
  344. printf("\nGOT entries:\n");
  345. for (unsigned int i = 0; i < ngot; ++i)
  346. {
  347. unsigned int got_type = igot_plt.get_got_type(i);
  348. unsigned int got_symndx = igot_plt.get_got_symndx(i);
  349. unsigned int got_input_index = igot_plt.get_got_input_index(i);
  350. printf("[%d] type %02x, ", i, got_type & 0x7f);
  351. if ((got_type & 0x7f) == 0x7f)
  352. printf("reserved");
  353. else if (got_type & 0x80)
  354. {
  355. Entry_reader input_file =
  356. incremental_inputs.input_file(got_input_index);
  357. const char* objname = input_file.filename();
  358. printf("local: %s (%d)", objname, got_symndx);
  359. }
  360. else
  361. {
  362. sym_p = symtab_view.data() + got_symndx * sym_size;
  363. elfcpp::Sym<size, big_endian> sym(sym_p);
  364. const char* symname;
  365. if (!strtab.get_c_string(sym.get_st_name(), &symname))
  366. symname = "<unknown>";
  367. printf("global %s (%d)", symname, got_symndx);
  368. }
  369. printf("\n");
  370. }
  371. printf("\nPLT entries:\n");
  372. for (unsigned int i = 0; i < nplt; ++i)
  373. {
  374. unsigned int plt_desc = igot_plt.get_plt_desc(i);
  375. printf("[%d] ", i);
  376. sym_p = symtab_view.data() + plt_desc * sym_size;
  377. elfcpp::Sym<size, big_endian> sym(sym_p);
  378. const char* symname;
  379. if (!strtab.get_c_string(sym.get_st_name(), &symname))
  380. symname = "<unknown>";
  381. printf("%s (%d)\n", symname, plt_desc);
  382. }
  383. printf("\nUnused archive symbols:\n");
  384. for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
  385. {
  386. Entry_reader input_file(incremental_inputs.input_file(i));
  387. if (input_file.type() != INCREMENTAL_INPUT_ARCHIVE)
  388. continue;
  389. const char* objname = input_file.filename();
  390. if (objname == NULL)
  391. {
  392. fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
  393. argv0, filename, i);
  394. exit(1);
  395. }
  396. printf("[%d] %s\n", i, objname);
  397. unsigned int nsyms = input_file.get_unused_symbol_count();
  398. for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
  399. printf(" %s\n", input_file.get_unused_symbol(symndx));
  400. }
  401. }
  402. int
  403. main(int argc, char** argv)
  404. {
  405. if (argc != 2)
  406. {
  407. fprintf(stderr, "Usage: %s <file>\n", argv[0]);
  408. return 1;
  409. }
  410. const char* filename = argv[1];
  411. Output_file* file = new Output_file(filename);
  412. bool t = file->open_base_file(NULL, false);
  413. if (!t)
  414. {
  415. fprintf(stderr, "%s: open_base_file(%s): %s\n", argv[0], filename,
  416. strerror(errno));
  417. return 1;
  418. }
  419. Incremental_binary* inc = open_incremental_binary(file);
  420. if (inc == NULL)
  421. {
  422. fprintf(stderr, "%s: open_incremental_binary(%s): %s\n", argv[0],
  423. filename, strerror(errno));
  424. return 1;
  425. }
  426. switch (parameters->size_and_endianness())
  427. {
  428. #ifdef HAVE_TARGET_32_LITTLE
  429. case Parameters::TARGET_32_LITTLE:
  430. dump_incremental_inputs<32, false>(
  431. argv[0], filename,
  432. static_cast<Sized_incremental_binary<32, false>*>(inc));
  433. break;
  434. #endif
  435. #ifdef HAVE_TARGET_32_BIG
  436. case Parameters::TARGET_32_BIG:
  437. dump_incremental_inputs<32, true>(
  438. argv[0], filename,
  439. static_cast<Sized_incremental_binary<32, true>*>(inc));
  440. break;
  441. #endif
  442. #ifdef HAVE_TARGET_64_LITTLE
  443. case Parameters::TARGET_64_LITTLE:
  444. dump_incremental_inputs<64, false>(
  445. argv[0], filename,
  446. static_cast<Sized_incremental_binary<64, false>*>(inc));
  447. break;
  448. #endif
  449. #ifdef HAVE_TARGET_64_BIG
  450. case Parameters::TARGET_64_BIG:
  451. dump_incremental_inputs<64, true>(
  452. argv[0], filename,
  453. static_cast<Sized_incremental_binary<64, true>*>(inc));
  454. break;
  455. #endif
  456. default:
  457. gold_unreachable();
  458. }
  459. return 0;
  460. }