aboutsummaryrefslogtreecommitdiff
path: root/src/libmach/machocorepower.c
blob: 89fc564f767008e53e334d07920927f4b5eb5165 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <u.h>
#include <libc.h>
#include <mach.h>
#include "macho.h"
#include "uregpower.h"

enum
{
	ThreadState = 1,
	FloatState,
	ExceptionState,
	VectorState,
	ThreadState64,
	ExceptionState64,
	ThreadStateNone
};

typedef struct Lreg Lreg;
typedef struct Lflt Lflt;
typedef struct Lexc Lexc;

struct Lreg
{
	u32int srr0;
	u32int srr1;
	u32int r0;
	u32int r1;
	u32int r2;
	u32int r3;
	u32int r4;
	u32int r5;
	u32int r6;
	u32int r7;
	u32int r8;
	u32int r9;
	u32int r10;
	u32int r11;
	u32int r12;
	u32int r13;
	u32int r14;
	u32int r15;
	u32int r16;
	u32int r17;
	u32int r18;
	u32int r19;
	u32int r20;
	u32int r21;
	u32int r22;
	u32int r23;
	u32int r24;
	u32int r25;
	u32int r26;
	u32int r27;
	u32int r28;
	u32int r29;
	u32int r30;
	u32int r31;

	u32int cr;
	u32int xer;
	u32int lr;
	u32int ctr;
	u32int mq;

	u32int vrsave;
};

struct Lflt
{
	u32int fpregs[32*2];		/* 32 doubles */
	u32int fpscr[2];

};

struct Lexc
{
	u32int dar;
	u32int dsisr;
	u32int exception;
	u32int pad0;
	u32int pad1[4];
};

static void
lreg2ureg(Lreg *l, Ureg *u)
{
	u->pc = l->srr0;
	u->srr1 = l->srr1;
	u->lr = l->lr;
	u->cr = l->cr;
	u->xer = l->xer;
	u->ctr = l->ctr;
	u->vrsave = l->vrsave;
	memmove(&u->r0, &l->r0, 32*4);
}

static void
lexc2ureg(Lexc *l, Ureg *u)
{
	u->cause = l->exception;
	u->dar = l->dar;
	u->dsisr = l->dsisr;
}

static uchar*
load(int fd, ulong off, int size)
{
	uchar *a;

	a = malloc(size);
	if(a == nil)
		return nil;
	if(seek(fd, off, 0) < 0 || readn(fd, a, size) != size){
		free(a);
		return nil;
	}
	return a;
}

int
coreregsmachopower(Macho *m, uchar **up)
{
	int i, havereg, haveexc;
	uchar *a, *p, *nextp;
	Ureg *u;
	ulong flavor, count;
	MachoCmd *c;

	*up = nil;
	for(i=0; i<m->ncmd; i++)
		if(m->cmd[i].type == MachoCmdThread)
			break;
	if(i == m->ncmd){
		werrstr("no registers found");
		return -1;
	}

	c = &m->cmd[i];
	a = load(m->fd, c->off, c->size);
	if(a == nil)
		return -1;

	if((u = mallocz(sizeof(Ureg), 1)) == nil){
		free(a);
		return -1;
	}

	havereg = haveexc = 0;
	for(p=a+8; p<a+c->size; p=nextp){
		flavor = m->e4(p);
		count = m->e4(p+4);
		nextp = p+8+count*4;
		if(flavor == ThreadState && count*4 == sizeof(Lreg)){
			havereg = 1;
			lreg2ureg((Lreg*)(p+8), u);
		}
		if(flavor == ExceptionState && count*4 == sizeof(Lexc)){
			haveexc = 1;
			lexc2ureg((Lexc*)(p+8), u);
		}
	}
	free(a);
	if(!havereg){
		werrstr("no registers found");
		free(u);
		return -1;
	}
	if(!haveexc)
		fprint(2, "warning: no exception state in core file registers\n");
	*up = (uchar*)u;
	return sizeof(*u);
}