plugin-api.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* plugin-api.h -- External linker plugin API. */
  2. /* Copyright (C) 2009-2019 Free Software Foundation, Inc.
  3. Written by Cary Coutant <ccoutant@google.com>.
  4. This file is part of binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* This file defines the interface for writing a linker plugin, which is
  18. described at < http://gcc.gnu.org/wiki/whopr/driver >. */
  19. #ifndef PLUGIN_API_H
  20. #define PLUGIN_API_H
  21. #ifdef HAVE_STDINT_H
  22. #include <stdint.h>
  23. #elif defined(HAVE_INTTYPES_H)
  24. #include <inttypes.h>
  25. #endif
  26. #include <sys/types.h>
  27. #if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && \
  28. !defined(UINT64_MAX) && !defined(uint64_t)
  29. #error cannot find uint64_t type
  30. #endif
  31. #ifdef __cplusplus
  32. extern "C"
  33. {
  34. #endif
  35. /* Status code returned by most API routines. */
  36. enum ld_plugin_status
  37. {
  38. LDPS_OK = 0,
  39. LDPS_NO_SYMS, /* Attempt to get symbols that haven't been added. */
  40. LDPS_BAD_HANDLE, /* No claimed object associated with given handle. */
  41. LDPS_ERR
  42. /* Additional Error codes TBD. */
  43. };
  44. /* The version of the API specification. */
  45. enum ld_plugin_api_version
  46. {
  47. LD_PLUGIN_API_VERSION = 1
  48. };
  49. /* The type of output file being generated by the linker. */
  50. enum ld_plugin_output_file_type
  51. {
  52. LDPO_REL,
  53. LDPO_EXEC,
  54. LDPO_DYN,
  55. LDPO_PIE
  56. };
  57. /* An input file managed by the plugin library. */
  58. struct ld_plugin_input_file
  59. {
  60. const char *name;
  61. int fd;
  62. off_t offset;
  63. off_t filesize;
  64. void *handle;
  65. };
  66. /* A symbol belonging to an input file managed by the plugin library. */
  67. struct ld_plugin_symbol
  68. {
  69. char *name;
  70. char *version;
  71. int def;
  72. int visibility;
  73. uint64_t size;
  74. char *comdat_key;
  75. int resolution;
  76. };
  77. /* An object's section. */
  78. struct ld_plugin_section
  79. {
  80. const void* handle;
  81. unsigned int shndx;
  82. };
  83. /* Whether the symbol is a definition, reference, or common, weak or not. */
  84. enum ld_plugin_symbol_kind
  85. {
  86. LDPK_DEF,
  87. LDPK_WEAKDEF,
  88. LDPK_UNDEF,
  89. LDPK_WEAKUNDEF,
  90. LDPK_COMMON
  91. };
  92. /* The visibility of the symbol. */
  93. enum ld_plugin_symbol_visibility
  94. {
  95. LDPV_DEFAULT,
  96. LDPV_PROTECTED,
  97. LDPV_INTERNAL,
  98. LDPV_HIDDEN
  99. };
  100. /* How a symbol is resolved. */
  101. enum ld_plugin_symbol_resolution
  102. {
  103. LDPR_UNKNOWN = 0,
  104. /* Symbol is still undefined at this point. */
  105. LDPR_UNDEF,
  106. /* This is the prevailing definition of the symbol, with references from
  107. regular object code. */
  108. LDPR_PREVAILING_DEF,
  109. /* This is the prevailing definition of the symbol, with no
  110. references from regular objects. It is only referenced from IR
  111. code. */
  112. LDPR_PREVAILING_DEF_IRONLY,
  113. /* This definition was pre-empted by a definition in a regular
  114. object file. */
  115. LDPR_PREEMPTED_REG,
  116. /* This definition was pre-empted by a definition in another IR file. */
  117. LDPR_PREEMPTED_IR,
  118. /* This symbol was resolved by a definition in another IR file. */
  119. LDPR_RESOLVED_IR,
  120. /* This symbol was resolved by a definition in a regular object
  121. linked into the main executable. */
  122. LDPR_RESOLVED_EXEC,
  123. /* This symbol was resolved by a definition in a shared object. */
  124. LDPR_RESOLVED_DYN,
  125. /* This is the prevailing definition of the symbol, with no
  126. references from regular objects. It is only referenced from IR
  127. code, but the symbol is exported and may be referenced from
  128. a dynamic object (not seen at link time). */
  129. LDPR_PREVAILING_DEF_IRONLY_EXP
  130. };
  131. /* The plugin library's "claim file" handler. */
  132. typedef
  133. enum ld_plugin_status
  134. (*ld_plugin_claim_file_handler) (
  135. const struct ld_plugin_input_file *file, int *claimed);
  136. /* The plugin library's "all symbols read" handler. */
  137. typedef
  138. enum ld_plugin_status
  139. (*ld_plugin_all_symbols_read_handler) (void);
  140. /* The plugin library's cleanup handler. */
  141. typedef
  142. enum ld_plugin_status
  143. (*ld_plugin_cleanup_handler) (void);
  144. /* The linker's interface for registering the "claim file" handler. */
  145. typedef
  146. enum ld_plugin_status
  147. (*ld_plugin_register_claim_file) (ld_plugin_claim_file_handler handler);
  148. /* The linker's interface for registering the "all symbols read" handler. */
  149. typedef
  150. enum ld_plugin_status
  151. (*ld_plugin_register_all_symbols_read) (
  152. ld_plugin_all_symbols_read_handler handler);
  153. /* The linker's interface for registering the cleanup handler. */
  154. typedef
  155. enum ld_plugin_status
  156. (*ld_plugin_register_cleanup) (ld_plugin_cleanup_handler handler);
  157. /* The linker's interface for adding symbols from a claimed input file. */
  158. typedef
  159. enum ld_plugin_status
  160. (*ld_plugin_add_symbols) (void *handle, int nsyms,
  161. const struct ld_plugin_symbol *syms);
  162. /* The linker's interface for getting the input file information with
  163. an open (possibly re-opened) file descriptor. */
  164. typedef
  165. enum ld_plugin_status
  166. (*ld_plugin_get_input_file) (const void *handle,
  167. struct ld_plugin_input_file *file);
  168. typedef
  169. enum ld_plugin_status
  170. (*ld_plugin_get_view) (const void *handle, const void **viewp);
  171. /* The linker's interface for releasing the input file. */
  172. typedef
  173. enum ld_plugin_status
  174. (*ld_plugin_release_input_file) (const void *handle);
  175. /* The linker's interface for retrieving symbol resolution information. */
  176. typedef
  177. enum ld_plugin_status
  178. (*ld_plugin_get_symbols) (const void *handle, int nsyms,
  179. struct ld_plugin_symbol *syms);
  180. /* The linker's interface for adding a compiled input file. */
  181. typedef
  182. enum ld_plugin_status
  183. (*ld_plugin_add_input_file) (const char *pathname);
  184. /* The linker's interface for adding a library that should be searched. */
  185. typedef
  186. enum ld_plugin_status
  187. (*ld_plugin_add_input_library) (const char *libname);
  188. /* The linker's interface for adding a library path that should be searched. */
  189. typedef
  190. enum ld_plugin_status
  191. (*ld_plugin_set_extra_library_path) (const char *path);
  192. /* The linker's interface for issuing a warning or error message. */
  193. typedef
  194. enum ld_plugin_status
  195. (*ld_plugin_message) (int level, const char *format, ...);
  196. /* The linker's interface for retrieving the number of sections in an object.
  197. The handle is obtained in the claim_file handler. This interface should
  198. only be invoked in the claim_file handler. This function sets *COUNT to
  199. the number of sections in the object. */
  200. typedef
  201. enum ld_plugin_status
  202. (*ld_plugin_get_input_section_count) (const void* handle, unsigned int *count);
  203. /* The linker's interface for retrieving the section type of a specific
  204. section in an object. This interface should only be invoked in the
  205. claim_file handler. This function sets *TYPE to an ELF SHT_xxx value. */
  206. typedef
  207. enum ld_plugin_status
  208. (*ld_plugin_get_input_section_type) (const struct ld_plugin_section section,
  209. unsigned int *type);
  210. /* The linker's interface for retrieving the name of a specific section in
  211. an object. This interface should only be invoked in the claim_file handler.
  212. This function sets *SECTION_NAME_PTR to a null-terminated buffer allocated
  213. by malloc. The plugin must free *SECTION_NAME_PTR. */
  214. typedef
  215. enum ld_plugin_status
  216. (*ld_plugin_get_input_section_name) (const struct ld_plugin_section section,
  217. char **section_name_ptr);
  218. /* The linker's interface for retrieving the contents of a specific section
  219. in an object. This interface should only be invoked in the claim_file
  220. handler. This function sets *SECTION_CONTENTS to point to a buffer that is
  221. valid until clam_file handler returns. It sets *LEN to the size of the
  222. buffer. */
  223. typedef
  224. enum ld_plugin_status
  225. (*ld_plugin_get_input_section_contents) (const struct ld_plugin_section section,
  226. const unsigned char **section_contents,
  227. size_t* len);
  228. /* The linker's interface for specifying the desired order of sections.
  229. The sections should be specifed using the array SECTION_LIST in the
  230. order in which they should appear in the final layout. NUM_SECTIONS
  231. specifies the number of entries in each array. This should be invoked
  232. in the all_symbols_read handler. */
  233. typedef
  234. enum ld_plugin_status
  235. (*ld_plugin_update_section_order) (const struct ld_plugin_section *section_list,
  236. unsigned int num_sections);
  237. /* The linker's interface for specifying that reordering of sections is
  238. desired so that the linker can prepare for it. This should be invoked
  239. before update_section_order, preferably in the claim_file handler. */
  240. typedef
  241. enum ld_plugin_status
  242. (*ld_plugin_allow_section_ordering) (void);
  243. /* The linker's interface for specifying that a subset of sections is
  244. to be mapped to a unique segment. If the plugin wants to call
  245. unique_segment_for_sections, it must call this function from a
  246. claim_file_handler or when it is first loaded. */
  247. typedef
  248. enum ld_plugin_status
  249. (*ld_plugin_allow_unique_segment_for_sections) (void);
  250. /* The linker's interface for specifying that a specific set of sections
  251. must be mapped to a unique segment. ELF segments do not have names
  252. and the NAME is used as the name of the newly created output section
  253. that is then placed in the unique PT_LOAD segment. FLAGS is used to
  254. specify if any additional segment flags need to be set. For instance,
  255. a specific segment flag can be set to identify this segment. Unsetting
  256. segment flags that would be set by default is not possible. The
  257. parameter SEGMENT_ALIGNMENT when non-zero will override the default. */
  258. typedef
  259. enum ld_plugin_status
  260. (*ld_plugin_unique_segment_for_sections) (
  261. const char* segment_name,
  262. uint64_t segment_flags,
  263. uint64_t segment_alignment,
  264. const struct ld_plugin_section * section_list,
  265. unsigned int num_sections);
  266. /* The linker's interface for retrieving the section alignment requirement
  267. of a specific section in an object. This interface should only be invoked in the
  268. claim_file handler. This function sets *ADDRALIGN to the ELF sh_addralign
  269. value of the input section. */
  270. typedef
  271. enum ld_plugin_status
  272. (*ld_plugin_get_input_section_alignment) (const struct ld_plugin_section section,
  273. unsigned int *addralign);
  274. /* The linker's interface for retrieving the section size of a specific section
  275. in an object. This interface should only be invoked in the claim_file handler.
  276. This function sets *SECSIZE to the ELF sh_size
  277. value of the input section. */
  278. typedef
  279. enum ld_plugin_status
  280. (*ld_plugin_get_input_section_size) (const struct ld_plugin_section section,
  281. uint64_t *secsize);
  282. typedef
  283. enum ld_plugin_status
  284. (*ld_plugin_new_input_handler) (const struct ld_plugin_input_file *file);
  285. /* The linker's interface for registering the "new_input" handler. This handler
  286. will be notified when a new input file has been added after the
  287. all_symbols_read event, allowing the plugin to, for example, set a unique
  288. segment for sections in plugin-generated input files. */
  289. typedef
  290. enum ld_plugin_status
  291. (*ld_plugin_register_new_input) (ld_plugin_new_input_handler handler);
  292. /* The linker's interface for getting the list of wrapped symbols using the
  293. --wrap option. This sets *NUM_SYMBOLS to number of wrapped symbols and
  294. *WRAP_SYMBOL_LIST to the list of wrapped symbols. */
  295. typedef
  296. enum ld_plugin_status
  297. (*ld_plugin_get_wrap_symbols) (uint64_t *num_symbols,
  298. const char ***wrap_symbol_list);
  299. enum ld_plugin_level
  300. {
  301. LDPL_INFO,
  302. LDPL_WARNING,
  303. LDPL_ERROR,
  304. LDPL_FATAL
  305. };
  306. /* Values for the tv_tag field of the transfer vector. */
  307. enum ld_plugin_tag
  308. {
  309. LDPT_NULL = 0,
  310. LDPT_API_VERSION = 1,
  311. LDPT_GOLD_VERSION = 2,
  312. LDPT_LINKER_OUTPUT = 3,
  313. LDPT_OPTION = 4,
  314. LDPT_REGISTER_CLAIM_FILE_HOOK = 5,
  315. LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK = 6,
  316. LDPT_REGISTER_CLEANUP_HOOK = 7,
  317. LDPT_ADD_SYMBOLS = 8,
  318. LDPT_GET_SYMBOLS = 9,
  319. LDPT_ADD_INPUT_FILE = 10,
  320. LDPT_MESSAGE = 11,
  321. LDPT_GET_INPUT_FILE = 12,
  322. LDPT_RELEASE_INPUT_FILE = 13,
  323. LDPT_ADD_INPUT_LIBRARY = 14,
  324. LDPT_OUTPUT_NAME = 15,
  325. LDPT_SET_EXTRA_LIBRARY_PATH = 16,
  326. LDPT_GNU_LD_VERSION = 17,
  327. LDPT_GET_VIEW = 18,
  328. LDPT_GET_INPUT_SECTION_COUNT = 19,
  329. LDPT_GET_INPUT_SECTION_TYPE = 20,
  330. LDPT_GET_INPUT_SECTION_NAME = 21,
  331. LDPT_GET_INPUT_SECTION_CONTENTS = 22,
  332. LDPT_UPDATE_SECTION_ORDER = 23,
  333. LDPT_ALLOW_SECTION_ORDERING = 24,
  334. LDPT_GET_SYMBOLS_V2 = 25,
  335. LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS = 26,
  336. LDPT_UNIQUE_SEGMENT_FOR_SECTIONS = 27,
  337. LDPT_GET_SYMBOLS_V3 = 28,
  338. LDPT_GET_INPUT_SECTION_ALIGNMENT = 29,
  339. LDPT_GET_INPUT_SECTION_SIZE = 30,
  340. LDPT_REGISTER_NEW_INPUT_HOOK = 31,
  341. LDPT_GET_WRAP_SYMBOLS = 32
  342. };
  343. /* The plugin transfer vector. */
  344. struct ld_plugin_tv
  345. {
  346. enum ld_plugin_tag tv_tag;
  347. union
  348. {
  349. int tv_val;
  350. const char *tv_string;
  351. ld_plugin_register_claim_file tv_register_claim_file;
  352. ld_plugin_register_all_symbols_read tv_register_all_symbols_read;
  353. ld_plugin_register_cleanup tv_register_cleanup;
  354. ld_plugin_add_symbols tv_add_symbols;
  355. ld_plugin_get_symbols tv_get_symbols;
  356. ld_plugin_add_input_file tv_add_input_file;
  357. ld_plugin_message tv_message;
  358. ld_plugin_get_input_file tv_get_input_file;
  359. ld_plugin_get_view tv_get_view;
  360. ld_plugin_release_input_file tv_release_input_file;
  361. ld_plugin_add_input_library tv_add_input_library;
  362. ld_plugin_set_extra_library_path tv_set_extra_library_path;
  363. ld_plugin_get_input_section_count tv_get_input_section_count;
  364. ld_plugin_get_input_section_type tv_get_input_section_type;
  365. ld_plugin_get_input_section_name tv_get_input_section_name;
  366. ld_plugin_get_input_section_contents tv_get_input_section_contents;
  367. ld_plugin_update_section_order tv_update_section_order;
  368. ld_plugin_allow_section_ordering tv_allow_section_ordering;
  369. ld_plugin_allow_unique_segment_for_sections tv_allow_unique_segment_for_sections;
  370. ld_plugin_unique_segment_for_sections tv_unique_segment_for_sections;
  371. ld_plugin_get_input_section_alignment tv_get_input_section_alignment;
  372. ld_plugin_get_input_section_size tv_get_input_section_size;
  373. ld_plugin_register_new_input tv_register_new_input;
  374. ld_plugin_get_wrap_symbols tv_get_wrap_symbols;
  375. } tv_u;
  376. };
  377. /* The plugin library's "onload" entry point. */
  378. typedef
  379. enum ld_plugin_status
  380. (*ld_plugin_onload) (struct ld_plugin_tv *tv);
  381. #ifdef __cplusplus
  382. }
  383. #endif
  384. #endif /* !defined(PLUGIN_API_H) */