blob: 7b448106d8e9f75a862be17bc81c596b93993d9e [file] [log] [blame]
Elliott Hughes0266ae52014-02-10 17:46:57 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <elf.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070030#include <string.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080031#include <sys/auxv.h>
32#include <sys/types.h>
33#include <link.h>
34
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070035#include "private/bionic_elf_tls.h"
36#include "private/bionic_globals.h"
37#include "pthread_internal.h"
38
Elliott Hughes0266ae52014-02-10 17:46:57 -080039/* ld provides this to us in the default link script */
40extern "C" void* __executable_start;
41
42int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) {
43 ElfW(Ehdr)* ehdr = reinterpret_cast<ElfW(Ehdr)*>(&__executable_start);
44
Elliott Hughes625993d2014-07-15 16:53:13 -070045 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
46 return -1;
47 }
Elliott Hughes0266ae52014-02-10 17:46:57 -080048
49 // Dynamic binaries get their dl_iterate_phdr from the dynamic linker, but
50 // static binaries get this. We don't have a list of shared objects to
51 // iterate over, since there's really only a single monolithic blob of
52 // code/data, plus optionally a VDSO.
53
54 struct dl_phdr_info exe_info;
55 exe_info.dlpi_addr = 0;
56 exe_info.dlpi_name = NULL;
57 exe_info.dlpi_phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(ehdr) + ehdr->e_phoff);
58 exe_info.dlpi_phnum = ehdr->e_phnum;
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070059 exe_info.dlpi_adds = 0;
60 exe_info.dlpi_subs = 0;
61
62 const TlsModules& tls_modules = __libc_shared_globals()->tls_modules;
63 if (tls_modules.module_count == 0) {
64 exe_info.dlpi_tls_modid = 0;
65 exe_info.dlpi_tls_data = nullptr;
66 } else {
67 const size_t kExeModuleId = 1;
68 const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
69 const TlsModule& tls_module = tls_modules.module_table[__tls_module_id_to_idx(kExeModuleId)];
70 char* static_tls = reinterpret_cast<char*>(__get_bionic_tcb()) - layout.offset_bionic_tcb();
71 exe_info.dlpi_tls_modid = kExeModuleId;
72 exe_info.dlpi_tls_data = static_tls + tls_module.static_offset;
73 }
Elliott Hughes0266ae52014-02-10 17:46:57 -080074
Elliott Hughes0266ae52014-02-10 17:46:57 -080075 // Try the executable first.
76 int rc = cb(&exe_info, sizeof(exe_info), data);
77 if (rc != 0) {
78 return rc;
79 }
80
81 // Try the VDSO if that didn't work.
82 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(getauxval(AT_SYSINFO_EHDR));
Elliott Hughes36f451a2014-09-10 15:20:40 -070083 if (ehdr_vdso == nullptr) {
84 // There is no VDSO, so there's nowhere left to look.
85 return rc;
86 }
87
Elliott Hughes0266ae52014-02-10 17:46:57 -080088 struct dl_phdr_info vdso_info;
89 vdso_info.dlpi_addr = 0;
90 vdso_info.dlpi_name = NULL;
91 vdso_info.dlpi_phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
92 vdso_info.dlpi_phnum = ehdr_vdso->e_phnum;
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070093 vdso_info.dlpi_adds = 0;
94 vdso_info.dlpi_subs = 0;
95 vdso_info.dlpi_tls_modid = 0;
96 vdso_info.dlpi_tls_data = nullptr;
Elliott Hughes0266ae52014-02-10 17:46:57 -080097 for (size_t i = 0; i < vdso_info.dlpi_phnum; ++i) {
98 if (vdso_info.dlpi_phdr[i].p_type == PT_LOAD) {
99 vdso_info.dlpi_addr = (ElfW(Addr)) ehdr_vdso - vdso_info.dlpi_phdr[i].p_vaddr;
100 break;
101 }
102 }
103 return cb(&vdso_info, sizeof(vdso_info), data);
Elliott Hughes0266ae52014-02-10 17:46:57 -0800104}