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 | |
| 39 | std::string dirname(const char* path) { |
| 40 | const char* last_slash = strrchr(path, '/'); |
| 41 | |
| 42 | if (last_slash == path) { |
| 43 | return "/"; |
| 44 | } else if (last_slash == nullptr) { |
| 45 | return "."; |
| 46 | } else { |
| 47 | return std::string(path, last_slash - path); |
| 48 | } |
| 49 | } |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 50 | |
| 51 | bool normalize_path(const char* path, std::string* normalized_path) { |
| 52 | // Input should be an absolute path |
| 53 | if (path[0] != '/') { |
Dimitry Ivanov | 769b33f | 2016-07-21 11:33:40 -0700 | [diff] [blame] | 54 | PRINT("normalize_path - invalid input: \"%s\", the input path should be absolute", path); |
Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
| 58 | const size_t len = strlen(path) + 1; |
| 59 | char buf[len]; |
| 60 | |
| 61 | const char* in_ptr = path; |
| 62 | char* out_ptr = buf; |
| 63 | |
| 64 | while (*in_ptr != 0) { |
| 65 | if (*in_ptr == '/') { |
| 66 | char c1 = in_ptr[1]; |
| 67 | if (c1 == '.') { |
| 68 | char c2 = in_ptr[2]; |
| 69 | if (c2 == '/') { |
| 70 | in_ptr += 2; |
| 71 | continue; |
| 72 | } else if (c2 == '.' && (in_ptr[3] == '/' || in_ptr[3] == 0)) { |
| 73 | in_ptr += 3; |
| 74 | while (out_ptr > buf && *--out_ptr != '/') { |
| 75 | } |
| 76 | if (in_ptr[0] == 0) { |
| 77 | // retain '/' |
| 78 | out_ptr++; |
| 79 | } |
| 80 | continue; |
| 81 | } |
| 82 | } else if (c1 == '/') { |
| 83 | ++in_ptr; |
| 84 | continue; |
| 85 | } |
| 86 | } |
| 87 | *out_ptr++ = *in_ptr++; |
| 88 | } |
| 89 | |
| 90 | *out_ptr = 0; |
| 91 | *normalized_path = buf; |
| 92 | return true; |
| 93 | } |
| 94 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 95 | bool file_is_in_dir(const std::string& file, const std::string& dir) { |
| 96 | const char* needle = dir.c_str(); |
| 97 | const char* haystack = file.c_str(); |
| 98 | size_t needle_len = strlen(needle); |
| 99 | |
Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 100 | return strncmp(haystack, needle, needle_len) == 0 && |
| 101 | haystack[needle_len] == '/' && |
| 102 | strchr(haystack + needle_len + 1, '/') == nullptr; |
| 103 | } |
| 104 | |
| 105 | bool file_is_under_dir(const std::string& file, const std::string& dir) { |
| 106 | const char* needle = dir.c_str(); |
| 107 | const char* haystack = file.c_str(); |
| 108 | size_t needle_len = strlen(needle); |
| 109 | |
| 110 | return strncmp(haystack, needle, needle_len) == 0 && |
| 111 | haystack[needle_len] == '/'; |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | const char* const kZipFileSeparator = "!/"; |
| 115 | |
| 116 | bool parse_zip_path(const char* input_path, std::string* zip_path, std::string* entry_path) { |
| 117 | std::string normalized_path; |
| 118 | if (!normalize_path(input_path, &normalized_path)) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | const char* const path = normalized_path.c_str(); |
Dimitry Ivanov | 769b33f | 2016-07-21 11:33:40 -0700 | [diff] [blame] | 123 | TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 124 | |
| 125 | // Treat an '!/' separator inside a path as the separator between the name |
| 126 | // of the zip file on disk and the subdirectory to search within it. |
| 127 | // For example, if path is "foo.zip!/bar/bas/x.so", then we search for |
| 128 | // "bar/bas/x.so" within "foo.zip". |
| 129 | const char* const separator = strstr(path, kZipFileSeparator); |
| 130 | if (separator == nullptr) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | char buf[512]; |
| 135 | if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) { |
| 136 | PRINT("Warning: ignoring very long library path: %s", path); |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | buf[separator - path] = '\0'; |
| 141 | |
| 142 | *zip_path = buf; |
| 143 | *entry_path = &buf[separator - path + 2]; |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
Dmitriy Ivanov | 84bab5a | 2015-11-20 13:34:11 -0800 | [diff] [blame] | 148 | constexpr off64_t kPageMask = ~static_cast<off64_t>(PAGE_SIZE-1); |
| 149 | |
| 150 | off64_t page_start(off64_t offset) { |
| 151 | return offset & kPageMask; |
| 152 | } |
| 153 | |
| 154 | bool safe_add(off64_t* out, off64_t a, size_t b) { |
| 155 | CHECK(a >= 0); |
| 156 | if (static_cast<uint64_t>(INT64_MAX - a) < b) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | *out = a + b; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | size_t page_offset(off64_t offset) { |
| 165 | return static_cast<size_t>(offset & (PAGE_SIZE-1)); |
| 166 | } |
| 167 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 168 | void split_path(const char* path, const char* delimiters, |
| 169 | std::vector<std::string>* paths) { |
| 170 | if (path != nullptr && path[0] != 0) { |
| 171 | *paths = android::base::Split(path, delimiters); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void resolve_paths(std::vector<std::string>& paths, |
| 176 | std::vector<std::string>* resolved_paths) { |
| 177 | resolved_paths->clear(); |
| 178 | for (const auto& path : paths) { |
Dimitry Ivanov | 01fdb6a | 2016-09-07 14:48:27 -0700 | [diff] [blame] | 179 | // skip empty paths |
| 180 | if (path.empty()) { |
| 181 | continue; |
| 182 | } |
| 183 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 184 | char resolved_path[PATH_MAX]; |
| 185 | const char* original_path = path.c_str(); |
| 186 | if (realpath(original_path, resolved_path) != nullptr) { |
| 187 | struct stat s; |
| 188 | if (stat(resolved_path, &s) == 0) { |
| 189 | if (S_ISDIR(s.st_mode)) { |
| 190 | resolved_paths->push_back(resolved_path); |
| 191 | } else { |
| 192 | DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path); |
| 193 | continue; |
| 194 | } |
| 195 | } else { |
| 196 | DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno)); |
| 197 | continue; |
| 198 | } |
| 199 | } else { |
| 200 | std::string zip_path; |
| 201 | std::string entry_path; |
| 202 | |
| 203 | std::string normalized_path; |
| 204 | |
| 205 | if (!normalize_path(original_path, &normalized_path)) { |
| 206 | DL_WARN("Warning: unable to normalize \"%s\"", original_path); |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) { |
| 211 | if (realpath(zip_path.c_str(), resolved_path) == nullptr) { |
| 212 | DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno)); |
| 213 | continue; |
| 214 | } |
| 215 | |
| 216 | resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |