-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathocaml-cross-4_14_2-a02-arm32.patch
913 lines (877 loc) · 34.7 KB
/
ocaml-cross-4_14_2-a02-arm32.patch
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
diff --git a/Makefile.config.in b/Makefile.config.in
index 377619f..b6b43f2 100644
--- a/Makefile.config.in
+++ b/Makefile.config.in
@@ -229,6 +229,7 @@ WITH_PROFINFO=@profinfo@
PROFINFO_WIDTH=@profinfo_width@
WITH_FPIC=@fpic@
TARGET=@target@
+TARGET_OS=@target_os@
HOST=@host@
FLAMBDA=@flambda@
WITH_FLAMBDA_INVARIANTS=@flambda_invariants@
diff --git a/asmcomp/arm/emit.mlp b/asmcomp/arm/emit.mlp
index e92e6c3..16645da 100644
--- a/asmcomp/arm/emit.mlp
+++ b/asmcomp/arm/emit.mlp
@@ -300,7 +300,7 @@ let emit_literals env =
` .align 2\n`;
List.iter
(fun { lbl_got; lbl_pic } ->
- `{emit_label lbl_pic}: .word _GLOBAL_OFFSET_TABLE_-({emit_label lbl_got}+{emit_int offset})\n`)
+ `{emit_label lbl_got}: .word _GLOBAL_OFFSET_TABLE_-({emit_label lbl_pic}+{emit_int offset})\n`)
env.gotrel_literals;
List.iter
(fun { sym; lbl } ->
@@ -959,13 +959,26 @@ let emit_item = function
let data l =
` .data\n`;
- List.iter emit_item l
+ List.iter emit_item l;
+ (* If data block is exclusively for one global symbol, emit ELF object info *)
+ match l with
+ | Cglobal_symbol s :: Cdefine_symbol s_ :: tl when s=s_ ->
+ if
+ List.exists
+ (function | Cglobal_symbol _ | Cdefine_symbol _ -> true | _ -> false)
+ tl
+ then
+ ()
+ else
+ ` .type {emit_symbol s}, %object\n`;
+ ` .size {emit_symbol s}, .-{emit_symbol s}\n`
+ | _ -> ()
(* Beginning / end of an assembly file *)
let begin_assembly() =
reset_debug_info();
- ` .file \"\"\n`; (* PR#7037 *)
+ ` .file\t1\t\"\"\n`; (* PR#7037 *)
` .syntax unified\n`;
begin match !arch with
| ARMv4 -> ` .arch armv4t\n`
diff --git a/asmcomp/arm64/emit.mlp b/asmcomp/arm64/emit.mlp
index d3012f3..087c140 100644
--- a/asmcomp/arm64/emit.mlp
+++ b/asmcomp/arm64/emit.mlp
@@ -1069,7 +1069,7 @@ let data l =
let begin_assembly() =
reset_debug_info();
- ` .file \"\"\n`; (* PR#7037 *)
+ ` .file\t1\t\"\"\n`; (* PR#7037 *)
let lbl_begin = Compilenv.make_symbol (Some "data_begin") in
` .data\n`;
` .globl {emit_symbol lbl_begin}\n`;
diff --git a/asmcomp/emitaux.ml b/asmcomp/emitaux.ml
index 0dca6a9..991ad3a 100644
--- a/asmcomp/emitaux.ml
+++ b/asmcomp/emitaux.ml
@@ -339,7 +339,14 @@ let file_pos_num_cnt = ref 1
(* Reset debug state at beginning of asm file *)
let reset_debug_info () =
file_pos_nums := [];
- file_pos_num_cnt := 1
+ (* Need to start at file number 2 so that GNU AS (GNU assembler (GNU Binutils) 2.27.0.20170315) with -g option
+ does not fail with: `/tmp/camlasm8bd07a.s:1105: Error: file number 1 already allocated` where .s is:
+ .file ""
+ /* ... */
+ camlStdlib__failwith_6:
+ .file 1 "stdlib.ml"
+ *)
+ file_pos_num_cnt := 2
let reset_debug_info_at_file_num file_num =
file_pos_nums := [];
diff --git a/asmcomp/emitaux.mli b/asmcomp/emitaux.mli
index 34d1ac1..400447c 100644
--- a/asmcomp/emitaux.mli
+++ b/asmcomp/emitaux.mli
@@ -31,6 +31,14 @@ val emit_float64_split_directive: string -> int64 -> unit
val emit_float32_directive: string -> int32 -> unit
val reset : unit -> unit
+
+(** [reset_debug_info ()] will reset the debug information and reserve DWARF2
+ [fileno = 1].
+
+ The caller is responsible for emitting an empty file directive
+ [(.file 1 "")] for a temporary assembly source file or
+ [(.file 1 "/some/assembly/code/in/non-temporary/location.s")]
+ otherwise. *)
val reset_debug_info: unit -> unit
val reset_debug_info_at_file_num: int -> unit
val emit_debug_info: Debuginfo.t -> unit
diff --git a/asmcomp/power/emit.mlp b/asmcomp/power/emit.mlp
index 28d9023..b472277 100644
--- a/asmcomp/power/emit.mlp
+++ b/asmcomp/power/emit.mlp
@@ -1188,7 +1188,7 @@ let data l =
let begin_assembly() =
reset_debug_info();
- ` .file \"\"\n`; (* PR#7037 *)
+ ` .file\t1\t\"\"\n`; (* PR#7037 *)
begin match abi with
| ELF64v2 -> ` .abiversion 2\n`
| _ -> ()
diff --git a/asmcomp/riscv/emit.mlp b/asmcomp/riscv/emit.mlp
index b36aa0e..497fc87 100644
--- a/asmcomp/riscv/emit.mlp
+++ b/asmcomp/riscv/emit.mlp
@@ -620,7 +620,7 @@ let data l =
let begin_assembly() =
if !Clflags.dlcode || !Clflags.pic_code then ` .option pic\n`;
- ` .file \"\"\n`; (* PR#7073 *)
+ ` .file\t1\t\"\"\n`; (* PR#7073 *)
reset_debug_info ();
(* Emit the beginning of the segments *)
let lbl_begin = Compilenv.make_symbol (Some "data_begin") in
diff --git a/asmcomp/s390x/emit.mlp b/asmcomp/s390x/emit.mlp
index 5b2e593..3cab6cf 100644
--- a/asmcomp/s390x/emit.mlp
+++ b/asmcomp/s390x/emit.mlp
@@ -726,7 +726,7 @@ let data l =
let begin_assembly() =
reset_debug_info();
- ` .file \"\"\n`; (* PR#7037 *)
+ ` .file\t1\t\"\"\n`; (* PR#7037 *)
(* Emit the beginning of the segments *)
let lbl_begin = Compilenv.make_symbol (Some "data_begin") in
emit_string data_space;
diff --git a/runtime/arm.S b/runtime/arm.S
index 612757a..ee06152 100644
--- a/runtime/arm.S
+++ b/runtime/arm.S
@@ -129,7 +129,9 @@ caml_hot__code_end:
#include "../runtime/caml/domain_state.tbl"
#undef DOMAIN_STATE
-#define Caml_state(var) [domain_state_ptr, 8*domain_field_caml_##var]
+/* Caml_state_var(var) is macro to get variable within Caml_state struct.
+ Caml_state(GOT_PREL), etc. are compiler expressions for the address of the Caml_state symbol. */
+#define Caml_state_var(var) [domain_state_ptr, 8*domain_field_caml_##var]
/* Allocation functions and GC interface */
TEXT_SECTION(caml_system__code_begin)
@@ -140,9 +142,9 @@ FUNCTION(caml_call_gc)
CFI_STARTPROC
.Lcaml_call_gc:
/* Record return address */
- str lr, Caml_state(last_return_address)
+ str lr, Caml_state_var(last_return_address)
/* Record lowest stack address */
- str sp, Caml_state(bottom_of_stack)
+ str sp, Caml_state_var(bottom_of_stack)
#if defined(SYS_linux_eabihf) || defined(SYS_netbsd)
/* Save caller floating-point registers on the stack */
vpush {d0-d7}; CFI_ADJUST(64)
@@ -155,11 +157,11 @@ FUNCTION(caml_call_gc)
CFI_OFFSET(lr, -4)
#endif
/* Store pointer to saved integer registers in Caml_state->gc_regs */
- str sp, Caml_state(gc_regs)
+ str sp, Caml_state_var(gc_regs)
/* Save current allocation pointer for debugging purposes */
- str alloc_ptr, Caml_state(young_ptr)
+ str alloc_ptr, Caml_state_var(young_ptr)
/* Save trap pointer in case an exception is raised during GC */
- str trap_ptr, Caml_state(exception_pointer)
+ str trap_ptr, Caml_state_var(exception_pointer)
/* Call the garbage collector */
bl caml_garbage_collection
/* Restore integer registers and return address from the stack */
@@ -169,7 +171,7 @@ FUNCTION(caml_call_gc)
vpop {d0-d7}; CFI_ADJUST(-64)
#endif
/* Reload new allocation pointer */
- ldr alloc_ptr, Caml_state(young_ptr)
+ ldr alloc_ptr, Caml_state_var(young_ptr)
/* Return to caller */
bx lr
CFI_ENDPROC
@@ -178,7 +180,7 @@ FUNCTION(caml_call_gc)
FUNCTION(caml_alloc1)
CFI_STARTPROC
sub alloc_ptr, alloc_ptr, 8
- ldr r7, Caml_state(young_limit)
+ ldr r7, Caml_state_var(young_limit)
cmp alloc_ptr, r7
bcc .Lcaml_call_gc
bx lr
@@ -188,7 +190,7 @@ FUNCTION(caml_alloc1)
FUNCTION(caml_alloc2)
CFI_STARTPROC
sub alloc_ptr, alloc_ptr, 12
- ldr r7, Caml_state(young_limit)
+ ldr r7, Caml_state_var(young_limit)
cmp alloc_ptr, r7
bcc .Lcaml_call_gc
bx lr
@@ -198,7 +200,7 @@ FUNCTION(caml_alloc2)
FUNCTION(caml_alloc3)
CFI_STARTPROC
sub alloc_ptr, alloc_ptr, 16
- ldr r7, Caml_state(young_limit)
+ ldr r7, Caml_state_var(young_limit)
cmp alloc_ptr, r7
bcc .Lcaml_call_gc
bx lr
@@ -208,7 +210,7 @@ FUNCTION(caml_alloc3)
FUNCTION(caml_allocN)
CFI_STARTPROC
sub alloc_ptr, alloc_ptr, r7
- ldr r7, Caml_state(young_limit)
+ ldr r7, Caml_state_var(young_limit)
cmp alloc_ptr, r7
bcc .Lcaml_call_gc
bx lr
@@ -216,39 +218,59 @@ FUNCTION(caml_allocN)
.size caml_allocN, .-caml_allocN
/* Call a C function from OCaml */
-/* Function to call is in r7 */
-
-FUNCTION(caml_c_call)
- CFI_STARTPROC
+/* Function to call is in r7 for non-PIC code, or is a
+ named PLT function for PIC code */
+/* A PLT function address is only available when "cited by a BL-class relocation directive" like `bl func(PLT)`.
+ We can't, for example, do `adr r7, func(PLT)` and then jump to caml_c_call or Ljump_to_caml.
+ So implemented as a macro that inserts the instructions inline and branch jumps (branch_op = bl or blx)
+ to (c_code_addr or caml_code_funcptr). Use blx for r7/r12 register and bl for named PLT functions.
+ Warning: Unlike blx the bl instruction does not switch to/from Thumb state since "it is generally pointless trying to construct a PLT entry
+ entirely in 16-bit Thumb instructions". In other words: Do not emit Thumb PLT entries for PIC code!
+ Confer https://github.com/ARM-software/abi-aa/blob/320a56971fdcba282b7001cf4b84abb4fd993131/aaelf32/aaelf32.rst#plt-sequences-and-usage-models .
+*/
+
+ .macro inline_caml_c_call branch_op c_code_addr
/* Record lowest stack address and return address */
- str lr, Caml_state(last_return_address)
- str sp, Caml_state(bottom_of_stack)
+ str lr, Caml_state_var(last_return_address)
+ str sp, Caml_state_var(bottom_of_stack)
/* Preserve return address in callee-save register r4 */
mov r4, lr
CFI_REGISTER(lr, r4)
/* Make the exception handler alloc ptr available to the C code */
- str alloc_ptr, Caml_state(young_ptr)
- str trap_ptr, Caml_state(exception_pointer)
+ str alloc_ptr, Caml_state_var(young_ptr)
+ str trap_ptr, Caml_state_var(exception_pointer)
/* Call the function */
- blx r7
+ \branch_op \c_code_addr
/* Reload alloc ptr */
- ldr alloc_ptr, Caml_state(young_ptr)
+ ldr alloc_ptr, Caml_state_var(young_ptr)
/* Return */
bx r4
+ .endm
+
+FUNCTION(caml_c_call)
+ CFI_STARTPROC
+ inline_caml_c_call blx r7
CFI_ENDPROC
.size caml_c_call, .-caml_c_call
/* Start the OCaml program */
-FUNCTION(caml_start_program)
- CFI_STARTPROC
- ldr r12, =caml_program
-
/* Code shared with caml_callback* */
-/* Address of OCaml code to call is in r12 */
/* Arguments to the OCaml code are in r0...r3 */
-
-.Ljump_to_caml:
+/* Instructions for using call_caml_XXXX:
+ 1. (optional) set r12
+ 2. `call_caml_enter`
+ 3. `blx some_function(PLT)` ...or if you preset r12... `bx r12`
+ 4. `call_caml_exit`
+
+ At end inside the CFI PROC do `call_caml_relocs`.
+ Why? Linker will do PIC relocations in this word space.
+
+ Add `call_caml_framedescr` in the PIC portion of caml_system__frametable.
+ And increment caml_system__frametable.num_descriptors.
+ Why? All callsite return addresses have to be visible to GC.
+*/
+ .macro call_caml_enter callsite_label:req
#if defined(SYS_linux_eabihf) || defined(SYS_netbsd)
/* Save callee-save floating-point registers */
vpush {d8-d15}; CFI_ADJUST(64)
@@ -260,43 +282,57 @@ FUNCTION(caml_start_program)
#else
CFI_OFFSET(lr, -4)
#endif
+#ifdef __PIC__
+ /* Read the PREL word in the local .text section, which is the PC-relative address of
+ Caml_state's GOT entry */
+ ldr.w domain_state_ptr, \callsite_label\()_isp
+ /* Label for PC of next add instruction */
+\callsite_label\()_pcnai: /* `.Lxyz_pcnai:` if callsite_label=`.Lxyz` */
+ add.w domain_state_ptr, pc
+ /* Load the entry from the GOT; the address of Caml_state variable */
+ ldr domain_state_ptr, [domain_state_ptr]
+#else
+ /* Load the address of Caml_state variable */
ldr domain_state_ptr, =Caml_state
+#endif
+ /* Load the Caml_state pointer */
ldr domain_state_ptr, [domain_state_ptr]
/* Setup a callback link on the stack */
sub sp, sp, 16; CFI_ADJUST(16) /* 8-byte alignment */
- ldr r4, Caml_state(bottom_of_stack)
- ldr r5, Caml_state(last_return_address)
- ldr r6, Caml_state(gc_regs)
+ ldr r4, Caml_state_var(bottom_of_stack)
+ ldr r5, Caml_state_var(last_return_address)
+ ldr r6, Caml_state_var(gc_regs)
str r4, [sp, 0]
str r5, [sp, 4]
str r6, [sp, 8]
/* Setup a trap frame to catch exceptions escaping the OCaml code */
sub sp, sp, 8; CFI_ADJUST(8)
- ldr r5, =.Ltrap_handler
- ldr r4, Caml_state(exception_pointer)
+ adr r5, .Ltrap_handler
+ ldr r4, Caml_state_var(exception_pointer)
str r4, [sp, 0]
str r5, [sp, 4]
mov trap_ptr, sp
/* Reload allocation pointer */
- ldr alloc_ptr, Caml_state(young_ptr)
- /* Call the OCaml code */
- blx r12
-.Lcaml_retaddr:
+ ldr alloc_ptr, Caml_state_var(young_ptr)
+ .endm
+
+ .macro call_caml_exit retaddr_label:req return_result_label:req
+\retaddr_label:
/* Pop the trap frame, restoring caml_exception_pointer */
ldr r5, [sp, 0]
- str r5, Caml_state(exception_pointer)
+ str r5, Caml_state_var(exception_pointer)
add sp, sp, 8; CFI_ADJUST(-8)
/* Pop the callback link, restoring the global variables */
-.Lreturn_result:
+\return_result_label:
ldr r5, [sp, 0]
- str r5, Caml_state(bottom_of_stack)
+ str r5, Caml_state_var(bottom_of_stack)
ldr r5, [sp, 4]
- str r5, Caml_state(last_return_address)
+ str r5, Caml_state_var(last_return_address)
ldr r5, [sp, 8]
- str r5, Caml_state(gc_regs)
+ str r5, Caml_state_var(gc_regs)
add sp, sp, 16; CFI_ADJUST(-16)
/* Update allocation pointer */
- str alloc_ptr, Caml_state(young_ptr)
+ str alloc_ptr, Caml_state_var(young_ptr)
/* Reload callee-save registers and return address */
pop {r4-r8,r10,r11,lr}; CFI_ADJUST(-32)
#if defined(SYS_linux_eabihf) || defined(SYS_netbsd)
@@ -304,6 +340,44 @@ FUNCTION(caml_start_program)
vpop {d8-d15}; CFI_ADJUST(-64)
#endif
bx lr
+ .endm
+
+ .macro call_caml_relocs callsite_label:req
+#ifdef __PIC__
+ .align 2
+ /* Local indirect symbol pointer for \label that will be linked statically with
+ the correct GOT math. */
+\callsite_label\()_isp: /* `.Lxyz_isp:` if callsite_label=`.Lxyz` */
+ /* 8 = opcode_size( ldr.w ; \callsite_label\()_pcnai: adr.w ) */
+ .word Caml_state(GOT_PREL) - ((\callsite_label\()_pcnai + 8) - \callsite_label\()_isp)
+#endif
+ .endm
+
+ .macro call_caml_framedescr retaddr_label:req
+#ifdef __PIC__
+ .word \retaddr_label /* return address into callback */
+ .short 0 /* zero frame size */
+ .short 0 /* no roots */
+ .align 2
+#endif
+ .endm
+
+FUNCTION(caml_start_program)
+ CFI_STARTPROC
+
+#ifdef __PIC__
+ call_caml_enter .Lcaml_start_program
+ bl caml_program(PLT)
+#else
+ ldr r12, =caml_program
+
+/* Address of OCaml code to call is in r12 */
+.Ljump_to_caml:
+ call_caml_enter .Lcaml_start_program
+ blx r12
+#endif
+ call_caml_exit .Lcaml_retaddr, .Lreturn_result
+ call_caml_relocs .Lcaml_start_program
CFI_ENDPROC
.type .Lcaml_retaddr, %function
.size .Lcaml_retaddr, .-.Lcaml_retaddr
@@ -315,7 +389,7 @@ FUNCTION(caml_start_program)
.Ltrap_handler:
CFI_STARTPROC
/* Save exception pointer */
- str trap_ptr, Caml_state(exception_pointer)
+ str trap_ptr, Caml_state_var(exception_pointer)
/* Encode exception bucket as an exception result */
orr r0, r0, 2
/* Return it */
@@ -329,7 +403,7 @@ FUNCTION(caml_start_program)
FUNCTION(caml_raise_exn)
CFI_STARTPROC
/* Test if backtrace is active */
- ldr r1, Caml_state(backtrace_active)
+ ldr r1, Caml_state_var(backtrace_active)
cbz r1, 1f
/* Preserve exception bucket in callee-save register r4 */
mov r4, r0
@@ -356,15 +430,15 @@ FUNCTION(caml_raise_exception)
/* Load exception bucket */
mov r0, r1
/* Reload trap ptr and alloc ptr */
- ldr trap_ptr, Caml_state(exception_pointer)
- ldr alloc_ptr, Caml_state(young_ptr)
+ ldr trap_ptr, Caml_state_var(exception_pointer)
+ ldr alloc_ptr, Caml_state_var(young_ptr)
/* Test if backtrace is active */
- ldr r1, Caml_state(backtrace_active)
+ ldr r1, Caml_state_var(backtrace_active)
cbz r1, 1f
/* Preserve exception bucket in callee-save register r4 */
mov r4, r0
- ldr r1, Caml_state(last_return_address) /* arg2: pc of raise */
- ldr r2, Caml_state(bottom_of_stack) /* arg3: sp of raise */
+ ldr r1, Caml_state_var(last_return_address) /* arg2: pc of raise */
+ ldr r2, Caml_state_var(bottom_of_stack) /* arg3: sp of raise */
mov r3, trap_ptr /* arg4: sp of handler */
bl caml_stash_backtrace
/* Restore exception bucket */
@@ -385,9 +459,20 @@ FUNCTION(caml_callback_asm)
ldr r0, [r2] /* r0 = first arg */
/* r1 = closure environment */
ldr r12, [r1] /* code pointer */
+#ifdef __PIC__
+ call_caml_enter .Lcaml_callback_asm
+ blx r12
+#else
b .Ljump_to_caml
+#endif
+ call_caml_exit .Lcaml_callback_asm_retaddr, .Lcaml_callback_asm_return_result
+ call_caml_relocs .Lcaml_callback_asm
CFI_ENDPROC
.size caml_callback_asm, .-caml_callback_asm
+#ifdef __PIC__
+ .type .Lcaml_callback_asm_retaddr, %function
+ .size .Lcaml_callback_asm_retaddr, .-.Lcaml_callback_asm_retaddr
+#endif
FUNCTION(caml_callback2_asm)
CFI_STARTPROC
@@ -397,10 +482,21 @@ FUNCTION(caml_callback2_asm)
ldr r0, [r2] /* r0 = first arg */
ldr r1, [r2,4] /* r1 = second arg */
mov r2, r12 /* r2 = closure environment */
+#ifdef __PIC__
+ call_caml_enter .Lcaml_callback2_asm
+ bl caml_apply2(PLT)
+#else
ldr r12, =caml_apply2
b .Ljump_to_caml
+#endif
+ call_caml_exit .Lcaml_callback2_asm_retaddr, .Lcaml_callback2_asm_return_result
+ call_caml_relocs .Lcaml_callback2_asm
CFI_ENDPROC
.size caml_callback2_asm, .-caml_callback2_asm
+#ifdef __PIC__
+ .type .Lcaml_callback2_asm_retaddr, %function
+ .size .Lcaml_callback2_asm_retaddr, .-.Lcaml2_callback_asm_retaddr
+#endif
FUNCTION(caml_callback3_asm)
CFI_STARTPROC
@@ -411,17 +507,32 @@ FUNCTION(caml_callback3_asm)
ldr r0, [r2] /* r0 = first arg */
ldr r1, [r2,4] /* r1 = second arg */
ldr r2, [r2,8] /* r2 = third arg */
+#ifdef __PIC__
+ call_caml_enter .Lcaml_callback3_asm
+ bl caml_apply3(PLT)
+#else
ldr r12, =caml_apply3
b .Ljump_to_caml
+#endif
+ call_caml_exit .Lcaml_callback3_asm_retaddr, .Lcaml_callback3_asm_return_result
+ call_caml_relocs .Lcaml_callback3_asm
CFI_ENDPROC
.size caml_callback3_asm, .-caml_callback3_asm
+#ifdef __PIC__
+ .type .Lcaml_callback3_asm_retaddr, %function
+ .size .Lcaml_callback3_asm_retaddr, .-.Lcaml3_callback_asm_retaddr
+#endif
FUNCTION(caml_ml_array_bound_error)
CFI_STARTPROC
+#ifdef __PIC__
+ inline_caml_c_call bl caml_array_bound_error(PLT)
+#else
/* Load address of [caml_array_bound_error] in r7 */
- ldr r7, =caml_array_bound_error
+ ldr r7, =caml_array_bound_error
/* Call that function */
b caml_c_call
+#endif
CFI_ENDPROC
.size caml_ml_array_bound_error, .-caml_ml_array_bound_error
@@ -435,7 +546,14 @@ caml_system__code_end:
.align 2
.globl caml_system__frametable
caml_system__frametable:
- .word 1 /* one descriptor */
+#ifdef __PIC__
+ .word 4 /* num_descriptors including .Lcaml_retaddr */
+ call_caml_framedescr .Lcaml_callback_asm_retaddr
+ call_caml_framedescr .Lcaml_callback2_asm_retaddr
+ call_caml_framedescr .Lcaml_callback3_asm_retaddr
+#else
+ .word 1 /* one descriptor (only .Lcaml_retaddr) */
+#endif
.word .Lcaml_retaddr /* return address into callback */
.short -1 /* negative frame size => use callback link */
.short 0 /* no roots */
diff --git a/runtime/i386.S b/runtime/i386.S
index 4d273c9..67aaa7b 100644
--- a/runtime/i386.S
+++ b/runtime/i386.S
@@ -78,6 +78,34 @@
#define CFI_ADJUST(n)
#endif
+/* PIC-friendly macro to get the address of the global variable Caml_state and set it in a register. */
+#ifdef __PIC__
+#define MOV_CAML_STATE_PTR(label, reg) \
+ call mov_caml_state_##label /* Push IP register */ ; \
+mov_caml_state_##label: \
+ popl ##reg ; \
+ addl $_GLOBAL_OFFSET_TABLE_, ##reg ; \
+ movl Caml_state@GOT(##reg), ##reg
+#else
+#define MOV_CAML_STATE_PTR(label, reg) \
+ movl G(Caml_state), ##reg
+#endif
+
+/* PIC-friendly macro to call a global function using PLT if PIC */
+ .macro call_global_func name,label
+#ifdef __PIC__
+ pushl %ebx /* Protect ebx */
+ call call_\label /* Push IP register */
+call_\label:
+ popl %ebx /* Pop IP register */
+ addl $_GLOBAL_OFFSET_TABLE_, %ebx /* ebx=GOT on PLT entry */
+ call \name@PLT
+ popl %ebx
+#else
+ call G(\name)
+#endif
+ .endm
+
#if !defined(SYS_mingw) && !defined(SYS_cygwin)
#define STACK_PROBE_SIZE 16384
#endif
@@ -116,7 +144,7 @@ FUNCTION(caml_call_gc)
CFI_STARTPROC
LBL(caml_call_gc):
/* Record lowest stack address and return address */
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_call_gc, %ebx)
movl (%esp), %eax
movl %eax, CAML_STATE(last_return_address, %ebx)
leal 4(%esp), %eax
@@ -139,7 +167,7 @@ LBL(caml_call_gc):
movl %esp, CAML_STATE(gc_regs, %ebx)
/* MacOSX note: 16-alignment of stack preserved at this point */
/* Call the garbage collector */
- call G(caml_garbage_collection)
+ call_global_func caml_garbage_collection, l_caml_call_gc
/* Restore all regs used by the code generator */
popl %eax; CFI_ADJUST(-4)
popl %ebx; CFI_ADJUST(-4)
@@ -156,7 +184,7 @@ LBL(caml_call_gc):
FUNCTION(caml_alloc1)
CFI_STARTPROC
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_alloc1, %ebx)
movl CAML_STATE(young_ptr, %ebx), %eax
subl $8, %eax
movl %eax, CAML_STATE(young_ptr, %ebx)
@@ -168,7 +196,7 @@ FUNCTION(caml_alloc1)
FUNCTION(caml_alloc2)
CFI_STARTPROC
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_alloc2, %ebx)
movl CAML_STATE(young_ptr, %ebx), %eax
subl $12, %eax
movl %eax, CAML_STATE(young_ptr, %ebx)
@@ -180,7 +208,7 @@ FUNCTION(caml_alloc2)
FUNCTION(caml_alloc3)
CFI_STARTPROC
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_alloc3, %ebx)
movl CAML_STATE(young_ptr, %ebx), %eax
subl $16, %eax
movl %eax, CAML_STATE(young_ptr, %ebx)
@@ -192,7 +220,7 @@ FUNCTION(caml_alloc3)
FUNCTION(caml_allocN)
CFI_STARTPROC
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_allocN, %ebx)
/* eax = size - Caml_state->young_ptr */
subl CAML_STATE(young_ptr, %ebx), %eax
negl %eax /* eax = Caml_state->young_ptr - size */
@@ -209,7 +237,7 @@ FUNCTION(caml_c_call)
CFI_STARTPROC
/* Record lowest stack address and return address */
/* ecx and edx are destroyed at C call. Use them as temp. */
- movl G(Caml_state), %ecx
+ MOV_CAML_STATE_PTR(l_caml_c_call, %ecx)
movl (%esp), %edx
movl %edx, CAML_STATE(last_return_address, %ecx)
leal 4(%esp), %edx
@@ -228,37 +256,76 @@ FUNCTION(caml_c_call)
/* Start the OCaml program */
-FUNCTION(caml_start_program)
- CFI_STARTPROC
- /* Save callee-save registers */
- pushl %ebx; CFI_ADJUST(4)
- pushl %esi; CFI_ADJUST(4)
- pushl %edi; CFI_ADJUST(4)
- pushl %ebp; CFI_ADJUST(4)
- /* Initial entry point is caml_program */
- movl $ G(caml_program), %esi
- /* Common code for caml_start_program and caml_callback* */
-LBL(106):
- movl G(Caml_state), %edi
+ /* Common code for caml_start_program and caml_callback* */
+ .macro inline_jump_to_caml_header label
+ MOV_CAML_STATE_PTR(\label, %edi)
/* Build a callback link */
pushl CAML_STATE(gc_regs, %edi); CFI_ADJUST(4)
pushl CAML_STATE(last_return_address, %edi); CFI_ADJUST(4)
pushl CAML_STATE(bottom_of_stack, %edi); CFI_ADJUST(4)
/* Note: 16-alignment preserved on MacOSX at this point */
/* Build an exception handler */
+#ifdef __PIC__
+ pushl %ebx /* Push C = orig(ebx) */
+ sub $ 4, %esp /* Push B = <tmp> */
+ call inline_jump_to_caml_exception_\label /* Push IP register */
+inline_jump_to_caml_exception_\label:
+ popl %ebx /* Pop IP register */
+ addl $_GLOBAL_OFFSET_TABLE_, %ebx
+ movl LBL(108)@GOT(%ebx), %ebx /* ebx = $ LBL(108) */
+ pushl %ebx /* Push A = $ LBL(108) */
+ /* Stack = A, B, C */
+ movl 8(%esp), %ebx
+ movl %ebx, 4(%esp) /* Stack = A, C, C */
+ movl 0(%esp), %ebx
+ movl %ebx, 8(%esp) /* Stack = A, C, A */
+ movl 4(%esp), %ebx /* ebx = C = orig(ebx) */
+ add $ 8, %esp /* Stack = A = $ LBL(108) */
+#else
pushl $ LBL(108); CFI_ADJUST(4)
+#endif
ALIGN_STACK(8)
pushl CAML_STATE(exception_pointer, %edi); CFI_ADJUST(4)
movl %esp, CAML_STATE(exception_pointer, %edi)
+ .endm
+
+ .macro inline_jump_to_caml_plt label caml_code_funcptr
+ /* Call the OCaml code */
+ pushl %ebx /* Protect ebx */
+ call inline_jump_to_caml_plt_\label /* Push IP register */
+inline_jump_to_caml_plt_\label:
+ popl %ebx /* Pop IP register */
+ addl $_GLOBAL_OFFSET_TABLE_, %ebx /* ebx=GOT on PLT entry */
+ call \caml_code_funcptr@PLT
+ popl %ebx
+ .endm
+
+FUNCTION(caml_start_program)
+ CFI_STARTPROC
+ /* Save callee-save registers */
+ pushl %ebx; CFI_ADJUST(4)
+ pushl %esi; CFI_ADJUST(4)
+ pushl %edi; CFI_ADJUST(4)
+ pushl %ebp; CFI_ADJUST(4)
+ /* Initial entry point is caml_program */
+#ifdef __PIC__
+ inline_jump_to_caml_header caml_start_program_initial
+ inline_jump_to_caml_plt caml_start_program_initial caml_program
+#else
+ movl $ G(caml_program), %esi
+#endif
+ /* Regardless of PIC status, this local label will be used by caml_callback_asm() */
+LBL(106):
+ inline_jump_to_caml_header caml_start_program_106
/* Call the OCaml code */
call *%esi
LBL(107):
- movl G(Caml_state), %edi
+ MOV_CAML_STATE_PTR(l_caml_start_program_107, %edi)
/* Pop the exception handler */
popl CAML_STATE(exception_pointer, %edi); CFI_ADJUST(-4)
addl $12, %esp ; CFI_ADJUST(-12)
LBL(109):
- movl G(Caml_state), %edi /* Reload for LBL(109) entry */
+ MOV_CAML_STATE_PTR(l_caml_start_program_109, %edi) /* Reload for LBL(109) entry */
/* Pop the callback link, restoring the global variables */
popl CAML_STATE(bottom_of_stack, %edi); CFI_ADJUST(-4)
popl CAML_STATE(last_return_address, %edi); CFI_ADJUST(-4)
@@ -282,7 +349,7 @@ LBL(108):
FUNCTION(caml_raise_exn)
CFI_STARTPROC
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_raise_exn, %ebx)
testl $1, CAML_STATE(backtrace_active, %ebx)
jne LBL(110)
movl CAML_STATE(exception_pointer, %ebx), %esp
@@ -299,7 +366,7 @@ LBL(110):
pushl %edx; CFI_ADJUST(4) /* arg 3: sp of raise */
pushl %eax; CFI_ADJUST(4) /* arg 2: pc of raise */
pushl %esi; CFI_ADJUST(4) /* arg 1: exception bucket */
- call G(caml_stash_backtrace)
+ call_global_func caml_stash_backtrace, l_caml_raise_exn
movl %esi, %eax /* Recover exception bucket */
movl %edi, %esp
popl CAML_STATE(exception_pointer, %ebx); CFI_ADJUST(-4)
@@ -312,7 +379,7 @@ LBL(110):
FUNCTION(caml_raise_exception)
CFI_STARTPROC
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_raise_exception, %ebx)
testl $1, CAML_STATE(backtrace_active, %ebx)
jne LBL(112)
movl 8(%esp), %eax
@@ -331,7 +398,7 @@ LBL(112):
pushl CAML_STATE(last_return_address, %ebx); CFI_ADJUST(4)
/* 1: exception bucket */
pushl %esi; CFI_ADJUST(4)
- call G(caml_stash_backtrace)
+ call_global_func caml_stash_backtrace, l_caml_raise_exception
movl %esi, %eax /* Recover exception bucket */
movl CAML_STATE(exception_pointer, %ebx), %esp
popl CAML_STATE(exception_pointer, %ebx); CFI_ADJUST(-4)
@@ -370,8 +437,13 @@ FUNCTION(caml_callback2_asm)
movl 28(%esp), %edi /* arguments array */
movl 0(%edi), %eax /* arg1: first argument */
movl 4(%edi), %ebx /* arg2: second argument */
+#ifdef __PIC__
+ inline_jump_to_caml_header caml_callback2_asm
+ inline_jump_to_caml_plt caml_callback2_asm caml_apply2
+#else
movl $ G(caml_apply2), %esi /* code pointer */
jmp LBL(106)
+#endif
CFI_ENDPROC
ENDFUNCTION(caml_callback2_asm)
@@ -388,8 +460,13 @@ FUNCTION(caml_callback3_asm)
movl 0(%edi), %eax /* arg1: first argument */
movl 4(%edi), %ebx /* arg2: second argument */
movl 8(%edi), %ecx /* arg3: third argument */
+#ifdef __PIC__
+ inline_jump_to_caml_header caml_callback3_asm
+ inline_jump_to_caml_plt caml_callback3_asm caml_apply3
+#else
movl $ G(caml_apply3), %esi /* code pointer */
jmp LBL(106)
+#endif
CFI_ENDPROC
ENDFUNCTION(caml_callback3_asm)
@@ -405,7 +482,7 @@ FUNCTION(caml_ml_array_bound_error)
ffree %st(6)
ffree %st(7)
/* Record lowest stack address and return address */
- movl G(Caml_state), %ebx
+ MOV_CAML_STATE_PTR(l_caml_ml_array_bound_error, %ebx)
movl (%esp), %edx
movl %edx, CAML_STATE(last_return_address, %ebx)
leal 4(%esp), %edx
@@ -413,7 +490,7 @@ FUNCTION(caml_ml_array_bound_error)
/* Re-align the stack */
andl $-16, %esp
/* Branch to [caml_array_bound_error] (never returns) */
- call G(caml_array_bound_error)
+ call_global_func caml_array_bound_error, l_caml_ml_array_bound_error
CFI_ENDPROC
ENDFUNCTION(caml_ml_array_bound_error)
diff --git a/runtime/roots_nat.c b/runtime/roots_nat.c
index d1612a2..fd23a70 100644
--- a/runtime/roots_nat.c
+++ b/runtime/roots_nat.c
@@ -116,7 +116,12 @@ static void fill_hashtable(link *frametables) {
h = (h+1) & caml_frame_descriptors_mask;
}
caml_frame_descriptors[h] = d;
- d = next_frame_descr(d);
+ /* Don't traverse to next frame unnecessarily. If last frame has
+ frame_size=-1 then we will be reading link-time determined bytes
+ of the bss or data segment (off-by-one bug). */
+ if (j != len - 1) {
+ d = next_frame_descr(d);
+ }
}
}
}
diff --git a/utils/Makefile b/utils/Makefile
index de92390..8955e07 100644
--- a/utils/Makefile
+++ b/utils/Makefile
@@ -83,6 +83,7 @@ config.ml: config.mlp $(ROOTDIR)/Makefile.config Makefile
$(call SUBST,SYSTEM) \
$(call SUBST,SYSTHREAD_SUPPORT) \
$(call SUBST,TARGET) \
+ $(call SUBST,TARGET_OS) \
$(call SUBST,WITH_FRAME_POINTERS) \
$(call SUBST,WITH_PROFINFO) \
$(call SUBST,FLAT_FLOAT_ARRAY) \
diff --git a/utils/clflags.ml b/utils/clflags.ml
index 83bd357..12ee3b1 100644
--- a/utils/clflags.ml
+++ b/utils/clflags.ml
@@ -160,9 +160,15 @@ let std_include_dir () =
let shared = ref false (* -shared *)
let dlcode = ref true (* not -nodynlink *)
-let pic_code = ref (match Config.architecture with (* -fPIC *)
- | "amd64" -> true
- | _ -> false)
+let pic_code = ref (
+ let android =
+ let l = String.length "linux-android" in
+ String.length Config.target_os >= l && String.equal (String.sub Config.target_os 0 l) "linux-android"
+ in
+ match Config.architecture, android with (* -fPIC *)
+ | "amd64", _ -> true
+ | _, true -> true
+ | _ -> false)
let runtime_variant = ref "";; (* -runtime-variant *)
let with_runtime = ref true;; (* -with-runtime *)
diff --git a/utils/config.mli b/utils/config.mli
index 94dee3c..34496f4 100644
--- a/utils/config.mli
+++ b/utils/config.mli
@@ -202,6 +202,9 @@ val host : string
val target : string
(** Whether the compiler is a cross-compiler *)
+val target_os : string
+(** The OS part of the target ABI triplet *)
+
val flambda : bool
(** Whether the compiler was configured for flambda *)
diff --git a/utils/config.mlp b/utils/config.mlp
index f758a9b..92ddbb9 100644
--- a/utils/config.mlp
+++ b/utils/config.mlp
@@ -141,6 +141,7 @@ let ext_dll = "%%EXT_DLL%%"
let host = "%%HOST%%"
let target = "%%TARGET%%"
+let target_os = "%%TARGET_OS%%"
let default_executable_name =
match Sys.os_type with