1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 This module writes out the final image by reading sections from the
25 input files, relocating them and writing them out
27 There are two main paths through this module, one for normal
28 operation and one for partial linking.
30 During normal operation, raw section data is read along with the
31 associated relocation information, the relocation info applied and
32 the section data written out on a section by section basis.
34 When partially linking, all the relocation records are read to work
35 out how big the output relocation vector will be. Then raw data is
36 read, relocated and written section by section.
38 Written by Steve Chamberlain steve@cygnus.com
56 /* Static vars for do_warnings and subroutines of it */
57 int list_unresolved_refs; /* List unresolved refs */
58 int list_warning_symbols; /* List warning syms */
59 int list_multiple_defs; /* List multiple definitions */
61 extern char *sys_errlist[];
63 extern unsigned int undefined_global_sym_count;
65 extern bfd *output_bfd;
67 extern struct lang_output_section_statement_struct * create_object_symbols;
72 void lang_for_each_statement(void (*func)());
74 void lang_for_each_statement();
77 extern bfd_size_type largest_section;
78 ld_config_type config;
80 extern unsigned int global_symbol_count;
85 DEFUN(perform_relocation,(input_bfd,
90 asection *input_section AND
94 static asymbol *error_symbol = (asymbol *)NULL;
95 static unsigned int error_count = 0;
96 #define MAX_ERRORS_IN_A_ROW 5
97 bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd, input_section);
99 arelent **reloc_vector = (arelent **)ldmalloc(reloc_size);
101 bfd *ob = output_bfd;
102 asection *os = input_section->output_section;
103 if (config.relocateable_output == false) ob = (bfd *)NULL;
105 input_section->_cooked_size = input_section->_raw_size;
106 input_section->reloc_done = 1;
108 if (bfd_canonicalize_reloc(input_bfd,
113 for (parent = reloc_vector; *parent; parent++)
116 bfd_reloc_status_type r=
117 bfd_perform_relocation(input_bfd,
123 if (r == bfd_reloc_ok) {
124 if (ob != (bfd *)NULL) {
125 /* A parital link, so keep the relocs */
127 /* Add to each relocation the offset of where it lives
128 in the output section */
129 /* (*parent)->address += input_section->output_offset;*/
131 os->orelocation[os->reloc_count] = *parent;
138 arelent *p = *parent;
140 if (ob != (bfd *)NULL) {
141 /* A parital link, so keep the relocs */
142 os->orelocation[os->reloc_count] = *parent;
146 if (p->sym_ptr_ptr != (asymbol **)NULL) {
147 s = *(p->sym_ptr_ptr);
154 case bfd_reloc_undefined:
155 /* We remember the symbol, and never print more than
156 a reasonable number of them in a row */
157 if (s == error_symbol) {
164 if (error_count < MAX_ERRORS_IN_A_ROW) {
165 einfo("%C: undefined reference to `%T'\n",
166 input_bfd, input_section, symbols,
167 (*parent)->address, s);
168 config.make_executable = false;
170 else if (error_count == MAX_ERRORS_IN_A_ROW) {
171 einfo("%C: more undefined references to `%T' follow\n",
172 input_bfd, input_section,
173 symbols, (*parent)->address, s);
176 /* Don't print any more */
179 case bfd_reloc_dangerous:
180 einfo("%B: relocation may be wrong `%T'\n",
183 case bfd_reloc_outofrange:
184 einfo("%B:%s relocation address out of range %T (%V)\n",
185 input_bfd, input_section->name, s, p->address);
187 case bfd_reloc_overflow:
188 einfo("%B:%s relocation overflow in %T reloc type %d\n",
189 input_bfd, input_section->name, s, p->howto->type);
192 einfo("%F%B: relocation error, symbol `%T'\n",
199 free((char *)reloc_vector);
210 DEFUN(copy_and_relocate,(statement),
211 lang_statement_union_type *statement)
213 switch (statement->header.type) {
214 case lang_fill_statement_enum:
217 bfd_byte play_area[SHORT_SIZE];
219 bfd_putshort(output_bfd, statement->fill_statement.fill, play_area);
220 /* Write out all entire shorts */
222 i < statement->fill_statement.size - SHORT_SIZE + 1;
225 bfd_set_section_contents(output_bfd,
226 statement->fill_statement.output_section,
228 statement->data_statement.output_offset +i,
233 /* Now write any remaining byte */
234 if (i < statement->fill_statement.size)
236 bfd_set_section_contents(output_bfd,
237 statement->fill_statement.output_section,
239 statement->data_statement.output_offset +i,
246 case lang_data_statement_enum:
248 bfd_vma value = statement->data_statement.value;
249 bfd_byte play_area[LONG_SIZE];
250 unsigned int size = 0;
251 switch (statement->data_statement.type) {
253 bfd_put_32(output_bfd, value, play_area);
257 bfd_put_16(output_bfd, value, play_area);
261 bfd_put_8(output_bfd, value, play_area);
266 bfd_set_section_contents(output_bfd,
267 statement->data_statement.output_section,
269 statement->data_statement.output_vma,
277 case lang_input_section_enum:
280 asection *i = statement->input_section.section;
281 asection *output_section = i->output_section;
282 lang_input_statement_type *ifile =
283 statement->input_section.ifile;
284 if (ifile->just_syms_flag == false) {
285 bfd *inbfd = ifile->the_bfd;
287 if (output_section->flags & SEC_LOAD &&
288 output_section->flags & SEC_ALLOC
289 && bfd_get_section_size_before_reloc(i) != 0)
291 if(bfd_get_section_contents(inbfd,
295 bfd_get_section_size_before_reloc(i)) == false)
297 einfo("%F%B error reading section contents %E\n", inbfd);
299 /* Set the reloc bit */
300 perform_relocation (inbfd, i, data_area, ifile->asymbols);
303 if(bfd_set_section_contents(output_bfd,
306 (file_ptr)i->output_offset,
307 bfd_get_section_size_after_reloc(i)) == false)
309 einfo("%F%B error writing section contents of %E\n",
320 /* All the other ones fall through */
327 DEFUN_VOID(write_norel)
329 /* Output the text and data segments, relocating as we go. */
330 lang_for_each_statement(copy_and_relocate);
335 DEFUN(read_relocs,(abfd, section, symbols),
337 asection *section AND
340 /* Work out the output section ascociated with this input section */
341 asection *output_section = section->output_section;
343 bfd_size_type reloc_size = bfd_get_reloc_upper_bound(abfd, section);
344 arelent **reloc_vector = (arelent **)ldmalloc(reloc_size);
346 if (bfd_canonicalize_reloc(abfd,
350 output_section->reloc_count += section->reloc_count;
356 DEFUN_VOID(write_rel)
359 Run through each section of each file and work work out the total
360 number of relocation records which will finally be in each output
364 LANG_FOR_EACH_INPUT_SECTION
365 (statement, abfd, section,
366 (read_relocs(abfd, section, statement->asymbols)));
371 Now run though all the output sections and allocate the space for
374 LANG_FOR_EACH_OUTPUT_SECTION
376 (section->orelocation =
377 (arelent **)ldmalloc((bfd_size_type)(sizeof(arelent **)*
378 section->reloc_count)),
379 section->reloc_count = 0,
380 section->flags |= SEC_HAS_CONTENTS));
384 Copy the data, relocating as we go
386 lang_for_each_statement(copy_and_relocate);
392 data_area = (PTR) ldmalloc(largest_section);
393 if (config.relocateable_output == true)
399 write_relaxnorel(output_bfd);
402 /* Output the symbol table (both globals and locals). */
404 /* Print a map, if requested. */
406 if (config.map_file) {
407 ldsym_print_symbol_table ();