我已经使用objdump
分解了已编译库文件中的所有功能,并将输出写入了text
文件中。在text
文件中,称为clear_bit
的函数的输出如下。
Disassembly of section .text.stm32f30x::spi1::cr1::_CRCENW::clear_bit:
0: 80 b5 84 b0 addlt r11, r4, r0, lsl #11
4: 01 46 03 90 andls r4, r3, r1, lsl #12
8: 03 98 00 22 andhs r9, r0, #196608
c: 02 91 11 46 ldrmi r9, [r1], -r2, lsl #2
10: ff f7 fe ff <unknown>
14: 01 90 ff e7 ldrb r9, [pc, r1]!
18: 01 98 04 b0 andlt r9, r4, r1, lsl #16
1c: 80 <unknown>
1d: bd <unknown>
另一个功能set_bit
的输出如下-:
Disassembly of section .text.stm32f30x::spi1::cr1::_CRCNEXTW::set_bit:
0: 80 b5 84 b0 addlt r11, r4, r0, lsl #11
4: 01 46 03 90 andls r4, r3, r1, lsl #12
8: 03 98 01 22 andhs r9, r1, #196608
c: 02 91 11 46 ldrmi r9, [r1], -r2, lsl #2
10: ff f7 fe ff <unknown>
14: 01 90 ff e7 ldrb r9, [pc, r1]!
18: 01 98 04 b0 andlt r9, r4, r1, lsl #16
1c: 80 <unknown>
1d: bd <unknown>
类似于以上两个功能,此output.txt
包含100多个此类功能的反汇编。但是,我需要在这里实现的是仅提取每个函数的十六进制字节值[80,b5,84,b0,01,..,b0,80,bd]
,而无需汇编指令,函数名称,偏移量等。我试图提取与每个函数相对应的这些字节序列没有单个序列,以便在机器学习中开发模型。以下是我仅对两个函数的期望。(注释仅出于理解目的,我不需要在期望的输出中使用任何函数)
// byte sequence related to first function
80 b5 84 b0 01 46 03 90 03 98 00 22 02 91 11 46 ff f7 fe ff 01 90 ff
e7 01 98 04 b0 80 bd
// byte sequence related to second function separated by a line
80 b5 84 b0 01 46 03 90 03 98 01 22 02 91 11 46 ff f7 fe ff 01 90 ff
e7 01 98 04 b0 80 bd
我使用了xxd -g 1
命令,但它给了我一个字节序列,如下所示,带有偏移量,字节值右边的一些其他值,并且似乎包含所有节的反汇编。(不仅文字部分)。
00000000: 21 3c 61 72 63 68 3e 0a 2f 20 20 20 20 20 20 20 !<arch>./
00000010: 20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20 0
00000020: 20 20 20 20 30 20 20 20 20 20 30 20 20 20 20 20 0 0
00000030: 30 20 20 20 20 20 20 20 34 37 33 32 34 30 20 20 0 473240
00000040: 20 20 60 0a 00 00 1c 8c 00 07 aa ea 00 07 aa ea `.............
00000050: 00 07 aa ea 00 07 aa ea 00 07 aa ea 00 07 aa ea ................
00000060: 00 07 aa ea 00 07 aa ea 00 07 aa ea 00 07 aa ea ................
00000070: 00 07 aa ea 00 07 aa ea 00 07 aa ea 00 07 aa ea ................
00000080: 00 07 aa ea 00 07 aa ea 00 07 aa ea 00 07 aa ea ................
00000090: 00 07 aa ea 00 07 aa ea 00 07 aa ea 00 07 aa ea ................
000000a0: 00 07 aa ea 00 07 aa ea 00 07 aa ea 00 07 aa ea ................
000000b0: 00 08 1a 1a 00 08 1a 1a 00 08 1a 1a 00 08 1a 1a ................
000000c0: 00 08 1a 1a 00 08 1a 1a 00 08 3a ee 00 08 3a ee ..........:...:.
我一直在尝试其他工具,并经历了其他类似的堆栈溢出问题,但到目前为止都失败了。我不知道我是否以错误的方式使用了xxd
,否则还有其他工具可以实现我的目标。任何帮助将不胜感激。谢谢!
请您尝试以下操作:
# fold $str, print and clear
flush() {
if [[ -n $str ]]; then
fold -w 69 <<< "$str"
echo
str=""
fi
}
header='^Disassembly of section'
body='^[[:blank:]]*[0-9a-fA-f]+:[[:blank:]]+(([0-9a-fA-f]{2} )+)'
while IFS= read -r line; do
if [[ $line =~ $header ]]; then
flush
echo "// $line"
elif [[ $line =~ $body ]]; then
# concatenate the byte sequence on $str
str+="${BASH_REMATCH[1]}"
fi
done < output.txt
flush
output.txt(作为上述脚本的输入):
Disassembly of section .text.stm32f30x::spi1::cr1::_CRCENW::clear_bit:
0: 80 b5 84 b0 addlt r11, r4, r0, lsl #11
4: 01 46 03 90 andls r4, r3, r1, lsl #12
8: 03 98 00 22 andhs r9, r0, #196608
c: 02 91 11 46 ldrmi r9, [r1], -r2, lsl #2
10: ff f7 fe ff <unknown>
14: 01 90 ff e7 ldrb r9, [pc, r1]!
18: 01 98 04 b0 andlt r9, r4, r1, lsl #16
1c: 80 <unknown>
1d: bd <unknown>
Disassembly of section .text.stm32f30x::spi1::cr1::_CRCNEXTW::set_bit:
0: 80 b5 84 b0 addlt r11, r4, r0, lsl #11
4: 01 46 03 90 andls r4, r3, r1, lsl #12
8: 03 98 01 22 andhs r9, r1, #196608
c: 02 91 11 46 ldrmi r9, [r1], -r2, lsl #2
10: ff f7 fe ff <unknown>
14: 01 90 ff e7 ldrb r9, [pc, r1]!
18: 01 98 04 b0 andlt r9, r4, r1, lsl #16
1c: 80 <unknown>
1d: bd <unknown>
结果:
// Disassembly of section .text.stm32f30x::spi1::cr1::_CRCENW::clear_bit:
80 b5 84 b0 01 46 03 90 03 98 00 22 02 91 11 46 ff f7 fe ff 01 90 ff
e7 01 98 04 b0 80 bd
// Disassembly of section .text.stm32f30x::spi1::cr1::_CRCNEXTW::set_bit:
80 b5 84 b0 01 46 03 90 03 98 01 22 02 91 11 46 ff f7 fe ff 01 90 ff
e7 01 98 04 b0 80 bd
${BASH_REMATCH[1]}
中。希望这就是您想要的。