-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathgas-zippatch.patch
8052 lines (7996 loc) · 263 KB
/
gas-zippatch.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
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
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/archures.c binutils-2.27-zip/bfd/archures.c
--- binutils-2.27-original/bfd/archures.c 2016-08-03 03:36:50.000000000 -0400
+++ binutils-2.27-zip/bfd/archures.c 2017-01-04 14:22:45.000000000 -0500
@@ -525,6 +525,8 @@
.#define bfd_mach_nios2r2 2
. bfd_arch_visium, {* Visium *}
.#define bfd_mach_visium 1
+. bfd_arch_zip, {* ZipCPU *}
+.#define bfd_mach_zip 0
. bfd_arch_last
. };
*/
@@ -655,6 +657,7 @@
extern const bfd_arch_info_type bfd_xgate_arch;
extern const bfd_arch_info_type bfd_z80_arch;
extern const bfd_arch_info_type bfd_z8k_arch;
+extern const bfd_arch_info_type bfd_zip_arch;
static const bfd_arch_info_type * const bfd_archures_list[] =
{
@@ -744,6 +747,7 @@
&bfd_xgate_arch,
&bfd_z80_arch,
&bfd_z8k_arch,
+ &bfd_zip_arch,
#endif
0
};
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/bfd-in2.h binutils-2.27-zip/bfd/bfd-in2.h
--- binutils-2.27-original/bfd/bfd-in2.h 2016-08-03 03:36:50.000000000 -0400
+++ binutils-2.27-zip/bfd/bfd-in2.h 2017-01-04 22:04:11.000000000 -0500
@@ -2336,6 +2336,8 @@
#define bfd_mach_nios2r2 2
bfd_arch_visium, /* Visium */
#define bfd_mach_visium 1
+ bfd_arch_zip, /* ZipCPU */
+#define bfd_mach_zip 0
bfd_arch_last
};
@@ -6335,6 +6337,22 @@
BFD_RELOC_VISIUM_HI16_PCREL,
BFD_RELOC_VISIUM_LO16_PCREL,
BFD_RELOC_VISIUM_IM16_PCREL,
+
+/* ZipCPU - 32 bit absolute value for LJMP instruction */
+ BFD_RELOC_ZIP_VALUE,
+
+/* ZipCPU - 18-bit PC-relative offset for BRA (ADD #x,PC) instructions */
+ BFD_RELOC_ZIP_BRANCH,
+
+/* ZipCPU value relocations */
+ BFD_RELOC_ZIP_OPB_IMM,
+ BFD_RELOC_ZIP_OPB_OFFSET,
+ BFD_RELOC_ZIP_OPB_PCREL,
+ BFD_RELOC_ZIP_MOV_OFFSET,
+ BFD_RELOC_ZIP_MOV_PCREL,
+ BFD_RELOC_ZIP_LDI,
+ BFD_RELOC_ZIP_LLO,
+ BFD_RELOC_ZIP_BREV,
BFD_RELOC_UNUSED };
typedef enum bfd_reloc_code_real bfd_reloc_code_real_type;
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/config.bfd binutils-2.27-zip/bfd/config.bfd
--- binutils-2.27-original/bfd/config.bfd 2016-08-03 03:36:50.000000000 -0400
+++ binutils-2.27-zip/bfd/config.bfd 2016-12-31 17:11:00.961307172 -0500
@@ -1742,6 +1742,10 @@
targ_underscore=yes
;;
+ zip*)
+ targ_defvec=zip_elf32_vec
+ ;;
+
*-*-ieee*)
targ_defvec=ieee_vec
;;
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/configure binutils-2.27-zip/bfd/configure
--- binutils-2.27-original/bfd/configure 2016-08-03 04:33:36.000000000 -0400
+++ binutils-2.27-zip/bfd/configure 2016-12-31 17:12:22.360697343 -0500
@@ -14542,6 +14542,7 @@
xtensa_elf32_le_vec) tb="$tb xtensa-isa.lo xtensa-modules.lo elf32-xtensa.lo elf32.lo $elf" ;;
z80_coff_vec) tb="$tb coff-z80.lo reloc16.lo $coffgen" ;;
z8k_coff_vec) tb="$tb coff-z8k.lo reloc16.lo $coff" ;;
+ zip_elf32_vec) tb="$tb elf32-zip.lo elf32.lo $elf" ;;
# These appear out of order in targets.c
srec_vec) tb="$tb srec.lo" ;;
@@ -14924,6 +14925,9 @@
x86_64-*-netbsd* | x86_64-*-openbsd*)
COREFILE=netbsd-core.lo
;;
+ zip*)
+ COREFILE=netbsd-core.lo
+ ;;
esac
case "$COREFILE" in
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/configure.ac binutils-2.27-zip/bfd/configure.ac
--- binutils-2.27-original/bfd/configure.ac 2016-08-03 03:36:50.000000000 -0400
+++ binutils-2.27-zip/bfd/configure.ac 2016-12-31 17:13:38.600136486 -0500
@@ -717,6 +717,7 @@
xtensa_elf32_le_vec) tb="$tb xtensa-isa.lo xtensa-modules.lo elf32-xtensa.lo elf32.lo $elf" ;;
z80_coff_vec) tb="$tb coff-z80.lo reloc16.lo $coffgen" ;;
z8k_coff_vec) tb="$tb coff-z8k.lo reloc16.lo $coff" ;;
+ zip_elf32_vec) tb="$tb elf32-zip.lo elf32.lo $elf" ;;
# These appear out of order in targets.c
srec_vec) tb="$tb srec.lo" ;;
@@ -1092,6 +1093,9 @@
x86_64-*-netbsd* | x86_64-*-openbsd*)
COREFILE=netbsd-core.lo
;;
+ zip*)
+ COREFILE=netbsd-core.lo
+ ;;
esac
case "$COREFILE" in
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/cpu-zip.c binutils-2.27-zip/bfd/cpu-zip.c
--- binutils-2.27-original/bfd/cpu-zip.c 1969-12-31 19:00:00.000000000 -0500
+++ binutils-2.27-zip/bfd/cpu-zip.c 2025-01-18 17:41:07.333677212 -0500
@@ -0,0 +1,65 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Filename: sw/binutils-2.27-zip/bfd/cpu-zip.c
+// {{{
+// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
+//
+// Purpose: BFD support for the Zip CPU architecture.
+//
+// This file is part of BFD, the Binary File Descriptor library.
+//
+// Creator: Dan Gisselquist, Ph.D.
+// Gisselquist Technology, LLC
+//
+////////////////////////////////////////////////////////////////////////////////
+//
+// Copyright (C) 2016-2025, Gisselquist Technology, LLC
+// {{{
+// This program is free software (firmware): you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or (at
+// your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. (It's in the $(ROOT)/doc directory. Run make with no
+// target there if the PDF file isn't present.) If not, see
+// <http://www.gnu.org/licenses/> for a copy.
+// }}}
+// License: GPL, v3, as defined and found on www.gnu.org,
+// {{{
+// http://www.gnu.org/licenses/gpl.html
+//
+////////////////////////////////////////////////////////////////////////////////
+#include "sysdep.h"
+#include "bfd.h"
+#include "libbfd.h"
+
+const bfd_arch_info_type
+bfd_zip_arch =
+{
+ 32, // There's 32 bits_per_word.
+ 32, // There's 32 bits_per_address.
+ 8, // There's 8 bits_per_byte.
+ bfd_arch_zip, // One of enum bfd_architecture, defined
+ // in archures.c and provided in
+ // generated header files.
+ bfd_mach_zip, // Random BFD-internal number for this
+ // machine, similarly listed in
+ // archures.c. Not emitted in output.
+ "zip", // The arch_name.
+ "zip", // The printable name is the same.
+ 2, // Section alignment power; each section
+ // is aligned to (only) 2^2 (i.e. 4) bytes.
+ TRUE, // This is the default "machine".
+ bfd_default_compatible, // Architecture comparison function
+ bfd_default_scan, // String to architecture conversion
+ bfd_arch_default_fill, // Default fill.
+ NULL // Pointer to next bfd_arch_info_type in
+ // the same family.
+};
+
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/doc/archures.texi binutils-2.27-zip/bfd/doc/archures.texi
--- binutils-2.27-original/bfd/doc/archures.texi 2016-08-03 04:36:22.000000000 -0400
+++ binutils-2.27-zip/bfd/doc/archures.texi 2016-12-31 17:14:43.103668704 -0500
@@ -492,6 +492,8 @@
#define bfd_mach_nios2r2 2
bfd_arch_visium, /* Visium */
#define bfd_mach_visium 1
+ bfd_mach_zip,
+#define bfd_mach_zip 0
bfd_arch_last
@};
@end example
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/doc/bfd.info binutils-2.27-zip/bfd/doc/bfd.info
--- binutils-2.27-original/bfd/doc/bfd.info 2016-08-03 04:36:22.000000000 -0400
+++ binutils-2.27-zip/bfd/doc/bfd.info 2017-01-04 14:40:21.000000000 -0500
@@ -8466,6 +8466,8 @@
#define bfd_mach_nios2r2 2
bfd_arch_visium, /* Visium */
#define bfd_mach_visium 1
+ bfd_arch_zip, /* ZipCPU */
+ #define bfd_mach_zip 0
bfd_arch_last
};
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/doc/reloc.texi binutils-2.27-zip/bfd/doc/reloc.texi
--- binutils-2.27-original/bfd/doc/reloc.texi 2016-08-03 04:36:22.000000000 -0400
+++ binutils-2.27-zip/bfd/doc/reloc.texi 2016-12-31 17:17:15.950640091 -0500
@@ -4214,6 +4214,19 @@
@deffnx {} BFD_RELOC_VISIUM_IM16_PCREL
Visium Relocations.
@end deffn
+@deffn {} BFD_RELOC_ZIP_VALUE
+@deffnx {} BFD_RELOC_OPB_IMM
+@deffnx {} BFD_RELOC_OPB_OFFSET
+@deffnx {} BFD_RELOC_OPB_PCREL
+@deffnx {} BFD_RELOC_OPB_GOTREL
+@deffnx {} BFD_RELOC_MOV_OFFSET
+@deffnx {} BFD_RELOC_MOV_PCREL
+@deffnx {} BFD_RELOC_MOV_GOTREL
+@deffnx {} BFD_RELOC_ZIP_LDI
+@deffnx {} BFD_RELOC_ZIP_LLO
+@deffnx {} BFD_RELOC_ZIP_LHI
+ZipCPU relocations
+@end deffn
@example
diff -Naur '--exclude=*.swp' binutils-2.27-original/bfd/elf32-zip.c binutils-2.27-zip/bfd/elf32-zip.c
--- binutils-2.27-original/bfd/elf32-zip.c 1969-12-31 19:00:00.000000000 -0500
+++ binutils-2.27-zip/bfd/elf32-zip.c 2025-01-18 17:40:22.002528731 -0500
@@ -0,0 +1,1134 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Filename: sw/binutils-2.27-zip/bfd/elf32-zip.c
+// {{{
+// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
+//
+// Purpose: Zip-specific support for 32-bit ELF.
+//
+// This file is part of BFD, the Binary File Descriptor library.
+//
+// Creator: Dan Gisselquist, Ph.D.
+// Gisselquist Technology, LLC
+//
+////////////////////////////////////////////////////////////////////////////////
+//
+// Copyright (C) 2016-2025, Gisselquist Technology, LLC
+// {{{
+// This program is free software (firmware): you can redistribute it and/or
+// modify it under the terms of the GNU General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or (at
+// your option) any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. (It's in the $(ROOT)/doc directory. Run make with no
+// target there if the PDF file isn't present.) If not, see
+// <http://www.gnu.org/licenses/> for a copy.
+// }}}
+// License: GPL, v3, as defined and found on www.gnu.org,
+// {{{
+// http://www.gnu.org/licenses/gpl.html
+//
+////////////////////////////////////////////////////////////////////////////////
+#include "sysdep.h"
+#include "bfd.h"
+#include "bfdlink.h"
+#include "libbfd.h"
+#include "elf-bfd.h"
+#include "elf/zip.h"
+#include <limits.h>
+#include <stdint.h>
+// }}}
+#define zip_relocation bfd_elf_generic_reloc
+
+static bfd_reloc_status_type
+zip_brev_relocation(bfd *, arelent *, asymbol *, void *, asection *,
+ bfd *, char **error_messsage);
+static uint32_t zip_bitreverse(uint32_t v);
+
+/* Forward declarations. */
+static reloc_howto_type zip_elf_howto_table [] =
+{
+ /* This reloc does nothing. */
+ HOWTO (R_ZIP_NONE, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 32, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_dont, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_NONE", /* name */
+ FALSE, /* partial_inplace */
+ 0, /* src_mask */
+ 0, /* dst_mask */
+ FALSE), /* pcrel_offset */
+
+ /* A 32 bit absolute relocation. */
+ HOWTO (R_ZIP_VALUE, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 32, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_dont, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_VALUE", /* name */
+ FALSE, /* partial_inplace */
+ 0x00000000, /* src_mask */
+ 0xffffffff, /* dst_mask */
+ FALSE), /* pcrel_offset */
+
+ HOWTO (R_ZIP_BREV, /* type -- LDIHI, but with bitreverse */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 16, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_bitfield, /* complain_on_overflow */
+ zip_brev_relocation, /* special_function--needed for the bitreverse */
+ "R_ZIP_BREV", /* name */
+ FALSE, /* partial_inplace */
+ 0, /* src_mask */
+ 0x0003ffff, /* dst_mask */
+ FALSE), /* pcrel_offset */
+
+ HOWTO (R_ZIP_LLO, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 23, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_dont, /* don't complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_LLO", /* name */
+ FALSE, /* partial_inplace */
+ 0, /* src_mask */
+ 0x0000ffff, /* dst_mask */
+ FALSE), /* pcrel_offset */
+
+ HOWTO (R_ZIP_LDI, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 23, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_signed, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_LDI", /* name */
+ FALSE, /* partial_inplace */
+ 0, /* src_mask */
+ 0x007fffff, /* dst_mask */
+ FALSE), /* pcrel_offset */
+
+ /* An 18 bit pc-relative relocation. */
+ HOWTO (R_ZIP_BRANCH, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 18, /* bitsize */
+ TRUE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_signed, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_BRANCH", /* name */
+ FALSE, /* partial_inplace */
+ 0x00000000, /* src_mask */
+ 0x0003fffc, /* dst_mask */
+ TRUE), /* pcrel_offset */
+
+ /* An 18 bit operand B immediate. */
+ HOWTO (R_ZIP_OPB_IMM, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 18, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_signed, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_OPB_IMM", /* name */
+ FALSE, /* partial_inplace */
+ 0x00000000, /* src_mask */
+ 0x0003ffff, /* dst_mask */
+ FALSE), /* pcrel_offset */
+
+ /* An 18 bit relocation. */
+ HOWTO (R_ZIP_OPB_OFFSET, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 14, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_signed, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_OPB_OFFSET", /* name */
+ FALSE, /* partial_inplace */
+ 0x00000000, /* src_mask */
+ 0x00003fff, /* dst_mask-14 bits */
+ FALSE), /* pcrel_offset */
+
+ /* An 18 bit operand B immediate, but relative to the current PC. */
+ HOWTO (R_ZIP_OPB_PCREL, /* type */
+ 2, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 14, /* bitsize */
+ TRUE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_signed, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_OPB_PCREL", /* name */
+ FALSE, /* partial_inplace */
+ 0x00000000, /* src_mask */
+ 0x00003fff, /* dst_mask-14 bits */
+ TRUE), /* pcrel_offset */
+
+ /* An 18 bit operand B immediate, but relative to the Global Offset Table. */
+ //HOWTO (R_ZIP_OPB_GOTREL, /* type */
+ //0, /* rightshift */
+ //2, /* size (0 = byte, 1 = short, 2 = long) */
+ //18, /* bitsize */
+ //FALSE, /* pc_relative */
+ //0, /* bitpos */
+ //complain_overflow_signed, /* complain_on_overflow */
+ //zip_relocation, /* special_function */
+ //"R_ZIP_OPB_GOTREL", /* name */
+ //FALSE, /* partial_inplace */
+ //0x00000000, /* src_mask */
+ //0x0003ffff, /* dst_mask-14 bits */
+ //FALSE), /* pcrel_offset */
+
+ /* */
+ HOWTO (R_ZIP_MOV_OFFSET, /* type */
+ 0, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 13, /* bitsize */
+ FALSE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_signed, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_MOV_OFFSET", /* name */
+ FALSE, /* partial_inplace */
+ 0, /* src_mask */
+ 0x00001fff, /* dst_mask */
+ FALSE), /* pcrel_offset */
+
+ /* */
+ HOWTO (R_ZIP_MOV_PCREL, /* type */
+ 2, /* rightshift */
+ 2, /* size (0 = byte, 1 = short, 2 = long) */
+ 13, /* bitsize */
+ TRUE, /* pc_relative */
+ 0, /* bitpos */
+ complain_overflow_signed, /* complain_on_overflow */
+ zip_relocation, /* special_function */
+ "R_ZIP_MOV_PCREL", /* name */
+ FALSE, /* partial_inplace */
+ 0, /* src_mask */
+ 0x00001fff, /* dst_mask */
+ TRUE) /* pcrel_offset */
+
+ /* */
+ //HOWTO (R_ZIP_MOV_GOTREL, /* type */
+ //0, /* rightshift */
+ //2, /* size (0 = byte, 1 = short, 2 = long) */
+ //13, /* bitsize */
+ //FALSE, /* pc_relative */
+ //0, /* bitpos */
+ //complain_overflow_signed, /* complain_on_overflow */
+ //zip_relocation, /* special_function */
+ //"R_ZIP_MOV_GOTREL", /* name */
+ //FALSE, /* partial_inplace */
+ //0, /* src_mask */
+ //0x00001fff, /* dst_mask */
+ //FALSE), /* pcrel_offset */
+
+};
+
+/* This structure is used to map BFD reloc codes to Zip ELF relocations */
+
+struct elf_reloc_map
+{
+ bfd_reloc_code_real_type bfd_reloc_val;
+ unsigned int elf_reloc_val;
+};
+
+static const struct elf_reloc_map zip_reloc_map [] =
+{
+ { BFD_RELOC_NONE, R_ZIP_NONE },
+ { BFD_RELOC_ZIP_VALUE, R_ZIP_VALUE },
+ { BFD_RELOC_ZIP_BRANCH, R_ZIP_BRANCH },
+ { BFD_RELOC_ZIP_OPB_IMM, R_ZIP_OPB_IMM },
+ { BFD_RELOC_ZIP_OPB_OFFSET, R_ZIP_OPB_OFFSET },
+ { BFD_RELOC_ZIP_MOV_OFFSET, R_ZIP_MOV_OFFSET },
+ { BFD_RELOC_ZIP_LDI, R_ZIP_LDI },
+ { BFD_RELOC_ZIP_LLO, R_ZIP_LLO },
+ { BFD_RELOC_ZIP_BREV, R_ZIP_BREV },
+ { BFD_RELOC_14, R_ZIP_OPB_OFFSET },
+ { BFD_RELOC_16, R_ZIP_LLO },
+ { BFD_RELOC_32, R_ZIP_VALUE },
+ { BFD_RELOC_ZIP_OPB_PCREL, R_ZIP_OPB_PCREL },
+ { BFD_RELOC_ZIP_MOV_PCREL, R_ZIP_MOV_PCREL }
+ // { BFD_RELOC_ZIP_OPB_GOTREL, R_ZIP_OPB_GOTREL },
+ // { BFD_RELOC_ZIP_MOV_GOTREL, R_ZIP_MOV_GOTREL },
+};
+
+/* Given a BFD reloc code, return the howto structure for the corresponding
+ * Zip ELF relocation. */
+static reloc_howto_type *
+zip_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
+ bfd_reloc_code_real_type code)
+{
+ unsigned int i;
+
+ for (i = 0; i < sizeof (zip_reloc_map) / sizeof (zip_reloc_map[0]); i++)
+ if (zip_reloc_map [i].bfd_reloc_val == code)
+ return & zip_elf_howto_table [(int)zip_reloc_map[i].elf_reloc_val];
+
+ return NULL;
+}
+
+static reloc_howto_type *
+zip_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
+{
+ unsigned int i;
+
+ for (i = 0;
+ i < sizeof (zip_elf_howto_table) / sizeof (zip_elf_howto_table[0]);
+ i++)
+ if (zip_elf_howto_table[i].name != NULL
+ && strcasecmp (zip_elf_howto_table[i].name, r_name) == 0)
+ return &zip_elf_howto_table[i];
+
+ return NULL;
+}
+
+/* Given an ELF reloc, fill in the howto field of a relent. */
+static void
+zip_elf_info_to_howto(bfd * abfd ATTRIBUTE_UNUSED,
+ arelent * cache_ptr,
+ Elf_Internal_Rela * dst)
+{
+ unsigned int r;
+
+ r = ELF32_R_TYPE(dst->r_info);
+ BFD_ASSERT (r < (unsigned int) R_ZIP_max);
+ cache_ptr->howto = &zip_elf_howto_table [r];
+}
+
+static bfd_boolean
+zip_elf_relocate_section(bfd *output_bfd,
+ struct bfd_link_info *info,
+ bfd *input_bfd,
+ asection *input_section,
+ bfd_byte *contents,
+ Elf_Internal_Rela *relocs,
+ Elf_Internal_Sym *local_syms,
+ asection **local_sections)
+{
+ Elf_Internal_Shdr *symtab_hdr;
+ struct elf_link_hash_entry **sym_hashes;
+ Elf_Internal_Rela *rel, *relend;
+
+ symtab_hdr = &elf_tdata(input_bfd)->symtab_hdr;
+ sym_hashes = elf_sym_hashes(input_bfd);
+ relend = relocs+input_section->reloc_count;
+
+ for(rel=relocs; rel<relend; rel++) {
+ reloc_howto_type *howto;
+ unsigned long r_symndx;
+ Elf_Internal_Sym *sym;
+ asection *sec;
+ struct elf_link_hash_entry *h;
+ bfd_vma relocation;
+ bfd_reloc_status_type r;
+ const char *name = NULL;
+ int r_type;
+
+ r_type = ELF32_R_TYPE(rel->r_info);
+ r_symndx = ELF32_R_SYM(rel->r_info);
+
+ if ((r_type < 0) || (r_type >= (int)R_ZIP_max))
+ {
+ bfd_set_error(bfd_error_bad_value);
+ return FALSE;
+ }
+
+ howto = zip_elf_howto_table + ELF32_R_TYPE(rel->r_info);
+ h = NULL;
+ sym = NULL;
+ sec = NULL;
+
+ if (r_symndx < symtab_hdr->sh_info)
+ {
+ sym = local_syms + r_symndx;
+ sec = local_sections[r_symndx];
+ relocation = _bfd_elf_rela_local_sym(output_bfd, sym, &sec, rel);
+ name = bfd_elf_string_from_elf_section
+ (input_bfd, symtab_hdr->sh_link, sym->st_name);
+ name = (name == NULL) ? bfd_section_name(input_bfd, sec)
+ : name;
+ } else {
+ bfd_boolean unresolved_reloc, warned, ignored;
+
+ RELOC_FOR_GLOBAL_SYMBOL(info, input_bfd, input_section,
+ rel, r_symndx, symtab_hdr, sym_hashes, h, sec,
+ relocation, unresolved_reloc, warned, ignored);
+ }
+
+ if ((sec != NULL)&&(discarded_section(sec))) {
+ RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd,
+ input_section, rel, 1, relend, howto, 0,
+ contents);
+ }
+
+ if (bfd_link_relocatable(info))
+ continue;
+
+ if (howto->type == R_ZIP_BREV) {
+ if (rel->r_offset > bfd_get_section_limit(input_bfd, input_section)) {
+ r = bfd_reloc_outofrange;
+ } else {
+ uint32_t brev_reloc;
+ bfd_byte *location;
+ bfd_vma insn;
+
+ location = contents + rel->r_offset * bfd_octets_per_byte(input_bfd);
+
+ relocation += rel->r_addend;
+ brev_reloc= zip_bitreverse(relocation);
+ insn = bfd_get_32(input_bfd, location);
+ insn = ((insn & ~howto->dst_mask)
+ |(((insn & howto->src_mask)+brev_reloc)&howto->dst_mask));
+ bfd_put_32(input_bfd, insn, location);
+ r = bfd_reloc_ok;
+ }
+ } else {
+ r = _bfd_final_link_relocate(howto, input_bfd,
+ input_section,
+ contents, rel->r_offset,
+ relocation,
+ rel->r_addend);
+ }
+
+
+ if (r != bfd_reloc_ok)
+ {
+ const char *msg = NULL;
+
+ switch(r)
+ {
+ case bfd_reloc_overflow:
+ info->callbacks->reloc_overflow(
+ info, (h?&h->root:NULL),
+ name, howto->name,
+ (bfd_vma)0, input_bfd,
+ input_section, rel->r_offset);
+ break;
+ case bfd_reloc_undefined:
+ info->callbacks->undefined_symbol(
+ info, name, input_bfd,
+ input_section, rel->r_offset,
+ TRUE);
+ break;
+ case bfd_reloc_outofrange:
+ msg = _("internal error: out of range error");
+ break;
+ case bfd_reloc_notsupported:
+ msg = _("internal error: unsupported relocation");
+ break;
+ case bfd_reloc_dangerous:
+ msg = _("internal error: dangerous relocation");
+ break;
+ default:
+ msg = _("internal error: unknown error");
+ break;
+ }
+
+ if (msg)
+ info->callbacks->warning(info, msg, name,
+ input_bfd, input_section,
+ rel->r_offset);
+
+ if (!r)
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+static uint32_t
+zip_bitreverse(uint32_t v) {
+ unsigned r = 0, b;
+
+ for(b=0; b<32; b++, v>>=1)
+ r = (r<<1)|(v&1);
+
+ return r;
+}
+
+static bfd_reloc_status_type
+zip_brev_relocation(bfd *abfd,
+ arelent *reloc_entry,
+ asymbol *symbol,
+ void *data,
+ asection *input_section,
+ bfd *output_bfd,
+ char **error_message)
+{
+ bfd_vma relocation;
+ bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte(abfd);
+ bfd_vma output_base = 0;
+ reloc_howto_type *howto = reloc_entry->howto;
+
+ // If this isn't a final relocation, then just use the generic
+ // relocation function.
+ if (output_bfd != NULL) {
+ return zip_relocation(abfd, reloc_entry, symbol, data,
+ input_section, output_bfd, error_message);
+ }
+
+ // Otherwise, we need to adjust our file itself with this value ...
+ // Check that our relocation lies within the file, and particularly
+ // the section we think it should. (This should really be an assert...)
+ if (reloc_entry->address > bfd_get_section_limit(abfd, input_section))
+ return bfd_reloc_outofrange;
+
+ // Get symbol value
+ if (bfd_is_com_section(symbol->section))
+ relocation = 0;
+ else
+ relocation = symbol->value;
+
+ /* Convert input-section relative symbol value to absolute */
+ if ((!howto->partial_inplace)
+ ||(symbol->section->output_section == NULL))
+ output_base = 0;
+ else
+ output_base = symbol->section->output_section->vma;
+
+ relocation += output_base + symbol->section->output_offset;
+
+ /* Add in supplied addend. */
+ relocation += reloc_entry->addend;
+
+ // BREV does not handle PC relative offsets
+
+ // Ignore overflow checking ... BREV handles the top 18 bits of a 32-bit
+ // number. Overflow would mean overflowing the 32-bit address space--
+ // not possible.
+ //
+ // if howto->complain_on_overflow ...
+
+ // relocation >>= howto->rightshift; // = 0
+ // relocation <<= howto->bitpos; // = 0
+
+ // Logic (nearly) copied from reloc.c:bfd_perform_relocation
+ unsigned insn = bfd_get_32(abfd, (bfd_byte *)data + octets);
+
+ // Here's why we are going through this pain!
+ insn = zip_bitreverse((unsigned)insn);
+
+ // Now we can continue as before....
+ insn = ((insn&(~howto->dst_mask))
+ |(((insn&howto->src_mask)+relocation)&howto->dst_mask));
+ bfd_put_32(abfd, (bfd_vma)insn, (bfd_byte *)data + octets);
+
+ return bfd_reloc_ok;
+}
+
+// Zip Defines
+#define TARGET_BIG_SYM zip_elf32_vec
+#define TARGET_BIG_NAME "elf32-zip"
+
+#define ELF_ARCH bfd_arch_zip
+// #define ELF_TARGET_ID ZIP_ELF_DATA
+#define ELF_MACHINE_CODE EM_ZIP
+
+#define ELF_MAXPAGESIZE 0x1000
+#define ARCH_SIZE 32
+
+
+#define bfd_elf32_bfd_define_common_symbol bfd_generic_define_common_symbol
+
+
+#define bfd_elf32_bfd_reloc_type_lookup zip_elf_reloc_type_lookup
+#define bfd_elf32_bfd_reloc_name_lookup zip_elf_reloc_name_lookup
+#define elf_info_to_howto_rel 0
+#define elf_info_to_howto zip_elf_info_to_howto
+#define elf_backend_relocate_section zip_elf_relocate_section
+#define elf_backend_rela_normal 1
+
+// Default ELF32 defines from elf32-target.h that we would've normally included
+// here, had we not been a OCTETS_PER_BYTE=4 machine
+
+#define bfd_elf32_close_and_cleanup _bfd_elf_close_and_cleanup
+#define bfd_elf32_bfd_free_cached_info _bfd_free_cached_info
+#define bfd_elf32_get_section_contents _bfd_generic_get_section_contents
+#define bfd_elf32_canonicalize_dynamic_symtab \
+ _bfd_elf_canonicalize_dynamic_symtab
+#define bfd_elf32_get_synthetic_symtab _bfd_elf_get_synthetic_symtab
+#define bfd_elf32_canonicalize_reloc _bfd_elf_canonicalize_reloc
+#define bfd_elf32_find_nearest_line _bfd_elf_find_nearest_line
+#define bfd_elf32_find_line _bfd_elf_find_line
+#define bfd_elf32_find_inliner_info _bfd_elf_find_inliner_info
+#define bfd_elf32_read_minisymbols _bfd_elf_read_minisymbols
+#define bfd_elf32_minisymbol_to_symbol _bfd_elf_minisymbol_to_symbol
+#define bfd_elf32_get_dynamic_symtab_upper_bound \
+ _bfd_elf_get_dynamic_symtab_upper_bound
+#define bfd_elf32_get_lineno _bfd_elf_get_lineno
+#define bfd_elf32_get_reloc_upper_bound _bfd_elf_get_reloc_upper_bound
+#define bfd_elf32_get_symbol_info _bfd_elf_get_symbol_info
+#define bfd_elf32_get_symbol_version_string _bfd_elf_get_symbol_version_string
+#define bfd_elf32_canonicalize_symtab _bfd_elf_canonicalize_symtab
+#define bfd_elf32_get_symtab_upper_bound _bfd_elf_get_symtab_upper_bound
+#define bfd_elf32_make_empty_symbol _bfd_elf_make_empty_symbol
+#define bfd_elf32_new_section_hook _bfd_elf_new_section_hook
+#define bfd_elf32_set_arch_mach _bfd_elf_set_arch_mach
+#define bfd_elf32_set_section_contents _bfd_elf_set_section_contents
+#define bfd_elf32_sizeof_headers _bfd_elf_sizeof_headers
+#define bfd_elf32_write_object_contents _bfd_elf_write_object_contents
+#define bfd_elf32_write_corefile_contents _bfd_elf_write_corefile_contents
+
+#define bfd_elf32_get_section_contents_in_window \
+ _bfd_generic_get_section_contents_in_window
+
+#define elf_backend_can_refcount 0
+#define elf_backend_want_got_plt 0
+#define elf_backend_plt_readonly 0
+#define elf_backend_want_plt_sym 0
+#define elf_backend_plt_not_loaded 0
+#define elf_backend_plt_alignment 2
+#define elf_backend_want_dynbss 1
+#define elf_backend_want_p_paddr_set_to_zero 0
+#define elf_backend_default_execstack 1
+#define elf_backend_caches_rawsize 0
+#define elf_backend_extern_protected_data 0
+#define elf_backend_stack_align 4
+#define elf_backend_strtab_flags 0
+
+#define bfd_elf32_bfd_debug_info_start bfd_void
+#define bfd_elf32_bfd_debug_info_end bfd_void
+#define bfd_elf32_bfd_debug_info_accumulate \
+ ((void (*) (bfd*, struct bfd_section *)) bfd_void)
+
+#define bfd_elf32_bfd_get_relocated_section_contents \
+ bfd_generic_get_relocated_section_contents
+
+#define bfd_elf32_bfd_relax_section bfd_generic_relax_section
+
+#define elf_backend_can_gc_sections 1
+#define elf_backend_can_refcount 0
+#define elf_backend_want_got_sym 1
+#define elf_backend_gc_keep _bfd_elf_gc_keep
+#define elf_backend_gc_mark_dynamic_ref bfd_elf_gc_mark_dynamic_ref_symbol
+#define elf_backend_gc_mark_hook _bfd_elf_gc_mark_hook
+#define elf_backend_gc_mark_extra_sections _bfd_elf_gc_mark_extra_sections
+#define elf_backend_gc_sweep_hook NULL
+#define bfd_elf32_bfd_gc_sections bfd_elf_gc_sections
+
+#ifndef bfd_elf32_bfd_merge_sections
+#define bfd_elf32_bfd_merge_sections _bfd_elf_merge_sections
+#endif
+
+#ifndef bfd_elf32_bfd_is_group_section
+#define bfd_elf32_bfd_is_group_section bfd_elf_is_group_section
+#endif
+
+#ifndef bfd_elf32_bfd_discard_group
+#define bfd_elf32_bfd_discard_group bfd_generic_discard_group
+#endif
+
+#ifndef bfd_elf32_section_already_linked
+#define bfd_elf32_section_already_linked _bfd_elf_section_already_linked
+#endif
+
+#ifndef bfd_elf32_bfd_define_common_symbol
+#define bfd_elf32_bfd_define_common_symbol bfd_generic_define_common_symbol
+#endif
+
+#ifndef bfd_elf32_bfd_lookup_section_flags
+#define bfd_elf32_bfd_lookup_section_flags bfd_elf_lookup_section_flags
+#endif
+
+#ifndef bfd_elf32_bfd_make_debug_symbol
+#define bfd_elf32_bfd_make_debug_symbol \
+ ((asymbol * (*) (bfd *, void *, unsigned long)) bfd_nullvoidptr)
+#endif
+
+#ifndef bfd_elf32_bfd_copy_private_symbol_data
+#define bfd_elf32_bfd_copy_private_symbol_data _bfd_elf_copy_private_symbol_data
+#endif
+
+#ifndef bfd_elf32_bfd_copy_private_section_data
+#define bfd_elf32_bfd_copy_private_section_data \
+ _bfd_elf_copy_private_section_data
+#endif
+#ifndef bfd_elf32_bfd_copy_private_header_data
+#define bfd_elf32_bfd_copy_private_header_data \
+ _bfd_elf_copy_private_header_data
+#endif
+#ifndef bfd_elf32_bfd_copy_private_bfd_data
+#define bfd_elf32_bfd_copy_private_bfd_data \
+ _bfd_elf_copy_private_bfd_data
+#endif
+#ifndef bfd_elf32_bfd_print_private_bfd_data
+#define bfd_elf32_bfd_print_private_bfd_data \
+ _bfd_elf_print_private_bfd_data
+#endif
+#ifndef bfd_elf32_bfd_merge_private_bfd_data
+#define bfd_elf32_bfd_merge_private_bfd_data \
+ ((bfd_boolean (*) (bfd *, bfd *)) bfd_true)
+#endif
+#ifndef bfd_elf32_bfd_set_private_flags
+#define bfd_elf32_bfd_set_private_flags \
+ ((bfd_boolean (*) (bfd *, flagword)) bfd_true)
+#endif
+#ifndef bfd_elf32_bfd_is_local_label_name
+#define bfd_elf32_bfd_is_local_label_name _bfd_elf_is_local_label_name
+#endif
+#ifndef bfd_elf32_bfd_is_target_special_symbol
+#define bfd_elf32_bfd_is_target_special_symbol \
+ ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
+#endif
+
+#ifndef bfd_elf32_get_dynamic_reloc_upper_bound
+#define bfd_elf32_get_dynamic_reloc_upper_bound \
+ _bfd_elf_get_dynamic_reloc_upper_bound
+#endif
+#ifndef bfd_elf32_canonicalize_dynamic_reloc
+#define bfd_elf32_canonicalize_dynamic_reloc _bfd_elf_canonicalize_dynamic_reloc
+#endif
+
+#define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
+#define bfd_elf32_bfd_link_add_symbols bfd_elf_link_add_symbols
+#define bfd_elf32_bfd_final_link bfd_elf_final_link
+
+#define bfd_elf32_bfd_link_just_syms _bfd_elf_link_just_syms
+
+#define bfd_elf32_bfd_copy_link_hash_symbol_type \
+ _bfd_elf_copy_link_hash_symbol_type
+
+#define bfd_elf32_bfd_link_split_section _bfd_generic_link_split_section
+#define bfd_elf32_bfd_link_check_relocs _bfd_generic_link_check_relocs
+#define bfd_elf32_archive_p bfd_generic_archive_p
+#define bfd_elf32_write_archive_contents _bfd_write_archive_contents
+#define bfd_elf32_mkobject bfd_elf_make_object
+#define bfd_elf32_mkcorefile bfd_elf_mkcorefile
+#define bfd_elf32_mkarchive _bfd_generic_mkarchive
+#define bfd_elf32_print_symbol bfd_elf_print_symbol
+#define elf_symbol_leading_char 0
+
+#define elf_backend_arch_data NULL
+
+#ifndef ELF_TARGET_ID
+#define ELF_TARGET_ID GENERIC_ELF_DATA
+#endif
+
+#ifndef ELF_OSABI
+#define ELF_OSABI ELFOSABI_NONE
+#endif
+
+#define ELF_COMMONPAGESIZE ELF_MAXPAGESIZE
+#define ELF_MINPAGESIZE ELF_COMMONPAGESIZE
+
+#if ELF_COMMONPAGESIZE > ELF_MAXPAGESIZE
+# error ELF_COMMONPAGESIZE > ELF_MAXPAGESIZE
+#endif
+#if ELF_MINPAGESIZE > ELF_COMMONPAGESIZE
+# error ELF_MINPAGESIZE > ELF_COMMONPAGESIZE
+#endif
+
+#ifndef ELF_DYNAMIC_SEC_FLAGS
+/* Note that we set the SEC_IN_MEMORY flag for these sections. */
+#define ELF_DYNAMIC_SEC_FLAGS \
+ (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS \
+ | SEC_IN_MEMORY | SEC_LINKER_CREATED)
+#endif
+
+#define elf_backend_collect FALSE
+#define elf_backend_type_change_ok FALSE
+
+#define elf_backend_sym_is_global 0
+#define elf_backend_object_p 0
+#define elf_backend_symbol_processing 0
+#define elf_backend_symbol_table_processing 0
+#define elf_backend_get_symbol_type 0
+#define elf_backend_archive_symbol_lookup _bfd_elf_archive_symbol_lookup
+#define elf_backend_name_local_section_symbols 0