1 /* Locating objects in the process image. ld.so implementation.
2 Copyright (C) 2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _DL_FIND_EH_FRAME_H
20 #define _DL_FIND_EH_FRAME_H
21
22 #include <assert.h>
23 #include <dlfcn.h>
24 #include <ldsodefs.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27
28 /* Internal version of struct dl_find_object. Does not include the
29 (yet unused) flags member. We need to make a copy of data also in
30 struct link_map to support non-contiguous mappings, and to support
31 software transactional memory (the link map is not covered by
32 transactions). */
33 struct dl_find_object_internal
34 {
35 uintptr_t map_start;
36 uintptr_t map_end; /* Set to map_start by dlclose. */
37 struct link_map *map; /* Set to NULL by dlclose. */
38 void *eh_frame;
39 #if DLFO_STRUCT_HAS_EH_DBASE
40 void *eh_dbase;
41 #endif
42 #if DLFO_STRUCT_HAS_EH_COUNT
43 int eh_count;
44 #endif
45 };
46
47 static inline void
_dl_find_object_to_external(struct dl_find_object_internal * internal,struct dl_find_object * external)48 _dl_find_object_to_external (struct dl_find_object_internal *internal,
49 struct dl_find_object *external)
50 {
51 external->dlfo_flags = 0;
52 external->dlfo_map_start = (void *) internal->map_start;
53 external->dlfo_map_end = (void *) internal->map_end;
54 external->dlfo_link_map = internal->map;
55 external->dlfo_eh_frame = internal->eh_frame;
56 # if DLFO_STRUCT_HAS_EH_DBASE
57 external->dlfo_eh_dbase = internal->eh_dbase;
58 # endif
59 # if DLFO_STRUCT_HAS_EH_COUNT
60 external->dlfo_eh_count = internal->eh_count;
61 # endif
62 }
63
64 /* Extract the object location data from a link map and writes it to
65 *RESULT. */
66 static void __attribute__ ((unused))
_dl_find_object_from_map(struct link_map * l,struct dl_find_object_internal * result)67 _dl_find_object_from_map (struct link_map *l,
68 struct dl_find_object_internal *result)
69 {
70 result->map_start = (uintptr_t) l->l_map_start;
71 result->map_end = (uintptr_t) l->l_map_end;
72 result->map = l;
73
74 #if DLFO_STRUCT_HAS_EH_DBASE
75 result->eh_dbase = (void *) l->l_info[DT_PLTGOT];
76 #endif
77
78 for (const ElfW(Phdr) *ph = l->l_phdr, *ph_end = l->l_phdr + l->l_phnum;
79 ph < ph_end; ++ph)
80 if (ph->p_type == DLFO_EH_SEGMENT_TYPE)
81 {
82 result->eh_frame = (void *) (ph->p_vaddr + l->l_addr);
83 #if DLFO_STRUCT_HAS_EH_COUNT
84 result->eh_count = ph->p_memsz / 8;
85 #endif
86 return;
87 }
88
89 /* Object has no exception handling segment. */
90 result->eh_frame = NULL;
91 #if DLFO_STRUCT_HAS_EH_COUNT
92 result->eh_count = 0;
93 #endif
94 }
95
96 /* Called by the dynamic linker to set up the data structures for the
97 initially loaded objects. This creates a few persistent
98 allocations, so it should be called with the minimal malloc. */
99 void _dl_find_object_init (void) attribute_hidden;
100
101 /* Called by dlopen/dlmopen to add new objects to the DWARF EH frame
102 data structures. NEW_MAP is the dlopen'ed link map. Link maps on
103 the l_next list are added if l_object_processed is 0. Needs to
104 be protected by loader write lock. Returns true on success, false
105 on malloc failure. */
106 bool _dl_find_object_update (struct link_map *new_map) attribute_hidden;
107
108 /* Called by dlclose to remove the link map from the DWARF EH frame
109 data structures. Needs to be protected by loader write lock. */
110 void _dl_find_object_dlclose (struct link_map *l) attribute_hidden;
111
112 /* Called from __libc_freeres to deallocate malloc'ed memory. */
113 void _dl_find_object_freeres (void) attribute_hidden;
114
115 #endif /* _DL_FIND_OBJECT_H */
116