在gdb中翻转字节序? [英] Flipping endianness in gdb?
问题描述
在gdb
中,我是一个困惑的困惑者,为什么有时它会从左到右,有时是从右到左打印字节.这是一个示例,其中没有指令在它们之间运行:
In gdb
I'm a bix perplexed as to why sometimes it prints bytes left-to-right and sometimes right-to-left. Here is an example where no instruction has been run in between:
>>> x/4b $rbp-4
0x7fffffffe43c: 0x04 0x00 0x03 0x16
>>> x/2h $rbp-4
0x7fffffffe43c: 0x0004 0x1603
为什么在gdb中完成此操作?我认为它将始终将它们打印为:
Why is this done in gdb? I would think it would always print them as:
04 00 03 16
推荐答案
GDB使用目标计算机的本机字节序(默认为 1 )将您请求的大小的块解释为整数,地址增加在大块之间从左到右. x86是低端字节序.
GDB uses the target machine's native endiannes (by default1) to interpret chunks of the size you requested as integers, with addresses increasing from left to right between chunks. x86 is little-endian.
在一个块中,04 00
解释为16位小尾数整数是 0x0004
.例如,这使GDB以您期望的方式转储short
数组.您要求GDB提供16位整数块,所以它按照您的指示进行操作,使用标准的位置值表示法显示整数 value ,其中最左边的数字最高.它并不是要单独打印字节,因为您要求输入半字.
Within a chunk, 04 00
interpreted as a 16-bit little-endian integer is 0x0004
. This makes GDB dump an array of short
the way you'd expect, for example. You asked GDB for 16-bit integer chunks, so it does what you told it, showing the integer value using standard place-value notation with the leftmost digit the most significant. It's not trying to print the bytes separately because you asked for half-words.
如果要按内存顺序排列字节,请使用b
.那就是它的目的.
If you want bytes in memory order, use b
. That's what it's for.
脚注1 :
您可以更改GDB的字节序设置; show endian
显示当前设置.但是,set endian big
会导致问题,损坏寄存器值.例如在_start
停下的时间:
You can change GDB's endianness setting; show endian
shows the current setting. However, set endian big
causes problems, corrupting register values. e.g. when stopped at _start
:
(gdb) p /x $rsp
$1 = 0x7fffffffe6d0 # this is normal for x86-64
(gdb) set endian big
The target is assumed to be big endian
(gdb) x /16xw $rsp
0xd0e6ffffff7f0000: Cannot access memory at address 0xd0e6ffffff7f0000
(gdb) p /x $rsp
$2 = 0xd0e6ffffff7f0000 # this is obviously broken, byte-reversed
寄存器没有字节序,并且在扩展其值时将其翻转,因为另一条命令的地址被完全破坏了.
Registers don't have an endianness, and flipping them when expanding their value as an address for another command is just totally broken.
不完全相关的重复项:
GDB是否正确解释了内存地址? - 为什么存储在内存中的数据被反转了?
- Big Endian和Little Endian有点困惑
- Is GDB interpreting the memory address correctly?
- Why is data stored in memory reversed?
- Big Endian and Little endian little confusion
这篇关于在gdb中翻转字节序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!