1 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 This file is part of GLD, the Gnu Linker.
5 GLD 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 1, or (at your option)
10 GLD 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 GLD; see the file COPYING. If not, write to
17 the Free Software Foundation, 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.
51 #include "ldgram.tab.h"
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 size_t largest_section;
78 ld_config_type config;
80 extern unsigned int global_symbol_count;
84 static void perform_relocation(input_bfd,
89 asection *input_section;
93 static asymbol *error_symbol = (asymbol *)NULL;
94 static unsigned int error_count = 0;
95 #define MAX_ERRORS_IN_A_ROW 5
96 size_t reloc_size = get_reloc_upper_bound(input_bfd, input_section);
98 arelent **reloc_vector = (arelent **)ldmalloc(reloc_size);
100 bfd *ob = output_bfd;
101 asection *os = input_section->output_section;
102 if (config.relocateable_output == false) ob = (bfd *)NULL;
104 if (bfd_canonicalize_reloc(input_bfd,
109 for (parent = reloc_vector; *parent; parent++)
112 bfd_reloc_status_enum_type r=
113 bfd_perform_relocation(input_bfd,
119 if (r == bfd_reloc_ok) {
120 if (ob != (bfd *)NULL) {
121 /* A parital link, so keep the relocs */
122 os->orelocation[os->reloc_count] = *parent;
129 arelent *p = *parent;
131 if (ob != (bfd *)NULL) {
132 /* A parital link, so keep the relocs */
133 os->orelocation[os->reloc_count] = *parent;
137 if (p->sym_ptr_ptr != (asymbol **)NULL) {
138 s = *(p->sym_ptr_ptr);
145 case bfd_reloc_undefined:
146 /* We remember the symbol, and never print more than
147 a reasonable number of them in a row */
148 if (s == error_symbol) {
155 if (error_count < MAX_ERRORS_IN_A_ROW) {
156 info("%C: undefined reference to `%T'\n",
162 config.make_executable = false;
164 else if (error_count == MAX_ERRORS_IN_A_ROW) {
165 info("%C: more undefined references to `%T' follow\n",
173 /* Don't print any more */
176 case bfd_reloc_dangerous:
177 info("%B: relocation may be wrong `%T'\n",
181 case bfd_reloc_outofrange:
182 info("%B:%s relocation address out of range %T (%V)\n",
188 case bfd_reloc_overflow:
189 info("%B:%s relocation overflow in %T reloc type %d\n",
196 info("%F%B: relocation error, symbol `%T'\n",
204 free((char *)reloc_vector);
215 copy_and_relocate(statement)
216 lang_statement_union_type *statement;
218 switch (statement->header.type) {
219 case lang_fill_statement_enum:
222 bfd_byte play_area[SHORT_SIZE];
224 bfd_putshort(output_bfd, statement->fill_statement.fill, play_area);
225 /* Write out all entire shorts */
227 i < statement->fill_statement.size - SHORT_SIZE + 1;
230 bfd_set_section_contents(output_bfd,
231 statement->fill_statement.output_section,
233 statement->data_statement.output_offset +i,
238 /* Now write any remaining byte */
239 if (i < statement->fill_statement.size)
241 bfd_set_section_contents(output_bfd,
242 statement->fill_statement.output_section,
244 statement->data_statement.output_offset +i,
251 case lang_data_statement_enum:
253 bfd_vma value = statement->data_statement.value;
254 bfd_byte play_area[LONG_SIZE];
256 switch (statement->data_statement.type) {
258 bfd_putlong(output_bfd, value, play_area);
262 bfd_putshort(output_bfd, value, play_area);
266 bfd_putchar(output_bfd, value, play_area);
271 bfd_set_section_contents(output_bfd,
272 statement->data_statement.output_section,
274 statement->data_statement.output_vma,
282 case lang_input_section_enum:
285 asection *i = statement->input_section.section;
286 asection *output_section = i->output_section;
287 lang_input_statement_type *ifile =
288 statement->input_section.ifile;
289 if (ifile->just_syms_flag == false) {
290 bfd *inbfd = ifile->the_bfd;
292 if (output_section->flags & SEC_LOAD && i->size != 0)
294 if(bfd_get_section_contents(inbfd,
300 info("%F%B error reading section contents %E\n",
303 perform_relocation (inbfd, i, data_area, ifile->asymbols);
306 if(bfd_set_section_contents(output_bfd,
309 (file_ptr)i->output_offset,
312 info("%F%B error writing section contents of %E\n",
323 /* All the other ones fall through */
332 /* Output the text and data segments, relocating as we go. */
333 lang_for_each_statement(copy_and_relocate);
337 static void read_relocs(abfd, section, symbols)
342 /* Work out the output section ascociated with this input section */
343 asection *output_section = section->output_section;
345 size_t reloc_size = get_reloc_upper_bound(abfd, section);
346 arelent **reloc_vector = (arelent **)ldmalloc(reloc_size);
348 if (bfd_canonicalize_reloc(abfd,
352 output_section->reloc_count += section->reloc_count;
361 Run through each section of each file and work work out the total
362 number of relocation records which will finally be in each output
366 LANG_FOR_EACH_INPUT_SECTION
367 (statement, abfd, section,
368 (read_relocs(abfd, section, statement->asymbols)));
373 Now run though all the output sections and allocate the space for
376 LANG_FOR_EACH_OUTPUT_SECTION
378 (section->orelocation =
379 (arelent **)ldmalloc((size_t)(sizeof(arelent **)*
380 section->reloc_count)),
381 section->reloc_count = 0,
382 section->flags |= SEC_HAS_CONTENTS));
386 Copy the data, relocating as we go
388 lang_for_each_statement(copy_and_relocate);
394 data_area = (PTR) ldmalloc(largest_section);
395 if (config.relocateable_output == true)
404 /* Output the symbol table (both globals and locals). */