Commit | Line | Data |
---|---|---|
075caafd | 1 | /* Support for the generic parts of COFF, for BFD. |
6dc6a81a | 2 | Copyright 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc. |
075caafd ILT |
3 | Written by Cygnus Support. |
4 | ||
5 | This file is part of BFD, the Binary File Descriptor library. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
ae115e51 | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
075caafd ILT |
20 | |
21 | /* Most of this hacked by Steve Chamberlain, sac@cygnus.com. | |
22 | Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */ | |
23 | ||
24 | /* This file contains COFF code that is not dependent on any | |
25 | particular COFF target. There is only one version of this file in | |
26 | libbfd.a, so no target specific code may be put in here. Or, to | |
27 | put it another way, | |
28 | ||
29 | ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE ********** | |
30 | ||
31 | If you need to add some target specific behaviour, add a new hook | |
32 | function to bfd_coff_backend_data. | |
33 | ||
34 | Some of these functions are also called by the ECOFF routines. | |
35 | Those functions may not use any COFF specific information, such as | |
36 | coff_data (abfd). */ | |
37 | ||
38 | #include "bfd.h" | |
39 | #include "sysdep.h" | |
40 | #include "libbfd.h" | |
41 | #include "coff/internal.h" | |
42 | #include "libcoff.h" | |
43 | ||
f0500a41 ILT |
44 | static void coff_fix_symbol_name |
45 | PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *, | |
46 | asection **, bfd_size_type *)); | |
47 | static boolean coff_write_symbol | |
48 | PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *, | |
49 | bfd_size_type *, asection **, bfd_size_type *)); | |
50 | static boolean coff_write_alien_symbol | |
51 | PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *, | |
52 | asection **, bfd_size_type *)); | |
53 | static boolean coff_write_native_symbol | |
54 | PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *, | |
55 | asection **, bfd_size_type *)); | |
bfe8224f | 56 | |
9fbe895a ILT |
57 | #define STRING_SIZE_SIZE (4) |
58 | ||
075caafd ILT |
59 | /* Take a section header read from a coff file (in HOST byte order), |
60 | and make a BFD "section" out of it. This is used by ECOFF. */ | |
6dc6a81a | 61 | static boolean |
25057836 | 62 | make_a_section_from_file (abfd, hdr, target_index) |
6dc6a81a ILT |
63 | bfd *abfd; |
64 | struct internal_scnhdr *hdr; | |
25057836 | 65 | unsigned int target_index; |
075caafd | 66 | { |
6dc6a81a | 67 | asection *return_section; |
075caafd | 68 | char *name; |
6dc6a81a | 69 | |
075caafd | 70 | /* Assorted wastage to null-terminate the name, thanks AT&T! */ |
6dc6a81a ILT |
71 | name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1); |
72 | if (name == NULL) | |
73 | { | |
d1ad85a6 | 74 | bfd_set_error (bfd_error_no_memory); |
075caafd ILT |
75 | return false; |
76 | } | |
6dc6a81a | 77 | strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); |
075caafd ILT |
78 | name[sizeof (hdr->s_name)] = 0; |
79 | ||
6dc6a81a | 80 | return_section = bfd_make_section (abfd, name); |
075caafd ILT |
81 | if (return_section == NULL) |
82 | return_section = bfd_coff_make_section_hook (abfd, name); | |
4c3721d5 ILT |
83 | |
84 | /* Handle several sections of the same name. For example, if an executable | |
85 | has two .bss sections, GDB better be able to find both of them | |
86 | (PR 3562). */ | |
87 | if (return_section == NULL) | |
88 | return_section = bfd_make_section_anyway (abfd, name); | |
89 | ||
075caafd ILT |
90 | if (return_section == NULL) |
91 | return false; | |
92 | ||
93 | /* s_paddr is presumed to be = to s_vaddr */ | |
94 | ||
95 | return_section->vma = hdr->s_vaddr; | |
c7e76b5e | 96 | return_section->lma = return_section->vma; |
075caafd ILT |
97 | return_section->_raw_size = hdr->s_size; |
98 | return_section->filepos = hdr->s_scnptr; | |
6dc6a81a | 99 | return_section->rel_filepos = hdr->s_relptr; |
075caafd ILT |
100 | return_section->reloc_count = hdr->s_nreloc; |
101 | ||
102 | bfd_coff_set_alignment_hook (abfd, return_section, hdr); | |
103 | ||
6dc6a81a | 104 | return_section->line_filepos = hdr->s_lnnoptr; |
075caafd ILT |
105 | |
106 | return_section->lineno_count = hdr->s_nlnno; | |
107 | return_section->userdata = NULL; | |
108 | return_section->next = (asection *) NULL; | |
e8fbe6d9 | 109 | return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name); |
075caafd ILT |
110 | |
111 | return_section->target_index = target_index; | |
112 | ||
2d1e6c9c KR |
113 | /* At least on i386-coff, the line number count for a shared library |
114 | section must be ignored. */ | |
c16313f0 | 115 | if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0) |
2d1e6c9c KR |
116 | return_section->lineno_count = 0; |
117 | ||
075caafd ILT |
118 | if (hdr->s_nreloc != 0) |
119 | return_section->flags |= SEC_RELOC; | |
120 | /* FIXME: should this check 'hdr->s_size > 0' */ | |
121 | if (hdr->s_scnptr != 0) | |
122 | return_section->flags |= SEC_HAS_CONTENTS; | |
123 | return true; | |
124 | } | |
125 | ||
126 | /* Read in a COFF object and make it into a BFD. This is used by | |
127 | ECOFF as well. */ | |
128 | ||
2f3508ad | 129 | static const bfd_target * |
25057836 | 130 | coff_real_object_p (abfd, nscns, internal_f, internal_a) |
6dc6a81a ILT |
131 | bfd *abfd; |
132 | unsigned nscns; | |
25057836 JL |
133 | struct internal_filehdr *internal_f; |
134 | struct internal_aouthdr *internal_a; | |
075caafd | 135 | { |
c7e76b5e ILT |
136 | flagword oflags = abfd->flags; |
137 | bfd_vma ostart = bfd_get_start_address (abfd); | |
075caafd | 138 | PTR tdata; |
6dc6a81a | 139 | size_t readsize; /* length of file_info */ |
075caafd ILT |
140 | unsigned int scnhsz; |
141 | char *external_sections; | |
142 | ||
c7e76b5e ILT |
143 | if (!(internal_f->f_flags & F_RELFLG)) |
144 | abfd->flags |= HAS_RELOC; | |
145 | if ((internal_f->f_flags & F_EXEC)) | |
146 | abfd->flags |= EXEC_P; | |
147 | if (!(internal_f->f_flags & F_LNNO)) | |
148 | abfd->flags |= HAS_LINENO; | |
149 | if (!(internal_f->f_flags & F_LSYMS)) | |
150 | abfd->flags |= HAS_LOCALS; | |
151 | ||
152 | /* FIXME: How can we set D_PAGED correctly? */ | |
153 | if ((internal_f->f_flags & F_EXEC) != 0) | |
154 | abfd->flags |= D_PAGED; | |
155 | ||
156 | bfd_get_symcount (abfd) = internal_f->f_nsyms; | |
157 | if (internal_f->f_nsyms) | |
158 | abfd->flags |= HAS_SYMS; | |
159 | ||
160 | if (internal_a != (struct internal_aouthdr *) NULL) | |
161 | bfd_get_start_address (abfd) = internal_a->entry; | |
162 | else | |
163 | bfd_get_start_address (abfd) = 0; | |
164 | ||
165 | /* Set up the tdata area. ECOFF uses its own routine, and overrides | |
166 | abfd->flags. */ | |
27f524a3 | 167 | tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a); |
075caafd ILT |
168 | if (tdata == NULL) |
169 | return 0; | |
170 | ||
171 | scnhsz = bfd_coff_scnhsz (abfd); | |
172 | readsize = nscns * scnhsz; | |
6dc6a81a | 173 | external_sections = (char *) bfd_alloc (abfd, readsize); |
9783e04a DM |
174 | if (!external_sections) |
175 | { | |
d1ad85a6 | 176 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
177 | goto fail; |
178 | } | |
075caafd | 179 | |
6dc6a81a | 180 | if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize) |
075caafd | 181 | goto fail; |
075caafd ILT |
182 | |
183 | /* Now copy data as required; construct all asections etc */ | |
6dc6a81a ILT |
184 | if (nscns != 0) |
185 | { | |
186 | unsigned int i; | |
187 | for (i = 0; i < nscns; i++) | |
188 | { | |
189 | struct internal_scnhdr tmp; | |
c7e76b5e ILT |
190 | bfd_coff_swap_scnhdr_in (abfd, |
191 | (PTR) (external_sections + i * scnhsz), | |
6dc6a81a ILT |
192 | (PTR) & tmp); |
193 | make_a_section_from_file (abfd, &tmp, i + 1); | |
194 | } | |
075caafd | 195 | } |
075caafd | 196 | |
6dc6a81a ILT |
197 | /* make_abs_section (abfd); */ |
198 | ||
075caafd ILT |
199 | if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false) |
200 | goto fail; | |
201 | ||
075caafd | 202 | return abfd->xvec; |
c7e76b5e | 203 | |
075caafd | 204 | fail: |
6dc6a81a | 205 | bfd_release (abfd, tdata); |
c7e76b5e ILT |
206 | abfd->flags = oflags; |
207 | bfd_get_start_address (abfd) = ostart; | |
6dc6a81a | 208 | return (const bfd_target *) NULL; |
075caafd ILT |
209 | } |
210 | ||
d1ad85a6 | 211 | /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is |
075caafd ILT |
212 | not a COFF file. This is also used by ECOFF. */ |
213 | ||
2f3508ad | 214 | const bfd_target * |
25057836 | 215 | coff_object_p (abfd) |
6dc6a81a | 216 | bfd *abfd; |
075caafd ILT |
217 | { |
218 | unsigned int filhsz; | |
219 | unsigned int aoutsz; | |
6dc6a81a | 220 | int nscns; |
075caafd ILT |
221 | PTR filehdr; |
222 | struct internal_filehdr internal_f; | |
223 | struct internal_aouthdr internal_a; | |
224 | ||
075caafd ILT |
225 | /* figure out how much to read */ |
226 | filhsz = bfd_coff_filhsz (abfd); | |
227 | aoutsz = bfd_coff_aoutsz (abfd); | |
228 | ||
229 | filehdr = bfd_alloc (abfd, filhsz); | |
230 | if (filehdr == NULL) | |
231 | return 0; | |
6dc6a81a | 232 | if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz) |
25057836 JL |
233 | { |
234 | if (bfd_get_error () != bfd_error_system_call) | |
235 | bfd_set_error (bfd_error_wrong_format); | |
236 | return 0; | |
237 | } | |
6dc6a81a | 238 | bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f); |
075caafd ILT |
239 | bfd_release (abfd, filehdr); |
240 | ||
6dc6a81a ILT |
241 | if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) |
242 | { | |
243 | bfd_set_error (bfd_error_wrong_format); | |
244 | return 0; | |
245 | } | |
246 | nscns = internal_f.f_nscns; | |
075caafd | 247 | |
6dc6a81a ILT |
248 | if (internal_f.f_opthdr) |
249 | { | |
250 | PTR opthdr; | |
075caafd | 251 | |
6dc6a81a ILT |
252 | opthdr = bfd_alloc (abfd, aoutsz); |
253 | if (opthdr == NULL) | |
254 | return 0;; | |
255 | if (bfd_read (opthdr, 1, aoutsz, abfd) != aoutsz) | |
256 | { | |
257 | return 0; | |
258 | } | |
259 | bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) & internal_a); | |
075caafd | 260 | } |
075caafd ILT |
261 | |
262 | /* Seek past the opt hdr stuff */ | |
6dc6a81a | 263 | if (bfd_seek (abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET) |
c16313f0 ILT |
264 | != 0) |
265 | return NULL; | |
075caafd | 266 | |
6dc6a81a ILT |
267 | return coff_real_object_p (abfd, nscns, &internal_f, |
268 | (internal_f.f_opthdr != 0 | |
269 | ? &internal_a | |
270 | : (struct internal_aouthdr *) NULL)); | |
075caafd ILT |
271 | } |
272 | ||
273 | /* Get the BFD section from a COFF symbol section number. */ | |
274 | ||
e8fbe6d9 | 275 | asection * |
25057836 | 276 | coff_section_from_bfd_index (abfd, index) |
6dc6a81a ILT |
277 | bfd *abfd; |
278 | int index; | |
075caafd ILT |
279 | { |
280 | struct sec *answer = abfd->sections; | |
281 | ||
6dc6a81a | 282 | if (index == N_ABS) |
e8fbe6d9 | 283 | return bfd_abs_section_ptr; |
075caafd | 284 | if (index == N_UNDEF) |
e8fbe6d9 | 285 | return bfd_und_section_ptr; |
6dc6a81a ILT |
286 | if (index == N_DEBUG) |
287 | return bfd_abs_section_ptr; | |
288 | ||
289 | while (answer) | |
290 | { | |
075caafd | 291 | if (answer->target_index == index) |
6dc6a81a | 292 | return answer; |
075caafd ILT |
293 | answer = answer->next; |
294 | } | |
c16313f0 ILT |
295 | |
296 | /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a | |
297 | has a bad symbol table in biglitpow.o. */ | |
e8fbe6d9 | 298 | return bfd_und_section_ptr; |
075caafd ILT |
299 | } |
300 | ||
301 | /* Get the upper bound of a COFF symbol table. */ | |
302 | ||
326e32d7 | 303 | long |
6dc6a81a ILT |
304 | coff_get_symtab_upper_bound (abfd) |
305 | bfd *abfd; | |
075caafd | 306 | { |
6dc6a81a | 307 | if (!bfd_coff_slurp_symbol_table (abfd)) |
326e32d7 | 308 | return -1; |
075caafd | 309 | |
6dc6a81a | 310 | return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *)); |
075caafd ILT |
311 | } |
312 | ||
313 | ||
314 | /* Canonicalize a COFF symbol table. */ | |
315 | ||
326e32d7 | 316 | long |
25057836 | 317 | coff_get_symtab (abfd, alocation) |
74942465 ILT |
318 | bfd *abfd; |
319 | asymbol **alocation; | |
075caafd | 320 | { |
74942465 ILT |
321 | unsigned int counter; |
322 | coff_symbol_type *symbase; | |
323 | coff_symbol_type **location = (coff_symbol_type **) alocation; | |
324 | ||
6dc6a81a | 325 | if (!bfd_coff_slurp_symbol_table (abfd)) |
74942465 ILT |
326 | return -1; |
327 | ||
328 | symbase = obj_symbols (abfd); | |
329 | counter = bfd_get_symcount (abfd); | |
330 | while (counter-- > 0) | |
331 | *location++ = symbase++; | |
332 | ||
333 | *location = NULL; | |
334 | ||
335 | return bfd_get_symcount (abfd); | |
075caafd ILT |
336 | } |
337 | ||
338 | /* Set lineno_count for the output sections of a COFF file. */ | |
339 | ||
27f524a3 | 340 | int |
25057836 | 341 | coff_count_linenumbers (abfd) |
69645d10 | 342 | bfd *abfd; |
075caafd | 343 | { |
6dc6a81a | 344 | unsigned int limit = bfd_get_symcount (abfd); |
69645d10 | 345 | unsigned int i; |
27f524a3 | 346 | int total = 0; |
69645d10 ILT |
347 | asymbol **p; |
348 | asection *s; | |
349 | ||
350 | if (limit == 0) | |
351 | { | |
352 | /* This may be from the backend linker, in which case the | |
353 | lineno_count in the sections is correct. */ | |
354 | for (s = abfd->sections; s != NULL; s = s->next) | |
355 | total += s->lineno_count; | |
356 | return total; | |
357 | } | |
358 | ||
359 | for (s = abfd->sections; s != NULL; s = s->next) | |
360 | BFD_ASSERT (s->lineno_count == 0); | |
361 | ||
362 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) | |
363 | { | |
364 | asymbol *q_maybe = *p; | |
365 | ||
366 | if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour) | |
367 | { | |
368 | coff_symbol_type *q = coffsymbol (q_maybe); | |
369 | ||
370 | if (q->lineno != NULL) | |
371 | { | |
372 | /* This symbol has line numbers. Increment the owning | |
6dc6a81a | 373 | section's linenumber count. */ |
69645d10 ILT |
374 | alent *l = q->lineno; |
375 | ||
376 | ++q->symbol.section->output_section->lineno_count; | |
377 | ++total; | |
378 | ++l; | |
379 | while (l->line_number != 0) | |
380 | { | |
381 | ++total; | |
382 | ++q->symbol.section->output_section->lineno_count; | |
383 | ++l; | |
384 | } | |
385 | } | |
075caafd | 386 | } |
075caafd | 387 | } |
69645d10 | 388 | |
27f524a3 | 389 | return total; |
075caafd ILT |
390 | } |
391 | ||
392 | /* Takes a bfd and a symbol, returns a pointer to the coff specific | |
393 | area of the symbol if there is one. */ | |
394 | ||
330595d0 | 395 | /*ARGSUSED*/ |
075caafd | 396 | coff_symbol_type * |
25057836 | 397 | coff_symbol_from (ignore_abfd, symbol) |
6dc6a81a ILT |
398 | bfd *ignore_abfd; |
399 | asymbol *symbol; | |
075caafd | 400 | { |
6dc6a81a ILT |
401 | if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour) |
402 | return (coff_symbol_type *) NULL; | |
075caafd | 403 | |
6dc6a81a ILT |
404 | if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL) |
405 | return (coff_symbol_type *) NULL; | |
075caafd | 406 | |
6dc6a81a | 407 | return (coff_symbol_type *) symbol; |
075caafd ILT |
408 | } |
409 | ||
410 | static void | |
25057836 JL |
411 | fixup_symbol_value (coff_symbol_ptr, syment) |
412 | coff_symbol_type *coff_symbol_ptr; | |
413 | struct internal_syment *syment; | |
075caafd ILT |
414 | { |
415 | ||
416 | /* Normalize the symbol flags */ | |
6dc6a81a ILT |
417 | if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) |
418 | { | |
419 | /* a common symbol is undefined with a value */ | |
420 | syment->n_scnum = N_UNDEF; | |
421 | syment->n_value = coff_symbol_ptr->symbol.value; | |
075caafd | 422 | } |
6dc6a81a ILT |
423 | else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) |
424 | { | |
075caafd ILT |
425 | syment->n_value = coff_symbol_ptr->symbol.value; |
426 | } | |
6dc6a81a ILT |
427 | else if (bfd_is_und_section (coff_symbol_ptr->symbol.section)) |
428 | { | |
429 | syment->n_scnum = N_UNDEF; | |
430 | syment->n_value = 0; | |
431 | } | |
432 | else | |
433 | { | |
434 | if (coff_symbol_ptr->symbol.section) | |
435 | { | |
436 | syment->n_scnum = | |
437 | coff_symbol_ptr->symbol.section->output_section->target_index; | |
438 | ||
439 | syment->n_value = | |
440 | coff_symbol_ptr->symbol.value + | |
441 | coff_symbol_ptr->symbol.section->output_offset + | |
442 | coff_symbol_ptr->symbol.section->output_section->vma; | |
443 | } | |
444 | else | |
445 | { | |
446 | BFD_ASSERT (0); | |
447 | /* This can happen, but I don't know why yet (steve@cygnus.com) */ | |
448 | syment->n_scnum = N_ABS; | |
449 | syment->n_value = coff_symbol_ptr->symbol.value; | |
450 | } | |
451 | } | |
075caafd ILT |
452 | } |
453 | ||
6dc6a81a ILT |
454 | /* Run through all the symbols in the symbol table and work out what |
455 | their indexes into the symbol table will be when output. | |
075caafd | 456 | |
6dc6a81a ILT |
457 | Coff requires that each C_FILE symbol points to the next one in the |
458 | chain, and that the last one points to the first external symbol. We | |
459 | do that here too. */ | |
075caafd | 460 | |
9783e04a | 461 | boolean |
c7e76b5e | 462 | coff_renumber_symbols (bfd_ptr, first_undef) |
25057836 | 463 | bfd *bfd_ptr; |
c7e76b5e | 464 | int *first_undef; |
075caafd | 465 | { |
6dc6a81a | 466 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
075caafd ILT |
467 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
468 | unsigned int native_index = 0; | |
6dc6a81a | 469 | struct internal_syment *last_file = (struct internal_syment *) NULL; |
075caafd ILT |
470 | unsigned int symbol_index; |
471 | ||
472 | /* COFF demands that undefined symbols come after all other symbols. | |
c7e76b5e ILT |
473 | Since we don't need to impose this extra knowledge on all our |
474 | client programs, deal with that here. Sort the symbol table; | |
475 | just move the undefined symbols to the end, leaving the rest | |
476 | alone. The O'Reilly book says that defined global symbols come | |
477 | at the end before the undefined symbols, so we do that here as | |
478 | well. */ | |
075caafd ILT |
479 | /* @@ Do we have some condition we could test for, so we don't always |
480 | have to do this? I don't think relocatability is quite right, but | |
481 | I'm not certain. [raeburn:19920508.1711EST] */ | |
482 | { | |
483 | asymbol **newsyms; | |
ae115e51 | 484 | unsigned int i; |
075caafd ILT |
485 | |
486 | newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr, | |
487 | sizeof (asymbol *) | |
488 | * (symbol_count + 1)); | |
9783e04a DM |
489 | if (!newsyms) |
490 | { | |
d1ad85a6 | 491 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
492 | return false; |
493 | } | |
075caafd ILT |
494 | bfd_ptr->outsymbols = newsyms; |
495 | for (i = 0; i < symbol_count; i++) | |
c7e76b5e ILT |
496 | if (!bfd_is_und_section (symbol_ptr_ptr[i]->section) |
497 | && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | |
498 | | BSF_NOT_AT_END | |
499 | | BSF_FUNCTION)) | |
500 | != BSF_GLOBAL)) | |
075caafd | 501 | *newsyms++ = symbol_ptr_ptr[i]; |
c7e76b5e ILT |
502 | |
503 | for (i = 0; i < symbol_count; i++) | |
504 | if (!bfd_is_und_section (symbol_ptr_ptr[i]->section) | |
505 | && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | |
506 | | BSF_NOT_AT_END | |
507 | | BSF_FUNCTION)) | |
508 | == BSF_GLOBAL)) | |
509 | *newsyms++ = symbol_ptr_ptr[i]; | |
510 | ||
511 | *first_undef = newsyms - bfd_ptr->outsymbols; | |
512 | ||
075caafd | 513 | for (i = 0; i < symbol_count; i++) |
e8fbe6d9 | 514 | if (bfd_is_und_section (symbol_ptr_ptr[i]->section)) |
075caafd ILT |
515 | *newsyms++ = symbol_ptr_ptr[i]; |
516 | *newsyms = (asymbol *) NULL; | |
517 | symbol_ptr_ptr = bfd_ptr->outsymbols; | |
518 | } | |
519 | ||
520 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) | |
6dc6a81a ILT |
521 | { |
522 | coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); | |
c7e76b5e | 523 | symbol_ptr_ptr[symbol_index]->udata.i = symbol_index; |
6dc6a81a ILT |
524 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
525 | { | |
075caafd ILT |
526 | combined_entry_type *s = coff_symbol_ptr->native; |
527 | int i; | |
528 | ||
529 | if (s->u.syment.n_sclass == C_FILE) | |
6dc6a81a ILT |
530 | { |
531 | if (last_file != (struct internal_syment *) NULL) | |
532 | last_file->n_value = native_index; | |
533 | last_file = &(s->u.syment); | |
534 | } | |
535 | else | |
536 | { | |
537 | ||
538 | /* Modify the symbol values according to their section and | |
539 | type */ | |
540 | ||
541 | fixup_symbol_value (coff_symbol_ptr, &(s->u.syment)); | |
542 | } | |
543 | for (i = 0; i < s->u.syment.n_numaux + 1; i++) | |
544 | s[i].offset = native_index++; | |
075caafd | 545 | } |
6dc6a81a ILT |
546 | else |
547 | { | |
075caafd ILT |
548 | native_index++; |
549 | } | |
6dc6a81a | 550 | } |
075caafd | 551 | obj_conv_table_size (bfd_ptr) = native_index; |
c7e76b5e | 552 | |
9783e04a | 553 | return true; |
075caafd ILT |
554 | } |
555 | ||
6dc6a81a ILT |
556 | /* Run thorough the symbol table again, and fix it so that all |
557 | pointers to entries are changed to the entries' index in the output | |
558 | symbol table. */ | |
075caafd | 559 | |
075caafd | 560 | void |
9783e04a DM |
561 | coff_mangle_symbols (bfd_ptr) |
562 | bfd *bfd_ptr; | |
075caafd | 563 | { |
9783e04a | 564 | unsigned int symbol_count = bfd_get_symcount (bfd_ptr); |
075caafd ILT |
565 | asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; |
566 | unsigned int symbol_index; | |
567 | ||
568 | for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) | |
9783e04a DM |
569 | { |
570 | coff_symbol_type *coff_symbol_ptr = | |
6dc6a81a | 571 | coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]); |
075caafd | 572 | |
9783e04a DM |
573 | if (coff_symbol_ptr && coff_symbol_ptr->native) |
574 | { | |
075caafd ILT |
575 | int i; |
576 | combined_entry_type *s = coff_symbol_ptr->native; | |
577 | ||
9783e04a DM |
578 | if (s->fix_value) |
579 | { | |
580 | /* FIXME: We should use a union here. */ | |
581 | s->u.syment.n_value = | |
582 | ((combined_entry_type *) s->u.syment.n_value)->offset; | |
583 | s->fix_value = 0; | |
075caafd | 584 | } |
6dc6a81a | 585 | for (i = 0; i < s->u.syment.n_numaux; i++) |
9783e04a DM |
586 | { |
587 | combined_entry_type *a = s + i + 1; | |
588 | if (a->fix_tag) | |
589 | { | |
590 | a->u.auxent.x_sym.x_tagndx.l = | |
591 | a->u.auxent.x_sym.x_tagndx.p->offset; | |
592 | a->fix_tag = 0; | |
593 | } | |
594 | if (a->fix_end) | |
595 | { | |
596 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l = | |
597 | a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset; | |
598 | a->fix_end = 0; | |
599 | } | |
600 | if (a->fix_scnlen) | |
601 | { | |
602 | a->u.auxent.x_csect.x_scnlen.l = | |
603 | a->u.auxent.x_csect.x_scnlen.p->offset; | |
604 | a->fix_scnlen = 0; | |
605 | } | |
075caafd | 606 | } |
075caafd | 607 | } |
9783e04a | 608 | } |
075caafd ILT |
609 | } |
610 | ||
075caafd | 611 | static void |
f0500a41 ILT |
612 | coff_fix_symbol_name (abfd, symbol, native, string_size_p, |
613 | debug_string_section_p, debug_string_size_p) | |
25057836 JL |
614 | bfd *abfd; |
615 | asymbol *symbol; | |
616 | combined_entry_type *native; | |
f0500a41 ILT |
617 | bfd_size_type *string_size_p; |
618 | asection **debug_string_section_p; | |
619 | bfd_size_type *debug_string_size_p; | |
075caafd | 620 | { |
6dc6a81a | 621 | unsigned int name_length; |
075caafd | 622 | union internal_auxent *auxent; |
6dc6a81a | 623 | char *name = (char *) (symbol->name); |
075caafd | 624 | |
6dc6a81a ILT |
625 | if (name == (char *) NULL) |
626 | { | |
627 | /* coff symbols always have names, so we'll make one up */ | |
628 | symbol->name = "strange"; | |
629 | name = (char *) symbol->name; | |
630 | } | |
631 | name_length = strlen (name); | |
075caafd | 632 | |
6dc6a81a ILT |
633 | if (native->u.syment.n_sclass == C_FILE) |
634 | { | |
635 | strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN); | |
636 | auxent = &(native + 1)->u.auxent; | |
075caafd | 637 | |
6dc6a81a ILT |
638 | if (bfd_coff_long_filenames (abfd)) |
639 | { | |
640 | if (name_length <= FILNMLEN) | |
641 | { | |
642 | strncpy (auxent->x_file.x_fname, name, FILNMLEN); | |
643 | } | |
644 | else | |
645 | { | |
f0500a41 | 646 | auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE; |
6dc6a81a | 647 | auxent->x_file.x_n.x_zeroes = 0; |
f0500a41 | 648 | *string_size_p += name_length + 1; |
6dc6a81a ILT |
649 | } |
650 | } | |
651 | else | |
652 | { | |
653 | strncpy (auxent->x_file.x_fname, name, FILNMLEN); | |
654 | if (name_length > FILNMLEN) | |
655 | { | |
656 | name[FILNMLEN] = '\0'; | |
657 | } | |
658 | } | |
075caafd | 659 | } |
075caafd | 660 | else |
9783e04a DM |
661 | { /* NOT A C_FILE SYMBOL */ |
662 | if (name_length <= SYMNMLEN) | |
663 | { | |
075caafd | 664 | /* This name will fit into the symbol neatly */ |
6dc6a81a | 665 | strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN); |
075caafd | 666 | } |
6dc6a81a | 667 | else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment)) |
9783e04a | 668 | { |
f0500a41 ILT |
669 | native->u.syment._n._n_n._n_offset = (*string_size_p |
670 | + STRING_SIZE_SIZE); | |
075caafd | 671 | native->u.syment._n._n_n._n_zeroes = 0; |
f0500a41 | 672 | *string_size_p += name_length + 1; |
075caafd | 673 | } |
9783e04a DM |
674 | else |
675 | { | |
676 | long filepos; | |
677 | bfd_byte buf[2]; | |
678 | ||
679 | /* This name should be written into the .debug section. For | |
680 | some reason each name is preceded by a two byte length | |
681 | and also followed by a null byte. FIXME: We assume that | |
682 | the .debug section has already been created, and that it | |
683 | is large enough. */ | |
f0500a41 ILT |
684 | if (*debug_string_section_p == (asection *) NULL) |
685 | *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug"); | |
9783e04a DM |
686 | filepos = bfd_tell (abfd); |
687 | bfd_put_16 (abfd, name_length + 1, buf); | |
6dc6a81a | 688 | if (!bfd_set_section_contents (abfd, |
f0500a41 | 689 | *debug_string_section_p, |
6dc6a81a | 690 | (PTR) buf, |
f0500a41 | 691 | (file_ptr) *debug_string_size_p, |
6dc6a81a ILT |
692 | (bfd_size_type) 2) |
693 | || !bfd_set_section_contents (abfd, | |
f0500a41 | 694 | *debug_string_section_p, |
6dc6a81a | 695 | (PTR) symbol->name, |
f0500a41 ILT |
696 | ((file_ptr) *debug_string_size_p |
697 | + 2), | |
6dc6a81a | 698 | (bfd_size_type) name_length + 1)) |
9783e04a DM |
699 | abort (); |
700 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) | |
701 | abort (); | |
f0500a41 | 702 | native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2; |
9783e04a | 703 | native->u.syment._n._n_n._n_zeroes = 0; |
f0500a41 | 704 | *debug_string_size_p += name_length + 3; |
9783e04a DM |
705 | } |
706 | } | |
075caafd ILT |
707 | } |
708 | ||
bfe8224f ILT |
709 | /* We need to keep track of the symbol index so that when we write out |
710 | the relocs we can get the index for a symbol. This method is a | |
711 | hack. FIXME. */ | |
712 | ||
74942465 | 713 | #define set_index(symbol, idx) ((symbol)->udata.i = (idx)) |
bfe8224f ILT |
714 | |
715 | /* Write a symbol out to a COFF file. */ | |
075caafd | 716 | |
bfe8224f | 717 | static boolean |
f0500a41 ILT |
718 | coff_write_symbol (abfd, symbol, native, written, string_size_p, |
719 | debug_string_section_p, debug_string_size_p) | |
25057836 JL |
720 | bfd *abfd; |
721 | asymbol *symbol; | |
722 | combined_entry_type *native; | |
bfe8224f | 723 | unsigned int *written; |
f0500a41 ILT |
724 | bfd_size_type *string_size_p; |
725 | asection **debug_string_section_p; | |
726 | bfd_size_type *debug_string_size_p; | |
075caafd | 727 | { |
bfe8224f ILT |
728 | unsigned int numaux = native->u.syment.n_numaux; |
729 | int type = native->u.syment.n_type; | |
6dc6a81a | 730 | int class = native->u.syment.n_sclass; |
075caafd ILT |
731 | PTR buf; |
732 | bfd_size_type symesz; | |
733 | ||
075caafd | 734 | if (native->u.syment.n_sclass == C_FILE) |
6dc6a81a | 735 | symbol->flags |= BSF_DEBUGGING; |
075caafd | 736 | |
6dc6a81a | 737 | if (symbol->flags & BSF_DEBUGGING) |
bfe8224f | 738 | { |
6dc6a81a | 739 | native->u.syment.n_scnum = N_DEBUG; |
bfe8224f | 740 | } |
6dc6a81a | 741 | else if (bfd_is_abs_section (symbol->section)) |
bfe8224f | 742 | { |
6dc6a81a | 743 | native->u.syment.n_scnum = N_ABS; |
bfe8224f | 744 | } |
e8fbe6d9 | 745 | else if (bfd_is_und_section (symbol->section)) |
bfe8224f ILT |
746 | { |
747 | native->u.syment.n_scnum = N_UNDEF; | |
748 | } | |
6dc6a81a | 749 | else |
bfe8224f ILT |
750 | { |
751 | native->u.syment.n_scnum = | |
752 | symbol->section->output_section->target_index; | |
753 | } | |
6dc6a81a | 754 | |
f0500a41 ILT |
755 | coff_fix_symbol_name (abfd, symbol, native, string_size_p, |
756 | debug_string_section_p, debug_string_size_p); | |
075caafd ILT |
757 | |
758 | symesz = bfd_coff_symesz (abfd); | |
759 | buf = bfd_alloc (abfd, symesz); | |
9783e04a DM |
760 | if (!buf) |
761 | { | |
d1ad85a6 | 762 | bfd_set_error (bfd_error_no_memory); |
bfe8224f | 763 | return false; |
9783e04a | 764 | } |
bfe8224f ILT |
765 | bfd_coff_swap_sym_out (abfd, &native->u.syment, buf); |
766 | if (bfd_write (buf, 1, symesz, abfd) != symesz) | |
767 | return false; | |
075caafd ILT |
768 | bfd_release (abfd, buf); |
769 | ||
770 | if (native->u.syment.n_numaux > 0) | |
771 | { | |
772 | bfd_size_type auxesz; | |
773 | unsigned int j; | |
774 | ||
775 | auxesz = bfd_coff_auxesz (abfd); | |
776 | buf = bfd_alloc (abfd, auxesz); | |
9783e04a DM |
777 | if (!buf) |
778 | { | |
d1ad85a6 | 779 | bfd_set_error (bfd_error_no_memory); |
bfe8224f | 780 | return false; |
9783e04a | 781 | } |
6dc6a81a | 782 | for (j = 0; j < native->u.syment.n_numaux; j++) |
075caafd | 783 | { |
bfe8224f ILT |
784 | bfd_coff_swap_aux_out (abfd, |
785 | &((native + j + 1)->u.auxent), | |
786 | type, | |
787 | class, | |
788 | j, | |
789 | native->u.syment.n_numaux, | |
790 | buf); | |
6dc6a81a | 791 | if (bfd_write (buf, 1, auxesz, abfd) != auxesz) |
bfe8224f | 792 | return false; |
075caafd ILT |
793 | } |
794 | bfd_release (abfd, buf); | |
795 | } | |
bfe8224f ILT |
796 | |
797 | /* Store the index for use when we write out the relocs. */ | |
798 | set_index (symbol, *written); | |
799 | ||
800 | *written += numaux + 1; | |
801 | return true; | |
075caafd ILT |
802 | } |
803 | ||
bfe8224f ILT |
804 | /* Write out a symbol to a COFF file that does not come from a COFF |
805 | file originally. This symbol may have been created by the linker, | |
806 | or we may be linking a non COFF file to a COFF file. */ | |
075caafd | 807 | |
bfe8224f | 808 | static boolean |
f0500a41 ILT |
809 | coff_write_alien_symbol (abfd, symbol, written, string_size_p, |
810 | debug_string_section_p, debug_string_size_p) | |
25057836 JL |
811 | bfd *abfd; |
812 | asymbol *symbol; | |
bfe8224f | 813 | unsigned int *written; |
f0500a41 ILT |
814 | bfd_size_type *string_size_p; |
815 | asection **debug_string_section_p; | |
816 | bfd_size_type *debug_string_size_p; | |
075caafd | 817 | { |
075caafd ILT |
818 | combined_entry_type *native; |
819 | combined_entry_type dummy; | |
bfe8224f | 820 | |
075caafd | 821 | native = &dummy; |
6dc6a81a ILT |
822 | native->u.syment.n_type = T_NULL; |
823 | native->u.syment.n_flags = 0; | |
e8fbe6d9 | 824 | if (bfd_is_und_section (symbol->section)) |
bfe8224f ILT |
825 | { |
826 | native->u.syment.n_scnum = N_UNDEF; | |
827 | native->u.syment.n_value = symbol->value; | |
075caafd | 828 | } |
e61cfdf8 | 829 | else if (bfd_is_com_section (symbol->section)) |
bfe8224f ILT |
830 | { |
831 | native->u.syment.n_scnum = N_UNDEF; | |
832 | native->u.syment.n_value = symbol->value; | |
075caafd | 833 | } |
bfe8224f | 834 | else if (symbol->flags & BSF_DEBUGGING) |
075caafd | 835 | { |
a74d1517 ILT |
836 | /* There isn't much point to writing out a debugging symbol |
837 | unless we are prepared to convert it into COFF debugging | |
715cde57 ILT |
838 | format. So, we just ignore them. We must clobber the symbol |
839 | name to keep it from being put in the string table. */ | |
840 | symbol->name = ""; | |
a74d1517 | 841 | return true; |
075caafd | 842 | } |
bfe8224f ILT |
843 | else |
844 | { | |
845 | native->u.syment.n_scnum = | |
846 | symbol->section->output_section->target_index; | |
847 | native->u.syment.n_value = (symbol->value | |
848 | + symbol->section->output_section->vma | |
849 | + symbol->section->output_offset); | |
850 | ||
851 | /* Copy the any flags from the the file header into the symbol. | |
6dc6a81a | 852 | FIXME: Why? */ |
bfe8224f ILT |
853 | { |
854 | coff_symbol_type *c = coff_symbol_from (abfd, symbol); | |
855 | if (c != (coff_symbol_type *) NULL) | |
856 | native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags; | |
857 | } | |
075caafd ILT |
858 | } |
859 | ||
bfe8224f | 860 | native->u.syment.n_type = 0; |
075caafd | 861 | if (symbol->flags & BSF_LOCAL) |
bfe8224f | 862 | native->u.syment.n_sclass = C_STAT; |
075caafd | 863 | else |
bfe8224f ILT |
864 | native->u.syment.n_sclass = C_EXT; |
865 | native->u.syment.n_numaux = 0; | |
075caafd | 866 | |
f0500a41 ILT |
867 | return coff_write_symbol (abfd, symbol, native, written, string_size_p, |
868 | debug_string_section_p, debug_string_size_p); | |
075caafd ILT |
869 | } |
870 | ||
bfe8224f ILT |
871 | /* Write a native symbol to a COFF file. */ |
872 | ||
873 | static boolean | |
f0500a41 ILT |
874 | coff_write_native_symbol (abfd, symbol, written, string_size_p, |
875 | debug_string_section_p, debug_string_size_p) | |
25057836 JL |
876 | bfd *abfd; |
877 | coff_symbol_type *symbol; | |
bfe8224f | 878 | unsigned int *written; |
f0500a41 ILT |
879 | bfd_size_type *string_size_p; |
880 | asection **debug_string_section_p; | |
881 | bfd_size_type *debug_string_size_p; | |
075caafd | 882 | { |
075caafd | 883 | combined_entry_type *native = symbol->native; |
bfe8224f | 884 | alent *lineno = symbol->lineno; |
075caafd | 885 | |
bfe8224f ILT |
886 | /* If this symbol has an associated line number, we must store the |
887 | symbol index in the line number field. We also tag the auxent to | |
888 | point to the right place in the lineno table. */ | |
889 | if (lineno && !symbol->done_lineno) | |
890 | { | |
891 | unsigned int count = 0; | |
892 | lineno[count].u.offset = *written; | |
893 | if (native->u.syment.n_numaux) | |
894 | { | |
6dc6a81a | 895 | union internal_auxent *a = &((native + 1)->u.auxent); |
075caafd | 896 | |
bfe8224f ILT |
897 | a->x_sym.x_fcnary.x_fcn.x_lnnoptr = |
898 | symbol->symbol.section->output_section->moving_line_filepos; | |
899 | } | |
075caafd | 900 | |
bfe8224f ILT |
901 | /* Count and relocate all other linenumbers. */ |
902 | count++; | |
903 | while (lineno[count].line_number != 0) | |
904 | { | |
075caafd | 905 | #if 0 |
bfe8224f ILT |
906 | /* 13 april 92. sac |
907 | I've been told this, but still need proof: | |
908 | > The second bug is also in `bfd/coffcode.h'. This bug | |
909 | > causes the linker to screw up the pc-relocations for | |
910 | > all the line numbers in COFF code. This bug isn't only | |
911 | > specific to A29K implementations, but affects all | |
912 | > systems using COFF format binaries. Note that in COFF | |
913 | > object files, the line number core offsets output by | |
914 | > the assembler are relative to the start of each | |
915 | > procedure, not to the start of the .text section. This | |
916 | > patch relocates the line numbers relative to the | |
917 | > `native->u.syment.n_value' instead of the section | |
918 | > virtual address. | |
919 | > modular!olson@cs.arizona.edu (Jon Olson) | |
6dc6a81a | 920 | */ |
bfe8224f | 921 | lineno[count].u.offset += native->u.syment.n_value; |
075caafd | 922 | #else |
bfe8224f ILT |
923 | lineno[count].u.offset += |
924 | (symbol->symbol.section->output_section->vma | |
925 | + symbol->symbol.section->output_offset); | |
075caafd | 926 | #endif |
bfe8224f ILT |
927 | count++; |
928 | } | |
929 | symbol->done_lineno = true; | |
6dc6a81a | 930 | |
bfe8224f ILT |
931 | symbol->symbol.section->output_section->moving_line_filepos += |
932 | count * bfd_coff_linesz (abfd); | |
933 | } | |
934 | ||
f0500a41 ILT |
935 | return coff_write_symbol (abfd, &(symbol->symbol), native, written, |
936 | string_size_p, debug_string_section_p, | |
937 | debug_string_size_p); | |
075caafd ILT |
938 | } |
939 | ||
bfe8224f ILT |
940 | /* Write out the COFF symbols. */ |
941 | ||
942 | boolean | |
25057836 | 943 | coff_write_symbols (abfd) |
bfe8224f | 944 | bfd *abfd; |
075caafd | 945 | { |
f0500a41 ILT |
946 | bfd_size_type string_size; |
947 | asection *debug_string_section; | |
948 | bfd_size_type debug_string_size; | |
bfe8224f | 949 | unsigned int i; |
6dc6a81a | 950 | unsigned int limit = bfd_get_symcount (abfd); |
bfe8224f ILT |
951 | unsigned int written = 0; |
952 | asymbol **p; | |
075caafd ILT |
953 | |
954 | string_size = 0; | |
f0500a41 | 955 | debug_string_section = NULL; |
9783e04a | 956 | debug_string_size = 0; |
075caafd ILT |
957 | |
958 | /* Seek to the right place */ | |
6dc6a81a | 959 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0) |
bfe8224f | 960 | return false; |
075caafd ILT |
961 | |
962 | /* Output all the symbols we have */ | |
963 | ||
964 | written = 0; | |
965 | for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) | |
bfe8224f ILT |
966 | { |
967 | asymbol *symbol = *p; | |
968 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol); | |
075caafd | 969 | |
bfe8224f | 970 | if (c_symbol == (coff_symbol_type *) NULL |
6dc6a81a | 971 | || c_symbol->native == (combined_entry_type *) NULL) |
bfe8224f | 972 | { |
f0500a41 ILT |
973 | if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size, |
974 | &debug_string_section, | |
975 | &debug_string_size)) | |
bfe8224f ILT |
976 | return false; |
977 | } | |
978 | else | |
979 | { | |
f0500a41 ILT |
980 | if (!coff_write_native_symbol (abfd, c_symbol, &written, |
981 | &string_size, &debug_string_section, | |
982 | &debug_string_size)) | |
bfe8224f ILT |
983 | return false; |
984 | } | |
985 | } | |
075caafd | 986 | |
74942465 ILT |
987 | obj_raw_syment_count (abfd) = written; |
988 | ||
075caafd ILT |
989 | /* Now write out strings */ |
990 | ||
991 | if (string_size != 0) | |
6dc6a81a ILT |
992 | { |
993 | unsigned int size = string_size + STRING_SIZE_SIZE; | |
994 | bfd_byte buffer[STRING_SIZE_SIZE]; | |
075caafd | 995 | |
9fbe895a | 996 | #if STRING_SIZE_SIZE == 4 |
6dc6a81a | 997 | bfd_h_put_32 (abfd, size, buffer); |
9fbe895a ILT |
998 | #else |
999 | #error Change bfd_h_put_32 | |
1000 | #endif | |
6dc6a81a ILT |
1001 | if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer)) |
1002 | return false; | |
1003 | for (p = abfd->outsymbols, i = 0; | |
1004 | i < limit; | |
1005 | i++, p++) | |
1006 | { | |
1007 | asymbol *q = *p; | |
1008 | size_t name_length = strlen (q->name); | |
1009 | coff_symbol_type *c_symbol = coff_symbol_from (abfd, q); | |
1010 | size_t maxlen; | |
1011 | ||
1012 | /* Figure out whether the symbol name should go in the string | |
1013 | table. Symbol names that are short enough are stored | |
1014 | directly in the syment structure. File names permit a | |
1015 | different, longer, length in the syment structure. On | |
1016 | XCOFF, some symbol names are stored in the .debug section | |
1017 | rather than in the string table. */ | |
1018 | ||
1019 | if (c_symbol == NULL | |
1020 | || c_symbol->native == NULL) | |
1021 | { | |
1022 | /* This is not a COFF symbol, so it certainly is not a | |
1023 | file name, nor does it go in the .debug section. */ | |
1024 | maxlen = SYMNMLEN; | |
1025 | } | |
1026 | else if (bfd_coff_symname_in_debug (abfd, | |
1027 | &c_symbol->native->u.syment)) | |
1028 | { | |
1029 | /* This symbol name is in the XCOFF .debug section. | |
1030 | Don't write it into the string table. */ | |
1031 | maxlen = name_length; | |
1032 | } | |
1033 | else if (c_symbol->native->u.syment.n_sclass == C_FILE) | |
1034 | maxlen = FILNMLEN; | |
1035 | else | |
1036 | maxlen = SYMNMLEN; | |
1037 | ||
1038 | if (name_length > maxlen) | |
1039 | { | |
1040 | if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd) | |
1041 | != name_length + 1) | |
1042 | return false; | |
1043 | } | |
1044 | } | |
1045 | } | |
bfe8224f ILT |
1046 | else |
1047 | { | |
1048 | /* We would normally not write anything here, but we'll write | |
6dc6a81a ILT |
1049 | out 4 so that any stupid coff reader which tries to read the |
1050 | string table even when there isn't one won't croak. */ | |
9fbe895a ILT |
1051 | unsigned int size = STRING_SIZE_SIZE; |
1052 | bfd_byte buffer[STRING_SIZE_SIZE]; | |
bfe8224f | 1053 | |
9fbe895a | 1054 | #if STRING_SIZE_SIZE == 4 |
bfe8224f | 1055 | bfd_h_put_32 (abfd, size, buffer); |
9fbe895a ILT |
1056 | #else |
1057 | #error Change bfd_h_put_32 | |
1058 | #endif | |
1059 | if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd) | |
1060 | != STRING_SIZE_SIZE) | |
bfe8224f ILT |
1061 | return false; |
1062 | } | |
9783e04a | 1063 | |
bfe8224f ILT |
1064 | /* Make sure the .debug section was created to be the correct size. |
1065 | We should create it ourselves on the fly, but we don't because | |
1066 | BFD won't let us write to any section until we know how large all | |
1067 | the sections are. We could still do it by making another pass | |
1068 | over the symbols. FIXME. */ | |
9783e04a DM |
1069 | BFD_ASSERT (debug_string_size == 0 |
1070 | || (debug_string_section != (asection *) NULL | |
1071 | && (BFD_ALIGN (debug_string_size, | |
1072 | 1 << debug_string_section->alignment_power) | |
1073 | == bfd_section_size (abfd, debug_string_section)))); | |
bfe8224f ILT |
1074 | |
1075 | return true; | |
075caafd ILT |
1076 | } |
1077 | ||
9783e04a | 1078 | boolean |
25057836 | 1079 | coff_write_linenumbers (abfd) |
6dc6a81a | 1080 | bfd *abfd; |
075caafd | 1081 | { |
6dc6a81a | 1082 | asection *s; |
075caafd ILT |
1083 | bfd_size_type linesz; |
1084 | PTR buff; | |
1085 | ||
1086 | linesz = bfd_coff_linesz (abfd); | |
1087 | buff = bfd_alloc (abfd, linesz); | |
9783e04a DM |
1088 | if (!buff) |
1089 | { | |
d1ad85a6 | 1090 | bfd_set_error (bfd_error_no_memory); |
25057836 | 1091 | return false; |
9783e04a | 1092 | } |
6dc6a81a ILT |
1093 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
1094 | { | |
1095 | if (s->lineno_count) | |
1096 | { | |
1097 | asymbol **q = abfd->outsymbols; | |
1098 | if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0) | |
1099 | return false; | |
1100 | /* Find all the linenumbers in this section */ | |
1101 | while (*q) | |
1102 | { | |
1103 | asymbol *p = *q; | |
1104 | if (p->section->output_section == s) | |
1105 | { | |
1106 | alent *l = | |
1107 | BFD_SEND (bfd_asymbol_bfd (p), _get_lineno, | |
1108 | (bfd_asymbol_bfd (p), p)); | |
1109 | if (l) | |
1110 | { | |
1111 | /* Found a linenumber entry, output */ | |
1112 | struct internal_lineno out; | |
1113 | memset ((PTR) & out, 0, sizeof (out)); | |
1114 | out.l_lnno = 0; | |
1115 | out.l_addr.l_symndx = l->u.offset; | |
1116 | bfd_coff_swap_lineno_out (abfd, &out, buff); | |
1117 | if (bfd_write (buff, 1, linesz, abfd) != linesz) | |
1118 | return false; | |
1119 | l++; | |
1120 | while (l->line_number) | |
1121 | { | |
1122 | out.l_lnno = l->line_number; | |
1123 | out.l_addr.l_symndx = l->u.offset; | |
1124 | bfd_coff_swap_lineno_out (abfd, &out, buff); | |
1125 | if (bfd_write (buff, 1, linesz, abfd) != linesz) | |
1126 | return false; | |
1127 | l++; | |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | q++; | |
27f524a3 | 1132 | } |
075caafd | 1133 | } |
075caafd | 1134 | } |
075caafd | 1135 | bfd_release (abfd, buff); |
9783e04a | 1136 | return true; |
075caafd ILT |
1137 | } |
1138 | ||
6dc6a81a ILT |
1139 | /*ARGSUSED */ |
1140 | alent * | |
25057836 | 1141 | coff_get_lineno (ignore_abfd, symbol) |
6dc6a81a ILT |
1142 | bfd *ignore_abfd; |
1143 | asymbol *symbol; | |
075caafd | 1144 | { |
6dc6a81a | 1145 | return coffsymbol (symbol)->lineno; |
075caafd ILT |
1146 | } |
1147 | ||
1148 | asymbol * | |
1149 | coff_section_symbol (abfd, name) | |
1150 | bfd *abfd; | |
1151 | char *name; | |
1152 | { | |
1153 | asection *sec = bfd_make_section_old_way (abfd, name); | |
1154 | asymbol *sym; | |
1155 | combined_entry_type *csym; | |
1156 | ||
1157 | sym = sec->symbol; | |
4c3721d5 | 1158 | csym = coff_symbol_from (abfd, sym)->native; |
075caafd ILT |
1159 | /* Make sure back-end COFF stuff is there. */ |
1160 | if (csym == 0) | |
1161 | { | |
6dc6a81a ILT |
1162 | struct foo |
1163 | { | |
1164 | coff_symbol_type sym; | |
1165 | /* @@FIXME This shouldn't use a fixed size!! */ | |
1166 | combined_entry_type e[10]; | |
1167 | }; | |
075caafd ILT |
1168 | struct foo *f; |
1169 | f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f)); | |
9783e04a DM |
1170 | if (!f) |
1171 | { | |
d1ad85a6 | 1172 | bfd_set_error (bfd_error_no_error); |
9783e04a DM |
1173 | return NULL; |
1174 | } | |
075caafd ILT |
1175 | memset ((char *) f, 0, sizeof (*f)); |
1176 | coff_symbol_from (abfd, sym)->native = csym = f->e; | |
1177 | } | |
1178 | csym[0].u.syment.n_sclass = C_STAT; | |
1179 | csym[0].u.syment.n_numaux = 1; | |
6dc6a81a | 1180 | /* SF_SET_STATICS (sym); @@ ??? */ |
4c3721d5 ILT |
1181 | csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size; |
1182 | csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count; | |
1183 | csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count; | |
1184 | ||
1185 | if (sec->output_section == NULL) | |
075caafd | 1186 | { |
4c3721d5 ILT |
1187 | sec->output_section = sec; |
1188 | sec->output_offset = 0; | |
075caafd | 1189 | } |
4c3721d5 | 1190 | |
075caafd ILT |
1191 | return sym; |
1192 | } | |
1193 | ||
1194 | /* This function transforms the offsets into the symbol table into | |
1195 | pointers to syments. */ | |
1196 | ||
1197 | static void | |
25057836 JL |
1198 | coff_pointerize_aux (abfd, table_base, type, class, auxent) |
1199 | bfd *abfd; | |
1200 | combined_entry_type *table_base; | |
ae115e51 ILT |
1201 | unsigned int type; |
1202 | unsigned int class; | |
25057836 | 1203 | combined_entry_type *auxent; |
075caafd ILT |
1204 | { |
1205 | /* Don't bother if this is a file or a section */ | |
6dc6a81a ILT |
1206 | if (class == C_STAT && type == T_NULL) |
1207 | return; | |
1208 | if (class == C_FILE) | |
1209 | return; | |
075caafd ILT |
1210 | |
1211 | /* Otherwise patch up */ | |
1212 | #define N_TMASK coff_data (abfd)->local_n_tmask | |
1213 | #define N_BTSHFT coff_data (abfd)->local_n_btshft | |
6dc6a81a | 1214 | if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK) |
69645d10 ILT |
1215 | && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0) |
1216 | { | |
1217 | auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = | |
1218 | (table_base | |
1219 | + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l); | |
075caafd ILT |
1220 | auxent->fix_end = 1; |
1221 | } | |
1222 | /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can | |
1223 | generate one, so we must be careful to ignore it. */ | |
6dc6a81a ILT |
1224 | if (auxent->u.auxent.x_sym.x_tagndx.l > 0) |
1225 | { | |
075caafd | 1226 | auxent->u.auxent.x_sym.x_tagndx.p = |
6dc6a81a | 1227 | table_base + auxent->u.auxent.x_sym.x_tagndx.l; |
075caafd ILT |
1228 | auxent->fix_tag = 1; |
1229 | } | |
1230 | } | |
1231 | ||
075caafd ILT |
1232 | /* Allocate space for the ".debug" section, and read it. |
1233 | We did not read the debug section until now, because | |
1234 | we didn't want to go to the trouble until someone needed it. */ | |
1235 | ||
1236 | static char * | |
25057836 JL |
1237 | build_debug_section (abfd) |
1238 | bfd *abfd; | |
075caafd ILT |
1239 | { |
1240 | char *debug_section; | |
1241 | long position; | |
1242 | ||
1243 | asection *sect = bfd_get_section_by_name (abfd, ".debug"); | |
1244 | ||
6dc6a81a ILT |
1245 | if (!sect) |
1246 | { | |
1247 | bfd_set_error (bfd_error_no_debug_section); | |
1248 | return NULL; | |
1249 | } | |
075caafd ILT |
1250 | |
1251 | debug_section = (PTR) bfd_alloc (abfd, | |
1252 | bfd_get_section_size_before_reloc (sect)); | |
6dc6a81a ILT |
1253 | if (debug_section == NULL) |
1254 | { | |
1255 | bfd_set_error (bfd_error_no_memory); | |
1256 | return NULL; | |
1257 | } | |
075caafd ILT |
1258 | |
1259 | /* Seek to the beginning of the `.debug' section and read it. | |
1260 | Save the current position first; it is needed by our caller. | |
1261 | Then read debug section and reset the file pointer. */ | |
1262 | ||
1263 | position = bfd_tell (abfd); | |
c16313f0 | 1264 | if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0 |
6dc6a81a | 1265 | || (bfd_read (debug_section, |
c16313f0 | 1266 | bfd_get_section_size_before_reloc (sect), 1, abfd) |
6dc6a81a | 1267 | != bfd_get_section_size_before_reloc (sect)) |
c16313f0 | 1268 | || bfd_seek (abfd, position, SEEK_SET) != 0) |
075caafd | 1269 | return NULL; |
075caafd ILT |
1270 | return debug_section; |
1271 | } | |
1272 | ||
1273 | ||
1274 | /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be | |
6dc6a81a ILT |
1275 | \0-terminated, but will not exceed 'maxlen' characters. The copy *will* |
1276 | be \0-terminated. */ | |
075caafd | 1277 | static char * |
25057836 JL |
1278 | copy_name (abfd, name, maxlen) |
1279 | bfd *abfd; | |
1280 | char *name; | |
1281 | int maxlen; | |
075caafd | 1282 | { |
6dc6a81a | 1283 | int len; |
075caafd ILT |
1284 | char *newname; |
1285 | ||
6dc6a81a ILT |
1286 | for (len = 0; len < maxlen; ++len) |
1287 | { | |
1288 | if (name[len] == '\0') | |
1289 | { | |
1290 | break; | |
1291 | } | |
075caafd | 1292 | } |
075caafd | 1293 | |
6dc6a81a ILT |
1294 | if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL) |
1295 | { | |
1296 | bfd_set_error (bfd_error_no_memory); | |
1297 | return (NULL); | |
1298 | } | |
1299 | strncpy (newname, name, len); | |
075caafd ILT |
1300 | newname[len] = '\0'; |
1301 | return newname; | |
1302 | } | |
1303 | ||
ae115e51 ILT |
1304 | /* Read in the external symbols. */ |
1305 | ||
1306 | boolean | |
1307 | _bfd_coff_get_external_symbols (abfd) | |
1308 | bfd *abfd; | |
1309 | { | |
1310 | bfd_size_type symesz; | |
1311 | size_t size; | |
1312 | PTR syms; | |
1313 | ||
1314 | if (obj_coff_external_syms (abfd) != NULL) | |
1315 | return true; | |
1316 | ||
1317 | symesz = bfd_coff_symesz (abfd); | |
1318 | ||
1319 | size = obj_raw_syment_count (abfd) * symesz; | |
1320 | ||
1321 | syms = malloc (size); | |
1322 | if (syms == NULL && size != 0) | |
1323 | { | |
1324 | bfd_set_error (bfd_error_no_memory); | |
1325 | return false; | |
1326 | } | |
1327 | ||
1328 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0 | |
1329 | || bfd_read (syms, size, 1, abfd) != size) | |
1330 | { | |
1331 | if (syms != NULL) | |
1332 | free (syms); | |
1333 | return false; | |
1334 | } | |
1335 | ||
1336 | obj_coff_external_syms (abfd) = syms; | |
1337 | ||
1338 | return true; | |
1339 | } | |
1340 | ||
1341 | /* Read in the external strings. The strings are not loaded until | |
1342 | they are needed. This is because we have no simple way of | |
1343 | detecting a missing string table in an archive. */ | |
1344 | ||
1345 | const char * | |
1346 | _bfd_coff_read_string_table (abfd) | |
1347 | bfd *abfd; | |
1348 | { | |
1349 | char extstrsize[STRING_SIZE_SIZE]; | |
1350 | size_t strsize; | |
1351 | char *strings; | |
1352 | ||
1353 | if (obj_coff_strings (abfd) != NULL) | |
1354 | return obj_coff_strings (abfd); | |
1355 | ||
1356 | if (bfd_seek (abfd, | |
1357 | (obj_sym_filepos (abfd) | |
1358 | + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)), | |
1359 | SEEK_SET) != 0) | |
1360 | return NULL; | |
1361 | ||
1362 | if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize) | |
1363 | { | |
1364 | if (bfd_get_error () != bfd_error_file_truncated) | |
1365 | return NULL; | |
1366 | ||
1367 | /* There is no string table. */ | |
1368 | strsize = STRING_SIZE_SIZE; | |
1369 | } | |
1370 | else | |
1371 | { | |
1372 | #if STRING_SIZE_SIZE == 4 | |
1373 | strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize); | |
1374 | #else | |
1375 | #error Change bfd_h_get_32 | |
1376 | #endif | |
1377 | } | |
1378 | ||
1379 | strings = malloc (strsize); | |
1380 | if (strings == NULL) | |
1381 | { | |
1382 | bfd_set_error (bfd_error_no_memory); | |
1383 | return NULL; | |
1384 | } | |
1385 | ||
1386 | if (bfd_read (strings + STRING_SIZE_SIZE, | |
1387 | strsize - STRING_SIZE_SIZE, 1, abfd) | |
1388 | != strsize - STRING_SIZE_SIZE) | |
1389 | { | |
1390 | free (strings); | |
1391 | return NULL; | |
1392 | } | |
1393 | ||
1394 | obj_coff_strings (abfd) = strings; | |
1395 | ||
1396 | return strings; | |
1397 | } | |
1398 | ||
1399 | /* Free up the external symbols and strings read from a COFF file. */ | |
1400 | ||
1401 | boolean | |
1402 | _bfd_coff_free_symbols (abfd) | |
1403 | bfd *abfd; | |
1404 | { | |
1405 | if (obj_coff_external_syms (abfd) != NULL | |
1406 | && ! obj_coff_keep_syms (abfd)) | |
1407 | { | |
1408 | free (obj_coff_external_syms (abfd)); | |
1409 | obj_coff_external_syms (abfd) = NULL; | |
1410 | } | |
1411 | if (obj_coff_strings (abfd) != NULL | |
1412 | && ! obj_coff_keep_strings (abfd)) | |
1413 | { | |
1414 | free (obj_coff_strings (abfd)); | |
1415 | obj_coff_strings (abfd) = NULL; | |
1416 | } | |
1417 | return true; | |
1418 | } | |
1419 | ||
075caafd ILT |
1420 | /* Read a symbol table into freshly bfd_allocated memory, swap it, and |
1421 | knit the symbol names into a normalized form. By normalized here I | |
1422 | mean that all symbols have an n_offset pointer that points to a null- | |
1423 | terminated string. */ | |
1424 | ||
1425 | combined_entry_type * | |
25057836 | 1426 | coff_get_normalized_symtab (abfd) |
6dc6a81a | 1427 | bfd *abfd; |
075caafd | 1428 | { |
6dc6a81a ILT |
1429 | combined_entry_type *internal; |
1430 | combined_entry_type *internal_ptr; | |
1431 | combined_entry_type *symbol_ptr; | |
1432 | combined_entry_type *internal_end; | |
075caafd | 1433 | bfd_size_type symesz; |
075caafd ILT |
1434 | char *raw_src; |
1435 | char *raw_end; | |
ae115e51 | 1436 | const char *string_table = NULL; |
6dc6a81a ILT |
1437 | char *debug_section = NULL; |
1438 | unsigned long size; | |
075caafd | 1439 | |
74942465 ILT |
1440 | if (obj_raw_syments (abfd) != NULL) |
1441 | return obj_raw_syments (abfd); | |
1442 | ||
1443 | size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type); | |
1444 | internal = (combined_entry_type *) bfd_alloc (abfd, size); | |
1445 | if (internal == NULL && size != 0) | |
9783e04a | 1446 | { |
d1ad85a6 | 1447 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1448 | return NULL; |
1449 | } | |
69645d10 | 1450 | internal_end = internal + obj_raw_syment_count (abfd); |
075caafd | 1451 | |
ae115e51 | 1452 | if (! _bfd_coff_get_external_symbols (abfd)) |
74942465 ILT |
1453 | return NULL; |
1454 | ||
ae115e51 ILT |
1455 | raw_src = (char *) obj_coff_external_syms (abfd); |
1456 | ||
075caafd | 1457 | /* mark the end of the symbols */ |
ae115e51 ILT |
1458 | symesz = bfd_coff_symesz (abfd); |
1459 | raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz; | |
74942465 | 1460 | |
6dc6a81a ILT |
1461 | /* FIXME SOMEDAY. A string table size of zero is very weird, but |
1462 | probably possible. If one shows up, it will probably kill us. */ | |
075caafd ILT |
1463 | |
1464 | /* Swap all the raw entries */ | |
ae115e51 | 1465 | for (internal_ptr = internal; |
075caafd | 1466 | raw_src < raw_end; |
6dc6a81a ILT |
1467 | raw_src += symesz, internal_ptr++) |
1468 | { | |
075caafd ILT |
1469 | |
1470 | unsigned int i; | |
6dc6a81a ILT |
1471 | bfd_coff_swap_sym_in (abfd, (PTR) raw_src, |
1472 | (PTR) & internal_ptr->u.syment); | |
9783e04a | 1473 | internal_ptr->fix_value = 0; |
075caafd ILT |
1474 | internal_ptr->fix_tag = 0; |
1475 | internal_ptr->fix_end = 0; | |
9783e04a | 1476 | internal_ptr->fix_scnlen = 0; |
075caafd | 1477 | symbol_ptr = internal_ptr; |
85fe7cff | 1478 | |
075caafd ILT |
1479 | for (i = 0; |
1480 | i < symbol_ptr->u.syment.n_numaux; | |
6dc6a81a | 1481 | i++) |
075caafd | 1482 | { |
6dc6a81a ILT |
1483 | internal_ptr++; |
1484 | raw_src += symesz; | |
1485 | ||
1486 | internal_ptr->fix_value = 0; | |
1487 | internal_ptr->fix_tag = 0; | |
1488 | internal_ptr->fix_end = 0; | |
1489 | internal_ptr->fix_scnlen = 0; | |
1490 | bfd_coff_swap_aux_in (abfd, (PTR) raw_src, | |
1491 | symbol_ptr->u.syment.n_type, | |
1492 | symbol_ptr->u.syment.n_sclass, | |
1493 | i, symbol_ptr->u.syment.n_numaux, | |
1494 | &(internal_ptr->u.auxent)); | |
1495 | /* Remember that bal entries arn't pointerized */ | |
1496 | if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC) | |
1497 | { | |
1498 | ||
1499 | coff_pointerize_aux (abfd, | |
1500 | internal, | |
1501 | symbol_ptr->u.syment.n_type, | |
1502 | symbol_ptr->u.syment.n_sclass, | |
1503 | internal_ptr); | |
1504 | } | |
1505 | ||
1506 | } | |
075caafd ILT |
1507 | } |
1508 | ||
ae115e51 ILT |
1509 | /* Free the raw symbols, but not the strings (if we have them). */ |
1510 | obj_coff_keep_strings (abfd) = true; | |
1511 | if (! _bfd_coff_free_symbols (abfd)) | |
1512 | return NULL; | |
075caafd ILT |
1513 | |
1514 | for (internal_ptr = internal; internal_ptr < internal_end; | |
6dc6a81a ILT |
1515 | internal_ptr++) |
1516 | { | |
1517 | if (internal_ptr->u.syment.n_sclass == C_FILE | |
1518 | && internal_ptr->u.syment.n_numaux > 0) | |
1519 | { | |
1520 | /* make a file symbol point to the name in the auxent, since | |
1521 | the text ".file" is redundant */ | |
1522 | if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0) | |
1523 | { | |
1524 | /* the filename is a long one, point into the string table */ | |
1525 | if (string_table == NULL) | |
ae115e51 ILT |
1526 | { |
1527 | string_table = _bfd_coff_read_string_table (abfd); | |
1528 | if (string_table == NULL) | |
1529 | return NULL; | |
1530 | } | |
075caafd | 1531 | |
6dc6a81a | 1532 | internal_ptr->u.syment._n._n_n._n_offset = |
ae115e51 ILT |
1533 | ((long) |
1534 | (string_table | |
1535 | + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset)); | |
6dc6a81a ILT |
1536 | } |
1537 | else | |
1538 | { | |
1539 | /* ordinary short filename, put into memory anyway */ | |
1540 | internal_ptr->u.syment._n._n_n._n_offset = (long) | |
1541 | copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname, | |
1542 | FILNMLEN); | |
1543 | } | |
1544 | } | |
1545 | else | |
1546 | { | |
1547 | if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) | |
1548 | { | |
1549 | /* This is a "short" name. Make it long. */ | |
1550 | unsigned long i = 0; | |
1551 | char *newstring = NULL; | |
1552 | ||
1553 | /* find the length of this string without walking into memory | |
1554 | that isn't ours. */ | |
1555 | for (i = 0; i < 8; ++i) | |
1556 | { | |
1557 | if (internal_ptr->u.syment._n._n_name[i] == '\0') | |
1558 | { | |
1559 | break; | |
1560 | } /* if end of string */ | |
1561 | } /* possible lengths of this string. */ | |
1562 | ||
1563 | if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL) | |
1564 | { | |
1565 | bfd_set_error (bfd_error_no_memory); | |
1566 | return (NULL); | |
1567 | } /* on error */ | |
1568 | memset (newstring, 0, i); | |
1569 | strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1); | |
1570 | internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring; | |
1571 | internal_ptr->u.syment._n._n_n._n_zeroes = 0; | |
1572 | } | |
1573 | else if (internal_ptr->u.syment._n._n_n._n_offset == 0) | |
1574 | internal_ptr->u.syment._n._n_n._n_offset = (long int) ""; | |
1575 | else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment)) | |
1576 | { | |
1577 | /* Long name already. Point symbol at the string in the | |
1578 | table. */ | |
1579 | if (string_table == NULL) | |
ae115e51 ILT |
1580 | { |
1581 | string_table = _bfd_coff_read_string_table (abfd); | |
1582 | if (string_table == NULL) | |
1583 | return NULL; | |
1584 | } | |
1585 | internal_ptr->u.syment._n._n_n._n_offset = | |
1586 | ((long int) | |
1587 | (string_table | |
1588 | + internal_ptr->u.syment._n._n_n._n_offset)); | |
6dc6a81a ILT |
1589 | } |
1590 | else | |
1591 | { | |
1592 | /* Long name in debug section. Very similar. */ | |
1593 | if (debug_section == NULL) | |
1594 | debug_section = build_debug_section (abfd); | |
1595 | internal_ptr->u.syment._n._n_n._n_offset = (long int) | |
1596 | (debug_section + internal_ptr->u.syment._n._n_n._n_offset); | |
1597 | } | |
1598 | } | |
1599 | internal_ptr += internal_ptr->u.syment.n_numaux; | |
1600 | } | |
1601 | ||
1602 | obj_raw_syments (abfd) = internal; | |
ae115e51 ILT |
1603 | BFD_ASSERT (obj_raw_syment_count (abfd) |
1604 | == (unsigned int) (internal_ptr - internal)); | |
075caafd ILT |
1605 | |
1606 | return (internal); | |
1607 | } /* coff_get_normalized_symtab() */ | |
1608 | ||
326e32d7 | 1609 | long |
25057836 | 1610 | coff_get_reloc_upper_bound (abfd, asect) |
6dc6a81a ILT |
1611 | bfd *abfd; |
1612 | sec_ptr asect; | |
075caafd | 1613 | { |
6dc6a81a ILT |
1614 | if (bfd_get_format (abfd) != bfd_object) |
1615 | { | |
1616 | bfd_set_error (bfd_error_invalid_operation); | |
1617 | return -1; | |
1618 | } | |
1619 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
075caafd ILT |
1620 | } |
1621 | ||
1622 | asymbol * | |
25057836 | 1623 | coff_make_empty_symbol (abfd) |
6dc6a81a | 1624 | bfd *abfd; |
075caafd | 1625 | { |
6dc6a81a ILT |
1626 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type)); |
1627 | if (new == NULL) | |
1628 | { | |
1629 | bfd_set_error (bfd_error_no_memory); | |
1630 | return (NULL); | |
1631 | } /* on error */ | |
9783e04a | 1632 | memset (new, 0, sizeof *new); |
075caafd ILT |
1633 | new->symbol.section = 0; |
1634 | new->native = 0; | |
1635 | new->lineno = (alent *) NULL; | |
1636 | new->done_lineno = false; | |
1637 | new->symbol.the_bfd = abfd; | |
1638 | return &new->symbol; | |
1639 | } | |
1640 | ||
2d1e6c9c KR |
1641 | /* Make a debugging symbol. */ |
1642 | ||
075caafd | 1643 | asymbol * |
2d1e6c9c KR |
1644 | coff_bfd_make_debug_symbol (abfd, ptr, sz) |
1645 | bfd *abfd; | |
1646 | PTR ptr; | |
1647 | unsigned long sz; | |
075caafd | 1648 | { |
6dc6a81a ILT |
1649 | coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type)); |
1650 | if (new == NULL) | |
1651 | { | |
1652 | bfd_set_error (bfd_error_no_memory); | |
1653 | return (NULL); | |
1654 | } /* on error */ | |
075caafd ILT |
1655 | /* @@ This shouldn't be using a constant multiplier. */ |
1656 | new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10); | |
9783e04a DM |
1657 | if (!new->native) |
1658 | { | |
d1ad85a6 | 1659 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1660 | return (NULL); |
1661 | } /* on error */ | |
6dc6a81a ILT |
1662 | new->symbol.section = bfd_abs_section_ptr; |
1663 | new->symbol.flags = BSF_DEBUGGING; | |
075caafd ILT |
1664 | new->lineno = (alent *) NULL; |
1665 | new->done_lineno = false; | |
1666 | new->symbol.the_bfd = abfd; | |
1667 | return &new->symbol; | |
1668 | } | |
1669 | ||
6dc6a81a | 1670 | /*ARGSUSED */ |
2d1e6c9c KR |
1671 | void |
1672 | coff_get_symbol_info (abfd, symbol, ret) | |
1673 | bfd *abfd; | |
1674 | asymbol *symbol; | |
1675 | symbol_info *ret; | |
1676 | { | |
1677 | bfd_symbol_info (symbol, ret); | |
1678 | } | |
1679 | ||
e61cfdf8 ILT |
1680 | /* Print out information about COFF symbol. */ |
1681 | ||
075caafd | 1682 | void |
e61cfdf8 ILT |
1683 | coff_print_symbol (abfd, filep, symbol, how) |
1684 | bfd *abfd; | |
1685 | PTR filep; | |
1686 | asymbol *symbol; | |
1687 | bfd_print_symbol_type how; | |
075caafd | 1688 | { |
e61cfdf8 ILT |
1689 | FILE *file = (FILE *) filep; |
1690 | ||
1691 | switch (how) | |
1692 | { | |
075caafd | 1693 | case bfd_print_symbol_name: |
e61cfdf8 | 1694 | fprintf (file, "%s", symbol->name); |
075caafd | 1695 | break; |
e61cfdf8 | 1696 | |
075caafd | 1697 | case bfd_print_symbol_more: |
e61cfdf8 | 1698 | fprintf (file, "coff %s %s", |
6dc6a81a ILT |
1699 | coffsymbol (symbol)->native ? "n" : "g", |
1700 | coffsymbol (symbol)->lineno ? "l" : " "); | |
075caafd | 1701 | break; |
075caafd | 1702 | |
e61cfdf8 | 1703 | case bfd_print_symbol_all: |
6dc6a81a | 1704 | if (coffsymbol (symbol)->native) |
075caafd | 1705 | { |
e61cfdf8 ILT |
1706 | unsigned int aux; |
1707 | combined_entry_type *combined = coffsymbol (symbol)->native; | |
1708 | combined_entry_type *root = obj_raw_syments (abfd); | |
6dc6a81a ILT |
1709 | struct lineno_cache_entry *l = coffsymbol (symbol)->lineno; |
1710 | ||
1711 | fprintf (file, "[%3ld]", (long) (combined - root)); | |
e61cfdf8 ILT |
1712 | |
1713 | fprintf (file, | |
6dc6a81a | 1714 | "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s", |
e61cfdf8 ILT |
1715 | combined->u.syment.n_scnum, |
1716 | combined->u.syment.n_flags, | |
1717 | combined->u.syment.n_type, | |
1718 | combined->u.syment.n_sclass, | |
1719 | combined->u.syment.n_numaux, | |
4c3721d5 | 1720 | (unsigned long) combined->u.syment.n_value, |
e61cfdf8 ILT |
1721 | symbol->name); |
1722 | ||
6dc6a81a | 1723 | for (aux = 0; aux < combined->u.syment.n_numaux; aux++) |
e61cfdf8 ILT |
1724 | { |
1725 | combined_entry_type *auxp = combined + aux + 1; | |
1726 | long tagndx; | |
1727 | ||
1728 | if (auxp->fix_tag) | |
1729 | tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root; | |
1730 | else | |
1731 | tagndx = auxp->u.auxent.x_sym.x_tagndx.l; | |
1732 | ||
1733 | fprintf (file, "\n"); | |
1734 | switch (combined->u.syment.n_sclass) | |
1735 | { | |
1736 | case C_FILE: | |
1737 | fprintf (file, "File "); | |
1738 | break; | |
e61cfdf8 | 1739 | |
de733a0e KR |
1740 | case C_STAT: |
1741 | if (combined->u.syment.n_type == T_NULL) | |
1742 | /* probably a section symbol? */ | |
1743 | { | |
1744 | fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d", | |
1745 | (long) auxp->u.auxent.x_scn.x_scnlen, | |
1746 | auxp->u.auxent.x_scn.x_nreloc, | |
1747 | auxp->u.auxent.x_scn.x_nlinno); | |
1748 | break; | |
1749 | } | |
1750 | /* else fall through */ | |
1751 | ||
1752 | default: | |
4c3721d5 | 1753 | fprintf (file, "AUX lnno %d size 0x%x tagndx %ld", |
e61cfdf8 ILT |
1754 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno, |
1755 | auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size, | |
1756 | tagndx); | |
69645d10 ILT |
1757 | if (auxp->fix_end) |
1758 | fprintf (file, " endndx %ld", | |
1759 | ((long) | |
1760 | (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p | |
1761 | - root))); | |
e61cfdf8 ILT |
1762 | break; |
1763 | } | |
075caafd | 1764 | } |
6dc6a81a | 1765 | |
e61cfdf8 ILT |
1766 | if (l) |
1767 | { | |
e4b6b3e7 | 1768 | fprintf (file, "\n%s :", l->u.sym->name); |
e61cfdf8 | 1769 | l++; |
6dc6a81a | 1770 | while (l->line_number) |
e61cfdf8 | 1771 | { |
4c3721d5 ILT |
1772 | fprintf (file, "\n%4d : 0x%lx", |
1773 | l->line_number, | |
1774 | ((unsigned long) | |
1775 | (l->u.offset + symbol->section->vma))); | |
e61cfdf8 ILT |
1776 | l++; |
1777 | } | |
1778 | } | |
6dc6a81a | 1779 | } |
e61cfdf8 | 1780 | else |
075caafd | 1781 | { |
e61cfdf8 ILT |
1782 | bfd_print_symbol_vandf ((PTR) file, symbol); |
1783 | fprintf (file, " %-5s %s %s %s", | |
1784 | symbol->section->name, | |
6dc6a81a ILT |
1785 | coffsymbol (symbol)->native ? "n" : "g", |
1786 | coffsymbol (symbol)->lineno ? "l" : " ", | |
e61cfdf8 | 1787 | symbol->name); |
075caafd | 1788 | } |
075caafd ILT |
1789 | } |
1790 | } | |
1791 | ||
1792 | /* Provided a BFD, a section and an offset into the section, calculate | |
1793 | and return the name of the source file and the line nearest to the | |
1794 | wanted location. */ | |
1795 | ||
330595d0 | 1796 | /*ARGSUSED*/ |
075caafd | 1797 | boolean |
25057836 JL |
1798 | coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr, |
1799 | functionname_ptr, line_ptr) | |
6dc6a81a ILT |
1800 | bfd *abfd; |
1801 | asection *section; | |
1802 | asymbol **ignore_symbols; | |
1803 | bfd_vma offset; | |
1804 | CONST char **filename_ptr; | |
1805 | CONST char **functionname_ptr; | |
1806 | unsigned int *line_ptr; | |
075caafd | 1807 | { |
a74d1517 | 1808 | unsigned int i; |
25b5a53d | 1809 | unsigned int line_base; |
6dc6a81a | 1810 | coff_data_type *cof = coff_data (abfd); |
075caafd ILT |
1811 | /* Run through the raw syments if available */ |
1812 | combined_entry_type *p; | |
a74d1517 | 1813 | combined_entry_type *pend; |
6dc6a81a | 1814 | alent *l; |
25b5a53d | 1815 | struct coff_section_tdata *sec_data; |
075caafd ILT |
1816 | |
1817 | *filename_ptr = 0; | |
1818 | *functionname_ptr = 0; | |
1819 | *line_ptr = 0; | |
1820 | ||
1821 | /* Don't try and find line numbers in a non coff file */ | |
1822 | if (abfd->xvec->flavour != bfd_target_coff_flavour) | |
1823 | return false; | |
1824 | ||
1825 | if (cof == NULL) | |
1826 | return false; | |
1827 | ||
a74d1517 | 1828 | /* Find the first C_FILE symbol. */ |
075caafd | 1829 | p = cof->raw_syments; |
a74d1517 ILT |
1830 | pend = p + cof->raw_syment_count; |
1831 | while (p < pend) | |
1832 | { | |
1833 | if (p->u.syment.n_sclass == C_FILE) | |
1834 | break; | |
1835 | p += 1 + p->u.syment.n_numaux; | |
1836 | } | |
075caafd | 1837 | |
a74d1517 ILT |
1838 | if (p < pend) |
1839 | { | |
6d04c6d4 ILT |
1840 | bfd_vma maxdiff; |
1841 | ||
a74d1517 | 1842 | /* Look through the C_FILE symbols to find the best one. */ |
075caafd | 1843 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; |
6d04c6d4 | 1844 | maxdiff = (bfd_vma) 0 - (bfd_vma) 1; |
a74d1517 ILT |
1845 | while (1) |
1846 | { | |
1847 | combined_entry_type *p2; | |
1848 | ||
6d04c6d4 ILT |
1849 | for (p2 = p + 1 + p->u.syment.n_numaux; |
1850 | p2 < pend; | |
1851 | p2 += 1 + p2->u.syment.n_numaux) | |
a74d1517 | 1852 | { |
6d04c6d4 ILT |
1853 | if (p2->u.syment.n_scnum > 0 |
1854 | && (section | |
1855 | == coff_section_from_bfd_index (abfd, | |
1856 | p2->u.syment.n_scnum))) | |
a74d1517 | 1857 | break; |
6d04c6d4 ILT |
1858 | if (p2->u.syment.n_sclass == C_FILE) |
1859 | { | |
1860 | p2 = pend; | |
1861 | break; | |
1862 | } | |
a74d1517 | 1863 | } |
6d04c6d4 | 1864 | |
a74d1517 | 1865 | if (p2 < pend |
ae115e51 ILT |
1866 | && offset >= (bfd_vma) p2->u.syment.n_value |
1867 | && offset - (bfd_vma) p2->u.syment.n_value < maxdiff) | |
6d04c6d4 ILT |
1868 | { |
1869 | *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; | |
1870 | maxdiff = offset - p2->u.syment.n_value; | |
1871 | } | |
1872 | ||
1873 | /* Avoid endless loops on erroneous files by ensuring that | |
1874 | we always move forward in the file. */ | |
1875 | if (p - cof->raw_syments >= p->u.syment.n_value) | |
a74d1517 ILT |
1876 | break; |
1877 | ||
6d04c6d4 ILT |
1878 | p = cof->raw_syments + p->u.syment.n_value; |
1879 | if (p > pend || p->u.syment.n_sclass != C_FILE) | |
1880 | break; | |
a74d1517 | 1881 | } |
075caafd | 1882 | } |
a74d1517 | 1883 | |
075caafd | 1884 | /* Now wander though the raw linenumbers of the section */ |
25b5a53d ILT |
1885 | /* If we have been called on this section before, and the offset we |
1886 | want is further down then we can prime the lookup loop. */ | |
1887 | sec_data = coff_section_data (abfd, section); | |
1888 | if (sec_data != NULL | |
1889 | && sec_data->i > 0 | |
1890 | && offset >= sec_data->offset) | |
1891 | { | |
1892 | i = sec_data->i; | |
1893 | *functionname_ptr = sec_data->function; | |
1894 | line_base = sec_data->line_base; | |
6dc6a81a ILT |
1895 | } |
1896 | else | |
1897 | { | |
1898 | i = 0; | |
25b5a53d | 1899 | line_base = 0; |
6dc6a81a | 1900 | } |
25b5a53d | 1901 | |
85fe7cff | 1902 | l = §ion->lineno[i]; |
075caafd | 1903 | |
6dc6a81a ILT |
1904 | for (; i < section->lineno_count; i++) |
1905 | { | |
1906 | if (l->line_number == 0) | |
1907 | { | |
1908 | /* Get the symbol this line number points at */ | |
1909 | coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); | |
1910 | if (coff->symbol.value > offset) | |
1911 | break; | |
1912 | *functionname_ptr = coff->symbol.name; | |
1913 | if (coff->native) | |
1914 | { | |
1915 | combined_entry_type *s = coff->native; | |
1916 | s = s + 1 + s->u.syment.n_numaux; | |
1917 | ||
1918 | /* In XCOFF a debugging symbol can follow the function | |
1919 | symbol. */ | |
1920 | if (s->u.syment.n_scnum == N_DEBUG) | |
1921 | s = s + 1 + s->u.syment.n_numaux; | |
1922 | ||
1923 | /* | |
1924 | S should now point to the .bf of the function | |
1925 | */ | |
1926 | if (s->u.syment.n_numaux) | |
1927 | { | |
1928 | /* | |
1929 | The linenumber is stored in the auxent | |
1930 | */ | |
1931 | union internal_auxent *a = &((s + 1)->u.auxent); | |
1932 | line_base = a->x_sym.x_misc.x_lnsz.x_lnno; | |
1933 | *line_ptr = line_base; | |
1934 | } | |
1935 | } | |
075caafd | 1936 | } |
6dc6a81a ILT |
1937 | else |
1938 | { | |
1939 | if (l->u.offset + bfd_get_section_vma (abfd, section) > offset) | |
1940 | break; | |
1941 | *line_ptr = l->line_number + line_base - 1; | |
1942 | } | |
1943 | l++; | |
075caafd | 1944 | } |
075caafd | 1945 | |
25b5a53d ILT |
1946 | /* Cache the results for the next call. */ |
1947 | if (sec_data == NULL) | |
1948 | { | |
1949 | section->used_by_bfd = | |
1950 | ((PTR) bfd_zalloc (abfd, | |
1951 | sizeof (struct coff_section_tdata))); | |
1952 | sec_data = section->used_by_bfd; | |
1953 | } | |
1954 | if (sec_data != NULL) | |
1955 | { | |
1956 | sec_data->offset = offset; | |
1957 | sec_data->i = i; | |
1958 | sec_data->function = *functionname_ptr; | |
1959 | sec_data->line_base = line_base; | |
1960 | } | |
075caafd ILT |
1961 | |
1962 | return true; | |
1963 | } | |
1964 | ||
1965 | int | |
25057836 JL |
1966 | coff_sizeof_headers (abfd, reloc) |
1967 | bfd *abfd; | |
1968 | boolean reloc; | |
075caafd | 1969 | { |
6dc6a81a | 1970 | size_t size; |
075caafd | 1971 | |
6dc6a81a ILT |
1972 | if (reloc == false) |
1973 | { | |
1974 | size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd); | |
075caafd | 1975 | } |
6dc6a81a ILT |
1976 | else |
1977 | { | |
1978 | size = bfd_coff_filhsz (abfd); | |
075caafd ILT |
1979 | } |
1980 | ||
6dc6a81a ILT |
1981 | size += abfd->section_count * bfd_coff_scnhsz (abfd); |
1982 | return size; | |
075caafd | 1983 | } |