设置1:在没有专用GCC的情况下编译自己的glibc并使用它
这种设置可能有效,而且很快,因为它不需要重新编译整个GCC工具链,只需要重新编译glibc。
但它不可靠,因为它使用宿主C运行时对象,如crt1。o, crti。哦,还有crtn。O由glibc提供。这在https://sourceware.org/glibc/wiki/Testing/Builds?action=recall&rev=21#Compile_against_glibc_in_an_installed_location中提到过,这些对象做了glibc所依赖的早期设置,所以如果事情以奇妙而微妙的方式崩溃,我不会感到惊讶。
要获得更可靠的设置,请参阅下面的设置2。
构建glibc并在本地安装:
export glibc_install="$(pwd)/glibc/build/install"
git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout glibc-2.28
mkdir build
cd build
../configure --prefix "$glibc_install"
make -j `nproc`
make install -j `nproc`
设置1:验证构建
test_glibc.c
#define _GNU_SOURCE
#include <assert.h>
#include <gnu/libc-version.h>
#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>
atomic_int acnt;
int cnt;
int f(void* thr_data) {
for(int n = 0; n < 1000; ++n) {
++cnt;
++acnt;
}
return 0;
}
int main(int argc, char **argv) {
/* Basic library version check. */
printf("gnu_get_libc_version() = %s\n", gnu_get_libc_version());
/* Exercise thrd_create from -pthread,
* which is not present in glibc 2.27 in Ubuntu 18.04.
* https://stackoverflow.com/questions/56810/how-do-i-start-threads-in-plain-c/52453291#52453291 */
thrd_t thr[10];
for(int n = 0; n < 10; ++n)
thrd_create(&thr[n], f, NULL);
for(int n = 0; n < 10; ++n)
thrd_join(thr[n], NULL);
printf("The atomic counter is %u\n", acnt);
printf("The non-atomic counter is %u\n", cnt);
}
使用test_glibc.sh编译并运行:
#!/usr/bin/env bash
set -eux
gcc \
-L "${glibc_install}/lib" \
-I "${glibc_install}/include" \
-Wl,--rpath="${glibc_install}/lib" \
-Wl,--dynamic-linker="${glibc_install}/lib/ld-linux-x86-64.so.2" \
-std=c11 \
-o test_glibc.out \
-v \
test_glibc.c \
-pthread \
;
ldd ./test_glibc.out
./test_glibc.out
程序输出预期的结果:
gnu_get_libc_version() = 2.28
The atomic counter is 10000
The non-atomic counter is 8674
命令改编自https://sourceware.org/glibc/wiki/Testing/Builds?action=recall&rev=21#Compile_against_glibc_in_an_installed_location but——sysroot使其失败:
cannot find /home/ciro/glibc/build/install/lib/libc.so.6 inside /home/ciro/glibc/build/install
所以我把它拿走了。
LDD输出确认了我们刚刚构建的LDD和库实际上正在按预期使用:
+ ldd test_glibc.out
linux-vdso.so.1 (0x00007ffe4bfd3000)
libpthread.so.0 => /home/ciro/glibc/build/install/lib/libpthread.so.0 (0x00007fc12ed92000)
libc.so.6 => /home/ciro/glibc/build/install/lib/libc.so.6 (0x00007fc12e9dc000)
/home/ciro/glibc/build/install/lib/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x00007fc12f1b3000)
gcc编译调试输出显示我的主机运行时对象被使用,这是坏的,如前所述,但我不知道如何解决它,例如它包含:
COLLECT_GCC_OPTIONS=/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/crt1.o
设置1:修改glibc
现在让我们修改glibc:
diff --git a/nptl/thrd_create.c b/nptl/thrd_create.c
index 113ba0d93e..b00f088abb 100644
--- a/nptl/thrd_create.c
+++ b/nptl/thrd_create.c
@@ -16,11 +16,14 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
+#include <stdio.h>
+
#include "thrd_priv.h"
int
thrd_create (thrd_t *thr, thrd_start_t func, void *arg)
{
+ puts("hacked");
_Static_assert (sizeof (thr) == sizeof (pthread_t),
"sizeof (thr) != sizeof (pthread_t)");
然后重新编译并重新安装glibc,重新编译并重新运行我们的程序:
cd glibc/build
make -j `nproc`
make -j `nproc` install
./test_glibc.sh
我们看到黑黑客像预期的那样印刷了几次。
这进一步证实了我们实际上使用的是我们编译的glibc,而不是宿主的glibc。
在Ubuntu 18.04上测试。
设置2:crosstool-NG原始设置
这是设置1的替代方案,而且它是我迄今为止实现的最正确的设置:据我观察,一切都是正确的,包括crt1等C运行时对象。o, crti。O和crtn.o。
在这个设置中,我们将编译一个使用我们想要的glibc的完整专用GCC工具链。
这种方法的唯一缺点是构建将花费更长的时间。但我不会冒险用其他东西来制作。
crosstool-NG是一组脚本,可以从源代码下载和编译所有东西,包括GCC、glibc和binutils。
是的,GCC的构建系统是如此糟糕,我们需要一个单独的项目。
这种设置并不完美,因为crosstool-NG不支持构建没有额外-Wl标记的可执行文件,这感觉很奇怪,因为我们已经构建了GCC本身。但一切似乎都可以工作,所以这只是一个不便。
获取crosstool-NG,配置并构建它:
git clone https://github.com/crosstool-ng/crosstool-ng
cd crosstool-ng
git checkout a6580b8e8b55345a5a342b5bd96e42c83e640ac5
export CT_PREFIX="$(pwd)/.build/install"
export PATH="/usr/lib/ccache:${PATH}"
./bootstrap
./configure --enable-local
make -j `nproc`
./ct-ng x86_64-unknown-linux-gnu
./ct-ng menuconfig
env -u LD_LIBRARY_PATH time ./ct-ng build CT_JOBS=`nproc`
构建大约需要30分钟到2个小时。
我能看到的唯一强制性配置选项是使它与您的主机内核版本相匹配,以使用正确的内核头文件。使用以下命令查找您的主机内核版本:
uname -a
这告诉我:
4.15.0-34-generic
所以在菜单中我这样做:
操作系统
linux版本
所以我选择:
4.14.71
这是第一个相同或更老的版本。它必须是旧的,因为内核是向后兼容的。
设置2:可选配置
我们用。/ct-ng x86_64-unknown-linux-gnu生成的.config有:
CT_GLIBC_V_2_27=y
要改变这一点,在menuconfig中执行:
c库
glibc的版本
保存.config,并继续构建。
或者,如果你想使用你自己的glibc源代码,比如从最新的git中使用glibc,可以这样做:
路径和misc选项
尝试标记为EXPERIMENTAL的特性:设置为true
c库
glibc的来源
自定义位置:说“是”
自定义位置
自定义源位置:指向包含glibc源的目录
glibc被克隆为:
git clone git://sourceware.org/git/glibc.git
cd glibc
git checkout glibc-2.28
设置2:测试它
一旦你建立了你想要的工具链,用以下方法测试它:
#!/usr/bin/env bash
set -eux
install_dir="${CT_PREFIX}/x86_64-unknown-linux-gnu"
PATH="${PATH}:${install_dir}/bin" \
x86_64-unknown-linux-gnu-gcc \
-Wl,--dynamic-linker="${install_dir}/x86_64-unknown-linux-gnu/sysroot/lib/ld-linux-x86-64.so.2" \
-Wl,--rpath="${install_dir}/x86_64-unknown-linux-gnu/sysroot/lib" \
-v \
-o test_glibc.out \
test_glibc.c \
-pthread \
;
ldd test_glibc.out
./test_glibc.out
除了现在使用了正确的运行时对象之外,一切都像在设置1中一样工作:
COLLECT_GCC_OPTIONS=/home/ciro/crosstool-ng/.build/install/x86_64-unknown-linux-gnu/bin/../x86_64-unknown-linux-gnu/sysroot/usr/lib/../lib64/crt1.o
设置2:高效的glibc重新编译尝试失败
这似乎不可能与crosstool-NG,如下所述。
如果你只是重建;
env -u LD_LIBRARY_PATH time ./ct-ng build CT_JOBS=`nproc`
然后考虑对自定义glibc源位置的更改,但它从头构建所有内容,使其无法用于迭代开发。
如果我们这样做:
./ct-ng list-steps
它给出了构建步骤的一个很好的概述:
Available build steps, in order:
- companion_tools_for_build
- companion_libs_for_build
- binutils_for_build
- companion_tools_for_host
- companion_libs_for_host
- binutils_for_host
- cc_core_pass_1
- kernel_headers
- libc_start_files
- cc_core_pass_2
- libc
- cc_for_build
- cc_for_host
- libc_post_cc
- companion_libs_for_target
- binutils_for_target
- debug
- test_suite
- finish
Use "<step>" as action to execute only that step.
Use "+<step>" as action to execute up to that step.
Use "<step>+" as action to execute from that step onward.
因此,我们看到glibc步骤与几个GCC步骤纠缠在一起,最明显的是libc_start_files在cc_core_pass_2之前,这可能是与cc_core_pass_1一起最昂贵的步骤。
为了只构建一个步骤,你必须首先在初始构建的.config选项中设置“保存中间步骤”:
路径和misc选项
调试crosstool-NG
节省中间步骤
然后你可以试试:
env -u LD_LIBRARY_PATH time ./ct-ng libc+ -j`nproc`
但不幸的是,+需要提到:https://github.com/crosstool-ng/crosstool-ng/issues/1033#issuecomment-424877536
但是请注意,在中间步骤重新启动会将安装目录重置为该步骤期间的状态。也就是说,你将得到一个重新构建的libc -但没有使用这个libc构建的最终编译器(因此,也没有像libstdc++这样的编译器库)。
基本上仍然使重建太慢,开发是可行的,我不知道如何克服这个没有修补crosstool-NG。
此外,从libc步骤开始似乎不会从自定义源位置再次复制源代码,进一步使该方法不可用。
奖励:stdlibc++
如果你也对c++标准库感兴趣,还有一个额外的好处:如何编辑和重新构建GCC libstdc++ c++标准库源代码?