3 # Check if a file is an ELF binary
5 function file_is_elf
() {
8 file "${file}" 2>/dev
/null |
grep -q "ELF"
11 # Check if a file is a script.
12 # If the first line starts with #! this is sufficient.
14 function file_is_script
() {
17 file ${file} 2>/dev
/null |
egrep -q ":.* (commands|script)"
20 # Check if file is an 64-bit binary.
21 function file_is_64bit
() {
24 file -L ${file} 2>/dev
/null |
grep -q "ELF 64-bit"
27 # Get the interpreter of a file.
29 function file_get_interpreter
() {
32 if file_is_elf
${file}; then
33 file_get_elf_interpreter
${file}
34 elif file_is_script
${file}; then
35 file_get_script_interpreter
${file}
39 # Hidden function that gets the interpreter from an ELF file.
41 function file_get_elf_interpreter
() {
44 local interp
=$
(readelf
-l ${file} 2>/dev
/null | \
45 grep "program interpreter" |
tr -d "]" |
awk '{ print $NF }')
47 # Only return real file names. Debugging files do not
48 # have those starting with a /.
49 if [ "${interp:0:1}" = "/" ]; then
55 # Hidden fucntion that gets the interpreter from a script file.
57 function file_get_script_interpreter
() {
60 # If the file is not executeable, no interpreter will be needed
61 [ -x "${file}" ] ||
return
63 local first_line
=$
(head -n1 ${file})
65 # Return nothing if no shebang was found.
66 [ "${first_line:0:2}" = "#!" ] ||
return
68 first_line
="${first_line:2:${#first_line}}"
70 # Choose the first argument and strip any parameters if available
72 for interpreter
in ${first_line}; do
73 # Skip interpreters that do that have an absolute path.
74 [ "${interpreter:0:1}" = "/" ] ||
break
81 # Check if a file is statically linked.
83 function file_is_static
() {
86 file ${file} 2>/dev
/null |
grep -q "statically linked"
89 # Get NEEDED from a file.
91 function file_get_needed
() {
94 local out
=$
(readelf
-d ${file} 2>/dev
/null |
grep NEEDED | \
95 tr -d "[]" |
awk '{ print $NF }')
97 echo "${out}$(file_is_64bit ${file})"
100 # Get RPATH from a file.
102 function file_get_rpath
() {
105 readelf
-d ${file} 2>/dev
/null |
grep RPATH | \
106 tr -d "[]" |
awk '{ print $NF }'
109 # Get SONAME from a file.
111 function file_get_soname
() {
114 objdump
-p ${file} 2>/dev
/null |
awk '/SONAME/ { print $2 }'
117 # Check if a file is a shared object.
119 function file_is_shared_object
() {
122 file ${file} 2>/dev
/null |
grep -q "shared object"
125 # Check if a file has the canary.
127 function file_has_canary
() {
130 readelf
-s ${file} 2>/dev
/null |
grep -q "__stack_chk_fail"
133 # Check if a file has an executeable stack.
135 function file_has_execstack
() {
138 readelf
-h ${file} 2>/dev
/null |
grep -qE "Type:[[:space:]]*EXEC"
141 # Check if a file has NX.
143 function file_has_nx
() {
146 readelf
-l ${file} 2>/dev
/null |
grep "GNU_STACK" |
grep -q "RWE"
150 # Check if a file is partly RELRO.
152 function file_is_relro_partly
() {
155 readelf
-l ${file} 2>/dev
/null |
grep -q "GNU_RELRO"
158 # Check if a file is fully RELRO.
160 function file_is_relro_full
() {
163 if file_is_relro_partly
${file}; then
164 readelf
-d ${file} 2>/dev
/null |
grep -q "BIND_NOW"
170 # Find all ELF files.
172 function find_elf_files
() {
177 while [ $# -gt 0 ]; do
180 prefix
="${1#--prefix=}/"
192 for dir
in ${dirs}; do
193 dir
="${prefix}${dir}"
194 for file in $
(find ${dir} -type f
-not -name "*.ko" 2>/dev
/null
); do
196 # Skip kernel modules.
202 if file_is_elf
${file} && ! file_is_static
${file}; then
203 files
="${files} ${file}"
211 function filter_startfiles
() {
214 grep -qE "crt[1in]\.o$" <<<${file}