14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER START
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The contents of this file are subject to the terms of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Common Development and Distribution License (the "License").
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You may not use this file except in compliance with the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * See the License for the specific language governing permissions
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and limitations under the License.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * When distributing Covered Code, include this CDDL HEADER in each
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If applicable, add the following below this CDDL HEADER, with the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * fields enclosed by brackets "[]" replaced with your own identifying
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * information: Portions Copyright [yyyy] [name of copyright owner]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * CDDL HEADER END
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Use is subject to license terms.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#pragma ident "%Z%%M% %I% %E% SMI"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (lseek64(fd, (off64_t)elf_file.shdr[(index)].sh_offset, SEEK_SET) != \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_write(dtp, fd, (data), elf_file.shdr[(index)].sh_size) != \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync struct dt_link_pair *dlp_next; /* next pair in linked list */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Rela *de_rel; /* array of relocations for sparc */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf32_Rel *de_rel; /* array of relocations for x86 */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync uint32_t de_global; /* index of the first global symbol */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprepare_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf32_t *dep)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * First compute the size of the string table and the number of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * relocations present in the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym = count + 1; /* the first symbol is always null */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf32_Sym))) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The first symbol table entry must be zeroed and is always ignored.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Take a second pass through the DOF sections filling in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * memory we allocated.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (j = 0; j < nrel; j++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Add 4 bytes to hit the low half of this 64-bit
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * big-endian address.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Add a symbol for the DOF itself. We use a different symbol for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * lazily and actively loaded DOF to make them easy to distinguish.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * First compute the size of the string table and the number of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * relocations present in the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dep->de_nsym = count + 1; /* the first symbol is always null */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The first symbol table entry must be zeroed and is always ignored.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Take a second pass through the DOF sections filling in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * memory we allocated.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (j = 0; j < nrel; j++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Add a symbol for the DOF itself. We use a different symbol for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * lazily and actively loaded DOF to make them easy to distinguish.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Write out an ELF32 file prologue consisting of a header, section headers,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and a section header string table. The DOF data will follow this prologue
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and complete the contents of the given ELF file.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If there are no relocations, we only need enough sections for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the shstrtab and the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Write out an ELF64 file prologue consisting of a header, section headers,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and a section header string table. The DOF data will follow this prologue
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * and complete the contents of the given ELF file.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If there are no relocations, we only need enough sections for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the shstrtab and the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_symtab_lookup(Elf_Data *data_sym, int nsym, uintptr_t addr, uint_t shn,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < nsym && gelf_getsym(data_sym, i, sym) != NULL; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_IS_MOV_O7(inst) (((inst) & 0xffffe000) == 0x9e100000)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_IS_RESTORE(inst) (((inst) & 0xc1f80000) == 0x81e80000)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_IS_RETL(inst) (((inst) & 0xfff83fff) == 0x81c02008)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#define DT_MAKE_RETL(reg) (0x81c02008 | ((reg) << 14))
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*ARGSUSED*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We only know about some specific relocation types.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (GELF_R_TYPE(rela->r_info) != R_SPARC_WDISP30 &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We may have already processed this object file in an earlier linker
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * invocation. Check to see if the present instruction sequence matches
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the one we would install below.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We only expect call instructions with a displacement of 0.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("found %x instead of a call instruction at %llx\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * It would necessarily indicate incorrect usage if an is-
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * enabled probe were tail-called so flag that as an error.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * It's also potentially (very) tricky to handle gracefully,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * but could be done if this were a desired use scenario.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("tail call to is-enabled probe at %llx\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * On SPARC, we take advantage of the fact that the first
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * argument shares the same register as for the return value.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The macro handles the work of zeroing that register so we
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * don't need to do anything special here. We instrument the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * instruction in the delay slot as we'll need to modify the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * return register after that instruction has been emulated.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If the call is followed by a restore, it's a tail call so
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * change the call to a ret. If the call if followed by a mov
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * of a register into %o7, it's a tail call in leaf context
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * so change the call to a retl-like instruction that returns
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * to that register value + 8 (rather than the typical %o7 +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * 8); the delay slot instruction is left, but should have no
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * effect. Otherwise we change the call to be a nop. We
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * identify the subsequent instruction as the probe point in
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * all but the leaf tail-call case to ensure that arguments to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the probe are complete and consistent. An astute, though
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * largely hypothetical, observer would note that there is the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * possibility of a false-positive probe firing if the function
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * contained a branch to the instruction in the delay slot of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the call. Fixing this would require significant in-kernel
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * modifications, and isn't worth doing until we see it in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * On x86, the first byte of the instruction is the call opcode and
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the next four bytes are the 32-bit address; the relocation is for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the address operand. We back up the offset to the first byte of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the instruction. For is-enabled probes, we later advance the offset
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * so that it hits the first nop in the instruction sequence.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We only know about some specific relocation types. Luckily
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * these types have the same values on both 32-bit and 64-bit
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * x86 architectures.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We may have already processed this object file in an earlier linker
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * invocation. Check to see if the present instruction sequence matches
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the one we would install. For is-enabled probes, we advance the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * offset to the first nop instruction in the sequence to match the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * text modification code below.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 &&
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We expect either a call instrution with a 32-bit displacement or a
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * jmp instruction with a 32-bit displacement acting as a tail-call.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_dprintf("found %x instead of a call or jmp instruction at "
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (-1);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Establish the instruction sequence -- all nops for probes, and an
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * instruction to clear the return value register (%eax/%rax) followed
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * by nops for is-enabled probes. For is-enabled probes, we advance
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the offset to the first nop. This isn't stricly necessary but makes
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * for more readable disassembly when the probe is enabled.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync/*PRINTFLIKE5*/
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync const char *format, ...)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprocess_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync Elf_Data *data_rel, *data_sym, *data_str, *data_tgt;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync char *s, *p, *r;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to process %s: %s", obj, elf_errmsg(elf_errno())));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs, "archives are not "
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "permitted; use the contents of the archive instead: %s",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "incorrect ELF machine type for object file: %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We use this token as a relatively unique handle for this file on the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * system in order to disambiguate potential conflicts between files of
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the same name which contain identially named local symbols.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to generate unique key for object file: %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Skip any non-relocation sections.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Grab the section, section header and section data for the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * symbol table that this relocation section references.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Ditto for that symbol table's string table.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Grab the section, section header and section data for the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * target section for the relocations. For the relocations
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * we're looking for -- this will typically be the text of the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * object file.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We're looking for relocations to symbols matching this form:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * __dtrace[enabled]_<prov>___<probe>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * For the generated object, we need to record the location
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * identified by the relocation, and create a new relocation
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * in the generated object that will be resolved at link time
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * to the location of the function in which the probe is
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * embedded. In the target object, we change the matched symbol
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * so that it will be ignored at link time, and we modify the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * target (text) section to replace the call instruction with
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * one or more nops.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If the function containing the probe is locally scoped
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * (static), we create an alias used by the relocation in the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * generated object. The alias, a new symbol, will be global
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * (so that the relocation from the generated object can be
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * resolved), and hidden (so that it is converted to a local
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * symbol at link time). Such aliases have this form:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * $dtrace<key>.<function>
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * We take a first pass through all the relocations to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * populate our string table and count the number of extra
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * symbols we'll require.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If this symbol isn't of type function, we've really
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * driven off the rails or the object file is corrupt.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "expected %s to be of type function", s));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If needed, allocate the additional space for the symbol
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * table and string table copying the old data into the new
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * buffers, and marking the buffers as dirty. We inject those
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * newly allocated buffers into the libelf data structures, but
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * are still responsible for freeing them once we're done with
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the elf handle.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * The first byte of the string table is reserved for
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the \0 entry.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size +
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Now that the tables have been allocated, perform the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * modifications described above.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Check to see if this is an 'is-enabled' check as
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * opposed to a normal probe.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (*s++ != '_')
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync p - s >= sizeof (pname))
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If a NULL relocation name is passed to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * dt_probe_define(), the function name is used for the
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * relocation. The relocation needs to use a mangled
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * name if the symbol is locally scoped; the function
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * name may need to change if we've found the global
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * alias for the locally scoped symbol (we prefer
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * global symbols to locals in dt_symtab_lookup()).
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "no such probe %s", p));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to allocate space for probe"));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * This symbol may already have been marked to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * be ignored by another relocation referencing
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * the same symbol or if this object file has
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * already been processed by an earlier link
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * invocation.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "an error was encountered while processing %s", obj));
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * A NULL program indicates a special use in which we just link
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * together a bunch of object files specified in objv and then
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * unlink(2) those object files.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cur += snprintf(cmd + cur, len - cur, " %s", objv[i]);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s failed due to signal %d",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s exited with status %d\n",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync for (i = 0; i < objc; i++) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If there are is-enabled probes then we need to force use of DOF
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * version 2.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Create a temporary file and then unlink it if we're going to
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * combine it with drti.o later. We can still refer to it in child
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * processes as /dev/fd/<fd>.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * If -xlinktype=DOF has been selected, just write out the DOF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync * Otherwise proceed to the default of generating and linking ELF.
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if (ret != 0) {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync return (0);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync break; /* fall through to the rest of dtrace_program_link() */
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd,
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti);
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s failed due to signal %d",
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync "failed to link %s: %s exited with status %d\n",