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