Blame


1 9ed434d7 2006-08-09 matthias> /*-
2 7d2c0d24 2012-09-11 matthias * Copyright (c) 2006 Matthias Schmidt <xhr @ giessen.ccc.de>
3 9ed434d7 2006-08-09 matthias> *
4 9ed434d7 2006-08-09 matthias> * Redistribution and use in source and binary forms, with or without
5 9ed434d7 2006-08-09 matthias> * modification, are permitted provided that the following conditions
6 9ed434d7 2006-08-09 matthias> * are met:
7 9ed434d7 2006-08-09 matthias> *
8 9ed434d7 2006-08-09 matthias> * 1. Redistributions of source code must retain the above copyright
9 9ed434d7 2006-08-09 matthias> * notice, this list of conditions and the following disclaimer.
10 9ed434d7 2006-08-09 matthias> * 2. Redistributions in binary form must reproduce the above copyright
11 9ed434d7 2006-08-09 matthias> * notice, this list of conditions and the following disclaimer in the
12 9ed434d7 2006-08-09 matthias> * documentation and/or other materials provided with the distribution.
13 9ed434d7 2006-08-09 matthias> *
14 9ed434d7 2006-08-09 matthias> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
15 9ed434d7 2006-08-09 matthias> * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 9ed434d7 2006-08-09 matthias> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 9ed434d7 2006-08-09 matthias> * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
18 9ed434d7 2006-08-09 matthias> * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 9ed434d7 2006-08-09 matthias> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 9ed434d7 2006-08-09 matthias> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 9ed434d7 2006-08-09 matthias> * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 9ed434d7 2006-08-09 matthias> * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 9ed434d7 2006-08-09 matthias> * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 9ed434d7 2006-08-09 matthias> * THE POSSIBILITY OF SUCH DAMAGE.
25 9ed434d7 2006-08-09 matthias> */
26 9ed434d7 2006-08-09 matthias>
27 acb3e90c 2006-09-02 matthias> /* $Id: util.c,v 1.20 2006/09/02 01:29:51 matthias Exp $ */
28 9ed434d7 2006-08-09 matthias>
29 9ed434d7 2006-08-09 matthias> #include "dermob.h"
30 03cf29b4 2006-08-10 matthias> #include "mach.h"
31 9d60efcf 2006-08-11 matthias> #include "defs.h"
32 7bf77b98 2006-08-15 matthias> #include "list.h"
33 9ed434d7 2006-08-09 matthias>
34 3fefb827 2006-08-10 matthias> #define swap_bo(i) \
35 3fefb827 2006-08-10 matthias> (((i & 0xFF000000) >> 24) | \
36 3fefb827 2006-08-10 matthias> ((i & 0x00FF0000) >> 8) | \
37 3fefb827 2006-08-10 matthias> ((i & 0x0000FF00) << 8) | \
38 2cfe27bf 2006-08-15 matthias> ((i & 0x000000FF) << 24))
39 3fefb827 2006-08-10 matthias>
40 9d60efcf 2006-08-11 matthias> int
41 acb3e90c 2006-09-02 matthias> display_fat_header(struct list *lst, char *buffer, int *roffset)
42 9d60efcf 2006-08-11 matthias> {
43 9d60efcf 2006-08-11 matthias> struct fat_header *fh;
44 9d60efcf 2006-08-11 matthias> struct fat_arch *fa;
45 9d60efcf 2006-08-11 matthias> int offset = 0, i, narch;
46 9d60efcf 2006-08-11 matthias>
47 9d60efcf 2006-08-11 matthias> fh = malloc(sizeof(*fh));
48 9d60efcf 2006-08-11 matthias>
49 9d60efcf 2006-08-11 matthias> if (analyse_fat_header(buffer, &offset, fh) < 0) {
50 9d60efcf 2006-08-11 matthias> free(fh);
51 9d60efcf 2006-08-11 matthias> return(-1);
52 9d60efcf 2006-08-11 matthias> }
53 9d60efcf 2006-08-11 matthias>
54 7bf77b98 2006-08-15 matthias> list_insert_node(lst, fh, 0x1);
55 9d60efcf 2006-08-11 matthias>
56 9d60efcf 2006-08-11 matthias> narch = swapi(fh->nfat_arch);
57 9d60efcf 2006-08-11 matthias> for (i = 0; i < narch; i++) {
58 7bf77b98 2006-08-15 matthias> fa = malloc(sizeof(*fa));
59 9d60efcf 2006-08-11 matthias> analyse_fat_arch(buffer, &offset, fa);
60 7bf77b98 2006-08-15 matthias>
61 7bf77b98 2006-08-15 matthias> list_insert_node(lst, fa, 0x2);
62 7bf77b98 2006-08-15 matthias>
63 a2a93b84 2006-08-12 matthias> if (swapi(fa->size) > size) {
64 a2a93b84 2006-08-12 matthias> printf("Malformed universal binary. Size for one " \
65 a2a93b84 2006-08-12 matthias> "Architecture is larger than the complete binary.\n");
66 a2a93b84 2006-08-12 matthias> exit(1);
67 a2a93b84 2006-08-12 matthias> }
68 a2a93b84 2006-08-12 matthias>
69 9d60efcf 2006-08-11 matthias> if (cpu == swapi(fa->cputype))
70 9d60efcf 2006-08-11 matthias> *roffset = swapi(fa->offset);
71 9d60efcf 2006-08-11 matthias> }
72 9d60efcf 2006-08-11 matthias>
73 9d60efcf 2006-08-11 matthias> return(narch);
74 9d60efcf 2006-08-11 matthias> }
75 9d60efcf 2006-08-11 matthias>
76 9d60efcf 2006-08-11 matthias> int
77 acb3e90c 2006-09-02 matthias> display_mo_header(struct list *lst, char *buffer, int *offset, int *ncmds)
78 9d60efcf 2006-08-11 matthias> {
79 9d60efcf 2006-08-11 matthias> struct mach_header *mh;
80 9d60efcf 2006-08-11 matthias>
81 9d60efcf 2006-08-11 matthias> mh = malloc(sizeof(*mh));
82 9d60efcf 2006-08-11 matthias> if (analyse_mo_header(buffer, offset, mh) < 0) {
83 9d60efcf 2006-08-11 matthias> free(mh);
84 9d60efcf 2006-08-11 matthias> return(-1);
85 9d60efcf 2006-08-11 matthias> }
86 9d60efcf 2006-08-11 matthias>
87 9d60efcf 2006-08-11 matthias> *ncmds = swapi(mh->ncmds);
88 9d60efcf 2006-08-11 matthias>
89 7bf77b98 2006-08-15 matthias> list_insert_node(lst, mh, 0x3);
90 9d60efcf 2006-08-11 matthias>
91 9d60efcf 2006-08-11 matthias> return(1);
92 9d60efcf 2006-08-11 matthias> }
93 9d60efcf 2006-08-11 matthias>
94 9ed434d7 2006-08-09 matthias> void
95 acb3e90c 2006-09-02 matthias> display_load_commands(struct list *lst, char *buffer, int *offset, int ncmds)
96 9d60efcf 2006-08-11 matthias> {
97 9d60efcf 2006-08-11 matthias> struct load_command *ld;
98 9d60efcf 2006-08-11 matthias> struct section *sec;
99 9d60efcf 2006-08-11 matthias> int i, nofx = 0, val = 0, j, offset_old;
100 7bf77b98 2006-08-15 matthias>
101 9d60efcf 2006-08-11 matthias> for (i = 0; i < ncmds; i++) {
102 7bf77b98 2006-08-15 matthias> ld = malloc(sizeof(*ld));
103 9d60efcf 2006-08-11 matthias> analyse_load_command(buffer, offset, ld);
104 7bf77b98 2006-08-15 matthias> list_insert_node(lst, ld, 0x4);
105 9d60efcf 2006-08-11 matthias> offset_old = *offset;
106 9d60efcf 2006-08-11 matthias> val = examine_segmet(buffer, offset, swapi(ld->cmd), swapi(ld->cmdsize), &nofx);
107 9d60efcf 2006-08-11 matthias> if (nofx > 0) {
108 9d60efcf 2006-08-11 matthias> for (j = 0; j < nofx; j++) {
109 7bf77b98 2006-08-15 matthias> sec = malloc(sizeof(*sec));
110 9d60efcf 2006-08-11 matthias> // Skip the segment header
111 9d60efcf 2006-08-11 matthias> if (j == 0) *offset += val;
112 9d60efcf 2006-08-11 matthias> examine_section(buffer, offset, sec);
113 e5c4b07e 2006-08-12 matthias> if ((strcmp(sec->segname, "__TEXT") == 0) &&
114 e5c4b07e 2006-08-12 matthias> (strcmp(sec->sectname, "__text") == 0)) {
115 e5c4b07e 2006-08-12 matthias> text_addr = swapi(sec->addr);
116 e5c4b07e 2006-08-12 matthias> text_size = swapi(sec->size);
117 e5c4b07e 2006-08-12 matthias> text_offset = swapi(sec->offset);
118 e5c4b07e 2006-08-12 matthias> }
119 e5c4b07e 2006-08-12 matthias> if ((strcmp(sec->segname, "__TEXT") == 0) &&
120 e5c4b07e 2006-08-12 matthias> (strcmp(sec->sectname, "__cstring") == 0)) {
121 e5c4b07e 2006-08-12 matthias> cs_addr = swapi(sec->addr);
122 e5c4b07e 2006-08-12 matthias> cs_size = swapi(sec->size);
123 e5c4b07e 2006-08-12 matthias> cs_offset = swapi(sec->offset);
124 e5c4b07e 2006-08-12 matthias> }
125 e5c4b07e 2006-08-12 matthias> if ((strcmp(sec->segname, "__DATA") == 0) &&
126 e5c4b07e 2006-08-12 matthias> (strcmp(sec->sectname, "__data") == 0)) {
127 e5c4b07e 2006-08-12 matthias> data_addr = swapi(sec->addr);
128 e5c4b07e 2006-08-12 matthias> data_size = swapi(sec->size);
129 e5c4b07e 2006-08-12 matthias> data_offset = swapi(sec->offset);
130 e5c4b07e 2006-08-12 matthias> }
131 7bf77b98 2006-08-15 matthias> list_insert_node(lst, sec, 0x5);
132 9d60efcf 2006-08-11 matthias> }
133 9d60efcf 2006-08-11 matthias> nofx = 0;
134 9d60efcf 2006-08-11 matthias> }
135 9d60efcf 2006-08-11 matthias> /* XXX Have to fix this
136 9d60efcf 2006-08-11 matthias> *
137 9d60efcf 2006-08-11 matthias> * Currently val is the length of the segment. We need val to
138 9d60efcf 2006-08-11 matthias> * skip the segment header, if the segment contains sections.
139 9d60efcf 2006-08-11 matthias> *
140 9d60efcf 2006-08-11 matthias> * Would be better to adjust offset in examine_segment for all
141 9d60efcf 2006-08-11 matthias> * possible segments (not currently implemented!)
142 9d60efcf 2006-08-11 matthias> */
143 9d60efcf 2006-08-11 matthias> else
144 9d60efcf 2006-08-11 matthias> *offset += swapi(ld->cmdsize);
145 9d60efcf 2006-08-11 matthias>
146 9d60efcf 2006-08-11 matthias> //if (*offset == offset_old)
147 9d60efcf 2006-08-11 matthias> // *offset += swapi(ld->cmdsize);
148 9d60efcf 2006-08-11 matthias> }
149 7bf77b98 2006-08-15 matthias> }
150 7bf77b98 2006-08-15 matthias>
151 7bf77b98 2006-08-15 matthias> void
152 7bf77b98 2006-08-15 matthias> print_lc_towlevel_hints(struct twolevel_hints_command *two)
153 7bf77b98 2006-08-15 matthias> {
154 7bf77b98 2006-08-15 matthias> mprintf(" Offset: %d\n", swapi(two->offset));
155 7bf77b98 2006-08-15 matthias> mprintf(" No of 2level hints: %d\n", swapi(two->nhints));
156 7bf77b98 2006-08-15 matthias> }
157 7bf77b98 2006-08-15 matthias>
158 7bf77b98 2006-08-15 matthias> void
159 7bf77b98 2006-08-15 matthias> print_lc_dysymtab(struct dysymtab_command *dsym)
160 7bf77b98 2006-08-15 matthias> {
161 7bf77b98 2006-08-15 matthias> mprintf(" ilocalsym: %d\n", swapi(dsym->ilocalsym));
162 7bf77b98 2006-08-15 matthias> mprintf(" nlocalsym: %d\n", swapi(dsym->nlocalsym));
163 7bf77b98 2006-08-15 matthias> mprintf(" iextdefsym: %d\n", swapi(dsym->iextdefsym));
164 7bf77b98 2006-08-15 matthias> mprintf(" nextdefsym: %d\n", swapi(dsym->nextdefsym));
165 7bf77b98 2006-08-15 matthias> mprintf(" iundefsym: %d\n", swapi(dsym->iundefsym));
166 7bf77b98 2006-08-15 matthias> mprintf(" nundefsym: %d\n", swapi(dsym->nundefsym));
167 7bf77b98 2006-08-15 matthias> mprintf(" tocoff: %d\n", swapi(dsym->tocoff));
168 7bf77b98 2006-08-15 matthias> mprintf(" ntoc: %d\n", swapi(dsym->ntoc));
169 7bf77b98 2006-08-15 matthias> mprintf(" modtaboff: %d\n", swapi(dsym->modtaboff));
170 7bf77b98 2006-08-15 matthias> mprintf(" nmodtab: %d\n", swapi(dsym->nmodtab));
171 7bf77b98 2006-08-15 matthias> mprintf(" extrefsymoff: %d\n", swapi(dsym->extrefsymoff));
172 7bf77b98 2006-08-15 matthias> mprintf(" nextrefsyms: %d\n", swapi(dsym->nextrefsyms));
173 7bf77b98 2006-08-15 matthias> mprintf(" indirectsymoff: %d\n", swapi(dsym->indirectsymoff));
174 7bf77b98 2006-08-15 matthias> mprintf(" nindirectsyms: %d\n", swapi(dsym->nindirectsyms));
175 7bf77b98 2006-08-15 matthias> mprintf(" extreloff: %d\n", swapi(dsym->extreloff));
176 7bf77b98 2006-08-15 matthias> mprintf(" nextrel: %d\n", swapi(dsym->nextrel));
177 7bf77b98 2006-08-15 matthias> mprintf(" locreloff: %d\n", swapi(dsym->locreloff));
178 7bf77b98 2006-08-15 matthias> mprintf(" nlocrel: %d\n", swapi(dsym->nlocrel));
179 9d60efcf 2006-08-11 matthias> }
180 9d60efcf 2006-08-11 matthias>
181 9d60efcf 2006-08-11 matthias> void
182 7bf77b98 2006-08-15 matthias> print_lc_load_dylib(struct dylib_command *dly)
183 7bf77b98 2006-08-15 matthias> {
184 7bf77b98 2006-08-15 matthias> time_t timev;
185 7bf77b98 2006-08-15 matthias>
186 7bf77b98 2006-08-15 matthias> if (dyn_display < 1) {
187 7bf77b98 2006-08-15 matthias> //mprintf(" Name: %s\n", ptr+swapi(dly->dylib.name.offset));
188 7bf77b98 2006-08-15 matthias> timev = swapi(dly->dylib.timestamp);
189 7bf77b98 2006-08-15 matthias> mprintf(" Timestamp: %s", ctime(&timev));
190 7bf77b98 2006-08-15 matthias> mprintf(" Current version: 0x%x\n", swapi(dly->dylib.current_version));
191 7bf77b98 2006-08-15 matthias> mprintf(" Compat version: 0x%x\n", swapi(dly->dylib.compatibility_version));
192 7bf77b98 2006-08-15 matthias> } else {
193 7bf77b98 2006-08-15 matthias> trigger = 0;
194 7bf77b98 2006-08-15 matthias> //mprintf(" + %s\n", ptr+swapi(dly->dylib.name.offset));
195 7bf77b98 2006-08-15 matthias> trigger = 1;
196 7bf77b98 2006-08-15 matthias> }
197 7bf77b98 2006-08-15 matthias> }
198 7bf77b98 2006-08-15 matthias>
199 7bf77b98 2006-08-15 matthias> void
200 7bf77b98 2006-08-15 matthias> print_lc_symtab(struct symtab_command *symc)
201 7bf77b98 2006-08-15 matthias> {
202 7bf77b98 2006-08-15 matthias> mprintf(" Symbol table offset: %d bytes\n", swapi(symc->symoff));
203 7bf77b98 2006-08-15 matthias> mprintf(" Symbol table entries: %d\n", swapi(symc->nsyms));
204 7bf77b98 2006-08-15 matthias> mprintf(" String table offset: %d bytes\n", swapi(symc->stroff));
205 7bf77b98 2006-08-15 matthias> mprintf(" String table size: %d bytes\n", swapi(symc->strsize));
206 7bf77b98 2006-08-15 matthias> }
207 7bf77b98 2006-08-15 matthias>
208 7bf77b98 2006-08-15 matthias> void
209 7bf77b98 2006-08-15 matthias> print_lc_segment(struct segment_command *sc)
210 7bf77b98 2006-08-15 matthias> {
211 7bf77b98 2006-08-15 matthias> mprintf(" Name: %s\n", sc->segname);
212 7bf77b98 2006-08-15 matthias> mprintf(" VM addr: 0x%.08x\n", swapi(sc->vmaddr));
213 7bf77b98 2006-08-15 matthias> mprintf(" VM size: 0x%.08x\n", swapi(sc->vmsize));
214 7bf77b98 2006-08-15 matthias> mprintf(" VM size: 0x%.08x\n", swapi(sc->vmsize));
215 7bf77b98 2006-08-15 matthias> mprintf(" File offset: 0x%.08x\n", swapi(sc->fileoff));
216 7bf77b98 2006-08-15 matthias> mprintf(" File size: %d bytes\n", swapi(sc->filesize));
217 7bf77b98 2006-08-15 matthias> mprintf(" Max prot: 0x%.08x\n", swapi(sc->maxprot));
218 7bf77b98 2006-08-15 matthias> mprintf(" Init prot: 0x%.08x\n", swapi(sc->initprot));
219 7bf77b98 2006-08-15 matthias> mprintf(" No of sects: %d\n", swapi(sc->nsects));
220 7bf77b98 2006-08-15 matthias> mprintf(" Flags: 0x%.08x\n", swapi(sc->flags));
221 7bf77b98 2006-08-15 matthias> }
222 7bf77b98 2006-08-15 matthias>
223 7bf77b98 2006-08-15 matthias> void
224 9d60efcf 2006-08-11 matthias> print_section(struct section *sec)
225 9d60efcf 2006-08-11 matthias> {
226 a2a93b84 2006-08-12 matthias> mprintf(" Sectname: %s\n", sec->sectname);
227 a2a93b84 2006-08-12 matthias> mprintf(" VM addr: 0x%.08x\n", swapi(sec->addr));
228 a2a93b84 2006-08-12 matthias> mprintf(" VM size: %d bytes\n", swapi(sec->size));
229 a2a93b84 2006-08-12 matthias> mprintf(" Offset: %d\n", swapi(sec->offset));
230 9d60efcf 2006-08-11 matthias> mprintf("\n");
231 9d60efcf 2006-08-11 matthias> }
232 9d60efcf 2006-08-11 matthias>
233 9d60efcf 2006-08-11 matthias> void
234 9d60efcf 2006-08-11 matthias> print_load_command(struct load_command *ld)
235 9d60efcf 2006-08-11 matthias> {
236 a2a93b84 2006-08-12 matthias> mprintf(" Command: ");
237 9d60efcf 2006-08-11 matthias> display_cmd_name(swapi(ld->cmd));
238 a2a93b84 2006-08-12 matthias> mprintf(" Command size: %d bytes\n", swapi(ld->cmdsize));
239 9d60efcf 2006-08-11 matthias> }
240 9d60efcf 2006-08-11 matthias>
241 9d60efcf 2006-08-11 matthias> void
242 9d60efcf 2006-08-11 matthias> print_mo_header(struct mach_header *mh)
243 9d60efcf 2006-08-11 matthias> {
244 a2a93b84 2006-08-12 matthias> mprintf("Magic: 0x%x\n", swapi(mh->magic));
245 9d60efcf 2006-08-11 matthias> mprintf(" CPU Type: ");
246 9d60efcf 2006-08-11 matthias> display_cpu_arch(swapi(mh->cputype));
247 9d60efcf 2006-08-11 matthias> mprintf("\n");
248 9d60efcf 2006-08-11 matthias> mprintf(" Subtype: %d\n", swapi(mh->cpusubtype));
249 9d60efcf 2006-08-11 matthias> mprintf(" Filetype: 0x%x\n", swapi(mh->filetype));
250 9d60efcf 2006-08-11 matthias> mprintf(" No load cmds: %d cmds\n", swapi(mh->ncmds));
251 9d60efcf 2006-08-11 matthias> mprintf(" Size of cmds: %d bytes\n", swapi(mh->sizeofcmds));
252 9d60efcf 2006-08-11 matthias> mprintf(" Flags: 0x%.08x\n", swapi(mh->flags));
253 9d60efcf 2006-08-11 matthias> mprintf("\n");
254 9d60efcf 2006-08-11 matthias> }
255 9d60efcf 2006-08-11 matthias>
256 9d60efcf 2006-08-11 matthias> void
257 9d60efcf 2006-08-11 matthias> print_fat_header(struct fat_header *fh)
258 9d60efcf 2006-08-11 matthias> {
259 a2a93b84 2006-08-12 matthias> mprintf("Magic: 0x%x\n", swapi(fh->magic));
260 9d60efcf 2006-08-11 matthias> }
261 9d60efcf 2006-08-11 matthias>
262 9d60efcf 2006-08-11 matthias> void
263 9d60efcf 2006-08-11 matthias> print_fat_arch(struct fat_arch *fa)
264 9d60efcf 2006-08-11 matthias> {
265 2cfe27bf 2006-08-15 matthias> /* The fat header is always stored in big-endian byte order, so we have
266 2cfe27bf 2006-08-15 matthias> * to swap the bo, if we work on a little-endian machine.
267 2cfe27bf 2006-08-15 matthias> */
268 2cfe27bf 2006-08-15 matthias> if (bo_a == LE) {
269 2cfe27bf 2006-08-15 matthias> mprintf(" CPU Type: (%x) ", swap_bo(fa->cputype));
270 2cfe27bf 2006-08-15 matthias> display_cpu_arch(swap_bo(fa->cputype));
271 2cfe27bf 2006-08-15 matthias> mprintf("\n");
272 2cfe27bf 2006-08-15 matthias> mprintf(" Subtype: %d\n", swap_bo(fa->cpusubtype));
273 2cfe27bf 2006-08-15 matthias> mprintf(" Offest: %d\n", swap_bo(fa->offset));
274 2cfe27bf 2006-08-15 matthias> mprintf(" Size: %d\n", swap_bo(fa->size));
275 2cfe27bf 2006-08-15 matthias> mprintf(" Align: %d\n", swap_bo(fa->align));
276 2cfe27bf 2006-08-15 matthias> } else {
277 2cfe27bf 2006-08-15 matthias> mprintf(" CPU Type: (%x) ", swapi(fa->cputype));
278 2cfe27bf 2006-08-15 matthias> display_cpu_arch(swapi(fa->cputype));
279 2cfe27bf 2006-08-15 matthias> mprintf("\n");
280 2cfe27bf 2006-08-15 matthias> mprintf(" Subtype: %d\n", swapi(fa->cpusubtype));
281 2cfe27bf 2006-08-15 matthias> mprintf(" Offest: %d\n", swapi(fa->offset));
282 2cfe27bf 2006-08-15 matthias> mprintf(" Size: %d\n", swapi(fa->size));
283 2cfe27bf 2006-08-15 matthias> mprintf(" Align: %d\n", swapi(fa->align));
284 2cfe27bf 2006-08-15 matthias>
285 2cfe27bf 2006-08-15 matthias> }
286 9d60efcf 2006-08-11 matthias> mprintf("\n");
287 9d60efcf 2006-08-11 matthias> }
288 9d60efcf 2006-08-11 matthias>
289 9d60efcf 2006-08-11 matthias> void
290 9ed434d7 2006-08-09 matthias> display_cmd_name(int cmd)
291 9ed434d7 2006-08-09 matthias> {
292 9ed434d7 2006-08-09 matthias> switch(cmd) {
293 9ed434d7 2006-08-09 matthias> case LC_SEGMENT:
294 9ed434d7 2006-08-09 matthias> mprintf("LC_SEGMENT\n"); break;
295 9ed434d7 2006-08-09 matthias> case LC_SYMTAB:
296 9ed434d7 2006-08-09 matthias> mprintf("LC_SYMTAB\n"); break;
297 9ed434d7 2006-08-09 matthias> case LC_LOAD_DYLIB:
298 9ed434d7 2006-08-09 matthias> mprintf("LC_LOAD_DYLIB\n"); break;
299 9ed434d7 2006-08-09 matthias> case LC_LOAD_DYLINKER:
300 9ed434d7 2006-08-09 matthias> mprintf("LC_LOAD_DYLINKER\n"); break;
301 9ed434d7 2006-08-09 matthias> case LC_DYSYMTAB:
302 9ed434d7 2006-08-09 matthias> mprintf("LC_DYSYMTAB\n"); break;
303 9ed434d7 2006-08-09 matthias> case LC_UNIXTHREAD:
304 9ed434d7 2006-08-09 matthias> mprintf("LC_UNIXTHREAD\n"); break;
305 d3f3e663 2006-08-09 matthias> case LC_TWOLEVEL_HINTS:
306 d3f3e663 2006-08-09 matthias> mprintf("LC_TWOLEVEL_HINTS\n"); break;
307 9ed434d7 2006-08-09 matthias> default:
308 9ed434d7 2006-08-09 matthias> mprintf("\n"); break;
309 9ed434d7 2006-08-09 matthias> }
310 9ed434d7 2006-08-09 matthias> }
311 9ed434d7 2006-08-09 matthias>
312 9ed434d7 2006-08-09 matthias> void
313 9ed434d7 2006-08-09 matthias> display_cpu_arch(int cputype)
314 9ed434d7 2006-08-09 matthias> {
315 9ed434d7 2006-08-09 matthias> switch (cputype) {
316 9ed434d7 2006-08-09 matthias> case CPU_TYPE_MC680x0:
317 680a6eb2 2006-08-09 matthias> mprintf("MC680x0"); break;
318 9ed434d7 2006-08-09 matthias> case CPU_TYPE_I386:
319 4c34a127 2012-09-11 matthias mprintf("x86_32"); break;
320 4c34a127 2012-09-11 matthias case CPU_TYPE_X86_64:
321 4c34a127 2012-09-11 matthias mprintf("x86_64"); break;
322 9ed434d7 2006-08-09 matthias> case CPU_TYPE_POWERPC:
323 680a6eb2 2006-08-09 matthias> mprintf("PowerPC"); break;
324 03cf29b4 2006-08-10 matthias> case CPU_TYPE_HPPA:
325 03cf29b4 2006-08-10 matthias> mprintf("HPPA"); break;
326 03cf29b4 2006-08-10 matthias> case CPU_TYPE_SPARC:
327 03cf29b4 2006-08-10 matthias> mprintf("Sparc"); break;
328 9ed434d7 2006-08-09 matthias> default:
329 9ed434d7 2006-08-09 matthias> mprintf("\n"); break;
330 9ed434d7 2006-08-09 matthias> }
331 9ed434d7 2006-08-09 matthias> }
332 9ed434d7 2006-08-09 matthias>
333 9ed434d7 2006-08-09 matthias> void
334 9ed434d7 2006-08-09 matthias> mprintf(const char *fmt, ...)
335 9ed434d7 2006-08-09 matthias> {
336 4c34a127 2012-09-11 matthias va_list ap;
337 9ed434d7 2006-08-09 matthias>
338 9ed434d7 2006-08-09 matthias> if (trigger == 1)
339 9ed434d7 2006-08-09 matthias> return;
340 9ed434d7 2006-08-09 matthias>
341 9ed434d7 2006-08-09 matthias> va_start(ap, fmt);
342 9ed434d7 2006-08-09 matthias> vfprintf(stdout, fmt, ap);
343 9ed434d7 2006-08-09 matthias> va_end(ap);
344 a27a8797 2006-08-09 matthias> }
345 a27a8797 2006-08-09 matthias>
346 a27a8797 2006-08-09 matthias> int
347 a27a8797 2006-08-09 matthias> get_cpu_information()
348 a27a8797 2006-08-09 matthias> {
349 80ade98a 2012-09-11 matthias #ifndef __linux__
350 f3cfd5fa 2006-08-10 matthias> char buf[50];
351 f3cfd5fa 2006-08-10 matthias> int mib[2];
352 f3cfd5fa 2006-08-10 matthias> size_t len;
353 f3cfd5fa 2006-08-10 matthias>
354 f3cfd5fa 2006-08-10 matthias> mib[0] = CTL_HW;
355 f3cfd5fa 2006-08-10 matthias> mib[1] = HW_MACHINE;
356 f3cfd5fa 2006-08-10 matthias> len = sizeof(buf);
357 f3cfd5fa 2006-08-10 matthias> if (sysctl(mib, 2, &buf, &len, NULL, 0) < 0)
358 f3cfd5fa 2006-08-10 matthias> errx(1, "Cannot determine local CPU type");
359 a27a8797 2006-08-09 matthias>
360 f3cfd5fa 2006-08-10 matthias> if (strncmp(buf, "i386", 4) == 0)
361 f3cfd5fa 2006-08-10 matthias> return(CPU_TYPE_X86);
362 f3cfd5fa 2006-08-10 matthias> else if (strncmp(buf, "Power Macintosh", 15) == 0)
363 f3cfd5fa 2006-08-10 matthias> return(CPU_TYPE_POWERPC);
364 80ade98a 2012-09-11 matthias #else /* __linux__ */
365 80ade98a 2012-09-11 matthias #if defined __x86_64__
366 80ade98a 2012-09-11 matthias return(CPU_TYPE_X86);
367 80ade98a 2012-09-11 matthias #elif defined __i386__
368 80ade98a 2012-09-11 matthias return(CPU_TYPE_I386);
369 80ade98a 2012-09-11 matthias #else
370 80ade98a 2012-09-11 matthias # error "Cannot determine local CPU type"
371 80ade98a 2012-09-11 matthias #endif
372 80ade98a 2012-09-11 matthias #endif /* __linux__ */
373 f3cfd5fa 2006-08-10 matthias> return(-1);
374 a27a8797 2006-08-09 matthias> }
375 a27a8797 2006-08-09 matthias>
376 a27a8797 2006-08-09 matthias> int
377 a27a8797 2006-08-09 matthias> get_bo_information()
378 a27a8797 2006-08-09 matthias> {
379 80ade98a 2012-09-11 matthias #ifndef __linux__
380 f3cfd5fa 2006-08-10 matthias> int mib[2], bo;
381 f3cfd5fa 2006-08-10 matthias> size_t len;
382 a27a8797 2006-08-09 matthias>
383 f3cfd5fa 2006-08-10 matthias> mib[0] = CTL_HW;
384 f3cfd5fa 2006-08-10 matthias> mib[1] = HW_BYTEORDER;
385 f3cfd5fa 2006-08-10 matthias> len = sizeof(bo);
386 f3cfd5fa 2006-08-10 matthias> if (sysctl(mib, 2, &bo, &len, NULL, 0) < 0)
387 f3cfd5fa 2006-08-10 matthias> errx(1, "Cannot determine local byte order");
388 a27a8797 2006-08-09 matthias>
389 f3cfd5fa 2006-08-10 matthias> if (bo == 1234)
390 f3cfd5fa 2006-08-10 matthias> return(LE);
391 f3cfd5fa 2006-08-10 matthias> else if (bo == 4321)
392 f3cfd5fa 2006-08-10 matthias> return(BE);
393 80ade98a 2012-09-11 matthias #else /* __linux__ */
394 80ade98a 2012-09-11 matthias #if defined(__BIG_ENDIAN)
395 80ade98a 2012-09-11 matthias return(BE);
396 80ade98a 2012-09-11 matthias #elif defined(__LITTLE_ENDIAN)
397 80ade98a 2012-09-11 matthias return(LE);
398 80ade98a 2012-09-11 matthias #else
399 80ade98a 2012-09-11 matthias # error "Cannot determine local byte order"
400 80ade98a 2012-09-11 matthias #endif /* byte order */
401 80ade98a 2012-09-11 matthias #endif /* __linux__ */
402 f3cfd5fa 2006-08-10 matthias>
403 f3cfd5fa 2006-08-10 matthias> return(-1);
404 a27a8797 2006-08-09 matthias> }
405 a27a8797 2006-08-09 matthias>
406 3fefb827 2006-08-10 matthias> unsigned int
407 3fefb827 2006-08-10 matthias> swapi(unsigned int i)
408 a27a8797 2006-08-09 matthias> {
409 ad17a9e6 2006-08-12 matthias> unsigned int ret = i;
410 ad17a9e6 2006-08-12 matthias>
411 ad17a9e6 2006-08-12 matthias> if (bo_a == LE && bo_b == BE) {
412 ad17a9e6 2006-08-12 matthias> ret = swap_bo(i);
413 ad17a9e6 2006-08-12 matthias> } else if(bo_a == BE && bo_b == LE) {
414 ad17a9e6 2006-08-12 matthias> ret = swap_bo(i);
415 ad17a9e6 2006-08-12 matthias> }
416 ad17a9e6 2006-08-12 matthias>
417 ad17a9e6 2006-08-12 matthias> return(ret);
418 5bb64da4 2006-08-10 matthias> }