···11-// Modified by Lubos Dolezel for Darling build
21/*
33- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
22+ * Copyright (c) 2016 Apple, Inc. All rights reserved.
43 *
54 * @APPLE_LICENSE_HEADER_START@
65 *
···4342 * and contains the constants for the possible values of these types.
4443 */
4544#include <stdint.h>
4646-4747-typedef int cpu_type_t;
4848-typedef int cpu_subtype_t;
4545+#include <mach/machine.h>
4646+#include <architecture/byte_order.h>
49475048#define FAT_MAGIC 0xcafebabe
5149#define FAT_CIGAM 0xbebafeca /* NXSwapLong(FAT_MAGIC) */
52505351struct fat_header {
5454- uint32_t magic; /* FAT_MAGIC */
5252+ uint32_t magic; /* FAT_MAGIC or FAT_MAGIC_64 */
5553 uint32_t nfat_arch; /* number of structs that follow */
5654};
5755···6159 uint32_t offset; /* file offset to this object file */
6260 uint32_t size; /* size of this object file */
6361 uint32_t align; /* alignment as a power of 2 */
6262+};
6363+6464+/*
6565+ * The support for the 64-bit fat file format described here is a work in
6666+ * progress and not yet fully supported in all the Apple Developer Tools.
6767+ *
6868+ * When a slice is greater than 4mb or an offset to a slice is greater than 4mb
6969+ * then the 64-bit fat file format is used.
7070+ */
7171+#define FAT_MAGIC_64 0xcafebabf
7272+#define FAT_CIGAM_64 0xbfbafeca /* NXSwapLong(FAT_MAGIC_64) */
7373+7474+struct fat_arch_64 {
7575+ cpu_type_t cputype; /* cpu specifier (int) */
7676+ cpu_subtype_t cpusubtype; /* machine specifier (int) */
7777+ uint64_t offset; /* file offset to this object file */
7878+ uint64_t size; /* size of this object file */
7979+ uint32_t align; /* alignment as a power of 2 */
8080+ uint32_t reserved; /* reserved */
6481};
65826683#endif /* _MACH_O_FAT_H_ */
+26-3
platform-include/mach-o/ranlib.h
···11/*
22- * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
22+ * Copyright (c) 2016 Apple, Inc. All rights reserved.
33 *
44 * @APPLE_LICENSE_HEADER_START@
55 *
···49495050/*
5151 * Structure of the __.SYMDEF table of contents for an archive.
5252- * __.SYMDEF begins with a long giving the size in bytes of the ranlib
5252+ * __.SYMDEF begins with a uint32_t giving the size in bytes of the ranlib
5353 * structures which immediately follow, and then continues with a string
5454- * table consisting of a long giving the number of bytes of strings which
5454+ * table consisting of a uint32_t giving the number of bytes of strings which
5555 * follow and then the strings themselves. The ran_strx fields index the
5656 * string table whose first byte is numbered 0.
5757 */
···6363#endif
6464 } ran_un;
6565 uint32_t ran_off; /* library member at this offset */
6666+};
6767+6868+#define SYMDEF_64 "__.SYMDEF_64"
6969+#define SYMDEF_64_SORTED "__.SYMDEF_64 SORTED"
7070+7171+/*
7272+ * The support for the 64-bit table of contents described here is a work in
7373+ * progress and not yet fully supported in all the Apple Developer Tools.
7474+ *
7575+ * When an archive offset to a library member is more than 32-bits then this is
7676+ * the structure of the __.SYMDEF_64 table of contents for an archive.
7777+ * __.SYMDEF_64 begins with a uint64_t giving the size in bytes of the ranlib
7878+ * structures which immediately follow, and then continues with a string
7979+ * table consisting of a uint64_t giving the number of bytes of strings which
8080+ * follow and then the strings themselves. The ran_strx fields index the
8181+ * string table whose first byte is numbered 0.
8282+ */
8383+8484+struct ranlib_64 {
8585+ union {
8686+ uint64_t ran_strx; /* string table index of */
8787+ } ran_un;
8888+ uint64_t ran_off; /* library member at this offset */
6689};
6790#endif /* _MACH_O_RANLIB_H_ */
···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2015 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#include "threads.h"
2121+#include <pthread.h>
2222+#include <sys/mman.h>
2323+#include <semaphore.h>
2424+#include <string.h>
2525+#include <stdbool.h>
2626+#include <stdlib.h>
2727+2828+// The point of this file is build macOS threads on top of native libc's threads,
2929+// otherwise it would not be possible to make native calls from these threads.
3030+3131+typedef void (*thread_ep)(void**, int, ...);
3232+struct arg_struct
3333+{
3434+ thread_ep entry_point;
3535+ uintptr_t arg3;
3636+ uintptr_t arg4;
3737+ uintptr_t arg5;
3838+ uintptr_t arg6;
3939+ union
4040+ {
4141+ int (*thread_self_trap)();
4242+ int port;
4343+ };
4444+ unsigned long pth_obj_size;
4545+ void* pth;
4646+};
4747+struct reaper_item
4848+{
4949+ struct reaper_item* next;
5050+ pthread_t thread;
5151+ void* stack;
5252+ size_t stacksize;
5353+};
5454+5555+static void* darling_thread_entry(void* p);
5656+static void start_reaper();
5757+5858+static sem_t reaper_sem;
5959+static pthread_mutex_t reaper_mutex = PTHREAD_MUTEX_INITIALIZER;
6060+static struct reaper_item *reaper_items_front = NULL, *reaper_items_end = NULL;
6161+static void reaper_item_push(struct reaper_item* item);
6262+static struct reaper_item* reaper_item_pop(void);
6363+6464+#ifndef PTHREAD_STACK_MIN
6565+# define PTHREAD_STACK_MIN 16384
6666+#endif
6767+6868+void* __darling_thread_create(unsigned long stack_size, unsigned long pth_obj_size,
6969+ void* entry_point, uintptr_t arg3,
7070+ uintptr_t arg4, uintptr_t arg5, uintptr_t arg6,
7171+ int (*thread_self_trap)())
7272+{
7373+ static pthread_once_t reaper_once = PTHREAD_ONCE_INIT;
7474+7575+ struct arg_struct args = { (thread_ep) entry_point, arg3,
7676+ arg4, arg5, arg6, thread_self_trap, pth_obj_size, NULL };
7777+ pthread_attr_t attr;
7878+ pthread_t nativeLibcThread;
7979+ void* pth;
8080+8181+ pthread_once(&reaper_once, start_reaper);
8282+8383+ pthread_attr_init(&attr);
8484+ //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
8585+ // pthread_attr_setstacksize(&attr, stack_size);
8686+8787+ pth = mmap(NULL, stack_size + pth_obj_size + 0x1000, PROT_READ | PROT_WRITE,
8888+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
8989+ pthread_attr_setstack(&attr, ((char*)pth) + pth_obj_size, stack_size - pth_obj_size - 0x1000);
9090+9191+ // std::cout << "Allocated stack at " << pth << ", size " << stack_size << std::endl;
9292+ pth = ((char*) pth) + stack_size + 0x1000;
9393+9494+ args.pth = pth;
9595+ pthread_create(&nativeLibcThread, &attr, darling_thread_entry, &args);
9696+ pthread_attr_destroy(&attr);
9797+9898+ while (args.pth != NULL)
9999+ sched_yield();
100100+101101+ return pth;
102102+}
103103+104104+static void* darling_thread_entry(void* p)
105105+{
106106+ struct arg_struct* in_args = (struct arg_struct*) p;
107107+ struct arg_struct args;
108108+109109+ memcpy(&args, in_args, sizeof(args));
110110+111111+ args.port = args.thread_self_trap();
112112+ in_args->pth = NULL;
113113+114114+#ifdef __x86_64__
115115+ __asm__ __volatile__ (
116116+ "movq %1, %%rdi\n"
117117+ "movq 40(%0), %%rsi\n"
118118+ "movq 8(%0), %%rdx\n"
119119+ "testq %%rdx, %%rdx\n"
120120+ "jnz 1f\n"
121121+ "movq %%rsp, %%rdx\n" // wqthread hack: if 3rd arg is null, we pass sp
122122+ "1:\n"
123123+ "movq 16(%0), %%rcx\n"
124124+ "movq 24(%0), %%r8\n"
125125+ "movq 32(%0), %%r9\n"
126126+ "movq %%rdi, 56(%0)\n"
127127+ "movq (%0), %%rax\n"
128128+ "andq $-0x10, %%rsp\n"
129129+ "pushq $0\n"
130130+ "pushq $0\n"
131131+ "jmpq *%%rax\n"
132132+ :: "a" (&args), "di" (args.pth));
133133+#elif defined(__i386__) // args in eax, ebx, ecx, edx, edi, esi
134134+ __asm__ __volatile__ (
135135+ "movl (%0), %%eax\n"
136136+ "pushl %%eax\n" // address to be jumped to
137137+ "movl %1, 28(%0)\n"
138138+ "movl %1, %%eax\n" // 1st arg
139139+ "movl 20(%0), %%ebx\n" // 2nd arg
140140+ "movl 8(%0), %%edx\n" // 4th arg
141141+ "movl 12(%0), %%edi\n" // 5th arg
142142+ "movl 16(%0), %%esi\n" // 6th arg
143143+ "movl 4(%0), %%ecx\n" // 3rd arg
144144+ "testl %%ecx, %%ecx\n" // FIXME: clobbered ecx!
145145+ "jnz 1f\n"
146146+ "movl %%esp, %%ecx\n"
147147+ "1:\n"
148148+ "ret\n" // Jump to the address pushed at the beginning
149149+ :: "c" (&args), "d" (args.pth));
150150+#endif
151151+ return NULL;
152152+}
153153+154154+int __darling_thread_terminate(void* stackaddr,
155155+ unsigned long freesize, unsigned long pthobj_size)
156156+{
157157+ struct reaper_item* item = (struct reaper_item*) malloc(sizeof(struct reaper_item));
158158+ item->thread = pthread_self();
159159+ item->stack = stackaddr;
160160+ item->stacksize = freesize;
161161+ reaper_item_push(item);
162162+163163+ sem_post(&reaper_sem);
164164+165165+ pthread_exit(NULL);
166166+167167+ __builtin_unreachable();
168168+}
169169+170170+void* __darling_thread_get_stack(void)
171171+{
172172+ pthread_attr_t attr;
173173+ void* stackaddr;
174174+ size_t stacksize;
175175+176176+ pthread_getattr_np(pthread_self(), &attr);
177177+ pthread_attr_getstack(&attr, &stackaddr, &stacksize);
178178+179179+ return ((char*)stackaddr) + stacksize - 0x2000;
180180+}
181181+182182+static void* reaper_entry(void* unused)
183183+{
184184+ while (true)
185185+ {
186186+ struct reaper_item* item;
187187+188188+ sem_wait(&reaper_sem);
189189+190190+ item = reaper_item_pop();
191191+ if (!item)
192192+ continue; // Should not happen!
193193+194194+ // std::cout << "Reaping thread " << (void*)item.thread << "; Free stack at " << item.stack << ", " << item.stacksize << " bytes\n";
195195+196196+ // Wait for thread to terminate
197197+ pthread_join(item->thread, NULL);
198198+199199+ // Free its stack in the extended range requested by Darwin's libc
200200+ munmap(item->stack, item->stacksize);
201201+202202+ free(item);
203203+ }
204204+}
205205+206206+static void start_reaper()
207207+{
208208+ pthread_attr_t attr;
209209+ pthread_t thread;
210210+211211+ pthread_attr_init(&attr);
212212+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
213213+214214+ sem_init(&reaper_sem, 0, 0);
215215+ pthread_create(&thread, &attr, reaper_entry, NULL);
216216+ pthread_attr_destroy(&attr);
217217+}
218218+219219+static void reaper_item_push(struct reaper_item* item)
220220+{
221221+ pthread_mutex_lock(&reaper_mutex);
222222+223223+ item->next = NULL;
224224+ if (reaper_items_end != NULL)
225225+ {
226226+ reaper_items_end->next = item;
227227+ reaper_items_end = item;
228228+ }
229229+ else
230230+ {
231231+ reaper_items_front = reaper_items_end = item;
232232+ }
233233+234234+ pthread_mutex_unlock(&reaper_mutex);
235235+}
236236+237237+static struct reaper_item* reaper_item_pop(void)
238238+{
239239+ struct reaper_item* e;
240240+ pthread_mutex_lock(&reaper_mutex);
241241+242242+ if (reaper_items_front != NULL)
243243+ {
244244+ e = reaper_items_front;
245245+246246+ if (reaper_items_front == reaper_items_end)
247247+ reaper_items_front = reaper_items_end = NULL; // The list is now empty
248248+ else
249249+ reaper_items_front = e->next;
250250+ }
251251+ else
252252+ e = NULL;
253253+254254+ pthread_mutex_unlock(&reaper_mutex);
255255+256256+ return e;
257257+}
258258+
+41
src/dyld/threads.h
···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2015 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#ifndef DYLD_THREADS_H
2121+#define DYLD_THREADS_H
2222+#include <stdint.h>
2323+2424+#ifdef __cplusplus
2525+extern "C" {
2626+#endif
2727+2828+void* __darling_thread_create(unsigned long stack_size, unsigned long pthobj_size,
2929+ void* entry_point, uintptr_t arg3,
3030+ uintptr_t arg4, uintptr_t arg5, uintptr_t arg6,
3131+ int (*thread_self_trap)());
3232+int __darling_thread_terminate(void* stackaddr,
3333+ unsigned long freesize, unsigned long pthobj_size);
3434+void* __darling_thread_get_stack(void);
3535+3636+#ifdef __cplusplus
3737+}
3838+#endif
3939+4040+#endif
4141+
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. Please obtain a copy of the License at
1010+ * http://www.opensource.apple.com/apsl/ and read it before using this
1111+ * file.
1212+ *
1313+ * The Original Code and all software distributed under the License are
1414+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1515+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
1616+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
1717+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
1818+ * Please see the License for the specific language governing rights and
1919+ * limitations under the License.
2020+ *
2121+ * @APPLE_LICENSE_HEADER_END@
2222+ */
2323+#ifndef RLD
2424+#include <string.h>
2525+#include <mach-o/ldsyms.h>
2626+#include <mach-o/getsect.h>
2727+#ifndef __OPENSTEP__
2828+#include <crt_externs.h>
2929+#else /* defined(__OPENSTEP__) */
3030+#ifdef __DYNAMIC__
3131+#include "mach-o/dyld.h" /* defines _dyld_lookup_and_bind() */
3232+#endif
3333+3434+#if !defined(__DYNAMIC__)
3535+#define DECLARE_VAR(var, type) \
3636+extern type var
3737+#define SETUP_VAR(var)
3838+#define USE_VAR(var) var
3939+#else
4040+#define STRINGIFY(a) # a
4141+#define DECLARE_VAR(var, type) \
4242+static type * var ## _pointer = 0
4343+#define SETUP_VAR(var) \
4444+if ( var ## _pointer == 0) { \
4545+ _dyld_lookup_and_bind( STRINGIFY(_ ## var), \
4646+ (uint32_t *) & var ## _pointer, 0); \
4747+}
4848+#define USE_VAR(var) (* var ## _pointer)
4949+#endif
5050+#endif /* __OPENSTEP__ */
5151+5252+/*
5353+ * This routine returns the highest address of the segments in the program (NOT
5454+ * the shared libraries). It is intended to be used as a stop gap for programs
5555+ * that make UNIX style assumptions about how memory is allocated. Typicly the
5656+ * asumptions under which this is used is that memory is contiguously allocated
5757+ * by the program's text and data from address 0 with no gaps. The value of
5858+ * this differs from the value of &_end in a UNIX program in that this routine
5959+ * returns the address of the end of the segment not the end of the last section
6060+ * in that segment as would be the value of the symbol &_end.
6161+ */
6262+unsigned long
6363+get_end(void)
6464+{
6565+#ifndef __LP64__
6666+6767+ struct segment_command *sgp;
6868+ unsigned long _end;
6969+ uint32_t i;
7070+#ifndef __OPENSTEP__
7171+ struct mach_header *mhp = _NSGetMachExecuteHeader();
7272+#else /* defined(__OPENSTEP__) */
7373+ static struct mach_header *mhp = NULL;
7474+ DECLARE_VAR(_mh_execute_header, struct mach_header);
7575+ SETUP_VAR(_mh_execute_header);
7676+7777+ mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header));
7878+#endif /* __OPENSTEP__ */
7979+ _end = 0;
8080+ sgp = (struct segment_command *)
8181+ ((char *)mhp + sizeof(struct mach_header));
8282+ for(i = 0; i < mhp->ncmds; i++){
8383+ if(sgp->cmd == LC_SEGMENT)
8484+ if(sgp->vmaddr + sgp->vmsize > _end)
8585+ _end = sgp->vmaddr + sgp->vmsize;
8686+ sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
8787+ }
8888+ return(_end);
8989+9090+#else /* defined(__LP64__) */
9191+9292+ struct mach_header_64 *mhp = _NSGetMachExecuteHeader();
9393+ struct segment_command_64 *sgp;
9494+ unsigned long _end;
9595+ uint32_t i;
9696+9797+ _end = 0;
9898+ sgp = (struct segment_command_64 *)
9999+ ((char *)mhp + sizeof(struct mach_header_64));
100100+ for(i = 0; i < mhp->ncmds; i++){
101101+ if(sgp->cmd == LC_SEGMENT_64)
102102+ if(sgp->vmaddr + sgp->vmsize > _end)
103103+ _end = sgp->vmaddr + sgp->vmsize;
104104+ sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
105105+ }
106106+ return(_end);
107107+108108+#endif /* defined(__LP64__) */
109109+}
110110+111111+/*
112112+ * This returns what was the value of the UNIX link editor defined symbol
113113+ * _etext (the first address after the text section). Note this my or may not
114114+ * be the only section in the __TEXT segment.
115115+ */
116116+unsigned long
117117+get_etext(void)
118118+{
119119+#ifndef __LP64__
120120+ const struct section *sp;
121121+#else /* defined(__LP64__) */
122122+ const struct section_64 *sp;
123123+#endif /* defined(__LP64__) */
124124+125125+ sp = getsectbyname(SEG_TEXT, SECT_TEXT);
126126+ if(sp)
127127+ return(sp->addr + sp->size);
128128+ else
129129+ return(0);
130130+}
131131+132132+/*
133133+ * This returns what was the value of the UNIX link editor defined symbol
134134+ * _edata (the first address after the data section). Note this my or may not
135135+ * be the last non-zero fill section in the __DATA segment.
136136+ */
137137+unsigned long
138138+get_edata(void)
139139+{
140140+#ifndef __LP64__
141141+ const struct section *sp;
142142+#else /* defined(__LP64__) */
143143+ const struct section_64 *sp;
144144+#endif /* defined(__LP64__) */
145145+146146+ sp = getsectbyname(SEG_DATA, SECT_DATA);
147147+ if(sp)
148148+ return(sp->addr + sp->size);
149149+ else
150150+ return(0);
151151+}
152152+#endif /* !defined(RLD) */
+569
src/libmacho/getsecbyname.c
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. Please obtain a copy of the License at
1010+ * http://www.opensource.apple.com/apsl/ and read it before using this
1111+ * file.
1212+ *
1313+ * The Original Code and all software distributed under the License are
1414+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1515+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
1616+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
1717+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
1818+ * Please see the License for the specific language governing rights and
1919+ * limitations under the License.
2020+ *
2121+ * @APPLE_LICENSE_HEADER_END@
2222+ */
2323+#ifndef RLD
2424+#include <mach-o/ldsyms.h>
2525+#include <mach-o/swap.h>
2626+#include <string.h>
2727+#ifdef __DYNAMIC__
2828+#include <mach-o/dyld.h> /* defines _dyld_lookup_and_bind() */
2929+#endif /* defined(__DYNAMIC__) */
3030+#ifndef __OPENSTEP__
3131+#include <crt_externs.h>
3232+#else /* defined(__OPENSTEP__) */
3333+3434+#if !defined(__DYNAMIC__)
3535+#define DECLARE_VAR(var, type) \
3636+extern type var
3737+#define SETUP_VAR(var)
3838+#define USE_VAR(var) var
3939+#else
4040+#define STRINGIFY(a) # a
4141+#define DECLARE_VAR(var, type) \
4242+static type * var ## _pointer = NULL
4343+#define SETUP_VAR(var) \
4444+if ( var ## _pointer == NULL) { \
4545+ _dyld_lookup_and_bind( STRINGIFY(_ ## var), \
4646+ (uint32_t *) & var ## _pointer, NULL); \
4747+}
4848+#define USE_VAR(var) (* var ## _pointer)
4949+#endif
5050+#endif /* __OPENSTEP__ */
5151+5252+/*
5353+ * This routine returns the section structure for the named section in the
5454+ * named segment for the mach_header pointer passed to it if it exist.
5555+ * Otherwise it returns zero.
5656+ */
5757+const struct section *
5858+getsectbynamefromheader(
5959+struct mach_header *mhp,
6060+const char *segname,
6161+const char *sectname)
6262+{
6363+ struct segment_command *sgp;
6464+ struct section *sp;
6565+ uint32_t i, j;
6666+6767+ sgp = (struct segment_command *)
6868+ ((char *)mhp + sizeof(struct mach_header));
6969+ for(i = 0; i < mhp->ncmds; i++){
7070+ if(sgp->cmd == LC_SEGMENT)
7171+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
7272+ mhp->filetype == MH_OBJECT){
7373+ sp = (struct section *)((char *)sgp +
7474+ sizeof(struct segment_command));
7575+ for(j = 0; j < sgp->nsects; j++){
7676+ if(strncmp(sp->sectname, sectname,
7777+ sizeof(sp->sectname)) == 0 &&
7878+ strncmp(sp->segname, segname,
7979+ sizeof(sp->segname)) == 0)
8080+ return(sp);
8181+ sp = (struct section *)((char *)sp +
8282+ sizeof(struct section));
8383+ }
8484+ }
8585+ sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
8686+ }
8787+ return((struct section *)0);
8888+}
8989+9090+/*
9191+ * This routine returns the section structure for the named section in the
9292+ * named segment for the mach_header_64 pointer passed to it if it exist.
9393+ * Otherwise it returns zero.
9494+ */
9595+const struct section_64 *
9696+getsectbynamefromheader_64(
9797+struct mach_header_64 *mhp,
9898+const char *segname,
9999+const char *sectname)
100100+{
101101+ struct segment_command_64 *sgp;
102102+ struct section_64 *sp;
103103+ uint32_t i, j;
104104+105105+ sgp = (struct segment_command_64 *)
106106+ ((char *)mhp + sizeof(struct mach_header_64));
107107+ for(i = 0; i < mhp->ncmds; i++){
108108+ if(sgp->cmd == LC_SEGMENT_64)
109109+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
110110+ mhp->filetype == MH_OBJECT){
111111+ sp = (struct section_64 *)((char *)sgp +
112112+ sizeof(struct segment_command_64));
113113+ for(j = 0; j < sgp->nsects; j++){
114114+ if(strncmp(sp->sectname, sectname,
115115+ sizeof(sp->sectname)) == 0 &&
116116+ strncmp(sp->segname, segname,
117117+ sizeof(sp->segname)) == 0)
118118+ return(sp);
119119+ sp = (struct section_64 *)((char *)sp +
120120+ sizeof(struct section_64));
121121+ }
122122+ }
123123+ sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
124124+ }
125125+ return((struct section_64 *)0);
126126+}
127127+128128+/*
129129+ * This routine returns the section structure for the named section in the
130130+ * named segment for the mach_header pointer passed to it if it exist.
131131+ * Otherwise it returns zero. If fSwap == YES (the mach header has been
132132+ * swapped to the endiannes of the current machine, but the segments and
133133+ * sections are different) then the segment and sections are swapped.
134134+ */
135135+const struct section *
136136+getsectbynamefromheaderwithswap(
137137+ struct mach_header *mhp,
138138+ const char *segname,
139139+ const char *sectname,
140140+ int fSwap)
141141+{
142142+ struct segment_command *sgp;
143143+ struct section *sp;
144144+ uint32_t i, j;
145145+146146+ sgp = (struct segment_command *)
147147+ ((char *)mhp + sizeof(struct mach_header));
148148+ for(i = 0; i < mhp->ncmds; i++){
149149+ if(sgp->cmd == (fSwap ? OSSwapInt32(LC_SEGMENT) : LC_SEGMENT)) {
150150+151151+ if (fSwap) {
152152+#ifdef __LITTLE_ENDIAN__
153153+ swap_segment_command(sgp, NX_BigEndian);
154154+#else
155155+ swap_segment_command(sgp, NX_LittleEndian);
156156+#endif /* __LITTLE_ENDIAN__ */
157157+ }
158158+159159+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
160160+ mhp->filetype == MH_OBJECT){
161161+ sp = (struct section *)((char *)sgp +
162162+ sizeof(struct segment_command));
163163+164164+ if (fSwap) {
165165+#ifdef __LITTLE_ENDIAN__
166166+ swap_section(sp, sgp->nsects, NX_BigEndian);
167167+#else
168168+ swap_section(sp, sgp->nsects, NX_LittleEndian);
169169+#endif /* __LITTLE_ENDIAN__ */
170170+ }
171171+172172+ for(j = 0; j < sgp->nsects; j++){
173173+ if(strncmp(sp->sectname, sectname,
174174+ sizeof(sp->sectname)) == 0 &&
175175+ strncmp(sp->segname, segname,
176176+ sizeof(sp->segname)) == 0)
177177+ return(sp);
178178+ sp = (struct section *)((char *)sp +
179179+ sizeof(struct section));
180180+ }
181181+ }
182182+ sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
183183+ } else {
184184+ sgp = (struct segment_command *)((char *)sgp +
185185+ (fSwap ? OSSwapInt32(sgp->cmdsize) : sgp->cmdsize));
186186+ }
187187+ }
188188+ return((struct section *)0);
189189+}
190190+191191+/*
192192+ * This routine returns the section_64 structure for the named section in the
193193+ * named segment for the mach_header_64 pointer passed to it if it exist.
194194+ * Otherwise it returns zero. If fSwap == YES (the mach header has been
195195+ * swapped to the endiannes of the current machine, but the segments and
196196+ * sections are different) then the segment and sections are swapped.
197197+ */
198198+const struct section_64 *
199199+getsectbynamefromheaderwithswap_64(
200200+ struct mach_header_64 *mhp,
201201+ const char *segname,
202202+ const char *sectname,
203203+ int fSwap)
204204+{
205205+ struct segment_command_64 *sgp;
206206+ struct section_64 *sp;
207207+ uint32_t i, j;
208208+209209+ sgp = (struct segment_command_64 *)
210210+ ((char *)mhp + sizeof(struct mach_header_64));
211211+ for(i = 0; i < mhp->ncmds; i++){
212212+ if(sgp->cmd == (fSwap ? OSSwapInt32(LC_SEGMENT) : LC_SEGMENT)) {
213213+214214+ if (fSwap) {
215215+#ifdef __LITTLE_ENDIAN__
216216+ swap_segment_command_64(sgp, NX_BigEndian);
217217+#else
218218+ swap_segment_command_64(sgp, NX_LittleEndian);
219219+#endif /* __LITTLE_ENDIAN__ */
220220+ }
221221+222222+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
223223+ mhp->filetype == MH_OBJECT){
224224+ sp = (struct section_64 *)((char *)sgp +
225225+ sizeof(struct segment_command_64));
226226+227227+ if (fSwap) {
228228+#ifdef __LITTLE_ENDIAN__
229229+ swap_section_64(sp, sgp->nsects, NX_BigEndian);
230230+#else
231231+ swap_section_64(sp, sgp->nsects, NX_LittleEndian);
232232+#endif /* __LITTLE_ENDIAN__ */
233233+ }
234234+235235+ for(j = 0; j < sgp->nsects; j++){
236236+ if(strncmp(sp->sectname, sectname,
237237+ sizeof(sp->sectname)) == 0 &&
238238+ strncmp(sp->segname, segname,
239239+ sizeof(sp->segname)) == 0)
240240+ return(sp);
241241+ sp = (struct section_64 *)((char *)sp +
242242+ sizeof(struct section_64));
243243+ }
244244+ }
245245+ sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
246246+ } else {
247247+ sgp = (struct segment_command_64 *)((char *)sgp +
248248+ (fSwap ? OSSwapInt32(sgp->cmdsize) : sgp->cmdsize));
249249+ }
250250+ }
251251+ return((struct section_64 *)0);
252252+}
253253+254254+/*
255255+ * This routine returns the a pointer the section structure of the named
256256+ * section in the named segment if it exist in the mach executable it is
257257+ * linked into. Otherwise it returns zero.
258258+ */
259259+#ifndef __LP64__
260260+261261+const struct section *
262262+getsectbyname(
263263+const char *segname,
264264+const char *sectname)
265265+{
266266+#ifndef __OPENSTEP__
267267+ struct mach_header *mhp = _NSGetMachExecuteHeader();
268268+#else /* defined(__OPENSTEP__) */
269269+ static struct mach_header *mhp = NULL;
270270+ DECLARE_VAR(_mh_execute_header, struct mach_header);
271271+ SETUP_VAR(_mh_execute_header);
272272+ mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header));
273273+#endif /* __OPENSTEP__ */
274274+ return(getsectbynamefromheader(mhp, segname, sectname));
275275+}
276276+277277+#else /* defined(__LP64__) */
278278+279279+const struct section_64 *
280280+getsectbyname(
281281+const char *segname,
282282+const char *sectname)
283283+{
284284+ struct mach_header_64 *mhp = _NSGetMachExecuteHeader();
285285+286286+ return(getsectbynamefromheader_64(mhp, segname, sectname));
287287+}
288288+289289+#endif /* defined(__LP64__) */
290290+291291+/*
292292+ * This routine returns the a pointer to the data for the named section in the
293293+ * named segment if it exist in the mach executable it is linked into. Also
294294+ * it returns the size of the section data indirectly through the pointer size.
295295+ * Otherwise it returns zero for the pointer and the size.
296296+ */
297297+char *
298298+getsectdata(
299299+const char *segname,
300300+const char *sectname,
301301+unsigned long *size)
302302+{
303303+#ifndef __LP64__
304304+ const struct section *sp;
305305+#else /* defined(__LP64__) */
306306+ const struct section_64 *sp;
307307+#endif /* defined(__LP64__) */
308308+309309+ sp = getsectbyname(segname, sectname);
310310+ if(sp == NULL){
311311+ *size = 0;
312312+ return(NULL);
313313+ }
314314+ *size = sp->size;
315315+ return((char *)(sp->addr));
316316+}
317317+318318+/*
319319+ * This routine returns the a pointer to the section contents of the named
320320+ * section in the named segment if it exists in the image pointed to by the
321321+ * mach header. Otherwise it returns zero.
322322+ */
323323+#ifndef __LP64__
324324+325325+uint8_t *
326326+getsectiondata(
327327+const struct mach_header *mhp,
328328+const char *segname,
329329+const char *sectname,
330330+unsigned long *size)
331331+{
332332+ struct segment_command *sgp;
333333+ struct section *sp;
334334+ uint32_t i, j;
335335+ intptr_t slide;
336336+337337+ slide = 0;
338338+ sp = 0;
339339+ sgp = (struct segment_command *)
340340+ ((char *)mhp + sizeof(struct mach_header));
341341+ for(i = 0; i < mhp->ncmds; i++){
342342+ if(sgp->cmd == LC_SEGMENT){
343343+ if(strcmp(sgp->segname, "__TEXT") == 0){
344344+ slide = (uintptr_t)mhp - sgp->vmaddr;
345345+ }
346346+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
347347+ sp = (struct section *)((char *)sgp +
348348+ sizeof(struct segment_command));
349349+ for(j = 0; j < sgp->nsects; j++){
350350+ if(strncmp(sp->sectname, sectname,
351351+ sizeof(sp->sectname)) == 0 &&
352352+ strncmp(sp->segname, segname,
353353+ sizeof(sp->segname)) == 0){
354354+ *size = sp->size;
355355+ return((uint8_t *)(sp->addr) + slide);
356356+ }
357357+ sp = (struct section *)((char *)sp +
358358+ sizeof(struct section));
359359+ }
360360+ }
361361+ }
362362+ sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
363363+ }
364364+ return(0);
365365+}
366366+367367+uint8_t *
368368+getsegmentdata(
369369+const struct mach_header *mhp,
370370+const char *segname,
371371+unsigned long *size)
372372+{
373373+ struct segment_command *sgp;
374374+ intptr_t slide;
375375+ uint32_t i;
376376+377377+ slide = 0;
378378+ sgp = (struct segment_command *)
379379+ ((char *)mhp + sizeof(struct mach_header));
380380+ for(i = 0; i < mhp->ncmds; i++){
381381+ if(sgp->cmd == LC_SEGMENT){
382382+ if(strcmp(sgp->segname, "__TEXT") == 0){
383383+ slide = (uintptr_t)mhp - sgp->vmaddr;
384384+ }
385385+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
386386+ *size = sgp->vmsize;
387387+ return((uint8_t *)(sgp->vmaddr + slide));
388388+ }
389389+ }
390390+ sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
391391+ }
392392+ return(0);
393393+}
394394+395395+#else /* defined(__LP64__) */
396396+397397+uint8_t *
398398+getsectiondata(
399399+const struct mach_header_64 *mhp,
400400+const char *segname,
401401+const char *sectname,
402402+unsigned long *size)
403403+{
404404+ struct segment_command_64 *sgp;
405405+ struct section_64 *sp;
406406+ uint32_t i, j;
407407+ intptr_t slide;
408408+409409+ slide = 0;
410410+ sp = 0;
411411+ sgp = (struct segment_command_64 *)
412412+ ((char *)mhp + sizeof(struct mach_header_64));
413413+ for(i = 0; i < mhp->ncmds; i++){
414414+ if(sgp->cmd == LC_SEGMENT_64){
415415+ if(strcmp(sgp->segname, "__TEXT") == 0){
416416+ slide = (uintptr_t)mhp - sgp->vmaddr;
417417+ }
418418+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
419419+ sp = (struct section_64 *)((char *)sgp +
420420+ sizeof(struct segment_command_64));
421421+ for(j = 0; j < sgp->nsects; j++){
422422+ if(strncmp(sp->sectname, sectname,
423423+ sizeof(sp->sectname)) == 0 &&
424424+ strncmp(sp->segname, segname,
425425+ sizeof(sp->segname)) == 0){
426426+ *size = sp->size;
427427+ return((uint8_t *)(sp->addr) + slide);
428428+ }
429429+ sp = (struct section_64 *)((char *)sp +
430430+ sizeof(struct section_64));
431431+ }
432432+ }
433433+ }
434434+ sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
435435+ }
436436+ return(0);
437437+}
438438+439439+uint8_t *
440440+getsegmentdata(
441441+const struct mach_header_64 *mhp,
442442+const char *segname,
443443+unsigned long *size)
444444+{
445445+ struct segment_command_64 *sgp;
446446+ intptr_t slide;
447447+ uint32_t i;
448448+449449+ slide = 0;
450450+ sgp = (struct segment_command_64 *)
451451+ ((char *)mhp + sizeof(struct mach_header_64));
452452+ for(i = 0; i < mhp->ncmds; i++){
453453+ if(sgp->cmd == LC_SEGMENT_64){
454454+ if(strcmp(sgp->segname, "__TEXT") == 0){
455455+ slide = (uintptr_t)mhp - sgp->vmaddr;
456456+ }
457457+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
458458+ *size = sgp->vmsize;
459459+ return((uint8_t *)(sgp->vmaddr + slide));
460460+ }
461461+ }
462462+ sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
463463+ }
464464+ return(0);
465465+}
466466+467467+#endif /* defined(__LP64__) */
468468+469469+/*
470470+ * This routine returns the a pointer to the data for the named section in the
471471+ * named segment if it exist in the mach header passed to it. Also it returns
472472+ * the size of the section data indirectly through the pointer size. Otherwise
473473+ * it returns zero for the pointer and the size.
474474+ */
475475+char *
476476+getsectdatafromheader(
477477+struct mach_header *mhp,
478478+const char *segname,
479479+const char *sectname,
480480+uint32_t *size)
481481+{
482482+ const struct section *sp;
483483+484484+ sp = getsectbynamefromheader(mhp, segname, sectname);
485485+ if(sp == NULL){
486486+ *size = 0;
487487+ return(NULL);
488488+ }
489489+ *size = sp->size;
490490+ return((char *)((uintptr_t)(sp->addr)));
491491+}
492492+493493+/*
494494+ * This routine returns the a pointer to the data for the named section in the
495495+ * named segment if it exist in the 64-bit mach header passed to it. Also it
496496+ * returns the size of the section data indirectly through the pointer size.
497497+ * Otherwise it returns zero for the pointer and the size.
498498+ */
499499+char *
500500+getsectdatafromheader_64(
501501+struct mach_header_64 *mhp,
502502+const char *segname,
503503+const char *sectname,
504504+uint64_t *size)
505505+{
506506+ const struct section_64 *sp;
507507+508508+ sp = getsectbynamefromheader_64(mhp, segname, sectname);
509509+ if(sp == NULL){
510510+ *size = 0;
511511+ return(NULL);
512512+ }
513513+ *size = sp->size;
514514+ return((char *)((uintptr_t)(sp->addr)));
515515+}
516516+517517+#ifdef __DYNAMIC__
518518+/*
519519+ * This routine returns the a pointer to the data for the named section in the
520520+ * named segment if it exist in the named Framework. Also it returns the size
521521+ * of the section data indirectly through the pointer size. Otherwise it
522522+ * returns zero for the pointer and the size. The last component of the path
523523+ * of the Framework is passed as FrameworkName.
524524+ */
525525+void *
526526+getsectdatafromFramework(
527527+const char *FrameworkName,
528528+const char *segname,
529529+const char *sectname,
530530+unsigned long *size)
531531+{
532532+ uint32_t i, n;
533533+ uintptr_t vmaddr_slide;
534534+#ifndef __LP64__
535535+ struct mach_header *mh;
536536+ const struct section *s;
537537+#else /* defined(__LP64__) */
538538+ struct mach_header_64 *mh;
539539+ const struct section_64 *s;
540540+#endif /* defined(__LP64__) */
541541+ char *name, *p;
542542+543543+ n = _dyld_image_count();
544544+ for(i = 0; i < n ; i++){
545545+ name = _dyld_get_image_name(i);
546546+ p = strrchr(name, '/');
547547+ if(p != NULL && p[1] != '\0')
548548+ name = p + 1;
549549+ if(strcmp(name, FrameworkName) != 0)
550550+ continue;
551551+ mh = _dyld_get_image_header(i);
552552+ vmaddr_slide = _dyld_get_image_vmaddr_slide(i);
553553+#ifndef __LP64__
554554+ s = getsectbynamefromheader(mh, segname, sectname);
555555+#else /* defined(__LP64__) */
556556+ s = getsectbynamefromheader_64(mh, segname, sectname);
557557+#endif /* defined(__LP64__) */
558558+ if(s == NULL){
559559+ *size = 0;
560560+ return(NULL);
561561+ }
562562+ *size = s->size;
563563+ return((void *)(s->addr + vmaddr_slide));
564564+ }
565565+ *size = 0;
566566+ return(NULL);
567567+}
568568+#endif /* __DYNAMIC__ */
569569+#endif /* !defined(RLD) */
+119
src/libmacho/getsegbyname.c
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. Please obtain a copy of the License at
1010+ * http://www.opensource.apple.com/apsl/ and read it before using this
1111+ * file.
1212+ *
1313+ * The Original Code and all software distributed under the License are
1414+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1515+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
1616+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
1717+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
1818+ * Please see the License for the specific language governing rights and
1919+ * limitations under the License.
2020+ *
2121+ * @APPLE_LICENSE_HEADER_END@
2222+ */
2323+#include <mach-o/ldsyms.h>
2424+#include <string.h>
2525+#ifndef __OPENSTEP__
2626+2727+#ifndef RLD
2828+#include <crt_externs.h>
2929+#endif /* !defined(RLD) */
3030+3131+#else /* defined(__OPENSTEP__) */
3232+#ifdef __DYNAMIC__
3333+#include "mach-o/dyld.h" /* defines _dyld_lookup_and_bind() */
3434+#endif
3535+3636+#if !defined(__DYNAMIC__)
3737+#define DECLARE_VAR(var, type) \
3838+extern type var
3939+#define SETUP_VAR(var)
4040+#define USE_VAR(var) var
4141+#else
4242+#define STRINGIFY(a) # a
4343+#define DECLARE_VAR(var, type) \
4444+static type * var ## _pointer = 0
4545+#define SETUP_VAR(var) \
4646+if ( var ## _pointer == 0) { \
4747+ _dyld_lookup_and_bind( STRINGIFY(_ ## var), \
4848+ (uint32_t *) & var ## _pointer, 0); \
4949+}
5050+#define USE_VAR(var) (* var ## _pointer)
5151+#endif
5252+#endif /* __OPENSTEP__ */
5353+5454+/*
5555+ * This routine returns the segment_command structure for the named segment if
5656+ * it exist in the mach executible it is linked into. Otherwise it returns
5757+ * zero. It uses the link editor defined symbol _mh_execute_header and just
5858+ * looks through the load commands. Since these are mapped into the text
5959+ * segment they are read only and thus const.
6060+ */
6161+#ifndef __LP64__
6262+6363+const struct segment_command *
6464+getsegbyname(
6565+char *segname)
6666+{
6767+ struct segment_command *sgp;
6868+ uint32_t i;
6969+#ifndef RLD
7070+#ifndef __OPENSTEP__
7171+ struct mach_header *mhp = _NSGetMachExecuteHeader();
7272+#else /* defined(__OPENSTEP__) */
7373+ static struct mach_header *mhp = NULL;
7474+ DECLARE_VAR(_mh_execute_header, struct mach_header);
7575+ SETUP_VAR(_mh_execute_header);
7676+ mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header));
7777+#endif /* __OPENSTEP__ */
7878+#else /* defined(RLD) */
7979+ mhp = (struct mach_header *)(&_mh_execute_header);
8080+#endif /* defined(RLD) */
8181+8282+ sgp = (struct segment_command *)
8383+ ((char *)mhp + sizeof(struct mach_header));
8484+ for(i = 0; i < mhp->ncmds; i++){
8585+ if(sgp->cmd == LC_SEGMENT)
8686+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0)
8787+ return(sgp);
8888+ sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
8989+ }
9090+ return(NULL);
9191+}
9292+9393+#else /* defined(__LP64__) */
9494+9595+const struct segment_command_64 *
9696+getsegbyname(
9797+char *segname)
9898+{
9999+ struct mach_header_64 *mhp = NULL;
100100+ struct segment_command_64 *sgp;
101101+ uint32_t i;
102102+103103+#ifndef RLD
104104+ mhp = _NSGetMachExecuteHeader();
105105+#else /* defined(RLD) */
106106+ mhp = (struct mach_header_64 *)(&_mh_execute_header);
107107+#endif /* defined(RLD) */
108108+109109+ sgp = (struct segment_command_64 *)
110110+ ((char *)mhp + sizeof(struct mach_header_64));
111111+ for(i = 0; i < mhp->ncmds; i++){
112112+ if(sgp->cmd == LC_SEGMENT_64)
113113+ if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0)
114114+ return(sgp);
115115+ sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
116116+ }
117117+ return(NULL);
118118+}
119119+#endif /* defined(__LP64__) */
+121
src/libmacho/hppa_swap.c
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. Please obtain a copy of the License at
1010+ * http://www.opensource.apple.com/apsl/ and read it before using this
1111+ * file.
1212+ *
1313+ * The Original Code and all software distributed under the License are
1414+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1515+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
1616+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
1717+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
1818+ * Please see the License for the specific language governing rights and
1919+ * limitations under the License.
2020+ *
2121+ * @APPLE_LICENSE_HEADER_END@
2222+ */
2323+#ifndef RLD
2424+#import <mach-o/hppa/swap.h>
2525+2626+void
2727+swap_hppa_integer_thread_state(
2828+struct hp_pa_integer_thread_state *regs,
2929+enum NXByteOrder target_byte_order)
3030+{
3131+ regs->ts_gr1 = OSSwapInt32(regs->ts_gr1);
3232+ regs->ts_gr2 = OSSwapInt32(regs->ts_gr2);
3333+ regs->ts_gr3 = OSSwapInt32(regs->ts_gr3);
3434+ regs->ts_gr4 = OSSwapInt32(regs->ts_gr4);
3535+ regs->ts_gr5 = OSSwapInt32(regs->ts_gr5);
3636+ regs->ts_gr6 = OSSwapInt32(regs->ts_gr6);
3737+ regs->ts_gr7 = OSSwapInt32(regs->ts_gr7);
3838+ regs->ts_gr8 = OSSwapInt32(regs->ts_gr8);
3939+ regs->ts_gr9 = OSSwapInt32(regs->ts_gr9);
4040+ regs->ts_gr10 = OSSwapInt32(regs->ts_gr10);
4141+ regs->ts_gr11 = OSSwapInt32(regs->ts_gr11);
4242+ regs->ts_gr12 = OSSwapInt32(regs->ts_gr12);
4343+ regs->ts_gr13 = OSSwapInt32(regs->ts_gr13);
4444+ regs->ts_gr14 = OSSwapInt32(regs->ts_gr14);
4545+ regs->ts_gr15 = OSSwapInt32(regs->ts_gr15);
4646+ regs->ts_gr16 = OSSwapInt32(regs->ts_gr16);
4747+ regs->ts_gr17 = OSSwapInt32(regs->ts_gr17);
4848+ regs->ts_gr18 = OSSwapInt32(regs->ts_gr18);
4949+ regs->ts_gr19 = OSSwapInt32(regs->ts_gr19);
5050+ regs->ts_gr20 = OSSwapInt32(regs->ts_gr20);
5151+ regs->ts_gr21 = OSSwapInt32(regs->ts_gr21);
5252+ regs->ts_gr22 = OSSwapInt32(regs->ts_gr22);
5353+ regs->ts_gr23 = OSSwapInt32(regs->ts_gr23);
5454+ regs->ts_gr24 = OSSwapInt32(regs->ts_gr24);
5555+ regs->ts_gr25 = OSSwapInt32(regs->ts_gr25);
5656+ regs->ts_gr26 = OSSwapInt32(regs->ts_gr26);
5757+ regs->ts_gr27 = OSSwapInt32(regs->ts_gr27);
5858+ regs->ts_gr28 = OSSwapInt32(regs->ts_gr28);
5959+ regs->ts_gr29 = OSSwapInt32(regs->ts_gr29);
6060+ regs->ts_gr30 = OSSwapInt32(regs->ts_gr30);
6161+ regs->ts_gr31 = OSSwapInt32(regs->ts_gr31);
6262+ regs->ts_sr0 = OSSwapInt32(regs->ts_sr0);
6363+ regs->ts_sr1 = OSSwapInt32(regs->ts_sr1);
6464+ regs->ts_sr2 = OSSwapInt32(regs->ts_sr2);
6565+ regs->ts_sr3 = OSSwapInt32(regs->ts_sr3);
6666+ regs->ts_sar = OSSwapInt32(regs->ts_sar);
6767+}
6868+6969+void swap_hppa_frame_thread_state(
7070+struct hp_pa_frame_thread_state *frame,
7171+enum NXByteOrder target_byte_order)
7272+{
7373+ frame->ts_pcsq_front = OSSwapInt32(frame->ts_pcsq_front);
7474+ frame->ts_pcsq_back = OSSwapInt32(frame->ts_pcsq_back);
7575+ frame->ts_pcoq_front = OSSwapInt32(frame->ts_pcoq_front);
7676+ frame->ts_pcoq_back = OSSwapInt32(frame->ts_pcoq_back);
7777+ frame->ts_psw = OSSwapInt32(frame->ts_psw);
7878+ frame->ts_unaligned_faults = OSSwapInt32(frame->ts_unaligned_faults);
7979+ frame->ts_fault_address = OSSwapInt32(frame->ts_fault_address);
8080+ frame->ts_step_range_start = OSSwapInt32(frame->ts_step_range_start);
8181+ frame->ts_step_range_stop = OSSwapInt32(frame->ts_step_range_stop);
8282+}
8383+8484+void swap_hppa_fp_thread_state(
8585+struct hp_pa_fp_thread_state *fp,
8686+enum NXByteOrder target_byte_order)
8787+{
8888+ fp->ts_fp0 = OSSwapInt64(fp->ts_fp0);
8989+ fp->ts_fp1 = OSSwapInt64(fp->ts_fp1);
9090+ fp->ts_fp2 = OSSwapInt64(fp->ts_fp2);
9191+ fp->ts_fp3 = OSSwapInt64(fp->ts_fp3);
9292+ fp->ts_fp4 = OSSwapInt64(fp->ts_fp4);
9393+ fp->ts_fp5 = OSSwapInt64(fp->ts_fp5);
9494+ fp->ts_fp6 = OSSwapInt64(fp->ts_fp6);
9595+ fp->ts_fp7 = OSSwapInt64(fp->ts_fp7);
9696+ fp->ts_fp8 = OSSwapInt64(fp->ts_fp8);
9797+ fp->ts_fp9 = OSSwapInt64(fp->ts_fp9);
9898+ fp->ts_fp10 = OSSwapInt64(fp->ts_fp10);
9999+ fp->ts_fp11 = OSSwapInt64(fp->ts_fp11);
100100+ fp->ts_fp12 = OSSwapInt64(fp->ts_fp12);
101101+ fp->ts_fp13 = OSSwapInt64(fp->ts_fp13);
102102+ fp->ts_fp14 = OSSwapInt64(fp->ts_fp14);
103103+ fp->ts_fp15 = OSSwapInt64(fp->ts_fp15);
104104+ fp->ts_fp16 = OSSwapInt64(fp->ts_fp16);
105105+ fp->ts_fp17 = OSSwapInt64(fp->ts_fp17);
106106+ fp->ts_fp18 = OSSwapInt64(fp->ts_fp18);
107107+ fp->ts_fp19 = OSSwapInt64(fp->ts_fp19);
108108+ fp->ts_fp20 = OSSwapInt64(fp->ts_fp20);
109109+ fp->ts_fp21 = OSSwapInt64(fp->ts_fp21);
110110+ fp->ts_fp22 = OSSwapInt64(fp->ts_fp22);
111111+ fp->ts_fp23 = OSSwapInt64(fp->ts_fp23);
112112+ fp->ts_fp24 = OSSwapInt64(fp->ts_fp24);
113113+ fp->ts_fp25 = OSSwapInt64(fp->ts_fp25);
114114+ fp->ts_fp26 = OSSwapInt64(fp->ts_fp26);
115115+ fp->ts_fp27 = OSSwapInt64(fp->ts_fp27);
116116+ fp->ts_fp28 = OSSwapInt64(fp->ts_fp28);
117117+ fp->ts_fp29 = OSSwapInt64(fp->ts_fp29);
118118+ fp->ts_fp30 = OSSwapInt64(fp->ts_fp30);
119119+ fp->ts_fp31 = OSSwapInt64(fp->ts_fp31);
120120+}
121121+#endif /* !defined(RLD) */
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. Please obtain a copy of the License at
1010+ * http://www.opensource.apple.com/apsl/ and read it before using this
1111+ * file.
1212+ *
1313+ * The Original Code and all software distributed under the License are
1414+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1515+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
1616+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
1717+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
1818+ * Please see the License for the specific language governing rights and
1919+ * limitations under the License.
2020+ *
2121+ * @APPLE_LICENSE_HEADER_END@
2222+ */
2323+#ifndef RLD
2424+#import <mach-o/i860/swap.h>
2525+2626+void
2727+swap_i860_thread_state_regs(
2828+struct i860_thread_state_regs *cpu,
2929+enum NXByteOrder target_byte_sex)
3030+{
3131+ int i;
3232+3333+ for(i = 0; i < 31; i++)
3434+ cpu->ireg[i] = OSSwapInt32(cpu->ireg[i]);
3535+ for(i = 0; i < 30; i++)
3636+ cpu->freg[i] = OSSwapInt32(cpu->freg[i]);
3737+ cpu->psr = OSSwapInt32(cpu->psr);
3838+ cpu->epsr = OSSwapInt32(cpu->epsr);
3939+ cpu->db = OSSwapInt32(cpu->db);
4040+ cpu->pc = OSSwapInt32(cpu->pc);
4141+ cpu->_padding_ = OSSwapInt32(cpu->_padding_);
4242+ cpu->Mres3 = OSSwapInt64(cpu->Mres3);
4343+ cpu->Ares3 = OSSwapInt64(cpu->Ares3);
4444+ cpu->Mres2 = OSSwapInt64(cpu->Mres2);
4545+ cpu->Ares2 = OSSwapInt64(cpu->Ares2);
4646+ cpu->Mres1 = OSSwapInt64(cpu->Mres1);
4747+ cpu->Ares1 = OSSwapInt64(cpu->Ares1);
4848+ cpu->Ires1 = OSSwapInt64(cpu->Ires1);
4949+ cpu->Lres3m = OSSwapInt64(cpu->Lres3m);
5050+ cpu->Lres2m = OSSwapInt64(cpu->Lres2m);
5151+ cpu->Lres1m = OSSwapInt64(cpu->Lres1m);
5252+ cpu->KR = OSSwapInt64(cpu->KR);
5353+ cpu->KI = OSSwapInt64(cpu->KI);
5454+ cpu->T = OSSwapInt64(cpu->T);
5555+ cpu->Fsr3 = OSSwapInt32(cpu->Fsr3);
5656+ cpu->Fsr2 = OSSwapInt32(cpu->Fsr2);
5757+ cpu->Fsr1 = OSSwapInt32(cpu->Fsr1);
5858+ cpu->Mergelo32 = OSSwapInt32(cpu->Mergelo32);
5959+ cpu->Mergehi32 = OSSwapInt32(cpu->Mergehi32);
6060+}
6161+#endif /* !defined(RLD) */
+68
src/libmacho/m68k_swap.c
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. Please obtain a copy of the License at
1010+ * http://www.opensource.apple.com/apsl/ and read it before using this
1111+ * file.
1212+ *
1313+ * The Original Code and all software distributed under the License are
1414+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1515+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
1616+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
1717+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
1818+ * Please see the License for the specific language governing rights and
1919+ * limitations under the License.
2020+ *
2121+ * @APPLE_LICENSE_HEADER_END@
2222+ */
2323+#ifndef RLD
2424+#import <mach-o/m68k/swap.h>
2525+2626+void
2727+swap_m68k_thread_state_regs(
2828+struct m68k_thread_state_regs *cpu,
2929+enum NXByteOrder target_byte_sex)
3030+{
3131+ uint32_t i;
3232+3333+ for(i = 0; i < 8; i++)
3434+ cpu->dreg[i] = OSSwapInt32(cpu->dreg[i]);
3535+ for(i = 0; i < 8; i++)
3636+ cpu->areg[i] = OSSwapInt32(cpu->areg[i]);
3737+ cpu->pad0 = OSSwapInt16(cpu->pad0);
3838+ cpu->sr = OSSwapInt16(cpu->sr);
3939+ cpu->pc = OSSwapInt32(cpu->pc);
4040+}
4141+4242+void
4343+swap_m68k_thread_state_68882(
4444+struct m68k_thread_state_68882 *fpu,
4545+enum NXByteOrder target_byte_sex)
4646+{
4747+ uint32_t i, tmp;
4848+4949+ for(i = 0; i < 8; i++){
5050+ tmp = OSSwapInt32(fpu->regs[i].fp[0]);
5151+ fpu->regs[i].fp[1] = OSSwapInt32(fpu->regs[i].fp[1]);
5252+ fpu->regs[i].fp[0] = OSSwapInt32(fpu->regs[i].fp[2]);
5353+ fpu->regs[i].fp[2] = tmp;
5454+ }
5555+ fpu->cr = OSSwapInt32(fpu->cr);
5656+ fpu->sr = OSSwapInt32(fpu->sr);
5757+ fpu->iar = OSSwapInt32(fpu->iar);
5858+ fpu->state = OSSwapInt32(fpu->state);
5959+}
6060+6161+void
6262+swap_m68k_thread_state_user_reg(
6363+struct m68k_thread_state_user_reg *user_reg,
6464+enum NXByteOrder target_byte_sex)
6565+{
6666+ user_reg->user_reg = OSSwapInt32(user_reg->user_reg);
6767+}
6868+#endif /* !defined(RLD) */
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. The rights granted to you under the License
1010+ * may not be used to create, or enable the creation or redistribution of,
1111+ * unlawful or unlicensed copies of an Apple operating system, or to
1212+ * circumvent, violate, or enable the circumvention or violation of, any
1313+ * terms of an Apple operating system software license agreement.
1414+ *
1515+ * Please obtain a copy of the License at
1616+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
1717+ *
1818+ * The Original Code and all software distributed under the License are
1919+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
2020+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
2121+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2222+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
2323+ * Please see the License for the specific language governing rights and
2424+ * limitations under the License.
2525+ *
2626+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
2727+ */
2828+/*
2929+ * File: slot_name.c
3030+ * Author: Avadis Tevanian, Jr.
3131+ *
3232+ * Copyright (C) 1987, Avadis Tevanian, Jr.
3333+ *
3434+ * Convert machine slot values to human readable strings.
3535+ *
3636+ * HISTORY
3737+ * 26-Jan-88 Mary Thompson (mrt) at Carnegie Mellon
3838+ * added case for CUP_SUBTYPE_RT_APC
3939+ *
4040+ * 28-Feb-87 Avadis Tevanian (avie) at Carnegie-Mellon University
4141+ * Created.
4242+ *
4343+ */
4444+4545+#include <mach-o/arch.h>
4646+#include <stddef.h>
4747+4848+/*
4949+ * Convert the specified cpu_type/cpu_subtype pair to their
5050+ * human readable form.
5151+ */
5252+void slot_name(cpu_type, cpu_subtype, cpu_name, cpu_subname)
5353+ cpu_type_t cpu_type;
5454+ cpu_subtype_t cpu_subtype;
5555+ char **cpu_name, **cpu_subname;
5656+{
5757+ register char *name = "Unknown CPU";
5858+ register char *subname = "";
5959+ const NXArchInfo *ai = NXGetArchInfoFromCpuType(cpu_type, cpu_subtype);
6060+ if (ai != NULL) {
6161+ name = (char *)ai->name;
6262+ subname = (char *)ai->description;
6363+ }
6464+ *cpu_name = name;
6565+ *cpu_subname = subname;
6666+}
+178
src/libmacho/sparc_swap.c
···11+/*
22+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
33+ *
44+ * @APPLE_LICENSE_HEADER_START@
55+ *
66+ * This file contains Original Code and/or Modifications of Original Code
77+ * as defined in and that are subject to the Apple Public Source License
88+ * Version 2.0 (the 'License'). You may not use this file except in
99+ * compliance with the License. Please obtain a copy of the License at
1010+ * http://www.opensource.apple.com/apsl/ and read it before using this
1111+ * file.
1212+ *
1313+ * The Original Code and all software distributed under the License are
1414+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1515+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
1616+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
1717+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
1818+ * Please see the License for the specific language governing rights and
1919+ * limitations under the License.
2020+ *
2121+ * @APPLE_LICENSE_HEADER_END@
2222+ */
2323+#ifndef RLD
2424+#include <string.h>
2525+#import <mach-o/sparc/swap.h>
2626+#import <architecture/nrw/reg_help.h>
2727+2828+void
2929+swap_sparc_thread_state_regs(
3030+struct sparc_thread_state_regs *cpu,
3131+enum NXByteOrder target_byte_sex)
3232+{
3333+ struct swapped_psr {
3434+ union {
3535+ struct {
3636+ unsigned int
3737+ cwp:BITS_WIDTH(4,0),
3838+ et:BIT_WIDTH(5),
3939+ ps:BIT_WIDTH(6),
4040+ s:BIT_WIDTH(7),
4141+ pil:BITS_WIDTH(11,8),
4242+ ef:BIT_WIDTH(12),
4343+ ec:BIT_WIDTH(13),
4444+ reserved:BITS_WIDTH(19,14),
4545+ icc:BITS_WIDTH(23,20),
4646+ ver:BITS_WIDTH(27,24),
4747+ impl:BITS_WIDTH(31,28);
4848+ } fields;
4949+ unsigned int word;
5050+ } u;
5151+ } spsr;
5252+ struct p_status *pr_status;
5353+ enum NXByteOrder host_byte_sex;
5454+5555+ host_byte_sex = NXHostByteOrder();
5656+5757+ cpu->regs.r_pc = OSSwapInt32(cpu->regs.r_pc);
5858+ cpu->regs.r_npc = OSSwapInt32(cpu->regs.r_npc);
5959+ cpu->regs.r_y = OSSwapInt32(cpu->regs.r_y);
6060+ cpu->regs.r_g1 = OSSwapInt32(cpu->regs.r_g1);
6161+ cpu->regs.r_g2 = OSSwapInt32(cpu->regs.r_g2);
6262+ cpu->regs.r_g3 = OSSwapInt32(cpu->regs.r_g3);
6363+ cpu->regs.r_g4 = OSSwapInt32(cpu->regs.r_g4);
6464+ cpu->regs.r_g5 = OSSwapInt32(cpu->regs.r_g5);
6565+ cpu->regs.r_g6 = OSSwapInt32(cpu->regs.r_g6);
6666+ cpu->regs.r_g7 = OSSwapInt32(cpu->regs.r_g7);
6767+ cpu->regs.r_o0 = OSSwapInt32(cpu->regs.r_o0);
6868+ cpu->regs.r_o1 = OSSwapInt32(cpu->regs.r_o1);
6969+ cpu->regs.r_o2 = OSSwapInt32(cpu->regs.r_o2);
7070+ cpu->regs.r_o3 = OSSwapInt32(cpu->regs.r_o3);
7171+ cpu->regs.r_o4 = OSSwapInt32(cpu->regs.r_o4);
7272+ cpu->regs.r_o5 = OSSwapInt32(cpu->regs.r_o5);
7373+ cpu->regs.r_o6 = OSSwapInt32(cpu->regs.r_o6);
7474+ cpu->regs.r_o7 = OSSwapInt32(cpu->regs.r_o7);
7575+7676+ pr_status = (struct p_status *) &(cpu->regs.r_psr);
7777+ if(target_byte_sex == host_byte_sex){
7878+ memcpy(&spsr, &(cpu->regs.r_psr), sizeof(struct swapped_psr));
7979+ spsr.u.word = OSSwapInt32(spsr.u.word);
8080+ pr_status->PSRREG.psr_bits.cwp = spsr.u.fields.cwp;
8181+ pr_status->PSRREG.psr_bits.ps = spsr.u.fields.ps;
8282+ pr_status->PSRREG.psr_bits.s = spsr.u.fields.s;
8383+ pr_status->PSRREG.psr_bits.pil = spsr.u.fields.pil;
8484+ pr_status->PSRREG.psr_bits.ef = spsr.u.fields.ef;
8585+ pr_status->PSRREG.psr_bits.ec = spsr.u.fields.ec;
8686+ pr_status->PSRREG.psr_bits.reserved = spsr.u.fields.reserved;
8787+ pr_status->PSRREG.psr_bits.icc = spsr.u.fields.icc;
8888+ pr_status->PSRREG.psr_bits.et = spsr.u.fields.ver;
8989+ pr_status->PSRREG.psr_bits.impl = spsr.u.fields.impl;
9090+ }
9191+ else{
9292+ spsr.u.fields.cwp = pr_status->PSRREG.psr_bits.cwp;
9393+ spsr.u.fields.ps = pr_status->PSRREG.psr_bits.ps;
9494+ spsr.u.fields.s = pr_status->PSRREG.psr_bits.s;
9595+ spsr.u.fields.pil = pr_status->PSRREG.psr_bits.pil;
9696+ spsr.u.fields.ef = pr_status->PSRREG.psr_bits.ef;
9797+ spsr.u.fields.ec = pr_status->PSRREG.psr_bits.ec;
9898+ spsr.u.fields.reserved = pr_status->PSRREG.psr_bits.reserved;
9999+ spsr.u.fields.icc = pr_status->PSRREG.psr_bits.icc;
100100+ spsr.u.fields.ver = pr_status->PSRREG.psr_bits.et;
101101+ spsr.u.fields.impl = pr_status->PSRREG.psr_bits.impl;
102102+ spsr.u.word = OSSwapInt32(spsr.u.word);
103103+ memcpy(&(cpu->regs.r_psr), &spsr, sizeof(struct swapped_psr));
104104+ }
105105+}
106106+107107+void
108108+swap_sparc_thread_state_fpu(
109109+struct sparc_thread_state_fpu *fpu,
110110+enum NXByteOrder target_byte_sex)
111111+{
112112+ struct swapped_fsr {
113113+ union {
114114+ struct {
115115+ unsigned int
116116+ cexc:BITS_WIDTH(4,0),
117117+ aexc:BITS_WIDTH(9,5),
118118+ fcc:BITS_WIDTH(11,10),
119119+ pr:BIT_WIDTH(12),
120120+ qne:BIT_WIDTH(13),
121121+ ftt:BITS_WIDTH(16,14),
122122+ res:BITS_WIDTH(22,17),
123123+ tem:BITS_WIDTH(27,23),
124124+ rp:BITS_WIDTH(29,28),
125125+ rd:BITS_WIDTH(31,30);
126126+ } fields;
127127+ unsigned int word;
128128+ } u;
129129+ } sfsr;
130130+ uint32_t i;
131131+ struct f_status *fpu_status;
132132+ enum NXByteOrder host_byte_sex;
133133+134134+ host_byte_sex = NXHostByteOrder();
135135+136136+137137+ /* floating point registers */
138138+ for(i = 0; i < 16; i++) /* 16 doubles */
139139+ fpu->fpu.fpu_fr.Fpu_dregs[i] =
140140+ OSSwapInt64(fpu->fpu.fpu_fr.Fpu_dregs[i]);
141141+142142+ fpu->fpu.Fpu_q[0].FQu.whole = OSSwapInt64(fpu->fpu.Fpu_q[0].FQu.whole);
143143+ fpu->fpu.Fpu_q[1].FQu.whole = OSSwapInt64(fpu->fpu.Fpu_q[1].FQu.whole);
144144+ fpu->fpu.Fpu_flags = OSSwapInt32(fpu->fpu.Fpu_flags);
145145+ fpu->fpu.Fpu_extra = OSSwapInt32(fpu->fpu.Fpu_extra);
146146+ fpu->fpu.Fpu_qcnt = OSSwapInt32(fpu->fpu.Fpu_qcnt);
147147+148148+ fpu_status = (struct f_status *) &(fpu->fpu.Fpu_fsr);
149149+ if(target_byte_sex == host_byte_sex){
150150+ memcpy(&sfsr, &(fpu->fpu.Fpu_fsr), sizeof(unsigned int));
151151+ sfsr.u.word = OSSwapInt32(sfsr.u.word);
152152+ fpu_status->FPUREG.Fpu_fsr_bits.rd = sfsr.u.fields.rd;
153153+ fpu_status->FPUREG.Fpu_fsr_bits.rp = sfsr.u.fields.rp;
154154+ fpu_status->FPUREG.Fpu_fsr_bits.tem = sfsr.u.fields.tem;
155155+ fpu_status->FPUREG.Fpu_fsr_bits.res = sfsr.u.fields.res;
156156+ fpu_status->FPUREG.Fpu_fsr_bits.ftt = sfsr.u.fields.ftt;
157157+ fpu_status->FPUREG.Fpu_fsr_bits.qne = sfsr.u.fields.qne;
158158+ fpu_status->FPUREG.Fpu_fsr_bits.pr = sfsr.u.fields.pr;
159159+ fpu_status->FPUREG.Fpu_fsr_bits.fcc = sfsr.u.fields.fcc;
160160+ fpu_status->FPUREG.Fpu_fsr_bits.aexc = sfsr.u.fields.aexc;
161161+ fpu_status->FPUREG.Fpu_fsr_bits.cexc = sfsr.u.fields.cexc;
162162+ }
163163+ else{
164164+ sfsr.u.fields.rd = fpu_status->FPUREG.Fpu_fsr_bits.rd;
165165+ sfsr.u.fields.rp = fpu_status->FPUREG.Fpu_fsr_bits.rp;
166166+ sfsr.u.fields.tem = fpu_status->FPUREG.Fpu_fsr_bits.tem;
167167+ sfsr.u.fields.res = fpu_status->FPUREG.Fpu_fsr_bits.res;
168168+ sfsr.u.fields.ftt = fpu_status->FPUREG.Fpu_fsr_bits.ftt;
169169+ sfsr.u.fields.qne = fpu_status->FPUREG.Fpu_fsr_bits.qne;
170170+ sfsr.u.fields.pr = fpu_status->FPUREG.Fpu_fsr_bits.pr;
171171+ sfsr.u.fields.fcc = fpu_status->FPUREG.Fpu_fsr_bits.fcc;
172172+ sfsr.u.fields.aexc = fpu_status->FPUREG.Fpu_fsr_bits.aexc;
173173+ sfsr.u.fields.cexc = fpu_status->FPUREG.Fpu_fsr_bits.cexc;
174174+ sfsr.u.word = OSSwapInt32(sfsr.u.word);
175175+ memcpy(&(fpu->fpu.Fpu_fsr), &sfsr, sizeof(struct swapped_fsr));
176176+ }
177177+}
178178+#endif /* !defined(RLD) */
+41
src/libmacho/stuff/openstep_mach.h
···11+/*
22+ * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
33+ *
44+ * Redistribution and use in source and binary forms, with or without
55+ * modification, are permitted provided that the following conditions
66+ * are met:
77+ * 1. Redistributions of source code must retain the above copyright
88+ * notice, this list of conditions and the following disclaimer.
99+ * 2. Redistributions in binary form must reproduce the above copyright
1010+ * notice, this list of conditions and the following disclaimer in the
1111+ * documentation and/or other materials provided with the distribution.
1212+ * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
1313+ * its contributors may be used to endorse or promote products derived
1414+ * from this software without specific prior written permission.
1515+ *
1616+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
1717+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1818+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1919+ * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
2020+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2121+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2222+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2323+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2424+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2525+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626+ * POSSIBILITY OF SUCH DAMAGE.
2727+ */
2828+#ifdef __OPENSTEP__
2929+/*
3030+ * This file is used to allow cctools to be compiled for Openstep now that
3131+ * the code has been changed to use Mach 3.0 names (which work for MacOS X and
3232+ * Rhapsody but not for Openstep).
3333+ */
3434+#define mach_task_self task_self
3535+#define mach_task_self_ task_self_
3636+#define mach_host_self host_self
3737+#define mach_thread_self thread_self
3838+#define mach_port_allocate port_allocate
3939+#define mach_port_deallocate port_deallocate
4040+#define mach_port_names port_names
4141+#endif /* __OPENSTEP__ */