* emultempl/aix.em: Convert to C90, remove unnecessary prototypes
[external/binutils.git] / ld / emultempl / mmo.em
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
3 #
4 # This file is part of GLD, the Gnu Linker.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #
20
21 # This file is sourced from elf32.em and mmo.em, used to define
22 # linker MMIX-specifics common to ELF and MMO.
23
24 cat >>e${EMULATION_NAME}.c <<EOF
25 /* Need to have this define before mmix-elfnmmo, which includes
26    needrelax.em which uses this name for the before_allocation function,
27    normally defined in elf32.em.  */
28 #define gldmmo_before_allocation before_allocation_default
29 EOF
30
31 . ${srcdir}/emultempl/mmix-elfnmmo.em
32
33 cat >>e${EMULATION_NAME}.c <<EOF
34
35 /* Find the last output section before given output statement.
36    Used by place_orphan.  */
37
38 static asection *
39 output_prev_sec_find (lang_output_section_statement_type *os)
40 {
41   asection *s = NULL;
42   lang_statement_union_type *u;
43   lang_output_section_statement_type *lookup;
44
45   for (u = lang_output_section_statement.head;
46        u != (lang_statement_union_type *) NULL;
47        u = lookup->next)
48     {
49       lookup = &u->output_section_statement;
50       if (lookup == os)
51         break;
52       if (lookup->bfd_section != NULL
53           && lookup->bfd_section != bfd_abs_section_ptr
54           && lookup->bfd_section != bfd_com_section_ptr
55           && lookup->bfd_section != bfd_und_section_ptr)
56         s = lookup->bfd_section;
57     }
58
59   if (u == NULL)
60     return NULL;
61
62   return s;
63 }
64
65 struct orphan_save {
66   lang_output_section_statement_type *os;
67   asection **section;
68   lang_statement_union_type **stmt;
69 };
70
71 #define HAVE_SECTION(hold, name) \
72 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
73
74 /* Place an orphan section.  We use this to put random SEC_CODE or
75    SEC_READONLY sections right after MMO_TEXT_SECTION_NAME.  Much borrowed
76    from elf32.em.  */
77
78 static bfd_boolean
79 mmo_place_orphan (lang_input_statement_type *file, asection *s)
80 {
81   static struct orphan_save hold_text;
82   struct orphan_save *place;
83   lang_output_section_statement_type *os;
84   lang_statement_list_type *old;
85   lang_statement_list_type add;
86   asection *snew, **pps, *bfd_section;
87
88   /* We have nothing to say for anything other than a final link.  */
89   if (link_info.relocatable
90       || (bfd_get_section_flags (s->owner, s)
91           & (SEC_EXCLUDE | SEC_LOAD)) != SEC_LOAD)
92     return FALSE;
93
94   /* Only care for sections we're going to load.  */
95   os = lang_output_section_find (bfd_get_section_name (s->owner, s));
96
97   /* We have an output section by this name.  Place the section inside it
98      (regardless of whether the linker script lists it as input).  */
99   if (os != NULL)
100     {
101       lang_add_section (&os->children, s, os, file);
102       return TRUE;
103     }
104
105   /* If this section does not have .text-type section flags or there's no
106      MMO_TEXT_SECTION_NAME, we don't have anything to say.  */
107   if ((bfd_get_section_flags (s->owner, s) & (SEC_CODE | SEC_READONLY)) == 0)
108     return FALSE;
109
110   if (hold_text.os == NULL)
111     hold_text.os = lang_output_section_find (MMO_TEXT_SECTION_NAME);
112
113   place = &hold_text;
114
115   /* If there's an output section by this name, we'll use it, regardless
116      of section flags, in contrast to what's done in elf32.em.  */
117
118   /* Start building a list of statements for this section.
119      First save the current statement pointer.  */
120   old = stat_ptr;
121
122   /* Add the output section statements for this orphan to our own private
123      list, inserting them later into the global statement list.  */
124   stat_ptr = &add;
125   lang_list_init (stat_ptr);
126
127   os = lang_enter_output_section_statement (bfd_get_section_name (s->owner,
128                                                                   s),
129                                             NULL, 0,
130                                             (bfd_vma) 0,
131                                             (etree_type *) NULL,
132                                             (etree_type *) NULL,
133                                             (etree_type *) NULL);
134
135   lang_add_section (&os->children, s, os, file);
136
137   lang_leave_output_section_statement
138     ((bfd_vma) 0, "*default*",
139      (struct lang_output_section_phdr_list *) NULL, NULL);
140
141   /* Restore the global list pointer.  */
142   stat_ptr = old;
143
144   snew = os->bfd_section;
145   if (snew == NULL)
146     /* /DISCARD/ section.  */
147     return TRUE;
148
149   /* We need an output section for .text as a root, so if there was none
150      (might happen with a peculiar linker script such as in "map
151      addresses", map-address.exp), we grab the output section created
152      above.  */
153   if (hold_text.os == NULL)
154     {
155       if (os == NULL)
156         return FALSE;
157       hold_text.os = os;
158     }
159
160   bfd_section = place->os->bfd_section;
161   if (place->section == NULL && bfd_section == NULL)
162     bfd_section = output_prev_sec_find (place->os);
163
164   if (place->section != NULL
165       || (bfd_section != NULL
166           && bfd_section != snew))
167     {
168       /* Shuffle the section to make the output file look neater.  This is
169          really only cosmetic.  */
170       if (place->section == NULL)
171         /* Put orphans after the first section on the list.  */
172         place->section = &bfd_section->next;
173
174       /* Unlink the section.  */
175       for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
176         ;
177       bfd_section_list_remove (output_bfd, pps);
178
179       /* Now tack it on to the "place->os" section list.  */
180       bfd_section_list_insert (output_bfd, place->section, snew);
181     }
182   place->section = &snew->next; /* Save the end of this list.  */
183
184   if (add.head != NULL)
185     {
186       /* We try to put the output statements in some sort of reasonable
187          order here, because they determine the final load addresses of
188          the orphan sections.  */
189       if (place->stmt == NULL)
190         {
191           /* Put the new statement list right at the head.  */
192           *add.tail = place->os->header.next;
193           place->os->header.next = add.head;
194         }
195       else
196         {
197           /* Put it after the last orphan statement we added.  */
198           *add.tail = *place->stmt;
199           *place->stmt = add.head;
200         }
201
202       /* Fix the global list pointer if we happened to tack our new list
203          at the tail.  */
204       if (*old->tail == add.head)
205         old->tail = add.tail;
206
207       /* Save the end of this list.  */
208       place->stmt = add.tail;
209     }
210
211   return TRUE;
212 }
213
214 /* Remove the spurious settings of SEC_RELOC that make it to the output at
215    link time.  We are as confused as elflink.h:elf_bfd_final_link, and
216    paper over the bug similarly.  */
217
218 static void
219 mmo_wipe_sec_reloc_flag (bfd *abfd, asection *sec, void *ptr ATTRIBUTE_UNUSED)
220 {
221   bfd_set_section_flags (abfd, sec,
222                          bfd_get_section_flags (abfd, sec) & ~SEC_RELOC);
223 }
224
225 /* Iterate with bfd_map_over_sections over mmo_wipe_sec_reloc_flag... */
226
227 static void
228 mmo_finish (void)
229 {
230   bfd_map_over_sections (output_bfd, mmo_wipe_sec_reloc_flag, NULL);
231 }
232 \f
233 /* To get on-demand global register allocation right, we need to parse the
234    relocs, like what happens when linking to ELF.  It needs to be done
235    before all input sections are supposed to be present.  When linking to
236    ELF, it's done when reading symbols.  When linking to mmo, we do it
237    when all input files are seen, which is equivalent.  */
238
239 static void
240 mmo_after_open (void)
241 {
242   /* When there's a mismatch between the output format and the emulation
243      (using weird combinations like "-m mmo --oformat elf64-mmix" for
244      example), we'd count relocs twice because they'd also be counted
245      along the usual route for ELF-only linking, which would lead to an
246      internal accounting error.  */
247   if (bfd_get_flavour (output_bfd) != bfd_target_elf_flavour)
248     {
249       LANG_FOR_EACH_INPUT_STATEMENT (is)
250         {
251           if (bfd_get_flavour (is->the_bfd) == bfd_target_elf_flavour
252               && !_bfd_mmix_check_all_relocs (is->the_bfd, &link_info))
253             einfo ("%X%P: Internal problems scanning %B after opening it",
254                    is->the_bfd);
255         }
256     }
257 }
258 EOF
259
260 LDEMUL_PLACE_ORPHAN=mmo_place_orphan
261 LDEMUL_FINISH=mmo_finish
262 LDEMUL_AFTER_OPEN=mmo_after_open