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