xref: /xnu-11417.121.6/libkern/os/internal.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions /*
2*a1e26a70SApple OSS Distributions  * Copyright (c) 2013-2015 Apple Inc. All rights reserved.
3*a1e26a70SApple OSS Distributions  *
4*a1e26a70SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*a1e26a70SApple OSS Distributions  *
6*a1e26a70SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*a1e26a70SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*a1e26a70SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*a1e26a70SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*a1e26a70SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*a1e26a70SApple OSS Distributions  * file.
12*a1e26a70SApple OSS Distributions  *
13*a1e26a70SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*a1e26a70SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*a1e26a70SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*a1e26a70SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*a1e26a70SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*a1e26a70SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*a1e26a70SApple OSS Distributions  * limitations under the License.
20*a1e26a70SApple OSS Distributions  *
21*a1e26a70SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*a1e26a70SApple OSS Distributions  */
23*a1e26a70SApple OSS Distributions 
24*a1e26a70SApple OSS Distributions #include "trace_internal.h"
25*a1e26a70SApple OSS Distributions #include <mach-o/loader.h>
26*a1e26a70SApple OSS Distributions #include <string.h>
27*a1e26a70SApple OSS Distributions 
28*a1e26a70SApple OSS Distributions static bool
_os_trace_addr_in_text_segment_32(const void * dso,const void * addr)29*a1e26a70SApple OSS Distributions _os_trace_addr_in_text_segment_32(const void *dso, const void *addr)
30*a1e26a70SApple OSS Distributions {
31*a1e26a70SApple OSS Distributions 	const struct mach_header *mhp = (const struct mach_header *) dso;
32*a1e26a70SApple OSS Distributions 	const struct segment_command *sgp = (const struct segment_command *)(const void *)((const char *)mhp + sizeof(struct mach_header));
33*a1e26a70SApple OSS Distributions 
34*a1e26a70SApple OSS Distributions 	for (uint32_t i = 0; i < mhp->ncmds; i++) {
35*a1e26a70SApple OSS Distributions 		if (sgp->cmd == LC_SEGMENT) {
36*a1e26a70SApple OSS Distributions 			if (strncmp(sgp->segname, SEG_TEXT, sizeof(sgp->segname)) == 0) {
37*a1e26a70SApple OSS Distributions 				return (uintptr_t)addr >= (sgp->vmaddr) && (uintptr_t)addr < (sgp->vmaddr + sgp->vmsize);
38*a1e26a70SApple OSS Distributions 			}
39*a1e26a70SApple OSS Distributions 		}
40*a1e26a70SApple OSS Distributions 		sgp = (const struct segment_command *)(const void *)((const char *)sgp + sgp->cmdsize);
41*a1e26a70SApple OSS Distributions 	}
42*a1e26a70SApple OSS Distributions 
43*a1e26a70SApple OSS Distributions 	return false;
44*a1e26a70SApple OSS Distributions }
45*a1e26a70SApple OSS Distributions 
46*a1e26a70SApple OSS Distributions static bool
_os_trace_addr_in_text_segment_64(const void * dso,const void * addr)47*a1e26a70SApple OSS Distributions _os_trace_addr_in_text_segment_64(const void *dso, const void *addr)
48*a1e26a70SApple OSS Distributions {
49*a1e26a70SApple OSS Distributions 	const struct mach_header_64 *mhp = (const struct mach_header_64 *) dso;
50*a1e26a70SApple OSS Distributions 	const struct segment_command_64 *sgp = (const struct segment_command_64 *)(const void *)((const char *)mhp + sizeof(struct mach_header_64));
51*a1e26a70SApple OSS Distributions 
52*a1e26a70SApple OSS Distributions 	for (uint32_t i = 0; i < mhp->ncmds; i++) {
53*a1e26a70SApple OSS Distributions 		if (sgp->cmd == LC_SEGMENT_64) {
54*a1e26a70SApple OSS Distributions 			if (strncmp(sgp->segname, SEG_TEXT, sizeof(sgp->segname)) == 0) {
55*a1e26a70SApple OSS Distributions 				return (uintptr_t)addr >= (sgp->vmaddr) && (uintptr_t)addr < (sgp->vmaddr + sgp->vmsize);
56*a1e26a70SApple OSS Distributions 			}
57*a1e26a70SApple OSS Distributions 		}
58*a1e26a70SApple OSS Distributions 		sgp = (const struct segment_command_64 *)(const void *)((const char *)sgp + sgp->cmdsize);
59*a1e26a70SApple OSS Distributions 	}
60*a1e26a70SApple OSS Distributions 
61*a1e26a70SApple OSS Distributions 	return false;
62*a1e26a70SApple OSS Distributions }
63*a1e26a70SApple OSS Distributions 
64*a1e26a70SApple OSS Distributions bool
_os_trace_addr_in_text_segment(const void * dso,const void * addr)65*a1e26a70SApple OSS Distributions _os_trace_addr_in_text_segment(const void *dso, const void *addr)
66*a1e26a70SApple OSS Distributions {
67*a1e26a70SApple OSS Distributions 	const struct mach_header *mhp = (const struct mach_header *) dso;
68*a1e26a70SApple OSS Distributions 	bool retval = false;
69*a1e26a70SApple OSS Distributions 
70*a1e26a70SApple OSS Distributions 	switch (mhp->magic) {
71*a1e26a70SApple OSS Distributions 	case MH_MAGIC:
72*a1e26a70SApple OSS Distributions 		retval = _os_trace_addr_in_text_segment_32(dso, addr);
73*a1e26a70SApple OSS Distributions 		break;
74*a1e26a70SApple OSS Distributions 
75*a1e26a70SApple OSS Distributions 	case MH_MAGIC_64:
76*a1e26a70SApple OSS Distributions 		retval = _os_trace_addr_in_text_segment_64(dso, addr);
77*a1e26a70SApple OSS Distributions 		break;
78*a1e26a70SApple OSS Distributions 
79*a1e26a70SApple OSS Distributions 	default:
80*a1e26a70SApple OSS Distributions 		retval = false;
81*a1e26a70SApple OSS Distributions 		break;
82*a1e26a70SApple OSS Distributions 	}
83*a1e26a70SApple OSS Distributions 
84*a1e26a70SApple OSS Distributions 	return retval;
85*a1e26a70SApple OSS Distributions }
86