| 1 | /* Post-processing of a symbol produced by dlsym, dlvsym. |
| 2 | Copyright (C) 1999-2020 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, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | |
| 20 | /* Return the link map containing the caller address. */ |
| 21 | static struct link_map * |
| 22 | _dl_sym_find_caller_link_map (ElfW(Addr) caller) |
| 23 | { |
| 24 | struct link_map *l = _dl_find_dso_for_object (caller); |
| 25 | if (l != NULL) |
| 26 | return l; |
| 27 | else |
| 28 | /* If the address is not recognized the call comes from the main |
| 29 | program (we hope). */ |
| 30 | return GL(dl_ns)[LM_ID_BASE]._ns_loaded; |
| 31 | } |
| 32 | |
| 33 | /* Translates RESULT, *REF, VALUE into a symbol address from the point |
| 34 | of view of MATCH. Performs IFUNC resolution and auditing if |
| 35 | necessary. If MATCH is NULL, CALLER is used to determine it. */ |
| 36 | static void * |
| 37 | _dl_sym_post (lookup_t result, const ElfW(Sym) *ref, void *value, |
| 38 | ElfW(Addr) caller, struct link_map *match) |
| 39 | { |
| 40 | /* Resolve indirect function address. */ |
| 41 | if (__glibc_unlikely (ELFW(ST_TYPE) (ref->st_info) == STT_GNU_IFUNC)) |
| 42 | { |
| 43 | DL_FIXUP_VALUE_TYPE fixup |
| 44 | = DL_FIXUP_MAKE_VALUE (result, (ElfW(Addr)) value); |
| 45 | fixup = elf_ifunc_invoke (DL_FIXUP_VALUE_ADDR (fixup)); |
| 46 | value = (void *) DL_FIXUP_VALUE_CODE_ADDR (fixup); |
| 47 | } |
| 48 | |
| 49 | #ifdef SHARED |
| 50 | /* Auditing checkpoint: we have a new binding. Provide the |
| 51 | auditing libraries the possibility to change the value and |
| 52 | tell us whether further auditing is wanted. */ |
| 53 | if (__glibc_unlikely (GLRO(dl_naudit) > 0)) |
| 54 | { |
| 55 | const char *strtab = (const char *) D_PTR (result, |
| 56 | l_info[DT_STRTAB]); |
| 57 | /* Compute index of the symbol entry in the symbol table of |
| 58 | the DSO with the definition. */ |
| 59 | unsigned int ndx = (ref - (ElfW(Sym) *) D_PTR (result, |
| 60 | l_info[DT_SYMTAB])); |
| 61 | |
| 62 | if (match == NULL) |
| 63 | match = _dl_sym_find_caller_link_map (caller); |
| 64 | |
| 65 | if ((match->l_audit_any_plt | result->l_audit_any_plt) != 0) |
| 66 | { |
| 67 | unsigned int altvalue = 0; |
| 68 | struct audit_ifaces *afct = GLRO(dl_audit); |
| 69 | /* Synthesize a symbol record where the st_value field is |
| 70 | the result. */ |
| 71 | ElfW(Sym) sym = *ref; |
| 72 | sym.st_value = (ElfW(Addr)) value; |
| 73 | |
| 74 | for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt) |
| 75 | { |
| 76 | struct auditstate *match_audit |
| 77 | = link_map_audit_state (match, cnt); |
| 78 | struct auditstate *result_audit |
| 79 | = link_map_audit_state (result, cnt); |
| 80 | if (afct->symbind != NULL |
| 81 | && ((match_audit->bindflags & LA_FLG_BINDFROM) != 0 |
| 82 | || ((result_audit->bindflags & LA_FLG_BINDTO) |
| 83 | != 0))) |
| 84 | { |
| 85 | unsigned int flags = altvalue | LA_SYMB_DLSYM; |
| 86 | uintptr_t new_value |
| 87 | = afct->symbind (&sym, ndx, |
| 88 | &match_audit->cookie, |
| 89 | &result_audit->cookie, |
| 90 | &flags, strtab + ref->st_name); |
| 91 | if (new_value != (uintptr_t) sym.st_value) |
| 92 | { |
| 93 | altvalue = LA_SYMB_ALTVALUE; |
| 94 | sym.st_value = new_value; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | afct = afct->next; |
| 99 | } |
| 100 | |
| 101 | value = (void *) sym.st_value; |
| 102 | } |
| 103 | } |
| 104 | #endif |
| 105 | return value; |
| 106 | } |
| 107 | |