kernel 4.17 with waring about ext4 soft dep on crc32c
[cinitramfs.git] / script / cpio_libs_add.sh
blobae2134f83e6268e4cd9c5b6b13c535b044fad9fc
1 #!/bin/sh
3 #will put all library dependencies in the elf interpreter directory
5 #we use readelf instead of objdump since we are dependent on elf dynamic
6 #loading technicalities
7 #the first argument is the path name of the target architecture readelf tool
8 target_readelf=$1
10 #the second argument is a colon separated path list where to look for target
11 #libs
12 lib_paths=$2
14 #the third argument is the path of the binary whose list of dependencies has
15 #to be build
16 bin=$3
18 #the fourth argument is the location of the target elf interpreter
19 elf_interpreter_location=$4
21 #the fith argument is the cpio files
22 cpio=$5
24 ################################################################################
26 lib_locate()
28 for p in $lib_paths;do
29 if test -e "$p/$1"; then
30 if file "$p/$1" | egrep -q ASCII;then
31 echo "WARNING:library $p/$1 seems to be a GNU ld script (EVIL!), skipping"
32 continue;
34 lib_path="$p/$1"
35 return
37 done
38 echo "unable to find $1 in path=$lib_paths"
39 exit 1
42 uniqify()
44 local p=
45 local paths_list=
46 echo "LIB_PATHS=$LIB_PATHS"
47 for p in $LIB_PATHS; do
48 case "$paths_list" in
49 *"$p"*) ;;
50 *) paths_list="$paths_list:$p";;
51 esac
52 done
53 #trim spurious coma
54 LIB_PATHS="${paths_list#:*}"
57 cpio_dirs_emit()
59 local cur=$(dirname $1)
60 if test "$cur" = "/"; then
61 return;
64 for p in $CPIO_DIRS_EMITED; do
66 if test "$p" = "$cur"; then
67 return;
69 done
70 cpio_dirs_emit $cur
71 echo "dir $cur 0755 0 0" >>$cpio
72 CPIO_DIRS_EMITED=$CPIO_DIRS_EMITED:$cur
75 cpio_libs_emit()
77 #we put the libs in "trusted" /lib (already emitted in the cpio file with modules)
78 #or the gnu dynamic loader will not find them while loading init
79 local p=
80 for p in $LIB_PATHS; do
81 #the shared lib soname file, may be a symbolic link, use readlink -f to sort that out
82 echo "file /lib/$(basename $p) $(readlink -f $p) 0755 0 0" >>$cpio
83 done
86 #the parameters are lib sonames
87 lib_paths_collect()
89 for soname in $*; do
90 if test "$soname" = "$(basename $ELF_BINARY_INTERPRETER)"; then
91 printf "soname $1 is elf interpreter, skipping\n--------\n"
92 continue
95 echo "collecting needed for soname=$soname"
96 local lib_path=
97 lib_locate $soname
98 echo "lib location is $lib_path";
100 local needed="$($target_readelf --dynamic "$lib_path" | egrep NEEDED | sed --regexp-extended 's/^.+\(NEEDED\).+Shared library:.+\[(.+)\]$/\1:/' | tr -d '\n')"
101 needed=${needed%:}
102 printf "needed=$needed\n--------\n"
103 lib_paths_collect $needed
104 LIB_PATHS="$LIB_PATHS:$lib_path"
105 done
106 LIB_PATHS=${LIB_PATHS#:}
109 ################################################################################
111 IFS=:
112 ELF_BINARY_SONAMES=$($target_readelf --dynamic ./init | egrep NEEDED \
113 | sed --regexp-extended \
114 's/^.+\(NEEDED\).+Shared library:.+\[(.+)\]$/\1:/' | tr -d '\n')
115 ELF_BINARY_SONAMES=${ELF_BINARY_SONAMES%:}
116 ELF_BINARY_INTERPRETER=$($target_readelf -l ./init | egrep 'Requesting program interpreter' | sed -r 's/^.+interpreter:[[:space:]]*(.+)\]/\1/')
118 echo "$3 sonames=$ELF_BINARY_SONAMES"
119 echo "$3 elf interpreter=$ELF_BINARY_INTERPRETER"
121 LIB_PATHS=
122 echo '================================================================================'
123 lib_paths_collect $ELF_BINARY_SONAMES
124 echo '================================================================================'
125 uniqify
126 CPIO_DIR_EMITED=
127 cpio_dirs_emit $ELF_BINARY_INTERPRETER
128 cpio_libs_emit
129 #sort out with readlink -f the case where the elf interpreter is a symbolic link
130 echo "file $ELF_BINARY_INTERPRETER $(readlink -f "$elf_interpreter_location") 0755 0 0" >>$cpio