• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <elf.h>
17 #include <link.h>
18 #include <limits.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include "libc.h"
22 #include "syscall.h"
23 
24 #ifdef VDSO_USEFUL
25 
26 #if ULONG_MAX == 0xffffffff
27 typedef Elf32_Ehdr Ehdr;
28 typedef Elf32_Phdr Phdr;
29 typedef Elf32_Sym Sym;
30 typedef Elf32_Verdef Verdef;
31 typedef Elf32_Verdaux Verdaux;
32 #else
33 typedef Elf64_Ehdr Ehdr;
34 typedef Elf64_Phdr Phdr;
35 typedef Elf64_Sym Sym;
36 typedef Elf64_Verdef Verdef;
37 typedef Elf64_Verdaux Verdaux;
38 #endif
39 
checkver(Verdef * def,int vsym,const char * vername,char * strings)40 static int checkver(Verdef *def, int vsym, const char *vername, char *strings)
41 {
42 	vsym &= 0x7fff;
43 	for (;;) {
44 		if (!(def->vd_flags & VER_FLG_BASE)
45 		  && (def->vd_ndx & 0x7fff) == vsym)
46 			break;
47 		if (def->vd_next == 0)
48 			return 0;
49 		def = (Verdef *)((char *)def + def->vd_next);
50 	}
51 	Verdaux *aux = (Verdaux *)((char *)def + def->vd_aux);
52 	return !strcmp(vername, strings + aux->vda_name);
53 }
54 
55 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
56 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
57 
58 struct __vdso_info {
59 	size_t base;
60 	char *strings;
61 	Sym *syms;
62 	Elf_Symndx *hashtab;
63 	uint16_t *versym;
64 	Verdef *verdef;
65 } vdso_info = {-1, 0, 0, 0, 0, 0};
66 
__get_vdso_info()67 void __get_vdso_info()
68 {
69 	if (vdso_info.base != -1) {
70 		return ;
71 	}
72 	size_t i;
73 	for (i=0; libc.auxv[i] != AT_SYSINFO_EHDR; i+=2)
74 		if (!libc.auxv[i]) return ;
75 	if (!libc.auxv[i+1]) return ;
76 	Ehdr *eh = (void *)libc.auxv[i+1];
77 	Phdr *ph = (void *)((char *)eh + eh->e_phoff);
78 	size_t *dynv=0, base=-1;
79 	for (i=0; i<eh->e_phnum; i++, ph=(void *)((char *)ph+eh->e_phentsize)) {
80 		if (ph->p_type == PT_LOAD)
81 			base = (size_t)eh + ph->p_offset - ph->p_vaddr;
82 		else if (ph->p_type == PT_DYNAMIC)
83 			dynv = (void *)((char *)eh + ph->p_offset);
84 	}
85 	if (!dynv || base==(size_t)-1) return ;
86 
87 	for (i=0; dynv[i]; i+=2) {
88 		void *p = (void *)(base + dynv[i+1]);
89 		switch(dynv[i]) {
90 		case DT_STRTAB: vdso_info.strings = p; break;
91 		case DT_SYMTAB: vdso_info.syms = p; break;
92 		case DT_HASH: vdso_info.hashtab = p; break;
93 		case DT_VERSYM: vdso_info.versym = p; break;
94 		case DT_VERDEF: vdso_info.verdef = p; break;
95 		}
96 	}
97     vdso_info.base = base;
98     return ;
99 }
100 
__get_vdso_addr(const char * vername,const char * name)101 void *__get_vdso_addr(const char *vername, const char *name)
102 {
103 	if (!vdso_info.strings || !vdso_info.syms || !vdso_info.hashtab) return 0;
104 	if (!vdso_info.verdef) vdso_info.versym = 0;
105 
106     size_t i;
107 	for (i=0; i<vdso_info.hashtab[1]; i++) {
108 		if (!(1<<(vdso_info.syms[i].st_info&0xf) & OK_TYPES)) continue;
109 		if (!(1<<(vdso_info.syms[i].st_info>>4) & OK_BINDS)) continue;
110 		if (!vdso_info.syms[i].st_shndx) continue;
111 		if (strcmp(name, vdso_info.strings+vdso_info.syms[i].st_name)) continue;
112 		if (vdso_info.versym && !checkver(vdso_info.verdef, vdso_info.versym[i], vername, vdso_info.strings))
113 			continue;
114 		return (void *)(vdso_info.base + vdso_info.syms[i].st_value);
115 	}
116 
117 	return 0;
118 }
119 
120 #endif
121