File: loadimag.c

package info (click to toggle)
rscheme 0.7.2-1.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 10,672 kB
  • ctags: 12,430
  • sloc: lisp: 37,104; ansic: 29,763; cpp: 2,630; sh: 1,677; makefile: 568; yacc: 202; lex: 175; perl: 33
file content (799 lines) | stat: -rw-r--r-- 20,004 bytes parent folder | download | duplicates (4)
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*-----------------------------------------------------------------*-C-*---
 * File:    handc/heapi/loadimag.c
 *
 *          Copyright (C)1997 Donovan Kolbly <d.kolbly@rscheme.org>
 *          as part of the RScheme project, licensed for free use.
 *          See <http://www.rscheme.org/> for the latest information.
 *
 * File version:     1.17
 * File mod date:    1997.11.29 23:10:44
 * System build:     v0.7.2, 97.12.21
 *
 * Purpose:          image loader (0.5 style)
 *------------------------------------------------------------------------*/

#include "heapi.h"
#include "imagfile.h"
#include <rscheme/scheme.h>
#include <rscheme/hashmain.h>
#include <rscheme/hashfn.h>
#include "mapf.h"
#include <string.h>
#include <rscheme/smemory.h>
#include <rscheme/linktype.h>
#include <rscheme/allocns.h>
#include <stdlib.h>

static obj pd_class = FALSE_OBJ;

/*  This is a special flag that tells the allocation subsystem
    that the "class" pointer may not be a real pointer
*/

rs_bool loading_image;

#define NO_PART_TOKEN MAKE_UNIQ_OBJ(MISSING_TAG)

#define IGNORE_FAULTS

/* #define LOADIMAGE_TRACE */
/* #define PAUSE_IN_MODE2 */

#ifdef LOADIMAGE_TRACE
/* #define SKIP_TEMPLATE_SWIZZLING */
FILE *debug_file;
FILE *debug_binary_file;
char info_buffer[1024*1024], *info_buf_ptr;
obj target_comments;	/* a hash table of comments */
char *comment_str;


obj unswiz_data[1024*60], *unswiz_data_ptr;

static void write_trace_info( obj item, int mode )
{
int n;

    fwrite( &item, sizeof(obj), 1, debug_binary_file );
    fwrite( &mode, sizeof(int), 1, debug_binary_file );
    n = unswiz_data_ptr - unswiz_data;
    fwrite( &n, sizeof(int), 1, debug_binary_file );
    fwrite( unswiz_data, sizeof(obj), n, debug_binary_file );
    n = info_buf_ptr - info_buffer;
    fwrite( &n, sizeof(int), 1, debug_binary_file );
    fwrite( info_buffer, 1, n, debug_binary_file );
}

static void reset_trace_info( void )
{
    info_buf_ptr = info_buffer;
    unswiz_data_ptr = unswiz_data;
}

static void tri_printf( const char *msg, ... )
{
va_list a;
unsigned n;

    va_start(a,msg);
    vsprintf( info_buf_ptr, msg, a );
    va_end(a);
    n = strlen(info_buf_ptr);
    fwrite( info_buf_ptr, 1, n, debug_file );
    info_buf_ptr += n;
}

static void tri_swizzled( int offset, obj from, obj to )
{
obj comment = FALSE_OBJ;

    if (OBJ_ISA_PTR(from))
    {
    obj k = OBJ( VAL(from) - POINTER_TAG );
    
	comment = objecttable_lookup( target_comments, k, k );
    }
    if (offset == -1)
	tri_printf( "  class" );
    else if (offset == -2)
	tri_printf( "   self" );
    else
	tri_printf( "    [%d]", offset/4 );
	
    tri_printf( " %#x => %#x", VAL(from), VAL(to) );
    
    if (OBJ_ISA_PTR(to))
    {
	*unswiz_data_ptr++ = to;
        *info_buf_ptr++ = ' ';
	*info_buf_ptr++ = '*';
    }

    if (!EQ(comment,FALSE_OBJ))
    {
        *info_buf_ptr++ = ' ';
	strcpy( info_buf_ptr, string_text(comment) );
	info_buf_ptr = strchr( info_buf_ptr, 0 );
    }
    *info_buf_ptr++ = '\n';
}


#endif

#define NUM_USEFUL_LOAD2_MODES (5)


static rs_bool remap_constants; /* do certain constants (like #none) need
				to get switched around? (ie, for loading
				version 1 images) */

typedef void swizzler_fn( obj item );

static void swizzle_class_only( obj item );
static void swizzle_gvec( obj item );
static void swizzle_alloc_area( obj item );
static void swizzle_template( obj item );
static void swizzle_partcont( obj item );

static struct {
    enum load2_mode mode;
    swizzler_fn		*swizzler;
} useful_load2_modes[NUM_USEFUL_LOAD2_MODES] =
    { { LOAD2_CLASS_ONLY, swizzle_class_only },
      { LOAD2_GVEC, swizzle_gvec },
      { LOAD2_TEMPLATE, swizzle_template },
      { LOAD2_PARTCONT, swizzle_partcont },
      { LOAD2_MIXVEC_2, swizzle_alloc_area } };


static obj load_ref( void *src, UINT_32 len, obj preamble );
static obj load_symbol( void *src, UINT_32 len, obj preamble );
static obj load_part( void *src, UINT_32 len, obj preamble );
static obj load_array_8( void *src, UINT_32 len, obj preamble );
static obj load_array_16( void *src, UINT_32 len, obj preamble );
static obj load_array_32( void *src, UINT_32 len, obj preamble );
static obj load_array_64( void *src, UINT_32 len, obj preamble );

static void *find_a_part( const char *module, int tag );

static obj *obj_table, *obj_table_limit;
static obj reference_table;

static _rs_volatile void load_image_error( const char *msg )
{
    fprintf( stderr, "Load error: %s\n", msg );
    abort();
    while (1);
}


static _rs_inline obj swizzle( obj item )
{
    if (OBJ_ISA_PTR(item))
    {
      obj result, *p;
      
      p = (obj *)(((char *)obj_table) + VAL(item) - POINTER_TAG);
      assert( p < obj_table_limit );

      result = *p;
#ifdef LOADIMAGE_TRACE
      fprintf( debug_file, "\t%#x ==> %#x\n", VAL(item), VAL(result) );
#endif
      return result;
    }
    else
    {
        if (remap_constants && OBJ_ISA_IMMOB(item))
	{
	    switch ((int)SECONDARY_TAG(item))
	    {
	      case 4: /* old #undef */
		item = UNDEFINED_OBJ;
		break;
	      case 5: /* old #uninit */
		item = UNINITIALIZED_OBJ;
		break;
	      case 6: /* old #none */
		item = NOVALUE_OBJ;
		break;
	      case 7: /* old #unbound */
		item = UNBOUND_OBJ;
		break;
	    }
	}
	return item;
    }
}


static void swizzle_class_only( obj item )
{
POBHeader *h = PTR_TO_HDRPTR(item);
obj n;

    assert( OBJ_ISA_PTR(item) );
    n = swizzle( h->pob_class );
#ifdef LOADIMAGE_TRACE
    tri_swizzled( -1, h->pob_class, n );
#endif
    h->pob_class = n;
}

static void swizzle_mixvec( obj item, UINT_32 num_slots )
{
POBHeader *h = PTR_TO_HDRPTR(item);
obj n, *d = (obj *)PTR_TO_DATAPTR(item);
UINT_32 i, len;

    assert( OBJ_ISA_PTR(item) );
    n = swizzle( h->pob_class );
    h->pob_class = n;

    len = SLOT(num_slots);
    for (i=0; i<len; i+=SLOT(1))
    {
	n = swizzle( *d++ );
#ifndef NDEBUG
	/* if we're in debug mode, gvec_write_init() will assert that
	 * the target slot is truly uninitialized (alloc() fills w/
	 * DEBUG_TRAP_OBJ to indicate uninitialzed data
	 */
	d[-1] = DEBUG_TRAP_OBJ;
#endif
        gvec_write_init( item, i, n );
    }
}

static void swizzle_alloc_area( obj item )
{
  AllocArea *aa = (AllocArea *)PTR_TO_DATAPTR(item);

  assert( OBJ_ISA_PTR(item) );
  swizzle_mixvec( item, 2 );

  /* reinstall the function pointer */
  aa->allocfn = default_alloc_obj;
}

static void swizzle_gvec( obj item )
{
POBHeader *h = PTR_TO_HDRPTR(item);
obj n, *d = (obj *)PTR_TO_DATAPTR(item);
UINT_32 i, len = SIZEOF_PTR(item);

    assert( OBJ_ISA_PTR(item) );
    n = swizzle( h->pob_class );
#ifdef LOADIMAGE_TRACE
    tri_swizzled( -1, h->pob_class, n );
#endif
    h->pob_class = n;

    for (i=0; i<len; i+=SLOT(1))
    {
	n = swizzle( *d );
#ifdef LOADIMAGE_TRACE
	tri_swizzled( i, *d, n );
#endif /* LOADIMAGE_TRACE */
	/* this acts like an initializing write because we
	 * don't consider the previous write (during unpacking)
	 * to have been a "real" write -- we were just temporarily
	 * storing some random bits there
	 * ... if we did `fresh' write here, then those random bits
	 *     would be interpreted as a pointer, Which Could Be Bad
	 *     (in particular, those random bits DO look like a pointer,
	 *     if we are storing a pointer into that slot)
	 */
#ifndef NDEBUG
	/* if we're in debug mode, gvec_write_init() will assert that
	 * the target slot is truly uninitialized (alloc() fills w/
	 * DEBUG_TRAP_OBJ to indicate uninitialzed data
	 */
	*((obj *)((char *)PTR_TO_DATAPTR(item) + i)) = DEBUG_TRAP_OBJ;
#endif
        gvec_write_init( item, i, n );
	d++;
    }
}

static void swizzle_template( obj item )
{
struct part_descr *part_ptr;
UINT_32 ix;
jump_addr entry_point;
struct function_descr *fn;
obj in_part;

    assert( OBJ_ISA_PTR(item) );
    swizzle_gvec( item );
    if (SIZEOF_PTR(item) < 8)
    {
	fprintf( stderr, 
	    "Warning: %#x (TEMPLATE) is too small! (code_ptr = %#x)\n", 
	    VAL(item), VAL(gvec_read(item,0)) );
	return;
    }
#ifndef SKIP_TEMPLATE_SWIZZLING

    in_part = gvec_read( item, SLOT(1) );
    /* if the "in_part" is a pointer, then it is presumably
       a pointer to a <part-descr> which we created because
       the part so referred to doesn't exist, in which case we
       should leave the <template> as it is (no wierd swizzling
       of code pointers)
     */

    if (!OBJ_ISA_PTR(in_part) && !EQ(in_part,NO_PART_TOKEN))
      {
	part_ptr = (struct part_descr *) OBJ_TO_RAW_PTR(in_part);
	
	if (!part_ptr)
	  {
	    fprintf( stderr, "Error: %#x (TEMPLATE) has no part ptr\n",
		 VAL(item) );
	    abort();  /* really oughtn't to have gotten here */
	  }
	ix = fx2int( gvec_read( item, SLOT(0) ) );
	fn = part_ptr->functions[ix];
	entry_point = fn->monotones[0];
	
#if 0
	printf( "template %#x ==> {%d.%d.0} ==> ", 
	       VAL(item), part_ptr->tag, ix );
	printf( " %#x\n", entry_point );
#endif
	gvec_write_fresh_non_ptr( item, 
				  SLOT(0), 
				  JUMP_ADDR_TO_OBJ(entry_point) );
	gvec_write_fresh_non_ptr( item, 
				  SLOT(1), 
				  RAW_PTR_TO_OBJ(fn) );
      }
#if 0
else
  {
	printf( "template %#x ==> in_part %#x\n", VAL(item), VAL(in_part) );
      }
#endif
#endif /* SKIP_TEMPLATE_SWIZZLING */
}

static void swizzle_partcont( obj item )
{
    assert( OBJ_ISA_PTR(item) );
    swizzle_gvec( item );
}

/* options is either #f or the class of used to create 
   <part-descr>'s for unresolved parts
*/

obj load_image_file( const char *path, 
		     obj the_reference_table, 
		     obj options,
		     int *pformat_version )
{
struct file_header *hdr;
UINT_32 i, *input_ptr, *input_buffer;
obj result;
int format_version;

    pd_class = options;

#ifdef LOADIMAGE_TRACE
    debug_file = fopen( "/tmp/loadimg.trc", "w" );
    debug_binary_file = fopen( "/tmp/loadimg.bin", "wb" );
    target_comments = make_primitive_table();
    info_buf_ptr = NULL;
#endif /* LOADIMAGE_TRACE */
    if (!mapf_open( path ))
	return FALSE_OBJ;
	
    reference_table = the_reference_table;
    mapf_seek( FILE_HDR_OFFSET );
    hdr = (struct file_header *)mapf_read( sizeof( struct file_header ) );
#ifndef PLATFORM_IS_BIG_ENDIAN
    hdr->magic = BIG_ENDIAN_TO_HOST_32(hdr->magic);
    hdr->version = BIG_ENDIAN_TO_HOST_32(hdr->version);
    for (i=0; i<NUM_LOAD2_MODES; i++)
    {
	hdr->load2_offset[i] = BIG_ENDIAN_TO_HOST_32(hdr->load2_offset[i]);
	hdr->load2_count[i] = BIG_ENDIAN_TO_HOST_32(hdr->load2_count[i]);
    }
    hdr->data_area_length = BIG_ENDIAN_TO_HOST_32(hdr->data_area_length);
    hdr->num_objects = BIG_ENDIAN_TO_HOST_32(hdr->num_objects);
    hdr->entry_object = BIG_ENDIAN_TO_HOST_OBJ(hdr->entry_object);
#endif

    if (hdr->magic != IMAGE_MAGIC_NUMBER)
    {
	load_image_error( "Not an image file" );
    }
    
    remap_constants = NO;
    format_version = 0;

    switch (hdr->version)
    {
    case IMAGE_VERSION_06_BOOT:
      /* an 0.6 bootable image file */
      if (truish(the_reference_table))
	load_image_error( "Cannot load bootable into running system" );
      format_version = FMTV_RSCHEME_0_6_BOOT;
      break;

    case IMAGE_VERSION_06:
      /* 0.6-format image file */
      if (!truish(the_reference_table))
	load_image_error( "Cannot boot from regular image" );
      format_version = FMTV_RSCHEME_0_6;
      break;
      
    case IMAGE_VERSION_NUMBER:
      format_version = FMTV_RSCHEME_0_5;
      break;

	case 1:
		/* we can handle version 1 images by remapping
		    some small constants */
	    	remap_constants = YES;
		break;
	default:
	    load_image_error( "Version number is incorrect" );
    }
    
    obj_table = (obj *)malloc( sizeof(obj) * hdr->num_objects );
    if (!obj_table)
    {
	load_image_error( "Could not malloc space for object table" );
    }
    obj_table_limit = obj_table + hdr->num_objects;

    /*  Pass I. Allocate space for and 
    		load the data for the objects
    		(and execute LOAD1 commands, involving
		 table lookups (REF and SYMBOL), and convert
		 endianess) */

    input_buffer = (UINT_32 *)mapf_read( hdr->data_area_length );
    input_ptr = input_buffer;

    loading_image = YES;
    
    for (i=0; i<hdr->num_objects; i++)
    {
    UINT_32 len;
    enum load1_mode mode1;
    obj preamble, item;
    void *p;
    
	len = BIG_ENDIAN_TO_HOST_32( input_ptr[0] );
	mode1 = (enum load1_mode) BIG_ENDIAN_TO_HOST_32( input_ptr[1] );
	preamble = BIG_ENDIAN_TO_HOST_OBJ( OBJ(input_ptr[2]) );
	p = &input_ptr[3];
#ifdef LOADIMAGE_TRACE
	fprintf( debug_file, 
		"Loading %#x := mode(I) %d (%u bytes) preamble %#x",
		(i*4+3), mode1, len, VAL(preamble) );
	comment_str = NULL;
#endif /* LOADIMAGE_TRACE */
	switch (mode1)
	{
	    case LOAD1_REF:	item = load_ref( p, len, preamble );
	    			break;
	    case LOAD1_SYMBOL:	item = load_symbol( p, len, preamble );
	    			break;
	    case LOAD1_PART:	item = load_part( p, len, preamble );
	    			break;
	    case LOAD1_ARRAY8:	item = load_array_8( p, len, preamble );
	    			break;
	    case LOAD1_ARRAY16:	item = load_array_16( p, len, preamble );
	    			break;
	    case LOAD1_ARRAY32:	item = load_array_32( p, len, preamble );
	    			break;
	    case LOAD1_ARRAY64:	item = load_array_64( p, len, preamble );
	    			break;
	    default:		item = ZERO;
	    			assert( !EQ(item,ZERO) );
				break;
	}
#ifdef LOADIMAGE_TRACE
	if (comment_str)
	{
	obj key = int2fx(i);
	
	    hashtable_install( target_comments, 
	    			key,
				key,
	    			make_string(comment_str) );
	    fprintf( debug_file, " %s", comment_str );
	}
        fprintf( debug_file, " := %#x\n", VAL(item) );
#endif /* LOADIMAGE_TRACE */
	obj_table[i] = item;
	input_ptr = (UINT_32 *)(((char *)input_ptr) + ((len + 3 + 12) & ~3));
    }
#ifdef LOADIMAGE_TRACE
    fflush( debug_file );
#endif /* LOADIMAGE_TRACE */

    /*  Pass II.  Go through the LOAD2 commands, executing
    		  them in order
    */
    
    for (i=0; i<NUM_USEFUL_LOAD2_MODES; i++)
    {
    enum load2_mode mode2 = useful_load2_modes[i].mode;
    swizzler_fn *proc = useful_load2_modes[i].swizzler;
    obj *table;
    UINT_32 j, count;
    
	count = hdr->load2_count[mode2];
#ifdef LOADIMAGE_TRACE
        fprintf( debug_file, "** LOAD2 (mode2 = %d): %u\n",
		             mode2,
		             count );
#endif
        if (count == 0)
	  continue;

	table = (obj *)mapf_read( sizeof(obj) * count );

#if defined(LOADIMAGE_TRACE) && defined(PAUSE_IN_MODE2)
	if (i > 0)
	{ 
	char temp[10];
	UINT_32 n = count;
	int getpid( void );

	    if (n > 20)
		n = 20;
		
	    boot_list = alloc( SLOT(n), ZERO );
	    for (j=0; j<n; j++)
		gvec_write( boot_list, SLOT(j), swizzle(table[j]) );
	    printf( "-m %d %d -- about to load %u in mode(II) %d?", 
			    getpid(), (int)master_table, count,  mode2 );
	    gets(temp);
	}
#endif /* LOADIMAGE_TRACE */

	for (j=0; j<count; j++)
	{
	obj item = swizzle(BIG_ENDIAN_TO_HOST_OBJ( table[j] ));

#ifdef LOADIMAGE_TRACE
	    reset_trace_info();
	    tri_printf( "Loading mode(II) %d %#x\n", mode2, VAL(item) );
	    tri_swizzled( -2, BIG_ENDIAN_TO_HOST_OBJ( table[j] ), item );
#endif /* LOADIMAGE_TRACE */
	    proc( item );
#ifdef LOADIMAGE_TRACE
	    tri_printf( "done\n" );
	    write_trace_info( item, mode2 );
#endif /* LOADIMAGE_TRACE */
	}
    }
    result = swizzle( hdr->entry_object );
#ifdef LOADIMAGE_TRACE
    fclose( debug_file );
    fclose( debug_binary_file );
#endif /* LOADIMAGE_TRACE */
    mapf_close();
    free( obj_table );

    loading_image = NO;
    *pformat_version = format_version;
    return result;
}

obj load_ref( void *src, UINT_32 len, obj preamble )
{
obj str = load_array_8( src, len, string_class );
obj item;

#ifdef LOADIMAGE_TRACE
    sprintf( info_buffer, "(REF '%s')", string_text(str) );
    comment_str = info_buffer;
#endif /* LOADIMAGE_TRACE */

    /* in the future, this will not be computed here... */
    preamble = raw_bytes_hash( src, len-1 );
    /* assert( EQ(preamble,raw_bytes_hash( src, len-1 )) ); */

    /* note that you can't bind a REF to #f, because we
       would think that it's not bound! */
  
    item = stringtable_lookup( reference_table, preamble, str );
    if (EQ(item,FALSE_OBJ))
    {
	fprintf( stderr, "REF `%s' not bound\n", (char *)src );
    }
    return item;
}

obj load_symbol( void *src, UINT_32 len, obj preamble )
{
obj str = load_array_8( src, len, string_class );

#ifdef LOADIMAGE_TRACE
    sprintf( info_buffer, " (SYMBOL '%s')", string_text(str) );
    comment_str = info_buffer;
#endif /* LOADIMAGE_TRACE */
    return intern( str );
}

obj load_part( void *src, UINT_32 len, obj preamble )
{
obj objp;
void *p;

    /* preamble is the part's tag,
       and the string is the name of the module.
       Returns a pointer to the part_descr (which is NOT
       a heap object, note) */
    assert( OBJ_ISA_FIXNUM(preamble) );
    if (!EQ(pd_class,FALSE_OBJ))
      {
	return make3( pd_class, 
		      make_string((char *)src),
		      preamble,
		      FALSE_OBJ );
      }
    
    p = find_a_part( (const char *)src, fx2int(preamble) );

/*printf( "load part: {%s.%d} => %#x\n", src, fx2int(preamble), p );*/

    if (!p)
      {
	/* the part isn't defined */
	fprintf( stderr, 
		 "Module %s has no part with tag %d\n",
		 (char *)src, fx2int(preamble) );
	objp = NO_PART_TOKEN;
      }
    else
      {
	objp = RAW_PTR_TO_OBJ(p);
	assert( !OBJ_ISA_PTR(objp) );
      }
#ifdef LOADIMAGE_TRACE
    if (p)
    {
	if (EQ(preamble,ZERO))
	    sprintf( info_buffer, 
		    " (PART %s)", ((struct module_descr *)p)->name );
	else
	    sprintf( info_buffer, 
		    " (PART %s, %s)", 
		    ((struct part_descr *)p)->in_module->name, 
		    ((struct part_descr *)p)->name );
    }
    else
	sprintf( info_buffer, " (NO PART)");
    comment_str = info_buffer;
#endif /* LOADIMAGE_TRACE */
    return objp;
}

obj load_array_8( void *src, UINT_32 len, obj preamble )
{
obj item = bvec_alloc( len, preamble );

    memcpy( PTR_TO_DATAPTR(item), src, len );
#ifdef LOADIMAGE_TRACE
    if (info_buf_ptr) /* might not be in mode II yet */
    {
    unsigned char *p = (unsigned char *)src;
    UINT_32 i, n;
    
	if (len > 64)
	    n = 64;
	else
	    n = len;
	tri_printf( "array8[%u] \"", len );
	for (i=0; i<len; i++)
	{
	unsigned char ch = *p++;
	
	    if ((ch >= 32) && (ch < 127))
		*info_buf_ptr++ = ch;
	    else
	    {
		*info_buf_ptr++ = '.';
	    }
	}
	if (len > 64)
	    tri_printf( "..." );
	*info_buf_ptr++ = '\"';
	*info_buf_ptr++ = '\n';
    }
#endif /* LOADIMAGE_TRACE */
    return item;
}

obj load_array_16( void *src, UINT_32 len, obj preamble )
{
obj item = bvec_alloc( len, preamble );
UINT_16 *s, *d, *limit;

    assert( (len & (sizeof(UINT_16)-1)) == 0 );
    s = (UINT_16 *)src;
    limit = (UINT_16 *)(len + (char *)s);
    
    d = (UINT_16 *)PTR_TO_DATAPTR(item);

    while (s < limit)
	*d++ = BIG_ENDIAN_TO_HOST_16(*s++);

    return item;
}

obj load_array_32( void *src, UINT_32 len, obj preamble )
{
obj item = alloc( len, preamble );
UINT_32 *s, *d, *limit;

    assert( (len & (sizeof(UINT_32)-1)) == 0 );
    s = (UINT_32 *)src;
    limit = (UINT_32 *)(len + (char *)s);
    d = (UINT_32 *)PTR_TO_DATAPTR(item);

    while (s < limit)
	*d++ = BIG_ENDIAN_TO_HOST_32(*s++);

    return item;
}

obj load_array_64( void *src, UINT_32 len, obj preamble )
{
obj item = alloc( len, preamble );
UINT_32 *d32, *s32, *limit32;

    assert( (len & (sizeof(IEEE_64)-1)) == 0 );
    s32 = (UINT_32 *)src;
    d32 = (UINT_32 *)PTR_TO_DATAPTR(item);
    limit32 = (UINT_32 *)(len + (char *)s32);

    while (s32 < limit32)
    {
	*d32++ = *s32++;
	*d32++ = *s32++;
    }
#ifndef PLATFORM_IS_BIG_ENDIAN
    {
    IEEE_64 *d, *limit;
	
	d = (IEEE_64 *)PTR_TO_DATAPTR(item);
	limit = (IEEE_64 *)(len + (char *)d);
    
	while (d < limit)
	{
	    *d = BIG_ENDIAN_TO_HOST_IEEE_64(*d);
	    d++;
	}
    }
#endif
    return item;
}

static void *find_a_part( const char *module, int tag )
{
  struct module_descr *m;

  m  = find_module( module ); /* handles "foo|/lib/libfoo.so" notation */

  if (tag)
    {
      if (m)
	return (void *)find_part( m, tag );
      else
	return NULL;
    }
  else
    {
      return (void *)m;
    }
}