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