Commit | Line | Data |
---|---|---|
3c3bdf30 NC |
1 | # This shell script emits a C file. -*- C -*- |
2 | # Copyright 2001 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 | . ${srcdir}/emultempl/mmix-elfnmmo.em | |
25 | ||
26 | cat >>e${EMULATION_NAME}.c <<EOF | |
27 | ||
28 | static boolean mmo_place_orphan | |
29 | PARAMS ((lang_input_statement_type *, asection *)); | |
30 | static asection *output_prev_sec_find | |
31 | PARAMS ((lang_output_section_statement_type *)); | |
32 | static void mmo_finish PARAMS ((void)); | |
33 | static void mmo_wipe_sec_reloc_flag PARAMS ((bfd *, asection *, PTR)); | |
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 (os) | |
40 | lang_output_section_statement_type *os; | |
41 | { | |
42 | asection *s = (asection *) NULL; | |
43 | lang_statement_union_type *u; | |
44 | lang_output_section_statement_type *lookup; | |
45 | ||
46 | for (u = lang_output_section_statement.head; | |
47 | u != (lang_statement_union_type *) NULL; | |
48 | u = lookup->next) | |
49 | { | |
50 | lookup = &u->output_section_statement; | |
51 | if (lookup == os) | |
52 | break; | |
53 | if (lookup->bfd_section != NULL | |
54 | && lookup->bfd_section != bfd_abs_section_ptr | |
55 | && lookup->bfd_section != bfd_com_section_ptr | |
56 | && lookup->bfd_section != bfd_und_section_ptr) | |
57 | s = lookup->bfd_section; | |
58 | } | |
59 | ||
60 | if (u == NULL) | |
61 | return NULL; | |
62 | ||
63 | return s; | |
64 | } | |
65 | ||
66 | struct orphan_save { | |
67 | lang_output_section_statement_type *os; | |
68 | asection **section; | |
69 | lang_statement_union_type **stmt; | |
70 | }; | |
71 | ||
72 | #define HAVE_SECTION(hold, name) \ | |
73 | (hold.os != NULL || (hold.os = lang_output_section_find (name)) != NULL) | |
74 | ||
75 | /* Place an orphan section. We use this to put random SEC_CODE or | |
76 | SEC_READONLY sections right after MMO_TEXT_SECTION_NAME. Much borrowed | |
77 | from elf32.em. */ | |
78 | ||
79 | static boolean | |
80 | mmo_place_orphan (file, s) | |
81 | lang_input_statement_type *file; | |
82 | asection *s; | |
83 | { | |
84 | static struct orphan_save hold_text; | |
85 | struct orphan_save *place; | |
86 | lang_output_section_statement_type *os; | |
87 | lang_statement_list_type *old; | |
88 | lang_statement_list_type add; | |
89 | asection *snew, **pps, *bfd_section; | |
90 | ||
91 | /* We have nothing to say for anything other than a final link. */ | |
92 | if (link_info.relocateable | |
93 | || (bfd_get_section_flags (s->owner, s) | |
94 | & (SEC_EXCLUDE | SEC_LOAD)) != SEC_LOAD) | |
95 | return false; | |
96 | ||
97 | /* Only care for sections we're going to load. */ | |
98 | os = lang_output_section_find (bfd_get_section_name (s->owner, s)); | |
99 | ||
100 | /* We have an output section by this name. Place the section inside it | |
101 | (regardless of whether the linker script lists it as input). */ | |
102 | if (os != NULL) | |
103 | { | |
104 | lang_add_section (&os->children, s, os, file); | |
105 | return true; | |
106 | } | |
107 | ||
108 | /* If this section does not have .text-type section flags or there's no | |
109 | MMO_TEXT_SECTION_NAME, we don't have anything to say. */ | |
110 | if ((bfd_get_section_flags (s->owner, s) & (SEC_CODE | SEC_READONLY)) == 0) | |
111 | return false; | |
112 | ||
113 | if (hold_text.os == NULL) | |
114 | hold_text.os = lang_output_section_find (MMO_TEXT_SECTION_NAME); | |
115 | ||
116 | place = &hold_text; | |
117 | ||
118 | /* If there's an output section by this name, we'll use it, regardless | |
119 | of section flags, in contrast to what's done in elf32.em. */ | |
120 | ||
121 | /* Start building a list of statements for this section. | |
122 | First save the current statement pointer. */ | |
123 | old = stat_ptr; | |
124 | ||
125 | /* Add the output section statements for this orphan to our own private | |
126 | list, inserting them later into the global statement list. */ | |
127 | stat_ptr = &add; | |
128 | lang_list_init (stat_ptr); | |
129 | ||
130 | os = lang_enter_output_section_statement (bfd_get_section_name (s->owner, | |
131 | s), | |
132 | NULL, 0, | |
133 | (bfd_vma) 0, | |
134 | (etree_type *) NULL, | |
135 | (etree_type *) NULL, | |
136 | (etree_type *) NULL); | |
137 | ||
138 | lang_add_section (&os->children, s, os, file); | |
139 | ||
140 | lang_leave_output_section_statement | |
141 | ((bfd_vma) 0, "*default*", | |
142 | (struct lang_output_section_phdr_list *) NULL, "*default*"); | |
143 | ||
144 | /* Restore the global list pointer. */ | |
145 | stat_ptr = old; | |
146 | ||
147 | snew = os->bfd_section; | |
148 | bfd_section = place->os->bfd_section; | |
149 | if (place->section == NULL && bfd_section == NULL) | |
150 | bfd_section = output_prev_sec_find (place->os); | |
151 | ||
152 | if (place->section != NULL | |
153 | || (bfd_section != NULL | |
154 | && bfd_section != snew)) | |
155 | { | |
156 | /* Shuffle the section to make the output file look neater. This is | |
157 | really only cosmetic. */ | |
158 | if (place->section == NULL) | |
159 | /* Put orphans after the first section on the list. */ | |
160 | place->section = &bfd_section->next; | |
161 | ||
3dfe7a8c | 162 | /* Unlink the section. */ |
3c3bdf30 NC |
163 | for (pps = &output_bfd->sections; *pps != snew; pps = &(*pps)->next) |
164 | ; | |
3dfe7a8c | 165 | bfd_section_list_remove (output_bfd, pps); |
3c3bdf30 NC |
166 | |
167 | /* Now tack it on to the "place->os" section list. */ | |
3dfe7a8c | 168 | bfd_section_list_insert (output_bfd, place->section, snew); |
3c3bdf30 NC |
169 | } |
170 | place->section = &snew->next; /* Save the end of this list. */ | |
171 | ||
172 | if (add.head != NULL) | |
173 | { | |
174 | /* We try to put the output statements in some sort of reasonable | |
175 | order here, because they determine the final load addresses of | |
176 | the orphan sections. */ | |
177 | if (place->stmt == NULL) | |
178 | { | |
179 | /* Put the new statement list right at the head. */ | |
180 | *add.tail = place->os->header.next; | |
181 | place->os->header.next = add.head; | |
182 | } | |
183 | else | |
184 | { | |
185 | /* Put it after the last orphan statement we added. */ | |
186 | *add.tail = *place->stmt; | |
187 | *place->stmt = add.head; | |
188 | } | |
189 | ||
190 | /* Fix the global list pointer if we happened to tack our new list | |
191 | at the tail. */ | |
192 | if (*old->tail == add.head) | |
193 | old->tail = add.tail; | |
194 | ||
195 | /* Save the end of this list. */ | |
196 | place->stmt = add.tail; | |
197 | } | |
198 | ||
199 | return true; | |
200 | } | |
201 | ||
202 | /* Remove the spurious settings of SEC_RELOC that make it to the output at | |
203 | link time. We are as confused as elflink.h:elf_bfd_final_link, and | |
204 | paper over the bug similarly. */ | |
205 | ||
206 | static void | |
207 | mmo_wipe_sec_reloc_flag (abfd, sec, ptr) | |
208 | bfd *abfd; | |
209 | asection *sec; | |
210 | PTR ptr ATTRIBUTE_UNUSED; | |
211 | { | |
212 | bfd_set_section_flags (abfd, sec, | |
213 | bfd_get_section_flags (abfd, sec) & ~SEC_RELOC); | |
214 | } | |
215 | ||
216 | /* Iterate with bfd_map_over_sections over mmo_wipe_sec_reloc_flag... */ | |
217 | ||
218 | static void | |
219 | mmo_finish () | |
220 | { | |
221 | bfd_map_over_sections (output_bfd, mmo_wipe_sec_reloc_flag, NULL); | |
222 | } | |
223 | ||
224 | EOF | |
225 | ||
226 | LDEMUL_PLACE_ORPHAN=mmo_place_orphan | |
227 | LDEMUL_FINISH=mmo_finish |