1# This shell script emits a C file. -*- C -*-
2# It does some substitutions.
3cat >e${EMULATION_NAME}.c <<EOF
4/* An emulation for HP PA-RISC ELF linkers.
5   Copyright (C) 1991, 93, 94, 95, 97, 1999 Free Software Foundation, Inc.
6   Written by Steve Chamberlain steve@cygnus.com
7
8This file is part of GLD, the Gnu Linker.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
24#include "bfd.h"
25#include "sysdep.h"
26#include "bfdlink.h"
27
28#include "ld.h"
29#include "ldemul.h"
30#include "ldfile.h"
31#include "ldexp.h"
32#include "ldlang.h"
33#include "ldmisc.h"
34#include "ldmain.h"
35#include "ldctor.h"
36
37/* Section in which we build stubs.  */
38static asection *stub_sec;
39static lang_input_statement_type *stub_file;
40
41
42/* FIXME.  This doesn't belong here.  */
43extern lang_statement_list_type file_chain;
44
45/* Perform some emulation specific initialization.  For PA ELF we set
46   up the local label prefix and the output architecture.  */
47
48static void
49hppaelf_before_parse ()
50{
51  ldfile_output_architecture = bfd_arch_hppa;
52}
53
54/* Set the output architecture and machine.  */
55
56static void
57hppaelf_set_output_arch()
58{
59  unsigned long machine = 0;
60
61  bfd_set_arch_mach (output_bfd, ldfile_output_architecture, machine);
62}
63
64/* This is called before the input files are opened.  We create a new
65   fake input file to hold the stub section.  */
66
67static void
68hppaelf_create_output_section_statements ()
69{
70  stub_file = lang_add_input_file ("linker stubs",
71				   lang_input_file_is_fake_enum,
72				   NULL);
73  stub_file->the_bfd = bfd_create ("linker stubs", output_bfd);
74  if (stub_file->the_bfd == NULL
75      || ! bfd_set_arch_mach (stub_file->the_bfd,
76			      bfd_get_arch (output_bfd),
77			      bfd_get_mach (output_bfd)))
78    {
79      einfo ("%X%P: can not create BFD %E\n");
80      return;
81    }
82
83  stub_sec = bfd_make_section_old_way (stub_file->the_bfd, ".text");
84  /* Don't set SEC_RELOC until we actually have relocations in this
85     section.  */
86  if (stub_sec == NULL
87      || ! bfd_set_section_flags (stub_file->the_bfd, stub_sec,
88				  (SEC_HAS_CONTENTS
89				   | SEC_ALLOC
90				   | SEC_LOAD
91				   | SEC_CODE
92				   | SEC_IN_MEMORY)))
93    {
94      einfo ("%X%P: can not create stub section: %E\n");
95      return;
96    }
97
98  ldlang_add_file (stub_file);
99}
100
101/* Walk all the lang statements splicing out any padding statements from
102   the list.  */
103
104static void
105hppaelf_delete_padding_statements (s, prev)
106     lang_statement_union_type *s;
107     lang_statement_union_type **prev;
108{
109  lang_statement_union_type *sprev = NULL;
110  for (; s != NULL; s = s->next)
111    {
112      switch (s->header.type)
113	{
114
115	/* We want recursively walk these sections.  */
116	case lang_constructors_statement_enum:
117	  hppaelf_delete_padding_statements (constructor_list.head,
118					     &constructor_list.head);
119	  break;
120
121	case lang_output_section_statement_enum:
122	  hppaelf_delete_padding_statements (s->output_section_statement.
123					       children.head,
124					     &s->output_section_statement.
125					       children.head);
126	  break;
127
128	/* Huh?  What is a lang_wild_statement?  */
129	case lang_wild_statement_enum:
130	  hppaelf_delete_padding_statements (s->wild_statement.
131					       children.head,
132					     &s->wild_statement.
133					       children.head);
134	  break;
135
136	/* Here's what we are really looking for.  Splice these out of
137	   the list.  */
138	case lang_padding_statement_enum:
139	  if (sprev)
140	    sprev->header.next = s->header.next;
141	  else
142	    **prev = *s;
143	  break;
144
145	/* We don't care about these cases.  */
146	case lang_data_statement_enum:
147	case lang_object_symbols_statement_enum:
148	case lang_output_statement_enum:
149	case lang_target_statement_enum:
150	case lang_input_section_enum:
151	case lang_input_statement_enum:
152	case lang_assignment_statement_enum:
153	case lang_address_statement_enum:
154	  break;
155
156	default:
157	  abort ();
158	  break;
159	}
160      sprev = s;
161    }
162}
163
164/* Final emulation specific call.  For the PA we use this opportunity
165   to build linker stubs.  */
166
167static void
168hppaelf_finish ()
169{
170  /* Call into the BFD backend to do the real work.  */
171  if (elf32_hppa_size_stubs (stub_file->the_bfd, output_bfd, &link_info)
172      == false)
173    {
174      einfo ("%X%P: can not size stub section: %E\n");
175      return;
176    }
177
178  /* If the size of the stub section is nonzero, then we need
179     to resize the sections, recompute the assignments, and finally
180     build the stubs.  */
181  if (bfd_section_size (stub_file->the_bfd, stub_file->the_bfd->sections) != 0)
182    {
183      /* Delete all the padding statements, they're no longer valid.  */
184      hppaelf_delete_padding_statements (stat_ptr->head, &stat_ptr->head);
185
186      /* Resize the sections.  */
187      lang_size_sections (stat_ptr->head, abs_output_section,
188			  &stat_ptr->head, 0, (bfd_vma) 0, false);
189
190      /* Redo special stuff.  */
191      ldemul_after_allocation ();
192
193      /* Do the assignments again.  */
194      lang_do_assignments (stat_ptr->head,
195			   abs_output_section,
196			   (fill_type) 0, (bfd_vma) 0);
197
198      /* Now build the linker stubs.  */
199      if (elf32_hppa_build_stubs (stub_file->the_bfd, &link_info) == false)
200	{
201	  einfo ("%X%P: can not build stubs: %E\n");
202	  return;
203	}
204    }
205}
206
207/* The script itself gets inserted here.  */
208
209static char *
210hppaelf_get_script(isfile)
211     int *isfile;
212EOF
213
214if test -n "$COMPILE_IN"
215then
216# Scripts compiled in.
217
218# sed commands to quote an ld script as a C string.
219sc="-f stringify.sed"
220
221cat >>e${EMULATION_NAME}.c <<EOF
222{
223  *isfile = 0;
224
225  if (link_info.relocateable == true && config.build_constructors == true)
226    return
227EOF
228sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
229echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
230sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
231echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
232sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
233echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
234sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
235echo '  ; else return'                                     >> e${EMULATION_NAME}.c
236sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
237echo '; }'                                                 >> e${EMULATION_NAME}.c
238
239else
240# Scripts read from the filesystem.
241
242cat >>e${EMULATION_NAME}.c <<EOF
243{
244  *isfile = 1;
245
246  if (link_info.relocateable == true && config.build_constructors == true)
247    return "ldscripts/${EMULATION_NAME}.xu";
248  else if (link_info.relocateable == true)
249    return "ldscripts/${EMULATION_NAME}.xr";
250  else if (!config.text_read_only)
251    return "ldscripts/${EMULATION_NAME}.xbn";
252  else if (!config.magic_demand_paged)
253    return "ldscripts/${EMULATION_NAME}.xn";
254  else
255    return "ldscripts/${EMULATION_NAME}.x";
256}
257EOF
258
259fi
260
261cat >>e${EMULATION_NAME}.c <<EOF
262
263struct ld_emulation_xfer_struct ld_hppaelf_emulation =
264{
265  hppaelf_before_parse,
266  syslib_default,
267  hll_default,
268  after_parse_default,
269  after_open_default,
270  after_allocation_default,
271  hppaelf_set_output_arch,
272  ldemul_default_target,
273  before_allocation_default,
274  hppaelf_get_script,
275  "hppaelf",
276  "elf32-hppa",
277  hppaelf_finish,
278  hppaelf_create_output_section_statements,
279  NULL,	/* open dynamic archive */
280  NULL,	/* place orphan */
281  NULL,	/* set symbols */
282  NULL,	/* parse args */
283  NULL,	/* unrecognized file */
284  NULL,	/* list options */
285  NULL,	/* recognized file */
286  NULL 	/* find_potential_libraries */
287};
288EOF
289