175fd0b74Schristos /* A program to test BFD.
2*e992f068Schristos Copyright (C) 2012-2022 Free Software Foundation, Inc.
375fd0b74Schristos
475fd0b74Schristos This file is part of the GNU Binutils.
575fd0b74Schristos
675fd0b74Schristos This program is free software; you can redistribute it and/or modify
775fd0b74Schristos it under the terms of the GNU General Public License as published by
875fd0b74Schristos the Free Software Foundation; either version 3 of the License, or
975fd0b74Schristos (at your option) any later version.
1075fd0b74Schristos
1175fd0b74Schristos This program is distributed in the hope that it will be useful,
1275fd0b74Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1375fd0b74Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1475fd0b74Schristos GNU General Public License for more details.
1575fd0b74Schristos
1675fd0b74Schristos You should have received a copy of the GNU General Public License
1775fd0b74Schristos along with this program; if not, write to the Free Software
1875fd0b74Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
1975fd0b74Schristos MA 02110-1301, USA. */
2075fd0b74Schristos
2175fd0b74Schristos #include "sysdep.h"
2275fd0b74Schristos #include "bfd.h"
2375fd0b74Schristos
2475fd0b74Schristos static void
die(const char * s)2575fd0b74Schristos die (const char *s)
2675fd0b74Schristos {
2775fd0b74Schristos printf ("oops: %s\n", s);
2875fd0b74Schristos exit (1);
2975fd0b74Schristos }
3075fd0b74Schristos
3175fd0b74Schristos static void *
iovec_open(struct bfd * nbfd ATTRIBUTE_UNUSED,void * open_closure)3275fd0b74Schristos iovec_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *open_closure)
3375fd0b74Schristos {
3475fd0b74Schristos return open_closure;
3575fd0b74Schristos }
3675fd0b74Schristos
iovec_read(struct bfd * nbfd ATTRIBUTE_UNUSED,void * stream,void * buf,file_ptr nbytes,file_ptr offset)3775fd0b74Schristos static file_ptr iovec_read (struct bfd *nbfd ATTRIBUTE_UNUSED,
3875fd0b74Schristos void *stream, void *buf, file_ptr nbytes,
3975fd0b74Schristos file_ptr offset)
4075fd0b74Schristos {
4175fd0b74Schristos FILE* file = (FILE*) stream;
4275fd0b74Schristos
4375fd0b74Schristos if (fseek(file, offset, SEEK_SET) != 0)
4475fd0b74Schristos die ("fseek error");
4575fd0b74Schristos
4675fd0b74Schristos return fread (buf, 1, nbytes, file);
4775fd0b74Schristos }
4875fd0b74Schristos
4975fd0b74Schristos static int
iovec_stat(struct bfd * abfd ATTRIBUTE_UNUSED,void * stream,struct stat * sb)5075fd0b74Schristos iovec_stat (struct bfd *abfd ATTRIBUTE_UNUSED,
5175fd0b74Schristos void *stream, struct stat *sb)
5275fd0b74Schristos {
5375fd0b74Schristos return fstat (fileno ((FILE*) stream), sb);
5475fd0b74Schristos }
5575fd0b74Schristos
56*e992f068Schristos static bool
check_format_any(struct bfd * abfd,bfd_format format)5775fd0b74Schristos check_format_any (struct bfd *abfd, bfd_format format)
5875fd0b74Schristos {
5975fd0b74Schristos char** targets = NULL;
6075fd0b74Schristos
6175fd0b74Schristos if (bfd_check_format_matches (abfd, format, &targets))
62*e992f068Schristos return true;
6375fd0b74Schristos
6475fd0b74Schristos if (targets)
6575fd0b74Schristos {
6675fd0b74Schristos bfd_find_target (targets[0], abfd);
6775fd0b74Schristos
6875fd0b74Schristos return bfd_check_format (abfd, format);
6975fd0b74Schristos }
7075fd0b74Schristos
71*e992f068Schristos return false;
7275fd0b74Schristos }
7375fd0b74Schristos
7475fd0b74Schristos int
main(int argc,const char ** argv)7575fd0b74Schristos main (int argc, const char** argv)
7675fd0b74Schristos {
7775fd0b74Schristos FILE* file;
7875fd0b74Schristos bfd *abfd, *mbfd;
7975fd0b74Schristos
8075fd0b74Schristos if (argc < 2)
8175fd0b74Schristos die ("Usage: test archivefile");
8275fd0b74Schristos
8375fd0b74Schristos file = fopen(argv[1], "rb");
8475fd0b74Schristos if (!file)
8575fd0b74Schristos die ("file not found");
8675fd0b74Schristos
8775fd0b74Schristos abfd = bfd_openr_iovec (argv[1], 0, iovec_open, file,
8875fd0b74Schristos iovec_read, NULL, iovec_stat);
8975fd0b74Schristos if (!abfd)
9075fd0b74Schristos die ("error opening file");
9175fd0b74Schristos
9275fd0b74Schristos if (!check_format_any (abfd, bfd_archive))
9375fd0b74Schristos die ("not an archive");
9475fd0b74Schristos
9575fd0b74Schristos mbfd = bfd_openr_next_archived_file (abfd, 0);
9675fd0b74Schristos if (!mbfd)
9775fd0b74Schristos die ("error opening archive member");
9875fd0b74Schristos
9975fd0b74Schristos if (!bfd_close (mbfd))
10075fd0b74Schristos die ("error closing archive member");
10175fd0b74Schristos
10275fd0b74Schristos if (!bfd_close (abfd))
10375fd0b74Schristos die ("error closing archive");
10475fd0b74Schristos
10575fd0b74Schristos return 0;
10675fd0b74Schristos }
107