-
Notifications
You must be signed in to change notification settings - Fork 17.7k
/
elf.go
2486 lines (2165 loc) · 63.9 KB
/
elf.go
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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ld
import (
"cmd/internal/hash"
"cmd/internal/objabi"
"cmd/internal/sys"
"cmd/link/internal/loader"
"cmd/link/internal/sym"
"debug/elf"
"encoding/binary"
"encoding/hex"
"fmt"
"internal/buildcfg"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
)
/*
* Derived from:
* $FreeBSD: src/sys/sys/elf32.h,v 1.8.14.1 2005/12/30 22:13:58 marcel Exp $
* $FreeBSD: src/sys/sys/elf64.h,v 1.10.14.1 2005/12/30 22:13:58 marcel Exp $
* $FreeBSD: src/sys/sys/elf_common.h,v 1.15.8.1 2005/12/30 22:13:58 marcel Exp $
* $FreeBSD: src/sys/alpha/include/elf.h,v 1.14 2003/09/25 01:10:22 peter Exp $
* $FreeBSD: src/sys/amd64/include/elf.h,v 1.18 2004/08/03 08:21:48 dfr Exp $
* $FreeBSD: src/sys/arm/include/elf.h,v 1.5.2.1 2006/06/30 21:42:52 cognet Exp $
* $FreeBSD: src/sys/i386/include/elf.h,v 1.16 2004/08/02 19:12:17 dfr Exp $
* $FreeBSD: src/sys/powerpc/include/elf.h,v 1.7 2004/11/02 09:47:01 ssouhlal Exp $
* $FreeBSD: src/sys/sparc64/include/elf.h,v 1.12 2003/09/25 01:10:26 peter Exp $
*
* Copyright (c) 1996-1998 John D. Polstra. All rights reserved.
* Copyright (c) 2001 David E. O'Brien
* Portions Copyright 2009 The Go Authors. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* ELF definitions that are independent of architecture or word size.
*/
/*
* Note header. The ".note" section contains an array of notes. Each
* begins with this header, aligned to a word boundary. Immediately
* following the note header is n_namesz bytes of name, padded to the
* next word boundary. Then comes n_descsz bytes of descriptor, again
* padded to a word boundary. The values of n_namesz and n_descsz do
* not include the padding.
*/
type elfNote struct {
nNamesz uint32
nDescsz uint32
nType uint32
}
/* For accessing the fields of r_info. */
/* For constructing r_info from field values. */
/*
* Relocation types.
*/
const (
ARM_MAGIC_TRAMP_NUMBER = 0x5c000003
)
/*
* Symbol table entries.
*/
/* For accessing the fields of st_info. */
/* For constructing st_info from field values. */
/* For accessing the fields of st_other. */
/*
* ELF header.
*/
type ElfEhdr elf.Header64
/*
* Section header.
*/
type ElfShdr struct {
elf.Section64
shnum elf.SectionIndex
}
/*
* Program header.
*/
type ElfPhdr elf.ProgHeader
/* For accessing the fields of r_info. */
/* For constructing r_info from field values. */
/*
* Symbol table entries.
*/
/* For accessing the fields of st_info. */
/* For constructing st_info from field values. */
/* For accessing the fields of st_other. */
/*
* Go linker interface
*/
const (
ELF64HDRSIZE = 64
ELF64PHDRSIZE = 56
ELF64SHDRSIZE = 64
ELF64RELSIZE = 16
ELF64RELASIZE = 24
ELF64SYMSIZE = 24
ELF32HDRSIZE = 52
ELF32PHDRSIZE = 32
ELF32SHDRSIZE = 40
ELF32SYMSIZE = 16
ELF32RELSIZE = 8
)
/*
* The interface uses the 64-bit structures always,
* to avoid code duplication. The writers know how to
* marshal a 32-bit representation from the 64-bit structure.
*/
var elfstrdat, elfshstrdat []byte
/*
* Total amount of space to reserve at the start of the file
* for Header, PHeaders, SHeaders, and interp.
* May waste some.
* On FreeBSD, cannot be larger than a page.
*/
const (
ELFRESERVE = 4096
)
/*
* We use the 64-bit data structures on both 32- and 64-bit machines
* in order to write the code just once. The 64-bit data structure is
* written in the 32-bit format on the 32-bit machines.
*/
const (
NSECT = 400
)
var (
Nelfsym = 1
elf64 bool
// Either ".rel" or ".rela" depending on which type of relocation the
// target platform uses.
elfRelType string
ehdr ElfEhdr
phdr [NSECT]*ElfPhdr
shdr [NSECT]*ElfShdr
interp string
)
// ELFArch includes target-specific hooks for ELF targets.
// This is initialized by the target-specific Init function
// called by the linker's main function in cmd/link/main.go.
type ELFArch struct {
// TODO: Document these fields.
Androiddynld string
Linuxdynld string
LinuxdynldMusl string
Freebsddynld string
Netbsddynld string
Openbsddynld string
Dragonflydynld string
Solarisdynld string
Reloc1 func(*Link, *OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int, int64) bool
RelocSize uint32 // size of an ELF relocation record, must match Reloc1.
SetupPLT func(ctxt *Link, ldr *loader.Loader, plt, gotplt *loader.SymbolBuilder, dynamic loader.Sym)
// DynamicReadOnly can be set to true to make the .dynamic
// section read-only. By default it is writable.
// This is used by MIPS targets.
DynamicReadOnly bool
}
type Elfstring struct {
s string
off int
}
var elfstr [100]Elfstring
var nelfstr int
var buildinfo []byte
/*
Initialize the global variable that describes the ELF header. It will be updated as
we write section and prog headers.
*/
func Elfinit(ctxt *Link) {
ctxt.IsELF = true
if ctxt.Arch.InFamily(sys.AMD64, sys.ARM64, sys.Loong64, sys.MIPS64, sys.PPC64, sys.RISCV64, sys.S390X) {
elfRelType = ".rela"
} else {
elfRelType = ".rel"
}
switch ctxt.Arch.Family {
// 64-bit architectures
case sys.PPC64, sys.S390X:
if ctxt.Arch.ByteOrder == binary.BigEndian && ctxt.HeadType != objabi.Hopenbsd {
ehdr.Flags = 1 /* Version 1 ABI */
} else {
ehdr.Flags = 2 /* Version 2 ABI */
}
fallthrough
case sys.AMD64, sys.ARM64, sys.Loong64, sys.MIPS64, sys.RISCV64:
if ctxt.Arch.Family == sys.MIPS64 {
ehdr.Flags = 0x20000004 /* MIPS 3 CPIC */
}
if ctxt.Arch.Family == sys.Loong64 {
ehdr.Flags = 0x43 /* DOUBLE_FLOAT, OBJABI_V1 */
}
if ctxt.Arch.Family == sys.RISCV64 {
ehdr.Flags = 0x4 /* RISCV Float ABI Double */
}
elf64 = true
ehdr.Phoff = ELF64HDRSIZE /* Must be ELF64HDRSIZE: first PHdr must follow ELF header */
ehdr.Shoff = ELF64HDRSIZE /* Will move as we add PHeaders */
ehdr.Ehsize = ELF64HDRSIZE /* Must be ELF64HDRSIZE */
ehdr.Phentsize = ELF64PHDRSIZE /* Must be ELF64PHDRSIZE */
ehdr.Shentsize = ELF64SHDRSIZE /* Must be ELF64SHDRSIZE */
// 32-bit architectures
case sys.ARM, sys.MIPS:
if ctxt.Arch.Family == sys.ARM {
// we use EABI on linux/arm, freebsd/arm, netbsd/arm.
if ctxt.HeadType == objabi.Hlinux || ctxt.HeadType == objabi.Hfreebsd || ctxt.HeadType == objabi.Hnetbsd {
// We set a value here that makes no indication of which
// float ABI the object uses, because this is information
// used by the dynamic linker to compare executables and
// shared libraries -- so it only matters for cgo calls, and
// the information properly comes from the object files
// produced by the host C compiler. parseArmAttributes in
// ldelf.go reads that information and updates this field as
// appropriate.
ehdr.Flags = 0x5000002 // has entry point, Version5 EABI
}
} else if ctxt.Arch.Family == sys.MIPS {
ehdr.Flags = 0x50001004 /* MIPS 32 CPIC O32*/
}
fallthrough
default:
ehdr.Phoff = ELF32HDRSIZE
/* Must be ELF32HDRSIZE: first PHdr must follow ELF header */
ehdr.Shoff = ELF32HDRSIZE /* Will move as we add PHeaders */
ehdr.Ehsize = ELF32HDRSIZE /* Must be ELF32HDRSIZE */
ehdr.Phentsize = ELF32PHDRSIZE /* Must be ELF32PHDRSIZE */
ehdr.Shentsize = ELF32SHDRSIZE /* Must be ELF32SHDRSIZE */
}
}
// Make sure PT_LOAD is aligned properly and
// that there is no gap,
// correct ELF loaders will do this implicitly,
// but buggy ELF loaders like the one in some
// versions of QEMU and UPX won't.
func fixElfPhdr(e *ElfPhdr) {
frag := int(e.Vaddr & (e.Align - 1))
e.Off -= uint64(frag)
e.Vaddr -= uint64(frag)
e.Paddr -= uint64(frag)
e.Filesz += uint64(frag)
e.Memsz += uint64(frag)
}
func elf64phdr(out *OutBuf, e *ElfPhdr) {
if e.Type == elf.PT_LOAD {
fixElfPhdr(e)
}
out.Write32(uint32(e.Type))
out.Write32(uint32(e.Flags))
out.Write64(e.Off)
out.Write64(e.Vaddr)
out.Write64(e.Paddr)
out.Write64(e.Filesz)
out.Write64(e.Memsz)
out.Write64(e.Align)
}
func elf32phdr(out *OutBuf, e *ElfPhdr) {
if e.Type == elf.PT_LOAD {
fixElfPhdr(e)
}
out.Write32(uint32(e.Type))
out.Write32(uint32(e.Off))
out.Write32(uint32(e.Vaddr))
out.Write32(uint32(e.Paddr))
out.Write32(uint32(e.Filesz))
out.Write32(uint32(e.Memsz))
out.Write32(uint32(e.Flags))
out.Write32(uint32(e.Align))
}
func elf64shdr(out *OutBuf, e *ElfShdr) {
out.Write32(e.Name)
out.Write32(uint32(e.Type))
out.Write64(uint64(e.Flags))
out.Write64(e.Addr)
out.Write64(e.Off)
out.Write64(e.Size)
out.Write32(e.Link)
out.Write32(e.Info)
out.Write64(e.Addralign)
out.Write64(e.Entsize)
}
func elf32shdr(out *OutBuf, e *ElfShdr) {
out.Write32(e.Name)
out.Write32(uint32(e.Type))
out.Write32(uint32(e.Flags))
out.Write32(uint32(e.Addr))
out.Write32(uint32(e.Off))
out.Write32(uint32(e.Size))
out.Write32(e.Link)
out.Write32(e.Info)
out.Write32(uint32(e.Addralign))
out.Write32(uint32(e.Entsize))
}
func elfwriteshdrs(out *OutBuf) uint32 {
if elf64 {
for i := 0; i < int(ehdr.Shnum); i++ {
elf64shdr(out, shdr[i])
}
return uint32(ehdr.Shnum) * ELF64SHDRSIZE
}
for i := 0; i < int(ehdr.Shnum); i++ {
elf32shdr(out, shdr[i])
}
return uint32(ehdr.Shnum) * ELF32SHDRSIZE
}
func elfsetstring(ctxt *Link, s loader.Sym, str string, off int) {
if nelfstr >= len(elfstr) {
ctxt.Errorf(s, "too many elf strings")
errorexit()
}
elfstr[nelfstr].s = str
elfstr[nelfstr].off = off
nelfstr++
}
func elfwritephdrs(out *OutBuf) uint32 {
if elf64 {
for i := 0; i < int(ehdr.Phnum); i++ {
elf64phdr(out, phdr[i])
}
return uint32(ehdr.Phnum) * ELF64PHDRSIZE
}
for i := 0; i < int(ehdr.Phnum); i++ {
elf32phdr(out, phdr[i])
}
return uint32(ehdr.Phnum) * ELF32PHDRSIZE
}
func newElfPhdr() *ElfPhdr {
e := new(ElfPhdr)
if ehdr.Phnum >= NSECT {
Errorf("too many phdrs")
} else {
phdr[ehdr.Phnum] = e
ehdr.Phnum++
}
if elf64 {
ehdr.Shoff += ELF64PHDRSIZE
} else {
ehdr.Shoff += ELF32PHDRSIZE
}
return e
}
func newElfShdr(name int64) *ElfShdr {
e := new(ElfShdr)
e.Name = uint32(name)
e.shnum = elf.SectionIndex(ehdr.Shnum)
if ehdr.Shnum >= NSECT {
Errorf("too many shdrs")
} else {
shdr[ehdr.Shnum] = e
ehdr.Shnum++
}
return e
}
func getElfEhdr() *ElfEhdr {
return &ehdr
}
func elf64writehdr(out *OutBuf) uint32 {
out.Write(ehdr.Ident[:])
out.Write16(uint16(ehdr.Type))
out.Write16(uint16(ehdr.Machine))
out.Write32(uint32(ehdr.Version))
out.Write64(ehdr.Entry)
out.Write64(ehdr.Phoff)
out.Write64(ehdr.Shoff)
out.Write32(ehdr.Flags)
out.Write16(ehdr.Ehsize)
out.Write16(ehdr.Phentsize)
out.Write16(ehdr.Phnum)
out.Write16(ehdr.Shentsize)
out.Write16(ehdr.Shnum)
out.Write16(ehdr.Shstrndx)
return ELF64HDRSIZE
}
func elf32writehdr(out *OutBuf) uint32 {
out.Write(ehdr.Ident[:])
out.Write16(uint16(ehdr.Type))
out.Write16(uint16(ehdr.Machine))
out.Write32(uint32(ehdr.Version))
out.Write32(uint32(ehdr.Entry))
out.Write32(uint32(ehdr.Phoff))
out.Write32(uint32(ehdr.Shoff))
out.Write32(ehdr.Flags)
out.Write16(ehdr.Ehsize)
out.Write16(ehdr.Phentsize)
out.Write16(ehdr.Phnum)
out.Write16(ehdr.Shentsize)
out.Write16(ehdr.Shnum)
out.Write16(ehdr.Shstrndx)
return ELF32HDRSIZE
}
func elfwritehdr(out *OutBuf) uint32 {
if elf64 {
return elf64writehdr(out)
}
return elf32writehdr(out)
}
/* Taken directly from the definition document for ELF64. */
func elfhash(name string) uint32 {
var h uint32
for i := 0; i < len(name); i++ {
h = (h << 4) + uint32(name[i])
if g := h & 0xf0000000; g != 0 {
h ^= g >> 24
}
h &= 0x0fffffff
}
return h
}
func elfWriteDynEntSym(ctxt *Link, s *loader.SymbolBuilder, tag elf.DynTag, t loader.Sym) {
Elfwritedynentsymplus(ctxt, s, tag, t, 0)
}
func Elfwritedynent(arch *sys.Arch, s *loader.SymbolBuilder, tag elf.DynTag, val uint64) {
if elf64 {
s.AddUint64(arch, uint64(tag))
s.AddUint64(arch, val)
} else {
s.AddUint32(arch, uint32(tag))
s.AddUint32(arch, uint32(val))
}
}
func Elfwritedynentsymplus(ctxt *Link, s *loader.SymbolBuilder, tag elf.DynTag, t loader.Sym, add int64) {
if elf64 {
s.AddUint64(ctxt.Arch, uint64(tag))
} else {
s.AddUint32(ctxt.Arch, uint32(tag))
}
s.AddAddrPlus(ctxt.Arch, t, add)
}
func elfwritedynentsymsize(ctxt *Link, s *loader.SymbolBuilder, tag elf.DynTag, t loader.Sym) {
if elf64 {
s.AddUint64(ctxt.Arch, uint64(tag))
} else {
s.AddUint32(ctxt.Arch, uint32(tag))
}
s.AddSize(ctxt.Arch, t)
}
func elfinterp(sh *ElfShdr, startva uint64, resoff uint64, p string) int {
interp = p
n := len(interp) + 1
sh.Addr = startva + resoff - uint64(n)
sh.Off = resoff - uint64(n)
sh.Size = uint64(n)
return n
}
func elfwriteinterp(out *OutBuf) int {
sh := elfshname(".interp")
out.SeekSet(int64(sh.Off))
out.WriteString(interp)
out.Write8(0)
return int(sh.Size)
}
// member of .gnu.attributes of MIPS for fpAbi
const (
// No floating point is present in the module (default)
MIPS_FPABI_NONE = 0
// FP code in the module uses the FP32 ABI for a 32-bit ABI
MIPS_FPABI_ANY = 1
// FP code in the module only uses single precision ABI
MIPS_FPABI_SINGLE = 2
// FP code in the module uses soft-float ABI
MIPS_FPABI_SOFT = 3
// FP code in the module assumes an FPU with FR=1 and has 12
// callee-saved doubles. Historic, no longer supported.
MIPS_FPABI_HIST = 4
// FP code in the module uses the FPXX ABI
MIPS_FPABI_FPXX = 5
// FP code in the module uses the FP64 ABI
MIPS_FPABI_FP64 = 6
// FP code in the module uses the FP64A ABI
MIPS_FPABI_FP64A = 7
)
func elfMipsAbiFlags(sh *ElfShdr, startva uint64, resoff uint64) int {
n := 24
sh.Addr = startva + resoff - uint64(n)
sh.Off = resoff - uint64(n)
sh.Size = uint64(n)
sh.Type = uint32(elf.SHT_MIPS_ABIFLAGS)
sh.Flags = uint64(elf.SHF_ALLOC)
return n
}
// Layout is given by this C definition:
//
// typedef struct
// {
// /* Version of flags structure. */
// uint16_t version;
// /* The level of the ISA: 1-5, 32, 64. */
// uint8_t isa_level;
// /* The revision of ISA: 0 for MIPS V and below, 1-n otherwise. */
// uint8_t isa_rev;
// /* The size of general purpose registers. */
// uint8_t gpr_size;
// /* The size of co-processor 1 registers. */
// uint8_t cpr1_size;
// /* The size of co-processor 2 registers. */
// uint8_t cpr2_size;
// /* The floating-point ABI. */
// uint8_t fp_abi;
// /* Processor-specific extension. */
// uint32_t isa_ext;
// /* Mask of ASEs used. */
// uint32_t ases;
// /* Mask of general flags. */
// uint32_t flags1;
// uint32_t flags2;
// } Elf_Internal_ABIFlags_v0;
func elfWriteMipsAbiFlags(ctxt *Link) int {
sh := elfshname(".MIPS.abiflags")
ctxt.Out.SeekSet(int64(sh.Off))
ctxt.Out.Write16(0) // version
ctxt.Out.Write8(32) // isaLevel
ctxt.Out.Write8(1) // isaRev
ctxt.Out.Write8(1) // gprSize
ctxt.Out.Write8(1) // cpr1Size
ctxt.Out.Write8(0) // cpr2Size
if buildcfg.GOMIPS == "softfloat" {
ctxt.Out.Write8(MIPS_FPABI_SOFT) // fpAbi
} else {
// Go cannot make sure non odd-number-fpr is used (ie, in load a double from memory).
// So, we mark the object is MIPS I style paired float/double register scheme,
// aka MIPS_FPABI_ANY. If we mark the object as FPXX, the kernel may use FR=1 mode,
// then we meet some problem.
// Note: MIPS_FPABI_ANY is bad naming: in fact it is MIPS I style FPR usage.
// It is not for 'ANY'.
// TODO: switch to FPXX after be sure that no odd-number-fpr is used.
ctxt.Out.Write8(MIPS_FPABI_ANY) // fpAbi
}
ctxt.Out.Write32(0) // isaExt
ctxt.Out.Write32(0) // ases
ctxt.Out.Write32(0) // flags1
ctxt.Out.Write32(0) // flags2
return int(sh.Size)
}
func elfnote(sh *ElfShdr, startva uint64, resoff uint64, sizes ...int) int {
n := resoff % 4
// if section contains multiple notes (as is the case with FreeBSD signature),
// multiple note sizes can be specified
for _, sz := range sizes {
n += 3*4 + uint64(sz)
}
sh.Type = uint32(elf.SHT_NOTE)
sh.Flags = uint64(elf.SHF_ALLOC)
sh.Addralign = 4
sh.Addr = startva + resoff - n
sh.Off = resoff - n
sh.Size = n - resoff%4
return int(n)
}
func elfwritenotehdr(out *OutBuf, str string, namesz uint32, descsz uint32, tag uint32) *ElfShdr {
sh := elfshname(str)
// Write Elf_Note header.
out.SeekSet(int64(sh.Off))
out.Write32(namesz)
out.Write32(descsz)
out.Write32(tag)
return sh
}
// NetBSD Signature (as per sys/exec_elf.h)
const (
ELF_NOTE_NETBSD_NAMESZ = 7
ELF_NOTE_NETBSD_DESCSZ = 4
ELF_NOTE_NETBSD_TAG = 1
ELF_NOTE_NETBSD_VERSION = 700000000 /* NetBSD 7.0 */
)
var ELF_NOTE_NETBSD_NAME = []byte("NetBSD\x00")
func elfnetbsdsig(sh *ElfShdr, startva uint64, resoff uint64) int {
n := int(Rnd(ELF_NOTE_NETBSD_NAMESZ, 4) + Rnd(ELF_NOTE_NETBSD_DESCSZ, 4))
return elfnote(sh, startva, resoff, n)
}
func elfwritenetbsdsig(out *OutBuf) int {
// Write Elf_Note header.
sh := elfwritenotehdr(out, ".note.netbsd.ident", ELF_NOTE_NETBSD_NAMESZ, ELF_NOTE_NETBSD_DESCSZ, ELF_NOTE_NETBSD_TAG)
if sh == nil {
return 0
}
// Followed by NetBSD string and version.
out.Write(ELF_NOTE_NETBSD_NAME)
out.Write8(0)
out.Write32(ELF_NOTE_NETBSD_VERSION)
return int(sh.Size)
}
// The race detector can't handle ASLR (address space layout randomization).
// ASLR is on by default for NetBSD, so we turn the ASLR off explicitly
// using a magic elf Note when building race binaries.
func elfnetbsdpax(sh *ElfShdr, startva uint64, resoff uint64) int {
n := int(Rnd(4, 4) + Rnd(4, 4))
return elfnote(sh, startva, resoff, n)
}
func elfwritenetbsdpax(out *OutBuf) int {
sh := elfwritenotehdr(out, ".note.netbsd.pax", 4 /* length of PaX\x00 */, 4 /* length of flags */, 0x03 /* PaX type */)
if sh == nil {
return 0
}
out.Write([]byte("PaX\x00"))
out.Write32(0x20) // 0x20 = Force disable ASLR
return int(sh.Size)
}
// OpenBSD Signature
const (
ELF_NOTE_OPENBSD_NAMESZ = 8
ELF_NOTE_OPENBSD_DESCSZ = 4
ELF_NOTE_OPENBSD_TAG = 1
ELF_NOTE_OPENBSD_VERSION = 0
)
var ELF_NOTE_OPENBSD_NAME = []byte("OpenBSD\x00")
func elfopenbsdsig(sh *ElfShdr, startva uint64, resoff uint64) int {
n := ELF_NOTE_OPENBSD_NAMESZ + ELF_NOTE_OPENBSD_DESCSZ
return elfnote(sh, startva, resoff, n)
}
func elfwriteopenbsdsig(out *OutBuf) int {
// Write Elf_Note header.
sh := elfwritenotehdr(out, ".note.openbsd.ident", ELF_NOTE_OPENBSD_NAMESZ, ELF_NOTE_OPENBSD_DESCSZ, ELF_NOTE_OPENBSD_TAG)
if sh == nil {
return 0
}
// Followed by OpenBSD string and version.
out.Write(ELF_NOTE_OPENBSD_NAME)
out.Write32(ELF_NOTE_OPENBSD_VERSION)
return int(sh.Size)
}
// FreeBSD Signature (as per sys/elf_common.h)
const (
ELF_NOTE_FREEBSD_NAMESZ = 8
ELF_NOTE_FREEBSD_DESCSZ = 4
ELF_NOTE_FREEBSD_ABI_TAG = 1
ELF_NOTE_FREEBSD_NOINIT_TAG = 2
ELF_NOTE_FREEBSD_FEATURE_CTL_TAG = 4
ELF_NOTE_FREEBSD_VERSION = 1203000 // 12.3-RELEASE
ELF_NOTE_FREEBSD_FCTL_ASLR_DISABLE = 0x1
)
const ELF_NOTE_FREEBSD_NAME = "FreeBSD\x00"
func elffreebsdsig(sh *ElfShdr, startva uint64, resoff uint64) int {
n := ELF_NOTE_FREEBSD_NAMESZ + ELF_NOTE_FREEBSD_DESCSZ
// FreeBSD signature section contains 3 equally sized notes
return elfnote(sh, startva, resoff, n, n, n)
}
// elfwritefreebsdsig writes FreeBSD .note section.
//
// See https://www.netbsd.org/docs/kernel/elf-notes.html for the description of
// a Note element format and
// https://github.com/freebsd/freebsd-src/blob/main/sys/sys/elf_common.h#L790
// for the FreeBSD-specific values.
func elfwritefreebsdsig(out *OutBuf) int {
sh := elfshname(".note.tag")
if sh == nil {
return 0
}
out.SeekSet(int64(sh.Off))
// NT_FREEBSD_ABI_TAG
out.Write32(ELF_NOTE_FREEBSD_NAMESZ)
out.Write32(ELF_NOTE_FREEBSD_DESCSZ)
out.Write32(ELF_NOTE_FREEBSD_ABI_TAG)
out.WriteString(ELF_NOTE_FREEBSD_NAME)
out.Write32(ELF_NOTE_FREEBSD_VERSION)
// NT_FREEBSD_NOINIT_TAG
out.Write32(ELF_NOTE_FREEBSD_NAMESZ)
out.Write32(ELF_NOTE_FREEBSD_DESCSZ)
out.Write32(ELF_NOTE_FREEBSD_NOINIT_TAG)
out.WriteString(ELF_NOTE_FREEBSD_NAME)
out.Write32(0)
// NT_FREEBSD_FEATURE_CTL
out.Write32(ELF_NOTE_FREEBSD_NAMESZ)
out.Write32(ELF_NOTE_FREEBSD_DESCSZ)
out.Write32(ELF_NOTE_FREEBSD_FEATURE_CTL_TAG)
out.WriteString(ELF_NOTE_FREEBSD_NAME)
if *flagRace {
// The race detector can't handle ASLR, turn the ASLR off when compiling with -race.
out.Write32(ELF_NOTE_FREEBSD_FCTL_ASLR_DISABLE)
} else {
out.Write32(0)
}
return int(sh.Size)
}
func addbuildinfo(ctxt *Link) {
val := *flagHostBuildid
if val == "" || val == "none" {
return
}
if val == "gobuildid" {
buildID := *flagBuildid
if buildID == "" {
Exitf("-B gobuildid requires a Go build ID supplied via -buildid")
}
if ctxt.IsDarwin() {
buildinfo = uuidFromGoBuildId(buildID)
return
}
hashedBuildID := hash.Sum32([]byte(buildID))
buildinfo = hashedBuildID[:20]
return
}
if !strings.HasPrefix(val, "0x") {
Exitf("-B argument must start with 0x: %s", val)
}
ov := val
val = val[2:]
maxLen := 32
if ctxt.IsDarwin() {
maxLen = 16
}
if hex.DecodedLen(len(val)) > maxLen {
Exitf("-B option too long (max %d digits): %s", maxLen, ov)
}
b, err := hex.DecodeString(val)
if err != nil {
if err == hex.ErrLength {
Exitf("-B argument must have even number of digits: %s", ov)
}
if inv, ok := err.(hex.InvalidByteError); ok {
Exitf("-B argument contains invalid hex digit %c: %s", byte(inv), ov)
}
Exitf("-B argument contains invalid hex: %s", ov)
}
buildinfo = b
}
// Build info note
const (
ELF_NOTE_BUILDINFO_NAMESZ = 4
ELF_NOTE_BUILDINFO_TAG = 3
)
var ELF_NOTE_BUILDINFO_NAME = []byte("GNU\x00")
func elfbuildinfo(sh *ElfShdr, startva uint64, resoff uint64) int {
n := int(ELF_NOTE_BUILDINFO_NAMESZ + Rnd(int64(len(buildinfo)), 4))
return elfnote(sh, startva, resoff, n)
}
func elfgobuildid(sh *ElfShdr, startva uint64, resoff uint64) int {
n := len(ELF_NOTE_GO_NAME) + int(Rnd(int64(len(*flagBuildid)), 4))
return elfnote(sh, startva, resoff, n)
}
func elfwritebuildinfo(out *OutBuf) int {
sh := elfwritenotehdr(out, ".note.gnu.build-id", ELF_NOTE_BUILDINFO_NAMESZ, uint32(len(buildinfo)), ELF_NOTE_BUILDINFO_TAG)
if sh == nil {
return 0
}
out.Write(ELF_NOTE_BUILDINFO_NAME)
out.Write(buildinfo)
var zero = make([]byte, 4)
out.Write(zero[:int(Rnd(int64(len(buildinfo)), 4)-int64(len(buildinfo)))])
return int(sh.Size)
}
func elfwritegobuildid(out *OutBuf) int {
sh := elfwritenotehdr(out, ".note.go.buildid", uint32(len(ELF_NOTE_GO_NAME)), uint32(len(*flagBuildid)), ELF_NOTE_GOBUILDID_TAG)
if sh == nil {
return 0
}
out.Write(ELF_NOTE_GO_NAME)
out.Write([]byte(*flagBuildid))
var zero = make([]byte, 4)
out.Write(zero[:int(Rnd(int64(len(*flagBuildid)), 4)-int64(len(*flagBuildid)))])
return int(sh.Size)
}
// Go specific notes
const (
ELF_NOTE_GOPKGLIST_TAG = 1
ELF_NOTE_GOABIHASH_TAG = 2
ELF_NOTE_GODEPS_TAG = 3
ELF_NOTE_GOBUILDID_TAG = 4
)
var ELF_NOTE_GO_NAME = []byte("Go\x00\x00")
var elfverneed int
type Elfaux struct {
next *Elfaux
num int
vers string
}
type Elflib struct {
next *Elflib
aux *Elfaux
file string
}
func addelflib(list **Elflib, file string, vers string) *Elfaux {
var lib *Elflib
for lib = *list; lib != nil; lib = lib.next {
if lib.file == file {
goto havelib
}
}
lib = new(Elflib)
lib.next = *list
lib.file = file
*list = lib
havelib:
for aux := lib.aux; aux != nil; aux = aux.next {
if aux.vers == vers {
return aux
}
}
aux := new(Elfaux)
aux.next = lib.aux
aux.vers = vers
lib.aux = aux
return aux
}
func elfdynhash(ctxt *Link) {
if !ctxt.IsELF {
return
}
nsym := Nelfsym
ldr := ctxt.loader
s := ldr.CreateSymForUpdate(".hash", 0)
s.SetType(sym.SELFROSECT)
i := nsym
nbucket := 1
for i > 0 {
nbucket++
i >>= 1
}
var needlib *Elflib
need := make([]*Elfaux, nsym)
chain := make([]uint32, nsym)
buckets := make([]uint32, nbucket)
for _, sy := range ldr.DynidSyms() {
dynid := ldr.SymDynid(sy)
if ldr.SymDynimpvers(sy) != "" {
need[dynid] = addelflib(&needlib, ldr.SymDynimplib(sy), ldr.SymDynimpvers(sy))
}
name := ldr.SymExtname(sy)
hc := elfhash(name)
b := hc % uint32(nbucket)
chain[dynid] = buckets[b]
buckets[b] = uint32(dynid)
}
// s390x (ELF64) hash table entries are 8 bytes
if ctxt.Arch.Family == sys.S390X {
s.AddUint64(ctxt.Arch, uint64(nbucket))
s.AddUint64(ctxt.Arch, uint64(nsym))
for i := 0; i < nbucket; i++ {
s.AddUint64(ctxt.Arch, uint64(buckets[i]))
}
for i := 0; i < nsym; i++ {
s.AddUint64(ctxt.Arch, uint64(chain[i]))