head	1.7;
access;
symbols
	netbsd-11-0-RC5:1.7
	netbsd-11-0-RC4:1.7
	netbsd-11-0-RC3:1.7
	netbsd-11-0-RC2:1.7
	netbsd-11-0-RC1:1.7
	perseant-exfatfs-base-20250801:1.7
	netbsd-11:1.7.0.2
	netbsd-11-base:1.7
	netbsd-10-1-RELEASE:1.5.2.1
	perseant-exfatfs-base-20240630:1.5
	perseant-exfatfs:1.5.0.4
	perseant-exfatfs-base:1.5
	netbsd-8-3-RELEASE:1.1
	netbsd-9-4-RELEASE:1.4
	netbsd-10-0-RELEASE:1.5
	netbsd-10-0-RC6:1.5
	netbsd-10-0-RC5:1.5
	netbsd-10-0-RC4:1.5
	netbsd-10-0-RC3:1.5
	netbsd-10-0-RC2:1.5
	netbsd-10-0-RC1:1.5
	netbsd-10:1.5.0.2
	netbsd-10-base:1.5
	netbsd-9-3-RELEASE:1.4
	cjep_sun2x-base1:1.4
	cjep_sun2x:1.4.0.8
	cjep_sun2x-base:1.4
	cjep_staticlib_x-base1:1.4
	netbsd-9-2-RELEASE:1.4
	cjep_staticlib_x:1.4.0.6
	cjep_staticlib_x-base:1.4
	netbsd-9-1-RELEASE:1.4
	phil-wifi-20200421:1.4
	phil-wifi-20200411:1.4
	is-mlppp:1.4.0.4
	is-mlppp-base:1.4
	phil-wifi-20200406:1.4
	netbsd-8-2-RELEASE:1.1
	netbsd-9-0-RELEASE:1.4
	netbsd-9-0-RC2:1.4
	netbsd-9-0-RC1:1.4
	phil-wifi-20191119:1.4
	netbsd-9:1.4.0.2
	netbsd-9-base:1.4
	phil-wifi-20190609:1.4
	netbsd-8-1-RELEASE:1.1
	netbsd-8-1-RC1:1.1
	pgoyette-compat-merge-20190127:1.2.2.2
	pgoyette-compat-20190127:1.4
	pgoyette-compat-20190118:1.3
	pgoyette-compat-1226:1.3
	pgoyette-compat-1126:1.3
	pgoyette-compat-1020:1.3
	pgoyette-compat-0930:1.3
	pgoyette-compat-0906:1.2
	netbsd-7-2-RELEASE:1.1
	pgoyette-compat-0728:1.2
	netbsd-8-0-RELEASE:1.1
	phil-wifi:1.2.0.4
	phil-wifi-base:1.2
	pgoyette-compat-0625:1.2
	netbsd-8-0-RC2:1.1
	pgoyette-compat-0521:1.2
	pgoyette-compat-0502:1.2
	pgoyette-compat-0422:1.2
	netbsd-8-0-RC1:1.1
	pgoyette-compat-0415:1.2
	pgoyette-compat-0407:1.2
	pgoyette-compat-0330:1.2
	pgoyette-compat-0322:1.2
	pgoyette-compat-0315:1.2
	netbsd-7-1-2-RELEASE:1.1
	pgoyette-compat:1.2.0.2
	pgoyette-compat-base:1.2
	netbsd-7-1-1-RELEASE:1.1
	matt-nb8-mediatek:1.1.0.22
	matt-nb8-mediatek-base:1.1
	perseant-stdc-iso10646:1.1.0.20
	perseant-stdc-iso10646-base:1.1
	netbsd-8:1.1.0.18
	netbsd-8-base:1.1
	prg-localcount2-base3:1.1
	prg-localcount2-base2:1.1
	prg-localcount2-base1:1.1
	prg-localcount2:1.1.0.16
	prg-localcount2-base:1.1
	pgoyette-localcount-20170426:1.1
	bouyer-socketcan-base1:1.1
	pgoyette-localcount-20170320:1.1
	netbsd-7-1:1.1.0.14
	netbsd-7-1-RELEASE:1.1
	netbsd-7-1-RC2:1.1
	netbsd-7-nhusb-base-20170116:1.1
	bouyer-socketcan:1.1.0.12
	bouyer-socketcan-base:1.1
	pgoyette-localcount-20170107:1.1
	netbsd-7-1-RC1:1.1
	pgoyette-localcount-20161104:1.1
	netbsd-7-0-2-RELEASE:1.1
	localcount-20160914:1.1
	netbsd-7-nhusb:1.1.0.10
	netbsd-7-nhusb-base:1.1
	pgoyette-localcount-20160806:1.1
	pgoyette-localcount-20160726:1.1
	pgoyette-localcount:1.1.0.8
	pgoyette-localcount-base:1.1
	netbsd-7-0-1-RELEASE:1.1
	netbsd-7-0:1.1.0.6
	netbsd-7-0-RELEASE:1.1
	netbsd-7-0-RC3:1.1
	netbsd-7-0-RC2:1.1
	netbsd-7-0-RC1:1.1
	tls-maxphys-base:1.1
	tls-maxphys:1.1.0.4
	netbsd-7:1.1.0.2
	netbsd-7-base:1.1;
locks; strict;
comment	@# @;


1.7
date	2024.07.23.18.11.53;	author riastradh;	state Exp;
branches;
next	1.6;
commitid	xPyiGeAaZeN3wZiF;

1.6
date	2024.07.22.23.18.50;	author riastradh;	state Exp;
branches;
next	1.5;
commitid	oRhd2RjSEoamfTiF;

1.5
date	2022.03.24.12.12.00;	author andvar;	state Exp;
branches
	1.5.2.1
	1.5.4.1;
next	1.4;
commitid	sk1Wh8qDMJGWTsxD;

1.4
date	2019.01.18.11.59.03;	author skrll;	state Exp;
branches
	1.4.2.1;
next	1.3;
commitid	gN3qAkY8cHxt8g8B;

1.3
date	2018.09.20.18.41.05;	author jakllsch;	state Exp;
branches;
next	1.2;
commitid	hX7B1wUA5FureSSA;

1.2
date	2018.02.04.21.49.51;	author skrll;	state Exp;
branches
	1.2.2.1
	1.2.4.1;
next	1.1;
commitid	qFAQlRPYfvSjBApA;

1.1
date	2014.08.10.05.47.37;	author matt;	state Exp;
branches
	1.1.4.1;
next	;
commitid	RVnl0fvYYjtzeMLx;

1.5.2.1
date	2024.08.07.11.00.12;	author martin;	state Exp;
branches;
next	;
commitid	jRVMntqupzW4ESkF;

1.5.4.1
date	2025.08.02.05.55.01;	author perseant;	state Exp;
branches;
next	;
commitid	23j6GFaDws3O875G;

1.4.2.1
date	2024.08.07.11.01.57;	author martin;	state Exp;
branches;
next	;
commitid	bcLmwfKbV11GESkF;

1.2.2.1
date	2018.09.30.01.45.34;	author pgoyette;	state Exp;
branches;
next	1.2.2.2;
commitid	SQ44grEPCeKPh4UA;

1.2.2.2
date	2019.01.26.21.59.58;	author pgoyette;	state Exp;
branches;
next	;
commitid	JKpcmvSjdT25dl9B;

1.2.4.1
date	2019.06.10.22.05.30;	author christos;	state Exp;
branches;
next	;
commitid	jtc8rnCzWiEEHGqB;

1.1.4.1
date	2014.08.10.05.47.37;	author tls;	state dead;
branches;
next	1.1.4.2;
commitid	jTnpym9Qu0o4R1Nx;

1.1.4.2
date	2014.08.20.00.02.22;	author tls;	state Exp;
branches;
next	;
commitid	jTnpym9Qu0o4R1Nx;


desc
@@


1.7
log
@ld.elf_so aarch64/rtld_start.S: Fix dynamic TLS fast path branch.

Bug found and patch prepared by pho@@.

PR lib/58154
@
text
@/* $NetBSD: rtld_start.S,v 1.6 2024/07/22 23:18:50 riastradh Exp $ */

/*-
 * Copyright (c) 2014 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Matt Thomas of 3am Software Foundry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*-
 * Copyright (c) 2014 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Andrew Turner under
 * sponsorship from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <machine/asm.h>

RCSID("$NetBSD: rtld_start.S,v 1.6 2024/07/22 23:18:50 riastradh Exp $")

/*
 * void _rtld_start(void (*cleanup)(void), const Obj_Entry *obj,
 *    struct ps_strings *ps_strings);
 *
 * X0		= NULL
 * X1		= NULL
 * X2		= ps_strings
 * X30 (LR)	= 0
 * X29 (FP)	= 0
 */
ENTRY_NP(_rtld_start)
	mov	x24, x2			/* save ps_strings */

	adrp	x1, :got:_DYNAMIC	/* load _DYNAMIC offset from GOT */
	ldr	x1, [x1, #:got_lo12:_DYNAMIC]

	adrp	x0, _DYNAMIC		/* get &_DYNAMIC */
	add	x0, x0, #:lo12:_DYNAMIC

	sub	x25, x0, x1		/* relocbase = &_DYNAMIC - GOT:_DYNAMIC */
	mov	x1, x25			/* pass as 2nd argument */
	bl	_C_LABEL(_rtld_relocate_nonplt_self)

	sub	sp, sp, #16		/* reserve space for returns */
	mov	x0, sp			/* pointer to reserved space */
	mov	x1, x25			/* pass relocbase */
	bl	_C_LABEL(_rtld)
	mov	x17, x0			/* save entry point */

	ldp	x0, x1, [sp], #16	/* pop cleanup & obj_main */
	mov	x2, x24			/* restore ps_strings */

	br	x17			/* call saved entry point */
END(_rtld_start)

/*
 * Upon entry from plt0 entry:
 *
 * SP+0		= &PLTGOT[n + 3]
 * SP+8		= return addr
 * X16		= &PLTGOT[2]
 */
ENTRY_NP(_rtld_bind_start)
	ldr	x9, [sp]		/* x9 = &PLTGOT[n+3] */

	/* save x0-x8 for arguments */
	stp	x0, x1, [sp, #-16]!
	stp	x2, x3, [sp, #-16]!
	stp	x4, x5, [sp, #-16]!
	stp	x6, x7, [sp, #-16]!
	stp	x8, xzr, [sp, #-16]!

	/* save q0-q7 for arguments */
	stp	q0, q1, [sp, #-32]!
	stp	q2, q3, [sp, #-32]!
	stp	q4, q5, [sp, #-32]!
	stp	q6, q7, [sp, #-32]!

	ldr	x0, [x16, #-8]	/* x0 = PLTGOT[1] */
	sub	x1, x9, x16	/* x1 = &PLTGOT[n+3] - &PLTGOT[1] = offset+8 */
	sub	x1, x1, #8	/* x1 = offset */
	lsr	x1, x1, #3	/* x1 /= sizeof(void *) */

	bl	_C_LABEL(_rtld_bind)
	mov	x17, x0		/* save result */

	/* restore q0-q7 for arguments */
	ldp	q6, q7, [sp], #32
	ldp	q4, q5, [sp], #32
	ldp	q2, q3, [sp], #32
	ldp	q0, q1, [sp], #32

	/* restore x0-x8 for arguments */
	ldp	x8, xzr, [sp], #16
	ldp	x6, x7, [sp], #16
	ldp	x4, x5, [sp], #16
	ldp	x2, x3, [sp], #16
	ldp	x0, x1, [sp], #16

	ldp	xzr, lr, [sp], #16	/* restore original lr pushed by plt0 */
	br	x17			/* call bound function */
END(_rtld_bind_start)

/*
 * Entry points used by _rtld_tlsdesc_fill.  They will be passed in x0
 * a pointer to:
 *
 *	struct rel_tlsdesc {
 *		uint64_t resolver_fnc;
 *		uint64_t resolver_arg;
 *	};
 *
 * They are called with nonstandard calling convention and must
 * preserve all registers except x0.
 */

/*
 * uint64_t@@x0
 * _rtld_tlsdesc_static(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols resolved at load time.
 *
 *	rel_tlsdesc->resolver_arg is the offset of the static
 *	thread-local storage region, relative to the start of the TCB.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
 */
ENTRY(_rtld_tlsdesc_static)
	.cfi_startproc
	ldr	x0, [x0, #8]		/* x0 := tcboffset */
	ret				/* return x0 = tcboffset */
	.cfi_endproc
END(_rtld_tlsdesc_static)

/*
 * uint64_t@@x0
 * _rtld_tlsdesc_undef(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for weak and undefined TLS symbols.
 *
 *	rel_tlsdesc->resolver_arg is the Elf_Rela rela->r_addend.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
 */
ENTRY(_rtld_tlsdesc_undef)
	.cfi_startproc
	str	x1, [sp, #-16]!		/* save x1 on stack */
	.cfi_adjust_cfa_offset	16

	mrs	x1, tpidr_el0		/* x1 := current thread tcb */
	ldr	x0, [x0, #8]		/* x0 := rela->r_addend */
	sub	x0, x0, x1		/* x0 := rela->r_addend - tcb */

	ldr	x1, [sp], #16		/* restore x1 from stack */
	.cfi_adjust_cfa_offset	-16
	.cfi_endproc
	ret				/* return x0 = rela->r_addend - tcb */
END(_rtld_tlsdesc_undef)

/*
 * uint64_t@@x0
 * _rtld_tlsdesc_dynamic(struct rel_tlsdesc *tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols from dlopen().
 *
 *	rel_tlsdesc->resolver_arg is a pointer to a struct tls_data
 *	object allocated during relocation.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
 */
ENTRY(_rtld_tlsdesc_dynamic)
	.cfi_startproc

	/* Save registers used in fast path */
	stp	x1, x2, [sp, #(-2 * 16)]!
	stp	x3, x4, [sp, #(1 * 16)]
	.cfi_adjust_cfa_offset	2 * 16
	.cfi_rel_offset		x1, 0
	.cfi_rel_offset		x2, 8
	.cfi_rel_offset		x3, 16
	.cfi_rel_offset		x4, 24

	/* Try for the fast path -- inlined version of __tls_get_addr. */

	ldr	x1, [x0, #8]		/* x1 := tlsdesc (struct tls_data *) */
	mrs	x4, tpidr_el0		/* x4 := tcb */
	ldr	x0, [x4]		/* x0 := dtv = tcb->tcb_dtv */

	ldr	x3, [x0, #-8]		/* x3 := max = DTV_MAX_INDEX(dtv) */
	ldr	x2, [x1, #0]		/* x2 := idx = tlsdesc->td_tlsindex */
	cmp	x2, x3
	b.gt	1f			/* Slow path if idx > max */

	ldr	x3, [x0, x2, lsl #3]	/* x3 := dtv[idx] */
	cbz	x3, 1f			/* Slow path if dtv[idx] is null */

	/*
	 * Fast path
	 *
	 * return (dtv[tlsdesc->td_tlsindex] + tlsdesc->td_tlsoffs - tcb)
	 */
	ldr	x2, [x1, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	add	x2, x2, x3		/* x2 := addr = dtv[idx] + offs */
	sub	x0, x2, x4		/* x0 := addr - tcb

	/* Restore fast path registers and return */
	ldp	x3, x4, [sp, #(1 * 16)]
	ldp	x1, x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset	-2 * 16
	ret				/* return x0 = addr - tcb */

	/*
	 * Slow path
	 *
	 * return _rtld_tls_get_addr(tp, tlsdesc->td_tlsindex,
	 *     tlsdesc->td_tlsoffs);
	 *
	 */
1:
	/* Save all integer registers */
	stp	x29, x30, [sp, #-(8 * 16)]!
	.cfi_adjust_cfa_offset	8 * 16
	.cfi_rel_offset		x29, 0
	.cfi_rel_offset		x30, 8

	stp	x5, x6, [sp, #(1 * 16)]
	stp	x7, x8, [sp, #(2 * 16)]
	stp	x9, x10, [sp, #(3 * 16)]
	stp	x11, x12, [sp, #(4 * 16)]
	stp	x13, x14, [sp, #(5 * 16)]
	stp	x15, x16, [sp, #(6 * 16)]
	stp	x17, x18, [sp, #(7 * 16)]
	.cfi_rel_offset		x5, 16
	.cfi_rel_offset		x6, 24
	.cfi_rel_offset		x7, 32
	.cfi_rel_offset		x8, 40
	.cfi_rel_offset		x9, 48
	.cfi_rel_offset		x10, 56
	.cfi_rel_offset		x11, 64
	.cfi_rel_offset		x12, 72
	.cfi_rel_offset		x13, 80
	.cfi_rel_offset		x14, 88
	.cfi_rel_offset		x15, 96
	.cfi_rel_offset		x16, 104
	.cfi_rel_offset		x17, 112
	.cfi_rel_offset		x18, 120

	/* Find the tls offset */
	mov	x0, x4			/* x0 := tcb */
	mov	x3, x1			/* x3 := tlsdesc */
	ldr	x1, [x3, #0]		/* x1 := idx = tlsdesc->td_tlsindex */
	ldr	x2, [x3, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	bl	_rtld_tls_get_addr	/* x0 := addr = _rtld_tls_get_addr(tcb,
					 *     idx, offs) */
	mrs	x1, tpidr_el0		/* x1 := tcb */
	sub	x0, x0, x1		/* x0 := addr - tcb */

	/* Restore slow path registers */
	ldp	x17, x18, [sp, #(7 * 16)]
	ldp	x15, x16, [sp, #(6 * 16)]
	ldp	x13, x14, [sp, #(5 * 16)]
	ldp	x11, x12, [sp, #(4 * 16)]
	ldp	x9, x10, [sp, #(3 * 16)]
	ldp	x7, x8, [sp, #(2 * 16)]
	ldp	x5, x6, [sp, #(1 * 16)]
	ldp	x29, x30, [sp], #(8 * 16)
	.cfi_adjust_cfa_offset	-8 * 16
	.cfi_restore		x29
	.cfi_restore		x30

	/* Restore fast path registers and return */
	ldp	x3, x4, [sp, #16]
	ldp	x1, x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset	-2 * 16
	.cfi_endproc
	ret				/* return x0 = addr - tcb */
END(_rtld_tlsdesc_dynamic)
@


1.6
log
@ld.elf_so aarch64/rtld_start.S: Sprinkle comments.

No functional change intended.

Prompted by PR lib/58154.
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.5 2022/03/24 12:12:00 andvar Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.5 2022/03/24 12:12:00 andvar Exp $")
d239 1
a239 2
	b.lt	1f			/* Slow path if idx < max */
					/* XXX PR lib/58154 */
@


1.5
log
@s/interger/integer/ and s/Compensatin/Compensation/ in comments.
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.4 2019/01/18 11:59:03 skrll Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.4 2019/01/18 11:59:03 skrll Exp $")
d149 2
a150 3
 * struct rel_tlsdesc {
 *  uint64_t resolver_fnc;
 *  uint64_t resolver_arg;
d152 4
d157 9
a165 1
 * uint64_t _rtld_tlsdesc_static(struct rel_tlsdesc *);
d167 5
a171 1
 * Resolver function for TLS symbols resolved at load time
d175 2
a176 2
	ldr	x0, [x0, #8]
	ret
d181 4
a184 1
 * uint64_t _rtld_tlsdesc_undef(void);
d186 4
a189 1
 * Resolver function for weak and undefined TLS symbols
d193 1
a193 1
	str	x1, [sp, #-16]!
d196 3
a198 3
	mrs	x1, tpidr_el0
	ldr	x0, [x0, #8]
	sub	x0, x0, x1
d200 2
a201 2
	ldr	x1, [sp], #16
	.cfi_adjust_cfa_offset 	-16
d203 1
a203 1
	ret
d207 4
a210 1
 * uint64_t _rtld_tlsdesc_dynamic(struct rel_tlsdesc *);
d212 5
a216 1
 * Resolver function for TLS symbols from dlopen()
d222 2
a223 2
	stp	x1,  x2, [sp, #(-2 * 16)]!
	stp	x3,  x4, [sp, #(1 * 16)]
d230 1
a230 1
	/* Test fastpath - inlined version of __tls_get_addr. */
d232 3
a234 3
	ldr	x1, [x0, #8]		/* tlsdesc ptr */
	mrs	x4, tpidr_el0
	ldr	x0, [x4]		/* DTV pointer (tcb->tcb_dtv) */
d236 2
a237 2
	ldr	x3, [x0, #-8]		/* DTV_MAX_INDEX(dtv) */
	ldr	x2, [x1, #0]		/* tlsdesc->td_tlsindex */
d239 5
a243 1
	b.lt	1f			/* Slow path */
d245 8
a252 2
	ldr     x3, [x0, x2, lsl #3]	/* dtv[tlsdesc->td_tlsindex] */
	cbz	x3, 1f
d254 5
a258 10
	/* Return (dtv[tlsdesc->td_tlsindex] + tlsdesc->td_tlsoffs - tp) */
	ldr	x2, [x1, #8]		/* tlsdesc->td_tlsoffs */
	add 	x2, x2, x3
	sub	x0, x2, x4

	/* Restore registers and return */
	ldp	 x3,  x4, [sp, #(1 * 16)]
	ldp	 x1,  x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset 	-2 * 16
	ret
d262 3
a264 1
	 * return _rtld_tls_get_addr(tp, tlsdesc->td_tlsindex, tlsdesc->td_tlsoffs);
d274 3
a276 3
	stp	x5,   x6, [sp, #(1 * 16)]
	stp	x7,   x8, [sp, #(2 * 16)]
	stp	x9,  x10, [sp, #(3 * 16)]
d281 5
a285 5
	.cfi_rel_offset		 x5, 16
	.cfi_rel_offset		 x6, 24
	.cfi_rel_offset		 x7, 32
	.cfi_rel_offset		 x8, 40
	.cfi_rel_offset		 x9, 48
d297 8
a304 7
	mov	x0, x4			/* tp */
	mov	x3, x1			/* tlsdesc ptr */
	ldr	x1, [x3, #0]		/* tlsdesc->td_tlsindex */
	ldr	x2, [x3, #8]		/* tlsdesc->td_tlsoffs */
	bl	_rtld_tls_get_addr
	mrs	x1, tpidr_el0
	sub	x0, x0, x1
d311 3
a313 3
	ldp	x9, x10,  [sp, #(3 * 16)]
	ldp	x7, x8,   [sp, #(2 * 16)]
	ldp	x5, x6,   [sp, #(1 * 16)]
d315 1
a315 1
	.cfi_adjust_cfa_offset 	-8 * 16
d320 2
a321 2
	ldp	 x3,  x4, [sp, #16]
	ldp	 x1,  x2, [sp], #(2 * 16)
d324 1
a324 1
	ret
@


1.5.4.1
log
@Sync with HEAD
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.7 2024/07/23 18:11:53 riastradh Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.7 2024/07/23 18:11:53 riastradh Exp $")
d149 3
a151 2
 * Entry points used by _rtld_tlsdesc_fill.  They will be passed in x0
 * a pointer to:
a152 4
 *	struct rel_tlsdesc {
 *		uint64_t resolver_fnc;
 *		uint64_t resolver_arg;
 *	};
d154 1
a154 9
 * They are called with nonstandard calling convention and must
 * preserve all registers except x0.
 */

/*
 * uint64_t@@x0
 * _rtld_tlsdesc_static(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols resolved at load time.
d156 1
a156 5
 *	rel_tlsdesc->resolver_arg is the offset of the static
 *	thread-local storage region, relative to the start of the TCB.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d160 2
a161 2
	ldr	x0, [x0, #8]		/* x0 := tcboffset */
	ret				/* return x0 = tcboffset */
d166 1
a166 4
 * uint64_t@@x0
 * _rtld_tlsdesc_undef(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for weak and undefined TLS symbols.
d168 1
a168 4
 *	rel_tlsdesc->resolver_arg is the Elf_Rela rela->r_addend.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d172 1
a172 1
	str	x1, [sp, #-16]!		/* save x1 on stack */
d175 3
a177 3
	mrs	x1, tpidr_el0		/* x1 := current thread tcb */
	ldr	x0, [x0, #8]		/* x0 := rela->r_addend */
	sub	x0, x0, x1		/* x0 := rela->r_addend - tcb */
d179 2
a180 2
	ldr	x1, [sp], #16		/* restore x1 from stack */
	.cfi_adjust_cfa_offset	-16
d182 1
a182 1
	ret				/* return x0 = rela->r_addend - tcb */
d186 1
a186 4
 * uint64_t@@x0
 * _rtld_tlsdesc_dynamic(struct rel_tlsdesc *tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols from dlopen().
d188 1
a188 5
 *	rel_tlsdesc->resolver_arg is a pointer to a struct tls_data
 *	object allocated during relocation.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d194 2
a195 2
	stp	x1, x2, [sp, #(-2 * 16)]!
	stp	x3, x4, [sp, #(1 * 16)]
d202 1
a202 1
	/* Try for the fast path -- inlined version of __tls_get_addr. */
d204 3
a206 3
	ldr	x1, [x0, #8]		/* x1 := tlsdesc (struct tls_data *) */
	mrs	x4, tpidr_el0		/* x4 := tcb */
	ldr	x0, [x4]		/* x0 := dtv = tcb->tcb_dtv */
d208 2
a209 2
	ldr	x3, [x0, #-8]		/* x3 := max = DTV_MAX_INDEX(dtv) */
	ldr	x2, [x1, #0]		/* x2 := idx = tlsdesc->td_tlsindex */
d211 1
a211 4
	b.gt	1f			/* Slow path if idx > max */

	ldr	x3, [x0, x2, lsl #3]	/* x3 := dtv[idx] */
	cbz	x3, 1f			/* Slow path if dtv[idx] is null */
d213 2
a214 8
	/*
	 * Fast path
	 *
	 * return (dtv[tlsdesc->td_tlsindex] + tlsdesc->td_tlsoffs - tcb)
	 */
	ldr	x2, [x1, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	add	x2, x2, x3		/* x2 := addr = dtv[idx] + offs */
	sub	x0, x2, x4		/* x0 := addr - tcb
d216 10
a225 5
	/* Restore fast path registers and return */
	ldp	x3, x4, [sp, #(1 * 16)]
	ldp	x1, x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset	-2 * 16
	ret				/* return x0 = addr - tcb */
d229 1
a229 3
	 *
	 * return _rtld_tls_get_addr(tp, tlsdesc->td_tlsindex,
	 *     tlsdesc->td_tlsoffs);
d239 3
a241 3
	stp	x5, x6, [sp, #(1 * 16)]
	stp	x7, x8, [sp, #(2 * 16)]
	stp	x9, x10, [sp, #(3 * 16)]
d246 5
a250 5
	.cfi_rel_offset		x5, 16
	.cfi_rel_offset		x6, 24
	.cfi_rel_offset		x7, 32
	.cfi_rel_offset		x8, 40
	.cfi_rel_offset		x9, 48
d262 7
a268 8
	mov	x0, x4			/* x0 := tcb */
	mov	x3, x1			/* x3 := tlsdesc */
	ldr	x1, [x3, #0]		/* x1 := idx = tlsdesc->td_tlsindex */
	ldr	x2, [x3, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	bl	_rtld_tls_get_addr	/* x0 := addr = _rtld_tls_get_addr(tcb,
					 *     idx, offs) */
	mrs	x1, tpidr_el0		/* x1 := tcb */
	sub	x0, x0, x1		/* x0 := addr - tcb */
d275 3
a277 3
	ldp	x9, x10, [sp, #(3 * 16)]
	ldp	x7, x8, [sp, #(2 * 16)]
	ldp	x5, x6, [sp, #(1 * 16)]
d279 1
a279 1
	.cfi_adjust_cfa_offset	-8 * 16
d284 2
a285 2
	ldp	x3, x4, [sp, #16]
	ldp	x1, x2, [sp], #(2 * 16)
d288 1
a288 1
	ret				/* return x0 = addr - tcb */
@


1.5.2.1
log
@Pull up following revision(s) (requested by riastradh in ticket #777):

	libexec/ld.elf_so/tls.c: revision 1.15
	libexec/ld.elf_so/arch/aarch64/rtld_start.S: revision 1.6
	libexec/ld.elf_so/arch/aarch64/rtld_start.S: revision 1.7
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.15
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.16
	libexec/ld.elf_so/README.TLS: revision 1.7
	libexec/ld.elf_so/tls.c: revision 1.20
	libexec/ld.elf_so/tls.c: revision 1.21

Alignment. NFCI.

ld.elf_so: Sprinkle comments and references for thread-local storage.

Maybe this will help the TLS business to be less mysterious to the
next traveller to pass by here.
Prompted by PR lib/58154.

ld.elf_so: Add comments explaining DTV allocation size.
Patch by pho@@ for PR lib/58154.

tests/libexec/ld.elf_so/t_tls_extern: Test PR lib/58154.

ld.elf_so aarch64/rtld_start.S: Sprinkle comments.
No functional change intended.
Prompted by PR lib/58154.

ld.elf_so aarch64/rtld_start.S: Fix dynamic TLS fast path branch.
Bug found and patch prepared by pho@@.
PR lib/58154
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.5 2022/03/24 12:12:00 andvar Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.5 2022/03/24 12:12:00 andvar Exp $")
d149 3
a151 2
 * Entry points used by _rtld_tlsdesc_fill.  They will be passed in x0
 * a pointer to:
a152 4
 *	struct rel_tlsdesc {
 *		uint64_t resolver_fnc;
 *		uint64_t resolver_arg;
 *	};
d154 1
a154 9
 * They are called with nonstandard calling convention and must
 * preserve all registers except x0.
 */

/*
 * uint64_t@@x0
 * _rtld_tlsdesc_static(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols resolved at load time.
d156 1
a156 5
 *	rel_tlsdesc->resolver_arg is the offset of the static
 *	thread-local storage region, relative to the start of the TCB.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d160 2
a161 2
	ldr	x0, [x0, #8]		/* x0 := tcboffset */
	ret				/* return x0 = tcboffset */
d166 1
a166 4
 * uint64_t@@x0
 * _rtld_tlsdesc_undef(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for weak and undefined TLS symbols.
d168 1
a168 4
 *	rel_tlsdesc->resolver_arg is the Elf_Rela rela->r_addend.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d172 1
a172 1
	str	x1, [sp, #-16]!		/* save x1 on stack */
d175 3
a177 3
	mrs	x1, tpidr_el0		/* x1 := current thread tcb */
	ldr	x0, [x0, #8]		/* x0 := rela->r_addend */
	sub	x0, x0, x1		/* x0 := rela->r_addend - tcb */
d179 2
a180 2
	ldr	x1, [sp], #16		/* restore x1 from stack */
	.cfi_adjust_cfa_offset	-16
d182 1
a182 1
	ret				/* return x0 = rela->r_addend - tcb */
d186 1
a186 4
 * uint64_t@@x0
 * _rtld_tlsdesc_dynamic(struct rel_tlsdesc *tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols from dlopen().
d188 1
a188 5
 *	rel_tlsdesc->resolver_arg is a pointer to a struct tls_data
 *	object allocated during relocation.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d194 2
a195 2
	stp	x1, x2, [sp, #(-2 * 16)]!
	stp	x3, x4, [sp, #(1 * 16)]
d202 1
a202 1
	/* Try for the fast path -- inlined version of __tls_get_addr. */
d204 3
a206 3
	ldr	x1, [x0, #8]		/* x1 := tlsdesc (struct tls_data *) */
	mrs	x4, tpidr_el0		/* x4 := tcb */
	ldr	x0, [x4]		/* x0 := dtv = tcb->tcb_dtv */
d208 2
a209 2
	ldr	x3, [x0, #-8]		/* x3 := max = DTV_MAX_INDEX(dtv) */
	ldr	x2, [x1, #0]		/* x2 := idx = tlsdesc->td_tlsindex */
d211 1
a211 4
	b.gt	1f			/* Slow path if idx > max */

	ldr	x3, [x0, x2, lsl #3]	/* x3 := dtv[idx] */
	cbz	x3, 1f			/* Slow path if dtv[idx] is null */
d213 2
a214 8
	/*
	 * Fast path
	 *
	 * return (dtv[tlsdesc->td_tlsindex] + tlsdesc->td_tlsoffs - tcb)
	 */
	ldr	x2, [x1, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	add	x2, x2, x3		/* x2 := addr = dtv[idx] + offs */
	sub	x0, x2, x4		/* x0 := addr - tcb
d216 10
a225 5
	/* Restore fast path registers and return */
	ldp	x3, x4, [sp, #(1 * 16)]
	ldp	x1, x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset	-2 * 16
	ret				/* return x0 = addr - tcb */
d229 1
a229 3
	 *
	 * return _rtld_tls_get_addr(tp, tlsdesc->td_tlsindex,
	 *     tlsdesc->td_tlsoffs);
d239 3
a241 3
	stp	x5, x6, [sp, #(1 * 16)]
	stp	x7, x8, [sp, #(2 * 16)]
	stp	x9, x10, [sp, #(3 * 16)]
d246 5
a250 5
	.cfi_rel_offset		x5, 16
	.cfi_rel_offset		x6, 24
	.cfi_rel_offset		x7, 32
	.cfi_rel_offset		x8, 40
	.cfi_rel_offset		x9, 48
d262 7
a268 8
	mov	x0, x4			/* x0 := tcb */
	mov	x3, x1			/* x3 := tlsdesc */
	ldr	x1, [x3, #0]		/* x1 := idx = tlsdesc->td_tlsindex */
	ldr	x2, [x3, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	bl	_rtld_tls_get_addr	/* x0 := addr = _rtld_tls_get_addr(tcb,
					 *     idx, offs) */
	mrs	x1, tpidr_el0		/* x1 := tcb */
	sub	x0, x0, x1		/* x0 := addr - tcb */
d275 3
a277 3
	ldp	x9, x10, [sp, #(3 * 16)]
	ldp	x7, x8, [sp, #(2 * 16)]
	ldp	x5, x6, [sp, #(1 * 16)]
d279 1
a279 1
	.cfi_adjust_cfa_offset	-8 * 16
d284 2
a285 2
	ldp	x3, x4, [sp, #16]
	ldp	x1, x2, [sp], #(2 * 16)
d288 1
a288 1
	ret				/* return x0 = addr - tcb */
@


1.4
log
@Adapt https://svnweb.freebsd.org/base?view=revision&revision=342113 to
NetBSD.

This brings us resolving for dynamically loaded libraries and makes
tests/libexec/ld.elf_so/t_thread_local_dtor pass.

With suggestions from joerg@@
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.3 2018/09/20 18:41:05 jakllsch Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.3 2018/09/20 18:41:05 jakllsch Exp $")
d233 1
a233 1
	/* Save all interger registers */
@


1.4.2.1
log
@Pull up following revision(s) (requested by riastradh in ticket #1864):

	libexec/ld.elf_so/tls.c: revision 1.15
	libexec/ld.elf_so/arch/aarch64/rtld_start.S: revision 1.6
	libexec/ld.elf_so/arch/aarch64/rtld_start.S: revision 1.7
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.15
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.16
	libexec/ld.elf_so/README.TLS: revision 1.7
	libexec/ld.elf_so/tls.c: revision 1.20
	libexec/ld.elf_so/tls.c: revision 1.21

Alignment. NFCI.

ld.elf_so: Sprinkle comments and references for thread-local storage.

Maybe this will help the TLS business to be less mysterious to the
next traveller to pass by here.
Prompted by PR lib/58154.

ld.elf_so: Add comments explaining DTV allocation size.
Patch by pho@@ for PR lib/58154.

tests/libexec/ld.elf_so/t_tls_extern: Test PR lib/58154.

ld.elf_so aarch64/rtld_start.S: Sprinkle comments.
No functional change intended.
Prompted by PR lib/58154.

ld.elf_so aarch64/rtld_start.S: Fix dynamic TLS fast path branch.
Bug found and patch prepared by pho@@.
PR lib/58154
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.4 2019/01/18 11:59:03 skrll Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.4 2019/01/18 11:59:03 skrll Exp $")
d149 3
a151 2
 * Entry points used by _rtld_tlsdesc_fill.  They will be passed in x0
 * a pointer to:
a152 4
 *	struct rel_tlsdesc {
 *		uint64_t resolver_fnc;
 *		uint64_t resolver_arg;
 *	};
d154 1
a154 9
 * They are called with nonstandard calling convention and must
 * preserve all registers except x0.
 */

/*
 * uint64_t@@x0
 * _rtld_tlsdesc_static(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols resolved at load time.
d156 1
a156 5
 *	rel_tlsdesc->resolver_arg is the offset of the static
 *	thread-local storage region, relative to the start of the TCB.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d160 2
a161 2
	ldr	x0, [x0, #8]		/* x0 := tcboffset */
	ret				/* return x0 = tcboffset */
d166 1
a166 4
 * uint64_t@@x0
 * _rtld_tlsdesc_undef(struct rel_tlsdesc *rel_tlsdesc@@x0);
 *
 *	Resolver function for weak and undefined TLS symbols.
d168 1
a168 4
 *	rel_tlsdesc->resolver_arg is the Elf_Rela rela->r_addend.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d172 1
a172 1
	str	x1, [sp, #-16]!		/* save x1 on stack */
d175 3
a177 3
	mrs	x1, tpidr_el0		/* x1 := current thread tcb */
	ldr	x0, [x0, #8]		/* x0 := rela->r_addend */
	sub	x0, x0, x1		/* x0 := rela->r_addend - tcb */
d179 2
a180 2
	ldr	x1, [sp], #16		/* restore x1 from stack */
	.cfi_adjust_cfa_offset	-16
d182 1
a182 1
	ret				/* return x0 = rela->r_addend - tcb */
d186 1
a186 4
 * uint64_t@@x0
 * _rtld_tlsdesc_dynamic(struct rel_tlsdesc *tlsdesc@@x0);
 *
 *	Resolver function for TLS symbols from dlopen().
d188 1
a188 5
 *	rel_tlsdesc->resolver_arg is a pointer to a struct tls_data
 *	object allocated during relocation.
 *
 *	Nonstandard calling convention: Must preserve all registers
 *	except x0.
d194 2
a195 2
	stp	x1, x2, [sp, #(-2 * 16)]!
	stp	x3, x4, [sp, #(1 * 16)]
d202 1
a202 1
	/* Try for the fast path -- inlined version of __tls_get_addr. */
d204 3
a206 3
	ldr	x1, [x0, #8]		/* x1 := tlsdesc (struct tls_data *) */
	mrs	x4, tpidr_el0		/* x4 := tcb */
	ldr	x0, [x4]		/* x0 := dtv = tcb->tcb_dtv */
d208 2
a209 2
	ldr	x3, [x0, #-8]		/* x3 := max = DTV_MAX_INDEX(dtv) */
	ldr	x2, [x1, #0]		/* x2 := idx = tlsdesc->td_tlsindex */
d211 1
a211 4
	b.gt	1f			/* Slow path if idx > max */

	ldr	x3, [x0, x2, lsl #3]	/* x3 := dtv[idx] */
	cbz	x3, 1f			/* Slow path if dtv[idx] is null */
d213 2
a214 8
	/*
	 * Fast path
	 *
	 * return (dtv[tlsdesc->td_tlsindex] + tlsdesc->td_tlsoffs - tcb)
	 */
	ldr	x2, [x1, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	add	x2, x2, x3		/* x2 := addr = dtv[idx] + offs */
	sub	x0, x2, x4		/* x0 := addr - tcb
d216 10
a225 5
	/* Restore fast path registers and return */
	ldp	x3, x4, [sp, #(1 * 16)]
	ldp	x1, x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset	-2 * 16
	ret				/* return x0 = addr - tcb */
d229 1
a229 3
	 *
	 * return _rtld_tls_get_addr(tp, tlsdesc->td_tlsindex,
	 *     tlsdesc->td_tlsoffs);
d239 3
a241 3
	stp	x5, x6, [sp, #(1 * 16)]
	stp	x7, x8, [sp, #(2 * 16)]
	stp	x9, x10, [sp, #(3 * 16)]
d246 5
a250 5
	.cfi_rel_offset		x5, 16
	.cfi_rel_offset		x6, 24
	.cfi_rel_offset		x7, 32
	.cfi_rel_offset		x8, 40
	.cfi_rel_offset		x9, 48
d262 7
a268 8
	mov	x0, x4			/* x0 := tcb */
	mov	x3, x1			/* x3 := tlsdesc */
	ldr	x1, [x3, #0]		/* x1 := idx = tlsdesc->td_tlsindex */
	ldr	x2, [x3, #8]		/* x2 := offs = tlsdesc->td_tlsoffs */
	bl	_rtld_tls_get_addr	/* x0 := addr = _rtld_tls_get_addr(tcb,
					 *     idx, offs) */
	mrs	x1, tpidr_el0		/* x1 := tcb */
	sub	x0, x0, x1		/* x0 := addr - tcb */
d275 3
a277 3
	ldp	x9, x10, [sp, #(3 * 16)]
	ldp	x7, x8, [sp, #(2 * 16)]
	ldp	x5, x6, [sp, #(1 * 16)]
d279 1
a279 1
	.cfi_adjust_cfa_offset	-8 * 16
d284 2
a285 2
	ldp	x3, x4, [sp, #16]
	ldp	x1, x2, [sp], #(2 * 16)
d288 1
a288 1
	ret				/* return x0 = addr - tcb */
@


1.3
log
@Improve support for R_AARCH64_TLSDESC relocations.

In large part from FreeBSD.
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.2 2018/02/04 21:49:51 skrll Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.2 2018/02/04 21:49:51 skrll Exp $")
d148 26
d175 1
a175 1
ENTRY(_rtld_tlsdesc)
d177 5
d183 1
a183 1
END(_rtld_tlsdesc)
d186 1
a186 1
 * uint64_t _rtld_tlsdesc_dynamic(struct tlsdesc *);
d188 1
a188 1
 * TODO: We could lookup the saved index here to skip saving the entire stack.
d191 1
a191 12
	/* Store any registers we may use in rtld_tlsdesc_handle */
	stp	x29, x30, [sp, #-(10 * 16)]!
	mov	x29, sp
	stp	x1, x2,   [sp, #(1 * 16)]
	stp	x3, x4,   [sp, #(2 * 16)]
	stp	x5, x6,   [sp, #(3 * 16)]
	stp	x7, x8,   [sp, #(4 * 16)]
	stp	x9, x10,  [sp, #(5 * 16)]
	stp	x11, x12, [sp, #(6 * 16)]
	stp	x13, x14, [sp, #(7 * 16)]
	stp	x15, x16, [sp, #(8 * 16)]
	stp	x17, x18, [sp, #(9 * 16)]
d193 33
a225 4
	/* Find the tls offset */
	ldr	x0, [x0, #8]
	mov	x1, #1
	bl	_rtld_tlsdesc_handle
d227 33
a259 11
	/* Restore the registers */
	ldp	x17, x18, [sp, #(9 * 16)]
	ldp	x15, x16, [sp, #(8 * 16)]
	ldp	x13, x14, [sp, #(7 * 16)]
	ldp	x11, x12, [sp, #(6 * 16)]
	ldp	x9, x10,  [sp, #(5 * 16)]
	ldp	x7, x8,   [sp, #(4 * 16)]
	ldp	x5, x6,   [sp, #(3 * 16)]
	ldp	x3, x4,   [sp, #(2 * 16)]
	ldp	x1, x2,   [sp, #(1 * 16)]
	ldp	x29, x30, [sp], #(10 * 16)
d261 27
@


1.2
log
@Various fixes / changes from Ryo Shimizu / Toru Nishimura to make this
work.

_rtld_call_ifunc copied from other rela platforms by me - not tested in
any way.
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.1 2014/08/10 05:47:37 matt Exp $ */
d32 29
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.1 2014/08/10 05:47:37 matt Exp $")
d153 39
@


1.2.4.1
log
@Sync with HEAD
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.4 2019/01/18 11:59:03 skrll Exp $ */
a31 29
/*-
 * Copyright (c) 2014 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Andrew Turner under
 * sponsorship from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

d34 1
a34 1
RCSID("$NetBSD: rtld_start.S,v 1.4 2019/01/18 11:59:03 skrll Exp $")
a118 16
/*
 * struct rel_tlsdesc {
 *  uint64_t resolver_fnc;
 *  uint64_t resolver_arg;
 *
 *
 * uint64_t _rtld_tlsdesc_static(struct rel_tlsdesc *);
 *
 * Resolver function for TLS symbols resolved at load time
 */
ENTRY(_rtld_tlsdesc_static)
	.cfi_startproc
	ldr	x0, [x0, #8]
	ret
	.cfi_endproc
END(_rtld_tlsdesc_static)
d120 1
a120 11
/*
 * uint64_t _rtld_tlsdesc_undef(void);
 *
 * Resolver function for weak and undefined TLS symbols
 */
ENTRY(_rtld_tlsdesc_undef)
	.cfi_startproc
	str	x1, [sp, #-16]!
	.cfi_adjust_cfa_offset	16

	mrs	x1, tpidr_el0
a121 111
	sub	x0, x0, x1

	ldr	x1, [sp], #16
	.cfi_adjust_cfa_offset 	-16
	.cfi_endproc
	ret
END(_rtld_tlsdesc_undef)

/*
 * uint64_t _rtld_tlsdesc_dynamic(struct rel_tlsdesc *);
 *
 * Resolver function for TLS symbols from dlopen()
 */
ENTRY(_rtld_tlsdesc_dynamic)
	.cfi_startproc

	/* Save registers used in fast path */
	stp	x1,  x2, [sp, #(-2 * 16)]!
	stp	x3,  x4, [sp, #(1 * 16)]
	.cfi_adjust_cfa_offset	2 * 16
	.cfi_rel_offset		x1, 0
	.cfi_rel_offset		x2, 8
	.cfi_rel_offset		x3, 16
	.cfi_rel_offset		x4, 24

	/* Test fastpath - inlined version of __tls_get_addr. */

	ldr	x1, [x0, #8]		/* tlsdesc ptr */
	mrs	x4, tpidr_el0
	ldr	x0, [x4]		/* DTV pointer (tcb->tcb_dtv) */

	ldr	x3, [x0, #-8]		/* DTV_MAX_INDEX(dtv) */
	ldr	x2, [x1, #0]		/* tlsdesc->td_tlsindex */
	cmp	x2, x3
	b.lt	1f			/* Slow path */

	ldr     x3, [x0, x2, lsl #3]	/* dtv[tlsdesc->td_tlsindex] */
	cbz	x3, 1f

	/* Return (dtv[tlsdesc->td_tlsindex] + tlsdesc->td_tlsoffs - tp) */
	ldr	x2, [x1, #8]		/* tlsdesc->td_tlsoffs */
	add 	x2, x2, x3
	sub	x0, x2, x4

	/* Restore registers and return */
	ldp	 x3,  x4, [sp, #(1 * 16)]
	ldp	 x1,  x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset 	-2 * 16
	ret

	/*
	 * Slow path
	 * return _rtld_tls_get_addr(tp, tlsdesc->td_tlsindex, tlsdesc->td_tlsoffs);
	 *
	 */
1:
	/* Save all interger registers */
	stp	x29, x30, [sp, #-(8 * 16)]!
	.cfi_adjust_cfa_offset	8 * 16
	.cfi_rel_offset		x29, 0
	.cfi_rel_offset		x30, 8

	stp	x5,   x6, [sp, #(1 * 16)]
	stp	x7,   x8, [sp, #(2 * 16)]
	stp	x9,  x10, [sp, #(3 * 16)]
	stp	x11, x12, [sp, #(4 * 16)]
	stp	x13, x14, [sp, #(5 * 16)]
	stp	x15, x16, [sp, #(6 * 16)]
	stp	x17, x18, [sp, #(7 * 16)]
	.cfi_rel_offset		 x5, 16
	.cfi_rel_offset		 x6, 24
	.cfi_rel_offset		 x7, 32
	.cfi_rel_offset		 x8, 40
	.cfi_rel_offset		 x9, 48
	.cfi_rel_offset		x10, 56
	.cfi_rel_offset		x11, 64
	.cfi_rel_offset		x12, 72
	.cfi_rel_offset		x13, 80
	.cfi_rel_offset		x14, 88
	.cfi_rel_offset		x15, 96
	.cfi_rel_offset		x16, 104
	.cfi_rel_offset		x17, 112
	.cfi_rel_offset		x18, 120

	/* Find the tls offset */
	mov	x0, x4			/* tp */
	mov	x3, x1			/* tlsdesc ptr */
	ldr	x1, [x3, #0]		/* tlsdesc->td_tlsindex */
	ldr	x2, [x3, #8]		/* tlsdesc->td_tlsoffs */
	bl	_rtld_tls_get_addr
	mrs	x1, tpidr_el0
	sub	x0, x0, x1

	/* Restore slow path registers */
	ldp	x17, x18, [sp, #(7 * 16)]
	ldp	x15, x16, [sp, #(6 * 16)]
	ldp	x13, x14, [sp, #(5 * 16)]
	ldp	x11, x12, [sp, #(4 * 16)]
	ldp	x9, x10,  [sp, #(3 * 16)]
	ldp	x7, x8,   [sp, #(2 * 16)]
	ldp	x5, x6,   [sp, #(1 * 16)]
	ldp	x29, x30, [sp], #(8 * 16)
	.cfi_adjust_cfa_offset 	-8 * 16
	.cfi_restore		x29
	.cfi_restore		x30

	/* Restore fast path registers and return */
	ldp	 x3,  x4, [sp, #16]
	ldp	 x1,  x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset	-2 * 16
	.cfi_endproc
d123 1
a123 1
END(_rtld_tlsdesc_dynamic)
@


1.2.2.1
log
@Ssync with HEAD
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.3 2018/09/20 18:41:05 jakllsch Exp $ */
a31 29
/*-
 * Copyright (c) 2014 The FreeBSD Foundation
 * All rights reserved.
 *
 * This software was developed by Andrew Turner under
 * sponsorship from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

d34 1
a34 1
RCSID("$NetBSD: rtld_start.S,v 1.3 2018/09/20 18:41:05 jakllsch Exp $")
a123 39

/*
 * uint64_t _rtld_tlsdesc_dynamic(struct tlsdesc *);
 *
 * TODO: We could lookup the saved index here to skip saving the entire stack.
 */
ENTRY(_rtld_tlsdesc_dynamic)
	/* Store any registers we may use in rtld_tlsdesc_handle */
	stp	x29, x30, [sp, #-(10 * 16)]!
	mov	x29, sp
	stp	x1, x2,   [sp, #(1 * 16)]
	stp	x3, x4,   [sp, #(2 * 16)]
	stp	x5, x6,   [sp, #(3 * 16)]
	stp	x7, x8,   [sp, #(4 * 16)]
	stp	x9, x10,  [sp, #(5 * 16)]
	stp	x11, x12, [sp, #(6 * 16)]
	stp	x13, x14, [sp, #(7 * 16)]
	stp	x15, x16, [sp, #(8 * 16)]
	stp	x17, x18, [sp, #(9 * 16)]

	/* Find the tls offset */
	ldr	x0, [x0, #8]
	mov	x1, #1
	bl	_rtld_tlsdesc_handle

	/* Restore the registers */
	ldp	x17, x18, [sp, #(9 * 16)]
	ldp	x15, x16, [sp, #(8 * 16)]
	ldp	x13, x14, [sp, #(7 * 16)]
	ldp	x11, x12, [sp, #(6 * 16)]
	ldp	x9, x10,  [sp, #(5 * 16)]
	ldp	x7, x8,   [sp, #(4 * 16)]
	ldp	x5, x6,   [sp, #(3 * 16)]
	ldp	x3, x4,   [sp, #(2 * 16)]
	ldp	x1, x2,   [sp, #(1 * 16)]
	ldp	x29, x30, [sp], #(10 * 16)

	ret
END(_rtld_tlsdesc_dynamic)
@


1.2.2.2
log
@Sync with HEAD
@
text
@d1 1
a1 1
/* $NetBSD: rtld_start.S,v 1.2.2.1 2018/09/30 01:45:34 pgoyette Exp $ */
d63 1
a63 1
RCSID("$NetBSD: rtld_start.S,v 1.2.2.1 2018/09/30 01:45:34 pgoyette Exp $")
a147 26
/*
 * struct rel_tlsdesc {
 *  uint64_t resolver_fnc;
 *  uint64_t resolver_arg;
 *
 *
 * uint64_t _rtld_tlsdesc_static(struct rel_tlsdesc *);
 *
 * Resolver function for TLS symbols resolved at load time
 */
ENTRY(_rtld_tlsdesc_static)
	.cfi_startproc
	ldr	x0, [x0, #8]
	ret
	.cfi_endproc
END(_rtld_tlsdesc_static)

/*
 * uint64_t _rtld_tlsdesc_undef(void);
 *
 * Resolver function for weak and undefined TLS symbols
 */
ENTRY(_rtld_tlsdesc_undef)
	.cfi_startproc
	str	x1, [sp, #-16]!
	.cfi_adjust_cfa_offset	16
d149 1
a149 1
	mrs	x1, tpidr_el0
a150 5
	sub	x0, x0, x1

	ldr	x1, [sp], #16
	.cfi_adjust_cfa_offset 	-16
	.cfi_endproc
d152 1
a152 1
END(_rtld_tlsdesc_undef)
d155 1
a155 1
 * uint64_t _rtld_tlsdesc_dynamic(struct rel_tlsdesc *);
d157 1
a157 1
 * Resolver function for TLS symbols from dlopen()
d160 12
a171 1
	.cfi_startproc
d173 4
a176 33
	/* Save registers used in fast path */
	stp	x1,  x2, [sp, #(-2 * 16)]!
	stp	x3,  x4, [sp, #(1 * 16)]
	.cfi_adjust_cfa_offset	2 * 16
	.cfi_rel_offset		x1, 0
	.cfi_rel_offset		x2, 8
	.cfi_rel_offset		x3, 16
	.cfi_rel_offset		x4, 24

	/* Test fastpath - inlined version of __tls_get_addr. */

	ldr	x1, [x0, #8]		/* tlsdesc ptr */
	mrs	x4, tpidr_el0
	ldr	x0, [x4]		/* DTV pointer (tcb->tcb_dtv) */

	ldr	x3, [x0, #-8]		/* DTV_MAX_INDEX(dtv) */
	ldr	x2, [x1, #0]		/* tlsdesc->td_tlsindex */
	cmp	x2, x3
	b.lt	1f			/* Slow path */

	ldr     x3, [x0, x2, lsl #3]	/* dtv[tlsdesc->td_tlsindex] */
	cbz	x3, 1f

	/* Return (dtv[tlsdesc->td_tlsindex] + tlsdesc->td_tlsoffs - tp) */
	ldr	x2, [x1, #8]		/* tlsdesc->td_tlsoffs */
	add 	x2, x2, x3
	sub	x0, x2, x4

	/* Restore registers and return */
	ldp	 x3,  x4, [sp, #(1 * 16)]
	ldp	 x1,  x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset 	-2 * 16
	ret
d178 11
a188 33
	/*
	 * Slow path
	 * return _rtld_tls_get_addr(tp, tlsdesc->td_tlsindex, tlsdesc->td_tlsoffs);
	 *
	 */
1:
	/* Save all interger registers */
	stp	x29, x30, [sp, #-(8 * 16)]!
	.cfi_adjust_cfa_offset	8 * 16
	.cfi_rel_offset		x29, 0
	.cfi_rel_offset		x30, 8

	stp	x5,   x6, [sp, #(1 * 16)]
	stp	x7,   x8, [sp, #(2 * 16)]
	stp	x9,  x10, [sp, #(3 * 16)]
	stp	x11, x12, [sp, #(4 * 16)]
	stp	x13, x14, [sp, #(5 * 16)]
	stp	x15, x16, [sp, #(6 * 16)]
	stp	x17, x18, [sp, #(7 * 16)]
	.cfi_rel_offset		 x5, 16
	.cfi_rel_offset		 x6, 24
	.cfi_rel_offset		 x7, 32
	.cfi_rel_offset		 x8, 40
	.cfi_rel_offset		 x9, 48
	.cfi_rel_offset		x10, 56
	.cfi_rel_offset		x11, 64
	.cfi_rel_offset		x12, 72
	.cfi_rel_offset		x13, 80
	.cfi_rel_offset		x14, 88
	.cfi_rel_offset		x15, 96
	.cfi_rel_offset		x16, 104
	.cfi_rel_offset		x17, 112
	.cfi_rel_offset		x18, 120
a189 27
	/* Find the tls offset */
	mov	x0, x4			/* tp */
	mov	x3, x1			/* tlsdesc ptr */
	ldr	x1, [x3, #0]		/* tlsdesc->td_tlsindex */
	ldr	x2, [x3, #8]		/* tlsdesc->td_tlsoffs */
	bl	_rtld_tls_get_addr
	mrs	x1, tpidr_el0
	sub	x0, x0, x1

	/* Restore slow path registers */
	ldp	x17, x18, [sp, #(7 * 16)]
	ldp	x15, x16, [sp, #(6 * 16)]
	ldp	x13, x14, [sp, #(5 * 16)]
	ldp	x11, x12, [sp, #(4 * 16)]
	ldp	x9, x10,  [sp, #(3 * 16)]
	ldp	x7, x8,   [sp, #(2 * 16)]
	ldp	x5, x6,   [sp, #(1 * 16)]
	ldp	x29, x30, [sp], #(8 * 16)
	.cfi_adjust_cfa_offset 	-8 * 16
	.cfi_restore		x29
	.cfi_restore		x30

	/* Restore fast path registers and return */
	ldp	 x3,  x4, [sp, #16]
	ldp	 x1,  x2, [sp], #(2 * 16)
	.cfi_adjust_cfa_offset	-2 * 16
	.cfi_endproc
@


1.1
log
@Preliminary files for AARCH64 (64-bit ARM) support.
Enough for a distribution build.
@
text
@d1 1
a1 1
/* $NetBSD$ */
d34 1
a34 1
RCSID("$NetBSD$")
a45 1

a48 4
#if 1
	adrp	x1, _PROCEDURE_LINKAGE_TABLE_	/* load _DYNAMIC offset from GOT */
	ldr	x1, [x1, #:got_lo12:_PROCEDURE_LINKAGE_TABLE_]
#else
a50 1
#endif
d54 1
d57 1
a57 1
	bl	_rtld_relocate_nonplt_self
d62 1
a62 1
	bl	_rtld
d73 4
a76 2
 * X17 = &PLTGOT[n]
 * X16 = &PLTGOT[2]
d79 37
a115 25
	sub	sp, sp, #96		/* reserve stack space */
	stp	x29, x30, [sp, #80]	/* save FP & LR */
	add	x29, sp, #80		/* get new FP */
	str	x24, [sp, #64]		/* save caller-saved register */
	stp	x6, x7, [sp, #48]	/* save arguments */
	stp	x4, x5, [sp, #32]	/* save arguments */
	stp	x2, x3, [sp, #16]	/* save arguments */
	stp	x0, x1, [sp, #0]	/* save arguments */

	sub	x16, x16, #16		/* adjust to &PLTGOT[0] */
	mov	x24, x17		/* preserve across _rtld_bind */
	sub	x1, x17, x16		/* x1 = &PLTGOT[N] - &PLTGOT[2] */
	lsr	x1, x1, #3		/* x1 = N - 2 */
	ldr	x0, [x16, #8]		/* get obj ptr from &PLTGOT[1] */
	bl	_rtld_bind
	str	x0, [x24]		/* save address in PLTGOT[N] */
	mov	x17, x0			/* save address */

	ldp	x0, x1, [sp, #0]	/* restore arguments */
	ldp	x2, x3, [sp, #16]	/* restore arguments */
	ldp	x4, x5, [sp, #32]	/* restore arguments */
	ldp	x6, x7, [sp, #48]	/* restore arguments */
	ldr	x24, [sp, #64]		/* save caller-saved register */
	ldp	x29, x30, [sp, #80]	/* restore FP & LR */
	add	sp, sp, #96		/* reclaim stack */
d118 6
@


1.1.4.1
log
@file rtld_start.S was added on branch tls-maxphys on 2014-08-20 00:02:22 +0000
@
text
@d1 108
@


1.1.4.2
log
@Rebase to HEAD as of a few days ago.
@
text
@a0 108
/* $NetBSD$ */

/*-
 * Copyright (c) 2014 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Matt Thomas of 3am Software Foundry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <machine/asm.h>

RCSID("$NetBSD$")

/*
 * void _rtld_start(void (*cleanup)(void), const Obj_Entry *obj,
 *    struct ps_strings *ps_strings);
 *
 * X0		= NULL
 * X1		= NULL
 * X2		= ps_strings
 * X30 (LR)	= 0
 * X29 (FP)	= 0
 */

ENTRY_NP(_rtld_start)
	mov	x24, x2			/* save ps_strings */

#if 1
	adrp	x1, _PROCEDURE_LINKAGE_TABLE_	/* load _DYNAMIC offset from GOT */
	ldr	x1, [x1, #:got_lo12:_PROCEDURE_LINKAGE_TABLE_]
#else
	adrp	x1, :got:_DYNAMIC	/* load _DYNAMIC offset from GOT */
	ldr	x1, [x1, #:got_lo12:_DYNAMIC]
#endif

	adrp	x0, _DYNAMIC		/* get &_DYNAMIC */
	add	x0, x0, #:lo12:_DYNAMIC
	sub	x25, x0, x1		/* relocbase = &_DYNAMIC - GOT:_DYNAMIC */
	mov	x1, x25			/* pass as 2nd argument */
	bl	_rtld_relocate_nonplt_self

	sub	sp, sp, #16		/* reserve space for returns */
	mov	x0, sp			/* pointer to reserved space */
	mov	x1, x25			/* pass relocbase */
	bl	_rtld
	mov	x17, x0			/* save entry point */

	ldp	x0, x1, [sp], #16	/* pop cleanup & obj_main */
	mov	x2, x24			/* restore ps_strings */

	br	x17			/* call saved entry point */
END(_rtld_start)

/*
 * Upon entry from plt0 entry:
 * X17 = &PLTGOT[n]
 * X16 = &PLTGOT[2]
 */
ENTRY_NP(_rtld_bind_start)
	sub	sp, sp, #96		/* reserve stack space */
	stp	x29, x30, [sp, #80]	/* save FP & LR */
	add	x29, sp, #80		/* get new FP */
	str	x24, [sp, #64]		/* save caller-saved register */
	stp	x6, x7, [sp, #48]	/* save arguments */
	stp	x4, x5, [sp, #32]	/* save arguments */
	stp	x2, x3, [sp, #16]	/* save arguments */
	stp	x0, x1, [sp, #0]	/* save arguments */

	sub	x16, x16, #16		/* adjust to &PLTGOT[0] */
	mov	x24, x17		/* preserve across _rtld_bind */
	sub	x1, x17, x16		/* x1 = &PLTGOT[N] - &PLTGOT[2] */
	lsr	x1, x1, #3		/* x1 = N - 2 */
	ldr	x0, [x16, #8]		/* get obj ptr from &PLTGOT[1] */
	bl	_rtld_bind
	str	x0, [x24]		/* save address in PLTGOT[N] */
	mov	x17, x0			/* save address */

	ldp	x0, x1, [sp, #0]	/* restore arguments */
	ldp	x2, x3, [sp, #16]	/* restore arguments */
	ldp	x4, x5, [sp, #32]	/* restore arguments */
	ldp	x6, x7, [sp, #48]	/* restore arguments */
	ldr	x24, [sp, #64]		/* save caller-saved register */
	ldp	x29, x30, [sp, #80]	/* restore FP & LR */
	add	sp, sp, #96		/* reclaim stack */
	br	x17			/* call bound function */
END(_rtld_bind_start)
@


