printf @ plt和puts @ plt之间的区别 [英] Difference between printf@plt and puts@plt
问题描述
我一直在通过反汇编一些C代码来学习汇编语言.当我使用GDB反汇编此基本C代码时:
I've been learning assembly language by disassembling some C code. When I disassemble this basic C code with GDB:
#include <stdio.h>
void main(void) {
printf("Hello World\n");
}
在汇编代码中,它给出了这一行:
Among assembly code, it gives this line:
0x08048424 <+25>: call 0x80482e0 <puts@plt>
但是,当我反汇编以下在printf函数中具有整数的代码时:
However, when I disassemble below code which has an integer in printf function:
#include <stdio.h>
void main(void) {
int a = 1;
printf("Hello Word %d\n", a);
}
它给出了这一行:
0x0804842e <+35>: call 0x80482e0 <printf@plt>
printf @ plt和puts @ plt有什么区别?
What is the difference between printf@plt and puts@plt?
为什么反汇编程序在没有整数参数的情况下无法识别printf函数?
Why disassembler does not recognize printf function without an integer parameter?
推荐答案
在GCC中, printf
和 puts
是内置函数.这意味着编译器完全了解其语义.在这种情况下,如果编译器认为会产生更好(更快和/或更紧凑)的代码,则可以将对一个函数的调用替换为对另一个函数的等效调用.
In GCC printf
and puts
are built-in functions. Which means that the compiler has full knowledge of their semantics. In such cases the compiler is free to replace a call to one function with an equivalent call to another, if it thinks that it will produce better (faster and/or more compact) code.
puts
是通常更有效的函数,因为它不必解析和解释格式字符串.
puts
is a generally more efficient function since it does not have to parse and interpret a format string.
这正是您的情况.您第一次拨打 printf
并不需要任何特定于 printf
的功能.您提供给 printf
的格式字符串很简单:它没有转换说明符.编译器认为,等效调用 printf
的首次调用.
This is exactly what happened in your case. Your first call to printf
does not really need any printf
-specific features. The format string you supplied to printf
is trivial: it has no conversion specifiers in it. The compiler thought that your first call to printf
is better served with an equivalent call to puts
.
与此同时,您第二次调用 printf
时,会轻而易举地使用 printf
格式字符串,即它依赖于 printf
特定的功能.
Meanwhile, your second call to printf
makes non-trivial use of printf
format string, i.e. it relies on printf
-specific features.
(从2005年开始对此问题进行了相当彻底的研究: http://www.ciselant.de/projects/gcc_printf/gcc_printf.html )
(Some rather thorough research of this specific matter from 2005: http://www.ciselant.de/projects/gcc_printf/gcc_printf.html)
这篇关于printf @ plt和puts @ plt之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!