]>
Commit | Line | Data |
---|---|---|
4a8db330 | 1 | /* BFD back-end for s-record objects. |
8f8fefcc | 2 | Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc. |
9898b929 | 3 | Written by Steve Chamberlain of Cygnus Support <[email protected]>. |
4a81b561 | 4 | |
387cbb2b | 5 | This file is part of BFD, the Binary File Descriptor library. |
4a81b561 | 6 | |
387cbb2b | 7 | This program is free software; you can redistribute it and/or modify |
4a81b561 | 8 | it under the terms of the GNU General Public License as published by |
387cbb2b JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
4a81b561 | 11 | |
387cbb2b | 12 | This program is distributed in the hope that it will be useful, |
4a81b561 DHW |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
387cbb2b JG |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
4a81b561 | 20 | |
9898b929 JG |
21 | /* |
22 | SUBSECTION | |
8f8fefcc | 23 | S-Record handling |
4a81b561 | 24 | |
9898b929 JG |
25 | DESCRIPTION |
26 | ||
8f8fefcc JG |
27 | Ordinary S-Records cannot hold anything but addresses and |
28 | data, so that's all that we implement. | |
9898b929 | 29 | |
8f8fefcc | 30 | The only interesting thing is that S-Records may come out of |
9898b929 JG |
31 | order and there is no header, so an initial scan is required |
32 | to discover the minimum and maximum addresses used to create | |
33 | the vma and size of the only section we create. We | |
34 | arbitrarily call this section ".text". | |
35 | ||
36 | When bfd_get_section_contents is called the file is read | |
37 | again, and this time the data is placed into a bfd_alloc'd | |
38 | area. | |
39 | ||
40 | Any number of sections may be created for output, we save them | |
41 | up and output them when it's time to close the bfd. | |
42 | ||
43 | An s record looks like: | |
44 | ||
45 | EXAMPLE | |
294eaca4 | 46 | S<type><length><address><data><checksum> |
9898b929 JG |
47 | |
48 | DESCRIPTION | |
49 | Where | |
50 | o length | |
51 | is the number of bytes following upto the checksum. Note that | |
52 | this is not the number of chars following, since it takes two | |
53 | chars to represent a byte. | |
54 | o type | |
55 | is one of: | |
56 | 0) header record | |
57 | 1) two byte address data record | |
58 | 2) three byte address data record | |
59 | 3) four byte address data record | |
60 | 7) four byte address termination record | |
61 | 8) three byte address termination record | |
62 | 9) two byte address termination record | |
63 | ||
64 | o address | |
65 | is the start address of the data following, or in the case of | |
66 | a termination record, the start address of the image | |
67 | o data | |
68 | is the data. | |
69 | o checksum | |
70 | is the sum of all the raw byte data in the record, from the length | |
71 | upwards, modulo 256 and subtracted from 255. | |
8f8fefcc JG |
72 | |
73 | ||
74 | SUBSECTION | |
75 | Symbol S-Record handling | |
76 | ||
77 | DESCRIPTION | |
78 | Some ICE equipment understands an addition to the standard | |
79 | S-Record format; symbols and their addresses can be sent | |
80 | before the data. | |
81 | ||
82 | The format of this is: | |
83 | ($$ <modulename> | |
84 | (<space> <symbol> <address>)*) | |
85 | $$ | |
86 | ||
87 | so a short symbol table could look like: | |
88 | ||
89 | EXAMPLE | |
90 | $$ flash.x | |
91 | $$ flash.c | |
92 | _port6 $0 | |
93 | _delay $4 | |
94 | _start $14 | |
95 | _etext $8036 | |
96 | _edata $8036 | |
97 | _end $8036 | |
98 | $$ | |
99 | ||
100 | DESCRIPTION | |
101 | We allow symbols to be anywhere in the data stream - the module names | |
102 | are always ignored. | |
103 | ||
9898b929 | 104 | */ |
7ed4093a | 105 | |
36773af5 | 106 | #include "bfd.h" |
9898b929 | 107 | #include "sysdep.h" |
4a81b561 DHW |
108 | #include "libbfd.h" |
109 | ||
5f9ca960 | 110 | /* Macros for converting between hex and binary */ |
4a81b561 | 111 | |
3039e8ee | 112 | static CONST char digs[] = "0123456789ABCDEF"; |
5f9ca960 JG |
113 | |
114 | static char hex_value[1 + (unsigned char)~0]; | |
9898b929 | 115 | |
9898b929 | 116 | #define NOT_HEX 20 |
5f9ca960 | 117 | #define NIBBLE(x) hex_value[(unsigned char)(x)] |
9898b929 | 118 | #define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1])) |
5f9ca960 JG |
119 | #define TOHEX(d, x, ch) \ |
120 | d[1] = digs[(x) & 0xf]; \ | |
121 | d[0] = digs[((x)>>4)&0xf]; \ | |
122 | ch += ((x) & 0xff); | |
123 | #define ISHEX(x) (hex_value[(unsigned char)(x)] != NOT_HEX) | |
4a81b561 | 124 | |
4a81b561 | 125 | |
9898b929 | 126 | |
e98e6ec1 | 127 | static void |
9898b929 JG |
128 | DEFUN_VOID(srec_init) |
129 | { | |
130 | unsigned int i; | |
131 | static boolean inited = false; | |
132 | ||
133 | if (inited == false) | |
134 | { | |
135 | ||
136 | inited = true; | |
137 | ||
5f9ca960 | 138 | for (i = 0; i < sizeof (hex_value); i++) |
9898b929 JG |
139 | { |
140 | hex_value[i] = NOT_HEX; | |
141 | } | |
142 | ||
143 | for (i = 0; i < 10; i++) | |
144 | { | |
145 | hex_value[i + '0'] = i; | |
146 | ||
147 | } | |
148 | for (i = 0; i < 6; i++) | |
149 | { | |
150 | hex_value[i + 'a'] = i+10; | |
151 | hex_value[i + 'A'] = i+10; | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
4a81b561 DHW |
156 | |
157 | /* The maximum number of bytes on a line is FF */ | |
158 | #define MAXCHUNK 0xff | |
159 | /* The number of bytes we fit onto a line on output */ | |
9898b929 JG |
160 | #define CHUNK 21 |
161 | ||
162 | /* We cannot output our srecords as we see them, we have to glue them | |
163 | together, this is done in this structure : */ | |
164 | ||
165 | struct srec_data_list_struct | |
166 | { | |
167 | unsigned char *data; | |
168 | bfd_vma where; | |
169 | bfd_size_type size; | |
170 | struct srec_data_list_struct *next; | |
8f8fefcc | 171 | |
9898b929 JG |
172 | |
173 | } ; | |
174 | typedef struct srec_data_list_struct srec_data_list_type; | |
175 | ||
4a81b561 | 176 | |
e98e6ec1 | 177 | typedef struct srec_data_struct |
4a81b561 | 178 | { |
9898b929 JG |
179 | srec_data_list_type *head; |
180 | unsigned int type; | |
8f8fefcc JG |
181 | |
182 | int done_symbol_read; | |
183 | int count; | |
184 | asymbol *symbols; | |
185 | char *strings; | |
186 | int symbol_idx; | |
187 | int string_size; | |
188 | int string_idx; | |
9898b929 JG |
189 | } tdata_type; |
190 | ||
191 | ||
4a81b561 | 192 | /* |
8f8fefcc | 193 | called once per input S-Record, used to work out vma and size of data. |
4a81b561 DHW |
194 | */ |
195 | ||
357a1f38 | 196 | static bfd_vma low,high; |
9898b929 | 197 | |
8f8fefcc JG |
198 | static void |
199 | size_symbols(abfd, buf, len, val) | |
200 | bfd *abfd; | |
201 | char *buf; | |
202 | int len; | |
203 | int val; | |
204 | { | |
205 | abfd->symcount ++; | |
206 | abfd->tdata.srec_data->string_size += len + 1; | |
207 | } | |
208 | ||
209 | static void | |
210 | fillup_symbols(abfd, buf, len, val) | |
211 | bfd *abfd; | |
212 | char *buf; | |
213 | int len; | |
214 | int val; | |
215 | { | |
216 | if (!abfd->tdata.srec_data->done_symbol_read) | |
217 | { | |
218 | asymbol *p; | |
219 | if (abfd->tdata.srec_data->symbols == 0) | |
220 | { | |
221 | abfd->tdata.srec_data->symbols = (asymbol *)bfd_alloc(abfd, abfd->symcount * sizeof(asymbol)); | |
222 | abfd->tdata.srec_data->strings = (char*)bfd_alloc(abfd, abfd->tdata.srec_data->string_size); | |
223 | abfd->tdata.srec_data->symbol_idx = 0; | |
224 | abfd->tdata.srec_data->string_idx = 0; | |
225 | } | |
226 | ||
227 | p = abfd->tdata.srec_data->symbols + abfd->tdata.srec_data->symbol_idx++; | |
228 | p->the_bfd = abfd; | |
229 | p->name = abfd->tdata.srec_data->strings + abfd->tdata.srec_data->string_idx; | |
230 | memcpy((char *)(p->name), buf, len+1); | |
231 | abfd->tdata.srec_data->string_idx += len + 1; | |
232 | p->value = val; | |
233 | p->flags = BSF_EXPORT | BSF_GLOBAL; | |
234 | p->section = &bfd_abs_section; | |
235 | p->udata = 0; | |
236 | } | |
237 | } | |
4a81b561 | 238 | static void |
9898b929 JG |
239 | DEFUN(size_srec,(abfd, section, address, raw, length), |
240 | bfd *abfd AND | |
241 | asection *section AND | |
242 | bfd_vma address AND | |
243 | bfd_byte *raw AND | |
244 | unsigned int length) | |
4a81b561 | 245 | { |
357a1f38 SC |
246 | if (address < low) |
247 | low = address; | |
248 | if (address + length > high) | |
9898b929 | 249 | high = address + length -1; |
4a81b561 DHW |
250 | } |
251 | ||
357a1f38 | 252 | |
4a81b561 | 253 | /* |
8f8fefcc | 254 | called once per input S-Record, copies data from input into bfd_alloc'd area |
4a81b561 DHW |
255 | */ |
256 | ||
257 | static void | |
9898b929 JG |
258 | DEFUN(fillup,(abfd, section, address, raw, length), |
259 | bfd *abfd AND | |
260 | asection *section AND | |
261 | bfd_vma address AND | |
262 | bfd_byte *raw AND | |
263 | unsigned int length) | |
4a81b561 | 264 | { |
9898b929 JG |
265 | unsigned int i; |
266 | bfd_byte *dst = | |
8f8fefcc | 267 | (bfd_byte *)(section->used_by_bfd) + address - section->vma; |
9898b929 JG |
268 | /* length -1 because we don't read in the checksum */ |
269 | for (i = 0; i < length -1 ; i++) { | |
270 | *dst = HEX(raw); | |
271 | dst++; | |
272 | raw+=2; | |
273 | } | |
4a81b561 DHW |
274 | } |
275 | ||
8f8fefcc | 276 | /* Pass over an S-Record file, calling one of the above functions on each |
387cbb2b JG |
277 | record. */ |
278 | ||
8f8fefcc JG |
279 | static int white(x) |
280 | char x; | |
281 | { | |
282 | return (x== ' ' || x == '\t' || x == '\n' || x == '\r'); | |
283 | } | |
284 | static int | |
285 | skipwhite(src,abfd) | |
286 | char *src; | |
287 | bfd *abfd; | |
288 | { | |
289 | int eof = 0; | |
290 | while (white(*src) && !eof) | |
291 | { | |
292 | eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1); | |
293 | } | |
294 | return eof; | |
295 | } | |
296 | ||
297 | static boolean | |
298 | DEFUN(srec_mkobject, (abfd), | |
299 | bfd *abfd) | |
300 | { | |
301 | if (abfd->tdata.srec_data == 0) | |
302 | { | |
303 | tdata_type *tdata = (tdata_type *)bfd_alloc(abfd, sizeof(tdata_type)); | |
304 | abfd->tdata.srec_data = tdata; | |
305 | tdata->type = 1; | |
306 | tdata->head = (srec_data_list_type *)NULL; | |
307 | } | |
308 | return true; | |
309 | ||
310 | } | |
311 | ||
4a81b561 | 312 | static void |
8f8fefcc | 313 | DEFUN(pass_over,(abfd, func, symbolfunc, section), |
9898b929 JG |
314 | bfd *abfd AND |
315 | void (*func)() AND | |
8f8fefcc | 316 | void (*symbolfunc)() AND |
9898b929 | 317 | asection *section) |
4a81b561 | 318 | { |
8f8fefcc JG |
319 | unsigned int bytes_on_line; |
320 | boolean eof = false; | |
e98e6ec1 | 321 | |
8f8fefcc JG |
322 | srec_mkobject(abfd); |
323 | /* To the front of the file */ | |
324 | bfd_seek(abfd, (file_ptr)0, SEEK_SET); | |
325 | while (eof == false) | |
326 | { | |
327 | char buffer[MAXCHUNK]; | |
328 | char *src = buffer; | |
329 | char type; | |
330 | bfd_vma address = 0; | |
331 | ||
332 | /* Find first 'S' or $ */ | |
333 | eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1); | |
334 | switch (*src) | |
4a81b561 | 335 | { |
8f8fefcc JG |
336 | default: |
337 | eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1); | |
338 | if (eof) return; | |
339 | break; | |
340 | ||
341 | case '$': | |
342 | /* Inside a symbol definition - just ignore the module name */ | |
343 | while (*src != '\n' && !eof) | |
344 | { | |
345 | eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1); | |
346 | } | |
347 | break; | |
348 | ||
349 | case ' ': | |
350 | /* spaces - maybe just before a symbol */ | |
351 | while (*src != '\n' && white(*src)) { | |
352 | eof = skipwhite(src, abfd); | |
353 | ||
354 | { | |
355 | int val = 0; | |
356 | int slen = 0; | |
357 | char symbol[MAXCHUNK]; | |
358 | ||
359 | /* get the symbol part */ | |
360 | while (!eof && !white(*src) && slen < MAXCHUNK) | |
361 | { | |
362 | symbol[slen++] = *src; | |
363 | eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1); | |
364 | } | |
365 | symbol[slen] = 0; | |
366 | eof = skipwhite(src, abfd); | |
367 | /* skip the $ for the hex value */ | |
368 | if (*src == '$') | |
369 | { | |
370 | eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1); | |
371 | } | |
372 | ||
373 | /* Scan off the hex number */ | |
374 | while (isxdigit(*src )) | |
375 | { | |
376 | val *= 16; | |
377 | if (isdigit(*src)) | |
378 | val += *src - '0'; | |
379 | else if (isupper(*src)) { | |
380 | val += *src - 'A' + 10; | |
381 | } | |
382 | else { | |
383 | val += *src - 'a' + 10; | |
384 | } | |
385 | eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1); | |
386 | } | |
387 | symbolfunc(abfd, symbol, slen, val); | |
388 | } | |
389 | } | |
390 | break; | |
391 | case 'S': | |
392 | src++; | |
393 | ||
394 | /* Fetch the type and the length */ | |
395 | bfd_read(src, 1, 3, abfd); | |
396 | ||
397 | type = *src++; | |
398 | ||
399 | if (!ISHEX (src[0]) || !ISHEX (src[1])) | |
400 | break; | |
401 | ||
402 | bytes_on_line = HEX(src); | |
403 | ||
404 | if (bytes_on_line > MAXCHUNK/2) | |
405 | break; | |
406 | src+=2 ; | |
407 | ||
408 | bfd_read(src, 1 , bytes_on_line * 2, abfd); | |
409 | ||
410 | switch (type) { | |
411 | case '0': | |
412 | case '5': | |
413 | /* Prologue - ignore */ | |
414 | break; | |
415 | case '3': | |
416 | address = HEX(src); | |
417 | src+=2; | |
418 | bytes_on_line--; | |
9898b929 | 419 | |
8f8fefcc JG |
420 | case '2': |
421 | address = HEX(src) | (address<<8) ; | |
422 | src+=2; | |
423 | bytes_on_line--; | |
424 | case '1': | |
425 | address = HEX(src) | (address<<8) ; | |
426 | src+=2; | |
427 | address = HEX(src) | (address<<8) ; | |
428 | src+=2; | |
429 | bytes_on_line-=2; | |
430 | func(abfd,section, address, src, bytes_on_line); | |
431 | break; | |
432 | default: | |
433 | return; | |
434 | } | |
4a81b561 | 435 | } |
8f8fefcc | 436 | } |
9898b929 | 437 | |
4a81b561 DHW |
438 | } |
439 | ||
8f8fefcc JG |
440 | static bfd_target * |
441 | object_p(abfd) | |
442 | bfd *abfd; | |
443 | { | |
444 | asection *section; | |
445 | /* We create one section called .text for all the contents, | |
446 | and allocate enough room for the entire file. */ | |
447 | ||
448 | section = bfd_make_section(abfd, ".text"); | |
449 | section->_raw_size = 0; | |
450 | section->vma = 0xffffffff; | |
451 | low = 0xffffffff; | |
452 | high = 0; | |
453 | pass_over(abfd, size_srec, size_symbols, section); | |
454 | section->_raw_size = high - low; | |
455 | section->vma = low; | |
456 | section->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC; | |
4a81b561 | 457 | |
8f8fefcc JG |
458 | if (abfd->symcount) |
459 | abfd->flags |= HAS_SYMS; | |
460 | return abfd->xvec; | |
461 | } | |
9898b929 JG |
462 | |
463 | static bfd_target * | |
464 | DEFUN(srec_object_p, (abfd), | |
465 | bfd *abfd) | |
4a81b561 | 466 | { |
387cbb2b | 467 | char b[4]; |
8f8fefcc | 468 | |
9898b929 JG |
469 | srec_init(); |
470 | ||
4a81b561 | 471 | bfd_seek(abfd, (file_ptr)0, SEEK_SET); |
387cbb2b | 472 | bfd_read(b, 1, 4, abfd); |
9898b929 | 473 | |
387cbb2b | 474 | if (b[0] != 'S' || !ISHEX(b[1]) || !ISHEX(b[2]) || !ISHEX(b[3])) |
8f8fefcc | 475 | return (bfd_target*) NULL; |
387cbb2b JG |
476 | |
477 | /* We create one section called .text for all the contents, | |
478 | and allocate enough room for the entire file. */ | |
4a81b561 | 479 | |
8f8fefcc JG |
480 | return object_p(abfd); |
481 | } | |
482 | ||
483 | ||
484 | static bfd_target * | |
485 | DEFUN(symbolsrec_object_p, (abfd), | |
486 | bfd *abfd) | |
487 | { | |
488 | char b[4]; | |
489 | ||
490 | srec_init(); | |
9898b929 | 491 | |
8f8fefcc JG |
492 | bfd_seek(abfd, (file_ptr)0, SEEK_SET); |
493 | bfd_read(b, 1, 4, abfd); | |
494 | ||
495 | if (b[0] != '$' || b[1] != '$') | |
496 | return (bfd_target*) NULL; | |
497 | ||
498 | return object_p(abfd); | |
4a81b561 DHW |
499 | } |
500 | ||
501 | ||
4a81b561 | 502 | static boolean |
9898b929 JG |
503 | DEFUN(srec_get_section_contents,(abfd, section, location, offset, count), |
504 | bfd *abfd AND | |
505 | asection *section AND | |
506 | PTR location AND | |
507 | file_ptr offset AND | |
508 | bfd_size_type count) | |
4a81b561 | 509 | { |
9898b929 JG |
510 | if (section->used_by_bfd == (PTR)NULL) |
511 | { | |
e98e6ec1 | 512 | section->used_by_bfd = (PTR)bfd_alloc (abfd, section->_raw_size); |
8f8fefcc JG |
513 | |
514 | pass_over(abfd, fillup, fillup_symbols, section); | |
9898b929 JG |
515 | } |
516 | (void) memcpy((PTR)location, | |
517 | (PTR)((char *)(section->used_by_bfd) + offset), | |
518 | count); | |
519 | return true; | |
4a81b561 DHW |
520 | } |
521 | ||
522 | ||
523 | ||
524 | boolean | |
9898b929 JG |
525 | DEFUN(srec_set_arch_mach,(abfd, arch, machine), |
526 | bfd *abfd AND | |
527 | enum bfd_architecture arch AND | |
528 | unsigned long machine) | |
4a81b561 | 529 | { |
9898b929 | 530 | return bfd_default_set_arch_mach(abfd, arch, machine); |
4a81b561 DHW |
531 | } |
532 | ||
533 | ||
9898b929 JG |
534 | /* we have to save up all the Srecords for a splurge before output, |
535 | also remember */ | |
4a81b561 | 536 | |
9898b929 JG |
537 | static boolean |
538 | DEFUN(srec_set_section_contents,(abfd, section, location, offset, bytes_to_do), | |
539 | bfd *abfd AND | |
540 | sec_ptr section AND | |
541 | PTR location AND | |
542 | file_ptr offset AND | |
543 | bfd_size_type bytes_to_do) | |
4a81b561 | 544 | { |
294eaca4 SC |
545 | tdata_type *tdata = abfd->tdata.srec_data; |
546 | srec_data_list_type *entry = (srec_data_list_type *) | |
8f8fefcc JG |
547 | bfd_alloc(abfd, sizeof(srec_data_list_type)); |
548 | ||
294eaca4 SC |
549 | if ((section->flags & SEC_ALLOC) |
550 | && (section->flags & SEC_LOAD)) | |
9898b929 | 551 | { |
8f8fefcc JG |
552 | unsigned char *data = (unsigned char *) bfd_alloc(abfd, bytes_to_do); |
553 | memcpy(data, location, bytes_to_do); | |
9898b929 | 554 | |
8f8fefcc JG |
555 | if ((section->lma + offset + bytes_to_do) <= 0xffff) |
556 | { | |
9898b929 | 557 | |
8f8fefcc JG |
558 | } |
559 | else if ((section->lma + offset + bytes_to_do) <= 0xffffff | |
560 | && tdata->type < 2) | |
561 | { | |
562 | tdata->type = 2; | |
563 | } | |
564 | else | |
565 | { | |
566 | tdata->type = 3; | |
567 | } | |
568 | ||
569 | entry->data = data; | |
570 | entry->where = section->lma + offset; | |
571 | entry->size = bytes_to_do; | |
572 | entry->next = tdata->head; | |
573 | tdata->head = entry; | |
574 | } | |
294eaca4 | 575 | return true; |
9898b929 JG |
576 | } |
577 | ||
578 | /* Write a record of type, of the supplied number of bytes. The | |
579 | supplied bytes and length don't have a checksum. That's worked out | |
580 | here | |
581 | */ | |
582 | static | |
583 | void DEFUN(srec_write_record,(abfd, type, address, data, end), | |
584 | bfd *abfd AND | |
585 | char type AND | |
586 | bfd_vma address AND | |
587 | CONST unsigned char *data AND | |
588 | CONST unsigned char *end) | |
589 | ||
590 | { | |
591 | char buffer[MAXCHUNK]; | |
592 | ||
593 | unsigned int check_sum = 0; | |
594 | unsigned CONST char *src = data; | |
595 | char *dst =buffer; | |
596 | char *length; | |
597 | ||
598 | ||
599 | *dst++ = 'S'; | |
600 | *dst++ = '0' + type; | |
601 | ||
602 | length = dst; | |
603 | dst+=2; /* leave room for dst*/ | |
604 | ||
605 | switch (type) | |
606 | { | |
607 | case 3: | |
608 | case 7: | |
609 | TOHEX(dst, (address >> 24), check_sum); | |
610 | dst+=2; | |
611 | case 8: | |
612 | case 2: | |
613 | TOHEX(dst, (address >> 16), check_sum); | |
614 | dst+=2; | |
615 | case 9: | |
616 | case 1: | |
617 | case 0: | |
618 | TOHEX(dst, (address >> 8), check_sum); | |
619 | dst+=2; | |
620 | TOHEX(dst, (address), check_sum); | |
621 | dst+=2; | |
622 | break; | |
4a81b561 | 623 | |
4a81b561 | 624 | } |
9898b929 JG |
625 | for (src = data; src < end; src++) |
626 | { | |
627 | TOHEX(dst, *src, check_sum); | |
628 | dst+=2; | |
4a81b561 DHW |
629 | } |
630 | ||
9898b929 JG |
631 | /* Fill in the length */ |
632 | TOHEX(length, (dst - length)/2, check_sum); | |
633 | check_sum &= 0xff; | |
634 | check_sum = 255 - check_sum; | |
635 | TOHEX(dst, check_sum, check_sum); | |
636 | dst+=2; | |
637 | ||
3039e8ee | 638 | *dst ++ = '\r'; |
9898b929 JG |
639 | *dst ++ = '\n'; |
640 | bfd_write((PTR)buffer, 1, dst - buffer , abfd); | |
641 | } | |
4a81b561 | 642 | |
4a81b561 | 643 | |
4a81b561 | 644 | |
9898b929 JG |
645 | static void |
646 | DEFUN(srec_write_header,(abfd), | |
647 | bfd *abfd) | |
648 | { | |
649 | unsigned char buffer[MAXCHUNK]; | |
650 | unsigned char *dst = buffer; | |
651 | unsigned int i; | |
4a81b561 | 652 | |
9898b929 JG |
653 | /* I'll put an arbitary 40 char limit on header size */ |
654 | for (i = 0; i < 40 && abfd->filename[i]; i++) | |
655 | { | |
656 | *dst++ = abfd->filename[i]; | |
657 | } | |
658 | srec_write_record(abfd,0, 0, buffer, dst); | |
4a81b561 DHW |
659 | } |
660 | ||
9898b929 JG |
661 | static void |
662 | DEFUN(srec_write_section,(abfd, tdata, list), | |
663 | bfd *abfd AND | |
664 | tdata_type *tdata AND | |
665 | srec_data_list_type *list) | |
666 | { | |
667 | unsigned int bytes_written = 0; | |
668 | unsigned char *location = list->data; | |
669 | ||
670 | while (bytes_written < list->size) | |
671 | { | |
9898b929 | 672 | bfd_vma address; |
9898b929 JG |
673 | |
674 | unsigned int bytes_this_chunk = list->size - bytes_written; | |
675 | ||
676 | if (bytes_this_chunk > CHUNK) | |
677 | { | |
678 | bytes_this_chunk = CHUNK; | |
679 | } | |
680 | ||
681 | address = list->where + bytes_written; | |
682 | ||
683 | srec_write_record(abfd, | |
684 | tdata->type, | |
685 | address, | |
686 | location, | |
687 | location + bytes_this_chunk); | |
688 | ||
689 | bytes_written += bytes_this_chunk; | |
690 | location += bytes_this_chunk; | |
691 | } | |
692 | ||
693 | } | |
694 | ||
695 | static void | |
696 | DEFUN(srec_write_terminator,(abfd, tdata), | |
697 | bfd *abfd AND | |
698 | tdata_type *tdata) | |
699 | { | |
700 | unsigned char buffer[2]; | |
701 | ||
702 | srec_write_record(abfd, 10 - tdata->type, | |
703 | abfd->start_address, buffer, buffer); | |
9898b929 | 704 | } |
3039e8ee | 705 | |
8f8fefcc JG |
706 | |
707 | ||
708 | static void | |
709 | srec_write_symbols(abfd) | |
2c3b9e47 | 710 | bfd *abfd; |
9898b929 | 711 | { |
8f8fefcc JG |
712 | char buffer[MAXCHUNK]; |
713 | /* Dump out the symbols of a bfd */ | |
714 | int i; | |
715 | int len = bfd_get_symcount(abfd); | |
716 | ||
717 | if (len) | |
718 | { | |
719 | asymbol **table = bfd_get_outsymbols(abfd); | |
720 | sprintf(buffer, "$$ %s\r\n", abfd->filename); | |
721 | ||
722 | bfd_write(buffer, strlen(buffer), 1, abfd); | |
723 | ||
724 | for (i = 0; i < len; i++) | |
725 | { | |
726 | asymbol *s = table[i]; | |
727 | #if 0 | |
728 | int len = strlen(s->name); | |
729 | ||
730 | /* If this symbol has a .[ocs] in it, it's probably a file name | |
731 | and we'll output that as the module name */ | |
732 | ||
733 | if (len > 3 && s->name[len-2] == '.') | |
734 | { | |
735 | int l; | |
736 | sprintf(buffer, "$$ %s\n\r", s->name); | |
737 | l = strlen(buffer); | |
738 | bfd_write(buffer, l, 1, abfd); | |
739 | } | |
740 | else | |
741 | #endif | |
742 | if (s->flags & (BSF_GLOBAL | BSF_LOCAL) | |
743 | && (s->flags & BSF_DEBUGGING) == 0 | |
744 | && s->name[0] != '.' | |
745 | && s->name[0] != 't') | |
746 | { | |
747 | /* Just dump out non debug symbols */ | |
748 | ||
749 | int l; | |
2c3b9e47 KR |
750 | char buf2[40], *p; |
751 | ||
752 | sprintf (buffer," %s $", s->name); | |
753 | sprintf_vma (buf2, s->value + s->section->lma); | |
754 | p = buf2; | |
755 | while (p[0] == '0' && p[1] != 0) | |
756 | p++; | |
757 | strcat (buffer, p); | |
758 | strcat (buffer, "\n\r"); | |
8f8fefcc JG |
759 | l = strlen(buffer); |
760 | bfd_write(buffer, l, 1,abfd); | |
761 | } | |
762 | } | |
763 | sprintf(buffer, "$$ \r\n"); | |
764 | bfd_write(buffer, strlen(buffer), 1, abfd); | |
765 | } | |
9898b929 JG |
766 | } |
767 | ||
9898b929 | 768 | static boolean |
8f8fefcc JG |
769 | internal_srec_write_object_contents(abfd, symbols) |
770 | bfd *abfd; | |
771 | int symbols; | |
4a81b561 | 772 | { |
9898b929 | 773 | int bytes_written; |
e98e6ec1 | 774 | tdata_type *tdata = abfd->tdata.srec_data; |
9898b929 JG |
775 | srec_data_list_type *list; |
776 | ||
777 | bytes_written = 0; | |
9898b929 | 778 | |
9898b929 | 779 | |
8f8fefcc JG |
780 | if (symbols) |
781 | srec_write_symbols(abfd); | |
782 | ||
783 | srec_write_header(abfd); | |
784 | ||
9898b929 JG |
785 | /* Now wander though all the sections provided and output them */ |
786 | list = tdata->head; | |
787 | ||
788 | while (list != (srec_data_list_type*)NULL) | |
789 | { | |
790 | srec_write_section(abfd, tdata, list); | |
791 | list = list->next; | |
792 | } | |
793 | srec_write_terminator(abfd, tdata); | |
794 | return true; | |
4a81b561 DHW |
795 | } |
796 | ||
8f8fefcc JG |
797 | static boolean |
798 | srec_write_object_contents(abfd) | |
799 | bfd *abfd; | |
800 | { | |
801 | return internal_srec_write_object_contents(abfd, 0); | |
802 | } | |
803 | ||
804 | static boolean | |
805 | symbolsrec_write_object_contents(abfd) | |
806 | bfd *abfd; | |
807 | { | |
808 | return internal_srec_write_object_contents(abfd, 1); | |
809 | } | |
810 | ||
39a2ce33 | 811 | static int |
7ed4093a SC |
812 | DEFUN(srec_sizeof_headers,(abfd, exec), |
813 | bfd *abfd AND | |
814 | boolean exec) | |
39a2ce33 SC |
815 | { |
816 | return 0; | |
817 | } | |
818 | ||
357a1f38 SC |
819 | static asymbol * |
820 | DEFUN(srec_make_empty_symbol, (abfd), | |
821 | bfd*abfd) | |
822 | { | |
823 | asymbol *new= (asymbol *)bfd_zalloc (abfd, sizeof (asymbol)); | |
824 | new->the_bfd = abfd; | |
825 | return new; | |
826 | } | |
8f8fefcc JG |
827 | |
828 | static unsigned int | |
829 | srec_get_symtab_upper_bound(abfd) | |
830 | bfd *abfd; | |
831 | { | |
832 | /* Read in all the info */ | |
833 | srec_get_section_contents(abfd,abfd->sections,0,0,0); | |
834 | return (bfd_get_symcount(abfd) + 1) * (sizeof(asymbol *)); | |
835 | } | |
836 | ||
837 | static unsigned int | |
838 | DEFUN(srec_get_symtab, (abfd, alocation), | |
839 | bfd *abfd AND | |
840 | asymbol **alocation) | |
841 | { | |
842 | int lim = abfd->symcount; | |
843 | int i; | |
844 | for (i = 0; i < lim; i++) { | |
845 | alocation[i] = abfd->tdata.srec_data->symbols + i; | |
846 | } | |
847 | alocation[i] = 0; | |
848 | return lim; | |
849 | } | |
850 | ||
2c3b9e47 KR |
851 | void |
852 | DEFUN(srec_get_symbol_info,(ignore_abfd, symbol, ret), | |
853 | bfd *ignore_abfd AND | |
854 | asymbol *symbol AND | |
855 | symbol_info *ret) | |
856 | { | |
857 | bfd_symbol_info (symbol, ret); | |
858 | } | |
859 | ||
8f8fefcc JG |
860 | void |
861 | DEFUN(srec_print_symbol,(ignore_abfd, afile, symbol, how), | |
862 | bfd *ignore_abfd AND | |
863 | PTR afile AND | |
864 | asymbol *symbol AND | |
865 | bfd_print_symbol_type how) | |
866 | { | |
867 | FILE *file = (FILE *)afile; | |
868 | switch (how) | |
869 | { | |
870 | case bfd_print_symbol_name: | |
871 | fprintf (file, "%s", symbol->name); | |
872 | break; | |
873 | default: | |
8f8fefcc JG |
874 | bfd_print_symbol_vandf ((PTR) file, symbol); |
875 | fprintf (file, " %-5s %s", | |
876 | symbol->section->name, | |
877 | symbol->name); | |
878 | ||
879 | } | |
880 | } | |
881 | ||
6f715d66 SC |
882 | #define FOO PROTO |
883 | #define srec_new_section_hook (FOO(boolean, (*), (bfd *, asection *)))bfd_true | |
8f8fefcc | 884 | |
6f715d66 SC |
885 | #define srec_get_reloc_upper_bound (FOO(unsigned int, (*),(bfd*, asection *)))bfd_false |
886 | #define srec_canonicalize_reloc (FOO(unsigned int, (*),(bfd*,asection *, arelent **, asymbol **))) bfd_0 | |
357a1f38 | 887 | |
8f8fefcc | 888 | |
d0ec7a8e | 889 | |
6f715d66 SC |
890 | #define srec_openr_next_archived_file (FOO(bfd *, (*), (bfd*,bfd*))) bfd_nullvoidptr |
891 | #define srec_find_nearest_line (FOO(boolean, (*),(bfd*,asection*,asymbol**,bfd_vma, CONST char**, CONST char**, unsigned int *))) bfd_false | |
892 | #define srec_generic_stat_arch_elt (FOO(int, (*), (bfd *,struct stat *))) bfd_0 | |
9872a49c | 893 | |
d0ec7a8e SC |
894 | |
895 | #define srec_core_file_failing_command (char *(*)())(bfd_nullvoidptr) | |
896 | #define srec_core_file_failing_signal (int (*)())bfd_0 | |
6f715d66 | 897 | #define srec_core_file_matches_executable_p (FOO(boolean, (*),(bfd*, bfd*)))bfd_false |
d0ec7a8e SC |
898 | #define srec_slurp_armap bfd_true |
899 | #define srec_slurp_extended_name_table bfd_true | |
900 | #define srec_truncate_arname (void (*)())bfd_nullvoidptr | |
9898b929 | 901 | #define srec_write_armap (FOO( boolean, (*),(bfd *, unsigned int, struct orl *, unsigned int, int))) bfd_nullvoidptr |
d0ec7a8e | 902 | #define srec_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr |
2b1d8a50 | 903 | #define srec_close_and_cleanup bfd_generic_close_and_cleanup |
6f715d66 SC |
904 | #define srec_bfd_debug_info_start bfd_void |
905 | #define srec_bfd_debug_info_end bfd_void | |
906 | #define srec_bfd_debug_info_accumulate (FOO(void, (*), (bfd *, asection *))) bfd_void | |
e98e6ec1 | 907 | #define srec_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents |
294eaca4 | 908 | #define srec_bfd_relax_section bfd_generic_relax_section |
3039e8ee | 909 | #define srec_bfd_seclet_link bfd_generic_seclet_link |
8f8fefcc JG |
910 | #define srec_bfd_reloc_type_lookup \ |
911 | ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr) | |
912 | #define srec_bfd_make_debug_symbol \ | |
913 | ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr) | |
3039e8ee | 914 | |
4a81b561 DHW |
915 | bfd_target srec_vec = |
916 | { | |
9898b929 JG |
917 | "srec", /* name */ |
918 | bfd_target_srec_flavour, | |
919 | true, /* target byte order */ | |
920 | true, /* target headers byte order */ | |
921 | (HAS_RELOC | EXEC_P | /* object flags */ | |
922 | HAS_LINENO | HAS_DEBUG | | |
923 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED), | |
924 | (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS | |
925 | |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
294eaca4 | 926 | 0, /* leading underscore */ |
9898b929 JG |
927 | ' ', /* ar_pad_char */ |
928 | 16, /* ar_max_namelen */ | |
6f715d66 | 929 | 1, /* minimum alignment */ |
2c3b9e47 KR |
930 | _do_getb64, _do_getb_signed_64, _do_putb64, |
931 | _do_getb32, _do_getb_signed_32, _do_putb32, | |
932 | _do_getb16, _do_getb_signed_16, _do_putb16, /* data */ | |
933 | _do_getb64, _do_getb_signed_64, _do_putb64, | |
934 | _do_getb32, _do_getb_signed_32, _do_putb32, | |
935 | _do_getb16, _do_getb_signed_16, _do_putb16, /* hdrs */ | |
9898b929 JG |
936 | |
937 | { | |
938 | _bfd_dummy_target, | |
939 | srec_object_p, /* bfd_check_format */ | |
940 | (struct bfd_target *(*)()) bfd_nullvoidptr, | |
941 | (struct bfd_target *(*)()) bfd_nullvoidptr, | |
942 | }, | |
943 | { | |
7ed4093a | 944 | bfd_false, |
9898b929 | 945 | srec_mkobject, |
7ed4093a SC |
946 | _bfd_generic_mkarchive, |
947 | bfd_false, | |
9898b929 JG |
948 | }, |
949 | { /* bfd_write_contents */ | |
7ed4093a SC |
950 | bfd_false, |
951 | srec_write_object_contents, | |
952 | _bfd_write_archive_contents, | |
953 | bfd_false, | |
9898b929 JG |
954 | }, |
955 | JUMP_TABLE(srec) | |
956 | }; | |
957 | ||
8f8fefcc JG |
958 | |
959 | ||
960 | bfd_target symbolsrec_vec = | |
961 | { | |
962 | "symbolsrec", /* name */ | |
963 | bfd_target_srec_flavour, | |
964 | true, /* target byte order */ | |
965 | true, /* target headers byte order */ | |
966 | (HAS_RELOC | EXEC_P | /* object flags */ | |
967 | HAS_LINENO | HAS_DEBUG | | |
968 | HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED), | |
969 | (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS | |
970 | |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
971 | 0, /* leading underscore */ | |
972 | ' ', /* ar_pad_char */ | |
973 | 16, /* ar_max_namelen */ | |
974 | 1, /* minimum alignment */ | |
2c3b9e47 KR |
975 | _do_getb64, _do_getb_signed_64, _do_putb64, |
976 | _do_getb32, _do_getb_signed_32, _do_putb32, | |
977 | _do_getb16, _do_getb_signed_16, _do_putb16, /* data */ | |
978 | _do_getb64, _do_getb_signed_64, _do_putb64, | |
979 | _do_getb32, _do_getb_signed_32, _do_putb32, | |
980 | _do_getb16, _do_getb_signed_16, _do_putb16, /* hdrs */ | |
8f8fefcc JG |
981 | |
982 | { | |
983 | _bfd_dummy_target, | |
984 | symbolsrec_object_p, /* bfd_check_format */ | |
985 | (struct bfd_target *(*)()) bfd_nullvoidptr, | |
986 | (struct bfd_target *(*)()) bfd_nullvoidptr, | |
987 | }, | |
988 | { | |
989 | bfd_false, | |
990 | srec_mkobject, | |
991 | _bfd_generic_mkarchive, | |
992 | bfd_false, | |
993 | }, | |
994 | { /* bfd_write_contents */ | |
995 | bfd_false, | |
996 | symbolsrec_write_object_contents, | |
997 | _bfd_write_archive_contents, | |
998 | bfd_false, | |
999 | }, | |
1000 | JUMP_TABLE(srec), | |
1001 | (PTR) 0 | |
1002 | }; | |
1003 |