xref: /netbsd-src/external/gpl3/binutils.old/dist/binutils/binemul.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* Binutils emulation layer.
2*e992f068Schristos    Copyright (C) 2002-2022 Free Software Foundation, Inc.
375fd0b74Schristos    Written by Tom Rix, Red Hat Inc.
475fd0b74Schristos 
575fd0b74Schristos    This file is part of GNU Binutils.
675fd0b74Schristos 
775fd0b74Schristos    This program is free software; you can redistribute it and/or modify
875fd0b74Schristos    it under the terms of the GNU General Public License as published by
975fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
1075fd0b74Schristos    (at your option) any later version.
1175fd0b74Schristos 
1275fd0b74Schristos    This program is distributed in the hope that it will be useful,
1375fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1475fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1575fd0b74Schristos    GNU General Public License for more details.
1675fd0b74Schristos 
1775fd0b74Schristos    You should have received a copy of the GNU General Public License
1875fd0b74Schristos    along with this program; if not, write to the Free Software
1975fd0b74Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2075fd0b74Schristos    MA 02110-1301, USA.  */
2175fd0b74Schristos 
2275fd0b74Schristos #include "binemul.h"
2375fd0b74Schristos 
2475fd0b74Schristos extern bin_emulation_xfer_type bin_dummy_emulation;
2575fd0b74Schristos 
2675fd0b74Schristos void
ar_emul_usage(FILE * fp)2775fd0b74Schristos ar_emul_usage (FILE *fp)
2875fd0b74Schristos {
2975fd0b74Schristos   if (bin_dummy_emulation.ar_usage)
3075fd0b74Schristos     bin_dummy_emulation.ar_usage (fp);
3175fd0b74Schristos }
3275fd0b74Schristos 
3375fd0b74Schristos void
ar_emul_default_usage(FILE * fp)3475fd0b74Schristos ar_emul_default_usage (FILE *fp)
3575fd0b74Schristos {
3675fd0b74Schristos   AR_EMUL_USAGE_PRINT_OPTION_HEADER (fp);
3775fd0b74Schristos   /* xgettext:c-format */
3875fd0b74Schristos   fprintf (fp, _("  No emulation specific options\n"));
3975fd0b74Schristos }
4075fd0b74Schristos 
41*e992f068Schristos bool
ar_emul_append(bfd ** after_bfd,char * file_name,const char * target,bool verbose,bool flatten)4275fd0b74Schristos ar_emul_append (bfd **after_bfd, char *file_name, const char *target,
43*e992f068Schristos 		bool verbose, bool flatten)
4475fd0b74Schristos {
45*e992f068Schristos   bfd *new_bfd;
46*e992f068Schristos 
47*e992f068Schristos   new_bfd = bfd_openr (file_name, target);
48*e992f068Schristos   AR_EMUL_ELEMENT_CHECK (new_bfd, file_name);
4975fd0b74Schristos   if (bin_dummy_emulation.ar_append)
50*e992f068Schristos     return bin_dummy_emulation.ar_append (after_bfd, new_bfd,
5175fd0b74Schristos 					  verbose, flatten);
5275fd0b74Schristos 
53*e992f068Schristos   return false;
5475fd0b74Schristos }
5575fd0b74Schristos 
56*e992f068Schristos bool
ar_emul_append_bfd(bfd ** after_bfd,bfd * new_bfd,bool verbose,bool flatten)57*e992f068Schristos ar_emul_append_bfd (bfd **after_bfd, bfd *new_bfd,
58*e992f068Schristos 		bool verbose, bool flatten)
59*e992f068Schristos {
60*e992f068Schristos   if (bin_dummy_emulation.ar_append)
61*e992f068Schristos     return bin_dummy_emulation.ar_append (after_bfd, new_bfd,
62*e992f068Schristos 					  verbose, flatten);
63*e992f068Schristos 
64*e992f068Schristos   return false;
65*e992f068Schristos }
66*e992f068Schristos 
67*e992f068Schristos static bool
any_ok(bfd * new_bfd ATTRIBUTE_UNUSED)6875fd0b74Schristos any_ok (bfd *new_bfd ATTRIBUTE_UNUSED)
6975fd0b74Schristos {
70*e992f068Schristos   return true;
7175fd0b74Schristos }
7275fd0b74Schristos 
73*e992f068Schristos bool
do_ar_emul_append(bfd ** after_bfd,bfd * new_bfd,bool verbose,bool flatten,bool (* check)(bfd *))7475fd0b74Schristos do_ar_emul_append (bfd **after_bfd, bfd *new_bfd,
75*e992f068Schristos 		   bool verbose, bool flatten,
76*e992f068Schristos 		   bool (*check) (bfd *))
7775fd0b74Schristos {
7875fd0b74Schristos   /* When flattening, add the members of an archive instead of the
7975fd0b74Schristos      archive itself.  */
8075fd0b74Schristos   if (flatten && bfd_check_format (new_bfd, bfd_archive))
8175fd0b74Schristos     {
8275fd0b74Schristos       bfd *elt;
83*e992f068Schristos       bool added = false;
8475fd0b74Schristos 
8575fd0b74Schristos       for (elt = bfd_openr_next_archived_file (new_bfd, NULL);
8675fd0b74Schristos            elt;
8775fd0b74Schristos            elt = bfd_openr_next_archived_file (new_bfd, elt))
8875fd0b74Schristos         {
89*e992f068Schristos           if (do_ar_emul_append (after_bfd, elt, verbose, true, check))
9075fd0b74Schristos             {
91*e992f068Schristos               added = true;
9275fd0b74Schristos               after_bfd = &((*after_bfd)->archive_next);
9375fd0b74Schristos             }
9475fd0b74Schristos         }
9575fd0b74Schristos 
9675fd0b74Schristos       return added;
9775fd0b74Schristos     }
9875fd0b74Schristos 
9975fd0b74Schristos   if (!check (new_bfd))
100*e992f068Schristos     return false;
10175fd0b74Schristos 
102*e992f068Schristos   AR_EMUL_APPEND_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd));
10375fd0b74Schristos 
10475fd0b74Schristos   new_bfd->archive_next = *after_bfd;
10575fd0b74Schristos   *after_bfd = new_bfd;
10675fd0b74Schristos 
107*e992f068Schristos   return true;
10875fd0b74Schristos }
10975fd0b74Schristos 
110*e992f068Schristos bool
ar_emul_default_append(bfd ** after_bfd,bfd * new_bfd,bool verbose,bool flatten)111*e992f068Schristos ar_emul_default_append (bfd **after_bfd, bfd *new_bfd,
112*e992f068Schristos 			bool verbose, bool flatten)
11375fd0b74Schristos {
11475fd0b74Schristos   return do_ar_emul_append (after_bfd, new_bfd, verbose, flatten, any_ok);
11575fd0b74Schristos }
11675fd0b74Schristos 
117*e992f068Schristos bool
ar_emul_replace(bfd ** after_bfd,char * file_name,const char * target,bool verbose)11875fd0b74Schristos ar_emul_replace (bfd **after_bfd, char *file_name, const char *target,
119*e992f068Schristos 		 bool verbose)
12075fd0b74Schristos {
12175fd0b74Schristos   bfd *new_bfd;
12275fd0b74Schristos 
12375fd0b74Schristos   new_bfd = bfd_openr (file_name, target);
12475fd0b74Schristos   AR_EMUL_ELEMENT_CHECK (new_bfd, file_name);
12575fd0b74Schristos 
126*e992f068Schristos   if (bin_dummy_emulation.ar_replace)
127*e992f068Schristos     return bin_dummy_emulation.ar_replace (after_bfd, new_bfd,
128*e992f068Schristos 					   verbose);
129*e992f068Schristos 
130*e992f068Schristos   return false;
131*e992f068Schristos }
132*e992f068Schristos 
133*e992f068Schristos bool
ar_emul_replace_bfd(bfd ** after_bfd,bfd * new_bfd,bool verbose)134*e992f068Schristos ar_emul_replace_bfd (bfd **after_bfd, bfd *new_bfd,
135*e992f068Schristos 		 bool verbose)
136*e992f068Schristos {
137*e992f068Schristos   if (bin_dummy_emulation.ar_replace)
138*e992f068Schristos     return bin_dummy_emulation.ar_replace (after_bfd, new_bfd,
139*e992f068Schristos 					   verbose);
140*e992f068Schristos 
141*e992f068Schristos   return false;
142*e992f068Schristos }
143*e992f068Schristos 
144*e992f068Schristos bool
ar_emul_default_replace(bfd ** after_bfd,bfd * new_bfd,bool verbose)145*e992f068Schristos ar_emul_default_replace (bfd **after_bfd, bfd *new_bfd,
146*e992f068Schristos 			 bool verbose)
147*e992f068Schristos {
148*e992f068Schristos   AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd));
14975fd0b74Schristos 
15075fd0b74Schristos   new_bfd->archive_next = *after_bfd;
15175fd0b74Schristos   *after_bfd = new_bfd;
15275fd0b74Schristos 
153*e992f068Schristos   return true;
15475fd0b74Schristos }
15575fd0b74Schristos 
156*e992f068Schristos bool
ar_emul_parse_arg(char * arg)15775fd0b74Schristos ar_emul_parse_arg (char *arg)
15875fd0b74Schristos {
15975fd0b74Schristos   if (bin_dummy_emulation.ar_parse_arg)
16075fd0b74Schristos     return bin_dummy_emulation.ar_parse_arg (arg);
16175fd0b74Schristos 
162*e992f068Schristos   return false;
16375fd0b74Schristos }
16475fd0b74Schristos 
165*e992f068Schristos bool
ar_emul_default_parse_arg(char * arg ATTRIBUTE_UNUSED)16675fd0b74Schristos ar_emul_default_parse_arg (char *arg ATTRIBUTE_UNUSED)
16775fd0b74Schristos {
168*e992f068Schristos   return false;
16975fd0b74Schristos }
170