Mach-o-matic: If the dylib refs expand in size, it usually is not a problem if a small expansion because Mach-O executables are page aligned. So if the expansion is less than 256 bytes do not count it as an error. Note in the source code the need to properly check if the expansion does intrude into segment data.

This commit is contained in:
Jonathan Campbell 2023-08-29 03:25:41 -07:00
parent 2c8b2739fa
commit 07dc679fb6

View File

@ -50,15 +50,15 @@ string dylib_replace(string path) {
if (str_startswith(s,"/usr/local/Cellar/"))
return string("@executable_path/x86_64/") + fn;
if (str_startswith(s,"@executable_path/")) { /* often in Brew followed by ../../.. etc */
if (str_startswith(s,"@loader_path/")) { /* often in Brew followed by ../../.. etc */
s = fn;
while (!strncmp(s,"../",3)) s += 3;
printf("'%s' = '%s'\n",path.c_str(),s);
return string("@executable_path/") + s;
return string("@executable_path/arm64/") + s;
}
if (str_startswith(s,"@rpath/"))
return string("@executable_path/") + fn;
return string("@executable_path/arm64/") + fn;
return path;
}
@ -289,8 +289,25 @@ int main(int argc,char **argv) {
off_t tgt = (off_t)(src_scan - src_mmap);
if (pos > tgt) {
fprintf(stderr,"Modification expanded load command list\n");
return 1;
/* If it doesn't expand much there's a good chance it just goes a bit more into the empty space
before the first sector or section. As segments are page aligned there should be enough to do
this so long as we're not expanding it too far! Note that it is necessary to scan the sections
because some declare segments that start at file offset zero but the sections do not.
TODO: It really would be a good idea to scan the segment load commands and the sections within
and error out if the newly written dylib commands really do extend into valid segment data.
At some point, this code should support arbitrary expansion by adjusting segment file offsets
farther out if necessary. */
uint64_t add = uint64_t(pos - tgt);
fprintf(stderr,"Modification expanded load command list by %llu bytes.\n",(unsigned long long)add);
if (add > 256)
return 1;
tgt = pos;
src_scan = src_mmap + size_t(pos);
assert(src_scan <= src_mmap_fence);
}
const uint8_t zc = 0;