4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #define ELF_TARGET_ALL
32 #include <sys/types.h>
34 #include <sys/sysmacros.h>
36 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
59 #include <dt_provider.h>
60 #include <dt_program.h>
61 #include <dt_string.h>
64 #define ESHDR_SHSTRTAB 1
66 #define ESHDR_STRTAB 3
67 #define ESHDR_SYMTAB 4
71 #define PWRITE_SCN(index, data) \
72 (lseek64(fd, (off64_t)elf_file.shdr[(index)].sh_offset, SEEK_SET) != \
73 (off64_t)elf_file.shdr[(index)].sh_offset || \
74 dt_write(dtp, fd, (data), elf_file.shdr[(index)].sh_size) != \
75 elf_file.shdr[(index)].sh_size)
77 static const char DTRACE_SHSTRTAB32[] = "\0"
79 ".SUNW_dof\0" /* 11 */
83 ".rela.SUNW_dof"; /* 37 */
85 ".rel.SUNW_dof"; /* 37 */
88 static const char DTRACE_SHSTRTAB64[] = "\0"
90 ".SUNW_dof\0" /* 11 */
93 ".rela.SUNW_dof"; /* 37 */
95 static const char DOFSTR[] = "__SUNW_dof";
96 static const char DOFLAZYSTR[] = "___SUNW_dof";
98 typedef struct dt_link_pair {
99 struct dt_link_pair *dlp_next; /* next pair in linked list */
100 void *dlp_str; /* buffer for string table */
101 void *dlp_sym; /* buffer for symbol table */
104 typedef struct dof_elf32 {
105 uint32_t de_nrel; /* relocation count */
107 Elf32_Rela *de_rel; /* array of relocations for sparc */
109 Elf32_Rel *de_rel; /* array of relocations for x86 */
111 uint32_t de_nsym; /* symbol count */
112 Elf32_Sym *de_sym; /* array of symbols */
113 uint32_t de_strlen; /* size of of string table */
114 char *de_strtab; /* string table */
115 uint32_t de_global; /* index of the first global symbol */
119 prepare_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf32_t *dep)
122 dof_relohdr_t *dofrh;
123 dof_relodesc_t *dofr;
137 dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
140 * First compute the size of the string table and the number of
141 * relocations present in the DOF.
143 for (i = 0; i < dof->dofh_secnum; i++) {
144 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
148 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
150 s = &dofs[dofrh->dofr_strtab];
151 strtab = (char *)dof + s->dofs_offset;
152 assert(strtab[0] == '\0');
153 strtabsz += s->dofs_size - 1;
155 s = &dofs[dofrh->dofr_relsec];
157 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
158 count += s->dofs_size / s->dofs_entsize;
161 dep->de_strlen = strtabsz;
162 dep->de_nrel = count;
163 dep->de_nsym = count + 1; /* the first symbol is always null */
165 if (dtp->dt_lazyload) {
166 dep->de_strlen += sizeof (DOFLAZYSTR);
169 dep->de_strlen += sizeof (DOFSTR);
173 if ((dep->de_rel = calloc(dep->de_nrel,
174 sizeof (dep->de_rel[0]))) == NULL) {
175 return (dt_set_errno(dtp, EDT_NOMEM));
178 if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf32_Sym))) == NULL) {
180 return (dt_set_errno(dtp, EDT_NOMEM));
183 if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
186 return (dt_set_errno(dtp, EDT_NOMEM));
191 dep->de_strtab[0] = '\0';
197 * The first symbol table entry must be zeroed and is always ignored.
199 bzero(sym, sizeof (Elf32_Sym));
203 * Take a second pass through the DOF sections filling in the
204 * memory we allocated.
206 for (i = 0; i < dof->dofh_secnum; i++) {
207 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
211 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
213 s = &dofs[dofrh->dofr_strtab];
214 strtab = (char *)dof + s->dofs_offset;
215 bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
217 strtabsz += s->dofs_size - 1;
219 s = &dofs[dofrh->dofr_relsec];
221 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
222 nrel = s->dofs_size / s->dofs_entsize;
224 s = &dofs[dofrh->dofr_tgtsec];
226 for (j = 0; j < nrel; j++) {
229 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
230 #elif defined(__ia64__)
232 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
233 #elif defined(__i386) || defined(__amd64)
234 rel->r_offset = s->dofs_offset +
236 rel->r_info = ELF32_R_INFO(count + dep->de_global,
238 #elif defined(__mips__)
240 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
241 #elif defined(__powerpc__)
243 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
244 #elif defined(__sparc)
246 * Add 4 bytes to hit the low half of this 64-bit
247 * big-endian address.
249 rel->r_offset = s->dofs_offset +
250 dofr[j].dofr_offset + 4;
251 rel->r_info = ELF32_R_INFO(count + dep->de_global,
257 sym->st_name = base + dofr[j].dofr_name - 1;
260 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC);
262 sym->st_shndx = SHN_UNDEF;
271 * Add a symbol for the DOF itself. We use a different symbol for
272 * lazily and actively loaded DOF to make them easy to distinguish.
274 sym->st_name = strtabsz;
276 sym->st_size = dof->dofh_filesz;
277 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT);
279 sym->st_shndx = ESHDR_DOF;
282 if (dtp->dt_lazyload) {
283 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
284 sizeof (DOFLAZYSTR));
285 strtabsz += sizeof (DOFLAZYSTR);
287 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
288 strtabsz += sizeof (DOFSTR);
291 assert(count == dep->de_nrel);
292 assert(strtabsz == dep->de_strlen);
298 typedef struct dof_elf64 {
311 prepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep)
314 dof_relohdr_t *dofrh;
315 dof_relodesc_t *dofr;
325 dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
328 * First compute the size of the string table and the number of
329 * relocations present in the DOF.
331 for (i = 0; i < dof->dofh_secnum; i++) {
332 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
336 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
338 s = &dofs[dofrh->dofr_strtab];
339 strtab = (char *)dof + s->dofs_offset;
340 assert(strtab[0] == '\0');
341 strtabsz += s->dofs_size - 1;
343 s = &dofs[dofrh->dofr_relsec];
345 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
346 count += s->dofs_size / s->dofs_entsize;
349 dep->de_strlen = strtabsz;
350 dep->de_nrel = count;
351 dep->de_nsym = count + 1; /* the first symbol is always null */
353 if (dtp->dt_lazyload) {
354 dep->de_strlen += sizeof (DOFLAZYSTR);
357 dep->de_strlen += sizeof (DOFSTR);
361 if ((dep->de_rel = calloc(dep->de_nrel,
362 sizeof (dep->de_rel[0]))) == NULL) {
363 return (dt_set_errno(dtp, EDT_NOMEM));
366 if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) {
368 return (dt_set_errno(dtp, EDT_NOMEM));
371 if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
374 return (dt_set_errno(dtp, EDT_NOMEM));
379 dep->de_strtab[0] = '\0';
385 * The first symbol table entry must be zeroed and is always ignored.
387 bzero(sym, sizeof (Elf64_Sym));
391 * Take a second pass through the DOF sections filling in the
392 * memory we allocated.
394 for (i = 0; i < dof->dofh_secnum; i++) {
395 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
399 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
401 s = &dofs[dofrh->dofr_strtab];
402 strtab = (char *)dof + s->dofs_offset;
403 bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
405 strtabsz += s->dofs_size - 1;
407 s = &dofs[dofrh->dofr_relsec];
409 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
410 nrel = s->dofs_size / s->dofs_entsize;
412 s = &dofs[dofrh->dofr_tgtsec];
414 for (j = 0; j < nrel; j++) {
415 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
419 #elif defined(__ia64__)
421 #elif defined(__mips__)
423 #elif defined(__powerpc__)
425 #elif defined(__i386) || defined(__amd64)
426 rel->r_offset = s->dofs_offset +
428 rel->r_info = ELF64_R_INFO(count + dep->de_global,
430 #elif defined(__sparc)
431 rel->r_offset = s->dofs_offset +
433 rel->r_info = ELF64_R_INFO(count + dep->de_global,
440 sym->st_name = base + dofr[j].dofr_name - 1;
443 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
445 sym->st_shndx = SHN_UNDEF;
454 * Add a symbol for the DOF itself. We use a different symbol for
455 * lazily and actively loaded DOF to make them easy to distinguish.
457 sym->st_name = strtabsz;
459 sym->st_size = dof->dofh_filesz;
460 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
462 sym->st_shndx = ESHDR_DOF;
465 if (dtp->dt_lazyload) {
466 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
467 sizeof (DOFLAZYSTR));
468 strtabsz += sizeof (DOFLAZYSTR);
470 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
471 strtabsz += sizeof (DOFSTR);
474 assert(count == dep->de_nrel);
475 assert(strtabsz == dep->de_strlen);
481 * Write out an ELF32 file prologue consisting of a header, section headers,
482 * and a section header string table. The DOF data will follow this prologue
483 * and complete the contents of the given ELF file.
486 dump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
490 Elf32_Shdr shdr[ESHDR_NUM];
499 if (prepare_elf32(dtp, dof, &de) != 0)
500 return (-1); /* errno is set for us */
503 * If there are no relocations, we only need enough sections for
504 * the shstrtab and the DOF.
506 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
508 bzero(&elf_file, sizeof (elf_file));
510 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
511 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
512 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
513 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
514 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
515 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS32;
516 #if BYTE_ORDER == _BIG_ENDIAN
517 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
519 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
521 #if defined(__FreeBSD__)
522 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
524 elf_file.ehdr.e_type = ET_REL;
526 elf_file.ehdr.e_machine = EM_ARM;
527 #elif defined(__ia64__)
528 elf_file.ehdr.e_machine = EM_IA_64;
529 #elif defined(__mips__)
530 elf_file.ehdr.e_machine = EM_MIPS;
531 #elif defined(__powerpc__)
532 elf_file.ehdr.e_machine = EM_PPC;
533 #elif defined(__sparc)
534 elf_file.ehdr.e_machine = EM_SPARC;
535 #elif defined(__i386) || defined(__amd64)
536 elf_file.ehdr.e_machine = EM_386;
538 elf_file.ehdr.e_version = EV_CURRENT;
539 elf_file.ehdr.e_shoff = sizeof (Elf32_Ehdr);
540 elf_file.ehdr.e_ehsize = sizeof (Elf32_Ehdr);
541 elf_file.ehdr.e_phentsize = sizeof (Elf32_Phdr);
542 elf_file.ehdr.e_shentsize = sizeof (Elf32_Shdr);
543 elf_file.ehdr.e_shnum = nshdr;
544 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
545 off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr);
547 shp = &elf_file.shdr[ESHDR_SHSTRTAB];
548 shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */
549 shp->sh_type = SHT_STRTAB;
550 shp->sh_offset = off;
551 shp->sh_size = sizeof (DTRACE_SHSTRTAB32);
552 shp->sh_addralign = sizeof (char);
553 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
555 shp = &elf_file.shdr[ESHDR_DOF];
556 shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */
557 shp->sh_flags = SHF_ALLOC;
558 shp->sh_type = SHT_SUNW_dof;
559 shp->sh_offset = off;
560 shp->sh_size = dof->dofh_filesz;
561 shp->sh_addralign = 8;
562 off = shp->sh_offset + shp->sh_size;
564 shp = &elf_file.shdr[ESHDR_STRTAB];
565 shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */
566 shp->sh_flags = SHF_ALLOC;
567 shp->sh_type = SHT_STRTAB;
568 shp->sh_offset = off;
569 shp->sh_size = de.de_strlen;
570 shp->sh_addralign = sizeof (char);
571 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
573 shp = &elf_file.shdr[ESHDR_SYMTAB];
574 shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */
575 shp->sh_flags = SHF_ALLOC;
576 shp->sh_type = SHT_SYMTAB;
577 shp->sh_entsize = sizeof (Elf32_Sym);
578 shp->sh_link = ESHDR_STRTAB;
579 shp->sh_offset = off;
580 shp->sh_info = de.de_global;
581 shp->sh_size = de.de_nsym * sizeof (Elf32_Sym);
582 shp->sh_addralign = 4;
583 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
585 if (de.de_nrel == 0) {
586 if (dt_write(dtp, fd, &elf_file,
587 sizeof (elf_file)) != sizeof (elf_file) ||
588 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
589 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
590 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
591 PWRITE_SCN(ESHDR_DOF, dof)) {
592 ret = dt_set_errno(dtp, errno);
595 shp = &elf_file.shdr[ESHDR_REL];
596 shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */
597 shp->sh_flags = SHF_ALLOC;
599 shp->sh_type = SHT_RELA;
601 shp->sh_type = SHT_REL;
603 shp->sh_entsize = sizeof (de.de_rel[0]);
604 shp->sh_link = ESHDR_SYMTAB;
605 shp->sh_info = ESHDR_DOF;
606 shp->sh_offset = off;
607 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
608 shp->sh_addralign = 4;
610 if (dt_write(dtp, fd, &elf_file,
611 sizeof (elf_file)) != sizeof (elf_file) ||
612 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
613 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
614 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
615 PWRITE_SCN(ESHDR_REL, de.de_rel) ||
616 PWRITE_SCN(ESHDR_DOF, dof)) {
617 ret = dt_set_errno(dtp, errno);
629 * Write out an ELF64 file prologue consisting of a header, section headers,
630 * and a section header string table. The DOF data will follow this prologue
631 * and complete the contents of the given ELF file.
634 dump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
638 Elf64_Shdr shdr[ESHDR_NUM];
647 if (prepare_elf64(dtp, dof, &de) != 0)
648 return (-1); /* errno is set for us */
651 * If there are no relocations, we only need enough sections for
652 * the shstrtab and the DOF.
654 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
656 bzero(&elf_file, sizeof (elf_file));
658 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
659 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
660 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
661 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
662 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
663 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS64;
664 #if BYTE_ORDER == _BIG_ENDIAN
665 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
667 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
669 #if defined(__FreeBSD__)
670 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
672 elf_file.ehdr.e_type = ET_REL;
674 elf_file.ehdr.e_machine = EM_ARM;
675 #elif defined(__ia64__)
676 elf_file.ehdr.e_machine = EM_IA_64;
677 #elif defined(__mips__)
678 elf_file.ehdr.e_machine = EM_MIPS;
679 #elif defined(__powerpc__)
680 elf_file.ehdr.e_machine = EM_PPC;
681 #elif defined(__sparc)
682 elf_file.ehdr.e_machine = EM_SPARCV9;
683 #elif defined(__i386) || defined(__amd64)
684 elf_file.ehdr.e_machine = EM_AMD64;
686 elf_file.ehdr.e_version = EV_CURRENT;
687 elf_file.ehdr.e_shoff = sizeof (Elf64_Ehdr);
688 elf_file.ehdr.e_ehsize = sizeof (Elf64_Ehdr);
689 elf_file.ehdr.e_phentsize = sizeof (Elf64_Phdr);
690 elf_file.ehdr.e_shentsize = sizeof (Elf64_Shdr);
691 elf_file.ehdr.e_shnum = nshdr;
692 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
693 off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr);
695 shp = &elf_file.shdr[ESHDR_SHSTRTAB];
696 shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */
697 shp->sh_type = SHT_STRTAB;
698 shp->sh_offset = off;
699 shp->sh_size = sizeof (DTRACE_SHSTRTAB64);
700 shp->sh_addralign = sizeof (char);
701 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
703 shp = &elf_file.shdr[ESHDR_DOF];
704 shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */
705 shp->sh_flags = SHF_ALLOC;
706 shp->sh_type = SHT_SUNW_dof;
707 shp->sh_offset = off;
708 shp->sh_size = dof->dofh_filesz;
709 shp->sh_addralign = 8;
710 off = shp->sh_offset + shp->sh_size;
712 shp = &elf_file.shdr[ESHDR_STRTAB];
713 shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */
714 shp->sh_flags = SHF_ALLOC;
715 shp->sh_type = SHT_STRTAB;
716 shp->sh_offset = off;
717 shp->sh_size = de.de_strlen;
718 shp->sh_addralign = sizeof (char);
719 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
721 shp = &elf_file.shdr[ESHDR_SYMTAB];
722 shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */
723 shp->sh_flags = SHF_ALLOC;
724 shp->sh_type = SHT_SYMTAB;
725 shp->sh_entsize = sizeof (Elf64_Sym);
726 shp->sh_link = ESHDR_STRTAB;
727 shp->sh_offset = off;
728 shp->sh_info = de.de_global;
729 shp->sh_size = de.de_nsym * sizeof (Elf64_Sym);
730 shp->sh_addralign = 8;
731 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
733 if (de.de_nrel == 0) {
734 if (dt_write(dtp, fd, &elf_file,
735 sizeof (elf_file)) != sizeof (elf_file) ||
736 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
737 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
738 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
739 PWRITE_SCN(ESHDR_DOF, dof)) {
740 ret = dt_set_errno(dtp, errno);
743 shp = &elf_file.shdr[ESHDR_REL];
744 shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */
745 shp->sh_flags = SHF_ALLOC;
746 shp->sh_type = SHT_RELA;
747 shp->sh_entsize = sizeof (de.de_rel[0]);
748 shp->sh_link = ESHDR_SYMTAB;
749 shp->sh_info = ESHDR_DOF;
750 shp->sh_offset = off;
751 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
752 shp->sh_addralign = 8;
754 if (dt_write(dtp, fd, &elf_file,
755 sizeof (elf_file)) != sizeof (elf_file) ||
756 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
757 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
758 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
759 PWRITE_SCN(ESHDR_REL, de.de_rel) ||
760 PWRITE_SCN(ESHDR_DOF, dof)) {
761 ret = dt_set_errno(dtp, errno);
773 dt_symtab_lookup(Elf_Data *data_sym, int nsym, uintptr_t addr, uint_t shn,
779 for (i = 0; i < nsym && gelf_getsym(data_sym, i, sym) != NULL; i++) {
780 if (GELF_ST_TYPE(sym->st_info) == STT_FUNC &&
781 shn == sym->st_shndx &&
782 sym->st_value <= addr &&
783 addr < sym->st_value + sym->st_size) {
784 if (GELF_ST_BIND(sym->st_info) == STB_GLOBAL)
800 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
803 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
806 #elif defined(__ia64__)
809 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
812 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
815 #elif defined(__mips__)
818 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
821 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
824 #elif defined(__powerpc__)
827 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
830 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
834 #elif defined(__sparc)
836 #define DT_OP_RET 0x81c7e008
837 #define DT_OP_NOP 0x01000000
838 #define DT_OP_CALL 0x40000000
839 #define DT_OP_CLR_O0 0x90102000
841 #define DT_IS_MOV_O7(inst) (((inst) & 0xffffe000) == 0x9e100000)
842 #define DT_IS_RESTORE(inst) (((inst) & 0xc1f80000) == 0x81e80000)
843 #define DT_IS_RETL(inst) (((inst) & 0xfff83fff) == 0x81c02008)
845 #define DT_RS2(inst) ((inst) & 0x1f)
846 #define DT_MAKE_RETL(reg) (0x81c02008 | ((reg) << 14))
850 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
855 if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
859 ip = (uint32_t *)(p + rela->r_offset);
862 * We only know about some specific relocation types.
864 if (GELF_R_TYPE(rela->r_info) != R_SPARC_WDISP30 &&
865 GELF_R_TYPE(rela->r_info) != R_SPARC_WPLT30)
869 * We may have already processed this object file in an earlier linker
870 * invocation. Check to see if the present instruction sequence matches
871 * the one we would install.
874 if (ip[0] == DT_OP_CLR_O0)
877 if (DT_IS_RESTORE(ip[1])) {
878 if (ip[0] == DT_OP_RET)
880 } else if (DT_IS_MOV_O7(ip[1])) {
881 if (DT_IS_RETL(ip[0]))
884 if (ip[0] == DT_OP_NOP) {
885 (*off) += sizeof (ip[0]);
892 * We only expect call instructions with a displacement of 0.
894 if (ip[0] != DT_OP_CALL) {
895 dt_dprintf("found %x instead of a call instruction at %llx\n",
896 ip[0], (u_longlong_t)rela->r_offset);
902 * It would necessarily indicate incorrect usage if an is-
903 * enabled probe were tail-called so flag that as an error.
904 * It's also potentially (very) tricky to handle gracefully,
905 * but could be done if this were a desired use scenario.
907 if (DT_IS_RESTORE(ip[1]) || DT_IS_MOV_O7(ip[1])) {
908 dt_dprintf("tail call to is-enabled probe at %llx\n",
909 (u_longlong_t)rela->r_offset);
913 ip[0] = DT_OP_CLR_O0;
916 * If the call is followed by a restore, it's a tail call so
917 * change the call to a ret. If the call if followed by a mov
918 * of a register into %o7, it's a tail call in leaf context
919 * so change the call to a retl-like instruction that returns
920 * to that register value + 8 (rather than the typical %o7 +
921 * 8); the delay slot instruction is left, but should have no
922 * effect. Otherwise we change the call to be a nop. In the
923 * first and the last case we adjust the offset to land on what
924 * was once the delay slot of the call so we correctly get all
925 * the arguments as they would have been passed in a normal
928 if (DT_IS_RESTORE(ip[1])) {
930 (*off) += sizeof (ip[0]);
931 } else if (DT_IS_MOV_O7(ip[1])) {
932 ip[0] = DT_MAKE_RETL(DT_RS2(ip[1]));
935 (*off) += sizeof (ip[0]);
942 #elif defined(__i386) || defined(__amd64)
944 #define DT_OP_NOP 0x90
945 #define DT_OP_RET 0xc3
946 #define DT_OP_CALL 0xe8
947 #define DT_OP_JMP32 0xe9
948 #define DT_OP_REX_RAX 0x48
949 #define DT_OP_XOR_EAX_0 0x33
950 #define DT_OP_XOR_EAX_1 0xc0
953 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
956 uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1);
960 * On x86, the first byte of the instruction is the call opcode and
961 * the next four bytes are the 32-bit address; the relocation is for
962 * the address operand. We back up the offset to the first byte of
963 * the instruction. For is-enabled probes, we later advance the offset
964 * so that it hits the first nop in the instruction sequence.
969 * We only know about some specific relocation types. Luckily
970 * these types have the same values on both 32-bit and 64-bit
973 if (GELF_R_TYPE(rela->r_info) != R_386_PC32 &&
974 GELF_R_TYPE(rela->r_info) != R_386_PLT32)
978 * We may have already processed this object file in an earlier linker
979 * invocation. Check to see if the present instruction sequence matches
980 * the one we would install. For is-enabled probes, we advance the
981 * offset to the first nop instruction in the sequence to match the
982 * text modification code below.
985 if ((ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET) &&
986 ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP &&
987 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP)
989 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
990 if (ip[0] == DT_OP_REX_RAX &&
991 ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 &&
992 (ip[3] == DT_OP_NOP || ip[3] == DT_OP_RET) &&
993 ip[4] == DT_OP_NOP) {
998 if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 &&
999 (ip[2] == DT_OP_NOP || ip[2] == DT_OP_RET) &&
1000 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) {
1007 * We expect either a call instrution with a 32-bit displacement or a
1008 * jmp instruction with a 32-bit displacement acting as a tail-call.
1010 if (ip[0] != DT_OP_CALL && ip[0] != DT_OP_JMP32) {
1011 dt_dprintf("found %x instead of a call or jmp instruction at "
1012 "%llx\n", ip[0], (u_longlong_t)rela->r_offset);
1016 ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP;
1019 * Establish the instruction sequence -- all nops for probes, and an
1020 * instruction to clear the return value register (%eax/%rax) followed
1021 * by nops for is-enabled probes. For is-enabled probes, we advance
1022 * the offset to the first nop. This isn't stricly necessary but makes
1023 * for more readable disassembly when the probe is enabled.
1031 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
1032 ip[0] = DT_OP_REX_RAX;
1033 ip[1] = DT_OP_XOR_EAX_0;
1034 ip[2] = DT_OP_XOR_EAX_1;
1039 ip[0] = DT_OP_XOR_EAX_0;
1040 ip[1] = DT_OP_XOR_EAX_1;
1056 dt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs,
1057 const char *format, ...)
1060 dt_link_pair_t *pair;
1062 va_start(ap, format);
1063 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1067 (void) elf_end(elf);
1072 while ((pair = bufs) != NULL) {
1073 bufs = pair->dlp_next;
1074 dt_free(dtp, pair->dlp_str);
1075 dt_free(dtp, pair->dlp_sym);
1079 return (dt_set_errno(dtp, EDT_COMPILER));
1083 process_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp)
1085 static const char dt_prefix[] = "__dtrace";
1086 static const char dt_enabled[] = "enabled";
1087 static const char dt_symprefix[] = "$dtrace";
1088 static const char dt_symfmt[] = "%s%d.%s";
1089 int fd, i, ndx, eprobe, mod = 0;
1092 Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt;
1093 Elf_Data *data_rel, *data_sym, *data_str, *data_tgt;
1094 GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt;
1095 GElf_Sym rsym, fsym, dsym;
1098 char pname[DTRACE_PROVNAMELEN];
1101 uint32_t off, eclass, emachine1, emachine2;
1102 size_t symsize, nsym, isym, istr, len;
1104 dt_link_pair_t *pair, *bufs = NULL;
1105 dt_strtab_t *strtab;
1107 if ((fd = open64(obj, O_RDWR)) == -1) {
1108 return (dt_link_error(dtp, elf, fd, bufs,
1109 "failed to open %s: %s", obj, strerror(errno)));
1112 if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
1113 return (dt_link_error(dtp, elf, fd, bufs,
1114 "failed to process %s: %s", obj, elf_errmsg(elf_errno())));
1117 switch (elf_kind(elf)) {
1121 return (dt_link_error(dtp, elf, fd, bufs, "archives are not "
1122 "permitted; use the contents of the archive instead: %s",
1125 return (dt_link_error(dtp, elf, fd, bufs,
1126 "invalid file type: %s", obj));
1129 if (gelf_getehdr(elf, &ehdr) == NULL) {
1130 return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s",
1134 if (dtp->dt_oflags & DTRACE_O_LP64) {
1135 eclass = ELFCLASS64;
1136 #if defined(__ia64__)
1137 emachine1 = emachine2 = EM_IA_64;
1138 #elif defined(__mips__)
1139 emachine1 = emachine2 = EM_MIPS;
1140 #elif defined(__powerpc__)
1141 emachine1 = emachine2 = EM_PPC64;
1142 #elif defined(__sparc)
1143 emachine1 = emachine2 = EM_SPARCV9;
1144 #elif defined(__i386) || defined(__amd64)
1145 emachine1 = emachine2 = EM_AMD64;
1147 symsize = sizeof (Elf64_Sym);
1149 eclass = ELFCLASS32;
1150 #if defined(__arm__)
1151 emachine1 = emachine2 = EM_ARM;
1152 #elif defined(__mips__)
1153 emachine1 = emachine2 = EM_MIPS;
1154 #elif defined(__powerpc__)
1155 emachine1 = emachine2 = EM_PPC;
1156 #elif defined(__sparc)
1157 emachine1 = EM_SPARC;
1158 emachine2 = EM_SPARC32PLUS;
1159 #elif defined(__i386) || defined(__amd64) || defined(__ia64__)
1160 emachine1 = emachine2 = EM_386;
1162 symsize = sizeof (Elf32_Sym);
1165 if (ehdr.e_ident[EI_CLASS] != eclass) {
1166 return (dt_link_error(dtp, elf, fd, bufs,
1167 "incorrect ELF class for object file: %s", obj));
1170 if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) {
1171 return (dt_link_error(dtp, elf, fd, bufs,
1172 "incorrect ELF machine type for object file: %s", obj));
1176 * We use this token as a relatively unique handle for this file on the
1177 * system in order to disambiguate potential conflicts between files of
1178 * the same name which contain identially named local symbols.
1180 if ((objkey = ftok(obj, 0)) == (key_t)-1) {
1181 return (dt_link_error(dtp, elf, fd, bufs,
1182 "failed to generate unique key for object file: %s", obj));
1186 while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) {
1187 if (gelf_getshdr(scn_rel, &shdr_rel) == NULL)
1191 * Skip any non-relocation sections.
1193 if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL)
1196 if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL)
1200 * Grab the section, section header and section data for the
1201 * symbol table that this relocation section references.
1203 if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL ||
1204 gelf_getshdr(scn_sym, &shdr_sym) == NULL ||
1205 (data_sym = elf_getdata(scn_sym, NULL)) == NULL)
1209 * Ditto for that symbol table's string table.
1211 if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL ||
1212 gelf_getshdr(scn_str, &shdr_str) == NULL ||
1213 (data_str = elf_getdata(scn_str, NULL)) == NULL)
1217 * Grab the section, section header and section data for the
1218 * target section for the relocations. For the relocations
1219 * we're looking for -- this will typically be the text of the
1222 if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL ||
1223 gelf_getshdr(scn_tgt, &shdr_tgt) == NULL ||
1224 (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL)
1228 * We're looking for relocations to symbols matching this form:
1230 * __dtrace[enabled]_<prov>___<probe>
1232 * For the generated object, we need to record the location
1233 * identified by the relocation, and create a new relocation
1234 * in the generated object that will be resolved at link time
1235 * to the location of the function in which the probe is
1236 * embedded. In the target object, we change the matched symbol
1237 * so that it will be ignored at link time, and we modify the
1238 * target (text) section to replace the call instruction with
1241 * If the function containing the probe is locally scoped
1242 * (static), we create an alias used by the relocation in the
1243 * generated object. The alias, a new symbol, will be global
1244 * (so that the relocation from the generated object can be
1245 * resolved), and hidden (so that it is converted to a local
1246 * symbol at link time). Such aliases have this form:
1248 * $dtrace<key>.<function>
1250 * We take a first pass through all the relocations to
1251 * populate our string table and count the number of extra
1252 * symbols we'll require.
1254 strtab = dt_strtab_create(1);
1256 isym = data_sym->d_size / symsize;
1257 istr = data_str->d_size;
1259 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1261 if (shdr_rel.sh_type == SHT_RELA) {
1262 if (gelf_getrela(data_rel, i, &rela) == NULL)
1266 if (gelf_getrel(data_rel, i, &rel) == NULL)
1268 rela.r_offset = rel.r_offset;
1269 rela.r_info = rel.r_info;
1273 if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info),
1275 dt_strtab_destroy(strtab);
1279 s = (char *)data_str->d_buf + rsym.st_name;
1281 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1284 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1285 shdr_rel.sh_info, &fsym) != 0) {
1286 dt_strtab_destroy(strtab);
1290 if (GELF_ST_BIND(fsym.st_info) != STB_LOCAL)
1293 if (fsym.st_name > data_str->d_size) {
1294 dt_strtab_destroy(strtab);
1298 s = (char *)data_str->d_buf + fsym.st_name;
1301 * If this symbol isn't of type function, we've really
1302 * driven off the rails or the object file is corrupt.
1304 if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) {
1305 dt_strtab_destroy(strtab);
1306 return (dt_link_error(dtp, elf, fd, bufs,
1307 "expected %s to be of type function", s));
1310 len = snprintf(NULL, 0, dt_symfmt, dt_symprefix,
1312 if ((p = dt_alloc(dtp, len)) == NULL) {
1313 dt_strtab_destroy(strtab);
1316 (void) snprintf(p, len, dt_symfmt, dt_symprefix,
1319 if (dt_strtab_index(strtab, p) == -1) {
1321 (void) dt_strtab_insert(strtab, p);
1328 * If needed, allocate the additional space for the symbol
1329 * table and string table copying the old data into the new
1330 * buffers, and marking the buffers as dirty. We inject those
1331 * newly allocated buffers into the libelf data structures, but
1332 * are still responsible for freeing them once we're done with
1337 * The first byte of the string table is reserved for
1340 len = dt_strtab_size(strtab) - 1;
1343 assert(dt_strtab_index(strtab, "") == 0);
1345 dt_strtab_destroy(strtab);
1347 if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL)
1350 if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size +
1356 if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size +
1357 nsym * symsize)) == NULL) {
1358 dt_free(dtp, pair->dlp_str);
1363 pair->dlp_next = bufs;
1366 bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size);
1367 data_str->d_buf = pair->dlp_str;
1368 data_str->d_size += len;
1369 (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY);
1371 shdr_str.sh_size += len;
1372 (void) gelf_update_shdr(scn_str, &shdr_str);
1374 bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size);
1375 data_sym->d_buf = pair->dlp_sym;
1376 data_sym->d_size += nsym * symsize;
1377 (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY);
1379 shdr_sym.sh_size += nsym * symsize;
1380 (void) gelf_update_shdr(scn_sym, &shdr_sym);
1384 dt_strtab_destroy(strtab);
1388 * Now that the tables have been allocated, perform the
1389 * modifications described above.
1391 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1393 if (shdr_rel.sh_type == SHT_RELA) {
1394 if (gelf_getrela(data_rel, i, &rela) == NULL)
1398 if (gelf_getrel(data_rel, i, &rel) == NULL)
1400 rela.r_offset = rel.r_offset;
1401 rela.r_info = rel.r_info;
1405 ndx = GELF_R_SYM(rela.r_info);
1407 if (gelf_getsym(data_sym, ndx, &rsym) == NULL ||
1408 rsym.st_name > data_str->d_size)
1411 s = (char *)data_str->d_buf + rsym.st_name;
1413 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1416 s += sizeof (dt_prefix) - 1;
1419 * Check to see if this is an 'is-enabled' check as
1420 * opposed to a normal probe.
1422 if (strncmp(s, dt_enabled,
1423 sizeof (dt_enabled) - 1) == 0) {
1424 s += sizeof (dt_enabled) - 1;
1427 dt_dprintf("is-enabled probe\n");
1430 dt_dprintf("normal probe\n");
1436 if ((p = strstr(s, "___")) == NULL ||
1437 p - s >= sizeof (pname))
1440 bcopy(s, pname, p - s);
1441 pname[p - s] = '\0';
1443 p = strhyphenate(p + 3); /* strlen("___") */
1445 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1446 shdr_rel.sh_info, &fsym) != 0)
1449 if (fsym.st_name > data_str->d_size)
1452 assert(GELF_ST_TYPE(fsym.st_info) == STT_FUNC);
1455 * If a NULL relocation name is passed to
1456 * dt_probe_define(), the function name is used for the
1457 * relocation. The relocation needs to use a mangled
1458 * name if the symbol is locally scoped; the function
1459 * name may need to change if we've found the global
1460 * alias for the locally scoped symbol (we prefer
1461 * global symbols to locals in dt_symtab_lookup()).
1463 s = (char *)data_str->d_buf + fsym.st_name;
1466 if (GELF_ST_BIND(fsym.st_info) == STB_LOCAL) {
1468 dsym.st_name = istr;
1469 dsym.st_info = GELF_ST_INFO(STB_GLOBAL,
1472 ELF64_ST_VISIBILITY(STV_ELIMINATE);
1473 (void) gelf_update_sym(data_sym, isym, &dsym);
1475 r = (char *)data_str->d_buf + istr;
1476 istr += 1 + sprintf(r, dt_symfmt,
1477 dt_symprefix, objkey, s);
1479 assert(isym <= nsym);
1481 } else if (strncmp(s, dt_symprefix,
1482 strlen(dt_symprefix)) == 0) {
1484 if ((s = strchr(s, '.')) == NULL)
1489 if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) {
1490 return (dt_link_error(dtp, elf, fd, bufs,
1491 "no such provider %s", pname));
1494 if ((prp = dt_probe_lookup(pvp, p)) == NULL) {
1495 return (dt_link_error(dtp, elf, fd, bufs,
1496 "no such probe %s", p));
1499 assert(fsym.st_value <= rela.r_offset);
1501 off = rela.r_offset - fsym.st_value;
1502 if (dt_modtext(dtp, data_tgt->d_buf, eprobe,
1503 &rela, &off) != 0) {
1507 if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) {
1508 return (dt_link_error(dtp, elf, fd, bufs,
1509 "failed to allocate space for probe"));
1513 (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY);
1516 * This symbol may already have been marked to
1517 * be ignored by another relocation referencing
1518 * the same symbol or if this object file has
1519 * already been processed by an earlier link
1522 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
1524 if (rsym.st_shndx != SHN_SUNW_IGNORE) {
1525 rsym.st_shndx = SHN_SUNW_IGNORE;
1526 (void) gelf_update_sym(data_sym, ndx, &rsym);
1532 if (mod && elf_update(elf, ELF_C_WRITE) == -1)
1535 (void) elf_end(elf);
1538 while ((pair = bufs) != NULL) {
1539 bufs = pair->dlp_next;
1540 dt_free(dtp, pair->dlp_str);
1541 dt_free(dtp, pair->dlp_sym);
1548 return (dt_link_error(dtp, elf, fd, bufs,
1549 "an error was encountered while processing %s", obj));
1553 dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
1554 const char *file, int objc, char *const objv[])
1557 char tfile[PATH_MAX];
1559 char drti[PATH_MAX];
1561 int fd, status, i, cur;
1564 int eprobes = 0, ret = 0;
1567 /* XXX Should get a temp file name here. */
1568 snprintf(tfile, sizeof(tfile), "%s.tmp", file);
1572 * A NULL program indicates a special use in which we just link
1573 * together a bunch of object files specified in objv and then
1574 * unlink(2) those object files.
1577 const char *fmt = "%s -o %s -r";
1579 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1;
1581 for (i = 0; i < objc; i++)
1582 len += strlen(objv[i]) + 1;
1586 cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file);
1588 for (i = 0; i < objc; i++)
1589 cur += snprintf(cmd + cur, len - cur, " %s", objv[i]);
1591 if ((status = system(cmd)) == -1) {
1592 return (dt_link_error(dtp, NULL, -1, NULL,
1593 "failed to run %s: %s", dtp->dt_ld_path,
1597 if (WIFSIGNALED(status)) {
1598 return (dt_link_error(dtp, NULL, -1, NULL,
1599 "failed to link %s: %s failed due to signal %d",
1600 file, dtp->dt_ld_path, WTERMSIG(status)));
1603 if (WEXITSTATUS(status) != 0) {
1604 return (dt_link_error(dtp, NULL, -1, NULL,
1605 "failed to link %s: %s exited with status %d\n",
1606 file, dtp->dt_ld_path, WEXITSTATUS(status)));
1609 for (i = 0; i < objc; i++) {
1610 if (strcmp(objv[i], file) != 0)
1611 (void) unlink(objv[i]);
1617 for (i = 0; i < objc; i++) {
1618 if (process_obj(dtp, objv[i], &eprobes) != 0)
1619 return (-1); /* errno is set for us */
1623 * If there are is-enabled probes then we need to force use of DOF
1626 if (eprobes && pgp->dp_dofversion < DOF_VERSION_2)
1627 pgp->dp_dofversion = DOF_VERSION_2;
1629 if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL)
1630 return (-1); /* errno is set for us */
1634 * Create a temporary file and then unlink it if we're going to
1635 * combine it with drti.o later. We can still refer to it in child
1636 * processes as /dev/fd/<fd>.
1638 if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
1639 return (dt_link_error(dtp, NULL, -1, NULL,
1640 "failed to open %s: %s", file, strerror(errno)));
1643 if ((fd = open(tfile, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
1644 return (dt_link_error(dtp, NULL, -1, NULL,
1645 "failed to open %s: %s", tfile, strerror(errno)));
1649 * If -xlinktype=DOF has been selected, just write out the DOF.
1650 * Otherwise proceed to the default of generating and linking ELF.
1652 switch (dtp->dt_linktype) {
1654 if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz)
1657 if (close(fd) != 0 && ret == 0)
1661 return (dt_link_error(dtp, NULL, -1, NULL,
1662 "failed to write %s: %s", file, strerror(ret)));
1668 break; /* fall through to the rest of dtrace_program_link() */
1671 return (dt_link_error(dtp, NULL, -1, NULL,
1672 "invalid link type %u\n", dtp->dt_linktype));
1677 if (!dtp->dt_lazyload)
1678 (void) unlink(file);
1681 if (dtp->dt_oflags & DTRACE_O_LP64)
1682 status = dump_elf64(dtp, dof, fd);
1684 status = dump_elf32(dtp, dof, fd);
1686 if (status != 0 || lseek(fd, 0, SEEK_SET) != 0) {
1687 return (dt_link_error(dtp, NULL, -1, NULL,
1688 "failed to write %s: %s", file, strerror(errno)));
1691 if (!dtp->dt_lazyload) {
1693 const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s";
1695 if (dtp->dt_oflags & DTRACE_O_LP64) {
1696 (void) snprintf(drti, sizeof (drti),
1697 "%s/64/drti.o", _dtrace_libdir);
1699 (void) snprintf(drti, sizeof (drti),
1700 "%s/drti.o", _dtrace_libdir);
1703 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd,
1708 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti);
1710 const char *fmt = "%s -o %s -r %s %s";
1712 #if defined(__amd64__)
1714 * Arches which default to 64-bit need to explicitly use
1715 * the 32-bit library path.
1717 int use_32 = !(dtp->dt_oflags & DTRACE_O_LP64);
1720 * Arches which are 32-bit only just use the normal
1723 #if defined(__i386__)
1724 int use_32 = 1; /* use /usr/lib/... -sson */
1730 (void) snprintf(drti, sizeof (drti), "/usr/lib%s/dtrace/drti.o",
1733 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, tfile,
1738 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, tfile, drti);
1741 if ((status = system(cmd)) == -1) {
1742 ret = dt_link_error(dtp, NULL, -1, NULL,
1743 "failed to run %s: %s", dtp->dt_ld_path,
1748 (void) close(fd); /* release temporary file */
1750 if (WIFSIGNALED(status)) {
1751 ret = dt_link_error(dtp, NULL, -1, NULL,
1752 "failed to link %s: %s failed due to signal %d",
1753 file, dtp->dt_ld_path, WTERMSIG(status));
1757 if (WEXITSTATUS(status) != 0) {
1758 ret = dt_link_error(dtp, NULL, -1, NULL,
1759 "failed to link %s: %s exited with status %d\n",
1760 file, dtp->dt_ld_path, WEXITSTATUS(status));
1768 dtrace_dof_destroy(dtp, dof);