glibc 知:构建2.34版本

文章目录

  • 获取版本
  • 构建安装
  • 测试
    • testrun.sh
    • strace

获取版本

主页:https://www.gnu.org/software/libc
glibc 知:构建2.34版本_第1张图片
glibc 知:构建2.34版本_第2张图片
glibc 知:构建2.34版本_第3张图片
这里选择任意一个压缩包下载即可。

构建安装

tar -Jxf glibc-2.34.tar.xz
mkdir build install
cd build
../glibc-2.34/configure --prefix=/usr
make -j4
make install DESTDIR=/xxx/xxx/install

1)make后面的-j4,其中4可以根据CPU数进行调整。
2)DESTDIR需要是绝对路径
3)configure阶段可能会检测某些软件不存在,根据错误提示在主机上安装即可
4)如果使用vmware虚拟机,build目录不能在Linux和Windows共享目录下,否则会构建错误

测试

下面写一个打印hello world的c程序,基于构建的glibc-2.34进行执行。

1)程序 test.c

#include 

int main(void)
{
    printf("hello world\n");
    return 0;
}

2)编译

gcc test.c

3)执行
基于build目录下的testrun.sh进行执行

$ /xxx/xxx/build/testrun.sh ./a.out
hello world

在这里插入图片描述

更多测试方法可以参考 glibc 知:构建和测试

testrun.sh

#!/bin/bash
builddir=`dirname "$0"`
GCONV_PATH="${builddir}/iconvdata"

usage () {
cat << EOF
Usage: $0 [OPTIONS]  [ARGUMENTS...]

  --tool=TOOL  Run with the specified TOOL. It can be strace, valgrind or
               container. The container will run within support/test-container.
EOF

  exit 1
}

toolname=default
while test $# -gt 0 ; do
  case "$1" in
    --tool=*)
      toolname="${1:7}"
      shift
      ;;
    --*)
      usage
      ;;
    *)
      break
      ;;
  esac
done

if test $# -eq 0 ; then
  usage
fi

case "$toolname" in
  default)
    exec   env GCONV_PATH="${builddir}"/iconvdata LOCPATH="${builddir}"/localedata LC_ALL=C  "${builddir}"/elf/ld-linux-x86-64.so.2 --library-path "${builddir}":"${builddir}"/math:"${builddir}"/elf:"${builddir}"/dlfcn:"${builddir}"/nss:"${builddir}"/nis:"${builddir}"/rt:"${builddir}"/resolv:"${builddir}"/mathvec:"${builddir}"/support:"${builddir}"/crypt:"${builddir}"/nptl ${1+"$@"}
    ;;
  strace)
    exec strace  -EGCONV_PATH=/home/maminjie/glibc/build/iconvdata  -ELOCPATH=/home/maminjie/glibc/build/localedata  -ELC_ALL=C  /home/maminjie/glibc/build/elf/ld-linux-x86-64.so.2 --library-path /home/maminjie/glibc/build:/home/maminjie/glibc/build/math:/home/maminjie/glibc/build/elf:/home/maminjie/glibc/build/dlfcn:/home/maminjie/glibc/build/nss:/home/maminjie/glibc/build/nis:/home/maminjie/glibc/build/rt:/home/maminjie/glibc/build/resolv:/home/maminjie/glibc/build/mathvec:/home/maminjie/glibc/build/support:/home/maminjie/glibc/build/crypt:/home/maminjie/glibc/build/nptl ${1+"$@"}
    ;;
  valgrind)
    exec env GCONV_PATH=/home/maminjie/glibc/build/iconvdata LOCPATH=/home/maminjie/glibc/build/localedata LC_ALL=C valgrind  /home/maminjie/glibc/build/elf/ld-linux-x86-64.so.2 --library-path /home/maminjie/glibc/build:/home/maminjie/glibc/build/math:/home/maminjie/glibc/build/elf:/home/maminjie/glibc/build/dlfcn:/home/maminjie/glibc/build/nss:/home/maminjie/glibc/build/nis:/home/maminjie/glibc/build/rt:/home/maminjie/glibc/build/resolv:/home/maminjie/glibc/build/mathvec:/home/maminjie/glibc/build/support:/home/maminjie/glibc/build/crypt:/home/maminjie/glibc/build/nptl ${1+"$@"}
    ;;
  container)
    exec env GCONV_PATH=/home/maminjie/glibc/build/iconvdata LOCPATH=/home/maminjie/glibc/build/localedata LC_ALL=C  /home/maminjie/glibc/build/elf/ld-linux-x86-64.so.2 --library-path /home/maminjie/glibc/build:/home/maminjie/glibc/build/math:/home/maminjie/glibc/build/elf:/home/maminjie/glibc/build/dlfcn:/home/maminjie/glibc/build/nss:/home/maminjie/glibc/build/nis:/home/maminjie/glibc/build/rt:/home/maminjie/glibc/build/resolv:/home/maminjie/glibc/build/mathvec:/home/maminjie/glibc/build/support:/home/maminjie/glibc/build/crypt:/home/maminjie/glibc/build/nptl /home/maminjie/glibc/build/support/test-container env GCONV_PATH=/home/maminjie/glibc/build/iconvdata LOCPATH=/home/maminjie/glibc/build/localedata LC_ALL=C  /home/maminjie/glibc/build/elf/ld-linux-x86-64.so.2 --library-path /home/maminjie/glibc/build:/home/maminjie/glibc/build/math:/home/maminjie/glibc/build/elf:/home/maminjie/glibc/build/dlfcn:/home/maminjie/glibc/build/nss:/home/maminjie/glibc/build/nis:/home/maminjie/glibc/build/rt:/home/maminjie/glibc/build/resolv:/home/maminjie/glibc/build/mathvec:/home/maminjie/glibc/build/support:/home/maminjie/glibc/build/crypt:/home/maminjie/glibc/build/nptl ${1+"$@"}
    ;;
  *)
    usage
    ;;
esac

简析如下:

  case "$1" in
    --tool=*)
      toolname="${1:7}"
      shift
      ;;

toolname为参数1的从索引7开始的字符,即去掉–tool=,默认toolname为default,则执行下面命令:
exec env GCONV_PATH="${builddir}"/iconvdata LOCPATH="${builddir}"/localedata LC_ALL=C "${builddir}"/elf/ld-linux-x86-64.so.2 --library-path "${builddir}":"${builddir}"/math:"${builddir}"/elf:"${builddir}"/dlfcn:"${builddir}"/nss:"${builddir}"/nis:"${builddir}"/rt:"${builddir}"/resolv:"${builddir}"/mathvec:"${builddir}"/support:"${builddir}"/crypt:"${builddir}"/nptl ${1+"$@"}

通过 ld-linux-x86-64.so.2 的参数 --library-path 设置库的路径,然后加载程序。

${1+"$@"}:当$1定义了,取$@,否则取$1

ld-linux-x86-64.so.2 的更多参数可以参考 glibc 知:ld.so

strace

maminjie@fedora hello]$ ~/glibc/build/testrun.sh --tool=strace ./a.out 
execve("/home/maminjie/glibc/build/elf/ld-linux-x86-64.so.2", ["/home/maminjie/glibc/build/elf/l"..., "--library-path", "/home/maminjie/glibc/build:/home"..., "./a.out"], 0x55d73ba1d6d0 /* 48 vars */) = 0
brk(NULL)                               = 0x555556d2e000
openat(AT_FDCWD, "./a.out", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\2\0>\0\1\0\0\0@\20@\0\0\0\0\0"..., 832) = 832
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\200\0\300\4\0\0\0\1\0\0\0\0\0\0\0", 32, 824) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\316\235\274\210\275\254]\372\27.\202\255\7\31\206\214"..., 68, 856) = 68
getcwd("/home/maminjie/glibc/test/hello", 128) = 32
mmap(0x400000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0) = 0x400000
mmap(0x401000, 4096, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1000) = 0x401000
mmap(0x402000, 4096, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x402000
mmap(0x403000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x403000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f76f94b5000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/glibc-hwcaps/x86-64-v3/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/glibc-hwcaps/x86-64-v3", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/glibc-hwcaps/x86-64-v2/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/glibc-hwcaps/x86-64-v2", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/tls/haswell/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/tls/haswell/x86_64", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/tls/haswell/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/tls/haswell", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/tls/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/tls/x86_64", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/tls/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/tls", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/haswell/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/haswell/x86_64", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/haswell/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/haswell", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/x86_64/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/home/maminjie/glibc/build/x86_64", 0x7ffef39e8740, 0) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/home/maminjie/glibc/build/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\324\2\0\0\0\0\0"..., 832) = 832
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
pread64(3, "\4\0\0\0\20\0\0\0\5\0\0\0GNU\0\2\200\0\300\4\0\0\0\1\0\0\0\0\0\0\0", 32, 848) = 32
pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\374&\320\320\314/\277I\361\270j\317\266`\246\365"..., 68, 880) = 68
newfstatat(3, "", {st_mode=S_IFREG|0775, st_size=12478512, ...}, AT_EMPTY_PATH) = 0
pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784
mmap(NULL, 2079920, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f76f92b9000
mprotect(0x7f76f92e5000, 1822720, PROT_NONE) = 0
mmap(0x7f76f92e5000, 1470464, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2c000) = 0x7f76f92e5000
mmap(0x7f76f944c000, 348160, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x193000) = 0x7f76f944c000
mmap(0x7f76f94a2000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e8000) = 0x7f76f94a2000
mmap(0x7f76f94a8000, 52400, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f76f94a8000
close(3)                                = 0
mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f76f92b6000
arch_prctl(ARCH_SET_FS, 0x7f76f92b6740) = 0
set_tid_address(0x7f76f92b6a10)         = 64383
set_robust_list(0x7f76f92b6a20, 24)     = 0
mprotect(0x7f76f94a2000, 12288, PROT_READ) = 0
mprotect(0x403000, 4096, PROT_READ)     = 0
mprotect(0x7f76f94e6000, 8192, PROT_READ) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
newfstatat(1, "", {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x1), ...}, AT_EMPTY_PATH) = 0
getrandom("\x10\xe9\x7d\x23\x41\xbe\xe9\x34", 8, GRND_NONBLOCK) = 8
brk(NULL)                               = 0x555556d2e000
brk(0x555556d4f000)                     = 0x555556d4f000
write(1, "hello world\n", 12hello world
)           = 12
exit_group(0)                           = ?
+++ exited with 0 +++
[maminjie@fedora hello]$ 

你可能感兴趣的:(libc,glibc)