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