• Home
  • Raw
  • Download

Lines Matching +full:dlclose +full:- +full:skip

79 #define MAXP2(a,b) (-(-(a)&-(b)))
80 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
83 size_t f = bloomwords[gho & (ght[2] - 1)]; \
88 #define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
100 #define INVALID_FD_INHIBIT_FURTHER_SEARCH (-2)
124 /* Used for dlclose */
299 ssize_t sz = datasz - *off; in parse_prot()
305 if (pr->pr_datasz > sz) in parse_prot()
307 ret = parse_elf_property(pr->pr_type, data + o); in parse_prot()
317 ssize_t sz = ph->p_filesz > BUF_MAX ? BUF_MAX : ph->p_filesz; in parse_extra_prot_fd()
318 ssize_t len = pread(fd, gnu_data.data, sz, ph->p_offset); in parse_extra_prot_fd()
325 off_gp = (off_gp + ELF_GNU_PROPERTY_ALIGN - 1) & (-(ELF_GNU_PROPERTY_ALIGN)); in parse_extra_prot_fd()
353 app->namespace = default_ns; in init_default_namespace()
372 ns_set_separated(ns, conf->get_separated(ns->ns_name)); in set_ns_attrs()
374 lib_paths = conf->get_lib_paths(ns->ns_name); in set_ns_attrs()
377 asan_lib_paths = conf->get_asan_lib_paths(ns->ns_name); in set_ns_attrs()
380 permitted_paths = conf->get_permitted_paths(ns->ns_name); in set_ns_attrs()
383 asan_permitted_paths = conf->get_asan_permitted_paths(ns->ns_name); in set_ns_attrs()
386 allowed_libs = conf->get_allowed_libs(ns->ns_name); in set_ns_attrs()
397 ns->ns_name, ns->separated, ns->lib_paths, ns->asan_lib_paths, permitted_paths, in set_ns_attrs()
407 strlist *inherits = conf->get_inherits(ns->ns_name); in set_ns_inherits()
409 for (size_t i = 0; i < inherits->num; i++) { in set_ns_inherits()
410 ns_t *inherited_ns = find_ns_by_name(inherits->strs[i]); in set_ns_inherits()
412 char *shared_libs = conf->get_inherit_shared_libs(ns->ns_name, inherited_ns->ns_name); in set_ns_inherits()
419 inherited_ns->ns_name, inherited_ns->separated, inherited_ns->lib_paths, in set_ns_inherits()
420 inherited_ns->asan_lib_paths); in set_ns_inherits()
433 strcpy(app_path, app->name); in init_namespace()
445 char file_path[sizeof "/etc/ld-musl-namespace-" + sizeof (LDSO_ARCH) + sizeof ".ini" + 1] = {0}; in init_namespace()
446 (void)snprintf(file_path, sizeof file_path, "/etc/ld-musl-namespace-%s.ini", LDSO_ARCH); in init_namespace()
450 int ret = conf->parse(file_path, app_path); in init_namespace()
476 strlist *s_ns = conf->get_namespaces(); in init_namespace()
478 for (size_t i = 0; i < s_ns->num; i++) { in init_namespace()
480 ns_set_name(ns, s_ns->strs[i]); in init_namespace()
489 for (size_t i = 0; i < nsl->num; i++) { in init_namespace()
490 set_ns_inherits(nsl->nss[i], conf); in init_namespace()
502 if (!p->loadmap) return p->base + v; in laddr()
503 for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++); in laddr()
504 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr); in laddr()
510 if (!p->loadmap) return p->base + v; in laddr_pg()
512 size_t a = p->loadmap->segs[j].p_vaddr; in laddr_pg()
513 size_t b = a + p->loadmap->segs[j].p_memsz; in laddr_pg()
514 a &= -pgsz; in laddr_pg()
515 b += pgsz-1; in laddr_pg()
516 b &= -pgsz; in laddr_pg()
517 if (v-a<b-a) break; in laddr_pg()
519 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr); in laddr_pg()
528 laddr(p, v), (p)->got }))
530 #define laddr(p, v) (void *)((p)->base + (v))
539 list->dso_list = NULL; in create_notify_dso_list()
540 list->capacity = 0; in create_notify_dso_list()
541 list->length = 0; in create_notify_dso_list()
550 if (list->dso_list) { in free_notify_dso_list()
551 free(list->dso_list); in free_notify_dso_list()
552 list->dso_list = NULL; in free_notify_dso_list()
554 list->capacity = 0; in free_notify_dso_list()
555 list->length = 0; in free_notify_dso_list()
562 if (list->length >= list->capacity) { in append_notify_dso()
564 …(struct dso **)realloc(list->dso_list, sizeof(struct dso *) * (list->capacity + NOTIFY_BASE_CAPACI… in append_notify_dso()
566 LD_LOGW("realloc failed for append notify for so %{public}s, errno %{public}d", p->name, errno); in append_notify_dso()
569 list->dso_list = realloced; in append_notify_dso()
570 list->capacity += NOTIFY_BASE_CAPACITY; in append_notify_dso()
572 list->dso_list[list->length++] = p; in append_notify_dso()
578 for (size_t index = 0; index < list->length; index++) { in iterate_notify_dso()
579 p = list->dso_list[index]; in iterate_notify_dso()
580 callback(p->map, p->map_len, p->name); in iterate_notify_dso()
621 …* Only async-signal-safe functions can be called safely in callback. OtherWise, the behavio…
626 * @retval -1 is returned on failure, and errno is set:
634 return -1; in register_ldso_func_for_add_dso()
639 for (p = head; p; p = p->next) { in register_ldso_func_for_add_dso()
640 // Skip vdso map. in register_ldso_func_for_add_dso()
641 if (!p->map) { in register_ldso_func_for_add_dso()
644 callback(p->map, p->map_len, p->name); in register_ldso_func_for_add_dso()
657 for (; v[0]; v+=2) if (v[0]-1<cnt-1) { in decode_vec()
680 if ((verdef->vd_ndx & 0x7fff) == vsym) { in check_vna_hash()
681 if (vna_hash == verdef->vd_hash) { in check_vna_hash()
689 if (verdef->vd_next == 0) { in check_vna_hash()
692 verdef = (Verdef *)((char *)verdef + verdef->vd_next); in check_vna_hash()
706 if (strlen(verinfo->v) == 0) { in check_verinfo()
709 LD_LOGD("check_verinfo versym or def is null and verinfo->v exist, s:%{public}s v:%{public}s.", in check_verinfo()
710 verinfo->s, verinfo->v); in check_verinfo()
718 if (verinfo->use_vna_hash) { in check_verinfo()
720 return check_vna_hash(def, vsym, verinfo->vna_hash); in check_verinfo()
725 if (strlen(verinfo->v) == 0) { in check_verinfo()
729 LD_LOGD("check_verinfo not default version. vsym:%{public}d s:%{public}s", vsym, verinfo->s); in check_verinfo()
737 if (!(def->vd_flags & VER_FLG_BASE) && (def->vd_ndx & 0x7fff) == vsym) { in check_verinfo()
740 if (def->vd_next == 0) { in check_verinfo()
743 def = (Verdef *)((char *)def + def->vd_next); in check_verinfo()
746 Verdaux *aux = (Verdaux *)((char *)def + def->vd_aux); in check_verinfo()
748 int ret = !strcmp(verinfo->v, strings + aux->vda_name); in check_verinfo()
752 verinfo->s, verinfo->v, vsym, strings + aux->vda_name); in check_verinfo()
768 s_info_p.sym_l = (char *)s - s0; in sysv_hash()
780 s_info_p.sym_l = (char *)s - s0; in gnu_hash()
788 Sym *syms = dso->syms; in sysv_lookup()
789 Elf_Symndx *hashtab = dso->hashtab; in sysv_lookup()
790 char *strings = dso->strings; in sysv_lookup()
792 if ((!dso->versym || (dso->versym[i] & 0x7fff) >= 0) in sysv_lookup()
793 && (!memcmp(verinfo->s, strings+syms[i].st_name, s_info_p.sym_l))) { in sysv_lookup()
794 if (!check_verinfo(dso->verdef, dso->versym, i, verinfo, dso->strings)) { in sysv_lookup()
804 dso->name, verinfo->s, verinfo->v, verinfo->use_vna_hash, verinfo->vna_hash); in sysv_lookup()
816 …D("gnu_lookup symbol not found (bloom filter), so:%{public}s s:%{public}s", dso->name, verinfo->s); in gnu_lookup()
820 uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]); in gnu_lookup()
824 if ((h1 == (h2|1)) && (!dso->versym || (dso->versym[i] & 0x7fff) >= 0) in gnu_lookup()
825 && !memcmp(verinfo->s, dso->strings + dso->syms[i].st_name, s_info_p.sym_l)) { in gnu_lookup()
826 if (!check_verinfo(dso->verdef, dso->versym, i, verinfo, dso->strings)) { in gnu_lookup()
830 return dso->syms+i; in gnu_lookup()
838 dso->name, verinfo->s, verinfo->v, verinfo->use_vna_hash, verinfo->vna_hash); in gnu_lookup()
844 if (!dso || !dso->namespace || !ns) { in check_sym_accessible()
848 if (dso->namespace == ns) { in check_sym_accessible()
851 for (int i = 0; i < dso->parents_count; i++) { in check_sym_accessible()
852 if (dso->parents[i]->namespace == ns) { in check_sym_accessible()
857 …"check_sym_accessible dso name [%{public}s] ns_name [%{public}s] not accessible!", dso->name, ns->… in check_sym_accessible()
863 if (dso->namespace == ns) { in is_dso_accessible()
866 for (int i = 0; i < dso->parents_count; i++) { in is_dso_accessible()
867 if (dso->parents[i]->namespace == ns) { in is_dso_accessible()
872 …"check_sym_accessible dso name [%{public}s] ns_name [%{public}s] not accessible!", dso->name, ns->… in is_dso_accessible()
878 int index = -1; in find_dso_parent()
879 for (int i = 0; i < p->parents_count; i++) { in find_dso_parent()
880 if (p->parents[i] == target) { in find_dso_parent()
891 if (index != -1) { in add_dso_parent()
894 if (p->parents_count + 1 > p->parents_capacity) { in add_dso_parent()
895 if (p->parents_capacity == 0) { in add_dso_parent()
896 p->parents = (struct dso **)malloc(sizeof(struct dso *) * PARENTS_BASE_CAPACITY); in add_dso_parent()
897 if (!p->parents) { in add_dso_parent()
900 p->parents_capacity = PARENTS_BASE_CAPACITY; in add_dso_parent()
903 p->parents, sizeof(struct dso *) * (p->parents_capacity + PARENTS_BASE_CAPACITY)); in add_dso_parent()
907 p->parents = realloced; in add_dso_parent()
908 p->parents_capacity += PARENTS_BASE_CAPACITY; in add_dso_parent()
911 p->parents[p->parents_count] = parent; in add_dso_parent()
912 p->parents_count++; in add_dso_parent()
918 if (index == -1) { in remove_dso_parent()
923 p->parents[i] = p->parents[i]; in remove_dso_parent()
925 for (i = index; i < p->parents_count - 1; i++) { in remove_dso_parent()
926 p->parents[i] = p->parents[i + 1]; in remove_dso_parent()
928 p->parents_count--; in remove_dso_parent()
933 if (p->reloc_can_search_dso_count + 1 > p->reloc_can_search_dso_capacity) { in add_reloc_can_search_dso()
934 if (p->reloc_can_search_dso_capacity == 0) { in add_reloc_can_search_dso()
935 p->reloc_can_search_dso_list = in add_reloc_can_search_dso()
937 if (!p->reloc_can_search_dso_list) { in add_reloc_can_search_dso()
940 p->reloc_can_search_dso_capacity = RELOC_CAN_SEARCH_DSO_BASE_CAPACITY; in add_reloc_can_search_dso()
943 p->reloc_can_search_dso_list, in add_reloc_can_search_dso()
944 sizeof(struct dso *) * (p->reloc_can_search_dso_capacity + RELOC_CAN_SEARCH_DSO_BASE_CAPACITY)); in add_reloc_can_search_dso()
948 p->reloc_can_search_dso_list = realloced; in add_reloc_can_search_dso()
949 p->reloc_can_search_dso_capacity += RELOC_CAN_SEARCH_DSO_BASE_CAPACITY; in add_reloc_can_search_dso()
952 p->reloc_can_search_dso_list[p->reloc_can_search_dso_count] = can_search_so; in add_reloc_can_search_dso()
953 p->reloc_can_search_dso_count++; in add_reloc_can_search_dso()
958 if (p->reloc_can_search_dso_list) { in free_reloc_can_search_dso()
959 free(p->reloc_can_search_dso_list); in free_reloc_can_search_dso()
960 p->reloc_can_search_dso_list = NULL; in free_reloc_can_search_dso()
961 p->reloc_can_search_dso_count = 0; in free_reloc_can_search_dso()
962 p->reloc_can_search_dso_capacity = 0; in free_reloc_can_search_dso()
967 * - The is_global flag of the so is true which means accessible by default.
969 * - We only check whether ns is accessible for the so if is_reloc_head_so_dep is true.
976 for (; p; p = p->syms_next) { in add_can_search_so_list_in_dso()
977 if (p->is_global) { in add_can_search_so_list_in_dso()
982 if (p->is_reloc_head_so_dep) { in add_can_search_so_list_in_dso()
983 if (dso_relocating->namespace && check_sym_accessible(p, dso_relocating->namespace)) { in add_can_search_so_list_in_dso()
1016 if ((ght = dso->ghashtab)) { in find_sym_impl()
1018 size_t f = bloomwords[gho & (ght[2] - 1)]; in find_sym_impl()
1029 s_info_s = sysv_hash(verinfo->s); in find_sym_impl()
1037 if (!sym->st_shndx) in find_sym_impl()
1038 if (need_def || (sym->st_info & 0xf) == STT_TLS || ARCH_SYM_REJECT_UND(sym)) in find_sym_impl()
1041 if (!sym->st_value) in find_sym_impl()
1042 if ((sym->st_info & 0xf) != STT_TLS) in find_sym_impl()
1045 if (!(1 << (sym->st_info & 0xf) & OK_TYPES)) in find_sym_impl()
1048 if (!(1 << (sym->st_info >> 4) & OK_BINDS)) in find_sym_impl()
1058 struct sym_info_pair s_info_g = gnu_hash(verinfo->s); in find_sym2()
1063 struct dso **deps = use_deps ? dso->deps : 0; in find_sym2()
1064 for (; dso; dso=use_deps ? *deps++ : dso->syms_next) { in find_sym2()
1067 if (!dso->is_preload && ns && !check_sym_accessible(dso, ns)) { in find_sym2()
1070 if ((ght = dso->ghashtab)) { in find_sym2()
1074 if (!s_info_s.sym_h) s_info_s = sysv_hash(verinfo->s); in find_sym2()
1079 if (!sym->st_shndx) in find_sym2()
1080 if (need_def || (sym->st_info&0xf) == STT_TLS in find_sym2()
1083 if (!sym->st_value) in find_sym2()
1084 if ((sym->st_info&0xf) != STT_TLS) in find_sym2()
1086 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue; in find_sym2()
1087 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue; in find_sym2()
1097 struct sym_info_pair s_info_g = gnu_hash(verinfo->s); in find_sym_by_deps()
1102 struct dso **deps = dso->deps; in find_sym_by_deps()
1108 if ((ght = dso->ghashtab)) { in find_sym_by_deps()
1112 if (!s_info_s.sym_h) s_info_s = sysv_hash(verinfo->s); in find_sym_by_deps()
1117 if (!sym->st_shndx) in find_sym_by_deps()
1118 if (need_def || (sym->st_info&0xf) == STT_TLS in find_sym_by_deps()
1121 if (!sym->st_value) in find_sym_by_deps()
1122 if ((sym->st_info&0xf) != STT_TLS) in find_sym_by_deps()
1124 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue; in find_sym_by_deps()
1125 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue; in find_sym_by_deps()
1136 struct sym_info_pair s_info_g = gnu_hash(verinfo->s); in find_sym_by_saved_so_list()
1141 // skip head dso. in find_sym_by_saved_so_list()
1144 for (int i = start_search_index; i < dso_relocating->reloc_can_search_dso_count; i++) { in find_sym_by_saved_so_list()
1145 dso_searching = dso_relocating->reloc_can_search_dso_list[i]; in find_sym_by_saved_so_list()
1147 if ((ght = dso_searching->ghashtab)) { in find_sym_by_saved_so_list()
1151 if (!s_info_s.sym_h) s_info_s = sysv_hash(verinfo->s); in find_sym_by_saved_so_list()
1155 if (!sym->st_shndx) in find_sym_by_saved_so_list()
1156 if (need_def || (sym->st_info&0xf) == STT_TLS in find_sym_by_saved_so_list()
1159 if (!sym->st_value) in find_sym_by_saved_so_list()
1160 if ((sym->st_info&0xf) != STT_TLS) in find_sym_by_saved_so_list()
1162 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue; in find_sym_by_saved_so_list()
1163 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue; in find_sym_by_saved_so_list()
1179 if (!dso->versym || !dso->verneed) { in get_vna_hash()
1183 uint16_t vsym = dso->versym[sym_index]; in get_vna_hash()
1189 Verneed *verneed = dso->verneed; in get_vna_hash()
1194 vernaux = (Vernaux *)((char *)verneed + verneed->vn_aux); in get_vna_hash()
1196 for (size_t cnt = 0; cnt < verneed->vn_cnt; cnt++) { in get_vna_hash()
1197 if ((vernaux->vna_other & 0x7fff) == vsym) { in get_vna_hash()
1199 *vna_hash = vernaux->vna_hash; in get_vna_hash()
1203 vernaux = (Vernaux *)((char *)vernaux + vernaux->vna_next); in get_vna_hash()
1210 if (verneed->vn_next == 0) { in get_vna_hash()
1214 verneed = (Verneed *)((char *)verneed + verneed->vn_next); in get_vna_hash()
1221 char *strings = dso->strings; in get_verinfo()
1223 int16_t vsym = dso->versym[sym_index]; in get_verinfo()
1224 Verdef *verdef = dso->verdef; in get_verinfo()
1234 if (!(verdef->vd_flags & VER_FLG_BASE) && (verdef->vd_ndx & 0x7fff) == vsym) { in get_verinfo()
1238 if (verdef->vd_next == 0) { in get_verinfo()
1241 verdef = (Verdef *)((char *)verdef + verdef->vd_next); in get_verinfo()
1244 Verdaux *aux = (Verdaux *)((char *)verdef + verdef->vd_aux); in get_verinfo()
1245 if (aux && aux->vda_name && strings && (dso->strings + aux->vda_name)) { in get_verinfo()
1246 vinfo->v = dso->strings + aux->vda_name; in get_verinfo()
1292 unsigned char *base = dso->base; in do_relocs()
1293 Sym *syms = dso->syms; in do_relocs()
1294 char *strings = dso->strings; in do_relocs()
1314 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) { in do_relocs()
1315 if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue; in do_relocs()
1338 name = strings + sym->st_name; in do_relocs()
1339 ctx = type==REL_COPY ? head->syms_next : head; in do_relocs()
1343 if (!vinfo.use_vna_hash && dso->versym && (dso->versym[sym_index] & 0x7fff) >= 0) { in do_relocs()
1346 if (dso->cache_sym_index == sym_index) { in do_relocs()
1347 def = (struct symdef){ .dso = dso->cache_dso, .sym = dso->cache_sym }; in do_relocs()
1349 def = (sym->st_info>>4) == STB_LOCAL in do_relocs()
1352 : find_sym2(ctx, &vinfo, type==REL_PLT, 0, dso->namespace); in do_relocs()
1353 dso->cache_sym_index = sym_index; in do_relocs()
1354 dso->cache_dso = def.dso; in do_relocs()
1355 dso->cache_sym = def.sym; in do_relocs()
1358 if (!def.sym && (sym->st_shndx != SHN_UNDEF in do_relocs()
1359 || sym->st_info>>4 != STB_WEAK)) { in do_relocs()
1362 * dls3 stage, so we will skip this step. */ in do_relocs()
1367 if (dso->lazy && (type==REL_PLT || type==REL_GOT)) { in do_relocs()
1368 dso->lazy[3*dso->lazy_cnt+0] = rel[0]; in do_relocs()
1369 dso->lazy[3*dso->lazy_cnt+1] = rel[1]; in do_relocs()
1370 dso->lazy[3*dso->lazy_cnt+2] = addend; in do_relocs()
1371 dso->lazy_cnt++; in do_relocs()
1376 dso->name, name, vinfo.use_vna_hash, vinfo.vna_hash); in do_relocs()
1378 dso->name, name); in do_relocs()
1388 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0; in do_relocs()
1389 tls_val = def.sym ? def.sym->st_value : 0; in do_relocs()
1392 && def.dso->tls_id > static_tls_cnt) { in do_relocs()
1393 error("Error relocating %s: %s: initial-exec TLS " in do_relocs()
1395 dso->name, name, def.dso->name); in do_relocs()
1401 addend -= (size_t)reloc_addr; in do_relocs()
1418 memcpy(reloc_addr, (void *)sym_val, sym->st_size); in do_relocs()
1422 - (size_t)reloc_addr; in do_relocs()
1425 *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs in do_relocs()
1426 + (def.sym - def.dso->syms)) : 0; in do_relocs()
1429 if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val; in do_relocs()
1431 reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0; in do_relocs()
1434 *reloc_addr = def.dso->tls_id; in do_relocs()
1437 *reloc_addr = tls_val + addend - DTP_OFFSET; in do_relocs()
1441 *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend; in do_relocs()
1445 *reloc_addr = tls_val - def.dso->tls.offset + addend; in do_relocs()
1448 *reloc_addr = def.dso->tls.offset - tls_val + addend; in do_relocs()
1460 if (def.dso->tls_id > static_tls_cnt) { in do_relocs()
1465 dso->name, sym ? name : "(local)" ); in do_relocs()
1468 new->next = dso->td_index; in do_relocs()
1469 dso->td_index = new; in do_relocs()
1470 new->args[0] = def.dso->tls_id; in do_relocs()
1471 new->args[1] = tls_val + addend - DTP_OFFSET; in do_relocs()
1477 reloc_addr[1] = tls_val + def.dso->tls.offset in do_relocs()
1480 reloc_addr[1] = tls_val - def.dso->tls.offset in do_relocs()
1484 /* Some archs (32-bit ARM at least) invert the order of in do_relocs()
1494 dso->name, type); in do_relocs()
1506 next = p->lazy_next; in redo_lazy_relocs()
1507 size_t size = p->lazy_cnt*3*sizeof(size_t); in redo_lazy_relocs()
1508 p->lazy_cnt = 0; in redo_lazy_relocs()
1509 do_relocs(p, p->lazy, size, 3); in redo_lazy_relocs()
1510 if (p->lazy_cnt) { in redo_lazy_relocs()
1511 p->lazy_next = lazy_head; in redo_lazy_relocs()
1514 free(p->lazy); in redo_lazy_relocs()
1515 p->lazy = 0; in redo_lazy_relocs()
1516 p->lazy_next = 0; in redo_lazy_relocs()
1528 if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end; in reclaim()
1529 if (end >= dso->relro_start && end < dso->relro_end) end = dso->relro_start; in reclaim()
1532 __malloc_donate(base, base+(end-start)); in reclaim()
1537 Phdr *ph = dso->phdr; in reclaim_gaps()
1538 size_t phcnt = dso->phnum; in reclaim_gaps()
1540 for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) { in reclaim_gaps()
1541 if (ph->p_type!=PT_LOAD) continue; in reclaim_gaps()
1542 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue; in reclaim_gaps()
1543 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr); in reclaim_gaps()
1544 reclaim(dso, ph->p_vaddr+ph->p_memsz, in reclaim_gaps()
1545 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE); in reclaim_gaps()
1567 for (q=p; n; q+=r, off+=r, n-=r) { in mmap_fixed()
1580 if (dso->loadmap) { in unmap_library()
1582 for (i=0; i<dso->loadmap->nsegs; i++) { in unmap_library()
1583 if (!dso->loadmap->segs[i].p_memsz) in unmap_library()
1586 munmap((void *)dso->loadmap->segs[i].addr, in unmap_library()
1587 dso->loadmap->segs[i].p_memsz); in unmap_library()
1589 (void)mprotect((void *)dso->loadmap->segs[i].addr, in unmap_library()
1590 dso->loadmap->segs[i].p_memsz, PROT_NONE); in unmap_library()
1593 free(dso->loadmap); in unmap_library()
1594 } else if (dso->map && dso->map_len) { in unmap_library()
1596 munmap(dso->map, dso->map_len); in unmap_library()
1598 mprotect(dso->map, dso->map_len, PROT_NONE); in unmap_library()
1642 int fd = -1; in get_transparent_hugepages_supported()
1651 read_size = read(fd, buf, HUGEPAGES_SUPPORTED_STR_SIZE - 1); in get_transparent_hugepages_supported()
1655 buf[HUGEPAGES_SUPPORTED_STR_SIZE - 1] = '\0'; in get_transparent_hugepages_supported()
1675 if ((phdr->p_type != PT_LOAD) || ((phdr->p_align & (phdr->p_align - 1)) != 0)) in phdr_table_get_maxinum_alignment()
1678 if (phdr->p_align > maxinum_alignment) in phdr_table_get_maxinum_alignment()
1679 maxinum_alignment = phdr->p_align; in phdr_table_get_maxinum_alignment()
1706 pac_items[index].base = dso->base; in add_pac_info()
1709 uint64_t begin_check = (((size_t)(dso->map) >> 12) & addr_mask) << 32; in add_pac_info()
1710 uint64_t end_check = (((size_t)(dso->map) + dso->map_len) >> 12) & addr_mask; in add_pac_info()
1713 pac_items[index].modifier_begin = (size_t)(dso->base) + dso->modifier_begin; in add_pac_info()
1714 pac_items[index].modifier_end = (size_t)(dso->base) + dso->modifier_end; in add_pac_info()
1715 dso->item = &pac_items[index]; in add_pac_info()
1725 if (dso->item == NULL) { in clear_pac_info()
1728 dso->item->valid = 0; in clear_pac_info()
1729 atomic_store(&dso->item->pc_check, 0); in clear_pac_info()
1730 dso->item->modifier_begin = 0; in clear_pac_info()
1731 dso->item->modifier_end = 0; in clear_pac_info()
1732 dso->item = NULL; in clear_pac_info()
1766 if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC)) in map_library()
1768 phsize = eh->e_phentsize * eh->e_phnum; in map_library()
1769 if (phsize > sizeof buf - sizeof *eh) { in map_library()
1772 l = pread(fd, allocated_buf, phsize, eh->e_phoff); in map_library()
1776 } else if (eh->e_phoff + phsize > l) { in map_library()
1777 l = pread(fd, buf+1, phsize, eh->e_phoff); in map_library()
1782 ph = ph0 = (void *)((char *)buf + eh->e_phoff); in map_library()
1784 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { in map_library()
1785 if (ph->p_type == PT_DYNAMIC) { in map_library()
1786 dyn = ph->p_vaddr; in map_library()
1787 } else if (ph->p_type == PT_TLS) { in map_library()
1788 tls_image = ph->p_vaddr; in map_library()
1789 dso->tls.align = ph->p_align; in map_library()
1790 dso->tls.len = ph->p_filesz; in map_library()
1791 dso->tls.size = ph->p_memsz; in map_library()
1792 } else if (ph->p_type == PT_GNU_RELRO) { in map_library()
1793 dso->relro_start = ph->p_vaddr & -PAGE_SIZE; in map_library()
1794 dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; in map_library()
1795 } else if (ph->p_type == PT_GNU_STACK) { in map_library()
1796 if (!runtime && ph->p_memsz > __default_stacksize) { in map_library()
1798 ph->p_memsz < DEFAULT_STACK_MAX ? in map_library()
1799 ph->p_memsz : DEFAULT_STACK_MAX; in map_library()
1801 } else if (ph->p_type == PT_OHOS_CFI_MODIFIER) { in map_library()
1802 dso->modifier_begin = ph->p_vaddr; in map_library()
1803 dso->modifier_end = ph->p_vaddr + ph->p_memsz; in map_library()
1808 if (ph->p_type == PT_GNU_PROPERTY || ph->p_type == PT_NOTE) { in map_library()
1812 if (ph->p_type != PT_LOAD) continue; in map_library()
1814 if (ph->p_vaddr < addr_min) { in map_library()
1815 addr_min = ph->p_vaddr; in map_library()
1816 off_start = ph->p_offset; in map_library()
1817 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | in map_library()
1818 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | in map_library()
1819 ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); in map_library()
1821 if (ph->p_flags & PF_X) { in map_library()
1826 if (ph->p_vaddr+ph->p_memsz > addr_max) { in map_library()
1827 addr_max = ph->p_vaddr+ph->p_memsz; in map_library()
1831 if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) { in map_library()
1832 dso->loadmap = calloc(1, sizeof *dso->loadmap in map_library()
1833 + nsegs * sizeof *dso->loadmap->segs); in map_library()
1834 if (!dso->loadmap) goto error; in map_library()
1835 dso->loadmap->nsegs = nsegs; in map_library()
1836 for (ph=ph0, i=0; i<nsegs; ph=(void *)((char *)ph+eh->e_phentsize)) { in map_library()
1837 if (ph->p_type != PT_LOAD) continue; in map_library()
1838 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | in map_library()
1839 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | in map_library()
1840 ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); in map_library()
1842 if (ph->p_flags & PF_X) { in map_library()
1846 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1), in map_library()
1848 fd, ph->p_offset & -PAGE_SIZE); in map_library()
1853 dso->loadmap->segs[i].addr = (size_t)map + in map_library()
1854 (ph->p_vaddr & PAGE_SIZE-1); in map_library()
1855 dso->loadmap->segs[i].p_vaddr = ph->p_vaddr; in map_library()
1856 dso->loadmap->segs[i].p_memsz = ph->p_memsz; in map_library()
1859 size_t brk = (ph->p_vaddr & PAGE_SIZE-1) in map_library()
1860 + ph->p_filesz; in map_library()
1861 size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE; in map_library()
1862 size_t pgend = brk + ph->p_memsz - ph->p_filesz in map_library()
1863 + PAGE_SIZE-1 & -PAGE_SIZE; in map_library()
1865 pgend-pgbrk, prot, in map_library()
1867 -1, off_start) == MAP_FAILED) in map_library()
1869 memset(map + brk, 0, pgbrk-brk); in map_library()
1872 map = (void *)dso->loadmap->segs[0].addr; in map_library()
1876 addr_max += PAGE_SIZE-1; in map_library()
1877 addr_max &= -PAGE_SIZE; in map_library()
1878 addr_min &= -PAGE_SIZE; in map_library()
1879 off_start &= -PAGE_SIZE; in map_library()
1880 map_len = addr_max - addr_min + off_start; in map_library()
1885 size_t maxinum_alignment = phdr_table_get_maxinum_alignment(ph0, eh->e_phnum); in map_library()
1891 if (map_len > reserved_params->reserved_size) { in map_library()
1892 if (reserved_params->must_use_reserved) { in map_library()
1896 start_addr = ((size_t)reserved_params->start_addr - 1 + PAGE_SIZE) & -PAGE_SIZE; in map_library()
1905 size_t tmp_map_len = ALIGN(map_len, mapping_align) + mapping_align - PAGE_SIZE; in map_library()
1913 …void *)start_addr, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) in map_library()
1918 if (reserved_params && map_len < reserved_params->reserved_size) { in map_library()
1919 reserved_params->reserved_size -= (map_len + (start_addr - (size_t)reserved_params->start_addr)); in map_library()
1920 reserved_params->start_addr = (void *)((uint8_t *)map + map_len); in map_library()
1925 …ed char *temp_map = mmap((void *)NULL, tmp_map_len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); in map_library()
1933 ? mmap(real_map, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) in map_library()
1942 * |--------------------------tmp_map_len--------------------------| in map_library()
1944 * |---unused_part_1---|---------map_len-------|---unused_part_2---| in map_library()
1948 size_t unused_part_1 = real_map - temp_map; in map_library()
1949 size_t unused_part_2 = temp_map_end - (real_map + map_len); in map_library()
1952 if (res1 == -1) { in map_library()
1959 if (res2 == -1) { in map_library()
1964 dso->map = map; in map_library()
1965 dso->map_len = map_len; in map_library()
1968 if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) { in map_library()
1972 base = map - addr_min; in map_library()
1973 dso->phdr = 0; in map_library()
1974 dso->phnum = 0; in map_library()
1975 for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) { in map_library()
1976 if (ph->p_type == PT_OHOS_RANDOMDATA) { in map_library()
1977 fill_random_data((void *)(ph->p_vaddr + base), ph->p_memsz); in map_library()
1980 if (ph->p_type != PT_LOAD) continue; in map_library()
1983 if (!dso->phdr && eh->e_phoff >= ph->p_offset in map_library()
1984 && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) { in map_library()
1985 dso->phdr = (void *)(base + ph->p_vaddr in map_library()
1986 + (eh->e_phoff-ph->p_offset)); in map_library()
1987 dso->phnum = eh->e_phnum; in map_library()
1988 dso->phentsize = eh->e_phentsize; in map_library()
1990 this_min = ph->p_vaddr & -PAGE_SIZE; in map_library()
1991 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE; in map_library()
1992 off_start = ph->p_offset & -PAGE_SIZE; in map_library()
1993 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) | in map_library()
1994 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) | in map_library()
1995 ((ph->p_flags&PF_X) ? PROT_EXEC : 0)); in map_library()
1997 if (ph->p_flags & PF_X) { in map_library()
2001 /* Reuse the existing mapping for the lowest-address LOAD */ in map_library()
2004 this_max - this_min, in map_library()
2011 if ((ph->p_flags & PF_X) && (ph->p_align == KPMD_SIZE) && hugepage_enabled) in map_library()
2012 madvise(base + this_min, this_max - this_min, MADV_HUGEPAGE); in map_library()
2013 if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) { in map_library()
2014 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz; in map_library()
2015 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE; in map_library()
2016 size_t zeromap_size = (size_t)base + this_max - pgbrk; in map_library()
2017 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1); in map_library()
2018 …if (pgbrk - (size_t)base < this_max && mmap_fixed((void *)pgbrk, zeromap_size, prot, MAP_PRIVATE |… in map_library()
2020 set_bss_vma_name(dso->name, (void *)pgbrk, zeromap_size); in map_library()
2031 dso->base = base; in map_library()
2032 dso->dynv = laddr(dso, dyn); in map_library()
2033 if (dso->tls.size) dso->tls.image = laddr(dso, tls_image); in map_library()
2035 if (dso->modifier_begin && dso->modifier_end) { in map_library()
2054 if (l-1 >= INT_MAX) return -1; in path_open()
2064 /* Any negative value but -1 will inhibit in path_open()
2066 return -2; in path_open()
2078 if (p->rpath || !p->rpath_orig) return 0; in fixup_rpath()
2079 if (!strchr(p->rpath_orig, '$')) { in fixup_rpath()
2080 p->rpath = ld_strdup(p->rpath_orig); in fixup_rpath()
2084 s = p->rpath_orig; in fixup_rpath()
2093 if (p->kernel_mapped) { in fixup_rpath()
2103 if (l == -1) switch (errno) { in fixup_rpath()
2109 return -1; in fixup_rpath()
2116 origin = p->name; in fixup_rpath()
2120 l = t-origin; in fixup_rpath()
2122 /* Normally p->name will always be an absolute or relative in fixup_rpath()
2129 /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */ in fixup_rpath()
2132 p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1); in fixup_rpath()
2133 if (!p->rpath) return -1; in fixup_rpath()
2135 d = p->rpath; in fixup_rpath()
2136 s = p->rpath_orig; in fixup_rpath()
2138 memcpy(d, s, t-s); in fixup_rpath()
2139 d += t-s; in fixup_rpath()
2154 decode_vec(p->dynv, dyn, DYN_CNT); in decode_dyn()
2155 search_vec(p->dynv, &flags1, DT_FLAGS_1); in decode_dyn()
2157 LD_LOGI("Add DF_1_GLOBAL for %{public}s", p->name); in decode_dyn()
2158 p->is_global = true; in decode_dyn()
2161 p->flags |= DSO_FLAGS_NODELETE; in decode_dyn()
2163 p->syms = laddr(p, dyn[DT_SYMTAB]); in decode_dyn()
2164 p->strings = laddr(p, dyn[DT_STRTAB]); in decode_dyn()
2166 p->hashtab = laddr(p, dyn[DT_HASH]); in decode_dyn()
2168 p->rpath_orig = p->strings + dyn[DT_RPATH]; in decode_dyn()
2170 p->rpath_orig = p->strings + dyn[DT_RUNPATH]; in decode_dyn()
2172 p->got = laddr(p, dyn[DT_PLTGOT]); in decode_dyn()
2173 if (search_vec(p->dynv, dyn, DT_GNU_HASH)) in decode_dyn()
2174 p->ghashtab = laddr(p, *dyn); in decode_dyn()
2175 if (search_vec(p->dynv, dyn, DT_VERSYM)) in decode_dyn()
2176 p->versym = laddr(p, *dyn); in decode_dyn()
2177 if (search_vec(p->dynv, dyn, DT_VERDEF)) in decode_dyn()
2178 p->verdef = laddr(p, *dyn); in decode_dyn()
2179 if (search_vec(p->dynv, dyn, DT_VERNEED)) in decode_dyn()
2180 p->verneed = laddr(p, *dyn); in decode_dyn()
2185 if (p->hashtab) return p->hashtab[1]; in count_syms()
2188 uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4); in count_syms()
2190 for (i = nsym = 0; i < p->ghashtab[0]; i++) { in count_syms()
2195 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]); in count_syms()
2207 p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0); in dl_mmap()
2209 p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0); in dl_mmap()
2211 return (unsigned long)p > -4096UL ? 0 : p; in dl_mmap()
2218 size_t i, size = nsym * sizeof(*p->funcdescs); in makefuncdescs()
2221 p->funcdescs = dl_mmap(size); in makefuncdescs()
2224 p->funcdescs = malloc(size); in makefuncdescs()
2226 if (!p->funcdescs) { in makefuncdescs()
2228 error("Error allocating function descriptors for %s", p->name); in makefuncdescs()
2232 if ((p->syms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) { in makefuncdescs()
2233 p->funcdescs[i].addr = laddr(p, p->syms[i].st_value); in makefuncdescs()
2234 p->funcdescs[i].got = p->got; in makefuncdescs()
2236 p->funcdescs[i].addr = 0; in makefuncdescs()
2237 p->funcdescs[i].got = 0; in makefuncdescs()
2247 sys_path = conf->get_sys_paths(); in get_sys_path()
2251 sys_path = conf->get_asan_sys_paths(); in get_sys_path()
2252 char *sys_path_default = conf->get_sys_paths(); in get_sys_path()
2270 LD_LOGD("search_dso_by_name name:%{public}s, ns_name:%{public}s", name, ns ? ns->ns_name: "NULL"); in search_dso_by_name()
2271 for (size_t i = 0; i < ns->ns_dsos->num; i++) { in search_dso_by_name()
2272 struct dso *p = ns->ns_dsos->dsos[i]; in search_dso_by_name()
2273 if (p->shortname && !strcmp(p->shortname, name)) { in search_dso_by_name()
2274 …LD_LOGD("search_dso_by_name found name:%{public}s, ns_name:%{public}s", name, ns ? ns->ns_name: "N… in search_dso_by_name()
2282 LD_LOGD("search_dso_by_fstat ns_name:%{public}s", ns ? ns->ns_name : "NULL"); in search_dso_by_fstat()
2283 for (size_t i = 0; i < ns->ns_dsos->num; i++) { in search_dso_by_fstat()
2284 struct dso *p = ns->ns_dsos->dsos[i]; in search_dso_by_fstat()
2285 if (p->dev == st->st_dev && p->ino == st->st_ino && p->file_offset == file_offset) { in search_dso_by_fstat()
2287 st->st_dev, st->st_ino, ns ? ns->ns_name : "NULL"); in search_dso_by_fstat()
2296 int fd = -1; in app_has_same_name_so()
2298 if (((ns->flag & LOCAL_NS_PREFERED) != 0) && ns->lib_paths) { in app_has_same_name_so()
2300 fd = path_open(so_name, ns->lib_paths, tmp_buf, sizeof tmp_buf); in app_has_same_name_so()
2310 ns ? ns->ns_name : "NULL", in find_library_by_name()
2314 if (check_inherited && ns->ns_inherits) { in find_library_by_name()
2315 for (size_t i = 0; i < ns->ns_inherits->num; i++) { in find_library_by_name()
2316 ns_inherit * inherit = ns->ns_inherits->inherits[i]; in find_library_by_name()
2317 p = search_dso_by_name(name, inherit->inherited_ns); in find_library_by_name()
2319 if (app_has_same_name_so(name, ns) != -1) { in find_library_by_name()
2331 ns ? ns->ns_name : "NULL", in find_library_by_fstat()
2335 if (check_inherited && ns->ns_inherits) { in find_library_by_fstat()
2336 for (size_t i = 0; i < ns->ns_inherits->num; i++) { in find_library_by_fstat()
2337 ns_inherit *inherit = ns->ns_inherits->inherits[i]; in find_library_by_fstat()
2338 p = search_dso_by_fstat(st, inherit->inherited_ns, file_offset); in find_library_by_fstat()
2339 if (p && is_sharable(inherit, p->shortname)) return p; in find_library_by_fstat()
2372 if (strncmp(name+3, rp, next-rp) == 0) in load_library()
2380 unsigned mask = 1U<<(rp-reserved); in load_library()
2394 tail->next = &ldso; in load_library()
2406 fd = -1; in load_library()
2407 LD_LOGD("load_library is_accessible return false,fd = -1"); in load_library()
2424 fd = -1; in load_library()
2425 if (namespace->env_paths) fd = path_open(name, namespace->env_paths, buf, sizeof buf); in load_library()
2426 for (p = needed_by; fd == -1 && p; p = p->needed_by) { in load_library()
2428 LD_LOGD("load_library Inhibit further search,fd = -2."); in load_library()
2429 fd = -2; /* Inhibit further search. */ in load_library()
2431 if (p->rpath) { in load_library()
2432 fd = path_open(name, p->rpath, buf, sizeof buf); in load_library()
2433 LD_LOGD("load_library p->rpath path_open fd:%{public}d.", fd); in load_library()
2441 if (fd == -1 && namespace->lib_paths) { in load_library()
2442 fd = path_open(name, namespace->lib_paths, buf, sizeof buf); in load_library()
2450 if (!check_inherited || !namespace->ns_inherits) return 0; in load_library()
2452 for (size_t i = 0; i < namespace->ns_inherits->num; i++) { in load_library()
2453 ns_inherit *inherit = namespace->ns_inherits->inherits[i]; in load_library()
2455 p = load_library(name, needed_by, inherit->inherited_ns, false, reserved_params); in load_library()
2458 inherit->inherited_ns->ns_name); in load_library()
2475 if (!p->shortname && pathname != name) in load_library()
2476 p->shortname = strrchr(p->name, '/')+1; in load_library()
2488 * false positives from interposition-hack libraries. */ in load_library()
2501 * storage must include a reservation for all pre-existing in load_library()
2504 * the newly-loaded DSO. */ in load_library()
2519 p->dev = st.st_dev; in load_library()
2520 p->ino = st.st_ino; in load_library()
2521 p->needed_by = needed_by; in load_library()
2522 p->name = p->buf; in load_library()
2523 p->runtime_loaded = runtime; in load_library()
2524 strcpy(p->name, pathname); in load_library()
2526 if (pathname != name) p->shortname = strrchr(p->name, '/')+1; in load_library()
2527 if (p->tls.image) { in load_library()
2528 p->tls_id = ++tls_cnt; in load_library()
2529 tls_align = MAXP2(tls_align, p->tls.align); in load_library()
2531 p->tls.offset = tls_offset + ( (p->tls.align-1) & in load_library()
2532 (-tls_offset + (uintptr_t)p->tls.image) ); in load_library()
2533 tls_offset = p->tls.offset + p->tls.size; in load_library()
2535 tls_offset += p->tls.size + p->tls.align - 1; in load_library()
2536 tls_offset -= (tls_offset + (uintptr_t)p->tls.image) in load_library()
2537 & (p->tls.align-1); in load_library()
2538 p->tls.offset = tls_offset; in load_library()
2540 p->new_dtv = (void *)(-sizeof(size_t) & in load_library()
2541 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t))); in load_library()
2542 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1)); in load_library()
2543 if (tls_tail) tls_tail->next = &p->tls; in load_library()
2544 else libc.tls_head = &p->tls; in load_library()
2545 tls_tail = &p->tls; in load_library()
2548 tail->next = p; in load_library()
2549 p->prev = tail; in load_library()
2553 p->namespace = namespace; in load_library()
2556 p->by_dlopen = 1; in load_library()
2560 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base); in load_library()
2569 if (p->deps) return; in load_direct_deps()
2570 /* For head, all preloads are direct pseudo-dependencies. in load_direct_deps()
2572 if (p==head) for (struct dso *q=p->next; q; q=q->next) in load_direct_deps()
2574 for (i=0; p->dynv[i]; i+=2) in load_direct_deps()
2575 if (p->dynv[i] == DT_NEEDED) cnt++; in load_direct_deps()
2578 p->deps = (p==head && cnt<2) ? builtin_deps : in load_direct_deps()
2579 calloc(cnt+1, sizeof *p->deps); in load_direct_deps()
2580 if (!p->deps) { in load_direct_deps()
2581 error("Error loading dependencies for %s", p->name); in load_direct_deps()
2585 if (p==head) for (struct dso *q=p->next; q; q=q->next) in load_direct_deps()
2586 p->deps[cnt++] = q; in load_direct_deps()
2587 for (i=0; p->dynv[i]; i+=2) { in load_direct_deps()
2588 if (p->dynv[i] != DT_NEEDED) continue; in load_direct_deps()
2589 struct dso *dep = load_library(p->strings + p->dynv[i + 1], p, namespace, true, reserved_params); in load_direct_deps()
2590 …D("loading shared library %{public}s: (needed by %{public}s)", p->strings + p->dynv[i+1], p->name); in load_direct_deps()
2593 p->strings + p->dynv[i+1], p->name); in load_direct_deps()
2597 p->deps[cnt++] = dep; in load_direct_deps()
2599 p->deps[cnt] = 0; in load_direct_deps()
2600 p->ndeps_direct = cnt; in load_direct_deps()
2601 for (i = 0; i < p->ndeps_direct; i++) { in load_direct_deps()
2602 add_dso_parent(p->deps[i], p); in load_direct_deps()
2608 if (p->deps) return; in load_deps()
2609 for (; p; p = p->next) in load_deps()
2610 load_direct_deps(p, p->namespace, reserved_params); in load_deps()
2619 /* Can't use realloc if the original p->deps was allocated at in extend_bfs_deps()
2621 * the builtin non-allocated trivial main program deps array. */ in extend_bfs_deps()
2622 int no_realloc = (__malloc_replaced && !p->runtime_loaded) in extend_bfs_deps()
2623 || p->deps == builtin_deps; in extend_bfs_deps()
2625 if (p->bfs_built) return; in extend_bfs_deps()
2626 if (to_deps_all && p->deps_all_built) { in extend_bfs_deps()
2630 ndeps_all = p->ndeps_direct; in extend_bfs_deps()
2633 p->deps_all = calloc(ndeps_all + 1, sizeof *p->deps); in extend_bfs_deps()
2637 for (i=0; p->deps[i]; i++) { in extend_bfs_deps()
2639 p->deps_all[i] = p->deps[i]; in extend_bfs_deps()
2641 p->deps[i]->mark = 1; in extend_bfs_deps()
2649 for (i=0; p->deps_all[i]; i++) { in extend_bfs_deps()
2650 struct dso *dep = p->deps_all[i]; in extend_bfs_deps()
2651 for (j=cnt=0; j<dep->ndeps_direct; j++) in extend_bfs_deps()
2652 if (!dep->deps[j]->mark) cnt++; in extend_bfs_deps()
2655 realloc(p->deps_all, sizeof(*tmp) * (ndeps_all+cnt+1)); in extend_bfs_deps()
2657 error("Error recording dependencies for %s", p->name); in extend_bfs_deps()
2662 memcpy(tmp, p->deps_all, sizeof(*tmp) * (ndeps_all+1)); in extend_bfs_deps()
2665 p->deps_all = tmp; in extend_bfs_deps()
2666 for (j=0; j<dep->ndeps_direct; j++) { in extend_bfs_deps()
2667 if (dep->deps[j]->mark) continue; in extend_bfs_deps()
2668 dep->deps[j]->mark = 1; in extend_bfs_deps()
2669 p->deps_all[ndeps_all++] = dep->deps[j]; in extend_bfs_deps()
2671 p->deps_all[ndeps_all] = 0; in extend_bfs_deps()
2673 p->deps_all_built = 1; in extend_bfs_deps()
2675 for (i=0; p->deps[i]; i++) { in extend_bfs_deps()
2676 struct dso *dep = p->deps[i]; in extend_bfs_deps()
2677 for (j=cnt=0; j<dep->ndeps_direct; j++) in extend_bfs_deps()
2678 if (!dep->deps[j]->mark) cnt++; in extend_bfs_deps()
2681 realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1)); in extend_bfs_deps()
2683 error("Error recording dependencies for %s", p->name); in extend_bfs_deps()
2688 memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1)); in extend_bfs_deps()
2691 p->deps = tmp; in extend_bfs_deps()
2692 for (j=0; j<dep->ndeps_direct; j++) { in extend_bfs_deps()
2693 if (dep->deps[j]->mark) continue; in extend_bfs_deps()
2694 dep->deps[j]->mark = 1; in extend_bfs_deps()
2695 p->deps[ndeps_all++] = dep->deps[j]; in extend_bfs_deps()
2697 p->deps[ndeps_all] = 0; in extend_bfs_deps()
2699 p->bfs_built = 1; in extend_bfs_deps()
2701 for (p=head; p; p=p->next) in extend_bfs_deps()
2702 p->mark = 0; in extend_bfs_deps()
2723 if (!p->syms_next && syms_tail != p) { in add_syms()
2724 syms_tail->syms_next = p; in add_syms()
2735 next = p->syms_next; in revert_syms()
2736 p->syms_next = 0; in revert_syms()
2744 unsigned char *base = p->base; in do_mips_relocs()
2745 i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO); in do_mips_relocs()
2749 while (i--) *got++ += (size_t)base; in do_mips_relocs()
2751 j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM); in do_mips_relocs()
2752 i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); in do_mips_relocs()
2753 Sym *sym = p->syms + j; in do_mips_relocs()
2754 rel[0] = (unsigned char *)got - base; in do_mips_relocs()
2755 for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) { in do_mips_relocs()
2756 rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT); in do_mips_relocs()
2780 result |= -((size_t)(1) << shift); in sleb128_decoder()
2793 search_vec(p->dynv, &android_rel_addr, dt_name); in do_android_relocs()
2794 search_vec(p->dynv, &android_rel_size, dt_size); in do_android_relocs()
2806 android_rel_size -= ANDROID_REL_SIGN_SIZE; in do_android_relocs()
2877 if (dso == &ldso) return; /* self-relocation was done in _dlstart */ in do_relr_relocs()
2878 unsigned char *base = dso->base; in do_relr_relocs()
2880 for (; relr_size; relr++, relr_size -= sizeof(size_t)) in do_relr_relocs()
2889 reloc_addr += 8 * sizeof(size_t) - 1; in do_relr_relocs()
2896 return; /* self-relocation has done in _dlstart*/ in do_auth_relr_relocs()
2900 search_vec(p->dynv, &auth_relr_addr, dt_name); in do_auth_relr_relocs()
2904 search_vec(p->dynv, &auth_relr_size, dt_size); in do_auth_relr_relocs()
2905 unsigned char *base = p->base; in do_auth_relr_relocs()
2908 for (; auth_relr_size; auth_relr++, auth_relr_size -= sizeof(size_t)) { in do_auth_relr_relocs()
2924 auth_reloc_addr += 8 * sizeof(size_t) - 1; in do_auth_relr_relocs()
2933 for (; p; p=p->next) { in reloc_all()
2934 if (p->relocated) continue; in reloc_all()
2938 decode_vec(p->dynv, dyn, DYN_CNT); in reloc_all()
2954 if (head != &ldso && p->relro_start != p->relro_end && in reloc_all()
2955 mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ) in reloc_all()
2958 p->name); in reloc_all()
2964 /* We need to skip dso with shared RELRO*/ in reloc_all()
2965 if (head != &ldso && p->relro_start != p->relro_end && extinfo == NULL) { in reloc_all()
2966 if (prctl(HM_GOT_RO, 0, laddr(p, p->relro_start), p->relro_end - p->relro_start)) { in reloc_all()
2968 …LD_LOGW("Failed to set readonly to relro segment of %{public}s, errno %{public}d", p->name, errno); in reloc_all()
2973 p->relocated = 1; in reloc_all()
2980 size_t min_addr = -1, max_addr = 0, cnt; in kernel_mapped_dso()
2981 Phdr *ph = p->phdr; in kernel_mapped_dso()
2982 for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) { in kernel_mapped_dso()
2983 if (ph->p_type == PT_DYNAMIC) { in kernel_mapped_dso()
2984 p->dynv = laddr(p, ph->p_vaddr); in kernel_mapped_dso()
2985 } else if (ph->p_type == PT_GNU_RELRO) { in kernel_mapped_dso()
2986 p->relro_start = ph->p_vaddr & -PAGE_SIZE; in kernel_mapped_dso()
2987 p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; in kernel_mapped_dso()
2988 } else if (ph->p_type == PT_GNU_STACK) { in kernel_mapped_dso()
2989 if (!runtime && ph->p_memsz > __default_stacksize) { in kernel_mapped_dso()
2991 ph->p_memsz < DEFAULT_STACK_MAX ? in kernel_mapped_dso()
2992 ph->p_memsz : DEFAULT_STACK_MAX; in kernel_mapped_dso()
2995 if (ph->p_type != PT_LOAD) continue; in kernel_mapped_dso()
2996 if (ph->p_vaddr < min_addr) in kernel_mapped_dso()
2997 min_addr = ph->p_vaddr; in kernel_mapped_dso()
2998 if (ph->p_vaddr+ph->p_memsz > max_addr) in kernel_mapped_dso()
2999 max_addr = ph->p_vaddr+ph->p_memsz; in kernel_mapped_dso()
3001 min_addr &= -PAGE_SIZE; in kernel_mapped_dso()
3002 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE; in kernel_mapped_dso()
3003 p->map = p->base + min_addr; in kernel_mapped_dso()
3004 p->map_len = max_addr - min_addr; in kernel_mapped_dso()
3005 p->kernel_mapped = 1; in kernel_mapped_dso()
3021 for (p=fini_head; p; p=p->fini_next) { in __libc_exit_fini()
3022 while (p->ctor_visitor && p->ctor_visitor!=self) in __libc_exit_fini()
3024 if (!p->constructed) continue; in __libc_exit_fini()
3025 decode_vec(p->dynv, dyn, DYN_CNT); in __libc_exit_fini()
3029 while (n--) ((void (*)(void))*--fn)(); in __libc_exit_fini()
3043 // so reset dlclose_lock to make sure child process can call dlclose after fork in __pthread_mutex_unlock_atfork()
3074 if (dso->bfs_built) { in queue_ctors()
3075 for (cnt=0; dso->deps[cnt]; cnt++) in queue_ctors()
3076 dso->deps[cnt]->mark = 0; in queue_ctors()
3079 for (cnt=0, p=head; p; cnt++, p=p->next) in queue_ctors()
3080 p->mark = 0; in queue_ctors()
3100 stack[--spos] = dso; in queue_ctors()
3101 dso->next_dep = 0; in queue_ctors()
3102 dso->mark = 1; in queue_ctors()
3104 /* Then perform pseudo-DFS sort, but ignoring circular deps. */ in queue_ctors()
3107 while (p->next_dep < p->ndeps_direct) { in queue_ctors()
3108 if (p->deps[p->next_dep]->mark) { in queue_ctors()
3109 p->next_dep++; in queue_ctors()
3111 stack[--spos] = p; in queue_ctors()
3112 p = p->deps[p->next_dep]; in queue_ctors()
3113 p->next_dep = 0; in queue_ctors()
3114 p->mark = 1; in queue_ctors()
3120 for (i=0; i<qpos; i++) queue[i]->mark = 0; in queue_ctors()
3133 while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down) in do_init_fini()
3135 if (p->ctor_visitor || p->constructed) in do_init_fini()
3137 p->ctor_visitor = self; in do_init_fini()
3139 decode_vec(p->dynv, dyn, DYN_CNT); in do_init_fini()
3141 p->fini_next = fini_head; in do_init_fini()
3155 trace_marker_begin(HITRACE_TAG_MUSL, "calling constructors: ", p->name); in do_init_fini()
3157 while (n--) ((void (*)(void))*fn++)(); in do_init_fini()
3164 p->ctor_visitor = 0; in do_init_fini()
3165 p->constructed = 1; in do_init_fini()
3206 uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv; in install_new_tls()
3209 size_t old_cnt = self->dtv[0]; in install_new_tls()
3214 get_tl_lock_caller_count()->install_new_tls_tl_lock++; in install_new_tls()
3217 for (i=0, td=self; !i || td!=self; i++, td=td->next) { in install_new_tls()
3218 memcpy(newdtv+i, td->dtv, in install_new_tls()
3223 for (p=head; ; p=p->next) { in install_new_tls()
3224 if (p->tls_id <= old_cnt) continue; in install_new_tls()
3225 unsigned char *mem = p->new_tls; in install_new_tls()
3228 new += ((uintptr_t)p->tls.image - (uintptr_t)mem) in install_new_tls()
3229 & (p->tls.align-1); in install_new_tls()
3230 memcpy(new, p->tls.image, p->tls.len); in install_new_tls()
3231 newdtv[j][p->tls_id] = in install_new_tls()
3233 mem += p->tls.size + p->tls.align; in install_new_tls()
3235 if (p->tls_id == tls_cnt) break; in install_new_tls()
3246 for (j=0, td=self; !j || td!=self; j++, td=td->next) { in install_new_tls()
3247 td->dtv = newdtv[j]; in install_new_tls()
3251 get_tl_lock_caller_count()->install_new_tls_tl_lock--; in install_new_tls()
3264 size_t modifier_size = (pac_items[index].modifier_end - pac_items[index].modifier_begin) / in check_modifier()
3267 size_t end = modifier_size - 1; in check_modifier()
3268 size_t addr_off = addr - pac_items[index].base; in check_modifier()
3270 size_t mid = start + (end - start) / 2; in check_modifier()
3276 end = mid - 1; in check_modifier()
3311 …if (siginfo == NULL || ucontext_raw == NULL || siginfo->si_signo != SIGILL || siginfo->si_code != … in pac_reset_handler()
3315 size_t addr = ucontext->uc_mcontext.regs[PAC_TARGET_ADDR_REGISTER]; in pac_reset_handler()
3316 size_t modifier = ucontext->uc_mcontext.regs[PAC_MODIFIER_REGISTER]; in pac_reset_handler()
3327 ucontext->uc_mcontext.regs[PAC_TARGET_ADDR_REGISTER] = addr; in pac_reset_handler()
3329 ucontext->uc_mcontext.pc = ucontext->uc_mcontext.pc + 4; in pac_reset_handler()
3361 void *p1 = (void *)sp[-2]; in __dls2()
3362 void *p2 = (void *)sp[-1]; in __dls2()
3367 else ldso.base = (void *)(aux[AT_PHDR] & -4096); in __dls2()
3380 ldso.phnum = ehdr->e_phnum; in __dls2()
3381 ldso.phdr = laddr(&ldso, ehdr->e_phoff); in __dls2()
3382 ldso.phentsize = ehdr->e_phentsize; in __dls2()
3400 for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) in __dls2()
3411 /* Call dynamic linker stage-2b, __dls2b, looking it up in __dls2()
3415 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp, auxv, aux); in __dls2()
3416 else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp, auxv, aux); in __dls2()
3437 __pthread_self()->stack = (void *)(sp + 1); in __dls2b()
3439 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp, auxv, aux); in __dls2b()
3440 else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp, auxv, aux); in __dls2b()
3464 __pthread_self()->sysinfo = __sysinfo; in __dls3()
3500 for (i = aux[AT_PHNUM]; i; i--, phdr = (void *)((char *)phdr + aux[AT_PHENT])) { in __dls3()
3501 if (phdr->p_type == PT_PHDR) in __dls3()
3502 app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr); in __dls3()
3503 else if (phdr->p_type == PT_INTERP) in __dls3()
3504 interp_off = (size_t)phdr->p_vaddr; in __dls3()
3505 else if (phdr->p_type == PT_TLS) { in __dls3()
3506 tls_image = phdr->p_vaddr; in __dls3()
3507 app.tls.len = phdr->p_filesz; in __dls3()
3508 app.tls.size = phdr->p_memsz; in __dls3()
3509 app.tls.align = phdr->p_align; in __dls3()
3525 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1; in __dls3()
3527 while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') { in __dls3()
3529 *argv++ = (void *)-1; in __dls3()
3534 } else if (!memcmp(opt, "library-path", 12)) { in __dls3()
3550 argv[-1] = (void *)(argc - (argv-argv_orig)); in __dls3()
3555 "Usage: %s [options] [--] pathname%s\n", in __dls3()
3573 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry); in __dls3()
3589 app.tls.offset += (-GAP_ABOVE_TP + (uintptr_t)app.tls.image) in __dls3()
3590 & (app.tls.align-1); in __dls3()
3594 + ( -((uintptr_t)app.tls.image + app.tls.size) in __dls3()
3595 & (app.tls.align-1) ); in __dls3()
3604 app.loadmap->nsegs = 1; in __dls3()
3605 app.loadmap->segs[0].addr = (size_t)app.map; in __dls3()
3606 app.loadmap->segs[0].p_vaddr = (size_t)app.map in __dls3()
3607 - (size_t)app.base; in __dls3()
3608 app.loadmap->segs[0].p_memsz = app.map_len; in __dls3()
3610 argv[-3] = (void *)app.loadmap; in __dls3()
3630 if (strstr(ldso.name, "-asan")) { in __dls3()
3647 for (struct dso *q = head; q; q = q->next) { in __dls3()
3648 q->is_global = true; in __dls3()
3649 q->is_preload = true; in __dls3()
3660 for (struct dso *q = head; q; q = q->next) { in __dls3()
3661 q->is_global = true; in __dls3()
3662 q->is_preload = true; in __dls3()
3668 for (struct dso *p = head; p; p = p->next) { in __dls3()
3669 p->is_reloc_head_so_dep = true; in __dls3()
3677 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff); in __dls3()
3678 vdso.phnum = ehdr->e_phnum; in __dls3()
3679 vdso.phentsize = ehdr->e_phentsize; in __dls3()
3680 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) { in __dls3()
3681 if (phdr->p_type == PT_DYNAMIC) in __dls3()
3682 vdso.dynv = (void *)(vdso_base + phdr->p_offset); in __dls3()
3683 if (phdr->p_type == PT_LOAD) in __dls3()
3684 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset); in __dls3()
3687 vdso.shortname = "linux-gate.so.1"; in __dls3()
3692 tail->next = &vdso; in __dls3()
3715 * are additional relocation-like fixups that only the entry point in __dls3()
3726 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n", in __dls3()
3737 for (struct dso *q = head; q; q = q->next) { in __dls3()
3738 q->is_reloc_head_so_dep = false; in __dls3()
3749 td->tsd = self->tsd; in __dls3()
3750 // Record stack here for unwinding in gwp-asan in __dls3()
3751 td->stack = self->stack; in __dls3()
3793 CRTJMP((void *)aux[AT_ENTRY], argv - 1); in __dls3()
3800 decode_vec(p->dynv, dyn, DYN_CNT); in prepare_lazy()
3801 search_vec(p->dynv, &flags1, DT_FLAGS_1); in prepare_lazy()
3806 size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM); in prepare_lazy()
3807 size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); in prepare_lazy()
3808 n += i-j; in prepare_lazy()
3810 p->lazy = calloc(n, 3*sizeof(size_t)); in prepare_lazy()
3811 if (!p->lazy) { in prepare_lazy()
3812 error("Error preparing lazy relocation for %s: %m", p->name); in prepare_lazy()
3815 p->lazy_next = lazy_head; in prepare_lazy()
3827 p->nr_dlopen++; in dlopen_post()
3829 LD_LOGW("[dlclose]: %{public}s nr_dlopen++ when dlopen %{public}s, nr_dlopen:%{public}d ", in dlopen_post()
3830 p->name, p->name, p->nr_dlopen); in dlopen_post()
3832 if (p->bfs_built) { in dlopen_post()
3833 for (int i = 0; p->deps[i]; i++) { in dlopen_post()
3834 p->deps[i]->nr_dlopen++; in dlopen_post()
3836 LD_LOGW("[dlclose]: %{public}s nr_dlopen++ when dlopen %{public}s, nr_dlopen:%{public}d", in dlopen_post()
3837 p->deps[i]->name, p->name, p->deps[i]->nr_dlopen); in dlopen_post()
3840 p->deps[i]->flags |= DSO_FLAGS_NODELETE; in dlopen_post()
3889 if ((caller == NULL) || (caller->namespace == NULL)) { in is_permitted()
3894 ns = caller->namespace; in is_permitted()
3895 if (in_permitted_list(ns->ns_name, target) == false) { in is_permitted()
3896 LD_LOGW("caller ns: %{public}s have no permission, target is %{public}s", ns->ns_name, target); in is_permitted()
3957 … int ret = snprintf(asan_file, sizeof asan_file, "%.*s/asan%s", (int)(place - file), file, place); in dlopen_impl()
3969 reserved_address_recursive = extinfo->flag & DL_EXT_RESERVED_ADDRESS_RECURSIVE; in dlopen_impl()
3970 if (extinfo->flag & DL_EXT_RESERVED_ADDRESS) { in dlopen_impl()
3972 reserved_params.start_addr = extinfo->reserved_addr; in dlopen_impl()
3973 reserved_params.reserved_size = extinfo->reserved_size; in dlopen_impl()
3976 } else if (extinfo->flag & DL_EXT_RESERVED_ADDRESS_HINT) { in dlopen_impl()
3978 reserved_params.start_addr = extinfo->reserved_addr; in dlopen_impl()
3979 reserved_params.reserved_size = extinfo->reserved_size; in dlopen_impl()
3995 if (!ns) ns = ((caller && caller->namespace) ? caller->namespace : get_default_ns()); in dlopen_impl()
4015 for (p = orig_tail->next; p; p = next) { in dlopen_impl()
4016 next = p->next; in dlopen_impl()
4017 while (p->td_index) { in dlopen_impl()
4018 void *tmp = p->td_index->next; in dlopen_impl()
4019 free(p->td_index); in dlopen_impl()
4020 p->td_index = tmp; in dlopen_impl()
4022 free(p->funcdescs); in dlopen_impl()
4023 free(p->rpath); in dlopen_impl()
4024 if (p->deps) { in dlopen_impl()
4025 for (int i = 0; i < p->ndeps_direct; i++) { in dlopen_impl()
4026 remove_dso_parent(p->deps[i], p); in dlopen_impl()
4029 free(p->deps); in dlopen_impl()
4032 if (p->parents) { in dlopen_impl()
4033 free(p->parents); in dlopen_impl()
4037 for (p=orig_tail->next; p; p=next) { in dlopen_impl()
4038 next = p->next; in dlopen_impl()
4045 if (tls_tail) tls_tail->next = 0; in dlopen_impl()
4051 tail->next = 0; in dlopen_impl()
4073 LD_LOGW("dlopen_impl load library header failed for %{public}s", task->name); in dlopen_impl()
4078 reserved_params.target = task->p; in dlopen_impl()
4081 if (!task->p) { in dlopen_impl()
4082 LD_LOGW("dlopen_impl load library failed for %{public}s", task->name); in dlopen_impl()
4091 dlopen_cost.entry_header_time = (time_end.tv_sec - time_start.tv_sec) * CLOCK_SECOND_TO_MILLI in dlopen_impl()
4092 + (time_end.tv_nsec - time_start.tv_nsec) / CLOCK_NANO_TO_MILLI; in dlopen_impl()
4093 if (!task->isloaded) { in dlopen_impl()
4097 preload_deps(task->p, tasks); in dlopen_impl()
4099 dlopen_cost.deps_header_time = (time_end.tv_sec - time_start.tv_sec) * CLOCK_SECOND_TO_MILLI in dlopen_impl()
4100 + (time_end.tv_nsec - time_start.tv_nsec) / CLOCK_NANO_TO_MILLI; in dlopen_impl()
4108 dlopen_cost.map_so_time = (time_end.tv_sec - time_start.tv_sec) * CLOCK_SECOND_TO_MILLI in dlopen_impl()
4109 + (time_end.tv_nsec - time_start.tv_nsec) / CLOCK_NANO_TO_MILLI; in dlopen_impl()
4110 p = task->p; in dlopen_impl()
4111 if (!task->isloaded) { in dlopen_impl()
4139 int constructed = p->constructed;
4142 if (!p->relocated && (mode & RTLD_LAZY)) {
4144 for (i = 0; p->deps[i]; i++)
4145 if (!p->deps[i]->relocated)
4146 prepare_lazy(p->deps[i]);
4148 if (!p->relocated || (mode & RTLD_GLOBAL)) {
4153 p->is_reloc_head_so_dep = true;
4154 for (i = 0; p->deps[i]; i++) {
4155 p->deps[i]->is_reloc_head_so_dep = true;
4156 add_syms(p->deps[i]);
4160 trace_marker_begin(HITRACE_TAG_MUSL, "linking: entry so", p->name);
4162 if (!p->relocated) {
4166 dlopen_cost.reloc_time = (time_end.tv_sec - time_start.tv_sec) * CLOCK_SECOND_TO_MILLI
4167 + (time_end.tv_nsec - time_start.tv_nsec) / CLOCK_NANO_TO_MILLI;
4169 reloc_head_so->is_reloc_head_so_dep = false;
4170 for (size_t i = 0; reloc_head_so->deps[i]; i++) {
4171 reloc_head_so->deps[i]->is_reloc_head_so_dep = false;
4190 dlopen_cost.map_cfi_time = (time_end.tv_sec - time_start.tv_sec) * CLOCK_SECOND_TO_MILLI
4191 + (time_end.tv_nsec - time_start.tv_nsec) / CLOCK_NANO_TO_MILLI;
4194 p->flags |= DSO_FLAGS_NODELETE;
4202 notify_addition_to_debugger(orig_tail->next);
4211 /* The shadow memory corresponding to HWASAN-instrumented global
4213 * tag-mismatch errors when do_init_fini call the initialization
4215 for (struct dso *new = notifier_tail->next; new; new = new->next) {
4217 libc.load_hook((long unsigned int)new->base, new->phdr, new->phnum);
4225 for (struct dso *new = notifier_tail->next; new; new = new->next) {
4226 if (!new->lazy_cnt) {
4257 dlopen_cost.init_time = (time_end.tv_sec - time_start.tv_sec) * CLOCK_SECOND_TO_MILLI
4258 + (time_end.tv_nsec - time_start.tv_nsec) / CLOCK_NANO_TO_MILLI;
4262 dlopen_cost.total_time = (total_end.tv_sec - total_start.tv_sec) * CLOCK_SECOND_TO_MILLI
4263 + (total_end.tv_nsec - total_start.tv_nsec) / CLOCK_NANO_TO_MILLI;
4275 current_so->name,
4304 dlns->name[0] = 0; in dlns_init()
4313 snprintf(dlns->name, sizeof dlns->name, name); in dlns_init()
4314 LD_LOGI("dlns_init dlns->name:%{public}s .", dlns->name); in dlns_init()
4330 ns = ((caller && caller->namespace) ? caller->namespace : get_default_ns()); in dlns_get()
4331 (void)snprintf(dlns->name, sizeof dlns->name, ns->ns_name); in dlns_get()
4332 LD_LOGI("dlns_get name is null, current dlns dlns->name:%{public}s.", dlns->name); in dlns_get()
4336 (void)snprintf(dlns->name, sizeof dlns->name, ns->ns_name); in dlns_get()
4337 LD_LOGI("dlns_get found ns, current dlns dlns->name:%{public}s.", dlns->name); in dlns_get()
4350 if (is_permitted(caller_addr, dlns->name) == false) { in dlopen_ns()
4356 …LD_LOGI("dlopen_ns file:%{public}s, mode:%{public}x , caller_addr:%{public}p , dlns->name:%{public… in dlopen_ns()
4360 dlns ? dlns->name : "NULL"); in dlopen_ns()
4361 return dlopen_impl(file, mode, dlns->name, caller_addr, NULL); in dlopen_ns()
4367 if (is_permitted(caller_addr, dlns->name) == false) { in dlopen_ns_ext()
4374 "dlns->name:%{public}s. , extinfo->flag:%{public}x", in dlopen_ns_ext()
4378 dlns->name, in dlopen_ns_ext()
4379 extinfo ? extinfo->flag : 0); in dlopen_ns_ext()
4380 return dlopen_impl(file, mode, dlns->name, caller_addr, extinfo); in dlopen_ns_ext()
4393 if (is_permitted(caller_addr, dlns->name) == false) { in dlns_create2()
4398 ns = find_ns_by_name(dlns->name); in dlns_create2()
4410 ns_set_name(ns, dlns->name); in dlns_create2()
4412 ns_add_dso(ns, get_default_ns()->ns_dsos->dsos[0]); /* add main app to this namespace*/ in dlns_create2()
4424 if (caller && caller->namespace) { in dlns_create2()
4425 ns_add_inherit(ns, caller->namespace, NULL); in dlns_create2()
4433 ns->ns_name, ns->separated, ns->lib_paths); in dlns_create2()
4454 if (is_permitted(caller_addr, dlns->name) == false) { in dlns_inherit()
4459 ns_t* ns = find_ns_by_name(dlns->name); in dlns_inherit()
4460 ns_t* ns_inherited = find_ns_by_name(inherited->name); in dlns_inherit()
4475 ns_t * ns = p->namespace; in dlclose_ns()
4476 if (!ns || !ns->ns_dsos) return; in dlclose_ns()
4477 for (size_t i = 0; i < ns->ns_dsos->num; i++) { in dlclose_ns()
4478 if (p == ns->ns_dsos->dsos[i]) { in dlclose_ns()
4479 for (size_t j = i + 1; j < ns->ns_dsos->num; j++) { in dlclose_ns()
4480 ns->ns_dsos->dsos[j - 1] = ns->ns_dsos->dsos[j]; in dlclose_ns()
4482 ns->ns_dsos->num--; in dlclose_ns()
4491 for (p=head; p; p=p->next) if (h==p) return 0; in __dl_invalid_handle()
4499 for (p=head; p; p=p->next) { in addr2dso()
4500 if (a < (size_t)p->map || a - (size_t)p->map >= p->map_len) continue; in addr2dso()
4501 Phdr *ph = p->phdr; in addr2dso()
4502 size_t phcnt = p->phnum; in addr2dso()
4503 size_t entsz = p->phentsize; in addr2dso()
4504 size_t base = (size_t)p->base; in addr2dso()
4505 for (; phcnt--; ph=(void *)((char *)ph+entsz)) { in addr2dso()
4506 if (ph->p_type != PT_LOAD) continue; in addr2dso()
4507 if (a-base-ph->p_vaddr < ph->p_memsz) in addr2dso()
4510 if (a-(size_t)p->map < p->map_len) in addr2dso()
4528 p = p->next; in do_dlsym()
4536 ns = p->namespace; in do_dlsym()
4540 if (caller && caller->namespace) { in do_dlsym()
4541 ns = caller->namespace; in do_dlsym()
4551 (p == NULL ? "NULL" : p->name), s, v); in do_dlsym()
4553 s, strlen(v) > 0 ? v : "null", (p == NULL ? "NULL" : p->name)); in do_dlsym()
4556 if ((def.sym->st_info&0xf) == STT_TLS) in do_dlsym()
4557 return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET}); in do_dlsym()
4558 if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC) in do_dlsym()
4559 return def.dso->funcdescs + (def.sym - def.dso->syms); in do_dlsym()
4560 return laddr(def.dso, def.sym->st_value); in do_dlsym()
4569 LD_LOGW("[dlclose]: invalid handle %{public}p", p); in so_can_unload()
4570 error("[dlclose]: Handle is invalid."); in so_can_unload()
4574 if (!p->by_dlopen) { in so_can_unload()
4575 LD_LOGD("[dlclose]: skip unload %{public}s because it's not loaded by dlopen", p->name); in so_can_unload()
4580 if ((p->flags & DSO_FLAGS_NODELETE) != 0) { in so_can_unload()
4581 LD_LOGD("[dlclose]: skip unload %{public}s because flags is RTLD_NODELETE", p->name); in so_can_unload()
4587 if (p->nr_dlopen > 0) { in so_can_unload()
4588 …LD_LOGD("[dlclose]: skip unload %{public}s because nr_dlopen=%{public}d > 0", p->name, p->nr_dlope… in so_can_unload()
4599 return -1; in dlclose_post()
4603 libc.unload_hook((unsigned long int)p->base, p->phdr, p->phnum); in dlclose_post()
4608 if (p->parents) { in dlclose_post()
4609 free(p->parents); in dlclose_post()
4612 if (p->tls.size == 0) { in dlclose_post()
4625 trace_marker_begin(HITRACE_TAG_MUSL, "dlclose", p->name); in dlclose_impl()
4628 if (p->syms_next) { in dlclose_impl()
4629 for (d = head; d->syms_next != p; d = d->syms_next) in dlclose_impl()
4631 d->syms_next = p->syms_next; in dlclose_impl()
4633 for (d = head; d->syms_next != p; d = d->syms_next) in dlclose_impl()
4635 d->syms_next = NULL; in dlclose_impl()
4641 lazy_head = p->lazy_next; in dlclose_impl()
4642 } else if (p->lazy_next) { in dlclose_impl()
4643 for (d = lazy_head; d->lazy_next != p; d = d->lazy_next) in dlclose_impl()
4645 d->lazy_next = p->lazy_next; in dlclose_impl()
4651 fini_head = p->fini_next; in dlclose_impl()
4652 } else if (p->fini_next) { in dlclose_impl()
4653 for (d = fini_head; d->fini_next != p; d = d->fini_next) in dlclose_impl()
4655 d->fini_next = p->fini_next; in dlclose_impl()
4660 if (p->tls.size != 0) { in dlclose_impl()
4661 p->tls.image = NULL; in dlclose_impl()
4666 tail = p->prev; in dlclose_impl()
4667 tail->next = NULL; in dlclose_impl()
4669 p->next->prev = p->prev; in dlclose_impl()
4670 p->prev->next = p->next; in dlclose_impl()
4689 if (p->lazy != NULL) in dlclose_impl()
4690 free(p->lazy); in dlclose_impl()
4691 if (p->deps != no_deps) in dlclose_impl()
4692 free(p->deps); in dlclose_impl()
4694 if (p->deps_all_built) { in dlclose_impl()
4695 free(p->deps_all); in dlclose_impl()
4698 if (p->item != NULL) { in dlclose_impl()
4720 if (p->nr_dlopen > 0) { in do_dlclose()
4721 --(p->nr_dlopen); in do_dlclose()
4723 …LD_LOGW("[dlclose]: number of dlopen and dlclose of %{public}s doesn't match when dlclose %{public… in do_dlclose()
4724 p->name, p->name); in do_dlclose()
4728 if (p->bfs_built) { in do_dlclose()
4729 for (int i = 0; p->deps[i]; i++) { in do_dlclose()
4730 if (p->deps[i]->nr_dlopen > 0) { in do_dlclose()
4731 p->deps[i]->nr_dlopen--; in do_dlclose()
4733 …LD_LOGW("[dlclose]: number of dlopen and dlclose of %{public}s doesn't match when dlclose %{public… in do_dlclose()
4734 p->deps[i]->name, p->name); in do_dlclose()
4740 … * - nr_dlopen increases for all deps(include self) when a thread local object destructor is added. in do_dlclose()
4741 …* - nr_dlopen decreases for all deps(include self) when a thread local object destructor is called. in do_dlclose()
4743 if (check_deps_all && p->deps_all_built) { in do_dlclose()
4744 for (int i = 0; p->deps_all[i]; i++) { in do_dlclose()
4745 if (p->deps_all[i]->nr_dlopen > 0) { in do_dlclose()
4746 p->deps_all[i]->nr_dlopen--; in do_dlclose()
4748 …LD_LOGW("[dlclose]: number of dlopen and dlclose of %{public}s doesn't match when dlclose %{public… in do_dlclose()
4749 p->deps_all[i]->name, p->name); in do_dlclose()
4763 start_entry->dso = p; in do_dlclose()
4768 struct dso *cur = ecur->dso; in do_dlclose()
4772 if (ef->dso == cur) { in do_dlclose()
4781 for (int i = 0; i < cur->ndeps_direct; i++) { in do_dlclose()
4782 remove_dso_parent(cur->deps[i], cur); in do_dlclose()
4783 if ((cur->deps[i]->parents_count == 0) && (so_can_unload(cur->deps[i], UNLOAD_ALL_CHECK) == 1)) { in do_dlclose()
4786 if (ef->dso == cur->deps[i]) { in do_dlclose()
4796 edeps->dso = cur->deps[i]; in do_dlclose()
4804 LD_LOGW("[dlclose]: unload %{public}s succeed when dlclose %{public}s", ef->dso->name, p->name); in do_dlclose()
4806 for (size_t deps_num = 0; p->deps[deps_num]; deps_num++) { in do_dlclose()
4809 if (ef->dso == p->deps[deps_num]) { in do_dlclose()
4815 LD_LOGW("[dlclose]: unload %{public}s failed when dlclose %{public}s," in do_dlclose()
4817 p->deps[deps_num]->name, p->name, p->deps[deps_num]->nr_dlopen, in do_dlclose()
4818 p->deps[deps_num]->by_dlopen, p->deps[deps_num]->parents_count); in do_dlclose()
4824 dlclose_impl(ef->dso); in do_dlclose()
4830 int constructed = ef->dso->constructed; in do_dlclose()
4835 decode_vec(ef->dso->dynv, dyn, DYN_CNT); in do_dlclose()
4838 size_t *fn = (size_t *)laddr(ef->dso, dyn[DT_FINI_ARRAY]) + n; in do_dlclose()
4839 trace_marker_begin(HITRACE_TAG_MUSL, "calling destructors:", ef->dso->name); in do_dlclose()
4842 while (n--) in do_dlclose()
4843 ((void (*)(void))*--fn)(); in do_dlclose()
4849 ef->dso->constructed = 0; in do_dlclose()
4855 dlclose_post(ef->dso); in do_dlclose()
4874 error("Cannot dlclose while program is exiting."); in __dlclose()
4877 return -1; in __dlclose()
4887 return -1; in __dlclose()
4901 return sym->st_value && in sym_is_matched()
4902 (1<<(sym->st_info&0xf) != STT_TLS) && in sym_is_matched()
4903 (addr_offset_so >= sym->st_value) && in sym_is_matched()
4904 (addr_offset_so < sym->st_value + sym->st_size); in sym_is_matched()
4908 uint32_t nsym = p->hashtab[1]; in find_addr_by_elf()
4909 Sym *sym = p->syms; in find_addr_by_elf()
4910 for (; nsym; nsym--, sym++) { in find_addr_by_elf()
4923 Sym *sym_tab = p->syms; in find_addr_by_gnu()
4924 uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2] * sizeof(size_t) / 4); in find_addr_by_gnu()
4930 for (i = nsym = 0; i < p->ghashtab[0]; i++) { in find_addr_by_gnu()
4944 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]); in find_addr_by_gnu()
4972 strings = p->strings; in dladdr()
4973 size_t addr_offset_so = addr - (size_t)p->base; in dladdr()
4975 info->dli_fname = p->name; in dladdr()
4976 info->dli_fbase = p->map; in dladdr()
4978 if (p->ghashtab) { in dladdr()
4986 info->dli_sname = 0; in dladdr()
4987 info->dli_saddr = 0; in dladdr()
4990 info->dli_sname = strings + match_sym->st_name; in dladdr()
4991 info->dli_saddr = (void *)laddr(p, match_sym->st_value); in dladdr()
5049 if (s[l-2]=='_' && s[l-1]=='r') { in __dlsym_redir_time64()
5050 l -= 2; in __dlsym_redir_time64()
5054 if (!strcmp(s+l-4, "time")) suffix = "64"; in __dlsym_redir_time64()
5074 info.dlpi_addr = (uintptr_t)current->base; in dl_iterate_phdr()
5075 info.dlpi_name = current->name; in dl_iterate_phdr()
5076 info.dlpi_phdr = current->phdr; in dl_iterate_phdr()
5077 info.dlpi_phnum = current->phnum; in dl_iterate_phdr()
5080 info.dlpi_tls_modid = current->tls_id; in dl_iterate_phdr()
5081 info.dlpi_tls_data = !current->tls_id ? 0 : in dl_iterate_phdr()
5082 __tls_get_addr((tls_mod_off_t[]){current->tls_id,0}); in dl_iterate_phdr()
5091 current = current->next; in dl_iterate_phdr()
5228 namespace ? namespace->ns_name : "NULL"); in handle_asan_path_open()
5230 if (fd == -1 && (namespace->asan_lib_paths || namespace->lib_paths)) { in handle_asan_path_open()
5231 if (namespace->lib_paths && namespace->asan_lib_paths) { in handle_asan_path_open()
5232 size_t newlen = strlen(namespace->asan_lib_paths) + strlen(namespace->lib_paths) + 2; in handle_asan_path_open()
5235 strcpy(new_lib_paths, namespace->asan_lib_paths); in handle_asan_path_open()
5237 strcat(new_lib_paths, namespace->lib_paths); in handle_asan_path_open()
5241 } else if (namespace->asan_lib_paths) { in handle_asan_path_open()
5242 fd_tmp = path_open(name, namespace->asan_lib_paths, buf, buf_size); in handle_asan_path_open()
5244 namespace->asan_lib_paths, in handle_asan_path_open()
5247 fd_tmp = path_open(name, namespace->lib_paths, buf, buf_size); in handle_asan_path_open()
5249 …"handle_asan_path_open path_open lib_paths:%{public}s ,fd: %{public}d.", namespace->lib_paths, fd_… in handle_asan_path_open()
5261 if ((extinfo->flag & ~(DL_EXT_VALID_FLAG_BITS)) != 0) { in dlopen_ext()
5262 LD_LOGW("Error dlopen_ext %{public}s: invalid flag %{public}x", file, extinfo->flag); in dlopen_ext()
5266 …LD_LOGI("dlopen_ext file:%{public}s, mode:%{public}x , caller_addr:%{public}p , extinfo->flag:%{pu… in dlopen_ext()
5270 extinfo ? extinfo->flag : 0); in dlopen_ext()
5277 char *buf = task->buf; in open_library_by_path()
5278 size_t buf_size = sizeof task->buf; in open_library_by_path()
5283 if (l-1 >= INT_MAX) return; in open_library_by_path()
5289 task->fd = z_info->fd; in open_library_by_path()
5290 task->file_offset = z_info->file_offset; in open_library_by_path()
5293 memset(z_info->path_buf, 0, sizeof(z_info->path_buf)); in open_library_by_path()
5296 if ((task->fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) break; in open_library_by_path()
5310 namespace ? namespace->ns_name : "NULL"); in handle_asan_path_open_by_task()
5311 if (fd == -1 && (namespace->asan_lib_paths || namespace->lib_paths)) { in handle_asan_path_open_by_task()
5312 if (namespace->lib_paths && namespace->asan_lib_paths) { in handle_asan_path_open_by_task()
5313 size_t newlen = strlen(namespace->asan_lib_paths) + strlen(namespace->lib_paths) + 2; in handle_asan_path_open_by_task()
5316 strcpy(new_lib_paths, namespace->asan_lib_paths); in handle_asan_path_open_by_task()
5318 strcat(new_lib_paths, namespace->lib_paths); in handle_asan_path_open_by_task()
5322 task->fd); in handle_asan_path_open_by_task()
5324 } else if (namespace->asan_lib_paths) { in handle_asan_path_open_by_task()
5325 open_library_by_path(name, namespace->asan_lib_paths, task, z_info); in handle_asan_path_open_by_task()
5327 namespace->asan_lib_paths, in handle_asan_path_open_by_task()
5328 task->fd); in handle_asan_path_open_by_task()
5330 open_library_by_path(name, namespace->lib_paths, task, z_info); in handle_asan_path_open_by_task()
5332 namespace->lib_paths, in handle_asan_path_open_by_task()
5333 task->fd); in handle_asan_path_open_by_task()
5348 * - path: x/xx/xxx.zip!/x/xx/xxx.so in open_uncompressed_library_in_zipfile()
5349 * - zipfile path: x/xx/xxx.zip in open_uncompressed_library_in_zipfile()
5350 * - library path in zipfile: x/xx/xxx.so */ in open_uncompressed_library_in_zipfile()
5351 if (strlcpy(z_info->path_buf, path, PATH_BUF_SIZE) >= PATH_BUF_SIZE) { in open_uncompressed_library_in_zipfile()
5353 return -1; in open_uncompressed_library_in_zipfile()
5355 z_info->path_buf[separator - path] = '\0'; in open_uncompressed_library_in_zipfile()
5356 z_info->file_path_index = separator - path + 2; in open_uncompressed_library_in_zipfile()
5357 char *zip_file_path = z_info->path_buf; in open_uncompressed_library_in_zipfile()
5358 char *lib_path = &z_info->path_buf[z_info->file_path_index]; in open_uncompressed_library_in_zipfile()
5361 return -1; in open_uncompressed_library_in_zipfile()
5370 return -1; in open_uncompressed_library_in_zipfile()
5375 return -1; in open_uncompressed_library_in_zipfile()
5378 if (zip_file_len == -1) { in open_uncompressed_library_in_zipfile()
5381 return -1; in open_uncompressed_library_in_zipfile()
5386 size_t end_locator_pos = zip_file_len - end_locator_len; in open_uncompressed_library_in_zipfile()
5390 return -1; in open_uncompressed_library_in_zipfile()
5395 return -1; in open_uncompressed_library_in_zipfile()
5405 return -1; in open_uncompressed_library_in_zipfile()
5410 return -1; in open_uncompressed_library_in_zipfile()
5416 return -1; in open_uncompressed_library_in_zipfile()
5423 return -1; in open_uncompressed_library_in_zipfile()
5428 return -1; in open_uncompressed_library_in_zipfile()
5433 return -1; in open_uncompressed_library_in_zipfile()
5436 z_info->file_offset = c_dir_entry.local_header_offset + sizeof(zip_file_header) + in open_uncompressed_library_in_zipfile()
5438 …if (zip_file_header.compression_method != COMPRESS_STORED || z_info->file_offset % PAGE_SIZE != 0)… in open_uncompressed_library_in_zipfile()
5441 lib_path, zip_file_path, zip_file_header.compression_method, z_info->file_offset); in open_uncompressed_library_in_zipfile()
5443 return -2; in open_uncompressed_library_in_zipfile()
5445 z_info->found = true; in open_uncompressed_library_in_zipfile()
5453 if (!z_info->found) { in open_uncompressed_library_in_zipfile()
5456 return -3; in open_uncompressed_library_in_zipfile()
5458 z_info->fd = fileno(zip_file); in open_uncompressed_library_in_zipfile()
5466 void *map = mmap(0, mapLen, PROT_READ, MAP_PRIVATE | MAP_XPM, task->fd, task->file_offset); in task_check_xpm()
5468 LD_LOGW("Xpm check failed for %{public}s, errno for mmap is: %{public}d", task->name, errno); in task_check_xpm()
5486 ssize_t l = pread(task->fd, task->ehdr_buf, sizeof task->ehdr_buf, task->file_offset); in map_library_header()
5487 task->eh = task->ehdr_buf; in map_library_header()
5489 …LD_LOGW("Error mapping header %{public}s: failed to read fd errno: %{public}d", task->name, errno); in map_library_header()
5492 if (l < sizeof(Ehdr) || (task->eh->e_type != ET_DYN && task->eh->e_type != ET_EXEC)) { in map_library_header()
5494 task->name, (int)l, task->eh->e_type); in map_library_header()
5497 task->phsize = task->eh->e_phentsize * task->eh->e_phnum; in map_library_header()
5498 if (task->phsize > sizeof task->ehdr_buf - sizeof(Ehdr)) { in map_library_header()
5499 task->allocated_buf = malloc(task->phsize); in map_library_header()
5500 if (!task->allocated_buf) { in map_library_header()
5501 …LD_LOGW("Error mapping header %{public}s: failed to alloc memory errno: %{public}d", task->name, e… in map_library_header()
5504 l = pread(task->fd, task->allocated_buf, task->phsize, task->eh->e_phoff + task->file_offset); in map_library_header()
5506 LD_LOGW("Error mapping header %{public}s: failed to pread errno: %{public}d", task->name, errno); in map_library_header()
5509 if (l != task->phsize) { in map_library_header()
5510 … LD_LOGW("Error mapping header %{public}s: unmatched phsize errno: %{public}d", task->name, errno); in map_library_header()
5513 ph = task->ph0 = task->allocated_buf; in map_library_header()
5514 } else if (task->eh->e_phoff + task->phsize > l) { in map_library_header()
5515 l = pread(task->fd, task->ehdr_buf + 1, task->phsize, task->eh->e_phoff + task->file_offset); in map_library_header()
5517 LD_LOGW("Error mapping header %{public}s: failed to pread errno: %{public}d", task->name, errno); in map_library_header()
5520 if (l != task->phsize) { in map_library_header()
5521 LD_LOGW("Error mapping header %{public}s: unmatched phsize", task->name); in map_library_header()
5524 ph = task->ph0 = (void *)(task->ehdr_buf + 1); in map_library_header()
5526 ph = task->ph0 = (void *)((char *)task->ehdr_buf + task->eh->e_phoff); in map_library_header()
5529 for (i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) { in map_library_header()
5530 if (ph->p_type == PT_DYNAMIC) { in map_library_header()
5531 task->dyn = ph->p_vaddr; in map_library_header()
5532 } else if (ph->p_type == PT_TLS) { in map_library_header()
5533 task->tls_image = ph->p_vaddr; in map_library_header()
5534 task->tls.align = ph->p_align; in map_library_header()
5535 task->tls.len = ph->p_filesz; in map_library_header()
5536 task->tls.size = ph->p_memsz; in map_library_header()
5539 if (ph->p_type != PT_DYNAMIC) { in map_library_header()
5543 off_start = ph->p_offset; in map_library_header()
5544 off_start &= -PAGE_SIZE; in map_library_header()
5545 task->dyn_map_len = ph->p_memsz + (ph->p_offset - off_start); in map_library_header()
5549 …task->dyn_map = mmap(0, task->dyn_map_len, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->fil… in map_library_header()
5550 if (task->dyn_map == MAP_FAILED) { in map_library_header()
5551 …or mapping header %{public}s: failed to map dynamic section errno: %{public}d", task->name, errno); in map_library_header()
5554 task->dyn_addr = (size_t *)((unsigned char *)task->dyn_map + (ph->p_offset - off_start)); in map_library_header()
5556 if (search_vec(task->dyn_addr, &dyn_tmp, DT_STRTAB)) { in map_library_header()
5559 LD_LOGW("Error mapping header %{public}s: DT_STRTAB not found", task->name); in map_library_header()
5562 if (search_vec(task->dyn_addr, &dyn_tmp, DT_STRSZ)) { in map_library_header()
5565 LD_LOGW("Error mapping header %{public}s: DT_STRSZ not found", task->name); in map_library_header()
5570 task->shsize = task->eh->e_shentsize * task->eh->e_shnum; in map_library_header()
5571 off_start = task->eh->e_shoff; in map_library_header()
5572 off_start &= -PAGE_SIZE; in map_library_header()
5573 task->shsize += task->eh->e_shoff - off_start; in map_library_header()
5574 …task->shdr_allocated_buf = mmap(0, task->shsize, PROT_READ, MAP_PRIVATE, task->fd, off_start + tas… in map_library_header()
5575 if (task->shdr_allocated_buf == MAP_FAILED) { in map_library_header()
5577 task->name, errno); in map_library_header()
5580 Shdr *sh = (Shdr *)((char *)task->shdr_allocated_buf + task->eh->e_shoff - off_start); in map_library_header()
5581 for (i = task->eh->e_shnum; i; i--, sh = (void *)((char *)sh + task->eh->e_shentsize)) { in map_library_header()
5582 if (sh->sh_type != SHT_STRTAB || sh->sh_addr != str_table || sh->sh_size != str_size) { in map_library_header()
5585 off_start = sh->sh_offset; in map_library_header()
5586 off_start &= -PAGE_SIZE; in map_library_header()
5587 task->str_map_len = sh->sh_size + (sh->sh_offset - off_start); in map_library_header()
5588 …task->str_map = mmap(0, task->str_map_len, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->fil… in map_library_header()
5589 if (task->str_map == MAP_FAILED) { in map_library_header()
5591 task->name, errno); in map_library_header()
5594 task->str_addr = (char *)task->str_map + sh->sh_offset - off_start; in map_library_header()
5597 if (!task->dyn) { in map_library_header()
5598 LD_LOGW("Error mapping header %{public}s: dynamic section not found", task->name); in map_library_header()
5601 if (task->shdr_allocated_buf != MAP_FAILED) { in map_library_header()
5602 munmap(task->shdr_allocated_buf, task->shsize); in map_library_header()
5603 task->shdr_allocated_buf = MAP_FAILED; in map_library_header()
5609 free(task->allocated_buf); in map_library_header()
5610 task->allocated_buf = NULL; in map_library_header()
5611 if (task->shdr_allocated_buf != MAP_FAILED) { in map_library_header()
5612 munmap(task->shdr_allocated_buf, task->shsize); in map_library_header()
5613 task->shdr_allocated_buf = MAP_FAILED; in map_library_header()
5624 Phdr *ph = task->ph0; in task_map_library()
5636 for (i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) { in task_map_library()
5637 if (ph->p_type == PT_GNU_RELRO) { in task_map_library()
5638 task->p->relro_start = ph->p_vaddr & -PAGE_SIZE; in task_map_library()
5639 task->p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE; in task_map_library()
5640 } else if (ph->p_type == PT_GNU_STACK) { in task_map_library()
5641 if (!runtime && ph->p_memsz > __default_stacksize) { in task_map_library()
5643 ph->p_memsz < DEFAULT_STACK_MAX ? in task_map_library()
5644 ph->p_memsz : DEFAULT_STACK_MAX; in task_map_library()
5646 } else if (ph->p_type == PT_OHOS_CFI_MODIFIER) { in task_map_library()
5647 task->p->modifier_begin = ph->p_vaddr; in task_map_library()
5648 task->p->modifier_end = ph->p_vaddr + ph->p_memsz; in task_map_library()
5653 if (ph->p_type == PT_GNU_PROPERTY || ph->p_type == PT_NOTE) { in task_map_library()
5654 ext_prot |= parse_extra_prot_fd(task->fd, ph); in task_map_library()
5657 if (ph->p_type != PT_LOAD) { in task_map_library()
5661 if (ph->p_vaddr < addr_min) { in task_map_library()
5662 addr_min = ph->p_vaddr; in task_map_library()
5663 off_start = ph->p_offset; in task_map_library()
5664 prot = (((ph->p_flags & PF_R) ? PROT_READ : 0) | in task_map_library()
5665 ((ph->p_flags & PF_W) ? PROT_WRITE : 0) | in task_map_library()
5666 ((ph->p_flags & PF_X) ? PROT_EXEC : 0)); in task_map_library()
5668 if (ph->p_flags & PF_X) { in task_map_library()
5673 if (ph->p_vaddr + ph->p_memsz > addr_max) { in task_map_library()
5674 addr_max = ph->p_vaddr + ph->p_memsz; in task_map_library()
5677 if (!task->dyn) { in task_map_library()
5678 …LD_LOGW("Error mapping library: !task->dyn dynamic section not found task->name=%{public}s", task- in task_map_library()
5681 if (DL_FDPIC && !(task->eh->e_flags & FDPIC_CONSTDISP_FLAG)) { in task_map_library()
5682 task->p->loadmap = calloc(1, sizeof(struct fdpic_loadmap) + nsegs * sizeof(struct fdpic_loadseg)); in task_map_library()
5683 if (!task->p->loadmap) { in task_map_library()
5687 task->p->loadmap->nsegs = nsegs; in task_map_library()
5688 for (ph = task->ph0, i = 0; i < nsegs; ph = (void *)((char *)ph + task->eh->e_phentsize)) { in task_map_library()
5689 if (ph->p_type != PT_LOAD) { in task_map_library()
5692 prot = (((ph->p_flags & PF_R) ? PROT_READ : 0) | in task_map_library()
5693 ((ph->p_flags & PF_W) ? PROT_WRITE : 0) | in task_map_library()
5694 ((ph->p_flags & PF_X) ? PROT_EXEC : 0)); in task_map_library()
5696 if (ph->p_flags & PF_X) { in task_map_library()
5700 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE - 1), in task_map_library()
5702 task->fd, ph->p_offset & -PAGE_SIZE + task->file_offset); in task_map_library()
5704 unmap_library(task->p); in task_map_library()
5705 …LD_LOGW("Error mapping library: PT_LOAD mmap failed task->name=%{public}s errno=%{public}d map_len… in task_map_library()
5706 task->name, errno, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE - 1)); in task_map_library()
5709 task->p->loadmap->segs[i].addr = (size_t)map + in task_map_library()
5710 (ph->p_vaddr & PAGE_SIZE - 1); in task_map_library()
5711 task->p->loadmap->segs[i].p_vaddr = ph->p_vaddr; in task_map_library()
5712 task->p->loadmap->segs[i].p_memsz = ph->p_memsz; in task_map_library()
5715 size_t brk = (ph->p_vaddr & PAGE_SIZE - 1) + ph->p_filesz; in task_map_library()
5716 size_t pgbrk = (brk + PAGE_SIZE - 1) & -PAGE_SIZE; in task_map_library()
5717 size_t pgend = (brk + ph->p_memsz - ph->p_filesz + PAGE_SIZE - 1) & -PAGE_SIZE; in task_map_library()
5719 pgend - pgbrk, prot, in task_map_library()
5721 -1, off_start) == MAP_FAILED) in task_map_library()
5724 memset(map + brk, 0, pgbrk - brk); in task_map_library()
5727 map = (void *)task->p->loadmap->segs[0].addr; in task_map_library()
5731 addr_max += PAGE_SIZE - 1; in task_map_library()
5732 addr_max &= -PAGE_SIZE; in task_map_library()
5733 addr_min &= -PAGE_SIZE; in task_map_library()
5734 off_start &= -PAGE_SIZE; in task_map_library()
5735 map_len = addr_max - addr_min + off_start; in task_map_library()
5740 size_t maxinum_alignment = phdr_table_get_maxinum_alignment(task->ph0, task->eh->e_phnum); in task_map_library()
5746 if (map_len > reserved_params->reserved_size) { in task_map_library()
5747 if (reserved_params->must_use_reserved) { in task_map_library()
5748 …Error mapping library: map len is larger than reserved address task->name=%{public}s", task->name); in task_map_library()
5752 start_addr = ((size_t)reserved_params->start_addr - 1 + PAGE_SIZE) & -PAGE_SIZE; in task_map_library()
5761 size_t tmp_map_len = ALIGN(map_len, mapping_align) + mapping_align - PAGE_SIZE; in task_map_library()
5769 …void *)start_addr, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) in task_map_library()
5770 : mmap((void *)start_addr, map_len, prot, map_flags, task->fd, off_start + task->file_offset); in task_map_library()
5773 " task->fd=%{public}d task->name=%{public}s map_len=%{public}lu", in task_map_library()
5774 errno, DL_NOMMU_SUPPORT, task->fd, task->name, map_len); in task_map_library()
5777 if (reserved_params && map_len < reserved_params->reserved_size) { in task_map_library()
5778 reserved_params->reserved_size -= (map_len + (start_addr - (size_t)reserved_params->start_addr)); in task_map_library()
5779 reserved_params->start_addr = (void *)((uint8_t *)map + map_len); in task_map_library()
5784 …ed char *temp_map = mmap((void *)NULL, tmp_map_len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); in task_map_library()
5795 ? mmap(real_map, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) in task_map_library()
5797 : mmap(real_map, map_len, prot, map_flags | MAP_FIXED, task->fd, off_start + task->file_offset); in task_map_library()
5800 "task->fd=%{public}d task->name=%{public}s map_len=%{public}lu", in task_map_library()
5801 errno, DL_NOMMU_SUPPORT, task->fd, task->name, map_len); in task_map_library()
5806 * |--------------------------tmp_map_len--------------------------| in task_map_library()
5808 * |---unused_part_1---|---------map_len-------|---unused_part_2---| in task_map_library()
5812 size_t unused_part_1 = real_map - temp_map; in task_map_library()
5813 size_t unused_part_2 = temp_map_end - (real_map + map_len); in task_map_library()
5816 if (res1 == -1) { in task_map_library()
5823 if (res2 == -1) { in task_map_library()
5828 task->p->map = map; in task_map_library()
5829 task->p->map_len = map_len; in task_map_library()
5832 if (task->eh->e_type != ET_DYN && addr_min && map != (void *)addr_min) { in task_map_library()
5833 LD_LOGW("Error mapping library: ET_DYN task->name=%{public}s", task->name); in task_map_library()
5837 base = map - addr_min; in task_map_library()
5838 task->p->phdr = 0; in task_map_library()
5839 task->p->phnum = 0; in task_map_library()
5840 …for (ph = task->ph0, i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsi… in task_map_library()
5841 if (ph->p_type == PT_OHOS_RANDOMDATA) { in task_map_library()
5842 fill_random_data((void *)(ph->p_vaddr + base), ph->p_memsz); in task_map_library()
5845 if (ph->p_type != PT_LOAD) { in task_map_library()
5850 if (!task->p->phdr && task->eh->e_phoff >= ph->p_offset in task_map_library()
5851 && task->eh->e_phoff + task->phsize <= ph->p_offset + ph->p_filesz) { in task_map_library()
5852 task->p->phdr = (void *)(base + ph->p_vaddr + (task->eh->e_phoff - ph->p_offset)); in task_map_library()
5853 task->p->phnum = task->eh->e_phnum; in task_map_library()
5854 task->p->phentsize = task->eh->e_phentsize; in task_map_library()
5856 this_min = ph->p_vaddr & -PAGE_SIZE; in task_map_library()
5857 this_max = ph->p_vaddr + ph->p_memsz + PAGE_SIZE - 1 & -PAGE_SIZE; in task_map_library()
5858 off_start = ph->p_offset & -PAGE_SIZE; in task_map_library()
5859 prot = (((ph->p_flags & PF_R) ? PROT_READ : 0) | in task_map_library()
5860 ((ph->p_flags & PF_W) ? PROT_WRITE : 0) | in task_map_library()
5861 ((ph->p_flags & PF_X) ? PROT_EXEC : 0)); in task_map_library()
5863 if (ph->p_flags & PF_X) { in task_map_library()
5867 /* Reuse the existing mapping for the lowest-address LOAD */ in task_map_library()
5870 this_max - this_min, in task_map_library()
5872 task->fd, in task_map_library()
5873 off_start + task->file_offset) == MAP_FAILED) { in task_map_library()
5874 …LD_LOGW("Error mapping library: mmap fix failed task->name=%{public}s errno=%{public}d", task->nam… in task_map_library()
5877 if ((ph->p_flags & PF_X) && (ph->p_align == KPMD_SIZE) && hugepage_enabled) in task_map_library()
5878 madvise(base + this_min, this_max - this_min, MADV_HUGEPAGE); in task_map_library()
5879 if (ph->p_memsz > ph->p_filesz && (ph->p_flags & PF_W)) { in task_map_library()
5880 size_t brk = (size_t)base + ph->p_vaddr + ph->p_filesz; in task_map_library()
5881 size_t pgbrk = brk + PAGE_SIZE - 1 & -PAGE_SIZE; in task_map_library()
5882 size_t zeromap_size = (size_t)base + this_max - pgbrk; in task_map_library()
5883 memset((void *)brk, 0, pgbrk - brk & PAGE_SIZE - 1); in task_map_library()
5884 if (pgbrk - (size_t)base < this_max && mmap_fixed( in task_map_library()
5889 -1, in task_map_library()
5891 …LD_LOGW("Error mapping library: PF_W mmap fix failed errno=%{public}d task->name=%{public}s zeroma… in task_map_library()
5892 errno, task->name, zeromap_size); in task_map_library()
5895 set_bss_vma_name(task->p->name, (void *)pgbrk, zeromap_size); in task_map_library()
5898 for (i = 0; ((size_t *)(base + task->dyn))[i]; i += NEXT_DYNAMIC_INDEX) { in task_map_library()
5899 if (((size_t *)(base + task->dyn))[i] == DT_TEXTREL) { in task_map_library()
5901 …LD_LOGW("Error mapping library: mprotect failed task->name=%{public}s errno=%{public}d", task->nam… in task_map_library()
5908 task->p->base = base; in task_map_library()
5909 task->p->dynv = laddr(task->p, task->dyn); in task_map_library()
5910 if (task->p->tls.size) { in task_map_library()
5911 task->p->tls.image = laddr(task->p, task->tls_image); in task_map_library()
5913 free(task->allocated_buf); in task_map_library()
5914 task->allocated_buf = NULL; in task_map_library()
5915 if (task->p->modifier_begin && task->p->modifier_end) { in task_map_library()
5916 add_pac_info(task->p); in task_map_library()
5923 unmap_library(task->p); in task_map_library()
5925 free(task->allocated_buf); in task_map_library()
5926 task->allocated_buf = NULL; in task_map_library()
5935 int ret = snprintf(proc_self_fd, sizeof(proc_self_fd), "/proc/self/fd/%d", task->fd); in resolve_fd_to_realpath()
5939 ssize_t len = readlink(proc_self_fd, resolved_path, sizeof(resolved_path) - 1); in resolve_fd_to_realpath()
5944 strncpy(task->buf, resolved_path, PATH_MAX); in resolve_fd_to_realpath()
5951 const char *name = task->name; in load_library_header()
5952 struct dso *needed_by = task->needed_by; in load_library_header()
5953 ns_t *namespace = task->namespace; in load_library_header()
5954 bool check_inherited = task->check_inherited; in load_library_header()
5975 if (strncmp(name + NAME_INDEX_THREE, rp, next - rp) == 0) { in load_library_header()
5984 unsigned mask = 1U << (rp - reserved); in load_library_header()
6000 tail->next = &ldso; in load_library_header()
6006 task->isloaded = true; in load_library_header()
6007 task->p = &ldso; in load_library_header()
6015 task->pathname = name; in load_library_header()
6016 if (!is_accessible(namespace, task->pathname, g_is_asan, check_inherited)) { in load_library_header()
6018 task->pathname, namespace ? namespace->ns_name : "NULL"); in load_library_header()
6019 task->fd = -1; in load_library_header()
6021 task->fd = z_info.fd; in load_library_header()
6022 task->file_offset = z_info.file_offset; in load_library_header()
6029 task->pathname = name; in load_library_header()
6030 if (!is_accessible(namespace, task->pathname, g_is_asan, check_inherited)) { in load_library_header()
6032 task->pathname, namespace ? namespace->ns_name : "NULL"); in load_library_header()
6033 task->fd = -1; in load_library_header()
6035 task->fd = open(name, O_RDONLY | O_CLOEXEC); in load_library_header()
6041 task->p = find_library_by_name(name, namespace, check_inherited); in load_library_header()
6042 if (task->p) { in load_library_header()
6043 task->isloaded = true; in load_library_header()
6045 name, namespace->ns_name, task->p->name, task->p->namespace->ns_name); in load_library_header()
6052 task->fd = -1; in load_library_header()
6053 if (namespace->env_paths) { in load_library_header()
6054 open_library_by_path(name, namespace->env_paths, task, &z_info); in load_library_header()
6056 for (task->p = needed_by; task->fd == -1 && task->p; task->p = task->p->needed_by) { in load_library_header()
6057 if (fixup_rpath(task->p, task->buf, sizeof task->buf) < 0) { in load_library_header()
6058 task->fd = INVALID_FD_INHIBIT_FURTHER_SEARCH; /* Inhibit further search. */ in load_library_header()
6060 if (task->p->rpath) { in load_library_header()
6061 open_library_by_path(name, task->p->rpath, task, &z_info); in load_library_header()
6062 if (task->fd != -1 && resolve_fd_to_realpath(task)) { in load_library_header()
6063 if (!is_accessible(namespace, task->buf, g_is_asan, check_inherited)) { in load_library_header()
6065 name, namespace ? namespace->ns_name : "NULL"); in load_library_header()
6066 close(task->fd); in load_library_header()
6067 task->fd = -1; in load_library_header()
6073 handle_asan_path_open_by_task(task->fd, name, namespace, task, &z_info); in load_library_header()
6074 LD_LOGD("load_library handle_asan_path_open_by_task fd:%{public}d.", task->fd); in load_library_header()
6076 if (task->fd == -1 && namespace->lib_paths) { in load_library_header()
6077 open_library_by_path(name, namespace->lib_paths, task, &z_info); in load_library_header()
6078 LD_LOGD("load_library no asan lib_paths path_open fd:%{public}d.", task->fd); in load_library_header()
6081 task->pathname = task->buf; in load_library_header()
6083 if (task->fd < 0) { in load_library_header()
6084 if (!check_inherited || !namespace->ns_inherits) { in load_library_header()
6086 task->name, namespace->ns_name, errno); in load_library_header()
6091 for (size_t i = 0; i < namespace->ns_inherits->num; i++) { in load_library_header()
6092 ns_inherit *inherit = namespace->ns_inherits->inherits[i]; in load_library_header()
6096 task->namespace = inherit->inherited_ns; in load_library_header()
6097 task->check_inherited = false; in load_library_header()
6103 task->name, namespace->ns_name, topLayerErrno); in load_library_header()
6107 if (fstat(task->fd, &st) < 0) { in load_library_header()
6108 …LD_LOGW("Error loading header %{public}s: failed to get file state errno=%{public}d", task->name, … in load_library_header()
6109 close(task->fd); in load_library_header()
6110 task->fd = -1; in load_library_header()
6114 task->p = find_library_by_fstat(&st, namespace, check_inherited, task->file_offset); in load_library_header()
6115 if (task->p) { in load_library_header()
6119 if (!task->p->shortname && task->pathname != name) { in load_library_header()
6120 task->p->shortname = strrchr(task->p->name, '/') + 1; in load_library_header()
6122 close(task->fd); in load_library_header()
6123 task->fd = -1; in load_library_header()
6124 task->isloaded = true; in load_library_header()
6126 name, namespace->ns_name, task->p->name, task->p->namespace->ns_name); in load_library_header()
6132 LD_LOGW("Error loading header %{public}s: failed to map header", task->name); in load_library_header()
6133 close(task->fd); in load_library_header()
6134 task->fd = -1; in load_library_header()
6139 * storage must include a reservation for all pre-existing in load_library_header()
6142 * the newly-loaded DSO. */ in load_library_header()
6143 alloc_size = sizeof(struct dso) + strlen(task->pathname) + 1; in load_library_header()
6144 if (runtime && task->tls.size) { in load_library_header()
6145 size_t per_th = task->tls.size + task->tls.align + sizeof(void *) * (tls_cnt + TLS_CNT_INCREASE); in load_library_header()
6153 task->p = calloc(1, alloc_size); in load_library_header()
6154 if (!task->p) { in load_library_header()
6155 LD_LOGW("Error loading header %{public}s: failed to allocate dso", task->name); in load_library_header()
6156 close(task->fd); in load_library_header()
6157 task->fd = -1; in load_library_header()
6160 task->p->dev = st.st_dev; in load_library_header()
6161 task->p->ino = st.st_ino; in load_library_header()
6162 task->p->file_offset = task->file_offset; in load_library_header()
6163 task->p->needed_by = needed_by; in load_library_header()
6164 task->p->name = task->p->buf; in load_library_header()
6165 strcpy(task->p->name, task->pathname); in load_library_header()
6166 task->p->tls = task->tls; in load_library_header()
6167 task->p->dynv = task->dyn_addr; in load_library_header()
6168 task->p->strings = task->str_addr; in load_library_header()
6171 if (search_vec(task->p->dynv, &rpath_offset, DT_RPATH)) in load_library_header()
6172 task->p->rpath_orig = task->p->strings + rpath_offset; in load_library_header()
6173 if (search_vec(task->p->dynv, &runpath_offset, DT_RUNPATH)) in load_library_header()
6174 task->p->rpath_orig = task->p->strings + runpath_offset; in load_library_header()
6177 if (task->pathname != name) { in load_library_header()
6178 task->p->shortname = strrchr(task->p->name, '/') + 1; in load_library_header()
6181 if (task->p->tls.size) { in load_library_header()
6182 task->p->tls_id = ++tls_cnt; in load_library_header()
6183 task->p->new_dtv = (void *)(-sizeof(size_t) & in load_library_header()
6184 (uintptr_t)(task->p->name + strlen(task->p->name) + sizeof(size_t))); in load_library_header()
6185 task->p->new_tls = (void *)(task->p->new_dtv + n_th * (tls_cnt + 1)); in load_library_header()
6188 tail->next = task->p; in load_library_header()
6189 task->p->prev = tail; in load_library_header()
6190 tail = task->p; in load_library_header()
6193 task->p->namespace = namespace; in load_library_header()
6194 ns_add_dso(namespace, task->p); in load_library_header()
6200 …ing ns=%{public}s name=%{public}s by_dlopen=%{public}d", task->namespace->ns_name, task->p->name, … in task_load_library()
6202 __close(task->fd); in task_load_library()
6203 task->fd = -1; in task_load_library()
6206 task->name, noload, errno); in task_load_library()
6207 …error("Error loading library %s: failed to map library noload=%d errno=%d", task->name, noload, er… in task_load_library()
6217 * false positives from interposition-hack libraries. */ in task_load_library()
6218 decode_dyn(task->p); in task_load_library()
6219 if (find_sym(task->p, "__libc_start_main", 1).sym && in task_load_library()
6220 find_sym(task->p, "stdin", 1).sym) { in task_load_library()
6221 do_dlclose(task->p, 0); in task_load_library()
6222 task->p = NULL; in task_load_library()
6223 free((void*)task->name); in task_load_library()
6224 task->name = ld_strdup("libc.so"); in task_load_library()
6225 task->check_inherited = true; in task_load_library()
6227 LD_LOGW("failed to load %{public}s: failed to load libc.so", task->name); in task_load_library()
6228 error("failed to load %s: failed to load libc.so", task->name); in task_load_library()
6239 reclaim_gaps(task->p); in task_load_library()
6241 task->p->runtime_loaded = runtime; in task_load_library()
6243 task->p->by_dlopen = 1; in task_load_library()
6248 makefuncdescs(task->p); in task_load_library()
6252 dprintf(1, "\t%s => %s (%p)\n", task->name, task->pathname, task->p->base); in task_load_library()
6257 libc.load_hook((long unsigned int)task->p->base, task->p->phdr, task->p->phnum); in task_load_library()
6265 if (p->deps) { in preload_direct_deps()
6268 /* For head, all preloads are direct pseudo-dependencies. in preload_direct_deps()
6271 for (struct dso *q = p->next; q; q = q->next) { in preload_direct_deps()
6275 for (i = 0; p->dynv[i]; i += NEXT_DYNAMIC_INDEX) { in preload_direct_deps()
6276 if (p->dynv[i] == DT_NEEDED) { in preload_direct_deps()
6282 p->deps = (p == head && cnt < MIN_DEPS_COUNT) ? builtin_deps : in preload_direct_deps()
6283 calloc(cnt + 1, sizeof *p->deps); in preload_direct_deps()
6284 if (!p->deps) { in preload_direct_deps()
6285 LD_LOGW("Error loading dependencies for %{public}s", p->name); in preload_direct_deps()
6286 error("Error loading dependencies for %s", p->name); in preload_direct_deps()
6293 for (struct dso *q = p->next; q; q = q->next) { in preload_direct_deps()
6294 p->deps[cnt++] = q; in preload_direct_deps()
6297 for (i = 0; p->dynv[i]; i += NEXT_DYNAMIC_INDEX) { in preload_direct_deps()
6298 if (p->dynv[i] != DT_NEEDED) { in preload_direct_deps()
6301 const char* dtneed_name = p->strings + p->dynv[i + 1]; in preload_direct_deps()
6302 …}s adding DT_NEEDED task %{public}s namespace(%{public}s)", p->name, dtneed_name, namespace->ns_na… in preload_direct_deps()
6305 LD_LOGW("Error loading dependencies %{public}s : create load task failed", p->name); in preload_direct_deps()
6306 error("Error loading dependencies for %s : create load task failed", p->name); in preload_direct_deps()
6312 …D("loading shared library %{public}s: (needed by %{public}s)", p->strings + p->dynv[i+1], p->name); in preload_direct_deps()
6317 p->strings + p->dynv[i + 1], in preload_direct_deps()
6318 p->name); in preload_direct_deps()
6320 p->strings + p->dynv[i + 1], p->name); in preload_direct_deps()
6326 p->deps[cnt++] = task->p; in preload_direct_deps()
6327 if (task->isloaded) { in preload_direct_deps()
6334 p->deps[cnt] = 0; in preload_direct_deps()
6335 p->ndeps_direct = cnt; in preload_direct_deps()
6336 for (i = 0; i < p->ndeps_direct; i++) { in preload_direct_deps()
6337 add_dso_parent(p->deps[i], p); in preload_direct_deps()
6344 for (size_t i = 0; i < tasks->length; i++) { in unmap_preloaded_sections()
6349 if (task->dyn_map_len) { in unmap_preloaded_sections()
6350 munmap(task->dyn_map, task->dyn_map_len); in unmap_preloaded_sections()
6351 task->dyn_map = NULL; in unmap_preloaded_sections()
6352 task->dyn_map_len = 0; in unmap_preloaded_sections()
6353 if (task->p) { in unmap_preloaded_sections()
6354 task->p->dynv = NULL; in unmap_preloaded_sections()
6357 if (task->str_map_len) { in unmap_preloaded_sections()
6358 munmap(task->str_map, task->str_map_len); in unmap_preloaded_sections()
6359 task->str_map = NULL; in unmap_preloaded_sections()
6360 task->str_map_len = 0; in unmap_preloaded_sections()
6361 if (task->p) { in unmap_preloaded_sections()
6362 task->p->strings = NULL; in unmap_preloaded_sections()
6370 if (p->deps) { in preload_deps()
6373 for (; p; p = p->next) { in preload_deps()
6374 preload_direct_deps(p, p->namespace, tasks); in preload_deps()
6382 for (size_t i = 0; i < tasks->length; i++) { in run_loadtasks()
6386 …reserved_address = reserved_params->reserved_address_recursive || (reserved_params->target == task in run_loadtasks()
6396 if (p->tls.image) { in assign_tls()
6397 tls_align = MAXP2(tls_align, p->tls.align); in assign_tls()
6399 p->tls.offset = tls_offset + ((p->tls.align - 1) & in assign_tls()
6400 (-tls_offset + (uintptr_t)p->tls.image)); in assign_tls()
6401 tls_offset = p->tls.offset + p->tls.size; in assign_tls()
6403 tls_offset += p->tls.size + p->tls.align - 1; in assign_tls()
6404 tls_offset -= (tls_offset + (uintptr_t)p->tls.image) in assign_tls()
6405 & (p->tls.align - 1); in assign_tls()
6406 p->tls.offset = tls_offset; in assign_tls()
6409 tls_tail->next = &p->tls; in assign_tls()
6411 libc.tls_head = &p->tls; in assign_tls()
6413 tls_tail = &p->tls; in assign_tls()
6416 p = p->next; in assign_tls()
6440 if (!task->isloaded) { in load_preload()
6455 ssize_t count = dso->relro_end - dso->relro_start; in serialize_gnu_relro()
6458 ssize_t write_size = TEMP_FAILURE_RETRY(write(fd, laddr(dso, dso->relro_start + offset), count)); in serialize_gnu_relro()
6459 if (-1 == write_size) { in serialize_gnu_relro()
6460 LD_LOGW("Error serializing relro %{public}s: failed to write GNU_RELRO", dso->name); in serialize_gnu_relro()
6461 return -1; in serialize_gnu_relro()
6464 count -= write_size; in serialize_gnu_relro()
6467 ssize_t size = dso->relro_end - dso->relro_start; in serialize_gnu_relro()
6469 laddr(dso, dso->relro_start), in serialize_gnu_relro()
6476 LD_LOGW("Error serializing relro %{public}s: failed to map GNU_RELRO", dso->name); in serialize_gnu_relro()
6477 return -1; in serialize_gnu_relro()
6488 LD_LOGW("Error mapping relro %{public}s: failed to get file state", dso->name); in map_gnu_relro()
6489 return -1; in map_gnu_relro()
6496 LD_LOGW("Error mapping relro %{public}s: failed to map fd", dso->name); in map_gnu_relro()
6497 return -1; in map_gnu_relro()
6501 char *mem_base = (char *)(laddr(dso, dso->relro_start)); in map_gnu_relro()
6503 ssize_t size = dso->relro_end - dso->relro_start; in map_gnu_relro()
6505 if (size > ext_fd_file_size - *file_offset) { in map_gnu_relro()
6506 LD_LOGW("Error mapping relro %{public}s: invalid file size", dso->name); in map_gnu_relro()
6507 return -1; in map_gnu_relro()
6528 ssize_t map_length = end_offset - start_offset; in map_gnu_relro()
6534 LD_LOGW("Error mapping relro %{public}s: failed to map GNU_RELRO", dso->name); in map_gnu_relro()
6536 return -1; in map_gnu_relro()
6552 if (extinfo->flag & DL_EXT_WRITE_RELRO) { in handle_relro_sharing()
6553 LD_LOGD("Serializing GNU_RELRO %{public}s", p->name); in handle_relro_sharing()
6554 if (serialize_gnu_relro(extinfo->relro_fd, p, relro_fd_offset) < 0) { in handle_relro_sharing()
6555 LD_LOGW("Error serializing GNU_RELRO %{public}s", p->name); in handle_relro_sharing()
6559 } else if (extinfo->flag & DL_EXT_USE_RELRO) { in handle_relro_sharing()
6560 LD_LOGD("Mapping GNU_RELRO %{public}s", p->name); in handle_relro_sharing()
6561 if (map_gnu_relro(extinfo->relro_fd, p, relro_fd_offset) < 0) { in handle_relro_sharing()
6562 LD_LOGW("Error mapping GNU_RELRO %{public}s", p->name); in handle_relro_sharing()
6589 Phdr *ph = p->phdr; in find_and_set_bss_name()
6590 for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) { in find_and_set_bss_name()
6591 if (ph->p_type != PT_LOAD) continue; in find_and_set_bss_name()
6592 size_t seg_start = p->base + ph->p_vaddr; in find_and_set_bss_name()
6593 size_t seg_file_end = seg_start + ph->p_filesz + PAGE_SIZE - 1 & -PAGE_SIZE; in find_and_set_bss_name()
6594 size_t seg_max_addr = seg_start + ph->p_memsz + PAGE_SIZE - 1 & -PAGE_SIZE; in find_and_set_bss_name()
6595 size_t zeromap_size = seg_max_addr - seg_file_end; in find_and_set_bss_name()
6596 if (zeromap_size > 0 && (ph->p_flags & PF_W)) { in find_and_set_bss_name()
6597 set_bss_vma_name(p->name, (void *)seg_file_end, zeromap_size); in find_and_set_bss_name()
6639 for (struct dso *so = p; so != NULL; so = so->next) { in add_dso_info_to_debug_map()
6642 LD_LOGW("malloc error! dso name: %{public}s.", so->name); in add_dso_info_to_debug_map()
6646 debug_info->loadmap = so->loadmap; in add_dso_info_to_debug_map()
6648 debug_info->base = so->base; in add_dso_info_to_debug_map()
6650 debug_info->name = so->name; in add_dso_info_to_debug_map()
6651 debug_info->dynv = so->dynv; in add_dso_info_to_debug_map()
6653 debug_info->prev = NULL; in add_dso_info_to_debug_map()
6654 debug_info->next = NULL; in add_dso_info_to_debug_map()
6657 debug_info->prev = debug_tail; in add_dso_info_to_debug_map()
6658 debug_info->next = NULL; in add_dso_info_to_debug_map()
6659 debug_tail->next = debug_info; in add_dso_info_to_debug_map()
6662 so->debug_info = debug_info; in add_dso_info_to_debug_map()
6668 struct dso_debug_info *debug_info = p->debug_info; in remove_dso_info_from_debug_map()
6670 debug_tail = debug_tail->prev; in remove_dso_info_from_debug_map()
6671 debug_tail->next = NULL; in remove_dso_info_from_debug_map()
6673 debug_info->next->prev = debug_info->prev; in remove_dso_info_from_debug_map()
6674 debug_info->prev->next = debug_info->next; in remove_dso_info_from_debug_map()
6692 if (cur->dso_handle == dso_handle) { in find_dso_handle_node()
6695 cur =cur->next; in find_dso_handle_node()
6711 node->count++; in add_dso_handle_node()
6712 …a_thread] increase dso node count of %{public}s, count:%{public}d ", node->dso->name, node->count); in add_dso_handle_node()
6733 if (p->by_dlopen) { in add_dso_handle_node()
6734 p->nr_dlopen++; in add_dso_handle_node()
6736 p->name, p->name, p->nr_dlopen); in add_dso_handle_node()
6737 if (p->bfs_built) { in add_dso_handle_node()
6738 for (size_t i = 0; p->deps[i]; i++) { in add_dso_handle_node()
6739 p->deps[i]->nr_dlopen++; in add_dso_handle_node()
6741 p->deps[i]->name, p->name, p->deps[i]->nr_dlopen); in add_dso_handle_node()
6746 for (size_t i = 0; p->deps_all[i]; i++) { in add_dso_handle_node()
6747 p->deps_all[i]->nr_dlopen++; in add_dso_handle_node()
6749 p->deps_all[i]->name, p->name, p->deps_all[i]->nr_dlopen); in add_dso_handle_node()
6753 cur->dso = p; in add_dso_handle_node()
6754 cur->dso_handle = dso_handle; in add_dso_handle_node()
6755 cur->count = 1; in add_dso_handle_node()
6756 cur->next = dso_handle_list; in add_dso_handle_node()
6773 if (node && node->count) { in remove_dso_handle_node()
6774 …read] decrease dso node count of %{public}s, count:%{public}d ", node->dso->name, node->count - 1); in remove_dso_handle_node()
6775 if ((--node->count) == 0) { in remove_dso_handle_node()
6776 LD_LOGD("[cxa_thread] call do_dlclose(%{public}s) when count is 0", node->dso->name); in remove_dso_handle_node()
6777 do_dlclose(node->dso, 1); in remove_dso_handle_node()
6779 node->dso_handle = NULL; in remove_dso_handle_node()
6785 dso_handle, node ? "not" : "is", node ? node->count : 0); in remove_dso_handle_node()
6787 dso_handle, node ? "not" : "is", node ? node->count : 0); in remove_dso_handle_node()