-
Notifications
You must be signed in to change notification settings - Fork 2
/
ld.c
1764 lines (1583 loc) · 38.4 KB
/
ld.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
/*
* Linker for RetroBSD, MIPS32 architecture.
*
* Copyright (C) 2011 Serge Vakulenko, <serge@vak.ru>
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is hereby
* granted, provided that the above copyright notice appear in all
* copies and that both that the copyright notice and this
* permission notice and warranty disclaimer appear in supporting
* documentation, and that the name of the author not be used in
* advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* The author disclaim all warranties with regard to this
* software, including all implied warranties of merchantability
* and fitness. In no event shall the author be liable for any
* special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action,
* arising out of or in connection with the use or performance of
* this software.
*/
#ifdef CROSS
# include <sys/types.h>
# include <sys/select.h>
# include <sys/stat.h>
# include <sys/time.h>
# include <sys/fcntl.h>
# include <sys/signal.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
# define MAXNAMLEN 63
#else
# include <string.h>
# include <signal.h>
# include <unistd.h>
#endif
#include "nativec.h"
# define MAXNAMLEN 16
#include <stdarg.h>
#include "a.out.h"
#include "ar.h"
#include "ff.h"
#include "ff_wrap.h"
#include "ranlib.h"
#define W 4 /* word size in bytes */
#define BADDR (RAM) /* start address in memory */
#define SYMDEF "__.SYMDEF"
#define IS_LOCSYM(s) ((s)->n_name[0] == 'L' || \
(s)->n_name[0] == '.')
#define hexdig(c) ((c)<='9' ? (c) - '0' : ((c)&7) + 9)
struct exec filhdr; /* aout header */
#define HDRSZ sizeof(struct exec)
struct archdr { /* archive header */
char * ar_name;
long ar_date;
int ar_uid;
int ar_gid;
int ar_mode;
long ar_size;
} archdr;
FILE *text, *reloc; /* input management */
/* output management */
FILE *outb, *toutb, *doutb, *troutb, *droutb, *soutb;
/* symbol management */
struct _local {
unsigned locindex; /* index to symbol in file */
struct nlist *locsymbol; /* ptr to symbol table */
};
#define NSYM 1000
#define NSYMPR 400
#define NLIBS 256
#define RANTABSZ 400
/* struct nlist cursym; /\* current symbol *\/ */
/* struct nlist symtab [NSYM]; /\* table of symbols *\/ */
/* struct nlist **symhash [NSYM]; /\* pointers to hash table *\/ */
/* struct nlist *lastsym; /\* last entered symbol *\/ */
/* struct nlist *hshtab [NSYM+2]; /\* hash table for symbols *\/ */
/* struct local local [NSYMPR]; */
/* int symindex; /\* next free entry of symbol table *\/ */
/* unsigned basaddr = BADDR; /\* base address of loading *\/ */
/* struct ranlib rantab [RANTABSZ]; */
/* int rancount; /\* number of elements in rantab *\/ */
typedef struct {
struct nlist cursym; /* current symbol */
struct nlist symtab [NSYM]; /* table of symbols */
struct nlist **symhash [NSYM]; /* pointers to hash table */
struct nlist *lastsym; /* last entered symbol */
struct nlist *hshtab [NSYM+2]; /* hash table for symbols */
struct _local local [NSYMPR];
int symindex; /* next free entry of symbol table */
struct ranlib rantab [RANTABSZ];
int rancount; /* number of elements in rantab */
unsigned liblist [NLIBS], *libp;
int _filenum;
struct nlist *p_etext, *p_edata, *p_end, *p_gp, *entrypt;
int trace; /* internal trace flag */
int xflag; /* discard local symbols */
int Xflag; /* discard locals starting with 'L' or '.' */
int Sflag; /* discard all except locals and globals*/
int rflag; /* preserve relocation bits, don't define commons */
int output_relinfo;
int sflag; /* discard all symbols */
int dflag; /* define common even with rflag */
int verbose; /* verbose mode */
unsigned tsize, dsize, bsize, ssize, nsym;
unsigned ctrel, cdrel, cbrel;
unsigned torigin, dorigin, borigin;
unsigned gp; /* allocated address */
int ofilfnd;
char *filname;
int errlev;
char tfname [6];
char *ofilename;
} ld_t;
int delarg = 4;
unsigned basaddr;// = BADDR; /* base address of loading */
unsigned gpoffset = 0x8000; /* offset from data start */
ld_t *g_ld;
#define cursym g_ld->cursym
#define symtab g_ld->symtab
#define symhash g_ld->symhash
#define lastsym g_ld->lastsym
#define hshtab g_ld->hshtab
#define local g_ld->local
#define symindex g_ld->symindex
#define rantab g_ld->rantab
#define rancount g_ld->rancount
#define liblist g_ld->liblist
#define libp g_ld->libp
#define filenum g_ld->_filenum
#define trace g_ld->trace
#define xflag g_ld->xflag
#define Xflag g_ld->Xflag
#define Sflag g_ld->Sflag
#define rflag g_ld->rflag
#define output_relinfo g_ld->output_relinfo
#define sflag g_ld->sflag
#define dflag g_ld->dflag
#define verbose g_ld->verbose
#define tsize g_ld->tsize
#define dsize g_ld->dsize
#define bsize g_ld->bsize
#define ssize g_ld->ssize
#define nsym g_ld->nsym
#define ctrel g_ld->ctrel
#define cdrel g_ld->cdrel
#define cbrel g_ld->cbrel
#define torigin g_ld->torigin
#define dorigin g_ld->dorigin
#define borigin g_ld->borigin
#define gp g_ld->gp
#define ofilfnd g_ld->ofilfnd
#define filname g_ld->filname
#define errlev g_ld->errlev
#define tfname g_ld->tfname
#define ofilename g_ld->ofilename
#define p_etext g_ld->p_etext
#define p_edata g_ld->p_edata
#define p_end g_ld->p_end
#define p_gp g_ld->p_gp
#define entrypt g_ld->entrypt
/*
* library management
*/
#define ALIGN(x,y) ((x)+(y)-1-((x)+(y)-1)%(y))
#include <limits.h>
int _space_sign(const char *s, const char **endptr)
{
while (isspace((unsigned char)*s))
++s;
int sign = 0;
switch (*s)
{
case '-':
sign = -1;
// fall through
case '+':
++s;
break;
}
*endptr = s;
return sign;
}
long strtol(const char * __restrict__ s, char ** __restrict__ endptr, int radix)
{
int sign = _space_sign(s, (const char**)&s);
long result;
if (s[0] == '0')
{
++s;
if ((s[1] | 0x20) == 'x')
{
if (radix == 0 || radix == 16)
{
++s;
radix = 16;
}
}
else if (radix == 0)
radix = 8;
}
else if (radix == 0)
radix = 10;
int c;
for (result = 0; c = tolower((unsigned char)*s), isdigit(c) || ('a' <= c && c <= 'z'); s++)
{
int d = isdigit(c) ? c - '0' : c - 'a' + 10;
if (d >= radix)
break;
if (result > (LONG_MAX - d - sign) / radix)
{
result = sign ? LONG_MIN : LONG_MAX;
}
else
{
result = result * radix + d;
}
}
if (endptr != NULL)
*endptr = (char*)s;
if (sign != 0)
result = -result;
return result;
}
static inline
int getc(FILE *fp){
unsigned int br;
unsigned char m;
f_read(fp,&m,1,&br);
if(br==0)return -1;
return m;
}
static
void putc(char m,FIL *fp){
unsigned int br;
f_write(fp,&m,1,&br);
if(br==0)printf("err as.c:438");
}
static
unsigned int fgetword (f)
register FILE *f;
{
register unsigned int h;
h = getc (f);
h |= getc (f) << 8;
h |= getc (f) << 16;
h |= getc (f) << 24;
return h;
}
static
void fputword (h, f)
register unsigned int h;
register FILE *f;
{
putc (h, f);
putc (h >> 8, f);
putc (h >> 16, f);
putc (h >> 24, f);
}
static
int fgethdr (text, h)
register FILE *text;
register struct exec *h;
{
h->a_midmag = fgetword (text);
h->a_text = fgetword (text);
h->a_data = fgetword (text);
h->a_bss = fgetword (text);
h->a_reltext = fgetword (text);
h->a_reldata = fgetword (text);
h->a_syms = fgetword (text);
h->a_entry = fgetword (text);
return (1);
}
static
void fputhdr (hdr, coutb)
register struct exec *hdr;
register FILE *coutb;
{
fputword (hdr->a_magic, coutb);
fputword (hdr->a_text, coutb);
fputword (hdr->a_data, coutb);
fputword (hdr->a_bss, coutb);
fputword (hdr->a_reltext, coutb);
fputword (hdr->a_reldata, coutb);
fputword (hdr->a_syms, coutb);
fputword (hdr->a_entry, coutb);
}
/*
* Read a relocation record: 1 to 6 bytes.
*/
static
void fgetrel (f, r)
register FILE *f;
register struct reloc *r;
{
r->flags = getc (f);
if ((r->flags & RSMASK) == REXT) {
r->index = getc (f);
r->index |= getc (f) << 8;
r->index |= getc (f) << 16;
}
if ((r->flags & RFMASK) == RHIGH16 ||
(r->flags & RFMASK) == RHIGH16S) {
r->offset = getc (f);
r->offset |= getc (f) << 8;
}
}
/*
* Emit a relocation record: 1 to 6 bytes.
* Return a written length.
*/
static
unsigned fputrel (r, f)
register struct reloc *r;
register FILE *f;
{
register unsigned nbytes = 1;
putc (r->flags, f);
if ((r->flags & RSMASK) == REXT) {
putc (r->index, f);
putc (r->index >> 8, f);
putc (r->index >> 16, f);
nbytes += 3;
}
if ((r->flags & RFMASK) == RHIGH16 ||
(r->flags & RFMASK) == RHIGH16S) {
putc (r->offset, f);
putc (r->offset >> 8, f);
nbytes += 2;
}
return nbytes;
}
static
void delexit (int sig)
{
unlink ("l.out");
/* chmod (ofilename, 0777 & ~umask(0)); */
if(delarg)
exit (delarg);
}
static
void error (int n, const char *s, ...)
{
va_list ap;
va_start (ap, s);
if (! errlev)
printf ("ld: ");
if (filname)
printf ("%s: ", filname);
xvprintf(s,ap);
va_end (ap);
printf ("\n");
if (n > 1)
delexit (0);
errlev = n;
}
static
int fgetsym (text, sym)
register FILE *text;
register struct nlist *sym;
{
register int c;
c = getc (text);
if (c <= 0)
return 0;
sym->n_len = c;
sym->n_name = umm_malloc (sym->n_len + 1);
if (! sym->n_name)
error (2, "out of memory");
sym->n_type = getc (text);
sym->n_value = fgetword (text);
for (c=0; c<sym->n_len; c++)
sym->n_name [c] = getc (text);
sym->n_name [sym->n_len] = '\0';
return sym->n_len + 6;
}
static
void fputsym (s, file)
register struct nlist *s;
register FILE *file;
{
register int i;
putc (s->n_len, file);
putc (s->n_type, file);
fputword (s->n_value, file);
for (i=0; i<s->n_len; i++)
putc (s->n_name[i], file);
}
/*
* Read the file header of the archive.
*/
static
int fgetarhdr (fd, h)
register FILE *fd;
register struct archdr *h;
{
struct ar_hdr hdr;
register int len, nr;
register char *p;
char buf[20];
/* Read arhive name. Spaces should never happen. */
nr = fread (buf, 1, sizeof (hdr.ar_name), fd);
if (buf[0] == ' ')
return 0;
buf[sizeof(hdr.ar_name)] = 0;
/* Long name support. Set the "real" size of the file,
* and the long name flag/size. */
h->ar_size = 0;
if (strncmp (buf, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0) {
len = atoi (buf + sizeof(AR_EFMT1) - 1);
if (len <= 0 || len > MAXNAMLEN)
return 0;
h->ar_name = umm_malloc (len + 1);
if (! h->ar_name)
return 0;
fread (h->ar_name, 1, len, fd);
h->ar_name[len] = 0;
h->ar_size -= len;
} else {
/* Strip trailing spaces, null terminate. */
p = buf + nr - 1;
while (*p == ' ')
--p;
*++p = '\0';
len = p - buf;
h->ar_name = umm_malloc (len + 1);
if (! h->ar_name)
return 0;
strcpy (h->ar_name, buf);
}
/* Read arhive date. */
fread (buf, 1, sizeof (hdr.ar_date), fd);
buf[sizeof(hdr.ar_date)] = 0;
h->ar_date = strtol (buf, 0, 10);
/* Read user id. */
fread (buf, 1, sizeof (hdr.ar_uid), fd);
buf[sizeof(hdr.ar_uid)] = 0;
h->ar_uid = strtol (buf, 0, 10);
/* Read group id. */
fread (buf, 1, sizeof (hdr.ar_gid), fd);
buf[sizeof (hdr.ar_gid)] = 0;
h->ar_gid = strtol (buf, 0, 10);
/* Read mode (octal). */
nr = fread (buf, 1, sizeof (hdr.ar_mode), fd);
buf[sizeof(hdr.ar_mode)] = 0;
h->ar_mode = strtol (buf, 0, 8);
/* Read archive size. */
nr = fread (buf, 1, sizeof (hdr.ar_size), fd);
buf[sizeof (hdr.ar_size)] = 0;
h->ar_size = strtol (buf, 0, 10);
/* Check secondary magic. */
nr = fread (buf, 1, sizeof (hdr.ar_fmag), fd);
buf[sizeof (hdr.ar_fmag)] = 0;
if (strcmp (buf, ARFMAG) != 0){
umm_free(hdr.ar_name);
return 0;
}
return 1;
}
static
void freerantab ()
{
register struct ranlib *p;
for (p=rantab; p<rantab+rancount; ++p)
umm_free (p->ran_name);
}
static
int fgetran (text, sym)
register FILE *text;
register struct ranlib *sym;
{
register int c;
/* read struct ranlib from file */
/* 1 byte - length of name */
/* 4 bytes - seek in archive */
/* 'len' bytes - symbol name */
/* if len == 0 then eof */
/* return 1 if ok, 0 on eof */
sym->ran_len = getc (text);
if (sym->ran_len <= 0)
return (0);
sym->ran_name = umm_malloc (sym->ran_len + 1);
if (! sym->ran_name)
error (2, "out of memory");
sym->ran_off = fgetword (text);
for (c=0; c<sym->ran_len; c++)
sym->ran_name [c] = getc (text);
sym->ran_name [sym->ran_len] = '\0';
return (1);
}
static
void getrantab ()
{
register struct ranlib *p;
for (p=rantab; p<rantab+RANTABSZ; ++p) {
if (! fgetran (text, p)) {
rancount = p - rantab;
return;
}
}
error (2, "ranlib buffer overflow");
}
static
void ldrsym (sp, val, type)
register struct nlist *sp;
unsigned val;
{
if (sp == 0)
return;
if (sp->n_type != N_EXT+N_UNDF) {
printf ("%s: ", sp->n_name);
error (1, "name redefined");
return;
}
sp->n_type = type;
sp->n_value = val;
}
static
void tcreat (buf, tempflg)
register FILE **buf;
register int tempflg;
{
tfname[4] = filenum+++'a';
*buf = fopen (tempflg ? tfname : ofilename, "w+");
if (! *buf)
error (2, tempflg ?
"cannot create temporary file" :
"cannot create output file");
}
static
int reltype (stype)
int stype;
{
switch (stype & N_TYPE) {
case N_UNDF: return (0);
case N_ABS: return (RABS);
case N_TEXT: return (RTEXT);
case N_DATA: return (RDATA);
case N_BSS: return (RBSS);
case N_STRNG: return (RDATA);
case N_COMM: return (RBSS);
case N_FN: return (0);
default: return (0);
}
}
struct nlist *lookloc (lp, sn)
register struct _local *lp;
register int sn;
{
register struct _local *clp;
for (clp=local; clp<lp; clp++)
if (clp->locindex == sn)
return (clp->locsymbol);
if (trace) {
printf( "*** %d ***\n", sn);
for (clp=local; clp<lp; clp++)
printf( "%u, ", clp->locindex);
printf( "\n");
}
error (2, "bad symbol reference");
return 0;
}
static
void printrel (word, rel)
register unsigned word;
register struct reloc *rel;
{
printf ("%08x %02x ", word, rel->flags);
if ((rel->flags & RSMASK) == REXT)
printf ("%-3d ", rel->index);
else
printf (" ");
if ((rel->flags & RFMASK) == RHIGH16 ||
(rel->flags & RFMASK) == RHIGH16S)
printf ("%08x", rel->offset);
else
printf (" ");
}
/*
* Relocate the word by a given offset.
* Return the new value of word and update rel.
*/
static
unsigned relword (lp, word, rel, offset)
struct _local *lp;
register unsigned word;
register struct reloc *rel;
unsigned offset;
{
register unsigned addr, delta;
register struct nlist *sp = 0;
if (trace > 2)
printrel (word, rel);
/*
* Extract an address field from the instruction.
*/
switch (rel->flags & RFMASK) {
case RBYTE16:
addr = word & 0xffff;
break;
case RBYTE32:
addr = word;
break;
case RWORD16:
addr = (word & 0xffff) << 2;
break;
case RWORD26:
addr = (word & 0x3ffffff) << 2;
break;
case RHIGH16:
addr = (word & 0xffff) << 16;
addr += rel->offset;
break;
case RHIGH16S:
addr = (word & 0xffff) << 16;
addr += (signed short) rel->offset;
break;
default:
addr = 0;
break;
}
/*
* Compute a delta for address.
* Update the relocation info, if needed.
*/
switch (rel->flags & RSMASK) {
case RTEXT:
delta = ctrel;
break;
case RDATA:
delta = cdrel;
break;
case RBSS:
delta = cbrel;
break;
case REXT:
sp = lookloc (lp, rel->index);
if (sp->n_type == N_EXT+N_UNDF ||
sp->n_type == N_EXT+N_COMM) {
rel->index = nsym + (sp - symtab);
sp = 0;
delta = 0;
} else {
rel->flags &= RFMASK | RGPREL;
rel->flags |= reltype (sp->n_type);
delta = sp->n_value;
}
break;
default:
delta = 0;
break;
}
if ((rel->flags & RGPREL) && ! output_relinfo) {
/*
* GP relative address.
*/
delta -= gp;
rel->flags &= ~RGPREL;
}
/*
* Update the address field of the instruction.
* Update the relocation info, if needed.
*/
switch (rel->flags & RFMASK) {
case RBYTE16:
addr += delta;
word &= ~0xffff;
word |= addr & 0xffff;
break;
case RBYTE32:
word = addr + delta;
break;
case RWORD16:
if (! sp)
break;
addr += delta - offset - 4;
word &= ~0xffff;
word |= (addr >> 2) & 0xffff;
rel->flags = RABS;
break;
case RWORD26:
addr += delta;
word &= ~0x3ffffff;
word |= (addr >> 2) & 0x3ffffff;
break;
case RHIGH16:
addr += delta;
word &= ~0xffff;
word |= (addr >> 16) & 0xffff;
break;
case RHIGH16S:
addr += delta;
word &= ~0xffff;
word |= ((addr + 0x8000) >> 16) & 0xffff;
break;
}
if (trace > 2) {
//printf (" +%#x ", delta);
printf (" -> ");
printrel (word, rel);
printf ("\n");
}
return word;
}
static
void relocate (lp, b1, b2, len, origin)
struct _local *lp;
FILE *b1, *b2;
unsigned len, origin;
{
unsigned word, offset;
struct reloc rel;
for (offset=0; offset<len; offset+=W) {
word = fgetword (text);
fgetrel (reloc, &rel);
word = relword (lp, word, &rel, offset + origin);
fputword (word, b1);
if (output_relinfo)
fputrel (&rel, b2);
}
}
static
unsigned copy_and_close (buf)
register FILE *buf;
{
register int c;
unsigned nbytes;
f_lseek (buf,0);
nbytes = 0;
while ((c = getc (buf)) != EOF) {
putc (c, outb);
nbytes++;
}
fclose (buf);
return nbytes;
}
static
int mkfsym (s, wflag)
register char *s;
{
register char *p;
if (sflag || xflag)
return (0);
for (p=s; *p;)
if (*p++ == '/')
s = p;
if (! wflag)
return (p - s + 6);
cursym.n_len = p - s;
cursym.n_name = umm_malloc (cursym.n_len + 1);
if (! cursym.n_name)
error (2, "out of memory");
for (p=cursym.n_name; *s; p++, s++)
*p = *s;
cursym.n_type = N_FN;
cursym.n_value = torigin;
fputsym (&cursym, soutb);
umm_free (cursym.n_name);
return (cursym.n_len + 6);
}
static
int getfile (cp)
register char *cp;
{
int c;
static char libname [] = "/lib/libxxxxxxx";
char magic [SARMAG];
text = 0;
filname = cp;
if (cp[0] == '-' && cp[1] == 'l') {
if (cp[2] == '\0')
cp = "-la";
filname = libname;
for (c = 0; cp [c+2]; c++)
filname [c + 8] = cp [c+2];
filname [c + 8] = '.';
filname [c + 8 + 1] = 'a';
filname [c + 8 + 2] = '\0';
}
text = fopen (filname, "r");
if (! text)
error (2, "cannot open");
reloc = fopen (filname, "r");
if (! reloc)
error (2, "cannot open");
/* Read file magic. */
if (fread(magic, 1, SARMAG, text) != SARMAG)
return (0); /* regular file */
if (strncmp (magic, ARMAG, SARMAG) != 0)
return (0); /* regular file */
if (! fgetarhdr (text, &archdr))
return (0); /* regular file */
if (strncmp (archdr.ar_name, SYMDEF, sizeof (SYMDEF)) != 0) {
umm_free (archdr.ar_name);
return (1); /* regular archive */
}
umm_free (archdr.ar_name);
return (2); /* randomized archive */
}
static
int enter (hp)
register struct nlist **hp;
{
register struct nlist *sp;
if (*hp) {
lastsym = *hp;
return (0);
}
if (symindex >= NSYM)
error (2, "symbol table overflow");
symhash [symindex] = hp;
*hp = lastsym = sp = &symtab[symindex++];
sp->n_len = cursym.n_len;
sp->n_name = cursym.n_name;
sp->n_type = cursym.n_type;
sp->n_value = cursym.n_value;
return (1);
}
static
void symreloc()
{
switch (cursym.n_type) {
case N_TEXT:
case N_EXT+N_TEXT:
cursym.n_value += ctrel;
return;
case N_DATA:
case N_EXT+N_DATA:
cursym.n_value += cdrel;
return;
case N_BSS:
case N_EXT+N_BSS:
cursym.n_value += cbrel;
return;
case N_EXT+N_UNDF:
case N_EXT+N_COMM:
return;
}
if (cursym.n_type & N_EXT)
cursym.n_type = N_EXT+N_ABS;
}
/*
* Suboptimal 32-bit hash function.
* Copyright (C) 2006 Serge Vakulenko.
*/
static
unsigned hash_rot13 (s)
register const char *s;
{
register unsigned hash, c;
hash = 0;
while ((c = (unsigned char) *s++) != 0) {
hash += c;
hash -= (hash << 13) | (hash >> 19);
}
return hash;
}
static
struct nlist **lookup()
{
int clash;
register char *cp, *cp1;
register struct nlist **hp;
hp = &hshtab[hash_rot13 (cursym.n_name) % NSYM + 2];
while (*hp != 0) {
cp1 = (*hp)->n_name;
clash = 0;
for (cp = cursym.n_name; *cp;) {
if (*cp++ != *cp1++) {
clash = 1;
break;
}
}
if (! clash)
break;
if (++hp >= &hshtab[NSYM+2])
hp = hshtab;
}
return (hp);
}
static
struct nlist **slookup (s)
char *s;
{
cursym.n_len = strlen (s) + 1;
cursym.n_name = s;
cursym.n_type = N_EXT+N_UNDF;
cursym.n_value = 0;
return (lookup ());