]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - ld/scripttempl/elf_chaos.sc
2.41 Release sources
[thirdparty/binutils-gdb.git] / ld / scripttempl / elf_chaos.sc
CommitLineData
d87bef3a 1# Copyright (C) 2014-2023 Free Software Foundation, Inc.
6c19b93b 2#
985743c7
NC
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.
b4477fa8
NC
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)
104d59d1 18# ATTRS_SECTIONS - at the end
b4477fa8
NC
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.
6c19b93b 34# EMBEDDED - whether this is for an embedded system.
b4477fa8
NC
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
6c19b93b 40# combination of .init sections.
b4477fa8 41# FINI_START, FINI_END - statements just before and just after
6c19b93b 42# combination of .fini sections.
b4477fa8 43# STACK_ADDR - start of a .stack section.
827a1c67 44# OTHER_SYMBOLS - symbols to place right at the end of the script.
b4477fa8
NC
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
71test -z "$ENTRY" && ENTRY=_start
72test -z "${BIG_OUTPUT_FORMAT}" && BIG_OUTPUT_FORMAT=${OUTPUT_FORMAT}
73test -z "${LITTLE_OUTPUT_FORMAT}" && LITTLE_OUTPUT_FORMAT=${OUTPUT_FORMAT}
74if [ -z "$MACHINE" ]; then OUTPUT_ARCH=${ARCH}; else OUTPUT_ARCH=${ARCH}:${MACHINE}; fi
75test -z "${ELFSIZE}" && ELFSIZE=32
76test -z "${ALIGNMENT}" && ALIGNMENT="${ELFSIZE} / 8"
104d59d1 77test -z "$ATTRS_SECTIONS" && ATTRS_SECTIONS=".gnu.attributes 0 : { KEEP (*(.gnu.attributes)) }"
b4477fa8 78test "$LD_FLAG" = "N" && DATA_ADDR=.
ec2d9b29
AM
79INTERP=".interp ${RELOCATING-0} : { *(.interp) }"
80PLT=".plt ${RELOCATING-0} : { *(.plt) }"
81DYNAMIC=".dynamic ${RELOCATING-0} : { *(.dynamic) }"
82RODATA=".rodata ${RELOCATING-0} : { *(.rodata${RELOCATING+ .rodata.* .gnu.linkonce.r.*}) }"
b4477fa8 83if test -z "${NO_SMALL_DATA}"; then
ec2d9b29 84 SBSS=".sbss ${RELOCATING-0} :
b4477fa8
NC
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 }"
ec2d9b29 94 SBSS2=".sbss2 ${RELOCATING-0} : { *(.sbss2${RELOCATING+ .sbss2.* .gnu.linkonce.sb2.*}) }"
b4477fa8
NC
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. */
6c19b93b 98 .sdata ${RELOCATING-0} :
b4477fa8
NC
99 {
100 ${RELOCATING+${SDATA_START_SYMBOLS}}
101 *(.sdata${RELOCATING+ .sdata.* .gnu.linkonce.s.*})
102 }"
ec2d9b29
AM
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.*}) }"
b4477fa8
NC
112fi
113CTOR="
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
40cf2291
AM
125 KEEP (*crtbegin.o(.ctors))
126 KEEP (*crtbegin?.o(.ctors))
b4477fa8
NC
127
128 /* We don't want to include the .ctor section from
bd6791bc 129 the crtend.o file until after the sorted ctors.
b4477fa8
NC
130 The .ctor section from the crtend file contains the
131 end of ctors marker and it must be last */
132
40cf2291 133 KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .ctors))
b4477fa8
NC
134 KEEP (*(SORT(.ctors.*)))
135 KEEP (*(.ctors))
136 ${CONSTRUCTING+${CTOR_END}}
137"
138DTOR="
139 ${CONSTRUCTING+${DTOR_START}}
40cf2291
AM
140 KEEP (*crtbegin.o(.dtors))
141 KEEP (*crtbegin?.o(.dtors))
142 KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o $OTHER_EXCLUDE_FILES) .dtors))
b4477fa8
NC
143 KEEP (*(SORT(.dtors.*)))
144 KEEP (*(.dtors))
145 ${CONSTRUCTING+${DTOR_END}}
146"
ec2d9b29 147STACK=" .stack ${RELOCATING-0}${RELOCATING+${STACK_ADDR}} :
b4477fa8
NC
148 {
149 ${RELOCATING+_stack = .;}
150 *(.stack)
151 }"
152
153test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}"
154
155cat <<EOF
d87bef3a 156/* Copyright (C) 2014-2023 Free Software Foundation, Inc.
985743c7
NC
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
b4477fa8
NC
162OUTPUT_FORMAT("${OUTPUT_FORMAT}", "${BIG_OUTPUT_FORMAT}",
163 "${LITTLE_OUTPUT_FORMAT}")
164OUTPUT_ARCH(${OUTPUT_ARCH})
b34c1498 165${RELOCATING+ENTRY(${ENTRY})}
b4477fa8
NC
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}}
ec2d9b29
AM
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. */}
b4477fa8
NC
176
177SECTIONS
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}}
ec2d9b29
AM
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) }
b4477fa8
NC
191
192EOF
193if [ "x$COMBRELOC" = x ]; then
194 COMBRELOCCAT=cat
195else
196 COMBRELOCCAT="cat > $COMBRELOC"
197fi
198eval $COMBRELOCCAT <<EOF
ec2d9b29
AM
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.*}) }
b4477fa8 207 ${OTHER_READONLY_RELOC_SECTIONS}
ec2d9b29
AM
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) }
b4477fa8
NC
216 ${OTHER_GOT_RELOC_SECTIONS}
217 ${REL_SDATA}
218 ${REL_SBSS}
219 ${REL_SDATA2}
220 ${REL_SBSS2}
ec2d9b29
AM
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.*}) }
b4477fa8
NC
223EOF
224if [ -n "$COMBRELOC" ]; then
225cat <<EOF
ec2d9b29 226 .rel.dyn ${RELOCATING-0} :
b4477fa8
NC
227 {
228EOF
6c19b93b 229sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rela\./d;s/^.*: { *\(.*\)}$/ \1/' $COMBRELOC
b4477fa8
NC
230cat <<EOF
231 }
ec2d9b29 232 .rela.dyn ${RELOCATING-0} :
b4477fa8
NC
233 {
234EOF
6c19b93b 235sed -e '/^[ ]*[{}][ ]*$/d;/:[ ]*$/d;/\.rel\./d;s/^.*: { *\(.*\)}/ \1/' $COMBRELOC
b4477fa8
NC
236cat <<EOF
237 }
238EOF
239fi
240cat <<EOF
2bf2bf23 241 ${RELOCATING+. = ALIGN(0x1000);}
ec2d9b29
AM
242 .rel.plt ${RELOCATING-0} : { *(.rel.plt) }
243 .rela.plt ${RELOCATING-0} : { *(.rela.plt) }
b4477fa8
NC
244 ${OTHER_PLT_RELOC_SECTIONS}
245 ${DATA_PLT-${BSS_PLT-${PLT}}}
ec2d9b29 246 .text ${RELOCATING-0} :
b4477fa8
NC
247 {
248 ${RELOCATING+${TEXT_START_SYMBOLS}}
249 *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
075a2b89 250 /* .gnu.warning sections are handled specially by elf.em. */
b4477fa8
NC
251 *(.gnu.warning)
252 ${RELOCATING+${OTHER_TEXT_SECTIONS}}
253 } =${NOP-0}
2d3181c7
AM
254 .init ${RELOCATING-0} :
255 {
256 ${RELOCATING+${INIT_START}}
257 KEEP (*(SORT_NONE(.init)))
258 ${RELOCATING+${INIT_END}}
259 } =${NOP-0}
ec2d9b29 260 .fini ${RELOCATING-0} :
b4477fa8
NC
261 {
262 ${RELOCATING+${FINI_START}}
2d3181c7 263 KEEP (*(SORT_NONE(.fini)))
b4477fa8
NC
264 ${RELOCATING+${FINI_END}}
265 } =${NOP-0}
266 ${RELOCATING+PROVIDE (__etext = .);}
267 ${RELOCATING+PROVIDE (_etext = .);}
268 ${RELOCATING+PROVIDE (etext = .);}
2bf2bf23 269 ${RELOCATING+. = ALIGN(0x1000);}
b4477fa8
NC
270 ${CREATE_SHLIB-${SDATA2}}
271 ${CREATE_SHLIB-${SBSS2}}
272 ${OTHER_READONLY_SECTIONS}
c2e86386 273 .eh_frame_hdr ${RELOCATING-0} : { *(.eh_frame_hdr) }
b4477fa8 274
2bf2bf23 275 ${RELOCATING+. = ALIGN(0x1000);}
ec2d9b29 276 .data ${RELOCATING-0} :
b4477fa8 277 {
2bf2bf23 278 ${RELOCATING+*(.rodata .rodata.*)
b4477fa8
NC
279 *(.rodata1)
280 *(.gnu.linkonce.r.*)
2bf2bf23 281 ${DATA_START_SYMBOLS}}
b4477fa8
NC
282 *(.data${RELOCATING+ .data.* .gnu.linkonce.d.*})
283 ${CONSTRUCTING+SORT(CONSTRUCTORS)}
2bf2bf23 284 ${RELOCATING+KEEP (*(.eh_frame))
b4477fa8
NC
285 *(.gcc_except_table)
286 ${CTOR}
287 ${DTOR}
2bf2bf23 288 KEEP (*(.jcr))}
b4477fa8 289 }
ec2d9b29 290 .data1 ${RELOCATING-0} : { *(.data1) }
2bf2bf23 291 ${RELOCATING+. = ALIGN(0x1000);}
ec2d9b29 292 .gcc_except_table ${RELOCATING-0} : { *(.gcc_except_table) }
b4477fa8
NC
293 ${WRITABLE_RODATA+${RODATA}}
294 ${OTHER_READWRITE_SECTIONS}
295 ${TEXT_DYNAMIC-${DYNAMIC}}
296 ${DATA_PLT+${PLT}}
297 ${RELOCATING+${OTHER_GOT_SYMBOLS}}
2bf2bf23 298 .got ${RELOCATING-0} : {${RELOCATING+ *(.got.plt)} *(.got) }
b4477fa8
NC
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}}
2bf2bf23 310 ${RELOCATING+. = ALIGN(0x1000);}
ec2d9b29 311 .bss ${RELOCATING-0} :
b4477fa8 312 {
2bf2bf23 313 ${RELOCATING+*(.dynbss)}
b4477fa8 314 *(.bss${RELOCATING+ .bss.* .gnu.linkonce.b.*})
2bf2bf23 315 ${RELOCATING+*(COMMON)
b4477fa8
NC
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. */
2bf2bf23 319 . = ALIGN(${ALIGNMENT});}
b4477fa8 320 }
827a1c67 321 ${RELOCATING+${OTHER_BSS_END_SYMBOLS}}
b4477fa8 322 ${RELOCATING+. = ALIGN(${ALIGNMENT});}
9f4fb502 323 ${RELOCATING+${OTHER_END_SYMBOLS}}
827a1c67 324 ${RELOCATING+_end = .;}
b4477fa8 325 ${RELOCATING+PROVIDE (end = .);}
74dc9032 326 ${STACK_ADDR+${STACK}}
b4477fa8 327
ceb0a680 328EOF
b4477fa8 329
6752dd75
AM
330source_sh $srcdir/scripttempl/misc-sections.sc
331source_sh $srcdir/scripttempl/DWARF.sc
b4477fa8 332
ceb0a680 333cat <<EOF
104d59d1 334 ${ATTRS_SECTIONS}
b4477fa8 335 ${OTHER_SECTIONS}
827a1c67 336 ${RELOCATING+${OTHER_SYMBOLS}}
b4477fa8
NC
337}
338EOF