RISC-V: PR27566, Do not relax when data segment phase is exp_seg_relro_adjust.
[deliverable/binutils-gdb.git] / ld / emultempl / riscvelf.em
CommitLineData
e23eba97 1# This shell script emits a C file. -*- C -*-
250d07de 2# Copyright (C) 2004-2021 Free Software Foundation, Inc.
e23eba97
NC
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
21fragment <<EOF
22
23#include "ldmain.h"
24#include "ldctor.h"
25#include "elf/riscv.h"
26#include "elfxx-riscv.h"
27
28static void
29riscv_elf_before_allocation (void)
30{
31 gld${EMULATION_NAME}_before_allocation ();
32
33 if (link_info.discard == discard_sec_merge)
34 link_info.discard = discard_l;
35
202d116e
AS
36 if (!bfd_link_relocatable (&link_info))
37 {
38 /* We always need at least some relaxation to handle code alignment. */
39 if (RELAXATION_DISABLED_BY_USER)
40 TARGET_ENABLE_RELAXATION;
41 else
42 ENABLE_RELAXATION;
43 }
e23eba97 44
abd20cb6 45 link_info.relax_pass = 4;
e23eba97
NC
46}
47
48static void
49gld${EMULATION_NAME}_after_allocation (void)
50{
51 int need_layout = 0;
52
53 /* Don't attempt to discard unused .eh_frame sections until the final link,
54 as we can't reliably tell if they're used until after relaxation. */
55 if (!bfd_link_relocatable (&link_info))
56 {
57 need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info);
58 if (need_layout < 0)
59 {
d003af55 60 einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
e23eba97
NC
61 return;
62 }
63 }
64
ef9d2565
NC
65 /* PR 27566, if the phase of data segment is exp_seg_relro_adjust,
66 that means we are still adjusting the relro, and shouldn't do the
67 relaxations at this stage. Otherwise, we will get the symbol
68 values beofore handling the relro, and may cause truncated fails
69 when the relax range crossing the data segment. One of the solution
70 is to monitor the data segment phase while relaxing, to know whether
71 the relro has been handled or not.
72
73 I think we probably need to record more information about data
74 segment or alignments in the future, to make sure it is safe
75 to doing relaxations. */
76 enum phase_enum *phase = &(expld.dataseg.phase);
77 bfd_elf${ELFSIZE}_riscv_set_data_segment_info (&link_info, (int *) phase);
78
ebdcad3f
NC
79 do
80 {
81 ldelf_map_segments (need_layout);
82 }
83 while (bfd_elf${ELFSIZE}_riscv_restart_relax_sections (&link_info));
e23eba97
NC
84}
85
770aa8a3
JW
86/* This is a convenient point to tell BFD about target specific flags.
87 After the output has been created, but before inputs are read. */
88
89static void
90riscv_create_output_section_statements (void)
91{
92 /* See PR 22920 for an example of why this is necessary. */
93 if (strstr (bfd_get_target (link_info.output_bfd), "riscv") == NULL)
94 {
95 /* The RISC-V backend needs special fields in the output hash structure.
96 These will only be created if the output format is a RISC-V format,
97 hence we do not support linking and changing output formats at the
98 same time. Use a link followed by objcopy to change output formats. */
99 einfo (_("%F%P: error: cannot change output format"
100 " whilst linking %s binaries\n"), "RISC-V");
101 return;
102 }
103}
104
e23eba97
NC
105EOF
106
107LDEMUL_BEFORE_ALLOCATION=riscv_elf_before_allocation
108LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
770aa8a3 109LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=riscv_create_output_section_statements
This page took 0.261931 seconds and 4 git commands to generate.