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_isfileref
= false;
57 del_subregion(struct subregion
*s
)
59 poison(s
, 0xfacefac1, sizeof (*s
));
64 clean_subregions(struct region
*r
)
66 for (unsigned i
= 0; i
< r
->r_nsubregions
; i
++)
67 del_subregion(r
->r_subregions
[i
]);
68 poison(r
->r_subregions
, 0xfac1fac1, sizeof (r
->r_subregions
[0]) * r
->r_nsubregions
);
69 free(r
->r_subregions
);
75 del_sparse_region(struct region
*r
)
78 poison(r
, 0xcafecaff, sizeof (*r
));
82 #define NULLsc ((native_segment_command_t *)0)
85 issamesubregiontype(const struct subregion
*s0
, const struct subregion
*s1
) {
86 return 0 == strncmp(S_MACHO_TYPE(s0
), S_MACHO_TYPE(s1
), sizeof (NULLsc
->segname
));
90 issubregiontype(const struct subregion
*s
, const char *sctype
) {
91 return 0 == strncmp(S_MACHO_TYPE(s
), sctype
, sizeof (NULLsc
->segname
));
95 elide_subregion(struct region
*r
, unsigned ind
)
97 del_subregion(r
->r_subregions
[ind
]);
98 for (unsigned j
= ind
; j
< r
->r_nsubregions
- 1; j
++)
99 r
->r_subregions
[j
] = r
->r_subregions
[j
+1];
100 assert(r
->r_nsubregions
!= 0);
101 r
->r_subregions
[--r
->r_nsubregions
] = NULL
;
104 struct subregionlist
{
105 STAILQ_ENTRY(subregionlist
) srl_linkage
;
106 struct subregion
*srl_s
;
108 typedef STAILQ_HEAD(, subregionlist
) subregionlisthead_t
;
111 add_subregions_for_libent(
112 subregionlisthead_t
*srlh
,
113 const struct region
*r
,
114 const native_mach_header_t
*mh
,
115 const mach_vm_offset_t mh_taddr
,
116 const struct libent
*le
)
118 const struct load_command
*lc
= (const void *)(mh
+ 1);
119 mach_vm_offset_t scoffset
= MACH_VM_MAX_ADDRESS
;
121 for (unsigned n
= 0; n
< mh
->ncmds
; n
++) {
123 const native_segment_command_t
*sc
;
126 case NATIVE_LC_SEGMENT
:
127 sc
= (const void *)lc
;
130 strlcpy(scsegname
, sc
->segname
, sizeof (scsegname
));
132 if (0 == sc
->vmaddr
&&
133 strcmp(scsegname
, SEG_PAGEZERO
) == 0)
136 /* -Depends- on finding a __TEXT segment first! */
138 if (MACH_VM_MAX_ADDRESS
== scoffset
) {
139 if (strcmp(scsegname
, SEG_TEXT
) == 0)
140 scoffset
= mh_taddr
- sc
->vmaddr
;
143 * Treat as error - don't want a partial description
144 * to cause something to be omitted from the dump.
146 printr(r
, "expected %s segment, found %s segment\n", SEG_TEXT
, scsegname
);
151 /* Eliminate non-overlapping sections first */
153 if (R_ENDADDR(r
) - 1 < sc
->vmaddr
+ scoffset
)
155 if (sc
->vmaddr
+ scoffset
+ sc
->vmsize
- 1 < R_ADDR(r
))
158 * Some part of this segment is in the region.
159 * Trim the edges in the case where we span regions.
161 mach_vm_offset_t loaddr
= sc
->vmaddr
+ scoffset
;
162 mach_vm_offset_t hiaddr
= loaddr
+ sc
->vmsize
;
163 if (loaddr
< R_ADDR(r
))
165 if (hiaddr
> R_ENDADDR(r
))
166 hiaddr
= R_ENDADDR(r
);
168 struct subregionlist
*srl
= calloc(1, sizeof (*srl
));
169 struct subregion
*s
= new_subregion(loaddr
, hiaddr
- loaddr
, sc
, le
);
170 assert(sc
->fileoff
>= 0);
172 STAILQ_INSERT_HEAD(srlh
, srl
, srl_linkage
);
174 if (opt
->debug
> 3) {
176 printr(r
, "subregion %llx-%llx %7s %12s\t%s [%x/%x off %zd for %zd nsects %u flags %x]\n",
177 S_ADDR(s
), S_ENDADDR(s
),
178 str_hsize(hstr
, S_SIZE(s
)),
181 sc
->initprot
, sc
->maxprot
,
182 sc
->fileoff
, sc
->filesize
,
183 sc
->nsects
, sc
->flags
);
190 lc
= (const void *)((caddr_t
)lc
+ lc
->cmdsize
);
194 return WALK_CONTINUE
;
198 * Because we aggregate information from multiple sources, there may
199 * be duplicate subregions. Eliminate them here.
201 * Note that the each library in the shared cache points
202 * separately at a single, unified (large!) __LINKEDIT section; these
203 * get removed here too.
205 * Assumes the subregion array is sorted by address!
208 eliminate_duplicate_subregions(struct region
*r
)
211 while (i
< r
->r_nsubregions
) {
212 struct subregion
*s0
= r
->r_subregions
[i
-1];
213 struct subregion
*s1
= r
->r_subregions
[i
];
215 if (S_ADDR(s0
) != S_ADDR(s1
) || S_SIZE(s0
) != S_SIZE(s1
)) {
219 if (memcmp(&s0
->s_segcmd
, &s1
->s_segcmd
, sizeof (s0
->s_segcmd
)) != 0) {
224 printr(r
, "eliding duplicate %s subregion (%llx-%llx) file %s\n",
225 S_MACHO_TYPE(s1
), S_ADDR(s1
), S_ENDADDR(s1
), S_FILENAME(s1
));
226 /* If the duplicate subregions aren't mapping the same file (?), forget the name */
227 if (s0
->s_libent
!= s1
->s_libent
)
228 s0
->s_libent
= s1
->s_libent
= NULL
;
229 elide_subregion(r
, i
);
234 * See if any of the dyld information we have can better describe this
235 * region of the target address space.
238 decorate_memory_region(struct region
*r
, void *arg
)
240 const dyld_process_info dpi
= arg
;
242 __block walk_return_t retval
= WALK_CONTINUE
;
243 __block subregionlisthead_t srlhead
= STAILQ_HEAD_INITIALIZER(srlhead
);
245 _dyld_process_info_for_each_image(dpi
, ^(uint64_t mhaddr
, const uuid_t uuid
, __unused
const char *path
) {
246 if (WALK_CONTINUE
== retval
) {
247 const struct libent
*le
= libent_lookup_byuuid(uuid
);
248 assert(le
->le_mhaddr
== mhaddr
);
250 * Core dumps conventionally contain the whole executable, but we're trying
251 * to elide everything that can't be found in a file elsewhere.
254 if (MH_EXECUTE
== le
->le_mh
->filetype
)
255 return; // cause the whole a.out to be emitted
257 retval
= add_subregions_for_libent(&srlhead
, r
, le
->le_mh
, le
->le_mhaddr
, le
);
260 if (WALK_CONTINUE
!= retval
)
264 * Take the unsorted list of subregions, if any,
265 * and hang a sorted array of ranges on the region structure.
267 if (!STAILQ_EMPTY(&srlhead
)) {
268 struct subregionlist
*srl
;
269 STAILQ_FOREACH(srl
, &srlhead
, srl_linkage
) {
272 assert(r
->r_nsubregions
);
274 r
->r_subregions
= calloc(r
->r_nsubregions
, sizeof (void *));
276 STAILQ_FOREACH(srl
, &srlhead
, srl_linkage
) {
277 r
->r_subregions
[i
++] = srl
->srl_s
;
279 qsort_b(r
->r_subregions
, r
->r_nsubregions
, sizeof (void *),
280 ^(const void *a
, const void *b
) {
281 const struct subregion
*lhs
= *(struct subregion
**)a
;
282 const struct subregion
*rhs
= *(struct subregion
**)b
;
283 if (S_ADDR(lhs
) > S_ADDR(rhs
))
285 if (S_ADDR(lhs
) < S_ADDR(rhs
))
290 eliminate_duplicate_subregions(r
);
292 const struct libent
*lesc
= NULL
; /* libent ref for shared cache */
293 if (r
->r_insharedregion
) {
295 if (get_sc_uuid(dpi
, uusc
)) {
296 lesc
= libent_lookup_byuuid(uusc
);
297 assert(NULL
== lesc
->le_mh
&& 0 == lesc
->le_mhaddr
);
302 * Only very specific segment types get to be filerefs
304 for (i
= 0; i
< r
->r_nsubregions
; i
++) {
305 struct subregion
*s
= r
->r_subregions
[i
];
307 * Anything writable is trivially disqualified
309 if (s
->s_segcmd
.initprot
& VM_PROT_WRITE
)
312 * As long as there's a filename, __TEXT and __LINKEDIT
313 * end up as a file reference.
315 * __LINKEDIT is more complicated: the segment commands point
316 * at a unified segment in the shared cache mapping.
317 * Ditto for __UNICODE(?)
319 if (issubregiontype(s
, SEG_TEXT
)) {
321 } else if (issubregiontype(s
, SEG_LINKEDIT
)) {
322 if (r
->r_insharedregion
)
324 } else if (issubregiontype(s
, "__UNICODE")) {
325 if (r
->r_insharedregion
)
331 s
->s_isfileref
= true;
334 assert(WALK_CONTINUE
== retval
);
337 if (!STAILQ_EMPTY(&srlhead
)) {
338 struct subregionlist
*srl
, *trl
;
339 STAILQ_FOREACH_SAFE(srl
, &srlhead
, srl_linkage
, trl
) {
347 * Strip region of all decoration
349 * Invoked (on every region!) after an error during the initial
350 * 'decoration' phase to discard to discard potentially incomplete
354 undecorate_memory_region(struct region
*r
, __unused
void *arg
)
356 assert(&sparse_ops
!= r
->r_op
);
357 return r
->r_nsubregions
? clean_subregions(r
) : WALK_CONTINUE
;
361 * This optimization occurs -after- the vanilla_region_optimizations(),
362 * and -after- we've tagged zfod and first-pass fileref's.
365 sparse_region_optimization(struct region
*r
, __unused
void *arg
)
367 assert(&sparse_ops
!= r
->r_op
);
369 if (r
->r_inzfodregion
) {
371 * Pure zfod region: almost certainly a more compact
372 * representation - keep it that way.
374 assert(&zfod_ops
== r
->r_op
);
375 return clean_subregions(r
);
381 * Already have a fileref for the whole region: almost
382 * certainly a more compact representation - keep
385 assert(&fileref_ops
== r
->r_op
);
386 return clean_subregions(r
);
390 if (r
->r_insharedregion
&& 0 == r
->r_nsubregions
) {
392 * A segment in the shared region needs to be
393 * identified with an LC_SEGMENT that dyld claims,
394 * otherwise (we assert) it's not useful to the dump.
398 printr(r
, "not referenced in dyld info => "
399 "eliding %s range in shared region\n",
400 str_hsize(hstr
, R_SIZE(r
)));
402 return WALK_DELETE_REGION
;
405 if (r
->r_nsubregions
> 1) {
407 * Merge adjacent or identical subregions that have no file reference
408 * (Reducing the number of subregions reduces header overhead and
409 * improves compressability)
412 while (i
< r
->r_nsubregions
) {
413 struct subregion
*s0
= r
->r_subregions
[i
-1];
414 struct subregion
*s1
= r
->r_subregions
[i
];
416 if (s0
->s_isfileref
) {
418 continue; /* => destined to be a fileref */
420 if (!issamesubregiontype(s0
, s1
)) {
422 continue; /* merge-able subregions must have same "type" */
425 if (S_ENDADDR(s0
) == S_ADDR(s1
)) {
426 /* directly adjacent subregions */
429 printr(r
, "merging subregions (%llx-%llx + %llx-%llx) -- adjacent\n",
430 S_ADDR(s0
), S_ENDADDR(s0
), S_ADDR(s1
), S_ENDADDR(s1
));
432 S_SETSIZE(s0
, S_ENDADDR(s1
) - S_ADDR(s0
));
433 elide_subregion(r
, i
);
437 const mach_vm_size_t pfn
[2] = {
438 S_ADDR(s0
) >> pageshift_host
,
439 S_ADDR(s1
) >> pageshift_host
441 const mach_vm_size_t endpfn
[2] = {
442 (S_ENDADDR(s0
) - 1) >> pageshift_host
,
443 (S_ENDADDR(s1
) - 1) >> pageshift_host
446 if (pfn
[0] == pfn
[1] && pfn
[0] == endpfn
[0] && pfn
[0] == endpfn
[1]) {
447 /* two small subregions share a host page */
450 printr(r
, "merging subregions (%llx-%llx + %llx-%llx) -- same page\n",
451 S_ADDR(s0
), S_ENDADDR(s0
), S_ADDR(s1
), S_ENDADDR(s1
));
453 S_SETSIZE(s0
, S_ENDADDR(s1
) - S_ADDR(s0
));
454 elide_subregion(r
, i
);
458 if (pfn
[1] == 1 + endpfn
[0]) {
459 /* subregions are pagewise-adjacent: bigger chunks to compress */
462 printr(r
, "merging subregions (%llx-%llx + %llx-%llx) -- adjacent pages\n",
463 S_ADDR(s0
), S_ENDADDR(s0
), S_ADDR(s1
), S_ENDADDR(s1
));
465 S_SETSIZE(s0
, S_ENDADDR(s1
) - S_ADDR(s0
));
466 elide_subregion(r
, i
);
470 i
++; /* this isn't the subregion we're looking for */
474 if (r
->r_nsubregions
)
475 r
->r_op
= &sparse_ops
;
477 return WALK_CONTINUE
;