summary refs log tree commit diff
path: root/sysdeps/nacl/nacl-interfaces.c
blob: a863d87bf19b969167d19b582a4879f32dbd6cad (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
/* Using NaCl interface tables.
   Copyright (C) 2013 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 == 0)
        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 ();
}