]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back-end for oasys objects. |
e2d34d7d | 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002 |
252b5132 RH |
3 | Free Software Foundation, Inc. |
4 | Written by Steve Chamberlain of Cygnus Support, <sac@cygnus.com>. | |
5 | ||
42ef282f | 6 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 7 | |
42ef282f NC |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
252b5132 | 12 | |
42ef282f NC |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
252b5132 | 17 | |
42ef282f NC |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
252b5132 RH |
21 | |
22 | #define UNDERSCORE_HACK 1 | |
23 | #include "bfd.h" | |
24 | #include "sysdep.h" | |
3882b010 | 25 | #include "safe-ctype.h" |
252b5132 RH |
26 | #include "libbfd.h" |
27 | #include "oasys.h" | |
28 | #include "liboasys.h" | |
29 | ||
dc810e39 AM |
30 | static boolean oasys_slurp_section_data PARAMS ((bfd * const)); |
31 | static boolean oasys_read_record PARAMS ((bfd *, oasys_record_union_type *)); | |
252b5132 | 32 | static boolean oasys_write_sections PARAMS ((bfd *)); |
dc810e39 AM |
33 | static boolean oasys_write_record |
34 | PARAMS ((bfd *, oasys_record_enum_type, oasys_record_union_type *, size_t)); | |
252b5132 RH |
35 | static boolean oasys_write_syms PARAMS ((bfd *)); |
36 | static boolean oasys_write_header PARAMS ((bfd *)); | |
37 | static boolean oasys_write_end PARAMS ((bfd *)); | |
38 | static boolean oasys_write_data PARAMS ((bfd *)); | |
24a35864 AJ |
39 | static size_t oasys_string_length PARAMS ((oasys_record_union_type *)); |
40 | static boolean oasys_slurp_symbol_table PARAMS ((bfd *const)); | |
41 | static long int oasys_get_symtab_upper_bound PARAMS ((bfd *const)); | |
42 | static const bfd_target *oasys_archive_p PARAMS ((bfd *)); | |
43 | static boolean oasys_mkobject PARAMS ((bfd *)); | |
44 | static const bfd_target *oasys_object_p PARAMS ((bfd *)); | |
45 | static void oasys_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *)); | |
dc810e39 AM |
46 | static void oasys_print_symbol |
47 | PARAMS ((bfd *, void *, asymbol *, bfd_print_symbol_type)); | |
24a35864 AJ |
48 | static boolean oasys_new_section_hook PARAMS ((bfd *, asection *)); |
49 | static long int oasys_get_reloc_upper_bound PARAMS ((bfd *, sec_ptr)); | |
50 | static boolean oasys_get_section_contents | |
51 | PARAMS ((bfd *, sec_ptr, void *, file_ptr, bfd_size_type)); | |
52 | static int comp PARAMS ((const void *, const void *)); | |
53 | static boolean oasys_write_object_contents PARAMS ((bfd *)); | |
54 | static boolean oasys_set_section_contents | |
55 | PARAMS ((bfd *, sec_ptr, void *, file_ptr, bfd_size_type)); | |
56 | static asymbol *oasys_make_empty_symbol PARAMS ((bfd *)); | |
57 | static bfd *oasys_openr_next_archived_file PARAMS ((bfd *, bfd *)); | |
58 | static boolean oasys_find_nearest_line | |
dc810e39 AM |
59 | PARAMS ((bfd *, asection *, asymbol **, bfd_vma, |
60 | const char **, const char **, unsigned int *)); | |
24a35864 AJ |
61 | static int oasys_generic_stat_arch_elt PARAMS ((bfd *, struct stat *)); |
62 | static int oasys_sizeof_headers PARAMS ((bfd *, boolean)); | |
252b5132 | 63 | |
42ef282f | 64 | long oasys_get_symtab PARAMS ((bfd *, asymbol **)); |
dc810e39 AM |
65 | long oasys_canonicalize_reloc |
66 | PARAMS ((bfd *, sec_ptr, arelent **, asymbol **)); | |
42ef282f NC |
67 | |
68 | /* Read in all the section data and relocation stuff too. */ | |
252b5132 RH |
69 | |
70 | static boolean | |
71 | oasys_read_record (abfd, record) | |
72 | bfd *abfd; | |
73 | oasys_record_union_type *record; | |
74 | { | |
dc810e39 AM |
75 | bfd_size_type amt = sizeof (record->header); |
76 | if (bfd_bread ((PTR) record, amt, abfd) != amt) | |
252b5132 RH |
77 | return false; |
78 | ||
dc810e39 AM |
79 | amt = record->header.length - sizeof (record->header); |
80 | if ((long) amt <= 0) | |
252b5132 | 81 | return true; |
dc810e39 AM |
82 | if (bfd_bread ((PTR) ((char *) record + sizeof (record->header)), amt, abfd) |
83 | != amt) | |
252b5132 RH |
84 | return false; |
85 | return true; | |
86 | } | |
dc810e39 | 87 | |
252b5132 RH |
88 | static size_t |
89 | oasys_string_length (record) | |
90 | oasys_record_union_type *record; | |
91 | { | |
92 | return record->header.length | |
93 | - ((char *) record->symbol.name - (char *) record); | |
94 | } | |
95 | ||
96 | /*****************************************************************************/ | |
97 | ||
98 | /* | |
99 | ||
100 | Slurp the symbol table by reading in all the records at the start file | |
101 | till we get to the first section record. | |
102 | ||
103 | We'll sort the symbolss into two lists, defined and undefined. The | |
104 | undefined symbols will be placed into the table according to their | |
105 | refno. | |
106 | ||
107 | We do this by placing all undefined symbols at the front of the table | |
108 | moving in, and the defined symbols at the end of the table moving back. | |
109 | ||
110 | */ | |
111 | ||
112 | static boolean | |
113 | oasys_slurp_symbol_table (abfd) | |
dc810e39 | 114 | bfd *const abfd; |
252b5132 RH |
115 | { |
116 | oasys_record_union_type record; | |
117 | oasys_data_type *data = OASYS_DATA (abfd); | |
118 | boolean loop = true; | |
119 | asymbol *dest_defined; | |
120 | asymbol *dest; | |
121 | char *string_ptr; | |
dc810e39 | 122 | bfd_size_type amt; |
252b5132 RH |
123 | |
124 | if (data->symbols != (asymbol *) NULL) | |
125 | { | |
126 | return true; | |
127 | } | |
128 | /* Buy enough memory for all the symbols and all the names */ | |
dc810e39 AM |
129 | amt = abfd->symcount; |
130 | amt *= sizeof (asymbol); | |
131 | data->symbols = (asymbol *) bfd_alloc (abfd, amt); | |
132 | ||
133 | amt = data->symbol_string_length; | |
252b5132 RH |
134 | #ifdef UNDERSCORE_HACK |
135 | /* buy 1 more char for each symbol to keep the underscore in*/ | |
dc810e39 | 136 | amt += abfd->symcount; |
252b5132 | 137 | #endif |
dc810e39 AM |
138 | data->strings = bfd_alloc (abfd, amt); |
139 | ||
252b5132 RH |
140 | if (!data->symbols || !data->strings) |
141 | return false; | |
142 | ||
143 | dest_defined = data->symbols + abfd->symcount - 1; | |
144 | ||
145 | string_ptr = data->strings; | |
146 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) | |
147 | return false; | |
148 | while (loop) | |
149 | { | |
150 | ||
151 | if (! oasys_read_record (abfd, &record)) | |
152 | return false; | |
153 | switch (record.header.type) | |
154 | { | |
155 | case oasys_record_is_header_enum: | |
156 | break; | |
157 | case oasys_record_is_local_enum: | |
158 | case oasys_record_is_symbol_enum: | |
159 | { | |
160 | int flag = record.header.type == (int) oasys_record_is_local_enum ? | |
161 | (BSF_LOCAL) : (BSF_GLOBAL | BSF_EXPORT); | |
162 | ||
163 | ||
164 | size_t length = oasys_string_length (&record); | |
165 | switch (record.symbol.relb & RELOCATION_TYPE_BITS) | |
166 | { | |
167 | case RELOCATION_TYPE_ABS: | |
168 | dest = dest_defined--; | |
169 | dest->section = bfd_abs_section_ptr; | |
170 | dest->flags = 0; | |
171 | ||
172 | break; | |
173 | case RELOCATION_TYPE_REL: | |
174 | dest = dest_defined--; | |
175 | dest->section = | |
176 | OASYS_DATA (abfd)->sections[record.symbol.relb & | |
177 | RELOCATION_SECT_BITS]; | |
178 | if (record.header.type == (int) oasys_record_is_local_enum) | |
179 | { | |
180 | dest->flags = BSF_LOCAL; | |
181 | if (dest->section == (asection *) (~0)) | |
182 | { | |
183 | /* It seems that sometimes internal symbols are tied up, but | |
184 | still get output, even though there is no | |
185 | section */ | |
186 | dest->section = 0; | |
187 | } | |
188 | } | |
189 | else | |
190 | { | |
191 | ||
192 | dest->flags = flag; | |
193 | } | |
194 | break; | |
195 | case RELOCATION_TYPE_UND: | |
dc810e39 | 196 | dest = data->symbols + H_GET_16 (abfd, record.symbol.refno); |
252b5132 RH |
197 | dest->section = bfd_und_section_ptr; |
198 | break; | |
199 | case RELOCATION_TYPE_COM: | |
200 | dest = dest_defined--; | |
201 | dest->name = string_ptr; | |
202 | dest->the_bfd = abfd; | |
203 | ||
204 | dest->section = bfd_com_section_ptr; | |
205 | ||
206 | break; | |
207 | default: | |
208 | dest = dest_defined--; | |
209 | BFD_ASSERT (0); | |
210 | break; | |
211 | } | |
212 | dest->name = string_ptr; | |
213 | dest->the_bfd = abfd; | |
214 | dest->udata.p = (PTR) NULL; | |
dc810e39 | 215 | dest->value = H_GET_32 (abfd, record.symbol.value); |
252b5132 RH |
216 | |
217 | #ifdef UNDERSCORE_HACK | |
218 | if (record.symbol.name[0] != '_') | |
219 | { | |
220 | string_ptr[0] = '_'; | |
221 | string_ptr++; | |
222 | } | |
223 | #endif | |
224 | memcpy (string_ptr, record.symbol.name, length); | |
225 | ||
226 | ||
227 | string_ptr[length] = 0; | |
228 | string_ptr += length + 1; | |
229 | } | |
230 | break; | |
231 | default: | |
232 | loop = false; | |
233 | } | |
234 | } | |
235 | return true; | |
236 | } | |
237 | ||
238 | static long | |
239 | oasys_get_symtab_upper_bound (abfd) | |
dc810e39 | 240 | bfd *const abfd; |
252b5132 RH |
241 | { |
242 | if (! oasys_slurp_symbol_table (abfd)) | |
243 | return -1; | |
244 | ||
245 | return (abfd->symcount + 1) * (sizeof (oasys_symbol_type *)); | |
246 | } | |
247 | ||
252b5132 RH |
248 | extern const bfd_target oasys_vec; |
249 | ||
250 | long | |
251 | oasys_get_symtab (abfd, location) | |
252 | bfd *abfd; | |
253 | asymbol **location; | |
254 | { | |
255 | asymbol *symbase; | |
256 | unsigned int counter; | |
82e51918 | 257 | if (! oasys_slurp_symbol_table (abfd)) |
252b5132 RH |
258 | { |
259 | return -1; | |
260 | } | |
261 | symbase = OASYS_DATA (abfd)->symbols; | |
262 | for (counter = 0; counter < abfd->symcount; counter++) | |
263 | { | |
264 | *(location++) = symbase++; | |
265 | } | |
266 | *location = 0; | |
267 | return abfd->symcount; | |
268 | } | |
269 | ||
270 | /*********************************************************************** | |
271 | * archive stuff | |
272 | */ | |
273 | ||
274 | static const bfd_target * | |
275 | oasys_archive_p (abfd) | |
276 | bfd *abfd; | |
277 | { | |
278 | oasys_archive_header_type header; | |
279 | oasys_extarchive_header_type header_ext; | |
280 | unsigned int i; | |
281 | file_ptr filepos; | |
dc810e39 | 282 | bfd_size_type amt; |
252b5132 | 283 | |
dc810e39 AM |
284 | amt = sizeof (header_ext); |
285 | if (bfd_seek (abfd, (file_ptr) 0, 0) != 0 | |
286 | || bfd_bread ((PTR) &header_ext, amt, abfd) != amt) | |
252b5132 RH |
287 | { |
288 | if (bfd_get_error () != bfd_error_system_call) | |
289 | bfd_set_error (bfd_error_wrong_format); | |
290 | return NULL; | |
291 | } | |
292 | ||
dc810e39 AM |
293 | header.version = H_GET_32 (abfd, header_ext.version); |
294 | header.mod_count = H_GET_32 (abfd, header_ext.mod_count); | |
295 | header.mod_tbl_offset = H_GET_32 (abfd, header_ext.mod_tbl_offset); | |
296 | header.sym_tbl_size = H_GET_32 (abfd, header_ext.sym_tbl_size); | |
297 | header.sym_count = H_GET_32 (abfd, header_ext.sym_count); | |
298 | header.sym_tbl_offset = H_GET_32 (abfd, header_ext.sym_tbl_offset); | |
299 | header.xref_count = H_GET_32 (abfd, header_ext.xref_count); | |
300 | header.xref_lst_offset = H_GET_32 (abfd, header_ext.xref_lst_offset); | |
252b5132 RH |
301 | |
302 | /* | |
303 | There isn't a magic number in an Oasys archive, so the best we | |
304 | can do to verify reasnableness is to make sure that the values in | |
305 | the header are too weird | |
306 | */ | |
307 | ||
308 | if (header.version > 10000 || | |
309 | header.mod_count > 10000 || | |
310 | header.sym_count > 100000 || | |
311 | header.xref_count > 100000) | |
312 | return (const bfd_target *) NULL; | |
313 | ||
314 | /* | |
315 | That all worked, let's buy the space for the header and read in | |
316 | the headers. | |
317 | */ | |
318 | { | |
dc810e39 AM |
319 | oasys_ar_data_type *ar; |
320 | oasys_module_info_type *module; | |
252b5132 RH |
321 | oasys_module_table_type record; |
322 | ||
dc810e39 AM |
323 | amt = sizeof (oasys_ar_data_type); |
324 | ar = (oasys_ar_data_type *) bfd_alloc (abfd, amt); | |
325 | ||
326 | amt = header.mod_count; | |
327 | amt *= sizeof (oasys_module_info_type); | |
328 | module = (oasys_module_info_type *) bfd_alloc (abfd, amt); | |
329 | ||
252b5132 RH |
330 | if (!ar || !module) |
331 | return NULL; | |
332 | ||
333 | abfd->tdata.oasys_ar_data = ar; | |
334 | ar->module = module; | |
335 | ar->module_count = header.mod_count; | |
336 | ||
337 | filepos = header.mod_tbl_offset; | |
338 | for (i = 0; i < header.mod_count; i++) | |
339 | { | |
340 | if (bfd_seek (abfd, filepos, SEEK_SET) != 0) | |
341 | return NULL; | |
342 | ||
343 | /* There are two ways of specifying the archive header */ | |
344 | ||
345 | if (0) | |
346 | { | |
347 | oasys_extmodule_table_type_a_type record_ext; | |
dc810e39 AM |
348 | |
349 | amt = sizeof (record_ext); | |
350 | if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt) | |
252b5132 RH |
351 | return NULL; |
352 | ||
dc810e39 AM |
353 | record.mod_size = H_GET_32 (abfd, record_ext.mod_size); |
354 | record.file_offset = H_GET_32 (abfd, record_ext.file_offset); | |
252b5132 | 355 | |
dc810e39 AM |
356 | record.dep_count = H_GET_32 (abfd, record_ext.dep_count); |
357 | record.depee_count = H_GET_32 (abfd, record_ext.depee_count); | |
358 | record.sect_count = H_GET_32 (abfd, record_ext.sect_count); | |
252b5132 | 359 | |
dc810e39 | 360 | module[i].name = bfd_alloc (abfd, (bfd_size_type) 33); |
252b5132 RH |
361 | if (!module[i].name) |
362 | return NULL; | |
363 | ||
364 | memcpy (module[i].name, record_ext.mod_name, 33); | |
365 | filepos += | |
366 | sizeof (record_ext) + | |
367 | record.dep_count * 4 + | |
368 | record.depee_count * 4 + | |
369 | record.sect_count * 8 + 187; | |
370 | } | |
371 | else | |
372 | { | |
373 | oasys_extmodule_table_type_b_type record_ext; | |
dc810e39 AM |
374 | |
375 | amt = sizeof (record_ext); | |
376 | if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt) | |
252b5132 RH |
377 | return NULL; |
378 | ||
dc810e39 AM |
379 | record.mod_size = H_GET_32 (abfd, record_ext.mod_size); |
380 | record.file_offset = H_GET_32 (abfd, record_ext.file_offset); | |
252b5132 | 381 | |
dc810e39 AM |
382 | record.dep_count = H_GET_32 (abfd, record_ext.dep_count); |
383 | record.depee_count = H_GET_32 (abfd, record_ext.depee_count); | |
384 | record.sect_count = H_GET_32 (abfd, record_ext.sect_count); | |
385 | record.module_name_size = H_GET_32 (abfd, | |
386 | record_ext.mod_name_length); | |
252b5132 | 387 | |
dc810e39 AM |
388 | amt = record.module_name_size; |
389 | module[i].name = bfd_alloc (abfd, amt + 1); | |
252b5132 RH |
390 | if (!module[i].name) |
391 | return NULL; | |
dc810e39 | 392 | if (bfd_bread ((PTR) module[i].name, amt, abfd) != amt) |
252b5132 RH |
393 | return NULL; |
394 | module[i].name[record.module_name_size] = 0; | |
dc810e39 AM |
395 | filepos += (sizeof (record_ext) |
396 | + record.dep_count * 4 | |
397 | + record.module_name_size + 1); | |
252b5132 RH |
398 | } |
399 | ||
252b5132 RH |
400 | module[i].size = record.mod_size; |
401 | module[i].pos = record.file_offset; | |
402 | module[i].abfd = 0; | |
403 | } | |
252b5132 RH |
404 | } |
405 | return abfd->xvec; | |
406 | } | |
407 | ||
408 | static boolean | |
409 | oasys_mkobject (abfd) | |
410 | bfd *abfd; | |
411 | { | |
dc810e39 AM |
412 | bfd_size_type amt = sizeof (oasys_data_type); |
413 | abfd->tdata.oasys_obj_data = (oasys_data_type *) bfd_alloc (abfd, amt); | |
82e51918 | 414 | return abfd->tdata.oasys_obj_data != NULL; |
252b5132 RH |
415 | } |
416 | ||
417 | #define MAX_SECS 16 | |
418 | static const bfd_target * | |
419 | oasys_object_p (abfd) | |
420 | bfd *abfd; | |
421 | { | |
422 | oasys_data_type *oasys; | |
423 | oasys_data_type *save = OASYS_DATA (abfd); | |
424 | boolean loop = true; | |
425 | boolean had_usefull = false; | |
426 | ||
427 | abfd->tdata.oasys_obj_data = 0; | |
428 | oasys_mkobject (abfd); | |
429 | oasys = OASYS_DATA (abfd); | |
430 | memset ((PTR) oasys->sections, 0xff, sizeof (oasys->sections)); | |
431 | ||
432 | /* Point to the start of the file */ | |
433 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) | |
434 | goto fail; | |
435 | oasys->symbol_string_length = 0; | |
436 | /* Inspect the records, but only keep the section info - | |
437 | remember the size of the symbols | |
438 | */ | |
439 | oasys->first_data_record = 0; | |
440 | while (loop) | |
441 | { | |
442 | oasys_record_union_type record; | |
443 | if (! oasys_read_record (abfd, &record)) | |
444 | goto fail; | |
445 | if ((size_t) record.header.length < (size_t) sizeof (record.header)) | |
446 | goto fail; | |
447 | ||
448 | ||
449 | switch ((oasys_record_enum_type) (record.header.type)) | |
450 | { | |
451 | case oasys_record_is_header_enum: | |
452 | had_usefull = true; | |
453 | break; | |
454 | case oasys_record_is_symbol_enum: | |
455 | case oasys_record_is_local_enum: | |
456 | /* Count symbols and remember their size for a future malloc */ | |
457 | abfd->symcount++; | |
458 | oasys->symbol_string_length += 1 + oasys_string_length (&record); | |
459 | had_usefull = true; | |
460 | break; | |
461 | case oasys_record_is_section_enum: | |
462 | { | |
463 | asection *s; | |
464 | char *buffer; | |
465 | unsigned int section_number; | |
466 | if (record.section.header.length != sizeof (record.section)) | |
467 | { | |
468 | goto fail; | |
469 | } | |
dc810e39 | 470 | buffer = bfd_alloc (abfd, (bfd_size_type) 3); |
252b5132 RH |
471 | if (!buffer) |
472 | goto fail; | |
473 | section_number = record.section.relb & RELOCATION_SECT_BITS; | |
474 | sprintf (buffer, "%u", section_number); | |
475 | s = bfd_make_section (abfd, buffer); | |
476 | oasys->sections[section_number] = s; | |
477 | switch (record.section.relb & RELOCATION_TYPE_BITS) | |
478 | { | |
479 | case RELOCATION_TYPE_ABS: | |
480 | case RELOCATION_TYPE_REL: | |
481 | break; | |
482 | case RELOCATION_TYPE_UND: | |
483 | case RELOCATION_TYPE_COM: | |
484 | BFD_FAIL (); | |
485 | } | |
486 | ||
dc810e39 AM |
487 | s->_raw_size = H_GET_32 (abfd, record.section.value); |
488 | s->vma = H_GET_32 (abfd, record.section.vma); | |
252b5132 RH |
489 | s->flags = 0; |
490 | had_usefull = true; | |
491 | } | |
492 | break; | |
493 | case oasys_record_is_data_enum: | |
494 | oasys->first_data_record = bfd_tell (abfd) - record.header.length; | |
495 | case oasys_record_is_debug_enum: | |
496 | case oasys_record_is_module_enum: | |
497 | case oasys_record_is_named_section_enum: | |
498 | case oasys_record_is_end_enum: | |
82e51918 | 499 | if (! had_usefull) |
252b5132 RH |
500 | goto fail; |
501 | loop = false; | |
502 | break; | |
503 | default: | |
504 | goto fail; | |
505 | } | |
506 | } | |
507 | oasys->symbols = (asymbol *) NULL; | |
508 | /* | |
509 | Oasys support several architectures, but I can't see a simple way | |
510 | to discover which one is in a particular file - we'll guess | |
511 | */ | |
512 | bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0); | |
513 | if (abfd->symcount != 0) | |
514 | { | |
515 | abfd->flags |= HAS_SYMS; | |
516 | } | |
517 | ||
518 | /* | |
519 | We don't know if a section has data until we've read it.. | |
520 | */ | |
521 | ||
522 | oasys_slurp_section_data (abfd); | |
523 | ||
524 | ||
525 | return abfd->xvec; | |
526 | ||
527 | fail: | |
528 | (void) bfd_release (abfd, oasys); | |
529 | abfd->tdata.oasys_obj_data = save; | |
530 | return (const bfd_target *) NULL; | |
531 | } | |
532 | ||
533 | ||
534 | static void | |
535 | oasys_get_symbol_info (ignore_abfd, symbol, ret) | |
5f771d47 | 536 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
537 | asymbol *symbol; |
538 | symbol_info *ret; | |
539 | { | |
540 | bfd_symbol_info (symbol, ret); | |
541 | if (!symbol->section) | |
542 | ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A'; | |
543 | } | |
544 | ||
545 | static void | |
60b89a18 L |
546 | oasys_print_symbol (abfd, afile, symbol, how) |
547 | bfd *abfd; | |
252b5132 RH |
548 | PTR afile; |
549 | asymbol *symbol; | |
550 | bfd_print_symbol_type how; | |
551 | { | |
552 | FILE *file = (FILE *) afile; | |
553 | ||
554 | switch (how) | |
555 | { | |
556 | case bfd_print_symbol_name: | |
557 | case bfd_print_symbol_more: | |
558 | fprintf (file, "%s", symbol->name); | |
559 | break; | |
560 | case bfd_print_symbol_all: | |
561 | { | |
dc810e39 AM |
562 | const char *section_name = symbol->section == (asection *) NULL ? |
563 | (const char *) "*abs" : symbol->section->name; | |
252b5132 | 564 | |
60b89a18 | 565 | bfd_print_symbol_vandf (abfd, (PTR) file, symbol); |
252b5132 RH |
566 | |
567 | fprintf (file, " %-5s %s", | |
568 | section_name, | |
569 | symbol->name); | |
570 | } | |
571 | break; | |
572 | } | |
573 | } | |
574 | /* | |
575 | The howto table is build using the top two bits of a reloc byte to | |
576 | index into it. The bits are PCREL,WORD/LONG | |
577 | */ | |
578 | static reloc_howto_type howto_table[] = | |
579 | { | |
580 | ||
581 | HOWTO (0, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false), | |
582 | HOWTO (0, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "abs32", true, 0xffffffff, 0xffffffff, false), | |
583 | HOWTO (0, 0, 1, 16, true, 0, complain_overflow_signed, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, false), | |
584 | HOWTO (0, 0, 2, 32, true, 0, complain_overflow_signed, 0, "pcrel32", true, 0xffffffff, 0xffffffff, false) | |
585 | }; | |
586 | ||
587 | /* Read in all the section data and relocation stuff too */ | |
588 | static boolean | |
589 | oasys_slurp_section_data (abfd) | |
dc810e39 | 590 | bfd *const abfd; |
252b5132 RH |
591 | { |
592 | oasys_record_union_type record; | |
593 | oasys_data_type *data = OASYS_DATA (abfd); | |
594 | boolean loop = true; | |
252b5132 | 595 | oasys_per_section_type *per; |
252b5132 | 596 | asection *s; |
dc810e39 | 597 | bfd_size_type amt; |
252b5132 RH |
598 | |
599 | /* See if the data has been slurped already .. */ | |
600 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) | |
601 | { | |
602 | per = oasys_per_section (s); | |
82e51918 | 603 | if (per->initialized) |
252b5132 RH |
604 | return true; |
605 | } | |
606 | ||
607 | if (data->first_data_record == 0) | |
608 | return true; | |
609 | ||
610 | if (bfd_seek (abfd, data->first_data_record, SEEK_SET) != 0) | |
611 | return false; | |
612 | while (loop) | |
613 | { | |
614 | if (! oasys_read_record (abfd, &record)) | |
615 | return false; | |
616 | switch (record.header.type) | |
617 | { | |
618 | case oasys_record_is_header_enum: | |
619 | break; | |
620 | case oasys_record_is_data_enum: | |
621 | { | |
622 | ||
623 | bfd_byte *src = record.data.data; | |
624 | bfd_byte *end_src = ((bfd_byte *) & record) + record.header.length; | |
625 | bfd_byte *dst_ptr; | |
626 | bfd_byte *dst_base_ptr; | |
627 | unsigned int relbit; | |
628 | unsigned int count; | |
629 | asection *section = | |
630 | data->sections[record.data.relb & RELOCATION_SECT_BITS]; | |
631 | bfd_vma dst_offset; | |
632 | ||
633 | per = oasys_per_section (section); | |
634 | ||
82e51918 | 635 | if (! per->initialized) |
252b5132 RH |
636 | { |
637 | per->data = (bfd_byte *) bfd_zalloc (abfd, section->_raw_size); | |
638 | if (!per->data) | |
639 | return false; | |
dc810e39 AM |
640 | per->reloc_tail_ptr |
641 | = (oasys_reloc_type **) §ion->relocation; | |
252b5132 RH |
642 | per->had_vma = false; |
643 | per->initialized = true; | |
644 | section->reloc_count = 0; | |
645 | section->flags = SEC_ALLOC; | |
646 | } | |
647 | ||
dc810e39 | 648 | dst_offset = H_GET_32 (abfd, record.data.addr); |
82e51918 | 649 | if (! per->had_vma) |
252b5132 RH |
650 | { |
651 | /* Take the first vma we see as the base */ | |
652 | section->vma = dst_offset; | |
653 | per->had_vma = true; | |
654 | } | |
655 | ||
656 | dst_offset -= section->vma; | |
657 | ||
658 | dst_base_ptr = oasys_per_section (section)->data; | |
659 | dst_ptr = oasys_per_section (section)->data + | |
660 | dst_offset; | |
661 | ||
662 | if (src < end_src) | |
663 | { | |
664 | section->flags |= SEC_LOAD | SEC_HAS_CONTENTS; | |
665 | } | |
666 | while (src < end_src) | |
667 | { | |
668 | unsigned char mod_byte = *src++; | |
669 | size_t gap = end_src - src; | |
670 | ||
671 | count = 8; | |
672 | if (mod_byte == 0 && gap >= 8) | |
673 | { | |
674 | dst_ptr[0] = src[0]; | |
675 | dst_ptr[1] = src[1]; | |
676 | dst_ptr[2] = src[2]; | |
677 | dst_ptr[3] = src[3]; | |
678 | dst_ptr[4] = src[4]; | |
679 | dst_ptr[5] = src[5]; | |
680 | dst_ptr[6] = src[6]; | |
681 | dst_ptr[7] = src[7]; | |
682 | dst_ptr += 8; | |
683 | src += 8; | |
684 | } | |
685 | else | |
686 | { | |
687 | for (relbit = 1; count-- != 0 && src < end_src; relbit <<= 1) | |
688 | { | |
689 | if (relbit & mod_byte) | |
690 | { | |
691 | unsigned char reloc = *src; | |
692 | /* This item needs to be relocated */ | |
693 | switch (reloc & RELOCATION_TYPE_BITS) | |
694 | { | |
695 | case RELOCATION_TYPE_ABS: | |
696 | ||
697 | break; | |
698 | ||
699 | case RELOCATION_TYPE_REL: | |
700 | { | |
701 | /* Relocate the item relative to the section */ | |
dc810e39 AM |
702 | oasys_reloc_type *r; |
703 | ||
704 | amt = sizeof (oasys_reloc_type); | |
705 | r = (oasys_reloc_type *) bfd_alloc (abfd, | |
706 | amt); | |
252b5132 RH |
707 | if (!r) |
708 | return false; | |
709 | *(per->reloc_tail_ptr) = r; | |
710 | per->reloc_tail_ptr = &r->next; | |
711 | r->next = (oasys_reloc_type *) NULL; | |
712 | /* Reference to undefined symbol */ | |
713 | src++; | |
714 | /* There is no symbol */ | |
715 | r->symbol = 0; | |
716 | /* Work out the howto */ | |
717 | abort (); | |
718 | #if 0 | |
719 | r->relent.section = | |
720 | data->sections[reloc & | |
721 | RELOCATION_SECT_BITS]; | |
722 | ||
723 | r->relent.addend = - | |
724 | r->relent.section->vma; | |
725 | #endif | |
726 | r->relent.address = dst_ptr - dst_base_ptr; | |
727 | r->relent.howto = &howto_table[reloc >> 6]; | |
728 | r->relent.sym_ptr_ptr = (asymbol **) NULL; | |
729 | section->reloc_count++; | |
730 | ||
82e51918 AM |
731 | /* Fake up the data to look like |
732 | it's got the -ve pc in it, this | |
733 | makes it much easier to convert | |
734 | into other formats. This is done | |
735 | by hitting the addend. */ | |
736 | if (r->relent.howto->pc_relative) | |
737 | r->relent.addend -= dst_ptr - dst_base_ptr; | |
252b5132 RH |
738 | } |
739 | break; | |
740 | ||
741 | ||
742 | case RELOCATION_TYPE_UND: | |
743 | { | |
dc810e39 AM |
744 | oasys_reloc_type *r; |
745 | ||
746 | amt = sizeof (oasys_reloc_type); | |
747 | r = (oasys_reloc_type *) bfd_alloc (abfd, | |
748 | amt); | |
252b5132 RH |
749 | if (!r) |
750 | return false; | |
751 | *(per->reloc_tail_ptr) = r; | |
752 | per->reloc_tail_ptr = &r->next; | |
753 | r->next = (oasys_reloc_type *) NULL; | |
754 | /* Reference to undefined symbol */ | |
755 | src++; | |
756 | /* Get symbol number */ | |
757 | r->symbol = (src[0] << 8) | src[1]; | |
758 | /* Work out the howto */ | |
759 | abort (); | |
760 | ||
761 | #if 0 | |
762 | r->relent.section = (asection | |
763 | *) NULL; | |
764 | #endif | |
765 | r->relent.addend = 0; | |
766 | r->relent.address = dst_ptr - dst_base_ptr; | |
767 | r->relent.howto = &howto_table[reloc >> 6]; | |
768 | r->relent.sym_ptr_ptr = (asymbol **) NULL; | |
769 | section->reloc_count++; | |
770 | ||
771 | src += 2; | |
82e51918 AM |
772 | /* Fake up the data to look like |
773 | it's got the -ve pc in it, this | |
774 | makes it much easier to convert | |
775 | into other formats. This is done | |
776 | by hitting the addend. */ | |
777 | if (r->relent.howto->pc_relative) | |
778 | r->relent.addend -= dst_ptr - dst_base_ptr; | |
252b5132 RH |
779 | } |
780 | break; | |
781 | case RELOCATION_TYPE_COM: | |
782 | BFD_FAIL (); | |
783 | } | |
784 | } | |
785 | *dst_ptr++ = *src++; | |
786 | } | |
787 | } | |
788 | } | |
789 | } | |
790 | break; | |
791 | case oasys_record_is_local_enum: | |
792 | case oasys_record_is_symbol_enum: | |
793 | case oasys_record_is_section_enum: | |
794 | break; | |
795 | default: | |
796 | loop = false; | |
797 | } | |
798 | } | |
799 | ||
800 | return true; | |
801 | ||
802 | } | |
803 | ||
804 | static boolean | |
805 | oasys_new_section_hook (abfd, newsect) | |
806 | bfd *abfd; | |
807 | asection *newsect; | |
808 | { | |
809 | newsect->used_by_bfd = (PTR) | |
dc810e39 | 810 | bfd_alloc (abfd, (bfd_size_type) sizeof (oasys_per_section_type)); |
252b5132 RH |
811 | if (!newsect->used_by_bfd) |
812 | return false; | |
813 | oasys_per_section (newsect)->data = (bfd_byte *) NULL; | |
814 | oasys_per_section (newsect)->section = newsect; | |
815 | oasys_per_section (newsect)->offset = 0; | |
816 | oasys_per_section (newsect)->initialized = false; | |
817 | newsect->alignment_power = 1; | |
818 | /* Turn the section string into an index */ | |
819 | ||
820 | sscanf (newsect->name, "%u", &newsect->target_index); | |
821 | ||
822 | return true; | |
823 | } | |
824 | ||
825 | ||
826 | static long | |
827 | oasys_get_reloc_upper_bound (abfd, asect) | |
828 | bfd *abfd; | |
829 | sec_ptr asect; | |
830 | { | |
831 | if (! oasys_slurp_section_data (abfd)) | |
832 | return -1; | |
833 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
834 | } | |
835 | ||
836 | static boolean | |
837 | oasys_get_section_contents (abfd, section, location, offset, count) | |
838 | bfd *abfd; | |
839 | sec_ptr section; | |
840 | PTR location; | |
841 | file_ptr offset; | |
842 | bfd_size_type count; | |
843 | { | |
844 | oasys_per_section_type *p = (oasys_per_section_type *) section->used_by_bfd; | |
845 | oasys_slurp_section_data (abfd); | |
82e51918 | 846 | if (! p->initialized) |
252b5132 | 847 | { |
dc810e39 | 848 | (void) memset (location, 0, (size_t) count); |
252b5132 RH |
849 | } |
850 | else | |
851 | { | |
dc810e39 | 852 | (void) memcpy (location, (PTR) (p->data + offset), (size_t) count); |
252b5132 RH |
853 | } |
854 | return true; | |
855 | } | |
856 | ||
857 | ||
858 | long | |
859 | oasys_canonicalize_reloc (ignore_abfd, section, relptr, symbols) | |
5f771d47 | 860 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
861 | sec_ptr section; |
862 | arelent **relptr; | |
5f771d47 | 863 | asymbol **symbols ATTRIBUTE_UNUSED; |
252b5132 RH |
864 | { |
865 | unsigned int reloc_count = 0; | |
866 | oasys_reloc_type *src = (oasys_reloc_type *) (section->relocation); | |
867 | while (src != (oasys_reloc_type *) NULL) | |
868 | { | |
869 | abort (); | |
870 | ||
871 | #if 0 | |
872 | if (src->relent.section == (asection *) NULL) | |
873 | { | |
874 | src->relent.sym_ptr_ptr = symbols + src->symbol; | |
875 | } | |
876 | #endif | |
877 | ||
878 | *relptr++ = &src->relent; | |
879 | src = src->next; | |
880 | reloc_count++; | |
881 | } | |
882 | *relptr = (arelent *) NULL; | |
883 | return section->reloc_count = reloc_count; | |
884 | } | |
885 | ||
886 | ||
887 | ||
888 | ||
889 | /* Writing */ | |
890 | ||
891 | ||
892 | /* Calculate the checksum and write one record */ | |
893 | static boolean | |
894 | oasys_write_record (abfd, type, record, size) | |
895 | bfd *abfd; | |
896 | oasys_record_enum_type type; | |
897 | oasys_record_union_type *record; | |
898 | size_t size; | |
899 | { | |
900 | int checksum; | |
901 | size_t i; | |
902 | unsigned char *ptr; | |
903 | ||
904 | record->header.length = size; | |
905 | record->header.type = (int) type; | |
906 | record->header.check_sum = 0; | |
907 | record->header.fill = 0; | |
908 | ptr = (unsigned char *) &record->pad[0]; | |
909 | checksum = 0; | |
910 | for (i = 0; i < size; i++) | |
911 | { | |
912 | checksum += *ptr++; | |
913 | } | |
914 | record->header.check_sum = 0xff & (-checksum); | |
dc810e39 | 915 | if (bfd_bwrite ((PTR) record, (bfd_size_type) size, abfd) != size) |
252b5132 RH |
916 | return false; |
917 | return true; | |
918 | } | |
919 | ||
920 | ||
921 | /* Write out all the symbols */ | |
922 | static boolean | |
923 | oasys_write_syms (abfd) | |
924 | bfd *abfd; | |
925 | { | |
926 | unsigned int count; | |
927 | asymbol **generic = bfd_get_outsymbols (abfd); | |
928 | unsigned int index = 0; | |
929 | for (count = 0; count < bfd_get_symcount (abfd); count++) | |
930 | { | |
931 | ||
932 | oasys_symbol_record_type symbol; | |
dc810e39 | 933 | asymbol *const g = generic[count]; |
252b5132 | 934 | |
dc810e39 | 935 | const char *src = g->name; |
252b5132 RH |
936 | char *dst = symbol.name; |
937 | unsigned int l = 0; | |
938 | ||
939 | if (bfd_is_com_section (g->section)) | |
940 | { | |
941 | symbol.relb = RELOCATION_TYPE_COM; | |
dc810e39 | 942 | H_PUT_16 (abfd, index, symbol.refno); |
252b5132 RH |
943 | index++; |
944 | } | |
945 | else if (bfd_is_abs_section (g->section)) | |
946 | { | |
947 | symbol.relb = RELOCATION_TYPE_ABS; | |
dc810e39 | 948 | H_PUT_16 (abfd, 0, symbol.refno); |
252b5132 RH |
949 | |
950 | } | |
951 | else if (bfd_is_und_section (g->section)) | |
952 | { | |
953 | symbol.relb = RELOCATION_TYPE_UND; | |
dc810e39 | 954 | H_PUT_16 (abfd, index, symbol.refno); |
252b5132 RH |
955 | /* Overload the value field with the output index number */ |
956 | index++; | |
957 | } | |
958 | else if (g->flags & BSF_DEBUGGING) | |
959 | { | |
960 | /* throw it away */ | |
961 | continue; | |
962 | } | |
963 | else | |
964 | { | |
965 | if (g->section == (asection *) NULL) | |
966 | { | |
967 | /* Sometime, the oasys tools give out a symbol with illegal | |
968 | bits in it, we'll output it in the same broken way */ | |
969 | ||
970 | symbol.relb = RELOCATION_TYPE_REL | 0; | |
971 | } | |
972 | else | |
973 | { | |
974 | symbol.relb = RELOCATION_TYPE_REL | g->section->output_section->target_index; | |
975 | } | |
dc810e39 | 976 | H_PUT_16 (abfd, 0, symbol.refno); |
252b5132 RH |
977 | } |
978 | #ifdef UNDERSCORE_HACK | |
979 | if (src[l] == '_') | |
980 | dst[l++] = '.'; | |
981 | #endif | |
982 | while (src[l]) | |
983 | { | |
984 | dst[l] = src[l]; | |
985 | l++; | |
986 | } | |
987 | ||
dc810e39 | 988 | H_PUT_32 (abfd, g->value, symbol.value); |
252b5132 RH |
989 | |
990 | ||
991 | if (g->flags & BSF_LOCAL) | |
992 | { | |
993 | if (! oasys_write_record (abfd, | |
994 | oasys_record_is_local_enum, | |
995 | (oasys_record_union_type *) & symbol, | |
996 | offsetof (oasys_symbol_record_type, | |
997 | name[0]) + l)) | |
998 | return false; | |
999 | } | |
1000 | else | |
1001 | { | |
1002 | if (! oasys_write_record (abfd, | |
1003 | oasys_record_is_symbol_enum, | |
1004 | (oasys_record_union_type *) & symbol, | |
1005 | offsetof (oasys_symbol_record_type, | |
1006 | name[0]) + l)) | |
1007 | return false; | |
1008 | } | |
1009 | g->value = index - 1; | |
1010 | } | |
1011 | ||
1012 | return true; | |
1013 | } | |
1014 | ||
1015 | ||
1016 | /* Write a section header for each section */ | |
1017 | static boolean | |
1018 | oasys_write_sections (abfd) | |
1019 | bfd *abfd; | |
1020 | { | |
1021 | asection *s; | |
1022 | static oasys_section_record_type out; | |
1023 | ||
1024 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) | |
1025 | { | |
3882b010 | 1026 | if (!ISDIGIT (s->name[0])) |
252b5132 RH |
1027 | { |
1028 | (*_bfd_error_handler) | |
1029 | (_("%s: can not represent section `%s' in oasys"), | |
1030 | bfd_get_filename (abfd), s->name); | |
1031 | bfd_set_error (bfd_error_nonrepresentable_section); | |
1032 | return false; | |
1033 | } | |
1034 | out.relb = RELOCATION_TYPE_REL | s->target_index; | |
dc810e39 AM |
1035 | H_PUT_32 (abfd, s->_cooked_size, out.value); |
1036 | H_PUT_32 (abfd, s->vma, out.vma); | |
252b5132 RH |
1037 | |
1038 | if (! oasys_write_record (abfd, | |
1039 | oasys_record_is_section_enum, | |
1040 | (oasys_record_union_type *) & out, | |
1041 | sizeof (out))) | |
1042 | return false; | |
1043 | } | |
1044 | return true; | |
1045 | } | |
1046 | ||
1047 | static boolean | |
1048 | oasys_write_header (abfd) | |
1049 | bfd *abfd; | |
1050 | { | |
1051 | /* Create and write the header */ | |
1052 | oasys_header_record_type r; | |
1053 | size_t length = strlen (abfd->filename); | |
1054 | if (length > (size_t) sizeof (r.module_name)) | |
1055 | { | |
1056 | length = sizeof (r.module_name); | |
1057 | } | |
1058 | ||
1059 | (void) memcpy (r.module_name, | |
1060 | abfd->filename, | |
1061 | length); | |
1062 | (void) memset (r.module_name + length, | |
1063 | ' ', | |
1064 | sizeof (r.module_name) - length); | |
1065 | ||
1066 | r.version_number = OASYS_VERSION_NUMBER; | |
1067 | r.rev_number = OASYS_REV_NUMBER; | |
1068 | if (! oasys_write_record (abfd, | |
1069 | oasys_record_is_header_enum, | |
1070 | (oasys_record_union_type *) & r, | |
1071 | offsetof (oasys_header_record_type, | |
1072 | description[0]))) | |
1073 | return false; | |
1074 | ||
1075 | return true; | |
1076 | } | |
1077 | ||
1078 | static boolean | |
1079 | oasys_write_end (abfd) | |
1080 | bfd *abfd; | |
1081 | { | |
1082 | oasys_end_record_type end; | |
1083 | unsigned char null = 0; | |
1084 | end.relb = RELOCATION_TYPE_ABS; | |
dc810e39 AM |
1085 | H_PUT_32 (abfd, abfd->start_address, end.entry); |
1086 | H_PUT_16 (abfd, 0, end.fill); | |
252b5132 RH |
1087 | end.zero = 0; |
1088 | if (! oasys_write_record (abfd, | |
1089 | oasys_record_is_end_enum, | |
1090 | (oasys_record_union_type *) & end, | |
1091 | sizeof (end))) | |
1092 | return false; | |
dc810e39 | 1093 | if (bfd_bwrite ((PTR) &null, (bfd_size_type) 1, abfd) != 1) |
252b5132 RH |
1094 | return false; |
1095 | return true; | |
1096 | } | |
1097 | ||
1098 | static int | |
1099 | comp (ap, bp) | |
dc810e39 AM |
1100 | const PTR ap; |
1101 | const PTR bp; | |
252b5132 RH |
1102 | { |
1103 | arelent *a = *((arelent **) ap); | |
1104 | arelent *b = *((arelent **) bp); | |
1105 | return a->address - b->address; | |
1106 | } | |
1107 | ||
1108 | /* | |
1109 | Writing data.. | |
1110 | ||
1111 | */ | |
1112 | static boolean | |
1113 | oasys_write_data (abfd) | |
1114 | bfd *abfd; | |
1115 | { | |
1116 | asection *s; | |
1117 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) | |
1118 | { | |
1119 | if (s->flags & SEC_LOAD) | |
1120 | { | |
1121 | bfd_byte *raw_data = oasys_per_section (s)->data; | |
1122 | oasys_data_record_type processed_data; | |
1123 | bfd_size_type current_byte_index = 0; | |
1124 | unsigned int relocs_to_go = s->reloc_count; | |
1125 | arelent **p = s->orelocation; | |
1126 | if (s->reloc_count != 0) | |
1127 | { | |
1128 | /* Sort the reloc records so it's easy to insert the relocs into the | |
1129 | data */ | |
1130 | ||
1131 | qsort (s->orelocation, | |
1132 | s->reloc_count, | |
1133 | sizeof (arelent **), | |
1134 | comp); | |
1135 | } | |
1136 | current_byte_index = 0; | |
1137 | processed_data.relb = s->target_index | RELOCATION_TYPE_REL; | |
1138 | ||
1139 | while (current_byte_index < s->_cooked_size) | |
1140 | { | |
1141 | /* Scan forwards by eight bytes or however much is left and see if | |
1142 | there are any relocations going on */ | |
1143 | bfd_byte *mod = &processed_data.data[0]; | |
1144 | bfd_byte *dst = &processed_data.data[1]; | |
1145 | ||
1146 | unsigned int i = 0; | |
1147 | *mod = 0; | |
1148 | ||
1149 | ||
dc810e39 AM |
1150 | H_PUT_32 (abfd, s->vma + current_byte_index, |
1151 | processed_data.addr); | |
252b5132 RH |
1152 | |
1153 | /* Don't start a relocation unless you're sure you can finish it | |
1154 | within the same data record. The worst case relocation is a | |
1155 | 4-byte relocatable value which is split across two modification | |
1156 | bytes (1 relocation byte + 2 symbol reference bytes + 2 data + | |
1157 | 1 modification byte + 2 data = 8 bytes total). That's where | |
1158 | the magic number 8 comes from. | |
1159 | */ | |
1160 | while (current_byte_index < s->_raw_size && dst <= | |
1161 | &processed_data.data[sizeof (processed_data.data) - 8]) | |
1162 | { | |
1163 | ||
1164 | ||
1165 | if (relocs_to_go != 0) | |
1166 | { | |
1167 | arelent *r = *p; | |
1168 | reloc_howto_type *const how = r->howto; | |
1169 | /* There is a relocation, is it for this byte ? */ | |
1170 | if (r->address == current_byte_index) | |
1171 | { | |
1172 | unsigned char rel_byte; | |
1173 | ||
1174 | p++; | |
1175 | relocs_to_go--; | |
1176 | ||
1177 | *mod |= (1 << i); | |
1178 | if (how->pc_relative) | |
1179 | { | |
1180 | rel_byte = RELOCATION_PCREL_BIT; | |
1181 | ||
1182 | /* Also patch the raw data so that it doesn't have | |
1183 | the -ve stuff any more */ | |
1184 | if (how->size != 2) | |
1185 | { | |
1186 | bfd_put_16 (abfd, | |
1187 | bfd_get_16 (abfd, raw_data) + | |
1188 | current_byte_index, raw_data); | |
1189 | } | |
1190 | ||
1191 | else | |
1192 | { | |
1193 | bfd_put_32 (abfd, | |
1194 | bfd_get_32 (abfd, raw_data) + | |
1195 | current_byte_index, raw_data); | |
1196 | } | |
1197 | } | |
1198 | else | |
1199 | { | |
1200 | rel_byte = 0; | |
1201 | } | |
1202 | if (how->size == 2) | |
1203 | { | |
1204 | rel_byte |= RELOCATION_32BIT_BIT; | |
1205 | } | |
1206 | ||
1207 | /* Is this a section relative relocation, or a symbol | |
1208 | relative relocation ? */ | |
1209 | abort (); | |
1210 | ||
1211 | #if 0 | |
1212 | if (r->section != (asection *) NULL) | |
1213 | { | |
1214 | /* The relent has a section attached, so it must be section | |
1215 | relative */ | |
1216 | rel_byte |= RELOCATION_TYPE_REL; | |
1217 | rel_byte |= r->section->output_section->target_index; | |
1218 | *dst++ = rel_byte; | |
1219 | } | |
1220 | else | |
1221 | #endif | |
1222 | { | |
dc810e39 | 1223 | asymbol *sym = *(r->sym_ptr_ptr); |
252b5132 RH |
1224 | |
1225 | /* If this symbol has a section attached, then it | |
1226 | has already been resolved. Change from a symbol | |
1227 | ref to a section ref */ | |
dc810e39 | 1228 | if (sym->section != (asection *) NULL) |
252b5132 RH |
1229 | { |
1230 | rel_byte |= RELOCATION_TYPE_REL; | |
1231 | rel_byte |= | |
dc810e39 | 1232 | sym->section->output_section->target_index; |
252b5132 RH |
1233 | *dst++ = rel_byte; |
1234 | } | |
1235 | else | |
1236 | { | |
1237 | rel_byte |= RELOCATION_TYPE_UND; | |
1238 | *dst++ = rel_byte; | |
1239 | /* Next two bytes are a symbol index - we can get | |
1240 | this from the symbol value which has been zapped | |
1241 | into the symbol index in the table when the | |
1242 | symbol table was written | |
1243 | */ | |
dc810e39 AM |
1244 | *dst++ = sym->value >> 8; |
1245 | *dst++ = sym->value; | |
252b5132 RH |
1246 | } |
1247 | } | |
1248 | #define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; } | |
1249 | /* relocations never occur from an unloadable section, | |
1250 | so we can assume that raw_data is not NULL | |
1251 | */ | |
1252 | *dst++ = *raw_data++; | |
1253 | ADVANCE | |
1254 | * dst++ = *raw_data++; | |
1255 | ADVANCE | |
1256 | if (how->size == 2) | |
1257 | { | |
1258 | *dst++ = *raw_data++; | |
1259 | ADVANCE | |
1260 | * dst++ = *raw_data++; | |
1261 | ADVANCE | |
1262 | } | |
1263 | continue; | |
1264 | } | |
1265 | } | |
1266 | /* If this is coming from an unloadable section then copy | |
1267 | zeros */ | |
1268 | if (raw_data == NULL) | |
1269 | { | |
1270 | *dst++ = 0; | |
1271 | } | |
1272 | else | |
1273 | { | |
1274 | *dst++ = *raw_data++; | |
1275 | } | |
1276 | ADVANCE | |
1277 | } | |
1278 | ||
1279 | /* Don't write a useless null modification byte */ | |
1280 | if (dst == mod + 1) | |
1281 | { | |
1282 | --dst; | |
1283 | } | |
1284 | ||
dc810e39 AM |
1285 | if (! (oasys_write_record |
1286 | (abfd, oasys_record_is_data_enum, | |
1287 | ((oasys_record_union_type *) &processed_data), | |
1288 | (size_t) (dst - (bfd_byte *) &processed_data)))) | |
252b5132 RH |
1289 | return false; |
1290 | } | |
1291 | } | |
1292 | } | |
1293 | ||
1294 | return true; | |
1295 | } | |
1296 | ||
1297 | static boolean | |
1298 | oasys_write_object_contents (abfd) | |
1299 | bfd *abfd; | |
1300 | { | |
1301 | if (! oasys_write_header (abfd)) | |
1302 | return false; | |
1303 | if (! oasys_write_syms (abfd)) | |
1304 | return false; | |
1305 | if (! oasys_write_sections (abfd)) | |
1306 | return false; | |
1307 | if (! oasys_write_data (abfd)) | |
1308 | return false; | |
1309 | if (! oasys_write_end (abfd)) | |
1310 | return false; | |
1311 | return true; | |
1312 | } | |
1313 | ||
1314 | ||
1315 | ||
1316 | ||
1317 | /** exec and core file sections */ | |
1318 | ||
1319 | /* set section contents is complicated with OASYS since the format is | |
1320 | * not a byte image, but a record stream. | |
1321 | */ | |
1322 | static boolean | |
1323 | oasys_set_section_contents (abfd, section, location, offset, count) | |
1324 | bfd *abfd; | |
1325 | sec_ptr section; | |
1326 | PTR location; | |
1327 | file_ptr offset; | |
1328 | bfd_size_type count; | |
1329 | { | |
1330 | if (count != 0) | |
1331 | { | |
1332 | if (oasys_per_section (section)->data == (bfd_byte *) NULL) | |
1333 | { | |
1334 | oasys_per_section (section)->data = | |
1335 | (bfd_byte *) (bfd_alloc (abfd, section->_cooked_size)); | |
1336 | if (!oasys_per_section (section)->data) | |
1337 | return false; | |
1338 | } | |
1339 | (void) memcpy ((PTR) (oasys_per_section (section)->data + offset), | |
1340 | location, | |
1341 | (size_t) count); | |
1342 | } | |
1343 | return true; | |
1344 | } | |
1345 | ||
1346 | ||
1347 | ||
1348 | /* Native-level interface to symbols. */ | |
1349 | ||
1350 | /* We read the symbols into a buffer, which is discarded when this | |
1351 | function exits. We read the strings into a buffer large enough to | |
1352 | hold them all plus all the cached symbol entries. */ | |
1353 | ||
1354 | static asymbol * | |
1355 | oasys_make_empty_symbol (abfd) | |
1356 | bfd *abfd; | |
1357 | { | |
dc810e39 AM |
1358 | bfd_size_type amt = sizeof (oasys_symbol_type); |
1359 | oasys_symbol_type *new = (oasys_symbol_type *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
1360 | if (!new) |
1361 | return NULL; | |
1362 | new->symbol.the_bfd = abfd; | |
1363 | return &new->symbol; | |
1364 | } | |
1365 | \f | |
1366 | ||
1367 | ||
1368 | ||
1369 | /* User should have checked the file flags; perhaps we should return | |
1370 | BFD_NO_MORE_SYMBOLS if there are none? */ | |
1371 | ||
1372 | static bfd * | |
1373 | oasys_openr_next_archived_file (arch, prev) | |
1374 | bfd *arch; | |
1375 | bfd *prev; | |
1376 | { | |
1377 | oasys_ar_data_type *ar = OASYS_AR_DATA (arch); | |
1378 | oasys_module_info_type *p; | |
1379 | /* take the next one from the arch state, or reset */ | |
1380 | if (prev == (bfd *) NULL) | |
1381 | { | |
1382 | /* Reset the index - the first two entries are bogus*/ | |
1383 | ar->module_index = 0; | |
1384 | } | |
1385 | ||
1386 | p = ar->module + ar->module_index; | |
1387 | ar->module_index++; | |
1388 | ||
1389 | if (ar->module_index <= ar->module_count) | |
1390 | { | |
1391 | if (p->abfd == (bfd *) NULL) | |
1392 | { | |
1393 | p->abfd = _bfd_create_empty_archive_element_shell (arch); | |
1394 | p->abfd->origin = p->pos; | |
1395 | p->abfd->filename = p->name; | |
1396 | ||
1397 | /* Fixup a pointer to this element for the member */ | |
1398 | p->abfd->arelt_data = (PTR) p; | |
1399 | } | |
1400 | return p->abfd; | |
1401 | } | |
1402 | else | |
1403 | { | |
1404 | bfd_set_error (bfd_error_no_more_archived_files); | |
1405 | return (bfd *) NULL; | |
1406 | } | |
1407 | } | |
1408 | ||
1409 | static boolean | |
dc810e39 AM |
1410 | oasys_find_nearest_line (abfd, section, symbols, offset, |
1411 | filename_ptr, functionname_ptr, line_ptr) | |
5f771d47 ILT |
1412 | bfd *abfd ATTRIBUTE_UNUSED; |
1413 | asection *section ATTRIBUTE_UNUSED; | |
1414 | asymbol **symbols ATTRIBUTE_UNUSED; | |
1415 | bfd_vma offset ATTRIBUTE_UNUSED; | |
dc810e39 AM |
1416 | const char **filename_ptr ATTRIBUTE_UNUSED; |
1417 | const char **functionname_ptr ATTRIBUTE_UNUSED; | |
5f771d47 | 1418 | unsigned int *line_ptr ATTRIBUTE_UNUSED; |
252b5132 RH |
1419 | { |
1420 | return false; | |
1421 | ||
1422 | } | |
1423 | ||
1424 | static int | |
1425 | oasys_generic_stat_arch_elt (abfd, buf) | |
1426 | bfd *abfd; | |
1427 | struct stat *buf; | |
1428 | { | |
1429 | oasys_module_info_type *mod = (oasys_module_info_type *) abfd->arelt_data; | |
1430 | if (mod == (oasys_module_info_type *) NULL) | |
1431 | { | |
1432 | bfd_set_error (bfd_error_invalid_operation); | |
1433 | return -1; | |
1434 | } | |
1435 | else | |
1436 | { | |
1437 | buf->st_size = mod->size; | |
1438 | buf->st_mode = 0666; | |
1439 | return 0; | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | static int | |
1444 | oasys_sizeof_headers (abfd, exec) | |
5f771d47 ILT |
1445 | bfd *abfd ATTRIBUTE_UNUSED; |
1446 | boolean exec ATTRIBUTE_UNUSED; | |
252b5132 RH |
1447 | { |
1448 | return 0; | |
1449 | } | |
1450 | ||
1451 | #define oasys_close_and_cleanup _bfd_generic_close_and_cleanup | |
1452 | #define oasys_bfd_free_cached_info _bfd_generic_bfd_free_cached_info | |
1453 | ||
1454 | #define oasys_slurp_armap bfd_true | |
1455 | #define oasys_slurp_extended_name_table bfd_true | |
1456 | #define oasys_construct_extended_name_table \ | |
1457 | ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \ | |
1458 | bfd_true) | |
1459 | #define oasys_truncate_arname bfd_dont_truncate_arname | |
1460 | #define oasys_write_armap \ | |
1461 | ((boolean (*) \ | |
1462 | PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \ | |
1463 | bfd_true) | |
1464 | #define oasys_read_ar_hdr bfd_nullvoidptr | |
1465 | #define oasys_get_elt_at_index _bfd_generic_get_elt_at_index | |
1466 | #define oasys_update_armap_timestamp bfd_true | |
1467 | ||
1468 | #define oasys_bfd_is_local_label_name bfd_generic_is_local_label_name | |
1469 | #define oasys_get_lineno _bfd_nosymbols_get_lineno | |
1470 | #define oasys_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol | |
1471 | #define oasys_read_minisymbols _bfd_generic_read_minisymbols | |
1472 | #define oasys_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol | |
1473 | ||
1474 | #define oasys_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup | |
1475 | ||
1476 | #define oasys_set_arch_mach bfd_default_set_arch_mach | |
1477 | ||
1478 | #define oasys_get_section_contents_in_window \ | |
1479 | _bfd_generic_get_section_contents_in_window | |
1480 | ||
1481 | #define oasys_bfd_get_relocated_section_contents \ | |
1482 | bfd_generic_get_relocated_section_contents | |
1483 | #define oasys_bfd_relax_section bfd_generic_relax_section | |
1484 | #define oasys_bfd_gc_sections bfd_generic_gc_sections | |
8550eb6e | 1485 | #define oasys_bfd_merge_sections bfd_generic_merge_sections |
e61463e1 | 1486 | #define oasys_bfd_discard_group bfd_generic_discard_group |
252b5132 | 1487 | #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create |
e2d34d7d | 1488 | #define oasys_bfd_link_hash_table_free _bfd_generic_link_hash_table_free |
252b5132 | 1489 | #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols |
2d653fc7 | 1490 | #define oasys_bfd_link_just_syms _bfd_generic_link_just_syms |
252b5132 RH |
1491 | #define oasys_bfd_final_link _bfd_generic_final_link |
1492 | #define oasys_bfd_link_split_section _bfd_generic_link_split_section | |
1493 | ||
1494 | /*SUPPRESS 460 */ | |
1495 | const bfd_target oasys_vec = | |
1496 | { | |
1497 | "oasys", /* name */ | |
1498 | bfd_target_oasys_flavour, | |
1499 | BFD_ENDIAN_BIG, /* target byte order */ | |
1500 | BFD_ENDIAN_BIG, /* target headers byte order */ | |
1501 | (HAS_RELOC | EXEC_P | /* object flags */ | |
1502 | HAS_LINENO | HAS_DEBUG | | |
1503 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | |
1504 | (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS | |
1505 | | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
1506 | 0, /* leading underscore */ | |
1507 | ' ', /* ar_pad_char */ | |
1508 | 16, /* ar_max_namelen */ | |
1509 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
1510 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1511 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ | |
1512 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
1513 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1514 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ | |
1515 | ||
1516 | {_bfd_dummy_target, | |
1517 | oasys_object_p, /* bfd_check_format */ | |
1518 | oasys_archive_p, | |
1519 | _bfd_dummy_target, | |
1520 | }, | |
1521 | { /* bfd_set_format */ | |
1522 | bfd_false, | |
1523 | oasys_mkobject, | |
1524 | _bfd_generic_mkarchive, | |
1525 | bfd_false | |
1526 | }, | |
1527 | { /* bfd_write_contents */ | |
1528 | bfd_false, | |
1529 | oasys_write_object_contents, | |
1530 | _bfd_write_archive_contents, | |
1531 | bfd_false, | |
1532 | }, | |
1533 | ||
1534 | BFD_JUMP_TABLE_GENERIC (oasys), | |
1535 | BFD_JUMP_TABLE_COPY (_bfd_generic), | |
1536 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | |
1537 | BFD_JUMP_TABLE_ARCHIVE (oasys), | |
1538 | BFD_JUMP_TABLE_SYMBOLS (oasys), | |
1539 | BFD_JUMP_TABLE_RELOCS (oasys), | |
1540 | BFD_JUMP_TABLE_WRITE (oasys), | |
1541 | BFD_JUMP_TABLE_LINK (oasys), | |
1542 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
1543 | ||
c3c89269 | 1544 | NULL, |
dc810e39 | 1545 | |
252b5132 RH |
1546 | (PTR) 0 |
1547 | }; |