本文介绍了它是如何,主要功能是始终在同一地址加载,而变量具有不同的地址大部分的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我今天撰文称这个小程序,我被吹走的结果。下面是节目

I wrote this small program today and I was blown away by the results. Here is the program



int main(int argc, char **argv)
{
 int a;
 printf("\n\tMain is located at: %p and the variable a is located at address: %p",main,&a);
 return 0;
}

在我的机器的主要功能始终加载地址0x80483d4和变量的地址不断变化怎么会这样?我在该虚拟化方案的一部分在OS不断重新定位的指令的地址的操作系统读取。那么,为什么说我每次运行这个程序,主要是在同一地址装?

on my machine the main function is always loaded at address "0x80483d4" and the address of the variable keeps on varying How does this happen? I read in operating systems that as a part of virtualization scheme the OS keeps relocating the address of instructions. So why is it that everytime I run this program that main is loaded at the same address?

提前家伙的感谢。

推荐答案

在ELF系统如Linux,地址在其中正常的可执行文件的段(ELF类型 ET_EXEC )负载固定在编译的时候。共享对象(ELF类型 ET_DYN ),如图书馆建成与位置无关,与他们自己的网段地址空间中的任何地方可装载(可能与某些架构限制)。有可能建立可执行,使得它们实际上是 ET_DYN - 这些被称为与位置无关的可执行文件(PIE),但并不是一个普通的技术

On ELF systems such as Linux, the addresses at which the segments of normal executable files (ELF type ET_EXEC) load are fixed at compile time. Shared objects (ELF type ET_DYN) such as libraries are built to be position-independent, with their segments loadable anywhere in the address space (potentially with some restrictions on some architectures). It is possible to build executables such that they are actually ET_DYN -- these are known as "position-independent executables" (PIE), but is not a common technique.

你们看到的是事实,你的的main()的功能是在你的编译的可执行文件的固定地址文本段。还尝试打印库函数的地址,如的printf()通过则dlsym中找到它()后 - 如果您的系统不支持并启用了地址空间布局随机化(ASLR),那么你应该看到运行的功能变化的地址来运行你的程序的。 (如果你只是直接把你的code参考打印库函数的地址,你实际上可能得​​到的是它是静态在一个固定的地址编译函数的程序查找表(PLT)蹦床,地址在你的可执行文件。)

What you are seeing is the fact that your main() function is in the fixed-address text segment of your compiled executable. Try also printing the address of a library function such as printf() after locating it via dlsym() -- if your system does support and have enabled address space layout randomization (ASLR), then you should see the address of that function change from run to run of your program. (If you just print the address of the library function by putting the reference directly in your code, what you may actually get is the address of the function's procedure lookup table (PLT) trampoline, which is statically compiled at a fixed address in your executable.)

您看到的变化地址变量运行到运行,因为它是在栈上创建一个自动变量,而不是在静态分配的内存。取决于操作系统和版本,叠层的基底的地址可以从运行切换到即使没有ASLR运行。如果将变量声明为全局的功能外,你看它的表现你的的main()函数的作用是一样的。

The variable you see change address from run-to-run because it is an automatic variable created on the stack, not in statically allocated memory. Depending on OS and version, the address of the base of the stack may shift from run to run even without ASLR. If you move the variable declaration to be a global outside of your function, you see it behave the same way your main() function does.

下面是一个完整的例子 - 的东西,如 -o GCC编译例如-dl example.c

Here's a full example -- compile with something like gcc -o example example.c -dl:

#include <stdio.h>
#include <dlfcn.h>

int a = 0;

int main(int argc, char **argv)
{
    int b = 0;
    void *handle = dlopen(NULL, RTLD_LAZY);
    printf("&main: %p; &a: %p\n", &main, &a);
    printf("&printf: %p; &b: %p\n", dlsym(handle, "printf"), &b);
    return 0;
}

这篇关于它是如何,主要功能是始终在同一地址加载,而变量具有不同的地址大部分的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:04