14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER START
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The contents of this file are subject to the terms of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Common Development and Distribution License (the "License").
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You may not use this file except in compliance with the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * or http://www.opensolaris.org/os/licensing.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * See the License for the specific language governing permissions
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and limitations under the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * When distributing Covered Code, include this CDDL HEADER in each
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If applicable, add the following below this CDDL HEADER, with the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * fields enclosed by brackets "[]" replaced with your own identifying
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * information: Portions Copyright [yyyy] [name of copyright owner]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER END
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Use is subject to license terms.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma ident "%Z%%M% %I% %E% SMI"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ELF_TARGET_ALL
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <elf.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <sys/types.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <sys/sysmacros.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <unistd.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <strings.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <alloca.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <limits.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <stddef.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <stdlib.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <stdio.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <fcntl.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <errno.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <wait.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <assert.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <sys/ipc.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_impl.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_provider.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_program.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#include <dt_string.h>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ESHDR_NULL 0
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ESHDR_SHSTRTAB 1
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ESHDR_DOF 2
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ESHDR_STRTAB 3
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ESHDR_SYMTAB 4
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ESHDR_REL 5
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define ESHDR_NUM 6
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define PWRITE_SCN(index, data) \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (lseek64(fd, (off64_t)elf_file.shdr[(index)].sh_offset, SEEK_SET) != \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (off64_t)elf_file.shdr[(index)].sh_offset || \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_write(dtp, fd, (data), elf_file.shdr[(index)].sh_size) != \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.shdr[(index)].sh_size)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic const char DTRACE_SHSTRTAB32[] = "\0"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".shstrtab\0" /* 1 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".SUNW_dof\0" /* 11 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".strtab\0" /* 21 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".symtab\0" /* 29 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#ifdef __sparc
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".rela.SUNW_dof"; /* 37 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".rel.SUNW_dof"; /* 37 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic const char DTRACE_SHSTRTAB64[] = "\0"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".shstrtab\0" /* 1 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".SUNW_dof\0" /* 11 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".strtab\0" /* 21 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".symtab\0" /* 29 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync".rela.SUNW_dof"; /* 37 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic const char DOFSTR[] = "__SUNW_dof";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic const char DOFLAZYSTR[] = "___SUNW_dof";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynctypedef struct dt_link_pair {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync struct dt_link_pair *dlp_next; /* next pair in linked list */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync void *dlp_str; /* buffer for string table */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync void *dlp_sym; /* buffer for symbol table */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync} dt_link_pair_t;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynctypedef struct dof_elf32 {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_nrel; /* relocation count */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#ifdef __sparc
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Rela *de_rel; /* array of relocations for sparc */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Rel *de_rel; /* array of relocations for x86 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_nsym; /* symbol count */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Sym *de_sym; /* array of symbols */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_strlen; /* size of of string table */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char *de_strtab; /* string table */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_global; /* index of the first global symbol */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync} dof_elf32_t;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprepare_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf32_t *dep)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_sec_t *dofs, *s;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_relohdr_t *dofrh;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_relodesc_t *dofr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char *strtab;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int i, j, nrel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync size_t strtabsz = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t count = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync size_t base;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Sym *sym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#ifdef __sparc
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Rela *rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Rel *rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * First compute the size of the string table and the number of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * relocations present in the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < dof->dofh_secnum; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dofs[i].dofs_type != DOF_SECT_URELHDR)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_strtab];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtab = (char *)dof + s->dofs_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(strtab[0] == '\0');
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += s->dofs_size - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_relsec];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync count += s->dofs_size / s->dofs_entsize;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strlen = strtabsz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nrel = count;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym = count + 1; /* the first symbol is always null */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dtp->dt_lazyload) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strlen += sizeof (DOFLAZYSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strlen += sizeof (DOFSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_rel = calloc(dep->de_nrel,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (dep->de_rel[0]))) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_set_errno(dtp, EDT_NOMEM));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf32_Sym))) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(dep->de_rel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_set_errno(dtp, EDT_NOMEM));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(dep->de_rel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(dep->de_sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_set_errno(dtp, EDT_NOMEM));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync count = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strtab[0] = '\0';
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel = dep->de_rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym = dep->de_sym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_global = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The first symbol table entry must be zeroed and is always ignored.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bzero(sym, sizeof (Elf32_Sym));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Take a second pass through the DOF sections filling in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * memory we allocated.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < dof->dofh_secnum; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dofs[i].dofs_type != DOF_SECT_URELHDR)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_strtab];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtab = (char *)dof + s->dofs_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync base = strtabsz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += s->dofs_size - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_relsec];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nrel = s->dofs_size / s->dofs_entsize;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_tgtsec];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (j = 0; j < nrel; j++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(__i386) || defined(__amd64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_offset = s->dofs_offset +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr[j].dofr_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_info = ELF32_R_INFO(count + dep->de_global,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync R_386_32);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(__sparc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Add 4 bytes to hit the low half of this 64-bit
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * big-endian address.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_offset = s->dofs_offset +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr[j].dofr_offset + 4;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_info = ELF32_R_INFO(count + dep->de_global,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync R_SPARC_32);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#error unknown ISA
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_name = base + dofr[j].dofr_name - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_value = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_size = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_other = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_shndx = SHN_UNDEF;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync count++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Add a symbol for the DOF itself. We use a different symbol for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * lazily and actively loaded DOF to make them easy to distinguish.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_name = strtabsz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_value = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_size = dof->dofh_filesz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_other = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_shndx = ESHDR_DOF;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dtp->dt_lazyload) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (DOFLAZYSTR));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += sizeof (DOFLAZYSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += sizeof (DOFSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(count == dep->de_nrel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(strtabsz == dep->de_strlen);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynctypedef struct dof_elf64 {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_nrel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Rela *de_rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_nsym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Sym *de_sym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_strlen;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char *de_strtab;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_global;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync} dof_elf64_t;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_sec_t *dofs, *s;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_relohdr_t *dofrh;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_relodesc_t *dofr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char *strtab;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int i, j, nrel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync size_t strtabsz = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t count = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync size_t base;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Sym *sym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Rela *rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * First compute the size of the string table and the number of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * relocations present in the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < dof->dofh_secnum; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dofs[i].dofs_type != DOF_SECT_URELHDR)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_strtab];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtab = (char *)dof + s->dofs_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(strtab[0] == '\0');
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += s->dofs_size - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_relsec];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync count += s->dofs_size / s->dofs_entsize;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strlen = strtabsz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nrel = count;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym = count + 1; /* the first symbol is always null */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dtp->dt_lazyload) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strlen += sizeof (DOFLAZYSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strlen += sizeof (DOFSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_rel = calloc(dep->de_nrel,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (dep->de_rel[0]))) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_set_errno(dtp, EDT_NOMEM));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(dep->de_rel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_set_errno(dtp, EDT_NOMEM));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(dep->de_rel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(dep->de_sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_set_errno(dtp, EDT_NOMEM));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync count = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_strtab[0] = '\0';
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel = dep->de_rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym = dep->de_sym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_global = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The first symbol table entry must be zeroed and is always ignored.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bzero(sym, sizeof (Elf64_Sym));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Take a second pass through the DOF sections filling in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * memory we allocated.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < dof->dofh_secnum; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dofs[i].dofs_type != DOF_SECT_URELHDR)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_strtab];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtab = (char *)dof + s->dofs_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync base = strtabsz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += s->dofs_size - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_relsec];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nrel = s->dofs_size / s->dofs_entsize;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = &dofs[dofrh->dofr_tgtsec];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (j = 0; j < nrel; j++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(__i386) || defined(__amd64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_offset = s->dofs_offset +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr[j].dofr_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_info = ELF64_R_INFO(count + dep->de_global,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync R_AMD64_64);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(__sparc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_offset = s->dofs_offset +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr[j].dofr_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel->r_info = ELF64_R_INFO(count + dep->de_global,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync R_SPARC_64);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#error unknown ISA
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_name = base + dofr[j].dofr_name - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_value = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_size = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_other = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_shndx = SHN_UNDEF;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rel++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync count++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Add a symbol for the DOF itself. We use a different symbol for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * lazily and actively loaded DOF to make them easy to distinguish.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_name = strtabsz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_value = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_size = dof->dofh_filesz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_other = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_shndx = ESHDR_DOF;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dtp->dt_lazyload) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (DOFLAZYSTR));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += sizeof (DOFLAZYSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtabsz += sizeof (DOFSTR);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(count == dep->de_nrel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(strtabsz == dep->de_strlen);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Write out an ELF32 file prologue consisting of a header, section headers,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and a section header string table. The DOF data will follow this prologue
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and complete the contents of the given ELF file.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync struct {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Ehdr ehdr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Shdr shdr[ESHDR_NUM];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } elf_file;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Shdr *shp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Off off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_elf32_t de;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int ret = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint_t nshdr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (prepare_elf32(dtp, dof, &de) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1); /* errno is set for us */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If there are no relocations, we only need enough sections for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the shstrtab and the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bzero(&elf_file, sizeof (elf_file));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS32;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(_BIG_ENDIAN)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(_LITTLE_ENDIAN)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_type = ET_REL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(__sparc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_machine = EM_SPARC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(__i386) || defined(__amd64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_machine = EM_386;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_version = EV_CURRENT;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shoff = sizeof (Elf32_Ehdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ehsize = sizeof (Elf32_Ehdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_phentsize = sizeof (Elf32_Phdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shentsize = sizeof (Elf32_Shdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shnum = nshdr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_SHSTRTAB];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_STRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = sizeof (DTRACE_SHSTRTAB32);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = sizeof (char);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_DOF];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_SUNW_dof;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = dof->dofh_filesz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = 8;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = shp->sh_offset + shp->sh_size;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_STRTAB];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_STRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = de.de_strlen;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = sizeof (char);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_SYMTAB];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_SYMTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_entsize = sizeof (Elf32_Sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_link = ESHDR_STRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_info = de.de_global;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = de.de_nsym * sizeof (Elf32_Sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = 4;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (de.de_nrel == 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_write(dtp, fd, &elf_file,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (elf_file)) != sizeof (elf_file) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_DOF, dof)) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = dt_set_errno(dtp, errno);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_REL];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#ifdef __sparc
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_RELA;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_REL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_entsize = sizeof (de.de_rel[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_link = ESHDR_SYMTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_info = ESHDR_DOF;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = 4;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_write(dtp, fd, &elf_file,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (elf_file)) != sizeof (elf_file) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_REL, de.de_rel) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_DOF, dof)) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = dt_set_errno(dtp, errno);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(de.de_strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(de.de_sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(de.de_rel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (ret);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Write out an ELF64 file prologue consisting of a header, section headers,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and a section header string table. The DOF data will follow this prologue
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and complete the contents of the given ELF file.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync struct {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Ehdr ehdr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Shdr shdr[ESHDR_NUM];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } elf_file;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Shdr *shp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf64_Off off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_elf64_t de;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int ret = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint_t nshdr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (prepare_elf64(dtp, dof, &de) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1); /* errno is set for us */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If there are no relocations, we only need enough sections for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the shstrtab and the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bzero(&elf_file, sizeof (elf_file));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS64;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(_BIG_ENDIAN)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(_LITTLE_ENDIAN)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_type = ET_REL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(__sparc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_machine = EM_SPARCV9;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(__i386) || defined(__amd64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_machine = EM_AMD64;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_version = EV_CURRENT;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shoff = sizeof (Elf64_Ehdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_ehsize = sizeof (Elf64_Ehdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_phentsize = sizeof (Elf64_Phdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shentsize = sizeof (Elf64_Shdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shnum = nshdr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_SHSTRTAB];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_STRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = sizeof (DTRACE_SHSTRTAB64);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = sizeof (char);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_DOF];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_SUNW_dof;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = dof->dofh_filesz;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = 8;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = shp->sh_offset + shp->sh_size;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_STRTAB];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_STRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = de.de_strlen;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = sizeof (char);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_SYMTAB];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_SYMTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_entsize = sizeof (Elf64_Sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_link = ESHDR_STRTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_info = de.de_global;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = de.de_nsym * sizeof (Elf64_Sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = 8;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (de.de_nrel == 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_write(dtp, fd, &elf_file,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (elf_file)) != sizeof (elf_file) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_DOF, dof)) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = dt_set_errno(dtp, errno);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp = &elf_file.shdr[ESHDR_REL];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_flags = SHF_ALLOC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_type = SHT_RELA;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_entsize = sizeof (de.de_rel[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_link = ESHDR_SYMTAB;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_info = ESHDR_DOF;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_offset = off;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_addralign = 8;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_write(dtp, fd, &elf_file,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (elf_file)) != sizeof (elf_file) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_REL, de.de_rel) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync PWRITE_SCN(ESHDR_DOF, dof)) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = dt_set_errno(dtp, errno);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(de.de_strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(de.de_sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync free(de.de_rel);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (ret);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_symtab_lookup(Elf_Data *data_sym, int nsym, uintptr_t addr, uint_t shn,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Sym *sym)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int i, ret = -1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Sym s;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < nsym && gelf_getsym(data_sym, i, sym) != NULL; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_ST_TYPE(sym->st_info) == STT_FUNC &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shn == sym->st_shndx &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_value <= addr &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync addr < sym->st_value + sym->st_size) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_ST_BIND(sym->st_info) == STB_GLOBAL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = *sym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ret == 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *sym = s;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (ret);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(__sparc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_RET 0x81c7e008
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_NOP 0x01000000
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_CALL 0x40000000
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_CLR_O0 0x90102000
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_IS_MOV_O7(inst) (((inst) & 0xffffe000) == 0x9e100000)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_IS_RESTORE(inst) (((inst) & 0xc1f80000) == 0x81e80000)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_IS_RETL(inst) (((inst) & 0xfff83fff) == 0x81c02008)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_RS2(inst) ((inst) & 0x1f)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_MAKE_RETL(reg) (0x81c02008 | ((reg) << 14))
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*ARGSUSED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t *off)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t *ip;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*LINTED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip = (uint32_t *)(p + rela->r_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We only know about some specific relocation types.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_R_TYPE(rela->r_info) != R_SPARC_WDISP30 &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GELF_R_TYPE(rela->r_info) != R_SPARC_WPLT30)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We may have already processed this object file in an earlier linker
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * invocation. Check to see if the present instruction sequence matches
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the one we would install below.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (isenabled) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] == DT_OP_NOP) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += sizeof (ip[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (DT_IS_RESTORE(ip[1])) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] == DT_OP_RET) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += sizeof (ip[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else if (DT_IS_MOV_O7(ip[1])) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (DT_IS_RETL(ip[0]))
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] == DT_OP_NOP) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += sizeof (ip[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We only expect call instructions with a displacement of 0.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] != DT_OP_CALL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("found %x instead of a call instruction at %llx\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0], (u_longlong_t)rela->r_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (isenabled) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * It would necessarily indicate incorrect usage if an is-
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * enabled probe were tail-called so flag that as an error.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * It's also potentially (very) tricky to handle gracefully,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * but could be done if this were a desired use scenario.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (DT_IS_RESTORE(ip[1]) || DT_IS_MOV_O7(ip[1])) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("tail call to is-enabled probe at %llx\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (u_longlong_t)rela->r_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * On SPARC, we take advantage of the fact that the first
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * argument shares the same register as for the return value.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The macro handles the work of zeroing that register so we
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * don't need to do anything special here. We instrument the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * instruction in the delay slot as we'll need to modify the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * return register after that instruction has been emulated.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += sizeof (ip[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If the call is followed by a restore, it's a tail call so
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * change the call to a ret. If the call if followed by a mov
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * of a register into %o7, it's a tail call in leaf context
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * so change the call to a retl-like instruction that returns
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * to that register value + 8 (rather than the typical %o7 +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * 8); the delay slot instruction is left, but should have no
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * effect. Otherwise we change the call to be a nop. We
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * identify the subsequent instruction as the probe point in
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * all but the leaf tail-call case to ensure that arguments to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the probe are complete and consistent. An astute, though
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * largely hypothetical, observer would note that there is the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * possibility of a false-positive probe firing if the function
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * contained a branch to the instruction in the delay slot of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the call. Fixing this would require significant in-kernel
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * modifications, and isn't worth doing until we see it in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * wild.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (DT_IS_RESTORE(ip[1])) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0] = DT_OP_RET;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += sizeof (ip[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else if (DT_IS_MOV_O7(ip[1])) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0] = DT_MAKE_RETL(DT_RS2(ip[1]));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += sizeof (ip[0]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(__i386) || defined(__amd64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_NOP 0x90
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_RET 0xc3
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_CALL 0xe8
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_JMP32 0xe9
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_REX_RAX 0x48
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_XOR_EAX_0 0x33
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_OP_XOR_EAX_1 0xc0
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t *off)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint8_t ret;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * On x86, the first byte of the instruction is the call opcode and
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the next four bytes are the 32-bit address; the relocation is for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the address operand. We back up the offset to the first byte of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the instruction. For is-enabled probes, we later advance the offset
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * so that it hits the first nop in the instruction sequence.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) -= 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We only know about some specific relocation types. Luckily
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * these types have the same values on both 32-bit and 64-bit
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * x86 architectures.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_R_TYPE(rela->r_info) != R_386_PC32 &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GELF_R_TYPE(rela->r_info) != R_386_PLT32)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We may have already processed this object file in an earlier linker
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * invocation. Check to see if the present instruction sequence matches
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the one we would install. For is-enabled probes, we advance the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * offset to the first nop instruction in the sequence to match the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * text modification code below.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (!isenabled) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET) &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else if (dtp->dt_oflags & DTRACE_O_LP64) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] == DT_OP_REX_RAX &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (ip[3] == DT_OP_NOP || ip[3] == DT_OP_RET) &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[4] == DT_OP_NOP) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += 3;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (ip[2] == DT_OP_NOP || ip[2] == DT_OP_RET) &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += 2;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We expect either a call instrution with a 32-bit displacement or a
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * jmp instruction with a 32-bit displacement acting as a tail-call.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] != DT_OP_CALL && ip[0] != DT_OP_JMP32) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("found %x instead of a call or jmp instruction at "
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "%llx\n", ip[0], (u_longlong_t)rela->r_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Establish the instruction sequence -- all nops for probes, and an
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * instruction to clear the return value register (%eax/%rax) followed
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * by nops for is-enabled probes. For is-enabled probes, we advance
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the offset to the first nop. This isn't stricly necessary but makes
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * for more readable disassembly when the probe is enabled.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (!isenabled) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0] = ret;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[1] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[2] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[3] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[4] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else if (dtp->dt_oflags & DTRACE_O_LP64) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0] = DT_OP_REX_RAX;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[1] = DT_OP_XOR_EAX_0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[2] = DT_OP_XOR_EAX_1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[3] = ret;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[4] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += 3;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[0] = DT_OP_XOR_EAX_0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[1] = DT_OP_XOR_EAX_1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[2] = ret;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[3] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[4] = DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (*off) += 2;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#error unknown ISA
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*PRINTFLIKE5*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync const char *format, ...)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync va_list ap;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_link_pair_t *pair;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync va_start(ap, format);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync va_end(ap);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (elf != NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_end(elf);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (fd >= 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) close(fd);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync while ((pair = bufs) != NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bufs = pair->dlp_next;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair->dlp_str);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair->dlp_sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_set_errno(dtp, EDT_COMPILER));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncstatic int
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprocess_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync static const char dt_prefix[] = "__dtrace";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync static const char dt_enabled[] = "enabled";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync static const char dt_symprefix[] = "$dtrace";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync static const char dt_symfmt[] = "%s%d.%s";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int fd, i, ndx, eprobe, mod = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf *elf = NULL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Ehdr ehdr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf_Data *data_rel, *data_sym, *data_str, *data_tgt;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Sym rsym, fsym, dsym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Rela rela;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char *s, *p, *r;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char pname[DTRACE_PROVNAMELEN];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_provider_t *pvp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_probe_t *prp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t off, eclass, emachine1, emachine2;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync size_t symsize, nsym, isym, istr, len;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync key_t objkey;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_link_pair_t *pair, *bufs = NULL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_t *strtab;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((fd = open64(obj, O_RDWR)) == -1) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to open %s: %s", obj, strerror(errno)));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to process %s: %s", obj, elf_errmsg(elf_errno())));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync switch (elf_kind(elf)) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case ELF_K_ELF:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync break;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case ELF_K_AR:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs, "archives are not "
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "permitted; use the contents of the archive instead: %s",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync default:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "invalid file type: %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getehdr(elf, &ehdr) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dtp->dt_oflags & DTRACE_O_LP64) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync eclass = ELFCLASS64;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(__sparc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync emachine1 = emachine2 = EM_SPARCV9;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(__i386) || defined(__amd64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync emachine1 = emachine2 = EM_AMD64;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync symsize = sizeof (Elf64_Sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync eclass = ELFCLASS32;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#if defined(__sparc)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync emachine1 = EM_SPARC;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync emachine2 = EM_SPARC32PLUS;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#elif defined(__i386) || defined(__amd64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync emachine1 = emachine2 = EM_386;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#endif
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync symsize = sizeof (Elf32_Sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ehdr.e_ident[EI_CLASS] != eclass) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "incorrect ELF class for object file: %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "incorrect ELF machine type for object file: %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We use this token as a relatively unique handle for this file on the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * system in order to disambiguate potential conflicts between files of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the same name which contain identially named local symbols.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((objkey = ftok(obj, 0)) == (key_t)-1) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to generate unique key for object file: %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync scn_rel = NULL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getshdr(scn_rel, &shdr_rel) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Skip any non-relocation sections.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Grab the section, section header and section data for the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * symbol table that this relocation section references.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync gelf_getshdr(scn_sym, &shdr_sym) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (data_sym = elf_getdata(scn_sym, NULL)) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Ditto for that symbol table's string table.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync gelf_getshdr(scn_str, &shdr_str) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (data_str = elf_getdata(scn_str, NULL)) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Grab the section, section header and section data for the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * target section for the relocations. For the relocations
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * we're looking for -- this will typically be the text of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * object file.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync gelf_getshdr(scn_tgt, &shdr_tgt) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We're looking for relocations to symbols matching this form:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * __dtrace[enabled]_<prov>___<probe>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * For the generated object, we need to record the location
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * identified by the relocation, and create a new relocation
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * in the generated object that will be resolved at link time
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * to the location of the function in which the probe is
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * embedded. In the target object, we change the matched symbol
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * so that it will be ignored at link time, and we modify the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * target (text) section to replace the call instruction with
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * one or more nops.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If the function containing the probe is locally scoped
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * (static), we create an alias used by the relocation in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * generated object. The alias, a new symbol, will be global
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * (so that the relocation from the generated object can be
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * resolved), and hidden (so that it is converted to a local
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * symbol at link time). Such aliases have this form:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * $dtrace<key>.<function>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We take a first pass through all the relocations to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * populate our string table and count the number of extra
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * symbols we'll require.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strtab = dt_strtab_create(1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nsym = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync isym = data_sym->d_size / symsize;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync istr = data_str->d_size;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (shdr_rel.sh_type == SHT_RELA) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getrela(data_rel, i, &rela) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Rel rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getrel(data_rel, i, &rel) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rela.r_offset = rel.r_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rela.r_info = rel.r_info;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rela.r_addend = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info),
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync &rsym) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = (char *)data_str->d_buf + rsym.st_name;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shdr_rel.sh_info, &fsym) != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_ST_BIND(fsym.st_info) != STB_LOCAL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (fsym.st_name > data_str->d_size) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = (char *)data_str->d_buf + fsym.st_name;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If this symbol isn't of type function, we've really
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * driven off the rails or the object file is corrupt.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "expected %s to be of type function", s));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len = snprintf(NULL, 0, dt_symfmt, dt_symprefix,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync objkey, s) + 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((p = dt_alloc(dtp, len)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) snprintf(p, len, dt_symfmt, dt_symprefix,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync objkey, s);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_strtab_index(strtab, p) == -1) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nsym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) dt_strtab_insert(strtab, p);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, p);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If needed, allocate the additional space for the symbol
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * table and string table copying the old data into the new
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * buffers, and marking the buffers as dirty. We inject those
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * newly allocated buffers into the libelf data structures, but
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * are still responsible for freeing them once we're done with
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the elf handle.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (nsym > 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The first byte of the string table is reserved for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the \0 entry.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len = dt_strtab_size(strtab) - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(len > 0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(dt_strtab_index(strtab, "") == 0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nsym * symsize)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair->dlp_str);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pair->dlp_next = bufs;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bufs = pair;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync data_str->d_buf = pair->dlp_str;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync data_str->d_size += len;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shdr_str.sh_size += len;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) gelf_update_shdr(scn_str, &shdr_str);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync data_sym->d_buf = pair->dlp_sym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync data_sym->d_size += nsym * symsize;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shdr_sym.sh_size += nsym * symsize;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) gelf_update_shdr(scn_sym, &shdr_sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nsym += isym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_strtab_destroy(strtab);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Now that the tables have been allocated, perform the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * modifications described above.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (shdr_rel.sh_type == SHT_RELA) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getrela(data_rel, i, &rela) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync GElf_Rel rel;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getrel(data_rel, i, &rel) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rela.r_offset = rel.r_offset;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rela.r_info = rel.r_info;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rela.r_addend = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ndx = GELF_R_SYM(rela.r_info);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (gelf_getsym(data_sym, ndx, &rsym) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rsym.st_name > data_str->d_size)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = (char *)data_str->d_buf + rsym.st_name;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync continue;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s += sizeof (dt_prefix) - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Check to see if this is an 'is-enabled' check as
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * opposed to a normal probe.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (strncmp(s, dt_enabled,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sizeof (dt_enabled) - 1) == 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s += sizeof (dt_enabled) - 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync eprobe = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync *eprobesp = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("is-enabled probe\n");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync eprobe = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("normal probe\n");
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (*s++ != '_')
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((p = strstr(s, "___")) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync p - s >= sizeof (pname))
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(s, pname, p - s);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pname[p - s] = '\0';
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync p = strhyphenate(p + 3); /* strlen("___") */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shdr_rel.sh_info, &fsym) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (fsym.st_name > data_str->d_size)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(GELF_ST_TYPE(fsym.st_info) == STT_FUNC);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If a NULL relocation name is passed to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * dt_probe_define(), the function name is used for the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * relocation. The relocation needs to use a mangled
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * name if the symbol is locally scoped; the function
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * name may need to change if we've found the global
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * alias for the locally scoped symbol (we prefer
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * global symbols to locals in dt_symtab_lookup()).
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s = (char *)data_str->d_buf + fsym.st_name;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync r = NULL;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_ST_BIND(fsym.st_info) == STB_LOCAL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dsym = fsym;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dsym.st_name = istr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dsym.st_info = GELF_ST_INFO(STB_GLOBAL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync STT_FUNC);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dsym.st_other =
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ELF64_ST_VISIBILITY(STV_ELIMINATE);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) gelf_update_sym(data_sym, isym, &dsym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync r = (char *)data_str->d_buf + istr;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync istr += 1 + sprintf(r, dt_symfmt,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_symprefix, objkey, s);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync isym++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(isym <= nsym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else if (strncmp(s, dt_symprefix,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strlen(dt_symprefix)) == 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync r = s;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((s = strchr(s, '.')) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync s++;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "no such provider %s", pname));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((prp = dt_probe_lookup(pvp, p)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "no such probe %s", p));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync assert(fsym.st_value <= rela.r_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = rela.r_offset - fsym.st_value;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_modtext(dtp, data_tgt->d_buf, eprobe,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync &rela, &off) != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to allocate space for probe"));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync mod = 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * This symbol may already have been marked to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * be ignored by another relocation referencing
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the same symbol or if this object file has
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * already been processed by an earlier link
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * invocation.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (rsym.st_shndx != SHN_SUNW_IGNORE) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync rsym.st_shndx = SHN_SUNW_IGNORE;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) gelf_update_sym(data_sym, ndx, &rsym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (mod && elf_update(elf, ELF_C_WRITE) == -1)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto err;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_end(elf);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) close(fd);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync while ((pair = bufs) != NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bufs = pair->dlp_next;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair->dlp_str);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair->dlp_sym);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_free(dtp, pair);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncerr:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "an error was encountered while processing %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncint
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync const char *file, int objc, char *const objv[])
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync{
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char drti[PATH_MAX];
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dof_hdr_t *dof;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int fd, status, i, cur;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char *cmd, tmp;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync size_t len;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync int eprobes = 0, ret = 0;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * A NULL program indicates a special use in which we just link
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * together a bunch of object files specified in objv and then
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * unlink(2) those object files.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (pgp == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync const char *fmt = "%s -o %s -r";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len += strlen(objv[i]) + 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cmd = alloca(len);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cur += snprintf(cmd + cur, len - cur, " %s", objv[i]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((status = system(cmd)) == -1) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to run %s: %s", dtp->dt_ld_path,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strerror(errno)));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (WIFSIGNALED(status)) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s failed due to signal %d",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync file, dtp->dt_ld_path, WTERMSIG(status)));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (WEXITSTATUS(status) != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s exited with status %d\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync file, dtp->dt_ld_path, WEXITSTATUS(status)));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (strcmp(objv[i], file) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) unlink(objv[i]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (process_obj(dtp, objv[i], &eprobes) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1); /* errno is set for us */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If there are is-enabled probes then we need to force use of DOF
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * version 2.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (eprobes && pgp->dp_dofversion < DOF_VERSION_2)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync pgp->dp_dofversion = DOF_VERSION_2;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1); /* errno is set for us */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Create a temporary file and then unlink it if we're going to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * combine it with drti.o later. We can still refer to it in child
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * processes as /dev/fd/<fd>.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to open %s: %s", file, strerror(errno)));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync /*
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If -xlinktype=DOF has been selected, just write out the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Otherwise proceed to the default of generating and linking ELF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync switch (dtp->dt_linktype) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case DT_LTYP_DOF:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = errno;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (close(fd) != 0 && ret == 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = errno;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ret != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to write %s: %s", file, strerror(ret)));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case DT_LTYP_ELF:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync break; /* fall through to the rest of dtrace_program_link() */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync default:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "invalid link type %u\n", dtp->dt_linktype));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (!dtp->dt_lazyload)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) unlink(file);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dtp->dt_oflags & DTRACE_O_LP64)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync status = dump_elf64(dtp, dof, fd);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync else
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync status = dump_elf32(dtp, dof, fd);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (status != 0 || lseek(fd, 0, SEEK_SET) != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to write %s: %s", file, strerror(errno)));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (!dtp->dt_lazyload) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dtp->dt_oflags & DTRACE_O_LP64) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) snprintf(drti, sizeof (drti),
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "%s/64/drti.o", _dtrace_libdir);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) snprintf(drti, sizeof (drti),
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "%s/drti.o", _dtrace_libdir);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync drti) + 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cmd = alloca(len);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((status = system(cmd)) == -1) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to run %s: %s", dtp->dt_ld_path,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync strerror(errno));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto done;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) close(fd); /* release temporary file */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (WIFSIGNALED(status)) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s failed due to signal %d",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync file, dtp->dt_ld_path, WTERMSIG(status));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto done;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (WEXITSTATUS(status) != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = dt_link_error(dtp, NULL, -1, NULL,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s exited with status %d\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync file, dtp->dt_ld_path, WEXITSTATUS(status));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync goto done;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync } else {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) close(fd);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdone:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dtrace_dof_destroy(dtp, dof);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (ret);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}