1 /* Maintenance of module list in libdwfl.
2 Copyright (C) 2005, 2006, 2007, 2008, 2014, 2015 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
30 #include "../libdw/cfi.h"
35 free_cu (struct dwfl_cu *cu)
37 if (cu->lines != NULL)
43 nofree (void *arg __attribute__ ((unused)))
48 free_file (struct dwfl_file *file)
52 /* Close the fd only on the last reference. */
53 if (file->elf != NULL && elf_end (file->elf) == 0 && file->fd != -1)
59 __libdwfl_module_free (Dwfl_Module *mod)
61 if (mod->lazy_cu_root != NULL)
62 tdestroy (mod->lazy_cu_root, nofree);
64 if (mod->aranges != NULL)
69 for (size_t i = 0; i < mod->ncu; ++i)
74 /* We might have primed the Dwarf_CFI ebl cache with our own ebl
75 in __libdwfl_set_cfi. Make sure we don't free it twice. */
76 if (mod->eh_cfi != NULL)
78 if (mod->eh_cfi->ebl != NULL && mod->eh_cfi->ebl == mod->ebl)
79 mod->eh_cfi->ebl = NULL;
80 dwarf_cfi_end (mod->eh_cfi);
83 if (mod->dwarf_cfi != NULL)
85 if (mod->dwarf_cfi->ebl != NULL && mod->dwarf_cfi->ebl == mod->ebl)
86 mod->dwarf_cfi->ebl = NULL;
87 /* We don't need to explicitly destroy the dwarf_cfi.
88 That will be done by dwarf_end. */
93 INTUSE(dwarf_end) (mod->dw);
96 INTUSE(dwarf_end) (mod->alt);
97 if (mod->alt_elf != NULL)
98 elf_end (mod->alt_elf);
99 if (mod->alt_fd != -1)
104 if (mod->ebl != NULL)
105 ebl_closebackend (mod->ebl);
107 if (mod->debug.elf != mod->main.elf)
108 free_file (&mod->debug);
109 free_file (&mod->main);
110 free_file (&mod->aux_sym);
112 if (mod->build_id_bits != NULL)
113 free (mod->build_id_bits);
115 if (mod->reloc_info != NULL)
116 free (mod->reloc_info);
123 dwfl_report_begin_add (Dwfl *dwfl __attribute__ ((unused)))
125 /* The lookup table will be cleared on demand, there is nothing we need
128 INTDEF (dwfl_report_begin_add)
131 dwfl_report_begin (Dwfl *dwfl)
133 /* Clear the segment lookup table. */
134 dwfl->lookup_elts = 0;
136 for (Dwfl_Module *m = dwfl->modulelist; m != NULL; m = m->next)
139 dwfl->offline_next_address = OFFLINE_REDZONE;
141 INTDEF (dwfl_report_begin)
143 static inline Dwfl_Module *
144 use (Dwfl_Module *mod, Dwfl_Module **tailp, Dwfl *dwfl)
149 if (unlikely (dwfl->lookup_module != NULL))
151 free (dwfl->lookup_module);
152 dwfl->lookup_module = NULL;
158 /* Report that a module called NAME spans addresses [START, END).
159 Returns the module handle, either existing or newly allocated,
160 or returns a null pointer for an allocation error. */
162 dwfl_report_module (Dwfl *dwfl, const char *name,
163 GElf_Addr start, GElf_Addr end)
165 Dwfl_Module **tailp = &dwfl->modulelist, **prevp = tailp;
167 for (Dwfl_Module *m = *prevp; m != NULL; m = *(prevp = &m->next))
169 if (m->low_addr == start && m->high_addr == end
170 && !strcmp (m->name, name))
172 /* This module is still here. Move it to the place in the list
173 after the last module already reported. */
176 return use (m, tailp, dwfl);
183 Dwfl_Module *mod = calloc (1, sizeof *mod);
187 mod->name = strdup (name);
188 if (mod->name == NULL)
192 __libdwfl_seterrno (DWFL_E_NOMEM);
196 mod->low_addr = start;
197 mod->high_addr = end;
200 return use (mod, tailp, dwfl);
202 INTDEF (dwfl_report_module)
205 /* Finish reporting the current set of modules to the library.
206 If REMOVED is not null, it's called for each module that
207 existed before but was not included in the current report.
208 Returns a nonzero return value from the callback.
209 DWFL cannot be used until this function has returned zero. */
211 dwfl_report_end (Dwfl *dwfl,
212 int (*removed) (Dwfl_Module *, void *,
213 const char *, Dwarf_Addr,
217 Dwfl_Module **tailp = &dwfl->modulelist;
218 while (*tailp != NULL)
220 Dwfl_Module *m = *tailp;
221 if (m->gc && removed != NULL)
223 int result = (*removed) (MODCB_ARGS (m), arg);
230 __libdwfl_module_free (m);
238 INTDEF (dwfl_report_end)