* objcopy.c (keep_specific_list, privatize_specific_list,
[deliverable/binutils-gdb.git] / binutils / objcopy.c
1 /* objcopy.c -- copy object file from input to output, optionally massaging it.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 \f
20 #include "bfd.h"
21 #include "progress.h"
22 #include "bucomm.h"
23 #include "getopt.h"
24 #include "libiberty.h"
25 #include "budbg.h"
26 #include <sys/stat.h>
27
28 #ifdef HAVE_GOOD_UTIME_H
29 #include <utime.h>
30 #else /* ! HAVE_GOOD_UTIME_H */
31 #ifdef HAVE_UTIMES
32 #include <sys/time.h>
33 #endif /* HAVE_UTIMES */
34 #endif /* ! HAVE_GOOD_UTIME_H */
35
36 static void copy_usage PARAMS ((FILE *, int));
37 static void strip_usage PARAMS ((FILE *, int));
38 static flagword parse_flags PARAMS ((const char *));
39 static struct section_list *find_section_list PARAMS ((const char *, boolean));
40 static void setup_section PARAMS ((bfd *, asection *, PTR));
41 static void copy_section PARAMS ((bfd *, asection *, PTR));
42 static void get_sections PARAMS ((bfd *, asection *, PTR));
43 static int compare_section_vma PARAMS ((const PTR, const PTR));
44 static void add_strip_symbol PARAMS ((const char *));
45 static boolean is_strip_symbol PARAMS ((const char *));
46 static boolean is_strip_section PARAMS ((bfd *, asection *));
47 static unsigned int filter_symbols
48 PARAMS ((bfd *, bfd *, asymbol **, asymbol **, long));
49 static void mark_symbols_used_in_relocations PARAMS ((bfd *, asection *, PTR));
50 static void filter_bytes PARAMS ((char *, bfd_size_type *));
51 static boolean write_debugging_info PARAMS ((bfd *, PTR, long *, asymbol ***));
52 static void copy_object PARAMS ((bfd *, bfd *));
53 static void copy_archive PARAMS ((bfd *, bfd *, const char *));
54 static void copy_file
55 PARAMS ((const char *, const char *, const char *, const char *));
56 static int simple_copy PARAMS ((const char *, const char *));
57 static int smart_rename PARAMS ((const char *, const char *));
58 static void set_times PARAMS ((const char *, const struct stat *));
59 static int strip_main PARAMS ((int, char **));
60 static int copy_main PARAMS ((int, char **));
61
62 #define nonfatal(s) {bfd_nonfatal(s); status = 1; return;}
63
64 static asymbol **isympp = NULL; /* Input symbols */
65 static asymbol **osympp = NULL; /* Output symbols that survive stripping */
66
67 /* If `copy_byte' >= 0, copy only that byte of every `interleave' bytes. */
68 static int copy_byte = -1;
69 static int interleave = 4;
70
71 static boolean verbose; /* Print file and target names. */
72 static int status = 0; /* Exit status. */
73
74 enum strip_action
75 {
76 strip_undef,
77 strip_none, /* don't strip */
78 strip_debug, /* strip all debugger symbols */
79 strip_unneeded, /* strip unnecessary symbols */
80 strip_all /* strip all symbols */
81 };
82
83 /* Which symbols to remove. */
84 static enum strip_action strip_symbols;
85
86 enum locals_action
87 {
88 locals_undef,
89 locals_start_L, /* discard locals starting with L */
90 locals_all /* discard all locals */
91 };
92
93 /* Which local symbols to remove. Overrides strip_all. */
94 static enum locals_action discard_locals;
95
96 /* Structure used to hold lists of sections and actions to take. */
97
98 struct section_list
99 {
100 /* Next section to adjust. */
101 struct section_list *next;
102 /* Section name. */
103 const char *name;
104 /* Whether this entry was used. */
105 boolean used;
106 /* Whether to remove this section. */
107 boolean remove;
108 /* Whether to adjust or set VMA. */
109 enum { ignore_vma, adjust_vma, set_vma } adjust;
110 /* Amount to adjust by or set to. */
111 bfd_vma val;
112 /* Whether to set the section flags. */
113 boolean set_flags;
114 /* What to set the section flags to. */
115 flagword flags;
116 };
117
118 static struct section_list *adjust_sections;
119 static boolean sections_removed;
120
121 /* Adjustments to the start address. */
122 static bfd_vma adjust_start = 0;
123 static boolean set_start_set = false;
124 static bfd_vma set_start;
125
126 /* Adjustments to section VMA's. */
127 static bfd_vma adjust_section_vma = 0;
128
129 /* Filling gaps between sections. */
130 static boolean gap_fill_set = false;
131 static bfd_byte gap_fill = 0;
132
133 /* Pad to a given address. */
134 static boolean pad_to_set = false;
135 static bfd_vma pad_to;
136
137 /* List of sections to add. */
138
139 struct section_add
140 {
141 /* Next section to add. */
142 struct section_add *next;
143 /* Name of section to add. */
144 const char *name;
145 /* Name of file holding section contents. */
146 const char *filename;
147 /* Size of file. */
148 size_t size;
149 /* Contents of file. */
150 bfd_byte *contents;
151 /* BFD section, after it has been added. */
152 asection *section;
153 };
154
155 static struct section_add *add_sections;
156
157 /* Whether to convert debugging information. */
158
159 static boolean convert_debugging = false;
160
161 /* Whether to change the leading character in symbol names. */
162
163 static boolean change_leading_char = false;
164
165 /* Whether to remove the leading character from global symbol names. */
166
167 static boolean remove_leading_char = false;
168
169 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
170
171 #define OPTION_ADD_SECTION 150
172 #define OPTION_ADJUST_START (OPTION_ADD_SECTION + 1)
173 #define OPTION_ADJUST_VMA (OPTION_ADJUST_START + 1)
174 #define OPTION_ADJUST_SECTION_VMA (OPTION_ADJUST_VMA + 1)
175 #define OPTION_ADJUST_WARNINGS (OPTION_ADJUST_SECTION_VMA + 1)
176 #define OPTION_CHANGE_LEADING_CHAR (OPTION_ADJUST_WARNINGS + 1)
177 #define OPTION_DEBUGGING (OPTION_CHANGE_LEADING_CHAR + 1)
178 #define OPTION_GAP_FILL (OPTION_DEBUGGING + 1)
179 #define OPTION_NO_ADJUST_WARNINGS (OPTION_GAP_FILL + 1)
180 #define OPTION_PAD_TO (OPTION_NO_ADJUST_WARNINGS + 1)
181 #define OPTION_REMOVE_LEADING_CHAR (OPTION_PAD_TO + 1)
182 #define OPTION_SET_SECTION_FLAGS (OPTION_REMOVE_LEADING_CHAR + 1)
183 #define OPTION_SET_START (OPTION_SET_SECTION_FLAGS + 1)
184 #define OPTION_STRIP_UNNEEDED (OPTION_SET_START + 1)
185 #define OPTION_WEAKEN (OPTION_STRIP_UNNEEDED + 1)
186
187 /* Options to handle if running as "strip". */
188
189 static struct option strip_options[] =
190 {
191 {"discard-all", no_argument, 0, 'x'},
192 {"discard-locals", no_argument, 0, 'X'},
193 {"format", required_argument, 0, 'F'}, /* Obsolete */
194 {"help", no_argument, 0, 'h'},
195 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
196 {"input-target", required_argument, 0, 'I'},
197 {"keep-symbol", required_argument, 0, 'K'},
198 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
199 {"output-target", required_argument, 0, 'O'},
200 {"preserve-dates", no_argument, 0, 'p'},
201 {"remove-section", required_argument, 0, 'R'},
202 {"strip-all", no_argument, 0, 's'},
203 {"strip-debug", no_argument, 0, 'S'},
204 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
205 {"strip-symbol", required_argument, 0, 'N'},
206 {"target", required_argument, 0, 'F'},
207 {"verbose", no_argument, 0, 'v'},
208 {"version", no_argument, 0, 'V'},
209 {0, no_argument, 0, 0}
210 };
211
212 /* Options to handle if running as "objcopy". */
213
214 static struct option copy_options[] =
215 {
216 {"add-section", required_argument, 0, OPTION_ADD_SECTION},
217 {"adjust-start", required_argument, 0, OPTION_ADJUST_START},
218 {"adjust-vma", required_argument, 0, OPTION_ADJUST_VMA},
219 {"adjust-section-vma", required_argument, 0, OPTION_ADJUST_SECTION_VMA},
220 {"adjust-warnings", no_argument, 0, OPTION_ADJUST_WARNINGS},
221 {"byte", required_argument, 0, 'b'},
222 {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
223 {"debugging", no_argument, 0, OPTION_DEBUGGING},
224 {"discard-all", no_argument, 0, 'x'},
225 {"discard-locals", no_argument, 0, 'X'},
226 {"format", required_argument, 0, 'F'}, /* Obsolete */
227 {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
228 {"help", no_argument, 0, 'h'},
229 {"input-format", required_argument, 0, 'I'}, /* Obsolete */
230 {"input-target", required_argument, 0, 'I'},
231 {"interleave", required_argument, 0, 'i'},
232 {"keep-symbol", required_argument, 0, 'K'},
233 {"no-adjust-warnings", no_argument, 0, OPTION_NO_ADJUST_WARNINGS},
234 {"output-format", required_argument, 0, 'O'}, /* Obsolete */
235 {"output-target", required_argument, 0, 'O'},
236 {"pad-to", required_argument, 0, OPTION_PAD_TO},
237 {"preserve-dates", no_argument, 0, 'p'},
238 {"privatize-symbol", required_argument, 0, 'P'},
239 {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
240 {"remove-section", required_argument, 0, 'R'},
241 {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
242 {"set-start", required_argument, 0, OPTION_SET_START},
243 {"strip-all", no_argument, 0, 'S'},
244 {"strip-debug", no_argument, 0, 'g'},
245 {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
246 {"strip-symbol", required_argument, 0, 'N'},
247 {"target", required_argument, 0, 'F'},
248 {"verbose", no_argument, 0, 'v'},
249 {"version", no_argument, 0, 'V'},
250 {"weaken", no_argument, 0, OPTION_WEAKEN},
251 {"weaken-symbol", required_argument, 0, 'W'},
252 {0, no_argument, 0, 0}
253 };
254
255 /* IMPORTS */
256 extern char *program_name;
257
258 /* This flag distinguishes between strip and objcopy:
259 1 means this is 'strip'; 0 means this is 'objcopy'.
260 -1 means if we should use argv[0] to decide. */
261 extern int is_strip;
262
263
264 static void
265 copy_usage (stream, exit_status)
266 FILE *stream;
267 int exit_status;
268 {
269 fprintf (stream, "\
270 Usage: %s [-vVSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-b byte]\n\
271 [-R section] [-i interleave] [--interleave=interleave] [--byte=byte]\n\
272 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
273 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
274 [--discard-locals] [--debugging] [--remove-section=section]\n",
275 program_name);
276 fprintf (stream, "\
277 [--gap-fill=val] [--pad-to=address] [--preserve-dates]\n\
278 [--set-start=val] [--adjust-start=incr]\n\
279 [--adjust-vma=incr] [--adjust-section-vma=section{=,+,-}val]\n\
280 [--adjust-warnings] [--no-adjust-warnings]\n\
281 [--set-section-flags=section=flags] [--add-section=sectionname=filename]\n\
282 [--keep-symbol symbol] [-K symbol] [--strip-symbol symbol] [-N symbol]\n\
283 [--privatize-symbol symbol] [-P symbol] [--weaken-symbol symbol]\n\
284 [-W symbol] [--change-leading-char] [--remove-leading-char] [--weaken]\n\
285 [--verbose] [--version] [--help] in-file [out-file]\n");
286 list_supported_targets (program_name, stream);
287 if (exit_status == 0)
288 fprintf (stream, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
289 exit (exit_status);
290 }
291
292 static void
293 strip_usage (stream, exit_status)
294 FILE *stream;
295 int exit_status;
296 {
297 fprintf (stream, "\
298 Usage: %s [-vVsSpgxX] [-I bfdname] [-O bfdname] [-F bfdname] [-R section]\n\
299 [--input-target=bfdname] [--output-target=bfdname] [--target=bfdname]\n\
300 [--strip-all] [--strip-debug] [--strip-unneeded] [--discard-all]\n\
301 [--discard-locals] [--keep-symbol symbol] [-K symbol]\n\
302 [--strip-symbol symbol] [-N symbol] [--remove-section=section]\n\
303 [-o file] [--preserve-dates] [--verbose] [--version] [--help] file...\n",
304 program_name);
305 list_supported_targets (program_name, stream);
306 if (exit_status == 0)
307 fprintf (stream, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
308 exit (exit_status);
309 }
310
311 /* Parse section flags into a flagword, with a fatal error if the
312 string can't be parsed. */
313
314 static flagword
315 parse_flags (s)
316 const char *s;
317 {
318 flagword ret;
319 const char *snext;
320 int len;
321
322 ret = SEC_NO_FLAGS;
323
324 do
325 {
326 snext = strchr (s, ',');
327 if (snext == NULL)
328 len = strlen (s);
329 else
330 {
331 len = snext - s;
332 ++snext;
333 }
334
335 #define PARSE_FLAG(fname,fval) if (strncmp (fname, s, len) == 0) ret |= fval;
336 PARSE_FLAG ("alloc", SEC_ALLOC);
337 PARSE_FLAG ("load", SEC_LOAD);
338 PARSE_FLAG ("readonly", SEC_READONLY);
339 PARSE_FLAG ("code", SEC_CODE);
340 PARSE_FLAG ("data", SEC_DATA);
341 PARSE_FLAG ("rom", SEC_ROM);
342 #undef PARSE_FLAG
343
344 s = snext;
345 }
346 while (s != NULL);
347
348 return ret;
349 }
350
351 /* Find and optionally add an entry in the adjust_sections list. */
352
353 static struct section_list *
354 find_section_list (name, add)
355 const char *name;
356 boolean add;
357 {
358 register struct section_list *p;
359
360 for (p = adjust_sections; p != NULL; p = p->next)
361 if (strcmp (p->name, name) == 0)
362 return p;
363
364 if (! add)
365 return NULL;
366
367 p = (struct section_list *) xmalloc (sizeof (struct section_list));
368 p->name = name;
369 p->used = false;
370 p->remove = false;
371 p->adjust = ignore_vma;
372 p->val = 0;
373 p->set_flags = false;
374 p->flags = 0;
375
376 p->next = adjust_sections;
377 adjust_sections = p;
378
379 return p;
380 }
381
382 /* Make a list of symbols to explicitly strip out, or to keep. A
383 linked list is good enough for a small number from the command
384 line, but this will slow things down a lot if many symbols are
385 being deleted. */
386
387 struct symlist
388 {
389 const char *name;
390 struct symlist *next;
391 };
392
393 /* List of symbols to strip, keep, privatize, and weaken. */
394
395 static struct symlist *strip_specific_list = NULL;
396 static struct symlist *keep_specific_list = NULL;
397 static struct symlist *privatize_specific_list = NULL;
398 static struct symlist *weaken_specific_list = NULL;
399
400 /* If this is true, we weaken global symbols (set BSF_WEAK). */
401
402 static boolean weaken = false;
403
404 /* Add a symbol to strip_specific_list. */
405
406 static void
407 add_specific_symbol (name, list)
408 const char *name;
409 struct symlist **list;
410 {
411 struct symlist *tmp_list;
412
413 tmp_list = (struct symlist *) xmalloc (sizeof (struct symlist));
414 tmp_list->name = name;
415 tmp_list->next = *list;
416 *list = tmp_list;
417 }
418
419 /* See whether a symbol should be stripped or kept based on
420 strip_specific_list and keep_symbols. */
421
422 static boolean
423 is_specified_symbol (name, list)
424 const char *name;
425 struct symlist *list;
426 {
427 struct symlist *tmp_list;
428
429 for (tmp_list = list; tmp_list; tmp_list = tmp_list->next)
430 {
431 if (strcmp (name, tmp_list->name) == 0)
432 return true;
433 }
434 return false;
435 }
436
437 /* See if a section is being removed. */
438
439 static boolean
440 is_strip_section (abfd, sec)
441 bfd *abfd;
442 asection *sec;
443 {
444 struct section_list *p;
445
446 if ((bfd_get_section_flags (abfd, sec) & SEC_DEBUGGING) != 0
447 && (strip_symbols == strip_debug
448 || strip_symbols == strip_unneeded
449 || strip_symbols == strip_all
450 || discard_locals == locals_all
451 || convert_debugging))
452 return true;
453
454 if (! sections_removed)
455 return false;
456 p = find_section_list (bfd_get_section_name (abfd, sec), false);
457 return p != NULL && p->remove ? true : false;
458 }
459
460 /* Choose which symbol entries to copy; put the result in OSYMS.
461 We don't copy in place, because that confuses the relocs.
462 Return the number of symbols to print. */
463
464 static unsigned int
465 filter_symbols (abfd, obfd, osyms, isyms, symcount)
466 bfd *abfd;
467 bfd *obfd;
468 asymbol **osyms, **isyms;
469 long symcount;
470 {
471 register asymbol **from = isyms, **to = osyms;
472 long src_count = 0, dst_count = 0;
473
474 for (; src_count < symcount; src_count++)
475 {
476 asymbol *sym = from[src_count];
477 flagword flags = sym->flags;
478 const char *name = bfd_asymbol_name (sym);
479 int keep;
480
481 if (change_leading_char
482 && (bfd_get_symbol_leading_char (abfd)
483 != bfd_get_symbol_leading_char (obfd))
484 && (bfd_get_symbol_leading_char (abfd) == '\0'
485 || (name[0] == bfd_get_symbol_leading_char (abfd))))
486 {
487 if (bfd_get_symbol_leading_char (obfd) == '\0')
488 name = bfd_asymbol_name (sym) = name + 1;
489 else
490 {
491 char *n;
492
493 n = xmalloc (strlen (name) + 2);
494 n[0] = bfd_get_symbol_leading_char (obfd);
495 if (bfd_get_symbol_leading_char (abfd) == '\0')
496 strcpy (n + 1, name);
497 else
498 strcpy (n + 1, name + 1);
499 name = bfd_asymbol_name (sym) = n;
500 }
501 }
502
503 if (remove_leading_char
504 && ((flags & BSF_GLOBAL) != 0
505 || (flags & BSF_WEAK) != 0
506 || bfd_is_und_section (bfd_get_section (sym))
507 || bfd_is_com_section (bfd_get_section (sym)))
508 && name[0] == bfd_get_symbol_leading_char (abfd))
509 name = bfd_asymbol_name (sym) = name + 1;
510
511 if ((flags & BSF_KEEP) != 0) /* Used in relocation. */
512 keep = 1;
513 else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
514 || (flags & BSF_WEAK) != 0
515 || bfd_is_und_section (bfd_get_section (sym))
516 || bfd_is_com_section (bfd_get_section (sym)))
517 keep = strip_symbols != strip_unneeded;
518 else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
519 keep = (strip_symbols != strip_debug
520 && strip_symbols != strip_unneeded
521 && ! convert_debugging);
522 else /* Local symbol. */
523 keep = (strip_symbols != strip_unneeded
524 && (discard_locals != locals_all
525 && (discard_locals != locals_start_L
526 || ! bfd_is_local_label (abfd, sym))));
527
528 if (keep && is_specified_symbol (name, strip_specific_list))
529 keep = 0;
530 if (!keep && is_specified_symbol (name, keep_specific_list))
531 keep = 1;
532 if (keep && is_strip_section (abfd, bfd_get_section (sym)))
533 keep = 0;
534
535 if (keep && (flags & BSF_GLOBAL) != 0
536 && (weaken || is_specified_symbol (name, weaken_specific_list)))
537 {
538 sym->flags &=~ BSF_GLOBAL;
539 sym->flags |= BSF_WEAK;
540 }
541 if (keep && (flags & (BSF_GLOBAL | BSF_WEAK))
542 && is_specified_symbol (name, privatize_specific_list))
543 {
544 sym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
545 sym->flags |= BSF_LOCAL;
546 }
547
548 if (keep)
549 to[dst_count++] = sym;
550 }
551
552 to[dst_count] = NULL;
553
554 return dst_count;
555 }
556
557 /* Keep only every `copy_byte'th byte in MEMHUNK, which is *SIZE bytes long.
558 Adjust *SIZE. */
559
560 static void
561 filter_bytes (memhunk, size)
562 char *memhunk;
563 bfd_size_type *size;
564 {
565 char *from = memhunk + copy_byte, *to = memhunk, *end = memhunk + *size;
566
567 for (; from < end; from += interleave)
568 *to++ = *from;
569 *size /= interleave;
570 }
571
572 /* Copy object file IBFD onto OBFD. */
573
574 static void
575 copy_object (ibfd, obfd)
576 bfd *ibfd;
577 bfd *obfd;
578 {
579 bfd_vma start;
580 long symcount;
581 asection **osections = NULL;
582 bfd_size_type *gaps = NULL;
583 bfd_size_type max_gap = 0;
584
585 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
586 {
587 nonfatal (bfd_get_filename (obfd));
588 }
589
590 if (verbose)
591 printf ("copy from %s(%s) to %s(%s)\n",
592 bfd_get_filename(ibfd), bfd_get_target(ibfd),
593 bfd_get_filename(obfd), bfd_get_target(obfd));
594
595 if (set_start_set)
596 start = set_start;
597 else
598 start = bfd_get_start_address (ibfd);
599 start += adjust_start;
600
601 if (!bfd_set_start_address (obfd, start)
602 || !bfd_set_file_flags (obfd,
603 (bfd_get_file_flags (ibfd)
604 & bfd_applicable_file_flags (obfd))))
605 {
606 nonfatal (bfd_get_filename (ibfd));
607 }
608
609 /* Copy architecture of input file to output file */
610 if (!bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
611 bfd_get_mach (ibfd)))
612 {
613 fprintf (stderr,
614 "Warning: Output file cannot represent architecture %s\n",
615 bfd_printable_arch_mach (bfd_get_arch (ibfd),
616 bfd_get_mach (ibfd)));
617 }
618 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
619 {
620 nonfatal (bfd_get_filename(ibfd));
621 }
622
623 if (isympp)
624 free (isympp);
625 if (osympp != isympp)
626 free (osympp);
627
628 /* bfd mandates that all output sections be created and sizes set before
629 any output is done. Thus, we traverse all sections multiple times. */
630 bfd_map_over_sections (ibfd, setup_section, (void *) obfd);
631
632 if (add_sections != NULL)
633 {
634 struct section_add *padd;
635 struct section_list *pset;
636
637 for (padd = add_sections; padd != NULL; padd = padd->next)
638 {
639 padd->section = bfd_make_section (obfd, padd->name);
640 if (padd->section == NULL)
641 {
642 fprintf (stderr, "%s: can't create section `%s': %s\n",
643 program_name, padd->name,
644 bfd_errmsg (bfd_get_error ()));
645 status = 1;
646 return;
647 }
648 else
649 {
650 flagword flags;
651
652 if (! bfd_set_section_size (obfd, padd->section, padd->size))
653 nonfatal (bfd_get_filename (obfd));
654
655 pset = find_section_list (padd->name, false);
656 if (pset != NULL)
657 pset->used = true;
658
659 if (pset != NULL && pset->set_flags)
660 flags = pset->flags | SEC_HAS_CONTENTS;
661 else
662 flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
663 if (! bfd_set_section_flags (obfd, padd->section, flags))
664 nonfatal (bfd_get_filename (obfd));
665
666 if (pset != NULL
667 && (pset->adjust == adjust_vma
668 || pset->adjust == set_vma))
669 {
670 if (! bfd_set_section_vma (obfd, padd->section, pset->val))
671 nonfatal (bfd_get_filename (obfd));
672 }
673 }
674 }
675 }
676
677 if (gap_fill_set || pad_to_set)
678 {
679 asection **set;
680 unsigned int c, i;
681
682 /* We must fill in gaps between the sections and/or we must pad
683 the last section to a specified address. We do this by
684 grabbing a list of the sections, sorting them by VMA, and
685 increasing the section sizes as required to fill the gaps.
686 We write out the gap contents below. */
687
688 c = bfd_count_sections (obfd);
689 osections = (asection **) xmalloc (c * sizeof (asection *));
690 set = osections;
691 bfd_map_over_sections (obfd, get_sections, (void *) &set);
692
693 qsort (osections, c, sizeof (asection *), compare_section_vma);
694
695 gaps = (bfd_size_type *) xmalloc (c * sizeof (bfd_size_type));
696 memset (gaps, 0, c * sizeof (bfd_size_type));
697
698 if (gap_fill_set)
699 {
700 for (i = 0; i < c - 1; i++)
701 {
702 flagword flags;
703 bfd_size_type size;
704 bfd_vma gap_start, gap_stop;
705
706 flags = bfd_get_section_flags (obfd, osections[i]);
707 if ((flags & SEC_HAS_CONTENTS) == 0
708 || (flags & SEC_LOAD) == 0)
709 continue;
710
711 size = bfd_section_size (obfd, osections[i]);
712 gap_start = bfd_section_vma (obfd, osections[i]) + size;
713 gap_stop = bfd_section_vma (obfd, osections[i + 1]);
714 if (gap_start < gap_stop)
715 {
716 if (! bfd_set_section_size (obfd, osections[i],
717 size + (gap_stop - gap_start)))
718 {
719 fprintf (stderr, "%s: Can't fill gap after %s: %s\n",
720 program_name,
721 bfd_get_section_name (obfd, osections[i]),
722 bfd_errmsg (bfd_get_error()));
723 status = 1;
724 break;
725 }
726 gaps[i] = gap_stop - gap_start;
727 if (max_gap < gap_stop - gap_start)
728 max_gap = gap_stop - gap_start;
729 }
730 }
731 }
732
733 if (pad_to_set)
734 {
735 bfd_vma vma;
736 bfd_size_type size;
737
738 vma = bfd_section_vma (obfd, osections[c - 1]);
739 size = bfd_section_size (obfd, osections[c - 1]);
740 if (vma + size < pad_to)
741 {
742 if (! bfd_set_section_size (obfd, osections[c - 1],
743 pad_to - vma))
744 {
745 fprintf (stderr, "%s: Can't add padding to %s: %s\n",
746 program_name,
747 bfd_get_section_name (obfd, osections[c - 1]),
748 bfd_errmsg (bfd_get_error ()));
749 status = 1;
750 }
751 else
752 {
753 gaps[c - 1] = pad_to - (vma + size);
754 if (max_gap < pad_to - (vma + size))
755 max_gap = pad_to - (vma + size);
756 }
757 }
758 }
759 }
760
761 /* Symbol filtering must happen after the output sections have
762 been created, but before their contents are set. */
763 if (strip_symbols == strip_all)
764 {
765 osympp = isympp = NULL;
766 symcount = 0;
767 }
768 else
769 {
770 long symsize;
771 PTR dhandle = NULL;
772
773 symsize = bfd_get_symtab_upper_bound (ibfd);
774 if (symsize < 0)
775 {
776 nonfatal (bfd_get_filename (ibfd));
777 }
778
779 osympp = isympp = (asymbol **) xmalloc (symsize);
780 symcount = bfd_canonicalize_symtab (ibfd, isympp);
781 if (symcount < 0)
782 {
783 nonfatal (bfd_get_filename (ibfd));
784 }
785
786 if (convert_debugging)
787 dhandle = read_debugging_info (ibfd, isympp, symcount);
788
789 if (strip_symbols == strip_debug
790 || strip_symbols == strip_unneeded
791 || discard_locals != locals_undef
792 || strip_specific_list != NULL
793 || keep_specific_list != NULL
794 || privatize_specific_list != NULL
795 || weaken_specific_list != NULL
796 || sections_removed
797 || convert_debugging
798 || change_leading_char
799 || remove_leading_char
800 || weaken)
801 {
802 /* Mark symbols used in output relocations so that they
803 are kept, even if they are local labels or static symbols.
804
805 Note we iterate over the input sections examining their
806 relocations since the relocations for the output sections
807 haven't been set yet. mark_symbols_used_in_relocations will
808 ignore input sections which have no corresponding output
809 section. */
810 bfd_map_over_sections (ibfd,
811 mark_symbols_used_in_relocations,
812 (PTR)isympp);
813 osympp = (asymbol **) xmalloc ((symcount + 1) * sizeof (asymbol *));
814 symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
815 }
816
817 if (convert_debugging && dhandle != NULL)
818 {
819 if (! write_debugging_info (obfd, dhandle, &symcount, &osympp))
820 {
821 status = 1;
822 return;
823 }
824 }
825 }
826
827 bfd_set_symtab (obfd, osympp, symcount);
828
829 /* This has to happen after the symbol table has been set. */
830 bfd_map_over_sections (ibfd, copy_section, (void *) obfd);
831
832 if (add_sections != NULL)
833 {
834 struct section_add *padd;
835
836 for (padd = add_sections; padd != NULL; padd = padd->next)
837 {
838 if (! bfd_set_section_contents (obfd, padd->section,
839 (PTR) padd->contents,
840 (file_ptr) 0,
841 (bfd_size_type) padd->size))
842 nonfatal (bfd_get_filename (obfd));
843 }
844 }
845
846 if (gap_fill_set || pad_to_set)
847 {
848 bfd_byte *buf;
849 int c, i;
850
851 /* Fill in the gaps. */
852
853 if (max_gap > 8192)
854 max_gap = 8192;
855 buf = (bfd_byte *) xmalloc (max_gap);
856 memset (buf, gap_fill, (size_t) max_gap);
857
858 c = bfd_count_sections (obfd);
859 for (i = 0; i < c; i++)
860 {
861 if (gaps[i] != 0)
862 {
863 bfd_size_type left;
864 file_ptr off;
865
866 left = gaps[i];
867 off = bfd_section_size (obfd, osections[i]) - left;
868 while (left > 0)
869 {
870 bfd_size_type now;
871
872 if (left > 8192)
873 now = 8192;
874 else
875 now = left;
876 if (! bfd_set_section_contents (obfd, osections[i], buf,
877 off, now))
878 {
879 nonfatal (bfd_get_filename (obfd));
880 }
881 left -= now;
882 off += now;
883 }
884 }
885 }
886 }
887
888 /* Allow the BFD backend to copy any private data it understands
889 from the input BFD to the output BFD. This is done last to
890 permit the routine to look at the filtered symbol table, which is
891 important for the ECOFF code at least. */
892 if (!bfd_copy_private_bfd_data (ibfd, obfd))
893 {
894 fprintf (stderr, "%s: %s: error copying private BFD data: %s\n",
895 program_name, bfd_get_filename (obfd),
896 bfd_errmsg (bfd_get_error ()));
897 status = 1;
898 return;
899 }
900 }
901
902 /* Read each archive element in turn from IBFD, copy the
903 contents to temp file, and keep the temp file handle. */
904
905 static void
906 copy_archive (ibfd, obfd, output_target)
907 bfd *ibfd;
908 bfd *obfd;
909 const char *output_target;
910 {
911 struct name_list
912 {
913 struct name_list *next;
914 char *name;
915 bfd *obfd;
916 } *list, *l;
917 bfd **ptr = &obfd->archive_head;
918 bfd *this_element;
919 char *dir = make_tempname (bfd_get_filename (obfd));
920
921 /* Make a temp directory to hold the contents. */
922 if (mkdir (dir, 0700) != 0)
923 {
924 fatal ("cannot mkdir %s for archive copying (error: %s)",
925 dir, strerror (errno));
926 }
927 obfd->has_armap = ibfd->has_armap;
928
929 list = NULL;
930
931 this_element = bfd_openr_next_archived_file (ibfd, NULL);
932 while (this_element != (bfd *) NULL)
933 {
934 /* Create an output file for this member. */
935 char *output_name = concat (dir, "/", bfd_get_filename(this_element),
936 (char *) NULL);
937 bfd *output_bfd = bfd_openw (output_name, output_target);
938 bfd *last_element;
939
940 l = (struct name_list *) xmalloc (sizeof (struct name_list));
941 l->name = output_name;
942 l->next = list;
943 list = l;
944
945 if (output_bfd == (bfd *) NULL)
946 {
947 nonfatal (output_name);
948 }
949 if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
950 {
951 nonfatal (bfd_get_filename (obfd));
952 }
953
954 if (bfd_check_format (this_element, bfd_object) == true)
955 {
956 copy_object (this_element, output_bfd);
957 }
958
959 bfd_close (output_bfd);
960
961 /* Open the newly output file and attach to our list. */
962 output_bfd = bfd_openr (output_name, output_target);
963
964 l->obfd = output_bfd;
965
966 *ptr = output_bfd;
967 ptr = &output_bfd->next;
968
969 last_element = this_element;
970
971 this_element = bfd_openr_next_archived_file (ibfd, last_element);
972
973 bfd_close (last_element);
974 }
975 *ptr = (bfd *) NULL;
976
977 if (!bfd_close (obfd))
978 {
979 nonfatal (bfd_get_filename (obfd));
980 }
981
982 if (!bfd_close (ibfd))
983 {
984 nonfatal (bfd_get_filename (ibfd));
985 }
986
987 /* Delete all the files that we opened. */
988 for (l = list; l != NULL; l = l->next)
989 {
990 bfd_close (l->obfd);
991 unlink (l->name);
992 }
993 rmdir (dir);
994 }
995
996 /* The top-level control. */
997
998 static void
999 copy_file (input_filename, output_filename, input_target, output_target)
1000 const char *input_filename;
1001 const char *output_filename;
1002 const char *input_target;
1003 const char *output_target;
1004 {
1005 bfd *ibfd;
1006 char **matching;
1007
1008 /* To allow us to do "strip *" without dying on the first
1009 non-object file, failures are nonfatal. */
1010
1011 ibfd = bfd_openr (input_filename, input_target);
1012 if (ibfd == NULL)
1013 {
1014 nonfatal (input_filename);
1015 }
1016
1017 if (bfd_check_format (ibfd, bfd_archive))
1018 {
1019 bfd *obfd;
1020
1021 /* bfd_get_target does not return the correct value until
1022 bfd_check_format succeeds. */
1023 if (output_target == NULL)
1024 output_target = bfd_get_target (ibfd);
1025
1026 obfd = bfd_openw (output_filename, output_target);
1027 if (obfd == NULL)
1028 {
1029 nonfatal (output_filename);
1030 }
1031 copy_archive (ibfd, obfd, output_target);
1032 }
1033 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1034 {
1035 bfd *obfd;
1036
1037 /* bfd_get_target does not return the correct value until
1038 bfd_check_format succeeds. */
1039 if (output_target == NULL)
1040 output_target = bfd_get_target (ibfd);
1041
1042 obfd = bfd_openw (output_filename, output_target);
1043 if (obfd == NULL)
1044 {
1045 nonfatal (output_filename);
1046 }
1047
1048 copy_object (ibfd, obfd);
1049
1050 if (!bfd_close (obfd))
1051 {
1052 nonfatal (output_filename);
1053 }
1054
1055 if (!bfd_close (ibfd))
1056 {
1057 nonfatal (input_filename);
1058 }
1059 }
1060 else
1061 {
1062 bfd_nonfatal (input_filename);
1063 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1064 {
1065 list_matching_formats (matching);
1066 free (matching);
1067 }
1068 status = 1;
1069 }
1070 }
1071
1072 /* Create a section in OBFD with the same name and attributes
1073 as ISECTION in IBFD. */
1074
1075 static void
1076 setup_section (ibfd, isection, obfdarg)
1077 bfd *ibfd;
1078 sec_ptr isection;
1079 PTR obfdarg;
1080 {
1081 bfd *obfd = (bfd *) obfdarg;
1082 struct section_list *p;
1083 sec_ptr osection;
1084 bfd_vma vma;
1085 bfd_vma lma;
1086 flagword flags;
1087 char *err;
1088
1089 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1090 && (strip_symbols == strip_debug
1091 || strip_symbols == strip_unneeded
1092 || strip_symbols == strip_all
1093 || discard_locals == locals_all
1094 || convert_debugging))
1095 return;
1096
1097 p = find_section_list (bfd_section_name (ibfd, isection), false);
1098 if (p != NULL)
1099 p->used = true;
1100
1101 if (p != NULL && p->remove)
1102 return;
1103
1104 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1105 if (osection == NULL)
1106 {
1107 err = "making";
1108 goto loser;
1109 }
1110
1111 if (!bfd_set_section_size (obfd,
1112 osection,
1113 bfd_section_size (ibfd, isection)))
1114 {
1115 err = "size";
1116 goto loser;
1117 }
1118
1119 vma = bfd_section_vma (ibfd, isection);
1120 if (p != NULL && p->adjust == adjust_vma)
1121 vma += p->val;
1122 else if (p != NULL && p->adjust == set_vma)
1123 vma = p->val;
1124 else
1125 vma += adjust_section_vma;
1126 if (! bfd_set_section_vma (obfd, osection, vma))
1127 {
1128 err = "vma";
1129 goto loser;
1130 }
1131
1132 lma = isection->lma;
1133 if (p != NULL && p->adjust == adjust_vma)
1134 lma += p->val;
1135 else if (p != NULL && p->adjust == set_vma)
1136 lma = p->val;
1137 else
1138 lma += adjust_section_vma;
1139 osection->lma = lma;
1140
1141 if (bfd_set_section_alignment (obfd,
1142 osection,
1143 bfd_section_alignment (ibfd, isection))
1144 == false)
1145 {
1146 err = "alignment";
1147 goto loser;
1148 }
1149
1150 flags = bfd_get_section_flags (ibfd, isection);
1151 if (p != NULL && p->set_flags)
1152 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1153 if (!bfd_set_section_flags (obfd, osection, flags))
1154 {
1155 err = "flags";
1156 goto loser;
1157 }
1158
1159 /* This used to be mangle_section; we do here to avoid using
1160 bfd_get_section_by_name since some formats allow multiple
1161 sections with the same name. */
1162 isection->output_section = osection;
1163 isection->output_offset = 0;
1164
1165 /* Allow the BFD backend to copy any private data it understands
1166 from the input section to the output section. */
1167 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1168 {
1169 err = "private data";
1170 goto loser;
1171 }
1172
1173 /* All went well */
1174 return;
1175
1176 loser:
1177 fprintf (stderr, "%s: %s: section `%s': error in %s: %s\n",
1178 program_name,
1179 bfd_get_filename (ibfd), bfd_section_name (ibfd, isection),
1180 err, bfd_errmsg (bfd_get_error ()));
1181 status = 1;
1182 }
1183
1184 /* Copy the data of input section ISECTION of IBFD
1185 to an output section with the same name in OBFD.
1186 If stripping then don't copy any relocation info. */
1187
1188 static void
1189 copy_section (ibfd, isection, obfdarg)
1190 bfd *ibfd;
1191 sec_ptr isection;
1192 PTR obfdarg;
1193 {
1194 bfd *obfd = (bfd *) obfdarg;
1195 struct section_list *p;
1196 arelent **relpp;
1197 long relcount;
1198 sec_ptr osection;
1199 bfd_size_type size;
1200
1201 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1202 && (strip_symbols == strip_debug
1203 || strip_symbols == strip_unneeded
1204 || strip_symbols == strip_all
1205 || discard_locals == locals_all
1206 || convert_debugging))
1207 {
1208 return;
1209 }
1210
1211 p = find_section_list (bfd_section_name (ibfd, isection), false);
1212
1213 if (p != NULL && p->remove)
1214 return;
1215
1216 osection = isection->output_section;
1217 size = bfd_get_section_size_before_reloc (isection);
1218
1219 if (size == 0 || osection == 0)
1220 return;
1221
1222 if (strip_symbols == strip_all)
1223 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1224 else
1225 {
1226 long relsize;
1227
1228 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1229 if (relsize < 0)
1230 {
1231 nonfatal (bfd_get_filename (ibfd));
1232 }
1233 if (relsize == 0)
1234 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1235 else
1236 {
1237 relpp = (arelent **) xmalloc (relsize);
1238 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1239 if (relcount < 0)
1240 {
1241 nonfatal (bfd_get_filename (ibfd));
1242 }
1243 bfd_set_reloc (obfd, osection, relpp, relcount);
1244 }
1245 }
1246
1247 isection->_cooked_size = isection->_raw_size;
1248 isection->reloc_done = true;
1249
1250 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1251 {
1252 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1253
1254 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1255 size))
1256 {
1257 nonfatal (bfd_get_filename (ibfd));
1258 }
1259
1260 if (copy_byte >= 0)
1261 {
1262 filter_bytes (memhunk, &size);
1263 /* The section has gotten smaller. */
1264 if (!bfd_set_section_size (obfd, osection, size))
1265 nonfatal (bfd_get_filename (obfd));
1266 }
1267
1268 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1269 size))
1270 {
1271 nonfatal (bfd_get_filename (obfd));
1272 }
1273 free (memhunk);
1274 }
1275 }
1276
1277 /* Get all the sections. This is used when --gap-fill or --pad-to is
1278 used. */
1279
1280 static void
1281 get_sections (obfd, osection, secppparg)
1282 bfd *obfd;
1283 asection *osection;
1284 PTR secppparg;
1285 {
1286 asection ***secppp = (asection ***) secppparg;
1287
1288 **secppp = osection;
1289 ++(*secppp);
1290 }
1291
1292 /* Sort sections by VMA. This is called via qsort, and is used when
1293 --gap-fill or --pad-to is used. We force non loadable or empty
1294 sections to the front, where they are easier to ignore. */
1295
1296 static int
1297 compare_section_vma (arg1, arg2)
1298 const PTR arg1;
1299 const PTR arg2;
1300 {
1301 const asection **sec1 = (const asection **) arg1;
1302 const asection **sec2 = (const asection **) arg2;
1303 flagword flags1, flags2;
1304
1305 /* Sort non loadable sections to the front. */
1306 flags1 = (*sec1)->flags;
1307 flags2 = (*sec2)->flags;
1308 if ((flags1 & SEC_HAS_CONTENTS) == 0
1309 || (flags1 & SEC_LOAD) == 0)
1310 {
1311 if ((flags2 & SEC_HAS_CONTENTS) != 0
1312 && (flags2 & SEC_LOAD) != 0)
1313 return -1;
1314 }
1315 else
1316 {
1317 if ((flags2 & SEC_HAS_CONTENTS) == 0
1318 || (flags2 & SEC_LOAD) == 0)
1319 return 1;
1320 }
1321
1322 /* Sort sections by VMA. */
1323 if ((*sec1)->vma > (*sec2)->vma)
1324 return 1;
1325 else if ((*sec1)->vma < (*sec2)->vma)
1326 return -1;
1327
1328 /* Sort sections with the same VMA by size. */
1329 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1330 return 1;
1331 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1332 return -1;
1333
1334 return 0;
1335 }
1336
1337 /* Mark all the symbols which will be used in output relocations with
1338 the BSF_KEEP flag so that those symbols will not be stripped.
1339
1340 Ignore relocations which will not appear in the output file. */
1341
1342 static void
1343 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1344 bfd *ibfd;
1345 sec_ptr isection;
1346 PTR symbolsarg;
1347 {
1348 asymbol **symbols = (asymbol **) symbolsarg;
1349 long relsize;
1350 arelent **relpp;
1351 long relcount, i;
1352
1353 /* Ignore an input section with no corresponding output section. */
1354 if (isection->output_section == NULL)
1355 return;
1356
1357 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1358 if (relsize < 0)
1359 bfd_fatal (bfd_get_filename (ibfd));
1360
1361 if (relsize == 0)
1362 return;
1363
1364 relpp = (arelent **) xmalloc (relsize);
1365 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1366 if (relcount < 0)
1367 bfd_fatal (bfd_get_filename (ibfd));
1368
1369 /* Examine each symbol used in a relocation. If it's not one of the
1370 special bfd section symbols, then mark it with BSF_KEEP. */
1371 for (i = 0; i < relcount; i++)
1372 {
1373 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1374 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1375 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1376 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1377 }
1378
1379 if (relpp != NULL)
1380 free (relpp);
1381 }
1382
1383 /* Write out debugging information. */
1384
1385 static boolean
1386 write_debugging_info (obfd, dhandle, symcountp, symppp)
1387 bfd *obfd;
1388 PTR dhandle;
1389 long *symcountp;
1390 asymbol ***symppp;
1391 {
1392 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1393 return write_ieee_debugging_info (obfd, dhandle);
1394
1395 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1396 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1397 {
1398 bfd_byte *syms, *strings;
1399 bfd_size_type symsize, stringsize;
1400 asection *stabsec, *stabstrsec;
1401
1402 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1403 &symsize, &strings,
1404 &stringsize))
1405 return false;
1406
1407 stabsec = bfd_make_section (obfd, ".stab");
1408 stabstrsec = bfd_make_section (obfd, ".stabstr");
1409 if (stabsec == NULL
1410 || stabstrsec == NULL
1411 || ! bfd_set_section_size (obfd, stabsec, symsize)
1412 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1413 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1414 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1415 || ! bfd_set_section_flags (obfd, stabsec,
1416 (SEC_HAS_CONTENTS
1417 | SEC_READONLY
1418 | SEC_DEBUGGING))
1419 || ! bfd_set_section_flags (obfd, stabstrsec,
1420 (SEC_HAS_CONTENTS
1421 | SEC_READONLY
1422 | SEC_DEBUGGING)))
1423 {
1424 fprintf (stderr, "%s: can't create debugging section: %s\n",
1425 bfd_get_filename (obfd), bfd_errmsg (bfd_get_error ()));
1426 return false;
1427 }
1428
1429 /* We can get away with setting the section contents now because
1430 the next thing the caller is going to do is copy over the
1431 real sections. We may someday have to split the contents
1432 setting out of this function. */
1433 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1434 symsize)
1435 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1436 (file_ptr) 0, stringsize))
1437 {
1438 fprintf (stderr, "%s: can't set debugging section contents: %s\n",
1439 bfd_get_filename (obfd), bfd_errmsg (bfd_get_error ()));
1440 return false;
1441 }
1442
1443 return true;
1444 }
1445
1446 fprintf (stderr,
1447 "%s: don't know how to write debugging information for %s\n",
1448 bfd_get_filename (obfd), bfd_get_target (obfd));
1449 return false;
1450 }
1451
1452 /* The number of bytes to copy at once. */
1453 #define COPY_BUF 8192
1454
1455 /* Copy file FROM to file TO, performing no translations.
1456 Return 0 if ok, -1 if error. */
1457
1458 static int
1459 simple_copy (from, to)
1460 const char *from;
1461 const char *to;
1462 {
1463 int fromfd, tofd, nread;
1464 int saved;
1465 char buf[COPY_BUF];
1466
1467 fromfd = open (from, O_RDONLY);
1468 if (fromfd < 0)
1469 return -1;
1470 tofd = creat (to, 0777);
1471 if (tofd < 0)
1472 {
1473 saved = errno;
1474 close (fromfd);
1475 errno = saved;
1476 return -1;
1477 }
1478 while ((nread = read (fromfd, buf, sizeof buf)) > 0)
1479 {
1480 if (write (tofd, buf, nread) != nread)
1481 {
1482 saved = errno;
1483 close (fromfd);
1484 close (tofd);
1485 errno = saved;
1486 return -1;
1487 }
1488 }
1489 saved = errno;
1490 close (fromfd);
1491 close (tofd);
1492 if (nread < 0)
1493 {
1494 errno = saved;
1495 return -1;
1496 }
1497 return 0;
1498 }
1499
1500 #ifndef S_ISLNK
1501 #ifdef S_IFLNK
1502 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1503 #else
1504 #define S_ISLNK(m) 0
1505 #define lstat stat
1506 #endif
1507 #endif
1508
1509 /* Rename FROM to TO, copying if TO is a link.
1510 Assumes that TO already exists, because FROM is a temp file.
1511 Return 0 if ok, -1 if error. */
1512
1513 static int
1514 smart_rename (from, to)
1515 const char *from;
1516 const char *to;
1517 {
1518 struct stat s;
1519 int ret = 0;
1520
1521 if (lstat (to, &s))
1522 return -1;
1523
1524 /* Use rename only if TO is not a symbolic link and has
1525 only one hard link. */
1526 if (!S_ISLNK (s.st_mode) && s.st_nlink == 1)
1527 {
1528 ret = rename (from, to);
1529 if (ret == 0)
1530 {
1531 /* Try to preserve the permission bits and ownership of TO.
1532 First get the mode right except for the setuid bit. Then
1533 change the ownership. Then fix the setuid bit. We do
1534 the chmod before the chown because if the chown succeeds,
1535 and we are a normal user, we won't be able to do the
1536 chmod afterward. We don't bother to fix the setuid bit
1537 first because that might introduce a fleeting security
1538 problem, and because the chown will clear the setuid bit
1539 anyhow. We only fix the setuid bit if the chown
1540 succeeds, because we don't want to introduce an
1541 unexpected setuid file owned by the user running objcopy. */
1542 chmod (to, s.st_mode & 0777);
1543 if (chown (to, s.st_uid, s.st_gid) >= 0)
1544 chmod (to, s.st_mode & 07777);
1545 }
1546 else
1547 {
1548 /* We have to clean up here. */
1549 int saved = errno;
1550 fprintf (stderr, "%s: %s: ", program_name, to);
1551 errno = saved;
1552 perror ("rename");
1553 unlink (from);
1554 }
1555 }
1556 else
1557 {
1558 ret = simple_copy (from, to);
1559 if (ret != 0)
1560 {
1561 int saved = errno;
1562 fprintf (stderr, "%s: %s: ", program_name, to);
1563 errno = saved;
1564 perror ("simple_copy");
1565 }
1566 unlink (from);
1567 }
1568 return ret;
1569 }
1570
1571 /* Set the times of the file DESTINATION to be the same as those in
1572 STATBUF. */
1573
1574 static void
1575 set_times (destination, statbuf)
1576 const char *destination;
1577 const struct stat *statbuf;
1578 {
1579 int result;
1580
1581 {
1582 #ifdef HAVE_GOOD_UTIME_H
1583 struct utimbuf tb;
1584
1585 tb.actime = statbuf->st_atime;
1586 tb.modtime = statbuf->st_mtime;
1587 result = utime (destination, &tb);
1588 #else /* ! HAVE_GOOD_UTIME_H */
1589 #ifndef HAVE_UTIMES
1590 long tb[2];
1591
1592 tb[0] = statbuf->st_atime;
1593 tb[1] = statbuf->st_mtime;
1594 result = utime (destination, tb);
1595 #else /* HAVE_UTIMES */
1596 struct timeval tv[2];
1597
1598 tv[0].tv_sec = statbuf->st_atime;
1599 tv[0].tv_usec = 0;
1600 tv[1].tv_sec = statbuf->st_mtime;
1601 tv[1].tv_usec = 0;
1602 result = utimes (destination, tv);
1603 #endif /* HAVE_UTIMES */
1604 #endif /* ! HAVE_GOOD_UTIME_H */
1605 }
1606
1607 if (result != 0)
1608 {
1609 fprintf (stderr, "%s: ", destination);
1610 perror ("can not set time");
1611 }
1612 }
1613
1614 static int
1615 strip_main (argc, argv)
1616 int argc;
1617 char *argv[];
1618 {
1619 char *input_target = NULL, *output_target = NULL;
1620 boolean show_version = false;
1621 boolean preserve_dates = false;
1622 int c, i;
1623 struct section_list *p;
1624 char *output_file = NULL;
1625
1626 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv",
1627 strip_options, (int *) 0)) != EOF)
1628 {
1629 switch (c)
1630 {
1631 case 'I':
1632 input_target = optarg;
1633 break;
1634 case 'O':
1635 output_target = optarg;
1636 break;
1637 case 'F':
1638 input_target = output_target = optarg;
1639 break;
1640 case 'R':
1641 p = find_section_list (optarg, true);
1642 p->remove = true;
1643 sections_removed = true;
1644 break;
1645 case 's':
1646 strip_symbols = strip_all;
1647 break;
1648 case 'S':
1649 case 'g':
1650 strip_symbols = strip_debug;
1651 break;
1652 case OPTION_STRIP_UNNEEDED:
1653 strip_symbols = strip_unneeded;
1654 break;
1655 case 'K':
1656 add_specific_symbol (optarg, &keep_specific_list);
1657 break;
1658 case 'N':
1659 add_specific_symbol (optarg, &strip_specific_list);
1660 break;
1661 case 'o':
1662 output_file = optarg;
1663 break;
1664 case 'p':
1665 preserve_dates = true;
1666 break;
1667 case 'x':
1668 discard_locals = locals_all;
1669 break;
1670 case 'X':
1671 discard_locals = locals_start_L;
1672 break;
1673 case 'v':
1674 verbose = true;
1675 break;
1676 case 'V':
1677 show_version = true;
1678 break;
1679 case 0:
1680 break; /* we've been given a long option */
1681 case 'h':
1682 strip_usage (stdout, 0);
1683 default:
1684 strip_usage (stderr, 1);
1685 }
1686 }
1687
1688 if (show_version)
1689 print_version ("strip");
1690
1691 /* Default is to strip all symbols. */
1692 if (strip_symbols == strip_undef
1693 && discard_locals == locals_undef
1694 && strip_specific_list == NULL)
1695 strip_symbols = strip_all;
1696
1697 if (output_target == (char *) NULL)
1698 output_target = input_target;
1699
1700 i = optind;
1701 if (i == argc
1702 || (output_file != NULL && (i + 1) < argc))
1703 strip_usage (stderr, 1);
1704
1705 for (; i < argc; i++)
1706 {
1707 int hold_status = status;
1708 struct stat statbuf;
1709 char *tmpname;
1710
1711 if (preserve_dates)
1712 {
1713 if (stat (argv[i], &statbuf) < 0)
1714 {
1715 fprintf (stderr, "%s: ", argv[i]);
1716 perror ("cannot stat");
1717 continue;
1718 }
1719 }
1720
1721 if (output_file != NULL)
1722 tmpname = output_file;
1723 else
1724 tmpname = make_tempname (argv[i]);
1725 status = 0;
1726
1727 copy_file (argv[i], tmpname, input_target, output_target);
1728 if (status == 0)
1729 {
1730 if (preserve_dates)
1731 set_times (tmpname, &statbuf);
1732 if (output_file == NULL)
1733 smart_rename (tmpname, argv[i]);
1734 status = hold_status;
1735 }
1736 else
1737 unlink (tmpname);
1738 if (output_file == NULL)
1739 free (tmpname);
1740 }
1741
1742 return 0;
1743 }
1744
1745 static int
1746 copy_main (argc, argv)
1747 int argc;
1748 char *argv[];
1749 {
1750 char *input_filename = NULL, *output_filename = NULL;
1751 char *input_target = NULL, *output_target = NULL;
1752 boolean show_version = false;
1753 boolean adjust_warn = true;
1754 boolean preserve_dates = false;
1755 int c;
1756 struct section_list *p;
1757 struct stat statbuf;
1758
1759 while ((c = getopt_long (argc, argv, "b:i:I:K:N:s:O:d:F:P:R:SpgxXVvW:",
1760 copy_options, (int *) 0)) != EOF)
1761 {
1762 switch (c)
1763 {
1764 case 'b':
1765 copy_byte = atoi(optarg);
1766 if (copy_byte < 0)
1767 {
1768 fprintf (stderr, "%s: byte number must be non-negative\n",
1769 program_name);
1770 exit (1);
1771 }
1772 break;
1773 case 'i':
1774 interleave = atoi(optarg);
1775 if (interleave < 1)
1776 {
1777 fprintf(stderr, "%s: interleave must be positive\n",
1778 program_name);
1779 exit (1);
1780 }
1781 break;
1782 case 'I':
1783 case 's': /* "source" - 'I' is preferred */
1784 input_target = optarg;
1785 break;
1786 case 'O':
1787 case 'd': /* "destination" - 'O' is preferred */
1788 output_target = optarg;
1789 break;
1790 case 'F':
1791 input_target = output_target = optarg;
1792 break;
1793 case 'R':
1794 p = find_section_list (optarg, true);
1795 p->remove = true;
1796 sections_removed = true;
1797 break;
1798 case 'S':
1799 strip_symbols = strip_all;
1800 break;
1801 case 'g':
1802 strip_symbols = strip_debug;
1803 break;
1804 case OPTION_STRIP_UNNEEDED:
1805 strip_symbols = strip_unneeded;
1806 break;
1807 case 'K':
1808 add_specific_symbol (optarg, &keep_specific_list);
1809 break;
1810 case 'N':
1811 add_specific_symbol (optarg, &strip_specific_list);
1812 break;
1813 case 'P':
1814 add_specific_symbol (optarg, &privatize_specific_list);
1815 break;
1816 case 'W':
1817 add_specific_symbol (optarg, &weaken_specific_list);
1818 break;
1819 case 'p':
1820 preserve_dates = true;
1821 break;
1822 case 'x':
1823 discard_locals = locals_all;
1824 break;
1825 case 'X':
1826 discard_locals = locals_start_L;
1827 break;
1828 case 'v':
1829 verbose = true;
1830 break;
1831 case 'V':
1832 show_version = true;
1833 break;
1834 case OPTION_WEAKEN:
1835 weaken = true;
1836 break;
1837 case OPTION_ADD_SECTION:
1838 {
1839 const char *s;
1840 struct stat st;
1841 struct section_add *pa;
1842 int len;
1843 char *name;
1844 FILE *f;
1845
1846 s = strchr (optarg, '=');
1847 if (s == NULL)
1848 {
1849 fprintf (stderr,
1850 "%s: bad format for --add-section NAME=FILENAME\n",
1851 program_name);
1852 exit (1);
1853 }
1854
1855 if (stat (s + 1, &st) < 0)
1856 {
1857 fprintf (stderr, "%s: ", program_name);
1858 perror (s + 1);
1859 exit (1);
1860 }
1861
1862 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1863
1864 len = s - optarg;
1865 name = (char *) xmalloc (len + 1);
1866 strncpy (name, optarg, len);
1867 name[len] = '\0';
1868 pa->name = name;
1869
1870 pa->filename = s + 1;
1871
1872 pa->size = st.st_size;
1873
1874 pa->contents = (bfd_byte *) xmalloc (pa->size);
1875 f = fopen (pa->filename, FOPEN_RB);
1876 if (f == NULL)
1877 {
1878 fprintf (stderr, "%s: ", program_name);
1879 perror (pa->filename);
1880 exit (1);
1881 }
1882 if (fread (pa->contents, 1, pa->size, f) == 0
1883 || ferror (f))
1884 {
1885 fprintf (stderr, "%s: %s: fread failed\n",
1886 program_name, pa->filename);
1887 exit (1);
1888 }
1889 fclose (f);
1890
1891 pa->next = add_sections;
1892 add_sections = pa;
1893 }
1894 break;
1895 case OPTION_ADJUST_START:
1896 adjust_start = parse_vma (optarg, "--adjust-start");
1897 break;
1898 case OPTION_ADJUST_SECTION_VMA:
1899 {
1900 const char *s;
1901 int len;
1902 char *name;
1903
1904 s = strchr (optarg, '=');
1905 if (s == NULL)
1906 {
1907 s = strchr (optarg, '+');
1908 if (s == NULL)
1909 {
1910 s = strchr (optarg, '-');
1911 if (s == NULL)
1912 {
1913 fprintf (stderr,
1914 "%s: bad format for --adjust-section-vma\n",
1915 program_name);
1916 exit (1);
1917 }
1918 }
1919 }
1920
1921 len = s - optarg;
1922 name = (char *) xmalloc (len + 1);
1923 strncpy (name, optarg, len);
1924 name[len] = '\0';
1925
1926 p = find_section_list (name, true);
1927
1928 p->val = parse_vma (s + 1, "--adjust-section-vma");
1929
1930 if (*s == '=')
1931 p->adjust = set_vma;
1932 else
1933 {
1934 p->adjust = adjust_vma;
1935 if (*s == '-')
1936 p->val = - p->val;
1937 }
1938 }
1939 break;
1940 case OPTION_ADJUST_VMA:
1941 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
1942 adjust_start = adjust_section_vma;
1943 break;
1944 case OPTION_ADJUST_WARNINGS:
1945 adjust_warn = true;
1946 break;
1947 case OPTION_CHANGE_LEADING_CHAR:
1948 change_leading_char = true;
1949 break;
1950 case OPTION_DEBUGGING:
1951 convert_debugging = true;
1952 break;
1953 case OPTION_GAP_FILL:
1954 {
1955 bfd_vma gap_fill_vma;
1956
1957 gap_fill_vma = parse_vma (optarg, "--gap-fill");
1958 gap_fill = (bfd_byte) gap_fill_vma;
1959 if ((bfd_vma) gap_fill != gap_fill_vma)
1960 {
1961 fprintf (stderr, "%s: warning: truncating gap-fill from 0x",
1962 program_name);
1963 fprintf_vma (stderr, gap_fill_vma);
1964 fprintf (stderr, "to 0x%x\n", (unsigned int) gap_fill);
1965 }
1966 gap_fill_set = true;
1967 }
1968 break;
1969 case OPTION_NO_ADJUST_WARNINGS:
1970 adjust_warn = false;
1971 break;
1972 case OPTION_PAD_TO:
1973 pad_to = parse_vma (optarg, "--pad-to");
1974 pad_to_set = true;
1975 break;
1976 case OPTION_REMOVE_LEADING_CHAR:
1977 remove_leading_char = true;
1978 break;
1979 case OPTION_SET_SECTION_FLAGS:
1980 {
1981 const char *s;
1982 int len;
1983 char *name;
1984
1985 s = strchr (optarg, '=');
1986 if (s == NULL)
1987 {
1988 fprintf (stderr, "%s: bad format for --set-section-flags\n",
1989 program_name);
1990 exit (1);
1991 }
1992
1993 len = s - optarg;
1994 name = (char *) xmalloc (len + 1);
1995 strncpy (name, optarg, len);
1996 name[len] = '\0';
1997
1998 p = find_section_list (name, true);
1999
2000 p->set_flags = true;
2001 p->flags = parse_flags (s + 1);
2002 }
2003 break;
2004 case OPTION_SET_START:
2005 set_start = parse_vma (optarg, "--set-start");
2006 set_start_set = true;
2007 break;
2008 case 0:
2009 break; /* we've been given a long option */
2010 case 'h':
2011 copy_usage (stdout, 0);
2012 default:
2013 copy_usage (stderr, 1);
2014 }
2015 }
2016
2017 if (show_version)
2018 print_version ("objcopy");
2019
2020 if (copy_byte >= interleave)
2021 {
2022 fprintf (stderr, "%s: byte number must be less than interleave\n",
2023 program_name);
2024 exit (1);
2025 }
2026
2027 if (optind == argc || optind + 2 < argc)
2028 copy_usage (stderr, 1);
2029
2030 input_filename = argv[optind];
2031 if (optind + 1 < argc)
2032 output_filename = argv[optind + 1];
2033
2034 /* Default is to strip no symbols. */
2035 if (strip_symbols == strip_undef && discard_locals == locals_undef)
2036 strip_symbols = strip_none;
2037
2038 if (output_target == (char *) NULL)
2039 output_target = input_target;
2040
2041 if (preserve_dates)
2042 {
2043 if (stat (input_filename, &statbuf) < 0)
2044 {
2045 fprintf (stderr, "%s: ", input_filename);
2046 perror ("cannot stat");
2047 exit (1);
2048 }
2049 }
2050
2051 /* If there is no destination file then create a temp and rename
2052 the result into the input. */
2053
2054 if (output_filename == (char *) NULL)
2055 {
2056 char *tmpname = make_tempname (input_filename);
2057
2058 copy_file (input_filename, tmpname, input_target, output_target);
2059 if (status == 0)
2060 {
2061 if (preserve_dates)
2062 set_times (tmpname, &statbuf);
2063 smart_rename (tmpname, input_filename);
2064 }
2065 else
2066 unlink (tmpname);
2067 }
2068 else
2069 {
2070 copy_file (input_filename, output_filename, input_target, output_target);
2071 if (status == 0 && preserve_dates)
2072 set_times (output_filename, &statbuf);
2073 }
2074
2075 if (adjust_warn)
2076 {
2077 for (p = adjust_sections; p != NULL; p = p->next)
2078 {
2079 if (! p->used && p->adjust != ignore_vma)
2080 {
2081 fprintf (stderr, "%s: warning: --adjust-section-vma %s%c0x",
2082 program_name, p->name,
2083 p->adjust == set_vma ? '=' : '+');
2084 fprintf_vma (stderr, p->val);
2085 fprintf (stderr, " never used\n");
2086 }
2087 }
2088 }
2089
2090 return 0;
2091 }
2092
2093 int
2094 main (argc, argv)
2095 int argc;
2096 char *argv[];
2097 {
2098 program_name = argv[0];
2099 xmalloc_set_program_name (program_name);
2100
2101 START_PROGRESS (program_name, 0);
2102
2103 strip_symbols = strip_undef;
2104 discard_locals = locals_undef;
2105
2106 bfd_init ();
2107 set_default_bfd_target ();
2108
2109 if (is_strip < 0)
2110 {
2111 int i = strlen (program_name);
2112 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
2113 }
2114
2115 if (is_strip)
2116 strip_main (argc, argv);
2117 else
2118 copy_main (argc, argv);
2119
2120 END_PROGRESS (program_name);
2121
2122 return status;
2123 }
This page took 0.072443 seconds and 5 git commands to generate.