FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
WebKit/Source/JavaScriptCore/jit/GdbJIT.cpp at main · WebKit/WebKit · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WebKit
/
WebKit
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Pull requests
2.6k
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
jit
/
GdbJIT.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1463 lines (1243 loc) · 42 KB
Breadcrumbs
WebKit
/
Source
/
JavaScriptCore
/
jit
/
GdbJIT.cpp
Copy path
File metadata and controls
1463 lines (1243 loc) · 42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 (C) 2025 Igalia, S.L. All rights reserved.
* Copyright 2010 the V8 project 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 APPLE INC. ``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 APPLE INC. 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.
*/
#
include
"
config.h
"
#
include
"
GdbJIT.h
"
#
include
<
wtf/TZoneMallocInlines.h
>
#
if
ENABLE(ASSEMBLER)
#
if
OS(DARWIN) || OS(LINUX)
#
include
"
CallFrame.h
"
#
include
"
CallFrameInlines.h
"
#
include
"
Options.h
"
#
include
"
ProfilerSupport.h
"
#
include
<
fcntl.h
>
#
include
<
mutex
>
#
include
<
wtf/DataLog.h
>
#
include
<
wtf/ProcessID.h
>
#
include
<
wtf/RefCountedAndCanMakeWeakPtr.h
>
#
include
<
wtf/StringPrintStream.h
>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
//
Binary GDB JIT Interface as described in
//
http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
extern
"
C
"
{
enum
JITAction {
NoAction =
0
,
RegisterFunction =
1
,
UnregisterFunction =
2
};
struct
JITCodeEntry
{
JITCodeEntry* next { };
JITCodeEntry* prev { };
uint8_t
* symfileAddr { };
uint64_t
symfileSize { };
};
struct
JITDescriptor
{
uint32_t
version { };
uint32_t
actionFlag { };
JITCodeEntry* relevantEntry { };
JITCodeEntry* firstEntry { };
};
//
GDB will place breakpoint into this function.
//
To prevent GCC from inlining or removing it we place noinline attribute
//
and inline assembler statement inside.
static
REFERENCED_FROM_ASM
NEVER_INLINE
void
__jit_debug_register_code
()
{
__asm__
(
"
"
);
}
//
GDB will inspect contents of this descriptor.
//
Static initialization is necessary to prevent GDB from seeing
//
uninitialized descriptor.
static
REFERENCED_FROM_ASM
JITDescriptor __jit_debug_descriptor = {
1
,
0
,
nullptr
,
nullptr
};
}
//
extern "C"
namespace
JSC
{
namespace
GdbJITInternal
{
static
constexpr
bool
verbose =
false
;
}
//
namespace GdbJITInternal
WTF_MAKE_TZONE_ALLOCATED_IMPL
(GdbJIT);
GdbJIT&
GdbJIT::singleton
()
{
static
LazyNeverDestroyed<GdbJIT> logger;
static
std::once_flag onceKey;
std::call_once
(onceKey, [] {
logger.
construct
();
});
return
logger.
get
();
}
#
if
OS(DARWIN)
class
MachO
;
class
MachOSection
;
using
DebugObject = MachO;
using
DebugSection = MachOSection;
#
else
class
ELF
;
class
ELFSection
;
class
ELFStringTable
;
using
DebugObject =
ELF
;
using
DebugSection = ELFSection;
#
endif
template
<
typename
V>
static
inline
void
writeUnalignedValue
(
uint8_t
* p, V value)
{
memcpy
(p, &value,
sizeof
(V));
}
class
Writer
:
public
RefCountedAndCanMakeWeakPtr
<Writer> {
public:
static
Ref<Writer>
NODELETE
create
(DebugObject* obj)
{
return
adoptRef
(*
new
Writer
(obj));
}
uintptr_t
NODELETE
position
()
const
{
return
m_position; }
template
<
typename
T>
class
Slot
{
public:
Slot
(WeakPtr<Writer> writer,
uintptr_t
offset)
: m_writer(writer)
, m_offset(offset)
{
}
T*
operator
->() {
return
m_writer->
template
rawSlotAt
<T>(m_offset); }
T
operator
*() {
return
*m_writer->
template
rawSlotAt
<T>(m_offset); }
void
set
(
const
T& value)
{
writeUnalignedValue
(m_writer->
template
addressAt
<T>(m_offset), value);
}
Slot<T>
at
(
int
i) {
return
Slot<T>(m_writer, m_offset +
sizeof
(T) * i); }
private:
WeakPtr<Writer> m_writer;
uintptr_t
m_offset;
};
template
<
typename
T>
void
write
(
const
T& val)
{
ensure
(m_position +
sizeof
(T));
writeUnalignedValue
(addressAt<T>(m_position), val);
m_position +=
sizeof
(T);
}
template
<
typename
T>
Slot<T>
slotAt
(
uintptr_t
offset) LIFETIME_BOUND
{
ensure
(offset +
sizeof
(T));
return
Slot<T>(*
this
, offset);
}
template
<
typename
T>
Slot<T>
createSlotHere
() LIFETIME_BOUND
{
return
createSlotsHere<T>(
1
);
}
template
<
typename
T>
Slot<T>
createSlotsHere
(
uint32_t
count) LIFETIME_BOUND
{
uintptr_t
slotPosition = m_position;
m_position +=
sizeof
(T) * count;
ensure
(m_position);
return
slotAt<T>(slotPosition);
}
void
ensure
(
uintptr_t
pos)
{
if
(m_buffer.
size
() < pos)
m_buffer.
grow
(pos);
}
DebugObject*
NODELETE
debugObject
() {
return
m_debugObject; }
uint8_t
*
NODELETE
buffer
() LIFETIME_BOUND {
return
&m_buffer[
0
]; }
void
align
(
uintptr_t
align)
{
uintptr_t
delta = m_position % align;
if
(!delta)
return
;
uintptr_t
padding = align - delta;
ensure
(m_position += padding);
ASSERT
(m_position % align ==
0
);
}
void
writeULEB128
(
uintptr_t
value)
{
do
{
uint8_t
byte = value &
0x7F
;
value >>=
7
;
if
(value)
byte |=
0x80
;
write<
uint8_t
>(byte);
}
while
(value);
}
void
writeSLEB128
(
intptr_t
value)
{
bool
more =
true
;
while
(more) {
int8_t
byte = value &
0x7F
;
bool
byteSign = byte &
0x40
;
value >>=
7
;
if
((!value && !byteSign) || (value == -
1
&& byteSign))
more =
false
;
else
byte |=
0x80
;
write<
int8_t
>(byte);
}
}
void
writeString
(
const
CString& str)
{
for
(
auto
c : str.
span
())
write<
char
>(c);
write<
char
>(
'
\0
'
);
}
private:
template
<
typename
T>
friend
class
Slot
;
template
<
typename
T>
uint8_t
*
NODELETE
addressAt
(
uintptr_t
offset) LIFETIME_BOUND
{
ASSERT
(offset < m_buffer.
size
() && offset +
sizeof
(T) <= m_buffer.
size
());
return
&m_buffer[offset];
}
template
<
typename
T>
T*
NODELETE
rawSlotAt
(
uintptr_t
offset) LIFETIME_BOUND
{
ASSERT
(offset < m_buffer.
size
() && offset +
sizeof
(T) <= m_buffer.
size
());
return
reinterpret_cast
<T*>(&m_buffer[offset]);
}
Writer
(DebugObject* debugObject)
: m_debugObject(debugObject)
, m_position(
0
)
{
}
DebugObject* m_debugObject;
uintptr_t
m_position;
Vector<
uint8_t
> m_buffer;
};
class
CodeDescription
:
public
RefCounted
<CodeDescription> {
public:
const
CString&
NODELETE
name
()
const
LIFETIME_BOUND {
return
m_name; }
const
void
*
NODELETE
codeStart
()
const
{
return
reinterpret_cast
<
const
void
*>(m_codeRegion.
data
()); }
const
void
*
NODELETE
codeEnd
()
const
{
return
reinterpret_cast
<
const
void
*>(
std::to_address
(m_codeRegion.
end
())); }
uintptr_t
NODELETE
codeSize
()
const
{
return
m_codeRegion.
size
(); }
std::span<
const
uint8_t
>
NODELETE
region
() {
return
m_codeRegion; }
static
Ref<CodeDescription>
NODELETE
create
(
const
CString& name, std::span<
const
uint8_t
> region)
{
return
adoptRef
(*
new
CodeDescription
(name, region));
}
private:
CodeDescription
(
const
CString& name, std::span<
const
uint8_t
> region)
: m_name(name)
, m_codeRegion(region)
{
}
CString m_name;
std::span<
const
uint8_t
> m_codeRegion;
};
static
JITCodeEntry*
createCodeEntry
(
uint8_t
* symfileAddr,
uintptr_t
symfileSize)
{
//
It is easiest to just leak this.
auto
* entry =
static_cast
<JITCodeEntry*>(
malloc
(
sizeof
(JITCodeEntry) + symfileSize));
entry->
symfileAddr
=
reinterpret_cast
<
uint8_t
*>(entry +
1
);
entry->
symfileSize
= symfileSize;
memcpy
(entry->
symfileAddr
, symfileAddr, symfileSize);
entry->
prev
= entry->
next
=
nullptr
;
return
entry;
}
static
void
registerCodeEntry
(JITCodeEntry* entry)
{
entry->
next
= __jit_debug_descriptor.
firstEntry
;
if
(entry->
next
)
entry->
next
->
prev
= entry;
__jit_debug_descriptor.
firstEntry
= __jit_debug_descriptor.
relevantEntry
= entry;
__jit_debug_descriptor.
actionFlag
= RegisterFunction;
__jit_debug_register_code
();
}
static
void
unregisterCodeEntry
(JITCodeEntry* entry)
{
if
(entry->
prev
)
entry->
prev
->
next
= entry->
next
;
else
__jit_debug_descriptor.
firstEntry
= entry->
next
;
if
(entry->
next
)
entry->
next
->
prev
= entry->
prev
;
__jit_debug_descriptor.
relevantEntry
= entry;
__jit_debug_descriptor.
actionFlag
= UnregisterFunction;
__jit_debug_register_code
();
}
template
<
typename
THeader>
class
DebugSectionBase
{
WTF_DEPRECATED_MAKE_FAST_ALLOCATED
(DebugSectionBase);
public:
virtual
~DebugSectionBase
() =
default
;
virtual
void
writeBody
(Writer::Slot<THeader> header, Ref<Writer> writer)
{
uint64_t
start = writer->
position
();
if
(
writeBodyInternal
(writer)) {
header->
offset
=
static_cast
<
uint32_t
>(start);
uint64_t
end = writer->
position
();
header->
size
=
std::max
(end - start,
static_cast
<
uint64_t
>(header->
size
));
}
}
virtual
bool
writeBodyInternal
(Ref<Writer>) {
return
false
; }
using
Header = THeader;
};
struct
MachOSectionHeader
{
char
sectname[
16
];
char
segname[
16
];
uint64_t
addr;
uint64_t
size;
uint32_t
offset;
uint32_t
align;
uint32_t
reloff;
uint32_t
nreloc;
uint32_t
flags;
uint32_t
reserved1;
uint32_t
reserved2;
uint32_t
reserved3;
} __attribute__((packed,aligned(
1
)));
class
MachOSection
:
public
DebugSectionBase
<MachOSectionHeader> {
public:
enum
Type {
Regular =
0x0u
,
AttrCoalesced =
0xBu
,
AttrSomeInstructions =
0x400u
,
AttrDebug =
0x02000000u
,
AttrPureInstructions =
0x80000000u
};
MachOSection
(
const
CString& name,
const
CString& segment,
uint32_t
align,
const
void
* addr,
size_t
size,
uint32_t
flags)
: m_name(name)
, m_segment(segment)
, m_align(align)
, m_addr(addr)
, m_size(size)
, m_flags(flags)
{
if
(m_align) {
ASSERT
(
WTF::isPowerOfTwo
(align));
m_align =
WTF::fastLog2
(align);
}
}
~MachOSection
()
override
=
default
;
virtual
void
populateHeader
(Writer::Slot<Header> header)
{
header->
addr
=
reinterpret_cast
<
uintptr_t
>(m_addr);
header->
size
= m_size;
header->
offset
=
0
;
header->
align
= m_align;
header->
reloff
=
0
;
header->
nreloc
=
0
;
header->
flags
= m_flags;
header->
reserved1
=
0
;
header->
reserved2
=
0
;
header->
reserved3
=
0
;
memset
(header->
sectname
,
0
,
sizeof
(header->
sectname
));
memset
(header->
segname
,
0
,
sizeof
(header->
segname
));
ASSERT
(m_name.
length
() <
sizeof
(header->
sectname
));
ASSERT
(m_segment.
length
() <
sizeof
(header->
segname
));
strncpy
(header->
sectname
, m_name.
data
(),
sizeof
(header->
sectname
));
strncpy
(header->
segname
, m_segment.
data
(),
sizeof
(header->
segname
));
}
const
void
*
NODELETE
addr
()
const
{
return
m_addr; }
size_t
NODELETE
size
()
const
{
return
m_size; }
private:
CString m_name;
CString m_segment;
uint32_t
m_align;
const
void
* m_addr;
size_t
m_size;
uint32_t
m_flags;
};
struct
ELFsectionHeader
{
uint32_t
name;
uint32_t
type;
uintptr_t
flags;
const
void
* address;
uintptr_t
offset;
uintptr_t
size;
uint32_t
link;
uint32_t
info;
uintptr_t
alignment;
uintptr_t
entrySize;
} __attribute__((packed,aligned(
1
)));
#
if
OS(LINUX)
class
ELFSection
:
public
DebugSectionBase
<ELFsectionHeader> {
public:
enum
Type {
TypeNull =
0
,
TypeProgBits =
1
,
TypeSymTab =
2
,
TypeStrTab =
3
,
TypeRela =
4
,
TypeHash =
5
,
TypeDynamic =
6
,
TypeNote =
7
,
TypeNoBits =
8
,
TypeRel =
9
,
TypeShLib =
10
,
TypeDynSym =
11
,
TypeLoProc =
0x70000000
,
TypeX86_64Unwind =
0x70000001
,
TypeHiProc =
0x7FFFFFFF
,
TypeLoUser =
0x80000000
,
TypeHiUser =
0xFFFFFFFF
};
enum
Flags {
FlagWrite =
1
,
FlagAlloc =
2
,
FlagExec =
4
};
enum
SpecialIndexes {
IndexAbsolute =
0xFFF1
};
ELFSection
(
const
CString& name, Type type,
uintptr_t
align)
: m_type(type)
, m_name(name)
, m_align(align)
{
}
~ELFSection
()
override
=
default
;
void
populateHeader
(Writer::Slot<Header>, ELFStringTable* strtab);
void
writeBody
(Writer::Slot<Header> header, Ref<Writer> writer)
override
{
uintptr_t
start = writer->
position
();
if
(
writeBodyInternal
(writer)) {
uintptr_t
end = writer->
position
();
header->
offset
= start;
header->
size
= end - start;
}
}
bool
writeBodyInternal
(Ref<Writer>)
override
{
return
false
; }
uint16_t
index
()
const
{
return
m_index; }
void
setIndex
(
uint16_t
index) { m_index = index; }
const
Type m_type;
protected:
virtual
void
populateHeader
(Writer::Slot<Header> header)
{
header->
flags
=
0
;
header->
address
=
0
;
header->
offset
=
0
;
header->
size
=
0
;
header->
link
=
0
;
header->
info
=
0
;
header->
entrySize
=
0
;
}
private:
CString m_name;
uintptr_t
m_align;
uint16_t
m_index;
};
#
endif
//
OS(LINUX)
#
if
OS(DARWIN)
class
MachOTextSection
:
public
MachOSection
{
public:
MachOTextSection
(
uint32_t
align,
const
void
* codeAddr,
uintptr_t
codeSize)
: MachOSection(
"
__text
"
,
"
__TEXT
"
, align, codeAddr, codeSize, MachOSection::Regular | MachOSection::AttrSomeInstructions | MachOSection::AttrPureInstructions)
, m_codeAddr(
reinterpret_cast
<
const
uint64_t
*>(codeAddr))
, m_codeSize(codeSize)
{
}
bool
writeBodyInternal
(Ref<Writer> writer)
override
{
for
(
auto
* ptr = m_codeAddr; ptr < m_codeAddr + m_codeSize /
sizeof
(
uint64_t
); ++ptr)
writer->
write
<
uint64_t
>(*ptr);
return
true
;
}
private:
const
uint64_t
* m_codeAddr;
const
size_t
m_codeSize;
};
#
endif
//
OS(DARWIN)
#
if
OS(LINUX)
class
FullHeaderELFSection
:
public
ELFSection
{
public:
FullHeaderELFSection
(
const
CString& name, Type type,
uintptr_t
align,
const
void
* addr,
uintptr_t
offset,
uintptr_t
size,
uintptr_t
flags)
: ELFSection(name, type, align)
, m_addr(addr)
, m_offset(offset)
, m_size(size)
, m_flags(flags)
{
}
protected:
void
populateHeader
(Writer::Slot<Header> header)
override
{
ELFSection::populateHeader
(header);
header->
address
= m_addr;
header->
offset
= m_offset;
header->
size
= m_size;
header->
flags
= m_flags;
}
private:
const
void
* m_addr;
uintptr_t
m_offset;
uintptr_t
m_size;
uintptr_t
m_flags;
};
class
ELFStringTable
:
public
ELFSection
{
public:
explicit
ELFStringTable
(
const
CString& name)
: ELFSection(name, TypeStrTab,
1
)
, m_writer(
nullptr
)
, m_offset(
0
)
, m_size(
0
)
{
}
uintptr_t
add
(
const
CString& str)
{
if
(!str.
length
())
return
0
;
uintptr_t
offset = m_size;
writeString
(str);
return
offset;
}
void
attachWriter
(Ref<Writer> w)
{
m_writer = w.
ptr
();
m_offset = m_writer->
position
();
//
First entry in the string table should be an empty string.
writeString
(
"
"
);
}
void
detachWriter
()
{
m_writer =
nullptr
;
}
void
writeBody
(Writer::Slot<Header> header, Ref<Writer>)
override
{
ASSERT
(!m_writer);
header->
offset
= m_offset;
header->
size
= m_size;
}
private:
void
writeString
(
const
CString& str)
{
for
(
auto
c : str.
span
())
m_writer->
write
(c);
m_writer->
write
(
'
\0
'
);
m_size += str.
length
() +
1
;
}
RefPtr<Writer> m_writer;
uintptr_t
m_offset;
uintptr_t
m_size;
};
void
ELFSection::populateHeader
(Writer::Slot<ELFSection::Header> header, ELFStringTable* strtab)
{
header->
name
=
static_cast
<
uint32_t
>(strtab->
add
(m_name));
header->
type
= m_type;
header->
alignment
= m_align;
populateHeader
(header);
}
#
endif
//
OS(LINUX)
#
if
OS(DARWIN)
class
MachO
{
WTF_DEPRECATED_MAKE_FAST_ALLOCATED
(MachO);
public:
size_t
addSection
(std::unique_ptr<MachOSection> section)
{
m_sections.
append
(
WTF::move
(section));
return
m_sections.
size
() -
1
;
}
void
write
(Ref<Writer> w,
const
CString& name,
uintptr_t
codeStart,
uintptr_t
)
{
Writer::Slot<MachOHeader> header =
writeHeader
(w);
uintptr_t
loadCommandStart = w->
position
();
Vector<Writer::Slot<MachOSegmentCommand>> segmentCommands;
Vector<Writer::Slot<MachOSection::Header>> sectionHeadersForSegments;
for
(
auto
& section : m_sections) {
segmentCommands.
append
(
writeSegmentCommand
(w,
reinterpret_cast
<
uintptr_t
>(section->
addr
()), section->
size
()));
sectionHeadersForSegments.
append
(w->
createSlotHere
<MachOSection::Header>());
}
auto
symtabCmd =
writeSymtabCommand
(w, name);
header->
sizeOfCommands
=
static_cast
<
uint32_t
>(w->
position
() - loadCommandStart);
for
(
unsigned
i =
0
; i < m_sections.
size
(); ++i) {
auto
segmentCmd = segmentCommands[i];
auto
sectionHeader = sectionHeadersForSegments[i];
segmentCmd->
fileOff
= w->
position
();
m_sections[i]->
populateHeader
(sectionHeader);
m_sections[i]->
writeBody
(sectionHeader, w);
segmentCmd->
fileSize
= w->
position
() - segmentCmd->
fileOff
;
segmentCmd->
vmSize
= sectionHeader->
size
;
}
writeNList
(w, symtabCmd, codeStart);
writeStringTable
(w, name, symtabCmd);
}
private:
struct
MachOHeader
{
uint32_t
magic;
uint32_t
cpuType;
uint32_t
cpuSubtype;
uint32_t
fileType;
uint32_t
numCommands;
uint32_t
sizeOfCommands;
uint32_t
flags;
uint32_t
reserved;
} __attribute__((packed,aligned(
1
)));
struct
MachOSegmentCommand
{
uint32_t
cmd;
uint32_t
cmdSize;
char
segname[
16
];
uint64_t
vmAddr;
uint64_t
vmSize;
uint64_t
fileOff;
uint64_t
fileSize;
uint32_t
maxProt;
uint32_t
initProt;
uint32_t
numSects;
uint32_t
flags;
} __attribute__((packed,aligned(
1
)));
struct
MachOSymtabCommand
{
uint32_t
cmd;
uint32_t
cmdSize;
uint32_t
symFileOff;
uint32_t
numSyms;
uint32_t
strFileOff;
uint32_t
stringBytes;
} __attribute__((packed,aligned(
1
)));
enum
MachOLoadCommandCmd {
LCSegment32 =
0x00000001u
,
LCSymTab =
0x00000002u
,
LCSegment64 =
0x00000019u
,
};
enum
NListType {
NAbs =
0x2
,
NSect =
0xe
,
};
enum
NListDescription {
ReferenceFlagDefined =
0x2
,
};
struct
__attribute__
((packed)) NList64 {
uint32_t
index;
uint8_t
type;
uint8_t
section;
uint16_t
desc;
uint64_t
value;
};
Writer::Slot<MachOHeader>
writeHeader
(Ref<Writer> writer)
{
ASSERT
(!writer->
position
());
Writer::Slot<MachOHeader> header = writer->
createSlotHere
<MachOHeader>();
#
if
CPU(ARM64)
header->
magic
=
0xFEEDFACFu
;
header->
cpuType
=
0x0000000C
|
0x01000000
;
//
ARM | 64-bit ABI
header->
cpuSubtype
=
0x00000000
;
//
All
header->
reserved
=
0
;
#
elif
CPU(X86_64)
header->
magic
=
0xFEEDFACFu
;
header->
cpuType
=
7
|
0x01000000
;
//
i386 | 64-bit ABI
header->
cpuSubtype
=
3
;
header->
reserved
=
0
;
#
else
#
error
Unsupported target architecture.
#
endif
header->
fileType
=
0x1
;
//
MH_OBJECT
header->
numCommands
=
3
;
header->
sizeOfCommands
=
0
;
header->
flags
=
0
;
return
header;
}
Writer::Slot<MachOSegmentCommand>
writeSegmentCommand
(Ref<Writer> writer,
uintptr_t
codeStart,
uintptr_t
codeSize)
{
auto
cmd = writer->
createSlotHere
<MachOSegmentCommand>();
cmd->
cmd
= LCSegment64;
cmd->
vmAddr
= codeStart;
cmd->
vmSize
= codeSize;
cmd->
fileOff
=
0
;
cmd->
fileSize
=
0
;
cmd->
maxProt
=
7
;
cmd->
initProt
=
7
;
cmd->
flags
=
0
;
cmd->
numSects
=
1
;
memset
(cmd->
segname
,
0
,
16
);
cmd->
cmdSize
=
sizeof
(MachOSegmentCommand) +
sizeof
(MachOSection::Header) * cmd->
numSects
;
return
cmd;
}
Writer::Slot<MachOSymtabCommand>
writeSymtabCommand
(Ref<Writer> writer,
const
CString& name)
{
auto
cmd = writer->
createSlotHere
<MachOSymtabCommand>();
cmd->
cmd
= LCSymTab;
cmd->
symFileOff
=
0
;
cmd->
numSyms
=
1
;
cmd->
strFileOff
=
0
;
cmd->
stringBytes
= name.
length
() +
1
;
cmd->
cmdSize
=
sizeof
(MachOSymtabCommand);
return
cmd;
}
void
writeNList
(Ref<Writer> writer, Writer::Slot<MachOSymtabCommand> cmd,
uintptr_t
codeStart)
{
cmd->
symFileOff
= writer->
position
();
auto
slot = writer->
createSlotHere
<NList64>();
slot->
index
=
1
;
slot->
type
= NSect;
slot->
section
=
2
;
slot->
desc
=
0
;
slot->
value
= codeStart;
}
void
writeStringTable
(Ref<Writer> writer,
const
CString& name, Writer::Slot<MachOSymtabCommand> cmd)
{
cmd->
strFileOff
= writer->
position
();
writer->
write
<
char
>(
'
\0
'
);
//
Index 0
for
(
auto
c : name.
span
())
writer->
write
<
char
>(c);
writer->
write
<
char
>(
'
\0
'
);
}
Vector<std::unique_ptr<MachOSection>> m_sections { };
};
#
endif
//
OS(DARWIN)
#
if
OS(LINUX)
class
ELF
{
public:
explicit
ELF
()
{
m_sections.
append
(
WTF
::makeUnique<ELFSection>(
"
"
, ELFSection::TypeNull,
0
));
m_sections.
append
(
WTF
::makeUnique<ELFStringTable>(
"
.shstrtab
"
));
}
void
write
(Ref<Writer> writer)
{
writeHeader
(writer);
writeSectionTable
(writer);
writeSections
(writer);
}
ELFSection*
SectionAt
(
uint32_t
index) {
return
m_sections[index].
get
(); }
size_t
addSection
(std::unique_ptr<ELFSection> section)
{
section->
setIndex
(m_sections.
size
());
m_sections.
append
(
WTF::move
(section));
return
m_sections.
size
() -
1
;
}
private:
struct
ELFHeader
{
uint8_t
ident[
16
];
uint16_t
type;
uint16_t
machine;
uint32_t
version;
uintptr_t
entry;
uintptr_t
phtOffset;
uintptr_t
shtOffset;
uint32_t
flags;
uint16_t
headerSize;
uint16_t
phtEntrySize;
uint16_t
phtEntryNum;
uint16_t
shtEntrySize;
uint16_t
shtEntryNum;
uint16_t
shtStrTabIndex;
} __attribute__((packed,aligned(
1
)));
void
writeHeader
(Ref<Writer> writer)
{
ASSERT
(!writer->
position
());
Writer::Slot<ELFHeader> header = writer->
createSlotHere
<ELFHeader>();
#
if
CPU(X86_64) || CPU(ARM64) || CPU(RISCV64)
const
uint8_t
ident[
16
] = {
0x7F
,
'
E
'
,
'
L
'
,
'
F
'
,
2
,
1
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
};
#
else
#
error
Unsupported target architecture.
#
endif
memcpy
(header->
ident
, ident,
16
);
header->
type
=
1
;
#
if
CPU(X86_64)
//
Processor identification value for x64 is 62 as defined in
//
System V ABI, AMD64 Supplement
//
http://www.x86-64.org/documentation/abi.pdf
header->
machine
=
62
;
#
elif
CPU(ARM64)
//
AARCH64
header->
machine
=
0xB7
;
#
elif
CPU(RISCV64)
//
RISC-V 64
header->
machine
=
0xF3
;
#
else
#
error
Unsupported target architecture.
#
endif
header->
version
=
1
;
header->
entry
=
0
;
header->
phtOffset
=
0
;
header->
shtOffset
=
sizeof
(ELFHeader);
//
Section table follows header.
header->
flags
=
0
;
header->
headerSize
=
sizeof
(ELFHeader);
header->
phtEntrySize
=
0
;
header->
phtEntryNum
=
0
;
header->
shtEntrySize
=
sizeof
(ELFSection::Header);
header->
shtEntryNum
= m_sections.
size
();
header->
shtStrTabIndex
=
1
;
}
void
writeSectionTable
(Ref<Writer> writer)
{
//
Section headers table immediately follows file header.
ASSERT
(writer->
position
() ==
sizeof
(ELFHeader));
Writer::Slot<ELFSection::Header> headers = writer->
createSlotsHere
<ELFSection::Header>(
static_cast
<
uint32_t
>(m_sections.
size
()));
//
String table for section table is the first section.
ELFStringTable* strtab =
static_cast
<ELFStringTable*>(
SectionAt
(
1
));
ASSERT
(strtab->
m_type
== ELFSection::TypeStrTab);
strtab->
attachWriter
(writer);
uint32_t
index =
0
;
for
(
auto
& section : m_sections) {
section->
populateHeader
(headers.
at
(index), strtab);
index++;
}
strtab->
detachWriter
();
}
void
writeSections
(Ref<Writer> writer)
{
Writer::Slot<ELFSection::Header> headers = writer->
slotAt
<ELFSection::Header>(
sizeof
(ELFHeader));
uint32_t
index =
0
;
for
(
auto
& section : m_sections) {
section->
writeBody
(headers.
at
(index), writer);
index++;
}
}
Vector<std::unique_ptr<ELFSection>> m_sections;
};
class
ELFSymbol
{
public:
enum
Type {
TypeNone =
0
,
TypeObject =
1
,
TypeFunction =
2
,
TypeSection =
3
,
TypeFile =
4
,
TypeLoProc =
13
,
TypeHiProc =
15
};
enum
Binding {
BindLocal =
0
,
BindGlobal =
1
,
BindWeak =
2
,
BindLoProc =
13
,
BindHiProc =
15
};
ELFSymbol
(
const
CString& name,
uintptr_t
value,
uintptr_t
size, Binding binding, Type type,
uint16_t
section)
: m_name(name)
, m_value(value)
, m_size(size)
, m_info((binding <<
4
) | type)
, m_other(
0
)
, m_section(section)
{
}
Binding
binding
()
const
{
return
static_cast
<Binding>(m_info >>
4
); }
#
if
CPU(X86_64) || CPU(ARM64) || CPU(RISCV64)
struct
SerializedLayout
{
SerializedLayout
(
uint32_t
name,
uintptr_t
value,
uintptr_t
size, Binding binding, Type type,
uint16_t
section)
: m_name(name)
, m_info((binding <<
4
) | type)
, m_other(
0
)
, m_section(section)
, m_value(value)
, m_size(size)
{
}
uint32_t
m_name;
uint8_t
m_info;
uint8_t
m_other;
uint16_t
m_section;
uintptr_t
m_value;
uintptr_t
m_size;
} __attribute__((packed,aligned(
1
)));
#
endif
void
write
(Writer::Slot<SerializedLayout> slot, ELFStringTable* table)
const
{
//
Convert symbol names from strings to indexes in the string table.
slot->
m_name
=
static_cast
<
uint32_t
>(table->
add
(m_name));
slot->
m_value
= m_value;
slot->
m_size
= m_size;
slot->
m_info
= m_info;
slot->
m_other
= m_other;
slot->
m_section
= m_section;
}
private:
CString m_name;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL