|
|
1.1 root 1: /*
2: * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3: *
4: * @APPLE_LICENSE_HEADER_START@
5: *
6: * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7: * Reserved. This file contains Original Code and/or Modifications of
8: * Original Code as defined in and that are subject to the Apple Public
9: * Source License Version 1.0 (the 'License'). You may not use this file
10: * except in compliance with the License. Please obtain a copy of the
11: * License at http://www.apple.com/publicsource and read it before using
12: * this file.
13: *
14: * The Original Code and all software distributed under the License are
15: * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19: * License for the specific language governing rights and limitations
20: * under the License."
21: *
22: * @APPLE_LICENSE_HEADER_END@
23: */
24: #include <mach-o/ldsyms.h>
25: #include <string.h>
26:
27: /*
28: * This routine returns the section structure for the named section in the
29: * named segment for the mach_header pointer passed to it if it exist.
30: * Otherwise it returns zero.
31: */
32: const struct section *
33: getsectbynamefromheader(
34: struct mach_header *mhp,
35: char *segname,
36: char *sectname)
37: {
38: struct segment_command *sgp;
39: struct section *sp;
40: long i, j;
41:
42: sgp = (struct segment_command *)
43: ((char *)mhp + sizeof(struct mach_header));
44: for(i = 0; i < mhp->ncmds; i++){
45: if(sgp->cmd == LC_SEGMENT)
46: if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
47: mhp->filetype == MH_OBJECT){
48: sp = (struct section *)((char *)sgp +
49: sizeof(struct segment_command));
50: for(j = 0; j < sgp->nsects; j++){
51: if(strncmp(sp->sectname, sectname,
52: sizeof(sp->sectname)) == 0 &&
53: strncmp(sp->segname, segname,
54: sizeof(sp->segname)) == 0)
55: return(sp);
56: sp = (struct section *)((char *)sp +
57: sizeof(struct section));
58: }
59: }
60: sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
61: }
62: return((struct section *)0);
63: }
64:
65:
66: /*
67: * This routine returns the section structure for the named section in the
68: * named segment for the mach_header pointer passed to it if it exist.
69: * Otherwise it returns zero. If fSwap == YES (the mach header has been
70: * swapped to the endiannes of the current machine, but the segments and
71: * sections are different) then the segment and sections are swapped.
72: */
73: const struct section *
74: getsectbynamefromheaderwithswap(
75: struct mach_header *mhp,
76: const char *segname,
77: const char *sectname,
78: int fSwap)
79: {
80: struct segment_command *sgp;
81: struct section *sp;
82: long i, j;
83:
84: sgp = (struct segment_command *)
85: ((char *)mhp + sizeof(struct mach_header));
86: for(i = 0; i < mhp->ncmds; i++){
87: if(sgp->cmd == (fSwap ? NXSwapLong(LC_SEGMENT) : LC_SEGMENT)) {
88:
89: if (fSwap) {
90: #ifdef __LITTLE_ENDIAN__
91: swap_segment_command(sgp, NX_BigEndian);
92: #else
93: swap_segment_command(sgp, NX_LittleEndian);
94: #endif __LITTLE_ENDIAN__
95: }
96:
97: if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
98: mhp->filetype == MH_OBJECT){
99: sp = (struct section *)((char *)sgp +
100: sizeof(struct segment_command));
101:
102: if (fSwap) {
103: #ifdef __LITTLE_ENDIAN__
104: swap_section(sp, sgp->nsects, NX_BigEndian);
105: #else
106: swap_section(sp, sgp->nsects, NX_LittleEndian);
107: #endif __LITTLE_ENDIAN__
108: }
109:
110: for(j = 0; j < sgp->nsects; j++){
111: if(strncmp(sp->sectname, sectname,
112: sizeof(sp->sectname)) == 0 &&
113: strncmp(sp->segname, segname,
114: sizeof(sp->segname)) == 0)
115: return(sp);
116: sp = (struct section *)((char *)sp +
117: sizeof(struct section));
118: }
119: }
120: sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
121: } else {
122: sgp = (struct segment_command *)((char *)sgp +
123: (fSwap ? NXSwapLong(sgp->cmdsize) : sgp->cmdsize));
124: }
125: }
126: return((struct section *)0);
127: }
128:
129:
130: /*
131: * This routine returns the a pointer the section structure of the named
132: * section in the named segment if it exist in the mach executable it is
133: * linked into. Otherwise it returns zero.
134: */
135: const struct section *
136: getsectbyname(
137: char *segname,
138: char *sectname)
139: {
140: return(getsectbynamefromheader(
141: (struct mach_header *)&_mh_execute_header, segname, sectname));
142: }
143:
144: /*
145: * This routine returns the a pointer to the data for the named section in the
146: * named segment if it exist in the mach executable it is linked into. Also
147: * it returns the size of the section data indirectly through the pointer size.
148: * Otherwise it returns zero for the pointer and the size.
149: */
150: char *
151: getsectdata(
152: char *segname,
153: char *sectname,
154: int *size)
155: {
156: const struct section *sp;
157:
158: sp = getsectbyname(segname, sectname);
159: if(sp == (struct section *)0){
160: *size = 0;
161: return((char *)0);
162: }
163: *size = sp->size;
164: return((char *)(sp->addr));
165: }
166:
167: /*
168: * This routine returns the a pointer to the data for the named section in the
169: * named segment if it exist in the mach header passed to it. Also it returns
170: * the size of the section data indirectly through the pointer size. Otherwise
171: * it returns zero for the pointer and the size.
172: */
173: char *
174: getsectdatafromheader(
175: struct mach_header *mhp,
176: char *segname,
177: char *sectname,
178: int *size)
179: {
180: const struct section *sp;
181:
182: sp = getsectbynamefromheader(mhp, segname, sectname);
183: if(sp == (struct section *)0){
184: *size = 0;
185: return((char *)0);
186: }
187: *size = sp->size;
188: return((char *)(sp->addr));
189: }
190:
191: /*
192: * This routine returns the a pointer to the data for the named section in the
193: * named segment if it exist in the named shared library the executable it is
194: * linked into. Also it returns the size of the section data indirectly
195: * through the pointer size. Otherwise it returns zero for the pointer and
196: * the size. The last component of the shared libraries name must be of the
197: * form libx.X.shlib. Where the library name passed to this routine would
198: * be libx and x is any string. In libx.X.shlib, X is any single character.
199: */
200: char *
201: getsectdatafromlib(
202: char *libname,
203: char *segname,
204: char *sectname,
205: int *size)
206: {
207: struct mach_header *mhp;
208: struct fvmlib_command *flp;
209: const struct section *sp;
210: long i, libnamelen, namelen;
211: char *name, *p;
212:
213: libnamelen = strlen(libname);
214: mhp = (struct mach_header *)(&_mh_execute_header);
215: flp = (struct fvmlib_command *)
216: ((char *)mhp + sizeof(struct mach_header));
217: for(i = 0; i < mhp->ncmds; i++){
218: if(flp->cmd == LC_LOADFVMLIB){
219: name = (char *)flp + flp->fvmlib.name.offset;
220: p = strrchr(name, '/');
221: if(p == NULL)
222: p = name;
223: else
224: p++;
225: namelen = strlen(p);
226: if(namelen >= libnamelen + sizeof(".X.shlib") - 1 &&
227: strncmp(p, libname, libnamelen) == 0 &&
228: *(p + libnamelen) == '.' &&
229: strcmp(p + libnamelen + 2, ".shlib") == 0){
230: sp = getsectbynamefromheader(
231: (struct mach_header *)flp->fvmlib.header_addr,
232: segname, sectname);
233: if(sp == (struct section *)0){
234: *size = 0;
235: return((char *)0);
236: }
237: *size = sp->size;
238: return((char *)(sp->addr));
239: }
240: }
241: flp = (struct fvmlib_command *)((char *)flp + flp->cmdsize);
242: }
243:
244: *size = 0;
245: return((char *)0);
246: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.