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