ecofflink.c revision 1.1 1 1.1 skrll /* Routines to link ECOFF debugging information.
2 1.1 skrll Copyright 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
3 1.1 skrll 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 1.1 skrll Written by Ian Lance Taylor, Cygnus Support, <ian (at) cygnus.com>.
5 1.1 skrll
6 1.1 skrll This file is part of BFD, the Binary File Descriptor library.
7 1.1 skrll
8 1.1 skrll This program is free software; you can redistribute it and/or modify
9 1.1 skrll it under the terms of the GNU General Public License as published by
10 1.1 skrll the Free Software Foundation; either version 3 of the License, or
11 1.1 skrll (at your option) any later version.
12 1.1 skrll
13 1.1 skrll This program is distributed in the hope that it will be useful,
14 1.1 skrll but WITHOUT ANY WARRANTY; without even the implied warranty of
15 1.1 skrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 1.1 skrll GNU General Public License for more details.
17 1.1 skrll
18 1.1 skrll You should have received a copy of the GNU General Public License
19 1.1 skrll along with this program; if not, write to the Free Software
20 1.1 skrll Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 1.1 skrll MA 02110-1301, USA. */
22 1.1 skrll
23 1.1 skrll #include "sysdep.h"
24 1.1 skrll #include "bfd.h"
25 1.1 skrll #include "bfdlink.h"
26 1.1 skrll #include "libbfd.h"
27 1.1 skrll #include "objalloc.h"
28 1.1 skrll #include "aout/stab_gnu.h"
29 1.1 skrll #include "coff/internal.h"
30 1.1 skrll #include "coff/sym.h"
31 1.1 skrll #include "coff/symconst.h"
32 1.1 skrll #include "coff/ecoff.h"
33 1.1 skrll #include "libcoff.h"
34 1.1 skrll #include "libecoff.h"
35 1.1 skrll
36 1.1 skrll static bfd_boolean ecoff_add_bytes
38 1.1 skrll PARAMS ((char **buf, char **bufend, size_t need));
39 1.1 skrll static struct bfd_hash_entry *string_hash_newfunc
40 1.1 skrll PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
41 1.1 skrll const char *));
42 1.1 skrll static void ecoff_align_debug
43 1.1 skrll PARAMS ((bfd *abfd, struct ecoff_debug_info *debug,
44 1.1 skrll const struct ecoff_debug_swap *swap));
45 1.1 skrll static bfd_boolean ecoff_write_symhdr
46 1.1 skrll PARAMS ((bfd *, struct ecoff_debug_info *, const struct ecoff_debug_swap *,
47 1.1 skrll file_ptr where));
48 1.1 skrll static int cmp_fdrtab_entry
49 1.1 skrll PARAMS ((const PTR, const PTR));
50 1.1 skrll static bfd_boolean mk_fdrtab
51 1.1 skrll PARAMS ((bfd *, struct ecoff_debug_info * const,
52 1.1 skrll const struct ecoff_debug_swap * const, struct ecoff_find_line *));
53 1.1 skrll static long fdrtab_lookup
54 1.1 skrll PARAMS ((struct ecoff_find_line *, bfd_vma));
55 1.1 skrll static bfd_boolean lookup_line
56 1.1 skrll PARAMS ((bfd *, struct ecoff_debug_info * const,
57 1.1 skrll const struct ecoff_debug_swap * const, struct ecoff_find_line *));
58 1.1 skrll
59 1.1 skrll /* Routines to swap auxiliary information in and out. I am assuming
61 1.1 skrll that the auxiliary information format is always going to be target
62 1.1 skrll independent. */
63 1.1 skrll
64 1.1 skrll /* Swap in a type information record.
65 1.1 skrll BIGEND says whether AUX symbols are big-endian or little-endian; this
66 1.1 skrll info comes from the file header record (fh-fBigendian). */
67 1.1 skrll
68 1.1 skrll void
69 1.1 skrll _bfd_ecoff_swap_tir_in (bigend, ext_copy, intern)
70 1.1 skrll int bigend;
71 1.1 skrll const struct tir_ext *ext_copy;
72 1.1 skrll TIR *intern;
73 1.1 skrll {
74 1.1 skrll struct tir_ext ext[1];
75 1.1 skrll
76 1.1 skrll *ext = *ext_copy; /* Make it reasonable to do in-place. */
77 1.1 skrll
78 1.1 skrll /* now the fun stuff... */
79 1.1 skrll if (bigend) {
80 1.1 skrll intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
81 1.1 skrll intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
82 1.1 skrll intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
83 1.1 skrll >> TIR_BITS1_BT_SH_BIG;
84 1.1 skrll intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
85 1.1 skrll >> TIR_BITS_TQ4_SH_BIG;
86 1.1 skrll intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
87 1.1 skrll >> TIR_BITS_TQ5_SH_BIG;
88 1.1 skrll intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
89 1.1 skrll >> TIR_BITS_TQ0_SH_BIG;
90 1.1 skrll intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
91 1.1 skrll >> TIR_BITS_TQ1_SH_BIG;
92 1.1 skrll intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
93 1.1 skrll >> TIR_BITS_TQ2_SH_BIG;
94 1.1 skrll intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
95 1.1 skrll >> TIR_BITS_TQ3_SH_BIG;
96 1.1 skrll } else {
97 1.1 skrll intern->fBitfield = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
98 1.1 skrll intern->continued = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
99 1.1 skrll intern->bt = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
100 1.1 skrll >> TIR_BITS1_BT_SH_LITTLE;
101 1.1 skrll intern->tq4 = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
102 1.1 skrll >> TIR_BITS_TQ4_SH_LITTLE;
103 1.1 skrll intern->tq5 = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
104 1.1 skrll >> TIR_BITS_TQ5_SH_LITTLE;
105 1.1 skrll intern->tq0 = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
106 1.1 skrll >> TIR_BITS_TQ0_SH_LITTLE;
107 1.1 skrll intern->tq1 = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
108 1.1 skrll >> TIR_BITS_TQ1_SH_LITTLE;
109 1.1 skrll intern->tq2 = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
110 1.1 skrll >> TIR_BITS_TQ2_SH_LITTLE;
111 1.1 skrll intern->tq3 = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
112 1.1 skrll >> TIR_BITS_TQ3_SH_LITTLE;
113 1.1 skrll }
114 1.1 skrll
115 1.1 skrll #ifdef TEST
116 1.1 skrll if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
117 1.1 skrll abort ();
118 1.1 skrll #endif
119 1.1 skrll }
120 1.1 skrll
121 1.1 skrll /* Swap out a type information record.
122 1.1 skrll BIGEND says whether AUX symbols are big-endian or little-endian; this
123 1.1 skrll info comes from the file header record (fh-fBigendian). */
124 1.1 skrll
125 1.1 skrll void
126 1.1 skrll _bfd_ecoff_swap_tir_out (bigend, intern_copy, ext)
127 1.1 skrll int bigend;
128 1.1 skrll const TIR *intern_copy;
129 1.1 skrll struct tir_ext *ext;
130 1.1 skrll {
131 1.1 skrll TIR intern[1];
132 1.1 skrll
133 1.1 skrll *intern = *intern_copy; /* Make it reasonable to do in-place. */
134 1.1 skrll
135 1.1 skrll /* now the fun stuff... */
136 1.1 skrll if (bigend) {
137 1.1 skrll ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
138 1.1 skrll | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
139 1.1 skrll | ((intern->bt << TIR_BITS1_BT_SH_BIG)
140 1.1 skrll & TIR_BITS1_BT_BIG));
141 1.1 skrll ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
142 1.1 skrll & TIR_BITS_TQ4_BIG)
143 1.1 skrll | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
144 1.1 skrll & TIR_BITS_TQ5_BIG));
145 1.1 skrll ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
146 1.1 skrll & TIR_BITS_TQ0_BIG)
147 1.1 skrll | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
148 1.1 skrll & TIR_BITS_TQ1_BIG));
149 1.1 skrll ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
150 1.1 skrll & TIR_BITS_TQ2_BIG)
151 1.1 skrll | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
152 1.1 skrll & TIR_BITS_TQ3_BIG));
153 1.1 skrll } else {
154 1.1 skrll ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
155 1.1 skrll | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
156 1.1 skrll | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
157 1.1 skrll & TIR_BITS1_BT_LITTLE));
158 1.1 skrll ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
159 1.1 skrll & TIR_BITS_TQ4_LITTLE)
160 1.1 skrll | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
161 1.1 skrll & TIR_BITS_TQ5_LITTLE));
162 1.1 skrll ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
163 1.1 skrll & TIR_BITS_TQ0_LITTLE)
164 1.1 skrll | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
165 1.1 skrll & TIR_BITS_TQ1_LITTLE));
166 1.1 skrll ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
167 1.1 skrll & TIR_BITS_TQ2_LITTLE)
168 1.1 skrll | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
169 1.1 skrll & TIR_BITS_TQ3_LITTLE));
170 1.1 skrll }
171 1.1 skrll
172 1.1 skrll #ifdef TEST
173 1.1 skrll if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
174 1.1 skrll abort ();
175 1.1 skrll #endif
176 1.1 skrll }
177 1.1 skrll
178 1.1 skrll /* Swap in a relative symbol record. BIGEND says whether it is in
179 1.1 skrll big-endian or little-endian format.*/
180 1.1 skrll
181 1.1 skrll void
182 1.1 skrll _bfd_ecoff_swap_rndx_in (bigend, ext_copy, intern)
183 1.1 skrll int bigend;
184 1.1 skrll const struct rndx_ext *ext_copy;
185 1.1 skrll RNDXR *intern;
186 1.1 skrll {
187 1.1 skrll struct rndx_ext ext[1];
188 1.1 skrll
189 1.1 skrll *ext = *ext_copy; /* Make it reasonable to do in-place. */
190 1.1 skrll
191 1.1 skrll /* now the fun stuff... */
192 1.1 skrll if (bigend) {
193 1.1 skrll intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
194 1.1 skrll | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
195 1.1 skrll >> RNDX_BITS1_RFD_SH_BIG);
196 1.1 skrll intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
197 1.1 skrll << RNDX_BITS1_INDEX_SH_LEFT_BIG)
198 1.1 skrll | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
199 1.1 skrll | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
200 1.1 skrll } else {
201 1.1 skrll intern->rfd = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
202 1.1 skrll | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
203 1.1 skrll << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
204 1.1 skrll intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
205 1.1 skrll >> RNDX_BITS1_INDEX_SH_LITTLE)
206 1.1 skrll | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
207 1.1 skrll | ((unsigned int) ext->r_bits[3]
208 1.1 skrll << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
209 1.1 skrll }
210 1.1 skrll
211 1.1 skrll #ifdef TEST
212 1.1 skrll if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
213 1.1 skrll abort ();
214 1.1 skrll #endif
215 1.1 skrll }
216 1.1 skrll
217 1.1 skrll /* Swap out a relative symbol record. BIGEND says whether it is in
218 1.1 skrll big-endian or little-endian format.*/
219 1.1 skrll
220 1.1 skrll void
221 1.1 skrll _bfd_ecoff_swap_rndx_out (bigend, intern_copy, ext)
222 1.1 skrll int bigend;
223 1.1 skrll const RNDXR *intern_copy;
224 1.1 skrll struct rndx_ext *ext;
225 1.1 skrll {
226 1.1 skrll RNDXR intern[1];
227 1.1 skrll
228 1.1 skrll *intern = *intern_copy; /* Make it reasonable to do in-place. */
229 1.1 skrll
230 1.1 skrll /* now the fun stuff... */
231 1.1 skrll if (bigend) {
232 1.1 skrll ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
233 1.1 skrll ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
234 1.1 skrll & RNDX_BITS1_RFD_BIG)
235 1.1 skrll | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
236 1.1 skrll & RNDX_BITS1_INDEX_BIG));
237 1.1 skrll ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
238 1.1 skrll ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
239 1.1 skrll } else {
240 1.1 skrll ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
241 1.1 skrll ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
242 1.1 skrll & RNDX_BITS1_RFD_LITTLE)
243 1.1 skrll | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
244 1.1 skrll & RNDX_BITS1_INDEX_LITTLE));
245 1.1 skrll ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
246 1.1 skrll ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
247 1.1 skrll }
248 1.1 skrll
249 1.1 skrll #ifdef TEST
250 1.1 skrll if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
251 1.1 skrll abort ();
252 1.1 skrll #endif
253 1.1 skrll }
254 1.1 skrll
255 1.1 skrll /* The minimum amount of data to allocate. */
257 1.1 skrll #define ALLOC_SIZE (4064)
258 1.1 skrll
259 1.1 skrll /* Add bytes to a buffer. Return success. */
260 1.1 skrll
261 1.1 skrll static bfd_boolean
262 1.1 skrll ecoff_add_bytes (buf, bufend, need)
263 1.1 skrll char **buf;
264 1.1 skrll char **bufend;
265 1.1 skrll size_t need;
266 1.1 skrll {
267 1.1 skrll size_t have;
268 1.1 skrll size_t want;
269 1.1 skrll char *newbuf;
270 1.1 skrll
271 1.1 skrll have = *bufend - *buf;
272 1.1 skrll if (have > need)
273 1.1 skrll want = ALLOC_SIZE;
274 1.1 skrll else
275 1.1 skrll {
276 1.1 skrll want = need - have;
277 1.1 skrll if (want < ALLOC_SIZE)
278 1.1 skrll want = ALLOC_SIZE;
279 1.1 skrll }
280 1.1 skrll newbuf = (char *) bfd_realloc (*buf, (bfd_size_type) have + want);
281 1.1 skrll if (newbuf == NULL)
282 1.1 skrll return FALSE;
283 1.1 skrll *buf = newbuf;
284 1.1 skrll *bufend = *buf + have + want;
285 1.1 skrll return TRUE;
286 1.1 skrll }
287 1.1 skrll
288 1.1 skrll /* We keep a hash table which maps strings to numbers. We use it to
289 1.1 skrll map FDR names to indices in the output file, and to map local
290 1.1 skrll strings when combining stabs debugging information. */
291 1.1 skrll
292 1.1 skrll struct string_hash_entry
293 1.1 skrll {
294 1.1 skrll struct bfd_hash_entry root;
295 1.1 skrll /* FDR index or string table offset. */
296 1.1 skrll long val;
297 1.1 skrll /* Next entry in string table. */
298 1.1 skrll struct string_hash_entry *next;
299 1.1 skrll };
300 1.1 skrll
301 1.1 skrll struct string_hash_table
302 1.1 skrll {
303 1.1 skrll struct bfd_hash_table table;
304 1.1 skrll };
305 1.1 skrll
306 1.1 skrll /* Routine to create an entry in a string hash table. */
307 1.1 skrll
308 1.1 skrll static struct bfd_hash_entry *
309 1.1 skrll string_hash_newfunc (entry, table, string)
310 1.1 skrll struct bfd_hash_entry *entry;
311 1.1 skrll struct bfd_hash_table *table;
312 1.1 skrll const char *string;
313 1.1 skrll {
314 1.1 skrll struct string_hash_entry *ret = (struct string_hash_entry *) entry;
315 1.1 skrll
316 1.1 skrll /* Allocate the structure if it has not already been allocated by a
317 1.1 skrll subclass. */
318 1.1 skrll if (ret == (struct string_hash_entry *) NULL)
319 1.1 skrll ret = ((struct string_hash_entry *)
320 1.1 skrll bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
321 1.1 skrll if (ret == (struct string_hash_entry *) NULL)
322 1.1 skrll return NULL;
323 1.1 skrll
324 1.1 skrll /* Call the allocation method of the superclass. */
325 1.1 skrll ret = ((struct string_hash_entry *)
326 1.1 skrll bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
327 1.1 skrll
328 1.1 skrll if (ret)
329 1.1 skrll {
330 1.1 skrll /* Initialize the local fields. */
331 1.1 skrll ret->val = -1;
332 1.1 skrll ret->next = NULL;
333 1.1 skrll }
334 1.1 skrll
335 1.1 skrll return (struct bfd_hash_entry *) ret;
336 1.1 skrll }
337 1.1 skrll
338 1.1 skrll /* Look up an entry in an string hash table. */
339 1.1 skrll
340 1.1 skrll #define string_hash_lookup(t, string, create, copy) \
341 1.1 skrll ((struct string_hash_entry *) \
342 1.1 skrll bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
343 1.1 skrll
344 1.1 skrll /* We can't afford to read in all the debugging information when we do
345 1.1 skrll a link. Instead, we build a list of these structures to show how
346 1.1 skrll different parts of the input file map to the output file. */
347 1.1 skrll
348 1.1 skrll struct shuffle
349 1.1 skrll {
350 1.1 skrll /* The next entry in this linked list. */
351 1.1 skrll struct shuffle *next;
352 1.1 skrll /* The length of the information. */
353 1.1 skrll unsigned long size;
354 1.1 skrll /* Whether this information comes from a file or not. */
355 1.1 skrll bfd_boolean filep;
356 1.1 skrll union
357 1.1 skrll {
358 1.1 skrll struct
359 1.1 skrll {
360 1.1 skrll /* The BFD the data comes from. */
361 1.1 skrll bfd *input_bfd;
362 1.1 skrll /* The offset within input_bfd. */
363 1.1 skrll file_ptr offset;
364 1.1 skrll } file;
365 1.1 skrll /* The data to be written out. */
366 1.1 skrll PTR memory;
367 1.1 skrll } u;
368 1.1 skrll };
369 1.1 skrll
370 1.1 skrll /* This structure holds information across calls to
371 1.1 skrll bfd_ecoff_debug_accumulate. */
372 1.1 skrll
373 1.1 skrll struct accumulate
374 1.1 skrll {
375 1.1 skrll /* The FDR hash table. */
376 1.1 skrll struct string_hash_table fdr_hash;
377 1.1 skrll /* The strings hash table. */
378 1.1 skrll struct string_hash_table str_hash;
379 1.1 skrll /* Linked lists describing how to shuffle the input debug
380 1.1 skrll information into the output file. We keep a pointer to both the
381 1.1 skrll head and the tail. */
382 1.1 skrll struct shuffle *line;
383 1.1 skrll struct shuffle *line_end;
384 1.1 skrll struct shuffle *pdr;
385 1.1 skrll struct shuffle *pdr_end;
386 1.1 skrll struct shuffle *sym;
387 1.1 skrll struct shuffle *sym_end;
388 1.1 skrll struct shuffle *opt;
389 1.1 skrll struct shuffle *opt_end;
390 1.1 skrll struct shuffle *aux;
391 1.1 skrll struct shuffle *aux_end;
392 1.1 skrll struct shuffle *ss;
393 1.1 skrll struct shuffle *ss_end;
394 1.1 skrll struct string_hash_entry *ss_hash;
395 1.1 skrll struct string_hash_entry *ss_hash_end;
396 1.1 skrll struct shuffle *fdr;
397 1.1 skrll struct shuffle *fdr_end;
398 1.1 skrll struct shuffle *rfd;
399 1.1 skrll struct shuffle *rfd_end;
400 1.1 skrll /* The size of the largest file shuffle. */
401 1.1 skrll unsigned long largest_file_shuffle;
402 1.1 skrll /* An objalloc for debugging information. */
403 1.1 skrll struct objalloc *memory;
404 1.1 skrll };
405 1.1 skrll
406 1.1 skrll /* Add a file entry to a shuffle list. */
407 1.1 skrll
408 1.1 skrll static bfd_boolean add_file_shuffle
409 1.1 skrll PARAMS ((struct accumulate *, struct shuffle **, struct shuffle **,
410 1.1 skrll bfd *, file_ptr, unsigned long));
411 1.1 skrll
412 1.1 skrll static bfd_boolean
413 1.1 skrll add_file_shuffle (ainfo, head, tail, input_bfd, offset, size)
414 1.1 skrll struct accumulate *ainfo;
415 1.1 skrll struct shuffle **head;
416 1.1 skrll struct shuffle **tail;
417 1.1 skrll bfd *input_bfd;
418 1.1 skrll file_ptr offset;
419 1.1 skrll unsigned long size;
420 1.1 skrll {
421 1.1 skrll struct shuffle *n;
422 1.1 skrll
423 1.1 skrll if (*tail != (struct shuffle *) NULL
424 1.1 skrll && (*tail)->filep
425 1.1 skrll && (*tail)->u.file.input_bfd == input_bfd
426 1.1 skrll && (*tail)->u.file.offset + (*tail)->size == (unsigned long) offset)
427 1.1 skrll {
428 1.1 skrll /* Just merge this entry onto the existing one. */
429 1.1 skrll (*tail)->size += size;
430 1.1 skrll if ((*tail)->size > ainfo->largest_file_shuffle)
431 1.1 skrll ainfo->largest_file_shuffle = (*tail)->size;
432 1.1 skrll return TRUE;
433 1.1 skrll }
434 1.1 skrll
435 1.1 skrll n = (struct shuffle *) objalloc_alloc (ainfo->memory,
436 1.1 skrll sizeof (struct shuffle));
437 1.1 skrll if (!n)
438 1.1 skrll {
439 1.1 skrll bfd_set_error (bfd_error_no_memory);
440 1.1 skrll return FALSE;
441 1.1 skrll }
442 1.1 skrll n->next = NULL;
443 1.1 skrll n->size = size;
444 1.1 skrll n->filep = TRUE;
445 1.1 skrll n->u.file.input_bfd = input_bfd;
446 1.1 skrll n->u.file.offset = offset;
447 1.1 skrll if (*head == (struct shuffle *) NULL)
448 1.1 skrll *head = n;
449 1.1 skrll if (*tail != (struct shuffle *) NULL)
450 1.1 skrll (*tail)->next = n;
451 1.1 skrll *tail = n;
452 1.1 skrll if (size > ainfo->largest_file_shuffle)
453 1.1 skrll ainfo->largest_file_shuffle = size;
454 1.1 skrll return TRUE;
455 1.1 skrll }
456 1.1 skrll
457 1.1 skrll /* Add a memory entry to a shuffle list. */
458 1.1 skrll
459 1.1 skrll static bfd_boolean add_memory_shuffle
460 1.1 skrll PARAMS ((struct accumulate *, struct shuffle **head, struct shuffle **tail,
461 1.1 skrll bfd_byte *data, unsigned long size));
462 1.1 skrll
463 1.1 skrll static bfd_boolean
464 1.1 skrll add_memory_shuffle (ainfo, head, tail, data, size)
465 1.1 skrll struct accumulate *ainfo;
466 1.1 skrll struct shuffle **head;
467 1.1 skrll struct shuffle **tail;
468 1.1 skrll bfd_byte *data;
469 1.1 skrll unsigned long size;
470 1.1 skrll {
471 1.1 skrll struct shuffle *n;
472 1.1 skrll
473 1.1 skrll n = (struct shuffle *) objalloc_alloc (ainfo->memory,
474 1.1 skrll sizeof (struct shuffle));
475 1.1 skrll if (!n)
476 1.1 skrll {
477 1.1 skrll bfd_set_error (bfd_error_no_memory);
478 1.1 skrll return FALSE;
479 1.1 skrll }
480 1.1 skrll n->next = NULL;
481 1.1 skrll n->size = size;
482 1.1 skrll n->filep = FALSE;
483 1.1 skrll n->u.memory = (PTR) data;
484 1.1 skrll if (*head == (struct shuffle *) NULL)
485 1.1 skrll *head = n;
486 1.1 skrll if (*tail != (struct shuffle *) NULL)
487 1.1 skrll (*tail)->next = n;
488 1.1 skrll *tail = n;
489 1.1 skrll return TRUE;
490 1.1 skrll }
491 1.1 skrll
492 1.1 skrll /* Initialize the FDR hash table. This returns a handle which is then
493 1.1 skrll passed in to bfd_ecoff_debug_accumulate, et. al. */
494 1.1 skrll
495 1.1 skrll PTR
496 1.1 skrll bfd_ecoff_debug_init (output_bfd, output_debug, output_swap, info)
497 1.1 skrll bfd *output_bfd ATTRIBUTE_UNUSED;
498 1.1 skrll struct ecoff_debug_info *output_debug;
499 1.1 skrll const struct ecoff_debug_swap *output_swap ATTRIBUTE_UNUSED;
500 1.1 skrll struct bfd_link_info *info;
501 1.1 skrll {
502 1.1 skrll struct accumulate *ainfo;
503 1.1 skrll bfd_size_type amt = sizeof (struct accumulate);
504 1.1 skrll
505 1.1 skrll ainfo = (struct accumulate *) bfd_malloc (amt);
506 1.1 skrll if (!ainfo)
507 1.1 skrll return NULL;
508 1.1 skrll if (!bfd_hash_table_init_n (&ainfo->fdr_hash.table, string_hash_newfunc,
509 1.1 skrll sizeof (struct string_hash_entry), 1021))
510 1.1 skrll return NULL;
511 1.1 skrll
512 1.1 skrll ainfo->line = NULL;
513 1.1 skrll ainfo->line_end = NULL;
514 1.1 skrll ainfo->pdr = NULL;
515 1.1 skrll ainfo->pdr_end = NULL;
516 1.1 skrll ainfo->sym = NULL;
517 1.1 skrll ainfo->sym_end = NULL;
518 1.1 skrll ainfo->opt = NULL;
519 1.1 skrll ainfo->opt_end = NULL;
520 1.1 skrll ainfo->aux = NULL;
521 1.1 skrll ainfo->aux_end = NULL;
522 1.1 skrll ainfo->ss = NULL;
523 1.1 skrll ainfo->ss_end = NULL;
524 1.1 skrll ainfo->ss_hash = NULL;
525 1.1 skrll ainfo->ss_hash_end = NULL;
526 1.1 skrll ainfo->fdr = NULL;
527 1.1 skrll ainfo->fdr_end = NULL;
528 1.1 skrll ainfo->rfd = NULL;
529 1.1 skrll ainfo->rfd_end = NULL;
530 1.1 skrll
531 1.1 skrll ainfo->largest_file_shuffle = 0;
532 1.1 skrll
533 1.1 skrll if (! info->relocatable)
534 1.1 skrll {
535 1.1 skrll if (!bfd_hash_table_init (&ainfo->str_hash.table, string_hash_newfunc,
536 1.1 skrll sizeof (struct string_hash_entry)))
537 1.1 skrll return NULL;
538 1.1 skrll
539 1.1 skrll /* The first entry in the string table is the empty string. */
540 1.1 skrll output_debug->symbolic_header.issMax = 1;
541 1.1 skrll }
542 1.1 skrll
543 1.1 skrll ainfo->memory = objalloc_create ();
544 1.1 skrll if (ainfo->memory == NULL)
545 1.1 skrll {
546 1.1 skrll bfd_set_error (bfd_error_no_memory);
547 1.1 skrll return NULL;
548 1.1 skrll }
549 1.1 skrll
550 1.1 skrll return (PTR) ainfo;
551 1.1 skrll }
552 1.1 skrll
553 1.1 skrll /* Free the accumulated debugging information. */
554 1.1 skrll
555 1.1 skrll void
556 1.1 skrll bfd_ecoff_debug_free (handle, output_bfd, output_debug, output_swap, info)
557 1.1 skrll PTR handle;
558 1.1 skrll bfd *output_bfd ATTRIBUTE_UNUSED;
559 1.1 skrll struct ecoff_debug_info *output_debug ATTRIBUTE_UNUSED;
560 1.1 skrll const struct ecoff_debug_swap *output_swap ATTRIBUTE_UNUSED;
561 1.1 skrll struct bfd_link_info *info;
562 1.1 skrll {
563 1.1 skrll struct accumulate *ainfo = (struct accumulate *) handle;
564 1.1 skrll
565 1.1 skrll bfd_hash_table_free (&ainfo->fdr_hash.table);
566 1.1 skrll
567 1.1 skrll if (! info->relocatable)
568 1.1 skrll bfd_hash_table_free (&ainfo->str_hash.table);
569 1.1 skrll
570 1.1 skrll objalloc_free (ainfo->memory);
571 1.1 skrll
572 1.1 skrll free (ainfo);
573 1.1 skrll }
574 1.1 skrll
575 1.1 skrll /* Accumulate the debugging information from INPUT_BFD into
576 1.1 skrll OUTPUT_BFD. The INPUT_DEBUG argument points to some ECOFF
577 1.1 skrll debugging information which we want to link into the information
578 1.1 skrll pointed to by the OUTPUT_DEBUG argument. OUTPUT_SWAP and
579 1.1 skrll INPUT_SWAP point to the swapping information needed. INFO is the
580 1.1 skrll linker information structure. HANDLE is returned by
581 1.1 skrll bfd_ecoff_debug_init. */
582 1.1 skrll
583 1.1 skrll bfd_boolean
584 1.1 skrll bfd_ecoff_debug_accumulate (handle, output_bfd, output_debug, output_swap,
585 1.1 skrll input_bfd, input_debug, input_swap,
586 1.1 skrll info)
587 1.1 skrll PTR handle;
588 1.1 skrll bfd *output_bfd;
589 1.1 skrll struct ecoff_debug_info *output_debug;
590 1.1 skrll const struct ecoff_debug_swap *output_swap;
591 1.1 skrll bfd *input_bfd;
592 1.1 skrll struct ecoff_debug_info *input_debug;
593 1.1 skrll const struct ecoff_debug_swap *input_swap;
594 1.1 skrll struct bfd_link_info *info;
595 1.1 skrll {
596 1.1 skrll struct accumulate *ainfo = (struct accumulate *) handle;
597 1.1 skrll void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
598 1.1 skrll = input_swap->swap_sym_in;
599 1.1 skrll void (* const swap_rfd_in) PARAMS ((bfd *, PTR, RFDT *))
600 1.1 skrll = input_swap->swap_rfd_in;
601 1.1 skrll void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
602 1.1 skrll = output_swap->swap_sym_out;
603 1.1 skrll void (* const swap_fdr_out) PARAMS ((bfd *, const FDR *, PTR))
604 1.1 skrll = output_swap->swap_fdr_out;
605 1.1 skrll void (* const swap_rfd_out) PARAMS ((bfd *, const RFDT *, PTR))
606 1.1 skrll = output_swap->swap_rfd_out;
607 1.1 skrll bfd_size_type external_pdr_size = output_swap->external_pdr_size;
608 1.1 skrll bfd_size_type external_sym_size = output_swap->external_sym_size;
609 1.1 skrll bfd_size_type external_opt_size = output_swap->external_opt_size;
610 1.1 skrll bfd_size_type external_fdr_size = output_swap->external_fdr_size;
611 1.1 skrll bfd_size_type external_rfd_size = output_swap->external_rfd_size;
612 1.1 skrll HDRR * const output_symhdr = &output_debug->symbolic_header;
613 1.1 skrll HDRR * const input_symhdr = &input_debug->symbolic_header;
614 1.1 skrll bfd_vma section_adjust[scMax];
615 1.1 skrll asection *sec;
616 1.1 skrll bfd_byte *fdr_start;
617 1.1 skrll bfd_byte *fdr_ptr;
618 1.1 skrll bfd_byte *fdr_end;
619 1.1 skrll bfd_size_type fdr_add;
620 1.1 skrll unsigned int copied;
621 1.1 skrll RFDT i;
622 1.1 skrll unsigned long sz;
623 1.1 skrll bfd_byte *rfd_out;
624 1.1 skrll bfd_byte *rfd_in;
625 1.1 skrll bfd_byte *rfd_end;
626 1.1 skrll long newrfdbase = 0;
627 1.1 skrll long oldrfdbase = 0;
628 1.1 skrll bfd_byte *fdr_out;
629 1.1 skrll bfd_size_type amt;
630 1.1 skrll
631 1.1 skrll /* Use section_adjust to hold the value to add to a symbol in a
632 1.1 skrll particular section. */
633 1.1 skrll memset ((PTR) section_adjust, 0, sizeof section_adjust);
634 1.1 skrll
635 1.1 skrll #define SET(name, indx) \
636 1.1 skrll sec = bfd_get_section_by_name (input_bfd, name); \
637 1.1 skrll if (sec != NULL) \
638 1.1 skrll section_adjust[indx] = (sec->output_section->vma \
639 1.1 skrll + sec->output_offset \
640 1.1 skrll - sec->vma);
641 1.1 skrll
642 1.1 skrll SET (".text", scText);
643 1.1 skrll SET (".data", scData);
644 1.1 skrll SET (".bss", scBss);
645 1.1 skrll SET (".sdata", scSData);
646 1.1 skrll SET (".sbss", scSBss);
647 1.1 skrll /* scRdata section may be either .rdata or .rodata. */
648 1.1 skrll SET (".rdata", scRData);
649 1.1 skrll SET (".rodata", scRData);
650 1.1 skrll SET (".init", scInit);
651 1.1 skrll SET (".fini", scFini);
652 1.1 skrll SET (".rconst", scRConst);
653 1.1 skrll
654 1.1 skrll #undef SET
655 1.1 skrll
656 1.1 skrll /* Find all the debugging information based on the FDR's. We need
657 1.1 skrll to handle them whether they are swapped or not. */
658 1.1 skrll if (input_debug->fdr != (FDR *) NULL)
659 1.1 skrll {
660 1.1 skrll fdr_start = (bfd_byte *) input_debug->fdr;
661 1.1 skrll fdr_add = sizeof (FDR);
662 1.1 skrll }
663 1.1 skrll else
664 1.1 skrll {
665 1.1 skrll fdr_start = (bfd_byte *) input_debug->external_fdr;
666 1.1 skrll fdr_add = input_swap->external_fdr_size;
667 1.1 skrll }
668 1.1 skrll fdr_end = fdr_start + input_symhdr->ifdMax * fdr_add;
669 1.1 skrll
670 1.1 skrll amt = input_symhdr->ifdMax;
671 1.1 skrll amt *= sizeof (RFDT);
672 1.1 skrll input_debug->ifdmap = (RFDT *) bfd_alloc (input_bfd, amt);
673 1.1 skrll
674 1.1 skrll sz = (input_symhdr->crfd + input_symhdr->ifdMax) * external_rfd_size;
675 1.1 skrll rfd_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
676 1.1 skrll if (!input_debug->ifdmap || !rfd_out)
677 1.1 skrll {
678 1.1 skrll bfd_set_error (bfd_error_no_memory);
679 1.1 skrll return FALSE;
680 1.1 skrll }
681 1.1 skrll if (!add_memory_shuffle (ainfo, &ainfo->rfd, &ainfo->rfd_end, rfd_out, sz))
682 1.1 skrll return FALSE;
683 1.1 skrll
684 1.1 skrll copied = 0;
685 1.1 skrll
686 1.1 skrll /* Look through the FDR's to see which ones we are going to include
687 1.1 skrll in the final output. We do not want duplicate FDR information
688 1.1 skrll for header files, because ECOFF debugging is often very large.
689 1.1 skrll When we find an FDR with no line information which can be merged,
690 1.1 skrll we look it up in a hash table to ensure that we only include it
691 1.1 skrll once. We keep a table mapping FDR numbers to the final number
692 1.1 skrll they get with the BFD, so that we can refer to it when we write
693 1.1 skrll out the external symbols. */
694 1.1 skrll for (fdr_ptr = fdr_start, i = 0;
695 1.1 skrll fdr_ptr < fdr_end;
696 1.1 skrll fdr_ptr += fdr_add, i++, rfd_out += external_rfd_size)
697 1.1 skrll {
698 1.1 skrll FDR fdr;
699 1.1 skrll
700 1.1 skrll if (input_debug->fdr != (FDR *) NULL)
701 1.1 skrll fdr = *(FDR *) fdr_ptr;
702 1.1 skrll else
703 1.1 skrll (*input_swap->swap_fdr_in) (input_bfd, (PTR) fdr_ptr, &fdr);
704 1.1 skrll
705 1.1 skrll /* See if this FDR can be merged with an existing one. */
706 1.1 skrll if (fdr.cbLine == 0 && fdr.rss != -1 && fdr.fMerge)
707 1.1 skrll {
708 1.1 skrll const char *name;
709 1.1 skrll char *lookup;
710 1.1 skrll struct string_hash_entry *fh;
711 1.1 skrll
712 1.1 skrll /* We look up a string formed from the file name and the
713 1.1 skrll number of symbols and aux entries. Sometimes an include
714 1.1 skrll file will conditionally define a typedef or something
715 1.1 skrll based on the order of include files. Using the number of
716 1.1 skrll symbols and aux entries as a hash reduces the chance that
717 1.1 skrll we will merge symbol information that should not be
718 1.1 skrll merged. */
719 1.1 skrll name = input_debug->ss + fdr.issBase + fdr.rss;
720 1.1 skrll
721 1.1 skrll lookup = (char *) bfd_malloc ((bfd_size_type) strlen (name) + 20);
722 1.1 skrll if (lookup == NULL)
723 1.1 skrll return FALSE;
724 1.1 skrll sprintf (lookup, "%s %lx %lx", name, (unsigned long) fdr.csym,
725 1.1 skrll (unsigned long) fdr.caux);
726 1.1 skrll
727 1.1 skrll fh = string_hash_lookup (&ainfo->fdr_hash, lookup, TRUE, TRUE);
728 1.1 skrll free (lookup);
729 1.1 skrll if (fh == (struct string_hash_entry *) NULL)
730 1.1 skrll return FALSE;
731 1.1 skrll
732 1.1 skrll if (fh->val != -1)
733 1.1 skrll {
734 1.1 skrll input_debug->ifdmap[i] = fh->val;
735 1.1 skrll (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i,
736 1.1 skrll (PTR) rfd_out);
737 1.1 skrll
738 1.1 skrll /* Don't copy this FDR. */
739 1.1 skrll continue;
740 1.1 skrll }
741 1.1 skrll
742 1.1 skrll fh->val = output_symhdr->ifdMax + copied;
743 1.1 skrll }
744 1.1 skrll
745 1.1 skrll input_debug->ifdmap[i] = output_symhdr->ifdMax + copied;
746 1.1 skrll (*swap_rfd_out) (output_bfd, input_debug->ifdmap + i, (PTR) rfd_out);
747 1.1 skrll ++copied;
748 1.1 skrll }
749 1.1 skrll
750 1.1 skrll newrfdbase = output_symhdr->crfd;
751 1.1 skrll output_symhdr->crfd += input_symhdr->ifdMax;
752 1.1 skrll
753 1.1 skrll /* Copy over any existing RFD's. RFD's are only created by the
754 1.1 skrll linker, so this will only happen for input files which are the
755 1.1 skrll result of a partial link. */
756 1.1 skrll rfd_in = (bfd_byte *) input_debug->external_rfd;
757 1.1 skrll rfd_end = rfd_in + input_symhdr->crfd * input_swap->external_rfd_size;
758 1.1 skrll for (;
759 1.1 skrll rfd_in < rfd_end;
760 1.1 skrll rfd_in += input_swap->external_rfd_size)
761 1.1 skrll {
762 1.1 skrll RFDT rfd;
763 1.1 skrll
764 1.1 skrll (*swap_rfd_in) (input_bfd, (PTR) rfd_in, &rfd);
765 1.1 skrll BFD_ASSERT (rfd >= 0 && rfd < input_symhdr->ifdMax);
766 1.1 skrll rfd = input_debug->ifdmap[rfd];
767 1.1 skrll (*swap_rfd_out) (output_bfd, &rfd, (PTR) rfd_out);
768 1.1 skrll rfd_out += external_rfd_size;
769 1.1 skrll }
770 1.1 skrll
771 1.1 skrll oldrfdbase = output_symhdr->crfd;
772 1.1 skrll output_symhdr->crfd += input_symhdr->crfd;
773 1.1 skrll
774 1.1 skrll /* Look through the FDR's and copy over all associated debugging
775 1.1 skrll information. */
776 1.1 skrll sz = copied * external_fdr_size;
777 1.1 skrll fdr_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
778 1.1 skrll if (!fdr_out)
779 1.1 skrll {
780 1.1 skrll bfd_set_error (bfd_error_no_memory);
781 1.1 skrll return FALSE;
782 1.1 skrll }
783 1.1 skrll if (!add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end, fdr_out, sz))
784 1.1 skrll return FALSE;
785 1.1 skrll for (fdr_ptr = fdr_start, i = 0;
786 1.1 skrll fdr_ptr < fdr_end;
787 1.1 skrll fdr_ptr += fdr_add, i++)
788 1.1 skrll {
789 1.1 skrll FDR fdr;
790 1.1 skrll bfd_byte *sym_out;
791 1.1 skrll bfd_byte *lraw_src;
792 1.1 skrll bfd_byte *lraw_end;
793 1.1 skrll bfd_boolean fgotfilename;
794 1.1 skrll
795 1.1 skrll if (input_debug->ifdmap[i] < output_symhdr->ifdMax)
796 1.1 skrll {
797 1.1 skrll /* We are not copying this FDR. */
798 1.1 skrll continue;
799 1.1 skrll }
800 1.1 skrll
801 1.1 skrll if (input_debug->fdr != (FDR *) NULL)
802 1.1 skrll fdr = *(FDR *) fdr_ptr;
803 1.1 skrll else
804 1.1 skrll (*input_swap->swap_fdr_in) (input_bfd, (PTR) fdr_ptr, &fdr);
805 1.1 skrll
806 1.1 skrll /* FIXME: It is conceivable that this FDR points to the .init or
807 1.1 skrll .fini section, in which case this will not do the right
808 1.1 skrll thing. */
809 1.1 skrll fdr.adr += section_adjust[scText];
810 1.1 skrll
811 1.1 skrll /* Swap in the local symbols, adjust their values, and swap them
812 1.1 skrll out again. */
813 1.1 skrll fgotfilename = FALSE;
814 1.1 skrll sz = fdr.csym * external_sym_size;
815 1.1 skrll sym_out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
816 1.1 skrll if (!sym_out)
817 1.1 skrll {
818 1.1 skrll bfd_set_error (bfd_error_no_memory);
819 1.1 skrll return FALSE;
820 1.1 skrll }
821 1.1 skrll if (!add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end, sym_out,
822 1.1 skrll sz))
823 1.1 skrll return FALSE;
824 1.1 skrll lraw_src = ((bfd_byte *) input_debug->external_sym
825 1.1 skrll + fdr.isymBase * input_swap->external_sym_size);
826 1.1 skrll lraw_end = lraw_src + fdr.csym * input_swap->external_sym_size;
827 1.1 skrll for (; lraw_src < lraw_end; lraw_src += input_swap->external_sym_size)
828 1.1 skrll {
829 1.1 skrll SYMR internal_sym;
830 1.1 skrll
831 1.1 skrll (*swap_sym_in) (input_bfd, (PTR) lraw_src, &internal_sym);
832 1.1 skrll
833 1.1 skrll BFD_ASSERT (internal_sym.sc != scCommon
834 1.1 skrll && internal_sym.sc != scSCommon);
835 1.1 skrll
836 1.1 skrll /* Adjust the symbol value if appropriate. */
837 1.1 skrll switch (internal_sym.st)
838 1.1 skrll {
839 1.1 skrll case stNil:
840 1.1 skrll if (ECOFF_IS_STAB (&internal_sym))
841 1.1 skrll break;
842 1.1 skrll /* Fall through. */
843 1.1 skrll case stGlobal:
844 1.1 skrll case stStatic:
845 1.1 skrll case stLabel:
846 1.1 skrll case stProc:
847 1.1 skrll case stStaticProc:
848 1.1 skrll internal_sym.value += section_adjust[internal_sym.sc];
849 1.1 skrll break;
850 1.1 skrll
851 1.1 skrll default:
852 1.1 skrll break;
853 1.1 skrll }
854 1.1 skrll
855 1.1 skrll /* If we are doing a final link, we hash all the strings in
856 1.1 skrll the local symbol table together. This reduces the amount
857 1.1 skrll of space required by debugging information. We don't do
858 1.1 skrll this when performing a relocatable link because it would
859 1.1 skrll prevent us from easily merging different FDR's. */
860 1.1 skrll if (! info->relocatable)
861 1.1 skrll {
862 1.1 skrll bfd_boolean ffilename;
863 1.1 skrll const char *name;
864 1.1 skrll
865 1.1 skrll if (! fgotfilename && internal_sym.iss == fdr.rss)
866 1.1 skrll ffilename = TRUE;
867 1.1 skrll else
868 1.1 skrll ffilename = FALSE;
869 1.1 skrll
870 1.1 skrll /* Hash the name into the string table. */
871 1.1 skrll name = input_debug->ss + fdr.issBase + internal_sym.iss;
872 1.1 skrll if (*name == '\0')
873 1.1 skrll internal_sym.iss = 0;
874 1.1 skrll else
875 1.1 skrll {
876 1.1 skrll struct string_hash_entry *sh;
877 1.1 skrll
878 1.1 skrll sh = string_hash_lookup (&ainfo->str_hash, name, TRUE, TRUE);
879 1.1 skrll if (sh == (struct string_hash_entry *) NULL)
880 1.1 skrll return FALSE;
881 1.1 skrll if (sh->val == -1)
882 1.1 skrll {
883 1.1 skrll sh->val = output_symhdr->issMax;
884 1.1 skrll output_symhdr->issMax += strlen (name) + 1;
885 1.1 skrll if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
886 1.1 skrll ainfo->ss_hash = sh;
887 1.1 skrll if (ainfo->ss_hash_end
888 1.1 skrll != (struct string_hash_entry *) NULL)
889 1.1 skrll ainfo->ss_hash_end->next = sh;
890 1.1 skrll ainfo->ss_hash_end = sh;
891 1.1 skrll }
892 1.1 skrll internal_sym.iss = sh->val;
893 1.1 skrll }
894 1.1 skrll
895 1.1 skrll if (ffilename)
896 1.1 skrll {
897 1.1 skrll fdr.rss = internal_sym.iss;
898 1.1 skrll fgotfilename = TRUE;
899 1.1 skrll }
900 1.1 skrll }
901 1.1 skrll
902 1.1 skrll (*swap_sym_out) (output_bfd, &internal_sym, sym_out);
903 1.1 skrll sym_out += external_sym_size;
904 1.1 skrll }
905 1.1 skrll
906 1.1 skrll fdr.isymBase = output_symhdr->isymMax;
907 1.1 skrll output_symhdr->isymMax += fdr.csym;
908 1.1 skrll
909 1.1 skrll /* Copy the information that does not need swapping. */
910 1.1 skrll
911 1.1 skrll /* FIXME: If we are relaxing, we need to adjust the line
912 1.1 skrll numbers. Frankly, forget it. Anybody using stabs debugging
913 1.1 skrll information will not use this line number information, and
914 1.1 skrll stabs are adjusted correctly. */
915 1.1 skrll if (fdr.cbLine > 0)
916 1.1 skrll {
917 1.1 skrll file_ptr pos = input_symhdr->cbLineOffset + fdr.cbLineOffset;
918 1.1 skrll if (!add_file_shuffle (ainfo, &ainfo->line, &ainfo->line_end,
919 1.1 skrll input_bfd, pos, (unsigned long) fdr.cbLine))
920 1.1 skrll return FALSE;
921 1.1 skrll fdr.ilineBase = output_symhdr->ilineMax;
922 1.1 skrll fdr.cbLineOffset = output_symhdr->cbLine;
923 1.1 skrll output_symhdr->ilineMax += fdr.cline;
924 1.1 skrll output_symhdr->cbLine += fdr.cbLine;
925 1.1 skrll }
926 1.1 skrll if (fdr.caux > 0)
927 1.1 skrll {
928 1.1 skrll file_ptr pos = (input_symhdr->cbAuxOffset
929 1.1 skrll + fdr.iauxBase * sizeof (union aux_ext));
930 1.1 skrll if (!add_file_shuffle (ainfo, &ainfo->aux, &ainfo->aux_end,
931 1.1 skrll input_bfd, pos,
932 1.1 skrll fdr.caux * sizeof (union aux_ext)))
933 1.1 skrll return FALSE;
934 1.1 skrll fdr.iauxBase = output_symhdr->iauxMax;
935 1.1 skrll output_symhdr->iauxMax += fdr.caux;
936 1.1 skrll }
937 1.1 skrll if (! info->relocatable)
938 1.1 skrll {
939 1.1 skrll
940 1.1 skrll /* When are are hashing strings, we lie about the number of
941 1.1 skrll strings attached to each FDR. We need to set cbSs
942 1.1 skrll because some versions of dbx apparently use it to decide
943 1.1 skrll how much of the string table to read in. */
944 1.1 skrll fdr.issBase = 0;
945 1.1 skrll fdr.cbSs = output_symhdr->issMax;
946 1.1 skrll }
947 1.1 skrll else if (fdr.cbSs > 0)
948 1.1 skrll {
949 1.1 skrll file_ptr pos = input_symhdr->cbSsOffset + fdr.issBase;
950 1.1 skrll if (!add_file_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end,
951 1.1 skrll input_bfd, pos, (unsigned long) fdr.cbSs))
952 1.1 skrll return FALSE;
953 1.1 skrll fdr.issBase = output_symhdr->issMax;
954 1.1 skrll output_symhdr->issMax += fdr.cbSs;
955 1.1 skrll }
956 1.1 skrll
957 1.1 skrll if (output_bfd->xvec->header_byteorder
958 1.1 skrll == input_bfd->xvec->header_byteorder)
959 1.1 skrll {
960 1.1 skrll /* The two BFD's have the same endianness, and we don't have
961 1.1 skrll to adjust the PDR addresses, so simply copying the
962 1.1 skrll information will suffice. */
963 1.1 skrll BFD_ASSERT (external_pdr_size == input_swap->external_pdr_size);
964 1.1 skrll if (fdr.cpd > 0)
965 1.1 skrll {
966 1.1 skrll file_ptr pos = (input_symhdr->cbPdOffset
967 1.1 skrll + fdr.ipdFirst * external_pdr_size);
968 1.1 skrll unsigned long size = fdr.cpd * external_pdr_size;
969 1.1 skrll if (!add_file_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end,
970 1.1 skrll input_bfd, pos, size))
971 1.1 skrll return FALSE;
972 1.1 skrll }
973 1.1 skrll BFD_ASSERT (external_opt_size == input_swap->external_opt_size);
974 1.1 skrll if (fdr.copt > 0)
975 1.1 skrll {
976 1.1 skrll file_ptr pos = (input_symhdr->cbOptOffset
977 1.1 skrll + fdr.ioptBase * external_opt_size);
978 1.1 skrll unsigned long size = fdr.copt * external_opt_size;
979 1.1 skrll if (!add_file_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end,
980 1.1 skrll input_bfd, pos, size))
981 1.1 skrll return FALSE;
982 1.1 skrll }
983 1.1 skrll }
984 1.1 skrll else
985 1.1 skrll {
986 1.1 skrll bfd_size_type outsz, insz;
987 1.1 skrll bfd_byte *in;
988 1.1 skrll bfd_byte *end;
989 1.1 skrll bfd_byte *out;
990 1.1 skrll
991 1.1 skrll /* The two BFD's have different endianness, so we must swap
992 1.1 skrll everything in and out. This code would always work, but
993 1.1 skrll it would be unnecessarily slow in the normal case. */
994 1.1 skrll outsz = external_pdr_size;
995 1.1 skrll insz = input_swap->external_pdr_size;
996 1.1 skrll in = ((bfd_byte *) input_debug->external_pdr
997 1.1 skrll + fdr.ipdFirst * insz);
998 1.1 skrll end = in + fdr.cpd * insz;
999 1.1 skrll sz = fdr.cpd * outsz;
1000 1.1 skrll out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
1001 1.1 skrll if (!out)
1002 1.1 skrll {
1003 1.1 skrll bfd_set_error (bfd_error_no_memory);
1004 1.1 skrll return FALSE;
1005 1.1 skrll }
1006 1.1 skrll if (!add_memory_shuffle (ainfo, &ainfo->pdr, &ainfo->pdr_end, out,
1007 1.1 skrll sz))
1008 1.1 skrll return FALSE;
1009 1.1 skrll for (; in < end; in += insz, out += outsz)
1010 1.1 skrll {
1011 1.1 skrll PDR pdr;
1012 1.1 skrll
1013 1.1 skrll (*input_swap->swap_pdr_in) (input_bfd, (PTR) in, &pdr);
1014 1.1 skrll (*output_swap->swap_pdr_out) (output_bfd, &pdr, (PTR) out);
1015 1.1 skrll }
1016 1.1 skrll
1017 1.1 skrll /* Swap over the optimization information. */
1018 1.1 skrll outsz = external_opt_size;
1019 1.1 skrll insz = input_swap->external_opt_size;
1020 1.1 skrll in = ((bfd_byte *) input_debug->external_opt
1021 1.1 skrll + fdr.ioptBase * insz);
1022 1.1 skrll end = in + fdr.copt * insz;
1023 1.1 skrll sz = fdr.copt * outsz;
1024 1.1 skrll out = (bfd_byte *) objalloc_alloc (ainfo->memory, sz);
1025 1.1 skrll if (!out)
1026 1.1 skrll {
1027 1.1 skrll bfd_set_error (bfd_error_no_memory);
1028 1.1 skrll return FALSE;
1029 1.1 skrll }
1030 1.1 skrll if (!add_memory_shuffle (ainfo, &ainfo->opt, &ainfo->opt_end, out,
1031 1.1 skrll sz))
1032 1.1 skrll return FALSE;
1033 1.1 skrll for (; in < end; in += insz, out += outsz)
1034 1.1 skrll {
1035 1.1 skrll OPTR opt;
1036 1.1 skrll
1037 1.1 skrll (*input_swap->swap_opt_in) (input_bfd, (PTR) in, &opt);
1038 1.1 skrll (*output_swap->swap_opt_out) (output_bfd, &opt, (PTR) out);
1039 1.1 skrll }
1040 1.1 skrll }
1041 1.1 skrll
1042 1.1 skrll fdr.ipdFirst = output_symhdr->ipdMax;
1043 1.1 skrll output_symhdr->ipdMax += fdr.cpd;
1044 1.1 skrll fdr.ioptBase = output_symhdr->ioptMax;
1045 1.1 skrll output_symhdr->ioptMax += fdr.copt;
1046 1.1 skrll
1047 1.1 skrll if (fdr.crfd <= 0)
1048 1.1 skrll {
1049 1.1 skrll /* Point this FDR at the table of RFD's we created. */
1050 1.1 skrll fdr.rfdBase = newrfdbase;
1051 1.1 skrll fdr.crfd = input_symhdr->ifdMax;
1052 1.1 skrll }
1053 1.1 skrll else
1054 1.1 skrll {
1055 1.1 skrll /* Point this FDR at the remapped RFD's. */
1056 1.1 skrll fdr.rfdBase += oldrfdbase;
1057 1.1 skrll }
1058 1.1 skrll
1059 1.1 skrll (*swap_fdr_out) (output_bfd, &fdr, fdr_out);
1060 1.1 skrll fdr_out += external_fdr_size;
1061 1.1 skrll ++output_symhdr->ifdMax;
1062 1.1 skrll }
1063 1.1 skrll
1064 1.1 skrll return TRUE;
1065 1.1 skrll }
1066 1.1 skrll
1067 1.1 skrll /* Add a string to the debugging information we are accumulating.
1068 1.1 skrll Return the offset from the fdr string base. */
1069 1.1 skrll
1070 1.1 skrll static long ecoff_add_string
1071 1.1 skrll PARAMS ((struct accumulate *, struct bfd_link_info *,
1072 1.1 skrll struct ecoff_debug_info *, FDR *fdr, const char *string));
1073 1.1 skrll
1074 1.1 skrll static long
1075 1.1 skrll ecoff_add_string (ainfo, info, debug, fdr, string)
1076 1.1 skrll struct accumulate *ainfo;
1077 1.1 skrll struct bfd_link_info *info;
1078 1.1 skrll struct ecoff_debug_info *debug;
1079 1.1 skrll FDR *fdr;
1080 1.1 skrll const char *string;
1081 1.1 skrll {
1082 1.1 skrll HDRR *symhdr;
1083 1.1 skrll size_t len;
1084 1.1 skrll bfd_size_type ret;
1085 1.1 skrll
1086 1.1 skrll symhdr = &debug->symbolic_header;
1087 1.1 skrll len = strlen (string);
1088 1.1 skrll if (info->relocatable)
1089 1.1 skrll {
1090 1.1 skrll if (!add_memory_shuffle (ainfo, &ainfo->ss, &ainfo->ss_end, (PTR) string,
1091 1.1 skrll len + 1))
1092 1.1 skrll return -1;
1093 1.1 skrll ret = symhdr->issMax;
1094 1.1 skrll symhdr->issMax += len + 1;
1095 1.1 skrll fdr->cbSs += len + 1;
1096 1.1 skrll }
1097 1.1 skrll else
1098 1.1 skrll {
1099 1.1 skrll struct string_hash_entry *sh;
1100 1.1 skrll
1101 1.1 skrll sh = string_hash_lookup (&ainfo->str_hash, string, TRUE, TRUE);
1102 1.1 skrll if (sh == (struct string_hash_entry *) NULL)
1103 1.1 skrll return -1;
1104 1.1 skrll if (sh->val == -1)
1105 1.1 skrll {
1106 1.1 skrll sh->val = symhdr->issMax;
1107 1.1 skrll symhdr->issMax += len + 1;
1108 1.1 skrll if (ainfo->ss_hash == (struct string_hash_entry *) NULL)
1109 1.1 skrll ainfo->ss_hash = sh;
1110 1.1 skrll if (ainfo->ss_hash_end
1111 1.1 skrll != (struct string_hash_entry *) NULL)
1112 1.1 skrll ainfo->ss_hash_end->next = sh;
1113 1.1 skrll ainfo->ss_hash_end = sh;
1114 1.1 skrll }
1115 1.1 skrll ret = sh->val;
1116 1.1 skrll }
1117 1.1 skrll
1118 1.1 skrll return ret;
1119 1.1 skrll }
1120 1.1 skrll
1121 1.1 skrll /* Add debugging information from a non-ECOFF file. */
1122 1.1 skrll
1123 1.1 skrll bfd_boolean
1124 1.1 skrll bfd_ecoff_debug_accumulate_other (handle, output_bfd, output_debug,
1125 1.1 skrll output_swap, input_bfd, info)
1126 1.1 skrll PTR handle;
1127 1.1 skrll bfd *output_bfd;
1128 1.1 skrll struct ecoff_debug_info *output_debug;
1129 1.1 skrll const struct ecoff_debug_swap *output_swap;
1130 1.1 skrll bfd *input_bfd;
1131 1.1 skrll struct bfd_link_info *info;
1132 1.1 skrll {
1133 1.1 skrll struct accumulate *ainfo = (struct accumulate *) handle;
1134 1.1 skrll void (* const swap_sym_out) PARAMS ((bfd *, const SYMR *, PTR))
1135 1.1 skrll = output_swap->swap_sym_out;
1136 1.1 skrll HDRR *output_symhdr = &output_debug->symbolic_header;
1137 1.1 skrll FDR fdr;
1138 1.1 skrll asection *sec;
1139 1.1 skrll asymbol **symbols;
1140 1.1 skrll asymbol **sym_ptr;
1141 1.1 skrll asymbol **sym_end;
1142 1.1 skrll long symsize;
1143 1.1 skrll long symcount;
1144 1.1 skrll PTR external_fdr;
1145 1.1 skrll
1146 1.1 skrll memset ((PTR) &fdr, 0, sizeof fdr);
1147 1.1 skrll
1148 1.1 skrll sec = bfd_get_section_by_name (input_bfd, ".text");
1149 1.1 skrll if (sec != NULL)
1150 1.1 skrll fdr.adr = sec->output_section->vma + sec->output_offset;
1151 1.1 skrll else
1152 1.1 skrll {
1153 1.1 skrll /* FIXME: What about .init or .fini? */
1154 1.1 skrll fdr.adr = 0;
1155 1.1 skrll }
1156 1.1 skrll
1157 1.1 skrll fdr.issBase = output_symhdr->issMax;
1158 1.1 skrll fdr.cbSs = 0;
1159 1.1 skrll fdr.rss = ecoff_add_string (ainfo, info, output_debug, &fdr,
1160 1.1 skrll input_bfd->filename);
1161 1.1 skrll if (fdr.rss == -1)
1162 1.1 skrll return FALSE;
1163 1.1 skrll fdr.isymBase = output_symhdr->isymMax;
1164 1.1 skrll
1165 1.1 skrll /* Get the local symbols from the input BFD. */
1166 1.1 skrll symsize = bfd_get_symtab_upper_bound (input_bfd);
1167 1.1 skrll if (symsize < 0)
1168 1.1 skrll return FALSE;
1169 1.1 skrll symbols = (asymbol **) bfd_alloc (output_bfd, (bfd_size_type) symsize);
1170 1.1 skrll if (symbols == (asymbol **) NULL)
1171 1.1 skrll return FALSE;
1172 1.1 skrll symcount = bfd_canonicalize_symtab (input_bfd, symbols);
1173 1.1 skrll if (symcount < 0)
1174 1.1 skrll return FALSE;
1175 1.1 skrll sym_end = symbols + symcount;
1176 1.1 skrll
1177 1.1 skrll /* Handle the local symbols. Any external symbols are handled
1178 1.1 skrll separately. */
1179 1.1 skrll fdr.csym = 0;
1180 1.1 skrll for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
1181 1.1 skrll {
1182 1.1 skrll SYMR internal_sym;
1183 1.1 skrll PTR external_sym;
1184 1.1 skrll
1185 1.1 skrll if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
1186 1.1 skrll continue;
1187 1.1 skrll memset ((PTR) &internal_sym, 0, sizeof internal_sym);
1188 1.1 skrll internal_sym.iss = ecoff_add_string (ainfo, info, output_debug, &fdr,
1189 1.1 skrll (*sym_ptr)->name);
1190 1.1 skrll
1191 1.1 skrll if (internal_sym.iss == -1)
1192 1.1 skrll return FALSE;
1193 1.1 skrll if (bfd_is_com_section ((*sym_ptr)->section)
1194 1.1 skrll || bfd_is_und_section ((*sym_ptr)->section))
1195 1.1 skrll internal_sym.value = (*sym_ptr)->value;
1196 1.1 skrll else
1197 1.1 skrll internal_sym.value = ((*sym_ptr)->value
1198 1.1 skrll + (*sym_ptr)->section->output_offset
1199 1.1 skrll + (*sym_ptr)->section->output_section->vma);
1200 1.1 skrll internal_sym.st = stNil;
1201 1.1 skrll internal_sym.sc = scUndefined;
1202 1.1 skrll internal_sym.index = indexNil;
1203 1.1 skrll
1204 1.1 skrll external_sym = (PTR) objalloc_alloc (ainfo->memory,
1205 1.1 skrll output_swap->external_sym_size);
1206 1.1 skrll if (!external_sym)
1207 1.1 skrll {
1208 1.1 skrll bfd_set_error (bfd_error_no_memory);
1209 1.1 skrll return FALSE;
1210 1.1 skrll }
1211 1.1 skrll (*swap_sym_out) (output_bfd, &internal_sym, external_sym);
1212 1.1 skrll add_memory_shuffle (ainfo, &ainfo->sym, &ainfo->sym_end,
1213 1.1 skrll external_sym,
1214 1.1 skrll (unsigned long) output_swap->external_sym_size);
1215 1.1 skrll ++fdr.csym;
1216 1.1 skrll ++output_symhdr->isymMax;
1217 1.1 skrll }
1218 1.1 skrll
1219 1.1 skrll bfd_release (output_bfd, (PTR) symbols);
1220 1.1 skrll
1221 1.1 skrll /* Leave everything else in the FDR zeroed out. This will cause
1222 1.1 skrll the lang field to be langC. The fBigendian field will
1223 1.1 skrll indicate little endian format, but it doesn't matter because
1224 1.1 skrll it only applies to aux fields and there are none. */
1225 1.1 skrll external_fdr = (PTR) objalloc_alloc (ainfo->memory,
1226 1.1 skrll output_swap->external_fdr_size);
1227 1.1 skrll if (!external_fdr)
1228 1.1 skrll {
1229 1.1 skrll bfd_set_error (bfd_error_no_memory);
1230 1.1 skrll return FALSE;
1231 1.1 skrll }
1232 1.1 skrll (*output_swap->swap_fdr_out) (output_bfd, &fdr, external_fdr);
1233 1.1 skrll add_memory_shuffle (ainfo, &ainfo->fdr, &ainfo->fdr_end,
1234 1.1 skrll external_fdr,
1235 1.1 skrll (unsigned long) output_swap->external_fdr_size);
1236 1.1 skrll
1237 1.1 skrll ++output_symhdr->ifdMax;
1238 1.1 skrll
1239 1.1 skrll return TRUE;
1240 1.1 skrll }
1241 1.1 skrll
1242 1.1 skrll /* Set up ECOFF debugging information for the external symbols.
1243 1.1 skrll FIXME: This is done using a memory buffer, but it should be
1244 1.1 skrll probably be changed to use a shuffle structure. The assembler uses
1245 1.1 skrll this interface, so that must be changed to do something else. */
1246 1.1 skrll
1247 1.1 skrll bfd_boolean
1248 1.1 skrll bfd_ecoff_debug_externals (abfd, debug, swap, relocatable, get_extr,
1249 1.1 skrll set_index)
1250 1.1 skrll bfd *abfd;
1251 1.1 skrll struct ecoff_debug_info *debug;
1252 1.1 skrll const struct ecoff_debug_swap *swap;
1253 1.1 skrll bfd_boolean relocatable;
1254 1.1 skrll bfd_boolean (*get_extr) PARAMS ((asymbol *, EXTR *));
1255 1.1 skrll void (*set_index) PARAMS ((asymbol *, bfd_size_type));
1256 1.1 skrll {
1257 1.1 skrll HDRR * const symhdr = &debug->symbolic_header;
1258 1.1 skrll asymbol **sym_ptr_ptr;
1259 1.1 skrll size_t c;
1260 1.1 skrll
1261 1.1 skrll sym_ptr_ptr = bfd_get_outsymbols (abfd);
1262 1.1 skrll if (sym_ptr_ptr == NULL)
1263 1.1 skrll return TRUE;
1264 1.1 skrll
1265 1.1 skrll for (c = bfd_get_symcount (abfd); c > 0; c--, sym_ptr_ptr++)
1266 1.1 skrll {
1267 1.1 skrll asymbol *sym_ptr;
1268 1.1 skrll EXTR esym;
1269 1.1 skrll
1270 1.1 skrll sym_ptr = *sym_ptr_ptr;
1271 1.1 skrll
1272 1.1 skrll /* Get the external symbol information. */
1273 1.1 skrll if (! (*get_extr) (sym_ptr, &esym))
1274 1.1 skrll continue;
1275 1.1 skrll
1276 1.1 skrll /* If we're producing an executable, move common symbols into
1277 1.1 skrll bss. */
1278 1.1 skrll if (! relocatable)
1279 1.1 skrll {
1280 1.1 skrll if (esym.asym.sc == scCommon)
1281 1.1 skrll esym.asym.sc = scBss;
1282 1.1 skrll else if (esym.asym.sc == scSCommon)
1283 1.1 skrll esym.asym.sc = scSBss;
1284 1.1 skrll }
1285 1.1 skrll
1286 1.1 skrll if (bfd_is_com_section (sym_ptr->section)
1287 1.1 skrll || bfd_is_und_section (sym_ptr->section)
1288 1.1 skrll || sym_ptr->section->output_section == (asection *) NULL)
1289 1.1 skrll {
1290 1.1 skrll /* FIXME: gas does not keep the value of a small undefined
1291 1.1 skrll symbol in the symbol itself, because of relocation
1292 1.1 skrll problems. */
1293 1.1 skrll if (esym.asym.sc != scSUndefined
1294 1.1 skrll || esym.asym.value == 0
1295 1.1 skrll || sym_ptr->value != 0)
1296 1.1 skrll esym.asym.value = sym_ptr->value;
1297 1.1 skrll }
1298 1.1 skrll else
1299 1.1 skrll esym.asym.value = (sym_ptr->value
1300 1.1 skrll + sym_ptr->section->output_offset
1301 1.1 skrll + sym_ptr->section->output_section->vma);
1302 1.1 skrll
1303 1.1 skrll if (set_index)
1304 1.1 skrll (*set_index) (sym_ptr, (bfd_size_type) symhdr->iextMax);
1305 1.1 skrll
1306 1.1 skrll if (! bfd_ecoff_debug_one_external (abfd, debug, swap,
1307 1.1 skrll sym_ptr->name, &esym))
1308 1.1 skrll return FALSE;
1309 1.1 skrll }
1310 1.1 skrll
1311 1.1 skrll return TRUE;
1312 1.1 skrll }
1313 1.1 skrll
1314 1.1 skrll /* Add a single external symbol to the debugging information. */
1315 1.1 skrll
1316 1.1 skrll bfd_boolean
1317 1.1 skrll bfd_ecoff_debug_one_external (abfd, debug, swap, name, esym)
1318 1.1 skrll bfd *abfd;
1319 1.1 skrll struct ecoff_debug_info *debug;
1320 1.1 skrll const struct ecoff_debug_swap *swap;
1321 1.1 skrll const char *name;
1322 1.1 skrll EXTR *esym;
1323 1.1 skrll {
1324 1.1 skrll const bfd_size_type external_ext_size = swap->external_ext_size;
1325 1.1 skrll void (* const swap_ext_out) PARAMS ((bfd *, const EXTR *, PTR))
1326 1.1 skrll = swap->swap_ext_out;
1327 1.1 skrll HDRR * const symhdr = &debug->symbolic_header;
1328 1.1 skrll size_t namelen;
1329 1.1 skrll
1330 1.1 skrll namelen = strlen (name);
1331 1.1 skrll
1332 1.1 skrll if ((size_t) (debug->ssext_end - debug->ssext)
1333 1.1 skrll < symhdr->issExtMax + namelen + 1)
1334 1.1 skrll {
1335 1.1 skrll if (! ecoff_add_bytes ((char **) &debug->ssext,
1336 1.1 skrll (char **) &debug->ssext_end,
1337 1.1 skrll symhdr->issExtMax + namelen + 1))
1338 1.1 skrll return FALSE;
1339 1.1 skrll }
1340 1.1 skrll if ((size_t) ((char *) debug->external_ext_end
1341 1.1 skrll - (char *) debug->external_ext)
1342 1.1 skrll < (symhdr->iextMax + 1) * external_ext_size)
1343 1.1 skrll {
1344 1.1 skrll char *external_ext = debug->external_ext;
1345 1.1 skrll char *external_ext_end = debug->external_ext_end;
1346 1.1 skrll if (! ecoff_add_bytes ((char **) &external_ext,
1347 1.1 skrll (char **) &external_ext_end,
1348 1.1 skrll (symhdr->iextMax + 1) * (size_t) external_ext_size))
1349 1.1 skrll return FALSE;
1350 1.1 skrll debug->external_ext = external_ext;
1351 1.1 skrll debug->external_ext_end = external_ext_end;
1352 1.1 skrll }
1353 1.1 skrll
1354 1.1 skrll esym->asym.iss = symhdr->issExtMax;
1355 1.1 skrll
1356 1.1 skrll (*swap_ext_out) (abfd, esym,
1357 1.1 skrll ((char *) debug->external_ext
1358 1.1 skrll + symhdr->iextMax * swap->external_ext_size));
1359 1.1 skrll
1360 1.1 skrll ++symhdr->iextMax;
1361 1.1 skrll
1362 1.1 skrll strcpy (debug->ssext + symhdr->issExtMax, name);
1363 1.1 skrll symhdr->issExtMax += namelen + 1;
1364 1.1 skrll
1365 1.1 skrll return TRUE;
1366 1.1 skrll }
1367 1.1 skrll
1368 1.1 skrll /* Align the ECOFF debugging information. */
1369 1.1 skrll
1370 1.1 skrll static void
1371 1.1 skrll ecoff_align_debug (abfd, debug, swap)
1372 1.1 skrll bfd *abfd ATTRIBUTE_UNUSED;
1373 1.1 skrll struct ecoff_debug_info *debug;
1374 1.1 skrll const struct ecoff_debug_swap *swap;
1375 1.1 skrll {
1376 1.1 skrll HDRR * const symhdr = &debug->symbolic_header;
1377 1.1 skrll bfd_size_type debug_align, aux_align, rfd_align;
1378 1.1 skrll size_t add;
1379 1.1 skrll
1380 1.1 skrll /* Adjust the counts so that structures are aligned. */
1381 1.1 skrll debug_align = swap->debug_align;
1382 1.1 skrll aux_align = debug_align / sizeof (union aux_ext);
1383 1.1 skrll rfd_align = debug_align / swap->external_rfd_size;
1384 1.1 skrll
1385 1.1 skrll add = debug_align - (symhdr->cbLine & (debug_align - 1));
1386 1.1 skrll if (add != debug_align)
1387 1.1 skrll {
1388 1.1 skrll if (debug->line != (unsigned char *) NULL)
1389 1.1 skrll memset ((PTR) (debug->line + symhdr->cbLine), 0, add);
1390 1.1 skrll symhdr->cbLine += add;
1391 1.1 skrll }
1392 1.1 skrll
1393 1.1 skrll add = debug_align - (symhdr->issMax & (debug_align - 1));
1394 1.1 skrll if (add != debug_align)
1395 1.1 skrll {
1396 1.1 skrll if (debug->ss != (char *) NULL)
1397 1.1 skrll memset ((PTR) (debug->ss + symhdr->issMax), 0, add);
1398 1.1 skrll symhdr->issMax += add;
1399 1.1 skrll }
1400 1.1 skrll
1401 1.1 skrll add = debug_align - (symhdr->issExtMax & (debug_align - 1));
1402 1.1 skrll if (add != debug_align)
1403 1.1 skrll {
1404 1.1 skrll if (debug->ssext != (char *) NULL)
1405 1.1 skrll memset ((PTR) (debug->ssext + symhdr->issExtMax), 0, add);
1406 1.1 skrll symhdr->issExtMax += add;
1407 1.1 skrll }
1408 1.1 skrll
1409 1.1 skrll add = aux_align - (symhdr->iauxMax & (aux_align - 1));
1410 1.1 skrll if (add != aux_align)
1411 1.1 skrll {
1412 1.1 skrll if (debug->external_aux != (union aux_ext *) NULL)
1413 1.1 skrll memset ((PTR) (debug->external_aux + symhdr->iauxMax), 0,
1414 1.1 skrll add * sizeof (union aux_ext));
1415 1.1 skrll symhdr->iauxMax += add;
1416 1.1 skrll }
1417 1.1 skrll
1418 1.1 skrll add = rfd_align - (symhdr->crfd & (rfd_align - 1));
1419 1.1 skrll if (add != rfd_align)
1420 1.1 skrll {
1421 1.1 skrll if (debug->external_rfd != (PTR) NULL)
1422 1.1 skrll memset ((PTR) ((char *) debug->external_rfd
1423 1.1 skrll + symhdr->crfd * swap->external_rfd_size),
1424 1.1 skrll 0, (size_t) (add * swap->external_rfd_size));
1425 1.1 skrll symhdr->crfd += add;
1426 1.1 skrll }
1427 1.1 skrll }
1428 1.1 skrll
1429 1.1 skrll /* Return the size required by the ECOFF debugging information. */
1430 1.1 skrll
1431 1.1 skrll bfd_size_type
1432 1.1 skrll bfd_ecoff_debug_size (abfd, debug, swap)
1433 1.1 skrll bfd *abfd;
1434 1.1 skrll struct ecoff_debug_info *debug;
1435 1.1 skrll const struct ecoff_debug_swap *swap;
1436 1.1 skrll {
1437 1.1 skrll bfd_size_type tot;
1438 1.1 skrll
1439 1.1 skrll ecoff_align_debug (abfd, debug, swap);
1440 1.1 skrll tot = swap->external_hdr_size;
1441 1.1 skrll
1442 1.1 skrll #define ADD(count, size) \
1443 1.1 skrll tot += debug->symbolic_header.count * size
1444 1.1 skrll
1445 1.1 skrll ADD (cbLine, sizeof (unsigned char));
1446 1.1 skrll ADD (idnMax, swap->external_dnr_size);
1447 1.1 skrll ADD (ipdMax, swap->external_pdr_size);
1448 1.1 skrll ADD (isymMax, swap->external_sym_size);
1449 1.1 skrll ADD (ioptMax, swap->external_opt_size);
1450 1.1 skrll ADD (iauxMax, sizeof (union aux_ext));
1451 1.1 skrll ADD (issMax, sizeof (char));
1452 1.1 skrll ADD (issExtMax, sizeof (char));
1453 1.1 skrll ADD (ifdMax, swap->external_fdr_size);
1454 1.1 skrll ADD (crfd, swap->external_rfd_size);
1455 1.1 skrll ADD (iextMax, swap->external_ext_size);
1456 1.1 skrll
1457 1.1 skrll #undef ADD
1458 1.1 skrll
1459 1.1 skrll return tot;
1460 1.1 skrll }
1461 1.1 skrll
1462 1.1 skrll /* Write out the ECOFF symbolic header, given the file position it is
1463 1.1 skrll going to be placed at. This assumes that the counts are set
1464 1.1 skrll correctly. */
1465 1.1 skrll
1466 1.1 skrll static bfd_boolean
1467 1.1 skrll ecoff_write_symhdr (abfd, debug, swap, where)
1468 1.1 skrll bfd *abfd;
1469 1.1 skrll struct ecoff_debug_info *debug;
1470 1.1 skrll const struct ecoff_debug_swap *swap;
1471 1.1 skrll file_ptr where;
1472 1.1 skrll {
1473 1.1 skrll HDRR * const symhdr = &debug->symbolic_header;
1474 1.1 skrll char *buff = NULL;
1475 1.1 skrll
1476 1.1 skrll ecoff_align_debug (abfd, debug, swap);
1477 1.1 skrll
1478 1.1 skrll /* Go to the right location in the file. */
1479 1.1 skrll if (bfd_seek (abfd, where, SEEK_SET) != 0)
1480 1.1 skrll return FALSE;
1481 1.1 skrll
1482 1.1 skrll where += swap->external_hdr_size;
1483 1.1 skrll
1484 1.1 skrll symhdr->magic = swap->sym_magic;
1485 1.1 skrll
1486 1.1 skrll /* Fill in the file offsets. */
1487 1.1 skrll #define SET(offset, count, size) \
1488 1.1 skrll if (symhdr->count == 0) \
1489 1.1 skrll symhdr->offset = 0; \
1490 1.1 skrll else \
1491 1.1 skrll { \
1492 1.1 skrll symhdr->offset = where; \
1493 1.1 skrll where += symhdr->count * size; \
1494 1.1 skrll }
1495 1.1 skrll
1496 1.1 skrll SET (cbLineOffset, cbLine, sizeof (unsigned char));
1497 1.1 skrll SET (cbDnOffset, idnMax, swap->external_dnr_size);
1498 1.1 skrll SET (cbPdOffset, ipdMax, swap->external_pdr_size);
1499 1.1 skrll SET (cbSymOffset, isymMax, swap->external_sym_size);
1500 1.1 skrll SET (cbOptOffset, ioptMax, swap->external_opt_size);
1501 1.1 skrll SET (cbAuxOffset, iauxMax, sizeof (union aux_ext));
1502 1.1 skrll SET (cbSsOffset, issMax, sizeof (char));
1503 1.1 skrll SET (cbSsExtOffset, issExtMax, sizeof (char));
1504 1.1 skrll SET (cbFdOffset, ifdMax, swap->external_fdr_size);
1505 1.1 skrll SET (cbRfdOffset, crfd, swap->external_rfd_size);
1506 1.1 skrll SET (cbExtOffset, iextMax, swap->external_ext_size);
1507 1.1 skrll #undef SET
1508 1.1 skrll
1509 1.1 skrll buff = (PTR) bfd_malloc (swap->external_hdr_size);
1510 1.1 skrll if (buff == NULL && swap->external_hdr_size != 0)
1511 1.1 skrll goto error_return;
1512 1.1 skrll
1513 1.1 skrll (*swap->swap_hdr_out) (abfd, symhdr, buff);
1514 1.1 skrll if (bfd_bwrite (buff, swap->external_hdr_size, abfd)
1515 1.1 skrll != swap->external_hdr_size)
1516 1.1 skrll goto error_return;
1517 1.1 skrll
1518 1.1 skrll if (buff != NULL)
1519 1.1 skrll free (buff);
1520 1.1 skrll return TRUE;
1521 1.1 skrll error_return:
1522 1.1 skrll if (buff != NULL)
1523 1.1 skrll free (buff);
1524 1.1 skrll return FALSE;
1525 1.1 skrll }
1526 1.1 skrll
1527 1.1 skrll /* Write out the ECOFF debugging information. This function assumes
1528 1.1 skrll that the information (the pointers and counts) in *DEBUG have been
1529 1.1 skrll set correctly. WHERE is the position in the file to write the
1530 1.1 skrll information to. This function fills in the file offsets in the
1531 1.1 skrll symbolic header. */
1532 1.1 skrll
1533 1.1 skrll bfd_boolean
1534 1.1 skrll bfd_ecoff_write_debug (abfd, debug, swap, where)
1535 1.1 skrll bfd *abfd;
1536 1.1 skrll struct ecoff_debug_info *debug;
1537 1.1 skrll const struct ecoff_debug_swap *swap;
1538 1.1 skrll file_ptr where;
1539 1.1 skrll {
1540 1.1 skrll HDRR * const symhdr = &debug->symbolic_header;
1541 1.1 skrll
1542 1.1 skrll if (! ecoff_write_symhdr (abfd, debug, swap, where))
1543 1.1 skrll return FALSE;
1544 1.1 skrll
1545 1.1 skrll #define WRITE(ptr, count, size, offset) \
1546 1.1 skrll BFD_ASSERT (symhdr->offset == 0 \
1547 1.1 skrll || (bfd_vma) bfd_tell (abfd) == symhdr->offset); \
1548 1.1 skrll if (bfd_bwrite ((PTR) debug->ptr, (bfd_size_type) size * symhdr->count, abfd)\
1549 1.1 skrll != size * symhdr->count) \
1550 1.1 skrll return FALSE;
1551 1.1 skrll
1552 1.1 skrll WRITE (line, cbLine, sizeof (unsigned char), cbLineOffset);
1553 1.1 skrll WRITE (external_dnr, idnMax, swap->external_dnr_size, cbDnOffset);
1554 1.1 skrll WRITE (external_pdr, ipdMax, swap->external_pdr_size, cbPdOffset);
1555 1.1 skrll WRITE (external_sym, isymMax, swap->external_sym_size, cbSymOffset);
1556 1.1 skrll WRITE (external_opt, ioptMax, swap->external_opt_size, cbOptOffset);
1557 1.1 skrll WRITE (external_aux, iauxMax, (bfd_size_type) sizeof (union aux_ext),
1558 1.1 skrll cbAuxOffset);
1559 1.1 skrll WRITE (ss, issMax, sizeof (char), cbSsOffset);
1560 1.1 skrll WRITE (ssext, issExtMax, sizeof (char), cbSsExtOffset);
1561 1.1 skrll WRITE (external_fdr, ifdMax, swap->external_fdr_size, cbFdOffset);
1562 1.1 skrll WRITE (external_rfd, crfd, swap->external_rfd_size, cbRfdOffset);
1563 1.1 skrll WRITE (external_ext, iextMax, swap->external_ext_size, cbExtOffset);
1564 1.1 skrll #undef WRITE
1565 1.1 skrll
1566 1.1 skrll return TRUE;
1567 1.1 skrll }
1568 1.1 skrll
1569 1.1 skrll /* Write out a shuffle list. */
1570 1.1 skrll
1571 1.1 skrll static bfd_boolean ecoff_write_shuffle
1572 1.1 skrll PARAMS ((bfd *, const struct ecoff_debug_swap *, struct shuffle *,
1573 1.1 skrll PTR space));
1574 1.1 skrll
1575 1.1 skrll static bfd_boolean
1576 1.1 skrll ecoff_write_shuffle (abfd, swap, shuffle, space)
1577 1.1 skrll bfd *abfd;
1578 1.1 skrll const struct ecoff_debug_swap *swap;
1579 1.1 skrll struct shuffle *shuffle;
1580 1.1 skrll PTR space;
1581 1.1 skrll {
1582 1.1 skrll register struct shuffle *l;
1583 1.1 skrll unsigned long total;
1584 1.1 skrll
1585 1.1 skrll total = 0;
1586 1.1 skrll for (l = shuffle; l != (struct shuffle *) NULL; l = l->next)
1587 1.1 skrll {
1588 1.1 skrll if (! l->filep)
1589 1.1 skrll {
1590 1.1 skrll if (bfd_bwrite (l->u.memory, (bfd_size_type) l->size, abfd)
1591 1.1 skrll != l->size)
1592 1.1 skrll return FALSE;
1593 1.1 skrll }
1594 1.1 skrll else
1595 1.1 skrll {
1596 1.1 skrll if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
1597 1.1 skrll || bfd_bread (space, (bfd_size_type) l->size,
1598 1.1 skrll l->u.file.input_bfd) != l->size
1599 1.1 skrll || bfd_bwrite (space, (bfd_size_type) l->size, abfd) != l->size)
1600 1.1 skrll return FALSE;
1601 1.1 skrll }
1602 1.1 skrll total += l->size;
1603 1.1 skrll }
1604 1.1 skrll
1605 1.1 skrll if ((total & (swap->debug_align - 1)) != 0)
1606 1.1 skrll {
1607 1.1 skrll unsigned int i;
1608 1.1 skrll bfd_byte *s;
1609 1.1 skrll
1610 1.1 skrll i = swap->debug_align - (total & (swap->debug_align - 1));
1611 1.1 skrll s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
1612 1.1 skrll if (s == NULL && i != 0)
1613 1.1 skrll return FALSE;
1614 1.1 skrll
1615 1.1 skrll if (bfd_bwrite ((PTR) s, (bfd_size_type) i, abfd) != i)
1616 1.1 skrll {
1617 1.1 skrll free (s);
1618 1.1 skrll return FALSE;
1619 1.1 skrll }
1620 1.1 skrll free (s);
1621 1.1 skrll }
1622 1.1 skrll
1623 1.1 skrll return TRUE;
1624 1.1 skrll }
1625 1.1 skrll
1626 1.1 skrll /* Write out debugging information using accumulated linker
1627 1.1 skrll information. */
1628 1.1 skrll
1629 1.1 skrll bfd_boolean
1630 1.1 skrll bfd_ecoff_write_accumulated_debug (handle, abfd, debug, swap, info, where)
1631 1.1 skrll PTR handle;
1632 1.1 skrll bfd *abfd;
1633 1.1 skrll struct ecoff_debug_info *debug;
1634 1.1 skrll const struct ecoff_debug_swap *swap;
1635 1.1 skrll struct bfd_link_info *info;
1636 1.1 skrll file_ptr where;
1637 1.1 skrll {
1638 1.1 skrll struct accumulate *ainfo = (struct accumulate *) handle;
1639 1.1 skrll PTR space = NULL;
1640 1.1 skrll bfd_size_type amt;
1641 1.1 skrll
1642 1.1 skrll if (! ecoff_write_symhdr (abfd, debug, swap, where))
1643 1.1 skrll goto error_return;
1644 1.1 skrll
1645 1.1 skrll amt = ainfo->largest_file_shuffle;
1646 1.1 skrll space = (PTR) bfd_malloc (amt);
1647 1.1 skrll if (space == NULL && ainfo->largest_file_shuffle != 0)
1648 1.1 skrll goto error_return;
1649 1.1 skrll
1650 1.1 skrll if (! ecoff_write_shuffle (abfd, swap, ainfo->line, space)
1651 1.1 skrll || ! ecoff_write_shuffle (abfd, swap, ainfo->pdr, space)
1652 1.1 skrll || ! ecoff_write_shuffle (abfd, swap, ainfo->sym, space)
1653 1.1 skrll || ! ecoff_write_shuffle (abfd, swap, ainfo->opt, space)
1654 1.1 skrll || ! ecoff_write_shuffle (abfd, swap, ainfo->aux, space))
1655 1.1 skrll goto error_return;
1656 1.1 skrll
1657 1.1 skrll /* The string table is written out from the hash table if this is a
1658 1.1 skrll final link. */
1659 1.1 skrll if (info->relocatable)
1660 1.1 skrll {
1661 1.1 skrll BFD_ASSERT (ainfo->ss_hash == (struct string_hash_entry *) NULL);
1662 1.1 skrll if (! ecoff_write_shuffle (abfd, swap, ainfo->ss, space))
1663 1.1 skrll goto error_return;
1664 1.1 skrll }
1665 1.1 skrll else
1666 1.1 skrll {
1667 1.1 skrll unsigned long total;
1668 1.1 skrll bfd_byte null;
1669 1.1 skrll struct string_hash_entry *sh;
1670 1.1 skrll
1671 1.1 skrll BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
1672 1.1 skrll null = 0;
1673 1.1 skrll if (bfd_bwrite ((PTR) &null, (bfd_size_type) 1, abfd) != 1)
1674 1.1 skrll goto error_return;
1675 1.1 skrll total = 1;
1676 1.1 skrll BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
1677 1.1 skrll for (sh = ainfo->ss_hash;
1678 1.1 skrll sh != (struct string_hash_entry *) NULL;
1679 1.1 skrll sh = sh->next)
1680 1.1 skrll {
1681 1.1 skrll size_t len;
1682 1.1 skrll
1683 1.1 skrll len = strlen (sh->root.string);
1684 1.1 skrll amt = len + 1;
1685 1.1 skrll if (bfd_bwrite ((PTR) sh->root.string, amt, abfd) != amt)
1686 1.1 skrll goto error_return;
1687 1.1 skrll total += len + 1;
1688 1.1 skrll }
1689 1.1 skrll
1690 1.1 skrll if ((total & (swap->debug_align - 1)) != 0)
1691 1.1 skrll {
1692 1.1 skrll unsigned int i;
1693 1.1 skrll bfd_byte *s;
1694 1.1 skrll
1695 1.1 skrll i = swap->debug_align - (total & (swap->debug_align - 1));
1696 1.1 skrll s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
1697 1.1 skrll if (s == NULL && i != 0)
1698 1.1 skrll goto error_return;
1699 1.1 skrll
1700 1.1 skrll if (bfd_bwrite ((PTR) s, (bfd_size_type) i, abfd) != i)
1701 1.1 skrll {
1702 1.1 skrll free (s);
1703 1.1 skrll goto error_return;
1704 1.1 skrll }
1705 1.1 skrll free (s);
1706 1.1 skrll }
1707 1.1 skrll }
1708 1.1 skrll
1709 1.1 skrll /* The external strings and symbol are not converted over to using
1710 1.1 skrll shuffles. FIXME: They probably should be. */
1711 1.1 skrll amt = debug->symbolic_header.issExtMax;
1712 1.1 skrll if (bfd_bwrite (debug->ssext, amt, abfd) != amt)
1713 1.1 skrll goto error_return;
1714 1.1 skrll if ((debug->symbolic_header.issExtMax & (swap->debug_align - 1)) != 0)
1715 1.1 skrll {
1716 1.1 skrll unsigned int i;
1717 1.1 skrll bfd_byte *s;
1718 1.1 skrll
1719 1.1 skrll i = (swap->debug_align
1720 1.1 skrll - (debug->symbolic_header.issExtMax & (swap->debug_align - 1)));
1721 1.1 skrll s = (bfd_byte *) bfd_zmalloc ((bfd_size_type) i);
1722 1.1 skrll if (s == NULL && i != 0)
1723 1.1 skrll goto error_return;
1724 1.1 skrll
1725 1.1 skrll if (bfd_bwrite ((PTR) s, (bfd_size_type) i, abfd) != i)
1726 1.1 skrll {
1727 1.1 skrll free (s);
1728 1.1 skrll goto error_return;
1729 1.1 skrll }
1730 1.1 skrll free (s);
1731 1.1 skrll }
1732 1.1 skrll
1733 1.1 skrll if (! ecoff_write_shuffle (abfd, swap, ainfo->fdr, space)
1734 1.1 skrll || ! ecoff_write_shuffle (abfd, swap, ainfo->rfd, space))
1735 1.1 skrll goto error_return;
1736 1.1 skrll
1737 1.1 skrll BFD_ASSERT (debug->symbolic_header.cbExtOffset == 0
1738 1.1 skrll || (debug->symbolic_header.cbExtOffset
1739 1.1 skrll == (bfd_vma) bfd_tell (abfd)));
1740 1.1 skrll
1741 1.1 skrll amt = debug->symbolic_header.iextMax * swap->external_ext_size;
1742 1.1 skrll if (bfd_bwrite (debug->external_ext, amt, abfd) != amt)
1743 1.1 skrll goto error_return;
1744 1.1 skrll
1745 1.1 skrll if (space != NULL)
1746 1.1 skrll free (space);
1747 1.1 skrll return TRUE;
1748 1.1 skrll
1749 1.1 skrll error_return:
1750 1.1 skrll if (space != NULL)
1751 1.1 skrll free (space);
1752 1.1 skrll return FALSE;
1753 1.1 skrll }
1754 1.1 skrll
1755 1.1 skrll /* Handle the find_nearest_line function for both ECOFF and MIPS ELF
1757 1.1 skrll files. */
1758 1.1 skrll
1759 1.1 skrll /* Compare FDR entries. This is called via qsort. */
1760 1.1 skrll
1761 1.1 skrll static int
1762 1.1 skrll cmp_fdrtab_entry (leftp, rightp)
1763 1.1 skrll const PTR leftp;
1764 1.1 skrll const PTR rightp;
1765 1.1 skrll {
1766 1.1 skrll const struct ecoff_fdrtab_entry *lp =
1767 1.1 skrll (const struct ecoff_fdrtab_entry *) leftp;
1768 1.1 skrll const struct ecoff_fdrtab_entry *rp =
1769 1.1 skrll (const struct ecoff_fdrtab_entry *) rightp;
1770 1.1 skrll
1771 1.1 skrll if (lp->base_addr < rp->base_addr)
1772 1.1 skrll return -1;
1773 1.1 skrll if (lp->base_addr > rp->base_addr)
1774 1.1 skrll return 1;
1775 1.1 skrll return 0;
1776 1.1 skrll }
1777 1.1 skrll
1778 1.1 skrll /* Each file descriptor (FDR) has a memory address, to simplify
1779 1.1 skrll looking up an FDR by address, we build a table covering all FDRs
1780 1.1 skrll that have a least one procedure descriptor in them. The final
1781 1.1 skrll table will be sorted by address so we can look it up via binary
1782 1.1 skrll search. */
1783 1.1 skrll
1784 1.1 skrll static bfd_boolean
1785 1.1 skrll mk_fdrtab (abfd, debug_info, debug_swap, line_info)
1786 1.1 skrll bfd *abfd;
1787 1.1 skrll struct ecoff_debug_info * const debug_info;
1788 1.1 skrll const struct ecoff_debug_swap * const debug_swap;
1789 1.1 skrll struct ecoff_find_line *line_info;
1790 1.1 skrll {
1791 1.1 skrll struct ecoff_fdrtab_entry *tab;
1792 1.1 skrll FDR *fdr_ptr;
1793 1.1 skrll FDR *fdr_start;
1794 1.1 skrll FDR *fdr_end;
1795 1.1 skrll bfd_boolean stabs;
1796 1.1 skrll long len;
1797 1.1 skrll bfd_size_type amt;
1798 1.1 skrll
1799 1.1 skrll fdr_start = debug_info->fdr;
1800 1.1 skrll fdr_end = fdr_start + debug_info->symbolic_header.ifdMax;
1801 1.1 skrll
1802 1.1 skrll /* First, let's see how long the table needs to be. */
1803 1.1 skrll for (len = 0, fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1804 1.1 skrll {
1805 1.1 skrll if (fdr_ptr->cpd == 0) /* Skip FDRs that have no PDRs. */
1806 1.1 skrll continue;
1807 1.1 skrll ++len;
1808 1.1 skrll }
1809 1.1 skrll
1810 1.1 skrll /* Now, create and fill in the table. */
1811 1.1 skrll amt = (bfd_size_type) len * sizeof (struct ecoff_fdrtab_entry);
1812 1.1 skrll line_info->fdrtab = (struct ecoff_fdrtab_entry*) bfd_zalloc (abfd, amt);
1813 1.1 skrll if (line_info->fdrtab == NULL)
1814 1.1 skrll return FALSE;
1815 1.1 skrll line_info->fdrtab_len = len;
1816 1.1 skrll
1817 1.1 skrll tab = line_info->fdrtab;
1818 1.1 skrll for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1819 1.1 skrll {
1820 1.1 skrll if (fdr_ptr->cpd == 0)
1821 1.1 skrll continue;
1822 1.1 skrll
1823 1.1 skrll /* Check whether this file has stabs debugging information. In
1824 1.1 skrll a file with stabs debugging information, the second local
1825 1.1 skrll symbol is named @stabs. */
1826 1.1 skrll stabs = FALSE;
1827 1.1 skrll if (fdr_ptr->csym >= 2)
1828 1.1 skrll {
1829 1.1 skrll char *sym_ptr;
1830 1.1 skrll SYMR sym;
1831 1.1 skrll
1832 1.1 skrll sym_ptr = ((char *) debug_info->external_sym
1833 1.1 skrll + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
1834 1.1 skrll (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
1835 1.1 skrll if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
1836 1.1 skrll STABS_SYMBOL) == 0)
1837 1.1 skrll stabs = TRUE;
1838 1.1 skrll }
1839 1.1 skrll
1840 1.1 skrll if (!stabs)
1841 1.1 skrll {
1842 1.1 skrll /* eraxxon: There are at least two problems with this computation:
1843 1.1 skrll 1) PDRs do *not* contain offsets but full vma's; and typically the
1844 1.1 skrll address of the first PDR is the address of the FDR, which will
1845 1.1 skrll make (most) of the results of the original computation 0!
1846 1.1 skrll 2) Once in a wacky while, the Compaq compiler generated PDR
1847 1.1 skrll addresses do not equal the FDR vma, but they (the PDR address)
1848 1.1 skrll are still vma's and not offsets. Cf. comments in
1849 1.1 skrll 'lookup_line'. */
1850 1.1 skrll /* The address of the first PDR is the offset of that
1851 1.1 skrll procedure relative to the beginning of file FDR. */
1852 1.1 skrll tab->base_addr = fdr_ptr->adr;
1853 1.1 skrll }
1854 1.1 skrll else
1855 1.1 skrll {
1856 1.1 skrll /* XXX I don't know about stabs, so this is a guess
1857 1.1 skrll (davidm (at) cs.arizona.edu). */
1858 1.1 skrll tab->base_addr = fdr_ptr->adr;
1859 1.1 skrll }
1860 1.1 skrll tab->fdr = fdr_ptr;
1861 1.1 skrll ++tab;
1862 1.1 skrll }
1863 1.1 skrll
1864 1.1 skrll /* Finally, the table is sorted in increasing memory-address order.
1865 1.1 skrll The table is mostly sorted already, but there are cases (e.g.,
1866 1.1 skrll static functions in include files), where this does not hold.
1867 1.1 skrll Use "odump -PFv" to verify... */
1868 1.1 skrll qsort ((PTR) line_info->fdrtab, (size_t) len,
1869 1.1 skrll sizeof (struct ecoff_fdrtab_entry), cmp_fdrtab_entry);
1870 1.1 skrll
1871 1.1 skrll return TRUE;
1872 1.1 skrll }
1873 1.1 skrll
1874 1.1 skrll /* Return index of first FDR that covers to OFFSET. */
1875 1.1 skrll
1876 1.1 skrll static long
1877 1.1 skrll fdrtab_lookup (line_info, offset)
1878 1.1 skrll struct ecoff_find_line *line_info;
1879 1.1 skrll bfd_vma offset;
1880 1.1 skrll {
1881 1.1 skrll long low, high, len;
1882 1.1 skrll long mid = -1;
1883 1.1 skrll struct ecoff_fdrtab_entry *tab;
1884 1.1 skrll
1885 1.1 skrll len = line_info->fdrtab_len;
1886 1.1 skrll if (len == 0)
1887 1.1 skrll return -1;
1888 1.1 skrll
1889 1.1 skrll tab = line_info->fdrtab;
1890 1.1 skrll for (low = 0, high = len - 1 ; low != high ;)
1891 1.1 skrll {
1892 1.1 skrll mid = (high + low) / 2;
1893 1.1 skrll if (offset >= tab[mid].base_addr && offset < tab[mid + 1].base_addr)
1894 1.1 skrll goto find_min;
1895 1.1 skrll
1896 1.1 skrll if (tab[mid].base_addr > offset)
1897 1.1 skrll high = mid;
1898 1.1 skrll else
1899 1.1 skrll low = mid + 1;
1900 1.1 skrll }
1901 1.1 skrll
1902 1.1 skrll /* eraxxon: at this point 'offset' is either lower than the lowest entry or
1903 1.1 skrll higher than the highest entry. In the former case high = low = mid = 0;
1904 1.1 skrll we want to return -1. In the latter case, low = high and mid = low - 1;
1905 1.1 skrll we want to return the index of the highest entry. Only in former case
1906 1.1 skrll will the following 'catch-all' test be true. */
1907 1.1 skrll ++mid;
1908 1.1 skrll
1909 1.1 skrll /* Last entry is catch-all for all higher addresses. */
1910 1.1 skrll if (offset < tab[mid].base_addr)
1911 1.1 skrll return -1;
1912 1.1 skrll
1913 1.1 skrll find_min:
1914 1.1 skrll
1915 1.1 skrll /* eraxxon: There may be multiple FDRs in the table with the
1916 1.1 skrll same base_addr; make sure that we are at the first one. */
1917 1.1 skrll while (mid > 0 && tab[mid - 1].base_addr == tab[mid].base_addr)
1918 1.1 skrll --mid;
1919 1.1 skrll
1920 1.1 skrll return mid;
1921 1.1 skrll }
1922 1.1 skrll
1923 1.1 skrll /* Look up a line given an address, storing the information in
1924 1.1 skrll LINE_INFO->cache. */
1925 1.1 skrll
1926 1.1 skrll static bfd_boolean
1927 1.1 skrll lookup_line (abfd, debug_info, debug_swap, line_info)
1928 1.1 skrll bfd *abfd;
1929 1.1 skrll struct ecoff_debug_info * const debug_info;
1930 1.1 skrll const struct ecoff_debug_swap * const debug_swap;
1931 1.1 skrll struct ecoff_find_line *line_info;
1932 1.1 skrll {
1933 1.1 skrll struct ecoff_fdrtab_entry *tab;
1934 1.1 skrll bfd_vma offset;
1935 1.1 skrll bfd_boolean stabs;
1936 1.1 skrll FDR *fdr_ptr;
1937 1.1 skrll int i;
1938 1.1 skrll
1939 1.1 skrll /* eraxxon: note that 'offset' is the full vma, not a section offset. */
1940 1.1 skrll offset = line_info->cache.start;
1941 1.1 skrll
1942 1.1 skrll /* Build FDR table (sorted by object file's base-address) if we
1943 1.1 skrll don't have it already. */
1944 1.1 skrll if (line_info->fdrtab == NULL
1945 1.1 skrll && !mk_fdrtab (abfd, debug_info, debug_swap, line_info))
1946 1.1 skrll return FALSE;
1947 1.1 skrll
1948 1.1 skrll tab = line_info->fdrtab;
1949 1.1 skrll
1950 1.1 skrll /* Find first FDR for address OFFSET. */
1951 1.1 skrll i = fdrtab_lookup (line_info, offset);
1952 1.1 skrll if (i < 0)
1953 1.1 skrll return FALSE; /* no FDR, no fun... */
1954 1.1 skrll
1955 1.1 skrll /* eraxxon: 'fdrtab_lookup' doesn't give what we want, at least for Compaq's
1956 1.1 skrll C++ compiler 6.2. Consider three FDRs with starting addresses of x, y,
1957 1.1 skrll and z, respectively, such that x < y < z. Assume further that
1958 1.1 skrll y < 'offset' < z. It is possible at times that the PDR for 'offset' is
1959 1.1 skrll associated with FDR x and *not* with FDR y. Erg!!
1960 1.1 skrll
1961 1.1 skrll From a binary dump of my C++ test case 'moo' using Compaq's coffobjanl
1962 1.1 skrll (output format has been edited for our purposes):
1963 1.1 skrll
1964 1.1 skrll FDR [2]: (main.C): First instruction: 0x12000207c <x>
1965 1.1 skrll PDR [5] for File [2]: LoopTest__Xv <0x1200020a0> (a)
1966 1.1 skrll PDR [7] for File [2]: foo__Xv <0x120002168>
1967 1.1 skrll FDR [1]: (-1): First instruction: 0x1200020e8 <y>
1968 1.1 skrll PDR [3] for File [1]: <0x120001ad0> (b)
1969 1.1 skrll FDR [6]: (-1): First instruction: 0x1200026f0 <z>
1970 1.1 skrll
1971 1.1 skrll (a) In the case of PDR5, the vma is such that the first few instructions
1972 1.1 skrll of the procedure can be found. But since the size of this procedure is
1973 1.1 skrll 160b, the vma will soon cross into the 'address space' of FDR1 and no
1974 1.1 skrll debugging info will be found. How repugnant!
1975 1.1 skrll
1976 1.1 skrll (b) It is also possible for a PDR to have a *lower* vma than its associated
1977 1.1 skrll FDR; see FDR1 and PDR3. Gross!
1978 1.1 skrll
1979 1.1 skrll Since the FDRs that are causing so much havok (in this case) 1) do not
1980 1.1 skrll describe actual files (fdr.rss == -1), and 2) contain only compiler
1981 1.1 skrll generated routines, I thought a simple fix would be to exclude them from
1982 1.1 skrll the FDR table in 'mk_fdrtab'. But, besides not knowing for certain
1983 1.1 skrll whether this would be correct, it creates an additional problem. If we
1984 1.1 skrll happen to ask for source file info on a compiler generated (procedure)
1985 1.1 skrll symbol -- which is still in the symbol table -- the result can be
1986 1.1 skrll information from a real procedure! This is because compiler generated
1987 1.1 skrll procedures with vma's higher than the last FDR in the fdr table will be
1988 1.1 skrll associated with a PDR from this FDR, specifically the PDR with the
1989 1.1 skrll highest vma. This wasn't a problem before, because each procedure had a
1990 1.1 skrll PDR. (Yes, this problem could be eliminated if we kept the size of the
1991 1.1 skrll last PDR around, but things are already getting ugly).
1992 1.1 skrll
1993 1.1 skrll Probably, a better solution would be to have a sorted PDR table. Each
1994 1.1 skrll PDR would have a pointer to its FDR so file information could still be
1995 1.1 skrll obtained. A FDR table could still be constructed if necessary -- since
1996 1.1 skrll it only contains pointers, not much extra memory would be used -- but
1997 1.1 skrll the PDR table would be searched to locate debugging info.
1998 1.1 skrll
1999 1.1 skrll There is still at least one remaining issue. Sometimes a FDR can have a
2000 1.1 skrll bogus name, but contain PDRs that should belong to another FDR with a
2001 1.1 skrll real name. E.g:
2002 1.1 skrll
2003 1.1 skrll FDR [3]: 0000000120001b50 (/home/.../Array.H~alt~deccxx_5E5A62AD)
2004 1.1 skrll PDR [a] for File [3]: 0000000120001b50
2005 1.1 skrll PDR [b] for File [3]: 0000000120001cf0
2006 1.1 skrll PDR [c] for File [3]: 0000000120001dc8
2007 1.1 skrll PDR [d] for File [3]: 0000000120001e40
2008 1.1 skrll PDR [e] for File [3]: 0000000120001eb8
2009 1.1 skrll PDR [f] for File [3]: 0000000120001f4c
2010 1.1 skrll FDR [4]: 0000000120001b50 (/home/.../Array.H)
2011 1.1 skrll
2012 1.1 skrll Here, FDR4 has the correct name, but should (seemingly) contain PDRa-f.
2013 1.1 skrll The symbol table for PDR4 does contain symbols for PDRa-f, but so does
2014 1.1 skrll the symbol table for FDR3. However the former is different; perhaps this
2015 1.1 skrll can be detected easily. (I'm not sure at this point.) This problem only
2016 1.1 skrll seems to be associated with files with templates. I am assuming the idea
2017 1.1 skrll is that there is a 'fake' FDR (with PDRs) for each differently typed set
2018 1.1 skrll of templates that must be generated. Currently, FDR4 is completely
2019 1.1 skrll excluded from the FDR table in 'mk_fdrtab' because it contains no PDRs.
2020 1.1 skrll
2021 1.1 skrll Since I don't have time to prepare a real fix for this right now, be
2022 1.1 skrll prepared for 'A Horrible Hack' to force the inspection of all non-stabs
2023 1.1 skrll FDRs. It's coming... */
2024 1.1 skrll fdr_ptr = tab[i].fdr;
2025 1.1 skrll
2026 1.1 skrll /* Check whether this file has stabs debugging information. In a
2027 1.1 skrll file with stabs debugging information, the second local symbol is
2028 1.1 skrll named @stabs. */
2029 1.1 skrll stabs = FALSE;
2030 1.1 skrll if (fdr_ptr->csym >= 2)
2031 1.1 skrll {
2032 1.1 skrll char *sym_ptr;
2033 1.1 skrll SYMR sym;
2034 1.1 skrll
2035 1.1 skrll sym_ptr = ((char *) debug_info->external_sym
2036 1.1 skrll + (fdr_ptr->isymBase + 1) * debug_swap->external_sym_size);
2037 1.1 skrll (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
2038 1.1 skrll if (strcmp (debug_info->ss + fdr_ptr->issBase + sym.iss,
2039 1.1 skrll STABS_SYMBOL) == 0)
2040 1.1 skrll stabs = TRUE;
2041 1.1 skrll }
2042 1.1 skrll
2043 1.1 skrll if (!stabs)
2044 1.1 skrll {
2045 1.1 skrll bfd_size_type external_pdr_size;
2046 1.1 skrll char *pdr_ptr;
2047 1.1 skrll char *best_pdr = NULL;
2048 1.1 skrll FDR *best_fdr;
2049 1.1 skrll bfd_signed_vma best_dist = -1;
2050 1.1 skrll PDR pdr;
2051 1.1 skrll unsigned char *line_ptr;
2052 1.1 skrll unsigned char *line_end;
2053 1.1 skrll int lineno;
2054 1.1 skrll /* This file uses ECOFF debugging information. Each FDR has a
2055 1.1 skrll list of procedure descriptors (PDR). The address in the FDR
2056 1.1 skrll is the absolute address of the first procedure. The address
2057 1.1 skrll in the first PDR gives the offset of that procedure relative
2058 1.1 skrll to the object file's base-address. The addresses in
2059 1.1 skrll subsequent PDRs specify each procedure's address relative to
2060 1.1 skrll the object file's base-address. To make things more juicy,
2061 1.1 skrll whenever the PROF bit in the PDR is set, the real entry point
2062 1.1 skrll of the procedure may be 16 bytes below what would normally be
2063 1.1 skrll the procedure's entry point. Instead, DEC came up with a
2064 1.1 skrll wicked scheme to create profiled libraries "on the fly":
2065 1.1 skrll instead of shipping a regular and a profiled version of each
2066 1.1 skrll library, they insert 16 bytes of unused space in front of
2067 1.1 skrll each procedure and set the "prof" bit in the PDR to indicate
2068 1.1 skrll that there is a gap there (this is done automagically by "as"
2069 1.1 skrll when option "-pg" is specified). Thus, normally, you link
2070 1.1 skrll against such a library and, except for lots of 16 byte gaps
2071 1.1 skrll between functions, things will behave as usual. However,
2072 1.1 skrll when invoking "ld" with option "-pg", it will fill those gaps
2073 1.1 skrll with code that calls mcount(). It then moves the function's
2074 1.1 skrll entry point down by 16 bytes, and out pops a binary that has
2075 1.1 skrll all functions profiled.
2076 1.1 skrll
2077 1.1 skrll NOTE: Neither FDRs nor PDRs are strictly sorted in memory
2078 1.1 skrll order. For example, when including header-files that
2079 1.1 skrll define functions, the FDRs follow behind the including
2080 1.1 skrll file, even though their code may have been generated at
2081 1.1 skrll a lower address. File coff-alpha.c from libbfd
2082 1.1 skrll illustrates this (use "odump -PFv" to look at a file's
2083 1.1 skrll FDR/PDR). Similarly, PDRs are sometimes out of order
2084 1.1 skrll as well. An example of this is OSF/1 v3.0 libc's
2085 1.1 skrll malloc.c. I'm not sure why this happens, but it could
2086 1.1 skrll be due to optimizations that reorder a function's
2087 1.1 skrll position within an object-file.
2088 1.1 skrll
2089 1.1 skrll Strategy:
2090 1.1 skrll
2091 1.1 skrll On the first call to this function, we build a table of FDRs
2092 1.1 skrll that is sorted by the base-address of the object-file the FDR
2093 1.1 skrll is referring to. Notice that each object-file may contain
2094 1.1 skrll code from multiple source files (e.g., due to code defined in
2095 1.1 skrll include files). Thus, for any given base-address, there may
2096 1.1 skrll be multiple FDRs (but this case is, fortunately, uncommon).
2097 1.1 skrll lookup(addr) guarantees to return the first FDR that applies
2098 1.1 skrll to address ADDR. Thus, after invoking lookup(), we have a
2099 1.1 skrll list of FDRs that may contain the PDR for ADDR. Next, we
2100 1.1 skrll walk through the PDRs of these FDRs and locate the one that
2101 1.1 skrll is closest to ADDR (i.e., for which the difference between
2102 1.1 skrll ADDR and the PDR's entry point is positive and minimal).
2103 1.1 skrll Once, the right FDR and PDR are located, we simply walk
2104 1.1 skrll through the line-number table to lookup the line-number that
2105 1.1 skrll best matches ADDR. Obviously, things could be sped up by
2106 1.1 skrll keeping a sorted list of PDRs instead of a sorted list of
2107 1.1 skrll FDRs. However, this would increase space requirements
2108 1.1 skrll considerably, which is undesirable. */
2109 1.1 skrll external_pdr_size = debug_swap->external_pdr_size;
2110 1.1 skrll
2111 1.1 skrll /* eraxxon: The Horrible Hack: Because of the problems above, set 'i'
2112 1.1 skrll to 0 so we look through all FDRs.
2113 1.1 skrll
2114 1.1 skrll Because FDR's without any symbols are assumed to be non-stabs,
2115 1.1 skrll searching through all FDRs may cause the following code to try to
2116 1.1 skrll read stabs FDRs as ECOFF ones. However, I don't think this will
2117 1.1 skrll harm anything. */
2118 1.1 skrll i = 0;
2119 1.1 skrll
2120 1.1 skrll /* Search FDR list starting at tab[i] for the PDR that best matches
2121 1.1 skrll OFFSET. Normally, the FDR list is only one entry long. */
2122 1.1 skrll best_fdr = NULL;
2123 1.1 skrll do
2124 1.1 skrll {
2125 1.1 skrll /* eraxxon: 'dist' and 'min_dist' can be negative now
2126 1.1 skrll because we iterate over every FDR rather than just ones
2127 1.1 skrll with a base address less than or equal to 'offset'. */
2128 1.1 skrll bfd_signed_vma dist = -1, min_dist = -1;
2129 1.1 skrll char *pdr_hold;
2130 1.1 skrll char *pdr_end;
2131 1.1 skrll
2132 1.1 skrll fdr_ptr = tab[i].fdr;
2133 1.1 skrll
2134 1.1 skrll pdr_ptr = ((char *) debug_info->external_pdr
2135 1.1 skrll + fdr_ptr->ipdFirst * external_pdr_size);
2136 1.1 skrll pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
2137 1.1 skrll (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2138 1.1 skrll /* Find PDR that is closest to OFFSET. If pdr.prof is set,
2139 1.1 skrll the procedure entry-point *may* be 0x10 below pdr.adr. We
2140 1.1 skrll simply pretend that pdr.prof *implies* a lower entry-point.
2141 1.1 skrll This is safe because it just means that may identify 4 NOPs
2142 1.1 skrll in front of the function as belonging to the function. */
2143 1.1 skrll for (pdr_hold = NULL;
2144 1.1 skrll pdr_ptr < pdr_end;
2145 1.1 skrll (pdr_ptr += external_pdr_size,
2146 1.1 skrll (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr)))
2147 1.1 skrll {
2148 1.1 skrll if (offset >= (pdr.adr - 0x10 * pdr.prof))
2149 1.1 skrll {
2150 1.1 skrll dist = offset - (pdr.adr - 0x10 * pdr.prof);
2151 1.1 skrll
2152 1.1 skrll /* eraxxon: 'dist' can be negative now. Note that
2153 1.1 skrll 'min_dist' can be negative if 'pdr_hold' below is NULL. */
2154 1.1 skrll if (!pdr_hold || (dist >= 0 && dist < min_dist))
2155 1.1 skrll {
2156 1.1 skrll min_dist = dist;
2157 1.1 skrll pdr_hold = pdr_ptr;
2158 1.1 skrll }
2159 1.1 skrll }
2160 1.1 skrll }
2161 1.1 skrll
2162 1.1 skrll if (!best_pdr || (min_dist >= 0 && min_dist < best_dist))
2163 1.1 skrll {
2164 1.1 skrll best_dist = (bfd_vma) min_dist;
2165 1.1 skrll best_fdr = fdr_ptr;
2166 1.1 skrll best_pdr = pdr_hold;
2167 1.1 skrll }
2168 1.1 skrll /* Continue looping until base_addr of next entry is different. */
2169 1.1 skrll }
2170 1.1 skrll /* eraxxon: We want to iterate over all FDRs.
2171 1.1 skrll See previous comment about 'fdrtab_lookup'. */
2172 1.1 skrll while (++i < line_info->fdrtab_len);
2173 1.1 skrll
2174 1.1 skrll if (!best_fdr || !best_pdr)
2175 1.1 skrll return FALSE; /* Shouldn't happen... */
2176 1.1 skrll
2177 1.1 skrll /* Phew, finally we got something that we can hold onto. */
2178 1.1 skrll fdr_ptr = best_fdr;
2179 1.1 skrll pdr_ptr = best_pdr;
2180 1.1 skrll (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
2181 1.1 skrll /* Now we can look for the actual line number. The line numbers
2182 1.1 skrll are stored in a very funky format, which I won't try to
2183 1.1 skrll describe. The search is bounded by the end of the FDRs line
2184 1.1 skrll number entries. */
2185 1.1 skrll line_end = debug_info->line + fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2186 1.1 skrll
2187 1.1 skrll /* Make offset relative to procedure entry. */
2188 1.1 skrll offset -= pdr.adr - 0x10 * pdr.prof;
2189 1.1 skrll lineno = pdr.lnLow;
2190 1.1 skrll line_ptr = debug_info->line + fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2191 1.1 skrll while (line_ptr < line_end)
2192 1.1 skrll {
2193 1.1 skrll int delta;
2194 1.1 skrll unsigned int count;
2195 1.1 skrll
2196 1.1 skrll delta = *line_ptr >> 4;
2197 1.1 skrll if (delta >= 0x8)
2198 1.1 skrll delta -= 0x10;
2199 1.1 skrll count = (*line_ptr & 0xf) + 1;
2200 1.1 skrll ++line_ptr;
2201 1.1 skrll if (delta == -8)
2202 1.1 skrll {
2203 1.1 skrll delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2204 1.1 skrll if (delta >= 0x8000)
2205 1.1 skrll delta -= 0x10000;
2206 1.1 skrll line_ptr += 2;
2207 1.1 skrll }
2208 1.1 skrll lineno += delta;
2209 1.1 skrll if (offset < count * 4)
2210 1.1 skrll {
2211 1.1 skrll line_info->cache.stop += count * 4 - offset;
2212 1.1 skrll break;
2213 1.1 skrll }
2214 1.1 skrll offset -= count * 4;
2215 1.1 skrll }
2216 1.1 skrll
2217 1.1 skrll /* If fdr_ptr->rss is -1, then this file does not have full
2218 1.1 skrll symbols, at least according to gdb/mipsread.c. */
2219 1.1 skrll if (fdr_ptr->rss == -1)
2220 1.1 skrll {
2221 1.1 skrll line_info->cache.filename = NULL;
2222 1.1 skrll if (pdr.isym == -1)
2223 1.1 skrll line_info->cache.functionname = NULL;
2224 1.1 skrll else
2225 1.1 skrll {
2226 1.1 skrll EXTR proc_ext;
2227 1.1 skrll
2228 1.1 skrll (*debug_swap->swap_ext_in)
2229 1.1 skrll (abfd,
2230 1.1 skrll ((char *) debug_info->external_ext
2231 1.1 skrll + pdr.isym * debug_swap->external_ext_size),
2232 1.1 skrll &proc_ext);
2233 1.1 skrll line_info->cache.functionname = (debug_info->ssext
2234 1.1 skrll + proc_ext.asym.iss);
2235 1.1 skrll }
2236 1.1 skrll }
2237 1.1 skrll else
2238 1.1 skrll {
2239 1.1 skrll SYMR proc_sym;
2240 1.1 skrll
2241 1.1 skrll line_info->cache.filename = (debug_info->ss
2242 1.1 skrll + fdr_ptr->issBase
2243 1.1 skrll + fdr_ptr->rss);
2244 1.1 skrll (*debug_swap->swap_sym_in)
2245 1.1 skrll (abfd,
2246 1.1 skrll ((char *) debug_info->external_sym
2247 1.1 skrll + ((fdr_ptr->isymBase + pdr.isym)
2248 1.1 skrll * debug_swap->external_sym_size)),
2249 1.1 skrll &proc_sym);
2250 1.1 skrll line_info->cache.functionname = (debug_info->ss
2251 1.1 skrll + fdr_ptr->issBase
2252 1.1 skrll + proc_sym.iss);
2253 1.1 skrll }
2254 1.1 skrll if (lineno == ilineNil)
2255 1.1 skrll lineno = 0;
2256 1.1 skrll line_info->cache.line_num = lineno;
2257 1.1 skrll }
2258 1.1 skrll else
2259 1.1 skrll {
2260 1.1 skrll bfd_size_type external_sym_size;
2261 1.1 skrll const char *directory_name;
2262 1.1 skrll const char *main_file_name;
2263 1.1 skrll const char *current_file_name;
2264 1.1 skrll const char *function_name;
2265 1.1 skrll const char *line_file_name;
2266 1.1 skrll bfd_vma low_func_vma;
2267 1.1 skrll bfd_vma low_line_vma;
2268 1.1 skrll bfd_boolean past_line;
2269 1.1 skrll bfd_boolean past_fn;
2270 1.1 skrll char *sym_ptr, *sym_ptr_end;
2271 1.1 skrll size_t len, funclen;
2272 1.1 skrll char *buffer = NULL;
2273 1.1 skrll
2274 1.1 skrll /* This file uses stabs debugging information. When gcc is not
2275 1.1 skrll optimizing, it will put the line number information before
2276 1.1 skrll the function name stabs entry. When gcc is optimizing, it
2277 1.1 skrll will put the stabs entry for all the function first, followed
2278 1.1 skrll by the line number information. (This appears to happen
2279 1.1 skrll because of the two output files used by the -mgpopt switch,
2280 1.1 skrll which is implied by -O). This means that we must keep
2281 1.1 skrll looking through the symbols until we find both a line number
2282 1.1 skrll and a function name which are beyond the address we want. */
2283 1.1 skrll
2284 1.1 skrll line_info->cache.filename = NULL;
2285 1.1 skrll line_info->cache.functionname = NULL;
2286 1.1 skrll line_info->cache.line_num = 0;
2287 1.1 skrll
2288 1.1 skrll directory_name = NULL;
2289 1.1 skrll main_file_name = NULL;
2290 1.1 skrll current_file_name = NULL;
2291 1.1 skrll function_name = NULL;
2292 1.1 skrll line_file_name = NULL;
2293 1.1 skrll low_func_vma = 0;
2294 1.1 skrll low_line_vma = 0;
2295 1.1 skrll past_line = FALSE;
2296 1.1 skrll past_fn = FALSE;
2297 1.1 skrll
2298 1.1 skrll external_sym_size = debug_swap->external_sym_size;
2299 1.1 skrll
2300 1.1 skrll sym_ptr = ((char *) debug_info->external_sym
2301 1.1 skrll + (fdr_ptr->isymBase + 2) * external_sym_size);
2302 1.1 skrll sym_ptr_end = sym_ptr + (fdr_ptr->csym - 2) * external_sym_size;
2303 1.1 skrll for (;
2304 1.1 skrll sym_ptr < sym_ptr_end && (! past_line || ! past_fn);
2305 1.1 skrll sym_ptr += external_sym_size)
2306 1.1 skrll {
2307 1.1 skrll SYMR sym;
2308 1.1 skrll
2309 1.1 skrll (*debug_swap->swap_sym_in) (abfd, sym_ptr, &sym);
2310 1.1 skrll
2311 1.1 skrll if (ECOFF_IS_STAB (&sym))
2312 1.1 skrll {
2313 1.1 skrll switch (ECOFF_UNMARK_STAB (sym.index))
2314 1.1 skrll {
2315 1.1 skrll case N_SO:
2316 1.1 skrll main_file_name = current_file_name =
2317 1.1 skrll debug_info->ss + fdr_ptr->issBase + sym.iss;
2318 1.1 skrll
2319 1.1 skrll /* Check the next symbol to see if it is also an
2320 1.1 skrll N_SO symbol. */
2321 1.1 skrll if (sym_ptr + external_sym_size < sym_ptr_end)
2322 1.1 skrll {
2323 1.1 skrll SYMR nextsym;
2324 1.1 skrll
2325 1.1 skrll (*debug_swap->swap_sym_in) (abfd,
2326 1.1 skrll sym_ptr + external_sym_size,
2327 1.1 skrll &nextsym);
2328 1.1 skrll if (ECOFF_IS_STAB (&nextsym)
2329 1.1 skrll && ECOFF_UNMARK_STAB (nextsym.index) == N_SO)
2330 1.1 skrll {
2331 1.1 skrll directory_name = current_file_name;
2332 1.1 skrll main_file_name = current_file_name =
2333 1.1 skrll debug_info->ss + fdr_ptr->issBase + nextsym.iss;
2334 1.1 skrll sym_ptr += external_sym_size;
2335 1.1 skrll }
2336 1.1 skrll }
2337 1.1 skrll break;
2338 1.1 skrll
2339 1.1 skrll case N_SOL:
2340 1.1 skrll current_file_name =
2341 1.1 skrll debug_info->ss + fdr_ptr->issBase + sym.iss;
2342 1.1 skrll break;
2343 1.1 skrll
2344 1.1 skrll case N_FUN:
2345 1.1 skrll if (sym.value > offset)
2346 1.1 skrll past_fn = TRUE;
2347 1.1 skrll else if (sym.value >= low_func_vma)
2348 1.1 skrll {
2349 1.1 skrll low_func_vma = sym.value;
2350 1.1 skrll function_name =
2351 1.1 skrll debug_info->ss + fdr_ptr->issBase + sym.iss;
2352 1.1 skrll }
2353 1.1 skrll break;
2354 1.1 skrll }
2355 1.1 skrll }
2356 1.1 skrll else if (sym.st == stLabel && sym.index != indexNil)
2357 1.1 skrll {
2358 1.1 skrll if (sym.value > offset)
2359 1.1 skrll past_line = TRUE;
2360 1.1 skrll else if (sym.value >= low_line_vma)
2361 1.1 skrll {
2362 1.1 skrll low_line_vma = sym.value;
2363 1.1 skrll line_file_name = current_file_name;
2364 1.1 skrll line_info->cache.line_num = sym.index;
2365 1.1 skrll }
2366 1.1 skrll }
2367 1.1 skrll }
2368 1.1 skrll
2369 1.1 skrll if (line_info->cache.line_num != 0)
2370 1.1 skrll main_file_name = line_file_name;
2371 1.1 skrll
2372 1.1 skrll /* We need to remove the stuff after the colon in the function
2373 1.1 skrll name. We also need to put the directory name and the file
2374 1.1 skrll name together. */
2375 1.1 skrll if (function_name == NULL)
2376 1.1 skrll len = funclen = 0;
2377 1.1 skrll else
2378 1.1 skrll len = funclen = strlen (function_name) + 1;
2379 1.1 skrll
2380 1.1 skrll if (main_file_name != NULL
2381 1.1 skrll && directory_name != NULL
2382 1.1 skrll && main_file_name[0] != '/')
2383 1.1 skrll len += strlen (directory_name) + strlen (main_file_name) + 1;
2384 1.1 skrll
2385 1.1 skrll if (len != 0)
2386 1.1 skrll {
2387 1.1 skrll if (line_info->find_buffer != NULL)
2388 1.1 skrll free (line_info->find_buffer);
2389 1.1 skrll buffer = (char *) bfd_malloc ((bfd_size_type) len);
2390 1.1 skrll if (buffer == NULL)
2391 1.1 skrll return FALSE;
2392 1.1 skrll line_info->find_buffer = buffer;
2393 1.1 skrll }
2394 1.1 skrll
2395 1.1 skrll if (function_name != NULL)
2396 1.1 skrll {
2397 1.1 skrll char *colon;
2398 1.1 skrll
2399 1.1 skrll strcpy (buffer, function_name);
2400 1.1 skrll colon = strchr (buffer, ':');
2401 1.1 skrll if (colon != NULL)
2402 1.1 skrll *colon = '\0';
2403 1.1 skrll line_info->cache.functionname = buffer;
2404 1.1 skrll }
2405 1.1 skrll
2406 1.1 skrll if (main_file_name != NULL)
2407 1.1 skrll {
2408 1.1 skrll if (directory_name == NULL || main_file_name[0] == '/')
2409 1.1 skrll line_info->cache.filename = main_file_name;
2410 1.1 skrll else
2411 1.1 skrll {
2412 1.1 skrll sprintf (buffer + funclen, "%s%s", directory_name,
2413 1.1 skrll main_file_name);
2414 1.1 skrll line_info->cache.filename = buffer + funclen;
2415 1.1 skrll }
2416 1.1 skrll }
2417 1.1 skrll }
2418 1.1 skrll
2419 1.1 skrll return TRUE;
2420 1.1 skrll }
2421 1.1 skrll
2422 1.1 skrll /* Do the work of find_nearest_line. */
2423 1.1 skrll
2424 1.1 skrll bfd_boolean
2425 1.1 skrll _bfd_ecoff_locate_line (abfd, section, offset, debug_info, debug_swap,
2426 1.1 skrll line_info, filename_ptr, functionname_ptr, retline_ptr)
2427 1.1 skrll bfd *abfd;
2428 1.1 skrll asection *section;
2429 1.1 skrll bfd_vma offset;
2430 1.1 skrll struct ecoff_debug_info * const debug_info;
2431 1.1 skrll const struct ecoff_debug_swap * const debug_swap;
2432 1.1 skrll struct ecoff_find_line *line_info;
2433 1.1 skrll const char **filename_ptr;
2434 1.1 skrll const char **functionname_ptr;
2435 1.1 skrll unsigned int *retline_ptr;
2436 1.1 skrll {
2437 1.1 skrll offset += section->vma;
2438 1.1 skrll
2439 1.1 skrll if (line_info->cache.sect == NULL
2440 1.1 skrll || line_info->cache.sect != section
2441 1.1 skrll || offset < line_info->cache.start
2442 1.1 skrll || offset >= line_info->cache.stop)
2443 1.1 skrll {
2444 1.1 skrll line_info->cache.sect = section;
2445 1.1 skrll line_info->cache.start = offset;
2446 1.1 skrll line_info->cache.stop = offset;
2447 1.1 skrll if (! lookup_line (abfd, debug_info, debug_swap, line_info))
2448 1.1 skrll {
2449 1.1 skrll line_info->cache.sect = NULL;
2450 1.1 skrll return FALSE;
2451 1.1 skrll }
2452 1.1 skrll }
2453 1.1 skrll
2454 1.1 skrll *filename_ptr = line_info->cache.filename;
2455 1.1 skrll *functionname_ptr = line_info->cache.functionname;
2456 1.1 skrll *retline_ptr = line_info->cache.line_num;
2457 1.1 skrll
2458 1.1 skrll return TRUE;
2459 1.1 skrll }
2460 1.1 skrll
2461 1.1 skrll /* These routines copy symbolic information into a memory buffer.
2463 1.1 skrll
2464 1.1 skrll FIXME: The whole point of the shuffle code is to avoid storing
2465 1.1 skrll everything in memory, since the linker is such a memory hog. This
2466 1.1 skrll code makes that effort useless. It is only called by the MIPS ELF
2467 1.1 skrll code when generating a shared library, so it is not that big a
2468 1.1 skrll deal, but it should be fixed eventually. */
2469 1.1 skrll
2470 1.1 skrll /* Collect a shuffle into a memory buffer. */
2471 1.1 skrll
2472 1.1 skrll static bfd_boolean ecoff_collect_shuffle
2473 1.1 skrll PARAMS ((struct shuffle *, bfd_byte *));
2474 1.1 skrll
2475 1.1 skrll static bfd_boolean
2476 1.1 skrll ecoff_collect_shuffle (l, buff)
2477 1.1 skrll struct shuffle *l;
2478 1.1 skrll bfd_byte *buff;
2479 1.1 skrll {
2480 1.1 skrll unsigned long total;
2481 1.1 skrll
2482 1.1 skrll total = 0;
2483 1.1 skrll for (; l != (struct shuffle *) NULL; l = l->next)
2484 1.1 skrll {
2485 1.1 skrll if (! l->filep)
2486 1.1 skrll memcpy (buff, l->u.memory, l->size);
2487 1.1 skrll else
2488 1.1 skrll {
2489 1.1 skrll if (bfd_seek (l->u.file.input_bfd, l->u.file.offset, SEEK_SET) != 0
2490 1.1 skrll || (bfd_bread (buff, (bfd_size_type) l->size, l->u.file.input_bfd)
2491 1.1 skrll != l->size))
2492 1.1 skrll return FALSE;
2493 1.1 skrll }
2494 1.1 skrll total += l->size;
2495 1.1 skrll buff += l->size;
2496 1.1 skrll }
2497 1.1 skrll
2498 1.1 skrll return TRUE;
2499 1.1 skrll }
2500 1.1 skrll
2501 1.1 skrll /* Copy PDR information into a memory buffer. */
2502 1.1 skrll
2503 1.1 skrll bfd_boolean
2504 1.1 skrll _bfd_ecoff_get_accumulated_pdr (handle, buff)
2505 1.1 skrll PTR handle;
2506 1.1 skrll bfd_byte *buff;
2507 1.1 skrll {
2508 1.1 skrll struct accumulate *ainfo = (struct accumulate *) handle;
2509 1.1 skrll
2510 1.1 skrll return ecoff_collect_shuffle (ainfo->pdr, buff);
2511 1.1 skrll }
2512 1.1 skrll
2513 1.1 skrll /* Copy symbol information into a memory buffer. */
2514 1.1 skrll
2515 1.1 skrll bfd_boolean
2516 1.1 skrll _bfd_ecoff_get_accumulated_sym (handle, buff)
2517 1.1 skrll PTR handle;
2518 1.1 skrll bfd_byte *buff;
2519 1.1 skrll {
2520 1.1 skrll struct accumulate *ainfo = (struct accumulate *) handle;
2521 1.1 skrll
2522 1.1 skrll return ecoff_collect_shuffle (ainfo->sym, buff);
2523 1.1 skrll }
2524 1.1 skrll
2525 1.1 skrll /* Copy the string table into a memory buffer. */
2526 1.1 skrll
2527 1.1 skrll bfd_boolean
2528 1.1 skrll _bfd_ecoff_get_accumulated_ss (handle, buff)
2529 1.1 skrll PTR handle;
2530 1.1 skrll bfd_byte *buff;
2531 1.1 skrll {
2532 1.1 skrll struct accumulate *ainfo = (struct accumulate *) handle;
2533 1.1 skrll struct string_hash_entry *sh;
2534 1.1 skrll unsigned long total;
2535 1.1 skrll
2536 1.1 skrll /* The string table is written out from the hash table if this is a
2537 1.1 skrll final link. */
2538 1.1 skrll BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
2539 1.1 skrll *buff++ = '\0';
2540 1.1 skrll total = 1;
2541 1.1 skrll BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
2542 1.1 skrll for (sh = ainfo->ss_hash;
2543 1.1 skrll sh != (struct string_hash_entry *) NULL;
2544 1.1 skrll sh = sh->next)
2545 1.1 skrll {
2546 1.1 skrll size_t len;
2547 1.1 skrll
2548 1.1 skrll len = strlen (sh->root.string);
2549 1.1 skrll memcpy (buff, (PTR) sh->root.string, len + 1);
2550 1.1 skrll total += len + 1;
2551 buff += len + 1;
2552 }
2553
2554 return TRUE;
2555 }
2556