riscvelf.em 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. #
  4. # This file is part of the GNU Binutils.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. # MA 02110-1301, USA.
  20. fragment <<EOF
  21. #include "ldmain.h"
  22. #include "ldctor.h"
  23. #include "elf/riscv.h"
  24. #include "elfxx-riscv.h"
  25. static void
  26. riscv_elf_before_allocation (void)
  27. {
  28. gld${EMULATION_NAME}_before_allocation ();
  29. if (link_info.discard == discard_sec_merge)
  30. link_info.discard = discard_l;
  31. if (!bfd_link_relocatable (&link_info))
  32. {
  33. /* We always need at least some relaxation to handle code alignment. */
  34. if (RELAXATION_DISABLED_BY_USER)
  35. TARGET_ENABLE_RELAXATION;
  36. else
  37. ENABLE_RELAXATION;
  38. }
  39. link_info.relax_pass = 3;
  40. }
  41. static void
  42. gld${EMULATION_NAME}_after_allocation (void)
  43. {
  44. int need_layout = 0;
  45. /* Don't attempt to discard unused .eh_frame sections until the final link,
  46. as we can't reliably tell if they're used until after relaxation. */
  47. if (!bfd_link_relocatable (&link_info))
  48. {
  49. need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info);
  50. if (need_layout < 0)
  51. {
  52. einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
  53. return;
  54. }
  55. }
  56. /* PR 27566, if the phase of data segment is exp_seg_relro_adjust,
  57. that means we are still adjusting the relro, and shouldn't do the
  58. relaxations at this stage. Otherwise, we will get the symbol
  59. values beofore handling the relro, and may cause truncated fails
  60. when the relax range crossing the data segment. One of the solution
  61. is to monitor the data segment phase while relaxing, to know whether
  62. the relro has been handled or not.
  63. I think we probably need to record more information about data
  64. segment or alignments in the future, to make sure it is safe
  65. to doing relaxations. */
  66. enum phase_enum *phase = &(expld.dataseg.phase);
  67. bfd_elf${ELFSIZE}_riscv_set_data_segment_info (&link_info, (int *) phase);
  68. ldelf_map_segments (need_layout);
  69. }
  70. /* This is a convenient point to tell BFD about target specific flags.
  71. After the output has been created, but before inputs are read. */
  72. static void
  73. riscv_create_output_section_statements (void)
  74. {
  75. /* See PR 22920 for an example of why this is necessary. */
  76. if (strstr (bfd_get_target (link_info.output_bfd), "riscv") == NULL)
  77. {
  78. /* The RISC-V backend needs special fields in the output hash structure.
  79. These will only be created if the output format is a RISC-V format,
  80. hence we do not support linking and changing output formats at the
  81. same time. Use a link followed by objcopy to change output formats. */
  82. einfo (_("%F%P: error: cannot change output format"
  83. " whilst linking %s binaries\n"), "RISC-V");
  84. return;
  85. }
  86. }
  87. EOF
  88. LDEMUL_BEFORE_ALLOCATION=riscv_elf_before_allocation
  89. LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
  90. LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=riscv_create_output_section_statements