2016-04-28 11 views
0
私は、ディレクトリ内の各ファイルに次の2つのコマンドを実行する必要があり

からセクションを削除するには。 0xXXXは、いくつかの進数でスクリプトは、ELFバイナリ

Machine: <unknown>: 0xXXX 

最初のコマンドの出力は次のようになります。 <Machine>は、最初のコマンドから進数であり、A_FILE.STRIPPEDobjcopyから出力ファイルの名前である

B)第2のコマンド

objcopy -I elf64-x86-64 -O elf64-x86-64 -R .source -R .llvmir -R .amdil --alt-machine-code=<Machine> A_FILE A_FILE.STRIPPED 

。 (STRIPPEDは任意であり、テキストの任意の部分であってもよい)

+0

は '第2のコマンドに含まれる、または従うだけ進数字こと0x'べきか? –

+0

...私はまた、質問のタイトルを「write-this-for-me!」より少し少なく更新しようとしました。スクリプトが実際に何をしているのかを表現するために、そのことは開発ツールに関連するものであり、したがって局所的であるからです。 :) –

+0

ありがとう、チャールズ。はい、 '0x'は2番目のコマンドに含める必要があります。 – Jacko

答えて

1
#!/bin/bash 
#  ^^^^- important, not /bin/sh 

# define a regex, in ERE form, to extract the content you want in a match group 
re='machine.*(0x[[:xdigit:]]{2,}) ' 

# iterate over files, putting each in $f 
for f in *; do 

    # don't operate on files we previously generated 
    [[ $f = *.stripped ]] && continue 

    # actually run readelf, taking first matching line 
    m_line=$(readelf -aW "$f" | egrep -m 1 "$re") 

    [[ $m_line =~ $re ]] || continue # check whether we match the regex 

    # if we get here, the regex matched; copy the first match group into a variable 
    code=${BASH_REMATCH[1]} 

    # ...and use that variable in calling objcopy 
    objcopy -I elf64-x86-64 -O elf64-x86-64 -R .source -R .llvmir -R .amdil \ 
    --alt-machine-code="$code" \ 
    "$f" "$f.stripped" 
done 
+0

パーフェクト!再度、感謝します。 – Jacko

関連する問題