Skip to content

Commit 0584e89

Browse files
author
Mark Seaborn
committed
Add a minimal "hello world" program which uses IRT interfaces directly
1 parent d64d4f4 commit 0584e89

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

hellow_minimal_irt.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
/*
3+
* This is a minimal NaCl "hello world" program. It uses NaCl's
4+
* stable IRT ABI.
5+
*/
6+
7+
#include <stdint.h>
8+
9+
#include "nacl_irt_interfaces.h"
10+
11+
struct nacl_irt_basic __libnacl_irt_basic;
12+
struct nacl_irt_fdio __libnacl_irt_fdio;
13+
TYPE_nacl_irt_query __nacl_irt_query;
14+
15+
static void grok_auxv(const Elf32_auxv_t *auxv) {
16+
const Elf32_auxv_t *av;
17+
for (av = auxv; av->a_type != AT_NULL; ++av) {
18+
if (av->a_type == AT_SYSINFO) {
19+
__nacl_irt_query = (TYPE_nacl_irt_query) av->a_un.a_val;
20+
}
21+
}
22+
}
23+
24+
static void __libnacl_mandatory_irt_query(const char *interface,
25+
void *table, size_t table_size) {
26+
__nacl_irt_query(interface, table, table_size);
27+
}
28+
29+
#define DO_QUERY(ident, name) \
30+
__libnacl_mandatory_irt_query(ident, &__libnacl_irt_##name, \
31+
sizeof(__libnacl_irt_##name))
32+
33+
void _start(uint32_t *info) {
34+
Elf32_auxv_t *auxv = nacl_startup_auxv(info);
35+
grok_auxv(auxv);
36+
DO_QUERY(NACL_IRT_BASIC_v0_1, basic);
37+
DO_QUERY(NACL_IRT_FDIO_v0_1, fdio);
38+
39+
const char string[] = "Hello world!\n";
40+
size_t written;
41+
__libnacl_irt_fdio.write(2, string, sizeof(string) - 1, &written);
42+
__libnacl_irt_basic.exit(0);
43+
}

nacl_irt_interfaces.h

+47-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
/* This is a subset of NaCl's IRT interfaces. */
66

7+
#include <sys/types.h>
8+
9+
struct stat;
10+
struct dirent;
11+
struct timeval;
12+
struct timespec;
13+
14+
715
/* From native_client/src/untrusted/irt/irt.h: */
816

917
#define NACL_IRT_BASIC_v0_1 "nacl-irt-basic-0.1"
@@ -28,17 +36,54 @@ struct nacl_irt_fdio {
2836
int (*getdents)(int fd, struct dirent *, size_t count, size_t *nread);
2937
};
3038

39+
typedef size_t (*TYPE_nacl_irt_query)(const char *interface_ident,
40+
void *table, size_t tablesize);
41+
42+
3143
/* From native_client/src/include/elf32.h: */
3244

33-
struct Elf32_auxv {
45+
typedef struct {
3446
int a_type;
3547
union {
3648
int a_val;
3749
} a_un;
38-
};
50+
} Elf32_auxv_t;
51+
3952

4053
/* From native_client/src/include/elf_auxv.h: */
4154

55+
#define AT_NULL 0
4256
#define AT_SYSINFO 32
4357

58+
59+
/* From native_client/src/untrusted/nacl/nacl_startup.h: */
60+
61+
enum NaClStartupInfoIndex {
62+
NACL_STARTUP_FINI, /* Cleanup function pointer for dynamic linking. */
63+
NACL_STARTUP_ENVC, /* Count of envp[] pointers. */
64+
NACL_STARTUP_ARGC, /* Count of argv[] pointers. */
65+
NACL_STARTUP_ARGV /* argv[0] pointer. */
66+
};
67+
68+
static inline int nacl_startup_argc(const uint32_t info[]) {
69+
return info[NACL_STARTUP_ARGC];
70+
}
71+
72+
static inline char **nacl_startup_argv(const uint32_t *info) {
73+
return (char **) &info[NACL_STARTUP_ARGV];
74+
}
75+
76+
static inline int nacl_startup_envc(const uint32_t info[]) {
77+
return info[NACL_STARTUP_ENVC];
78+
}
79+
80+
static inline char **nacl_startup_envp(const uint32_t *info) {
81+
return &nacl_startup_argv(info)[nacl_startup_argc(info) + 1];
82+
}
83+
84+
static inline Elf32_auxv_t *nacl_startup_auxv(const uint32_t *info) {
85+
char **envend = &nacl_startup_envp(info)[nacl_startup_envc(info) + 1];
86+
return (Elf32_auxv_t *) envend;
87+
}
88+
4489
#endif

run.sh

+4
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ g++ -m32 \
3838
$($llvm_config --ldflags --libs) -ldl \
3939
-o run_program
4040
41+
clang -m32 -O2 -c -emit-llvm hellow_minimal_irt.c -o hellow_minimal_irt.pexe
42+
4143
./codegen_test
44+
45+
./run_program hellow_minimal_irt.pexe

run_program.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct startup_info {
5959
int argc;
6060
char *argv0;
6161
char *envp0;
62-
struct Elf32_auxv auxv[2];
62+
Elf32_auxv_t auxv[2];
6363
};
6464

6565
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)