summaryrefslogtreecommitdiff
path: root/linker/linker_translate_path.cpp
blob: 99a9c9714e43c5711577b9c9f3ef109f55c1c3f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "linker_translate_path.h"

#include <string>

#include <android/api-level.h>

#include "linker.h"

// Handle dlopen by full path.
//
// 1. Translate original path to native_bridge path.
//
// Native bridge libraries reside in $LIB/$ABI subdirectory. For example:
//   /system/lib/liblog.so -> /system/lib/arm/liblog.so
//
// Native bridge libraries do not use apex. For example:
//   /apex/com.android.i18n/lib/libicuuc.so -> /system/lib/arm/libicuuc.so
//
// 2. Repeat linker workaround to open apex libraries by system path (see http://b/121248172).
//
// For older target SDK versions, linker allows to open apex libraries by system path, so it does:
//   /system/lib/libicuuc.so -> /apex/com.android.art/lib/libicuuc.so
//
// Adding native bridge path translation, we get:
//   /system/lib/libicuuc.so -> /apex/com.android.art/lib/libicuuc.so -> /system/lib/arm/libicuuc.so

#if defined(__arm__)
#define SYSTEM_LIB(name) \
  { "/system/lib/" name, "/system/lib/arm/" name }
#define APEX_LIB(apex, name) \
  { "/apex/" apex "/lib/" name, "/system/lib/arm/" name }
#elif defined(__aarch64__)
#define SYSTEM_LIB(name) \
  { "/system/lib64/" name, "/system/lib64/arm64/" name }
#define APEX_LIB(apex, name) \
  { "/apex/" apex "/lib64/" name, "/system/lib64/arm64/" name }
#else
#error "Unknown guest arch"
#endif

/**
 * Translate /system path or /apex path to native_bridge path
 * Function name is misleading, as it overrides the corresponding function in original linker.
 *
 * param out_name pointing to native_bridge path
 * return true if translation is needed
 */
bool translateSystemPathToApexPath(const char* name, std::string* out_name) {
  static constexpr const char* kPathTranslation[][2] = {
    // Libraries accessible by system path.
    SYSTEM_LIB("libEGL.so"),
    SYSTEM_LIB("libGLESv1_CM.so"),
    SYSTEM_LIB("libGLESv2.so"),
    SYSTEM_LIB("libGLESv3.so"),
    SYSTEM_LIB("libOpenMAXAL.so"),
    SYSTEM_LIB("libOpenSLES.so"),
    SYSTEM_LIB("libRS.so"),
    SYSTEM_LIB("libaaudio.so"),
    SYSTEM_LIB("libamidi.so"),
    SYSTEM_LIB("libandroid.so"),
    SYSTEM_LIB("libbinder_ndk.so"),
    SYSTEM_LIB("libc.so"),
    SYSTEM_LIB("libcamera2ndk.so"),
    SYSTEM_LIB("libdl.so"),
    SYSTEM_LIB("libjnigraphics.so"),
    SYSTEM_LIB("liblog.so"),
    SYSTEM_LIB("libm.so"),
    SYSTEM_LIB("libmediandk.so"),
    SYSTEM_LIB("libnativewindow.so"),
    SYSTEM_LIB("libstdc++.so"),
    SYSTEM_LIB("libsync.so"),
    SYSTEM_LIB("libvulkan.so"),
    SYSTEM_LIB("libwebviewchromium_plat_support.so"),
    SYSTEM_LIB("libz.so"),
    // Apex/system after R.
    APEX_LIB("com.android.i18n", "libandroidicu.so"),
    APEX_LIB("com.android.i18n", "libicu.so"),
    APEX_LIB("com.android.i18n", "libicui18n.so"),
    APEX_LIB("com.android.i18n", "libicuuc.so"),
    APEX_LIB("com.android.neuralnetworks", "libneuralnetworks.so"),
    // Apex/system on R (see http://b/161958857).
    APEX_LIB("com.android.art", "libicui18n.so"),
    APEX_LIB("com.android.art", "libicuuc.so"),
    APEX_LIB("com.android.art", "libnativehelper.so"),
    // Apex/system on Q.
    APEX_LIB("com.android.runtime", "libicui18n.so"),
    APEX_LIB("com.android.runtime", "libicuuc.so"),
  };

  static constexpr const char* kPathTranslationQ[][2] = {
    // Apps targeting below Q can open apex libraries by system path.
    SYSTEM_LIB("libicui18n.so"),
    SYSTEM_LIB("libicuuc.so"),
    SYSTEM_LIB("libneuralnetworks.so"),
  };

  static constexpr const char* kPathTranslationN[][2] = {
    // Apps targeting below N can open greylisted libraries.
    SYSTEM_LIB("libandroid_runtime.so"),
    SYSTEM_LIB("libbinder.so"),
    SYSTEM_LIB("libcrypto.so"),
    SYSTEM_LIB("libcutils.so"),
    SYSTEM_LIB("libexpat.so"),
    SYSTEM_LIB("libgui.so"),
    SYSTEM_LIB("libmedia.so"),
    SYSTEM_LIB("libnativehelper.so"),
    SYSTEM_LIB("libssl.so"),
    SYSTEM_LIB("libstagefright.so"),
    SYSTEM_LIB("libsqlite.so"),
    SYSTEM_LIB("libui.so"),
    SYSTEM_LIB("libutils.so"),
    SYSTEM_LIB("libvorbisidec.so"),
  };

  if (name == nullptr) {
    return false;
  }

  auto comparator = [name](auto p) { return strcmp(name, p[0]) == 0; };

  if (auto it = std::find_if(std::begin(kPathTranslation), std::end(kPathTranslation), comparator);
      it != std::end(kPathTranslation)) {
    *out_name = (*it)[1];
    return true;
  }

  if (get_application_target_sdk_version() < __ANDROID_API_Q__) {
    if (auto it =
            std::find_if(std::begin(kPathTranslationQ), std::end(kPathTranslationQ), comparator);
        it != std::end(kPathTranslationQ)) {
      *out_name = (*it)[1];
      return true;
    }
  }

  if (get_application_target_sdk_version() < __ANDROID_API_N__) {
    if (auto it =
            std::find_if(std::begin(kPathTranslationN), std::end(kPathTranslationN), comparator);
        it != std::end(kPathTranslationN)) {
      *out_name = (*it)[1];
      return true;
    }
  }

  return false;
}