aboutsummaryrefslogtreecommitdiff
path: root/gdb/solib-target.c
blob: 74be9a188810b139f59377558ed8ef8a04bbce2d (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
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
/* Definitions for targets which report shared library events.

   Copyright (C) 2006
   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., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.  */

#include "defs.h"
#include "solist.h"
#include "symtab.h"
#include "symfile.h"
#include "target.h"

#include "gdb_string.h"

struct lm_info
{
  CORE_ADDR textSeg, dataSeg;
};

static struct so_list *solib_start;
static int solibs_fetched;

static struct so_list *
solib_target_current_sos (void)
{
  struct so_list *sop;
  struct so_list *start = NULL;
  struct so_list *last = NULL;

  /* If we have not asked the target for the list of shared libraries
     yet, do it now.  */
  if (!solibs_fetched)
    {
      solibs_fetched = 1;
      target_get_shared_libraries ();
    }

  for (sop = solib_start; sop; sop = sop->next)
    {
      struct so_list *new;

      /* Duplicate the recorded solib.  */
      new = XZALLOC (struct so_list);
      strcpy (new->so_name, sop->so_name);
      strcpy (new->so_original_name, sop->so_original_name);
      new->lm_info = XMALLOC (struct lm_info);
      *new->lm_info = *sop->lm_info;

      /* Add it to the list.  */
      if (!start)
	last = start = new;
      else
	{
	  last->next = new;
	  last = new;
	}
    }

  return start;
}

static void
solib_target_special_symbol_handling (void)
{
  /* Nothing needed.  */
}

static void
solib_target_solib_create_inferior_hook (void)
{
  /* Nothing needed.  */
}

static void
solib_target_clear_solib (void)
{
  struct so_list *sop, *next;

  for (sop = solib_start; sop; sop = next)
    {
      next = sop->next;

      free_so (sop);
    }

  solib_start = NULL;
  solibs_fetched = 0;
}

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

static void
solib_target_relocate_section_addresses (struct so_list *so,
					 struct section_table *sec)
{
  int flags = bfd_get_section_flags (sec->bfd, sec->the_bfd_section);
  CORE_ADDR offset;

  offset = symfile_section_offset_from_segment (sec->bfd, sec->the_bfd_section,
						so->lm_info->textSeg,
						so->lm_info->dataSeg);

  sec->addr += offset;
  sec->endaddr += offset;

  /* If we haven't set these yet, do so now.  If this fails, we may waste some
     cycles uselessly retrying it, but that is rare and harmless.  */
  if (so->addr_low == 0 && so->addr_high == 0)
    {
      CORE_ADDR text_len, data_len;

      symfile_find_segment_lengths (sec->bfd, &text_len, &data_len);

      if (text_len)
	{
	  so->addr_low = so->lm_info->textSeg;
	  so->addr_high = so->addr_low + text_len;
	}
      else if (data_len)
	{
	  so->addr_low = so->lm_info->dataSeg;
	  so->addr_high = so->addr_low + data_len;
	}
    }
}

static int
solib_target_open_symbol_file_object (void *from_ttyp)
{
  /* We can't locate the main symbol file based on the target's
     knowledge; the user has to specify it.  */
  return 0;
}

static int
solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
{
  /* Assume there isn't target dynsym resolution code.  DLL targets
     generally have only import stubs (which GDB treats as "PLT entries"),
     and no runtime binding code.  */
  return 0;
}

static void
solib_target_add_one_solib (char *soname, CORE_ADDR textSeg,
			    CORE_ADDR dataSeg)
{
  struct so_list *new_solib, *so;

  /* We should already have queried the target for shared libraries
     before this point.  If we haven't, we may have just connected;
     we'll be querying shortly.  */
  if (!solibs_fetched)
    return;

  /* Check for duplicates already on the list.  This can happen, for
     instance, if we are stopped at a DLL load event when we first
     connect to a remote target: the DLL will already be in the
     queried list, but also be reported by the initial wait.  */
  for (so = solib_start; so; so = so->next)
    if (strcmp (so->so_name, soname) == 0
	&& so->lm_info->textSeg == textSeg
	&& so->lm_info->dataSeg == dataSeg)
      return;

  new_solib = XZALLOC (struct so_list);
  strncpy (new_solib->so_name, soname, SO_NAME_MAX_PATH_SIZE - 1);
  new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
  strncpy (new_solib->so_original_name, soname, SO_NAME_MAX_PATH_SIZE - 1);
  new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';

  new_solib->lm_info = XZALLOC (struct lm_info);
  new_solib->lm_info->textSeg = textSeg;
  new_solib->lm_info->dataSeg = dataSeg;

  if (solib_start == NULL)
    solib_start = new_solib;
  else
    {
      so = solib_start;
      while (so->next)
	so = so->next;
      so->next = new_solib;
    }

  /* We do not trigger symbol reading here; the target will do it,
     after all load events have been processed.  */
}

static void
solib_target_remove_one_solib (char *soname, CORE_ADDR textSeg,
			       CORE_ADDR dataSeg)
{
  struct so_list **slot, *removed;

  /* We should already have queried the target for shared libraries
     before this point.  If we haven't, we may have just connected;
     we'll be querying shortly.  */
  if (!solibs_fetched)
    return;

  for (slot = &solib_start; *slot != NULL; slot = &(*slot)->next)
    {
      if (textSeg != ~(CORE_ADDR) 0 && textSeg != (*slot)->lm_info->textSeg)
	continue;
      if (dataSeg != ~(CORE_ADDR) 0 && dataSeg != (*slot)->lm_info->dataSeg)
	continue;
      if (soname != NULL && strcmp (soname, (*slot)->so_name) != 0)
	continue;
      break;
    }

  if (*slot == NULL)
    return;

  removed = *slot;
  *slot = removed->next;

  free_so (removed);

  /* We do not trigger symbol unloading here; the target will do it,
     after all unload events have been processed.  */
}

static struct target_so_ops solib_target_so_ops;

extern initialize_file_ftype _initialize_solib_target; /* -Wmissing-prototypes */

void
_initialize_solib_target (void)
{
  solib_target_so_ops.relocate_section_addresses
    = solib_target_relocate_section_addresses;
  solib_target_so_ops.free_so = solib_target_free_so;
  solib_target_so_ops.clear_solib = solib_target_clear_solib;
  solib_target_so_ops.solib_create_inferior_hook
    = solib_target_solib_create_inferior_hook;
  solib_target_so_ops.special_symbol_handling
    = solib_target_special_symbol_handling;
  solib_target_so_ops.current_sos = solib_target_current_sos;
  solib_target_so_ops.open_symbol_file_object
    = solib_target_open_symbol_file_object;
  solib_target_so_ops.in_dynsym_resolve_code
    = solib_target_in_dynsym_resolve_code;
  solib_target_so_ops.add_one_solib = solib_target_add_one_solib;
  solib_target_so_ops.remove_one_solib = solib_target_remove_one_solib;

  current_target_so_ops = &solib_target_so_ops;
}