-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcexpmod.c
1059 lines (894 loc) · 23.2 KB
/
cexpmod.c
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
/* Implementation of cexp modules */
/* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*
* Authorship
* ----------
* This software (CEXP - C-expression interpreter and runtime
* object loader/linker) was created by
*
* Till Straumann <[email protected]>, 2002-2008,
* Stanford Linear Accelerator Center, Stanford University.
*
* Acknowledgement of sponsorship
* ------------------------------
* This software was produced by
* the Stanford Linear Accelerator Center, Stanford University,
* under Contract DE-AC03-76SFO0515 with the Department of Energy.
*
* Government disclaimer of liability
* ----------------------------------
* Neither the United States nor the United States Department of Energy,
* nor any of their employees, makes any warranty, express or implied, or
* assumes any legal liability or responsibility for the accuracy,
* completeness, or usefulness of any data, apparatus, product, or process
* disclosed, or represents that its use would not infringe privately owned
* rights.
*
* Stanford disclaimer of liability
* --------------------------------
* Stanford University makes no representations or warranties, express or
* implied, nor assumes any liability for the use of this software.
*
* Stanford disclaimer of copyright
* --------------------------------
* Stanford University, owner of the copyright, hereby disclaims its
* copyright and all other rights in this software. Hence, anyone may
* freely use it for any purpose without restriction.
*
* Maintenance of notices
* ----------------------
* In the interest of clarity regarding the origin and status of this
* SLAC software, this and all the preceding Stanford University notices
* are to remain affixed to any copy or derivative of this software made
* or distributed by the recipient and are to be affixed to any copy of
* software made or distributed by the recipient that contains a copy or
* derivative of this software.
*
* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <cexp_regex.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* FIXME: would like to use uintptr_t but some RTEMS versions
* make this long long (64-bit) even on a 32-bit machine :-(
* which makes the output look bad. Hopefully this can
* be fixed in the future...
#include <inttypes.h>
*/
typedef unsigned long myuintptr_t;
typedef long myintptr_t;
#define MYPRIxPTR "lx"
#include "cexpmodP.h"
#include "cexpsymsP.h"
#include "cexplock.h"
#include "cexpsegsP.h"
#include "cexpveneerP.h"
#define _INSIDE_CEXP_
#include "cexpHelp.h"
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#ifdef USE_PMBFD
#include <pmelf.h>
#ifdef USE_LOADER
#include <pmbfd.h>
#endif
#endif
#ifdef HAVE_BFD_DISASSEMBLER
/* Oh well; rtems/score/types.h defines boolean and bfd
* defines boolean as well :-( Can't you people use names
* less prone to clashes???
* We redefine bfd's boolean here
*/
#define boolean bfdddd_bbboolean
#include <bfd.h>
#undef boolean
extern void cexpDisassemblerInstall(bfd *abfd);
#endif
CexpModule cexpSystemModule=0;
/* change this whenever the layout of internal data changes */
char *
cexpMagicString = CEXPMOD_MAGIC;
static CexpRWLockRec _rwlock;
#define __RLOCK() cexpReadLock(&_rwlock)
#define __RUNLOCK() cexpReadUnlock(&_rwlock)
#define __WLOCK() cexpWriteLock(&_rwlock)
#define __WUNLOCK() cexpWriteUnlock(&_rwlock)
void
cexpModuleInitOnce(void)
{
memset(&_rwlock, 0, sizeof(_rwlock));
cexpRWLockInit(&_rwlock);
}
#ifdef USE_LOADER
/* Search predecessor of a module in the list.
* This should be called with the lock held!
*/
static CexpModule
predecessor(CexpModule mod)
{
register CexpModule pred;
if (!mod) return 0;
for (pred=cexpSystemModule; pred && mod!=pred->next; pred=pred->next)
/* nothing else to do */;
return pred;
}
#endif
/* verify the validity of a handle;
* should be called with the lock held
*/
static int
modIsStale(CexpModule mod)
{
register CexpModule m;
for (m=cexpSystemModule; m && m!=mod; m=m->next)
/* nothing else to do */;
return m==0;
}
/* search for a name in all module's symbol tables */
CexpSym
cexpSymLookup(const char *name, CexpModule *pmod)
{
CexpModule m;
CexpSym rval=0;
int index;
__RLOCK();
for (m=cexpSystemModule, index=0; m; m=m->next, index++) {
if ((rval=cexpSymTblLookup(name,m->symtbl)))
break;
}
if (pmod)
*pmod=m;
__RUNLOCK();
return rval;
}
static int addrInModule(void *addr, CexpModule m)
{
CexpSymTbl t;
#ifdef USE_LOADER
CexpSegment s;
if ( (s = m->segs) ) {
for ( ; s->name; s++ ) {
if ( ! s->chunk ) {
continue;
}
if ( (char*)addr >= (char*)s->chunk && (char*)addr < (char*)s->chunk + s->size )
return 1;
}
}
#endif
if ( m == cexpSystemModule ) {
t = m->symtbl;
/* assume system module is a single chunk (not allocated though) */
return addr >= (void*)t->aindex[0]->value.ptv && addr <= (void*)t->aindex[t->nentries-1]->value.ptv;
}
return 0;
}
static void *
gaddr(CexpSymAIdx ar)
{
return ar->mod->symtbl->aindex[ar->idx]->value.ptv;
}
void
cexpSymLkAddrRange(void *addr, CexpSymAIdx ar, int margin)
{
const int n = 2*margin + 1;
int i,cli,j;
void *tstaddr, *limaddr;
CexpSymAIdxRec thisone;
CexpModule m;
CexpSymTbl t;
for ( i=0; i<n; i++ )
ar[i].mod = 0;
__RLOCK();
for ( m = cexpSystemModule; m; m=m->next ) {
if ( !addrInModule(addr, m) )
continue;
t = m->symtbl;
thisone.mod = m;
cli = cexpSymTblLkAddrIdx(addr, 0, 0, t);
/* Look for all addresses in this module which are lower
* than the one we are looking for and which are closer
* than what we already have.
*/
for ( i = cli; i>=0; i-- ) {
thisone.idx = i;
tstaddr = gaddr( &thisone );
limaddr = ar[0].mod ? gaddr(&ar[0]) : (void*)0;
/* if there is no entry [0] then there is space; we set 'limaddr' == 0 above
* so the tstaddr < limaddr test can never succeed
*/
if ( tstaddr > addr || tstaddr < limaddr )
break;
for ( j=1; j<=margin && ( ! ar[j].mod || tstaddr > gaddr(&ar[j]) ); j++ ) {
ar[j-1] = ar[j];
}
ar[j-1].idx = i;
ar[j-1].mod = m;
}
for ( i = cli+1; i < t->nentries; i++ ) {
thisone.idx = i;
tstaddr = gaddr( &thisone );
limaddr = ar[n-1].mod ? gaddr(&ar[n-1]) : (void*)UINTPTR_MAX;
/* if there is no entry [n-1] then there is space;
* we set 'limaddr' == UINTPTR_MAX above
* so the tstaddr > limaddr test can never succeed
*/
if ( tstaddr <= addr || tstaddr > limaddr )
break;
for ( j=n-2; j>margin && (! ar[j].mod || tstaddr < gaddr(&ar[j]) ); j-- ) {
ar[j+1] = ar[j];
}
ar[j+1].idx = i;
ar[j+1].mod = m;
}
}
__RUNLOCK();
}
/* search for (the closest) address in all modules giving its
* aindex
*
* RETURNS: -1 if not found (i.e. within the boundaries of) any module.
*/
int
cexpSymLkAddrIdx(void *addr, int margin, FILE *f, CexpModule *pmod)
{
CexpSymAIdxRec ar[margin<0 ? 1 : 2*margin+1];
int i;
CexpModule mfnd = 0, m;
if ( margin < 0 || !f )
margin = 0;
cexpSymLkAddrRange(addr, ar, margin);
if ( f ) {
for ( i=0; i<2*margin+1; i++ ) {
if ( ! (m = ar[i].mod) )
continue;
if ( mfnd != m ) {
fprintf(f,"===== In module '%s' =====:\n",m->name);
mfnd = m;
}
cexpSymPrintInfo( m->symtbl->aindex[ar[i].idx], f );
}
}
if ( pmod )
*pmod = ar[margin].mod;
return ar[margin].mod ? ar[margin].idx : -1;
}
/* search for an address in all modules */
CexpSym
cexpSymLkAddr(void *addr, int margin, FILE *f, CexpModule *pmod)
{
int i;
CexpModule m;
if (!pmod)
pmod = &m;
i=cexpSymLkAddrIdx(addr,margin,f,pmod);
return i >= 0 ? (*pmod)->symtbl->aindex[i] : 0;
}
int
cexpAddrFind(void **addr, char *buf, int size)
{
CexpSym s;
CexpModule m;
s=cexpSymLkAddr(*addr,0,0,&m);
if (!s)
return -1;
if (size>0) {
snprintf(buf,size,"%s:%s",cexpModuleName(m),s->name);
/* don't know if snprintf puts a 0 there */
buf[size-1]=0;
}
*addr=s->value.ptv;
return 0;
}
/* return a module's name (string owned by module code) */
const char *
cexpModuleName(CexpModule mod)
{
return mod->name;
}
/* see comments in cexpsyms.c about this routine. The version
* here is just a wrapper for looping over modules
*/
CexpSym
_cexpSymLookupRegex(cexp_regex *rc, int *pmax, CexpSym s, FILE *f, CexpModule *pmod)
{
CexpModule m=0,mfound;
int max=24;
if (!pmax) pmax=&max;
if (pmod) {
/* start at module/symbol passed in */
m=*pmod;
if (s && !(++s)->name) {
/* was the last one */
s=0;
if (!(m=m->next))
return 0;
}
} else {
s=0;
}
if (!m) m=cexpSystemModule;
__RLOCK();
if (modIsStale(m)) {
__RUNLOCK();
fprintf(f ? f : stderr,"Got a stale module handle; giving up...\n");
return 0;
}
for (; m; m=m->next) {
if (!s) s=m->symtbl->syms;
mfound=0;
while (s->name && *pmax) {
if (cexp_regexec(rc,s->name)) {
if (!mfound) {
if (f) fprintf(f,"===== In module '%s' (0x%08"MYPRIxPTR") =====:\n",m->name, (myuintptr_t)m);
mfound=m; /* print module name only once */
(*pmax)--;
}
if (f) cexpSymPrintInfo(s,f);
if (--(*pmax) <= 0) {
if (pmod)
*pmod=m;
__RUNLOCK();
return s;
}
}
s++;
}
s=0;
}
__RUNLOCK();
return 0;
}
static void
bitmapInfo(FILE *f, BitmapWord *bm)
{
CexpModule m;
for (m=cexpSystemModule; m; m=m->next) {
if (BITMAP_TST(bm, m->id))
fprintf(f," %s",m->name);
}
}
CexpModule
cexpModIterate(CexpModule mod, FILE *f, void mcallback(CexpModule m, FILE *f, void *arg), void *arg)
{
CexpModule m = mod ? mod : cexpSystemModule;
if (!f) f=stdout;
__RLOCK();
if (modIsStale(m)) {
__RUNLOCK();
fprintf(f,"Got a stale module handle; giving up...\n");
return 0;
}
for (; m; m=m->next) {
mcallback(m, f, arg);
if (mod) {
mod = mod->next;
break; /* info only for the particular module requested */
}
}
__RUNLOCK();
return mod;
}
static void
modPrintInfo(CexpModule m, FILE *f, void *closure)
{
myintptr_t level = (myintptr_t)closure;
CexpSym *psects;
CexpSegment s;
fprintf(f,"Module '%s' (0x%08"MYPRIxPTR"):\n",
m->name, (myuintptr_t)m);
if ( level > 0 )
fprintf(f,"Full path '%s'\n",m->fileName);
if ( level > 1 ) {
fprintf(f," %li symbol table entries\n",
m->symtbl->nentries);
fprintf(f," %li bytes of memory allocated to binary\n",
m->memSize);
if ( m->segs ) {
fprintf(f,"Memory segment layout:\n");
for ( s=m->segs; s->name; s++ ) {
fprintf(f," %20s@0x%08lx (0x%08lx/%lu bytes)\n",
s->name,
(unsigned long)s->chunk,
s->size, s->size);
}
}
}
fprintf(f," Text starts at: 0x%08x\n",
(unsigned)m->text_vma);
if ( level > 0 ) {
fprintf(f," Needs:"); bitmapInfo(f,m->needs); fputc('\n',f);
fprintf(f," Needed by:"); bitmapInfo(f,m->neededby); fputc('\n',f);
}
if ( level > 2 && ( psects = m->section_syms ) ) {
fprintf(f," Section load info:\n");
for ( ; *psects; psects++ )
fprintf(f," @0x%08"MYPRIxPTR": %s\n", (myuintptr_t)(*psects)->value.ptv, (*psects)->name);
}
}
CexpModule
cexpModuleInfo(CexpModule mod, int level, FILE *f)
{
return cexpModIterate(mod, f, modPrintInfo, (void*)(myintptr_t)level);
}
static void
modPrintGdbSects(CexpModule m, FILE *f, void *closure)
{
const char *prefix = (const char*)closure;
CexpSym *psects;
if ( m == cexpSystemModule )
return;
if ( !prefix )
prefix="add-symbol-file ";
else if ( ((myintptr_t)-1) == (myintptr_t)prefix )
prefix=0;
fprintf(f,"%s%s 0x%08lx", prefix ? prefix : "", m->name, m->text_vma);
if ( ( psects = m->section_syms ) ) {
for ( ; *psects; psects++ )
fprintf(f," -s %s 0x%08"MYPRIxPTR, (*psects)->name, (myuintptr_t)(*psects)->value.ptv);
}
fputc('\n',f);
}
CexpModule
cexpModuleDumpGdbSectionInfo(CexpModule mod, const char *prefix, FILE *feil)
{
return cexpModIterate(mod, feil, modPrintGdbSects, (void*)prefix);
}
CexpModule
cexpModuleFindByName(const char *needle, FILE *f)
{
cexp_regex *rc=0;
CexpModule m,found=0;
if (!f)
f=stdout;
else if (CEXP_FILE_QUIET == f)
f=0;
if (!(rc=cexp_regcomp(needle))) {
fprintf(stderr,"unable to compile regexp '%s'\n", needle);
return 0;
}
__RLOCK();
for (m=cexpSystemModule; m; m=m->next) {
if (cexp_regexec(rc,m->name)) {
/* record first item found */
if (!found)
found=m;
if (f)
fprintf(f,"0x%08"MYPRIxPTR": %s\n",(myuintptr_t)m, m->name);
else
break;
}
}
__RUNLOCK();
cexp_regfree(rc);
return found;
}
#ifdef USE_LOADER
int
cexpModuleUnload(CexpModule mod)
{
int i;
CexpModule pred,m;
CexpSegment s;
__WLOCK();
/* is mod in the list at all ? */
pred=predecessor(mod);
if (mod && mod==cexpSystemModule) {
fprintf(stderr,"Cannot unload system symbol table\n");
goto cleanup;
}
if (!mod || !pred) {
fprintf(stderr,"Cannot unload: bad module handle\n");
goto cleanup;
}
for (i=0; i<BITMAP_DEPTH; i++) {
if (mod->neededby[i]) {
fprintf(stderr,"Cannot unload %s; still needed by:", mod->name);
bitmapInfo(stderr,mod->neededby);
fputc('\n',stderr);
goto cleanup;
}
}
if (mod->finiCallback && mod->finiCallback(mod)) {
fprintf(stderr,"Unload rejected by module finalizer!\n");
/* unload rejected by module */
goto cleanup;
}
/* remove from dependency bitmaps */
for (m=cexpSystemModule; m; m=m->next)
BITMAP_CLR(m->neededby, mod->id);
/* call destructors */
{
int i;
for (i=0; i<mod->nDtors; i++) {
if (mod->dtor_list[i])
mod->dtor_list[i]();
}
}
/* call module cleanup routine */
if (mod->cleanup)
mod->cleanup(mod);
/* remove from list */
pred->next=mod->next;
mod->next=0;
__WUNLOCK();
if ( mod->segs ) {
for ( s = mod->segs; s->name; s++ ) {
if ( ! s->chunk )
continue;
#ifdef HAVE_SYS_MMAN_H
/* HACK: NEVER remove 'x' attribute. Since our memory segments are not
* page-aligned it could be that we revoke 'x' for parts of memory
* that are located outside of our segments which may still be
* live, executable modules.
*/
if ( 0 ) {
unsigned long nsiz, pgbeg, pgmsk;
pgmsk = getpagesize()-1;
pgbeg = (unsigned long)s->chunk;
pgbeg &= ~pgmsk;
nsiz = s->size + (unsigned long)s->chunk - pgbeg;
nsiz = (nsiz + pgmsk) & ~pgmsk;
if ( mprotect((void*)pgbeg, nsiz, PROT_READ | PROT_WRITE) )
perror("ERROR -- mprotect(PROT_READ|PROT_WRITE)");
}
#endif
memset(s->chunk, 0, s->size);
}
}
/* could flush the caches here */
cexpModuleFree(&mod);
return 0;
cleanup:
__WUNLOCK();
return -1;
}
#endif
static void
addDependencies(CexpModule nmod)
{
CexpModule m;
for (m=cexpSystemModule; m; m=m->next) {
if (BITMAP_TST(nmod->needs,m->id))
BITMAP_SET(m->neededby,nmod->id);
}
}
/* NOTE: the caller of this helper must hold the module lock
*
* This routine is NOT reentrant.
*
* RETURNS: new unused module id, -1 on error
*/
static int
idAlloc(CexpModule mod)
{
static ModuleId id=MAX_NUM_MODULES-1;
ModuleId new_id;
BITMAP_DECLARE(used);
CexpModule m;
memset(used,0,sizeof(used));
/* mark all used ids */
for (m=cexpSystemModule; m; m=m->next) {
BITMAP_SET(used,m->id);
}
new_id = id;
do {
new_id = (new_id + 1) % MAX_NUM_MODULES;
if (new_id == id)
return -1;
} while (BITMAP_TST(used,new_id));
mod->id=id=new_id;
return 0;
}
static CexpSym cexpNoBuiltinSymbols = 0;
char *cexpBuiltinCpuArch = 0;
extern CexpSym cexpSystemSymbols __attribute__((weak, alias("cexpNoBuiltinSymbols")));
#ifdef USE_PMBFD
static unsigned cexpNoBuiltinAttributesSize = 0;
extern const uint8_t *cexpSystemAttributes __attribute__((weak));
extern unsigned cexpSystemAttributesSize __attribute__((weak, alias("cexpNoBuiltinAttributesSize")));
#endif
static int
cexpLoadBuiltinSymtab(CexpModule nmod)
{
int nsyms, nsect_syms, i;
CexpSym s;
if ( !cexpSystemSymbols ) {
fprintf(stderr,"No builtin symbol list found\n");
return -1;
}
for ( nsect_syms = nsyms=0; cexpSystemSymbols[nsyms].name; nsyms++ ) {
if ( cexpSystemSymbols[nsyms].flags & CEXP_SYMFLG_SECT )
nsect_syms++;
}
if ( !(nmod->symtbl = cexpCreateSymTbl(cexpSystemSymbols, sizeof(*cexpSystemSymbols), nsyms, 0, 0, 0)) ) {
fprintf(stderr,"Reading builtin system symbol table failed\n");
return -1;
}
#ifdef USE_LOADER
cexpSegsInit( &nmod->segs );
cexpSymTblFixup(nmod->symtbl, cexpSegsGet( nmod->segs, CEXP_SEG_VENR ) );
#endif
if ( cexpIndexSymTbl(nmod->symtbl) )
return -1;
nmod->text_vma = 0xdeadbeef;
nmod->section_syms = malloc( (nsect_syms + 1) * sizeof(CexpSym) );
if ( ! nmod->section_syms )
return -1;
for ( i = 0, s = cexpSystemSymbols, nsect_syms = 0; i<nsyms; i++, s++ ) {
if ( s->flags & CEXP_SYMFLG_SECT ) {
nmod->section_syms[nsect_syms++] = s;
if ( !strcmp(".text",s->name) ) {
nmod->text_vma = (unsigned long)s->value.ptv;
}
}
/* guess type */
if ( TVoid == s->value.type )
s->value.type = cexpTypeGuessFromSize(s->size);
}
nmod->section_syms[nsect_syms] = 0; /* tag end */
#if defined(USE_PMBFD) && defined(USE_LOADER)
if ( cexpSystemAttributesSize > 0 ) {
bfd_init();
nmod->fileAttributes = pmelf_read_attribute_set(cexpSystemAttributes, cexpSystemAttributesSize, 0, "SYSTEM");
}
#endif
#ifdef HAVE_BFD_DISASSEMBLER
/* finding our BFD architecture turns out to be non-trivial! */
{
bfd *abfd = 0; /* keep compiler happy */
char *tn = 0;
const char **tgts = 0;
#ifndef USE_PMBFD
const bfd_arch_info_type *ai = 0;
/* must open a BFD for the default target */
if ( !(tn=malloc(L_tmpnam)) || !tmpnam(tn) || !(abfd=bfd_openw(tn,"default")) ) {
fprintf(stderr,"cexpLoadBuiltinSymtab(): unable to open a dummy BFD for determining disassembler arch\n");
goto cleanup;
}
if ( cexpBuiltinCpuArch ) {
if ( !(ai=bfd_scan_arch(cexpBuiltinCpuArch)) )
fprintf(stderr,"cexpLoadBuiltinSymtab(): User supplied CPU Architecture '%s' invalid; trying default...\n",
cexpBuiltinCpuArch);
}
if ( !ai && (tgts=bfd_target_list()) && *tgts && (cexpBuiltinCpuArch = strrchr(*tgts,'-')) ) {
ai = bfd_scan_arch(++cexpBuiltinCpuArch);
}
if ( !ai ) {
cexpBuiltinCpuArch = 0;
goto cleanup;
}
abfd->arch_info = ai;
#else
/*
* pmbfd can deal with the NULL BFD that the opcode
* library passes...
*/
#endif
cexpDisassemblerInstall(abfd);
#ifndef USE_PMBFD
cleanup:
if ( !ai )
fprintf(stderr,"Unable to determine target CPU architecture -- skipping disassembler installation\n");
#endif
if ( abfd )
bfd_close_all_done(abfd);
if ( tn ) {
unlink(tn);
free(tn);
}
free(tgts);
}
#endif
return 0;
}
CexpModule
cexpModuleLoad(const char *filename, const char *modulename)
{
CexpModule m,tail,nmod,rval=0;
char *slash = filename ? strrchr(filename,'/') : 0;
if (slash)
slash++;
if (!modulename)
modulename=slash ? slash : filename;
if (!modulename)
modulename="SYSTEM-BUILTIN";
if (!(nmod=(CexpModule)malloc(sizeof(*nmod))))
return 0;
memset(nmod,0,sizeof(*nmod));
__WLOCK();
for (m=cexpSystemModule, tail=0; m; m=m->next) {
tail=m;
if (!strcmp(m->name,modulename)) {
fprintf(stderr,
"ERROR: a module '%s' exists already\n",
modulename);
goto cleanup;
}
}
if (idAlloc(nmod)) {
fprintf(stderr,
"Unable to allocate a module ID (more than %i loaded?)\n",
MAX_NUM_MODULES);
goto cleanup;
}
if (!(nmod->name=(char*)malloc(strlen(modulename)+1))) {
goto cleanup;
}
strcpy(nmod->name,modulename);
if ( filename ) {
if (cexpLoadFile(filename,nmod)) {
goto cleanup;
}
} else {
if (cexpLoadBuiltinSymtab(nmod)) {
goto cleanup;
}
}
/* add help tables */
{
CexpSym found;
int max;
cexp_regex *rc;
assert(rc=cexp_regcomp("^"CEXP_HELP_TAB_NAME));
for (found=0,max=1; (found=_cexpSymTblLookupRegex(rc,&max,found,0,nmod->symtbl)); found++,max=1) {
cexpAddHelpToSymTab((CexpHelpTab)found->value.ptv, nmod->symtbl);
}
cexp_regfree(rc);
}
#ifdef HAVE_SYS_MMAN_H
if ( nmod->segs ) {
CexpSegment s;
for ( s=nmod->segs; s->name; s++ ) {
/* make executable */
if ( s->chunk ) {
unsigned long nsiz, pgbeg, pgmsk;
pgmsk = getpagesize()-1;
pgbeg = (unsigned long)s->chunk;
pgbeg &= ~pgmsk;
nsiz = s->size + (unsigned long)s->chunk - pgbeg;
nsiz = (nsiz + pgmsk) & ~pgmsk;
if ( mprotect((void*)pgbeg, nsiz, PROT_READ | PROT_WRITE | PROT_EXEC) )
perror("ERROR -- mprotect(PROT_READ|PROT_WRITE|PROT_EXEC)");
}
}
}
#endif
/* call the constructors */
{
int i;
for (i=0; i<nmod->nCtors; i++) {
if (nmod->ctor_list[i])
nmod->ctor_list[i]();
}
/* the constructors are not really needed
* anymore...
*/
free(nmod->ctor_list);
nmod->ctor_list=0;
nmod->nCtors=0;
}
/* call 'non-C++' constructor */
if (nmod->iniCallback)
nmod->iniCallback(nmod);
addDependencies(nmod);
/* chain to the list of modules */
if (tail)
tail->next=nmod;
else
cexpSystemModule=nmod;
rval=nmod;
nmod=0;
cleanup:
__WUNLOCK();
if (nmod) {
cexpModuleFree(&nmod);
return 0;
}
return rval;
}
void
cexpModuleFree(CexpModule *mp)
{
CexpModule mod=*mp;
if (mod) {
assert( ! mod->next);
free(mod->name);
#ifdef USE_LOADER
cexpSegsDeleteAll(mod->segs);
#else
if ( mod->segs ) {
fprintf(stderr,"WARNING: we shouldn't get here -- memory leak at %s:%u\n",__FILE__,__LINE__);
}
#endif
free(mod->ctor_list);
free(mod->dtor_list);
free(mod->section_syms);
free(mod->fileName);
cexpFreeSymTbl(&mod->symtbl);
#ifdef USE_PMBFD
if (mod->fileAttributes)
pmelf_destroy_attribute_set(mod->fileAttributes);
#endif
free(mod);
}
*mp=0;
}
#if defined(__arm__) && defined(USE_LOADER)
void *
__gnu_Unwind_Find_exidx(void *pc, int *pNumEntries)
__attribute__((weak,alias("__cexp_Unwind_Find_exidx")));
void *
__cexp_Unwind_Find_exidx(void *pc, int *pNumEntries)
{
CexpModule m;
void *rval;
int nEntries;
static void *textStart = 0, *textEnd = 0;
static int haveAddrs = 0;
CexpSym sb,se,eb,ee;
#ifdef DEBUG_EXIDX
printf("Unwind_Find_exidx for %p\n", pc);
#endif
__RLOCK();