Linux的libc库

Linux的libc库

0x00 libc

  1. libc是Standard C library的简称,它是符合ANSI C标准的一个函数库。libc库提供C语言中所使用的宏,类型定义,字符串操作函数,数学计算函数以及输入输出函数等。正如ANSI C是C语言的标准一样,libc只是一种函数库标准,每个操作系统都会按照该标准对标准库进行具体实现
  2. 通常我们所说的libc是特指某个操作系统的标准库,比如我们在Linux操作系统下所说的libc即glibc。glibc是类Unix操作系统中使用最广泛的libc库,它的全称是GNU C Library。类Unix操作系统通常将libc库作为操作系统的一部分 (被视为操作系统与用户程序之间的接口)

libc库不仅实现标准C语言中的函数,而且也包含自己所属的函数接口。比如在glibc库中,既包含标准C中的fopen(),又包含类Unix系统中的open()。在类Unix操作系统中,如果缺失了标准库,那么整个操作系统将不能正常运转。

而Windows系统并不将libc库作为整个核心操作系统的一部分。通常每个编译器都附属自己的libc库,这些libc既可以静态编译到程序中,又可以动态编译到程序中。也就是说应用程序依赖编译器而不是操作系统。

  1. 封装函数

    在Linux系统中,glibc库中包含许多API,大多数API都对应一个系统调用,比如应用程序中使用的接口open()就对应同名的系统调用open()。

    glibc库中通过封装例程(Wrapper Routine)将API和系统调用关联起来。

    API是头文件中所定义的函数接口,而位于glibc中的封装例程则是对该API对应功能的具体实现. 事实上接口open()所要完成的功能是通过系统调用open()完成的,因此封装例程要做的工作是先将接口open()中的参数复制到相应寄存器中,然后引发一个异常,从而系统进入内核去执行sys_open(),最后当系统调用执行完毕后,封装例程还要将错误码返回到应用程序中。

【注意】函数库中的API和系统调用并没有一一对应的关系。应用程序借助系统调用可以获得内核所提供的服务,像字符串操作这样的函数并不需要借助内核来实现,因此也就不必与某个系统调用关联。也不是必须通过封装例程才能使用系统调用,syscall()和_syscallx()两个函数可以直接调用系统调用。

glibc除了封装linux操作系统所提供的系统服务外,它本身也提供了许多其它一些必要功能服务的实现,主要的如下:

(1)string,字符串处理
(2)signal,信号处理
(3)dlfcn,管理共享库的动态加载
(4)direct,文件目录操作
(5)elf,共享库的动态加载器,也即interpreter
(6)iconv,不同字符集的编码转换
(7)inet,socket接口的实现
(8)intl,国际化,也即gettext的实现
(9)io
(10)linuxthreads
(11)locale,本地化
(12)login,虚拟终端设备的管理,及系统的安全访问
(13)malloc,动态内存的分配与管理
(14)nis
(15)stdlib,其它基本功能

0x01 Ubuntu

  1. 可以执行libc.so.6来获取信息,需要携带路径,如果不知道lib.so.6在哪个文件中,可以执行find / -name libc.so.6来定位文件位置

    zbb@ubuntu:~$ sudo find / -name lib.so.6
    [sudo] password for zbb: 
    find: ‘/run/user/108/gvfs’: Permission denied
    zbb@ubuntu:~$ sudo find / -name libc.so.6
    /lib/x86_64-linux-gnu/libc.so.6
    find: ‘/run/user/108/gvfs’: Permission denied
    /lib32/libc.so.6
    find: ‘/proc/37558’: No such file or directory
    find: ‘/proc/37559’: No such file or directory
    /libx32/libc.so.6
    

针对64位系统使用命令:

zbb@ubuntu:~$ ls -l /lib/x86_64-linux-gnu/libc.so.6 
lrwxrwxrwx 1 root root 12 Feb  6  2019 /lib/x86_64-linux-gnu/libc.so.6 -> libc-2.23.so

针对32位系统使用命令:

zbb@ubuntu:~$ ls -l /lib/i386-linux-gnu/libc.so.6 
  1. 使用命令

    zbb@ubuntu:~$ apt-cache show libc6
    Package: libc6
    Architecture: amd64
    Version: 2.23-0ubuntu11
    Multi-Arch: same
    Priority: required
    Section: libs
    Source: glibc
    Origin: Ubuntu
    Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
    Original-Maintainer: GNU Libc Maintainers <debian-glibc@lists.debian.org>
    Bugs: https://bugs.launchpad.net/ubuntu/+filebug
    Installed-Size: 10953
    Depends: libgcc1
    Suggests: glibc-doc, debconf | debconf-2.0, locales
    Breaks: hurd (<< 1:0.5.git20140203-1), libtirpc1 (<< 0.2.3), locales (<< 2.23), locales-all (<< 2.23), lsb-core (<= 3.2-27), nscd (<< 2.23)
    Replaces: libc6-amd64
    Filename: pool/main/g/glibc/libc6_2.23-0ubuntu11_amd64.deb
    Size: 2576636
    MD5sum: 9fa554db26eaa705ff02cab4e0289e4f
    SHA1: 6228335ebf0b5426f2d220baaabf1083e1401e8e
    SHA256: 7c802a3051203c32bba4e2e1c6cab831c338c186810bc5cab59fc600a63ac8e0
    Homepage: http://www.gnu.org/software/libc/libc.html
    Description-en: GNU C Library: Shared libraries
     Contains the standard libraries that are used by nearly all programs on
     the system. This package includes shared versions of the standard C library
     and the standard math library, as well as many others.
    Description-md5: fc3001b0b90a1c8e6690b283a619d57f
    Task: minimal
    Supported: 5y
    ...
    

    从中我们可以获取自己需要的libc的版本信息

0x02 CentOS

  1. 使用命令
➜  ~ rpm -qa |grep glibc
glibc-devel-2.17-157.el7_3.5.x86_64
glibc-2.17-157.el7_3.5.x86_64
glibc-common-2.17-157.el7_3.5.x86_64
glibc-headers-2.17-157.el7_3.5.x86_64
  1. 使用命令
➜  ~ ls -l /lib64/libc.so.6 
lrwxrwxrwx 1 root root 12 Aug 18  2017 /lib64/libc.so.6 -> libc-2.17.so

0x03 下载libc源码

源码下载

查找对应版本进行下载源代码

0x04 其他

glibc2.27文档
自glibc2.27起 pow10, pow10f and pow10l 函数被取消

  • The libm functions pow10, pow10f and pow10l are no longer supported for
    new programs. Programs should use the standard names exp10, exp10f and
    exp10l for these functions instead.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值