项目作者: fedefloris

项目描述 :
A reproduction of nm and otool functionalities.
高级语言: C
项目地址: git://github.com/fedefloris/Nm-otool.git
创建时间: 2019-02-08T10:53:11Z
项目社区:https://github.com/fedefloris/Nm-otool

开源协议:MIT License

下载


Nm-otool - 42born2code

Build Status


Challenge

To recode the UNIX commands nm (with no options) and otool (the same as -t) The executables must be named ft_nm and ft_otool

The function must conform to 42’s norm.

The programs cannot leak. All errors must be handled carefully. In no way can the programs quit in an unexpected manner (Segmentation fault, bus error, double free, etc).

Allowed functions for the mandatory part are:

  • open(2)
  • close(2)
  • mmap(2)
  • munmap(2)
  • write(2)
  • fstat(2)
  • malloc(3)
  • free(3)

Everything else is forbidden. Other functions can be used for bonus, but their use must be justified.


Using the project

Compile

To compile the project, run make. This will compile ft_nm and ft_otool

  1. make

Execute

To execute (see the below options) execute like a normal non-$PATH binary.

  1. $> ./ft_nm ls
  2. U __DefaultRuneLocale
  3. U ___assert_rtn
  4. U ___bzero
  5. U ___error
  6. U ___maskrune
  7. U ___snprintf_chk
  8. U ___stack_chk_fail
  9. U ___stack_chk_guard
  10. U ___stderrp
  11. U ___stdoutp
  12. U ___tolower
  13. 0000000100000000 T __mh_execute_header
  14. U _acl_free
  15. U _acl_get_entry
  16. U _acl_get_flag_np
  17. U _acl_get_flagset_np
  18. U _acl_get_link_np
  19. U _acl_get_perm_np
  20. U _acl_get_permset
  21. U _acl_get_qualifier
  22. U _acl_get_tag_type
  23. U _atoi
  24. U _calloc
  25. U _compat_mode
  26. U _err
  27. U _exit
  28. U _fflagstostr
  29. U _fprintf
  30. U _fputs
  31. U _free
  32. U _fts_children$INODE64
  33. U _fts_close$INODE64
  34. U _fts_open$INODE64
  35. U _fts_read$INODE64
  36. U _fts_set$INODE64
  37. U _fwrite
  38. U _getbsize
  39. U _getenv
  40. U _getopt
  41. U _getpid
  42. U _getuid
  43. U _getxattr
  44. U _group_from_gid
  45. U _humanize_number
  46. U _ioctl
  47. U _isatty
  48. U _kill
  49. U _listxattr
  50. U _localtime
  51. U _malloc
  52. U _mbr_identifier_translate
  53. U _mbrtowc
  54. U _memchr
  55. U _nl_langinfo
  56. U _optind
  57. U _printf
  58. U _putchar
  59. U _readlink
  60. U _realloc
  61. U _reallocf
  62. U _setenv
  63. U _setlocale
  64. U _signal
  65. U _sscanf
  66. U _strcoll
  67. U _strcpy
  68. U _strdup
  69. U _strerror
  70. U _strftime
  71. U _strlen
  72. U _strmode
  73. U _tgetent
  74. U _tgetstr
  75. U _tgoto
  76. U _time
  77. U _tputs
  78. U _user_from_uid
  79. U _uuid_unparse_upper
  80. U _warn
  81. U _warnx
  82. U _wcwidth
  83. U _write
  84. U dyld_stub_binder
  1. $> ./ft_otool -t ls
  2. ls:
  3. Contents of (__TEXT,__text) section
  4. 0000000100000f0c 55 48 89 e5 48 83 c7 68 48 83 c6 68 5d e9 5e 36
  5. 0000000100000f1c 00 00 55 48 89 e5 48 8d 46 68 48 8d 77 68 48 89
  6. 0000000100000f2c c7 5d e9 49 36 00 00 55 48 89 e5 4c 8b 46 60 48
  7. 0000000100000f3c 8b 57 60 48 8b 4a 30 b8 01 00 00 00 49 39 48 30
  8. 0000000100000f4c 7f 1a 7d 07 b8 ff ff ff ff eb 11 48 8b 4a 38 49
  9. 0000000100000f5c 39 48 38 7f 07 b8 ff ff ff ff 7d 02 5d c3 48 83
  10. 00000001000043ac 48 ff c3 49 ff cf 75 f0 8b bd 4c ff ff ff 44 89
  11. 00000001000043bc e3 e8 1a 02 00 00 01 c3 e9 64 ff ff ff 48 8b 05
  12. 00000001000043cc 48 0c 00 00 48 8b 00 48 3b 45 d0 75 14 89 d8 48
  13. 00000001000043dc 81 c4 98 00 00 00 5b 41 5c 41 5d 41 5e 41 5f 5d
  14. 00000001000043ec c3 e8 4c 00 00 00 55 48 89 e5 48 8b 05 23 0c 00
  15. 00000001000043fc 00 48 8b 08 48 8d 3d 21 0b 00 00 be 3e 00 00 00
  16. 000000010000440c ba 01 00 00 00 e8 be 00 00 00 bf 01 00 00 00 e8
  17. 000000010000441c 78 00 00 00

Bonus

  • ft_nm works on macOS and Linux

  • ft_nm works on ELF binaries. ELF binaries are a completely different architecture to mach-o. This functionality is an entire ft_nm in its own right.

  • Binary finder: ./ft_nm ls is equivalent to ./ft_nm /bin/ls.

  • ft_otool is way faster than otool. One benchmark measured:
    ft_otool: 73 seconds
    otool: 551 seconds

  • Unit testing

  • Every error has an individual message.

  • -j Just display the symbol names (no value or type).

  • -r Sort in reverse order.

  • -p Don’t sort; display in symbol-table order.

  • -h Help; for usage.