Made many changes to eliminate gcc warnings. Made various
[external/binutils.git] / ld / ldwrite.c
1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2
3 This file is part of GLD, the Gnu Linker.
4
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.
9
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.
14
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.  */
18
19 /*
20    This module writes out the final image by reading sections from the
21    input files, relocating them and writing them out
22
23    There are two main paths through this module, one for normal
24    operation and one for partial linking.
25
26    During  normal operation, raw section data is read along with the
27    associated relocation information, the relocation info applied and
28    the section data written out on a section by section basis.
29
30    When partially linking, all the relocation records are read to work
31    out how big the output relocation vector will be. Then raw data is
32    read, relocated and written section by section.
33
34    Written by Steve Chamberlain sac@cygnus.com
35
36 */
37
38
39 #include "bfd.h"
40 #include "sysdep.h"
41
42 #include "ld.h"
43 #include "ldexp.h"
44 #include "ldlang.h"
45 #include "ldwrite.h"
46 #include "ldmisc.h"
47 #include "ldsym.h"
48 #include "ldgram.h"
49 #include "ldmain.h"
50 #include "relax.h"
51
52 static void
53 read_relocs (abfd, section, symbols)
54      bfd * abfd;
55      asection * section;
56      asymbol ** symbols;
57 {
58   /* Work out the output section ascociated with this input section */
59   asection *output_section = section->output_section;
60
61   bfd_size_type reloc_size = bfd_get_reloc_upper_bound (abfd, section);
62   arelent **reloc_vector = (arelent **) ldmalloc (reloc_size);
63
64   if (bfd_canonicalize_reloc (abfd,
65                               section,
66                               reloc_vector,
67                               symbols))
68     {
69       output_section->reloc_count += section->reloc_count;
70     }
71 }
72
73
74 static void
75 setup_rel ()
76 {
77   /*
78     Run through each section of each file and work work out the total
79     number of relocation records which will finally be in each output
80     section
81     */
82
83   LANG_FOR_EACH_INPUT_SECTION
84   (statement, abfd, section,
85    (read_relocs (abfd, section, statement->asymbols)));
86
87
88
89   /*
90     Now run though all the output sections and allocate the space for
91     all the relocations
92     */
93   LANG_FOR_EACH_OUTPUT_SECTION
94     (section,
95      (section->orelocation =
96       (arelent **) ldmalloc ((bfd_size_type) (sizeof (arelent **) *
97                                               section->reloc_count)),
98       section->reloc_count = 0));
99 }
100
101 void
102 ldwrite ()
103 {
104   PTR data_area = (PTR) ldmalloc (largest_section);
105
106   ldsym_write ();
107
108   if (config.relocateable_output == true)
109     setup_rel ();
110
111   write_relax (output_bfd, data_area, config.relocateable_output);
112
113   free (data_area);
114
115   /* Output the symbol table (both globals and locals).  */
116
117   /* Print a map, if requested and possible.  */
118
119   if (config.map_file)
120   {
121     ldsym_print_symbol_table ();
122     lang_map ();
123   }
124 }