| 1 | /* |
| 2 | * Copyright (c) 2008 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 6 | * This file contains Original Code and/or Modifications of Original Code |
| 7 | * as defined in and that are subject to the Apple Public Source License |
| 8 | * Version 2.0 (the 'License'). You may not use this file except in |
| 9 | * compliance with the License. The rights granted to you under the License |
| 10 | * may not be used to create, or enable the creation or redistribution of, |
| 11 | * unlawful or unlicensed copies of an Apple operating system, or to |
| 12 | * circumvent, violate, or enable the circumvention or violation of, any |
| 13 | * terms of an Apple operating system software license agreement. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 18 | * The Original Code and all software distributed under the License are |
| 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 23 | * Please see the License for the specific language governing rights and |
| 24 | * limitations under the License. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | #include <string.h> |
| 29 | #include <mach-o/loader.h> |
| 30 | #include <mach-o/nlist.h> |
| 31 | #include <sys/queue.h> |
| 32 | #include <sys/types.h> |
| 33 | |
| 34 | #define DEBUG_ASSERT_COMPONENT_NAME_STRING "kxld" |
| 35 | #include <AssertMacros.h> |
| 36 | |
| 37 | #include "kxld_array.h" |
| 38 | #include "kxld_dict.h" |
| 39 | #include "kxld_sect.h" |
| 40 | #include "kxld_sym.h" |
| 41 | #include "kxld_symtab.h" |
| 42 | #include "kxld_util.h" |
| 43 | |
| 44 | struct kxld_symtab { |
| 45 | KXLDArray syms; |
| 46 | KXLDDict cxx_index; |
| 47 | KXLDDict name_index; |
| 48 | char *strings; |
| 49 | u_int strsize; |
| 50 | boolean_t cxx_index_initialized; |
| 51 | boolean_t name_index_initialized; |
| 52 | }; |
| 53 | |
| 54 | /******************************************************************************* |
| 55 | * Prototypes |
| 56 | *******************************************************************************/ |
| 57 | |
| 58 | static kern_return_t init_macho(KXLDSymtab *symtab, struct symtab_command *src, |
| 59 | u_char *macho, KXLDSeg * kernel_linkedit_seg, |
| 60 | boolean_t is_32_bit) |
| 61 | __attribute__((nonnull(1,2))); |
| 62 | |
| 63 | #if KXLD_USER_OR_ILP32 |
| 64 | static kern_return_t init_syms_32(KXLDSymtab *symtab, u_char *macho, u_long offset, |
| 65 | u_int nsyms); |
| 66 | #endif |
| 67 | #if KXLD_USER_OR_LP64 |
| 68 | static kern_return_t init_syms_64(KXLDSymtab *symtab, u_char *macho, u_long offset, |
| 69 | u_int nsyms); |
| 70 | #endif |
| 71 | |
| 72 | static void restrict_private_symbols(KXLDSymtab *symtab) |
| 73 | __attribute__((nonnull)); |
| 74 | static boolean_t sym_is_defined_cxx(const KXLDSym *sym); |
| 75 | static boolean_t sym_is_name_indexed(const KXLDSym *sym); |
| 76 | |
| 77 | /******************************************************************************* |
| 78 | *******************************************************************************/ |
| 79 | size_t |
| 80 | kxld_symtab_sizeof() |
| 81 | { |
| 82 | return sizeof(KXLDSymtab); |
| 83 | } |
| 84 | |
| 85 | #if KXLD_USER_OR_ILP32 |
| 86 | /******************************************************************************* |
| 87 | *******************************************************************************/ |
| 88 | kern_return_t |
| 89 | kxld_symtab_init_from_macho_32(KXLDSymtab *symtab, struct symtab_command *src, |
| 90 | u_char *macho, KXLDSeg * kernel_linkedit_seg) |
| 91 | { |
| 92 | return init_macho(symtab, src, macho, kernel_linkedit_seg, |
| 93 | /* is_32_bit */ TRUE); |
| 94 | } |
| 95 | #endif /* KXLD_USER_ILP32 */ |
| 96 | |
| 97 | #if KXLD_USER_OR_LP64 |
| 98 | /******************************************************************************* |
| 99 | *******************************************************************************/ |
| 100 | kern_return_t |
| 101 | kxld_symtab_init_from_macho_64(KXLDSymtab *symtab, struct symtab_command *src, |
| 102 | u_char *macho, KXLDSeg * kernel_linkedit_seg) |
| 103 | { |
| 104 | return init_macho(symtab, src, macho, kernel_linkedit_seg, |
| 105 | /* is_32_bit */ FALSE); |
| 106 | } |
| 107 | #endif /* KXLD_USER_OR_LP64 */ |
| 108 | |
| 109 | /******************************************************************************* |
| 110 | *******************************************************************************/ |
| 111 | static kern_return_t |
| 112 | init_macho(KXLDSymtab *symtab, struct symtab_command *src, |
| 113 | u_char *macho, KXLDSeg * kernel_linkedit_seg, |
| 114 | boolean_t is_32_bit __unused) |
| 115 | { |
| 116 | kern_return_t rval = KERN_FAILURE; |
| 117 | u_long symoff; |
| 118 | u_char * macho_or_linkedit = macho; |
| 119 | |
| 120 | check(symtab); |
| 121 | check(src); |
| 122 | check(macho); |
| 123 | |
| 124 | /* Initialize the symbol array */ |
| 125 | |
| 126 | rval = kxld_array_init(&symtab->syms, sizeof(KXLDSym), src->nsyms); |
| 127 | require_noerr(rval, finish); |
| 128 | |
| 129 | /* Initialize the string table */ |
| 130 | |
| 131 | if (kernel_linkedit_seg) { |
| 132 | |
| 133 | /* If initing the kernel file in memory, we can't trust |
| 134 | * the symtab offsets directly, because the kernel file has been mapped |
| 135 | * into memory and the mach-o offsets are disk-based. |
| 136 | * |
| 137 | * The symoff is an offset relative to the linkedit segment |
| 138 | * so we just subtract the fileoffset of the linkedit segment |
| 139 | * to get its relative start. |
| 140 | * |
| 141 | * The strings table is an actual pointer, so we calculate that from |
| 142 | * the linkedit's vmaddr. |
| 143 | * |
| 144 | * Further, the init_syms_... functions need an adjusted base |
| 145 | * pointer instead of the beginning of the macho, so we substitute |
| 146 | * the base of the linkedit segment. |
| 147 | */ |
| 148 | |
| 149 | symoff = (u_long)(src->symoff - kernel_linkedit_seg->fileoff); |
| 150 | symtab->strings = (char *)(uintptr_t)kernel_linkedit_seg->base_addr + |
| 151 | src->stroff - kernel_linkedit_seg->fileoff; |
| 152 | macho_or_linkedit = (u_char *)(uintptr_t)kernel_linkedit_seg->base_addr; |
| 153 | } else { |
| 154 | symoff = (u_long)src->symoff; |
| 155 | symtab->strings = (char *) (macho + src->stroff); |
| 156 | } |
| 157 | |
| 158 | symtab->strsize = src->strsize; |
| 159 | |
| 160 | /* Initialize the symbols */ |
| 161 | |
| 162 | KXLD_3264_FUNC(is_32_bit, rval, |
| 163 | init_syms_32, init_syms_64, |
| 164 | symtab, macho_or_linkedit, symoff, src->nsyms); |
| 165 | require_noerr(rval, finish); |
| 166 | |
| 167 | /* Some symbols must be forced private for compatibility */ |
| 168 | (void) restrict_private_symbols(symtab); |
| 169 | |
| 170 | /* Save the output */ |
| 171 | |
| 172 | rval = KERN_SUCCESS; |
| 173 | |
| 174 | finish: |
| 175 | return rval; |
| 176 | } |
| 177 | |
| 178 | #if KXLD_USER_OR_ILP32 |
| 179 | /******************************************************************************* |
| 180 | * In the running kernel, 'macho' is actually the start of the linkedit segment. |
| 181 | *******************************************************************************/ |
| 182 | static kern_return_t |
| 183 | init_syms_32(KXLDSymtab *symtab, u_char *macho, u_long offset, u_int nsyms) |
| 184 | { |
| 185 | kern_return_t rval = KERN_FAILURE; |
| 186 | KXLDSym *sym = NULL; |
| 187 | u_int i = 0; |
| 188 | struct nlist *src_syms = (struct nlist *) ((void *) (macho + offset)); |
| 189 | |
| 190 | for (i = 0; i < nsyms; ++i) { |
| 191 | sym = kxld_array_get_item(&symtab->syms, i); |
| 192 | require_action(sym, finish, rval=KERN_FAILURE); |
| 193 | |
| 194 | rval = kxld_sym_init_from_macho32(sym, symtab->strings, &src_syms[i]); |
| 195 | require_noerr(rval, finish); |
| 196 | } |
| 197 | |
| 198 | rval = KERN_SUCCESS; |
| 199 | |
| 200 | finish: |
| 201 | return rval; |
| 202 | } |
| 203 | #endif /* KXLD_USER_OR_ILP32 */ |
| 204 | |
| 205 | #if KXLD_USER_OR_LP64 |
| 206 | /******************************************************************************* |
| 207 | * In the running kernel, 'macho' is actually the start of the linkedit segment. |
| 208 | *******************************************************************************/ |
| 209 | static kern_return_t |
| 210 | init_syms_64(KXLDSymtab *symtab, u_char *macho, u_long offset, u_int nsyms) |
| 211 | { |
| 212 | kern_return_t rval = KERN_FAILURE; |
| 213 | KXLDSym *sym = NULL; |
| 214 | u_int i = 0; |
| 215 | struct nlist_64 *src_syms = (struct nlist_64 *) ((void *) (macho + offset)); |
| 216 | |
| 217 | for (i = 0; i < nsyms; ++i) { |
| 218 | sym = kxld_array_get_item(&symtab->syms, i); |
| 219 | require_action(sym, finish, rval=KERN_FAILURE); |
| 220 | |
| 221 | rval = kxld_sym_init_from_macho64(sym, symtab->strings, &src_syms[i]); |
| 222 | require_noerr(rval, finish); |
| 223 | } |
| 224 | |
| 225 | rval = KERN_SUCCESS; |
| 226 | |
| 227 | finish: |
| 228 | return rval; |
| 229 | } |
| 230 | #endif /* KXLD_USER_OR_LP64 */ |
| 231 | |
| 232 | /******************************************************************************* |
| 233 | * Temporary workaround for PR-6668105 |
| 234 | * new, new[], delete, and delete[] may be overridden globally in a kext. |
| 235 | * We should do this with some sort of weak symbols, but we'll use a whitelist |
| 236 | * for now to minimize risk. |
| 237 | *******************************************************************************/ |
| 238 | static void |
| 239 | restrict_private_symbols(KXLDSymtab *symtab) |
| 240 | { |
| 241 | const char *private_symbols[] = { |
| 242 | KXLD_KMOD_INFO_SYMBOL, |
| 243 | KXLD_OPERATOR_NEW_SYMBOL, |
| 244 | KXLD_OPERATOR_NEW_ARRAY_SYMBOL, |
| 245 | KXLD_OPERATOR_DELETE_SYMBOL, |
| 246 | KXLD_OPERATOR_DELETE_ARRAY_SYMBOL |
| 247 | }; |
| 248 | KXLDSymtabIterator iter; |
| 249 | KXLDSym *sym = NULL; |
| 250 | const char *name = NULL; |
| 251 | u_int i = 0; |
| 252 | |
| 253 | kxld_symtab_iterator_init(&iter, symtab, kxld_sym_is_exported, FALSE); |
| 254 | while ((sym = kxld_symtab_iterator_get_next(&iter))) { |
| 255 | for (i = 0; i < const_array_len(private_symbols); ++i) { |
| 256 | name = private_symbols[i]; |
| 257 | if (!streq(sym->name, name)) { |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | kxld_sym_mark_private(sym); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /******************************************************************************* |
| 268 | *******************************************************************************/ |
| 269 | void |
| 270 | kxld_symtab_iterator_init(KXLDSymtabIterator *iter, const KXLDSymtab *symtab, |
| 271 | KXLDSymPredicateTest test, boolean_t negate) |
| 272 | { |
| 273 | check(iter); |
| 274 | check(symtab); |
| 275 | check(test); |
| 276 | |
| 277 | iter->symtab = symtab; |
| 278 | iter->idx = 0; |
| 279 | iter->test = test; |
| 280 | iter->negate = negate; |
| 281 | } |
| 282 | |
| 283 | /******************************************************************************* |
| 284 | *******************************************************************************/ |
| 285 | void |
| 286 | kxld_symtab_clear(KXLDSymtab *symtab) |
| 287 | { |
| 288 | check(symtab); |
| 289 | |
| 290 | kxld_array_clear(&symtab->syms); |
| 291 | kxld_dict_clear(&symtab->cxx_index); |
| 292 | kxld_dict_clear(&symtab->name_index); |
| 293 | symtab->strings = NULL; |
| 294 | symtab->strsize = 0; |
| 295 | symtab->cxx_index_initialized = 0; |
| 296 | symtab->name_index_initialized = 0; |
| 297 | } |
| 298 | |
| 299 | /******************************************************************************* |
| 300 | *******************************************************************************/ |
| 301 | void |
| 302 | kxld_symtab_deinit(KXLDSymtab *symtab) |
| 303 | { |
| 304 | check(symtab); |
| 305 | |
| 306 | kxld_array_deinit(&symtab->syms); |
| 307 | kxld_dict_deinit(&symtab->cxx_index); |
| 308 | kxld_dict_deinit(&symtab->name_index); |
| 309 | bzero(symtab, sizeof(*symtab)); |
| 310 | } |
| 311 | |
| 312 | /******************************************************************************* |
| 313 | *******************************************************************************/ |
| 314 | u_int |
| 315 | kxld_symtab_get_num_symbols(const KXLDSymtab *symtab) |
| 316 | { |
| 317 | check(symtab); |
| 318 | |
| 319 | return symtab->syms.nitems; |
| 320 | } |
| 321 | |
| 322 | /******************************************************************************* |
| 323 | *******************************************************************************/ |
| 324 | KXLDSym * |
| 325 | kxld_symtab_get_symbol_by_index(const KXLDSymtab *symtab, u_int idx) |
| 326 | { |
| 327 | check(symtab); |
| 328 | |
| 329 | return kxld_array_get_item(&symtab->syms, idx); |
| 330 | } |
| 331 | |
| 332 | /******************************************************************************* |
| 333 | *******************************************************************************/ |
| 334 | KXLDSym * |
| 335 | kxld_symtab_get_symbol_by_name(const KXLDSymtab *symtab, const char *name) |
| 336 | { |
| 337 | KXLDSym *sym = NULL; |
| 338 | u_int i = 0; |
| 339 | |
| 340 | for (i = 0; i < symtab->syms.nitems; ++i) { |
| 341 | sym = kxld_array_get_item(&symtab->syms, i); |
| 342 | |
| 343 | if (streq(sym->name, name)) { |
| 344 | return sym; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | /******************************************************************************* |
| 352 | *******************************************************************************/ |
| 353 | KXLDSym * |
| 354 | kxld_symtab_get_locally_defined_symbol_by_name(const KXLDSymtab *symtab, |
| 355 | const char *name) |
| 356 | { |
| 357 | check(symtab); |
| 358 | check(name); |
| 359 | |
| 360 | return kxld_dict_find(&symtab->name_index, name); |
| 361 | } |
| 362 | |
| 363 | /******************************************************************************* |
| 364 | *******************************************************************************/ |
| 365 | KXLDSym * |
| 366 | kxld_symtab_get_cxx_symbol_by_value(const KXLDSymtab *symtab, kxld_addr_t value) |
| 367 | { |
| 368 | check(symtab); |
| 369 | |
| 370 | return kxld_dict_find(&symtab->cxx_index, &value); |
| 371 | } |
| 372 | |
| 373 | /******************************************************************************* |
| 374 | *******************************************************************************/ |
| 375 | kern_return_t |
| 376 | kxld_symtab_get_sym_index(const KXLDSymtab *symtab, const KXLDSym *sym, |
| 377 | u_int *symindex) |
| 378 | { |
| 379 | kern_return_t rval = KERN_FAILURE; |
| 380 | |
| 381 | rval = kxld_array_get_index(&symtab->syms, sym, symindex); |
| 382 | require_noerr(rval, finish); |
| 383 | |
| 384 | rval = KERN_SUCCESS; |
| 385 | |
| 386 | finish: |
| 387 | return rval; |
| 388 | } |
| 389 | |
| 390 | /******************************************************************************* |
| 391 | *******************************************************************************/ |
| 392 | u_long |
| 393 | (void) |
| 394 | { |
| 395 | return sizeof(struct symtab_command); |
| 396 | } |
| 397 | |
| 398 | /******************************************************************************* |
| 399 | *******************************************************************************/ |
| 400 | u_long |
| 401 | kxld_symtab_get_macho_data_size(const KXLDSymtab *symtab, boolean_t is_32_bit) |
| 402 | { |
| 403 | KXLDSymtabIterator iter; |
| 404 | KXLDSym *sym = NULL; |
| 405 | u_long size = 1; /* strtab start padding */ |
| 406 | u_int nsyms = 0; |
| 407 | |
| 408 | check(symtab); |
| 409 | |
| 410 | kxld_symtab_iterator_init(&iter, symtab, |
| 411 | kxld_sym_is_defined_locally, FALSE); |
| 412 | |
| 413 | while ((sym = kxld_symtab_iterator_get_next(&iter))) { |
| 414 | size += strlen(sym->name) + 1; |
| 415 | ++nsyms; |
| 416 | } |
| 417 | |
| 418 | if (is_32_bit) { |
| 419 | size += nsyms * sizeof(struct nlist); |
| 420 | } else { |
| 421 | size += nsyms * sizeof(struct nlist_64); |
| 422 | } |
| 423 | |
| 424 | size = (size + 7) & ~7; |
| 425 | |
| 426 | return size; |
| 427 | } |
| 428 | |
| 429 | /******************************************************************************* |
| 430 | *******************************************************************************/ |
| 431 | kern_return_t |
| 432 | kxld_symtab_export_macho(const KXLDSymtab *symtab, u_char *buf, |
| 433 | u_long *, u_long , |
| 434 | u_long *data_offset, u_long data_size, |
| 435 | boolean_t is_32_bit) |
| 436 | { |
| 437 | kern_return_t rval = KERN_FAILURE; |
| 438 | KXLDSymtabIterator iter; |
| 439 | KXLDSym *sym = NULL; |
| 440 | struct symtab_command *symtabhdr = NULL; |
| 441 | u_char *nl = NULL; |
| 442 | u_long nlistsize = 0; |
| 443 | char *strtab = NULL; |
| 444 | u_long stroff = 1; /* strtab start padding */ |
| 445 | |
| 446 | check(symtab); |
| 447 | check(buf); |
| 448 | check(header_offset); |
| 449 | check(data_offset); |
| 450 | |
| 451 | require_action(sizeof(*symtabhdr) <= header_size - *header_offset, |
| 452 | finish, rval=KERN_FAILURE); |
| 453 | symtabhdr = (struct symtab_command *) ((void *) (buf + *header_offset)); |
| 454 | *header_offset += sizeof(*symtabhdr); |
| 455 | |
| 456 | /* Initialize the symbol table header */ |
| 457 | |
| 458 | // note - this assumes LC_SYMTAB is always before the LC_DYSYMTAB in the |
| 459 | // macho header we are processing. |
| 460 | symtabhdr->cmd = LC_SYMTAB; |
| 461 | symtabhdr->cmdsize = (uint32_t) sizeof(*symtabhdr); |
| 462 | symtabhdr->symoff = (uint32_t) *data_offset; |
| 463 | symtabhdr->strsize = 1; /* strtab start padding */ |
| 464 | |
| 465 | /* Find the size of the symbol and string tables */ |
| 466 | |
| 467 | kxld_symtab_iterator_init(&iter, symtab, |
| 468 | kxld_sym_is_defined_locally, FALSE); |
| 469 | |
| 470 | while ((sym = kxld_symtab_iterator_get_next(&iter))) { |
| 471 | symtabhdr->nsyms++; |
| 472 | symtabhdr->strsize += (uint32_t) (strlen(sym->name) + 1); |
| 473 | } |
| 474 | |
| 475 | if (is_32_bit) { |
| 476 | nlistsize = sizeof(struct nlist); |
| 477 | } else { |
| 478 | nlistsize = sizeof(struct nlist_64); |
| 479 | } |
| 480 | |
| 481 | symtabhdr->stroff = (uint32_t) (symtabhdr->symoff + |
| 482 | (symtabhdr->nsyms * nlistsize)); |
| 483 | require_action(symtabhdr->stroff + symtabhdr->strsize <= data_size, finish, |
| 484 | rval=KERN_FAILURE); |
| 485 | |
| 486 | /* Get pointers to the symbol and string tables */ |
| 487 | nl = buf + symtabhdr->symoff; |
| 488 | strtab = (char *) (buf + symtabhdr->stroff); |
| 489 | |
| 490 | /* Copy over the symbols */ |
| 491 | |
| 492 | kxld_symtab_iterator_reset(&iter); |
| 493 | while ((sym = kxld_symtab_iterator_get_next(&iter))) { |
| 494 | KXLD_3264_FUNC(is_32_bit, rval, |
| 495 | kxld_sym_export_macho_32, kxld_sym_export_macho_64, |
| 496 | sym, nl, strtab, &stroff, symtabhdr->strsize); |
| 497 | require_noerr(rval, finish); |
| 498 | |
| 499 | nl += nlistsize; |
| 500 | stroff += rval; |
| 501 | } |
| 502 | |
| 503 | /* Update the data offset */ |
| 504 | *data_offset += (symtabhdr->nsyms * nlistsize) + stroff; |
| 505 | |
| 506 | *data_offset = (*data_offset + 7) & ~7; |
| 507 | // at this point data_offset will be the offset just past the |
| 508 | // symbols and strings in the __LINKEDIT data |
| 509 | |
| 510 | |
| 511 | #if SPLIT_KEXTS_DEBUG |
| 512 | { |
| 513 | kxld_log(kKxldLogLinking, kKxldLogErr, |
| 514 | " %p to %p (size %lu) symtabhdr <%s>" , |
| 515 | (void *) symtabhdr, |
| 516 | (void *) ((u_char *)symtabhdr + sizeof(*symtabhdr)), |
| 517 | sizeof(*symtabhdr), |
| 518 | __func__); |
| 519 | |
| 520 | kxld_log(kKxldLogLinking, kKxldLogErr, |
| 521 | " symtabhdr %p cmdsize %u symoff %u nsyms %u stroff %u strsize %u <%s>" , |
| 522 | (void *) symtabhdr, |
| 523 | symtabhdr->cmdsize, |
| 524 | symtabhdr->symoff, |
| 525 | symtabhdr->nsyms, |
| 526 | symtabhdr->stroff, |
| 527 | symtabhdr->strsize, |
| 528 | __func__); |
| 529 | } |
| 530 | #endif |
| 531 | |
| 532 | rval = KERN_SUCCESS; |
| 533 | |
| 534 | finish: |
| 535 | return rval; |
| 536 | } |
| 537 | |
| 538 | /******************************************************************************* |
| 539 | *******************************************************************************/ |
| 540 | u_int |
| 541 | kxld_symtab_iterator_get_num_remaining(const KXLDSymtabIterator *iter) |
| 542 | { |
| 543 | u_int idx = 0; |
| 544 | u_int count = 0; |
| 545 | |
| 546 | check(iter); |
| 547 | |
| 548 | for (idx = iter->idx; idx < iter->symtab->syms.nitems; ++idx) { |
| 549 | count += iter->test(kxld_array_get_item(&iter->symtab->syms, idx)); |
| 550 | } |
| 551 | |
| 552 | return count; |
| 553 | } |
| 554 | |
| 555 | /******************************************************************************* |
| 556 | *******************************************************************************/ |
| 557 | kern_return_t |
| 558 | kxld_symtab_index_cxx_symbols_by_value(KXLDSymtab *symtab) |
| 559 | { |
| 560 | kern_return_t rval = KERN_FAILURE; |
| 561 | KXLDSymtabIterator iter; |
| 562 | KXLDSym *sym = NULL; |
| 563 | u_int nsyms = 0; |
| 564 | |
| 565 | check(symtab); |
| 566 | |
| 567 | if (symtab->cxx_index_initialized) { |
| 568 | rval = KERN_SUCCESS; |
| 569 | goto finish; |
| 570 | } |
| 571 | |
| 572 | /* Count the number of C++ symbols */ |
| 573 | kxld_symtab_iterator_init(&iter, symtab, sym_is_defined_cxx, FALSE); |
| 574 | nsyms = kxld_symtab_iterator_get_num_remaining(&iter); |
| 575 | |
| 576 | /* Create the dictionary */ |
| 577 | rval = kxld_dict_init(&symtab->cxx_index, kxld_dict_kxldaddr_hash, |
| 578 | kxld_dict_kxldaddr_cmp, nsyms); |
| 579 | require_noerr(rval, finish); |
| 580 | |
| 581 | /* Insert the non-stab symbols */ |
| 582 | while ((sym = kxld_symtab_iterator_get_next(&iter))) { |
| 583 | rval = kxld_dict_insert(&symtab->cxx_index, &sym->base_addr, sym); |
| 584 | require_noerr(rval, finish); |
| 585 | } |
| 586 | |
| 587 | |
| 588 | symtab->cxx_index_initialized = TRUE; |
| 589 | rval = KERN_SUCCESS; |
| 590 | finish: |
| 591 | return rval; |
| 592 | } |
| 593 | |
| 594 | /******************************************************************************* |
| 595 | *******************************************************************************/ |
| 596 | static boolean_t |
| 597 | sym_is_defined_cxx(const KXLDSym *sym) |
| 598 | { |
| 599 | return (kxld_sym_is_defined_locally(sym) && kxld_sym_is_cxx(sym)); |
| 600 | } |
| 601 | |
| 602 | /******************************************************************************* |
| 603 | *******************************************************************************/ |
| 604 | kern_return_t |
| 605 | kxld_symtab_index_symbols_by_name(KXLDSymtab *symtab) |
| 606 | { |
| 607 | kern_return_t rval = KERN_FAILURE; |
| 608 | KXLDSymtabIterator iter; |
| 609 | KXLDSym *sym = NULL; |
| 610 | u_int nsyms = 0; |
| 611 | |
| 612 | check(symtab); |
| 613 | |
| 614 | if (symtab->name_index_initialized) { |
| 615 | rval = KERN_SUCCESS; |
| 616 | goto finish; |
| 617 | } |
| 618 | |
| 619 | /* Count the number of symbols we need to index by name */ |
| 620 | kxld_symtab_iterator_init(&iter, symtab, sym_is_name_indexed, FALSE); |
| 621 | nsyms = kxld_symtab_iterator_get_num_remaining(&iter); |
| 622 | |
| 623 | /* Create the dictionary */ |
| 624 | rval = kxld_dict_init(&symtab->name_index, kxld_dict_string_hash, |
| 625 | kxld_dict_string_cmp, nsyms); |
| 626 | require_noerr(rval, finish); |
| 627 | |
| 628 | /* Insert the non-stab symbols */ |
| 629 | while ((sym = kxld_symtab_iterator_get_next(&iter))) { |
| 630 | rval = kxld_dict_insert(&symtab->name_index, sym->name, sym); |
| 631 | require_noerr(rval, finish); |
| 632 | } |
| 633 | |
| 634 | symtab->name_index_initialized = TRUE; |
| 635 | rval = KERN_SUCCESS; |
| 636 | finish: |
| 637 | |
| 638 | return rval; |
| 639 | } |
| 640 | /******************************************************************************* |
| 641 | *******************************************************************************/ |
| 642 | static boolean_t |
| 643 | sym_is_name_indexed(const KXLDSym *sym) |
| 644 | { |
| 645 | return (kxld_sym_is_defined_locally(sym) && !kxld_sym_is_stab(sym)); |
| 646 | } |
| 647 | |
| 648 | /******************************************************************************* |
| 649 | *******************************************************************************/ |
| 650 | kern_return_t |
| 651 | kxld_symtab_relocate(KXLDSymtab *symtab, const KXLDArray *sectarray) |
| 652 | { |
| 653 | kern_return_t rval = KERN_FAILURE; |
| 654 | KXLDSymtabIterator iter; |
| 655 | KXLDSym *sym = NULL; |
| 656 | const KXLDSect *sect = NULL; |
| 657 | |
| 658 | check(symtab); |
| 659 | check(sectarray); |
| 660 | |
| 661 | kxld_symtab_iterator_init(&iter, symtab, kxld_sym_is_section, FALSE); |
| 662 | |
| 663 | while ((sym = kxld_symtab_iterator_get_next(&iter))) { |
| 664 | sect = kxld_array_get_item(sectarray, sym->sectnum); |
| 665 | require_action(sect, finish, rval=KERN_FAILURE); |
| 666 | kxld_sym_relocate(sym, sect); |
| 667 | } |
| 668 | |
| 669 | rval = KERN_SUCCESS; |
| 670 | |
| 671 | finish: |
| 672 | |
| 673 | return rval; |
| 674 | } |
| 675 | |
| 676 | /******************************************************************************* |
| 677 | * This extends the symbol table and initializes the new symbol. We insert the |
| 678 | * symbol into the name index, but we don't bother with the c++ value index |
| 679 | * because it is based on the base_addr of the symbol, and the base_addr of |
| 680 | * all synthesized symbols will be 0. |
| 681 | *******************************************************************************/ |
| 682 | kern_return_t |
| 683 | kxld_symtab_add_symbol(KXLDSymtab *symtab, char *name, kxld_addr_t link_addr, |
| 684 | KXLDSym **symout) |
| 685 | { |
| 686 | kern_return_t rval = KERN_FAILURE; |
| 687 | KXLDSym *sym = NULL; |
| 688 | u_int symindex = symtab->syms.nitems; |
| 689 | |
| 690 | rval = kxld_array_resize(&symtab->syms, symindex + 1); |
| 691 | require_noerr(rval, finish); |
| 692 | |
| 693 | sym = kxld_array_get_item(&symtab->syms, symindex); |
| 694 | kxld_sym_init_absolute(sym, name, link_addr); |
| 695 | |
| 696 | rval = kxld_dict_insert(&symtab->name_index, sym->name, sym); |
| 697 | require_noerr(rval, finish); |
| 698 | |
| 699 | rval = KERN_SUCCESS; |
| 700 | *symout = sym; |
| 701 | |
| 702 | finish: |
| 703 | return rval; |
| 704 | } |
| 705 | |
| 706 | /******************************************************************************* |
| 707 | *******************************************************************************/ |
| 708 | KXLDSym * |
| 709 | kxld_symtab_iterator_get_next(KXLDSymtabIterator *iter) |
| 710 | { |
| 711 | KXLDSym *sym = NULL; |
| 712 | KXLDSym *tmp = NULL; |
| 713 | boolean_t cmp = FALSE; |
| 714 | |
| 715 | check(iter); |
| 716 | |
| 717 | for (; iter->idx < iter->symtab->syms.nitems; ++iter->idx) { |
| 718 | tmp = kxld_array_get_item(&iter->symtab->syms, iter->idx); |
| 719 | cmp = iter->test(tmp); |
| 720 | if (iter->negate) cmp = !cmp; |
| 721 | |
| 722 | if (cmp) { |
| 723 | sym = tmp; |
| 724 | ++iter->idx; |
| 725 | break; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return sym; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | /******************************************************************************* |
| 734 | *******************************************************************************/ |
| 735 | void |
| 736 | kxld_symtab_iterator_reset(KXLDSymtabIterator *iter) |
| 737 | { |
| 738 | check(iter); |
| 739 | iter->idx = 0; |
| 740 | } |
| 741 | |