| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Manage function descriptors.  Generic version. | 
|  | 2 | Copyright (C) 1999-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 |  | 
|  | 5 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 6 | modify it under the terms of the GNU Lesser General Public | 
|  | 7 | License as published by the Free Software Foundation; either | 
|  | 8 | version 2.1 of the License, or (at your option) any later version. | 
|  | 9 |  | 
|  | 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 13 | Lesser General Public License for more details. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU Lesser General Public | 
|  | 16 | License along with the GNU C Library; if not, write to the Free | 
|  | 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | 
|  | 18 | 02111-1307 USA.  */ | 
|  | 19 |  | 
|  | 20 | #include <libintl.h> | 
|  | 21 | #include <unistd.h> | 
|  | 22 | #include <string.h> | 
|  | 23 | #include <sys/param.h> | 
|  | 24 | #include <sys/mman.h> | 
|  | 25 | #include <link.h> | 
|  | 26 | #include <ldsodefs.h> | 
|  | 27 | #include <elf/dynamic-link.h> | 
|  | 28 | #include <dl-fptr.h> | 
|  | 29 | #include <dl-unmap-segments.h> | 
|  | 30 | #include <atomic.h> | 
|  | 31 |  | 
|  | 32 | #ifndef ELF_MACHINE_BOOT_FPTR_TABLE_LEN | 
|  | 33 | /* ELF_MACHINE_BOOT_FPTR_TABLE_LEN should be greater than the number of | 
|  | 34 | dynamic symbols in ld.so.  */ | 
|  | 35 | # define ELF_MACHINE_BOOT_FPTR_TABLE_LEN 256 | 
|  | 36 | #endif | 
|  | 37 |  | 
|  | 38 | #ifndef ELF_MACHINE_LOAD_ADDRESS | 
|  | 39 | # error "ELF_MACHINE_LOAD_ADDRESS is not defined." | 
|  | 40 | #endif | 
|  | 41 |  | 
|  | 42 | #ifndef COMPARE_AND_SWAP | 
|  | 43 | # define COMPARE_AND_SWAP(ptr, old, new) \ | 
|  | 44 | (catomic_compare_and_exchange_bool_acq (ptr, new, old) == 0) | 
|  | 45 | #endif | 
|  | 46 |  | 
|  | 47 | ElfW(Addr) _dl_boot_fptr_table [ELF_MACHINE_BOOT_FPTR_TABLE_LEN]; | 
|  | 48 |  | 
|  | 49 | static struct local | 
|  | 50 | { | 
|  | 51 | struct fdesc_table *root; | 
|  | 52 | struct fdesc *free_list; | 
|  | 53 | unsigned int npages;		/* # of pages to allocate */ | 
|  | 54 | /* the next to members MUST be consecutive! */ | 
|  | 55 | struct fdesc_table boot_table; | 
|  | 56 | struct fdesc boot_fdescs[1024]; | 
|  | 57 | } | 
|  | 58 | local = | 
|  | 59 | { | 
|  | 60 | #ifdef SHARED | 
|  | 61 | /* Address of .boot_table is not known until runtime.  */ | 
|  | 62 | .root = 0, | 
|  | 63 | #else | 
|  | 64 | .root = &local.boot_table, | 
|  | 65 | #endif | 
|  | 66 | .npages = 2, | 
|  | 67 | .boot_table = | 
|  | 68 | { | 
|  | 69 | .len = sizeof (local.boot_fdescs) / sizeof (local.boot_fdescs[0]), | 
|  | 70 | .first_unused = 0 | 
|  | 71 | } | 
|  | 72 | }; | 
|  | 73 |  | 
|  | 74 | /* Create a new fdesc table and return a pointer to the first fdesc | 
|  | 75 | entry.  The fdesc lock must have been acquired already.  */ | 
|  | 76 |  | 
|  | 77 | static struct fdesc_table * | 
|  | 78 | new_fdesc_table (struct local *l, size_t *size) | 
|  | 79 | { | 
|  | 80 | size_t old_npages = l->npages; | 
|  | 81 | size_t new_npages = old_npages + old_npages; | 
|  | 82 | struct fdesc_table *new_table; | 
|  | 83 |  | 
|  | 84 | /* If someone has just created a new table, we return NULL to tell | 
|  | 85 | the caller to use the new table.  */ | 
|  | 86 | if (! COMPARE_AND_SWAP (&l->npages, old_npages, new_npages)) | 
|  | 87 | return (struct fdesc_table *) NULL; | 
|  | 88 |  | 
|  | 89 | *size = old_npages * GLRO(dl_pagesize); | 
|  | 90 | new_table = __mmap (NULL, *size, | 
|  | 91 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); | 
|  | 92 | if (new_table == MAP_FAILED) | 
|  | 93 | _dl_signal_error (errno, NULL, NULL, | 
|  | 94 | N_("cannot map pages for fdesc table")); | 
|  | 95 |  | 
|  | 96 | new_table->len | 
|  | 97 | = (*size - sizeof (*new_table)) / sizeof (struct fdesc); | 
|  | 98 | new_table->first_unused = 1; | 
|  | 99 | return new_table; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | /* Must call _dl_fptr_init before using any other function.  */ | 
|  | 103 | void | 
|  | 104 | _dl_fptr_init (void) | 
|  | 105 | { | 
|  | 106 | struct local *l; | 
|  | 107 |  | 
|  | 108 | ELF_MACHINE_LOAD_ADDRESS (l, local); | 
|  | 109 | l->root = &l->boot_table; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | static ElfW(Addr) | 
|  | 113 | make_fdesc (ElfW(Addr) ip, ElfW(Addr) gp) | 
|  | 114 | { | 
|  | 115 | struct fdesc *fdesc = NULL; | 
|  | 116 | struct fdesc_table *root; | 
|  | 117 | unsigned int old; | 
|  | 118 | struct local *l; | 
|  | 119 |  | 
|  | 120 | ELF_MACHINE_LOAD_ADDRESS (l, local); | 
|  | 121 |  | 
|  | 122 | retry: | 
|  | 123 | root = l->root; | 
|  | 124 | while (1) | 
|  | 125 | { | 
|  | 126 | old = root->first_unused; | 
|  | 127 | if (old >= root->len) | 
|  | 128 | break; | 
|  | 129 | else if (COMPARE_AND_SWAP (&root->first_unused, old, old + 1)) | 
|  | 130 | { | 
|  | 131 | fdesc = &root->fdesc[old]; | 
|  | 132 | goto install; | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | if (l->free_list) | 
|  | 137 | { | 
|  | 138 | /* Get it from free-list.  */ | 
|  | 139 | do | 
|  | 140 | { | 
|  | 141 | fdesc = l->free_list; | 
|  | 142 | if (fdesc == NULL) | 
|  | 143 | goto retry; | 
|  | 144 | } | 
|  | 145 | while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->free_list, | 
|  | 146 | (ElfW(Addr)) fdesc, fdesc->ip)); | 
|  | 147 | } | 
|  | 148 | else | 
|  | 149 | { | 
|  | 150 | /* Create a new fdesc table.  */ | 
|  | 151 | size_t size; | 
|  | 152 | struct fdesc_table *new_table = new_fdesc_table (l, &size); | 
|  | 153 |  | 
|  | 154 | if (new_table == NULL) | 
|  | 155 | goto retry; | 
|  | 156 |  | 
|  | 157 | new_table->next = root; | 
|  | 158 | if (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->root, | 
|  | 159 | (ElfW(Addr)) root, | 
|  | 160 | (ElfW(Addr)) new_table)) | 
|  | 161 | { | 
|  | 162 | /* Someone has just installed a new table. Return NULL to | 
|  | 163 | tell the caller to use the new table.  */ | 
|  | 164 | __munmap (new_table, size); | 
|  | 165 | goto retry; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | /* Note that the first entry was reserved while allocating the | 
|  | 169 | memory for the new page.  */ | 
|  | 170 | fdesc = &new_table->fdesc[0]; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | install: | 
|  | 174 | fdesc->ip = ip; | 
|  | 175 | fdesc->gp = gp; | 
|  | 176 |  | 
|  | 177 | return (ElfW(Addr)) fdesc; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 |  | 
|  | 181 | static inline ElfW(Addr) * __attribute__ ((always_inline)) | 
|  | 182 | make_fptr_table (struct link_map *map) | 
|  | 183 | { | 
|  | 184 | const ElfW(Sym) *symtab | 
|  | 185 | = (const void *) D_PTR (map, l_info[DT_SYMTAB]); | 
|  | 186 | const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); | 
|  | 187 | ElfW(Addr) *fptr_table; | 
|  | 188 | size_t size; | 
|  | 189 | size_t len; | 
|  | 190 |  | 
|  | 191 | /* XXX Apparently the only way to find out the size of the dynamic | 
|  | 192 | symbol section is to assume that the string table follows right | 
|  | 193 | afterwards...  */ | 
|  | 194 | len = ((strtab - (char *) symtab) | 
|  | 195 | / map->l_info[DT_SYMENT]->d_un.d_val); | 
|  | 196 | size = ((len * sizeof (fptr_table[0]) + GLRO(dl_pagesize) - 1) | 
|  | 197 | & -GLRO(dl_pagesize)); | 
|  | 198 | /* XXX We don't support here in the moment systems without MAP_ANON. | 
|  | 199 | There probably are none for IA-64.  In case this is proven wrong | 
|  | 200 | we will have to open /dev/null here and use the file descriptor | 
|  | 201 | instead of the hard-coded -1.  */ | 
|  | 202 | fptr_table = __mmap (NULL, size, | 
|  | 203 | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, | 
|  | 204 | -1, 0); | 
|  | 205 | if (fptr_table == MAP_FAILED) | 
|  | 206 | _dl_signal_error (errno, NULL, NULL, | 
|  | 207 | N_("cannot map pages for fptr table")); | 
|  | 208 |  | 
|  | 209 | if (COMPARE_AND_SWAP ((ElfW(Addr) *) &map->l_mach.fptr_table, | 
|  | 210 | (ElfW(Addr)) NULL, (ElfW(Addr)) fptr_table)) | 
|  | 211 | map->l_mach.fptr_table_len = len; | 
|  | 212 | else | 
|  | 213 | __munmap (fptr_table, len * sizeof (fptr_table[0])); | 
|  | 214 |  | 
|  | 215 | return map->l_mach.fptr_table; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 |  | 
|  | 219 | ElfW(Addr) | 
|  | 220 | _dl_make_fptr (struct link_map *map, const ElfW(Sym) *sym, | 
|  | 221 | ElfW(Addr) ip) | 
|  | 222 | { | 
|  | 223 | ElfW(Addr) *ftab = map->l_mach.fptr_table; | 
|  | 224 | const ElfW(Sym) *symtab; | 
|  | 225 | Elf_Symndx symidx; | 
|  | 226 | struct local *l; | 
|  | 227 |  | 
|  | 228 | if (__builtin_expect (ftab == NULL, 0)) | 
|  | 229 | ftab = make_fptr_table (map); | 
|  | 230 |  | 
|  | 231 | symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]); | 
|  | 232 | symidx = sym - symtab; | 
|  | 233 |  | 
|  | 234 | if (symidx >= map->l_mach.fptr_table_len) | 
|  | 235 | _dl_signal_error (0, NULL, NULL, | 
|  | 236 | N_("internal error: symidx out of range of fptr table")); | 
|  | 237 |  | 
|  | 238 | while (ftab[symidx] == 0) | 
|  | 239 | { | 
|  | 240 | /* GOT has already been relocated in elf_get_dynamic_info - | 
|  | 241 | don't try to relocate it again.  */ | 
|  | 242 | ElfW(Addr) fdesc | 
|  | 243 | = make_fdesc (ip, map->l_info[DT_PLTGOT]->d_un.d_ptr); | 
|  | 244 |  | 
|  | 245 | if (__builtin_expect (COMPARE_AND_SWAP (&ftab[symidx], (ElfW(Addr)) NULL, | 
|  | 246 | fdesc), 1)) | 
|  | 247 | { | 
|  | 248 | /* Noone has updated the entry and the new function | 
|  | 249 | descriptor has been installed.  */ | 
|  | 250 | #if 0 | 
|  | 251 | const char *strtab | 
|  | 252 | = (const void *) D_PTR (map, l_info[DT_STRTAB]); | 
|  | 253 |  | 
|  | 254 | ELF_MACHINE_LOAD_ADDRESS (l, local); | 
|  | 255 | if (l->root != &l->boot_table | 
|  | 256 | || l->boot_table.first_unused > 20) | 
|  | 257 | _dl_debug_printf ("created fdesc symbol `%s' at %lx\n", | 
|  | 258 | strtab + sym->st_name, ftab[symidx]); | 
|  | 259 | #endif | 
|  | 260 | break; | 
|  | 261 | } | 
|  | 262 | else | 
|  | 263 | { | 
|  | 264 | /* We created a duplicated function descriptor. We put it on | 
|  | 265 | free-list.  */ | 
|  | 266 | struct fdesc *f = (struct fdesc *) fdesc; | 
|  | 267 |  | 
|  | 268 | ELF_MACHINE_LOAD_ADDRESS (l, local); | 
|  | 269 |  | 
|  | 270 | do | 
|  | 271 | f->ip = (ElfW(Addr)) l->free_list; | 
|  | 272 | while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &l->free_list, | 
|  | 273 | f->ip, fdesc)); | 
|  | 274 | } | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | return ftab[symidx]; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 |  | 
|  | 281 | void | 
|  | 282 | _dl_unmap (struct link_map *map) | 
|  | 283 | { | 
|  | 284 | ElfW(Addr) *ftab = map->l_mach.fptr_table; | 
|  | 285 | struct fdesc *head = NULL, *tail = NULL; | 
|  | 286 | size_t i; | 
|  | 287 |  | 
|  | 288 | _dl_unmap_segments (map); | 
|  | 289 |  | 
|  | 290 | if (ftab == NULL) | 
|  | 291 | return; | 
|  | 292 |  | 
|  | 293 | /* String together the fdesc structures that are being freed.  */ | 
|  | 294 | for (i = 0; i < map->l_mach.fptr_table_len; ++i) | 
|  | 295 | { | 
|  | 296 | if (ftab[i]) | 
|  | 297 | { | 
|  | 298 | *(struct fdesc **) ftab[i] = head; | 
|  | 299 | head = (struct fdesc *) ftab[i]; | 
|  | 300 | if (tail == NULL) | 
|  | 301 | tail = head; | 
|  | 302 | } | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | /* Prepend the new list to the free_list: */ | 
|  | 306 | if (tail) | 
|  | 307 | do | 
|  | 308 | tail->ip = (ElfW(Addr)) local.free_list; | 
|  | 309 | while (! COMPARE_AND_SWAP ((ElfW(Addr) *) &local.free_list, | 
|  | 310 | tail->ip, (ElfW(Addr)) head)); | 
|  | 311 |  | 
|  | 312 | __munmap (ftab, (map->l_mach.fptr_table_len | 
|  | 313 | * sizeof (map->l_mach.fptr_table[0]))); | 
|  | 314 |  | 
|  | 315 | map->l_mach.fptr_table = NULL; | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | extern ElfW(Addr) _dl_fixup (struct link_map *, ElfW(Word)) attribute_hidden; | 
|  | 319 |  | 
|  | 320 | static inline Elf32_Addr | 
|  | 321 | elf_machine_resolve (void) | 
|  | 322 | { | 
|  | 323 | Elf32_Addr addr; | 
|  | 324 |  | 
|  | 325 | asm ("b,l     1f,%0\n" | 
|  | 326 | "	depi	0,31,2,%0\n" | 
|  | 327 | "1:	addil	L'_dl_runtime_resolve - ($PIC_pcrel$0 - 8),%0\n" | 
|  | 328 | "	ldo	R'_dl_runtime_resolve - ($PIC_pcrel$0 - 12)(%%r1),%0\n" | 
|  | 329 | : "=r" (addr) : : "r1"); | 
|  | 330 |  | 
|  | 331 | return addr; | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | ElfW(Addr) | 
|  | 335 | _dl_lookup_address (const void *address) | 
|  | 336 | { | 
|  | 337 | ElfW(Addr) addr = (ElfW(Addr)) address; | 
|  | 338 | unsigned int *desc, *gptr; | 
|  | 339 |  | 
|  | 340 | /* Check for special cases.  */ | 
|  | 341 | if ((int) addr == -1 | 
|  | 342 | || (unsigned int) addr < 4096 | 
|  | 343 | || !((unsigned int) addr & 2)) | 
|  | 344 | return addr; | 
|  | 345 |  | 
|  | 346 | /* Clear least-significant two bits from descriptor address.  */ | 
|  | 347 | desc = (unsigned int *) ((unsigned int) addr & ~3); | 
|  | 348 |  | 
|  | 349 | /* Check if descriptor requires resolution.  The following trampoline is | 
|  | 350 | used in each global offset table for function resolution: | 
|  | 351 |  | 
|  | 352 | ldw 0(r20),r22 | 
|  | 353 | bv r0(r22) | 
|  | 354 | ldw 4(r20),r21 | 
|  | 355 | tramp:	b,l .-12,r20 | 
|  | 356 | depwi 0,31,2,r20 | 
|  | 357 | .word _dl_runtime_resolve | 
|  | 358 | .word "_dl_runtime_resolve ltp" | 
|  | 359 | got:	.word _DYNAMIC | 
|  | 360 | .word "struct link map address" */ | 
|  | 361 | gptr = (unsigned int *) desc[0]; | 
|  | 362 | if (gptr[0] == 0xea9f1fdd			/* b,l .-12,r20     */ | 
|  | 363 | && gptr[1] == 0xd6801c1e			/* depwi 0,31,2,r20 */ | 
|  | 364 | && (ElfW(Addr)) gptr[2] == elf_machine_resolve ()) | 
|  | 365 | _dl_fixup ((struct link_map *) gptr[5], (ElfW(Word)) desc[1]); | 
|  | 366 |  | 
|  | 367 | return (ElfW(Addr)) desc[0]; | 
|  | 368 | } |