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