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