요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
=========================
GCC plugin infrastructure
=========================
Introduction
============
GCC plugins are loadable modules that provide extra features to the
compiler [1]_. They are useful for runtime instrumentation and static analysis.
We can analyse, change and add further code during compilation via
callbacks [2]_, GIMPLE [3]_, IPA [4]_ and RTL passes [5]_.
The GCC plugin infrastructure of the kernel supports building out-of-tree
modules, cross-compilation and building in a separate directory.
Plugin source files have to be compilable by a C++ compiler.
Currently the GCC plugin infrastructure supports only some architectures.
Grep "select HAVE_GCC_PLUGINS" to find out which architectures support
GCC plugins.
This infrastructure was ported from grsecurity [6]_ and PaX [7]_.
--
.. [1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html
.. [2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API
.. [3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html
.. [4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html
.. [5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html
.. [6] https://grsecurity.net/
.. [7] https://pax.grsecurity.net/
Purpose
=======
GCC plugins are designed to provide a place to experiment with potential
compiler features that are neither in GCC nor Clang upstream. Once
their utility is proven, the goal is to upstream the feature into GCC
(and Clang), and then to finally remove them from the kernel once the
feature is available in all supported versions of GCC.
Specifically, new plugins should implement only features that have no
upstream compiler support (in either GCC or Clang).
When a feature exists in Clang but not GCC, effort should be made to
bring the feature to upstream GCC (rather than just as a kernel-specific
GCC plugin), so the entire ecosystem can benefit from it.
Similarly, even if a feature provided by a GCC plugin does *not* exist
in Clang, but the feature is proven to be useful, effort should be spent
to upstream the feature to GCC (and Clang).
After a feature is available in upstream GCC, the plugin will be made
unbuildable for the corresponding GCC version (and later). Once all
kernel-supported versions of GCC provide the feature, the plugin will
be removed from the kernel.
Files
=====
**$(src)/scripts/gcc-plugins**
This is the directory of the GCC plugins.
**$(src)/scripts/gcc-plugins/gcc-common.h**
This is a compatibility header for GCC plugins.
It should be always included instead of individual gcc headers.
**$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h,
$(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h,
$(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h,
$(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h**
These headers automatically generate the registration structures for
GIMPLE, SIMPLE_IPA, IPA and RTL passes.
They should be preferred to creating the structures by hand.
Usage
=====
You must install the gcc plugin headers for your gcc version,
e.g., on Ubuntu for gcc-10::
apt-get install gcc-10-plugin-dev
Or on Fedora::
dnf install gcc-plugin-devel libmpc-devel
Or on Fedora when using cross-compilers that include plugins::
dnf install libmpc-devel
Enable the GCC plugin infrastructure and some plugin(s) you want to use
in the kernel config::
CONFIG_GCC_PLUGINS=y
CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y
...
Run gcc (native or cross-compiler) to ensure plugin headers are detected::
gcc -print-file-name=plugin
CROSS_COMPILE=arm-linux-gnu- ${CROSS_COMPILE}gcc -print-file-name=plugin
The word "plugin" means they are not detected::
plugin
A full path means they are detected::
/usr/lib/gcc/x86_64-redhat-linux/12/plugin
To compile the minimum tool set including the plugin(s)::
make scripts
or just run the kernel make and compile the whole kernel with
the cyclomatic complexity GCC plugin.
4. How to add a new GCC plugin
==============================
The GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files
right under scripts/gcc-plugins/. Creating subdirectories is not supported.
It must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins
and a relevant Kconfig file.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
GCC plugin infrastructure 소개
1-33GCC plugin은 compiler에 추가 기능을 제공하는 loadable module입니다. Runtime instrumentation과 static analysis에 유용하며 callback, GIMPLE, IPA, RTL pass를 통해 compile 중인 code를 분석하거나 바꾸고 새 code를 추가할 수 있습니다.
Compile pipeline에서 사용할 수 있는 주요 interface입니다.
Kernel의 GCC plugin infrastructure는 out-of-tree module build, cross-compilation, 별도 build directory를 지원합니다. Plugin source file은 C++ compiler로 compile할 수 있어야 합니다.
현재 일부 architecture만 GCC plugin infrastructure를 지원합니다. Kernel source에서 `select HAVE_GCC_PLUGINS`를 검색하면 지원 architecture를 찾을 수 있습니다. 이 infrastructure는 grsecurity와 PaX에서 port되었습니다.
원문 각주가 연결하는 compiler interface와 project입니다.
=========================
GCC plugin infrastructure
=========================
Introduction
============
GCC plugins are loadable modules that provide extra features to the
compiler [1]_. They are useful for runtime instrumentation and static analysis.
We can analyse, change and add further code during compilation via
callbacks [2]_, GIMPLE [3]_, IPA [4]_ and RTL passes [5]_.
The GCC plugin infrastructure of the kernel supports building out-of-tree
modules, cross-compilation and building in a separate directory.
Plugin source files have to be compilable by a C++ compiler.
Currently the GCC plugin infrastructure supports only some architectures.
Grep "select HAVE_GCC_PLUGINS" to find out which architectures support
GCC plugins.
This infrastructure was ported from grsecurity [6]_ and PaX [7]_.
--
.. [1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html
.. [2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API
.. [3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html
.. [4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html
.. [5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html
.. [6] https://grsecurity.net/
.. [7] https://pax.grsecurity.net/
실험에서 upstream과 제거까지
34-60GCC plugin은 GCC와 Clang upstream 어느 쪽에도 아직 없는 잠재적 compiler 기능을 실험하는 장소입니다. 유용성이 입증되면 기능을 GCC와 Clang upstream에 넣고, 모든 kernel 지원 GCC version에서 사용할 수 있게 된 뒤 kernel의 plugin을 제거하는 것이 목표입니다.
따라서 새 plugin은 GCC나 Clang에 upstream 지원이 없는 기능만 구현해야 합니다. 기능이 Clang에는 있지만 GCC에는 없다면 kernel 전용 GCC plugin으로만 만들기보다 upstream GCC에 도입해 전체 ecosystem이 혜택을 받도록 노력해야 합니다.
GCC plugin이 제공하는 기능이 Clang에도 없더라도 유용성이 입증되면 GCC와 Clang upstream 도입에 힘써야 합니다.
기능이 upstream GCC에 들어오면 해당 GCC version 이상에서는 plugin을 build할 수 없게 만듭니다. Kernel이 지원하는 모든 GCC version이 그 기능을 제공하면 plugin을 kernel에서 제거합니다.
Kernel plugin은 영구 구현이 아니라 upstream compiler 기능으로 가는 실험 단계입니다.
Purpose
=======
GCC plugins are designed to provide a place to experiment with potential
compiler features that are neither in GCC nor Clang upstream. Once
their utility is proven, the goal is to upstream the feature into GCC
(and Clang), and then to finally remove them from the kernel once the
feature is available in all supported versions of GCC.
Specifically, new plugins should implement only features that have no
upstream compiler support (in either GCC or Clang).
When a feature exists in Clang but not GCC, effort should be made to
bring the feature to upstream GCC (rather than just as a kernel-specific
GCC plugin), so the entire ecosystem can benefit from it.
Similarly, even if a feature provided by a GCC plugin does *not* exist
in Clang, but the feature is proven to be useful, effort should be spent
to upstream the feature to GCC (and Clang).
After a feature is available in upstream GCC, the plugin will be made
unbuildable for the corresponding GCC version (and later). Once all
kernel-supported versions of GCC provide the feature, the plugin will
be removed from the kernel.
Plugin directory와 compatibility header
61-82`$(src)/scripts/gcc-plugins`는 GCC plugin directory입니다.
`$(src)/scripts/gcc-plugins/gcc-common.h`는 GCC plugin용 compatibility header입니다. 개별 GCC header를 직접 include하지 말고 항상 이 header를 include해야 합니다.
`gcc-generate-gimple-pass.h`, `gcc-generate-ipa-pass.h`, `gcc-generate-simple_ipa-pass.h`, `gcc-generate-rtl-pass.h`는 각각 GIMPLE, IPA, SIMPLE_IPA, RTL pass의 등록 구조체를 자동 생성합니다. 구조체를 직접 만드는 것보다 이 header를 우선 사용해야 합니다.
공통 header와 pass registration generator의 역할입니다.
Files
=====
**$(src)/scripts/gcc-plugins**
This is the directory of the GCC plugins.
**$(src)/scripts/gcc-plugins/gcc-common.h**
This is a compatibility header for GCC plugins.
It should be always included instead of individual gcc headers.
**$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h,
$(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h,
$(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h,
$(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h**
These headers automatically generate the registration structures for
GIMPLE, SIMPLE_IPA, IPA and RTL passes.
They should be preferred to creating the structures by hand.
Dependency 설치, 설정과 plugin 탐지
83-126사용 중인 GCC version에 맞는 plugin header를 설치해야 합니다. Ubuntu에서 GCC 10은 `apt-get install gcc-10-plugin-dev`, Fedora는 `dnf install gcc-plugin-devel libmpc-devel`을 사용합니다. Plugin을 포함한 cross-compiler를 Fedora에서 쓸 때는 `dnf install libmpc-devel`이면 됩니다.
Compiler와 distribution에 따른 package 설치 예입니다.
Kernel config에서 GCC plugin infrastructure와 원하는 plugin을 활성화합니다. 예제는 `CONFIG_GCC_PLUGINS=y`와 `CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y`를 설정합니다.
Native GCC 또는 cross-compiler에 `-print-file-name=plugin`을 실행해 plugin header 탐지를 확인합니다. 출력이 단어 `plugin`뿐이면 탐지되지 않은 것이고, `/usr/lib/gcc/x86_64-redhat-linux/12/plugin` 같은 전체 path가 나오면 탐지된 것입니다.
`-print-file-name=plugin` 출력 해석입니다.
Prefix를 지정한 compiler binary에 같은 query를 적용합니다.
Plugin을 포함한 최소 tool set만 compile하려면 `make scripts`를 실행합니다. 또는 일반 kernel build를 실행하여 cyclomatic complexity GCC plugin을 포함한 전체 kernel을 compile할 수 있습니다.
Usage
=====
You must install the gcc plugin headers for your gcc version,
e.g., on Ubuntu for gcc-10::
apt-get install gcc-10-plugin-dev
Or on Fedora::
dnf install gcc-plugin-devel libmpc-devel
Or on Fedora when using cross-compilers that include plugins::
dnf install libmpc-devel
Enable the GCC plugin infrastructure and some plugin(s) you want to use
in the kernel config::
CONFIG_GCC_PLUGINS=y
CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y
...
Run gcc (native or cross-compiler) to ensure plugin headers are detected::
gcc -print-file-name=plugin
CROSS_COMPILE=arm-linux-gnu- ${CROSS_COMPILE}gcc -print-file-name=plugin
The word "plugin" means they are not detected::
plugin
A full path means they are detected::
/usr/lib/gcc/x86_64-redhat-linux/12/plugin
To compile the minimum tool set including the plugin(s)::
make scripts
or just run the kernel make and compile the whole kernel with
the cyclomatic complexity GCC plugin.
새 GCC plugin 추가
127-133GCC plugin은 `scripts/gcc-plugins/`에 있습니다. 새 plugin source file도 이 directory 바로 아래에 두어야 하며 subdirectory는 지원하지 않습니다.
새 plugin은 `scripts/gcc-plugins/Makefile`, `scripts/Makefile.gcc-plugins`, 관련 Kconfig file에 각각 추가해야 합니다.
Source 배치와 build·configuration 등록에 필요한 파일입니다.
4. How to add a new GCC plugin
==============================
The GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files
right under scripts/gcc-plugins/. Creating subdirectories is not supported.
It must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins
and a relevant Kconfig file.
요약·해설
gcc-plugins.rst:1-133Kernel GCC plugin은 upstream compiler에 아직 없는 instrumentation·analysis 기능을 검증하는 임시 실험장입니다. 기능이 GCC와 Clang에 들어가 모든 지원 GCC에서 제공되면 plugin은 kernel에서 제거됩니다.
Dependency 확인부터 upstream 전환까지입니다.