* emultempl/aix.em (gld${EMULATION_NAME}_parse_args): Treat
[deliverable/binutils-gdb.git] / ld / emultempl / hppaelf.em
1 # This shell script emits a C file. -*- C -*-
2 # It does some substitutions.
3 cat >em_${EMULATION_NAME}.c <<EOF
4 /* An emulation for HP PA-RISC ELF linkers.
5 Copyright (C) 1991, 1993 Free Software Foundation, Inc.
6 Written by Steve Chamberlain steve@cygnus.com
7
8 This file is part of GLD, the Gnu Linker.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "bfdlink.h"
27
28 #include "ld.h"
29 #include "config.h"
30 #include "ldemul.h"
31 #include "ldfile.h"
32 #include "ldexp.h"
33 #include "ldlang.h"
34 #include "ldmisc.h"
35 #include "ldmain.h"
36 #include "ldctor.h"
37
38 static lang_input_statement_type *stub_file = 0;
39 static lang_input_section_type *stub_input_section = NULL;
40
41 /* FIXME. This doesn't belong here. */
42 extern lang_statement_list_type file_chain;
43
44 /* Perform some emulation specific initialization. For PA ELF we set
45 up the local label prefix and the output architecture. */
46
47 static void
48 hppaelf_before_parse ()
49 {
50 link_info.lprefix = "L$";
51 link_info.lprefix_len = 2;
52
53 ldfile_output_architecture = bfd_arch_hppa;
54 }
55
56 /* Walk all the lang statements splicing out any padding statements from
57 the list. */
58
59 static void
60 hppaelf_search_for_padding_statements (s, prev)
61 lang_statement_union_type *s;
62 lang_statement_union_type **prev;
63 {
64 lang_statement_union_type *sprev = NULL;
65 for (; s != NULL; s = s->next)
66 {
67 switch (s->header.type)
68 {
69
70 /* We want recursively walk these sections. */
71 case lang_constructors_statement_enum:
72 hppaelf_search_for_padding_statements (constructor_list.head,
73 &constructor_list.head);
74 break;
75
76 case lang_output_section_statement_enum:
77 hppaelf_search_for_padding_statements (s->output_section_statement.
78 children.head,
79 &s->output_section_statement.
80 children.head);
81 break;
82
83 /* Huh? What is a lang_wild_statement? */
84 case lang_wild_statement_enum:
85 hppaelf_search_for_padding_statements(s->wild_statement.
86 children.head,
87 &s->wild_statement.
88 children.head);
89 break;
90
91 /* Here's what we are really looking for. Splice these out of
92 the list. */
93 case lang_padding_statement_enum:
94 if (sprev)
95 sprev->header.next = s->header.next;
96 else
97 **prev = *s;
98 break;
99
100 /* We don't care about these cases. */
101 case lang_data_statement_enum:
102 case lang_object_symbols_statement_enum:
103 case lang_output_statement_enum:
104 case lang_target_statement_enum:
105 case lang_input_section_enum:
106 case lang_input_statement_enum:
107 case lang_assignment_statement_enum:
108 case lang_address_statement_enum:
109 break;
110
111 default:
112 abort ();
113 break;
114 }
115 sprev = s;
116 }
117 }
118
119 /* Final emulation specific call. For the PA we use this opportunity
120 to determine what linker stubs are needed and generate them.
121
122 FIXME: fast-linker work broke this in a big way. statement->asymbols
123 doesn't have anything useful in it anymore. And if we slurp in
124 the symbol table here and pass it down then we get lots of undefined
125 symbols. Egad. */
126
127 static void
128 hppaelf_finish ()
129 {
130
131 /* Disabled until it's fixed to work with the new linker. A noteworty
132 amount of code will still function without linker stubs allowing us
133 to continue testing. */
134
135 /* Only create stubs for final objects. */
136 if (link_info.relocateable == false)
137 {
138 lang_input_statement_type *statement;
139
140 /* Look at all the statements. */
141 for (statement = (lang_input_statement_type *)file_chain.head;
142 statement != NULL;
143 statement = (lang_input_statement_type *)statement->next)
144 {
145 asection *section;
146 bfd *abfd = statement->the_bfd;
147
148 /* Look at all the sections attached to the bfd associated with
149 the current statement. */
150 for (section = abfd->sections;
151 section != (asection *)NULL;
152 section = section ->next)
153 {
154 int new_sym_cnt = 0;
155 int i,j;
156 asymbol *syms;
157
158 /* Do the dirty work; an array of symbols for each new stub
159 will be returned. */
160 syms = hppa_look_for_stubs_in_section (stub_file->the_bfd,
161 abfd,
162 output_bfd,
163 section,
164 &new_sym_cnt,
165 &link_info);
166
167 if (new_sym_cnt > 0 && syms)
168 {
169 struct symbol_cache_entry **old_asymbols;
170
171 old_asymbols = stub_file->asymbols;
172
173 /* Allocate space for the updated symbols */
174 stub_file->asymbols
175 = xmalloc ((stub_file->symbol_count + new_sym_cnt)
176 * sizeof(asymbol *));
177 if (stub_file->asymbols == NULL)
178 abort ();
179
180 /* Copy the old symbols.
181
182 FIXME. Shouldn't we free the space used by the old
183 symbols here? Might there be dangling references
184 made within hppa_look_for_stubs_in_section? */
185 for (j = 0; j < stub_file->symbol_count; j++)
186 stub_file->asymbols[j] = old_asymbols[j];
187
188 /* Copy in the new symbols. */
189 for (j = 0, i = stub_file->symbol_count;
190 j < new_sym_cnt;
191 j++, i++)
192 stub_file->asymbols[i] = &syms[j];
193
194 /* Finally, adjust the symbol count. */
195 stub_file->symbol_count += new_sym_cnt;
196 }
197 }
198 }
199
200 /* Add a statement to get the linker stubs included in the output. */
201 lang_add_wild (".hppa_linker_stubs",NULL);
202
203 /* If stubs were added, then remove all the (now invalid) padding
204 statements. */
205 hppaelf_search_for_padding_statements (stat_ptr->head,
206 &stat_ptr->head);
207 }
208
209 /* Size up the sections again. */
210 lang_size_sections (stat_ptr->head, abs_output_section,
211 &stat_ptr->head, 0, (bfd_vma) 0, false);
212
213 /* FIXME: Do we need to redo the "assignments" too? */
214 }
215
216 /* Create any emulation specific output statements. FIXME? Is this
217 redundant with above lang_add_wild or the code in the script? */
218
219 static void
220 hppaelf_create_output_section_statements ()
221 {
222 asection *stub_sec;
223 asection *output_text_sec = bfd_make_section_old_way (output_bfd, ".text");
224 lang_input_section_type *new_input_sec;
225
226 /* Add a new "input file" (the linker stubs themselves). */
227 stub_file = lang_add_input_file ("linker stubs",
228 lang_input_file_is_fake_enum,
229 NULL);
230 stub_file->the_bfd = bfd_create ("linker stubs", output_bfd);
231 stub_file->symbol_count = 0;
232 stub_file->the_bfd->sections = 0;
233
234 /* Add a section to the fake input file. */
235 stub_sec = bfd_make_section_old_way (stub_file->the_bfd,
236 ".hppa_linker_stubs");
237 stub_sec->output_section = output_text_sec;
238 bfd_set_section_flags (stub_file->the_bfd, stub_sec,
239 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_CODE | SEC_RELOC);
240
241 /* The user data of a bfd points to the input statement attached. */
242 stub_file->the_bfd->usrdata = (void *)stub_file;
243 stub_file->common_section = bfd_make_section(stub_file->the_bfd,"COMMON");
244
245 new_input_sec = (lang_input_section_type *)
246 stat_alloc (sizeof (lang_input_section_type));
247
248 if (new_input_sec)
249 {
250 lang_output_section_statement_type *text_output_sec;
251 lang_statement_union_type *stmt;
252
253 new_input_sec->section = stub_sec;
254 new_input_sec->ifile = stub_file;
255 new_input_sec->header.type = lang_input_section_enum;
256 new_input_sec->header.next = NULL;
257
258 stub_input_section = new_input_sec;
259
260 /* Find the output_section_statement for .text,
261 then find the wild_statement for .hppa_linker_stubs. */
262 text_output_sec = lang_output_section_find (".text");
263
264 stmt = text_output_sec->children.head;
265
266 while (stmt && stmt->header.type != lang_wild_statement_enum)
267 stmt = stmt->header.next;
268
269 /* Do something with the wild statement. FIXME. */
270 if (stmt)
271 {
272 lang_wild_statement_type *wstmt = (lang_wild_statement_type *)stmt;
273 lang_list_init (&wstmt->children);
274 lang_statement_append (&wstmt->children,
275 (lang_statement_union_type *)new_input_sec,
276 &new_input_sec->header.next);
277 }
278 }
279 }
280
281 /* Set the output architecture and machine. */
282
283 static void
284 hppaelf_set_output_arch()
285 {
286 unsigned long machine = 0;
287
288 bfd_set_arch_mach (output_bfd, ldfile_output_architecture, machine);
289 }
290
291 /* The script itself gets inserted here. */
292
293 static char *
294 hppaelf_get_script(isfile)
295 int *isfile;
296 EOF
297
298 if test -n "$COMPILE_IN"
299 then
300 # Scripts compiled in.
301
302 # sed commands to quote an ld script as a C string.
303 sc='s/["\\]/\\&/g
304 s/$/\\n\\/
305 1s/^/"/
306 $s/$/n"/
307 '
308
309 cat >>em_${EMULATION_NAME}.c <<EOF
310 {
311 *isfile = 0;
312
313 if (link_info.relocateable == true && config.build_constructors == true)
314 return `sed "$sc" ldscripts/${EMULATION_NAME}.xu`;
315 else if (link_info.relocateable == true)
316 return `sed "$sc" ldscripts/${EMULATION_NAME}.xr`;
317 else if (!config.text_read_only)
318 return `sed "$sc" ldscripts/${EMULATION_NAME}.xbn`;
319 else if (!config.magic_demand_paged)
320 return `sed "$sc" ldscripts/${EMULATION_NAME}.xn`;
321 else
322 return `sed "$sc" ldscripts/${EMULATION_NAME}.x`;
323 }
324 EOF
325
326 else
327 # Scripts read from the filesystem.
328
329 cat >>em_${EMULATION_NAME}.c <<EOF
330 {
331 *isfile = 1;
332
333 if (link_info.relocateable == true && config.build_constructors == true)
334 return "ldscripts/${EMULATION_NAME}.xu";
335 else if (link_info.relocateable == true)
336 return "ldscripts/${EMULATION_NAME}.xr";
337 else if (!config.text_read_only)
338 return "ldscripts/${EMULATION_NAME}.xbn";
339 else if (!config.magic_demand_paged)
340 return "ldscripts/${EMULATION_NAME}.xn";
341 else
342 return "ldscripts/${EMULATION_NAME}.x";
343 }
344 EOF
345
346 fi
347
348 cat >>em_${EMULATION_NAME}.c <<EOF
349
350 struct ld_emulation_xfer_struct ld_hppaelf_emulation =
351 {
352 hppaelf_before_parse,
353 syslib_default,
354 hll_default,
355 after_parse_default,
356 after_allocation_default,
357 hppaelf_set_output_arch,
358 ldemul_default_target,
359 before_allocation_default,
360 hppaelf_get_script,
361 "hppaelf",
362 "elf32-hppa",
363 hppaelf_finish,
364 hppaelf_create_output_section_statements
365 };
366 EOF
This page took 0.037528 seconds and 4 git commands to generate.