1*a9fa9459Szrj /* COFF specific linker code.
2*a9fa9459Szrj Copyright (C) 1994-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj Written by Ian Lance Taylor, Cygnus Support.
4*a9fa9459Szrj
5*a9fa9459Szrj This file is part of BFD, the Binary File Descriptor library.
6*a9fa9459Szrj
7*a9fa9459Szrj This program is free software; you can redistribute it and/or modify
8*a9fa9459Szrj it under the terms of the GNU General Public License as published by
9*a9fa9459Szrj the Free Software Foundation; either version 3 of the License, or
10*a9fa9459Szrj (at your option) any later version.
11*a9fa9459Szrj
12*a9fa9459Szrj This program is distributed in the hope that it will be useful,
13*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
14*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*a9fa9459Szrj GNU General Public License for more details.
16*a9fa9459Szrj
17*a9fa9459Szrj You should have received a copy of the GNU General Public License
18*a9fa9459Szrj along with this program; if not, write to the Free Software
19*a9fa9459Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20*a9fa9459Szrj MA 02110-1301, USA. */
21*a9fa9459Szrj
22*a9fa9459Szrj /* This file contains the COFF backend linker code. */
23*a9fa9459Szrj
24*a9fa9459Szrj #include "sysdep.h"
25*a9fa9459Szrj #include "bfd.h"
26*a9fa9459Szrj #include "bfdlink.h"
27*a9fa9459Szrj #include "libbfd.h"
28*a9fa9459Szrj #include "coff/internal.h"
29*a9fa9459Szrj #include "libcoff.h"
30*a9fa9459Szrj #include "safe-ctype.h"
31*a9fa9459Szrj
32*a9fa9459Szrj static bfd_boolean coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
33*a9fa9459Szrj static bfd_boolean coff_link_check_archive_element
34*a9fa9459Szrj (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
35*a9fa9459Szrj bfd_boolean *);
36*a9fa9459Szrj static bfd_boolean coff_link_add_symbols (bfd *, struct bfd_link_info *);
37*a9fa9459Szrj
38*a9fa9459Szrj /* Return TRUE if SYM is a weak, external symbol. */
39*a9fa9459Szrj #define IS_WEAK_EXTERNAL(abfd, sym) \
40*a9fa9459Szrj ((sym).n_sclass == C_WEAKEXT \
41*a9fa9459Szrj || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
42*a9fa9459Szrj
43*a9fa9459Szrj /* Return TRUE if SYM is an external symbol. */
44*a9fa9459Szrj #define IS_EXTERNAL(abfd, sym) \
45*a9fa9459Szrj ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
46*a9fa9459Szrj
47*a9fa9459Szrj /* Define macros so that the ISFCN, et. al., macros work correctly.
48*a9fa9459Szrj These macros are defined in include/coff/internal.h in terms of
49*a9fa9459Szrj N_TMASK, etc. These definitions require a user to define local
50*a9fa9459Szrj variables with the appropriate names, and with values from the
51*a9fa9459Szrj coff_data (abfd) structure. */
52*a9fa9459Szrj
53*a9fa9459Szrj #define N_TMASK n_tmask
54*a9fa9459Szrj #define N_BTSHFT n_btshft
55*a9fa9459Szrj #define N_BTMASK n_btmask
56*a9fa9459Szrj
57*a9fa9459Szrj /* Create an entry in a COFF linker hash table. */
58*a9fa9459Szrj
59*a9fa9459Szrj struct bfd_hash_entry *
_bfd_coff_link_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)60*a9fa9459Szrj _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
61*a9fa9459Szrj struct bfd_hash_table *table,
62*a9fa9459Szrj const char *string)
63*a9fa9459Szrj {
64*a9fa9459Szrj struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
65*a9fa9459Szrj
66*a9fa9459Szrj /* Allocate the structure if it has not already been allocated by a
67*a9fa9459Szrj subclass. */
68*a9fa9459Szrj if (ret == (struct coff_link_hash_entry *) NULL)
69*a9fa9459Szrj ret = ((struct coff_link_hash_entry *)
70*a9fa9459Szrj bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
71*a9fa9459Szrj if (ret == (struct coff_link_hash_entry *) NULL)
72*a9fa9459Szrj return (struct bfd_hash_entry *) ret;
73*a9fa9459Szrj
74*a9fa9459Szrj /* Call the allocation method of the superclass. */
75*a9fa9459Szrj ret = ((struct coff_link_hash_entry *)
76*a9fa9459Szrj _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
77*a9fa9459Szrj table, string));
78*a9fa9459Szrj if (ret != (struct coff_link_hash_entry *) NULL)
79*a9fa9459Szrj {
80*a9fa9459Szrj /* Set local fields. */
81*a9fa9459Szrj ret->indx = -1;
82*a9fa9459Szrj ret->type = T_NULL;
83*a9fa9459Szrj ret->symbol_class = C_NULL;
84*a9fa9459Szrj ret->numaux = 0;
85*a9fa9459Szrj ret->auxbfd = NULL;
86*a9fa9459Szrj ret->aux = NULL;
87*a9fa9459Szrj }
88*a9fa9459Szrj
89*a9fa9459Szrj return (struct bfd_hash_entry *) ret;
90*a9fa9459Szrj }
91*a9fa9459Szrj
92*a9fa9459Szrj /* Initialize a COFF linker hash table. */
93*a9fa9459Szrj
94*a9fa9459Szrj bfd_boolean
_bfd_coff_link_hash_table_init(struct coff_link_hash_table * table,bfd * abfd,struct bfd_hash_entry * (* newfunc)(struct bfd_hash_entry *,struct bfd_hash_table *,const char *),unsigned int entsize)95*a9fa9459Szrj _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
96*a9fa9459Szrj bfd *abfd,
97*a9fa9459Szrj struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
98*a9fa9459Szrj struct bfd_hash_table *,
99*a9fa9459Szrj const char *),
100*a9fa9459Szrj unsigned int entsize)
101*a9fa9459Szrj {
102*a9fa9459Szrj memset (&table->stab_info, 0, sizeof (table->stab_info));
103*a9fa9459Szrj return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
104*a9fa9459Szrj }
105*a9fa9459Szrj
106*a9fa9459Szrj /* Create a COFF linker hash table. */
107*a9fa9459Szrj
108*a9fa9459Szrj struct bfd_link_hash_table *
_bfd_coff_link_hash_table_create(bfd * abfd)109*a9fa9459Szrj _bfd_coff_link_hash_table_create (bfd *abfd)
110*a9fa9459Szrj {
111*a9fa9459Szrj struct coff_link_hash_table *ret;
112*a9fa9459Szrj bfd_size_type amt = sizeof (struct coff_link_hash_table);
113*a9fa9459Szrj
114*a9fa9459Szrj ret = (struct coff_link_hash_table *) bfd_malloc (amt);
115*a9fa9459Szrj if (ret == NULL)
116*a9fa9459Szrj return NULL;
117*a9fa9459Szrj
118*a9fa9459Szrj if (! _bfd_coff_link_hash_table_init (ret, abfd,
119*a9fa9459Szrj _bfd_coff_link_hash_newfunc,
120*a9fa9459Szrj sizeof (struct coff_link_hash_entry)))
121*a9fa9459Szrj {
122*a9fa9459Szrj free (ret);
123*a9fa9459Szrj return (struct bfd_link_hash_table *) NULL;
124*a9fa9459Szrj }
125*a9fa9459Szrj return &ret->root;
126*a9fa9459Szrj }
127*a9fa9459Szrj
128*a9fa9459Szrj /* Create an entry in a COFF debug merge hash table. */
129*a9fa9459Szrj
130*a9fa9459Szrj struct bfd_hash_entry *
_bfd_coff_debug_merge_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)131*a9fa9459Szrj _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
132*a9fa9459Szrj struct bfd_hash_table *table,
133*a9fa9459Szrj const char *string)
134*a9fa9459Szrj {
135*a9fa9459Szrj struct coff_debug_merge_hash_entry *ret =
136*a9fa9459Szrj (struct coff_debug_merge_hash_entry *) entry;
137*a9fa9459Szrj
138*a9fa9459Szrj /* Allocate the structure if it has not already been allocated by a
139*a9fa9459Szrj subclass. */
140*a9fa9459Szrj if (ret == (struct coff_debug_merge_hash_entry *) NULL)
141*a9fa9459Szrj ret = ((struct coff_debug_merge_hash_entry *)
142*a9fa9459Szrj bfd_hash_allocate (table,
143*a9fa9459Szrj sizeof (struct coff_debug_merge_hash_entry)));
144*a9fa9459Szrj if (ret == (struct coff_debug_merge_hash_entry *) NULL)
145*a9fa9459Szrj return (struct bfd_hash_entry *) ret;
146*a9fa9459Szrj
147*a9fa9459Szrj /* Call the allocation method of the superclass. */
148*a9fa9459Szrj ret = ((struct coff_debug_merge_hash_entry *)
149*a9fa9459Szrj bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
150*a9fa9459Szrj if (ret != (struct coff_debug_merge_hash_entry *) NULL)
151*a9fa9459Szrj {
152*a9fa9459Szrj /* Set local fields. */
153*a9fa9459Szrj ret->types = NULL;
154*a9fa9459Szrj }
155*a9fa9459Szrj
156*a9fa9459Szrj return (struct bfd_hash_entry *) ret;
157*a9fa9459Szrj }
158*a9fa9459Szrj
159*a9fa9459Szrj /* Given a COFF BFD, add symbols to the global hash table as
160*a9fa9459Szrj appropriate. */
161*a9fa9459Szrj
162*a9fa9459Szrj bfd_boolean
_bfd_coff_link_add_symbols(bfd * abfd,struct bfd_link_info * info)163*a9fa9459Szrj _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
164*a9fa9459Szrj {
165*a9fa9459Szrj switch (bfd_get_format (abfd))
166*a9fa9459Szrj {
167*a9fa9459Szrj case bfd_object:
168*a9fa9459Szrj return coff_link_add_object_symbols (abfd, info);
169*a9fa9459Szrj case bfd_archive:
170*a9fa9459Szrj return _bfd_generic_link_add_archive_symbols
171*a9fa9459Szrj (abfd, info, coff_link_check_archive_element);
172*a9fa9459Szrj default:
173*a9fa9459Szrj bfd_set_error (bfd_error_wrong_format);
174*a9fa9459Szrj return FALSE;
175*a9fa9459Szrj }
176*a9fa9459Szrj }
177*a9fa9459Szrj
178*a9fa9459Szrj /* Add symbols from a COFF object file. */
179*a9fa9459Szrj
180*a9fa9459Szrj static bfd_boolean
coff_link_add_object_symbols(bfd * abfd,struct bfd_link_info * info)181*a9fa9459Szrj coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
182*a9fa9459Szrj {
183*a9fa9459Szrj if (! _bfd_coff_get_external_symbols (abfd))
184*a9fa9459Szrj return FALSE;
185*a9fa9459Szrj if (! coff_link_add_symbols (abfd, info))
186*a9fa9459Szrj return FALSE;
187*a9fa9459Szrj
188*a9fa9459Szrj if (! info->keep_memory
189*a9fa9459Szrj && ! _bfd_coff_free_symbols (abfd))
190*a9fa9459Szrj return FALSE;
191*a9fa9459Szrj
192*a9fa9459Szrj return TRUE;
193*a9fa9459Szrj }
194*a9fa9459Szrj
195*a9fa9459Szrj /* Check a single archive element to see if we need to include it in
196*a9fa9459Szrj the link. *PNEEDED is set according to whether this element is
197*a9fa9459Szrj needed in the link or not. This is called via
198*a9fa9459Szrj _bfd_generic_link_add_archive_symbols. */
199*a9fa9459Szrj
200*a9fa9459Szrj static bfd_boolean
coff_link_check_archive_element(bfd * abfd,struct bfd_link_info * info,struct bfd_link_hash_entry * h,const char * name,bfd_boolean * pneeded)201*a9fa9459Szrj coff_link_check_archive_element (bfd *abfd,
202*a9fa9459Szrj struct bfd_link_info *info,
203*a9fa9459Szrj struct bfd_link_hash_entry *h,
204*a9fa9459Szrj const char *name,
205*a9fa9459Szrj bfd_boolean *pneeded)
206*a9fa9459Szrj {
207*a9fa9459Szrj *pneeded = FALSE;
208*a9fa9459Szrj
209*a9fa9459Szrj /* We are only interested in symbols that are currently undefined.
210*a9fa9459Szrj If a symbol is currently known to be common, COFF linkers do not
211*a9fa9459Szrj bring in an object file which defines it. */
212*a9fa9459Szrj if (h->type != bfd_link_hash_undefined)
213*a9fa9459Szrj return TRUE;
214*a9fa9459Szrj
215*a9fa9459Szrj /* Include this element? */
216*a9fa9459Szrj if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
217*a9fa9459Szrj return TRUE;
218*a9fa9459Szrj *pneeded = TRUE;
219*a9fa9459Szrj
220*a9fa9459Szrj return coff_link_add_object_symbols (abfd, info);
221*a9fa9459Szrj }
222*a9fa9459Szrj
223*a9fa9459Szrj /* Add all the symbols from an object file to the hash table. */
224*a9fa9459Szrj
225*a9fa9459Szrj static bfd_boolean
coff_link_add_symbols(bfd * abfd,struct bfd_link_info * info)226*a9fa9459Szrj coff_link_add_symbols (bfd *abfd,
227*a9fa9459Szrj struct bfd_link_info *info)
228*a9fa9459Szrj {
229*a9fa9459Szrj unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
230*a9fa9459Szrj unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
231*a9fa9459Szrj unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
232*a9fa9459Szrj bfd_boolean keep_syms;
233*a9fa9459Szrj bfd_boolean default_copy;
234*a9fa9459Szrj bfd_size_type symcount;
235*a9fa9459Szrj struct coff_link_hash_entry **sym_hash;
236*a9fa9459Szrj bfd_size_type symesz;
237*a9fa9459Szrj bfd_byte *esym;
238*a9fa9459Szrj bfd_byte *esym_end;
239*a9fa9459Szrj bfd_size_type amt;
240*a9fa9459Szrj
241*a9fa9459Szrj symcount = obj_raw_syment_count (abfd);
242*a9fa9459Szrj
243*a9fa9459Szrj if (symcount == 0)
244*a9fa9459Szrj return TRUE; /* Nothing to do. */
245*a9fa9459Szrj
246*a9fa9459Szrj /* Keep the symbols during this function, in case the linker needs
247*a9fa9459Szrj to read the generic symbols in order to report an error message. */
248*a9fa9459Szrj keep_syms = obj_coff_keep_syms (abfd);
249*a9fa9459Szrj obj_coff_keep_syms (abfd) = TRUE;
250*a9fa9459Szrj
251*a9fa9459Szrj if (info->keep_memory)
252*a9fa9459Szrj default_copy = FALSE;
253*a9fa9459Szrj else
254*a9fa9459Szrj default_copy = TRUE;
255*a9fa9459Szrj
256*a9fa9459Szrj /* We keep a list of the linker hash table entries that correspond
257*a9fa9459Szrj to particular symbols. */
258*a9fa9459Szrj amt = symcount * sizeof (struct coff_link_hash_entry *);
259*a9fa9459Szrj sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
260*a9fa9459Szrj if (sym_hash == NULL)
261*a9fa9459Szrj goto error_return;
262*a9fa9459Szrj obj_coff_sym_hashes (abfd) = sym_hash;
263*a9fa9459Szrj
264*a9fa9459Szrj symesz = bfd_coff_symesz (abfd);
265*a9fa9459Szrj BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
266*a9fa9459Szrj esym = (bfd_byte *) obj_coff_external_syms (abfd);
267*a9fa9459Szrj esym_end = esym + symcount * symesz;
268*a9fa9459Szrj while (esym < esym_end)
269*a9fa9459Szrj {
270*a9fa9459Szrj struct internal_syment sym;
271*a9fa9459Szrj enum coff_symbol_classification classification;
272*a9fa9459Szrj bfd_boolean copy;
273*a9fa9459Szrj
274*a9fa9459Szrj bfd_coff_swap_sym_in (abfd, esym, &sym);
275*a9fa9459Szrj
276*a9fa9459Szrj classification = bfd_coff_classify_symbol (abfd, &sym);
277*a9fa9459Szrj if (classification != COFF_SYMBOL_LOCAL)
278*a9fa9459Szrj {
279*a9fa9459Szrj const char *name;
280*a9fa9459Szrj char buf[SYMNMLEN + 1];
281*a9fa9459Szrj flagword flags;
282*a9fa9459Szrj asection *section;
283*a9fa9459Szrj bfd_vma value;
284*a9fa9459Szrj bfd_boolean addit;
285*a9fa9459Szrj
286*a9fa9459Szrj /* This symbol is externally visible. */
287*a9fa9459Szrj
288*a9fa9459Szrj name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
289*a9fa9459Szrj if (name == NULL)
290*a9fa9459Szrj goto error_return;
291*a9fa9459Szrj
292*a9fa9459Szrj /* We must copy the name into memory if we got it from the
293*a9fa9459Szrj syment itself, rather than the string table. */
294*a9fa9459Szrj copy = default_copy;
295*a9fa9459Szrj if (sym._n._n_n._n_zeroes != 0
296*a9fa9459Szrj || sym._n._n_n._n_offset == 0)
297*a9fa9459Szrj copy = TRUE;
298*a9fa9459Szrj
299*a9fa9459Szrj value = sym.n_value;
300*a9fa9459Szrj
301*a9fa9459Szrj switch (classification)
302*a9fa9459Szrj {
303*a9fa9459Szrj default:
304*a9fa9459Szrj abort ();
305*a9fa9459Szrj
306*a9fa9459Szrj case COFF_SYMBOL_GLOBAL:
307*a9fa9459Szrj flags = BSF_EXPORT | BSF_GLOBAL;
308*a9fa9459Szrj section = coff_section_from_bfd_index (abfd, sym.n_scnum);
309*a9fa9459Szrj if (! obj_pe (abfd))
310*a9fa9459Szrj value -= section->vma;
311*a9fa9459Szrj break;
312*a9fa9459Szrj
313*a9fa9459Szrj case COFF_SYMBOL_UNDEFINED:
314*a9fa9459Szrj flags = 0;
315*a9fa9459Szrj section = bfd_und_section_ptr;
316*a9fa9459Szrj break;
317*a9fa9459Szrj
318*a9fa9459Szrj case COFF_SYMBOL_COMMON:
319*a9fa9459Szrj flags = BSF_GLOBAL;
320*a9fa9459Szrj section = bfd_com_section_ptr;
321*a9fa9459Szrj break;
322*a9fa9459Szrj
323*a9fa9459Szrj case COFF_SYMBOL_PE_SECTION:
324*a9fa9459Szrj flags = BSF_SECTION_SYM | BSF_GLOBAL;
325*a9fa9459Szrj section = coff_section_from_bfd_index (abfd, sym.n_scnum);
326*a9fa9459Szrj break;
327*a9fa9459Szrj }
328*a9fa9459Szrj
329*a9fa9459Szrj if (IS_WEAK_EXTERNAL (abfd, sym))
330*a9fa9459Szrj flags = BSF_WEAK;
331*a9fa9459Szrj
332*a9fa9459Szrj addit = TRUE;
333*a9fa9459Szrj
334*a9fa9459Szrj /* In the PE format, section symbols actually refer to the
335*a9fa9459Szrj start of the output section. We handle them specially
336*a9fa9459Szrj here. */
337*a9fa9459Szrj if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
338*a9fa9459Szrj {
339*a9fa9459Szrj *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
340*a9fa9459Szrj name, FALSE, copy, FALSE);
341*a9fa9459Szrj if (*sym_hash != NULL)
342*a9fa9459Szrj {
343*a9fa9459Szrj if (((*sym_hash)->coff_link_hash_flags
344*a9fa9459Szrj & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
345*a9fa9459Szrj && (*sym_hash)->root.type != bfd_link_hash_undefined
346*a9fa9459Szrj && (*sym_hash)->root.type != bfd_link_hash_undefweak)
347*a9fa9459Szrj (*_bfd_error_handler)
348*a9fa9459Szrj ("Warning: symbol `%s' is both section and non-section",
349*a9fa9459Szrj name);
350*a9fa9459Szrj
351*a9fa9459Szrj addit = FALSE;
352*a9fa9459Szrj }
353*a9fa9459Szrj }
354*a9fa9459Szrj
355*a9fa9459Szrj /* The Microsoft Visual C compiler does string pooling by
356*a9fa9459Szrj hashing the constants to an internal symbol name, and
357*a9fa9459Szrj relying on the linker comdat support to discard
358*a9fa9459Szrj duplicate names. However, if one string is a literal and
359*a9fa9459Szrj one is a data initializer, one will end up in the .data
360*a9fa9459Szrj section and one will end up in the .rdata section. The
361*a9fa9459Szrj Microsoft linker will combine them into the .data
362*a9fa9459Szrj section, which seems to be wrong since it might cause the
363*a9fa9459Szrj literal to change.
364*a9fa9459Szrj
365*a9fa9459Szrj As long as there are no external references to the
366*a9fa9459Szrj symbols, which there shouldn't be, we can treat the .data
367*a9fa9459Szrj and .rdata instances as separate symbols. The comdat
368*a9fa9459Szrj code in the linker will do the appropriate merging. Here
369*a9fa9459Szrj we avoid getting a multiple definition error for one of
370*a9fa9459Szrj these special symbols.
371*a9fa9459Szrj
372*a9fa9459Szrj FIXME: I don't think this will work in the case where
373*a9fa9459Szrj there are two object files which use the constants as a
374*a9fa9459Szrj literal and two object files which use it as a data
375*a9fa9459Szrj initializer. One or the other of the second object files
376*a9fa9459Szrj is going to wind up with an inappropriate reference. */
377*a9fa9459Szrj if (obj_pe (abfd)
378*a9fa9459Szrj && (classification == COFF_SYMBOL_GLOBAL
379*a9fa9459Szrj || classification == COFF_SYMBOL_PE_SECTION)
380*a9fa9459Szrj && coff_section_data (abfd, section) != NULL
381*a9fa9459Szrj && coff_section_data (abfd, section)->comdat != NULL
382*a9fa9459Szrj && CONST_STRNEQ (name, "??_")
383*a9fa9459Szrj && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
384*a9fa9459Szrj {
385*a9fa9459Szrj if (*sym_hash == NULL)
386*a9fa9459Szrj *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
387*a9fa9459Szrj name, FALSE, copy, FALSE);
388*a9fa9459Szrj if (*sym_hash != NULL
389*a9fa9459Szrj && (*sym_hash)->root.type == bfd_link_hash_defined
390*a9fa9459Szrj && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
391*a9fa9459Szrj && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
392*a9fa9459Szrj coff_section_data (abfd, section)->comdat->name) == 0)
393*a9fa9459Szrj addit = FALSE;
394*a9fa9459Szrj }
395*a9fa9459Szrj
396*a9fa9459Szrj if (addit)
397*a9fa9459Szrj {
398*a9fa9459Szrj if (! (bfd_coff_link_add_one_symbol
399*a9fa9459Szrj (info, abfd, name, flags, section, value,
400*a9fa9459Szrj (const char *) NULL, copy, FALSE,
401*a9fa9459Szrj (struct bfd_link_hash_entry **) sym_hash)))
402*a9fa9459Szrj goto error_return;
403*a9fa9459Szrj }
404*a9fa9459Szrj
405*a9fa9459Szrj if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
406*a9fa9459Szrj (*sym_hash)->coff_link_hash_flags |=
407*a9fa9459Szrj COFF_LINK_HASH_PE_SECTION_SYMBOL;
408*a9fa9459Szrj
409*a9fa9459Szrj /* Limit the alignment of a common symbol to the possible
410*a9fa9459Szrj alignment of a section. There is no point to permitting
411*a9fa9459Szrj a higher alignment for a common symbol: we can not
412*a9fa9459Szrj guarantee it, and it may cause us to allocate extra space
413*a9fa9459Szrj in the common section. */
414*a9fa9459Szrj if (section == bfd_com_section_ptr
415*a9fa9459Szrj && (*sym_hash)->root.type == bfd_link_hash_common
416*a9fa9459Szrj && ((*sym_hash)->root.u.c.p->alignment_power
417*a9fa9459Szrj > bfd_coff_default_section_alignment_power (abfd)))
418*a9fa9459Szrj (*sym_hash)->root.u.c.p->alignment_power
419*a9fa9459Szrj = bfd_coff_default_section_alignment_power (abfd);
420*a9fa9459Szrj
421*a9fa9459Szrj if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
422*a9fa9459Szrj {
423*a9fa9459Szrj /* If we don't have any symbol information currently in
424*a9fa9459Szrj the hash table, or if we are looking at a symbol
425*a9fa9459Szrj definition, then update the symbol class and type in
426*a9fa9459Szrj the hash table. */
427*a9fa9459Szrj if (((*sym_hash)->symbol_class == C_NULL
428*a9fa9459Szrj && (*sym_hash)->type == T_NULL)
429*a9fa9459Szrj || sym.n_scnum != 0
430*a9fa9459Szrj || (sym.n_value != 0
431*a9fa9459Szrj && (*sym_hash)->root.type != bfd_link_hash_defined
432*a9fa9459Szrj && (*sym_hash)->root.type != bfd_link_hash_defweak))
433*a9fa9459Szrj {
434*a9fa9459Szrj (*sym_hash)->symbol_class = sym.n_sclass;
435*a9fa9459Szrj if (sym.n_type != T_NULL)
436*a9fa9459Szrj {
437*a9fa9459Szrj /* We want to warn if the type changed, but not
438*a9fa9459Szrj if it changed from an unspecified type.
439*a9fa9459Szrj Testing the whole type byte may work, but the
440*a9fa9459Szrj change from (e.g.) a function of unspecified
441*a9fa9459Szrj type to function of known type also wants to
442*a9fa9459Szrj skip the warning. */
443*a9fa9459Szrj if ((*sym_hash)->type != T_NULL
444*a9fa9459Szrj && (*sym_hash)->type != sym.n_type
445*a9fa9459Szrj && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
446*a9fa9459Szrj && (BTYPE ((*sym_hash)->type) == T_NULL
447*a9fa9459Szrj || BTYPE (sym.n_type) == T_NULL)))
448*a9fa9459Szrj (*_bfd_error_handler)
449*a9fa9459Szrj (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
450*a9fa9459Szrj abfd, name, (*sym_hash)->type, sym.n_type);
451*a9fa9459Szrj
452*a9fa9459Szrj /* We don't want to change from a meaningful
453*a9fa9459Szrj base type to a null one, but if we know
454*a9fa9459Szrj nothing, take what little we might now know. */
455*a9fa9459Szrj if (BTYPE (sym.n_type) != T_NULL
456*a9fa9459Szrj || (*sym_hash)->type == T_NULL)
457*a9fa9459Szrj (*sym_hash)->type = sym.n_type;
458*a9fa9459Szrj }
459*a9fa9459Szrj (*sym_hash)->auxbfd = abfd;
460*a9fa9459Szrj if (sym.n_numaux != 0)
461*a9fa9459Szrj {
462*a9fa9459Szrj union internal_auxent *alloc;
463*a9fa9459Szrj unsigned int i;
464*a9fa9459Szrj bfd_byte *eaux;
465*a9fa9459Szrj union internal_auxent *iaux;
466*a9fa9459Szrj
467*a9fa9459Szrj (*sym_hash)->numaux = sym.n_numaux;
468*a9fa9459Szrj alloc = ((union internal_auxent *)
469*a9fa9459Szrj bfd_hash_allocate (&info->hash->table,
470*a9fa9459Szrj (sym.n_numaux
471*a9fa9459Szrj * sizeof (*alloc))));
472*a9fa9459Szrj if (alloc == NULL)
473*a9fa9459Szrj goto error_return;
474*a9fa9459Szrj for (i = 0, eaux = esym + symesz, iaux = alloc;
475*a9fa9459Szrj i < sym.n_numaux;
476*a9fa9459Szrj i++, eaux += symesz, iaux++)
477*a9fa9459Szrj bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
478*a9fa9459Szrj sym.n_sclass, (int) i,
479*a9fa9459Szrj sym.n_numaux, iaux);
480*a9fa9459Szrj (*sym_hash)->aux = alloc;
481*a9fa9459Szrj }
482*a9fa9459Szrj }
483*a9fa9459Szrj }
484*a9fa9459Szrj
485*a9fa9459Szrj if (classification == COFF_SYMBOL_PE_SECTION
486*a9fa9459Szrj && (*sym_hash)->numaux != 0)
487*a9fa9459Szrj {
488*a9fa9459Szrj /* Some PE sections (such as .bss) have a zero size in
489*a9fa9459Szrj the section header, but a non-zero size in the AUX
490*a9fa9459Szrj record. Correct that here.
491*a9fa9459Szrj
492*a9fa9459Szrj FIXME: This is not at all the right place to do this.
493*a9fa9459Szrj For example, it won't help objdump. This needs to be
494*a9fa9459Szrj done when we swap in the section header. */
495*a9fa9459Szrj BFD_ASSERT ((*sym_hash)->numaux == 1);
496*a9fa9459Szrj if (section->size == 0)
497*a9fa9459Szrj section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
498*a9fa9459Szrj
499*a9fa9459Szrj /* FIXME: We could test whether the section sizes
500*a9fa9459Szrj matches the size in the aux entry, but apparently
501*a9fa9459Szrj that sometimes fails unexpectedly. */
502*a9fa9459Szrj }
503*a9fa9459Szrj }
504*a9fa9459Szrj
505*a9fa9459Szrj esym += (sym.n_numaux + 1) * symesz;
506*a9fa9459Szrj sym_hash += sym.n_numaux + 1;
507*a9fa9459Szrj }
508*a9fa9459Szrj
509*a9fa9459Szrj /* If this is a non-traditional, non-relocatable link, try to
510*a9fa9459Szrj optimize the handling of any .stab/.stabstr sections. */
511*a9fa9459Szrj if (! bfd_link_relocatable (info)
512*a9fa9459Szrj && ! info->traditional_format
513*a9fa9459Szrj && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
514*a9fa9459Szrj && (info->strip != strip_all && info->strip != strip_debugger))
515*a9fa9459Szrj {
516*a9fa9459Szrj asection *stabstr;
517*a9fa9459Szrj
518*a9fa9459Szrj stabstr = bfd_get_section_by_name (abfd, ".stabstr");
519*a9fa9459Szrj
520*a9fa9459Szrj if (stabstr != NULL)
521*a9fa9459Szrj {
522*a9fa9459Szrj bfd_size_type string_offset = 0;
523*a9fa9459Szrj asection *stab;
524*a9fa9459Szrj
525*a9fa9459Szrj for (stab = abfd->sections; stab; stab = stab->next)
526*a9fa9459Szrj if (CONST_STRNEQ (stab->name, ".stab")
527*a9fa9459Szrj && (!stab->name[5]
528*a9fa9459Szrj || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
529*a9fa9459Szrj {
530*a9fa9459Szrj struct coff_link_hash_table *table;
531*a9fa9459Szrj struct coff_section_tdata *secdata
532*a9fa9459Szrj = coff_section_data (abfd, stab);
533*a9fa9459Szrj
534*a9fa9459Szrj if (secdata == NULL)
535*a9fa9459Szrj {
536*a9fa9459Szrj amt = sizeof (struct coff_section_tdata);
537*a9fa9459Szrj stab->used_by_bfd = bfd_zalloc (abfd, amt);
538*a9fa9459Szrj if (stab->used_by_bfd == NULL)
539*a9fa9459Szrj goto error_return;
540*a9fa9459Szrj secdata = coff_section_data (abfd, stab);
541*a9fa9459Szrj }
542*a9fa9459Szrj
543*a9fa9459Szrj table = coff_hash_table (info);
544*a9fa9459Szrj
545*a9fa9459Szrj if (! _bfd_link_section_stabs (abfd, &table->stab_info,
546*a9fa9459Szrj stab, stabstr,
547*a9fa9459Szrj &secdata->stab_info,
548*a9fa9459Szrj &string_offset))
549*a9fa9459Szrj goto error_return;
550*a9fa9459Szrj }
551*a9fa9459Szrj }
552*a9fa9459Szrj }
553*a9fa9459Szrj
554*a9fa9459Szrj obj_coff_keep_syms (abfd) = keep_syms;
555*a9fa9459Szrj
556*a9fa9459Szrj return TRUE;
557*a9fa9459Szrj
558*a9fa9459Szrj error_return:
559*a9fa9459Szrj obj_coff_keep_syms (abfd) = keep_syms;
560*a9fa9459Szrj return FALSE;
561*a9fa9459Szrj }
562*a9fa9459Szrj
563*a9fa9459Szrj /* Do the final link step. */
564*a9fa9459Szrj
565*a9fa9459Szrj bfd_boolean
_bfd_coff_final_link(bfd * abfd,struct bfd_link_info * info)566*a9fa9459Szrj _bfd_coff_final_link (bfd *abfd,
567*a9fa9459Szrj struct bfd_link_info *info)
568*a9fa9459Szrj {
569*a9fa9459Szrj bfd_size_type symesz;
570*a9fa9459Szrj struct coff_final_link_info flaginfo;
571*a9fa9459Szrj bfd_boolean debug_merge_allocated;
572*a9fa9459Szrj bfd_boolean long_section_names;
573*a9fa9459Szrj asection *o;
574*a9fa9459Szrj struct bfd_link_order *p;
575*a9fa9459Szrj bfd_size_type max_sym_count;
576*a9fa9459Szrj bfd_size_type max_lineno_count;
577*a9fa9459Szrj bfd_size_type max_reloc_count;
578*a9fa9459Szrj bfd_size_type max_output_reloc_count;
579*a9fa9459Szrj bfd_size_type max_contents_size;
580*a9fa9459Szrj file_ptr rel_filepos;
581*a9fa9459Szrj unsigned int relsz;
582*a9fa9459Szrj file_ptr line_filepos;
583*a9fa9459Szrj unsigned int linesz;
584*a9fa9459Szrj bfd *sub;
585*a9fa9459Szrj bfd_byte *external_relocs = NULL;
586*a9fa9459Szrj char strbuf[STRING_SIZE_SIZE];
587*a9fa9459Szrj bfd_size_type amt;
588*a9fa9459Szrj
589*a9fa9459Szrj symesz = bfd_coff_symesz (abfd);
590*a9fa9459Szrj
591*a9fa9459Szrj flaginfo.info = info;
592*a9fa9459Szrj flaginfo.output_bfd = abfd;
593*a9fa9459Szrj flaginfo.strtab = NULL;
594*a9fa9459Szrj flaginfo.section_info = NULL;
595*a9fa9459Szrj flaginfo.last_file_index = -1;
596*a9fa9459Szrj flaginfo.last_bf_index = -1;
597*a9fa9459Szrj flaginfo.internal_syms = NULL;
598*a9fa9459Szrj flaginfo.sec_ptrs = NULL;
599*a9fa9459Szrj flaginfo.sym_indices = NULL;
600*a9fa9459Szrj flaginfo.outsyms = NULL;
601*a9fa9459Szrj flaginfo.linenos = NULL;
602*a9fa9459Szrj flaginfo.contents = NULL;
603*a9fa9459Szrj flaginfo.external_relocs = NULL;
604*a9fa9459Szrj flaginfo.internal_relocs = NULL;
605*a9fa9459Szrj flaginfo.global_to_static = FALSE;
606*a9fa9459Szrj debug_merge_allocated = FALSE;
607*a9fa9459Szrj
608*a9fa9459Szrj coff_data (abfd)->link_info = info;
609*a9fa9459Szrj
610*a9fa9459Szrj flaginfo.strtab = _bfd_stringtab_init ();
611*a9fa9459Szrj if (flaginfo.strtab == NULL)
612*a9fa9459Szrj goto error_return;
613*a9fa9459Szrj
614*a9fa9459Szrj if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
615*a9fa9459Szrj goto error_return;
616*a9fa9459Szrj debug_merge_allocated = TRUE;
617*a9fa9459Szrj
618*a9fa9459Szrj /* Compute the file positions for all the sections. */
619*a9fa9459Szrj if (! abfd->output_has_begun)
620*a9fa9459Szrj {
621*a9fa9459Szrj if (! bfd_coff_compute_section_file_positions (abfd))
622*a9fa9459Szrj goto error_return;
623*a9fa9459Szrj }
624*a9fa9459Szrj
625*a9fa9459Szrj /* Count the line numbers and relocation entries required for the
626*a9fa9459Szrj output file. Set the file positions for the relocs. */
627*a9fa9459Szrj rel_filepos = obj_relocbase (abfd);
628*a9fa9459Szrj relsz = bfd_coff_relsz (abfd);
629*a9fa9459Szrj max_contents_size = 0;
630*a9fa9459Szrj max_lineno_count = 0;
631*a9fa9459Szrj max_reloc_count = 0;
632*a9fa9459Szrj
633*a9fa9459Szrj long_section_names = FALSE;
634*a9fa9459Szrj for (o = abfd->sections; o != NULL; o = o->next)
635*a9fa9459Szrj {
636*a9fa9459Szrj o->reloc_count = 0;
637*a9fa9459Szrj o->lineno_count = 0;
638*a9fa9459Szrj for (p = o->map_head.link_order; p != NULL; p = p->next)
639*a9fa9459Szrj {
640*a9fa9459Szrj if (p->type == bfd_indirect_link_order)
641*a9fa9459Szrj {
642*a9fa9459Szrj asection *sec;
643*a9fa9459Szrj
644*a9fa9459Szrj sec = p->u.indirect.section;
645*a9fa9459Szrj
646*a9fa9459Szrj /* Mark all sections which are to be included in the
647*a9fa9459Szrj link. This will normally be every section. We need
648*a9fa9459Szrj to do this so that we can identify any sections which
649*a9fa9459Szrj the linker has decided to not include. */
650*a9fa9459Szrj sec->linker_mark = TRUE;
651*a9fa9459Szrj
652*a9fa9459Szrj if (info->strip == strip_none
653*a9fa9459Szrj || info->strip == strip_some)
654*a9fa9459Szrj o->lineno_count += sec->lineno_count;
655*a9fa9459Szrj
656*a9fa9459Szrj if (bfd_link_relocatable (info))
657*a9fa9459Szrj o->reloc_count += sec->reloc_count;
658*a9fa9459Szrj
659*a9fa9459Szrj if (sec->rawsize > max_contents_size)
660*a9fa9459Szrj max_contents_size = sec->rawsize;
661*a9fa9459Szrj if (sec->size > max_contents_size)
662*a9fa9459Szrj max_contents_size = sec->size;
663*a9fa9459Szrj if (sec->lineno_count > max_lineno_count)
664*a9fa9459Szrj max_lineno_count = sec->lineno_count;
665*a9fa9459Szrj if (sec->reloc_count > max_reloc_count)
666*a9fa9459Szrj max_reloc_count = sec->reloc_count;
667*a9fa9459Szrj }
668*a9fa9459Szrj else if (bfd_link_relocatable (info)
669*a9fa9459Szrj && (p->type == bfd_section_reloc_link_order
670*a9fa9459Szrj || p->type == bfd_symbol_reloc_link_order))
671*a9fa9459Szrj ++o->reloc_count;
672*a9fa9459Szrj }
673*a9fa9459Szrj if (o->reloc_count == 0)
674*a9fa9459Szrj o->rel_filepos = 0;
675*a9fa9459Szrj else
676*a9fa9459Szrj {
677*a9fa9459Szrj o->flags |= SEC_RELOC;
678*a9fa9459Szrj o->rel_filepos = rel_filepos;
679*a9fa9459Szrj rel_filepos += o->reloc_count * relsz;
680*a9fa9459Szrj /* In PE COFF, if there are at least 0xffff relocations an
681*a9fa9459Szrj extra relocation will be written out to encode the count. */
682*a9fa9459Szrj if (obj_pe (abfd) && o->reloc_count >= 0xffff)
683*a9fa9459Szrj rel_filepos += relsz;
684*a9fa9459Szrj }
685*a9fa9459Szrj
686*a9fa9459Szrj if (bfd_coff_long_section_names (abfd)
687*a9fa9459Szrj && strlen (o->name) > SCNNMLEN)
688*a9fa9459Szrj {
689*a9fa9459Szrj /* This section has a long name which must go in the string
690*a9fa9459Szrj table. This must correspond to the code in
691*a9fa9459Szrj coff_write_object_contents which puts the string index
692*a9fa9459Szrj into the s_name field of the section header. That is why
693*a9fa9459Szrj we pass hash as FALSE. */
694*a9fa9459Szrj if (_bfd_stringtab_add (flaginfo.strtab, o->name, FALSE, FALSE)
695*a9fa9459Szrj == (bfd_size_type) -1)
696*a9fa9459Szrj goto error_return;
697*a9fa9459Szrj long_section_names = TRUE;
698*a9fa9459Szrj }
699*a9fa9459Szrj }
700*a9fa9459Szrj
701*a9fa9459Szrj /* If doing a relocatable link, allocate space for the pointers we
702*a9fa9459Szrj need to keep. */
703*a9fa9459Szrj if (bfd_link_relocatable (info))
704*a9fa9459Szrj {
705*a9fa9459Szrj unsigned int i;
706*a9fa9459Szrj
707*a9fa9459Szrj /* We use section_count + 1, rather than section_count, because
708*a9fa9459Szrj the target_index fields are 1 based. */
709*a9fa9459Szrj amt = abfd->section_count + 1;
710*a9fa9459Szrj amt *= sizeof (struct coff_link_section_info);
711*a9fa9459Szrj flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
712*a9fa9459Szrj if (flaginfo.section_info == NULL)
713*a9fa9459Szrj goto error_return;
714*a9fa9459Szrj for (i = 0; i <= abfd->section_count; i++)
715*a9fa9459Szrj {
716*a9fa9459Szrj flaginfo.section_info[i].relocs = NULL;
717*a9fa9459Szrj flaginfo.section_info[i].rel_hashes = NULL;
718*a9fa9459Szrj }
719*a9fa9459Szrj }
720*a9fa9459Szrj
721*a9fa9459Szrj /* We now know the size of the relocs, so we can determine the file
722*a9fa9459Szrj positions of the line numbers. */
723*a9fa9459Szrj line_filepos = rel_filepos;
724*a9fa9459Szrj linesz = bfd_coff_linesz (abfd);
725*a9fa9459Szrj max_output_reloc_count = 0;
726*a9fa9459Szrj for (o = abfd->sections; o != NULL; o = o->next)
727*a9fa9459Szrj {
728*a9fa9459Szrj if (o->lineno_count == 0)
729*a9fa9459Szrj o->line_filepos = 0;
730*a9fa9459Szrj else
731*a9fa9459Szrj {
732*a9fa9459Szrj o->line_filepos = line_filepos;
733*a9fa9459Szrj line_filepos += o->lineno_count * linesz;
734*a9fa9459Szrj }
735*a9fa9459Szrj
736*a9fa9459Szrj if (o->reloc_count != 0)
737*a9fa9459Szrj {
738*a9fa9459Szrj /* We don't know the indices of global symbols until we have
739*a9fa9459Szrj written out all the local symbols. For each section in
740*a9fa9459Szrj the output file, we keep an array of pointers to hash
741*a9fa9459Szrj table entries. Each entry in the array corresponds to a
742*a9fa9459Szrj reloc. When we find a reloc against a global symbol, we
743*a9fa9459Szrj set the corresponding entry in this array so that we can
744*a9fa9459Szrj fix up the symbol index after we have written out all the
745*a9fa9459Szrj local symbols.
746*a9fa9459Szrj
747*a9fa9459Szrj Because of this problem, we also keep the relocs in
748*a9fa9459Szrj memory until the end of the link. This wastes memory,
749*a9fa9459Szrj but only when doing a relocatable link, which is not the
750*a9fa9459Szrj common case. */
751*a9fa9459Szrj BFD_ASSERT (bfd_link_relocatable (info));
752*a9fa9459Szrj amt = o->reloc_count;
753*a9fa9459Szrj amt *= sizeof (struct internal_reloc);
754*a9fa9459Szrj flaginfo.section_info[o->target_index].relocs =
755*a9fa9459Szrj (struct internal_reloc *) bfd_malloc (amt);
756*a9fa9459Szrj amt = o->reloc_count;
757*a9fa9459Szrj amt *= sizeof (struct coff_link_hash_entry *);
758*a9fa9459Szrj flaginfo.section_info[o->target_index].rel_hashes =
759*a9fa9459Szrj (struct coff_link_hash_entry **) bfd_malloc (amt);
760*a9fa9459Szrj if (flaginfo.section_info[o->target_index].relocs == NULL
761*a9fa9459Szrj || flaginfo.section_info[o->target_index].rel_hashes == NULL)
762*a9fa9459Szrj goto error_return;
763*a9fa9459Szrj
764*a9fa9459Szrj if (o->reloc_count > max_output_reloc_count)
765*a9fa9459Szrj max_output_reloc_count = o->reloc_count;
766*a9fa9459Szrj }
767*a9fa9459Szrj
768*a9fa9459Szrj /* Reset the reloc and lineno counts, so that we can use them to
769*a9fa9459Szrj count the number of entries we have output so far. */
770*a9fa9459Szrj o->reloc_count = 0;
771*a9fa9459Szrj o->lineno_count = 0;
772*a9fa9459Szrj }
773*a9fa9459Szrj
774*a9fa9459Szrj obj_sym_filepos (abfd) = line_filepos;
775*a9fa9459Szrj
776*a9fa9459Szrj /* Figure out the largest number of symbols in an input BFD. Take
777*a9fa9459Szrj the opportunity to clear the output_has_begun fields of all the
778*a9fa9459Szrj input BFD's. */
779*a9fa9459Szrj max_sym_count = 0;
780*a9fa9459Szrj for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
781*a9fa9459Szrj {
782*a9fa9459Szrj size_t sz;
783*a9fa9459Szrj
784*a9fa9459Szrj sub->output_has_begun = FALSE;
785*a9fa9459Szrj sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
786*a9fa9459Szrj if (sz > max_sym_count)
787*a9fa9459Szrj max_sym_count = sz;
788*a9fa9459Szrj }
789*a9fa9459Szrj
790*a9fa9459Szrj /* Allocate some buffers used while linking. */
791*a9fa9459Szrj amt = max_sym_count * sizeof (struct internal_syment);
792*a9fa9459Szrj flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
793*a9fa9459Szrj amt = max_sym_count * sizeof (asection *);
794*a9fa9459Szrj flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
795*a9fa9459Szrj amt = max_sym_count * sizeof (long);
796*a9fa9459Szrj flaginfo.sym_indices = (long int *) bfd_malloc (amt);
797*a9fa9459Szrj flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
798*a9fa9459Szrj amt = max_lineno_count * bfd_coff_linesz (abfd);
799*a9fa9459Szrj flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
800*a9fa9459Szrj flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
801*a9fa9459Szrj amt = max_reloc_count * relsz;
802*a9fa9459Szrj flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
803*a9fa9459Szrj if (! bfd_link_relocatable (info))
804*a9fa9459Szrj {
805*a9fa9459Szrj amt = max_reloc_count * sizeof (struct internal_reloc);
806*a9fa9459Szrj flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
807*a9fa9459Szrj }
808*a9fa9459Szrj if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
809*a9fa9459Szrj || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
810*a9fa9459Szrj || (flaginfo.sym_indices == NULL && max_sym_count > 0)
811*a9fa9459Szrj || flaginfo.outsyms == NULL
812*a9fa9459Szrj || (flaginfo.linenos == NULL && max_lineno_count > 0)
813*a9fa9459Szrj || (flaginfo.contents == NULL && max_contents_size > 0)
814*a9fa9459Szrj || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
815*a9fa9459Szrj || (! bfd_link_relocatable (info)
816*a9fa9459Szrj && flaginfo.internal_relocs == NULL
817*a9fa9459Szrj && max_reloc_count > 0))
818*a9fa9459Szrj goto error_return;
819*a9fa9459Szrj
820*a9fa9459Szrj /* We now know the position of everything in the file, except that
821*a9fa9459Szrj we don't know the size of the symbol table and therefore we don't
822*a9fa9459Szrj know where the string table starts. We just build the string
823*a9fa9459Szrj table in memory as we go along. We process all the relocations
824*a9fa9459Szrj for a single input file at once. */
825*a9fa9459Szrj obj_raw_syment_count (abfd) = 0;
826*a9fa9459Szrj
827*a9fa9459Szrj if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
828*a9fa9459Szrj {
829*a9fa9459Szrj if (! bfd_coff_start_final_link (abfd, info))
830*a9fa9459Szrj goto error_return;
831*a9fa9459Szrj }
832*a9fa9459Szrj
833*a9fa9459Szrj for (o = abfd->sections; o != NULL; o = o->next)
834*a9fa9459Szrj {
835*a9fa9459Szrj for (p = o->map_head.link_order; p != NULL; p = p->next)
836*a9fa9459Szrj {
837*a9fa9459Szrj if (p->type == bfd_indirect_link_order
838*a9fa9459Szrj && bfd_family_coff (p->u.indirect.section->owner))
839*a9fa9459Szrj {
840*a9fa9459Szrj sub = p->u.indirect.section->owner;
841*a9fa9459Szrj if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
842*a9fa9459Szrj {
843*a9fa9459Szrj if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
844*a9fa9459Szrj goto error_return;
845*a9fa9459Szrj sub->output_has_begun = TRUE;
846*a9fa9459Szrj }
847*a9fa9459Szrj }
848*a9fa9459Szrj else if (p->type == bfd_section_reloc_link_order
849*a9fa9459Szrj || p->type == bfd_symbol_reloc_link_order)
850*a9fa9459Szrj {
851*a9fa9459Szrj if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
852*a9fa9459Szrj goto error_return;
853*a9fa9459Szrj }
854*a9fa9459Szrj else
855*a9fa9459Szrj {
856*a9fa9459Szrj if (! _bfd_default_link_order (abfd, info, o, p))
857*a9fa9459Szrj goto error_return;
858*a9fa9459Szrj }
859*a9fa9459Szrj }
860*a9fa9459Szrj }
861*a9fa9459Szrj
862*a9fa9459Szrj if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
863*a9fa9459Szrj {
864*a9fa9459Szrj /* Add local symbols from foreign inputs. */
865*a9fa9459Szrj for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
866*a9fa9459Szrj {
867*a9fa9459Szrj unsigned int i;
868*a9fa9459Szrj
869*a9fa9459Szrj if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
870*a9fa9459Szrj continue;
871*a9fa9459Szrj for (i = 0; i < bfd_get_symcount (sub); ++i)
872*a9fa9459Szrj {
873*a9fa9459Szrj asymbol *sym = bfd_get_outsymbols (sub) [i];
874*a9fa9459Szrj file_ptr pos;
875*a9fa9459Szrj struct internal_syment isym;
876*a9fa9459Szrj union internal_auxent iaux;
877*a9fa9459Szrj bfd_size_type string_size = 0, indx;
878*a9fa9459Szrj bfd_vma written = 0;
879*a9fa9459Szrj bfd_boolean rewrite = FALSE, hash;
880*a9fa9459Szrj
881*a9fa9459Szrj if (! (sym->flags & BSF_LOCAL)
882*a9fa9459Szrj || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
883*a9fa9459Szrj | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
884*a9fa9459Szrj | BSF_SYNTHETIC))
885*a9fa9459Szrj || ((sym->flags & BSF_DEBUGGING)
886*a9fa9459Szrj && ! (sym->flags & BSF_FILE)))
887*a9fa9459Szrj continue;
888*a9fa9459Szrj
889*a9fa9459Szrj /* See if we are discarding symbols with this name. */
890*a9fa9459Szrj if ((flaginfo.info->strip == strip_some
891*a9fa9459Szrj && (bfd_hash_lookup (flaginfo.info->keep_hash,
892*a9fa9459Szrj bfd_asymbol_name(sym), FALSE, FALSE)
893*a9fa9459Szrj == NULL))
894*a9fa9459Szrj || (((flaginfo.info->discard == discard_sec_merge
895*a9fa9459Szrj && (bfd_get_section (sym)->flags & SEC_MERGE)
896*a9fa9459Szrj && ! bfd_link_relocatable (flaginfo.info))
897*a9fa9459Szrj || flaginfo.info->discard == discard_l)
898*a9fa9459Szrj && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
899*a9fa9459Szrj continue;
900*a9fa9459Szrj
901*a9fa9459Szrj pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
902*a9fa9459Szrj * symesz;
903*a9fa9459Szrj if (bfd_seek (abfd, pos, SEEK_SET) != 0)
904*a9fa9459Szrj goto error_return;
905*a9fa9459Szrj if (! coff_write_alien_symbol(abfd, sym, &isym, &iaux, &written,
906*a9fa9459Szrj &string_size, NULL, NULL))
907*a9fa9459Szrj goto error_return;
908*a9fa9459Szrj
909*a9fa9459Szrj hash = !flaginfo.info->traditional_format;
910*a9fa9459Szrj
911*a9fa9459Szrj if (string_size >= 6 && isym.n_sclass == C_FILE
912*a9fa9459Szrj && ! isym._n._n_n._n_zeroes && isym.n_numaux)
913*a9fa9459Szrj {
914*a9fa9459Szrj indx = _bfd_stringtab_add (flaginfo.strtab, ".file", hash,
915*a9fa9459Szrj FALSE);
916*a9fa9459Szrj if (indx == (bfd_size_type) -1)
917*a9fa9459Szrj goto error_return;
918*a9fa9459Szrj isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
919*a9fa9459Szrj bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
920*a9fa9459Szrj if (bfd_seek (abfd, pos, SEEK_SET) != 0
921*a9fa9459Szrj || bfd_bwrite (flaginfo.outsyms, symesz,
922*a9fa9459Szrj abfd) != symesz)
923*a9fa9459Szrj goto error_return;
924*a9fa9459Szrj string_size -= 6;
925*a9fa9459Szrj }
926*a9fa9459Szrj
927*a9fa9459Szrj if (string_size)
928*a9fa9459Szrj {
929*a9fa9459Szrj indx = _bfd_stringtab_add (flaginfo.strtab,
930*a9fa9459Szrj bfd_asymbol_name (sym), hash,
931*a9fa9459Szrj FALSE);
932*a9fa9459Szrj if (indx == (bfd_size_type) -1)
933*a9fa9459Szrj goto error_return;
934*a9fa9459Szrj if (isym.n_sclass != C_FILE)
935*a9fa9459Szrj {
936*a9fa9459Szrj isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
937*a9fa9459Szrj bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
938*a9fa9459Szrj rewrite = TRUE;
939*a9fa9459Szrj }
940*a9fa9459Szrj else
941*a9fa9459Szrj {
942*a9fa9459Szrj BFD_ASSERT (isym.n_numaux == 1);
943*a9fa9459Szrj iaux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
944*a9fa9459Szrj bfd_coff_swap_aux_out (abfd, &iaux, isym.n_type, C_FILE,
945*a9fa9459Szrj 0, 1, flaginfo.outsyms + symesz);
946*a9fa9459Szrj if (bfd_seek (abfd, pos + symesz, SEEK_SET) != 0
947*a9fa9459Szrj || bfd_bwrite (flaginfo.outsyms + symesz, symesz,
948*a9fa9459Szrj abfd) != symesz)
949*a9fa9459Szrj goto error_return;
950*a9fa9459Szrj }
951*a9fa9459Szrj }
952*a9fa9459Szrj
953*a9fa9459Szrj if (isym.n_sclass == C_FILE)
954*a9fa9459Szrj {
955*a9fa9459Szrj if (flaginfo.last_file_index != -1)
956*a9fa9459Szrj {
957*a9fa9459Szrj flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
958*a9fa9459Szrj bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
959*a9fa9459Szrj flaginfo.outsyms);
960*a9fa9459Szrj pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
961*a9fa9459Szrj * symesz;
962*a9fa9459Szrj rewrite = TRUE;
963*a9fa9459Szrj }
964*a9fa9459Szrj flaginfo.last_file_index = obj_raw_syment_count (abfd);
965*a9fa9459Szrj flaginfo.last_file = isym;
966*a9fa9459Szrj }
967*a9fa9459Szrj
968*a9fa9459Szrj if (rewrite
969*a9fa9459Szrj && (bfd_seek (abfd, pos, SEEK_SET) != 0
970*a9fa9459Szrj || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz))
971*a9fa9459Szrj goto error_return;
972*a9fa9459Szrj
973*a9fa9459Szrj obj_raw_syment_count (abfd) += written;
974*a9fa9459Szrj }
975*a9fa9459Szrj }
976*a9fa9459Szrj }
977*a9fa9459Szrj
978*a9fa9459Szrj if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
979*a9fa9459Szrj goto error_return;
980*a9fa9459Szrj
981*a9fa9459Szrj /* Free up the buffers used by _bfd_coff_link_input_bfd. */
982*a9fa9459Szrj
983*a9fa9459Szrj coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
984*a9fa9459Szrj debug_merge_allocated = FALSE;
985*a9fa9459Szrj
986*a9fa9459Szrj if (flaginfo.internal_syms != NULL)
987*a9fa9459Szrj {
988*a9fa9459Szrj free (flaginfo.internal_syms);
989*a9fa9459Szrj flaginfo.internal_syms = NULL;
990*a9fa9459Szrj }
991*a9fa9459Szrj if (flaginfo.sec_ptrs != NULL)
992*a9fa9459Szrj {
993*a9fa9459Szrj free (flaginfo.sec_ptrs);
994*a9fa9459Szrj flaginfo.sec_ptrs = NULL;
995*a9fa9459Szrj }
996*a9fa9459Szrj if (flaginfo.sym_indices != NULL)
997*a9fa9459Szrj {
998*a9fa9459Szrj free (flaginfo.sym_indices);
999*a9fa9459Szrj flaginfo.sym_indices = NULL;
1000*a9fa9459Szrj }
1001*a9fa9459Szrj if (flaginfo.linenos != NULL)
1002*a9fa9459Szrj {
1003*a9fa9459Szrj free (flaginfo.linenos);
1004*a9fa9459Szrj flaginfo.linenos = NULL;
1005*a9fa9459Szrj }
1006*a9fa9459Szrj if (flaginfo.contents != NULL)
1007*a9fa9459Szrj {
1008*a9fa9459Szrj free (flaginfo.contents);
1009*a9fa9459Szrj flaginfo.contents = NULL;
1010*a9fa9459Szrj }
1011*a9fa9459Szrj if (flaginfo.external_relocs != NULL)
1012*a9fa9459Szrj {
1013*a9fa9459Szrj free (flaginfo.external_relocs);
1014*a9fa9459Szrj flaginfo.external_relocs = NULL;
1015*a9fa9459Szrj }
1016*a9fa9459Szrj if (flaginfo.internal_relocs != NULL)
1017*a9fa9459Szrj {
1018*a9fa9459Szrj free (flaginfo.internal_relocs);
1019*a9fa9459Szrj flaginfo.internal_relocs = NULL;
1020*a9fa9459Szrj }
1021*a9fa9459Szrj
1022*a9fa9459Szrj /* The value of the last C_FILE symbol is supposed to be the symbol
1023*a9fa9459Szrj index of the first external symbol. Write it out again if
1024*a9fa9459Szrj necessary. */
1025*a9fa9459Szrj if (flaginfo.last_file_index != -1
1026*a9fa9459Szrj && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
1027*a9fa9459Szrj {
1028*a9fa9459Szrj file_ptr pos;
1029*a9fa9459Szrj
1030*a9fa9459Szrj flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
1031*a9fa9459Szrj bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
1032*a9fa9459Szrj flaginfo.outsyms);
1033*a9fa9459Szrj
1034*a9fa9459Szrj pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
1035*a9fa9459Szrj if (bfd_seek (abfd, pos, SEEK_SET) != 0
1036*a9fa9459Szrj || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
1037*a9fa9459Szrj return FALSE;
1038*a9fa9459Szrj }
1039*a9fa9459Szrj
1040*a9fa9459Szrj /* If doing task linking (ld --task-link) then make a pass through the
1041*a9fa9459Szrj global symbols, writing out any that are defined, and making them
1042*a9fa9459Szrj static. */
1043*a9fa9459Szrj if (info->task_link)
1044*a9fa9459Szrj {
1045*a9fa9459Szrj flaginfo.failed = FALSE;
1046*a9fa9459Szrj coff_link_hash_traverse (coff_hash_table (info),
1047*a9fa9459Szrj _bfd_coff_write_task_globals, &flaginfo);
1048*a9fa9459Szrj if (flaginfo.failed)
1049*a9fa9459Szrj goto error_return;
1050*a9fa9459Szrj }
1051*a9fa9459Szrj
1052*a9fa9459Szrj /* Write out the global symbols. */
1053*a9fa9459Szrj flaginfo.failed = FALSE;
1054*a9fa9459Szrj bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
1055*a9fa9459Szrj if (flaginfo.failed)
1056*a9fa9459Szrj goto error_return;
1057*a9fa9459Szrj
1058*a9fa9459Szrj /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1059*a9fa9459Szrj if (flaginfo.outsyms != NULL)
1060*a9fa9459Szrj {
1061*a9fa9459Szrj free (flaginfo.outsyms);
1062*a9fa9459Szrj flaginfo.outsyms = NULL;
1063*a9fa9459Szrj }
1064*a9fa9459Szrj
1065*a9fa9459Szrj if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
1066*a9fa9459Szrj {
1067*a9fa9459Szrj /* Now that we have written out all the global symbols, we know
1068*a9fa9459Szrj the symbol indices to use for relocs against them, and we can
1069*a9fa9459Szrj finally write out the relocs. */
1070*a9fa9459Szrj amt = max_output_reloc_count * relsz;
1071*a9fa9459Szrj external_relocs = (bfd_byte *) bfd_malloc (amt);
1072*a9fa9459Szrj if (external_relocs == NULL)
1073*a9fa9459Szrj goto error_return;
1074*a9fa9459Szrj
1075*a9fa9459Szrj for (o = abfd->sections; o != NULL; o = o->next)
1076*a9fa9459Szrj {
1077*a9fa9459Szrj struct internal_reloc *irel;
1078*a9fa9459Szrj struct internal_reloc *irelend;
1079*a9fa9459Szrj struct coff_link_hash_entry **rel_hash;
1080*a9fa9459Szrj bfd_byte *erel;
1081*a9fa9459Szrj
1082*a9fa9459Szrj if (o->reloc_count == 0)
1083*a9fa9459Szrj continue;
1084*a9fa9459Szrj
1085*a9fa9459Szrj irel = flaginfo.section_info[o->target_index].relocs;
1086*a9fa9459Szrj irelend = irel + o->reloc_count;
1087*a9fa9459Szrj rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
1088*a9fa9459Szrj erel = external_relocs;
1089*a9fa9459Szrj for (; irel < irelend; irel++, rel_hash++, erel += relsz)
1090*a9fa9459Szrj {
1091*a9fa9459Szrj if (*rel_hash != NULL)
1092*a9fa9459Szrj {
1093*a9fa9459Szrj BFD_ASSERT ((*rel_hash)->indx >= 0);
1094*a9fa9459Szrj irel->r_symndx = (*rel_hash)->indx;
1095*a9fa9459Szrj }
1096*a9fa9459Szrj bfd_coff_swap_reloc_out (abfd, irel, erel);
1097*a9fa9459Szrj }
1098*a9fa9459Szrj
1099*a9fa9459Szrj if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
1100*a9fa9459Szrj goto error_return;
1101*a9fa9459Szrj if (obj_pe (abfd) && o->reloc_count >= 0xffff)
1102*a9fa9459Szrj {
1103*a9fa9459Szrj /* In PE COFF, write the count of relocs as the first
1104*a9fa9459Szrj reloc. The header overflow bit will be set
1105*a9fa9459Szrj elsewhere. */
1106*a9fa9459Szrj struct internal_reloc incount;
1107*a9fa9459Szrj bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
1108*a9fa9459Szrj
1109*a9fa9459Szrj memset (&incount, 0, sizeof (incount));
1110*a9fa9459Szrj incount.r_vaddr = o->reloc_count + 1;
1111*a9fa9459Szrj bfd_coff_swap_reloc_out (abfd, &incount, excount);
1112*a9fa9459Szrj if (bfd_bwrite (excount, relsz, abfd) != relsz)
1113*a9fa9459Szrj /* We'll leak, but it's an error anyway. */
1114*a9fa9459Szrj goto error_return;
1115*a9fa9459Szrj free (excount);
1116*a9fa9459Szrj }
1117*a9fa9459Szrj if (bfd_bwrite (external_relocs,
1118*a9fa9459Szrj (bfd_size_type) relsz * o->reloc_count, abfd)
1119*a9fa9459Szrj != (bfd_size_type) relsz * o->reloc_count)
1120*a9fa9459Szrj goto error_return;
1121*a9fa9459Szrj }
1122*a9fa9459Szrj
1123*a9fa9459Szrj free (external_relocs);
1124*a9fa9459Szrj external_relocs = NULL;
1125*a9fa9459Szrj }
1126*a9fa9459Szrj
1127*a9fa9459Szrj /* Free up the section information. */
1128*a9fa9459Szrj if (flaginfo.section_info != NULL)
1129*a9fa9459Szrj {
1130*a9fa9459Szrj unsigned int i;
1131*a9fa9459Szrj
1132*a9fa9459Szrj for (i = 0; i < abfd->section_count; i++)
1133*a9fa9459Szrj {
1134*a9fa9459Szrj if (flaginfo.section_info[i].relocs != NULL)
1135*a9fa9459Szrj free (flaginfo.section_info[i].relocs);
1136*a9fa9459Szrj if (flaginfo.section_info[i].rel_hashes != NULL)
1137*a9fa9459Szrj free (flaginfo.section_info[i].rel_hashes);
1138*a9fa9459Szrj }
1139*a9fa9459Szrj free (flaginfo.section_info);
1140*a9fa9459Szrj flaginfo.section_info = NULL;
1141*a9fa9459Szrj }
1142*a9fa9459Szrj
1143*a9fa9459Szrj /* If we have optimized stabs strings, output them. */
1144*a9fa9459Szrj if (coff_hash_table (info)->stab_info.stabstr != NULL)
1145*a9fa9459Szrj {
1146*a9fa9459Szrj if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
1147*a9fa9459Szrj return FALSE;
1148*a9fa9459Szrj }
1149*a9fa9459Szrj
1150*a9fa9459Szrj /* Write out the string table. */
1151*a9fa9459Szrj if (obj_raw_syment_count (abfd) != 0 || long_section_names)
1152*a9fa9459Szrj {
1153*a9fa9459Szrj file_ptr pos;
1154*a9fa9459Szrj
1155*a9fa9459Szrj pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
1156*a9fa9459Szrj if (bfd_seek (abfd, pos, SEEK_SET) != 0)
1157*a9fa9459Szrj return FALSE;
1158*a9fa9459Szrj
1159*a9fa9459Szrj #if STRING_SIZE_SIZE == 4
1160*a9fa9459Szrj H_PUT_32 (abfd,
1161*a9fa9459Szrj _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
1162*a9fa9459Szrj strbuf);
1163*a9fa9459Szrj #else
1164*a9fa9459Szrj #error Change H_PUT_32 above
1165*a9fa9459Szrj #endif
1166*a9fa9459Szrj
1167*a9fa9459Szrj if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
1168*a9fa9459Szrj != STRING_SIZE_SIZE)
1169*a9fa9459Szrj return FALSE;
1170*a9fa9459Szrj
1171*a9fa9459Szrj if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
1172*a9fa9459Szrj return FALSE;
1173*a9fa9459Szrj
1174*a9fa9459Szrj obj_coff_strings_written (abfd) = TRUE;
1175*a9fa9459Szrj }
1176*a9fa9459Szrj
1177*a9fa9459Szrj _bfd_stringtab_free (flaginfo.strtab);
1178*a9fa9459Szrj
1179*a9fa9459Szrj /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1180*a9fa9459Szrj not try to write out the symbols. */
1181*a9fa9459Szrj bfd_get_symcount (abfd) = 0;
1182*a9fa9459Szrj
1183*a9fa9459Szrj return TRUE;
1184*a9fa9459Szrj
1185*a9fa9459Szrj error_return:
1186*a9fa9459Szrj if (debug_merge_allocated)
1187*a9fa9459Szrj coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
1188*a9fa9459Szrj if (flaginfo.strtab != NULL)
1189*a9fa9459Szrj _bfd_stringtab_free (flaginfo.strtab);
1190*a9fa9459Szrj if (flaginfo.section_info != NULL)
1191*a9fa9459Szrj {
1192*a9fa9459Szrj unsigned int i;
1193*a9fa9459Szrj
1194*a9fa9459Szrj for (i = 0; i < abfd->section_count; i++)
1195*a9fa9459Szrj {
1196*a9fa9459Szrj if (flaginfo.section_info[i].relocs != NULL)
1197*a9fa9459Szrj free (flaginfo.section_info[i].relocs);
1198*a9fa9459Szrj if (flaginfo.section_info[i].rel_hashes != NULL)
1199*a9fa9459Szrj free (flaginfo.section_info[i].rel_hashes);
1200*a9fa9459Szrj }
1201*a9fa9459Szrj free (flaginfo.section_info);
1202*a9fa9459Szrj }
1203*a9fa9459Szrj if (flaginfo.internal_syms != NULL)
1204*a9fa9459Szrj free (flaginfo.internal_syms);
1205*a9fa9459Szrj if (flaginfo.sec_ptrs != NULL)
1206*a9fa9459Szrj free (flaginfo.sec_ptrs);
1207*a9fa9459Szrj if (flaginfo.sym_indices != NULL)
1208*a9fa9459Szrj free (flaginfo.sym_indices);
1209*a9fa9459Szrj if (flaginfo.outsyms != NULL)
1210*a9fa9459Szrj free (flaginfo.outsyms);
1211*a9fa9459Szrj if (flaginfo.linenos != NULL)
1212*a9fa9459Szrj free (flaginfo.linenos);
1213*a9fa9459Szrj if (flaginfo.contents != NULL)
1214*a9fa9459Szrj free (flaginfo.contents);
1215*a9fa9459Szrj if (flaginfo.external_relocs != NULL)
1216*a9fa9459Szrj free (flaginfo.external_relocs);
1217*a9fa9459Szrj if (flaginfo.internal_relocs != NULL)
1218*a9fa9459Szrj free (flaginfo.internal_relocs);
1219*a9fa9459Szrj if (external_relocs != NULL)
1220*a9fa9459Szrj free (external_relocs);
1221*a9fa9459Szrj return FALSE;
1222*a9fa9459Szrj }
1223*a9fa9459Szrj
1224*a9fa9459Szrj /* Parse out a -heap <reserved>,<commit> line. */
1225*a9fa9459Szrj
1226*a9fa9459Szrj static char *
dores_com(char * ptr,bfd * output_bfd,int heap)1227*a9fa9459Szrj dores_com (char *ptr, bfd *output_bfd, int heap)
1228*a9fa9459Szrj {
1229*a9fa9459Szrj if (coff_data(output_bfd)->pe)
1230*a9fa9459Szrj {
1231*a9fa9459Szrj int val = strtoul (ptr, &ptr, 0);
1232*a9fa9459Szrj
1233*a9fa9459Szrj if (heap)
1234*a9fa9459Szrj pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
1235*a9fa9459Szrj else
1236*a9fa9459Szrj pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
1237*a9fa9459Szrj
1238*a9fa9459Szrj if (ptr[0] == ',')
1239*a9fa9459Szrj {
1240*a9fa9459Szrj val = strtoul (ptr+1, &ptr, 0);
1241*a9fa9459Szrj if (heap)
1242*a9fa9459Szrj pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
1243*a9fa9459Szrj else
1244*a9fa9459Szrj pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
1245*a9fa9459Szrj }
1246*a9fa9459Szrj }
1247*a9fa9459Szrj return ptr;
1248*a9fa9459Szrj }
1249*a9fa9459Szrj
1250*a9fa9459Szrj static char *
get_name(char * ptr,char ** dst)1251*a9fa9459Szrj get_name (char *ptr, char **dst)
1252*a9fa9459Szrj {
1253*a9fa9459Szrj while (*ptr == ' ')
1254*a9fa9459Szrj ptr++;
1255*a9fa9459Szrj *dst = ptr;
1256*a9fa9459Szrj while (*ptr && *ptr != ' ')
1257*a9fa9459Szrj ptr++;
1258*a9fa9459Szrj *ptr = 0;
1259*a9fa9459Szrj return ptr+1;
1260*a9fa9459Szrj }
1261*a9fa9459Szrj
1262*a9fa9459Szrj /* Process any magic embedded commands in a section called .drectve. */
1263*a9fa9459Szrj
1264*a9fa9459Szrj static int
process_embedded_commands(bfd * output_bfd,struct bfd_link_info * info ATTRIBUTE_UNUSED,bfd * abfd)1265*a9fa9459Szrj process_embedded_commands (bfd *output_bfd,
1266*a9fa9459Szrj struct bfd_link_info *info ATTRIBUTE_UNUSED,
1267*a9fa9459Szrj bfd *abfd)
1268*a9fa9459Szrj {
1269*a9fa9459Szrj asection *sec = bfd_get_section_by_name (abfd, ".drectve");
1270*a9fa9459Szrj char *s;
1271*a9fa9459Szrj char *e;
1272*a9fa9459Szrj bfd_byte *copy;
1273*a9fa9459Szrj
1274*a9fa9459Szrj if (!sec)
1275*a9fa9459Szrj return 1;
1276*a9fa9459Szrj
1277*a9fa9459Szrj if (!bfd_malloc_and_get_section (abfd, sec, ©))
1278*a9fa9459Szrj {
1279*a9fa9459Szrj if (copy != NULL)
1280*a9fa9459Szrj free (copy);
1281*a9fa9459Szrj return 0;
1282*a9fa9459Szrj }
1283*a9fa9459Szrj e = (char *) copy + sec->size;
1284*a9fa9459Szrj
1285*a9fa9459Szrj for (s = (char *) copy; s < e ; )
1286*a9fa9459Szrj {
1287*a9fa9459Szrj if (s[0] != '-')
1288*a9fa9459Szrj {
1289*a9fa9459Szrj s++;
1290*a9fa9459Szrj continue;
1291*a9fa9459Szrj }
1292*a9fa9459Szrj if (CONST_STRNEQ (s, "-attr"))
1293*a9fa9459Szrj {
1294*a9fa9459Szrj char *name;
1295*a9fa9459Szrj char *attribs;
1296*a9fa9459Szrj asection *asec;
1297*a9fa9459Szrj int loop = 1;
1298*a9fa9459Szrj int had_write = 0;
1299*a9fa9459Szrj int had_exec= 0;
1300*a9fa9459Szrj
1301*a9fa9459Szrj s += 5;
1302*a9fa9459Szrj s = get_name (s, &name);
1303*a9fa9459Szrj s = get_name (s, &attribs);
1304*a9fa9459Szrj
1305*a9fa9459Szrj while (loop)
1306*a9fa9459Szrj {
1307*a9fa9459Szrj switch (*attribs++)
1308*a9fa9459Szrj {
1309*a9fa9459Szrj case 'W':
1310*a9fa9459Szrj had_write = 1;
1311*a9fa9459Szrj break;
1312*a9fa9459Szrj case 'R':
1313*a9fa9459Szrj break;
1314*a9fa9459Szrj case 'S':
1315*a9fa9459Szrj break;
1316*a9fa9459Szrj case 'X':
1317*a9fa9459Szrj had_exec = 1;
1318*a9fa9459Szrj break;
1319*a9fa9459Szrj default:
1320*a9fa9459Szrj loop = 0;
1321*a9fa9459Szrj }
1322*a9fa9459Szrj }
1323*a9fa9459Szrj asec = bfd_get_section_by_name (abfd, name);
1324*a9fa9459Szrj if (asec)
1325*a9fa9459Szrj {
1326*a9fa9459Szrj if (had_exec)
1327*a9fa9459Szrj asec->flags |= SEC_CODE;
1328*a9fa9459Szrj if (!had_write)
1329*a9fa9459Szrj asec->flags |= SEC_READONLY;
1330*a9fa9459Szrj }
1331*a9fa9459Szrj }
1332*a9fa9459Szrj else if (CONST_STRNEQ (s, "-heap"))
1333*a9fa9459Szrj s = dores_com (s + 5, output_bfd, 1);
1334*a9fa9459Szrj
1335*a9fa9459Szrj else if (CONST_STRNEQ (s, "-stack"))
1336*a9fa9459Szrj s = dores_com (s + 6, output_bfd, 0);
1337*a9fa9459Szrj
1338*a9fa9459Szrj /* GNU extension for aligned commons. */
1339*a9fa9459Szrj else if (CONST_STRNEQ (s, "-aligncomm:"))
1340*a9fa9459Szrj {
1341*a9fa9459Szrj /* Common symbols must be aligned on reading, as it
1342*a9fa9459Szrj is too late to do anything here, after they have
1343*a9fa9459Szrj already been allocated, so just skip the directive. */
1344*a9fa9459Szrj s += 11;
1345*a9fa9459Szrj }
1346*a9fa9459Szrj
1347*a9fa9459Szrj else
1348*a9fa9459Szrj s++;
1349*a9fa9459Szrj }
1350*a9fa9459Szrj free (copy);
1351*a9fa9459Szrj return 1;
1352*a9fa9459Szrj }
1353*a9fa9459Szrj
1354*a9fa9459Szrj /* Place a marker against all symbols which are used by relocations.
1355*a9fa9459Szrj This marker can be picked up by the 'do we skip this symbol ?'
1356*a9fa9459Szrj loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1357*a9fa9459Szrj that symbol. */
1358*a9fa9459Szrj
1359*a9fa9459Szrj static void
mark_relocs(struct coff_final_link_info * flaginfo,bfd * input_bfd)1360*a9fa9459Szrj mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1361*a9fa9459Szrj {
1362*a9fa9459Szrj asection * a;
1363*a9fa9459Szrj
1364*a9fa9459Szrj if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
1365*a9fa9459Szrj return;
1366*a9fa9459Szrj
1367*a9fa9459Szrj for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
1368*a9fa9459Szrj {
1369*a9fa9459Szrj struct internal_reloc * internal_relocs;
1370*a9fa9459Szrj struct internal_reloc * irel;
1371*a9fa9459Szrj struct internal_reloc * irelend;
1372*a9fa9459Szrj
1373*a9fa9459Szrj if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1
1374*a9fa9459Szrj || a->linker_mark == 0)
1375*a9fa9459Szrj continue;
1376*a9fa9459Szrj /* Don't mark relocs in excluded sections. */
1377*a9fa9459Szrj if (a->output_section == bfd_abs_section_ptr)
1378*a9fa9459Szrj continue;
1379*a9fa9459Szrj
1380*a9fa9459Szrj /* Read in the relocs. */
1381*a9fa9459Szrj internal_relocs = _bfd_coff_read_internal_relocs
1382*a9fa9459Szrj (input_bfd, a, FALSE,
1383*a9fa9459Szrj flaginfo->external_relocs,
1384*a9fa9459Szrj bfd_link_relocatable (flaginfo->info),
1385*a9fa9459Szrj (bfd_link_relocatable (flaginfo->info)
1386*a9fa9459Szrj ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
1387*a9fa9459Szrj : flaginfo->internal_relocs)
1388*a9fa9459Szrj );
1389*a9fa9459Szrj
1390*a9fa9459Szrj if (internal_relocs == NULL)
1391*a9fa9459Szrj continue;
1392*a9fa9459Szrj
1393*a9fa9459Szrj irel = internal_relocs;
1394*a9fa9459Szrj irelend = irel + a->reloc_count;
1395*a9fa9459Szrj
1396*a9fa9459Szrj /* Place a mark in the sym_indices array (whose entries have
1397*a9fa9459Szrj been initialised to 0) for all of the symbols that are used
1398*a9fa9459Szrj in the relocation table. This will then be picked up in the
1399*a9fa9459Szrj skip/don't-skip pass. */
1400*a9fa9459Szrj for (; irel < irelend; irel++)
1401*a9fa9459Szrj flaginfo->sym_indices[ irel->r_symndx ] = -1;
1402*a9fa9459Szrj }
1403*a9fa9459Szrj }
1404*a9fa9459Szrj
1405*a9fa9459Szrj /* Link an input file into the linker output file. This function
1406*a9fa9459Szrj handles all the sections and relocations of the input file at once. */
1407*a9fa9459Szrj
1408*a9fa9459Szrj bfd_boolean
_bfd_coff_link_input_bfd(struct coff_final_link_info * flaginfo,bfd * input_bfd)1409*a9fa9459Szrj _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
1410*a9fa9459Szrj {
1411*a9fa9459Szrj unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
1412*a9fa9459Szrj unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
1413*a9fa9459Szrj bfd_boolean (*adjust_symndx)
1414*a9fa9459Szrj (bfd *, struct bfd_link_info *, bfd *, asection *,
1415*a9fa9459Szrj struct internal_reloc *, bfd_boolean *);
1416*a9fa9459Szrj bfd *output_bfd;
1417*a9fa9459Szrj const char *strings;
1418*a9fa9459Szrj bfd_size_type syment_base;
1419*a9fa9459Szrj bfd_boolean copy, hash;
1420*a9fa9459Szrj bfd_size_type isymesz;
1421*a9fa9459Szrj bfd_size_type osymesz;
1422*a9fa9459Szrj bfd_size_type linesz;
1423*a9fa9459Szrj bfd_byte *esym;
1424*a9fa9459Szrj bfd_byte *esym_end;
1425*a9fa9459Szrj struct internal_syment *isymp;
1426*a9fa9459Szrj asection **secpp;
1427*a9fa9459Szrj long *indexp;
1428*a9fa9459Szrj unsigned long output_index;
1429*a9fa9459Szrj bfd_byte *outsym;
1430*a9fa9459Szrj struct coff_link_hash_entry **sym_hash;
1431*a9fa9459Szrj asection *o;
1432*a9fa9459Szrj
1433*a9fa9459Szrj /* Move all the symbols to the output file. */
1434*a9fa9459Szrj
1435*a9fa9459Szrj output_bfd = flaginfo->output_bfd;
1436*a9fa9459Szrj strings = NULL;
1437*a9fa9459Szrj syment_base = obj_raw_syment_count (output_bfd);
1438*a9fa9459Szrj isymesz = bfd_coff_symesz (input_bfd);
1439*a9fa9459Szrj osymesz = bfd_coff_symesz (output_bfd);
1440*a9fa9459Szrj linesz = bfd_coff_linesz (input_bfd);
1441*a9fa9459Szrj BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
1442*a9fa9459Szrj
1443*a9fa9459Szrj copy = FALSE;
1444*a9fa9459Szrj if (! flaginfo->info->keep_memory)
1445*a9fa9459Szrj copy = TRUE;
1446*a9fa9459Szrj hash = TRUE;
1447*a9fa9459Szrj if (flaginfo->info->traditional_format)
1448*a9fa9459Szrj hash = FALSE;
1449*a9fa9459Szrj
1450*a9fa9459Szrj if (! _bfd_coff_get_external_symbols (input_bfd))
1451*a9fa9459Szrj return FALSE;
1452*a9fa9459Szrj
1453*a9fa9459Szrj esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1454*a9fa9459Szrj esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1455*a9fa9459Szrj isymp = flaginfo->internal_syms;
1456*a9fa9459Szrj secpp = flaginfo->sec_ptrs;
1457*a9fa9459Szrj indexp = flaginfo->sym_indices;
1458*a9fa9459Szrj output_index = syment_base;
1459*a9fa9459Szrj outsym = flaginfo->outsyms;
1460*a9fa9459Szrj
1461*a9fa9459Szrj if (coff_data (output_bfd)->pe
1462*a9fa9459Szrj && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
1463*a9fa9459Szrj return FALSE;
1464*a9fa9459Szrj
1465*a9fa9459Szrj /* If we are going to perform relocations and also strip/discard some
1466*a9fa9459Szrj symbols then we must make sure that we do not strip/discard those
1467*a9fa9459Szrj symbols that are going to be involved in the relocations. */
1468*a9fa9459Szrj if (( flaginfo->info->strip != strip_none
1469*a9fa9459Szrj || flaginfo->info->discard != discard_none)
1470*a9fa9459Szrj && bfd_link_relocatable (flaginfo->info))
1471*a9fa9459Szrj {
1472*a9fa9459Szrj /* Mark the symbol array as 'not-used'. */
1473*a9fa9459Szrj memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
1474*a9fa9459Szrj
1475*a9fa9459Szrj mark_relocs (flaginfo, input_bfd);
1476*a9fa9459Szrj }
1477*a9fa9459Szrj
1478*a9fa9459Szrj while (esym < esym_end)
1479*a9fa9459Szrj {
1480*a9fa9459Szrj struct internal_syment isym;
1481*a9fa9459Szrj enum coff_symbol_classification classification;
1482*a9fa9459Szrj bfd_boolean skip;
1483*a9fa9459Szrj bfd_boolean global;
1484*a9fa9459Szrj bfd_boolean dont_skip_symbol;
1485*a9fa9459Szrj int add;
1486*a9fa9459Szrj
1487*a9fa9459Szrj bfd_coff_swap_sym_in (input_bfd, esym, isymp);
1488*a9fa9459Szrj
1489*a9fa9459Szrj /* Make a copy of *isymp so that the relocate_section function
1490*a9fa9459Szrj always sees the original values. This is more reliable than
1491*a9fa9459Szrj always recomputing the symbol value even if we are stripping
1492*a9fa9459Szrj the symbol. */
1493*a9fa9459Szrj isym = *isymp;
1494*a9fa9459Szrj
1495*a9fa9459Szrj classification = bfd_coff_classify_symbol (input_bfd, &isym);
1496*a9fa9459Szrj switch (classification)
1497*a9fa9459Szrj {
1498*a9fa9459Szrj default:
1499*a9fa9459Szrj abort ();
1500*a9fa9459Szrj case COFF_SYMBOL_GLOBAL:
1501*a9fa9459Szrj case COFF_SYMBOL_PE_SECTION:
1502*a9fa9459Szrj case COFF_SYMBOL_LOCAL:
1503*a9fa9459Szrj *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
1504*a9fa9459Szrj break;
1505*a9fa9459Szrj case COFF_SYMBOL_COMMON:
1506*a9fa9459Szrj *secpp = bfd_com_section_ptr;
1507*a9fa9459Szrj break;
1508*a9fa9459Szrj case COFF_SYMBOL_UNDEFINED:
1509*a9fa9459Szrj *secpp = bfd_und_section_ptr;
1510*a9fa9459Szrj break;
1511*a9fa9459Szrj }
1512*a9fa9459Szrj
1513*a9fa9459Szrj /* Extract the flag indicating if this symbol is used by a
1514*a9fa9459Szrj relocation. */
1515*a9fa9459Szrj if ((flaginfo->info->strip != strip_none
1516*a9fa9459Szrj || flaginfo->info->discard != discard_none)
1517*a9fa9459Szrj && bfd_link_relocatable (flaginfo->info))
1518*a9fa9459Szrj dont_skip_symbol = *indexp;
1519*a9fa9459Szrj else
1520*a9fa9459Szrj dont_skip_symbol = FALSE;
1521*a9fa9459Szrj
1522*a9fa9459Szrj *indexp = -1;
1523*a9fa9459Szrj
1524*a9fa9459Szrj skip = FALSE;
1525*a9fa9459Szrj global = FALSE;
1526*a9fa9459Szrj add = 1 + isym.n_numaux;
1527*a9fa9459Szrj
1528*a9fa9459Szrj /* If we are stripping all symbols, we want to skip this one. */
1529*a9fa9459Szrj if (flaginfo->info->strip == strip_all && ! dont_skip_symbol)
1530*a9fa9459Szrj skip = TRUE;
1531*a9fa9459Szrj
1532*a9fa9459Szrj if (! skip)
1533*a9fa9459Szrj {
1534*a9fa9459Szrj switch (classification)
1535*a9fa9459Szrj {
1536*a9fa9459Szrj default:
1537*a9fa9459Szrj abort ();
1538*a9fa9459Szrj case COFF_SYMBOL_GLOBAL:
1539*a9fa9459Szrj case COFF_SYMBOL_COMMON:
1540*a9fa9459Szrj case COFF_SYMBOL_PE_SECTION:
1541*a9fa9459Szrj /* This is a global symbol. Global symbols come at the
1542*a9fa9459Szrj end of the symbol table, so skip them for now.
1543*a9fa9459Szrj Locally defined function symbols, however, are an
1544*a9fa9459Szrj exception, and are not moved to the end. */
1545*a9fa9459Szrj global = TRUE;
1546*a9fa9459Szrj if (! ISFCN (isym.n_type))
1547*a9fa9459Szrj skip = TRUE;
1548*a9fa9459Szrj break;
1549*a9fa9459Szrj
1550*a9fa9459Szrj case COFF_SYMBOL_UNDEFINED:
1551*a9fa9459Szrj /* Undefined symbols are left for the end. */
1552*a9fa9459Szrj global = TRUE;
1553*a9fa9459Szrj skip = TRUE;
1554*a9fa9459Szrj break;
1555*a9fa9459Szrj
1556*a9fa9459Szrj case COFF_SYMBOL_LOCAL:
1557*a9fa9459Szrj /* This is a local symbol. Skip it if we are discarding
1558*a9fa9459Szrj local symbols. */
1559*a9fa9459Szrj if (flaginfo->info->discard == discard_all && ! dont_skip_symbol)
1560*a9fa9459Szrj skip = TRUE;
1561*a9fa9459Szrj break;
1562*a9fa9459Szrj }
1563*a9fa9459Szrj }
1564*a9fa9459Szrj
1565*a9fa9459Szrj #ifndef COFF_WITH_PE
1566*a9fa9459Szrj /* Skip section symbols for sections which are not going to be
1567*a9fa9459Szrj emitted. */
1568*a9fa9459Szrj if (!skip
1569*a9fa9459Szrj && !dont_skip_symbol
1570*a9fa9459Szrj && isym.n_sclass == C_STAT
1571*a9fa9459Szrj && isym.n_type == T_NULL
1572*a9fa9459Szrj && isym.n_numaux > 0
1573*a9fa9459Szrj && ((*secpp)->output_section == bfd_abs_section_ptr
1574*a9fa9459Szrj || bfd_section_removed_from_list (output_bfd,
1575*a9fa9459Szrj (*secpp)->output_section)))
1576*a9fa9459Szrj skip = TRUE;
1577*a9fa9459Szrj #endif
1578*a9fa9459Szrj
1579*a9fa9459Szrj /* If we stripping debugging symbols, and this is a debugging
1580*a9fa9459Szrj symbol, then skip it. FIXME: gas sets the section to N_ABS
1581*a9fa9459Szrj for some types of debugging symbols; I don't know if this is
1582*a9fa9459Szrj a bug or not. In any case, we handle it here. */
1583*a9fa9459Szrj if (! skip
1584*a9fa9459Szrj && flaginfo->info->strip == strip_debugger
1585*a9fa9459Szrj && ! dont_skip_symbol
1586*a9fa9459Szrj && (isym.n_scnum == N_DEBUG
1587*a9fa9459Szrj || (isym.n_scnum == N_ABS
1588*a9fa9459Szrj && (isym.n_sclass == C_AUTO
1589*a9fa9459Szrj || isym.n_sclass == C_REG
1590*a9fa9459Szrj || isym.n_sclass == C_MOS
1591*a9fa9459Szrj || isym.n_sclass == C_MOE
1592*a9fa9459Szrj || isym.n_sclass == C_MOU
1593*a9fa9459Szrj || isym.n_sclass == C_ARG
1594*a9fa9459Szrj || isym.n_sclass == C_REGPARM
1595*a9fa9459Szrj || isym.n_sclass == C_FIELD
1596*a9fa9459Szrj || isym.n_sclass == C_EOS))))
1597*a9fa9459Szrj skip = TRUE;
1598*a9fa9459Szrj
1599*a9fa9459Szrj /* If some symbols are stripped based on the name, work out the
1600*a9fa9459Szrj name and decide whether to skip this symbol. */
1601*a9fa9459Szrj if (! skip
1602*a9fa9459Szrj && (flaginfo->info->strip == strip_some
1603*a9fa9459Szrj || flaginfo->info->discard == discard_l))
1604*a9fa9459Szrj {
1605*a9fa9459Szrj const char *name;
1606*a9fa9459Szrj char buf[SYMNMLEN + 1];
1607*a9fa9459Szrj
1608*a9fa9459Szrj name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1609*a9fa9459Szrj if (name == NULL)
1610*a9fa9459Szrj return FALSE;
1611*a9fa9459Szrj
1612*a9fa9459Szrj if (! dont_skip_symbol
1613*a9fa9459Szrj && ((flaginfo->info->strip == strip_some
1614*a9fa9459Szrj && (bfd_hash_lookup (flaginfo->info->keep_hash, name, FALSE,
1615*a9fa9459Szrj FALSE) == NULL))
1616*a9fa9459Szrj || (! global
1617*a9fa9459Szrj && flaginfo->info->discard == discard_l
1618*a9fa9459Szrj && bfd_is_local_label_name (input_bfd, name))))
1619*a9fa9459Szrj skip = TRUE;
1620*a9fa9459Szrj }
1621*a9fa9459Szrj
1622*a9fa9459Szrj /* If this is an enum, struct, or union tag, see if we have
1623*a9fa9459Szrj already output an identical type. */
1624*a9fa9459Szrj if (! skip
1625*a9fa9459Szrj && !flaginfo->info->traditional_format
1626*a9fa9459Szrj && (isym.n_sclass == C_ENTAG
1627*a9fa9459Szrj || isym.n_sclass == C_STRTAG
1628*a9fa9459Szrj || isym.n_sclass == C_UNTAG)
1629*a9fa9459Szrj && isym.n_numaux == 1)
1630*a9fa9459Szrj {
1631*a9fa9459Szrj const char *name;
1632*a9fa9459Szrj char buf[SYMNMLEN + 1];
1633*a9fa9459Szrj struct coff_debug_merge_hash_entry *mh;
1634*a9fa9459Szrj struct coff_debug_merge_type *mt;
1635*a9fa9459Szrj union internal_auxent aux;
1636*a9fa9459Szrj struct coff_debug_merge_element **epp;
1637*a9fa9459Szrj bfd_byte *esl, *eslend;
1638*a9fa9459Szrj struct internal_syment *islp;
1639*a9fa9459Szrj bfd_size_type amt;
1640*a9fa9459Szrj
1641*a9fa9459Szrj name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
1642*a9fa9459Szrj if (name == NULL)
1643*a9fa9459Szrj return FALSE;
1644*a9fa9459Szrj
1645*a9fa9459Szrj /* Ignore fake names invented by compiler; treat them all as
1646*a9fa9459Szrj the same name. */
1647*a9fa9459Szrj if (*name == '~' || *name == '.' || *name == '$'
1648*a9fa9459Szrj || (*name == bfd_get_symbol_leading_char (input_bfd)
1649*a9fa9459Szrj && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
1650*a9fa9459Szrj name = "";
1651*a9fa9459Szrj
1652*a9fa9459Szrj mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
1653*a9fa9459Szrj TRUE, TRUE);
1654*a9fa9459Szrj if (mh == NULL)
1655*a9fa9459Szrj return FALSE;
1656*a9fa9459Szrj
1657*a9fa9459Szrj /* Allocate memory to hold type information. If this turns
1658*a9fa9459Szrj out to be a duplicate, we pass this address to
1659*a9fa9459Szrj bfd_release. */
1660*a9fa9459Szrj amt = sizeof (struct coff_debug_merge_type);
1661*a9fa9459Szrj mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
1662*a9fa9459Szrj if (mt == NULL)
1663*a9fa9459Szrj return FALSE;
1664*a9fa9459Szrj mt->type_class = isym.n_sclass;
1665*a9fa9459Szrj
1666*a9fa9459Szrj /* Pick up the aux entry, which points to the end of the tag
1667*a9fa9459Szrj entries. */
1668*a9fa9459Szrj bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
1669*a9fa9459Szrj isym.n_type, isym.n_sclass, 0, isym.n_numaux,
1670*a9fa9459Szrj &aux);
1671*a9fa9459Szrj
1672*a9fa9459Szrj /* Gather the elements. */
1673*a9fa9459Szrj epp = &mt->elements;
1674*a9fa9459Szrj mt->elements = NULL;
1675*a9fa9459Szrj islp = isymp + 2;
1676*a9fa9459Szrj esl = esym + 2 * isymesz;
1677*a9fa9459Szrj eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
1678*a9fa9459Szrj + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
1679*a9fa9459Szrj while (esl < eslend)
1680*a9fa9459Szrj {
1681*a9fa9459Szrj const char *elename;
1682*a9fa9459Szrj char elebuf[SYMNMLEN + 1];
1683*a9fa9459Szrj char *name_copy;
1684*a9fa9459Szrj
1685*a9fa9459Szrj bfd_coff_swap_sym_in (input_bfd, esl, islp);
1686*a9fa9459Szrj
1687*a9fa9459Szrj amt = sizeof (struct coff_debug_merge_element);
1688*a9fa9459Szrj *epp = (struct coff_debug_merge_element *)
1689*a9fa9459Szrj bfd_alloc (input_bfd, amt);
1690*a9fa9459Szrj if (*epp == NULL)
1691*a9fa9459Szrj return FALSE;
1692*a9fa9459Szrj
1693*a9fa9459Szrj elename = _bfd_coff_internal_syment_name (input_bfd, islp,
1694*a9fa9459Szrj elebuf);
1695*a9fa9459Szrj if (elename == NULL)
1696*a9fa9459Szrj return FALSE;
1697*a9fa9459Szrj
1698*a9fa9459Szrj amt = strlen (elename) + 1;
1699*a9fa9459Szrj name_copy = (char *) bfd_alloc (input_bfd, amt);
1700*a9fa9459Szrj if (name_copy == NULL)
1701*a9fa9459Szrj return FALSE;
1702*a9fa9459Szrj strcpy (name_copy, elename);
1703*a9fa9459Szrj
1704*a9fa9459Szrj (*epp)->name = name_copy;
1705*a9fa9459Szrj (*epp)->type = islp->n_type;
1706*a9fa9459Szrj (*epp)->tagndx = 0;
1707*a9fa9459Szrj if (islp->n_numaux >= 1
1708*a9fa9459Szrj && islp->n_type != T_NULL
1709*a9fa9459Szrj && islp->n_sclass != C_EOS)
1710*a9fa9459Szrj {
1711*a9fa9459Szrj union internal_auxent eleaux;
1712*a9fa9459Szrj long indx;
1713*a9fa9459Szrj
1714*a9fa9459Szrj bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
1715*a9fa9459Szrj islp->n_type, islp->n_sclass, 0,
1716*a9fa9459Szrj islp->n_numaux, &eleaux);
1717*a9fa9459Szrj indx = eleaux.x_sym.x_tagndx.l;
1718*a9fa9459Szrj
1719*a9fa9459Szrj /* FIXME: If this tagndx entry refers to a symbol
1720*a9fa9459Szrj defined later in this file, we just ignore it.
1721*a9fa9459Szrj Handling this correctly would be tedious, and may
1722*a9fa9459Szrj not be required. */
1723*a9fa9459Szrj if (indx > 0
1724*a9fa9459Szrj && (indx
1725*a9fa9459Szrj < ((esym -
1726*a9fa9459Szrj (bfd_byte *) obj_coff_external_syms (input_bfd))
1727*a9fa9459Szrj / (long) isymesz)))
1728*a9fa9459Szrj {
1729*a9fa9459Szrj (*epp)->tagndx = flaginfo->sym_indices[indx];
1730*a9fa9459Szrj if ((*epp)->tagndx < 0)
1731*a9fa9459Szrj (*epp)->tagndx = 0;
1732*a9fa9459Szrj }
1733*a9fa9459Szrj }
1734*a9fa9459Szrj epp = &(*epp)->next;
1735*a9fa9459Szrj *epp = NULL;
1736*a9fa9459Szrj
1737*a9fa9459Szrj esl += (islp->n_numaux + 1) * isymesz;
1738*a9fa9459Szrj islp += islp->n_numaux + 1;
1739*a9fa9459Szrj }
1740*a9fa9459Szrj
1741*a9fa9459Szrj /* See if we already have a definition which matches this
1742*a9fa9459Szrj type. We always output the type if it has no elements,
1743*a9fa9459Szrj for simplicity. */
1744*a9fa9459Szrj if (mt->elements == NULL)
1745*a9fa9459Szrj bfd_release (input_bfd, mt);
1746*a9fa9459Szrj else
1747*a9fa9459Szrj {
1748*a9fa9459Szrj struct coff_debug_merge_type *mtl;
1749*a9fa9459Szrj
1750*a9fa9459Szrj for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
1751*a9fa9459Szrj {
1752*a9fa9459Szrj struct coff_debug_merge_element *me, *mel;
1753*a9fa9459Szrj
1754*a9fa9459Szrj if (mtl->type_class != mt->type_class)
1755*a9fa9459Szrj continue;
1756*a9fa9459Szrj
1757*a9fa9459Szrj for (me = mt->elements, mel = mtl->elements;
1758*a9fa9459Szrj me != NULL && mel != NULL;
1759*a9fa9459Szrj me = me->next, mel = mel->next)
1760*a9fa9459Szrj {
1761*a9fa9459Szrj if (strcmp (me->name, mel->name) != 0
1762*a9fa9459Szrj || me->type != mel->type
1763*a9fa9459Szrj || me->tagndx != mel->tagndx)
1764*a9fa9459Szrj break;
1765*a9fa9459Szrj }
1766*a9fa9459Szrj
1767*a9fa9459Szrj if (me == NULL && mel == NULL)
1768*a9fa9459Szrj break;
1769*a9fa9459Szrj }
1770*a9fa9459Szrj
1771*a9fa9459Szrj if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
1772*a9fa9459Szrj {
1773*a9fa9459Szrj /* This is the first definition of this type. */
1774*a9fa9459Szrj mt->indx = output_index;
1775*a9fa9459Szrj mt->next = mh->types;
1776*a9fa9459Szrj mh->types = mt;
1777*a9fa9459Szrj }
1778*a9fa9459Szrj else
1779*a9fa9459Szrj {
1780*a9fa9459Szrj /* This is a redefinition which can be merged. */
1781*a9fa9459Szrj bfd_release (input_bfd, mt);
1782*a9fa9459Szrj *indexp = mtl->indx;
1783*a9fa9459Szrj add = (eslend - esym) / isymesz;
1784*a9fa9459Szrj skip = TRUE;
1785*a9fa9459Szrj }
1786*a9fa9459Szrj }
1787*a9fa9459Szrj }
1788*a9fa9459Szrj
1789*a9fa9459Szrj /* We now know whether we are to skip this symbol or not. */
1790*a9fa9459Szrj if (! skip)
1791*a9fa9459Szrj {
1792*a9fa9459Szrj /* Adjust the symbol in order to output it. */
1793*a9fa9459Szrj
1794*a9fa9459Szrj if (isym._n._n_n._n_zeroes == 0
1795*a9fa9459Szrj && isym._n._n_n._n_offset != 0)
1796*a9fa9459Szrj {
1797*a9fa9459Szrj const char *name;
1798*a9fa9459Szrj bfd_size_type indx;
1799*a9fa9459Szrj
1800*a9fa9459Szrj /* This symbol has a long name. Enter it in the string
1801*a9fa9459Szrj table we are building. Note that we do not check
1802*a9fa9459Szrj bfd_coff_symname_in_debug. That is only true for
1803*a9fa9459Szrj XCOFF, and XCOFF requires different linking code
1804*a9fa9459Szrj anyhow. */
1805*a9fa9459Szrj name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
1806*a9fa9459Szrj if (name == NULL)
1807*a9fa9459Szrj return FALSE;
1808*a9fa9459Szrj indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
1809*a9fa9459Szrj if (indx == (bfd_size_type) -1)
1810*a9fa9459Szrj return FALSE;
1811*a9fa9459Szrj isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1812*a9fa9459Szrj }
1813*a9fa9459Szrj
1814*a9fa9459Szrj switch (isym.n_sclass)
1815*a9fa9459Szrj {
1816*a9fa9459Szrj case C_AUTO:
1817*a9fa9459Szrj case C_MOS:
1818*a9fa9459Szrj case C_EOS:
1819*a9fa9459Szrj case C_MOE:
1820*a9fa9459Szrj case C_MOU:
1821*a9fa9459Szrj case C_UNTAG:
1822*a9fa9459Szrj case C_STRTAG:
1823*a9fa9459Szrj case C_ENTAG:
1824*a9fa9459Szrj case C_TPDEF:
1825*a9fa9459Szrj case C_ARG:
1826*a9fa9459Szrj case C_USTATIC:
1827*a9fa9459Szrj case C_REG:
1828*a9fa9459Szrj case C_REGPARM:
1829*a9fa9459Szrj case C_FIELD:
1830*a9fa9459Szrj /* The symbol value should not be modified. */
1831*a9fa9459Szrj break;
1832*a9fa9459Szrj
1833*a9fa9459Szrj case C_FCN:
1834*a9fa9459Szrj if (obj_pe (input_bfd)
1835*a9fa9459Szrj && strcmp (isym.n_name, ".bf") != 0
1836*a9fa9459Szrj && isym.n_scnum > 0)
1837*a9fa9459Szrj {
1838*a9fa9459Szrj /* For PE, .lf and .ef get their value left alone,
1839*a9fa9459Szrj while .bf gets relocated. However, they all have
1840*a9fa9459Szrj "real" section numbers, and need to be moved into
1841*a9fa9459Szrj the new section. */
1842*a9fa9459Szrj isym.n_scnum = (*secpp)->output_section->target_index;
1843*a9fa9459Szrj break;
1844*a9fa9459Szrj }
1845*a9fa9459Szrj /* Fall through. */
1846*a9fa9459Szrj default:
1847*a9fa9459Szrj case C_LABEL: /* Not completely sure about these 2 */
1848*a9fa9459Szrj case C_EXTDEF:
1849*a9fa9459Szrj case C_BLOCK:
1850*a9fa9459Szrj case C_EFCN:
1851*a9fa9459Szrj case C_NULL:
1852*a9fa9459Szrj case C_EXT:
1853*a9fa9459Szrj case C_STAT:
1854*a9fa9459Szrj case C_SECTION:
1855*a9fa9459Szrj case C_NT_WEAK:
1856*a9fa9459Szrj /* Compute new symbol location. */
1857*a9fa9459Szrj if (isym.n_scnum > 0)
1858*a9fa9459Szrj {
1859*a9fa9459Szrj isym.n_scnum = (*secpp)->output_section->target_index;
1860*a9fa9459Szrj isym.n_value += (*secpp)->output_offset;
1861*a9fa9459Szrj if (! obj_pe (input_bfd))
1862*a9fa9459Szrj isym.n_value -= (*secpp)->vma;
1863*a9fa9459Szrj if (! obj_pe (flaginfo->output_bfd))
1864*a9fa9459Szrj isym.n_value += (*secpp)->output_section->vma;
1865*a9fa9459Szrj }
1866*a9fa9459Szrj break;
1867*a9fa9459Szrj
1868*a9fa9459Szrj case C_FILE:
1869*a9fa9459Szrj /* The value of a C_FILE symbol is the symbol index of
1870*a9fa9459Szrj the next C_FILE symbol. The value of the last C_FILE
1871*a9fa9459Szrj symbol is the symbol index to the first external
1872*a9fa9459Szrj symbol (actually, coff_renumber_symbols does not get
1873*a9fa9459Szrj this right--it just sets the value of the last C_FILE
1874*a9fa9459Szrj symbol to zero--and nobody has ever complained about
1875*a9fa9459Szrj it). We try to get this right, below, just before we
1876*a9fa9459Szrj write the symbols out, but in the general case we may
1877*a9fa9459Szrj have to write the symbol out twice. */
1878*a9fa9459Szrj if (flaginfo->last_file_index != -1
1879*a9fa9459Szrj && flaginfo->last_file.n_value != (bfd_vma) output_index)
1880*a9fa9459Szrj {
1881*a9fa9459Szrj /* We must correct the value of the last C_FILE
1882*a9fa9459Szrj entry. */
1883*a9fa9459Szrj flaginfo->last_file.n_value = output_index;
1884*a9fa9459Szrj if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
1885*a9fa9459Szrj {
1886*a9fa9459Szrj /* The last C_FILE symbol is in this input file. */
1887*a9fa9459Szrj bfd_coff_swap_sym_out (output_bfd,
1888*a9fa9459Szrj &flaginfo->last_file,
1889*a9fa9459Szrj (flaginfo->outsyms
1890*a9fa9459Szrj + ((flaginfo->last_file_index
1891*a9fa9459Szrj - syment_base)
1892*a9fa9459Szrj * osymesz)));
1893*a9fa9459Szrj }
1894*a9fa9459Szrj else
1895*a9fa9459Szrj {
1896*a9fa9459Szrj file_ptr pos;
1897*a9fa9459Szrj
1898*a9fa9459Szrj /* We have already written out the last C_FILE
1899*a9fa9459Szrj symbol. We need to write it out again. We
1900*a9fa9459Szrj borrow *outsym temporarily. */
1901*a9fa9459Szrj bfd_coff_swap_sym_out (output_bfd,
1902*a9fa9459Szrj &flaginfo->last_file, outsym);
1903*a9fa9459Szrj pos = obj_sym_filepos (output_bfd);
1904*a9fa9459Szrj pos += flaginfo->last_file_index * osymesz;
1905*a9fa9459Szrj if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
1906*a9fa9459Szrj || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
1907*a9fa9459Szrj return FALSE;
1908*a9fa9459Szrj }
1909*a9fa9459Szrj }
1910*a9fa9459Szrj
1911*a9fa9459Szrj flaginfo->last_file_index = output_index;
1912*a9fa9459Szrj flaginfo->last_file = isym;
1913*a9fa9459Szrj break;
1914*a9fa9459Szrj }
1915*a9fa9459Szrj
1916*a9fa9459Szrj /* If doing task linking, convert normal global function symbols to
1917*a9fa9459Szrj static functions. */
1918*a9fa9459Szrj if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
1919*a9fa9459Szrj isym.n_sclass = C_STAT;
1920*a9fa9459Szrj
1921*a9fa9459Szrj /* Output the symbol. */
1922*a9fa9459Szrj bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
1923*a9fa9459Szrj
1924*a9fa9459Szrj *indexp = output_index;
1925*a9fa9459Szrj
1926*a9fa9459Szrj if (global)
1927*a9fa9459Szrj {
1928*a9fa9459Szrj long indx;
1929*a9fa9459Szrj struct coff_link_hash_entry *h;
1930*a9fa9459Szrj
1931*a9fa9459Szrj indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
1932*a9fa9459Szrj / isymesz);
1933*a9fa9459Szrj h = obj_coff_sym_hashes (input_bfd)[indx];
1934*a9fa9459Szrj if (h == NULL)
1935*a9fa9459Szrj {
1936*a9fa9459Szrj /* This can happen if there were errors earlier in
1937*a9fa9459Szrj the link. */
1938*a9fa9459Szrj bfd_set_error (bfd_error_bad_value);
1939*a9fa9459Szrj return FALSE;
1940*a9fa9459Szrj }
1941*a9fa9459Szrj h->indx = output_index;
1942*a9fa9459Szrj }
1943*a9fa9459Szrj
1944*a9fa9459Szrj output_index += add;
1945*a9fa9459Szrj outsym += add * osymesz;
1946*a9fa9459Szrj }
1947*a9fa9459Szrj
1948*a9fa9459Szrj esym += add * isymesz;
1949*a9fa9459Szrj isymp += add;
1950*a9fa9459Szrj ++secpp;
1951*a9fa9459Szrj ++indexp;
1952*a9fa9459Szrj for (--add; add > 0; --add)
1953*a9fa9459Szrj {
1954*a9fa9459Szrj *secpp++ = NULL;
1955*a9fa9459Szrj *indexp++ = -1;
1956*a9fa9459Szrj }
1957*a9fa9459Szrj }
1958*a9fa9459Szrj
1959*a9fa9459Szrj /* Fix up the aux entries. This must be done in a separate pass,
1960*a9fa9459Szrj because we don't know the correct symbol indices until we have
1961*a9fa9459Szrj already decided which symbols we are going to keep. */
1962*a9fa9459Szrj esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1963*a9fa9459Szrj esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
1964*a9fa9459Szrj isymp = flaginfo->internal_syms;
1965*a9fa9459Szrj indexp = flaginfo->sym_indices;
1966*a9fa9459Szrj sym_hash = obj_coff_sym_hashes (input_bfd);
1967*a9fa9459Szrj outsym = flaginfo->outsyms;
1968*a9fa9459Szrj
1969*a9fa9459Szrj while (esym < esym_end)
1970*a9fa9459Szrj {
1971*a9fa9459Szrj int add;
1972*a9fa9459Szrj
1973*a9fa9459Szrj add = 1 + isymp->n_numaux;
1974*a9fa9459Szrj
1975*a9fa9459Szrj if ((*indexp < 0
1976*a9fa9459Szrj || (bfd_size_type) *indexp < syment_base)
1977*a9fa9459Szrj && (*sym_hash == NULL
1978*a9fa9459Szrj || (*sym_hash)->auxbfd != input_bfd))
1979*a9fa9459Szrj esym += add * isymesz;
1980*a9fa9459Szrj else
1981*a9fa9459Szrj {
1982*a9fa9459Szrj struct coff_link_hash_entry *h;
1983*a9fa9459Szrj int i;
1984*a9fa9459Szrj
1985*a9fa9459Szrj h = NULL;
1986*a9fa9459Szrj if (*indexp < 0)
1987*a9fa9459Szrj {
1988*a9fa9459Szrj h = *sym_hash;
1989*a9fa9459Szrj
1990*a9fa9459Szrj /* The m68k-motorola-sysv assembler will sometimes
1991*a9fa9459Szrj generate two symbols with the same name, but only one
1992*a9fa9459Szrj will have aux entries. */
1993*a9fa9459Szrj BFD_ASSERT (isymp->n_numaux == 0
1994*a9fa9459Szrj || h->numaux == 0
1995*a9fa9459Szrj || h->numaux == isymp->n_numaux);
1996*a9fa9459Szrj }
1997*a9fa9459Szrj
1998*a9fa9459Szrj esym += isymesz;
1999*a9fa9459Szrj
2000*a9fa9459Szrj if (h == NULL)
2001*a9fa9459Szrj outsym += osymesz;
2002*a9fa9459Szrj
2003*a9fa9459Szrj /* Handle the aux entries. This handling is based on
2004*a9fa9459Szrj coff_pointerize_aux. I don't know if it always correct. */
2005*a9fa9459Szrj for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
2006*a9fa9459Szrj {
2007*a9fa9459Szrj union internal_auxent aux;
2008*a9fa9459Szrj union internal_auxent *auxp;
2009*a9fa9459Szrj
2010*a9fa9459Szrj if (h != NULL && h->aux != NULL && (h->numaux > i))
2011*a9fa9459Szrj auxp = h->aux + i;
2012*a9fa9459Szrj else
2013*a9fa9459Szrj {
2014*a9fa9459Szrj bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
2015*a9fa9459Szrj isymp->n_sclass, i, isymp->n_numaux, &aux);
2016*a9fa9459Szrj auxp = &aux;
2017*a9fa9459Szrj }
2018*a9fa9459Szrj
2019*a9fa9459Szrj if (isymp->n_sclass == C_FILE)
2020*a9fa9459Szrj {
2021*a9fa9459Szrj /* If this is a long filename, we must put it in the
2022*a9fa9459Szrj string table. */
2023*a9fa9459Szrj if (auxp->x_file.x_n.x_zeroes == 0
2024*a9fa9459Szrj && auxp->x_file.x_n.x_offset != 0)
2025*a9fa9459Szrj {
2026*a9fa9459Szrj const char *filename;
2027*a9fa9459Szrj bfd_size_type indx;
2028*a9fa9459Szrj
2029*a9fa9459Szrj BFD_ASSERT (auxp->x_file.x_n.x_offset
2030*a9fa9459Szrj >= STRING_SIZE_SIZE);
2031*a9fa9459Szrj if (strings == NULL)
2032*a9fa9459Szrj {
2033*a9fa9459Szrj strings = _bfd_coff_read_string_table (input_bfd);
2034*a9fa9459Szrj if (strings == NULL)
2035*a9fa9459Szrj return FALSE;
2036*a9fa9459Szrj }
2037*a9fa9459Szrj if ((bfd_size_type) auxp->x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd))
2038*a9fa9459Szrj filename = _("<corrupt>");
2039*a9fa9459Szrj else
2040*a9fa9459Szrj filename = strings + auxp->x_file.x_n.x_offset;
2041*a9fa9459Szrj indx = _bfd_stringtab_add (flaginfo->strtab, filename,
2042*a9fa9459Szrj hash, copy);
2043*a9fa9459Szrj if (indx == (bfd_size_type) -1)
2044*a9fa9459Szrj return FALSE;
2045*a9fa9459Szrj auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
2046*a9fa9459Szrj }
2047*a9fa9459Szrj }
2048*a9fa9459Szrj else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
2049*a9fa9459Szrj && isymp->n_sclass != C_NT_WEAK)
2050*a9fa9459Szrj {
2051*a9fa9459Szrj unsigned long indx;
2052*a9fa9459Szrj
2053*a9fa9459Szrj if (ISFCN (isymp->n_type)
2054*a9fa9459Szrj || ISTAG (isymp->n_sclass)
2055*a9fa9459Szrj || isymp->n_sclass == C_BLOCK
2056*a9fa9459Szrj || isymp->n_sclass == C_FCN)
2057*a9fa9459Szrj {
2058*a9fa9459Szrj indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
2059*a9fa9459Szrj if (indx > 0
2060*a9fa9459Szrj && indx < obj_raw_syment_count (input_bfd))
2061*a9fa9459Szrj {
2062*a9fa9459Szrj /* We look forward through the symbol for
2063*a9fa9459Szrj the index of the next symbol we are going
2064*a9fa9459Szrj to include. I don't know if this is
2065*a9fa9459Szrj entirely right. */
2066*a9fa9459Szrj while ((flaginfo->sym_indices[indx] < 0
2067*a9fa9459Szrj || ((bfd_size_type) flaginfo->sym_indices[indx]
2068*a9fa9459Szrj < syment_base))
2069*a9fa9459Szrj && indx < obj_raw_syment_count (input_bfd))
2070*a9fa9459Szrj ++indx;
2071*a9fa9459Szrj if (indx >= obj_raw_syment_count (input_bfd))
2072*a9fa9459Szrj indx = output_index;
2073*a9fa9459Szrj else
2074*a9fa9459Szrj indx = flaginfo->sym_indices[indx];
2075*a9fa9459Szrj auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
2076*a9fa9459Szrj }
2077*a9fa9459Szrj }
2078*a9fa9459Szrj
2079*a9fa9459Szrj indx = auxp->x_sym.x_tagndx.l;
2080*a9fa9459Szrj if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
2081*a9fa9459Szrj {
2082*a9fa9459Szrj long symindx;
2083*a9fa9459Szrj
2084*a9fa9459Szrj symindx = flaginfo->sym_indices[indx];
2085*a9fa9459Szrj if (symindx < 0)
2086*a9fa9459Szrj auxp->x_sym.x_tagndx.l = 0;
2087*a9fa9459Szrj else
2088*a9fa9459Szrj auxp->x_sym.x_tagndx.l = symindx;
2089*a9fa9459Szrj }
2090*a9fa9459Szrj
2091*a9fa9459Szrj /* The .bf symbols are supposed to be linked through
2092*a9fa9459Szrj the endndx field. We need to carry this list
2093*a9fa9459Szrj across object files. */
2094*a9fa9459Szrj if (i == 0
2095*a9fa9459Szrj && h == NULL
2096*a9fa9459Szrj && isymp->n_sclass == C_FCN
2097*a9fa9459Szrj && (isymp->_n._n_n._n_zeroes != 0
2098*a9fa9459Szrj || isymp->_n._n_n._n_offset == 0)
2099*a9fa9459Szrj && isymp->_n._n_name[0] == '.'
2100*a9fa9459Szrj && isymp->_n._n_name[1] == 'b'
2101*a9fa9459Szrj && isymp->_n._n_name[2] == 'f'
2102*a9fa9459Szrj && isymp->_n._n_name[3] == '\0')
2103*a9fa9459Szrj {
2104*a9fa9459Szrj if (flaginfo->last_bf_index != -1)
2105*a9fa9459Szrj {
2106*a9fa9459Szrj flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
2107*a9fa9459Szrj *indexp;
2108*a9fa9459Szrj
2109*a9fa9459Szrj if ((bfd_size_type) flaginfo->last_bf_index
2110*a9fa9459Szrj >= syment_base)
2111*a9fa9459Szrj {
2112*a9fa9459Szrj void *auxout;
2113*a9fa9459Szrj
2114*a9fa9459Szrj /* The last .bf symbol is in this input
2115*a9fa9459Szrj file. This will only happen if the
2116*a9fa9459Szrj assembler did not set up the .bf
2117*a9fa9459Szrj endndx symbols correctly. */
2118*a9fa9459Szrj auxout = (flaginfo->outsyms
2119*a9fa9459Szrj + ((flaginfo->last_bf_index
2120*a9fa9459Szrj - syment_base)
2121*a9fa9459Szrj * osymesz));
2122*a9fa9459Szrj
2123*a9fa9459Szrj bfd_coff_swap_aux_out (output_bfd,
2124*a9fa9459Szrj &flaginfo->last_bf,
2125*a9fa9459Szrj isymp->n_type,
2126*a9fa9459Szrj isymp->n_sclass,
2127*a9fa9459Szrj 0, isymp->n_numaux,
2128*a9fa9459Szrj auxout);
2129*a9fa9459Szrj }
2130*a9fa9459Szrj else
2131*a9fa9459Szrj {
2132*a9fa9459Szrj file_ptr pos;
2133*a9fa9459Szrj
2134*a9fa9459Szrj /* We have already written out the last
2135*a9fa9459Szrj .bf aux entry. We need to write it
2136*a9fa9459Szrj out again. We borrow *outsym
2137*a9fa9459Szrj temporarily. FIXME: This case should
2138*a9fa9459Szrj be made faster. */
2139*a9fa9459Szrj bfd_coff_swap_aux_out (output_bfd,
2140*a9fa9459Szrj &flaginfo->last_bf,
2141*a9fa9459Szrj isymp->n_type,
2142*a9fa9459Szrj isymp->n_sclass,
2143*a9fa9459Szrj 0, isymp->n_numaux,
2144*a9fa9459Szrj outsym);
2145*a9fa9459Szrj pos = obj_sym_filepos (output_bfd);
2146*a9fa9459Szrj pos += flaginfo->last_bf_index * osymesz;
2147*a9fa9459Szrj if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2148*a9fa9459Szrj || (bfd_bwrite (outsym, osymesz, output_bfd)
2149*a9fa9459Szrj != osymesz))
2150*a9fa9459Szrj return FALSE;
2151*a9fa9459Szrj }
2152*a9fa9459Szrj }
2153*a9fa9459Szrj
2154*a9fa9459Szrj if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
2155*a9fa9459Szrj flaginfo->last_bf_index = -1;
2156*a9fa9459Szrj else
2157*a9fa9459Szrj {
2158*a9fa9459Szrj /* The endndx field of this aux entry must
2159*a9fa9459Szrj be updated with the symbol number of the
2160*a9fa9459Szrj next .bf symbol. */
2161*a9fa9459Szrj flaginfo->last_bf = *auxp;
2162*a9fa9459Szrj flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
2163*a9fa9459Szrj / osymesz)
2164*a9fa9459Szrj + syment_base);
2165*a9fa9459Szrj }
2166*a9fa9459Szrj }
2167*a9fa9459Szrj }
2168*a9fa9459Szrj
2169*a9fa9459Szrj if (h == NULL)
2170*a9fa9459Szrj {
2171*a9fa9459Szrj bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
2172*a9fa9459Szrj isymp->n_sclass, i, isymp->n_numaux,
2173*a9fa9459Szrj outsym);
2174*a9fa9459Szrj outsym += osymesz;
2175*a9fa9459Szrj }
2176*a9fa9459Szrj
2177*a9fa9459Szrj esym += isymesz;
2178*a9fa9459Szrj }
2179*a9fa9459Szrj }
2180*a9fa9459Szrj
2181*a9fa9459Szrj indexp += add;
2182*a9fa9459Szrj isymp += add;
2183*a9fa9459Szrj sym_hash += add;
2184*a9fa9459Szrj }
2185*a9fa9459Szrj
2186*a9fa9459Szrj /* Relocate the line numbers, unless we are stripping them. */
2187*a9fa9459Szrj if (flaginfo->info->strip == strip_none
2188*a9fa9459Szrj || flaginfo->info->strip == strip_some)
2189*a9fa9459Szrj {
2190*a9fa9459Szrj for (o = input_bfd->sections; o != NULL; o = o->next)
2191*a9fa9459Szrj {
2192*a9fa9459Szrj bfd_vma offset;
2193*a9fa9459Szrj bfd_byte *eline;
2194*a9fa9459Szrj bfd_byte *elineend;
2195*a9fa9459Szrj bfd_byte *oeline;
2196*a9fa9459Szrj bfd_boolean skipping;
2197*a9fa9459Szrj file_ptr pos;
2198*a9fa9459Szrj bfd_size_type amt;
2199*a9fa9459Szrj
2200*a9fa9459Szrj /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2201*a9fa9459Szrj build_link_order in ldwrite.c will not have created a
2202*a9fa9459Szrj link order, which means that we will not have seen this
2203*a9fa9459Szrj input section in _bfd_coff_final_link, which means that
2204*a9fa9459Szrj we will not have allocated space for the line numbers of
2205*a9fa9459Szrj this section. I don't think line numbers can be
2206*a9fa9459Szrj meaningful for a section which does not have
2207*a9fa9459Szrj SEC_HAS_CONTENTS set, but, if they do, this must be
2208*a9fa9459Szrj changed. */
2209*a9fa9459Szrj if (o->lineno_count == 0
2210*a9fa9459Szrj || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
2211*a9fa9459Szrj continue;
2212*a9fa9459Szrj
2213*a9fa9459Szrj if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
2214*a9fa9459Szrj || bfd_bread (flaginfo->linenos, linesz * o->lineno_count,
2215*a9fa9459Szrj input_bfd) != linesz * o->lineno_count)
2216*a9fa9459Szrj return FALSE;
2217*a9fa9459Szrj
2218*a9fa9459Szrj offset = o->output_section->vma + o->output_offset - o->vma;
2219*a9fa9459Szrj eline = flaginfo->linenos;
2220*a9fa9459Szrj oeline = flaginfo->linenos;
2221*a9fa9459Szrj elineend = eline + linesz * o->lineno_count;
2222*a9fa9459Szrj skipping = FALSE;
2223*a9fa9459Szrj for (; eline < elineend; eline += linesz)
2224*a9fa9459Szrj {
2225*a9fa9459Szrj struct internal_lineno iline;
2226*a9fa9459Szrj
2227*a9fa9459Szrj bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
2228*a9fa9459Szrj
2229*a9fa9459Szrj if (iline.l_lnno != 0)
2230*a9fa9459Szrj iline.l_addr.l_paddr += offset;
2231*a9fa9459Szrj else if (iline.l_addr.l_symndx >= 0
2232*a9fa9459Szrj && ((unsigned long) iline.l_addr.l_symndx
2233*a9fa9459Szrj < obj_raw_syment_count (input_bfd)))
2234*a9fa9459Szrj {
2235*a9fa9459Szrj long indx;
2236*a9fa9459Szrj
2237*a9fa9459Szrj indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
2238*a9fa9459Szrj
2239*a9fa9459Szrj if (indx < 0)
2240*a9fa9459Szrj {
2241*a9fa9459Szrj /* These line numbers are attached to a symbol
2242*a9fa9459Szrj which we are stripping. We must discard the
2243*a9fa9459Szrj line numbers because reading them back with
2244*a9fa9459Szrj no associated symbol (or associating them all
2245*a9fa9459Szrj with symbol #0) will fail. We can't regain
2246*a9fa9459Szrj the space in the output file, but at least
2247*a9fa9459Szrj they're dense. */
2248*a9fa9459Szrj skipping = TRUE;
2249*a9fa9459Szrj }
2250*a9fa9459Szrj else
2251*a9fa9459Szrj {
2252*a9fa9459Szrj struct internal_syment is;
2253*a9fa9459Szrj union internal_auxent ia;
2254*a9fa9459Szrj
2255*a9fa9459Szrj /* Fix up the lnnoptr field in the aux entry of
2256*a9fa9459Szrj the symbol. It turns out that we can't do
2257*a9fa9459Szrj this when we modify the symbol aux entries,
2258*a9fa9459Szrj because gas sometimes screws up the lnnoptr
2259*a9fa9459Szrj field and makes it an offset from the start
2260*a9fa9459Szrj of the line numbers rather than an absolute
2261*a9fa9459Szrj file index. */
2262*a9fa9459Szrj bfd_coff_swap_sym_in (output_bfd,
2263*a9fa9459Szrj (flaginfo->outsyms
2264*a9fa9459Szrj + ((indx - syment_base)
2265*a9fa9459Szrj * osymesz)), &is);
2266*a9fa9459Szrj if ((ISFCN (is.n_type)
2267*a9fa9459Szrj || is.n_sclass == C_BLOCK)
2268*a9fa9459Szrj && is.n_numaux >= 1)
2269*a9fa9459Szrj {
2270*a9fa9459Szrj void *auxptr;
2271*a9fa9459Szrj
2272*a9fa9459Szrj auxptr = (flaginfo->outsyms
2273*a9fa9459Szrj + ((indx - syment_base + 1)
2274*a9fa9459Szrj * osymesz));
2275*a9fa9459Szrj bfd_coff_swap_aux_in (output_bfd, auxptr,
2276*a9fa9459Szrj is.n_type, is.n_sclass,
2277*a9fa9459Szrj 0, is.n_numaux, &ia);
2278*a9fa9459Szrj ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
2279*a9fa9459Szrj (o->output_section->line_filepos
2280*a9fa9459Szrj + o->output_section->lineno_count * linesz
2281*a9fa9459Szrj + eline - flaginfo->linenos);
2282*a9fa9459Szrj bfd_coff_swap_aux_out (output_bfd, &ia,
2283*a9fa9459Szrj is.n_type, is.n_sclass, 0,
2284*a9fa9459Szrj is.n_numaux, auxptr);
2285*a9fa9459Szrj }
2286*a9fa9459Szrj
2287*a9fa9459Szrj skipping = FALSE;
2288*a9fa9459Szrj }
2289*a9fa9459Szrj
2290*a9fa9459Szrj iline.l_addr.l_symndx = indx;
2291*a9fa9459Szrj }
2292*a9fa9459Szrj
2293*a9fa9459Szrj if (!skipping)
2294*a9fa9459Szrj {
2295*a9fa9459Szrj bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
2296*a9fa9459Szrj oeline += linesz;
2297*a9fa9459Szrj }
2298*a9fa9459Szrj }
2299*a9fa9459Szrj
2300*a9fa9459Szrj pos = o->output_section->line_filepos;
2301*a9fa9459Szrj pos += o->output_section->lineno_count * linesz;
2302*a9fa9459Szrj amt = oeline - flaginfo->linenos;
2303*a9fa9459Szrj if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2304*a9fa9459Szrj || bfd_bwrite (flaginfo->linenos, amt, output_bfd) != amt)
2305*a9fa9459Szrj return FALSE;
2306*a9fa9459Szrj
2307*a9fa9459Szrj o->output_section->lineno_count += amt / linesz;
2308*a9fa9459Szrj }
2309*a9fa9459Szrj }
2310*a9fa9459Szrj
2311*a9fa9459Szrj /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2312*a9fa9459Szrj symbol will be the first symbol in the next input file. In the
2313*a9fa9459Szrj normal case, this will save us from writing out the C_FILE symbol
2314*a9fa9459Szrj again. */
2315*a9fa9459Szrj if (flaginfo->last_file_index != -1
2316*a9fa9459Szrj && (bfd_size_type) flaginfo->last_file_index >= syment_base)
2317*a9fa9459Szrj {
2318*a9fa9459Szrj flaginfo->last_file.n_value = output_index;
2319*a9fa9459Szrj bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
2320*a9fa9459Szrj (flaginfo->outsyms
2321*a9fa9459Szrj + ((flaginfo->last_file_index - syment_base)
2322*a9fa9459Szrj * osymesz)));
2323*a9fa9459Szrj }
2324*a9fa9459Szrj
2325*a9fa9459Szrj /* Write the modified symbols to the output file. */
2326*a9fa9459Szrj if (outsym > flaginfo->outsyms)
2327*a9fa9459Szrj {
2328*a9fa9459Szrj file_ptr pos;
2329*a9fa9459Szrj bfd_size_type amt;
2330*a9fa9459Szrj
2331*a9fa9459Szrj pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
2332*a9fa9459Szrj amt = outsym - flaginfo->outsyms;
2333*a9fa9459Szrj if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2334*a9fa9459Szrj || bfd_bwrite (flaginfo->outsyms, amt, output_bfd) != amt)
2335*a9fa9459Szrj return FALSE;
2336*a9fa9459Szrj
2337*a9fa9459Szrj BFD_ASSERT ((obj_raw_syment_count (output_bfd)
2338*a9fa9459Szrj + (outsym - flaginfo->outsyms) / osymesz)
2339*a9fa9459Szrj == output_index);
2340*a9fa9459Szrj
2341*a9fa9459Szrj obj_raw_syment_count (output_bfd) = output_index;
2342*a9fa9459Szrj }
2343*a9fa9459Szrj
2344*a9fa9459Szrj /* Relocate the contents of each section. */
2345*a9fa9459Szrj adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
2346*a9fa9459Szrj for (o = input_bfd->sections; o != NULL; o = o->next)
2347*a9fa9459Szrj {
2348*a9fa9459Szrj bfd_byte *contents;
2349*a9fa9459Szrj struct coff_section_tdata *secdata;
2350*a9fa9459Szrj
2351*a9fa9459Szrj if (! o->linker_mark)
2352*a9fa9459Szrj /* This section was omitted from the link. */
2353*a9fa9459Szrj continue;
2354*a9fa9459Szrj
2355*a9fa9459Szrj if ((o->flags & SEC_LINKER_CREATED) != 0)
2356*a9fa9459Szrj continue;
2357*a9fa9459Szrj
2358*a9fa9459Szrj if ((o->flags & SEC_HAS_CONTENTS) == 0
2359*a9fa9459Szrj || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
2360*a9fa9459Szrj {
2361*a9fa9459Szrj if ((o->flags & SEC_RELOC) != 0
2362*a9fa9459Szrj && o->reloc_count != 0)
2363*a9fa9459Szrj {
2364*a9fa9459Szrj (*_bfd_error_handler)
2365*a9fa9459Szrj (_("%B: relocs in section `%A', but it has no contents"),
2366*a9fa9459Szrj input_bfd, o);
2367*a9fa9459Szrj bfd_set_error (bfd_error_no_contents);
2368*a9fa9459Szrj return FALSE;
2369*a9fa9459Szrj }
2370*a9fa9459Szrj
2371*a9fa9459Szrj continue;
2372*a9fa9459Szrj }
2373*a9fa9459Szrj
2374*a9fa9459Szrj secdata = coff_section_data (input_bfd, o);
2375*a9fa9459Szrj if (secdata != NULL && secdata->contents != NULL)
2376*a9fa9459Szrj contents = secdata->contents;
2377*a9fa9459Szrj else
2378*a9fa9459Szrj {
2379*a9fa9459Szrj contents = flaginfo->contents;
2380*a9fa9459Szrj if (! bfd_get_full_section_contents (input_bfd, o, &contents))
2381*a9fa9459Szrj return FALSE;
2382*a9fa9459Szrj }
2383*a9fa9459Szrj
2384*a9fa9459Szrj if ((o->flags & SEC_RELOC) != 0)
2385*a9fa9459Szrj {
2386*a9fa9459Szrj int target_index;
2387*a9fa9459Szrj struct internal_reloc *internal_relocs;
2388*a9fa9459Szrj struct internal_reloc *irel;
2389*a9fa9459Szrj
2390*a9fa9459Szrj /* Read in the relocs. */
2391*a9fa9459Szrj target_index = o->output_section->target_index;
2392*a9fa9459Szrj internal_relocs = (_bfd_coff_read_internal_relocs
2393*a9fa9459Szrj (input_bfd, o, FALSE, flaginfo->external_relocs,
2394*a9fa9459Szrj bfd_link_relocatable (flaginfo->info),
2395*a9fa9459Szrj (bfd_link_relocatable (flaginfo->info)
2396*a9fa9459Szrj ? (flaginfo->section_info[target_index].relocs
2397*a9fa9459Szrj + o->output_section->reloc_count)
2398*a9fa9459Szrj : flaginfo->internal_relocs)));
2399*a9fa9459Szrj if (internal_relocs == NULL
2400*a9fa9459Szrj && o->reloc_count > 0)
2401*a9fa9459Szrj return FALSE;
2402*a9fa9459Szrj
2403*a9fa9459Szrj /* Run through the relocs looking for relocs against symbols
2404*a9fa9459Szrj coming from discarded sections and complain about them. */
2405*a9fa9459Szrj irel = internal_relocs;
2406*a9fa9459Szrj for (; irel < &internal_relocs[o->reloc_count]; irel++)
2407*a9fa9459Szrj {
2408*a9fa9459Szrj struct coff_link_hash_entry *h;
2409*a9fa9459Szrj asection *ps = NULL;
2410*a9fa9459Szrj long symndx = irel->r_symndx;
2411*a9fa9459Szrj if (symndx < 0)
2412*a9fa9459Szrj continue;
2413*a9fa9459Szrj h = obj_coff_sym_hashes (input_bfd)[symndx];
2414*a9fa9459Szrj if (h == NULL)
2415*a9fa9459Szrj continue;
2416*a9fa9459Szrj while (h->root.type == bfd_link_hash_indirect
2417*a9fa9459Szrj || h->root.type == bfd_link_hash_warning)
2418*a9fa9459Szrj h = (struct coff_link_hash_entry *) h->root.u.i.link;
2419*a9fa9459Szrj if (h->root.type == bfd_link_hash_defined
2420*a9fa9459Szrj || h->root.type == bfd_link_hash_defweak)
2421*a9fa9459Szrj ps = h->root.u.def.section;
2422*a9fa9459Szrj if (ps == NULL)
2423*a9fa9459Szrj continue;
2424*a9fa9459Szrj /* Complain if definition comes from an excluded section. */
2425*a9fa9459Szrj if (ps->flags & SEC_EXCLUDE)
2426*a9fa9459Szrj (*flaginfo->info->callbacks->einfo)
2427*a9fa9459Szrj (_("%X`%s' referenced in section `%A' of %B: "
2428*a9fa9459Szrj "defined in discarded section `%A' of %B\n"),
2429*a9fa9459Szrj h->root.root.string, o, input_bfd, ps, ps->owner);
2430*a9fa9459Szrj }
2431*a9fa9459Szrj
2432*a9fa9459Szrj /* Call processor specific code to relocate the section
2433*a9fa9459Szrj contents. */
2434*a9fa9459Szrj if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
2435*a9fa9459Szrj input_bfd, o,
2436*a9fa9459Szrj contents,
2437*a9fa9459Szrj internal_relocs,
2438*a9fa9459Szrj flaginfo->internal_syms,
2439*a9fa9459Szrj flaginfo->sec_ptrs))
2440*a9fa9459Szrj return FALSE;
2441*a9fa9459Szrj
2442*a9fa9459Szrj if (bfd_link_relocatable (flaginfo->info))
2443*a9fa9459Szrj {
2444*a9fa9459Szrj bfd_vma offset;
2445*a9fa9459Szrj struct internal_reloc *irelend;
2446*a9fa9459Szrj struct coff_link_hash_entry **rel_hash;
2447*a9fa9459Szrj
2448*a9fa9459Szrj offset = o->output_section->vma + o->output_offset - o->vma;
2449*a9fa9459Szrj irel = internal_relocs;
2450*a9fa9459Szrj irelend = irel + o->reloc_count;
2451*a9fa9459Szrj rel_hash = (flaginfo->section_info[target_index].rel_hashes
2452*a9fa9459Szrj + o->output_section->reloc_count);
2453*a9fa9459Szrj for (; irel < irelend; irel++, rel_hash++)
2454*a9fa9459Szrj {
2455*a9fa9459Szrj struct coff_link_hash_entry *h;
2456*a9fa9459Szrj bfd_boolean adjusted;
2457*a9fa9459Szrj
2458*a9fa9459Szrj *rel_hash = NULL;
2459*a9fa9459Szrj
2460*a9fa9459Szrj /* Adjust the reloc address and symbol index. */
2461*a9fa9459Szrj irel->r_vaddr += offset;
2462*a9fa9459Szrj
2463*a9fa9459Szrj if (irel->r_symndx == -1)
2464*a9fa9459Szrj continue;
2465*a9fa9459Szrj
2466*a9fa9459Szrj if (adjust_symndx)
2467*a9fa9459Szrj {
2468*a9fa9459Szrj if (! (*adjust_symndx) (output_bfd, flaginfo->info,
2469*a9fa9459Szrj input_bfd, o, irel,
2470*a9fa9459Szrj &adjusted))
2471*a9fa9459Szrj return FALSE;
2472*a9fa9459Szrj if (adjusted)
2473*a9fa9459Szrj continue;
2474*a9fa9459Szrj }
2475*a9fa9459Szrj
2476*a9fa9459Szrj h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
2477*a9fa9459Szrj if (h != NULL)
2478*a9fa9459Szrj {
2479*a9fa9459Szrj /* This is a global symbol. */
2480*a9fa9459Szrj if (h->indx >= 0)
2481*a9fa9459Szrj irel->r_symndx = h->indx;
2482*a9fa9459Szrj else
2483*a9fa9459Szrj {
2484*a9fa9459Szrj /* This symbol is being written at the end
2485*a9fa9459Szrj of the file, and we do not yet know the
2486*a9fa9459Szrj symbol index. We save the pointer to the
2487*a9fa9459Szrj hash table entry in the rel_hash list.
2488*a9fa9459Szrj We set the indx field to -2 to indicate
2489*a9fa9459Szrj that this symbol must not be stripped. */
2490*a9fa9459Szrj *rel_hash = h;
2491*a9fa9459Szrj h->indx = -2;
2492*a9fa9459Szrj }
2493*a9fa9459Szrj }
2494*a9fa9459Szrj else
2495*a9fa9459Szrj {
2496*a9fa9459Szrj long indx;
2497*a9fa9459Szrj
2498*a9fa9459Szrj indx = flaginfo->sym_indices[irel->r_symndx];
2499*a9fa9459Szrj if (indx != -1)
2500*a9fa9459Szrj irel->r_symndx = indx;
2501*a9fa9459Szrj else
2502*a9fa9459Szrj {
2503*a9fa9459Szrj struct internal_syment *is;
2504*a9fa9459Szrj const char *name;
2505*a9fa9459Szrj char buf[SYMNMLEN + 1];
2506*a9fa9459Szrj
2507*a9fa9459Szrj /* This reloc is against a symbol we are
2508*a9fa9459Szrj stripping. This should have been handled
2509*a9fa9459Szrj by the 'dont_skip_symbol' code in the while
2510*a9fa9459Szrj loop at the top of this function. */
2511*a9fa9459Szrj is = flaginfo->internal_syms + irel->r_symndx;
2512*a9fa9459Szrj
2513*a9fa9459Szrj name = (_bfd_coff_internal_syment_name
2514*a9fa9459Szrj (input_bfd, is, buf));
2515*a9fa9459Szrj if (name == NULL)
2516*a9fa9459Szrj return FALSE;
2517*a9fa9459Szrj
2518*a9fa9459Szrj (*flaginfo->info->callbacks->unattached_reloc)
2519*a9fa9459Szrj (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
2520*a9fa9459Szrj }
2521*a9fa9459Szrj }
2522*a9fa9459Szrj }
2523*a9fa9459Szrj
2524*a9fa9459Szrj o->output_section->reloc_count += o->reloc_count;
2525*a9fa9459Szrj }
2526*a9fa9459Szrj }
2527*a9fa9459Szrj
2528*a9fa9459Szrj /* Write out the modified section contents. */
2529*a9fa9459Szrj if (secdata == NULL || secdata->stab_info == NULL)
2530*a9fa9459Szrj {
2531*a9fa9459Szrj file_ptr loc = o->output_offset * bfd_octets_per_byte (output_bfd);
2532*a9fa9459Szrj if (! bfd_set_section_contents (output_bfd, o->output_section,
2533*a9fa9459Szrj contents, loc, o->size))
2534*a9fa9459Szrj return FALSE;
2535*a9fa9459Szrj }
2536*a9fa9459Szrj else
2537*a9fa9459Szrj {
2538*a9fa9459Szrj if (! (_bfd_write_section_stabs
2539*a9fa9459Szrj (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
2540*a9fa9459Szrj o, &secdata->stab_info, contents)))
2541*a9fa9459Szrj return FALSE;
2542*a9fa9459Szrj }
2543*a9fa9459Szrj }
2544*a9fa9459Szrj
2545*a9fa9459Szrj if (! flaginfo->info->keep_memory
2546*a9fa9459Szrj && ! _bfd_coff_free_symbols (input_bfd))
2547*a9fa9459Szrj return FALSE;
2548*a9fa9459Szrj
2549*a9fa9459Szrj return TRUE;
2550*a9fa9459Szrj }
2551*a9fa9459Szrj
2552*a9fa9459Szrj /* Write out a global symbol. Called via bfd_hash_traverse. */
2553*a9fa9459Szrj
2554*a9fa9459Szrj bfd_boolean
_bfd_coff_write_global_sym(struct bfd_hash_entry * bh,void * data)2555*a9fa9459Szrj _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
2556*a9fa9459Szrj {
2557*a9fa9459Szrj struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
2558*a9fa9459Szrj struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2559*a9fa9459Szrj bfd *output_bfd;
2560*a9fa9459Szrj struct internal_syment isym;
2561*a9fa9459Szrj bfd_size_type symesz;
2562*a9fa9459Szrj unsigned int i;
2563*a9fa9459Szrj file_ptr pos;
2564*a9fa9459Szrj
2565*a9fa9459Szrj output_bfd = flaginfo->output_bfd;
2566*a9fa9459Szrj
2567*a9fa9459Szrj if (h->root.type == bfd_link_hash_warning)
2568*a9fa9459Szrj {
2569*a9fa9459Szrj h = (struct coff_link_hash_entry *) h->root.u.i.link;
2570*a9fa9459Szrj if (h->root.type == bfd_link_hash_new)
2571*a9fa9459Szrj return TRUE;
2572*a9fa9459Szrj }
2573*a9fa9459Szrj
2574*a9fa9459Szrj if (h->indx >= 0)
2575*a9fa9459Szrj return TRUE;
2576*a9fa9459Szrj
2577*a9fa9459Szrj if (h->indx != -2
2578*a9fa9459Szrj && (flaginfo->info->strip == strip_all
2579*a9fa9459Szrj || (flaginfo->info->strip == strip_some
2580*a9fa9459Szrj && (bfd_hash_lookup (flaginfo->info->keep_hash,
2581*a9fa9459Szrj h->root.root.string, FALSE, FALSE)
2582*a9fa9459Szrj == NULL))))
2583*a9fa9459Szrj return TRUE;
2584*a9fa9459Szrj
2585*a9fa9459Szrj switch (h->root.type)
2586*a9fa9459Szrj {
2587*a9fa9459Szrj default:
2588*a9fa9459Szrj case bfd_link_hash_new:
2589*a9fa9459Szrj case bfd_link_hash_warning:
2590*a9fa9459Szrj abort ();
2591*a9fa9459Szrj return FALSE;
2592*a9fa9459Szrj
2593*a9fa9459Szrj case bfd_link_hash_undefined:
2594*a9fa9459Szrj case bfd_link_hash_undefweak:
2595*a9fa9459Szrj isym.n_scnum = N_UNDEF;
2596*a9fa9459Szrj isym.n_value = 0;
2597*a9fa9459Szrj break;
2598*a9fa9459Szrj
2599*a9fa9459Szrj case bfd_link_hash_defined:
2600*a9fa9459Szrj case bfd_link_hash_defweak:
2601*a9fa9459Szrj {
2602*a9fa9459Szrj asection *sec;
2603*a9fa9459Szrj
2604*a9fa9459Szrj sec = h->root.u.def.section->output_section;
2605*a9fa9459Szrj if (bfd_is_abs_section (sec))
2606*a9fa9459Szrj isym.n_scnum = N_ABS;
2607*a9fa9459Szrj else
2608*a9fa9459Szrj isym.n_scnum = sec->target_index;
2609*a9fa9459Szrj isym.n_value = (h->root.u.def.value
2610*a9fa9459Szrj + h->root.u.def.section->output_offset);
2611*a9fa9459Szrj if (! obj_pe (flaginfo->output_bfd))
2612*a9fa9459Szrj isym.n_value += sec->vma;
2613*a9fa9459Szrj }
2614*a9fa9459Szrj break;
2615*a9fa9459Szrj
2616*a9fa9459Szrj case bfd_link_hash_common:
2617*a9fa9459Szrj isym.n_scnum = N_UNDEF;
2618*a9fa9459Szrj isym.n_value = h->root.u.c.size;
2619*a9fa9459Szrj break;
2620*a9fa9459Szrj
2621*a9fa9459Szrj case bfd_link_hash_indirect:
2622*a9fa9459Szrj /* Just ignore these. They can't be handled anyhow. */
2623*a9fa9459Szrj return TRUE;
2624*a9fa9459Szrj }
2625*a9fa9459Szrj
2626*a9fa9459Szrj if (strlen (h->root.root.string) <= SYMNMLEN)
2627*a9fa9459Szrj strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
2628*a9fa9459Szrj else
2629*a9fa9459Szrj {
2630*a9fa9459Szrj bfd_boolean hash;
2631*a9fa9459Szrj bfd_size_type indx;
2632*a9fa9459Szrj
2633*a9fa9459Szrj hash = TRUE;
2634*a9fa9459Szrj if (flaginfo->info->traditional_format)
2635*a9fa9459Szrj hash = FALSE;
2636*a9fa9459Szrj indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
2637*a9fa9459Szrj FALSE);
2638*a9fa9459Szrj if (indx == (bfd_size_type) -1)
2639*a9fa9459Szrj {
2640*a9fa9459Szrj flaginfo->failed = TRUE;
2641*a9fa9459Szrj return FALSE;
2642*a9fa9459Szrj }
2643*a9fa9459Szrj isym._n._n_n._n_zeroes = 0;
2644*a9fa9459Szrj isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
2645*a9fa9459Szrj }
2646*a9fa9459Szrj
2647*a9fa9459Szrj isym.n_sclass = h->symbol_class;
2648*a9fa9459Szrj isym.n_type = h->type;
2649*a9fa9459Szrj
2650*a9fa9459Szrj if (isym.n_sclass == C_NULL)
2651*a9fa9459Szrj isym.n_sclass = C_EXT;
2652*a9fa9459Szrj
2653*a9fa9459Szrj /* If doing task linking and this is the pass where we convert
2654*a9fa9459Szrj defined globals to statics, then do that conversion now. If the
2655*a9fa9459Szrj symbol is not being converted, just ignore it and it will be
2656*a9fa9459Szrj output during a later pass. */
2657*a9fa9459Szrj if (flaginfo->global_to_static)
2658*a9fa9459Szrj {
2659*a9fa9459Szrj if (! IS_EXTERNAL (output_bfd, isym))
2660*a9fa9459Szrj return TRUE;
2661*a9fa9459Szrj
2662*a9fa9459Szrj isym.n_sclass = C_STAT;
2663*a9fa9459Szrj }
2664*a9fa9459Szrj
2665*a9fa9459Szrj /* When a weak symbol is not overridden by a strong one,
2666*a9fa9459Szrj turn it into an external symbol when not building a
2667*a9fa9459Szrj shared or relocatable object. */
2668*a9fa9459Szrj if (! bfd_link_pic (flaginfo->info)
2669*a9fa9459Szrj && ! bfd_link_relocatable (flaginfo->info)
2670*a9fa9459Szrj && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
2671*a9fa9459Szrj isym.n_sclass = C_EXT;
2672*a9fa9459Szrj
2673*a9fa9459Szrj isym.n_numaux = h->numaux;
2674*a9fa9459Szrj
2675*a9fa9459Szrj bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
2676*a9fa9459Szrj
2677*a9fa9459Szrj symesz = bfd_coff_symesz (output_bfd);
2678*a9fa9459Szrj
2679*a9fa9459Szrj pos = obj_sym_filepos (output_bfd);
2680*a9fa9459Szrj pos += obj_raw_syment_count (output_bfd) * symesz;
2681*a9fa9459Szrj if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
2682*a9fa9459Szrj || bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
2683*a9fa9459Szrj {
2684*a9fa9459Szrj flaginfo->failed = TRUE;
2685*a9fa9459Szrj return FALSE;
2686*a9fa9459Szrj }
2687*a9fa9459Szrj
2688*a9fa9459Szrj h->indx = obj_raw_syment_count (output_bfd);
2689*a9fa9459Szrj
2690*a9fa9459Szrj ++obj_raw_syment_count (output_bfd);
2691*a9fa9459Szrj
2692*a9fa9459Szrj /* Write out any associated aux entries. Most of the aux entries
2693*a9fa9459Szrj will have been modified in _bfd_coff_link_input_bfd. We have to
2694*a9fa9459Szrj handle section aux entries here, now that we have the final
2695*a9fa9459Szrj relocation and line number counts. */
2696*a9fa9459Szrj for (i = 0; i < isym.n_numaux; i++)
2697*a9fa9459Szrj {
2698*a9fa9459Szrj union internal_auxent *auxp;
2699*a9fa9459Szrj
2700*a9fa9459Szrj auxp = h->aux + i;
2701*a9fa9459Szrj
2702*a9fa9459Szrj /* Look for a section aux entry here using the same tests that
2703*a9fa9459Szrj coff_swap_aux_out uses. */
2704*a9fa9459Szrj if (i == 0
2705*a9fa9459Szrj && (isym.n_sclass == C_STAT
2706*a9fa9459Szrj || isym.n_sclass == C_HIDDEN)
2707*a9fa9459Szrj && isym.n_type == T_NULL
2708*a9fa9459Szrj && (h->root.type == bfd_link_hash_defined
2709*a9fa9459Szrj || h->root.type == bfd_link_hash_defweak))
2710*a9fa9459Szrj {
2711*a9fa9459Szrj asection *sec;
2712*a9fa9459Szrj
2713*a9fa9459Szrj sec = h->root.u.def.section->output_section;
2714*a9fa9459Szrj if (sec != NULL)
2715*a9fa9459Szrj {
2716*a9fa9459Szrj auxp->x_scn.x_scnlen = sec->size;
2717*a9fa9459Szrj
2718*a9fa9459Szrj /* For PE, an overflow on the final link reportedly does
2719*a9fa9459Szrj not matter. FIXME: Why not? */
2720*a9fa9459Szrj if (sec->reloc_count > 0xffff
2721*a9fa9459Szrj && (! obj_pe (output_bfd)
2722*a9fa9459Szrj || bfd_link_relocatable (flaginfo->info)))
2723*a9fa9459Szrj (*_bfd_error_handler)
2724*a9fa9459Szrj (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2725*a9fa9459Szrj bfd_get_filename (output_bfd),
2726*a9fa9459Szrj bfd_get_section_name (output_bfd, sec),
2727*a9fa9459Szrj sec->reloc_count);
2728*a9fa9459Szrj
2729*a9fa9459Szrj if (sec->lineno_count > 0xffff
2730*a9fa9459Szrj && (! obj_pe (output_bfd)
2731*a9fa9459Szrj || bfd_link_relocatable (flaginfo->info)))
2732*a9fa9459Szrj (*_bfd_error_handler)
2733*a9fa9459Szrj (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2734*a9fa9459Szrj bfd_get_filename (output_bfd),
2735*a9fa9459Szrj bfd_get_section_name (output_bfd, sec),
2736*a9fa9459Szrj sec->lineno_count);
2737*a9fa9459Szrj
2738*a9fa9459Szrj auxp->x_scn.x_nreloc = sec->reloc_count;
2739*a9fa9459Szrj auxp->x_scn.x_nlinno = sec->lineno_count;
2740*a9fa9459Szrj auxp->x_scn.x_checksum = 0;
2741*a9fa9459Szrj auxp->x_scn.x_associated = 0;
2742*a9fa9459Szrj auxp->x_scn.x_comdat = 0;
2743*a9fa9459Szrj }
2744*a9fa9459Szrj }
2745*a9fa9459Szrj
2746*a9fa9459Szrj bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
2747*a9fa9459Szrj isym.n_sclass, (int) i, isym.n_numaux,
2748*a9fa9459Szrj flaginfo->outsyms);
2749*a9fa9459Szrj if (bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
2750*a9fa9459Szrj {
2751*a9fa9459Szrj flaginfo->failed = TRUE;
2752*a9fa9459Szrj return FALSE;
2753*a9fa9459Szrj }
2754*a9fa9459Szrj ++obj_raw_syment_count (output_bfd);
2755*a9fa9459Szrj }
2756*a9fa9459Szrj
2757*a9fa9459Szrj return TRUE;
2758*a9fa9459Szrj }
2759*a9fa9459Szrj
2760*a9fa9459Szrj /* Write out task global symbols, converting them to statics. Called
2761*a9fa9459Szrj via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2762*a9fa9459Szrj the dirty work, if the symbol we are processing needs conversion. */
2763*a9fa9459Szrj
2764*a9fa9459Szrj bfd_boolean
_bfd_coff_write_task_globals(struct coff_link_hash_entry * h,void * data)2765*a9fa9459Szrj _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
2766*a9fa9459Szrj {
2767*a9fa9459Szrj struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
2768*a9fa9459Szrj bfd_boolean rtnval = TRUE;
2769*a9fa9459Szrj bfd_boolean save_global_to_static;
2770*a9fa9459Szrj
2771*a9fa9459Szrj if (h->root.type == bfd_link_hash_warning)
2772*a9fa9459Szrj h = (struct coff_link_hash_entry *) h->root.u.i.link;
2773*a9fa9459Szrj
2774*a9fa9459Szrj if (h->indx < 0)
2775*a9fa9459Szrj {
2776*a9fa9459Szrj switch (h->root.type)
2777*a9fa9459Szrj {
2778*a9fa9459Szrj case bfd_link_hash_defined:
2779*a9fa9459Szrj case bfd_link_hash_defweak:
2780*a9fa9459Szrj save_global_to_static = flaginfo->global_to_static;
2781*a9fa9459Szrj flaginfo->global_to_static = TRUE;
2782*a9fa9459Szrj rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
2783*a9fa9459Szrj flaginfo->global_to_static = save_global_to_static;
2784*a9fa9459Szrj break;
2785*a9fa9459Szrj default:
2786*a9fa9459Szrj break;
2787*a9fa9459Szrj }
2788*a9fa9459Szrj }
2789*a9fa9459Szrj return (rtnval);
2790*a9fa9459Szrj }
2791*a9fa9459Szrj
2792*a9fa9459Szrj /* Handle a link order which is supposed to generate a reloc. */
2793*a9fa9459Szrj
2794*a9fa9459Szrj bfd_boolean
_bfd_coff_reloc_link_order(bfd * output_bfd,struct coff_final_link_info * flaginfo,asection * output_section,struct bfd_link_order * link_order)2795*a9fa9459Szrj _bfd_coff_reloc_link_order (bfd *output_bfd,
2796*a9fa9459Szrj struct coff_final_link_info *flaginfo,
2797*a9fa9459Szrj asection *output_section,
2798*a9fa9459Szrj struct bfd_link_order *link_order)
2799*a9fa9459Szrj {
2800*a9fa9459Szrj reloc_howto_type *howto;
2801*a9fa9459Szrj struct internal_reloc *irel;
2802*a9fa9459Szrj struct coff_link_hash_entry **rel_hash_ptr;
2803*a9fa9459Szrj
2804*a9fa9459Szrj howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
2805*a9fa9459Szrj if (howto == NULL)
2806*a9fa9459Szrj {
2807*a9fa9459Szrj bfd_set_error (bfd_error_bad_value);
2808*a9fa9459Szrj return FALSE;
2809*a9fa9459Szrj }
2810*a9fa9459Szrj
2811*a9fa9459Szrj if (link_order->u.reloc.p->addend != 0)
2812*a9fa9459Szrj {
2813*a9fa9459Szrj bfd_size_type size;
2814*a9fa9459Szrj bfd_byte *buf;
2815*a9fa9459Szrj bfd_reloc_status_type rstat;
2816*a9fa9459Szrj bfd_boolean ok;
2817*a9fa9459Szrj file_ptr loc;
2818*a9fa9459Szrj
2819*a9fa9459Szrj size = bfd_get_reloc_size (howto);
2820*a9fa9459Szrj buf = (bfd_byte *) bfd_zmalloc (size);
2821*a9fa9459Szrj if (buf == NULL && size != 0)
2822*a9fa9459Szrj return FALSE;
2823*a9fa9459Szrj
2824*a9fa9459Szrj rstat = _bfd_relocate_contents (howto, output_bfd,
2825*a9fa9459Szrj (bfd_vma) link_order->u.reloc.p->addend,\
2826*a9fa9459Szrj buf);
2827*a9fa9459Szrj switch (rstat)
2828*a9fa9459Szrj {
2829*a9fa9459Szrj case bfd_reloc_ok:
2830*a9fa9459Szrj break;
2831*a9fa9459Szrj default:
2832*a9fa9459Szrj case bfd_reloc_outofrange:
2833*a9fa9459Szrj abort ();
2834*a9fa9459Szrj case bfd_reloc_overflow:
2835*a9fa9459Szrj (*flaginfo->info->callbacks->reloc_overflow)
2836*a9fa9459Szrj (flaginfo->info, NULL,
2837*a9fa9459Szrj (link_order->type == bfd_section_reloc_link_order
2838*a9fa9459Szrj ? bfd_section_name (output_bfd,
2839*a9fa9459Szrj link_order->u.reloc.p->u.section)
2840*a9fa9459Szrj : link_order->u.reloc.p->u.name),
2841*a9fa9459Szrj howto->name, link_order->u.reloc.p->addend,
2842*a9fa9459Szrj (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2843*a9fa9459Szrj break;
2844*a9fa9459Szrj }
2845*a9fa9459Szrj loc = link_order->offset * bfd_octets_per_byte (output_bfd);
2846*a9fa9459Szrj ok = bfd_set_section_contents (output_bfd, output_section, buf,
2847*a9fa9459Szrj loc, size);
2848*a9fa9459Szrj free (buf);
2849*a9fa9459Szrj if (! ok)
2850*a9fa9459Szrj return FALSE;
2851*a9fa9459Szrj }
2852*a9fa9459Szrj
2853*a9fa9459Szrj /* Store the reloc information in the right place. It will get
2854*a9fa9459Szrj swapped and written out at the end of the final_link routine. */
2855*a9fa9459Szrj irel = (flaginfo->section_info[output_section->target_index].relocs
2856*a9fa9459Szrj + output_section->reloc_count);
2857*a9fa9459Szrj rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
2858*a9fa9459Szrj + output_section->reloc_count);
2859*a9fa9459Szrj
2860*a9fa9459Szrj memset (irel, 0, sizeof (struct internal_reloc));
2861*a9fa9459Szrj *rel_hash_ptr = NULL;
2862*a9fa9459Szrj
2863*a9fa9459Szrj irel->r_vaddr = output_section->vma + link_order->offset;
2864*a9fa9459Szrj
2865*a9fa9459Szrj if (link_order->type == bfd_section_reloc_link_order)
2866*a9fa9459Szrj {
2867*a9fa9459Szrj /* We need to somehow locate a symbol in the right section. The
2868*a9fa9459Szrj symbol must either have a value of zero, or we must adjust
2869*a9fa9459Szrj the addend by the value of the symbol. FIXME: Write this
2870*a9fa9459Szrj when we need it. The old linker couldn't handle this anyhow. */
2871*a9fa9459Szrj abort ();
2872*a9fa9459Szrj *rel_hash_ptr = NULL;
2873*a9fa9459Szrj irel->r_symndx = 0;
2874*a9fa9459Szrj }
2875*a9fa9459Szrj else
2876*a9fa9459Szrj {
2877*a9fa9459Szrj struct coff_link_hash_entry *h;
2878*a9fa9459Szrj
2879*a9fa9459Szrj h = ((struct coff_link_hash_entry *)
2880*a9fa9459Szrj bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
2881*a9fa9459Szrj link_order->u.reloc.p->u.name,
2882*a9fa9459Szrj FALSE, FALSE, TRUE));
2883*a9fa9459Szrj if (h != NULL)
2884*a9fa9459Szrj {
2885*a9fa9459Szrj if (h->indx >= 0)
2886*a9fa9459Szrj irel->r_symndx = h->indx;
2887*a9fa9459Szrj else
2888*a9fa9459Szrj {
2889*a9fa9459Szrj /* Set the index to -2 to force this symbol to get
2890*a9fa9459Szrj written out. */
2891*a9fa9459Szrj h->indx = -2;
2892*a9fa9459Szrj *rel_hash_ptr = h;
2893*a9fa9459Szrj irel->r_symndx = 0;
2894*a9fa9459Szrj }
2895*a9fa9459Szrj }
2896*a9fa9459Szrj else
2897*a9fa9459Szrj {
2898*a9fa9459Szrj (*flaginfo->info->callbacks->unattached_reloc)
2899*a9fa9459Szrj (flaginfo->info, link_order->u.reloc.p->u.name,
2900*a9fa9459Szrj (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
2901*a9fa9459Szrj irel->r_symndx = 0;
2902*a9fa9459Szrj }
2903*a9fa9459Szrj }
2904*a9fa9459Szrj
2905*a9fa9459Szrj /* FIXME: Is this always right? */
2906*a9fa9459Szrj irel->r_type = howto->type;
2907*a9fa9459Szrj
2908*a9fa9459Szrj /* r_size is only used on the RS/6000, which needs its own linker
2909*a9fa9459Szrj routines anyhow. r_extern is only used for ECOFF. */
2910*a9fa9459Szrj
2911*a9fa9459Szrj /* FIXME: What is the right value for r_offset? Is zero OK? */
2912*a9fa9459Szrj ++output_section->reloc_count;
2913*a9fa9459Szrj
2914*a9fa9459Szrj return TRUE;
2915*a9fa9459Szrj }
2916*a9fa9459Szrj
2917*a9fa9459Szrj /* A basic reloc handling routine which may be used by processors with
2918*a9fa9459Szrj simple relocs. */
2919*a9fa9459Szrj
2920*a9fa9459Szrj bfd_boolean
_bfd_coff_generic_relocate_section(bfd * output_bfd,struct bfd_link_info * info,bfd * input_bfd,asection * input_section,bfd_byte * contents,struct internal_reloc * relocs,struct internal_syment * syms,asection ** sections)2921*a9fa9459Szrj _bfd_coff_generic_relocate_section (bfd *output_bfd,
2922*a9fa9459Szrj struct bfd_link_info *info,
2923*a9fa9459Szrj bfd *input_bfd,
2924*a9fa9459Szrj asection *input_section,
2925*a9fa9459Szrj bfd_byte *contents,
2926*a9fa9459Szrj struct internal_reloc *relocs,
2927*a9fa9459Szrj struct internal_syment *syms,
2928*a9fa9459Szrj asection **sections)
2929*a9fa9459Szrj {
2930*a9fa9459Szrj struct internal_reloc *rel;
2931*a9fa9459Szrj struct internal_reloc *relend;
2932*a9fa9459Szrj
2933*a9fa9459Szrj rel = relocs;
2934*a9fa9459Szrj relend = rel + input_section->reloc_count;
2935*a9fa9459Szrj for (; rel < relend; rel++)
2936*a9fa9459Szrj {
2937*a9fa9459Szrj long symndx;
2938*a9fa9459Szrj struct coff_link_hash_entry *h;
2939*a9fa9459Szrj struct internal_syment *sym;
2940*a9fa9459Szrj bfd_vma addend;
2941*a9fa9459Szrj bfd_vma val;
2942*a9fa9459Szrj asection *sec;
2943*a9fa9459Szrj reloc_howto_type *howto;
2944*a9fa9459Szrj bfd_reloc_status_type rstat;
2945*a9fa9459Szrj
2946*a9fa9459Szrj symndx = rel->r_symndx;
2947*a9fa9459Szrj
2948*a9fa9459Szrj if (symndx == -1)
2949*a9fa9459Szrj {
2950*a9fa9459Szrj h = NULL;
2951*a9fa9459Szrj sym = NULL;
2952*a9fa9459Szrj }
2953*a9fa9459Szrj else if (symndx < 0
2954*a9fa9459Szrj || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
2955*a9fa9459Szrj {
2956*a9fa9459Szrj (*_bfd_error_handler)
2957*a9fa9459Szrj ("%B: illegal symbol index %ld in relocs", input_bfd, symndx);
2958*a9fa9459Szrj return FALSE;
2959*a9fa9459Szrj }
2960*a9fa9459Szrj else
2961*a9fa9459Szrj {
2962*a9fa9459Szrj h = obj_coff_sym_hashes (input_bfd)[symndx];
2963*a9fa9459Szrj sym = syms + symndx;
2964*a9fa9459Szrj }
2965*a9fa9459Szrj
2966*a9fa9459Szrj /* COFF treats common symbols in one of two ways. Either the
2967*a9fa9459Szrj size of the symbol is included in the section contents, or it
2968*a9fa9459Szrj is not. We assume that the size is not included, and force
2969*a9fa9459Szrj the rtype_to_howto function to adjust the addend as needed. */
2970*a9fa9459Szrj if (sym != NULL && sym->n_scnum != 0)
2971*a9fa9459Szrj addend = - sym->n_value;
2972*a9fa9459Szrj else
2973*a9fa9459Szrj addend = 0;
2974*a9fa9459Szrj
2975*a9fa9459Szrj howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
2976*a9fa9459Szrj sym, &addend);
2977*a9fa9459Szrj if (howto == NULL)
2978*a9fa9459Szrj return FALSE;
2979*a9fa9459Szrj
2980*a9fa9459Szrj /* If we are doing a relocatable link, then we can just ignore
2981*a9fa9459Szrj a PC relative reloc that is pcrel_offset. It will already
2982*a9fa9459Szrj have the correct value. If this is not a relocatable link,
2983*a9fa9459Szrj then we should ignore the symbol value. */
2984*a9fa9459Szrj if (howto->pc_relative && howto->pcrel_offset)
2985*a9fa9459Szrj {
2986*a9fa9459Szrj if (bfd_link_relocatable (info))
2987*a9fa9459Szrj continue;
2988*a9fa9459Szrj if (sym != NULL && sym->n_scnum != 0)
2989*a9fa9459Szrj addend += sym->n_value;
2990*a9fa9459Szrj }
2991*a9fa9459Szrj
2992*a9fa9459Szrj val = 0;
2993*a9fa9459Szrj sec = NULL;
2994*a9fa9459Szrj if (h == NULL)
2995*a9fa9459Szrj {
2996*a9fa9459Szrj if (symndx == -1)
2997*a9fa9459Szrj {
2998*a9fa9459Szrj sec = bfd_abs_section_ptr;
2999*a9fa9459Szrj val = 0;
3000*a9fa9459Szrj }
3001*a9fa9459Szrj else
3002*a9fa9459Szrj {
3003*a9fa9459Szrj sec = sections[symndx];
3004*a9fa9459Szrj
3005*a9fa9459Szrj /* PR 19623: Relocations against symbols in
3006*a9fa9459Szrj the absolute sections should ignored. */
3007*a9fa9459Szrj if (bfd_is_abs_section (sec))
3008*a9fa9459Szrj continue;
3009*a9fa9459Szrj
3010*a9fa9459Szrj val = (sec->output_section->vma
3011*a9fa9459Szrj + sec->output_offset
3012*a9fa9459Szrj + sym->n_value);
3013*a9fa9459Szrj if (! obj_pe (input_bfd))
3014*a9fa9459Szrj val -= sec->vma;
3015*a9fa9459Szrj }
3016*a9fa9459Szrj }
3017*a9fa9459Szrj else
3018*a9fa9459Szrj {
3019*a9fa9459Szrj if (h->root.type == bfd_link_hash_defined
3020*a9fa9459Szrj || h->root.type == bfd_link_hash_defweak)
3021*a9fa9459Szrj {
3022*a9fa9459Szrj /* Defined weak symbols are a GNU extension. */
3023*a9fa9459Szrj sec = h->root.u.def.section;
3024*a9fa9459Szrj val = (h->root.u.def.value
3025*a9fa9459Szrj + sec->output_section->vma
3026*a9fa9459Szrj + sec->output_offset);
3027*a9fa9459Szrj }
3028*a9fa9459Szrj
3029*a9fa9459Szrj else if (h->root.type == bfd_link_hash_undefweak)
3030*a9fa9459Szrj {
3031*a9fa9459Szrj if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
3032*a9fa9459Szrj {
3033*a9fa9459Szrj /* See _Microsoft Portable Executable and Common Object
3034*a9fa9459Szrj File Format Specification_, section 5.5.3.
3035*a9fa9459Szrj Note that weak symbols without aux records are a GNU
3036*a9fa9459Szrj extension.
3037*a9fa9459Szrj FIXME: All weak externals are treated as having
3038*a9fa9459Szrj characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
3039*a9fa9459Szrj These behave as per SVR4 ABI: A library member
3040*a9fa9459Szrj will resolve a weak external only if a normal
3041*a9fa9459Szrj external causes the library member to be linked.
3042*a9fa9459Szrj See also linker.c: generic_link_check_archive_element. */
3043*a9fa9459Szrj struct coff_link_hash_entry *h2 =
3044*a9fa9459Szrj h->auxbfd->tdata.coff_obj_data->sym_hashes[
3045*a9fa9459Szrj h->aux->x_sym.x_tagndx.l];
3046*a9fa9459Szrj
3047*a9fa9459Szrj if (!h2 || h2->root.type == bfd_link_hash_undefined)
3048*a9fa9459Szrj {
3049*a9fa9459Szrj sec = bfd_abs_section_ptr;
3050*a9fa9459Szrj val = 0;
3051*a9fa9459Szrj }
3052*a9fa9459Szrj else
3053*a9fa9459Szrj {
3054*a9fa9459Szrj sec = h2->root.u.def.section;
3055*a9fa9459Szrj val = h2->root.u.def.value
3056*a9fa9459Szrj + sec->output_section->vma + sec->output_offset;
3057*a9fa9459Szrj }
3058*a9fa9459Szrj }
3059*a9fa9459Szrj else
3060*a9fa9459Szrj /* This is a GNU extension. */
3061*a9fa9459Szrj val = 0;
3062*a9fa9459Szrj }
3063*a9fa9459Szrj
3064*a9fa9459Szrj else if (! bfd_link_relocatable (info))
3065*a9fa9459Szrj (*info->callbacks->undefined_symbol)
3066*a9fa9459Szrj (info, h->root.root.string, input_bfd, input_section,
3067*a9fa9459Szrj rel->r_vaddr - input_section->vma, TRUE);
3068*a9fa9459Szrj }
3069*a9fa9459Szrj
3070*a9fa9459Szrj /* If the input section defining the symbol has been discarded
3071*a9fa9459Szrj then zero this reloc field. */
3072*a9fa9459Szrj if (sec != NULL && discarded_section (sec))
3073*a9fa9459Szrj {
3074*a9fa9459Szrj _bfd_clear_contents (howto, input_bfd, input_section,
3075*a9fa9459Szrj contents + (rel->r_vaddr - input_section->vma));
3076*a9fa9459Szrj continue;
3077*a9fa9459Szrj }
3078*a9fa9459Szrj
3079*a9fa9459Szrj if (info->base_file)
3080*a9fa9459Szrj {
3081*a9fa9459Szrj /* Emit a reloc if the backend thinks it needs it. */
3082*a9fa9459Szrj if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
3083*a9fa9459Szrj {
3084*a9fa9459Szrj /* Relocation to a symbol in a section which isn't
3085*a9fa9459Szrj absolute. We output the address here to a file.
3086*a9fa9459Szrj This file is then read by dlltool when generating the
3087*a9fa9459Szrj reloc section. Note that the base file is not
3088*a9fa9459Szrj portable between systems. We write out a bfd_vma here,
3089*a9fa9459Szrj and dlltool reads in a bfd_vma. */
3090*a9fa9459Szrj bfd_vma addr = (rel->r_vaddr
3091*a9fa9459Szrj - input_section->vma
3092*a9fa9459Szrj + input_section->output_offset
3093*a9fa9459Szrj + input_section->output_section->vma);
3094*a9fa9459Szrj if (coff_data (output_bfd)->pe)
3095*a9fa9459Szrj addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
3096*a9fa9459Szrj if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
3097*a9fa9459Szrj != sizeof (bfd_vma))
3098*a9fa9459Szrj {
3099*a9fa9459Szrj bfd_set_error (bfd_error_system_call);
3100*a9fa9459Szrj return FALSE;
3101*a9fa9459Szrj }
3102*a9fa9459Szrj }
3103*a9fa9459Szrj }
3104*a9fa9459Szrj
3105*a9fa9459Szrj rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
3106*a9fa9459Szrj contents,
3107*a9fa9459Szrj rel->r_vaddr - input_section->vma,
3108*a9fa9459Szrj val, addend);
3109*a9fa9459Szrj
3110*a9fa9459Szrj switch (rstat)
3111*a9fa9459Szrj {
3112*a9fa9459Szrj default:
3113*a9fa9459Szrj abort ();
3114*a9fa9459Szrj case bfd_reloc_ok:
3115*a9fa9459Szrj break;
3116*a9fa9459Szrj case bfd_reloc_outofrange:
3117*a9fa9459Szrj (*_bfd_error_handler)
3118*a9fa9459Szrj (_("%B: bad reloc address 0x%lx in section `%A'"),
3119*a9fa9459Szrj input_bfd, input_section, (unsigned long) rel->r_vaddr);
3120*a9fa9459Szrj return FALSE;
3121*a9fa9459Szrj case bfd_reloc_overflow:
3122*a9fa9459Szrj {
3123*a9fa9459Szrj const char *name;
3124*a9fa9459Szrj char buf[SYMNMLEN + 1];
3125*a9fa9459Szrj
3126*a9fa9459Szrj if (symndx == -1)
3127*a9fa9459Szrj name = "*ABS*";
3128*a9fa9459Szrj else if (h != NULL)
3129*a9fa9459Szrj name = NULL;
3130*a9fa9459Szrj else
3131*a9fa9459Szrj {
3132*a9fa9459Szrj name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
3133*a9fa9459Szrj if (name == NULL)
3134*a9fa9459Szrj return FALSE;
3135*a9fa9459Szrj }
3136*a9fa9459Szrj
3137*a9fa9459Szrj (*info->callbacks->reloc_overflow)
3138*a9fa9459Szrj (info, (h ? &h->root : NULL), name, howto->name,
3139*a9fa9459Szrj (bfd_vma) 0, input_bfd, input_section,
3140*a9fa9459Szrj rel->r_vaddr - input_section->vma);
3141*a9fa9459Szrj }
3142*a9fa9459Szrj }
3143*a9fa9459Szrj }
3144*a9fa9459Szrj return TRUE;
3145*a9fa9459Szrj }
3146