Return unique_xmalloc_ptr from some solib.c functions
[external/binutils.git] / gdb / solist.h
1 /* Shared library declarations for GDB, the GNU Debugger.
2    Copyright (C) 1990-2018 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #ifndef SOLIST_H
20 #define SOLIST_H
21
22 #define SO_NAME_MAX_PATH_SIZE 512       /* FIXME: Should be dynamic */
23 /* For domain_enum domain.  */
24 #include "symtab.h"
25 #include "gdb_bfd.h"
26
27 #define ALL_SO_LIBS(so) \
28     for (so = so_list_head; \
29          so != NULL; \
30          so = so->next)
31
32 /* Base class for target-specific link map information.  */
33
34 struct lm_info_base
35 {
36 };
37
38 struct so_list
39   {
40     /* The following fields of the structure come directly from the
41        dynamic linker's tables in the inferior, and are initialized by
42        current_sos.  */
43
44     struct so_list *next;       /* next structure in linked list */
45
46     /* A pointer to target specific link map information.  Often this
47        will be a copy of struct link_map from the user process, but
48        it need not be; it can be any collection of data needed to
49        traverse the dynamic linker's data structures.  */
50     lm_info_base *lm_info;
51
52     /* Shared object file name, exactly as it appears in the
53        inferior's link map.  This may be a relative path, or something
54        which needs to be looked up in LD_LIBRARY_PATH, etc.  We use it
55        to tell which entries in the inferior's dynamic linker's link
56        map we've already loaded.  */
57     char so_original_name[SO_NAME_MAX_PATH_SIZE];
58
59     /* Shared object file name, expanded to something GDB can open.  */
60     char so_name[SO_NAME_MAX_PATH_SIZE];
61
62     /* Program space this shared library belongs to.  */
63     struct program_space *pspace;
64
65     /* The following fields of the structure are built from
66        information gathered from the shared object file itself, and
67        are set when we actually add it to our symbol tables.
68
69        current_sos must initialize these fields to 0.  */
70
71     bfd *abfd;
72     char symbols_loaded;        /* flag: symbols read in yet?  */
73
74     /* objfile with symbols for a loaded library.  Target memory is read from
75        ABFD.  OBJFILE may be NULL either before symbols have been loaded, if
76        the file cannot be found or after the command "nosharedlibrary".  */
77     struct objfile *objfile;
78
79     struct target_section *sections;
80     struct target_section *sections_end;
81
82     /* Record the range of addresses belonging to this shared library.
83        There may not be just one (e.g. if two segments are relocated
84        differently).  This is used for "info sharedlibrary" and
85        the MI command "-file-list-shared-libraries".  The latter has a format
86        that supports outputting multiple segments once the related code
87        supports them.  */
88     CORE_ADDR addr_low, addr_high;
89   };
90
91 struct target_so_ops
92   {
93     /* Adjust the section binding addresses by the base address at
94        which the object was actually mapped.  */
95     void (*relocate_section_addresses) (struct so_list *so,
96                                         struct target_section *);
97
98     /* Free the link map info and any other private data structures
99        associated with a so_list entry.  */
100     void (*free_so) (struct so_list *so);
101
102     /* Reset private data structures associated with SO.
103        This is called when SO is about to be reloaded.
104        It is also called before free_so when SO is about to be freed.  */
105     void (*clear_so) (struct so_list *so);
106
107     /* Reset or free private data structures not associated with
108        so_list entries.  */
109     void (*clear_solib) (void);
110
111     /* Target dependent code to run after child process fork.  */
112     void (*solib_create_inferior_hook) (int from_tty);
113
114     /* Construct a list of the currently loaded shared objects.  This
115        list does not include an entry for the main executable file.
116
117        Note that we only gather information directly available from the
118        inferior --- we don't examine any of the shared library files
119        themselves.  The declaration of `struct so_list' says which fields
120        we provide values for.  */
121     struct so_list *(*current_sos) (void);
122
123     /* Find, open, and read the symbols for the main executable.  If
124        FROM_TTY is non-zero, allow messages to be printed.  */
125     int (*open_symbol_file_object) (int from_ttyp);
126
127     /* Determine if PC lies in the dynamic symbol resolution code of
128        the run time loader.  */
129     int (*in_dynsym_resolve_code) (CORE_ADDR pc);
130
131     /* Find and open shared library binary file.  */
132     gdb_bfd_ref_ptr (*bfd_open) (char *pathname);
133
134     /* Optional extra hook for finding and opening a solib.
135        If TEMP_PATHNAME is non-NULL: If the file is successfully opened a
136        pointer to a malloc'd and realpath'd copy of SONAME is stored there,
137        otherwise NULL is stored there.  */
138     int (*find_and_open_solib) (const char *soname,
139         unsigned o_flags, char **temp_pathname);
140
141     /* Hook for looking up global symbols in a library-specific way.  */
142     struct block_symbol (*lookup_lib_global_symbol)
143       (struct objfile *objfile,
144        const char *name,
145        const domain_enum domain);
146
147     /* Given two so_list objects, one from the GDB thread list
148        and another from the list returned by current_sos, return 1
149        if they represent the same library.
150        Falls back to using strcmp on so_original_name field when set
151        to NULL.  */
152     int (*same) (struct so_list *gdb, struct so_list *inferior);
153
154     /* Return whether a region of memory must be kept in a core file
155        for shared libraries loaded before "gcore" is used to be
156        handled correctly when the core file is loaded.  This only
157        applies when the section would otherwise not be kept in the
158        core file (in particular, for readonly sections).  */
159     int (*keep_data_in_core) (CORE_ADDR vaddr,
160                               unsigned long size);
161
162     /* Enable or disable optional solib event breakpoints as
163        appropriate.  This should be called whenever
164        stop_on_solib_events is changed.  This pointer can be
165        NULL, in which case no enabling or disabling is necessary
166        for this target.  */
167     void (*update_breakpoints) (void);
168
169     /* Target-specific processing of solib events that will be
170        performed before solib_add is called.  This pointer can be
171        NULL, in which case no specific preprocessing is necessary
172        for this target.  */
173     void (*handle_event) (void);
174   };
175
176 /* Free the memory associated with a (so_list *).  */
177 void free_so (struct so_list *so);
178
179 /* A deleter that calls free_so.  */
180 struct so_deleter
181 {
182   void operator() (struct so_list *so) const
183   {
184     free_so (so);
185   }
186 };
187
188 /* A unique pointer to a so_list.  */
189 typedef std::unique_ptr<so_list, so_deleter> so_list_up;
190
191 /* Return address of first so_list entry in master shared object list.  */
192 struct so_list *master_so_list (void);
193
194 /* Find main executable binary file.  */
195 extern gdb::unique_xmalloc_ptr<char> exec_file_find (const char *in_pathname,
196                                                      int *fd);
197
198 /* Find shared library binary file.  */
199 extern gdb::unique_xmalloc_ptr<char> solib_find (const char *in_pathname,
200                                                  int *fd);
201
202 /* Open BFD for shared library file.  */
203 extern gdb_bfd_ref_ptr solib_bfd_fopen (const char *pathname, int fd);
204
205 /* Find solib binary file and open it.  */
206 extern gdb_bfd_ref_ptr solib_bfd_open (char *in_pathname);
207
208 /* FIXME: gdbarch needs to control this variable.  */
209 extern struct target_so_ops *current_target_so_ops;
210
211 /* Handler for library-specific global symbol lookup in solib.c.  */
212 struct block_symbol solib_global_lookup (struct objfile *objfile,
213                                             const char *name,
214                                             const domain_enum domain);
215
216 #endif