]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/scripttempl/elf_chaos.sc
2.41 Release sources
[thirdparty/binutils-gdb.git] / ld / scripttempl / elf_chaos.sc
1 # Copyright (C) 2014-2023 Free Software Foundation, Inc.
2 #
3 # Copying and distribution of this file, with or without modification,
4 # are permitted in any medium without royalty provided the copyright
5 # notice and this notice are preserved.
6 #
7 # Unusual variables checked by this code:
8 # NOP - four byte opcode for no-op (defaults to 0)
9 # NO_SMALL_DATA - no .sbss/.sbss2/.sdata/.sdata2 sections if not
10 # empty.
11 # DATA_ADDR - if end-of-text-plus-one-page isn't right for data start
12 # INITIAL_READONLY_SECTIONS - at start of text segment
13 # OTHER_READONLY_SECTIONS - other than .text .init .rodata ...
14 # (e.g., .PARISC.milli)
15 # OTHER_TEXT_SECTIONS - these get put in .text when relocating
16 # OTHER_READWRITE_SECTIONS - other than .data .bss .ctors .sdata ...
17 # (e.g., .PARISC.global)
18 # ATTRS_SECTIONS - at the end
19 # OTHER_SECTIONS - at the end
20 # EXECUTABLE_SYMBOLS - symbols that must be defined for an
21 # executable (e.g., _DYNAMIC_LINK)
22 # TEXT_START_SYMBOLS - symbols that appear at the start of the
23 # .text section.
24 # DATA_START_SYMBOLS - symbols that appear at the start of the
25 # .data section.
26 # OTHER_GOT_SYMBOLS - symbols defined just before .got.
27 # OTHER_GOT_SECTIONS - sections just after .got.
28 # OTHER_SDATA_SECTIONS - sections just after .sdata.
29 # OTHER_BSS_SYMBOLS - symbols that appear at the start of the
30 # .bss section besides __bss_start.
31 # DATA_PLT - .plt should be in data segment, not text segment.
32 # BSS_PLT - .plt should be in bss segment
33 # TEXT_DYNAMIC - .dynamic in text segment, not data segment.
34 # EMBEDDED - whether this is for an embedded system.
35 # SHLIB_TEXT_START_ADDR - if set, add to SIZEOF_HEADERS to set
36 # start address of shared library.
37 # INPUT_FILES - INPUT command of files to always include
38 # WRITABLE_RODATA - if set, the .rodata section should be writable
39 # INIT_START, INIT_END - statements just before and just after
40 # combination of .init sections.
41 # FINI_START, FINI_END - statements just before and just after
42 # combination of .fini sections.
43 # STACK_ADDR - start of a .stack section.
44 # OTHER_SYMBOLS - symbols to place right at the end of the script.
45 #
46 # When adding sections, do note that the names of some sections are used
47 # when specifying the start address of the next.
48 #
49
50 # Many sections come in three flavours. There is the 'real' section,
51 # like ".data". Then there are the per-procedure or per-variable
52 # sections, generated by -ffunction-sections and -fdata-sections in GCC,
53 # and useful for --gc-sections, which for a variable "foo" might be
54 # ".data.foo". Then there are the linkonce sections, for which the linker
55 # eliminates duplicates, which are named like ".gnu.linkonce.d.foo".
56 # The exact correspondences are:
57 #
58 # Section Linkonce section
59 # .text .gnu.linkonce.t.foo
60 # .rodata .gnu.linkonce.r.foo
61 # .data .gnu.linkonce.d.foo
62 # .bss .gnu.linkonce.b.foo
63 # .sdata .gnu.linkonce.s.foo
64 # .sbss .gnu.linkonce.sb.foo
65 # .sdata2 .gnu.linkonce.s2.foo
66 # .sbss2 .gnu.linkonce.sb2.foo
67 # .debug_info .gnu.linkonce.wi.foo
68 #
69 # Each of these can also have corresponding .rel.* and .rela.* sections.
70
71 test -z "$ENTRY" && ENTRY=_start
72 test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
73 test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
74 if [ -z "$MACHINE" ]; then OUTPUT_ARCH=${ARCH}; else OUTPUT_ARCH=${ARCH}:${MACHINE}; fi
75 test -z "${ELFSIZE}" && ELFSIZE=32
76 test -z "${ALIGNMENT}" && ALIGNMENT="${ELFSIZE} / 8"
77 test -z "$ATTRS_SECTIONS" && ATTRS_SECTIONS=".gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }"
78 test "$LD_FLAG" = "N" && DATA_ADDR=.
79 INTERP=".interp ${RELOCATING-0} : { *(.interp) }"
80 PLT=".plt ${RELOCATING-0} : { *(.plt) }"
81 DYNAMIC=".dynamic ${RELOCATING-0} : { *(.dynamic) }"
82 RODATA=".rodata ${RELOCATING-0} : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
83 if test -z "${NO_SMALL_DATA}"; then
84 SBSS=".sbss ${RELOCATING-0} :
85 {
86 ${RELOCATING+PROVIDE (__sbss_start = .);}
87 ${RELOCATING+PROVIDE (___sbss_start = .);}
88 *(.dynsbss)
89 *(.sbss${RELOCATING+ .sbss.* .gnu.linkonce.sb.*})
90 *(.scommon)
91 ${RELOCATING+PROVIDE (__sbss_end = .);}
92 ${RELOCATING+PROVIDE (___sbss_end = .);}
93 }"
94 SBSS2=".sbss2 ${RELOCATING-0} : { *(.sbss2${RELOCATING+ .sbss2.* .gnu.linkonce.sb2.*}) }"
95 SDATA="/* We want the small data sections together, so single-instruction offsets
96 can access them all, and initialized data all before uninitialized, so
97 we can shorten the on-disk segment size. */
98 .sdata ${RELOCATING-0} :
99 {
100 ${RELOCATING+${SDATA_START_SYMBOLS}}
101 *(.sdata${RELOCATING+ .sdata.* .gnu.linkonce.s.*})
102 }"
103 SDATA2=".sdata2 ${RELOCATING-0} : { *(.sdata2${RELOCATING+ .sdata2.* .gnu.linkonce.s2.*}) }"
104 REL_SDATA=".rel.sdata ${RELOCATING-0} : { *(.rel.sdata${RELOCATING+ .rel.sdata.* .rel.gnu.linkonce.s.*}) }
105 .rela.sdata ${RELOCATING-0} : { *(.rela.sdata${RELOCATING+ .rela.sdata.* .rela.gnu.linkonce.s.*}) }"
106 REL_SBSS=".rel.sbss ${RELOCATING-0} : { *(.rel.sbss${RELOCATING+ .rel.sbss.* .rel.gnu.linkonce.sb.*}) }
107 .rela.sbss ${RELOCATING-0} : { *(.rela.sbss${RELOCATING+ .rela.sbss.* .rela.gnu.linkonce.sb.*}) }"
108 REL_SDATA2=".rel.sdata2 ${RELOCATING-0} : { *(.rel.sdata2${RELOCATING+ .rel.sdata2.* .rel.gnu.linkonce.s2.*}) }
109 .rela.sdata2 ${RELOCATING-0} : { *(.rela.sdata2${RELOCATING+ .rela.sdata2.* .rela.gnu.linkonce.s2.*}) }"
110 REL_SBSS2=".rel.sbss2 ${RELOCATING-0} : { *(.rel.sbss2${RELOCATING+ .rel.sbss2.* .rel.gnu.linkonce.sb2.*}) }
111 .rela.sbss2 ${RELOCATING-0} : { *(.rela.sbss2${RELOCATING+ .rela.sbss2.* .rela.gnu.linkonce.sb2.*}) }"
112 fi
113 CTOR="
114 ${CONSTRUCTING+${CTOR_START}}
115 /* gcc uses crtbegin.o to find the start of
116 the constructors, so we make sure it is
117 first. Because this is a wildcard, it
118 doesn't matter if the user does not
119 actually link against crtbegin.o; the
120 linker won't look for a file to match a
121 wildcard. The wildcard also means that it
122 doesn't matter which directory crtbegin.o
123 is in. */
124
125 KEEP (*crtbegin.o(.ctors))
126 KEEP (*crtbegin?.o(.ctors))
127
128 /* We don't want to include the .ctor section from
129 the crtend.o file until after the sorted ctors.
130 The .ctor section from the crtend file contains the
131 end of ctors marker and it must be last */
132
133 KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors))
134 KEEP (*(SORT(.ctors.*)))
135 KEEP (*(.ctors))
136 ${CONSTRUCTING+${CTOR_END}}
137 "
138 DTOR="
139 ${CONSTRUCTING+${DTOR_START}}
140 KEEP (*crtbegin.o(.dtors))
141 KEEP (*crtbegin?.o(.dtors))
142 KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors))
143 KEEP (*(SORT(.dtors.*)))
144 KEEP (*(.dtors))
145 ${CONSTRUCTING+${DTOR_END}}
146 "
147 STACK=" .stack ${RELOCATING-0}${RELOCATING+${STACK_ADDR}} :
148 {
149 ${RELOCATING+_stack = .;}
150 *(.stack)
151 }"
152
153 test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}"
154
155 cat <<EOF
156 /* Copyright (C) 2014-2023 Free Software Foundation, Inc.
157
158 Copying and distribution of this script, with or without modification,
159 are permitted in any medium without royalty provided the copyright
160 notice and this notice are preserved. */
161
162 OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
163 "${LITTLE_OUTPUT_FORMAT}")
164 OUTPUT_ARCH(${OUTPUT_ARCH})
165 ${RELOCATING+ENTRY(${ENTRY})}
166
167 ${RELOCATING+${LIB_SEARCH_DIRS}}
168 ${RELOCATING+/* Do we need any of these for elf?
169 __DYNAMIC = 0; ${STACKZERO+${STACKZERO}} ${SHLIB_PATH+${SHLIB_PATH}} */}
170 ${RELOCATING+${EXECUTABLE_SYMBOLS}}
171 ${RELOCATING+${INPUT_FILES}}
172 ${RELOCATING- /* For some reason, the Solaris linker makes bad executables
173 if gld -r is used and the intermediate file has sections starting
174 at non-zero addresses. Could be a Solaris ld bug, could be a GNU ld
175 bug. But for now assigning the zero vmas works. */}
176
177 SECTIONS
178 {
179 /* Read-only sections, merged into text segment: */
180 ${CREATE_SHLIB-${RELOCATING+. = ${TEXT_BASE_ADDRESS};}}
181 ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR:-0};}}
182 ${CREATE_SHLIB-${INTERP}}
183 ${INITIAL_READONLY_SECTIONS}
184 ${TEXT_DYNAMIC+${DYNAMIC}}
185 .hash ${RELOCATING-0} : { *(.hash) }
186 .dynsym ${RELOCATING-0} : { *(.dynsym) }
187 .dynstr ${RELOCATING-0} : { *(.dynstr) }
188 .gnu.version ${RELOCATING-0} : { *(.gnu.version) }
189 .gnu.version_d ${RELOCATING-0}: { *(.gnu.version_d) }
190 .gnu.version_r ${RELOCATING-0}: { *(.gnu.version_r) }
191
192 EOF
193 if [ "x$COMBRELOC" = x ]; then
194 COMBRELOCCAT=cat
195 else
196 COMBRELOCCAT="cat > $COMBRELOC"
197 fi
198 eval $COMBRELOCCAT <<EOF
199 .rel.init ${RELOCATING-0} : { *(.rel.init) }
200 .rela.init ${RELOCATING-0} : { *(.rela.init) }
201 .rel.text ${RELOCATING-0} : { *(.rel.text${RELOCATING+ .rel.text.* .rel.gnu.linkonce.t.*}) }
202 .rela.text ${RELOCATING-0} : { *(.rela.text${RELOCATING+ .rela.text.* .rela.gnu.linkonce.t.*}) }
203 .rel.fini ${RELOCATING-0} : { *(.rel.fini) }
204 .rela.fini ${RELOCATING-0} : { *(.rela.fini) }
205 .rel.rodata ${RELOCATING-0} : { *(.rel.rodata${RELOCATING+ .rel.rodata.* .rel.gnu.linkonce.r.*}) }
206 .rela.rodata ${RELOCATING-0} : { *(.rela.rodata${RELOCATING+ .rela.rodata.* .rela.gnu.linkonce.r.*}) }
207 ${OTHER_READONLY_RELOC_SECTIONS}
208 .rel.data ${RELOCATING-0} : { *(.rel.data${RELOCATING+ .rel.data.* .rel.gnu.linkonce.d.*}) }
209 .rela.data ${RELOCATING-0} : { *(.rela.data${RELOCATING+ .rela.data.* .rela.gnu.linkonce.d.*}) }
210 .rel.ctors ${RELOCATING-0} : { *(.rel.ctors) }
211 .rela.ctors ${RELOCATING-0} : { *(.rela.ctors) }
212 .rel.dtors ${RELOCATING-0} : { *(.rel.dtors) }
213 .rela.dtors ${RELOCATING-0} : { *(.rela.dtors) }
214 .rel.got ${RELOCATING-0} : { *(.rel.got) }
215 .rela.got ${RELOCATING-0} : { *(.rela.got) }
216 ${OTHER_GOT_RELOC_SECTIONS}
217 ${REL_SDATA}
218 ${REL_SBSS}
219 ${REL_SDATA2}
220 ${REL_SBSS2}
221 .rel.bss ${RELOCATING-0} : { *(.rel.bss${RELOCATING+ .rel.bss.* .rel.gnu.linkonce.b.*}) }
222 .rela.bss ${RELOCATING-0} : { *(.rela.bss${RELOCATING+ .rela.bss.* .rela.gnu.linkonce.b.*}) }
223 EOF
224 if [ -n "$COMBRELOC" ]; then
225 cat <<EOF
226 .rel.dyn ${RELOCATING-0} :
227 {
228 EOF
229 sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rela\./d;s/^.*: { *\(.*\)}$/ \1/' $COMBRELOC
230 cat <<EOF
231 }
232 .rela.dyn ${RELOCATING-0} :
233 {
234 EOF
235 sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rel\./d;s/^.*: { *\(.*\)}/ \1/' $COMBRELOC
236 cat <<EOF
237 }
238 EOF
239 fi
240 cat <<EOF
241 ${RELOCATING+. = ALIGN(0x1000);}
242 .rel.plt ${RELOCATING-0} : { *(.rel.plt) }
243 .rela.plt ${RELOCATING-0} : { *(.rela.plt) }
244 ${OTHER_PLT_RELOC_SECTIONS}
245 ${DATA_PLT-${BSS_PLT-${PLT}}}
246 .text ${RELOCATING-0} :
247 {
248 ${RELOCATING+${TEXT_START_SYMBOLS}}
249 *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
250 /* .gnu.warning sections are handled specially by elf.em. */
251 *(.gnu.warning)
252 ${RELOCATING+${OTHER_TEXT_SECTIONS}}
253 } =${NOP-0}
254 .init ${RELOCATING-0} :
255 {
256 ${RELOCATING+${INIT_START}}
257 KEEP (*(SORT_NONE(.init)))
258 ${RELOCATING+${INIT_END}}
259 } =${NOP-0}
260 .fini ${RELOCATING-0} :
261 {
262 ${RELOCATING+${FINI_START}}
263 KEEP (*(SORT_NONE(.fini)))
264 ${RELOCATING+${FINI_END}}
265 } =${NOP-0}
266 ${RELOCATING+PROVIDE (__etext = .);}
267 ${RELOCATING+PROVIDE (_etext = .);}
268 ${RELOCATING+PROVIDE (etext = .);}
269 ${RELOCATING+. = ALIGN(0x1000);}
270 ${CREATE_SHLIB-${SDATA2}}
271 ${CREATE_SHLIB-${SBSS2}}
272 ${OTHER_READONLY_SECTIONS}
273 .eh_frame_hdr ${RELOCATING-0} : { *(.eh_frame_hdr) }
274
275 ${RELOCATING+. = ALIGN(0x1000);}
276 .data ${RELOCATING-0} :
277 {
278 ${RELOCATING+*(.rodata .rodata.*)
279 *(.rodata1)
280 *(.gnu.linkonce.r.*)
281 ${DATA_START_SYMBOLS}}
282 *(.data${RELOCATING+ .data.* .gnu.linkonce.d.*})
283 ${CONSTRUCTING+SORT(CONSTRUCTORS)}
284 ${RELOCATING+KEEP (*(.eh_frame))
285 *(.gcc_except_table)
286 ${CTOR}
287 ${DTOR}
288 KEEP (*(.jcr))}
289 }
290 .data1 ${RELOCATING-0} : { *(.data1) }
291 ${RELOCATING+. = ALIGN(0x1000);}
292 .gcc_except_table ${RELOCATING-0} : { *(.gcc_except_table) }
293 ${WRITABLE_RODATA+${RODATA}}
294 ${OTHER_READWRITE_SECTIONS}
295 ${TEXT_DYNAMIC-${DYNAMIC}}
296 ${DATA_PLT+${PLT}}
297 ${RELOCATING+${OTHER_GOT_SYMBOLS}}
298 .got ${RELOCATING-0} : {${RELOCATING+ *(.got.plt)} *(.got) }
299 ${OTHER_GOT_SECTIONS}
300 ${CREATE_SHLIB+${SDATA2}}
301 ${CREATE_SHLIB+${SBSS2}}
302 ${SDATA}
303 ${OTHER_SDATA_SECTIONS}
304 ${RELOCATING+_edata = .;}
305 ${RELOCATING+PROVIDE (edata = .);}
306 ${RELOCATING+__bss_start = .;}
307 ${RELOCATING+${OTHER_BSS_SYMBOLS}}
308 ${SBSS}
309 ${BSS_PLT+${PLT}}
310 ${RELOCATING+. = ALIGN(0x1000);}
311 .bss ${RELOCATING-0} :
312 {
313 ${RELOCATING+*(.dynbss)}
314 *(.bss${RELOCATING+ .bss.* .gnu.linkonce.b.*})
315 ${RELOCATING+*(COMMON)
316 /* Align here to ensure that the .bss section occupies space up to
317 _end. Align after .bss to ensure correct alignment even if the
318 .bss section disappears because there are no input sections. */
319 . = ALIGN(${ALIGNMENT});}
320 }
321 ${RELOCATING+${OTHER_BSS_END_SYMBOLS}}
322 ${RELOCATING+. = ALIGN(${ALIGNMENT});}
323 ${RELOCATING+${OTHER_END_SYMBOLS}}
324 ${RELOCATING+_end = .;}
325 ${RELOCATING+PROVIDE (end = .);}
326 ${STACK_ADDR+${STACK}}
327
328 EOF
329
330 source_sh $srcdir/scripttempl/misc-sections.sc
331 source_sh $srcdir/scripttempl/DWARF.sc
332
333 cat <<EOF
334 ${ATTRS_SECTIONS}
335 ${OTHER_SECTIONS}
336 ${RELOCATING+${OTHER_SYMBOLS}}
337 }
338 EOF