2 * Initialization code for .so modules
4 * Copyright (C) 2020 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include <sys/types.h>
45 /* The PT_LOAD segments are sorted in increasing order, and the first
46 * starts at the beginning of the ELF file. By parsing the file, we can
47 * find that first PT_LOAD segment, from which we can find the base
48 * address it wanted, and knowing mapbase where the binary was actually
49 * loaded, use them to work out the relocbase offset. */
50 static BOOL
get_relocbase(caddr_t mapbase
, caddr_t
*relocbase
)
54 const Elf64_Ehdr
*elf_header
= (Elf64_Ehdr
*) mapbase
;
56 const Elf32_Ehdr
*elf_header
= (Elf32_Ehdr
*) mapbase
;
58 const Elf_Phdr
*prog_header
= (const Elf_Phdr
*)(mapbase
+ elf_header
->e_phoff
);
60 for (i
= 0; i
< elf_header
->e_phnum
; i
++)
62 if (prog_header
->p_type
== PT_LOAD
)
64 caddr_t desired_base
= (caddr_t
)((prog_header
->p_vaddr
/ prog_header
->p_align
) * prog_header
->p_align
);
65 *relocbase
= (caddr_t
) (mapbase
- desired_base
);
75 /*************************************************************************
78 void __wine_init_so_dll(void)
80 #if defined(HAVE_DLADDR1) || (defined(HAVE_DLINFO) && defined(RTLD_SELF))
82 void (*init_func
)(int, char **, char **) = NULL
;
83 void (**init_array
)(int, char **, char **) = NULL
;
84 long i
, init_arraysz
= 0;
93 if (!dladdr1( __wine_init_so_dll
, &info
, (void **)&map
, RTLD_DL_LINKMAP
)) return;
95 if (dlinfo( RTLD_SELF
, RTLD_DI_LINKMAP
, &map
)) return;
98 for (dyn
= map
->l_ld
; dyn
->d_tag
; dyn
++)
100 caddr_t relocbase
= (caddr_t
)map
->l_addr
;
103 /* On older FreeBSD versions, l_addr was the absolute load address, now it's the relocation offset. */
104 if (offsetof(struct link_map
, l_addr
) == 0)
105 if (!get_relocbase(map
->l_addr
, &relocbase
))
110 case 0x60009994: init_array
= (void *)(relocbase
+ dyn
->d_un
.d_val
); break;
111 case 0x60009995: init_arraysz
= dyn
->d_un
.d_val
; break;
112 case 0x60009996: init_func
= (void *)(relocbase
+ dyn
->d_un
.d_val
); break;
116 if (init_func
) init_func( 0, NULL
, NULL
);
119 for (i
= 0; i
< init_arraysz
/ sizeof(*init_array
); i
++)
120 init_array
[i
]( 0, NULL
, NULL
);