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