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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
* Copyright (C) 2015 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_utils.h"
#include "linker_debug.h"
#include "linker_globals.h"
#include "android-base/strings.h"
#include <sys/stat.h>
#include <unistd.h>
std::string dirname(const char* path) {
const char* last_slash = strrchr(path, '/');
if (last_slash == path) {
return "/";
} else if (last_slash == nullptr) {
return ".";
} else {
return std::string(path, last_slash - path);
}
}
bool normalize_path(const char* path, std::string* normalized_path) {
// Input should be an absolute path
if (path[0] != '/') {
PRINT("normalize_path - invalid input: \"%s\", the input path should be absolute", path);
return false;
}
const size_t len = strlen(path) + 1;
char buf[len];
const char* in_ptr = path;
char* out_ptr = buf;
while (*in_ptr != 0) {
if (*in_ptr == '/') {
char c1 = in_ptr[1];
if (c1 == '.') {
char c2 = in_ptr[2];
if (c2 == '/') {
in_ptr += 2;
continue;
} else if (c2 == '.' && (in_ptr[3] == '/' || in_ptr[3] == 0)) {
in_ptr += 3;
while (out_ptr > buf && *--out_ptr != '/') {
}
if (in_ptr[0] == 0) {
// retain '/'
out_ptr++;
}
continue;
}
} else if (c1 == '/') {
++in_ptr;
continue;
}
}
*out_ptr++ = *in_ptr++;
}
*out_ptr = 0;
*normalized_path = buf;
return true;
}
bool file_is_in_dir(const std::string& file, const std::string& dir) {
const char* needle = dir.c_str();
const char* haystack = file.c_str();
size_t needle_len = strlen(needle);
return strncmp(haystack, needle, needle_len) == 0 &&
haystack[needle_len] == '/' &&
strchr(haystack + needle_len + 1, '/') == nullptr;
}
bool file_is_under_dir(const std::string& file, const std::string& dir) {
const char* needle = dir.c_str();
const char* haystack = file.c_str();
size_t needle_len = strlen(needle);
return strncmp(haystack, needle, needle_len) == 0 &&
haystack[needle_len] == '/';
}
const char* const kZipFileSeparator = "!/";
bool parse_zip_path(const char* input_path, std::string* zip_path, std::string* entry_path) {
std::string normalized_path;
if (!normalize_path(input_path, &normalized_path)) {
return false;
}
const char* const path = normalized_path.c_str();
TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
// Treat an '!/' separator inside a path as the separator between the name
// of the zip file on disk and the subdirectory to search within it.
// For example, if path is "foo.zip!/bar/bas/x.so", then we search for
// "bar/bas/x.so" within "foo.zip".
const char* const separator = strstr(path, kZipFileSeparator);
if (separator == nullptr) {
return false;
}
char buf[512];
if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
PRINT("Warning: ignoring very long library path: %s", path);
return false;
}
buf[separator - path] = '\0';
*zip_path = buf;
*entry_path = &buf[separator - path + 2];
return true;
}
constexpr off64_t kPageMask = ~static_cast<off64_t>(PAGE_SIZE-1);
off64_t page_start(off64_t offset) {
return offset & kPageMask;
}
bool safe_add(off64_t* out, off64_t a, size_t b) {
CHECK(a >= 0);
if (static_cast<uint64_t>(INT64_MAX - a) < b) {
return false;
}
*out = a + b;
return true;
}
size_t page_offset(off64_t offset) {
return static_cast<size_t>(offset & (PAGE_SIZE-1));
}
void split_path(const char* path, const char* delimiters,
std::vector<std::string>* paths) {
if (path != nullptr && path[0] != 0) {
*paths = android::base::Split(path, delimiters);
}
}
void resolve_paths(std::vector<std::string>& paths,
std::vector<std::string>* resolved_paths) {
resolved_paths->clear();
for (const auto& path : paths) {
char resolved_path[PATH_MAX];
const char* original_path = path.c_str();
if (realpath(original_path, resolved_path) != nullptr) {
struct stat s;
if (stat(resolved_path, &s) == 0) {
if (S_ISDIR(s.st_mode)) {
resolved_paths->push_back(resolved_path);
} else {
DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
continue;
}
} else {
DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
continue;
}
} else {
std::string zip_path;
std::string entry_path;
std::string normalized_path;
if (!normalize_path(original_path, &normalized_path)) {
DL_WARN("Warning: unable to normalize \"%s\"", original_path);
continue;
}
if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
continue;
}
resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
}
}
}
}
|