-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathldscript.c
More file actions
2250 lines (1923 loc) · 54.8 KB
/
ldscript.c
File metadata and controls
2250 lines (1923 loc) · 54.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
/* $VER: vlink ldscript.c V0.17a (19.06.22)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2022 Frank Wille
*/
#define LDSCRIPT_C
#include "vlink.h"
#include "elfcommon.h"
#include "ldscript.h"
#define DUMMY_SEC_FROM_PATTERN 1
static const char *scriptbase;
static const char *scriptname;
static struct ObjectUnit *script_obj;
static struct MemoryDescr *memory_blocks;
static struct MemoryDescr *defmem,*atdefmem,*vdefmem,*ldefmem;
static const char *defmemname = "default";
static bool preparse; /* no sym-assignments in SECTIONS when true */
static int level; /* 0=outside SECTIONS,1=inside,2=section def. */
static struct LinkedSection *current_ls; /* current section in work */
static const char *new_ls_name; /* newly defined sect. name (pass 1) */
/* BYTE, SHORT, LONG, etc. data commands */
static int datasize,dataalign; /* datasize != 0 enables data command */
static lword dataval;
/* for 2nd pass over the SECTIONS block during linking: */
static char *secblkbase;
static int secblkline;
/* for 2nd pass over a section definition block: */
static char *secdefbase;
static int secdefline;
/* Default segment names (including a blank to prevent redefinitions) */
static const char *defhdr = " headers";
static const char *defint = " interp";
static const char *defload =" load";
static const char *deftxt = " text";
static const char *defdat = " data";
static const char *defdyn = " dynamic";
static const char *defnot = " note";
/* Linker script functions: */
static int sf_addr(struct GlobalVars *,lword,lword *);
static int sf_align(struct GlobalVars *,lword,lword *);
static int sf_length(struct GlobalVars *,lword,lword *);
static int sf_loadaddr(struct GlobalVars *,lword,lword *);
static int sf_max(struct GlobalVars *,lword,lword *);
static int sf_min(struct GlobalVars *,lword,lword *);
static int sf_origin(struct GlobalVars *,lword,lword *);
static int sf_sizeof(struct GlobalVars *,lword,lword *);
static int sf_sizeofheaders(struct GlobalVars *,lword,lword *);
struct ScriptFunc ldFunctions[] = {
{ "ADDR",sf_addr },
{ "ALIGN",sf_align },
{ "LENGTH",sf_length },
{ "LOADADDR",sf_loadaddr },
{ "MAX",sf_max },
{ "MIN",sf_min },
{ "ORIGIN",sf_origin },
{ "SIZEOF",sf_sizeof },
{ "SIZEOF_HEADERS",sf_sizeofheaders },
{ NULL,NULL }
};
/* Linker script commands: */
static void sc_ctors_gnu(struct GlobalVars *);
static void sc_ctors_vbcc(struct GlobalVars *);
static void sc_ctors_vbcc_elf(struct GlobalVars *);
static void sc_assert(struct GlobalVars *);
static void sc_entry(struct GlobalVars *);
static void sc_extern(struct GlobalVars *);
static void sc_fill8(struct GlobalVars *);
static void sc_fill16(struct GlobalVars *);
static void sc_input(struct GlobalVars *);
static void sc_provide(struct GlobalVars *);
static void sc_searchdir(struct GlobalVars *);
static void sc_byte(struct GlobalVars *);
static void sc_short(struct GlobalVars *);
static void sc_long(struct GlobalVars *);
static void sc_quad(struct GlobalVars *);
static void sc_reserve(struct GlobalVars *);
struct ScriptCmd ldCommands[] = {
{ "ASSERT",SCMDF_PAREN|SCMDF_GLOBAL,sc_assert },
{ "BYTE",SCMDF_PAREN|SCMDF_SEMIC|SCMDF_SECDEF,sc_byte },
{ "CONSTRUCTORS",SCMDF_GLOBAL,sc_ctors_gnu },
{ "ENTRY",SCMDF_PAREN|SCMDF_GLOBAL,sc_entry },
{ "EXTERN",SCMDF_PAREN|SCMDF_GLOBAL,sc_extern },
{ "FILL8",SCMDF_PAREN|SCMDF_GLOBAL,sc_fill8 },
{ "FILL16",SCMDF_PAREN|SCMDF_GLOBAL,sc_fill16 },
{ "GROUP",SCMDF_PAREN|SCMDF_GLOBAL,sc_input },
{ "INPUT",SCMDF_PAREN|SCMDF_GLOBAL,sc_input },
{ "LONG",SCMDF_PAREN|SCMDF_SEMIC|SCMDF_SECDEF,sc_long },
{ "OUTPUT_ARCH",SCMDF_PAREN|SCMDF_GLOBAL,NULL },
{ "OUTPUT_FORMAT",SCMDF_PAREN|SCMDF_GLOBAL,NULL },
{ "PROVIDE",SCMDF_PAREN|SCMDF_SEMIC|SCMDF_GLOBAL,sc_provide },
{ "QUAD",SCMDF_PAREN|SCMDF_SEMIC|SCMDF_SECDEF,sc_quad },
{ "SEARCH_DIR",SCMDF_PAREN|SCMDF_GLOBAL,sc_searchdir },
{ "SHORT",SCMDF_PAREN|SCMDF_SEMIC|SCMDF_SECDEF,sc_short },
{ "SQUAD",SCMDF_PAREN|SCMDF_SEMIC|SCMDF_SECDEF,sc_quad },
{ "RESERVE",SCMDF_PAREN|SCMDF_SEMIC|SCMDF_SECDEF,sc_reserve },
{ "VBCC_CONSTRUCTORS",SCMDF_GLOBAL,sc_ctors_vbcc },
{ "VBCC_CONSTRUCTORS_ELF",SCMDF_GLOBAL,sc_ctors_vbcc_elf },
{ NULL,0,NULL }
};
static char *getword(void)
{
return getarg(1+32);
}
static char *getalnum(void)
{
return getarg(1+2);
}
static char *getpattern(void)
{
return getarg(1+4+32);
}
static char *getfilename(void)
{
if (getchr() == '\"') {
back(1);
return getquoted();
}
back(1);
return getarg(27);
}
#if 0
/* Not used at the moment!
When this function is reused, the 'provided' case needs to be implemented!
*/
static int parse_assignment_expr(lword caddr,lword *result,int provided)
{
int abs;
abs = parse_expr(caddr,result);
if (provided) {
...
}
if (getchr() != ';')
error(66,scriptname,getlineno(),';'); /* ';' expected */
else
back(1);
return abs;
}
#endif
static bool startofblock(char c)
{
if (getchr() != c) {
error(66,scriptname,getlineno(),c);
back(1);
return FALSE;
}
return TRUE;
}
static bool endofblock(char c1,char c2)
{
if (getchr() != c2) {
error(66,scriptname,getlineno(),c2);
skipblock(1,c1,c2);
return FALSE;
}
return TRUE;
}
static lword term_absexp(void)
{
lword val = 0;
if (startofblock('(')) {
if (!parse_expr(-1,&val))
error(67,scriptname,getlineno()); /* Absolute number expected */
endofblock('(',')');
}
return val;
}
static void term_two_exp(lword caddr,
int *abs1,lword *val1,int *abs2,lword *val2)
{
*abs1 = *abs2 = 1;
*val1 = *val2 = 0;
if (startofblock('(')) {
*abs1 = parse_expr(caddr,val1);
skip();
if (getchr() == ',') {
*abs2 = parse_expr(caddr,val2);
endofblock('(',')');
}
else {
error(66,scriptname,getlineno(),','); /* ',' expected */
skipblock(1,'(',')');
}
}
}
static struct Phdr *add_phdr(struct GlobalVars *gv,const char *name,
uint32_t type,uint16_t flags,lword addr,
struct MemoryDescr *vmreg,struct MemoryDescr *lmreg)
{
struct Phdr *p,*new;
if (type == PT_PHDR) {
flags |= PHDR_USED;
}
else if ((flags&(PHDR_FILEHDR|PHDR_PHDRS))==PHDR_FILEHDR ||
(flags&(PHDR_FILEHDR|PHDR_PHDRS))==PHDR_PHDRS) {
/* must include both: FILEHDR and PHDR */
error(77,scriptname,getlineno(),name);
return NULL;
}
new = alloczero(sizeof(struct Phdr));
new->name = allocstring(name);
new->type = type;
new->flags = flags;
new->start = addr;
new->start_vma = ADDR_NONE;
new->vmregion = vmreg;
new->lmregion = lmreg;
if (type == PT_LOAD) {
if (!gv->no_page_align)
new->alignment = shiftcnt(fff[gv->dest_format]->page_size);
}
else if (type == PT_PHDR)
new->alignment = 2;
if (p = gv->phdrlist) {
while (p->next) {
if (type==PT_PHDR && p->type==PT_PHDR) {
free(new);
return NULL; /* reject double PHDR segment */
}
p = p->next;
}
p->next = new;
}
else
gv->phdrlist = new;
return new;
}
static struct Phdr *find_phdr(struct GlobalVars *gv,const char *name,
struct MemoryDescr *vmreg,
struct MemoryDescr *lmreg)
{
struct Phdr *p;
if (p = gv->phdrlist) {
do {
if (!strcmp(p->name,name) && p->vmregion==vmreg && p->lmregion==lmreg)
break;
}
while (p = p->next);
}
return p;
}
static void use_segment(struct GlobalVars *gv,const char *name,uint32_t type,
struct MemoryDescr *vmreg,struct MemoryDescr *lmreg)
{
struct Phdr *p;
if (p = find_phdr(gv,name,vmreg,lmreg)) {
if (p->type == PT_NULL)
return;
}
else {
if (!(p = add_phdr(gv,name,type,0,ADDR_NONE,vmreg,lmreg)))
return;
}
p->flags |= PHDR_USED;
}
static uint16_t guess_special_segment(const char *secname,const char **segname)
{
uint16_t type;
if (!strncmp(secname,".interp",7)) {
*segname = defint;
type = PT_INTERP;
}
else if (!strncmp(secname,".dynamic",8)) {
*segname = defdyn;
type = PT_DYNAMIC;
}
else if (!strncmp(secname,".note",5)) {
*segname = defnot;
type = PT_NOTE;
}
else {
*segname = NULL;
type = 0;
}
return type;
}
static void scriptsymbol(struct GlobalVars *gv,char *name,int try,
lword val,uint8_t type,uint8_t flags)
{
struct Symbol *sym;
struct Symbol **chain = &script_obj->objsyms[elf_hash(name)%OBJSYMHTABSIZE];
while (sym = *chain) {
if (!strcmp(name,sym->name)) {
if (!try)
error(109,scriptname,getlineno(),name); /* already defined */
return;
}
chain = &sym->obj_chain;
}
sym = alloczero(sizeof(struct Symbol));
if (check_protection(gv,name))
flags |= SYMF_PROTECTED;
sym->name = allocstring(name);
sym->value = val;
sym->type = type;
sym->flags = flags;
sym->bind = SYMB_GLOBAL;
/* These symbols will be moved from scriptsymbols list into
LinkedSection.symbols during linker_copy() - at least
the remaining, absolute ones. */
if (addglobsym(gv,sym)) {
*chain = sym;
addtail(&gv->scriptsymbols,&sym->n);
}
else
free(sym);
}
bool is_ld_script(struct ObjectUnit *obj)
{
return obj==NULL ? FALSE : strncmp(obj->objname,"Linker Script ",14)==0;
}
static void add_cmdline_lnksyms(struct GlobalVars *gv)
{
struct SymNames *sn;
for (sn=gv->lnk_syms; sn!=NULL; sn=sn->next)
scriptsymbol(gv,(char *)sn->name,0,sn->value,SYM_ABS,0);
}
static struct MemoryDescr *find_memblock(char *name)
{
struct MemoryDescr *md = memory_blocks;
while (md) {
if (!strcmp(md->name,name))
return md;
md = md->next;
}
return NULL;
}
void update_address(struct MemoryDescr *rmd,struct MemoryDescr *dmd,
unsigned long addbytes)
{
const char *secname = current_ls ? current_ls->name : defmemname;
rmd->current += (lword)addbytes;
if (rmd->current > rmd->org + rmd->len) {
/* Fatal: Size of memory region exceeded! */
error(63,rmd->name,secname,(unsigned long long)rmd->current);
}
if (dmd != rmd) {
dmd->current += addbytes;
if (dmd->current > dmd->org + dmd->len) {
/* Fatal: Size of memory region exceeded! */
error(63,dmd->name,secname,(unsigned long long)dmd->current);
}
}
}
void align_address(struct MemoryDescr *rmd,struct MemoryDescr *dmd,
unsigned long alignment)
{
unsigned long addbytes = align(rmd->current,alignment);
update_address(rmd,dmd,addbytes);
}
static lword change_address(struct MemoryDescr *md,lword newval)
{
const char *secname = current_ls ? current_ls->name : defmemname;
lword oldval = md->current;
md->current = newval;
if ((md->current < md->org) || (md->current > md->org + md->len)) {
/* Fatal: Size of memory region exceeded! */
error(63,md->name,secname,(unsigned long long)md->current);
}
return newval-oldval;
}
static void skip_expr(int provided)
/* skip normal or provided expression */
{
char c;
if (provided)
skipblock(1,'(',')');
do {
c = getchr();
}
while (c!=';' && c!='}' && c!='\0');
if (c != ';')
error(66,scriptname,getlineno(),';'); /* ';' expected */
}
#if !DUMMY_SEC_FROM_PATTERN
static struct Section *get_dummy_sec(const char *name)
/* return dummy section of our artificial script-object with name */
{
struct Section *sec;
for (sec=(struct Section *)script_obj->sections.first;
sec->n.next!=NULL; sec=(struct Section *)sec->n.next) {
if (!strcmp(sec->name,name))
break;
}
if (sec->n.next == NULL)
ierror("get_dummy_sec(): Dummy section \"%s\" not found",name);
return sec;
}
#endif
static void symbol_assignment(struct GlobalVars *gv,
char *symword,uint8_t symflags)
{
char *fn = "symbol_assignment(): ";
struct LinkedSection *cls = current_ls;
struct MemoryDescr *rmd = cls ? cls->relocmem : vdefmem;
struct MemoryDescr *dmd = cls ? cls->destmem : vdefmem;
char symname[MAXLEN];
struct Symbol *sym;
lword expr_val;
int try;
try = testchr('?'); /* =? only assign undefined symbols */
strcpy(symname,symword); /* symword is guaranteed to fit into MAXLEN */
if (!strcmp(symname,".")) {
if (level >= 1) {
if (!preparse) {
if (!(symflags & SYMF_PROVIDED)) {
lword offs;
parse_expr(rmd->current,&expr_val);
offs = change_address(rmd,expr_val);
if (dmd != rmd) {
/* change address in destmem by the same amount */
change_address(dmd,dmd->current+offs);
}
}
else {
/* Address symbol '.' cannot be provided */
error(108,scriptname,getlineno());
}
}
}
else {
/* Address symbol '.' invalid outside SECTIONS block */
error(101,scriptname,getlineno());
}
}
else {
/* real symbol assignment */
if (level < 1) {
if (preparse) { /* level 0 (outside SECTION) is only parsed once! */
if (parse_expr(-1,&expr_val)) {
scriptsymbol(gv,symname,try,expr_val,SYM_ABS,symflags);
}
else {
/* Only absolute expr. may be assigned outside SECTIONS block */
error(110,scriptname,getlineno());
}
}
}
else {
if (preparse) {
scriptsymbol(gv,symname,try,0,SYM_ABS,symflags);
}
else {
if (sym = findsymbol(gv,NULL,symname,0)) {
int abs = parse_expr(rmd->current,&expr_val);
if (level < 2)
abs = 1;
sym->type = abs ? SYM_ABS : SYM_RELOC;
if (!abs) {
if (current_ls) {
/* assign section and remove from scriptsymbol list,
will be moved into section's symbol list in linker_copy() */
#if !DUMMY_SEC_FROM_PATTERN
if (listempty(¤t_ls->sections))
sym->relsect = get_dummy_sec(current_ls->name);
else
#endif
sym->relsect = (struct Section *)current_ls->sections.first;
/* a section with symbols must be allocated? @@@ */
current_ls->flags |= SF_ALLOC;
remnode(&sym->n);
}
else
ierror("%sNo current LinkedSection set, while "
"defining %s",fn,symname);
}
sym->value = abs ? expr_val : (expr_val - current_ls->base);
}
else
ierror("%s%s disappeared",fn,symname);
}
}
}
skip_expr(symflags&SYMF_PROVIDED);
}
static struct LinkedSection *getsection(struct GlobalVars *gv)
{
struct LinkedSection *ls = NULL;
char *sname;
if (sname = getword()) {
if (!(ls = find_lnksec(gv,sname,0,0,0,0)))
error(79,scriptname,getlineno(),sname); /* Undefined section */
}
else
error(78,scriptname,getlineno());
return ls;
}
static struct MemoryDescr *getmemblock(struct GlobalVars *gv)
{
struct MemoryDescr *md = NULL;
char *mname;
if (mname = getword()) {
if (!(md = find_memblock(mname)))
error(135,scriptname,getlineno(),mname); /* Undefined memory region */
}
else
error(78,scriptname,getlineno());
return md;
}
#if DUMMY_SEC_FROM_PATTERN
static struct Section *make_dummy_sec_from_pattern(struct GlobalVars *gv,
struct LinkedSection *ls)
/* The first section is always an empty dummy section, which is needed
for all symbol definitions by the linker script.
Its 'lnksec' reference and 'va' will be set during linking.
Its name is build from the first section pattern found. */
{
char *pp,*name,*np,c;
struct Section *dummy;
pp = getpattern();
if (pp == NULL)
return NULL;
np = name = alloc(strlen(pp)+1);
while (c = *pp++) {
switch (c) {
case '*':
break;
case '?':
*np++ = '_';
break;
case '[':
/* @@@ ! is not supported */
if (*pp)
*np++ = *pp++;
while (*pp!='\0' && *pp!=']')
pp++;
if (*pp == ']')
pp++;
break;
default:
*np++ = c;
break;
}
}
*np = '\0';
dummy = create_section(script_obj,name,NULL,0);
dummy->lnksec = ls; /* assign to ls */
addtail(&ls->sections,&dummy->n);
return dummy;
}
#endif
static int sf_addr(struct GlobalVars *gv,lword addr,lword *res)
{
struct LinkedSection *ls;
if (startofblock('(')) {
if (ls = getsection(gv))
*res = ls->base;
endofblock('(',')');
}
return 1;
}
static int sf_align(struct GlobalVars *gv,lword addr,lword *res)
{
lword a = term_absexp();
addr += a - 1;
*res = addr - addr % a;
return 0;
}
static int sf_length(struct GlobalVars *gv,lword addr,lword *res)
{
struct MemoryDescr *md;
if (startofblock('(')) {
if (md = getmemblock(gv))
*res = md->len;
endofblock('(',')');
}
return 1;
}
static int sf_loadaddr(struct GlobalVars *gv,lword addr,lword *res)
{
struct LinkedSection *ls;
if (startofblock('(')) {
if (ls = getsection(gv))
*res = ls->copybase;
endofblock('(',')');
}
return 1;
}
static int sf_max(struct GlobalVars *gv,lword addr,lword *res)
{
int abs1,abs2;
lword val1,val2;
term_two_exp(addr,&abs1,&val1,&abs2,&val2);
if (val1 < val2) {
*res = val2;
return abs2;
}
*res = val1;
return abs1;
}
static int sf_min(struct GlobalVars *gv,lword addr,lword *res)
{
int abs1,abs2;
lword val1,val2;
term_two_exp(addr,&abs1,&val1,&abs2,&val2);
if (val1 > val2) {
*res = val2;
return abs2;
}
*res = val1;
return abs1;
}
static int sf_origin(struct GlobalVars *gv,lword addr,lword *res)
{
struct MemoryDescr *md;
if (startofblock('(')) {
if (md = getmemblock(gv))
*res = md->org;
endofblock('(',')');
}
return 1;
}
static int sf_sizeof(struct GlobalVars *gv,lword addr,lword *res)
{
struct LinkedSection *ls;
if (startofblock('(')) {
if (ls = getsection(gv))
*res = ls->size;
endofblock('(',')');
}
return 1;
}
static int sf_sizeofheaders(struct GlobalVars *gv,lword addr,lword *res)
{
/* SIZEOF_HEADERS means that we want to include File- and PHDR in
the first ELF segment */
if (!find_phdr(gv,defhdr,defmem,defmem))
add_phdr(gv,defhdr,PT_PHDR,PHDR_PHDRS,ADDR_NONE,defmem,defmem);
*res = fff[gv->dest_format]->headersize(gv);
return 1;
}
static void set_ctors_type(struct GlobalVars *gv,uint8_t type)
{
if (gv->collect_ctors_type!=CCDT_NONE &&
gv->collect_ctors_type!=type)
error(71,scriptname,getlineno()); /* multiple constructor types */
gv->collect_ctors_type = type;
if (new_ls_name)
gv->collect_ctors_secname = new_ls_name;
}
static void sc_ctors_gnu(struct GlobalVars *gv)
/* CONSTRUCTORS */
{
set_ctors_type(gv,CCDT_GNU);
}
static void sc_ctors_vbcc(struct GlobalVars *gv)
/* VBCC_CONSTRUCTORS */
{
set_ctors_type(gv,CCDT_VBCC);
}
static void sc_ctors_vbcc_elf(struct GlobalVars *gv)
/* VBCC_CONSTRUCTORS_ELF */
{
set_ctors_type(gv,CCDT_VBCC_ELF);
}
static void sc_assert(struct GlobalVars *gv)
/* ASSERT(expression,"message") */
{
if (startofblock('(')) {
struct LinkedSection *cls = current_ls;
struct MemoryDescr *md = cls ? cls->relocmem : vdefmem;
lword val = -1;
char *msg;
if (!preparse)
parse_expr(md->current,&val);
else
parse_expr(-2,&val);
if (getchr() == ',')
msg = getquoted();
else
msg = NULL;
if (val == 0)
error(73,scriptname,getlineno(),msg?msg:noname);
endofblock('(',')');
}
}
static void sc_entry(struct GlobalVars *gv)
/* ENTRY(symbol or address) */
{
if (startofblock('(')) {
char *entryname;
if (entryname = getalnum()) {
if (gv->entry_name == NULL)
gv->entry_name = allocstring(entryname);
}
else
error(78,scriptname,getlineno());
endofblock('(',')');
}
}
static void sc_extern(struct GlobalVars *gv)
/* EXTERN(symbol [symbol...]) */
{
if (startofblock('(')) {
char c;
do {
char *name = getword();
if (*name)
add_symnames(&gv->undef_syms,allocstring(name),0);
else
error(78,scriptname,getlineno()); /* missing argument */
if (c = getchr())
back(1);
}
while (c!='\0' && c!=')');
endofblock('(',')');
}
}
static void dofill(struct GlobalVars *gv,int sz)
/* fill with 8/16-bit value */
{
if (startofblock('(')) {
lword val;
if (parse_expr(preparse ? -1 : 0,&val))
gv->filldata = sz==1 ? (uint16_t)((val << 8) | (val & 0xff))
: (uint16_t)(val & 0xffff);
else
error(67,scriptname,getlineno()); /* Absolute number expected */
endofblock('(',')');
}
}
static void sc_fill8(struct GlobalVars *gv)
/* FILL8(data8) */
{
dofill(gv,1);
}
static void sc_fill16(struct GlobalVars *gv)
/* FILL16(data16) */
{
dofill(gv,2);
}
static bool get_dataval(void)
{
if (startofblock('(')) {
struct LinkedSection *cls = current_ls;
struct MemoryDescr *md = cls ? cls->relocmem : vdefmem;
if (!preparse)
parse_expr(md->current,&dataval);
return endofblock('(',')');
}
return FALSE;
}
static void sc_byte(struct GlobalVars *gv)
/* BYTE(data8) */
{
if (get_dataval()) {
datasize = 1;
dataalign = 0;
}
}
static void sc_short(struct GlobalVars *gv)
/* SHORT(data16) */
{
if (get_dataval()) {
datasize = 2;
dataalign = 0; /* @@@ */
}
}
static void sc_long(struct GlobalVars *gv)
/* LONG(data32) */
{
if (get_dataval()) {
datasize = 4;
dataalign = 0; /* @@@ */
}
}
static void sc_quad(struct GlobalVars *gv)
/* QUAD(data64) */
{
if (get_dataval()) {
datasize = 8;
dataalign = 0; /* @@@ */
}
}
static void sc_reserve(struct GlobalVars *gv)
/* RESERVE(space) */
{
if (get_dataval()) {
datasize = -1;
dataalign = 0;
}
}
static void sc_input(struct GlobalVars *gv)
/* INPUT(file1 [file2...]) */
{
if (startofblock('(')) {
char c,*fname;
while (fname = getfilename()) {
struct InputFile *ifn = alloc(sizeof(struct InputFile));
if (strlen(fname)>2 && !strncmp(fname,"-l",2)) {
fname += 2;
while (isspace((unsigned)*fname))
fname++;
ifn->lib = TRUE;
ifn->dynamic = gv->dynamic;
ifn->so_ver = 0; /* @@@ */
}
else
ifn->lib = FALSE;
ifn->name = allocstring(fname);
ifn->flags = 0; /* @@@ add support for clr/set flags? */
ifn->renames = getsecrename();
addtail(&gv->inputlist,&ifn->n);
if ((c = getchr()) == ',')
continue;
back(1);
if (c=='\0' || c==')')
break;
}
endofblock('(',')');
}
}
static void sc_provide(struct GlobalVars *gv)
/* PROVIDE(symbol = expression); */