16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License (the "License").
66ff6d951SJohn Birrell  * You may not use this file except in compliance with the License.
76ff6d951SJohn Birrell  *
86ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
106ff6d951SJohn Birrell  * See the License for the specific language governing permissions
116ff6d951SJohn Birrell  * and limitations under the License.
126ff6d951SJohn Birrell  *
136ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
146ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
166ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
176ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
186ff6d951SJohn Birrell  *
196ff6d951SJohn Birrell  * CDDL HEADER END
206ff6d951SJohn Birrell  */
216ff6d951SJohn Birrell 
226ff6d951SJohn Birrell /*
232693feb4SJohn Birrell  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246ff6d951SJohn Birrell  * Use is subject to license terms.
25df7165b8SMark Johnston  * Copyright 2017-2018 Mark Johnston <markj@FreeBSD.org>
266ff6d951SJohn Birrell  */
276ff6d951SJohn Birrell 
280e15d9fbSMark Johnston #include <sys/param.h>
290e15d9fbSMark Johnston #include <sys/mman.h>
300e15d9fbSMark Johnston #include <sys/wait.h>
316ff6d951SJohn Birrell 
320e15d9fbSMark Johnston #include <assert.h>
336ff6d951SJohn Birrell #include <elf.h>
340e15d9fbSMark Johnston #include <fcntl.h>
350e15d9fbSMark Johnston #include <gelf.h>
366ff6d951SJohn Birrell #include <limits.h>
376ff6d951SJohn Birrell #include <stddef.h>
386ff6d951SJohn Birrell #include <stdio.h>
390e15d9fbSMark Johnston #include <stdlib.h>
400e15d9fbSMark Johnston #include <strings.h>
416ff6d951SJohn Birrell #include <errno.h>
420e15d9fbSMark Johnston #include <unistd.h>
430e15d9fbSMark Johnston 
440f2bd1e8SRui Paulo #include <libelf.h>
456ff6d951SJohn Birrell 
466ff6d951SJohn Birrell #include <dt_impl.h>
476ff6d951SJohn Birrell #include <dt_provider.h>
486ff6d951SJohn Birrell #include <dt_program.h>
496ff6d951SJohn Birrell #include <dt_string.h>
506ff6d951SJohn Birrell 
516ff6d951SJohn Birrell #define	ESHDR_NULL	0
526ff6d951SJohn Birrell #define	ESHDR_SHSTRTAB	1
536ff6d951SJohn Birrell #define	ESHDR_DOF	2
546ff6d951SJohn Birrell #define	ESHDR_STRTAB	3
556ff6d951SJohn Birrell #define	ESHDR_SYMTAB	4
566ff6d951SJohn Birrell #define	ESHDR_REL	5
576ff6d951SJohn Birrell #define	ESHDR_NUM	6
586ff6d951SJohn Birrell 
596ff6d951SJohn Birrell #define	PWRITE_SCN(index, data) \
606ff6d951SJohn Birrell 	(lseek64(fd, (off64_t)elf_file.shdr[(index)].sh_offset, SEEK_SET) != \
616ff6d951SJohn Birrell 	(off64_t)elf_file.shdr[(index)].sh_offset || \
626ff6d951SJohn Birrell 	dt_write(dtp, fd, (data), elf_file.shdr[(index)].sh_size) != \
636ff6d951SJohn Birrell 	elf_file.shdr[(index)].sh_size)
646ff6d951SJohn Birrell 
656ff6d951SJohn Birrell static const char DTRACE_SHSTRTAB32[] = "\0"
666ff6d951SJohn Birrell ".shstrtab\0"		/* 1 */
676ff6d951SJohn Birrell ".SUNW_dof\0"		/* 11 */
686ff6d951SJohn Birrell ".strtab\0"		/* 21 */
696ff6d951SJohn Birrell ".symtab\0"		/* 29 */
706ff6d951SJohn Birrell ".rel.SUNW_dof";	/* 37 */
716ff6d951SJohn Birrell 
726ff6d951SJohn Birrell static const char DTRACE_SHSTRTAB64[] = "\0"
736ff6d951SJohn Birrell ".shstrtab\0"		/* 1 */
746ff6d951SJohn Birrell ".SUNW_dof\0"		/* 11 */
756ff6d951SJohn Birrell ".strtab\0"		/* 21 */
766ff6d951SJohn Birrell ".symtab\0"		/* 29 */
776ff6d951SJohn Birrell ".rela.SUNW_dof";	/* 37 */
786ff6d951SJohn Birrell 
796ff6d951SJohn Birrell static const char DOFSTR[] = "__SUNW_dof";
806ff6d951SJohn Birrell static const char DOFLAZYSTR[] = "___SUNW_dof";
816ff6d951SJohn Birrell 
826ff6d951SJohn Birrell typedef struct dt_link_pair {
836ff6d951SJohn Birrell 	struct dt_link_pair *dlp_next;	/* next pair in linked list */
846ff6d951SJohn Birrell 	void *dlp_str;			/* buffer for string table */
856ff6d951SJohn Birrell 	void *dlp_sym;			/* buffer for symbol table */
866ff6d951SJohn Birrell } dt_link_pair_t;
876ff6d951SJohn Birrell 
886ff6d951SJohn Birrell typedef struct dof_elf32 {
896ff6d951SJohn Birrell 	uint32_t de_nrel;		/* relocation count */
906ff6d951SJohn Birrell 	Elf32_Rel *de_rel;		/* array of relocations for x86 */
916ff6d951SJohn Birrell 	uint32_t de_nsym;		/* symbol count */
926ff6d951SJohn Birrell 	Elf32_Sym *de_sym;		/* array of symbols */
936ff6d951SJohn Birrell 	uint32_t de_strlen;		/* size of of string table */
946ff6d951SJohn Birrell 	char *de_strtab;		/* string table */
956ff6d951SJohn Birrell 	uint32_t de_global;		/* index of the first global symbol */
966ff6d951SJohn Birrell } dof_elf32_t;
976ff6d951SJohn Birrell 
986ff6d951SJohn Birrell static int
996ff6d951SJohn Birrell prepare_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf32_t *dep)
1006ff6d951SJohn Birrell {
1016ff6d951SJohn Birrell 	dof_sec_t *dofs, *s;
1026ff6d951SJohn Birrell 	dof_relohdr_t *dofrh;
1036ff6d951SJohn Birrell 	dof_relodesc_t *dofr;
1046ff6d951SJohn Birrell 	char *strtab;
1056ff6d951SJohn Birrell 	int i, j, nrel;
1066ff6d951SJohn Birrell 	size_t strtabsz = 1;
1076ff6d951SJohn Birrell 	uint32_t count = 0;
1086ff6d951SJohn Birrell 	size_t base;
1096ff6d951SJohn Birrell 	Elf32_Sym *sym;
1106ff6d951SJohn Birrell 	Elf32_Rel *rel;
1116ff6d951SJohn Birrell 
1126ff6d951SJohn Birrell 	/*LINTED*/
1136ff6d951SJohn Birrell 	dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
1146ff6d951SJohn Birrell 
1156ff6d951SJohn Birrell 	/*
1166ff6d951SJohn Birrell 	 * First compute the size of the string table and the number of
1176ff6d951SJohn Birrell 	 * relocations present in the DOF.
1186ff6d951SJohn Birrell 	 */
1196ff6d951SJohn Birrell 	for (i = 0; i < dof->dofh_secnum; i++) {
1206ff6d951SJohn Birrell 		if (dofs[i].dofs_type != DOF_SECT_URELHDR)
1216ff6d951SJohn Birrell 			continue;
1226ff6d951SJohn Birrell 
1236ff6d951SJohn Birrell 		/*LINTED*/
1246ff6d951SJohn Birrell 		dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
1256ff6d951SJohn Birrell 
1266ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_strtab];
1276ff6d951SJohn Birrell 		strtab = (char *)dof + s->dofs_offset;
1286ff6d951SJohn Birrell 		assert(strtab[0] == '\0');
1296ff6d951SJohn Birrell 		strtabsz += s->dofs_size - 1;
1306ff6d951SJohn Birrell 
1316ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_relsec];
1326ff6d951SJohn Birrell 		/*LINTED*/
1336ff6d951SJohn Birrell 		dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
1346ff6d951SJohn Birrell 		count += s->dofs_size / s->dofs_entsize;
1356ff6d951SJohn Birrell 	}
1366ff6d951SJohn Birrell 
1376ff6d951SJohn Birrell 	dep->de_strlen = strtabsz;
1386ff6d951SJohn Birrell 	dep->de_nrel = count;
1396ff6d951SJohn Birrell 	dep->de_nsym = count + 1; /* the first symbol is always null */
1406ff6d951SJohn Birrell 
1416ff6d951SJohn Birrell 	if (dtp->dt_lazyload) {
1426ff6d951SJohn Birrell 		dep->de_strlen += sizeof (DOFLAZYSTR);
1436ff6d951SJohn Birrell 		dep->de_nsym++;
1446ff6d951SJohn Birrell 	} else {
1456ff6d951SJohn Birrell 		dep->de_strlen += sizeof (DOFSTR);
1466ff6d951SJohn Birrell 		dep->de_nsym++;
1476ff6d951SJohn Birrell 	}
1486ff6d951SJohn Birrell 
1496ff6d951SJohn Birrell 	if ((dep->de_rel = calloc(dep->de_nrel,
1506ff6d951SJohn Birrell 	    sizeof (dep->de_rel[0]))) == NULL) {
1516ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
1526ff6d951SJohn Birrell 	}
1536ff6d951SJohn Birrell 
1546ff6d951SJohn Birrell 	if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf32_Sym))) == NULL) {
1556ff6d951SJohn Birrell 		free(dep->de_rel);
1566ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
1576ff6d951SJohn Birrell 	}
1586ff6d951SJohn Birrell 
1596ff6d951SJohn Birrell 	if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
1606ff6d951SJohn Birrell 		free(dep->de_rel);
1616ff6d951SJohn Birrell 		free(dep->de_sym);
1626ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
1636ff6d951SJohn Birrell 	}
1646ff6d951SJohn Birrell 
1656ff6d951SJohn Birrell 	count = 0;
1666ff6d951SJohn Birrell 	strtabsz = 1;
1676ff6d951SJohn Birrell 	dep->de_strtab[0] = '\0';
1686ff6d951SJohn Birrell 	rel = dep->de_rel;
1696ff6d951SJohn Birrell 	sym = dep->de_sym;
1706ff6d951SJohn Birrell 	dep->de_global = 1;
1716ff6d951SJohn Birrell 
1726ff6d951SJohn Birrell 	/*
1736ff6d951SJohn Birrell 	 * The first symbol table entry must be zeroed and is always ignored.
1746ff6d951SJohn Birrell 	 */
1756ff6d951SJohn Birrell 	bzero(sym, sizeof (Elf32_Sym));
1766ff6d951SJohn Birrell 	sym++;
1776ff6d951SJohn Birrell 
1786ff6d951SJohn Birrell 	/*
1796ff6d951SJohn Birrell 	 * Take a second pass through the DOF sections filling in the
1806ff6d951SJohn Birrell 	 * memory we allocated.
1816ff6d951SJohn Birrell 	 */
1826ff6d951SJohn Birrell 	for (i = 0; i < dof->dofh_secnum; i++) {
1836ff6d951SJohn Birrell 		if (dofs[i].dofs_type != DOF_SECT_URELHDR)
1846ff6d951SJohn Birrell 			continue;
1856ff6d951SJohn Birrell 
1866ff6d951SJohn Birrell 		/*LINTED*/
1876ff6d951SJohn Birrell 		dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
1886ff6d951SJohn Birrell 
1896ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_strtab];
1906ff6d951SJohn Birrell 		strtab = (char *)dof + s->dofs_offset;
1916ff6d951SJohn Birrell 		bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
1926ff6d951SJohn Birrell 		base = strtabsz;
1936ff6d951SJohn Birrell 		strtabsz += s->dofs_size - 1;
1946ff6d951SJohn Birrell 
1956ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_relsec];
1966ff6d951SJohn Birrell 		/*LINTED*/
1976ff6d951SJohn Birrell 		dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
1986ff6d951SJohn Birrell 		nrel = s->dofs_size / s->dofs_entsize;
1996ff6d951SJohn Birrell 
2006ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_tgtsec];
2016ff6d951SJohn Birrell 
2026ff6d951SJohn Birrell 		for (j = 0; j < nrel; j++) {
203b78ee15eSRuslan Bukin #if defined(__aarch64__)
204a2ea7849SMark Johnston 			rel->r_offset = s->dofs_offset +
205a2ea7849SMark Johnston 			    dofr[j].dofr_offset;
206a2ea7849SMark Johnston 			rel->r_info = ELF32_R_INFO(count + dep->de_global,
207a2ea7849SMark Johnston 			    R_ARM_REL32);
208b78ee15eSRuslan Bukin #elif defined(__arm__)
2092693feb4SJohn Birrell /* XXX */
210732f2c69SEd Maste 			printf("%s:%s(%d): arm not implemented\n",
211732f2c69SEd Maste 			    __FUNCTION__, __FILE__, __LINE__);
2122693feb4SJohn Birrell #elif defined(__i386) || defined(__amd64)
2136ff6d951SJohn Birrell 			rel->r_offset = s->dofs_offset +
2146ff6d951SJohn Birrell 			    dofr[j].dofr_offset;
2156ff6d951SJohn Birrell 			rel->r_info = ELF32_R_INFO(count + dep->de_global,
216e801af6fSMark Johnston 			    R_386_PC32);
2172693feb4SJohn Birrell #elif defined(__mips__)
2182693feb4SJohn Birrell /* XXX */
219732f2c69SEd Maste 			printf("%s:%s(%d): MIPS not implemented\n",
220732f2c69SEd Maste 			    __FUNCTION__, __FILE__, __LINE__);
2212693feb4SJohn Birrell #elif defined(__powerpc__)
22230b318b9SJustin Hibbits 			/*
22330b318b9SJustin Hibbits 			 * Add 4 bytes to hit the low half of this 64-bit
22430b318b9SJustin Hibbits 			 * big-endian address.
22530b318b9SJustin Hibbits 			 */
22630b318b9SJustin Hibbits 			rel->r_offset = s->dofs_offset +
22730b318b9SJustin Hibbits 			    dofr[j].dofr_offset + 4;
22830b318b9SJustin Hibbits 			rel->r_info = ELF32_R_INFO(count + dep->de_global,
22930b318b9SJustin Hibbits 			    R_PPC_REL32);
230ca20f8ecSRuslan Bukin #elif defined(__riscv)
231fed1ca4bSRuslan Bukin /* XXX */
232732f2c69SEd Maste 			printf("%s:%s(%d): RISC-V not implemented\n",
233732f2c69SEd Maste 			    __FUNCTION__, __FILE__, __LINE__);
2346ff6d951SJohn Birrell #else
2356ff6d951SJohn Birrell #error unknown ISA
2366ff6d951SJohn Birrell #endif
2376ff6d951SJohn Birrell 
2386ff6d951SJohn Birrell 			sym->st_name = base + dofr[j].dofr_name - 1;
2396ff6d951SJohn Birrell 			sym->st_value = 0;
2406ff6d951SJohn Birrell 			sym->st_size = 0;
2416ff6d951SJohn Birrell 			sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC);
242df7165b8SMark Johnston 			sym->st_other = ELF32_ST_VISIBILITY(STV_HIDDEN);
2436ff6d951SJohn Birrell 			sym->st_shndx = SHN_UNDEF;
2446ff6d951SJohn Birrell 
2456ff6d951SJohn Birrell 			rel++;
2466ff6d951SJohn Birrell 			sym++;
2476ff6d951SJohn Birrell 			count++;
2486ff6d951SJohn Birrell 		}
2496ff6d951SJohn Birrell 	}
2506ff6d951SJohn Birrell 
2516ff6d951SJohn Birrell 	/*
2526ff6d951SJohn Birrell 	 * Add a symbol for the DOF itself. We use a different symbol for
2536ff6d951SJohn Birrell 	 * lazily and actively loaded DOF to make them easy to distinguish.
2546ff6d951SJohn Birrell 	 */
2556ff6d951SJohn Birrell 	sym->st_name = strtabsz;
2566ff6d951SJohn Birrell 	sym->st_value = 0;
2576ff6d951SJohn Birrell 	sym->st_size = dof->dofh_filesz;
2586ff6d951SJohn Birrell 	sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT);
25903a5f9f0SMark Johnston 	sym->st_other = ELF32_ST_VISIBILITY(STV_HIDDEN);
2606ff6d951SJohn Birrell 	sym->st_shndx = ESHDR_DOF;
2616ff6d951SJohn Birrell 	sym++;
2626ff6d951SJohn Birrell 
2636ff6d951SJohn Birrell 	if (dtp->dt_lazyload) {
2646ff6d951SJohn Birrell 		bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
2656ff6d951SJohn Birrell 		    sizeof (DOFLAZYSTR));
2666ff6d951SJohn Birrell 		strtabsz += sizeof (DOFLAZYSTR);
2676ff6d951SJohn Birrell 	} else {
2686ff6d951SJohn Birrell 		bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
2696ff6d951SJohn Birrell 		strtabsz += sizeof (DOFSTR);
2706ff6d951SJohn Birrell 	}
2716ff6d951SJohn Birrell 
2726ff6d951SJohn Birrell 	assert(count == dep->de_nrel);
2736ff6d951SJohn Birrell 	assert(strtabsz == dep->de_strlen);
2746ff6d951SJohn Birrell 
2756ff6d951SJohn Birrell 	return (0);
2766ff6d951SJohn Birrell }
2776ff6d951SJohn Birrell 
2786ff6d951SJohn Birrell 
2796ff6d951SJohn Birrell typedef struct dof_elf64 {
2806ff6d951SJohn Birrell 	uint32_t de_nrel;
2816ff6d951SJohn Birrell 	Elf64_Rela *de_rel;
2826ff6d951SJohn Birrell 	uint32_t de_nsym;
2836ff6d951SJohn Birrell 	Elf64_Sym *de_sym;
2846ff6d951SJohn Birrell 
2856ff6d951SJohn Birrell 	uint32_t de_strlen;
2866ff6d951SJohn Birrell 	char *de_strtab;
2876ff6d951SJohn Birrell 
2886ff6d951SJohn Birrell 	uint32_t de_global;
2896ff6d951SJohn Birrell } dof_elf64_t;
2906ff6d951SJohn Birrell 
2916ff6d951SJohn Birrell static int
2926ff6d951SJohn Birrell prepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep)
2936ff6d951SJohn Birrell {
2946ff6d951SJohn Birrell 	dof_sec_t *dofs, *s;
2956ff6d951SJohn Birrell 	dof_relohdr_t *dofrh;
2966ff6d951SJohn Birrell 	dof_relodesc_t *dofr;
2976ff6d951SJohn Birrell 	char *strtab;
2986ff6d951SJohn Birrell 	int i, j, nrel;
2996ff6d951SJohn Birrell 	size_t strtabsz = 1;
3008caf8a8dSMark Johnston 	uint64_t count = 0;
3016ff6d951SJohn Birrell 	size_t base;
3026ff6d951SJohn Birrell 	Elf64_Sym *sym;
3036ff6d951SJohn Birrell 	Elf64_Rela *rel;
3046ff6d951SJohn Birrell 
3056ff6d951SJohn Birrell 	/*LINTED*/
3066ff6d951SJohn Birrell 	dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
3076ff6d951SJohn Birrell 
3086ff6d951SJohn Birrell 	/*
3096ff6d951SJohn Birrell 	 * First compute the size of the string table and the number of
3106ff6d951SJohn Birrell 	 * relocations present in the DOF.
3116ff6d951SJohn Birrell 	 */
3126ff6d951SJohn Birrell 	for (i = 0; i < dof->dofh_secnum; i++) {
3136ff6d951SJohn Birrell 		if (dofs[i].dofs_type != DOF_SECT_URELHDR)
3146ff6d951SJohn Birrell 			continue;
3156ff6d951SJohn Birrell 
3166ff6d951SJohn Birrell 		/*LINTED*/
3176ff6d951SJohn Birrell 		dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
3186ff6d951SJohn Birrell 
3196ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_strtab];
3206ff6d951SJohn Birrell 		strtab = (char *)dof + s->dofs_offset;
3216ff6d951SJohn Birrell 		assert(strtab[0] == '\0');
3226ff6d951SJohn Birrell 		strtabsz += s->dofs_size - 1;
3236ff6d951SJohn Birrell 
3246ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_relsec];
3256ff6d951SJohn Birrell 		/*LINTED*/
3266ff6d951SJohn Birrell 		dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
3276ff6d951SJohn Birrell 		count += s->dofs_size / s->dofs_entsize;
3286ff6d951SJohn Birrell 	}
3296ff6d951SJohn Birrell 
3306ff6d951SJohn Birrell 	dep->de_strlen = strtabsz;
3316ff6d951SJohn Birrell 	dep->de_nrel = count;
3326ff6d951SJohn Birrell 	dep->de_nsym = count + 1; /* the first symbol is always null */
3336ff6d951SJohn Birrell 
3346ff6d951SJohn Birrell 	if (dtp->dt_lazyload) {
3356ff6d951SJohn Birrell 		dep->de_strlen += sizeof (DOFLAZYSTR);
3366ff6d951SJohn Birrell 		dep->de_nsym++;
3376ff6d951SJohn Birrell 	} else {
3386ff6d951SJohn Birrell 		dep->de_strlen += sizeof (DOFSTR);
3396ff6d951SJohn Birrell 		dep->de_nsym++;
3406ff6d951SJohn Birrell 	}
3416ff6d951SJohn Birrell 
3426ff6d951SJohn Birrell 	if ((dep->de_rel = calloc(dep->de_nrel,
3436ff6d951SJohn Birrell 	    sizeof (dep->de_rel[0]))) == NULL) {
3446ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
3456ff6d951SJohn Birrell 	}
3466ff6d951SJohn Birrell 
3476ff6d951SJohn Birrell 	if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) {
3486ff6d951SJohn Birrell 		free(dep->de_rel);
3496ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
3506ff6d951SJohn Birrell 	}
3516ff6d951SJohn Birrell 
3526ff6d951SJohn Birrell 	if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
3536ff6d951SJohn Birrell 		free(dep->de_rel);
3546ff6d951SJohn Birrell 		free(dep->de_sym);
3556ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EDT_NOMEM));
3566ff6d951SJohn Birrell 	}
3576ff6d951SJohn Birrell 
3586ff6d951SJohn Birrell 	count = 0;
3596ff6d951SJohn Birrell 	strtabsz = 1;
3606ff6d951SJohn Birrell 	dep->de_strtab[0] = '\0';
3616ff6d951SJohn Birrell 	rel = dep->de_rel;
3626ff6d951SJohn Birrell 	sym = dep->de_sym;
3636ff6d951SJohn Birrell 	dep->de_global = 1;
3646ff6d951SJohn Birrell 
3656ff6d951SJohn Birrell 	/*
3666ff6d951SJohn Birrell 	 * The first symbol table entry must be zeroed and is always ignored.
3676ff6d951SJohn Birrell 	 */
3686ff6d951SJohn Birrell 	bzero(sym, sizeof (Elf64_Sym));
3696ff6d951SJohn Birrell 	sym++;
3706ff6d951SJohn Birrell 
3716ff6d951SJohn Birrell 	/*
3726ff6d951SJohn Birrell 	 * Take a second pass through the DOF sections filling in the
3736ff6d951SJohn Birrell 	 * memory we allocated.
3746ff6d951SJohn Birrell 	 */
3756ff6d951SJohn Birrell 	for (i = 0; i < dof->dofh_secnum; i++) {
3766ff6d951SJohn Birrell 		if (dofs[i].dofs_type != DOF_SECT_URELHDR)
3776ff6d951SJohn Birrell 			continue;
3786ff6d951SJohn Birrell 
3796ff6d951SJohn Birrell 		/*LINTED*/
3806ff6d951SJohn Birrell 		dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
3816ff6d951SJohn Birrell 
3826ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_strtab];
3836ff6d951SJohn Birrell 		strtab = (char *)dof + s->dofs_offset;
3846ff6d951SJohn Birrell 		bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
3856ff6d951SJohn Birrell 		base = strtabsz;
3866ff6d951SJohn Birrell 		strtabsz += s->dofs_size - 1;
3876ff6d951SJohn Birrell 
3886ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_relsec];
3896ff6d951SJohn Birrell 		/*LINTED*/
3906ff6d951SJohn Birrell 		dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
3916ff6d951SJohn Birrell 		nrel = s->dofs_size / s->dofs_entsize;
3926ff6d951SJohn Birrell 
3936ff6d951SJohn Birrell 		s = &dofs[dofrh->dofr_tgtsec];
3946ff6d951SJohn Birrell 
3956ff6d951SJohn Birrell 		for (j = 0; j < nrel; j++) {
396b78ee15eSRuslan Bukin #if defined(__aarch64__)
397a2ea7849SMark Johnston 			rel->r_offset = s->dofs_offset +
398a2ea7849SMark Johnston 			    dofr[j].dofr_offset;
399a2ea7849SMark Johnston 			rel->r_info = ELF64_R_INFO(count + dep->de_global,
400a2ea7849SMark Johnston 			    R_AARCH64_PREL64);
401b78ee15eSRuslan Bukin #elif defined(__arm__)
4022693feb4SJohn Birrell /* XXX */
4032693feb4SJohn Birrell #elif defined(__mips__)
4042693feb4SJohn Birrell /* XXX */
4052693feb4SJohn Birrell #elif defined(__powerpc__)
40630b318b9SJustin Hibbits 			rel->r_offset = s->dofs_offset +
40730b318b9SJustin Hibbits 			    dofr[j].dofr_offset;
40830b318b9SJustin Hibbits 			rel->r_info = ELF64_R_INFO(count + dep->de_global,
40930b318b9SJustin Hibbits 			    R_PPC64_REL64);
410ca20f8ecSRuslan Bukin #elif defined(__riscv)
411fed1ca4bSRuslan Bukin /* XXX */
4122693feb4SJohn Birrell #elif defined(__i386) || defined(__amd64)
4136ff6d951SJohn Birrell 			rel->r_offset = s->dofs_offset +
4146ff6d951SJohn Birrell 			    dofr[j].dofr_offset;
4156ff6d951SJohn Birrell 			rel->r_info = ELF64_R_INFO(count + dep->de_global,
416e801af6fSMark Johnston 			    R_X86_64_PC64);
4176ff6d951SJohn Birrell #else
4186ff6d951SJohn Birrell #error unknown ISA
4196ff6d951SJohn Birrell #endif
4206ff6d951SJohn Birrell 
4216ff6d951SJohn Birrell 			sym->st_name = base + dofr[j].dofr_name - 1;
4226ff6d951SJohn Birrell 			sym->st_value = 0;
4236ff6d951SJohn Birrell 			sym->st_size = 0;
4246ff6d951SJohn Birrell 			sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
425df7165b8SMark Johnston 			sym->st_other = ELF64_ST_VISIBILITY(STV_HIDDEN);
4266ff6d951SJohn Birrell 			sym->st_shndx = SHN_UNDEF;
4276ff6d951SJohn Birrell 
4286ff6d951SJohn Birrell 			rel++;
4296ff6d951SJohn Birrell 			sym++;
4306ff6d951SJohn Birrell 			count++;
4316ff6d951SJohn Birrell 		}
4326ff6d951SJohn Birrell 	}
4336ff6d951SJohn Birrell 
4346ff6d951SJohn Birrell 	/*
4356ff6d951SJohn Birrell 	 * Add a symbol for the DOF itself. We use a different symbol for
4366ff6d951SJohn Birrell 	 * lazily and actively loaded DOF to make them easy to distinguish.
4376ff6d951SJohn Birrell 	 */
4386ff6d951SJohn Birrell 	sym->st_name = strtabsz;
4396ff6d951SJohn Birrell 	sym->st_value = 0;
4406ff6d951SJohn Birrell 	sym->st_size = dof->dofh_filesz;
4416ff6d951SJohn Birrell 	sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
44203a5f9f0SMark Johnston 	sym->st_other = ELF64_ST_VISIBILITY(STV_HIDDEN);
4436ff6d951SJohn Birrell 	sym->st_shndx = ESHDR_DOF;
4446ff6d951SJohn Birrell 	sym++;
4456ff6d951SJohn Birrell 
4466ff6d951SJohn Birrell 	if (dtp->dt_lazyload) {
4476ff6d951SJohn Birrell 		bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
4486ff6d951SJohn Birrell 		    sizeof (DOFLAZYSTR));
4496ff6d951SJohn Birrell 		strtabsz += sizeof (DOFLAZYSTR);
4506ff6d951SJohn Birrell 	} else {
4516ff6d951SJohn Birrell 		bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
4526ff6d951SJohn Birrell 		strtabsz += sizeof (DOFSTR);
4536ff6d951SJohn Birrell 	}
4546ff6d951SJohn Birrell 
4556ff6d951SJohn Birrell 	assert(count == dep->de_nrel);
4566ff6d951SJohn Birrell 	assert(strtabsz == dep->de_strlen);
4576ff6d951SJohn Birrell 
4586ff6d951SJohn Birrell 	return (0);
4596ff6d951SJohn Birrell }
4606ff6d951SJohn Birrell 
4616ff6d951SJohn Birrell /*
4626ff6d951SJohn Birrell  * Write out an ELF32 file prologue consisting of a header, section headers,
4636ff6d951SJohn Birrell  * and a section header string table.  The DOF data will follow this prologue
4646ff6d951SJohn Birrell  * and complete the contents of the given ELF file.
4656ff6d951SJohn Birrell  */
4666ff6d951SJohn Birrell static int
4676ff6d951SJohn Birrell dump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
4686ff6d951SJohn Birrell {
4696ff6d951SJohn Birrell 	struct {
4706ff6d951SJohn Birrell 		Elf32_Ehdr ehdr;
4716ff6d951SJohn Birrell 		Elf32_Shdr shdr[ESHDR_NUM];
4726ff6d951SJohn Birrell 	} elf_file;
4736ff6d951SJohn Birrell 
4746ff6d951SJohn Birrell 	Elf32_Shdr *shp;
4756ff6d951SJohn Birrell 	Elf32_Off off;
4766ff6d951SJohn Birrell 	dof_elf32_t de;
4776ff6d951SJohn Birrell 	int ret = 0;
4786ff6d951SJohn Birrell 	uint_t nshdr;
4796ff6d951SJohn Birrell 
4806ff6d951SJohn Birrell 	if (prepare_elf32(dtp, dof, &de) != 0)
4816ff6d951SJohn Birrell 		return (-1); /* errno is set for us */
4826ff6d951SJohn Birrell 
4836ff6d951SJohn Birrell 	/*
4846ff6d951SJohn Birrell 	 * If there are no relocations, we only need enough sections for
4856ff6d951SJohn Birrell 	 * the shstrtab and the DOF.
4866ff6d951SJohn Birrell 	 */
4876ff6d951SJohn Birrell 	nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
4886ff6d951SJohn Birrell 
4896ff6d951SJohn Birrell 	bzero(&elf_file, sizeof (elf_file));
4906ff6d951SJohn Birrell 
4916ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
4926ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
4936ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
4946ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
4956ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
4966ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS32;
4972693feb4SJohn Birrell #if BYTE_ORDER == _BIG_ENDIAN
4986ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
4992693feb4SJohn Birrell #else
5006ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
5016ff6d951SJohn Birrell #endif
5022693feb4SJohn Birrell 	elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
5036ff6d951SJohn Birrell 	elf_file.ehdr.e_type = ET_REL;
5042693feb4SJohn Birrell #if defined(__arm__)
5052693feb4SJohn Birrell 	elf_file.ehdr.e_machine = EM_ARM;
5062693feb4SJohn Birrell #elif defined(__mips__)
5072693feb4SJohn Birrell 	elf_file.ehdr.e_machine = EM_MIPS;
5082693feb4SJohn Birrell #elif defined(__powerpc__)
5092693feb4SJohn Birrell 	elf_file.ehdr.e_machine = EM_PPC;
5106ff6d951SJohn Birrell #elif defined(__i386) || defined(__amd64)
5116ff6d951SJohn Birrell 	elf_file.ehdr.e_machine = EM_386;
512a2ea7849SMark Johnston #elif defined(__aarch64__)
513a2ea7849SMark Johnston 	elf_file.ehdr.e_machine = EM_AARCH64;
5146ff6d951SJohn Birrell #endif
5156ff6d951SJohn Birrell 	elf_file.ehdr.e_version = EV_CURRENT;
5166ff6d951SJohn Birrell 	elf_file.ehdr.e_shoff = sizeof (Elf32_Ehdr);
5176ff6d951SJohn Birrell 	elf_file.ehdr.e_ehsize = sizeof (Elf32_Ehdr);
5186ff6d951SJohn Birrell 	elf_file.ehdr.e_phentsize = sizeof (Elf32_Phdr);
5196ff6d951SJohn Birrell 	elf_file.ehdr.e_shentsize = sizeof (Elf32_Shdr);
5206ff6d951SJohn Birrell 	elf_file.ehdr.e_shnum = nshdr;
5216ff6d951SJohn Birrell 	elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
5226ff6d951SJohn Birrell 	off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr);
5236ff6d951SJohn Birrell 
5246ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_SHSTRTAB];
5256ff6d951SJohn Birrell 	shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */
5266ff6d951SJohn Birrell 	shp->sh_type = SHT_STRTAB;
5276ff6d951SJohn Birrell 	shp->sh_offset = off;
5286ff6d951SJohn Birrell 	shp->sh_size = sizeof (DTRACE_SHSTRTAB32);
5296ff6d951SJohn Birrell 	shp->sh_addralign = sizeof (char);
5300e15d9fbSMark Johnston 	off = roundup2(shp->sh_offset + shp->sh_size, 8);
5316ff6d951SJohn Birrell 
5326ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_DOF];
5336ff6d951SJohn Birrell 	shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */
5346ff6d951SJohn Birrell 	shp->sh_flags = SHF_ALLOC;
5356ff6d951SJohn Birrell 	shp->sh_type = SHT_SUNW_dof;
5366ff6d951SJohn Birrell 	shp->sh_offset = off;
5376ff6d951SJohn Birrell 	shp->sh_size = dof->dofh_filesz;
5386ff6d951SJohn Birrell 	shp->sh_addralign = 8;
5396ff6d951SJohn Birrell 	off = shp->sh_offset + shp->sh_size;
5406ff6d951SJohn Birrell 
5416ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_STRTAB];
5426ff6d951SJohn Birrell 	shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */
5436ff6d951SJohn Birrell 	shp->sh_flags = SHF_ALLOC;
5446ff6d951SJohn Birrell 	shp->sh_type = SHT_STRTAB;
5456ff6d951SJohn Birrell 	shp->sh_offset = off;
5466ff6d951SJohn Birrell 	shp->sh_size = de.de_strlen;
5476ff6d951SJohn Birrell 	shp->sh_addralign = sizeof (char);
5480e15d9fbSMark Johnston 	off = roundup2(shp->sh_offset + shp->sh_size, 4);
5496ff6d951SJohn Birrell 
5506ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_SYMTAB];
5516ff6d951SJohn Birrell 	shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */
5526ff6d951SJohn Birrell 	shp->sh_flags = SHF_ALLOC;
5536ff6d951SJohn Birrell 	shp->sh_type = SHT_SYMTAB;
5546ff6d951SJohn Birrell 	shp->sh_entsize = sizeof (Elf32_Sym);
5556ff6d951SJohn Birrell 	shp->sh_link = ESHDR_STRTAB;
5566ff6d951SJohn Birrell 	shp->sh_offset = off;
5576ff6d951SJohn Birrell 	shp->sh_info = de.de_global;
5586ff6d951SJohn Birrell 	shp->sh_size = de.de_nsym * sizeof (Elf32_Sym);
5596ff6d951SJohn Birrell 	shp->sh_addralign = 4;
5600e15d9fbSMark Johnston 	off = roundup2(shp->sh_offset + shp->sh_size, 4);
5616ff6d951SJohn Birrell 
5626ff6d951SJohn Birrell 	if (de.de_nrel == 0) {
5636ff6d951SJohn Birrell 		if (dt_write(dtp, fd, &elf_file,
5646ff6d951SJohn Birrell 		    sizeof (elf_file)) != sizeof (elf_file) ||
5656ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
5666ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
5676ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
5686ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_DOF, dof)) {
5696ff6d951SJohn Birrell 			ret = dt_set_errno(dtp, errno);
5706ff6d951SJohn Birrell 		}
5716ff6d951SJohn Birrell 	} else {
5726ff6d951SJohn Birrell 		shp = &elf_file.shdr[ESHDR_REL];
5736ff6d951SJohn Birrell 		shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */
5746ff6d951SJohn Birrell 		shp->sh_flags = SHF_ALLOC;
5756ff6d951SJohn Birrell 		shp->sh_type = SHT_REL;
5766ff6d951SJohn Birrell 		shp->sh_entsize = sizeof (de.de_rel[0]);
5776ff6d951SJohn Birrell 		shp->sh_link = ESHDR_SYMTAB;
5786ff6d951SJohn Birrell 		shp->sh_info = ESHDR_DOF;
5796ff6d951SJohn Birrell 		shp->sh_offset = off;
5806ff6d951SJohn Birrell 		shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
5816ff6d951SJohn Birrell 		shp->sh_addralign = 4;
5826ff6d951SJohn Birrell 
5836ff6d951SJohn Birrell 		if (dt_write(dtp, fd, &elf_file,
5846ff6d951SJohn Birrell 		    sizeof (elf_file)) != sizeof (elf_file) ||
5856ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
5866ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
5876ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
5886ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_REL, de.de_rel) ||
5896ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_DOF, dof)) {
5906ff6d951SJohn Birrell 			ret = dt_set_errno(dtp, errno);
5916ff6d951SJohn Birrell 		}
5926ff6d951SJohn Birrell 	}
5936ff6d951SJohn Birrell 
5946ff6d951SJohn Birrell 	free(de.de_strtab);
5956ff6d951SJohn Birrell 	free(de.de_sym);
5966ff6d951SJohn Birrell 	free(de.de_rel);
5976ff6d951SJohn Birrell 
5986ff6d951SJohn Birrell 	return (ret);
5996ff6d951SJohn Birrell }
6006ff6d951SJohn Birrell 
6016ff6d951SJohn Birrell /*
6026ff6d951SJohn Birrell  * Write out an ELF64 file prologue consisting of a header, section headers,
6036ff6d951SJohn Birrell  * and a section header string table.  The DOF data will follow this prologue
6046ff6d951SJohn Birrell  * and complete the contents of the given ELF file.
6056ff6d951SJohn Birrell  */
6066ff6d951SJohn Birrell static int
6076ff6d951SJohn Birrell dump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
6086ff6d951SJohn Birrell {
6096ff6d951SJohn Birrell 	struct {
6106ff6d951SJohn Birrell 		Elf64_Ehdr ehdr;
6116ff6d951SJohn Birrell 		Elf64_Shdr shdr[ESHDR_NUM];
6126ff6d951SJohn Birrell 	} elf_file;
6136ff6d951SJohn Birrell 
6146ff6d951SJohn Birrell 	Elf64_Shdr *shp;
6156ff6d951SJohn Birrell 	Elf64_Off off;
6166ff6d951SJohn Birrell 	dof_elf64_t de;
6176ff6d951SJohn Birrell 	int ret = 0;
6186ff6d951SJohn Birrell 	uint_t nshdr;
6196ff6d951SJohn Birrell 
6206ff6d951SJohn Birrell 	if (prepare_elf64(dtp, dof, &de) != 0)
6216ff6d951SJohn Birrell 		return (-1); /* errno is set for us */
6226ff6d951SJohn Birrell 
6236ff6d951SJohn Birrell 	/*
6246ff6d951SJohn Birrell 	 * If there are no relocations, we only need enough sections for
6256ff6d951SJohn Birrell 	 * the shstrtab and the DOF.
6266ff6d951SJohn Birrell 	 */
6276ff6d951SJohn Birrell 	nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
6286ff6d951SJohn Birrell 
6296ff6d951SJohn Birrell 	bzero(&elf_file, sizeof (elf_file));
6306ff6d951SJohn Birrell 
6316ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
6326ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
6336ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
6346ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
6356ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
6366ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS64;
6372693feb4SJohn Birrell #if BYTE_ORDER == _BIG_ENDIAN
6386ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
6392693feb4SJohn Birrell #else
6406ff6d951SJohn Birrell 	elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
6416ff6d951SJohn Birrell #endif
6422693feb4SJohn Birrell 	elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
6436ff6d951SJohn Birrell 	elf_file.ehdr.e_type = ET_REL;
6442693feb4SJohn Birrell #if defined(__arm__)
6452693feb4SJohn Birrell 	elf_file.ehdr.e_machine = EM_ARM;
6462693feb4SJohn Birrell #elif defined(__mips__)
6472693feb4SJohn Birrell 	elf_file.ehdr.e_machine = EM_MIPS;
648b08e2b04SJustin Hibbits #elif defined(__powerpc64__)
6497283901aSBrandon Bergren #if defined(_CALL_ELF) && _CALL_ELF == 2
6507283901aSBrandon Bergren 	elf_file.ehdr.e_flags = 2;
6517283901aSBrandon Bergren #endif
652b08e2b04SJustin Hibbits 	elf_file.ehdr.e_machine = EM_PPC64;
6536ff6d951SJohn Birrell #elif defined(__i386) || defined(__amd64)
6546ff6d951SJohn Birrell 	elf_file.ehdr.e_machine = EM_AMD64;
655a2ea7849SMark Johnston #elif defined(__aarch64__)
656a2ea7849SMark Johnston 	elf_file.ehdr.e_machine = EM_AARCH64;
6576ff6d951SJohn Birrell #endif
6586ff6d951SJohn Birrell 	elf_file.ehdr.e_version = EV_CURRENT;
6596ff6d951SJohn Birrell 	elf_file.ehdr.e_shoff = sizeof (Elf64_Ehdr);
6606ff6d951SJohn Birrell 	elf_file.ehdr.e_ehsize = sizeof (Elf64_Ehdr);
6616ff6d951SJohn Birrell 	elf_file.ehdr.e_phentsize = sizeof (Elf64_Phdr);
6626ff6d951SJohn Birrell 	elf_file.ehdr.e_shentsize = sizeof (Elf64_Shdr);
6636ff6d951SJohn Birrell 	elf_file.ehdr.e_shnum = nshdr;
6646ff6d951SJohn Birrell 	elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
6656ff6d951SJohn Birrell 	off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr);
6666ff6d951SJohn Birrell 
6676ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_SHSTRTAB];
6686ff6d951SJohn Birrell 	shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */
6696ff6d951SJohn Birrell 	shp->sh_type = SHT_STRTAB;
6706ff6d951SJohn Birrell 	shp->sh_offset = off;
6716ff6d951SJohn Birrell 	shp->sh_size = sizeof (DTRACE_SHSTRTAB64);
6726ff6d951SJohn Birrell 	shp->sh_addralign = sizeof (char);
6730e15d9fbSMark Johnston 	off = roundup2(shp->sh_offset + shp->sh_size, 8);
6746ff6d951SJohn Birrell 
6756ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_DOF];
6766ff6d951SJohn Birrell 	shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */
6776ff6d951SJohn Birrell 	shp->sh_flags = SHF_ALLOC;
6786ff6d951SJohn Birrell 	shp->sh_type = SHT_SUNW_dof;
6796ff6d951SJohn Birrell 	shp->sh_offset = off;
6806ff6d951SJohn Birrell 	shp->sh_size = dof->dofh_filesz;
6816ff6d951SJohn Birrell 	shp->sh_addralign = 8;
6826ff6d951SJohn Birrell 	off = shp->sh_offset + shp->sh_size;
6836ff6d951SJohn Birrell 
6846ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_STRTAB];
6856ff6d951SJohn Birrell 	shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */
6866ff6d951SJohn Birrell 	shp->sh_flags = SHF_ALLOC;
6876ff6d951SJohn Birrell 	shp->sh_type = SHT_STRTAB;
6886ff6d951SJohn Birrell 	shp->sh_offset = off;
6896ff6d951SJohn Birrell 	shp->sh_size = de.de_strlen;
6906ff6d951SJohn Birrell 	shp->sh_addralign = sizeof (char);
6910e15d9fbSMark Johnston 	off = roundup2(shp->sh_offset + shp->sh_size, 8);
6926ff6d951SJohn Birrell 
6936ff6d951SJohn Birrell 	shp = &elf_file.shdr[ESHDR_SYMTAB];
6946ff6d951SJohn Birrell 	shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */
6956ff6d951SJohn Birrell 	shp->sh_flags = SHF_ALLOC;
6966ff6d951SJohn Birrell 	shp->sh_type = SHT_SYMTAB;
6976ff6d951SJohn Birrell 	shp->sh_entsize = sizeof (Elf64_Sym);
6986ff6d951SJohn Birrell 	shp->sh_link = ESHDR_STRTAB;
6996ff6d951SJohn Birrell 	shp->sh_offset = off;
7006ff6d951SJohn Birrell 	shp->sh_info = de.de_global;
7016ff6d951SJohn Birrell 	shp->sh_size = de.de_nsym * sizeof (Elf64_Sym);
7026ff6d951SJohn Birrell 	shp->sh_addralign = 8;
7030e15d9fbSMark Johnston 	off = roundup2(shp->sh_offset + shp->sh_size, 8);
7046ff6d951SJohn Birrell 
7056ff6d951SJohn Birrell 	if (de.de_nrel == 0) {
7066ff6d951SJohn Birrell 		if (dt_write(dtp, fd, &elf_file,
7076ff6d951SJohn Birrell 		    sizeof (elf_file)) != sizeof (elf_file) ||
7086ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
7096ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
7106ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
7116ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_DOF, dof)) {
7126ff6d951SJohn Birrell 			ret = dt_set_errno(dtp, errno);
7136ff6d951SJohn Birrell 		}
7146ff6d951SJohn Birrell 	} else {
7156ff6d951SJohn Birrell 		shp = &elf_file.shdr[ESHDR_REL];
7166ff6d951SJohn Birrell 		shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */
7176ff6d951SJohn Birrell 		shp->sh_flags = SHF_ALLOC;
7186ff6d951SJohn Birrell 		shp->sh_type = SHT_RELA;
7196ff6d951SJohn Birrell 		shp->sh_entsize = sizeof (de.de_rel[0]);
7206ff6d951SJohn Birrell 		shp->sh_link = ESHDR_SYMTAB;
7216ff6d951SJohn Birrell 		shp->sh_info = ESHDR_DOF;
7226ff6d951SJohn Birrell 		shp->sh_offset = off;
7236ff6d951SJohn Birrell 		shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
7246ff6d951SJohn Birrell 		shp->sh_addralign = 8;
7256ff6d951SJohn Birrell 
7266ff6d951SJohn Birrell 		if (dt_write(dtp, fd, &elf_file,
7276ff6d951SJohn Birrell 		    sizeof (elf_file)) != sizeof (elf_file) ||
7286ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
7296ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
7306ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
7316ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_REL, de.de_rel) ||
7326ff6d951SJohn Birrell 		    PWRITE_SCN(ESHDR_DOF, dof)) {
7336ff6d951SJohn Birrell 			ret = dt_set_errno(dtp, errno);
7346ff6d951SJohn Birrell 		}
7356ff6d951SJohn Birrell 	}
7366ff6d951SJohn Birrell 
7376ff6d951SJohn Birrell 	free(de.de_strtab);
7386ff6d951SJohn Birrell 	free(de.de_sym);
7396ff6d951SJohn Birrell 	free(de.de_rel);
7406ff6d951SJohn Birrell 
7416ff6d951SJohn Birrell 	return (ret);
7426ff6d951SJohn Birrell }
7436ff6d951SJohn Birrell 
7446ff6d951SJohn Birrell static int
745e801af6fSMark Johnston dt_symtab_lookup(Elf_Data *data_sym, int start, int end, uintptr_t addr,
746e801af6fSMark Johnston     uint_t shn, GElf_Sym *sym, int uses_funcdesc, Elf *elf)
7476ff6d951SJohn Birrell {
748b08e2b04SJustin Hibbits 	Elf64_Addr symval;
749b08e2b04SJustin Hibbits 	Elf_Scn *opd_scn;
750b08e2b04SJustin Hibbits 	Elf_Data *opd_desc;
751e801af6fSMark Johnston 	int i;
7526ff6d951SJohn Birrell 
753e801af6fSMark Johnston 	for (i = start; i < end && gelf_getsym(data_sym, i, sym) != NULL; i++) {
754b08e2b04SJustin Hibbits 		if (GELF_ST_TYPE(sym->st_info) == STT_FUNC) {
755b08e2b04SJustin Hibbits 			symval = sym->st_value;
756b08e2b04SJustin Hibbits 			if (uses_funcdesc) {
757b08e2b04SJustin Hibbits 				opd_scn = elf_getscn(elf, sym->st_shndx);
758b08e2b04SJustin Hibbits 				opd_desc = elf_rawdata(opd_scn, NULL);
759b08e2b04SJustin Hibbits 				symval =
760b08e2b04SJustin Hibbits 				    *(uint64_t*)((char *)opd_desc->d_buf + symval);
761b08e2b04SJustin Hibbits 			}
762b08e2b04SJustin Hibbits 			if ((uses_funcdesc || shn == sym->st_shndx) &&
763e801af6fSMark Johnston 			    symval <= addr && addr < symval + sym->st_size)
7646ff6d951SJohn Birrell 				return (0);
7656ff6d951SJohn Birrell 		}
766b08e2b04SJustin Hibbits 	}
7676ff6d951SJohn Birrell 
768e801af6fSMark Johnston 	return (-1);
7696ff6d951SJohn Birrell }
7706ff6d951SJohn Birrell 
771b78ee15eSRuslan Bukin #if defined(__aarch64__)
772a2ea7849SMark Johnston #define	DT_OP_NOP		0xd503201f
773a2ea7849SMark Johnston #define	DT_OP_RET		0xd65f03c0
774a2ea7849SMark Johnston #define	DT_OP_CALL26		0x94000000
775a2ea7849SMark Johnston #define	DT_OP_JUMP26		0x14000000
776e627909dSMark Johnston #define	DT_REL_NONE		R_AARCH64_NONE
777a2ea7849SMark Johnston 
778b78ee15eSRuslan Bukin static int
779b78ee15eSRuslan Bukin dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
780b78ee15eSRuslan Bukin     uint32_t *off)
781b78ee15eSRuslan Bukin {
782a2ea7849SMark Johnston 	uint32_t *ip;
783a2ea7849SMark Johnston 
784a2ea7849SMark Johnston 	/*
785a2ea7849SMark Johnston 	 * Ensure that the offset is aligned on an instruction boundary.
786a2ea7849SMark Johnston 	 */
787a2ea7849SMark Johnston 	if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
7884e4805ddSEd Maste 		return (-1);
789a2ea7849SMark Johnston 
790a2ea7849SMark Johnston 	/*
791a2ea7849SMark Johnston 	 * We only know about some specific relocation types.
792a2ea7849SMark Johnston 	 * We also recognize relocation type NONE, since that gets used for
793a2ea7849SMark Johnston 	 * relocations of USDT probes, and we might be re-processing a file.
794a2ea7849SMark Johnston 	 */
795a2ea7849SMark Johnston 	if (GELF_R_TYPE(rela->r_info) != R_AARCH64_CALL26 &&
796a2ea7849SMark Johnston 	    GELF_R_TYPE(rela->r_info) != R_AARCH64_JUMP26 &&
797a2ea7849SMark Johnston 	    GELF_R_TYPE(rela->r_info) != R_AARCH64_NONE)
798a2ea7849SMark Johnston 		return (-1);
799a2ea7849SMark Johnston 
800a2ea7849SMark Johnston 	ip = (uint32_t *)(p + rela->r_offset);
801a2ea7849SMark Johnston 
802a2ea7849SMark Johnston 	/*
803a2ea7849SMark Johnston 	 * We may have already processed this object file in an earlier linker
804a2ea7849SMark Johnston 	 * invocation. Check to see if the present instruction sequence matches
805a2ea7849SMark Johnston 	 * the one we would install below.
806a2ea7849SMark Johnston 	 */
807a2ea7849SMark Johnston 	if (ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET)
808a2ea7849SMark Johnston 		return (0);
809a2ea7849SMark Johnston 
810a2ea7849SMark Johnston 	/*
811a2ea7849SMark Johnston 	 * We only expect call instructions with a displacement of 0, or a jump
812a2ea7849SMark Johnston 	 * instruction acting as a tail call.
813a2ea7849SMark Johnston 	 */
814a2ea7849SMark Johnston 	if (ip[0] != DT_OP_CALL26 && ip[0] != DT_OP_JUMP26) {
815a2ea7849SMark Johnston 		dt_dprintf("found %x instead of a call or jmp instruction at "
816a2ea7849SMark Johnston 		    "%llx\n", ip[0], (u_longlong_t)rela->r_offset);
817a2ea7849SMark Johnston 		return (-1);
818a2ea7849SMark Johnston 	}
819a2ea7849SMark Johnston 
820a2ea7849SMark Johnston 	/*
821a2ea7849SMark Johnston 	 * On arm64, we do not have to differentiate between regular probes and
822a2ea7849SMark Johnston 	 * is-enabled probes.  Both cases are encoded as a regular branch for
823a2ea7849SMark Johnston 	 * non-tail call locations, and a jump for tail call locations.  Calls
824a2ea7849SMark Johnston 	 * are to be converted into a no-op whereas jumps should become a
825a2ea7849SMark Johnston 	 * return.
826a2ea7849SMark Johnston 	 */
827a2ea7849SMark Johnston 	if (ip[0] == DT_OP_CALL26)
828a2ea7849SMark Johnston 		ip[0] = DT_OP_NOP;
829a2ea7849SMark Johnston 	else
830a2ea7849SMark Johnston 		ip[0] = DT_OP_RET;
831a2ea7849SMark Johnston 
832a2ea7849SMark Johnston 	return (0);
833b78ee15eSRuslan Bukin }
834b78ee15eSRuslan Bukin #elif defined(__arm__)
835d2d16e56SMark Johnston #define	DT_REL_NONE		R_ARM_NONE
836d2d16e56SMark Johnston 
8372693feb4SJohn Birrell static int
8382693feb4SJohn Birrell dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
8392693feb4SJohn Birrell     uint32_t *off)
8402693feb4SJohn Birrell {
841732f2c69SEd Maste 	printf("%s:%s(%d): arm not implemented\n", __FUNCTION__, __FILE__,
842732f2c69SEd Maste 	    __LINE__);
8434e4805ddSEd Maste 	return (-1);
8442693feb4SJohn Birrell }
8452693feb4SJohn Birrell #elif defined(__mips__)
846d2d16e56SMark Johnston #define	DT_REL_NONE		R_MIPS_NONE
847d2d16e56SMark Johnston 
8482693feb4SJohn Birrell static int
8492693feb4SJohn Birrell dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
8502693feb4SJohn Birrell     uint32_t *off)
8512693feb4SJohn Birrell {
852732f2c69SEd Maste 	printf("%s:%s(%d): MIPS not implemented\n", __FUNCTION__, __FILE__,
853732f2c69SEd Maste 	    __LINE__);
8544e4805ddSEd Maste 	return (-1);
8552693feb4SJohn Birrell }
8562693feb4SJohn Birrell #elif defined(__powerpc__)
85730b318b9SJustin Hibbits /* The sentinel is 'xor r3,r3,r3'. */
85830b318b9SJustin Hibbits #define DT_OP_XOR_R3	0x7c631a78
85930b318b9SJustin Hibbits 
86030b318b9SJustin Hibbits #define DT_OP_NOP		0x60000000
86130b318b9SJustin Hibbits #define DT_OP_BLR		0x4e800020
86230b318b9SJustin Hibbits 
86330b318b9SJustin Hibbits /* This captures all forms of branching to address. */
86430b318b9SJustin Hibbits #define DT_IS_BRANCH(inst)	((inst & 0xfc000000) == 0x48000000)
86530b318b9SJustin Hibbits #define DT_IS_BL(inst)	(DT_IS_BRANCH(inst) && (inst & 0x01))
86630b318b9SJustin Hibbits 
867d2d16e56SMark Johnston #define	DT_REL_NONE		R_PPC_NONE
868d2d16e56SMark Johnston 
8692693feb4SJohn Birrell static int
8702693feb4SJohn Birrell dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
8712693feb4SJohn Birrell     uint32_t *off)
8722693feb4SJohn Birrell {
87330b318b9SJustin Hibbits 	uint32_t *ip;
87430b318b9SJustin Hibbits 
87530b318b9SJustin Hibbits 	if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
87630b318b9SJustin Hibbits 		return (-1);
87730b318b9SJustin Hibbits 
87830b318b9SJustin Hibbits 	/*LINTED*/
87930b318b9SJustin Hibbits 	ip = (uint32_t *)(p + rela->r_offset);
88030b318b9SJustin Hibbits 
88130b318b9SJustin Hibbits 	/*
88230b318b9SJustin Hibbits 	 * We only know about some specific relocation types.
88330b318b9SJustin Hibbits 	 */
88430b318b9SJustin Hibbits 	if (GELF_R_TYPE(rela->r_info) != R_PPC_REL24 &&
885d2d16e56SMark Johnston 	    GELF_R_TYPE(rela->r_info) != R_PPC_PLTREL24 &&
886d2d16e56SMark Johnston 	    GELF_R_TYPE(rela->r_info) != R_PPC_NONE)
88730b318b9SJustin Hibbits 		return (-1);
88830b318b9SJustin Hibbits 
88930b318b9SJustin Hibbits 	/*
89030b318b9SJustin Hibbits 	 * We may have already processed this object file in an earlier linker
89130b318b9SJustin Hibbits 	 * invocation. Check to see if the present instruction sequence matches
89230b318b9SJustin Hibbits 	 * the one we would install below.
89330b318b9SJustin Hibbits 	 */
89430b318b9SJustin Hibbits 	if (isenabled) {
89530b318b9SJustin Hibbits 		if (ip[0] == DT_OP_XOR_R3) {
89630b318b9SJustin Hibbits 			(*off) += sizeof (ip[0]);
89730b318b9SJustin Hibbits 			return (0);
89830b318b9SJustin Hibbits 		}
89930b318b9SJustin Hibbits 	} else {
90030b318b9SJustin Hibbits 		if (ip[0] == DT_OP_NOP) {
90130b318b9SJustin Hibbits 			(*off) += sizeof (ip[0]);
90230b318b9SJustin Hibbits 			return (0);
90330b318b9SJustin Hibbits 		}
90430b318b9SJustin Hibbits 	}
90530b318b9SJustin Hibbits 
90630b318b9SJustin Hibbits 	/*
90730b318b9SJustin Hibbits 	 * We only expect branch to address instructions.
90830b318b9SJustin Hibbits 	 */
90930b318b9SJustin Hibbits 	if (!DT_IS_BRANCH(ip[0])) {
91030b318b9SJustin Hibbits 		dt_dprintf("found %x instead of a branch instruction at %llx\n",
91130b318b9SJustin Hibbits 		    ip[0], (u_longlong_t)rela->r_offset);
91230b318b9SJustin Hibbits 		return (-1);
91330b318b9SJustin Hibbits 	}
91430b318b9SJustin Hibbits 
91530b318b9SJustin Hibbits 	if (isenabled) {
91630b318b9SJustin Hibbits 		/*
91730b318b9SJustin Hibbits 		 * It would necessarily indicate incorrect usage if an is-
91830b318b9SJustin Hibbits 		 * enabled probe were tail-called so flag that as an error.
91930b318b9SJustin Hibbits 		 * It's also potentially (very) tricky to handle gracefully,
92030b318b9SJustin Hibbits 		 * but could be done if this were a desired use scenario.
92130b318b9SJustin Hibbits 		 */
92230b318b9SJustin Hibbits 		if (!DT_IS_BL(ip[0])) {
92330b318b9SJustin Hibbits 			dt_dprintf("tail call to is-enabled probe at %llx\n",
92430b318b9SJustin Hibbits 			    (u_longlong_t)rela->r_offset);
92530b318b9SJustin Hibbits 			return (-1);
92630b318b9SJustin Hibbits 		}
92730b318b9SJustin Hibbits 
92830b318b9SJustin Hibbits 		ip[0] = DT_OP_XOR_R3;
92930b318b9SJustin Hibbits 		(*off) += sizeof (ip[0]);
93030b318b9SJustin Hibbits 	} else {
93130b318b9SJustin Hibbits 		if (DT_IS_BL(ip[0]))
93230b318b9SJustin Hibbits 			ip[0] = DT_OP_NOP;
93330b318b9SJustin Hibbits 		else
93430b318b9SJustin Hibbits 			ip[0] = DT_OP_BLR;
93530b318b9SJustin Hibbits 	}
93630b318b9SJustin Hibbits 
9372693feb4SJohn Birrell 	return (0);
9382693feb4SJohn Birrell }
939ca20f8ecSRuslan Bukin #elif defined(__riscv)
940d2d16e56SMark Johnston #define	DT_REL_NONE		R_RISCV_NONE
941fed1ca4bSRuslan Bukin static int
942fed1ca4bSRuslan Bukin dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
943fed1ca4bSRuslan Bukin     uint32_t *off)
944fed1ca4bSRuslan Bukin {
945732f2c69SEd Maste 	printf("%s:%s(%d): RISC-V implementation required\n", __FUNCTION__,
946732f2c69SEd Maste 	    __FILE__, __LINE__);
9474e4805ddSEd Maste 	return (-1);
948fed1ca4bSRuslan Bukin }
9496ff6d951SJohn Birrell 
9506ff6d951SJohn Birrell #elif defined(__i386) || defined(__amd64)
9516ff6d951SJohn Birrell 
9526ff6d951SJohn Birrell #define	DT_OP_NOP		0x90
9532693feb4SJohn Birrell #define	DT_OP_RET		0xc3
9546ff6d951SJohn Birrell #define	DT_OP_CALL		0xe8
9552693feb4SJohn Birrell #define	DT_OP_JMP32		0xe9
9566ff6d951SJohn Birrell #define	DT_OP_REX_RAX		0x48
9576ff6d951SJohn Birrell #define	DT_OP_XOR_EAX_0		0x33
9586ff6d951SJohn Birrell #define	DT_OP_XOR_EAX_1		0xc0
9596ff6d951SJohn Birrell 
960d2d16e56SMark Johnston #define	DT_REL_NONE		R_386_NONE
961d2d16e56SMark Johnston 
9626ff6d951SJohn Birrell static int
9636ff6d951SJohn Birrell dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
9646ff6d951SJohn Birrell     uint32_t *off)
9656ff6d951SJohn Birrell {
9666ff6d951SJohn Birrell 	uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1);
9672693feb4SJohn Birrell 	uint8_t ret;
9686ff6d951SJohn Birrell 
9696ff6d951SJohn Birrell 	/*
9706ff6d951SJohn Birrell 	 * On x86, the first byte of the instruction is the call opcode and
9716ff6d951SJohn Birrell 	 * the next four bytes are the 32-bit address; the relocation is for
9726ff6d951SJohn Birrell 	 * the address operand. We back up the offset to the first byte of
9736ff6d951SJohn Birrell 	 * the instruction. For is-enabled probes, we later advance the offset
9746ff6d951SJohn Birrell 	 * so that it hits the first nop in the instruction sequence.
9756ff6d951SJohn Birrell 	 */
9766ff6d951SJohn Birrell 	(*off) -= 1;
9776ff6d951SJohn Birrell 
9786ff6d951SJohn Birrell 	/*
9796ff6d951SJohn Birrell 	 * We only know about some specific relocation types. Luckily
9806ff6d951SJohn Birrell 	 * these types have the same values on both 32-bit and 64-bit
9816ff6d951SJohn Birrell 	 * x86 architectures.
9826ff6d951SJohn Birrell 	 */
9836ff6d951SJohn Birrell 	if (GELF_R_TYPE(rela->r_info) != R_386_PC32 &&
984d2d16e56SMark Johnston 	    GELF_R_TYPE(rela->r_info) != R_386_PLT32 &&
985d2d16e56SMark Johnston 	    GELF_R_TYPE(rela->r_info) != R_386_NONE)
9866ff6d951SJohn Birrell 		return (-1);
9876ff6d951SJohn Birrell 
9886ff6d951SJohn Birrell 	/*
9896ff6d951SJohn Birrell 	 * We may have already processed this object file in an earlier linker
9906ff6d951SJohn Birrell 	 * invocation. Check to see if the present instruction sequence matches
9916ff6d951SJohn Birrell 	 * the one we would install. For is-enabled probes, we advance the
9922693feb4SJohn Birrell 	 * offset to the first nop instruction in the sequence to match the
9932693feb4SJohn Birrell 	 * text modification code below.
9946ff6d951SJohn Birrell 	 */
9956ff6d951SJohn Birrell 	if (!isenabled) {
9962693feb4SJohn Birrell 		if ((ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET) &&
9972693feb4SJohn Birrell 		    ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP &&
9982693feb4SJohn Birrell 		    ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP)
9996ff6d951SJohn Birrell 			return (0);
10006ff6d951SJohn Birrell 	} else if (dtp->dt_oflags & DTRACE_O_LP64) {
10016ff6d951SJohn Birrell 		if (ip[0] == DT_OP_REX_RAX &&
10026ff6d951SJohn Birrell 		    ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 &&
10032693feb4SJohn Birrell 		    (ip[3] == DT_OP_NOP || ip[3] == DT_OP_RET) &&
10042693feb4SJohn Birrell 		    ip[4] == DT_OP_NOP) {
10056ff6d951SJohn Birrell 			(*off) += 3;
10066ff6d951SJohn Birrell 			return (0);
10076ff6d951SJohn Birrell 		}
10086ff6d951SJohn Birrell 	} else {
10096ff6d951SJohn Birrell 		if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 &&
10102693feb4SJohn Birrell 		    (ip[2] == DT_OP_NOP || ip[2] == DT_OP_RET) &&
10112693feb4SJohn Birrell 		    ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) {
10126ff6d951SJohn Birrell 			(*off) += 2;
10136ff6d951SJohn Birrell 			return (0);
10146ff6d951SJohn Birrell 		}
10156ff6d951SJohn Birrell 	}
10166ff6d951SJohn Birrell 
10176ff6d951SJohn Birrell 	/*
10182693feb4SJohn Birrell 	 * We expect either a call instrution with a 32-bit displacement or a
10192693feb4SJohn Birrell 	 * jmp instruction with a 32-bit displacement acting as a tail-call.
10206ff6d951SJohn Birrell 	 */
10212693feb4SJohn Birrell 	if (ip[0] != DT_OP_CALL && ip[0] != DT_OP_JMP32) {
10222693feb4SJohn Birrell 		dt_dprintf("found %x instead of a call or jmp instruction at "
10232693feb4SJohn Birrell 		    "%llx\n", ip[0], (u_longlong_t)rela->r_offset);
10246ff6d951SJohn Birrell 		return (-1);
10256ff6d951SJohn Birrell 	}
10266ff6d951SJohn Birrell 
10272693feb4SJohn Birrell 	ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP;
10282693feb4SJohn Birrell 
10296ff6d951SJohn Birrell 	/*
10306ff6d951SJohn Birrell 	 * Establish the instruction sequence -- all nops for probes, and an
10316ff6d951SJohn Birrell 	 * instruction to clear the return value register (%eax/%rax) followed
10326ff6d951SJohn Birrell 	 * by nops for is-enabled probes. For is-enabled probes, we advance
10336ff6d951SJohn Birrell 	 * the offset to the first nop. This isn't stricly necessary but makes
10346ff6d951SJohn Birrell 	 * for more readable disassembly when the probe is enabled.
10356ff6d951SJohn Birrell 	 */
10366ff6d951SJohn Birrell 	if (!isenabled) {
10372693feb4SJohn Birrell 		ip[0] = ret;
10386ff6d951SJohn Birrell 		ip[1] = DT_OP_NOP;
10396ff6d951SJohn Birrell 		ip[2] = DT_OP_NOP;
10406ff6d951SJohn Birrell 		ip[3] = DT_OP_NOP;
10416ff6d951SJohn Birrell 		ip[4] = DT_OP_NOP;
10426ff6d951SJohn Birrell 	} else if (dtp->dt_oflags & DTRACE_O_LP64) {
10436ff6d951SJohn Birrell 		ip[0] = DT_OP_REX_RAX;
10446ff6d951SJohn Birrell 		ip[1] = DT_OP_XOR_EAX_0;
10456ff6d951SJohn Birrell 		ip[2] = DT_OP_XOR_EAX_1;
10462693feb4SJohn Birrell 		ip[3] = ret;
10476ff6d951SJohn Birrell 		ip[4] = DT_OP_NOP;
10486ff6d951SJohn Birrell 		(*off) += 3;
10496ff6d951SJohn Birrell 	} else {
10506ff6d951SJohn Birrell 		ip[0] = DT_OP_XOR_EAX_0;
10516ff6d951SJohn Birrell 		ip[1] = DT_OP_XOR_EAX_1;
10522693feb4SJohn Birrell 		ip[2] = ret;
10536ff6d951SJohn Birrell 		ip[3] = DT_OP_NOP;
10546ff6d951SJohn Birrell 		ip[4] = DT_OP_NOP;
10556ff6d951SJohn Birrell 		(*off) += 2;
10566ff6d951SJohn Birrell 	}
10576ff6d951SJohn Birrell 
10586ff6d951SJohn Birrell 	return (0);
10596ff6d951SJohn Birrell }
10606ff6d951SJohn Birrell 
10616ff6d951SJohn Birrell #else
10626ff6d951SJohn Birrell #error unknown ISA
10636ff6d951SJohn Birrell #endif
10646ff6d951SJohn Birrell 
10656ff6d951SJohn Birrell /*PRINTFLIKE5*/
10666ff6d951SJohn Birrell static int
10676ff6d951SJohn Birrell dt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs,
10686ff6d951SJohn Birrell     const char *format, ...)
10696ff6d951SJohn Birrell {
10706ff6d951SJohn Birrell 	va_list ap;
10716ff6d951SJohn Birrell 	dt_link_pair_t *pair;
10726ff6d951SJohn Birrell 
10736ff6d951SJohn Birrell 	va_start(ap, format);
10746ff6d951SJohn Birrell 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
10756ff6d951SJohn Birrell 	va_end(ap);
10766ff6d951SJohn Birrell 
10776ff6d951SJohn Birrell 	if (elf != NULL)
10786ff6d951SJohn Birrell 		(void) elf_end(elf);
10796ff6d951SJohn Birrell 
10806ff6d951SJohn Birrell 	if (fd >= 0)
10816ff6d951SJohn Birrell 		(void) close(fd);
10826ff6d951SJohn Birrell 
10836ff6d951SJohn Birrell 	while ((pair = bufs) != NULL) {
10846ff6d951SJohn Birrell 		bufs = pair->dlp_next;
10856ff6d951SJohn Birrell 		dt_free(dtp, pair->dlp_str);
10866ff6d951SJohn Birrell 		dt_free(dtp, pair->dlp_sym);
10876ff6d951SJohn Birrell 		dt_free(dtp, pair);
10886ff6d951SJohn Birrell 	}
10896ff6d951SJohn Birrell 
10906ff6d951SJohn Birrell 	return (dt_set_errno(dtp, EDT_COMPILER));
10916ff6d951SJohn Birrell }
10926ff6d951SJohn Birrell 
1093938acb08SMark Johnston /*
1094938acb08SMark Johnston  * Provide a unique identifier used when adding global symbols to an object.
1095938acb08SMark Johnston  * This is the FNV-1a hash of an absolute path for the file.
1096938acb08SMark Johnston  */
1097938acb08SMark Johnston static unsigned int
1098938acb08SMark Johnston hash_obj(const char *obj, int fd)
1099938acb08SMark Johnston {
1100938acb08SMark Johnston 	char path[PATH_MAX];
1101938acb08SMark Johnston 	unsigned int h;
1102938acb08SMark Johnston 
1103938acb08SMark Johnston 	if (realpath(obj, path) == NULL)
1104938acb08SMark Johnston 		return (-1);
1105938acb08SMark Johnston 
1106938acb08SMark Johnston 	for (h = 2166136261u, obj = &path[0]; *obj != '\0'; obj++)
1107938acb08SMark Johnston 		h = (h ^ *obj) * 16777619;
1108938acb08SMark Johnston 	h &= 0x7fffffff;
1109938acb08SMark Johnston 	return (h);
1110938acb08SMark Johnston }
1111938acb08SMark Johnston 
11126ff6d951SJohn Birrell static int
11136ff6d951SJohn Birrell process_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp)
11146ff6d951SJohn Birrell {
11156ff6d951SJohn Birrell 	static const char dt_prefix[] = "__dtrace";
11166ff6d951SJohn Birrell 	static const char dt_enabled[] = "enabled";
11176ff6d951SJohn Birrell 	static const char dt_symprefix[] = "$dtrace";
1118938acb08SMark Johnston 	static const char dt_symfmt[] = "%s%u.%s";
1119281e4f2dSMark Johnston 	static const char dt_weaksymfmt[] = "%s.%s";
11203a3e3279SMark Johnston 	char probename[DTRACE_NAMELEN];
11217283901aSBrandon Bergren 	int fd, i, ndx, eprobe, uses_funcdesc = 0, mod = 0;
11226ff6d951SJohn Birrell 	Elf *elf = NULL;
11236ff6d951SJohn Birrell 	GElf_Ehdr ehdr;
11246ff6d951SJohn Birrell 	Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt;
11256ff6d951SJohn Birrell 	Elf_Data *data_rel, *data_sym, *data_str, *data_tgt;
11266ff6d951SJohn Birrell 	GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt;
11276ff6d951SJohn Birrell 	GElf_Sym rsym, fsym, dsym;
11286ff6d951SJohn Birrell 	GElf_Rela rela;
11296ff6d951SJohn Birrell 	char *s, *p, *r;
11306ff6d951SJohn Birrell 	char pname[DTRACE_PROVNAMELEN];
11316ff6d951SJohn Birrell 	dt_provider_t *pvp;
11326ff6d951SJohn Birrell 	dt_probe_t *prp;
11336ff6d951SJohn Birrell 	uint32_t off, eclass, emachine1, emachine2;
1134e801af6fSMark Johnston 	size_t symsize, osym, nsym, isym, istr, len;
1135938acb08SMark Johnston 	unsigned int objkey;
11366ff6d951SJohn Birrell 	dt_link_pair_t *pair, *bufs = NULL;
11376ff6d951SJohn Birrell 	dt_strtab_t *strtab;
1138273efb05SMark Johnston 	void *tmp;
11396ff6d951SJohn Birrell 
11406ff6d951SJohn Birrell 	if ((fd = open64(obj, O_RDWR)) == -1) {
11416ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs,
11426ff6d951SJohn Birrell 		    "failed to open %s: %s", obj, strerror(errno)));
11436ff6d951SJohn Birrell 	}
11446ff6d951SJohn Birrell 
11456ff6d951SJohn Birrell 	if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
11466ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs,
11476ff6d951SJohn Birrell 		    "failed to process %s: %s", obj, elf_errmsg(elf_errno())));
11486ff6d951SJohn Birrell 	}
11496ff6d951SJohn Birrell 
11506ff6d951SJohn Birrell 	switch (elf_kind(elf)) {
11516ff6d951SJohn Birrell 	case ELF_K_ELF:
11526ff6d951SJohn Birrell 		break;
11536ff6d951SJohn Birrell 	case ELF_K_AR:
11546ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs, "archives are not "
11556ff6d951SJohn Birrell 		    "permitted; use the contents of the archive instead: %s",
11566ff6d951SJohn Birrell 		    obj));
11576ff6d951SJohn Birrell 	default:
11586ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs,
11596ff6d951SJohn Birrell 		    "invalid file type: %s", obj));
11606ff6d951SJohn Birrell 	}
11616ff6d951SJohn Birrell 
11626ff6d951SJohn Birrell 	if (gelf_getehdr(elf, &ehdr) == NULL) {
11636ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s",
11646ff6d951SJohn Birrell 		    obj));
11656ff6d951SJohn Birrell 	}
11666ff6d951SJohn Birrell 
11676ff6d951SJohn Birrell 	if (dtp->dt_oflags & DTRACE_O_LP64) {
11686ff6d951SJohn Birrell 		eclass = ELFCLASS64;
1169e7d939bdSMarcel Moolenaar #if defined(__mips__)
11702693feb4SJohn Birrell 		emachine1 = emachine2 = EM_MIPS;
11712693feb4SJohn Birrell #elif defined(__powerpc__)
11722693feb4SJohn Birrell 		emachine1 = emachine2 = EM_PPC64;
11737283901aSBrandon Bergren #if !defined(_CALL_ELF) || _CALL_ELF == 1
11747283901aSBrandon Bergren 		uses_funcdesc = 1;
11757283901aSBrandon Bergren #endif
11766ff6d951SJohn Birrell #elif defined(__i386) || defined(__amd64)
11776ff6d951SJohn Birrell 		emachine1 = emachine2 = EM_AMD64;
1178a2ea7849SMark Johnston #elif defined(__aarch64__)
1179a2ea7849SMark Johnston 		emachine1 = emachine2 = EM_AARCH64;
11806ff6d951SJohn Birrell #endif
11816ff6d951SJohn Birrell 		symsize = sizeof (Elf64_Sym);
11826ff6d951SJohn Birrell 	} else {
11836ff6d951SJohn Birrell 		eclass = ELFCLASS32;
11842693feb4SJohn Birrell #if defined(__arm__)
11852693feb4SJohn Birrell 		emachine1 = emachine2 = EM_ARM;
11862693feb4SJohn Birrell #elif defined(__mips__)
11872693feb4SJohn Birrell 		emachine1 = emachine2 = EM_MIPS;
11882693feb4SJohn Birrell #elif defined(__powerpc__)
11892693feb4SJohn Birrell 		emachine1 = emachine2 = EM_PPC;
1190e7d939bdSMarcel Moolenaar #elif defined(__i386) || defined(__amd64)
11916ff6d951SJohn Birrell 		emachine1 = emachine2 = EM_386;
11926ff6d951SJohn Birrell #endif
11936ff6d951SJohn Birrell 		symsize = sizeof (Elf32_Sym);
11946ff6d951SJohn Birrell 	}
11956ff6d951SJohn Birrell 
11966ff6d951SJohn Birrell 	if (ehdr.e_ident[EI_CLASS] != eclass) {
11976ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs,
11986ff6d951SJohn Birrell 		    "incorrect ELF class for object file: %s", obj));
11996ff6d951SJohn Birrell 	}
12006ff6d951SJohn Birrell 
12016ff6d951SJohn Birrell 	if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) {
12026ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs,
12036ff6d951SJohn Birrell 		    "incorrect ELF machine type for object file: %s", obj));
12046ff6d951SJohn Birrell 	}
12056ff6d951SJohn Birrell 
12066ff6d951SJohn Birrell 	/*
12076ff6d951SJohn Birrell 	 * We use this token as a relatively unique handle for this file on the
12086ff6d951SJohn Birrell 	 * system in order to disambiguate potential conflicts between files of
12096ff6d951SJohn Birrell 	 * the same name which contain identially named local symbols.
12106ff6d951SJohn Birrell 	 */
1211938acb08SMark Johnston 	if ((objkey = hash_obj(obj, fd)) == (unsigned int)-1)
12126ff6d951SJohn Birrell 		return (dt_link_error(dtp, elf, fd, bufs,
12136ff6d951SJohn Birrell 		    "failed to generate unique key for object file: %s", obj));
12146ff6d951SJohn Birrell 
12156ff6d951SJohn Birrell 	scn_rel = NULL;
12166ff6d951SJohn Birrell 	while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) {
12176ff6d951SJohn Birrell 		if (gelf_getshdr(scn_rel, &shdr_rel) == NULL)
12186ff6d951SJohn Birrell 			goto err;
12196ff6d951SJohn Birrell 
12206ff6d951SJohn Birrell 		/*
12216ff6d951SJohn Birrell 		 * Skip any non-relocation sections.
12226ff6d951SJohn Birrell 		 */
12236ff6d951SJohn Birrell 		if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL)
12246ff6d951SJohn Birrell 			continue;
12256ff6d951SJohn Birrell 
12266ff6d951SJohn Birrell 		if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL)
12276ff6d951SJohn Birrell 			goto err;
12286ff6d951SJohn Birrell 
12296ff6d951SJohn Birrell 		/*
12306ff6d951SJohn Birrell 		 * Grab the section, section header and section data for the
12316ff6d951SJohn Birrell 		 * symbol table that this relocation section references.
12326ff6d951SJohn Birrell 		 */
12336ff6d951SJohn Birrell 		if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL ||
12346ff6d951SJohn Birrell 		    gelf_getshdr(scn_sym, &shdr_sym) == NULL ||
12356ff6d951SJohn Birrell 		    (data_sym = elf_getdata(scn_sym, NULL)) == NULL)
12366ff6d951SJohn Birrell 			goto err;
12376ff6d951SJohn Birrell 
12386ff6d951SJohn Birrell 		/*
12396ff6d951SJohn Birrell 		 * Ditto for that symbol table's string table.
12406ff6d951SJohn Birrell 		 */
12416ff6d951SJohn Birrell 		if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL ||
12426ff6d951SJohn Birrell 		    gelf_getshdr(scn_str, &shdr_str) == NULL ||
12436ff6d951SJohn Birrell 		    (data_str = elf_getdata(scn_str, NULL)) == NULL)
12446ff6d951SJohn Birrell 			goto err;
12456ff6d951SJohn Birrell 
12466ff6d951SJohn Birrell 		/*
12476ff6d951SJohn Birrell 		 * Grab the section, section header and section data for the
12486ff6d951SJohn Birrell 		 * target section for the relocations. For the relocations
12496ff6d951SJohn Birrell 		 * we're looking for -- this will typically be the text of the
12506ff6d951SJohn Birrell 		 * object file.
12516ff6d951SJohn Birrell 		 */
12526ff6d951SJohn Birrell 		if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL ||
12536ff6d951SJohn Birrell 		    gelf_getshdr(scn_tgt, &shdr_tgt) == NULL ||
12546ff6d951SJohn Birrell 		    (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL)
12556ff6d951SJohn Birrell 			goto err;
12566ff6d951SJohn Birrell 
12576ff6d951SJohn Birrell 		/*
12586ff6d951SJohn Birrell 		 * We're looking for relocations to symbols matching this form:
12596ff6d951SJohn Birrell 		 *
12606ff6d951SJohn Birrell 		 *   __dtrace[enabled]_<prov>___<probe>
12616ff6d951SJohn Birrell 		 *
12626ff6d951SJohn Birrell 		 * For the generated object, we need to record the location
12636ff6d951SJohn Birrell 		 * identified by the relocation, and create a new relocation
12646ff6d951SJohn Birrell 		 * in the generated object that will be resolved at link time
12656ff6d951SJohn Birrell 		 * to the location of the function in which the probe is
12666ff6d951SJohn Birrell 		 * embedded. In the target object, we change the matched symbol
12676ff6d951SJohn Birrell 		 * so that it will be ignored at link time, and we modify the
12686ff6d951SJohn Birrell 		 * target (text) section to replace the call instruction with
12696ff6d951SJohn Birrell 		 * one or more nops.
12706ff6d951SJohn Birrell 		 *
1271e801af6fSMark Johnston 		 * To avoid runtime overhead, the relocations added to the
1272e801af6fSMark Johnston 		 * generated object should be resolved at static link time. We
1273e801af6fSMark Johnston 		 * therefore create aliases for the functions that contain
1274e801af6fSMark Johnston 		 * probes. An alias is global (so that the relocation from the
1275e801af6fSMark Johnston 		 * generated object can be resolved), and hidden (so that its
1276e801af6fSMark Johnston 		 * address is known at static link time). Such aliases have this
1277e801af6fSMark Johnston 		 * form:
12786ff6d951SJohn Birrell 		 *
12796ff6d951SJohn Birrell 		 *   $dtrace<key>.<function>
12806ff6d951SJohn Birrell 		 *
12816ff6d951SJohn Birrell 		 * We take a first pass through all the relocations to
12826ff6d951SJohn Birrell 		 * populate our string table and count the number of extra
12836ff6d951SJohn Birrell 		 * symbols we'll require.
1284d2d16e56SMark Johnston 		 *
1285d2d16e56SMark Johnston 		 * We also handle the case where the object has already been
1286d2d16e56SMark Johnston 		 * processed, to support incremental rebuilds.  Relocations
1287d2d16e56SMark Johnston 		 * of interest are converted to type NONE, but all information
1288d2d16e56SMark Johnston 		 * needed to reconstruct the output DOF is retained.
12896ff6d951SJohn Birrell 		 */
12906ff6d951SJohn Birrell 		strtab = dt_strtab_create(1);
12916ff6d951SJohn Birrell 		nsym = 0;
12926ff6d951SJohn Birrell 		isym = data_sym->d_size / symsize;
12936ff6d951SJohn Birrell 		istr = data_str->d_size;
12946ff6d951SJohn Birrell 
12956ff6d951SJohn Birrell 		for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
12966ff6d951SJohn Birrell 			if (shdr_rel.sh_type == SHT_RELA) {
12976ff6d951SJohn Birrell 				if (gelf_getrela(data_rel, i, &rela) == NULL)
12986ff6d951SJohn Birrell 					continue;
12996ff6d951SJohn Birrell 			} else {
13006ff6d951SJohn Birrell 				GElf_Rel rel;
13016ff6d951SJohn Birrell 				if (gelf_getrel(data_rel, i, &rel) == NULL)
13026ff6d951SJohn Birrell 					continue;
13036ff6d951SJohn Birrell 				rela.r_offset = rel.r_offset;
13046ff6d951SJohn Birrell 				rela.r_info = rel.r_info;
13056ff6d951SJohn Birrell 				rela.r_addend = 0;
13066ff6d951SJohn Birrell 			}
13076ff6d951SJohn Birrell 
13086ff6d951SJohn Birrell 			if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info),
13096ff6d951SJohn Birrell 			    &rsym) == NULL) {
13106ff6d951SJohn Birrell 				dt_strtab_destroy(strtab);
13116ff6d951SJohn Birrell 				goto err;
13126ff6d951SJohn Birrell 			}
13136ff6d951SJohn Birrell 
13146ff6d951SJohn Birrell 			s = (char *)data_str->d_buf + rsym.st_name;
13156ff6d951SJohn Birrell 
13166ff6d951SJohn Birrell 			if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
13176ff6d951SJohn Birrell 				continue;
13186ff6d951SJohn Birrell 
1319e801af6fSMark Johnston 			if (dt_symtab_lookup(data_sym, 0, isym, rela.r_offset,
13207283901aSBrandon Bergren 			    shdr_rel.sh_info, &fsym, uses_funcdesc,
1321e801af6fSMark Johnston 			    elf) != 0) {
13226ff6d951SJohn Birrell 				dt_strtab_destroy(strtab);
13236ff6d951SJohn Birrell 				goto err;
13246ff6d951SJohn Birrell 			}
13256ff6d951SJohn Birrell 
13266ff6d951SJohn Birrell 			if (fsym.st_name > data_str->d_size) {
13276ff6d951SJohn Birrell 				dt_strtab_destroy(strtab);
13286ff6d951SJohn Birrell 				goto err;
13296ff6d951SJohn Birrell 			}
13306ff6d951SJohn Birrell 
13316ff6d951SJohn Birrell 			s = (char *)data_str->d_buf + fsym.st_name;
13326ff6d951SJohn Birrell 
13336ff6d951SJohn Birrell 			/*
13346ff6d951SJohn Birrell 			 * If this symbol isn't of type function, we've really
13356ff6d951SJohn Birrell 			 * driven off the rails or the object file is corrupt.
13366ff6d951SJohn Birrell 			 */
13376ff6d951SJohn Birrell 			if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) {
13386ff6d951SJohn Birrell 				dt_strtab_destroy(strtab);
13396ff6d951SJohn Birrell 				return (dt_link_error(dtp, elf, fd, bufs,
13406ff6d951SJohn Birrell 				    "expected %s to be of type function", s));
13416ff6d951SJohn Birrell 			}
13426ff6d951SJohn Birrell 
1343ad1633b3SMark Johnston 			/*
1344ad1633b3SMark Johnston 			 * Aliases of weak symbols don't get a uniquifier.
1345ad1633b3SMark Johnston 			 */
1346ad1633b3SMark Johnston 			if (GELF_ST_BIND(fsym.st_info) == STB_WEAK)
1347ad1633b3SMark Johnston 				len = snprintf(NULL, 0, dt_weaksymfmt,
1348ad1633b3SMark Johnston 				    dt_symprefix, s) + 1;
1349ad1633b3SMark Johnston 			else
13506ff6d951SJohn Birrell 				len = snprintf(NULL, 0, dt_symfmt, dt_symprefix,
13516ff6d951SJohn Birrell 				    objkey, s) + 1;
13526ff6d951SJohn Birrell 			if ((p = dt_alloc(dtp, len)) == NULL) {
13536ff6d951SJohn Birrell 				dt_strtab_destroy(strtab);
13546ff6d951SJohn Birrell 				goto err;
13556ff6d951SJohn Birrell 			}
13566ff6d951SJohn Birrell 			(void) snprintf(p, len, dt_symfmt, dt_symprefix,
13576ff6d951SJohn Birrell 			    objkey, s);
13586ff6d951SJohn Birrell 
13596ff6d951SJohn Birrell 			if (dt_strtab_index(strtab, p) == -1) {
1360d2d16e56SMark Johnston 				/*
1361d2d16e56SMark Johnston 				 * Do not add new symbols if this object file
1362d2d16e56SMark Johnston 				 * has already been processed.
1363d2d16e56SMark Johnston 				 */
1364d2d16e56SMark Johnston 				if (GELF_R_TYPE(rela.r_info) != DT_REL_NONE)
13656ff6d951SJohn Birrell 					nsym++;
13666ff6d951SJohn Birrell 				(void) dt_strtab_insert(strtab, p);
13676ff6d951SJohn Birrell 			}
13686ff6d951SJohn Birrell 
13696ff6d951SJohn Birrell 			dt_free(dtp, p);
13706ff6d951SJohn Birrell 		}
13716ff6d951SJohn Birrell 
13726ff6d951SJohn Birrell 		/*
1373d2d16e56SMark Johnston 		 * If any new probes were found, allocate the additional space
1374d2d16e56SMark Johnston 		 * for the symbol table and string table, copying the old data
1375d2d16e56SMark Johnston 		 * into the new buffers, and marking the buffers as dirty. We
1376d2d16e56SMark Johnston 		 * inject those newly allocated buffers into the libelf data
1377e801af6fSMark Johnston 		 * structures, but are still responsible for freeing them once
1378e801af6fSMark Johnston 		 * we're done with the elf handle.
13796ff6d951SJohn Birrell 		 */
1380d2d16e56SMark Johnston 		osym = isym;
13816ff6d951SJohn Birrell 		if (nsym > 0) {
13826ff6d951SJohn Birrell 			/*
13836ff6d951SJohn Birrell 			 * The first byte of the string table is reserved for
13846ff6d951SJohn Birrell 			 * the \0 entry.
13856ff6d951SJohn Birrell 			 */
13866ff6d951SJohn Birrell 			len = dt_strtab_size(strtab) - 1;
13876ff6d951SJohn Birrell 
13886ff6d951SJohn Birrell 			assert(len > 0);
13896ff6d951SJohn Birrell 			assert(dt_strtab_index(strtab, "") == 0);
13906ff6d951SJohn Birrell 
13916ff6d951SJohn Birrell 			dt_strtab_destroy(strtab);
13926ff6d951SJohn Birrell 
13936ff6d951SJohn Birrell 			if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL)
13946ff6d951SJohn Birrell 				goto err;
13956ff6d951SJohn Birrell 
13966ff6d951SJohn Birrell 			if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size +
13976ff6d951SJohn Birrell 			    len)) == NULL) {
13986ff6d951SJohn Birrell 				dt_free(dtp, pair);
13996ff6d951SJohn Birrell 				goto err;
14006ff6d951SJohn Birrell 			}
14016ff6d951SJohn Birrell 
14026ff6d951SJohn Birrell 			if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size +
14036ff6d951SJohn Birrell 			    nsym * symsize)) == NULL) {
14046ff6d951SJohn Birrell 				dt_free(dtp, pair->dlp_str);
14056ff6d951SJohn Birrell 				dt_free(dtp, pair);
14066ff6d951SJohn Birrell 				goto err;
14076ff6d951SJohn Birrell 			}
14086ff6d951SJohn Birrell 
14096ff6d951SJohn Birrell 			pair->dlp_next = bufs;
14106ff6d951SJohn Birrell 			bufs = pair;
14116ff6d951SJohn Birrell 
14126ff6d951SJohn Birrell 			bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size);
1413273efb05SMark Johnston 			tmp = data_str->d_buf;
14146ff6d951SJohn Birrell 			data_str->d_buf = pair->dlp_str;
1415273efb05SMark Johnston 			pair->dlp_str = tmp;
14166ff6d951SJohn Birrell 			data_str->d_size += len;
14176ff6d951SJohn Birrell 			(void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY);
14186ff6d951SJohn Birrell 
14196ff6d951SJohn Birrell 			shdr_str.sh_size += len;
14206ff6d951SJohn Birrell 			(void) gelf_update_shdr(scn_str, &shdr_str);
14216ff6d951SJohn Birrell 
14226ff6d951SJohn Birrell 			bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size);
1423273efb05SMark Johnston 			tmp = data_sym->d_buf;
14246ff6d951SJohn Birrell 			data_sym->d_buf = pair->dlp_sym;
1425273efb05SMark Johnston 			pair->dlp_sym = tmp;
14266ff6d951SJohn Birrell 			data_sym->d_size += nsym * symsize;
14276ff6d951SJohn Birrell 			(void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY);
14286ff6d951SJohn Birrell 
14296ff6d951SJohn Birrell 			shdr_sym.sh_size += nsym * symsize;
14306ff6d951SJohn Birrell 			(void) gelf_update_shdr(scn_sym, &shdr_sym);
14316ff6d951SJohn Birrell 
14326ff6d951SJohn Birrell 			nsym += isym;
1433d2d16e56SMark Johnston 		} else if (dt_strtab_empty(strtab)) {
14346ff6d951SJohn Birrell 			dt_strtab_destroy(strtab);
1435e801af6fSMark Johnston 			continue;
14366ff6d951SJohn Birrell 		}
14376ff6d951SJohn Birrell 
14386ff6d951SJohn Birrell 		/*
14396ff6d951SJohn Birrell 		 * Now that the tables have been allocated, perform the
14406ff6d951SJohn Birrell 		 * modifications described above.
14416ff6d951SJohn Birrell 		 */
14426ff6d951SJohn Birrell 		for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
14436ff6d951SJohn Birrell 			if (shdr_rel.sh_type == SHT_RELA) {
14446ff6d951SJohn Birrell 				if (gelf_getrela(data_rel, i, &rela) == NULL)
14456ff6d951SJohn Birrell 					continue;
14466ff6d951SJohn Birrell 			} else {
14476ff6d951SJohn Birrell 				GElf_Rel rel;
14486ff6d951SJohn Birrell 				if (gelf_getrel(data_rel, i, &rel) == NULL)
14496ff6d951SJohn Birrell 					continue;
14506ff6d951SJohn Birrell 				rela.r_offset = rel.r_offset;
14516ff6d951SJohn Birrell 				rela.r_info = rel.r_info;
14526ff6d951SJohn Birrell 				rela.r_addend = 0;
14536ff6d951SJohn Birrell 			}
14546ff6d951SJohn Birrell 
14556ff6d951SJohn Birrell 			ndx = GELF_R_SYM(rela.r_info);
14566ff6d951SJohn Birrell 
14576ff6d951SJohn Birrell 			if (gelf_getsym(data_sym, ndx, &rsym) == NULL ||
14586ff6d951SJohn Birrell 			    rsym.st_name > data_str->d_size)
14596ff6d951SJohn Birrell 				goto err;
14606ff6d951SJohn Birrell 
14616ff6d951SJohn Birrell 			s = (char *)data_str->d_buf + rsym.st_name;
14626ff6d951SJohn Birrell 
14636ff6d951SJohn Birrell 			if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
14646ff6d951SJohn Birrell 				continue;
14656ff6d951SJohn Birrell 
14666ff6d951SJohn Birrell 			s += sizeof (dt_prefix) - 1;
14676ff6d951SJohn Birrell 
14686ff6d951SJohn Birrell 			/*
14696ff6d951SJohn Birrell 			 * Check to see if this is an 'is-enabled' check as
14706ff6d951SJohn Birrell 			 * opposed to a normal probe.
14716ff6d951SJohn Birrell 			 */
14726ff6d951SJohn Birrell 			if (strncmp(s, dt_enabled,
14736ff6d951SJohn Birrell 			    sizeof (dt_enabled) - 1) == 0) {
14746ff6d951SJohn Birrell 				s += sizeof (dt_enabled) - 1;
14756ff6d951SJohn Birrell 				eprobe = 1;
14766ff6d951SJohn Birrell 				*eprobesp = 1;
14776ff6d951SJohn Birrell 				dt_dprintf("is-enabled probe\n");
14786ff6d951SJohn Birrell 			} else {
14796ff6d951SJohn Birrell 				eprobe = 0;
14806ff6d951SJohn Birrell 				dt_dprintf("normal probe\n");
14816ff6d951SJohn Birrell 			}
14826ff6d951SJohn Birrell 
14836ff6d951SJohn Birrell 			if (*s++ != '_')
14846ff6d951SJohn Birrell 				goto err;
14856ff6d951SJohn Birrell 
14866ff6d951SJohn Birrell 			if ((p = strstr(s, "___")) == NULL ||
14876ff6d951SJohn Birrell 			    p - s >= sizeof (pname))
14886ff6d951SJohn Birrell 				goto err;
14896ff6d951SJohn Birrell 
14906ff6d951SJohn Birrell 			bcopy(s, pname, p - s);
14916ff6d951SJohn Birrell 			pname[p - s] = '\0';
14926ff6d951SJohn Birrell 
1493e801af6fSMark Johnston 			if (dt_symtab_lookup(data_sym, osym, isym,
1494e801af6fSMark Johnston 			    rela.r_offset, shdr_rel.sh_info, &fsym,
14957283901aSBrandon Bergren 			    uses_funcdesc, elf) == 0) {
14966ff6d951SJohn Birrell 				if (fsym.st_name > data_str->d_size)
14976ff6d951SJohn Birrell 					goto err;
14986ff6d951SJohn Birrell 
1499281e4f2dSMark Johnston 				r = s = (char *) data_str->d_buf + fsym.st_name;
1500281e4f2dSMark Johnston 				assert(strstr(s, dt_symprefix) == s);
1501281e4f2dSMark Johnston 				s = strchr(s, '.') + 1;
1502281e4f2dSMark Johnston 			} else if (dt_symtab_lookup(data_sym, 0, osym,
1503281e4f2dSMark Johnston 			    rela.r_offset, shdr_rel.sh_info, &fsym,
15047283901aSBrandon Bergren 			    uses_funcdesc, elf) == 0) {
1505281e4f2dSMark Johnston 				u_int bind;
1506281e4f2dSMark Johnston 
1507281e4f2dSMark Johnston 				bind = GELF_ST_BIND(fsym.st_info) == STB_WEAK ?
1508281e4f2dSMark Johnston 				    STB_WEAK : STB_GLOBAL;
1509d2d16e56SMark Johnston 				s = (char *) data_str->d_buf + fsym.st_name;
1510d2d16e56SMark Johnston 				if (GELF_R_TYPE(rela.r_info) != DT_REL_NONE) {
15116ff6d951SJohn Birrell 					/*
1512d2d16e56SMark Johnston 					 * Emit an alias for the symbol. It
1513d2d16e56SMark Johnston 					 * needs to be non-preemptible so that
1514d2d16e56SMark Johnston 					 * .SUNW_dof relocations may be resolved
1515d2d16e56SMark Johnston 					 * at static link time. Aliases of weak
1516d2d16e56SMark Johnston 					 * symbols are given a non-unique name
1517d2d16e56SMark Johnston 					 * so that they may be merged by the
1518d2d16e56SMark Johnston 					 * linker.
15196ff6d951SJohn Birrell 					 */
15206ff6d951SJohn Birrell 					dsym = fsym;
15216ff6d951SJohn Birrell 					dsym.st_name = istr;
1522d2d16e56SMark Johnston 					dsym.st_info = GELF_ST_INFO(bind,
1523d2d16e56SMark Johnston 					    STT_FUNC);
1524d2d16e56SMark Johnston 					dsym.st_other =
1525d2d16e56SMark Johnston 					    GELF_ST_VISIBILITY(STV_HIDDEN);
1526d2d16e56SMark Johnston 					(void) gelf_update_sym(data_sym, isym,
1527d2d16e56SMark Johnston 					    &dsym);
15286ff6d951SJohn Birrell 					isym++;
15296ff6d951SJohn Birrell 					assert(isym <= nsym);
1530d2d16e56SMark Johnston 
1531d2d16e56SMark Johnston 					r = (char *) data_str->d_buf + istr;
1532d2d16e56SMark Johnston 					if (bind == STB_WEAK) {
1533d2d16e56SMark Johnston 						istr += sprintf(r,
1534d2d16e56SMark Johnston 						    dt_weaksymfmt, dt_symprefix,
1535d2d16e56SMark Johnston 						    s);
1536d2d16e56SMark Johnston 					} else {
1537d2d16e56SMark Johnston 						istr += sprintf(r, dt_symfmt,
1538d2d16e56SMark Johnston 						    dt_symprefix, objkey, s);
1539d2d16e56SMark Johnston 					}
1540d2d16e56SMark Johnston 					istr++;
1541d2d16e56SMark Johnston 				} else {
1542d2d16e56SMark Johnston 					if (bind == STB_WEAK) {
1543d2d16e56SMark Johnston 						(void) asprintf(&r,
1544d2d16e56SMark Johnston 						    dt_weaksymfmt, dt_symprefix,
1545d2d16e56SMark Johnston 						    s);
1546d2d16e56SMark Johnston 					} else {
1547d2d16e56SMark Johnston 						(void) asprintf(&r, dt_symfmt,
1548d2d16e56SMark Johnston 						    dt_symprefix, objkey, s);
1549d2d16e56SMark Johnston 					}
1550d2d16e56SMark Johnston 				}
1551d2d16e56SMark Johnston 			} else {
1552281e4f2dSMark Johnston 				goto err;
1553d2d16e56SMark Johnston 			}
15546ff6d951SJohn Birrell 
15556ff6d951SJohn Birrell 			if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) {
15566ff6d951SJohn Birrell 				return (dt_link_error(dtp, elf, fd, bufs,
15576ff6d951SJohn Birrell 				    "no such provider %s", pname));
15586ff6d951SJohn Birrell 			}
15596ff6d951SJohn Birrell 
15603a3e3279SMark Johnston 			if (strlcpy(probename, p + 3, sizeof (probename)) >=
15613a3e3279SMark Johnston 			    sizeof (probename))
15626ff6d951SJohn Birrell 				return (dt_link_error(dtp, elf, fd, bufs,
15633a3e3279SMark Johnston 				    "invalid probe name %s", probename));
15643a3e3279SMark Johnston 			(void) strhyphenate(probename);
15653a3e3279SMark Johnston 			if ((prp = dt_probe_lookup(pvp, probename)) == NULL)
15663a3e3279SMark Johnston 				return (dt_link_error(dtp, elf, fd, bufs,
15673a3e3279SMark Johnston 				    "no such probe %s", probename));
15686ff6d951SJohn Birrell 
15696ff6d951SJohn Birrell 			assert(fsym.st_value <= rela.r_offset);
15706ff6d951SJohn Birrell 
15716ff6d951SJohn Birrell 			off = rela.r_offset - fsym.st_value;
15726ff6d951SJohn Birrell 			if (dt_modtext(dtp, data_tgt->d_buf, eprobe,
15730f2bd1e8SRui Paulo 			    &rela, &off) != 0)
15746ff6d951SJohn Birrell 				goto err;
15756ff6d951SJohn Birrell 
15766ff6d951SJohn Birrell 			if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) {
15776ff6d951SJohn Birrell 				return (dt_link_error(dtp, elf, fd, bufs,
15786ff6d951SJohn Birrell 				    "failed to allocate space for probe"));
15796ff6d951SJohn Birrell 			}
15800e15d9fbSMark Johnston 
15810f2bd1e8SRui Paulo 			/*
1582d2d16e56SMark Johnston 			 * We are done with this relocation, but it must be
1583d2d16e56SMark Johnston 			 * preserved in order to support incremental rebuilds.
15840f2bd1e8SRui Paulo 			 */
15852b374230SMark Johnston 			if (shdr_rel.sh_type == SHT_RELA) {
1586c612709bSMark Johnston 				rela.r_info = GELF_R_INFO(
1587c612709bSMark Johnston 				    GELF_R_SYM(rela.r_info), DT_REL_NONE);
15880f2bd1e8SRui Paulo 				(void) gelf_update_rela(data_rel, i, &rela);
15892b374230SMark Johnston 			} else {
15902b374230SMark Johnston 				GElf_Rel rel;
1591d2d16e56SMark Johnston 				rel.r_offset = rela.r_offset;
1592c612709bSMark Johnston 				rel.r_info = GELF_R_INFO(
1593c612709bSMark Johnston 				    GELF_R_SYM(rela.r_info), DT_REL_NONE);
15942b374230SMark Johnston 				(void) gelf_update_rel(data_rel, i, &rel);
15952b374230SMark Johnston 			}
15966ff6d951SJohn Birrell 
15976ff6d951SJohn Birrell 			mod = 1;
15986ff6d951SJohn Birrell 			(void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY);
15996ff6d951SJohn Birrell 
16006ff6d951SJohn Birrell 			/*
16016ff6d951SJohn Birrell 			 * This symbol may already have been marked to
16026ff6d951SJohn Birrell 			 * be ignored by another relocation referencing
16036ff6d951SJohn Birrell 			 * the same symbol or if this object file has
16046ff6d951SJohn Birrell 			 * already been processed by an earlier link
16056ff6d951SJohn Birrell 			 * invocation.
16066ff6d951SJohn Birrell 			 */
16070e15d9fbSMark Johnston 			if (rsym.st_shndx != SHN_ABS) {
16080e15d9fbSMark Johnston 				rsym.st_shndx = SHN_ABS;
16096ff6d951SJohn Birrell 				(void) gelf_update_sym(data_sym, ndx, &rsym);
16106ff6d951SJohn Birrell 			}
16116ff6d951SJohn Birrell 		}
16126ff6d951SJohn Birrell 	}
16136ff6d951SJohn Birrell 
16146ff6d951SJohn Birrell 	if (mod && elf_update(elf, ELF_C_WRITE) == -1)
16156ff6d951SJohn Birrell 		goto err;
16166ff6d951SJohn Birrell 
16176ff6d951SJohn Birrell 	(void) elf_end(elf);
16186ff6d951SJohn Birrell 	(void) close(fd);
16196ff6d951SJohn Birrell 
16206ff6d951SJohn Birrell 	while ((pair = bufs) != NULL) {
16216ff6d951SJohn Birrell 		bufs = pair->dlp_next;
16226ff6d951SJohn Birrell 		dt_free(dtp, pair->dlp_str);
16236ff6d951SJohn Birrell 		dt_free(dtp, pair->dlp_sym);
16246ff6d951SJohn Birrell 		dt_free(dtp, pair);
16256ff6d951SJohn Birrell 	}
16266ff6d951SJohn Birrell 
16276ff6d951SJohn Birrell 	return (0);
16286ff6d951SJohn Birrell 
16296ff6d951SJohn Birrell err:
16306ff6d951SJohn Birrell 	return (dt_link_error(dtp, elf, fd, bufs,
16316ff6d951SJohn Birrell 	    "an error was encountered while processing %s", obj));
16326ff6d951SJohn Birrell }
16336ff6d951SJohn Birrell 
16346ff6d951SJohn Birrell int
16356ff6d951SJohn Birrell dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
16366ff6d951SJohn Birrell     const char *file, int objc, char *const objv[])
16376ff6d951SJohn Birrell {
16382693feb4SJohn Birrell 	char tfile[PATH_MAX];
16396ff6d951SJohn Birrell 	char drti[PATH_MAX];
16406ff6d951SJohn Birrell 	dof_hdr_t *dof;
16416ff6d951SJohn Birrell 	int fd, status, i, cur;
16426ff6d951SJohn Birrell 	char *cmd, tmp;
16436ff6d951SJohn Birrell 	size_t len;
16446ff6d951SJohn Birrell 	int eprobes = 0, ret = 0;
16456ff6d951SJohn Birrell 
16466ff6d951SJohn Birrell 	/*
16476ff6d951SJohn Birrell 	 * A NULL program indicates a special use in which we just link
16486ff6d951SJohn Birrell 	 * together a bunch of object files specified in objv and then
16496ff6d951SJohn Birrell 	 * unlink(2) those object files.
16506ff6d951SJohn Birrell 	 */
16516ff6d951SJohn Birrell 	if (pgp == NULL) {
16526ff6d951SJohn Birrell 		const char *fmt = "%s -o %s -r";
16536ff6d951SJohn Birrell 
16546ff6d951SJohn Birrell 		len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1;
16556ff6d951SJohn Birrell 
16566ff6d951SJohn Birrell 		for (i = 0; i < objc; i++)
16576ff6d951SJohn Birrell 			len += strlen(objv[i]) + 1;
16586ff6d951SJohn Birrell 
16596ff6d951SJohn Birrell 		cmd = alloca(len);
16606ff6d951SJohn Birrell 
16616ff6d951SJohn Birrell 		cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file);
16626ff6d951SJohn Birrell 
16636ff6d951SJohn Birrell 		for (i = 0; i < objc; i++)
16646ff6d951SJohn Birrell 			cur += snprintf(cmd + cur, len - cur, " %s", objv[i]);
16656ff6d951SJohn Birrell 
16666ff6d951SJohn Birrell 		if ((status = system(cmd)) == -1) {
16676ff6d951SJohn Birrell 			return (dt_link_error(dtp, NULL, -1, NULL,
16686ff6d951SJohn Birrell 			    "failed to run %s: %s", dtp->dt_ld_path,
16696ff6d951SJohn Birrell 			    strerror(errno)));
16706ff6d951SJohn Birrell 		}
16716ff6d951SJohn Birrell 
16726ff6d951SJohn Birrell 		if (WIFSIGNALED(status)) {
16736ff6d951SJohn Birrell 			return (dt_link_error(dtp, NULL, -1, NULL,
16746ff6d951SJohn Birrell 			    "failed to link %s: %s failed due to signal %d",
16756ff6d951SJohn Birrell 			    file, dtp->dt_ld_path, WTERMSIG(status)));
16766ff6d951SJohn Birrell 		}
16776ff6d951SJohn Birrell 
16786ff6d951SJohn Birrell 		if (WEXITSTATUS(status) != 0) {
16796ff6d951SJohn Birrell 			return (dt_link_error(dtp, NULL, -1, NULL,
16806ff6d951SJohn Birrell 			    "failed to link %s: %s exited with status %d\n",
16816ff6d951SJohn Birrell 			    file, dtp->dt_ld_path, WEXITSTATUS(status)));
16826ff6d951SJohn Birrell 		}
16836ff6d951SJohn Birrell 
16846ff6d951SJohn Birrell 		for (i = 0; i < objc; i++) {
16856ff6d951SJohn Birrell 			if (strcmp(objv[i], file) != 0)
16866ff6d951SJohn Birrell 				(void) unlink(objv[i]);
16876ff6d951SJohn Birrell 		}
16886ff6d951SJohn Birrell 
16896ff6d951SJohn Birrell 		return (0);
16906ff6d951SJohn Birrell 	}
16916ff6d951SJohn Birrell 
16926ff6d951SJohn Birrell 	for (i = 0; i < objc; i++) {
16936ff6d951SJohn Birrell 		if (process_obj(dtp, objv[i], &eprobes) != 0)
16946ff6d951SJohn Birrell 			return (-1); /* errno is set for us */
16956ff6d951SJohn Birrell 	}
16966ff6d951SJohn Birrell 
16976ff6d951SJohn Birrell 	/*
16986ff6d951SJohn Birrell 	 * If there are is-enabled probes then we need to force use of DOF
16996ff6d951SJohn Birrell 	 * version 2.
17006ff6d951SJohn Birrell 	 */
17016ff6d951SJohn Birrell 	if (eprobes && pgp->dp_dofversion < DOF_VERSION_2)
17026ff6d951SJohn Birrell 		pgp->dp_dofversion = DOF_VERSION_2;
17036ff6d951SJohn Birrell 
17046ff6d951SJohn Birrell 	if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL)
17056ff6d951SJohn Birrell 		return (-1); /* errno is set for us */
17066ff6d951SJohn Birrell 
170733d84250SMark Johnston 	snprintf(tfile, sizeof(tfile), "%s.XXXXXX", file);
1708ed09cc1bSMark Johnston 	if ((fd = mkostemp(tfile, O_CLOEXEC)) == -1)
17092693feb4SJohn Birrell 		return (dt_link_error(dtp, NULL, -1, NULL,
171033d84250SMark Johnston 		    "failed to create temporary file %s: %s",
171133d84250SMark Johnston 		    tfile, strerror(errno)));
17126ff6d951SJohn Birrell 
17136ff6d951SJohn Birrell 	/*
17146ff6d951SJohn Birrell 	 * If -xlinktype=DOF has been selected, just write out the DOF.
17156ff6d951SJohn Birrell 	 * Otherwise proceed to the default of generating and linking ELF.
17166ff6d951SJohn Birrell 	 */
17176ff6d951SJohn Birrell 	switch (dtp->dt_linktype) {
17186ff6d951SJohn Birrell 	case DT_LTYP_DOF:
17196ff6d951SJohn Birrell 		if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz)
17206ff6d951SJohn Birrell 			ret = errno;
17216ff6d951SJohn Birrell 
17226ff6d951SJohn Birrell 		if (close(fd) != 0 && ret == 0)
17236ff6d951SJohn Birrell 			ret = errno;
17246ff6d951SJohn Birrell 
17256ff6d951SJohn Birrell 		if (ret != 0) {
17266ff6d951SJohn Birrell 			return (dt_link_error(dtp, NULL, -1, NULL,
17276ff6d951SJohn Birrell 			    "failed to write %s: %s", file, strerror(ret)));
17286ff6d951SJohn Birrell 		}
17296ff6d951SJohn Birrell 
17306ff6d951SJohn Birrell 		return (0);
17316ff6d951SJohn Birrell 
17326ff6d951SJohn Birrell 	case DT_LTYP_ELF:
17336ff6d951SJohn Birrell 		break; /* fall through to the rest of dtrace_program_link() */
17346ff6d951SJohn Birrell 
17356ff6d951SJohn Birrell 	default:
17366ff6d951SJohn Birrell 		return (dt_link_error(dtp, NULL, -1, NULL,
17376ff6d951SJohn Birrell 		    "invalid link type %u\n", dtp->dt_linktype));
17386ff6d951SJohn Birrell 	}
17396ff6d951SJohn Birrell 
17406ff6d951SJohn Birrell 
17416ff6d951SJohn Birrell 	if (dtp->dt_oflags & DTRACE_O_LP64)
17426ff6d951SJohn Birrell 		status = dump_elf64(dtp, dof, fd);
17436ff6d951SJohn Birrell 	else
17446ff6d951SJohn Birrell 		status = dump_elf32(dtp, dof, fd);
17456ff6d951SJohn Birrell 
17468caf8a8dSMark Johnston 	if (status != 0)
174733d84250SMark Johnston 		return (dt_link_error(dtp, NULL, -1, NULL,
17488caf8a8dSMark Johnston 		    "failed to write %s: %s", tfile,
17498caf8a8dSMark Johnston 		    strerror(dtrace_errno(dtp))));
17506ff6d951SJohn Birrell 
17516ff6d951SJohn Birrell 	if (!dtp->dt_lazyload) {
17528caf8a8dSMark Johnston 		const char *fmt = "%s -o %s -r %s %s";
17530fff3baaSMark Johnston 		dt_dirpath_t *dp = dt_list_next(&dtp->dt_lib_path);
17542693feb4SJohn Birrell 
17550fff3baaSMark Johnston 		(void) snprintf(drti, sizeof (drti), "%s/drti.o", dp->dir_path);
17562693feb4SJohn Birrell 
17572693feb4SJohn Birrell 		len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, tfile,
17582693feb4SJohn Birrell 		    drti) + 1;
17592693feb4SJohn Birrell 
17602693feb4SJohn Birrell 		cmd = alloca(len);
17612693feb4SJohn Birrell 
17628caf8a8dSMark Johnston 		(void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, tfile,
17630f2bd1e8SRui Paulo 		    drti);
17646ff6d951SJohn Birrell 		if ((status = system(cmd)) == -1) {
176503a5f9f0SMark Johnston 			ret = dt_link_error(dtp, NULL, fd, NULL,
17666ff6d951SJohn Birrell 			    "failed to run %s: %s", dtp->dt_ld_path,
17676ff6d951SJohn Birrell 			    strerror(errno));
17686ff6d951SJohn Birrell 			goto done;
17696ff6d951SJohn Birrell 		}
17706ff6d951SJohn Birrell 
17716ff6d951SJohn Birrell 		if (WIFSIGNALED(status)) {
177203a5f9f0SMark Johnston 			ret = dt_link_error(dtp, NULL, fd, NULL,
17736ff6d951SJohn Birrell 			    "failed to link %s: %s failed due to signal %d",
17746ff6d951SJohn Birrell 			    file, dtp->dt_ld_path, WTERMSIG(status));
17756ff6d951SJohn Birrell 			goto done;
17766ff6d951SJohn Birrell 		}
17776ff6d951SJohn Birrell 
17786ff6d951SJohn Birrell 		if (WEXITSTATUS(status) != 0) {
177903a5f9f0SMark Johnston 			ret = dt_link_error(dtp, NULL, fd, NULL,
17806ff6d951SJohn Birrell 			    "failed to link %s: %s exited with status %d\n",
17816ff6d951SJohn Birrell 			    file, dtp->dt_ld_path, WEXITSTATUS(status));
17826ff6d951SJohn Birrell 			goto done;
17836ff6d951SJohn Birrell 		}
17840f2bd1e8SRui Paulo 		(void) close(fd); /* release temporary file */
178503a5f9f0SMark Johnston 
178603a5f9f0SMark Johnston 		/*
178703a5f9f0SMark Johnston 		 * Now that we've linked drti.o, reduce the global __SUNW_dof
178803a5f9f0SMark Johnston 		 * symbol to a local symbol. This is needed to so that multiple
178903a5f9f0SMark Johnston 		 * generated object files (for different providers, for
179003a5f9f0SMark Johnston 		 * instance) can be linked together. This is accomplished using
179103a5f9f0SMark Johnston 		 * the -Blocal flag with Sun's linker, but GNU ld doesn't appear
179203a5f9f0SMark Johnston 		 * to have an equivalent option.
179303a5f9f0SMark Johnston 		 */
179403a5f9f0SMark Johnston 		asprintf(&cmd, "%s --localize-hidden %s", dtp->dt_objcopy_path,
179503a5f9f0SMark Johnston 		    file);
179603a5f9f0SMark Johnston 		if ((status = system(cmd)) == -1) {
179703a5f9f0SMark Johnston 			ret = dt_link_error(dtp, NULL, -1, NULL,
179803a5f9f0SMark Johnston 			    "failed to run %s: %s", dtp->dt_objcopy_path,
179903a5f9f0SMark Johnston 			    strerror(errno));
180003a5f9f0SMark Johnston 			free(cmd);
180103a5f9f0SMark Johnston 			goto done;
180203a5f9f0SMark Johnston 		}
180303a5f9f0SMark Johnston 		free(cmd);
180403a5f9f0SMark Johnston 
180503a5f9f0SMark Johnston 		if (WIFSIGNALED(status)) {
180603a5f9f0SMark Johnston 			ret = dt_link_error(dtp, NULL, -1, NULL,
180703a5f9f0SMark Johnston 			    "failed to link %s: %s failed due to signal %d",
180803a5f9f0SMark Johnston 			    file, dtp->dt_objcopy_path, WTERMSIG(status));
180903a5f9f0SMark Johnston 			goto done;
181003a5f9f0SMark Johnston 		}
181103a5f9f0SMark Johnston 
181203a5f9f0SMark Johnston 		if (WEXITSTATUS(status) != 0) {
181303a5f9f0SMark Johnston 			ret = dt_link_error(dtp, NULL, -1, NULL,
181403a5f9f0SMark Johnston 			    "failed to link %s: %s exited with status %d\n",
181503a5f9f0SMark Johnston 			    file, dtp->dt_objcopy_path, WEXITSTATUS(status));
181603a5f9f0SMark Johnston 			goto done;
181703a5f9f0SMark Johnston 		}
18186ff6d951SJohn Birrell 	} else {
1819ed09cc1bSMark Johnston 		if (rename(tfile, file) != 0) {
1820ed09cc1bSMark Johnston 			ret = dt_link_error(dtp, NULL, fd, NULL,
1821ed09cc1bSMark Johnston 			    "failed to rename %s to %s: %s", tfile, file,
1822ed09cc1bSMark Johnston 			    strerror(errno));
1823ed09cc1bSMark Johnston 			goto done;
1824ed09cc1bSMark Johnston 		}
18256ff6d951SJohn Birrell 		(void) close(fd);
18266ff6d951SJohn Birrell 	}
18276ff6d951SJohn Birrell 
18286ff6d951SJohn Birrell done:
18296ff6d951SJohn Birrell 	dtrace_dof_destroy(dtp, dof);
18302693feb4SJohn Birrell 
1831ed09cc1bSMark Johnston 	if (!dtp->dt_lazyload)
1832ed09cc1bSMark Johnston 		(void) unlink(tfile);
18336ff6d951SJohn Birrell 	return (ret);
18346ff6d951SJohn Birrell }
1835