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