| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Using NaCl interface tables. | 
|  | 2 | Copyright (C) 2015-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, see | 
|  | 17 | <http://www.gnu.org/licenses/>.  */ | 
|  | 18 |  | 
|  | 19 | #include <errno.h> | 
|  | 20 | #include <nacl-interfaces.h> | 
|  | 21 | #include <ldsodefs.h> | 
|  | 22 |  | 
|  | 23 |  | 
|  | 24 | /* These magic symbols are provided implicitly by the linker to | 
|  | 25 | give us the bounds of the specially-named sections.  */ | 
|  | 26 |  | 
|  | 27 | extern const struct nacl_interface __start_nacl_mandatory_interface_names[] | 
|  | 28 | attribute_hidden; | 
|  | 29 | extern const struct nacl_interface __stop_nacl_mandatory_interface_names[] | 
|  | 30 | attribute_hidden; | 
|  | 31 |  | 
|  | 32 | extern uintptr_t __start_nacl_mandatory_interface_tables[] | 
|  | 33 | attribute_hidden; | 
|  | 34 | extern uintptr_t __stop_nacl_mandatory_interface_tables[] | 
|  | 35 | attribute_hidden; | 
|  | 36 |  | 
|  | 37 | /* We use weak references for the optional ones, since they | 
|  | 38 | might not be included at all in any given statically-linked program.  */ | 
|  | 39 |  | 
|  | 40 | extern const struct nacl_interface __start_nacl_optional_interface_names[] | 
|  | 41 | attribute_hidden __attribute__ ((weak)); | 
|  | 42 | extern const struct nacl_interface __stop_nacl_optional_interface_names[] | 
|  | 43 | attribute_hidden __attribute__ ((weak)); | 
|  | 44 |  | 
|  | 45 | extern uintptr_t __start_nacl_optional_interface_tables[] | 
|  | 46 | attribute_hidden __attribute__ ((weak)); | 
|  | 47 | extern uintptr_t __stop_nacl_optional_interface_tables[] | 
|  | 48 | attribute_hidden __attribute__ ((weak)); | 
|  | 49 |  | 
|  | 50 | static uintptr_t * | 
|  | 51 | next_nacl_table (uintptr_t *t, const struct nacl_interface *i) | 
|  | 52 | { | 
|  | 53 | return (void *) t + i->table_size; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | static void __attribute__ ((noreturn)) | 
|  | 57 | missing_mandatory_interface (const struct nacl_interface *i) | 
|  | 58 | { | 
|  | 59 | static const char before[] = | 
|  | 60 | "FATAL: NaCl IRT interface query failed for essential interface \""; | 
|  | 61 | static const char after[] = | 
|  | 62 | "\"\n"; | 
|  | 63 |  | 
|  | 64 | if (__nacl_irt_fdio.write != NULL) | 
|  | 65 | { | 
|  | 66 | size_t wrote; | 
|  | 67 | (*__nacl_irt_fdio.write) (2, before, sizeof before - 1, &wrote); | 
|  | 68 | (*__nacl_irt_fdio.write) (2, i->name, i->namelen - 1, &wrote); | 
|  | 69 | (*__nacl_irt_fdio.write) (2, after, sizeof after - 1, &wrote); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | if (__nacl_irt_basic.exit != NULL) | 
|  | 73 | (*__nacl_irt_basic.exit) (-1); | 
|  | 74 |  | 
|  | 75 | __builtin_trap (); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | static void | 
|  | 79 | initialize_mandatory_interfaces (void) | 
|  | 80 | { | 
|  | 81 | const struct nacl_interface *i = __start_nacl_mandatory_interface_names; | 
|  | 82 | uintptr_t *t = __start_nacl_mandatory_interface_tables; | 
|  | 83 | while (i < __stop_nacl_mandatory_interface_names) | 
|  | 84 | { | 
|  | 85 | if (__nacl_irt_query (i->name, t, i->table_size) != i->table_size) | 
|  | 86 | missing_mandatory_interface (i); | 
|  | 87 |  | 
|  | 88 | t = next_nacl_table (t, i); | 
|  | 89 | i = next_nacl_interface (i); | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 |  | 
|  | 94 | static int | 
|  | 95 | nacl_missing_optional_interface (void) | 
|  | 96 | { | 
|  | 97 | return ENOSYS; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | static void | 
|  | 101 | initialize_optional_interfaces (void) | 
|  | 102 | { | 
|  | 103 | const struct nacl_interface *i = __start_nacl_optional_interface_names; | 
|  | 104 | uintptr_t *t = __start_nacl_optional_interface_tables; | 
|  | 105 | while (i < __stop_nacl_optional_interface_names) | 
|  | 106 | { | 
|  | 107 | size_t filled = __nacl_irt_query (i->name, t, i->table_size); | 
|  | 108 | if (filled != i->table_size) | 
|  | 109 | for (size_t slot = 0; slot < i->table_size / sizeof *t; ++slot) | 
|  | 110 | t[slot] = (uintptr_t) &nacl_missing_optional_interface; | 
|  | 111 |  | 
|  | 112 | t = next_nacl_table (t, i); | 
|  | 113 | i = next_nacl_interface (i); | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 |  | 
|  | 118 | void attribute_hidden | 
|  | 119 | __nacl_initialize_interfaces (void) | 
|  | 120 | { | 
|  | 121 | initialize_mandatory_interfaces (); | 
|  | 122 | initialize_optional_interfaces (); | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 |  | 
|  | 126 | static bool | 
|  | 127 | try_supply (const struct nacl_interface *const start, | 
|  | 128 | const struct nacl_interface *const stop, | 
|  | 129 | uintptr_t *all_tables, | 
|  | 130 | const char *ident, size_t ident_len, | 
|  | 131 | const void *table, size_t tablesize) | 
|  | 132 | { | 
|  | 133 | const struct nacl_interface *i = start; | 
|  | 134 | uintptr_t *t = all_tables; | 
|  | 135 | while (i < stop) | 
|  | 136 | { | 
|  | 137 | if (i->table_size == tablesize | 
|  | 138 | && i->namelen == ident_len | 
|  | 139 | && !memcmp (i->name, ident, ident_len)) | 
|  | 140 | { | 
|  | 141 | memcpy (t, table, tablesize); | 
|  | 142 | return true; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | t = next_nacl_table (t, i); | 
|  | 146 | i = next_nacl_interface (i); | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | return false; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | internal_function | 
|  | 153 | bool | 
|  | 154 | PASTE_NAME (__nacl_supply_interface_, MODULE_NAME) | 
|  | 155 | (const char *ident, size_t ident_len, const void *table, size_t tablesize) | 
|  | 156 | { | 
|  | 157 | return (try_supply (__start_nacl_mandatory_interface_names, | 
|  | 158 | __stop_nacl_mandatory_interface_names, | 
|  | 159 | __start_nacl_mandatory_interface_tables, | 
|  | 160 | ident, ident_len, table, tablesize) | 
|  | 161 | || try_supply (__start_nacl_optional_interface_names, | 
|  | 162 | __stop_nacl_optional_interface_names, | 
|  | 163 | __start_nacl_optional_interface_tables, | 
|  | 164 | ident, ident_len, table, tablesize)); | 
|  | 165 | } |