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)))
62 #include <dt_provider.h>
63 #include <dt_program.h>
64 #include <dt_string.h>
67 #define ESHDR_SHSTRTAB 1
69 #define ESHDR_STRTAB 3
70 #define ESHDR_SYMTAB 4
74 #define PWRITE_SCN(index, data) \
75 (lseek64(fd, (off64_t)elf_file.shdr[(index)].sh_offset, SEEK_SET) != \
76 (off64_t)elf_file.shdr[(index)].sh_offset || \
77 dt_write(dtp, fd, (data), elf_file.shdr[(index)].sh_size) != \
78 elf_file.shdr[(index)].sh_size)
80 static const char DTRACE_SHSTRTAB32[] = "\0"
82 ".SUNW_dof\0" /* 11 */
86 ".rela.SUNW_dof"; /* 37 */
88 ".rel.SUNW_dof"; /* 37 */
91 static const char DTRACE_SHSTRTAB64[] = "\0"
93 ".SUNW_dof\0" /* 11 */
96 ".rela.SUNW_dof"; /* 37 */
98 static const char DOFSTR[] = "__SUNW_dof";
99 static const char DOFLAZYSTR[] = "___SUNW_dof";
101 typedef struct dt_link_pair {
102 struct dt_link_pair *dlp_next; /* next pair in linked list */
103 void *dlp_str; /* buffer for string table */
104 void *dlp_sym; /* buffer for symbol table */
107 typedef struct dof_elf32 {
108 uint32_t de_nrel; /* relocation count */
110 Elf32_Rela *de_rel; /* array of relocations for sparc */
112 Elf32_Rel *de_rel; /* array of relocations for x86 */
114 uint32_t de_nsym; /* symbol count */
115 Elf32_Sym *de_sym; /* array of symbols */
116 uint32_t de_strlen; /* size of of string table */
117 char *de_strtab; /* string table */
118 uint32_t de_global; /* index of the first global symbol */
122 prepare_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf32_t *dep)
125 dof_relohdr_t *dofrh;
126 dof_relodesc_t *dofr;
140 dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
143 * First compute the size of the string table and the number of
144 * relocations present in the DOF.
146 for (i = 0; i < dof->dofh_secnum; i++) {
147 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
151 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
153 s = &dofs[dofrh->dofr_strtab];
154 strtab = (char *)dof + s->dofs_offset;
155 assert(strtab[0] == '\0');
156 strtabsz += s->dofs_size - 1;
158 s = &dofs[dofrh->dofr_relsec];
160 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
161 count += s->dofs_size / s->dofs_entsize;
164 dep->de_strlen = strtabsz;
165 dep->de_nrel = count;
166 dep->de_nsym = count + 1; /* the first symbol is always null */
168 if (dtp->dt_lazyload) {
169 dep->de_strlen += sizeof (DOFLAZYSTR);
172 dep->de_strlen += sizeof (DOFSTR);
176 if ((dep->de_rel = calloc(dep->de_nrel,
177 sizeof (dep->de_rel[0]))) == NULL) {
178 return (dt_set_errno(dtp, EDT_NOMEM));
181 if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf32_Sym))) == NULL) {
183 return (dt_set_errno(dtp, EDT_NOMEM));
186 if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
189 return (dt_set_errno(dtp, EDT_NOMEM));
194 dep->de_strtab[0] = '\0';
200 * The first symbol table entry must be zeroed and is always ignored.
202 bzero(sym, sizeof (Elf32_Sym));
206 * Take a second pass through the DOF sections filling in the
207 * memory we allocated.
209 for (i = 0; i < dof->dofh_secnum; i++) {
210 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
214 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
216 s = &dofs[dofrh->dofr_strtab];
217 strtab = (char *)dof + s->dofs_offset;
218 bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
220 strtabsz += s->dofs_size - 1;
222 s = &dofs[dofrh->dofr_relsec];
224 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
225 nrel = s->dofs_size / s->dofs_entsize;
227 s = &dofs[dofrh->dofr_tgtsec];
229 for (j = 0; j < nrel; j++) {
232 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
233 #elif defined(__ia64__)
235 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
236 #elif defined(__i386) || defined(__amd64)
237 rel->r_offset = s->dofs_offset +
239 rel->r_info = ELF32_R_INFO(count + dep->de_global,
241 #elif defined(__mips__)
243 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
244 #elif defined(__powerpc__)
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,
253 #elif defined(__sparc)
255 * Add 4 bytes to hit the low half of this 64-bit
256 * big-endian address.
258 rel->r_offset = s->dofs_offset +
259 dofr[j].dofr_offset + 4;
260 rel->r_info = ELF32_R_INFO(count + dep->de_global,
266 sym->st_name = base + dofr[j].dofr_name - 1;
269 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC);
271 sym->st_shndx = SHN_UNDEF;
280 * Add a symbol for the DOF itself. We use a different symbol for
281 * lazily and actively loaded DOF to make them easy to distinguish.
283 sym->st_name = strtabsz;
285 sym->st_size = dof->dofh_filesz;
286 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT);
288 sym->st_shndx = ESHDR_DOF;
291 if (dtp->dt_lazyload) {
292 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
293 sizeof (DOFLAZYSTR));
294 strtabsz += sizeof (DOFLAZYSTR);
296 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
297 strtabsz += sizeof (DOFSTR);
300 assert(count == dep->de_nrel);
301 assert(strtabsz == dep->de_strlen);
307 typedef struct dof_elf64 {
320 prepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep)
323 dof_relohdr_t *dofrh;
324 dof_relodesc_t *dofr;
334 dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
337 * First compute the size of the string table and the number of
338 * relocations present in the DOF.
340 for (i = 0; i < dof->dofh_secnum; i++) {
341 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
345 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
347 s = &dofs[dofrh->dofr_strtab];
348 strtab = (char *)dof + s->dofs_offset;
349 assert(strtab[0] == '\0');
350 strtabsz += s->dofs_size - 1;
352 s = &dofs[dofrh->dofr_relsec];
354 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
355 count += s->dofs_size / s->dofs_entsize;
358 dep->de_strlen = strtabsz;
359 dep->de_nrel = count;
360 dep->de_nsym = count + 1; /* the first symbol is always null */
362 if (dtp->dt_lazyload) {
363 dep->de_strlen += sizeof (DOFLAZYSTR);
366 dep->de_strlen += sizeof (DOFSTR);
370 if ((dep->de_rel = calloc(dep->de_nrel,
371 sizeof (dep->de_rel[0]))) == NULL) {
372 return (dt_set_errno(dtp, EDT_NOMEM));
375 if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) {
377 return (dt_set_errno(dtp, EDT_NOMEM));
380 if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
383 return (dt_set_errno(dtp, EDT_NOMEM));
388 dep->de_strtab[0] = '\0';
394 * The first symbol table entry must be zeroed and is always ignored.
396 bzero(sym, sizeof (Elf64_Sym));
400 * Take a second pass through the DOF sections filling in the
401 * memory we allocated.
403 for (i = 0; i < dof->dofh_secnum; i++) {
404 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
408 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
410 s = &dofs[dofrh->dofr_strtab];
411 strtab = (char *)dof + s->dofs_offset;
412 bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
414 strtabsz += s->dofs_size - 1;
416 s = &dofs[dofrh->dofr_relsec];
418 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
419 nrel = s->dofs_size / s->dofs_entsize;
421 s = &dofs[dofrh->dofr_tgtsec];
423 for (j = 0; j < nrel; j++) {
427 #elif defined(__ia64__)
429 #elif defined(__mips__)
431 #elif defined(__powerpc__)
432 rel->r_offset = s->dofs_offset +
434 rel->r_info = ELF64_R_INFO(count + dep->de_global,
436 #elif defined(__i386) || defined(__amd64)
437 rel->r_offset = s->dofs_offset +
439 rel->r_info = ELF64_R_INFO(count + dep->de_global,
441 #elif defined(__sparc)
442 rel->r_offset = s->dofs_offset +
444 rel->r_info = ELF64_R_INFO(count + dep->de_global,
451 sym->st_name = base + dofr[j].dofr_name - 1;
454 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
456 sym->st_shndx = SHN_UNDEF;
465 * Add a symbol for the DOF itself. We use a different symbol for
466 * lazily and actively loaded DOF to make them easy to distinguish.
468 sym->st_name = strtabsz;
470 sym->st_size = dof->dofh_filesz;
471 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
473 sym->st_shndx = ESHDR_DOF;
476 if (dtp->dt_lazyload) {
477 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
478 sizeof (DOFLAZYSTR));
479 strtabsz += sizeof (DOFLAZYSTR);
481 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
482 strtabsz += sizeof (DOFSTR);
485 assert(count == dep->de_nrel);
486 assert(strtabsz == dep->de_strlen);
492 * Write out an ELF32 file prologue consisting of a header, section headers,
493 * and a section header string table. The DOF data will follow this prologue
494 * and complete the contents of the given ELF file.
497 dump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
501 Elf32_Shdr shdr[ESHDR_NUM];
510 if (prepare_elf32(dtp, dof, &de) != 0)
511 return (-1); /* errno is set for us */
514 * If there are no relocations, we only need enough sections for
515 * the shstrtab and the DOF.
517 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
519 bzero(&elf_file, sizeof (elf_file));
521 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
522 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
523 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
524 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
525 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
526 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS32;
527 #if BYTE_ORDER == _BIG_ENDIAN
528 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
530 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
532 #if defined(__FreeBSD__)
533 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
535 elf_file.ehdr.e_type = ET_REL;
537 elf_file.ehdr.e_machine = EM_ARM;
538 #elif defined(__ia64__)
539 elf_file.ehdr.e_machine = EM_IA_64;
540 #elif defined(__mips__)
541 elf_file.ehdr.e_machine = EM_MIPS;
542 #elif defined(__powerpc__)
543 elf_file.ehdr.e_machine = EM_PPC;
544 #elif defined(__sparc)
545 elf_file.ehdr.e_machine = EM_SPARC;
546 #elif defined(__i386) || defined(__amd64)
547 elf_file.ehdr.e_machine = EM_386;
549 elf_file.ehdr.e_version = EV_CURRENT;
550 elf_file.ehdr.e_shoff = sizeof (Elf32_Ehdr);
551 elf_file.ehdr.e_ehsize = sizeof (Elf32_Ehdr);
552 elf_file.ehdr.e_phentsize = sizeof (Elf32_Phdr);
553 elf_file.ehdr.e_shentsize = sizeof (Elf32_Shdr);
554 elf_file.ehdr.e_shnum = nshdr;
555 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
556 off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr);
558 shp = &elf_file.shdr[ESHDR_SHSTRTAB];
559 shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */
560 shp->sh_type = SHT_STRTAB;
561 shp->sh_offset = off;
562 shp->sh_size = sizeof (DTRACE_SHSTRTAB32);
563 shp->sh_addralign = sizeof (char);
564 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
566 shp = &elf_file.shdr[ESHDR_DOF];
567 shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */
568 shp->sh_flags = SHF_ALLOC;
569 shp->sh_type = SHT_SUNW_dof;
570 shp->sh_offset = off;
571 shp->sh_size = dof->dofh_filesz;
572 shp->sh_addralign = 8;
573 off = shp->sh_offset + shp->sh_size;
575 shp = &elf_file.shdr[ESHDR_STRTAB];
576 shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */
577 shp->sh_flags = SHF_ALLOC;
578 shp->sh_type = SHT_STRTAB;
579 shp->sh_offset = off;
580 shp->sh_size = de.de_strlen;
581 shp->sh_addralign = sizeof (char);
582 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
584 shp = &elf_file.shdr[ESHDR_SYMTAB];
585 shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */
586 shp->sh_flags = SHF_ALLOC;
587 shp->sh_type = SHT_SYMTAB;
588 shp->sh_entsize = sizeof (Elf32_Sym);
589 shp->sh_link = ESHDR_STRTAB;
590 shp->sh_offset = off;
591 shp->sh_info = de.de_global;
592 shp->sh_size = de.de_nsym * sizeof (Elf32_Sym);
593 shp->sh_addralign = 4;
594 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
596 if (de.de_nrel == 0) {
597 if (dt_write(dtp, fd, &elf_file,
598 sizeof (elf_file)) != sizeof (elf_file) ||
599 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
600 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
601 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
602 PWRITE_SCN(ESHDR_DOF, dof)) {
603 ret = dt_set_errno(dtp, errno);
606 shp = &elf_file.shdr[ESHDR_REL];
607 shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */
608 shp->sh_flags = SHF_ALLOC;
610 shp->sh_type = SHT_RELA;
612 shp->sh_type = SHT_REL;
614 shp->sh_entsize = sizeof (de.de_rel[0]);
615 shp->sh_link = ESHDR_SYMTAB;
616 shp->sh_info = ESHDR_DOF;
617 shp->sh_offset = off;
618 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
619 shp->sh_addralign = 4;
621 if (dt_write(dtp, fd, &elf_file,
622 sizeof (elf_file)) != sizeof (elf_file) ||
623 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
624 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
625 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
626 PWRITE_SCN(ESHDR_REL, de.de_rel) ||
627 PWRITE_SCN(ESHDR_DOF, dof)) {
628 ret = dt_set_errno(dtp, errno);
640 * Write out an ELF64 file prologue consisting of a header, section headers,
641 * and a section header string table. The DOF data will follow this prologue
642 * and complete the contents of the given ELF file.
645 dump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
649 Elf64_Shdr shdr[ESHDR_NUM];
658 if (prepare_elf64(dtp, dof, &de) != 0)
659 return (-1); /* errno is set for us */
662 * If there are no relocations, we only need enough sections for
663 * the shstrtab and the DOF.
665 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
667 bzero(&elf_file, sizeof (elf_file));
669 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
670 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
671 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
672 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
673 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
674 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS64;
675 #if BYTE_ORDER == _BIG_ENDIAN
676 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
678 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
680 #if defined(__FreeBSD__)
681 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
683 elf_file.ehdr.e_type = ET_REL;
685 elf_file.ehdr.e_machine = EM_ARM;
686 #elif defined(__ia64__)
687 elf_file.ehdr.e_machine = EM_IA_64;
688 #elif defined(__mips__)
689 elf_file.ehdr.e_machine = EM_MIPS;
690 #elif defined(__powerpc__)
691 elf_file.ehdr.e_machine = EM_PPC;
692 #elif defined(__sparc)
693 elf_file.ehdr.e_machine = EM_SPARCV9;
694 #elif defined(__i386) || defined(__amd64)
695 elf_file.ehdr.e_machine = EM_AMD64;
697 elf_file.ehdr.e_version = EV_CURRENT;
698 elf_file.ehdr.e_shoff = sizeof (Elf64_Ehdr);
699 elf_file.ehdr.e_ehsize = sizeof (Elf64_Ehdr);
700 elf_file.ehdr.e_phentsize = sizeof (Elf64_Phdr);
701 elf_file.ehdr.e_shentsize = sizeof (Elf64_Shdr);
702 elf_file.ehdr.e_shnum = nshdr;
703 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
704 off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr);
706 shp = &elf_file.shdr[ESHDR_SHSTRTAB];
707 shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */
708 shp->sh_type = SHT_STRTAB;
709 shp->sh_offset = off;
710 shp->sh_size = sizeof (DTRACE_SHSTRTAB64);
711 shp->sh_addralign = sizeof (char);
712 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
714 shp = &elf_file.shdr[ESHDR_DOF];
715 shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */
716 shp->sh_flags = SHF_ALLOC;
717 shp->sh_type = SHT_SUNW_dof;
718 shp->sh_offset = off;
719 shp->sh_size = dof->dofh_filesz;
720 shp->sh_addralign = 8;
721 off = shp->sh_offset + shp->sh_size;
723 shp = &elf_file.shdr[ESHDR_STRTAB];
724 shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */
725 shp->sh_flags = SHF_ALLOC;
726 shp->sh_type = SHT_STRTAB;
727 shp->sh_offset = off;
728 shp->sh_size = de.de_strlen;
729 shp->sh_addralign = sizeof (char);
730 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
732 shp = &elf_file.shdr[ESHDR_SYMTAB];
733 shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */
734 shp->sh_flags = SHF_ALLOC;
735 shp->sh_type = SHT_SYMTAB;
736 shp->sh_entsize = sizeof (Elf64_Sym);
737 shp->sh_link = ESHDR_STRTAB;
738 shp->sh_offset = off;
739 shp->sh_info = de.de_global;
740 shp->sh_size = de.de_nsym * sizeof (Elf64_Sym);
741 shp->sh_addralign = 8;
742 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
744 if (de.de_nrel == 0) {
745 if (dt_write(dtp, fd, &elf_file,
746 sizeof (elf_file)) != sizeof (elf_file) ||
747 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
748 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
749 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
750 PWRITE_SCN(ESHDR_DOF, dof)) {
751 ret = dt_set_errno(dtp, errno);
754 shp = &elf_file.shdr[ESHDR_REL];
755 shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */
756 shp->sh_flags = SHF_ALLOC;
757 shp->sh_type = SHT_RELA;
758 shp->sh_entsize = sizeof (de.de_rel[0]);
759 shp->sh_link = ESHDR_SYMTAB;
760 shp->sh_info = ESHDR_DOF;
761 shp->sh_offset = off;
762 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
763 shp->sh_addralign = 8;
765 if (dt_write(dtp, fd, &elf_file,
766 sizeof (elf_file)) != sizeof (elf_file) ||
767 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
768 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
769 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
770 PWRITE_SCN(ESHDR_REL, de.de_rel) ||
771 PWRITE_SCN(ESHDR_DOF, dof)) {
772 ret = dt_set_errno(dtp, errno);
784 dt_symtab_lookup(Elf_Data *data_sym, int nsym, uintptr_t addr, uint_t shn,
790 for (i = 0; i < nsym && gelf_getsym(data_sym, i, sym) != NULL; i++) {
791 if (GELF_ST_TYPE(sym->st_info) == STT_FUNC &&
792 shn == sym->st_shndx &&
793 sym->st_value <= addr &&
794 addr < sym->st_value + sym->st_size) {
795 if (GELF_ST_BIND(sym->st_info) == STB_GLOBAL)
811 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
814 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
817 #elif defined(__ia64__)
820 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
823 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
826 #elif defined(__mips__)
829 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
832 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
835 #elif defined(__powerpc__)
836 /* The sentinel is 'xor r3,r3,r3'. */
837 #define DT_OP_XOR_R3 0x7c631a78
839 #define DT_OP_NOP 0x60000000
840 #define DT_OP_BLR 0x4e800020
842 /* This captures all forms of branching to address. */
843 #define DT_IS_BRANCH(inst) ((inst & 0xfc000000) == 0x48000000)
844 #define DT_IS_BL(inst) (DT_IS_BRANCH(inst) && (inst & 0x01))
848 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
853 if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
857 ip = (uint32_t *)(p + rela->r_offset);
860 * We only know about some specific relocation types.
862 if (GELF_R_TYPE(rela->r_info) != R_PPC_REL24 &&
863 GELF_R_TYPE(rela->r_info) != R_PPC_PLTREL24)
867 * We may have already processed this object file in an earlier linker
868 * invocation. Check to see if the present instruction sequence matches
869 * the one we would install below.
872 if (ip[0] == DT_OP_XOR_R3) {
873 (*off) += sizeof (ip[0]);
877 if (ip[0] == DT_OP_NOP) {
878 (*off) += sizeof (ip[0]);
884 * We only expect branch to address instructions.
886 if (!DT_IS_BRANCH(ip[0])) {
887 dt_dprintf("found %x instead of a branch instruction at %llx\n",
888 ip[0], (u_longlong_t)rela->r_offset);
894 * It would necessarily indicate incorrect usage if an is-
895 * enabled probe were tail-called so flag that as an error.
896 * It's also potentially (very) tricky to handle gracefully,
897 * but could be done if this were a desired use scenario.
899 if (!DT_IS_BL(ip[0])) {
900 dt_dprintf("tail call to is-enabled probe at %llx\n",
901 (u_longlong_t)rela->r_offset);
905 ip[0] = DT_OP_XOR_R3;
906 (*off) += sizeof (ip[0]);
917 #elif defined(__sparc)
919 #define DT_OP_RET 0x81c7e008
920 #define DT_OP_NOP 0x01000000
921 #define DT_OP_CALL 0x40000000
922 #define DT_OP_CLR_O0 0x90102000
924 #define DT_IS_MOV_O7(inst) (((inst) & 0xffffe000) == 0x9e100000)
925 #define DT_IS_RESTORE(inst) (((inst) & 0xc1f80000) == 0x81e80000)
926 #define DT_IS_RETL(inst) (((inst) & 0xfff83fff) == 0x81c02008)
928 #define DT_RS2(inst) ((inst) & 0x1f)
929 #define DT_MAKE_RETL(reg) (0x81c02008 | ((reg) << 14))
933 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
938 if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
942 ip = (uint32_t *)(p + rela->r_offset);
945 * We only know about some specific relocation types.
947 if (GELF_R_TYPE(rela->r_info) != R_SPARC_WDISP30 &&
948 GELF_R_TYPE(rela->r_info) != R_SPARC_WPLT30)
952 * We may have already processed this object file in an earlier linker
953 * invocation. Check to see if the present instruction sequence matches
954 * the one we would install below.
957 if (ip[0] == DT_OP_NOP) {
958 (*off) += sizeof (ip[0]);
962 if (DT_IS_RESTORE(ip[1])) {
963 if (ip[0] == DT_OP_RET) {
964 (*off) += sizeof (ip[0]);
967 } else if (DT_IS_MOV_O7(ip[1])) {
968 if (DT_IS_RETL(ip[0]))
971 if (ip[0] == DT_OP_NOP) {
972 (*off) += sizeof (ip[0]);
979 * We only expect call instructions with a displacement of 0.
981 if (ip[0] != DT_OP_CALL) {
982 dt_dprintf("found %x instead of a call instruction at %llx\n",
983 ip[0], (u_longlong_t)rela->r_offset);
989 * It would necessarily indicate incorrect usage if an is-
990 * enabled probe were tail-called so flag that as an error.
991 * It's also potentially (very) tricky to handle gracefully,
992 * but could be done if this were a desired use scenario.
994 if (DT_IS_RESTORE(ip[1]) || DT_IS_MOV_O7(ip[1])) {
995 dt_dprintf("tail call to is-enabled probe at %llx\n",
996 (u_longlong_t)rela->r_offset);
1002 * On SPARC, we take advantage of the fact that the first
1003 * argument shares the same register as for the return value.
1004 * The macro handles the work of zeroing that register so we
1005 * don't need to do anything special here. We instrument the
1006 * instruction in the delay slot as we'll need to modify the
1007 * return register after that instruction has been emulated.
1010 (*off) += sizeof (ip[0]);
1013 * If the call is followed by a restore, it's a tail call so
1014 * change the call to a ret. If the call if followed by a mov
1015 * of a register into %o7, it's a tail call in leaf context
1016 * so change the call to a retl-like instruction that returns
1017 * to that register value + 8 (rather than the typical %o7 +
1018 * 8); the delay slot instruction is left, but should have no
1019 * effect. Otherwise we change the call to be a nop. We
1020 * identify the subsequent instruction as the probe point in
1021 * all but the leaf tail-call case to ensure that arguments to
1022 * the probe are complete and consistent. An astute, though
1023 * largely hypothetical, observer would note that there is the
1024 * possibility of a false-positive probe firing if the function
1025 * contained a branch to the instruction in the delay slot of
1026 * the call. Fixing this would require significant in-kernel
1027 * modifications, and isn't worth doing until we see it in the
1030 if (DT_IS_RESTORE(ip[1])) {
1032 (*off) += sizeof (ip[0]);
1033 } else if (DT_IS_MOV_O7(ip[1])) {
1034 ip[0] = DT_MAKE_RETL(DT_RS2(ip[1]));
1037 (*off) += sizeof (ip[0]);
1044 #elif defined(__i386) || defined(__amd64)
1046 #define DT_OP_NOP 0x90
1047 #define DT_OP_RET 0xc3
1048 #define DT_OP_CALL 0xe8
1049 #define DT_OP_JMP32 0xe9
1050 #define DT_OP_REX_RAX 0x48
1051 #define DT_OP_XOR_EAX_0 0x33
1052 #define DT_OP_XOR_EAX_1 0xc0
1055 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
1058 uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1);
1062 * On x86, the first byte of the instruction is the call opcode and
1063 * the next four bytes are the 32-bit address; the relocation is for
1064 * the address operand. We back up the offset to the first byte of
1065 * the instruction. For is-enabled probes, we later advance the offset
1066 * so that it hits the first nop in the instruction sequence.
1071 * We only know about some specific relocation types. Luckily
1072 * these types have the same values on both 32-bit and 64-bit
1073 * x86 architectures.
1075 if (GELF_R_TYPE(rela->r_info) != R_386_PC32 &&
1076 GELF_R_TYPE(rela->r_info) != R_386_PLT32)
1080 * We may have already processed this object file in an earlier linker
1081 * invocation. Check to see if the present instruction sequence matches
1082 * the one we would install. For is-enabled probes, we advance the
1083 * offset to the first nop instruction in the sequence to match the
1084 * text modification code below.
1087 if ((ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET) &&
1088 ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP &&
1089 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP)
1091 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
1092 if (ip[0] == DT_OP_REX_RAX &&
1093 ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 &&
1094 (ip[3] == DT_OP_NOP || ip[3] == DT_OP_RET) &&
1095 ip[4] == DT_OP_NOP) {
1100 if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 &&
1101 (ip[2] == DT_OP_NOP || ip[2] == DT_OP_RET) &&
1102 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) {
1109 * We expect either a call instrution with a 32-bit displacement or a
1110 * jmp instruction with a 32-bit displacement acting as a tail-call.
1112 if (ip[0] != DT_OP_CALL && ip[0] != DT_OP_JMP32) {
1113 dt_dprintf("found %x instead of a call or jmp instruction at "
1114 "%llx\n", ip[0], (u_longlong_t)rela->r_offset);
1118 ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP;
1121 * Establish the instruction sequence -- all nops for probes, and an
1122 * instruction to clear the return value register (%eax/%rax) followed
1123 * by nops for is-enabled probes. For is-enabled probes, we advance
1124 * the offset to the first nop. This isn't stricly necessary but makes
1125 * for more readable disassembly when the probe is enabled.
1133 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
1134 ip[0] = DT_OP_REX_RAX;
1135 ip[1] = DT_OP_XOR_EAX_0;
1136 ip[2] = DT_OP_XOR_EAX_1;
1141 ip[0] = DT_OP_XOR_EAX_0;
1142 ip[1] = DT_OP_XOR_EAX_1;
1158 dt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs,
1159 const char *format, ...)
1162 dt_link_pair_t *pair;
1164 va_start(ap, format);
1165 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1169 (void) elf_end(elf);
1174 while ((pair = bufs) != NULL) {
1175 bufs = pair->dlp_next;
1176 dt_free(dtp, pair->dlp_str);
1177 dt_free(dtp, pair->dlp_sym);
1181 return (dt_set_errno(dtp, EDT_COMPILER));
1185 process_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp)
1187 static const char dt_prefix[] = "__dtrace";
1188 static const char dt_enabled[] = "enabled";
1189 static const char dt_symprefix[] = "$dtrace";
1190 static const char dt_symfmt[] = "%s%ld.%s";
1191 int fd, i, ndx, eprobe, mod = 0;
1194 Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt;
1195 Elf_Data *data_rel, *data_sym, *data_str, *data_tgt;
1196 GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt;
1197 GElf_Sym rsym, fsym, dsym;
1200 char pname[DTRACE_PROVNAMELEN];
1203 uint32_t off, eclass, emachine1, emachine2;
1204 size_t symsize, nsym, isym, istr, len;
1206 dt_link_pair_t *pair, *bufs = NULL;
1207 dt_strtab_t *strtab;
1209 if ((fd = open64(obj, O_RDWR)) == -1) {
1210 return (dt_link_error(dtp, elf, fd, bufs,
1211 "failed to open %s: %s", obj, strerror(errno)));
1214 if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
1215 return (dt_link_error(dtp, elf, fd, bufs,
1216 "failed to process %s: %s", obj, elf_errmsg(elf_errno())));
1219 switch (elf_kind(elf)) {
1223 return (dt_link_error(dtp, elf, fd, bufs, "archives are not "
1224 "permitted; use the contents of the archive instead: %s",
1227 return (dt_link_error(dtp, elf, fd, bufs,
1228 "invalid file type: %s", obj));
1231 if (gelf_getehdr(elf, &ehdr) == NULL) {
1232 return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s",
1236 if (dtp->dt_oflags & DTRACE_O_LP64) {
1237 eclass = ELFCLASS64;
1238 #if defined(__ia64__)
1239 emachine1 = emachine2 = EM_IA_64;
1240 #elif defined(__mips__)
1241 emachine1 = emachine2 = EM_MIPS;
1242 #elif defined(__powerpc__)
1243 emachine1 = emachine2 = EM_PPC64;
1244 #elif defined(__sparc)
1245 emachine1 = emachine2 = EM_SPARCV9;
1246 #elif defined(__i386) || defined(__amd64)
1247 emachine1 = emachine2 = EM_AMD64;
1249 symsize = sizeof (Elf64_Sym);
1251 eclass = ELFCLASS32;
1252 #if defined(__arm__)
1253 emachine1 = emachine2 = EM_ARM;
1254 #elif defined(__mips__)
1255 emachine1 = emachine2 = EM_MIPS;
1256 #elif defined(__powerpc__)
1257 emachine1 = emachine2 = EM_PPC;
1258 #elif defined(__sparc)
1259 emachine1 = EM_SPARC;
1260 emachine2 = EM_SPARC32PLUS;
1261 #elif defined(__i386) || defined(__amd64) || defined(__ia64__)
1262 emachine1 = emachine2 = EM_386;
1264 symsize = sizeof (Elf32_Sym);
1267 if (ehdr.e_ident[EI_CLASS] != eclass) {
1268 return (dt_link_error(dtp, elf, fd, bufs,
1269 "incorrect ELF class for object file: %s", obj));
1272 if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) {
1273 return (dt_link_error(dtp, elf, fd, bufs,
1274 "incorrect ELF machine type for object file: %s", obj));
1278 * We use this token as a relatively unique handle for this file on the
1279 * system in order to disambiguate potential conflicts between files of
1280 * the same name which contain identially named local symbols.
1282 if ((objkey = ftok(obj, 0)) == (key_t)-1) {
1283 return (dt_link_error(dtp, elf, fd, bufs,
1284 "failed to generate unique key for object file: %s", obj));
1288 while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) {
1289 if (gelf_getshdr(scn_rel, &shdr_rel) == NULL)
1293 * Skip any non-relocation sections.
1295 if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL)
1298 if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL)
1302 * Grab the section, section header and section data for the
1303 * symbol table that this relocation section references.
1305 if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL ||
1306 gelf_getshdr(scn_sym, &shdr_sym) == NULL ||
1307 (data_sym = elf_getdata(scn_sym, NULL)) == NULL)
1311 * Ditto for that symbol table's string table.
1313 if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL ||
1314 gelf_getshdr(scn_str, &shdr_str) == NULL ||
1315 (data_str = elf_getdata(scn_str, NULL)) == NULL)
1319 * Grab the section, section header and section data for the
1320 * target section for the relocations. For the relocations
1321 * we're looking for -- this will typically be the text of the
1324 if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL ||
1325 gelf_getshdr(scn_tgt, &shdr_tgt) == NULL ||
1326 (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL)
1330 * We're looking for relocations to symbols matching this form:
1332 * __dtrace[enabled]_<prov>___<probe>
1334 * For the generated object, we need to record the location
1335 * identified by the relocation, and create a new relocation
1336 * in the generated object that will be resolved at link time
1337 * to the location of the function in which the probe is
1338 * embedded. In the target object, we change the matched symbol
1339 * so that it will be ignored at link time, and we modify the
1340 * target (text) section to replace the call instruction with
1343 * If the function containing the probe is locally scoped
1344 * (static), we create an alias used by the relocation in the
1345 * generated object. The alias, a new symbol, will be global
1346 * (so that the relocation from the generated object can be
1347 * resolved), and hidden (so that it is converted to a local
1348 * symbol at link time). Such aliases have this form:
1350 * $dtrace<key>.<function>
1352 * We take a first pass through all the relocations to
1353 * populate our string table and count the number of extra
1354 * symbols we'll require.
1356 strtab = dt_strtab_create(1);
1358 isym = data_sym->d_size / symsize;
1359 istr = data_str->d_size;
1361 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1363 if (shdr_rel.sh_type == SHT_RELA) {
1364 if (gelf_getrela(data_rel, i, &rela) == NULL)
1368 if (gelf_getrel(data_rel, i, &rel) == NULL)
1370 rela.r_offset = rel.r_offset;
1371 rela.r_info = rel.r_info;
1375 if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info),
1377 dt_strtab_destroy(strtab);
1381 s = (char *)data_str->d_buf + rsym.st_name;
1383 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1386 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1387 shdr_rel.sh_info, &fsym) != 0) {
1388 dt_strtab_destroy(strtab);
1392 if (GELF_ST_BIND(fsym.st_info) != STB_LOCAL)
1395 if (fsym.st_name > data_str->d_size) {
1396 dt_strtab_destroy(strtab);
1400 s = (char *)data_str->d_buf + fsym.st_name;
1403 * If this symbol isn't of type function, we've really
1404 * driven off the rails or the object file is corrupt.
1406 if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) {
1407 dt_strtab_destroy(strtab);
1408 return (dt_link_error(dtp, elf, fd, bufs,
1409 "expected %s to be of type function", s));
1412 len = snprintf(NULL, 0, dt_symfmt, dt_symprefix,
1414 if ((p = dt_alloc(dtp, len)) == NULL) {
1415 dt_strtab_destroy(strtab);
1418 (void) snprintf(p, len, dt_symfmt, dt_symprefix,
1421 if (dt_strtab_index(strtab, p) == -1) {
1423 (void) dt_strtab_insert(strtab, p);
1430 * If needed, allocate the additional space for the symbol
1431 * table and string table copying the old data into the new
1432 * buffers, and marking the buffers as dirty. We inject those
1433 * newly allocated buffers into the libelf data structures, but
1434 * are still responsible for freeing them once we're done with
1439 * The first byte of the string table is reserved for
1442 len = dt_strtab_size(strtab) - 1;
1445 assert(dt_strtab_index(strtab, "") == 0);
1447 dt_strtab_destroy(strtab);
1449 if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL)
1452 if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size +
1458 if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size +
1459 nsym * symsize)) == NULL) {
1460 dt_free(dtp, pair->dlp_str);
1465 pair->dlp_next = bufs;
1468 bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size);
1469 data_str->d_buf = pair->dlp_str;
1470 data_str->d_size += len;
1471 (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY);
1473 shdr_str.sh_size += len;
1474 (void) gelf_update_shdr(scn_str, &shdr_str);
1476 bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size);
1477 data_sym->d_buf = pair->dlp_sym;
1478 data_sym->d_size += nsym * symsize;
1479 (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY);
1481 shdr_sym.sh_size += nsym * symsize;
1482 (void) gelf_update_shdr(scn_sym, &shdr_sym);
1486 dt_strtab_destroy(strtab);
1490 * Now that the tables have been allocated, perform the
1491 * modifications described above.
1493 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1495 if (shdr_rel.sh_type == SHT_RELA) {
1496 if (gelf_getrela(data_rel, i, &rela) == NULL)
1500 if (gelf_getrel(data_rel, i, &rel) == NULL)
1502 rela.r_offset = rel.r_offset;
1503 rela.r_info = rel.r_info;
1507 ndx = GELF_R_SYM(rela.r_info);
1509 if (gelf_getsym(data_sym, ndx, &rsym) == NULL ||
1510 rsym.st_name > data_str->d_size)
1513 s = (char *)data_str->d_buf + rsym.st_name;
1515 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1518 s += sizeof (dt_prefix) - 1;
1521 * Check to see if this is an 'is-enabled' check as
1522 * opposed to a normal probe.
1524 if (strncmp(s, dt_enabled,
1525 sizeof (dt_enabled) - 1) == 0) {
1526 s += sizeof (dt_enabled) - 1;
1529 dt_dprintf("is-enabled probe\n");
1532 dt_dprintf("normal probe\n");
1538 if ((p = strstr(s, "___")) == NULL ||
1539 p - s >= sizeof (pname))
1542 bcopy(s, pname, p - s);
1543 pname[p - s] = '\0';
1545 p = strhyphenate(p + 3); /* strlen("___") */
1547 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1548 shdr_rel.sh_info, &fsym) != 0)
1551 if (fsym.st_name > data_str->d_size)
1554 assert(GELF_ST_TYPE(fsym.st_info) == STT_FUNC);
1557 * If a NULL relocation name is passed to
1558 * dt_probe_define(), the function name is used for the
1559 * relocation. The relocation needs to use a mangled
1560 * name if the symbol is locally scoped; the function
1561 * name may need to change if we've found the global
1562 * alias for the locally scoped symbol (we prefer
1563 * global symbols to locals in dt_symtab_lookup()).
1565 s = (char *)data_str->d_buf + fsym.st_name;
1568 if (GELF_ST_BIND(fsym.st_info) == STB_LOCAL) {
1570 dsym.st_name = istr;
1571 dsym.st_info = GELF_ST_INFO(STB_GLOBAL,
1574 ELF64_ST_VISIBILITY(STV_ELIMINATE);
1575 (void) gelf_update_sym(data_sym, isym, &dsym);
1577 r = (char *)data_str->d_buf + istr;
1578 istr += 1 + sprintf(r, dt_symfmt,
1579 dt_symprefix, objkey, s);
1581 assert(isym <= nsym);
1583 } else if (strncmp(s, dt_symprefix,
1584 strlen(dt_symprefix)) == 0) {
1586 if ((s = strchr(s, '.')) == NULL)
1591 if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) {
1592 return (dt_link_error(dtp, elf, fd, bufs,
1593 "no such provider %s", pname));
1596 if ((prp = dt_probe_lookup(pvp, p)) == NULL) {
1597 return (dt_link_error(dtp, elf, fd, bufs,
1598 "no such probe %s", p));
1601 assert(fsym.st_value <= rela.r_offset);
1603 off = rela.r_offset - fsym.st_value;
1604 if (dt_modtext(dtp, data_tgt->d_buf, eprobe,
1608 if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) {
1609 return (dt_link_error(dtp, elf, fd, bufs,
1610 "failed to allocate space for probe"));
1614 * Our linker doesn't understand the SUNW_IGNORE ndx and
1615 * will try to use this relocation when we build the
1616 * final executable. Since we are done processing this
1617 * relocation, mark it as inexistant and let libelf
1618 * remove it from the file.
1619 * If this wasn't done, we would have garbage added to
1620 * the executable file as the symbol is going to be
1621 * change from UND to ABS.
1623 if (shdr_rel.sh_type == SHT_RELA) {
1627 (void) gelf_update_rela(data_rel, i, &rela);
1632 (void) gelf_update_rel(data_rel, i, &rel);
1637 (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY);
1640 * This symbol may already have been marked to
1641 * be ignored by another relocation referencing
1642 * the same symbol or if this object file has
1643 * already been processed by an earlier link
1647 #define SHN_SUNW_IGNORE SHN_ABS
1649 if (rsym.st_shndx != SHN_SUNW_IGNORE) {
1650 rsym.st_shndx = SHN_SUNW_IGNORE;
1651 (void) gelf_update_sym(data_sym, ndx, &rsym);
1656 if (mod && elf_update(elf, ELF_C_WRITE) == -1)
1659 (void) elf_end(elf);
1665 while ((pair = bufs) != NULL) {
1666 bufs = pair->dlp_next;
1667 dt_free(dtp, pair->dlp_str);
1668 dt_free(dtp, pair->dlp_sym);
1675 return (dt_link_error(dtp, elf, fd, bufs,
1676 "an error was encountered while processing %s", obj));
1680 dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
1681 const char *file, int objc, char *const objv[])
1684 char tfile[PATH_MAX];
1699 char drti[PATH_MAX];
1701 int fd, status, i, cur;
1704 int eprobes = 0, ret = 0;
1707 if (access(file, R_OK) == 0) {
1708 fprintf(stderr, "dtrace: target object (%s) already exists. "
1709 "Please remove the target\ndtrace: object and rebuild all "
1710 "the source objects if you wish to run the DTrace\n"
1711 "dtrace: linking process again\n", file);
1713 * Several build infrastructures run DTrace twice (e.g.
1714 * postgres) and we don't want the build to fail. Return
1715 * 0 here since this isn't really a fatal error.
1719 /* XXX Should get a temp file name here. */
1720 snprintf(tfile, sizeof(tfile), "%s.tmp", file);
1724 * A NULL program indicates a special use in which we just link
1725 * together a bunch of object files specified in objv and then
1726 * unlink(2) those object files.
1729 const char *fmt = "%s -o %s -r";
1731 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1;
1733 for (i = 0; i < objc; i++)
1734 len += strlen(objv[i]) + 1;
1738 cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file);
1740 for (i = 0; i < objc; i++)
1741 cur += snprintf(cmd + cur, len - cur, " %s", objv[i]);
1743 if ((status = system(cmd)) == -1) {
1744 return (dt_link_error(dtp, NULL, -1, NULL,
1745 "failed to run %s: %s", dtp->dt_ld_path,
1749 if (WIFSIGNALED(status)) {
1750 return (dt_link_error(dtp, NULL, -1, NULL,
1751 "failed to link %s: %s failed due to signal %d",
1752 file, dtp->dt_ld_path, WTERMSIG(status)));
1755 if (WEXITSTATUS(status) != 0) {
1756 return (dt_link_error(dtp, NULL, -1, NULL,
1757 "failed to link %s: %s exited with status %d\n",
1758 file, dtp->dt_ld_path, WEXITSTATUS(status)));
1761 for (i = 0; i < objc; i++) {
1762 if (strcmp(objv[i], file) != 0)
1763 (void) unlink(objv[i]);
1769 for (i = 0; i < objc; i++) {
1770 if (process_obj(dtp, objv[i], &eprobes) != 0)
1771 return (-1); /* errno is set for us */
1775 * If there are is-enabled probes then we need to force use of DOF
1778 if (eprobes && pgp->dp_dofversion < DOF_VERSION_2)
1779 pgp->dp_dofversion = DOF_VERSION_2;
1781 if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL)
1782 return (-1); /* errno is set for us */
1786 * Create a temporary file and then unlink it if we're going to
1787 * combine it with drti.o later. We can still refer to it in child
1788 * processes as /dev/fd/<fd>.
1790 if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
1791 return (dt_link_error(dtp, NULL, -1, NULL,
1792 "failed to open %s: %s", file, strerror(errno)));
1795 if ((fd = open(tfile, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
1796 return (dt_link_error(dtp, NULL, -1, NULL,
1797 "failed to open %s: %s", tfile, strerror(errno)));
1801 * If -xlinktype=DOF has been selected, just write out the DOF.
1802 * Otherwise proceed to the default of generating and linking ELF.
1804 switch (dtp->dt_linktype) {
1806 if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz)
1809 if (close(fd) != 0 && ret == 0)
1813 return (dt_link_error(dtp, NULL, -1, NULL,
1814 "failed to write %s: %s", file, strerror(ret)));
1820 break; /* fall through to the rest of dtrace_program_link() */
1823 return (dt_link_error(dtp, NULL, -1, NULL,
1824 "invalid link type %u\n", dtp->dt_linktype));
1829 if (!dtp->dt_lazyload)
1830 (void) unlink(file);
1834 if (dtp->dt_oflags & DTRACE_O_LP64)
1835 status = dump_elf64(dtp, dof, fd);
1837 status = dump_elf32(dtp, dof, fd);
1839 if (status != 0 || lseek(fd, 0, SEEK_SET) != 0) {
1841 /* We don't write the ELF header, just the DOF section */
1842 if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz) {
1844 return (dt_link_error(dtp, NULL, -1, NULL,
1845 "failed to write %s: %s", file, strerror(errno)));
1848 if (!dtp->dt_lazyload) {
1850 const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s";
1852 if (dtp->dt_oflags & DTRACE_O_LP64) {
1853 (void) snprintf(drti, sizeof (drti),
1854 "%s/64/drti.o", _dtrace_libdir);
1856 (void) snprintf(drti, sizeof (drti),
1857 "%s/drti.o", _dtrace_libdir);
1860 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd,
1865 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti);
1867 const char *fmt = "%s -o %s -r %s";
1869 #if defined(__amd64__)
1871 * Arches which default to 64-bit need to explicitly use
1872 * the 32-bit library path.
1874 int use_32 = (dtp->dt_oflags & DTRACE_O_ILP32);
1877 * Arches which are 32-bit only just use the normal
1883 (void) snprintf(drti, sizeof (drti), "/usr/lib%s/dtrace/drti.o",
1886 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, tfile,
1892 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file,
1895 if ((status = system(cmd)) == -1) {
1896 ret = dt_link_error(dtp, NULL, -1, NULL,
1897 "failed to run %s: %s", dtp->dt_ld_path,
1902 if (WIFSIGNALED(status)) {
1903 ret = dt_link_error(dtp, NULL, -1, NULL,
1904 "failed to link %s: %s failed due to signal %d",
1905 file, dtp->dt_ld_path, WTERMSIG(status));
1909 if (WEXITSTATUS(status) != 0) {
1910 ret = dt_link_error(dtp, NULL, -1, NULL,
1911 "failed to link %s: %s exited with status %d\n",
1912 file, dtp->dt_ld_path, WEXITSTATUS(status));
1917 * FreeBSD's ld(1) is not instructed to interpret and add
1918 * correctly the SUNW_dof section present in tfile.
1919 * We use libelf to add this section manually and hope the next
1920 * ld invocation won't remove it.
1922 elf_version(EV_CURRENT);
1923 if ((efd = open(file, O_RDWR, 0)) < 0) {
1924 ret = dt_link_error(dtp, NULL, -1, NULL,
1925 "failed to open file %s: %s",
1926 file, strerror(errno));
1929 if ((e = elf_begin(efd, ELF_C_RDWR, NULL)) == NULL) {
1931 ret = dt_link_error(dtp, NULL, -1, NULL,
1932 "failed to open elf file: %s",
1933 elf_errmsg(elf_errno()));
1937 * Add the string '.SUWN_dof' to the shstrtab section.
1939 elf_getshdrstrndx(e, &stridx);
1940 scn = elf_getscn(e, stridx);
1941 gelf_getshdr(scn, &shdr);
1942 data = elf_newdata(scn);
1943 data->d_off = shdr.sh_size;
1944 data->d_buf = ".SUNW_dof";
1946 data->d_type = ELF_T_BYTE;
1948 shdr.sh_size += data->d_size;
1949 gelf_update_shdr(scn, &shdr);
1951 * Construct the .SUNW_dof section.
1953 scn = elf_newscn(e);
1954 data = elf_newdata(scn);
1955 buf = mmap(NULL, dof->dofh_filesz, PROT_READ, MAP_SHARED,
1957 if (buf == MAP_FAILED) {
1958 ret = dt_link_error(dtp, NULL, -1, NULL,
1959 "failed to mmap buffer %s", strerror(errno));
1966 data->d_size = dof->dofh_filesz;
1967 data->d_version = EV_CURRENT;
1968 gelf_getshdr(scn, &shdr);
1970 shdr.sh_flags = SHF_ALLOC;
1972 * Actually this should be SHT_SUNW_dof, but FreeBSD's ld(1)
1973 * will remove this 'unknown' section when we try to create an
1974 * executable using the object we are modifying, so we stop
1975 * playing by the rules and use SHT_PROGBITS.
1976 * Also, note that our drti has modifications to handle this.
1978 shdr.sh_type = SHT_PROGBITS;
1979 shdr.sh_addralign = 4;
1980 gelf_update_shdr(scn, &shdr);
1981 if (elf_update(e, ELF_C_WRITE) < 0) {
1982 ret = dt_link_error(dtp, NULL, -1, NULL,
1983 "failed to add the SUNW_dof section: %s",
1984 elf_errmsg(elf_errno()));
1985 munmap(buf, dof->dofh_filesz);
1990 munmap(buf, dof->dofh_filesz);
1994 (void) close(fd); /* release temporary file */
2000 dtrace_dof_destroy(dtp, dof);