83808ee89699d4fe2d5be834dd3f899bb4853029
[external/binutils.git] / bfd / ihex.c
1 /* BFD back-end for Intel Hex objects.
2    Copyright 1995, 1996, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Written by Ian Lance Taylor of Cygnus Support <ian@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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* This is what Intel Hex files look like:
23
24 1. INTEL FORMATS
25
26 A. Intel 1
27
28    16-bit address-field format, for files 64k bytes in length or less.
29
30    DATA RECORD
31    Byte 1       Header = colon(:)
32    2..3         The number of data bytes in hex notation
33    4..5         High byte of the record load address
34    6..7         Low byte of the record load address
35    8..9         Record type, must be "00"
36    10..x        Data bytes in hex notation:
37         x = (number of bytes - 1) * 2 + 11
38    x+1..x+2     Checksum in hex notation
39    x+3..x+4     Carriage return, line feed
40
41    END RECORD
42    Byte 1       Header = colon (:)
43    2..3         The byte count, must be "00"
44    4..7         Transfer-address (usually "0000")
45                 the jump-to address, execution start address
46    8..9         Record type, must be "01"
47    10..11       Checksum, in hex notation
48    12..13       Carriage return, line feed
49
50 B. INTEL 2
51
52    MCS-86 format, using a 20-bit address for files larger than 64K bytes.
53
54    DATA RECORD
55    Byte 1       Header = colon (:)
56    2..3         The byte count of this record, hex notation
57    4..5         High byte of the record load address
58    6..7         Low byte of the record load address
59    8..9         Record type, must be "00"
60    10..x        The data bytes in hex notation:
61         x = (number of data bytes - 1) * 2 + 11
62    x+1..x+2     Checksum in hex notation
63    x+3..x+4     Carriage return, line feed
64
65    EXTENDED ADDRESS RECORD
66    Byte 1       Header = colon(:)
67    2..3         The byte count, must be "02"
68    4..7         Load address, must be "0000"
69    8..9         Record type, must be "02"
70    10..11       High byte of the offset address
71    12..13       Low byte of the offset address
72    14..15       Checksum in hex notation
73    16..17       Carriage return, line feed
74
75    The checksums are the two's complement of the 8-bit sum
76    without carry of the byte count, offset address, and the
77    record type.
78
79    START ADDRESS RECORD
80    Byte 1       Header = colon (:)
81    2..3         The byte count, must be "04"
82    4..7         Load address, must be "0000"
83    8..9         Record type, must be "03"
84    10..13       8086 CS value
85    14..17       8086 IP value
86    18..19       Checksum in hex notation
87    20..21       Carriage return, line feed
88
89 Another document reports these additional types:
90
91    EXTENDED LINEAR ADDRESS RECORD
92    Byte 1       Header = colon (:)
93    2..3         The byte count, must be "02"
94    4..7         Load address, must be "0000"
95    8..9         Record type, must be "04"
96    10..13       Upper 16 bits of address of subsequent records
97    14..15       Checksum in hex notation
98    16..17       Carriage return, line feed
99
100    START LINEAR ADDRESS RECORD
101    Byte 1       Header = colon (:)
102    2..3         The byte count, must be "02"
103    4..7         Load address, must be "0000"
104    8..9         Record type, must be "05"
105    10..13       Upper 16 bits of start address
106    14..15       Checksum in hex notation
107    16..17       Carriage return, line feed
108
109 The MRI compiler uses this, which is a repeat of type 5:
110
111   EXTENDED START RECORD
112    Byte 1       Header = colon (:)
113    2..3         The byte count, must be "04"
114    4..7         Load address, must be "0000"
115    8..9         Record type, must be "05"
116    10..13       Upper 16 bits of start address
117    14..17       Lower 16 bits of start address
118    18..19       Checksum in hex notation
119    20..21       Carriage return, line feed
120 */
121
122 #include "bfd.h"
123 #include "sysdep.h"
124 #include "libbfd.h"
125 #include "libiberty.h"
126
127 #include <ctype.h>
128
129 static void ihex_init PARAMS ((void));
130 static boolean ihex_mkobject PARAMS ((bfd *));
131 static INLINE int ihex_get_byte PARAMS ((bfd *, boolean *));
132 static void ihex_bad_byte PARAMS ((bfd *, unsigned int, int, boolean));
133 static boolean ihex_scan PARAMS ((bfd *));
134 static const bfd_target *ihex_object_p PARAMS ((bfd *));
135 static boolean ihex_read_section PARAMS ((bfd *, asection *, bfd_byte *));
136 static boolean ihex_get_section_contents
137   PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
138 static boolean ihex_set_section_contents
139   PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
140 static boolean ihex_write_record
141   PARAMS ((bfd *, size_t, unsigned int, unsigned int, bfd_byte *));
142 static boolean ihex_write_object_contents PARAMS ((bfd *));
143 static asymbol *ihex_make_empty_symbol PARAMS ((bfd *));
144 static boolean ihex_set_arch_mach
145   PARAMS ((bfd *, enum bfd_architecture, unsigned long));
146 static int ihex_sizeof_headers PARAMS ((bfd *, boolean));
147
148 /* The number of bytes we put on one line during output.  */
149
150 #define CHUNK 16
151
152 /* Macros for converting between hex and binary. */
153
154 #define NIBBLE(x) (hex_value (x))
155 #define HEX2(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1]))
156 #define HEX4(buffer) ((HEX2 (buffer) << 8) + HEX2 ((buffer) + 2))
157 #define ISHEX(x) (hex_p (x))
158
159 /* When we write out an ihex value, the values can not be output as
160    they are seen.  Instead, we hold them in memory in this structure.  */
161
162 struct ihex_data_list
163 {
164   struct ihex_data_list *next;
165   bfd_byte *data;
166   bfd_vma where;
167   bfd_size_type size;
168 };
169
170 /* The ihex tdata information.  */
171
172 struct ihex_data_struct
173 {
174   struct ihex_data_list *head;
175   struct ihex_data_list *tail;
176 };
177
178 /* Initialize by filling in the hex conversion array.  */
179
180 static void
181 ihex_init ()
182 {
183   static boolean inited;
184
185   if (! inited)
186     {
187       inited = true;
188       hex_init ();
189     }
190 }
191
192 /* Create an ihex object.  */
193
194 static boolean
195 ihex_mkobject (abfd)
196      bfd *abfd;
197 {
198   if (abfd->tdata.ihex_data == NULL)
199     {
200       struct ihex_data_struct *tdata;
201       bfd_size_type amt = sizeof (struct ihex_data_struct);
202
203       tdata = (struct ihex_data_struct *) bfd_alloc (abfd, amt);
204       if (tdata == NULL)
205         return false;
206       abfd->tdata.ihex_data = tdata;
207       tdata->head = NULL;
208       tdata->tail = NULL;
209     }
210
211   return true;
212 }
213
214 /* Read a byte from a BFD.  Set *ERRORPTR if an error occurred.
215    Return EOF on error or end of file.  */
216
217 static INLINE int
218 ihex_get_byte (abfd, errorptr)
219      bfd *abfd;
220      boolean *errorptr;
221 {
222   bfd_byte c;
223
224   if (bfd_bread (&c, (bfd_size_type) 1, abfd) != 1)
225     {
226       if (bfd_get_error () != bfd_error_file_truncated)
227         *errorptr = true;
228       return EOF;
229     }
230
231   return (int) (c & 0xff);
232 }
233
234 /* Report a problem in an Intel Hex file.  */
235
236 static void
237 ihex_bad_byte (abfd, lineno, c, error)
238      bfd *abfd;
239      unsigned int lineno;
240      int c;
241      boolean error;
242 {
243   if (c == EOF)
244     {
245       if (! error)
246         bfd_set_error (bfd_error_file_truncated);
247     }
248   else
249     {
250       char buf[10];
251
252       if (! isprint (c))
253         sprintf (buf, "\\%03o", (unsigned int) c);
254       else
255         {
256           buf[0] = c;
257           buf[1] = '\0';
258         }
259       (*_bfd_error_handler)
260         (_("%s:%d: unexpected character `%s' in Intel Hex file\n"),
261          bfd_get_filename (abfd), lineno, buf);
262       bfd_set_error (bfd_error_bad_value);
263     }
264 }
265
266 /* Read an Intel hex file and turn it into sections.  We create a new
267    section for each contiguous set of bytes.  */
268
269 static boolean
270 ihex_scan (abfd)
271      bfd *abfd;
272 {
273   bfd_vma segbase;
274   bfd_vma extbase;
275   asection *sec;
276   unsigned int lineno;
277   boolean error;
278   bfd_byte *buf = NULL;
279   size_t bufsize;
280   int c;
281
282   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
283     goto error_return;
284
285   abfd->start_address = 0;
286
287   segbase = 0;
288   extbase = 0;
289   sec = NULL;
290   lineno = 1;
291   error = false;
292   bufsize = 0;
293   while ((c = ihex_get_byte (abfd, &error)) != EOF)
294     {
295       if (c == '\r')
296         continue;
297       else if (c == '\n')
298         {
299           ++lineno;
300           continue;
301         }
302       else if (c != ':')
303         {
304           ihex_bad_byte (abfd, lineno, c, error);
305           goto error_return;
306         }
307       else
308         {
309           file_ptr pos;
310           char hdr[8];
311           unsigned int i;
312           unsigned int len;
313           bfd_vma addr;
314           unsigned int type;
315           unsigned int chars;
316           unsigned int chksum;
317
318           /* This is a data record.  */
319
320           pos = bfd_tell (abfd) - 1;
321
322           /* Read the header bytes.  */
323
324           if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8)
325             goto error_return;
326
327           for (i = 0; i < 8; i++)
328             {
329               if (! ISHEX (hdr[i]))
330                 {
331                   ihex_bad_byte (abfd, lineno, hdr[i], error);
332                   goto error_return;
333                 }
334             }
335
336           len = HEX2 (hdr);
337           addr = HEX4 (hdr + 2);
338           type = HEX2 (hdr + 6);
339
340           /* Read the data bytes.  */
341
342           chars = len * 2 + 2;
343           if (chars >= bufsize)
344             {
345               buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) chars);
346               if (buf == NULL)
347                 goto error_return;
348               bufsize = chars;
349             }
350
351           if (bfd_bread (buf, (bfd_size_type) chars, abfd) != chars)
352             goto error_return;
353
354           for (i = 0; i < chars; i++)
355             {
356               if (! ISHEX (buf[i]))
357                 {
358                   ihex_bad_byte (abfd, lineno, hdr[i], error);
359                   goto error_return;
360                 }
361             }
362
363           /* Check the checksum.  */
364           chksum = len + addr + (addr >> 8) + type;
365           for (i = 0; i < len; i++)
366             chksum += HEX2 (buf + 2 * i);
367           if (((- chksum) & 0xff) != (unsigned int) HEX2 (buf + 2 * i))
368             {
369               (*_bfd_error_handler)
370                 (_("%s:%u: bad checksum in Intel Hex file (expected %u, found %u)"),
371                  bfd_get_filename (abfd), lineno,
372                  (- chksum) & 0xff, (unsigned int) HEX2 (buf + 2 * i));
373               bfd_set_error (bfd_error_bad_value);
374               goto error_return;
375             }
376
377           switch (type)
378             {
379             case 0:
380               /* This is a data record.  */
381               if (sec != NULL
382                   && sec->vma + sec->_raw_size == extbase + segbase + addr)
383                 {
384                   /* This data goes at the end of the section we are
385                      currently building.  */
386                   sec->_raw_size += len;
387                 }
388               else if (len > 0)
389                 {
390                   char secbuf[20];
391                   char *secname;
392                   bfd_size_type amt;
393
394                   sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
395                   amt = strlen (secbuf) + 1;
396                   secname = (char *) bfd_alloc (abfd, amt);
397                   if (secname == NULL)
398                     goto error_return;
399                   strcpy (secname, secbuf);
400                   sec = bfd_make_section (abfd, secname);
401                   if (sec == NULL)
402                     goto error_return;
403                   sec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
404                   sec->vma = extbase + segbase + addr;
405                   sec->lma = extbase + segbase + addr;
406                   sec->_raw_size = len;
407                   sec->filepos = pos;
408                 }
409               break;
410
411             case 1:
412               /* An end record.  */
413               if (abfd->start_address == 0)
414                 abfd->start_address = addr;
415               if (buf != NULL)
416                 free (buf);
417               return true;
418
419             case 2:
420               /* An extended address record.  */
421               if (len != 2)
422                 {
423                   (*_bfd_error_handler)
424                     (_("%s:%u: bad extended address record length in Intel Hex file"),
425                      bfd_get_filename (abfd), lineno);
426                   bfd_set_error (bfd_error_bad_value);
427                   goto error_return;
428                 }
429
430               segbase = HEX4 (buf) << 4;
431
432               sec = NULL;
433
434               break;
435
436             case 3:
437               /* An extended start address record.  */
438               if (len != 4)
439                 {
440                   (*_bfd_error_handler)
441                     (_("%s:%u: bad extended start address length in Intel Hex file"),
442                      bfd_get_filename (abfd), lineno);
443                   bfd_set_error (bfd_error_bad_value);
444                   goto error_return;
445                 }
446
447               abfd->start_address += (HEX4 (buf) << 4) + HEX4 (buf + 4);
448
449               sec = NULL;
450
451               break;
452
453             case 4:
454               /* An extended linear address record.  */
455               if (len != 2)
456                 {
457                   (*_bfd_error_handler)
458                     (_("%s:%u: bad extended linear address record length in Intel Hex file"),
459                      bfd_get_filename (abfd), lineno);
460                   bfd_set_error (bfd_error_bad_value);
461                   goto error_return;
462                 }
463
464               extbase = HEX4 (buf) << 16;
465
466               sec = NULL;
467
468               break;
469
470             case 5:
471               /* An extended linear start address record.  */
472               if (len != 2 && len != 4)
473                 {
474                   (*_bfd_error_handler)
475                     (_("%s:%u: bad extended linear start address length in Intel Hex file"),
476                      bfd_get_filename (abfd), lineno);
477                   bfd_set_error (bfd_error_bad_value);
478                   goto error_return;
479                 }
480
481               if (len == 2)
482                 abfd->start_address += HEX4 (buf) << 16;
483               else
484                 abfd->start_address = (HEX4 (buf) << 16) + HEX4 (buf + 4);
485
486               sec = NULL;
487
488               break;
489
490             default:
491               (*_bfd_error_handler)
492                 (_("%s:%u: unrecognized ihex type %u in Intel Hex file\n"),
493                  bfd_get_filename (abfd), lineno, type);
494               bfd_set_error (bfd_error_bad_value);
495               goto error_return;
496             }
497         }
498     }
499
500   if (error)
501     goto error_return;
502
503   if (buf != NULL)
504     free (buf);
505
506   return true;
507
508  error_return:
509   if (buf != NULL)
510     free (buf);
511   return false;
512 }
513
514 /* Try to recognize an Intel Hex file.  */
515
516 static const bfd_target *
517 ihex_object_p (abfd)
518      bfd *abfd;
519 {
520   bfd_byte b[9];
521   unsigned int i;
522   unsigned int type;
523
524   ihex_init ();
525
526   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
527     return NULL;
528   if (bfd_bread (b, (bfd_size_type) 9, abfd) != 9)
529     {
530       if (bfd_get_error () == bfd_error_file_truncated)
531         bfd_set_error (bfd_error_wrong_format);
532       return NULL;
533     }
534
535   if (b[0] != ':')
536     {
537       bfd_set_error (bfd_error_wrong_format);
538       return NULL;
539     }
540
541   for (i = 1; i < 9; i++)
542     {
543       if (! ISHEX (b[i]))
544         {
545           bfd_set_error (bfd_error_wrong_format);
546           return NULL;
547         }
548     }
549
550   type = HEX2 (b + 7);
551   if (type > 5)
552     {
553       bfd_set_error (bfd_error_wrong_format);
554       return NULL;
555     }
556
557   /* OK, it looks like it really is an Intel Hex file.  */
558
559   if (! ihex_mkobject (abfd)
560       || ! ihex_scan (abfd))
561     return NULL;
562
563   return abfd->xvec;
564 }
565
566 /* Read the contents of a section in an Intel Hex file.  */
567
568 static boolean
569 ihex_read_section (abfd, section, contents)
570      bfd *abfd;
571      asection *section;
572      bfd_byte *contents;
573 {
574   int c;
575   bfd_byte *p;
576   bfd_byte *buf = NULL;
577   size_t bufsize;
578   boolean error;
579
580   if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0)
581     goto error_return;
582
583   p = contents;
584   bufsize = 0;
585   error = false;
586   while ((c = ihex_get_byte (abfd, &error)) != EOF)
587     {
588       char hdr[8];
589       unsigned int len;
590       bfd_vma addr;
591       unsigned int type;
592       unsigned int i;
593
594       if (c == '\r' || c == '\n')
595         continue;
596
597       /* This is called after ihex_scan has succeeded, so we ought to
598          know the exact format.  */
599       BFD_ASSERT (c == ':');
600
601       if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8)
602         goto error_return;
603
604       len = HEX2 (hdr);
605       addr = HEX4 (hdr + 2);
606       type = HEX2 (hdr + 6);
607
608       /* We should only see type 0 records here.  */
609       if (type != 0)
610         {
611           (*_bfd_error_handler)
612             (_("%s: internal error in ihex_read_section"),
613              bfd_get_filename (abfd));
614           bfd_set_error (bfd_error_bad_value);
615           goto error_return;
616         }
617
618       if (len * 2 > bufsize)
619         {
620           buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) len * 2);
621           if (buf == NULL)
622             goto error_return;
623           bufsize = len * 2;
624         }
625
626       if (bfd_bread (buf, (bfd_size_type) len * 2, abfd) != len * 2)
627         goto error_return;
628
629       for (i = 0; i < len; i++)
630         *p++ = HEX2 (buf + 2 * i);
631       if ((bfd_size_type) (p - contents) >= section->_raw_size)
632         {
633           /* We've read everything in the section.  */
634           if (buf != NULL)
635             free (buf);
636           return true;
637         }
638
639       /* Skip the checksum.  */
640       if (bfd_bread (buf, (bfd_size_type) 2, abfd) != 2)
641         goto error_return;
642     }
643
644   if ((bfd_size_type) (p - contents) < section->_raw_size)
645     {
646       (*_bfd_error_handler)
647         (_("%s: bad section length in ihex_read_section"),
648          bfd_get_filename (abfd));
649       bfd_set_error (bfd_error_bad_value);
650       goto error_return;
651     }
652
653   if (buf != NULL)
654     free (buf);
655
656   return true;
657
658  error_return:
659   if (buf != NULL)
660     free (buf);
661   return false;
662 }
663
664 /* Get the contents of a section in an Intel Hex file.  */
665
666 static boolean
667 ihex_get_section_contents (abfd, section, location, offset, count)
668      bfd *abfd;
669      asection *section;
670      PTR location;
671      file_ptr offset;
672      bfd_size_type count;
673 {
674   if (section->used_by_bfd == NULL)
675     {
676       section->used_by_bfd = bfd_alloc (abfd, section->_raw_size);
677       if (section->used_by_bfd == NULL)
678         return false;
679       if (! ihex_read_section (abfd, section, section->used_by_bfd))
680         return false;
681     }
682
683   memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
684           (size_t) count);
685
686   return true;
687 }
688
689 /* Set the contents of a section in an Intel Hex file.  */
690
691 static boolean
692 ihex_set_section_contents (abfd, section, location, offset, count)
693      bfd *abfd;
694      asection *section;
695      PTR location;
696      file_ptr offset;
697      bfd_size_type count;
698 {
699   struct ihex_data_list *n;
700   bfd_byte *data;
701   struct ihex_data_struct *tdata;
702   bfd_size_type amt;
703
704   if (count == 0
705       || (section->flags & SEC_ALLOC) == 0
706       || (section->flags & SEC_LOAD) == 0)
707     return true;
708
709   amt = sizeof (struct ihex_data_list);
710   n = (struct ihex_data_list *) bfd_alloc (abfd, amt);
711   if (n == NULL)
712     return false;
713
714   data = (bfd_byte *) bfd_alloc (abfd, count);
715   if (data == NULL)
716     return false;
717   memcpy (data, location, (size_t) count);
718
719   n->data = data;
720   n->where = section->lma + offset;
721   n->size = count;
722
723   /* Sort the records by address.  Optimize for the common case of
724      adding a record to the end of the list.  */
725   tdata = abfd->tdata.ihex_data;
726   if (tdata->tail != NULL
727       && n->where >= tdata->tail->where)
728     {
729       tdata->tail->next = n;
730       n->next = NULL;
731       tdata->tail = n;
732     }
733   else
734     {
735       register struct ihex_data_list **pp;
736
737       for (pp = &tdata->head;
738            *pp != NULL && (*pp)->where < n->where;
739            pp = &(*pp)->next)
740         ;
741       n->next = *pp;
742       *pp = n;
743       if (n->next == NULL)
744         tdata->tail = n;
745     }
746
747   return true;
748 }
749
750 /* Write a record out to an Intel Hex file.  */
751
752 static boolean
753 ihex_write_record (abfd, count, addr, type, data)
754      bfd *abfd;
755      size_t count;
756      unsigned int addr;
757      unsigned int type;
758      bfd_byte *data;
759 {
760   static const char digs[] = "0123456789ABCDEF";
761   char buf[9 + CHUNK * 2 + 4];
762   char *p;
763   unsigned int chksum;
764   unsigned int i;
765   size_t total;
766
767 #define TOHEX(buf, v) \
768   ((buf)[0] = digs[((v) >> 4) & 0xf], (buf)[1] = digs[(v) & 0xf])
769
770   buf[0] = ':';
771   TOHEX (buf + 1, count);
772   TOHEX (buf + 3, (addr >> 8) & 0xff);
773   TOHEX (buf + 5, addr & 0xff);
774   TOHEX (buf + 7, type);
775
776   chksum = count + addr + (addr >> 8) + type;
777
778   for (i = 0, p = buf + 9; i < count; i++, p += 2, data++)
779     {
780       TOHEX (p, *data);
781       chksum += *data;
782     }
783
784   TOHEX (p, (- chksum) & 0xff);
785   p[2] = '\r';
786   p[3] = '\n';
787
788   total = 9 + count * 2 + 4;
789   if (bfd_bwrite (buf, (bfd_size_type) total, abfd) != total)
790     return false;
791
792   return true;
793 }
794
795 /* Write out an Intel Hex file.  */
796
797 static boolean
798 ihex_write_object_contents (abfd)
799      bfd *abfd;
800 {
801   bfd_vma segbase;
802   bfd_vma extbase;
803   struct ihex_data_list *l;
804
805   segbase = 0;
806   extbase = 0;
807   for (l = abfd->tdata.ihex_data->head; l != NULL; l = l->next)
808     {
809       bfd_vma where;
810       bfd_byte *p;
811       bfd_size_type count;
812
813       where = l->where;
814       p = l->data;
815       count = l->size;
816       while (count > 0)
817         {
818           size_t now;
819           unsigned int rec_addr;
820
821           now = count;
822           if (count > CHUNK)
823             now = CHUNK;
824
825           if (where > segbase + extbase + 0xffff)
826             {
827               bfd_byte addr[2];
828
829               /* We need a new base address.  */
830               if (where <= 0xfffff)
831                 {
832                   /* The addresses should be sorted.  */
833                   BFD_ASSERT (extbase == 0);
834
835                   segbase = where & 0xf0000;
836                   addr[0] = (bfd_byte)(segbase >> 12) & 0xff;
837                   addr[1] = (bfd_byte)(segbase >> 4) & 0xff;
838                   if (! ihex_write_record (abfd, 2, 0, 2, addr))
839                     return false;
840                 }
841               else
842                 {
843                   /* The extended address record and the extended
844                      linear address record are combined, at least by
845                      some readers.  We need an extended linear address
846                      record here, so if we've already written out an
847                      extended address record, zero it out to avoid
848                      confusion.  */
849                   if (segbase != 0)
850                     {
851                       addr[0] = 0;
852                       addr[1] = 0;
853                       if (! ihex_write_record (abfd, 2, 0, 2, addr))
854                         return false;
855                       segbase = 0;
856                     }
857
858                   extbase = where & 0xffff0000;
859                   if (where > extbase + 0xffff)
860                     {
861                       char buf[20];
862
863                       sprintf_vma (buf, where);
864                       (*_bfd_error_handler)
865                         (_("%s: address 0x%s out of range for Intex Hex file"),
866                          bfd_get_filename (abfd), buf);
867                       bfd_set_error (bfd_error_bad_value);
868                       return false;
869                     }
870                   addr[0] = (bfd_byte)(extbase >> 24) & 0xff;
871                   addr[1] = (bfd_byte)(extbase >> 16) & 0xff;
872                   if (! ihex_write_record (abfd, 2, 0, 4, addr))
873                     return false;
874                 }
875             }
876
877           rec_addr = where - (extbase + segbase);
878           if (! ihex_write_record (abfd, now, rec_addr, 0, p))
879             return false;
880
881           where += now;
882           p += now;
883           count -= now;
884         }
885     }
886
887   if (abfd->start_address != 0)
888     {
889       bfd_vma start;
890       bfd_byte startbuf[4];
891
892       start = abfd->start_address;
893
894       if (start <= 0xfffff)
895         {
896           startbuf[0] = (bfd_byte)((start & 0xf0000) >> 12) & 0xff;
897           startbuf[1] = 0;
898           startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
899           startbuf[3] = (bfd_byte)start & 0xff;
900           if (! ihex_write_record (abfd, 4, 0, 3, startbuf))
901             return false;
902         }
903       else
904         {
905           startbuf[0] = (bfd_byte)(start >> 24) & 0xff;
906           startbuf[1] = (bfd_byte)(start >> 16) & 0xff;
907           startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
908           startbuf[3] = (bfd_byte)start & 0xff;
909           if (! ihex_write_record (abfd, 4, 0, 5, startbuf))
910             return false;
911         }
912     }
913
914   if (! ihex_write_record (abfd, 0, 0, 1, NULL))
915     return false;
916
917   return true;
918 }
919
920 /* Make an empty symbol.  This is required only because
921    bfd_make_section_anyway wants to create a symbol for the section.  */
922
923 static asymbol *
924 ihex_make_empty_symbol (abfd)
925      bfd *abfd;
926 {
927   asymbol *new;
928
929   new = (asymbol *) bfd_zalloc (abfd, (bfd_size_type) sizeof (asymbol));
930   if (new != NULL)
931     new->the_bfd = abfd;
932   return new;
933 }
934
935 /* Set the architecture for the output file.  The architecture is
936    irrelevant, so we ignore errors about unknown architectures.  */
937
938 static boolean
939 ihex_set_arch_mach (abfd, arch, mach)
940      bfd *abfd;
941      enum bfd_architecture arch;
942      unsigned long mach;
943 {
944   if (! bfd_default_set_arch_mach (abfd, arch, mach))
945     {
946       if (arch != bfd_arch_unknown)
947         return false;
948     }
949   return true;
950 }
951
952 /* Get the size of the headers, for the linker.  */
953
954 /*ARGSUSED*/
955 static int
956 ihex_sizeof_headers (abfd, exec)
957      bfd *abfd ATTRIBUTE_UNUSED;
958      boolean exec ATTRIBUTE_UNUSED;
959 {
960   return 0;
961 }
962
963 /* Some random definitions for the target vector.  */
964
965 #define ihex_close_and_cleanup _bfd_generic_close_and_cleanup
966 #define ihex_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
967 #define ihex_new_section_hook _bfd_generic_new_section_hook
968 #define ihex_get_section_contents_in_window \
969   _bfd_generic_get_section_contents_in_window
970
971 #define ihex_get_symtab_upper_bound bfd_0l
972 #define ihex_get_symtab \
973   ((long (*) PARAMS ((bfd *, asymbol **))) bfd_0l)
974 #define ihex_print_symbol _bfd_nosymbols_print_symbol
975 #define ihex_get_symbol_info _bfd_nosymbols_get_symbol_info
976 #define ihex_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
977 #define ihex_get_lineno _bfd_nosymbols_get_lineno
978 #define ihex_find_nearest_line _bfd_nosymbols_find_nearest_line
979 #define ihex_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
980 #define ihex_read_minisymbols _bfd_nosymbols_read_minisymbols
981 #define ihex_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
982
983 #define ihex_get_reloc_upper_bound \
984   ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
985 #define ihex_canonicalize_reloc \
986   ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
987 #define ihex_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
988
989 #define ihex_bfd_get_relocated_section_contents \
990   bfd_generic_get_relocated_section_contents
991 #define ihex_bfd_relax_section bfd_generic_relax_section
992 #define ihex_bfd_gc_sections bfd_generic_gc_sections
993 #define ihex_bfd_merge_sections bfd_generic_merge_sections
994 #define ihex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
995 #define ihex_bfd_link_add_symbols _bfd_generic_link_add_symbols
996 #define ihex_bfd_final_link _bfd_generic_final_link
997 #define ihex_bfd_link_split_section _bfd_generic_link_split_section
998
999 /* The Intel Hex target vector.  */
1000
1001 const bfd_target ihex_vec =
1002 {
1003   "ihex",                       /* name */
1004   bfd_target_ihex_flavour,
1005   BFD_ENDIAN_UNKNOWN,           /* target byte order */
1006   BFD_ENDIAN_UNKNOWN,           /* target headers byte order */
1007   0,                            /* object flags */
1008   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD),    /* section flags */
1009   0,                            /* leading underscore */
1010   ' ',                          /* ar_pad_char */
1011   16,                           /* ar_max_namelen */
1012   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1013   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1014   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
1015   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1016   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1017   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
1018
1019   {
1020     _bfd_dummy_target,
1021     ihex_object_p,              /* bfd_check_format */
1022     _bfd_dummy_target,
1023     _bfd_dummy_target,
1024   },
1025   {
1026     bfd_false,
1027     ihex_mkobject,
1028     _bfd_generic_mkarchive,
1029     bfd_false,
1030   },
1031   {                             /* bfd_write_contents */
1032     bfd_false,
1033     ihex_write_object_contents,
1034     _bfd_write_archive_contents,
1035     bfd_false,
1036   },
1037
1038   BFD_JUMP_TABLE_GENERIC (ihex),
1039   BFD_JUMP_TABLE_COPY (_bfd_generic),
1040   BFD_JUMP_TABLE_CORE (_bfd_nocore),
1041   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1042   BFD_JUMP_TABLE_SYMBOLS (ihex),
1043   BFD_JUMP_TABLE_RELOCS (ihex),
1044   BFD_JUMP_TABLE_WRITE (ihex),
1045   BFD_JUMP_TABLE_LINK (ihex),
1046   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1047
1048   NULL,
1049
1050   (PTR) 0
1051 };