コンパイラ作成(10) clang先輩

clangのアセンブラ

まずはclangのアセンブラの話。mycで生成したアセンブラコードはclangでアセンブル、リンクしてるんだけど、clangはどうやってアセンブルしてるんだろうか。Gasなりなんなりに処理を投げてる?良く分からないんでちょっと調べてみたよ。

Assember

Clang can either use LLVM’s integrated assembler or an external system-specific tool (for instance, the GNU Assembler on GNU OSes) to produce machine code from assembly. By default, Clang uses LLVM’s integrated assembler on all targets where it is supported. If you wish to use the system assember instead, use the -fno-integrated-as option.

Assembling a Complete Toolchain — Clang 7 documentation

これみるとintegrated assemblerって独自のアセンブラで処理してるみたい。こいつの細かい仕様がどうなってるのかは資料を見つけられなかったよ。なんとなくGasと大体互換っぽい?からUsing as: Topを参照してる。そういやclangって今Ver7.0まで進んでるんだな。特に困ったことないんで昔インストールしたVer5.0をずっと使ってるよ。

clangでアセンブリ出力

前回の記事でちょっと書いたけど、mycのコード生成の参考にclang先輩のアセンブリコードを見て勉強してるよ。

~/myc/clang$ clang-5.0 -x c -S -mllvm -x86-asm-syntax=intel -fno-asynchronous-unwind-tables -O0 c.myc 

こんな感じでintel形式のコードを出力させてる。-fnoなんちゃらってのはCFI関連のディレクティブが出力されないようにするためのオプション。CFIってのはmalwareとかが不正な関数呼び出しをしてプログラムを乗っ取ってないかをチェックする仕組み。mycの参考にするには邪魔なんで出力しないようにしてるよ。
参考:Control-flow integrity - Wikipedia