home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / ld / ldwrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  3.3 KB  |  128 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2.  
  3. This file is part of GLD, the Gnu Linker.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.    This module writes out the final image by reading sections from the
  21.    input files, relocating them and writing them out
  22.  
  23.    There are two main paths through this module, one for normal
  24.    operation and one for partial linking.
  25.  
  26.    During  normal operation, raw section data is read along with the
  27.    associated relocation information, the relocation info applied and
  28.    the section data written out on a section by section basis.
  29.  
  30.    When partially linking, all the relocation records are read to work
  31.    out how big the output relocation vector will be. Then raw data is
  32.    read, relocated and written section by section.
  33.  
  34.    Written by Steve Chamberlain sac@cygnus.com
  35.  
  36. */
  37.  
  38.  
  39. #include "bfd.h"
  40. #include "sysdep.h"
  41.  
  42. #include "ldlang.h"
  43. #include "ld.h"
  44. #include "ldwrite.h"
  45. #include "ldmisc.h"
  46. #include "ldsym.h"
  47. #include "ldgram.h"
  48. #include "relax.h"
  49.  
  50.  
  51. extern bfd *output_bfd;
  52. extern bfd_size_type largest_section;
  53. ld_config_type config;
  54.  
  55. static void
  56. DEFUN (read_relocs, (abfd, section, symbols),
  57.        bfd * abfd AND
  58.        asection * section AND
  59.        asymbol ** symbols)
  60. {
  61.   /* Work out the output section ascociated with this input section */
  62.   asection *output_section = section->output_section;
  63.  
  64.   bfd_size_type reloc_size = bfd_get_reloc_upper_bound (abfd, section);
  65.   arelent **reloc_vector = (arelent **) ldmalloc (reloc_size);
  66.  
  67.   if (bfd_canonicalize_reloc (abfd,
  68.                   section,
  69.                   reloc_vector,
  70.                   symbols))
  71.     {
  72.       output_section->reloc_count += section->reloc_count;
  73.     }
  74. }
  75.  
  76.  
  77. static void
  78. DEFUN_VOID (setup_rel)
  79. {
  80.   /*
  81.     Run through each section of each file and work work out the total
  82.     number of relocation records which will finally be in each output
  83.     section
  84.     */
  85.  
  86.   LANG_FOR_EACH_INPUT_SECTION
  87.   (statement, abfd, section,
  88.    (read_relocs (abfd, section, statement->asymbols)));
  89.  
  90.  
  91.  
  92.   /*
  93.     Now run though all the output sections and allocate the space for
  94.     all the relocations
  95.     */
  96.   LANG_FOR_EACH_OUTPUT_SECTION
  97.     (section,
  98.      (section->orelocation =
  99.       (arelent **) ldmalloc ((bfd_size_type) (sizeof (arelent **) *
  100.                           section->reloc_count)),
  101.       section->reloc_count = 0));
  102. }
  103.  
  104. void
  105. DEFUN_VOID (ldwrite)
  106. {
  107.   PTR data_area = (PTR) ldmalloc (largest_section);
  108.  
  109.   ldsym_write ();
  110.  
  111.   if (config.relocateable_output == true)
  112.     setup_rel ();
  113.  
  114.   write_relax (output_bfd, data_area, config.relocateable_output);
  115.  
  116.   free (data_area);
  117.  
  118.   /* Output the symbol table (both globals and locals).  */
  119.  
  120.   /* Print a map, if requested and possible.  */
  121.  
  122.   if (config.map_file)
  123.   {
  124.     ldsym_print_symbol_table ();
  125.     lang_map ();
  126.   }
  127. }
  128.