10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1239Sahl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #define ELF_TARGET_ALL 300Sstevel@tonic-gate #include <elf.h> 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <sys/sysmacros.h> 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include <unistd.h> 360Sstevel@tonic-gate #include <strings.h> 370Sstevel@tonic-gate #include <alloca.h> 380Sstevel@tonic-gate #include <limits.h> 390Sstevel@tonic-gate #include <stddef.h> 400Sstevel@tonic-gate #include <stdlib.h> 410Sstevel@tonic-gate #include <stdio.h> 420Sstevel@tonic-gate #include <fcntl.h> 430Sstevel@tonic-gate #include <errno.h> 440Sstevel@tonic-gate #include <wait.h> 450Sstevel@tonic-gate #include <assert.h> 46*1239Sahl #include <sys/ipc.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate #include <dt_impl.h> 490Sstevel@tonic-gate #include <dt_provider.h> 500Sstevel@tonic-gate #include <dt_string.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #define ESHDR_NULL 0 530Sstevel@tonic-gate #define ESHDR_SHSTRTAB 1 540Sstevel@tonic-gate #define ESHDR_DOF 2 550Sstevel@tonic-gate #define ESHDR_STRTAB 3 560Sstevel@tonic-gate #define ESHDR_SYMTAB 4 570Sstevel@tonic-gate #define ESHDR_REL 5 580Sstevel@tonic-gate #define ESHDR_NUM 6 590Sstevel@tonic-gate 600Sstevel@tonic-gate #define PWRITE_SCN(index, data) \ 610Sstevel@tonic-gate (lseek64(fd, (off64_t)elf_file.shdr[(index)].sh_offset, SEEK_SET) != \ 620Sstevel@tonic-gate (off64_t)elf_file.shdr[(index)].sh_offset || \ 630Sstevel@tonic-gate dt_write(dtp, fd, (data), elf_file.shdr[(index)].sh_size) != \ 640Sstevel@tonic-gate elf_file.shdr[(index)].sh_size) 650Sstevel@tonic-gate 660Sstevel@tonic-gate static const char DTRACE_SHSTRTAB32[] = "\0" 670Sstevel@tonic-gate ".shstrtab\0" /* 1 */ 680Sstevel@tonic-gate ".SUNW_dof\0" /* 11 */ 690Sstevel@tonic-gate ".strtab\0" /* 21 */ 700Sstevel@tonic-gate ".symtab\0" /* 29 */ 710Sstevel@tonic-gate #ifdef __sparc 720Sstevel@tonic-gate ".rela.SUNW_dof"; /* 37 */ 730Sstevel@tonic-gate #else 740Sstevel@tonic-gate ".rel.SUNW_dof"; /* 37 */ 750Sstevel@tonic-gate #endif 760Sstevel@tonic-gate 770Sstevel@tonic-gate static const char DTRACE_SHSTRTAB64[] = "\0" 780Sstevel@tonic-gate ".shstrtab\0" /* 1 */ 790Sstevel@tonic-gate ".SUNW_dof\0" /* 11 */ 800Sstevel@tonic-gate ".strtab\0" /* 21 */ 810Sstevel@tonic-gate ".symtab\0" /* 29 */ 820Sstevel@tonic-gate ".rela.SUNW_dof"; /* 37 */ 830Sstevel@tonic-gate 840Sstevel@tonic-gate static const char DOFSTR[] = "__SUNW_dof"; 850Sstevel@tonic-gate static const char DOFLAZYSTR[] = "___SUNW_dof"; 860Sstevel@tonic-gate 87*1239Sahl typedef struct dt_link_pair { 88*1239Sahl struct dt_link_pair *dlp_next; /* next pair in linked list */ 89*1239Sahl void *dlp_str; /* buffer for string table */ 90*1239Sahl void *dlp_sym; /* buffer for symbol table */ 91*1239Sahl } dt_link_pair_t; 92*1239Sahl 930Sstevel@tonic-gate typedef struct dof_elf32 { 940Sstevel@tonic-gate uint32_t de_nrel; /* relocation count */ 950Sstevel@tonic-gate #ifdef __sparc 960Sstevel@tonic-gate Elf32_Rela *de_rel; /* array of relocations for sparc */ 970Sstevel@tonic-gate #else 980Sstevel@tonic-gate Elf32_Rel *de_rel; /* array of relocations for x86 */ 990Sstevel@tonic-gate #endif 1000Sstevel@tonic-gate uint32_t de_nsym; /* symbol count */ 1010Sstevel@tonic-gate Elf32_Sym *de_sym; /* array of symbols */ 1020Sstevel@tonic-gate uint32_t de_strlen; /* size of of string table */ 1030Sstevel@tonic-gate char *de_strtab; /* string table */ 1040Sstevel@tonic-gate uint32_t de_global; /* index of the first global symbol */ 1050Sstevel@tonic-gate } dof_elf32_t; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate static int 1080Sstevel@tonic-gate prepare_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf32_t *dep) 1090Sstevel@tonic-gate { 1100Sstevel@tonic-gate dof_sec_t *dofs, *s; 1110Sstevel@tonic-gate dof_relohdr_t *dofrh; 1120Sstevel@tonic-gate dof_relodesc_t *dofr; 1130Sstevel@tonic-gate char *strtab; 1140Sstevel@tonic-gate int i, j, nrel; 1150Sstevel@tonic-gate size_t strtabsz = 1; 1160Sstevel@tonic-gate uint32_t count = 0; 1170Sstevel@tonic-gate size_t base; 1180Sstevel@tonic-gate Elf32_Sym *sym; 1190Sstevel@tonic-gate #ifdef __sparc 1200Sstevel@tonic-gate Elf32_Rela *rel; 1210Sstevel@tonic-gate #else 1220Sstevel@tonic-gate Elf32_Rel *rel; 1230Sstevel@tonic-gate #endif 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /*LINTED*/ 1260Sstevel@tonic-gate dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * First compute the size of the string table and the number of 1300Sstevel@tonic-gate * relocations present in the DOF. 1310Sstevel@tonic-gate */ 1320Sstevel@tonic-gate for (i = 0; i < dof->dofh_secnum; i++) { 1330Sstevel@tonic-gate if (dofs[i].dofs_type != DOF_SECT_URELHDR) 1340Sstevel@tonic-gate continue; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /*LINTED*/ 1370Sstevel@tonic-gate dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate s = &dofs[dofrh->dofr_strtab]; 1400Sstevel@tonic-gate strtab = (char *)dof + s->dofs_offset; 1410Sstevel@tonic-gate assert(strtab[0] == '\0'); 1420Sstevel@tonic-gate strtabsz += s->dofs_size - 1; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate s = &dofs[dofrh->dofr_relsec]; 1450Sstevel@tonic-gate /*LINTED*/ 1460Sstevel@tonic-gate dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 1470Sstevel@tonic-gate count += s->dofs_size / s->dofs_entsize; 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate dep->de_strlen = strtabsz; 1510Sstevel@tonic-gate dep->de_nrel = count; 1520Sstevel@tonic-gate dep->de_nsym = count + 1; /* the first symbol is always null */ 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate if (dtp->dt_lazyload) { 1550Sstevel@tonic-gate dep->de_strlen += sizeof (DOFLAZYSTR); 1560Sstevel@tonic-gate dep->de_nsym++; 1570Sstevel@tonic-gate } else { 1580Sstevel@tonic-gate dep->de_strlen += sizeof (DOFSTR); 1590Sstevel@tonic-gate dep->de_nsym++; 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate if ((dep->de_rel = calloc(dep->de_nrel, 1630Sstevel@tonic-gate sizeof (dep->de_rel[0]))) == NULL) { 1640Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf32_Sym))) == NULL) { 1680Sstevel@tonic-gate free(dep->de_rel); 1690Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) { 1730Sstevel@tonic-gate free(dep->de_rel); 1740Sstevel@tonic-gate free(dep->de_sym); 1750Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate count = 0; 1790Sstevel@tonic-gate strtabsz = 1; 1800Sstevel@tonic-gate dep->de_strtab[0] = '\0'; 1810Sstevel@tonic-gate rel = dep->de_rel; 1820Sstevel@tonic-gate sym = dep->de_sym; 1830Sstevel@tonic-gate dep->de_global = 1; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate /* 1860Sstevel@tonic-gate * The first symbol table entry must be zeroed and is always ignored. 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate bzero(sym, sizeof (Elf32_Sym)); 1890Sstevel@tonic-gate sym++; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate /* 1920Sstevel@tonic-gate * Take a second pass through the DOF sections filling in the 1930Sstevel@tonic-gate * memory we allocated. 1940Sstevel@tonic-gate */ 1950Sstevel@tonic-gate for (i = 0; i < dof->dofh_secnum; i++) { 1960Sstevel@tonic-gate if (dofs[i].dofs_type != DOF_SECT_URELHDR) 1970Sstevel@tonic-gate continue; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /*LINTED*/ 2000Sstevel@tonic-gate dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate s = &dofs[dofrh->dofr_strtab]; 2030Sstevel@tonic-gate strtab = (char *)dof + s->dofs_offset; 2040Sstevel@tonic-gate bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size); 2050Sstevel@tonic-gate base = strtabsz; 2060Sstevel@tonic-gate strtabsz += s->dofs_size - 1; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate s = &dofs[dofrh->dofr_relsec]; 2090Sstevel@tonic-gate /*LINTED*/ 2100Sstevel@tonic-gate dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 2110Sstevel@tonic-gate nrel = s->dofs_size / s->dofs_entsize; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate s = &dofs[dofrh->dofr_tgtsec]; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate for (j = 0; j < nrel; j++) { 2160Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 2170Sstevel@tonic-gate rel->r_offset = s->dofs_offset + 2180Sstevel@tonic-gate dofr[j].dofr_offset; 2190Sstevel@tonic-gate rel->r_info = ELF32_R_INFO(count + dep->de_global, 2200Sstevel@tonic-gate R_386_32); 2210Sstevel@tonic-gate #elif defined(__sparc) 2220Sstevel@tonic-gate /* 2230Sstevel@tonic-gate * Add 4 bytes to hit the low half of this 64-bit 2240Sstevel@tonic-gate * big-endian address. 2250Sstevel@tonic-gate */ 2260Sstevel@tonic-gate rel->r_offset = s->dofs_offset + 2270Sstevel@tonic-gate dofr[j].dofr_offset + 4; 2280Sstevel@tonic-gate rel->r_info = ELF32_R_INFO(count + dep->de_global, 2290Sstevel@tonic-gate R_SPARC_32); 2300Sstevel@tonic-gate #else 2310Sstevel@tonic-gate #error unknown ISA 2320Sstevel@tonic-gate #endif 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate sym->st_name = base + dofr[j].dofr_name - 1; 2350Sstevel@tonic-gate sym->st_value = 0; 2360Sstevel@tonic-gate sym->st_size = 0; 237*1239Sahl sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC); 2380Sstevel@tonic-gate sym->st_other = 0; 2390Sstevel@tonic-gate sym->st_shndx = SHN_UNDEF; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate rel++; 2420Sstevel@tonic-gate sym++; 2430Sstevel@tonic-gate count++; 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * Add a symbol for the DOF itself. We use a different symbol for 2490Sstevel@tonic-gate * lazily and actively loaded DOF to make them easy to distinguish. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate sym->st_name = strtabsz; 2520Sstevel@tonic-gate sym->st_value = 0; 2530Sstevel@tonic-gate sym->st_size = dof->dofh_filesz; 2540Sstevel@tonic-gate sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT); 2550Sstevel@tonic-gate sym->st_other = 0; 2560Sstevel@tonic-gate sym->st_shndx = ESHDR_DOF; 2570Sstevel@tonic-gate sym++; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate if (dtp->dt_lazyload) { 2600Sstevel@tonic-gate bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz, 2610Sstevel@tonic-gate sizeof (DOFLAZYSTR)); 2620Sstevel@tonic-gate strtabsz += sizeof (DOFLAZYSTR); 2630Sstevel@tonic-gate } else { 2640Sstevel@tonic-gate bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR)); 2650Sstevel@tonic-gate strtabsz += sizeof (DOFSTR); 2660Sstevel@tonic-gate } 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate assert(count == dep->de_nrel); 2690Sstevel@tonic-gate assert(strtabsz == dep->de_strlen); 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate return (0); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate typedef struct dof_elf64 { 2760Sstevel@tonic-gate uint32_t de_nrel; 2770Sstevel@tonic-gate Elf64_Rela *de_rel; 2780Sstevel@tonic-gate uint32_t de_nsym; 2790Sstevel@tonic-gate Elf64_Sym *de_sym; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate uint32_t de_strlen; 2820Sstevel@tonic-gate char *de_strtab; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate uint32_t de_global; 2850Sstevel@tonic-gate } dof_elf64_t; 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate static int 2880Sstevel@tonic-gate prepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep) 2890Sstevel@tonic-gate { 2900Sstevel@tonic-gate dof_sec_t *dofs, *s; 2910Sstevel@tonic-gate dof_relohdr_t *dofrh; 2920Sstevel@tonic-gate dof_relodesc_t *dofr; 2930Sstevel@tonic-gate char *strtab; 2940Sstevel@tonic-gate int i, j, nrel; 2950Sstevel@tonic-gate size_t strtabsz = 1; 2960Sstevel@tonic-gate uint32_t count = 0; 2970Sstevel@tonic-gate size_t base; 2980Sstevel@tonic-gate Elf64_Sym *sym; 2990Sstevel@tonic-gate Elf64_Rela *rel; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /*LINTED*/ 3020Sstevel@tonic-gate dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * First compute the size of the string table and the number of 3060Sstevel@tonic-gate * relocations present in the DOF. 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate for (i = 0; i < dof->dofh_secnum; i++) { 3090Sstevel@tonic-gate if (dofs[i].dofs_type != DOF_SECT_URELHDR) 3100Sstevel@tonic-gate continue; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /*LINTED*/ 3130Sstevel@tonic-gate dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate s = &dofs[dofrh->dofr_strtab]; 3160Sstevel@tonic-gate strtab = (char *)dof + s->dofs_offset; 3170Sstevel@tonic-gate assert(strtab[0] == '\0'); 3180Sstevel@tonic-gate strtabsz += s->dofs_size - 1; 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate s = &dofs[dofrh->dofr_relsec]; 3210Sstevel@tonic-gate /*LINTED*/ 3220Sstevel@tonic-gate dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 3230Sstevel@tonic-gate count += s->dofs_size / s->dofs_entsize; 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate dep->de_strlen = strtabsz; 3270Sstevel@tonic-gate dep->de_nrel = count; 3280Sstevel@tonic-gate dep->de_nsym = count + 1; /* the first symbol is always null */ 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate if (dtp->dt_lazyload) { 3310Sstevel@tonic-gate dep->de_strlen += sizeof (DOFLAZYSTR); 3320Sstevel@tonic-gate dep->de_nsym++; 3330Sstevel@tonic-gate } else { 3340Sstevel@tonic-gate dep->de_strlen += sizeof (DOFSTR); 3350Sstevel@tonic-gate dep->de_nsym++; 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate if ((dep->de_rel = calloc(dep->de_nrel, 3390Sstevel@tonic-gate sizeof (dep->de_rel[0]))) == NULL) { 3400Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 3410Sstevel@tonic-gate } 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) { 3440Sstevel@tonic-gate free(dep->de_rel); 3450Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) { 3490Sstevel@tonic-gate free(dep->de_rel); 3500Sstevel@tonic-gate free(dep->de_sym); 3510Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_NOMEM)); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate count = 0; 3550Sstevel@tonic-gate strtabsz = 1; 3560Sstevel@tonic-gate dep->de_strtab[0] = '\0'; 3570Sstevel@tonic-gate rel = dep->de_rel; 3580Sstevel@tonic-gate sym = dep->de_sym; 3590Sstevel@tonic-gate dep->de_global = 1; 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate /* 3620Sstevel@tonic-gate * The first symbol table entry must be zeroed and is always ignored. 3630Sstevel@tonic-gate */ 3640Sstevel@tonic-gate bzero(sym, sizeof (Elf64_Sym)); 3650Sstevel@tonic-gate sym++; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate /* 3680Sstevel@tonic-gate * Take a second pass through the DOF sections filling in the 3690Sstevel@tonic-gate * memory we allocated. 3700Sstevel@tonic-gate */ 3710Sstevel@tonic-gate for (i = 0; i < dof->dofh_secnum; i++) { 3720Sstevel@tonic-gate if (dofs[i].dofs_type != DOF_SECT_URELHDR) 3730Sstevel@tonic-gate continue; 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate /*LINTED*/ 3760Sstevel@tonic-gate dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 3770Sstevel@tonic-gate 3780Sstevel@tonic-gate s = &dofs[dofrh->dofr_strtab]; 3790Sstevel@tonic-gate strtab = (char *)dof + s->dofs_offset; 3800Sstevel@tonic-gate bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size); 3810Sstevel@tonic-gate base = strtabsz; 3820Sstevel@tonic-gate strtabsz += s->dofs_size - 1; 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate s = &dofs[dofrh->dofr_relsec]; 3850Sstevel@tonic-gate /*LINTED*/ 3860Sstevel@tonic-gate dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 3870Sstevel@tonic-gate nrel = s->dofs_size / s->dofs_entsize; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate s = &dofs[dofrh->dofr_tgtsec]; 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate for (j = 0; j < nrel; j++) { 3920Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 3930Sstevel@tonic-gate rel->r_offset = s->dofs_offset + 3940Sstevel@tonic-gate dofr[j].dofr_offset; 3950Sstevel@tonic-gate rel->r_info = ELF64_R_INFO(count + dep->de_global, 3960Sstevel@tonic-gate R_AMD64_64); 3970Sstevel@tonic-gate #elif defined(__sparc) 3980Sstevel@tonic-gate rel->r_offset = s->dofs_offset + 3990Sstevel@tonic-gate dofr[j].dofr_offset; 4000Sstevel@tonic-gate rel->r_info = ELF64_R_INFO(count + dep->de_global, 4010Sstevel@tonic-gate R_SPARC_64); 4020Sstevel@tonic-gate #else 4030Sstevel@tonic-gate #error unknown ISA 4040Sstevel@tonic-gate #endif 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate sym->st_name = base + dofr[j].dofr_name - 1; 4070Sstevel@tonic-gate sym->st_value = 0; 4080Sstevel@tonic-gate sym->st_size = 0; 409*1239Sahl sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC); 4100Sstevel@tonic-gate sym->st_other = 0; 4110Sstevel@tonic-gate sym->st_shndx = SHN_UNDEF; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate rel++; 4140Sstevel@tonic-gate sym++; 4150Sstevel@tonic-gate count++; 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * Add a symbol for the DOF itself. We use a different symbol for 4210Sstevel@tonic-gate * lazily and actively loaded DOF to make them easy to distinguish. 4220Sstevel@tonic-gate */ 4230Sstevel@tonic-gate sym->st_name = strtabsz; 4240Sstevel@tonic-gate sym->st_value = 0; 4250Sstevel@tonic-gate sym->st_size = dof->dofh_filesz; 426*1239Sahl sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 4270Sstevel@tonic-gate sym->st_other = 0; 4280Sstevel@tonic-gate sym->st_shndx = ESHDR_DOF; 4290Sstevel@tonic-gate sym++; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate if (dtp->dt_lazyload) { 4320Sstevel@tonic-gate bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz, 4330Sstevel@tonic-gate sizeof (DOFLAZYSTR)); 4340Sstevel@tonic-gate strtabsz += sizeof (DOFLAZYSTR); 4350Sstevel@tonic-gate } else { 4360Sstevel@tonic-gate bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR)); 4370Sstevel@tonic-gate strtabsz += sizeof (DOFSTR); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate assert(count == dep->de_nrel); 4410Sstevel@tonic-gate assert(strtabsz == dep->de_strlen); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate return (0); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate /* 4470Sstevel@tonic-gate * Write out an ELF32 file prologue consisting of a header, section headers, 4480Sstevel@tonic-gate * and a section header string table. The DOF data will follow this prologue 4490Sstevel@tonic-gate * and complete the contents of the given ELF file. 4500Sstevel@tonic-gate */ 4510Sstevel@tonic-gate static int 4520Sstevel@tonic-gate dump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd) 4530Sstevel@tonic-gate { 4540Sstevel@tonic-gate struct { 4550Sstevel@tonic-gate Elf32_Ehdr ehdr; 4560Sstevel@tonic-gate Elf32_Shdr shdr[ESHDR_NUM]; 4570Sstevel@tonic-gate } elf_file; 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate Elf32_Shdr *shp; 4600Sstevel@tonic-gate Elf32_Off off; 4610Sstevel@tonic-gate dof_elf32_t de; 4620Sstevel@tonic-gate int ret = 0; 4630Sstevel@tonic-gate uint_t nshdr; 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate if (prepare_elf32(dtp, dof, &de) != 0) 4660Sstevel@tonic-gate return (-1); /* errno is set for us */ 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * If there are no relocations, we only need enough sections for 4700Sstevel@tonic-gate * the shstrtab and the DOF. 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM; 4730Sstevel@tonic-gate 4740Sstevel@tonic-gate bzero(&elf_file, sizeof (elf_file)); 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0; 4770Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1; 4780Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2; 4790Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3; 4800Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT; 4810Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS32; 4820Sstevel@tonic-gate #if defined(_BIG_ENDIAN) 4830Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB; 4840Sstevel@tonic-gate #elif defined(_LITTLE_ENDIAN) 4850Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB; 4860Sstevel@tonic-gate #endif 4870Sstevel@tonic-gate elf_file.ehdr.e_type = ET_REL; 4880Sstevel@tonic-gate #if defined(__sparc) 4890Sstevel@tonic-gate elf_file.ehdr.e_machine = EM_SPARC; 4900Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 4910Sstevel@tonic-gate elf_file.ehdr.e_machine = EM_386; 4920Sstevel@tonic-gate #endif 4930Sstevel@tonic-gate elf_file.ehdr.e_version = EV_CURRENT; 4940Sstevel@tonic-gate elf_file.ehdr.e_shoff = sizeof (Elf32_Ehdr); 4950Sstevel@tonic-gate elf_file.ehdr.e_ehsize = sizeof (Elf32_Ehdr); 4960Sstevel@tonic-gate elf_file.ehdr.e_phentsize = sizeof (Elf32_Phdr); 4970Sstevel@tonic-gate elf_file.ehdr.e_shentsize = sizeof (Elf32_Shdr); 4980Sstevel@tonic-gate elf_file.ehdr.e_shnum = nshdr; 4990Sstevel@tonic-gate elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB; 5000Sstevel@tonic-gate off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_SHSTRTAB]; 5030Sstevel@tonic-gate shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */ 5040Sstevel@tonic-gate shp->sh_type = SHT_STRTAB; 5050Sstevel@tonic-gate shp->sh_offset = off; 5060Sstevel@tonic-gate shp->sh_size = sizeof (DTRACE_SHSTRTAB32); 5070Sstevel@tonic-gate shp->sh_addralign = sizeof (char); 5080Sstevel@tonic-gate off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_DOF]; 5110Sstevel@tonic-gate shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */ 5120Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 5130Sstevel@tonic-gate shp->sh_type = SHT_SUNW_dof; 5140Sstevel@tonic-gate shp->sh_offset = off; 5150Sstevel@tonic-gate shp->sh_size = dof->dofh_filesz; 5160Sstevel@tonic-gate shp->sh_addralign = 8; 5170Sstevel@tonic-gate off = shp->sh_offset + shp->sh_size; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_STRTAB]; 5200Sstevel@tonic-gate shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */ 5210Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 5220Sstevel@tonic-gate shp->sh_type = SHT_STRTAB; 5230Sstevel@tonic-gate shp->sh_offset = off; 5240Sstevel@tonic-gate shp->sh_size = de.de_strlen; 5250Sstevel@tonic-gate shp->sh_addralign = sizeof (char); 5260Sstevel@tonic-gate off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_SYMTAB]; 5290Sstevel@tonic-gate shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */ 5300Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 5310Sstevel@tonic-gate shp->sh_type = SHT_SYMTAB; 5320Sstevel@tonic-gate shp->sh_entsize = sizeof (Elf32_Sym); 5330Sstevel@tonic-gate shp->sh_link = ESHDR_STRTAB; 5340Sstevel@tonic-gate shp->sh_offset = off; 5350Sstevel@tonic-gate shp->sh_info = de.de_global; 5360Sstevel@tonic-gate shp->sh_size = de.de_nsym * sizeof (Elf32_Sym); 5370Sstevel@tonic-gate shp->sh_addralign = 4; 5380Sstevel@tonic-gate off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4); 5390Sstevel@tonic-gate 5400Sstevel@tonic-gate if (de.de_nrel == 0) { 5410Sstevel@tonic-gate if (dt_write(dtp, fd, &elf_file, 5420Sstevel@tonic-gate sizeof (elf_file)) != sizeof (elf_file) || 5430Sstevel@tonic-gate PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) || 5440Sstevel@tonic-gate PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) || 5450Sstevel@tonic-gate PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) || 5460Sstevel@tonic-gate PWRITE_SCN(ESHDR_DOF, dof)) { 5470Sstevel@tonic-gate ret = dt_set_errno(dtp, errno); 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate } else { 5500Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_REL]; 5510Sstevel@tonic-gate shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */ 5520Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 5530Sstevel@tonic-gate #ifdef __sparc 5540Sstevel@tonic-gate shp->sh_type = SHT_RELA; 5550Sstevel@tonic-gate #else 5560Sstevel@tonic-gate shp->sh_type = SHT_REL; 5570Sstevel@tonic-gate #endif 5580Sstevel@tonic-gate shp->sh_entsize = sizeof (de.de_rel[0]); 5590Sstevel@tonic-gate shp->sh_link = ESHDR_SYMTAB; 5600Sstevel@tonic-gate shp->sh_info = ESHDR_DOF; 5610Sstevel@tonic-gate shp->sh_offset = off; 5620Sstevel@tonic-gate shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]); 5630Sstevel@tonic-gate shp->sh_addralign = 4; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate if (dt_write(dtp, fd, &elf_file, 5660Sstevel@tonic-gate sizeof (elf_file)) != sizeof (elf_file) || 5670Sstevel@tonic-gate PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) || 5680Sstevel@tonic-gate PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) || 5690Sstevel@tonic-gate PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) || 5700Sstevel@tonic-gate PWRITE_SCN(ESHDR_REL, de.de_rel) || 5710Sstevel@tonic-gate PWRITE_SCN(ESHDR_DOF, dof)) { 5720Sstevel@tonic-gate ret = dt_set_errno(dtp, errno); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate } 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate free(de.de_strtab); 5770Sstevel@tonic-gate free(de.de_sym); 5780Sstevel@tonic-gate free(de.de_rel); 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate return (ret); 5810Sstevel@tonic-gate } 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate /* 5840Sstevel@tonic-gate * Write out an ELF64 file prologue consisting of a header, section headers, 5850Sstevel@tonic-gate * and a section header string table. The DOF data will follow this prologue 5860Sstevel@tonic-gate * and complete the contents of the given ELF file. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate static int 5890Sstevel@tonic-gate dump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd) 5900Sstevel@tonic-gate { 5910Sstevel@tonic-gate struct { 5920Sstevel@tonic-gate Elf64_Ehdr ehdr; 5930Sstevel@tonic-gate Elf64_Shdr shdr[ESHDR_NUM]; 5940Sstevel@tonic-gate } elf_file; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate Elf64_Shdr *shp; 5970Sstevel@tonic-gate Elf64_Off off; 5980Sstevel@tonic-gate dof_elf64_t de; 5990Sstevel@tonic-gate int ret = 0; 6000Sstevel@tonic-gate uint_t nshdr; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate if (prepare_elf64(dtp, dof, &de) != 0) 6030Sstevel@tonic-gate return (-1); /* errno is set for us */ 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate /* 6060Sstevel@tonic-gate * If there are no relocations, we only need enough sections for 6070Sstevel@tonic-gate * the shstrtab and the DOF. 6080Sstevel@tonic-gate */ 6090Sstevel@tonic-gate nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate bzero(&elf_file, sizeof (elf_file)); 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0; 6140Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1; 6150Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2; 6160Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3; 6170Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT; 6180Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS64; 6190Sstevel@tonic-gate #if defined(_BIG_ENDIAN) 6200Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB; 6210Sstevel@tonic-gate #elif defined(_LITTLE_ENDIAN) 6220Sstevel@tonic-gate elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB; 6230Sstevel@tonic-gate #endif 6240Sstevel@tonic-gate elf_file.ehdr.e_type = ET_REL; 6250Sstevel@tonic-gate #if defined(__sparc) 6260Sstevel@tonic-gate elf_file.ehdr.e_machine = EM_SPARCV9; 6270Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 6280Sstevel@tonic-gate elf_file.ehdr.e_machine = EM_AMD64; 6290Sstevel@tonic-gate #endif 6300Sstevel@tonic-gate elf_file.ehdr.e_version = EV_CURRENT; 6310Sstevel@tonic-gate elf_file.ehdr.e_shoff = sizeof (Elf64_Ehdr); 6320Sstevel@tonic-gate elf_file.ehdr.e_ehsize = sizeof (Elf64_Ehdr); 6330Sstevel@tonic-gate elf_file.ehdr.e_phentsize = sizeof (Elf64_Phdr); 6340Sstevel@tonic-gate elf_file.ehdr.e_shentsize = sizeof (Elf64_Shdr); 6350Sstevel@tonic-gate elf_file.ehdr.e_shnum = nshdr; 6360Sstevel@tonic-gate elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB; 6370Sstevel@tonic-gate off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr); 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_SHSTRTAB]; 6400Sstevel@tonic-gate shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */ 6410Sstevel@tonic-gate shp->sh_type = SHT_STRTAB; 6420Sstevel@tonic-gate shp->sh_offset = off; 6430Sstevel@tonic-gate shp->sh_size = sizeof (DTRACE_SHSTRTAB64); 6440Sstevel@tonic-gate shp->sh_addralign = sizeof (char); 6450Sstevel@tonic-gate off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_DOF]; 6480Sstevel@tonic-gate shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */ 6490Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 6500Sstevel@tonic-gate shp->sh_type = SHT_SUNW_dof; 6510Sstevel@tonic-gate shp->sh_offset = off; 6520Sstevel@tonic-gate shp->sh_size = dof->dofh_filesz; 6530Sstevel@tonic-gate shp->sh_addralign = 8; 6540Sstevel@tonic-gate off = shp->sh_offset + shp->sh_size; 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_STRTAB]; 6570Sstevel@tonic-gate shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */ 6580Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 6590Sstevel@tonic-gate shp->sh_type = SHT_STRTAB; 6600Sstevel@tonic-gate shp->sh_offset = off; 6610Sstevel@tonic-gate shp->sh_size = de.de_strlen; 6620Sstevel@tonic-gate shp->sh_addralign = sizeof (char); 6630Sstevel@tonic-gate off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_SYMTAB]; 6660Sstevel@tonic-gate shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */ 6670Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 6680Sstevel@tonic-gate shp->sh_type = SHT_SYMTAB; 6690Sstevel@tonic-gate shp->sh_entsize = sizeof (Elf64_Sym); 6700Sstevel@tonic-gate shp->sh_link = ESHDR_STRTAB; 6710Sstevel@tonic-gate shp->sh_offset = off; 6720Sstevel@tonic-gate shp->sh_info = de.de_global; 6730Sstevel@tonic-gate shp->sh_size = de.de_nsym * sizeof (Elf64_Sym); 6740Sstevel@tonic-gate shp->sh_addralign = 8; 6750Sstevel@tonic-gate off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate if (de.de_nrel == 0) { 6780Sstevel@tonic-gate if (dt_write(dtp, fd, &elf_file, 6790Sstevel@tonic-gate sizeof (elf_file)) != sizeof (elf_file) || 6800Sstevel@tonic-gate PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) || 6810Sstevel@tonic-gate PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) || 6820Sstevel@tonic-gate PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) || 6830Sstevel@tonic-gate PWRITE_SCN(ESHDR_DOF, dof)) { 6840Sstevel@tonic-gate ret = dt_set_errno(dtp, errno); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate } else { 6870Sstevel@tonic-gate shp = &elf_file.shdr[ESHDR_REL]; 6880Sstevel@tonic-gate shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */ 6890Sstevel@tonic-gate shp->sh_flags = SHF_ALLOC; 6900Sstevel@tonic-gate shp->sh_type = SHT_RELA; 6910Sstevel@tonic-gate shp->sh_entsize = sizeof (de.de_rel[0]); 6920Sstevel@tonic-gate shp->sh_link = ESHDR_SYMTAB; 6930Sstevel@tonic-gate shp->sh_info = ESHDR_DOF; 6940Sstevel@tonic-gate shp->sh_offset = off; 6950Sstevel@tonic-gate shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]); 6960Sstevel@tonic-gate shp->sh_addralign = 8; 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate if (dt_write(dtp, fd, &elf_file, 6990Sstevel@tonic-gate sizeof (elf_file)) != sizeof (elf_file) || 7000Sstevel@tonic-gate PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) || 7010Sstevel@tonic-gate PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) || 7020Sstevel@tonic-gate PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) || 7030Sstevel@tonic-gate PWRITE_SCN(ESHDR_REL, de.de_rel) || 7040Sstevel@tonic-gate PWRITE_SCN(ESHDR_DOF, dof)) { 7050Sstevel@tonic-gate ret = dt_set_errno(dtp, errno); 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate free(de.de_strtab); 7100Sstevel@tonic-gate free(de.de_sym); 7110Sstevel@tonic-gate free(de.de_rel); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate return (ret); 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate static int 7170Sstevel@tonic-gate dt_symtab_lookup(Elf_Data *data_sym, uintptr_t addr, uint_t shn, GElf_Sym *sym) 7180Sstevel@tonic-gate { 7190Sstevel@tonic-gate int i, ret = -1; 7200Sstevel@tonic-gate GElf_Sym s; 7210Sstevel@tonic-gate 7220Sstevel@tonic-gate for (i = 0; gelf_getsym(data_sym, i, sym) != NULL; i++) { 7230Sstevel@tonic-gate if (GELF_ST_TYPE(sym->st_info) == STT_FUNC && 7240Sstevel@tonic-gate shn == sym->st_shndx && 7250Sstevel@tonic-gate sym->st_value <= addr && 7260Sstevel@tonic-gate addr < sym->st_value + sym->st_size) { 7270Sstevel@tonic-gate if (GELF_ST_BIND(sym->st_info) == STB_GLOBAL) 7280Sstevel@tonic-gate return (0); 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate ret = 0; 7310Sstevel@tonic-gate s = *sym; 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate } 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate if (ret == 0) 7360Sstevel@tonic-gate *sym = s; 7370Sstevel@tonic-gate return (ret); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate #if defined(__sparc) 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate #define DT_OP_RET 0x81c7e008 7430Sstevel@tonic-gate #define DT_OP_NOP 0x01000000 7440Sstevel@tonic-gate #define DT_OP_CALL 0x40000000 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate #define DT_IS_MOV_O7(inst) (((inst) & 0xffffe000) == 0x9e100000) 7470Sstevel@tonic-gate #define DT_IS_RESTORE(inst) (((inst) & 0xc1f80000) == 0x81e80000) 7480Sstevel@tonic-gate #define DT_IS_RETL(inst) (((inst) & 0xfff83fff) == 0x81c02008) 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate #define DT_RS2(inst) ((inst) & 0x1f) 7510Sstevel@tonic-gate #define DT_MAKE_RETL(reg) (0x81c02008 | ((reg) << 14)) 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate static int 7540Sstevel@tonic-gate dt_modtext(char *p, GElf_Rela *rela, uint32_t *off) 7550Sstevel@tonic-gate { 7560Sstevel@tonic-gate uint32_t *ip; 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0) 7590Sstevel@tonic-gate return (-1); 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate /*LINTED*/ 7620Sstevel@tonic-gate ip = (uint32_t *)(p + rela->r_offset); 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * We only know about some specific relocation types. 7660Sstevel@tonic-gate */ 7670Sstevel@tonic-gate if (GELF_R_TYPE(rela->r_info) != R_SPARC_WDISP30 && 7680Sstevel@tonic-gate GELF_R_TYPE(rela->r_info) != R_SPARC_WPLT30) 7690Sstevel@tonic-gate return (-1); 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate /* 7720Sstevel@tonic-gate * We may have already processed this object file in an earlier 7730Sstevel@tonic-gate * linker invocation in which case we'd expect to see a ret/restore 7740Sstevel@tonic-gate * pair, a retl-like/mov pair or a nop; return success in that case. 7750Sstevel@tonic-gate */ 7760Sstevel@tonic-gate if (DT_IS_RESTORE(ip[1])) { 7770Sstevel@tonic-gate if (ip[0] == DT_OP_RET) { 7780Sstevel@tonic-gate return (0); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate } else if (DT_IS_MOV_O7(ip[1])) { 7810Sstevel@tonic-gate if (DT_IS_RETL(ip[0])) { 7820Sstevel@tonic-gate return (0); 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate } else { 7850Sstevel@tonic-gate if (ip[0] == DT_OP_NOP) { 7860Sstevel@tonic-gate (*off) += sizeof (ip[0]); 7870Sstevel@tonic-gate return (0); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate /* 7920Sstevel@tonic-gate * We only expect call instructions with a displacement of 0. 7930Sstevel@tonic-gate */ 7940Sstevel@tonic-gate if (ip[0] != DT_OP_CALL) { 7950Sstevel@tonic-gate dt_dprintf("found %x instead of a call instruction at %llx\n", 7960Sstevel@tonic-gate ip[0], (u_longlong_t)rela->r_offset); 7970Sstevel@tonic-gate return (-1); 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate /* 8010Sstevel@tonic-gate * If the call is followed by a restore, it's a tail call so change 8020Sstevel@tonic-gate * the call to a ret. If the call if followed by a mov of a register 8030Sstevel@tonic-gate * into %o7, it's a tail call in leaf context so change the call to 8040Sstevel@tonic-gate * a retl-like instruction that returns to that register value + 8 8050Sstevel@tonic-gate * (rather than the typical %o7 + 8). Otherwise we adjust the offset 8060Sstevel@tonic-gate * to land on what was once the delay slot of the call so we 8070Sstevel@tonic-gate * correctly get all the arguments. 8080Sstevel@tonic-gate */ 8090Sstevel@tonic-gate if (DT_IS_RESTORE(ip[1])) { 8100Sstevel@tonic-gate ip[0] = DT_OP_RET; 8110Sstevel@tonic-gate } else if (DT_IS_MOV_O7(ip[1])) { 8120Sstevel@tonic-gate ip[0] = DT_MAKE_RETL(DT_RS2(ip[1])); 8130Sstevel@tonic-gate } else { 8140Sstevel@tonic-gate ip[0] = DT_OP_NOP; 8150Sstevel@tonic-gate (*off) += sizeof (ip[0]); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate return (0); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate 8210Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate #define DT_OP_NOP 0x90 8240Sstevel@tonic-gate #define DT_OP_CALL 0xe8 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate static int 8270Sstevel@tonic-gate dt_modtext(char *p, GElf_Rela *rela, uint32_t *off) 8280Sstevel@tonic-gate { 8290Sstevel@tonic-gate uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1); 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate /* 8320Sstevel@tonic-gate * On x86, the first byte of the instruction is the call opcode and 8330Sstevel@tonic-gate * the next four bytes are the 32-bit address; the relocation is for 8340Sstevel@tonic-gate * the address so we back up one byte to land on the opcode. 8350Sstevel@tonic-gate */ 8360Sstevel@tonic-gate (*off) -= 1; 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate /* 8390Sstevel@tonic-gate * We only know about some specific relocation types. Luckily 8400Sstevel@tonic-gate * these types have the same values on both 32-bit and 64-bit 8410Sstevel@tonic-gate * x86 architectures. 8420Sstevel@tonic-gate */ 8430Sstevel@tonic-gate if (GELF_R_TYPE(rela->r_info) != R_386_PC32 && 8440Sstevel@tonic-gate GELF_R_TYPE(rela->r_info) != R_386_PLT32) 8450Sstevel@tonic-gate return (-1); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /* 8480Sstevel@tonic-gate * We may have already processed this object file in an earlier 8490Sstevel@tonic-gate * linker invocation in which case we'd expect to see a bunch 8500Sstevel@tonic-gate * of nops; return success in that case. 8510Sstevel@tonic-gate */ 8520Sstevel@tonic-gate if (ip[0] == DT_OP_NOP && ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP && 8530Sstevel@tonic-gate ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) 8540Sstevel@tonic-gate return (0); 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate /* 8570Sstevel@tonic-gate * We only expect a call instrution with a 32-bit displacement. 8580Sstevel@tonic-gate */ 8590Sstevel@tonic-gate if (ip[0] != DT_OP_CALL) { 8600Sstevel@tonic-gate dt_dprintf("found %x instead of a call instruction at %llx\n", 8610Sstevel@tonic-gate ip[0], (u_longlong_t)rela->r_offset); 8620Sstevel@tonic-gate return (-1); 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate ip[0] = DT_OP_NOP; 8660Sstevel@tonic-gate ip[1] = DT_OP_NOP; 8670Sstevel@tonic-gate ip[2] = DT_OP_NOP; 8680Sstevel@tonic-gate ip[3] = DT_OP_NOP; 8690Sstevel@tonic-gate ip[4] = DT_OP_NOP; 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate return (0); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate #else 8750Sstevel@tonic-gate #error unknown ISA 8760Sstevel@tonic-gate #endif 8770Sstevel@tonic-gate 878*1239Sahl /*PRINTFLIKE5*/ 8790Sstevel@tonic-gate static int 880*1239Sahl dt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs, 881*1239Sahl const char *format, ...) 8820Sstevel@tonic-gate { 8830Sstevel@tonic-gate va_list ap; 884*1239Sahl dt_link_pair_t *pair; 8850Sstevel@tonic-gate 8860Sstevel@tonic-gate va_start(ap, format); 8870Sstevel@tonic-gate dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap); 8880Sstevel@tonic-gate va_end(ap); 8890Sstevel@tonic-gate 890265Smws if (elf != NULL) 891265Smws (void) elf_end(elf); 892265Smws 893*1239Sahl if (fd >= 0) 894*1239Sahl (void) close(fd); 895*1239Sahl 896*1239Sahl while ((pair = bufs) != NULL) { 897*1239Sahl bufs = pair->dlp_next; 898*1239Sahl dt_free(dtp, pair->dlp_str); 899*1239Sahl dt_free(dtp, pair->dlp_sym); 900*1239Sahl dt_free(dtp, pair); 901*1239Sahl } 902*1239Sahl 9030Sstevel@tonic-gate return (dt_set_errno(dtp, EDT_COMPILER)); 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate static int 9070Sstevel@tonic-gate process_obj(dtrace_hdl_t *dtp, const char *obj) 9080Sstevel@tonic-gate { 9090Sstevel@tonic-gate static const char dt_prefix[] = "__dtrace_"; 910*1239Sahl static const char dt_symprefix[] = "$dtrace"; 911*1239Sahl static const char dt_symfmt[] = "%s%d.%s"; 9120Sstevel@tonic-gate int fd, i, ndx, mod = 0; 913265Smws Elf *elf = NULL; 9140Sstevel@tonic-gate GElf_Ehdr ehdr; 915*1239Sahl Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt; 916*1239Sahl Elf_Data *data_rel, *data_sym, *data_str, *data_tgt; 917*1239Sahl GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt; 918*1239Sahl GElf_Sym rsym, fsym, dsym; 9190Sstevel@tonic-gate GElf_Rela rela; 920*1239Sahl char *s, *p, *r; 9210Sstevel@tonic-gate char pname[DTRACE_PROVNAMELEN]; 9220Sstevel@tonic-gate dt_provider_t *pvp; 9230Sstevel@tonic-gate dt_probe_t *prp; 9240Sstevel@tonic-gate uint32_t off, eclass, emachine1, emachine2; 925*1239Sahl size_t count_sym, count_str, symsize; 926*1239Sahl key_t objkey; 927*1239Sahl dt_link_pair_t *pair, *bufs = NULL; 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate if ((fd = open64(obj, O_RDWR)) == -1) { 930*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 931*1239Sahl "failed to open %s: %s", obj, strerror(errno))); 9320Sstevel@tonic-gate } 9330Sstevel@tonic-gate 934265Smws if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) { 935*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 936*1239Sahl "failed to process %s: %s", obj, elf_errmsg(elf_errno()))); 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate switch (elf_kind(elf)) { 9400Sstevel@tonic-gate case ELF_K_ELF: 9410Sstevel@tonic-gate break; 9420Sstevel@tonic-gate case ELF_K_AR: 943*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, "archives are not " 944*1239Sahl "permitted; use the contents of the archive instead: %s", 945*1239Sahl obj)); 9460Sstevel@tonic-gate default: 947*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 948*1239Sahl "invalid file type: %s", obj)); 9490Sstevel@tonic-gate } 9500Sstevel@tonic-gate 951*1239Sahl if (gelf_getehdr(elf, &ehdr) == NULL) { 952*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s", 953*1239Sahl obj)); 954*1239Sahl } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate if (dtp->dt_oflags & DTRACE_O_LP64) { 9570Sstevel@tonic-gate eclass = ELFCLASS64; 9580Sstevel@tonic-gate #if defined(__sparc) 9590Sstevel@tonic-gate emachine1 = emachine2 = EM_SPARCV9; 9600Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 9610Sstevel@tonic-gate emachine1 = emachine2 = EM_AMD64; 9620Sstevel@tonic-gate #endif 963*1239Sahl symsize = sizeof (Elf64_Sym); 9640Sstevel@tonic-gate } else { 9650Sstevel@tonic-gate eclass = ELFCLASS32; 9660Sstevel@tonic-gate #if defined(__sparc) 9670Sstevel@tonic-gate emachine1 = EM_SPARC; 9680Sstevel@tonic-gate emachine2 = EM_SPARC32PLUS; 9690Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64) 9700Sstevel@tonic-gate emachine1 = emachine2 = EM_386; 9710Sstevel@tonic-gate #endif 972*1239Sahl symsize = sizeof (Elf32_Sym); 9730Sstevel@tonic-gate } 9740Sstevel@tonic-gate 975265Smws if (ehdr.e_ident[EI_CLASS] != eclass) { 976*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 977265Smws "incorrect ELF class for object file: %s", obj)); 978265Smws } 9790Sstevel@tonic-gate 980*1239Sahl if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) { 981*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 982*1239Sahl "incorrect ELF machine type for object file: %s", obj)); 983*1239Sahl } 984*1239Sahl 985*1239Sahl /* 986*1239Sahl * We use this token as a relatively unique handle for this file on the 987*1239Sahl * system in order to disambiguate potential conflicts between files of 988*1239Sahl * the same name which contain identially named local symbols. 989*1239Sahl */ 990*1239Sahl if ((objkey = ftok(obj, 0)) == (key_t)-1) { 991*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 992*1239Sahl "failed to generate unique key for object file: %s", obj)); 993*1239Sahl } 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate scn_rel = NULL; 9960Sstevel@tonic-gate while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) { 9970Sstevel@tonic-gate if (gelf_getshdr(scn_rel, &shdr_rel) == NULL) 9980Sstevel@tonic-gate goto err; 9990Sstevel@tonic-gate 1000*1239Sahl /* 1001*1239Sahl * Skip any non-relocation sections. 1002*1239Sahl */ 10030Sstevel@tonic-gate if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL) 10040Sstevel@tonic-gate continue; 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL) 10070Sstevel@tonic-gate goto err; 10080Sstevel@tonic-gate 1009*1239Sahl /* 1010*1239Sahl * Grab the section, section header and section data for the 1011*1239Sahl * symbol table that this relocation section references. 1012*1239Sahl */ 10130Sstevel@tonic-gate if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL || 10140Sstevel@tonic-gate gelf_getshdr(scn_sym, &shdr_sym) == NULL || 10150Sstevel@tonic-gate (data_sym = elf_getdata(scn_sym, NULL)) == NULL) 10160Sstevel@tonic-gate goto err; 10170Sstevel@tonic-gate 1018*1239Sahl /* 1019*1239Sahl * Ditto for that symbol table's string table. 1020*1239Sahl */ 1021*1239Sahl if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL || 1022*1239Sahl gelf_getshdr(scn_str, &shdr_str) == NULL || 1023*1239Sahl (data_str = elf_getdata(scn_str, NULL)) == NULL) 1024*1239Sahl goto err; 1025*1239Sahl 1026*1239Sahl /* 1027*1239Sahl * Grab the section, section header and section data for the 1028*1239Sahl * target section for the relocations. For the relocations 1029*1239Sahl * we're looking for -- this will typically be the text of the 1030*1239Sahl * object file. 1031*1239Sahl */ 10320Sstevel@tonic-gate if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL || 10330Sstevel@tonic-gate gelf_getshdr(scn_tgt, &shdr_tgt) == NULL || 10340Sstevel@tonic-gate (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL) 10350Sstevel@tonic-gate goto err; 10360Sstevel@tonic-gate 1037*1239Sahl /* 1038*1239Sahl * We're looking for relocations to symbols matching this form: 1039*1239Sahl * 1040*1239Sahl * __dtrace_<prov>___<probe> 1041*1239Sahl * 1042*1239Sahl * For the generated object, we need to record the location 1043*1239Sahl * identified by the relocation, and create a new relocation 1044*1239Sahl * in the generated object that will be resolved at link time 1045*1239Sahl * to the location of the function in which the probe is 1046*1239Sahl * embedded. In the target object, we change the matched symbol 1047*1239Sahl * so that it will be ignored at link time, and we modify the 1048*1239Sahl * target (text) section to replace the call instruction with 1049*1239Sahl * one or more nops. 1050*1239Sahl * 1051*1239Sahl * If the function containing the probe is locally scoped 1052*1239Sahl * (static), we create an alias used by the relocation in the 1053*1239Sahl * generated object. The alias, a new symbol, will be global 1054*1239Sahl * (so that the relocation from the generated object can be 1055*1239Sahl * resolved), and hidden (so that it is converted to a local 1056*1239Sahl * symbol at link time). Such aliases have this form: 1057*1239Sahl * 1058*1239Sahl * $dtrace<key>.<function> 1059*1239Sahl * 1060*1239Sahl * We take a first pass through all the relocations to 1061*1239Sahl * calculate an upper bound on the number of symbols we may 1062*1239Sahl * need to add as well as the size of the strings we may need 1063*1239Sahl * to add to the string table for those symbols. 1064*1239Sahl */ 1065*1239Sahl count_sym = count_str = 0; 10660Sstevel@tonic-gate for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) { 10670Sstevel@tonic-gate 10680Sstevel@tonic-gate if (shdr_rel.sh_type == SHT_RELA) { 10690Sstevel@tonic-gate if (gelf_getrela(data_rel, i, &rela) == NULL) 10700Sstevel@tonic-gate continue; 10710Sstevel@tonic-gate } else { 1072*1239Sahl GElf_Rel rel; 1073*1239Sahl if (gelf_getrel(data_rel, i, &rel) == NULL) 1074*1239Sahl continue; 1075*1239Sahl rela.r_offset = rel.r_offset; 1076*1239Sahl rela.r_info = rel.r_info; 1077*1239Sahl rela.r_addend = 0; 1078*1239Sahl } 1079*1239Sahl 1080*1239Sahl if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info), 1081*1239Sahl &rsym) == NULL) 1082*1239Sahl goto err; 1083*1239Sahl 1084*1239Sahl s = (char *)data_str->d_buf + rsym.st_name; 1085*1239Sahl 1086*1239Sahl if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0) 1087*1239Sahl continue; 1088*1239Sahl 1089*1239Sahl if (dt_symtab_lookup(data_sym, rela.r_offset, 1090*1239Sahl shdr_rel.sh_info, &fsym) != 0) 1091*1239Sahl goto err; 1092*1239Sahl 1093*1239Sahl if (GELF_ST_BIND(fsym.st_info) != STB_LOCAL) 1094*1239Sahl continue; 1095*1239Sahl 1096*1239Sahl if (fsym.st_name > data_str->d_size) 1097*1239Sahl goto err; 1098*1239Sahl 1099*1239Sahl s = (char *)data_str->d_buf + fsym.st_name; 1100*1239Sahl 1101*1239Sahl /* 1102*1239Sahl * If this symbol isn't of type function, we've really 1103*1239Sahl * driven off the rails or the object file is corrupt. 1104*1239Sahl */ 1105*1239Sahl if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) { 1106*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 1107*1239Sahl "expected %s to be of type function", s)); 1108*1239Sahl } 1109*1239Sahl 1110*1239Sahl count_sym++; 1111*1239Sahl count_str += 1 + snprintf(NULL, 0, dt_symfmt, 1112*1239Sahl dt_symprefix, objkey, s); 1113*1239Sahl } 1114*1239Sahl 1115*1239Sahl /* 1116*1239Sahl * If needed, allocate the additional space for the symbol 1117*1239Sahl * table and string table copying the old data into the new 1118*1239Sahl * buffers, and marking the buffers as dirty. We inject those 1119*1239Sahl * newly allocated buffers into the libelf data structures, but 1120*1239Sahl * are still responsible for freeing them once we're done with 1121*1239Sahl * the elf handle. 1122*1239Sahl */ 1123*1239Sahl if (count_sym > 0) { 1124*1239Sahl assert(count_str > 0); 1125*1239Sahl 1126*1239Sahl if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL) 1127*1239Sahl goto err; 1128*1239Sahl 1129*1239Sahl if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size + 1130*1239Sahl count_str)) == NULL) { 1131*1239Sahl dt_free(dtp, pair); 1132*1239Sahl goto err; 1133*1239Sahl } 1134*1239Sahl 1135*1239Sahl if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size + 1136*1239Sahl count_sym * symsize)) == NULL) { 1137*1239Sahl dt_free(dtp, pair->dlp_str); 1138*1239Sahl dt_free(dtp, pair); 1139*1239Sahl goto err; 1140*1239Sahl } 1141*1239Sahl 1142*1239Sahl pair->dlp_next = bufs; 1143*1239Sahl bufs = pair; 1144*1239Sahl 1145*1239Sahl bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size); 1146*1239Sahl data_str->d_buf = pair->dlp_str; 1147*1239Sahl data_str->d_size += count_str; 1148*1239Sahl (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY); 1149*1239Sahl 1150*1239Sahl shdr_str.sh_size += count_str; 1151*1239Sahl (void) gelf_update_shdr(scn_str, &shdr_str); 1152*1239Sahl 1153*1239Sahl bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size); 1154*1239Sahl data_sym->d_buf = pair->dlp_sym; 1155*1239Sahl data_sym->d_size += count_sym * symsize; 1156*1239Sahl (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY); 1157*1239Sahl 1158*1239Sahl shdr_sym.sh_size += count_sym * symsize; 1159*1239Sahl (void) gelf_update_shdr(scn_sym, &shdr_sym); 1160*1239Sahl } 1161*1239Sahl 1162*1239Sahl count_str = shdr_str.sh_size - count_str; 1163*1239Sahl count_sym = data_sym->d_size / symsize - count_sym; 1164*1239Sahl 1165*1239Sahl /* 1166*1239Sahl * Now that the tables have been allocated, perform the 1167*1239Sahl * modifications described above. 1168*1239Sahl */ 1169*1239Sahl for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) { 1170*1239Sahl 1171*1239Sahl if (shdr_rel.sh_type == SHT_RELA) { 1172*1239Sahl if (gelf_getrela(data_rel, i, &rela) == NULL) 1173*1239Sahl continue; 1174*1239Sahl } else { 1175*1239Sahl GElf_Rel rel; 11760Sstevel@tonic-gate if (gelf_getrel(data_rel, i, &rel) == NULL) 11770Sstevel@tonic-gate continue; 11780Sstevel@tonic-gate rela.r_offset = rel.r_offset; 11790Sstevel@tonic-gate rela.r_info = rel.r_info; 11800Sstevel@tonic-gate rela.r_addend = 0; 11810Sstevel@tonic-gate } 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate ndx = GELF_R_SYM(rela.r_info); 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate if (gelf_getsym(data_sym, ndx, &rsym) == NULL || 1186*1239Sahl rsym.st_name > data_str->d_size) 11870Sstevel@tonic-gate goto err; 11880Sstevel@tonic-gate 1189*1239Sahl s = (char *)data_str->d_buf + rsym.st_name; 1190*1239Sahl 11910Sstevel@tonic-gate if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0) 11920Sstevel@tonic-gate continue; 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate s += sizeof (dt_prefix) - 1; 11950Sstevel@tonic-gate if ((p = strstr(s, "___")) == NULL || 11960Sstevel@tonic-gate p - s >= sizeof (pname)) 11970Sstevel@tonic-gate goto err; 11980Sstevel@tonic-gate 1199*1239Sahl bcopy(s, pname, p - s); 12000Sstevel@tonic-gate pname[p - s] = '\0'; 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate p = strhyphenate(p + 3); /* strlen("___") */ 12030Sstevel@tonic-gate 1204*1239Sahl if (dt_symtab_lookup(data_sym, rela.r_offset, 1205*1239Sahl shdr_rel.sh_info, &fsym) != 0) 1206*1239Sahl goto err; 1207*1239Sahl 1208*1239Sahl if (fsym.st_name > data_str->d_size) 12090Sstevel@tonic-gate goto err; 12100Sstevel@tonic-gate 1211*1239Sahl assert(GELF_ST_TYPE(fsym.st_info) == STT_FUNC); 1212*1239Sahl 1213*1239Sahl /* 1214*1239Sahl * If a NULL relocation name is passed to 1215*1239Sahl * dt_probe_define(), the function name is used for the 1216*1239Sahl * relocation. The relocation needs to use a mangled 1217*1239Sahl * name if the symbol is locally scoped; the function 1218*1239Sahl * name may need to change if we've found the global 1219*1239Sahl * alias for the locally scoped symbol (we prefer 1220*1239Sahl * global symbols to locals in dt_symtab_lookup()). 1221*1239Sahl */ 1222*1239Sahl s = (char *)data_str->d_buf + fsym.st_name; 1223*1239Sahl r = NULL; 1224*1239Sahl 1225*1239Sahl if (GELF_ST_BIND(fsym.st_info) == STB_LOCAL) { 1226*1239Sahl dsym = fsym; 1227*1239Sahl dsym.st_name = count_str; 1228*1239Sahl dsym.st_info = GELF_ST_INFO(STB_GLOBAL, 1229*1239Sahl STT_FUNC); 1230*1239Sahl dsym.st_other = ELF64_ST_VISIBILITY(STV_HIDDEN); 1231*1239Sahl (void) gelf_update_sym(data_sym, count_sym, 1232*1239Sahl &dsym); 1233*1239Sahl 1234*1239Sahl r = (char *)data_str->d_buf + count_str; 1235*1239Sahl count_str += 1 + sprintf(r, dt_symfmt, 1236*1239Sahl dt_symprefix, objkey, s); 1237*1239Sahl count_sym++; 1238*1239Sahl 1239*1239Sahl } else if (strncmp(s, dt_symprefix, 1240*1239Sahl strlen(dt_symprefix)) == 0) { 1241*1239Sahl r = s; 1242*1239Sahl if ((s = strchr(s, '.')) == NULL) 1243*1239Sahl goto err; 1244*1239Sahl s++; 1245*1239Sahl } 1246*1239Sahl 12470Sstevel@tonic-gate if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) { 1248*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 12490Sstevel@tonic-gate "no such provider %s", pname)); 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate if ((prp = dt_probe_lookup(pvp, p)) == NULL) { 1253*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 12540Sstevel@tonic-gate "no such probe %s", p)); 12550Sstevel@tonic-gate } 12560Sstevel@tonic-gate 12570Sstevel@tonic-gate assert(fsym.st_value <= rela.r_offset); 12580Sstevel@tonic-gate 12590Sstevel@tonic-gate off = rela.r_offset - fsym.st_value; 12600Sstevel@tonic-gate if (dt_modtext(data_tgt->d_buf, &rela, &off) != 0) 12610Sstevel@tonic-gate goto err; 12620Sstevel@tonic-gate 1263*1239Sahl if (dt_probe_define(pvp, prp, s, r, off) != 0) { 1264*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 1265*1239Sahl "failed to allocate space for probe")); 1266*1239Sahl } 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate mod = 1; 1269*1239Sahl (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY); 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate /* 12720Sstevel@tonic-gate * This symbol may already have been marked to 12730Sstevel@tonic-gate * be ignored by another relocation referencing 12740Sstevel@tonic-gate * the same symbol or if this object file has 12750Sstevel@tonic-gate * already been processed by an earlier link 12760Sstevel@tonic-gate * invocation. 12770Sstevel@tonic-gate */ 12780Sstevel@tonic-gate if (rsym.st_shndx != SHN_SUNW_IGNORE) { 12790Sstevel@tonic-gate rsym.st_shndx = SHN_SUNW_IGNORE; 12800Sstevel@tonic-gate (void) gelf_update_sym(data_sym, ndx, &rsym); 12810Sstevel@tonic-gate } 12820Sstevel@tonic-gate } 1283*1239Sahl 1284*1239Sahl /* 1285*1239Sahl * The full buffer may not have been used so shrink them here 1286*1239Sahl * to match the sizes actually used. 1287*1239Sahl */ 1288*1239Sahl data_str->d_size = count_str; 1289*1239Sahl data_sym->d_size = count_sym * symsize; 12900Sstevel@tonic-gate } 12910Sstevel@tonic-gate 12920Sstevel@tonic-gate if (mod && elf_update(elf, ELF_C_WRITE) == -1) 12930Sstevel@tonic-gate goto err; 12940Sstevel@tonic-gate 1295265Smws (void) elf_end(elf); 1296*1239Sahl (void) close(fd); 1297*1239Sahl 1298*1239Sahl while ((pair = bufs) != NULL) { 1299*1239Sahl bufs = pair->dlp_next; 1300*1239Sahl dt_free(dtp, pair->dlp_str); 1301*1239Sahl dt_free(dtp, pair->dlp_sym); 1302*1239Sahl dt_free(dtp, pair); 1303*1239Sahl } 1304*1239Sahl 13050Sstevel@tonic-gate return (0); 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate err: 1308*1239Sahl return (dt_link_error(dtp, elf, fd, bufs, 13090Sstevel@tonic-gate "an error was encountered while processing %s", obj)); 13100Sstevel@tonic-gate } 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate int 13130Sstevel@tonic-gate dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags, 13140Sstevel@tonic-gate const char *file, int objc, char *const objv[]) 13150Sstevel@tonic-gate { 13160Sstevel@tonic-gate char drti[PATH_MAX]; 13170Sstevel@tonic-gate dof_hdr_t *dof; 1318191Sahl int fd, status, i, cur; 13190Sstevel@tonic-gate char *cmd, tmp; 13200Sstevel@tonic-gate size_t len; 13210Sstevel@tonic-gate int ret = 0; 13220Sstevel@tonic-gate 1323191Sahl /* 1324191Sahl * A NULL program indicates a special use in which we just link 1325191Sahl * together a bunch of object files specified in objv and then 1326191Sahl * unlink(2) those object files. 1327191Sahl */ 1328191Sahl if (pgp == NULL) { 1329191Sahl const char *fmt = "%s -o %s -r"; 1330191Sahl 1331191Sahl len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1; 1332191Sahl 1333191Sahl for (i = 0; i < objc; i++) 1334191Sahl len += strlen(objv[i]) + 1; 1335191Sahl 1336191Sahl cmd = alloca(len); 1337191Sahl 1338191Sahl cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file); 1339191Sahl 1340191Sahl for (i = 0; i < objc; i++) 1341191Sahl cur += snprintf(cmd + cur, len - cur, " %s", objv[i]); 1342191Sahl 1343191Sahl if ((status = system(cmd)) == -1) { 1344*1239Sahl return (dt_link_error(dtp, NULL, -1, NULL, 1345*1239Sahl "failed to run %s: %s", dtp->dt_ld_path, 1346*1239Sahl strerror(errno))); 1347191Sahl } 1348191Sahl 1349191Sahl if (WIFSIGNALED(status)) { 1350*1239Sahl return (dt_link_error(dtp, NULL, -1, NULL, 1351191Sahl "failed to link %s: %s failed due to signal %d", 1352191Sahl file, dtp->dt_ld_path, WTERMSIG(status))); 1353191Sahl } 1354191Sahl 1355191Sahl if (WEXITSTATUS(status) != 0) { 1356*1239Sahl return (dt_link_error(dtp, NULL, -1, NULL, 1357191Sahl "failed to link %s: %s exited with status %d\n", 1358191Sahl file, dtp->dt_ld_path, WEXITSTATUS(status))); 1359191Sahl } 1360191Sahl 1361191Sahl for (i = 0; i < objc; i++) { 1362191Sahl if (strcmp(objv[i], file) != 0) 1363191Sahl (void) unlink(objv[i]); 1364191Sahl } 1365191Sahl 1366191Sahl return (0); 1367191Sahl } 1368191Sahl 13690Sstevel@tonic-gate for (i = 0; i < objc; i++) { 13700Sstevel@tonic-gate if (process_obj(dtp, objv[i]) != 0) 13710Sstevel@tonic-gate return (-1); /* errno is set for us */ 13720Sstevel@tonic-gate } 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL) 13750Sstevel@tonic-gate return (-1); /* errno is set for us */ 13760Sstevel@tonic-gate 13770Sstevel@tonic-gate /* 13780Sstevel@tonic-gate * Create a temporary file and then unlink it if we're going to 13790Sstevel@tonic-gate * combine it with drti.o later. We can still refer to it in child 13800Sstevel@tonic-gate * processes as /dev/fd/<fd>. 13810Sstevel@tonic-gate */ 13820Sstevel@tonic-gate if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) { 1383*1239Sahl return (dt_link_error(dtp, NULL, -1, NULL, 13840Sstevel@tonic-gate "failed to open %s: %s", file, strerror(errno))); 13850Sstevel@tonic-gate } 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate /* 13880Sstevel@tonic-gate * If -xlinktype=DOF has been selected, just write out the DOF. 13890Sstevel@tonic-gate * Otherwise proceed to the default of generating and linking ELF. 13900Sstevel@tonic-gate */ 13910Sstevel@tonic-gate switch (dtp->dt_linktype) { 13920Sstevel@tonic-gate case DT_LTYP_DOF: 13930Sstevel@tonic-gate if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz) 13940Sstevel@tonic-gate ret = errno; 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate if (close(fd) != 0 && ret == 0) 13970Sstevel@tonic-gate ret = errno; 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate if (ret != 0) { 1400*1239Sahl return (dt_link_error(dtp, NULL, -1, NULL, 14010Sstevel@tonic-gate "failed to write %s: %s", file, strerror(ret))); 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate return (0); 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate case DT_LTYP_ELF: 14070Sstevel@tonic-gate break; /* fall through to the rest of dtrace_program_link() */ 14080Sstevel@tonic-gate 14090Sstevel@tonic-gate default: 1410*1239Sahl return (dt_link_error(dtp, NULL, -1, NULL, 14110Sstevel@tonic-gate "invalid link type %u\n", dtp->dt_linktype)); 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate 14150Sstevel@tonic-gate if (!dtp->dt_lazyload) 14160Sstevel@tonic-gate (void) unlink(file); 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate if (dtp->dt_oflags & DTRACE_O_LP64) 14190Sstevel@tonic-gate status = dump_elf64(dtp, dof, fd); 14200Sstevel@tonic-gate else 14210Sstevel@tonic-gate status = dump_elf32(dtp, dof, fd); 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate if (status != 0 || lseek(fd, 0, SEEK_SET) != 0) { 1424*1239Sahl return (dt_link_error(dtp, NULL, -1, NULL, 14250Sstevel@tonic-gate "failed to write %s: %s", file, strerror(errno))); 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate 14280Sstevel@tonic-gate if (!dtp->dt_lazyload) { 1429191Sahl const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s"; 1430191Sahl 14310Sstevel@tonic-gate if (dtp->dt_oflags & DTRACE_O_LP64) { 14320Sstevel@tonic-gate (void) snprintf(drti, sizeof (drti), 14330Sstevel@tonic-gate "%s/64/drti.o", _dtrace_libdir); 14340Sstevel@tonic-gate } else { 14350Sstevel@tonic-gate (void) snprintf(drti, sizeof (drti), 14360Sstevel@tonic-gate "%s/drti.o", _dtrace_libdir); 14370Sstevel@tonic-gate } 14380Sstevel@tonic-gate 1439191Sahl len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd, 1440191Sahl drti) + 1; 14410Sstevel@tonic-gate 14420Sstevel@tonic-gate cmd = alloca(len); 14430Sstevel@tonic-gate 1444191Sahl (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti); 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate if ((status = system(cmd)) == -1) { 1447*1239Sahl ret = dt_link_error(dtp, NULL, -1, NULL, 1448*1239Sahl "failed to run %s: %s", dtp->dt_ld_path, 1449*1239Sahl strerror(errno)); 14500Sstevel@tonic-gate goto done; 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate (void) close(fd); /* release temporary file */ 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate if (WIFSIGNALED(status)) { 1456*1239Sahl ret = dt_link_error(dtp, NULL, -1, NULL, 14570Sstevel@tonic-gate "failed to link %s: %s failed due to signal %d", 14580Sstevel@tonic-gate file, dtp->dt_ld_path, WTERMSIG(status)); 14590Sstevel@tonic-gate goto done; 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate 14620Sstevel@tonic-gate if (WEXITSTATUS(status) != 0) { 1463*1239Sahl ret = dt_link_error(dtp, NULL, -1, NULL, 14640Sstevel@tonic-gate "failed to link %s: %s exited with status %d\n", 14650Sstevel@tonic-gate file, dtp->dt_ld_path, WEXITSTATUS(status)); 14660Sstevel@tonic-gate goto done; 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate } else { 14690Sstevel@tonic-gate (void) close(fd); 14700Sstevel@tonic-gate } 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate done: 14730Sstevel@tonic-gate dtrace_dof_destroy(dtp, dof); 14740Sstevel@tonic-gate return (ret); 14750Sstevel@tonic-gate } 1476