glibc库版本查看和动态库增加可执行属性

glibc库版本查看和动态库增加可执行属性

1.查看当前glibc版本

查看当前系统的glibc版本:

#/lib/libc.so.6
#/lib/x86-64-linux/libc.so.6
➜  ~ /lib64/libc.so.6
GNU C Library (GNU libc) stable release version 2.17, by Roland McGrath et al.
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.8.5 20150623 (Red Hat 4.8.5-44).
Compiled on a Linux 3.10.0 system on 2021-02-08.
Available extensions:
        The C stubs add-on version 2.1.2.
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
        RT using linux kernel aio
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

# 因为ldd命令也是glibc提供的,所以也能查看
➜  ~ ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

为什么这个库可以直接run呢? 原来在libc的代码中有一点小手脚:

Makerules:708:
# Give libc.so an entry point and make it directly runnable itself.
LDFLAGS-c.so += -e __libc_main

查看__libc_main函数的实现

/* Copyright (C) 1992-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include "version.h"
#include <tls.h>
#include <libc-abis.h>
#include <gnu/libc-version.h>

static const char __libc_release[] = RELEASE;
static const char __libc_version[] = VERSION;

static const char banner[] =
"GNU C Library "PKGVERSION RELEASE" release version "VERSION".\n\
Copyright (C) 2018 Free Software Foundation, Inc.\n\
This is free software; see the source for copying conditions.\n\
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
PARTICULAR PURPOSE.\n\
Compiled by GNU CC version "__VERSION__".\n"
#ifdef LIBC_ABIS_STRING
LIBC_ABIS_STRING
#endif
"For bug reporting instructions, please see:\n\
"REPORT_BUGS_TO".\n";

#include <unistd.h>

extern void __libc_print_version (void) attribute_hidden;
void
__libc_print_version (void)
{
  __write (STDOUT_FILENO, banner, sizeof banner - 1);
}

extern const char *__gnu_get_libc_release (void);
const char *
__gnu_get_libc_release (void)
{
  return __libc_release;
}
weak_alias (__gnu_get_libc_release, gnu_get_libc_release)

extern const char *__gnu_get_libc_version (void);
const char *
__gnu_get_libc_version (void)
{
  return __libc_version;
}
weak_alias (__gnu_get_libc_version, gnu_get_libc_version)

/* This function is the entry point for the shared object.
   Running the library as a program will get here.  */

extern void __libc_main (void) __attribute__ ((noreturn));
void
__libc_main (void)
{
  __libc_print_version ();
  _exit (0);
}

2. 动态库增加版本信息

文件列表

.
├── const_static.cpp
├── const_static.h
└── test.so

const_static.*源文件

// const_static.h
#include <iostream>
#include <unistd.h>
#include <stdio.h>

class Test
{
public:
    int GetIndex();

    void Print(const int & index);

private:
    static const int index_ = 10;
};


extern "C"
{

void lib_main (void) __attribute__ ((noreturn));

#ifdef __LP64__
char const __invoke_dynamic_linker__[] __attribute__ ((section (".interp"))) = "/lib64/ld-linux-x86-64.so.2";
#else
char const __invoke_dynamic_linker__[] __attribute__ ((section (".interp"))) = "/lib/ld-linux.so.2";
#endif

void lib_main(void)
{
    printf("Test.so Version 0.0.1\n");
    _exit(0);
}
}

// const_static.cpp
#include "const_static.h"

const int Test::index_;
int Test::GetIndex()
{
    std::cout << "helllo world " << std::endl;
    Print(index_);
    return 0;
}

void Test::Print(const int & index)
{
    std::cout << "index: " << index << std::endl;
}

编译并执行库:

➜  test_const_static g++ -Wall -Wextra -fPIC -shared const_static.h const_static.cpp -o test.so -e lib_main
➜  test_const_static ls
const_static.cpp  const_static.h  readme.txt  test.so
➜  test_const_static ./test.so
Test.so Version 0.0.1

注意: 在lib_main中使用std::cout输出时运行报segmentfault,lib_main需要使用C函数

3. 源码下载

glibc源码下载地址
http://ftp.gnu.org/pub/gnu/glibc/
http://www.gnu.org/software/libc/libc.html
http://mirrors.nju.edu.cn/gnu/libc/
http://ftp.ntu.edu.tw/gnu/glibc/
http://mirrors.syringanetworks.net/gnu/libc/
http://alpha.gnu.org/gnu/glibc/

在线源码阅读
https://code.woboq.org/userspace/glibc/
https://elixir.bootlin.com/glibc/glibc-2.31/source


Reference:

[1]. https://unix.stackexchange.com/questions/223385/why-and-how-are-some-shared-libraries-runnable-as-though-they-are-executables

[2]. https://stackoverflow.com/questions/57066758/how-am-i-able-to-run-a-shared-library-as-an-executable-from-the-terminal

[3]. https://zgserver.com/gnu-libc-so.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Erice_s

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

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

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

打赏作者

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

抵扣说明:

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

余额充值