xref: /netbsd-src/external/gpl3/binutils/dist/ld/emultempl/mipself.em (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1# This shell script emits a C file. -*- C -*-
2#   Copyright 2006, 2007 Free Software Foundation, Inc.
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 "ldctor.h"
24#include "elf/mips.h"
25#include "elfxx-mips.h"
26
27#define is_mips_elf(bfd)				\
28  (bfd_get_flavour (bfd) == bfd_target_elf_flavour	\
29   && elf_tdata (bfd) != NULL				\
30   && elf_object_id (bfd) == MIPS_ELF_TDATA)
31
32/* Fake input file for stubs.  */
33static lang_input_statement_type *stub_file;
34static bfd *stub_bfd;
35
36static void
37mips_after_parse (void)
38{
39  /* .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
40     ways.  .gnu.hash needs symbols to be grouped by hash code whereas the
41     MIPS ABI requires a mapping between the GOT and the symbol table.  */
42  if (link_info.emit_gnu_hash)
43    {
44      einfo ("%X%P: .gnu.hash is incompatible with the MIPS ABI\n");
45      link_info.emit_hash = TRUE;
46      link_info.emit_gnu_hash = FALSE;
47    }
48  after_parse_default ();
49}
50
51struct hook_stub_info
52{
53  lang_statement_list_type add;
54  asection *input_section;
55};
56
57/* Traverse the linker tree to find the spot where the stub goes.  */
58
59static bfd_boolean
60hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
61{
62  lang_statement_union_type *l;
63  bfd_boolean ret;
64
65  for (; (l = *lp) != NULL; lp = &l->header.next)
66    {
67      switch (l->header.type)
68	{
69	case lang_constructors_statement_enum:
70	  ret = hook_in_stub (info, &constructor_list.head);
71	  if (ret)
72	    return ret;
73	  break;
74
75	case lang_output_section_statement_enum:
76	  ret = hook_in_stub (info,
77			      &l->output_section_statement.children.head);
78	  if (ret)
79	    return ret;
80	  break;
81
82	case lang_wild_statement_enum:
83	  ret = hook_in_stub (info, &l->wild_statement.children.head);
84	  if (ret)
85	    return ret;
86	  break;
87
88	case lang_group_statement_enum:
89	  ret = hook_in_stub (info, &l->group_statement.children.head);
90	  if (ret)
91	    return ret;
92	  break;
93
94	case lang_input_section_enum:
95	  if (info->input_section == NULL
96	      || l->input_section.section == info->input_section)
97	    {
98	      /* We've found our section.  Insert the stub immediately
99		 before its associated input section.  */
100	      *lp = info->add.head;
101	      *(info->add.tail) = l;
102	      return TRUE;
103	    }
104	  break;
105
106	case lang_data_statement_enum:
107	case lang_reloc_statement_enum:
108	case lang_object_symbols_statement_enum:
109	case lang_output_statement_enum:
110	case lang_target_statement_enum:
111	case lang_input_statement_enum:
112	case lang_assignment_statement_enum:
113	case lang_padding_statement_enum:
114	case lang_address_statement_enum:
115	case lang_fill_statement_enum:
116	  break;
117
118	default:
119	  FAIL ();
120	  break;
121	}
122    }
123  return FALSE;
124}
125
126/* Create a new stub section called STUB_SEC_NAME and arrange for it to
127   be linked in OUTPUT_SECTION.  The section should go at the beginning of
128   OUTPUT_SECTION if INPUT_SECTION is null, otherwise it must go immediately
129   before INPUT_SECTION.  */
130
131static asection *
132mips_add_stub_section (const char *stub_sec_name, asection *input_section,
133		       asection *output_section)
134{
135  asection *stub_sec;
136  flagword flags;
137  const char *secname;
138  lang_output_section_statement_type *os;
139  struct hook_stub_info info;
140
141  /* Create the stub file, if we haven't already.  */
142  if (stub_file == NULL)
143    {
144      stub_file = lang_add_input_file ("linker stubs",
145				       lang_input_file_is_fake_enum,
146				       NULL);
147      stub_bfd = bfd_create ("linker stubs", link_info.output_bfd);
148      if (stub_bfd == NULL
149	  || !bfd_set_arch_mach (stub_bfd,
150				 bfd_get_arch (link_info.output_bfd),
151				 bfd_get_mach (link_info.output_bfd)))
152	{
153	  einfo ("%F%P: can not create BFD %E\n");
154	  return NULL;
155	}
156      stub_bfd->flags |= BFD_LINKER_CREATED;
157      stub_file->the_bfd = stub_bfd;
158      ldlang_add_file (stub_file);
159    }
160
161  /* Create the section.  */
162  stub_sec = bfd_make_section_anyway (stub_bfd, stub_sec_name);
163  if (stub_sec == NULL)
164    goto err_ret;
165
166  /* Set the flags.  */
167  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
168	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
169  if (!bfd_set_section_flags (stub_bfd, stub_sec, flags))
170    goto err_ret;
171
172  /* Create an output section statement.  */
173  secname = bfd_get_section_name (output_section->owner, output_section);
174  os = lang_output_section_find (secname);
175
176  /* Initialize a statement list that contains only the new statement.  */
177  lang_list_init (&info.add);
178  lang_add_section (&info.add, stub_sec, os);
179  if (info.add.head == NULL)
180    goto err_ret;
181
182  /* Insert the new statement in the appropriate place.  */
183  info.input_section = input_section;
184  if (hook_in_stub (&info, &os->children.head))
185    return stub_sec;
186
187 err_ret:
188  einfo ("%X%P: can not make stub section: %E\n");
189  return NULL;
190}
191
192/* This is called before the input files are opened.  */
193
194static void
195mips_create_output_section_statements (void)
196{
197  if (is_mips_elf (link_info.output_bfd))
198    _bfd_mips_elf_init_stubs (&link_info, mips_add_stub_section);
199}
200
201/* This is called after we have merged the private data of the input bfds.  */
202
203static void
204mips_before_allocation (void)
205{
206  flagword flags;
207
208  flags = elf_elfheader (link_info.output_bfd)->e_flags;
209  if (!link_info.shared
210      && !link_info.nocopyreloc
211      && (flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
212    _bfd_mips_elf_use_plts_and_copy_relocs (&link_info);
213
214  gld${EMULATION_NAME}_before_allocation ();
215}
216
217/* Avoid processing the fake stub_file in vercheck, stat_needed and
218   check_needed routines.  */
219
220static void (*real_func) (lang_input_statement_type *);
221
222static void mips_for_each_input_file_wrapper (lang_input_statement_type *l)
223{
224  if (l != stub_file)
225    (*real_func) (l);
226}
227
228static void
229mips_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
230{
231  real_func = func;
232  lang_for_each_input_file (&mips_for_each_input_file_wrapper);
233}
234
235#define lang_for_each_input_file mips_lang_for_each_input_file
236
237EOF
238
239LDEMUL_AFTER_PARSE=mips_after_parse
240LDEMUL_BEFORE_ALLOCATION=mips_before_allocation
241LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements
242