head	1.48;
access;
symbols
	netbsd-11-0-RC5:1.48
	netbsd-11-0-RC4:1.48
	netbsd-11-0-RC3:1.48
	netbsd-11-0-RC2:1.48
	netbsd-11-0-RC1:1.48
	perseant-exfatfs-base-20250801:1.48
	netbsd-11:1.48.0.2
	netbsd-11-base:1.48
	netbsd-10-1-RELEASE:1.45.6.1
	perseant-exfatfs-base-20240630:1.46
	perseant-exfatfs:1.46.0.2
	perseant-exfatfs-base:1.46
	netbsd-8-3-RELEASE:1.38.8.1
	netbsd-9-4-RELEASE:1.44.4.1
	netbsd-10-0-RELEASE:1.45.6.1
	netbsd-10-0-RC6:1.45.6.1
	netbsd-10-0-RC5:1.45.6.1
	netbsd-10-0-RC4:1.45.6.1
	netbsd-10-0-RC3:1.45.6.1
	netbsd-10-0-RC2:1.45.6.1
	netbsd-10-0-RC1:1.45.6.1
	netbsd-10:1.45.0.6
	netbsd-10-base:1.45
	netbsd-9-3-RELEASE:1.44
	cjep_sun2x-base1:1.45
	cjep_sun2x:1.45.0.4
	cjep_sun2x-base:1.45
	cjep_staticlib_x-base1:1.45
	netbsd-9-2-RELEASE:1.44
	cjep_staticlib_x:1.45.0.2
	cjep_staticlib_x-base:1.45
	netbsd-9-1-RELEASE:1.44
	phil-wifi-20200421:1.44
	phil-wifi-20200411:1.44
	is-mlppp:1.44.0.6
	is-mlppp-base:1.44
	phil-wifi-20200406:1.44
	netbsd-8-2-RELEASE:1.38.8.1
	netbsd-9-0-RELEASE:1.44
	netbsd-9-0-RC2:1.44
	netbsd-9-0-RC1:1.44
	phil-wifi-20191119:1.44
	netbsd-9:1.44.0.4
	netbsd-9-base:1.44
	phil-wifi-20190609:1.44
	netbsd-8-1-RELEASE:1.38.8.1
	netbsd-8-1-RC1:1.38.8.1
	pgoyette-compat-merge-20190127:1.43.2.1
	pgoyette-compat-20190127:1.44
	pgoyette-compat-20190118:1.44
	pgoyette-compat-1226:1.44
	pgoyette-compat-1126:1.44
	pgoyette-compat-1020:1.44
	pgoyette-compat-0930:1.44
	pgoyette-compat-0906:1.44
	netbsd-7-2-RELEASE:1.37.18.1
	pgoyette-compat-0728:1.44
	netbsd-8-0-RELEASE:1.38.8.1
	phil-wifi:1.44.0.2
	phil-wifi-base:1.44
	pgoyette-compat-0625:1.44
	netbsd-8-0-RC2:1.38.8.1
	pgoyette-compat-0521:1.44
	pgoyette-compat-0502:1.44
	pgoyette-compat-0422:1.44
	netbsd-8-0-RC1:1.38.8.1
	pgoyette-compat-0415:1.44
	pgoyette-compat-0407:1.44
	pgoyette-compat-0330:1.43
	pgoyette-compat-0322:1.43
	pgoyette-compat-0315:1.43
	netbsd-7-1-2-RELEASE:1.37.18.1
	pgoyette-compat:1.43.0.2
	pgoyette-compat-base:1.43
	netbsd-7-1-1-RELEASE:1.37.18.1
	matt-nb8-mediatek:1.38.8.1.0.2
	matt-nb8-mediatek-base:1.38.8.1
	perseant-stdc-iso10646:1.41.0.2
	perseant-stdc-iso10646-base:1.41
	netbsd-8:1.38.0.8
	netbsd-8-base:1.38
	prg-localcount2-base3:1.38
	prg-localcount2-base2:1.38
	prg-localcount2-base1:1.38
	prg-localcount2:1.38.0.6
	prg-localcount2-base:1.38
	pgoyette-localcount-20170426:1.38
	bouyer-socketcan-base1:1.38
	pgoyette-localcount-20170320:1.38
	netbsd-7-1:1.37.18.1.0.4
	netbsd-7-1-RELEASE:1.37.18.1
	netbsd-7-1-RC2:1.37.18.1
	netbsd-7-nhusb-base-20170116:1.37.18.1
	bouyer-socketcan:1.38.0.4
	bouyer-socketcan-base:1.38
	pgoyette-localcount-20170107:1.38
	netbsd-7-1-RC1:1.37.18.1
	pgoyette-localcount-20161104:1.38
	netbsd-7-0-2-RELEASE:1.37
	localcount-20160914:1.38
	netbsd-7-nhusb:1.37.18.1.0.2
	netbsd-7-nhusb-base:1.37.18.1
	pgoyette-localcount-20160806:1.38
	pgoyette-localcount-20160726:1.38
	pgoyette-localcount:1.38.0.2
	pgoyette-localcount-base:1.38
	netbsd-7-0-1-RELEASE:1.37
	netbsd-7-0:1.37.0.20
	netbsd-7-0-RELEASE:1.37
	netbsd-7-0-RC3:1.37
	netbsd-7-0-RC2:1.37
	netbsd-7-0-RC1:1.37
	netbsd-5-2-3-RELEASE:1.28.4.2
	netbsd-5-1-5-RELEASE:1.28
	netbsd-6-0-6-RELEASE:1.37
	netbsd-6-1-5-RELEASE:1.37
	netbsd-7:1.37.0.18
	netbsd-7-base:1.37
	yamt-pagecache-base9:1.37
	yamt-pagecache-tag8:1.36.4.1
	netbsd-6-1-4-RELEASE:1.37
	netbsd-6-0-5-RELEASE:1.37
	tls-earlyentropy:1.37.0.16
	tls-earlyentropy-base:1.37
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.37
	riastradh-drm2-base3:1.37
	netbsd-6-1-3-RELEASE:1.37
	netbsd-6-0-4-RELEASE:1.37
	netbsd-5-2-2-RELEASE:1.28.4.2
	netbsd-5-1-4-RELEASE:1.28
	netbsd-6-1-2-RELEASE:1.37
	netbsd-6-0-3-RELEASE:1.37
	netbsd-5-2-1-RELEASE:1.28.4.2
	netbsd-5-1-3-RELEASE:1.28
	netbsd-6-1-1-RELEASE:1.37
	riastradh-drm2-base2:1.37
	riastradh-drm2-base1:1.37
	riastradh-drm2:1.37.0.10
	riastradh-drm2-base:1.37
	netbsd-6-1:1.37.0.14
	netbsd-6-0-2-RELEASE:1.37
	netbsd-6-1-RELEASE:1.37
	netbsd-6-1-RC4:1.37
	netbsd-6-1-RC3:1.37
	agc-symver:1.37.0.12
	agc-symver-base:1.37
	netbsd-6-1-RC2:1.37
	netbsd-6-1-RC1:1.37
	yamt-pagecache-base8:1.37
	netbsd-5-2:1.28.4.2.0.2
	netbsd-6-0-1-RELEASE:1.37
	yamt-pagecache-base7:1.37
	netbsd-5-2-RELEASE:1.28.4.2
	netbsd-5-2-RC1:1.28.4.2
	matt-nb6-plus-nbase:1.37
	yamt-pagecache-base6:1.37
	netbsd-6-0:1.37.0.8
	netbsd-6-0-RELEASE:1.37
	netbsd-6-0-RC2:1.37
	tls-maxphys:1.37.0.6
	tls-maxphys-base:1.37
	matt-nb6-plus:1.37.0.4
	matt-nb6-plus-base:1.37
	netbsd-6-0-RC1:1.37
	yamt-pagecache-base5:1.37
	yamt-pagecache-base4:1.37
	netbsd-6:1.37.0.2
	netbsd-6-base:1.37
	netbsd-5-1-2-RELEASE:1.28
	netbsd-5-1-1-RELEASE:1.28
	yamt-pagecache-base3:1.36
	yamt-pagecache-base2:1.36
	yamt-pagecache:1.36.0.4
	yamt-pagecache-base:1.36
	cherry-xenmp:1.36.0.2
	cherry-xenmp-base:1.36
	bouyer-quota2-nbase:1.34
	bouyer-quota2:1.34.0.2
	bouyer-quota2-base:1.34
	matt-mips64-premerge-20101231:1.34
	matt-nb5-mips64-premerge-20101231:1.28
	matt-nb5-pq3:1.28.0.14
	matt-nb5-pq3-base:1.28
	netbsd-5-1:1.28.0.12
	netbsd-5-1-RELEASE:1.28
	netbsd-5-1-RC4:1.28
	matt-nb5-mips64-k15:1.28
	netbsd-5-1-RC3:1.28
	netbsd-5-1-RC2:1.28
	netbsd-5-1-RC1:1.28
	netbsd-5-0-2-RELEASE:1.28
	matt-nb5-mips64-premerge-20091211:1.28
	matt-premerge-20091211:1.30
	matt-nb5-mips64-u2-k2-k4-k7-k8-k9:1.28
	matt-nb4-mips64-k7-u2a-k9b:1.28
	matt-nb5-mips64-u1-k1-k5:1.28
	matt-nb5-mips64:1.28.0.10
	netbsd-5-0-1-RELEASE:1.28
	jym-xensuspend-nbase:1.29
	netbsd-5-0:1.28.0.8
	netbsd-5-0-RELEASE:1.28
	netbsd-5-0-RC4:1.28
	netbsd-5-0-RC3:1.28
	netbsd-5-0-RC2:1.28
	jym-xensuspend:1.28.0.6
	jym-xensuspend-base:1.29
	netbsd-5-0-RC1:1.28
	netbsd-5:1.28.0.4
	netbsd-5-base:1.28
	matt-mips64-base2:1.28
	matt-mips64:1.27.0.22
	netbsd-4-0-1-RELEASE:1.27
	wrstuden-revivesa-base-3:1.28
	wrstuden-revivesa-base-2:1.28
	wrstuden-fixsa-newbase:1.27
	wrstuden-revivesa-base-1:1.27
	yamt-pf42-base4:1.27
	yamt-pf42-base3:1.27
	hpcarm-cleanup-nbase:1.27
	yamt-pf42-baseX:1.27
	yamt-pf42-base2:1.27
	wrstuden-revivesa:1.27.0.20
	wrstuden-revivesa-base:1.27
	yamt-pf42:1.27.0.18
	yamt-pf42-base:1.27
	keiichi-mipv6:1.27.0.16
	keiichi-mipv6-base:1.27
	matt-armv6-nbase:1.27
	matt-armv6-prevmlocking:1.27
	wrstuden-fixsa-base-1:1.27
	netbsd-4-0:1.27.0.14
	netbsd-4-0-RELEASE:1.27
	cube-autoconf:1.27.0.12
	cube-autoconf-base:1.27
	netbsd-4-0-RC5:1.27
	netbsd-4-0-RC4:1.27
	netbsd-4-0-RC3:1.27
	netbsd-4-0-RC2:1.27
	netbsd-4-0-RC1:1.27
	matt-armv6:1.27.0.10
	matt-armv6-base:1.27
	matt-mips64-base:1.27
	hpcarm-cleanup:1.27.0.8
	hpcarm-cleanup-base:1.27
	netbsd-3-1-1-RELEASE:1.25
	netbsd-3-0-3-RELEASE:1.25
	wrstuden-fixsa:1.27.0.6
	wrstuden-fixsa-base:1.27
	abandoned-netbsd-4-base:1.27
	abandoned-netbsd-4:1.27.0.2
	netbsd-3-1:1.25.0.6
	netbsd-3-1-RELEASE:1.25
	netbsd-3-0-2-RELEASE:1.25
	netbsd-3-1-RC4:1.25
	netbsd-3-1-RC3:1.25
	netbsd-3-1-RC2:1.25
	netbsd-3-1-RC1:1.25
	netbsd-4:1.27.0.4
	netbsd-4-base:1.27
	netbsd-3-0-1-RELEASE:1.25
	netbsd-3-0:1.25.0.4
	netbsd-3-0-RELEASE:1.25
	netbsd-3-0-RC6:1.25
	netbsd-3-0-RC5:1.25
	netbsd-3-0-RC4:1.25
	netbsd-3-0-RC3:1.25
	netbsd-3-0-RC2:1.25
	netbsd-3-0-RC1:1.25
	netbsd-2-0-3-RELEASE:1.23
	netbsd-2-1:1.23.0.8
	netbsd-2-1-RELEASE:1.23
	netbsd-2-1-RC6:1.23
	netbsd-2-1-RC5:1.23
	netbsd-2-1-RC4:1.23
	netbsd-2-1-RC3:1.23
	netbsd-2-1-RC2:1.23
	netbsd-2-1-RC1:1.23
	netbsd-2-0-2-RELEASE:1.23
	netbsd-3:1.25.0.2
	netbsd-3-base:1.25
	netbsd-2-0-1-RELEASE:1.23
	netbsd-2:1.23.0.6
	netbsd-2-base:1.23
	netbsd-2-0-RELEASE:1.23
	netbsd-2-0-RC5:1.23
	netbsd-2-0-RC4:1.23
	netbsd-2-0-RC3:1.23
	netbsd-2-0-RC2:1.23
	netbsd-2-0-RC1:1.23
	netbsd-1-6:1.23.0.4
	netbsd-2-0:1.23.0.2
	netbsd-2-0-base:1.23
	fvdl_fs64_base:1.20;
locks; strict;
comment	@ * @;


1.48
date	2024.08.03.21.59.58;	author riastradh;	state Exp;
branches;
next	1.47;
commitid	R5nq4VHUPO6kqqkF;

1.47
date	2024.07.22.23.10.35;	author riastradh;	state Exp;
branches;
next	1.46;
commitid	p1FHYC54EYCwcTiF;

1.46
date	2023.06.04.01.24.57;	author joerg;	state Exp;
branches
	1.46.2.1;
next	1.45;
commitid	jBAtDE6SYslzbzrE;

1.45
date	2020.06.16.21.02.20;	author joerg;	state Exp;
branches
	1.45.6.1;
next	1.44;
commitid	kxwDbVAGN44NBucC;

1.44
date	2018.04.03.21.10.27;	author joerg;	state Exp;
branches
	1.44.4.1;
next	1.43;
commitid	pNUMy9CwSMm3w2xA;

1.43
date	2017.11.06.21.16.04;	author joerg;	state Exp;
branches
	1.43.2.1;
next	1.42;
commitid	XwUFbplE2Hyqj1eA;

1.42
date	2017.08.10.19.03.26;	author joerg;	state Exp;
branches;
next	1.41;
commitid	UgH5577k4GrjpH2A;

1.41
date	2017.06.20.12.41.49;	author joerg;	state Exp;
branches;
next	1.40;
commitid	gm1xZ7i4L5mjW6Wz;

1.40
date	2017.06.20.11.01.18;	author joerg;	state Exp;
branches;
next	1.39;
commitid	C7gP3cNt9D8Pn6Wz;

1.39
date	2017.06.19.11.57.01;	author joerg;	state Exp;
branches;
next	1.38;
commitid	pYVYx1tH1kZmHYVz;

1.38
date	2014.08.25.20.40.52;	author joerg;	state Exp;
branches
	1.38.8.1;
next	1.37;
commitid	hm6gwLhn9YorHMNx;

1.37
date	2011.11.18.16.10.03;	author joerg;	state Exp;
branches
	1.37.18.1;
next	1.36;

1.36
date	2011.04.12.16.40.04;	author matt;	state Exp;
branches
	1.36.4.1;
next	1.35;

1.35
date	2011.03.25.18.07.05;	author joerg;	state Exp;
branches;
next	1.34;

1.34
date	2010.08.06.16.33.17;	author joerg;	state Exp;
branches;
next	1.33;

1.33
date	2010.01.14.12.12.07;	author skrll;	state Exp;
branches;
next	1.32;

1.32
date	2010.01.14.11.57.06;	author skrll;	state Exp;
branches;
next	1.31;

1.31
date	2010.01.13.20.17.22;	author christos;	state Exp;
branches;
next	1.30;

1.30
date	2009.08.29.13.46.54;	author jmmv;	state Exp;
branches;
next	1.29;

1.29
date	2009.03.16.02.46.47;	author lukem;	state Exp;
branches;
next	1.28;

1.28
date	2008.07.24.04.39.25;	author matt;	state Exp;
branches
	1.28.4.1
	1.28.6.1;
next	1.27;

1.27
date	2005.12.24.20.59.30;	author perry;	state Exp;
branches
	1.27.20.1;
next	1.26;

1.26
date	2005.08.20.19.01.16;	author skrll;	state Exp;
branches;
next	1.25;

1.25
date	2004.12.17.10.53.27;	author mycroft;	state Exp;
branches;
next	1.24;

1.24
date	2004.08.21.11.14.07;	author rearnsha;	state Exp;
branches;
next	1.23;

1.23
date	2003.07.26.15.04.38;	author mrg;	state Exp;
branches
	1.23.4.1;
next	1.22;

1.22
date	2003.07.24.10.12.27;	author skrll;	state Exp;
branches;
next	1.21;

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

1.20
date	2002.10.03.20.39.22;	author mycroft;	state Exp;
branches;
next	1.19;

1.19
date	2002.09.26.20.42.11;	author mycroft;	state Exp;
branches;
next	1.18;

1.18
date	2002.09.25.07.27.51;	author mycroft;	state Exp;
branches;
next	1.17;

1.17
date	2002.09.15.01.08.04;	author thorpej;	state Exp;
branches;
next	1.16;

1.16
date	2002.09.15.00.52.08;	author thorpej;	state Exp;
branches;
next	1.15;

1.15
date	2002.09.13.05.45.46;	author mycroft;	state Exp;
branches;
next	1.14;

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

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

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

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

1.10
date	2002.09.09.19.28.40;	author mycroft;	state Exp;
branches;
next	1.9;

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

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

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

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

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

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

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

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

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

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

1.45.6.1
date	2023.08.01.16.34.58;	author martin;	state Exp;
branches;
next	;
commitid	ygiHGMga8HEgs6zE;

1.44.4.1
date	2023.08.04.12.55.46;	author martin;	state Exp;
branches;
next	1.44.4.2;
commitid	WQUk0iGBkKS39tzE;

1.44.4.2
date	2024.08.07.10.45.41;	author martin;	state Exp;
branches;
next	;
commitid	oeC8MTiJlGv6zSkF;

1.43.2.1
date	2018.04.07.04.12.09;	author pgoyette;	state Exp;
branches;
next	;
commitid	ZMkKSYuBapz7LsxA;

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

1.37.18.1
date	2016.03.06.18.17.55;	author martin;	state Exp;
branches;
next	;
commitid	Ns3WeAQc8ORoVBXy;

1.36.4.1
date	2012.04.17.00.05.36;	author yamt;	state Exp;
branches;
next	;

1.28.4.1
date	2012.03.17.18.28.37;	author bouyer;	state Exp;
branches;
next	1.28.4.2;

1.28.4.2
date	2012.03.30.19.23.35;	author bouyer;	state Exp;
branches;
next	;

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

1.27.20.1
date	2008.09.18.04.39.17;	author wrstuden;	state Exp;
branches;
next	;

1.23.4.1
date	2003.07.26.15.04.38;	author tron;	state dead;
branches;
next	1.23.4.2;

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


desc
@@


1.48
log
@ld.elf_so(1): Nix trailing whitespace.

No functional change intended.
@
text
@/*	$NetBSD: mdreloc.c,v 1.47 2024/07/22 23:10:35 riastradh Exp $	*/

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.47 2024/07/22 23:10:35 riastradh Exp $");
#endif /* not lint */

/*
 * Arm (32-bit) ELF relocations.
 *
 * Reference:
 *
 *	[AAELF32] ELF for the Arm Architecture, 2022Q3.  Arm Ltd.
 *	https://github.com/ARM-software/abi-aa/blob/2982a9f3b512a5bfdc9e3fea5d3b298f9165c36b/aaelf32/aaelf32.rst
 */

#include <sys/types.h>
#include <string.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_Rel *rel = 0, *rellim;
	Elf_Addr relsz = 0;
	Elf_Addr *where;

	for (; dynp->d_tag != DT_NULL; dynp++) {
		switch (dynp->d_tag) {
		case DT_REL:
			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
			break;
		case DT_RELSZ:
			relsz = dynp->d_un.d_val;
			break;
		}
	}
	rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
	for (; rel < rellim; rel++) {
		where = (Elf_Addr *)(relocbase + rel->r_offset);
		*where += (Elf_Addr)relocbase;
	}
}

/*
 * It is possible for the compiler to emit relocations for unaligned data.
 * We handle this situation with these inlines.
 */
#define	RELOC_ALIGNED_P(x) \
	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)

static inline Elf_Addr
load_ptr(void *where)
{
	Elf_Addr res;

	memcpy(&res, where, sizeof(res));

	return (res);
}

static inline void
store_ptr(void *where, Elf_Addr val)
{

	memcpy(where, &val, sizeof(val));
}

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

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

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

		switch (ELF_R_TYPE(rel->r_info)) {
		case R_TYPE(PC24):	/* word32 S - P + A */
		case R_TYPE(ABS32):	/* word32 B + S + A */
		case R_TYPE(GLOB_DAT):	/* word32 B + S */
		case R_TYPE(TLS_DTPOFF32):
		case R_TYPE(TLS_DTPMOD32):
		case R_TYPE(TLS_TPOFF32):
			symnum = ELF_R_SYM(rel->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(rel->r_info)) {
		case R_TYPE(NONE):
			break;

#if 1 /* XXX should not occur */
		case R_TYPE(PC24): {	/* word32 S - P + A */
			Elf32_Sword addend;

			/*
			 * Extract addend and sign-extend if needed.
			 */
			addend = *where;
			if (addend & 0x00800000)
				addend |= 0xff000000;
			tmp = (Elf_Addr)obj->relocbase + def->st_value
			    - (Elf_Addr)where + (addend << 2);
			if ((tmp & 0xfe000000) != 0xfe000000 &&
			    (tmp & 0xfe000000) != 0) {
				_rtld_error(
				"%s: R_ARM_PC24 relocation @@ %p to %s failed "
				"(displacement %ld (%#lx) out of range)",
				    obj->path, where,
				    obj->strtab + obj->symtab[
				        ELF_R_SYM(rel->r_info)].st_name,
				    (long) tmp, (long) tmp);
				return -1;
			}
			tmp >>= 2;
			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
			rdbg(("PC24 %s in %s --> %p @@ %p in %s",
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)*where, where,
			    defobj->path));
			break;
		}
#endif

		case R_TYPE(ABS32):	/* word32 B + S + A */
		case R_TYPE(GLOB_DAT):	/* word32 B + S */
			if (__predict_true(RELOC_ALIGNED_P(where))) {
				tmp = *where + (Elf_Addr)defobj->relocbase +
				    def->st_value;
				/* Set the Thumb bit, if needed.  */
				if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
				    tmp |= 1;
				*where = tmp;
			} else {
				tmp = load_ptr(where) +
				    (Elf_Addr)defobj->relocbase +
				    def->st_value;
				/* Set the Thumb bit, if needed.  */
				if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
				    tmp |= 1;
				store_ptr(where, tmp);
			}
			rdbg(("ABS32/GLOB_DAT %s in %s --> %p @@ %p in %s",
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)tmp, where,
			    defobj->path));
			break;

		case R_TYPE(IRELATIVE):
			/* IFUNC relocations are handled in _rtld_call_ifunc */
			if (obj->ifunc_remaining_nonplt == 0)
				obj->ifunc_remaining_nonplt = obj->rellim - rel;
			/* FALL-THROUGH */

		case R_TYPE(RELATIVE):	/* word32 B + A */
			if (__predict_true(RELOC_ALIGNED_P(where))) {
				tmp = *where + (Elf_Addr)obj->relocbase;
				*where = tmp;
			} else {
				tmp = load_ptr(where) +
				    (Elf_Addr)obj->relocbase;
				store_ptr(where, tmp);
			}
			rdbg(("RELATIVE in %s --> %p", obj->path,
			    (void *)tmp));
			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):
			tmp = (Elf_Addr)(def->st_value);
			if (__predict_true(RELOC_ALIGNED_P(where)))
				*where = tmp;
			else
				store_ptr(where, tmp);

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

			break;
		case R_TYPE(TLS_DTPMOD32):
			tmp = (Elf_Addr)(defobj->tlsindex);
			if (__predict_true(RELOC_ALIGNED_P(where)))
				*where = tmp;
			else
				store_ptr(where, tmp);

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

			break;

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

			if (__predict_true(RELOC_ALIGNED_P(where)))
				tmp = *where;
			else
				tmp = load_ptr(where);
			tmp += (Elf_Addr)def->st_value + defobj->tlsoffset + sizeof(struct tls_tcb);
			if (__predict_true(RELOC_ALIGNED_P(where)))
				*where = tmp;
			else
				store_ptr(where, tmp);
			rdbg(("TLS_TPOFF32 %s in %s --> %p",
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)tmp));
			break;

		default:
			rdbg(("sym = %lu, type = %lu, offset = %p, "
			    "contents = %p",
			    (u_long)ELF_R_SYM(rel->r_info),
			    (u_long)ELF_R_TYPE(rel->r_info),
			    (void *)rel->r_offset, (void *)load_ptr(where)));
			_rtld_error("%s: Unsupported relocation type %ld "
			    "in non-PLT relocations",
			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
			return -1;
		}
	}
	return 0;
}

int
_rtld_relocate_plt_lazy(Obj_Entry *obj)
{
	const Elf_Rel *rel;

	for (rel = obj->pltrellim; rel-- > obj->pltrel; ) {
		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);

		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT) ||
		       ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE));

		if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE))
			obj->ifunc_remaining = obj->pltrellim - rel;

		/* 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;
}

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

	assert(ELF_R_TYPE(info) == R_TYPE(JUMP_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);
	}
	/* Set the Thumb bit, if needed.  */
	if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
		new_value |= 1;
	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;
}

caddr_t
_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
{
	const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff);
	Elf_Addr new_value = 0;	/* XXX gcc */
	int err;

	_rtld_shared_enter();
	err = _rtld_relocate_plt_object(obj, rel, &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_Rel *rel;
	int err = 0;

	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
		err = _rtld_relocate_plt_object(obj, rel, NULL);
		if (err)
			break;
	}

	return err;
}
@


1.47
log
@ld.elf_so: Cite reference for Arm ELF relocations.

PR lib/58455: Missing references for processor-specific ELF
relocation semantics
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.46 2023/06/04 01:24:57 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.46 2023/06/04 01:24:57 joerg Exp $");
d353 1
a353 1
	
@


1.46
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.45 2020/06/16 21:02:20 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.45 2020/06/16 21:02:20 joerg Exp $");
d8 9
@


1.46.2.1
log
@Sync with HEAD
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.48 2024/08/03 21:59:58 riastradh Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.48 2024/08/03 21:59:58 riastradh Exp $");
a7 9
/*
 * Arm (32-bit) ELF relocations.
 *
 * Reference:
 *
 *	[AAELF32] ELF for the Arm Architecture, 2022Q3.  Arm Ltd.
 *	https://github.com/ARM-software/abi-aa/blob/2982a9f3b512a5bfdc9e3fea5d3b298f9165c36b/aaelf32/aaelf32.rst
 */

d344 1
a344 1

@


1.45
log
@R_ARM_TLS_TPOFF32 needs to adjust the existing value, not blindly
overwrite it.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.44 2018/04/03 21:10:27 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.44 2018/04/03 21:10:27 joerg Exp $");
d231 2
a232 2
			if (!defobj->tls_done &&
			    _rtld_tls_offset_allocate(obj))
@


1.45.6.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.46 2023/06/04 01:24:57 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.46 2023/06/04 01:24:57 joerg Exp $");
d231 2
a232 2
			if (!defobj->tls_static &&
			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
@


1.44
log
@Rework ifunc support to address a number of short comings:
- Move to a shared _rtld_call_ifunc for rel and rela architectures
- Architectures using rel format must patch IRELATIVE non-PLT
  relocations like RELATIVE in additition to the later ifunc handling
- Consistently record the delta to the end of the relocation group for
  non-PLT IRELATIVE relocations

Hidden ifunc is now supported on all ifunc platforms, even when using
-fno-plt. The combination of -fno-plt and relro is broken due to
incorrect GNU ld output though.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.43 2017/11/06 21:16:04 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.43 2017/11/06 21:16:04 joerg Exp $");
d235 5
a239 2
			tmp = (Elf_Addr)def->st_value + defobj->tlsoffset +
			    sizeof(struct tls_tcb);
@


1.44.4.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.44 2018/04/03 21:10:27 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.44 2018/04/03 21:10:27 joerg Exp $");
d231 2
a232 2
			if (!defobj->tls_static &&
			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
@


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

	libexec/ld.elf_so/arch/arm/mdreloc.c: revision 1.45
	libexec/ld.elf_so/arch/aarch64/mdreloc.c: revision 1.14

Honor addend for R_AARCH64_TLS_TPREL relocation.

R_ARM_TLS_TPOFF32 needs to adjust the existing value, not blindly
overwrite it.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.44.4.1 2023/08/04 12:55:46 martin Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.44.4.1 2023/08/04 12:55:46 martin Exp $");
d235 2
a236 5
			if (__predict_true(RELOC_ALIGNED_P(where)))
				tmp = *where;
			else
				tmp = load_ptr(where);
			tmp += (Elf_Addr)def->st_value + defobj->tlsoffset + sizeof(struct tls_tcb);
@


1.43
log
@init/fini for the main program is handled by crt0.o, so ifunc handling
is skipped right now as it iterates the same list. Don't repeat that
mistake and explicitly take care of it in the dynamic linker.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.42 2017/08/10 19:03:26 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.42 2017/08/10 19:03:26 joerg Exp $");
d170 6
a282 20
void
_rtld_call_ifunc(Obj_Entry *obj, sigset_t *mask, u_int cur_objgen)
{
	const Elf_Rel *rel;
	Elf_Addr *where, target;

	while (obj->ifunc_remaining > 0 && _rtld_objgen == cur_objgen) {
		rel = obj->pltrellim - obj->ifunc_remaining;
		--obj->ifunc_remaining;
		if (ELF_R_TYPE(rel->r_info) == R_TYPE(IRELATIVE)) {
			where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
			_rtld_exclusive_exit(mask);
			target = _rtld_resolve_ifunc2(obj, *where);
			_rtld_exclusive_enter(mask);
			if (*where != target)
				*where = target;
		}
	}
}

@


1.43.2.1
log
@Sync with HEAD.  77 conflicts resolved - all of them $NetBSD$
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.44 2018/04/03 21:10:27 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.44 2018/04/03 21:10:27 joerg Exp $");
a169 6
		case R_TYPE(IRELATIVE):
			/* IFUNC relocations are handled in _rtld_call_ifunc */
			if (obj->ifunc_remaining_nonplt == 0)
				obj->ifunc_remaining_nonplt = obj->rellim - rel;
			/* FALL-THROUGH */

d277 20
@


1.42
log
@Add IRELATIVE support for ARM, X86 and PowerPC.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.41 2017/06/20 12:41:49 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.41 2017/06/20 12:41:49 joerg Exp $");
a259 3
	if (!obj->relocbase)
		return 0;

@


1.41
log
@Expand symnum, GCC's uninitialized used tracking is too imprecise.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.40 2017/06/20 11:01:18 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.40 2017/06/20 11:01:18 joerg Exp $");
d256 1
a256 1
_rtld_relocate_plt_lazy(const Obj_Entry *obj)
d263 1
a263 1
	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
d266 5
a270 1
		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
d280 20
@


1.40
log
@Drop symbol number from default branch diagnostic, it isn't set at this
point and most likely not valid either.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.39 2017/06/19 11:57:01 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.39 2017/06/19 11:57:01 joerg Exp $");
d131 2
a132 1
				    obj->strtab + obj->symtab[symnum].st_name,
d139 3
a141 2
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, where, defobj->path));
d165 3
a167 2
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)tmp, where, defobj->path));
d207 2
a208 2
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)tmp));
d219 2
a220 2
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)tmp));
d236 2
a237 2
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)tmp));
@


1.39
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.38 2014/08/25 20:40:52 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.38 2014/08/25 20:40:52 joerg Exp $");
d239 1
a239 1
			    "contents = %p, symbol = %s",
d242 1
a242 2
			    (void *)rel->r_offset, (void *)load_ptr(where),
			    obj->strtab + obj->symtab[symnum].st_name));
@


1.38
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.37 2011/11/18 16:10:03 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.37 2011/11/18 16:10:03 joerg Exp $");
d77 3
a82 2
		const Elf_Sym   *def;
		const Obj_Entry *defobj;
d87 21
a107 1
		symnum = ELF_R_SYM(rel->r_info);
a122 4

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

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

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

d240 2
a241 1
			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
@


1.38.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.38 2014/08/25 20:40:52 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.38 2014/08/25 20:40:52 joerg Exp $");
a76 3
	const Elf_Sym *def = NULL;
	const Obj_Entry *defobj = NULL;
	unsigned long last_symnum = ULONG_MAX;
d80 2
d86 1
a86 21

		switch (ELF_R_TYPE(rel->r_info)) {
		case R_TYPE(PC24):	/* word32 S - P + A */
		case R_TYPE(ABS32):	/* word32 B + S + A */
		case R_TYPE(GLOB_DAT):	/* word32 B + S */
		case R_TYPE(TLS_DTPOFF32):
		case R_TYPE(TLS_DTPMOD32):
		case R_TYPE(TLS_TPOFF32):
			symnum = ELF_R_SYM(rel->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;
		}
d102 4
d114 1
a114 2
				    obj->strtab + obj->symtab[
				        ELF_R_SYM(rel->r_info)].st_name,
d121 2
a122 3
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)*where, where,
			    defobj->path));
d129 3
d149 2
a150 3
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)tmp, where,
			    defobj->path));
d183 4
d194 2
a195 2
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)tmp));
d199 4
d210 2
a211 2
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)tmp));
d216 4
d231 2
a232 2
			    obj->strtab + obj->symtab[ELF_R_SYM(rel->r_info)]
			    .st_name, obj->path, (void *)tmp));
d237 4
a240 4
			    "contents = %p",
			    (u_long)ELF_R_SYM(rel->r_info),
			    (u_long)ELF_R_TYPE(rel->r_info),
			    (void *)rel->r_offset, (void *)load_ptr(where)));
@


1.37
log
@Hook up TLS support on ARM. Tested by martin@@, kernel support from matt@@
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.36 2011/04/12 16:40:04 matt Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.36 2011/04/12 16:40:04 matt Exp $");
d289 7
a295 1
	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
@


1.37.18.1
log
@Catch up to -current, via patch, requested by christos in ticket #1126:
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.37 2011/11/18 16:10:03 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.37 2011/11/18 16:10:03 joerg Exp $");
d289 1
a289 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.36
log
@Add support for TLS relocations.
From Joerg Sonnenberger
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.35 2011/03/25 18:07:05 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.35 2011/03/25 18:07:05 joerg Exp $");
a181 1
#ifdef __HAVE_TLS_VARIANT_I
a233 1
#endif /* __HAVE_TLS_VARIANT_I */
@


1.36.4.1
log
@sync with head
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.36 2011/04/12 16:40:04 matt Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.36 2011/04/12 16:40:04 matt Exp $");
d182 1
d235 1
@


1.35
log
@Add basic locking to ld.elf_so.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.34 2010/08/06 16:33:17 joerg Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.34 2010/08/06 16:33:17 joerg Exp $");
d182 55
@


1.34
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.33 2010/01/14 12:12:07 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.33 2010/01/14 12:12:07 skrll Exp $");
d257 1
d261 1
@


1.33
log
@Make DEBUG build compile.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.32 2010/01/14 11:57:06 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.32 2010/01/14 11:57:06 skrll Exp $");
a8 2
#include <sys/stat.h>

d74 1
a74 1
_rtld_relocate_nonplt_objects(const Obj_Entry *obj)
@


1.32
log
@Shut gcc up.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.31 2010/01/13 20:17:22 christos Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.31 2010/01/13 20:17:22 christos Exp $");
d230 1
a230 1
	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
@


1.31
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.30 2009/08/29 13:46:54 jmmv Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.30 2009/08/29 13:46:54 jmmv Exp $");
d256 1
a256 1
	Elf_Addr new_value;
@


1.30
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.29 2009/03/16 02:46:47 lukem Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $");
d228 1
d230 1
a230 1
	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
d232 2
a233 2
	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
	if (def == NULL)
d235 2
d260 1
a260 1
	if (err || new_value == 0)
@


1.29
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.28 2008/07/24 04:39:25 matt Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.28 2008/07/24 04:39:25 matt Exp $");
d191 1
a191 1
			    "in non-PLT relocations\n",
@


1.28
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.27 2005/12/24 20:59:30 perry Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.27 2005/12/24 20:59:30 perry Exp $");
d44 1
a44 1
	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
d252 1
a252 1
	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
@


1.28.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.28 2008/07/24 04:39:25 matt Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.28 2008/07/24 04:39:25 matt Exp $");
a227 1
	unsigned long info = rel->r_info;
d229 1
a229 1
	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
d231 2
a232 2
	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
	if (__predict_false(def == NULL))
a233 2
	if (__predict_false(def == &_rtld_sym_zero))
		return 0;
d257 1
a257 1
	if (err)
@


1.28.4.2
log
@Apply patch, requested by skrll in ticket #1724
	src/libexec/ld.elf_so/arch/alpha/alpha_reloc.c	patch
	src/libexec/ld.elf_so/arch/arm/mdreloc.c	patch
	src/libexec/ld.elf_so/arch/hppa/hppa_reloc.c	patch

Fix gcc uninitialized variable warning.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.28.4.1 2012/03/17 18:28:37 bouyer Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.28.4.1 2012/03/17 18:28:37 bouyer Exp $");
d256 1
a256 1
	Elf_Addr new_value = 0; /* XXX gcc */
@


1.28.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.29 2009/03/16 02:46:47 lukem Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.29 2009/03/16 02:46:47 lukem Exp $");
d44 1
a44 1
	rellim = (const Elf_Rel *)((const uint8_t *)rel + relsz);
d252 1
a252 1
	const Elf_Rel *rel = (const Elf_Rel *)((const uint8_t *)obj->pltrel + reloff);
@


1.27
log
@Remove leading __ from __(const|inline|signed|volatile) -- it is obsolete.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.26 2005/08/20 19:01:16 skrll Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.26 2005/08/20 19:01:16 skrll Exp $");
d220 3
a222 2
caddr_t
_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
a223 1
	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
d233 1
a233 1
		_rtld_die();
d243 16
a261 1

d266 1
d269 3
a271 21
		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
		Elf_Addr target;
		const Elf_Sym *def;
		const Obj_Entry *defobj;
		
		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
		
		def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
		    true);
		if (def == NULL)
			return -1;
		target = (Elf_Addr)(defobj->relocbase + def->st_value);
		/* Set the Thumb bit, if needed.  */
		if (ELF_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
			target |= 1;

		rdbg(("bind now/fixup in %s --> old=%p new=%p",
		    defobj->strtab + def->st_name, (void *)*where, 
		    (void *)target));
		if (*where != target)
			*where = target;
d273 2
a274 1
	return 0;
@


1.27.20.1
log
@Sync with wrstuden-revivesa-base-2.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.27 2005/12/24 20:59:30 perry Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD: mdreloc.c,v 1.27 2005/12/24 20:59:30 perry Exp $");
d220 2
a221 3
static int
_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rel *rel,
	Elf_Addr *tp)
d223 1
d233 1
a233 1
		return -1;
a242 2
	if (tp)
		*tp = new_value;
d244 1
a244 1
	return 0;
a246 13
caddr_t
_rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
{
	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
	Elf_Addr new_value;
	int err;

	err = _rtld_relocate_plt_object(obj, rel, &new_value);
	if (err || new_value == 0)
		_rtld_die();

	return (caddr_t)new_value;
}
a250 1
	int err = 0;
d253 21
a273 3
		err = _rtld_relocate_plt_object(obj, rel, NULL);
		if (err)
			break;
d275 1
a275 2

	return err;
@


1.26
log
@Add __RCSID.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.25 2004/12/17 10:53:27 mycroft Exp $	*/
d5 1
a5 1
__RCSID("$NetBSD$");
d58 1
a58 1
static __inline Elf_Addr
d68 1
a68 1
static __inline void
@


1.25
log
@ELF32_ST_TYPE -> ELF_ST_TYPE
@
text
@d1 6
a6 1
/*	$NetBSD: mdreloc.c,v 1.24 2004/08/21 11:14:07 rearnsha Exp $	*/
@


1.24
log
@Check the type of symbols; if it's a Thumb function, then set the Thumb
bit in the relocated value (JUMP_SLOT, ABS32 & GLOB_DAT).
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.23 2003/07/26 15:04:38 mrg Exp $	*/
d133 1
a133 1
				if (ELF32_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
d141 1
a141 1
				if (ELF32_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
d232 1
a232 1
	if (ELF32_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
d261 1
a261 1
		if (ELF32_ST_TYPE(def->st_info) == STT_ARM_TFUNC)
@


1.23
log
@include <string.h> or <stdlib.h> for all prototypes
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.22 2003/07/24 10:12:27 skrll Exp $	*/
d132 3
d140 3
d231 3
d260 4
@


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


1.23.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 258
/*	$NetBSD: mdreloc.c,v 1.23.4.1 2004/05/28 08:31:22 tron Exp $	*/

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

#include <string.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_Rel *rel = 0, *rellim;
	Elf_Addr relsz = 0;
	Elf_Addr *where;

	for (; dynp->d_tag != DT_NULL; dynp++) {
		switch (dynp->d_tag) {
		case DT_REL:
			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
			break;
		case DT_RELSZ:
			relsz = dynp->d_un.d_val;
			break;
		}
	}
	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
	for (; rel < rellim; rel++) {
		where = (Elf_Addr *)(relocbase + rel->r_offset);
		*where += (Elf_Addr)relocbase;
	}
}

/*
 * It is possible for the compiler to emit relocations for unaligned data.
 * We handle this situation with these inlines.
 */
#define	RELOC_ALIGNED_P(x) \
	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)

static __inline Elf_Addr
load_ptr(void *where)
{
	Elf_Addr res;

	memcpy(&res, where, sizeof(res));

	return (res);
}

static __inline void
store_ptr(void *where, Elf_Addr val)
{

	memcpy(where, &val, sizeof(val));
}

int
_rtld_relocate_nonplt_objects(const Obj_Entry *obj)
{
	const Elf_Rel *rel;

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

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

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

#if 1 /* XXX should not occur */
		case R_TYPE(PC24): {	/* word32 S - P + A */
			Elf32_Sword addend;

			/*
			 * Extract addend and sign-extend if needed.
			 */
			addend = *where;
			if (addend & 0x00800000)
				addend |= 0xff000000;

			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;
			tmp = (Elf_Addr)obj->relocbase + def->st_value
			    - (Elf_Addr)where + (addend << 2);
			if ((tmp & 0xfe000000) != 0xfe000000 &&
			    (tmp & 0xfe000000) != 0) {
				_rtld_error(
				"%s: R_ARM_PC24 relocation @@ %p to %s failed "
				"(displacement %ld (%#lx) out of range)",
				    obj->path, where,
				    obj->strtab + obj->symtab[symnum].st_name,
				    (long) tmp, (long) tmp);
				return -1;
			}
			tmp >>= 2;
			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
			rdbg(("PC24 %s in %s --> %p @@ %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, where, defobj->path));
			break;
		}
#endif

		case R_TYPE(ABS32):	/* word32 B + S + A */
		case R_TYPE(GLOB_DAT):	/* word32 B + S */
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;
			if (__predict_true(RELOC_ALIGNED_P(where))) {
				tmp = *where + (Elf_Addr)defobj->relocbase +
				    def->st_value;
				*where = tmp;
			} else {
				tmp = load_ptr(where) +
				    (Elf_Addr)defobj->relocbase +
				    def->st_value;
				store_ptr(where, tmp);
			}
			rdbg(("ABS32/GLOB_DAT %s in %s --> %p @@ %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)tmp, where, defobj->path));
			break;

		case R_TYPE(RELATIVE):	/* word32 B + A */
			if (__predict_true(RELOC_ALIGNED_P(where))) {
				tmp = *where + (Elf_Addr)obj->relocbase;
				*where = tmp;
			} else {
				tmp = load_ptr(where) +
				    (Elf_Addr)obj->relocbase;
				store_ptr(where, tmp);
			}
			rdbg(("RELATIVE in %s --> %p", obj->path,
			    (void *)tmp));
			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, "
			    "contents = %p, symbol = %s",
			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
			    (void *)rel->r_offset, (void *)load_ptr(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(rel->r_info));
			return -1;
		}
	}
	return 0;
}

int
_rtld_relocate_plt_lazy(const Obj_Entry *obj)
{
	const Elf_Rel *rel;

	if (!obj->relocbase)
		return 0;

	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);

		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_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_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
	Elf_Addr new_value;
	const Elf_Sym  *def;
	const Obj_Entry *defobj;

	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));

	def = _rtld_find_symdef(ELF_R_SYM(rel->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;
}

int
_rtld_relocate_plt_objects(const Obj_Entry *obj)
{
	const Elf_Rel *rel;
	
	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
		Elf_Addr target;
		const Elf_Sym *def;
		const Obj_Entry *defobj;
		
		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
		
		def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
		    true);
		if (def == NULL)
			return -1;
		target = (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 *)target));
		if (*where != target)
			*where = target;
	}
	return 0;
}
@


1.22
log
@ANSIfy and de-__P().
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.21 2003/07/21 15:34:36 skrll Exp $	*/
d5 2
@


1.21
log
@Support immediate binding on arm.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.20 2002/10/03 20:39:22 mycroft Exp $	*/
d11 1
a11 1
caddr_t _rtld_bind __P((const Obj_Entry *, Elf_Word));
d21 1
a21 3
_rtld_relocate_nonplt_self(dynp, relocbase)
	Elf_Dyn *dynp;
	Elf_Addr relocbase;
d69 1
a69 2
_rtld_relocate_nonplt_objects(obj)
	const Obj_Entry *obj;
d187 1
a187 2
_rtld_relocate_plt_lazy(obj)
	const Obj_Entry *obj;
d208 1
a208 3
_rtld_bind(obj, reloff)
	const Obj_Entry *obj;
	Elf_Word reloff;
@


1.20
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.19 2002/09/26 20:42:11 mycroft Exp $	*/
d235 27
@


1.19
log
@Remove the `self' args to _rtld_relocate_objects() and
_rtld_relocate_nonplt_objects().
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.18 2002/09/25 07:27:51 mycroft Exp $	*/
d195 1
a195 1
	if (!obj->isdynamic)
@


1.18
log
@Push the _rtld_bind() interface into MD code -- it's just a trivial wrapper
anyway.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.17 2002/09/15 01:08:04 thorpej Exp $	*/
d71 1
a71 1
_rtld_relocate_nonplt_objects(obj, self)
a72 1
	bool self;
a74 3

	if (self)
		return 0;
@


1.17
log
@No PC24 relocs will ever be unaligned.  Pointed out by mycroft.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.16 2002/09/15 00:52:08 thorpej Exp $	*/
d11 1
d215 2
a216 2
int
_rtld_relocate_plt_object(obj, rela, addrp)
d218 1
a218 2
	const Elf_Rela *rela;
	caddr_t *addrp;
d220 2
a221 1
	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
d226 1
a226 1
	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
d228 1
a228 1
	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
d230 1
a230 1
		return -1;
d238 1
a238 2
	*addrp = (caddr_t)new_value;
	return 0;
@


1.16
log
@It is possible for reloc entries to point to unaligned locations; handle
this.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.15 2002/09/13 05:45:46 mycroft Exp $	*/
a95 1
			Elf_Addr val;
d100 1
a100 6
			if (__predict_true(RELOC_ALIGNED_P(where)))
				val = *where;
			else
				val = load_ptr(where);

			addend = val;
d120 1
a120 5
			val = (val & 0xff000000) | (tmp & 0x00ffffff);
			if (__predict_true(RELOC_ALIGNED_P(where)))
				*where = val;
			else
				store_ptr(where, val);
d123 1
a123 1
			    obj->path, (void *)val, where, defobj->path));
@


1.15
log
@Use the addend on GLOB_DAT relocs (same as ABS32).
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.14 2002/09/12 22:56:29 mycroft Exp $	*/
d45 24
d96 1
d101 6
a106 1
			addend = *where;
d126 5
a130 1
			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
d133 1
a133 1
			    obj->path, (void *)*where, where, defobj->path));
d143 10
a152 1
			*where += (Elf_Addr)defobj->relocbase + def->st_value;
d155 1
a155 1
			    obj->path, (void *)*where, where, defobj->path));
d159 8
a166 1
			*where += (Elf_Addr)obj->relocbase;
d168 1
a168 1
			    (void *)*where));
d191 1
a191 1
			    (void *)rel->r_offset, (void *)*where,
@


1.14
log
@Nuke -DRTLD_RELOCATE_SELF and `dodebug' from orbit.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.13 2002/09/12 20:20:59 mycroft Exp $	*/
d105 1
d110 1
a110 11
			rdbg(("ABS32 %s in %s --> %p @@ %p in %s",
			    obj->strtab + obj->symtab[symnum].st_name,
			    obj->path, (void *)*where, where, defobj->path));
			break;

		case R_TYPE(GLOB_DAT):	/* word32 B + S */
			def = _rtld_find_symdef(symnum, obj, &defobj, false);
			if (def == NULL)
				return -1;
			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
			rdbg(("GLOB_DAT %s in %s --> %p @@ %p in %s",
@


1.13
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.12 2002/09/11 22:28:33 mycroft Exp $	*/
d46 1
a46 1
_rtld_relocate_nonplt_objects(obj, self, dodebug)
a48 1
	bool dodebug;
d97 1
a97 1
			rdbg(dodebug, ("PC24 %s in %s --> %p @@ %p in %s",
d109 1
a109 1
			rdbg(dodebug, ("ABS32 %s in %s --> %p @@ %p in %s",
d119 1
a119 1
			rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @@ %p in %s",
d126 1
a126 1
			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
d143 1
a143 1
			rdbg(dodebug, ("COPY (avoid in main)"));
d147 1
a147 1
			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
d162 1
a162 1
_rtld_relocate_plt_lazy(obj, dodebug)
a163 1
	bool dodebug;
d177 1
a177 2
		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
		    (void *)*where));
d184 1
a184 1
_rtld_relocate_plt_object(obj, rela, addrp, dodebug)
a187 1
	bool dodebug;
d201 1
a201 1
	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
@


1.12
log
@Do the _rtld_relocate_nonplt_self() thing here, and nuke the ld script.
@
text
@d1 1
a1 1
/*	$NetBSD: mdreloc.c,v 1.11 2002/09/11 14:19:30 junyoung Exp $	*/
d9 1
@


1.11
log
@Add $NetBSD$.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d9 2
d18 26
d52 3
d125 3
a127 14
		    {
			extern Elf_Addr	_GLOBAL_OFFSET_TABLE_[];
			extern Elf_Addr	_GOT_END_[];

			/* This is the ...iffy hueristic. */
			if (!self ||
			    (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
			    (caddr_t)where >= (caddr_t)_GOT_END_) {
				*where += (Elf_Addr)obj->relocbase;
				rdbg(dodebug, ("RELATIVE in %s --> %p",
				    obj->path, (void *)*where));
			} else
				rdbg(dodebug, ("RELATIVE in %s stays at %p",
				    obj->path, (void *)*where));
a128 1
		    }
@


1.10
log
@Nuke RTLD_RELOCATE_SELF on ARM.
@
text
@d1 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
@d92 14
a105 3
			*where += (Elf_Addr)obj->relocbase;
			rdbg(dodebug, ("RELATIVE in %s --> %p @@ %p", obj->path,
			    (void *)*where, where));
d107 1
@


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
@d103 1
a103 1
			if (!obj->mainprog) {
d134 1
a134 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;
d129 1
a129 1
	Obj_Entry *obj;
d153 1
a153 1
	Obj_Entry *obj;
@


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


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
@d57 2
a58 2
				    defobj->strtab + def->st_name, (long) tmp,
				    (long) tmp);
d64 2
a65 2
			    defobj->strtab + def->st_name, obj->path,
			    (void *)*where, where, defobj->path));
d76 2
a77 2
			    defobj->strtab + def->st_name, obj->path,
			    (void *)*where, where, defobj->path));
d86 2
a87 2
			    defobj->strtab + def->st_name, obj->path,
			    (void *)*where, 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
d46 1
a46 2
			def = _rtld_find_symdef(rel->r_info, obj, &defobj,
			    false);
d71 1
a71 2
			def = _rtld_find_symdef(rel->r_info, obj, &defobj,
			    false);
d81 1
a81 2
			def = _rtld_find_symdef(rel->r_info, obj, &defobj,
			    false);
a112 2
			def = _rtld_find_symdef(rel->r_info, obj, &defobj,
			    true);
d115 1
a115 2
			    (u_long)ELF_R_SYM(rel->r_info),
			    (u_long)ELF_R_TYPE(rel->r_info),
d117 1
a117 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 4
	Elf_Addr        *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
	const Elf_Sym   *def;
	const Obj_Entry *defobj;
	Elf_Addr         tmp;
d21 11
a31 4
	switch (ELF_R_TYPE(rela->r_info)) {

	case R_TYPE(NONE):
		break;
d34 2
a35 9
	case R_TYPE(PC24): {	/* word32 S - P + A */
		Elf32_Sword addend;

		/*
		 * Extract addend and sign-extend if needed.
		 */
		addend = *where;
		if (addend & 0x00800000)
			addend |= 0xff000000;
d37 29
a65 13
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
		if (def == NULL)
			return -1;
		tmp = (Elf_Addr)obj->relocbase + def->st_value
		    - (Elf_Addr)where + (addend << 2);
		if ((tmp & 0xfe000000) != 0xfe000000 &&
		    (tmp & 0xfe000000) != 0) {
			_rtld_error(
			"%s: R_ARM_PC24 relocation @@ %p to %s failed "
			"(displacement %ld (%#lx) out of range)",
			    obj->path, where, defobj->strtab + def->st_name,
			    (long) tmp, (long) tmp);
			return -1;
a66 7
		tmp >>= 2;
		*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
		rdbg(dodebug, ("PC24 %s in %s --> %p @@ %p in %s",
		    defobj->strtab + def->st_name, obj->path,
		    (void *)*where, where, defobj->path));
		break;
	}
d69 37
a105 35
	case R_TYPE(ABS32):	/* word32 B + S + A */
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
		if (def == NULL)
			return -1;
		*where += (Elf_Addr)defobj->relocbase + def->st_value;
		rdbg(dodebug, ("ABS32 %s in %s --> %p @@ %p in %s",
		    defobj->strtab + def->st_name, obj->path,
		    (void *)*where, where, defobj->path));
		break;

	case R_TYPE(GLOB_DAT):	/* word32 B + S */
		def = _rtld_find_symdef(rela->r_info, obj, &defobj, false);
		if (def == NULL)
			return -1;
		*where = (Elf_Addr)(defobj->relocbase + def->st_value);
		rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @@ %p in %s",
		    defobj->strtab + def->st_name, obj->path,
		    (void *)*where, where, defobj->path));
		break;

	case R_TYPE(RELATIVE):	/* word32 B + A */
		*where += (Elf_Addr)obj->relocbase;
		rdbg(dodebug, ("RELATIVE in %s --> %p @@ %p", obj->path,
		    (void *)*where, 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(
d107 18
a124 1
			    obj->path);
a126 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 109
@
