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 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
247 #elif defined(__sparc)
249 * Add 4 bytes to hit the low half of this 64-bit
250 * big-endian address.
252 rel->r_offset = s->dofs_offset +
253 dofr[j].dofr_offset + 4;
254 rel->r_info = ELF32_R_INFO(count + dep->de_global,
260 sym->st_name = base + dofr[j].dofr_name - 1;
263 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC);
265 sym->st_shndx = SHN_UNDEF;
274 * Add a symbol for the DOF itself. We use a different symbol for
275 * lazily and actively loaded DOF to make them easy to distinguish.
277 sym->st_name = strtabsz;
279 sym->st_size = dof->dofh_filesz;
280 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT);
282 sym->st_shndx = ESHDR_DOF;
285 if (dtp->dt_lazyload) {
286 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
287 sizeof (DOFLAZYSTR));
288 strtabsz += sizeof (DOFLAZYSTR);
290 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
291 strtabsz += sizeof (DOFSTR);
294 assert(count == dep->de_nrel);
295 assert(strtabsz == dep->de_strlen);
301 typedef struct dof_elf64 {
314 prepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep)
317 dof_relohdr_t *dofrh;
318 dof_relodesc_t *dofr;
328 dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff);
331 * First compute the size of the string table and the number of
332 * relocations present in the DOF.
334 for (i = 0; i < dof->dofh_secnum; i++) {
335 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
339 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
341 s = &dofs[dofrh->dofr_strtab];
342 strtab = (char *)dof + s->dofs_offset;
343 assert(strtab[0] == '\0');
344 strtabsz += s->dofs_size - 1;
346 s = &dofs[dofrh->dofr_relsec];
348 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
349 count += s->dofs_size / s->dofs_entsize;
352 dep->de_strlen = strtabsz;
353 dep->de_nrel = count;
354 dep->de_nsym = count + 1; /* the first symbol is always null */
356 if (dtp->dt_lazyload) {
357 dep->de_strlen += sizeof (DOFLAZYSTR);
360 dep->de_strlen += sizeof (DOFSTR);
364 if ((dep->de_rel = calloc(dep->de_nrel,
365 sizeof (dep->de_rel[0]))) == NULL) {
366 return (dt_set_errno(dtp, EDT_NOMEM));
369 if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) {
371 return (dt_set_errno(dtp, EDT_NOMEM));
374 if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) {
377 return (dt_set_errno(dtp, EDT_NOMEM));
382 dep->de_strtab[0] = '\0';
388 * The first symbol table entry must be zeroed and is always ignored.
390 bzero(sym, sizeof (Elf64_Sym));
394 * Take a second pass through the DOF sections filling in the
395 * memory we allocated.
397 for (i = 0; i < dof->dofh_secnum; i++) {
398 if (dofs[i].dofs_type != DOF_SECT_URELHDR)
402 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset);
404 s = &dofs[dofrh->dofr_strtab];
405 strtab = (char *)dof + s->dofs_offset;
406 bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size);
408 strtabsz += s->dofs_size - 1;
410 s = &dofs[dofrh->dofr_relsec];
412 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset);
413 nrel = s->dofs_size / s->dofs_entsize;
415 s = &dofs[dofrh->dofr_tgtsec];
417 for (j = 0; j < nrel; j++) {
421 #elif defined(__ia64__)
423 #elif defined(__mips__)
425 #elif defined(__powerpc__)
427 #elif defined(__i386) || defined(__amd64)
428 rel->r_offset = s->dofs_offset +
430 rel->r_info = ELF64_R_INFO(count + dep->de_global,
432 #elif defined(__sparc)
433 rel->r_offset = s->dofs_offset +
435 rel->r_info = ELF64_R_INFO(count + dep->de_global,
442 sym->st_name = base + dofr[j].dofr_name - 1;
445 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC);
447 sym->st_shndx = SHN_UNDEF;
456 * Add a symbol for the DOF itself. We use a different symbol for
457 * lazily and actively loaded DOF to make them easy to distinguish.
459 sym->st_name = strtabsz;
461 sym->st_size = dof->dofh_filesz;
462 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
464 sym->st_shndx = ESHDR_DOF;
467 if (dtp->dt_lazyload) {
468 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz,
469 sizeof (DOFLAZYSTR));
470 strtabsz += sizeof (DOFLAZYSTR);
472 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR));
473 strtabsz += sizeof (DOFSTR);
476 assert(count == dep->de_nrel);
477 assert(strtabsz == dep->de_strlen);
483 * Write out an ELF32 file prologue consisting of a header, section headers,
484 * and a section header string table. The DOF data will follow this prologue
485 * and complete the contents of the given ELF file.
488 dump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
492 Elf32_Shdr shdr[ESHDR_NUM];
501 if (prepare_elf32(dtp, dof, &de) != 0)
502 return (-1); /* errno is set for us */
505 * If there are no relocations, we only need enough sections for
506 * the shstrtab and the DOF.
508 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
510 bzero(&elf_file, sizeof (elf_file));
512 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
513 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
514 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
515 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
516 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
517 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS32;
518 #if BYTE_ORDER == _BIG_ENDIAN
519 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
521 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
523 #if defined(__FreeBSD__)
524 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
526 elf_file.ehdr.e_type = ET_REL;
528 elf_file.ehdr.e_machine = EM_ARM;
529 #elif defined(__ia64__)
530 elf_file.ehdr.e_machine = EM_IA_64;
531 #elif defined(__mips__)
532 elf_file.ehdr.e_machine = EM_MIPS;
533 #elif defined(__powerpc__)
534 elf_file.ehdr.e_machine = EM_PPC;
535 #elif defined(__sparc)
536 elf_file.ehdr.e_machine = EM_SPARC;
537 #elif defined(__i386) || defined(__amd64)
538 elf_file.ehdr.e_machine = EM_386;
540 elf_file.ehdr.e_version = EV_CURRENT;
541 elf_file.ehdr.e_shoff = sizeof (Elf32_Ehdr);
542 elf_file.ehdr.e_ehsize = sizeof (Elf32_Ehdr);
543 elf_file.ehdr.e_phentsize = sizeof (Elf32_Phdr);
544 elf_file.ehdr.e_shentsize = sizeof (Elf32_Shdr);
545 elf_file.ehdr.e_shnum = nshdr;
546 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
547 off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr);
549 shp = &elf_file.shdr[ESHDR_SHSTRTAB];
550 shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */
551 shp->sh_type = SHT_STRTAB;
552 shp->sh_offset = off;
553 shp->sh_size = sizeof (DTRACE_SHSTRTAB32);
554 shp->sh_addralign = sizeof (char);
555 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
557 shp = &elf_file.shdr[ESHDR_DOF];
558 shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */
559 shp->sh_flags = SHF_ALLOC;
560 shp->sh_type = SHT_SUNW_dof;
561 shp->sh_offset = off;
562 shp->sh_size = dof->dofh_filesz;
563 shp->sh_addralign = 8;
564 off = shp->sh_offset + shp->sh_size;
566 shp = &elf_file.shdr[ESHDR_STRTAB];
567 shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */
568 shp->sh_flags = SHF_ALLOC;
569 shp->sh_type = SHT_STRTAB;
570 shp->sh_offset = off;
571 shp->sh_size = de.de_strlen;
572 shp->sh_addralign = sizeof (char);
573 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
575 shp = &elf_file.shdr[ESHDR_SYMTAB];
576 shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */
577 shp->sh_flags = SHF_ALLOC;
578 shp->sh_type = SHT_SYMTAB;
579 shp->sh_entsize = sizeof (Elf32_Sym);
580 shp->sh_link = ESHDR_STRTAB;
581 shp->sh_offset = off;
582 shp->sh_info = de.de_global;
583 shp->sh_size = de.de_nsym * sizeof (Elf32_Sym);
584 shp->sh_addralign = 4;
585 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4);
587 if (de.de_nrel == 0) {
588 if (dt_write(dtp, fd, &elf_file,
589 sizeof (elf_file)) != sizeof (elf_file) ||
590 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
591 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
592 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
593 PWRITE_SCN(ESHDR_DOF, dof)) {
594 ret = dt_set_errno(dtp, errno);
597 shp = &elf_file.shdr[ESHDR_REL];
598 shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */
599 shp->sh_flags = SHF_ALLOC;
601 shp->sh_type = SHT_RELA;
603 shp->sh_type = SHT_REL;
605 shp->sh_entsize = sizeof (de.de_rel[0]);
606 shp->sh_link = ESHDR_SYMTAB;
607 shp->sh_info = ESHDR_DOF;
608 shp->sh_offset = off;
609 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
610 shp->sh_addralign = 4;
612 if (dt_write(dtp, fd, &elf_file,
613 sizeof (elf_file)) != sizeof (elf_file) ||
614 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) ||
615 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
616 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
617 PWRITE_SCN(ESHDR_REL, de.de_rel) ||
618 PWRITE_SCN(ESHDR_DOF, dof)) {
619 ret = dt_set_errno(dtp, errno);
631 * Write out an ELF64 file prologue consisting of a header, section headers,
632 * and a section header string table. The DOF data will follow this prologue
633 * and complete the contents of the given ELF file.
636 dump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd)
640 Elf64_Shdr shdr[ESHDR_NUM];
649 if (prepare_elf64(dtp, dof, &de) != 0)
650 return (-1); /* errno is set for us */
653 * If there are no relocations, we only need enough sections for
654 * the shstrtab and the DOF.
656 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM;
658 bzero(&elf_file, sizeof (elf_file));
660 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0;
661 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1;
662 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2;
663 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3;
664 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT;
665 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS64;
666 #if BYTE_ORDER == _BIG_ENDIAN
667 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
669 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
671 #if defined(__FreeBSD__)
672 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
674 elf_file.ehdr.e_type = ET_REL;
676 elf_file.ehdr.e_machine = EM_ARM;
677 #elif defined(__ia64__)
678 elf_file.ehdr.e_machine = EM_IA_64;
679 #elif defined(__mips__)
680 elf_file.ehdr.e_machine = EM_MIPS;
681 #elif defined(__powerpc__)
682 elf_file.ehdr.e_machine = EM_PPC;
683 #elif defined(__sparc)
684 elf_file.ehdr.e_machine = EM_SPARCV9;
685 #elif defined(__i386) || defined(__amd64)
686 elf_file.ehdr.e_machine = EM_AMD64;
688 elf_file.ehdr.e_version = EV_CURRENT;
689 elf_file.ehdr.e_shoff = sizeof (Elf64_Ehdr);
690 elf_file.ehdr.e_ehsize = sizeof (Elf64_Ehdr);
691 elf_file.ehdr.e_phentsize = sizeof (Elf64_Phdr);
692 elf_file.ehdr.e_shentsize = sizeof (Elf64_Shdr);
693 elf_file.ehdr.e_shnum = nshdr;
694 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB;
695 off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr);
697 shp = &elf_file.shdr[ESHDR_SHSTRTAB];
698 shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */
699 shp->sh_type = SHT_STRTAB;
700 shp->sh_offset = off;
701 shp->sh_size = sizeof (DTRACE_SHSTRTAB64);
702 shp->sh_addralign = sizeof (char);
703 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
705 shp = &elf_file.shdr[ESHDR_DOF];
706 shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */
707 shp->sh_flags = SHF_ALLOC;
708 shp->sh_type = SHT_SUNW_dof;
709 shp->sh_offset = off;
710 shp->sh_size = dof->dofh_filesz;
711 shp->sh_addralign = 8;
712 off = shp->sh_offset + shp->sh_size;
714 shp = &elf_file.shdr[ESHDR_STRTAB];
715 shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */
716 shp->sh_flags = SHF_ALLOC;
717 shp->sh_type = SHT_STRTAB;
718 shp->sh_offset = off;
719 shp->sh_size = de.de_strlen;
720 shp->sh_addralign = sizeof (char);
721 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
723 shp = &elf_file.shdr[ESHDR_SYMTAB];
724 shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */
725 shp->sh_flags = SHF_ALLOC;
726 shp->sh_type = SHT_SYMTAB;
727 shp->sh_entsize = sizeof (Elf64_Sym);
728 shp->sh_link = ESHDR_STRTAB;
729 shp->sh_offset = off;
730 shp->sh_info = de.de_global;
731 shp->sh_size = de.de_nsym * sizeof (Elf64_Sym);
732 shp->sh_addralign = 8;
733 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8);
735 if (de.de_nrel == 0) {
736 if (dt_write(dtp, fd, &elf_file,
737 sizeof (elf_file)) != sizeof (elf_file) ||
738 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
739 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
740 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
741 PWRITE_SCN(ESHDR_DOF, dof)) {
742 ret = dt_set_errno(dtp, errno);
745 shp = &elf_file.shdr[ESHDR_REL];
746 shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */
747 shp->sh_flags = SHF_ALLOC;
748 shp->sh_type = SHT_RELA;
749 shp->sh_entsize = sizeof (de.de_rel[0]);
750 shp->sh_link = ESHDR_SYMTAB;
751 shp->sh_info = ESHDR_DOF;
752 shp->sh_offset = off;
753 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]);
754 shp->sh_addralign = 8;
756 if (dt_write(dtp, fd, &elf_file,
757 sizeof (elf_file)) != sizeof (elf_file) ||
758 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) ||
759 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) ||
760 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) ||
761 PWRITE_SCN(ESHDR_REL, de.de_rel) ||
762 PWRITE_SCN(ESHDR_DOF, dof)) {
763 ret = dt_set_errno(dtp, errno);
775 dt_symtab_lookup(Elf_Data *data_sym, int nsym, uintptr_t addr, uint_t shn,
781 for (i = 0; i < nsym && gelf_getsym(data_sym, i, sym) != NULL; i++) {
782 if (GELF_ST_TYPE(sym->st_info) == STT_FUNC &&
783 shn == sym->st_shndx &&
784 sym->st_value <= addr &&
785 addr < sym->st_value + sym->st_size) {
786 if (GELF_ST_BIND(sym->st_info) == STB_GLOBAL)
802 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
805 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
808 #elif defined(__ia64__)
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(__mips__)
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(__powerpc__)
829 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
832 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
836 #elif defined(__sparc)
838 #define DT_OP_RET 0x81c7e008
839 #define DT_OP_NOP 0x01000000
840 #define DT_OP_CALL 0x40000000
841 #define DT_OP_CLR_O0 0x90102000
843 #define DT_IS_MOV_O7(inst) (((inst) & 0xffffe000) == 0x9e100000)
844 #define DT_IS_RESTORE(inst) (((inst) & 0xc1f80000) == 0x81e80000)
845 #define DT_IS_RETL(inst) (((inst) & 0xfff83fff) == 0x81c02008)
847 #define DT_RS2(inst) ((inst) & 0x1f)
848 #define DT_MAKE_RETL(reg) (0x81c02008 | ((reg) << 14))
852 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
857 if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0)
861 ip = (uint32_t *)(p + rela->r_offset);
864 * We only know about some specific relocation types.
866 if (GELF_R_TYPE(rela->r_info) != R_SPARC_WDISP30 &&
867 GELF_R_TYPE(rela->r_info) != R_SPARC_WPLT30)
871 * We may have already processed this object file in an earlier linker
872 * invocation. Check to see if the present instruction sequence matches
873 * the one we would install below.
876 if (ip[0] == DT_OP_NOP) {
877 (*off) += sizeof (ip[0]);
881 if (DT_IS_RESTORE(ip[1])) {
882 if (ip[0] == DT_OP_RET) {
883 (*off) += sizeof (ip[0]);
886 } else if (DT_IS_MOV_O7(ip[1])) {
887 if (DT_IS_RETL(ip[0]))
890 if (ip[0] == DT_OP_NOP) {
891 (*off) += sizeof (ip[0]);
898 * We only expect call instructions with a displacement of 0.
900 if (ip[0] != DT_OP_CALL) {
901 dt_dprintf("found %x instead of a call instruction at %llx\n",
902 ip[0], (u_longlong_t)rela->r_offset);
908 * It would necessarily indicate incorrect usage if an is-
909 * enabled probe were tail-called so flag that as an error.
910 * It's also potentially (very) tricky to handle gracefully,
911 * but could be done if this were a desired use scenario.
913 if (DT_IS_RESTORE(ip[1]) || DT_IS_MOV_O7(ip[1])) {
914 dt_dprintf("tail call to is-enabled probe at %llx\n",
915 (u_longlong_t)rela->r_offset);
921 * On SPARC, we take advantage of the fact that the first
922 * argument shares the same register as for the return value.
923 * The macro handles the work of zeroing that register so we
924 * don't need to do anything special here. We instrument the
925 * instruction in the delay slot as we'll need to modify the
926 * return register after that instruction has been emulated.
929 (*off) += sizeof (ip[0]);
932 * If the call is followed by a restore, it's a tail call so
933 * change the call to a ret. If the call if followed by a mov
934 * of a register into %o7, it's a tail call in leaf context
935 * so change the call to a retl-like instruction that returns
936 * to that register value + 8 (rather than the typical %o7 +
937 * 8); the delay slot instruction is left, but should have no
938 * effect. Otherwise we change the call to be a nop. We
939 * identify the subsequent instruction as the probe point in
940 * all but the leaf tail-call case to ensure that arguments to
941 * the probe are complete and consistent. An astute, though
942 * largely hypothetical, observer would note that there is the
943 * possibility of a false-positive probe firing if the function
944 * contained a branch to the instruction in the delay slot of
945 * the call. Fixing this would require significant in-kernel
946 * modifications, and isn't worth doing until we see it in the
949 if (DT_IS_RESTORE(ip[1])) {
951 (*off) += sizeof (ip[0]);
952 } else if (DT_IS_MOV_O7(ip[1])) {
953 ip[0] = DT_MAKE_RETL(DT_RS2(ip[1]));
956 (*off) += sizeof (ip[0]);
963 #elif defined(__i386) || defined(__amd64)
965 #define DT_OP_NOP 0x90
966 #define DT_OP_RET 0xc3
967 #define DT_OP_CALL 0xe8
968 #define DT_OP_JMP32 0xe9
969 #define DT_OP_REX_RAX 0x48
970 #define DT_OP_XOR_EAX_0 0x33
971 #define DT_OP_XOR_EAX_1 0xc0
974 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela,
977 uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1);
981 * On x86, the first byte of the instruction is the call opcode and
982 * the next four bytes are the 32-bit address; the relocation is for
983 * the address operand. We back up the offset to the first byte of
984 * the instruction. For is-enabled probes, we later advance the offset
985 * so that it hits the first nop in the instruction sequence.
990 * We only know about some specific relocation types. Luckily
991 * these types have the same values on both 32-bit and 64-bit
994 if (GELF_R_TYPE(rela->r_info) != R_386_PC32 &&
995 GELF_R_TYPE(rela->r_info) != R_386_PLT32)
999 * We may have already processed this object file in an earlier linker
1000 * invocation. Check to see if the present instruction sequence matches
1001 * the one we would install. For is-enabled probes, we advance the
1002 * offset to the first nop instruction in the sequence to match the
1003 * text modification code below.
1006 if ((ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET) &&
1007 ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP &&
1008 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP)
1010 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
1011 if (ip[0] == DT_OP_REX_RAX &&
1012 ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 &&
1013 (ip[3] == DT_OP_NOP || ip[3] == DT_OP_RET) &&
1014 ip[4] == DT_OP_NOP) {
1019 if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 &&
1020 (ip[2] == DT_OP_NOP || ip[2] == DT_OP_RET) &&
1021 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) {
1028 * We expect either a call instrution with a 32-bit displacement or a
1029 * jmp instruction with a 32-bit displacement acting as a tail-call.
1031 if (ip[0] != DT_OP_CALL && ip[0] != DT_OP_JMP32) {
1032 dt_dprintf("found %x instead of a call or jmp instruction at "
1033 "%llx\n", ip[0], (u_longlong_t)rela->r_offset);
1037 ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP;
1040 * Establish the instruction sequence -- all nops for probes, and an
1041 * instruction to clear the return value register (%eax/%rax) followed
1042 * by nops for is-enabled probes. For is-enabled probes, we advance
1043 * the offset to the first nop. This isn't stricly necessary but makes
1044 * for more readable disassembly when the probe is enabled.
1052 } else if (dtp->dt_oflags & DTRACE_O_LP64) {
1053 ip[0] = DT_OP_REX_RAX;
1054 ip[1] = DT_OP_XOR_EAX_0;
1055 ip[2] = DT_OP_XOR_EAX_1;
1060 ip[0] = DT_OP_XOR_EAX_0;
1061 ip[1] = DT_OP_XOR_EAX_1;
1077 dt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs,
1078 const char *format, ...)
1081 dt_link_pair_t *pair;
1083 va_start(ap, format);
1084 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
1088 (void) elf_end(elf);
1093 while ((pair = bufs) != NULL) {
1094 bufs = pair->dlp_next;
1095 dt_free(dtp, pair->dlp_str);
1096 dt_free(dtp, pair->dlp_sym);
1100 return (dt_set_errno(dtp, EDT_COMPILER));
1104 process_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp)
1106 static const char dt_prefix[] = "__dtrace";
1107 static const char dt_enabled[] = "enabled";
1108 static const char dt_symprefix[] = "$dtrace";
1109 static const char dt_symfmt[] = "%s%ld.%s";
1110 int fd, i, ndx, eprobe, mod = 0;
1113 Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt;
1114 Elf_Data *data_rel, *data_sym, *data_str, *data_tgt;
1115 GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt;
1116 GElf_Sym rsym, fsym, dsym;
1119 char pname[DTRACE_PROVNAMELEN];
1122 uint32_t off, eclass, emachine1, emachine2;
1123 size_t symsize, nsym, isym, istr, len;
1125 dt_link_pair_t *pair, *bufs = NULL;
1126 dt_strtab_t *strtab;
1128 if ((fd = open64(obj, O_RDWR)) == -1) {
1129 return (dt_link_error(dtp, elf, fd, bufs,
1130 "failed to open %s: %s", obj, strerror(errno)));
1133 if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) {
1134 return (dt_link_error(dtp, elf, fd, bufs,
1135 "failed to process %s: %s", obj, elf_errmsg(elf_errno())));
1138 switch (elf_kind(elf)) {
1142 return (dt_link_error(dtp, elf, fd, bufs, "archives are not "
1143 "permitted; use the contents of the archive instead: %s",
1146 return (dt_link_error(dtp, elf, fd, bufs,
1147 "invalid file type: %s", obj));
1150 if (gelf_getehdr(elf, &ehdr) == NULL) {
1151 return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s",
1155 if (dtp->dt_oflags & DTRACE_O_LP64) {
1156 eclass = ELFCLASS64;
1157 #if defined(__ia64__)
1158 emachine1 = emachine2 = EM_IA_64;
1159 #elif defined(__mips__)
1160 emachine1 = emachine2 = EM_MIPS;
1161 #elif defined(__powerpc__)
1162 emachine1 = emachine2 = EM_PPC64;
1163 #elif defined(__sparc)
1164 emachine1 = emachine2 = EM_SPARCV9;
1165 #elif defined(__i386) || defined(__amd64)
1166 emachine1 = emachine2 = EM_AMD64;
1168 symsize = sizeof (Elf64_Sym);
1170 eclass = ELFCLASS32;
1171 #if defined(__arm__)
1172 emachine1 = emachine2 = EM_ARM;
1173 #elif defined(__mips__)
1174 emachine1 = emachine2 = EM_MIPS;
1175 #elif defined(__powerpc__)
1176 emachine1 = emachine2 = EM_PPC;
1177 #elif defined(__sparc)
1178 emachine1 = EM_SPARC;
1179 emachine2 = EM_SPARC32PLUS;
1180 #elif defined(__i386) || defined(__amd64) || defined(__ia64__)
1181 emachine1 = emachine2 = EM_386;
1183 symsize = sizeof (Elf32_Sym);
1186 if (ehdr.e_ident[EI_CLASS] != eclass) {
1187 return (dt_link_error(dtp, elf, fd, bufs,
1188 "incorrect ELF class for object file: %s", obj));
1191 if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) {
1192 return (dt_link_error(dtp, elf, fd, bufs,
1193 "incorrect ELF machine type for object file: %s", obj));
1197 * We use this token as a relatively unique handle for this file on the
1198 * system in order to disambiguate potential conflicts between files of
1199 * the same name which contain identially named local symbols.
1201 if ((objkey = ftok(obj, 0)) == (key_t)-1) {
1202 return (dt_link_error(dtp, elf, fd, bufs,
1203 "failed to generate unique key for object file: %s", obj));
1207 while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) {
1208 if (gelf_getshdr(scn_rel, &shdr_rel) == NULL)
1212 * Skip any non-relocation sections.
1214 if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL)
1217 if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL)
1221 * Grab the section, section header and section data for the
1222 * symbol table that this relocation section references.
1224 if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL ||
1225 gelf_getshdr(scn_sym, &shdr_sym) == NULL ||
1226 (data_sym = elf_getdata(scn_sym, NULL)) == NULL)
1230 * Ditto for that symbol table's string table.
1232 if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL ||
1233 gelf_getshdr(scn_str, &shdr_str) == NULL ||
1234 (data_str = elf_getdata(scn_str, NULL)) == NULL)
1238 * Grab the section, section header and section data for the
1239 * target section for the relocations. For the relocations
1240 * we're looking for -- this will typically be the text of the
1243 if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL ||
1244 gelf_getshdr(scn_tgt, &shdr_tgt) == NULL ||
1245 (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL)
1249 * We're looking for relocations to symbols matching this form:
1251 * __dtrace[enabled]_<prov>___<probe>
1253 * For the generated object, we need to record the location
1254 * identified by the relocation, and create a new relocation
1255 * in the generated object that will be resolved at link time
1256 * to the location of the function in which the probe is
1257 * embedded. In the target object, we change the matched symbol
1258 * so that it will be ignored at link time, and we modify the
1259 * target (text) section to replace the call instruction with
1262 * If the function containing the probe is locally scoped
1263 * (static), we create an alias used by the relocation in the
1264 * generated object. The alias, a new symbol, will be global
1265 * (so that the relocation from the generated object can be
1266 * resolved), and hidden (so that it is converted to a local
1267 * symbol at link time). Such aliases have this form:
1269 * $dtrace<key>.<function>
1271 * We take a first pass through all the relocations to
1272 * populate our string table and count the number of extra
1273 * symbols we'll require.
1275 strtab = dt_strtab_create(1);
1277 isym = data_sym->d_size / symsize;
1278 istr = data_str->d_size;
1280 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1282 if (shdr_rel.sh_type == SHT_RELA) {
1283 if (gelf_getrela(data_rel, i, &rela) == NULL)
1287 if (gelf_getrel(data_rel, i, &rel) == NULL)
1289 rela.r_offset = rel.r_offset;
1290 rela.r_info = rel.r_info;
1294 if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info),
1296 dt_strtab_destroy(strtab);
1300 s = (char *)data_str->d_buf + rsym.st_name;
1302 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1305 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1306 shdr_rel.sh_info, &fsym) != 0) {
1307 dt_strtab_destroy(strtab);
1311 if (GELF_ST_BIND(fsym.st_info) != STB_LOCAL)
1314 if (fsym.st_name > data_str->d_size) {
1315 dt_strtab_destroy(strtab);
1319 s = (char *)data_str->d_buf + fsym.st_name;
1322 * If this symbol isn't of type function, we've really
1323 * driven off the rails or the object file is corrupt.
1325 if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) {
1326 dt_strtab_destroy(strtab);
1327 return (dt_link_error(dtp, elf, fd, bufs,
1328 "expected %s to be of type function", s));
1331 len = snprintf(NULL, 0, dt_symfmt, dt_symprefix,
1333 if ((p = dt_alloc(dtp, len)) == NULL) {
1334 dt_strtab_destroy(strtab);
1337 (void) snprintf(p, len, dt_symfmt, dt_symprefix,
1340 if (dt_strtab_index(strtab, p) == -1) {
1342 (void) dt_strtab_insert(strtab, p);
1349 * If needed, allocate the additional space for the symbol
1350 * table and string table copying the old data into the new
1351 * buffers, and marking the buffers as dirty. We inject those
1352 * newly allocated buffers into the libelf data structures, but
1353 * are still responsible for freeing them once we're done with
1358 * The first byte of the string table is reserved for
1361 len = dt_strtab_size(strtab) - 1;
1364 assert(dt_strtab_index(strtab, "") == 0);
1366 dt_strtab_destroy(strtab);
1368 if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL)
1371 if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size +
1377 if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size +
1378 nsym * symsize)) == NULL) {
1379 dt_free(dtp, pair->dlp_str);
1384 pair->dlp_next = bufs;
1387 bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size);
1388 data_str->d_buf = pair->dlp_str;
1389 data_str->d_size += len;
1390 (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY);
1392 shdr_str.sh_size += len;
1393 (void) gelf_update_shdr(scn_str, &shdr_str);
1395 bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size);
1396 data_sym->d_buf = pair->dlp_sym;
1397 data_sym->d_size += nsym * symsize;
1398 (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY);
1400 shdr_sym.sh_size += nsym * symsize;
1401 (void) gelf_update_shdr(scn_sym, &shdr_sym);
1405 dt_strtab_destroy(strtab);
1409 * Now that the tables have been allocated, perform the
1410 * modifications described above.
1412 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) {
1414 if (shdr_rel.sh_type == SHT_RELA) {
1415 if (gelf_getrela(data_rel, i, &rela) == NULL)
1419 if (gelf_getrel(data_rel, i, &rel) == NULL)
1421 rela.r_offset = rel.r_offset;
1422 rela.r_info = rel.r_info;
1426 ndx = GELF_R_SYM(rela.r_info);
1428 if (gelf_getsym(data_sym, ndx, &rsym) == NULL ||
1429 rsym.st_name > data_str->d_size)
1432 s = (char *)data_str->d_buf + rsym.st_name;
1434 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0)
1437 s += sizeof (dt_prefix) - 1;
1440 * Check to see if this is an 'is-enabled' check as
1441 * opposed to a normal probe.
1443 if (strncmp(s, dt_enabled,
1444 sizeof (dt_enabled) - 1) == 0) {
1445 s += sizeof (dt_enabled) - 1;
1448 dt_dprintf("is-enabled probe\n");
1451 dt_dprintf("normal probe\n");
1457 if ((p = strstr(s, "___")) == NULL ||
1458 p - s >= sizeof (pname))
1461 bcopy(s, pname, p - s);
1462 pname[p - s] = '\0';
1464 p = strhyphenate(p + 3); /* strlen("___") */
1466 if (dt_symtab_lookup(data_sym, isym, rela.r_offset,
1467 shdr_rel.sh_info, &fsym) != 0)
1470 if (fsym.st_name > data_str->d_size)
1473 assert(GELF_ST_TYPE(fsym.st_info) == STT_FUNC);
1476 * If a NULL relocation name is passed to
1477 * dt_probe_define(), the function name is used for the
1478 * relocation. The relocation needs to use a mangled
1479 * name if the symbol is locally scoped; the function
1480 * name may need to change if we've found the global
1481 * alias for the locally scoped symbol (we prefer
1482 * global symbols to locals in dt_symtab_lookup()).
1484 s = (char *)data_str->d_buf + fsym.st_name;
1487 if (GELF_ST_BIND(fsym.st_info) == STB_LOCAL) {
1489 dsym.st_name = istr;
1490 dsym.st_info = GELF_ST_INFO(STB_GLOBAL,
1493 ELF64_ST_VISIBILITY(STV_ELIMINATE);
1494 (void) gelf_update_sym(data_sym, isym, &dsym);
1496 r = (char *)data_str->d_buf + istr;
1497 istr += 1 + sprintf(r, dt_symfmt,
1498 dt_symprefix, objkey, s);
1500 assert(isym <= nsym);
1502 } else if (strncmp(s, dt_symprefix,
1503 strlen(dt_symprefix)) == 0) {
1505 if ((s = strchr(s, '.')) == NULL)
1510 if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) {
1511 return (dt_link_error(dtp, elf, fd, bufs,
1512 "no such provider %s", pname));
1515 if ((prp = dt_probe_lookup(pvp, p)) == NULL) {
1516 return (dt_link_error(dtp, elf, fd, bufs,
1517 "no such probe %s", p));
1520 assert(fsym.st_value <= rela.r_offset);
1522 off = rela.r_offset - fsym.st_value;
1523 if (dt_modtext(dtp, data_tgt->d_buf, eprobe,
1527 if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) {
1528 return (dt_link_error(dtp, elf, fd, bufs,
1529 "failed to allocate space for probe"));
1533 * Our linker doesn't understand the SUNW_IGNORE ndx and
1534 * will try to use this relocation when we build the
1535 * final executable. Since we are done processing this
1536 * relocation, mark it as inexistant and let libelf
1537 * remove it from the file.
1538 * If this wasn't done, we would have garbage added to
1539 * the executable file as the symbol is going to be
1540 * change from UND to ABS.
1545 (void) gelf_update_rela(data_rel, i, &rela);
1549 (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY);
1552 * This symbol may already have been marked to
1553 * be ignored by another relocation referencing
1554 * the same symbol or if this object file has
1555 * already been processed by an earlier link
1559 #define SHN_SUNW_IGNORE SHN_ABS
1561 if (rsym.st_shndx != SHN_SUNW_IGNORE) {
1562 rsym.st_shndx = SHN_SUNW_IGNORE;
1563 (void) gelf_update_sym(data_sym, ndx, &rsym);
1568 if (mod && elf_update(elf, ELF_C_WRITE) == -1)
1571 (void) elf_end(elf);
1577 while ((pair = bufs) != NULL) {
1578 bufs = pair->dlp_next;
1579 dt_free(dtp, pair->dlp_str);
1580 dt_free(dtp, pair->dlp_sym);
1587 return (dt_link_error(dtp, elf, fd, bufs,
1588 "an error was encountered while processing %s", obj));
1592 dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
1593 const char *file, int objc, char *const objv[])
1596 char tfile[PATH_MAX];
1611 char drti[PATH_MAX];
1613 int fd, status, i, cur;
1616 int eprobes = 0, ret = 0;
1619 if (access(file, R_OK) == 0) {
1620 fprintf(stderr, "dtrace: target object (%s) already exists. "
1621 "Please remove the target\ndtrace: object and rebuild all "
1622 "the source objects if you wish to run the DTrace\n"
1623 "dtrace: linking process again\n", file);
1625 * Several build infrastructures run DTrace twice (e.g.
1626 * postgres) and we don't want the build to fail. Return
1627 * 0 here since this isn't really a fatal error.
1631 /* XXX Should get a temp file name here. */
1632 snprintf(tfile, sizeof(tfile), "%s.tmp", file);
1636 * A NULL program indicates a special use in which we just link
1637 * together a bunch of object files specified in objv and then
1638 * unlink(2) those object files.
1641 const char *fmt = "%s -o %s -r";
1643 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1;
1645 for (i = 0; i < objc; i++)
1646 len += strlen(objv[i]) + 1;
1650 cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file);
1652 for (i = 0; i < objc; i++)
1653 cur += snprintf(cmd + cur, len - cur, " %s", objv[i]);
1655 if ((status = system(cmd)) == -1) {
1656 return (dt_link_error(dtp, NULL, -1, NULL,
1657 "failed to run %s: %s", dtp->dt_ld_path,
1661 if (WIFSIGNALED(status)) {
1662 return (dt_link_error(dtp, NULL, -1, NULL,
1663 "failed to link %s: %s failed due to signal %d",
1664 file, dtp->dt_ld_path, WTERMSIG(status)));
1667 if (WEXITSTATUS(status) != 0) {
1668 return (dt_link_error(dtp, NULL, -1, NULL,
1669 "failed to link %s: %s exited with status %d\n",
1670 file, dtp->dt_ld_path, WEXITSTATUS(status)));
1673 for (i = 0; i < objc; i++) {
1674 if (strcmp(objv[i], file) != 0)
1675 (void) unlink(objv[i]);
1681 for (i = 0; i < objc; i++) {
1682 if (process_obj(dtp, objv[i], &eprobes) != 0)
1683 return (-1); /* errno is set for us */
1687 * If there are is-enabled probes then we need to force use of DOF
1690 if (eprobes && pgp->dp_dofversion < DOF_VERSION_2)
1691 pgp->dp_dofversion = DOF_VERSION_2;
1693 if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL)
1694 return (-1); /* errno is set for us */
1698 * Create a temporary file and then unlink it if we're going to
1699 * combine it with drti.o later. We can still refer to it in child
1700 * processes as /dev/fd/<fd>.
1702 if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
1703 return (dt_link_error(dtp, NULL, -1, NULL,
1704 "failed to open %s: %s", file, strerror(errno)));
1707 if ((fd = open(tfile, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1)
1708 return (dt_link_error(dtp, NULL, -1, NULL,
1709 "failed to open %s: %s", tfile, strerror(errno)));
1713 * If -xlinktype=DOF has been selected, just write out the DOF.
1714 * Otherwise proceed to the default of generating and linking ELF.
1716 switch (dtp->dt_linktype) {
1718 if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz)
1721 if (close(fd) != 0 && ret == 0)
1725 return (dt_link_error(dtp, NULL, -1, NULL,
1726 "failed to write %s: %s", file, strerror(ret)));
1732 break; /* fall through to the rest of dtrace_program_link() */
1735 return (dt_link_error(dtp, NULL, -1, NULL,
1736 "invalid link type %u\n", dtp->dt_linktype));
1741 if (!dtp->dt_lazyload)
1742 (void) unlink(file);
1746 if (dtp->dt_oflags & DTRACE_O_LP64)
1747 status = dump_elf64(dtp, dof, fd);
1749 status = dump_elf32(dtp, dof, fd);
1751 if (status != 0 || lseek(fd, 0, SEEK_SET) != 0) {
1753 /* We don't write the ELF header, just the DOF section */
1754 if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz) {
1756 return (dt_link_error(dtp, NULL, -1, NULL,
1757 "failed to write %s: %s", file, strerror(errno)));
1760 if (!dtp->dt_lazyload) {
1762 const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s";
1764 if (dtp->dt_oflags & DTRACE_O_LP64) {
1765 (void) snprintf(drti, sizeof (drti),
1766 "%s/64/drti.o", _dtrace_libdir);
1768 (void) snprintf(drti, sizeof (drti),
1769 "%s/drti.o", _dtrace_libdir);
1772 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd,
1777 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti);
1779 const char *fmt = "%s -o %s -r %s";
1781 #if defined(__amd64__)
1783 * Arches which default to 64-bit need to explicitly use
1784 * the 32-bit library path.
1786 int use_32 = !(dtp->dt_oflags & DTRACE_O_LP64);
1789 * Arches which are 32-bit only just use the normal
1795 (void) snprintf(drti, sizeof (drti), "/usr/lib%s/dtrace/drti.o",
1798 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, tfile,
1806 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file,
1809 if ((status = system(cmd)) == -1) {
1810 ret = dt_link_error(dtp, NULL, -1, NULL,
1811 "failed to run %s: %s", dtp->dt_ld_path,
1816 if (WIFSIGNALED(status)) {
1817 ret = dt_link_error(dtp, NULL, -1, NULL,
1818 "failed to link %s: %s failed due to signal %d",
1819 file, dtp->dt_ld_path, WTERMSIG(status));
1823 if (WEXITSTATUS(status) != 0) {
1824 ret = dt_link_error(dtp, NULL, -1, NULL,
1825 "failed to link %s: %s exited with status %d\n",
1826 file, dtp->dt_ld_path, WEXITSTATUS(status));
1830 #define BROKEN_LIBELF
1832 * FreeBSD's ld(1) is not instructed to interpret and add
1833 * correctly the SUNW_dof section present in tfile.
1834 * We use libelf to add this section manually and hope the next
1835 * ld invocation won't remove it.
1837 elf_version(EV_CURRENT);
1838 if ((efd = open(file, O_RDWR, 0)) < 0) {
1839 ret = dt_link_error(dtp, NULL, -1, NULL,
1840 "failed to open file %s: %s",
1841 file, strerror(errno));
1844 if ((e = elf_begin(efd, ELF_C_RDWR, NULL)) == NULL) {
1846 ret = dt_link_error(dtp, NULL, -1, NULL,
1847 "failed to open elf file: %s",
1848 elf_errmsg(elf_errno()));
1852 * Add the string '.SUWN_dof' to the shstrtab section.
1854 #ifdef BROKEN_LIBELF
1855 elf_flagelf(e, ELF_C_SET, ELF_F_LAYOUT);
1857 elf_getshdrstrndx(e, &stridx);
1858 scn = elf_getscn(e, stridx);
1859 gelf_getshdr(scn, &shdr);
1860 data = elf_newdata(scn);
1861 data->d_off = shdr.sh_size;
1862 data->d_buf = ".SUNW_dof";
1864 data->d_type = ELF_T_BYTE;
1866 shdr.sh_size += data->d_size;
1867 gelf_update_shdr(scn, &shdr);
1868 #ifdef BROKEN_LIBELF
1869 off = shdr.sh_offset;
1870 rc = shdr.sh_offset + shdr.sh_size;
1871 gelf_getehdr(e, &ehdr);
1872 if (ehdr.e_shoff > off) {
1873 off = ehdr.e_shoff + ehdr.e_shnum * ehdr.e_shentsize;
1874 rc = roundup(rc, 8);
1876 gelf_update_ehdr(e, &ehdr);
1877 rc += ehdr.e_shnum * ehdr.e_shentsize;
1882 while ((scn = elf_nextscn(e, scn)) != NULL) {
1883 gelf_getshdr(scn, &shdr);
1884 if (shdr.sh_type == SHT_NOBITS ||
1885 shdr.sh_offset < off)
1887 /* Find the immediately adjcent section. */
1889 shdr.sh_offset < shdr0.sh_offset) {
1891 gelf_getshdr(scn0, &shdr0);
1896 /* Load section data to work around another bug */
1897 elf_getdata(scn0, NULL);
1898 /* Update section header, assure section alignment */
1899 off = shdr0.sh_offset + shdr0.sh_size;
1900 rc = roundup(rc, shdr0.sh_addralign);
1901 shdr0.sh_offset = rc;
1902 gelf_update_shdr(scn0, &shdr0);
1903 rc += shdr0.sh_size;
1905 if (elf_update(e, ELF_C_WRITE) < 0) {
1906 ret = dt_link_error(dtp, NULL, -1, NULL,
1907 "failed to add append the shstrtab section: %s",
1908 elf_errmsg(elf_errno()));
1914 e = elf_begin(efd, ELF_C_RDWR, NULL);
1917 * Construct the .SUNW_dof section.
1919 scn = elf_newscn(e);
1920 data = elf_newdata(scn);
1921 buf = mmap(NULL, dof->dofh_filesz, PROT_READ, MAP_SHARED,
1923 if (buf == MAP_FAILED) {
1924 ret = dt_link_error(dtp, NULL, -1, NULL,
1925 "failed to mmap buffer %s", strerror(errno));
1932 data->d_size = dof->dofh_filesz;
1933 data->d_version = EV_CURRENT;
1934 gelf_getshdr(scn, &shdr);
1936 shdr.sh_flags = SHF_ALLOC;
1938 * Actually this should be SHT_SUNW_dof, but FreeBSD's ld(1)
1939 * will remove this 'unknown' section when we try to create an
1940 * executable using the object we are modifying, so we stop
1941 * playing by the rules and use SHT_PROGBITS.
1942 * Also, note that our drti has modifications to handle this.
1944 shdr.sh_type = SHT_PROGBITS;
1945 shdr.sh_addralign = 4;
1946 gelf_update_shdr(scn, &shdr);
1947 if (elf_update(e, ELF_C_WRITE) < 0) {
1948 ret = dt_link_error(dtp, NULL, -1, NULL,
1949 "failed to add the SUNW_dof section: %s",
1950 elf_errmsg(elf_errno()));
1951 munmap(buf, dof->dofh_filesz);
1956 munmap(buf, dof->dofh_filesz);
1960 (void) close(fd); /* release temporary file */
1966 dtrace_dof_destroy(dtp, dof);