home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / binutils / objcopy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-20  |  17.4 KB  |  676 lines

  1. /* objcopy.c -- copy object file from input to output, optionally massaging it.
  2.    Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Binutils.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22. #include "bucomm.h"
  23. #include <getopt.h>
  24.  
  25. asymbol       **sympp;
  26. char           *input_target = NULL;
  27. char           *output_target = NULL;
  28. char           *input_filename = NULL;
  29. char           *output_filename = NULL;
  30.  
  31.  
  32. static void     setup_sections();
  33. static void     copy_sections();
  34. static boolean verbose;
  35.  
  36. /* This flag distinguishes between strip and objcopy:
  37.    1 means this is 'strip'; 0 means this is 'objcopy'.
  38.    -1 means if we should use argv[0] to decide. */
  39. extern int is_strip;
  40.  
  41. int          show_version = 0;
  42.  
  43. enum strip_action
  44. {
  45.   strip_undef,
  46.   strip_none,            /* don't strip */
  47.   strip_debug,            /* strip all debugger symbols */
  48.   strip_all            /* strip all symbols */
  49. };
  50.  
  51. /* Which symbols to remove. */
  52. enum strip_action strip_symbols;
  53.  
  54. enum locals_action
  55. {
  56.   locals_undef,
  57.   locals_start_L,        /* discard locals starting with L */
  58.   locals_all            /* discard all locals */
  59. };
  60.  
  61. /* Which local symbols to remove. */
  62. enum locals_action discard_locals;
  63.  
  64. /* Options to handle if running as "strip". */
  65.  
  66. struct option strip_options[] = {
  67.     {"strip-all",    no_argument, 0, 's'},
  68.     {"strip-debug",    no_argument, 0, 'S'},
  69.     {"discard-all",    no_argument, 0, 'x'},
  70.     {"discard-locals",    no_argument, 0, 'X'},
  71.     {"input-format",    required_argument, 0, 'I'},
  72.     {"output-format",    required_argument, 0, 'O'},
  73.     {"format",        required_argument, 0, 'F'},
  74.     {"target",        required_argument, 0, 'F'},
  75.  
  76.     {"version",         no_argument, 0, 'V'},
  77.     {"verbose",         no_argument, 0, 'v'},
  78.     {0, no_argument, 0, 0}
  79. };
  80.  
  81. /* Options to handle if running as "objcopy". */
  82.  
  83. struct option copy_options[] = {
  84.     {"strip-all",    no_argument, 0, 'S'},
  85.     {"strip-debug",    no_argument, 0, 'g'},
  86.     {"discard-all",    no_argument, 0, 'x'},
  87.     {"discard-locals",    no_argument, 0, 'X'},
  88.     {"input-format",    required_argument, 0, 'I'},
  89.     {"output-format",    required_argument, 0, 'O'},
  90.     {"format",        required_argument, 0, 'F'},
  91.     {"target",        required_argument, 0, 'F'},
  92.     
  93.     {"version",         no_argument, 0, 'V'},
  94.     {"verbose",         no_argument, 0, 'v'},
  95.     {0,            no_argument, 0, 0}
  96. };
  97.  
  98. /* IMPORTS */
  99. extern char    *program_name;
  100. extern char    *program_version;
  101.  
  102.  
  103. static
  104. void            
  105. copy_usage()
  106. {
  107.     fprintf(stderr, "\
  108. Usage: %s [-vVSgxX] [-I format] [-O format] [-F format]\n\
  109.        [--format=format] [--target=format] [--input-format=format]\n\
  110.        [--output-format=format] [--strip-all] [--strip-debug]\n\
  111.        [--discard-all] [--discard-locals] [--verbose] [--version]\n\
  112.        in-file [out-file]\n", program_name);
  113.     exit(1);
  114. }
  115.  
  116. static
  117. void            
  118. strip_usage()
  119. {
  120.     fprintf(stderr, "strip %s\n\
  121. Usage: %s [-vVsSgxX] [-I format] [-O format] [-F format]\n\
  122.        [--format=format] [--target=format] [--input-format=format]\n\
  123.        [--output-format=format] [--strip-all] [--strip-debug]\n\
  124.        [--discard-all] [--discard-locals] [--verbose] [--version] file...\n",
  125.         program_version, program_name);
  126.     exit(1);
  127. }
  128.  
  129.  
  130. /* Create a temp file in the same directory as supplied */
  131. static
  132. char *
  133. make_tempname(filename)
  134. char *filename;
  135. {
  136.     static char template[] = "stXXXXXX";
  137.     char *tmpname;
  138.     char *      slash = strrchr( filename, '/' );
  139.     if (slash != (char *)NULL){
  140.     *slash = 0;
  141.     tmpname = xmalloc(strlen(filename) + sizeof(template) + 1 );
  142.     strcpy(tmpname, filename);
  143.     strcat(tmpname, "/" );
  144.     strcat(tmpname, template);
  145.     mktemp(tmpname );
  146.     *slash = '/';
  147.     } else {
  148.     tmpname = xmalloc(sizeof(template));
  149.     strcpy(tmpname, template);
  150.     mktemp(tmpname);
  151.     }
  152.     return tmpname;
  153. }
  154.  
  155. /*
  156.    All the symbols have been read in and point to their owning input section.
  157.    They have been relocated to that they are all relative to the base of
  158.    their owning section. On the way out, all the symbols will be relocated to
  159.    their new location in the output file, through some complex sums.
  160.  
  161. */
  162. static void
  163. mangle_sections(ibfd, obfd)
  164.     bfd            *ibfd;
  165.     bfd            *obfd;
  166. {
  167.     asection       *current = ibfd->sections;
  168.     for (; current != NULL; current = current->next) {
  169.     current->output_section = bfd_get_section_by_name(obfd, current->name);
  170.     current->output_offset = 0;
  171.     }
  172. }
  173.  
  174. /* Choose which symbol entries to copy;
  175.    compact them downward to get rid of the rest.
  176.    Return the number of symbols to be printed.  */
  177. static unsigned int
  178. filter_symbols (abfd, syms, symcount)
  179.      bfd *abfd;
  180.      asymbol **syms;
  181.      unsigned long symcount;
  182. {
  183.   asymbol **from, **to;
  184.   unsigned int dst_count = 0;
  185.   asymbol *sym;
  186.   char locals_prefix = bfd_get_symbol_leading_char(abfd) == '_' ? 'L' : '.';
  187.  
  188.   unsigned int src_count;
  189.   for (from = to = syms, src_count = 0; src_count <symcount; src_count++) {
  190.     int keep = 0;
  191.  
  192.     flagword flags = (from[src_count])->flags;
  193.     sym = from[src_count];
  194.     if ((flags & BSF_GLOBAL) /* Keep if external */
  195.     || (sym->section == &bfd_und_section)
  196.     || (bfd_is_com_section (sym->section)))
  197.     keep = 1;
  198.     else if ((flags & BSF_DEBUGGING) != 0) /* debugging symbol */
  199.     keep = strip_symbols != strip_debug;
  200.     else /* local symbol */
  201.     keep = (discard_locals != locals_all)
  202.                 && !(discard_locals == locals_start_L &&
  203.                      sym->name[0] == locals_prefix);
  204.  
  205.  
  206.     if (keep) {
  207.       to[dst_count++] = from[src_count];
  208.     }
  209.   }
  210.  
  211.   return dst_count;
  212. }
  213.  
  214. static 
  215. void
  216. copy_object(ibfd, obfd)
  217. bfd *ibfd;
  218. bfd *obfd;
  219. {
  220.  
  221.     unsigned int symcount;
  222.  
  223.  
  224.     if (!bfd_set_format(obfd, bfd_get_format(ibfd)))
  225.     bfd_fatal(output_filename);
  226.  
  227.  
  228.     if (verbose)
  229.     printf("copy from %s(%s) to %s(%s)\n",
  230.            ibfd->filename, ibfd->xvec->name,
  231.            obfd->filename, obfd->xvec->name);
  232.  
  233.     if ((bfd_set_start_address(obfd, bfd_get_start_address(ibfd)) == false) 
  234.     ||
  235.     (bfd_set_file_flags(obfd, (bfd_get_file_flags(ibfd) &
  236.                    (HAS_LINENO | HAS_DEBUG |
  237.                     HAS_RELOC | HAS_SYMS | D_PAGED |
  238.                      HAS_LOCALS))) == false)) {
  239.       bfd_fatal(bfd_get_filename(ibfd));
  240.     }
  241.  
  242.     /* Copy architecture of input file to output file */
  243.     if (!bfd_set_arch_mach(obfd, bfd_get_arch(ibfd),
  244.                bfd_get_mach(ibfd))) {
  245.     fprintf(stderr, "Output file cannot represent architecture %s\n",
  246.         bfd_printable_arch_mach(bfd_get_arch(ibfd),
  247.                     bfd_get_mach(ibfd)));
  248.     }
  249.     if (!bfd_set_format(obfd, bfd_get_format(ibfd)))
  250.     {
  251.         bfd_fatal(ibfd->filename);
  252.     }
  253.  
  254.     sympp = (asymbol **) xmalloc(get_symtab_upper_bound(ibfd));
  255.     symcount = bfd_canonicalize_symtab(ibfd, sympp);
  256.  
  257.     if (strip_symbols == strip_debug || discard_locals != locals_undef)
  258.       symcount = filter_symbols (ibfd, sympp, symcount);
  259.  
  260.  
  261.     bfd_set_symtab(obfd, sympp,
  262.            strip_symbols == strip_all ? 0 : symcount);
  263.     
  264.     /*
  265.       bfd mandates that all output sections be created and sizes set before
  266.       any output is done.  Thus, we traverse all sections twice.
  267.       */
  268.     bfd_map_over_sections(ibfd, setup_sections, (void *) obfd);
  269.     bfd_map_over_sections(ibfd, copy_sections, (void *) obfd);
  270.     mangle_sections(ibfd, obfd);
  271. }
  272. static
  273. char *
  274. cat(a,b,c)
  275. char *a;
  276. char *b;
  277. char *c;
  278. {
  279.     int size = strlen(a) + strlen(b) + strlen(c);
  280.     char *r = xmalloc(size+1);
  281.     strcpy(r,a);
  282.     strcat(r,b);
  283.     strcat(r,c);
  284.     return r;
  285. }
  286.  
  287. static void 
  288. copy_archive(ibfd, obfd)
  289. bfd *ibfd;
  290. bfd *obfd;
  291. {
  292.     bfd **ptr = &obfd->archive_head;
  293.     bfd *this_element;
  294.     /* Read each archive element in turn from the input, copy the
  295.        contents to a temp file, and keep the temp file handle */
  296.     char *dir = cat("./#",make_tempname(""),"cd");
  297.  
  298.     /* Make a temp directory to hold the contents */
  299.     mkdir(dir,0777);
  300.     obfd->has_armap = ibfd->has_armap;
  301.     this_element = bfd_openr_next_archived_file(ibfd, NULL);
  302.     ibfd->archive_head = this_element;
  303.     while (this_element != (bfd *)NULL) {
  304.  
  305.     /* Create an output file for this member */
  306.     char *output_name = cat(dir, "/",this_element->filename);
  307.     bfd *output_bfd = bfd_openw(output_name, output_target);
  308.  
  309.     if (!bfd_set_format(obfd, bfd_get_format(ibfd)))
  310.         bfd_fatal(output_filename);
  311.  
  312.     if (output_bfd == (bfd *)NULL) {
  313.         bfd_fatal(output_name);
  314.     }
  315.     if (bfd_check_format(this_element, bfd_object) == true) {
  316.         copy_object(this_element, output_bfd);
  317.     }
  318.  
  319.     bfd_close(output_bfd);
  320.     /* Now open the newly output file and attatch to our list */
  321.     output_bfd = bfd_openr(output_name, output_target);
  322.     /* Mark it for deletion */
  323.  
  324.     *ptr = output_bfd;
  325.  
  326.     ptr = &output_bfd->next;
  327.     this_element->next = bfd_openr_next_archived_file(ibfd, this_element);
  328.     this_element = this_element->next;
  329.  
  330.     }
  331.     *ptr = (bfd *)NULL;
  332.  
  333.     if (!bfd_close(obfd))
  334.     bfd_fatal(output_filename);
  335.  
  336.     /* Now delete all the files that we opened.
  337.        Construct their names again, unfortunately, but so what;
  338.        we're about to exit anyway. */
  339.     for (this_element = ibfd->archive_head;
  340.      this_element != (bfd *)NULL;
  341.      this_element = this_element->next) 
  342.     {
  343.     unlink(cat(dir,"/",this_element->filename));
  344.     }
  345.     rmdir(dir);
  346.     if (!bfd_close(ibfd))
  347.     bfd_fatal(input_filename);
  348.  
  349. }
  350.  
  351. static
  352. void
  353. copy_file(input_filename, output_filename)
  354.     char           *input_filename;
  355.     char           *output_filename;
  356. {
  357.   bfd            *ibfd;
  358.  
  359.   ibfd = bfd_openr(input_filename, input_target);
  360.   if (ibfd == NULL)
  361.     bfd_fatal(input_filename);
  362.  
  363.   if (bfd_check_format(ibfd, bfd_object)) {
  364.     bfd *obfd;
  365.  
  366.     if (output_target == NULL)
  367.       output_target = bfd_get_target(ibfd);
  368.  
  369.     obfd = bfd_openw(output_filename, output_target);
  370.     if (obfd == NULL)
  371.       bfd_fatal(output_filename);
  372.  
  373.     copy_object(ibfd, obfd);
  374.  
  375.     if (ibfd->flags & EXEC_P)
  376.     obfd->flags |= EXEC_P;
  377.     if (!bfd_close(obfd))
  378.       bfd_fatal(output_filename);
  379.  
  380.     if (!bfd_close(ibfd))
  381.       bfd_fatal(input_filename);
  382.   }
  383.   else if (bfd_check_format(ibfd, bfd_archive)) {
  384.     bfd *obfd;
  385.  
  386.     if (output_target == NULL)
  387.       output_target = bfd_get_target(ibfd);
  388.  
  389.     obfd = bfd_openw(output_filename, output_target);
  390.     if (obfd == NULL)
  391.       bfd_fatal(output_filename);
  392.     copy_archive(ibfd, obfd);
  393.   }
  394. }
  395.  
  396.  
  397.  
  398. /** Actually do the work */
  399. static void
  400. setup_sections(ibfd, isection, obfd)
  401.     bfd            *ibfd;
  402.     sec_ptr         isection;
  403.     bfd            *obfd;
  404. {
  405.     sec_ptr         osection;
  406.     char           *err;
  407.  
  408.     osection = bfd_get_section_by_name(obfd, bfd_section_name(ibfd, isection));
  409.     if (osection == NULL) {
  410.     osection = bfd_make_section(obfd, bfd_section_name(ibfd, isection));
  411.     if (osection == NULL) {
  412.         err = "making";
  413.         goto loser;
  414.     }
  415.     }
  416.  
  417.     if (!bfd_set_section_size(obfd,
  418.                   osection,
  419.                   bfd_section_size(ibfd, isection))) {
  420.     err = "size";
  421.     goto loser;
  422.     }
  423.  
  424.     if (bfd_set_section_vma(obfd,
  425.                 osection,
  426.                 bfd_section_vma(ibfd, isection))
  427.     == false) {
  428.     err = "vma";
  429.     goto loser;
  430.     }                /* on error */
  431.  
  432.     if (bfd_set_section_alignment(obfd,
  433.                   osection,
  434.                   bfd_section_alignment(ibfd, isection))
  435.     == false) {
  436.     err = "alignment";
  437.     goto loser;
  438.     }                /* on error */
  439.  
  440.     if (!bfd_set_section_flags(obfd, osection,
  441.                    bfd_get_section_flags(ibfd, isection))) {
  442.     err = "flags";
  443.     goto loser;
  444.     }
  445.  
  446.     /* All went well */
  447.     return;
  448.  
  449. loser:
  450.     fprintf(stderr, "%s: file \"%s\", section \"%s\": error in %s: %s\n",
  451.         program_name,
  452.         bfd_get_filename(ibfd), bfd_section_name(ibfd, isection),
  453.         err, bfd_errmsg(bfd_error));
  454.     exit(1);
  455. }                /* setup_sections() */
  456.  
  457. /*
  458. Copy all the section related data from an input section
  459. to an output section
  460.  
  461. If stripping then don't copy any relocation info
  462. */
  463. static void
  464. copy_sections(ibfd, isection, obfd)
  465.     bfd            *ibfd;
  466.     sec_ptr         isection;
  467.     bfd            *obfd;
  468. {
  469.  
  470.   arelent       **relpp;
  471.   int             relcount;
  472.   sec_ptr         osection;
  473.   bfd_size_type   size;
  474.   osection = bfd_get_section_by_name(obfd,
  475.                      bfd_section_name(ibfd, isection));
  476.  
  477.   size = bfd_get_section_size_before_reloc(isection);
  478.  
  479.   if (size == 0)
  480.     return;
  481.  
  482.   if (strip_symbols == strip_all
  483.       || bfd_get_reloc_upper_bound(ibfd, isection) == 0) 
  484.     {
  485.       bfd_set_reloc(obfd, osection, (arelent **)NULL, 0);
  486.     } 
  487.   else 
  488.     {
  489.       relpp = (arelent **) xmalloc(bfd_get_reloc_upper_bound(ibfd, isection));
  490.       relcount = bfd_canonicalize_reloc(ibfd, isection, relpp, sympp);
  491.       bfd_set_reloc(obfd, osection, relpp, relcount);
  492.     }
  493.  
  494.   isection->_cooked_size = isection->_raw_size;
  495.   isection->reloc_done =true;
  496.   
  497.  
  498.   if (bfd_get_section_flags(ibfd, isection) & SEC_HAS_CONTENTS) 
  499.     {
  500.       PTR memhunk = (PTR) xmalloc((unsigned)size);
  501.  
  502.       if (!bfd_get_section_contents(ibfd, isection, memhunk, (file_ptr) 0, size))
  503.     bfd_fatal(bfd_get_filename(ibfd));
  504.  
  505.       if (!bfd_set_section_contents(obfd, osection, memhunk, (file_ptr)0, size))
  506.     bfd_fatal(bfd_get_filename(obfd));
  507.       free(memhunk);
  508.     }
  509.  
  510.  
  511. }
  512. int
  513. main(argc, argv)
  514.     int             argc;
  515.     char           *argv[];
  516. {
  517.   int             i;
  518.   int c;            /* sez which option char */
  519.  
  520.   program_name = argv[0];
  521.  
  522.   strip_symbols = strip_undef;    /* default is to strip everything.  */
  523.   discard_locals = locals_undef;
  524.  
  525.   bfd_init();
  526.  
  527.   if (is_strip < 0) {
  528.       i = strlen (program_name);
  529.       is_strip = (i >= 5 && strcmp(program_name+i-5,"strip"));
  530.   }
  531.  
  532.   if (is_strip) {
  533.     
  534.       while ((c = getopt_long(argc, argv, "I:O:F:sSgxXVv",
  535.                   strip_options, (int *) 0))
  536.          != EOF) {
  537.       switch (c) {
  538.         case 'I':
  539.           input_target = optarg;
  540.         case 'O':
  541.           output_target = optarg;
  542.           break;
  543.         case 'F':
  544.           input_target = output_target = optarg;
  545.           break;
  546.  
  547.         case 's':
  548.           strip_symbols = strip_all;
  549.           break;
  550.         case 'S':
  551.         case 'g':
  552.           strip_symbols = strip_debug;
  553.           break;
  554.         case 'x':
  555.           discard_locals = locals_all;
  556.           break;
  557.         case 'X':
  558.           discard_locals = locals_start_L;
  559.           break;
  560.         case 'v':
  561.           verbose = true;
  562.           show_version = true;
  563.           break;
  564.         case 'V':
  565.           show_version = true;
  566.           break;
  567.         case  0:
  568.           break;        /* we've been given a long option */
  569.         default:
  570.           strip_usage ();
  571.       }
  572.       }
  573.       
  574.       i = optind;
  575.  
  576.       /* Default is to strip all symbols.  */
  577.       if (strip_symbols == strip_undef && discard_locals == locals_undef)
  578.       strip_symbols = strip_all;
  579.  
  580.       if (output_target == (char *) NULL)
  581.       output_target = input_target;
  582.  
  583.       if (show_version)
  584.       printf ("%s version %s\n", program_name, program_version);
  585.       else if (i == argc)
  586.       strip_usage();
  587.       for ( ; i < argc; i++) {
  588.         char *tmpname = make_tempname(argv[i]);
  589.         copy_file(argv[i], tmpname);
  590.         rename(tmpname, argv[i]);
  591.       }
  592.       return 0;
  593.     }
  594.  
  595.   /* Invoked as "objcopy", not "strip" */
  596.  
  597.   while ((c = getopt_long(argc, argv, "I:s:O:d:F:b:SgxXVv",
  598.               strip_options, (int *) 0))
  599.      != EOF) {
  600.       switch (c) {
  601.     case 'I':
  602.     case 's': /* "source" - 'I' is preferred */
  603.       input_target = optarg;
  604.     case 'O':
  605.     case 'd': /* "destination" - 'O' is preferred */
  606.       output_target = optarg;
  607.       break;
  608.     case 'F':
  609.     case 'b': /* "both" - 'F' is preferred */
  610.       input_target = output_target = optarg;
  611.       break;
  612.       
  613.     case 'S':
  614.       strip_symbols = strip_all;
  615.       break;
  616.     case 'g':
  617.       strip_symbols = strip_debug;
  618.       break;
  619.     case 'x':
  620.       discard_locals = locals_all;
  621.       break;
  622.     case 'X':
  623.       discard_locals = locals_start_L;
  624.       break;
  625.     case 'v':
  626.       verbose = true;
  627.       show_version = true;
  628.       break;
  629.     case 'V':
  630.       show_version = true;
  631.       break;
  632.     case  0:
  633.       break;        /* we've been given a long option */
  634.     default:
  635.       copy_usage ();
  636.       }
  637.   }
  638.       
  639.   if (show_version)
  640.     printf ("%s version %s\n", program_name, program_version);
  641.  
  642.   if (optind == argc)
  643.       if (show_version)
  644.       exit(0);
  645.       else
  646.       copy_usage();
  647.  
  648.   input_filename = argv[optind];
  649.   if (optind + 1 < argc)
  650.       output_filename = argv[optind+1];
  651.  
  652.   /* Default is to strip no symbols.  */
  653.   if (strip_symbols == strip_undef && discard_locals == locals_undef)
  654.       strip_symbols = strip_none;
  655.  
  656.   if (input_filename == (char *) NULL)
  657.     copy_usage();
  658.  
  659.   if (output_target == (char *) NULL)
  660.     output_target = input_target;
  661.  
  662.   /* If there is no  destination file then create a temp and rename
  663.      the result into the input */
  664.  
  665.   if (output_filename == (char *)NULL) {
  666.     char *    tmpname = make_tempname(input_filename);
  667.     copy_file(input_filename, tmpname);
  668.     output_filename = input_filename;
  669.     rename(tmpname, input_filename);
  670.   }
  671.   else {
  672.     copy_file(input_filename, output_filename);
  673.   }
  674.   return 0;
  675. }
  676.