Bug Summary

File:src/gnu/usr.bin/binutils/bfd/tekhex.c
Warning:line 661, column 28
Array access (via field 'chunk_data') results in a null pointer dereference

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name tekhex.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/binutils/obj/bfd -resource-dir /usr/local/lib/clang/13.0.0 -D HAVE_CONFIG_H -I . -I /usr/src/gnu/usr.bin/binutils/bfd -I . -D _GNU_SOURCE -D NETBSD_CORE -I . -I /usr/src/gnu/usr.bin/binutils/bfd -I /usr/src/gnu/usr.bin/binutils/bfd/../include -I /usr/src/gnu/usr.bin/binutils/bfd/../intl -I ../intl -D PIE_DEFAULT=1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/gnu/usr.bin/binutils/obj/bfd -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/gnu/usr.bin/binutils/bfd/tekhex.c
1/* BFD backend for Extended Tektronix Hex Format objects.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22/*
23SUBSECTION
24 Tektronix Hex Format handling
25
26DESCRIPTION
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 described 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
35 read in the file.
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
45 create.
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 A TekHex record looks like:
53EXAMPLE
54 %<block length><type><checksum><stuff><cr>
55
56DESCRIPTION
57 Where
58 o length
59 is the number of bytes in the record not including the % sign.
60 o type
61 is one of:
62 3) symbol record
63 6) data record
64 8) termination record
65
66The data can come out of order, and may be discontigous. This is a
67serial protocol, so big files are unlikely, so we keep a list of 8k chunks
68*/
69
70#include "bfd.h"
71#include "sysdep.h"
72#include "libbfd.h"
73#include "libiberty.h"
74
75typedef struct
76 {
77 bfd_vma low;
78 bfd_vma high;
79 } addr_range_type;
80
81typedef struct tekhex_symbol_struct
82 {
83
84 asymbol symbol;
85 struct tekhex_symbol_struct *prev;
86
87 } tekhex_symbol_type;
88
89static const char digs[] = "0123456789ABCDEF";
90
91static char sum_block[256];
92
93#define NOT_HEX20 20
94#define NIBBLE(x)((unsigned int) _hex_value[(unsigned char) (x)]) hex_value(x)((unsigned int) _hex_value[(unsigned char) (x)])
95#define HEX(buffer)((((unsigned int) _hex_value[(unsigned char) ((buffer)[0])])<<
4) + ((unsigned int) _hex_value[(unsigned char) ((buffer)[1])
]))
((NIBBLE((buffer)[0])((unsigned int) _hex_value[(unsigned char) ((buffer)[0])])<<4) + NIBBLE((buffer)[1])((unsigned int) _hex_value[(unsigned char) ((buffer)[1])]))
96#define TOHEX(d,x)(d)[1] = digs[(x) & 0xf]; (d)[0] = digs[((x)>>4)&
0xf];
\
97(d)[1] = digs[(x) & 0xf]; \
98(d)[0] = digs[((x)>>4)&0xf];
99#define ISHEX(x)(((unsigned int) _hex_value[(unsigned char) (x)]) != 99) hex_p(x)(((unsigned int) _hex_value[(unsigned char) (x)]) != 99)
100
101static void tekhex_init PARAMS ((void))(void);
102static bfd_boolean getvalue PARAMS ((char **, bfd_vma *))(char **, bfd_vma *);
103static void tekhex_print_symbol
104 PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type))(bfd *, void *, asymbol *, bfd_print_symbol_type);
105static void tekhex_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *))(bfd *, asymbol *, symbol_info *);
106static asymbol *tekhex_make_empty_symbol PARAMS ((bfd *))(bfd *);
107static int tekhex_sizeof_headers PARAMS ((bfd *, bfd_boolean))(bfd *, bfd_boolean);
108static bfd_boolean tekhex_write_object_contents PARAMS ((bfd *))(bfd *);
109static void out PARAMS ((bfd *, int, char *, char *))(bfd *, int, char *, char *);
110static void writesym PARAMS ((char **, const char *))(char **, const char *);
111static void writevalue PARAMS ((char **, bfd_vma))(char **, bfd_vma);
112static bfd_boolean tekhex_set_section_contents
113 PARAMS ((bfd*, sec_ptr, const PTR, file_ptr, bfd_size_type))(bfd*, sec_ptr, const void *, file_ptr, bfd_size_type);
114static bfd_boolean tekhex_set_arch_mach
115 PARAMS ((bfd *, enum bfd_architecture, unsigned long))(bfd *, enum bfd_architecture, unsigned long);
116static bfd_boolean tekhex_get_section_contents
117 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type))(bfd *, asection *, void *, file_ptr, bfd_size_type);
118static void move_section_contents
119 PARAMS ((bfd *, asection *, const PTR, file_ptr, bfd_size_type, bfd_boolean))(bfd *, asection *, const void *, file_ptr, bfd_size_type, bfd_boolean
)
;
120static const bfd_target *tekhex_object_p PARAMS ((bfd *))(bfd *);
121static bfd_boolean tekhex_mkobject PARAMS ((bfd *))(bfd *);
122static long tekhex_get_symtab_upper_bound PARAMS ((bfd *))(bfd *);
123static long tekhex_canonicalize_symtab PARAMS ((bfd *, asymbol **))(bfd *, asymbol **);
124static bfd_boolean pass_over PARAMS ((bfd *, bfd_boolean (*) (bfd*, int, char *)))(bfd *, bfd_boolean (*) (bfd*, int, char *));
125static bfd_boolean first_phase PARAMS ((bfd *, int, char *))(bfd *, int, char *);
126static void insert_byte PARAMS ((bfd *, int, bfd_vma))(bfd *, int, bfd_vma);
127static struct data_struct *find_chunk PARAMS ((bfd *, bfd_vma))(bfd *, bfd_vma);
128static bfd_boolean getsym PARAMS ((char *, char **, unsigned int *))(char *, char **, unsigned int *);
129
130/*
131Here's an example
132%3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75
133%1B3709T_SEGMENT1108FFFFFFFF
134%2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10
135%373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710
136%373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10
137%373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10
138%373049T_SEGMENT80long$long$int$t71080short$unsigned$i10
139%373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10
140%373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010
141%373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10
142%2734D9T_SEGMENT8Bvoid$t15$151035_main10
143%2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110
144%2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214
145%07 8 10 10
146
147explanation:
148%3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75
149 ^ ^^ ^ ^-data
150 | || +------ 4 char integer 0x8000
151 | |+-------- checksum
152 | +--------- type 6 (data record)
153 +----------- length 3a chars
154 <---------------------- 3a (58 chars) ------------------->
155
156%1B3709T_SEGMENT1108FFFFFFFF
157 ^ ^^ ^- 8 character integer 0xffffffff
158 | |+- 1 character integer 0
159 | +-- type 1 symbol (section definition)
160 +------------ 9 char symbol T_SEGMENT
161
162%2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10
163%373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710
164%373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10
165%373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10
166%373049T_SEGMENT80long$long$int$t71080short$unsigned$i10
167%373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10
168%373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010
169%373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10
170%2734D9T_SEGMENT8Bvoid$t15$151035_main10
171%2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110
172%2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214
173%0781010
174
175Turns into
176sac@thepub$ ./objdump -dx -m m68k f
177
178f: file format tekhex
179-----x--- 9/55728 -134219416 Sep 29 15:13 1995 f
180architecture: UNKNOWN!, flags 0x00000010:
181HAS_SYMS
182start address 0x00000000
183SECTION 0 [D00000000] : size 00020000 vma 00000000 align 2**0
184 ALLOC, LOAD
185SECTION 1 [D00008000] : size 00002001 vma 00008000 align 2**0
186
187SECTION 2 [T_SEGMENT] : size ffffffff vma 00000000 align 2**0
188
189SYMBOL TABLE:
19000000000 g T_SEGMENT gcc_compiled$
19100000000 g T_SEGMENT hello$c
19200000000 g T_SEGMENT int$t1$r1$$21474
19300000000 g T_SEGMENT char$t2$r2$0$127
19400000000 g T_SEGMENT long$int$t3$r1$$
19500000000 g T_SEGMENT unsigned$int$t4$
19600000000 g T_SEGMENT long$unsigned$in
19700000000 g T_SEGMENT short$int$t6$r1$
19800000000 g T_SEGMENT long$long$int$t7
19900000000 g T_SEGMENT short$unsigned$i
20000000000 g T_SEGMENT long$long$unsign
20100000000 g T_SEGMENT signed$char$t10$
20200000000 g T_SEGMENT unsigned$char$t1
20300000000 g T_SEGMENT float$t12$r1$4$0
20400000000 g T_SEGMENT double$t13$r1$8$
20500000000 g T_SEGMENT long$double$t14$
20600000000 g T_SEGMENT void$t15$15
20700000000 g T_SEGMENT _main
20800000000 g T_SEGMENT $
20900000000 g T_SEGMENT $
21000000000 g T_SEGMENT $
21100000010 g T_SEGMENT $
21200000000 g T_SEGMENT main$F1
213fcffffff g T_SEGMENT i$1
21400000000 g T_SEGMENT $
21500000010 g T_SEGMENT $
216
217RELOCATION RECORDS FOR [D00000000]: (none)
218
219RELOCATION RECORDS FOR [D00008000]: (none)
220
221RELOCATION RECORDS FOR [T_SEGMENT]: (none)
222
223Disassembly of section D00000000:
224...
22500008000 ($+)7ff0 linkw fp,#-4
22600008004 ($+)7ff4 nop
22700008006 ($+)7ff6 movel #99,d0
22800008008 ($+)7ff8 cmpl fp@(-4),d0
2290000800c ($+)7ffc blts 00008014 ($+)8004
2300000800e ($+)7ffe addql #1,fp@(-4)
23100008012 ($+)8002 bras 00008006 ($+)7ff6
23200008014 ($+)8004 unlk fp
23300008016 ($+)8006 rts
234...
235
236*/
237
238static void
239tekhex_init ()
240{
241 unsigned int i;
242 static bfd_boolean inited = FALSE0;
243 int val;
244
245 if (! inited)
246 {
247 inited = TRUE1;
248 hex_init ();
249 val = 0;
250 for (i = 0; i < 10; i++)
251 {
252 sum_block[i + '0'] = val++;
253 }
254 for (i = 'A'; i <= 'Z'; i++)
255 {
256 sum_block[i] = val++;
257 }
258 sum_block['$'] = val++;
259 sum_block['%'] = val++;
260 sum_block['.'] = val++;
261 sum_block['_'] = val++;
262 for (i = 'a'; i <= 'z'; i++)
263 {
264 sum_block[i] = val++;
265 }
266 }
267}
268
269/* The maximum number of bytes on a line is FF */
270#define MAXCHUNK0xff 0xff
271/* The number of bytes we fit onto a line on output */
272#define CHUNK21 21
273
274/* We cannot output our tekhexords as we see them, we have to glue them
275 together, this is done in this structure : */
276
277struct tekhex_data_list_struct
278{
279 unsigned char *data;
280 bfd_vma where;
281 bfd_size_type size;
282 struct tekhex_data_list_struct *next;
283
284};
285typedef struct tekhex_data_list_struct tekhex_data_list_type;
286
287#define CHUNK_MASK0x1fff 0x1fff
288
289struct data_struct
290 {
291 char chunk_data[CHUNK_MASK0x1fff + 1];
292 char chunk_init[CHUNK_MASK0x1fff + 1];
293 bfd_vma vma;
294 struct data_struct *next;
295 };
296
297typedef struct tekhex_data_struct
298{
299 tekhex_data_list_type *head;
300 unsigned int type;
301 struct tekhex_symbol_struct *symbols;
302 struct data_struct *data;
303} tdata_type;
304
305#define enda(x)(x->vma + x->size) (x->vma + x->size)
306
307static bfd_boolean
308getvalue (char **srcp, bfd_vma *valuep)
309{
310 char *src = *srcp;
311 bfd_vma value = 0;
312 unsigned int len;
313
314 if (!ISHEX (*src)(((unsigned int) _hex_value[(unsigned char) (*src)]) != 99))
315 return FALSE0;
316
317 len = hex_value(*src++)((unsigned int) _hex_value[(unsigned char) (*src++)]);
318
319 if (len == 0)
320 len = 16;
321 while (len--)
322 {
323 if (!ISHEX (*src)(((unsigned int) _hex_value[(unsigned char) (*src)]) != 99))
324 return FALSE0;
325 value = value << 4 | hex_value(*src++)((unsigned int) _hex_value[(unsigned char) (*src++)]);
326 }
327 *srcp = src;
328 *valuep = value;
329 return TRUE1;
330}
331
332static bfd_boolean
333getsym (char *dstp, char **srcp, unsigned int *lenp)
334{
335 char *src = *srcp;
336 unsigned int i;
337 unsigned int len;
338
339 if (!ISHEX (*src)(((unsigned int) _hex_value[(unsigned char) (*src)]) != 99))
340 return FALSE0;
341
342 len = hex_value(*src++)((unsigned int) _hex_value[(unsigned char) (*src++)]);
343 if (len == 0)
344 len = 16;
345 for (i = 0; i < len; i++)
346 dstp[i] = src[i];
347 dstp[i] = 0;
348 *srcp = src + i;
349 *lenp = len;
350 return TRUE1;
351}
352
353static struct data_struct *
354find_chunk (abfd, vma)
355 bfd *abfd;
356 bfd_vma vma;
357{
358 struct data_struct *d = abfd->tdata.tekhex_data->data;
359
360 vma &= ~CHUNK_MASK0x1fff;
361 while (d && (d->vma) != vma)
362 {
363 d = d->next;
364 }
365 if (!d)
366 {
367 /* No chunk for this address, so make one up */
368 d = ((struct data_struct *)
369 bfd_zalloc (abfd, (bfd_size_type) sizeof (struct data_struct)));
370
371 if (!d)
372 return NULL((void*)0);
373
374 d->next = abfd->tdata.tekhex_data->data;
375 d->vma = vma;
376 abfd->tdata.tekhex_data->data = d;
377 }
378 return d;
379}
380
381static void
382insert_byte (abfd, value, addr)
383 bfd *abfd;
384 int value;
385 bfd_vma addr;
386{
387 /* Find the chunk that this byte needs and put it in */
388 struct data_struct *d = find_chunk (abfd, addr);
389
390 d->chunk_data[addr & CHUNK_MASK0x1fff] = value;
391 d->chunk_init[addr & CHUNK_MASK0x1fff] = 1;
392}
393
394/* The first pass is to find the names of all the sections, and see
395 how big the data is */
396static bfd_boolean
397first_phase (abfd, type, src)
398 bfd *abfd;
399 int type;
400 char *src;
401{
402 asection *section = bfd_abs_section_ptr((asection *) &bfd_abs_section);
403 unsigned int len;
404 bfd_vma val;
405 char sym[17]; /* A symbol can only be 16chars long */
406
407 switch (type)
408 {
409 case '6':
410 /* Data record - read it and store it */
411 {
412 bfd_vma addr;
413
414 if (!getvalue (&src, &addr))
415 return FALSE0;
416
417 while (*src)
418 {
419 insert_byte (abfd, HEX (src)((((unsigned int) _hex_value[(unsigned char) ((src)[0])])<<
4) + ((unsigned int) _hex_value[(unsigned char) ((src)[1])]))
, addr);
420 src += 2;
421 addr++;
422 }
423 }
424
425 return TRUE1;
426 case '3':
427 /* Symbol record, read the segment */
428 if (!getsym (sym, &src, &len))
429 return FALSE0;
430 section = bfd_get_section_by_name (abfd, sym);
431 if (section == (asection *) NULL((void*)0))
432 {
433 char *n = bfd_alloc (abfd, (bfd_size_type) len + 1);
434
435 if (!n)
436 return FALSE0;
437 memcpy (n, sym, len + 1);
438 section = bfd_make_section (abfd, n);
439 }
440 while (*src)
441 {
442 switch (*src)
443 {
444 case '1': /* section range */
445 src++;
446 if (!getvalue (&src, &section->vma))
447 return FALSE0;
448 if (!getvalue (&src, &val))
449 return FALSE0;
450 section->_raw_size = val - section->vma;
451 section->flags = SEC_HAS_CONTENTS0x200 | SEC_LOAD0x002 | SEC_ALLOC0x001;
452 break;
453 case '0':
454 case '2':
455 case '3':
456 case '4':
457 case '6':
458 case '7':
459 case '8':
460 /* Symbols, add to section */
461 {
462 bfd_size_type amt = sizeof (tekhex_symbol_type);
463 tekhex_symbol_type *new =
464 (tekhex_symbol_type *) bfd_alloc (abfd, amt);
465 char stype = (*src);
466
467 if (!new)
468 return FALSE0;
469 new->symbol.the_bfd = abfd;
470 src++;
471 abfd->symcount++;
472 abfd->flags |= HAS_SYMS0x10;
473 new->prev = abfd->tdata.tekhex_data->symbols;
474 abfd->tdata.tekhex_data->symbols = new;
475 if (!getsym (sym, &src, &len))
476 return FALSE0;
477 new->symbol.name = bfd_alloc (abfd, (bfd_size_type) len + 1);
478 if (!new->symbol.name)
479 return FALSE0;
480 memcpy ((char *) (new->symbol.name), sym, len + 1);
481 new->symbol.section = section;
482 if (stype <= '4')
483 new->symbol.flags = (BSF_GLOBAL0x02 | BSF_EXPORT0x02);
484 else
485 new->symbol.flags = BSF_LOCAL0x01;
486 if (!getvalue (&src, &val))
487 return FALSE0;
488 new->symbol.value = val - section->vma;
489 }
490 default:
491 return FALSE0;
492 }
493 }
494 }
495
496 return TRUE1;
497}
498
499/* Pass over a tekhex, calling one of the above functions on each
500 record. */
501
502static bfd_boolean
503pass_over (abfd, func)
504 bfd *abfd;
505 bfd_boolean (*func) PARAMS ((bfd *, int, char *))(bfd *, int, char *);
506{
507 unsigned int chars_on_line;
508 bfd_boolean eof = FALSE0;
509
510 /* To the front of the file */
511 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET0) != 0)
512 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils/bfd/tekhex.c", 512
, __PRETTY_FUNCTION__)
;
513 while (! eof)
514 {
515 char buffer[MAXCHUNK0xff];
516 char *src = buffer;
517 char type;
518
519 /* Find first '%' */
520 eof = (bfd_boolean) (bfd_bread (src, (bfd_size_type) 1, abfd) != 1);
521 while (*src != '%' && !eof)
522 {
523 eof = (bfd_boolean) (bfd_bread (src, (bfd_size_type) 1, abfd) != 1);
524 }
525 if (eof)
526 break;
527 src++;
528
529 /* Fetch the type and the length and the checksum */
530 if (bfd_bread (src, (bfd_size_type) 5, abfd) != 5)
531 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils/bfd/tekhex.c", 531
, __PRETTY_FUNCTION__)
; /* FIXME */
532
533 type = src[2];
534
535 if (!ISHEX (src[0])(((unsigned int) _hex_value[(unsigned char) (src[0])]) != 99) || !ISHEX (src[1])(((unsigned int) _hex_value[(unsigned char) (src[1])]) != 99))
536 break;
537
538 chars_on_line = HEX (src)((((unsigned int) _hex_value[(unsigned char) ((src)[0])])<<
4) + ((unsigned int) _hex_value[(unsigned char) ((src)[1])]))
- 5; /* Already read five char */
539
540 if (bfd_bread (src, (bfd_size_type) chars_on_line, abfd) != chars_on_line)
541 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils/bfd/tekhex.c", 541
, __PRETTY_FUNCTION__)
; /* FIXME */
542 src[chars_on_line] = 0; /* put a null at the end */
543
544 if (!func (abfd, type, src))
545 return FALSE0;
546 }
547
548 return TRUE1;
549}
550
551static long
552tekhex_canonicalize_symtab (abfd, table)
553 bfd *abfd;
554 asymbol **table;
555{
556 tekhex_symbol_type *p = abfd->tdata.tekhex_data->symbols;
557 unsigned int c = bfd_get_symcount (abfd)((abfd)->symcount);
558
559 table[c] = 0;
560 while (p)
561 {
562 table[--c] = &(p->symbol);
563 p = p->prev;
564 }
565
566 return bfd_get_symcount (abfd)((abfd)->symcount);
567}
568
569static long
570tekhex_get_symtab_upper_bound (abfd)
571 bfd *abfd;
572{
573 return (abfd->symcount + 1) * (sizeof (struct tekhex_asymbol_struct *));
574
575}
576
577static bfd_boolean
578tekhex_mkobject (abfd)
579 bfd *abfd;
580{
581 tdata_type *tdata;
582
583 tdata = (tdata_type *) bfd_alloc (abfd, (bfd_size_type) sizeof (tdata_type));
584 if (!tdata)
585 return FALSE0;
586 abfd->tdata.tekhex_data = tdata;
587 tdata->type = 1;
588 tdata->head = (tekhex_data_list_type *) NULL((void*)0);
589 tdata->symbols = (struct tekhex_symbol_struct *) NULL((void*)0);
590 tdata->data = (struct data_struct *) NULL((void*)0);
591 return TRUE1;
592}
593
594/*
595 Return TRUE if the file looks like it's in TekHex format. Just look
596 for a percent sign and some hex digits */
597
598static const bfd_target *
599tekhex_object_p (abfd)
600 bfd *abfd;
601{
602 char b[4];
603
604 tekhex_init ();
605
606 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET0) != 0
607 || bfd_bread (b, (bfd_size_type) 4, abfd) != 4)
608 return NULL((void*)0);
609
610 if (b[0] != '%' || !ISHEX (b[1])(((unsigned int) _hex_value[(unsigned char) (b[1])]) != 99) || !ISHEX (b[2])(((unsigned int) _hex_value[(unsigned char) (b[2])]) != 99) || !ISHEX (b[3])(((unsigned int) _hex_value[(unsigned char) (b[3])]) != 99))
611 return (const bfd_target *) NULL((void*)0);
612
613 tekhex_mkobject (abfd);
614
615 if (!pass_over (abfd, first_phase))
616 return NULL((void*)0);
617
618 return abfd->xvec;
619}
620
621static void
622move_section_contents (abfd, section, locationp, offset, count, get)
623 bfd *abfd;
624 asection *section;
625 const PTRvoid * locationp;
626 file_ptr offset;
627 bfd_size_type count;
628 bfd_boolean get;
629{
630 bfd_vma addr;
631 char *location = (char *) locationp;
632 bfd_vma prev_number = 1; /* Nothing can have this as a high bit*/
633 struct data_struct *d = (struct data_struct *) NULL((void*)0);
6
'd' initialized to a null pointer value
634
635 BFD_ASSERT (offset == 0){ if (!(offset == 0)) bfd_assert("/usr/src/gnu/usr.bin/binutils/bfd/tekhex.c"
,635); }
;
7
Assuming 'offset' is equal to 0
8
Taking false branch
636 for (addr = section->vma; count != 0; count--, addr++)
9
Assuming 'count' is not equal to 0
10
Loop condition is true. Entering loop body
637 {
638 /* Get high bits of address. */
639 bfd_vma chunk_number = addr & ~(bfd_vma) CHUNK_MASK0x1fff;
640 bfd_vma low_bits = addr & CHUNK_MASK0x1fff;
641
642 if (chunk_number != prev_number)
11
Assuming 'chunk_number' is equal to 'prev_number'
12
Taking false branch
643 {
644 /* Different chunk, so move pointer */
645 d = find_chunk (abfd, chunk_number);
646 }
647
648 if (get
12.1
'get' is 0
)
13
Taking false branch
649 {
650 if (d->chunk_init[low_bits])
651 {
652 *location = d->chunk_data[low_bits];
653 }
654 else
655 {
656 *location = 0;
657 }
658 }
659 else
660 {
661 d->chunk_data[low_bits] = *location;
14
Array access (via field 'chunk_data') results in a null pointer dereference
662 d->chunk_init[low_bits] = (*location != 0);
663 }
664
665 location++;
666
667 }
668
669}
670
671static bfd_boolean
672tekhex_get_section_contents (abfd, section, locationp, offset, count)
673 bfd *abfd;
674 asection *section;
675 PTRvoid * locationp;
676 file_ptr offset;
677 bfd_size_type count;
678{
679 if (section->flags & (SEC_LOAD0x002 | SEC_ALLOC0x001))
680 {
681 move_section_contents (abfd, section, locationp, offset, count, TRUE1);
682 return TRUE1;
683 }
684 else
685 return FALSE0;
686}
687
688static bfd_boolean
689tekhex_set_arch_mach (abfd, arch, machine)
690 bfd *abfd;
691 enum bfd_architecture arch;
692 unsigned long machine;
693{
694 return bfd_default_set_arch_mach (abfd, arch, machine);
695}
696
697/* we have to save up all the Tekhexords for a splurge before output,
698 */
699
700static bfd_boolean
701tekhex_set_section_contents (abfd, section, locationp, offset, bytes_to_do)
702 bfd *abfd;
703 sec_ptr section;
704 const PTRvoid * locationp;
705 file_ptr offset;
706 bfd_size_type bytes_to_do;
707{
708
709 if (! abfd->output_has_begun)
1
Assuming field 'output_has_begun' is not equal to 0
2
Taking false branch
710 {
711 /* The first time around, allocate enough sections to hold all the chunks */
712 asection *s = abfd->sections;
713 bfd_vma vma;
714
715 for (s = abfd->sections; s; s = s->next)
716 {
717 if (s->flags & SEC_LOAD0x002)
718 {
719 for (vma = s->vma & ~(bfd_vma) CHUNK_MASK0x1fff;
720 vma < s->vma + s->_raw_size;
721 vma += CHUNK_MASK0x1fff)
722 find_chunk (abfd, vma);
723 }
724 }
725
726 }
727 if (section->flags & (SEC_LOAD0x002 | SEC_ALLOC0x001))
3
Assuming the condition is true
4
Taking true branch
728 {
729 move_section_contents (abfd, section, locationp, offset, bytes_to_do,
5
Calling 'move_section_contents'
730 FALSE0);
731 return TRUE1;
732 }
733 else
734 return FALSE0;
735
736}
737
738static void
739writevalue (dst, value)
740 char **dst;
741 bfd_vma value;
742{
743 char *p = *dst;
744 int len;
745 int shift;
746
747 for (len = 8, shift = 28; shift; shift -= 4, len--)
748 {
749 if ((value >> shift) & 0xf)
750 {
751 *p++ = len + '0';
752 while (len)
753 {
754 *p++ = digs[(value >> shift) & 0xf];
755 shift -= 4;
756 len--;
757 }
758 *dst = p;
759 return;
760
761 }
762 }
763 *p++ = '1';
764 *p++ = '0';
765 *dst = p;
766}
767
768static void
769writesym (dst, sym)
770 char **dst;
771 const char *sym;
772{
773 char *p = *dst;
774 int len = (sym ? strlen (sym) : 0);
775
776 if (len >= 16)
777 {
778 *p++ = '0';
779 len = 16;
780 }
781
782 else
783 {
784 if (len == 0)
785 {
786 *p++ = '1';
787 sym = "$";
788 len = 1;
789 }
790 else
791 {
792 *p++ = digs[len];
793 }
794 }
795
796 while (len--)
797 {
798 *p++ = *sym++;
799 }
800 *dst = p;
801}
802
803static void
804out (abfd, type, start, end)
805 bfd *abfd;
806 int type;
807 char *start;
808 char *end;
809{
810 int sum = 0;
811 char *s;
812 char front[6];
813 bfd_size_type wrlen;
814
815 front[0] = '%';
816 TOHEX (front + 1, end - start + 5)(front + 1)[1] = digs[(end - start + 5) & 0xf]; (front + 1
)[0] = digs[((end - start + 5)>>4)&0xf];
;
817 front[3] = type;
818
819 for (s = start; s < end; s++)
820 {
821 sum += sum_block[(unsigned char) *s];
822 }
823
824 sum += sum_block[(unsigned char) front[1]]; /* length */
825 sum += sum_block[(unsigned char) front[2]];
826 sum += sum_block[(unsigned char) front[3]]; /* type */
827 TOHEX (front + 4, sum)(front + 4)[1] = digs[(sum) & 0xf]; (front + 4)[0] = digs
[((sum)>>4)&0xf];
;
828 if (bfd_bwrite (front, (bfd_size_type) 6, abfd) != 6)
829 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils/bfd/tekhex.c", 829
, __PRETTY_FUNCTION__)
;
830 end[0] = '\n';
831 wrlen = end - start + 1;
832 if (bfd_bwrite (start, wrlen, abfd) != wrlen)
833 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils/bfd/tekhex.c", 833
, __PRETTY_FUNCTION__)
;
834}
835
836static bfd_boolean
837tekhex_write_object_contents (abfd)
838 bfd *abfd;
839{
840 int bytes_written;
841 char buffer[100];
842 asymbol **p;
843 asection *s;
844 struct data_struct *d;
845
846 tekhex_init ();
847
848 bytes_written = 0;
849
850 /* And the raw data */
851 for (d = abfd->tdata.tekhex_data->data;
852 d != (struct data_struct *) NULL((void*)0);
853 d = d->next)
854 {
855 int low;
856
857 const int span = 32;
858 int addr;
859
860 /* Write it in blocks of 32 bytes */
861
862 for (addr = 0; addr < CHUNK_MASK0x1fff + 1; addr += span)
863 {
864 int need = 0;
865
866 /* Check to see if necessary */
867 for (low = 0; !need && low < span; low++)
868 {
869 if (d->chunk_init[addr + low])
870 need = 1;
871 }
872 if (need)
873 {
874 char *dst = buffer;
875
876 writevalue (&dst, addr + d->vma);
877 for (low = 0; low < span; low++)
878 {
879 TOHEX (dst, d->chunk_data[addr + low])(dst)[1] = digs[(d->chunk_data[addr + low]) & 0xf]; (dst
)[0] = digs[((d->chunk_data[addr + low])>>4)&0xf
];
;
880 dst += 2;
881 }
882 out (abfd, '6', buffer, dst);
883 }
884 }
885 }
886 /* write all the section headers for the sections */
887 for (s = abfd->sections; s != (asection *) NULL((void*)0); s = s->next)
888 {
889 char *dst = buffer;
890
891 writesym (&dst, s->name);
892 *dst++ = '1';
893 writevalue (&dst, s->vma);
894 writevalue (&dst, s->vma + s->_raw_size);
895 out (abfd, '3', buffer, dst);
896 }
897
898 /* And the symbols */
899 if (abfd->outsymbols)
900 {
901 for (p = abfd->outsymbols; *p; p++)
902 {
903 int section_code = bfd_decode_symclass (*p);
904
905 if (section_code != '?')
906 { /* do not include debug symbols */
907 asymbol *sym = *p;
908 char *dst = buffer;
909
910 writesym (&dst, sym->section->name);
911
912 switch (section_code)
913 {
914 case 'A':
915 *dst++ = '2';
916 break;
917 case 'a':
918 *dst++ = '6';
919 break;
920 case 'D':
921 case 'B':
922 case 'O':
923 *dst++ = '4';
924 break;
925 case 'd':
926 case 'b':
927 case 'o':
928 *dst++ = '8';
929 break;
930 case 'T':
931 *dst++ = '3';
932 break;
933 case 't':
934 *dst++ = '7';
935 break;
936 case 'C':
937 case 'U':
938 bfd_set_error (bfd_error_wrong_format);
939 return FALSE0;
940 }
941
942 writesym (&dst, sym->name);
943 writevalue (&dst, sym->value + sym->section->vma);
944 out (abfd, '3', buffer, dst);
945 }
946 }
947 }
948
949 /* And the terminator */
950 if (bfd_bwrite ("%0781010\n", (bfd_size_type) 9, abfd) != 9)
951 abort ()_bfd_abort ("/usr/src/gnu/usr.bin/binutils/bfd/tekhex.c", 951
, __PRETTY_FUNCTION__)
;
952 return TRUE1;
953}
954
955static int
956tekhex_sizeof_headers (abfd, exec)
957 bfd *abfd ATTRIBUTE_UNUSED__attribute__ ((__unused__));
958 bfd_boolean exec ATTRIBUTE_UNUSED__attribute__ ((__unused__));
959
960{
961 return 0;
962}
963
964static asymbol *
965tekhex_make_empty_symbol (abfd)
966 bfd *abfd;
967{
968 bfd_size_type amt = sizeof (struct tekhex_symbol_struct);
969 tekhex_symbol_type *new = (tekhex_symbol_type *) bfd_zalloc (abfd, amt);
970
971 if (!new)
972 return NULL((void*)0);
973 new->symbol.the_bfd = abfd;
974 new->prev = (struct tekhex_symbol_struct *) NULL((void*)0);
975 return &(new->symbol);
976}
977
978static void
979tekhex_get_symbol_info (ignore_abfd, symbol, ret)
980 bfd *ignore_abfd ATTRIBUTE_UNUSED__attribute__ ((__unused__));
981 asymbol *symbol;
982 symbol_info *ret;
983{
984 bfd_symbol_info (symbol, ret);
985}
986
987static void
988tekhex_print_symbol (abfd, filep, symbol, how)
989 bfd *abfd;
990 PTRvoid * filep;
991 asymbol *symbol;
992 bfd_print_symbol_type how;
993{
994 FILE *file = (FILE *) filep;
995
996 switch (how)
997 {
998 case bfd_print_symbol_name:
999 fprintf (file, "%s", symbol->name);
1000 break;
1001 case bfd_print_symbol_more:
1002 break;
1003
1004 case bfd_print_symbol_all:
1005 {
1006 const char *section_name = symbol->section->name;
1007
1008 bfd_print_symbol_vandf (abfd, (PTRvoid *) file, symbol);
1009
1010 fprintf (file, " %-5s %s",
1011 section_name,
1012 symbol->name);
1013 }
1014 }
1015}
1016
1017#define tekhex_close_and_cleanupbfd_true _bfd_generic_close_and_cleanupbfd_true
1018#define tekhex_bfd_free_cached_infobfd_true _bfd_generic_bfd_free_cached_infobfd_true
1019#define tekhex_new_section_hook((bfd_boolean (*) (bfd *, asection *)) bfd_true) _bfd_generic_new_section_hook((bfd_boolean (*) (bfd *, asection *)) bfd_true)
1020
1021#define tekhex_bfd_is_local_label_namebfd_generic_is_local_label_name bfd_generic_is_local_label_name
1022#define tekhex_get_lineno((alent *(*) (bfd *, asymbol *)) bfd_nullvoidptr) _bfd_nosymbols_get_lineno((alent *(*) (bfd *, asymbol *)) bfd_nullvoidptr)
1023#define tekhex_find_nearest_line((bfd_boolean (*) (bfd *, asection *, asymbol **, bfd_vma, const
char **, const char **, unsigned int *)) bfd_false)
_bfd_nosymbols_find_nearest_line((bfd_boolean (*) (bfd *, asection *, asymbol **, bfd_vma, const
char **, const char **, unsigned int *)) bfd_false)
1024#define tekhex_bfd_make_debug_symbol((asymbol *(*) (bfd *, void *, unsigned long)) bfd_nullvoidptr
)
_bfd_nosymbols_bfd_make_debug_symbol((asymbol *(*) (bfd *, void *, unsigned long)) bfd_nullvoidptr
)
1025#define tekhex_read_minisymbols_bfd_generic_read_minisymbols _bfd_generic_read_minisymbols
1026#define tekhex_minisymbol_to_symbol_bfd_generic_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
1027
1028#define tekhex_bfd_get_relocated_section_contentsbfd_generic_get_relocated_section_contents \
1029 bfd_generic_get_relocated_section_contents
1030#define tekhex_bfd_relax_sectionbfd_generic_relax_section bfd_generic_relax_section
1031#define tekhex_bfd_gc_sectionsbfd_generic_gc_sections bfd_generic_gc_sections
1032#define tekhex_bfd_merge_sectionsbfd_generic_merge_sections bfd_generic_merge_sections
1033#define tekhex_bfd_discard_groupbfd_generic_discard_group bfd_generic_discard_group
1034#define tekhex_bfd_link_hash_table_create_bfd_generic_link_hash_table_create _bfd_generic_link_hash_table_create
1035#define tekhex_bfd_link_hash_table_free_bfd_generic_link_hash_table_free _bfd_generic_link_hash_table_free
1036#define tekhex_bfd_link_add_symbols_bfd_generic_link_add_symbols _bfd_generic_link_add_symbols
1037#define tekhex_bfd_link_just_syms_bfd_generic_link_just_syms _bfd_generic_link_just_syms
1038#define tekhex_bfd_final_link_bfd_generic_final_link _bfd_generic_final_link
1039#define tekhex_bfd_link_split_section_bfd_generic_link_split_section _bfd_generic_link_split_section
1040
1041#define tekhex_get_section_contents_in_window_bfd_generic_get_section_contents_in_window \
1042 _bfd_generic_get_section_contents_in_window
1043
1044const bfd_target tekhex_vec =
1045{
1046 "tekhex", /* name */
1047 bfd_target_tekhex_flavour,
1048 BFD_ENDIAN_UNKNOWN, /* target byte order */
1049 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
1050 (EXEC_P0x02 | /* object flags */
1051 HAS_SYMS0x10 | HAS_LINENO0x04 | HAS_DEBUG0x08 | HAS_RELOC0x01 | HAS_LOCALS0x20 |
1052 WP_TEXT0x80 | D_PAGED0x100),
1053 (SEC_CODE0x020 | SEC_DATA0x040 | SEC_ROM0x080 | SEC_HAS_CONTENTS0x200
1054 | SEC_ALLOC0x001 | SEC_LOAD0x002 | SEC_RELOC0x004), /* section flags */
1055 0, /* leading underscore */
1056 ' ', /* ar_pad_char */
1057 16, /* ar_max_namelen */
1058 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1059 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1060 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1061 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1062 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1063 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1064
1065 {
1066 _bfd_dummy_target,
1067 tekhex_object_p, /* bfd_check_format */
1068 _bfd_dummy_target,
1069 _bfd_dummy_target,
1070 },
1071 {
1072 bfd_false,
1073 tekhex_mkobject,
1074 _bfd_generic_mkarchive,
1075 bfd_false,
1076 },
1077 { /* bfd_write_contents */
1078 bfd_false,
1079 tekhex_write_object_contents,
1080 _bfd_write_archive_contents,
1081 bfd_false,
1082 },
1083
1084 BFD_JUMP_TABLE_GENERIC (tekhex)bfd_true, bfd_true, ((bfd_boolean (*) (bfd *, asection *)) bfd_true
), tekhex_get_section_contents, _bfd_generic_get_section_contents_in_window
,
1085 BFD_JUMP_TABLE_COPY (_bfd_generic)((bfd_boolean (*) (bfd *, bfd *)) bfd_true), ((bfd_boolean (*
) (bfd *, bfd *)) bfd_true), ((bfd_boolean (*) (bfd *, asection
*, bfd *, asection *)) bfd_true), ((bfd_boolean (*) (bfd *, asymbol
*, bfd *, asymbol *)) bfd_true), ((bfd_boolean (*) (bfd *, flagword
)) bfd_true), ((bfd_boolean (*) (bfd *, void *)) bfd_true)
,
1086 BFD_JUMP_TABLE_CORE (_bfd_nocore)_bfd_nocore_core_file_failing_command, _bfd_nocore_core_file_failing_signal
, _bfd_nocore_core_file_matches_executable_p
,
1087 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive)bfd_false, bfd_false, ((bfd_boolean (*) (bfd *, char **, bfd_size_type
*, const char **)) bfd_false), ((void (*) (bfd *, const char
*, char *)) bfd_void), ((bfd_boolean (*) (bfd *, unsigned int
, struct orl *, unsigned int, int)) bfd_false), bfd_nullvoidptr
, ((bfd *(*) (bfd *, bfd *)) bfd_nullvoidptr), ((bfd *(*) (bfd
*, symindex)) bfd_nullvoidptr), bfd_generic_stat_arch_elt, bfd_false
,
1088 BFD_JUMP_TABLE_SYMBOLS (tekhex)tekhex_get_symtab_upper_bound, tekhex_canonicalize_symtab, tekhex_make_empty_symbol
, tekhex_print_symbol, tekhex_get_symbol_info, bfd_generic_is_local_label_name
, ((alent *(*) (bfd *, asymbol *)) bfd_nullvoidptr), ((bfd_boolean
(*) (bfd *, asection *, asymbol **, bfd_vma, const char **, const
char **, unsigned int *)) bfd_false), ((asymbol *(*) (bfd *,
void *, unsigned long)) bfd_nullvoidptr), _bfd_generic_read_minisymbols
, _bfd_generic_minisymbol_to_symbol
,
1089 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs)((long (*) (bfd *, asection *)) _bfd_n1), ((long (*) (bfd *, asection
*, arelent **, asymbol **)) _bfd_n1), ((reloc_howto_type *(*
) (bfd *, bfd_reloc_code_real_type)) bfd_nullvoidptr)
,
1090 BFD_JUMP_TABLE_WRITE (tekhex)tekhex_set_arch_mach, tekhex_set_section_contents,
1091 BFD_JUMP_TABLE_LINK (tekhex)tekhex_sizeof_headers, bfd_generic_get_relocated_section_contents
, bfd_generic_relax_section, _bfd_generic_link_hash_table_create
, _bfd_generic_link_hash_table_free, _bfd_generic_link_add_symbols
, _bfd_generic_link_just_syms, _bfd_generic_final_link, _bfd_generic_link_split_section
, bfd_generic_gc_sections, bfd_generic_merge_sections, bfd_generic_discard_group
,
1092 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic)_bfd_n1, ((long (*) (bfd *, asymbol **)) _bfd_n1), _bfd_n1, (
(long (*) (bfd *, arelent **, asymbol **)) _bfd_n1)
,
1093
1094 NULL((void*)0),
1095
1096 (PTRvoid *) 0
1097};