1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #define ELF_TARGET_ALL 30 #ifdef __NetBSD__ 31 #ifdef __x86_64__ 32 #include <i386/elf_machdep.h> 33 #undef ELF32_MACHDEP_ID_CASES 34 #undef ELF64_MACHDEP_ID_CASES 35 #undef ELF64_MACHDEP_ENDIANNESS 36 #undef KERN_ELFSIZE 37 #undef ARCH_ELFSIZE 38 #undef R_TYPE 39 #endif 40 #endif 41 #include <elf.h> 42 43 #include <sys/types.h> 44 #ifdef illumos 45 #include <sys/sysmacros.h> 46 #else 47 #define P2ROUNDUP(x, align) (-(-(x) & -(align))) 48 #endif 49 50 #include <unistd.h> 51 #include <strings.h> 52 #ifdef illumos 53 #include <alloca.h> 54 #endif 55 #include <limits.h> 56 #include <stddef.h> 57 #include <stdlib.h> 58 #include <stdio.h> 59 #include <fcntl.h> 60 #include <errno.h> 61 #ifdef illumos 62 #include <wait.h> 63 #else 64 #include <sys/wait.h> 65 #include <libelf.h> 66 #include <gelf.h> 67 #include <sys/mman.h> 68 #endif 69 #include <assert.h> 70 #include <sys/ipc.h> 71 72 #include <dt_impl.h> 73 #include <dt_provider.h> 74 #include <dt_program.h> 75 #include <dt_string.h> 76 77 #define ESHDR_NULL 0 78 #define ESHDR_SHSTRTAB 1 79 #define ESHDR_DOF 2 80 #define ESHDR_STRTAB 3 81 #define ESHDR_SYMTAB 4 82 #define ESHDR_REL 5 83 #define ESHDR_NUM 6 84 85 #define PWRITE_SCN(index, data) \ 86 (lseek64(fd, (off64_t)elf_file.shdr[(index)].sh_offset, SEEK_SET) != \ 87 (off64_t)elf_file.shdr[(index)].sh_offset || \ 88 dt_write(dtp, fd, (data), elf_file.shdr[(index)].sh_size) != \ 89 elf_file.shdr[(index)].sh_size) 90 91 static const char DTRACE_SHSTRTAB32[] = "\0" 92 ".shstrtab\0" /* 1 */ 93 ".SUNW_dof\0" /* 11 */ 94 ".strtab\0" /* 21 */ 95 ".symtab\0" /* 29 */ 96 #ifdef __sparc 97 ".rela.SUNW_dof"; /* 37 */ 98 #else 99 ".rel.SUNW_dof"; /* 37 */ 100 #endif 101 102 static const char DTRACE_SHSTRTAB64[] = "\0" 103 ".shstrtab\0" /* 1 */ 104 ".SUNW_dof\0" /* 11 */ 105 ".strtab\0" /* 21 */ 106 ".symtab\0" /* 29 */ 107 ".rela.SUNW_dof"; /* 37 */ 108 109 static const char DOFSTR[] = "__SUNW_dof"; 110 static const char DOFLAZYSTR[] = "___SUNW_dof"; 111 112 typedef struct dt_link_pair { 113 struct dt_link_pair *dlp_next; /* next pair in linked list */ 114 void *dlp_str; /* buffer for string table */ 115 void *dlp_sym; /* buffer for symbol table */ 116 } dt_link_pair_t; 117 118 typedef struct dof_elf32 { 119 uint32_t de_nrel; /* relocation count */ 120 #ifdef __sparc 121 Elf32_Rela *de_rel; /* array of relocations for sparc */ 122 #else 123 Elf32_Rel *de_rel; /* array of relocations for x86 */ 124 #endif 125 uint32_t de_nsym; /* symbol count */ 126 Elf32_Sym *de_sym; /* array of symbols */ 127 uint32_t de_strlen; /* size of of string table */ 128 char *de_strtab; /* string table */ 129 uint32_t de_global; /* index of the first global symbol */ 130 } dof_elf32_t; 131 132 static int 133 prepare_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf32_t *dep) 134 { 135 dof_sec_t *dofs, *s; 136 dof_relohdr_t *dofrh; 137 dof_relodesc_t *dofr; 138 char *strtab; 139 int i, j, nrel; 140 size_t strtabsz = 1; 141 uint32_t count = 0; 142 size_t base; 143 Elf32_Sym *sym; 144 #ifdef __sparc 145 Elf32_Rela *rel; 146 #else 147 Elf32_Rel *rel; 148 #endif 149 150 /*LINTED*/ 151 dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff); 152 153 /* 154 * First compute the size of the string table and the number of 155 * relocations present in the DOF. 156 */ 157 for (i = 0; i < dof->dofh_secnum; i++) { 158 if (dofs[i].dofs_type != DOF_SECT_URELHDR) 159 continue; 160 161 /*LINTED*/ 162 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 163 164 s = &dofs[dofrh->dofr_strtab]; 165 strtab = (char *)dof + s->dofs_offset; 166 assert(strtab[0] == '\0'); 167 strtabsz += s->dofs_size - 1; 168 169 s = &dofs[dofrh->dofr_relsec]; 170 /*LINTED*/ 171 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 172 count += s->dofs_size / s->dofs_entsize; 173 } 174 175 dep->de_strlen = strtabsz; 176 dep->de_nrel = count; 177 dep->de_nsym = count + 1; /* the first symbol is always null */ 178 179 if (dtp->dt_lazyload) { 180 dep->de_strlen += sizeof (DOFLAZYSTR); 181 dep->de_nsym++; 182 } else { 183 dep->de_strlen += sizeof (DOFSTR); 184 dep->de_nsym++; 185 } 186 187 if ((dep->de_rel = calloc(dep->de_nrel, 188 sizeof (dep->de_rel[0]))) == NULL) { 189 return (dt_set_errno(dtp, EDT_NOMEM)); 190 } 191 192 if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf32_Sym))) == NULL) { 193 free(dep->de_rel); 194 return (dt_set_errno(dtp, EDT_NOMEM)); 195 } 196 197 if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) { 198 free(dep->de_rel); 199 free(dep->de_sym); 200 return (dt_set_errno(dtp, EDT_NOMEM)); 201 } 202 203 count = 0; 204 strtabsz = 1; 205 dep->de_strtab[0] = '\0'; 206 rel = dep->de_rel; 207 sym = dep->de_sym; 208 dep->de_global = 1; 209 210 /* 211 * The first symbol table entry must be zeroed and is always ignored. 212 */ 213 bzero(sym, sizeof (Elf32_Sym)); 214 sym++; 215 216 /* 217 * Take a second pass through the DOF sections filling in the 218 * memory we allocated. 219 */ 220 for (i = 0; i < dof->dofh_secnum; i++) { 221 if (dofs[i].dofs_type != DOF_SECT_URELHDR) 222 continue; 223 224 /*LINTED*/ 225 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 226 227 s = &dofs[dofrh->dofr_strtab]; 228 strtab = (char *)dof + s->dofs_offset; 229 bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size); 230 base = strtabsz; 231 strtabsz += s->dofs_size - 1; 232 233 s = &dofs[dofrh->dofr_relsec]; 234 /*LINTED*/ 235 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 236 nrel = s->dofs_size / s->dofs_entsize; 237 238 s = &dofs[dofrh->dofr_tgtsec]; 239 240 for (j = 0; j < nrel; j++) { 241 #if defined(__aarch64__) 242 /* XXX */ 243 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 244 #elif defined(__arm__) 245 /* XXX */ 246 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 247 #elif defined(__i386) || defined(__amd64) 248 rel->r_offset = s->dofs_offset + 249 dofr[j].dofr_offset; 250 rel->r_info = ELF32_R_INFO(count + dep->de_global, 251 R_386_PC32); 252 #elif defined(__mips__) 253 /* XXX */ 254 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 255 #elif defined(__powerpc__) 256 /* 257 * Add 4 bytes to hit the low half of this 64-bit 258 * big-endian address. 259 */ 260 rel->r_offset = s->dofs_offset + 261 dofr[j].dofr_offset + 4; 262 rel->r_info = ELF32_R_INFO(count + dep->de_global, 263 R_PPC_REL32); 264 #elif defined(__sparc) 265 /* 266 * Add 4 bytes to hit the low half of this 64-bit 267 * big-endian address. 268 */ 269 rel->r_offset = s->dofs_offset + 270 dofr[j].dofr_offset + 4; 271 rel->r_info = ELF32_R_INFO(count + dep->de_global, 272 R_SPARC_32); 273 #elif defined(__riscv__) 274 /* XXX */ 275 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 276 #else 277 #error unknown ISA 278 #endif 279 280 sym->st_name = base + dofr[j].dofr_name - 1; 281 sym->st_value = 0; 282 sym->st_size = 0; 283 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC); 284 sym->st_other = 0; 285 sym->st_shndx = SHN_UNDEF; 286 287 rel++; 288 sym++; 289 count++; 290 } 291 } 292 293 /* 294 * Add a symbol for the DOF itself. We use a different symbol for 295 * lazily and actively loaded DOF to make them easy to distinguish. 296 */ 297 sym->st_name = strtabsz; 298 sym->st_value = 0; 299 sym->st_size = dof->dofh_filesz; 300 sym->st_info = ELF32_ST_INFO(STB_GLOBAL, STT_OBJECT); 301 sym->st_other = ELF32_ST_VISIBILITY(STV_HIDDEN); 302 sym->st_shndx = ESHDR_DOF; 303 sym++; 304 305 if (dtp->dt_lazyload) { 306 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz, 307 sizeof (DOFLAZYSTR)); 308 strtabsz += sizeof (DOFLAZYSTR); 309 } else { 310 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR)); 311 strtabsz += sizeof (DOFSTR); 312 } 313 314 assert(count == dep->de_nrel); 315 assert(strtabsz == dep->de_strlen); 316 317 return (0); 318 } 319 320 321 typedef struct dof_elf64 { 322 uint32_t de_nrel; 323 Elf64_Rela *de_rel; 324 uint32_t de_nsym; 325 Elf64_Sym *de_sym; 326 327 uint32_t de_strlen; 328 char *de_strtab; 329 330 uint32_t de_global; 331 } dof_elf64_t; 332 333 static int 334 prepare_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, dof_elf64_t *dep) 335 { 336 dof_sec_t *dofs, *s; 337 dof_relohdr_t *dofrh; 338 dof_relodesc_t *dofr; 339 char *strtab; 340 int i, j, nrel; 341 size_t strtabsz = 1; 342 #ifdef illumos 343 uint32_t count = 0; 344 #else 345 uint64_t count = 0; 346 #endif 347 size_t base; 348 Elf64_Sym *sym; 349 Elf64_Rela *rel; 350 351 /*LINTED*/ 352 dofs = (dof_sec_t *)((char *)dof + dof->dofh_secoff); 353 354 /* 355 * First compute the size of the string table and the number of 356 * relocations present in the DOF. 357 */ 358 for (i = 0; i < dof->dofh_secnum; i++) { 359 if (dofs[i].dofs_type != DOF_SECT_URELHDR) 360 continue; 361 362 /*LINTED*/ 363 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 364 365 s = &dofs[dofrh->dofr_strtab]; 366 strtab = (char *)dof + s->dofs_offset; 367 assert(strtab[0] == '\0'); 368 strtabsz += s->dofs_size - 1; 369 370 s = &dofs[dofrh->dofr_relsec]; 371 /*LINTED*/ 372 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 373 count += s->dofs_size / s->dofs_entsize; 374 } 375 376 dep->de_strlen = strtabsz; 377 dep->de_nrel = count; 378 dep->de_nsym = count + 1; /* the first symbol is always null */ 379 380 if (dtp->dt_lazyload) { 381 dep->de_strlen += sizeof (DOFLAZYSTR); 382 dep->de_nsym++; 383 } else { 384 dep->de_strlen += sizeof (DOFSTR); 385 dep->de_nsym++; 386 } 387 388 if ((dep->de_rel = calloc(dep->de_nrel, 389 sizeof (dep->de_rel[0]))) == NULL) { 390 return (dt_set_errno(dtp, EDT_NOMEM)); 391 } 392 393 if ((dep->de_sym = calloc(dep->de_nsym, sizeof (Elf64_Sym))) == NULL) { 394 free(dep->de_rel); 395 return (dt_set_errno(dtp, EDT_NOMEM)); 396 } 397 398 if ((dep->de_strtab = calloc(dep->de_strlen, 1)) == NULL) { 399 free(dep->de_rel); 400 free(dep->de_sym); 401 return (dt_set_errno(dtp, EDT_NOMEM)); 402 } 403 404 count = 0; 405 strtabsz = 1; 406 dep->de_strtab[0] = '\0'; 407 rel = dep->de_rel; 408 sym = dep->de_sym; 409 dep->de_global = 1; 410 411 /* 412 * The first symbol table entry must be zeroed and is always ignored. 413 */ 414 bzero(sym, sizeof (Elf64_Sym)); 415 sym++; 416 417 /* 418 * Take a second pass through the DOF sections filling in the 419 * memory we allocated. 420 */ 421 for (i = 0; i < dof->dofh_secnum; i++) { 422 if (dofs[i].dofs_type != DOF_SECT_URELHDR) 423 continue; 424 425 /*LINTED*/ 426 dofrh = (dof_relohdr_t *)((char *)dof + dofs[i].dofs_offset); 427 428 s = &dofs[dofrh->dofr_strtab]; 429 strtab = (char *)dof + s->dofs_offset; 430 bcopy(strtab + 1, dep->de_strtab + strtabsz, s->dofs_size); 431 base = strtabsz; 432 strtabsz += s->dofs_size - 1; 433 434 s = &dofs[dofrh->dofr_relsec]; 435 /*LINTED*/ 436 dofr = (dof_relodesc_t *)((char *)dof + s->dofs_offset); 437 nrel = s->dofs_size / s->dofs_entsize; 438 439 s = &dofs[dofrh->dofr_tgtsec]; 440 441 for (j = 0; j < nrel; j++) { 442 #if defined(__aarch64__) 443 /* XXX */ 444 #elif defined(__arm__) 445 /* XXX */ 446 #elif defined(__mips__) 447 /* XXX */ 448 #elif defined(__powerpc__) 449 rel->r_offset = s->dofs_offset + 450 dofr[j].dofr_offset; 451 rel->r_info = ELF64_R_INFO(count + dep->de_global, 452 R_PPC64_REL64); 453 #elif defined(__riscv__) 454 /* XXX */ 455 #elif defined(__i386) || defined(__amd64) 456 #ifndef R_X86_64_PC64 457 #define R_X86_64_PC64 24 458 #endif 459 rel->r_offset = s->dofs_offset + 460 dofr[j].dofr_offset; 461 rel->r_info = ELF64_R_INFO(count + dep->de_global, 462 R_X86_64_PC64); 463 #else 464 #error unknown ISA 465 #endif 466 467 sym->st_name = base + dofr[j].dofr_name - 1; 468 sym->st_value = 0; 469 sym->st_size = 0; 470 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_FUNC); 471 sym->st_other = 0; 472 sym->st_shndx = SHN_UNDEF; 473 474 rel++; 475 sym++; 476 count++; 477 } 478 } 479 480 /* 481 * Add a symbol for the DOF itself. We use a different symbol for 482 * lazily and actively loaded DOF to make them easy to distinguish. 483 */ 484 sym->st_name = strtabsz; 485 sym->st_value = 0; 486 sym->st_size = dof->dofh_filesz; 487 sym->st_info = GELF_ST_INFO(STB_GLOBAL, STT_OBJECT); 488 sym->st_other = ELF64_ST_VISIBILITY(STV_HIDDEN); 489 sym->st_shndx = ESHDR_DOF; 490 sym++; 491 492 if (dtp->dt_lazyload) { 493 bcopy(DOFLAZYSTR, dep->de_strtab + strtabsz, 494 sizeof (DOFLAZYSTR)); 495 strtabsz += sizeof (DOFLAZYSTR); 496 } else { 497 bcopy(DOFSTR, dep->de_strtab + strtabsz, sizeof (DOFSTR)); 498 strtabsz += sizeof (DOFSTR); 499 } 500 501 assert(count == dep->de_nrel); 502 assert(strtabsz == dep->de_strlen); 503 504 return (0); 505 } 506 507 /* 508 * Write out an ELF32 file prologue consisting of a header, section headers, 509 * and a section header string table. The DOF data will follow this prologue 510 * and complete the contents of the given ELF file. 511 */ 512 static int 513 dump_elf32(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd) 514 { 515 struct { 516 Elf32_Ehdr ehdr; 517 Elf32_Shdr shdr[ESHDR_NUM]; 518 } elf_file; 519 520 Elf32_Shdr *shp; 521 Elf32_Off off; 522 dof_elf32_t de; 523 int ret = 0; 524 uint_t nshdr; 525 526 memset(&de, 0, sizeof(de)); // XXX: gcc 527 if (prepare_elf32(dtp, dof, &de) != 0) 528 return (-1); /* errno is set for us */ 529 530 /* 531 * If there are no relocations, we only need enough sections for 532 * the shstrtab and the DOF. 533 */ 534 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM; 535 536 bzero(&elf_file, sizeof (elf_file)); 537 538 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0; 539 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1; 540 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2; 541 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3; 542 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT; 543 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS32; 544 #if BYTE_ORDER == _BIG_ENDIAN 545 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB; 546 #else 547 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB; 548 #endif 549 #if defined(__FreeBSD__) || defined(__NetBSD__) 550 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 551 #endif 552 elf_file.ehdr.e_type = ET_REL; 553 #if defined(__arm__) 554 elf_file.ehdr.e_machine = EM_ARM; 555 #elif defined(__mips__) 556 elf_file.ehdr.e_machine = EM_MIPS; 557 #elif defined(__powerpc__) 558 elf_file.ehdr.e_machine = EM_PPC; 559 #elif defined(__sparc) 560 elf_file.ehdr.e_machine = EM_SPARC; 561 #elif defined(__i386) || defined(__amd64) 562 elf_file.ehdr.e_machine = EM_386; 563 #endif 564 elf_file.ehdr.e_version = EV_CURRENT; 565 elf_file.ehdr.e_shoff = sizeof (Elf32_Ehdr); 566 elf_file.ehdr.e_ehsize = sizeof (Elf32_Ehdr); 567 elf_file.ehdr.e_phentsize = sizeof (Elf32_Phdr); 568 elf_file.ehdr.e_shentsize = sizeof (Elf32_Shdr); 569 elf_file.ehdr.e_shnum = nshdr; 570 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB; 571 off = sizeof (elf_file) + nshdr * sizeof (Elf32_Shdr); 572 573 shp = &elf_file.shdr[ESHDR_SHSTRTAB]; 574 shp->sh_name = 1; /* DTRACE_SHSTRTAB32[1] = ".shstrtab" */ 575 shp->sh_type = SHT_STRTAB; 576 shp->sh_offset = off; 577 shp->sh_size = sizeof (DTRACE_SHSTRTAB32); 578 shp->sh_addralign = sizeof (char); 579 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 580 581 shp = &elf_file.shdr[ESHDR_DOF]; 582 shp->sh_name = 11; /* DTRACE_SHSTRTAB32[11] = ".SUNW_dof" */ 583 shp->sh_flags = SHF_ALLOC; 584 shp->sh_type = SHT_SUNW_dof; 585 shp->sh_offset = off; 586 shp->sh_size = dof->dofh_filesz; 587 shp->sh_addralign = 8; 588 off = shp->sh_offset + shp->sh_size; 589 590 shp = &elf_file.shdr[ESHDR_STRTAB]; 591 shp->sh_name = 21; /* DTRACE_SHSTRTAB32[21] = ".strtab" */ 592 shp->sh_flags = SHF_ALLOC; 593 shp->sh_type = SHT_STRTAB; 594 shp->sh_offset = off; 595 shp->sh_size = de.de_strlen; 596 shp->sh_addralign = sizeof (char); 597 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4); 598 599 shp = &elf_file.shdr[ESHDR_SYMTAB]; 600 shp->sh_name = 29; /* DTRACE_SHSTRTAB32[29] = ".symtab" */ 601 shp->sh_flags = SHF_ALLOC; 602 shp->sh_type = SHT_SYMTAB; 603 shp->sh_entsize = sizeof (Elf32_Sym); 604 shp->sh_link = ESHDR_STRTAB; 605 shp->sh_offset = off; 606 shp->sh_info = de.de_global; 607 shp->sh_size = de.de_nsym * sizeof (Elf32_Sym); 608 shp->sh_addralign = 4; 609 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 4); 610 611 if (de.de_nrel == 0) { 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_DOF, dof)) { 618 ret = dt_set_errno(dtp, errno); 619 } 620 } else { 621 shp = &elf_file.shdr[ESHDR_REL]; 622 shp->sh_name = 37; /* DTRACE_SHSTRTAB32[37] = ".rel.SUNW_dof" */ 623 shp->sh_flags = SHF_ALLOC; 624 #ifdef __sparc 625 shp->sh_type = SHT_RELA; 626 #else 627 shp->sh_type = SHT_REL; 628 #endif 629 shp->sh_entsize = sizeof (de.de_rel[0]); 630 shp->sh_link = ESHDR_SYMTAB; 631 shp->sh_info = ESHDR_DOF; 632 shp->sh_offset = off; 633 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]); 634 shp->sh_addralign = 4; 635 636 if (dt_write(dtp, fd, &elf_file, 637 sizeof (elf_file)) != sizeof (elf_file) || 638 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB32) || 639 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) || 640 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) || 641 PWRITE_SCN(ESHDR_REL, de.de_rel) || 642 PWRITE_SCN(ESHDR_DOF, dof)) { 643 ret = dt_set_errno(dtp, errno); 644 } 645 } 646 647 free(de.de_strtab); 648 free(de.de_sym); 649 free(de.de_rel); 650 651 return (ret); 652 } 653 654 /* 655 * Write out an ELF64 file prologue consisting of a header, section headers, 656 * and a section header string table. The DOF data will follow this prologue 657 * and complete the contents of the given ELF file. 658 */ 659 static int 660 dump_elf64(dtrace_hdl_t *dtp, const dof_hdr_t *dof, int fd) 661 { 662 struct { 663 Elf64_Ehdr ehdr; 664 Elf64_Shdr shdr[ESHDR_NUM]; 665 } elf_file; 666 667 Elf64_Shdr *shp; 668 Elf64_Off off; 669 dof_elf64_t de; 670 int ret = 0; 671 uint_t nshdr; 672 673 memset(&de, 0, sizeof(de)); // XXX: gcc 674 if (prepare_elf64(dtp, dof, &de) != 0) 675 return (-1); /* errno is set for us */ 676 677 /* 678 * If there are no relocations, we only need enough sections for 679 * the shstrtab and the DOF. 680 */ 681 nshdr = de.de_nrel == 0 ? ESHDR_SYMTAB + 1 : ESHDR_NUM; 682 683 bzero(&elf_file, sizeof (elf_file)); 684 685 elf_file.ehdr.e_ident[EI_MAG0] = ELFMAG0; 686 elf_file.ehdr.e_ident[EI_MAG1] = ELFMAG1; 687 elf_file.ehdr.e_ident[EI_MAG2] = ELFMAG2; 688 elf_file.ehdr.e_ident[EI_MAG3] = ELFMAG3; 689 elf_file.ehdr.e_ident[EI_VERSION] = EV_CURRENT; 690 elf_file.ehdr.e_ident[EI_CLASS] = ELFCLASS64; 691 #if BYTE_ORDER == _BIG_ENDIAN 692 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2MSB; 693 #else 694 elf_file.ehdr.e_ident[EI_DATA] = ELFDATA2LSB; 695 #endif 696 #if defined(__FreeBSD__) || defined(__NetBSD__) 697 elf_file.ehdr.e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 698 #endif 699 elf_file.ehdr.e_type = ET_REL; 700 #if defined(__arm__) 701 elf_file.ehdr.e_machine = EM_ARM; 702 #elif defined(__mips__) 703 elf_file.ehdr.e_machine = EM_MIPS; 704 #elif defined(__powerpc64__) 705 elf_file.ehdr.e_machine = EM_PPC64; 706 #elif defined(__sparc) 707 elf_file.ehdr.e_machine = EM_SPARCV9; 708 #elif defined(__i386) || defined(__amd64) 709 elf_file.ehdr.e_machine = EM_AMD64; 710 #endif 711 elf_file.ehdr.e_version = EV_CURRENT; 712 elf_file.ehdr.e_shoff = sizeof (Elf64_Ehdr); 713 elf_file.ehdr.e_ehsize = sizeof (Elf64_Ehdr); 714 elf_file.ehdr.e_phentsize = sizeof (Elf64_Phdr); 715 elf_file.ehdr.e_shentsize = sizeof (Elf64_Shdr); 716 elf_file.ehdr.e_shnum = nshdr; 717 elf_file.ehdr.e_shstrndx = ESHDR_SHSTRTAB; 718 off = sizeof (elf_file) + nshdr * sizeof (Elf64_Shdr); 719 720 shp = &elf_file.shdr[ESHDR_SHSTRTAB]; 721 shp->sh_name = 1; /* DTRACE_SHSTRTAB64[1] = ".shstrtab" */ 722 shp->sh_type = SHT_STRTAB; 723 shp->sh_offset = off; 724 shp->sh_size = sizeof (DTRACE_SHSTRTAB64); 725 shp->sh_addralign = sizeof (char); 726 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 727 728 shp = &elf_file.shdr[ESHDR_DOF]; 729 shp->sh_name = 11; /* DTRACE_SHSTRTAB64[11] = ".SUNW_dof" */ 730 shp->sh_flags = SHF_ALLOC; 731 shp->sh_type = SHT_SUNW_dof; 732 shp->sh_offset = off; 733 shp->sh_size = dof->dofh_filesz; 734 shp->sh_addralign = 8; 735 off = shp->sh_offset + shp->sh_size; 736 737 shp = &elf_file.shdr[ESHDR_STRTAB]; 738 shp->sh_name = 21; /* DTRACE_SHSTRTAB64[21] = ".strtab" */ 739 shp->sh_flags = SHF_ALLOC; 740 shp->sh_type = SHT_STRTAB; 741 shp->sh_offset = off; 742 shp->sh_size = de.de_strlen; 743 shp->sh_addralign = sizeof (char); 744 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 745 746 shp = &elf_file.shdr[ESHDR_SYMTAB]; 747 shp->sh_name = 29; /* DTRACE_SHSTRTAB64[29] = ".symtab" */ 748 shp->sh_flags = SHF_ALLOC; 749 shp->sh_type = SHT_SYMTAB; 750 shp->sh_entsize = sizeof (Elf64_Sym); 751 shp->sh_link = ESHDR_STRTAB; 752 shp->sh_offset = off; 753 shp->sh_info = de.de_global; 754 shp->sh_size = de.de_nsym * sizeof (Elf64_Sym); 755 shp->sh_addralign = 8; 756 off = P2ROUNDUP(shp->sh_offset + shp->sh_size, 8); 757 758 if (de.de_nrel == 0) { 759 if (dt_write(dtp, fd, &elf_file, 760 sizeof (elf_file)) != sizeof (elf_file) || 761 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) || 762 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) || 763 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) || 764 PWRITE_SCN(ESHDR_DOF, dof)) { 765 ret = dt_set_errno(dtp, errno); 766 } 767 } else { 768 shp = &elf_file.shdr[ESHDR_REL]; 769 shp->sh_name = 37; /* DTRACE_SHSTRTAB64[37] = ".rel.SUNW_dof" */ 770 shp->sh_flags = SHF_ALLOC; 771 shp->sh_type = SHT_RELA; 772 shp->sh_entsize = sizeof (de.de_rel[0]); 773 shp->sh_link = ESHDR_SYMTAB; 774 shp->sh_info = ESHDR_DOF; 775 shp->sh_offset = off; 776 shp->sh_size = de.de_nrel * sizeof (de.de_rel[0]); 777 shp->sh_addralign = 8; 778 779 if (dt_write(dtp, fd, &elf_file, 780 sizeof (elf_file)) != sizeof (elf_file) || 781 PWRITE_SCN(ESHDR_SHSTRTAB, DTRACE_SHSTRTAB64) || 782 PWRITE_SCN(ESHDR_STRTAB, de.de_strtab) || 783 PWRITE_SCN(ESHDR_SYMTAB, de.de_sym) || 784 PWRITE_SCN(ESHDR_REL, de.de_rel) || 785 PWRITE_SCN(ESHDR_DOF, dof)) { 786 ret = dt_set_errno(dtp, errno); 787 } 788 } 789 790 free(de.de_strtab); 791 free(de.de_sym); 792 free(de.de_rel); 793 794 return (ret); 795 } 796 797 static int 798 dt_symtab_lookup(Elf_Data *data_sym, int start, int end, uintptr_t addr, 799 uint_t shn, GElf_Sym *sym, int uses_funcdesc, Elf *elf) 800 { 801 Elf64_Addr symval; 802 Elf_Scn *opd_scn; 803 Elf_Data *opd_desc; 804 int i; 805 806 for (i = start; i < end && gelf_getsym(data_sym, i, sym) != NULL; i++) { 807 if (GELF_ST_TYPE(sym->st_info) == STT_FUNC) { 808 symval = sym->st_value; 809 if (uses_funcdesc) { 810 opd_scn = elf_getscn(elf, sym->st_shndx); 811 opd_desc = elf_rawdata(opd_scn, NULL); 812 symval = 813 *(uint64_t*)((char *)opd_desc->d_buf + symval); 814 } 815 if ((uses_funcdesc || shn == sym->st_shndx) && 816 symval <= addr && addr < symval + sym->st_size) 817 return (0); 818 } 819 } 820 821 return (-1); 822 } 823 824 #if defined(__aarch64__) 825 /* XXX */ 826 static int 827 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela, 828 uint32_t *off) 829 { 830 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 831 return (0); 832 } 833 #elif defined(__arm__) 834 /* XXX */ 835 static int 836 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela, 837 uint32_t *off) 838 { 839 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 840 return (0); 841 } 842 #elif defined(__mips__) 843 /* XXX */ 844 static int 845 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela, 846 uint32_t *off) 847 { 848 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 849 return (0); 850 } 851 #elif defined(__powerpc__) 852 /* The sentinel is 'xor r3,r3,r3'. */ 853 #define DT_OP_XOR_R3 0x7c631a78 854 855 #define DT_OP_NOP 0x60000000 856 #define DT_OP_BLR 0x4e800020 857 858 /* This captures all forms of branching to address. */ 859 #define DT_IS_BRANCH(inst) ((inst & 0xfc000000) == 0x48000000) 860 #define DT_IS_BL(inst) (DT_IS_BRANCH(inst) && (inst & 0x01)) 861 862 /* XXX */ 863 static int 864 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela, 865 uint32_t *off) 866 { 867 uint32_t *ip; 868 869 if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0) 870 return (-1); 871 872 /*LINTED*/ 873 ip = (uint32_t *)(p + rela->r_offset); 874 875 /* 876 * We only know about some specific relocation types. 877 */ 878 if (GELF_R_TYPE(rela->r_info) != R_PPC_REL24 && 879 GELF_R_TYPE(rela->r_info) != R_PPC_PLTREL24) 880 return (-1); 881 882 /* 883 * We may have already processed this object file in an earlier linker 884 * invocation. Check to see if the present instruction sequence matches 885 * the one we would install below. 886 */ 887 if (isenabled) { 888 if (ip[0] == DT_OP_XOR_R3) { 889 (*off) += sizeof (ip[0]); 890 return (0); 891 } 892 } else { 893 if (ip[0] == DT_OP_NOP) { 894 (*off) += sizeof (ip[0]); 895 return (0); 896 } 897 } 898 899 /* 900 * We only expect branch to address instructions. 901 */ 902 if (!DT_IS_BRANCH(ip[0])) { 903 dt_dprintf("found %x instead of a branch instruction at %llx\n", 904 ip[0], (u_longlong_t)rela->r_offset); 905 return (-1); 906 } 907 908 if (isenabled) { 909 /* 910 * It would necessarily indicate incorrect usage if an is- 911 * enabled probe were tail-called so flag that as an error. 912 * It's also potentially (very) tricky to handle gracefully, 913 * but could be done if this were a desired use scenario. 914 */ 915 if (!DT_IS_BL(ip[0])) { 916 dt_dprintf("tail call to is-enabled probe at %llx\n", 917 (u_longlong_t)rela->r_offset); 918 return (-1); 919 } 920 921 ip[0] = DT_OP_XOR_R3; 922 (*off) += sizeof (ip[0]); 923 } else { 924 if (DT_IS_BL(ip[0])) 925 ip[0] = DT_OP_NOP; 926 else 927 ip[0] = DT_OP_BLR; 928 } 929 930 return (0); 931 } 932 #elif defined(__riscv__) 933 /* XXX */ 934 static int 935 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela, 936 uint32_t *off) 937 { 938 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__); 939 return (0); 940 } 941 #elif defined(__sparc) 942 943 #define DT_OP_RET 0x81c7e008 944 #define DT_OP_NOP 0x01000000 945 #define DT_OP_CALL 0x40000000 946 #define DT_OP_CLR_O0 0x90102000 947 948 #define DT_IS_MOV_O7(inst) (((inst) & 0xffffe000) == 0x9e100000) 949 #define DT_IS_RESTORE(inst) (((inst) & 0xc1f80000) == 0x81e80000) 950 #define DT_IS_RETL(inst) (((inst) & 0xfff83fff) == 0x81c02008) 951 952 #define DT_RS2(inst) ((inst) & 0x1f) 953 #define DT_MAKE_RETL(reg) (0x81c02008 | ((reg) << 14)) 954 955 /*ARGSUSED*/ 956 static int 957 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela, 958 uint32_t *off) 959 { 960 uint32_t *ip; 961 962 if ((rela->r_offset & (sizeof (uint32_t) - 1)) != 0) 963 return (-1); 964 965 /*LINTED*/ 966 ip = (uint32_t *)(p + rela->r_offset); 967 968 /* 969 * We only know about some specific relocation types. 970 */ 971 if (GELF_R_TYPE(rela->r_info) != R_SPARC_WDISP30 && 972 GELF_R_TYPE(rela->r_info) != R_SPARC_WPLT30) 973 return (-1); 974 975 /* 976 * We may have already processed this object file in an earlier linker 977 * invocation. Check to see if the present instruction sequence matches 978 * the one we would install below. 979 */ 980 if (isenabled) { 981 if (ip[0] == DT_OP_NOP) { 982 (*off) += sizeof (ip[0]); 983 return (0); 984 } 985 } else { 986 if (DT_IS_RESTORE(ip[1])) { 987 if (ip[0] == DT_OP_RET) { 988 (*off) += sizeof (ip[0]); 989 return (0); 990 } 991 } else if (DT_IS_MOV_O7(ip[1])) { 992 if (DT_IS_RETL(ip[0])) 993 return (0); 994 } else { 995 if (ip[0] == DT_OP_NOP) { 996 (*off) += sizeof (ip[0]); 997 return (0); 998 } 999 } 1000 } 1001 1002 /* 1003 * We only expect call instructions with a displacement of 0. 1004 */ 1005 if (ip[0] != DT_OP_CALL) { 1006 dt_dprintf("found %x instead of a call instruction at %llx\n", 1007 ip[0], (u_longlong_t)rela->r_offset); 1008 return (-1); 1009 } 1010 1011 if (isenabled) { 1012 /* 1013 * It would necessarily indicate incorrect usage if an is- 1014 * enabled probe were tail-called so flag that as an error. 1015 * It's also potentially (very) tricky to handle gracefully, 1016 * but could be done if this were a desired use scenario. 1017 */ 1018 if (DT_IS_RESTORE(ip[1]) || DT_IS_MOV_O7(ip[1])) { 1019 dt_dprintf("tail call to is-enabled probe at %llx\n", 1020 (u_longlong_t)rela->r_offset); 1021 return (-1); 1022 } 1023 1024 1025 /* 1026 * On SPARC, we take advantage of the fact that the first 1027 * argument shares the same register as for the return value. 1028 * The macro handles the work of zeroing that register so we 1029 * don't need to do anything special here. We instrument the 1030 * instruction in the delay slot as we'll need to modify the 1031 * return register after that instruction has been emulated. 1032 */ 1033 ip[0] = DT_OP_NOP; 1034 (*off) += sizeof (ip[0]); 1035 } else { 1036 /* 1037 * If the call is followed by a restore, it's a tail call so 1038 * change the call to a ret. If the call if followed by a mov 1039 * of a register into %o7, it's a tail call in leaf context 1040 * so change the call to a retl-like instruction that returns 1041 * to that register value + 8 (rather than the typical %o7 + 1042 * 8); the delay slot instruction is left, but should have no 1043 * effect. Otherwise we change the call to be a nop. We 1044 * identify the subsequent instruction as the probe point in 1045 * all but the leaf tail-call case to ensure that arguments to 1046 * the probe are complete and consistent. An astute, though 1047 * largely hypothetical, observer would note that there is the 1048 * possibility of a false-positive probe firing if the function 1049 * contained a branch to the instruction in the delay slot of 1050 * the call. Fixing this would require significant in-kernel 1051 * modifications, and isn't worth doing until we see it in the 1052 * wild. 1053 */ 1054 if (DT_IS_RESTORE(ip[1])) { 1055 ip[0] = DT_OP_RET; 1056 (*off) += sizeof (ip[0]); 1057 } else if (DT_IS_MOV_O7(ip[1])) { 1058 ip[0] = DT_MAKE_RETL(DT_RS2(ip[1])); 1059 } else { 1060 ip[0] = DT_OP_NOP; 1061 (*off) += sizeof (ip[0]); 1062 } 1063 } 1064 1065 return (0); 1066 } 1067 1068 #elif defined(__i386) || defined(__amd64) 1069 1070 #define DT_OP_NOP 0x90 1071 #define DT_OP_RET 0xc3 1072 #define DT_OP_CALL 0xe8 1073 #define DT_OP_JMP32 0xe9 1074 #define DT_OP_REX_RAX 0x48 1075 #define DT_OP_XOR_EAX_0 0x33 1076 #define DT_OP_XOR_EAX_1 0xc0 1077 1078 static int 1079 dt_modtext(dtrace_hdl_t *dtp, char *p, int isenabled, GElf_Rela *rela, 1080 uint32_t *off) 1081 { 1082 uint8_t *ip = (uint8_t *)(p + rela->r_offset - 1); 1083 uint8_t ret; 1084 1085 /* 1086 * On x86, the first byte of the instruction is the call opcode and 1087 * the next four bytes are the 32-bit address; the relocation is for 1088 * the address operand. We back up the offset to the first byte of 1089 * the instruction. For is-enabled probes, we later advance the offset 1090 * so that it hits the first nop in the instruction sequence. 1091 */ 1092 (*off) -= 1; 1093 1094 /* 1095 * We only know about some specific relocation types. Luckily 1096 * these types have the same values on both 32-bit and 64-bit 1097 * x86 architectures. 1098 */ 1099 if (GELF_R_TYPE(rela->r_info) != R_386_PC32 && 1100 GELF_R_TYPE(rela->r_info) != R_386_PLT32) 1101 return (-1); 1102 1103 /* 1104 * We may have already processed this object file in an earlier linker 1105 * invocation. Check to see if the present instruction sequence matches 1106 * the one we would install. For is-enabled probes, we advance the 1107 * offset to the first nop instruction in the sequence to match the 1108 * text modification code below. 1109 */ 1110 if (!isenabled) { 1111 if ((ip[0] == DT_OP_NOP || ip[0] == DT_OP_RET) && 1112 ip[1] == DT_OP_NOP && ip[2] == DT_OP_NOP && 1113 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) 1114 return (0); 1115 } else if (dtp->dt_oflags & DTRACE_O_LP64) { 1116 if (ip[0] == DT_OP_REX_RAX && 1117 ip[1] == DT_OP_XOR_EAX_0 && ip[2] == DT_OP_XOR_EAX_1 && 1118 (ip[3] == DT_OP_NOP || ip[3] == DT_OP_RET) && 1119 ip[4] == DT_OP_NOP) { 1120 (*off) += 3; 1121 return (0); 1122 } 1123 } else { 1124 if (ip[0] == DT_OP_XOR_EAX_0 && ip[1] == DT_OP_XOR_EAX_1 && 1125 (ip[2] == DT_OP_NOP || ip[2] == DT_OP_RET) && 1126 ip[3] == DT_OP_NOP && ip[4] == DT_OP_NOP) { 1127 (*off) += 2; 1128 return (0); 1129 } 1130 } 1131 1132 /* 1133 * We expect either a call instrution with a 32-bit displacement or a 1134 * jmp instruction with a 32-bit displacement acting as a tail-call. 1135 */ 1136 if (ip[0] != DT_OP_CALL && ip[0] != DT_OP_JMP32) { 1137 dt_dprintf("found %x instead of a call or jmp instruction at " 1138 "%llx\n", ip[0], (u_longlong_t)rela->r_offset); 1139 return (-1); 1140 } 1141 1142 ret = (ip[0] == DT_OP_JMP32) ? DT_OP_RET : DT_OP_NOP; 1143 1144 /* 1145 * Establish the instruction sequence -- all nops for probes, and an 1146 * instruction to clear the return value register (%eax/%rax) followed 1147 * by nops for is-enabled probes. For is-enabled probes, we advance 1148 * the offset to the first nop. This isn't stricly necessary but makes 1149 * for more readable disassembly when the probe is enabled. 1150 */ 1151 if (!isenabled) { 1152 ip[0] = ret; 1153 ip[1] = DT_OP_NOP; 1154 ip[2] = DT_OP_NOP; 1155 ip[3] = DT_OP_NOP; 1156 ip[4] = DT_OP_NOP; 1157 } else if (dtp->dt_oflags & DTRACE_O_LP64) { 1158 ip[0] = DT_OP_REX_RAX; 1159 ip[1] = DT_OP_XOR_EAX_0; 1160 ip[2] = DT_OP_XOR_EAX_1; 1161 ip[3] = ret; 1162 ip[4] = DT_OP_NOP; 1163 (*off) += 3; 1164 } else { 1165 ip[0] = DT_OP_XOR_EAX_0; 1166 ip[1] = DT_OP_XOR_EAX_1; 1167 ip[2] = ret; 1168 ip[3] = DT_OP_NOP; 1169 ip[4] = DT_OP_NOP; 1170 (*off) += 2; 1171 } 1172 1173 return (0); 1174 } 1175 1176 #else 1177 #error unknown ISA 1178 #endif 1179 1180 /*PRINTFLIKE5*/ 1181 static int 1182 dt_link_error(dtrace_hdl_t *dtp, Elf *elf, int fd, dt_link_pair_t *bufs, 1183 const char *format, ...) 1184 { 1185 va_list ap; 1186 dt_link_pair_t *pair; 1187 1188 va_start(ap, format); 1189 dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap); 1190 va_end(ap); 1191 1192 if (elf != NULL) 1193 (void) elf_end(elf); 1194 1195 if (fd >= 0) 1196 (void) close(fd); 1197 1198 while ((pair = bufs) != NULL) { 1199 bufs = pair->dlp_next; 1200 dt_free(dtp, pair->dlp_str); 1201 dt_free(dtp, pair->dlp_sym); 1202 dt_free(dtp, pair); 1203 } 1204 1205 return (dt_set_errno(dtp, EDT_COMPILER)); 1206 } 1207 1208 static int 1209 process_obj(dtrace_hdl_t *dtp, const char *obj, int *eprobesp) 1210 { 1211 static const char dt_prefix[] = "__dtrace"; 1212 static const char dt_enabled[] = "enabled"; 1213 static const char dt_symprefix[] = "$dtrace"; 1214 static const char dt_symfmt[] = "%s%ld.%s"; 1215 static const char dt_weaksymfmt[] = "%s.%s"; 1216 char probename[DTRACE_NAMELEN]; 1217 int fd, i, ndx, eprobe, mod = 0; 1218 Elf *elf = NULL; 1219 GElf_Ehdr ehdr; 1220 Elf_Scn *scn_rel, *scn_sym, *scn_str, *scn_tgt; 1221 Elf_Data *data_rel, *data_sym, *data_str, *data_tgt; 1222 GElf_Shdr shdr_rel, shdr_sym, shdr_str, shdr_tgt; 1223 GElf_Sym rsym, fsym, dsym; 1224 GElf_Rela rela; 1225 char *s, *p, *r; 1226 char pname[DTRACE_PROVNAMELEN]; 1227 dt_provider_t *pvp; 1228 dt_probe_t *prp; 1229 uint32_t off, eclass, emachine1, emachine2; 1230 size_t symsize, osym, nsym, isym, istr, len; 1231 key_t objkey; 1232 dt_link_pair_t *pair, *bufs = NULL; 1233 dt_strtab_t *strtab; 1234 void *tmp; 1235 1236 if ((fd = open64(obj, O_RDWR)) == -1) { 1237 return (dt_link_error(dtp, elf, fd, bufs, 1238 "failed to open %s: %s", obj, strerror(errno))); 1239 } 1240 1241 if ((elf = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) { 1242 return (dt_link_error(dtp, elf, fd, bufs, 1243 "failed to process %s: %s", obj, elf_errmsg(elf_errno()))); 1244 } 1245 1246 switch (elf_kind(elf)) { 1247 case ELF_K_ELF: 1248 break; 1249 case ELF_K_AR: 1250 return (dt_link_error(dtp, elf, fd, bufs, "archives are not " 1251 "permitted; use the contents of the archive instead: %s", 1252 obj)); 1253 default: 1254 return (dt_link_error(dtp, elf, fd, bufs, 1255 "invalid file type: %s", obj)); 1256 } 1257 1258 if (gelf_getehdr(elf, &ehdr) == NULL) { 1259 return (dt_link_error(dtp, elf, fd, bufs, "corrupt file: %s", 1260 obj)); 1261 } 1262 1263 if (dtp->dt_oflags & DTRACE_O_LP64) { 1264 eclass = ELFCLASS64; 1265 #if defined(__mips__) 1266 emachine1 = emachine2 = EM_MIPS; 1267 #elif defined(__powerpc__) 1268 emachine1 = emachine2 = EM_PPC64; 1269 #elif defined(__sparc) 1270 emachine1 = emachine2 = EM_SPARCV9; 1271 #elif defined(__i386) || defined(__amd64) 1272 emachine1 = emachine2 = EM_AMD64; 1273 #endif 1274 symsize = sizeof (Elf64_Sym); 1275 } else { 1276 eclass = ELFCLASS32; 1277 #if defined(__arm__) 1278 emachine1 = emachine2 = EM_ARM; 1279 #elif defined(__mips__) 1280 emachine1 = emachine2 = EM_MIPS; 1281 #elif defined(__powerpc__) 1282 emachine1 = emachine2 = EM_PPC; 1283 #elif defined(__sparc) 1284 emachine1 = EM_SPARC; 1285 emachine2 = EM_SPARC32PLUS; 1286 #elif defined(__i386) || defined(__amd64) 1287 emachine1 = emachine2 = EM_386; 1288 #endif 1289 symsize = sizeof (Elf32_Sym); 1290 } 1291 1292 if (ehdr.e_ident[EI_CLASS] != eclass) { 1293 return (dt_link_error(dtp, elf, fd, bufs, 1294 "incorrect ELF class for object file: %s", obj)); 1295 } 1296 1297 if (ehdr.e_machine != emachine1 && ehdr.e_machine != emachine2) { 1298 return (dt_link_error(dtp, elf, fd, bufs, 1299 "incorrect ELF machine type for object file: %s", obj)); 1300 } 1301 1302 /* 1303 * We use this token as a relatively unique handle for this file on the 1304 * system in order to disambiguate potential conflicts between files of 1305 * the same name which contain identially named local symbols. 1306 */ 1307 if ((objkey = ftok(obj, 0)) == (key_t)-1) { 1308 return (dt_link_error(dtp, elf, fd, bufs, 1309 "failed to generate unique key for object file: %s", obj)); 1310 } 1311 1312 scn_rel = NULL; 1313 while ((scn_rel = elf_nextscn(elf, scn_rel)) != NULL) { 1314 if (gelf_getshdr(scn_rel, &shdr_rel) == NULL) 1315 goto err; 1316 1317 /* 1318 * Skip any non-relocation sections. 1319 */ 1320 if (shdr_rel.sh_type != SHT_RELA && shdr_rel.sh_type != SHT_REL) 1321 continue; 1322 1323 if ((data_rel = elf_getdata(scn_rel, NULL)) == NULL) 1324 goto err; 1325 1326 /* 1327 * Grab the section, section header and section data for the 1328 * symbol table that this relocation section references. 1329 */ 1330 if ((scn_sym = elf_getscn(elf, shdr_rel.sh_link)) == NULL || 1331 gelf_getshdr(scn_sym, &shdr_sym) == NULL || 1332 (data_sym = elf_getdata(scn_sym, NULL)) == NULL) 1333 goto err; 1334 1335 /* 1336 * Ditto for that symbol table's string table. 1337 */ 1338 if ((scn_str = elf_getscn(elf, shdr_sym.sh_link)) == NULL || 1339 gelf_getshdr(scn_str, &shdr_str) == NULL || 1340 (data_str = elf_getdata(scn_str, NULL)) == NULL) 1341 goto err; 1342 1343 /* 1344 * Grab the section, section header and section data for the 1345 * target section for the relocations. For the relocations 1346 * we're looking for -- this will typically be the text of the 1347 * object file. 1348 */ 1349 if ((scn_tgt = elf_getscn(elf, shdr_rel.sh_info)) == NULL || 1350 gelf_getshdr(scn_tgt, &shdr_tgt) == NULL || 1351 (data_tgt = elf_getdata(scn_tgt, NULL)) == NULL) 1352 goto err; 1353 1354 /* 1355 * We're looking for relocations to symbols matching this form: 1356 * 1357 * __dtrace[enabled]_<prov>___<probe> 1358 * 1359 * For the generated object, we need to record the location 1360 * identified by the relocation, and create a new relocation 1361 * in the generated object that will be resolved at link time 1362 * to the location of the function in which the probe is 1363 * embedded. In the target object, we change the matched symbol 1364 * so that it will be ignored at link time, and we modify the 1365 * target (text) section to replace the call instruction with 1366 * one or more nops. 1367 * 1368 * To avoid runtime overhead, the relocations added to the 1369 * generated object should be resolved at static link time. We 1370 * therefore create aliases for the functions that contain 1371 * probes. An alias is global (so that the relocation from the 1372 * generated object can be resolved), and hidden (so that its 1373 * address is known at static link time). Such aliases have this 1374 * form: 1375 * 1376 * $dtrace<key>.<function> 1377 * 1378 * We take a first pass through all the relocations to 1379 * populate our string table and count the number of extra 1380 * symbols we'll require. 1381 */ 1382 strtab = dt_strtab_create(1); 1383 nsym = 0; 1384 isym = data_sym->d_size / symsize; 1385 istr = data_str->d_size; 1386 1387 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) { 1388 1389 if (shdr_rel.sh_type == SHT_RELA) { 1390 if (gelf_getrela(data_rel, i, &rela) == NULL) 1391 continue; 1392 } else { 1393 GElf_Rel rel; 1394 if (gelf_getrel(data_rel, i, &rel) == NULL) 1395 continue; 1396 rela.r_offset = rel.r_offset; 1397 rela.r_info = rel.r_info; 1398 rela.r_addend = 0; 1399 } 1400 1401 if (gelf_getsym(data_sym, GELF_R_SYM(rela.r_info), 1402 &rsym) == NULL) { 1403 dt_strtab_destroy(strtab); 1404 goto err; 1405 } 1406 1407 s = (char *)data_str->d_buf + rsym.st_name; 1408 1409 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0) 1410 continue; 1411 1412 if (dt_symtab_lookup(data_sym, 0, isym, rela.r_offset, 1413 shdr_rel.sh_info, &fsym, (emachine1 == EM_PPC64), 1414 elf) != 0) { 1415 dt_strtab_destroy(strtab); 1416 goto err; 1417 } 1418 1419 if (fsym.st_name > data_str->d_size) { 1420 dt_strtab_destroy(strtab); 1421 goto err; 1422 } 1423 1424 s = (char *)data_str->d_buf + fsym.st_name; 1425 1426 /* 1427 * If this symbol isn't of type function, we've really 1428 * driven off the rails or the object file is corrupt. 1429 */ 1430 if (GELF_ST_TYPE(fsym.st_info) != STT_FUNC) { 1431 dt_strtab_destroy(strtab); 1432 return (dt_link_error(dtp, elf, fd, bufs, 1433 "expected %s to be of type function", s)); 1434 } 1435 1436 len = snprintf(NULL, 0, dt_symfmt, dt_symprefix, 1437 objkey, s) + 1; 1438 if ((p = dt_alloc(dtp, len)) == NULL) { 1439 dt_strtab_destroy(strtab); 1440 goto err; 1441 } 1442 (void) snprintf(p, len, dt_symfmt, dt_symprefix, 1443 objkey, s); 1444 1445 if (dt_strtab_index(strtab, p) == -1) { 1446 nsym++; 1447 (void) dt_strtab_insert(strtab, p); 1448 } 1449 1450 dt_free(dtp, p); 1451 } 1452 1453 /* 1454 * If any probes were found, allocate the additional space for 1455 * the symbol table and string table, copying the old data into 1456 * the new buffers, and marking the buffers as dirty. We inject 1457 * those newly allocated buffers into the libelf data 1458 * structures, but are still responsible for freeing them once 1459 * we're done with the elf handle. 1460 */ 1461 if (nsym > 0) { 1462 /* 1463 * The first byte of the string table is reserved for 1464 * the \0 entry. 1465 */ 1466 len = dt_strtab_size(strtab) - 1; 1467 1468 assert(len > 0); 1469 assert(dt_strtab_index(strtab, "") == 0); 1470 1471 dt_strtab_destroy(strtab); 1472 1473 if ((pair = dt_alloc(dtp, sizeof (*pair))) == NULL) 1474 goto err; 1475 1476 if ((pair->dlp_str = dt_alloc(dtp, data_str->d_size + 1477 len)) == NULL) { 1478 dt_free(dtp, pair); 1479 goto err; 1480 } 1481 1482 if ((pair->dlp_sym = dt_alloc(dtp, data_sym->d_size + 1483 nsym * symsize)) == NULL) { 1484 dt_free(dtp, pair->dlp_str); 1485 dt_free(dtp, pair); 1486 goto err; 1487 } 1488 1489 pair->dlp_next = bufs; 1490 bufs = pair; 1491 1492 bcopy(data_str->d_buf, pair->dlp_str, data_str->d_size); 1493 tmp = data_str->d_buf; 1494 data_str->d_buf = pair->dlp_str; 1495 pair->dlp_str = tmp; 1496 data_str->d_size += len; 1497 (void) elf_flagdata(data_str, ELF_C_SET, ELF_F_DIRTY); 1498 1499 shdr_str.sh_size += len; 1500 (void) gelf_update_shdr(scn_str, &shdr_str); 1501 1502 bcopy(data_sym->d_buf, pair->dlp_sym, data_sym->d_size); 1503 tmp = data_sym->d_buf; 1504 data_sym->d_buf = pair->dlp_sym; 1505 pair->dlp_sym = tmp; 1506 data_sym->d_size += nsym * symsize; 1507 (void) elf_flagdata(data_sym, ELF_C_SET, ELF_F_DIRTY); 1508 1509 shdr_sym.sh_size += nsym * symsize; 1510 (void) gelf_update_shdr(scn_sym, &shdr_sym); 1511 1512 osym = isym; 1513 nsym += isym; 1514 } else { 1515 dt_strtab_destroy(strtab); 1516 continue; 1517 } 1518 1519 /* 1520 * Now that the tables have been allocated, perform the 1521 * modifications described above. 1522 */ 1523 for (i = 0; i < shdr_rel.sh_size / shdr_rel.sh_entsize; i++) { 1524 1525 if (shdr_rel.sh_type == SHT_RELA) { 1526 if (gelf_getrela(data_rel, i, &rela) == NULL) 1527 continue; 1528 } else { 1529 GElf_Rel rel; 1530 if (gelf_getrel(data_rel, i, &rel) == NULL) 1531 continue; 1532 rela.r_offset = rel.r_offset; 1533 rela.r_info = rel.r_info; 1534 rela.r_addend = 0; 1535 } 1536 1537 ndx = GELF_R_SYM(rela.r_info); 1538 1539 if (gelf_getsym(data_sym, ndx, &rsym) == NULL || 1540 rsym.st_name > data_str->d_size) 1541 goto err; 1542 1543 s = (char *)data_str->d_buf + rsym.st_name; 1544 1545 if (strncmp(s, dt_prefix, sizeof (dt_prefix) - 1) != 0) 1546 continue; 1547 1548 s += sizeof (dt_prefix) - 1; 1549 1550 /* 1551 * Check to see if this is an 'is-enabled' check as 1552 * opposed to a normal probe. 1553 */ 1554 if (strncmp(s, dt_enabled, 1555 sizeof (dt_enabled) - 1) == 0) { 1556 s += sizeof (dt_enabled) - 1; 1557 eprobe = 1; 1558 *eprobesp = 1; 1559 dt_dprintf("is-enabled probe\n"); 1560 } else { 1561 eprobe = 0; 1562 dt_dprintf("normal probe\n"); 1563 } 1564 1565 if (*s++ != '_') 1566 goto err; 1567 1568 if ((p = strstr(s, "___")) == NULL || 1569 p - s >= sizeof (pname)) 1570 goto err; 1571 1572 bcopy(s, pname, p - s); 1573 pname[p - s] = '\0'; 1574 1575 if (dt_symtab_lookup(data_sym, osym, isym, 1576 rela.r_offset, shdr_rel.sh_info, &fsym, 1577 (emachine1 == EM_PPC64), elf) == 0) { 1578 if (fsym.st_name > data_str->d_size) 1579 goto err; 1580 1581 r = s = (char *) data_str->d_buf + fsym.st_name; 1582 assert(strstr(s, dt_symprefix) == s); 1583 s = strchr(s, '.') + 1; 1584 } else if (dt_symtab_lookup(data_sym, 0, osym, 1585 rela.r_offset, shdr_rel.sh_info, &fsym, 1586 (emachine1 == EM_PPC64), elf) == 0) { 1587 u_int bind; 1588 1589 bind = GELF_ST_BIND(fsym.st_info) == STB_WEAK ? 1590 STB_WEAK : STB_GLOBAL; 1591 1592 /* 1593 * Emit an alias for the symbol. It needs to be 1594 * non-preemptible so that .SUNW_dof relocations 1595 * may be resolved at static link time. Aliases 1596 * of weak symbols are given a non-unique name 1597 * so that they may be merged by the linker. 1598 */ 1599 dsym = fsym; 1600 dsym.st_name = istr; 1601 dsym.st_info = GELF_ST_INFO(bind, STT_FUNC); 1602 dsym.st_other = GELF_ST_VISIBILITY(STV_HIDDEN); 1603 (void) gelf_update_sym(data_sym, isym, &dsym); 1604 r = (char *) data_str->d_buf + istr; 1605 s = (char *) data_str->d_buf + fsym.st_name; 1606 if (bind == STB_WEAK) 1607 istr += sprintf(r, dt_weaksymfmt, 1608 dt_symprefix, s); 1609 else 1610 istr += sprintf(r, dt_symfmt, 1611 dt_symprefix, objkey, s); 1612 istr++; 1613 isym++; 1614 assert(isym <= nsym); 1615 } else 1616 goto err; 1617 1618 if ((pvp = dt_provider_lookup(dtp, pname)) == NULL) { 1619 return (dt_link_error(dtp, elf, fd, bufs, 1620 "no such provider %s", pname)); 1621 } 1622 1623 if (strlcpy(probename, p + 3, sizeof (probename)) >= 1624 sizeof (probename)) 1625 return (dt_link_error(dtp, elf, fd, bufs, 1626 "invalid probe name %s", probename)); 1627 (void) strhyphenate(probename); 1628 if ((prp = dt_probe_lookup(pvp, probename)) == NULL) 1629 return (dt_link_error(dtp, elf, fd, bufs, 1630 "no such probe %s", probename)); 1631 1632 assert(fsym.st_value <= rela.r_offset); 1633 1634 off = rela.r_offset - fsym.st_value; 1635 if (dt_modtext(dtp, data_tgt->d_buf, eprobe, 1636 &rela, &off) != 0) 1637 goto err; 1638 1639 if (dt_probe_define(pvp, prp, s, r, off, eprobe) != 0) { 1640 return (dt_link_error(dtp, elf, fd, bufs, 1641 "failed to allocate space for probe")); 1642 } 1643 #ifndef illumos 1644 /* 1645 * Our linker doesn't understand the SUNW_IGNORE ndx and 1646 * will try to use this relocation when we build the 1647 * final executable. Since we are done processing this 1648 * relocation, mark it as inexistant and let libelf 1649 * remove it from the file. 1650 * If this wasn't done, we would have garbage added to 1651 * the executable file as the symbol is going to be 1652 * change from UND to ABS. 1653 */ 1654 if (shdr_rel.sh_type == SHT_RELA) { 1655 rela.r_offset = 0; 1656 rela.r_info = 0; 1657 rela.r_addend = 0; 1658 (void) gelf_update_rela(data_rel, i, &rela); 1659 } else { 1660 GElf_Rel rel; 1661 rel.r_offset = 0; 1662 rel.r_info = 0; 1663 (void) gelf_update_rel(data_rel, i, &rel); 1664 } 1665 #endif 1666 1667 mod = 1; 1668 (void) elf_flagdata(data_tgt, ELF_C_SET, ELF_F_DIRTY); 1669 1670 /* 1671 * This symbol may already have been marked to 1672 * be ignored by another relocation referencing 1673 * the same symbol or if this object file has 1674 * already been processed by an earlier link 1675 * invocation. 1676 */ 1677 #ifndef illumos 1678 #define SHN_SUNW_IGNORE SHN_ABS 1679 #endif 1680 if (rsym.st_shndx != SHN_SUNW_IGNORE) { 1681 rsym.st_shndx = SHN_SUNW_IGNORE; 1682 (void) gelf_update_sym(data_sym, ndx, &rsym); 1683 } 1684 } 1685 } 1686 1687 if (mod && elf_update(elf, ELF_C_WRITE) == -1) 1688 goto err; 1689 1690 (void) elf_end(elf); 1691 (void) close(fd); 1692 1693 while ((pair = bufs) != NULL) { 1694 bufs = pair->dlp_next; 1695 dt_free(dtp, pair->dlp_str); 1696 dt_free(dtp, pair->dlp_sym); 1697 dt_free(dtp, pair); 1698 } 1699 1700 return (0); 1701 1702 err: 1703 return (dt_link_error(dtp, elf, fd, bufs, 1704 "an error was encountered while processing %s", obj)); 1705 } 1706 1707 int 1708 dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags, 1709 const char *file, int objc, char *const objv[]) 1710 { 1711 #ifndef illumos 1712 char tfile[PATH_MAX]; 1713 #endif 1714 char drti[PATH_MAX]; 1715 dof_hdr_t *dof; 1716 int fd, status, i, cur; 1717 char *cmd, tmp; 1718 size_t len; 1719 int eprobes = 0, ret = 0; 1720 1721 #ifndef illumos 1722 if (access(file, R_OK) == 0) { 1723 fprintf(stderr, "dtrace: target object (%s) already exists. " 1724 "Please remove the target\ndtrace: object and rebuild all " 1725 "the source objects if you wish to run the DTrace\n" 1726 "dtrace: linking process again\n", file); 1727 /* 1728 * Several build infrastructures run DTrace twice (e.g. 1729 * postgres) and we don't want the build to fail. Return 1730 * 0 here since this isn't really a fatal error. 1731 */ 1732 return (0); 1733 } 1734 #endif 1735 1736 /* 1737 * A NULL program indicates a special use in which we just link 1738 * together a bunch of object files specified in objv and then 1739 * unlink(2) those object files. 1740 */ 1741 if (pgp == NULL) { 1742 const char *fmt = "%s -o %s -r"; 1743 1744 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file) + 1; 1745 1746 for (i = 0; i < objc; i++) 1747 len += strlen(objv[i]) + 1; 1748 1749 cmd = alloca(len); 1750 1751 cur = snprintf(cmd, len, fmt, dtp->dt_ld_path, file); 1752 1753 for (i = 0; i < objc; i++) 1754 cur += snprintf(cmd + cur, len - cur, " %s", objv[i]); 1755 1756 if ((status = system(cmd)) == -1) { 1757 return (dt_link_error(dtp, NULL, -1, NULL, 1758 "failed to run %s: %s", dtp->dt_ld_path, 1759 strerror(errno))); 1760 } 1761 1762 if (WIFSIGNALED(status)) { 1763 return (dt_link_error(dtp, NULL, -1, NULL, 1764 "failed to link %s: %s failed due to signal %d", 1765 file, dtp->dt_ld_path, WTERMSIG(status))); 1766 } 1767 1768 if (WEXITSTATUS(status) != 0) { 1769 return (dt_link_error(dtp, NULL, -1, NULL, 1770 "failed to link %s: %s exited with status %d\n", 1771 file, dtp->dt_ld_path, WEXITSTATUS(status))); 1772 } 1773 1774 for (i = 0; i < objc; i++) { 1775 if (strcmp(objv[i], file) != 0) 1776 (void) unlink(objv[i]); 1777 } 1778 1779 return (0); 1780 } 1781 1782 for (i = 0; i < objc; i++) { 1783 if (process_obj(dtp, objv[i], &eprobes) != 0) 1784 return (-1); /* errno is set for us */ 1785 } 1786 1787 /* 1788 * If there are is-enabled probes then we need to force use of DOF 1789 * version 2. 1790 */ 1791 if (eprobes && pgp->dp_dofversion < DOF_VERSION_2) 1792 pgp->dp_dofversion = DOF_VERSION_2; 1793 1794 if ((dof = dtrace_dof_create(dtp, pgp, dflags)) == NULL) 1795 return (-1); /* errno is set for us */ 1796 1797 #ifdef illumos 1798 /* 1799 * Create a temporary file and then unlink it if we're going to 1800 * combine it with drti.o later. We can still refer to it in child 1801 * processes as /dev/fd/<fd>. 1802 */ 1803 if ((fd = open64(file, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) { 1804 return (dt_link_error(dtp, NULL, -1, NULL, 1805 "failed to open %s: %s", file, strerror(errno))); 1806 } 1807 #else 1808 snprintf(tfile, sizeof(tfile), "%s.XXXXXX", file); 1809 if ((fd = mkostemp(tfile, O_CLOEXEC)) == -1) 1810 return (dt_link_error(dtp, NULL, -1, NULL, 1811 "failed to create temporary file %s: %s", 1812 tfile, strerror(errno))); 1813 #endif 1814 1815 /* 1816 * If -xlinktype=DOF has been selected, just write out the DOF. 1817 * Otherwise proceed to the default of generating and linking ELF. 1818 */ 1819 switch (dtp->dt_linktype) { 1820 case DT_LTYP_DOF: 1821 if (dt_write(dtp, fd, dof, dof->dofh_filesz) < dof->dofh_filesz) 1822 ret = errno; 1823 1824 if (close(fd) != 0 && ret == 0) 1825 ret = errno; 1826 1827 if (ret != 0) { 1828 return (dt_link_error(dtp, NULL, -1, NULL, 1829 "failed to write %s: %s", file, strerror(ret))); 1830 } 1831 1832 return (0); 1833 1834 case DT_LTYP_ELF: 1835 break; /* fall through to the rest of dtrace_program_link() */ 1836 1837 default: 1838 return (dt_link_error(dtp, NULL, -1, NULL, 1839 "invalid link type %u\n", dtp->dt_linktype)); 1840 } 1841 1842 1843 #ifdef illumos 1844 if (!dtp->dt_lazyload) 1845 (void) unlink(file); 1846 #endif 1847 1848 if (dtp->dt_oflags & DTRACE_O_LP64) 1849 status = dump_elf64(dtp, dof, fd); 1850 else 1851 status = dump_elf32(dtp, dof, fd); 1852 1853 #ifdef illumos 1854 if (status != 0 || lseek(fd, 0, SEEK_SET) != 0) { 1855 return (dt_link_error(dtp, NULL, -1, NULL, 1856 "failed to write %s: %s", file, strerror(errno))); 1857 } 1858 #else 1859 if (status != 0) 1860 return (dt_link_error(dtp, NULL, -1, NULL, 1861 "failed to write %s: %s", tfile, 1862 strerror(dtrace_errno(dtp)))); 1863 #endif 1864 1865 if (!dtp->dt_lazyload) { 1866 #ifdef illumos 1867 const char *fmt = "%s -o %s -r -Blocal -Breduce /dev/fd/%d %s"; 1868 1869 if (dtp->dt_oflags & DTRACE_O_LP64) { 1870 (void) snprintf(drti, sizeof (drti), 1871 "%s/64/drti.o", _dtrace_libdir); 1872 } else { 1873 (void) snprintf(drti, sizeof (drti), 1874 "%s/drti.o", _dtrace_libdir); 1875 } 1876 1877 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, fd, 1878 drti) + 1; 1879 1880 cmd = alloca(len); 1881 1882 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, fd, drti); 1883 #else 1884 const char *fmt = "%s -o %s -r %s %s"; 1885 dt_dirpath_t *dp = dt_list_next(&dtp->dt_lib_path); 1886 1887 (void) snprintf(drti, sizeof (drti), "%s/drti.o", dp->dir_path); 1888 1889 len = snprintf(&tmp, 1, fmt, dtp->dt_ld_path, file, tfile, 1890 drti) + 1; 1891 1892 cmd = alloca(len); 1893 1894 (void) snprintf(cmd, len, fmt, dtp->dt_ld_path, file, tfile, 1895 drti); 1896 #endif 1897 if ((status = system(cmd)) == -1) { 1898 ret = dt_link_error(dtp, NULL, fd, NULL, 1899 "failed to run %s: %s", dtp->dt_ld_path, 1900 strerror(errno)); 1901 goto done; 1902 } 1903 1904 if (WIFSIGNALED(status)) { 1905 ret = dt_link_error(dtp, NULL, fd, NULL, 1906 "failed to link %s: %s failed due to signal %d", 1907 file, dtp->dt_ld_path, WTERMSIG(status)); 1908 goto done; 1909 } 1910 1911 if (WEXITSTATUS(status) != 0) { 1912 ret = dt_link_error(dtp, NULL, fd, NULL, 1913 "failed to link %s: %s exited with status %d\n", 1914 file, dtp->dt_ld_path, WEXITSTATUS(status)); 1915 goto done; 1916 } 1917 (void) close(fd); /* release temporary file */ 1918 1919 #if defined(__FreeBSD__) || defined(__NetBSD__) 1920 /* 1921 * Now that we've linked drti.o, reduce the global __SUNW_dof 1922 * symbol to a local symbol. This is needed to so that multiple 1923 * generated object files (for different providers, for 1924 * instance) can be linked together. This is accomplished using 1925 * the -Blocal flag with Sun's linker, but GNU ld doesn't appear 1926 * to have an equivalent option. 1927 */ 1928 asprintf(&cmd, "%s --localize-hidden %s", dtp->dt_objcopy_path, 1929 file); 1930 if ((status = system(cmd)) == -1) { 1931 ret = dt_link_error(dtp, NULL, -1, NULL, 1932 "failed to run %s: %s", dtp->dt_objcopy_path, 1933 strerror(errno)); 1934 free(cmd); 1935 goto done; 1936 } 1937 free(cmd); 1938 1939 if (WIFSIGNALED(status)) { 1940 ret = dt_link_error(dtp, NULL, -1, NULL, 1941 "failed to link %s: %s failed due to signal %d", 1942 file, dtp->dt_objcopy_path, WTERMSIG(status)); 1943 goto done; 1944 } 1945 1946 if (WEXITSTATUS(status) != 0) { 1947 ret = dt_link_error(dtp, NULL, -1, NULL, 1948 "failed to link %s: %s exited with status %d\n", 1949 file, dtp->dt_objcopy_path, WEXITSTATUS(status)); 1950 goto done; 1951 } 1952 #endif 1953 } else { 1954 #if defined(__FreeBSD__) || defined(__NetBSD__) 1955 if (rename(tfile, file) != 0) { 1956 ret = dt_link_error(dtp, NULL, fd, NULL, 1957 "failed to rename %s to %s: %s", tfile, file, 1958 strerror(errno)); 1959 goto done; 1960 } 1961 #endif 1962 (void) close(fd); 1963 } 1964 1965 done: 1966 dtrace_dof_destroy(dtp, dof); 1967 1968 #if defined(__FreeBSD__) || defined(__NetBSD__) 1969 if (!dtp->dt_lazyload) 1970 (void) unlink(tfile); 1971 #endif 1972 return (ret); 1973 } 1974