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