Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/winecrt0/dll_soinit.c
12343 views
1
/*
2
* Initialization code for .so modules
3
*
4
* Copyright (C) 2020 Alexandre Julliard
5
*
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.
10
*
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.
15
*
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
19
*/
20
21
#if 0
22
#pragma makedep unix
23
#endif
24
25
#include "config.h"
26
27
#include <stdarg.h>
28
#include <stdio.h>
29
#include <string.h>
30
#include <stdlib.h>
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <unistd.h>
34
#include <dlfcn.h>
35
#ifdef HAVE_ELF_H
36
# include <elf.h>
37
#endif
38
#ifdef HAVE_LINK_H
39
# include <link.h>
40
#endif
41
#include "windef.h"
42
43
44
#ifdef __FreeBSD__
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)
51
{
52
Elf_Half i;
53
#ifdef _WIN64
54
const Elf64_Ehdr *elf_header = (Elf64_Ehdr*) mapbase;
55
#else
56
const Elf32_Ehdr *elf_header = (Elf32_Ehdr*) mapbase;
57
#endif
58
const Elf_Phdr *prog_header = (const Elf_Phdr *)(mapbase + elf_header->e_phoff);
59
60
for (i = 0; i < elf_header->e_phnum; i++)
61
{
62
if (prog_header->p_type == PT_LOAD)
63
{
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);
66
return TRUE;
67
}
68
prog_header++;
69
}
70
return FALSE;
71
}
72
#endif
73
74
75
/*************************************************************************
76
* __wine_init_so_dll
77
*/
78
void __wine_init_so_dll(void)
79
{
80
#if defined(HAVE_DLADDR1) || (defined(HAVE_DLINFO) && defined(RTLD_SELF))
81
struct link_map *map;
82
void (*init_func)(int, char **, char **) = NULL;
83
void (**init_array)(int, char **, char **) = NULL;
84
long i, init_arraysz = 0;
85
#ifdef _WIN64
86
const Elf64_Dyn *dyn;
87
#else
88
const Elf32_Dyn *dyn;
89
#endif
90
91
#ifdef HAVE_DLADDR1
92
Dl_info info;
93
if (!dladdr1( __wine_init_so_dll, &info, (void **)&map, RTLD_DL_LINKMAP )) return;
94
#else
95
if (dlinfo( RTLD_SELF, RTLD_DI_LINKMAP, &map )) return;
96
#endif
97
98
for (dyn = map->l_ld; dyn->d_tag; dyn++)
99
{
100
caddr_t relocbase = (caddr_t)map->l_addr;
101
102
#ifdef __FreeBSD__
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))
106
return;
107
#endif
108
switch (dyn->d_tag)
109
{
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;
113
}
114
}
115
116
if (init_func) init_func( 0, NULL, NULL );
117
118
if (init_array)
119
for (i = 0; i < init_arraysz / sizeof(*init_array); i++)
120
init_array[i]( 0, NULL, NULL );
121
#endif
122
}
123
124