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 below.
874 if (ip[0] == DT_OP_NOP) {
875 (*off) += sizeof (ip[0]);
879 if (DT_IS_RESTORE(ip[1])) {
880 if (ip[0] == DT_OP_RET) {
881 (*off) += sizeof (ip[0]);
884 } else if (DT_IS_MOV_O7(ip[1])) {
885 if (DT_IS_RETL(ip[0]))
888 if (ip[0] == DT_OP_NOP) {
889 (*off) += sizeof (ip[0]);
896 * We only expect call instructions with a displacement of 0.
898 if (ip[0] != DT_OP_CALL) {
899 dt_dprintf("found %x instead of a call instruction at %llx\n",
900 ip[0], (u_longlong_t)rela->r_offset);
906 * It would necessarily indicate incorrect usage if an is-
907 * enabled probe were tail-called so flag that as an error.
908 * It's also potentially (very) tricky to handle gracefully,
909 * but could be done if this were a desired use scenario.
911 if (DT_IS_RESTORE(ip[1]) || DT_IS_MOV_O7(ip[1])) {
912 dt_dprintf("tail call to is-enabled probe at %llx\n",
913 (u_longlong_t)rela->r_offset);
919 * On SPARC, we take advantage of the fact that the first
920 * argument shares the same register as for the return value.
921 * The macro handles the work of zeroing that register so we
922 * don't need to do anything special here. We instrument the
923 * instruction in the delay slot as we'll need to modify the
924 * return register after that instruction has been emulated.
927 (*off) += sizeof (ip[0]);
930 * If the call is followed by a restore, it's a tail call so
931 * change the call to a ret. If the call if followed by a mov
932 * of a register into %o7, it's a tail call in leaf context
933 * so change the call to a retl-like instruction that returns
934 * to that register value + 8 (rather than the typical %o7 +
935 * 8); the delay slot instruction is left, but should have no
936 * effect. Otherwise we change the call to be a nop. We
937 * identify the subsequent instruction as the probe point in
938 * all but the leaf tail-call case to ensure that arguments to
939 * the probe are complete and consistent. An astute, though
940 * largely hypothetical, observer would note that there is the
941 * possibility of a false-positive probe firing if the function
942 * contained a branch to the instruction in the delay slot of
943 * the call. Fixing this would require significant in-kernel
944 * modifications, and isn't worth doing until we see it in the
947 if (DT_IS_RESTORE(ip[1])) {
949 (*off) += sizeof (ip[0]);
950 } else if (DT_IS_MOV_O7(ip[1])) {
951 ip[0] = DT_MAKE_RETL(DT_RS2(ip[1]));
954 (*off) += sizeof (ip[0]);
961 #elif defined(__i386) || defined(__amd64)
963 #define DT_OP_NOP 0x90
964 #define DT_OP_RET 0xc3
965 #define DT_OP_CALL 0xe8
966 #define DT_OP_JMP32 0xe9
967 #define DT_OP_REX_RAX 0x48
968 #define DT_OP_XOR_EAX_0 0x33
969 #define DT_OP_XOR_EAX_1 0xc0
972 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
975 uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1);
979 * On x86, the first byte of the instruction is the call opcode and
980 * the next four bytes are the 32-bit address; the relocation is for
981 * the address operand. We back up the offset to the first byte of
982 * the instruction. For is-enabled probes, we later advance the offset
983 * so that it hits the first nop in the instruction sequence.
988 * We only know about some specific relocation types. Luckily
989 * these types have the same values on both 32-bit and 64-bit
992 if (GELF_R_TYPE(rela->r_info) != R_386_PC32 &&
993 GELF_R_TYPE(rela->r_info) != R_386_PLT32)
997 * We may have already processed this object file in an earlier linker
998 * invocation. Check to see if the present instruction sequence matches
999 * the one we would install. For is-enabled probes, we advance the
1000 * offset to the first nop instruction in the sequence to match the
1001 * text modification code below.
1004 if ((ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET) &&
1005 ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP &&
1006 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP)
1008 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
1009 if (ip[0] == DT_OP_REX_RAX &&
1010 ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 &&
1011 (ip[3] == DT_OP_NOP || ip[3] == DT_OP_RET) &&
1012 ip[4] == DT_OP_NOP) {
1017 if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 &&
1018 (ip[2] == DT_OP_NOP || ip[2] == DT_OP_RET) &&
1019 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) {
1026 * We expect either a call instrution with a 32-bit displacement or a
1027 * jmp instruction with a 32-bit displacement acting as a tail-call.
1029 if (ip[0] != DT_OP_CALL && ip[0] != DT_OP_JMP32) {
1030 dt_dprintf("found %x instead of a call or jmp instruction at "
1031 "%llx\n", ip[0], (u_longlong_t)rela->r_offset);
1035 ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP;
1038 * Establish the instruction sequence -- all nops for probes, and an
1039 * instruction to clear the return value register (%eax/%rax) followed
1040 * by nops for is-enabled probes. For is-enabled probes, we advance
1041 * the offset to the first nop. This isn't stricly necessary but makes
1042 * for more readable disassembly when the probe is enabled.
1050 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
1051 ip[0] = DT_OP_REX_RAX;
1052 ip[1] = DT_OP_XOR_EAX_0;
1053 ip[2] = DT_OP_XOR_EAX_1;
1058 ip[0] = DT_OP_XOR_EAX_0;
1059 ip[1] = DT_OP_XOR_EAX_1;
1075 dt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs,
1076 const char *format, ...)
1079 dt_link_pair_t *pair;
1081 va_start(ap, format);
1082 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1086 (void) elf_end(elf);
1091 while ((pair = bufs) != NULL) {
1092 bufs = pair->dlp_next;
1093 dt_free(dtp, pair->dlp_str);
1094 dt_free(dtp, pair->dlp_sym);
1098 return (dt_set_errno(dtp, EDT_COMPILER));
1102 process_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp)
1104 static const char dt_prefix[] = "__dtrace";
1105 static const char dt_enabled[] = "enabled";
1106 static const char dt_symprefix[] = "$dtrace";
1107 static const char dt_symfmt[] = "%s%d.%s";
1108 int fd, i, ndx, eprobe, mod = 0;
1111 Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt;
1112 Elf_Data *data_rel, *data_sym, *data_str, *data_tgt;
1113 GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt;
1114 GElf_Sym rsym, fsym, dsym;
1117 char pname[DTRACE_PROVNAMELEN];
1120 uint32_t off, eclass, emachine1, emachine2;
1121 size_t symsize, nsym, isym, istr, len;
1123 dt_link_pair_t *pair, *bufs = NULL;
1124 dt_strtab_t *strtab;
1126 if ((fd = open64(obj, O_RDWR)) == -1) {
1127 return (dt_link_error(dtp, elf, fd, bufs,
1128 "failed to open %s: %s", obj, strerror(errno)));
1131 if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
1132 return (dt_link_error(dtp, elf, fd, bufs,
1133 "failed to process %s: %s", obj, elf_errmsg(elf_errno())));
1136 switch (elf_kind(elf)) {
1140 return (dt_link_error(dtp, elf, fd, bufs, "archives are not "
1141 "permitted; use the contents of the archive instead: %s",
1144 return (dt_link_error(dtp, elf, fd, bufs,
1145 "invalid file type: %s", obj));
1148 if (gelf_getehdr(elf, &ehdr) == NULL) {
1149 return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s",
1153 if (dtp->dt_oflags & DTRACE_O_LP64) {
1154 eclass = ELFCLASS64;
1155 #if defined(__ia64__)
1156 emachine1 = emachine2 = EM_IA_64;
1157 #elif defined(__mips__)
1158 emachine1 = emachine2 = EM_MIPS;
1159 #elif defined(__powerpc__)
1160 emachine1 = emachine2 = EM_PPC64;
1161 #elif defined(__sparc)
1162 emachine1 = emachine2 = EM_SPARCV9;
1163 #elif defined(__i386) || defined(__amd64)
1164 emachine1 = emachine2 = EM_AMD64;
1166 symsize = sizeof (Elf64_Sym);
1168 eclass = ELFCLASS32;
1169 #if defined(__arm__)
1170 emachine1 = emachine2 = EM_ARM;
1171 #elif defined(__mips__)
1172 emachine1 = emachine2 = EM_MIPS;
1173 #elif defined(__powerpc__)
1174 emachine1 = emachine2 = EM_PPC;
1175 #elif defined(__sparc)
1176 emachine1 = EM_SPARC;
1177 emachine2 = EM_SPARC32PLUS;
1178 #elif defined(__i386) || defined(__amd64) || defined(__ia64__)
1179 emachine1 = emachine2 = EM_386;
1181 symsize = sizeof (Elf32_Sym);
1184 if (ehdr.e_ident[EI_CLASS] != eclass) {
1185 return (dt_link_error(dtp, elf, fd, bufs,
1186 "incorrect ELF class for object file: %s", obj));
1189 if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) {
1190 return (dt_link_error(dtp, elf, fd, bufs,
1191 "incorrect ELF machine type for object file: %s", obj));
1195 * We use this token as a relatively unique handle for this file on the
1196 * system in order to disambiguate potential conflicts between files of
1197 * the same name which contain identially named local symbols.
1199 if ((objkey = ftok(obj, 0)) == (key_t)-1) {
1200 return (dt_link_error(dtp, elf, fd, bufs,
1201 "failed to generate unique key for object file: %s", obj));
1205 while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) {
1206 if (gelf_getshdr(scn_rel, &shdr_rel) == NULL)
1210 * Skip any non-relocation sections.
1212 if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL)
1215 if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL)
1219 * Grab the section, section header and section data for the
1220 * symbol table that this relocation section references.
1222 if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL ||
1223 gelf_getshdr(scn_sym, &shdr_sym) == NULL ||
1224 (data_sym = elf_getdata(scn_sym, NULL)) == NULL)
1228 * Ditto for that symbol table's string table.
1230 if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL ||
1231 gelf_getshdr(scn_str, &shdr_str) == NULL ||
1232 (data_str = elf_getdata(scn_str, NULL)) == NULL)
1236 * Grab the section, section header and section data for the
1237 * target section for the relocations. For the relocations
1238 * we're looking for -- this will typically be the text of the
1241 if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL ||
1242 gelf_getshdr(scn_tgt, &shdr_tgt) == NULL ||
1243 (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL)
1247 * We're looking for relocations to symbols matching this form:
1249 * __dtrace[enabled]_<prov>___<probe>
1251 * For the generated object, we need to record the location
1252 * identified by the relocation, and create a new relocation
1253 * in the generated object that will be resolved at link time
1254 * to the location of the function in which the probe is
1255 * embedded. In the target object, we change the matched symbol
1256 * so that it will be ignored at link time, and we modify the
1257 * target (text) section to replace the call instruction with
1260 * If the function containing the probe is locally scoped
1261 * (static), we create an alias used by the relocation in the
1262 * generated object. The alias, a new symbol, will be global
1263 * (so that the relocation from the generated object can be
1264 * resolved), and hidden (so that it is converted to a local
1265 * symbol at link time). Such aliases have this form:
1267 * $dtrace<key>.<function>
1269 * We take a first pass through all the relocations to
1270 * populate our string table and count the number of extra
1271 * symbols we'll require.
1273 strtab = dt_strtab_create(1);
1275 isym = data_sym->d_size / symsize;
1276 istr = data_str->d_size;
1278 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1280 if (shdr_rel.sh_type == SHT_RELA) {
1281 if (gelf_getrela(data_rel, i, &rela) == NULL)
1285 if (gelf_getrel(data_rel, i, &rel) == NULL)
1287 rela.r_offset = rel.r_offset;
1288 rela.r_info = rel.r_info;
1292 if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info),
1294 dt_strtab_destroy(strtab);
1298 s = (char *)data_str->d_buf + rsym.st_name;
1300 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1303 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1304 shdr_rel.sh_info, &fsym) != 0) {
1305 dt_strtab_destroy(strtab);
1309 if (GELF_ST_BIND(fsym.st_info) != STB_LOCAL)
1312 if (fsym.st_name > data_str->d_size) {
1313 dt_strtab_destroy(strtab);
1317 s = (char *)data_str->d_buf + fsym.st_name;
1320 * If this symbol isn't of type function, we've really
1321 * driven off the rails or the object file is corrupt.
1323 if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) {
1324 dt_strtab_destroy(strtab);
1325 return (dt_link_error(dtp, elf, fd, bufs,
1326 "expected %s to be of type function", s));
1329 len = snprintf(NULL, 0, dt_symfmt, dt_symprefix,
1331 if ((p = dt_alloc(dtp, len)) == NULL) {
1332 dt_strtab_destroy(strtab);
1335 (void) snprintf(p, len, dt_symfmt, dt_symprefix,
1338 if (dt_strtab_index(strtab, p) == -1) {
1340 (void) dt_strtab_insert(strtab, p);
1347 * If needed, allocate the additional space for the symbol
1348 * table and string table copying the old data into the new
1349 * buffers, and marking the buffers as dirty. We inject those
1350 * newly allocated buffers into the libelf data structures, but
1351 * are still responsible for freeing them once we're done with
1356 * The first byte of the string table is reserved for
1359 len = dt_strtab_size(strtab) - 1;
1362 assert(dt_strtab_index(strtab, "") == 0);
1364 dt_strtab_destroy(strtab);
1366 if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL)
1369 if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size +
1375 if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size +
1376 nsym * symsize)) == NULL) {
1377 dt_free(dtp, pair->dlp_str);
1382 pair->dlp_next = bufs;
1385 bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size);
1386 data_str->d_buf = pair->dlp_str;
1387 data_str->d_size += len;
1388 (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY);
1390 shdr_str.sh_size += len;
1391 (void) gelf_update_shdr(scn_str, &shdr_str);
1393 bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size);
1394 data_sym->d_buf = pair->dlp_sym;
1395 data_sym->d_size += nsym * symsize;
1396 (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY);
1398 shdr_sym.sh_size += nsym * symsize;
1399 (void) gelf_update_shdr(scn_sym, &shdr_sym);
1403 dt_strtab_destroy(strtab);
1407 * Now that the tables have been allocated, perform the
1408 * modifications described above.
1410 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1412 if (shdr_rel.sh_type == SHT_RELA) {
1413 if (gelf_getrela(data_rel, i, &rela) == NULL)
1417 if (gelf_getrel(data_rel, i, &rel) == NULL)
1419 rela.r_offset = rel.r_offset;
1420 rela.r_info = rel.r_info;
1424 ndx = GELF_R_SYM(rela.r_info);
1426 if (gelf_getsym(data_sym, ndx, &rsym) == NULL ||
1427 rsym.st_name > data_str->d_size)
1430 s = (char *)data_str->d_buf + rsym.st_name;
1432 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1435 s += sizeof (dt_prefix) - 1;
1438 * Check to see if this is an 'is-enabled' check as
1439 * opposed to a normal probe.
1441 if (strncmp(s, dt_enabled,
1442 sizeof (dt_enabled) - 1) == 0) {
1443 s += sizeof (dt_enabled) - 1;
1446 dt_dprintf("is-enabled probe\n");
1449 dt_dprintf("normal probe\n");
1455 if ((p = strstr(s, "___")) == NULL ||
1456 p - s >= sizeof (pname))
1459 bcopy(s, pname, p - s);
1460 pname[p - s] = '\0';
1462 p = strhyphenate(p + 3); /* strlen("___") */
1464 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1465 shdr_rel.sh_info, &fsym) != 0)
1468 if (fsym.st_name > data_str->d_size)
1471 assert(GELF_ST_TYPE(fsym.st_info) == STT_FUNC);
1474 * If a NULL relocation name is passed to
1475 * dt_probe_define(), the function name is used for the
1476 * relocation. The relocation needs to use a mangled
1477 * name if the symbol is locally scoped; the function
1478 * name may need to change if we've found the global
1479 * alias for the locally scoped symbol (we prefer
1480 * global symbols to locals in dt_symtab_lookup()).
1482 s = (char *)data_str->d_buf + fsym.st_name;
1485 if (GELF_ST_BIND(fsym.st_info) == STB_LOCAL) {
1487 dsym.st_name = istr;
1488 dsym.st_info = GELF_ST_INFO(STB_GLOBAL,
1491 ELF64_ST_VISIBILITY(STV_ELIMINATE);
1492 (void) gelf_update_sym(data_sym, isym, &dsym);
1494 r = (char *)data_str->d_buf + istr;
1495 istr += 1 + sprintf(r, dt_symfmt,
1496 dt_symprefix, objkey, s);
1498 assert(isym <= nsym);
1500 } else if (strncmp(s, dt_symprefix,
1501 strlen(dt_symprefix)) == 0) {
1503 if ((s = strchr(s, '.')) == NULL)
1508 if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) {
1509 return (dt_link_error(dtp, elf, fd, bufs,
1510 "no such provider %s", pname));
1513 if ((prp = dt_probe_lookup(pvp, p)) == NULL) {
1514 return (dt_link_error(dtp, elf, fd, bufs,
1515 "no such probe %s", p));
1518 assert(fsym.st_value <= rela.r_offset);
1520 off = rela.r_offset - fsym.st_value;
1521 if (dt_modtext(dtp, data_tgt->d_buf, eprobe,
1522 &rela, &off) != 0) {
1526 if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) {
1527 return (dt_link_error(dtp, elf, fd, bufs,
1528 "failed to allocate space for probe"));
1532 (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY);
1535 * This symbol may already have been marked to
1536 * be ignored by another relocation referencing
1537 * the same symbol or if this object file has
1538 * already been processed by an earlier link
1541 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
1543 if (rsym.st_shndx != SHN_SUNW_IGNORE) {
1544 rsym.st_shndx = SHN_SUNW_IGNORE;
1545 (void) gelf_update_sym(data_sym, ndx, &rsym);
1551 if (mod && elf_update(elf, ELF_C_WRITE) == -1)
1554 (void) elf_end(elf);
1557 while ((pair = bufs) != NULL) {
1558 bufs = pair->dlp_next;
1559 dt_free(dtp, pair->dlp_str);
1560 dt_free(dtp, pair->dlp_sym);
1567 return (dt_link_error(dtp, elf, fd, bufs,
1568 "an error was encountered while processing %s", obj));
1572 dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
1573 const char *file, int objc, char *const objv[])
1576 char tfile[PATH_MAX];
1578 char drti[PATH_MAX];
1580 int fd, status, i, cur;
1583 int eprobes = 0, ret = 0;
1586 /* XXX Should get a temp file name here. */
1587 snprintf(tfile, sizeof(tfile), "%s.tmp", file);
1591 * A NULL program indicates a special use in which we just link
1592 * together a bunch of object files specified in objv and then
1593 * unlink(2) those object files.
1596 const char *fmt = "%s -o %s -r";
1598 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1;
1600 for (i = 0; i < objc; i++)
1601 len += strlen(objv[i]) + 1;
1605 cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file);
1607 for (i = 0; i < objc; i++)
1608 cur += snprintf(cmd + cur, len - cur, " %s", objv[i]);
1610 if ((status = system(cmd)) == -1) {
1611 return (dt_link_error(dtp, NULL, -1, NULL,
1612 "failed to run %s: %s", dtp->dt_ld_path,
1616 if (WIFSIGNALED(status)) {
1617 return (dt_link_error(dtp, NULL, -1, NULL,
1618 "failed to link %s: %s failed due to signal %d",
1619 file, dtp->dt_ld_path, WTERMSIG(status)));
1622 if (WEXITSTATUS(status) != 0) {
1623 return (dt_link_error(dtp, NULL, -1, NULL,
1624 "failed to link %s: %s exited with status %d\n",
1625 file, dtp->dt_ld_path, WEXITSTATUS(status)));
1628 for (i = 0; i < objc; i++) {
1629 if (strcmp(objv[i], file) != 0)
1630 (void) unlink(objv[i]);
1636 for (i = 0; i < objc; i++) {
1637 if (process_obj(dtp, objv[i], &eprobes) != 0)
1638 return (-1); /* errno is set for us */
1642 * If there are is-enabled probes then we need to force use of DOF
1645 if (eprobes && pgp->dp_dofversion < DOF_VERSION_2)
1646 pgp->dp_dofversion = DOF_VERSION_2;
1648 if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL)
1649 return (-1); /* errno is set for us */
1653 * Create a temporary file and then unlink it if we're going to
1654 * combine it with drti.o later. We can still refer to it in child
1655 * processes as /dev/fd/<fd>.
1657 if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
1658 return (dt_link_error(dtp, NULL, -1, NULL,
1659 "failed to open %s: %s", file, strerror(errno)));
1662 if ((fd = open(tfile, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
1663 return (dt_link_error(dtp, NULL, -1, NULL,
1664 "failed to open %s: %s", tfile, strerror(errno)));
1668 * If -xlinktype=DOF has been selected, just write out the DOF.
1669 * Otherwise proceed to the default of generating and linking ELF.
1671 switch (dtp->dt_linktype) {
1673 if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz)
1676 if (close(fd) != 0 && ret == 0)
1680 return (dt_link_error(dtp, NULL, -1, NULL,
1681 "failed to write %s: %s", file, strerror(ret)));
1687 break; /* fall through to the rest of dtrace_program_link() */
1690 return (dt_link_error(dtp, NULL, -1, NULL,
1691 "invalid link type %u\n", dtp->dt_linktype));
1696 if (!dtp->dt_lazyload)
1697 (void) unlink(file);
1700 if (dtp->dt_oflags & DTRACE_O_LP64)
1701 status = dump_elf64(dtp, dof, fd);
1703 status = dump_elf32(dtp, dof, fd);
1705 if (status != 0 || lseek(fd, 0, SEEK_SET) != 0) {
1706 return (dt_link_error(dtp, NULL, -1, NULL,
1707 "failed to write %s: %s", file, strerror(errno)));
1710 if (!dtp->dt_lazyload) {
1712 const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s";
1714 if (dtp->dt_oflags & DTRACE_O_LP64) {
1715 (void) snprintf(drti, sizeof (drti),
1716 "%s/64/drti.o", _dtrace_libdir);
1718 (void) snprintf(drti, sizeof (drti),
1719 "%s/drti.o", _dtrace_libdir);
1722 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd,
1727 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti);
1729 const char *fmt = "%s -o %s -r %s %s";
1731 #if defined(__amd64__)
1733 * Arches which default to 64-bit need to explicitly use
1734 * the 32-bit library path.
1736 int use_32 = !(dtp->dt_oflags & DTRACE_O_LP64);
1739 * Arches which are 32-bit only just use the normal
1745 (void) snprintf(drti, sizeof (drti), "/usr/lib%s/dtrace/drti.o",
1748 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, tfile,
1753 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, tfile, drti);
1756 if ((status = system(cmd)) == -1) {
1757 ret = dt_link_error(dtp, NULL, -1, NULL,
1758 "failed to run %s: %s", dtp->dt_ld_path,
1763 (void) close(fd); /* release temporary file */
1765 if (WIFSIGNALED(status)) {
1766 ret = dt_link_error(dtp, NULL, -1, NULL,
1767 "failed to link %s: %s failed due to signal %d",
1768 file, dtp->dt_ld_path, WTERMSIG(status));
1772 if (WEXITSTATUS(status) != 0) {
1773 ret = dt_link_error(dtp, NULL, -1, NULL,
1774 "failed to link %s: %s exited with status %d\n",
1775 file, dtp->dt_ld_path, WEXITSTATUS(status));
1783 dtrace_dof_destroy(dtp, dof);