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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
|
//==========================================================================
//
// loader.cxx
//
// Loader class implementation
//
//==========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later
// version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eCos; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// As a special exception, if other files instantiate templates or use
// macros or inline functions from this file, or you compile this file
// and link it with other works to produce a work based on this file,
// this file does not by itself cause the resulting work to be covered by
// the GNU General Public License. However the source code for this file
// must still be made available in accordance with section (3) of the GNU
// General Public License v2.
//
// This exception does not invalidate any other reasons why a work based
// on this file might be covered by the GNU General Public License.
// -------------------------------------------
// ####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): nickg
// Contributors: nickg
// Date: 2000-11-03
// Purpose: Loader class implementation
// Description: This file contains the implementation of the ELF loader
// classes.
//
//
//
//####DESCRIPTIONEND####
//
//==========================================================================
#include <pkgconf/hal.h>
#include <pkgconf/kernel.h>
#include <pkgconf/isoinfra.h>
#include <cyg/kernel/ktypes.h> // base kernel types
#include <cyg/infra/cyg_trac.h> // tracing macros
#include <cyg/infra/cyg_ass.h> // assertion macros
#include <string.h>
#include <cyg/loader/loader.hxx> // our header
#if CYGINT_ISO_MALLOC
#include <stdlib.h> // for malloc() etc
#endif
// ----------------------------------------------------------------------------
#ifdef CYGPKG_LIBC_STRING
#define streq( a, b ) (strcmp(a,b) == 0)
#else
static int streq( const char *s1, const char *s2 )
{
while( *s1 == *s2 && *s1 && *s2 ) s1++,s2++;
return !(*s2-*s1);
}
#endif
// ----------------------------------------------------------------------------
// new operator to allow us to invoke constructors on previously allocated
// memory.
inline void *operator new(size_t size, void *ptr) { return ptr; };
// =========================================================================
// Static objects
// Default memory allocator
static Cyg_LoaderMemAlloc memalloc;
// Loader object
static Cyg_Loader loader(&memalloc);
Cyg_Loader *Cyg_Loader::loader = &::loader;
// =========================================================================
// Main loader class members
// -------------------------------------------------------------------------
// Constructor
Cyg_Loader::Cyg_Loader( Cyg_LoaderMemAlloc *memalloc )
{
CYG_REPORT_FUNCTION();
error = 0;
mem_default = memalloc;
// build an object for the main program
Cyg_LoaderMemBlock *obj = mem_default->alloc( sizeof(Cyg_LoadObject));
main = new(obj->address) Cyg_LoadObject( );
// Add to load list
loadlist.add_head(main);
error = main->get_error();
CYG_REPORT_RETURN();
}
// -------------------------------------------------------------------------
// Destructor
Cyg_Loader::~Cyg_Loader()
{
CYG_REPORT_FUNCTION();
CYG_REPORT_RETURN();
}
// -------------------------------------------------------------------------
// Load an object and all its dependencies.
cyg_code Cyg_Loader::load( Cyg_LoaderStream& stream,
cyg_uint32 mode,
Cyg_LoadObject **object)
{
CYG_REPORT_FUNCTION();
CYG_REPORT_FUNCARG3XV( &stream, mode, object );
cyg_code error = 0;
Cyg_LoaderMemBlock *obj = mem_default->alloc( sizeof(Cyg_LoadObject));
Cyg_LoadObject *pobj = NULL;
pobj = new(obj->address) Cyg_LoadObject( stream, mode, mem_default, obj );
error = pobj->get_error();
if( error != 0 )
goto finish;
// Add this object to list before we do any relocations to make
// the symbol lookups work.
loadlist.add_tail(pobj);
// The object is now loaded. We must now do any relocations.
pobj->relocate();
error = pobj->get_error();
if( error != 0 )
goto finish;
// Handle PLT relocations if we are told to do so
// We always do this for now..
// if( mode & RTLD_NOW )
pobj->relocate_plt();
error = pobj->get_error();
finish:
if( error != 0 )
{
// remove object from list.
loadlist.remove( pobj );
pobj->~Cyg_LoadObject();
mem_default->free( obj );
}
else
{
// return it from this function.
*object = pobj;
}
CYG_REPORT_RETVAL(error);
return error;
}
// -------------------------------------------------------------------------
// Close object and remove it from memory
cyg_code Cyg_Loader::close( Cyg_LoadObject *object )
{
CYG_REPORT_FUNCTION();
cyg_code error = 0;
Cyg_LoaderMemBlock *block = object->get_block();
object->~Cyg_LoadObject();
if( block )
block->free();
CYG_REPORT_RETVAL(error);
return error;
}
// -------------------------------------------------------------------------
// Translate current error code into a string.
const char *Cyg_Loader::error_string( )
{
CYG_REPORT_FUNCTION();
char *ret = "";
CYG_REPORT_RETVAL(ret);
return ret;
}
// -------------------------------------------------------------------------
// Look up a named symbol in loadlist
CYG_ADDRESS Cyg_Loader::hash_lookup_addr( const char *name )
{
CYG_ADDRESS addr = 0;
Cyg_LoadObject *object = loadlist.get_head();
do
{
addr = object->hash_lookup_addr( name );
if( addr != CYG_LOADER_NULLSYMADDR )
break;
object = object->get_next();
} while( object != loadlist.get_head() );
if( addr == CYG_LOADER_NULLSYMADDR )
error = CYG_LOADERR_NO_SYMBOL;
return addr;
}
// =========================================================================
// Loader Object class members
Cyg_LoadObject_Base::Cyg_LoadObject_Base()
{
e_type = ET_EXEC;
e_entry = 0;
base = 0;
dynamic = _DYNAMIC;
parse_dynamic( dynamic );
}
// -------------------------------------------------------------------------
// Constructor - reads and allocates the executable.
Cyg_LoadObject_Base::Cyg_LoadObject_Base( Cyg_LoaderStream& stream,
cyg_uint32 amode,
Cyg_LoaderMemAlloc *mem )
{
CYG_REPORT_FUNCTION();
CYG_REPORT_FUNCARG3XV( &stream, mode, mem );
Cyg_LoaderMemBlock *phblock = NULL;
Cyg_LoaderMemBlock *block = NULL;
Elf32_Phdr *phdr;
Elf32_Phdr *dynhdr = NULL;
cyg_uint32 memsize = 0;
cyg_uint32 maxalign = 0;
CYG_BYTE *memaddr;
Elf32_Addr vaddr_low = 0x7FFFFFFF;
Elf32_Addr vaddr_hi = 0;
mode = amode;
memalloc = mem;
error = CYG_LOADERR_NOERROR;
// OK, let's start by getting the ELF header...
Elf32_Ehdr elf_hdr;
error = stream.get_data( (CYG_BYTE *)&elf_hdr, sizeof( elf_hdr ) );
if( error != 0 )
goto finish;
// Check that this is a valid ELF file and that the various header
// fields match what we expect for our current architecture and
// platform.
if( !IS_ELF( elf_hdr ) )
error = CYG_LOADERR_NOT_ELF;
else if( elf_hdr.e_ident[EI_CLASS] != ELFCLASS32 )
error = CYG_LOADERR_INVALID_CLASS;
#if CYG_BYTEORDER == CYG_LSBFIRST
else if( elf_hdr.e_ident[EI_DATA] != ELFDATA2LSB )
#else
else if( elf_hdr.e_ident[EI_DATA] != ELFDATA2MSB )
#endif
error = CYG_LOADERR_INVALID_BYTEORDER;
else if( elf_hdr.e_ident[EI_VERSION] != EV_CURRENT )
error = CYG_LOADERR_INVALID_VERSION;
else if( elf_hdr.e_machine != CYG_ELF_MACHINE )
error = CYG_LOADERR_INVALID_MACHINE;
else if( elf_hdr.e_version != EV_CURRENT )
error = CYG_LOADERR_INVALID_VERSION;
else if( elf_hdr.e_phentsize != sizeof(Elf32_Phdr) )
error = CYG_LOADERR_INVALID_VERSION;
if( error != 0 )
goto finish;
// OK that all seems in order, save some fields away for later.
e_type = elf_hdr.e_type;
e_entry = elf_hdr.e_entry;
// Now we must read the program header and prepare to read the
// object file into memory.
error = stream.seek( elf_hdr.e_phoff );
if( error != 0 )
goto finish;
// Allocate space for the header
phblock = memalloc->alloc( elf_hdr.e_phentsize * elf_hdr.e_phnum );
if( phblock == NULL )
{
error = CYG_LOADERR_NO_MEMORY;
goto finish;
}
error = stream.get_data( (CYG_BYTE *)phblock->address, sizeof(Elf32_Phdr)*elf_hdr.e_phnum );
if( error != 0 )
goto finish;
phdr = (Elf32_Phdr *)phblock->address;
// Loop over the program headers, totalling the sizes of the the
// PT_LOAD entries and saving a pointer to the PT_DYNAMIC entry
// when we find it.
// Since the segments must retain the same relationship to
// eachother in memory that their virtual addresses do in the
// headers, we determine the amount of memory needed by finding
// the extent of the virtual addresses covered by the executable.
for( int i = 0; i < elf_hdr.e_phnum; i++,phdr++ )
{
if( phdr->p_type == PT_DYNAMIC )
{
dynhdr = phdr;
continue;
}
if( phdr->p_type != PT_LOAD )
continue;
if( phdr->p_vaddr < vaddr_low )
vaddr_low = phdr->p_vaddr;
if( (phdr->p_vaddr+phdr->p_memsz) > vaddr_hi )
vaddr_hi = phdr->p_vaddr+phdr->p_memsz;
if( phdr->p_align > maxalign )
maxalign = phdr->p_align;
}
// Calculate how much memory we need and allocate it
memsize = vaddr_hi - vaddr_low;
block = memalloc->alloc( memsize, maxalign );
if( block == NULL )
{
error = CYG_LOADERR_NO_MEMORY;
goto finish;
}
// Attach to segments list
segs.add_tail( block );
// Calculate the base address for this executable. This is the
// difference between the actual address the executable is loaded
// at and its lowest virtual address. This value must be added to
// all addresses derived from the executable to relocate them into
// the real memory space.
base = (CYG_ADDRESS)block->address - vaddr_low;
// Loop over the program headers again, this time loading them
// into the memory segment we have allocated and clearing any
// unused areas to zero.
phdr = (Elf32_Phdr *)phblock->address;
memaddr = (CYG_BYTE *)block->address;
for( int i = 0; i < elf_hdr.e_phnum; i++,phdr++ )
{
if( phdr->p_type != PT_LOAD )
continue;
error = stream.seek( phdr->p_offset );
if( error != 0 ) break;
// Calculate the actual load address for this segment.
CYG_BYTE *loadaddr = (CYG_BYTE *)(phdr->p_vaddr + base);
error = stream.get_data( loadaddr, phdr->p_filesz );
if( error != 0 ) break;
// If the memory size is more than we got from the file, zero the remainder.
if( phdr->p_filesz < phdr->p_memsz )
memset( loadaddr+phdr->p_filesz,
0,
phdr->p_memsz-phdr->p_filesz );
}
dynamic = (Elf32_Dyn *)(dynhdr->p_vaddr + base);
parse_dynamic( dynamic );
finish:
if( phblock != NULL )
memalloc->free( phblock );
CYG_REPORT_RETURN();
}
// -------------------------------------------------------------------------
// Parse the dynamic segment
void Cyg_LoadObject_Base::parse_dynamic( Elf32_Dyn *dynamic )
{
CYG_REPORT_FUNCTION();
flags = 0;
for(;; dynamic++)
{
switch( dynamic->d_tag )
{
case DT_NULL: /* marks end of _DYNAMIC array */
return;
case DT_NEEDED: /* string table offset of needed lib */
break; // ignore for now
case DT_PLTRELSZ: /* size of relocation entries in PLT */
pltrelsz = dynamic->d_un.d_val;
break;
case DT_PLTGOT: /* address PLT/GOT */
pltgot = dynamic->d_un.d_ptr + base;
break;
case DT_HASH: /* address of symbol hash table */
hash = (Elf_Hash *)(dynamic->d_un.d_ptr + base);
bucket = (Elf32_Word *)(hash+1);
chain = bucket+hash->nbucket;
break;
case DT_STRTAB: /* address of string table */
strtab = (unsigned char *)(dynamic->d_un.d_ptr + base);
break;
case DT_SYMTAB: /* address of symbol table */
symtab = (Elf32_Sym *)(dynamic->d_un.d_ptr + base);
break;
case DT_RELA: /* address of relocation table */
rela = (Elf32_Rela *)(dynamic->d_un.d_ptr + base);
break;
case DT_RELASZ: /* size of relocation table */
relasize = dynamic->d_un.d_val;
break;
case DT_RELAENT: /* size of relocation entry */
relaent = dynamic->d_un.d_val;
break;
case DT_STRSZ: /* size of string table */
strsize = dynamic->d_un.d_val;
break;
case DT_SYMENT: /* size of symbol table entry */
syment = dynamic->d_un.d_val;
break;
case DT_INIT: /* address of initialization func. */
init = dynamic->d_un.d_ptr + base;
break;
case DT_FINI: /* address of termination function */
fini = dynamic->d_un.d_ptr + base;
break;
case DT_SONAME: /* string table offset of shared obj */
soname = dynamic->d_un.d_val;
break;
case DT_SYMBOLIC: /* start sym search in shared obj. */
flags |= DF_SYMBOLIC;
break;
case DT_REL: /* address of rel. tbl. w addends */
rel = (Elf32_Rel *)(dynamic->d_un.d_ptr + base);
break;
case DT_RELSZ: /* size of DT_REL relocation table */
relsize = dynamic->d_un.d_val;
break;
case DT_RELENT: /* size of DT_REL relocation entry */
relent = dynamic->d_un.d_val;
break;
case DT_PLTREL: /* PLT referenced relocation entry */
pltrel = dynamic->d_un.d_val;
break;
case DT_DEBUG: /* Debug data */
break; /* ignore for now */
case DT_TEXTREL: /* Allow rel. mod. to unwritable seg */
flags |= DF_TEXTREL;
break;
case DT_JMPREL: /* add. of PLT's relocation entries */
jmprel = dynamic->d_un.d_ptr + base;
break;
case DT_BIND_NOW: /* Bind now regardless of env setting */
flags |= DF_BIND_NOW;
break;
case DT_INIT_ARRAY: /* init array address */
init_array = (Elf32_Addr *)(dynamic->d_un.d_ptr + base);
break;
case DT_FINI_ARRAY: /* fini array address */
fini_array = (Elf32_Addr *)(dynamic->d_un.d_ptr + base);
break;
case DT_INIT_ARRAYSZ: /* init array size */
init_array_sz = dynamic->d_un.d_val;
break;
case DT_FINI_ARRAYSZ: /* fini array size */
fini_array_sz = dynamic->d_un.d_val;
break;
case DT_FLAGS: /* flags */
flags |= dynamic->d_un.d_val;
break;
case DT_PREINIT_ARRAY: /* preinit array address */
pre_init_array = (Elf32_Addr *)(dynamic->d_un.d_ptr + base);
break;
case DT_PREINIT_ARRAYSZ: /* preinit array size */
pre_init_array_sz = dynamic->d_un.d_val;
break;
default:
// handle format-specific entries
break;
}
}
CYG_REPORT_RETURN();
}
// -------------------------------------------------------------------------
// Get the symbol name from the current object's symbol table, look it
// up in the hash tables of the loaded objects, and return its address.
CYG_ADDRESS Cyg_LoadObject_Base::get_sym_addr_from_ix( Elf32_Word ix )
{
Elf32_Sym *sym = get_sym( ix );
if( sym == NULL ) return 0;
const char *name = get_name( sym->st_name );
// If the symbol has local binding, we must look for
// it in this object only.
if( ELF32_ST_BIND(sym->st_info) == STB_LOCAL )
return hash_lookup_addr( name );
// Otherwise search the loaded objects in load order
return Cyg_Loader::loader->hash_lookup_addr( name );
}
// -------------------------------------------------------------------------
// Lookup the name in our hash table and return the symbol table entry
Elf32_Sym *Cyg_LoadObject_Base::hash_lookup( const char *name )
{
Elf32_Sym *ret = NULL;
if( hash == NULL )
{
error = CYG_LOADERR_NO_HASHTABLE;
return NULL;
}
error = CYG_LOADERR_NO_SYMBOL;
Elf32_Word ix = elf_hash( (const unsigned char *)name );
ix %= hash->nbucket;
// get head of chain
Elf32_Word iy = bucket[ ix ];
while( iy != STN_UNDEF )
{
Elf32_Sym *sym = get_sym( iy );
const char *sname = get_name( sym->st_name );
if( streq( name, sname ) )
{
ret = sym;
error = CYG_LOADERR_NOERROR;
break;
}
iy = chain[ iy ];
}
return ret;
}
// -------------------------------------------------------------------------
// Lookup the given name in our symbol table and return it's value
// relocated to our load address.
CYG_ADDRESS Cyg_LoadObject_Base::hash_lookup_addr( const char *name )
{
Elf32_Sym *sym = hash_lookup( name );
if( sym == NULL )
return CYG_LOADER_NULLSYMADDR;
// Check that this symbol is for a defined object, if its type is
// NOTYPE then it is undefined, here, and we cannot take its address.
// Hopefully it is defined in some other object.
if( ELF32_ST_TYPE(sym->st_info) == STT_NOTYPE )
{
error = CYG_LOADERR_NO_SYMBOL;
return CYG_LOADER_NULLSYMADDR;
}
return sym->st_value + base;
}
// -------------------------------------------------------------------------
// ELF hash function
// This is the standard hash function used for indexing the bucket
// array in the hash table.
unsigned long Cyg_LoadObject_Base::elf_hash( const unsigned char *name )
{
unsigned long h = 0, g;
while( *name )
{
h = ( h << 4 ) + *name++;
if( (g = h & 0xf0000000) != 0 )
h ^= g >> 24;
h &= ~g;
}
return h;
}
// -------------------------------------------------------------------------
//
Elf32_Sym *Cyg_LoadObject_Base::get_sym( Elf32_Word ix )
{
if( symtab == NULL )
return NULL;
return &symtab[ix];
}
char *Cyg_LoadObject_Base::get_name( Elf32_Word offset )
{
if( strtab == NULL || offset > strsize )
return NULL;
return (char *)(&strtab[offset]);
}
// -------------------------------------------------------------------------
// Destructor
// -------------------------------------------------------------------------
// Cyg_LoadObject_Base destructor
Cyg_LoadObject_Base::~Cyg_LoadObject_Base()
{
CYG_REPORT_FUNCTION();
// empty out segments list
while( !segs.empty() )
{
Cyg_LoaderMemBlock *block = segs.rem_head();
block->free();
}
CYG_REPORT_RETURN();
}
// -------------------------------------------------------------------------
// Translate a symbol into its address.
void *Cyg_LoadObject_Base::symbol( const char *name )
{
CYG_REPORT_FUNCTION();
Elf32_Addr addr = hash_lookup_addr( name );
if( addr == CYG_LOADER_NULLSYMADDR )
addr = 0;
CYG_REPORT_RETVAL( addr );
return (void *)addr;
}
// -------------------------------------------------------------------------
// Start the given executable object running
cyg_code Cyg_LoadObject_Base::exec(int argc, char **argv, char **envv)
{
CYG_REPORT_FUNCTION();
CYG_REPORT_FUNCARG3XV( argc, argv, envv );
cyg_code error = 0;
CYG_REPORT_RETVAL(error);
return error;
}
// =========================================================================
// Cyg_LoadObject members
// -------------------------------------------------------------------------
// Apply relocations
void Cyg_LoadObject::relocate()
{
CYG_REPORT_FUNCTION();
if( rel != NULL )
{
Elf32_Rel *r = rel;
for( int i = relsize; i > 0 && error == 0; i -= relent, r++ )
error = apply_rel( ELF32_R_TYPE(r->r_info),
ELF32_R_SYM(r->r_info),
r->r_offset);
}
if( error == 0 && rela != NULL )
{
Elf32_Rela *r = rela;
for( int i = relasize; i > 0 && error == 0; i -= relaent, r++ )
error = apply_rela( ELF32_R_TYPE(r->r_info),
ELF32_R_SYM(r->r_info),
r->r_offset,
r->r_addend);
}
CYG_REPORT_RETURN();
}
// -------------------------------------------------------------------------
// Apply JMPREL relocations for the PLT
void Cyg_LoadObject::relocate_plt()
{
CYG_REPORT_FUNCTION();
if( pltrel == DT_REL )
{
Elf32_Rel *r = (Elf32_Rel *)jmprel;
for( int i = pltrelsz; i > 0 && error == 0; i -= sizeof(Elf32_Rel), r++ )
error = apply_rel( ELF32_R_TYPE(r->r_info),
ELF32_R_SYM(r->r_info),
r->r_offset);
}
if( error == 0 && pltrel == DT_RELA )
{
Elf32_Rela *r = (Elf32_Rela *)jmprel;
for( int i = pltrelsz; i > 0 && error == 0; i -= sizeof(Elf32_Rela), r++ )
error = apply_rela( ELF32_R_TYPE(r->r_info),
ELF32_R_SYM(r->r_info),
r->r_offset,
r->r_addend);
}
CYG_REPORT_RETURN();
}
// =========================================================================
// Loader memory allocator default class methods.
// The default behaviour of this class is to use malloc/realloc/free
// to handle memory.
// -------------------------------------------------------------------------
Cyg_LoaderMemAlloc::Cyg_LoaderMemAlloc()
{
// no initialization needed
}
// -------------------------------------------------------------------------
Cyg_LoaderMemAlloc::~Cyg_LoaderMemAlloc()
{
// No destruction needed
}
// -------------------------------------------------------------------------
// Allocate memory of the supplied size and alignment.
// size - size in bytes
// alignment - alignment expressed as a power of 2
Cyg_LoaderMemBlock *Cyg_LoaderMemAlloc::alloc( cyg_int32 size,
cyg_int32 alignment)
{
#if CYGINT_ISO_MALLOC
Cyg_LoaderMemBlock *block;
cyg_uint8 *mem;
cyg_uint32 acsize = sizeof(Cyg_LoaderMemBlock) + size + alignment;
mem = (cyg_uint8 *)::malloc( acsize );
if( mem == NULL )
return NULL;
block = (Cyg_LoaderMemBlock *)mem;
// set up aligned block address
block->address = (void *)((((CYG_ADDRWORD)mem+sizeof(Cyg_LoaderMemBlock))+alignment) & ~(alignment-1));
block->size = size;
block->alignment = alignment;
block->mem = this;
block->actual_address = (CYG_ADDRESS) mem;
block->actual_size = acsize;
return block;
#else
return 0;
#endif
}
// -------------------------------------------------------------------------
// Reallocate block
Cyg_LoaderMemBlock *Cyg_LoaderMemAlloc::realloc( Cyg_LoaderMemBlock *oblock,
cyg_int32 size,
cyg_int32 alignment)
{
#if CYGINT_ISO_MALLOC
Cyg_LoaderMemBlock *block;
cyg_uint8 *mem;
if( alignment == -1 )
alignment = oblock->alignment;
cyg_uint32 acsize = sizeof(Cyg_LoaderMemBlock) + size + alignment;
mem = (cyg_uint8 *)::realloc( (void *)(oblock->actual_address), acsize );
if( mem == NULL )
return NULL;
block = (Cyg_LoaderMemBlock *)mem;
// set up aligned block address
block->address = (void *)((((CYG_ADDRWORD)mem+sizeof(Cyg_LoaderMemBlock))+alignment) & (alignment-1));
block->size = size;
block->alignment = alignment;
block->mem = this;
block->actual_address = (CYG_ADDRESS) mem;
block->actual_size = acsize;
return block;
#else
return NULL;
#endif
}
// -------------------------------------------------------------------------
// Free a previously allocated memory segment.
void Cyg_LoaderMemAlloc::free( Cyg_LoaderMemBlock *block )
{
#if CYGINT_ISO_MALLOC
::free( (void *)block->actual_address );
#endif
}
// =========================================================================
// Loader stream functions
Cyg_LoaderStream::Cyg_LoaderStream()
{
}
Cyg_LoaderStream::~Cyg_LoaderStream()
{
}
cyg_code Cyg_LoaderStream::get_byte(CYG_BYTE *val)
{
return CYG_LOADERR_EOF;
}
cyg_code Cyg_LoaderStream::get_data(CYG_BYTE *addr, CYG_ADDRWORD size)
{
return CYG_LOADERR_EOF;
}
cyg_code Cyg_LoaderStream::seek(CYG_ADDRWORD pos)
{
return CYG_LOADERR_SEEK;
}
// =========================================================================
// Memory based loader stream
Cyg_LoaderStream_Mem::Cyg_LoaderStream_Mem( const void *addr, cyg_int32 size )
{
base = pos = (CYG_ADDRESS)addr;
end = base + size;
}
Cyg_LoaderStream_Mem::~Cyg_LoaderStream_Mem()
{
// nothing to do
}
cyg_code Cyg_LoaderStream_Mem::get_byte(CYG_BYTE *val)
{
if( pos == end )
return CYG_LOADERR_EOF;
*val = *(CYG_BYTE *)pos;
pos++;
return CYG_LOADERR_NOERROR;
}
cyg_code Cyg_LoaderStream_Mem::get_data(CYG_BYTE *addr, CYG_ADDRWORD size)
{
if( pos == end || (pos+size) > end )
return CYG_LOADERR_EOF;
memcpy( (void *)addr, (void *)pos, size );
pos += size;
return CYG_LOADERR_NOERROR;
}
cyg_code Cyg_LoaderStream_Mem::seek(CYG_ADDRWORD apos)
{
CYG_ADDRWORD npos = base+apos;
if( npos > end || npos < base )
return CYG_LOADERR_SEEK;
pos = npos;
return CYG_LOADERR_NOERROR;
}
// =========================================================================
// file based loader stream
#ifdef CYGPKG_IO_FILEIO
#include <unistd.h>
Cyg_LoaderStream_File::Cyg_LoaderStream_File( int afd )
{
fd = afd;
}
Cyg_LoaderStream_File::~Cyg_LoaderStream_File()
{
// nothing to do
fd = 0;
}
cyg_code Cyg_LoaderStream_File::get_byte(CYG_BYTE *val)
{
ssize_t got = read( fd, (void *)val, 1 );
if( got == 0 )
return CYG_LOADERR_EOF;
return CYG_LOADERR_NOERROR;
}
cyg_code Cyg_LoaderStream_File::get_data(CYG_BYTE *addr, CYG_ADDRWORD size)
{
ssize_t got = read( fd, (void *)addr, size );
if( got != (ssize_t)size )
return CYG_LOADERR_EOF;
return CYG_LOADERR_NOERROR;
}
cyg_code Cyg_LoaderStream_File::seek(CYG_ADDRWORD apos)
{
off_t npos = lseek( fd, apos, SEEK_SET );
if( npos != apos )
return CYG_LOADERR_SEEK;
return CYG_LOADERR_NOERROR;
}
#endif
// =========================================================================
// EOF loader.cxx
|