freebsd-nq/lib/libdtrace/common/dt_link.c
Rui Paulo e0ea83ebb1 Update DTrace userland code to the latest available.
Summary of changes:

- libdtrace

changeset:   12902:3bb859a7330c
user:        Bryan Cantrill <Bryan.Cantrill@Sun.COM>
date:        Fri Jul 23 17:34:02 2010 -0700
summary:     6679140 asymmetric alloc/dealloc activity can induce dynamic variable drops

changeset:   12692:4341b447c069
user:        Ali Bahrami <Ali.Bahrami@Oracle.COM>
date:        Thu Jun 24 18:16:42 2010 -0600
summary:     6916796 OSnet mapfiles should use version 2 link-editor syntax

changeset:   12507:501806a754d2
user:        Alan Maguire <Alan.Maguire@Sun.COM>
date:        Thu May 27 17:29:51 2010 -0400
summary:     PSARC 2010/106 DTrace TCP and UDP providers

changeset:   11798:1e7f1f154004
user:        Roger A. Faulkner <Roger.Faulkner@Sun.COM>
date:        Sun Feb 28 18:42:20 2010 -0800
summary:     PSARC 2009/657 delete obsolete system call traps

changeset:   11466:d60272412fb0
user:        Roger A. Faulkner <Roger.Faulkner@Sun.COM>
date:        Mon Jan 11 17:42:13 2010 -0800
summary:     6915578 MUTEX_HELD() and RW_LOCK_HELD() macros should be available to Posix threads

changeset:   11237:0d23e47ed228
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Thu Dec 03 13:39:19 2009 +0000
summary:     6795386 macro arguments and globbing in DTrace probe descriptions don't mix

changeset:   10791:944abfb5b345
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Wed Oct 14 11:25:23 2009 +0100
summary:     6886953 large symbols lead to stack exhaustion

changeset:   10326:8e3fbeec2d76
user:        Siddheshwar Mahesh <Siddheshwar.Mahesh@Sun.COM>
date:        Mon Aug 17 13:26:49 2009 -0500
summary:     6868411 NFS provider generates error on ci_remote on RDMA operations

changeset:   10207:87c40ea3fc4b
user:        jmcp <James.McPherson@Sun.COM>
date:        Wed Jul 29 16:56:03 2009 -0700
summary:     6864230 hiho, hiho, it'ch chtime for CH to go

changeset:   10044:2643c1cd9e2a
user:        Priya Krishnan <Priya.Krishnan@Sun.COM>
date:        Mon Jul 06 21:19:41 2009 -0400
summary:     6855027 iscsit.d breaks dtrace in osol based on snv_117

changeset:   9900:1b86d65a4f9e
user:        Ali Bahrami <Ali.Bahrami@Sun.COM>
date:        Thu Jun 18 13:16:39 2009 -0600
summary:     6851224 elf_getshnum() and elf_getshstrndx() incompatible with 2002 ELF gABI agreement

changeset:   9885:a3d5e9d9e779
user:        Robert Mastors <Robert.Mastors@Sun.COM>
date:        Tue Jun 16 15:25:25 2009 -0500
summary:     6711844 assert: vp->v_shrlocks == 0L, file: ../../common/fs/vnode.c, line: 2333

changeset:   9881:741c9e4e094c
user:        Charles Ting <Charles.Ting@Sun.COM>
date:        Tue Jun 16 14:51:40 2009 -0400
summary:     6849606 SRP DTrace Probe for xfer-done misses completion of READ transfers

changeset:   9829:e8059fcaee97
user:        Charles Ting <Charles.Ting@Sun.COM>
date:        Tue Jun 09 10:11:35 2009 -0400
summary:     6804431 Add Dtrace probes to SRPT

changeset:   9812:a2990074321f
user:        Priya Krishnan <Priya.Krishnan@Sun.COM>
date:        Mon Jun 08 09:49:48 2009 -0400
summary:     6847237 The iscsit.d DTrace translator should include iscsi.d for the definition of iscsiinfo_t

changeset:   9721:4f7e194c7c37
user:        Priya Krishnan <Priya.Krishnan@Sun.COM>
date:        Tue May 26 10:40:43 2009 -0400
summary:     6809997 COMSTAR iscsi target DTrace Provider needed

changeset:   9625:8aa5731291b4
user:        Sam Cramer <Sam.Cramer@Sun.COM>
date:        Wed May 13 17:10:06 2009 -0700
summary:     6840354 "/usr/lib/dtrace/fc.d", line 59: syntax error near "fct_local_port_t"

changeset:   9609:8874cc8d5e3f
user:        Sam Cramer <Sam.Cramer@Sun.COM>
date:        Mon May 11 21:02:27 2009 -0700
summary:     6809580 fct DTrace providers needed for qlt

changeset:   9578:c4b38ec17f4e
user:        Sam Cramer <Sam.Cramer@Sun.COM>
date:        Fri May 08 12:12:40 2009 -0700
summary:     6809580 fct DTrace providers needed for qlt

changeset:   9531:dc8924ef7839
user:        Rafael Vanoni <rafael.vanoni@sun.com>
date:        Mon May 04 11:48:15 2009 -0700
summary:     6730130 dtrace missing printf handler for stdev

changeset:   9389:750ed3471e90
user:        Vamsi Nagineni <Vamsi.Krishna@Sun.COM>
date:        Fri Apr 17 06:26:47 2009 -0700
summary:     6812050 dtrace should translate curpsinfo->pr_contract

changeset:   9085:ff7eb0bace56
user:        Ali Bahrami <Ali.Bahrami@Sun.COM>
date:        Wed Mar 18 13:28:28 2009 -0600
summary:     6813909 generalize eh_frame support to non-amd64 platforms

changeset:   8803:8c01b39012c9
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Fri Feb 13 07:13:13 2009 +0000
summary:     PSARC 2008/480 DTrace CPC Provider

changeset:   8744:03d5725cda56
user:        Ali Bahrami <Ali.Bahrami@Sun.COM>
date:        Tue Feb 10 09:38:02 2009 -0700
summary:     6798660 Cadmium .NOT file processing problem with CWD relative file paths

changeset:   8337:079ecc003ca6
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Thu Dec 11 11:26:47 2008 +0000
summary:     6750659 drti.o crashes app due to corrupt environment

changeset:   7991:d3b751ef3d85
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Mon Nov 03 10:26:23 2008 +0000
summary:     6738982 Representative thread after DTrace stop() action is incorrect

changeset:   7208:568549b138d8
user:        vv149972
date:        Mon Jul 28 23:14:31 2008 -0700
summary:     6696397 NFS v3 provider reports all UDP clients as 0.0.0.0

changeset:   6878:360e73ea6b0c
user:        brendan
date:        Fri Jun 13 19:06:55 2008 -0700
summary:     PSARC 2008/302 DTrace IP Provider

changeset:   6554:b5817e112852
user:        ahl
date:        Mon May 05 14:38:24 2008 -0700
summary:     6677812 race between dtrace activities in non-local zones

----

- ctf tools:

changeset:   12177:800b7f847f1e
user:        Surya Prakki <Surya.Prakki@Sun.COM>
date:        Sun Apr 18 23:59:57 2010 -0700
summary:     6941452 ctfconvert fails on VLAs with code generated by SS12u1

changeset:   11432:c1c450bf62f2
user:        John Levon <john.levon@sun.com>
date:        Tue Jan 05 06:57:53 2010 -0800
summary:     6905711 anonymous and empty SOUs crash ctfconvert

changeset:   11227:cd2ac59c39f2
user:        Ali Bahrami <Ali.Bahrami@Sun.COM>
date:        Wed Dec 02 15:37:55 2009 -0700
summary:     6900241 ld should track SHT_GROUP sections by symbol name, not section name

changeset:   10380:5394a7172e1f
user:        Ali Bahrami <Ali.Bahrami@Sun.COM>
date:        Tue Aug 25 13:51:43 2009 -0600
summary:     6866605 SUNWonbld ELF analysis tools need overhaul (fix ctfmerge/libc dependency)

changeset:   10207:87c40ea3fc4b
user:        jmcp <James.McPherson@Sun.COM>
date:        Wed Jul 29 16:56:03 2009 -0700
summary:     6864230 hiho, hiho, it'ch chtime for CH to go

changeset:   10206:51f52702df72
user:        John Levon <john.levon@sun.com>
date:        Wed Jul 29 14:36:30 2009 -0700
summary:     6854065 CTF tools should error out given 1024+-member structures

changeset:   7230:429b4f7acf1a
user:        sn199410
date:        Wed Jul 30 16:10:30 2008 -0700
summary:     6575435 ctf tools cannot handle C99 VLAs ("variable length arrays")

changeset:   6936:72189fcd99e4
user:        sommerfe
date:        Sun Jun 22 09:13:44 2008 -0700
summary:     6716983 left-for-dead ctfmerge worker threads awake to take out maker

----

- dtrace command utility:

changeset:   12507:501806a754d2
user:        Alan Maguire <Alan.Maguire@Sun.COM>
date:        Thu May 27 17:29:51 2010 -0400
summary:     PSARC 2010/106 DTrace TCP and UDP providers

changeset:   11838:32bb5d254240
user:        Liane Praza <Liane.Praza@Sun.COM>
date:        Tue Mar 02 19:29:26 2010 -0700
summary:     PSARC 2010/067 Interim modernization updates

changeset:   11270:47a962fe7b45
user:        Frank Van Der Linden <Frank.Vanderlinden@Sun.COM>
date:        Mon Dec 07 13:47:36 2009 -0800
summary:     6907170 fix for 6875656 left out updates to DTrace test suite

changeset:   11237:0d23e47ed228
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Thu Dec 03 13:39:19 2009 +0000
summary:     6795386 macro arguments and globbing in DTrace probe descriptions don't mix

changeset:   11153:dec430d20576
user:        Frank Van Der Linden <Frank.Vanderlinden@Sun.COM>
date:        Sun Nov 22 19:22:26 2009 -0800
summary:     6875656 xdt needs to support more XenTrace probes

changeset:   11102:b91faef0c984
user:        Gavin Maltby <Gavin.Maltby@Sun.COM>
date:        Thu Nov 19 15:28:11 2009 +1100
summary:     PSARC/2009/554 door_xcreate - extended door creation interface for private doors

changeset:   11066:cebb50cbe4f9
user:        Rafael Vanoni <rafael.vanoni@sun.com>
date:        Fri Nov 13 01:32:32 2009 -0800
summary:     PSARC/2009/396 Tickless Kernel Architecture / lbolt decoupling

changeset:   10791:944abfb5b345
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Wed Oct 14 11:25:23 2009 +0100
summary:     6886953 large symbols lead to stack exhaustion

changeset:   10207:87c40ea3fc4b
user:        jmcp <James.McPherson@Sun.COM>
date:        Wed Jul 29 16:56:03 2009 -0700
summary:     6864230 hiho, hiho, it'ch chtime for CH to go

changeset:   9531:dc8924ef7839
user:        Rafael Vanoni <rafael.vanoni@sun.com>
date:        Mon May 04 11:48:15 2009 -0700
summary:     6730130 dtrace missing printf handler for stdev

changeset:   9397:e667d620a75c
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Mon Apr 20 07:58:44 2009 +0100
summary:     6806023 cpc provider event name validation needs to be a bit tighter

changeset:   8803:8c01b39012c9
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Fri Feb 13 07:13:13 2009 +0000
summary:     PSARC 2008/480 DTrace CPC Provider

changeset:   8605:0189cb9c5358
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Thu Jan 22 12:09:13 2009 +0000
summary:     6749441 intrstat(1M) shows zeroed values after suspend/resume

changeset:   8337:079ecc003ca6
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Thu Dec 11 11:26:47 2008 +0000
summary:     6750659 drti.o crashes app due to corrupt environment

changeset:   8287:771477e4b843
user:        John Sonnenschein <John.Sonnenschein@Sun.COM>
date:        Fri Dec 05 19:08:38 2008 -0800
summary:     PSARC 2005/462 Removal of Perl 5.6.1 from Solaris 11

changeset:   7991:d3b751ef3d85
user:        Jonathan Haslam <Jonathan.Haslam@Sun.COM>
date:        Mon Nov 03 10:26:23 2008 +0000
summary:     6738982 Representative thread after DTrace stop() action is incorrect

changeset:   7502:da077e5d991e
user:        Aruna Ramakrishna <aruna@cs.umn.edu>
date:        Sat Sep 06 05:36:02 2008 -0400
summary:     6706947 tcp_trace should be replaced with dtrace probes.

changeset:   7484:a48e950bad22
user:        Tom Erickson <tomee@eng.sun.com>
date:        Wed Sep 03 15:14:25 2008 -0700
summary:     6737926 getAggregate() method fails to specify anonymous aggregation explicitly

changeset:   7299:d9a056040774
user:        John Beck <John.Beck@Sun.COM>
date:        Thu Aug 07 12:44:26 2008 -0700
summary:     6734627 protocmp complains about opt/SUNWdtrt/README after TW -> Hg switch

changeset:   6998:58787ea78303
user:        brendan
date:        Tue Jul 01 18:28:22 2008 -0700
summary:     6721426 tst.sdtargs.d passes despite dtrace "invalid address" error

changeset:   6878:360e73ea6b0c
user:        brendan
date:        Fri Jun 13 19:06:55 2008 -0700
summary:     PSARC 2008/302 DTrace IP Provider

changeset:   6670:1961a43f2335
user:        tariq
date:        Tue May 20 15:08:16 2008 -0700
summary:     6685348 Hypervisor event provider for DTrace

changeset:   6554:b5817e112852
user:        ahl
date:        Mon May 05 14:38:24 2008 -0700
summary:     6677812 race between dtrace activities in non-local zones

changeset:   6543:465433824d87
user:        rie
date:        Fri May 02 15:01:06 2008 -0700
summary:     6683064 check_rtime could do with some spring cleaning
2010-08-02 12:13:33 +00:00

1635 lines
43 KiB
C

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