home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / ld / ldwrite.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  13KB  |  490 lines

  1. /* ldwrite.c -- write out the linked file
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.    Written by Steve Chamberlain sac@cygnus.com
  4.  
  5. This file is part of GLD, the Gnu Linker.
  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 02111-1307, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "bfdlink.h"
  24. #include "libiberty.h"
  25.  
  26. #include "ld.h"
  27. #include "ldexp.h"
  28. #include "ldlang.h"
  29. #include "ldwrite.h"
  30. #include "ldmisc.h"
  31. #include "ldgram.h"
  32. #include "ldmain.h"
  33.  
  34. static void build_link_order PARAMS ((lang_statement_union_type *));
  35.  
  36. /* Build link_order structures for the BFD linker.  */
  37.  
  38. static void
  39. build_link_order (statement)
  40.      lang_statement_union_type *statement;
  41. {
  42.   switch (statement->header.type)
  43.     {
  44.     case lang_data_statement_enum:
  45.       {
  46.     asection *output_section;
  47.     struct bfd_link_order *link_order;
  48.     bfd_vma value;
  49.  
  50.     output_section = statement->data_statement.output_section;
  51.     ASSERT (output_section->owner == output_bfd);
  52.  
  53.     link_order = bfd_new_link_order (output_bfd, output_section);
  54.     if (link_order == NULL)
  55.       einfo ("%P%F: bfd_new_link_order failed\n");
  56.  
  57.     link_order->type = bfd_data_link_order;
  58.     link_order->offset = statement->data_statement.output_vma;
  59.     link_order->u.data.contents = (bfd_byte *) xmalloc (QUAD_SIZE);
  60.  
  61.     value = statement->data_statement.value;
  62.  
  63.     /* If the endianness of the output BFD is not known, then we
  64.        base the endianness of the data on the first input file.
  65.        By convention, the bfd_put routines for an unknown
  66.        endianness are big endian, so we must swap here if the
  67.        input file is little endian.  */
  68.     if (! bfd_big_endian (output_bfd)
  69.         && ! bfd_little_endian (output_bfd))
  70.       {
  71.         boolean swap;
  72.  
  73.         swap = false;
  74.         if (command_line.endian == ENDIAN_LITTLE)
  75.           swap = true;
  76.         else if (command_line.endian == ENDIAN_UNSET)
  77.           {
  78.         LANG_FOR_EACH_INPUT_STATEMENT (s)
  79.           {
  80.             if (s->the_bfd != NULL)
  81.               {
  82.             if (bfd_little_endian (s->the_bfd))
  83.               swap = true;
  84.             break;
  85.               }
  86.           }
  87.           }
  88.  
  89.         if (swap)
  90.           {
  91.         bfd_byte buffer[8];
  92.  
  93.         switch (statement->data_statement.type)
  94.           {
  95.           case QUAD:
  96.             bfd_putl64 (value, buffer);
  97.             value = bfd_getb64 (buffer);
  98.             break;
  99.           case LONG:
  100.             bfd_putl32 (value, buffer);
  101.             value = bfd_getb32 (buffer);
  102.             break;
  103.           case SHORT:
  104.             bfd_putl16 (value, buffer);
  105.             value = bfd_getb16 (buffer);
  106.             break;
  107.           case BYTE:
  108.             break;
  109.           default:
  110.             abort ();
  111.           }
  112.           }
  113.       }
  114.  
  115.     ASSERT (output_section->owner == output_bfd);
  116.     switch (statement->data_statement.type)
  117.       {
  118.       case QUAD:
  119.         bfd_put_64 (output_bfd, value, link_order->u.data.contents);
  120.         link_order->size = QUAD_SIZE;
  121.         break;
  122.       case LONG:
  123.         bfd_put_32 (output_bfd, value, link_order->u.data.contents);
  124.         link_order->size = LONG_SIZE;
  125.         break;
  126.       case SHORT:
  127.         bfd_put_16 (output_bfd, value, link_order->u.data.contents);
  128.         link_order->size = SHORT_SIZE;
  129.         break;
  130.       case BYTE:
  131.         bfd_put_8 (output_bfd, value, link_order->u.data.contents);
  132.         link_order->size = BYTE_SIZE;
  133.         break;
  134.       default:
  135.         abort ();
  136.       }
  137.       }
  138.       break;
  139.  
  140.     case lang_reloc_statement_enum:
  141.       {
  142.     lang_reloc_statement_type *rs;
  143.     asection *output_section;
  144.     struct bfd_link_order *link_order;
  145.  
  146.     rs = &statement->reloc_statement;
  147.  
  148.     output_section = rs->output_section;
  149.     ASSERT (output_section->owner == output_bfd);
  150.  
  151.     link_order = bfd_new_link_order (output_bfd, output_section);
  152.     if (link_order == NULL)
  153.       einfo ("%P%F: bfd_new_link_order failed\n");
  154.  
  155.     link_order->offset = rs->output_vma;
  156.     link_order->size = bfd_get_reloc_size (rs->howto);
  157.  
  158.     link_order->u.reloc.p =
  159.       ((struct bfd_link_order_reloc *)
  160.        xmalloc (sizeof (struct bfd_link_order_reloc)));
  161.  
  162.     link_order->u.reloc.p->reloc = rs->reloc;
  163.     link_order->u.reloc.p->addend = rs->addend_value;
  164.  
  165.     if (rs->name == NULL)
  166.       {
  167.         link_order->type = bfd_section_reloc_link_order;
  168.         if (rs->section->owner == output_bfd)
  169.           link_order->u.reloc.p->u.section = rs->section;
  170.         else
  171.           {
  172.         link_order->u.reloc.p->u.section = rs->section->output_section;
  173.         link_order->u.reloc.p->addend += rs->section->output_offset;
  174.           }
  175.       }
  176.     else
  177.       {
  178.         link_order->type = bfd_symbol_reloc_link_order;
  179.         link_order->u.reloc.p->u.name = rs->name;
  180.       }
  181.       }
  182.       break;
  183.  
  184.     case lang_input_section_enum:
  185.       /* Create a new link_order in the output section with this
  186.      attached */
  187.       if (statement->input_section.ifile->just_syms_flag == false)
  188.     {
  189.       asection *i = statement->input_section.section;
  190.       asection *output_section = i->output_section;
  191.  
  192.       ASSERT (output_section->owner == output_bfd);
  193.  
  194.       if ((output_section->flags & SEC_HAS_CONTENTS) != 0)
  195.         {
  196.           struct bfd_link_order *link_order;
  197.  
  198.           link_order = bfd_new_link_order (output_bfd, output_section);
  199.  
  200.           if (i->flags & SEC_NEVER_LOAD)
  201.         {
  202.           /* We've got a never load section inside one which
  203.              is going to be output, we'll change it into a
  204.              fill link_order */
  205.           link_order->type = bfd_fill_link_order;
  206.           link_order->u.fill.value = 0;
  207.         }
  208.           else
  209.         {
  210.           link_order->type = bfd_indirect_link_order;
  211.           link_order->u.indirect.section = i;
  212.           ASSERT (i->output_section == output_section);
  213.         }
  214.           if (i->_cooked_size)
  215.         link_order->size = i->_cooked_size;
  216.           else
  217.         link_order->size = bfd_get_section_size_before_reloc (i);
  218.           link_order->offset = i->output_offset;
  219.         }
  220.     }
  221.       break;
  222.  
  223.     case lang_padding_statement_enum:
  224.       /* Make a new link_order with the right filler */
  225.       {
  226.     asection *output_section;
  227.     struct bfd_link_order *link_order;
  228.  
  229.     output_section = statement->padding_statement.output_section;
  230.     ASSERT (statement->padding_statement.output_section->owner
  231.         == output_bfd);
  232.     if ((output_section->flags & SEC_HAS_CONTENTS) != 0)
  233.       {
  234.         link_order = bfd_new_link_order (output_bfd, output_section);
  235.         link_order->type = bfd_fill_link_order;
  236.         link_order->size = statement->padding_statement.size;
  237.         link_order->offset = statement->padding_statement.output_offset;
  238.         link_order->u.fill.value = statement->padding_statement.fill;
  239.       }
  240.       }
  241.       break;
  242.  
  243.     default:
  244.       /* All the other ones fall through */
  245.       break;
  246.     }
  247. }
  248.  
  249. /* Call BFD to write out the linked file.  */
  250.  
  251.  
  252. /**********************************************************************/
  253.  
  254.  
  255. /* Wander around the input sections, make sure that
  256.    we'll never try and create an output section with more relocs
  257.    than will fit.. Do this by always assuming the worst case, and
  258.    creating new output sections with all the right bits */
  259. #define TESTIT 1
  260. static asection *
  261. clone_section (abfd, s, count)
  262.      bfd *abfd;
  263.      asection *s;
  264.      int *count;
  265. {
  266. #define SSIZE 8
  267.   char sname[SSIZE];        /* ??  find the name for this size */
  268.   asection *n;
  269.   struct bfd_link_hash_entry *h;
  270.   /* Invent a section name - use first five
  271.      chars of base section name and a digit suffix */
  272.   do
  273.     {
  274.       unsigned int i;
  275.       char b[6];
  276.       for (i = 0; i < sizeof (b) - 1 && s->name[i]; i++)
  277.     b[i] = s->name[i];
  278.       b[i] = 0;
  279.       sprintf (sname, "%s%d", b, (*count)++);
  280.     }
  281.   while (bfd_get_section_by_name (abfd, sname));
  282.  
  283.   n = bfd_make_section_anyway (abfd, xstrdup (sname));
  284.  
  285.   /* Create a symbol of the same name */
  286.  
  287.   h = bfd_link_hash_lookup (link_info.hash,
  288.                 sname, true, true, false);
  289.   h->type = bfd_link_hash_defined;
  290.   h->u.def.value = 0;
  291.   h->u.def.section = n   ;
  292.  
  293.  
  294.   n->flags = s->flags;
  295.   n->vma = s->vma;
  296.   n->user_set_vma = s->user_set_vma;
  297.   n->lma = s->lma;
  298.   n->_cooked_size = 0;
  299.   n->_raw_size = 0;
  300.   n->output_offset = s->output_offset;
  301.   n->output_section = n;
  302.   n->orelocation = 0;
  303.   n->reloc_count = 0;
  304.   n->alignment_power = s->alignment_power;
  305.   return n;
  306. }
  307.  
  308. #if TESTING
  309. static void 
  310. ds (s)
  311.      asection *s;
  312. {
  313.   struct bfd_link_order *l = s->link_order_head;
  314.   printf ("vma %x size %x\n", s->vma, s->_raw_size);
  315.   while (l)
  316.     {
  317.       if (l->type == bfd_indirect_link_order)
  318.     {
  319.       printf ("%8x %s\n", l->offset, l->u.indirect.section->owner->filename);
  320.     }
  321.       else
  322.     {
  323.       printf ("%8x something else\n", l->offset);
  324.     }
  325.       l = l->next;
  326.     }
  327.   printf ("\n");
  328. }
  329. dump (s, a1, a2)
  330.      char *s;
  331.      asection *a1;
  332.      asection *a2;
  333. {
  334.   printf ("%s\n", s);
  335.   ds (a1);
  336.   ds (a2);
  337. }
  338.  
  339. static void 
  340. sanity_check (abfd)
  341.      bfd *abfd;
  342. {
  343.   asection *s;
  344.   for (s = abfd->sections; s; s = s->next)
  345.     {
  346.       struct bfd_link_order *p;
  347.       bfd_vma prev = 0;
  348.       for (p = s->link_order_head; p; p = p->next)
  349.     {
  350.       if (p->offset > 100000)
  351.         abort ();
  352.       if (p->offset < prev)
  353.         abort ();
  354.       prev = p->offset;
  355.     }
  356.     }
  357. }
  358. #else
  359. #define sanity_check(a)
  360. #define dump(a, b, c)
  361. #endif
  362.  
  363.  
  364. void 
  365. split_sections (abfd, info)
  366.      bfd *abfd;
  367.      struct bfd_link_info *info;
  368. {
  369.   asection *original_sec;
  370.   int nsecs = abfd->section_count;
  371.   sanity_check (abfd);
  372.   /* look through all the original sections */
  373.   for (original_sec = abfd->sections;
  374.        original_sec && nsecs;
  375.        original_sec = original_sec->next, nsecs--)
  376.     {
  377.       boolean first = true;
  378.       int count = 0;
  379.       int lines = 0;
  380.       int relocs = 0;
  381.       struct bfd_link_order **pp;
  382.       bfd_vma vma = original_sec->vma;
  383.       bfd_vma shift_offset = 0;
  384.       asection *cursor = original_sec;
  385.  
  386.       /* count up the relocations and line entries to see if
  387.      anything would be too big to fit */
  388.       for (pp = &(cursor->link_order_head); *pp; pp = &((*pp)->next))
  389.     {
  390.       struct bfd_link_order *p = *pp;
  391.       int thislines = 0;
  392.       int thisrelocs = 0;
  393.       if (p->type == bfd_indirect_link_order)
  394.         {
  395.           asection *sec;
  396.  
  397.           sec = p->u.indirect.section;
  398.  
  399.           if (info->strip == strip_none
  400.           || info->strip == strip_some)
  401.         thislines = sec->lineno_count;
  402.  
  403.           if (info->relocateable)
  404.         thisrelocs = sec->reloc_count;
  405.  
  406.         }
  407.       else if (info->relocateable
  408.            && (p->type == bfd_section_reloc_link_order
  409.                || p->type == bfd_symbol_reloc_link_order))
  410.         thisrelocs++;
  411.  
  412.       if (! first
  413.           && (thisrelocs + relocs > config.split_by_reloc
  414.           || thislines + lines > config.split_by_reloc
  415.           || config.split_by_file))
  416.         {
  417.           /* create a new section and put this link order and the
  418.          following link orders into it */
  419.           struct bfd_link_order *l = p;
  420.           asection *n = clone_section (abfd, cursor, &count);
  421.           *pp = NULL;    /* Snip off link orders from old section */
  422.           n->link_order_head = l;    /* attach to new section */
  423.           pp = &n->link_order_head;
  424.  
  425.           /* change the size of the original section and
  426.          update the vma of the new one */
  427.  
  428.           dump ("before snip", cursor, n);
  429.  
  430.           n->_raw_size = cursor->_raw_size - l->offset;
  431.           cursor->_raw_size = l->offset;
  432.  
  433.           vma += cursor->_raw_size;
  434.           n->lma = n->vma = vma;
  435.  
  436.           shift_offset = l->offset;
  437.  
  438.           /* run down the chain and change the output section to
  439.          the right one, update the offsets too */
  440.  
  441.           while (l)
  442.         {
  443.           l->offset -= shift_offset;
  444.           if (l->type == bfd_indirect_link_order)
  445.             {
  446.               l->u.indirect.section->output_section = n;
  447.               l->u.indirect.section->output_offset = l->offset;
  448.             }
  449.           l = l->next;
  450.         }
  451.           dump ("after snip", cursor, n);
  452.           cursor = n;
  453.           relocs = thisrelocs;
  454.           lines = thislines;
  455.         }
  456.       else
  457.         {
  458.           relocs += thisrelocs;
  459.           lines += thislines;
  460.         }
  461.  
  462.       first = false;
  463.     }
  464.     }
  465.   sanity_check (abfd);
  466. }
  467. /**********************************************************************/
  468. void
  469. ldwrite ()
  470. {
  471.   /* Reset error indicator, which can typically something like invalid
  472.      format from openning up the .o files */
  473.   bfd_set_error (bfd_error_no_error);
  474.   lang_for_each_statement (build_link_order);
  475.  
  476.   if (config.split_by_reloc || config.split_by_file)
  477.     split_sections (output_bfd, &link_info);
  478.   if (!bfd_final_link (output_bfd, &link_info))
  479.     {
  480.       /* If there was an error recorded, print it out.  Otherwise assume
  481.      an appropriate error message like unknown symbol was printed
  482.      out.  */
  483.  
  484.       if (bfd_get_error () != bfd_error_no_error)
  485.     einfo ("%F%P: final link failed: %E\n", output_bfd);
  486.       else
  487.     xexit(1);
  488.     }
  489. }
  490.