1 # Linker script for PE. 2 # 3 # Copyright (C) 2014-2025 Free Software Foundation, Inc. 4 # 5 # Copying and distribution of this file, with or without modification, 6 # are permitted in any medium without royalty provided the copyright 7 # notice and this notice are preserved. 8 9 if test -z "${RELOCATEABLE_OUTPUT_FORMAT}"; then 10 RELOCATEABLE_OUTPUT_FORMAT=${OUTPUT_FORMAT} 11 fi 12 13 # We can't easily and portably get an unquoted $ in a shell 14 # substitution, so we do this instead. 15 # Sorting of the .foo$* sections is required by the definition of 16 # grouped sections in PE. 17 # Sorting of the file names in R_IDATA and R_DIDAT is required by the 18 # current implementation of dlltool (this could probably be changed to 19 # use grouped sections instead). 20 if test "${RELOCATING}"; then 21 R_TEXT='*(SORT(.text$*))' 22 if test "x$LD_FLAG" = "xauto_import" ; then 23 R_DATA='*(SORT(.data$*)) 24 *(.rdata) 25 *(SORT(.rdata$*))' 26 R_RDATA='' 27 else 28 R_DATA='*(SORT(.data$*))' 29 R_RDATA='*(.rdata) 30 *(SORT(.rdata$*))' 31 fi 32 R_IDATA234=' 33 KEEP (SORT(*)(.idata$2)) 34 KEEP (SORT(*)(.idata$3)) 35 /* These zeroes mark the end of the import list. */ 36 LONG (0); LONG (0); LONG (0); LONG (0); LONG (0); 37 KEEP (SORT(*)(.idata$4))' 38 R_IDATA5='KEEP (SORT(*)(.idata$5))' 39 R_IDATA67=' 40 KEEP (SORT(*)(.idata$6)) 41 KEEP (SORT(*)(.idata$7))' 42 R_DIDAT234=' 43 __DELAY_IMPORT_DIRECTORY_start__ = .; 44 KEEP (SORT(*)(.didat$2)) 45 KEEP (SORT(*)(.didat$3)) 46 __DELAY_IMPORT_DIRECTORY_end__ = .; 47 /* These zeroes mark the end of the import list. */ 48 . += (__DELAY_IMPORT_DIRECTORY_end__ - __DELAY_IMPORT_DIRECTORY_start__) ? 8*4 : 0; 49 KEEP (SORT(*)(.didat$4))' 50 R_DIDAT5='KEEP (SORT(*)(.didat$5))' 51 R_DIDAT67=' 52 KEEP (SORT(*)(.didat$6)) 53 KEEP (SORT(*)(.didat$7))' 54 R_CRT_XC='KEEP (*(SORT(.CRT$XC*))) /* C initialization */' 55 R_CRT_XI='KEEP (*(SORT(.CRT$XI*))) /* C++ initialization */' 56 R_CRT_XL='KEEP (*(SORT(.CRT$XL*))) /* TLS callbacks */' 57 R_CRT_XP='KEEP (*(SORT(.CRT$XP*))) /* Pre-termination */' 58 R_CRT_XT='KEEP (*(SORT(.CRT$XT*))) /* Termination */' 59 R_CRT_XD='KEEP (*(SORT(.CRT$XD*))) /* Dynamic TLS Initializer */' 60 R_TLS=' 61 KEEP (*(.tls$AAA)) 62 KEEP (*(.tls)) 63 KEEP (*(.tls$)) 64 KEEP (*(SORT(.tls$*))) 65 KEEP (*(.tls$ZZZ))' 66 R_RSRC=' 67 KEEP (*(.rsrc)) 68 KEEP (*(.rsrc$*))' 69 else 70 R_TEXT= 71 R_DATA= 72 R_RDATA='*(.rdata)' 73 R_IDATA234= 74 R_IDATA5= 75 R_IDATA67= 76 R_DIDAT234= 77 R_DIDAT5= 78 R_DIDAT67= 79 R_CRT_XC= 80 R_CRT_XI= 81 R_CRT_XL= 82 R_CRT_XP= 83 R_CRT_XT= 84 R_CRT_XD= 85 R_TLS='*(.tls)' 86 R_RSRC='*(.rsrc)' 87 fi 88 89 cat <<EOF 90 /* Copyright (C) 2014-2025 Free Software Foundation, Inc. 91 92 Copying and distribution of this script, with or without modification, 93 are permitted in any medium without royalty provided the copyright 94 notice and this notice are preserved. */ 95 96 ${RELOCATING+OUTPUT_FORMAT(${OUTPUT_FORMAT})} 97 ${RELOCATING-OUTPUT_FORMAT(${RELOCATEABLE_OUTPUT_FORMAT})} 98 ${OUTPUT_ARCH+OUTPUT_ARCH(${OUTPUT_ARCH})} 99 100 ${LIB_SEARCH_DIRS} 101 102 SECTIONS 103 { 104 ${RELOCATING+/* Make the virtual address and file offset synced if the alignment is} 105 ${RELOCATING+ lower than the target page size. */} 106 ${RELOCATING+. = SIZEOF_HEADERS;} 107 ${RELOCATING+. = ALIGN(__section_alignment__);} 108 .text ${RELOCATING+ __image_base__ + ( __section_alignment__ < ${TARGET_PAGE_SIZE} ? . : __section_alignment__ )} : 109 { 110 ${RELOCATING+KEEP (*(SORT_NONE(.init)))} 111 *(.text) 112 ${R_TEXT} 113 ${RELOCATING+ *(.text.*)} 114 ${RELOCATING+ *(.gnu.linkonce.t.*)} 115 ${RELOCATING+*(.glue_7t)} 116 ${RELOCATING+*(.glue_7)} 117 ${RELOCATING+KEEP (*(SORT_NONE(.fini)))} 118 ${RELOCATING+/* ??? Why is .gcc_exc here? */} 119 ${RELOCATING+ *(.gcc_exc)} 120 ${RELOCATING+PROVIDE (etext = .);} 121 ${RELOCATING+PROVIDE (_etext = .);} 122 ${RELOCATING+ KEEP (*(.gcc_except_table))} 123 } 124 125 /* The Cygwin32 library uses a section to avoid copying certain data 126 on fork. This used to be named ".data$nocopy". The linker used 127 to include this between __data_start__ and __data_end__, but that 128 breaks building the cygwin32 dll. Instead, we name the section 129 ".data_cygwin_nocopy" and explicitly include it after __data_end__. */ 130 131 .data ${RELOCATING+BLOCK(__section_alignment__)} : 132 { 133 ${RELOCATING+__data_start__ = . ;} 134 *(.data) 135 ${RELOCATING+*(.data2)} 136 ${R_DATA} 137 KEEP(*(.jcr)) 138 ${RELOCATING+__data_end__ = . ;} 139 ${RELOCATING+*(.data_cygwin_nocopy)} 140 } 141 142 .rdata ${RELOCATING+BLOCK(__section_alignment__)} : 143 { 144 ${R_RDATA} 145 . = ALIGN(4); 146 ${RELOCATING+__rt_psrelocs_start = .;} 147 ${RELOCATING+KEEP(*(.rdata_runtime_pseudo_reloc))} 148 ${RELOCATING+__rt_psrelocs_end = .;} 149 /* read-only parts of .didat */ 150 /* This cannot currently be handled with grouped sections. 151 See pe.em:sort_sections. */ 152 ${R_DIDAT234} 153 ${R_DIDAT67} 154 155 /* .ctors & .dtors */ 156 ${CONSTRUCTING+ 157 /* Note: we always define __CTOR_LIST__ and ___CTOR_LIST__ here, 158 we do not PROVIDE them. This is because the ctors.o startup 159 code in libgcc defines them as common symbols, with the 160 expectation that they will be overridden by the definitions 161 here. If we PROVIDE the symbols then they will not be 162 overridden and global constructors will not be run. 163 See PR 22762 for more details. 164 165 This does mean that it is not possible for a user to define 166 their own __CTOR_LIST__ and __DTOR_LIST__ symbols; if they do, 167 the content from those variables are included but the symbols 168 defined here silently take precedence. If they truly need to 169 be redefined, a custom linker script will have to be used. 170 (The custom script can just be a copy of this script with the 171 PROVIDE() qualifiers added). 172 173 In particular this means that ld -Ur does not work, because 174 the proper __CTOR_LIST__ set by ld -Ur is overridden by a 175 bogus __CTOR_LIST__ set by the final link. See PR 46. */ 176 ___CTOR_LIST__ = .; 177 __CTOR_LIST__ = .; 178 LONG (-1); 179 KEEP(*(.ctors)); 180 KEEP(*(.ctor)); 181 KEEP(*(SORT_BY_NAME(.ctors.*))); 182 LONG (0); 183 } 184 ${CONSTRUCTING+ 185 /* See comment about __CTOR_LIST__ above. The same reasoning 186 applies here too. */ 187 ___DTOR_LIST__ = .; 188 __DTOR_LIST__ = .; 189 LONG (-1); 190 KEEP(*(.dtors)); 191 KEEP(*(.dtor)); 192 KEEP(*(SORT_BY_NAME(.dtors.*))); 193 LONG (0); 194 } 195 196 /* .CRT */ 197 ${RELOCATING+___crt_xc_start__ = . ;} 198 ${R_CRT_XC} 199 ${RELOCATING+___crt_xc_end__ = . ;} 200 ${RELOCATING+___crt_xi_start__ = . ;} 201 ${R_CRT_XI} 202 ${RELOCATING+___crt_xi_end__ = . ;} 203 ${RELOCATING+___crt_xl_start__ = . ;} 204 ${R_CRT_XL} 205 /* ___crt_xl_end__ is defined in the TLS Directory support code */ 206 ${RELOCATING+___crt_xp_start__ = . ;} 207 ${R_CRT_XP} 208 ${RELOCATING+___crt_xp_end__ = . ;} 209 ${RELOCATING+___crt_xt_start__ = . ;} 210 ${R_CRT_XT} 211 ${RELOCATING+___crt_xt_end__ = . ;} 212 ${RELOCATING+___crt_xd_start__ = . ;} 213 ${R_CRT_XD} 214 ${RELOCATING+___crt_xd_end__ = . ;} 215 } 216 217 ${RELOCATING+___RUNTIME_PSEUDO_RELOC_LIST_END__ = __rt_psrelocs_end;} 218 ${RELOCATING+__RUNTIME_PSEUDO_RELOC_LIST_END__ = __rt_psrelocs_end;} 219 ${RELOCATING+___RUNTIME_PSEUDO_RELOC_LIST__ = __rt_psrelocs_start;} 220 ${RELOCATING+__RUNTIME_PSEUDO_RELOC_LIST__ = __rt_psrelocs_start;} 221 222 .eh_frame ${RELOCATING+BLOCK(__section_alignment__)} : 223 { 224 KEEP(*(.eh_frame${RELOCATING+*})) 225 } 226 227 .pdata ${RELOCATING+BLOCK(__section_alignment__)} : 228 { 229 KEEP(*(.pdata${RELOCATING+*})) 230 } 231 232 .bss ${RELOCATING+BLOCK(__section_alignment__)} : 233 { 234 ${RELOCATING+__bss_start__ = . ;} 235 *(.bss) 236 *(COMMON) 237 ${RELOCATING+__bss_end__ = . ;} 238 } 239 240 .edata ${RELOCATING+BLOCK(__section_alignment__)} : 241 { 242 *(.edata) 243 } 244 245 /DISCARD/ : 246 { 247 *(.debug\$S) 248 *(.debug\$T) 249 *(.debug\$F) 250 ${RELOCATING+ *(.drectve)} 251 ${RELOCATING+ *(.note.GNU-stack)} 252 ${RELOCATING+ *(.gnu.lto_*)} 253 ${RELOCATING+ *(.gnu_object_only)} 254 } 255 256 .idata ${RELOCATING+BLOCK(__section_alignment__)} : 257 { 258 /* This cannot currently be handled with grouped sections. 259 See pe.em:sort_sections. */ 260 ${R_IDATA234} 261 ${RELOCATING+__IAT_start__ = .;} 262 ${R_IDATA5} 263 ${RELOCATING+__IAT_end__ = .;} 264 ${R_IDATA67} 265 } 266 267 .didat ${RELOCATING+BLOCK(__section_alignment__)} : 268 { 269 /* This cannot currently be handled with grouped sections. 270 See pe.em:sort_sections. */ 271 ${R_DIDAT5} 272 } 273 274 /* Windows TLS expects .tls\$AAA to be at the start and .tls\$ZZZ to be 275 at the end of section. This is important because _tls_start MUST 276 be at the beginning of the section to enable SECREL32 relocations with TLS 277 data. */ 278 .tls ${RELOCATING+BLOCK(__section_alignment__)} : 279 { 280 ${RELOCATING+___tls_start__ = . ;} 281 ${R_TLS} 282 ${RELOCATING+___tls_end__ = . ;} 283 } 284 285 .endjunk ${RELOCATING+BLOCK(__section_alignment__)} : 286 { 287 /* end is deprecated, don't use it */ 288 ${RELOCATING+PROVIDE (end = .);} 289 ${RELOCATING+PROVIDE ( _end = .);} 290 ${RELOCATING+ __end__ = .;} 291 } 292 293 .rsrc ${RELOCATING+BLOCK(__section_alignment__)} : SUBALIGN(4) 294 { 295 ${R_RSRC} 296 } 297 298 .reloc ${RELOCATING+BLOCK(__section_alignment__)} : 299 { 300 *(.reloc) 301 } 302 303 .stab ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 304 { 305 *(.stab) 306 } 307 308 .stabstr ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 309 { 310 *(.stabstr) 311 } 312 313 /* DWARF debug sections. 314 Symbols in the DWARF debugging sections are relative to the beginning 315 of the section. Unlike other targets that fake this by putting the 316 section VMA at 0, the PE format will not allow it. */ 317 318 /* DWARF 1.1 and DWARF 2. */ 319 .debug_aranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 320 { 321 *(.debug_aranges) 322 } 323 .zdebug_aranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 324 { 325 *(.zdebug_aranges) 326 } 327 328 .debug_pubnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 329 { 330 *(.debug_pubnames) 331 } 332 .zdebug_pubnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 333 { 334 *(.zdebug_pubnames) 335 } 336 337 /* DWARF 2. */ 338 .debug_info ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 339 { 340 *(.debug_info${RELOCATING+ .gnu.linkonce.wi.*}) 341 } 342 .zdebug_info ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 343 { 344 *(.zdebug_info${RELOCATING+ .zdebug.gnu.linkonce.wi.*}) 345 } 346 347 .debug_abbrev ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 348 { 349 *(.debug_abbrev) 350 } 351 .zdebug_abbrev ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 352 { 353 *(.zdebug_abbrev) 354 } 355 356 .debug_line ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 357 { 358 *(.debug_line) 359 } 360 .zdebug_line ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 361 { 362 *(.zdebug_line) 363 } 364 365 .debug_frame ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 366 { 367 *(.debug_frame*) 368 } 369 .zdebug_frame ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 370 { 371 *(.zdebug_frame*) 372 } 373 374 .debug_str ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 375 { 376 *(.debug_str) 377 } 378 .zdebug_str ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 379 { 380 *(.zdebug_str) 381 } 382 383 .debug_loc ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 384 { 385 *(.debug_loc) 386 } 387 .zdebug_loc ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 388 { 389 *(.zdebug_loc) 390 } 391 392 .debug_macinfo ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 393 { 394 *(.debug_macinfo) 395 } 396 .zdebug_macinfo ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 397 { 398 *(.zdebug_macinfo) 399 } 400 401 /* SGI/MIPS DWARF 2 extensions. */ 402 .debug_weaknames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 403 { 404 *(.debug_weaknames) 405 } 406 .zdebug_weaknames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 407 { 408 *(.zdebug_weaknames) 409 } 410 411 .debug_funcnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 412 { 413 *(.debug_funcnames) 414 } 415 .zdebug_funcnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 416 { 417 *(.zdebug_funcnames) 418 } 419 420 .debug_typenames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 421 { 422 *(.debug_typenames) 423 } 424 .zdebug_typenames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 425 { 426 *(.zdebug_typenames) 427 } 428 429 .debug_varnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 430 { 431 *(.debug_varnames) 432 } 433 .zdebug_varnames ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 434 { 435 *(.zdebug_varnames) 436 } 437 438 /* DWARF 3. */ 439 .debug_pubtypes ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 440 { 441 *(.debug_pubtypes) 442 } 443 .zdebug_pubtypes ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 444 { 445 *(.zdebug_pubtypes) 446 } 447 448 .debug_ranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 449 { 450 *(.debug_ranges) 451 } 452 .zdebug_ranges ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 453 { 454 *(.zdebug_ranges) 455 } 456 457 /* DWARF 4. */ 458 .debug_types ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 459 { 460 *(.debug_types${RELOCATING+ .gnu.linkonce.wt.*}) 461 } 462 .zdebug_types ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 463 { 464 *(.zdebug_types${RELOCATING+ .gnu.linkonce.wt.*}) 465 } 466 467 /* DWARF 5. */ 468 .debug_addr ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 469 { 470 *(.debug_addr) 471 } 472 .zdebug_addr ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 473 { 474 *(.zdebug_addr) 475 } 476 .debug_line_str ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 477 { 478 *(.debug_line_str) 479 } 480 .zdebug_line_str ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 481 { 482 *(.zdebug_line_str) 483 } 484 .debug_loclists ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 485 { 486 *(.debug_loclists) 487 } 488 .zdebug_loclists ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 489 { 490 *(.zdebug_loclists) 491 } 492 .debug_macro ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 493 { 494 *(.debug_macro) 495 } 496 .zdebug_macro ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 497 { 498 *(.zdebug_macro) 499 } 500 .debug_names ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 501 { 502 *(.debug_names) 503 } 504 .zdebug_names ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 505 { 506 *(.zdebug_names) 507 } 508 .debug_rnglists ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 509 { 510 *(.debug_rnglists) 511 } 512 .zdebug_rnglists ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 513 { 514 *(.zdebug_rnglists) 515 } 516 .debug_str_offsets ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 517 { 518 *(.debug_str_offsets) 519 } 520 .zdebug_str_offsets ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 521 { 522 *(.zdebug_str_offsets) 523 } 524 .debug_sup ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 525 { 526 *(.debug_sup) 527 } 528 529 /* For Go and Rust. */ 530 .debug_gdb_scripts ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 531 { 532 *(.debug_gdb_scripts) 533 } 534 .zdebug_gdb_scripts ${RELOCATING+BLOCK(__section_alignment__)} ${RELOCATING+(NOLOAD)} : 535 { 536 *(.zdebug_gdb_scripts) 537 } 538 } 539 EOF 540