LCOV - code coverage report
Current view: top level - sal/osl/unx - module.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 57 81 70.4 %
Date: 2014-04-14 Functions: 8 11 72.7 %
Legend: Lines: hit not hit

          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 <sal/log.hxx>
      23             : #include <sal/types.h>
      24             : #include <osl/module.h>
      25             : #include <osl/thread.h>
      26             : #include <osl/process.h>
      27             : #include <osl/file.h>
      28             : 
      29             : #include "system.h"
      30             : 
      31             : #ifdef AIX
      32             : #include <sys/ldr.h>
      33             : #endif
      34             : 
      35             : #ifdef ANDROID
      36             : #include <osl/detail/android-bootstrap.h>
      37             : #endif
      38             : 
      39             : /* implemented in file.c */
      40             : extern "C" int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
      41             : 
      42       85018 : static bool getModulePathFromAddress(void * address, rtl_String ** path) {
      43       85018 :     bool result = false;
      44             :     // We do want to have this functionality also in the
      45             :     // DISABLE_DYNLOADING case, I think?
      46             : #if defined(AIX)
      47             :     int size = 4 * 1024;
      48             :     char *buf, *filename=NULL;
      49             :     struct ld_info *lp;
      50             : 
      51             :     if ((buf = (char*)malloc(size)) == NULL)
      52             :         return false;
      53             : 
      54             :     //figure out how big a buffer we need
      55             :     while (loadquery(L_GETINFO, buf, size) == -1 && errno == ENOMEM)
      56             :     {
      57             :         size += 4 * 1024;
      58             :         free(buf);
      59             :         if ((buf = (char*)malloc(size)) == NULL)
      60             :             return false;
      61             :     }
      62             : 
      63             :     lp = (struct ld_info*) buf;
      64             :     while (lp)
      65             :     {
      66             :         unsigned long start = (unsigned long)lp->ldinfo_dataorg;
      67             :         unsigned long end = start + lp->ldinfo_datasize;
      68             :         if (start <= (unsigned long)address && end > (unsigned long)address)
      69             :         {
      70             :             filename = lp->ldinfo_filename;
      71             :             break;
      72             :         }
      73             :         if (!lp->ldinfo_next)
      74             :             break;
      75             :         lp = (struct ld_info*) ((char *) lp + lp->ldinfo_next);
      76             :     }
      77             : 
      78             :     if (filename)
      79             :     {
      80             :         rtl_string_newFromStr(path, filename);
      81             :         result = sal_True;
      82             :     }
      83             :     else
      84             :     {
      85             :         result = sal_False;
      86             :     }
      87             : 
      88             :     free(buf);
      89             : #else
      90             :     Dl_info dl_info;
      91             : 
      92             : #if defined(ANDROID) && !defined(DISABLE_DYNLOADING)
      93             :     result = lo_dladdr(address, &dl_info) != 0;
      94             : #else
      95       85018 :     result = dladdr(address, &dl_info) != 0;
      96             : #endif
      97             : 
      98       85018 :     if (result)
      99             :     {
     100       85018 :         rtl_string_newFromStr(path, dl_info.dli_fname);
     101             : #if defined(ANDROID) && !defined(DISABLE_DYNLOADING)
     102             :         free((void *) dl_info.dli_fname);
     103             : #endif
     104             :     }
     105             : #endif
     106       85018 :     return result;
     107             : }
     108             : 
     109             : #ifndef DISABLE_DYNLOADING
     110             : 
     111             : /*****************************************************************************/
     112             : /* osl_loadModule */
     113             : /*****************************************************************************/
     114             : 
     115       42514 : oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
     116             : {
     117       42514 :     oslModule pModule=0;
     118       42514 :     rtl_uString* ustrTmp = NULL;
     119             : 
     120             :     SAL_WARN_IF(ustrModuleName == 0, "sal.osl", "string is not valid");
     121             : 
     122             :     /* ensure ustrTmp hold valid string */
     123       42514 :     if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp))
     124           0 :         rtl_uString_assign(&ustrTmp, ustrModuleName);
     125             : 
     126       42514 :     if (ustrTmp)
     127             :     {
     128             :         char buffer[PATH_MAX];
     129             : 
     130       42514 :         if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length))
     131       42514 :             pModule = osl_loadModuleAscii(buffer, nRtldMode);
     132       42514 :         rtl_uString_release(ustrTmp);
     133             :     }
     134             : 
     135       42514 :     return pModule;
     136             : }
     137             : 
     138             : /*****************************************************************************/
     139             : /* osl_loadModuleAscii */
     140             : /*****************************************************************************/
     141             : 
     142       42514 : oslModule SAL_CALL osl_loadModuleAscii(const sal_Char *pModuleName, sal_Int32 nRtldMode)
     143             : {
     144             :     SAL_WARN_IF(
     145             :         ((nRtldMode & SAL_LOADMODULE_LAZY) != 0
     146             :          && (nRtldMode & SAL_LOADMODULE_NOW) != 0),
     147             :         "sal.osl", "only either LAZY or NOW");
     148       42514 :     if (pModuleName)
     149             :     {
     150             : #ifdef ANDROID
     151             :         (void) nRtldMode;
     152             :         void *pLib = lo_dlopen(pModuleName);
     153             : #else
     154             :         int rtld_mode =
     155       42514 :             ((nRtldMode & SAL_LOADMODULE_NOW) ? RTLD_NOW : RTLD_LAZY) |
     156       42514 :             ((nRtldMode & SAL_LOADMODULE_GLOBAL) ? RTLD_GLOBAL : RTLD_LOCAL);
     157       42514 :         void* pLib = dlopen(pModuleName, rtld_mode);
     158             : 
     159             :         SAL_INFO_IF(
     160             :             pLib == 0, "sal.osl",
     161             :             "dlopen(" << pModuleName << ", " << rtld_mode << "): "
     162             :                 << dlerror());
     163             : #endif
     164       42514 :         return ((oslModule)(pLib));
     165             :     }
     166           0 :     return NULL;
     167             : }
     168             : 
     169           0 : oslModule osl_loadModuleRelativeAscii(
     170             :     oslGenericFunction baseModule, char const * relativePath, sal_Int32 mode)
     171             : {
     172             :     SAL_WARN_IF(relativePath == 0, "sal.osl", "illegal argument");
     173           0 :     if (relativePath[0] == '/') {
     174           0 :         return osl_loadModuleAscii(relativePath, mode);
     175             :     } else {
     176           0 :         rtl_String * path = NULL;
     177           0 :         rtl_String * suffix = NULL;
     178             :         oslModule module;
     179           0 :         if (!getModulePathFromAddress(
     180           0 :                 reinterpret_cast< void * >(baseModule), &path))
     181             :         {
     182           0 :             return NULL;
     183             :         }
     184             :         rtl_string_newFromStr_WithLength(
     185             :             &path, path->buffer,
     186           0 :             (rtl_str_lastIndexOfChar_WithLength(path->buffer, path->length, '/')
     187           0 :              + 1));
     188             :             /* cut off everything after the last slash; should the original path
     189             :                contain no slash, the resulting path is the empty string */
     190           0 :         rtl_string_newFromStr(&suffix, relativePath);
     191           0 :         rtl_string_newConcat(&path, path, suffix);
     192           0 :         rtl_string_release(suffix);
     193           0 :         module = osl_loadModuleAscii(path->buffer, mode);
     194           0 :         rtl_string_release(path);
     195           0 :         return module;
     196             :     }
     197             : }
     198             : 
     199             : #endif // !DISABLE_DYNLOADING
     200             : 
     201             : /*****************************************************************************/
     202             : /* osl_getModuleHandle */
     203             : /*****************************************************************************/
     204             : 
     205             : sal_Bool SAL_CALL
     206           0 : osl_getModuleHandle(rtl_uString *, oslModule *pResult)
     207             : {
     208             : #if !defined(DISABLE_DYNLOADING) || defined(IOS)
     209           0 :     *pResult = (oslModule) RTLD_DEFAULT;
     210             : #else
     211             :     *pResult = NULL;
     212             : #endif
     213           0 :     return sal_True;
     214             : }
     215             : 
     216             : #ifndef DISABLE_DYNLOADING
     217             : 
     218             : /*****************************************************************************/
     219             : /* osl_unloadModule */
     220             : /*****************************************************************************/
     221       42515 : void SAL_CALL osl_unloadModule(oslModule hModule)
     222             : {
     223       42515 :     if (hModule)
     224             :     {
     225             : #ifdef ANDROID
     226             :         int nRet = lo_dlclose(hModule);
     227             : #else
     228       42507 :         int nRet = dlclose(hModule);
     229             : #endif
     230             :         SAL_INFO_IF(
     231             :             nRet != 0, "sal.osl", "dlclose(" << hModule << "): " << dlerror());
     232             :     }
     233       42515 : }
     234             : 
     235             : #endif // !DISABLE_DYNLOADING
     236             : 
     237             : /*****************************************************************************/
     238             : /* osl_getSymbol */
     239             : /*****************************************************************************/
     240             : void* SAL_CALL
     241           0 : osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
     242             : {
     243           0 :     return (void *) osl_getFunctionSymbol(Module, pSymbolName);
     244             : }
     245             : 
     246             : /*****************************************************************************/
     247             : /* osl_getAsciiFunctionSymbol */
     248             : /*****************************************************************************/
     249             : oslGenericFunction SAL_CALL
     250       42522 : osl_getAsciiFunctionSymbol(oslModule Module, const sal_Char *pSymbol)
     251             : {
     252       42522 :     void *fcnAddr = NULL;
     253             : 
     254             :     // We do want to use dlsym() also in the DISABLE_DYNLOADING case
     255             :     // just to look up symbols in the static executable, I think.
     256       42522 :     if (pSymbol)
     257             :     {
     258       42522 :         fcnAddr = dlsym(Module, pSymbol);
     259             :         SAL_INFO_IF(
     260             :             fcnAddr == 0, "sal.osl",
     261             :             "dlsym(" << Module << ", " << pSymbol << "): " << dlerror());
     262             :     }
     263             : 
     264       42522 :     return (oslGenericFunction) fcnAddr;
     265             : }
     266             : 
     267             : /*****************************************************************************/
     268             : /* osl_getFunctionSymbol */
     269             : /*****************************************************************************/
     270             : oslGenericFunction SAL_CALL
     271       42521 : osl_getFunctionSymbol(oslModule module, rtl_uString *puFunctionSymbolName)
     272             : {
     273       42521 :     oslGenericFunction pSymbol = NULL;
     274             : 
     275       42521 :     if( puFunctionSymbolName )
     276             :     {
     277       42521 :         rtl_String* pSymbolName = NULL;
     278             : 
     279             :         rtl_uString2String( &pSymbolName,
     280       42521 :             rtl_uString_getStr(puFunctionSymbolName),
     281             :             rtl_uString_getLength(puFunctionSymbolName),
     282             :             RTL_TEXTENCODING_UTF8,
     283       85042 :             OUSTRING_TO_OSTRING_CVTFLAGS );
     284             : 
     285       42521 :         if( pSymbolName != NULL )
     286             :         {
     287       42521 :             pSymbol = osl_getAsciiFunctionSymbol(module, rtl_string_getStr(pSymbolName));
     288       42521 :             rtl_string_release(pSymbolName);
     289             :         }
     290             :     }
     291             : 
     292       42521 :     return pSymbol;
     293             : }
     294             : 
     295             : /*****************************************************************************/
     296             : /* osl_getModuleURLFromAddress */
     297             : /*****************************************************************************/
     298       85018 : sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
     299             : {
     300       85018 :     bool result = false;
     301       85018 :     rtl_String * path = NULL;
     302       85018 :     if (getModulePathFromAddress(addr, &path))
     303             :     {
     304       85018 :         rtl_uString * workDir = NULL;
     305       85018 :         osl_getProcessWorkingDir(&workDir);
     306       85018 :         if (workDir)
     307             :         {
     308             :             SAL_INFO(
     309             :                 "sal.osl", "osl_getModuleURLFromAddress: " << path->buffer);
     310             :             rtl_string2UString(ppLibraryUrl,
     311             :                                path->buffer,
     312             :                                path->length,
     313       85018 :                                osl_getThreadTextEncoding(),
     314       85018 :                                OSTRING_TO_OUSTRING_CVTFLAGS);
     315             : 
     316             :             SAL_WARN_IF(
     317             :                 *ppLibraryUrl == 0, "sal.osl", "rtl_string2UString failed");
     318       85018 :             osl_getFileURLFromSystemPath(*ppLibraryUrl, ppLibraryUrl);
     319       85018 :             osl_getAbsoluteFileURL(workDir, *ppLibraryUrl, ppLibraryUrl);
     320             : 
     321       85018 :             rtl_uString_release(workDir);
     322       85018 :             result = true;
     323             :         }
     324             :         else
     325             :         {
     326           0 :             result = false;
     327             :         }
     328       85018 :         rtl_string_release(path);
     329             :     }
     330       85018 :     return result;
     331             : }
     332             : 
     333             : /*****************************************************************************/
     334             : /* osl_getModuleURLFromFunctionAddress */
     335             : /*****************************************************************************/
     336       85015 : sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress(oslGenericFunction addr, rtl_uString ** ppLibraryUrl)
     337             : {
     338       85015 :     return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl);
     339             : }
     340             : 
     341             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10