]> git.ipfire.org Git - people/arne_f/ipfire-3.x.git/blob - pkgs/build-essentials/buildsystem-tools/functions-files
avahi: Update to 0.6.30.
[people/arne_f/ipfire-3.x.git] / pkgs / build-essentials / buildsystem-tools / functions-files
1 #!/bin/bash
2
3 # Check if a file is an ELF binary
4 #
5 function file_is_elf() {
6 local file=${1}
7
8 file "${file}" | grep -q "ELF"
9 }
10
11 # Check if a file is a script.
12 # If the first line starts with #! this is sufficient.
13 #
14 function file_is_script() {
15 local file=${1}
16
17 local first_line=$(head -n1 ${file})
18
19 [ "${first_line:0:2}" = "#!" ]
20 }
21
22 # Get the interpreter of a file.
23 #
24 function file_get_interpreter() {
25 local file=${1}
26
27 if file_is_elf ${file}; then
28 _file_get_elf_interpreter ${file}
29 elif file_is_script ${file}; then
30 _file_get_script_interpreter ${file}
31 fi
32 }
33
34 # Hidden function that gets the interpreter from an ELF file.
35 #
36 function _file_get_elf_interpreter() {
37 local file=${1}
38
39 readelf -l ${file} | grep "program interpreter" | \
40 tr -d "]" | awk '{ print $NF }'
41 }
42
43 # Hidden fucntion that gets the interpreter from a script file.
44 #
45 function _file_get_script_interpreter() {
46 local file=${1}
47
48 # If the file is not executeable, no interpreter will be needed
49 [ -x "${file}" ] || return
50
51 local first_line=$(head -n1 ${file})
52
53 first_line="${first_line:2:${#first_line}}"
54
55 # Choose the first argument and strip any parameters if available
56 local interpreter
57 for interpreter in ${first_line}; do
58 echo "${interpreter}"
59 return
60 done
61 }
62
63 # Check if a file is statically linked.
64 #
65 function file_is_static() {
66 local file=${1}
67
68 file ${file} | grep -q "statically linked"
69 }
70
71 # Get NEEDED from a file.
72 #
73 function file_get_needed() {
74 local file=${1}
75
76 readelf -d ${file} | grep NEEDED | \
77 tr -d "[]" | awk '{ print $NF }'
78 }
79
80 # Get RPATH from a file.
81 #
82 function file_get_rpath() {
83 local file=${1}
84
85 readelf -d ${file} | grep RPATH | \
86 tr -d "[]" | awk '{ print $NF }'
87 }
88
89 # Get SONAME from a file.
90 #
91 function file_get_soname() {
92 local file=${1}
93
94 local file_basename=$(basename ${file})
95 if [ "${file_basename:0:3}" = "ld-" ]; then
96 log DEBUG "Don't return a SONAME for linkers: ${file}"
97 return
98 fi
99
100 readelf -d ${file} | grep SONAME | \
101 tr -d "[]" | awk '{ print $NF }'
102 }
103
104 # Check if a file is a shared object.
105 #
106 function file_is_shared_object() {
107 local file=${1}
108
109 file ${file} | grep -q "shared object"
110 }
111
112 # Check if a file has the canary.
113 #
114 function file_has_canary() {
115 local file=${1}
116
117 readelf -s ${file} | grep -q "__stack_chk_fail"
118 }
119
120 # Check if a file has an executeable stack.
121 #
122 function file_has_execstack() {
123 local file=${1}
124
125 readelf -h ${file} | grep -qE "Type:[[:space:]]*EXEC"
126 }
127
128 # Check if a file has NX.
129 #
130 function file_has_nx() {
131 local file=${1}
132
133 readelf -l ${file} | grep "GNU_STACK" | grep -q "RWE"
134 [ $? != 0 ]
135 }
136
137 # Check if a file is partly RELRO.
138 #
139 function file_is_relro_partly() {
140 local file=${1}
141
142 readelf -l ${file} | grep -q "GNU_RELRO"
143 }
144
145 # Check if a file is fully RELRO.
146 #
147 function file_is_relro_full() {
148 local file=${1}
149
150 if file_is_relro_partly ${file}; then
151 readelf -d ${file} | grep -q "BIND_NOW"
152 return $?
153 fi
154 return 1
155 }
156
157 # Find all ELF files.
158 #
159 function find_elf_files() {
160 local dir
161 local dirs
162 local prefix
163
164 while [ $# -gt 0 ]; do
165 case "${1}" in
166 --prefix=*)
167 prefix="${1#--prefix=}/"
168 ;;
169 *)
170 dirs="${dirs} ${1}"
171 ;;
172 esac
173 shift
174 done
175
176 local file
177 local files
178
179 for dir in ${dirs}; do
180 dir="${prefix}${dir}"
181 for file in $(find ${dir} -type f 2>/dev/null); do
182 if file_is_elf ${file} && ! file_is_static ${file}; then
183 files="${files} ${file}"
184 fi
185 done
186 done
187
188 echo ${files}
189 }
190
191 function filter_startfiles() {
192 local file=${1}
193
194 grep -qE "crt[1in]\.o$" <<<${file}
195 }