plugin-api.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* plugin-api.h -- External linker plugin API. */
  2. /* Copyright 2009, 2010 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 can not 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. enum ld_plugin_level
  267. {
  268. LDPL_INFO,
  269. LDPL_WARNING,
  270. LDPL_ERROR,
  271. LDPL_FATAL
  272. };
  273. /* Values for the tv_tag field of the transfer vector. */
  274. enum ld_plugin_tag
  275. {
  276. LDPT_NULL = 0,
  277. LDPT_API_VERSION = 1,
  278. LDPT_GOLD_VERSION = 2,
  279. LDPT_LINKER_OUTPUT = 3,
  280. LDPT_OPTION = 4,
  281. LDPT_REGISTER_CLAIM_FILE_HOOK = 5,
  282. LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK = 6,
  283. LDPT_REGISTER_CLEANUP_HOOK = 7,
  284. LDPT_ADD_SYMBOLS = 8,
  285. LDPT_GET_SYMBOLS = 9,
  286. LDPT_ADD_INPUT_FILE = 10,
  287. LDPT_MESSAGE = 11,
  288. LDPT_GET_INPUT_FILE = 12,
  289. LDPT_RELEASE_INPUT_FILE = 13,
  290. LDPT_ADD_INPUT_LIBRARY = 14,
  291. LDPT_OUTPUT_NAME = 15,
  292. LDPT_SET_EXTRA_LIBRARY_PATH = 16,
  293. LDPT_GNU_LD_VERSION = 17,
  294. LDPT_GET_VIEW = 18,
  295. LDPT_GET_INPUT_SECTION_COUNT = 19,
  296. LDPT_GET_INPUT_SECTION_TYPE = 20,
  297. LDPT_GET_INPUT_SECTION_NAME = 21,
  298. LDPT_GET_INPUT_SECTION_CONTENTS = 22,
  299. LDPT_UPDATE_SECTION_ORDER = 23,
  300. LDPT_ALLOW_SECTION_ORDERING = 24,
  301. LDPT_GET_SYMBOLS_V2 = 25,
  302. LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS = 26,
  303. LDPT_UNIQUE_SEGMENT_FOR_SECTIONS = 27
  304. };
  305. /* The plugin transfer vector. */
  306. struct ld_plugin_tv
  307. {
  308. enum ld_plugin_tag tv_tag;
  309. union
  310. {
  311. int tv_val;
  312. const char *tv_string;
  313. ld_plugin_register_claim_file tv_register_claim_file;
  314. ld_plugin_register_all_symbols_read tv_register_all_symbols_read;
  315. ld_plugin_register_cleanup tv_register_cleanup;
  316. ld_plugin_add_symbols tv_add_symbols;
  317. ld_plugin_get_symbols tv_get_symbols;
  318. ld_plugin_add_input_file tv_add_input_file;
  319. ld_plugin_message tv_message;
  320. ld_plugin_get_input_file tv_get_input_file;
  321. ld_plugin_get_view tv_get_view;
  322. ld_plugin_release_input_file tv_release_input_file;
  323. ld_plugin_add_input_library tv_add_input_library;
  324. ld_plugin_set_extra_library_path tv_set_extra_library_path;
  325. ld_plugin_get_input_section_count tv_get_input_section_count;
  326. ld_plugin_get_input_section_type tv_get_input_section_type;
  327. ld_plugin_get_input_section_name tv_get_input_section_name;
  328. ld_plugin_get_input_section_contents tv_get_input_section_contents;
  329. ld_plugin_update_section_order tv_update_section_order;
  330. ld_plugin_allow_section_ordering tv_allow_section_ordering;
  331. ld_plugin_allow_unique_segment_for_sections tv_allow_unique_segment_for_sections;
  332. ld_plugin_unique_segment_for_sections tv_unique_segment_for_sections;
  333. } tv_u;
  334. };
  335. /* The plugin library's "onload" entry point. */
  336. typedef
  337. enum ld_plugin_status
  338. (*ld_plugin_onload) (struct ld_plugin_tv *tv);
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif /* !defined(PLUGIN_API_H) */