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

  1. /* Linker command language support.
  2.    Copyright 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GLD, the Gnu Linker.
  5.  
  6. GLD 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 1, or (at your option)
  9. any later version.
  10.  
  11. GLD 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 GLD; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "bfd.h"
  21. #include "sysdep.h"
  22.  
  23. #include "ld.h"
  24. #include "ldmain.h"
  25. #include "ldsym.h"
  26. #include "ldgram.h"
  27. #include "ldwarn.h"
  28. #include "ldlang.h"
  29. #include "ldexp.h"
  30. #include "ldemul.h"
  31. #include "ldlex.h"
  32. #include "ldmisc.h"
  33. #include "ldindr.h"
  34. #include "ldctor.h"
  35. /* FORWARDS */
  36. static void print_statements PARAMS ((void));
  37. static void print_statement PARAMS ((lang_statement_union_type *,
  38.                       lang_output_section_statement_type *));
  39.  
  40. /* LOCALS */
  41. static struct obstack stat_obstack;
  42.  
  43. #define obstack_chunk_alloc ldmalloc
  44. #define obstack_chunk_free free
  45. static CONST char *startup_file;
  46. static lang_statement_list_type input_file_chain;
  47.  
  48. /* Points to the last statement in the .data section, so we can add
  49.    stuff to the data section without pain */
  50. static lang_statement_list_type end_of_data_section_statement_list;
  51.  
  52. /* List of statements needed to handle constructors */
  53. extern lang_statement_list_type constructor_list;
  54.  
  55. static boolean placed_commons = false;
  56. static lang_output_section_statement_type *default_common_section;
  57. static boolean map_option_f;
  58. static bfd_vma print_dot;
  59. static lang_input_statement_type *first_file;
  60. static lang_statement_list_type lang_output_section_statement;
  61. static CONST char *current_target;
  62. static CONST char *output_target;
  63. static size_t longest_section_name = 8;
  64. static section_userdata_type common_section_userdata;
  65. static lang_statement_list_type statement_list;
  66.  
  67. /* EXPORTS */
  68. boolean relaxing;
  69. lang_output_section_statement_type *abs_output_section;
  70. lang_statement_list_type *stat_ptr = &statement_list;
  71. lang_input_statement_type *script_file = 0;
  72. boolean option_longmap = false;
  73. lang_statement_list_type file_chain =
  74. {0};
  75. CONST char *entry_symbol = 0;
  76. bfd_size_type largest_section = 0;
  77. boolean lang_has_input_file = false;
  78. lang_output_section_statement_type *create_object_symbols = 0;
  79. boolean had_output_filename = false;
  80. boolean lang_float_flag = false;
  81.  
  82. /* IMPORTS */
  83. extern char *default_target;
  84.  
  85. extern unsigned int undefined_global_sym_count;
  86. extern char *current_file;
  87. extern bfd *output_bfd;
  88. extern enum bfd_architecture ldfile_output_architecture;
  89. extern unsigned long ldfile_output_machine;
  90. extern char *ldfile_output_machine_name;
  91. extern ldsym_type *symbol_head;
  92. extern unsigned int commons_pending;
  93. extern args_type command_line;
  94. extern ld_config_type config;
  95. extern boolean had_script;
  96. extern boolean write_map;
  97. extern int g_switch_value;
  98.  
  99.  
  100. etree_type *base; /* Relocation base - or null */
  101.  
  102.  
  103. #ifdef __STDC__
  104. #define cat(a,b) a##b
  105. #else
  106. #define cat(a,b) a/**/b
  107. #endif
  108.  
  109. #define new_stat(x,y) (cat(x,_type)*) new_statement(cat(x,_enum), sizeof(cat(x,_type)),y)
  110.  
  111. #define outside_section_address(q) ( (q)->output_offset + (q)->output_section->vma)
  112.  
  113. #define outside_symbol_address(q) ((q)->value +   outside_section_address(q->section))
  114.  
  115. void lang_add_data PARAMS ((int type, union etree_union * exp));
  116.  
  117. PTR
  118. DEFUN (stat_alloc, (size),
  119.        size_t size)
  120. {
  121.   return obstack_alloc (&stat_obstack, size);
  122. }
  123. static void
  124. DEFUN (print_size, (value),
  125.        size_t value)
  126. {
  127.   fprintf (config.map_file, "%5x", (unsigned) value);
  128. }
  129. static void
  130. DEFUN (print_alignment, (value),
  131.        unsigned int value)
  132. {
  133.   fprintf (config.map_file, "2**%1u", value);
  134. }
  135. static void
  136. DEFUN (print_fill, (value),
  137.        fill_type value)
  138. {
  139.   fprintf (config.map_file, "%04x", (unsigned) value);
  140. }
  141.  
  142.  
  143. static void
  144. DEFUN (print_section, (name),
  145.        CONST char *CONST name)
  146. {
  147.   fprintf (config.map_file, "%*s", -longest_section_name, name);
  148. }
  149.  
  150. /*----------------------------------------------------------------------
  151.   lang_for_each_statement walks the parse tree and calls the provided
  152.   function for each node
  153. */
  154.  
  155. static void
  156. DEFUN (lang_for_each_statement_worker, (func, s),
  157.        void (*func) ()AND
  158.        lang_statement_union_type * s)
  159. {
  160.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  161.     {
  162.       func (s);
  163.  
  164.       switch (s->header.type)
  165.     {
  166.     case lang_constructors_statement_enum:
  167.       lang_for_each_statement_worker (func, constructor_list.head);
  168.       break;
  169.     case lang_output_section_statement_enum:
  170.       lang_for_each_statement_worker
  171.         (func,
  172.          s->output_section_statement.children.head);
  173.       break;
  174.     case lang_wild_statement_enum:
  175.       lang_for_each_statement_worker
  176.         (func,
  177.          s->wild_statement.children.head);
  178.       break;
  179.     case lang_data_statement_enum:
  180.     case lang_object_symbols_statement_enum:
  181.     case lang_output_statement_enum:
  182.     case lang_target_statement_enum:
  183.     case lang_input_section_enum:
  184.     case lang_input_statement_enum:
  185.     case lang_assignment_statement_enum:
  186.     case lang_padding_statement_enum:
  187.     case lang_address_statement_enum:
  188.       break;
  189.     default:
  190.       FAIL ();
  191.       break;
  192.     }
  193.     }
  194. }
  195.  
  196. void
  197. DEFUN (lang_for_each_statement, (func),
  198.        void (*func) ())
  199. {
  200.   lang_for_each_statement_worker (func,
  201.                   statement_list.head);
  202. }
  203.  
  204. /*----------------------------------------------------------------------*/
  205. void
  206. DEFUN (lang_list_init, (list),
  207.        lang_statement_list_type * list)
  208. {
  209.   list->head = (lang_statement_union_type *) NULL;
  210.   list->tail = &list->head;
  211. }
  212.  
  213. /*----------------------------------------------------------------------
  214.  
  215.   build a new statement node for the parse tree
  216.  
  217.  */
  218.  
  219. static
  220. lang_statement_union_type *
  221. DEFUN (new_statement, (type, size, list),
  222.        enum statement_enum type AND
  223.        bfd_size_type size AND
  224.        lang_statement_list_type * list)
  225. {
  226.   lang_statement_union_type *new = (lang_statement_union_type *)
  227.   stat_alloc (size);
  228.  
  229.   new->header.type = type;
  230.   new->header.next = (lang_statement_union_type *) NULL;
  231.   lang_statement_append (list, new, &new->header.next);
  232.   return new;
  233. }
  234.  
  235. /*
  236.   Build a new input file node for the language. There are several ways
  237.   in which we treat an input file, eg, we only look at symbols, or
  238.   prefix it with a -l etc.
  239.  
  240.   We can be supplied with requests for input files more than once;
  241.   they may, for example be split over serveral lines like foo.o(.text)
  242.   foo.o(.data) etc, so when asked for a file we check that we havn't
  243.   got it already so we don't duplicate the bfd.
  244.  
  245.  */
  246. static lang_input_statement_type *
  247. DEFUN (new_afile, (name, file_type, target),
  248.        CONST char *CONST name AND
  249.        CONST lang_input_file_enum_type file_type AND
  250.        CONST char *CONST target)
  251. {
  252.  
  253.   lang_input_statement_type *p = new_stat (lang_input_statement,
  254.                        stat_ptr);
  255.  
  256.   lang_has_input_file = true;
  257.   p->target = target;
  258.   p->complained = false;
  259.   switch (file_type)
  260.     {
  261.     case lang_input_file_is_symbols_only_enum:
  262.       p->filename = name;
  263.       p->is_archive = false;
  264.       p->real = true;
  265.       p->local_sym_name = name;
  266.       p->just_syms_flag = true;
  267.       p->search_dirs_flag = false;
  268.       break;
  269.     case lang_input_file_is_fake_enum:
  270.       p->filename = name;
  271.       p->is_archive = false;
  272.       p->real = false;
  273.       p->local_sym_name = name;
  274.       p->just_syms_flag = false;
  275.       p->search_dirs_flag = false;
  276.       break;
  277.     case lang_input_file_is_l_enum:
  278.       p->is_archive = true;
  279.       p->filename = name;
  280.       p->real = true;
  281.       p->local_sym_name = concat ("-l", name, "");
  282.       p->just_syms_flag = false;
  283.       p->search_dirs_flag = true;
  284.       break;
  285.     case lang_input_file_is_search_file_enum:
  286.     case lang_input_file_is_marker_enum:
  287.       p->filename = name;
  288.       p->is_archive = false;
  289.       p->real = true;
  290.       p->local_sym_name = name;
  291.       p->just_syms_flag = false;
  292.       p->search_dirs_flag = true;
  293.       break;
  294.     case lang_input_file_is_file_enum:
  295.       p->filename = name;
  296.       p->is_archive = false;
  297.       p->real = true;
  298.       p->local_sym_name = name;
  299.       p->just_syms_flag = false;
  300.       p->search_dirs_flag = false;
  301.       break;
  302.     default:
  303.       FAIL ();
  304.     }
  305.   p->asymbols = (asymbol **) NULL;
  306.   p->superfile = (lang_input_statement_type *) NULL;
  307.   p->next_real_file = (lang_statement_union_type *) NULL;
  308.   p->next = (lang_statement_union_type *) NULL;
  309.   p->symbol_count = 0;
  310.   p->common_output_section = (asection *) NULL;
  311.   lang_statement_append (&input_file_chain,
  312.              (lang_statement_union_type *) p,
  313.              &p->next_real_file);
  314.   return p;
  315. }
  316.  
  317. lang_input_statement_type *
  318. DEFUN (lang_add_input_file, (name, file_type, target),
  319.        CONST char *name AND
  320.        lang_input_file_enum_type file_type AND
  321.        CONST char *target)
  322. {
  323.   /* Look it up or build a new one */
  324.   lang_has_input_file = true;
  325. #if 0
  326.   lang_input_statement_type *p;
  327.  
  328.   for (p = (lang_input_statement_type *) input_file_chain.head;
  329.        p != (lang_input_statement_type *) NULL;
  330.        p = (lang_input_statement_type *) (p->next_real_file))
  331.     {
  332.       /* Sometimes we have incomplete entries in here */
  333.       if (p->filename != (char *) NULL)
  334.     {
  335.       if (strcmp (name, p->filename) == 0)
  336.         return p;
  337.     }
  338.  
  339.     }
  340. #endif
  341.   return new_afile (name, file_type, target);
  342. }
  343.  
  344. void
  345. DEFUN (lang_add_keepsyms_file, (filename),
  346.        CONST char *filename)
  347. {
  348.   extern strip_symbols_type strip_symbols;
  349.   if (keepsyms_file != 0)
  350.     info ("%X%P error: duplicated keep-symbols-file value\n");
  351.   keepsyms_file = filename;
  352.   if (strip_symbols != STRIP_NONE)
  353.     info ("%P `-keep-only-symbols-file' overrides `-s' and `-S'\n");
  354.   strip_symbols = STRIP_SOME;
  355. }
  356.  
  357. /* Build enough state so that the parser can build its tree */
  358. void
  359. DEFUN_VOID (lang_init)
  360. {
  361.   obstack_begin (&stat_obstack, 1000);
  362.  
  363.   stat_ptr = &statement_list;
  364.  
  365.   lang_list_init (stat_ptr);
  366.  
  367.   lang_list_init (&input_file_chain);
  368.   lang_list_init (&lang_output_section_statement);
  369.   lang_list_init (&file_chain);
  370.   first_file = lang_add_input_file ((char *) NULL,
  371.                     lang_input_file_is_marker_enum,
  372.                     (char *) NULL);
  373.   abs_output_section = lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
  374.  
  375.   abs_output_section->bfd_section = &bfd_abs_section;
  376.  
  377. }
  378.  
  379. /*----------------------------------------------------------------------
  380.  A region is an area of memory declared with the
  381.  MEMORY {  name:org=exp, len=exp ... }
  382.  syntax.
  383.  
  384.  We maintain a list of all the regions here
  385.  
  386.  If no regions are specified in the script, then the default is used
  387.  which is created when looked up to be the entire data space
  388. */
  389.  
  390. static lang_memory_region_type *lang_memory_region_list;
  391. static lang_memory_region_type **lang_memory_region_list_tail = &lang_memory_region_list;
  392.  
  393. lang_memory_region_type *
  394. DEFUN (lang_memory_region_lookup, (name),
  395.        CONST char *CONST name)
  396. {
  397.  
  398.   lang_memory_region_type *p = lang_memory_region_list;
  399.  
  400.   for (p = lang_memory_region_list;
  401.        p != (lang_memory_region_type *) NULL;
  402.        p = p->next)
  403.     {
  404.       if (strcmp (p->name, name) == 0)
  405.     {
  406.       return p;
  407.     }
  408.     }
  409.   if (strcmp (name, "*default*") == 0)
  410.     {
  411.       /* This is the default region, dig out first one on the list */
  412.       if (lang_memory_region_list != (lang_memory_region_type *) NULL)
  413.     {
  414.       return lang_memory_region_list;
  415.     }
  416.     }
  417.   {
  418.     lang_memory_region_type *new =
  419.     (lang_memory_region_type *) stat_alloc ((bfd_size_type) (sizeof (lang_memory_region_type)));
  420.  
  421.     new->name = buystring (name);
  422.     new->next = (lang_memory_region_type *) NULL;
  423.  
  424.     *lang_memory_region_list_tail = new;
  425.     lang_memory_region_list_tail = &new->next;
  426.     new->origin = 0;
  427.     new->length = ~(bfd_size_type)0;
  428.     new->current = 0;
  429.     new->had_full_message = false;
  430.  
  431.     return new;
  432.   }
  433. }
  434.  
  435.  
  436. lang_output_section_statement_type *
  437. DEFUN (lang_output_section_find, (name),
  438.        CONST char *CONST name)
  439. {
  440.   lang_statement_union_type *u;
  441.   lang_output_section_statement_type *lookup;
  442.  
  443.   for (u = lang_output_section_statement.head;
  444.        u != (lang_statement_union_type *) NULL;
  445.        u = lookup->next)
  446.     {
  447.       lookup = &u->output_section_statement;
  448.       if (strcmp (name, lookup->name) == 0)
  449.     {
  450.       return lookup;
  451.     }
  452.     }
  453.   return (lang_output_section_statement_type *) NULL;
  454. }
  455.  
  456. lang_output_section_statement_type *
  457. DEFUN (lang_output_section_statement_lookup, (name),
  458.        CONST char *CONST name)
  459. {
  460.   lang_output_section_statement_type *lookup;
  461.  
  462.   lookup = lang_output_section_find (name);
  463.   if (lookup == (lang_output_section_statement_type *) NULL)
  464.     {
  465.  
  466.       lookup = (lang_output_section_statement_type *)
  467.     new_stat (lang_output_section_statement, stat_ptr);
  468.       lookup->region = (lang_memory_region_type *) NULL;
  469.       lookup->fill = 0;
  470.       lookup->block_value = 1;
  471.       lookup->name = name;
  472.  
  473.       lookup->next = (lang_statement_union_type *) NULL;
  474.       lookup->bfd_section = (asection *) NULL;
  475.       lookup->processed = false;
  476.       lookup->loadable = 1;
  477.       lookup->addr_tree = (etree_type *) NULL;
  478.       lang_list_init (&lookup->children);
  479.  
  480.       lookup->memspec = (CONST char *) NULL;
  481.       lookup->flags = 0;
  482.       lookup->subsection_alignment = -1;
  483.       lookup->section_alignment = -1;
  484.       lookup->load_base = (union etree_union *) NULL;
  485.  
  486.       lang_statement_append (&lang_output_section_statement,
  487.                  (lang_statement_union_type *) lookup,
  488.                  &lookup->next);
  489.     }
  490.   return lookup;
  491. }
  492.  
  493. /*ARGSUSED*/
  494. static void
  495. DEFUN (print_flags, (ignore_flags),
  496.        int *ignore_flags)
  497. {
  498.   fprintf (config.map_file, "(");
  499. #if 0
  500.   if (flags->flag_read)
  501.     fprintf (outfile, "R");
  502.   if (flags->flag_write)
  503.     fprintf (outfile, "W");
  504.   if (flags->flag_executable)
  505.     fprintf (outfile, "X");
  506.   if (flags->flag_loadable)
  507.     fprintf (outfile, "L");
  508. #endif
  509.  fprintf (config.map_file, ")");
  510. }
  511.  
  512. void
  513. DEFUN_VOID (lang_map)
  514. {
  515.   lang_memory_region_type *m;
  516.  
  517.   fprintf (config.map_file, "**MEMORY CONFIGURATION**\n\n");
  518. #ifdef HOST_64_BIT
  519.   fprintf (config.map_file, "name\t\torigin\t\tlength\t\tattributes\n");
  520. #else
  521.   fprintf (config.map_file,
  522.        "name\t\torigin   length   r_size   c_size    is    attributes\n");
  523.  
  524. #endif
  525.   for (m = lang_memory_region_list;
  526.        m != (lang_memory_region_type *) NULL;
  527.        m = m->next)
  528.     {
  529.       fprintf (config.map_file, "%-16s", m->name);
  530.       print_address (m->origin);
  531.       print_space ();
  532.       print_address ((bfd_vma)m->length);
  533.       print_space ();
  534.       print_address ((bfd_vma)m->old_length);
  535.       print_space();
  536.       print_address (m->current - m->origin);
  537.       print_space();
  538.       if (m->old_length)
  539.        fprintf(config.map_file," %2d%%  ", ( m->current - m->origin) * 100 / m->old_length);
  540.       print_flags (&m->flags);
  541.       fprintf (config.map_file, "\n");
  542.     }
  543.   fprintf (config.map_file, "\n\n**LINK EDITOR MEMORY MAP**\n\n");
  544.   fprintf (config.map_file, "output   input     virtual\n");
  545.   fprintf (config.map_file, "section  section   address    tsize\n\n");
  546.  
  547.   print_statements ();
  548.  
  549. }
  550.  
  551. /*
  552.  *
  553.  */
  554. static void
  555. DEFUN (init_os, (s),
  556.        lang_output_section_statement_type * s)
  557. {
  558. /*  asection *section = bfd_get_section_by_name(output_bfd, s->name);*/
  559.   section_userdata_type *new =
  560.   (section_userdata_type *)
  561.   stat_alloc ((bfd_size_type) (sizeof (section_userdata_type)));
  562.  
  563.   s->bfd_section = bfd_get_section_by_name (output_bfd, s->name);
  564.   if (s->bfd_section == (asection *) NULL)
  565.     s->bfd_section = bfd_make_section (output_bfd, s->name);
  566.   if (s->bfd_section == (asection *) NULL)
  567.     {
  568.       einfo ("%P%F output format %s cannot represent section called %s\n",
  569.          output_bfd->xvec->name, s->name);
  570.     }
  571.   s->bfd_section->output_section = s->bfd_section;
  572. /*  s->bfd_section->flags = s->flags;*/
  573.  
  574.   /* We initialize an output sections output offset to minus its own */
  575.   /* vma to allow us to output a section through itself */
  576.   s->bfd_section->output_offset = 0;
  577.   get_userdata (s->bfd_section) = (PTR) new;
  578.  
  579. }
  580.  
  581. /***********************************************************************
  582.   The wild routines.
  583.  
  584.   These expand statements like *(.text) and foo.o to a list of
  585.   explicit actions, like foo.o(.text), bar.o(.text) and
  586.   foo.o(.text,.data) .
  587.  
  588.   The toplevel routine, wild, takes a statement, section, file and
  589.   target. If either the section or file is null it is taken to be the
  590.   wildcard. Seperate lang_input_section statements are created for
  591.   each part of the expanstion, and placed after the statement provided.
  592.  
  593. */
  594.  
  595. static void
  596. DEFUN (wild_doit, (ptr, section, output, file),
  597.        lang_statement_list_type * ptr AND
  598.        asection * section AND
  599.        lang_output_section_statement_type * output AND
  600.        lang_input_statement_type * file)
  601. {
  602.   if (output->bfd_section == (asection *) NULL)
  603.   {
  604.     init_os (output);
  605.     /* Initialize the vma and size to the existing section.  This will
  606.        be overriden in lang_size_sections unless SEC_NEVER_LOAD gets
  607.        set.  */
  608.     if (section != (asection *) NULL)
  609.     {
  610.       bfd_set_section_vma (0, output->bfd_section,
  611.                bfd_section_vma (0, section));
  612.       output->bfd_section->_raw_size = section->_raw_size;
  613.     }
  614.   }
  615.  
  616.   if (section != (asection *) NULL
  617.       && section->output_section == (asection *) NULL)
  618.   {
  619.     /* Add a section reference to the list */
  620.     lang_input_section_type *new = new_stat (lang_input_section, ptr);
  621.  
  622.     new->section = section;
  623.     new->ifile = file;
  624.     section->output_section = output->bfd_section;
  625.  
  626.     /* Be selective about what the output section inherits from the
  627.        input section */
  628.  
  629.     if ((section->flags & SEC_SHARED_LIBRARY) != 0)
  630.       section->output_section->flags |= section->flags;
  631.     else
  632.       section->output_section->flags |= section->flags & ~SEC_NEVER_LOAD;
  633.  
  634.     if (!output->loadable) 
  635.     {
  636.       /* Turn off load flag */
  637.       output->bfd_section->flags &= ~SEC_LOAD;
  638.       output->bfd_section->flags |= SEC_NEVER_LOAD;
  639.     }
  640.     if (section->alignment_power > output->bfd_section->alignment_power)
  641.     {
  642.       output->bfd_section->alignment_power = section->alignment_power;
  643.     }
  644.     /* If supplied an aligmnet, then force it */
  645.     if (output->section_alignment != -1)
  646.     {
  647.       output->bfd_section->alignment_power = output->section_alignment;
  648.     }
  649.   }
  650. }
  651.  
  652. static asection *
  653. DEFUN (our_bfd_get_section_by_name, (abfd, section),
  654.        bfd * abfd AND
  655.        CONST char *section)
  656. {
  657.   return bfd_get_section_by_name (abfd, section);
  658. }
  659.  
  660. static void
  661. DEFUN (wild_section, (ptr, section, file, output),
  662.        lang_wild_statement_type * ptr AND
  663.        CONST char *section AND
  664.        lang_input_statement_type * file AND
  665.        lang_output_section_statement_type * output)
  666. {
  667.   asection *s;
  668.  
  669.   if (file->just_syms_flag == false)
  670.     {
  671.       if (section == (char *) NULL)
  672.     {
  673.       /* Do the creation to all sections in the file */
  674.       for (s = file->the_bfd->sections; s != (asection *) NULL; s = s->next)
  675.       {
  676.         /* except for bss */
  677.         if ((s->flags & SEC_IS_COMMON)  == 0)
  678.         {
  679.           wild_doit (&ptr->children, s, output, file);
  680.         }
  681.       }
  682.     }
  683.       else
  684.     {
  685.       /* Do the creation to the named section only */
  686.       wild_doit (&ptr->children,
  687.              our_bfd_get_section_by_name (file->the_bfd, section),
  688.              output, file);
  689.     }
  690.     }
  691. }
  692.  
  693. /* passed a file name (which must have been seen already and added to
  694.    the statement tree. We will see if it has been opened already and
  695.    had its symbols read. If not then we'll read it.
  696.  
  697.    Archives are pecuilar here. We may open them once, but if they do
  698.    not define anything we need at the time, they won't have all their
  699.    symbols read. If we need them later, we'll have to redo it.
  700.    */
  701. static
  702. lang_input_statement_type *
  703. DEFUN (lookup_name, (name),
  704.        CONST char *CONST name)
  705. {
  706.   lang_input_statement_type *search;
  707.  
  708.   for (search = (lang_input_statement_type *) input_file_chain.head;
  709.        search != (lang_input_statement_type *) NULL;
  710.        search = (lang_input_statement_type *) search->next_real_file)
  711.     {
  712.       if (search->filename == (char *) NULL && name == (char *) NULL)
  713.     {
  714.       return search;
  715.     }
  716.       if (search->filename != (char *) NULL && name != (char *) NULL)
  717.     {
  718.       if (strcmp (search->filename, name) == 0)
  719.         {
  720.           ldmain_open_file_read_symbol (search);
  721.           return search;
  722.         }
  723.     }
  724.     }
  725.  
  726.   /* There isn't an afile entry for this file yet, this must be
  727.      because the name has only appeared inside a load script and not
  728.      on the command line  */
  729.   search = new_afile (name, lang_input_file_is_file_enum, default_target);
  730.   ldmain_open_file_read_symbol (search);
  731.   return search;
  732.  
  733.  
  734. }
  735.  
  736. static void
  737. DEFUN (wild, (s, section, file, target, output),
  738.        lang_wild_statement_type * s AND
  739.        CONST char *CONST section AND
  740.        CONST char *CONST file AND
  741.        CONST char *CONST target AND
  742.        lang_output_section_statement_type * output)
  743. {
  744.   lang_input_statement_type *f;
  745.  
  746.   if (file == (char *) NULL)
  747.     {
  748.       /* Perform the iteration over all files in the list */
  749.       for (f = (lang_input_statement_type *) file_chain.head;
  750.        f != (lang_input_statement_type *) NULL;
  751.        f = (lang_input_statement_type *) f->next)
  752.     {
  753.       wild_section (s, section, f, output);
  754.     }
  755.       /* Once more for the script file */
  756.       wild_section(s, section, script_file, output);
  757.     }
  758.   else
  759.     {
  760.       /* Perform the iteration over a single file */
  761.       wild_section (s, section, lookup_name (file), output);
  762.     }
  763.   if (section != (char *) NULL
  764.       && strcmp (section, "COMMON") == 0
  765.    && default_common_section == (lang_output_section_statement_type *) NULL)
  766.     {
  767.       /* Remember the section that common is going to incase we later
  768.          get something which doesn't know where to put it */
  769.       default_common_section = output;
  770.     }
  771. }
  772.  
  773. /*
  774.   read in all the files
  775.   */
  776. static bfd *
  777. DEFUN (open_output, (name),
  778.        CONST char *CONST name)
  779. {
  780.   extern unsigned long ldfile_output_machine;
  781.   extern enum bfd_architecture ldfile_output_architecture;
  782.  
  783.   extern CONST char *output_filename;
  784.   bfd *output;
  785.  
  786.   if (output_target == (char *) NULL)
  787.     {
  788.       if (current_target != (char *) NULL)
  789.     output_target = current_target;
  790.       else
  791.     output_target = default_target;
  792.     }
  793.   output = bfd_openw (name, output_target);
  794.   output_filename = name;
  795.  
  796.   if (output == (bfd *) NULL)
  797.     {
  798.       if (bfd_error == invalid_target)
  799.     {
  800.       einfo ("%P%F target %s not found\n", output_target);
  801.     }
  802.       einfo ("%P%F problem opening output file %s, %E\n", name);
  803.     }
  804.  
  805.   /*  output->flags |= D_PAGED;*/
  806.  
  807.   bfd_set_format (output, bfd_object);
  808.   bfd_set_arch_mach (output,
  809.              ldfile_output_architecture,
  810.              ldfile_output_machine);
  811.   bfd_set_gp_size (output, g_switch_value);
  812.   return output;
  813. }
  814.  
  815.  
  816.  
  817.  
  818. static void
  819. DEFUN (ldlang_open_output, (statement),
  820.        lang_statement_union_type * statement)
  821. {
  822.   switch (statement->header.type)
  823.     {
  824.       case lang_output_statement_enum:
  825.       output_bfd = open_output (statement->output_statement.name);
  826.       ldemul_set_output_arch ();
  827.       if (config.magic_demand_paged && !config.relocateable_output)
  828.     output_bfd->flags |= D_PAGED;
  829.       else
  830.     output_bfd->flags &= ~D_PAGED;
  831.       if (config.text_read_only)
  832.     output_bfd->flags |= WP_TEXT;
  833.       else
  834.     output_bfd->flags &= ~WP_TEXT;
  835.       break;
  836.  
  837.     case lang_target_statement_enum:
  838.       current_target = statement->target_statement.target;
  839.       break;
  840.     default:
  841.       break;
  842.     }
  843. }
  844.  
  845. static void
  846. DEFUN (open_input_bfds, (statement),
  847.        lang_statement_union_type * statement)
  848. {
  849.   switch (statement->header.type)
  850.     {
  851.       case lang_target_statement_enum:
  852.       current_target = statement->target_statement.target;
  853.       break;
  854.     case lang_wild_statement_enum:
  855.       /* Maybe we should load the file's symbols */
  856.       if (statement->wild_statement.filename)
  857.     {
  858.       (void) lookup_name (statement->wild_statement.filename);
  859.     }
  860.       break;
  861.     case lang_input_statement_enum:
  862.       if (statement->input_statement.real == true)
  863.     {
  864.       statement->input_statement.target = current_target;
  865.       lookup_name (statement->input_statement.filename);
  866.     }
  867.       break;
  868.     default:
  869.       break;
  870.     }
  871. }
  872.  
  873. /* If there are [COMMONS] statements, put a wild one into the bss section */
  874.  
  875. static void
  876. lang_reasonable_defaults ()
  877. {
  878.  
  879.  
  880.  
  881. #if 0
  882.   lang_output_section_statement_lookup (".text");
  883.   lang_output_section_statement_lookup (".data");
  884.  
  885.   default_common_section =
  886.     lang_output_section_statement_lookup (".bss");
  887.  
  888.  
  889.   if (placed_commons == false)
  890.     {
  891.       lang_wild_statement_type *new =
  892.       new_stat (lang_wild_statement,
  893.         &default_common_section->children);
  894.  
  895.       new->section_name = "COMMON";
  896.       new->filename = (char *) NULL;
  897.       lang_list_init (&new->children);
  898.     }
  899. #endif
  900.  
  901. }
  902.  
  903. /*
  904.  Add the supplied name to the symbol table as an undefined reference.
  905.  Remove items from the chain as we open input bfds
  906.  */
  907. typedef struct ldlang_undef_chain_list
  908. {
  909.   struct ldlang_undef_chain_list *next;
  910.   char *name;
  911. }                       ldlang_undef_chain_list_type;
  912.  
  913. static ldlang_undef_chain_list_type *ldlang_undef_chain_list_head;
  914.  
  915. void
  916. DEFUN (ldlang_add_undef, (name),
  917.        CONST char *CONST name)
  918. {
  919.   ldlang_undef_chain_list_type *new =
  920.   (ldlang_undef_chain_list_type
  921.    *) stat_alloc ((bfd_size_type) (sizeof (ldlang_undef_chain_list_type)));
  922.  
  923.   new->next = ldlang_undef_chain_list_head;
  924.   ldlang_undef_chain_list_head = new;
  925.  
  926.   new->name = buystring (name);
  927. }
  928.  
  929. /* Run through the list of undefineds created above and place them
  930.    into the linker hash table as undefined symbols belonging to the
  931.    script file.
  932. */
  933. static void
  934. DEFUN_VOID (lang_place_undefineds)
  935. {
  936.   ldlang_undef_chain_list_type *ptr = ldlang_undef_chain_list_head;
  937.  
  938.   while (ptr != (ldlang_undef_chain_list_type *) NULL)
  939.     {
  940.       asymbol *def;
  941.       asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **)));
  942.  
  943.       def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd);
  944.       *def_ptr = def;
  945.       def->name = ptr->name;
  946.       def->section = &bfd_und_section;
  947.       Q_enter_global_ref (def_ptr, ptr->name);
  948.       ptr = ptr->next;
  949.     }
  950. }
  951.  
  952. /* Copy important data from out internal form to the bfd way. Also
  953.    create a section for the dummy file
  954.  */
  955.  
  956. static void
  957. DEFUN_VOID (lang_create_output_section_statements)
  958. {
  959.   lang_statement_union_type *os;
  960.  
  961.   for (os = lang_output_section_statement.head;
  962.        os != (lang_statement_union_type *) NULL;
  963.        os = os->output_section_statement.next)
  964.     {
  965.       lang_output_section_statement_type *s =
  966.       &os->output_section_statement;
  967.  
  968.       init_os (s);
  969.     }
  970.  
  971. }
  972.  
  973. static void
  974. DEFUN_VOID (lang_init_script_file)
  975. {
  976.   script_file = lang_add_input_file ("command line",
  977.                      lang_input_file_is_fake_enum,
  978.                      (char *) NULL);
  979.   script_file->the_bfd = bfd_create ("command line", output_bfd);
  980.   script_file->symbol_count = 0;
  981.   script_file->the_bfd->sections = 0;
  982.  
  983.   /* The user data of a bfd points to the input statement attatched */
  984.   script_file->the_bfd->usrdata  = (void *)script_file;
  985.   script_file->common_section =
  986.    bfd_make_section(script_file->the_bfd,"COMMON");
  987.  
  988.   abs_output_section =
  989.    lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME);
  990.  
  991.   abs_output_section->bfd_section = &bfd_abs_section;
  992.  
  993. }
  994.  
  995. /* Open input files and attatch to output sections */
  996. static void
  997. DEFUN (map_input_to_output_sections, (s, target, output_section_statement),
  998.        lang_statement_union_type * s AND
  999.        CONST char *target AND
  1000.        lang_output_section_statement_type * output_section_statement)
  1001. {
  1002.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  1003.     {
  1004.       switch (s->header.type)
  1005.     {
  1006.  
  1007.  
  1008.     case lang_wild_statement_enum:
  1009.       wild (&s->wild_statement, s->wild_statement.section_name,
  1010.         s->wild_statement.filename, target,
  1011.         output_section_statement);
  1012.  
  1013.       break;
  1014.     case lang_constructors_statement_enum:
  1015.       map_input_to_output_sections (constructor_list.head,
  1016.                     target,
  1017.                     output_section_statement);
  1018.       break;
  1019.     case lang_output_section_statement_enum:
  1020.       map_input_to_output_sections (s->output_section_statement.children.head,
  1021.                     target,
  1022.                     &s->output_section_statement);
  1023.       break;
  1024.     case lang_output_statement_enum:
  1025.       break;
  1026.     case lang_target_statement_enum:
  1027.       target = s->target_statement.target;
  1028.       break;
  1029.     case lang_fill_statement_enum:
  1030.     case lang_input_section_enum:
  1031.     case lang_object_symbols_statement_enum:
  1032.     case lang_data_statement_enum:
  1033.     case lang_assignment_statement_enum:
  1034.     case lang_padding_statement_enum:
  1035.       break;
  1036.     case lang_afile_asection_pair_statement_enum:
  1037.       FAIL ();
  1038.       break;
  1039.     case lang_address_statement_enum:
  1040.       /* Mark the specified section with the supplied address */
  1041.       {
  1042.         lang_output_section_statement_type *os =
  1043.         lang_output_section_statement_lookup
  1044.         (s->address_statement.section_name);
  1045.  
  1046.         os->addr_tree = s->address_statement.address;
  1047.         if (os->bfd_section == (asection *) NULL)
  1048.           {
  1049.         einfo ("%P%F can't set the address of undefined section %s\n",
  1050.                s->address_statement.section_name);
  1051.           }
  1052.       }
  1053.       break;
  1054.     case lang_input_statement_enum:
  1055.       /* A standard input statement, has no wildcards */
  1056.       /*    ldmain_open_file_read_symbol(&s->input_statement);*/
  1057.       break;
  1058.     }
  1059.     }
  1060. }
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066. static void
  1067. DEFUN (print_output_section_statement, (output_section_statement),
  1068.        lang_output_section_statement_type * output_section_statement)
  1069. {
  1070.   asection *section = output_section_statement->bfd_section;
  1071.  
  1072.   print_nl ();
  1073.   print_section (output_section_statement->name);
  1074.  
  1075.  
  1076.   if (section)
  1077.   {
  1078.     print_dot = section->vma;
  1079.     print_space ();
  1080.     print_section ("");
  1081.     print_space ();
  1082.     print_address (section->vma);
  1083.     print_space ();
  1084.     print_size (section->_raw_size);
  1085.     print_space();
  1086.     print_size(section->_cooked_size);
  1087.     print_space ();
  1088.     print_alignment (section->alignment_power);
  1089.     print_space ();
  1090. #if 0
  1091.     fprintf (config.map_file, "%s flags", output_section_statement->region->name);
  1092.     print_flags (stdout, &output_section_statement->flags);
  1093. #endif
  1094.     if (section->flags & SEC_LOAD)
  1095.      fprintf (config.map_file, "load ");
  1096.     if (section->flags & SEC_ALLOC)
  1097.      fprintf (config.map_file, "alloc ");
  1098.     if (section->flags & SEC_RELOC)
  1099.      fprintf (config.map_file, "reloc ");
  1100.     if (section->flags & SEC_HAS_CONTENTS)
  1101.      fprintf (config.map_file, "contents ");
  1102.  
  1103.   }
  1104.   else
  1105.   {
  1106.     fprintf (config.map_file, "No attached output section");
  1107.   }
  1108.   print_nl ();
  1109.   if (output_section_statement->load_base)
  1110.     {
  1111.       int b = exp_get_value_int(output_section_statement->load_base,
  1112.                 0, "output base", lang_final_phase_enum);
  1113.       printf("Output address   %08x\n", b);
  1114.     }
  1115.   if (output_section_statement->section_alignment >= 0
  1116.       || output_section_statement->section_alignment >= 0) 
  1117.   {
  1118.     printf("\t\t\t\t\tforced alignment ");
  1119.     if ( output_section_statement->section_alignment >= 0) 
  1120.     {
  1121.       printf("section 2**%d ",output_section_statement->section_alignment );
  1122.     }
  1123.     if ( output_section_statement->subsection_alignment >= 0) 
  1124.     {
  1125.       printf("subsection 2**%d ",output_section_statement->subsection_alignment );
  1126.     }
  1127.   
  1128.     print_nl ();
  1129.   }
  1130.   print_statement (output_section_statement->children.head,
  1131.            output_section_statement);
  1132.  
  1133. }
  1134.  
  1135. static void
  1136. DEFUN (print_assignment, (assignment, output_section),
  1137.        lang_assignment_statement_type * assignment AND
  1138.        lang_output_section_statement_type * output_section)
  1139. {
  1140.   etree_value_type result;
  1141.  
  1142.   print_section ("");
  1143.   print_space ();
  1144.   print_section ("");
  1145.   print_space ();
  1146.   print_address (print_dot);
  1147.   print_space ();
  1148.   result = exp_fold_tree (assignment->exp->assign.src,
  1149.               output_section,
  1150.               lang_final_phase_enum,
  1151.               print_dot,
  1152.               &print_dot);
  1153.  
  1154.   if (result.valid)
  1155.     {
  1156.       print_address (result.value);
  1157.     }
  1158.   else
  1159.     {
  1160.       fprintf (config.map_file, "*undefined*");
  1161.     }
  1162.   print_space ();
  1163.   exp_print_tree (assignment->exp);
  1164.  
  1165.   fprintf (config.map_file, "\n");
  1166. }
  1167.  
  1168. static void
  1169. DEFUN (print_input_statement, (statm),
  1170.        lang_input_statement_type * statm)
  1171. {
  1172.   if (statm->filename != (char *) NULL)
  1173.     {
  1174.       fprintf (config.map_file, "LOAD %s\n", statm->filename);
  1175.     }
  1176. }
  1177.  
  1178. static void
  1179. DEFUN (print_symbol, (q),
  1180.        asymbol * q)
  1181. {
  1182.   print_section ("");
  1183.   fprintf (config.map_file, " ");
  1184.   print_section ("");
  1185.   fprintf (config.map_file, " ");
  1186.   print_address (outside_symbol_address (q));
  1187.   fprintf (config.map_file, "              %s", q->name ? q->name : " ");
  1188.   print_nl ();
  1189. }
  1190.  
  1191. static void
  1192. DEFUN (print_input_section, (in),
  1193.        lang_input_section_type * in)
  1194. {
  1195.   asection *i = in->section;
  1196.   int size = i->reloc_done ?
  1197.   bfd_get_section_size_after_reloc (i) :
  1198.   bfd_get_section_size_before_reloc (i);
  1199.  
  1200.   if (size != 0)
  1201.     {
  1202.       print_section ("");
  1203.       fprintf (config.map_file, " ");
  1204.       print_section (i->name);
  1205.       fprintf (config.map_file, " ");
  1206.       if (i->output_section)
  1207.     {
  1208.       print_address (i->output_section->vma + i->output_offset);
  1209.       fprintf (config.map_file, " ");
  1210.       print_size (i->_raw_size);
  1211.       fprintf (config.map_file, " ");
  1212.       print_size(i->_cooked_size);
  1213.       fprintf (config.map_file, " ");
  1214.       print_alignment (i->alignment_power);
  1215.       fprintf (config.map_file, " ");
  1216.       if (in->ifile)
  1217.         {
  1218.  
  1219.           bfd *abfd = in->ifile->the_bfd;
  1220.  
  1221.           if (in->ifile->just_syms_flag == true)
  1222.         {
  1223.           fprintf (config.map_file, "symbols only ");
  1224.         }
  1225.  
  1226.           fprintf (config.map_file, " %s ", abfd->xvec->name);
  1227.           if (abfd->my_archive != (bfd *) NULL)
  1228.         {
  1229.           fprintf (config.map_file, "[%s]%s", abfd->my_archive->filename,
  1230.                abfd->filename);
  1231.         }
  1232.           else
  1233.         {
  1234.           fprintf (config.map_file, "%s", abfd->filename);
  1235.         }
  1236.           fprintf (config.map_file, "(overhead %d bytes)", (int) bfd_alloc_size (abfd));
  1237.           print_nl ();
  1238.  
  1239.           /* Find all the symbols in this file defined in this section */
  1240.  
  1241.           if (in->ifile->symbol_count)
  1242.         {
  1243.           asymbol **p;
  1244.  
  1245.           for (p = in->ifile->asymbols; *p; p++)
  1246.             {
  1247.               asymbol *q = *p;
  1248.  
  1249.               if (bfd_get_section (q) == i && q->flags & BSF_GLOBAL)
  1250.             {
  1251.               print_symbol (q);
  1252.             }
  1253.             }
  1254.         }
  1255.         }
  1256.       else
  1257.         {
  1258.           print_nl ();
  1259.         }
  1260.  
  1261.  
  1262.       print_dot = outside_section_address (i) + size;
  1263.     }
  1264.       else
  1265.     {
  1266.       fprintf (config.map_file, "No output section allocated\n");
  1267.     }
  1268.     }
  1269. }
  1270.  
  1271. static void
  1272. DEFUN (print_fill_statement, (fill),
  1273.        lang_fill_statement_type * fill)
  1274. {
  1275.   fprintf (config.map_file, "FILL mask ");
  1276.   print_fill (fill->fill);
  1277. }
  1278.  
  1279. static void
  1280. DEFUN (print_data_statement, (data),
  1281.        lang_data_statement_type * data)
  1282. {
  1283. /*  bfd_vma value; */
  1284.   print_section ("");
  1285.   print_space ();
  1286.   print_section ("");
  1287.   print_space ();
  1288. /*  ASSERT(print_dot == data->output_vma);*/
  1289.  
  1290.   print_address (data->output_vma + data->output_section->vma);
  1291.   print_space ();
  1292.   print_address (data->value);
  1293.   print_space ();
  1294.   switch (data->type)
  1295.     {
  1296.     case BYTE:
  1297.       fprintf (config.map_file, "BYTE ");
  1298.       print_dot += BYTE_SIZE;
  1299.       break;
  1300.     case SHORT:
  1301.       fprintf (config.map_file, "SHORT ");
  1302.       print_dot += SHORT_SIZE;
  1303.       break;
  1304.     case LONG:
  1305.       fprintf (config.map_file, "LONG ");
  1306.       print_dot += LONG_SIZE;
  1307.       break;
  1308.     }
  1309.  
  1310.   exp_print_tree (data->exp);
  1311.  
  1312.   fprintf (config.map_file, "\n");
  1313. }
  1314.  
  1315.  
  1316. static void
  1317. DEFUN (print_padding_statement, (s),
  1318.        lang_padding_statement_type * s)
  1319. {
  1320.   print_section ("");
  1321.   print_space ();
  1322.   print_section ("*fill*");
  1323.   print_space ();
  1324.   print_address (s->output_offset + s->output_section->vma);
  1325.   print_space ();
  1326.   print_size (s->size);
  1327.   print_space ();
  1328.   print_fill (s->fill);
  1329.   print_nl ();
  1330.  
  1331.   print_dot = s->output_offset + s->output_section->vma + s->size;
  1332.  
  1333. }
  1334.  
  1335. static void
  1336. DEFUN (print_wild_statement, (w, os),
  1337.        lang_wild_statement_type * w AND
  1338.        lang_output_section_statement_type * os)
  1339. {
  1340.   fprintf (config.map_file, " from ");
  1341.   if (w->filename != (char *) NULL)
  1342.     {
  1343.       fprintf (config.map_file, "%s", w->filename);
  1344.     }
  1345.   else
  1346.     {
  1347.       fprintf (config.map_file, "*");
  1348.     }
  1349.   if (w->section_name != (char *) NULL)
  1350.     {
  1351.       fprintf (config.map_file, "(%s)", w->section_name);
  1352.     }
  1353.   else
  1354.     {
  1355.       fprintf (config.map_file, "(*)");
  1356.     }
  1357.   print_nl ();
  1358.   print_statement (w->children.head, os);
  1359.  
  1360. }
  1361. static void
  1362. DEFUN (print_statement, (s, os),
  1363.        lang_statement_union_type * s AND
  1364.        lang_output_section_statement_type * os)
  1365. {
  1366.   while (s)
  1367.     {
  1368.       switch (s->header.type)
  1369.     {
  1370.       case lang_constructors_statement_enum:
  1371.       fprintf (config.map_file, "constructors:\n");
  1372.       print_statement (constructor_list.head, os);
  1373.       break;
  1374.     case lang_wild_statement_enum:
  1375.       print_wild_statement (&s->wild_statement, os);
  1376.       break;
  1377.     default:
  1378.       fprintf (config.map_file, "Fail with %d\n", s->header.type);
  1379.       FAIL ();
  1380.       break;
  1381.     case lang_address_statement_enum:
  1382.       fprintf (config.map_file, "address\n");
  1383.       break;
  1384.     case lang_object_symbols_statement_enum:
  1385.       fprintf (config.map_file, "object symbols\n");
  1386.       break;
  1387.     case lang_fill_statement_enum:
  1388.       print_fill_statement (&s->fill_statement);
  1389.       break;
  1390.     case lang_data_statement_enum:
  1391.       print_data_statement (&s->data_statement);
  1392.       break;
  1393.     case lang_input_section_enum:
  1394.       print_input_section (&s->input_section);
  1395.       break;
  1396.     case lang_padding_statement_enum:
  1397.       print_padding_statement (&s->padding_statement);
  1398.       break;
  1399.     case lang_output_section_statement_enum:
  1400.       print_output_section_statement (&s->output_section_statement);
  1401.       break;
  1402.     case lang_assignment_statement_enum:
  1403.       print_assignment (&s->assignment_statement,
  1404.                 os);
  1405.       break;
  1406.     case lang_target_statement_enum:
  1407.       fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
  1408.       break;
  1409.     case lang_output_statement_enum:
  1410.       fprintf (config.map_file, "OUTPUT(%s %s)\n",
  1411.            s->output_statement.name,
  1412.            output_target ? output_target : "");
  1413.       break;
  1414.     case lang_input_statement_enum:
  1415.       print_input_statement (&s->input_statement);
  1416.       break;
  1417.     case lang_afile_asection_pair_statement_enum:
  1418.       FAIL ();
  1419.       break;
  1420.     }
  1421.       s = s->next;
  1422.     }
  1423. }
  1424.  
  1425.  
  1426. static void
  1427. DEFUN_VOID (print_statements)
  1428. {
  1429.   print_statement (statement_list.head,
  1430.            abs_output_section);
  1431.  
  1432. }
  1433.  
  1434. static bfd_vma
  1435. DEFUN (insert_pad, (this_ptr, fill, power, output_section_statement, dot),
  1436.        lang_statement_union_type ** this_ptr AND
  1437.        fill_type fill AND
  1438.        unsigned int power AND
  1439.        asection * output_section_statement AND
  1440.        bfd_vma dot)
  1441. {
  1442.   /* Align this section first to the
  1443.      input sections requirement, then
  1444.      to the output section's requirement.
  1445.      If this alignment is > than any seen before,
  1446.      then record it too. Perform the alignment by
  1447.      inserting a magic 'padding' statement.
  1448.      */
  1449.  
  1450.   unsigned int alignment_needed = align_power (dot, power) - dot;
  1451.  
  1452.   if (alignment_needed != 0)
  1453.     {
  1454.       lang_statement_union_type *new =
  1455.       (lang_statement_union_type *)
  1456.       stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
  1457.  
  1458.       /* Link into existing chain */
  1459.       new->header.next = *this_ptr;
  1460.       *this_ptr = new;
  1461.       new->header.type = lang_padding_statement_enum;
  1462.       new->padding_statement.output_section = output_section_statement;
  1463.       new->padding_statement.output_offset =
  1464.     dot - output_section_statement->vma;
  1465.       new->padding_statement.fill = fill;
  1466.       new->padding_statement.size = alignment_needed;
  1467.     }
  1468.  
  1469.  
  1470.   /* Remember the most restrictive alignment */
  1471.   if (power > output_section_statement->alignment_power)
  1472.     {
  1473.       output_section_statement->alignment_power = power;
  1474.     }
  1475.   output_section_statement->_raw_size += alignment_needed;
  1476.   return alignment_needed + dot;
  1477.  
  1478. }
  1479.  
  1480. /* Work out how much this section will move the dot point */
  1481. static bfd_vma
  1482. DEFUN (size_input_section, (this_ptr, output_section_statement, fill,
  1483.                 dot, relax),
  1484.        lang_statement_union_type ** this_ptr AND
  1485.        lang_output_section_statement_type * output_section_statement AND
  1486.        unsigned short fill AND
  1487.        bfd_vma dot AND
  1488.        boolean relax)
  1489. {
  1490.   lang_input_section_type *is = &((*this_ptr)->input_section);
  1491.   asection *i = is->section;
  1492.  
  1493.   if (is->ifile->just_syms_flag == false)
  1494.     {
  1495.       if (output_section_statement->subsection_alignment != -1)
  1496.        i->alignment_power =
  1497.     output_section_statement->subsection_alignment;
  1498.  
  1499.       dot = insert_pad (this_ptr, fill, i->alignment_power,
  1500.             output_section_statement->bfd_section, dot);
  1501.  
  1502.       /* remember the largest size so we can malloc the largest area
  1503.          needed for the output stage. Only remember the size of sections
  1504.          which we will actually allocate  */
  1505.       if ((i->flags & SEC_HAS_CONTENTS) != 0
  1506.       && (bfd_get_section_size_before_reloc (i) > largest_section))
  1507.     {
  1508.       largest_section = bfd_get_section_size_before_reloc (i);
  1509.     }
  1510.  
  1511.       /* Remember where in the output section this input section goes */
  1512.  
  1513.       i->output_offset = dot - output_section_statement->bfd_section->vma;
  1514.  
  1515.       /* Mark how big the output section must be to contain this now
  1516.      */
  1517.       if (relax)
  1518.     {
  1519.       dot += i->_cooked_size;
  1520.     }
  1521.       else
  1522.     {
  1523.       dot += i->_raw_size;
  1524.     }
  1525.       output_section_statement->bfd_section->_raw_size = dot - output_section_statement->bfd_section->vma;
  1526.     }
  1527.   else
  1528.     {
  1529.       i->output_offset = i->vma - output_section_statement->bfd_section->vma;
  1530.     }
  1531.  
  1532.   return dot;
  1533. }
  1534.  
  1535. /* Sizing happens in two passes, first pass we allocate worst case
  1536.    stuff. The second pass (if relaxing), we use what we learnt to
  1537.    change the size of some relocs from worst case to better
  1538.    */
  1539. static boolean had_relax;
  1540.  
  1541. static bfd_vma
  1542. DEFUN (lang_size_sections, (s, output_section_statement, prev, fill,
  1543.                 dot, relax),
  1544.        lang_statement_union_type * s AND
  1545.        lang_output_section_statement_type * output_section_statement AND
  1546.        lang_statement_union_type ** prev AND
  1547.        unsigned short fill AND
  1548.        bfd_vma dot AND
  1549.        boolean relax)
  1550. {
  1551.   /* Size up the sections from their constituent parts */
  1552.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  1553.   {
  1554.     switch (s->header.type)
  1555.     {
  1556.  
  1557.      case lang_output_section_statement_enum:
  1558.      {
  1559.        bfd_vma after;
  1560.        lang_output_section_statement_type *os = &s->output_section_statement;
  1561.  
  1562.        /* If this is a shared library section, don't change the size
  1563.       and address.  */
  1564.        if (os->bfd_section->flags & SEC_SHARED_LIBRARY)
  1565.      break;
  1566.  
  1567.        if (os->bfd_section == &bfd_abs_section)
  1568.        {
  1569.      /* No matter what happens, an abs section starts at zero */
  1570.      bfd_set_section_vma (0, os->bfd_section, 0);
  1571.        }
  1572.        else
  1573.        {
  1574.      if (os->addr_tree == (etree_type *) NULL)
  1575.      {
  1576.        /* No address specified for this section, get one
  1577.           from the region specification
  1578.           */
  1579.        if (os->region == (lang_memory_region_type *) NULL)
  1580.        {
  1581.          os->region = lang_memory_region_lookup ("*default*");
  1582.        }
  1583.        dot = os->region->current;
  1584.      }
  1585.      else
  1586.      {
  1587.        etree_value_type r;
  1588.  
  1589.        r = exp_fold_tree (os->addr_tree,
  1590.                   abs_output_section,
  1591.                   lang_allocating_phase_enum,
  1592.                   dot, &dot);
  1593.        if (r.valid == false)
  1594.        {
  1595.          einfo ("%F%S: non constant address expression for section %s\n",
  1596.             os->name);
  1597.        }
  1598.        dot = r.value;
  1599.      }
  1600.      /* The section starts here */
  1601.      /* First, align to what the section needs */
  1602.  
  1603.  
  1604.      dot = align_power (dot, os->bfd_section->alignment_power);
  1605.      bfd_set_section_vma (0, os->bfd_section, dot);
  1606.      
  1607.      if (os->load_base) {
  1608.        os->bfd_section->lma 
  1609.          = exp_get_value_int(os->load_base, 0,"load base", lang_final_phase_enum);
  1610.      }
  1611.        }
  1612.  
  1613.  
  1614.        os->bfd_section->output_offset = 0;
  1615.  
  1616.        (void) lang_size_sections (os->children.head, os, &os->children.head,
  1617.                   os->fill, dot, relax);
  1618.        /* Ignore the size of the input sections, use the vma and size to */
  1619.        /* align against */
  1620.  
  1621.  
  1622.        after = ALIGN_N (os->bfd_section->vma +
  1623.               os->bfd_section->_raw_size,
  1624.               os->block_value);
  1625.  
  1626.  
  1627.        os->bfd_section->_raw_size = after - os->bfd_section->vma;
  1628.        dot = os->bfd_section->vma + os->bfd_section->_raw_size;
  1629.        os->processed = true;
  1630.  
  1631.        /* Replace into region ? */
  1632.        if (os->addr_tree == (etree_type *) NULL
  1633.        && os->region != (lang_memory_region_type *) NULL)
  1634.        {
  1635.      os->region->current = dot;
  1636.      /* Make sure this isn't silly */
  1637.      if (( os->region->current
  1638.               > os->region->origin + os->region->length)
  1639.          || ( os->region->origin > os->region->current ))
  1640.        {
  1641.          einfo ("%X%P: Region %s is full (%B section %s)\n",
  1642.             os->region->name,
  1643.             os->bfd_section->owner,
  1644.             os->bfd_section->name);
  1645.          /* Reset the region pointer */
  1646.          os->region->current = 0;
  1647.  
  1648.        }
  1649.  
  1650.        }
  1651.      }
  1652.  
  1653.       break;
  1654.      case lang_constructors_statement_enum:
  1655.       dot = lang_size_sections (constructor_list.head,
  1656.                 output_section_statement,
  1657.                 &s->wild_statement.children.head,
  1658.                 fill,
  1659.                 dot, relax);
  1660.       break;
  1661.  
  1662.      case lang_data_statement_enum:
  1663.      {
  1664.        unsigned int size = 0;
  1665.  
  1666.        s->data_statement.output_vma = dot - output_section_statement->bfd_section->vma;
  1667.        s->data_statement.output_section =
  1668.     output_section_statement->bfd_section;
  1669.  
  1670.        switch (s->data_statement.type)
  1671.        {
  1672.     case LONG:
  1673.      size = LONG_SIZE;
  1674.      break;
  1675.     case SHORT:
  1676.      size = SHORT_SIZE;
  1677.      break;
  1678.     case BYTE:
  1679.      size = BYTE_SIZE;
  1680.      break;
  1681.  
  1682.        }
  1683.        dot += size;
  1684.        output_section_statement->bfd_section->_raw_size += size;
  1685.      }
  1686.       break;
  1687.  
  1688.      case lang_wild_statement_enum:
  1689.  
  1690.       dot = lang_size_sections (s->wild_statement.children.head,
  1691.                 output_section_statement,
  1692.                 &s->wild_statement.children.head,
  1693.  
  1694.                 fill, dot, relax);
  1695.  
  1696.       break;
  1697.  
  1698.      case lang_object_symbols_statement_enum:
  1699.       create_object_symbols = output_section_statement;
  1700.       break;
  1701.      case lang_output_statement_enum:
  1702.      case lang_target_statement_enum:
  1703.       break;
  1704.      case lang_input_section_enum:
  1705.       if (relax)
  1706.       {
  1707.     relaxing = true;
  1708.  
  1709.     if( relax_section (prev))
  1710.      had_relax = true;
  1711.     relaxing = false;
  1712.  
  1713.       }
  1714.       else  {
  1715.     (*prev)->input_section.section->_cooked_size = 
  1716.      (*prev)->input_section.section->_raw_size ;
  1717.  
  1718.       }
  1719.       dot = size_input_section (prev,
  1720.                 output_section_statement,
  1721.                 output_section_statement->fill,
  1722.                 dot, relax);
  1723.       break;
  1724.      case lang_input_statement_enum:
  1725.       break;
  1726.      case lang_fill_statement_enum:
  1727.       s->fill_statement.output_section = output_section_statement->bfd_section;
  1728.  
  1729.       fill = s->fill_statement.fill;
  1730.       break;
  1731.      case lang_assignment_statement_enum:
  1732.      {
  1733.        bfd_vma newdot = dot;
  1734.  
  1735.        exp_fold_tree (s->assignment_statement.exp,
  1736.               output_section_statement,
  1737.               lang_allocating_phase_enum,
  1738.               dot,
  1739.               &newdot);
  1740.  
  1741.        if (newdot != dot && !relax)
  1742.     /* We've been moved ! so insert a pad */
  1743.        {
  1744.      lang_statement_union_type *new =
  1745.       (lang_statement_union_type *)
  1746.        stat_alloc ((bfd_size_type) (sizeof (lang_padding_statement_type)));
  1747.  
  1748.      /* Link into existing chain */
  1749.      new->header.next = *prev;
  1750.      *prev = new;
  1751.      new->header.type = lang_padding_statement_enum;
  1752.      new->padding_statement.output_section =
  1753.       output_section_statement->bfd_section;
  1754.      new->padding_statement.output_offset =
  1755.       dot - output_section_statement->bfd_section->vma;
  1756.      new->padding_statement.fill = fill;
  1757.      new->padding_statement.size = newdot - dot;
  1758.      output_section_statement->bfd_section->_raw_size +=
  1759.       new->padding_statement.size;
  1760.      dot = newdot;
  1761.        }
  1762.      }
  1763.  
  1764.       break;
  1765.      default:
  1766.       FAIL ();
  1767.       break;
  1768.       /* This can only get here when relaxing is turned on */
  1769.      case lang_padding_statement_enum:
  1770.  
  1771.      case lang_address_statement_enum:
  1772.       break;
  1773.     }
  1774.     prev = &s->header.next;
  1775.   }
  1776.   return dot;
  1777. }
  1778.  
  1779. static bfd_vma
  1780. DEFUN (lang_do_assignments, (s, output_section_statement, fill, dot),
  1781.        lang_statement_union_type * s AND
  1782.        lang_output_section_statement_type * output_section_statement AND
  1783.        unsigned short fill AND
  1784.        bfd_vma dot)
  1785. {
  1786.  
  1787.   for (; s != (lang_statement_union_type *) NULL; s = s->next)
  1788.     {
  1789.       switch (s->header.type)
  1790.     {
  1791.     case lang_constructors_statement_enum:
  1792.       dot = lang_do_assignments (constructor_list.head,
  1793.                      output_section_statement,
  1794.                      fill,
  1795.                      dot);
  1796.       break;
  1797.  
  1798.     case lang_output_section_statement_enum:
  1799.       {
  1800.         lang_output_section_statement_type *os =
  1801.         &(s->output_section_statement);
  1802.  
  1803.         dot = os->bfd_section->vma;
  1804.         (void) lang_do_assignments (os->children.head, os, os->fill, dot);
  1805.         dot = os->bfd_section->vma + os->bfd_section->_raw_size;
  1806.       }
  1807.       break;
  1808.     case lang_wild_statement_enum:
  1809.  
  1810.       dot = lang_do_assignments (s->wild_statement.children.head,
  1811.                      output_section_statement,
  1812.                      fill, dot);
  1813.  
  1814.       break;
  1815.  
  1816.     case lang_object_symbols_statement_enum:
  1817.     case lang_output_statement_enum:
  1818.     case lang_target_statement_enum:
  1819. #if 0
  1820.     case lang_common_statement_enum:
  1821. #endif
  1822.       break;
  1823.     case lang_data_statement_enum:
  1824.       {
  1825.         etree_value_type value;
  1826.  
  1827.         value = exp_fold_tree (s->data_statement.exp,
  1828.                    abs_output_section,
  1829.                    lang_final_phase_enum, dot, &dot);
  1830.         s->data_statement.value = value.value;
  1831.         if (value.valid == false)
  1832.           einfo ("%F%P: Invalid data statement\n");
  1833.       }
  1834.       switch (s->data_statement.type)
  1835.         {
  1836.         case LONG:
  1837.           dot += LONG_SIZE;
  1838.           break;
  1839.         case SHORT:
  1840.           dot += SHORT_SIZE;
  1841.           break;
  1842.         case BYTE:
  1843.           dot += BYTE_SIZE;
  1844.           break;
  1845.         }
  1846.       break;
  1847.     case lang_input_section_enum:
  1848.       {
  1849.         asection *in = s->input_section.section;
  1850.  
  1851.         dot += bfd_get_section_size_before_reloc (in);
  1852.       }
  1853.       break;
  1854.  
  1855.     case lang_input_statement_enum:
  1856.       break;
  1857.     case lang_fill_statement_enum:
  1858.       fill = s->fill_statement.fill;
  1859.       break;
  1860.     case lang_assignment_statement_enum:
  1861.       {
  1862.         exp_fold_tree (s->assignment_statement.exp,
  1863.                output_section_statement,
  1864.                lang_final_phase_enum,
  1865.                dot,
  1866.                &dot);
  1867.       }
  1868.  
  1869.       break;
  1870.     case lang_padding_statement_enum:
  1871.       dot += s->padding_statement.size;
  1872.       break;
  1873.     default:
  1874.       FAIL ();
  1875.       break;
  1876.     case lang_address_statement_enum:
  1877.       break;
  1878.     }
  1879.  
  1880.     }
  1881.   return dot;
  1882. }
  1883.  
  1884.  
  1885.  
  1886. static void
  1887. DEFUN_VOID (lang_relocate_globals)
  1888. {
  1889.  
  1890.   /*
  1891.     Each ldsym_type maintains a chain of pointers to asymbols which
  1892.     references the definition.  Replace each pointer to the referenence
  1893.     with a pointer to only one place, preferably the definition. If
  1894.     the defintion isn't available then the common symbol, and if
  1895.     there isn't one of them then choose one reference.
  1896.     */
  1897.  
  1898.   FOR_EACH_LDSYM (lgs)
  1899.   {
  1900.     asymbol *it;
  1901.  
  1902.     if (lgs->sdefs_chain)
  1903.       {
  1904.     it = *(lgs->sdefs_chain);
  1905.       }
  1906.     else if (lgs->scoms_chain != (asymbol **) NULL)
  1907.       {
  1908.     it = *(lgs->scoms_chain);
  1909.       }
  1910.     else if (lgs->srefs_chain != (asymbol **) NULL)
  1911.       {
  1912.     it = *(lgs->srefs_chain);
  1913.       }
  1914.     else
  1915.       {
  1916.     /* This can happen when the command line asked for a symbol to
  1917.        be -u */
  1918.     it = (asymbol *) NULL;
  1919.       }
  1920.     if (it != (asymbol *) NULL)
  1921.       {
  1922.     asymbol **prev = 0;
  1923.     asymbol **ptr = lgs->srefs_chain;;
  1924.     if (lgs->flags & SYM_WARNING)
  1925.       {
  1926.         produce_warnings (lgs, it);
  1927.       }
  1928.  
  1929.     while (ptr != (asymbol **) NULL
  1930.            && ptr != prev)
  1931.       {
  1932.         asymbol *ref = *ptr;
  1933.         prev = ptr;
  1934.         *ptr = it;
  1935.         ptr = (asymbol **) (ref->udata);
  1936.       }
  1937.       }
  1938.   }
  1939. }
  1940.  
  1941.  
  1942.  
  1943. static void
  1944. DEFUN_VOID (lang_finish)
  1945. {
  1946.   ldsym_type *lgs;
  1947.   int warn = config.relocateable_output != true;
  1948.   if (entry_symbol == (char *) NULL)
  1949.   {
  1950.     /* No entry has been specified, look for start, but don't warn */
  1951.     entry_symbol = "start";
  1952.     warn =0;
  1953.   }
  1954.   lgs = ldsym_get_soft (entry_symbol);
  1955.   if (lgs && lgs->sdefs_chain)
  1956.   {
  1957.     asymbol *sy = *(lgs->sdefs_chain);
  1958.  
  1959.     /* We can set the entry address*/
  1960.     bfd_set_start_address (output_bfd,
  1961.                outside_symbol_address (sy));
  1962.  
  1963.   }
  1964.   else
  1965.   {
  1966.     /* Can't find anything reasonable,
  1967.        use the first address in the text section
  1968.        */
  1969.     asection *ts = bfd_get_section_by_name (output_bfd, ".text");
  1970.     if (ts)
  1971.     {
  1972.       if (warn)
  1973.        einfo ("%P: Warning, can't find entry symbol %s, defaulting to %V\n",
  1974.           entry_symbol, ts->vma);
  1975.  
  1976.       bfd_set_start_address (output_bfd, ts->vma);
  1977.     }
  1978.     else 
  1979.     {
  1980.       if (warn)
  1981.        einfo ("%P: Warning, can't find entry symbol %s, not setting start address\n",
  1982.           entry_symbol);
  1983.     }
  1984.   }
  1985. }
  1986.  
  1987. /* By now we know the target architecture, and we may have an */
  1988. /* ldfile_output_machine_name */
  1989. static void
  1990. DEFUN_VOID (lang_check)
  1991. {
  1992.   lang_statement_union_type *file;
  1993.   bfd *input_bfd;
  1994.   unsigned long input_machine;
  1995.   enum bfd_architecture input_architecture;
  1996.   CONST bfd_arch_info_type *compatible;
  1997.  
  1998.   for (file = file_chain.head;
  1999.        file != (lang_statement_union_type *) NULL;
  2000.        file = file->input_statement.next)
  2001.     {
  2002.       input_bfd = file->input_statement.the_bfd;
  2003.  
  2004.       input_machine = bfd_get_mach (input_bfd);
  2005.       input_architecture = bfd_get_arch (input_bfd);
  2006.  
  2007.  
  2008.       /* Inspect the architecture and ensure we're linking like with
  2009.          like */
  2010.  
  2011.       compatible = bfd_arch_get_compatible (input_bfd,
  2012.                         output_bfd);
  2013.  
  2014.       if (compatible)
  2015.     {
  2016.       ldfile_output_machine = compatible->mach;
  2017.       ldfile_output_architecture = compatible->arch;
  2018.     }
  2019.       else
  2020.     {
  2021.  
  2022.       info ("%P: warning, %s architecture of input file `%B' incompatible with %s output\n",
  2023.         bfd_printable_name (input_bfd), input_bfd,
  2024.         bfd_printable_name (output_bfd));
  2025.  
  2026.       bfd_set_arch_mach (output_bfd,
  2027.                  input_architecture,
  2028.                  input_machine);
  2029.     }
  2030.  
  2031.     }
  2032. }
  2033.  
  2034. /*
  2035.  * run through all the global common symbols and tie them
  2036.  * to the output section requested.
  2037.  *
  2038.  As an experiment we do this 4 times, once for all the byte sizes,
  2039.  then all the two  bytes, all the four bytes and then everything else
  2040.   */
  2041.  
  2042. static void
  2043. DEFUN_VOID (lang_common)
  2044. {
  2045.   ldsym_type *lgs;
  2046.   size_t power;
  2047.  
  2048.   if (config.relocateable_output == false ||
  2049.       command_line.force_common_definition == true)
  2050.     {
  2051.       for (power = 1; (config.sort_common == true && power == 1) || (power <= 16); power <<= 1)
  2052.     {
  2053.       for (lgs = symbol_head;
  2054.            lgs != (ldsym_type *) NULL;
  2055.            lgs = lgs->next)
  2056.         {
  2057.           asymbol *com;
  2058.           unsigned int power_of_two;
  2059.           size_t size;
  2060.           size_t align;
  2061.  
  2062.           if (lgs->scoms_chain != (asymbol **) NULL)
  2063.         {
  2064.           com = *(lgs->scoms_chain);
  2065.           size = com->value;
  2066.           switch (size)
  2067.             {
  2068.             case 0:
  2069.             case 1:
  2070.               align = 1;
  2071.               power_of_two = 0;
  2072.               break;
  2073.             case 2:
  2074.               power_of_two = 1;
  2075.               align = 2;
  2076.               break;
  2077.             case 3:
  2078.             case 4:
  2079.               power_of_two = 2;
  2080.               align = 4;
  2081.               break;
  2082.             case 5:
  2083.             case 6:
  2084.             case 7:
  2085.             case 8:
  2086.               power_of_two = 3;
  2087.               align = 8;
  2088.               break;
  2089.             default:
  2090.               power_of_two = 4;
  2091.               align = 16;
  2092.               break;
  2093.             }
  2094.           if (config.sort_common == false || align == power)
  2095.             {
  2096.               bfd *symbfd;
  2097.  
  2098.               /* Change from a common symbol into a definition of
  2099.              a symbol */
  2100.               lgs->sdefs_chain = lgs->scoms_chain;
  2101.               lgs->scoms_chain = (asymbol **) NULL;
  2102.               commons_pending--;
  2103.  
  2104.               /* Point to the correct common section */
  2105.               symbfd = bfd_asymbol_bfd (com);
  2106.               if (com->section == &bfd_com_section)
  2107.             com->section =
  2108.               ((lang_input_statement_type *) symbfd->usrdata)
  2109.                 ->common_section;
  2110.               else
  2111.             {
  2112.               CONST char *name;
  2113.               asection *newsec;
  2114.  
  2115.               name = bfd_get_section_name (symbfd,
  2116.                                com->section);
  2117.               newsec = bfd_get_section_by_name (symbfd,
  2118.                                 name);
  2119.               /* BFD backend must provide this section. */
  2120.               if (newsec == (asection *) NULL)
  2121.                 einfo ("%P%F: No output section %s", name);
  2122.               com->section = newsec;
  2123.             }
  2124.  
  2125.               /*  Fix the size of the common section */
  2126.  
  2127.               com->section->_raw_size =
  2128.             ALIGN_N (com->section->_raw_size, align);
  2129.  
  2130.               /* Remember if this is the biggest alignment ever seen */
  2131.               if (power_of_two > com->section->alignment_power)
  2132.             {
  2133.               com->section->alignment_power = power_of_two;
  2134.             }
  2135.  
  2136.               /* Symbol stops being common and starts being global, but
  2137.              we remember that it was common once. */
  2138.  
  2139.               com->flags = BSF_EXPORT | BSF_GLOBAL | BSF_OLD_COMMON;
  2140.               com->value = com->section->_raw_size;
  2141.  
  2142.               if (write_map && config.map_file)
  2143.             {
  2144.               fprintf (config.map_file, "Allocating common %s: %x at %x %s\n",
  2145.                    lgs->name,
  2146.                    (unsigned) size,
  2147.                    (unsigned) com->value,
  2148.                    bfd_asymbol_bfd(com)->filename);
  2149.             }
  2150.  
  2151.               com->section->_raw_size += size;
  2152.  
  2153.             }
  2154.         }
  2155.  
  2156.         }
  2157.     }
  2158.     }
  2159.  
  2160.  
  2161. }
  2162.  
  2163. /*
  2164. run through the input files and ensure that every input
  2165. section has somewhere to go. If one is found without
  2166. a destination then create an input request and place it
  2167. into the statement tree.
  2168. */
  2169.  
  2170. static void
  2171. DEFUN_VOID (lang_place_orphans)
  2172. {
  2173.   lang_input_statement_type *file;
  2174.  
  2175.   for (file = (lang_input_statement_type *) file_chain.head;
  2176.        file != (lang_input_statement_type *) NULL;
  2177.        file = (lang_input_statement_type *) file->next)
  2178.     {
  2179.       asection *s;
  2180.  
  2181.       for (s = file->the_bfd->sections;
  2182.        s != (asection *) NULL;
  2183.        s = s->next)
  2184.     {
  2185.       if (s->output_section == (asection *) NULL)
  2186.         {
  2187.           /* This section of the file is not attatched, root
  2188.              around for a sensible place for it to go */
  2189.  
  2190.           if (file->common_section == s)
  2191.         {
  2192.           /* This is a lonely common section which must
  2193.              have come from an archive. We attatch to the
  2194.              section with the wildcard  */
  2195.           if (config.relocateable_output != true
  2196.               && command_line.force_common_definition == false)
  2197.             {
  2198.               if (default_common_section ==
  2199.               (lang_output_section_statement_type *) NULL)
  2200.             {
  2201.               info ("%P: No [COMMON] command, defaulting to .bss\n");
  2202.  
  2203.               default_common_section =
  2204.                 lang_output_section_statement_lookup (".bss");
  2205.  
  2206.             }
  2207.               wild_doit (&default_common_section->children, s,
  2208.                  default_common_section, file);
  2209.             }
  2210.         }
  2211.           else
  2212.         {
  2213.           lang_output_section_statement_type *os =
  2214.           lang_output_section_statement_lookup (s->name);
  2215.  
  2216.           wild_doit (&os->children, s, os, file);
  2217.         }
  2218.         }
  2219.     }
  2220.     }
  2221. }
  2222.  
  2223.  
  2224. void
  2225. DEFUN (lang_set_flags, (ptr, flags),
  2226.        int *ptr AND
  2227.        CONST char *flags)
  2228. {
  2229.   boolean state = false;
  2230.  
  2231.   *ptr = 0;
  2232.   while (*flags)
  2233.     {
  2234.       if (*flags == '!')
  2235.     {
  2236.       state = false;
  2237.       flags++;
  2238.     }
  2239.       else
  2240.     state = true;
  2241.       switch (*flags)
  2242.     {
  2243.     case 'R':
  2244.       /*      ptr->flag_read = state; */
  2245.       break;
  2246.     case 'W':
  2247.       /*      ptr->flag_write = state; */
  2248.       break;
  2249.     case 'X':
  2250.       /*      ptr->flag_executable= state;*/
  2251.       break;
  2252.     case 'L':
  2253.     case 'I':
  2254.       /*      ptr->flag_loadable= state;*/
  2255.       break;
  2256.     default:
  2257.       einfo ("%P%F illegal syntax in flags\n");
  2258.       break;
  2259.     }
  2260.       flags++;
  2261.     }
  2262. }
  2263.  
  2264.  
  2265.  
  2266. void
  2267. DEFUN (lang_for_each_file, (func),
  2268.        void (*func) PARAMS ((lang_input_statement_type *)))
  2269. {
  2270.   lang_input_statement_type *f;
  2271.  
  2272.   for (f = (lang_input_statement_type *) file_chain.head;
  2273.        f != (lang_input_statement_type *) NULL;
  2274.        f = (lang_input_statement_type *) f->next)
  2275.     {
  2276.       func (f);
  2277.     }
  2278. }
  2279.  
  2280.  
  2281. void
  2282. DEFUN (lang_for_each_input_section, (func),
  2283.        void (*func) PARAMS ((bfd * ab, asection * as)))
  2284. {
  2285.   lang_input_statement_type *f;
  2286.  
  2287.   for (f = (lang_input_statement_type *) file_chain.head;
  2288.        f != (lang_input_statement_type *) NULL;
  2289.        f = (lang_input_statement_type *) f->next)
  2290.     {
  2291.       asection *s;
  2292.  
  2293.       for (s = f->the_bfd->sections;
  2294.        s != (asection *) NULL;
  2295.        s = s->next)
  2296.     {
  2297.       func (f->the_bfd, s);
  2298.     }
  2299.     }
  2300. }
  2301.  
  2302.  
  2303.  
  2304. void
  2305. DEFUN (ldlang_add_file, (entry),
  2306.        lang_input_statement_type * entry)
  2307. {
  2308.  
  2309.   lang_statement_append (&file_chain,
  2310.              (lang_statement_union_type *) entry,
  2311.              &entry->next);
  2312. }
  2313.  
  2314. void
  2315. DEFUN (lang_add_output, (name),
  2316.        CONST char *name)
  2317. {
  2318.   lang_output_statement_type *new = new_stat (lang_output_statement,
  2319.                           stat_ptr);
  2320.  
  2321.   new->name = name;
  2322.   had_output_filename = true;
  2323. }
  2324.  
  2325.  
  2326. static lang_output_section_statement_type *current_section;
  2327.  
  2328. static int topower(x)
  2329.  int x;
  2330. {
  2331.   unsigned  int i = 1;
  2332.   int l;
  2333.   if (x < 0) return -1;
  2334.   for (l = 0; l < 32; l++) 
  2335.   {
  2336.     if (i >= x) return l;
  2337.     i<<=1;
  2338.   }
  2339.   return 0;
  2340. }
  2341. void
  2342. DEFUN (lang_enter_output_section_statement,
  2343.        (output_section_statement_name,
  2344.     address_exp,
  2345.     flags,
  2346.     block_value, 
  2347.     align, subalign, base),
  2348.        char *output_section_statement_name AND
  2349.        etree_type * address_exp AND
  2350.        int flags AND
  2351.        bfd_vma block_value AND
  2352.        etree_type *align AND
  2353.        etree_type *subalign AND
  2354.        etree_type *base)
  2355. {
  2356.   lang_output_section_statement_type *os;
  2357.  
  2358.   current_section =
  2359.    os =
  2360.     lang_output_section_statement_lookup (output_section_statement_name);
  2361.  
  2362.  
  2363.  
  2364.   /* Add this statement to tree */
  2365.   /*  add_statement(lang_output_section_statement_enum,
  2366.       output_section_statement);*/
  2367.   /* Make next things chain into subchain of this */
  2368.  
  2369.   if (os->addr_tree ==
  2370.       (etree_type *) NULL)
  2371.   {
  2372.     os->addr_tree =
  2373.      address_exp;
  2374.   }
  2375.   os->flags = flags;
  2376.   if (flags & SEC_NEVER_LOAD)
  2377.    os->loadable = 0;
  2378.   else
  2379.    os->loadable = 1;
  2380.   os->block_value = block_value ? block_value : 1;
  2381.   stat_ptr = &os->children;
  2382.  
  2383.   os->subsection_alignment = topower(
  2384.    exp_get_value_int(subalign, -1,
  2385.              "subsection alignment",
  2386.              0));
  2387.   os->section_alignment = topower(
  2388.    exp_get_value_int(align, -1,
  2389.              "section alignment", 0));
  2390.  
  2391.   os->load_base = base;
  2392. }
  2393.  
  2394.  
  2395. void
  2396. DEFUN_VOID (lang_final)
  2397. {
  2398.   if (had_output_filename == false)
  2399.     {
  2400.       extern CONST char *output_filename;
  2401.  
  2402.       lang_add_output (output_filename);
  2403.     }
  2404. }
  2405.  
  2406. /* Reset the current counters in the regions */
  2407. static void
  2408. DEFUN_VOID (reset_memory_regions)
  2409. {
  2410.   lang_memory_region_type *p = lang_memory_region_list;
  2411.  
  2412.   for (p = lang_memory_region_list;
  2413.        p != (lang_memory_region_type *) NULL;
  2414.        p = p->next)
  2415.     {
  2416.       p->old_length = (bfd_size_type) (p->current - p->origin);
  2417.       p->current = p->origin;
  2418.     }
  2419. }
  2420.  
  2421.  
  2422.  
  2423. asymbol *
  2424. DEFUN (create_symbol, (name, flags, section),
  2425.        CONST char *name AND
  2426.        flagword flags AND
  2427.        asection * section)
  2428. {
  2429.   extern lang_input_statement_type *script_file;
  2430.   asymbol **def_ptr = (asymbol **) stat_alloc ((bfd_size_type) (sizeof (asymbol **)));
  2431.  
  2432.   /* Add this definition to script file */
  2433.   asymbol *def = (asymbol *) bfd_make_empty_symbol (script_file->the_bfd);
  2434.   def->name = buystring (name);
  2435.   def->udata = 0;
  2436.   def->flags = flags;
  2437.   def->section = section;
  2438.   *def_ptr = def;
  2439.   Q_enter_global_ref (def_ptr, name);
  2440.   return def;
  2441. }
  2442.  
  2443. void
  2444. DEFUN_VOID (lang_process)
  2445. {
  2446.  
  2447.   if (had_script == false)
  2448.     {
  2449.       parse_line (ldemul_get_script (), 1);
  2450.     }
  2451.   lang_reasonable_defaults ();
  2452.   current_target = default_target;
  2453.  
  2454.   lang_for_each_statement (ldlang_open_output);    /* Open the output file */
  2455.   /* For each output section statement, create a section in the output
  2456.      file */
  2457.   lang_create_output_section_statements ();
  2458.  
  2459.   /* Create a dummy bfd for the script */
  2460.   lang_init_script_file ();
  2461.  
  2462.   /* Add to the hash table all undefineds on the command line */
  2463.   lang_place_undefineds ();
  2464.  
  2465.   /* Create a bfd for each input file */
  2466.   current_target = default_target;
  2467.   lang_for_each_statement (open_input_bfds);
  2468.  
  2469.   /* Run through the contours of the script and attatch input sections
  2470.      to the correct output sections
  2471.      */
  2472.   find_constructors ();
  2473.   map_input_to_output_sections (statement_list.head, (char *) NULL,
  2474.                 (lang_output_section_statement_type *) NULL);
  2475.  
  2476.  
  2477.   /* Find any sections not attatched explicitly and handle them */
  2478.   lang_place_orphans ();
  2479.  
  2480.   /* Size up the common data */
  2481.   lang_common ();
  2482.  
  2483.   ldemul_before_allocation ();
  2484.  
  2485.  
  2486. #if 0
  2487.   had_relax = true;
  2488.   while (had_relax)
  2489.     {
  2490.  
  2491.       had_relax = false;
  2492.  
  2493.       lang_size_sections (statement_list.head,
  2494.               (lang_output_section_statement_type *) NULL,
  2495.               &(statement_list.head), 0, (bfd_vma) 0, true);
  2496.       /* FIXME. Until the code in relax is fixed so that it only reads in
  2497.          stuff once, we cant iterate since there is no way for the linker to
  2498.          know what has been patched and what hasn't */
  2499.       break;
  2500.  
  2501.     }
  2502. #endif
  2503.  
  2504.   /* Now run around and relax if we can */
  2505.   if (command_line.relax)
  2506.     {
  2507.       /* First time round is a trial run to get the 'worst case' addresses of the
  2508.          objects if there was no relaxing */
  2509.       lang_size_sections (statement_list.head,
  2510.               (lang_output_section_statement_type *) NULL,
  2511.               &(statement_list.head), 0, (bfd_vma) 0, false);
  2512.  
  2513.  
  2514.  
  2515.   /* Move the global symbols around so the second pass of relaxing can
  2516.      see them */
  2517.   lang_relocate_globals ();
  2518.  
  2519.   reset_memory_regions ();
  2520.  
  2521.   /* Do all the assignments, now that we know the final restingplaces
  2522.      of all the symbols */
  2523.  
  2524.   lang_do_assignments (statement_list.head,
  2525.                abs_output_section,
  2526.                0, (bfd_vma) 0);
  2527.  
  2528.  
  2529.       /* Perform another relax pass - this time we know where the
  2530.      globals are, so can make better guess */
  2531.       lang_size_sections (statement_list.head,
  2532.               (lang_output_section_statement_type *) NULL,
  2533.               &(statement_list.head), 0, (bfd_vma) 0, true);
  2534.  
  2535.  
  2536.  
  2537.     }
  2538.  
  2539.   else
  2540.     {
  2541.       /* Size up the sections */
  2542.       lang_size_sections (statement_list.head,
  2543.               abs_output_section,
  2544.               &(statement_list.head), 0, (bfd_vma) 0, false);
  2545.  
  2546.     }
  2547.  
  2548.  
  2549.   /* See if anything special should be done now we know how big
  2550.      everything is */
  2551.   ldemul_after_allocation ();
  2552.  
  2553.   /* Do all the assignments, now that we know the final restingplaces
  2554.      of all the symbols */
  2555.  
  2556.   lang_do_assignments (statement_list.head,
  2557.                abs_output_section,
  2558.                0, (bfd_vma) 0);
  2559.  
  2560.  
  2561.   /* Move the global symbols around */
  2562.   lang_relocate_globals ();
  2563.  
  2564.   /* Make sure that we're not mixing architectures */
  2565.  
  2566.   lang_check ();
  2567.  
  2568.   /* Final stuffs */
  2569.   lang_finish ();
  2570. }
  2571.  
  2572. /* EXPORTED TO YACC */
  2573.  
  2574. void
  2575. DEFUN (lang_add_wild, (section_name, filename),
  2576.        CONST char *CONST section_name AND
  2577.        CONST char *CONST filename)
  2578. {
  2579.   lang_wild_statement_type *new = new_stat (lang_wild_statement,
  2580.                         stat_ptr);
  2581.  
  2582.   if (section_name != (char *) NULL && strcmp (section_name, "COMMON") == 0)
  2583.     {
  2584.       placed_commons = true;
  2585.     }
  2586.   if (filename != (char *) NULL)
  2587.     {
  2588.       lang_has_input_file = true;
  2589.     }
  2590.   new->section_name = section_name;
  2591.   new->filename = filename;
  2592.   lang_list_init (&new->children);
  2593. }
  2594.  
  2595. void
  2596. DEFUN (lang_section_start, (name, address),
  2597.        CONST char *name AND
  2598.        etree_type * address)
  2599. {
  2600.   lang_address_statement_type *ad = new_stat (lang_address_statement, stat_ptr);
  2601.  
  2602.   ad->section_name = name;
  2603.   ad->address = address;
  2604. }
  2605.  
  2606. void
  2607. DEFUN (lang_add_entry, (name),
  2608.        CONST char *name)
  2609. {
  2610.   entry_symbol = name;
  2611. }
  2612.  
  2613. void
  2614. DEFUN (lang_add_target, (name),
  2615.        CONST char *name)
  2616. {
  2617.   lang_target_statement_type *new = new_stat (lang_target_statement,
  2618.                           stat_ptr);
  2619.  
  2620.   new->target = name;
  2621.  
  2622. }
  2623.  
  2624. void
  2625. DEFUN (lang_add_map, (name),
  2626.        CONST char *name)
  2627. {
  2628.   while (*name)
  2629.     {
  2630.       switch (*name)
  2631.     {
  2632.       case 'F':
  2633.       map_option_f = true;
  2634.       break;
  2635.     }
  2636.       name++;
  2637.     }
  2638. }
  2639.  
  2640. void
  2641. DEFUN (lang_add_fill, (exp),
  2642.        int exp)
  2643. {
  2644.   lang_fill_statement_type *new = new_stat (lang_fill_statement,
  2645.                         stat_ptr);
  2646.  
  2647.   new->fill = exp;
  2648. }
  2649.  
  2650. void
  2651. DEFUN (lang_add_data, (type, exp),
  2652.        int type AND
  2653.        union etree_union *exp)
  2654. {
  2655.  
  2656.   lang_data_statement_type *new = new_stat (lang_data_statement,
  2657.                         stat_ptr);
  2658.  
  2659.   new->exp = exp;
  2660.   new->type = type;
  2661.  
  2662. }
  2663.  
  2664. void
  2665. DEFUN (lang_add_assignment, (exp),
  2666.        etree_type * exp)
  2667. {
  2668.   lang_assignment_statement_type *new = new_stat (lang_assignment_statement,
  2669.                           stat_ptr);
  2670.  
  2671.   new->exp = exp;
  2672. }
  2673.  
  2674. void
  2675. DEFUN (lang_add_attribute, (attribute),
  2676.        enum statement_enum attribute)
  2677. {
  2678.   new_statement (attribute, sizeof (lang_statement_union_type), stat_ptr);
  2679. }
  2680.  
  2681. void
  2682. DEFUN (lang_startup, (name),
  2683.        CONST char *name)
  2684. {
  2685.   if (startup_file != (char *) NULL)
  2686.     {
  2687.       einfo ("%P%FMultiple STARTUP files\n");
  2688.     }
  2689.   first_file->filename = name;
  2690.   first_file->local_sym_name = name;
  2691.  
  2692.   startup_file = name;
  2693. }
  2694.  
  2695. void
  2696. DEFUN (lang_float, (maybe),
  2697.        boolean maybe)
  2698. {
  2699.   lang_float_flag = maybe;
  2700. }
  2701.  
  2702. void
  2703. DEFUN (lang_leave_output_section_statement, (fill, memspec),
  2704.        bfd_vma fill AND
  2705.        CONST char *memspec)
  2706. {
  2707.   current_section->fill = fill;
  2708.   current_section->region = lang_memory_region_lookup (memspec);
  2709.   stat_ptr = &statement_list;
  2710.  
  2711.   /* We remember if we are closing a .data section, since we use it to
  2712.      store constructors in */
  2713.   if (strcmp (current_section->name, ".data") == 0)
  2714.     {
  2715.       end_of_data_section_statement_list = statement_list;
  2716.  
  2717.     }
  2718. }
  2719.  
  2720. /*
  2721.  Create an absolute symbol with the given name with the value of the
  2722.  address of first byte of the section named.
  2723.  
  2724.  If the symbol already exists, then do nothing.
  2725. */
  2726. void
  2727. DEFUN (lang_abs_symbol_at_beginning_of, (section, name),
  2728.        CONST char *section AND
  2729.        CONST char *name)
  2730. {
  2731.   if (ldsym_undefined (name))
  2732.     {
  2733.       asection *s = bfd_get_section_by_name (output_bfd, section);
  2734.       asymbol *def = create_symbol (name,
  2735.                     BSF_GLOBAL | BSF_EXPORT,
  2736.                     &bfd_abs_section);
  2737.  
  2738.       if (s != (asection *) NULL)
  2739.     {
  2740.       def->value = s->vma;
  2741.     }
  2742.       else
  2743.     {
  2744.       def->value = 0;
  2745.     }
  2746.     }
  2747. }
  2748.  
  2749. /*
  2750.  Create an absolute symbol with the given name with the value of the
  2751.  address of the first byte after the end of the section named.
  2752.  
  2753.  If the symbol already exists, then do nothing.
  2754. */
  2755. void
  2756. DEFUN (lang_abs_symbol_at_end_of, (section, name),
  2757.        CONST char *section AND
  2758.        CONST char *name)
  2759. {
  2760.   if (ldsym_undefined (name))
  2761.     {
  2762.       asection *s = bfd_get_section_by_name (output_bfd, section);
  2763.  
  2764.       /* Add a symbol called _end */
  2765.       asymbol *def = create_symbol (name,
  2766.                     BSF_GLOBAL | BSF_EXPORT,
  2767.                     &bfd_abs_section);
  2768.  
  2769.       if (s != (asection *) NULL)
  2770.     {
  2771.       def->value = s->vma + s->_raw_size;
  2772.     }
  2773.       else
  2774.     {
  2775.       def->value = 0;
  2776.     }
  2777.     }
  2778. }
  2779.  
  2780. void
  2781. DEFUN (lang_statement_append, (list, element, field),
  2782.        lang_statement_list_type * list AND
  2783.        lang_statement_union_type * element AND
  2784.        lang_statement_union_type ** field)
  2785. {
  2786.   *(list->tail) = element;
  2787.   list->tail = field;
  2788. }
  2789.  
  2790. /* Set the output format type */
  2791. void
  2792. DEFUN (lang_add_output_format, (format),
  2793.        CONST char *format)
  2794. {
  2795.   output_target = format;
  2796. }
  2797.  
  2798.