readelf.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/MappedFile.h>
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/StringView.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/File.h>
  12. #include <LibELF/DynamicLoader.h>
  13. #include <LibELF/DynamicObject.h>
  14. #include <LibELF/Image.h>
  15. #include <LibELF/Validation.h>
  16. #include <ctype.h>
  17. #include <fcntl.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. static const char* object_file_type_to_string(ElfW(Half) type)
  21. {
  22. switch (type) {
  23. case ET_NONE:
  24. return "None";
  25. case ET_REL:
  26. return "Relocatable";
  27. case ET_EXEC:
  28. return "Executable";
  29. case ET_DYN:
  30. return "Shared object";
  31. case ET_CORE:
  32. return "Core";
  33. default:
  34. return "(?)";
  35. }
  36. }
  37. static const char* object_machine_type_to_string(ElfW(Half) type)
  38. {
  39. switch (type) {
  40. case ET_NONE:
  41. return "None";
  42. case EM_M32:
  43. return "AT&T WE 32100";
  44. case EM_SPARC:
  45. return "SPARC";
  46. case EM_386:
  47. return "Intel 80386";
  48. case EM_68K:
  49. return "Motorola 68000";
  50. case EM_88K:
  51. return "Motorola 88000";
  52. case EM_486:
  53. return "Intel 80486";
  54. case EM_860:
  55. return "Intel 80860";
  56. case EM_MIPS:
  57. return "MIPS R3000 Big-Endian only";
  58. case EM_X86_64:
  59. return "Advanced Micro Devices X86-64";
  60. default:
  61. return "(?)";
  62. }
  63. }
  64. static const char* object_program_header_type_to_string(ElfW(Word) type)
  65. {
  66. switch (type) {
  67. case PT_NULL:
  68. return "NULL";
  69. case PT_LOAD:
  70. return "LOAD";
  71. case PT_DYNAMIC:
  72. return "DYNAMIC";
  73. case PT_INTERP:
  74. return "INTERP";
  75. case PT_NOTE:
  76. return "NOTE";
  77. case PT_SHLIB:
  78. return "SHLIB";
  79. case PT_PHDR:
  80. return "PHDR";
  81. case PT_TLS:
  82. return "TLS";
  83. case PT_LOOS:
  84. return "LOOS";
  85. case PT_HIOS:
  86. return "HIOS";
  87. case PT_LOPROC:
  88. return "LOPROC";
  89. case PT_HIPROC:
  90. return "HIPROC";
  91. case PT_GNU_EH_FRAME:
  92. return "GNU_EH_FRAME";
  93. case PT_GNU_RELRO:
  94. return "GNU_RELRO";
  95. case PT_GNU_STACK:
  96. return "GNU_STACK";
  97. case PT_OPENBSD_RANDOMIZE:
  98. return "OPENBSD_RANDOMIZE";
  99. case PT_OPENBSD_WXNEEDED:
  100. return "OPENBSD_WXNEEDED";
  101. case PT_OPENBSD_BOOTDATA:
  102. return "OPENBSD_BOOTDATA";
  103. default:
  104. return "(?)";
  105. }
  106. }
  107. static const char* object_section_header_type_to_string(ElfW(Word) type)
  108. {
  109. switch (type) {
  110. case SHT_NULL:
  111. return "NULL";
  112. case SHT_PROGBITS:
  113. return "PROGBITS";
  114. case SHT_SYMTAB:
  115. return "SYMTAB";
  116. case SHT_STRTAB:
  117. return "STRTAB";
  118. case SHT_RELA:
  119. return "RELA";
  120. case SHT_HASH:
  121. return "HASH";
  122. case SHT_DYNAMIC:
  123. return "DYNAMIC";
  124. case SHT_NOTE:
  125. return "NOTE";
  126. case SHT_NOBITS:
  127. return "NOBITS";
  128. case SHT_REL:
  129. return "REL";
  130. case SHT_SHLIB:
  131. return "SHLIB";
  132. case SHT_DYNSYM:
  133. return "DYNSYM";
  134. case SHT_NUM:
  135. return "NUM";
  136. case SHT_INIT_ARRAY:
  137. return "INIT_ARRAY";
  138. case SHT_FINI_ARRAY:
  139. return "FINI_ARRAY";
  140. case SHT_PREINIT_ARRAY:
  141. return "PREINIT_ARRAY";
  142. case SHT_GROUP:
  143. return "GROUP";
  144. case SHT_SYMTAB_SHNDX:
  145. return "SYMTAB_SHNDX";
  146. case SHT_LOOS:
  147. return "SOOS";
  148. case SHT_SUNW_dof:
  149. return "SUNW_dof";
  150. case SHT_GNU_LIBLIST:
  151. return "GNU_LIBLIST";
  152. case SHT_SUNW_move:
  153. return "SUNW_move";
  154. case SHT_SUNW_syminfo:
  155. return "SUNW_syminfo";
  156. case SHT_SUNW_verdef:
  157. return "SUNW_verdef";
  158. case SHT_SUNW_verneed:
  159. return "SUNW_verneed";
  160. case SHT_SUNW_versym: // or SHT_HIOS
  161. return "SUNW_versym";
  162. case SHT_LOPROC:
  163. return "LOPROC";
  164. case SHT_HIPROC:
  165. return "HIPROC";
  166. case SHT_LOUSER:
  167. return "LOUSER";
  168. case SHT_HIUSER:
  169. return "HIUSER";
  170. case SHT_GNU_HASH:
  171. return "GNU_HASH";
  172. default:
  173. return "(?)";
  174. }
  175. }
  176. static const char* object_symbol_type_to_string(ElfW(Word) type)
  177. {
  178. switch (type) {
  179. case STT_NOTYPE:
  180. return "NOTYPE";
  181. case STT_OBJECT:
  182. return "OBJECT";
  183. case STT_FUNC:
  184. return "FUNC";
  185. case STT_SECTION:
  186. return "SECTION";
  187. case STT_FILE:
  188. return "FILE";
  189. case STT_TLS:
  190. return "TLS";
  191. case STT_LOPROC:
  192. return "LOPROC";
  193. case STT_HIPROC:
  194. return "HIPROC";
  195. default:
  196. return "(?)";
  197. }
  198. }
  199. static const char* object_symbol_binding_to_string(ElfW(Word) type)
  200. {
  201. switch (type) {
  202. case STB_LOCAL:
  203. return "LOCAL";
  204. case STB_GLOBAL:
  205. return "GLOBAL";
  206. case STB_WEAK:
  207. return "WEAK";
  208. case STB_NUM:
  209. return "NUM";
  210. case STB_LOPROC:
  211. return "LOPROC";
  212. case STB_HIPROC:
  213. return "HIPROC";
  214. default:
  215. return "(?)";
  216. }
  217. }
  218. static const char* object_relocation_type_to_string(ElfW(Word) type)
  219. {
  220. switch (type) {
  221. #if ARCH(I386)
  222. case R_386_NONE:
  223. return "R_386_NONE";
  224. case R_386_32:
  225. return "R_386_32";
  226. case R_386_PC32:
  227. return "R_386_PC32";
  228. case R_386_GOT32:
  229. return "R_386_GOT32";
  230. case R_386_PLT32:
  231. return "R_386_PLT32";
  232. case R_386_COPY:
  233. return "R_386_COPY";
  234. case R_386_GLOB_DAT:
  235. return "R_386_GLOB_DAT";
  236. case R_386_JMP_SLOT:
  237. return "R_386_JMP_SLOT";
  238. case R_386_RELATIVE:
  239. return "R_386_RELATIVE";
  240. case R_386_TLS_TPOFF:
  241. return "R_386_TLS_TPOFF";
  242. case R_386_TLS_TPOFF32:
  243. return "R_386_TLS_TPOFF32";
  244. #else
  245. case R_X86_64_NONE:
  246. return "R_X86_64_NONE";
  247. case R_X86_64_64:
  248. return "R_X86_64";
  249. case R_X86_64_GLOB_DAT:
  250. return "R_x86_64_GLOB_DAT";
  251. case R_X86_64_JUMP_SLOT:
  252. return "R_X86_64_JUMP_SLOT";
  253. case R_X86_64_RELATIVE:
  254. return "R_X86_64_RELATIVE";
  255. case R_X86_64_TPOFF64:
  256. return "R_X86_64_TPOFF64";
  257. #endif
  258. default:
  259. return "(?)";
  260. }
  261. }
  262. int main(int argc, char** argv)
  263. {
  264. if (pledge("stdio rpath", nullptr) < 0) {
  265. perror("pledge");
  266. return 1;
  267. }
  268. const char* path;
  269. static bool display_all = false;
  270. static bool display_elf_header = false;
  271. static bool display_program_headers = false;
  272. static bool display_section_headers = false;
  273. static bool display_headers = false;
  274. static bool display_symbol_table = false;
  275. static bool display_dynamic_symbol_table = false;
  276. static bool display_core_notes = false;
  277. static bool display_relocations = false;
  278. static bool display_unwind_info = false;
  279. static bool display_dynamic_section = false;
  280. static bool display_hardening = false;
  281. Core::ArgsParser args_parser;
  282. args_parser.add_option(display_all, "Display all", "all", 'a');
  283. args_parser.add_option(display_elf_header, "Display ELF header", "file-header", 'h');
  284. args_parser.add_option(display_program_headers, "Display program headers", "program-headers", 'l');
  285. args_parser.add_option(display_section_headers, "Display section headers", "section-headers", 'S');
  286. args_parser.add_option(display_headers, "Equivalent to: -h -l -S -s -r -d -n -u -c", "headers", 'e');
  287. args_parser.add_option(display_symbol_table, "Display the symbol table", "syms", 's');
  288. args_parser.add_option(display_dynamic_symbol_table, "Display the dynamic symbol table", "dyn-syms", '\0');
  289. args_parser.add_option(display_dynamic_section, "Display the dynamic section", "dynamic", 'd');
  290. args_parser.add_option(display_core_notes, "Display core notes", "notes", 'n');
  291. args_parser.add_option(display_relocations, "Display relocations", "relocs", 'r');
  292. args_parser.add_option(display_unwind_info, "Display unwind info", "unwind", 'u');
  293. args_parser.add_option(display_hardening, "Display security hardening info", "checksec", 'c');
  294. args_parser.add_positional_argument(path, "ELF path", "path");
  295. args_parser.parse(argc, argv);
  296. if (argc < 3) {
  297. args_parser.print_usage(stderr, argv[0]);
  298. return -1;
  299. }
  300. if (display_headers) {
  301. display_elf_header = true;
  302. display_program_headers = true;
  303. display_section_headers = true;
  304. }
  305. if (display_all) {
  306. display_elf_header = true;
  307. display_program_headers = true;
  308. display_section_headers = true;
  309. display_dynamic_symbol_table = true;
  310. display_dynamic_section = true;
  311. display_core_notes = true;
  312. display_relocations = true;
  313. display_unwind_info = true;
  314. display_symbol_table = true;
  315. display_hardening = true;
  316. }
  317. auto file_or_error = MappedFile::map(path);
  318. if (file_or_error.is_error()) {
  319. warnln("Unable to map file {}: {}", path, file_or_error.error());
  320. return -1;
  321. }
  322. auto elf_image_data = file_or_error.value()->bytes();
  323. ELF::Image elf_image(elf_image_data);
  324. if (!elf_image.is_valid()) {
  325. warnln("File is not a valid ELF object");
  326. return -1;
  327. }
  328. String interpreter_path;
  329. if (!ELF::validate_program_headers(*(const ElfW(Ehdr)*)elf_image_data.data(), elf_image_data.size(), (const u8*)elf_image_data.data(), elf_image_data.size(), &interpreter_path)) {
  330. warnln("Invalid ELF headers");
  331. return -1;
  332. }
  333. auto& header = *reinterpret_cast<const ElfW(Ehdr)*>(elf_image_data.data());
  334. RefPtr<ELF::DynamicObject> object = nullptr;
  335. if (elf_image.is_dynamic()) {
  336. if (interpreter_path.is_null()) {
  337. interpreter_path = "/usr/lib/Loader.so";
  338. warnln("Warning: Dynamic ELF object has no interpreter path. Using: {}", interpreter_path);
  339. }
  340. auto interpreter_file_or_error = MappedFile::map(interpreter_path);
  341. if (interpreter_file_or_error.is_error()) {
  342. warnln("Unable to map interpreter file {}: {}", interpreter_path, interpreter_file_or_error.error());
  343. return -1;
  344. }
  345. auto interpreter_image_data = interpreter_file_or_error.value()->bytes();
  346. ELF::Image interpreter_image(interpreter_image_data);
  347. if (!interpreter_image.is_valid()) {
  348. warnln("ELF interpreter image is invalid");
  349. return -1;
  350. }
  351. int fd = open(path, O_RDONLY);
  352. if (fd < 0) {
  353. outln("Unable to open file {}", path);
  354. return 1;
  355. }
  356. auto result = ELF::DynamicLoader::try_create(fd, path);
  357. if (result.is_error()) {
  358. outln("{}", result.error().text);
  359. return 1;
  360. }
  361. auto& loader = result.value();
  362. if (!loader->is_valid()) {
  363. outln("{} is not a valid ELF dynamic shared object!", path);
  364. return 1;
  365. }
  366. object = loader->map();
  367. if (!object) {
  368. outln("Failed to map dynamic ELF object {}", path);
  369. return 1;
  370. }
  371. }
  372. if (display_elf_header) {
  373. outln("ELF header:");
  374. out(" Magic: ");
  375. for (char i : StringView { header.e_ident, sizeof(header.e_ident) }) {
  376. if (isprint(i)) {
  377. out("{:c} ", i);
  378. } else {
  379. out("{:02x} ", i);
  380. }
  381. }
  382. outln();
  383. outln(" Type: {} ({})", header.e_type, object_file_type_to_string(header.e_type));
  384. outln(" Machine: {} ({})", header.e_machine, object_machine_type_to_string(header.e_machine));
  385. outln(" Version: {:#x}", header.e_version);
  386. outln(" Entry point address: {:#x}", header.e_entry);
  387. outln(" Start of program headers: {} (bytes into file)", header.e_phoff);
  388. outln(" Start of section headers: {} (bytes into file)", header.e_shoff);
  389. outln(" Flags: {:#x}", header.e_flags);
  390. outln(" Size of this header: {} (bytes)", header.e_ehsize);
  391. outln(" Size of program headers: {} (bytes)", header.e_phentsize);
  392. outln(" Number of program headers: {}", header.e_phnum);
  393. outln(" Size of section headers: {} (bytes)", header.e_shentsize);
  394. outln(" Number of section headers: {}", header.e_shnum);
  395. outln(" Section header string table index: {}", header.e_shstrndx);
  396. outln();
  397. }
  398. #if ARCH(I386)
  399. auto addr_padding = "";
  400. #else
  401. auto addr_padding = " ";
  402. #endif
  403. if (display_section_headers) {
  404. if (!display_all) {
  405. outln("There are {} section headers, starting at offset {:#x}:", header.e_shnum, header.e_shoff);
  406. outln();
  407. }
  408. if (!elf_image.section_count()) {
  409. outln("There are no sections in this file.");
  410. } else {
  411. outln("Section Headers:");
  412. outln(" Name Type Address{} Offset{} Size{} Flags", addr_padding, addr_padding, addr_padding);
  413. elf_image.for_each_section([](const ELF::Image::Section& section) {
  414. out(" {:19} ", section.name());
  415. out("{:15} ", object_section_header_type_to_string(section.type()));
  416. out("{:p} ", section.address());
  417. out("{:p} ", section.offset());
  418. out("{:p} ", section.size());
  419. out("{}", section.flags());
  420. outln();
  421. });
  422. }
  423. outln();
  424. }
  425. if (display_program_headers) {
  426. if (!display_all) {
  427. outln("ELF file type is {} ({})", header.e_type, object_file_type_to_string(header.e_type));
  428. outln("Entry point {:#x}\n", header.e_entry);
  429. outln("There are {} program headers, starting at offset {}", header.e_phnum, header.e_phoff);
  430. outln();
  431. }
  432. if (!elf_image.program_header_count()) {
  433. outln("There are no program headers in this file.");
  434. } else {
  435. outln("Program Headers:");
  436. outln(" Type Offset{} VirtAddr{} PhysAddr{} FileSiz{} MemSiz{} Flg Align",
  437. addr_padding, addr_padding, addr_padding, addr_padding, addr_padding);
  438. elf_image.for_each_program_header([](const ELF::Image::ProgramHeader& program_header) {
  439. out(" ");
  440. out("{:14} ", object_program_header_type_to_string(program_header.type()));
  441. out("{:p} ", program_header.offset());
  442. out("{:p} ", program_header.vaddr().as_ptr());
  443. out("{:p} ", program_header.vaddr().as_ptr()); // FIXME: assumes PhysAddr = VirtAddr
  444. out("{:p} ", program_header.size_in_image());
  445. out("{:p} ", program_header.size_in_memory());
  446. out("{:04x} ", program_header.flags());
  447. out("{:p}", program_header.alignment());
  448. outln();
  449. if (program_header.type() == PT_INTERP)
  450. outln(" [Interpreter: {}]", program_header.raw_data());
  451. });
  452. }
  453. // TODO: Display section to segment mapping
  454. outln();
  455. }
  456. if (display_dynamic_section) {
  457. auto found_dynamic_section = false;
  458. if (elf_image.is_dynamic()) {
  459. elf_image.for_each_section([&found_dynamic_section](const ELF::Image::Section& section) {
  460. if (section.name() != ELF_DYNAMIC)
  461. return IterationDecision::Continue;
  462. found_dynamic_section = true;
  463. if (section.entry_count()) {
  464. outln("Dynamic section '{}' at offset {:#08x} contains {} entries.", section.name().to_string(), section.offset(), section.entry_count());
  465. } else {
  466. outln("Dynamic section '{}' at offset {:#08x} contains zero entries.", section.name().to_string(), section.offset());
  467. }
  468. return IterationDecision::Break;
  469. });
  470. Vector<String> libraries;
  471. object->for_each_needed_library([&libraries](StringView entry) {
  472. libraries.append(String::formatted("{}", entry));
  473. });
  474. auto library_index = 0;
  475. outln(" Tag Type Name / Value");
  476. object->for_each_dynamic_entry([&library_index, &libraries, &object](const ELF::DynamicObject::DynamicEntry& entry) {
  477. out(" {:#08x} ", entry.tag());
  478. out("{:17} ", ELF::DynamicObject::name_for_dtag(entry.tag()));
  479. if (entry.tag() == DT_NEEDED) {
  480. outln("Shared library: {}", libraries[library_index]);
  481. library_index++;
  482. } else if (entry.tag() == DT_RPATH) {
  483. outln("Library rpath: {}", object->rpath());
  484. } else if (entry.tag() == DT_RUNPATH) {
  485. outln("Library runpath: {}", object->runpath());
  486. } else if (entry.tag() == DT_SONAME) {
  487. outln("Library soname: {}", object->soname());
  488. } else {
  489. outln("{:#08x}", entry.val());
  490. }
  491. });
  492. }
  493. if (!found_dynamic_section)
  494. outln("No dynamic section in this file.");
  495. outln();
  496. }
  497. if (display_relocations) {
  498. if (elf_image.is_dynamic()) {
  499. if (!object->relocation_section().entry_count()) {
  500. outln("Relocation section '{}' at offset {:#08x} contains zero entries:", object->relocation_section().name(), object->relocation_section().offset());
  501. } else {
  502. outln("Relocation section '{}' at offset {:#08x} contains {} entries:", object->relocation_section().name(), object->relocation_section().offset(), object->relocation_section().entry_count());
  503. outln(" Offset{} Type Sym Value{} Sym Name", addr_padding, addr_padding);
  504. object->relocation_section().for_each_relocation([](const ELF::DynamicObject::Relocation& reloc) {
  505. out(" {:p} ", reloc.offset());
  506. out(" {:18} ", object_relocation_type_to_string(reloc.type()));
  507. out(" {:p} ", reloc.symbol().value());
  508. out(" {}", reloc.symbol().name());
  509. outln();
  510. });
  511. }
  512. outln();
  513. if (!object->plt_relocation_section().entry_count()) {
  514. outln("Relocation section '{}' at offset {:#08x} contains zero entries:", object->plt_relocation_section().name(), object->plt_relocation_section().offset());
  515. } else {
  516. outln("Relocation section '{}' at offset {:#08x} contains {} entries:", object->plt_relocation_section().name(), object->plt_relocation_section().offset(), object->plt_relocation_section().entry_count());
  517. outln(" Offset{} Type Sym Value{} Sym Name", addr_padding, addr_padding);
  518. object->plt_relocation_section().for_each_relocation([](const ELF::DynamicObject::Relocation& reloc) {
  519. out(" {:p} ", reloc.offset());
  520. out(" {:18} ", object_relocation_type_to_string(reloc.type()));
  521. out(" {:p} ", reloc.symbol().value());
  522. out(" {}", reloc.symbol().name());
  523. outln();
  524. });
  525. }
  526. } else {
  527. outln("No relocations in this file.");
  528. }
  529. outln();
  530. }
  531. if (display_unwind_info) {
  532. // TODO: Unwind info
  533. outln("Decoding of unwind sections for machine type {} is not supported.", object_machine_type_to_string(header.e_machine));
  534. outln();
  535. }
  536. if (display_core_notes) {
  537. auto found_notes = false;
  538. elf_image.for_each_program_header([&found_notes](const ELF::Image::ProgramHeader& program_header) {
  539. if (program_header.type() != PT_NOTE)
  540. return;
  541. found_notes = true;
  542. outln("Displaying notes section '{}' at offset {:#08x} of length {:#08x}:", object_program_header_type_to_string(program_header.type()), program_header.offset(), program_header.size_in_image());
  543. // FIXME: Parse CORE notes. Notes are in JSON format on SerenityOS, but vary between systems.
  544. outln("{}", program_header.raw_data());
  545. });
  546. if (!found_notes)
  547. outln("No core notes in this file.");
  548. outln();
  549. }
  550. if (display_dynamic_symbol_table || display_symbol_table) {
  551. auto found_dynamic_symbol_table = false;
  552. if (elf_image.is_dynamic()) {
  553. elf_image.for_each_section([&found_dynamic_symbol_table](const ELF::Image::Section& section) {
  554. if (section.name() != ELF_DYNSYM)
  555. return IterationDecision::Continue;
  556. found_dynamic_symbol_table = true;
  557. if (!section.entry_count()) {
  558. outln("Symbol table '{}' contains zero entries.", ELF_DYNSYM);
  559. } else {
  560. outln("Symbol table '{}' contains {} entries.", ELF_DYNSYM, section.entry_count());
  561. }
  562. return IterationDecision::Break;
  563. });
  564. if (object->symbol_count()) {
  565. // FIXME: Add support for init/fini/start/main sections
  566. outln(" Num: Value{} Size{} Type Bind Name", addr_padding, addr_padding);
  567. object->for_each_symbol([](const ELF::DynamicObject::Symbol& sym) {
  568. out(" {:>4}: ", sym.index());
  569. out("{:p} ", sym.value());
  570. out("{:p} ", sym.size());
  571. out("{:8} ", object_symbol_type_to_string(sym.type()));
  572. out("{:8} ", object_symbol_binding_to_string(sym.bind()));
  573. out("{}", sym.name());
  574. outln();
  575. });
  576. }
  577. }
  578. if (!found_dynamic_symbol_table)
  579. outln("No dynamic symbol information for this file.");
  580. outln();
  581. }
  582. if (display_symbol_table) {
  583. if (elf_image.symbol_count()) {
  584. outln("Symbol table '{}' contains {} entries:", ELF_SYMTAB, elf_image.symbol_count());
  585. outln(" Num: Value{} Size{} Type Bind Name", addr_padding, addr_padding);
  586. elf_image.for_each_symbol([](const ELF::Image::Symbol& sym) {
  587. out(" {:>4}: ", sym.index());
  588. out("{:p} ", sym.value());
  589. out("{:p} ", sym.size());
  590. out("{:8} ", object_symbol_type_to_string(sym.type()));
  591. out("{:8} ", object_symbol_binding_to_string(sym.bind()));
  592. out("{}", sym.name());
  593. outln();
  594. });
  595. } else {
  596. outln("Symbol table '{}' contains zero entries.", ELF_SYMTAB);
  597. }
  598. outln();
  599. }
  600. if (display_hardening) {
  601. outln("Security Hardening:");
  602. outln("RELRO Stack Canary NX PIE RPATH RUNPATH Symbols ");
  603. bool relro = false;
  604. elf_image.for_each_program_header([&relro](const ELF::Image::ProgramHeader& program_header) {
  605. if (program_header.type() == PT_GNU_RELRO) {
  606. relro = true;
  607. return IterationDecision::Break;
  608. }
  609. return IterationDecision::Continue;
  610. });
  611. bool full_relro = false;
  612. if (relro) {
  613. object->for_each_dynamic_entry([&full_relro](const ELF::DynamicObject::DynamicEntry& entry) {
  614. if (entry.tag() == DT_BIND_NOW) {
  615. full_relro = true;
  616. return IterationDecision::Break;
  617. }
  618. return IterationDecision::Continue;
  619. });
  620. if (full_relro)
  621. out("\033[0;32m{:13}\033[0m ", "Full RELRO");
  622. else
  623. out("\033[0;33m{:13}\033[0m ", "Partial RELRO");
  624. } else {
  625. out("\033[0;31m{:13}\033[0m ", "No RELRO");
  626. }
  627. bool canary = false;
  628. elf_image.for_each_symbol([&canary](const ELF::Image::Symbol& sym) {
  629. if (sym.name() == "__stack_chk_fail" || sym.name() == "__intel_security_cookie") {
  630. canary = true;
  631. return IterationDecision::Break;
  632. }
  633. return IterationDecision::Continue;
  634. });
  635. if (canary)
  636. out("\033[0;32m{:12}\033[0m ", "Canary found");
  637. else
  638. out("\033[0;31m{:12}\033[0m ", "No canary");
  639. bool nx = false;
  640. elf_image.for_each_program_header([&nx](const ELF::Image::ProgramHeader& program_header) {
  641. if (program_header.type() == PT_GNU_STACK) {
  642. if (program_header.flags() & PF_X)
  643. nx = false;
  644. else
  645. nx = true;
  646. return IterationDecision::Break;
  647. }
  648. return IterationDecision::Continue;
  649. });
  650. if (nx)
  651. out("\033[0;32m{:12}\033[0m ", "NX enabled");
  652. else
  653. out("\033[0;31m{:12}\033[0m ", "NX disabled");
  654. bool pie = false;
  655. if (header.e_type == ET_REL || header.e_type == ET_DYN)
  656. pie = true;
  657. if (pie)
  658. out("\033[0;32m{:12}\033[0m ", "PIE enabled");
  659. else
  660. out("\033[0;31m{:12}\033[0m ", "No PIE");
  661. StringView rpath;
  662. if (elf_image.is_dynamic())
  663. rpath = object->rpath();
  664. if (rpath.is_empty())
  665. out("\033[0;32m{:12}\033[0m ", "No RPATH");
  666. else
  667. out("\033[0;31m{:12}\033[0m ", rpath);
  668. StringView runpath;
  669. if (elf_image.is_dynamic())
  670. runpath = object->runpath();
  671. if (runpath.is_empty())
  672. out("\033[0;32m{:12}\033[0m ", "No RUNPATH");
  673. else
  674. out("\033[0;31m{:12}\033[0m ", runpath);
  675. out("{} symbols", elf_image.symbol_count());
  676. outln();
  677. }
  678. return 0;
  679. }