2 * Copyright (c) 2016 Apple Inc. All rights reserved.
15 #include <sys/types.h>
16 #include <sys/sysctl.h>
33 #include <mach/mach.h>
35 static struct subregion
*
37 const mach_vm_offset_t vmaddr
,
38 const mach_vm_offset_t vmsize
,
39 const native_segment_command_t
*sc
,
40 const struct libent
*le
)
42 struct subregion
*s
= malloc(sizeof (*s
));
44 assert(vmaddr
!= 0 && vmsize
!= 0);
45 assert(vmaddr
< vmaddr
+ vmsize
);
52 s
->s_isuuidref
= false;
57 del_subregion(struct subregion
*s
)
59 poison(s
, 0xfacefac1, sizeof (*s
));
64 clean_subregions(struct region
*r
)
66 if (r
->r_nsubregions
) {
67 assert(r
->r_subregions
);
68 for (unsigned i
= 0; i
< r
->r_nsubregions
; i
++)
69 del_subregion(r
->r_subregions
[i
]);
70 poison(r
->r_subregions
, 0xfac1fac1, sizeof (r
->r_subregions
[0]) * r
->r_nsubregions
);
71 free(r
->r_subregions
);
73 r
->r_subregions
= NULL
;
75 assert(NULL
== r
->r_subregions
);
81 del_sparse_region(struct region
*r
)
84 poison(r
, 0xcafecaff, sizeof (*r
));
88 #define NULLsc ((native_segment_command_t *)0)
91 issamesubregiontype(const struct subregion
*s0
, const struct subregion
*s1
) {
92 return 0 == strncmp(S_MACHO_TYPE(s0
), S_MACHO_TYPE(s1
), sizeof (NULLsc
->segname
));
96 issubregiontype(const struct subregion
*s
, const char *sctype
) {
97 return 0 == strncmp(S_MACHO_TYPE(s
), sctype
, sizeof (NULLsc
->segname
));
101 elide_subregion(struct region
*r
, unsigned ind
)
103 del_subregion(r
->r_subregions
[ind
]);
104 for (unsigned j
= ind
; j
< r
->r_nsubregions
- 1; j
++)
105 r
->r_subregions
[j
] = r
->r_subregions
[j
+1];
106 assert(r
->r_nsubregions
!= 0);
107 r
->r_subregions
[--r
->r_nsubregions
] = NULL
;
110 struct subregionlist
{
111 STAILQ_ENTRY(subregionlist
) srl_linkage
;
112 struct subregion
*srl_s
;
114 typedef STAILQ_HEAD(, subregionlist
) subregionlisthead_t
;
117 add_subregions_for_libent(
118 subregionlisthead_t
*srlh
,
119 const struct region
*r
,
120 const native_mach_header_t
*mh
,
121 const mach_vm_offset_t __unused mh_taddr
, // address in target
122 const struct libent
*le
)
124 const struct load_command
*lc
= (const void *)(mh
+ 1);
125 mach_vm_offset_t objoff
= le
->le_objoff
;
126 for (unsigned n
= 0; n
< mh
->ncmds
; n
++) {
128 const native_segment_command_t
*sc
;
131 case NATIVE_LC_SEGMENT
:
132 sc
= (const void *)lc
;
134 if (0 == sc
->vmaddr
&& strcmp(sc
->segname
, SEG_PAGEZERO
) == 0)
136 mach_vm_offset_t lo
= sc
->vmaddr
+ objoff
;
137 mach_vm_offset_t hi
= lo
+ sc
->vmsize
;
139 /* Eliminate non-overlapping sections first */
141 if (R_ENDADDR(r
) - 1 < lo
)
143 if (hi
- 1 < R_ADDR(r
))
147 * Some part of this segment is in the region.
148 * Trim the edges in the case where we span regions.
152 if (hi
> R_ENDADDR(r
))
155 struct subregionlist
*srl
= calloc(1, sizeof (*srl
));
156 struct subregion
*s
= new_subregion(lo
, hi
- lo
, sc
, le
);
157 assert(sc
->fileoff
>= 0);
159 STAILQ_INSERT_HEAD(srlh
, srl
, srl_linkage
);
161 if (OPTIONS_DEBUG(opt
, 2)) {
163 printr(r
, "subregion %llx-%llx %7s %12s\t%s [%s off %zd for %zd nsects %u flags %x]\n",
164 S_ADDR(s
), S_ENDADDR(s
),
165 str_hsize(hstr
, S_SIZE(s
)),
168 str_prot(sc
->initprot
),
169 sc
->fileoff
, sc
->filesize
,
170 sc
->nsects
, sc
->flags
);
177 lc
= (const void *)((caddr_t
)lc
+ lc
->cmdsize
);
181 return WALK_CONTINUE
;
185 * Because we aggregate information from multiple sources, there may
186 * be duplicate subregions. Eliminate them here.
188 * Note that the each library in the shared cache points
189 * separately at a single, unified (large!) __LINKEDIT section; these
190 * get removed here too.
192 * Assumes the subregion array is sorted by address!
195 eliminate_duplicate_subregions(struct region
*r
)
198 while (i
< r
->r_nsubregions
) {
199 struct subregion
*s0
= r
->r_subregions
[i
-1];
200 struct subregion
*s1
= r
->r_subregions
[i
];
202 if (S_ADDR(s0
) != S_ADDR(s1
) || S_SIZE(s0
) != S_SIZE(s1
)) {
206 if (memcmp(&s0
->s_segcmd
, &s1
->s_segcmd
, sizeof (s0
->s_segcmd
)) != 0) {
210 if (OPTIONS_DEBUG(opt
, 3))
211 printr(r
, "eliding duplicate %s subregion (%llx-%llx) file %s\n",
212 S_MACHO_TYPE(s1
), S_ADDR(s1
), S_ENDADDR(s1
), S_FILENAME(s1
));
213 /* If the duplicate subregions aren't mapping the same file (?), forget the name */
214 if (s0
->s_libent
!= s1
->s_libent
)
215 s0
->s_libent
= s1
->s_libent
= NULL
;
216 elide_subregion(r
, i
);
221 * See if any of the dyld information we have can better describe this
222 * region of the target address space.
225 decorate_memory_region(struct region
*r
, void *arg
)
227 if (r
->r_inzfodregion
|| r
->r_incommregion
)
228 return WALK_CONTINUE
;
230 const dyld_process_info dpi
= arg
;
232 __block walk_return_t retval
= WALK_CONTINUE
;
233 __block subregionlisthead_t srlhead
= STAILQ_HEAD_INITIALIZER(srlhead
);
235 _dyld_process_info_for_each_image(dpi
, ^(uint64_t __unused mhaddr
, const uuid_t uuid
, __unused
const char *path
) {
236 if (WALK_CONTINUE
== retval
) {
237 const struct libent
*le
= libent_lookup_byuuid(uuid
);
238 assert(le
->le_mhaddr
== mhaddr
);
239 bool shouldskip
= false;
240 if (V_SIZE(&le
->le_vr
))
241 shouldskip
= (R_ENDADDR(r
) < V_ADDR(&le
->le_vr
) ||
242 R_ADDR(r
) > V_ENDADDR(&le
->le_vr
));
244 retval
= add_subregions_for_libent(&srlhead
, r
, le
->le_mh
, le
->le_mhaddr
, le
);
247 if (WALK_CONTINUE
!= retval
)
251 * Take the unsorted list of subregions, if any,
252 * and hang a sorted array of ranges on the region structure.
254 if (!STAILQ_EMPTY(&srlhead
)) {
255 struct subregionlist
*srl
;
256 STAILQ_FOREACH(srl
, &srlhead
, srl_linkage
) {
259 assert(r
->r_nsubregions
);
261 r
->r_subregions
= calloc(r
->r_nsubregions
, sizeof (void *));
263 STAILQ_FOREACH(srl
, &srlhead
, srl_linkage
) {
264 r
->r_subregions
[i
++] = srl
->srl_s
;
266 qsort_b(r
->r_subregions
, r
->r_nsubregions
, sizeof (void *),
267 ^(const void *a
, const void *b
) {
268 const struct subregion
*lhs
= *(struct subregion
**)a
;
269 const struct subregion
*rhs
= *(struct subregion
**)b
;
270 if (S_ADDR(lhs
) > S_ADDR(rhs
))
272 if (S_ADDR(lhs
) < S_ADDR(rhs
))
277 eliminate_duplicate_subregions(r
);
279 if (r
->r_info
.external_pager
) {
281 * Only very specific segment types get to be filerefs
283 for (i
= 0; i
< r
->r_nsubregions
; i
++) {
284 struct subregion
*s
= r
->r_subregions
[i
];
286 * Anything marked writable is trivially disqualified; we're
287 * going to copy it anyway.
289 if (s
->s_segcmd
.initprot
& VM_PROT_WRITE
)
292 /* __TEXT and __LINKEDIT are our real targets */
293 if (!issubregiontype(s
, SEG_TEXT
) && !issubregiontype(s
, SEG_LINKEDIT
) && !issubregiontype(s
, "__UNICODE")) {
294 if (OPTIONS_DEBUG(opt
, 3)) {
296 printvr(S_RANGE(s
), "skipping read-only %s segment %s\n", S_MACHO_TYPE(s
), str_hsize(hstr
, S_SIZE(s
)));
300 if (r
->r_insharedregion
) {
302 * Part of the shared region: things get more complicated.
306 * There's a file reference here for the whole region.
307 * For __TEXT subregions, we could, in principle (though
308 * see below) generate references to the individual
309 * dylibs that dyld reports in the region. If the
310 * debugger could then use the __LINKEDIT info in the
311 * file, then we'd be done. But as long as the dump
312 * includes __LINKEDIT sections, we're going to
313 * end up generating a file reference to the combined
314 * __LINKEDIT section in the shared cache anyway, so
315 * we might as well do that for the __TEXT regions as
318 s
->s_libent
= r
->r_fileref
->fr_libent
;
319 s
->s_isuuidref
= true;
322 * If we get here, it's likely that the shared cache
323 * name can't be found e.g. update_dyld_shared_cache(1).
324 * For __TEXT subregions, we could generate refs to
325 * the individual dylibs, but note that the mach header
326 * and segment commands in memory are still pointing
327 * into the shared cache so any act of reconstruction
328 * is fiendishly complex. So copy it.
330 assert(!s
->s_isuuidref
);
333 /* Just a regular dylib? */
335 s
->s_isuuidref
= true;
340 assert(WALK_CONTINUE
== retval
);
343 if (!STAILQ_EMPTY(&srlhead
)) {
344 struct subregionlist
*srl
, *trl
;
345 STAILQ_FOREACH_SAFE(srl
, &srlhead
, srl_linkage
, trl
) {
353 * Strip region of all decoration
355 * Invoked (on every region!) after an error during the initial
356 * 'decoration' phase to discard potentially incomplete information.
359 undecorate_memory_region(struct region
*r
, __unused
void *arg
)
361 assert(&sparse_ops
!= r
->r_op
);
362 return r
->r_nsubregions
? clean_subregions(r
) : WALK_CONTINUE
;
366 * This optimization occurs -after- the vanilla_region_optimizations(),
367 * and -after- we've tagged zfod and first-pass fileref's.
370 sparse_region_optimization(struct region
*r
, __unused
void *arg
)
372 assert(&sparse_ops
!= r
->r_op
);
374 if (r
->r_inzfodregion
) {
376 * Pure zfod region: almost certainly a more compact
377 * representation - keep it that way.
379 if (OPTIONS_DEBUG(opt
, 3))
380 printr(r
, "retaining zfod region\n");
381 assert(&zfod_ops
== r
->r_op
);
382 return clean_subregions(r
);
385 if (r
->r_insharedregion
&& 0 == r
->r_nsubregions
) {
387 * A segment in the shared region needs to be
388 * identified with an LC_SEGMENT that dyld claims,
389 * otherwise (we assert) it's not useful to the dump.
391 if (OPTIONS_DEBUG(opt
, 2)) {
393 printr(r
, "not referenced in dyld info => "
394 "eliding %s range in shared region\n",
395 str_hsize(hstr
, R_SIZE(r
)));
397 if (0 == r
->r_info
.pages_dirtied
&& 0 == r
->r_info
.pages_swapped_out
)
398 return WALK_DELETE_REGION
;
399 if (OPTIONS_DEBUG(opt
, 2)) {
401 printr(r
, "dirty pages, but not referenced in dyld info => "
402 "NOT eliding %s range in shared region\n",
403 str_hsize(hstr
, R_SIZE(r
)));
409 * Already have a fileref for the whole region: already
410 * a more compact representation - keep it that way.
412 if (OPTIONS_DEBUG(opt
, 3))
413 printr(r
, "retaining fileref region\n");
414 assert(&fileref_ops
== r
->r_op
);
415 return clean_subregions(r
);
418 if (r
->r_nsubregions
> 1) {
420 * Merge adjacent or identical subregions that have no file reference
421 * (Reducing the number of subregions reduces header overhead and
422 * improves compressability)
425 while (i
< r
->r_nsubregions
) {
426 struct subregion
*s0
= r
->r_subregions
[i
-1];
427 struct subregion
*s1
= r
->r_subregions
[i
];
429 if (s0
->s_isuuidref
) {
431 continue; /* => destined to be a fileref */
433 if (!issamesubregiontype(s0
, s1
)) {
435 continue; /* merge-able subregions must have same "type" */
438 if (S_ENDADDR(s0
) == S_ADDR(s1
)) {
439 /* directly adjacent subregions */
440 if (OPTIONS_DEBUG(opt
, 2))
441 printr(r
, "merging subregions (%llx-%llx + %llx-%llx) -- adjacent\n",
442 S_ADDR(s0
), S_ENDADDR(s0
), S_ADDR(s1
), S_ENDADDR(s1
));
443 S_SETSIZE(s0
, S_ENDADDR(s1
) - S_ADDR(s0
));
444 elide_subregion(r
, i
);
448 const mach_vm_size_t pfn
[2] = {
449 S_ADDR(s0
) >> pageshift_host
,
450 S_ADDR(s1
) >> pageshift_host
452 const mach_vm_size_t endpfn
[2] = {
453 (S_ENDADDR(s0
) - 1) >> pageshift_host
,
454 (S_ENDADDR(s1
) - 1) >> pageshift_host
457 if (pfn
[0] == pfn
[1] && pfn
[0] == endpfn
[0] && pfn
[0] == endpfn
[1]) {
458 /* two small subregions share a host page */
459 if (OPTIONS_DEBUG(opt
, 2))
460 printr(r
, "merging subregions (%llx-%llx + %llx-%llx) -- same page\n",
461 S_ADDR(s0
), S_ENDADDR(s0
), S_ADDR(s1
), S_ENDADDR(s1
));
462 S_SETSIZE(s0
, S_ENDADDR(s1
) - S_ADDR(s0
));
463 elide_subregion(r
, i
);
467 if (pfn
[1] == 1 + endpfn
[0]) {
468 /* subregions are pagewise-adjacent: bigger chunks to compress */
469 if (OPTIONS_DEBUG(opt
, 2))
470 printr(r
, "merging subregions (%llx-%llx + %llx-%llx) -- adjacent pages\n",
471 S_ADDR(s0
), S_ENDADDR(s0
), S_ADDR(s1
), S_ENDADDR(s1
));
472 S_SETSIZE(s0
, S_ENDADDR(s1
) - S_ADDR(s0
));
473 elide_subregion(r
, i
);
477 i
++; /* this isn't the subregion we're looking for */
481 if (1 == r
->r_nsubregions
) {
482 struct subregion
*s
= r
->r_subregions
[0];
483 if (!s
->s_isuuidref
&&
484 R_ADDR(r
) == S_ADDR(s
) && R_ENDADDR(r
) == S_ENDADDR(s
)) {
485 if (OPTIONS_DEBUG(opt
, 3))
486 printr(r
, "subregion (%llx-%llx) reverts to region\n",
487 S_ADDR(s
), S_ENDADDR(s
));
488 return clean_subregions(r
);
492 if (r
->r_nsubregions
)
493 r
->r_op
= &sparse_ops
;
495 return WALK_CONTINUE
;