forked from TinyCC/tinycc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tccelf.c
4017 lines (3689 loc) · 128 KB
/
tccelf.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
/*
* ELF file handling for TCC
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tcc.h"
/* Define this to get some debug output during relocation processing. */
#undef DEBUG_RELOC
/********************************************************/
/* global variables */
/* elf version information */
struct sym_version {
char *lib;
char *version;
int out_index;
int prev_same_lib;
};
#define nb_sym_versions s1->nb_sym_versions
#define sym_versions s1->sym_versions
#define nb_sym_to_version s1->nb_sym_to_version
#define sym_to_version s1->sym_to_version
#define dt_verneednum s1->dt_verneednum
#define versym_section s1->versym_section
#define verneed_section s1->verneed_section
/* special flag to indicate that the section should not be linked to the other ones */
#define SHF_PRIVATE 0x80000000
/* section is dynsymtab_section */
#define SHF_DYNSYM 0x40000000
#ifdef TCC_TARGET_PE
#define shf_RELRO SHF_ALLOC
static const char rdata[] = ".rdata";
#else
#define shf_RELRO s1->shf_RELRO
static const char rdata[] = ".data.ro";
#endif
/* ------------------------------------------------------------------------- */
ST_FUNC void tccelf_new(TCCState *s)
{
TCCState *s1 = s;
#ifndef TCC_TARGET_PE
shf_RELRO = SHF_ALLOC;
if (s1->output_type != TCC_OUTPUT_MEMORY)
shf_RELRO |= SHF_WRITE; /* the ELF loader will set it to RO at runtime */
#endif
/* no section zero */
dynarray_add(&s->sections, &s->nb_sections, NULL);
/* create standard sections */
text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
/* create ro data section (make ro after relocation done with GNU_RELRO) */
rodata_section = new_section(s, rdata, SHT_PROGBITS, shf_RELRO);
bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
common_section = new_section(s, ".common", SHT_NOBITS, SHF_PRIVATE);
common_section->sh_num = SHN_COMMON;
/* symbols are always generated for linking stage */
symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
".strtab",
".hashtab", SHF_PRIVATE);
/* private symbol table for dynamic symbols */
s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE|SHF_DYNSYM,
".dynstrtab",
".dynhashtab", SHF_PRIVATE);
get_sym_attr(s, 0, 1);
if (s->do_debug) {
/* add debug sections */
tcc_debug_new(s);
}
#ifdef CONFIG_TCC_BCHECK
if (s->do_bounds_check) {
/* if bound checking, then add corresponding sections */
/* (make ro after relocation done with GNU_RELRO) */
bounds_section = new_section(s, ".bounds", SHT_PROGBITS, shf_RELRO);
lbounds_section = new_section(s, ".lbounds", SHT_PROGBITS, shf_RELRO);
}
#endif
}
ST_FUNC void free_section(Section *s)
{
if (!s)
return;
tcc_free(s->data);
s->data = NULL;
s->data_allocated = s->data_offset = 0;
}
ST_FUNC void tccelf_delete(TCCState *s1)
{
int i;
#ifndef ELF_OBJ_ONLY
/* free symbol versions */
for (i = 0; i < nb_sym_versions; i++) {
tcc_free(sym_versions[i].version);
tcc_free(sym_versions[i].lib);
}
tcc_free(sym_versions);
tcc_free(sym_to_version);
#endif
/* free all sections */
for(i = 1; i < s1->nb_sections; i++)
free_section(s1->sections[i]);
dynarray_reset(&s1->sections, &s1->nb_sections);
for(i = 0; i < s1->nb_priv_sections; i++)
free_section(s1->priv_sections[i]);
dynarray_reset(&s1->priv_sections, &s1->nb_priv_sections);
tcc_free(s1->sym_attrs);
symtab_section = NULL; /* for tccrun.c:rt_printline() */
}
/* save section data state */
ST_FUNC void tccelf_begin_file(TCCState *s1)
{
Section *s; int i;
for (i = 1; i < s1->nb_sections; i++) {
s = s1->sections[i];
s->sh_offset = s->data_offset;
}
/* disable symbol hashing during compilation */
s = s1->symtab, s->reloc = s->hash, s->hash = NULL;
#if defined TCC_TARGET_X86_64 && defined TCC_TARGET_PE
s1->uw_sym = 0;
#endif
}
/* At the end of compilation, convert any UNDEF syms to global, and merge
with previously existing symbols */
ST_FUNC void tccelf_end_file(TCCState *s1)
{
Section *s = s1->symtab;
int first_sym, nb_syms, *tr, i;
first_sym = s->sh_offset / sizeof (ElfSym);
nb_syms = s->data_offset / sizeof (ElfSym) - first_sym;
s->data_offset = s->sh_offset;
s->link->data_offset = s->link->sh_offset;
s->hash = s->reloc, s->reloc = NULL;
tr = tcc_mallocz(nb_syms * sizeof *tr);
for (i = 0; i < nb_syms; ++i) {
ElfSym *sym = (ElfSym*)s->data + first_sym + i;
if (sym->st_shndx == SHN_UNDEF) {
int sym_bind = ELFW(ST_BIND)(sym->st_info);
int sym_type = ELFW(ST_TYPE)(sym->st_info);
if (sym_bind == STB_LOCAL)
sym_bind = STB_GLOBAL;
#ifndef TCC_TARGET_PE
if (sym_bind == STB_GLOBAL && s1->output_type == TCC_OUTPUT_OBJ) {
/* undefined symbols with STT_FUNC are confusing gnu ld when
linking statically to STT_GNU_IFUNC */
sym_type = STT_NOTYPE;
}
#endif
sym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
}
tr[i] = set_elf_sym(s, sym->st_value, sym->st_size, sym->st_info,
sym->st_other, sym->st_shndx, (char*)s->link->data + sym->st_name);
}
/* now update relocations */
for (i = 1; i < s1->nb_sections; i++) {
Section *sr = s1->sections[i];
if (sr->sh_type == SHT_RELX && sr->link == s) {
ElfW_Rel *rel = (ElfW_Rel*)(sr->data + sr->sh_offset);
ElfW_Rel *rel_end = (ElfW_Rel*)(sr->data + sr->data_offset);
for (; rel < rel_end; ++rel) {
int n = ELFW(R_SYM)(rel->r_info) - first_sym;
if (n < 0) /* zero sym_index in reloc (can happen with asm) */
continue;
rel->r_info = ELFW(R_INFO)(tr[n], ELFW(R_TYPE)(rel->r_info));
}
}
}
tcc_free(tr);
/* record text/data/bss output for -bench info */
for (i = 0; i < 4; ++i) {
s = s1->sections[i + 1];
s1->total_output[i] += s->data_offset - s->sh_offset;
}
}
ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
{
Section *sec;
sec = tcc_mallocz(sizeof(Section) + strlen(name));
sec->s1 = s1;
strcpy(sec->name, name);
sec->sh_type = sh_type;
sec->sh_flags = sh_flags;
switch(sh_type) {
case SHT_GNU_versym:
sec->sh_addralign = 2;
break;
case SHT_HASH:
case SHT_GNU_HASH:
case SHT_REL:
case SHT_RELA:
case SHT_DYNSYM:
case SHT_SYMTAB:
case SHT_DYNAMIC:
case SHT_GNU_verneed:
case SHT_GNU_verdef:
sec->sh_addralign = PTR_SIZE;
break;
case SHT_STRTAB:
sec->sh_addralign = 1;
break;
default:
sec->sh_addralign = PTR_SIZE; /* gcc/pcc default alignment */
break;
}
if (sh_flags & SHF_PRIVATE) {
dynarray_add(&s1->priv_sections, &s1->nb_priv_sections, sec);
} else {
sec->sh_num = s1->nb_sections;
dynarray_add(&s1->sections, &s1->nb_sections, sec);
}
return sec;
}
ST_FUNC void init_symtab(Section *s)
{
int *ptr, nb_buckets = 1;
put_elf_str(s->link, "");
section_ptr_add(s, sizeof (ElfW(Sym)));
ptr = section_ptr_add(s->hash, (2 + nb_buckets + 1) * sizeof(int));
ptr[0] = nb_buckets;
ptr[1] = 1;
memset(ptr + 2, 0, (nb_buckets + 1) * sizeof(int));
}
ST_FUNC Section *new_symtab(TCCState *s1,
const char *symtab_name, int sh_type, int sh_flags,
const char *strtab_name,
const char *hash_name, int hash_sh_flags)
{
Section *symtab, *strtab, *hash;
symtab = new_section(s1, symtab_name, sh_type, sh_flags);
symtab->sh_entsize = sizeof(ElfW(Sym));
strtab = new_section(s1, strtab_name, SHT_STRTAB, sh_flags);
symtab->link = strtab;
hash = new_section(s1, hash_name, SHT_HASH, hash_sh_flags);
hash->sh_entsize = sizeof(int);
symtab->hash = hash;
hash->link = symtab;
init_symtab(symtab);
return symtab;
}
/* realloc section and set its content to zero */
ST_FUNC void section_realloc(Section *sec, unsigned long new_size)
{
unsigned long size;
unsigned char *data;
size = sec->data_allocated;
if (size == 0)
size = 1;
while (size < new_size)
size = size * 2;
data = tcc_realloc(sec->data, size);
memset(data + sec->data_allocated, 0, size - sec->data_allocated);
sec->data = data;
sec->data_allocated = size;
}
/* reserve at least 'size' bytes aligned per 'align' in section
'sec' from current offset, and return the aligned offset */
ST_FUNC size_t section_add(Section *sec, addr_t size, int align)
{
size_t offset, offset1;
offset = (sec->data_offset + align - 1) & -align;
offset1 = offset + size;
if (sec->sh_type != SHT_NOBITS && offset1 > sec->data_allocated)
section_realloc(sec, offset1);
sec->data_offset = offset1;
if (align > sec->sh_addralign)
sec->sh_addralign = align;
return offset;
}
/* reserve at least 'size' bytes in section 'sec' from
sec->data_offset. */
ST_FUNC void *section_ptr_add(Section *sec, addr_t size)
{
size_t offset = section_add(sec, size, 1);
return sec->data + offset;
}
#ifndef ELF_OBJ_ONLY
/* reserve at least 'size' bytes from section start */
static void section_reserve(Section *sec, unsigned long size)
{
if (size > sec->data_allocated)
section_realloc(sec, size);
if (size > sec->data_offset)
sec->data_offset = size;
}
#endif
static Section *have_section(TCCState *s1, const char *name)
{
Section *sec;
int i;
for(i = 1; i < s1->nb_sections; i++) {
sec = s1->sections[i];
if (!strcmp(name, sec->name))
return sec;
}
return NULL;
}
/* return a reference to a section, and create it if it does not
exists */
ST_FUNC Section *find_section(TCCState *s1, const char *name)
{
Section *sec = have_section(s1, name);
if (sec)
return sec;
/* sections are created as PROGBITS */
return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
}
/* ------------------------------------------------------------------------- */
ST_FUNC int put_elf_str(Section *s, const char *sym)
{
int offset, len;
char *ptr;
len = strlen(sym) + 1;
offset = s->data_offset;
ptr = section_ptr_add(s, len);
memmove(ptr, sym, len);
return offset;
}
/* elf symbol hashing function */
static ElfW(Word) elf_hash(const unsigned char *name)
{
ElfW(Word) h = 0, g;
while (*name) {
h = (h << 4) + *name++;
g = h & 0xf0000000;
if (g)
h ^= g >> 24;
h &= ~g;
}
return h;
}
/* rebuild hash table of section s */
/* NOTE: we do factorize the hash table code to go faster */
static void rebuild_hash(Section *s, unsigned int nb_buckets)
{
ElfW(Sym) *sym;
int *ptr, *hash, nb_syms, sym_index, h;
unsigned char *strtab;
strtab = s->link->data;
nb_syms = s->data_offset / sizeof(ElfW(Sym));
if (!nb_buckets)
nb_buckets = ((int*)s->hash->data)[0];
s->hash->data_offset = 0;
ptr = section_ptr_add(s->hash, (2 + nb_buckets + nb_syms) * sizeof(int));
ptr[0] = nb_buckets;
ptr[1] = nb_syms;
ptr += 2;
hash = ptr;
memset(hash, 0, (nb_buckets + 1) * sizeof(int));
ptr += nb_buckets + 1;
sym = (ElfW(Sym) *)s->data + 1;
for(sym_index = 1; sym_index < nb_syms; sym_index++) {
if (ELFW(ST_BIND)(sym->st_info) != STB_LOCAL) {
h = elf_hash(strtab + sym->st_name) % nb_buckets;
*ptr = hash[h];
hash[h] = sym_index;
} else {
*ptr = 0;
}
ptr++;
sym++;
}
}
/* return the symbol number */
ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size,
int info, int other, int shndx, const char *name)
{
int name_offset, sym_index;
int nbuckets, h;
ElfW(Sym) *sym;
Section *hs;
sym = section_ptr_add(s, sizeof(ElfW(Sym)));
if (name && name[0])
name_offset = put_elf_str(s->link, name);
else
name_offset = 0;
/* XXX: endianness */
sym->st_name = name_offset;
sym->st_value = value;
sym->st_size = size;
sym->st_info = info;
sym->st_other = other;
sym->st_shndx = shndx;
sym_index = sym - (ElfW(Sym) *)s->data;
hs = s->hash;
if (hs) {
int *ptr, *base;
ptr = section_ptr_add(hs, sizeof(int));
base = (int *)hs->data;
/* only add global or weak symbols. */
if (ELFW(ST_BIND)(info) != STB_LOCAL) {
/* add another hashing entry */
nbuckets = base[0];
h = elf_hash((unsigned char *)s->link->data + name_offset) % nbuckets;
*ptr = base[2 + h];
base[2 + h] = sym_index;
base[1]++;
/* we resize the hash table */
hs->nb_hashed_syms++;
if (hs->nb_hashed_syms > 2 * nbuckets) {
rebuild_hash(s, 2 * nbuckets);
}
} else {
*ptr = 0;
base[1]++;
}
}
return sym_index;
}
ST_FUNC int find_elf_sym(Section *s, const char *name)
{
ElfW(Sym) *sym;
Section *hs;
int nbuckets, sym_index, h;
const char *name1;
hs = s->hash;
if (!hs)
return 0;
nbuckets = ((int *)hs->data)[0];
h = elf_hash((unsigned char *) name) % nbuckets;
sym_index = ((int *)hs->data)[2 + h];
while (sym_index != 0) {
sym = &((ElfW(Sym) *)s->data)[sym_index];
name1 = (char *) s->link->data + sym->st_name;
if (!strcmp(name, name1))
return sym_index;
sym_index = ((int *)hs->data)[2 + nbuckets + sym_index];
}
return 0;
}
/* return elf symbol value, signal error if 'err' is nonzero, decorate
name if FORC */
ST_FUNC addr_t get_sym_addr(TCCState *s1, const char *name, int err, int forc)
{
int sym_index;
ElfW(Sym) *sym;
char buf[256];
if (forc && s1->leading_underscore
#ifdef TCC_TARGET_PE
/* win32-32bit stdcall symbols always have _ already */
&& !strchr(name, '@')
#endif
) {
buf[0] = '_';
pstrcpy(buf + 1, sizeof(buf) - 1, name);
name = buf;
}
sym_index = find_elf_sym(s1->symtab, name);
sym = &((ElfW(Sym) *)s1->symtab->data)[sym_index];
if (!sym_index || sym->st_shndx == SHN_UNDEF) {
if (err)
tcc_error_noabort("%s not defined", name);
return (addr_t)-1;
}
return sym->st_value;
}
/* return elf symbol value */
LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name)
{
addr_t addr = get_sym_addr(s, name, 0, 1);
return addr == -1 ? NULL : (void*)(uintptr_t)addr;
}
/* list elf symbol names and values */
ST_FUNC void list_elf_symbols(TCCState *s, void *ctx,
void (*symbol_cb)(void *ctx, const char *name, const void *val))
{
ElfW(Sym) *sym;
Section *symtab;
int sym_index, end_sym;
const char *name;
unsigned char sym_vis, sym_bind;
symtab = s->symtab;
end_sym = symtab->data_offset / sizeof (ElfSym);
for (sym_index = 0; sym_index < end_sym; ++sym_index) {
sym = &((ElfW(Sym) *)symtab->data)[sym_index];
if (sym->st_value) {
name = (char *) symtab->link->data + sym->st_name;
sym_bind = ELFW(ST_BIND)(sym->st_info);
sym_vis = ELFW(ST_VISIBILITY)(sym->st_other);
if (sym_bind == STB_GLOBAL && sym_vis == STV_DEFAULT)
symbol_cb(ctx, name, (void*)(uintptr_t)sym->st_value);
}
}
}
/* list elf symbol names and values */
LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx,
void (*symbol_cb)(void *ctx, const char *name, const void *val))
{
list_elf_symbols(s, ctx, symbol_cb);
}
#ifndef ELF_OBJ_ONLY
static void
version_add (TCCState *s1)
{
int i;
ElfW(Sym) *sym;
ElfW(Verneed) *vn = NULL;
Section *symtab;
int sym_index, end_sym, nb_versions = 2, nb_entries = 0;
ElfW(Half) *versym;
const char *name;
if (0 == nb_sym_versions)
return;
versym_section = new_section(s1, ".gnu.version", SHT_GNU_versym, SHF_ALLOC);
versym_section->sh_entsize = sizeof(ElfW(Half));
versym_section->link = s1->dynsym;
/* add needed symbols */
symtab = s1->dynsym;
end_sym = symtab->data_offset / sizeof (ElfSym);
versym = section_ptr_add(versym_section, end_sym * sizeof(ElfW(Half)));
for (sym_index = 1; sym_index < end_sym; ++sym_index) {
int dllindex, verndx;
sym = &((ElfW(Sym) *)symtab->data)[sym_index];
if (sym->st_shndx != SHN_UNDEF)
continue; /* defined symbol doesn't need library version */
name = (char *) symtab->link->data + sym->st_name;
dllindex = find_elf_sym(s1->dynsymtab_section, name);
verndx = (dllindex && dllindex < nb_sym_to_version)
? sym_to_version[dllindex] : -1;
if (verndx >= 0) {
if (!sym_versions[verndx].out_index)
sym_versions[verndx].out_index = nb_versions++;
versym[sym_index] = sym_versions[verndx].out_index;
}
}
/* generate verneed section, but not when it will be empty. Some
dynamic linkers look at their contents even when DTVERNEEDNUM and
section size is zero. */
if (nb_versions > 2) {
verneed_section = new_section(s1, ".gnu.version_r",
SHT_GNU_verneed, SHF_ALLOC);
verneed_section->link = s1->dynsym->link;
for (i = nb_sym_versions; i-- > 0;) {
struct sym_version *sv = &sym_versions[i];
int n_same_libs = 0, prev;
size_t vnofs;
ElfW(Vernaux) *vna = 0;
if (sv->out_index < 1)
continue;
/* make sure that a DT_NEEDED tag is put */
/* abitest-tcc fails on older i386-linux with "ld-linux.so.2" DT_NEEDED
ret_int_test... Inconsistency detected by ld.so: dl-minimal.c: 148:
realloc: Assertion `ptr == alloc_last_block' failed! */
if (strcmp(sv->lib, "ld-linux.so.2"))
tcc_add_dllref(s1, sv->lib, 0);
vnofs = section_add(verneed_section, sizeof(*vn), 1);
vn = (ElfW(Verneed)*)(verneed_section->data + vnofs);
vn->vn_version = 1;
vn->vn_file = put_elf_str(verneed_section->link, sv->lib);
vn->vn_aux = sizeof (*vn);
do {
prev = sv->prev_same_lib;
if (sv->out_index > 0) {
vna = section_ptr_add(verneed_section, sizeof(*vna));
vna->vna_hash = elf_hash ((const unsigned char *)sv->version);
vna->vna_flags = 0;
vna->vna_other = sv->out_index;
sv->out_index = -2;
vna->vna_name = put_elf_str(verneed_section->link, sv->version);
vna->vna_next = sizeof (*vna);
n_same_libs++;
}
if (prev >= 0)
sv = &sym_versions[prev];
} while(prev >= 0);
vna->vna_next = 0;
vn = (ElfW(Verneed)*)(verneed_section->data + vnofs);
vn->vn_cnt = n_same_libs;
vn->vn_next = sizeof(*vn) + n_same_libs * sizeof(*vna);
nb_entries++;
}
if (vn)
vn->vn_next = 0;
verneed_section->sh_info = nb_entries;
}
dt_verneednum = nb_entries;
}
#endif /* ndef ELF_OBJ_ONLY */
/* add an elf symbol : check if it is already defined and patch
it. Return symbol index. NOTE that sh_num can be SHN_UNDEF. */
ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size,
int info, int other, int shndx, const char *name)
{
TCCState *s1 = s->s1;
ElfW(Sym) *esym;
int sym_bind, sym_index, sym_type, esym_bind;
unsigned char sym_vis, esym_vis, new_vis;
sym_bind = ELFW(ST_BIND)(info);
sym_type = ELFW(ST_TYPE)(info);
sym_vis = ELFW(ST_VISIBILITY)(other);
if (sym_bind != STB_LOCAL) {
/* we search global or weak symbols */
sym_index = find_elf_sym(s, name);
if (!sym_index)
goto do_def;
esym = &((ElfW(Sym) *)s->data)[sym_index];
if (esym->st_value == value && esym->st_size == size && esym->st_info == info
&& esym->st_other == other && esym->st_shndx == shndx)
return sym_index;
if (esym->st_shndx != SHN_UNDEF) {
esym_bind = ELFW(ST_BIND)(esym->st_info);
/* propagate the most constraining visibility */
/* STV_DEFAULT(0)<STV_PROTECTED(3)<STV_HIDDEN(2)<STV_INTERNAL(1) */
esym_vis = ELFW(ST_VISIBILITY)(esym->st_other);
if (esym_vis == STV_DEFAULT) {
new_vis = sym_vis;
} else if (sym_vis == STV_DEFAULT) {
new_vis = esym_vis;
} else {
new_vis = (esym_vis < sym_vis) ? esym_vis : sym_vis;
}
esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
| new_vis;
if (shndx == SHN_UNDEF) {
/* ignore adding of undefined symbol if the
corresponding symbol is already defined */
} else if (sym_bind == STB_GLOBAL && esym_bind == STB_WEAK) {
/* global overrides weak, so patch */
goto do_patch;
} else if (sym_bind == STB_WEAK && esym_bind == STB_GLOBAL) {
/* weak is ignored if already global */
} else if (sym_bind == STB_WEAK && esym_bind == STB_WEAK) {
/* keep first-found weak definition, ignore subsequents */
} else if (sym_vis == STV_HIDDEN || sym_vis == STV_INTERNAL) {
/* ignore hidden symbols after */
} else if ((esym->st_shndx == SHN_COMMON
|| esym->st_shndx == bss_section->sh_num)
&& (shndx < SHN_LORESERVE
&& shndx != bss_section->sh_num)) {
/* data symbol gets precedence over common/bss */
goto do_patch;
} else if (shndx == SHN_COMMON || shndx == bss_section->sh_num) {
/* data symbol keeps precedence over common/bss */
} else if (s->sh_flags & SHF_DYNSYM) {
/* we accept that two DLL define the same symbol */
} else if (esym->st_other & ST_ASM_SET) {
/* If the existing symbol came from an asm .set
we can override. */
goto do_patch;
} else {
#if 0
printf("new_bind=%x new_shndx=%x new_vis=%x old_bind=%x old_shndx=%x old_vis=%x\n",
sym_bind, shndx, new_vis, esym_bind, esym->st_shndx, esym_vis);
#endif
tcc_error_noabort("'%s' defined twice", name);
}
} else {
esym->st_other = other;
do_patch:
esym->st_info = ELFW(ST_INFO)(sym_bind, sym_type);
esym->st_shndx = shndx;
s1->new_undef_sym = 1;
esym->st_value = value;
esym->st_size = size;
}
} else {
do_def:
sym_index = put_elf_sym(s, value, size,
ELFW(ST_INFO)(sym_bind, sym_type), other,
shndx, name);
}
return sym_index;
}
/* put relocation */
ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset,
int type, int symbol, addr_t addend)
{
TCCState *s1 = s->s1;
char buf[256];
Section *sr;
ElfW_Rel *rel;
sr = s->reloc;
if (!sr) {
/* if no relocation section, create it */
snprintf(buf, sizeof(buf), REL_SECTION_FMT, s->name);
/* if the symtab is allocated, then we consider the relocation
are also */
sr = new_section(s->s1, buf, SHT_RELX, symtab->sh_flags);
sr->sh_entsize = sizeof(ElfW_Rel);
sr->link = symtab;
sr->sh_info = s->sh_num;
s->reloc = sr;
}
rel = section_ptr_add(sr, sizeof(ElfW_Rel));
rel->r_offset = offset;
rel->r_info = ELFW(R_INFO)(symbol, type);
#if SHT_RELX == SHT_RELA
rel->r_addend = addend;
#endif
if (SHT_RELX != SHT_RELA && addend)
tcc_error_noabort("non-zero addend on REL architecture");
}
ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
int type, int symbol)
{
put_elf_reloca(symtab, s, offset, type, symbol, 0);
}
ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc)
{
int n;
struct sym_attr *tab;
if (index >= s1->nb_sym_attrs) {
if (!alloc)
return s1->sym_attrs;
/* find immediately bigger power of 2 and reallocate array */
n = 1;
while (index >= n)
n *= 2;
tab = tcc_realloc(s1->sym_attrs, n * sizeof(*s1->sym_attrs));
s1->sym_attrs = tab;
memset(s1->sym_attrs + s1->nb_sym_attrs, 0,
(n - s1->nb_sym_attrs) * sizeof(*s1->sym_attrs));
s1->nb_sym_attrs = n;
}
return &s1->sym_attrs[index];
}
static void modify_reloctions_old_to_new(TCCState *s1, Section *s, int *old_to_new_syms)
{
int i, type, sym_index;
Section *sr;
ElfW_Rel *rel;
for(i = 1; i < s1->nb_sections; i++) {
sr = s1->sections[i];
if (sr->sh_type == SHT_RELX && sr->link == s) {
for_each_elem(sr, 0, rel, ElfW_Rel) {
sym_index = ELFW(R_SYM)(rel->r_info);
type = ELFW(R_TYPE)(rel->r_info);
sym_index = old_to_new_syms[sym_index];
rel->r_info = ELFW(R_INFO)(sym_index, type);
}
}
}
}
/* In an ELF file symbol table, the local symbols must appear below
the global and weak ones. Since TCC cannot sort it while generating
the code, we must do it after. All the relocation tables are also
modified to take into account the symbol table sorting */
static void sort_syms(TCCState *s1, Section *s)
{
int *old_to_new_syms;
ElfW(Sym) *new_syms;
int nb_syms, i;
ElfW(Sym) *p, *q;
nb_syms = s->data_offset / sizeof(ElfW(Sym));
new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
/* first pass for local symbols */
p = (ElfW(Sym) *)s->data;
q = new_syms;
for(i = 0; i < nb_syms; i++) {
if (ELFW(ST_BIND)(p->st_info) == STB_LOCAL) {
old_to_new_syms[i] = q - new_syms;
*q++ = *p;
}
p++;
}
/* save the number of local symbols in section header */
if( s->sh_size ) /* this 'if' makes IDA happy */
s->sh_info = q - new_syms;
/* then second pass for non local symbols */
p = (ElfW(Sym) *)s->data;
for(i = 0; i < nb_syms; i++) {
if (ELFW(ST_BIND)(p->st_info) != STB_LOCAL) {
old_to_new_syms[i] = q - new_syms;
*q++ = *p;
}
p++;
}
/* we copy the new symbols to the old */
memcpy(s->data, new_syms, nb_syms * sizeof(ElfW(Sym)));
tcc_free(new_syms);
modify_reloctions_old_to_new(s1, s, old_to_new_syms);
tcc_free(old_to_new_syms);
}
#ifndef ELF_OBJ_ONLY
/* See: https://flapenguin.me/elf-dt-gnu-hash */
#define ELFCLASS_BITS (PTR_SIZE * 8)
static Section *create_gnu_hash(TCCState *s1)
{
int nb_syms, i, ndef, nbuckets, symoffset, bloom_size, bloom_shift;
ElfW(Sym) *p;
Section *gnu_hash;
Section *dynsym = s1->dynsym;
Elf32_Word *ptr;
gnu_hash = new_section(s1, ".gnu.hash", SHT_GNU_HASH, SHF_ALLOC);
gnu_hash->link = dynsym->hash->link;
nb_syms = dynsym->data_offset / sizeof(ElfW(Sym));
/* count def symbols */
ndef = 0;
p = (ElfW(Sym) *)dynsym->data;
for(i = 0; i < nb_syms; i++, p++)
ndef += p->st_shndx != SHN_UNDEF;
/* calculate gnu hash sizes and fill header */
nbuckets = ndef / 4 + 1;
symoffset = nb_syms - ndef;
bloom_shift = PTR_SIZE == 8 ? 6 : 5;
bloom_size = 1; /* must be power of two */
while (ndef >= bloom_size * (1 << (bloom_shift - 3)))
bloom_size *= 2;
ptr = section_ptr_add(gnu_hash, 4 * 4 +
PTR_SIZE * bloom_size +
nbuckets * 4 +
ndef * 4);
ptr[0] = nbuckets;
ptr[1] = symoffset;
ptr[2] = bloom_size;
ptr[3] = bloom_shift;
return gnu_hash;
}
static Elf32_Word elf_gnu_hash (const unsigned char *name)
{
Elf32_Word h = 5381;
unsigned char c;
while ((c = *name++))
h = h * 33 + c;
return h;
}
static void update_gnu_hash(TCCState *s1, Section *gnu_hash)
{
int *old_to_new_syms;
ElfW(Sym) *new_syms;
int nb_syms, i, nbuckets, bloom_size, bloom_shift;
ElfW(Sym) *p, *q;
Section *vs;
Section *dynsym = s1->dynsym;
Elf32_Word *ptr, *buckets, *chain, *hash;
unsigned int *nextbuck;
addr_t *bloom;
unsigned char *strtab;
struct { int first, last; } *buck;
strtab = dynsym->link->data;
nb_syms = dynsym->data_offset / sizeof(ElfW(Sym));
new_syms = tcc_malloc(nb_syms * sizeof(ElfW(Sym)));
old_to_new_syms = tcc_malloc(nb_syms * sizeof(int));
hash = tcc_malloc(nb_syms * sizeof(Elf32_Word));
nextbuck = tcc_malloc(nb_syms * sizeof(int));
/* calculate hashes and copy undefs */
p = (ElfW(Sym) *)dynsym->data;
q = new_syms;
for(i = 0; i < nb_syms; i++, p++) {
if (p->st_shndx == SHN_UNDEF) {
old_to_new_syms[i] = q - new_syms;
*q++ = *p;
}
else
hash[i] = elf_gnu_hash(strtab + p->st_name);
}
ptr = (Elf32_Word *) gnu_hash->data;
nbuckets = ptr[0];
bloom_size = ptr[2];
bloom_shift = ptr[3];
bloom = (addr_t *) (void *) &ptr[4];
buckets = (Elf32_Word*) (void *) &bloom[bloom_size];
chain = &buckets[nbuckets];
buck = tcc_malloc(nbuckets * sizeof(*buck));
if (gnu_hash->data_offset != 4 * 4 +
PTR_SIZE * bloom_size +
nbuckets * 4 +
(nb_syms - (q - new_syms)) * 4)
tcc_error_noabort ("gnu_hash size incorrect");
/* find buckets */
for(i = 0; i < nbuckets; i++)
buck[i].first = -1;
p = (ElfW(Sym) *)dynsym->data;
for(i = 0; i < nb_syms; i++, p++)
if (p->st_shndx != SHN_UNDEF) {
int bucket = hash[i] % nbuckets;
if (buck[bucket].first == -1)
buck[bucket].first = buck[bucket].last = i;
else {
nextbuck[buck[bucket].last] = i;
buck[bucket].last = i;
}
}
/* fill buckets/chains/bloom and sort symbols */
p = (ElfW(Sym) *)dynsym->data;
for(i = 0; i < nbuckets; i++) {
int cur = buck[i].first;
if (cur != -1) {
buckets[i] = q - new_syms;
for (;;) {
old_to_new_syms[cur] = q - new_syms;
*q++ = p[cur];
*chain++ = hash[cur] & ~1;
bloom[(hash[cur] / ELFCLASS_BITS) % bloom_size] |=
(addr_t)1 << (hash[cur] % ELFCLASS_BITS) |
(addr_t)1 << ((hash[cur] >> bloom_shift) % ELFCLASS_BITS);