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