> Date: Fri, 7 May 2021 00:04:57 -0400
> From: George Koehler <[email protected]>
>
> Hello tech list,
>
> I want macppc to switch from ld.bfd to ld.lld, but there is a problem
> when lld links ofwboot or the kernel. I propose to fix it by adding
> ld.script for both. These scripts also work with ld.bfd, so I want to
> commit my diff at the end of this mail, ok?
>
> lld sets the symbol "etext" to a nonsense value like 0x10000034. In
> ofwboot, wrong "etext" causes freeze, failure to boot kernel. (Wrong
> "etext" in kernel doesn't cause an obvious problem.) Other lld arches
> use an ld.script to set a correct "etext" in kernel.
>
> I copied the ld.script from powerpc64's kernel and made these changes
> for macppc's kernel:
> - change "_start" to "start" to match macppc/locore0.S
> - remove PT_DYNAMIC and sections (don't exist in macppc kernel)
> - put .text at 0x00100114 to match Makefile
> - remove symbols like "_erodata" (not used by macppc kernel)
> - add ".rodata.*" and such, so sections and segments look correct
Makes sense to me. It seems ldd always seems to require a little bit
more coercion to produce non-standard binaries. We use linker scripts
for the various EFI bootloaders as well.
ok kettenis@
> | --- arch/powerpc64/conf/ld.script Sat Jul 18 09:16:32 2020
> | +++ arch/macppc/conf/ld.script Sat Apr 24 11:52:34 2021
> | @@ -16,18 +16,17 @@
> | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> | */
> |
> | -ENTRY(_start)
> | +ENTRY(start)
> |
> | PHDRS
> | {
> | text PT_LOAD;
> | - dynamic PT_DYNAMIC;
> | openbsd_randomize PT_OPENBSD_RANDOMIZE;
> | }
> |
> | SECTIONS
> | {
> | - . = 0x00100000;
> | + . = 0x00100114;
> | .text :
> | {
> | *(.text)
> | @@ -35,49 +34,35 @@
> | PROVIDE (etext = .);
> | PROVIDE (_etext = .);
> |
> | - . = ALIGN(4096);
> | - .rela.dyn : { *(.rela.dyn) }
> | -
> | - .dynamic :
> | + .rodata :
> | {
> | - *(.dynamic)
> | - } :dynamic :text
> | + *(.rodata .rodata.*)
> | + } :text
> |
> | - .rodata :
> | + .data.rel.ro :
> | {
> | - *(.rodata)
> | *(.data.rel.ro)
> | } :text
> |
> | .openbsd.randomdata :
> | {
> | - *(.openbsd.randomdata)
> | + *(.openbsd.randomdata .openbsd.randomdata.*)
> | } :openbsd_randomize :text
> | - PROVIDE (_erodata = .);
> |
> | - . = ALIGN(4096);
> | .data :
> | {
> | *(.data)
> | } :text
> |
> | - . = ALIGN(4096);
> | - .got : { *(.got) }
> | - .toc : { *(.toc) }
> | + .sbss :
> | + {
> | + *(.sbss)
> | + }
> |
> | - PROVIDE (__bss_start = .);
> | .bss :
> | {
> | *(.bss)
> | }
> | PROVIDE (end = .);
> | PROVIDE (_end = .);
> | -
> | - /DISCARD/ :
> | - {
> | - *(.dynsym)
> | - *(.dynstr)
> | - *(.gnu.hash)
> | - *(.hash)
> | - }
> | }
>
> Then I made these changes for ofwboot:
> - use "_start" and 0x00020000 to match Makefile
> - remove randomdata (doesn't exist in ofwboot)
>
> | --- arch/macppc/conf/ld.script Sat Apr 24 11:52:34 2021
> | +++ arch/macppc/stand/ofwboot/ld.script Sat Apr 24 11:52:34 2021
> | @@ -16,17 +16,17 @@
> | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> | */
> |
> | -ENTRY(start)
> | +ENTRY(_start)
> |
> | PHDRS
> | {
> | text PT_LOAD;
> | - openbsd_randomize PT_OPENBSD_RANDOMIZE;
> | }
> |
> | SECTIONS
> | {
> | - . = 0x00100114;
> | + /* Must match RELOC in Makefile */
> | + . = 0x00020000;
> | .text :
> | {
> | *(.text)
> | @@ -43,11 +43,6 @@
> | {
> | *(.data.rel.ro)
> | } :text
> | -
> | - .openbsd.randomdata :
> | - {
> | - *(.openbsd.randomdata .openbsd.randomdata.*)
> | - } :openbsd_randomize :text
> |
> | .data :
> | {
>
> Index: arch/macppc/conf/Makefile.macppc
> ===================================================================
> RCS file: /cvs/src/sys/arch/macppc/conf/Makefile.macppc,v
> retrieving revision 1.101
> diff -u -p -r1.101 Makefile.macppc
> --- arch/macppc/conf/Makefile.macppc 28 Jan 2021 17:39:03 -0000 1.101
> +++ arch/macppc/conf/Makefile.macppc 6 May 2021 20:01:08 -0000
> @@ -51,7 +51,7 @@ DEBUG?= -g
> COPTIMIZE?= -O2
> CFLAGS= ${DEBUG} ${CWARNFLAGS} ${CMACHFLAGS} ${COPTIMIZE}
> ${COPTS} ${PIPE}
> AFLAGS= -D_LOCORE ${CMACHFLAGS}
> -LINKFLAGS= -N -Ttext 100114 -e start --warn-common -nopie
> +LINKFLAGS= -N -T ld.script --warn-common -nopie
>
> .if ${MACHINE} == "powerpc64"
> CFLAGS+= -m32
> Index: arch/macppc/conf/ld.script
> ===================================================================
> RCS file: /cvs/src/sys/arch/macppc/conf/ld.script,v
> retrieving revision 1.1
> diff -u -p -r1.1 ld.script
> --- arch/macppc/conf/ld.script 13 Jun 2017 01:42:52 -0000 1.1
> +++ arch/macppc/conf/ld.script 6 May 2021 20:01:08 -0000
> @@ -0,0 +1,68 @@
> +/* $OpenBSD: ld.script,v 1.4 2020/07/18 13:16:32 kettenis Exp $ */
> +
> +/*
> + * Copyright (c) 2013 Mark Kettenis <[email protected]>
> + *
> + * Permission to use, copy, modify, and distribute this software for any
> + * purpose with or without fee is hereby granted, provided that the above
> + * copyright notice and this permission notice appear in all copies.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
> + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
> + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
> + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
> + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
> + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> + */
> +
> +ENTRY(start)
> +
> +PHDRS
> +{
> + text PT_LOAD;
> + openbsd_randomize PT_OPENBSD_RANDOMIZE;
> +}
> +
> +SECTIONS
> +{
> + . = 0x00100114;
> + .text :
> + {
> + *(.text)
> + } :text
> + PROVIDE (etext = .);
> + PROVIDE (_etext = .);
> +
> + .rodata :
> + {
> + *(.rodata .rodata.*)
> + } :text
> +
> + .data.rel.ro :
> + {
> + *(.data.rel.ro)
> + } :text
> +
> + .openbsd.randomdata :
> + {
> + *(.openbsd.randomdata .openbsd.randomdata.*)
> + } :openbsd_randomize :text
> +
> + .data :
> + {
> + *(.data)
> + } :text
> +
> + .sbss :
> + {
> + *(.sbss)
> + }
> +
> + .bss :
> + {
> + *(.bss)
> + }
> + PROVIDE (end = .);
> + PROVIDE (_end = .);
> +}
> Index: arch/macppc/stand/ofwboot/Makefile
> ===================================================================
> RCS file: /cvs/src/sys/arch/macppc/stand/ofwboot/Makefile,v
> retrieving revision 1.20
> diff -u -p -r1.20 Makefile
> --- arch/macppc/stand/ofwboot/Makefile 16 Mar 2020 07:02:10 -0000
> 1.20
> +++ arch/macppc/stand/ofwboot/Makefile 6 May 2021 20:01:08 -0000
> @@ -31,18 +31,17 @@ SRCS+= moddi3.c
>
> NEWVERSWHAT= "OpenFirmware Boot"
>
> -# For now...
> +# Must match . in ld.script
> RELOC= 20000
>
> -ENTRY= _start
> -
> CPPFLAGS+= -I. -I${.CURDIR}/../../.. -I${.CURDIR}/../../../..
> CPPFLAGS+= -DRELOC=0x${RELOC} -DCONSPEED=57600
>
> LIBS!= cd $(.CURDIR)/$(R); $(MAKE) libdep
>
> -${PROG}: ${OBJS} ${LIBSA} ${LIBZ}
> - ${LD} -nopie -znorelro -N -X -Ttext ${RELOC} -e ${ENTRY} -o ${PROG} \
> +${PROG}: ${OBJS} ${LIBSA} ${LIBZ} ld.script
> + pwd
> + ${LD} -nopie -znorelro -N -X -T ${.CURDIR}/ld.script -o ${PROG} \
> ${OBJS} ${LIBS}
>
> .include <bsd.prog.mk>
> Index: arch/macppc/stand/ofwboot/ld.script
> ===================================================================
> RCS file: arch/macppc/stand/ofwboot/ld.script
> diff -N arch/macppc/stand/ofwboot/ld.script
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ arch/macppc/stand/ofwboot/ld.script 6 May 2021 20:01:08 -0000
> @@ -0,0 +1,63 @@
> +/* $OpenBSD: ld.script,v 1.4 2020/07/18 13:16:32 kettenis Exp $ */
> +
> +/*
> + * Copyright (c) 2013 Mark Kettenis <[email protected]>
> + *
> + * Permission to use, copy, modify, and distribute this software for any
> + * purpose with or without fee is hereby granted, provided that the above
> + * copyright notice and this permission notice appear in all copies.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
> + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
> + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
> + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
> + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
> + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> + */
> +
> +ENTRY(_start)
> +
> +PHDRS
> +{
> + text PT_LOAD;
> +}
> +
> +SECTIONS
> +{
> + /* Must match RELOC in Makefile */
> + . = 0x00020000;
> + .text :
> + {
> + *(.text)
> + } :text
> + PROVIDE (etext = .);
> + PROVIDE (_etext = .);
> +
> + .rodata :
> + {
> + *(.rodata .rodata.*)
> + } :text
> +
> + .data.rel.ro :
> + {
> + *(.data.rel.ro)
> + } :text
> +
> + .data :
> + {
> + *(.data)
> + } :text
> +
> + .sbss :
> + {
> + *(.sbss)
> + }
> +
> + .bss :
> + {
> + *(.bss)
> + }
> + PROVIDE (end = .);
> + PROVIDE (_end = .);
> +}
>