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