some minor cleanup
[cinitramfs.git] / script / cpio_libs_add.sh
blob9cad189931c02ca74bc8722542f9e7a860cb1dee
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 for p in $LIB_PATHS; do
47 case "$paths_list" in
48 *"$p"*) ;;
49 *) paths_list="$paths_list:$p";;
50 esac
51 done
52 #trim spurious coma
53 LIB_PATHS="${paths_list#:*}"
56 cpio_dirs_emit()
58 local cur
59 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 local p
78 for p in $LIB_PATHS; do
79 #the shared lib soname file, may be a symbolic link, use realpath to sort that out
80 echo "file $(dirname $ELF_BINARY_INTERPRETER)/$(basename $p) $(realpath $p) 0755 0 0" >>$cpio
81 done
84 #the parameters are lib sonames
85 lib_paths_collect()
87 for soname in $*; do
88 if test "$soname" == "$(basename $ELF_BINARY_INTERPRETER)"; then
89 echo -e "soname $1 is elf interpreter, skipping\n--------"
90 continue
93 echo -e "collecting needed for soname=$soname"
94 local lib_path=
95 lib_locate $soname
96 echo "lib location is $lib_path";
98 local needed=$($target_readelf --dynamic "$lib_path" | egrep NEEDED | sed --regexp-extended 's/^.+\(NEEDED\).+Shared library:.+\[(.+)\]$/\1:/' | tr -d '\n')
99 needed=${needed%:}
100 echo -e "needed=$needed\n--------"
101 lib_paths_collect $needed
102 LIB_PATHS="$LIB_PATHS:$lib_path"
103 done
104 LIB_PATHS=${LIB_PATHS#:}
107 ################################################################################
109 IFS=:
110 ELF_BINARY_SONAMES=$($target_readelf --dynamic ./init | egrep NEEDED \
111 | sed --regexp-extended \
112 's/^.+\(NEEDED\).+Shared library:.+\[(.+)\]$/\1:/' | tr -d '\n')
113 ELF_BINARY_SONAMES=${ELF_BINARY_SONAMES%:}
114 ELF_BINARY_INTERPRETER=$($target_readelf -l ./init | egrep 'Requesting program interpreter' | sed -r 's/^.+interpreter:[[:space:]]*(.+)\]/\1/')
116 echo "$3 sonames=$ELF_BINARY_SONAMES"
117 echo "$3 elf interpreter=$ELF_BINARY_INTERPRETER"
119 LIB_PATHS=
120 echo '================================================================================'
121 lib_paths_collect $ELF_BINARY_SONAMES
122 echo '================================================================================'
123 uniqify
124 CPIO_DIR_EMITED=
125 cpio_dirs_emit $ELF_BINARY_INTERPRETER
126 cpio_libs_emit
127 #sort out with realpath the case where the elf interpreter is a symbolic link
128 echo "file $ELF_BINARY_INTERPRETER $(realpath "$elf_interpreter_location") 0755 0 0" >>$cpio