Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178295630
D62423.1796455143.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
12 KB
Referenced Files
None
Subscribers
None
D62423.1796455143.diff
View Options
diff --git a/include/dlfcn.h b/include/dlfcn.h
--- a/include/dlfcn.h
+++ b/include/dlfcn.h
@@ -111,6 +111,43 @@
Dl_serpath dls_serpath[1]; /* there may be more than one */
} Dl_serinfo;
+/*
+ * Structure filled in by _dl_find_object(), compatible with glibc.
+ */
+#if defined(__i386__)
+#define DLFO_STRUCT_HAS_EH_DBASE 1
+#else
+#define DLFO_STRUCT_HAS_EH_DBASE 0
+#endif
+#if defined(__arm__)
+#define DLFO_STRUCT_HAS_EH_COUNT 1
+#define DLFO_EH_SEGMENT_TYPE PT_ARM_EXIDX
+#else
+#define DLFO_STRUCT_HAS_EH_COUNT 0
+#define DLFO_EH_SEGMENT_TYPE PT_GNU_EH_FRAME
+#endif
+
+struct link_map;
+
+struct dl_find_object {
+ __uint64_t dlfo_flags;
+ void *dlfo_map_start;
+ void *dlfo_map_end;
+ struct link_map *dlfo_link_map;
+ void *dlfo_eh_frame;
+#if DLFO_STRUCT_HAS_EH_DBASE
+ void *dlfo_eh_dbase;
+#ifndef __LP64__
+ unsigned int __dlfo_eh_dbase_pad;
+#endif
+#endif
+#if DLFO_STRUCT_HAS_EH_COUNT
+ int dlfo_eh_count;
+ unsigned int __dlfo_eh_count_pad;
+#endif
+ __uint64_t __dlfo_reserved[7];
+};
+
#endif /* __BSD_VISIBLE */
__BEGIN_DECLS
@@ -134,6 +171,7 @@
void (*_context_destroy)(void *_context));
void *dlvsym(void * __restrict, const char * __restrict,
const char * __restrict);
+int _dl_find_object(void *, struct dl_find_object *);
#endif /* __BSD_VISIBLE */
__END_DECLS
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc
--- a/lib/libc/gen/Makefile.inc
+++ b/lib/libc/gen/Makefile.inc
@@ -224,6 +224,7 @@
devname.3 \
directory.3 \
dirname.3 \
+ _dl_find_object.3 \
dl_iterate_phdr.3 \
dladdr.3 \
dlinfo.3 \
diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map
--- a/lib/libc/gen/Symbol.map
+++ b/lib/libc/gen/Symbol.map
@@ -466,6 +466,7 @@
};
FBSD_1.9 {
+ _dl_find_object;
freadlink;
fts_children;
fts_close;
diff --git a/lib/libc/gen/_dl_find_object.3 b/lib/libc/gen/_dl_find_object.3
new file mode 100644
--- /dev/null
+++ b/lib/libc/gen/_dl_find_object.3
@@ -0,0 +1,124 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.\" Copyright (c) 2026 ShengYi Hung
+.\"
+.Dd December 2, 2026
+.Dt _DL_FIND_OBJECT 3
+.Os
+.Sh NAME
+.Nm _dl_find_object
+.Nd find the loaded object and unwind data for an address
+.Sh LIBRARY
+For dynamically linked programs, the service is provided by the
+.Xr ld-elf.so.1 1
+dynamic linker.
+Statically linked programs use an implementation of
+.Fn _dl_find_object
+from libc.
+.Sh SYNOPSIS
+.In dlfcn.h
+.Ft int
+.Fn _dl_find_object "void *addr" "struct dl_find_object *result"
+.Sh DESCRIPTION
+The
+.Fn _dl_find_object
+function finds the loaded ELF object whose mapping contains
+.Fa addr ,
+and fills
+.Fa result
+with information about it.
+It is intended for in-process stack unwinders, which look up the unwind
+data for every stack frame.
+Unlike
+.Xr dl_iterate_phdr 3 ,
+it does not serialize callers, so threads that unwind at the same time
+do not wait for each other.
+.Pp
+The structure is defined as:
+.Bd -literal
+struct dl_find_object {
+ uint64_t dlfo_flags;
+ void *dlfo_map_start;
+ void *dlfo_map_end;
+ struct link_map *dlfo_link_map;
+ void *dlfo_eh_frame;
+#if DLFO_STRUCT_HAS_EH_DBASE
+ void *dlfo_eh_dbase;
+#endif
+#if DLFO_STRUCT_HAS_EH_COUNT
+ int dlfo_eh_count;
+#endif
+};
+.Ed
+.Pp
+The members are:
+.Bl -tag -width dlfo_map_start
+.It Va dlfo_flags
+Always zero.
+.It Va dlfo_map_start
+The start of the address range mapped for the object.
+.It Va dlfo_map_end
+The end of that range, exclusive.
+.It Va dlfo_link_map
+The object's link map, as returned by
+.Xr dlinfo 3
+with
+.Dv RTLD_DI_LINKMAP .
+.It Va dlfo_eh_frame
+The address of the object's unwind data segment, or
+.Dv NULL
+if the object has none.
+The segment type is given by the
+.Dv DLFO_EH_SEGMENT_TYPE
+macro, which is
+.Dv PT_ARM_EXIDX
+on arm and
+.Dv PT_GNU_EH_FRAME
+on all other architectures.
+.It Va dlfo_eh_dbase
+Only present if
+.Dv DLFO_STRUCT_HAS_EH_DBASE
+is non-zero, which is the case on i386.
+The base address for
+.Dv DW_EH_PE_datarel
+encoded pointers, which is the object's global offset table.
+.It Va dlfo_eh_count
+Only present if
+.Dv DLFO_STRUCT_HAS_EH_COUNT
+is non-zero, which is the case on arm.
+The number of entries in the
+.Dv PT_ARM_EXIDX
+segment.
+.El
+.Pp
+The pointers in
+.Fa result
+are only valid while the object stays loaded.
+.Pp
+In statically linked programs, only the program itself is known, and
+.Va dlfo_link_map
+is
+.Dv NULL .
+.Sh RETURN VALUES
+The
+.Fn _dl_find_object
+function returns 0 if
+.Fa addr
+belongs to a loaded object, and \-1 otherwise.
+.Sh SEE ALSO
+.Xr ld-elf.so.1 1 ,
+.Xr dl_iterate_phdr 3 ,
+.Xr dladdr 3 ,
+.Xr dlinfo 3
+.Sh STANDARDS
+The
+.Fn _dl_find_object
+function and
+.Vt struct dl_find_object
+are compatible with the GNU C Library.
+.Sh HISTORY
+The
+.Fn _dl_find_object
+function first appeared in
+.Fx 16.0 .
diff --git a/lib/libc/gen/dl_iterate_phdr.3 b/lib/libc/gen/dl_iterate_phdr.3
--- a/lib/libc/gen/dl_iterate_phdr.3
+++ b/lib/libc/gen/dl_iterate_phdr.3
@@ -14,7 +14,7 @@
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.\" $OpenBSD: dl_iterate_phdr.3,v 1.3 2007/05/31 19:19:48 jmc Exp $
-.Dd April 5, 2021
+.Dd December 2, 2026
.Dt DL_ITERATE_PHDR 3
.Os
.Sh NAME
@@ -108,6 +108,7 @@
.Sh SEE ALSO
.Xr ld 1 ,
.Xr ld-elf.so.1 1 ,
+.Xr _dl_find_object 3 ,
.Xr dlopen 3 ,
.Xr elf 5
.Sh HISTORY
diff --git a/lib/libc/gen/dlfcn.c b/lib/libc/gen/dlfcn.c
--- a/lib/libc/gen/dlfcn.c
+++ b/lib/libc/gen/dlfcn.c
@@ -253,6 +253,72 @@
return (error);
}
+#pragma weak _dl_find_object
+int
+_dl_find_object(void *addr __unused, struct dl_find_object *result __unused)
+{
+#if defined(IN_LIBDL) || defined(PIC)
+ return (-1);
+#else
+ const Elf_Phdr *ph, *phlimit;
+ Elf_Addr a, lo, hi, start;
+ int found;
+
+ __init_elf_aux_vector();
+ if (__elf_aux_vector == NULL)
+ return (-1);
+ _once(&dl_phdr_info_once, dl_init_phdr_info);
+
+ a = (Elf_Addr)addr;
+ lo = ~(Elf_Addr)0;
+ hi = 0;
+ found = 0;
+ phlimit = phdr_info.dlpi_phdr + phdr_info.dlpi_phnum;
+ for (ph = phdr_info.dlpi_phdr; ph < phlimit; ph++) {
+ if (ph->p_type != PT_LOAD)
+ continue;
+ start = phdr_info.dlpi_addr + ph->p_vaddr;
+ if (start <= a && a < start + ph->p_memsz)
+ found = 1;
+ if (start < lo)
+ lo = start;
+ if (start + ph->p_memsz > hi)
+ hi = start + ph->p_memsz;
+ }
+ if (!found)
+ return (-1);
+
+ memset(result, 0, sizeof(*result));
+ result->dlfo_map_start = (void *)lo;
+ result->dlfo_map_end = (void *)hi;
+ for (ph = phdr_info.dlpi_phdr; ph < phlimit; ph++) {
+ start = phdr_info.dlpi_addr + ph->p_vaddr;
+ if (ph->p_type == DLFO_EH_SEGMENT_TYPE) {
+ result->dlfo_eh_frame = (void *)start;
+#if DLFO_STRUCT_HAS_EH_COUNT
+ result->dlfo_eh_count = ph->p_memsz / 8;
+#endif
+ }
+#if DLFO_STRUCT_HAS_EH_DBASE
+ if (ph->p_type == PT_DYNAMIC) {
+ const Elf_Dyn *dyn;
+
+ for (dyn = (const Elf_Dyn *)start; dyn->d_tag != DT_NULL;
+ dyn++) {
+ if (dyn->d_tag == DT_PLTGOT) {
+ result->dlfo_eh_dbase = (void *)
+ (phdr_info.dlpi_addr +
+ dyn->d_un.d_ptr);
+ break;
+ }
+ }
+ }
+#endif
+ }
+ return (0);
+#endif
+}
+
#pragma weak fdlopen
void *
fdlopen(int fd __unused, int mode __unused)
diff --git a/libexec/rtld-elf/Symbol.map b/libexec/rtld-elf/Symbol.map
--- a/libexec/rtld-elf/Symbol.map
+++ b/libexec/rtld-elf/Symbol.map
@@ -26,6 +26,10 @@
rtld_set_var;
};
+FBSD_1.9 {
+ _dl_find_object;
+};
+
FBSDprivate_1.0 {
_dl_iterate_phdr_locked;
_rtld_thread_init;
diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c
--- a/libexec/rtld-elf/rtld.c
+++ b/libexec/rtld-elf/rtld.c
@@ -47,6 +47,8 @@
#include <sys/uio.h>
#include <sys/utsname.h>
+#include <machine/atomic.h>
+
#include <dlfcn.h>
#include <err.h>
#include <errno.h>
@@ -109,6 +111,8 @@
static void dump_auxv(Elf_Auxinfo **aux_info);
static void errmsg_restore(struct dlerror_save *);
static struct dlerror_save *errmsg_save(void);
+static void dlfo_init(void);
+static void dlfo_update(Obj_Entry *, bool);
static void *fill_search_info(const char *, size_t, void *);
static char *find_library(const char *, const Obj_Entry *, int *);
static const char *gethints(bool);
@@ -282,6 +286,7 @@
int _dl_iterate_phdr_locked(__dl_iterate_hdr_callback, void *) __exported;
int dl_iterate_phdr(__dl_iterate_hdr_callback, void *) __exported;
int _rtld_addr_phdr(const void *, struct dl_phdr_info *) __exported;
+int _dl_find_object(void *, struct dl_find_object *) __exported;
int _rtld_get_stack_prot(void) __exported;
int _rtld_is_dlopened(void *) __exported;
void _rtld_error(const char *, ...) __exported;
@@ -934,6 +939,9 @@
}
dbg("tcb_list_entry_offset %zu", tcb_list_entry_offset);
+ /* Before any code in the loaded objects runs. */
+ dlfo_init();
+
if (relocate_objects(obj_main,
ld_bind_now != NULL && *ld_bind_now != '\0', &obj_rtld,
SYMLOOK_EARLY, NULL) == -1)
@@ -3051,6 +3059,7 @@
TAILQ_INSERT_TAIL(&obj_list, obj, next);
obj_count++;
obj_loads++;
+ dlfo_update(obj, true);
linkmap_add(obj); /* for GDB & dlinfo() */
max_stack_flags |= obj->stack_flags;
@@ -3135,6 +3144,7 @@
TAILQ_INSERT_TAIL(&obj_list, obj, next);
obj_count++;
obj_loads++;
+ dlfo_update(obj, true);
linkmap_add(obj); /* for GDB & dlinfo() */
max_stack_flags |= obj->stack_flags;
@@ -4370,6 +4380,170 @@
return (1);
}
+struct dlfo_entry {
+ uintptr_t start;
+ uintptr_t end;
+ Obj_Entry *obj;
+ void *eh_frame;
+#if DLFO_STRUCT_HAS_EH_DBASE
+ void *eh_dbase;
+#endif
+#if DLFO_STRUCT_HAS_EH_COUNT
+ int eh_count;
+#endif
+};
+
+struct dlfo_table {
+ u_int size;
+ u_int count;
+ struct dlfo_entry ent[];
+};
+
+static struct dlfo_table *dlfo_tables[2];
+static u_int dlfo_gen;
+static bool dlfo_ready;
+
+static void
+dlfo_fill(struct dlfo_entry *e, Obj_Entry *obj)
+{
+ const Elf_Phdr *ph;
+
+ e->start = (uintptr_t)obj->mapbase;
+ e->end = (uintptr_t)obj->mapbase + obj->mapsize;
+ e->obj = obj;
+ e->eh_frame = NULL;
+#if DLFO_STRUCT_HAS_EH_DBASE
+ e->eh_dbase = obj->pltgot;
+#endif
+#if DLFO_STRUCT_HAS_EH_COUNT
+ e->eh_count = 0;
+#endif
+ for (ph = obj->phdr; ph < obj->phdr + obj->phnum; ph++) {
+ if (ph->p_type == DLFO_EH_SEGMENT_TYPE) {
+ e->eh_frame = obj->relocbase + ph->p_vaddr;
+#if DLFO_STRUCT_HAS_EH_COUNT
+ e->eh_count = ph->p_memsz / 8;
+#endif
+ break;
+ }
+ }
+}
+
+/*
+ * Add obj to the table (sorted) or remove it. Called with the bind lock held
+ * for writing.
+ */
+static void
+dlfo_update(Obj_Entry *obj, bool add)
+{
+ const struct dlfo_table *cur;
+ struct dlfo_table *t;
+ u_int gen, hi, i, lo, mid, n, size;
+
+ /* Objects loaded at startup are added by dlfo_init(). */
+ if (!dlfo_ready)
+ return;
+
+ cur = dlfo_tables[dlfo_gen & 1];
+ n = cur != NULL ? cur->count : 0;
+ gen = dlfo_gen + 1;
+ t = dlfo_tables[gen & 1];
+ if (t == NULL || t->size < n + 1) {
+ size = MAX(2 * (n + 1), 64);
+ t = xmalloc(sizeof(*t) + size * sizeof(t->ent[0]));
+ t->size = size;
+ }
+
+ // Two sequential write may inconsist if there is a reader looking at
+ // this.
+ atomic_thread_fence_rel();
+ if (add) {
+ lo = 0;
+ hi = n;
+ while (lo < hi) {
+ mid = lo + (hi - lo) / 2;
+ if (cur->ent[mid].start <= (uintptr_t)obj->mapbase)
+ lo = mid + 1;
+ else
+ hi = mid;
+ }
+ if (n > 0) {
+ memcpy(&t->ent[0], &cur->ent[0],
+ lo * sizeof(t->ent[0]));
+ memcpy(&t->ent[lo + 1], &cur->ent[lo],
+ (n - lo) * sizeof(t->ent[0]));
+ }
+ dlfo_fill(&t->ent[lo], obj);
+ n++;
+ } else {
+ for (i = lo = 0; i < n; i++) {
+ if (cur->ent[i].obj != obj)
+ t->ent[lo++] = cur->ent[i];
+ }
+ n = lo;
+ }
+ t->count = n;
+ dlfo_tables[gen & 1] = t;
+ atomic_store_rel_int(&dlfo_gen, gen);
+}
+
+static void
+dlfo_init(void)
+{
+ Obj_Entry *obj;
+
+ dlfo_ready = true;
+ TAILQ_FOREACH(obj, &obj_list, next) {
+ if (!obj->marker)
+ dlfo_update(obj, true);
+ }
+ dlfo_update(&obj_rtld, true); /* rtld is not on obj_list. */
+}
+
+int
+_dl_find_object(void *addr, struct dl_find_object *result)
+{
+ const struct dlfo_table *t;
+ struct dlfo_entry e;
+ uintptr_t a;
+ u_int gen, hi, lo, mid;
+
+ a = (uintptr_t)addr;
+ do {
+ gen = atomic_load_acq_int(&dlfo_gen);
+ t = dlfo_tables[gen & 1];
+ if (t == NULL)
+ return (-1);
+ lo = 0;
+ hi = MIN(t->count, t->size);
+ while (lo < hi) {
+ mid = lo + (hi - lo) / 2;
+ if (t->ent[mid].start <= a)
+ lo = mid + 1;
+ else
+ hi = mid;
+ }
+ if (lo > 0)
+ e = t->ent[lo - 1];
+ atomic_thread_fence_acq();
+ } while (atomic_load_int(&dlfo_gen) != gen);
+
+ if (lo == 0 || a >= e.end)
+ return (-1);
+ memset(result, 0, sizeof(*result));
+ result->dlfo_map_start = (void *)e.start;
+ result->dlfo_map_end = (void *)e.end;
+ result->dlfo_link_map = &e.obj->linkmap;
+ result->dlfo_eh_frame = e.eh_frame;
+#if DLFO_STRUCT_HAS_EH_DBASE
+ result->dlfo_eh_dbase = e.eh_dbase;
+#endif
+#if DLFO_STRUCT_HAS_EH_COUNT
+ result->dlfo_eh_count = e.eh_count;
+#endif
+ return (0);
+}
+
int
dladdr(const void *addr, Dl_info *info)
{
@@ -5470,6 +5644,7 @@
*/
TAILQ_REMOVE(&obj_list, obj, next);
obj_count--;
+ dlfo_update(obj, false);
if (obj->filtees_loaded) {
if (next != NULL) {
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 5, 7:19 AM (21 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42222350
Default Alt Text
D62423.1796455143.diff (12 KB)
Attached To
Mode
D62423: rtld: Add _dl_find_object
Attached
Detach File
Event Timeline