forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsym.c
More file actions
546 lines (483 loc) · 10.3 KB
/
Copy pathsym.c
File metadata and controls
546 lines (483 loc) · 10.3 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
* sym.c for mdb
*/
#include "mdb.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <a.out.h>
#include "proto.h"
#if GNU_SUPPORT
#define ZMAGIC 0413
#define NMAGIC 0410
#define QMAGIC 0314
#endif
struct symtab_s
{
struct nlist *start;
struct nlist *end;
int text;
int data;
unsigned nsym;
};
PRIVATE struct symtab_s symtab;
PRIVATE int type_of_exec;
FORWARD _PROTOTYPE( int check_exec, (struct exec *hdr) );
FORWARD _PROTOTYPE( void sortsyms , (struct nlist *array , struct nlist *top ));
FORWARD _PROTOTYPE( int symeq , (char *t , struct nlist *sp ));
FORWARD _PROTOTYPE( int symprefix , (char *t , struct nlist *sp ));
FORWARD _PROTOTYPE( struct nlist *findsname, (char *name, int is_text, int allflag) );
FORWARD _PROTOTYPE( void outsym, (struct nlist *sp, off_t off) );
FORWARD _PROTOTYPE( struct nlist *findsval, (off_t value, int where) );
PUBLIC void syminit( filename )
char *filename;
{
int fd;
struct exec header;
register struct symtab_s *tp;
tp = &symtab;
if ( (fd = open( filename, O_RDONLY)) < 0) {
fprintf(stderr, "Couldn't open %s.\n", filename);
perror(filename);
exit(1);
}
if( read( fd, (char *) &header, sizeof header ) != sizeof header )
{
fprintf(stderr, "Couldn't read %d bytes from %s.\n", sizeof(header), filename);
close( fd );
exit(1);
}
type_of_exec = check_exec(&header);
#if EXTRA_SYMBOLS
if ( type_of_exec == GNU_SYMBOLS) {
close(fd);
gnu_init(filename);
return;
}
#endif
/* For MINIX EXEC */
if ( lseek( fd, A_SYMPOS( header ), 0 ) != A_SYMPOS( header ) )
{
do_error( "mdb - reading header" );
close( fd );
exit(1);
}
if ( (int) header.a_syms < 0 ||
(unsigned) header.a_syms != header.a_syms ||
(tp->start = (struct nlist *) malloc( (unsigned) header.a_syms ))
== (struct nlist *) NULL &&
header.a_syms != 0 )
{
Printf("mdb: no room for symbol table" );
close( fd );
return;
}
if ( read( fd, (char *) tp->start, (int) header.a_syms ) < 0 )
{
do_error( "mdb - reading symbol table" );
close( fd );
return;
}
close( fd );
tp->nsym = (unsigned) header.a_syms / sizeof (struct nlist);
tp->end = tp->start + tp->nsym;
tp->text = 0x07;
tp->data = 0x0F;
/* sort on value only, name search not used much and storage a problem */
Printf("Sorting %d MINIX symbols ....", tp->nsym );
sortsyms( tp->start, tp->end );
Printf("\n");
}
/* Check exec file
* return type of exec
* or exit
*/
PRIVATE int check_exec(hdr)
struct exec *hdr;
{
long magic;
/* Check MAGIC number */
if (hdr->a_magic[0] != A_MAGIC0 || hdr->a_magic[1] != A_MAGIC1) {
#if GNU_SUPPORT
memcpy(&magic, hdr, sizeof(long));
/* Clear bits */
magic &= 0xFFFF;
if ( magic == ZMAGIC || magic == QMAGIC ) {
is_separate = FALSE;
return GNU_SYMBOLS;
}
if ( magic == NMAGIC ) {
is_separate = TRUE;
return GNU_SYMBOLS;
}
#endif
Printf("mdb: invalid magic number in exec header - %02x %02x\n",
hdr->a_magic[0],
hdr->a_magic[1]);
exit(1);
}
/* Check CPU */
#if (CHIP == INTEL)
#if (_WORD_SIZE == 4)
if (hdr->a_cpu != A_I80386)
#else
if (hdr->a_cpu != A_I8086)
#endif
#endif
#if (CHIP == M68000)
if (hdr->a_cpu != A_M68K)
#endif
{
Printf("mdb: invalid cpu in exec header - %04x\n",
hdr->a_cpu);
exit(1);
}
is_separate = FALSE;
#ifdef MINIX_PC
if (hdr->a_flags & A_SEP)
is_separate = TRUE;
#endif
/*
* A_EXEC is not being set by current cc
* It was set in Minix 1.5.0
*/
#if 0
/* Check flags - separate I & D or not */
if (hdr->a_flags & A_EXEC)
is_separate = FALSE;
else {
Printf("mdb: object file not exec %04x\n",
hdr->a_flags);
exit(1);
}
#endif
return MINIX_SYMBOLS;
}
PUBLIC long symbolvalue( name, is_text )
char *name;
int is_text;
{
register struct nlist *sp;
#if EXTRA_SYMBOLS
if ( type_of_exec == GNU_SYMBOLS )
return gnu_symbolvalue( name, is_text );
#endif
/* For MINIX EXEC */
sp = findsname(name, is_text, 0);
if (sp != NULL)
return sp->n_value;
else
return 0L;
}
PRIVATE struct nlist *findsname( name, is_text, allflag )
char *name;
int is_text;
int allflag;
{
char *s;
unsigned char sclass;
int schar;
char *send;
register struct nlist *sp;
register struct symtab_s *tp;
tp = &symtab;
if ( allflag )
{
/* find and print all matching symbols */
for ( sp = tp->start; sp < tp->end; ++sp )
{
if ( symprefix( name, sp ) )
{
sp = sp;
for ( s = sp->n_name, send = s + sizeof sp->n_name;
*s != 0 && s < send; ++s )
outbyte( *s );
for ( ; s <= send; ++s )
outspace();
switch( sp->n_sclass & N_SECT )
{
case N_ABS: schar = 'a'; break;
case N_TEXT: schar = 't'; break;
case N_DATA: schar = 'd'; break;
case N_BSS: schar = 'b'; break;
default: schar = '?'; break;
}
if ( (sp->n_sclass & N_CLASS) == C_EXT && schar != '?' )
schar += 'A' - 'a';
outbyte( schar );
outspace();
#if (_WORD_SIZE == 2)
outh16( (u16_t) sp->n_value );
#else
outh32( sp->n_value );
#endif
outbyte('\n');
}
}
}
else
{
/* find symbol by dumb linear search */
for ( sp = tp->start; sp < tp->end; ++sp )
{
sclass = sp->n_sclass & N_SECT;
if ( (is_text && sclass == N_TEXT ||
!is_text && (sclass == N_DATA || sclass == N_BSS)) &&
symeq( name, sp ) )
return sp;
}
}
return NULL;
}
PRIVATE struct nlist *findsval( value, where )
off_t value;
int where;
{
int left;
int middle;
int right;
unsigned char sclass;
register struct nlist *sp;
register struct symtab_s *tp;
tp = &symtab;
/* find last symbol with value <= desired one by binary search */
for ( left = 0, right = tp->nsym - 1; left <= right; )
{
middle = (left + right) / 2;
sp = tp->start + middle;
if ( value < sp->n_value )
right = middle - 1;
else
left = middle + 1;
}
if ( right >= 0 )
/* otherwise tp->start + right may wrap around to > tp->start !! */
for ( sp = tp->start + right; sp >= tp->start; --sp )
{
if ( (sp->n_sclass & N_CLASS) != C_EXT ) continue;
sclass = sp->n_sclass & N_SECT;
if ( (where == CSEG && sclass == N_TEXT ||
where != CSEG && (sclass == N_DATA || sclass == N_BSS)) )
return sp;
}
return NULL;
}
PUBLIC void printhex(v)
off_t v;
{
if ( v >= 65536L )
outh32( v );
else if ( v >= 256 )
outh16( (u16_t) v );
else
outh8( (u8_t) v );
}
PRIVATE void outsym( sp, off )
struct nlist *sp;
off_t off;
{
register char *s;
char *send;
for ( s = sp->n_name, send = s + sizeof sp->n_name; *s != 0 && s < send; ++s )
outbyte( *s );
if ( (off -= sp->n_value) != 0 )
{
outbyte( '+' );
printhex(off);
}
}
/* shell sort symbols on value */
PRIVATE void sortsyms( array, top )
struct nlist *array;
struct nlist *top;
{
int gap;
int i;
int j;
register struct nlist *left;
register struct nlist *right;
struct nlist swaptemp;
int size;
size = top - array;
/* choose gaps according to Knuth V3 p95 */
for ( gap = 1, i = 4; (j = 3 * i + 1) < size; gap = i, i = j )
;
do
{
for ( j = gap; j < size; ++j )
for ( i = j - gap; i >= 0; i -= gap )
{
left = array + i;
right = array + (i + gap);
if ( (off_t) left->n_value <=
right->n_value )
break;
swaptemp = *left;
*left = *right;
*right = swaptemp;
}
}
while ( (gap /= 3) != 0 );
}
PUBLIC void symbolic( value, separator )
off_t value;
int separator;
{
register struct nlist *sp;
long off;
#if EXTRA_SYMBOLS
if ( type_of_exec == GNU_SYMBOLS ) {
gnu_symbolic( value, separator );
return;
}
#endif
/* For MINIX EXEC */
if (value < st_addr || value > end_addr) {
outstr("0x");
printhex(value);
outbyte(separator);
return;
}
if ( (sp = findsval( value, CSEG )) != NULL )
{
outsym( sp, value );
}
else if ( (sp = findsval( value, DSEG )) != NULL )
{
outsym( sp, value );
}
else
{
outstr("_start");
off = value - st_addr;
if ( off != 0 )
{
outbyte( '+' );
printhex(off);
}
}
outbyte( separator );
}
PRIVATE int symeq( t, sp )
register char *t;
struct nlist *sp;
{
return strncmp( t, sp->n_name, sizeof sp->n_name ) == 0;
}
PRIVATE int symprefix( t, sp )
register char *t;
struct nlist *sp;
{
register char *s;
char *send;
for ( ; *t == '_'; ++t )
;
for ( s = sp->n_name, send = s + sizeof sp->n_name;
s < send && *s == '_'; ++s )
;
return strncmp( s, t, (size_t)(send - s) ) == 0;
}
/* list all symbols - test for selection criteria */
PUBLIC void listsym(cmd)
char *cmd;
{
register struct symtab_s *tp;
register struct nlist *sp;
char *s;
char *send;
char schar;
char tchar;
/* set selection */
cmd = skip(cmd+1);
if( *cmd == '\n' || *cmd == ';' )
tchar = '*';
else
tchar = *cmd;
#if EXTRA_SYMBOLS
if ( type_of_exec == GNU_SYMBOLS ) {
gnu_listsym(tchar);
return;
}
#endif
/* For MINIX EXEC */
tp = &symtab;
for ( sp = tp->start; sp < tp->end; ++sp )
{
switch( sp->n_sclass & N_SECT )
{
case N_ABS: schar = 'a'; break;
case N_TEXT: schar = 't'; break;
case N_DATA: schar = 'd'; break;
case N_BSS: schar = 'b'; break;
default: schar = '?'; break;
}
if ( (sp->n_sclass & N_CLASS) == C_EXT && schar != '?' )
schar += 'A' - 'a';
/* check for selection */
if ( tchar != '*' && schar != tchar)
continue;
/* print symbol type and value */
for ( s = sp->n_name, send = s + sizeof sp->n_name;
*s != 0 && s < send; ++s ) outbyte( *s );
for ( ; s <= send; ++s ) outspace();
outbyte( schar );
outspace();
#if (_WORD_SIZE == 2)
outh16( (u16_t) sp->n_value );
#else
outh32( sp->n_value );
#endif
outbyte('\n');
}
}
PUBLIC int text_symbol(value)
off_t value;
{
struct nlist *sp;
#if EXTRA_SYMBOLS
if ( type_of_exec == GNU_SYMBOLS )
return gnu_text_symbol(value);
#endif
if ((sp = findsval(value, CSEG)) != NULL && sp->n_value == value)
{
outsym(sp, value);
return TRUE;
}
else
return FALSE;
}
PUBLIC int finds_data(off,data_seg)
off_t off;
int data_seg;
{
struct nlist *sp;
#if EXTRA_SYMBOLS
if ( type_of_exec == GNU_SYMBOLS )
return gnu_finds_data(off,data_seg);
#endif
if ((sp = findsval(off, data_seg)) != NULL)
{
outsym(sp, off);
return TRUE;
}
else
return FALSE;
}
PUBLIC int finds_pc(pc)
off_t pc;
{
struct nlist *sp;
#if EXTRA_SYMBOLS
if ( type_of_exec == GNU_SYMBOLS )
return gnu_finds_pc(pc);
#endif
if ((sp = findsval(pc, CSEG)) != NULL)
{
outsym(sp, pc);
return TRUE;
}
else
return FALSE;
}