]>
Commit | Line | Data |
---|---|---|
c0367ba5 | 1 | /* objcopy.c -- copy object file from input to output, optionally massaging it. |
f7b839f7 | 2 | Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc. |
c0367ba5 | 3 | |
46050fe4 ILT |
4 | This file is part of GNU Binutils. |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | \f | |
c0367ba5 ILT |
20 | #include "bfd.h" |
21 | #include "sysdep.h" | |
22 | #include "bucomm.h" | |
23 | #include <getopt.h> | |
24 | ||
f7b839f7 DM |
25 | static void setup_section (); |
26 | static void copy_section (); | |
c0367ba5 | 27 | |
46050fe4 | 28 | #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;} |
c0367ba5 | 29 | |
46050fe4 ILT |
30 | static asymbol **isympp = NULL; /* Input symbols */ |
31 | static asymbol **osympp = NULL; /* Output symbols that survive stripping */ | |
f7b839f7 DM |
32 | |
33 | /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */ | |
34 | static int copy_byte = -1; | |
35 | static int interleave = 4; | |
36 | ||
46050fe4 | 37 | static boolean verbose; /* Print file and target names. */ |
f7b839f7 | 38 | static int status = 0; /* Exit status. */ |
c0367ba5 ILT |
39 | |
40 | enum strip_action | |
46050fe4 ILT |
41 | { |
42 | strip_undef, | |
43 | strip_none, /* don't strip */ | |
44 | strip_debug, /* strip all debugger symbols */ | |
45 | strip_all /* strip all symbols */ | |
46 | }; | |
c0367ba5 ILT |
47 | |
48 | /* Which symbols to remove. */ | |
46050fe4 | 49 | static enum strip_action strip_symbols; |
c0367ba5 ILT |
50 | |
51 | enum locals_action | |
46050fe4 ILT |
52 | { |
53 | locals_undef, | |
54 | locals_start_L, /* discard locals starting with L */ | |
55 | locals_all /* discard all locals */ | |
56 | }; | |
57 | ||
58 | /* Which local symbols to remove. Overrides strip_all. */ | |
59 | static enum locals_action discard_locals; | |
60 | ||
61 | /* Options to handle if running as "strip". */ | |
62 | ||
63 | static struct option strip_options[] = | |
c0367ba5 | 64 | { |
46050fe4 ILT |
65 | {"discard-all", no_argument, 0, 'x'}, |
66 | {"discard-locals", no_argument, 0, 'X'}, | |
f7b839f7 | 67 | {"format", required_argument, 0, 'F'}, /* Obsolete */ |
46050fe4 | 68 | {"help", no_argument, 0, 'h'}, |
46050fe4 | 69 | {"input-format", required_argument, 0, 'I'}, /* Obsolete */ |
f7b839f7 | 70 | {"input-target", required_argument, 0, 'I'}, |
46050fe4 | 71 | {"output-format", required_argument, 0, 'O'}, /* Obsolete */ |
f7b839f7 DM |
72 | {"output-target", required_argument, 0, 'O'}, |
73 | {"strip-all", no_argument, 0, 's'}, | |
74 | {"strip-debug", no_argument, 0, 'S'}, | |
46050fe4 | 75 | {"target", required_argument, 0, 'F'}, |
46050fe4 | 76 | {"verbose", no_argument, 0, 'v'}, |
f7b839f7 | 77 | {"version", no_argument, 0, 'V'}, |
46050fe4 | 78 | {0, no_argument, 0, 0} |
c0367ba5 ILT |
79 | }; |
80 | ||
46050fe4 | 81 | /* Options to handle if running as "objcopy". */ |
c0367ba5 | 82 | |
46050fe4 ILT |
83 | static struct option copy_options[] = |
84 | { | |
f7b839f7 | 85 | {"byte", required_argument, 0, 'b'}, |
46050fe4 ILT |
86 | {"discard-all", no_argument, 0, 'x'}, |
87 | {"discard-locals", no_argument, 0, 'X'}, | |
f7b839f7 | 88 | {"format", required_argument, 0, 'F'}, /* Obsolete */ |
46050fe4 | 89 | {"help", no_argument, 0, 'h'}, |
46050fe4 | 90 | {"input-format", required_argument, 0, 'I'}, /* Obsolete */ |
f7b839f7 DM |
91 | {"input-target", required_argument, 0, 'I'}, |
92 | {"interleave", required_argument, 0, 'i'}, | |
46050fe4 | 93 | {"output-format", required_argument, 0, 'O'}, /* Obsolete */ |
f7b839f7 DM |
94 | {"output-target", required_argument, 0, 'O'}, |
95 | {"strip-all", no_argument, 0, 'S'}, | |
96 | {"strip-debug", no_argument, 0, 'g'}, | |
46050fe4 | 97 | {"target", required_argument, 0, 'F'}, |
46050fe4 | 98 | {"verbose", no_argument, 0, 'v'}, |
f7b839f7 | 99 | {"version", no_argument, 0, 'V'}, |
46050fe4 | 100 | {0, no_argument, 0, 0} |
c0367ba5 ILT |
101 | }; |
102 | ||
103 | /* IMPORTS */ | |
46050fe4 ILT |
104 | extern char *program_name; |
105 | extern char *program_version; | |
106 | ||
107 | /* This flag distinguishes between strip and objcopy: | |
108 | 1 means this is 'strip'; 0 means this is 'objcopy'. | |
109 | -1 means if we should use argv[0] to decide. */ | |
110 | extern int is_strip; | |
c0367ba5 ILT |
111 | |
112 | ||
46050fe4 ILT |
113 | static void |
114 | copy_usage (stream, status) | |
c0367ba5 ILT |
115 | FILE *stream; |
116 | int status; | |
117 | { | |
46050fe4 | 118 | fprintf (stream, "\ |
f7b839f7 DM |
119 | Usage: %s [-vVSgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\ |
120 | [-i interleave] [--interleave=interleave] [--byte=byte]\n\ | |
46050fe4 ILT |
121 | [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\ |
122 | [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\ | |
f7b839f7 DM |
123 | [--verbose] [--version] [--help] in-file [out-file]\n", |
124 | program_name); | |
46050fe4 | 125 | exit (status); |
c0367ba5 ILT |
126 | } |
127 | ||
46050fe4 ILT |
128 | static void |
129 | strip_usage (stream, status) | |
c0367ba5 ILT |
130 | FILE *stream; |
131 | int status; | |
132 | { | |
46050fe4 ILT |
133 | fprintf (stream, "\ |
134 | Usage: %s [-vVsSgxX] [-I bfdname] [-O bfdname] [-F bfdname]\n\ | |
135 | [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\ | |
136 | [--strip-all] [--strip-debug] [--discard-all] [--discard-locals]\n\ | |
137 | [--verbose] [--version] [--help] file...\n", | |
138 | program_name); | |
139 | exit (status); | |
c0367ba5 ILT |
140 | } |
141 | ||
142 | ||
46050fe4 | 143 | /* Return the name of a temporary file in the same directory as FILENAME. */ |
c0367ba5 | 144 | |
46050fe4 ILT |
145 | static char * |
146 | make_tempname (filename) | |
147 | char *filename; | |
c0367ba5 | 148 | { |
46050fe4 ILT |
149 | static char template[] = "stXXXXXX"; |
150 | char *tmpname; | |
151 | char *slash = strrchr (filename, '/'); | |
152 | ||
153 | if (slash != (char *) NULL) | |
154 | { | |
155 | *slash = 0; | |
156 | tmpname = xmalloc (strlen (filename) + sizeof (template) + 1); | |
157 | strcpy (tmpname, filename); | |
158 | strcat (tmpname, "/"); | |
159 | strcat (tmpname, template); | |
160 | mktemp (tmpname); | |
161 | *slash = '/'; | |
162 | } | |
163 | else | |
164 | { | |
165 | tmpname = xmalloc (sizeof (template)); | |
166 | strcpy (tmpname, template); | |
167 | mktemp (tmpname); | |
c0367ba5 | 168 | } |
46050fe4 | 169 | return tmpname; |
c0367ba5 ILT |
170 | } |
171 | ||
46050fe4 | 172 | /* Choose which symbol entries to copy; put the result in OSYMS. |
c0367ba5 | 173 | We don't copy in place, because that confuses the relocs. |
46050fe4 ILT |
174 | Return the number of symbols to print. */ |
175 | ||
c0367ba5 ILT |
176 | static unsigned int |
177 | filter_symbols (abfd, osyms, isyms, symcount) | |
178 | bfd *abfd; | |
179 | asymbol **osyms, **isyms; | |
180 | unsigned long symcount; | |
181 | { | |
182 | register asymbol **from = isyms, **to = osyms; | |
46050fe4 ILT |
183 | char locals_prefix = bfd_get_symbol_leading_char (abfd) == '_' ? 'L' : '.'; |
184 | unsigned int src_count = 0, dst_count = 0; | |
c0367ba5 | 185 | |
46050fe4 ILT |
186 | for (; src_count < symcount; src_count++) |
187 | { | |
188 | asymbol *sym = from[src_count]; | |
189 | flagword flags = sym->flags; | |
190 | int keep; | |
c0367ba5 | 191 | |
46050fe4 ILT |
192 | if ((flags & BSF_GLOBAL) /* Keep if external. */ |
193 | || bfd_get_section (sym) == &bfd_und_section | |
194 | || bfd_is_com_section (bfd_get_section (sym))) | |
195 | keep = 1; | |
196 | else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */ | |
197 | keep = strip_symbols != strip_debug; | |
198 | else /* Local symbol. */ | |
199 | keep = discard_locals != locals_all | |
200 | && (discard_locals != locals_start_L || | |
201 | bfd_asymbol_name (sym)[0] != locals_prefix); | |
202 | if (keep) | |
203 | to[dst_count++] = sym; | |
c0367ba5 | 204 | } |
c0367ba5 ILT |
205 | |
206 | return dst_count; | |
207 | } | |
208 | ||
f7b839f7 DM |
209 | /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long. |
210 | Adjust *SIZE. */ | |
211 | ||
212 | void | |
213 | filter_bytes (memhunk, size) | |
5d2f7e30 | 214 | char *memhunk; |
f7b839f7 DM |
215 | bfd_size_type *size; |
216 | { | |
217 | char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size; | |
218 | ||
219 | for (; from < end; from += interleave) | |
220 | *to++ = *from; | |
221 | *size /= interleave; | |
222 | } | |
223 | ||
46050fe4 ILT |
224 | /* Copy object file IBFD onto OBFD. */ |
225 | ||
c0367ba5 | 226 | static void |
46050fe4 ILT |
227 | copy_object (ibfd, obfd) |
228 | bfd *ibfd; | |
229 | bfd *obfd; | |
c0367ba5 | 230 | { |
46050fe4 | 231 | unsigned int symcount; |
c0367ba5 | 232 | |
46050fe4 ILT |
233 | if (!bfd_set_format (obfd, bfd_get_format (ibfd))) |
234 | { | |
235 | nonfatal (bfd_get_filename (obfd)); | |
236 | } | |
c0367ba5 | 237 | |
46050fe4 ILT |
238 | if (verbose) |
239 | printf ("copy from %s(%s) to %s(%s)\n", | |
240 | bfd_get_filename(ibfd), bfd_get_target(ibfd), | |
241 | bfd_get_filename(obfd), bfd_get_target(obfd)); | |
c0367ba5 | 242 | |
46050fe4 ILT |
243 | if (!bfd_set_start_address (obfd, bfd_get_start_address (ibfd)) |
244 | || !bfd_set_file_flags (obfd, | |
245 | (bfd_get_file_flags (ibfd) | |
246 | & bfd_applicable_file_flags (obfd)))) | |
247 | { | |
248 | nonfatal (bfd_get_filename (ibfd)); | |
249 | } | |
c0367ba5 | 250 | |
46050fe4 ILT |
251 | /* Copy architecture of input file to output file */ |
252 | if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), | |
253 | bfd_get_mach (ibfd))) | |
254 | { | |
255 | fprintf (stderr, "Output file cannot represent architecture %s\n", | |
256 | bfd_printable_arch_mach (bfd_get_arch (ibfd), | |
257 | bfd_get_mach (ibfd))); | |
258 | } | |
259 | if (!bfd_set_format (obfd, bfd_get_format (ibfd))) | |
260 | { | |
261 | nonfatal (bfd_get_filename(ibfd)); | |
262 | } | |
c0367ba5 | 263 | |
46050fe4 ILT |
264 | if (isympp) |
265 | free (isympp); | |
266 | if (osympp != isympp) | |
267 | free (osympp); | |
c0367ba5 | 268 | |
46050fe4 ILT |
269 | if (strip_symbols == strip_all && discard_locals == locals_undef) |
270 | { | |
271 | osympp = isympp = NULL; | |
272 | symcount = 0; | |
c0367ba5 | 273 | } |
46050fe4 ILT |
274 | else |
275 | { | |
276 | osympp = isympp = (asymbol **) xmalloc (get_symtab_upper_bound (ibfd)); | |
277 | symcount = bfd_canonicalize_symtab (ibfd, isympp); | |
278 | ||
279 | if (strip_symbols == strip_debug || discard_locals != locals_undef) | |
c0367ba5 | 280 | { |
46050fe4 ILT |
281 | osympp = (asymbol **) xmalloc (symcount * sizeof (asymbol *)); |
282 | symcount = filter_symbols (ibfd, osympp, isympp, symcount); | |
c0367ba5 | 283 | } |
46050fe4 ILT |
284 | } |
285 | ||
286 | bfd_set_symtab (obfd, osympp, symcount); | |
c0367ba5 | 287 | |
46050fe4 ILT |
288 | /* bfd mandates that all output sections be created and sizes set before |
289 | any output is done. Thus, we traverse all sections multiple times. */ | |
f7b839f7 DM |
290 | bfd_map_over_sections (ibfd, setup_section, (void *) obfd); |
291 | bfd_map_over_sections (ibfd, copy_section, (void *) obfd); | |
c0367ba5 | 292 | } |
46050fe4 ILT |
293 | |
294 | static char * | |
295 | cat (a, b, c) | |
296 | char *a; | |
297 | char *b; | |
298 | char *c; | |
c0367ba5 | 299 | { |
46050fe4 ILT |
300 | size_t size = strlen (a) + strlen (b) + strlen (c); |
301 | char *r = xmalloc (size + 1); | |
302 | ||
303 | strcpy (r, a); | |
304 | strcat (r, b); | |
305 | strcat (r, c); | |
306 | return r; | |
c0367ba5 ILT |
307 | } |
308 | ||
46050fe4 ILT |
309 | /* Read each archive element in turn from IBFD, copy the |
310 | contents to temp file, and keep the temp file handle. */ | |
311 | ||
312 | static void | |
313 | copy_archive (ibfd, obfd, output_target) | |
314 | bfd *ibfd; | |
315 | bfd *obfd; | |
316 | char *output_target; | |
c0367ba5 | 317 | { |
46050fe4 ILT |
318 | bfd **ptr = &obfd->archive_head; |
319 | bfd *this_element; | |
320 | char *dir = cat ("./#", make_tempname (""), "cd"); | |
321 | ||
322 | /* Make a temp directory to hold the contents. */ | |
323 | mkdir (dir, 0777); | |
324 | obfd->has_armap = ibfd->has_armap; | |
325 | ||
326 | this_element = bfd_openr_next_archived_file (ibfd, NULL); | |
327 | ibfd->archive_head = this_element; | |
328 | while (this_element != (bfd *) NULL) | |
329 | { | |
330 | /* Create an output file for this member. */ | |
331 | char *output_name = cat (dir, "/", bfd_get_filename(this_element)); | |
332 | bfd *output_bfd = bfd_openw (output_name, output_target); | |
333 | ||
334 | if (output_bfd == (bfd *) NULL) | |
335 | { | |
336 | nonfatal (output_name); | |
c0367ba5 | 337 | } |
46050fe4 ILT |
338 | if (!bfd_set_format (obfd, bfd_get_format (ibfd))) |
339 | { | |
340 | nonfatal (bfd_get_filename (obfd)); | |
c0367ba5 ILT |
341 | } |
342 | ||
46050fe4 ILT |
343 | if (bfd_check_format (this_element, bfd_object) == true) |
344 | { | |
345 | copy_object (this_element, output_bfd); | |
346 | } | |
c0367ba5 | 347 | |
46050fe4 ILT |
348 | bfd_close (output_bfd); |
349 | /* Open the newly output file and attatch to our list. */ | |
350 | output_bfd = bfd_openr (output_name, output_target); | |
c0367ba5 | 351 | |
46050fe4 ILT |
352 | /* Mark it for deletion. */ |
353 | *ptr = output_bfd; | |
354 | ptr = &output_bfd->next; | |
355 | this_element->next = bfd_openr_next_archived_file (ibfd, this_element); | |
356 | this_element = this_element->next; | |
c0367ba5 | 357 | } |
46050fe4 | 358 | *ptr = (bfd *) NULL; |
c0367ba5 | 359 | |
46050fe4 ILT |
360 | if (!bfd_close (obfd)) |
361 | { | |
362 | nonfatal (bfd_get_filename (obfd)); | |
c0367ba5 | 363 | } |
c0367ba5 | 364 | |
46050fe4 ILT |
365 | /* Delete all the files that we opened. |
366 | Construct their names again, unfortunately, but | |
367 | we're about to exit anyway. */ | |
368 | for (this_element = ibfd->archive_head; | |
369 | this_element != (bfd *) NULL; | |
370 | this_element = this_element->next) | |
371 | { | |
372 | unlink (cat (dir, "/", bfd_get_filename (this_element))); | |
373 | } | |
374 | rmdir (dir); | |
375 | if (!bfd_close (ibfd)) | |
376 | { | |
377 | nonfatal (bfd_get_filename (ibfd)); | |
378 | } | |
c0367ba5 ILT |
379 | } |
380 | ||
46050fe4 ILT |
381 | /* The top-level control. */ |
382 | ||
383 | static void | |
384 | copy_file (input_filename, output_filename, input_target, output_target) | |
385 | char *input_filename; | |
386 | char *output_filename; | |
387 | char *input_target; | |
388 | char *output_target; | |
c0367ba5 | 389 | { |
46050fe4 | 390 | bfd *ibfd; |
cef35d48 | 391 | char **matching; |
c0367ba5 ILT |
392 | |
393 | /* To allow us to do "strip *" without dying on the first | |
394 | non-object file, failures are nonfatal. */ | |
395 | ||
46050fe4 | 396 | ibfd = bfd_openr (input_filename, input_target); |
c0367ba5 ILT |
397 | if (ibfd == NULL) |
398 | { | |
46050fe4 | 399 | nonfatal (input_filename); |
c0367ba5 ILT |
400 | } |
401 | ||
cef35d48 DM |
402 | if (bfd_check_format (ibfd, bfd_archive)) |
403 | { | |
404 | bfd *obfd = bfd_openw (output_filename, output_target); | |
405 | if (obfd == NULL) | |
406 | { | |
407 | nonfatal (output_filename); | |
408 | } | |
409 | copy_archive (ibfd, obfd, output_target); | |
410 | } | |
411 | else if (bfd_check_format_matches (ibfd, bfd_object, &matching)) | |
46050fe4 ILT |
412 | { |
413 | bfd *obfd = bfd_openw (output_filename, output_target); | |
414 | if (obfd == NULL) | |
415 | { | |
416 | nonfatal (output_filename); | |
417 | } | |
c0367ba5 | 418 | |
46050fe4 | 419 | copy_object (ibfd, obfd); |
c0367ba5 | 420 | |
46050fe4 ILT |
421 | if (!bfd_close (obfd)) |
422 | { | |
423 | nonfatal (output_filename); | |
424 | } | |
425 | ||
426 | if (!bfd_close (ibfd)) | |
427 | { | |
428 | nonfatal (input_filename); | |
429 | } | |
430 | } | |
cef35d48 | 431 | else |
46050fe4 | 432 | { |
cef35d48 | 433 | bfd_nonfatal (input_filename); |
c9563567 | 434 | if (bfd_get_error () == bfd_error_file_ambiguously_recognized) |
46050fe4 | 435 | { |
cef35d48 DM |
436 | list_matching_formats (matching); |
437 | free (matching); | |
46050fe4 | 438 | } |
cef35d48 | 439 | status = 1; |
46050fe4 ILT |
440 | } |
441 | } | |
442 | ||
443 | /* Create a section in OBFD with the same name and attributes | |
444 | as ISECTION in IBFD. */ | |
c0367ba5 | 445 | |
c0367ba5 | 446 | static void |
f7b839f7 | 447 | setup_section (ibfd, isection, obfd) |
46050fe4 ILT |
448 | bfd *ibfd; |
449 | sec_ptr isection; | |
450 | bfd *obfd; | |
c0367ba5 | 451 | { |
46050fe4 ILT |
452 | sec_ptr osection; |
453 | char *err; | |
454 | ||
455 | if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0 | |
456 | && (strip_symbols == strip_debug | |
457 | || strip_symbols == strip_all | |
458 | || discard_locals == locals_all)) | |
459 | return; | |
460 | ||
461 | osection = bfd_get_section_by_name (obfd, bfd_section_name (ibfd, isection)); | |
462 | if (osection == NULL) | |
463 | { | |
464 | osection = bfd_make_section (obfd, bfd_section_name (ibfd, isection)); | |
465 | if (osection == NULL) | |
466 | { | |
467 | err = "making"; | |
468 | goto loser; | |
c0367ba5 ILT |
469 | } |
470 | } | |
471 | ||
46050fe4 ILT |
472 | if (!bfd_set_section_size (obfd, |
473 | osection, | |
474 | bfd_section_size (ibfd, isection))) | |
475 | { | |
476 | err = "size"; | |
477 | goto loser; | |
c0367ba5 ILT |
478 | } |
479 | ||
46050fe4 ILT |
480 | if (bfd_set_section_vma (obfd, |
481 | osection, | |
482 | bfd_section_vma (ibfd, isection)) | |
483 | == false) | |
484 | { | |
485 | err = "vma"; | |
486 | goto loser; | |
487 | } | |
c0367ba5 | 488 | |
46050fe4 ILT |
489 | if (bfd_set_section_alignment (obfd, |
490 | osection, | |
491 | bfd_section_alignment (ibfd, isection)) | |
492 | == false) | |
493 | { | |
494 | err = "alignment"; | |
495 | goto loser; | |
496 | } | |
c0367ba5 | 497 | |
46050fe4 ILT |
498 | if (!bfd_set_section_flags (obfd, osection, |
499 | bfd_get_section_flags (ibfd, isection))) | |
500 | { | |
501 | err = "flags"; | |
502 | goto loser; | |
c0367ba5 ILT |
503 | } |
504 | ||
c9563567 JL |
505 | /* This used to be mangle_section; we do here to avoid using |
506 | bfd_get_section_by_name since some formats allow multiple | |
507 | sections with the same name. */ | |
508 | isection->output_section = osection; | |
509 | isection->output_offset = 0; | |
510 | ||
46050fe4 ILT |
511 | /* All went well */ |
512 | return; | |
c0367ba5 ILT |
513 | |
514 | loser: | |
46050fe4 ILT |
515 | fprintf (stderr, "%s: %s: section `%s': error in %s: %s\n", |
516 | program_name, | |
517 | bfd_get_filename (ibfd), bfd_section_name (ibfd, isection), | |
c9563567 | 518 | err, bfd_errmsg (bfd_get_error ())); |
46050fe4 ILT |
519 | status = 1; |
520 | } | |
521 | ||
522 | /* Copy the data of input section ISECTION of IBFD | |
523 | to an output section with the same name in OBFD. | |
524 | If stripping then don't copy any relocation info. */ | |
525 | ||
c0367ba5 | 526 | static void |
f7b839f7 | 527 | copy_section (ibfd, isection, obfd) |
46050fe4 ILT |
528 | bfd *ibfd; |
529 | sec_ptr isection; | |
530 | bfd *obfd; | |
c0367ba5 | 531 | { |
46050fe4 ILT |
532 | arelent **relpp; |
533 | int relcount; | |
534 | sec_ptr osection; | |
535 | bfd_size_type size; | |
536 | ||
537 | if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0 | |
538 | && (strip_symbols == strip_debug | |
539 | || strip_symbols == strip_all | |
540 | || discard_locals == locals_all)) | |
541 | { | |
542 | return; | |
543 | } | |
c0367ba5 | 544 | |
46050fe4 ILT |
545 | osection = bfd_get_section_by_name (obfd, |
546 | bfd_section_name (ibfd, isection)); | |
c0367ba5 | 547 | |
46050fe4 | 548 | size = bfd_get_section_size_before_reloc (isection); |
c0367ba5 ILT |
549 | |
550 | if (size == 0) | |
551 | return; | |
552 | ||
553 | if (strip_symbols == strip_all | |
46050fe4 | 554 | || bfd_get_reloc_upper_bound (ibfd, isection) == 0) |
c0367ba5 | 555 | { |
46050fe4 ILT |
556 | bfd_set_reloc (obfd, osection, (arelent **) NULL, 0); |
557 | } | |
558 | else | |
c0367ba5 | 559 | { |
46050fe4 ILT |
560 | relpp = (arelent **) xmalloc (bfd_get_reloc_upper_bound (ibfd, isection)); |
561 | relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp); | |
562 | bfd_set_reloc (obfd, osection, relpp, relcount); | |
c0367ba5 ILT |
563 | } |
564 | ||
565 | isection->_cooked_size = isection->_raw_size; | |
46050fe4 | 566 | isection->reloc_done = true; |
c0367ba5 | 567 | |
46050fe4 | 568 | if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS) |
c0367ba5 | 569 | { |
46050fe4 | 570 | PTR memhunk = (PTR) xmalloc ((unsigned) size); |
c0367ba5 | 571 | |
46050fe4 ILT |
572 | if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0, |
573 | size)) | |
574 | { | |
575 | nonfatal (bfd_get_filename (ibfd)); | |
576 | } | |
c0367ba5 | 577 | |
f7b839f7 DM |
578 | if (copy_byte >= 0) |
579 | filter_bytes (memhunk, &size); | |
580 | ||
46050fe4 ILT |
581 | if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0, |
582 | size)) | |
583 | { | |
584 | nonfatal (bfd_get_filename (obfd)); | |
585 | } | |
586 | free (memhunk); | |
c0367ba5 | 587 | } |
46050fe4 | 588 | } |
c0367ba5 | 589 | |
46050fe4 ILT |
590 | /* The number of bytes to copy at once. */ |
591 | #define COPY_BUF 8192 | |
592 | ||
593 | /* Copy file FROM to file TO, performing no translations. | |
594 | Return 0 if ok, -1 if error. */ | |
595 | ||
596 | static int | |
597 | simple_copy (from, to) | |
598 | char *from, *to; | |
c0367ba5 | 599 | { |
46050fe4 ILT |
600 | int fromfd, tofd, nread; |
601 | char buf[COPY_BUF]; | |
602 | ||
603 | fromfd = open (from, O_RDONLY); | |
604 | if (fromfd < 0) | |
605 | return -1; | |
606 | tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC); | |
607 | if (tofd < 0) | |
608 | { | |
609 | close (fromfd); | |
610 | return -1; | |
611 | } | |
612 | while ((nread = read (fromfd, buf, sizeof buf)) > 0) | |
613 | { | |
614 | if (write (tofd, buf, nread) != nread) | |
615 | { | |
616 | close (fromfd); | |
617 | close (tofd); | |
618 | return -1; | |
619 | } | |
620 | } | |
621 | close (fromfd); | |
622 | close (tofd); | |
623 | if (nread < 0) | |
624 | return -1; | |
625 | return 0; | |
626 | } | |
c0367ba5 | 627 | |
46050fe4 ILT |
628 | #ifndef S_ISLNK |
629 | #ifdef S_IFLNK | |
630 | #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) | |
631 | #else | |
632 | #define S_ISLNK(m) 0 | |
633 | #define lstat stat | |
634 | #endif | |
635 | #endif | |
636 | ||
637 | /* Rename FROM to TO, copying if TO is a link. | |
638 | Assumes that TO already exists, because FROM is a temp file. | |
639 | Return 0 if ok, -1 if error. */ | |
640 | ||
641 | static int | |
642 | smart_rename (from, to) | |
643 | char *from, *to; | |
644 | { | |
645 | struct stat s; | |
646 | int ret = 0; | |
c0367ba5 | 647 | |
46050fe4 ILT |
648 | if (lstat (to, &s)) |
649 | return -1; | |
c0367ba5 | 650 | |
46050fe4 ILT |
651 | /* Use rename only if TO is not a symbolic link and has |
652 | only one hard link. */ | |
653 | if (!S_ISLNK (s.st_mode) && s.st_nlink == 1) | |
654 | { | |
655 | ret = rename (from, to); | |
656 | if (ret == 0) | |
657 | { | |
658 | /* Try to preserve the permission bits and ownership of TO. */ | |
659 | chmod (to, s.st_mode & 07777); | |
660 | chown (to, s.st_uid, s.st_gid); | |
661 | } | |
662 | } | |
663 | else | |
664 | { | |
665 | ret = simple_copy (from, to); | |
666 | if (ret == 0) | |
667 | unlink (from); | |
668 | } | |
669 | return ret; | |
670 | } | |
671 | ||
672 | static int | |
673 | strip_main (argc, argv) | |
674 | int argc; | |
675 | char *argv[]; | |
676 | { | |
677 | char *input_target = NULL, *output_target = NULL; | |
678 | boolean show_version = false; | |
679 | int c, i; | |
680 | ||
681 | while ((c = getopt_long (argc, argv, "I:O:F:sSgxXVv", | |
682 | strip_options, (int *) 0)) != EOF) | |
683 | { | |
684 | switch (c) | |
685 | { | |
686 | case 'I': | |
687 | input_target = optarg; | |
704bbd0d | 688 | break; |
46050fe4 ILT |
689 | case 'O': |
690 | output_target = optarg; | |
691 | break; | |
692 | case 'F': | |
693 | input_target = output_target = optarg; | |
694 | break; | |
695 | case 's': | |
c0367ba5 | 696 | strip_symbols = strip_all; |
46050fe4 ILT |
697 | break; |
698 | case 'S': | |
699 | case 'g': | |
700 | strip_symbols = strip_debug; | |
701 | break; | |
702 | case 'x': | |
703 | discard_locals = locals_all; | |
704 | break; | |
705 | case 'X': | |
706 | discard_locals = locals_start_L; | |
707 | break; | |
708 | case 'v': | |
709 | verbose = true; | |
710 | break; | |
711 | case 'V': | |
712 | show_version = true; | |
713 | break; | |
714 | case 0: | |
715 | break; /* we've been given a long option */ | |
716 | case 'h': | |
717 | strip_usage (stdout, 0); | |
718 | default: | |
719 | strip_usage (stderr, 1); | |
720 | } | |
721 | } | |
722 | ||
723 | if (show_version) | |
724 | { | |
725 | printf ("GNU %s version %s\n", program_name, program_version); | |
726 | exit (0); | |
727 | } | |
c0367ba5 | 728 | |
46050fe4 ILT |
729 | /* Default is to strip all symbols. */ |
730 | if (strip_symbols == strip_undef && discard_locals == locals_undef) | |
731 | strip_symbols = strip_all; | |
732 | ||
733 | if (output_target == (char *) NULL) | |
734 | output_target = input_target; | |
735 | ||
736 | i = optind; | |
737 | if (i == argc) | |
738 | strip_usage (stderr, 1); | |
739 | ||
740 | for (; i < argc; i++) | |
741 | { | |
742 | int hold_status = status; | |
c0367ba5 | 743 | |
46050fe4 ILT |
744 | char *tmpname = make_tempname (argv[i]); |
745 | status = 0; | |
746 | copy_file (argv[i], tmpname, input_target, output_target); | |
747 | if (status == 0) | |
748 | { | |
749 | smart_rename (tmpname, argv[i]); | |
750 | status = hold_status; | |
c0367ba5 | 751 | } |
46050fe4 ILT |
752 | else |
753 | unlink (tmpname); | |
754 | free (tmpname); | |
755 | } | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | static int | |
761 | copy_main (argc, argv) | |
762 | int argc; | |
763 | char *argv[]; | |
764 | { | |
765 | char *input_filename, *output_filename; | |
766 | char *input_target = NULL, *output_target = NULL; | |
767 | boolean show_version = false; | |
768 | int c; | |
769 | ||
f7b839f7 | 770 | while ((c = getopt_long (argc, argv, "b:i:I:s:O:d:F:SgxXVv", |
46050fe4 ILT |
771 | copy_options, (int *) 0)) != EOF) |
772 | { | |
773 | switch (c) | |
774 | { | |
f7b839f7 DM |
775 | case 'b': |
776 | copy_byte = atoi(optarg); | |
777 | if (copy_byte < 0) | |
778 | { | |
779 | fprintf (stderr, "%s: byte number must be non-negative\n", | |
780 | program_name); | |
781 | exit (1); | |
782 | } | |
783 | break; | |
784 | case 'i': | |
785 | interleave = atoi(optarg); | |
786 | if (interleave < 1) | |
787 | { | |
788 | fprintf(stderr, "%s: interleave must be positive\n", | |
789 | program_name); | |
790 | exit (1); | |
791 | } | |
792 | break; | |
c0367ba5 | 793 | case 'I': |
46050fe4 | 794 | case 's': /* "source" - 'I' is preferred */ |
c0367ba5 | 795 | input_target = optarg; |
704bbd0d | 796 | break; |
c0367ba5 | 797 | case 'O': |
46050fe4 | 798 | case 'd': /* "destination" - 'O' is preferred */ |
c0367ba5 ILT |
799 | output_target = optarg; |
800 | break; | |
801 | case 'F': | |
c0367ba5 ILT |
802 | input_target = output_target = optarg; |
803 | break; | |
c0367ba5 ILT |
804 | case 'S': |
805 | strip_symbols = strip_all; | |
806 | break; | |
807 | case 'g': | |
808 | strip_symbols = strip_debug; | |
809 | break; | |
810 | case 'x': | |
811 | discard_locals = locals_all; | |
812 | break; | |
813 | case 'X': | |
814 | discard_locals = locals_start_L; | |
815 | break; | |
816 | case 'v': | |
817 | verbose = true; | |
818 | break; | |
819 | case 'V': | |
820 | show_version = true; | |
821 | break; | |
46050fe4 | 822 | case 0: |
c0367ba5 ILT |
823 | break; /* we've been given a long option */ |
824 | case 'h': | |
825 | copy_usage (stdout, 0); | |
826 | default: | |
827 | copy_usage (stderr, 1); | |
46050fe4 ILT |
828 | } |
829 | } | |
830 | ||
831 | if (show_version) | |
832 | { | |
833 | printf ("GNU %s version %s\n", program_name, program_version); | |
834 | exit (0); | |
835 | } | |
c0367ba5 | 836 | |
f7b839f7 DM |
837 | if (copy_byte >= interleave) |
838 | { | |
839 | fprintf (stderr, "%s: byte number must be less than interleave\n", | |
840 | program_name); | |
841 | exit (1); | |
842 | } | |
843 | ||
46050fe4 ILT |
844 | if (optind == argc || optind + 2 < argc) |
845 | copy_usage (stderr, 1); | |
c0367ba5 ILT |
846 | |
847 | input_filename = argv[optind]; | |
848 | if (optind + 1 < argc) | |
46050fe4 | 849 | output_filename = argv[optind + 1]; |
c0367ba5 ILT |
850 | |
851 | /* Default is to strip no symbols. */ | |
852 | if (strip_symbols == strip_undef && discard_locals == locals_undef) | |
46050fe4 | 853 | strip_symbols = strip_none; |
c0367ba5 ILT |
854 | |
855 | if (output_target == (char *) NULL) | |
856 | output_target = input_target; | |
857 | ||
46050fe4 ILT |
858 | /* If there is no destination file then create a temp and rename |
859 | the result into the input. */ | |
860 | ||
861 | if (output_filename == (char *) NULL) | |
862 | { | |
863 | char *tmpname = make_tempname (input_filename); | |
864 | copy_file (input_filename, tmpname, input_target, output_target); | |
865 | if (status == 0) | |
866 | smart_rename (tmpname, input_filename); | |
867 | else | |
868 | unlink (tmpname); | |
869 | } | |
870 | else | |
871 | { | |
872 | copy_file (input_filename, output_filename, input_target, output_target); | |
873 | } | |
874 | ||
c0367ba5 ILT |
875 | return 0; |
876 | } | |
46050fe4 ILT |
877 | |
878 | int | |
879 | main (argc, argv) | |
880 | int argc; | |
881 | char *argv[]; | |
882 | { | |
883 | program_name = argv[0]; | |
704bbd0d | 884 | xmalloc_set_program_name (program_name); |
46050fe4 ILT |
885 | strip_symbols = strip_undef; |
886 | discard_locals = locals_undef; | |
887 | ||
888 | bfd_init (); | |
889 | ||
890 | if (is_strip < 0) | |
891 | { | |
892 | int i = strlen (program_name); | |
893 | is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip")); | |
894 | } | |
895 | ||
896 | if (is_strip) | |
897 | strip_main (argc, argv); | |
898 | else | |
899 | copy_main (argc, argv); | |
900 | ||
901 | return status; | |
902 | } |