coffgen.c revision 1.9 1 1.1 christos /* Support for the generic parts of COFF, for BFD.
2 1.9 christos Copyright (C) 1990-2024 Free Software Foundation, Inc.
3 1.1 christos Written by Cygnus Support.
4 1.1 christos
5 1.1 christos This file is part of BFD, the Binary File Descriptor library.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program; if not, write to the Free Software
19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 1.1 christos MA 02110-1301, USA. */
21 1.1 christos
22 1.1 christos /* Most of this hacked by Steve Chamberlain, sac (at) cygnus.com.
23 1.1 christos Split out of coffcode.h by Ian Taylor, ian (at) cygnus.com. */
24 1.1 christos
25 1.1 christos /* This file contains COFF code that is not dependent on any
26 1.1 christos particular COFF target. There is only one version of this file in
27 1.1 christos libbfd.a, so no target specific code may be put in here. Or, to
28 1.1 christos put it another way,
29 1.1 christos
30 1.1 christos ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31 1.1 christos
32 1.1 christos If you need to add some target specific behaviour, add a new hook
33 1.1 christos function to bfd_coff_backend_data.
34 1.1 christos
35 1.1 christos Some of these functions are also called by the ECOFF routines.
36 1.1 christos Those functions may not use any COFF specific information, such as
37 1.1 christos coff_data (abfd). */
38 1.1 christos
39 1.1 christos #include "sysdep.h"
40 1.7 christos #include <limits.h>
41 1.1 christos #include "bfd.h"
42 1.1 christos #include "libbfd.h"
43 1.1 christos #include "coff/internal.h"
44 1.1 christos #include "libcoff.h"
45 1.9 christos #include "hashtab.h"
46 1.9 christos
47 1.9 christos /* Extract a long section name at STRINDEX and copy it to the bfd objstack.
48 1.9 christos Return NULL in case of error. */
49 1.9 christos
50 1.9 christos static char *
51 1.9 christos extract_long_section_name(bfd *abfd, unsigned long strindex)
52 1.9 christos {
53 1.9 christos const char *strings;
54 1.9 christos char *name;
55 1.9 christos
56 1.9 christos strings = _bfd_coff_read_string_table (abfd);
57 1.9 christos if (strings == NULL)
58 1.9 christos return NULL;
59 1.9 christos if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
60 1.9 christos return NULL;
61 1.9 christos strings += strindex;
62 1.9 christos name = (char *) bfd_alloc (abfd, (bfd_size_type) strlen (strings) + 1);
63 1.9 christos if (name == NULL)
64 1.9 christos return NULL;
65 1.9 christos strcpy (name, strings);
66 1.9 christos
67 1.9 christos return name;
68 1.9 christos }
69 1.9 christos
70 1.9 christos /* Decode a base 64 coded string at STR of length LEN, and write the result
71 1.9 christos to RES. Return true on success.
72 1.9 christos Return false in case of invalid character or overflow. */
73 1.9 christos
74 1.9 christos static bool
75 1.9 christos decode_base64 (const char *str, unsigned len, uint32_t *res)
76 1.9 christos {
77 1.9 christos unsigned i;
78 1.9 christos uint32_t val;
79 1.9 christos
80 1.9 christos val = 0;
81 1.9 christos for (i = 0; i < len; i++)
82 1.9 christos {
83 1.9 christos char c = str[i];
84 1.9 christos unsigned d;
85 1.9 christos
86 1.9 christos if (c >= 'A' && c <= 'Z')
87 1.9 christos d = c - 'A';
88 1.9 christos else if (c >= 'a' && c <= 'z')
89 1.9 christos d = c - 'a' + 26;
90 1.9 christos else if (c >= '0' && c <= '9')
91 1.9 christos d = c - '0' + 52;
92 1.9 christos else if (c == '+')
93 1.9 christos d = 62;
94 1.9 christos else if (c == '/')
95 1.9 christos d = 63;
96 1.9 christos else
97 1.9 christos return false;
98 1.9 christos
99 1.9 christos /* Check for overflow. */
100 1.9 christos if ((val >> 26) != 0)
101 1.9 christos return false;
102 1.9 christos
103 1.9 christos val = (val << 6) + d;
104 1.9 christos }
105 1.9 christos
106 1.9 christos *res = val;
107 1.9 christos return true;
108 1.9 christos }
109 1.1 christos
110 1.1 christos /* Take a section header read from a coff file (in HOST byte order),
111 1.1 christos and make a BFD "section" out of it. This is used by ECOFF. */
112 1.1 christos
113 1.8 christos static bool
114 1.1 christos make_a_section_from_file (bfd *abfd,
115 1.1 christos struct internal_scnhdr *hdr,
116 1.1 christos unsigned int target_index)
117 1.1 christos {
118 1.9 christos asection *newsect;
119 1.1 christos char *name;
120 1.8 christos bool result = true;
121 1.1 christos flagword flags;
122 1.1 christos
123 1.1 christos name = NULL;
124 1.1 christos
125 1.1 christos /* Handle long section names as in PE. On reading, we want to
126 1.1 christos accept long names if the format permits them at all, regardless
127 1.1 christos of the current state of the flag that dictates if we would generate
128 1.1 christos them in outputs; this construct checks if that is the case by
129 1.1 christos attempting to set the flag, without changing its state; the call
130 1.1 christos will fail for formats that do not support long names at all. */
131 1.1 christos if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
132 1.1 christos && hdr->s_name[0] == '/')
133 1.1 christos {
134 1.1 christos /* Flag that this BFD uses long names, even though the format might
135 1.6 christos expect them to be off by default. This won't directly affect the
136 1.6 christos format of any output BFD created from this one, but the information
137 1.6 christos can be used to decide what to do. */
138 1.8 christos bfd_coff_set_long_section_names (abfd, true);
139 1.9 christos
140 1.9 christos if (hdr->s_name[1] == '/')
141 1.1 christos {
142 1.9 christos /* LLVM extension: the '/' is followed by another '/' and then by
143 1.9 christos the index in the strtab encoded in base64 without NUL at the
144 1.9 christos end. */
145 1.9 christos uint32_t strindex;
146 1.9 christos
147 1.9 christos /* Decode the index. No overflow is expected as the string table
148 1.9 christos length is at most 2^32 - 1 (the length is written on the first
149 1.9 christos four bytes).
150 1.9 christos Also, contrary to RFC 4648, all the characters must be decoded,
151 1.9 christos there is no padding. */
152 1.9 christos if (!decode_base64 (hdr->s_name + 2, SCNNMLEN - 2, &strindex))
153 1.8 christos return false;
154 1.9 christos
155 1.9 christos name = extract_long_section_name (abfd, strindex);
156 1.1 christos if (name == NULL)
157 1.8 christos return false;
158 1.9 christos }
159 1.9 christos else
160 1.9 christos {
161 1.9 christos /* PE classic long section name. The '/' is followed by the index
162 1.9 christos in the strtab. The index is formatted as a decimal string. */
163 1.9 christos char buf[SCNNMLEN];
164 1.9 christos long strindex;
165 1.9 christos char *p;
166 1.9 christos
167 1.9 christos memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
168 1.9 christos buf[SCNNMLEN - 1] = '\0';
169 1.9 christos strindex = strtol (buf, &p, 10);
170 1.9 christos if (*p == '\0' && strindex >= 0)
171 1.9 christos {
172 1.9 christos name = extract_long_section_name (abfd, strindex);
173 1.9 christos if (name == NULL)
174 1.9 christos return false;
175 1.9 christos }
176 1.1 christos }
177 1.1 christos }
178 1.1 christos
179 1.1 christos if (name == NULL)
180 1.1 christos {
181 1.1 christos /* Assorted wastage to null-terminate the name, thanks AT&T! */
182 1.1 christos name = (char *) bfd_alloc (abfd,
183 1.6 christos (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
184 1.1 christos if (name == NULL)
185 1.8 christos return false;
186 1.1 christos strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
187 1.1 christos name[sizeof (hdr->s_name)] = 0;
188 1.1 christos }
189 1.1 christos
190 1.9 christos newsect = bfd_make_section_anyway (abfd, name);
191 1.9 christos if (newsect == NULL)
192 1.8 christos return false;
193 1.1 christos
194 1.9 christos newsect->vma = hdr->s_vaddr;
195 1.9 christos newsect->lma = hdr->s_paddr;
196 1.9 christos newsect->size = hdr->s_size;
197 1.9 christos newsect->filepos = hdr->s_scnptr;
198 1.9 christos newsect->rel_filepos = hdr->s_relptr;
199 1.9 christos newsect->reloc_count = hdr->s_nreloc;
200 1.9 christos
201 1.9 christos bfd_coff_set_alignment_hook (abfd, newsect, hdr);
202 1.9 christos
203 1.9 christos newsect->line_filepos = hdr->s_lnnoptr;
204 1.9 christos
205 1.9 christos newsect->lineno_count = hdr->s_nlnno;
206 1.9 christos newsect->userdata = NULL;
207 1.9 christos newsect->next = NULL;
208 1.9 christos newsect->target_index = target_index;
209 1.1 christos
210 1.9 christos if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags))
211 1.8 christos result = false;
212 1.1 christos
213 1.1 christos /* At least on i386-coff, the line number count for a shared library
214 1.1 christos section must be ignored. */
215 1.9 christos if ((flags & SEC_COFF_SHARED_LIBRARY) != 0)
216 1.9 christos newsect->lineno_count = 0;
217 1.1 christos
218 1.1 christos if (hdr->s_nreloc != 0)
219 1.9 christos flags |= SEC_RELOC;
220 1.1 christos /* FIXME: should this check 'hdr->s_size > 0'. */
221 1.1 christos if (hdr->s_scnptr != 0)
222 1.9 christos flags |= SEC_HAS_CONTENTS;
223 1.1 christos
224 1.9 christos newsect->flags = flags;
225 1.9 christos
226 1.9 christos /* Compress/decompress DWARF debug sections. */
227 1.9 christos if ((flags & SEC_DEBUGGING) != 0
228 1.9 christos && (flags & SEC_HAS_CONTENTS) != 0
229 1.9 christos && (startswith (name, ".debug_")
230 1.9 christos || startswith (name, ".zdebug_")
231 1.9 christos || startswith (name, ".gnu.debuglto_.debug_")
232 1.9 christos || startswith (name, ".gnu.linkonce.wi.")))
233 1.3 christos {
234 1.3 christos enum { nothing, compress, decompress } action = nothing;
235 1.3 christos
236 1.9 christos if (bfd_is_section_compressed (abfd, newsect))
237 1.3 christos {
238 1.3 christos /* Compressed section. Check if we should decompress. */
239 1.3 christos if ((abfd->flags & BFD_DECOMPRESS))
240 1.3 christos action = decompress;
241 1.3 christos }
242 1.9 christos else
243 1.3 christos {
244 1.3 christos /* Normal section. Check if we should compress. */
245 1.9 christos if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
246 1.3 christos action = compress;
247 1.3 christos }
248 1.3 christos
249 1.9 christos if (action == compress)
250 1.3 christos {
251 1.9 christos if (!bfd_init_section_compress_status (abfd, newsect))
252 1.3 christos {
253 1.6 christos _bfd_error_handler
254 1.9 christos /* xgettext:c-format */
255 1.9 christos (_("%pB: unable to compress section %s"), abfd, name);
256 1.8 christos return false;
257 1.3 christos }
258 1.9 christos }
259 1.9 christos else if (action == decompress)
260 1.9 christos {
261 1.9 christos if (!bfd_init_section_decompress_status (abfd, newsect))
262 1.3 christos {
263 1.6 christos _bfd_error_handler
264 1.9 christos /* xgettext:c-format */
265 1.9 christos (_("%pB: unable to decompress section %s"), abfd, name);
266 1.8 christos return false;
267 1.3 christos }
268 1.9 christos if (abfd->is_linker_input
269 1.9 christos && name[1] == 'z')
270 1.3 christos {
271 1.9 christos /* Rename section from .zdebug_* to .debug_* so that ld
272 1.9 christos scripts will see this section as a debug section. */
273 1.9 christos char *new_name = bfd_zdebug_name_to_debug (abfd, name);
274 1.3 christos if (new_name == NULL)
275 1.8 christos return false;
276 1.9 christos bfd_rename_section (newsect, new_name);
277 1.3 christos }
278 1.3 christos }
279 1.3 christos }
280 1.3 christos
281 1.1 christos return result;
282 1.1 christos }
283 1.1 christos
284 1.9 christos void
285 1.9 christos coff_object_cleanup (bfd *abfd)
286 1.9 christos {
287 1.9 christos struct coff_tdata *td = coff_data (abfd);
288 1.9 christos if (td != NULL)
289 1.9 christos {
290 1.9 christos if (td->section_by_index)
291 1.9 christos htab_delete (td->section_by_index);
292 1.9 christos if (td->section_by_target_index)
293 1.9 christos htab_delete (td->section_by_target_index);
294 1.9 christos if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
295 1.9 christos htab_delete (pe_data (abfd)->comdat_hash);
296 1.9 christos }
297 1.9 christos }
298 1.9 christos
299 1.1 christos /* Read in a COFF object and make it into a BFD. This is used by
300 1.1 christos ECOFF as well. */
301 1.8 christos bfd_cleanup
302 1.1 christos coff_real_object_p (bfd *abfd,
303 1.1 christos unsigned nscns,
304 1.1 christos struct internal_filehdr *internal_f,
305 1.1 christos struct internal_aouthdr *internal_a)
306 1.1 christos {
307 1.1 christos flagword oflags = abfd->flags;
308 1.1 christos bfd_vma ostart = bfd_get_start_address (abfd);
309 1.1 christos void * tdata;
310 1.1 christos void * tdata_save;
311 1.1 christos bfd_size_type readsize; /* Length of file_info. */
312 1.1 christos unsigned int scnhsz;
313 1.1 christos char *external_sections;
314 1.1 christos
315 1.1 christos if (!(internal_f->f_flags & F_RELFLG))
316 1.1 christos abfd->flags |= HAS_RELOC;
317 1.1 christos if ((internal_f->f_flags & F_EXEC))
318 1.1 christos abfd->flags |= EXEC_P;
319 1.1 christos if (!(internal_f->f_flags & F_LNNO))
320 1.1 christos abfd->flags |= HAS_LINENO;
321 1.1 christos if (!(internal_f->f_flags & F_LSYMS))
322 1.1 christos abfd->flags |= HAS_LOCALS;
323 1.1 christos
324 1.1 christos /* FIXME: How can we set D_PAGED correctly? */
325 1.1 christos if ((internal_f->f_flags & F_EXEC) != 0)
326 1.1 christos abfd->flags |= D_PAGED;
327 1.1 christos
328 1.7 christos abfd->symcount = internal_f->f_nsyms;
329 1.1 christos if (internal_f->f_nsyms)
330 1.1 christos abfd->flags |= HAS_SYMS;
331 1.1 christos
332 1.1 christos if (internal_a != (struct internal_aouthdr *) NULL)
333 1.7 christos abfd->start_address = internal_a->entry;
334 1.1 christos else
335 1.7 christos abfd->start_address = 0;
336 1.1 christos
337 1.1 christos /* Set up the tdata area. ECOFF uses its own routine, and overrides
338 1.1 christos abfd->flags. */
339 1.1 christos tdata_save = abfd->tdata.any;
340 1.1 christos tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
341 1.1 christos if (tdata == NULL)
342 1.1 christos goto fail2;
343 1.1 christos
344 1.1 christos scnhsz = bfd_coff_scnhsz (abfd);
345 1.1 christos readsize = (bfd_size_type) nscns * scnhsz;
346 1.8 christos external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize);
347 1.1 christos if (!external_sections)
348 1.1 christos goto fail;
349 1.1 christos
350 1.1 christos /* Set the arch/mach *before* swapping in sections; section header swapping
351 1.1 christos may depend on arch/mach info. */
352 1.1 christos if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
353 1.1 christos goto fail;
354 1.1 christos
355 1.1 christos /* Now copy data as required; construct all asections etc. */
356 1.1 christos if (nscns != 0)
357 1.1 christos {
358 1.1 christos unsigned int i;
359 1.1 christos for (i = 0; i < nscns; i++)
360 1.1 christos {
361 1.1 christos struct internal_scnhdr tmp;
362 1.1 christos bfd_coff_swap_scnhdr_in (abfd,
363 1.1 christos (void *) (external_sections + i * scnhsz),
364 1.1 christos (void *) & tmp);
365 1.1 christos if (! make_a_section_from_file (abfd, &tmp, i + 1))
366 1.1 christos goto fail;
367 1.1 christos }
368 1.1 christos }
369 1.1 christos
370 1.7 christos _bfd_coff_free_symbols (abfd);
371 1.9 christos return coff_object_cleanup;
372 1.1 christos
373 1.1 christos fail:
374 1.9 christos coff_object_cleanup (abfd);
375 1.7 christos _bfd_coff_free_symbols (abfd);
376 1.1 christos bfd_release (abfd, tdata);
377 1.1 christos fail2:
378 1.1 christos abfd->tdata.any = tdata_save;
379 1.1 christos abfd->flags = oflags;
380 1.7 christos abfd->start_address = ostart;
381 1.8 christos return NULL;
382 1.1 christos }
383 1.1 christos
384 1.1 christos /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
385 1.1 christos not a COFF file. This is also used by ECOFF. */
386 1.1 christos
387 1.8 christos bfd_cleanup
388 1.1 christos coff_object_p (bfd *abfd)
389 1.1 christos {
390 1.1 christos bfd_size_type filhsz;
391 1.1 christos bfd_size_type aoutsz;
392 1.1 christos unsigned int nscns;
393 1.1 christos void * filehdr;
394 1.1 christos struct internal_filehdr internal_f;
395 1.1 christos struct internal_aouthdr internal_a;
396 1.1 christos
397 1.1 christos /* Figure out how much to read. */
398 1.1 christos filhsz = bfd_coff_filhsz (abfd);
399 1.1 christos aoutsz = bfd_coff_aoutsz (abfd);
400 1.1 christos
401 1.8 christos filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz);
402 1.1 christos if (filehdr == NULL)
403 1.1 christos {
404 1.1 christos if (bfd_get_error () != bfd_error_system_call)
405 1.1 christos bfd_set_error (bfd_error_wrong_format);
406 1.1 christos return NULL;
407 1.1 christos }
408 1.1 christos bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
409 1.1 christos bfd_release (abfd, filehdr);
410 1.1 christos
411 1.1 christos /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
412 1.1 christos (less than aoutsz) used in object files and AOUTSZ (equal to
413 1.1 christos aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
414 1.1 christos expects this header to be aoutsz bytes in length, so we use that
415 1.1 christos value in the call to bfd_alloc below. But we must be careful to
416 1.9 christos only read in f_opthdr bytes in the call to bfd_read. We should
417 1.1 christos also attempt to catch corrupt or non-COFF binaries with a strange
418 1.1 christos value for f_opthdr. */
419 1.1 christos if (! bfd_coff_bad_format_hook (abfd, &internal_f)
420 1.1 christos || internal_f.f_opthdr > aoutsz)
421 1.1 christos {
422 1.1 christos bfd_set_error (bfd_error_wrong_format);
423 1.1 christos return NULL;
424 1.1 christos }
425 1.1 christos nscns = internal_f.f_nscns;
426 1.1 christos
427 1.1 christos if (internal_f.f_opthdr)
428 1.1 christos {
429 1.1 christos void * opthdr;
430 1.1 christos
431 1.8 christos opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr);
432 1.1 christos if (opthdr == NULL)
433 1.1 christos return NULL;
434 1.3 christos /* PR 17512: file: 11056-1136-0.004. */
435 1.3 christos if (internal_f.f_opthdr < aoutsz)
436 1.8 christos memset (((char *) opthdr) + internal_f.f_opthdr, 0,
437 1.8 christos aoutsz - internal_f.f_opthdr);
438 1.3 christos
439 1.1 christos bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
440 1.1 christos bfd_release (abfd, opthdr);
441 1.1 christos }
442 1.1 christos
443 1.1 christos return coff_real_object_p (abfd, nscns, &internal_f,
444 1.1 christos (internal_f.f_opthdr != 0
445 1.1 christos ? &internal_a
446 1.1 christos : (struct internal_aouthdr *) NULL));
447 1.1 christos }
448 1.1 christos
449 1.9 christos static hashval_t
450 1.9 christos htab_hash_section_target_index (const void * entry)
451 1.9 christos {
452 1.9 christos const struct bfd_section * sec = entry;
453 1.9 christos return sec->target_index;
454 1.9 christos }
455 1.9 christos
456 1.9 christos static int
457 1.9 christos htab_eq_section_target_index (const void * e1, const void * e2)
458 1.9 christos {
459 1.9 christos const struct bfd_section * sec1 = e1;
460 1.9 christos const struct bfd_section * sec2 = e2;
461 1.9 christos return sec1->target_index == sec2->target_index;
462 1.9 christos }
463 1.9 christos
464 1.1 christos /* Get the BFD section from a COFF symbol section number. */
465 1.1 christos
466 1.1 christos asection *
467 1.1 christos coff_section_from_bfd_index (bfd *abfd, int section_index)
468 1.1 christos {
469 1.1 christos if (section_index == N_ABS)
470 1.1 christos return bfd_abs_section_ptr;
471 1.1 christos if (section_index == N_UNDEF)
472 1.1 christos return bfd_und_section_ptr;
473 1.1 christos if (section_index == N_DEBUG)
474 1.1 christos return bfd_abs_section_ptr;
475 1.1 christos
476 1.9 christos struct bfd_section *answer;
477 1.9 christos htab_t table = coff_data (abfd)->section_by_target_index;
478 1.9 christos
479 1.9 christos if (!table)
480 1.1 christos {
481 1.9 christos table = htab_create (10, htab_hash_section_target_index,
482 1.9 christos htab_eq_section_target_index, NULL);
483 1.9 christos if (table == NULL)
484 1.9 christos return bfd_und_section_ptr;
485 1.9 christos coff_data (abfd)->section_by_target_index = table;
486 1.9 christos }
487 1.9 christos
488 1.9 christos if (htab_elements (table) == 0)
489 1.9 christos {
490 1.9 christos for (answer = abfd->sections; answer; answer = answer->next)
491 1.9 christos {
492 1.9 christos void **slot = htab_find_slot (table, answer, INSERT);
493 1.9 christos if (slot == NULL)
494 1.9 christos return bfd_und_section_ptr;
495 1.9 christos *slot = answer;
496 1.9 christos }
497 1.9 christos }
498 1.9 christos
499 1.9 christos struct bfd_section needle;
500 1.9 christos needle.target_index = section_index;
501 1.9 christos
502 1.9 christos answer = htab_find (table, &needle);
503 1.9 christos if (answer != NULL)
504 1.9 christos return answer;
505 1.9 christos
506 1.9 christos /* Cover the unlikely case of sections added after the first call to
507 1.9 christos this function. */
508 1.9 christos for (answer = abfd->sections; answer; answer = answer->next)
509 1.9 christos if (answer->target_index == section_index)
510 1.9 christos {
511 1.9 christos void **slot = htab_find_slot (table, answer, INSERT);
512 1.9 christos if (slot != NULL)
513 1.9 christos *slot = answer;
514 1.1 christos return answer;
515 1.9 christos }
516 1.1 christos
517 1.1 christos /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
518 1.1 christos has a bad symbol table in biglitpow.o. */
519 1.1 christos return bfd_und_section_ptr;
520 1.1 christos }
521 1.1 christos
522 1.1 christos /* Get the upper bound of a COFF symbol table. */
523 1.1 christos
524 1.1 christos long
525 1.1 christos coff_get_symtab_upper_bound (bfd *abfd)
526 1.1 christos {
527 1.1 christos if (!bfd_coff_slurp_symbol_table (abfd))
528 1.1 christos return -1;
529 1.1 christos
530 1.1 christos return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
531 1.1 christos }
532 1.1 christos
533 1.1 christos /* Canonicalize a COFF symbol table. */
534 1.1 christos
535 1.1 christos long
536 1.1 christos coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
537 1.1 christos {
538 1.1 christos unsigned int counter;
539 1.1 christos coff_symbol_type *symbase;
540 1.1 christos coff_symbol_type **location = (coff_symbol_type **) alocation;
541 1.1 christos
542 1.1 christos if (!bfd_coff_slurp_symbol_table (abfd))
543 1.1 christos return -1;
544 1.1 christos
545 1.1 christos symbase = obj_symbols (abfd);
546 1.1 christos counter = bfd_get_symcount (abfd);
547 1.1 christos while (counter-- > 0)
548 1.1 christos *location++ = symbase++;
549 1.1 christos
550 1.1 christos *location = NULL;
551 1.1 christos
552 1.1 christos return bfd_get_symcount (abfd);
553 1.1 christos }
554 1.1 christos
555 1.1 christos /* Get the name of a symbol. The caller must pass in a buffer of size
556 1.1 christos >= SYMNMLEN + 1. */
557 1.1 christos
558 1.1 christos const char *
559 1.1 christos _bfd_coff_internal_syment_name (bfd *abfd,
560 1.1 christos const struct internal_syment *sym,
561 1.1 christos char *buf)
562 1.1 christos {
563 1.1 christos /* FIXME: It's not clear this will work correctly if sizeof
564 1.1 christos (_n_zeroes) != 4. */
565 1.1 christos if (sym->_n._n_n._n_zeroes != 0
566 1.1 christos || sym->_n._n_n._n_offset == 0)
567 1.1 christos {
568 1.1 christos memcpy (buf, sym->_n._n_name, SYMNMLEN);
569 1.1 christos buf[SYMNMLEN] = '\0';
570 1.1 christos return buf;
571 1.1 christos }
572 1.1 christos else
573 1.1 christos {
574 1.1 christos const char *strings;
575 1.1 christos
576 1.1 christos BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
577 1.1 christos strings = obj_coff_strings (abfd);
578 1.1 christos if (strings == NULL)
579 1.1 christos {
580 1.1 christos strings = _bfd_coff_read_string_table (abfd);
581 1.1 christos if (strings == NULL)
582 1.1 christos return NULL;
583 1.1 christos }
584 1.9 christos if (sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
585 1.3 christos return NULL;
586 1.1 christos return strings + sym->_n._n_n._n_offset;
587 1.1 christos }
588 1.1 christos }
589 1.1 christos
590 1.1 christos /* Read in and swap the relocs. This returns a buffer holding the
591 1.1 christos relocs for section SEC in file ABFD. If CACHE is TRUE and
592 1.1 christos INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
593 1.1 christos the function is called again. If EXTERNAL_RELOCS is not NULL, it
594 1.1 christos is a buffer large enough to hold the unswapped relocs. If
595 1.1 christos INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
596 1.1 christos the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
597 1.1 christos value must be INTERNAL_RELOCS. The function returns NULL on error. */
598 1.1 christos
599 1.1 christos struct internal_reloc *
600 1.1 christos _bfd_coff_read_internal_relocs (bfd *abfd,
601 1.1 christos asection *sec,
602 1.8 christos bool cache,
603 1.1 christos bfd_byte *external_relocs,
604 1.8 christos bool require_internal,
605 1.1 christos struct internal_reloc *internal_relocs)
606 1.1 christos {
607 1.1 christos bfd_size_type relsz;
608 1.1 christos bfd_byte *free_external = NULL;
609 1.1 christos struct internal_reloc *free_internal = NULL;
610 1.1 christos bfd_byte *erel;
611 1.1 christos bfd_byte *erel_end;
612 1.1 christos struct internal_reloc *irel;
613 1.1 christos bfd_size_type amt;
614 1.1 christos
615 1.1 christos if (sec->reloc_count == 0)
616 1.1 christos return internal_relocs; /* Nothing to do. */
617 1.1 christos
618 1.1 christos if (coff_section_data (abfd, sec) != NULL
619 1.1 christos && coff_section_data (abfd, sec)->relocs != NULL)
620 1.1 christos {
621 1.1 christos if (! require_internal)
622 1.1 christos return coff_section_data (abfd, sec)->relocs;
623 1.1 christos memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
624 1.1 christos sec->reloc_count * sizeof (struct internal_reloc));
625 1.1 christos return internal_relocs;
626 1.1 christos }
627 1.1 christos
628 1.1 christos relsz = bfd_coff_relsz (abfd);
629 1.1 christos
630 1.1 christos amt = sec->reloc_count * relsz;
631 1.1 christos if (external_relocs == NULL)
632 1.1 christos {
633 1.1 christos free_external = (bfd_byte *) bfd_malloc (amt);
634 1.1 christos if (free_external == NULL)
635 1.1 christos goto error_return;
636 1.1 christos external_relocs = free_external;
637 1.1 christos }
638 1.1 christos
639 1.1 christos if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
640 1.9 christos || bfd_read (external_relocs, amt, abfd) != amt)
641 1.1 christos goto error_return;
642 1.1 christos
643 1.1 christos if (internal_relocs == NULL)
644 1.1 christos {
645 1.1 christos amt = sec->reloc_count;
646 1.1 christos amt *= sizeof (struct internal_reloc);
647 1.1 christos free_internal = (struct internal_reloc *) bfd_malloc (amt);
648 1.1 christos if (free_internal == NULL)
649 1.1 christos goto error_return;
650 1.1 christos internal_relocs = free_internal;
651 1.1 christos }
652 1.1 christos
653 1.1 christos /* Swap in the relocs. */
654 1.1 christos erel = external_relocs;
655 1.1 christos erel_end = erel + relsz * sec->reloc_count;
656 1.1 christos irel = internal_relocs;
657 1.1 christos for (; erel < erel_end; erel += relsz, irel++)
658 1.1 christos bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
659 1.1 christos
660 1.8 christos free (free_external);
661 1.8 christos free_external = NULL;
662 1.1 christos
663 1.1 christos if (cache && free_internal != NULL)
664 1.1 christos {
665 1.1 christos if (coff_section_data (abfd, sec) == NULL)
666 1.1 christos {
667 1.1 christos amt = sizeof (struct coff_section_tdata);
668 1.1 christos sec->used_by_bfd = bfd_zalloc (abfd, amt);
669 1.1 christos if (sec->used_by_bfd == NULL)
670 1.1 christos goto error_return;
671 1.1 christos coff_section_data (abfd, sec)->contents = NULL;
672 1.1 christos }
673 1.1 christos coff_section_data (abfd, sec)->relocs = free_internal;
674 1.1 christos }
675 1.1 christos
676 1.1 christos return internal_relocs;
677 1.1 christos
678 1.1 christos error_return:
679 1.8 christos free (free_external);
680 1.8 christos free (free_internal);
681 1.1 christos return NULL;
682 1.1 christos }
683 1.1 christos
684 1.1 christos /* Set lineno_count for the output sections of a COFF file. */
685 1.1 christos
686 1.1 christos int
687 1.1 christos coff_count_linenumbers (bfd *abfd)
688 1.1 christos {
689 1.1 christos unsigned int limit = bfd_get_symcount (abfd);
690 1.1 christos unsigned int i;
691 1.1 christos int total = 0;
692 1.1 christos asymbol **p;
693 1.1 christos asection *s;
694 1.1 christos
695 1.1 christos if (limit == 0)
696 1.1 christos {
697 1.1 christos /* This may be from the backend linker, in which case the
698 1.6 christos lineno_count in the sections is correct. */
699 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
700 1.1 christos total += s->lineno_count;
701 1.1 christos return total;
702 1.1 christos }
703 1.1 christos
704 1.1 christos for (s = abfd->sections; s != NULL; s = s->next)
705 1.1 christos BFD_ASSERT (s->lineno_count == 0);
706 1.1 christos
707 1.1 christos for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
708 1.1 christos {
709 1.1 christos asymbol *q_maybe = *p;
710 1.1 christos
711 1.8 christos if (bfd_asymbol_bfd (q_maybe) != NULL
712 1.8 christos && bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
713 1.1 christos {
714 1.1 christos coff_symbol_type *q = coffsymbol (q_maybe);
715 1.1 christos
716 1.1 christos /* The AIX 4.1 compiler can sometimes generate line numbers
717 1.6 christos attached to debugging symbols. We try to simply ignore
718 1.6 christos those here. */
719 1.1 christos if (q->lineno != NULL
720 1.1 christos && q->symbol.section->owner != NULL)
721 1.1 christos {
722 1.1 christos /* This symbol has line numbers. Increment the owning
723 1.6 christos section's linenumber count. */
724 1.1 christos alent *l = q->lineno;
725 1.1 christos
726 1.1 christos do
727 1.1 christos {
728 1.1 christos asection * sec = q->symbol.section->output_section;
729 1.1 christos
730 1.1 christos /* Do not try to update fields in read-only sections. */
731 1.1 christos if (! bfd_is_const_section (sec))
732 1.1 christos sec->lineno_count ++;
733 1.1 christos
734 1.1 christos ++total;
735 1.1 christos ++l;
736 1.1 christos }
737 1.1 christos while (l->line_number != 0);
738 1.1 christos }
739 1.1 christos }
740 1.1 christos }
741 1.1 christos
742 1.1 christos return total;
743 1.1 christos }
744 1.1 christos
745 1.1 christos static void
746 1.1 christos fixup_symbol_value (bfd *abfd,
747 1.1 christos coff_symbol_type *coff_symbol_ptr,
748 1.1 christos struct internal_syment *syment)
749 1.1 christos {
750 1.1 christos /* Normalize the symbol flags. */
751 1.3 christos if (coff_symbol_ptr->symbol.section
752 1.1 christos && bfd_is_com_section (coff_symbol_ptr->symbol.section))
753 1.1 christos {
754 1.1 christos /* A common symbol is undefined with a value. */
755 1.1 christos syment->n_scnum = N_UNDEF;
756 1.1 christos syment->n_value = coff_symbol_ptr->symbol.value;
757 1.1 christos }
758 1.1 christos else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
759 1.1 christos && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
760 1.1 christos {
761 1.1 christos syment->n_value = coff_symbol_ptr->symbol.value;
762 1.1 christos }
763 1.1 christos else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
764 1.1 christos {
765 1.1 christos syment->n_scnum = N_UNDEF;
766 1.1 christos syment->n_value = 0;
767 1.1 christos }
768 1.1 christos /* FIXME: Do we need to handle the absolute section here? */
769 1.1 christos else
770 1.1 christos {
771 1.1 christos if (coff_symbol_ptr->symbol.section)
772 1.1 christos {
773 1.1 christos syment->n_scnum =
774 1.1 christos coff_symbol_ptr->symbol.section->output_section->target_index;
775 1.1 christos
776 1.1 christos syment->n_value = (coff_symbol_ptr->symbol.value
777 1.1 christos + coff_symbol_ptr->symbol.section->output_offset);
778 1.1 christos if (! obj_pe (abfd))
779 1.6 christos {
780 1.6 christos syment->n_value += (syment->n_sclass == C_STATLAB)
781 1.6 christos ? coff_symbol_ptr->symbol.section->output_section->lma
782 1.6 christos : coff_symbol_ptr->symbol.section->output_section->vma;
783 1.6 christos }
784 1.1 christos }
785 1.1 christos else
786 1.1 christos {
787 1.1 christos BFD_ASSERT (0);
788 1.1 christos /* This can happen, but I don't know why yet (steve (at) cygnus.com) */
789 1.1 christos syment->n_scnum = N_ABS;
790 1.1 christos syment->n_value = coff_symbol_ptr->symbol.value;
791 1.1 christos }
792 1.1 christos }
793 1.1 christos }
794 1.1 christos
795 1.1 christos /* Run through all the symbols in the symbol table and work out what
796 1.1 christos their indexes into the symbol table will be when output.
797 1.1 christos
798 1.1 christos Coff requires that each C_FILE symbol points to the next one in the
799 1.1 christos chain, and that the last one points to the first external symbol. We
800 1.1 christos do that here too. */
801 1.1 christos
802 1.8 christos bool
803 1.1 christos coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
804 1.1 christos {
805 1.1 christos unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
806 1.1 christos asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
807 1.1 christos unsigned int native_index = 0;
808 1.1 christos struct internal_syment *last_file = NULL;
809 1.1 christos unsigned int symbol_index;
810 1.1 christos
811 1.1 christos /* COFF demands that undefined symbols come after all other symbols.
812 1.1 christos Since we don't need to impose this extra knowledge on all our
813 1.1 christos client programs, deal with that here. Sort the symbol table;
814 1.1 christos just move the undefined symbols to the end, leaving the rest
815 1.1 christos alone. The O'Reilly book says that defined global symbols come
816 1.1 christos at the end before the undefined symbols, so we do that here as
817 1.1 christos well. */
818 1.1 christos /* @@ Do we have some condition we could test for, so we don't always
819 1.1 christos have to do this? I don't think relocatability is quite right, but
820 1.1 christos I'm not certain. [raeburn:19920508.1711EST] */
821 1.1 christos {
822 1.1 christos asymbol **newsyms;
823 1.1 christos unsigned int i;
824 1.1 christos bfd_size_type amt;
825 1.1 christos
826 1.1 christos amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
827 1.1 christos newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
828 1.1 christos if (!newsyms)
829 1.8 christos return false;
830 1.1 christos bfd_ptr->outsymbols = newsyms;
831 1.1 christos for (i = 0; i < symbol_count; i++)
832 1.1 christos if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
833 1.1 christos || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
834 1.1 christos && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
835 1.1 christos && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
836 1.1 christos || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
837 1.1 christos == 0))))
838 1.1 christos *newsyms++ = symbol_ptr_ptr[i];
839 1.1 christos
840 1.1 christos for (i = 0; i < symbol_count; i++)
841 1.1 christos if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
842 1.1 christos && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
843 1.1 christos && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
844 1.1 christos || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
845 1.1 christos && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
846 1.1 christos != 0))))
847 1.1 christos *newsyms++ = symbol_ptr_ptr[i];
848 1.1 christos
849 1.1 christos *first_undef = newsyms - bfd_ptr->outsymbols;
850 1.1 christos
851 1.1 christos for (i = 0; i < symbol_count; i++)
852 1.1 christos if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
853 1.1 christos && bfd_is_und_section (symbol_ptr_ptr[i]->section))
854 1.1 christos *newsyms++ = symbol_ptr_ptr[i];
855 1.1 christos *newsyms = (asymbol *) NULL;
856 1.1 christos symbol_ptr_ptr = bfd_ptr->outsymbols;
857 1.1 christos }
858 1.1 christos
859 1.1 christos for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
860 1.1 christos {
861 1.3 christos coff_symbol_type *coff_symbol_ptr;
862 1.3 christos
863 1.3 christos coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
864 1.1 christos symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
865 1.1 christos if (coff_symbol_ptr && coff_symbol_ptr->native)
866 1.1 christos {
867 1.1 christos combined_entry_type *s = coff_symbol_ptr->native;
868 1.1 christos int i;
869 1.1 christos
870 1.3 christos BFD_ASSERT (s->is_sym);
871 1.1 christos if (s->u.syment.n_sclass == C_FILE)
872 1.1 christos {
873 1.1 christos if (last_file != NULL)
874 1.1 christos last_file->n_value = native_index;
875 1.1 christos last_file = &(s->u.syment);
876 1.1 christos }
877 1.1 christos else
878 1.1 christos /* Modify the symbol values according to their section and
879 1.1 christos type. */
880 1.1 christos fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
881 1.1 christos
882 1.1 christos for (i = 0; i < s->u.syment.n_numaux + 1; i++)
883 1.1 christos s[i].offset = native_index++;
884 1.1 christos }
885 1.1 christos else
886 1.1 christos native_index++;
887 1.1 christos }
888 1.1 christos
889 1.1 christos obj_conv_table_size (bfd_ptr) = native_index;
890 1.1 christos
891 1.8 christos return true;
892 1.1 christos }
893 1.1 christos
894 1.1 christos /* Run thorough the symbol table again, and fix it so that all
895 1.1 christos pointers to entries are changed to the entries' index in the output
896 1.1 christos symbol table. */
897 1.1 christos
898 1.1 christos void
899 1.1 christos coff_mangle_symbols (bfd *bfd_ptr)
900 1.1 christos {
901 1.1 christos unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
902 1.1 christos asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
903 1.1 christos unsigned int symbol_index;
904 1.1 christos
905 1.1 christos for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
906 1.1 christos {
907 1.3 christos coff_symbol_type *coff_symbol_ptr;
908 1.1 christos
909 1.3 christos coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
910 1.1 christos if (coff_symbol_ptr && coff_symbol_ptr->native)
911 1.1 christos {
912 1.1 christos int i;
913 1.1 christos combined_entry_type *s = coff_symbol_ptr->native;
914 1.1 christos
915 1.3 christos BFD_ASSERT (s->is_sym);
916 1.1 christos if (s->fix_value)
917 1.1 christos {
918 1.1 christos /* FIXME: We should use a union here. */
919 1.1 christos s->u.syment.n_value =
920 1.8 christos (uintptr_t) ((combined_entry_type *)
921 1.8 christos (uintptr_t) s->u.syment.n_value)->offset;
922 1.1 christos s->fix_value = 0;
923 1.1 christos }
924 1.1 christos if (s->fix_line)
925 1.1 christos {
926 1.1 christos /* The value is the offset into the line number entries
927 1.6 christos for the symbol's section. On output, the symbol's
928 1.6 christos section should be N_DEBUG. */
929 1.1 christos s->u.syment.n_value =
930 1.1 christos (coff_symbol_ptr->symbol.section->output_section->line_filepos
931 1.1 christos + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
932 1.1 christos coff_symbol_ptr->symbol.section =
933 1.1 christos coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
934 1.1 christos BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
935 1.1 christos }
936 1.1 christos for (i = 0; i < s->u.syment.n_numaux; i++)
937 1.1 christos {
938 1.1 christos combined_entry_type *a = s + i + 1;
939 1.7 christos
940 1.3 christos BFD_ASSERT (! a->is_sym);
941 1.1 christos if (a->fix_tag)
942 1.1 christos {
943 1.9 christos a->u.auxent.x_sym.x_tagndx.u32 =
944 1.1 christos a->u.auxent.x_sym.x_tagndx.p->offset;
945 1.1 christos a->fix_tag = 0;
946 1.1 christos }
947 1.1 christos if (a->fix_end)
948 1.1 christos {
949 1.9 christos a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 =
950 1.1 christos a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
951 1.1 christos a->fix_end = 0;
952 1.1 christos }
953 1.1 christos if (a->fix_scnlen)
954 1.1 christos {
955 1.9 christos a->u.auxent.x_csect.x_scnlen.u64 =
956 1.1 christos a->u.auxent.x_csect.x_scnlen.p->offset;
957 1.1 christos a->fix_scnlen = 0;
958 1.1 christos }
959 1.1 christos }
960 1.1 christos }
961 1.1 christos }
962 1.1 christos }
963 1.1 christos
964 1.8 christos static bool
965 1.8 christos coff_write_auxent_fname (bfd *abfd,
966 1.8 christos char *str,
967 1.8 christos union internal_auxent *auxent,
968 1.8 christos struct bfd_strtab_hash *strtab,
969 1.8 christos bool hash)
970 1.8 christos {
971 1.8 christos unsigned int str_length = strlen (str);
972 1.8 christos unsigned int filnmlen = bfd_coff_filnmlen (abfd);
973 1.8 christos
974 1.8 christos if (bfd_coff_long_filenames (abfd))
975 1.8 christos {
976 1.8 christos if (str_length <= filnmlen)
977 1.8 christos strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
978 1.8 christos else
979 1.8 christos {
980 1.8 christos bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false);
981 1.8 christos
982 1.8 christos if (indx == (bfd_size_type) -1)
983 1.8 christos return false;
984 1.8 christos
985 1.8 christos auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
986 1.8 christos auxent->x_file.x_n.x_n.x_zeroes = 0;
987 1.8 christos }
988 1.8 christos }
989 1.8 christos else
990 1.8 christos {
991 1.8 christos strncpy (auxent->x_file.x_n.x_fname, str, filnmlen);
992 1.8 christos if (str_length > filnmlen)
993 1.8 christos str[filnmlen] = '\0';
994 1.8 christos }
995 1.8 christos
996 1.8 christos return true;
997 1.8 christos }
998 1.8 christos
999 1.8 christos static bool
1000 1.1 christos coff_fix_symbol_name (bfd *abfd,
1001 1.1 christos asymbol *symbol,
1002 1.1 christos combined_entry_type *native,
1003 1.8 christos struct bfd_strtab_hash *strtab,
1004 1.8 christos bool hash,
1005 1.1 christos asection **debug_string_section_p,
1006 1.1 christos bfd_size_type *debug_string_size_p)
1007 1.1 christos {
1008 1.1 christos unsigned int name_length;
1009 1.1 christos char *name = (char *) (symbol->name);
1010 1.8 christos bfd_size_type indx;
1011 1.1 christos
1012 1.1 christos if (name == NULL)
1013 1.1 christos {
1014 1.1 christos /* COFF symbols always have names, so we'll make one up. */
1015 1.1 christos symbol->name = "strange";
1016 1.1 christos name = (char *) symbol->name;
1017 1.1 christos }
1018 1.1 christos name_length = strlen (name);
1019 1.1 christos
1020 1.3 christos BFD_ASSERT (native->is_sym);
1021 1.1 christos if (native->u.syment.n_sclass == C_FILE
1022 1.1 christos && native->u.syment.n_numaux > 0)
1023 1.1 christos {
1024 1.1 christos if (bfd_coff_force_symnames_in_strings (abfd))
1025 1.1 christos {
1026 1.8 christos indx = _bfd_stringtab_add (strtab, ".file", hash, false);
1027 1.8 christos if (indx == (bfd_size_type) -1)
1028 1.8 christos return false;
1029 1.8 christos
1030 1.8 christos native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1031 1.1 christos native->u.syment._n._n_n._n_zeroes = 0;
1032 1.1 christos }
1033 1.1 christos else
1034 1.6 christos strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
1035 1.1 christos
1036 1.3 christos BFD_ASSERT (! (native + 1)->is_sym);
1037 1.8 christos if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent,
1038 1.8 christos strtab, hash))
1039 1.8 christos return false;
1040 1.1 christos }
1041 1.1 christos else
1042 1.1 christos {
1043 1.1 christos if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
1044 1.1 christos /* This name will fit into the symbol neatly. */
1045 1.1 christos strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
1046 1.1 christos
1047 1.1 christos else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
1048 1.1 christos {
1049 1.8 christos indx = _bfd_stringtab_add (strtab, name, hash, false);
1050 1.8 christos if (indx == (bfd_size_type) -1)
1051 1.8 christos return false;
1052 1.8 christos
1053 1.8 christos native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
1054 1.1 christos native->u.syment._n._n_n._n_zeroes = 0;
1055 1.1 christos }
1056 1.1 christos else
1057 1.1 christos {
1058 1.1 christos file_ptr filepos;
1059 1.1 christos bfd_byte buf[4];
1060 1.1 christos int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
1061 1.1 christos
1062 1.1 christos /* This name should be written into the .debug section. For
1063 1.1 christos some reason each name is preceded by a two byte length
1064 1.1 christos and also followed by a null byte. FIXME: We assume that
1065 1.1 christos the .debug section has already been created, and that it
1066 1.1 christos is large enough. */
1067 1.1 christos if (*debug_string_section_p == (asection *) NULL)
1068 1.1 christos *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
1069 1.1 christos filepos = bfd_tell (abfd);
1070 1.1 christos if (prefix_len == 4)
1071 1.1 christos bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
1072 1.1 christos else
1073 1.1 christos bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
1074 1.1 christos
1075 1.1 christos if (!bfd_set_section_contents (abfd,
1076 1.1 christos *debug_string_section_p,
1077 1.1 christos (void *) buf,
1078 1.1 christos (file_ptr) *debug_string_size_p,
1079 1.1 christos (bfd_size_type) prefix_len)
1080 1.1 christos || !bfd_set_section_contents (abfd,
1081 1.1 christos *debug_string_section_p,
1082 1.1 christos (void *) symbol->name,
1083 1.1 christos (file_ptr) (*debug_string_size_p
1084 1.1 christos + prefix_len),
1085 1.1 christos (bfd_size_type) name_length + 1))
1086 1.1 christos abort ();
1087 1.1 christos if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
1088 1.1 christos abort ();
1089 1.1 christos native->u.syment._n._n_n._n_offset =
1090 1.1 christos *debug_string_size_p + prefix_len;
1091 1.1 christos native->u.syment._n._n_n._n_zeroes = 0;
1092 1.1 christos *debug_string_size_p += name_length + 1 + prefix_len;
1093 1.1 christos }
1094 1.1 christos }
1095 1.8 christos
1096 1.8 christos return true;
1097 1.1 christos }
1098 1.1 christos
1099 1.1 christos /* We need to keep track of the symbol index so that when we write out
1100 1.1 christos the relocs we can get the index for a symbol. This method is a
1101 1.1 christos hack. FIXME. */
1102 1.1 christos
1103 1.1 christos #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
1104 1.1 christos
1105 1.1 christos /* Write a symbol out to a COFF file. */
1106 1.1 christos
1107 1.8 christos static bool
1108 1.1 christos coff_write_symbol (bfd *abfd,
1109 1.1 christos asymbol *symbol,
1110 1.1 christos combined_entry_type *native,
1111 1.1 christos bfd_vma *written,
1112 1.8 christos struct bfd_strtab_hash *strtab,
1113 1.8 christos bool hash,
1114 1.1 christos asection **debug_string_section_p,
1115 1.1 christos bfd_size_type *debug_string_size_p)
1116 1.1 christos {
1117 1.1 christos unsigned int numaux = native->u.syment.n_numaux;
1118 1.1 christos int type = native->u.syment.n_type;
1119 1.1 christos int n_sclass = (int) native->u.syment.n_sclass;
1120 1.1 christos asection *output_section = symbol->section->output_section
1121 1.1 christos ? symbol->section->output_section
1122 1.1 christos : symbol->section;
1123 1.1 christos void * buf;
1124 1.1 christos bfd_size_type symesz;
1125 1.1 christos
1126 1.3 christos BFD_ASSERT (native->is_sym);
1127 1.3 christos
1128 1.1 christos if (native->u.syment.n_sclass == C_FILE)
1129 1.1 christos symbol->flags |= BSF_DEBUGGING;
1130 1.1 christos
1131 1.1 christos if (symbol->flags & BSF_DEBUGGING
1132 1.1 christos && bfd_is_abs_section (symbol->section))
1133 1.1 christos native->u.syment.n_scnum = N_DEBUG;
1134 1.1 christos
1135 1.1 christos else if (bfd_is_abs_section (symbol->section))
1136 1.1 christos native->u.syment.n_scnum = N_ABS;
1137 1.1 christos
1138 1.1 christos else if (bfd_is_und_section (symbol->section))
1139 1.1 christos native->u.syment.n_scnum = N_UNDEF;
1140 1.1 christos
1141 1.1 christos else
1142 1.1 christos native->u.syment.n_scnum =
1143 1.1 christos output_section->target_index;
1144 1.1 christos
1145 1.8 christos if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash,
1146 1.8 christos debug_string_section_p, debug_string_size_p))
1147 1.8 christos return false;
1148 1.1 christos
1149 1.1 christos symesz = bfd_coff_symesz (abfd);
1150 1.1 christos buf = bfd_alloc (abfd, symesz);
1151 1.1 christos if (!buf)
1152 1.8 christos return false;
1153 1.1 christos bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
1154 1.9 christos if (bfd_write (buf, symesz, abfd) != symesz)
1155 1.8 christos return false;
1156 1.1 christos bfd_release (abfd, buf);
1157 1.1 christos
1158 1.1 christos if (native->u.syment.n_numaux > 0)
1159 1.1 christos {
1160 1.1 christos bfd_size_type auxesz;
1161 1.1 christos unsigned int j;
1162 1.1 christos
1163 1.1 christos auxesz = bfd_coff_auxesz (abfd);
1164 1.1 christos buf = bfd_alloc (abfd, auxesz);
1165 1.1 christos if (!buf)
1166 1.8 christos return false;
1167 1.1 christos for (j = 0; j < native->u.syment.n_numaux; j++)
1168 1.1 christos {
1169 1.3 christos BFD_ASSERT (! (native + j + 1)->is_sym);
1170 1.8 christos
1171 1.8 christos /* Adjust auxent only if this isn't the filename
1172 1.8 christos auxiliary entry. */
1173 1.8 christos if (native->u.syment.n_sclass == C_FILE
1174 1.9 christos && (native + j + 1)->u.auxent.x_file.x_ftype
1175 1.9 christos && (native + j + 1)->extrap)
1176 1.8 christos coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap,
1177 1.8 christos &(native + j + 1)->u.auxent, strtab, hash);
1178 1.8 christos
1179 1.1 christos bfd_coff_swap_aux_out (abfd,
1180 1.1 christos &((native + j + 1)->u.auxent),
1181 1.1 christos type, n_sclass, (int) j,
1182 1.1 christos native->u.syment.n_numaux,
1183 1.1 christos buf);
1184 1.9 christos if (bfd_write (buf, auxesz, abfd) != auxesz)
1185 1.8 christos return false;
1186 1.1 christos }
1187 1.1 christos bfd_release (abfd, buf);
1188 1.1 christos }
1189 1.1 christos
1190 1.1 christos /* Store the index for use when we write out the relocs. */
1191 1.1 christos set_index (symbol, *written);
1192 1.1 christos
1193 1.1 christos *written += numaux + 1;
1194 1.8 christos return true;
1195 1.1 christos }
1196 1.1 christos
1197 1.1 christos /* Write out a symbol to a COFF file that does not come from a COFF
1198 1.1 christos file originally. This symbol may have been created by the linker,
1199 1.1 christos or we may be linking a non COFF file to a COFF file. */
1200 1.1 christos
1201 1.8 christos bool
1202 1.1 christos coff_write_alien_symbol (bfd *abfd,
1203 1.1 christos asymbol *symbol,
1204 1.1 christos struct internal_syment *isym,
1205 1.1 christos bfd_vma *written,
1206 1.8 christos struct bfd_strtab_hash *strtab,
1207 1.8 christos bool hash,
1208 1.1 christos asection **debug_string_section_p,
1209 1.1 christos bfd_size_type *debug_string_size_p)
1210 1.1 christos {
1211 1.1 christos combined_entry_type *native;
1212 1.1 christos combined_entry_type dummy[2];
1213 1.1 christos asection *output_section = symbol->section->output_section
1214 1.1 christos ? symbol->section->output_section
1215 1.1 christos : symbol->section;
1216 1.1 christos struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1217 1.8 christos bool ret;
1218 1.1 christos
1219 1.1 christos if ((!link_info || link_info->strip_discarded)
1220 1.1 christos && !bfd_is_abs_section (symbol->section)
1221 1.1 christos && symbol->section->output_section == bfd_abs_section_ptr)
1222 1.1 christos {
1223 1.1 christos symbol->name = "";
1224 1.1 christos if (isym != NULL)
1225 1.6 christos memset (isym, 0, sizeof (*isym));
1226 1.8 christos return true;
1227 1.1 christos }
1228 1.8 christos memset (dummy, 0, sizeof dummy);
1229 1.1 christos native = dummy;
1230 1.8 christos native->is_sym = true;
1231 1.8 christos native[1].is_sym = false;
1232 1.1 christos native->u.syment.n_type = T_NULL;
1233 1.1 christos native->u.syment.n_flags = 0;
1234 1.1 christos native->u.syment.n_numaux = 0;
1235 1.1 christos if (bfd_is_und_section (symbol->section))
1236 1.1 christos {
1237 1.1 christos native->u.syment.n_scnum = N_UNDEF;
1238 1.1 christos native->u.syment.n_value = symbol->value;
1239 1.1 christos }
1240 1.1 christos else if (bfd_is_com_section (symbol->section))
1241 1.1 christos {
1242 1.1 christos native->u.syment.n_scnum = N_UNDEF;
1243 1.1 christos native->u.syment.n_value = symbol->value;
1244 1.1 christos }
1245 1.1 christos else if (symbol->flags & BSF_FILE)
1246 1.1 christos {
1247 1.1 christos native->u.syment.n_scnum = N_DEBUG;
1248 1.1 christos native->u.syment.n_numaux = 1;
1249 1.1 christos }
1250 1.1 christos else if (symbol->flags & BSF_DEBUGGING)
1251 1.1 christos {
1252 1.1 christos /* There isn't much point to writing out a debugging symbol
1253 1.6 christos unless we are prepared to convert it into COFF debugging
1254 1.6 christos format. So, we just ignore them. We must clobber the symbol
1255 1.6 christos name to keep it from being put in the string table. */
1256 1.1 christos symbol->name = "";
1257 1.1 christos if (isym != NULL)
1258 1.6 christos memset (isym, 0, sizeof (*isym));
1259 1.8 christos return true;
1260 1.1 christos }
1261 1.1 christos else
1262 1.1 christos {
1263 1.1 christos native->u.syment.n_scnum = output_section->target_index;
1264 1.1 christos native->u.syment.n_value = (symbol->value
1265 1.1 christos + symbol->section->output_offset);
1266 1.1 christos if (! obj_pe (abfd))
1267 1.1 christos native->u.syment.n_value += output_section->vma;
1268 1.1 christos
1269 1.1 christos /* Copy the any flags from the file header into the symbol.
1270 1.6 christos FIXME: Why? */
1271 1.1 christos {
1272 1.3 christos coff_symbol_type *c = coff_symbol_from (symbol);
1273 1.1 christos if (c != (coff_symbol_type *) NULL)
1274 1.1 christos native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1275 1.1 christos }
1276 1.1 christos }
1277 1.1 christos
1278 1.1 christos native->u.syment.n_type = 0;
1279 1.1 christos if (symbol->flags & BSF_FILE)
1280 1.1 christos native->u.syment.n_sclass = C_FILE;
1281 1.1 christos else if (symbol->flags & BSF_LOCAL)
1282 1.1 christos native->u.syment.n_sclass = C_STAT;
1283 1.1 christos else if (symbol->flags & BSF_WEAK)
1284 1.1 christos native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1285 1.1 christos else
1286 1.1 christos native->u.syment.n_sclass = C_EXT;
1287 1.1 christos
1288 1.8 christos ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash,
1289 1.1 christos debug_string_section_p, debug_string_size_p);
1290 1.1 christos if (isym != NULL)
1291 1.1 christos *isym = native->u.syment;
1292 1.1 christos return ret;
1293 1.1 christos }
1294 1.1 christos
1295 1.1 christos /* Write a native symbol to a COFF file. */
1296 1.1 christos
1297 1.8 christos static bool
1298 1.1 christos coff_write_native_symbol (bfd *abfd,
1299 1.1 christos coff_symbol_type *symbol,
1300 1.1 christos bfd_vma *written,
1301 1.8 christos struct bfd_strtab_hash *strtab,
1302 1.1 christos asection **debug_string_section_p,
1303 1.1 christos bfd_size_type *debug_string_size_p)
1304 1.1 christos {
1305 1.1 christos combined_entry_type *native = symbol->native;
1306 1.1 christos alent *lineno = symbol->lineno;
1307 1.1 christos struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1308 1.1 christos
1309 1.1 christos if ((!link_info || link_info->strip_discarded)
1310 1.1 christos && !bfd_is_abs_section (symbol->symbol.section)
1311 1.1 christos && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1312 1.1 christos {
1313 1.1 christos symbol->symbol.name = "";
1314 1.8 christos return true;
1315 1.1 christos }
1316 1.1 christos
1317 1.3 christos BFD_ASSERT (native->is_sym);
1318 1.1 christos /* If this symbol has an associated line number, we must store the
1319 1.1 christos symbol index in the line number field. We also tag the auxent to
1320 1.1 christos point to the right place in the lineno table. */
1321 1.1 christos if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1322 1.1 christos {
1323 1.1 christos unsigned int count = 0;
1324 1.1 christos
1325 1.1 christos lineno[count].u.offset = *written;
1326 1.1 christos if (native->u.syment.n_numaux)
1327 1.1 christos {
1328 1.1 christos union internal_auxent *a = &((native + 1)->u.auxent);
1329 1.1 christos
1330 1.1 christos a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1331 1.1 christos symbol->symbol.section->output_section->moving_line_filepos;
1332 1.1 christos }
1333 1.1 christos
1334 1.1 christos /* Count and relocate all other linenumbers. */
1335 1.1 christos count++;
1336 1.1 christos while (lineno[count].line_number != 0)
1337 1.1 christos {
1338 1.1 christos lineno[count].u.offset +=
1339 1.1 christos (symbol->symbol.section->output_section->vma
1340 1.1 christos + symbol->symbol.section->output_offset);
1341 1.1 christos count++;
1342 1.1 christos }
1343 1.8 christos symbol->done_lineno = true;
1344 1.1 christos
1345 1.1 christos if (! bfd_is_const_section (symbol->symbol.section->output_section))
1346 1.1 christos symbol->symbol.section->output_section->moving_line_filepos +=
1347 1.1 christos count * bfd_coff_linesz (abfd);
1348 1.1 christos }
1349 1.1 christos
1350 1.1 christos return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1351 1.8 christos strtab, true, debug_string_section_p,
1352 1.1 christos debug_string_size_p);
1353 1.1 christos }
1354 1.1 christos
1355 1.1 christos static void
1356 1.6 christos null_error_handler (const char *fmt ATTRIBUTE_UNUSED,
1357 1.6 christos va_list ap ATTRIBUTE_UNUSED)
1358 1.1 christos {
1359 1.1 christos }
1360 1.1 christos
1361 1.1 christos /* Write out the COFF symbols. */
1362 1.1 christos
1363 1.8 christos bool
1364 1.1 christos coff_write_symbols (bfd *abfd)
1365 1.1 christos {
1366 1.8 christos struct bfd_strtab_hash *strtab;
1367 1.1 christos asection *debug_string_section;
1368 1.1 christos bfd_size_type debug_string_size;
1369 1.1 christos unsigned int i;
1370 1.1 christos unsigned int limit = bfd_get_symcount (abfd);
1371 1.1 christos bfd_vma written = 0;
1372 1.1 christos asymbol **p;
1373 1.1 christos
1374 1.1 christos debug_string_section = NULL;
1375 1.1 christos debug_string_size = 0;
1376 1.1 christos
1377 1.8 christos strtab = _bfd_stringtab_init ();
1378 1.8 christos if (strtab == NULL)
1379 1.8 christos return false;
1380 1.8 christos
1381 1.1 christos /* If this target supports long section names, they must be put into
1382 1.1 christos the string table. This is supported by PE. This code must
1383 1.1 christos handle section names just as they are handled in
1384 1.8 christos coff_write_object_contents. This is why we pass hash as FALSE below. */
1385 1.1 christos if (bfd_coff_long_section_names (abfd))
1386 1.1 christos {
1387 1.1 christos asection *o;
1388 1.1 christos
1389 1.1 christos for (o = abfd->sections; o != NULL; o = o->next)
1390 1.8 christos if (strlen (o->name) > SCNNMLEN
1391 1.8 christos && _bfd_stringtab_add (strtab, o->name, false, false)
1392 1.8 christos == (bfd_size_type) -1)
1393 1.8 christos return false;
1394 1.1 christos }
1395 1.1 christos
1396 1.1 christos /* Seek to the right place. */
1397 1.1 christos if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1398 1.8 christos return false;
1399 1.1 christos
1400 1.1 christos /* Output all the symbols we have. */
1401 1.1 christos written = 0;
1402 1.1 christos for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1403 1.1 christos {
1404 1.1 christos asymbol *symbol = *p;
1405 1.3 christos coff_symbol_type *c_symbol = coff_symbol_from (symbol);
1406 1.1 christos
1407 1.1 christos if (c_symbol == (coff_symbol_type *) NULL
1408 1.1 christos || c_symbol->native == (combined_entry_type *) NULL)
1409 1.1 christos {
1410 1.8 christos if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1411 1.8 christos strtab, true, &debug_string_section,
1412 1.1 christos &debug_string_size))
1413 1.8 christos return false;
1414 1.1 christos }
1415 1.1 christos else
1416 1.1 christos {
1417 1.1 christos if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1418 1.1 christos {
1419 1.1 christos bfd_error_handler_type current_error_handler;
1420 1.1 christos enum coff_symbol_classification sym_class;
1421 1.1 christos unsigned char *n_sclass;
1422 1.1 christos
1423 1.1 christos /* Suppress error reporting by bfd_coff_classify_symbol.
1424 1.1 christos Error messages can be generated when we are processing a local
1425 1.1 christos symbol which has no associated section and we do not have to
1426 1.1 christos worry about this, all we need to know is that it is local. */
1427 1.1 christos current_error_handler = bfd_set_error_handler (null_error_handler);
1428 1.3 christos BFD_ASSERT (c_symbol->native->is_sym);
1429 1.1 christos sym_class = bfd_coff_classify_symbol (abfd,
1430 1.3 christos &c_symbol->native->u.syment);
1431 1.1 christos (void) bfd_set_error_handler (current_error_handler);
1432 1.1 christos
1433 1.1 christos n_sclass = &c_symbol->native->u.syment.n_sclass;
1434 1.1 christos
1435 1.1 christos /* If the symbol class has been changed (eg objcopy/ld script/etc)
1436 1.1 christos we cannot retain the existing sclass from the original symbol.
1437 1.1 christos Weak symbols only have one valid sclass, so just set it always.
1438 1.1 christos If it is not local class and should be, set it C_STAT.
1439 1.1 christos If it is global and not classified as global, or if it is
1440 1.1 christos weak (which is also classified as global), set it C_EXT. */
1441 1.1 christos
1442 1.1 christos if (symbol->flags & BSF_WEAK)
1443 1.1 christos *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1444 1.1 christos else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
1445 1.1 christos *n_sclass = C_STAT;
1446 1.1 christos else if (symbol->flags & BSF_GLOBAL
1447 1.1 christos && (sym_class != COFF_SYMBOL_GLOBAL
1448 1.1 christos #ifdef COFF_WITH_PE
1449 1.1 christos || *n_sclass == C_NT_WEAK
1450 1.1 christos #endif
1451 1.1 christos || *n_sclass == C_WEAKEXT))
1452 1.1 christos c_symbol->native->u.syment.n_sclass = C_EXT;
1453 1.1 christos }
1454 1.1 christos
1455 1.1 christos if (!coff_write_native_symbol (abfd, c_symbol, &written,
1456 1.8 christos strtab, &debug_string_section,
1457 1.1 christos &debug_string_size))
1458 1.8 christos return false;
1459 1.1 christos }
1460 1.1 christos }
1461 1.1 christos
1462 1.1 christos obj_raw_syment_count (abfd) = written;
1463 1.1 christos
1464 1.8 christos /* Now write out strings.
1465 1.8 christos
1466 1.8 christos We would normally not write anything here if there are no strings, but
1467 1.8 christos we'll write out 4 so that any stupid coff reader which tries to read the
1468 1.8 christos string table even when there isn't one won't croak. */
1469 1.8 christos {
1470 1.8 christos bfd_byte buffer[STRING_SIZE_SIZE];
1471 1.1 christos
1472 1.1 christos #if STRING_SIZE_SIZE == 4
1473 1.8 christos H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer);
1474 1.1 christos #else
1475 1.1 christos #error Change H_PUT_32
1476 1.1 christos #endif
1477 1.9 christos if (bfd_write (buffer, sizeof (buffer), abfd) != sizeof (buffer))
1478 1.8 christos return false;
1479 1.1 christos
1480 1.8 christos if (! _bfd_stringtab_emit (abfd, strtab))
1481 1.8 christos return false;
1482 1.8 christos }
1483 1.1 christos
1484 1.8 christos _bfd_stringtab_free (strtab);
1485 1.1 christos
1486 1.1 christos /* Make sure the .debug section was created to be the correct size.
1487 1.1 christos We should create it ourselves on the fly, but we don't because
1488 1.1 christos BFD won't let us write to any section until we know how large all
1489 1.1 christos the sections are. We could still do it by making another pass
1490 1.1 christos over the symbols. FIXME. */
1491 1.1 christos BFD_ASSERT (debug_string_size == 0
1492 1.1 christos || (debug_string_section != (asection *) NULL
1493 1.1 christos && (BFD_ALIGN (debug_string_size,
1494 1.1 christos 1 << debug_string_section->alignment_power)
1495 1.1 christos == debug_string_section->size)));
1496 1.1 christos
1497 1.8 christos return true;
1498 1.1 christos }
1499 1.1 christos
1500 1.8 christos bool
1501 1.1 christos coff_write_linenumbers (bfd *abfd)
1502 1.1 christos {
1503 1.1 christos asection *s;
1504 1.1 christos bfd_size_type linesz;
1505 1.1 christos void * buff;
1506 1.1 christos
1507 1.1 christos linesz = bfd_coff_linesz (abfd);
1508 1.1 christos buff = bfd_alloc (abfd, linesz);
1509 1.1 christos if (!buff)
1510 1.8 christos return false;
1511 1.1 christos for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1512 1.1 christos {
1513 1.1 christos if (s->lineno_count)
1514 1.1 christos {
1515 1.1 christos asymbol **q = abfd->outsymbols;
1516 1.1 christos if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1517 1.8 christos return false;
1518 1.1 christos /* Find all the linenumbers in this section. */
1519 1.1 christos while (*q)
1520 1.1 christos {
1521 1.1 christos asymbol *p = *q;
1522 1.1 christos if (p->section->output_section == s)
1523 1.1 christos {
1524 1.1 christos alent *l =
1525 1.1 christos BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1526 1.1 christos (bfd_asymbol_bfd (p), p));
1527 1.1 christos if (l)
1528 1.1 christos {
1529 1.1 christos /* Found a linenumber entry, output. */
1530 1.1 christos struct internal_lineno out;
1531 1.3 christos
1532 1.1 christos memset ((void *) & out, 0, sizeof (out));
1533 1.1 christos out.l_lnno = 0;
1534 1.1 christos out.l_addr.l_symndx = l->u.offset;
1535 1.1 christos bfd_coff_swap_lineno_out (abfd, &out, buff);
1536 1.9 christos if (bfd_write (buff, linesz, abfd) != linesz)
1537 1.8 christos return false;
1538 1.1 christos l++;
1539 1.1 christos while (l->line_number)
1540 1.1 christos {
1541 1.1 christos out.l_lnno = l->line_number;
1542 1.1 christos out.l_addr.l_symndx = l->u.offset;
1543 1.1 christos bfd_coff_swap_lineno_out (abfd, &out, buff);
1544 1.9 christos if (bfd_write (buff, linesz, abfd) != linesz)
1545 1.8 christos return false;
1546 1.1 christos l++;
1547 1.1 christos }
1548 1.1 christos }
1549 1.1 christos }
1550 1.1 christos q++;
1551 1.1 christos }
1552 1.1 christos }
1553 1.1 christos }
1554 1.1 christos bfd_release (abfd, buff);
1555 1.8 christos return true;
1556 1.1 christos }
1557 1.1 christos
1558 1.1 christos alent *
1559 1.1 christos coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
1560 1.1 christos {
1561 1.1 christos return coffsymbol (symbol)->lineno;
1562 1.1 christos }
1563 1.1 christos
1564 1.1 christos /* This function transforms the offsets into the symbol table into
1565 1.1 christos pointers to syments. */
1566 1.1 christos
1567 1.1 christos static void
1568 1.1 christos coff_pointerize_aux (bfd *abfd,
1569 1.1 christos combined_entry_type *table_base,
1570 1.1 christos combined_entry_type *symbol,
1571 1.1 christos unsigned int indaux,
1572 1.9 christos combined_entry_type *auxent)
1573 1.1 christos {
1574 1.1 christos unsigned int type = symbol->u.syment.n_type;
1575 1.1 christos unsigned int n_sclass = symbol->u.syment.n_sclass;
1576 1.1 christos
1577 1.3 christos BFD_ASSERT (symbol->is_sym);
1578 1.1 christos if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1579 1.1 christos {
1580 1.1 christos if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1581 1.1 christos (abfd, table_base, symbol, indaux, auxent))
1582 1.1 christos return;
1583 1.1 christos }
1584 1.1 christos
1585 1.1 christos /* Don't bother if this is a file or a section. */
1586 1.1 christos if (n_sclass == C_STAT && type == T_NULL)
1587 1.1 christos return;
1588 1.1 christos if (n_sclass == C_FILE)
1589 1.1 christos return;
1590 1.8 christos if (n_sclass == C_DWARF)
1591 1.8 christos return;
1592 1.1 christos
1593 1.3 christos BFD_ASSERT (! auxent->is_sym);
1594 1.1 christos /* Otherwise patch up. */
1595 1.1 christos #define N_TMASK coff_data (abfd)->local_n_tmask
1596 1.1 christos #define N_BTSHFT coff_data (abfd)->local_n_btshft
1597 1.3 christos
1598 1.1 christos if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1599 1.1 christos || n_sclass == C_FCN)
1600 1.9 christos && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32 > 0
1601 1.9 christos && (auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32
1602 1.9 christos < obj_raw_syment_count (abfd)))
1603 1.1 christos {
1604 1.1 christos auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1605 1.9 christos table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
1606 1.1 christos auxent->fix_end = 1;
1607 1.1 christos }
1608 1.7 christos
1609 1.1 christos /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1610 1.1 christos generate one, so we must be careful to ignore it. */
1611 1.9 christos if (auxent->u.auxent.x_sym.x_tagndx.u32 < obj_raw_syment_count (abfd))
1612 1.1 christos {
1613 1.1 christos auxent->u.auxent.x_sym.x_tagndx.p =
1614 1.9 christos table_base + auxent->u.auxent.x_sym.x_tagndx.u32;
1615 1.1 christos auxent->fix_tag = 1;
1616 1.1 christos }
1617 1.1 christos }
1618 1.1 christos
1619 1.1 christos /* Allocate space for the ".debug" section, and read it.
1620 1.1 christos We did not read the debug section until now, because
1621 1.1 christos we didn't want to go to the trouble until someone needed it. */
1622 1.1 christos
1623 1.1 christos static char *
1624 1.3 christos build_debug_section (bfd *abfd, asection ** sect_return)
1625 1.1 christos {
1626 1.1 christos char *debug_section;
1627 1.1 christos file_ptr position;
1628 1.1 christos bfd_size_type sec_size;
1629 1.1 christos
1630 1.1 christos asection *sect = bfd_get_section_by_name (abfd, ".debug");
1631 1.1 christos
1632 1.1 christos if (!sect)
1633 1.1 christos {
1634 1.1 christos bfd_set_error (bfd_error_no_debug_section);
1635 1.1 christos return NULL;
1636 1.1 christos }
1637 1.1 christos
1638 1.1 christos /* Seek to the beginning of the `.debug' section and read it.
1639 1.1 christos Save the current position first; it is needed by our caller.
1640 1.1 christos Then read debug section and reset the file pointer. */
1641 1.1 christos
1642 1.1 christos position = bfd_tell (abfd);
1643 1.8 christos if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0)
1644 1.8 christos return NULL;
1645 1.8 christos
1646 1.8 christos sec_size = sect->size;
1647 1.9 christos debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size);
1648 1.8 christos if (debug_section == NULL)
1649 1.8 christos return NULL;
1650 1.9 christos debug_section[sec_size] = 0;
1651 1.8 christos
1652 1.8 christos if (bfd_seek (abfd, position, SEEK_SET) != 0)
1653 1.1 christos return NULL;
1654 1.3 christos
1655 1.3 christos * sect_return = sect;
1656 1.1 christos return debug_section;
1657 1.1 christos }
1658 1.1 christos
1659 1.1 christos /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1660 1.1 christos \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1661 1.1 christos be \0-terminated. */
1662 1.1 christos
1663 1.1 christos static char *
1664 1.1 christos copy_name (bfd *abfd, char *name, size_t maxlen)
1665 1.1 christos {
1666 1.1 christos size_t len;
1667 1.1 christos char *newname;
1668 1.1 christos
1669 1.1 christos for (len = 0; len < maxlen; ++len)
1670 1.1 christos if (name[len] == '\0')
1671 1.1 christos break;
1672 1.1 christos
1673 1.1 christos if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
1674 1.1 christos return NULL;
1675 1.1 christos
1676 1.1 christos strncpy (newname, name, len);
1677 1.1 christos newname[len] = '\0';
1678 1.1 christos return newname;
1679 1.1 christos }
1680 1.1 christos
1681 1.1 christos /* Read in the external symbols. */
1682 1.1 christos
1683 1.8 christos bool
1684 1.1 christos _bfd_coff_get_external_symbols (bfd *abfd)
1685 1.1 christos {
1686 1.8 christos size_t symesz;
1687 1.8 christos size_t size;
1688 1.1 christos void * syms;
1689 1.9 christos ufile_ptr filesize;
1690 1.1 christos
1691 1.1 christos if (obj_coff_external_syms (abfd) != NULL)
1692 1.8 christos return true;
1693 1.1 christos
1694 1.1 christos symesz = bfd_coff_symesz (abfd);
1695 1.8 christos if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size))
1696 1.6 christos {
1697 1.8 christos bfd_set_error (bfd_error_file_truncated);
1698 1.8 christos return false;
1699 1.6 christos }
1700 1.1 christos
1701 1.8 christos if (size == 0)
1702 1.8 christos return true;
1703 1.1 christos
1704 1.9 christos filesize = bfd_get_file_size (abfd);
1705 1.9 christos if (filesize != 0
1706 1.9 christos && ((ufile_ptr) obj_sym_filepos (abfd) > filesize
1707 1.9 christos || size > filesize - obj_sym_filepos (abfd)))
1708 1.9 christos {
1709 1.9 christos bfd_set_error (bfd_error_file_truncated);
1710 1.9 christos return false;
1711 1.9 christos }
1712 1.9 christos
1713 1.8 christos if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1714 1.8 christos return false;
1715 1.8 christos syms = _bfd_malloc_and_read (abfd, size, size);
1716 1.1 christos obj_coff_external_syms (abfd) = syms;
1717 1.8 christos return syms != NULL;
1718 1.1 christos }
1719 1.1 christos
1720 1.1 christos /* Read in the external strings. The strings are not loaded until
1721 1.1 christos they are needed. This is because we have no simple way of
1722 1.3 christos detecting a missing string table in an archive. If the strings
1723 1.3 christos are loaded then the STRINGS and STRINGS_LEN fields in the
1724 1.3 christos coff_tdata structure will be set. */
1725 1.1 christos
1726 1.1 christos const char *
1727 1.1 christos _bfd_coff_read_string_table (bfd *abfd)
1728 1.1 christos {
1729 1.1 christos char extstrsize[STRING_SIZE_SIZE];
1730 1.1 christos bfd_size_type strsize;
1731 1.1 christos char *strings;
1732 1.8 christos ufile_ptr pos;
1733 1.8 christos ufile_ptr filesize;
1734 1.8 christos size_t symesz;
1735 1.8 christos size_t size;
1736 1.1 christos
1737 1.1 christos if (obj_coff_strings (abfd) != NULL)
1738 1.1 christos return obj_coff_strings (abfd);
1739 1.1 christos
1740 1.1 christos if (obj_sym_filepos (abfd) == 0)
1741 1.1 christos {
1742 1.1 christos bfd_set_error (bfd_error_no_symbols);
1743 1.1 christos return NULL;
1744 1.1 christos }
1745 1.1 christos
1746 1.8 christos symesz = bfd_coff_symesz (abfd);
1747 1.1 christos pos = obj_sym_filepos (abfd);
1748 1.8 christos if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)
1749 1.8 christos || pos + size < pos)
1750 1.8 christos {
1751 1.8 christos bfd_set_error (bfd_error_file_truncated);
1752 1.8 christos return NULL;
1753 1.8 christos }
1754 1.8 christos
1755 1.8 christos if (bfd_seek (abfd, pos + size, SEEK_SET) != 0)
1756 1.1 christos return NULL;
1757 1.1 christos
1758 1.9 christos if (bfd_read (extstrsize, sizeof extstrsize, abfd) != sizeof extstrsize)
1759 1.1 christos {
1760 1.1 christos if (bfd_get_error () != bfd_error_file_truncated)
1761 1.1 christos return NULL;
1762 1.1 christos
1763 1.1 christos /* There is no string table. */
1764 1.1 christos strsize = STRING_SIZE_SIZE;
1765 1.1 christos }
1766 1.1 christos else
1767 1.1 christos {
1768 1.1 christos #if STRING_SIZE_SIZE == 4
1769 1.1 christos strsize = H_GET_32 (abfd, extstrsize);
1770 1.1 christos #else
1771 1.1 christos #error Change H_GET_32
1772 1.1 christos #endif
1773 1.1 christos }
1774 1.1 christos
1775 1.8 christos filesize = bfd_get_file_size (abfd);
1776 1.8 christos if (strsize < STRING_SIZE_SIZE
1777 1.8 christos || (filesize != 0 && strsize > filesize))
1778 1.1 christos {
1779 1.6 christos _bfd_error_handler
1780 1.6 christos /* xgettext: c-format */
1781 1.6 christos (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize);
1782 1.1 christos bfd_set_error (bfd_error_bad_value);
1783 1.1 christos return NULL;
1784 1.1 christos }
1785 1.1 christos
1786 1.3 christos strings = (char *) bfd_malloc (strsize + 1);
1787 1.1 christos if (strings == NULL)
1788 1.1 christos return NULL;
1789 1.1 christos
1790 1.3 christos /* PR 17521 file: 079-54929-0.004.
1791 1.3 christos A corrupt file could contain an index that points into the first
1792 1.3 christos STRING_SIZE_SIZE bytes of the string table, so make sure that
1793 1.3 christos they are zero. */
1794 1.3 christos memset (strings, 0, STRING_SIZE_SIZE);
1795 1.3 christos
1796 1.9 christos if (bfd_read (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
1797 1.1 christos != strsize - STRING_SIZE_SIZE)
1798 1.1 christos {
1799 1.1 christos free (strings);
1800 1.1 christos return NULL;
1801 1.1 christos }
1802 1.1 christos
1803 1.1 christos obj_coff_strings (abfd) = strings;
1804 1.3 christos obj_coff_strings_len (abfd) = strsize;
1805 1.3 christos /* Terminate the string table, just in case. */
1806 1.3 christos strings[strsize] = 0;
1807 1.1 christos return strings;
1808 1.1 christos }
1809 1.1 christos
1810 1.1 christos /* Free up the external symbols and strings read from a COFF file. */
1811 1.1 christos
1812 1.8 christos bool
1813 1.1 christos _bfd_coff_free_symbols (bfd *abfd)
1814 1.1 christos {
1815 1.6 christos if (! bfd_family_coff (abfd))
1816 1.8 christos return false;
1817 1.6 christos
1818 1.1 christos if (obj_coff_external_syms (abfd) != NULL
1819 1.1 christos && ! obj_coff_keep_syms (abfd))
1820 1.1 christos {
1821 1.1 christos free (obj_coff_external_syms (abfd));
1822 1.1 christos obj_coff_external_syms (abfd) = NULL;
1823 1.1 christos }
1824 1.6 christos
1825 1.1 christos if (obj_coff_strings (abfd) != NULL
1826 1.1 christos && ! obj_coff_keep_strings (abfd))
1827 1.1 christos {
1828 1.1 christos free (obj_coff_strings (abfd));
1829 1.1 christos obj_coff_strings (abfd) = NULL;
1830 1.3 christos obj_coff_strings_len (abfd) = 0;
1831 1.1 christos }
1832 1.6 christos
1833 1.8 christos return true;
1834 1.1 christos }
1835 1.1 christos
1836 1.1 christos /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1837 1.1 christos knit the symbol names into a normalized form. By normalized here I
1838 1.1 christos mean that all symbols have an n_offset pointer that points to a null-
1839 1.1 christos terminated string. */
1840 1.1 christos
1841 1.1 christos combined_entry_type *
1842 1.1 christos coff_get_normalized_symtab (bfd *abfd)
1843 1.1 christos {
1844 1.1 christos combined_entry_type *internal;
1845 1.1 christos combined_entry_type *internal_ptr;
1846 1.1 christos size_t symesz;
1847 1.1 christos char *raw_src;
1848 1.1 christos char *raw_end;
1849 1.1 christos const char *string_table = NULL;
1850 1.3 christos asection * debug_sec = NULL;
1851 1.3 christos char *debug_sec_data = NULL;
1852 1.1 christos bfd_size_type size;
1853 1.1 christos
1854 1.1 christos if (obj_raw_syments (abfd) != NULL)
1855 1.1 christos return obj_raw_syments (abfd);
1856 1.1 christos
1857 1.3 christos if (! _bfd_coff_get_external_symbols (abfd))
1858 1.3 christos return NULL;
1859 1.3 christos
1860 1.7 christos size = obj_raw_syment_count (abfd);
1861 1.6 christos /* Check for integer overflow. */
1862 1.7 christos if (size > (bfd_size_type) -1 / sizeof (combined_entry_type))
1863 1.6 christos return NULL;
1864 1.7 christos size *= sizeof (combined_entry_type);
1865 1.1 christos internal = (combined_entry_type *) bfd_zalloc (abfd, size);
1866 1.1 christos if (internal == NULL && size != 0)
1867 1.1 christos return NULL;
1868 1.1 christos
1869 1.1 christos raw_src = (char *) obj_coff_external_syms (abfd);
1870 1.1 christos
1871 1.1 christos /* Mark the end of the symbols. */
1872 1.1 christos symesz = bfd_coff_symesz (abfd);
1873 1.8 christos raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz);
1874 1.1 christos
1875 1.1 christos /* FIXME SOMEDAY. A string table size of zero is very weird, but
1876 1.1 christos probably possible. If one shows up, it will probably kill us. */
1877 1.1 christos
1878 1.1 christos /* Swap all the raw entries. */
1879 1.1 christos for (internal_ptr = internal;
1880 1.1 christos raw_src < raw_end;
1881 1.1 christos raw_src += symesz, internal_ptr++)
1882 1.1 christos {
1883 1.3 christos unsigned int i;
1884 1.1 christos
1885 1.1 christos bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1886 1.1 christos (void *) & internal_ptr->u.syment);
1887 1.8 christos internal_ptr->is_sym = true;
1888 1.9 christos combined_entry_type *sym = internal_ptr;
1889 1.8 christos
1890 1.8 christos /* PR 17512: Prevent buffer overrun. */
1891 1.9 christos if (sym->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
1892 1.9 christos return NULL;
1893 1.3 christos
1894 1.9 christos for (i = 0; i < sym->u.syment.n_numaux; i++)
1895 1.1 christos {
1896 1.1 christos internal_ptr++;
1897 1.7 christos raw_src += symesz;
1898 1.7 christos
1899 1.1 christos bfd_coff_swap_aux_in (abfd, (void *) raw_src,
1900 1.9 christos sym->u.syment.n_type,
1901 1.9 christos sym->u.syment.n_sclass,
1902 1.9 christos (int) i, sym->u.syment.n_numaux,
1903 1.1 christos &(internal_ptr->u.auxent));
1904 1.3 christos
1905 1.8 christos internal_ptr->is_sym = false;
1906 1.9 christos coff_pointerize_aux (abfd, internal, sym, i, internal_ptr);
1907 1.1 christos }
1908 1.1 christos
1909 1.9 christos if (sym->u.syment.n_sclass == C_FILE
1910 1.9 christos && sym->u.syment.n_numaux > 0)
1911 1.1 christos {
1912 1.9 christos combined_entry_type * aux = sym + 1;
1913 1.3 christos
1914 1.1 christos /* Make a file symbol point to the name in the auxent, since
1915 1.1 christos the text ".file" is redundant. */
1916 1.3 christos BFD_ASSERT (! aux->is_sym);
1917 1.3 christos
1918 1.8 christos if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1919 1.1 christos {
1920 1.1 christos /* The filename is a long one, point into the string table. */
1921 1.1 christos if (string_table == NULL)
1922 1.1 christos {
1923 1.1 christos string_table = _bfd_coff_read_string_table (abfd);
1924 1.1 christos if (string_table == NULL)
1925 1.1 christos return NULL;
1926 1.1 christos }
1927 1.1 christos
1928 1.9 christos if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1929 1.3 christos >= obj_coff_strings_len (abfd))
1930 1.9 christos sym->u.syment._n._n_n._n_offset =
1931 1.8 christos (uintptr_t) _("<corrupt>");
1932 1.3 christos else
1933 1.9 christos sym->u.syment._n._n_n._n_offset =
1934 1.8 christos (uintptr_t) (string_table
1935 1.8 christos + aux->u.auxent.x_file.x_n.x_n.x_offset);
1936 1.1 christos }
1937 1.1 christos else
1938 1.1 christos {
1939 1.1 christos /* Ordinary short filename, put into memory anyway. The
1940 1.6 christos Microsoft PE tools sometimes store a filename in
1941 1.6 christos multiple AUX entries. */
1942 1.9 christos size_t len;
1943 1.9 christos char *src;
1944 1.9 christos if (sym->u.syment.n_numaux > 1 && obj_pe (abfd))
1945 1.9 christos {
1946 1.9 christos len = sym->u.syment.n_numaux * symesz;
1947 1.9 christos src = raw_src - (len - symesz);
1948 1.9 christos }
1949 1.1 christos else
1950 1.9 christos {
1951 1.9 christos len = bfd_coff_filnmlen (abfd);
1952 1.9 christos src = aux->u.auxent.x_file.x_n.x_fname;
1953 1.9 christos }
1954 1.9 christos sym->u.syment._n._n_n._n_offset =
1955 1.9 christos (uintptr_t) copy_name (abfd, src, len);
1956 1.1 christos }
1957 1.8 christos
1958 1.8 christos /* Normalize other strings available in C_FILE aux entries. */
1959 1.9 christos if (!obj_pe (abfd))
1960 1.9 christos for (int numaux = 1;
1961 1.9 christos numaux < sym->u.syment.n_numaux;
1962 1.9 christos numaux++)
1963 1.8 christos {
1964 1.9 christos aux = sym + numaux + 1;
1965 1.8 christos BFD_ASSERT (! aux->is_sym);
1966 1.8 christos
1967 1.8 christos if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0)
1968 1.8 christos {
1969 1.9 christos /* The string information is a long one, point
1970 1.9 christos into the string table. */
1971 1.8 christos if (string_table == NULL)
1972 1.8 christos {
1973 1.8 christos string_table = _bfd_coff_read_string_table (abfd);
1974 1.8 christos if (string_table == NULL)
1975 1.8 christos return NULL;
1976 1.8 christos }
1977 1.8 christos
1978 1.9 christos if ((bfd_size_type) aux->u.auxent.x_file.x_n.x_n.x_offset
1979 1.8 christos >= obj_coff_strings_len (abfd))
1980 1.8 christos aux->u.auxent.x_file.x_n.x_n.x_offset =
1981 1.8 christos (uintptr_t) _("<corrupt>");
1982 1.8 christos else
1983 1.8 christos aux->u.auxent.x_file.x_n.x_n.x_offset =
1984 1.8 christos (uintptr_t) (string_table
1985 1.9 christos + aux->u.auxent.x_file.x_n.x_n.x_offset);
1986 1.8 christos }
1987 1.8 christos else
1988 1.8 christos aux->u.auxent.x_file.x_n.x_n.x_offset =
1989 1.8 christos ((uintptr_t)
1990 1.8 christos copy_name (abfd,
1991 1.8 christos aux->u.auxent.x_file.x_n.x_fname,
1992 1.9 christos bfd_coff_filnmlen (abfd)));
1993 1.8 christos }
1994 1.8 christos
1995 1.1 christos }
1996 1.1 christos else
1997 1.1 christos {
1998 1.9 christos if (sym->u.syment._n._n_n._n_zeroes != 0)
1999 1.1 christos {
2000 1.1 christos /* This is a "short" name. Make it long. */
2001 1.1 christos char *newstring;
2002 1.1 christos
2003 1.1 christos /* Find the length of this string without walking into memory
2004 1.6 christos that isn't ours. */
2005 1.9 christos for (i = 0; i < SYMNMLEN; ++i)
2006 1.9 christos if (sym->u.syment._n._n_name[i] == '\0')
2007 1.1 christos break;
2008 1.1 christos
2009 1.9 christos newstring = bfd_alloc (abfd, i + 1);
2010 1.1 christos if (newstring == NULL)
2011 1.1 christos return NULL;
2012 1.9 christos memcpy (newstring, sym->u.syment._n._n_name, i);
2013 1.9 christos newstring[i] = 0;
2014 1.9 christos sym->u.syment._n._n_n._n_offset = (uintptr_t) newstring;
2015 1.9 christos sym->u.syment._n._n_n._n_zeroes = 0;
2016 1.9 christos }
2017 1.9 christos else if (sym->u.syment._n._n_n._n_offset == 0)
2018 1.9 christos sym->u.syment._n._n_n._n_offset = (uintptr_t) "";
2019 1.9 christos else if (!bfd_coff_symname_in_debug (abfd, &sym->u.syment))
2020 1.1 christos {
2021 1.1 christos /* Long name already. Point symbol at the string in the
2022 1.6 christos table. */
2023 1.1 christos if (string_table == NULL)
2024 1.1 christos {
2025 1.1 christos string_table = _bfd_coff_read_string_table (abfd);
2026 1.1 christos if (string_table == NULL)
2027 1.1 christos return NULL;
2028 1.1 christos }
2029 1.9 christos if (sym->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd))
2030 1.9 christos sym->u.syment._n._n_n._n_offset =
2031 1.8 christos (uintptr_t) _("<corrupt>");
2032 1.3 christos else
2033 1.9 christos sym->u.syment._n._n_n._n_offset =
2034 1.9 christos (uintptr_t) (string_table
2035 1.9 christos + sym->u.syment._n._n_n._n_offset);
2036 1.1 christos }
2037 1.1 christos else
2038 1.1 christos {
2039 1.1 christos /* Long name in debug section. Very similar. */
2040 1.3 christos if (debug_sec_data == NULL)
2041 1.3 christos {
2042 1.9 christos debug_sec_data = build_debug_section (abfd, &debug_sec);
2043 1.9 christos if (debug_sec_data == NULL)
2044 1.9 christos return NULL;
2045 1.3 christos }
2046 1.9 christos /* PR binutils/17512: Catch out of range offsets into
2047 1.9 christos the debug data. */
2048 1.9 christos if (sym->u.syment._n._n_n._n_offset >= debug_sec->size)
2049 1.9 christos sym->u.syment._n._n_n._n_offset =
2050 1.9 christos (uintptr_t) _("<corrupt>");
2051 1.3 christos else
2052 1.9 christos sym->u.syment._n._n_n._n_offset =
2053 1.9 christos (uintptr_t) (debug_sec_data
2054 1.9 christos + sym->u.syment._n._n_n._n_offset);
2055 1.1 christos }
2056 1.1 christos }
2057 1.9 christos }
2058 1.9 christos
2059 1.9 christos /* Free the raw symbols. */
2060 1.9 christos if (obj_coff_external_syms (abfd) != NULL
2061 1.9 christos && ! obj_coff_keep_syms (abfd))
2062 1.9 christos {
2063 1.9 christos free (obj_coff_external_syms (abfd));
2064 1.9 christos obj_coff_external_syms (abfd) = NULL;
2065 1.1 christos }
2066 1.1 christos
2067 1.1 christos obj_raw_syments (abfd) = internal;
2068 1.1 christos BFD_ASSERT (obj_raw_syment_count (abfd)
2069 1.9 christos == (size_t) (internal_ptr - internal));
2070 1.1 christos
2071 1.1 christos return internal;
2072 1.1 christos }
2073 1.1 christos
2074 1.1 christos long
2075 1.1 christos coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2076 1.1 christos {
2077 1.9 christos size_t count, raw;
2078 1.9 christos
2079 1.9 christos count = asect->reloc_count;
2080 1.9 christos if (count >= LONG_MAX / sizeof (arelent *)
2081 1.9 christos || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw))
2082 1.1 christos {
2083 1.9 christos bfd_set_error (bfd_error_file_too_big);
2084 1.1 christos return -1;
2085 1.1 christos }
2086 1.9 christos if (!bfd_write_p (abfd))
2087 1.7 christos {
2088 1.9 christos ufile_ptr filesize = bfd_get_file_size (abfd);
2089 1.9 christos if (filesize != 0 && raw > filesize)
2090 1.9 christos {
2091 1.9 christos bfd_set_error (bfd_error_file_truncated);
2092 1.9 christos return -1;
2093 1.9 christos }
2094 1.7 christos }
2095 1.9 christos return (count + 1) * sizeof (arelent *);
2096 1.1 christos }
2097 1.1 christos
2098 1.1 christos asymbol *
2099 1.1 christos coff_make_empty_symbol (bfd *abfd)
2100 1.1 christos {
2101 1.8 christos size_t amt = sizeof (coff_symbol_type);
2102 1.1 christos coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
2103 1.1 christos
2104 1.1 christos if (new_symbol == NULL)
2105 1.1 christos return NULL;
2106 1.1 christos new_symbol->symbol.section = 0;
2107 1.3 christos new_symbol->native = NULL;
2108 1.1 christos new_symbol->lineno = NULL;
2109 1.8 christos new_symbol->done_lineno = false;
2110 1.1 christos new_symbol->symbol.the_bfd = abfd;
2111 1.1 christos
2112 1.1 christos return & new_symbol->symbol;
2113 1.1 christos }
2114 1.1 christos
2115 1.1 christos /* Make a debugging symbol. */
2116 1.1 christos
2117 1.1 christos asymbol *
2118 1.9 christos coff_bfd_make_debug_symbol (bfd *abfd)
2119 1.1 christos {
2120 1.8 christos size_t amt = sizeof (coff_symbol_type);
2121 1.1 christos coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
2122 1.1 christos
2123 1.1 christos if (new_symbol == NULL)
2124 1.1 christos return NULL;
2125 1.1 christos /* @@ The 10 is a guess at a plausible maximum number of aux entries
2126 1.1 christos (but shouldn't be a constant). */
2127 1.1 christos amt = sizeof (combined_entry_type) * 10;
2128 1.1 christos new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2129 1.1 christos if (!new_symbol->native)
2130 1.1 christos return NULL;
2131 1.8 christos new_symbol->native->is_sym = true;
2132 1.1 christos new_symbol->symbol.section = bfd_abs_section_ptr;
2133 1.1 christos new_symbol->symbol.flags = BSF_DEBUGGING;
2134 1.1 christos new_symbol->lineno = NULL;
2135 1.8 christos new_symbol->done_lineno = false;
2136 1.1 christos new_symbol->symbol.the_bfd = abfd;
2137 1.3 christos
2138 1.1 christos return & new_symbol->symbol;
2139 1.1 christos }
2140 1.1 christos
2141 1.1 christos void
2142 1.1 christos coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
2143 1.1 christos {
2144 1.1 christos bfd_symbol_info (symbol, ret);
2145 1.1 christos
2146 1.1 christos if (coffsymbol (symbol)->native != NULL
2147 1.3 christos && coffsymbol (symbol)->native->fix_value
2148 1.3 christos && coffsymbol (symbol)->native->is_sym)
2149 1.8 christos ret->value
2150 1.8 christos = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value
2151 1.8 christos - (uintptr_t) obj_raw_syments (abfd))
2152 1.8 christos / sizeof (combined_entry_type));
2153 1.1 christos }
2154 1.1 christos
2155 1.1 christos /* Print out information about COFF symbol. */
2156 1.1 christos
2157 1.1 christos void
2158 1.1 christos coff_print_symbol (bfd *abfd,
2159 1.1 christos void * filep,
2160 1.1 christos asymbol *symbol,
2161 1.1 christos bfd_print_symbol_type how)
2162 1.1 christos {
2163 1.1 christos FILE * file = (FILE *) filep;
2164 1.1 christos
2165 1.1 christos switch (how)
2166 1.1 christos {
2167 1.1 christos case bfd_print_symbol_name:
2168 1.1 christos fprintf (file, "%s", symbol->name);
2169 1.1 christos break;
2170 1.1 christos
2171 1.1 christos case bfd_print_symbol_more:
2172 1.1 christos fprintf (file, "coff %s %s",
2173 1.1 christos coffsymbol (symbol)->native ? "n" : "g",
2174 1.1 christos coffsymbol (symbol)->lineno ? "l" : " ");
2175 1.1 christos break;
2176 1.1 christos
2177 1.1 christos case bfd_print_symbol_all:
2178 1.1 christos if (coffsymbol (symbol)->native)
2179 1.1 christos {
2180 1.1 christos bfd_vma val;
2181 1.1 christos unsigned int aux;
2182 1.1 christos combined_entry_type *combined = coffsymbol (symbol)->native;
2183 1.1 christos combined_entry_type *root = obj_raw_syments (abfd);
2184 1.1 christos struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2185 1.1 christos
2186 1.1 christos fprintf (file, "[%3ld]", (long) (combined - root));
2187 1.1 christos
2188 1.3 christos /* PR 17512: file: 079-33786-0.001:0.1. */
2189 1.3 christos if (combined < obj_raw_syments (abfd)
2190 1.3 christos || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
2191 1.3 christos {
2192 1.3 christos fprintf (file, _("<corrupt info> %s"), symbol->name);
2193 1.3 christos break;
2194 1.3 christos }
2195 1.3 christos
2196 1.3 christos BFD_ASSERT (combined->is_sym);
2197 1.1 christos if (! combined->fix_value)
2198 1.1 christos val = (bfd_vma) combined->u.syment.n_value;
2199 1.1 christos else
2200 1.8 christos val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root)
2201 1.8 christos / sizeof (combined_entry_type));
2202 1.1 christos
2203 1.8 christos fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x",
2204 1.1 christos combined->u.syment.n_scnum,
2205 1.1 christos combined->u.syment.n_flags,
2206 1.1 christos combined->u.syment.n_type,
2207 1.1 christos combined->u.syment.n_sclass,
2208 1.1 christos combined->u.syment.n_numaux);
2209 1.1 christos bfd_fprintf_vma (abfd, file, val);
2210 1.1 christos fprintf (file, " %s", symbol->name);
2211 1.1 christos
2212 1.1 christos for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2213 1.1 christos {
2214 1.1 christos combined_entry_type *auxp = combined + aux + 1;
2215 1.1 christos long tagndx;
2216 1.1 christos
2217 1.3 christos BFD_ASSERT (! auxp->is_sym);
2218 1.1 christos if (auxp->fix_tag)
2219 1.1 christos tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2220 1.1 christos else
2221 1.9 christos tagndx = auxp->u.auxent.x_sym.x_tagndx.u32;
2222 1.1 christos
2223 1.1 christos fprintf (file, "\n");
2224 1.1 christos
2225 1.1 christos if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2226 1.1 christos continue;
2227 1.1 christos
2228 1.1 christos switch (combined->u.syment.n_sclass)
2229 1.1 christos {
2230 1.1 christos case C_FILE:
2231 1.1 christos fprintf (file, "File ");
2232 1.8 christos /* Add additional information if this isn't the filename
2233 1.8 christos auxiliary entry. */
2234 1.8 christos if (auxp->u.auxent.x_file.x_ftype)
2235 1.8 christos fprintf (file, "ftype %d fname \"%s\"",
2236 1.8 christos auxp->u.auxent.x_file.x_ftype,
2237 1.8 christos (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset);
2238 1.8 christos break;
2239 1.8 christos
2240 1.8 christos case C_DWARF:
2241 1.9 christos fprintf (file, "AUX scnlen %#" PRIx64 " nreloc %" PRId64,
2242 1.9 christos auxp->u.auxent.x_sect.x_scnlen,
2243 1.8 christos auxp->u.auxent.x_sect.x_nreloc);
2244 1.1 christos break;
2245 1.1 christos
2246 1.1 christos case C_STAT:
2247 1.1 christos if (combined->u.syment.n_type == T_NULL)
2248 1.1 christos /* Probably a section symbol ? */
2249 1.1 christos {
2250 1.1 christos fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2251 1.1 christos (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
2252 1.1 christos auxp->u.auxent.x_scn.x_nreloc,
2253 1.1 christos auxp->u.auxent.x_scn.x_nlinno);
2254 1.1 christos if (auxp->u.auxent.x_scn.x_checksum != 0
2255 1.1 christos || auxp->u.auxent.x_scn.x_associated != 0
2256 1.1 christos || auxp->u.auxent.x_scn.x_comdat != 0)
2257 1.9 christos fprintf (file, " checksum 0x%x assoc %d comdat %d",
2258 1.1 christos auxp->u.auxent.x_scn.x_checksum,
2259 1.1 christos auxp->u.auxent.x_scn.x_associated,
2260 1.1 christos auxp->u.auxent.x_scn.x_comdat);
2261 1.1 christos break;
2262 1.1 christos }
2263 1.6 christos /* Fall through. */
2264 1.1 christos case C_EXT:
2265 1.1 christos case C_AIX_WEAKEXT:
2266 1.1 christos if (ISFCN (combined->u.syment.n_type))
2267 1.1 christos {
2268 1.1 christos long next, llnos;
2269 1.1 christos
2270 1.1 christos if (auxp->fix_end)
2271 1.1 christos next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2272 1.1 christos - root);
2273 1.1 christos else
2274 1.9 christos next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.u32;
2275 1.1 christos llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
2276 1.1 christos fprintf (file,
2277 1.1 christos "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2278 1.1 christos tagndx,
2279 1.1 christos (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
2280 1.1 christos llnos, next);
2281 1.1 christos break;
2282 1.1 christos }
2283 1.6 christos /* Fall through. */
2284 1.1 christos default:
2285 1.1 christos fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2286 1.1 christos auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2287 1.1 christos auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2288 1.1 christos tagndx);
2289 1.1 christos if (auxp->fix_end)
2290 1.1 christos fprintf (file, " endndx %ld",
2291 1.1 christos ((long)
2292 1.1 christos (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2293 1.1 christos - root)));
2294 1.1 christos break;
2295 1.1 christos }
2296 1.1 christos }
2297 1.1 christos
2298 1.1 christos if (l)
2299 1.1 christos {
2300 1.1 christos fprintf (file, "\n%s :", l->u.sym->name);
2301 1.1 christos l++;
2302 1.1 christos while (l->line_number)
2303 1.1 christos {
2304 1.3 christos if (l->line_number > 0)
2305 1.3 christos {
2306 1.3 christos fprintf (file, "\n%4d : ", l->line_number);
2307 1.3 christos bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
2308 1.3 christos }
2309 1.1 christos l++;
2310 1.1 christos }
2311 1.1 christos }
2312 1.1 christos }
2313 1.1 christos else
2314 1.1 christos {
2315 1.1 christos bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2316 1.1 christos fprintf (file, " %-5s %s %s %s",
2317 1.1 christos symbol->section->name,
2318 1.1 christos coffsymbol (symbol)->native ? "n" : "g",
2319 1.1 christos coffsymbol (symbol)->lineno ? "l" : " ",
2320 1.1 christos symbol->name);
2321 1.1 christos }
2322 1.1 christos }
2323 1.1 christos }
2324 1.1 christos
2325 1.1 christos /* Return whether a symbol name implies a local symbol. In COFF,
2326 1.1 christos local symbols generally start with ``.L''. Most targets use this
2327 1.1 christos function for the is_local_label_name entry point, but some may
2328 1.1 christos override it. */
2329 1.1 christos
2330 1.8 christos bool
2331 1.1 christos _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2332 1.1 christos const char *name)
2333 1.1 christos {
2334 1.1 christos return name[0] == '.' && name[1] == 'L';
2335 1.1 christos }
2336 1.1 christos
2337 1.1 christos /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2338 1.1 christos section, calculate and return the name of the source file and the line
2339 1.1 christos nearest to the wanted location. */
2340 1.1 christos
2341 1.8 christos bool
2342 1.1 christos coff_find_nearest_line_with_names (bfd *abfd,
2343 1.6 christos asymbol **symbols,
2344 1.6 christos asection *section,
2345 1.6 christos bfd_vma offset,
2346 1.6 christos const char **filename_ptr,
2347 1.6 christos const char **functionname_ptr,
2348 1.6 christos unsigned int *line_ptr,
2349 1.6 christos const struct dwarf_debug_section *debug_sections)
2350 1.1 christos {
2351 1.8 christos bool found;
2352 1.1 christos unsigned int i;
2353 1.1 christos unsigned int line_base;
2354 1.1 christos coff_data_type *cof = coff_data (abfd);
2355 1.1 christos /* Run through the raw syments if available. */
2356 1.1 christos combined_entry_type *p;
2357 1.1 christos combined_entry_type *pend;
2358 1.1 christos alent *l;
2359 1.1 christos struct coff_section_tdata *sec_data;
2360 1.8 christos size_t amt;
2361 1.1 christos
2362 1.1 christos /* Before looking through the symbol table, try to use a .stab
2363 1.1 christos section to find the information. */
2364 1.1 christos if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2365 1.1 christos &found, filename_ptr,
2366 1.1 christos functionname_ptr, line_ptr,
2367 1.1 christos &coff_data(abfd)->line_info))
2368 1.8 christos return false;
2369 1.1 christos
2370 1.1 christos if (found)
2371 1.8 christos return true;
2372 1.1 christos
2373 1.1 christos /* Also try examining DWARF2 debugging information. */
2374 1.3 christos if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
2375 1.1 christos filename_ptr, functionname_ptr,
2376 1.7 christos line_ptr, NULL, debug_sections,
2377 1.1 christos &coff_data(abfd)->dwarf2_find_line_info))
2378 1.8 christos return true;
2379 1.1 christos
2380 1.6 christos sec_data = coff_section_data (abfd, section);
2381 1.6 christos
2382 1.3 christos /* If the DWARF lookup failed, but there is DWARF information available
2383 1.3 christos then the problem might be that the file has been rebased. This tool
2384 1.3 christos changes the VMAs of all the sections, but it does not update the DWARF
2385 1.3 christos information. So try again, using a bias against the address sought. */
2386 1.3 christos if (coff_data (abfd)->dwarf2_find_line_info != NULL)
2387 1.3 christos {
2388 1.7 christos bfd_signed_vma bias = 0;
2389 1.3 christos
2390 1.6 christos /* Create a cache of the result for the next call. */
2391 1.6 christos if (sec_data == NULL && section->owner == abfd)
2392 1.6 christos {
2393 1.6 christos amt = sizeof (struct coff_section_tdata);
2394 1.6 christos section->used_by_bfd = bfd_zalloc (abfd, amt);
2395 1.6 christos sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2396 1.6 christos }
2397 1.6 christos
2398 1.6 christos if (sec_data != NULL && sec_data->saved_bias)
2399 1.8 christos bias = sec_data->bias;
2400 1.7 christos else if (symbols)
2401 1.6 christos {
2402 1.6 christos bias = _bfd_dwarf2_find_symbol_bias (symbols,
2403 1.6 christos & coff_data (abfd)->dwarf2_find_line_info);
2404 1.7 christos
2405 1.6 christos if (sec_data)
2406 1.6 christos {
2407 1.8 christos sec_data->saved_bias = true;
2408 1.6 christos sec_data->bias = bias;
2409 1.6 christos }
2410 1.6 christos }
2411 1.3 christos
2412 1.3 christos if (bias
2413 1.3 christos && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
2414 1.3 christos offset + bias,
2415 1.3 christos filename_ptr, functionname_ptr,
2416 1.7 christos line_ptr, NULL, debug_sections,
2417 1.3 christos &coff_data(abfd)->dwarf2_find_line_info))
2418 1.8 christos return true;
2419 1.3 christos }
2420 1.3 christos
2421 1.1 christos *filename_ptr = 0;
2422 1.1 christos *functionname_ptr = 0;
2423 1.1 christos *line_ptr = 0;
2424 1.1 christos
2425 1.1 christos /* Don't try and find line numbers in a non coff file. */
2426 1.1 christos if (!bfd_family_coff (abfd))
2427 1.8 christos return false;
2428 1.1 christos
2429 1.1 christos if (cof == NULL)
2430 1.8 christos return false;
2431 1.1 christos
2432 1.1 christos /* Find the first C_FILE symbol. */
2433 1.1 christos p = cof->raw_syments;
2434 1.1 christos if (!p)
2435 1.8 christos return false;
2436 1.1 christos
2437 1.1 christos pend = p + cof->raw_syment_count;
2438 1.1 christos while (p < pend)
2439 1.1 christos {
2440 1.3 christos BFD_ASSERT (p->is_sym);
2441 1.1 christos if (p->u.syment.n_sclass == C_FILE)
2442 1.1 christos break;
2443 1.1 christos p += 1 + p->u.syment.n_numaux;
2444 1.1 christos }
2445 1.1 christos
2446 1.1 christos if (p < pend)
2447 1.1 christos {
2448 1.1 christos bfd_vma sec_vma;
2449 1.1 christos bfd_vma maxdiff;
2450 1.1 christos
2451 1.1 christos /* Look through the C_FILE symbols to find the best one. */
2452 1.7 christos sec_vma = bfd_section_vma (section);
2453 1.1 christos *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2454 1.1 christos maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2455 1.1 christos while (1)
2456 1.1 christos {
2457 1.1 christos bfd_vma file_addr;
2458 1.1 christos combined_entry_type *p2;
2459 1.1 christos
2460 1.1 christos for (p2 = p + 1 + p->u.syment.n_numaux;
2461 1.1 christos p2 < pend;
2462 1.1 christos p2 += 1 + p2->u.syment.n_numaux)
2463 1.1 christos {
2464 1.3 christos BFD_ASSERT (p2->is_sym);
2465 1.1 christos if (p2->u.syment.n_scnum > 0
2466 1.1 christos && (section
2467 1.1 christos == coff_section_from_bfd_index (abfd,
2468 1.1 christos p2->u.syment.n_scnum)))
2469 1.1 christos break;
2470 1.1 christos if (p2->u.syment.n_sclass == C_FILE)
2471 1.1 christos {
2472 1.1 christos p2 = pend;
2473 1.1 christos break;
2474 1.1 christos }
2475 1.1 christos }
2476 1.3 christos if (p2 >= pend)
2477 1.3 christos break;
2478 1.1 christos
2479 1.1 christos file_addr = (bfd_vma) p2->u.syment.n_value;
2480 1.1 christos /* PR 11512: Include the section address of the function name symbol. */
2481 1.1 christos if (p2->u.syment.n_scnum > 0)
2482 1.1 christos file_addr += coff_section_from_bfd_index (abfd,
2483 1.1 christos p2->u.syment.n_scnum)->vma;
2484 1.1 christos /* We use <= MAXDIFF here so that if we get a zero length
2485 1.6 christos file, we actually use the next file entry. */
2486 1.1 christos if (p2 < pend
2487 1.1 christos && offset + sec_vma >= file_addr
2488 1.1 christos && offset + sec_vma - file_addr <= maxdiff)
2489 1.1 christos {
2490 1.1 christos *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2491 1.1 christos maxdiff = offset + sec_vma - p2->u.syment.n_value;
2492 1.1 christos }
2493 1.1 christos
2494 1.7 christos if (p->u.syment.n_value >= cof->raw_syment_count)
2495 1.7 christos break;
2496 1.7 christos
2497 1.1 christos /* Avoid endless loops on erroneous files by ensuring that
2498 1.1 christos we always move forward in the file. */
2499 1.1 christos if (p >= cof->raw_syments + p->u.syment.n_value)
2500 1.1 christos break;
2501 1.1 christos
2502 1.1 christos p = cof->raw_syments + p->u.syment.n_value;
2503 1.7 christos if (!p->is_sym || p->u.syment.n_sclass != C_FILE)
2504 1.1 christos break;
2505 1.1 christos }
2506 1.1 christos }
2507 1.1 christos
2508 1.6 christos if (section->lineno_count == 0)
2509 1.6 christos {
2510 1.6 christos *functionname_ptr = NULL;
2511 1.6 christos *line_ptr = 0;
2512 1.8 christos return true;
2513 1.6 christos }
2514 1.6 christos
2515 1.6 christos /* Now wander though the raw linenumbers of the section.
2516 1.6 christos If we have been called on this section before, and the offset
2517 1.6 christos we want is further down then we can prime the lookup loop. */
2518 1.1 christos if (sec_data != NULL
2519 1.1 christos && sec_data->i > 0
2520 1.1 christos && offset >= sec_data->offset)
2521 1.1 christos {
2522 1.1 christos i = sec_data->i;
2523 1.1 christos *functionname_ptr = sec_data->function;
2524 1.1 christos line_base = sec_data->line_base;
2525 1.1 christos }
2526 1.1 christos else
2527 1.1 christos {
2528 1.1 christos i = 0;
2529 1.1 christos line_base = 0;
2530 1.1 christos }
2531 1.1 christos
2532 1.1 christos if (section->lineno != NULL)
2533 1.1 christos {
2534 1.1 christos bfd_vma last_value = 0;
2535 1.1 christos
2536 1.1 christos l = §ion->lineno[i];
2537 1.1 christos
2538 1.1 christos for (; i < section->lineno_count; i++)
2539 1.1 christos {
2540 1.1 christos if (l->line_number == 0)
2541 1.1 christos {
2542 1.1 christos /* Get the symbol this line number points at. */
2543 1.1 christos coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2544 1.1 christos if (coff->symbol.value > offset)
2545 1.1 christos break;
2546 1.6 christos
2547 1.1 christos *functionname_ptr = coff->symbol.name;
2548 1.1 christos last_value = coff->symbol.value;
2549 1.1 christos if (coff->native)
2550 1.1 christos {
2551 1.1 christos combined_entry_type *s = coff->native;
2552 1.3 christos
2553 1.3 christos BFD_ASSERT (s->is_sym);
2554 1.1 christos s = s + 1 + s->u.syment.n_numaux;
2555 1.1 christos
2556 1.1 christos /* In XCOFF a debugging symbol can follow the
2557 1.1 christos function symbol. */
2558 1.8 christos if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2559 1.8 christos < obj_raw_syment_count (abfd) * sizeof (*s))
2560 1.8 christos && s->u.syment.n_scnum == N_DEBUG)
2561 1.1 christos s = s + 1 + s->u.syment.n_numaux;
2562 1.1 christos
2563 1.1 christos /* S should now point to the .bf of the function. */
2564 1.8 christos if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
2565 1.8 christos < obj_raw_syment_count (abfd) * sizeof (*s))
2566 1.8 christos && s->u.syment.n_numaux)
2567 1.1 christos {
2568 1.1 christos /* The linenumber is stored in the auxent. */
2569 1.1 christos union internal_auxent *a = &((s + 1)->u.auxent);
2570 1.3 christos
2571 1.1 christos line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2572 1.1 christos *line_ptr = line_base;
2573 1.1 christos }
2574 1.1 christos }
2575 1.1 christos }
2576 1.1 christos else
2577 1.1 christos {
2578 1.1 christos if (l->u.offset > offset)
2579 1.1 christos break;
2580 1.1 christos *line_ptr = l->line_number + line_base - 1;
2581 1.1 christos }
2582 1.1 christos l++;
2583 1.1 christos }
2584 1.1 christos
2585 1.1 christos /* If we fell off the end of the loop, then assume that this
2586 1.1 christos symbol has no line number info. Otherwise, symbols with no
2587 1.1 christos line number info get reported with the line number of the
2588 1.1 christos last line of the last symbol which does have line number
2589 1.1 christos info. We use 0x100 as a slop to account for cases where the
2590 1.1 christos last line has executable code. */
2591 1.1 christos if (i >= section->lineno_count
2592 1.1 christos && last_value != 0
2593 1.1 christos && offset - last_value > 0x100)
2594 1.1 christos {
2595 1.1 christos *functionname_ptr = NULL;
2596 1.1 christos *line_ptr = 0;
2597 1.1 christos }
2598 1.1 christos }
2599 1.1 christos
2600 1.1 christos /* Cache the results for the next call. */
2601 1.1 christos if (sec_data == NULL && section->owner == abfd)
2602 1.1 christos {
2603 1.1 christos amt = sizeof (struct coff_section_tdata);
2604 1.1 christos section->used_by_bfd = bfd_zalloc (abfd, amt);
2605 1.1 christos sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2606 1.1 christos }
2607 1.6 christos
2608 1.1 christos if (sec_data != NULL)
2609 1.1 christos {
2610 1.1 christos sec_data->offset = offset;
2611 1.1 christos sec_data->i = i - 1;
2612 1.1 christos sec_data->function = *functionname_ptr;
2613 1.1 christos sec_data->line_base = line_base;
2614 1.1 christos }
2615 1.1 christos
2616 1.8 christos return true;
2617 1.1 christos }
2618 1.1 christos
2619 1.8 christos bool
2620 1.1 christos coff_find_nearest_line (bfd *abfd,
2621 1.3 christos asymbol **symbols,
2622 1.1 christos asection *section,
2623 1.1 christos bfd_vma offset,
2624 1.1 christos const char **filename_ptr,
2625 1.1 christos const char **functionname_ptr,
2626 1.3 christos unsigned int *line_ptr,
2627 1.3 christos unsigned int *discriminator_ptr)
2628 1.1 christos {
2629 1.3 christos if (discriminator_ptr)
2630 1.3 christos *discriminator_ptr = 0;
2631 1.3 christos return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
2632 1.6 christos filename_ptr, functionname_ptr,
2633 1.6 christos line_ptr, dwarf_debug_sections);
2634 1.1 christos }
2635 1.1 christos
2636 1.8 christos bool
2637 1.1 christos coff_find_inliner_info (bfd *abfd,
2638 1.1 christos const char **filename_ptr,
2639 1.1 christos const char **functionname_ptr,
2640 1.1 christos unsigned int *line_ptr)
2641 1.1 christos {
2642 1.8 christos bool found;
2643 1.1 christos
2644 1.1 christos found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2645 1.1 christos functionname_ptr, line_ptr,
2646 1.1 christos &coff_data(abfd)->dwarf2_find_line_info);
2647 1.1 christos return (found);
2648 1.1 christos }
2649 1.1 christos
2650 1.1 christos int
2651 1.1 christos coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
2652 1.1 christos {
2653 1.1 christos size_t size;
2654 1.1 christos
2655 1.3 christos if (!bfd_link_relocatable (info))
2656 1.1 christos size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
2657 1.1 christos else
2658 1.1 christos size = bfd_coff_filhsz (abfd);
2659 1.1 christos
2660 1.1 christos size += abfd->section_count * bfd_coff_scnhsz (abfd);
2661 1.1 christos return size;
2662 1.1 christos }
2663 1.1 christos
2664 1.1 christos /* Change the class of a coff symbol held by BFD. */
2665 1.1 christos
2666 1.8 christos bool
2667 1.6 christos bfd_coff_set_symbol_class (bfd * abfd,
2668 1.6 christos asymbol * symbol,
2669 1.6 christos unsigned int symbol_class)
2670 1.1 christos {
2671 1.1 christos coff_symbol_type * csym;
2672 1.1 christos
2673 1.3 christos csym = coff_symbol_from (symbol);
2674 1.1 christos if (csym == NULL)
2675 1.1 christos {
2676 1.1 christos bfd_set_error (bfd_error_invalid_operation);
2677 1.8 christos return false;
2678 1.1 christos }
2679 1.1 christos else if (csym->native == NULL)
2680 1.1 christos {
2681 1.1 christos /* This is an alien symbol which no native coff backend data.
2682 1.1 christos We cheat here by creating a fake native entry for it and
2683 1.1 christos then filling in the class. This code is based on that in
2684 1.1 christos coff_write_alien_symbol(). */
2685 1.1 christos
2686 1.1 christos combined_entry_type * native;
2687 1.8 christos size_t amt = sizeof (* native);
2688 1.1 christos
2689 1.1 christos native = (combined_entry_type *) bfd_zalloc (abfd, amt);
2690 1.1 christos if (native == NULL)
2691 1.8 christos return false;
2692 1.1 christos
2693 1.8 christos native->is_sym = true;
2694 1.1 christos native->u.syment.n_type = T_NULL;
2695 1.1 christos native->u.syment.n_sclass = symbol_class;
2696 1.1 christos
2697 1.1 christos if (bfd_is_und_section (symbol->section))
2698 1.1 christos {
2699 1.1 christos native->u.syment.n_scnum = N_UNDEF;
2700 1.1 christos native->u.syment.n_value = symbol->value;
2701 1.1 christos }
2702 1.1 christos else if (bfd_is_com_section (symbol->section))
2703 1.1 christos {
2704 1.1 christos native->u.syment.n_scnum = N_UNDEF;
2705 1.1 christos native->u.syment.n_value = symbol->value;
2706 1.1 christos }
2707 1.1 christos else
2708 1.1 christos {
2709 1.1 christos native->u.syment.n_scnum =
2710 1.1 christos symbol->section->output_section->target_index;
2711 1.1 christos native->u.syment.n_value = (symbol->value
2712 1.1 christos + symbol->section->output_offset);
2713 1.1 christos if (! obj_pe (abfd))
2714 1.1 christos native->u.syment.n_value += symbol->section->output_section->vma;
2715 1.1 christos
2716 1.1 christos /* Copy the any flags from the file header into the symbol.
2717 1.1 christos FIXME: Why? */
2718 1.1 christos native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2719 1.1 christos }
2720 1.1 christos
2721 1.1 christos csym->native = native;
2722 1.1 christos }
2723 1.1 christos else
2724 1.1 christos csym->native->u.syment.n_sclass = symbol_class;
2725 1.1 christos
2726 1.8 christos return true;
2727 1.1 christos }
2728 1.1 christos
2729 1.8 christos bool
2730 1.1 christos _bfd_coff_section_already_linked (bfd *abfd,
2731 1.1 christos asection *sec,
2732 1.1 christos struct bfd_link_info *info)
2733 1.1 christos {
2734 1.1 christos flagword flags;
2735 1.1 christos const char *name, *key;
2736 1.1 christos struct bfd_section_already_linked *l;
2737 1.1 christos struct bfd_section_already_linked_hash_entry *already_linked_list;
2738 1.1 christos struct coff_comdat_info *s_comdat;
2739 1.1 christos
2740 1.7 christos if (sec->output_section == bfd_abs_section_ptr)
2741 1.8 christos return false;
2742 1.7 christos
2743 1.1 christos flags = sec->flags;
2744 1.1 christos if ((flags & SEC_LINK_ONCE) == 0)
2745 1.8 christos return false;
2746 1.1 christos
2747 1.1 christos /* The COFF backend linker doesn't support group sections. */
2748 1.1 christos if ((flags & SEC_GROUP) != 0)
2749 1.8 christos return false;
2750 1.1 christos
2751 1.7 christos name = bfd_section_name (sec);
2752 1.1 christos s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2753 1.1 christos
2754 1.1 christos if (s_comdat != NULL)
2755 1.1 christos key = s_comdat->name;
2756 1.1 christos else
2757 1.1 christos {
2758 1.8 christos if (startswith (name, ".gnu.linkonce.")
2759 1.1 christos && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2760 1.1 christos key++;
2761 1.1 christos else
2762 1.1 christos /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2763 1.1 christos .xdata$<key> and .pdata$<key> only the first of which has a
2764 1.1 christos comdat key. Should these all match the LTO IR key? */
2765 1.1 christos key = name;
2766 1.1 christos }
2767 1.1 christos
2768 1.1 christos already_linked_list = bfd_section_already_linked_table_lookup (key);
2769 1.1 christos
2770 1.1 christos for (l = already_linked_list->entry; l != NULL; l = l->next)
2771 1.1 christos {
2772 1.1 christos struct coff_comdat_info *l_comdat;
2773 1.1 christos
2774 1.1 christos l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2775 1.1 christos
2776 1.1 christos /* The section names must match, and both sections must be
2777 1.1 christos comdat and have the same comdat name, or both sections must
2778 1.1 christos be non-comdat. LTO IR plugin sections are an exception. They
2779 1.1 christos are always named .gnu.linkonce.t.<key> (<key> is some string)
2780 1.1 christos and match any comdat section with comdat name of <key>, and
2781 1.1 christos any linkonce section with the same suffix, ie.
2782 1.1 christos .gnu.linkonce.*.<key>. */
2783 1.1 christos if (((s_comdat != NULL) == (l_comdat != NULL)
2784 1.1 christos && strcmp (name, l->sec->name) == 0)
2785 1.8 christos || (l->sec->owner->flags & BFD_PLUGIN) != 0
2786 1.8 christos || (sec->owner->flags & BFD_PLUGIN) != 0)
2787 1.1 christos {
2788 1.1 christos /* The section has already been linked. See if we should
2789 1.1 christos issue a warning. */
2790 1.1 christos return _bfd_handle_already_linked (sec, l, info);
2791 1.1 christos }
2792 1.1 christos }
2793 1.1 christos
2794 1.1 christos /* This is the first section with this name. Record it. */
2795 1.1 christos if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2796 1.1 christos info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2797 1.8 christos return false;
2798 1.1 christos }
2799 1.3 christos
2800 1.3 christos /* Initialize COOKIE for input bfd ABFD. */
2801 1.3 christos
2802 1.8 christos static bool
2803 1.3 christos init_reloc_cookie (struct coff_reloc_cookie *cookie,
2804 1.3 christos struct bfd_link_info *info ATTRIBUTE_UNUSED,
2805 1.3 christos bfd *abfd)
2806 1.3 christos {
2807 1.3 christos /* Sometimes the symbol table does not yet have been loaded here. */
2808 1.3 christos bfd_coff_slurp_symbol_table (abfd);
2809 1.3 christos
2810 1.3 christos cookie->abfd = abfd;
2811 1.3 christos cookie->sym_hashes = obj_coff_sym_hashes (abfd);
2812 1.3 christos
2813 1.3 christos cookie->symbols = obj_symbols (abfd);
2814 1.3 christos
2815 1.8 christos return true;
2816 1.3 christos }
2817 1.3 christos
2818 1.3 christos /* Free the memory allocated by init_reloc_cookie, if appropriate. */
2819 1.3 christos
2820 1.3 christos static void
2821 1.3 christos fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
2822 1.3 christos bfd *abfd ATTRIBUTE_UNUSED)
2823 1.3 christos {
2824 1.3 christos /* Nothing to do. */
2825 1.3 christos }
2826 1.3 christos
2827 1.3 christos /* Initialize the relocation information in COOKIE for input section SEC
2828 1.3 christos of input bfd ABFD. */
2829 1.3 christos
2830 1.8 christos static bool
2831 1.3 christos init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2832 1.3 christos struct bfd_link_info *info ATTRIBUTE_UNUSED,
2833 1.3 christos bfd *abfd,
2834 1.3 christos asection *sec)
2835 1.3 christos {
2836 1.3 christos if (sec->reloc_count == 0)
2837 1.3 christos {
2838 1.3 christos cookie->rels = NULL;
2839 1.3 christos cookie->relend = NULL;
2840 1.3 christos cookie->rel = NULL;
2841 1.8 christos return true;
2842 1.3 christos }
2843 1.3 christos
2844 1.8 christos cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL,
2845 1.8 christos 0, NULL);
2846 1.3 christos
2847 1.3 christos if (cookie->rels == NULL)
2848 1.8 christos return false;
2849 1.3 christos
2850 1.3 christos cookie->rel = cookie->rels;
2851 1.3 christos cookie->relend = (cookie->rels + sec->reloc_count);
2852 1.8 christos return true;
2853 1.3 christos }
2854 1.3 christos
2855 1.3 christos /* Free the memory allocated by init_reloc_cookie_rels,
2856 1.3 christos if appropriate. */
2857 1.3 christos
2858 1.3 christos static void
2859 1.3 christos fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
2860 1.3 christos asection *sec)
2861 1.3 christos {
2862 1.5 christos if (cookie->rels
2863 1.5 christos /* PR 20401. The relocs may not have been cached, so check first.
2864 1.5 christos If the relocs were loaded by init_reloc_cookie_rels() then this
2865 1.5 christos will be the case. FIXME: Would performance be improved if the
2866 1.5 christos relocs *were* cached ? */
2867 1.5 christos && coff_section_data (NULL, sec)
2868 1.5 christos && coff_section_data (NULL, sec)->relocs != cookie->rels)
2869 1.3 christos free (cookie->rels);
2870 1.3 christos }
2871 1.3 christos
2872 1.3 christos /* Initialize the whole of COOKIE for input section SEC. */
2873 1.3 christos
2874 1.8 christos static bool
2875 1.3 christos init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2876 1.3 christos struct bfd_link_info *info,
2877 1.3 christos asection *sec)
2878 1.3 christos {
2879 1.3 christos if (!init_reloc_cookie (cookie, info, sec->owner))
2880 1.8 christos return false;
2881 1.3 christos
2882 1.3 christos if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
2883 1.3 christos {
2884 1.3 christos fini_reloc_cookie (cookie, sec->owner);
2885 1.8 christos return false;
2886 1.3 christos }
2887 1.8 christos return true;
2888 1.3 christos }
2889 1.3 christos
2890 1.3 christos /* Free the memory allocated by init_reloc_cookie_for_section,
2891 1.3 christos if appropriate. */
2892 1.3 christos
2893 1.3 christos static void
2894 1.3 christos fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
2895 1.3 christos asection *sec)
2896 1.3 christos {
2897 1.3 christos fini_reloc_cookie_rels (cookie, sec);
2898 1.3 christos fini_reloc_cookie (cookie, sec->owner);
2899 1.3 christos }
2900 1.3 christos
2901 1.3 christos static asection *
2902 1.3 christos _bfd_coff_gc_mark_hook (asection *sec,
2903 1.3 christos struct bfd_link_info *info ATTRIBUTE_UNUSED,
2904 1.3 christos struct internal_reloc *rel ATTRIBUTE_UNUSED,
2905 1.3 christos struct coff_link_hash_entry *h,
2906 1.3 christos struct internal_syment *sym)
2907 1.3 christos {
2908 1.3 christos if (h != NULL)
2909 1.3 christos {
2910 1.3 christos switch (h->root.type)
2911 1.6 christos {
2912 1.6 christos case bfd_link_hash_defined:
2913 1.6 christos case bfd_link_hash_defweak:
2914 1.6 christos return h->root.u.def.section;
2915 1.6 christos
2916 1.6 christos case bfd_link_hash_common:
2917 1.6 christos return h->root.u.c.p->section;
2918 1.6 christos
2919 1.6 christos case bfd_link_hash_undefweak:
2920 1.6 christos if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
2921 1.6 christos {
2922 1.6 christos /* PE weak externals. A weak symbol may include an auxiliary
2923 1.6 christos record indicating that if the weak symbol is not resolved,
2924 1.6 christos another external symbol is used instead. */
2925 1.6 christos struct coff_link_hash_entry *h2 =
2926 1.9 christos h->auxbfd->tdata.coff_obj_data->sym_hashes
2927 1.9 christos [h->aux->x_sym.x_tagndx.u32];
2928 1.3 christos
2929 1.6 christos if (h2 && h2->root.type != bfd_link_hash_undefined)
2930 1.6 christos return h2->root.u.def.section;
2931 1.6 christos }
2932 1.6 christos break;
2933 1.3 christos
2934 1.3 christos case bfd_link_hash_undefined:
2935 1.6 christos default:
2936 1.6 christos break;
2937 1.6 christos }
2938 1.3 christos return NULL;
2939 1.3 christos }
2940 1.3 christos
2941 1.3 christos return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
2942 1.3 christos }
2943 1.3 christos
2944 1.3 christos /* COOKIE->rel describes a relocation against section SEC, which is
2945 1.3 christos a section we've decided to keep. Return the section that contains
2946 1.3 christos the relocation symbol, or NULL if no section contains it. */
2947 1.3 christos
2948 1.3 christos static asection *
2949 1.3 christos _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
2950 1.3 christos coff_gc_mark_hook_fn gc_mark_hook,
2951 1.3 christos struct coff_reloc_cookie *cookie)
2952 1.3 christos {
2953 1.3 christos struct coff_link_hash_entry *h;
2954 1.3 christos
2955 1.3 christos h = cookie->sym_hashes[cookie->rel->r_symndx];
2956 1.3 christos if (h != NULL)
2957 1.3 christos {
2958 1.3 christos while (h->root.type == bfd_link_hash_indirect
2959 1.3 christos || h->root.type == bfd_link_hash_warning)
2960 1.3 christos h = (struct coff_link_hash_entry *) h->root.u.i.link;
2961 1.3 christos
2962 1.3 christos return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
2963 1.3 christos }
2964 1.3 christos
2965 1.3 christos return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
2966 1.3 christos &(cookie->symbols
2967 1.3 christos + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
2968 1.3 christos }
2969 1.3 christos
2970 1.8 christos static bool _bfd_coff_gc_mark
2971 1.3 christos (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
2972 1.3 christos
2973 1.3 christos /* COOKIE->rel describes a relocation against section SEC, which is
2974 1.3 christos a section we've decided to keep. Mark the section that contains
2975 1.3 christos the relocation symbol. */
2976 1.3 christos
2977 1.8 christos static bool
2978 1.3 christos _bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
2979 1.3 christos asection *sec,
2980 1.3 christos coff_gc_mark_hook_fn gc_mark_hook,
2981 1.3 christos struct coff_reloc_cookie *cookie)
2982 1.3 christos {
2983 1.3 christos asection *rsec;
2984 1.3 christos
2985 1.3 christos rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
2986 1.3 christos if (rsec && !rsec->gc_mark)
2987 1.3 christos {
2988 1.3 christos if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
2989 1.3 christos rsec->gc_mark = 1;
2990 1.3 christos else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
2991 1.8 christos return false;
2992 1.3 christos }
2993 1.8 christos return true;
2994 1.3 christos }
2995 1.3 christos
2996 1.3 christos /* The mark phase of garbage collection. For a given section, mark
2997 1.3 christos it and any sections in this section's group, and all the sections
2998 1.3 christos which define symbols to which it refers. */
2999 1.3 christos
3000 1.8 christos static bool
3001 1.3 christos _bfd_coff_gc_mark (struct bfd_link_info *info,
3002 1.3 christos asection *sec,
3003 1.3 christos coff_gc_mark_hook_fn gc_mark_hook)
3004 1.3 christos {
3005 1.8 christos bool ret = true;
3006 1.3 christos
3007 1.3 christos sec->gc_mark = 1;
3008 1.3 christos
3009 1.3 christos /* Look through the section relocs. */
3010 1.3 christos if ((sec->flags & SEC_RELOC) != 0
3011 1.3 christos && sec->reloc_count > 0)
3012 1.3 christos {
3013 1.3 christos struct coff_reloc_cookie cookie;
3014 1.3 christos
3015 1.3 christos if (!init_reloc_cookie_for_section (&cookie, info, sec))
3016 1.8 christos ret = false;
3017 1.3 christos else
3018 1.6 christos {
3019 1.6 christos for (; cookie.rel < cookie.relend; cookie.rel++)
3020 1.6 christos {
3021 1.3 christos if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
3022 1.3 christos {
3023 1.8 christos ret = false;
3024 1.3 christos break;
3025 1.3 christos }
3026 1.3 christos }
3027 1.6 christos fini_reloc_cookie_for_section (&cookie, sec);
3028 1.6 christos }
3029 1.3 christos }
3030 1.3 christos
3031 1.3 christos return ret;
3032 1.3 christos }
3033 1.3 christos
3034 1.8 christos static bool
3035 1.3 christos _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
3036 1.3 christos coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
3037 1.3 christos {
3038 1.3 christos bfd *ibfd;
3039 1.3 christos
3040 1.3 christos for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3041 1.3 christos {
3042 1.3 christos asection *isec;
3043 1.8 christos bool some_kept;
3044 1.3 christos
3045 1.6 christos if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour)
3046 1.3 christos continue;
3047 1.3 christos
3048 1.3 christos /* Ensure all linker created sections are kept, and see whether
3049 1.3 christos any other section is already marked. */
3050 1.8 christos some_kept = false;
3051 1.3 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3052 1.3 christos {
3053 1.3 christos if ((isec->flags & SEC_LINKER_CREATED) != 0)
3054 1.3 christos isec->gc_mark = 1;
3055 1.3 christos else if (isec->gc_mark)
3056 1.8 christos some_kept = true;
3057 1.3 christos }
3058 1.3 christos
3059 1.3 christos /* If no section in this file will be kept, then we can
3060 1.3 christos toss out debug sections. */
3061 1.3 christos if (!some_kept)
3062 1.3 christos continue;
3063 1.3 christos
3064 1.3 christos /* Keep debug and special sections like .comment when they are
3065 1.3 christos not part of a group, or when we have single-member groups. */
3066 1.3 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next)
3067 1.3 christos if ((isec->flags & SEC_DEBUGGING) != 0
3068 1.3 christos || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3069 1.3 christos isec->gc_mark = 1;
3070 1.3 christos }
3071 1.8 christos return true;
3072 1.3 christos }
3073 1.3 christos
3074 1.3 christos /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */
3075 1.3 christos
3076 1.8 christos static bool
3077 1.3 christos coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
3078 1.3 christos void *data ATTRIBUTE_UNUSED)
3079 1.3 christos {
3080 1.3 christos if (h->root.type == bfd_link_hash_warning)
3081 1.3 christos h = (struct coff_link_hash_entry *) h->root.u.i.link;
3082 1.3 christos
3083 1.3 christos if ((h->root.type == bfd_link_hash_defined
3084 1.3 christos || h->root.type == bfd_link_hash_defweak)
3085 1.3 christos && !h->root.u.def.section->gc_mark
3086 1.3 christos && !(h->root.u.def.section->owner->flags & DYNAMIC))
3087 1.3 christos {
3088 1.3 christos /* Do our best to hide the symbol. */
3089 1.3 christos h->root.u.def.section = bfd_und_section_ptr;
3090 1.3 christos h->symbol_class = C_HIDDEN;
3091 1.3 christos }
3092 1.3 christos
3093 1.8 christos return true;
3094 1.3 christos }
3095 1.3 christos
3096 1.3 christos /* The sweep phase of garbage collection. Remove all garbage sections. */
3097 1.3 christos
3098 1.8 christos typedef bool (*gc_sweep_hook_fn)
3099 1.3 christos (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
3100 1.3 christos
3101 1.8 christos static bool
3102 1.3 christos coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3103 1.3 christos {
3104 1.3 christos bfd *sub;
3105 1.3 christos
3106 1.3 christos for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3107 1.3 christos {
3108 1.3 christos asection *o;
3109 1.3 christos
3110 1.3 christos if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3111 1.3 christos continue;
3112 1.3 christos
3113 1.3 christos for (o = sub->sections; o != NULL; o = o->next)
3114 1.3 christos {
3115 1.3 christos /* Keep debug and special sections. */
3116 1.6 christos if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
3117 1.3 christos || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
3118 1.3 christos o->gc_mark = 1;
3119 1.8 christos else if (startswith (o->name, ".idata")
3120 1.8 christos || startswith (o->name, ".pdata")
3121 1.8 christos || startswith (o->name, ".xdata")
3122 1.8 christos || startswith (o->name, ".rsrc"))
3123 1.3 christos o->gc_mark = 1;
3124 1.3 christos
3125 1.3 christos if (o->gc_mark)
3126 1.3 christos continue;
3127 1.3 christos
3128 1.3 christos /* Skip sweeping sections already excluded. */
3129 1.3 christos if (o->flags & SEC_EXCLUDE)
3130 1.3 christos continue;
3131 1.3 christos
3132 1.3 christos /* Since this is early in the link process, it is simple
3133 1.3 christos to remove a section from the output. */
3134 1.3 christos o->flags |= SEC_EXCLUDE;
3135 1.3 christos
3136 1.3 christos if (info->print_gc_sections && o->size != 0)
3137 1.6 christos /* xgettext: c-format */
3138 1.6 christos _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
3139 1.6 christos o, sub);
3140 1.3 christos
3141 1.3 christos #if 0
3142 1.3 christos /* But we also have to update some of the relocation
3143 1.3 christos info we collected before. */
3144 1.3 christos if (gc_sweep_hook
3145 1.3 christos && (o->flags & SEC_RELOC) != 0
3146 1.3 christos && o->reloc_count > 0
3147 1.3 christos && !bfd_is_abs_section (o->output_section))
3148 1.3 christos {
3149 1.3 christos struct internal_reloc *internal_relocs;
3150 1.8 christos bool r;
3151 1.3 christos
3152 1.3 christos internal_relocs
3153 1.3 christos = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
3154 1.3 christos info->keep_memory);
3155 1.3 christos if (internal_relocs == NULL)
3156 1.8 christos return false;
3157 1.3 christos
3158 1.3 christos r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
3159 1.3 christos
3160 1.3 christos if (coff_section_data (o)->relocs != internal_relocs)
3161 1.3 christos free (internal_relocs);
3162 1.3 christos
3163 1.3 christos if (!r)
3164 1.8 christos return false;
3165 1.3 christos }
3166 1.3 christos #endif
3167 1.3 christos }
3168 1.3 christos }
3169 1.3 christos
3170 1.3 christos /* Remove the symbols that were in the swept sections from the dynamic
3171 1.3 christos symbol table. */
3172 1.3 christos coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
3173 1.3 christos NULL);
3174 1.3 christos
3175 1.8 christos return true;
3176 1.3 christos }
3177 1.3 christos
3178 1.3 christos /* Keep all sections containing symbols undefined on the command-line,
3179 1.3 christos and the section containing the entry symbol. */
3180 1.3 christos
3181 1.3 christos static void
3182 1.3 christos _bfd_coff_gc_keep (struct bfd_link_info *info)
3183 1.3 christos {
3184 1.3 christos struct bfd_sym_chain *sym;
3185 1.3 christos
3186 1.3 christos for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
3187 1.3 christos {
3188 1.3 christos struct coff_link_hash_entry *h;
3189 1.3 christos
3190 1.3 christos h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
3191 1.8 christos false, false, false);
3192 1.3 christos
3193 1.3 christos if (h != NULL
3194 1.3 christos && (h->root.type == bfd_link_hash_defined
3195 1.3 christos || h->root.type == bfd_link_hash_defweak)
3196 1.3 christos && !bfd_is_abs_section (h->root.u.def.section))
3197 1.3 christos h->root.u.def.section->flags |= SEC_KEEP;
3198 1.3 christos }
3199 1.3 christos }
3200 1.3 christos
3201 1.3 christos /* Do mark and sweep of unused sections. */
3202 1.3 christos
3203 1.8 christos bool
3204 1.3 christos bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
3205 1.3 christos {
3206 1.3 christos bfd *sub;
3207 1.3 christos
3208 1.3 christos /* FIXME: Should we implement this? */
3209 1.3 christos #if 0
3210 1.3 christos const bfd_coff_backend_data *bed = coff_backend_info (abfd);
3211 1.3 christos
3212 1.3 christos if (!bed->can_gc_sections
3213 1.3 christos || !is_coff_hash_table (info->hash))
3214 1.3 christos {
3215 1.6 christos _bfd_error_handler(_("warning: gc-sections option ignored"));
3216 1.8 christos return true;
3217 1.3 christos }
3218 1.3 christos #endif
3219 1.3 christos
3220 1.3 christos _bfd_coff_gc_keep (info);
3221 1.3 christos
3222 1.3 christos /* Grovel through relocs to find out who stays ... */
3223 1.3 christos for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3224 1.3 christos {
3225 1.3 christos asection *o;
3226 1.3 christos
3227 1.3 christos if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
3228 1.6 christos continue;
3229 1.3 christos
3230 1.3 christos for (o = sub->sections; o != NULL; o = o->next)
3231 1.6 christos {
3232 1.3 christos if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
3233 1.8 christos || startswith (o->name, ".vectors")
3234 1.8 christos || startswith (o->name, ".ctors")
3235 1.8 christos || startswith (o->name, ".dtors"))
3236 1.3 christos && !o->gc_mark)
3237 1.3 christos {
3238 1.3 christos if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
3239 1.8 christos return false;
3240 1.3 christos }
3241 1.6 christos }
3242 1.3 christos }
3243 1.3 christos
3244 1.3 christos /* Allow the backend to mark additional target specific sections. */
3245 1.3 christos _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
3246 1.3 christos
3247 1.3 christos /* ... and mark SEC_EXCLUDE for those that go. */
3248 1.3 christos return coff_gc_sweep (abfd, info);
3249 1.3 christos }
3250 1.7 christos
3251 1.7 christos /* Return name used to identify a comdat group. */
3252 1.7 christos
3253 1.7 christos const char *
3254 1.7 christos bfd_coff_group_name (bfd *abfd, const asection *sec)
3255 1.7 christos {
3256 1.7 christos struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec);
3257 1.7 christos if (ci != NULL)
3258 1.7 christos return ci->name;
3259 1.7 christos return NULL;
3260 1.7 christos }
3261 1.7 christos
3262 1.8 christos bool
3263 1.9 christos _bfd_coff_free_cached_info (bfd *abfd)
3264 1.7 christos {
3265 1.9 christos struct coff_tdata *tdata;
3266 1.8 christos
3267 1.9 christos if (bfd_family_coff (abfd)
3268 1.9 christos && (bfd_get_format (abfd) == bfd_object
3269 1.9 christos || bfd_get_format (abfd) == bfd_core)
3270 1.9 christos && (tdata = coff_data (abfd)) != NULL)
3271 1.8 christos {
3272 1.9 christos if (tdata->section_by_index)
3273 1.9 christos {
3274 1.9 christos htab_delete (tdata->section_by_index);
3275 1.9 christos tdata->section_by_index = NULL;
3276 1.9 christos }
3277 1.9 christos
3278 1.9 christos if (tdata->section_by_target_index)
3279 1.9 christos {
3280 1.9 christos htab_delete (tdata->section_by_target_index);
3281 1.9 christos tdata->section_by_target_index = NULL;
3282 1.9 christos }
3283 1.9 christos
3284 1.9 christos if (obj_pe (abfd) && pe_data (abfd)->comdat_hash)
3285 1.9 christos {
3286 1.9 christos htab_delete (pe_data (abfd)->comdat_hash);
3287 1.9 christos pe_data (abfd)->comdat_hash = NULL;
3288 1.9 christos }
3289 1.9 christos
3290 1.9 christos _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
3291 1.9 christos _bfd_stab_cleanup (abfd, &tdata->line_info);
3292 1.9 christos
3293 1.8 christos /* PR 25447:
3294 1.8 christos Do not clear the keep_syms and keep_strings flags.
3295 1.8 christos These may have been set by pe_ILF_build_a_bfd() indicating
3296 1.8 christos that the syms and strings pointers are not to be freed. */
3297 1.9 christos if (!_bfd_coff_free_symbols (abfd))
3298 1.8 christos return false;
3299 1.9 christos }
3300 1.8 christos
3301 1.9 christos return _bfd_generic_bfd_free_cached_info (abfd);
3302 1.7 christos }
3303