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