about summary refs log tree commit diff
path: root/sysdeps/nacl/nacl-interfaces.c
blob: e7771d9f7dc8eff848b3c771031ef3367790fadf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Using NaCl interface tables.
   Copyright (C) 2015-2017 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <nacl-interfaces.h>
#include <ldsodefs.h>


/* These magic symbols are provided implicitly by the linker to
   give us the bounds of the specially-named sections.  */

extern const struct nacl_interface __start_nacl_mandatory_interface_names[]
  attribute_hidden;
extern const struct nacl_interface __stop_nacl_mandatory_interface_names[]
  attribute_hidden;

extern uintptr_t __start_nacl_mandatory_interface_tables[]
  attribute_hidden;
extern uintptr_t __stop_nacl_mandatory_interface_tables[]
  attribute_hidden;

/* We use weak references for the optional ones, since they
   might not be included at all in any given statically-linked program.  */

extern const struct nacl_interface __start_nacl_optional_interface_names[]
  attribute_hidden __attribute__ ((weak));
extern const struct nacl_interface __stop_nacl_optional_interface_names[]
  attribute_hidden __attribute__ ((weak));

extern uintptr_t __start_nacl_optional_interface_tables[]
  attribute_hidden __attribute__ ((weak));
extern uintptr_t __stop_nacl_optional_interface_tables[]
  attribute_hidden __attribute__ ((weak));

static uintptr_t *
next_nacl_table (uintptr_t *t, const struct nacl_interface *i)
{
  return (void *) t + i->table_size;
}

static void __attribute__ ((noreturn))
missing_mandatory_interface (const struct nacl_interface *i)
{
  static const char before[] =
    "FATAL: NaCl IRT interface query failed for essential interface \"";
  static const char after[] =
    "\"\n";

  if (__nacl_irt_fdio.write != NULL)
    {
      size_t wrote;
      (*__nacl_irt_fdio.write) (2, before, sizeof before - 1, &wrote);
      (*__nacl_irt_fdio.write) (2, i->name, i->namelen - 1, &wrote);
      (*__nacl_irt_fdio.write) (2, after, sizeof after - 1, &wrote);
    }

  if (__nacl_irt_basic.exit != NULL)
    (*__nacl_irt_basic.exit) (-1);

  __builtin_trap ();
}

static void
initialize_mandatory_interfaces (void)
{
  const struct nacl_interface *i = __start_nacl_mandatory_interface_names;
  uintptr_t *t = __start_nacl_mandatory_interface_tables;
  while (i < __stop_nacl_mandatory_interface_names)
    {
      if (__nacl_irt_query (i->name, t, i->table_size) != i->table_size)
	missing_mandatory_interface (i);

      t = next_nacl_table (t, i);
      i = next_nacl_interface (i);
    }
}


static int
nacl_missing_optional_interface (void)
{
  return ENOSYS;
}

static void
initialize_optional_interfaces (void)
{
  const struct nacl_interface *i = __start_nacl_optional_interface_names;
  uintptr_t *t = __start_nacl_optional_interface_tables;
  while (i < __stop_nacl_optional_interface_names)
    {
      size_t filled = __nacl_irt_query (i->name, t, i->table_size);
      if (filled != i->table_size)
	for (size_t slot = 0; slot < i->table_size / sizeof *t; ++slot)
	  t[slot] = (uintptr_t) &nacl_missing_optional_interface;

      t = next_nacl_table (t, i);
      i = next_nacl_interface (i);
    }
}


void attribute_hidden
__nacl_initialize_interfaces (void)
{
  initialize_mandatory_interfaces ();
  initialize_optional_interfaces ();
}


static bool
try_supply (const struct nacl_interface *const start,
	    const struct nacl_interface *const stop,
	    uintptr_t *all_tables,
	    const char *ident, size_t ident_len,
	    const void *table, size_t tablesize)
{
  const struct nacl_interface *i = start;
  uintptr_t *t = all_tables;
  while (i < stop)
    {
      if (i->table_size == tablesize
	  && i->namelen == ident_len
	  && !memcmp (i->name, ident, ident_len))
	{
	  memcpy (t, table, tablesize);
	  return true;
	}

      t = next_nacl_table (t, i);
      i = next_nacl_interface (i);
    }

  return false;
}

internal_function
bool
PASTE_NAME (__nacl_supply_interface_, MODULE_NAME)
  (const char *ident, size_t ident_len, const void *table, size_t tablesize)
{
  return (try_supply (__start_nacl_mandatory_interface_names,
		      __stop_nacl_mandatory_interface_names,
		      __start_nacl_mandatory_interface_tables,
		      ident, ident_len, table, tablesize)
	  || try_supply (__start_nacl_optional_interface_names,
			 __stop_nacl_optional_interface_names,
			 __start_nacl_optional_interface_tables,
			 ident, ident_len, table, tablesize));
}