Lines Matching defs:task

185 static bool map_library_header(struct loadtask *task);
186 static bool task_map_library(struct loadtask *task, struct reserved_address_params *reserved_params);
187 static bool resolve_fd_to_realpath(struct loadtask *task);
188 static bool load_library_header(struct loadtask *task);
189 static void task_load_library(struct loadtask *task, struct reserved_address_params *reserved_params);
196 static void open_library_by_path(const char *name, const char *s, struct loadtask *task, struct zip_info *z_info);
197 static void handle_asan_path_open_by_task(int fd, const char *name, ns_t *namespace, struct loadtask *task, struct zip_info *z_info);
3325 struct loadtask *task = NULL;
3428 task = create_loadtask(file, head, ns, true);
3429 if (!task) {
3434 if (!load_library_header(task)) {
3439 LD_LOGE("dlopen_impl load library header failed for %{public}s", task->name);
3444 reserved_params.target = task->p;
3447 if (!task->p) {
3448 LD_LOGE("dlopen_impl load library failed for %{public}s", task->name);
3456 if (!task->isloaded) {
3457 is_task_appended = append_loadtasks(tasks, task);
3459 preload_deps(task->p, tasks);
3465 p = task->p;
3466 if (!task->isloaded) {
3470 free_task(task);
3471 task = NULL;
3559 free_task(task);
4519 static void open_library_by_path(const char *name, const char *s, struct loadtask *task, struct zip_info *z_info)
4521 char *buf = task->buf;
4522 size_t buf_size = sizeof task->buf;
4533 task->fd = z_info->fd;
4534 task->file_offset = z_info->file_offset;
4540 if ((task->fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) break;
4548 static void handle_asan_path_open_by_task(int fd, const char *name, ns_t *namespace, struct loadtask *task,
4563 open_library_by_path(name, new_lib_paths, task, z_info);
4566 task->fd);
4569 open_library_by_path(name, namespace->asan_lib_paths, task, z_info);
4572 task->fd);
4574 open_library_by_path(name, namespace->lib_paths, task, z_info);
4577 task->fd);
4707 static bool map_library_header(struct loadtask *task)
4715 ssize_t l = pread(task->fd, task->ehdr_buf, sizeof task->ehdr_buf, task->file_offset);
4716 task->eh = task->ehdr_buf;
4718 LD_LOGE("Error mapping header %{public}s: failed to read fd errno: %{public}d", task->name, errno);
4721 if (l < sizeof(Ehdr) || (task->eh->e_type != ET_DYN && task->eh->e_type != ET_EXEC)) {
4722 LD_LOGE("Error mapping header %{public}s: invaliled Ehdr l=%{public}d", task->name, l);
4725 task->phsize = task->eh->e_phentsize * task->eh->e_phnum;
4726 if (task->phsize > sizeof task->ehdr_buf - sizeof(Ehdr)) {
4727 task->allocated_buf = malloc(task->phsize);
4728 if (!task->allocated_buf) {
4729 LD_LOGE("Error mapping header %{public}s: failed to alloc memory errno: %{public}d", task->name, errno);
4732 l = pread(task->fd, task->allocated_buf, task->phsize, task->eh->e_phoff + task->file_offset);
4734 LD_LOGE("Error mapping header %{public}s: failed to pread errno: %{public}d", task->name, errno);
4737 if (l != task->phsize) {
4738 LD_LOGE("Error mapping header %{public}s: unmatched phsize errno: %{public}d", task->name, errno);
4741 ph = task->ph0 = task->allocated_buf;
4742 } else if (task->eh->e_phoff + task->phsize > l) {
4743 l = pread(task->fd, task->ehdr_buf + 1, task->phsize, task->eh->e_phoff + task->file_offset);
4745 LD_LOGE("Error mapping header %{public}s: failed to pread errno: %{public}d", task->name, errno);
4748 if (l != task->phsize) {
4749 LD_LOGE("Error mapping header %{public}s: unmatched phsize", task->name);
4752 ph = task->ph0 = (void *)(task->ehdr_buf + 1);
4754 ph = task->ph0 = (void *)((char *)task->ehdr_buf + task->eh->e_phoff);
4757 for (i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) {
4759 task->dyn = ph->p_vaddr;
4761 task->tls_image = ph->p_vaddr;
4762 task->tls.align = ph->p_align;
4763 task->tls.len = ph->p_filesz;
4764 task->tls.size = ph->p_memsz;
4773 task->dyn_map_len = ph->p_memsz + (ph->p_offset - off_start);
4777 task->dyn_map = mmap(0, task->dyn_map_len, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->file_offset);
4778 if (task->dyn_map == MAP_FAILED) {
4779 LD_LOGE("Error mapping header %{public}s: failed to map dynamic section errno: %{public}d", task->name, errno);
4782 task->dyn_addr = (size_t *)((unsigned char *)task->dyn_map + (ph->p_offset - off_start));
4784 if (search_vec(task->dyn_addr, &dyn_tmp, DT_STRTAB)) {
4787 LD_LOGE("Error mapping header %{public}s: DT_STRTAB not found", task->name);
4790 if (search_vec(task->dyn_addr, &dyn_tmp, DT_STRSZ)) {
4793 LD_LOGE("Error mapping header %{public}s: DT_STRSZ not found", task->name);
4798 task->shsize = task->eh->e_shentsize * task->eh->e_shnum;
4799 off_start = task->eh->e_shoff;
4801 task->shsize += task->eh->e_shoff - off_start;
4802 task->shdr_allocated_buf = mmap(0, task->shsize, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->file_offset);
4803 if (task->shdr_allocated_buf == MAP_FAILED) {
4805 task->name, errno);
4808 Shdr *sh = (char *)task->shdr_allocated_buf + task->eh->e_shoff - off_start;
4809 for (i = task->eh->e_shnum; i; i--, sh = (void *)((char *)sh + task->eh->e_shentsize)) {
4815 task->str_map_len = sh->sh_size + (sh->sh_offset - off_start);
4816 task->str_map = mmap(0, task->str_map_len, PROT_READ, MAP_PRIVATE, task->fd, off_start + task->file_offset);
4817 if (task->str_map == MAP_FAILED) {
4819 task->name, errno);
4822 task->str_addr = (char *)task->str_map + sh->sh_offset - off_start;
4825 if (!task->dyn) {
4826 LD_LOGE("Error mapping header %{public}s: dynamic section not found", task->name);
4833 free(task->allocated_buf);
4834 task->allocated_buf = NULL;
4835 if (task->shdr_allocated_buf != MAP_FAILED) {
4836 munmap(task->shdr_allocated_buf, task->shsize);
4837 task->shdr_allocated_buf = MAP_FAILED;
4842 static bool task_map_library(struct loadtask *task, struct reserved_address_params *reserved_params)
4848 Phdr *ph = task->ph0;
4857 for (i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) {
4859 task->p->relro_start = ph->p_vaddr & -PAGE_SIZE;
4860 task->p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
4883 if (!task->dyn) {
4884 LD_LOGE("Error mapping library: !task->dyn dynamic section not found task->name=%{public}s", task->name);
4887 if (DL_FDPIC && !(task->eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
4888 task->p->loadmap = calloc(1, sizeof(struct fdpic_loadmap) + nsegs * sizeof(struct fdpic_loadseg));
4889 if (!task->p->loadmap) {
4893 task->p->loadmap->nsegs = nsegs;
4894 for (ph = task->ph0, i = 0; i < nsegs; ph = (void *)((char *)ph + task->eh->e_phentsize)) {
4903 task->fd, ph->p_offset & -PAGE_SIZE + task->file_offset);
4905 unmap_library(task->p);
4906 LD_LOGE("Error mapping library: PT_LOAD mmap failed task->name=%{public}s errno=%{public}d map_len=%{public}d",
4907 task->name, errno, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE - 1));
4910 task->p->loadmap->segs[i].addr = (size_t)map +
4912 task->p->loadmap->segs[i].p_vaddr = ph->p_vaddr;
4913 task->p->loadmap->segs[i].p_memsz = ph->p_memsz;
4928 map = (void *)task->p->loadmap->segs[0].addr;
4941 size_t maxinum_alignment = phdr_table_get_maxinum_alignment(task->ph0, task->eh->e_phnum);
4949 LD_LOGE("Error mapping library: map len is larger than reserved address task->name=%{public}s", task->name);
4971 : mmap((void *)start_addr, map_len, prot, map_flags, task->fd, off_start + task->file_offset);
4974 " task->fd=%{public}d task->name=%{public}s map_len=%{public}d",
4975 errno, DL_NOMMU_SUPPORT, task->fd, task->name, map_len);
5001 : mmap(real_map, map_len, prot, map_flags, task->fd, off_start + task->file_offset);
5004 "task->fd=%{public}d task->name=%{public}s map_len=%{public}d",
5005 errno, DL_NOMMU_SUPPORT, task->fd, task->name, map_len);
5009 task->p->map = map;
5010 task->p->map_len = map_len;
5013 if (task->eh->e_type != ET_DYN && addr_min && map != (void *)addr_min) {
5014 LD_LOGE("Error mapping library: ET_DYN task->name=%{public}s", task->name);
5019 task->p->phdr = 0;
5020 task->p->phnum = 0;
5021 for (ph = task->ph0, i = task->eh->e_phnum; i; i--, ph = (void *)((char *)ph + task->eh->e_phentsize)) {
5031 if (!task->p->phdr && task->eh->e_phoff >= ph->p_offset
5032 && task->eh->e_phoff + task->phsize <= ph->p_offset + ph->p_filesz) {
5033 task->p->phdr = (void *)(base + ph->p_vaddr + (task->eh->e_phoff - ph->p_offset));
5034 task->p->phnum = task->eh->e_phnum;
5035 task->p->phentsize = task->eh->e_phentsize;
5048 task->fd,
5049 off_start + task->file_offset) == MAP_FAILED) {
5050 LD_LOGE("Error mapping library: mmap fix failed task->name=%{public}s errno=%{public}d", task->name, errno);
5067 LD_LOGE("Error mapping library: PF_W mmap fix failed errno=%{public}d task->name=%{public}s zeromap_size=%{public}d",
5068 errno, task->name, zeromap_size);
5071 set_bss_vma_name(task->p->name, (void *)pgbrk, zeromap_size);
5074 for (i = 0; ((size_t *)(base + task->dyn))[i]; i += NEXT_DYNAMIC_INDEX) {
5075 if (((size_t *)(base + task->dyn))[i] == DT_TEXTREL) {
5077 LD_LOGE("Error mapping library: mprotect failed task->name=%{public}s errno=%{public}d", task->name, errno);
5084 task->p->base = base;
5085 task->p->dynv = laddr(task->p, task->dyn);
5086 if (task->p->tls.size) {
5087 task->p->tls.image = laddr(task->p, task->tls_image);
5089 free(task->allocated_buf);
5090 task->allocated_buf = NULL;
5091 if (task->shdr_allocated_buf != MAP_FAILED) {
5092 munmap(task->shdr_allocated_buf, task->shsize);
5093 task->shdr_allocated_buf = MAP_FAILED;
5100 unmap_library(task->p);
5102 free(task->allocated_buf);
5103 task->allocated_buf = NULL;
5104 if (task->shdr_allocated_buf != MAP_FAILED) {
5105 munmap(task->shdr_allocated_buf, task->shsize);
5106 task->shdr_allocated_buf = MAP_FAILED;
5111 static bool resolve_fd_to_realpath(struct loadtask *task)
5116 int ret = snprintf(proc_self_fd, sizeof(proc_self_fd), "/proc/self/fd/%d", task->fd);
5125 strncpy(task->buf, resolved_path, PATH_MAX);
5130 static bool load_library_header(struct loadtask *task)
5132 const char *name = task->name;
5133 struct dso *needed_by = task->needed_by;
5134 ns_t *namespace = task->namespace;
5135 bool check_inherited = task->check_inherited;
5187 task->isloaded = true;
5188 task->p = &ldso;
5196 task->pathname = name;
5197 if (!is_accessible(namespace, task->pathname, g_is_asan, check_inherited)) {
5199 task->pathname, namespace ? namespace->ns_name : "NULL");
5200 task->fd = -1;
5202 task->fd = z_info.fd;
5203 task->file_offset = z_info.file_offset;
5210 task->pathname = name;
5211 if (!is_accessible(namespace, task->pathname, g_is_asan, check_inherited)) {
5213 task->pathname, namespace ? namespace->ns_name : "NULL");
5214 task->fd = -1;
5216 task->fd = open(name, O_RDONLY | O_CLOEXEC);
5222 task->p = find_library_by_name(name, namespace, check_inherited);
5223 if (task->p) {
5224 task->isloaded = true;
5226 name, namespace->ns_name, task->p->name, task->p->namespace->ns_name);
5233 task->fd = -1;
5235 open_library_by_path(name, namespace->env_paths, task, &z_info);
5237 for (task->p = needed_by; task->fd == -1 && task->p; task->p = task->p->needed_by) {
5238 if (fixup_rpath(task->p, task->buf, sizeof task->buf) < 0) {
5239 task->fd = INVALID_FD_INHIBIT_FURTHER_SEARCH; /* Inhibit further search. */
5241 if (task->p->rpath) {
5242 open_library_by_path(name, task->p->rpath, task, &z_info);
5243 if (task->fd != -1 && resolve_fd_to_realpath(task)) {
5244 if (!is_accessible(namespace, task->buf, g_is_asan, check_inherited)) {
5247 close(task->fd);
5248 task->fd = -1;
5254 handle_asan_path_open_by_task(task->fd, name, namespace, task, &z_info);
5255 LD_LOGD("load_library handle_asan_path_open_by_task fd:%{public}d.", task->fd);
5257 if (task->fd == -1 && namespace->lib_paths) {
5258 open_library_by_path(name, namespace->lib_paths, task, &z_info);
5259 LD_LOGD("load_library no asan lib_paths path_open fd:%{public}d.", task->fd);
5262 task->pathname = task->buf;
5264 if (task->fd < 0) {
5267 task->name, namespace->ns_name, errno);
5276 task->namespace = inherit->inherited_ns;
5277 task->check_inherited = false;
5278 if (load_library_header(task)) {
5283 task->name, namespace->ns_name);
5287 if (fstat(task->fd, &st) < 0) {
5288 LD_LOGE("Error loading header %{public}s: failed to get file state errno=%{public}d", task->name, errno);
5289 close(task->fd);
5290 task->fd = -1;
5294 task->p = find_library_by_fstat(&st, namespace, check_inherited, task->file_offset);
5295 if (task->p) {
5299 if (!task->p->shortname && task->pathname != name) {
5300 task->p->shortname = strrchr(task->p->name, '/') + 1;
5302 close(task->fd);
5303 task->fd = -1;
5304 task->isloaded = true;
5306 name, namespace->ns_name, task->p->name, task->p->namespace->ns_name);
5310 map = noload ? 0 : map_library_header(task);
5312 LD_LOGE("Error loading header %{public}s: failed to map header", task->name);
5313 close(task->fd);
5314 task->fd = -1;
5323 alloc_size = sizeof(struct dso) + strlen(task->pathname) + 1;
5324 if (runtime && task->tls.size) {
5325 size_t per_th = task->tls.size + task->tls.align + sizeof(void *) * (tls_cnt + TLS_CNT_INCREASE);
5333 task->p = calloc(1, alloc_size);
5334 if (!task->p) {
5335 LD_LOGE("Error loading header %{public}s: failed to allocate dso", task->name);
5336 close(task->fd);
5337 task->fd = -1;
5340 task->p->dev = st.st_dev;
5341 task->p->ino = st.st_ino;
5342 task->p->file_offset = task->file_offset;
5343 task->p->needed_by = needed_by;
5344 task->p->name = task->p->buf;
5345 strcpy(task->p->name, task->pathname);
5346 task->p->tls = task->tls;
5347 task->p->dynv = task->dyn_addr;
5348 task->p->strings = task->str_addr;
5351 if (search_vec(task->p->dynv, &rpath_offset, DT_RPATH))
5352 task->p->rpath_orig = task->p->strings + rpath_offset;
5353 if (search_vec(task->p->dynv, &runpath_offset, DT_RUNPATH))
5354 task->p->rpath_orig = task->p->strings + runpath_offset;
5357 if (task->pathname != name) {
5358 task->p->shortname = strrchr(task->p->name, '/') + 1;
5361 if (task->p->tls.size) {
5362 task->p->tls_id = ++tls_cnt;
5363 task->p->new_dtv = (void *)(-sizeof(size_t) &
5364 (uintptr_t)(task->p->name + strlen(task->p->name) + sizeof(size_t)));
5365 task->p->new_tls = (void *)(task->p->new_dtv + n_th * (tls_cnt + 1));
5368 tail->next = task->p;
5369 task->p->prev = tail;
5370 tail = task->p;
5373 task->p->namespace = namespace;
5374 ns_add_dso(namespace, task->p);
5378 static void task_load_library(struct loadtask *task, struct reserved_address_params *reserved_params)
5380 LD_LOGD("load_library loading ns=%{public}s name=%{public}s by_dlopen=%{public}d", task->namespace->ns_name, task->p->name, runtime);
5381 bool map = noload ? 0 : task_map_library(task, reserved_params);
5382 __close(task->fd);
5383 task->fd = -1;
5386 task->name, noload, errno);
5387 error("Error loading library %s: failed to map library noload=%d errno=%d", task->name, noload, errno);
5398 decode_dyn(task->p);
5399 if (find_sym(task->p, "__libc_start_main", 1).sym &&
5400 find_sym(task->p, "stdin", 1).sym) {
5401 do_dlclose(task->p);
5402 task->p = NULL;
5403 free((void*)task->name);
5404 task->name = ld_strdup("libc.so");
5405 task->check_inherited = true;
5406 if (!load_library_header(task)) {
5407 LD_LOGE("Error loading library %{public}s: failed to load libc.so", task->name);
5408 error("Error loading library %s: failed to load libc.so", task->name);
5419 reclaim_gaps(task->p);
5421 task->p->runtime_loaded = runtime;
5423 task->p->by_dlopen = 1;
5426 makefuncdescs(task->p);
5430 dprintf(1, "\t%s => %s (%p)\n", task->name, task->pathname, task->p->base);
5435 libc.load_hook((long unsigned int)task->p->base, task->p->phdr, task->p->phnum);
5480 LD_LOGD("load_library %{public}s adding DT_NEEDED task %{public}s namespace(%{public}s)", p->name, dtneed_name, namespace->ns_name);
5481 struct loadtask *task = create_loadtask(dtneed_name, p, namespace, true);
5482 if (!task) {
5483 LD_LOGE("Error loading dependencies %{public}s : create load task failed", p->name);
5484 error("Error loading dependencies for %s : create load task failed", p->name);
5491 if (!load_library_header(task)) {
5492 free_task(task);
5493 task = NULL;
5504 p->deps[cnt++] = task->p;
5505 if (task->isloaded) {
5506 free_task(task);
5507 task = NULL;
5509 append_loadtasks(tasks, task);
5521 struct loadtask *task = NULL;
5523 task = get_loadtask(tasks, i);
5524 if (!task) {
5527 if (task->dyn_map_len) {
5528 munmap(task->dyn_map, task->dyn_map_len);
5529 task->dyn_map = NULL;
5530 task->dyn_map_len = 0;
5531 if (task->p) {
5532 task->p->dynv = NULL;
5535 if (task->str_map_len) {
5536 munmap(task->str_map, task->str_map_len);
5537 task->str_map = NULL;
5538 task->str_map_len = 0;
5539 if (task->p) {
5540 task->p->strings = NULL;
5558 struct loadtask *task = NULL;
5561 task = get_loadtask(tasks, i);
5562 if (task) {
5564 reserved_address = reserved_params->reserved_address_recursive || (reserved_params->target == task->p);
5566 task_load_library(task, reserved_address ? reserved_params : NULL);
5603 struct loadtask *task = NULL;
5613 task = create_loadtask(s, NULL, ns, true);
5614 if (!task) {
5617 if (load_library_header(task)) {
5618 if (!task->isloaded) {
5619 append_loadtasks(tasks, task);
5620 task = NULL;
5623 if (task) {
5624 free_task(task);