OSDN Git Service

* ldgram.y (sect_constraint): New.
[pf3gnuchains/pf3gnuchains3x.git] / ld / emultempl / mmo.em
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 2001, 2002, 2003, 2004 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->constraint == -1)
51         continue;
52       if (lookup == os)
53         break;
54       if (lookup->bfd_section != NULL
55           && lookup->bfd_section != bfd_abs_section_ptr
56           && lookup->bfd_section != bfd_com_section_ptr
57           && lookup->bfd_section != bfd_und_section_ptr)
58         s = lookup->bfd_section;
59     }
60
61   if (u == NULL)
62     return NULL;
63
64   return s;
65 }
66
67 struct orphan_save {
68   lang_output_section_statement_type *os;
69   asection **section;
70   lang_statement_union_type **stmt;
71 };
72
73 #define HAVE_SECTION(hold, name) \
74 (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL)
75
76 /* Place an orphan section.  We use this to put random SEC_CODE or
77    SEC_READONLY sections right after MMO_TEXT_SECTION_NAME.  Much borrowed
78    from elf32.em.  */
79
80 static bfd_boolean
81 mmo_place_orphan (lang_input_statement_type *file, asection *s)
82 {
83   static struct orphan_save hold_text;
84   struct orphan_save *place;
85   lang_output_section_statement_type *os;
86   lang_statement_list_type *old;
87   lang_statement_list_type add;
88   asection *snew, **pps, *bfd_section;
89
90   /* We have nothing to say for anything other than a final link.  */
91   if (link_info.relocatable
92       || (bfd_get_section_flags (s->owner, s)
93           & (SEC_EXCLUDE | SEC_LOAD)) != SEC_LOAD)
94     return FALSE;
95
96   /* Only care for sections we're going to load.  */
97   os = lang_output_section_find (bfd_get_section_name (s->owner, s));
98
99   /* We have an output section by this name.  Place the section inside it
100      (regardless of whether the linker script lists it as input).  */
101   if (os != NULL)
102     {
103       lang_add_section (&os->children, s, os, file);
104       return TRUE;
105     }
106
107   /* If this section does not have .text-type section flags or there's no
108      MMO_TEXT_SECTION_NAME, we don't have anything to say.  */
109   if ((bfd_get_section_flags (s->owner, s) & (SEC_CODE | SEC_READONLY)) == 0)
110     return FALSE;
111
112   if (hold_text.os == NULL)
113     hold_text.os = lang_output_section_find (MMO_TEXT_SECTION_NAME);
114
115   place = &hold_text;
116
117   /* If there's an output section by this name, we'll use it, regardless
118      of section flags, in contrast to what's done in elf32.em.  */
119
120   /* Start building a list of statements for this section.
121      First save the current statement pointer.  */
122   old = stat_ptr;
123
124   /* Add the output section statements for this orphan to our own private
125      list, inserting them later into the global statement list.  */
126   stat_ptr = &add;
127   lang_list_init (stat_ptr);
128
129   os = lang_enter_output_section_statement (bfd_get_section_name (s->owner,
130                                                                   s),
131                                             NULL, 0,
132                                             (etree_type *) NULL,
133                                             (etree_type *) NULL,
134                                             (etree_type *) NULL, 0);
135
136   lang_add_section (&os->children, s, os, file);
137
138   lang_leave_output_section_statement
139     ((bfd_vma) 0, "*default*",
140      (struct lang_output_section_phdr_list *) NULL, NULL);
141
142   /* Restore the global list pointer.  */
143   stat_ptr = old;
144
145   snew = os->bfd_section;
146   if (snew == NULL)
147     /* /DISCARD/ section.  */
148     return TRUE;
149
150   /* We need an output section for .text as a root, so if there was none
151      (might happen with a peculiar linker script such as in "map
152      addresses", map-address.exp), we grab the output section created
153      above.  */
154   if (hold_text.os == NULL)
155     {
156       if (os == NULL)
157         return FALSE;
158       hold_text.os = os;
159     }
160
161   bfd_section = place->os->bfd_section;
162   if (place->section == NULL && bfd_section == NULL)
163     bfd_section = output_prev_sec_find (place->os);
164
165   if (place->section != NULL
166       || (bfd_section != NULL
167           && bfd_section != snew))
168     {
169       /* Shuffle the section to make the output file look neater.  This is
170          really only cosmetic.  */
171       if (place->section == NULL)
172         /* Put orphans after the first section on the list.  */
173         place->section = &bfd_section->next;
174
175       /* Unlink the section.  */
176       for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next)
177         ;
178       bfd_section_list_remove (output_bfd, pps);
179
180       /* Now tack it on to the "place->os" section list.  */
181       bfd_section_list_insert (output_bfd, place->section, snew);
182     }
183   place->section = &snew->next; /* Save the end of this list.  */
184
185   if (add.head != NULL)
186     {
187       /* We try to put the output statements in some sort of reasonable
188          order here, because they determine the final load addresses of
189          the orphan sections.  */
190       if (place->stmt == NULL)
191         {
192           /* Put the new statement list right at the head.  */
193           *add.tail = place->os->header.next;
194           place->os->header.next = add.head;
195         }
196       else
197         {
198           /* Put it after the last orphan statement we added.  */
199           *add.tail = *place->stmt;
200           *place->stmt = add.head;
201         }
202
203       /* Fix the global list pointer if we happened to tack our new list
204          at the tail.  */
205       if (*old->tail == add.head)
206         old->tail = add.tail;
207
208       /* Save the end of this list.  */
209       place->stmt = add.tail;
210     }
211
212   return TRUE;
213 }
214
215 /* Remove the spurious settings of SEC_RELOC that make it to the output at
216    link time.  We are as confused as elflink.h:elf_bfd_final_link, and
217    paper over the bug similarly.  */
218
219 static void
220 mmo_wipe_sec_reloc_flag (bfd *abfd, asection *sec, void *ptr ATTRIBUTE_UNUSED)
221 {
222   bfd_set_section_flags (abfd, sec,
223                          bfd_get_section_flags (abfd, sec) & ~SEC_RELOC);
224 }
225
226 /* Iterate with bfd_map_over_sections over mmo_wipe_sec_reloc_flag... */
227
228 static void
229 mmo_finish (void)
230 {
231   bfd_map_over_sections (output_bfd, mmo_wipe_sec_reloc_flag, NULL);
232 }
233 \f
234 /* To get on-demand global register allocation right, we need to parse the
235    relocs, like what happens when linking to ELF.  It needs to be done
236    before all input sections are supposed to be present.  When linking to
237    ELF, it's done when reading symbols.  When linking to mmo, we do it
238    when all input files are seen, which is equivalent.  */
239
240 static void
241 mmo_after_open (void)
242 {
243   /* When there's a mismatch between the output format and the emulation
244      (using weird combinations like "-m mmo --oformat elf64-mmix" for
245      example), we'd count relocs twice because they'd also be counted
246      along the usual route for ELF-only linking, which would lead to an
247      internal accounting error.  */
248   if (bfd_get_flavour (output_bfd) != bfd_target_elf_flavour)
249     {
250       LANG_FOR_EACH_INPUT_STATEMENT (is)
251         {
252           if (bfd_get_flavour (is->the_bfd) == bfd_target_elf_flavour
253               && !_bfd_mmix_check_all_relocs (is->the_bfd, &link_info))
254             einfo ("%X%P: Internal problems scanning %B after opening it",
255                    is->the_bfd);
256         }
257     }
258 }
259 EOF
260
261 LDEMUL_PLACE_ORPHAN=mmo_place_orphan
262 LDEMUL_FINISH=mmo_finish
263 LDEMUL_AFTER_OPEN=mmo_after_open