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 / mri.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  8KB  |  376 lines

  1. /* Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  2.    
  3. This file is part of GLD, the Gnu Linker.
  4.  
  5. GLD 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 1, or (at your option)
  8. any later version.
  9.  
  10. GLD 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 GLD; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19.  
  20. /* This bit does the tree decoration when MRI style link scripts are parsed */
  21.  
  22. /*
  23.   contributed by Steve Chamberlain
  24.            sac@cygnus.com
  25.  
  26. */
  27.  
  28. #include "bfd.h"
  29. #include "sysdep.h" 
  30. #include "ld.h"
  31. #include "ldexp.h"
  32. #include "ldlang.h"
  33. #include "ldmisc.h"
  34. #include "mri.h"
  35. #include "ldgram.h"
  36.  
  37.  
  38. struct section_name_struct {
  39.   struct section_name_struct *next;
  40.   CONST char *name;
  41.   CONST char *alias;
  42.   etree_type *vma;
  43.   etree_type *align;
  44.   etree_type *subalign;
  45.   int ok_to_load;
  46. } ;
  47.  
  48. unsigned int symbol_truncate = 10000;
  49. struct section_name_struct *order;
  50. struct section_name_struct *only_load;
  51. struct section_name_struct *address;
  52. struct section_name_struct *alias;
  53.  
  54. struct section_name_struct *alignment;
  55. struct section_name_struct *subalignment;
  56.  
  57. extern char *strdup();
  58.  
  59. static struct section_name_struct **lookup
  60.   PARAMS ((const char *name, struct section_name_struct **list));
  61. static void mri_add_to_list PARAMS ((struct section_name_struct **list,
  62.                      const char *name, etree_type *vma,
  63.                      const char *zalias, etree_type *align,
  64.                      etree_type *subalign));
  65.  
  66. static struct section_name_struct **
  67. lookup (name, list)
  68.      CONST char *name;
  69.      struct section_name_struct **list;
  70. {
  71.  
  72.   struct section_name_struct **ptr = list;
  73.   while (*ptr) 
  74.   {
  75.     if (strcmp(name, (*ptr)->name) == 0) {
  76.       /* If this is a match, delete it, we only keep the last instance
  77.      of any name */
  78.       *ptr = (*ptr)->next;
  79.     }
  80.     else {
  81.       ptr = &((*ptr)->next);
  82.     }
  83.   }
  84.  
  85.   *ptr = (struct section_name_struct *)xmalloc(sizeof(struct section_name_struct));
  86.   return ptr;
  87. }
  88.  
  89. static void
  90. mri_add_to_list (list, name, vma, zalias, align, subalign)
  91.      struct section_name_struct **list;
  92.      CONST char *name;
  93.      etree_type *vma;
  94.      CONST char *zalias;
  95.      etree_type *align;
  96.      etree_type *subalign;
  97. {
  98.   struct section_name_struct **ptr = lookup(name,list);
  99.   (*ptr)->name = name;
  100.   (*ptr)->vma = vma;
  101.   (*ptr)->next = (struct section_name_struct *)NULL;
  102.   (*ptr)->ok_to_load = 0;
  103.   (*ptr)->alias = zalias;
  104.   (*ptr)->align = align;
  105.   (*ptr)->subalign = subalign;
  106. }
  107.  
  108.  
  109. void
  110. mri_output_section (name, vma)
  111.      CONST char *name;
  112.      etree_type *vma;
  113. {
  114.   mri_add_to_list(&address, name, vma, 0,0,0);
  115. }
  116.  
  117. /* if any ABSOLUTE <name> are in the script, only load those files
  118. marked thus */
  119.  
  120. void
  121. mri_only_load (name)
  122.      CONST char *name;
  123. {
  124.   mri_add_to_list(&only_load, name, 0, 0,0,0);
  125. }
  126.  
  127.  
  128. void
  129. mri_base (exp)
  130.      etree_type *exp;
  131. {
  132.   base = exp;
  133. }
  134.  
  135. static int done_tree = 0;
  136.  
  137. void
  138. mri_draw_tree ()
  139. {
  140.   if (done_tree) return;
  141.  
  142.   /* We don't bother with memory regions.  */
  143. #if 0
  144.   /* Create the regions */
  145.  {
  146.    lang_memory_region_type *r;
  147.    r = lang_memory_region_lookup("long");
  148.    r->current = r->origin = exp_get_vma(base, (bfd_vma)0, "origin",
  149.                     lang_first_phase_enum);
  150.    r->length = (bfd_size_type) exp_get_vma(0, (bfd_vma) ~((bfd_size_type)0),
  151.                        "length", lang_first_phase_enum);
  152.  }
  153. #endif
  154.   
  155.   /* Now build the statements for the ldlang machine */
  156.  
  157.  
  158.   /* Attatch the addresses of any which have addresses, and add the
  159.      ones not mentioned */
  160.   if (address != (struct section_name_struct *)NULL) {
  161.     struct section_name_struct *alist;
  162.     struct section_name_struct *olist;
  163.     if (order == (struct section_name_struct *)NULL) {
  164.       order = address;
  165.     }
  166.  
  167.     for (alist = address;
  168.      alist != (struct section_name_struct*)NULL;
  169.      alist = alist->next) 
  170.     {
  171.       int done = 0;
  172.       for (olist = order;
  173.        done == 0 &&
  174.        olist != (struct section_name_struct *)NULL;
  175.        olist = olist->next) 
  176.       {
  177.     if (strcmp(alist->name, olist->name) == 0) 
  178.     {
  179.       olist->vma = alist->vma;
  180.       done = 1;
  181.     }
  182.       }
  183.       if (!done) {
  184.     /* add this onto end of order list */
  185.     mri_add_to_list(&order, alist->name, alist->vma, 0,0,0);
  186.       }
  187.  
  188.     }
  189.  
  190.   }
  191.  
  192.   /* If we're only supposed to load a subset of them in, then prune
  193.      the list.  */
  194.  
  195.   if (only_load != (struct section_name_struct *)NULL) 
  196.   {
  197.     struct section_name_struct *ptr1;
  198.     struct section_name_struct *ptr2;
  199.     if (order == (struct section_name_struct*)NULL)
  200.      order = only_load;
  201.     
  202.     /* See if this name is in the list, if it is then we can load it
  203.      */
  204.     for (ptr1 = only_load; ptr1; ptr1 = ptr1->next) 
  205.     {
  206.       for (ptr2= order; ptr2; ptr2=ptr2->next) 
  207.       {
  208.     if (strcmp(ptr2->name, ptr1->name)==0) {
  209.       ptr2->ok_to_load = 1;
  210.     }
  211.       }
  212.     }
  213.   }
  214.   else 
  215.   {
  216.     /* No only load list, so everything is ok to load */
  217.     struct section_name_struct *ptr;
  218.     for (ptr = order; ptr; ptr=ptr->next) {
  219.       ptr->ok_to_load = 1;
  220.     }
  221.   }
  222.  
  223.  
  224.  
  225.   /* Create the order of sections to load */
  226.   if (order != (struct section_name_struct *)NULL) 
  227.   {
  228.     /* Been told to output the sections in a certain order */
  229.     struct section_name_struct *p = order;
  230.     while (p) 
  231.     {
  232.       struct section_name_struct *aptr;
  233.       etree_type *align = 0;
  234.       etree_type *subalign = 0;
  235.       /* See if an alignment has been specified */
  236.  
  237.       for (aptr = alignment; aptr; aptr= aptr->next)
  238.       {
  239.     if (strcmp(aptr->name, p->name)==0) {
  240.       align =  aptr->align;
  241.     }
  242.       }
  243.  
  244.       for (aptr = subalignment; aptr; aptr= aptr->next)
  245.       {
  246.     if (strcmp(aptr->name, p->name)==0) {
  247.       subalign =  aptr->subalign;
  248.     }
  249.       }
  250.  
  251.       if (base == 0) {
  252.     base = p->vma ? p->vma :exp_nameop(NAME, ".");
  253.       }
  254.       lang_enter_output_section_statement (p->name, base,
  255.                        p->ok_to_load ? 0 : SEC_NEVER_LOAD,
  256.                        1, align, subalign,
  257.                        (etree_type *) NULL);
  258.       base = 0;
  259.       lang_add_wild(p->name, (char *)NULL);
  260.       /* If there is an alias for this section, add it too */
  261.       for (aptr = alias; aptr; aptr = aptr->next) {
  262.  
  263.     if (strcmp(aptr->alias, p->name)== 0) {
  264.       lang_add_wild(aptr->name, (char *)NULL);
  265.     }
  266.       }
  267.     
  268.       lang_leave_output_section_statement(0, "*default*");
  269.       p = p->next;
  270.     }
  271.   }
  272.  
  273.  
  274.   done_tree = 1;
  275.  
  276. }
  277. void
  278. mri_load (name)
  279.      CONST char *name;
  280. {
  281.   base = 0;
  282.   lang_add_input_file(name,
  283.               lang_input_file_is_file_enum, (char *)NULL);
  284.   /*  lang_leave_output_section_statement(0,"*default*");*/
  285. }
  286.  
  287.  
  288. void
  289. mri_order (name)
  290.      CONST char *name;
  291. {
  292.   mri_add_to_list(&order, name, 0, 0,0,0);
  293. }
  294.  
  295. void 
  296. mri_alias (want, is, isn)
  297.      CONST char *want;
  298.      CONST char *is;
  299.      int isn;
  300. {
  301.   if (!is) {
  302.     /* Some sections are digits - */
  303.     char buf[20];
  304.     sprintf(buf, "%d", isn);
  305.     is =strdup(buf);
  306.     if (is == NULL)
  307.       abort ();
  308.   }
  309.   mri_add_to_list(&alias, is, 0, want,0,0);
  310.  
  311. }
  312.  
  313.  
  314. void 
  315. mri_name (name)
  316.      CONST char *name;
  317. {
  318.   lang_add_output(name, 1);
  319.  
  320. }
  321.  
  322.  
  323. void
  324. mri_format (name)
  325.      CONST char *name;
  326. {
  327.   if (strcmp(name, "S") == 0)
  328.   {
  329.     lang_add_output_format("srec", (char *) NULL, (char *) NULL, 1);
  330.   }
  331.   else if (strcmp(name, "IEEE") == 0)
  332.   {
  333.     lang_add_output_format("ieee", (char *) NULL, (char *) NULL, 1);
  334.   }
  335.   else if (strcmp(name, "COFF") == 0)
  336.   {
  337.     lang_add_output_format("coff-m68k", (char *) NULL, (char *) NULL, 1);
  338.   }
  339.   else {
  340.     einfo("%P%F: unknown format type %s\n", name);
  341.   }
  342. }
  343.  
  344.  
  345. void
  346. mri_public (name, exp)
  347.      CONST char *name;
  348.      etree_type *exp;
  349. {
  350.   lang_add_assignment(exp_assop('=', name, exp));
  351. }
  352.  
  353. void 
  354. mri_align (name, exp)
  355.      CONST char *name;
  356.      etree_type *exp;
  357. {
  358.   mri_add_to_list(&alignment, name,0,0,exp,0);
  359. }
  360.  
  361. void 
  362. mri_alignmod (name, exp)
  363.      CONST char *name;
  364.      etree_type *exp;
  365. {
  366.   mri_add_to_list(&subalignment, name,0,0,0,exp);
  367. }
  368.  
  369.  
  370. void 
  371. mri_truncate (exp)
  372.      unsigned int exp;
  373. {
  374.   symbol_truncate = exp;
  375. }
  376.