Add missing \n\ from help description.
[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 (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 bfd_close (output_bfd);
987
988 /* Open the newly output file and attach to our list. */
989 output_bfd = bfd_openr (output_name, output_target);
990
991 l->obfd = output_bfd;
992
993 *ptr = output_bfd;
994 ptr = &output_bfd->next;
995
996 last_element = this_element;
997
998 this_element = bfd_openr_next_archived_file (ibfd, last_element);
999
1000 bfd_close (last_element);
1001 }
1002 *ptr = (bfd *) NULL;
1003
1004 if (!bfd_close (obfd))
1005 RETURN_NONFATAL (bfd_get_filename (obfd));
1006
1007 if (!bfd_close (ibfd))
1008 RETURN_NONFATAL (bfd_get_filename (ibfd));
1009
1010 /* Delete all the files that we opened. */
1011 for (l = list; l != NULL; l = l->next)
1012 {
1013 bfd_close (l->obfd);
1014 unlink (l->name);
1015 }
1016 rmdir (dir);
1017 }
1018
1019 /* The top-level control. */
1020
1021 static void
1022 copy_file (input_filename, output_filename, input_target, output_target)
1023 const char *input_filename;
1024 const char *output_filename;
1025 const char *input_target;
1026 const char *output_target;
1027 {
1028 bfd *ibfd;
1029 char **matching;
1030
1031 /* To allow us to do "strip *" without dying on the first
1032 non-object file, failures are nonfatal. */
1033
1034 ibfd = bfd_openr (input_filename, input_target);
1035 if (ibfd == NULL)
1036 RETURN_NONFATAL (input_filename);
1037
1038 if (bfd_check_format (ibfd, bfd_archive))
1039 {
1040 bfd *obfd;
1041
1042 /* bfd_get_target does not return the correct value until
1043 bfd_check_format succeeds. */
1044 if (output_target == NULL)
1045 output_target = bfd_get_target (ibfd);
1046
1047 obfd = bfd_openw (output_filename, output_target);
1048 if (obfd == NULL)
1049 RETURN_NONFATAL (output_filename);
1050
1051 copy_archive (ibfd, obfd, output_target);
1052 }
1053 else if (bfd_check_format_matches (ibfd, bfd_object, &matching))
1054 {
1055 bfd *obfd;
1056
1057 /* bfd_get_target does not return the correct value until
1058 bfd_check_format succeeds. */
1059 if (output_target == NULL)
1060 output_target = bfd_get_target (ibfd);
1061
1062 obfd = bfd_openw (output_filename, output_target);
1063 if (obfd == NULL)
1064 RETURN_NONFATAL (output_filename);
1065
1066 copy_object (ibfd, obfd);
1067
1068 if (!bfd_close (obfd))
1069 RETURN_NONFATAL (output_filename);
1070
1071 if (!bfd_close (ibfd))
1072 RETURN_NONFATAL (input_filename);
1073 }
1074 else
1075 {
1076 bfd_nonfatal (input_filename);
1077
1078 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1079 {
1080 list_matching_formats (matching);
1081 free (matching);
1082 }
1083
1084 status = 1;
1085 }
1086 }
1087
1088 /* Create a section in OBFD with the same name and attributes
1089 as ISECTION in IBFD. */
1090
1091 static void
1092 setup_section (ibfd, isection, obfdarg)
1093 bfd *ibfd;
1094 sec_ptr isection;
1095 PTR obfdarg;
1096 {
1097 bfd *obfd = (bfd *) obfdarg;
1098 struct section_list *p;
1099 sec_ptr osection;
1100 bfd_vma vma;
1101 bfd_vma lma;
1102 flagword flags;
1103 char *err;
1104
1105 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1106 && (strip_symbols == STRIP_DEBUG
1107 || strip_symbols == STRIP_UNNEEDED
1108 || strip_symbols == STRIP_ALL
1109 || discard_locals == LOCALS_ALL
1110 || convert_debugging))
1111 return;
1112
1113 p = find_section_list (bfd_section_name (ibfd, isection), false);
1114 if (p != NULL)
1115 p->used = true;
1116
1117 if (p != NULL && p->remove)
1118 return;
1119
1120 osection = bfd_make_section_anyway (obfd, bfd_section_name (ibfd, isection));
1121 if (osection == NULL)
1122 {
1123 err = "making";
1124 goto loser;
1125 }
1126
1127 if (!bfd_set_section_size (obfd,
1128 osection,
1129 bfd_section_size (ibfd, isection)))
1130 {
1131 err = "size";
1132 goto loser;
1133 }
1134
1135 vma = bfd_section_vma (ibfd, isection);
1136 if (p != NULL && p->change_vma == CHANGE_MODIFY)
1137 vma += p->vma_val;
1138 else if (p != NULL && p->change_vma == CHANGE_SET)
1139 vma = p->vma_val;
1140 else
1141 vma += change_section_address;
1142
1143 if (! bfd_set_section_vma (obfd, osection, vma))
1144 {
1145 err = "vma";
1146 goto loser;
1147 }
1148
1149 lma = isection->lma;
1150 if ((p != NULL) && p->change_lma != CHANGE_IGNORE)
1151 {
1152 if (p->change_lma == CHANGE_MODIFY)
1153 lma += p->lma_val;
1154 else if (p->change_lma == CHANGE_SET)
1155 lma = p->lma_val;
1156 else
1157 abort();
1158 }
1159 else
1160 lma += change_section_address;
1161
1162 osection->lma = lma;
1163
1164 /* FIXME: This is probably not enough. If we change the LMA we
1165 may have to recompute the header for the file as well. */
1166 if (bfd_set_section_alignment (obfd,
1167 osection,
1168 bfd_section_alignment (ibfd, isection))
1169 == false)
1170 {
1171 err = "alignment";
1172 goto loser;
1173 }
1174
1175 flags = bfd_get_section_flags (ibfd, isection);
1176 if (p != NULL && p->set_flags)
1177 flags = p->flags | (flags & SEC_HAS_CONTENTS);
1178 if (!bfd_set_section_flags (obfd, osection, flags))
1179 {
1180 err = "flags";
1181 goto loser;
1182 }
1183
1184 /* This used to be mangle_section; we do here to avoid using
1185 bfd_get_section_by_name since some formats allow multiple
1186 sections with the same name. */
1187 isection->output_section = osection;
1188 isection->output_offset = 0;
1189
1190 /* Allow the BFD backend to copy any private data it understands
1191 from the input section to the output section. */
1192 if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
1193 {
1194 err = "private data";
1195 goto loser;
1196 }
1197
1198 /* All went well */
1199 return;
1200
1201 loser:
1202 non_fatal (_("%s: section `%s': error in %s: %s"),
1203 bfd_get_filename (ibfd),
1204 bfd_section_name (ibfd, isection),
1205 err, bfd_errmsg (bfd_get_error ()));
1206 status = 1;
1207 }
1208
1209 /* Copy the data of input section ISECTION of IBFD
1210 to an output section with the same name in OBFD.
1211 If stripping then don't copy any relocation info. */
1212
1213 static void
1214 copy_section (ibfd, isection, obfdarg)
1215 bfd *ibfd;
1216 sec_ptr isection;
1217 PTR obfdarg;
1218 {
1219 bfd *obfd = (bfd *) obfdarg;
1220 struct section_list *p;
1221 arelent **relpp;
1222 long relcount;
1223 sec_ptr osection;
1224 bfd_size_type size;
1225
1226 /* If we have already failed earlier on, do not keep on generating
1227 complaints now. */
1228 if (status != 0)
1229 return;
1230
1231 if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0
1232 && (strip_symbols == STRIP_DEBUG
1233 || strip_symbols == STRIP_UNNEEDED
1234 || strip_symbols == STRIP_ALL
1235 || discard_locals == LOCALS_ALL
1236 || convert_debugging))
1237 {
1238 return;
1239 }
1240
1241 p = find_section_list (bfd_section_name (ibfd, isection), false);
1242
1243 if (p != NULL && p->remove)
1244 return;
1245
1246 osection = isection->output_section;
1247 size = bfd_get_section_size_before_reloc (isection);
1248
1249 if (size == 0 || osection == 0)
1250 return;
1251
1252 if (strip_symbols == STRIP_ALL)
1253 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1254 else
1255 {
1256 long relsize;
1257
1258 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1259 if (relsize < 0)
1260 RETURN_NONFATAL (bfd_get_filename (ibfd));
1261
1262 if (relsize == 0)
1263 bfd_set_reloc (obfd, osection, (arelent **) NULL, 0);
1264 else
1265 {
1266 relpp = (arelent **) xmalloc (relsize);
1267 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
1268 if (relcount < 0)
1269 RETURN_NONFATAL (bfd_get_filename (ibfd));
1270
1271 bfd_set_reloc (obfd, osection, relpp, relcount);
1272 }
1273 }
1274
1275 isection->_cooked_size = isection->_raw_size;
1276 isection->reloc_done = true;
1277
1278 if (bfd_get_section_flags (ibfd, isection) & SEC_HAS_CONTENTS)
1279 {
1280 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1281
1282 if (!bfd_get_section_contents (ibfd, isection, memhunk, (file_ptr) 0,
1283 size))
1284 RETURN_NONFATAL (bfd_get_filename (ibfd));
1285
1286 if (copy_byte >= 0)
1287 {
1288 filter_bytes (memhunk, &size);
1289 /* The section has gotten smaller. */
1290 if (!bfd_set_section_size (obfd, osection, size))
1291 RETURN_NONFATAL (bfd_get_filename (obfd));
1292 }
1293
1294 if (!bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1295 size))
1296 RETURN_NONFATAL (bfd_get_filename (obfd));
1297
1298 free (memhunk);
1299 }
1300 else if (p != NULL && p->set_flags && (p->flags & SEC_HAS_CONTENTS) != 0)
1301 {
1302 PTR memhunk = (PTR) xmalloc ((unsigned) size);
1303
1304 /* We don't permit the user to turn off the SEC_HAS_CONTENTS
1305 flag--they can just remove the section entirely and add it
1306 back again. However, we do permit them to turn on the
1307 SEC_HAS_CONTENTS flag, and take it to mean that the section
1308 contents should be zeroed out. */
1309
1310 memset (memhunk, 0, size);
1311 if (! bfd_set_section_contents (obfd, osection, memhunk, (file_ptr) 0,
1312 size))
1313 RETURN_NONFATAL (bfd_get_filename (obfd));
1314 free (memhunk);
1315 }
1316 }
1317
1318 /* Get all the sections. This is used when --gap-fill or --pad-to is
1319 used. */
1320
1321 static void
1322 get_sections (obfd, osection, secppparg)
1323 bfd *obfd;
1324 asection *osection;
1325 PTR secppparg;
1326 {
1327 asection ***secppp = (asection ***) secppparg;
1328
1329 **secppp = osection;
1330 ++(*secppp);
1331 }
1332
1333 /* Sort sections by VMA. This is called via qsort, and is used when
1334 --gap-fill or --pad-to is used. We force non loadable or empty
1335 sections to the front, where they are easier to ignore. */
1336
1337 static int
1338 compare_section_lma (arg1, arg2)
1339 const PTR arg1;
1340 const PTR arg2;
1341 {
1342 const asection **sec1 = (const asection **) arg1;
1343 const asection **sec2 = (const asection **) arg2;
1344 flagword flags1, flags2;
1345
1346 /* Sort non loadable sections to the front. */
1347 flags1 = (*sec1)->flags;
1348 flags2 = (*sec2)->flags;
1349 if ((flags1 & SEC_HAS_CONTENTS) == 0
1350 || (flags1 & SEC_LOAD) == 0)
1351 {
1352 if ((flags2 & SEC_HAS_CONTENTS) != 0
1353 && (flags2 & SEC_LOAD) != 0)
1354 return -1;
1355 }
1356 else
1357 {
1358 if ((flags2 & SEC_HAS_CONTENTS) == 0
1359 || (flags2 & SEC_LOAD) == 0)
1360 return 1;
1361 }
1362
1363 /* Sort sections by LMA. */
1364 if ((*sec1)->lma > (*sec2)->lma)
1365 return 1;
1366 else if ((*sec1)->lma < (*sec2)->lma)
1367 return -1;
1368
1369 /* Sort sections with the same LMA by size. */
1370 if ((*sec1)->_raw_size > (*sec2)->_raw_size)
1371 return 1;
1372 else if ((*sec1)->_raw_size < (*sec2)->_raw_size)
1373 return -1;
1374
1375 return 0;
1376 }
1377
1378 /* Mark all the symbols which will be used in output relocations with
1379 the BSF_KEEP flag so that those symbols will not be stripped.
1380
1381 Ignore relocations which will not appear in the output file. */
1382
1383 static void
1384 mark_symbols_used_in_relocations (ibfd, isection, symbolsarg)
1385 bfd *ibfd;
1386 sec_ptr isection;
1387 PTR symbolsarg;
1388 {
1389 asymbol **symbols = (asymbol **) symbolsarg;
1390 long relsize;
1391 arelent **relpp;
1392 long relcount, i;
1393
1394 /* Ignore an input section with no corresponding output section. */
1395 if (isection->output_section == NULL)
1396 return;
1397
1398 relsize = bfd_get_reloc_upper_bound (ibfd, isection);
1399 if (relsize < 0)
1400 bfd_fatal (bfd_get_filename (ibfd));
1401
1402 if (relsize == 0)
1403 return;
1404
1405 relpp = (arelent **) xmalloc (relsize);
1406 relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
1407 if (relcount < 0)
1408 bfd_fatal (bfd_get_filename (ibfd));
1409
1410 /* Examine each symbol used in a relocation. If it's not one of the
1411 special bfd section symbols, then mark it with BSF_KEEP. */
1412 for (i = 0; i < relcount; i++)
1413 {
1414 if (*relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
1415 && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
1416 && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
1417 (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
1418 }
1419
1420 if (relpp != NULL)
1421 free (relpp);
1422 }
1423
1424 /* Write out debugging information. */
1425
1426 static boolean
1427 write_debugging_info (obfd, dhandle, symcountp, symppp)
1428 bfd *obfd;
1429 PTR dhandle;
1430 long *symcountp;
1431 asymbol ***symppp;
1432 {
1433 if (bfd_get_flavour (obfd) == bfd_target_ieee_flavour)
1434 return write_ieee_debugging_info (obfd, dhandle);
1435
1436 if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
1437 || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
1438 {
1439 bfd_byte *syms, *strings;
1440 bfd_size_type symsize, stringsize;
1441 asection *stabsec, *stabstrsec;
1442
1443 if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
1444 &symsize, &strings,
1445 &stringsize))
1446 return false;
1447
1448 stabsec = bfd_make_section (obfd, ".stab");
1449 stabstrsec = bfd_make_section (obfd, ".stabstr");
1450 if (stabsec == NULL
1451 || stabstrsec == NULL
1452 || ! bfd_set_section_size (obfd, stabsec, symsize)
1453 || ! bfd_set_section_size (obfd, stabstrsec, stringsize)
1454 || ! bfd_set_section_alignment (obfd, stabsec, 2)
1455 || ! bfd_set_section_alignment (obfd, stabstrsec, 0)
1456 || ! bfd_set_section_flags (obfd, stabsec,
1457 (SEC_HAS_CONTENTS
1458 | SEC_READONLY
1459 | SEC_DEBUGGING))
1460 || ! bfd_set_section_flags (obfd, stabstrsec,
1461 (SEC_HAS_CONTENTS
1462 | SEC_READONLY
1463 | SEC_DEBUGGING)))
1464 {
1465 non_fatal (_("%s: can't create debugging section: %s"),
1466 bfd_get_filename (obfd),
1467 bfd_errmsg (bfd_get_error ()));
1468 return false;
1469 }
1470
1471 /* We can get away with setting the section contents now because
1472 the next thing the caller is going to do is copy over the
1473 real sections. We may someday have to split the contents
1474 setting out of this function. */
1475 if (! bfd_set_section_contents (obfd, stabsec, syms, (file_ptr) 0,
1476 symsize)
1477 || ! bfd_set_section_contents (obfd, stabstrsec, strings,
1478 (file_ptr) 0, stringsize))
1479 {
1480 non_fatal (_("%s: can't set debugging section contents: %s"),
1481 bfd_get_filename (obfd),
1482 bfd_errmsg (bfd_get_error ()));
1483 return false;
1484 }
1485
1486 return true;
1487 }
1488
1489 non_fatal (_("%s: don't know how to write debugging information for %s"),
1490 bfd_get_filename (obfd), bfd_get_target (obfd));
1491 return false;
1492 }
1493
1494 /* The number of bytes to copy at once. */
1495 #define COPY_BUF 8192
1496
1497 /* Copy file FROM to file TO, performing no translations.
1498 Return 0 if ok, -1 if error. */
1499
1500 static int
1501 simple_copy (from, to)
1502 const char *from;
1503 const char *to;
1504 {
1505 int fromfd, tofd, nread;
1506 int saved;
1507 char buf[COPY_BUF];
1508
1509 fromfd = open (from, O_RDONLY);
1510 if (fromfd < 0)
1511 return -1;
1512 tofd = creat (to, 0777);
1513 if (tofd < 0)
1514 {
1515 saved = errno;
1516 close (fromfd);
1517 errno = saved;
1518 return -1;
1519 }
1520 while ((nread = read (fromfd, buf, sizeof buf)) > 0)
1521 {
1522 if (write (tofd, buf, nread) != nread)
1523 {
1524 saved = errno;
1525 close (fromfd);
1526 close (tofd);
1527 errno = saved;
1528 return -1;
1529 }
1530 }
1531 saved = errno;
1532 close (fromfd);
1533 close (tofd);
1534 if (nread < 0)
1535 {
1536 errno = saved;
1537 return -1;
1538 }
1539 return 0;
1540 }
1541
1542 #ifndef S_ISLNK
1543 #ifdef S_IFLNK
1544 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1545 #else
1546 #define S_ISLNK(m) 0
1547 #define lstat stat
1548 #endif
1549 #endif
1550
1551 /* Rename FROM to TO, copying if TO is a link.
1552 Assumes that TO already exists, because FROM is a temp file.
1553 Return 0 if ok, -1 if error. */
1554
1555 static int
1556 smart_rename (from, to)
1557 const char *from;
1558 const char *to;
1559 {
1560 struct stat s;
1561 int ret = 0;
1562
1563 if (lstat (to, &s))
1564 return -1;
1565
1566 #if defined (_WIN32) && !defined (__CYGWIN32__)
1567 /* Win32, unlike unix, will not erase `to' in `rename(from, to)' but
1568 fail instead. Also, chown is not present. */
1569
1570 if (stat (to, &s) == 0)
1571 remove (to);
1572
1573 ret = rename (from, to);
1574 if (ret != 0)
1575 {
1576 /* We have to clean up here. */
1577
1578 non_fatal (_("%s: rename: %s"), to, strerror (errno));
1579 unlink (from);
1580 }
1581 #else
1582 /* Use rename only if TO is not a symbolic link and has
1583 only one hard link. */
1584 if (!S_ISLNK (s.st_mode) && s.st_nlink == 1)
1585 {
1586 ret = rename (from, to);
1587 if (ret == 0)
1588 {
1589 /* Try to preserve the permission bits and ownership of TO.
1590 First get the mode right except for the setuid bit. Then
1591 change the ownership. Then fix the setuid bit. We do
1592 the chmod before the chown because if the chown succeeds,
1593 and we are a normal user, we won't be able to do the
1594 chmod afterward. We don't bother to fix the setuid bit
1595 first because that might introduce a fleeting security
1596 problem, and because the chown will clear the setuid bit
1597 anyhow. We only fix the setuid bit if the chown
1598 succeeds, because we don't want to introduce an
1599 unexpected setuid file owned by the user running objcopy. */
1600 chmod (to, s.st_mode & 0777);
1601 if (chown (to, s.st_uid, s.st_gid) >= 0)
1602 chmod (to, s.st_mode & 07777);
1603 }
1604 else
1605 {
1606 /* We have to clean up here. */
1607 non_fatal (_("%s: rename: %s"), to, strerror (errno));
1608 unlink (from);
1609 }
1610 }
1611 else
1612 {
1613 ret = simple_copy (from, to);
1614 if (ret != 0)
1615 non_fatal (_("%s: simple_copy: %s"), to, strerror (errno));
1616
1617 if (preserve_dates)
1618 set_times (to, &s);
1619 unlink (from);
1620 }
1621 #endif /* _WIN32 && !__CYGWIN32__ */
1622
1623 return ret;
1624 }
1625
1626 /* Set the times of the file DESTINATION to be the same as those in
1627 STATBUF. */
1628
1629 static void
1630 set_times (destination, statbuf)
1631 const char *destination;
1632 const struct stat *statbuf;
1633 {
1634 int result;
1635
1636 {
1637 #ifdef HAVE_GOOD_UTIME_H
1638 struct utimbuf tb;
1639
1640 tb.actime = statbuf->st_atime;
1641 tb.modtime = statbuf->st_mtime;
1642 result = utime (destination, &tb);
1643 #else /* ! HAVE_GOOD_UTIME_H */
1644 #ifndef HAVE_UTIMES
1645 long tb[2];
1646
1647 tb[0] = statbuf->st_atime;
1648 tb[1] = statbuf->st_mtime;
1649 result = utime (destination, tb);
1650 #else /* HAVE_UTIMES */
1651 struct timeval tv[2];
1652
1653 tv[0].tv_sec = statbuf->st_atime;
1654 tv[0].tv_usec = 0;
1655 tv[1].tv_sec = statbuf->st_mtime;
1656 tv[1].tv_usec = 0;
1657 result = utimes (destination, tv);
1658 #endif /* HAVE_UTIMES */
1659 #endif /* ! HAVE_GOOD_UTIME_H */
1660 }
1661
1662 if (result != 0)
1663 non_fatal (_("%s: cannot set time: %s"), destination, strerror (errno));
1664 }
1665
1666 static int
1667 strip_main (argc, argv)
1668 int argc;
1669 char *argv[];
1670 {
1671 char *input_target = NULL, *output_target = NULL;
1672 boolean show_version = false;
1673 int c, i;
1674 struct section_list *p;
1675 char *output_file = NULL;
1676
1677 while ((c = getopt_long (argc, argv, "I:O:F:K:N:R:o:sSpgxXVv",
1678 strip_options, (int *) 0)) != EOF)
1679 {
1680 switch (c)
1681 {
1682 case 'I':
1683 input_target = optarg;
1684 break;
1685 case 'O':
1686 output_target = optarg;
1687 break;
1688 case 'F':
1689 input_target = output_target = optarg;
1690 break;
1691 case 'R':
1692 p = find_section_list (optarg, true);
1693 p->remove = true;
1694 sections_removed = true;
1695 break;
1696 case 's':
1697 strip_symbols = STRIP_ALL;
1698 break;
1699 case 'S':
1700 case 'g':
1701 strip_symbols = STRIP_DEBUG;
1702 break;
1703 case OPTION_STRIP_UNNEEDED:
1704 strip_symbols = STRIP_UNNEEDED;
1705 break;
1706 case 'K':
1707 add_specific_symbol (optarg, &keep_specific_list);
1708 break;
1709 case 'N':
1710 add_specific_symbol (optarg, &strip_specific_list);
1711 break;
1712 case 'o':
1713 output_file = optarg;
1714 break;
1715 case 'p':
1716 preserve_dates = true;
1717 break;
1718 case 'x':
1719 discard_locals = LOCALS_ALL;
1720 break;
1721 case 'X':
1722 discard_locals = LOCALS_START_L;
1723 break;
1724 case 'v':
1725 verbose = true;
1726 break;
1727 case 'V':
1728 show_version = true;
1729 break;
1730 case 0:
1731 break; /* we've been given a long option */
1732 case 'h':
1733 strip_usage (stdout, 0);
1734 default:
1735 strip_usage (stderr, 1);
1736 }
1737 }
1738
1739 if (show_version)
1740 print_version ("strip");
1741
1742 /* Default is to strip all symbols. */
1743 if (strip_symbols == STRIP_UNDEF
1744 && discard_locals == LOCALS_UNDEF
1745 && strip_specific_list == NULL)
1746 strip_symbols = STRIP_ALL;
1747
1748 if (output_target == (char *) NULL)
1749 output_target = input_target;
1750
1751 i = optind;
1752 if (i == argc
1753 || (output_file != NULL && (i + 1) < argc))
1754 strip_usage (stderr, 1);
1755
1756 for (; i < argc; i++)
1757 {
1758 int hold_status = status;
1759 struct stat statbuf;
1760 char *tmpname;
1761
1762 if (preserve_dates)
1763 {
1764 if (stat (argv[i], &statbuf) < 0)
1765 {
1766 non_fatal (_("%s: cannot stat: %s"), argv[i], strerror (errno));
1767 continue;
1768 }
1769 }
1770
1771 if (output_file != NULL)
1772 tmpname = output_file;
1773 else
1774 tmpname = make_tempname (argv[i]);
1775 status = 0;
1776
1777 copy_file (argv[i], tmpname, input_target, output_target);
1778 if (status == 0)
1779 {
1780 if (preserve_dates)
1781 set_times (tmpname, &statbuf);
1782 if (output_file == NULL)
1783 smart_rename (tmpname, argv[i]);
1784 status = hold_status;
1785 }
1786 else
1787 unlink (tmpname);
1788 if (output_file == NULL)
1789 free (tmpname);
1790 }
1791
1792 return 0;
1793 }
1794
1795 static int
1796 copy_main (argc, argv)
1797 int argc;
1798 char *argv[];
1799 {
1800 char *input_filename = NULL, *output_filename = NULL;
1801 char *input_target = NULL, *output_target = NULL;
1802 boolean show_version = false;
1803 boolean change_warn = true;
1804 int c;
1805 struct section_list *p;
1806 struct stat statbuf;
1807
1808 while ((c = getopt_long (argc, argv, "b:i:I:K:N:s:O:d:F:L:R:SpgxXVvW:",
1809 copy_options, (int *) 0)) != EOF)
1810 {
1811 switch (c)
1812 {
1813 case 'b':
1814 copy_byte = atoi(optarg);
1815 if (copy_byte < 0)
1816 fatal (_("byte number must be non-negative"));
1817 break;
1818 case 'i':
1819 interleave = atoi(optarg);
1820 if (interleave < 1)
1821 fatal (_("interleave must be positive"));
1822 break;
1823 case 'I':
1824 case 's': /* "source" - 'I' is preferred */
1825 input_target = optarg;
1826 break;
1827 case 'O':
1828 case 'd': /* "destination" - 'O' is preferred */
1829 output_target = optarg;
1830 break;
1831 case 'F':
1832 input_target = output_target = optarg;
1833 break;
1834 case 'R':
1835 p = find_section_list (optarg, true);
1836 p->remove = true;
1837 sections_removed = true;
1838 break;
1839 case 'S':
1840 strip_symbols = STRIP_ALL;
1841 break;
1842 case 'g':
1843 strip_symbols = STRIP_DEBUG;
1844 break;
1845 case OPTION_STRIP_UNNEEDED:
1846 strip_symbols = STRIP_UNNEEDED;
1847 break;
1848 case 'K':
1849 add_specific_symbol (optarg, &keep_specific_list);
1850 break;
1851 case 'N':
1852 add_specific_symbol (optarg, &strip_specific_list);
1853 break;
1854 case 'L':
1855 add_specific_symbol (optarg, &localize_specific_list);
1856 break;
1857 case 'W':
1858 add_specific_symbol (optarg, &weaken_specific_list);
1859 break;
1860 case 'p':
1861 preserve_dates = true;
1862 break;
1863 case 'x':
1864 discard_locals = LOCALS_ALL;
1865 break;
1866 case 'X':
1867 discard_locals = LOCALS_START_L;
1868 break;
1869 case 'v':
1870 verbose = true;
1871 break;
1872 case 'V':
1873 show_version = true;
1874 break;
1875 case OPTION_WEAKEN:
1876 weaken = true;
1877 break;
1878 case OPTION_ADD_SECTION:
1879 {
1880 const char *s;
1881 struct stat st;
1882 struct section_add *pa;
1883 int len;
1884 char *name;
1885 FILE *f;
1886
1887 s = strchr (optarg, '=');
1888
1889 if (s == NULL)
1890 fatal (_("bad format for --add-section NAME=FILENAME"));
1891
1892 if (stat (s + 1, & st) < 0)
1893 fatal (_("cannot stat: %s: %s"), s + 1, strerror (errno));
1894
1895 pa = (struct section_add *) xmalloc (sizeof (struct section_add));
1896
1897 len = s - optarg;
1898 name = (char *) xmalloc (len + 1);
1899 strncpy (name, optarg, len);
1900 name[len] = '\0';
1901 pa->name = name;
1902
1903 pa->filename = s + 1;
1904
1905 pa->size = st.st_size;
1906
1907 pa->contents = (bfd_byte *) xmalloc (pa->size);
1908 f = fopen (pa->filename, FOPEN_RB);
1909
1910 if (f == NULL)
1911 fatal (_("cannot open: %s: %s"), pa->filename, strerror (errno));
1912
1913 if (fread (pa->contents, 1, pa->size, f) == 0
1914 || ferror (f))
1915 fatal (_("%s: fread failed"), pa->filename);
1916
1917 fclose (f);
1918
1919 pa->next = add_sections;
1920 add_sections = pa;
1921 }
1922 break;
1923 case OPTION_CHANGE_START:
1924 change_start = parse_vma (optarg, "--change-start");
1925 break;
1926 case OPTION_CHANGE_SECTION_ADDRESS:
1927 case OPTION_CHANGE_SECTION_LMA:
1928 case OPTION_CHANGE_SECTION_VMA:
1929 {
1930 const char *s;
1931 int len;
1932 char *name;
1933 char *option;
1934 bfd_vma val;
1935 enum change_action what;
1936
1937 switch (c)
1938 {
1939 case OPTION_CHANGE_SECTION_ADDRESS: option = "--change-section-address"; break;
1940 case OPTION_CHANGE_SECTION_LMA: option = "--change-section-lma"; break;
1941 case OPTION_CHANGE_SECTION_VMA: option = "--change-section-vma"; break;
1942 }
1943
1944 s = strchr (optarg, '=');
1945 if (s == NULL)
1946 {
1947 s = strchr (optarg, '+');
1948 if (s == NULL)
1949 {
1950 s = strchr (optarg, '-');
1951 if (s == NULL)
1952 fatal (_("bad format for %s"), option);
1953 }
1954 }
1955
1956 len = s - optarg;
1957 name = (char *) xmalloc (len + 1);
1958 strncpy (name, optarg, len);
1959 name[len] = '\0';
1960
1961 p = find_section_list (name, true);
1962
1963 val = parse_vma (s + 1, option);
1964
1965 switch (*s)
1966 {
1967 case '=': what = CHANGE_SET; break;
1968 case '-': val = - val; /* Drop through. */
1969 case '+': what = CHANGE_MODIFY; break;
1970 }
1971
1972 switch (c)
1973 {
1974 case OPTION_CHANGE_SECTION_ADDRESS:
1975 p->change_vma = what;
1976 p->vma_val = val;
1977 /* Drop through. */
1978
1979 case OPTION_CHANGE_SECTION_LMA:
1980 p->change_lma = what;
1981 p->lma_val = val;
1982 break;
1983
1984 case OPTION_CHANGE_SECTION_VMA:
1985 p->change_vma = what;
1986 p->vma_val = val;
1987 break;
1988 }
1989 }
1990 break;
1991 case OPTION_CHANGE_ADDRESSES:
1992 change_section_address = parse_vma (optarg, "--change-addresses");
1993 change_start = change_section_address;
1994 break;
1995 case OPTION_CHANGE_WARNINGS:
1996 change_warn = true;
1997 break;
1998 case OPTION_CHANGE_LEADING_CHAR:
1999 change_leading_char = true;
2000 break;
2001 case OPTION_DEBUGGING:
2002 convert_debugging = true;
2003 break;
2004 case OPTION_GAP_FILL:
2005 {
2006 bfd_vma gap_fill_vma;
2007
2008 gap_fill_vma = parse_vma (optarg, "--gap-fill");
2009 gap_fill = (bfd_byte) gap_fill_vma;
2010 if ((bfd_vma) gap_fill != gap_fill_vma)
2011 {
2012 char buff[20];
2013
2014 sprintf_vma (buff, gap_fill_vma);
2015
2016 non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
2017 buff, gap_fill);
2018 }
2019 gap_fill_set = true;
2020 }
2021 break;
2022 case OPTION_NO_CHANGE_WARNINGS:
2023 change_warn = false;
2024 break;
2025 case OPTION_PAD_TO:
2026 pad_to = parse_vma (optarg, "--pad-to");
2027 pad_to_set = true;
2028 break;
2029 case OPTION_REMOVE_LEADING_CHAR:
2030 remove_leading_char = true;
2031 break;
2032 case OPTION_SET_SECTION_FLAGS:
2033 {
2034 const char *s;
2035 int len;
2036 char *name;
2037
2038 s = strchr (optarg, '=');
2039 if (s == NULL)
2040 fatal (_("bad format for --set-section-flags"));
2041
2042 len = s - optarg;
2043 name = (char *) xmalloc (len + 1);
2044 strncpy (name, optarg, len);
2045 name[len] = '\0';
2046
2047 p = find_section_list (name, true);
2048
2049 p->set_flags = true;
2050 p->flags = parse_flags (s + 1);
2051 }
2052 break;
2053 case OPTION_SET_START:
2054 set_start = parse_vma (optarg, "--set-start");
2055 set_start_set = true;
2056 break;
2057 case 0:
2058 break; /* we've been given a long option */
2059 case 'h':
2060 copy_usage (stdout, 0);
2061 default:
2062 copy_usage (stderr, 1);
2063 }
2064 }
2065
2066 if (show_version)
2067 print_version ("objcopy");
2068
2069 if (copy_byte >= interleave)
2070 fatal (_("byte number must be less than interleave"));
2071
2072 if (optind == argc || optind + 2 < argc)
2073 copy_usage (stderr, 1);
2074
2075 input_filename = argv[optind];
2076 if (optind + 1 < argc)
2077 output_filename = argv[optind + 1];
2078
2079 /* Default is to strip no symbols. */
2080 if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
2081 strip_symbols = STRIP_NONE;
2082
2083 if (output_target == (char *) NULL)
2084 output_target = input_target;
2085
2086 if (preserve_dates)
2087 {
2088 if (stat (input_filename, &statbuf) < 0)
2089 fatal (_("Cannot stat: %s: %s"), input_filename, strerror (errno));
2090 }
2091
2092 /* If there is no destination file then create a temp and rename
2093 the result into the input. */
2094
2095 if (output_filename == (char *) NULL)
2096 {
2097 char *tmpname = make_tempname (input_filename);
2098
2099 copy_file (input_filename, tmpname, input_target, output_target);
2100 if (status == 0)
2101 {
2102 if (preserve_dates)
2103 set_times (tmpname, &statbuf);
2104 smart_rename (tmpname, input_filename);
2105 }
2106 else
2107 unlink (tmpname);
2108 }
2109 else
2110 {
2111 copy_file (input_filename, output_filename, input_target, output_target);
2112 if (status == 0 && preserve_dates)
2113 set_times (output_filename, &statbuf);
2114 }
2115
2116 if (change_warn)
2117 {
2118 for (p = change_sections; p != NULL; p = p->next)
2119 {
2120 if (! p->used)
2121 {
2122 if (p->change_vma != CHANGE_IGNORE)
2123 {
2124 char buff [20];
2125
2126 sprintf_vma (buff, p->vma_val);
2127
2128 /* xgettext:c-format */
2129 non_fatal (_("Warning: --change-section-vma %s%c0x%s never used"),
2130 p->name,
2131 p->change_vma == CHANGE_SET ? '=' : '+',
2132 buff);
2133 }
2134
2135 if (p->change_lma != CHANGE_IGNORE)
2136 {
2137 char buff [20];
2138
2139 sprintf_vma (buff, p->lma_val);
2140
2141 /* xgettext:c-format */
2142 non_fatal (_("Warning: --change-section-lma %s%c0x%s never used"),
2143 p->name,
2144 p->change_lma == CHANGE_SET ? '=' : '+',
2145 buff);
2146 }
2147 }
2148 }
2149 }
2150
2151 return 0;
2152 }
2153
2154 int
2155 main (argc, argv)
2156 int argc;
2157 char *argv[];
2158 {
2159 #ifdef HAVE_SETLOCALE
2160 setlocale (LC_MESSAGES, "");
2161 #endif
2162 bindtextdomain (PACKAGE, LOCALEDIR);
2163 textdomain (PACKAGE);
2164
2165 program_name = argv[0];
2166 xmalloc_set_program_name (program_name);
2167
2168 START_PROGRESS (program_name, 0);
2169
2170 strip_symbols = STRIP_UNDEF;
2171 discard_locals = LOCALS_UNDEF;
2172
2173 bfd_init ();
2174 set_default_bfd_target ();
2175
2176 if (is_strip < 0)
2177 {
2178 int i = strlen (program_name);
2179 is_strip = (i >= 5 && strcmp (program_name + i - 5, "strip") == 0);
2180 }
2181
2182 if (is_strip)
2183 strip_main (argc, argv);
2184 else
2185 copy_main (argc, argv);
2186
2187 END_PROGRESS (program_name);
2188
2189 return status;
2190 }
This page took 0.078961 seconds and 5 git commands to generate.