CS:APP-3.2.2

复现

mstore.c文件

long mult2(long,long);

void multstore(long x,long y,long *dest)
{
	long t=mult2(x,y);
	*dest=t;
}

然后执行命令:

$ gcc -Og -S mstore.c

接着就生成了一个.s文件。

在这里插入图片描述
文件可以直接打开,内容如下:

	.file	"mstore.c"
	.text
	.globl	multstore
	.type	multstore, @function
multstore:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdx, %rbx
	call	mult2@PLT
	movq	%rax, (%rbx)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE0:
	.size	multstore, .-multstore
	.ident	"GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	 1f - 0f
	.long	 4f - 1f
	.long	 5
0:
	.string	 "GNU"
1:
	.align 8
	.long	 0xc0000002
	.long	 3f - 2f
2:
	.long	 0x3
3:
	.align 8
4:

我们可以找到书上写的那些代码:

multstore:
.LFB0:
	//.cfi_startproc
	//endbr64
	pushq	%rbx
	//.cfi_def_cfa_offset 16
	//.cfi_offset 3, -16
	movq	%rdx, %rbx
	call	mult2@PLT
	movq	%rax, (%rbx)
	popq	%rbx
	//.cfi_def_cfa_offset 8
	ret
	//.cfi_endproc

也可以看看intel汇编的东西:

指令:
$ gcc -Og -S -masm=intel mstore.c -o mstore-intel

打开文件mstore-intel,内容如下:

	.file	"mstore.c"
	.intel_syntax noprefix
	.text
	.globl	multstore
	.type	multstore, @function
multstore:
.LFB0:
	.cfi_startproc
	endbr64
	push	rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	mov	rbx, rdx
	call	mult2@PLT
	mov	QWORD PTR [rbx], rax
	pop	rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE0:
	.size	multstore, .-multstore
	.ident	"GCC: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	 1f - 0f
	.long	 4f - 1f
	.long	 5
0:
	.string	 "GNU"
1:
	.align 8
	.long	 0xc0000002
	.long	 3f - 2f
2:
	.long	 0x3
3:
	.align 8
4:

找到如下代码:

multstore:
.LFB0:
	//.cfi_startproc
	endbr64
	push	rbx
	//.cfi_def_cfa_offset 16
	//.cfi_offset 3, -16
	mov	rbx, rdx
	call	mult2@PLT
	mov	QWORD PTR [rbx], rax
	pop	rbx
	//.cfi_def_cfa_offset 8
	ret
	//.cfi_endproc

这个对我来说,看起来就舒服许多了。

接着,我们可以编译得到.o文件(目标代码文件),

指令:
$ gcc -Og -c mstore.c

因为它是二进制格式的,所以是无法直接打开查看的。

在这里插入图片描述

接着如果想要查看.o文件的话,可以使用gdb:

在这里插入图片描述
上面查看的长度不够,我们再多查看一点:

pwndbg> x/20xb multstore 
0x0 <multstore>:	0xf3	0x0f	0x1e	0xfa	0x53	0x48	0x89	0xd3
0x8 <multstore+8>:	0xe8	0x00	0x00	0x00	0x00	0x48	0x89	0x03
0x10 <multstore+16>:	0x5b	0xc3	Cannot access memory at address 0x12

查看多了,具体是18个字节:

pwndbg> x/18xb multstore 
0x0 <multstore>:	0xf3	0x0f	0x1e	0xfa	0x53	0x48	0x89	0xd3
0x8 <multstore+8>:	0xe8	0x00	0x00	0x00	0x00	0x48	0x89	0x03
0x10 <multstore+16>:	0x5b	0xc3

我们来看这样一段数据:

53 48 89 d3 e8 00 00 00	00 48 89 03 5b c3

刚好是书本上写的14个字节。

我们还可以使用objdump:

指令:
$ objdump -d mstore.o

在这里插入图片描述

也看到了这段代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值