The blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

120 lines
4.5 KiB

  1. ---
  2. title: "CTF + ARMeb + debugging"
  3. description: >
  4. CTF + ARMeb + debugging
  5. created: !!timestamp '2014-03-05'
  6. time: 7:21 PM
  7. tags:
  8. - ctf
  9. - debugging
  10. - arm
  11. ---
  12. I've been working on making the AVILA board work again with FreeBSD.
  13. Thanks to Jim from Netgate for sending me a board to do this work.
  14. I still have a pending patch waiting to go through bde to fix an
  15. unaligned off_t store which gets things farther, but with the patch I'm
  16. getting a: `panic: vm_page_alloc: page 0xc0805db0 is wired` shortly after
  17. the machine launches the daemons.
  18. I did work to get cross gdb working for armeb (committed in r261787 and
  19. r261788), but that didn't help as there is no kernel gdb support on
  20. armeb. As I'm doing this debugging over the network, I can't dump a
  21. core.
  22. I didn't feel like hand decoding a `struct vm_page`, so I thought of other
  23. methods, and one way is to use CTF to parse the data type and decode the
  24. data. I know python and ctypes, so I decided to wrap libctf and see
  25. what I could do.
  26. Getting the initial python wrapper working was easy, but my initial test
  27. data was the kernel on my amd64 box that I am developing on. Now I
  28. needed to use real armeb CTF data. I point it to my kernel, and I get:
  29. "`File uses more recent ELF version than libctf`". Ok, extract the CTF
  30. data from the kernel (ctf data is stored in a section named `.SUNW_ctf`)
  31. and work on that directly:
  32. ```
  33. $ objcopy -O binary --set-section-flags optfiles=load,alloc -j .SUNW_ctf /tftpboot/kernel.avila.avila /dev/null
  34. objcopy: /tftpboot/kernel.avila.avila: File format not recognized
  35. ```
  36. Well, ok, that's not too surprising since it's an ARMEB binary, lets try:
  37. ```
  38. $ /usr/obj/arm.armeb/usr/src.avila/tmp/usr/bin/objcopy -O binary --set-section-flags optfiles=load,alloc -j .SUNW_ctf /tftpboot/kernel.avila.avila /tmp/test.avila.ctf
  39. $ ls -l /tmp/test.avila.ctf
  40. -rwxr-xr-x 1 jmg wheel 0 Mar 5 17:59 /tmp/test.avila.ctf
  41. ```
  42. Hmm, that didn't work too well, ok, lets just use dd to extract the data
  43. using info from `objdump -x`.
  44. Ok, now that I've done that, I get:
  45. ```
  46. ValueError: '/tmp/avila.ctf': File is not in CTF or ELF format
  47. ```
  48. Hmm, why is that? Well, it turns out that the endian of the CTF data
  49. is wrong. The magic is `cf f1`, but the magic on amd64 is `f1 cf`, it's
  50. endian swapped. That's annoying. After spending some time trying to
  51. build an cross shared version of libctf, I find that it has the same
  52. issue.
  53. After a bit of looking around, I discover the CTF can only ever read
  54. native endianness, but `ctfmerge` has a magic option that will write out
  55. endian swapped data if necessary depending upon the ELF file it's
  56. putting in. This means that the CTF data in an armeb object file will
  57. be different depending upon the endian you compiled it on, so the object
  58. file isn't cross compatible. But, this does mean that the data in the
  59. object files will be readable by libctf, just not the data written into
  60. the kernel.
  61. So, I create a sacrificial amd64 binary:
  62. ```
  63. $ echo 'int main() {}' | cc -o /tmp/avila2.ctf -x c -
  64. ```
  65. And use `ctfmerge` to put the data in it:
  66. ```
  67. $ ctfmerge -L fldkj -o /tmp/avila2.ctf /usr/obj/arm.armeb/usr/src.avila/sys/AVILA/*.o
  68. ```
  69. and again use `dd` to extract the `.SUNW_ctf` section into a separate file.
  70. With all this work, I finally have the CTF data in a format that libctf
  71. can parse, so, I try to parse some data. Now the interesting thing is
  72. that the CTF data does encode sizes of integers, but it uses the native
  73. arch's pointer sizes for `CTF_K_POINTER` types, which means that pointers
  74. appear to be 8 bytes in size instead of the correct 4 bytes. A little
  75. more hacking on the ctf.py script to force all pointers to be 4 bytes,
  76. and a little help to convert ddb output to a string and finally, I have
  77. a dump of the <code>struct&nbsp;vm_page</code> that I was trying to get all along:
  78. ```
  79. {'act_count': '\x00',
  80. 'aflags': '\x00',
  81. 'busy_lock': 1,
  82. 'dirty': '\xff',
  83. 'flags': 0,
  84. 'hold_count': 0,
  85. 'listq': {'tqe_next': 0xc0805e00, 'tqe_prev': 0xc06d18a0},
  86. 'md': {'pv_kva': 3235856384,
  87. 'pv_list': {'tqh_first': 0x0, 'tqh_last': 0xc0805de0},
  88. 'pv_memattr': '\x00',
  89. 'pvh_attrs': 0},
  90. 'object': 0xc06d1878,
  91. 'oflags': '\x04',
  92. 'order': '\t',
  93. 'phys_addr': 17776640,
  94. 'pindex': 3572,
  95. 'plinks': {'memguard': {'p': 0, 'v': 3228376932},
  96. 'q': {'tqe_next': 0x0, 'tqe_prev': 0xc06d1f64},
  97. 's': {'pv': 0xc06d1f64, 'ss': {'sle_next': 0x0}}},
  98. 'pool': '\x00',
  99. 'queue': '\xff',
  100. 'segind': '\x01',
  101. 'valid': '\xff',
  102. 'wire_count': 1}
  103. ```
  104. So, the above was produced w/ the final [ctf.py](https://www.funkthat.com/~jmg/ctf.py) script.