xref: /netbsd-src/external/gpl3/binutils.old/dist/binutils/binemul.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1 /* Binutils emulation layer.
2    Copyright (C) 2002-2022 Free Software Foundation, Inc.
3    Written by Tom Rix, Red Hat Inc.
4 
5    This file is part of GNU Binutils.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21 
22 #include "binemul.h"
23 
24 extern bin_emulation_xfer_type bin_dummy_emulation;
25 
26 void
ar_emul_usage(FILE * fp)27 ar_emul_usage (FILE *fp)
28 {
29   if (bin_dummy_emulation.ar_usage)
30     bin_dummy_emulation.ar_usage (fp);
31 }
32 
33 void
ar_emul_default_usage(FILE * fp)34 ar_emul_default_usage (FILE *fp)
35 {
36   AR_EMUL_USAGE_PRINT_OPTION_HEADER (fp);
37   /* xgettext:c-format */
38   fprintf (fp, _("  No emulation specific options\n"));
39 }
40 
41 bool
ar_emul_append(bfd ** after_bfd,char * file_name,const char * target,bool verbose,bool flatten)42 ar_emul_append (bfd **after_bfd, char *file_name, const char *target,
43 		bool verbose, bool flatten)
44 {
45   bfd *new_bfd;
46 
47   new_bfd = bfd_openr (file_name, target);
48   AR_EMUL_ELEMENT_CHECK (new_bfd, file_name);
49   if (bin_dummy_emulation.ar_append)
50     return bin_dummy_emulation.ar_append (after_bfd, new_bfd,
51 					  verbose, flatten);
52 
53   return false;
54 }
55 
56 bool
ar_emul_append_bfd(bfd ** after_bfd,bfd * new_bfd,bool verbose,bool flatten)57 ar_emul_append_bfd (bfd **after_bfd, bfd *new_bfd,
58 		bool verbose, bool flatten)
59 {
60   if (bin_dummy_emulation.ar_append)
61     return bin_dummy_emulation.ar_append (after_bfd, new_bfd,
62 					  verbose, flatten);
63 
64   return false;
65 }
66 
67 static bool
any_ok(bfd * new_bfd ATTRIBUTE_UNUSED)68 any_ok (bfd *new_bfd ATTRIBUTE_UNUSED)
69 {
70   return true;
71 }
72 
73 bool
do_ar_emul_append(bfd ** after_bfd,bfd * new_bfd,bool verbose,bool flatten,bool (* check)(bfd *))74 do_ar_emul_append (bfd **after_bfd, bfd *new_bfd,
75 		   bool verbose, bool flatten,
76 		   bool (*check) (bfd *))
77 {
78   /* When flattening, add the members of an archive instead of the
79      archive itself.  */
80   if (flatten && bfd_check_format (new_bfd, bfd_archive))
81     {
82       bfd *elt;
83       bool added = false;
84 
85       for (elt = bfd_openr_next_archived_file (new_bfd, NULL);
86            elt;
87            elt = bfd_openr_next_archived_file (new_bfd, elt))
88         {
89           if (do_ar_emul_append (after_bfd, elt, verbose, true, check))
90             {
91               added = true;
92               after_bfd = &((*after_bfd)->archive_next);
93             }
94         }
95 
96       return added;
97     }
98 
99   if (!check (new_bfd))
100     return false;
101 
102   AR_EMUL_APPEND_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd));
103 
104   new_bfd->archive_next = *after_bfd;
105   *after_bfd = new_bfd;
106 
107   return true;
108 }
109 
110 bool
ar_emul_default_append(bfd ** after_bfd,bfd * new_bfd,bool verbose,bool flatten)111 ar_emul_default_append (bfd **after_bfd, bfd *new_bfd,
112 			bool verbose, bool flatten)
113 {
114   return do_ar_emul_append (after_bfd, new_bfd, verbose, flatten, any_ok);
115 }
116 
117 bool
ar_emul_replace(bfd ** after_bfd,char * file_name,const char * target,bool verbose)118 ar_emul_replace (bfd **after_bfd, char *file_name, const char *target,
119 		 bool verbose)
120 {
121   bfd *new_bfd;
122 
123   new_bfd = bfd_openr (file_name, target);
124   AR_EMUL_ELEMENT_CHECK (new_bfd, file_name);
125 
126   if (bin_dummy_emulation.ar_replace)
127     return bin_dummy_emulation.ar_replace (after_bfd, new_bfd,
128 					   verbose);
129 
130   return false;
131 }
132 
133 bool
ar_emul_replace_bfd(bfd ** after_bfd,bfd * new_bfd,bool verbose)134 ar_emul_replace_bfd (bfd **after_bfd, bfd *new_bfd,
135 		 bool verbose)
136 {
137   if (bin_dummy_emulation.ar_replace)
138     return bin_dummy_emulation.ar_replace (after_bfd, new_bfd,
139 					   verbose);
140 
141   return false;
142 }
143 
144 bool
ar_emul_default_replace(bfd ** after_bfd,bfd * new_bfd,bool verbose)145 ar_emul_default_replace (bfd **after_bfd, bfd *new_bfd,
146 			 bool verbose)
147 {
148   AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd));
149 
150   new_bfd->archive_next = *after_bfd;
151   *after_bfd = new_bfd;
152 
153   return true;
154 }
155 
156 bool
ar_emul_parse_arg(char * arg)157 ar_emul_parse_arg (char *arg)
158 {
159   if (bin_dummy_emulation.ar_parse_arg)
160     return bin_dummy_emulation.ar_parse_arg (arg);
161 
162   return false;
163 }
164 
165 bool
ar_emul_default_parse_arg(char * arg ATTRIBUTE_UNUSED)166 ar_emul_default_parse_arg (char *arg ATTRIBUTE_UNUSED)
167 {
168   return false;
169 }
170