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