2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* TODO: add comment why this is needed */
39 #include <linux/limits.h>
45 #include <sys/types.h>
49 #include "native_client/nonnacl_util/sel_ldr_launcher.h"
54 const char* SelLdrLauncher::GetPluginDirname() {
56 char* pathname
= NULL
;
57 static char bin_dir
[2 * PATH_MAX
+ 1];
58 // C++ really does not like to convert function pointer to regular pointers.
59 // This is apparently the only way to do it without compiler warnings
60 void* sym_addr
= reinterpret_cast<void*>(
61 reinterpret_cast<uintptr_t>(GetPluginDirname
));
63 // First, try looking a symbol up in the dynamic loader's records.
64 if (0 == dladdr(sym_addr
, &info
)) {
65 printf("dladdr failed\n");
67 strncpy(bin_dir
, info
.dli_fname
, sizeof(bin_dir
));
70 // What follows is probably major overkill if the dladdr scheme works.
71 if (NULL
== pathname
) {
72 // Identify the path the plugin was loaded from. For Linux we look through
73 // the address ranges in /proc/self/maps for the address of a static
74 // variable. If that fails, look in /proc/self/exe.
77 // Open the file, returning NULL for failure.
78 if (NULL
== (fp
= fopen("/proc/self/maps", "r"))) {
83 static int dummy_for_address
;
84 const unsigned kAddressInSo
= (unsigned) &dummy_for_address
;
93 if (7 != fscanf(fp
, "%x-%x %s %x %x:%x %x",
94 &low_addr
, &high_addr
, perm
, &offset
, &dev_maj
,
98 if (NULL
== fgets(bin_dir
, sizeof(bin_dir
), fp
)) {
101 // If the address is in the range, break out of the loop.
102 if (kAddressInSo
>= low_addr
&& kAddressInSo
< high_addr
) {
103 pathname
= strchr(bin_dir
, '/');
107 // Close /proc/self/maps.
110 // If we didn't find a matching pathname, try the path of the current
112 if (NULL
== pathname
) {
113 int bin_dir_size
= readlink("/proc/self/exe", bin_dir
, PATH_MAX
);
114 if (bin_dir_size
< 0 || bin_dir_size
> PATH_MAX
) {
117 bin_dir
[bin_dir_size
] = '\0';
120 // Return just the directory path.
121 return dirname(pathname
);