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