Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <cassert>
23 : #include <typeinfo>
24 : #include <unordered_map>
25 : #include <utility>
26 : #include <vector>
27 :
28 : #include <dlfcn.h>
29 :
30 : #include <osl/mutex.hxx>
31 : #include <rtl/instance.hxx>
32 : #include <rtl/strbuf.hxx>
33 : #include <rtl/ustring.hxx>
34 : #include <sal/log.hxx>
35 : #include <typelib/typedescription.h>
36 :
37 : #include <rtti.hxx>
38 : #include <share.hxx>
39 :
40 : namespace {
41 :
42 : class RTTI
43 : {
44 : typedef std::unordered_map< OUString, std::type_info *, OUStringHash > t_rtti_map;
45 :
46 : osl::Mutex m_mutex;
47 : t_rtti_map m_rttis;
48 : t_rtti_map m_generatedRttis;
49 :
50 : void * m_hApp;
51 :
52 : public:
53 : RTTI();
54 : ~RTTI();
55 :
56 : std::type_info * getRTTI(typelib_TypeDescription const &);
57 : };
58 :
59 196 : RTTI::RTTI()
60 : #if defined(FREEBSD) && __FreeBSD_version < 702104
61 : : m_hApp( dlopen( 0, RTLD_NOW | RTLD_GLOBAL ) )
62 : #else
63 196 : : m_hApp( dlopen( 0, RTLD_LAZY ) )
64 : #endif
65 : {
66 196 : }
67 :
68 392 : RTTI::~RTTI()
69 : {
70 196 : dlclose( m_hApp );
71 196 : }
72 :
73 22966 : std::type_info * RTTI::getRTTI(typelib_TypeDescription const & pTypeDescr)
74 : {
75 : std::type_info * rtti;
76 :
77 22966 : OUString const & unoName = OUString::unacquired(&pTypeDescr.pTypeName);
78 :
79 22966 : osl::MutexGuard guard( m_mutex );
80 22966 : t_rtti_map::const_iterator iFind( m_rttis.find( unoName ) );
81 22966 : if (iFind == m_rttis.end())
82 : {
83 : // RTTI symbol
84 255 : OStringBuffer buf( 64 );
85 255 : buf.append( "_ZTIN" );
86 255 : sal_Int32 index = 0;
87 1275 : do
88 : {
89 1275 : OUString token( unoName.getToken( 0, '.', index ) );
90 1275 : buf.append( token.getLength() );
91 2550 : OString c_token( OUStringToOString( token, RTL_TEXTENCODING_ASCII_US ) );
92 2550 : buf.append( c_token );
93 : }
94 1275 : while (index >= 0);
95 255 : buf.append( 'E' );
96 :
97 510 : OString symName( buf.makeStringAndClear() );
98 : #if defined(FREEBSD) && __FreeBSD_version < 702104 /* #i22253# */
99 : rtti = (std::type_info *)dlsym( RTLD_DEFAULT, symName.getStr() );
100 : #else
101 255 : rtti = static_cast<std::type_info *>(dlsym( m_hApp, symName.getStr() ));
102 : #endif
103 :
104 255 : if (rtti)
105 : {
106 : std::pair< t_rtti_map::iterator, bool > insertion (
107 253 : m_rttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
108 : SAL_WARN_IF( !insertion.second, "bridges", "key " << unoName << " already in rtti map" );
109 : }
110 : else
111 : {
112 : // try to lookup the symbol in the generated rtti map
113 2 : t_rtti_map::const_iterator iFind2( m_generatedRttis.find( unoName ) );
114 2 : if (iFind2 == m_generatedRttis.end())
115 : {
116 : // we must generate it !
117 : // symbol and rtti-name is nearly identical,
118 : // the symbol is prefixed with _ZTI
119 2 : char const * rttiName = symName.getStr() +4;
120 : #if OSL_DEBUG_LEVEL > 1
121 : fprintf( stderr,"generated rtti for %s\n", rttiName );
122 : #endif
123 2 : switch (pTypeDescr.eTypeClass) {
124 : case typelib_TypeClass_EXCEPTION:
125 : {
126 : typelib_CompoundTypeDescription const & ctd
127 : = reinterpret_cast<
128 : typelib_CompoundTypeDescription const &>(
129 2 : pTypeDescr);
130 2 : if (ctd.pBaseTypeDescription)
131 : {
132 : // ensure availability of base
133 : std::type_info * base_rtti = getRTTI(
134 2 : ctd.pBaseTypeDescription->aBase);
135 : rtti = new __cxxabiv1::__si_class_type_info(
136 2 : strdup( rttiName ), static_cast<__cxxabiv1::__class_type_info *>(base_rtti) );
137 : }
138 : else
139 : {
140 : // this class has no base class
141 0 : rtti = new __cxxabiv1::__class_type_info( strdup( rttiName ) );
142 : }
143 2 : break;
144 : }
145 : case typelib_TypeClass_INTERFACE:
146 : {
147 : typelib_InterfaceTypeDescription const & itd
148 : = reinterpret_cast<
149 : typelib_InterfaceTypeDescription const &>(
150 0 : pTypeDescr);
151 0 : std::vector<std::type_info *> bases;
152 0 : for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i) {
153 0 : bases.push_back(getRTTI(itd.ppBaseTypes[i]->aBase));
154 : }
155 0 : switch (itd.nBaseTypes) {
156 : case 0:
157 : rtti = new __cxxabiv1::__class_type_info(
158 0 : strdup(rttiName));
159 0 : break;
160 : case 1:
161 : rtti = new __cxxabiv1::__si_class_type_info(
162 : strdup(rttiName),
163 : static_cast<__cxxabiv1::__class_type_info *>(
164 0 : bases[0]));
165 0 : break;
166 : default:
167 : {
168 : char * pad = new char[
169 : sizeof (__cxxabiv1::__vmi_class_type_info)
170 0 : + ((itd.nBaseTypes - 1)
171 0 : * sizeof (
172 0 : __cxxabiv1::__base_class_type_info))];
173 : __cxxabiv1::__vmi_class_type_info * info
174 : = new(pad)
175 : __cxxabiv1::__vmi_class_type_info(
176 : strdup(rttiName),
177 0 : __cxxabiv1::__vmi_class_type_info::__flags_unknown_mask);
178 0 : info->__base_count = itd.nBaseTypes;
179 0 : for (sal_Int32 i = 0; i != itd.nBaseTypes; ++i)
180 : {
181 : info->__base_info[i].__base_type
182 : = static_cast<
183 : __cxxabiv1::__class_type_info *>(
184 0 : bases[i]);
185 : info->__base_info[i].__offset_flags
186 : = (__cxxabiv1::__base_class_type_info::__public_mask
187 0 : | ((8 * i) << __cxxabiv1::__base_class_type_info::__offset_shift));
188 : }
189 0 : rtti = info;
190 0 : break;
191 : }
192 : }
193 0 : break;
194 : }
195 : default:
196 : assert(false); // cannot happen
197 : }
198 2 : if (rtti != 0) {
199 : std::pair< t_rtti_map::iterator, bool > insertion (
200 2 : m_generatedRttis.insert( t_rtti_map::value_type( unoName, rtti ) ) );
201 : SAL_WARN_IF( !insertion.second, "bridges", "key " << unoName << " already in generated rtti map" );
202 : }
203 : }
204 : else // taking already generated rtti
205 : {
206 0 : rtti = iFind2->second;
207 : }
208 255 : }
209 : }
210 : else
211 : {
212 22711 : rtti = iFind->second;
213 : }
214 :
215 22966 : return rtti;
216 : }
217 :
218 : struct theRttiFactory: public rtl::Static<RTTI, theRttiFactory> {};
219 :
220 : }
221 :
222 22964 : std::type_info * x86_64::getRtti(typelib_TypeDescription const & type) {
223 22964 : return theRttiFactory::get().getRTTI(type);
224 : }
225 :
226 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|