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