Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа №1 / lab1 / lab1
.sh#!/bin/bash
#Список ELF-файлов в папке
listELF()
{
dir=$1
if ! [ -d $dir ]
then
echo "Error. $dir is not directory."
return 1
fi
echo "Listing the elf's modules in dir" $2
allFiles=$(ls $dir)
for file in $allFiles
do
if (file $dir$file | grep ELF) #> /dev/null)
then
echo $file " is ELF file"
fi
done
}
#Точка входа для программы и дизасм первых строк
entryPoint()
{
file=$1
if ! [ -f $file ]
then
echo "Error. $file not exist or not simple file"
return 1
fi
`objdump -f $file > objDumpRes.txt`
start=`tail -c 10 objDumpRes.txt`
rm objDumpRes.txt
start=`hex2dec $start`
stop=`expr $start + 10`
objdump -d --start-address=$start --stop-address=$stop
}
#Вывод списка секций
listSections()
{
file=$1
if ! [ -f $file ]
then
echo "Error. $file not exist or not simple file"
return 1
fi
readelf -S $file
}
#Проверка наличия строки в секции .dynstr
checkDstr()
{
if ! [ $# -eq 2 ]
then
echo "Error. No such argument."
fi
file=$1
if ! [ -f $file ]
then
echo "Error. $file not exist or not simple file."
return 1
fi
string=$2
if (objdump -d -j .dynstr $file | grep $string > /dev/null)
then
echo "String contains in section .dynstr"
else
echo "String not contains in section .dynstr"
fi
}
#Список неопределённых символов
listUndefined()
{
file=$1
if ! [ -f $file ]
then
echo "Error. $file not exist or not simple file."
return 1
fi
nm -u $file
}
#Список всех RO-секций и их суммарный размер
allRO()
{
file=$1
length=0
if ! [ -f $file ]
then
echo "Error. $file not exist or not simple file."
fi
readelf -S $file | grep -v W > tmp
cut -c 58-64 tmp > tmp1
head -n 2 tmp1>tmp
sum=0
for sizeHex in `grep "" tmp1`
do
sizeDec=`hex2dec $sizeHex`
sum=`expr $sum + $sizeDec`
done
echo "Summary length of all RO sections is: $sum"
rm tmp
rm tmp1
}
#Дизасм по адресу
disasm()
{
if ! [ $# -eq 3 ]
then
echo "No such arguments."
return 1
fi
file=$1
if ! [ -f $file ]
then
echo "Error. $file not exist or not simple file."
return 1
fi
objdump -d --start-address=$2 --stop-address=$3 $file
}
#Основная программа
if [ $# -ne 2 ]
then
echo "No such argument"
fi
case $1 in
"-le")
listELF $2
;;
"-ep")
entryPoint $2
;;
"-ls")
listSections $2
;;
"-ds")
checkDstr $2 $3
;;
"-us")
listUndefined $2
;;
"-da")
disasm $2 $3 $4
;;
"-ro")
allRO $2
;;
"*")
echo "Unsupported operation. Use \"-le\",\"-ep\",\"-ls\",\"-ds\" command"
;;
esac