glibc 知:构建机制

【日常记录】解决‘GLIBC_2.34‘ not found,并且gcc制定glibc版本编译 阅读详情

1. 前言

文本结合glibc-2.34的构建过程,解读其Makefile是如何工作的。

参考“glibc 知:构建2.34版本”的构建方法:

mkdir -p glibc-2.34/build
cd glibc-2.34/build
/xxxx/glibc-2.34/configure --prefix=/usr
make -j8 2>&1 | tee ../build.log

作者水平有限,只能渐进明细,本文会不断更新迭代。

1.1. 执行配置命令

执行configure之后,构建目录下生成如下一些文件:

[maminjie@fedora build]$ /mnt/hgfs/projects/opensrc/glibc/glibc-2.34/configure --prefix=/usr
[maminjie@fedora build]$ ls
bits  config.h  config.log  config.make  config.status  Makefile

在当前目录下,直接执行make命令就可以进行构建glibc了。不过,这里我们先不构建,先来看一下配置生成的Makefile内容:

srcdir = /mnt/hgfs/projects/opensrc/glibc/glibc-2.34

# Uncomment the line below if you want to do parallel build.
# PARALLELMFLAGS = -j 4

.PHONY: all install bench

all .DEFAULT:
    $(MAKE) -r PARALLELMFLAGS="$(PARALLELMFLAGS)" -C $(srcdir) objdir=`pwd` $@

install:
    LC_ALL=C; export LC_ALL; \
    $(MAKE) -r PARALLELMFLAGS="$(PARALLELMFLAGS)" -C $(srcdir) objdir=`pwd` $@

bench bench-clean bench-build:
    $(MAKE) -C $(srcdir)/benchtests $(PARALLELMFLAGS) objdir=`pwd` $@

# Convenience target to rebuild ULPs for all math tests.
regen-ulps:
    $(MAKE) -C $(srcdir)/math $(PARALLELMFLAGS) objdir=`pwd` $@

该Makefile内容很简单,终极目标all的执行命令是到glibc的源码目录下执行真正的Makefile。

1.2. 生成构建日志

执行make生成构建日志,如下所示:

$ make -j8 2>&1 | tee ../build.log
$ ls
abi-versions.h            jmp_buf-ssp.h          linkobj                  soversions.mk
argp                      jmp_buf-ssp.h.d        locale                   stamp.o
assert                    ldbl-compat-choose.h   localedata               stamp.os
bits                      ld.map                 locale-defines.h         stamp.oS
catgets                   libanl.map             locale-defines.h.d       stdio-common
config.h                  libBrokenLocale.map    login                    stdlib
config.log                libc.a                 Makefile                 string
config.make               libc-abis.h            malloc                   sunrpc
config.status             libc-abis.stamp        manual                   support
conform                   libc_malloc_debug.map  math                     sysd-rules
cpu-features-offsets.h    libc.map               mathvec                  sysd-sorted
cpu-features-offsets.h.d  libc-modules.h         misc                     sysd-syscalls
crypt                     libc-modules.stmp      nis                      sysd-versions
csu                       libc_nonshared.a       nptl                     sysvipc
ctype                     libc_pic.a             nptl_db                  tcb-offsets.h
debug                     libc_pic.opts          nscd                     tcb-offsets.h.d
debugglibc.sh             libc_pic.os            nss                      termios
dirent                    libc_pic.os.clean      po                       testrun.sh
dlfcn                     libcrypt.map           posix                    time
dl-tunable-list.h         libc.so                pthread-pi-defines.h     time64-compat.mk
dl-tunable-list.stmp      libc.so.6              pthread-pi-defines.h.d   time64-compat.mk.i
dummy.c                   libdl.map              pwd                      timezone
dummy.o                   libio                  resolv                   tlsdesc.h
dummy.o.dt                libm.map               resource                 tlsdesc.h.d
elf                       libmvec.map            rt                       ucontext_i.h
first-versions.h          libnsl.map             rtld-offsets.h           ucontext_i.h.d
format.lds                libnss_compat.map      rtld-offsets.h.d         unwindbuf.h
gmon                      libnss_db.map          runtime-linker.h         unwindbuf.h.d
gnu                       libnss_dns.map         runtime-linker.stamp     Versions.all
gnulib                    libnss_files.map       setjmp                   Versions.def
grp                       libnss_hesiod.map      shadow                   Versions.mk
gshadow                   libpthread.map         shlib-versions.v         versions.stmp
hesiod                    libresolv.map          shlib-versions.v.i       Versions.tmp
iconv                     librt.map              sigaltstack-offsets.h    Versions.v
iconvdata                 libthread_db.map       sigaltstack-offsets.h.d  Versions.v.i
inet                      libutil.map            signal                   wcsmbs
intl                      link-defines.h         socket                   wctype
io                        link-defines.h.d       soversions.i

build.log日志文件的前面内容如下所示:

ake -r PARALLELMFLAGS="" -C /mnt/hgfs/projects/opensrc/glibc/glibc-2.34 objdir=`pwd` all
make[1]: Entering directory '/mnt/hgfs/projects/opensrc/glibc/glibc-2.34'
LC_ALL=C gawk -f scripts/sysd-rules.awk > /home/maminjie/glibc-2.34/build/sysd-rulesT \
        -v all_object_suffixes='.o .os .oS' \
        -v inhibit_sysdep_asm='' \
        -v sysd_rules_patterns='%:% rtld-%:rtld-% rtld-%:% m_%:s_%' \
        -v config_sysdirs='sysdeps/unix/sysv/linux/x86_64/64 sysdeps/unix/sysv/linux/x86_64 sysdeps/unix/sysv/linux/x86 sysdeps/x86/nptl sysdeps/unix/sysv/linux/wordsize-64 sysdeps/x86_64/nptl sysdeps/unix/sysv/linux sysdeps/nptl sysdeps/pthread sysdeps/gnu sysdeps/unix/inet sysdeps/unix/sysv sysdeps/unix/x86_64 sysdeps/unix sysdeps/posix sysdeps/x86_64/64 sysdeps/x86_64/fpu/multiarch sysdeps/x86_64/fpu sysdeps/x86/fpu sysdeps/x86_64/multiarch sysdeps/x86_64 sysdeps/x86 sysdeps/ieee754/float128 sysdeps/ieee754/ldbl-96 sysdeps/ieee754/dbl-64 sysdeps/ieee754/flt-32 sysdeps/wordsize-64 sysdeps/ieee754 sysdeps/generic'
mv -f /home/maminjie/glibc-2.34/build/sysd-rulesT /home/maminjie/glibc-2.34/build/sysd-rules
gawk -f scripts/gen-sorted.awk \
       -v subdirs='csu assert ctype locale intl catgets math setjmp signal stdlib stdio-common libio malloc string wcsmbs time dirent grp pwd posix io termios resource misc socket sysvipc gmon gnulib iconv iconvdata wctype manual shadow gshadow po argp localedata timezone rt conform debug mathvec support dlfcn elf crypt' \
       -v srcpfx='' \
       sysdeps/nptl/Subdirs sysdeps/un
...

2. 构建

2.1. 总体机制

下面将通过分析glibc源码目录下的顶层Makefile,Makeconfig,Makerules,Rules等来解读glibc的构建机制。

2.1.1. 默认规则

$ grep -n "^include" Makefile
25:include Makeconfig
89:include Makerules

$ grep -nw "all:" Makeconfig Makerules Makefile
Makeconfig:26:all: # Make this the default goal
Makeconfig:1237:$(common-objpfx)Versions.all: $(..)scripts/firstversions.awk \
Makefile:30:all: minihelp lib others

2.1.2. lib规则

$ grep -nw "lib:" Makeconfig Makerules Makefile
Makerules:835:lib: lib-noranlib $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))
Makerules:981:install-lib.so := $(filter %.so,$(install-lib:%_pic.a=%.so))
Makefile:131:lib: $(common-objpfx)libc.so $(common-objpfx)linkobj/libc.so

$ grep -nw "lib-noranlib:" Makeconfig Makerules Makefile
Makerules:836:lib-noranlib: libobjs
Makefile:127:lib-noranlib: subdir_lib

$ grep -nw "libobjs:" Makeconfig Makerules Makefile
Makerules:896:libobjs: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)

目标lib的依赖分别是:stamp$o,subdir_lib,libtypes,libc.so几个。

2.1.3. stamp规则

stamp%o是glibc中间目标文件的时间戳文件

$ grep -n stamp Makeconfig Makerules Makefile
Makerules:294:# We use a stamp file to avoid unnecessary recompilations.
Makerules:849:# Create the stamp$o files to keep the parent makefile happy.
Makerules:850:subdir_lib: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
Makerules:851:$(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o):
Makerules:856:# Define explicit rules to update each $(objpfx)stamp.SUFFIX
Makerules:857:# timestamp file; these rules (one explicit rule is generated for each
Makerules:858:# object suffix) write a list of objects to update in the stamp file.
Makerules:862:$(objpfx)stamp$o: $(o-objects); $$(do-stamp)
Makerules:864:define do-stamp
Makerules:875:# on the stamp files built above.
Makerules:878:          $(subdirs-stamp-o) $(common-objpfx)stamp$o; $$(do-makelib)
Makerules:884:subdirs-stamps := $(foreach d,$(subdirs),$(common-objpfx)$d/stamp%)
Makerules:885:subdirs-stamp-o = $(subst %,$o,$(subdirs-stamps))
Makerules:887:$(subdirs-stamps): subdir_lib;
Makerules:896:libobjs: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
Makerules:1464:-rm -f $(objpfx)stamp$o $(o-objects))

Makerules文件中关于stamp内容如下:

# For object-suffix $o, the list of objects with that suffix.
# Makefiles can define `elide-routines.so = foo' to leave foo.so out.
o-objects = $(patsubst %.o,%$o,$(filter-out $(patsubst %,$(objpfx)%.o,\
						       $(elide-routines$o)),\
					    $(objects))) \
	    $(addprefix $(objpfx),$(o-objects$o))

others: $(addprefix $(objpfx),$(install-lib))

ifndef objects

# Create the stamp$o files to keep the parent makefile happy.
subdir_lib: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
$(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o):
	$(make-target-directory)
	rm -f $@; > $@
else

# Define explicit rules to update each $(objpfx)stamp.SUFFIX
# timestamp file; these rules (one explicit rule is generated for each
# object suffix) write a list of objects to update in the stamp file.
# The parent will then actually add them all to the archive in the
# archive rule, below.
define o-iterator-doit
$(objpfx)stamp$o: $(o-objects); $$(do-stamp)
endef
define do-stamp
$(make-target-directory)
echo '$(patsubst $(objpfx)%,$(addsuffix /,$(subdir))%,$^)' > $@T
mv -f $@T $@
endef
object-suffixes-left := $(object-suffixes-for-libc)
include $(o-iterator)

endif

根据构建过程,结合构建规则,实际glibc各子目录下生成的stamp%o文件内容为各子目录编译的中间o文件列表。

以csu子目录为例:

[maminjie@fedora csu]$ ls
abi-note.o       dso_handle.o.dt   gmon-start.os.dt  Mcrt1.o             sysdep.o
abi-note.o.dt    dso_handle.os     init-first.o      Scrt1.o             sysdep.o.dt
abi-tag.h        dso_handle.os.dt  init-first.o.dt   stamp.o             sysdep.os
check_fds.o      errno-loc.o       init-first.os     stamp.os            sysdep.os.dt
check_fds.o.dt   errno-loc.o.dt    init-first.os.dt  stamp.oS            unwind-resume.os
check_fds.os     errno-loc.os      init.o            start.o             unwind-resume.os.dt
check_fds.os.dt  errno-loc.os.dt   init.o.dt         start.o.dt          version.o
crt1.o           errno.o           libc-start.o      start.os            version.o.dt
crti.o           errno.o.dt        libc-start.o.dt   start.os.dt         version.os
crti.o.dt        errno.os          libc-start.os     static-reloc.o      version.os.dt
crtn.o           errno.os.dt       libc-start.os.dt  static-reloc.o.dt
crtn.o.dt        gcrt1.o           libc-tls.o        static-reloc.os
dso_handle.o     gmon-start.os     libc-tls.o.dt     static-reloc.os.dt
[maminjie@fedora csu]$ cat stamp.o
csu/init-first.o csu/libc-start.o csu/sysdep.o csu/version.o csu/check_fds.o csu/libc-tls.o csu/dso_handle.o csu/errno.o csu/errno-loc.o
[maminjie@fedora csu]$ cat stamp.os
csu/init-first.os csu/libc-start.os csu/sysdep.os csu/version.os csu/check_fds.os csu/dso_handle.os csu/unwind-resume.os csu/errno.os csu/errno-loc.os

2.1.4. subdir_lib规则

$ grep -nw "subdir_lib" Makeconfig Makerules Makefile
Makerules:850:subdir_lib: $(foreach o,$(object-suffixes-for-libc),$(objpfx)stamp$o)
Makerules:887:$(subdirs-stamps): subdir_lib;
Makefile:61:+subdir_targets     := subdir_lib objects objs others subdir_mostlyclean    \
Makefile:127:lib-noranlib: subdir_lib

Makefile文件中关于subdir_lib的主要内容如下:

# These are the targets that are made by making them in each subdirectory.
+subdir_targets	:= subdir_lib objects objs others subdir_mostlyclean	\
		   subdir_clean subdir_distclean subdir_realclean	\
		   tests xtests						\
		   subdir_update-abi subdir_check-abi			\
		   subdir_update-all-abi				\
		   subdir_echo-headers					\
		   subdir_install					\
		   subdir_objs subdir_stubs subdir_testclean		\
		   $(addprefix install-, no-libc.a bin lib data headers others)

...
# For each target, make it depend on DIR/target for each subdirectory DIR.
$(+subdir_targets): %: $(addsuffix /%,$(subdirs))

# Compute a list of all those targets.
all-subdirs-targets := $(foreach dir,$(subdirs),\
				 $(addprefix $(dir)/,$(+subdir_targets)))

# The action for each of those is to cd into the directory and make the
# target there.
$(all-subdirs-targets):
	$(MAKE) $(PARALLELMFLAGS) $(subdir-target-args) $(@F)

define subdir-target-args
subdir=$(@D)$(if $($(@D)-srcdir),\
-C $($(@D)-srcdir) ..=`pwd`/,\
-C $(@D) ..=../)
endef

subdir_lib目标实际上是递归glibc的源码子目录,编译每个子目录的subdir_lib目标,其它子目录目标类比,简单的解析如下:

subdirs := csu iconv locale ...
subdir_lib: csu/subdir_lib iconv/subdir_lib locale/subdir_lib
all-subdirs-targets := csu/subdir_lib csu/objects ... iconv/subdir_lib iconv/objects ..
csu/subdir_lib:
   $(MAKE) $(PARALLELMFLAGS) $(subdir-target-args) subdir_lib
iconv/subdir_lib:
   $(MAKE) $(PARALLELMFLAGS) $(subdir-target-args) subdir_lib
2.1.4.1. subdirs列表

subdirs的内容如下所示:

$ grep -nw "subdirs" Makeconfig Makerules Makefile
Makeconfig:1103:subdirs = $(sorted-subdirs)

$ grep -rn sorted-subdirs
build/sysd-sorted:6:sorted-subdirs := csu iconv locale localedata iconvdata assert ctype intl catgets math setjmp signal stdlib stdio-common libio dlfcn nptl rt malloc string wcsmbs timezone time dirent grp pwd posix io termios resource misc socket sysvipc gmon gnulib wctype manual shadow gshadow po argp conform debug mathvec support crypt nptl_db inet resolv nss hesiod sunrpc nis nscd login elf

$ $ grep -n sysd-sorted Makeconfig Makerules Makefile
Makeconfig:1102:-include $(common-objpfx)sysd-sorted
...
Makeconfig:1342:$(common-objpfx)sysd-sorted: $(..)scripts/gen-sorted.awk \
Makefile:507:       $(addprefix $(objpfx),sysd-sorted soversions.mk soversions.i)

Makeconfig文件中生成sysd-sorted文件的主要内容如下:

# This is a partial list of subdirectories containing the library source.
# The order is more or less arbitrary.  The sorting step will take care of the
# dependencies and generate sorted-subdirs dynamically.
all-subdirs = csu assert ctype locale intl catgets math setjmp signal	    \
	      stdlib stdio-common libio malloc string wcsmbs time dirent    \
	      grp pwd posix io termios resource misc socket sysvipc gmon    \
	      gnulib iconv iconvdata wctype manual shadow gshadow po argp   \
	      localedata timezone rt conform debug mathvec support	    \
	      dlfcn elf

ifeq ($(build-crypt),yes)
all-subdirs += crypt
rpath-dirs += crypt
endif

ifndef avoid-generated
# sysd-sorted itself will contain rules making the sysd-sorted target
# depend on Depend files.  But if you just added a Depend file to an
# existing directory not in all-subdirs, then sysd-sorted needs to
# be regenerated, so it depends on existing $(sorted-subdirs:=/Depend) files.
all-Depend-files := $(wildcard $(sort \
			$(foreach dir,$(all-subdirs),\
				  $(firstword $($(dir)-srcdir) \
				  $(..)$(dir))/Depend) \
			$(sorted-subdirs:=/Depend)))
$(common-objpfx)sysd-sorted: $(..)scripts/gen-sorted.awk \
			     $(common-objpfx)config.make $(..)Makeconfig \
			     $(wildcard $(sysdirs:=/Subdirs)) \
			     $(all-Depend-files)
	$(AWK) -f $< \
	       -v subdirs='$(all-subdirs)' \
	       -v srcpfx='$(..)' \
	       $(filter %/Subdirs %/Depend,$^) > $@-tmp
	mv -f $@-tmp $@
$(all-Depend-files): ;
endif

构建顺序是按照sorted-subdirs列表的顺序,如下所示:

[maminjie@fedora glibc]$ grep "make  subdir" build.log
make  subdir=csu -C csu ..=../ subdir_lib
make  subdir=iconv -C iconv ..=../ subdir_lib
make  subdir=locale -C locale ..=../ subdir_lib
make  subdir=localedata -C localedata ..=../ subdir_lib
make  subdir=iconvdata -C iconvdata ..=../ subdir_lib
make  subdir=assert -C assert ..=../ subdir_lib
make  subdir=ctype -C ctype ..=../ subdir_lib
make  subdir=intl -C intl ..=../ subdir_lib
make  subdir=catgets -C catgets ..=../ subdir_lib
make  subdir=math -C math ..=../ subdir_lib
make  subdir=setjmp -C setjmp ..=../ subdir_lib
make  subdir=signal -C signal ..=../ subdir_lib
make  subdir=stdlib -C stdlib ..=../ subdir_lib
make  subdir=stdio-common -C stdio-common ..=../ subdir_lib
make  subdir=libio -C libio ..=../ subdir_lib
make  subdir=dlfcn -C dlfcn ..=../ subdir_lib
make  subdir=nptl -C nptl ..=../ subdir_lib
make  subdir=rt -C rt ..=../ subdir_lib
make  subdir=malloc -C malloc ..=../ subdir_lib
make  subdir=string -C string ..=../ subdir_lib
make  subdir=wcsmbs -C wcsmbs ..=../ subdir_lib
make  subdir=timezone -C timezone ..=../ subdir_lib
make  subdir=time -C time ..=../ subdir_lib
make  subdir=dirent -C dirent ..=../ subdir_lib
make  subdir=grp -C grp ..=../ subdir_lib
make  subdir=pwd -C pwd ..=../ subdir_lib
make  subdir=posix -C posix ..=../ subdir_lib
make  subdir=io -C io ..=../ subdir_lib
make  subdir=termios -C termios ..=../ subdir_lib
make  subdir=resource -C resource ..=../ subdir_lib
make  subdir=misc -C misc ..=../ subdir_lib
make  subdir=socket -C socket ..=../ subdir_lib
make  subdir=sysvipc -C sysvipc ..=../ subdir_lib
make  subdir=gmon -C gmon ..=../ subdir_lib
make  subdir=gnulib -C gnulib ..=../ subdir_lib
make  subdir=wctype -C wctype ..=../ subdir_lib
make  subdir=manual -C manual ..=../ subdir_lib
make  subdir=shadow -C shadow ..=../ subdir_lib
make  subdir=gshadow -C gshadow ..=../ subdir_lib
make  subdir=po -C po ..=../ subdir_lib
make  subdir=argp -C argp ..=../ subdir_lib
make  subdir=conform -C conform ..=../ subdir_lib
make  subdir=debug -C debug ..=../ subdir_lib
make  subdir=mathvec -C mathvec ..=../ subdir_lib
make  subdir=support -C support ..=../ subdir_lib
make  subdir=crypt -C crypt ..=../ subdir_lib
make  subdir=nptl_db -C nptl_db ..=../ subdir_lib
make  subdir=inet -C inet ..=../ subdir_lib
make  subdir=resolv -C resolv ..=../ subdir_lib
make  subdir=nss -C nss ..=../ subdir_lib
make  subdir=hesiod -C hesiod ..=../ subdir_lib
make  subdir=sunrpc -C sunrpc ..=../ subdir_lib
make  subdir=nis -C nis ..=../ subdir_lib
make  subdir=nscd -C nscd ..=../ subdir_lib
make  subdir=login -C login ..=../ subdir_lib
make  subdir=elf -C elf ..=../ subdir_lib
make  subdir=csu -C csu ..=../ others
make  subdir=iconv -C iconv ..=../ others
make  subdir=locale -C locale ..=../ others
make  subdir=localedata -C localedata ..=../ others
make  subdir=iconvdata -C iconvdata ..=../ others
make  subdir=assert -C assert ..=../ others
make  subdir=ctype -C ctype ..=../ others
make  subdir=intl -C intl ..=../ others
make  subdir=catgets -C catgets ..=../ others
make  subdir=math -C math ..=../ others
make  subdir=setjmp -C setjmp ..=../ others
make  subdir=signal -C signal ..=../ others
make  subdir=stdlib -C stdlib ..=../ others
make  subdir=stdio-common -C stdio-common ..=../ others
make  subdir=libio -C libio ..=../ others
make  subdir=dlfcn -C dlfcn ..=../ others
make  subdir=nptl -C nptl ..=../ others
make  subdir=rt -C rt ..=../ others
make  subdir=malloc -C malloc ..=../ others
make  subdir=string -C string ..=../ others
make  subdir=wcsmbs -C wcsmbs ..=../ others
make  subdir=timezone -C timezone ..=../ others
make  subdir=time -C time ..=../ others
make  subdir=dirent -C dirent ..=../ others
make  subdir=grp -C grp ..=../ others
make  subdir=pwd -C pwd ..=../ others
make  subdir=posix -C posix ..=../ others
make  subdir=io -C io ..=../ others
make  subdir=termios -C termios ..=../ others
make  subdir=resource -C resource ..=../ others
make  subdir=misc -C misc ..=../ others
make  subdir=socket -C socket ..=../ others
make  subdir=sysvipc -C sysvipc ..=../ others
make  subdir=gmon -C gmon ..=../ others
make  subdir=gnulib -C gnulib ..=../ others
make  subdir=wctype -C wctype ..=../ others
make  subdir=manual -C manual ..=../ others
make  subdir=shadow -C shadow ..=../ others
make  subdir=gshadow -C gshadow ..=../ others
make  subdir=po -C po ..=../ others
make  subdir=argp -C argp ..=../ others
make  subdir=conform -C conform ..=../ others
make  subdir=debug -C debug ..=../ others
make  subdir=mathvec -C mathvec ..=../ others
make  subdir=support -C support ..=../ others
make  subdir=crypt -C crypt ..=../ others
make  subdir=nptl_db -C nptl_db ..=../ others
make  subdir=inet -C inet ..=../ others
make  subdir=resolv -C resolv ..=../ others
make  subdir=nss -C nss ..=../ others
make  subdir=hesiod -C hesiod ..=../ others
make  subdir=sunrpc -C sunrpc ..=../ others
make  subdir=nis -C nis ..=../ others
make  subdir=nscd -C nscd ..=../ others
make  subdir=login -C login ..=../ others
make  subdir=elf -C elf ..=../ others

2.1.5. libtypes规则

$ grep -n libtypes Makeconfig Makerules Makefile
Makeconfig:1035:libtypes = $(foreach o,$(object-suffixes-for-libc),$(libtype$o))
Makerules:820:.PRECIOUS: $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))
Makerules:835:lib: lib-noranlib $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))

Makeconfig文件中关于libtypes的内容如下:

# Enable object files for different versions of the library.
# Various things use $(object-suffixes) to know what all to make.
# The compilation rules use $(CPPFLAGS-${SUFFIX}) and $(CFLAGS-${SUFFIX})
# to pass different flags for each flavor.
libtypes = $(foreach o,$(object-suffixes-for-libc),$(libtype$o))
# .op may be added to all-object-suffixes below.
all-object-suffixes := .o .os .oS
object-suffixes :=
CPPFLAGS-.o = $(pic-default)
# libc.a must be compiled with -fPIE/-fpie for static PIE.
CFLAGS-.o = $(filter %frame-pointer,$(+cflags)) $(pie-default)
libtype.o := lib%.a
object-suffixes += .o
ifeq (yes,$(build-shared))
# Under --enable-shared, we will build a shared library of PIC objects.
# The PIC object files are named foo.os.
object-suffixes += .os
CPPFLAGS-.os = -DPIC -DSHARED
CFLAGS-.os = $(filter %frame-pointer,$(+cflags)) $(pic-ccflag)
libtype.os := lib%_pic.a
...

# Convenience variable for when we want to treat shared-library cases
# differently from the rest.
object-suffixes-noshared := $(filter-out .os,$(object-suffixes))

object-suffixes-for-libc := $(object-suffixes)

ifeq (yes,$(build-shared))
# Build special library that contains the static-only routines for libc.
object-suffixes-for-libc += .oS

# Must build the routines as PIC, though, because they can end up in (users')
# shared objects.  We don't want to use CFLAGS-os because users may, for
# example, make that processor-specific.
CFLAGS-.oS = $(CFLAGS-.o) $(PIC-ccflag) $(extra-nonshared-cflags)
CPPFLAGS-.oS = $(CPPFLAGS-.o) -DPIC -DLIBC_NONSHARED=1
libtype.oS = lib%_nonshared.a
endif

libtypes为:libtype.o,libtype.os等。
libtype$o为:lib%.a,lib%_pic.a等静态库

Makerules文件中关于libtype$o的内容如下:

# Now define explicit rules to build the library archives; these depend
# on the stamp files built above.
define o-iterator-doit
$(common-objpfx)$(patsubst %,$(libtype$o),c): \
		$(subdirs-stamp-o) $(common-objpfx)stamp$o; $$(do-makelib)
endef
define do-makelib
cd $(common-objdir) && \
$(AR) $(CREATE_ARFLAGS) $(@F) `cat $(patsubst $(common-objpfx)%,%,$^)`
endef
subdirs-stamps := $(foreach d,$(subdirs),$(common-objpfx)$d/stamp%)
subdirs-stamp-o = $(subst %,$o,$(subdirs-stamps))
ifndef subdir
$(subdirs-stamps): subdir_lib;
endif
object-suffixes-left = $(object-suffixes-for-libc)
include $(o-iterator)
2.1.5.1. o-iterator

o-iterator-doit在o-iterator.mk中调用展开:

$ grep -rn o-iterator-doit
o-iterator.mk:9:$(o-iterator-doit)

o-iterator.mk内容如下:

o := $(firstword $(object-suffixes-left))
object-suffixes-left := $(filter-out $o,$(object-suffixes-left))

$(o-iterator-doit)

o-iterator.mk通过include $(o-iterator)包含进Makerules中,如下所示:

$ grep -nw "o-iterator" Makeconfig Makerules Makefile
Makerules:62:o-iterator = $(patsubst %,$(..)o-iterator.mk,$(object-suffixes-left))

Makerules文件中关于o-iterator的内容如下:

# This variable is used in ``include $(o-iterator)'' after defining
# $(o-iterator-doit) to produce some desired rule using $o for the object
# suffix, and setting $(object-suffixes-left) to $(object-suffixes); a copy
# is produced for each object suffix in use.
o-iterator = $(patsubst %,$(..)o-iterator.mk,$(object-suffixes-left))

libtype$o最后被转为:libc.a,libc_pic.a等静态库,这些静态库基于各子目录subdirs中的时间戳stamp$o文件取出中间目标文件,然后通过ar命令归档。

[maminjie@fedora glibc]$ grep libc_pic.a build.log
cd /home/maminjie/glibc-2.34/build && ar cruv libc_pic.a `cat csu/stamp.os iconv/stamp.os locale/stamp.os localedata/stamp.os iconvdata/stamp.os assert/stamp.os ctype/stamp.os intl/stamp.os catgets/stamp.os math/stamp.os setjmp/stamp.os signal/stamp.os stdlib/stamp.os stdio-common/stamp.os libio/stamp.os dlfcn/stamp.os nptl/stamp.os rt/stamp.os malloc/stamp.os string/stamp.os wcsmbs/stamp.os timezone/stamp.os time/stamp.os dirent/stamp.os grp/stamp.os pwd/stamp.os posix/stamp.os io/stamp.os termios/stamp.os resource/stamp.os misc/stamp.os socket/stamp.os sysvipc/stamp.os gmon/stamp.os gnulib/stamp.os wctype/stamp.os manual/stamp.os shadow/stamp.os gshadow/stamp.os po/stamp.os argp/stamp.os conform/stamp.os debug/stamp.os mathvec/stamp.os support/stamp.os crypt/stamp.os nptl_db/stamp.os inet/stamp.os resolv/stamp.os nss/stamp.os hesiod/stamp.os sunrpc/stamp.os nis/stamp.os nscd/stamp.os login/stamp.os elf/stamp.os stamp.os`
...

2.1.6. libc.so规则

$ grep -nw "libc.so:" Makeconfig Makerules Makefile
Makerules:686:$(common-objpfx)libc.so: link-libc-deps = # empty
Makerules:687:$(common-objpfx)linkobj/libc.so: link-libc-deps = # empty
Makerules:691:$(common-objpfx)libc.so: $(common-objpfx)libc_pic.os$(libc_pic_clean) \
Makerules:699:$(common-objpfx)linkobj/libc.so: $(common-objpfx)linkobj/libc_pic.a \
Makerules:708:$(common-objpfx)libc.so: $(common-objpfx)libc.map
Makerules:1056:$(inst_libdir)/libc.so: $(common-objpfx)format.lds \
Makerules:1075:$(inst_slibdir)/libc.so: $(common-objpfx)libc.so $(+force)

libc.so依赖libc_pic.a。

由上面libtypes规则知道libc_pic.a由各个子目录编译出的中间目标文件归档而来。

Makerules文件中关于libc.so的主要内容如下所示:


define build-shlib-helper
$(LINK.o) -shared -static-libgcc -Wl,-O1 $(sysdep-LDFLAGS) \
	  $(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) $(rtld-LDFLAGS) \
	  $(extra-B-$(@F:lib%.so=%).so) -B$(csu-objpfx) \
	  $(extra-B-$(@F:lib%.so=%).so) $(load-map-file) \
	  -Wl,-soname=lib$(libprefix)$(@F:lib%.so=%).so$($(@F)-version) \
	  $(LDFLAGS.so) $(LDFLAGS-lib.so) $(LDFLAGS-$(@F:lib%.so=%).so) \
	  -L$(subst :, -L,$(rpath-link)) -Wl,-rpath-link=$(rpath-link)
endef
...

define build-shlib
$(build-shlib-helper) -o $@ $(shlib-lds-flags) \
	  $(csu-objpfx)abi-note.o $(build-shlib-objlist)
endef
...
# Clear link-libc-deps for the libc.so libraries so build-shlibs does not
# filter ld.so out of the list of linked objects.
$(common-objpfx)libc.so: link-libc-deps = # empty
$(common-objpfx)linkobj/libc.so: link-libc-deps = # empty

# Use our own special initializer and finalizer files for the libc.so
# libraries.
$(common-objpfx)libc.so: $(common-objpfx)libc_pic.os$(libc_pic_clean) \
			 $(elf-objpfx)sofini.os \
			 $(elf-objpfx)interp.os \
			 $(elf-objpfx)ld.so \
			 $(shlib-lds)
	$(build-shlib)
	$(call after-link,$@)

$(common-objpfx)linkobj/libc.so: $(common-objpfx)linkobj/libc_pic.a \
			 $(elf-objpfx)sofini.os \
			 $(elf-objpfx)interp.os \
			 $(elf-objpfx)ld.so \
			 $(shlib-lds)
	$(build-shlib)
	$(call after-link,$@)

ifeq ($(build-shared),yes)
$(common-objpfx)libc.so: $(common-objpfx)libc.map
endif
common-generated += libc.so libc_pic.os
ifdef libc.so-version
$(common-objpfx)libc.so$(libc.so-version): $(common-objpfx)libc.so
	$(make-link)
common-generated += libc.so$(libc.so-version)
endif
...

2.2. 系统调用的选择机制

在“glibc 知:系统调用”中作者发现glibc中对于系统调用会有很多同名c文件(实现文件),这些文件最终使用的是哪个,作者当时没有找到直接依据,只是大胆猜测其使用顺序:优先使用特定架构下的,其次是linux下的,再是generic下面的,最后是glibc自实现的(往往是空函数)。本次,作者根据构建机制再探究竟。

glibc提供的系统调用一般与平台相关,不同的平台有不同的实现。glibc的做法是平台无关目录下提供一个默认的实现,返回ENOSYS;在具体的平台目录下再有一个真正的实现。

2.2.1. strcpy实现

以strcpy为例,其它系统调用读者可以根据机制自行推导。

$ $ find -name strcpy.*
./string/strcpy.c
./sysdeps/aarch64/strcpy.S
./sysdeps/alpha/strcpy.S
./sysdeps/arm/armv6/strcpy.S
./sysdeps/csky/abiv2/strcpy.S
./sysdeps/i386/i586/strcpy.S
./sysdeps/i386/i686/multiarch/strcpy.c
./sysdeps/ia64/strcpy.S
./sysdeps/powerpc/powerpc32/405/strcpy.S
./sysdeps/powerpc/powerpc32/strcpy.S
./sysdeps/powerpc/powerpc64/le/power9/strcpy.S
./sysdeps/powerpc/powerpc64/multiarch/strcpy.c
./sysdeps/powerpc/powerpc64/power8/strcpy.S
./sysdeps/s390/strcpy.c
./sysdeps/sparc/sparc32/sparcv9/strcpy.S
./sysdeps/sparc/sparc32/strcpy.S
./sysdeps/sparc/sparc64/strcpy.S
./sysdeps/x86_64/multiarch/strcpy.c
./sysdeps/x86_64/strcpy.S

2.2.2. 系统依赖规则

Makerules文件中关于系统依赖规则的内容如下:

###############################################################################
# NOTE!  Everything adding to before-compile needs to come before this point! #
###############################################################################

# Generate an ordered list of implicit rules which find the source files in
# each sysdep directory.  The old method was to use vpath to search all the
# sysdep directories.  However, that had the problem that a .S file in a
# later directory would be chosen over a .c file in an earlier directory,
# which does not preserve the desired sysdeps ordering behavior.

# System-dependent makefiles can put in `inhibit-sysdep-asm' regexps
# matching sysdep directories whose assembly source files should be
# suppressed.

-include $(common-objpfx)sysd-rules
ifneq ($(sysd-rules-sysdirs),$(config-sysdirs))
# The value of $(+sysdep_dirs) the sysd-rules was computed for
# differs from the one we are using now.  So force a rebuild of sysd-rules.
sysd-rules-force = FORCE
FORCE:
endif
$(common-objpfx)sysd-rules: $(..)scripts/sysd-rules.awk \
			    $(common-objpfx)config.make $(..)Makerules \
			    $(sysdep-makefiles) $(sysdep-makeconfigs) \
			    $(sysd-rules-force)
	-@rm -f $@T
	LC_ALL=C $(AWK) -f $< > $@T \
			-v all_object_suffixes='$(all-object-suffixes)' \
			-v inhibit_sysdep_asm='$(inhibit-sysdep-asm)' \
			-v sysd_rules_patterns='$(sysd-rules-patterns)' \
			-v config_sysdirs='$(config-sysdirs)'
	mv -f $@T $@

ifndef sysd-rules-done
# Don't do deps until this exists, because it provides rules to make the deps.
no_deps=t
endif

define o-iterator-doit
$(objpfx)%$o: %.S $(before-compile); $$(compile-command.S)
endef
object-suffixes-left := $(all-object-suffixes)
include $(o-iterator)

define o-iterator-doit
$(objpfx)%$o: %.c $(before-compile); $$(compile-command.c)
endef
object-suffixes-left := $(all-object-suffixes)
include $(o-iterator)

define o-iterator-doit
$(objpfx)%$o: %.cc $(before-compile); $$(compile-command.cc)
endef
object-suffixes-left := $(all-object-suffixes)
include $(o-iterator)
...

生成隐式规则的有序列表,这些规则在每个sysdep目录中查找源文件。旧方法是使用vpath搜索所有sysdep目录。然而,它有一个问题,即在一个更后的目录中选择一个.S文件,而不是在一个更前的目录中选择一个.c文件,这不会保留所需的sysdeps排序行为。

1)sysd-rules中处理的是系统依赖目录下的实现。
2)o-iterator处理的是当前目录下的实现。

疑问:这里对于多实现的文件,其目标都是一个,Makefile中对于多规则目标的约定,此处的用法似乎有点问题,莫非高版本的make支持这种用法??

before-compile和compile-command的定义如下:

canpool@DESKTOP-ODCM7SC:~/projects/linux/glibc$ grep -rn ^before-compile
csu/Makefile:92:before-compile += $(objpfx)abi-tag.h
elf/Makefile:190:before-compile  += $(objpfx)trusted-dirs.h
iconvdata/Makefile:209:before-compile += $(addprefix $(objpfx),$(generated-modules:=.h))
intl/Makefile:37:before-compile += $(objpfx)msgs.h
locale/Makefile:42:before-compile       += $(objpfx)C-translit.h
mach/Machrules:196:before-compile += $(interface-headers:%=$(objpfx)%)
mach/Makefile:159:before-compile += $(objpfx)mach-shortcuts.h
Makeconfig:1174:before-compile += $(common-objpfx)libc-modules.h
Makeconfig:1193:before-compile += $(common-objpfx)dl-tunable-list.h
Makeconfig:1214:before-compile += $(common-objpfx)gcc-macros.h
Makerules:90:before-compile := $(filter $(common-objpfx)mach% $(common-objpfx)hurd%,\
Makerules:98:before-compile := $(common-objpfx)abi-versions.h $(before-compile)
Makerules:111:before-compile := $(common-objpfx)first-versions.h \
Makerules:129:before-compile := $(common-objpfx)cstdlib $(common-objpfx)cmath \
Makerules:140:before-compile := $(common-objpfx)bits/std_abs.h $(before-compile)
Makerules:147:before-compile := $(common-objpfx)libc-abis.h $(before-compile)
Makerules:174:before-compile += $(objpfx).
Makerules:185:before-compile := $(filter-out $(wildcard $(before-compile)),$(before-compile))
Makerules:281:before-compile += $(gen-as-const-headers:%.sym=$(common-objpfx)%.h)
Makerules:295:before-compile += $(common-objpfx)gnu/lib-names.h
Makerules:303:before-compile += $(common-objpfx)$(lib-names-h-abi)
posix/Makefile:144:before-compile       += $(objpfx)posix-conf-vars-def.h
Rules:64:before-compile += $(common-objpfx)bits/stdio_lim.h
Rules:417:before-compile: $(before-compile)
sysdeps/mach/hurd/Makefile:48:before-compile += $(patsubst %,$(hurd-objpfx)hurd/%.h,auth io fs process)
sysdeps/mach/Makefile:50:before-compile += $(mach-before-compile)
sysdeps/powerpc/powerpc64/le/Makefile:27:before-compile += $(common-objpfx)no_ldbl_gnu_attribute.bin
sysdeps/unix/sysv/linux/Makefile:187:before-compile += $(objpfx)bits/syscall.h

canpool@DESKTOP-ODCM7SC:~/projects/linux/glibc$ grep -rnE "^compile-command\.(S|c|cc)"
Makerules:437:compile-command.S = $(compile.S) $(OUTPUT_OPTION) $(compile-mkdep-flags)
Makerules:438:compile-command.c = $(compile.c) $(OUTPUT_OPTION) $(compile-mkdep-flags)
Makerules:439:compile-command.cc = $(compile.cc) $(OUTPUT_OPTION) $(compile-mkdep-flags)

canpool@DESKTOP-ODCM7SC:~/projects/linux/glibc$ grep -rnE "^compile\.(S|c|cc)"
Makerules:447:compile.c = $(CC) $< -c $(CFLAGS) $(CPPFLAGS)
Makerules:448:compile.cc = $(CXX) $< -c $(CXXFLAGS) $(CPPFLAGS)
Makerules:449:compile.S = $(CC) $< -c $(CPPFLAGS) $(S-CPPFLAGS) \

2.2.3. sysd-rules.awk

sysd-rules.awk文件内容如下:

# This is a GAWK script to generate the sysd-rules file.
# It does not read any input, but it requires that several variables
# be set on its command line (using -v) to their makefile counterparts:
#	all_object_suffixes	$(all-object-suffixes)
#	inhibit_sysdep_asm	$(inhibit-sysdep-asm)
#	config_sysdirs		$(config_sysdirs)
#	sysd_rules_patterns	$(sysd-rules-patterns)

BEGIN {
  print "sysd-rules-sysdirs :=", config_sysdirs;

  nsuffixes = split(all_object_suffixes, suffixes);
  ninhibit_asm = split(inhibit_sysdep_asm, inhibit_asm);
  nsysdirs = split(config_sysdirs, sysdirs);
  npatterns = split(sysd_rules_patterns, patterns);

  # Each element of $(sysd-rules-patterns) is a pair TARGET:DEP.
  # They are no in particular order.  We need to sort them so that
  # the longest TARGET is first, and, among elements with the same
  # TARGET, the longest DEP is first.
  for (i = 1; i <= npatterns; ++i) {
    if (split(patterns[i], td, ":") != 2) {
      msg = "bad sysd-rules-patterns element '" patterns[i] "'";
      print msg > "/dev/stderr";
      exit 2;
    }
    target_order = sprintf("%09d", npatterns + 1 - length(td[1]));
    dep_order = sprintf("%09d", npatterns - length(td[2]));
    sort_patterns[target_order SUBSEP dep_order] = patterns[i];
  }
  asorti(sort_patterns, map_patterns);
  for (i in map_patterns) {
    patterns[i] = sort_patterns[map_patterns[i]];
  }

  for (sysdir_idx = 1; sysdir_idx <= nsysdirs; ++sysdir_idx) {
    dir = sysdirs[sysdir_idx];
    if (dir !~ /^\//) dir = "$(..)" dir;
    asm_rules = 1;
    for (i = 1; i <= ninhibit_asm; ++i) {
      if (dir ~ ("^.*sysdeps/" inhibit_asm[i] "$")) {
        asm_rules = 0;
        break;
      }
    }
    for (suffix_idx = 1; suffix_idx <= nsuffixes; ++suffix_idx) {
      o = suffixes[suffix_idx];
      for (pattern_idx = 1; pattern_idx <= npatterns; ++pattern_idx) {
        pattern = patterns[pattern_idx];
        split(pattern, td, ":");
        target_pattern = td[1];
        dep_pattern = td[2];
        # rtld objects are always PIC.
        if (target_pattern ~ /^rtld/ && o != ".os") {
            continue;
        }
        if (target_pattern == "%") {
          command_suffix = "";
        } else {
          prefix = gensub(/%/, "", 1, target_pattern);
          command_suffix = " $(" prefix  "CPPFLAGS)" " $(" prefix  "CFLAGS)";
        }
        target = "$(objpfx)" target_pattern o ":";
        if (asm_rules) {
          dep = dir "/" dep_pattern ".S";
          print target, dep, "$(before-compile)";
          print "\t$(compile-command.S)" command_suffix;
        }
        dep = dir "/" dep_pattern ".c";
        print target, dep, "$(before-compile)";
        print "\t$(compile-command.c)" command_suffix;
      }
    }
    print "$(inst_includedir)/%.h:", dir "/%.h", "$(+force)";
    print "\t$(do-install)";
  }

  print "sysd-rules-done := t";
  exit 0;
}

$(sysd-rules-patterns)的每个元素都是一对TARGET:DEP。它们没有特定的顺序。我们需要对它们进行排序,以便最长的目标是第一个,并且在具有相同目标的元素中,最长的DEP是第一个。

sysd-rules-patterns的内容如下:

$ grep -rn sysd-rules-patterns
Makeconfig:1358:sysd-rules-patterns := %:% rtld-%:rtld-% rtld-%:% m_%:s_%

2.2.4. sysd-rules文件

生成的sysd-rules文件部分内容如下(长目标在前;目标相同时,长依赖在前;.S文件先于.c文件):

$ head -n10 sysd-rules
sysd-rules-sysdirs := sysdeps/unix/sysv/linux/x86_64/64 sysdeps/unix/sysv/linux/x86_64 sysdeps/unix/sysv/linux/x86 sysdeps/x86/nptl sysdeps/unix/sysv/linux/wordsize-64 sysdeps/x86_64/nptl sysdeps/unix/sysv/linux sysdeps/nptl sysdeps/pthread sysdeps/gnu sysdeps/unix/inet sysdeps/unix/sysv sysdeps/unix/x86_64 sysdeps/unix sysdeps/posix sysdeps/x86_64/64 sysdeps/x86_64/fpu/multiarch sysdeps/x86_64/fpu sysdeps/x86/fpu sysdeps/x86_64/multiarch sysdeps/x86_64 sysdeps/x86 sysdeps/ieee754/float128 sysdeps/ieee754/ldbl-96 sysdeps/ieee754/dbl-64 sysdeps/ieee754/flt-32 sysdeps/wordsize-64 sysdeps/ieee754 sysdeps/generic
$(objpfx)m_%.o: $(..)sysdeps/unix/sysv/linux/x86_64/64/s_%.S $(before-compile)
        $(compile-command.S) $(m_CPPFLAGS) $(m_CFLAGS)
$(objpfx)m_%.o: $(..)sysdeps/unix/sysv/linux/x86_64/64/s_%.c $(before-compile)
        $(compile-command.c) $(m_CPPFLAGS) $(m_CFLAGS)
$(objpfx)%.o: $(..)sysdeps/unix/sysv/linux/x86_64/64/%.S $(before-compile)
        $(compile-command.S)
$(objpfx)%.o: $(..)sysdeps/unix/sysv/linux/x86_64/64/%.c $(before-compile)
        $(compile-command.c)
$(objpfx)rtld-%.os: $(..)sysdeps/unix/sysv/linux/x86_64/64/rtld-%.S $(before-compile)

$ tail -n10 sysd-rules
        $(compile-command.S) $(m_CPPFLAGS) $(m_CFLAGS)
$(objpfx)m_%.oS: $(..)sysdeps/generic/s_%.c $(before-compile)
        $(compile-command.c) $(m_CPPFLAGS) $(m_CFLAGS)
$(objpfx)%.oS: $(..)sysdeps/generic/%.S $(before-compile)
        $(compile-command.S)
$(objpfx)%.oS: $(..)sysdeps/generic/%.c $(before-compile)
        $(compile-command.c)
$(inst_includedir)/%.h: $(..)sysdeps/generic/%.h $(+force)
        $(do-install)
sysd-rules-done := t

可以看出生成.o文件优先选择系统相关目录下的实现,如果没有才会选择当前目录下的实现。

2.2.5. config-sysdirs列表

系统依赖规则中有一个强制目标,判断条件是sysd-rules-sysdirs不等于config-sysdirs。

-include $(common-objpfx)sysd-rules
ifneq ($(sysd-rules-sysdirs),$(config-sysdirs))
# The value of $(+sysdep_dirs) the sysd-rules was computed for
# differs from the one we are using now.  So force a rebuild of sysd-rules.
sysd-rules-force = FORCE
FORCE:
endif

当sysd-rules文件未生成时,sysd-rules-sysdirs自然不等于config-sysdirs。当sysd-rules文件生成后,二者便相等了。

canpool@DESKTOP-ODCM7SC:~/projects/linux/glibc$ grep -rn config-sysdirs
build/config.make:38:config-sysdirs =  sysdeps/unix/sysv/linux/x86_64/64 sysdeps/unix/sysv/linux/x86_64 sysdeps/unix/sysv/linux/x86 sysdeps/x86/nptl sysdeps/unix/sysv/linux/wordsize-64 sysdeps/x86_64/nptl sysdeps/unix/sysv/linux sysdeps/nptl sysdeps/pthread sysdeps/gnu sysdeps/unix/inet sysdeps/unix/sysv sysdeps/unix/x86_64 sysdeps/unix sysdeps/posix sysdeps/x86_64/64 sysdeps/x86_64/fpu/multiarch sysdeps/x86_64/fpu sysdeps/x86/fpu sysdeps/x86_64/multiarch sysdeps/x86_64 sysdeps/x86 sysdeps/ieee754/float128 sysdeps/ieee754/ldbl-96 sysdeps/ieee754/dbl-64 sysdeps/ieee754/flt-32 sysdeps/wordsize-64 sysdeps/ieee754 sysdeps/generic

文件config.make由configure命令生成的配置文件,被Makeconfig文件包含:

canpool@DESKTOP-ODCM7SC:~/projects/linux/glibc$ grep -rn config.make Makeconfig Makerules Makefile
Makeconfig:51:include $(common-objpfx)config.make
Makeconfig:60:# `configure' writes a definition of `config-sysdirs' in `config.make'.
Makeconfig:68:# Run config.status to update config.make and config.h.  We don't show the
Makeconfig:71:# config.make should suffice to force regeneration and re-exec, and the new
Makeconfig:73:$(common-objpfx)config.make: $(common-objpfx)config.status \
Makeconfig:74:                       $(..)config.make.in $(..)config.h.in
Makeconfig:1343:                             $(common-objpfx)config.make $(..)Makeconfig \
Makerules:163:$(common-objpfx)runtime-linker.stamp: $(common-objpfx)config.make
Makerules:377:                      $(common-objpfx)config.make $(..)Makerules \
Makerules:553:$(common-objpfx)shlib.lds: $(common-objpfx)config.make $(..)Makerules
Makerules:1040:$(common-objpfx)format.lds: $(common-objpfx)config.make \
Makerules:1300:$(objpfx)check-abi-%.out: $(common-objpfx)config.make %.abilist \
Makerules:1304:$(objpfx)check-abi-%.out: $(common-objpfx)config.make %.abilist \
Makerules:1381:            $(common-objpfx)config.make
Makefile:201:$(common-objpfx)testrun.sh: $(common-objpfx)config.make \
Makefile:407:$(common-objpfx)debugglibc.sh: $(common-objpfx)config.make \
Makefile:672:   -rm -f $(addprefix $(objpfx),config.make config-name.h config.h)

2.2.6. 小结

对于x86_64平台,strcpy根据系统依赖规则,其实现文件查找顺序如下:

$ $ find -name strcpy.*
./string/strcpy.c
./sysdeps/aarch64/strcpy.S
./sysdeps/alpha/strcpy.S
./sysdeps/arm/armv6/strcpy.S
./sysdeps/csky/abiv2/strcpy.S
./sysdeps/i386/i586/strcpy.S
./sysdeps/i386/i686/multiarch/strcpy.c
./sysdeps/ia64/strcpy.S
./sysdeps/powerpc/powerpc32/405/strcpy.S
./sysdeps/powerpc/powerpc32/strcpy.S
./sysdeps/powerpc/powerpc64/le/power9/strcpy.S
./sysdeps/powerpc/powerpc64/multiarch/strcpy.c
./sysdeps/powerpc/powerpc64/power8/strcpy.S
./sysdeps/s390/strcpy.c
./sysdeps/sparc/sparc32/sparcv9/strcpy.S
./sysdeps/sparc/sparc32/strcpy.S
./sysdeps/sparc/sparc64/strcpy.S
./sysdeps/x86_64/multiarch/strcpy.c
./sysdeps/x86_64/strcpy.S

==>
./sysdeps/x86_64/multiarch/strcpy.c
./sysdeps/x86_64/strcpy.S
./string/strcpy.c

结论:根据构建规则,似乎知道了对多实现是如何选择的,但又没有完全搞清楚。

2.3. 包含关系

canpool@DESKTOP-ODCM7SC:~/projects/linux/glibc$ grep -rnwE "^-*include\s*" Makefile Makeconfig Makerules Rules csu/Makefile
Makefile:25:include Makeconfig
Makefile:89:include Makerules
Makeconfig:51:include $(common-objpfx)config.make
Makeconfig:98:include $(..)configparms
Makeconfig:102:include $(common-objpfx)configparms
Makeconfig:1102:-include $(common-objpfx)sysd-sorted
Makeconfig:1144:-include $(common-objpfx)soversions.mk
Makeconfig:1219:-include $(common-objpfx)sysd-versions
Makeconfig:1220:-include $(common-objpfx)Versions.mk
Makeconfig:1270:-include $(common-objpfx)time64-compat.mk
Makeconfig:1363:include $(sysdep-makeconfigs)
Makerules:55:include $(..)Makeconfig
Makerules:82:include $(sysdep-makefiles)
Makerules:369:-include $(common-objpfx)sysd-rules
Makerules:397:include $(o-iterator)
Makerules:403:include $(o-iterator)
Makerules:409:include $(o-iterator)
Makerules:420:include $(o-iterator)
Makerules:426:include $(o-iterator)
Makerules:432:include $(o-iterator)
Makerules:740:include $(patsubst %,$(..)extra-lib.mk,$(extra-libs))
Makerules:751:include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
Makerules:758:include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
Makerules:799:-include $(+depfiles)
Makerules:870:include $(o-iterator)
Makerules:890:include $(o-iterator)
Makerules:1087:include $(o-iterator)
Makerules:1096:include $(o-iterator)
Makerules:1105:include $(o-iterator)
Makerules:1238:include $(o-iterator)
Makerules:1244:include $(o-iterator)
Makerules:1259:include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
Makerules:1266:include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
Makerules:1276:include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
Makerules:1407:-include $(stdio_lim:h=d)
Rules:28:include $(firstword $(..) ../)Makeconfig
Rules:66:include $(..)Makerules
csu/Makefile:27:include ../Makeconfig
csu/Makefile:120:include ../Rules

3. 参考

glibc 知:构建2.34版本 文章目录获取版本构建安装 获取版本 主页:https://www.gnu.org/software/libc 这里选择任意一个压缩包下载即可。 构建安装 tar -Jxf glibc-2.34.tar.xz mkdir build install cd build ../glibc-2.34/configure --prefix=/usr make -j4 make install DESTDIR=../install ... 阅读详情

相关推荐

【gcc】高版本gcc编译出的程序在低版本glibc机器上运行

目录 1.静态编译(多数场景不行) 2.容器发布(部分场景可以使用) 3.打包依赖的so发布(通用方案) 3.1 方式1 在编译时设置rpath和dynamic linker 3.2 方式2 直接修改二进制程序的rpath和interpreter 比如我们用gcc 9.3.0编译程序,但需要发布的机器gcc版本是4.8.5,怎么办? 你可能想到如下方法 静态编译 容器发布 打包依赖的so,使用本地so运行程序 1.静态编译(多数场景不行) 其中静态编译是行不通的,libstdc++..

我的笔记本 1万+

关于version `GLIBC_2.34‘ not found解决办法(非升级glibc库或Linux系统)

指定编译器链接库目录从而解决glibc版本不匹配问题

YuHWEI的博客 2万+

glibc 2.34 构建实战:从源码到测试的完整指南

本文提供了glibc 2.34从源码到测试的完整构建实战指南。针对旧系统运行新软件时常见的GLIBC版本缺失问题,详细讲解了安全构建、独立安装及使用testrun.sh验证的方法,帮助开发者在不影响系统稳定性的前提下获得新版C库功能。

weixin_28834765的博客 478

【程序员的自我修养10】动态库的兼容问题——GLIBC_2.34‘ not found

前几章,花了较大篇幅介绍了动态库链接过程原理,需要面对的场景以及解决思路,真真切切的了解到动态库的优秀之处。这就导致大量开始程序使用动态链接。随着系统中的动态库越来越多,也会出现一些问题。本文介绍动态库的发展过程中遇到的问题,以及如何解决的。以及非常好用的调试tips。本文花了较大篇幅介绍了动态库如何解决和避免兼容性问题,以及分享两个分享实用的环境变量LD_PRELOADLD_DEBUG,至此关于动态库的分享算是结束了。下一章节,我们开始向《库与运行库》进军。若我的内容对您有所帮助,还请关注我的公众号。

点滴路程 4506

CentOS7下DuckDB JDBC驱动编译踩坑实录:如何绕过GLIBC版本限制

本文详细介绍了在CentOS7系统下编译DuckDB JDBC驱动时遇到的GLIBC版本限制问题,并提供了三种实战验证的解决方案:Docker环境隔离构建法、静态链接编译方案和混合部署策略。这些方法帮助开发者在不升级系统核心组件的情况下,成功绕过GLIBC_2.23版本限制,实现驱动的高效部署。

weixin_42549496的博客 306

[Pthread] Linux中的线程同步机制(二)--In Glibc

在linux中进行多线程开发,同步是不可回避的一个问题。在POSIX标准中定义了三种线程同步机制: Mutexes(互斥量), Condition Variables(条件变量)和POSIX Semaphores(信号量)。NPTL基本上实现了POSIX,而glibc又使用NPTL作为自己的线程库。因此glibc中包含了这三种同步机制的实现(当然还包括其他的同步机制,如APUE里提到的读写锁)。G

Javadino的专栏 5729

glibc Tcache机制

Tcache的简述 在Glibc的2.26中 新增了Tcache机制 这是ptmalloc2的缓存机制 Tcache在glibc中是默认开启的,在Tcache被开启的时候会定义如下东西 #if USE_TCACHE /* We want 64 entries. This is an arbitrary limit, which tunables can reduce. */ # define ...

For Geek 3764

glibc-2.34.tar.gz

glibc-2.34.tar.gz

Docker-alpine-glibc深度解析:如何巧妙实现glibc与musl libc的共存机制

想要在超轻量的Alpine Linux中运行需要glibc的应用程序吗?Docker-alpine-glibc镜像为你提供了终极解决方案!这个精心构建的Docker镜像仅约12MB,却实现了glibc与musl libc的完美共存,让OracleJDK、Anaconda等专有软件能够在Alpine环境中无缝运行。本文将为你深度解析这个巧妙的技术实现,揭示glibc与musl libc共存的完整机制

gitblog_00797的博客 763

《AI大模型评测与智能测试》

第三篇“实践蓝图”(第6-7章)着眼于测试领域AI应用,重点介绍了RAG、AI Agent开发及构建端到端智能测试系统的方法。本书聚焦AI时代的“质量”核心,以“AI赋能测试”(AI For Testing)与“AI模型评测”(Testing For AI)为双轮驱动。内容不仅深入剖析如何科学度量AI大模型的内在质量,还系统讲解如何利用AI赋能传统软件测试,帮助读者贯通从模型原理、评测到应用开发的全链路知识。

酌沧 1147

基于Codex与Skills的学术研究自动化全流程实战指南

还在为海量文献下载、整理、阅读和综述写作而头疼吗?面对堆积如山的PDF,你是否感到无从下手?从选题、文献检索、到写作、绘图、润色,再到应对AI检测和审稿意见,学术研究的每一步都充满挑战。本文将为你带来一套堪称“王炸组合”的解决方案: Codex + Skills 。这不是简单的工具介绍,而是一套从零开始,覆盖学术研究全流程的保姆级实战教程。我们将手把手教你如何安装、配置,并利用强大的Skills(技能)自动化处理文献下载管理、高效撰写综述、生成专业图表、智能润色降重,直至应对审稿。无论你是科研新手还是希望提

weixin_29041195的博客 776

Docker-alpine-glibc实战教程:从零构建支持glibc的Alpine基础镜像

Docker-alpine-glibc是一个精心打造的轻量级Alpine Docker镜像,它将Alpine Linux的小巧特性与glibc库完美结合,仅约12MB的体积就能让依赖glibc的应用程序在Alpine系统上顺畅运行。本文将为你提供一份简单快速的实战指南,帮助你从零开始构建并使用这个强大的基础镜像。 ## 为什么选择Docker-alpine-glibc? Alpine Linu

gitblog_00425的博客 619

【Android】条形码/二维码扫描——ZXing源码分析

正在更新中 针对3.2.1版本源码 与其他版本代码可能会有出入,但核心思想基本不变。 首先从com.google.zxing.client.android包中的目录结构分析:Book:这个文件夹中有5个类: BrowseBookListener 实现了adapter的点击事项响应,通过获取ISBN,然后跳转到Google books中查询响应的详细信息。 SearchBookContents

年轻的学徒 2458

Docker-alpine-glibc进阶技巧:优化镜像大小与构建速度的10个实用方法

Docker-alpine-glibc是一个精心打造的轻量级Alpine Docker镜像,通过集成glibc实现了仅约12MB的极小体积,同时保持了对GNU C库的兼容性。本文将分享10个实用技巧,帮助开发者进一步优化基于docker-alpine-glibc的镜像大小和构建速度,提升容器化应用的部署效率。 ## 1. 精准控制Alpine基础版本 选择合适的Alpine版本是优化的第一步。

gitblog_00943的博客 696

从OAuth到DID:Web4.0身份验证协议的技术演进图谱

在数字身份体系从Web2.0向Web4.0跨越的进程中,身份验证协议正经历着从中心化授权到自主主权的范式转移。本文通过对比OAuth 2.0、OpenID Connect等传统方案与W3C DID、ION等去中心化协议的技术架构,揭示身份验证技术在性能、安全、隐私三大维度的代际差异。基于对GitHub、欧盟数字身份钱包等典型系统的实测数据分析,量化评估中心化授权服务器的单点故障风险与DID链上验证的吞吐量瓶颈。结合零知识证明、分布式密钥管理等前沿技术,为开发者提供面向Web4.0的渐进式身份架构迁移策略。

水务人 839

STM32开发 -- GPS模块开发详解

一、了解硬件 我使用的GPS模块型号为UBX-M8030 参看:UBX-M8030 系列 查看一下它的特性: 多用途 GNSS 芯片,提供三种产品等级 最多可并发接收 3 个 GNSS(GPS、伽利略、GLONASS、北斗) 行业领先的 -167 dBm 导航灵敏度 业界最低电流消耗 在城市峡谷中具有绝佳的定位精度 安全性和完整性保护 支持所有的卫星增强系统 车载级芯片的工作...

不积跬步,无以至千里 3万+

glibc 内存分配与释放机制详解

作者:来自 vivo 互联网存储团队- Wang Yuzhi本文以一次线上故障为基础介绍了使用 glibc 进行内存管理可能碰到问题,进而对库中内存分配与释放机制进行分析,最后提供了相应问题的解决方案。一、引言内存对象的分配与释放一直是后端开发人员代码设计中需要考虑的问题,考虑不周极易造成内存泄漏、内存访问越界等问题。在发...

qq_35485206的博客 1482

RK3566/RK3568 Android 11添加系统服务

【代码】RK3566/RK3568 Android 11添加系统服务。

qq_23921815的博客 643

Docker-alpine-glibc CI/CD集成:使用GitHub Actions自动化构建与发布终极指南 [特殊字符]

想要快速掌握Docker-alpine-glibc的CI/CD自动化流程吗?本文将为您详细介绍如何使用GitHub Actions实现Docker-alpine-glibc镜像的自动化构建与发布,让您的容器化部署更加高效便捷。 ## 为什么选择Docker-alpine-glibc? 🤔 Docker-alpine-glibc是一个精心打造的Alpine Docker镜像,集成了glibc库

gitblog_00635的博客 922

AMBA-CHI协议详解(三)

Write Transactions、Atomic transactions、 Stash transactions、 Dataless transactions的等各个事务的事务流。

Crazzy_M的博客 3187

2025年华为杯中国研究生数学建模竞赛E题 高速列车轴承智能故障诊断问题 思路+代码

all_data, file_info = preprocessor.load_mat_files(file_list[:50]) # 加载前50个文件作为示例。[y_source, y_source, domain_labels], # 注意:这里使用伪标签。X_target = np.random.randn(100, X_source.shape[1], 1) # 示例数据。"""构建CNN-Transformer混合模型""""""构建Transformer模型"""

YOLO 1049

glibc 2.34 实战:从源码构建到测试全流程解析

本文详细解析了从源码构建glibc 2.34的全流程,包括准备工作、配置编译、测试验证及实际应用。针对常见的`GLIBC_2.34 not found`错误,提供了从依赖安装、避坑指南到使用`testrun.sh`和`patchelf`进行安全测试与集成的完整解决方案,帮助开发者掌控特定版本的C库环境。

view3的博客 664

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Yocto 项目中的 glibc 编译失败全解析:原因、原理与修复策略

本文详细探讨了在Yocto项目中使用glibc 2.39构建嵌入式Linux系统时可能遇到的编译失败问题。文章首先强调了glibc作为系统基础组件的重要性,随后分析了编译失败的典型原因,包括主机依赖不满足、内存不足、并发问题、源代码补丁应用失败、QEMU模拟错误以及Yocto层冲突等。针对这些问题,提供了具体的修复方法和排错步骤,如调整构建配置、检查日志文件、确保必要的工具和依赖项等。此外,文章还介绍了在开发调试阶段快速绕过构建失败的策略,并提供了一个通用的修复脚本,用于构建前的环境检测。最后,总结了处理g

Interview_TC的博客 1490

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

glibc编译及makefile机制

本文只列出glibc的makefile中比较难以找出的一些规则,一些细节可以详细阅读glibc的makefile系统。   I.编译 [redhat@localhost build]$ ../glibc-2.16.0/configure --disable-sanity-checks [redhat@localhost build]$ make | tee make.log i.

永无止境 7159

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

glibc-all-in-one性能优化:如何加速glibc下载与编译过程

你是否曾经为下载和编译glibc库而烦恼?耗时耗力的配置过程常常让开发者望而却步。今天,我将为你介绍一个终极解决方案——glibc-all-in-one工具,它能显著加速glibc下载与编译过程,让你的开发工作更加高效顺畅。 ## 📦 glibc-all-in-one项目简介 glibc-all-in-one是一个专门为glibc二进制文件和调试文件下载以及源代码自动构建设计的便捷工具。它简

gitblog_00147的博客 1042
上一篇: build 知:Makefile
下一篇: LiteOS 知:目录结构
canpool
博客等级 码龄7年 273粉丝 172原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

canpool

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

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

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

打赏作者

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

抵扣说明:

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

余额充值