summaryrefslogtreecommitdiff
path: root/libbsd/libdl/dl_main.c
blob: 70e23fe19d6701b435824e772135c6ece2677e00 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright 2018 Chris Johns <chrisj@rtems.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

#include <dlfcn.h>

#include <rtems.h>
#include <rtems/rtl/rtl-shell.h>
#include <rtems/rtl/rtl-trace.h>
#include <rtems/untar.h>

#include "libbsd-dl-rootfs-tar.h"

#define EXAMPLE_NAME "LIBBSD LIBDL 1"

#define ARCHIVE_TRACE (RTEMS_RTL_TRACE_DETAIL | \
                       RTEMS_RTL_TRACE_WARNING | \
                       RTEMS_RTL_TRACE_LOAD | \
                       RTEMS_RTL_TRACE_UNLOAD | \
                       RTEMS_RTL_TRACE_SYMBOL | \
                       RTEMS_RTL_TRACE_RELOC | \
                       RTEMS_RTL_TRACE_ALLOCATOR | \
                       RTEMS_RTL_TRACE_UNRESOLVED | \
                       RTEMS_RTL_TRACE_ARCHIVES | \
                       RTEMS_RTL_TRACE_DEPENDENCY)
#define EXAMPLE_DEBUG_TRACE ARCHIVE_TRACE

#define TARFILE_START libbsd_dl_rootfs_tar
#define TARFILE_SIZE  libbsd_dl_rootfs_tar_size

typedef int (*call_sig)(void);

static void
dl_load_dump (void)
{
#if LIBDL01_CMDS
  char* list[] = { "rtl", "list", NULL };
  char* sym[] = { "rtl", "sym", NULL };
  printf ("RTL List:\n");
  rtems_rtl_shell_command (2, list);
  printf ("RTL Sym:\n");
  rtems_rtl_shell_command (2, sym);
#endif
}

static void
example_fatal_error (void)
{
  rtems_status_code sc = rtems_task_wake_after( 3 );
  assert(sc == RTEMS_SUCCESSFUL);
  assert( 0 );
}

rtems_task Init(
  rtems_task_argument ignored
)
{
  const char* name = "dl_libbsd.o";
  void*       handle;
  int         unresolved = 0;
  call_sig    call;
  int         te;

  puts( "\n\nExample: " EXAMPLE_NAME );

  te = Untar_FromMemory((void *)TARFILE_START, (size_t)TARFILE_SIZE);
  if (te != 0)
  {
    printf("untar failed: %d\n", te);
    example_fatal_error();
  }

#if EXAMPLE_DEBUG_TRACE
  rtems_rtl_trace_set_mask (EXAMPLE_DEBUG_TRACE);
#endif

  printf("load: %s\n", name);

  handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL);
  if (handle == NULL)
  {
    printf("error: loading %s failed: %s\n", name, dlerror ());
    example_fatal_error ();
  }

  printf ("handle: %p loaded\n", handle);

  if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) != 0)
  {
    printf ("error: cannot get module's unresolved status\n");
    example_fatal_error ();
  }

  printf ("unresolved: %p: %s \n", handle, unresolved == 0 ? "resolved" : "UNRESOLVED");

  if (unresolved != 0)
  {
    printf ("error: module has unresolved externals\n");
    example_fatal_error ();
  }

  call = dlsym (handle, "rtems_main");
  if (call == NULL)
  {
    printf ("error: cannot find symbol: rtems_main\n");
    example_fatal_error ();
  }

  call ();

  if (dlclose (handle) != 0)
  {
    printf ("error: unloading %s failed: %d\n", name, dlerror ());
    example_fatal_error ();
  }
}

/*
 * Configure RTEMS.
 */
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_STUB_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_ZERO_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK

#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 32

#define CONFIGURE_MAXIMUM_USER_EXTENSIONS 1

#define CONFIGURE_UNLIMITED_ALLOCATION_SIZE 32
#define CONFIGURE_UNLIMITED_OBJECTS
#define CONFIGURE_UNIFIED_WORK_AREAS

#define CONFIGURE_STACK_CHECKER_ENABLED

#define CONFIGURE_BDBUF_BUFFER_MAX_SIZE (64 * 1024)
#define CONFIGURE_BDBUF_MAX_READ_AHEAD_BLOCKS 4
#define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE (1 * 1024 * 1024)

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
#define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES
#define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT

#define CONFIGURE_INIT

#include <rtems/confdefs.h>