使用gcc了解共享库 [英] understanding shared libraries using gcc

查看:153
本文介绍了使用gcc了解共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解共享库在C以下行为


  

机一


  $猫one.c
#包括LT&;&stdio.h中GT;诠释主(){
    的printf(%D,45);
}
$ GCC one.c -o 1 -O3
$ LDD 1
    Linux的gate.so.1 => (0x00331000)
    libc.so.6的= GT; /lib/tls/i686/cmov/libc.so.6(0x00bc2000)
    /lib/ld-linux.so.2(0x006dc000)
$猫two.c
诠释主(){
    INT I = 0;
}
$ GCC two.c -o 2 -O3
$ LDD 2
    Linux的gate.so.1 => (0x006f7000)
    libc.so.6的= GT; /lib/tls/i686/cmov/libc.so.6(0x00110000)
    /lib/ld-linux.so.2(0x00eb0000)
$


  

机两台


  $猫three.c
#包括LT&;&stdio.h中GT;诠释主(){
    的printf(%D,45);
}
$ GCC three.c -o 3 -O3
$ LDD 3
    /usr/lib/libcwait.so(0xb7ffd000)
    libc.so.6的= GT; /lib/tls/i686/nosegneg/libc.so.6(0x002de000)
    /lib/ld-linux.so.2(0x002bf000)
$

有几件事情,我不完全理解为present:


  • 这是什么在括号中给出的地址(例如,(0x002de000))是什么意思?

    这些地址是即使是相同的机器上的相同的库,这表明这些是其中这些库被加载在存储器中的位置的地址不同。但是,如果这是真的,为什么这些库都加载到内存中(我没有执行该程序还,不应该,他们只在运行时加载?)。


  • 为什么两个需要的库呢?我已经使用 -O3 ,并汇编输出

      $ GCC two.c -S -O3
    $猫two.s
        .file例如,two.c
        。文本
        .p2align 4日,15
    .globl主
        .TYPE为主,@function
    主要:
        pushl%EBP
        MOVL%ESP,EBP%
        popl%EBP
        RET
        .size为主,。,主
        .identGCC:(Ubuntu的4.4.3-4ubuntu5)4.4.3
        .section伪.note.GNU堆栈,,@ PROGBITS
    $

    什么是所有的库?

  • 的需要
  • 在机两台,为什么 /usr/lib/libcwait.so 正在使用 Linux的gate.so.1代替

    我想这是因为机器的两个内核是很老(2.6.9)和库 Linux的gate.so.1 不可用。那是什么原因呢?



解决方案

  

这是什么在括号中给出的地址(例如,(0x002de000))是什么意思?


有其中库加载的(虚拟)存储器地址。
最近的系统可以提供要将库装载虽然随机化,所以
该地址可能会调用之间变化。


  

不应该,他们只在运行时加载?


是的,他们都是。 LDD经历大致相同的步骤,是什么
在运行时完成,虽然,要能找出各种各样的事情。


  

为什么二者必须的库呢?


libc.so.6的是标准C库(和其他的东西,如接口内核)和总是在纽约默认链接。 GCC有选项来控制这虽然,例如在 -nostdlib 标志

ld-linux.so是一个动态加载器,并且是repsonsible装载/迁往其他共享libraryis并运行应用程序。对于ld-linux.so联机帮助页为您提供了详细信息。

Linux的gate.so.1是一个虚拟的图书馆,它的存在只是在内核内存。它是用来执行系统调用内核,并计算出最有效的方式根据你的CPU上这样做。这可能是以后添加到Linux比你其他的2.6.9内核的机器。

我不知道是什么/usr/lib/libcwait.so是,但机会是你可以通过做得到关于它的一些信息转-qif /usr/lib/libcwait.so

I am trying to understand the following behavior of shared libraries in C

Machine One

$ cat one.c 
#include<stdio.h>

int main() {
    printf ("%d", 45);
}
$ gcc one.c -o one -O3
$ ldd one
    linux-gate.so.1 =>  (0x00331000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00bc2000)
    /lib/ld-linux.so.2 (0x006dc000)
$ cat two.c 
int main() {
    int i = 0;
}
$ gcc two.c -o two -O3
$ ldd two
    linux-gate.so.1 =>  (0x006f7000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00110000)
    /lib/ld-linux.so.2 (0x00eb0000)
$

Machine Two

$ cat three.c
#include<stdio.h>

int main() {
    printf ("%d", 45);
}
$ gcc three.c -o three -O3
$ ldd three
    /usr/lib/libcwait.so (0xb7ffd000)
    libc.so.6 => /lib/tls/i686/nosegneg/libc.so.6 (0x002de000)
    /lib/ld-linux.so.2 (0x002bf000)
$

A few things I don't fully understand at present:

  • What does the address given in brackets (for example, (0x002de000)) mean?

    These addresses are different even for the same library on the same machine, which suggests these are addresses of the locations in memory where these libraries are loaded. But, if that is true, why are these libraries loaded in memory at all (I did not execute the programs yet, shouldn't they be loaded only at runtime?).

  • Why does two need any libraries at all? I have used -O3, and the assembler output is

    $ gcc two.c -S -O3 
    $ cat two.s
        .file   "two.c"
        .text
        .p2align 4,,15
    .globl main
        .type   main, @function
    main:
        pushl   %ebp
        movl    %esp, %ebp
        popl    %ebp
        ret
        .size   main, .-main
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section    .note.GNU-stack,"",@progbits
    $
    

    What is the need of any libraries at all?

  • On Machine Two, why is /usr/lib/libcwait.so being used instead of linux-gate.so.1?

    I think this is because the kernel on Machine Two is very old (2.6.9) and the library linux-gate.so.1 is not available. Is that the reason?

解决方案

What does the address given in brackets (for example, (0x002de000)) mean?

It is the (virtual) memory address where the library is loaded. Recent system can provide randomization of where libraries are loaded though, so that address might vary between invocations.

shouldn't they be loaded only at runtime?

Yes they are. ldd goes through much of the same procedure as what is done at runtime though, to be able to figure out various things.

Why does two need any libraries at all?

libc.so.6 is the standard C library (and other stuff, like the interface to the kernel) and is always linked in ny default. gcc has options to control this though, e.g. the -nostdlib flag

ld-linux.so is a the dynamic loader, and is repsonsible for loading/relocating other shared libraryis and run your application. The manpage for ld-linux.so gives you the details.

linux-gate.so.1 is a virtual library, it exists only in memory in the kernel. It's used to perform system calls to the kernel, and figures out the most efficient way to do so based on your CPU. This was probably added to linux later than your other 2.6.9 kernel machine.

I don't know what /usr/lib/libcwait.so is , but chances are you can get some info about it by doing rpm -qif /usr/lib/libcwait.so

这篇关于使用gcc了解共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆