PR 868
[deliverable/binutils-gdb.git] / ld / emultempl / m68kelf.em
CommitLineData
0752970e 1# This shell script emits a C file. -*- C -*-
f96b4a7b 2# Copyright 2000, 2001, 2003, 2007 Free Software Foundation, Inc.
0752970e
NC
3# Written by Michael Sokolov <msokolov@ivan.Harhan.ORG>, based on armelf.em
4#
f96b4a7b 5# This file is part of the GNU Binutils.
0752970e
NC
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
f96b4a7b 9# the Free Software Foundation; either version 3 of the License, or
0752970e
NC
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; if not, write to the Free Software
f96b4a7b
NC
19# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20# MA 02110-1301, USA.
21
0752970e 22
36bdbeec
NC
23# This file is sourced from elf32.em, and defines some extra routines for m68k
24# embedded systems using ELF and for some other systems using m68k ELF. While
25# it is sourced from elf32.em for all m68k ELF configurations, here we include
26# only the features we want depending on the configuration.
27
28case ${target} in
29 m68*-*-elf)
30 echo "#define SUPPORT_EMBEDDED_RELOCS" >>e${EMULATION_NAME}.c
31 ;;
32esac
33
92b93329 34fragment <<EOF
0752970e 35
36bdbeec 36#ifdef SUPPORT_EMBEDDED_RELOCS
0c7a8e5a 37static void check_sections (bfd *, asection *, void *);
36bdbeec 38#endif
0752970e 39
36bdbeec 40/* This function is run after all the input files have been opened. */
0752970e
NC
41
42static void
0c7a8e5a 43m68k_elf_after_open (void)
0752970e 44{
0752970e
NC
45 /* Call the standard elf routine. */
46 gld${EMULATION_NAME}_after_open ();
47
36bdbeec
NC
48#ifdef SUPPORT_EMBEDDED_RELOCS
49 if (command_line.embedded_relocs
1049f94e 50 && (! link_info.relocatable))
0752970e 51 {
36bdbeec
NC
52 bfd *abfd;
53
54 /* In the embedded relocs mode we create a .emreloc section for each
55 input file with a nonzero .data section. The BFD backend will fill in
56 these sections with magic numbers which can be used to relocate the
57 data section at run time. */
58 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
0752970e 59 {
36bdbeec
NC
60 asection *datasec;
61
62 /* As first-order business, make sure that each input BFD is either
63 COFF or ELF. We need to call a special BFD backend function to
64 generate the embedded relocs, and we have such functions only for
65 COFF and ELF. */
66 if (bfd_get_flavour (abfd) != bfd_target_coff_flavour
67 && bfd_get_flavour (abfd) != bfd_target_elf_flavour)
68 einfo ("%F%B: all input objects must be COFF or ELF for --embedded-relocs\n");
69
70 datasec = bfd_get_section_by_name (abfd, ".data");
71
72 /* Note that we assume that the reloc_count field has already
73 been set up. We could call bfd_get_reloc_upper_bound, but
74 that returns the size of a memory buffer rather than a reloc
75 count. We do not want to call bfd_canonicalize_reloc,
76 because although it would always work it would force us to
77 read in the relocs into BFD canonical form, which would waste
78 a significant amount of time and memory. */
79 if (datasec != NULL && datasec->reloc_count > 0)
80 {
81 asection *relsec;
82
83 relsec = bfd_make_section (abfd, ".emreloc");
84 if (relsec == NULL
85 || ! bfd_set_section_flags (abfd, relsec,
86 (SEC_ALLOC
87 | SEC_LOAD
88 | SEC_HAS_CONTENTS
89 | SEC_IN_MEMORY))
90 || ! bfd_set_section_alignment (abfd, relsec, 2)
91 || ! bfd_set_section_size (abfd, relsec,
92 datasec->reloc_count * 12))
93 einfo ("%F%B: can not create .emreloc section: %E\n");
94 }
95
96 /* Double check that all other data sections are empty, as is
97 required for embedded PIC code. */
1579bae1 98 bfd_map_over_sections (abfd, check_sections, datasec);
0752970e 99 }
0752970e 100 }
36bdbeec 101#endif /* SUPPORT_EMBEDDED_RELOCS */
0752970e
NC
102}
103
36bdbeec 104#ifdef SUPPORT_EMBEDDED_RELOCS
0752970e
NC
105/* Check that of the data sections, only the .data section has
106 relocs. This is called via bfd_map_over_sections. */
107
108static void
1579bae1 109check_sections (bfd *abfd, asection *sec, void *datasec)
0752970e
NC
110{
111 if ((bfd_get_section_flags (abfd, sec) & SEC_DATA)
1579bae1 112 && sec != datasec
0752970e
NC
113 && sec->reloc_count != 0)
114 einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
115 abfd, bfd_get_section_name (abfd, sec));
116}
117
36bdbeec
NC
118#endif /* SUPPORT_EMBEDDED_RELOCS */
119
0752970e 120/* This function is called after the section sizes and offsets have
36bdbeec 121 been set. */
0752970e
NC
122
123static void
0c7a8e5a 124m68k_elf_after_allocation (void)
0752970e 125{
0752970e 126 /* Call the standard elf routine. */
1c7566d1 127 after_allocation_default ();
0752970e 128
36bdbeec
NC
129#ifdef SUPPORT_EMBEDDED_RELOCS
130 if (command_line.embedded_relocs
1049f94e 131 && (! link_info.relocatable))
0752970e 132 {
36bdbeec 133 bfd *abfd;
0752970e 134
36bdbeec
NC
135 /* If we are generating embedded relocs, call a special BFD backend
136 routine to do the work. */
137 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
0752970e 138 {
36bdbeec
NC
139 asection *datasec, *relsec;
140 char *errmsg;
141
142 datasec = bfd_get_section_by_name (abfd, ".data");
143
144 if (datasec == NULL || datasec->reloc_count == 0)
145 continue;
146
147 relsec = bfd_get_section_by_name (abfd, ".emreloc");
148 ASSERT (relsec != NULL);
149
150 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
151 {
152 if (! bfd_m68k_coff_create_embedded_relocs (abfd, &link_info,
153 datasec, relsec,
154 &errmsg))
155 {
156 if (errmsg == NULL)
157 einfo ("%B%X: can not create runtime reloc information: %E\n",
158 abfd);
159 else
160 einfo ("%X%B: can not create runtime reloc information: %s\n",
161 abfd, errmsg);
162 }
163 }
164 else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
165 {
166 if (! bfd_m68k_elf32_create_embedded_relocs (abfd, &link_info,
167 datasec, relsec,
168 &errmsg))
169 {
170 if (errmsg == NULL)
171 einfo ("%B%X: can not create runtime reloc information: %E\n",
172 abfd);
173 else
174 einfo ("%X%B: can not create runtime reloc information: %s\n",
175 abfd, errmsg);
176 }
177 }
0752970e 178 else
36bdbeec 179 abort ();
0752970e
NC
180 }
181 }
36bdbeec 182#endif /* SUPPORT_EMBEDDED_RELOCS */
0752970e
NC
183}
184
185EOF
186
187# We have our own after_open and after_allocation functions, but they call
188# the standard routines, so give them a different name.
189LDEMUL_AFTER_OPEN=m68k_elf_after_open
190LDEMUL_AFTER_ALLOCATION=m68k_elf_after_allocation
This page took 0.392661 seconds and 4 git commands to generate.