ubuntu环境下编译glibc源码遇到的问题

编译问题1

gcc wctomb.c -c -std=gnu11 -fgnu89-inline  -g -O2 -Wall -Wwrite-strings -Wundef -Werror -fmerge-all-constants -frounding-math -fno-stack-protector -fno-common -Wp,-U_FORTIFY_SOURCE -Wstrict-prototypes -Wold-style-definition -fmath-errno    -fPIC  -Wp,-U_FORTIFY_SOURCE  -fcf-protection   -ftls-model=initial-exec     -I../include -I/home/UserName/develop/glibc-2.39/build/stdlib  -I/home/UserName/develop/glibc-2.39/build  -I../sysdeps/unix/sysv/linux/x86_64/64  -I../sysdeps/unix/sysv/linux/x86_64/include -I../sysdeps/unix/sysv/linux/x86_64  -I../sysdeps/unix/sysv/linux/x86/include -I../sysdeps/unix/sysv/linux/x86  -I../sysdeps/x86/nptl  -I../sysdeps/unix/sysv/linux/wordsize-64  -I../sysdeps/x86_64/nptl  -I../sysdeps/unix/sysv/linux/include -I../sysdeps/unix/sysv/linux  -I../sysdeps/nptl  -I../sysdeps/pthread  -I../sysdeps/gnu  -I../sysdeps/unix/inet  -I../sysdeps/unix/sysv  -I../sysdeps/unix/x86_64  -I../sysdeps/unix  -I../sysdeps/posix  -I../sysdeps/x86_64/64  -I../sysdeps/x86_64/fpu/multiarch  -I../sysdeps/x86_64/fpu  -I../sysdeps/x86/fpu  -I../sysdeps/x86_64/multiarch  -I../sysdeps/x86_64  -I../sysdeps/x86/include -I../sysdeps/x86  -I../sysdeps/ieee754/float128  -I../sysdeps/ieee754/ldbl-96/include -I../sysdeps/ieee754/ldbl-96  -I../sysdeps/ieee754/dbl-64  -I../sysdeps/ieee754/flt-32  -I../sysdeps/wordsize-64  -I../sysdeps/ieee754  -I../sysdeps/generic  -I.. -I../libio -I.  -D_LIBC_REENTRANT -include /home/UserName/develop/glibc-2.39/build/libc-modules.h -DMODULE_NAME=libc -include ../include/libc-symbols.h  -DPIC -DSHARED     -DTOP_NAMESPACE=glibc -o /home/UserName/develop/glibc-2.39/build/stdlib/wctomb.os -MD -MP -MF /home/UserName/develop/glibc-2.39/build/stdlib/wctomb.os.dt -MT /home/UserName/develop/glibc-2.39/build/stdlib/wctomb.os
wctomb.c:57:1: error: ‘artificial’ attribute ignored [-Werror=attributes]
   57 | libc_hidden_def (wctomb)
      | ^~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[2]: *** [../o-iterator.mk:9: /home/UserName/develop/glibc-2.39/build/stdlib/wctomb.os] Error 1
make[2]: Leaving directory '/home/UserName/develop/glibc-2.39/stdlib'
make[1]: *** [Makefile:484: stdlib/subdir_lib] Error 2
make[1]: Leaving directory '/home/UserName/develop/glibc-2.39'
make: *** [Makefile:9: all] Error 2

临时解决办法:

../configure 加 --disable-werror 

编译问题2

gcc syslog.c -c -std=gnu11 -fgnu89-inline  -g -O2 -Wall -Wwrite-strings -Wundef -fmerge-all-constants -frounding-math -fno-stack-protector -fno-common -Wp,-U_FORTIFY_SOURCE -Wstrict-prototypes -Wold-style-definition -fmath-errno    -fPIE  -Wp,-U_FORTIFY_SOURCE  -fcf-protection -fexceptions  -ftls-model=initial-exec     -I../include -I/home/UserName/develop/glibc-2.39/build/misc  -I/home/UserName/develop/glibc-2.39/build  -I../sysdeps/unix/sysv/linux/x86_64/64  -I../sysdeps/unix/sysv/linux/x86_64/include -I../sysdeps/unix/sysv/linux/x86_64  -I../sysdeps/unix/sysv/linux/x86/include -I../sysdeps/unix/sysv/linux/x86  -I../sysdeps/x86/nptl  -I../sysdeps/unix/sysv/linux/wordsize-64  -I../sysdeps/x86_64/nptl  -I../sysdeps/unix/sysv/linux/include -I../sysdeps/unix/sysv/linux  -I../sysdeps/nptl  -I../sysdeps/pthread  -I../sysdeps/gnu  -I../sysdeps/unix/inet  -I../sysdeps/unix/sysv  -I../sysdeps/unix/x86_64  -I../sysdeps/unix  -I../sysdeps/posix  -I../sysdeps/x86_64/64  -I../sysdeps/x86_64/fpu/multiarch  -I../sysdeps/x86_64/fpu  -I../sysdeps/x86/fpu  -I../sysdeps/x86_64/multiarch  -I../sysdeps/x86_64  -I../sysdeps/x86/include -I../sysdeps/x86  -I../sysdeps/ieee754/float128  -I../sysdeps/ieee754/ldbl-96/include -I../sysdeps/ieee754/ldbl-96  -I../sysdeps/ieee754/dbl-64  -I../sysdeps/ieee754/flt-32  -I../sysdeps/wordsize-64  -I../sysdeps/ieee754  -I../sysdeps/generic  -I.. -I../libio -I.  -D_LIBC_REENTRANT -include /home/UserName/develop/glibc-2.39/build/libc-modules.h -DMODULE_NAME=libc -include ../include/libc-symbols.h  -DPIC     -DTOP_NAMESPACE=glibc -o /home/UserName/develop/glibc-2.39/build/misc/syslog.o -MD -MP -MF /home/UserName/develop/glibc-2.39/build/misc/syslog.o.dt -MT /home/UserName/develop/glibc-2.39/build/misc/syslog.o
In file included from <command-line>:
syslog.c: In function ‘__vsyslog_internal’:
syslog.c:95:30: error: inlining failed in call to ‘always_inline’ ‘syslog’: function not inlinable
   95 | ldbl_strong_alias (__syslog, syslog)
      |                              ^~~~~~
./../include/libc-symbols.h:143:26: note: in definition of macro ‘_strong_alias’
  143 |   extern __typeof (name) aliasname __attribute__ ((alias (#name))) \
      |                          ^~~~~~~~~
../sysdeps/generic/math_ldbl_opt.h:14:44: note: in expansion of macro ‘strong_alias’
   14 | #define ldbl_strong_alias(name, aliasname) strong_alias (name, aliasname)
      |                                            ^~~~~~~~~~~~
syslog.c:95:1: note: in expansion of macro ‘ldbl_strong_alias’
   95 | ldbl_strong_alias (__syslog, syslog)
      | ^~~~~~~~~~~~~~~~~
syslog.c:138:7: note: called from here
  138 |       syslog (INTERNALLOG, "syslog: unknown facility/priority: %x", pri);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [../o-iterator.mk:9: /home/UserName/develop/glibc-2.39/build/misc/syslog.o] Error 1
make[2]: Leaving directory '/home/UserName/develop/glibc-2.39/misc'
make[1]: *** [Makefile:484: misc/subdir_lib] Error 2
make[1]: Leaving directory '/home/UserName/develop/glibc-2.39'
make: *** [Makefile:9: all] Error 2

临时解决办法:

注掉 syslog.c:138: syslog (INTERNALLOG, "syslog: unknown facility/priority: %x", pri); 

编译问题3

gcc   -nostdlib -nostartfiles -shared -o /home/UserName/develop/glibc-2.39/build/elf/ld.so.new        \
      -Wl,-z,relro -Wl,-z,nomark-plt -Wl,-z,defs     \
      -Wl,-z,pack-relative-relocs \
      /home/UserName/develop/glibc-2.39/build/elf/librtld.os -Wl,--version-script=/home/UserName/develop/glibc-2.39/build/ld.map        \
      -Wl,-soname=ld-linux-x86-64.so.2
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memcpy':
/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x1add): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x1b0c): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x2256): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x2649): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x4964): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os:/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29: more undefined references to `__memcpy_chk' follow
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memset':
/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:59:(.text+0x101c6): undefined reference to `__memset_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:59:(.text+0x1031b): undefined reference to `__memset_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memmove':
/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:36:(.text+0x10360): undefined reference to `__memmove_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memset':
/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:59:(.text+0x10ac9): undefined reference to `__memset_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memcpy':
/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x10b0f): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x137af): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memset':
/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:59:(.text+0x13d29): undefined reference to `__memset_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:59:(.text+0x1962a): undefined reference to `__memset_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memcpy':
/home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x1b751): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x1e961): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x1fb22): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/elf/../string/bits/string_fortified.h:29:(.text+0x20270): undefined reference to `__memcpy_chk'
/usr/bin/ld: /home/UserName/develop/glibc-2.39/build/elf/librtld.os: in function `memcpy':
/home/UserName/develop/glibc-2.39/string/../string/bits/string_fortified.h:29:(.text+0x23e77): undefined reference to `__memcpy_chk'
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:1332: /home/UserName/develop/glibc-2.39/build/elf/ld.so] Error 1
make[2]: Leaving directory '/home/UserName/develop/glibc-2.39/elf'
make[1]: *** [Makefile:484: elf/subdir_lib] Error 2
make[1]: Leaving directory '/home/UserName/develop/glibc-2.39'
make: *** [Makefile:9: all] Error 2

尝试 --disable-hidden-plt
尝试 --disable-option-checking
尝试 --enable-fortify-source=1
尝试 --disable-fortify-source
尝试 --disable-default-pie
都未解决

最终解决办法

sudo apt install glibc-source
cd /usr/src/glibc
sudo tar -xvf glibc-2.39.tar.xz
cd glibc-2.39
sudo mkdir build
cd build
sudo ../configure --prefix=/usr/src/glibc/glibc-2.39
sudo make

 根因分析

直接的差异在于官网下载的glibc-2.39.tar.xz和apt install glibc-source下载的glibc-2.39.tar.xz中的 configure 文件不同。

进一步查看glibc代码仓提交记录: 

引入问题的提交 https://sourceware.org/git/?p=glibc.git;a=commit;h=64d9580cdf7e417170abbef0327e04b29712e949
解决问题的提交 https://sourceware.org/git/?p=glibc.git;a=commit;h=127cefd84dc551a43d0ac58b4619503d04869042

Do not use -Wp to disable fortify (BZ 31928)

The -Wp does not work properly if the compiler is configured to enable
fortify by default, since it bypasses the compiler driver (which defines
the fortify flags in this case).

This patch is similar to the one used on Ubuntu [1].

I checked with a build for x86_64-linux-gnu, i686-linux-gnu,
aarch64-linux-gnu, s390x-linux-gnu, and riscv64-linux-gnu with
gcc-13 that enables the fortify by default.


上述所有问题都可以用这个patch解决
https://git.launchpad.net/~ubuntu-core-dev/ubuntu/+source/glibc/tree/debian/patches/ubuntu/fix-fortify-source.patch

这也就是为什么用apt install glibc-source下载的代码没有问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值