* archive.c (_bfd_write_archive_contents): If read fails,

set bfd_error to malformed_archive (since this probably
	indicates a truncated archive), rather than system_call_error.
(Handles PRMS 1624.)
This commit is contained in:
Per Bothner
1992-10-05 21:36:56 +00:00
parent b5ddf9421c
commit 286fd2f9e5
2 changed files with 172 additions and 92 deletions

View File

@ -1,3 +1,9 @@
Mon Oct 5 14:32:55 1992 Per Bothner (bothner@cygnus.com)
* archive.c (_bfd_write_archive_contents): If read fails,
set bfd_error to malformed_archive (since this probably
indicates a truncated archive), rather than system_call_error.
Mon Oct 5 03:33:39 1992 Mark Eichin (eichin at tweedledumber.cygnus.com)
* Makefile.in: added rules for i386bsd.c

View File

@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/*doc*
/*
@setfilename archive-info
SECTION
Archives
@ -93,8 +93,6 @@ DESCRIPTION
front, and need to touch every entry to do so. C'est la vie.
*/
/* $Id$ */
#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"
@ -128,7 +126,7 @@ boolean
_bfd_generic_mkarchive (abfd)
bfd *abfd;
{
set_tdata (abfd, bfd_zalloc(abfd, sizeof (struct artdata)));
abfd->tdata.aout_ar_data = (struct artdata *)bfd_zalloc(abfd, sizeof (struct artdata));
if (bfd_ardata (abfd) == NULL) {
bfd_error = no_memory;
@ -160,7 +158,7 @@ DESCRIPTION
*/
symindex
DEFUN(bfd_get_next_mapent,(abfd, prev, entry)
DEFUN(bfd_get_next_mapent,(abfd, prev, entry),
bfd *abfd AND
symindex prev AND
carsym **entry)
@ -330,7 +328,8 @@ snarf_ar_hdr (abfd)
/* extract the filename from the archive - there are two ways to
specify an extendend name table, either the first char of the
name is a space, or it's a slash */
if ((hdr.ar_name[0] == '/' || hdr.ar_name[0] == ' ') && bfd_ardata (abfd)->extended_names != NULL) {
if ((hdr.ar_name[0] == '/' || hdr.ar_name[0] == ' ')
&& bfd_ardata (abfd)->extended_names != NULL) {
filename = get_extended_arelt_filename (abfd, hdr.ar_name);
if (filename == NULL) {
bfd_error = malformed_archive;
@ -432,7 +431,7 @@ DESCRIPTION
*/
bfd *
DEFUN(bfd_get_elt_at_index,(abfd, index)
DEFUN(bfd_get_elt_at_index,(abfd, index),
bfd *abfd AND
int index)
{
@ -511,12 +510,15 @@ bfd_generic_archive_p (abfd)
#ifdef GNU960
if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG)) return 0;
#else
if (strncmp (armag, ARMAG, SARMAG)) return 0;
if (strncmp (armag, ARMAG, SARMAG) &&
strncmp (armag, ARMAGB, SARMAG)) return 0;
#endif
/* We are setting bfd_ardata(abfd) here, but since bfd_ardata
involves a cast, we can't do it as the left operand of assignment. */
set_tdata (abfd, bfd_zalloc(abfd,sizeof (struct artdata)));
abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc(abfd,sizeof (struct artdata));
if (bfd_ardata (abfd) == NULL) {
bfd_error = no_memory;
@ -527,13 +529,13 @@ bfd_generic_archive_p (abfd)
if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))) {
bfd_release(abfd, bfd_ardata (abfd));
abfd->tdata = NULL;
abfd->tdata.aout_ar_data = NULL;
return 0;
}
if (!BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) {
bfd_release(abfd, bfd_ardata (abfd));
abfd->tdata = NULL;
abfd->tdata.aout_ar_data = NULL;
return 0;
}
@ -581,11 +583,20 @@ bfd_slurp_bsd_armap (abfd)
if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
mapdata->parsed_size) {
bfd_error = malformed_archive;
byebyebye:
bfd_release (abfd, (PTR)raw_armap);
goto byebye;
}
ardata->symdef_count = bfd_h_get_32(abfd, (PTR)raw_armap) / sizeof (struct symdef);
if (ardata->symdef_count * sizeof (struct symdef)
> mapdata->parsed_size - sizeof (*raw_armap)) {
/* Probably we're using the wrong byte ordering. */
bfd_error = wrong_format;
goto byebyebye;
}
ardata->cache = 0;
rbase = raw_armap+1;
ardata->symdefs = (carsym *) rbase;
@ -621,6 +632,7 @@ bfd_slurp_coff_armap (abfd)
unsigned int stringsize;
carsym *carsyms;
int result;
bfd_vma (*swap)();
result = bfd_read ((PTR)&nextname, 1, 1, abfd);
bfd_seek (abfd, -1L, SEEK_CUR);
@ -659,10 +671,27 @@ bfd_slurp_coff_armap (abfd)
It seems that all numeric information in a coff archive is always
in big endian format, nomatter the host or target. */
stringsize = mapdata->parsed_size - (4 * (_do_getb32((PTR)raw_armap))) - 4;
stringsize
= mapdata->parsed_size - (4 * (_do_getb32((PTR)raw_armap))) - 4;
/* Except that some archive formats are broken, and it may be our
fault - the i960 little endian coff sometimes has big and sometimes
little, because our tools changed. Here's a horrible hack to clean
up the crap
*/
swap = _do_getb32;
if (stringsize > 0xfffff)
{
/* This looks dangerous, let's do it the other way around */
stringsize = mapdata->parsed_size - (4 *
(_do_getl32((PTR)raw_armap))) - 4;
swap = _do_getl32;
}
{
unsigned int nsymz = _do_getb32( (PTR)raw_armap);
unsigned int nsymz = swap( (PTR)raw_armap);
unsigned int carsym_size = (nsymz * sizeof (carsym));
unsigned int ptrsize = (4 * nsymz);
unsigned int i;
@ -681,14 +710,14 @@ bfd_slurp_coff_armap (abfd)
for (i = 0; i < nsymz; i++)
{
rawptr = raw_armap + i + 1;
carsyms->file_offset = _do_getb32((PTR)rawptr);
carsyms->file_offset = swap((PTR)rawptr);
carsyms->name = stringbase;
for (; *(stringbase++););
carsyms++;
}
*stringbase = 0;
}
ardata->symdef_count = _do_getb32((PTR)raw_armap);
ardata->symdef_count = swap((PTR)raw_armap);
ardata->first_file_filepos = bfd_tell (abfd);
/* Pad to an even boundary if you have to */
ardata->first_file_filepos += (ardata->first_file_filepos) %2;
@ -709,7 +738,7 @@ bfd_slurp_coff_armap (abfd)
Intel has extended the format: longer names are stored in a special
element (the first in the archive, or second if there is an armap);
the name in the ar_hdr is replaced by <space><index into filename
element>. Index is the P.R. of an int (radix: 8). Data General have
element>. Index is the P.R. of an int (decimal). Data General have
extended the format by using the prefix // for the special element */
/* Returns false on error, true otherwise */
@ -773,11 +802,48 @@ _bfd_slurp_extended_name_table (abfd)
return true;
}
static
char *normalize(file)
char *file;
#ifdef VMS
/* Return a copy of the stuff in the filename between any :]> and a
semicolon */
static CONST char *
DEFUN(normalize,(file),
CONST char *file)
{
char * filename = strrchr(file, '/');
CONST char *first;
CONST char *last;
char *copy;
first = file + strlen(file)-1;
last = first+1;
while (first != file)
{
if (*first == ';')
last = first;
if (*first == ':' || *first == ']' ||*first == '>')
{
first++;
break;
}
first --;
}
copy = malloc(last - first + 1);
memcpy(copy, first, last-first);
copy[last-first] = 0;
return copy;
}
#else
static
CONST char *normalize(file)
CONST char *file;
{
CONST char * filename = strrchr(file, '/');
if (filename != (char *)NULL) {
filename ++;
}
@ -786,7 +852,7 @@ char *file;
}
return filename;
}
#endif
/* Follows archive_head and produces an extended name table if necessary.
Returns (in tabloc) a pointer to an extended name table, and in tablen
the length of the table. If it makes an entry it clobbers the filename
@ -826,7 +892,7 @@ bfd_construct_extended_name_table (abfd, tabloc, tablen)
for (current = abfd->archive_head; current != NULL; current =
current->next) {
char *normal =normalize( current->filename);
CONST char *normal =normalize( current->filename);
unsigned int thislen = strlen (normal);
if (thislen > maxname) {
/* Works for now; may need to be re-engineered if we encounter an oddball
@ -837,7 +903,7 @@ bfd_construct_extended_name_table (abfd, tabloc, tablen)
hdr->ar_name[0] = ' ';
/* We know there will always be enough room (one of the few cases
where you may safely use sprintf). */
sprintf ((hdr->ar_name) + 1, "%-o", (unsigned) (strptr - *tabloc));
sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
/* Kinda Kludgy. We should just use the returned value of sprintf
but not all implementations get this right */
{
@ -970,14 +1036,9 @@ bfd_dont_truncate_arname (abfd, pathname, arhdr)
struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
int length;
CONST char *filename = strrchr (pathname, '/');
CONST char *filename = normalize(pathname);
int maxlen = ar_maxnamelen (abfd);
if (filename == NULL)
filename = pathname;
else
++filename;
length = strlen (filename);
if (length <= maxlen)
@ -1156,7 +1217,11 @@ _bfd_write_archive_contents (arch)
if (amt > remaining) {
amt = remaining;
}
if (bfd_read (buffer, amt, 1, current) != amt) goto syserr;
if (bfd_read (buffer, amt, 1, current) != amt) {
/* Looks like a truncated archive. */
bfd_error = malformed_archive;
return false;
}
if (bfd_write (buffer, amt, 1, arch) != amt) goto syserr;
remaining -= amt;
}
@ -1221,9 +1286,14 @@ compute_and_write_armap (arch, elength)
/* Now map over all the symbols, picking out the ones we want */
for (src_count = 0; src_count <symcount; src_count++) {
flagword flags = (syms[src_count])->flags;
flagword flags =
(syms[src_count])->flags;
asection *sec =
syms[src_count]->section;
if ((flags & BSF_GLOBAL) ||
(flags & BSF_FORT_COMM)) {
(flags & BSF_INDIRECT) ||
(sec == &bfd_com_section)) {
/* This symbol will go into the archive header */
if (orl_count == orl_max)
@ -1282,7 +1352,13 @@ bsd_write_armap (arch, elength, map, orl_count, stridx)
stat (arch->filename, &statbuf);
memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
sprintf (hdr.ar_name, RANLIBMAG);
sprintf (hdr.ar_date, "%ld", statbuf.st_mtime);
/* write the timestamp of the archive header to be just a little bit
later than the timestamp of the file, otherwise the linker will
complain that the index is out of date.
*/
sprintf (hdr.ar_date, "%ld", statbuf.st_mtime + 60);
sprintf (hdr.ar_uid, "%d", getuid());
sprintf (hdr.ar_gid, "%d", getgid());
sprintf (hdr.ar_size, "%-10d", (int) mapsize);
@ -1357,7 +1433,6 @@ coff_write_armap (arch, elength, map, symbol_count, stridx)
unsigned int mapsize = stringsize + ranlibsize;
file_ptr archive_member_file_ptr;
bfd *current = arch->archive_head;
bfd *last_elt = current; /* last element arch seen */
int count;
struct ar_hdr hdr;
unsigned int i;
@ -1389,11 +1464,10 @@ coff_write_armap (arch, elength, map, symbol_count, stridx)
bfd_write_bigendian_4byte_int(arch, symbol_count);
/* Two passes, first write the file offsets for each symbol -
remembering that each offset is on a two byte boundary
*/
remembering that each offset is on a two byte boundary. */
/* Write out the file offset for the file associated with each
symbol, and remember to keep the offsets padded out */
symbol, and remember to keep the offsets padded out. */
current = arch->archive_head;
count = 0;