1 /* Copyright (C) 1990 Free Software Foundation, Inc.
3 This file is part of GDB.
5 GDB is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
10 GDB is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GDB; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <sys/types.h>
25 #include <sys/param.h>
36 ** local data declarations
38 #define MAX_PATH_SIZE 256
40 struct link_map inferior_lm
; /* inferior link map */
41 struct link_map
*inferior_lm_add
;
43 char inferior_so_name
[MAX_PATH_SIZE
]; /* Shared Object Library Name */
44 struct so_list
*next
; /* Next Structure */
47 struct section_table
*so_sections
;
48 struct section_table
*so_sections_end
;
51 static struct so_list
*so_list_head
= 0;
54 ** Build a section map for a shared library, record its text size in
55 ** the so_list structure and set up the text section of the shared lib.
58 solib_map_sections(so
)
62 char *scratch_pathname
;
64 struct section_table
*p
;
66 filename
= tilde_expand (so
->inferior_so_name
);
67 make_cleanup (free
, filename
);
69 scratch_chan
= openp (getenv ("PATH"), 1, filename
, O_RDONLY
, 0,
72 scratch_chan
= openp (getenv ("LD_LIBRARY_PATH"), 1, filename
, O_RDONLY
, 0,
75 perror_with_name (filename
);
77 so
->so_bfd
= bfd_fdopenr (scratch_pathname
, NULL
, scratch_chan
);
79 error ("Could not open `%s' as an executable file: %s",
80 scratch_pathname
, bfd_errmsg (bfd_error
));
81 if (!bfd_check_format (so
->so_bfd
, bfd_object
))
82 error ("\"%s\": not in executable format: %s.",
83 scratch_pathname
, bfd_errmsg (bfd_error
));
84 if (build_section_table (so
->so_bfd
, &so
->so_sections
, &so
->so_sections_end
))
85 error ("Can't find the file sections in `%s': %s",
86 exec_bfd
->filename
, bfd_errmsg (bfd_error
));
88 for (p
= so
->so_sections
; p
< so
->so_sections_end
; p
++)
90 if (strcmp (bfd_section_name (so
->so_bfd
, p
->sec_ptr
), ".text") == 0)
92 /* Determine length of text section and relocate it. */
93 so
->ld_text
= p
->endaddr
- p
->addr
;
94 p
->addr
+= (CORE_ADDR
)so
->inferior_lm
.lm_addr
;
95 p
->endaddr
+= (CORE_ADDR
)so
->inferior_lm
.lm_addr
;
98 /* All other sections are ignored for now. */
99 p
->addr
= p
->endaddr
= 0;
103 /*=======================================================================*/
109 ** This module contains the routine which finds the names of any loaded
110 ** "images" in the current process. The argument in must be NULL on the
111 ** first call, and then the returned value must be passed in on
112 ** subsequent calls. This provides the capability to "step" down the
113 ** list of loaded objects. On the last object, a NULL value is returned.
114 ** The arg and return value are "struct link_map" pointers, as defined
117 ** NOTE: This only works under SunOS4.0.
120 struct so_list
*find_solib(so_list_ptr
)
121 struct so_list
*so_list_ptr
; /* so_list_head position ptr */
123 struct so_list
*so_list_next
= 0;
124 CORE_ADDR inferior_dynamic_ptr
= 0;
125 struct link_map
*inferior_lm
= 0;
126 struct link_dynamic inferior_dynamic_cpy
;
127 struct link_dynamic_2 inferior_ld_2_cpy
;
132 if (!(so_list_next
= so_list_head
)) {
133 for (i
= 0; i
< misc_function_count
; i
++) {
134 if (!strcmp (misc_function_vector
[i
].name
, "_DYNAMIC")) {
135 inferior_dynamic_ptr
= misc_function_vector
[i
].address
;
139 if (inferior_dynamic_ptr
) {
140 read_memory(inferior_dynamic_ptr
, &inferior_dynamic_cpy
, sizeof(struct link_dynamic
));
141 if (inferior_dynamic_cpy
.ld_version
== 3) {
142 read_memory((CORE_ADDR
)inferior_dynamic_cpy
.ld_un
.ld_2
,
144 sizeof(struct link_dynamic_2
));
145 inferior_lm
= inferior_ld_2_cpy
.ld_loaded
;
151 ** Advance to next local abbreviated load_map structure
153 if (!(inferior_lm
= so_list_ptr
->inferior_lm
.lm_next
)) {
154 /* See if any were added, but be quiet if we can't read
155 from the target any more. */
158 status
= target_read_memory (
159 (CORE_ADDR
)so_list_ptr
->inferior_lm_add
,
160 &so_list_ptr
->inferior_lm
,
161 sizeof(struct link_map
));
163 inferior_lm
= so_list_ptr
->inferior_lm
.lm_next
;
167 so_list_next
= so_list_ptr
->next
;
169 if ((!so_list_next
) && inferior_lm
) {
171 ** Get Next LM Structure from inferior image and build
172 ** an local abbreviated load_map structure
174 new = (struct so_list
*) xmalloc(sizeof(struct so_list
));
175 new->inferior_lm_add
= inferior_lm
;
176 read_memory((CORE_ADDR
)inferior_lm
,
178 sizeof(struct link_map
));
180 read_memory((CORE_ADDR
)new->inferior_lm
.lm_name
,
181 new->inferior_so_name
,
183 new->inferior_so_name
[MAX_PATH_SIZE
- 1] = 0;
184 /* Zero everything after the first terminating null */
185 strncpy(new->inferior_so_name
, new->inferior_so_name
, MAX_PATH_SIZE
);
188 /* This doesn't work for core files, so instead get ld_text
189 using solib_map_sections (below). */
190 read_memory((CORE_ADDR
)new->inferior_lm
.lm_ld
,
191 &inferior_dynamic_cpy
,
192 sizeof(struct link_dynamic
));
193 read_memory((CORE_ADDR
)inferior_dynamic_cpy
.ld_un
.ld_2
,
195 sizeof(struct link_dynamic_2
));
196 new->ld_text
= inferior_ld_2_cpy
.ld_text
;
200 new->symbols_loaded
= 0;
202 new->so_sections
= NULL
;
204 so_list_ptr
->next
= new;
208 solib_map_sections (new);
212 return(so_list_next
);
216 ** Called by core_xfer_memory if the transfer form the core file failed.
217 ** We try to satisfy the request from the text sections of the shared libs.
220 solib_xfer_memory (memaddr
, myaddr
, len
, write
)
227 register struct so_list
*so
= 0;
229 while (so
= find_solib(so
))
231 res
= xfer_memory (memaddr
, myaddr
, len
, write
,
232 so
->so_bfd
, so
->so_sections
, so
->so_sections_end
);
238 /*=======================================================================*/
240 void solib_add(arg_string
, from_tty
)
244 register struct so_list
*so
= 0; /* link map state variable */
249 else if (val
= (char *) re_comp (arg_string
)) {
250 error ("Invalid regexp: %s", val
);
253 printf_filtered ("All shared libraries");
255 printf_filtered (" matching regular expresion \"%s\"", arg_string
);
256 printf_filtered (":\n");
260 while (so
= find_solib(so
)) {
261 if (re_exec(so
->inferior_so_name
)) {
262 if (so
->symbols_loaded
) {
263 printf("Symbols already loaded for %s\n", so
->inferior_so_name
);
265 symbol_file_add (so
->inferior_so_name
, from_tty
,
266 (unsigned int)so
->inferior_lm
.lm_addr
, 0);
267 so
->symbols_loaded
= 1;
272 /*=======================================================================*/
274 static void solib_info()
276 register struct so_list
*so
= 0; /* link map state variable */
278 while (so
= find_solib(so
)) {
279 if (so
== so_list_head
) {
280 printf(" Address Range Symbols Shared Object Library\n");
282 printf(" 0x%08x - 0x%08x %s %s\n",
283 so
->inferior_lm
.lm_addr
,
284 so
->inferior_lm
.lm_addr
+ so
->ld_text
- 1,
285 (so
->symbols_loaded
? "Yes" : "No "),
286 so
->inferior_so_name
);
289 printf("No shared libraries loaded at this time.\n");
294 ** Called by Insert Breakpoint to see if Address is Shared Library Address
297 solib_address(address
)
300 register struct so_list
*so
= 0; /* link map state variable */
302 while (so
= find_solib(so
)) {
303 if ((address
>= (CORE_ADDR
) so
->inferior_lm
.lm_addr
) &&
304 (address
< (CORE_ADDR
) so
->inferior_lm
.lm_addr
+ so
->ld_text
))
311 ** Called by free_all_symtabs
316 struct so_list
*next
;
318 while (so_list_head
) {
319 if (so_list_head
->so_sections
)
320 free (so_list_head
->so_sections
);
321 if (so_list_head
->so_bfd
)
322 bfd_close (so_list_head
->so_bfd
);
323 next
= so_list_head
->next
;
334 add_com("sharedlibrary", class_files
, solib_add
,
335 "Load shared object library symbols for files matching REGEXP.");
336 add_info("sharedlibrary", solib_info
,
337 "Status of loaded shared object libraries");