Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "linker_utils.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 30 | |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 31 | #include "linker_debug.h" |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 32 | #include "linker_globals.h" |
| 33 | |
| 34 | #include "android-base/strings.h" |
| 35 | |
| 36 | #include <sys/stat.h> |
| 37 | #include <unistd.h> |
| 38 | |
Dimitry Ivanov | 2a6d9b2 | 2017-03-11 14:35:38 -0800 | [diff] [blame] | 39 | void format_string(std::string* str, const std::vector<std::pair<std::string, std::string>>& params) { |
| 40 | size_t pos = 0; |
| 41 | while (pos < str->size()) { |
| 42 | pos = str->find("$", pos); |
| 43 | if (pos == std::string::npos) break; |
| 44 | for (const auto& param : params) { |
| 45 | const std::string& token = param.first; |
| 46 | const std::string& replacement = param.second; |
| 47 | if (str->substr(pos + 1, token.size()) == token) { |
| 48 | str->replace(pos, token.size() + 1, replacement); |
| 49 | // -1 to compensate for the ++pos below. |
| 50 | pos += replacement.size() - 1; |
| 51 | break; |
| 52 | } else if (str->substr(pos + 1, token.size() + 2) == "{" + token + "}") { |
| 53 | str->replace(pos, token.size() + 3, replacement); |
| 54 | pos += replacement.size() - 1; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | // Skip $ in case it did not match any of the known substitutions. |
| 59 | ++pos; |
| 60 | } |
| 61 | } |
| 62 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 63 | std::string dirname(const char* path) { |
| 64 | const char* last_slash = strrchr(path, '/'); |
| 65 | |
| 66 | if (last_slash == path) { |
| 67 | return "/"; |
| 68 | } else if (last_slash == nullptr) { |
| 69 | return "."; |
| 70 | } else { |
| 71 | return std::string(path, last_slash - path); |
| 72 | } |
| 73 | } |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 74 | |
| 75 | bool normalize_path(const char* path, std::string* normalized_path) { |
| 76 | // Input should be an absolute path |
| 77 | if (path[0] != '/') { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame^] | 78 | DL_WARN("normalize_path - invalid input: \"%s\", the input path should be absolute", path); |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
| 82 | const size_t len = strlen(path) + 1; |
| 83 | char buf[len]; |
| 84 | |
| 85 | const char* in_ptr = path; |
| 86 | char* out_ptr = buf; |
| 87 | |
| 88 | while (*in_ptr != 0) { |
| 89 | if (*in_ptr == '/') { |
| 90 | char c1 = in_ptr[1]; |
| 91 | if (c1 == '.') { |
| 92 | char c2 = in_ptr[2]; |
| 93 | if (c2 == '/') { |
| 94 | in_ptr += 2; |
| 95 | continue; |
| 96 | } else if (c2 == '.' && (in_ptr[3] == '/' || in_ptr[3] == 0)) { |
| 97 | in_ptr += 3; |
| 98 | while (out_ptr > buf && *--out_ptr != '/') { |
| 99 | } |
| 100 | if (in_ptr[0] == 0) { |
Ryan Prichard | 269bb49 | 2018-10-04 14:45:55 -0700 | [diff] [blame] | 101 | // retain '/' (or write the initial '/' for "/..") |
| 102 | *out_ptr++ = '/'; |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 103 | } |
| 104 | continue; |
| 105 | } |
| 106 | } else if (c1 == '/') { |
| 107 | ++in_ptr; |
| 108 | continue; |
| 109 | } |
| 110 | } |
| 111 | *out_ptr++ = *in_ptr++; |
| 112 | } |
| 113 | |
| 114 | *out_ptr = 0; |
| 115 | *normalized_path = buf; |
| 116 | return true; |
| 117 | } |
| 118 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 119 | bool file_is_in_dir(const std::string& file, const std::string& dir) { |
| 120 | const char* needle = dir.c_str(); |
| 121 | const char* haystack = file.c_str(); |
| 122 | size_t needle_len = strlen(needle); |
| 123 | |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 124 | return strncmp(haystack, needle, needle_len) == 0 && |
| 125 | haystack[needle_len] == '/' && |
| 126 | strchr(haystack + needle_len + 1, '/') == nullptr; |
| 127 | } |
| 128 | |
| 129 | bool file_is_under_dir(const std::string& file, const std::string& dir) { |
| 130 | const char* needle = dir.c_str(); |
| 131 | const char* haystack = file.c_str(); |
| 132 | size_t needle_len = strlen(needle); |
| 133 | |
| 134 | return strncmp(haystack, needle, needle_len) == 0 && |
| 135 | haystack[needle_len] == '/'; |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | const char* const kZipFileSeparator = "!/"; |
| 139 | |
| 140 | bool parse_zip_path(const char* input_path, std::string* zip_path, std::string* entry_path) { |
| 141 | std::string normalized_path; |
| 142 | if (!normalize_path(input_path, &normalized_path)) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | const char* const path = normalized_path.c_str(); |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame^] | 147 | LD_DEBUG(any, "Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 148 | |
| 149 | // Treat an '!/' separator inside a path as the separator between the name |
| 150 | // of the zip file on disk and the subdirectory to search within it. |
| 151 | // For example, if path is "foo.zip!/bar/bas/x.so", then we search for |
| 152 | // "bar/bas/x.so" within "foo.zip". |
| 153 | const char* const separator = strstr(path, kZipFileSeparator); |
| 154 | if (separator == nullptr) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | char buf[512]; |
| 159 | if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame^] | 160 | DL_WARN("ignoring very long library path: %s", path); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 161 | return false; |
| 162 | } |
| 163 | |
| 164 | buf[separator - path] = '\0'; |
| 165 | |
| 166 | *zip_path = buf; |
| 167 | *entry_path = &buf[separator - path + 2]; |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
Dmitriy Ivanov | 84bab5a | 2015-11-20 13:34:11 -0800 | [diff] [blame] | 172 | bool safe_add(off64_t* out, off64_t a, size_t b) { |
| 173 | CHECK(a >= 0); |
| 174 | if (static_cast<uint64_t>(INT64_MAX - a) < b) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | *out = a + b; |
| 179 | return true; |
| 180 | } |
| 181 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 182 | void split_path(const char* path, const char* delimiters, |
| 183 | std::vector<std::string>* paths) { |
| 184 | if (path != nullptr && path[0] != 0) { |
| 185 | *paths = android::base::Split(path, delimiters); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void resolve_paths(std::vector<std::string>& paths, |
| 190 | std::vector<std::string>* resolved_paths) { |
| 191 | resolved_paths->clear(); |
| 192 | for (const auto& path : paths) { |
Dimitry Ivanov | 01fdb6a | 2016-09-07 14:48:27 -0700 | [diff] [blame] | 193 | // skip empty paths |
| 194 | if (path.empty()) { |
| 195 | continue; |
| 196 | } |
Jiyong Park | 341b61e | 2019-05-13 16:52:01 +0900 | [diff] [blame] | 197 | std::string resolved = resolve_path(path); |
| 198 | if (!resolved.empty()) { |
| 199 | resolved_paths->push_back(std::move(resolved)); |
| 200 | } |
| 201 | } |
| 202 | } |
Dimitry Ivanov | 01fdb6a | 2016-09-07 14:48:27 -0700 | [diff] [blame] | 203 | |
Jiyong Park | 341b61e | 2019-05-13 16:52:01 +0900 | [diff] [blame] | 204 | std::string resolve_path(const std::string& path) { |
| 205 | char resolved_path[PATH_MAX]; |
| 206 | const char* original_path = path.c_str(); |
| 207 | if (realpath(original_path, resolved_path) != nullptr) { |
| 208 | struct stat s; |
| 209 | if (stat(resolved_path, &s) == -1) { |
Elliott Hughes | f5e21d9 | 2024-07-26 11:48:19 +0000 | [diff] [blame] | 210 | DL_WARN("Warning: cannot stat file \"%s\": %m (ignoring)", resolved_path); |
Jiyong Park | 341b61e | 2019-05-13 16:52:01 +0900 | [diff] [blame] | 211 | return ""; |
| 212 | } |
| 213 | if (!S_ISDIR(s.st_mode)) { |
| 214 | DL_WARN("Warning: \"%s\" is not a directory (ignoring)", resolved_path); |
| 215 | return ""; |
| 216 | } |
| 217 | return resolved_path; |
| 218 | } else { |
| 219 | std::string normalized_path; |
| 220 | if (!normalize_path(original_path, &normalized_path)) { |
| 221 | DL_WARN("Warning: unable to normalize \"%s\" (ignoring)", original_path); |
| 222 | return ""; |
| 223 | } |
| 224 | |
| 225 | std::string zip_path; |
| 226 | std::string entry_path; |
| 227 | if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) { |
| 228 | if (realpath(zip_path.c_str(), resolved_path) == nullptr) { |
Elliott Hughes | f5e21d9 | 2024-07-26 11:48:19 +0000 | [diff] [blame] | 229 | DL_WARN("Warning: unable to resolve \"%s\": %m (ignoring)", zip_path.c_str()); |
Jiyong Park | 341b61e | 2019-05-13 16:52:01 +0900 | [diff] [blame] | 230 | return ""; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 231 | } |
Jiyong Park | 341b61e | 2019-05-13 16:52:01 +0900 | [diff] [blame] | 232 | |
| 233 | return std::string(resolved_path) + kZipFileSeparator + entry_path; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 234 | } else { |
Jiyong Park | 341b61e | 2019-05-13 16:52:01 +0900 | [diff] [blame] | 235 | struct stat s; |
| 236 | if (stat(normalized_path.c_str(), &s) == 0 && S_ISDIR(s.st_mode)) { |
| 237 | // Path is not a zip path, but an existing directory. Then add it |
| 238 | // although we failed to resolve it. b/119656753 |
| 239 | return normalized_path; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |
Jiyong Park | 341b61e | 2019-05-13 16:52:01 +0900 | [diff] [blame] | 243 | return ""; |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 244 | } |
Jiyong Park | 31cd08f | 2018-06-01 19:18:56 +0900 | [diff] [blame] | 245 | |
Tom Cherry | 66bc428 | 2018-11-08 13:40:52 -0800 | [diff] [blame] | 246 | bool is_first_stage_init() { |
| 247 | static bool ret = (getpid() == 1 && access("/proc/self/exe", F_OK) == -1); |
Jiyong Park | 31cd08f | 2018-06-01 19:18:56 +0900 | [diff] [blame] | 248 | return ret; |
| 249 | } |