aboutsummaryrefslogtreecommitdiff
path: root/gnu/usr.bin/binutils/gdb/solib-fbsd-kld.c
blob: 190e6e6687164c081b3c5da84f96d838029777ba (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Handle FreeBSD kernel modules as shared libraries.
   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000,
   2001
   Free Software Foundation, Inc.

   This file is part of GDB.

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

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

/* $FreeBSD$ */

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/stat.h>
#define _KERNEL
#include <sys/linker.h>
#undef _KERNEL

/* XXX, kludge to avoid duplicate definitions while sys/linker.h is used. */
#define _ELF_COMMON_H

#include "defs.h"
#include "symtab.h"
#include "bfd.h"
#include "symfile.h"
#include "objfiles.h"
#include "gdbcore.h"
#include "target.h"
#include "inferior.h"

#include "solist.h"

struct lm_info
  {
     CORE_ADDR address;
  };

static int try_modpath (char *buf, int buflen, char *fmt, ...);
static char *guess_modpath (char *modname);

static void
kgdb_relocate_section_addresses (struct so_list *so,
				 struct section_table *sec)
{
  sec->addr += so->lm_info->address;
  sec->endaddr += so->lm_info->address;
}

static int
kgdb_open_symbol_file_object (void *from_ttyp)
{
  warning ("kgdb_open_symbol_file_object called\n");
  return 0;
}

static struct so_list *
kgdb_current_sos (void)
{
  linker_file_list_t linker_files;
  struct linker_file lfile;
  struct minimal_symbol *msymbol;
  struct linker_file *lfilek;
  struct so_list *head = NULL;
  struct so_list **link_ptr = &head;

  CORE_ADDR lfiles_addr;

  msymbol = lookup_minimal_symbol ("linker_files", NULL, symfile_objfile);
  if (msymbol == NULL || SYMBOL_VALUE_ADDRESS (msymbol) == 0)
    {
      warning ("failed to find linker_files symbol\n");
      return 0;
    }
  lfiles_addr = SYMBOL_VALUE_ADDRESS (msymbol);
  if (target_read_memory (lfiles_addr, (char *)&linker_files,
			  sizeof (linker_files)))
    {
      warning ("failed to read linker_files data\n");
      return 0;
    }
  for (lfilek = TAILQ_FIRST (&linker_files); lfilek != NULL;
       lfilek = TAILQ_NEXT (&lfile, link))
   {
      struct so_list *new;
      struct cleanup *old_chain;
      char *buf;
      int errcode;

      if (target_read_memory ((CORE_ADDR) lfilek, (char *) &lfile,
	  sizeof (lfile)))
	{
	  warning ("failed to read linker file data at %p\n", lfilek);
	  return 0;
	}
      target_read_string ((CORE_ADDR) lfile.filename, &buf,
			  SO_NAME_MAX_PATH_SIZE - 1, &errcode);
      if (errcode != 0)
	{
	  warning ("cannot read linker file pathname: %s\n",
		   safe_strerror (errcode));
	  return 0;
	}
      if (strlen (buf) < 3 || strcmp (&buf[strlen (buf) - 3], ".ko") != 0)
	{
	  xfree (buf);
	  continue;
	}

      new = (struct so_list *) xmalloc (sizeof (struct so_list));
      old_chain = make_cleanup (xfree, new);

      memset (new, 0, sizeof (*new));

      new->lm_info = xmalloc (sizeof (struct lm_info));
      make_cleanup (xfree, new->lm_info);

      new->lm_info->address = (CORE_ADDR) lfile.address;

      strncpy (new->so_original_name, buf, SO_NAME_MAX_PATH_SIZE - 1);
      new->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
      xfree (buf);
      snprintf (new->so_name, SO_NAME_MAX_PATH_SIZE, "%s",
		guess_modpath (new->so_original_name));

      new->next = NULL;
      *link_ptr = new;
      link_ptr = &new->next;

      discard_cleanups (old_chain);
    }
  return head;
}

static int
kgdb_in_dynsym_resolve_code (CORE_ADDR pc)
{
  warning ("kgdb_in_dynsym_resolve_code called\n");
  return 0;
}

static void
kgdb_special_symbol_handling (void)
{
}

static void
kgdb_solib_create_inferior_hook (void)
{
  struct so_list *inferior_sos;

  inferior_sos = kgdb_current_sos ();
  if (inferior_sos)
    {
      solib_add (NULL, /*from_tty*/0, NULL, auto_solib_add);
    }
}

static void
kgdb_clear_solib (void)
{
}

static void
kgdb_free_so (struct so_list *so)
{
  xfree (so->lm_info);
}

static int
try_modpath (char *buf, int buflen, char *fmt, ...)
{
  struct stat sb;
  va_list ap;

  va_start (ap, fmt);
  vsnprintf (buf, buflen, fmt, ap);
  va_end(ap);

  return (stat (buf, &sb) == 0);
}

static char *
guess_modpath (char *modname)
{
  static char buf[2048], moddir[128], syspath[1024];
  struct minimal_symbol *msymbol;
  char *kernpath, *objpath, *p, *version;
  int errcode, n, syspathlen;

  /* Set default module location */
  snprintf (buf, sizeof (buf), "/boot/kernel/%s", modname);

  /* Guess at the subdirectory off sys/modules. XXX, only sometimes correct */
  n = strlen (modname);
  if (n > 3 && strcmp (&modname[n - 3], ".ko") == 0)
    n -= 3;
  snprintf (moddir, sizeof (moddir), "%.*s", n, modname);

  /* Try to locate the kernel compile location from version[] */
  msymbol = lookup_minimal_symbol ("version", NULL, symfile_objfile);
  if (msymbol == NULL || SYMBOL_VALUE_ADDRESS (msymbol) == 0)
    {
      warning("cannot find `version' symbol; using default module path\n");
      return buf;
    }
  target_read_string (SYMBOL_VALUE_ADDRESS (msymbol), &version, 2048, &errcode);
  if (errcode != 0)
    {
      warning ("cannot read `version' string; using default module path: %s\n",
	       safe_strerror (errcode));
      return buf;
    }

  /* Find the kernel build path after user@host: on the second line. */
  if ((p = strchr (version, '\n')) == NULL ||
      (kernpath = strchr (p, ':')) == NULL ||
      (p = strchr (kernpath, '\n')) == NULL)
    {
      warning ("cannot parse version[]; using default module path\n");
      xfree (version);
      return buf;
    }
  kernpath++;
  *p = '\0';

  /*
   * Find the absolute path to src/sys by skipping back over path
   * components until we find a "/sys/".
   */
  syspathlen = 0;
  while (p > kernpath && syspathlen == 0)
    {
      while (p > kernpath && *p != '/')
	p--;
      if (strncmp (p, "/sys/", 5) == 0)
        syspathlen = p - kernpath + 4;
      else if (p > kernpath)
	p--;
    }
  if (syspathlen == 0)
    {
      warning ("cannot find /sys/ in `%s'; using default module path\n",
	       kernpath);
      xfree (version);
      return buf;
    }
  /*
   * For kernels compiled with buildkernel, the object path will have
   * been prepended to the /sys/ path in `kernpath'.
   */
  objpath = getenv ("MAKEOBJDIRPREFIX");
  if (objpath == NULL)
    objpath = "/usr/obj";
  n = strlen (objpath);
  if (syspathlen > n + 1 && strncmp (kernpath, objpath, n) == 0 &&
      kernpath[n] == '/')
    snprintf (syspath, sizeof (syspath), "%.*s", syspathlen - n, kernpath + n);
  else
    snprintf (syspath, sizeof (syspath), "%.*s", syspathlen, kernpath);

  /* Now try to find the module file */
  if (!try_modpath (buf, sizeof (buf), "./%s.debug", modname) &&
      !try_modpath (buf, sizeof (buf), "./%s", modname) && !try_modpath (buf,
      sizeof (buf), "%s/modules%s/modules/%s/%s.debug", kernpath, syspath,
      moddir, modname) && !try_modpath (buf, sizeof (buf),
      "%s/modules%s/modules/%s/%s", kernpath, syspath, moddir, modname) &&
      !try_modpath (buf, sizeof (buf), "/boot/kernel/%s.debug", modname) &&
      !try_modpath (buf, sizeof (buf), "/boot/kernel/%s", modname))
    {
      warning ("cannot find file for module %s\n", modname);
      snprintf (buf, sizeof (buf), "%s", modname);
    }
  xfree (version);

  return buf;
}

struct target_so_ops kgdb_so_ops;

void
_initialize_kgdb_solib (void)
{
  kgdb_so_ops.relocate_section_addresses = kgdb_relocate_section_addresses;
  kgdb_so_ops.free_so = kgdb_free_so;
  kgdb_so_ops.clear_solib = kgdb_clear_solib;
  kgdb_so_ops.solib_create_inferior_hook = kgdb_solib_create_inferior_hook;
  kgdb_so_ops.special_symbol_handling = kgdb_special_symbol_handling;
  kgdb_so_ops.current_sos = kgdb_current_sos;
  kgdb_so_ops.open_symbol_file_object = kgdb_open_symbol_file_object;
  kgdb_so_ops.in_dynsym_resolve_code = kgdb_in_dynsym_resolve_code;
}