ldemul.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* ldemul.c -- clearing house for ld emulation states
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "getopt.h"
  19. #include "bfdlink.h"
  20. #include "ctf-api.h"
  21. #include "ld.h"
  22. #include "ldmisc.h"
  23. #include "ldexp.h"
  24. #include "ldlang.h"
  25. #include "ldfile.h"
  26. #include "ldemul.h"
  27. #include "ldmain.h"
  28. #include "ldemul-list.h"
  29. static ld_emulation_xfer_type *ld_emulation;
  30. void
  31. ldemul_hll (char *name)
  32. {
  33. ld_emulation->hll (name);
  34. }
  35. void
  36. ldemul_syslib (char *name)
  37. {
  38. ld_emulation->syslib (name);
  39. }
  40. void
  41. ldemul_after_parse (void)
  42. {
  43. ld_emulation->after_parse ();
  44. }
  45. void
  46. ldemul_before_parse (void)
  47. {
  48. ld_emulation->before_parse ();
  49. }
  50. void
  51. ldemul_before_plugin_all_symbols_read (void)
  52. {
  53. if (ld_emulation->before_plugin_all_symbols_read)
  54. ld_emulation->before_plugin_all_symbols_read ();
  55. }
  56. void
  57. ldemul_after_open (void)
  58. {
  59. ld_emulation->after_open ();
  60. }
  61. void
  62. ldemul_after_check_relocs (void)
  63. {
  64. ld_emulation->after_check_relocs ();
  65. }
  66. void
  67. ldemul_before_place_orphans (void)
  68. {
  69. ld_emulation->before_place_orphans ();
  70. }
  71. void
  72. ldemul_after_allocation (void)
  73. {
  74. ld_emulation->after_allocation ();
  75. }
  76. void
  77. ldemul_before_allocation (void)
  78. {
  79. ld_emulation->before_allocation ();
  80. }
  81. void
  82. ldemul_set_output_arch (void)
  83. {
  84. ld_emulation->set_output_arch ();
  85. }
  86. void
  87. ldemul_finish (void)
  88. {
  89. ld_emulation->finish ();
  90. }
  91. void
  92. ldemul_set_symbols (void)
  93. {
  94. if (ld_emulation->set_symbols)
  95. ld_emulation->set_symbols ();
  96. }
  97. void
  98. ldemul_create_output_section_statements (void)
  99. {
  100. if (ld_emulation->create_output_section_statements)
  101. ld_emulation->create_output_section_statements ();
  102. }
  103. char *
  104. ldemul_get_script (int *isfile)
  105. {
  106. return ld_emulation->get_script (isfile);
  107. }
  108. bool
  109. ldemul_open_dynamic_archive (const char *arch, search_dirs_type *search,
  110. lang_input_statement_type *entry)
  111. {
  112. if (ld_emulation->open_dynamic_archive)
  113. return (*ld_emulation->open_dynamic_archive) (arch, search, entry);
  114. return false;
  115. }
  116. lang_output_section_statement_type *
  117. ldemul_place_orphan (asection *s, const char *name, int constraint)
  118. {
  119. if (ld_emulation->place_orphan)
  120. return (*ld_emulation->place_orphan) (s, name, constraint);
  121. return NULL;
  122. }
  123. void
  124. ldemul_add_options (int ns, char **shortopts, int nl,
  125. struct option **longopts, int nrl,
  126. struct option **really_longopts)
  127. {
  128. if (ld_emulation->add_options)
  129. (*ld_emulation->add_options) (ns, shortopts, nl, longopts,
  130. nrl, really_longopts);
  131. }
  132. bool
  133. ldemul_handle_option (int optc)
  134. {
  135. if (ld_emulation->handle_option)
  136. return (*ld_emulation->handle_option) (optc);
  137. return false;
  138. }
  139. bool
  140. ldemul_parse_args (int argc, char **argv)
  141. {
  142. /* Try and use the emulation parser if there is one. */
  143. if (ld_emulation->parse_args)
  144. return (*ld_emulation->parse_args) (argc, argv);
  145. return false;
  146. }
  147. /* Let the emulation code handle an unrecognized file. */
  148. bool
  149. ldemul_unrecognized_file (lang_input_statement_type *entry)
  150. {
  151. if (ld_emulation->unrecognized_file)
  152. return (*ld_emulation->unrecognized_file) (entry);
  153. return false;
  154. }
  155. /* Let the emulation code handle a recognized file. */
  156. bool
  157. ldemul_recognized_file (lang_input_statement_type *entry)
  158. {
  159. if (ld_emulation->recognized_file)
  160. return (*ld_emulation->recognized_file) (entry);
  161. return false;
  162. }
  163. char *
  164. ldemul_choose_target (int argc, char **argv)
  165. {
  166. return ld_emulation->choose_target (argc, argv);
  167. }
  168. /* The default choose_target function. */
  169. char *
  170. ldemul_default_target (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
  171. {
  172. char *from_outside = getenv (TARGET_ENVIRON);
  173. if (from_outside != (char *) NULL)
  174. return from_outside;
  175. return ld_emulation->target_name;
  176. }
  177. /* If the entry point was not specified as an address, then add the
  178. symbol as undefined. This will cause ld to extract an archive
  179. element defining the entry if ld is linking against such an archive.
  180. We don't do this when generating shared libraries unless given -e
  181. on the command line, because most shared libs are not designed to
  182. be run as an executable. However, some are, eg. glibc ld.so and
  183. may rely on the default linker script supplying ENTRY. So we can't
  184. remove the ENTRY from the script, but would rather not insert
  185. undefined _start syms. */
  186. void
  187. after_parse_default (void)
  188. {
  189. if (entry_symbol.name != NULL
  190. && (bfd_link_executable (&link_info) || entry_from_cmdline))
  191. {
  192. bool is_vma = false;
  193. if (entry_from_cmdline)
  194. {
  195. const char *send;
  196. bfd_scan_vma (entry_symbol.name, &send, 0);
  197. is_vma = *send == '\0';
  198. }
  199. if (!is_vma)
  200. ldlang_add_undef (entry_symbol.name, entry_from_cmdline);
  201. }
  202. if (link_info.maxpagesize == 0)
  203. link_info.maxpagesize = bfd_emul_get_maxpagesize (default_target);
  204. if (link_info.commonpagesize == 0)
  205. link_info.commonpagesize = bfd_emul_get_commonpagesize (default_target);
  206. }
  207. void
  208. after_open_default (void)
  209. {
  210. link_info.big_endian = true;
  211. if (bfd_big_endian (link_info.output_bfd))
  212. ;
  213. else if (bfd_little_endian (link_info.output_bfd))
  214. link_info.big_endian = false;
  215. else
  216. {
  217. if (command_line.endian == ENDIAN_BIG)
  218. ;
  219. else if (command_line.endian == ENDIAN_LITTLE)
  220. link_info.big_endian = false;
  221. else if (command_line.endian == ENDIAN_UNSET)
  222. {
  223. LANG_FOR_EACH_INPUT_STATEMENT (s)
  224. if (s->the_bfd != NULL)
  225. {
  226. if (bfd_little_endian (s->the_bfd))
  227. link_info.big_endian = false;
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. void
  234. after_check_relocs_default (void)
  235. {
  236. }
  237. void
  238. before_place_orphans_default (void)
  239. {
  240. }
  241. void
  242. after_allocation_default (void)
  243. {
  244. lang_relax_sections (false);
  245. }
  246. void
  247. before_allocation_default (void)
  248. {
  249. if (!bfd_link_relocatable (&link_info))
  250. strip_excluded_output_sections ();
  251. }
  252. void
  253. finish_default (void)
  254. {
  255. if (!bfd_link_relocatable (&link_info))
  256. _bfd_fix_excluded_sec_syms (link_info.output_bfd, &link_info);
  257. }
  258. void
  259. set_output_arch_default (void)
  260. {
  261. /* Set the output architecture and machine if possible. */
  262. bfd_set_arch_mach (link_info.output_bfd,
  263. ldfile_output_architecture, ldfile_output_machine);
  264. }
  265. void
  266. syslib_default (char *ignore ATTRIBUTE_UNUSED)
  267. {
  268. info_msg (_("%pS SYSLIB ignored\n"), NULL);
  269. }
  270. void
  271. hll_default (char *ignore ATTRIBUTE_UNUSED)
  272. {
  273. info_msg (_("%pS HLL ignored\n"), NULL);
  274. }
  275. ld_emulation_xfer_type *ld_emulations[] = { EMULATION_LIST };
  276. void
  277. ldemul_choose_mode (char *target)
  278. {
  279. ld_emulation_xfer_type **eptr = ld_emulations;
  280. /* Ignore "gld" prefix. */
  281. if (target[0] == 'g' && target[1] == 'l' && target[2] == 'd')
  282. target += 3;
  283. for (; *eptr; eptr++)
  284. {
  285. if (strcmp (target, (*eptr)->emulation_name) == 0)
  286. {
  287. ld_emulation = *eptr;
  288. return;
  289. }
  290. }
  291. einfo (_("%P: unrecognised emulation mode: %s\n"), target);
  292. einfo (_("Supported emulations: "));
  293. ldemul_list_emulations (stderr);
  294. einfo ("%F\n");
  295. }
  296. void
  297. ldemul_list_emulations (FILE *f)
  298. {
  299. ld_emulation_xfer_type **eptr = ld_emulations;
  300. bool first = true;
  301. for (; *eptr; eptr++)
  302. {
  303. if (first)
  304. first = false;
  305. else
  306. fprintf (f, " ");
  307. fprintf (f, "%s", (*eptr)->emulation_name);
  308. }
  309. }
  310. void
  311. ldemul_list_emulation_options (FILE *f)
  312. {
  313. ld_emulation_xfer_type **eptr;
  314. int options_found = 0;
  315. for (eptr = ld_emulations; *eptr; eptr++)
  316. {
  317. ld_emulation_xfer_type *emul = *eptr;
  318. if (emul->list_options)
  319. {
  320. fprintf (f, "%s: \n", emul->emulation_name);
  321. emul->list_options (f);
  322. options_found = 1;
  323. }
  324. }
  325. if (!options_found)
  326. fprintf (f, _(" no emulation specific options.\n"));
  327. }
  328. int
  329. ldemul_find_potential_libraries (char *name, lang_input_statement_type *entry)
  330. {
  331. if (ld_emulation->find_potential_libraries)
  332. return ld_emulation->find_potential_libraries (name, entry);
  333. return 0;
  334. }
  335. struct bfd_elf_version_expr *
  336. ldemul_new_vers_pattern (struct bfd_elf_version_expr *entry)
  337. {
  338. if (ld_emulation->new_vers_pattern)
  339. entry = (*ld_emulation->new_vers_pattern) (entry);
  340. return entry;
  341. }
  342. void
  343. ldemul_extra_map_file_text (bfd *abfd, struct bfd_link_info *info, FILE *mapf)
  344. {
  345. if (ld_emulation->extra_map_file_text)
  346. ld_emulation->extra_map_file_text (abfd, info, mapf);
  347. }
  348. int
  349. ldemul_emit_ctf_early (void)
  350. {
  351. if (ld_emulation->emit_ctf_early)
  352. return ld_emulation->emit_ctf_early ();
  353. /* If the emulation doesn't know if it wants to emit CTF early, it is going
  354. to do so. */
  355. return 1;
  356. }
  357. void
  358. ldemul_acquire_strings_for_ctf (struct ctf_dict *ctf_output,
  359. struct elf_strtab_hash *symstrtab)
  360. {
  361. if (ld_emulation->acquire_strings_for_ctf)
  362. ld_emulation->acquire_strings_for_ctf (ctf_output, symstrtab);
  363. }
  364. void
  365. ldemul_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx,
  366. struct elf_internal_sym *sym)
  367. {
  368. if (ld_emulation->new_dynsym_for_ctf)
  369. ld_emulation->new_dynsym_for_ctf (ctf_output, symidx, sym);
  370. }
  371. bool
  372. ldemul_print_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
  373. {
  374. if (ld_emulation->print_symbol)
  375. return ld_emulation->print_symbol (hash_entry, ptr);
  376. return print_one_symbol (hash_entry, ptr);
  377. }