forked from JWasm/JWasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf.c
1391 lines (1220 loc) · 52.4 KB
/
elf.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
/****************************************************************************
*
* This code is Public Domain.
*
* ========================================================================
*
* Description: ELF output routines
*
****************************************************************************/
#include <ctype.h>
#include <time.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "mangle.h"
#include "fixup.h"
#include "segment.h"
#include "extern.h"
#include "elf.h"
#include "elfspec.h"
#include "myassert.h"
#if ELF_SUPPORT
/* v2.03: create weak externals for ALIAS symbols.
* Since the syntax of the ALIAS directive requires
* 2 names and, OTOH, the ELF implementation of weak
* externals has no "default resolution", the usefullness
* of this option is questionable. Additionally, there's
* the "EXTERN <name> (<altname>)" syntax, which also allows
* to define a weak external.
*/
#define ELFALIAS 0
/* start label is always public for COFF/ELF, no need to add it */
#define ADDSTARTLABEL 0
/* there's no STT_IMPORT type for ELF, it's OW specific */
#define OWELFIMPORT 0
/* use GNU extensions for LD ( 16bit and 8bit relocations ) */
#define GNURELOCS 1
#define MANGLE_BYTES 8 /* extra size required for name decoration */
#define IsWeak( x ) ( x.iscomm == FALSE && x.altname )
/* section attributes for ELF
* execute write alloc type
*---------------------------------------
* CODE x x progbits
* CONST x progbits
* DATA x x progbits
* BSS x x nobits
* STACK x x progbits
* others x x progbits
*
* todo: translate section bits:
* - INFO -> SHT_NOTE (added in v2.07)
* - DISCARD ->
* - SHARED ->
* - EXECUTE -> SHF_EXECINSTR
* - READ ->
* - WRITE -> SHF_WRITE
*/
/* v2.12: global variables replaced by struct elfmod */
//static uint_32 symindex; /* entries in symbol table */
//static uint_32 start_globals; /* start index globals in symbol table */
//static char *srcname; /* name of source module (name + extension) */
//static bool extused;
struct localname {
void *next;
struct asym *sym;
};
enum internal_sections {
SHSTRTAB_IDX,
SYMTAB_IDX,
STRTAB_IDX,
NUM_INTSEGS
};
struct intsegparm {
const char *name;
unsigned type;
};
/* constant parameters of internal sections; order must match enum internal_sections */
static const struct intsegparm internal_segparms[] = {
{ ".shstrtab", SHT_STRTAB },
{ ".symtab", SHT_SYMTAB },
{ ".strtab", SHT_STRTAB },
};
struct intseg {
uint_32 size;
uint_32 fileoffset;
void *data;
};
//static struct intseg internal_segs[NUM_INTSEGS];
/* v2.12: all global variables replaced by struct elfmod */
struct elfmod {
uint_32 symindex; /* entries in symbol table */
uint_32 start_globals; /* start index globals in symbol table */
char *srcname; /* name of source module (name + extension) */
#if GNURELOCS
bool extused; /* gnu extensions used */
#endif
struct intseg internal_segs[NUM_INTSEGS];
union {
Elf32_Ehdr ehdr32;
#if AMD64_SUPPORT
Elf64_Ehdr ehdr64;
#endif
};
};
/* struct to help convert section names in COFF, ELF, PE */
struct conv_section {
uint_8 len;
uint_8 flags; /* see below */
const char *src;
const char *dst;
};
enum cvs_flags {
CSF_GRPCHK = 1
};
static const struct conv_section cst[] = {
{ 5, CSF_GRPCHK, "_TEXT", ".text" },
{ 5, CSF_GRPCHK, "_DATA", ".data" },
{ 5, CSF_GRPCHK, "CONST", ".rodata" }, /* v2.05: .rdata -> .rodata */
{ 4, 0, "_BSS", ".bss" }
};
/* translate section names:
* see cst[] above for details.
*/
static char *ElfConvertSectionName( const struct asym *sym, char *buffer )
/************************************************************************/
{
int i;
for ( i = 0; i < sizeof( cst ) / sizeof( cst[0] ); i++ ) {
if ( memcmp( sym->name, cst[i].src, cst[i].len ) == 0 ) {
if ( sym->name[cst[i].len] == NULLC )
return( (char *)cst[i].dst );
else if ( ( cst[i].flags & CSF_GRPCHK ) && sym->name[cst[i].len] == '$' ) {
strcpy( buffer, cst[i].dst );
strcat( buffer, sym->name+cst[i].len );
return( buffer );
}
}
}
return( sym->name );
}
/* get number of sections that have relocations */
static int get_num_reloc_sections( void )
/***************************************/
{
struct dsym *curr;
int num = 0;
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
if ( curr->e.seginfo->FixupList.head )
num++;
}
return( num );
}
/* fill entries in ELF32 symbol table */
static uint_32 set_symtab32( struct elfmod *em, uint_32 entries, struct localname *localshead )
/*********************************************************************************************/
{
uint_32 strsize = 1;
uint_32 len;
uint_8 stt;
struct dsym *curr;
struct asym *sym;
struct localname *localscurr;
struct qnode *q;
Elf32_Sym *p32;
char buffer[MAX_ID_LEN + MANGLE_BYTES + 1];
em->internal_segs[SYMTAB_IDX].size = entries * sizeof( Elf32_Sym );
em->internal_segs[SYMTAB_IDX].data = LclAlloc( em->internal_segs[SYMTAB_IDX].size );
memset( em->internal_segs[SYMTAB_IDX].data, 0, em->internal_segs[SYMTAB_IDX].size );
p32 = (Elf32_Sym *)em->internal_segs[SYMTAB_IDX].data;
p32++; /* skip NULL entry */
/* 1. make file entry */
p32->st_name = strsize; /* symbol's name in string table */
strsize += strlen( em->srcname ) + 1;
p32->st_value = 0;
p32->st_size = 0;
p32->st_info = ELF32_ST_INFO( STB_LOCAL, STT_FILE ); /* symbol's type and binding info */
p32->st_shndx = SHN_ABS; /* section index */
p32++;
/* 2. make section entries */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
//p32->st_name = ?; /* name isn't set */
p32->st_info = ELF32_ST_INFO( STB_LOCAL, STT_SECTION );
p32->st_shndx = GetSegIdx( curr->sym.segment );
p32++;
}
/* 3. locals */
for ( localscurr = localshead ; localscurr ; localscurr = localscurr->next ) {
len = Mangle( localscurr->sym, buffer );
p32->st_name = strsize;
curr = (struct dsym *)localscurr->sym->segment;
if ( curr && curr->e.seginfo->segtype != SEGTYPE_CODE )
stt = STT_OBJECT;
else
stt = STT_FUNC;
p32->st_info = ELF32_ST_INFO( STB_LOCAL, stt );
p32->st_value = localscurr->sym->offset;
#if 1 /* v2.07: changed - to make MT_ABS obsolete */
if ( curr )
p32->st_shndx = GetSegIdx( &curr->sym );
else
p32->st_shndx = SHN_ABS;
#else
if ( localscurr->sym->mem_type == MT_ABS )
p32->st_shndx = SHN_ABS;
else
p32->st_shndx = GetSegIdx( &curr->sym );
#endif
strsize += len + 1;
DebugMsg(("set_symtab32, LOCAL: symbol %s, value=%X\n", buffer, p32->st_value));
p32++;
}
/* 4. externals + communals (+ protos [since v2.01]) */
for( curr = SymTables[TAB_EXT].head ; curr != NULL ;curr = curr->next ) {
/* skip "weak" (=unused) externdefs */
if ( curr->sym.iscomm == FALSE && curr->sym.weak == TRUE )
continue;
len = Mangle( &curr->sym, buffer );
p32->st_name = strsize;
/* for COMMUNALs, store their size in the Value field */
if ( curr->sym.iscomm == TRUE ) {
p32->st_info = ELF32_ST_INFO( STB_GLOBAL, STT_COMMON );
p32->st_value = curr->sym.total_size;
p32->st_shndx = SHN_COMMON;
} else {
#if OWELFIMPORT
p32->st_info = ( IsWeak( curr->sym ) ? ELF32_ST_INFO( STB_WEAK, STT_IMPORT ) : ELF32_ST_INFO( STB_GLOBAL, STT_IMPORT ) );
#else
/* todo: set STT_FUNC for prototypes/code labels??? */
p32->st_info = ( IsWeak( curr->sym ) ? ELF32_ST_INFO( STB_WEAK, STT_NOTYPE ) : ELF32_ST_INFO( STB_GLOBAL, STT_NOTYPE ) );
#endif
p32->st_value = curr->sym.offset; /* is always 0 */
p32->st_shndx = SHN_UNDEF;
}
strsize += len + 1;
DebugMsg(("set_symtab32, EXTERNAL: symbol %s, info=%X, shndx=%X, value=%X\n", buffer, p32->st_info, p32->st_shndx, p32->st_value));
p32++;
}
#if ELFALIAS
/* 5. aliases */
for( curr = SymTables[TAB_ALIAS].head ; curr != NULL ;curr = curr->next ) {
len = Mangle( &curr->sym, buffer );
p32->st_name = strsize;
#if OWELFIMPORT
p32->st_info = ELF32_ST_INFO( STB_WEAK, STT_IMPORT );
#else
p32->st_info = ELF32_ST_INFO( STB_WEAK, STT_NOTYPE );
#endif
p32->st_value = 0; /* is always 0 */
p32->st_shndx = SHN_UNDEF;
strsize += len + 1;
DebugMsg(("set_symtab32, ALIAS: symbol %s, value=%X\n", buffer, p32->st_value));
p32++;
}
#endif
/* 6. PUBLIC entries */
for ( q = ModuleInfo.g.PubQueue.head; q; q = q->next ) {
sym = q->sym;
len = Mangle( sym, buffer );
curr = (struct dsym *)sym->segment;
if ( curr && curr->e.seginfo->segtype != SEGTYPE_CODE )
stt = STT_OBJECT;
else
stt = STT_FUNC;
p32->st_name = strsize;
p32->st_info = ELF32_ST_INFO( STB_GLOBAL, stt );
p32->st_value = sym->offset;
#if 1 /* v2.07: changed - to make MT_ABS obsolete */
if ( sym->state == SYM_INTERNAL )
if ( curr )
p32->st_shndx = GetSegIdx( &curr->sym );
else
p32->st_shndx = SHN_ABS;
else
p32->st_shndx = SHN_UNDEF;
#else
if ( sym->mem_type == MT_ABS )
p32->st_shndx = SHN_ABS;
else if ( curr )
p32->st_shndx = GetSegIdx( &curr->sym );
else
p32->st_shndx = SHN_UNDEF;
#endif
strsize += len + 1;
DebugMsg(("set_symtab32, PUBLIC+LOCAL: symbol %s, value=%X\n", buffer, p32->st_value));
p32++;
}
#if ADDSTARTLABEL
if ( ModuleInfo.g.start_label ) {
len = Mangle( ModuleInfo.g.start_label, buffer );
p32->st_name = strsize;
p32->st_info = ELF32_ST_INFO( STB_ENTRY, STT_FUNC );
p32->st_value = ModuleInfo.g.start_label->offset;
p32->st_shndx = GetSegIdx( ModuleInfo.g.start_label->segment );
strsize += len + 1;
DebugMsg(("set_symtab32, ENTRY: symbol %s, value=%X\n", buffer, p32->st_value));
p32++;
}
#endif
return( strsize );
}
#if AMD64_SUPPORT
/* fill entries in ELF64 symbol table:
typedef struct {
uint_32 st_name; // +0 symbol name index into string table
uint_8 st_info; // +4 symbol's type and binding attribs.
uint_8 st_other; // +5 no meaning yet.
uint_16 st_shndx; // +6 section index
uint_64 st_value; // +8 symbol "value"
uint_64 st_size; // +16 symbol size
} Elf64_Sym;
*/
static uint_32 set_symtab64( struct elfmod *em, uint_32 entries, struct localname *localshead )
/*********************************************************************************************/
{
uint_32 strsize = 1;
uint_32 len;
uint_8 stt;
struct dsym *curr;
struct asym *sym;
struct localname *localscurr;
struct qnode *q;
Elf64_Sym *p64;
char buffer[MAX_ID_LEN + MANGLE_BYTES + 1];
em->internal_segs[SYMTAB_IDX].size = entries * sizeof( Elf64_Sym );
em->internal_segs[SYMTAB_IDX].data = LclAlloc( em->internal_segs[SYMTAB_IDX].size );
memset( em->internal_segs[SYMTAB_IDX].data, 0, em->internal_segs[SYMTAB_IDX].size );
p64 = (Elf64_Sym *)em->internal_segs[SYMTAB_IDX].data;
p64++; /* skip NULL entry */
/* 1. make file entry */
p64->st_name = strsize; /* symbol's name in string table */
strsize += strlen( em->srcname ) + 1;
p64->st_value = 0;
p64->st_size = 0;
p64->st_info = ELF64_ST_INFO( STB_LOCAL, STT_FILE ); /* symbol's type and binding info */
p64->st_shndx = SHN_ABS; /* section index */
p64++;
/* 2. make section entries */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
//p64->st_name = ?; /* name isn't set */
p64->st_info = ELF64_ST_INFO( STB_LOCAL, STT_SECTION );
p64->st_shndx = GetSegIdx( curr->sym.segment );
p64++;
}
/* 3. locals */
for ( localscurr = localshead ; localscurr ; localscurr = localscurr->next ) {
len = Mangle( localscurr->sym, buffer );
p64->st_name = strsize;
curr = (struct dsym *)localscurr->sym->segment;
if ( curr && curr->e.seginfo->segtype != SEGTYPE_CODE )
stt = STT_OBJECT;
else
stt = STT_FUNC;
p64->st_info = ELF64_ST_INFO( STB_LOCAL, stt );
p64->st_value = localscurr->sym->offset;
#if 1 /* v2.07: changed - to make MT_ABS obsolete */
if ( curr )
p64->st_shndx = GetSegIdx( &curr->sym );
else
p64->st_shndx = SHN_ABS;
#else
if ( localscurr->sym->mem_type == MT_ABS )
p64->st_shndx = SHN_ABS;
else
p64->st_shndx = GetSegIdx( &curr->sym );
#endif
strsize += len + 1;
DebugMsg(("set_symtab64, LOCAL: symbol %s, value=%" I64_SPEC "X\n", buffer, p64->st_value));
p64++;
}
/* 4. externals + communals ( + protos [since v2.01]) */
for( curr = SymTables[TAB_EXT].head ; curr != NULL ;curr = curr->next ) {
/* skip "weak" (=unused) externdefs */
if ( curr->sym.iscomm == FALSE && curr->sym.weak == TRUE )
continue;
len = Mangle( &curr->sym, buffer );
p64->st_name = strsize;
/* for COMMUNALs, store their size in the Value field */
if ( curr->sym.iscomm == TRUE ) {
p64->st_info = ELF64_ST_INFO( STB_GLOBAL, STT_COMMON );
p64->st_value = curr->sym.total_size;
p64->st_shndx = SHN_COMMON;
} else {
#if OWELFIMPORT
p64->st_info = ( IsWeak( curr->sym ) ? ELF64_ST_INFO( STB_WEAK, STT_IMPORT ) : ELF64_ST_INFO( STB_GLOBAL, STT_IMPORT ) );
#else
/* todo: set STT_FUNC for prototypes??? */
p64->st_info = ( IsWeak( curr->sym ) ? ELF64_ST_INFO( STB_WEAK, STT_NOTYPE ) : ELF64_ST_INFO( STB_GLOBAL, STT_NOTYPE ) );
#endif
p64->st_value = curr->sym.offset; /* is always 0 */
p64->st_shndx = SHN_UNDEF;
}
strsize += len + 1;
DebugMsg(("set_symtab64, EXTERNAL: symbol %s, info=%X, shndx=%X, value=%" I64_SPEC "X\n", buffer, p64->st_info, p64->st_shndx, p64->st_value));
p64++;
}
#if ELFALIAS
/* 5. aliases */
for( curr = SymTables[TAB_ALIAS].head ; curr != NULL ;curr = curr->next ) {
len = Mangle( &curr->sym, buffer );
p64->st_name = strsize;
#if OWELFIMPORT
p64->st_info = ELF64_ST_INFO( STB_WEAK, STT_IMPORT );
#else
p64->st_info = ELF64_ST_INFO( STB_WEAK, STT_NOTYPE );
#endif
p64->st_value = 0; /* is always 0 */
p64->st_shndx = SHN_UNDEF;
strsize += len + 1;
DebugMsg(("set_symtab64, ALIASES: symbol %s, value=%" I64_SPEC "X\n", buffer, p64->st_value));
p64++;
}
#endif
/* 6. PUBLIC entries */
for ( q = ModuleInfo.g.PubQueue.head; q; q = q->next ) {
sym = q->sym;
len = Mangle( sym, buffer );
curr = (struct dsym *)sym->segment;
if ( curr && curr->e.seginfo->segtype != SEGTYPE_CODE )
stt = STT_OBJECT;
else
stt = STT_FUNC;
p64->st_name = strsize;
p64->st_info = ELF64_ST_INFO( STB_GLOBAL, stt );
p64->st_value = sym->offset;
#if 1 /* v2.07: changed - to make MT_ABS obsolete */
if ( sym->state == SYM_INTERNAL )
if ( curr )
p64->st_shndx = GetSegIdx( &curr->sym );
else
p64->st_shndx = SHN_ABS;
else
p64->st_shndx = SHN_UNDEF;
#else
if ( sym->mem_type == MT_ABS )
p64->st_shndx = SHN_ABS;
else if ( curr )
p64->st_shndx = GetSegIdx( &curr->sym );
else
p64->st_shndx = SHN_UNDEF;
#endif
strsize += len + 1;
DebugMsg(("set_symtab64, PUBLIC+LOCAL: symbol %s, info=%X, shndx=%X, value=%" I64_SPEC "X\n", buffer, p64->st_info, p64->st_shndx, p64->st_value));
p64++;
}
#if ADDSTARTLABEL
if ( ModuleInfo.g.start_label ) {
len = Mangle( ModuleInfo.g.start_label, buffer );
p64->st_name = strsize;
p64->st_info = ELF64_ST_INFO( STB_ENTRY, STT_FUNC );
p64->st_value = ModuleInfo.g.start_label->offset;
p64->st_shndx = GetSegIdx( ModuleInfo.g.start_label->segment );
strsize += len + 1;
DebugMsg(("set_symtab64, ENTRY: symbol %s, value=%" I64_SPEC "X\n", buffer, p64->st_value));
p64++;
}
#endif
return( strsize );
}
#endif
/* calculate size of .symtab + .strtab section.
* set content of these sections.
*/
static void set_symtab_values( struct elfmod *em )
/************************************************/
{
uint_32 strsize;
uint_32 entries;
struct dsym *curr;
//struct asym *sym;
struct qnode *q;
char *p2;
struct {
struct localname *head;
struct localname *tail;
} locals = { NULL, NULL };
struct localname *localscurr;
/* symbol table. there is
- 1 NULL entry,
- 1 entry for the module/file,
- 1 entry for each section and
- n entries for local symbols
- m entries for global symbols
*/
/* symbol table starts with 1 NULL entry + 1 file entry */
em->symindex = 1 + 1;
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next )
curr->sym.ext_idx = em->symindex++;
/* add local symbols to symbol table */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
if ( curr->e.seginfo->num_relocs ) {
struct fixup *fix = curr->e.seginfo->FixupList.head;
for ( ; fix; fix = fix->nextrlc ) {
/* if it's not EXTERNAL/PUBLIC, add symbol. */
/* however, if it's an assembly time variable */
/* use a raw section reference */
if ( fix->sym->variable ) {
fix->sym = fix->segment_var;
} else if ( ( fix->sym->state == SYM_INTERNAL ) &&
fix->sym->included == FALSE &&
fix->sym->ispublic == FALSE ) {
fix->sym->included = TRUE;
localscurr = LclAlloc( sizeof( struct localname ) );
localscurr->next = NULL;
localscurr->sym = fix->sym;
if (locals.tail) {
locals.tail->next = localscurr;
locals.tail = localscurr;
} else {
locals.head = locals.tail = localscurr;
}
fix->sym->ext_idx = em->symindex++;
}
}
}
}
DebugMsg(("set_symtab_values: index after sections: %u\n", em->symindex));
em->start_globals = em->symindex;
/* count EXTERNs and used EXTERNDEFs (and PROTOs [since v2.01]) */
for( curr = SymTables[TAB_EXT].head ; curr != NULL ;curr = curr->next ) {
if ( curr->sym.iscomm == FALSE && curr->sym.weak == TRUE )
continue;
curr->sym.ext_idx = em->symindex++;
}
DebugMsg(("set_symtab_values: index after EXTERNALs: %u\n", em->symindex));
#if ELFALIAS
/* count aliases */
for( curr = SymTables[TAB_ALIAS].head ; curr != NULL ;curr = curr->next ) {
curr->sym.idx = em->symindex++;
}
DebugMsg(("set_symtab_values: index after ALIASES: %u\n", em->symindex));
#endif
/* count publics */
for ( q = ModuleInfo.g.PubQueue.head; q; q = q->next ) {
q->sym->ext_idx = em->symindex++;
}
DebugMsg(("set_symtab_values: index after PUBLICs: %u\n", em->symindex));
/* size of symbol table is defined */
entries = em->symindex;
#if ADDSTARTLABEL
if ( ModuleInfo.g.start_label )
entries++;
#endif
#if AMD64_SUPPORT
if ( ModuleInfo.defOfssize == USE64 )
strsize = set_symtab64( em, entries, locals.head );
else
#endif
strsize = set_symtab32( em, entries, locals.head );
/* generate the string table */
DebugMsg(("set_symtab_values: creating string table, size=%X\n", strsize));
em->internal_segs[STRTAB_IDX].size = strsize;
em->internal_segs[STRTAB_IDX].data = LclAlloc( strsize );
memset( em->internal_segs[STRTAB_IDX].data, 0, strsize );
p2 = em->internal_segs[STRTAB_IDX].data;
*p2++ = NULLC;
strcpy( p2, em->srcname );
p2 += strlen( p2 ) + 1;
for ( localscurr = locals.head ; localscurr ; localscurr = localscurr->next ) {
p2 += Mangle( localscurr->sym, p2 ) + 1;
}
for( curr = SymTables[TAB_EXT].head ; curr != NULL ;curr = curr->next ) {
if ( curr->sym.iscomm == FALSE && curr->sym.weak == TRUE )
continue;
p2 += Mangle( &curr->sym, p2 ) + 1;
}
#if ELFALIAS
for( curr = SymTables[TAB_ALIAS].head ; curr != NULL ;curr = curr->next ) {
p2 += Mangle( &curr->sym, p2 ) + 1;
}
#endif
for ( q = ModuleInfo.g.PubQueue.head; q; q = q->next ) {
p2 += Mangle( q->sym, p2 ) + 1;
}
#if ADDSTARTLABEL
if ( ModuleInfo.g.start_label ) {
Mangle( ModuleInfo.g.start_label, p2 );
}
#endif
DebugMsg(("set_symtab_values: exit, symindex=%u\n", em->symindex ));
return;
}
/* set content + size of .shstrtab section.
* this section contains the names of all sections.
* three groups of sections are handled:
* - sections defined in the program
* - ELF internal sections
* - relocation sections
* alloc .shstrtab
*/
static void set_shstrtab_values( struct elfmod *em )
/**************************************************/
{
int i;
struct dsym *curr;
char *p;
unsigned int size = 1; /* the first byte at offset 0 is the NULL section name */
char buffer[MAX_ID_LEN+1];
/* get size of section names defined in the program & relocation sections ) */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
/* v2.07: ALIAS name defined? */
p = ( curr->e.seginfo->aliasname ? curr->e.seginfo->aliasname : ElfConvertSectionName( &curr->sym, buffer ) );
size += strlen( p ) + 1;
if ( curr->e.seginfo->FixupList.head )
size += strlen( p ) +
#if AMD64_SUPPORT
(( ModuleInfo.defOfssize == USE64 ) ? sizeof(".rela") : sizeof(".rel"));
#else
sizeof(".rel");
#endif
}
/* get internal section name sizes */
for ( i = 0; i < NUM_INTSEGS; i++ ) {
size += strlen( internal_segparms[i].name ) + 1;
}
em->internal_segs[SHSTRTAB_IDX].size = size;
/* size is known, now alloc .shstrtab data buffer and fill it */
em->internal_segs[SHSTRTAB_IDX].data = LclAlloc( size );
p = (char *)em->internal_segs[SHSTRTAB_IDX].data;
*p++ = NULLC; /* NULL section name */
/* 1. names of program sections */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
strcpy( p, curr->e.seginfo->aliasname ? curr->e.seginfo->aliasname : ElfConvertSectionName( &curr->sym, buffer ) );
p += strlen( p ) + 1;
}
/* 2. names of internal sections */
for ( i = 0; i < NUM_INTSEGS; i++ ) {
strcpy( p, internal_segparms[i].name );
p += strlen( p ) + 1;
}
/* 3. names of "relocation" sections */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
if ( curr->e.seginfo->FixupList.head ) {
#if AMD64_SUPPORT
strcpy( p, (( ModuleInfo.defOfssize == USE64 ) ? ".rela": ".rel") );
#else
strcpy( p, ".rel" );
#endif
p += strlen( p );
strcpy( p, curr->e.seginfo->aliasname ? curr->e.seginfo->aliasname : ElfConvertSectionName( &curr->sym, buffer ) );
p += strlen( p ) + 1;
}
}
/**/myassert( size == p - (char *)em->internal_segs[SHSTRTAB_IDX].data );
DebugMsg(("set_shstrtab_values: size=%X\n", size));
return;
}
static unsigned int get_relocation_count( struct dsym *curr )
/***********************************************************/
{
unsigned relocs;
struct fixup *fix;
for ( relocs = 0, fix = curr->e.seginfo->FixupList.head; fix ; fix = fix->nextrlc, relocs++ );
return( relocs );
}
static unsigned int Get_Alignment( struct dsym *curr )
/****************************************************/
{
if ( curr->e.seginfo->alignment == MAX_SEGALIGNMENT )
return( 0 );
return( 1 << curr->e.seginfo->alignment );
}
/* write ELF32 section table.
* fileoffset: start of section data ( behind elf header and section table )
* there are 3 groups of sections to handle:
* - the sections defined in the module
* - the internal ELF sections ( .shstrtab, .symtab, .strtab )
* - the 'relocation' sections
*/
static int elf_write_section_table32( struct module_info *modinfo, struct elfmod *em, uint_32 fileoffset )
/********************************************************************************************************/
{
int i;
struct dsym *curr;
uint_8 *p;
//uint_32 fileoffset;
Elf32_Shdr shdr32;
DebugMsg(("elf_write_section_table32( fileofs=%X ): enter\n", fileoffset ));
//fileoffset = sizeof(Elf64_Ehdr) + ehdr->e_shnum * ehdr->e_shentsize;
fileoffset = (fileoffset + 0xF) & ~0xF;
/* set contents and size of internal .shstrtab section */
set_shstrtab_values( em );
/* write the NULL entry */
memset( &shdr32, 0, sizeof( shdr32) );
if ( fwrite( &shdr32, 1, sizeof(shdr32), CurrFile[OBJ] ) != sizeof(shdr32) ) /* write the empty NULL entry */
WriteError();
/* use p to scan strings (=section names) of .shstrtab */
p = (uint_8 *)em->internal_segs[SHSTRTAB_IDX].data;
p++; /* skip 'name' of NULL entry */
/* write the section headers defined in the module, */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
memset( &shdr32, 0, sizeof(shdr32) );
shdr32.sh_name = p - (uint_8 *)em->internal_segs[SHSTRTAB_IDX].data;
p += strlen( (char *)p ) + 1;
if ( curr->e.seginfo->info == TRUE ) { /* v2.07:added; v2.12: highest priority */
shdr32.sh_type = SHT_NOTE;
shdr32.sh_flags = 0;
} else {
shdr32.sh_type = ( curr->e.seginfo->segtype != SEGTYPE_BSS ? SHT_PROGBITS : SHT_NOBITS );
if ( curr->e.seginfo->segtype == SEGTYPE_CODE ) {
shdr32.sh_flags = SHF_EXECINSTR | SHF_ALLOC;
} else if ( curr->e.seginfo->readonly == TRUE ) {
shdr32.sh_flags = SHF_ALLOC;
} else if ( curr->e.seginfo->clsym && strcmp( curr->e.seginfo->clsym->name, "CONST" ) == 0 ) {
shdr32.sh_flags = SHF_ALLOC; /* v2.07: added */
} else {
shdr32.sh_flags = SHF_WRITE | SHF_ALLOC;
}
}
#if 0
/* todo: translate values in field <characteristics> to
* elf section flags.
*/
if ( curr->e.seginfo->characteristics == ??? ) {
}
#endif
shdr32.sh_addr = 0;
/* v2.12: the sh_offset field holds the file position, even for SHT_NOBITS */
//if ( shdr32.sh_type != SHT_NOBITS ) {
shdr32.sh_offset = fileoffset; /* start of section in file */
curr->e.seginfo->fileoffset = fileoffset; /* save the offset in the segment */
//}
/* v2.07: set size for all sections, including .bss */
shdr32.sh_size = curr->sym.max_offset;
shdr32.sh_link = 0;
shdr32.sh_info = 0;
shdr32.sh_addralign = Get_Alignment( curr );
shdr32.sh_entsize = 0;
if ( fwrite( &shdr32, 1, sizeof(shdr32), CurrFile[OBJ] ) != sizeof(shdr32) )
WriteError();
curr->e.seginfo->num_relocs = get_relocation_count( curr );
/* v2.12: don't adjust fileoffset for SHT_NOBITS sections.
* it didn't cause fatal damage previously, but made the
* object module unnecessary large.
*/
if ( shdr32.sh_type != SHT_NOBITS ) {
fileoffset += shdr32.sh_size;
fileoffset = (fileoffset + 0xF) & ~0xF;
}
DebugMsg(("elf_write_section_table32(%s): ofs=%X size=%X numrelocs=%u\n", curr->sym.name, shdr32.sh_offset, shdr32.sh_size, curr->e.seginfo->num_relocs));
}
/* set size and contents of .symtab and .strtab sections */
set_symtab_values( em );
/* write headers of internal sections */
for ( i = 0; i < NUM_INTSEGS; i++ ) {
shdr32.sh_name = p - (uint_8 *)em->internal_segs[SHSTRTAB_IDX].data;
p += strlen( (char *)p ) + 1;
shdr32.sh_type = internal_segparms[i].type;
shdr32.sh_flags = 0;
shdr32.sh_offset = fileoffset; /* start of section in file */
em->internal_segs[i].fileoffset = fileoffset;
shdr32.sh_size = em->internal_segs[i].size;
/* section .symtab is special */
if ( i == SYMTAB_IDX ) {
shdr32.sh_link = 1 + modinfo->g.num_segs + STRTAB_IDX;
shdr32.sh_info = em->start_globals;
shdr32.sh_addralign = 4;
shdr32.sh_entsize = sizeof( Elf32_Sym );
} else {
shdr32.sh_link = 0;
shdr32.sh_info = 0;
shdr32.sh_addralign = 1;
shdr32.sh_entsize = 0;
}
if ( fwrite( &shdr32, 1, sizeof( shdr32 ), CurrFile[OBJ] ) != sizeof( shdr32 ) )
WriteError();
fileoffset += shdr32.sh_size;
fileoffset = (fileoffset + 0xF) & ~0xF;
DebugMsg(("elf_write_section_table32(%s): ofs=%X size=%X\n", internal_segparms[i].name, shdr32.sh_offset, shdr32.sh_size));
}
/* write headers of reloc sections */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
if ( curr->e.seginfo->FixupList.head == NULL )
continue;
memset( &shdr32, 0, sizeof( shdr32 ) );
shdr32.sh_name = p - (uint_8 *)em->internal_segs[SHSTRTAB_IDX].data;
p += strlen( (char *)p ) + 1;
shdr32.sh_type = SHT_REL;
shdr32.sh_flags = 0;
shdr32.sh_addr = 0;
shdr32.sh_offset = fileoffset; /* start of section in file */
/* save the file offset in the slot reserved for ELF relocs */
curr->e.seginfo->reloc_offset = fileoffset;
/* size of section in file */
shdr32.sh_size = curr->e.seginfo->num_relocs * sizeof( Elf32_Rel );
shdr32.sh_link = 1 + modinfo->g.num_segs + SYMTAB_IDX;
/* set info to the src section index */
shdr32.sh_info = GetSegIdx( curr->sym.segment );
shdr32.sh_addralign = 4;
shdr32.sh_entsize = sizeof( Elf32_Rel );
if ( fwrite( &shdr32, 1, sizeof( shdr32 ), CurrFile[OBJ] ) != sizeof( shdr32 ) )
WriteError();
fileoffset += shdr32.sh_size;
fileoffset = (fileoffset + 0xF) & ~0xF;
DebugMsg(("elf_write_section_table32(%s): relocs, ofs=%X size=%X\n", curr->sym.name, shdr32.sh_offset, shdr32.sh_size));
}
DebugMsg(("elf_write_section_table32: exit, final fileofs=%X\n", fileoffset ));
return( NOT_ERROR );
}
#if AMD64_SUPPORT
/* write ELF64 section table. */
static int elf_write_section_table64( struct module_info *modinfo, struct elfmod *em, uint_32 fileoffset )
/********************************************************************************************************/
{
int i;
struct dsym *curr;
uint_8 *p;
//uint_32 fileoffset;
Elf64_Shdr shdr64;
DebugMsg(("elf_write_section_table64( fileofs=%X ): enter\n", fileoffset ));
//fileoffset = sizeof(Elf64_Ehdr) + ehdr->e_shnum * ehdr->e_shentsize;
fileoffset = (fileoffset + 0xF) & ~0xF;
/* set contents and size of internal .shstrtab section */
set_shstrtab_values( em );
/* write the NULL entry */
memset( &shdr64, 0, sizeof( shdr64) );
if ( fwrite( &shdr64, 1, sizeof(shdr64), CurrFile[OBJ] ) != sizeof(shdr64) ) /* write the empty NULL entry */
WriteError();
/* use p to scan strings (=section names) of .shstrtab */
p = (uint_8 *)em->internal_segs[SHSTRTAB_IDX].data;
p++; /* skip 'name' of NULL entry */
/* write the section headers defined in the module */
for( curr = SymTables[TAB_SEG].head; curr; curr = curr->next ) {
memset( &shdr64, 0, sizeof(shdr64) );
shdr64.sh_name = p - (uint_8 *)em->internal_segs[SHSTRTAB_IDX].data;
p += strlen( (char *)p ) + 1;
if ( curr->e.seginfo->info == TRUE ) { /* v2.07:added; v2.12: highest priority */
shdr64.sh_type = SHT_NOTE;
shdr64.sh_flags = 0;
} else {
shdr64.sh_type = ( curr->e.seginfo->segtype != SEGTYPE_BSS ? SHT_PROGBITS : SHT_NOBITS );
if ( curr->e.seginfo->segtype == SEGTYPE_CODE ) {
shdr64.sh_flags = SHF_EXECINSTR | SHF_ALLOC;
} else if ( curr->e.seginfo->readonly == TRUE ) {
shdr64.sh_flags = SHF_ALLOC;
} else if ( curr->e.seginfo->clsym && strcmp( curr->e.seginfo->clsym->name, "CONST" ) == 0 ) {
shdr64.sh_flags = SHF_ALLOC; /* v2.07: added */
} else {
shdr64.sh_flags = SHF_WRITE | SHF_ALLOC;
}
}
#if 0
/* todo: translate values in field <characteristics> to
* elf section flags.
*/
if ( curr->e.seginfo->characteristics == ??? ) {
}
#endif
shdr64.sh_addr = 0;
/* v2.12: the sh_offset field holds the file position, even for SHT_NOBITS */
//if ( shdr64.sh_type != SHT_NOBITS ) {
shdr64.sh_offset = fileoffset; /* start of section in file */
curr->e.seginfo->fileoffset = fileoffset; /* save the offset in the segment */
//}
/* v2.07: set size for all sections, including .bss */
shdr64.sh_size = curr->sym.max_offset;
shdr64.sh_link = 0;
shdr64.sh_info = 0;
shdr64.sh_addralign = Get_Alignment( curr );
shdr64.sh_entsize = 0;
if ( fwrite( &shdr64, 1, sizeof(shdr64), CurrFile[OBJ] ) != sizeof(shdr64) )
WriteError();
curr->e.seginfo->num_relocs = get_relocation_count( curr );