1# This shell script emits a C file. -*- C -*- 2# Copyright (C) 2021-2022 Free Software Foundation, Inc. 3# Contributed by Loongson Ltd. 4# 5# This file is part of the GNU Binutils. 6# 7# This program is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 3 of the License, or 10# (at your option) any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program; see the file COPYING3. If not, 19# see <http://www.gnu.org/licenses/>. 20 21fragment <<EOF 22 23#include "ldmain.h" 24#include "ldctor.h" 25#include "elf/loongarch.h" 26 27static void 28larch_elf_before_allocation (void) 29{ 30 gld${EMULATION_NAME}_before_allocation (); 31 32 if (link_info.discard == discard_sec_merge) 33 link_info.discard = discard_l; 34 35 if (!bfd_link_relocatable (&link_info)) 36 { 37 /* We always need at least some relaxation to handle code alignment. */ 38 if (RELAXATION_DISABLED_BY_USER) 39 TARGET_ENABLE_RELAXATION; 40 else 41 ENABLE_RELAXATION; 42 } 43 44 link_info.relax_pass = 3; 45} 46 47static void 48gld${EMULATION_NAME}_after_allocation (void) 49{ 50 int need_layout = 0; 51 52 /* Don't attempt to discard unused .eh_frame sections until the final link, 53 as we can't reliably tell if they're used until after relaxation. */ 54 if (!bfd_link_relocatable (&link_info)) 55 { 56 need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info); 57 if (need_layout < 0) 58 { 59 einfo (_("%X%P: .eh_frame/.stab edit: %E\n")); 60 return; 61 } 62 } 63 64 /* gld${EMULATION_NAME}_map_segments (need_layout); */ 65 ldelf_map_segments (need_layout); 66} 67 68/* This is a convenient point to tell BFD about target specific flags. 69 After the output has been created, but before inputs are read. */ 70 71static void 72larch_create_output_section_statements (void) 73{ 74 /* See PR 22920 for an example of why this is necessary. */ 75 if (strstr (bfd_get_target (link_info.output_bfd), "loong") == NULL) 76 { 77 einfo (_("%F%P: error: cannot change output format" 78 " whilst linking %s binaries\n"), "LoongArch"); 79 return; 80 } 81} 82 83EOF 84 85LDEMUL_BEFORE_ALLOCATION=larch_elf_before_allocation 86LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation 87LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=larch_create_output_section_statements 88