head	1.38;
access;
symbols
	netbsd-11-0-RC5:1.38
	netbsd-11-0-RC4:1.38
	netbsd-11-0-RC3:1.38
	netbsd-11-0-RC2:1.38
	netbsd-11-0-RC1:1.38
	perseant-exfatfs-base-20250801:1.38
	netbsd-11:1.38.0.2
	netbsd-11-base:1.38
	netbsd-10-1-RELEASE:1.35.14.1
	perseant-exfatfs-base-20240630:1.36
	perseant-exfatfs:1.36.0.2
	perseant-exfatfs-base:1.36
	netbsd-8-3-RELEASE:1.32.8.1
	netbsd-9-4-RELEASE:1.35.6.1
	netbsd-10-0-RELEASE:1.35.14.1
	netbsd-10-0-RC6:1.35.14.1
	netbsd-10-0-RC5:1.35.14.1
	netbsd-10-0-RC4:1.35.14.1
	netbsd-10-0-RC3:1.35.14.1
	netbsd-10-0-RC2:1.35.14.1
	netbsd-10-0-RC1:1.35.14.1
	netbsd-10:1.35.0.14
	netbsd-10-base:1.35
	netbsd-9-3-RELEASE:1.35
	cjep_sun2x-base1:1.35
	cjep_sun2x:1.35.0.12
	cjep_sun2x-base:1.35
	cjep_staticlib_x-base1:1.35
	netbsd-9-2-RELEASE:1.35
	cjep_staticlib_x:1.35.0.10
	cjep_staticlib_x-base:1.35
	netbsd-9-1-RELEASE:1.35
	phil-wifi-20200421:1.35
	phil-wifi-20200411:1.35
	is-mlppp:1.35.0.8
	is-mlppp-base:1.35
	phil-wifi-20200406:1.35
	netbsd-8-2-RELEASE:1.32.8.1
	netbsd-9-0-RELEASE:1.35
	netbsd-9-0-RC2:1.35
	netbsd-9-0-RC1:1.35
	phil-wifi-20191119:1.35
	netbsd-9:1.35.0.6
	netbsd-9-base:1.35
	phil-wifi-20190609:1.35
	netbsd-8-1-RELEASE:1.32.8.1
	netbsd-8-1-RC1:1.32.8.1
	pgoyette-compat-merge-20190127:1.35
	pgoyette-compat-20190127:1.35
	pgoyette-compat-20190118:1.35
	pgoyette-compat-1226:1.35
	pgoyette-compat-1126:1.35
	pgoyette-compat-1020:1.35
	pgoyette-compat-0930:1.35
	pgoyette-compat-0906:1.35
	netbsd-7-2-RELEASE:1.30.22.1
	pgoyette-compat-0728:1.35
	netbsd-8-0-RELEASE:1.32.8.1
	phil-wifi:1.35.0.4
	phil-wifi-base:1.35
	pgoyette-compat-0625:1.35
	netbsd-8-0-RC2:1.32.8.1
	pgoyette-compat-0521:1.35
	pgoyette-compat-0502:1.35
	pgoyette-compat-0422:1.35
	netbsd-8-0-RC1:1.32.8.1
	pgoyette-compat-0415:1.35
	pgoyette-compat-0407:1.35
	pgoyette-compat-0330:1.35
	pgoyette-compat-0322:1.35
	pgoyette-compat-0315:1.35
	netbsd-7-1-2-RELEASE:1.30.22.1
	pgoyette-compat:1.35.0.2
	pgoyette-compat-base:1.35
	netbsd-7-1-1-RELEASE:1.30.22.1
	matt-nb8-mediatek:1.32.8.1.0.2
	matt-nb8-mediatek-base:1.32.8.1
	perseant-stdc-iso10646:1.34.0.2
	perseant-stdc-iso10646-base:1.34
	netbsd-8:1.32.0.8
	netbsd-8-base:1.32
	prg-localcount2-base3:1.32
	prg-localcount2-base2:1.32
	prg-localcount2-base1:1.32
	prg-localcount2:1.32.0.6
	prg-localcount2-base:1.32
	pgoyette-localcount-20170426:1.32
	bouyer-socketcan-base1:1.32
	pgoyette-localcount-20170320:1.32
	netbsd-7-1:1.30.22.1.0.4
	netbsd-7-1-RELEASE:1.30.22.1
	netbsd-7-1-RC2:1.30.22.1
	netbsd-7-nhusb-base-20170116:1.30.22.1
	bouyer-socketcan:1.32.0.4
	bouyer-socketcan-base:1.32
	pgoyette-localcount-20170107:1.32
	netbsd-7-1-RC1:1.30.22.1
	pgoyette-localcount-20161104:1.32
	netbsd-7-0-2-RELEASE:1.30
	localcount-20160914:1.32
	netbsd-7-nhusb:1.30.22.1.0.2
	netbsd-7-nhusb-base:1.30.22.1
	pgoyette-localcount-20160806:1.32
	pgoyette-localcount-20160726:1.32
	pgoyette-localcount:1.32.0.2
	pgoyette-localcount-base:1.32
	netbsd-7-0-1-RELEASE:1.30
	netbsd-7-0:1.30.0.24
	netbsd-7-0-RELEASE:1.30
	netbsd-7-0-RC3:1.30
	netbsd-7-0-RC2:1.30
	netbsd-7-0-RC1:1.30
	netbsd-5-2-3-RELEASE:1.24.4.1
	netbsd-5-1-5-RELEASE:1.24
	netbsd-6-0-6-RELEASE:1.30
	netbsd-6-1-5-RELEASE:1.30
	netbsd-7:1.30.0.22
	netbsd-7-base:1.30
	yamt-pagecache-base9:1.30
	yamt-pagecache-tag8:1.30
	netbsd-6-1-4-RELEASE:1.30
	netbsd-6-0-5-RELEASE:1.30
	tls-earlyentropy:1.30.0.20
	tls-earlyentropy-base:1.30
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.30
	riastradh-drm2-base3:1.30
	netbsd-6-1-3-RELEASE:1.30
	netbsd-6-0-4-RELEASE:1.30
	netbsd-5-2-2-RELEASE:1.24.4.1
	netbsd-5-1-4-RELEASE:1.24
	netbsd-6-1-2-RELEASE:1.30
	netbsd-6-0-3-RELEASE:1.30
	netbsd-5-2-1-RELEASE:1.24.4.1
	netbsd-5-1-3-RELEASE:1.24
	netbsd-6-1-1-RELEASE:1.30
	riastradh-drm2-base2:1.30
	riastradh-drm2-base1:1.30
	riastradh-drm2:1.30.0.14
	riastradh-drm2-base:1.30
	netbsd-6-1:1.30.0.18
	netbsd-6-0-2-RELEASE:1.30
	netbsd-6-1-RELEASE:1.30
	netbsd-6-1-RC4:1.30
	netbsd-6-1-RC3:1.30
	agc-symver:1.30.0.16
	agc-symver-base:1.30
	netbsd-6-1-RC2:1.30
	netbsd-6-1-RC1:1.30
	yamt-pagecache-base8:1.30
	netbsd-5-2:1.24.4.1.0.2
	netbsd-6-0-1-RELEASE:1.30
	yamt-pagecache-base7:1.30
	netbsd-5-2-RELEASE:1.24.4.1
	netbsd-5-2-RC1:1.24.4.1
	matt-nb6-plus-nbase:1.30
	yamt-pagecache-base6:1.30
	netbsd-6-0:1.30.0.12
	netbsd-6-0-RELEASE:1.30
	netbsd-6-0-RC2:1.30
	tls-maxphys:1.30.0.10
	tls-maxphys-base:1.30
	matt-nb6-plus:1.30.0.8
	matt-nb6-plus-base:1.30
	netbsd-6-0-RC1:1.30
	yamt-pagecache-base5:1.30
	yamt-pagecache-base4:1.30
	netbsd-6:1.30.0.6
	netbsd-6-base:1.30
	netbsd-5-1-2-RELEASE:1.24
	netbsd-5-1-1-RELEASE:1.24
	yamt-pagecache-base3:1.30
	yamt-pagecache-base2:1.30
	yamt-pagecache:1.30.0.4
	yamt-pagecache-base:1.30
	cherry-xenmp:1.30.0.2
	cherry-xenmp-base:1.30
	bouyer-quota2-nbase:1.28
	bouyer-quota2:1.28.0.2
	bouyer-quota2-base:1.28
	matt-mips64-premerge-20101231:1.28
	matt-nb5-mips64-premerge-20101231:1.24
	matt-nb5-pq3:1.24.0.14
	matt-nb5-pq3-base:1.24
	netbsd-5-1:1.24.0.12
	netbsd-5-1-RELEASE:1.24
	netbsd-5-1-RC4:1.24
	matt-nb5-mips64-k15:1.24
	netbsd-5-1-RC3:1.24
	netbsd-5-1-RC2:1.24
	netbsd-5-1-RC1:1.24
	netbsd-5-0-2-RELEASE:1.24
	matt-nb5-mips64-premerge-20091211:1.24
	matt-premerge-20091211:1.26
	matt-nb5-mips64-u2-k2-k4-k7-k8-k9:1.24
	matt-nb4-mips64-k7-u2a-k9b:1.24
	matt-nb5-mips64-u1-k1-k5:1.24
	matt-nb5-mips64:1.24.0.10
	netbsd-5-0-1-RELEASE:1.24
	jym-xensuspend-nbase:1.25
	netbsd-5-0:1.24.0.8
	netbsd-5-0-RELEASE:1.24
	netbsd-5-0-RC4:1.24
	netbsd-5-0-RC3:1.24
	netbsd-5-0-RC2:1.24
	jym-xensuspend:1.24.0.6
	jym-xensuspend-base:1.25
	netbsd-5-0-RC1:1.24
	netbsd-5:1.24.0.4
	netbsd-5-base:1.24
	matt-mips64-base2:1.24
	matt-mips64:1.22.0.22
	netbsd-4-0-1-RELEASE:1.22
	wrstuden-revivesa-base-3:1.24
	wrstuden-revivesa-base-2:1.24
	wrstuden-fixsa-newbase:1.22
	wrstuden-revivesa-base-1:1.22
	yamt-pf42-base4:1.22
	yamt-pf42-base3:1.22
	hpcarm-cleanup-nbase:1.22
	yamt-pf42-baseX:1.22
	yamt-pf42-base2:1.22
	wrstuden-revivesa:1.22.0.20
	wrstuden-revivesa-base:1.22
	yamt-pf42:1.22.0.18
	yamt-pf42-base:1.22
	keiichi-mipv6:1.22.0.16
	keiichi-mipv6-base:1.22
	matt-armv6-nbase:1.22
	matt-armv6-prevmlocking:1.22
	wrstuden-fixsa-base-1:1.22
	netbsd-4-0:1.22.0.14
	netbsd-4-0-RELEASE:1.22
	cube-autoconf:1.22.0.12
	cube-autoconf-base:1.22
	netbsd-4-0-RC5:1.22
	netbsd-4-0-RC4:1.22
	netbsd-4-0-RC3:1.22
	netbsd-4-0-RC2:1.22
	netbsd-4-0-RC1:1.22
	matt-armv6:1.22.0.10
	matt-armv6-base:1.22
	matt-mips64-base:1.22
	hpcarm-cleanup:1.22.0.8
	hpcarm-cleanup-base:1.22
	netbsd-3-1-1-RELEASE:1.19
	netbsd-3-0-3-RELEASE:1.19
	wrstuden-fixsa:1.22.0.6
	wrstuden-fixsa-base:1.22
	abandoned-netbsd-4-base:1.22
	abandoned-netbsd-4:1.22.0.2
	netbsd-3-1:1.19.0.14
	netbsd-3-1-RELEASE:1.19
	netbsd-3-0-2-RELEASE:1.19
	netbsd-3-1-RC4:1.19
	netbsd-3-1-RC3:1.19
	netbsd-3-1-RC2:1.19
	netbsd-3-1-RC1:1.19
	netbsd-4:1.22.0.4
	netbsd-4-base:1.22
	netbsd-3-0-1-RELEASE:1.19
	netbsd-3-0:1.19.0.12
	netbsd-3-0-RELEASE:1.19
	netbsd-3-0-RC6:1.19
	netbsd-3-0-RC5:1.19
	netbsd-3-0-RC4:1.19
	netbsd-3-0-RC3:1.19
	netbsd-3-0-RC2:1.19
	netbsd-3-0-RC1:1.19
	netbsd-2-0-3-RELEASE:1.19
	netbsd-2-1:1.19.0.10
	netbsd-2-1-RELEASE:1.19
	netbsd-2-1-RC6:1.19
	netbsd-2-1-RC5:1.19
	netbsd-2-1-RC4:1.19
	netbsd-2-1-RC3:1.19
	netbsd-2-1-RC2:1.19
	netbsd-2-1-RC1:1.19
	netbsd-2-0-2-RELEASE:1.19
	netbsd-3:1.19.0.8
	netbsd-3-base:1.19
	netbsd-2-0-1-RELEASE:1.19
	netbsd-2:1.19.0.6
	netbsd-2-base:1.19
	netbsd-2-0-RELEASE:1.19
	netbsd-2-0-RC5:1.19
	netbsd-2-0-RC4:1.19
	netbsd-2-0-RC3:1.19
	netbsd-2-0-RC2:1.19
	netbsd-2-0-RC1:1.19
	netbsd-1-6:1.19.0.4
	netbsd-2-0:1.19.0.2
	netbsd-2-0-base:1.19
	fvdl_fs64_base:1.14;
locks; strict;
comment	@ * @;


1.38
date	2025.04.16.17.51.01;	author riastradh;	state Exp;
branches;
next	1.37;
commitid	pkSVcp42CKMEOiRF;

1.37
date	2024.07.23.09.27.00;	author uwe;	state Exp;
branches;
next	1.36;
commitid	bLr1Yn2Js9f0CWiF;

1.36
date	2023.06.04.01.24.58;	author joerg;	state Exp;
branches
	1.36.2.1;
next	1.35;
commitid	jBAtDE6SYslzbzrE;

1.35
date	2017.08.10.19.03.26;	author joerg;	state Exp;
branches
	1.35.6.1
	1.35.14.1;
next	1.34;
commitid	UgH5577k4GrjpH2A;

1.34
date	2017.06.20.13.45.20;	author joerg;	state Exp;
branches;
next	1.33;
commitid	FuYVGJcYAjabi7Wz;

1.33
date	2017.06.19.11.57.02;	author joerg;	state Exp;
branches;
next	1.32;
commitid	pYVYx1tH1kZmHYVz;

1.32
date	2016.04.14.20.17.07;	author skrll;	state Exp;
branches
	1.32.8.1;
next	1.31;
commitid	7xdAu6FeGesilD2z;

1.31
date	2014.08.25.20.40.52;	author joerg;	state Exp;
branches;
next	1.30;
commitid	hm6gwLhn9YorHMNx;

1.30
date	2011.03.25.18.07.06;	author joerg;	state Exp;
branches
	1.30.22.1;
next	1.29;

1.29
date	2011.03.12.22.54.36;	author joerg;	state Exp;
branches;
next	1.28;

1.28
date	2010.08.06.16.33.18;	author joerg;	state Exp;
branches;
next	1.27;

1.27
date	2010.01.13.20.17.22;	author christos;	state Exp;
branches;
next	1.26;

1.26
date	2009.08.29.13.46.55;	author jmmv;	state Exp;
branches;
next	1.25;

1.25
date	2009.03.16.02.46.47;	author lukem;	state Exp;
branches;
next	1.24;

1.24
date	2008.07.24.06.51.58;	author skrll;	state Exp;
branches
	1.24.4.1
	1.24.6.1;
next	1.23;

1.23
date	2008.07.24.04.39.25;	author matt;	state Exp;
branches;
next	1.22;

1.22
date	2006.05.21.04.17.35;	author mrg;	state Exp;
branches
	1.22.20.1;
next	1.21;

1.21
date	2005.08.20.19.01.17;	author skrll;	state Exp;
branches;
next	1.20;

1.20
date	2005.05.31.19.30.38;	author skrll;	state Exp;
branches;
next	1.19;

1.19
date	2003.07.24.10.12.29;	author skrll;	state Exp;
branches
	1.19.4.1;
next	1.18;

1.18
date	2003.07.05.20.48.39;	author marcus;	state Exp;
branches;
next	1.17;

1.17
date	2003.07.05.18.18.51;	author tsutsui;	state Exp;
branches;
next	1.16;

1.16
date	2003.07.02.15.29.34;	author marcus;	state Exp;
branches;
next	1.15;

1.15
date	2003.06.30.00.50.46;	author marcus;	state Exp;
branches;
next	1.14;

1.14
date	2002.10.03.20.39.23;	author mycroft;	state Exp;
branches;
next	1.13;

1.13
date	2002.09.26.20.42.12;	author mycroft;	state Exp;
branches;
next	1.12;

1.12
date	2002.09.12.22.56.31;	author mycroft;	state Exp;
branches;
next	1.11;

1.11
date	2002.09.12.20.21.01;	author mycroft;	state Exp;
branches;
next	1.10;

1.10
date	2002.09.11.14.19.30;	author junyoung;	state Exp;
branches;
next	1.9;

1.9
date	2002.09.06.15.17.58;	author mycroft;	state Exp;
branches;
next	1.8;

1.8
date	2002.09.06.13.20.34;	author mycroft;	state Exp;
branches;
next	1.7;

1.7
date	2002.09.06.03.12.07;	author mycroft;	state Exp;
branches;
next	1.6;

1.6
date	2002.09.06.03.05.37;	author mycroft;	state Exp;
branches;
next	1.5;

1.5
date	2002.09.05.21.31.34;	author mycroft;	state Exp;
branches;
next	1.4;

1.4
date	2002.09.05.21.21.11;	author mycroft;	state Exp;
branches;
next	1.3;

1.3
date	2002.09.05.20.08.18;	author mycroft;	state Exp;
branches;
next	1.2;

1.2
date	2002.09.05.18.25.47;	author mycroft;	state Exp;
branches;
next	1.1;

1.1
date	2002.09.05.15.38.30;	author mycroft;	state Exp;
branches;
next	;

1.36.2.1
date	2025.08.02.05.55.02;	author perseant;	state Exp;
branches;
next	;
commitid	23j6GFaDws3O875G;

1.35.6.1
date	2023.08.04.12.55.45;	author martin;	state Exp;
branches;
next	;
commitid	WQUk0iGBkKS39tzE;

1.35.14.1
date	2023.08.01.16.34.56;	author martin;	state Exp;
branches;
next	;
commitid	ygiHGMga8HEgs6zE;

1.32.8.1
date	2017.07.04.12.47.58;	author martin;	state Exp;
branches;
next	;
commitid	WH1NGk1BGSqEwUXz;

1.30.22.1
date	2016.03.06.18.17.56;	author martin;	state Exp;
branches;
next	;
commitid	Ns3WeAQc8ORoVBXy;

1.24.4.1
date	2012.03.17.18.28.39;	author bouyer;	state Exp;
branches;
next	;

1.24.6.1
date	2009.05.13.19.18.41;	author jym;	state Exp;
branches;
next	;

1.22.20.1
date	2008.09.18.04.39.18;	author wrstuden;	state Exp;
branches;
next	;

1.19.4.1
date	2003.07.24.10.12.29;	author tron;	state dead;
branches;
next	1.19.4.2;

1.19.4.2
date	2004.05.28.08.31.22;	author tron;	state Exp;
branches;
next	;


desc
@@


1.38
log
@ld.elf_so/arch/sh3: Add copyright notice from original reloc.c.

Originally copied from libexec/ld.elf_so/reloc.c rev. 1.58.
@
text
@/*	$NetBSD: mdreloc.c,v 1.37 2024/07/23 09:27:00 uwe Exp $	*/

/*
 * Copyright 1996 John D. Polstra.
 * Copyright 1996 Matt Thomas <matt@@3am-software.com>
 * All rights reserved.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by John Polstra.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.37 2024/07/23 09:27:00 uwe Exp $");
#endif /* not lint */

/*
 * SuperH ELF relocations.
 *
 * Reference:
 *
 *	[RM0197] SH-4 generic and C specific application binary interface
 *	https://www.st.com/resource/en/reference_manual/rm0197-sh4-generic-and-c-specific-application-binary-interface-stmicroelectronics.pdf
 */

#include <sys/types.h>
#include <sys/tls.h>

#include "debug.h"
#include "rtld.h"

void _rtld_bind_start(void);
void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
static inline int _rtld_relocate_plt_object(const Obj_Entry *,
    const Elf_Rela *, Elf_Addr *);

void
_rtld_setup_pltgot(const Obj_Entry *obj)
{
	obj->pltgot[1] = (Elf_Addr) obj;
	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
}

void
_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
{
	const Elf_Rela *rela = 0, *relalim;
	Elf_Addr relasz = 0;
	Elf_Addr *where;

	for (; dynp->d_tag != DT_NULL; dynp++) {
		switch (dynp->d_tag) {
		case DT_RELA:
			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
			break;
		case DT_RELASZ:
			relasz = dynp->d_un.d_val;
			break;
		}
	}
	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
	for (; rela < relalim; rela++) {
		where = (Elf_Addr *)(relocbase + rela->r_offset);
		*where = (Elf_Addr)(relocbase + rela->r_addend);
	}
}

int
_rtld_relocate_nonplt_objects(Obj_Entry *obj)
{
	const Elf_Rela *rela;
	const Elf_Sym   *def = NULL;
	const Obj_Entry *defobj = NULL;
	unsigned long last_symnum = ULONG_MAX;

	for (rela = obj->rela; rela < obj->relalim; rela++) {
		Elf_Addr        *where;
		Elf_Addr         tmp;
		unsigned long	 symnum;

		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);

		switch (ELF_R_TYPE(rela->r_info)) {
		case R_TYPE(GOT32):
		case R_TYPE(REL32):
		case R_TYPE(DIR32):
		case R_TYPE(GLOB_DAT):
		case R_TYPE(TLS_DTPOFF32):
		case R_TYPE(TLS_DTPMOD32):
		case R_TYPE(TLS_TPOFF32):
			symnum = ELF_R_SYM(rela->r_info);
			if (last_symnum != symnum) {
				last_symnum = symnum;
				def = _rtld_find_symdef(symnum, obj, &defobj,
				    false);
				if (def == NULL)
					return -1;
			}
			break;
		default:
			break;
		}

		switch (ELF_R_TYPE(rela->r_info)) {
		case R_TYPE(NONE):
			break;

#if 1 /* XXX should not occur */
		case R_TYPE(GOT32):
			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
			    rela->r_addend);
			if (*where != tmp)
				*where = tmp;
			rdbg(("GOT32 %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;

		case R_TYPE(REL32):
			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
			    rela->r_addend) - (Elf_Addr)where;
			if (*where != tmp)
				*where = tmp;
			rdbg(("PC32 %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;
#endif

		case R_TYPE(DIR32):
			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
			    rela->r_addend);
			if (*where != tmp)
				*where = tmp;
			rdbg(("32 %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;

		case R_TYPE(GLOB_DAT):
			tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
			    rela->r_addend;
			if (*where != tmp)
				*where = tmp;
			rdbg(("GLOB_DAT %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;

		case R_TYPE(RELATIVE):
			if (rela->r_addend)
				*where = (Elf_Addr)obj->relocbase + rela->r_addend;
			else
				*where += (Elf_Addr)obj->relocbase;
			rdbg(("RELATIVE in %s --> %p", obj->path,
			    (void *)*where));
			break;

		case R_TYPE(COPY):
			/*
			 * These are deferred until all other relocations have
			 * been done.  All we do here is make sure that the
			 * COPY relocation is not in a shared library.  They
			 * are allowed only in executable files.
			 */
			if (obj->isdynamic) {
				_rtld_error(
			"%s: Unexpected R_COPY relocation in shared library",
				    obj->path);
				return -1;
			}
			rdbg(("COPY (avoid in main)"));
			break;

		case R_TYPE(TLS_DTPOFF32):
			*where = (Elf_Addr)(def->st_value);

			rdbg(("TLS_DTPOFF32 %s in %s --> %p",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where));

			break;
		case R_TYPE(TLS_DTPMOD32):
			*where = (Elf_Addr)(defobj->tlsindex);

			rdbg(("TLS_DTPMOD32 %s in %s --> %p",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where));

			break;

		case R_TYPE(TLS_TPOFF32):
			if (!defobj->tls_static &&
			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
				return -1;

			*where = (Elf_Addr)def->st_value +
			    rela->r_addend + defobj->tlsoffset +
			    sizeof(struct tls_tcb);

			rdbg(("TLS_TPOFF32 %s in %s --> %p",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where));
			break;

		default:
			rdbg(("sym = %lu, type = %lu, offset = %p, "
			    "addend = %p, contents = %p, symbol = %s",
			    (u_long)ELF_R_SYM(rela->r_info),
			    (u_long)ELF_R_TYPE(rela->r_info),
			    (void *)rela->r_offset, (void *)rela->r_addend,
			    (void *)*where,
			    obj->strtab + obj->symtab[symnum].st_name));
			_rtld_error("%s: Unsupported relocation type %ld "
			    "in non-PLT relocations",
			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
			return -1;
		}
	}
	return 0;
}

int
_rtld_relocate_plt_lazy(Obj_Entry *obj)
{
	const Elf_Rela *rela;

	if (!obj->relocbase)
		return 0;

	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);

		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));

		/* Just relocate the GOT slots pointing into the PLT */
		*where += (Elf_Addr)obj->relocbase;
		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
	}

	return 0;
}

caddr_t
_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
{
	const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
	Elf_Addr new_value;
	int err;

	new_value = 0;	/* XXX gcc */

	_rtld_shared_enter();
	err = _rtld_relocate_plt_object(obj, rela, &new_value);
	if (err)
		_rtld_die();
	_rtld_shared_exit();

	return (caddr_t)new_value;
}

int
_rtld_relocate_plt_objects(const Obj_Entry *obj)
{
	const Elf_Rela *rela = obj->pltrela;

	for (; rela < obj->pltrelalim; rela++)
		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
			return -1;

	return 0;
}

static inline int
_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
{
	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
	Elf_Addr new_value;
	const Elf_Sym  *def;
	const Obj_Entry *defobj;
	unsigned long info = rela->r_info;

	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));

	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
	if (__predict_false(def == NULL))
		return -1;
	if (__predict_false(def == &_rtld_sym_zero))
		return 0;

	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
		if (tp == NULL)
			return 0;
		new_value = _rtld_resolve_ifunc(defobj, def);
	} else {
		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
	}
	rdbg(("bind now/fixup in %s --> old=%p new=%p",
	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
	if (*where != new_value)
		*where = new_value;

	if (tp)
		*tp = new_value;

	return 0;
}
@


1.37
log
@ld.elf_so: Cite reference for SuperH ELF relocations

PR lib/58455: Missing references for processor-specific ELF
relocation semantics
@
text
@d1 32
a32 1
/*	$NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $	*/
d36 1
a36 1
__RCSID("$NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $");
@


1.36
log
@Fix interactions of initial-exec TLS model and dlopen

(1) If an initial-exec relocation was used for a non-local symbol
(i.e. the definition of the symbol is in a different DSO), the
computation of the static TLS offset used the wrong DSO.
This would effectively mean the wrong address was computed
(PR toolchain/50277, PR pkg/57445).

Fix this by forcing the computation of the correct DSO (the one defining
the symbol).

This code uses __UNCONST to avoid the vast interface changes for this
special case.

(2) If symbols from a DSO loaded via dlopen are used with both
global-dynamic/local-dynamic and initial-exec relocations AND
a initial-exec relocation was resolved first in a thread, a split brain
situation could exist where the dynamic relocations would use one memory
block (separate allocation) and the initial-exec relocations the static
per-thread TLS space.

(3) If the initial-exec relocation in (2) is seen after any thread has
already used a GD/LD allocation, bail out. Since IE relocations are used
only in the GOT, this will prevent the dlopen. This is a bit more
aggressive than necessary, but a full blown reference counting doesn't
seem to be justified.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.35 2017/08/10 19:03:26 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.35 2017/08/10 19:03:26 joerg Exp $");
d8 9
@


1.36.2.1
log
@Sync with HEAD
@
text
@d1 1
a1 32
/*	$NetBSD: mdreloc.c,v 1.38 2025/04/16 17:51:01 riastradh Exp $	*/

/*
 * Copyright 1996 John D. Polstra.
 * Copyright 1996 Matt Thomas <matt@@3am-software.com>
 * All rights reserved.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by John Polstra.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
 */
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.38 2025/04/16 17:51:01 riastradh Exp $");
a7 9
/*
 * SuperH ELF relocations.
 *
 * Reference:
 *
 *	[RM0197] SH-4 generic and C specific application binary interface
 *	https://www.st.com/resource/en/reference_manual/rm0197-sh4-generic-and-c-specific-application-binary-interface-stmicroelectronics.pdf
 */

@


1.35
log
@Add IRELATIVE support for ARM, X86 and PowerPC.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.34 2017/06/20 13:45:20 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.34 2017/06/20 13:45:20 joerg Exp $");
d176 2
a177 2
			if (!defobj->tls_done &&
			    _rtld_tls_offset_allocate(obj))
@


1.35.6.1
log
@Pull up following revision(s), all via patch,
(requested by riastradh in ticket #1699):

	distrib/sets/lists/tests/shl.mi: revision 1.14
	distrib/sets/lists/tests/shl.mi: revision 1.15
	distrib/sets/lists/tests/shl.mi: revision 1.16
	tests/libexec/ld.elf_so/helper_def_static/h_def_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_def_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_def_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile: revision 1.2
	libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.75
	distrib/sets/lists/tests/mi: revision 1.1265
	libexec/ld.elf_so/arch/sh3/mdreloc.c: revision 1.36
	libexec/ld.elf_so/rtld.c: revision 1.214
	tests/libexec/ld.elf_so/helper_onlydef_static/Makefile: revision 1.1
	distrib/sets/lists/debug/mi: revision 1.400
	tests/libexec/ld.elf_so/helper_onlydef_static/Makefile: revision 1.2
	distrib/sets/lists/debug/mi: revision 1.401
	distrib/sets/lists/debug/mi: revision 1.402
	tests/libexec/ld.elf_so/helper_dso2/Makefile: revision 1.2
	distrib/sets/lists/debug/mi: revision 1.403
	tests/libexec/ld.elf_so/helper_symver_dso0/Makefile: revision 1.2
	libexec/ld.elf_so/arch/x86_64/mdreloc.c: revision 1.48
	distrib/sets/lists/debug/mi: revision 1.406
	tests/libexec/ld.elf_so/helper_use_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_use_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile: revision 1.2
	libexec/ld.elf_so/arch/sparc64/mdreloc.c: revision 1.70
	libexec/ld.elf_so/arch/aarch64/mdreloc.c: revision 1.18
	tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/Makefile: revision 1.13
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.46
	libexec/ld.elf_so/rtld.h: revision 1.146
	tests/libexec/ld.elf_so/Makefile: revision 1.14
	distrib/sets/lists/debug/shl.mi: revision 1.306
	tests/libexec/ld.elf_so/Makefile: revision 1.15
	tests/libexec/ld.elf_so/helper_abuse_static/Makefile: revision 1.1
	distrib/sets/lists/debug/shl.mi: revision 1.307
	tests/libexec/ld.elf_so/Makefile: revision 1.16
	tests/libexec/ld.elf_so/helper_abuse_static/Makefile: revision 1.2
	distrib/sets/lists/debug/shl.mi: revision 1.308
	tests/libexec/ld.elf_so/Makefile: revision 1.17
	distrib/sets/lists/debug/shl.mi: revision 1.309
	tests/libexec/ld.elf_so/Makefile: revision 1.18
	tests/libexec/ld.elf_so/Makefile: revision 1.19
	libexec/ld.elf_so/tls.c: revision 1.16
	libexec/ld.elf_so/tls.c: revision 1.17
	libexec/ld.elf_so/tls.c: revision 1.18
	libexec/ld.elf_so/tls.c: revision 1.19
	tests/libexec/ld.elf_so/helper_onlydef_static/h_onlydef_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_use_static/h_use_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_use_static/h_use_static.c: revision 1.2
	tests/libexec/ld.elf_so/helper_def_static/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_def_static/Makefile: revision 1.2
	libexec/ld.elf_so/arch/hppa/hppa_reloc.c: revision 1.50
	distrib/sets/lists/debug/shl.mi: revision 1.310
	libexec/ld.elf_so/README.TLS: revision 1.6
	distrib/sets/lists/debug/shl.mi: revision 1.311
	distrib/sets/lists/debug/shl.mi: revision 1.314
	tests/libexec/ld.elf_so/helper_dso3/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_symver_dso1/Makefile: revision 1.4
	libexec/ld.elf_so/arch/powerpc/ppc_reloc.c: revision 1.63
	tests/libexec/ld.elf_so/helper_def_dynamic/h_def_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlydef/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_onlydef/Makefile: revision 1.2
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.10
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.11
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.12
	libexec/ld.elf_so/map_object.c: revision 1.66
	tests/libexec/ld.elf_so/helper.mk: revision 1.1
	libexec/ld.elf_so/arch/sparc/mdreloc.c: revision 1.57
	libexec/ld.elf_so/map_object.c: revision 1.67
	tests/libexec/ld.elf_so/helper_onlydef/h_onlydef.c: revision 1.1
	tests/libexec/ld.elf_so/helper_symver_dso2/Makefile: revision 1.4
	tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.3
	tests/libexec/ld.elf_so/helper_use_dynamic/h_use_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_abuse_static/h_abuse_static.c: revision 1.1
	libexec/ld.elf_so/arch/riscv/mdreloc.c: revision 1.9
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.1
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.2
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.3
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.4
	tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.5
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.6
	libexec/ld.elf_so/arch/m68k/mdreloc.c: revision 1.34
	tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.7
	libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.42
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.8
	libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.43
	libexec/ld.elf_so/arch/or1k/mdreloc.c: revision 1.4
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.9
	tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_abuse_dynamic/h_abuse_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyctor_dynamic/h_onlyctor_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_static/h_onlyuse_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_dynamic/h_onlyuse_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_dso1/Makefile: revision 1.2
	distrib/sets/lists/tests/shl.mi: revision 1.12
	distrib/sets/lists/tests/shl.mi: revision 1.13
	libexec/ld.elf_so/arch/alpha/alpha_reloc.c: revision 1.44

ld.elf_so: New test for extern initial-exec TLS, PR toolchain/50277.

ld.elf_so: Fix extern TLS test to match PR toolchain/50277.
Now it's actually testing the problem.
ld.elf_so: Nix inadvertently committed private test program.
ld.elf_so: Fix set lists for MKDEBUG=yes builds with t_tls_extern.

ld.elf_so: Sprinkle tls debug messages.

ld.elf_so: Make tls alloc debug messages more detailed and greppable.

ld.elf_so: Test variations on PR toolchain/50277.

ld.elf_so: Test extern dynamic TLS too.

ld.elf_so: Factor out logic in TLS tests to make writing more easier.
No functional change intended.

ld.elf_so: Test TLS abuse of static def, dynamic use and vice versa.

ld.elf_so: Shorter test names.
No functional non-cosmetic change intended.

ld.elf_so: Separately test eager and lazy resolution of def tls ptr.
eager: before loading use library
lazy: after loading use library

Add recent ld.elf_so test helpers debug info
ld.elf_so: Add new files to debug/shl.mi.

ld.elf_so: tls_extern dynamic_defabuse_eager must xfail differently.
If a symbol has already been resolved as dynamic TLS, any library
that tries to use it as static TLS cannot be dlopened.

ld.elf_so: Test another edge case of mismatched TLS models.
One library defines a symbol and _doesn't_ use it, so it has no
indication of whether the symbol is for static TLS or dynamic TLS,
and then two other libraries use it in different ways.

ld.elf_so: Test dynamic-then-static abuse via ctor.

ld.elf_so: Fix missing tab in debug/shl.mi in last change.

Fix interactions of initial-exec TLS model and dlopen

(1) If an initial-exec relocation was used for a non-local symbol
(i.e. the definition of the symbol is in a different DSO), the
computation of the static TLS offset used the wrong DSO.

This would effectively mean the wrong address was computed
(PR toolchain/50277, PR pkg/57445).
Fix this by forcing the computation of the correct DSO (the one defining
the symbol).
This code uses __UNCONST to avoid the vast interface changes for this
special case.

(2) If symbols from a DSO loaded via dlopen are used with both
global-dynamic/local-dynamic and initial-exec relocations AND
a initial-exec relocation was resolved first in a thread, a split brain
situation could exist where the dynamic relocations would use one memory
block (separate allocation) and the initial-exec relocations the static
per-thread TLS space.

(3) If the initial-exec relocation in (2) is seen after any thread has
already used a GD/LD allocation, bail out. Since IE relocations are used
only in the GOT, this will prevent the dlopen. This is a bit more
aggressive than necessary, but a full blown reference counting doesn't
seem to be justified.
Avoid using uninitialized variable "symnum" when building with DEBUG
enabled by borrowing the rdbg_symname() macro from arch/x86_64.
ld.elf_so: Sprinkle more debug messages on dlopen and error.

PR pkg/57445

Fix MKDEBUGLIB build by adding these installed files to the debug
set list.

One could argue that these files are not of any use, so why install
them?  I don't have a good argument either way, and this is (for
now) a simple work-around for PR bin/57455   Please feel free to
commit a different fix to avoid installing these files at all.

Fix markup of libh_ MKDEBUGLIB=yes only files

TLS variant I archs need to fudge the offset by the size of the TCB.
tests/libexec/ld.elf_so: Fix helper library makefiles.
1. Consolidate logic into a single helper.mk to reduce duplication.
2. Set NO* variables, not MK* variables which are reserved for user.
3. Avoid eager X!= in favour of lazy ${X:sh}.
4. Mark _g.a set list entries obsolete.  Never should've been built!
PR misc/57462
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $");
d176 2
a177 2
			if (!defobj->tls_static &&
			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
@


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

	distrib/sets/lists/tests/shl.mi: revision 1.14
	distrib/sets/lists/tests/shl.mi: revision 1.15
	distrib/sets/lists/tests/shl.mi: revision 1.16
	tests/libexec/ld.elf_so/helper_def_static/h_def_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_def_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_def_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_static/Makefile: revision 1.2
	libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.75
	distrib/sets/lists/tests/mi: revision 1.1265
	libexec/ld.elf_so/arch/sh3/mdreloc.c: revision 1.36
	libexec/ld.elf_so/rtld.c: revision 1.214
	tests/libexec/ld.elf_so/helper_onlydef_static/Makefile: revision 1.1
	distrib/sets/lists/debug/mi: revision 1.400
	tests/libexec/ld.elf_so/helper_onlydef_static/Makefile: revision 1.2
	distrib/sets/lists/debug/mi: revision 1.401
	distrib/sets/lists/debug/mi: revision 1.402
	tests/libexec/ld.elf_so/helper_dso2/Makefile: revision 1.2
	distrib/sets/lists/debug/mi: revision 1.403
	tests/libexec/ld.elf_so/helper_symver_dso0/Makefile: revision 1.2
	libexec/ld.elf_so/arch/x86_64/mdreloc.c: revision 1.48
	distrib/sets/lists/debug/mi: revision 1.406
	tests/libexec/ld.elf_so/helper_use_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_use_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_ifunc_dso/Makefile: revision 1.2
	libexec/ld.elf_so/arch/sparc64/mdreloc.c: revision 1.70
	libexec/ld.elf_so/arch/aarch64/mdreloc.c: revision 1.18
	tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_abuse_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/Makefile: revision 1.13
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.46
	libexec/ld.elf_so/rtld.h: revision 1.146
	tests/libexec/ld.elf_so/Makefile: revision 1.14
	distrib/sets/lists/debug/shl.mi: revision 1.306
	tests/libexec/ld.elf_so/Makefile: revision 1.15
	tests/libexec/ld.elf_so/helper_abuse_static/Makefile: revision 1.1
	distrib/sets/lists/debug/shl.mi: revision 1.307
	tests/libexec/ld.elf_so/Makefile: revision 1.16
	tests/libexec/ld.elf_so/helper_abuse_static/Makefile: revision 1.2
	distrib/sets/lists/debug/shl.mi: revision 1.308
	tests/libexec/ld.elf_so/Makefile: revision 1.17
	distrib/sets/lists/debug/shl.mi: revision 1.309
	tests/libexec/ld.elf_so/Makefile: revision 1.18
	tests/libexec/ld.elf_so/Makefile: revision 1.19
	libexec/ld.elf_so/tls.c: revision 1.16
	libexec/ld.elf_so/tls.c: revision 1.17
	libexec/ld.elf_so/tls.c: revision 1.18
	libexec/ld.elf_so/tls.c: revision 1.19
	tests/libexec/ld.elf_so/helper_onlydef_static/h_onlydef_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_use_static/h_use_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_use_static/h_use_static.c: revision 1.2
	tests/libexec/ld.elf_so/helper_def_static/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_def_static/Makefile: revision 1.2
	libexec/ld.elf_so/arch/hppa/hppa_reloc.c: revision 1.50
	distrib/sets/lists/debug/shl.mi: revision 1.310
	libexec/ld.elf_so/README.TLS: revision 1.6
	distrib/sets/lists/debug/shl.mi: revision 1.311
	distrib/sets/lists/debug/shl.mi: revision 1.314
	tests/libexec/ld.elf_so/helper_dso3/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_symver_dso1/Makefile: revision 1.4
	libexec/ld.elf_so/arch/powerpc/ppc_reloc.c: revision 1.63
	tests/libexec/ld.elf_so/helper_def_dynamic/h_def_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlydef/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_onlydef/Makefile: revision 1.2
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.10
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.11
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.12
	libexec/ld.elf_so/map_object.c: revision 1.66
	tests/libexec/ld.elf_so/helper.mk: revision 1.1
	libexec/ld.elf_so/arch/sparc/mdreloc.c: revision 1.57
	libexec/ld.elf_so/map_object.c: revision 1.67
	tests/libexec/ld.elf_so/helper_onlydef/h_onlydef.c: revision 1.1
	tests/libexec/ld.elf_so/helper_symver_dso2/Makefile: revision 1.4
	tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_use_static/Makefile: revision 1.3
	tests/libexec/ld.elf_so/helper_use_dynamic/h_use_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_abuse_static/h_abuse_static.c: revision 1.1
	libexec/ld.elf_so/arch/riscv/mdreloc.c: revision 1.9
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.1
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.2
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.3
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.4
	tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.5
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.6
	libexec/ld.elf_so/arch/m68k/mdreloc.c: revision 1.34
	tests/libexec/ld.elf_so/helper_onlyctor_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.7
	libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.42
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.8
	libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.43
	libexec/ld.elf_so/arch/or1k/mdreloc.c: revision 1.4
	tests/libexec/ld.elf_so/t_tls_extern.c: revision 1.9
	tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_dynamic/Makefile: revision 1.2
	tests/libexec/ld.elf_so/helper_abuse_dynamic/h_abuse_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyctor_dynamic/h_onlyctor_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_static/h_onlyuse_static.c: revision 1.1
	tests/libexec/ld.elf_so/helper_onlyuse_dynamic/h_onlyuse_dynamic.c: revision 1.1
	tests/libexec/ld.elf_so/helper_dso1/Makefile: revision 1.2
	distrib/sets/lists/tests/shl.mi: revision 1.12
	distrib/sets/lists/tests/shl.mi: revision 1.13
	libexec/ld.elf_so/arch/alpha/alpha_reloc.c: revision 1.44
	(all via patch)

ld.elf_so: New test for extern initial-exec TLS, PR toolchain/50277.

ld.elf_so: Fix extern TLS test to match PR toolchain/50277.
Now it's actually testing the problem.
ld.elf_so: Nix inadvertently committed private test program.
ld.elf_so: Fix set lists for MKDEBUG=yes builds with t_tls_extern.

ld.elf_so: Sprinkle tls debug messages.

ld.elf_so: Make tls alloc debug messages more detailed and greppable.

ld.elf_so: Test variations on PR toolchain/50277.

ld.elf_so: Test extern dynamic TLS too.

ld.elf_so: Factor out logic in TLS tests to make writing more easier.
No functional change intended.

ld.elf_so: Test TLS abuse of static def, dynamic use and vice versa.

ld.elf_so: Shorter test names.
No functional non-cosmetic change intended.

ld.elf_so: Separately test eager and lazy resolution of def tls ptr.
eager: before loading use library
lazy: after loading use library

Add recent ld.elf_so test helpers debug info
ld.elf_so: Add new files to debug/shl.mi.

ld.elf_so: tls_extern dynamic_defabuse_eager must xfail differently.
If a symbol has already been resolved as dynamic TLS, any library
that tries to use it as static TLS cannot be dlopened.

ld.elf_so: Test another edge case of mismatched TLS models.
One library defines a symbol and _doesn't_ use it, so it has no
indication of whether the symbol is for static TLS or dynamic TLS,
and then two other libraries use it in different ways.

ld.elf_so: Test dynamic-then-static abuse via ctor.

ld.elf_so: Fix missing tab in debug/shl.mi in last change.

Fix interactions of initial-exec TLS model and dlopen
(1) If an initial-exec relocation was used for a non-local symbol
(i.e. the definition of the symbol is in a different DSO), the
computation of the static TLS offset used the wrong DSO.
This would effectively mean the wrong address was computed
(PR toolchain/50277, PR pkg/57445).
Fix this by forcing the computation of the correct DSO (the one defining
the symbol).
This code uses __UNCONST to avoid the vast interface changes for this
special case.
(2) If symbols from a DSO loaded via dlopen are used with both
global-dynamic/local-dynamic and initial-exec relocations AND
a initial-exec relocation was resolved first in a thread, a split brain
situation could exist where the dynamic relocations would use one memory
block (separate allocation) and the initial-exec relocations the static
per-thread TLS space.
(3) If the initial-exec relocation in (2) is seen after any thread has
already used a GD/LD allocation, bail out. Since IE relocations are used
only in the GOT, this will prevent the dlopen. This is a bit more
aggressive than necessary, but a full blown reference counting doesn't
seem to be justified.
Avoid using uninitialized variable "symnum" when building with DEBUG
enabled by borrowing the rdbg_symname() macro from arch/x86_64.
ld.elf_so: Sprinkle more debug messages on dlopen and error.
PR pkg/57445
Fix MKDEBUGLIB build by adding these installed files to the debug
set list.
XXX
One could argue that these files are not of any use, so why install
them?  I don't have a good argument either way, and this is (for
now) a simple work-around for PR bin/57455   Please feel free to
commit a different fix to avoid installing these files at all.
Fix markup of libh_ MKDEBUGLIB=yes only files
TLS variant I archs need to fudge the offset by the size of the TCB.
tests/libexec/ld.elf_so: Fix helper library makefiles.
1. Consolidate logic into a single helper.mk to reduce duplication.
2. Set NO* variables, not MK* variables which are reserved for user.
3. Avoid eager X!= in favour of lazy ${X:sh}.
4. Mark _g.a set list entries obsolete.  Never should've been built!
PR misc/57462
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.36 2023/06/04 01:24:58 joerg Exp $");
d176 2
a177 2
			if (!defobj->tls_static &&
			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
@


1.34
log
@Add last_symnum, move up def and defobj.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.33 2017/06/19 11:57:02 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.33 2017/06/19 11:57:02 joerg Exp $");
d207 1
a207 1
_rtld_relocate_plt_lazy(const Obj_Entry *obj)
@


1.33
log
@Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.32 2016/04/14 20:17:07 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.32 2016/04/14 20:17:07 skrll Exp $");
d55 3
a60 2
		const Elf_Sym   *def;
		const Obj_Entry *defobj;
@


1.32
log
@Remove duplicated __RCSIDs I added years ago - I blame CVS.

Spotted by Miod Vallat
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.31 2014/08/25 20:40:52 joerg Exp $");
d64 21
a84 1
		symnum = ELF_R_SYM(rela->r_info);
a91 4
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

a101 4
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

a112 4
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

a122 4
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

a157 4
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

a165 4
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

a174 4
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

d191 2
a192 1
			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
@


1.32.8.1
log
@Pull up following revision(s) (requested by joerg in ticket #64):
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.40
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.41
	libexec/ld.elf_so/arch/hppa/hppa_reloc.c: revision 1.44
	libexec/ld.elf_so/arch/riscv/mdreloc.c: revision 1.3
	libexec/ld.elf_so/arch/aarch64/mdreloc.c: revision 1.3
	libexec/ld.elf_so/arch/sparc64/mdreloc.c: revision 1.60
	libexec/ld.elf_so/arch/m68k/mdreloc.c: revision 1.32
	libexec/ld.elf_so/arch/sparc64/mdreloc.c: revision 1.61
	libexec/ld.elf_so/arch/or1k/mdreloc.c: revision 1.2
	libexec/ld.elf_so/arch/sparc/mdreloc.c: revision 1.50
	libexec/ld.elf_so/arch/sh3/mdreloc.c: revision 1.33
	libexec/ld.elf_so/arch/sh3/mdreloc.c: revision 1.34
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.39
	libexec/ld.elf_so/symbol.c: revision 1.68
	libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.66
	libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.67
	libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.68
	libexec/ld.elf_so/arch/x86_64/mdreloc.c: revision 1.42
	libexec/ld.elf_so/arch/powerpc/ppc_reloc.c: revision 1.54
	libexec/ld.elf_so/Makefile: revision 1.137
	libexec/ld.elf_so/arch/vax/mdreloc.c: revision 1.32
	libexec/ld.elf_so/rtld.h: revision 1.127
	libexec/ld.elf_so/arch/vax/mdreloc.c: revision 1.33
	libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.38
	libexec/ld.elf_so/arch/alpha/alpha_reloc.c: revision 1.42
	libexec/ld.elf_so/map_object.c: revision 1.58
	libexec/ld.elf_so/arch/sparc/mdreloc.c: revision 1.49
Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.
The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.
Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.
Drop symbol number from default branch diagnostic, it isn't set at this
point and most likely not valid either.
Expand symnum, GCC's uninitialized used tracking is too imprecise.
Fix C&P bug. Deal with more MIPS hacks overriding def.
Add last_symnum, move up def and defobj.
Add back symnum, fix debug print.
Replace last use of r_type.
Fix indentation.
Fix indentation.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.32 2016/04/14 20:17:07 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.32 2016/04/14 20:17:07 skrll Exp $");
a54 3
	const Elf_Sym   *def = NULL;
	const Obj_Entry *defobj = NULL;
	unsigned long last_symnum = ULONG_MAX;
d58 2
d64 1
a64 21

		switch (ELF_R_TYPE(rela->r_info)) {
		case R_TYPE(GOT32):
		case R_TYPE(REL32):
		case R_TYPE(DIR32):
		case R_TYPE(GLOB_DAT):
		case R_TYPE(TLS_DTPOFF32):
		case R_TYPE(TLS_DTPMOD32):
		case R_TYPE(TLS_TPOFF32):
			symnum = ELF_R_SYM(rela->r_info);
			if (last_symnum != symnum) {
				last_symnum = symnum;
				def = _rtld_find_symdef(symnum, obj, &defobj,
				    false);
				if (def == NULL)
					return -1;
			}
			break;
		default:
			break;
		}
d72 4
d86 4
d101 4
d115 4
d154 4
d166 4
d179 4
d199 1
a199 2
			    (u_long)ELF_R_SYM(rela->r_info),
			    (u_long)ELF_R_TYPE(rela->r_info),
@


1.31
log
@Add basic support for indirect functions. It allows providing a public
function symbol with an implementation choosen at run time.
Refactor calls to functions by address in ld.elf_so to create temporary
function descriptors on the stack, if the address is not leaked outside.

Limitations:
- no support for initialising static storage with function pointers
- no support for unnamed resolver functions

Inspired by FreeBSD's r228435 by kib@@freebsd.org.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.30 2011/03/25 18:07:06 joerg Exp $	*/
d5 1
a5 6
__RCSID("$NetBSD: mdreloc.c,v 1.30 2011/03/25 18:07:06 joerg Exp $");
#endif /* not lint */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.30 2011/03/25 18:07:06 joerg Exp $");
@


1.30
log
@Add basic locking to ld.elf_so.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.29 2011/03/12 22:54:36 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.29 2011/03/12 22:54:36 joerg Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.29 2011/03/12 22:54:36 joerg Exp $");
d285 7
a291 1
	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
@


1.30.22.1
log
@Catch up to -current, via patch, requested by christos in ticket #1126:
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.30 2011/03/25 18:07:06 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.30 2011/03/25 18:07:06 joerg Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.30 2011/03/25 18:07:06 joerg Exp $");
d285 1
a285 7
	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
		if (tp == NULL)
			return 0;
		new_value = _rtld_resolve_ifunc(defobj, def);
	} else {
		new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
	}
@


1.29
log
@Add TLS support for AMD64, i386 and SH3.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $");
d247 1
d251 1
@


1.28
log
@Reduce header pollution for mdreloc.c. Make Obj_Entry argument of
_rtld_relocate_nonplt_objects non-const in preparation for TLS support.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.27 2010/01/13 20:17:22 christos Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.27 2010/01/13 20:17:22 christos Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.27 2010/01/13 20:17:22 christos Exp $");
d14 1
d158 43
@


1.27
log
@PR/39240: Satoshi Suetake: Don't fail when attempting to resolve weak symbols
when we are doing immediate binding, leave them alone and they will be dealt
with later during lazy binding. From skrll@@
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.26 2009/08/29 13:46:55 jmmv Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.26 2009/08/29 13:46:55 jmmv Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.26 2009/08/29 13:46:55 jmmv Exp $");
a13 1
#include <sys/stat.h>
d56 1
a56 1
_rtld_relocate_nonplt_objects(const Obj_Entry *obj)
@


1.26
log
@Remove trailing \n in calls to _rtld_error: a newline is automatically
added by a call to the function.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.25 2009/03/16 02:46:47 lukem Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.25 2009/03/16 02:46:47 lukem Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.25 2009/03/16 02:46:47 lukem Exp $");
d205 1
a205 1
	if (err || new_value == 0)
d230 1
d232 1
a232 1
	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
d234 2
a235 2
	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
	if (def == NULL)
d237 2
@


1.25
log
@Fix const issues (cast const pointers to "const uint8_t *" instead of "caddr_t")

NOTE: change based on ../i386/mdreloc.c 1.27
XXX: not compile tested
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.24 2008/07/24 06:51:58 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.24 2008/07/24 06:51:58 skrll Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.24 2008/07/24 06:51:58 skrll Exp $");
d166 1
a166 1
			    "in non-PLT relocations\n",
@


1.24
log
@RCSID police
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.23 2008/07/24 04:39:25 matt Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.23 2008/07/24 04:39:25 matt Exp $");
d10 1
a10 1
__RCSID("$NetBSD$");
d49 1
a49 1
	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
d198 1
a198 1
	const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
@


1.24.4.1
log
@Pull up following revision(s) via patch (requested by skrll in ticket #1724):
	rescue/list.ldd: revision 1.4
	lib/libc/dlfcn/dlfcn_elf.c: revision 1.7
	libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.57
	distrib/sets/lists/comp/mi: revision 1.1512
	share/man/man3/Makefile: revision 1.56
	libexec/ld.elf_so/arch/mips/mips_reloc.c: revision 1.58
	usr.bin/ldd/ldd.c: revision 1.15
	libexec/ld.elf_so/rtld.h: revision 1.84
	share/man/man3/dl_iterate_phdr.3: revision 1.1
	libexec/ld.elf_so/rtld.c: revision 1.129
	libexec/ld.elf_so/arch/powerpc/ppc_reloc.c: revision 1.44
	libexec/ld.elf_so/rtld.h: revision 1.89
	libexec/ld.elf_so/arch/x86_64/mdreloc.c: revision 1.36
	libexec/ld.elf_so/map_object.c: revision 1.41
	libexec/ld.elf_so/arch/x86_64/mdreloc.c: revision 1.37
	libexec/ld.elf_so/arch/sparc64/mdreloc.c: revision 1.46
	include/link_elf.h: revision 1.10
	libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.29
	libexec/ld.elf_so/arch/vax/mdreloc.c: revision 1.26
	libexec/ld.elf_so/arch/alpha/alpha_reloc.c: revision 1.34
	libexec/ld.elf_so/arch/hppa/hppa_reloc.c: revision 1.31
	libexec/ld.elf_so/arch/alpha/alpha_reloc.c: revision 1.35
	libexec/ld.elf_so/Makefile: revision 1.94
	libexec/ld.elf_so/arch/hppa/hppa_reloc.c: revision 1.32
	libexec/ld.elf_so/Makefile: revision 1.95
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.31
	libexec/ld.elf_so/Makefile: revision 1.96
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.32
	libexec/ld.elf_so/reloc.c: revision 1.98
	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.33
	sys/sys/exec_elf.h: revision 1.106
	libexec/ld.elf_so/rtld.c: revision 1.130
	libexec/ld.elf_so/load.c: revision 1.37
	libexec/ld.elf_so/rtld.c: revision 1.131
	libexec/ld.elf_so/load.c: revision 1.38
	libexec/ld.elf_so/rtld.h: revision 1.90
	libexec/ld.elf_so/headers.c: revision 1.36
	libexec/ld.elf_so/rtld.h: revision 1.95
	libexec/ld.elf_so/arch/i386/mdreloc.c: revision 1.30
	libexec/ld.elf_so/arch/m68k/mdreloc.c: revision 1.25
	libexec/ld.elf_so/symbol.c: revision 1.50
	libexec/ld.elf_so/symbol.c: revision 1.51
	libexec/ld.elf_so/arch/sparc/mdreloc.c: revision 1.43
	libexec/ld.elf_so/symbol.c: revision 1.52
	libexec/ld.elf_so/arch/sh3/mdreloc.c: revision 1.27
	libexec/ld.elf_so/symbol.c: revision 1.54
PR/39240: Satoshi Suetake: Don't fail when attempting to resolve weak symbols
when we are doing immediate binding, leave them alone and they will be dealt
with later during lazy binding. From skrll@@
Implement negative cache checks for symbol lookups.
Uses the Donelist idea from FreeBSD.
Use alloca(3) instead of local xmalloc for creating our DoneLists.
This allows threaded programs to use us a little better, PR lib/43005.
Implement dl_iterate_phdr.
Somewhat taken from FreeBSD. Manual page from OpenBSD.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.24 2008/07/24 06:51:58 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.24 2008/07/24 06:51:58 skrll Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.24 2008/07/24 06:51:58 skrll Exp $");
d205 1
a205 1
	if (err)
a229 1
	unsigned long info = rela->r_info;
d231 1
a231 1
	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
d233 2
a234 2
	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
	if (__predict_false(def == NULL))
a235 2
	if (__predict_false(def == &_rtld_sym_zero))
		return 0;
@


1.24.6.1
log
@Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.25 2009/03/16 02:46:47 lukem Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.25 2009/03/16 02:46:47 lukem Exp $");
d10 1
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.25 2009/03/16 02:46:47 lukem Exp $");
d49 1
a49 1
	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
d198 1
a198 1
	const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
@


1.23
log
@Refactor common code to _rtld_relocate_plt_object to i386 and arm so they
act like the other versions.
In _rtld_bind, if the result is 0, call _rtld_die.
Initialize _rtld_sym_zero.st_value to -_rtld_objself.maprelocbase.  Now when
the symbol is resolved, st_value + maprelocbase will equal 0 and the above
check in _rtld_bind will fire and a call to NULL will be avoided.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.22 2006/05/21 04:17:35 mrg Exp $	*/
d5 6
a10 1
__RCSID("$NetBSD: mdreloc.c,v 1.22 2006/05/21 04:17:35 mrg Exp $");
@


1.22
log
@avoid more GCC4 uninitialised variable problems.  (sh3 known, vax guessed.)
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.21 2005/08/20 19:01:17 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.21 2005/08/20 19:01:17 skrll Exp $");
d200 1
a200 1
	if (err)
@


1.22.20.1
log
@Sync with wrstuden-revivesa-base-2.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.22 2006/05/21 04:17:35 mrg Exp $	*/
d5 1
a5 6
__RCSID("$NetBSD: mdreloc.c,v 1.22 2006/05/21 04:17:35 mrg Exp $");
#endif /* not lint */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD$");
d200 1
a200 1
	if (err || new_value == 0)
@


1.21
log
@Add __RCSID.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.20 2005/05/31 19:30:38 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD$");
d197 2
@


1.20
log
@Support LD_BIND_NOW on sh3..
@
text
@d1 6
a6 1
/*	$NetBSD: mdreloc.c,v 1.19 2003/07/24 10:12:29 skrll Exp $	*/
@


1.19
log
@ANSIfy and de-__P().
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.18 2003/07/05 20:48:39 marcus Exp $	*/
d12 2
d189 25
d223 1
a223 1
		_rtld_die();
d231 4
a234 1
	return (caddr_t)new_value;
a235 1

@


1.19.4.1
log
@file mdreloc.c was added on branch netbsd-1-6 on 2004-05-28 08:31:22 +0000
@
text
@d1 206
@


1.19.4.2
log
@Apply patch (request by skrll in ticket #1702):
Bring "ld.elf_so" (mostly) in sync with NetBSD-current:
- MI and MD (e.g. under NetBSD-alpha) performance improvements
- RTLD_SELF, RTLD_NEXT, RTLD_DEFAULT support
- much better structured code
- closes PR bin/25464
@
text
@a0 206
/*	$NetBSD: mdreloc.c,v 1.19.4.1 2004/05/28 08:31:22 tron Exp $	*/

#include <sys/types.h>
#include <sys/stat.h>

#include "debug.h"
#include "rtld.h"

void _rtld_bind_start(void);
void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);

void
_rtld_setup_pltgot(const Obj_Entry *obj)
{
	obj->pltgot[1] = (Elf_Addr) obj;
	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
}

void
_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
{
	const Elf_Rela *rela = 0, *relalim;
	Elf_Addr relasz = 0;
	Elf_Addr *where;

	for (; dynp->d_tag != DT_NULL; dynp++) {
		switch (dynp->d_tag) {
		case DT_RELA:
			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
			break;
		case DT_RELASZ:
			relasz = dynp->d_un.d_val;
			break;
		}
	}
	relalim = (const Elf_Rela *)((caddr_t)rela + relasz);
	for (; rela < relalim; rela++) {
		where = (Elf_Addr *)(relocbase + rela->r_offset);
		*where = (Elf_Addr)(relocbase + rela->r_addend);
	}
}

int
_rtld_relocate_nonplt_objects(const Obj_Entry *obj)
{
	const Elf_Rela *rela;

	for (rela = obj->rela; rela < obj->relalim; rela++) {
		Elf_Addr        *where;
		const Elf_Sym   *def;
		const Obj_Entry *defobj;
		Elf_Addr         tmp;
		unsigned long	 symnum;

		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
		symnum = ELF_R_SYM(rela->r_info);

		switch (ELF_R_TYPE(rela->r_info)) {
		case R_TYPE(NONE):
			break;

#if 1 /* XXX should not occur */
		case R_TYPE(GOT32):
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
			    rela->r_addend);
			if (*where != tmp)
				*where = tmp;
			rdbg(("GOT32 %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;

		case R_TYPE(REL32):
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
			    rela->r_addend) - (Elf_Addr)where;
			if (*where != tmp)
				*where = tmp;
			rdbg(("PC32 %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;
#endif

		case R_TYPE(DIR32):
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
			    rela->r_addend);
			if (*where != tmp)
				*where = tmp;
			rdbg(("32 %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;

		case R_TYPE(GLOB_DAT):
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;

			tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
			    rela->r_addend;
			if (*where != tmp)
				*where = tmp;
			rdbg(("GLOB_DAT %s in %s --> %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, defobj->path));
			break;

		case R_TYPE(RELATIVE):
			if (rela->r_addend)
				*where = (Elf_Addr)obj->relocbase + rela->r_addend;
			else
				*where += (Elf_Addr)obj->relocbase;
			rdbg(("RELATIVE in %s --> %p", obj->path,
			    (void *)*where));
			break;

		case R_TYPE(COPY):
			/*
			 * These are deferred until all other relocations have
			 * been done.  All we do here is make sure that the
			 * COPY relocation is not in a shared library.  They
			 * are allowed only in executable files.
			 */
			if (obj->isdynamic) {
				_rtld_error(
			"%s: Unexpected R_COPY relocation in shared library",
				    obj->path);
				return -1;
			}
			rdbg(("COPY (avoid in main)"));
			break;

		default:
			rdbg(("sym = %lu, type = %lu, offset = %p, "
			    "addend = %p, contents = %p, symbol = %s",
			    symnum, (u_long)ELF_R_TYPE(rela->r_info),
			    (void *)rela->r_offset, (void *)rela->r_addend,
			    (void *)*where,
			    obj->strtab + obj->symtab[symnum].st_name));
			_rtld_error("%s: Unsupported relocation type %ld "
			    "in non-PLT relocations\n",
			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
			return -1;
		}
	}
	return 0;
}

int
_rtld_relocate_plt_lazy(const Obj_Entry *obj)
{
	const Elf_Rela *rela;

	if (!obj->relocbase)
		return 0;

	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);

		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));

		/* Just relocate the GOT slots pointing into the PLT */
		*where += (Elf_Addr)obj->relocbase;
		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
	}

	return 0;
}

caddr_t
_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
{
	const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff);
	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
	Elf_Addr new_value;
	const Elf_Sym  *def;
	const Obj_Entry *defobj;

	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));

	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
	if (def == NULL)
		_rtld_die();

	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
	rdbg(("bind now/fixup in %s --> old=%p new=%p",
	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
	if (*where != new_value)
		*where = new_value;

	return (caddr_t)new_value;
}

@


1.18
log
@Removed unused function.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.17 2003/07/05 18:18:51 tsutsui Exp $	*/
d45 1
a45 2
_rtld_relocate_nonplt_objects(obj)
	const Obj_Entry *obj;
d163 1
a163 2
_rtld_relocate_plt_lazy(obj)
	const Obj_Entry *obj;
d184 1
a184 3
_rtld_bind(obj, reloff)
	const Obj_Entry *obj;
	Elf_Word reloff;
@


1.17
log
@Add function prototype declarations.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.16 2003/07/02 15:29:34 marcus Exp $	*/
a11 1
int _rtld_relocate_plt_object(const Obj_Entry *, const Elf_Rela *, caddr_t *);
a210 27
int
_rtld_relocate_plt_object(obj, rela, addrp)
	const Obj_Entry *obj;
	const Elf_Rela *rela;
	caddr_t *addrp;
{
	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
	Elf_Addr new_value;
	const Elf_Sym  *def;
	const Obj_Entry *defobj;

	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));

	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
	if (def == NULL)
		return -1;

	new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
	    rela->r_addend);
	rdbg(("bind now/fixup in %s --> old=%p new=%p",
	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
	if (*where != new_value)
		*where = new_value;

	*addrp = (caddr_t)new_value;
	return 0;
}
@


1.16
log
@Bugfix: Don't pick addend both from reloc and contents at the same time.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.15 2003/06/30 00:50:46 marcus Exp $	*/
d10 3
@


1.15
log
@Updated to work with current rtld code.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.14 2002/10/03 20:39:23 mycroft Exp $	*/
d82 1
a82 1
			*where += (Elf_Addr)(defobj->relocbase + def->st_value +
d84 2
d97 1
a97 1
			*where += (Elf_Addr)(defobj->relocbase + def->st_value +
d99 2
@


1.14
log
@Skip the lazy PLT relocation if relocbase==0 (useful if libraries are loaded
at their VMA address).
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.13 2002/09/26 20:42:12 mycroft Exp $	*/
d18 24
d177 26
@


1.13
log
@Remove the `self' args to _rtld_relocate_objects() and
_rtld_relocate_nonplt_objects().
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.12 2002/09/12 22:56:31 mycroft Exp $	*/
d139 1
a139 1
	if (!obj->isdynamic)
@


1.12
log
@Nuke -DRTLD_RELOCATE_SELF and `dodebug' from orbit.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.11 2002/09/12 20:21:01 mycroft Exp $	*/
d19 1
a19 1
_rtld_relocate_nonplt_objects(obj, self)
a20 1
	bool self;
@


1.11
log
@_rtld_bind_start() is not used in MI code, so declare it in the MD code.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.10 2002/09/11 14:19:30 junyoung Exp $	*/
d19 1
a19 1
_rtld_relocate_nonplt_objects(obj, self, dodebug)
a21 1
	bool dodebug;
d49 1
a49 1
			rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
d61 1
a61 1
			rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
d74 1
a74 1
			rdbg(dodebug, ("32 %s in %s --> %p in %s",
d88 1
a88 1
			rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
d98 1
a98 1
			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
d115 1
a115 1
			rdbg(dodebug, ("COPY (avoid in main)"));
d119 1
a119 1
			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
d135 1
a135 1
_rtld_relocate_plt_lazy(obj, dodebug)
a136 1
	bool dodebug;
d150 1
a150 2
		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
		    (void *)*where));
d157 1
a157 1
_rtld_relocate_plt_object(obj, rela, addrp, dodebug)
a160 1
	bool dodebug;
d175 1
a175 1
	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
@


1.10
log
@Add $NetBSD$.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d8 2
@


1.9
log
@Pass down an additional flag to _rtld_relocate_nonplt_objects() which
indicates whether we're relocating ld.elf_so itself.  Use this in some places
rather than hackish tests on `dodebug'.  (The Alpha and HPPA `dodebug' tests
were actually noops, because RTLD_RELOCATE_SELF is not set, and therefore
dodebug is always true.)
@
text
@d1 2
@


1.8
log
@Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN.  Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT.  (This allows loading
non-relocatable objects, should we desire to do that at some point...)
@
text
@d15 1
a15 1
_rtld_relocate_nonplt_objects(obj, dodebug)
d17 1
@


1.7
log
@Re-poison a lot of consts, now that the mark shite is gone.
@
text
@d105 1
a105 1
			if (!obj->mainprog) {
d137 1
a137 1
	if (obj->mainprog)
@


1.6
log
@Split _rtld_relocate_plt_object() into two MD functions:
* _rtld_relocate_plt_lazy() fixes up all the relocs pointing to the PLT.  (On
  most platforms it just does a simple base-relative fixup; on SPARC it does
  nothing.)
* _rtld_relocate_plt_object() does immediate binding for a PLT entry.
The basic gist is that this saves a bit of time on SPARC (where the iteration
through the pltrela table was gratuitous), and a little less time on all other
platforms.  A whole lot of #ifdef'ed crap is moved out of reloc.c, too.

NOT tested on: hppa sh x86_64
@
text
@d16 1
a16 1
	Obj_Entry *obj;
d132 1
a132 1
	Obj_Entry *obj;
d156 1
a156 1
	Obj_Entry *obj;
@


1.5
log
@Partially fix up some debug printf()s that don't need to use defobj.
@
text
@d129 53
@


1.4
log
@A few things:
* Pass a symbol number to _rtld_find_symdef(), not a r_info.
* Don't try to do a symbol lookup when we find an unsupported relocation;
  instead get the symbol name from the referencing object's strtab.
* Add preliminary support for `-z combreloc'-style startup optimization on
  i386, `#ifdef COMBRELOC'.
@
text
@d46 2
a47 2
			    defobj->strtab + def->st_name, obj->path,
			    (void *)*where, defobj->path));
d58 2
a59 2
			    defobj->strtab + def->st_name, obj->path,
			    (void *)*where, defobj->path));
d71 2
a72 2
			    defobj->strtab + def->st_name, obj->path,
			    (void *)*where, defobj->path));
d85 2
a86 2
			    defobj->strtab + def->st_name, obj->path,
			    (void *)*where, defobj->path));
@


1.3
log
@Rename _rtld_relocate_nonplt_object() to _rtld_relocate_nonplt_objects(),
and push the outer loop into it.  This actually shaves a couple % off startup
time at least on PCs.
@
text
@d26 1
d29 1
d37 1
a37 2
			def = _rtld_find_symdef(rela->r_info, obj, &defobj,
			    false);
d51 1
a51 2
			def = _rtld_find_symdef(rela->r_info, obj, &defobj,
			    false);
d64 1
a64 2
			def = _rtld_find_symdef(rela->r_info, obj, &defobj,
			    false);
d76 1
a76 2
			def = _rtld_find_symdef(rela->r_info, obj, &defobj,
			    false);
a114 2
			def = _rtld_find_symdef(rela->r_info, obj, &defobj,
			    true);
d117 1
a117 2
			    (u_long)ELF_R_SYM(rela->r_info),
			    (u_long)ELF_R_TYPE(rela->r_info),
d120 1
a120 1
			    def ? defobj->strtab + def->st_name : "??"));
@


1.2
log
@Split _rtld_relocate_nonplt_object() into separate MD files.
@
text
@d15 1
a15 1
_rtld_relocate_nonplt_object(obj, rela, dodebug)
a16 1
	const Elf_Rela *rela;
d19 1
a19 6
	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
	const Elf_Sym   *def;
	const Obj_Entry *defobj;
	Elf_Addr         tmp;

	switch (ELF_R_TYPE(rela->r_info)) {
d21 11
a31 2
	case R_TYPE(NONE):
		break;
d34 27
a60 25
	case R_TYPE(GOT32):
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
		if (def == NULL)
			return -1;

		tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
		    rela->r_addend);
		if (*where != tmp) 
			*where = tmp;
		rdbg(dodebug, ("GOT32 %s in %s --> %p in %s",
		    defobj->strtab + def->st_name, obj->path,
		    (void *)*where, defobj->path));
		break;

	case R_TYPE(REL32):  
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
		if (def == NULL)
			return -1;

		*where += (Elf_Addr)(defobj->relocbase + def->st_value +
		    rela->r_addend) - (Elf_Addr)where;
		rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
		    defobj->strtab + def->st_name, obj->path,
		    (void *)*where, defobj->path));
		break;
d63 46
a108 44
	case R_TYPE(DIR32):
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
		if (def == NULL)
			return -1;

		*where += (Elf_Addr)(defobj->relocbase + def->st_value +
		    rela->r_addend);
		rdbg(dodebug, ("32 %s in %s --> %p in %s",
		    defobj->strtab + def->st_name, obj->path,
		    (void *)*where, defobj->path));
		break;

	case R_TYPE(GLOB_DAT):
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
		if (def == NULL)
			return -1;

		tmp = (Elf_Addr)(defobj->relocbase + def->st_value) +
		    rela->r_addend;
		if (*where != tmp)
			*where = tmp;
		rdbg(dodebug, ("GLOB_DAT %s in %s --> %p in %s",
		    defobj->strtab + def->st_name, obj->path,
		    (void *)*where, defobj->path));
		break;

	case R_TYPE(RELATIVE):
		if (rela->r_addend)
			*where = (Elf_Addr)obj->relocbase + rela->r_addend;
		else
			*where += (Elf_Addr)obj->relocbase;
		rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
		    (void *)*where));
		break;

	case R_TYPE(COPY):
		/*
		 * These are deferred until all other relocations have
		 * been done.  All we do here is make sure that the COPY
		 * relocation is not in a shared library.  They are allowed
		 * only in executable files.
		 */
		if (!obj->mainprog) {
			_rtld_error(
d110 19
a128 1
			    obj->path);
a130 16
		rdbg(dodebug, ("COPY (avoid in main)"));
		break;

	default:
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, true);
		rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
		    "addend = %p, contents = %p, symbol = %s",
		    (u_long)ELF_R_SYM(rela->r_info),
		    (u_long)ELF_R_TYPE(rela->r_info),
		    (void *)rela->r_offset, (void *)rela->r_addend,
		    (void *)*where,
		    def ? defobj->strtab + def->st_name : "??"));
		_rtld_error("%s: Unsupported relocation type %ld "
		    "in non-PLT relocations\n",
		    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
		return -1;
@


1.1
log
@Make _rtld_setup_pltgot() a consistent interface on all platforms.
@
text
@d13 112
@
