/
elfobj.c
3786 lines (3382 loc) · 118 KB
/
elfobj.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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) ?-1998 by Symantec
* Copyright (c) 2000-2017 by Digital Mars, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/ddmd/backend/elfobj.c, backend/elfobj.c)
*/
/****
* Output to ELF object files
* http://www.sco.com/developers/gabi/2003-12-17/ch4.sheader.html
*/
#if SCPP || MARS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if __sun
#include <alloca.h>
#endif
#include "cc.h"
#include "global.h"
#include "code.h"
#include "type.h"
#include "melf.h"
#include "outbuf.h"
#include "filespec.h"
#include "cv4.h"
#include "cgcv.h"
#include "dt.h"
#include "aa.h"
#include "tinfo.h"
#if ELFOBJ
#include "dwarf.h"
#include "aa.h"
#include "tinfo.h"
#ifndef ELFOSABI
# if TARGET_LINUX
# define ELFOSABI ELFOSABI_LINUX
# elif TARGET_FREEBSD
# define ELFOSABI ELFOSABI_FREEBSD
# elif TARGET_SOLARIS
# define ELFOSABI ELFOSABI_SYSV
# elif TARGET_OPENBSD
# define ELFOSABI ELFOSABI_OPENBSD
# else
# error "No ELF OS ABI defined. Please fix"
# endif
#endif
//#define DEBSYM 0x7E
static Outbuffer *fobjbuf;
static char __file__[] = __FILE__; // for tassert.h
#include "tassert.h"
#define MATCH_SECTION 1
#define DEST_LEN (IDMAX + IDOHD + 1)
char *obj_mangle2(Symbol *s,char *dest, size_t *destlen);
#if MARS
// C++ name mangling is handled by front end
#define cpp_mangle(s) ((s)->Sident)
#endif
void addSegmentToComdat(segidx_t seg, segidx_t comdatseg);
/**
* If set the compiler requires full druntime support of the new
* section registration.
*/
#define REQUIRE_DSO_REGISTRY (DMDV2 && (TARGET_LINUX || TARGET_FREEBSD))
/******
* FreeBSD uses ELF, but the linker crashes with Elf comdats with the following message:
* /usr/bin/ld: BFD 2.15 [FreeBSD] 2004-05-23 internal error, aborting at
* /usr/src/gnu/usr.bin/binutils/libbfd/../../../../contrib/binutils/bfd/elfcode.h
* line 213 in bfd_elf32_swap_symbol_out
* For the time being, just stick with Linux.
*/
#define ELF_COMDAT TARGET_LINUX
/***************************************************
* Correspondence of relocation types
* 386 32 bit in 64 64 in 64
* R_386_32 R_X86_64_32 R_X86_64_64
* R_386_GOTOFF R_X86_64_PC32 R_X86_64_
* R_386_GOTPC R_X86_64_ R_X86_64_
* R_386_GOT32 R_X86_64_ R_X86_64_
* R_386_TLS_GD R_X86_64_TLSGD R_X86_64_
* R_386_TLS_IE R_X86_64_GOTTPOFF R_X86_64_
* R_386_TLS_LE R_X86_64_TPOFF32 R_X86_64_
* R_386_PLT32 R_X86_64_PLT32 R_X86_64_
* R_386_PC32 R_X86_64_PC32 R_X86_64_
*/
/******************************************
*/
symbol *GOTsym; // global offset table reference
symbol *Obj::getGOTsym()
{
if (!GOTsym)
{
GOTsym = symbol_name("_GLOBAL_OFFSET_TABLE_",SCglobal,tspvoid);
}
return GOTsym;
}
void Obj::refGOTsym()
{
if (!GOTsym)
{
symbol *s = Obj::getGOTsym();
Obj::external(s);
}
}
static void objfile_write(FILE *fd, void *buffer, unsigned len);
STATIC char * objmodtoseg (const char *modname);
STATIC void objfixupp (struct FIXUP *);
STATIC void ledata_new (int seg,targ_size_t offset);
STATIC void obj_tlssections();
#if MARS
static void obj_rtinit();
#endif
static void addSectionToComdat(IDXSEC secidx, segidx_t comdatseg);
static IDXSYM elf_addsym(IDXSTR sym, targ_size_t val, unsigned sz,
unsigned typ,unsigned bind,IDXSEC sec,
unsigned char visibility=STV_DEFAULT);
static long elf_align(targ_size_t size, long offset);
// The object file is built is several separate pieces
// Non-repeatable section types have single output buffers
// Pre-allocated buffers are defined for:
// Section Names string table
// Section Headers table
// Symbol table
// String table
// Notes section
// Comment data
// Section Names - String table for section names only
static Outbuffer *section_names;
#define SEC_NAMES_INIT 800
#define SEC_NAMES_INC 400
// Hash table for section_names
AArray *section_names_hashtable;
int jmpseg;
/* ====================== Cached Strings in section_names ================= */
struct TypeInfo_Idxstr : TypeInfo
{
const char* toString();
hash_t getHash(void *p);
int equals(void *p1, void *p2);
int compare(void *p1, void *p2);
size_t tsize();
void swap(void *p1, void *p2);
};
TypeInfo_Idxstr ti_idxstr;
const char* TypeInfo_Idxstr::toString()
{
return "IDXSTR";
}
hash_t TypeInfo_Idxstr::getHash(void *p)
{
IDXSTR a = *(IDXSTR *)p;
hash_t hash = 0;
for (const char *s = (char *)(section_names->buf + a);
*s;
s++)
{
hash = hash * 11 + *s;
}
return hash;
}
int TypeInfo_Idxstr::equals(void *p1, void *p2)
{
IDXSTR a1 = *(IDXSTR*)p1;
IDXSTR a2 = *(IDXSTR*)p2;
const char *s1 = (char *)(section_names->buf + a1);
const char *s2 = (char *)(section_names->buf + a2);
return strcmp(s1, s2) == 0;
}
int TypeInfo_Idxstr::compare(void *p1, void *p2)
{
IDXSTR a1 = *(IDXSTR*)p1;
IDXSTR a2 = *(IDXSTR*)p2;
const char *s1 = (char *)(section_names->buf + a1);
const char *s2 = (char *)(section_names->buf + a2);
return strcmp(s1, s2);
}
size_t TypeInfo_Idxstr::tsize()
{
return sizeof(IDXSTR);
}
void TypeInfo_Idxstr::swap(void *p1, void *p2)
{
assert(0);
}
/* ======================================================================== */
// String Table - String table for all other names
static Outbuffer *symtab_strings;
// Section Headers
Outbuffer *SECbuf; // Buffer to build section table in
#define SecHdrTab ((Elf32_Shdr *)SECbuf->buf)
#define GET_SECTION(secidx) (SecHdrTab + secidx)
#define GET_SECTION_NAME(secidx) (section_names->buf + SecHdrTab[secidx].sh_name)
// The relocation for text and data seems to get lost.
// Try matching the order gcc output them
// This means defining the sections and then removing them if they are
// not used.
static int section_cnt; // Number of sections in table
#define SHN_TEXT 1
#define SHN_RELTEXT 2
#define SHN_DATA 3
#define SHN_RELDATA 4
#define SHN_BSS 5
#define SHN_RODAT 6
#define SHN_STRINGS 7
#define SHN_SYMTAB 8
#define SHN_SECNAMES 9
#define SHN_COM 10
#define SHN_NOTE 11
#define SHN_GNUSTACK 12
#define SHN_CDATAREL 13
IDXSYM *mapsec2sym;
#define S2S_INC 20
#define SymbolTable ((Elf32_Sym *)SYMbuf->buf)
#define SymbolTable64 ((Elf64_Sym *)SYMbuf->buf)
static int symbol_idx; // Number of symbols in symbol table
static int local_cnt; // Number of symbols with STB_LOCAL
#define STI_FILE 1 // Where file symbol table entry is
#define STI_TEXT 2
#define STI_DATA 3
#define STI_BSS 4
#define STI_GCC 5 // Where "gcc2_compiled" symbol is */
#define STI_RODAT 6 // Symbol for readonly data
#define STI_NOTE 7 // Where note symbol table entry is
#define STI_COM 8
#define STI_CDATAREL 9 // Symbol for readonly data with relocations
// NOTE: There seems to be a requirement that the read-only data have the
// same symbol table index and section index. Use section NOTE as a place
// holder. When a read-only string section is required, swap to NOTE.
// Symbol Table
Outbuffer *SYMbuf; // Buffer to build symbol table in
static Outbuffer *reset_symbuf; // Keep pointers to reset symbols
// Extended section header indices
static Outbuffer *shndx_data;
static const IDXSEC secidx_shndx = SHN_HIRESERVE + 1;
// Notes data (note currently used)
static Outbuffer *note_data;
static IDXSEC secidx_note; // Final table index for note data
// Comment data for compiler version
static Outbuffer *comment_data;
static const char compiler[] = "\0Digital Mars C/C++"
VERSION
; // compiled by ...
// Each compiler segment is an elf section
// Predefined compiler segments CODE,DATA,CDATA,UDATA map to indexes
// into SegData[]
// An additionl index is reserved for comment data
// New compiler segments are added to end.
//
// There doesn't seem to be any way to get reserved data space in the
// same section as initialized data or code, so section offsets should
// be continuous when adding data. Fix-ups anywhere withing existing data.
#define COMD CDATAREL+1
#define OB_SEG_SIZ 10 // initial number of segments supported
#define OB_SEG_INC 10 // increment for additional segments
#define OB_CODE_STR 100000 // initial size for code
#define OB_CODE_INC 100000 // increment for additional code
#define OB_DATA_STR 100000 // initial size for data
#define OB_DATA_INC 100000 // increment for additional data
#define OB_CDATA_STR 1024 // initial size for data
#define OB_CDATA_INC 1024 // increment for additional data
#define OB_COMD_STR 256 // initial size for comments
// increment as needed
#define OB_XTRA_STR 250 // initial size for extra segments
#define OB_XTRA_INC 10000 // increment size
#define MAP_SEG2SECIDX(seg) (SegData[seg]->SDshtidx)
#define MAP_SEG2SYMIDX(seg) (SegData[seg]->SDsymidx)
#define MAP_SEG2SEC(seg) (&SecHdrTab[MAP_SEG2SECIDX(seg)])
#define MAP_SEG2TYP(seg) (MAP_SEG2SEC(seg)->sh_flags & SHF_EXECINSTR ? CODE : DATA)
seg_data **SegData;
int seg_count;
int seg_max;
int seg_tlsseg = UNKNOWN;
int seg_tlsseg_bss = UNKNOWN;
static int elf_addsegment2(IDXSEC shtidx, IDXSYM symidx, IDXSEC relidx);
static int elf_addsegment(IDXSTR namidx, int type, int flags, int align);
static int elf_getsegment(IDXSTR namidx);
/*******************************
* Output a string into a string table
* Input:
* strtab = string table for entry
* str = string to add
*
* Returns index into the specified string table.
*/
IDXSTR Obj::addstr(Outbuffer *strtab, const char *str)
{
//dbg_printf("Obj::addstr(strtab = x%x str = '%s')\n",strtab,str);
IDXSTR idx = strtab->size(); // remember starting offset
strtab->writeString(str);
//dbg_printf("\tidx %d, new size %d\n",idx,strtab->size());
return idx;
}
/*******************************
* Find a string in a string table
* Input:
* strtab = string table for entry
* str = string to find
*
* Returns index into the specified string table or 0.
*/
static IDXSTR elf_findstr(Outbuffer *strtab, const char *str, const char *suffix)
{
//printf("elf_findstr(strtab = %p, str = %s, suffix = %s\n", strtab, str ? str : "", suffix ? suffix : "");
size_t len = strlen(str);
// Combine str~suffix and have buf point to the combination
#ifdef DEBUG
char tmpbuf[25]; // to exercise the alloca() code path
#else
char tmpbuf[1024]; // the alloca() code path is slow
#endif
const char *buf;
if (suffix)
{
size_t suffixlen = strlen(suffix);
if (len + suffixlen >= sizeof(tmpbuf))
{
buf = (char *)alloca(len + suffixlen + 1);
assert(buf);
}
else
{
buf = tmpbuf;
}
memcpy((char *)buf, str, len);
memcpy((char *)buf + len, suffix, suffixlen + 1);
len += suffixlen;
}
else
buf = str;
// Linear search, slow
const char *ent = (char *)strtab->buf+1;
const char *pend = ent+strtab->size() - 1;
while (ent + len < pend)
{
if (memcmp(buf, ent, len + 1) == 0)
return ent - (const char *)strtab->buf;
ent = (const char *)memchr(ent, 0, pend - ent);
ent += 1;
}
return 0; // never found match
}
/*******************************
* Output a mangled string into the symbol string table
* Input:
* str = string to add
*
* Returns index into the table.
*/
static IDXSTR elf_addmangled(Symbol *s)
{
//printf("elf_addmangled(%s)\n", s->Sident);
char dest[DEST_LEN];
IDXSTR namidx = symtab_strings->size();
size_t len;
char *destr = obj_mangle2(s, dest, &len);
const char *name = destr;
if (CPP && name[0] == '_' && name[1] == '_')
{
if (strncmp(name,"__ct__",6) == 0)
{
name += 4;
len -= 4;
}
#if 0
switch(name[2])
{
case 'c':
if (strncmp(name,"__ct__",6) == 0)
name += 4;
break;
case 'd':
if (strcmp(name,"__dl__FvP") == 0)
name = "__builtin_delete";
break;
case 'v':
//if (strcmp(name,"__vec_delete__FvPiUIPi") == 0)
//name = "__builtin_vec_del";
//else
//if (strcmp(name,"__vn__FPUI") == 0)
//name = "__builtin_vec_new";
break;
case 'n':
if (strcmp(name,"__nw__FPUI") == 0)
name = "__builtin_new";
break;
}
#endif
}
else if (tyfunc(s->ty()) && s->Sfunc && s->Sfunc->Fredirect)
{
name = s->Sfunc->Fredirect;
len = strlen(name);
}
symtab_strings->reserve(len+1);
memcpy((char *)symtab_strings->p, name, len + 1);
symtab_strings->setsize(namidx+len+1);
if (destr != dest) // if we resized result
mem_free(destr);
//dbg_printf("\telf_addmagled symtab_strings %s namidx %d len %d size %d\n",name, namidx,len,symtab_strings->size());
return namidx;
}
/*******************************
* Output a symbol into the symbol table
* Input:
* stridx = string table index for name
* val = value associated with symbol
* sz = symbol size
* typ = symbol type
* bind = symbol binding
* sec = index of section where symbol is defined
* visibility = visibility of symbol (STV_xxxx)
*
* Returns the symbol table index for the symbol
*/
static IDXSYM elf_addsym(IDXSTR nam, targ_size_t val, unsigned sz,
unsigned typ, unsigned bind, IDXSEC sec,
unsigned char visibility /*= STV_DEFAULT*/)
{
//dbg_printf("elf_addsym(nam %d, val %d, sz %x, typ %x, bind %x, sec %d\n",
//nam,val,sz,typ,bind,sec);
/* We want globally defined data symbols to have a size because
* zero sized symbols break copy relocations for shared libraries.
*/
if(sz == 0 && (bind == STB_GLOBAL || bind == STB_WEAK) &&
(typ == STT_OBJECT || typ == STT_TLS) &&
sec != SHN_UNDEF)
sz = 1; // so fake it if it doesn't
if (sec > SHN_HIRESERVE)
{ // If the section index is too big we need to store it as
// extended section header index.
if (!shndx_data)
shndx_data = new Outbuffer(50 * sizeof(Elf64_Word));
// fill with zeros up to symbol_idx
const size_t shndx_idx = shndx_data->size() / sizeof(Elf64_Word);
shndx_data->writezeros((symbol_idx - shndx_idx) * sizeof(Elf64_Word));
shndx_data->write32(sec);
sec = SHN_XINDEX;
}
if (I64)
{
if (!SYMbuf)
{ SYMbuf = new Outbuffer(50 * sizeof(Elf64_Sym));
SYMbuf->reserve(100 * sizeof(Elf64_Sym));
}
Elf64_Sym sym;
sym.st_name = nam;
sym.st_value = val;
sym.st_size = sz;
sym.st_info = ELF64_ST_INFO(bind,typ);
sym.st_other = visibility;
sym.st_shndx = sec;
SYMbuf->write(&sym,sizeof(sym));
}
else
{
if (!SYMbuf)
{ SYMbuf = new Outbuffer(50 * sizeof(Elf32_Sym));
SYMbuf->reserve(100 * sizeof(Elf32_Sym));
}
Elf32_Sym sym;
sym.st_name = nam;
sym.st_value = val;
sym.st_size = sz;
sym.st_info = ELF32_ST_INFO(bind,typ);
sym.st_other = visibility;
sym.st_shndx = sec;
SYMbuf->write(&sym,sizeof(sym));
}
if (bind == STB_LOCAL)
local_cnt++;
//dbg_printf("\treturning symbol table index %d\n",symbol_idx);
return symbol_idx++;
}
/*******************************
* Create a new section header table entry.
*
* Input:
* name = section name
* suffix = suffix for name or NULL
* type = type of data in section sh_type
* flags = attribute flags sh_flags
* Output:
* section_cnt = assigned number for this section
* Note: Sections will be reordered on output
*/
static IDXSEC elf_newsection2(
Elf32_Word name,
Elf32_Word type,
Elf32_Word flags,
Elf32_Addr addr,
Elf32_Off offset,
Elf32_Word size,
Elf32_Word link,
Elf32_Word info,
Elf32_Word addralign,
Elf32_Word entsize)
{
Elf32_Shdr sec;
sec.sh_name = name;
sec.sh_type = type;
sec.sh_flags = flags;
sec.sh_addr = addr;
sec.sh_offset = offset;
sec.sh_size = size;
sec.sh_link = link;
sec.sh_info = info;
sec.sh_addralign = addralign;
sec.sh_entsize = entsize;
if (!SECbuf)
{ SECbuf = new Outbuffer(4 * sizeof(Elf32_Shdr));
SECbuf->reserve(16 * sizeof(Elf32_Shdr));
}
if (section_cnt == SHN_LORESERVE)
{ // insert dummy null sections to skip reserved section indices
section_cnt = SHN_HIRESERVE + 1;
SECbuf->writezeros((SHN_HIRESERVE + 1 - SHN_LORESERVE) * sizeof(sec));
// shndx itself becomes the first section with an extended index
IDXSTR namidx = Obj::addstr(section_names, ".symtab_shndx");
elf_newsection2(namidx,SHT_SYMTAB_SHNDX,0,0,0,0,SHN_SYMTAB,0,4,4);
}
SECbuf->write((void *)&sec, sizeof(sec));
return section_cnt++;
}
/**
Add a new section name or get the string table index of an existing entry.
Params:
name = name of section
suffix = append to name
padded = set to true when entry was newly added
Returns:
String index of new or existing section name.
*/
static IDXSTR elf_addsectionname(const char *name, const char *suffix = NULL, bool *padded = NULL)
{
IDXSTR namidx = section_names->size();
section_names->writeString(name);
if (suffix)
{ // Append suffix string
section_names->setsize(section_names->size() - 1); // back up over terminating 0
section_names->writeString(suffix);
}
IDXSTR *pidx = (IDXSTR *)section_names_hashtable->get(&namidx);
if (*pidx)
{
// this section name already exists, remove addition
section_names->setsize(namidx);
return *pidx;
}
if (padded)
*padded = true;
return *pidx = namidx;
}
static IDXSEC elf_newsection(const char *name, const char *suffix,
Elf32_Word type, Elf32_Word flags)
{
// dbg_printf("elf_newsection(%s,%s,type %d, flags x%x)\n",
// name?name:"",suffix?suffix:"",type,flags);
bool added = false;
IDXSTR namidx = elf_addsectionname(name, suffix, &added);
assert(added);
return elf_newsection2(namidx,type,flags,0,0,0,0,0,0,0);
}
/**************************
* Ouput read only data and generate a symbol for it.
*
*/
symbol *Obj::sym_cdata(tym_t ty,char *p,int len)
{
symbol *s;
#if 0
if (OPT_IS_SET(OPTfwritable_strings))
{
alignOffset(DATA, tysize(ty));
s = symboldata(Offset(DATA), ty);
SegData[DATA]->SDbuf->write(p,len);
s->Sseg = DATA;
s->Soffset = Offset(DATA); // Remember its offset into DATA section
Offset(DATA) += len;
}
else
#endif
{
//printf("Obj::sym_cdata(ty = %x, p = %x, len = %d, Offset(CDATA) = %x)\n", ty, p, len, Offset(CDATA));
alignOffset(CDATA, tysize(ty));
s = symboldata(Offset(CDATA), ty);
Obj::bytes(CDATA, Offset(CDATA), len, p);
s->Sseg = CDATA;
}
s->Sfl = /*(config.flags3 & CFG3pic) ? FLgotoff :*/ FLextern;
return s;
}
/**************************
* Ouput read only data for data.
* Output:
* *pseg segment of that data
* Returns:
* offset of that data
*/
int Obj::data_readonly(char *p, int len, int *pseg)
{
int oldoff = Offset(CDATA);
SegData[CDATA]->SDbuf->reserve(len);
SegData[CDATA]->SDbuf->writen(p,len);
Offset(CDATA) += len;
*pseg = CDATA;
return oldoff;
}
int Obj::data_readonly(char *p, int len)
{
int pseg;
return Obj::data_readonly(p, len, &pseg);
}
/******************************
* Get segment for readonly string literals.
* The linker will pool strings in this section.
* Params:
* sz = number of bytes per character (1, 2, or 4)
* Returns:
* segment index
*/
int Obj::string_literal_segment(unsigned sz)
{
/* Elf special sections:
* .rodata.strM.N - M is size of character
* N is alignment
* .rodata.cstN - N fixed size readonly constants N bytes in size,
* aligned to the same size
*/
static const char name[3][4] = { "1.1", "2.2", "4.4" };
const int i = (sz == 4) ? 2 : sz - 1;
const IDXSEC seg =
ElfObj::getsegment(".rodata.str", name[i], SHT_PROGBITS, SHF_ALLOC | SHF_MERGE | SHF_STRINGS, sz);
return seg;
}
/******************************
* Perform initialization that applies to all .o output files.
* Called before any other obj_xxx routines
*/
Obj *Obj::init(Outbuffer *objbuf, const char *filename, const char *csegname)
{
//printf("Obj::init()\n");
ElfObj *obj = new ElfObj();
cseg = CODE;
fobjbuf = objbuf;
mapsec2sym = NULL;
note_data = NULL;
secidx_note = 0;
comment_data = NULL;
seg_tlsseg = UNKNOWN;
seg_tlsseg_bss = UNKNOWN;
GOTsym = NULL;
// Initialize buffers
if (symtab_strings)
symtab_strings->setsize(1);
else
{ symtab_strings = new Outbuffer(1024);
symtab_strings->reserve(2048);
symtab_strings->writeByte(0);
}
if (SECbuf)
SECbuf->setsize(0);
section_cnt = 0;
if (I64)
{
static char section_names_init64[] =
"\0.symtab\0.strtab\0.shstrtab\0.text\0.data\0.bss\0.note\0.comment\0.rodata\0.note.GNU-stack\0.data.rel.ro\0.rela.text\0.rela.data";
#define NAMIDX_NONE 0
#define NAMIDX_SYMTAB 1 // .symtab
#define NAMIDX_STRTAB 9 // .strtab
#define NAMIDX_SHSTRTAB 17 // .shstrtab
#define NAMIDX_TEXT 27 // .text
#define NAMIDX_DATA 33 // .data
#define NAMIDX_BSS 39 // .bss
#define NAMIDX_NOTE 44 // .note
#define NAMIDX_COMMENT 50 // .comment
#define NAMIDX_RODATA 59 // .rodata
#define NAMIDX_GNUSTACK 67 // .note.GNU-stack
#define NAMIDX_CDATAREL 83 // .data.rel.ro
#define NAMIDX_RELTEXT 96 // .rel.text and .rela.text
#define NAMIDX_RELDATA 106 // .rel.data
#define NAMIDX_RELDATA64 107 // .rela.data
if (section_names)
section_names->setsize(sizeof(section_names_init64));
else
{ section_names = new Outbuffer(512);
section_names->reserve(1024);
section_names->writen(section_names_init64, sizeof(section_names_init64));
}
if (section_names_hashtable)
delete section_names_hashtable;
section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR));
// name,type,flags,addr,offset,size,link,info,addralign,entsize
elf_newsection2(0, SHT_NULL, 0, 0,0,0,0,0, 0,0);
elf_newsection2(NAMIDX_TEXT,SHT_PROGBITS,SHF_ALLOC|SHF_EXECINSTR,0,0,0,0,0, 4,0);
elf_newsection2(NAMIDX_RELTEXT,SHT_RELA, 0,0,0,0,SHN_SYMTAB, SHN_TEXT, 8,0x18);
elf_newsection2(NAMIDX_DATA,SHT_PROGBITS,SHF_ALLOC|SHF_WRITE, 0,0,0,0,0, 8,0);
elf_newsection2(NAMIDX_RELDATA64,SHT_RELA, 0,0,0,0,SHN_SYMTAB, SHN_DATA, 8,0x18);
elf_newsection2(NAMIDX_BSS, SHT_NOBITS,SHF_ALLOC|SHF_WRITE, 0,0,0,0,0, 16,0);
elf_newsection2(NAMIDX_RODATA,SHT_PROGBITS,SHF_ALLOC, 0,0,0,0,0, 16,0);
elf_newsection2(NAMIDX_STRTAB,SHT_STRTAB, 0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_SYMTAB,SHT_SYMTAB, 0, 0,0,0,0,0, 8,0);
elf_newsection2(NAMIDX_SHSTRTAB,SHT_STRTAB, 0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_COMMENT, SHT_PROGBITS,0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_NOTE,SHT_NOTE, 0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_GNUSTACK,SHT_PROGBITS,0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_CDATAREL,SHT_PROGBITS,SHF_ALLOC|SHF_WRITE,0,0,0,0,0, 16,0);
IDXSTR namidx;
namidx = NAMIDX_TEXT; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_RELTEXT; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_DATA; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_RELDATA64; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_BSS; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_RODATA; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_STRTAB; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_SYMTAB; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_SHSTRTAB; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_COMMENT; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_NOTE; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_GNUSTACK; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_CDATAREL; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
}
else
{
static char section_names_init[] =
"\0.symtab\0.strtab\0.shstrtab\0.text\0.data\0.bss\0.note\0.comment\0.rodata\0.note.GNU-stack\0.data.rel.ro\0.rel.text\0.rel.data";
if (section_names)
section_names->setsize(sizeof(section_names_init));
else
{ section_names = new Outbuffer(512);
section_names->reserve(100*1024);
section_names->writen(section_names_init, sizeof(section_names_init));
}
if (section_names_hashtable)
delete section_names_hashtable;
section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR));
// name,type,flags,addr,offset,size,link,info,addralign,entsize
elf_newsection2(0, SHT_NULL, 0, 0,0,0,0,0, 0,0);
elf_newsection2(NAMIDX_TEXT,SHT_PROGBITS,SHF_ALLOC|SHF_EXECINSTR,0,0,0,0,0, 16,0);
elf_newsection2(NAMIDX_RELTEXT,SHT_REL, 0,0,0,0,SHN_SYMTAB, SHN_TEXT, 4,8);
elf_newsection2(NAMIDX_DATA,SHT_PROGBITS,SHF_ALLOC|SHF_WRITE, 0,0,0,0,0, 4,0);
elf_newsection2(NAMIDX_RELDATA,SHT_REL, 0,0,0,0,SHN_SYMTAB, SHN_DATA, 4,8);
elf_newsection2(NAMIDX_BSS, SHT_NOBITS,SHF_ALLOC|SHF_WRITE, 0,0,0,0,0, 32,0);
elf_newsection2(NAMIDX_RODATA,SHT_PROGBITS,SHF_ALLOC, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_STRTAB,SHT_STRTAB, 0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_SYMTAB,SHT_SYMTAB, 0, 0,0,0,0,0, 4,0);
elf_newsection2(NAMIDX_SHSTRTAB,SHT_STRTAB, 0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_COMMENT, SHT_PROGBITS,0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_NOTE,SHT_NOTE, 0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_GNUSTACK,SHT_PROGBITS,0, 0,0,0,0,0, 1,0);
elf_newsection2(NAMIDX_CDATAREL,SHT_PROGBITS,SHF_ALLOC|SHF_WRITE,0,0,0,0,0, 1,0);
IDXSTR namidx;
namidx = NAMIDX_TEXT; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_RELTEXT; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_DATA; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_RELDATA; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_BSS; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_RODATA; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_STRTAB; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_SYMTAB; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_SHSTRTAB; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_COMMENT; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_NOTE; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_GNUSTACK; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
namidx = NAMIDX_CDATAREL; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx;
}
if (SYMbuf)
SYMbuf->setsize(0);
if (reset_symbuf)
{
symbol **p = (symbol **)reset_symbuf->buf;
const size_t n = reset_symbuf->size() / sizeof(symbol *);
for (size_t i = 0; i < n; ++i)
symbol_reset(p[i]);
reset_symbuf->setsize(0);
}
else
{
reset_symbuf = new Outbuffer(50 * sizeof(symbol *));
}
if (shndx_data)
shndx_data->setsize(0);
symbol_idx = 0;
local_cnt = 0;
// The symbols that every object file has
elf_addsym(0, 0, 0, STT_NOTYPE, STB_LOCAL, 0);
elf_addsym(0, 0, 0, STT_FILE, STB_LOCAL, SHN_ABS); // STI_FILE
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHN_TEXT); // STI_TEXT
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHN_DATA); // STI_DATA
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHN_BSS); // STI_BSS
elf_addsym(0, 0, 0, STT_NOTYPE, STB_LOCAL, SHN_TEXT); // STI_GCC
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHN_RODAT); // STI_RODAT
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHN_NOTE); // STI_NOTE
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHN_COM); // STI_COM
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHN_CDATAREL); // STI_CDATAREL
// Initialize output buffers for CODE, DATA and COMMENTS
// (NOTE not supported, BSS not required)
seg_count = 0;
elf_addsegment2(SHN_TEXT, STI_TEXT, SHN_RELTEXT);
assert(SegData[CODE]->SDseg == CODE);
elf_addsegment2(SHN_DATA, STI_DATA, SHN_RELDATA);
assert(SegData[DATA]->SDseg == DATA);
elf_addsegment2(SHN_RODAT, STI_RODAT, 0);
assert(SegData[CDATA]->SDseg == CDATA);
elf_addsegment2(SHN_BSS, STI_BSS, 0);
assert(SegData[UDATA]->SDseg == UDATA);
elf_addsegment2(SHN_CDATAREL, STI_CDATAREL, 0);
assert(SegData[CDATAREL]->SDseg == CDATAREL);
elf_addsegment2(SHN_COM, STI_COM, 0);
assert(SegData[COMD]->SDseg == COMD);
dwarf_initfile(filename);
return obj;
}
/**************************
* Initialize the start of object output for this particular .o file.
*
* Input:
* filename: Name of source file
* csegname: User specified default code segment name
*/
void Obj::initfile(const char *filename, const char *csegname, const char *modname)
{
//dbg_printf("Obj::initfile(filename = %s, modname = %s)\n",filename,modname);
IDXSTR name = Obj::addstr(symtab_strings, filename);
if (I64)
SymbolTable64[STI_FILE].st_name = name;
else
SymbolTable[STI_FILE].st_name = name;
#if 0
// compiler flag for linker
if (I64)
SymbolTable64[STI_GCC].st_name = Obj::addstr(symtab_strings,"gcc2_compiled.");
else
SymbolTable[STI_GCC].st_name = Obj::addstr(symtab_strings,"gcc2_compiled.");
#endif
if (csegname && *csegname && strcmp(csegname,".text"))
{ // Define new section and make it the default for cseg segment
// NOTE: cseg is initialized to CODE
IDXSEC newsecidx;
Elf32_Shdr *newtextsec;
IDXSYM newsymidx;
SegData[cseg]->SDshtidx = newsecidx =
elf_newsection(csegname,0,SHT_PROGBITS,SHF_ALLOC|SHF_EXECINSTR);
newtextsec = &SecHdrTab[newsecidx];
newtextsec->sh_addralign = 4;
SegData[cseg]->SDsymidx =
elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, newsecidx);
}
if (config.fulltypes)
dwarf_initmodule(filename, modname);
}
/***************************
* Renumber symbols so they are
* ordered as locals, weak and then global
* Returns:
* sorted symbol table, caller must free with util_free()
*/
void *elf_renumbersyms()
{ void *symtab;
int nextlocal = 0;
int nextglobal = local_cnt;
SYMIDX *sym_map = (SYMIDX *)util_malloc(sizeof(SYMIDX),symbol_idx);
if (I64)
{
Elf64_Sym *oldsymtab = (Elf64_Sym *)SYMbuf->buf;