1# This shell script emits a C file. -*- C -*-
2#   Copyright (C) 2000-2016 Free Software Foundation, Inc.
3#   Written by Michael Sokolov <msokolov@ivan.Harhan.ORG>, based on armelf.em
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; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20# MA 02110-1301, USA.
21
22
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
34case ${target} in
35  *-linux*)
36# Don't use multi-GOT by default due to glibc linker's assumption
37# that GOT pointer points to GOT[0].
38#   got_handling_target_default=GOT_HANDLING_MULTIGOT
39    got_handling_target_default=GOT_HANDLING_SINGLE
40    ;;
41  *)
42    got_handling_target_default=GOT_HANDLING_SINGLE
43    ;;
44esac
45
46fragment <<EOF
47
48#define GOT_HANDLING_SINGLE   (0)
49#define GOT_HANDLING_NEGATIVE (1)
50#define GOT_HANDLING_MULTIGOT (2)
51#define GOT_HANDLING_TARGET_DEFAULT ${got_handling_target_default}
52
53/* How to generate GOT.  */
54static int got_handling = GOT_HANDLING_DEFAULT;
55
56#ifdef SUPPORT_EMBEDDED_RELOCS
57static void check_sections (bfd *, asection *, void *);
58#endif
59
60/* This function is run after all the input files have been opened.  */
61
62static void
63m68k_elf_after_open (void)
64{
65  /* Call the standard elf routine.  */
66  gld${EMULATION_NAME}_after_open ();
67
68#ifdef SUPPORT_EMBEDDED_RELOCS
69  if (command_line.embedded_relocs
70      && (!bfd_link_relocatable (&link_info)))
71    {
72      bfd *abfd;
73
74      /* In the embedded relocs mode we create a .emreloc section for each
75	 input file with a nonzero .data section.  The BFD backend will fill in
76	 these sections with magic numbers which can be used to relocate the
77	 data section at run time.  */
78      for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
79	{
80	  asection *datasec;
81
82	  /* As first-order business, make sure that each input BFD is either
83	     COFF or ELF.  We need to call a special BFD backend function to
84	     generate the embedded relocs, and we have such functions only for
85	     COFF and ELF.  */
86	  if (bfd_get_flavour (abfd) != bfd_target_coff_flavour
87	      && bfd_get_flavour (abfd) != bfd_target_elf_flavour)
88	    einfo ("%F%B: all input objects must be COFF or ELF for --embedded-relocs\n");
89
90	  datasec = bfd_get_section_by_name (abfd, ".data");
91
92	  /* Note that we assume that the reloc_count field has already
93	     been set up.  We could call bfd_get_reloc_upper_bound, but
94	     that returns the size of a memory buffer rather than a reloc
95	     count.  We do not want to call bfd_canonicalize_reloc,
96	     because although it would always work it would force us to
97	     read in the relocs into BFD canonical form, which would waste
98	     a significant amount of time and memory.  */
99	  if (datasec != NULL && datasec->reloc_count > 0)
100	    {
101	      asection *relsec;
102
103	      relsec = bfd_make_section_with_flags (abfd, ".emreloc",
104						    (SEC_ALLOC
105						    | SEC_LOAD
106						    | SEC_HAS_CONTENTS
107						    | SEC_IN_MEMORY));
108	      if (relsec == NULL
109		  || ! bfd_set_section_alignment (abfd, relsec, 2)
110		  || ! bfd_set_section_size (abfd, relsec,
111					     datasec->reloc_count * 12))
112		einfo ("%F%B: can not create .emreloc section: %E\n");
113	    }
114
115	  /* Double check that all other data sections are empty, as is
116	     required for embedded PIC code.  */
117	  bfd_map_over_sections (abfd, check_sections, datasec);
118	}
119    }
120#endif /* SUPPORT_EMBEDDED_RELOCS */
121}
122
123#ifdef SUPPORT_EMBEDDED_RELOCS
124/* Check that of the data sections, only the .data section has
125   relocs.  This is called via bfd_map_over_sections.  */
126
127static void
128check_sections (bfd *abfd, asection *sec, void *datasec)
129{
130  if ((bfd_get_section_flags (abfd, sec) & SEC_DATA)
131      && sec != datasec
132      && sec->reloc_count != 0)
133    einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
134	   abfd, bfd_get_section_name (abfd, sec));
135}
136
137#endif /* SUPPORT_EMBEDDED_RELOCS */
138
139/* This function is called after the section sizes and offsets have
140   been set.  */
141
142static void
143m68k_elf_after_allocation (void)
144{
145  /* Call the standard elf routine.  */
146  gld${EMULATION_NAME}_after_allocation ();
147
148#ifdef SUPPORT_EMBEDDED_RELOCS
149  if (command_line.embedded_relocs
150      && (!bfd_link_relocatable (&link_info)))
151    {
152      bfd *abfd;
153
154      /* If we are generating embedded relocs, call a special BFD backend
155	 routine to do the work.  */
156      for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
157	{
158	  asection *datasec, *relsec;
159	  char *errmsg;
160
161	  datasec = bfd_get_section_by_name (abfd, ".data");
162
163	  if (datasec == NULL || datasec->reloc_count == 0)
164	    continue;
165
166	  relsec = bfd_get_section_by_name (abfd, ".emreloc");
167	  ASSERT (relsec != NULL);
168
169	  if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
170	    {
171	      if (! bfd_m68k_coff_create_embedded_relocs (abfd, &link_info,
172							  datasec, relsec,
173							  &errmsg))
174		{
175		  if (errmsg == NULL)
176		    einfo ("%B%X: can not create runtime reloc information: %E\n",
177			   abfd);
178		  else
179		    einfo ("%X%B: can not create runtime reloc information: %s\n",
180			   abfd, errmsg);
181		}
182	    }
183	  else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
184	    {
185	      if (! bfd_m68k_elf32_create_embedded_relocs (abfd, &link_info,
186							   datasec, relsec,
187							   &errmsg))
188		{
189		  if (errmsg == NULL)
190		    einfo ("%B%X: can not create runtime reloc information: %E\n",
191			   abfd);
192		  else
193		    einfo ("%X%B: can not create runtime reloc information: %s\n",
194			   abfd, errmsg);
195		}
196	    }
197	  else
198	    abort ();
199	}
200    }
201#endif /* SUPPORT_EMBEDDED_RELOCS */
202}
203
204/* This is a convenient point to tell BFD about target specific flags.
205   After the output has been created, but before inputs are read.  */
206
207static void
208elf_m68k_create_output_section_statements (void)
209{
210  bfd_elf_m68k_set_target_options (&link_info, got_handling);
211}
212
213EOF
214
215# Define some shell vars to insert bits of code into the standard elf
216# parse_args and list_options functions.
217#
218PARSE_AND_LIST_PROLOGUE='
219#define OPTION_GOT	301
220'
221
222PARSE_AND_LIST_LONGOPTS='
223  { "got", required_argument, NULL, OPTION_GOT},
224'
225
226PARSE_AND_LIST_OPTIONS='
227  fprintf (file, _("  --got=<type>                Specify GOT handling scheme\n"));
228'
229
230PARSE_AND_LIST_ARGS_CASES='
231    case OPTION_GOT:
232      if (strcmp (optarg, "target") == 0)
233        got_handling = GOT_HANDLING_TARGET_DEFAULT;
234      else if (strcmp (optarg, "single") == 0)
235        got_handling = 0;
236      else if (strcmp (optarg, "negative") == 0)
237        got_handling = 1;
238      else if (strcmp (optarg, "multigot") == 0)
239        got_handling = 2;
240      else
241        einfo (_("Unrecognized --got argument '\''%s'\''.\n"), optarg);
242      break;
243'
244
245# We have our own after_open and after_allocation functions, but they call
246# the standard routines, so give them a different name.
247LDEMUL_AFTER_OPEN=m68k_elf_after_open
248LDEMUL_AFTER_ALLOCATION=m68k_elf_after_allocation
249LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=elf_m68k_create_output_section_statements
250