Made sure that every call to bfd_read, bfd_write, and bfd_seek
checks the return value and handled bfd_error correctly. These changes are not itemised. Also: * srec.c (srec_write_record): Change return type to boolean. (srec_write_header): Likewise. Also, check srec_write_record return value. (srec_write_section, srec_write_terminator): Likewise. (srec_write_symbols): Change return type to boolean. (internal_srec_write_object_contents): Check return value of srec_write_symbols, srec_write_header, srec_write_section, and srec_write_terminator.
This commit is contained in:
parent
5f322b5eb0
commit
6d4f771502
1 changed files with 64 additions and 35 deletions
91
bfd/srec.c
91
bfd/srec.c
|
@ -107,6 +107,12 @@ DESCRIPTION
|
||||||
#include "sysdep.h"
|
#include "sysdep.h"
|
||||||
#include "libbfd.h"
|
#include "libbfd.h"
|
||||||
|
|
||||||
|
static boolean srec_write_record PARAMS ((bfd *, int, bfd_vma,
|
||||||
|
const unsigned char *,
|
||||||
|
const unsigned char *));
|
||||||
|
static boolean srec_write_header PARAMS ((bfd *));
|
||||||
|
static boolean srec_write_symbols PARAMS ((bfd *));
|
||||||
|
|
||||||
/* Macros for converting between hex and binary. */
|
/* Macros for converting between hex and binary. */
|
||||||
|
|
||||||
static CONST char digs[] = "0123456789ABCDEF";
|
static CONST char digs[] = "0123456789ABCDEF";
|
||||||
|
@ -188,6 +194,9 @@ typedef struct srec_data_struct
|
||||||
}
|
}
|
||||||
tdata_type;
|
tdata_type;
|
||||||
|
|
||||||
|
static boolean srec_write_section PARAMS ((bfd *, tdata_type *,
|
||||||
|
srec_data_list_type *));
|
||||||
|
static boolean srec_write_terminator PARAMS ((bfd *, tdata_type *));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
called once per input S-Record, used to work out vma and size of data.
|
called once per input S-Record, used to work out vma and size of data.
|
||||||
|
@ -336,7 +345,8 @@ pass_over (abfd, func, symbolfunc, section)
|
||||||
|
|
||||||
srec_mkobject (abfd);
|
srec_mkobject (abfd);
|
||||||
/* To the front of the file */
|
/* To the front of the file */
|
||||||
bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
|
if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
|
||||||
|
abort ();
|
||||||
while (eof == false)
|
while (eof == false)
|
||||||
{
|
{
|
||||||
char buffer[MAXCHUNK];
|
char buffer[MAXCHUNK];
|
||||||
|
@ -410,7 +420,8 @@ pass_over (abfd, func, symbolfunc, section)
|
||||||
src++;
|
src++;
|
||||||
|
|
||||||
/* Fetch the type and the length */
|
/* Fetch the type and the length */
|
||||||
bfd_read (src, 1, 3, abfd);
|
if (bfd_read (src, 1, 3, abfd) != 3)
|
||||||
|
abort (); /* FIXME */
|
||||||
|
|
||||||
type = *src++;
|
type = *src++;
|
||||||
|
|
||||||
|
@ -423,7 +434,8 @@ pass_over (abfd, func, symbolfunc, section)
|
||||||
break;
|
break;
|
||||||
src += 2;
|
src += 2;
|
||||||
|
|
||||||
bfd_read (src, 1, bytes_on_line * 2, abfd);
|
if (bfd_read (src, 1, bytes_on_line * 2, abfd) != bytes_on_line * 2)
|
||||||
|
abort (); /* FIXME */
|
||||||
|
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
|
@ -487,8 +499,9 @@ srec_object_p (abfd)
|
||||||
|
|
||||||
srec_init ();
|
srec_init ();
|
||||||
|
|
||||||
bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
|
if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
|
||||||
bfd_read (b, 1, 4, abfd);
|
|| bfd_read (b, 1, 4, abfd) != 4)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (b[0] != 'S' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
|
if (b[0] != 'S' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
|
||||||
return (bfd_target *) NULL;
|
return (bfd_target *) NULL;
|
||||||
|
@ -508,8 +521,9 @@ symbolsrec_object_p (abfd)
|
||||||
|
|
||||||
srec_init ();
|
srec_init ();
|
||||||
|
|
||||||
bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
|
if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
|
||||||
bfd_read (b, 1, 4, abfd);
|
|| bfd_read (b, 1, 4, abfd) != 4)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (b[0] != '$' || b[1] != '$')
|
if (b[0] != '$' || b[1] != '$')
|
||||||
return (bfd_target *) NULL;
|
return (bfd_target *) NULL;
|
||||||
|
@ -614,15 +628,13 @@ srec_set_section_contents (abfd, section, location, offset, bytes_to_do)
|
||||||
supplied bytes and length don't have a checksum. That's worked out
|
supplied bytes and length don't have a checksum. That's worked out
|
||||||
here
|
here
|
||||||
*/
|
*/
|
||||||
static
|
static boolean
|
||||||
void
|
|
||||||
srec_write_record (abfd, type, address, data, end)
|
srec_write_record (abfd, type, address, data, end)
|
||||||
bfd *abfd;
|
bfd *abfd;
|
||||||
char type;
|
int type;
|
||||||
bfd_vma address;
|
bfd_vma address;
|
||||||
CONST unsigned char *data;
|
const unsigned char *data;
|
||||||
CONST unsigned char *end;
|
const unsigned char *end;
|
||||||
|
|
||||||
{
|
{
|
||||||
char buffer[MAXCHUNK];
|
char buffer[MAXCHUNK];
|
||||||
|
|
||||||
|
@ -673,12 +685,14 @@ srec_write_record (abfd, type, address, data, end)
|
||||||
|
|
||||||
*dst++ = '\r';
|
*dst++ = '\r';
|
||||||
*dst++ = '\n';
|
*dst++ = '\n';
|
||||||
bfd_write ((PTR) buffer, 1, dst - buffer, abfd);
|
if (bfd_write ((PTR) buffer, 1, dst - buffer, abfd) != dst - buffer)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static boolean
|
||||||
srec_write_header (abfd)
|
srec_write_header (abfd)
|
||||||
bfd *abfd;
|
bfd *abfd;
|
||||||
{
|
{
|
||||||
|
@ -691,10 +705,10 @@ srec_write_header (abfd)
|
||||||
{
|
{
|
||||||
*dst++ = abfd->filename[i];
|
*dst++ = abfd->filename[i];
|
||||||
}
|
}
|
||||||
srec_write_record (abfd, 0, 0, buffer, dst);
|
return srec_write_record (abfd, 0, 0, buffer, dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static boolean
|
||||||
srec_write_section (abfd, tdata, list)
|
srec_write_section (abfd, tdata, list)
|
||||||
bfd *abfd;
|
bfd *abfd;
|
||||||
tdata_type *tdata;
|
tdata_type *tdata;
|
||||||
|
@ -716,32 +730,34 @@ srec_write_section (abfd, tdata, list)
|
||||||
|
|
||||||
address = list->where + bytes_written;
|
address = list->where + bytes_written;
|
||||||
|
|
||||||
srec_write_record (abfd,
|
if (! srec_write_record (abfd,
|
||||||
tdata->type,
|
tdata->type,
|
||||||
address,
|
address,
|
||||||
location,
|
location,
|
||||||
location + bytes_this_chunk);
|
location + bytes_this_chunk))
|
||||||
|
return false;
|
||||||
|
|
||||||
bytes_written += bytes_this_chunk;
|
bytes_written += bytes_this_chunk;
|
||||||
location += bytes_this_chunk;
|
location += bytes_this_chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static boolean
|
||||||
srec_write_terminator (abfd, tdata)
|
srec_write_terminator (abfd, tdata)
|
||||||
bfd *abfd;
|
bfd *abfd;
|
||||||
tdata_type *tdata;
|
tdata_type *tdata;
|
||||||
{
|
{
|
||||||
unsigned char buffer[2];
|
unsigned char buffer[2];
|
||||||
|
|
||||||
srec_write_record (abfd, 10 - tdata->type,
|
return srec_write_record (abfd, 10 - tdata->type,
|
||||||
abfd->start_address, buffer, buffer);
|
abfd->start_address, buffer, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
static boolean
|
||||||
srec_write_symbols (abfd)
|
srec_write_symbols (abfd)
|
||||||
bfd *abfd;
|
bfd *abfd;
|
||||||
{
|
{
|
||||||
|
@ -752,10 +768,13 @@ srec_write_symbols (abfd)
|
||||||
|
|
||||||
if (len)
|
if (len)
|
||||||
{
|
{
|
||||||
|
size_t len;
|
||||||
asymbol **table = bfd_get_outsymbols (abfd);
|
asymbol **table = bfd_get_outsymbols (abfd);
|
||||||
sprintf (buffer, "$$ %s\r\n", abfd->filename);
|
sprintf (buffer, "$$ %s\r\n", abfd->filename);
|
||||||
|
|
||||||
bfd_write (buffer, strlen (buffer), 1, abfd);
|
len = strlen (buffer) + 1;
|
||||||
|
if (bfd_write (buffer, len, 1, abfd) != len)
|
||||||
|
return false;
|
||||||
|
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
|
@ -771,7 +790,8 @@ srec_write_symbols (abfd)
|
||||||
int l;
|
int l;
|
||||||
sprintf (buffer, "$$ %s\r\n", s->name);
|
sprintf (buffer, "$$ %s\r\n", s->name);
|
||||||
l = strlen (buffer);
|
l = strlen (buffer);
|
||||||
bfd_write (buffer, l, 1, abfd);
|
if (bfd_write (buffer, l, 1, abfd) != l)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
|
@ -793,12 +813,17 @@ srec_write_symbols (abfd)
|
||||||
p++;
|
p++;
|
||||||
sprintf (buffer, " %s $%s\r\n", s->name, p);
|
sprintf (buffer, " %s $%s\r\n", s->name, p);
|
||||||
l = strlen (buffer);
|
l = strlen (buffer);
|
||||||
bfd_write (buffer, l, 1, abfd);
|
if (bfd_write (buffer, l, 1, abfd) != l)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sprintf (buffer, "$$ \r\n");
|
sprintf (buffer, "$$ \r\n");
|
||||||
bfd_write (buffer, strlen (buffer), 1, abfd);
|
len = strlen (buffer) + 1;
|
||||||
|
if (bfd_write (buffer, len, 1, abfd) != len)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean
|
static boolean
|
||||||
|
@ -810,20 +835,24 @@ internal_srec_write_object_contents (abfd, symbols)
|
||||||
srec_data_list_type *list;
|
srec_data_list_type *list;
|
||||||
|
|
||||||
if (symbols)
|
if (symbols)
|
||||||
srec_write_symbols (abfd);
|
{
|
||||||
|
if (! srec_write_symbols (abfd))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
srec_write_header (abfd);
|
if (! srec_write_header (abfd))
|
||||||
|
return false;
|
||||||
|
|
||||||
/* Now wander though all the sections provided and output them */
|
/* Now wander though all the sections provided and output them */
|
||||||
list = tdata->head;
|
list = tdata->head;
|
||||||
|
|
||||||
while (list != (srec_data_list_type *) NULL)
|
while (list != (srec_data_list_type *) NULL)
|
||||||
{
|
{
|
||||||
srec_write_section (abfd, tdata, list);
|
if (! srec_write_section (abfd, tdata, list))
|
||||||
|
return false;
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
srec_write_terminator (abfd, tdata);
|
return srec_write_terminator (abfd, tdata);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean
|
static boolean
|
||||||
|
|
Loading…
Reference in a new issue