Add plugin functionality for link-time optimization (LTO).
[external/binutils.git] / include / plugin-api.h
1 /* plugin-api.h -- External linker plugin API.  */
2
3 /* Copyright 2008 Free Software Foundation, Inc.
4    Written by Cary Coutant <ccoutant@google.com>.
5
6    This file is part of binutils.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 /* This file defines the interface for writing a linker plugin, which is
24    described at < http://gcc.gnu.org/wiki/whopr/driver >.  */
25
26 #ifndef PLUGIN_API_H
27 #define PLUGIN_API_H
28
29 #include <stdint.h>
30 #include <sys/types.h>
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #endif
36
37 /* Status code returned by most API routines.  */
38
39 enum ld_plugin_status
40 {
41   LDPS_OK = 0,
42   LDPS_NO_SYMS,         // Attempt to get symbols that haven't been added.
43   LDPS_ERR,
44   /* Additional Error codes TBD.  */
45 };
46
47 /* The version of the API specification.  */
48
49 enum ld_plugin_api_version
50 {
51   LD_PLUGIN_API_VERSION = 1,
52 };
53
54 /* The type of output file being generated by the linker.  */
55
56 enum ld_plugin_output_file_type
57 {
58   LDPO_REL,
59   LDPO_EXEC,
60   LDPO_DYN,
61 };
62
63 /* An input file managed by the plugin library.  */
64
65 struct ld_plugin_input_file
66 {
67   const char *name;
68   int fd;
69   off_t offset;
70   off_t filesize;
71   void *handle;
72 };
73
74 /* A symbol belonging to an input file managed by the plugin library.  */
75
76 struct ld_plugin_symbol
77 {
78   char *name;
79   char *version;
80   int def;
81   int visibility;
82   uint64_t size;
83   char *comdat_key;
84   int resolution;
85 };
86
87 /* Whether the symbol is a definition, reference, or common, weak or not.  */
88
89 enum ld_plugin_symbol_kind
90 {
91   LDPK_DEF,
92   LDPK_WEAKDEF,
93   LDPK_UNDEF,
94   LDPK_WEAKUNDEF,
95   LDPK_COMMON,
96 };
97
98 /* The visibility of the symbol.  */
99
100 enum ld_plugin_symbol_visibility
101 {
102   LDPV_DEFAULT,
103   LDPV_PROTECTED,
104   LDPV_INTERNAL,
105   LDPV_HIDDEN,
106 };
107
108 /* How a symbol is resolved.  */
109
110 enum ld_plugin_symbol_resolution
111 {
112   LDPR_UNKNOWN = 0,
113   LDPR_UNDEF,
114   LDPR_PREVAILING_DEF,
115   LDPR_PREVAILING_DEF_IRONLY,
116   LDPR_PREEMPTED_REG,
117   LDPR_PREEMPTED_IR,
118   LDPR_RESOLVED_IR,
119   LDPR_RESOLVED_EXEC,
120   LDPR_RESOLVED_DYN,
121 };
122
123 /* The plugin library's "claim file" handler.  */
124
125 typedef
126 enum ld_plugin_status
127 (*ld_plugin_claim_file_handler) (
128   const struct ld_plugin_input_file *file, int *claimed);
129
130 /* The plugin library's "all symbols read" handler.  */
131
132 typedef
133 enum ld_plugin_status
134 (*ld_plugin_all_symbols_read_handler) (void);
135
136 /* The plugin library's cleanup handler.  */
137
138 typedef
139 enum ld_plugin_status
140 (*ld_plugin_cleanup_handler) (void);
141
142 /* The linker's interface for registering the "claim file" handler.  */
143
144 typedef
145 enum ld_plugin_status
146 (*ld_plugin_register_claim_file) (ld_plugin_claim_file_handler handler);
147
148 /* The linker's interface for registering the "all symbols read" handler.  */
149
150 typedef
151 enum ld_plugin_status
152 (*ld_plugin_register_all_symbols_read) (
153   ld_plugin_all_symbols_read_handler handler);
154
155 /* The linker's interface for registering the cleanup handler.  */
156
157 typedef
158 enum ld_plugin_status
159 (*ld_plugin_register_cleanup) (ld_plugin_cleanup_handler handler);
160
161 /* The linker's interface for adding symbols from a claimed input file.  */
162
163 typedef
164 enum ld_plugin_status
165 (*ld_plugin_add_symbols) (void *handle, int nsyms,
166                           const struct ld_plugin_symbol *syms);
167
168 /* The linker's interface for retrieving symbol resolution information.  */
169
170 typedef
171 enum ld_plugin_status
172 (*ld_plugin_get_symbols) (const void *handle, int nsyms,
173                           struct ld_plugin_symbol *syms);
174
175 /* The linker's interface for adding a compiled input file.  */
176
177 typedef
178 enum ld_plugin_status
179 (*ld_plugin_add_input_file) (char *pathname);
180
181 /* The linker's interface for issuing a warning or error message.  */
182
183 typedef
184 enum ld_plugin_status
185 (*ld_plugin_message) (int level, char *format, ...);
186
187 enum ld_plugin_level
188 {
189   LDPL_INFO,
190   LDPL_WARNING,
191   LDPL_ERROR,
192   LDPL_FATAL,
193 };
194
195 /* Values for the tv_tag field of the transfer vector.  */
196
197 enum ld_plugin_tag
198 {
199   LDPT_NULL = 0,
200   LDPT_API_VERSION,
201   LDPT_GOLD_VERSION,
202   LDPT_LINKER_OUTPUT,
203   LDPT_OPTION,
204   LDPT_REGISTER_CLAIM_FILE_HOOK,
205   LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK,
206   LDPT_REGISTER_CLEANUP_HOOK,
207   LDPT_ADD_SYMBOLS,
208   LDPT_GET_SYMBOLS,
209   LDPT_ADD_INPUT_FILE,
210   LDPT_MESSAGE,
211 };
212
213 /* The plugin transfer vector.  */
214
215 struct ld_plugin_tv
216 {
217   enum ld_plugin_tag tv_tag;
218   union
219   {
220     int tv_val;
221     const char *tv_string;
222     ld_plugin_register_claim_file tv_register_claim_file;
223     ld_plugin_register_all_symbols_read tv_register_all_symbols_read;
224     ld_plugin_register_cleanup tv_register_cleanup;
225     ld_plugin_add_symbols tv_add_symbols;
226     ld_plugin_get_symbols tv_get_symbols;
227     ld_plugin_add_input_file tv_add_input_file;
228     ld_plugin_message tv_message;
229   } tv_u;
230 };
231
232 /* The plugin library's "onload" entry point.  */
233
234 typedef
235 enum ld_plugin_status
236 (*ld_plugin_onload) (struct ld_plugin_tv *tv);
237
238 #ifdef __cplusplus
239 };
240 #endif
241
242 #endif /* !defined(PLUGIN_API_H) */