← Documents Documentation/kbuild/gcc-plugins.rst GitHub 원문 ↗

Linux 6.18.37 · Kbuild

GCC plugin infrastructure

Kernel GCC plugin의 실험 목적, compatibility header, dependency와 탐지, upstream 후 제거 정책을 설명합니다.

Source pathDocumentation/kbuild/gcc-plugins.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

gcc-plugins.rst:1-133

Kernel GCC plugin은 upstream compiler에 아직 없는 instrumentation·analysis 기능을 검증하는 임시 실험장입니다. 기능이 GCC와 Clang에 들어가 모든 지원 GCC에서 제공되면 plugin은 kernel에서 제거됩니다.

GCC plugin 작업 경로
Architecture·plugin header 지원 확인Kconfig에서 infrastructure와 plugin 활성화`make scripts` 또는 kernel build기능 유용성 검증Compiler upstream 추진지원 version 확대 후 kernel plugin 제거

Dependency 확인부터 upstream 전환까지입니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 =========================
2 GCC plugin infrastructure
3 =========================
4
5
6 Introduction
7 ============
8
9 GCC plugins are loadable modules that provide extra features to the
10 compiler [1]_. They are useful for runtime instrumentation and static analysis.
11 We can analyse, change and add further code during compilation via
12 callbacks [2]_, GIMPLE [3]_, IPA [4]_ and RTL passes [5]_.
13
14 The GCC plugin infrastructure of the kernel supports building out-of-tree
15 modules, cross-compilation and building in a separate directory.
16 Plugin source files have to be compilable by a C++ compiler.
17
18 Currently the GCC plugin infrastructure supports only some architectures.
19 Grep "select HAVE_GCC_PLUGINS" to find out which architectures support
20 GCC plugins.
21
22 This infrastructure was ported from grsecurity [6]_ and PaX [7]_.
23
24 --
25
26 .. [1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html
27 .. [2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API
28 .. [3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html
29 .. [4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html
30 .. [5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html
31 .. [6] https://grsecurity.net/
32 .. [7] https://pax.grsecurity.net/
33
34
35 Purpose
36 =======
37
38 GCC plugins are designed to provide a place to experiment with potential
39 compiler features that are neither in GCC nor Clang upstream. Once
40 their utility is proven, the goal is to upstream the feature into GCC
41 (and Clang), and then to finally remove them from the kernel once the
42 feature is available in all supported versions of GCC.
43
44 Specifically, new plugins should implement only features that have no
45 upstream compiler support (in either GCC or Clang).
46
47 When a feature exists in Clang but not GCC, effort should be made to
48 bring the feature to upstream GCC (rather than just as a kernel-specific
49 GCC plugin), so the entire ecosystem can benefit from it.
50
51 Similarly, even if a feature provided by a GCC plugin does *not* exist
52 in Clang, but the feature is proven to be useful, effort should be spent
53 to upstream the feature to GCC (and Clang).
54
55 After a feature is available in upstream GCC, the plugin will be made
56 unbuildable for the corresponding GCC version (and later). Once all
57 kernel-supported versions of GCC provide the feature, the plugin will
58 be removed from the kernel.
59
60
61 Files
62 =====
63
64 **$(src)/scripts/gcc-plugins**
65
66 This is the directory of the GCC plugins.
67
68 **$(src)/scripts/gcc-plugins/gcc-common.h**
69
70 This is a compatibility header for GCC plugins.
71 It should be always included instead of individual gcc headers.
72
73 **$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h,
74 $(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h,
75 $(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h,
76 $(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h**
77
78 These headers automatically generate the registration structures for
79 GIMPLE, SIMPLE_IPA, IPA and RTL passes.
80 They should be preferred to creating the structures by hand.
81
82
83 Usage
84 =====
85
86 You must install the gcc plugin headers for your gcc version,
87 e.g., on Ubuntu for gcc-10::
88
89 apt-get install gcc-10-plugin-dev
90
91 Or on Fedora::
92
93 dnf install gcc-plugin-devel libmpc-devel
94
95 Or on Fedora when using cross-compilers that include plugins::
96
97 dnf install libmpc-devel
98
99 Enable the GCC plugin infrastructure and some plugin(s) you want to use
100 in the kernel config::
101
102 CONFIG_GCC_PLUGINS=y
103 CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y
104 ...
105
106 Run gcc (native or cross-compiler) to ensure plugin headers are detected::
107
108 gcc -print-file-name=plugin
109 CROSS_COMPILE=arm-linux-gnu- ${CROSS_COMPILE}gcc -print-file-name=plugin
110
111 The word "plugin" means they are not detected::
112
113 plugin
114
115 A full path means they are detected::
116
117 /usr/lib/gcc/x86_64-redhat-linux/12/plugin
118
119 To compile the minimum tool set including the plugin(s)::
120
121 make scripts
122
123 or just run the kernel make and compile the whole kernel with
124 the cyclomatic complexity GCC plugin.
125
126
127 4. How to add a new GCC plugin
128 ==============================
129
130 The GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files
131 right under scripts/gcc-plugins/. Creating subdirectories is not supported.
132 It must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins
133 and a relevant Kconfig file.
134

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

GCC plugin infrastructure 소개

1-33

GCC plugin은 compiler에 추가 기능을 제공하는 loadable module입니다. Runtime instrumentation과 static analysis에 유용하며 callback, GIMPLE, IPA, RTL pass를 통해 compile 중인 code를 분석하거나 바꾸고 새 code를 추가할 수 있습니다.

GCC plugin 확장 지점
확장 지점용도
Callbackcompiler event에 반응
GIMPLE pass중간 표현 분석·변환
IPAinter-procedural analysis와 변환
RTL pass저수준 register transfer 단계 분석·변환

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되었습니다.

참고 자료
항목URL
GCC Pluginshttps://gcc.gnu.org/onlinedocs/gccint/Plugins.html
Plugin APIhttps://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API
GIMPLEhttps://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html
IPAhttps://gcc.gnu.org/onlinedocs/gccint/IPA.html
RTLhttps://gcc.gnu.org/onlinedocs/gccint/RTL.html
grsecurityhttps://grsecurity.net/
PaXhttps://pax.grsecurity.net/

원문 각주가 연결하는 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-60

GCC 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에서 제거합니다.

Plugin 기능의 수명주기
GCC·Clang에 없는 기능 제안Kernel GCC plugin으로 실험유용성 입증GCC와 Clang upstream 추진지원 GCC에서는 plugin build 차단모든 지원 GCC가 제공하면 kernel plugin 제거

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를 우선 사용해야 합니다.

GCC plugin source 구성
파일역할
`scripts/gcc-plugins/`plugin source directory
`gcc-common.h`GCC version 간 compatibility layer
`gcc-generate-gimple-pass.h`GIMPLE pass 등록 구조체 생성
`gcc-generate-simple_ipa-pass.h`SIMPLE_IPA pass 등록 구조체 생성
`gcc-generate-ipa-pass.h`IPA pass 등록 구조체 생성
`gcc-generate-rtl-pass.h`RTL pass 등록 구조체 생성

공통 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`이면 됩니다.

Distribution별 dependency
환경명령
Ubuntu, gcc-10`apt-get install gcc-10-plugin-dev`
Fedora native GCC`dnf install gcc-plugin-devel libmpc-devel`
Fedora plugin 포함 cross-compiler`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가 나오면 탐지된 것입니다.

Plugin directory 탐지 결과
출력판정
`plugin`plugin directory를 찾지 못함
전체 filesystem pathplugin directory 탐지 성공

`-print-file-name=plugin` 출력 해석입니다.

Cross-compiler 점검
`CROSS_COMPILE=arm-linux-gnu-` 설정`${CROSS_COMPILE}gcc -print-file-name=plugin` 실행출력이 `plugin`인지 full path인지 확인Full path면 kernel plugin build 진행

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-133

GCC plugin은 `scripts/gcc-plugins/`에 있습니다. 새 plugin source file도 이 directory 바로 아래에 두어야 하며 subdirectory는 지원하지 않습니다.

새 plugin은 `scripts/gcc-plugins/Makefile`, `scripts/Makefile.gcc-plugins`, 관련 Kconfig file에 각각 추가해야 합니다.

새 plugin 연결 지점
위치필수 작업
`scripts/gcc-plugins/`source file을 바로 아래에 배치
`scripts/gcc-plugins/Makefile`plugin build rule 추가
`scripts/Makefile.gcc-plugins`kernel build 연결 추가
관련 Kconfigconfiguration option 추가

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.