1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2004-2024 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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.
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.
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,
25 #include "elf/riscv.h"
26 #include "elfxx-riscv.h"
28 static struct riscv_elf_params params = { .relax_gp = 1,
32 # Define some shell vars to insert bits of code into the standard elf
33 # parse_args and list_options functions. */
34 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
35 { "relax-gp", no_argument, NULL, OPTION_RELAX_GP },
36 { "no-relax-gp", no_argument, NULL, OPTION_NO_RELAX_GP },
37 { "check-uleb128", no_argument, NULL, OPTION_CHECK_ULEB128 },
38 { "no-check-uleb128", no_argument, NULL, OPTION_NO_CHECK_ULEB128 },
41 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
42 fprintf (file, _(" --relax-gp Perform GP relaxation\n"));
43 fprintf (file, _(" --no-relax-gp Don'\''t perform GP relaxation\n"));
44 fprintf (file, _(" --check-uleb128 Check if SUB_ULEB128 has non-zero addend\n"));
45 fprintf (file, _(" --no-check-uleb128 Don'\''t check if SUB_ULEB128 has non-zero addend\n"));
48 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
53 case OPTION_NO_RELAX_GP:
57 case OPTION_CHECK_ULEB128:
58 params.check_uleb128 = 1;
61 case OPTION_NO_CHECK_ULEB128:
62 params.check_uleb128 = 0;
68 riscv_elf_before_allocation (void)
70 gld${EMULATION_NAME}_before_allocation ();
72 if (link_info.discard == discard_sec_merge)
73 link_info.discard = discard_l;
75 if (!bfd_link_relocatable (&link_info))
77 /* We always need at least some relaxation to handle code alignment. */
78 if (RELAXATION_DISABLED_BY_USER)
79 TARGET_ENABLE_RELAXATION;
84 link_info.relax_pass = 2;
88 gld${EMULATION_NAME}_after_allocation (void)
92 /* Don't attempt to discard unused .eh_frame sections until the final link,
93 as we can't reliably tell if they're used until after relaxation. */
94 if (!bfd_link_relocatable (&link_info))
96 need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info);
99 einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
104 /* PR 27566, if the phase of data segment is exp_seg_relro_adjust,
105 that means we are still adjusting the relro, and shouldn't do the
106 relaxations at this stage. Otherwise, we will get the symbol
107 values beofore handling the relro, and may cause truncated fails
108 when the relax range crossing the data segment. One of the solution
109 is to monitor the data segment phase while relaxing, to know whether
110 the relro has been handled or not.
112 I think we probably need to record more information about data
113 segment or alignments in the future, to make sure it is safe
114 to doing relaxations. */
115 enum phase_enum *phase = &(expld.dataseg.phase);
116 bfd_elf${ELFSIZE}_riscv_set_data_segment_info (&link_info, (int *) phase);
118 ldelf_map_segments (need_layout);
121 /* This is a convenient point to tell BFD about target specific flags.
122 After the output has been created, but before inputs are read. */
125 riscv_create_output_section_statements (void)
127 /* See PR 22920 for an example of why this is necessary. */
128 if (strstr (bfd_get_target (link_info.output_bfd), "riscv") == NULL)
130 /* The RISC-V backend needs special fields in the output hash structure.
131 These will only be created if the output format is a RISC-V format,
132 hence we do not support linking and changing output formats at the
133 same time. Use a link followed by objcopy to change output formats. */
134 einfo (_("%F%P: error: cannot change output format"
135 " whilst linking %s binaries\n"), "RISC-V");
139 riscv_elf${ELFSIZE}_set_options (&link_info, ¶ms);
144 LDEMUL_BEFORE_ALLOCATION=riscv_elf_before_allocation
145 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
146 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=riscv_create_output_section_statements