plugin-api.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* plugin-api.h -- External linker plugin API. */
  2. /* Copyright (C) 2009-2022 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. /* Detect endianess based on __BYTE_ORDER__ macro. */
  32. #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
  33. defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_PDP_ENDIAN__)
  34. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  35. #define PLUGIN_LITTLE_ENDIAN 1
  36. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  37. #define PLUGIN_BIG_ENDIAN 1
  38. #elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
  39. #define PLUGIN_PDP_ENDIAN 1
  40. #endif
  41. #else
  42. /* Older GCC releases (<4.6.0) can make detection from glibc macros. */
  43. #if defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID__)
  44. #include <endian.h>
  45. #ifdef __BYTE_ORDER
  46. #if __BYTE_ORDER == __LITTLE_ENDIAN
  47. #define PLUGIN_LITTLE_ENDIAN 1
  48. #elif __BYTE_ORDER == __BIG_ENDIAN
  49. #define PLUGIN_BIG_ENDIAN 1
  50. #endif
  51. #endif
  52. #endif
  53. /* Include all necessary header files based on target. */
  54. #if defined(__SVR4) && defined(__sun)
  55. #include <sys/byteorder.h>
  56. #endif
  57. #if defined(__FreeBSD__) || defined(__NetBSD__) || \
  58. defined(__DragonFly__) || defined(__minix)
  59. #include <sys/endian.h>
  60. #endif
  61. #if defined(__OpenBSD__)
  62. #include <machine/endian.h>
  63. #endif
  64. /* Detect endianess based on _BYTE_ORDER. */
  65. #ifdef _BYTE_ORDER
  66. #if _BYTE_ORDER == _LITTLE_ENDIAN
  67. #define PLUGIN_LITTLE_ENDIAN 1
  68. #elif _BYTE_ORDER == _BIG_ENDIAN
  69. #define PLUGIN_BIG_ENDIAN 1
  70. #endif
  71. #endif
  72. /* Detect based on _WIN32. */
  73. #if defined(_WIN32)
  74. #define PLUGIN_LITTLE_ENDIAN 1
  75. #endif
  76. /* Detect based on __BIG_ENDIAN__ and __LITTLE_ENDIAN__ */
  77. #ifdef __LITTLE_ENDIAN__
  78. #define PLUGIN_LITTLE_ENDIAN 1
  79. #endif
  80. #ifdef __BIG_ENDIAN__
  81. #define PLUGIN_BIG_ENDIAN 1
  82. #endif
  83. #endif
  84. #ifdef __cplusplus
  85. extern "C"
  86. {
  87. #endif
  88. /* Status code returned by most API routines. */
  89. enum ld_plugin_status
  90. {
  91. LDPS_OK = 0,
  92. LDPS_NO_SYMS, /* Attempt to get symbols that haven't been added. */
  93. LDPS_BAD_HANDLE, /* No claimed object associated with given handle. */
  94. LDPS_ERR
  95. /* Additional Error codes TBD. */
  96. };
  97. /* The version of the API specification. */
  98. enum ld_plugin_api_version
  99. {
  100. LD_PLUGIN_API_VERSION = 1
  101. };
  102. /* The type of output file being generated by the linker. */
  103. enum ld_plugin_output_file_type
  104. {
  105. LDPO_REL,
  106. LDPO_EXEC,
  107. LDPO_DYN,
  108. LDPO_PIE
  109. };
  110. /* An input file managed by the plugin library. */
  111. struct ld_plugin_input_file
  112. {
  113. const char *name;
  114. int fd;
  115. off_t offset;
  116. off_t filesize;
  117. void *handle;
  118. };
  119. /* A symbol belonging to an input file managed by the plugin library. */
  120. struct ld_plugin_symbol
  121. {
  122. char *name;
  123. char *version;
  124. /* This is for compatibility with older ABIs. The older ABI defined
  125. only 'def' field. */
  126. #if PLUGIN_BIG_ENDIAN == 1
  127. char unused;
  128. char section_kind;
  129. char symbol_type;
  130. char def;
  131. #elif PLUGIN_LITTLE_ENDIAN == 1
  132. char def;
  133. char symbol_type;
  134. char section_kind;
  135. char unused;
  136. #elif PLUGIN_PDP_ENDIAN == 1
  137. char symbol_type;
  138. char def;
  139. char unused;
  140. char section_kind;
  141. #else
  142. #error "Could not detect architecture endianess"
  143. #endif
  144. int visibility;
  145. uint64_t size;
  146. char *comdat_key;
  147. int resolution;
  148. };
  149. /* An object's section. */
  150. struct ld_plugin_section
  151. {
  152. const void* handle;
  153. unsigned int shndx;
  154. };
  155. /* Whether the symbol is a definition, reference, or common, weak or not. */
  156. enum ld_plugin_symbol_kind
  157. {
  158. LDPK_DEF,
  159. LDPK_WEAKDEF,
  160. LDPK_UNDEF,
  161. LDPK_WEAKUNDEF,
  162. LDPK_COMMON
  163. };
  164. /* The visibility of the symbol. */
  165. enum ld_plugin_symbol_visibility
  166. {
  167. LDPV_DEFAULT,
  168. LDPV_PROTECTED,
  169. LDPV_INTERNAL,
  170. LDPV_HIDDEN
  171. };
  172. /* The type of the symbol. */
  173. enum ld_plugin_symbol_type
  174. {
  175. LDST_UNKNOWN,
  176. LDST_FUNCTION,
  177. LDST_VARIABLE
  178. };
  179. enum ld_plugin_symbol_section_kind
  180. {
  181. LDSSK_DEFAULT,
  182. LDSSK_BSS
  183. };
  184. /* How a symbol is resolved. */
  185. enum ld_plugin_symbol_resolution
  186. {
  187. LDPR_UNKNOWN = 0,
  188. /* Symbol is still undefined at this point. */
  189. LDPR_UNDEF,
  190. /* This is the prevailing definition of the symbol, with references from
  191. regular object code. */
  192. LDPR_PREVAILING_DEF,
  193. /* This is the prevailing definition of the symbol, with no
  194. references from regular objects. It is only referenced from IR
  195. code. */
  196. LDPR_PREVAILING_DEF_IRONLY,
  197. /* This definition was pre-empted by a definition in a regular
  198. object file. */
  199. LDPR_PREEMPTED_REG,
  200. /* This definition was pre-empted by a definition in another IR file. */
  201. LDPR_PREEMPTED_IR,
  202. /* This symbol was resolved by a definition in another IR file. */
  203. LDPR_RESOLVED_IR,
  204. /* This symbol was resolved by a definition in a regular object
  205. linked into the main executable. */
  206. LDPR_RESOLVED_EXEC,
  207. /* This symbol was resolved by a definition in a shared object. */
  208. LDPR_RESOLVED_DYN,
  209. /* This is the prevailing definition of the symbol, with no
  210. references from regular objects. It is only referenced from IR
  211. code, but the symbol is exported and may be referenced from
  212. a dynamic object (not seen at link time). */
  213. LDPR_PREVAILING_DEF_IRONLY_EXP
  214. };
  215. /* The plugin library's "claim file" handler. */
  216. typedef
  217. enum ld_plugin_status
  218. (*ld_plugin_claim_file_handler) (
  219. const struct ld_plugin_input_file *file, int *claimed);
  220. /* The plugin library's "all symbols read" handler. */
  221. typedef
  222. enum ld_plugin_status
  223. (*ld_plugin_all_symbols_read_handler) (void);
  224. /* The plugin library's cleanup handler. */
  225. typedef
  226. enum ld_plugin_status
  227. (*ld_plugin_cleanup_handler) (void);
  228. /* The linker's interface for registering the "claim file" handler. */
  229. typedef
  230. enum ld_plugin_status
  231. (*ld_plugin_register_claim_file) (ld_plugin_claim_file_handler handler);
  232. /* The linker's interface for registering the "all symbols read" handler. */
  233. typedef
  234. enum ld_plugin_status
  235. (*ld_plugin_register_all_symbols_read) (
  236. ld_plugin_all_symbols_read_handler handler);
  237. /* The linker's interface for registering the cleanup handler. */
  238. typedef
  239. enum ld_plugin_status
  240. (*ld_plugin_register_cleanup) (ld_plugin_cleanup_handler handler);
  241. /* The linker's interface for adding symbols from a claimed input file. */
  242. typedef
  243. enum ld_plugin_status
  244. (*ld_plugin_add_symbols) (void *handle, int nsyms,
  245. const struct ld_plugin_symbol *syms);
  246. /* The linker's interface for getting the input file information with
  247. an open (possibly re-opened) file descriptor. */
  248. typedef
  249. enum ld_plugin_status
  250. (*ld_plugin_get_input_file) (const void *handle,
  251. struct ld_plugin_input_file *file);
  252. typedef
  253. enum ld_plugin_status
  254. (*ld_plugin_get_view) (const void *handle, const void **viewp);
  255. /* The linker's interface for releasing the input file. */
  256. typedef
  257. enum ld_plugin_status
  258. (*ld_plugin_release_input_file) (const void *handle);
  259. /* The linker's interface for retrieving symbol resolution information. */
  260. typedef
  261. enum ld_plugin_status
  262. (*ld_plugin_get_symbols) (const void *handle, int nsyms,
  263. struct ld_plugin_symbol *syms);
  264. /* The linker's interface for adding a compiled input file. */
  265. typedef
  266. enum ld_plugin_status
  267. (*ld_plugin_add_input_file) (const char *pathname);
  268. /* The linker's interface for adding a library that should be searched. */
  269. typedef
  270. enum ld_plugin_status
  271. (*ld_plugin_add_input_library) (const char *libname);
  272. /* The linker's interface for adding a library path that should be searched. */
  273. typedef
  274. enum ld_plugin_status
  275. (*ld_plugin_set_extra_library_path) (const char *path);
  276. /* The linker's interface for issuing a warning or error message. */
  277. typedef
  278. enum ld_plugin_status
  279. (*ld_plugin_message) (int level, const char *format, ...);
  280. /* The linker's interface for retrieving the number of sections in an object.
  281. The handle is obtained in the claim_file handler. This interface should
  282. only be invoked in the claim_file handler. This function sets *COUNT to
  283. the number of sections in the object. */
  284. typedef
  285. enum ld_plugin_status
  286. (*ld_plugin_get_input_section_count) (const void* handle, unsigned int *count);
  287. /* The linker's interface for retrieving the section type of a specific
  288. section in an object. This interface should only be invoked in the
  289. claim_file handler. This function sets *TYPE to an ELF SHT_xxx value. */
  290. typedef
  291. enum ld_plugin_status
  292. (*ld_plugin_get_input_section_type) (const struct ld_plugin_section section,
  293. unsigned int *type);
  294. /* The linker's interface for retrieving the name of a specific section in
  295. an object. This interface should only be invoked in the claim_file handler.
  296. This function sets *SECTION_NAME_PTR to a null-terminated buffer allocated
  297. by malloc. The plugin must free *SECTION_NAME_PTR. */
  298. typedef
  299. enum ld_plugin_status
  300. (*ld_plugin_get_input_section_name) (const struct ld_plugin_section section,
  301. char **section_name_ptr);
  302. /* The linker's interface for retrieving the contents of a specific section
  303. in an object. This interface should only be invoked in the claim_file
  304. handler. This function sets *SECTION_CONTENTS to point to a buffer that is
  305. valid until clam_file handler returns. It sets *LEN to the size of the
  306. buffer. */
  307. typedef
  308. enum ld_plugin_status
  309. (*ld_plugin_get_input_section_contents) (const struct ld_plugin_section section,
  310. const unsigned char **section_contents,
  311. size_t* len);
  312. /* The linker's interface for specifying the desired order of sections.
  313. The sections should be specifed using the array SECTION_LIST in the
  314. order in which they should appear in the final layout. NUM_SECTIONS
  315. specifies the number of entries in each array. This should be invoked
  316. in the all_symbols_read handler. */
  317. typedef
  318. enum ld_plugin_status
  319. (*ld_plugin_update_section_order) (const struct ld_plugin_section *section_list,
  320. unsigned int num_sections);
  321. /* The linker's interface for specifying that reordering of sections is
  322. desired so that the linker can prepare for it. This should be invoked
  323. before update_section_order, preferably in the claim_file handler. */
  324. typedef
  325. enum ld_plugin_status
  326. (*ld_plugin_allow_section_ordering) (void);
  327. /* The linker's interface for specifying that a subset of sections is
  328. to be mapped to a unique segment. If the plugin wants to call
  329. unique_segment_for_sections, it must call this function from a
  330. claim_file_handler or when it is first loaded. */
  331. typedef
  332. enum ld_plugin_status
  333. (*ld_plugin_allow_unique_segment_for_sections) (void);
  334. /* The linker's interface for specifying that a specific set of sections
  335. must be mapped to a unique segment. ELF segments do not have names
  336. and the NAME is used as the name of the newly created output section
  337. that is then placed in the unique PT_LOAD segment. FLAGS is used to
  338. specify if any additional segment flags need to be set. For instance,
  339. a specific segment flag can be set to identify this segment. Unsetting
  340. segment flags that would be set by default is not possible. The
  341. parameter SEGMENT_ALIGNMENT when non-zero will override the default. */
  342. typedef
  343. enum ld_plugin_status
  344. (*ld_plugin_unique_segment_for_sections) (
  345. const char* segment_name,
  346. uint64_t segment_flags,
  347. uint64_t segment_alignment,
  348. const struct ld_plugin_section * section_list,
  349. unsigned int num_sections);
  350. /* The linker's interface for retrieving the section alignment requirement
  351. of a specific section in an object. This interface should only be invoked in the
  352. claim_file handler. This function sets *ADDRALIGN to the ELF sh_addralign
  353. value of the input section. */
  354. typedef
  355. enum ld_plugin_status
  356. (*ld_plugin_get_input_section_alignment) (const struct ld_plugin_section section,
  357. unsigned int *addralign);
  358. /* The linker's interface for retrieving the section size of a specific section
  359. in an object. This interface should only be invoked in the claim_file handler.
  360. This function sets *SECSIZE to the ELF sh_size
  361. value of the input section. */
  362. typedef
  363. enum ld_plugin_status
  364. (*ld_plugin_get_input_section_size) (const struct ld_plugin_section section,
  365. uint64_t *secsize);
  366. typedef
  367. enum ld_plugin_status
  368. (*ld_plugin_new_input_handler) (const struct ld_plugin_input_file *file);
  369. /* The linker's interface for registering the "new_input" handler. This handler
  370. will be notified when a new input file has been added after the
  371. all_symbols_read event, allowing the plugin to, for example, set a unique
  372. segment for sections in plugin-generated input files. */
  373. typedef
  374. enum ld_plugin_status
  375. (*ld_plugin_register_new_input) (ld_plugin_new_input_handler handler);
  376. /* The linker's interface for getting the list of wrapped symbols using the
  377. --wrap option. This sets *NUM_SYMBOLS to number of wrapped symbols and
  378. *WRAP_SYMBOL_LIST to the list of wrapped symbols. */
  379. typedef
  380. enum ld_plugin_status
  381. (*ld_plugin_get_wrap_symbols) (uint64_t *num_symbols,
  382. const char ***wrap_symbol_list);
  383. enum ld_plugin_level
  384. {
  385. LDPL_INFO,
  386. LDPL_WARNING,
  387. LDPL_ERROR,
  388. LDPL_FATAL
  389. };
  390. /* Values for the tv_tag field of the transfer vector. */
  391. enum ld_plugin_tag
  392. {
  393. LDPT_NULL = 0,
  394. LDPT_API_VERSION = 1,
  395. LDPT_GOLD_VERSION = 2,
  396. LDPT_LINKER_OUTPUT = 3,
  397. LDPT_OPTION = 4,
  398. LDPT_REGISTER_CLAIM_FILE_HOOK = 5,
  399. LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK = 6,
  400. LDPT_REGISTER_CLEANUP_HOOK = 7,
  401. LDPT_ADD_SYMBOLS = 8,
  402. LDPT_GET_SYMBOLS = 9,
  403. LDPT_ADD_INPUT_FILE = 10,
  404. LDPT_MESSAGE = 11,
  405. LDPT_GET_INPUT_FILE = 12,
  406. LDPT_RELEASE_INPUT_FILE = 13,
  407. LDPT_ADD_INPUT_LIBRARY = 14,
  408. LDPT_OUTPUT_NAME = 15,
  409. LDPT_SET_EXTRA_LIBRARY_PATH = 16,
  410. LDPT_GNU_LD_VERSION = 17,
  411. LDPT_GET_VIEW = 18,
  412. LDPT_GET_INPUT_SECTION_COUNT = 19,
  413. LDPT_GET_INPUT_SECTION_TYPE = 20,
  414. LDPT_GET_INPUT_SECTION_NAME = 21,
  415. LDPT_GET_INPUT_SECTION_CONTENTS = 22,
  416. LDPT_UPDATE_SECTION_ORDER = 23,
  417. LDPT_ALLOW_SECTION_ORDERING = 24,
  418. LDPT_GET_SYMBOLS_V2 = 25,
  419. LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS = 26,
  420. LDPT_UNIQUE_SEGMENT_FOR_SECTIONS = 27,
  421. LDPT_GET_SYMBOLS_V3 = 28,
  422. LDPT_GET_INPUT_SECTION_ALIGNMENT = 29,
  423. LDPT_GET_INPUT_SECTION_SIZE = 30,
  424. LDPT_REGISTER_NEW_INPUT_HOOK = 31,
  425. LDPT_GET_WRAP_SYMBOLS = 32,
  426. LDPT_ADD_SYMBOLS_V2 = 33
  427. };
  428. /* The plugin transfer vector. */
  429. struct ld_plugin_tv
  430. {
  431. enum ld_plugin_tag tv_tag;
  432. union
  433. {
  434. int tv_val;
  435. const char *tv_string;
  436. ld_plugin_register_claim_file tv_register_claim_file;
  437. ld_plugin_register_all_symbols_read tv_register_all_symbols_read;
  438. ld_plugin_register_cleanup tv_register_cleanup;
  439. ld_plugin_add_symbols tv_add_symbols;
  440. ld_plugin_get_symbols tv_get_symbols;
  441. ld_plugin_add_input_file tv_add_input_file;
  442. ld_plugin_message tv_message;
  443. ld_plugin_get_input_file tv_get_input_file;
  444. ld_plugin_get_view tv_get_view;
  445. ld_plugin_release_input_file tv_release_input_file;
  446. ld_plugin_add_input_library tv_add_input_library;
  447. ld_plugin_set_extra_library_path tv_set_extra_library_path;
  448. ld_plugin_get_input_section_count tv_get_input_section_count;
  449. ld_plugin_get_input_section_type tv_get_input_section_type;
  450. ld_plugin_get_input_section_name tv_get_input_section_name;
  451. ld_plugin_get_input_section_contents tv_get_input_section_contents;
  452. ld_plugin_update_section_order tv_update_section_order;
  453. ld_plugin_allow_section_ordering tv_allow_section_ordering;
  454. ld_plugin_allow_unique_segment_for_sections tv_allow_unique_segment_for_sections;
  455. ld_plugin_unique_segment_for_sections tv_unique_segment_for_sections;
  456. ld_plugin_get_input_section_alignment tv_get_input_section_alignment;
  457. ld_plugin_get_input_section_size tv_get_input_section_size;
  458. ld_plugin_register_new_input tv_register_new_input;
  459. ld_plugin_get_wrap_symbols tv_get_wrap_symbols;
  460. } tv_u;
  461. };
  462. /* The plugin library's "onload" entry point. */
  463. typedef
  464. enum ld_plugin_status
  465. (*ld_plugin_onload) (struct ld_plugin_tv *tv);
  466. #ifdef __cplusplus
  467. }
  468. #endif
  469. #endif /* !defined(PLUGIN_API_H) */