3 # Check if a file is an ELF binary
5 function file_is_elf
() {
8 file "${file}" |
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 local first_line
=$
(head -n1 ${file})
19 [ "${first_line:0:2}" = "#!" ]
22 # Get the interpreter of a file.
24 function file_get_interpreter
() {
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}
34 # Hidden function that gets the interpreter from an ELF file.
36 function _file_get_elf_interpreter
() {
39 readelf
-l ${file} |
grep "program interpreter" | \
40 tr -d "]" |
awk '{ print $NF }'
43 # Hidden fucntion that gets the interpreter from a script file.
45 function _file_get_script_interpreter
() {
48 # If the file is not executeable, no interpreter will be needed
49 [ -x "${file}" ] ||
return
51 local first_line
=$
(head -n1 ${file})
53 first_line
="${first_line:2:${#first_line}}"
55 # Choose the first argument and strip any parameters if available
57 for interpreter
in ${first_line}; do
63 # Check if a file is statically linked.
65 function file_is_static
() {
68 file ${file} |
grep -q "statically linked"
71 # Get NEEDED from a file.
73 function file_get_needed
() {
76 readelf
-d ${file} |
grep NEEDED | \
77 tr -d "[]" |
awk '{ print $NF }'
80 # Get RPATH from a file.
82 function file_get_rpath
() {
85 readelf
-d ${file} |
grep RPATH | \
86 tr -d "[]" |
awk '{ print $NF }'
89 # Get SONAME from a file.
91 function file_get_soname
() {
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}"
100 readelf
-d ${file} |
grep SONAME | \
101 tr -d "[]" |
awk '{ print $NF }'
104 # Check if a file is a shared object.
106 function file_is_shared_object
() {
109 file ${file} |
grep -q "shared object"
112 # Check if a file has the canary.
114 function file_has_canary
() {
117 readelf
-s ${file} |
grep -q "__stack_chk_fail"
120 # Check if a file has an executeable stack.
122 function file_has_execstack
() {
125 readelf
-h ${file} |
grep -qE "Type:[[:space:]]*EXEC"
128 # Check if a file has NX.
130 function file_has_nx
() {
133 readelf
-l ${file} |
grep "GNU_STACK" |
grep -q "RWE"
137 # Check if a file is partly RELRO.
139 function file_is_relro_partly
() {
142 readelf
-l ${file} |
grep -q "GNU_RELRO"
145 # Check if a file is fully RELRO.
147 function file_is_relro_full
() {
150 if file_is_relro_partly
${file}; then
151 readelf
-d ${file} |
grep -q "BIND_NOW"
157 # Find all ELF files.
159 function find_elf_files
() {
164 while [ $# -gt 0 ]; do
167 prefix
="${1#--prefix=}/"
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}"
191 function filter_startfiles
() {
194 grep -qE "crt[1in]\.o$" <<<${file}