Commit | Line | Data |
---|---|---|
d3e667e8 | 1 | /* BFD backend for Extended Tektronix Hex Format objects. |
dfc1c006 | 2 | Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc. |
d3e667e8 JG |
3 | |
4 | Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>. | |
5 | ||
6 | This file is part of BFD, the Binary File Descriptor library. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | /* | |
23 | SUBSECTION | |
24 | Tektronix Hex Format handling | |
25 | ||
26 | DESCRIPTION | |
27 | ||
28 | Tek Hex records can hold symbols and data, but not | |
29 | relocations. Their main application is communication with | |
30 | devices like PROM programmers and ICE equipment. | |
31 | ||
32 | It seems that the sections are descibed as being really big, | |
33 | the example I have says that the text section is 0..ffffffff. | |
34 | BFD would barf with this, many apps would try to alloc 4GB to | |
9783e04a | 35 | read in the file. |
d3e667e8 JG |
36 | |
37 | Tex Hex may contain many sections, but the data which comes in | |
38 | has no tag saying which section it belongs to, so we create | |
39 | one section for each block of data, called "blknnnn" which we | |
40 | stick all the data into. | |
41 | ||
42 | TekHex may come out of order and there is no header, so an | |
43 | initial scan is required to discover the minimum and maximum | |
44 | addresses used to create the vma and size of the sections we | |
9783e04a | 45 | create. |
d3e667e8 JG |
46 | We read in the data into pages of CHUNK_MASK+1 size and read |
47 | them out from that whenever we need to. | |
48 | ||
49 | Any number of sections may be created for output, we save them | |
50 | up and output them when it's time to close the bfd. | |
51 | ||
52 | ||
53 | A TekHex record looks like: | |
54 | EXAMPLE | |
55 | %<block length><type><checksum><stuff><cr> | |
56 | ||
57 | DESCRIPTION | |
58 | Where | |
59 | o length | |
60 | is the number of bytes in the record not including the % sign. | |
61 | o type | |
62 | is one of: | |
63 | 3) symbol record | |
64 | 6) data record | |
65 | 8) termination record | |
66 | ||
67 | ||
68 | The data can come out of order, and may be discontigous. This is a | |
69 | serial protocol, so big files are unlikely, so we keep a list of 8k chunks | |
70 | */ | |
71 | ||
72 | #include "bfd.h" | |
73 | #include "sysdep.h" | |
74 | #include "libbfd.h" | |
75 | ||
9783e04a DM |
76 | typedef struct |
77 | { | |
78 | bfd_vma low; | |
79 | bfd_vma high; | |
80 | } addr_range_type; | |
d3e667e8 | 81 | |
9783e04a DM |
82 | typedef struct tekhex_symbol_struct |
83 | { | |
d3e667e8 | 84 | |
9783e04a DM |
85 | asymbol symbol; |
86 | struct tekhex_symbol_struct *prev; | |
d3e667e8 | 87 | |
9783e04a | 88 | } tekhex_symbol_type; |
d3e667e8 JG |
89 | |
90 | static char digs[] = "0123456789ABCDEF"; | |
91 | ||
92 | static char sum_block[256]; | |
9783e04a | 93 | |
d3e667e8 JG |
94 | /* Horrible ascii dependent macros for converting between hex and |
95 | binary */ | |
96 | ||
97 | #define CHARS_IN_SET 256 | |
98 | static char hex_value[CHARS_IN_SET]; | |
9783e04a | 99 | |
d3e667e8 JG |
100 | #define NOT_HEX 20 |
101 | #define NIBBLE(x) hex_value[x] | |
102 | #define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1])) | |
103 | #define TOHEX(d,x) \ | |
104 | (d)[1] = digs[(x) & 0xf]; \ | |
105 | (d)[0] = digs[((x)>>4)&0xf]; | |
106 | #define ISHEX(x) (hex_value[x] != NOT_HEX) | |
107 | ||
108 | /* | |
109 | Here's an example | |
110 | %3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75 | |
111 | %1B3709T_SEGMENT1108FFFFFFFF | |
112 | %2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10 | |
113 | %373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710 | |
114 | %373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10 | |
115 | %373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10 | |
116 | %373049T_SEGMENT80long$long$int$t71080short$unsigned$i10 | |
117 | %373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10 | |
118 | %373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010 | |
119 | %373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10 | |
120 | %2734D9T_SEGMENT8Bvoid$t15$151035_main10 | |
121 | %2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110 | |
122 | %2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214 | |
123 | %07 8 10 10 | |
124 | ||
125 | explanation: | |
126 | %3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75 | |
127 | ^ ^^ ^ ^-data | |
128 | | || +------ 4 char integer 0x8000 | |
129 | | |+-------- checksum | |
130 | | +--------- type 6 (data record) | |
131 | +----------- length 3a chars | |
132 | <---------------------- 3a (58 chars) -------------------> | |
133 | ||
134 | %1B3709T_SEGMENT1108FFFFFFFF | |
135 | ^ ^^ ^- 8 character integer 0xffffffff | |
136 | | |+- 1 character integer 0 | |
137 | | +-- type 1 symbol (section definition) | |
138 | +------------ 9 char symbol T_SEGMENT | |
139 | ||
140 | %2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10 | |
141 | %373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710 | |
142 | %373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10 | |
143 | %373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10 | |
144 | %373049T_SEGMENT80long$long$int$t71080short$unsigned$i10 | |
145 | %373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10 | |
146 | %373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010 | |
147 | %373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10 | |
148 | %2734D9T_SEGMENT8Bvoid$t15$151035_main10 | |
149 | %2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110 | |
150 | %2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214 | |
151 | %0781010 | |
152 | ||
9783e04a | 153 | Turns into |
d3e667e8 JG |
154 | sac@thepub$ ./objdump -dx -m m68k f |
155 | ||
156 | f: file format tekhex | |
157 | -----x--- 9/55728 -134219416 Sep 29 15:13 1995 f | |
158 | architecture: UNKNOWN!, flags 0x00000010: | |
159 | HAS_SYMS | |
160 | start address 0x00000000 | |
161 | SECTION 0 [D00000000] : size 00020000 vma 00000000 align 2**0 | |
162 | ALLOC, LOAD | |
163 | SECTION 1 [D00008000] : size 00002001 vma 00008000 align 2**0 | |
9783e04a | 164 | |
d3e667e8 | 165 | SECTION 2 [T_SEGMENT] : size ffffffff vma 00000000 align 2**0 |
9783e04a | 166 | |
d3e667e8 JG |
167 | SYMBOL TABLE: |
168 | 00000000 g T_SEGMENT gcc_compiled$ | |
169 | 00000000 g T_SEGMENT hello$c | |
170 | 00000000 g T_SEGMENT int$t1$r1$$21474 | |
171 | 00000000 g T_SEGMENT char$t2$r2$0$127 | |
172 | 00000000 g T_SEGMENT long$int$t3$r1$$ | |
173 | 00000000 g T_SEGMENT unsigned$int$t4$ | |
174 | 00000000 g T_SEGMENT long$unsigned$in | |
175 | 00000000 g T_SEGMENT short$int$t6$r1$ | |
176 | 00000000 g T_SEGMENT long$long$int$t7 | |
177 | 00000000 g T_SEGMENT short$unsigned$i | |
178 | 00000000 g T_SEGMENT long$long$unsign | |
179 | 00000000 g T_SEGMENT signed$char$t10$ | |
180 | 00000000 g T_SEGMENT unsigned$char$t1 | |
181 | 00000000 g T_SEGMENT float$t12$r1$4$0 | |
182 | 00000000 g T_SEGMENT double$t13$r1$8$ | |
183 | 00000000 g T_SEGMENT long$double$t14$ | |
184 | 00000000 g T_SEGMENT void$t15$15 | |
185 | 00000000 g T_SEGMENT _main | |
186 | 00000000 g T_SEGMENT $ | |
187 | 00000000 g T_SEGMENT $ | |
188 | 00000000 g T_SEGMENT $ | |
189 | 00000010 g T_SEGMENT $ | |
190 | 00000000 g T_SEGMENT main$F1 | |
191 | fcffffff g T_SEGMENT i$1 | |
192 | 00000000 g T_SEGMENT $ | |
193 | 00000010 g T_SEGMENT $ | |
194 | ||
195 | ||
196 | RELOCATION RECORDS FOR [D00000000]: (none) | |
197 | ||
198 | RELOCATION RECORDS FOR [D00008000]: (none) | |
199 | ||
200 | RELOCATION RECORDS FOR [T_SEGMENT]: (none) | |
201 | ||
202 | Disassembly of section D00000000: | |
203 | ... | |
204 | 00008000 ($+)7ff0 linkw fp,#-4 | |
205 | 00008004 ($+)7ff4 nop | |
206 | 00008006 ($+)7ff6 movel #99,d0 | |
207 | 00008008 ($+)7ff8 cmpl fp@(-4),d0 | |
208 | 0000800c ($+)7ffc blts 00008014 ($+)8004 | |
209 | 0000800e ($+)7ffe addql #1,fp@(-4) | |
210 | 00008012 ($+)8002 bras 00008006 ($+)7ff6 | |
211 | 00008014 ($+)8004 unlk fp | |
212 | 00008016 ($+)8006 rts | |
213 | ... | |
214 | ||
215 | */ | |
216 | ||
217 | static void | |
9783e04a | 218 | tekhex_init () |
d3e667e8 JG |
219 | { |
220 | unsigned int i; | |
221 | static boolean inited = false; | |
9783e04a DM |
222 | int val; |
223 | ||
224 | if (inited == false) | |
d3e667e8 | 225 | { |
9783e04a | 226 | |
d3e667e8 | 227 | inited = true; |
9783e04a DM |
228 | |
229 | for (i = 0; i < CHARS_IN_SET; i++) | |
d3e667e8 JG |
230 | { |
231 | hex_value[i] = NOT_HEX; | |
232 | } | |
9783e04a DM |
233 | |
234 | for (i = 0; i < 10; i++) | |
d3e667e8 JG |
235 | { |
236 | hex_value[i + '0'] = i; | |
9783e04a | 237 | |
d3e667e8 | 238 | } |
9783e04a | 239 | for (i = 0; i < 6; i++) |
d3e667e8 | 240 | { |
9783e04a DM |
241 | hex_value[i + 'a'] = i + 10; |
242 | hex_value[i + 'A'] = i + 10; | |
d3e667e8 JG |
243 | } |
244 | val = 0; | |
9783e04a DM |
245 | for (i = 0; i < 10; i++) |
246 | { | |
247 | sum_block[i + '0'] = val++; | |
248 | } | |
249 | for (i = 'A'; i <= 'Z'; i++) | |
250 | { | |
251 | sum_block[i] = val++; | |
252 | } | |
d3e667e8 JG |
253 | sum_block['$'] = val++; |
254 | sum_block['%'] = val++; | |
255 | sum_block['.'] = val++; | |
256 | sum_block['_'] = val++; | |
9783e04a DM |
257 | for (i = 'a'; i <= 'z'; i++) |
258 | { | |
259 | sum_block[i] = val++; | |
260 | } | |
261 | } | |
d3e667e8 JG |
262 | } |
263 | ||
d3e667e8 | 264 | /* The maximum number of bytes on a line is FF */ |
9783e04a | 265 | #define MAXCHUNK 0xff |
d3e667e8 JG |
266 | /* The number of bytes we fit onto a line on output */ |
267 | #define CHUNK 21 | |
268 | ||
269 | /* We cannot output our tekhexords as we see them, we have to glue them | |
270 | together, this is done in this structure : */ | |
271 | ||
272 | struct tekhex_data_list_struct | |
273 | { | |
9783e04a DM |
274 | unsigned char *data; |
275 | bfd_vma where; | |
276 | bfd_size_type size; | |
277 | struct tekhex_data_list_struct *next; | |
d3e667e8 | 278 | |
9783e04a DM |
279 | }; |
280 | typedef struct tekhex_data_list_struct tekhex_data_list_type; | |
d3e667e8 JG |
281 | |
282 | #define CHUNK_MASK 0x1fff | |
283 | ||
9783e04a DM |
284 | struct data_struct |
285 | { | |
286 | char chunk_data[CHUNK_MASK + 1]; | |
287 | char chunk_init[CHUNK_MASK + 1]; | |
288 | bfd_vma vma; | |
289 | struct data_struct *next; | |
290 | }; | |
d3e667e8 JG |
291 | |
292 | typedef struct tekhex_data_struct | |
293 | { | |
9783e04a DM |
294 | tekhex_data_list_type *head; |
295 | unsigned int type; | |
296 | struct tekhex_symbol_struct *symbols; | |
297 | struct data_struct *data; | |
d3e667e8 JG |
298 | } tdata_type; |
299 | ||
d3e667e8 JG |
300 | #define enda(x) (x->vma + x->size) |
301 | ||
9783e04a DM |
302 | static bfd_vma |
303 | getvalue (srcp) | |
304 | char **srcp; | |
d3e667e8 JG |
305 | { |
306 | char *src = *srcp; | |
307 | bfd_vma value = 0; | |
308 | unsigned int len = hex_value[*src++]; | |
9783e04a DM |
309 | |
310 | if (len == 0) | |
311 | len = 16; | |
312 | while (len--) | |
313 | { | |
314 | value = value << 4 | hex_value[*src++]; | |
315 | } | |
d3e667e8 JG |
316 | *srcp = src; |
317 | return value; | |
318 | } | |
319 | ||
9783e04a DM |
320 | static unsigned int |
321 | getsym (dstp, srcp) | |
322 | char *dstp; | |
323 | char **srcp; | |
d3e667e8 JG |
324 | { |
325 | char *src = *srcp; | |
326 | unsigned int i; | |
327 | unsigned int len = hex_value[*src++]; | |
9783e04a DM |
328 | |
329 | if (len == 0) | |
330 | len = 16; | |
d3e667e8 JG |
331 | for (i = 0; i < len; i++) |
332 | dstp[i] = src[i]; | |
333 | dstp[i] = 0; | |
334 | *srcp = src + i; | |
335 | return len; | |
336 | } | |
337 | ||
338 | struct data_struct * | |
9783e04a DM |
339 | find_chunk (abfd, vma) |
340 | bfd *abfd; | |
341 | bfd_vma vma; | |
d3e667e8 JG |
342 | { |
343 | struct data_struct *d = abfd->tdata.tekhex_data->data; | |
9783e04a | 344 | |
d3e667e8 JG |
345 | vma &= ~CHUNK_MASK; |
346 | while (d && (d->vma) != vma) | |
347 | { | |
348 | d = d->next; | |
349 | } | |
9783e04a DM |
350 | if (!d) |
351 | { | |
352 | char *sname = bfd_alloc (abfd, 12); | |
353 | asection *s; | |
354 | ||
355 | /* No chunk for this address, so make one up */ | |
356 | d = (struct data_struct *) | |
357 | bfd_alloc (abfd, sizeof (struct data_struct)); | |
358 | ||
359 | if (!sname || !d) | |
360 | { | |
326e32d7 | 361 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
362 | return NULL; |
363 | } | |
364 | ||
365 | memset (d->chunk_init, 0, CHUNK_MASK + 1); | |
366 | memset (d->chunk_data, 0, CHUNK_MASK + 1); | |
367 | d->next = abfd->tdata.tekhex_data->data; | |
368 | d->vma = vma; | |
369 | abfd->tdata.tekhex_data->data = d; | |
370 | } | |
d3e667e8 JG |
371 | return d; |
372 | } | |
373 | ||
9783e04a DM |
374 | static void |
375 | insert_byte (abfd, value, addr) | |
376 | bfd *abfd; | |
377 | int value; | |
378 | bfd_vma addr; | |
d3e667e8 JG |
379 | { |
380 | /* Find the chunk that this byte needs and put it in */ | |
9783e04a | 381 | struct data_struct *d = find_chunk (abfd, addr); |
d3e667e8 | 382 | |
9783e04a DM |
383 | d->chunk_data[addr & CHUNK_MASK] = value; |
384 | d->chunk_init[addr & CHUNK_MASK] = 1; | |
d3e667e8 | 385 | } |
9783e04a DM |
386 | |
387 | /* The first pass is to find the names of all the sections, and see | |
d3e667e8 JG |
388 | how big the data is */ |
389 | static void | |
9783e04a DM |
390 | first_phase (abfd, type, src) |
391 | bfd *abfd; | |
392 | char type; | |
393 | char *src; | |
d3e667e8 JG |
394 | { |
395 | asection *section = &bfd_abs_section; | |
396 | int len; | |
397 | char sym[17]; /* A symbol can only be 16chars long */ | |
9783e04a | 398 | |
d3e667e8 JG |
399 | switch (type) |
400 | { | |
401 | case '6': | |
402 | /* Data record - read it and store it */ | |
403 | { | |
9783e04a DM |
404 | bfd_vma addr = getvalue (&src); |
405 | ||
406 | while (*src) | |
407 | { | |
408 | insert_byte (abfd, HEX (src), addr); | |
409 | src += 2; | |
410 | addr++; | |
411 | } | |
d3e667e8 | 412 | } |
9783e04a | 413 | |
d3e667e8 JG |
414 | return; |
415 | case '3': | |
416 | /* Symbol record, read the segment */ | |
9783e04a DM |
417 | len = getsym (sym, &src); |
418 | section = bfd_get_section_by_name (abfd, sym); | |
419 | if (section == (asection *) NULL) | |
d3e667e8 | 420 | { |
9783e04a DM |
421 | char *n = bfd_alloc (abfd, len + 1); |
422 | ||
423 | if (!n) | |
424 | { | |
326e32d7 | 425 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
426 | abort(); /* FIXME */ |
427 | } | |
428 | memcpy (n, sym, len + 1); | |
429 | section = bfd_make_section (abfd, n); | |
d3e667e8 | 430 | } |
9783e04a | 431 | while (*src) |
d3e667e8 | 432 | { |
9783e04a | 433 | switch (*src) |
d3e667e8 | 434 | { |
9783e04a DM |
435 | asection *s; |
436 | ||
437 | case '1': /* section range */ | |
d3e667e8 | 438 | src++; |
9783e04a DM |
439 | section->vma = getvalue (&src); |
440 | section->_raw_size = getvalue (&src) - section->vma; | |
441 | section->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC; | |
442 | break; | |
443 | ||
444 | case '2': | |
445 | case '3': | |
446 | case '4': | |
447 | case '6': | |
448 | case '7': | |
449 | case '8': | |
450 | /* Symbols, add to section */ | |
451 | { | |
452 | tekhex_symbol_type *new = | |
453 | (tekhex_symbol_type *) bfd_alloc (abfd, | |
454 | sizeof (tekhex_symbol_type)); | |
455 | char type = (*src); | |
456 | ||
457 | if (!new) | |
458 | { | |
326e32d7 | 459 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
460 | abort(); /* FIXME */ |
461 | } | |
462 | new->symbol.the_bfd = abfd; | |
463 | src++; | |
464 | abfd->symcount++; | |
465 | abfd->flags |= HAS_SYMS; | |
466 | new->prev = abfd->tdata.tekhex_data->symbols; | |
467 | abfd->tdata.tekhex_data->symbols = new; | |
468 | len = getsym (sym, &src); | |
469 | new->symbol.name = bfd_alloc (abfd, len + 1); | |
470 | if (!new->symbol.name) | |
471 | { | |
326e32d7 | 472 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
473 | abort(); /* FIXME */ |
474 | } | |
475 | memcpy ((char *) (new->symbol.name), sym, len + 1); | |
476 | new->symbol.section = section; | |
477 | if (type <= '4') | |
478 | new->symbol.flags = (BSF_GLOBAL | BSF_EXPORT); | |
479 | else | |
d3e667e8 | 480 | new->symbol.flags = BSF_LOCAL; |
9783e04a DM |
481 | new->symbol.value = getvalue (&src) - section->vma; |
482 | } | |
d3e667e8 | 483 | } |
9783e04a | 484 | } |
d3e667e8 JG |
485 | } |
486 | } | |
487 | ||
d3e667e8 JG |
488 | /* Pass over an tekhex, calling one of the above functions on each |
489 | record. */ | |
490 | ||
491 | static void | |
9783e04a DM |
492 | pass_over (abfd, func) |
493 | bfd *abfd; | |
494 | void (*func) (); | |
d3e667e8 JG |
495 | { |
496 | unsigned int chars_on_line; | |
497 | boolean eof = false; | |
498 | ||
499 | /* To the front of the file */ | |
4002f18a ILT |
500 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) |
501 | abort (); | |
d3e667e8 JG |
502 | while (eof == false) |
503 | { | |
504 | char buffer[MAXCHUNK]; | |
505 | char *src = buffer; | |
506 | char type; | |
507 | bfd_vma address = 0; | |
508 | ||
509 | /* Find first '%' */ | |
9783e04a DM |
510 | eof = (boolean) (bfd_read (src, 1, 1, abfd) != 1); |
511 | while (*src != '%' && !eof) | |
512 | { | |
513 | eof = (boolean) (bfd_read (src, 1, 1, abfd) != 1); | |
514 | } | |
515 | if (eof) | |
516 | break; | |
d3e667e8 JG |
517 | src++; |
518 | ||
d3e667e8 | 519 | /* Fetch the type and the length and the checksum */ |
4002f18a ILT |
520 | if (bfd_read (src, 1, 5, abfd) != 5) |
521 | abort (); /* FIXME */ | |
d3e667e8 JG |
522 | |
523 | type = src[2]; | |
524 | ||
525 | if (!ISHEX (src[0]) || !ISHEX (src[1])) | |
526 | break; | |
527 | ||
9783e04a | 528 | chars_on_line = HEX (src) - 5; /* Already read five char */ |
d3e667e8 | 529 | |
4002f18a ILT |
530 | if (bfd_read (src, 1, chars_on_line, abfd) != chars_on_line) |
531 | abort (); /* FIXME */ | |
d3e667e8 JG |
532 | src[chars_on_line] = 0; /* put a null at the end */ |
533 | ||
9783e04a | 534 | func (abfd, type, src); |
d3e667e8 JG |
535 | } |
536 | ||
537 | } | |
538 | ||
326e32d7 | 539 | long |
9783e04a DM |
540 | tekhex_get_symtab (abfd, table) |
541 | bfd *abfd; | |
542 | asymbol **table; | |
543 | ||
d3e667e8 JG |
544 | { |
545 | tekhex_symbol_type *p = abfd->tdata.tekhex_data->symbols; | |
9783e04a DM |
546 | unsigned int c = bfd_get_symcount (abfd); |
547 | ||
548 | table[c] = 0; | |
549 | while (p) | |
550 | { | |
551 | table[--c] = &(p->symbol); | |
552 | p = p->prev; | |
553 | } | |
554 | ||
555 | return bfd_get_symcount (abfd); | |
d3e667e8 | 556 | } |
9783e04a | 557 | |
326e32d7 | 558 | long |
9783e04a DM |
559 | tekhex_get_symtab_upper_bound (abfd) |
560 | bfd *abfd; | |
d3e667e8 | 561 | { |
9783e04a | 562 | return (abfd->symcount + 1) * (sizeof (struct tekhex_asymbol_struct *)); |
d3e667e8 JG |
563 | |
564 | } | |
565 | ||
566 | static boolean | |
9783e04a DM |
567 | tekhex_mkobject (abfd) |
568 | bfd *abfd; | |
d3e667e8 | 569 | { |
9783e04a DM |
570 | tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type)); |
571 | ||
572 | if (!tdata) | |
573 | { | |
326e32d7 | 574 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
575 | return false; |
576 | } | |
577 | abfd->tdata.tekhex_data = tdata; | |
578 | tdata->type = 1; | |
579 | tdata->head = (tekhex_data_list_type *) NULL; | |
580 | tdata->symbols = (struct tekhex_symbol_struct *) NULL; | |
581 | tdata->data = (struct data_struct *) NULL; | |
582 | return true; | |
d3e667e8 JG |
583 | } |
584 | ||
9783e04a | 585 | /* |
d3e667e8 JG |
586 | Return true if the file looks like it's in TekHex format. Just look |
587 | for a percent sign and some hex digits */ | |
d3e667e8 | 588 | |
2f3508ad | 589 | static const bfd_target * |
9783e04a DM |
590 | tekhex_object_p (abfd) |
591 | bfd *abfd; | |
d3e667e8 JG |
592 | { |
593 | char b[4]; | |
594 | asection *section; | |
d3e667e8 | 595 | |
9783e04a DM |
596 | tekhex_init (); |
597 | ||
4002f18a ILT |
598 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 |
599 | || bfd_read (b, 1, 4, abfd) != 4) | |
600 | return NULL; | |
9783e04a DM |
601 | |
602 | if (b[0] != '%' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3])) | |
2f3508ad | 603 | return (const bfd_target *) NULL; |
d3e667e8 | 604 | |
9783e04a | 605 | tekhex_mkobject (abfd); |
d3e667e8 | 606 | |
9783e04a | 607 | pass_over (abfd, first_phase); |
d3e667e8 JG |
608 | return abfd->xvec; |
609 | } | |
610 | ||
d3e667e8 | 611 | static boolean |
9783e04a DM |
612 | move_section_contents (abfd, section, locationp, offset, count, get) |
613 | bfd *abfd; | |
614 | asection *section; | |
615 | PTR locationp; | |
616 | file_ptr offset; | |
617 | bfd_size_type count; | |
618 | boolean get; | |
d3e667e8 JG |
619 | { |
620 | bfd_vma addr; | |
9783e04a | 621 | char *location = (char *) locationp; |
d3e667e8 | 622 | bfd_vma prev_number = 1; /* Nothing can have this as a high bit*/ |
9783e04a DM |
623 | struct data_struct *d = (struct data_struct *) NULL; |
624 | ||
625 | for (addr = section->vma; count != 0; count--, addr++) | |
d3e667e8 JG |
626 | { |
627 | ||
9783e04a | 628 | bfd_vma chunk_number = addr & ~CHUNK_MASK; /* Get high bits of address */ |
d3e667e8 | 629 | bfd_vma low_bits = addr & CHUNK_MASK; |
9783e04a DM |
630 | |
631 | if (chunk_number != prev_number) | |
d3e667e8 JG |
632 | { |
633 | /* Different chunk, so move pointer */ | |
9783e04a | 634 | d = find_chunk (abfd, chunk_number); |
d3e667e8 JG |
635 | } |
636 | ||
9783e04a DM |
637 | if (get) |
638 | { | |
639 | if (d->chunk_init[low_bits]) | |
640 | { | |
641 | *location = d->chunk_data[low_bits]; | |
642 | } | |
643 | else | |
644 | { | |
645 | *location = 0; | |
646 | } | |
647 | } | |
648 | else | |
649 | { | |
650 | d->chunk_data[low_bits] = *location; | |
651 | d->chunk_init[low_bits] = (*location != 0); | |
652 | } | |
d3e667e8 | 653 | |
9783e04a | 654 | location++; |
d3e667e8 JG |
655 | |
656 | } | |
657 | ||
9783e04a | 658 | } |
d3e667e8 | 659 | static boolean |
9783e04a DM |
660 | tekhex_get_section_contents (abfd, section, locationp, offset, count) |
661 | bfd *abfd; | |
662 | asection *section; | |
663 | PTR locationp; | |
664 | file_ptr offset; | |
665 | bfd_size_type count; | |
d3e667e8 JG |
666 | { |
667 | if (section->flags & (SEC_LOAD | SEC_ALLOC)) | |
668 | { | |
9783e04a | 669 | move_section_contents (abfd, section, locationp, offset, count, true); |
d3e667e8 JG |
670 | return true; |
671 | } | |
9783e04a DM |
672 | else |
673 | return false; | |
d3e667e8 | 674 | } |
d3e667e8 JG |
675 | |
676 | boolean | |
9783e04a DM |
677 | tekhex_set_arch_mach (abfd, arch, machine) |
678 | bfd *abfd; | |
679 | enum bfd_architecture arch; | |
680 | unsigned long machine; | |
d3e667e8 | 681 | { |
9783e04a | 682 | return bfd_default_set_arch_mach (abfd, arch, machine); |
d3e667e8 JG |
683 | } |
684 | ||
d3e667e8 JG |
685 | /* we have to save up all the Tekhexords for a splurge before output, |
686 | */ | |
687 | ||
688 | static boolean | |
9783e04a DM |
689 | tekhex_set_section_contents (abfd, section, locationp, offset, bytes_to_do) |
690 | bfd *abfd; | |
691 | sec_ptr section; | |
692 | PTR locationp; | |
693 | file_ptr offset; | |
694 | bfd_size_type bytes_to_do; | |
d3e667e8 JG |
695 | { |
696 | ||
9783e04a DM |
697 | if (abfd->output_has_begun == false) |
698 | { | |
699 | /* The first time around, allocate enough sections to hold all the chunks */ | |
700 | asection *s = abfd->sections; | |
701 | bfd_vma vma; | |
702 | ||
703 | for (s = abfd->sections; s; s = s->next) | |
704 | { | |
705 | if (s->flags & SEC_LOAD) | |
706 | { | |
707 | for (vma = s->vma & ~CHUNK_MASK; | |
708 | vma < s->vma + s->_raw_size; | |
709 | vma += CHUNK_MASK) | |
710 | find_chunk (abfd, vma); | |
711 | } | |
712 | } | |
d3e667e8 | 713 | |
9783e04a | 714 | } |
d3e667e8 JG |
715 | if (section->flags & (SEC_LOAD | SEC_ALLOC)) |
716 | { | |
9783e04a | 717 | move_section_contents (abfd, section, locationp, offset, bytes_to_do, false); |
d3e667e8 JG |
718 | return true; |
719 | } | |
9783e04a DM |
720 | else |
721 | return false; | |
d3e667e8 JG |
722 | |
723 | } | |
724 | ||
d3e667e8 | 725 | static void |
9783e04a DM |
726 | writevalue (dst, value) |
727 | char **dst; | |
728 | bfd_vma value; | |
d3e667e8 JG |
729 | { |
730 | char *p = *dst; | |
9783e04a | 731 | int len; |
d3e667e8 | 732 | int shift; |
9783e04a DM |
733 | |
734 | for (len = 8, shift = 28; shift; shift -= 4, len--) | |
d3e667e8 | 735 | { |
9783e04a DM |
736 | if ((value >> shift) & 0xf) |
737 | { | |
738 | *p++ = len + '0'; | |
739 | while (len) | |
740 | { | |
741 | *p++ = digs[(value >> shift) & 0xf]; | |
742 | shift -= 4; | |
743 | len--; | |
744 | } | |
745 | *dst = p; | |
746 | return; | |
d3e667e8 | 747 | |
9783e04a | 748 | } |
d3e667e8 JG |
749 | } |
750 | *p++ = '1'; | |
751 | *p++ = '0'; | |
752 | *dst = p; | |
753 | } | |
754 | ||
755 | static void | |
9783e04a DM |
756 | writesym (dst, sym) |
757 | char **dst; | |
758 | CONST char *sym; | |
d3e667e8 JG |
759 | { |
760 | char *p = *dst; | |
9783e04a | 761 | int len = (sym ? strlen (sym) : 0); |
d3e667e8 | 762 | |
9783e04a DM |
763 | if (len >= 16) |
764 | { | |
765 | *p++ = '0'; | |
766 | len = 16; | |
d3e667e8 | 767 | } |
d3e667e8 | 768 | |
9783e04a DM |
769 | else |
770 | { | |
771 | if (len == 0) | |
772 | { | |
773 | *p++ = '1'; | |
774 | sym = "$"; | |
775 | len = 1; | |
776 | } | |
777 | else | |
778 | { | |
779 | *p++ = digs[len]; | |
780 | } | |
781 | } | |
782 | ||
783 | while (len--) | |
784 | { | |
785 | *p++ = *sym++; | |
786 | } | |
d3e667e8 JG |
787 | *dst = p; |
788 | } | |
789 | ||
9783e04a DM |
790 | static void |
791 | out (abfd, type, start, end) | |
792 | bfd *abfd; | |
793 | char type; | |
794 | char *start; | |
795 | char *end; | |
d3e667e8 JG |
796 | { |
797 | int sum = 0; | |
798 | char *s; | |
799 | char front[6]; | |
9783e04a | 800 | |
d3e667e8 | 801 | front[0] = '%'; |
9783e04a | 802 | TOHEX (front + 1, end - start + 5); |
d3e667e8 JG |
803 | front[3] = type; |
804 | ||
9783e04a DM |
805 | for (s = start; s < end; s++) |
806 | { | |
807 | sum += sum_block[*s]; | |
808 | } | |
d3e667e8 | 809 | |
9783e04a | 810 | sum += sum_block[front[1]]; /* length */ |
d3e667e8 | 811 | sum += sum_block[front[2]]; |
9783e04a DM |
812 | sum += sum_block[front[3]]; /* type */ |
813 | TOHEX (front + 4, sum); | |
4002f18a ILT |
814 | if (bfd_write (front, 1, 6, abfd) != 6) |
815 | abort (); | |
d3e667e8 | 816 | end[0] = '\n'; |
4002f18a ILT |
817 | if (bfd_write (start, 1, end - start + 1, abfd) != end - start + 1) |
818 | abort (); | |
d3e667e8 | 819 | } |
9783e04a | 820 | |
d3e667e8 | 821 | static boolean |
9783e04a DM |
822 | tekhex_write_object_contents (abfd) |
823 | bfd *abfd; | |
d3e667e8 JG |
824 | { |
825 | int bytes_written; | |
826 | char buffer[100]; | |
827 | asymbol **p; | |
828 | tdata_type *tdata = abfd->tdata.tekhex_data; | |
829 | tekhex_data_list_type *list; | |
830 | asection *s; | |
831 | struct data_struct *d; | |
9783e04a | 832 | |
d3e667e8 JG |
833 | bytes_written = 0; |
834 | ||
835 | /* And the raw data */ | |
836 | for (d = abfd->tdata.tekhex_data->data; | |
9783e04a | 837 | d != (struct data_struct *) NULL; |
d3e667e8 JG |
838 | d = d->next) |
839 | { | |
840 | int low; | |
841 | ||
9783e04a | 842 | CONST int span = 32; |
d3e667e8 | 843 | int addr; |
9783e04a | 844 | |
d3e667e8 JG |
845 | /* Write it in blocks of 32 bytes */ |
846 | ||
9783e04a | 847 | for (addr = 0; addr < CHUNK_MASK + 1; addr += span) |
d3e667e8 JG |
848 | { |
849 | int need = 0; | |
9783e04a | 850 | |
d3e667e8 | 851 | /* Check to see if necessary */ |
9783e04a DM |
852 | for (low = 0; !need && low < span; low++) |
853 | { | |
854 | if (d->chunk_init[addr + low]) | |
855 | need = 1; | |
856 | } | |
857 | if (need) | |
858 | { | |
859 | char *dst = buffer; | |
860 | ||
861 | writevalue (&dst, addr + d->vma); | |
862 | for (low = 0; low < span; low++) | |
863 | { | |
864 | TOHEX (dst, d->chunk_data[addr + low]); | |
865 | dst += 2; | |
866 | } | |
867 | out (abfd, '6', buffer, dst); | |
d3e667e8 | 868 | } |
d3e667e8 JG |
869 | } |
870 | } | |
871 | /* write all the section headers for the sections */ | |
9783e04a | 872 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
d3e667e8 JG |
873 | { |
874 | char *dst = buffer; | |
9783e04a DM |
875 | |
876 | writesym (&dst, s->name); | |
d3e667e8 | 877 | *dst++ = '1'; |
9783e04a DM |
878 | writevalue (&dst, s->vma); |
879 | writevalue (&dst, s->vma + s->_raw_size); | |
880 | out (abfd, '3', buffer, dst); | |
d3e667e8 JG |
881 | } |
882 | ||
883 | /* And the symbols */ | |
9783e04a DM |
884 | for (p = abfd->outsymbols; *p; p++) |
885 | { | |
886 | int section_code = bfd_decode_symclass (*p); | |
887 | ||
888 | if (section_code != '?') | |
889 | { /* do not include debug symbols */ | |
890 | asymbol *s = *p; | |
891 | char *dst = buffer; | |
892 | ||
893 | writesym (&dst, s->section->name); | |
894 | ||
895 | switch (section_code) | |
896 | { | |
897 | case 'A': | |
898 | *dst++ = '2'; | |
899 | break; | |
900 | case 'a': | |
901 | *dst++ = '6'; | |
902 | break; | |
903 | case 'D': | |
904 | case 'B': | |
905 | case 'O': | |
906 | *dst++ = '4'; | |
907 | break; | |
908 | case 'd': | |
909 | case 'b': | |
910 | case 'o': | |
911 | *dst++ = '8'; | |
912 | break; | |
913 | case 'T': | |
914 | *dst++ = '3'; | |
915 | break; | |
916 | case 't': | |
917 | *dst++ = '7'; | |
918 | break; | |
919 | case 'C': | |
920 | case 'U': | |
326e32d7 | 921 | bfd_set_error (bfd_error_wrong_format); |
9783e04a DM |
922 | return false; |
923 | } | |
d3e667e8 | 924 | |
9783e04a DM |
925 | writesym (&dst, s->name); |
926 | writevalue (&dst, s->value + s->section->vma); | |
927 | out (abfd, '3', buffer, dst); | |
928 | } | |
d3e667e8 | 929 | } |
d3e667e8 | 930 | |
d3e667e8 | 931 | /* And the terminator */ |
4002f18a ILT |
932 | if (bfd_write ("%7081010\n", 1, 9, abfd) != 9) |
933 | abort (); | |
9783e04a | 934 | return true; |
d3e667e8 | 935 | } |
d3e667e8 | 936 | |
9783e04a DM |
937 | static int |
938 | tekhex_sizeof_headers (abfd, exec) | |
939 | bfd *abfd; | |
940 | boolean exec; | |
941 | ||
d3e667e8 | 942 | { |
9783e04a | 943 | return 0; |
d3e667e8 JG |
944 | } |
945 | ||
946 | static asymbol * | |
9783e04a DM |
947 | tekhex_make_empty_symbol (abfd) |
948 | bfd *abfd; | |
d3e667e8 | 949 | { |
9783e04a DM |
950 | tekhex_symbol_type *new = |
951 | (tekhex_symbol_type *) bfd_zalloc (abfd, sizeof (struct tekhex_symbol_struct)); | |
952 | ||
953 | if (!new) | |
954 | { | |
326e32d7 | 955 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
956 | return NULL; |
957 | } | |
d3e667e8 | 958 | new->symbol.the_bfd = abfd; |
9783e04a | 959 | new->prev = (struct tekhex_symbol_struct *) NULL; |
d3e667e8 JG |
960 | return &(new->symbol); |
961 | } | |
962 | ||
963 | static void | |
9783e04a DM |
964 | tekhex_get_symbol_info (ignore_abfd, symbol, ret) |
965 | bfd *ignore_abfd; | |
966 | asymbol *symbol; | |
967 | symbol_info *ret; | |
d3e667e8 | 968 | { |
9783e04a DM |
969 | bfd_symbol_info (symbol, ret); |
970 | } | |
d3e667e8 | 971 | |
9783e04a DM |
972 | static void |
973 | tekhex_print_symbol (ignore_abfd, filep, symbol, how) | |
974 | bfd *ignore_abfd; | |
975 | PTR filep; | |
976 | asymbol *symbol; | |
977 | bfd_print_symbol_type how; | |
978 | { | |
979 | FILE *file = (FILE *) filep; | |
980 | ||
981 | switch (how) | |
d3e667e8 | 982 | { |
9783e04a DM |
983 | case bfd_print_symbol_name: |
984 | fprintf (file, "%s", symbol->name); | |
985 | break; | |
986 | case bfd_print_symbol_more: | |
987 | break; | |
d3e667e8 | 988 | |
9783e04a DM |
989 | case bfd_print_symbol_all: |
990 | { | |
991 | CONST char *section_name = symbol->section->name; | |
992 | ||
993 | bfd_print_symbol_vandf ((PTR) file, symbol); | |
994 | ||
995 | fprintf (file, " %-5s %s", | |
996 | section_name, | |
997 | symbol->name); | |
998 | } | |
999 | } | |
d3e667e8 JG |
1000 | } |
1001 | ||
6812b607 ILT |
1002 | #define tekhex_close_and_cleanup _bfd_generic_close_and_cleanup |
1003 | #define tekhex_bfd_free_cached_info _bfd_generic_bfd_free_cached_info | |
1004 | #define tekhex_new_section_hook _bfd_generic_new_section_hook | |
1005 | ||
1006 | #define tekhex_bfd_is_local_label bfd_generic_is_local_label | |
1007 | #define tekhex_get_lineno _bfd_nosymbols_get_lineno | |
1008 | #define tekhex_find_nearest_line _bfd_nosymbols_find_nearest_line | |
1009 | #define tekhex_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol | |
1010 | ||
1011 | #define tekhex_bfd_get_relocated_section_contents \ | |
1012 | bfd_generic_get_relocated_section_contents | |
d3e667e8 | 1013 | #define tekhex_bfd_relax_section bfd_generic_relax_section |
9783e04a DM |
1014 | #define tekhex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create |
1015 | #define tekhex_bfd_link_add_symbols _bfd_generic_link_add_symbols | |
1016 | #define tekhex_bfd_final_link _bfd_generic_final_link | |
1017 | ||
2f3508ad | 1018 | const bfd_target tekhex_vec = |
d3e667e8 | 1019 | { |
9783e04a DM |
1020 | "tekhex", /* name */ |
1021 | bfd_target_tekhex_flavour, | |
1022 | true, /* target byte order */ | |
1023 | true, /* target headers byte order */ | |
1024 | (EXEC_P | /* object flags */ | |
1025 | HAS_SYMS | HAS_LINENO | HAS_DEBUG | HAS_RELOC | HAS_LOCALS | | |
1026 | WP_TEXT | D_PAGED), | |
1027 | (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS | |
1028 | | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ | |
1029 | 0, /* leading underscore */ | |
1030 | ' ', /* ar_pad_char */ | |
1031 | 16, /* ar_max_namelen */ | |
1032 | 1, /* minimum alignment */ | |
1033 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
1034 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1035 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ | |
1036 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, | |
1037 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
1038 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ | |
d3e667e8 JG |
1039 | |
1040 | { | |
9783e04a DM |
1041 | _bfd_dummy_target, |
1042 | tekhex_object_p, /* bfd_check_format */ | |
2f3508ad ILT |
1043 | _bfd_dummy_target, |
1044 | _bfd_dummy_target, | |
d3e667e8 JG |
1045 | }, |
1046 | { | |
9783e04a DM |
1047 | bfd_false, |
1048 | tekhex_mkobject, | |
1049 | _bfd_generic_mkarchive, | |
1050 | bfd_false, | |
d3e667e8 JG |
1051 | }, |
1052 | { /* bfd_write_contents */ | |
9783e04a DM |
1053 | bfd_false, |
1054 | tekhex_write_object_contents, | |
1055 | _bfd_write_archive_contents, | |
1056 | bfd_false, | |
d3e667e8 | 1057 | }, |
6812b607 ILT |
1058 | |
1059 | BFD_JUMP_TABLE_GENERIC (tekhex), | |
1060 | BFD_JUMP_TABLE_COPY (_bfd_generic), | |
1061 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | |
1062 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), | |
1063 | BFD_JUMP_TABLE_SYMBOLS (tekhex), | |
1064 | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), | |
1065 | BFD_JUMP_TABLE_WRITE (tekhex), | |
1066 | BFD_JUMP_TABLE_LINK (tekhex), | |
dfc1c006 | 1067 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
6812b607 | 1068 | |
9783e04a DM |
1069 | (PTR) 0 |
1070 | }; |