/
po64-s390.c
1733 lines (1472 loc) · 56.4 KB
/
po64-s390.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* IBM z/OS Program Object support
Copyright (C) 2019 Free Software Foundation, Inc.
Contributed by Michael Colavita <mcolavita@rocketsoftware.com>
and Giancarlo Frix <gfrix@rocketsoftware.com>.
This file is part of BFD, the Binary File Descriptor library.
This program is free software; 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
MERCHANTABILITY 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; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
#include "sysdep.h"
#include "bfd.h"
#include "bfdlink.h"
#include "libbfd.h"
#include "elf-bfd.h"
#include "elf/s390.h"
#include "po-bfd.h"
#define min(a, b) ((a) < (b) ? (a) : (b))
/* Instead of using copy relocs, generate load-time relocations.
Note that since we don't have a guaranteed load address, we would
need load-time relocs in one form or another to even implement copy
relocs. Note that, as always, this is only relevant for
position-dependent shared libraries and executables. */
#define ELIMINATE_COPY_RELOCS 1
/* Define this to TRUE to prevent the generation of load-time consistency
checks in the PRDT (relocation section). They do not appear to be
required, and they prevent the binary from being edited by most simple
tools. It's unclear if there are any benefits to the consistency
checks on modern systems, so we do not generate them by default. */
#define NO_CONSISTENCY_CHECKING TRUE
static
const unsigned char iso88591_to_ibm1047[256] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F, 0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
/* 1 */ 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26, 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
/* 2 */ 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D, 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
/* 3 */ 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
/* 4 */ 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
/* 5 */ 0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
/* 6 */ 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
/* 7 */ 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
/* 8 */ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x09, 0x0A, 0x1B,
/* 9 */ 0x30, 0x31, 0x1A, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3A, 0x3B, 0x04, 0x14, 0x3E, 0xFF,
/* A */ 0x41, 0xAA, 0x4A, 0xB1, 0x9F, 0xB2, 0x6A, 0xB5, 0xBB, 0xB4, 0x9A, 0x8A, 0xB0, 0xCA, 0xAF, 0xBC,
/* B */ 0x90, 0x8F, 0xEA, 0xFA, 0xBE, 0xA0, 0xB6, 0xB3, 0x9D, 0xDA, 0x9B, 0x8B, 0xB7, 0xB8, 0xB9, 0xAB,
/* C */ 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9E, 0x68, 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77,
/* D */ 0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0xEC, 0xBF, 0x80, 0xFD, 0xFE, 0xFB, 0xFC, 0xBA, 0xAE, 0x59,
/* E */ 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9C, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,
/* F */ 0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1, 0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, 0xDF};
static const char eyecatcher_plmh[] =
{ 0xC9, 0xC5, 0xE6, 0xD7, 0xD3, 0xD4, 0xC8, 0x40 };
static const char eyecatcher_prat[] =
{ 0xC9, 0xC5, 0xE6, 0xD7, 0xD9, 0xC1, 0xE3, 0x40 };
static const char eyecatcher_prdt[] =
{ 0xC9, 0xC5, 0xE6, 0xD7, 0xD9, 0xC4, 0xE3, 0x40 };
static const char eyecatcher_lidx[] =
{ 0xC9, 0xC5, 0xE6, 0xD3, 0xC9, 0xC4, 0xE7, 0x40 };
static const char eyecatcher_psegm[] =
{ 0xC9, 0xC5, 0xE6, 0xD7, 0xE2, 0xC5, 0xC7, 0xD4 };
static const char text_pad[] =
{ 0xC9, 0xC5, 0xE6, 0xD7 };
static const char no_checksum_val[] =
{ 0x95, 0x96, 0x83, 0x88 }; /* 'noch' in EBCDIC. */
typedef bfd_vma prat_ent;
/* NOTE: Keep this structure in sync with
the one declared in elf64-s390.c. */
struct po_s390_obj_tdata
{
struct elf_obj_tdata root;
struct plt_entry *local_plt;
char *local_got_tls_type;
/* Program Object fields below here. */
/* High level internal structures */
struct po_internal_plmh header;
struct po_internal_pmar pmar;
struct po_internal_pmarl pmarl;
struct po_internal_prat prat;
struct po_internal_prdt prdt;
struct po_internal_lidx lidx;
struct po_internal_psegm psegm;
struct po_internal_po_name_header po_name_header;
/* Repeating internal structures TODO: refactor? */
struct po_internal_header_rec_decl *rec_decls;
struct po_internal_prdt_page *prdt_pages;
struct po_internal_lidx_entry *lidx_entries;
struct po_internal_psegm_entry *psegm_entries;
struct po_internal_po_name_header_entry *po_name_header_entries;
char **po_names;
prat_ent *prat_entries;
char *section_contents;
/* Computed values */
unsigned rec_decl_count;
unsigned int text_pad_words;
bfd_boolean headers_computed;
bfd_boolean sizes_computed;
/* File offset of the start of the elf file. If this is zero then
there is no contained elf file. */
ufile_ptr elf_offset;
};
#define po_tdata(bfd) ((struct po_s390_obj_tdata *) (bfd)->tdata.any)
#define po_header(bfd) (po_tdata (bfd)->header)
#define po_rec_decls(bfd) (po_tdata (bfd)->rec_decls)
#define po_rec_decl_count(bfd) (po_tdata (bfd)->rec_decl_count)
#define po_pmar(bfd) (po_tdata (bfd)->pmar)
#define po_pmarl(bfd) (po_tdata (bfd)->pmarl)
#define po_name_header(bfd) (po_tdata (bfd)->po_name_header)
#define po_name_header_entries(bfd) (po_tdata (bfd)->po_name_header_entries)
#define po_names(bfd) (po_tdata (bfd)->po_names)
#define po_prat(bfd) (po_tdata (bfd)->prat)
#define po_prat_entries(bfd) (po_tdata (bfd)->prat_entries)
#define po_prdt(bfd) (po_tdata (bfd)->prdt)
#define po_prdt_pages(bfd) (po_tdata (bfd)->prdt_pages)
#define po_lidx(bfd) (po_tdata (bfd)->lidx)
#define po_lidx_entries(bfd) (po_tdata (bfd)->lidx_entries)
#define po_psegm(bfd) (po_tdata (bfd)->psegm)
#define po_psegm_entries(bfd) (po_tdata (bfd)->psegm_entries)
#define po_text_pad_words(bfd) (po_tdata (bfd)->text_pad_words)
#define po_headers_computed(bfd) (po_tdata (bfd)->headers_computed)
#define po_section_contents(bfd) (po_tdata (bfd)->section_contents)
#define po_sizes_computed(bfd) (po_tdata (bfd)->sizes_computed)
#define po_elf_offset(bfd) (po_tdata (bfd)->elf_offset)
#define write_ext(buf, abfd) \
(bfd_bwrite ((buf), sizeof (*buf), abfd) != sizeof (*buf))
static bfd_boolean
full_write (const void *buf, bfd_size_type size, bfd *abfd)
{
bfd_size_type res;
while ((res = bfd_bwrite (buf, size, abfd)) == -1UL
&& errno == EINTR);
if (res != size)
return FALSE;
return TRUE;
}
static bfd_boolean
full_read (void *buf, bfd_size_type size, bfd *abfd)
{
bfd_size_type res;
while ((res = bfd_bread (buf, size, abfd)) == -1UL
&& errno == EINTR);
if (res != size)
return FALSE;
return TRUE;
}
static bfd_boolean
add_prdt_entry (bfd *abfd, int r_type, bfd_vma offset, bfd_vma addend);
static void
convert_iso88591_to_ibm1047 (char *ebcdic, char *ascii, bfd_size_type length)
{
for (unsigned i = 0; i < length; i ++)
ebcdic[i] = iso88591_to_ibm1047[(int) ascii[i]];
}
static void
bfd_po_swap_plmh_out (bfd *abfd, struct po_internal_plmh *src, struct po_external_plmh *dst)
{
memset(dst, 0, sizeof(*dst));
memcpy(dst->fixed_eyecatcher, src->fixed_eyecatcher, sizeof(dst->fixed_eyecatcher));
dst->version = src->version;
H_PUT_32 (abfd, src->length, &dst->length);
H_PUT_32 (abfd, src->uncompressed_module_size, &dst->uncompressed_module_size);
H_PUT_32 (abfd, src->rec_decl_count, &dst->rec_decl_count);
}
static void
bfd_po_swap_header_rec_decl_out (bfd *abfd, struct po_internal_header_rec_decl *src, struct po_external_header_rec_decl *dst)
{
memset(dst, 0, sizeof(*dst));
H_PUT_16 (abfd, src->rec_type, &dst->rec_type);
H_PUT_32 (abfd, src->rec_offset, &dst->rec_offset);
H_PUT_32 (abfd, src->rec_length, &dst->rec_length);
}
static void
bfd_po_swap_pmar_out (bfd *abfd, struct po_internal_pmar *src, struct po_external_pmar *dst)
{
memset (dst, 0, sizeof(*dst));
H_PUT_16 (abfd, src->length, &dst->length);
dst->po_level = src->po_level;
dst->binder_level = src->binder_level;
dst->attr1 = src->attr1;
dst->attr2 = src->attr2;
dst->attr3 = src->attr3;
dst->attr4 = src->attr4;
dst->attr5 = src->attr5;
dst->apf_auth_code = src->apf_auth_code;
H_PUT_32 (abfd, src->virtual_storage_required, &dst->virtual_storage_required);
H_PUT_32 (abfd, src->main_entry_point_offset, &dst->main_entry_point_offset);
H_PUT_32 (abfd, src->this_entry_point_offset, &dst->this_entry_point_offset);
dst->change_level_of_member = src->change_level_of_member;
dst->ssi_flag_byte = src->ssi_flag_byte;
memcpy(dst->member_serial_number, src->member_serial_number, sizeof(dst->member_serial_number));
memcpy(dst->extended_attributes, src->extended_attributes, sizeof(dst->extended_attributes));
}
static void
bfd_po_swap_pmarl_out (bfd *abfd, struct po_internal_pmarl *src, struct po_external_pmarl *dst)
{
char userid_ibm1047[8];
memset (dst, 0, sizeof(*dst));
H_PUT_16 (abfd, src->length, &dst->length);
dst->attr1 = src->attr1;
dst->attr2 = src->attr2;
dst->fill_char_value = src->fill_char_value;
dst->po_sublevel = src->po_sublevel;
H_PUT_32 (abfd, src->program_length_no_gas, &dst->program_length_no_gas);
H_PUT_32 (abfd, src->length_text, &dst->length_text);
H_PUT_32 (abfd, src->offset_text, &dst->offset_text);
H_PUT_32 (abfd, src->offset_binder_index, &dst->offset_binder_index);
H_PUT_32 (abfd, src->prdt_length, &dst->prdt_length);
H_PUT_32 (abfd, src->prdt_offset, &dst->prdt_offset);
H_PUT_32 (abfd, src->prat_length, &dst->prat_length);
H_PUT_32 (abfd, src->prat_offset, &dst->prat_offset);
H_PUT_32 (abfd, src->po_virtual_pages, &dst->po_virtual_pages);
H_PUT_32 (abfd, src->ls_loader_data_offset, &dst->ls_loader_data_offset);
H_PUT_16 (abfd, src->loadable_segment_count, &dst->loadable_segment_count);
H_PUT_16 (abfd, src->gas_table_entry_count, &dst->gas_table_entry_count);
H_PUT_32 (abfd, src->virtual_storage_for_first_segment, &dst->virtual_storage_for_first_segment);
H_PUT_32 (abfd, src->virtual_storage_for_second_segment, &dst->virtual_storage_for_second_segment);
H_PUT_32 (abfd, src->offset_to_second_text_segment, &dst->offset_to_second_text_segment);
memcpy (dst->date_saved, src->date_saved, sizeof(dst->date_saved));
memcpy (dst->time_saved, src->time_saved, sizeof(dst->time_saved));
convert_iso88591_to_ibm1047 (userid_ibm1047, src->userid, sizeof(userid_ibm1047));
memcpy (dst->userid, userid_ibm1047, sizeof(dst->userid));
dst->pm3_flags = src->pm3_flags;
dst->cms_flags = src->cms_flags;
H_PUT_16 (abfd, src->deferred_class_count, &dst->deferred_class_count);
H_PUT_32 (abfd, src->deferred_class_total_length, &dst->deferred_class_total_length);
H_PUT_32 (abfd, src->offset_to_first_deferred_class, &dst->offset_to_first_deferred_class);
H_PUT_32 (abfd, src->offset_blit, &dst->offset_blit);
dst->attr3 = src->attr3;
}
static void
bfd_po_swap_po_name_header_out (bfd *abfd,
struct po_internal_po_name_header *src,
struct po_external_po_name_header *dst)
{
memset(dst, 0, sizeof(*dst));
H_PUT_32 (abfd, src->alias_count, &dst->alias_count);
}
static void
bfd_po_swap_po_name_header_entry_out (bfd *abfd,
struct po_internal_po_name_header_entry *src,
struct po_external_po_name_header_entry *dst)
{
memset(dst, 0, sizeof(*dst));
H_PUT_32 (abfd, src->alias_offset, &dst->alias_offset);
H_PUT_16 (abfd, src->alias_length, &dst->alias_length);
dst->flags = src->flags;
memcpy(dst->alias_marker, src->alias_marker, sizeof(dst->alias_marker));
}
static void
bfd_po_swap_prat_out (bfd *abfd,
struct po_internal_prat *src,
struct po_external_prat *dst)
{
memset(dst, 0, sizeof(*dst));
memcpy(dst->fixed_eyecatcher, src->fixed_eyecatcher, sizeof(dst->fixed_eyecatcher));
H_PUT_32 (abfd, src->length, &dst->length);
dst->version = src->version;
H_PUT_32 (abfd, src->occupied_entries, &dst->occupied_entries);
H_PUT_32 (abfd, src->total_entries, &dst->total_entries);
H_PUT_16 (abfd, src->single_entry_length, &dst->single_entry_length);
H_PUT_16 (abfd, src->unknown_flags, &dst->unknown_flags);
}
static void
bfd_po_swap_prdt_out (bfd *abfd,
struct po_internal_prdt *src,
struct po_external_prdt *dst)
{
memset(dst, 0, sizeof(*dst));
memcpy(dst->fixed_eyecatcher, src->fixed_eyecatcher, sizeof(dst->fixed_eyecatcher));
H_PUT_32 (abfd, src->length, &dst->length);
dst->version = src->version;
H_PUT_32 (abfd, src->total_length, &dst->total_length);
}
static void
po_swap_prdt_page_header_out (bfd *abfd,
struct po_internal_prdt_page *src,
struct po_external_prdt_page_header *dst)
{
H_PUT_32 (abfd, src->num, &dst->page_number);
H_PUT_16 (abfd, src->seg_idx, &dst->segment_index);
memcpy(dst->checksum, src->checksum, sizeof(dst->checksum));
H_PUT_16 (abfd, src->count, &dst->reloc_count_total);
}
static void
po_swap_reloc_32_out (bfd *abfd,
const struct po_internal_relent *src,
struct po_external_reloc_32 *dst)
{
/* We only keep the offset from the start of the page. */
unsigned short page_offset = src->offset & 0x0fff;
H_PUT_16 (abfd, page_offset, &dst->offset);
H_PUT_32 (abfd, src->addend, &dst->value);
}
static void
po_swap_reloc_32_ext_out (bfd *abfd,
const struct po_internal_relent *src,
struct po_external_reloc_32_ext *dst)
{
unsigned short page_offset = src->offset & 0x0fff;
dst->type = (unsigned char) src->type;
dst->flags = src->flags;
H_PUT_16 (abfd, page_offset, &dst->offset);
H_PUT_32 (abfd, src->addend, &dst->value);
}
static void
po_swap_reloc_64_out (bfd *abfd,
const struct po_internal_relent *src,
struct po_external_reloc_64 *dst)
{
unsigned short page_offset = src->offset & 0x0fff;
H_PUT_16 (abfd, page_offset, &dst->offset);
H_PUT_64 (abfd, src->addend, &dst->value);
}
static void
po_swap_reloc_64_ext_out (bfd *abfd,
const struct po_internal_relent *src,
struct po_external_reloc_64_ext *dst)
{
unsigned short page_offset = src->offset & 0x0fff;
dst->type = (unsigned char) src->type;
dst->flags = src->flags;
H_PUT_16 (abfd, page_offset, &dst->offset);
H_PUT_64 (abfd, src->addend, &dst->value);
}
static void
bfd_po_swap_lidx_out (bfd *abfd, struct po_internal_lidx *src,
struct po_external_lidx *dst)
{
memset(dst, 0, sizeof(*dst));
memcpy(dst->fixed_eyecatcher, src->fixed_eyecatcher, sizeof(dst->fixed_eyecatcher));
H_PUT_32 (abfd, src->length, &dst->length);
dst->version = src->version;
H_PUT_32 (abfd, src->element_count, &dst->element_count);
}
static void
bfd_po_swap_lidx_entry_out (bfd *abfd, struct po_internal_lidx_entry *src,
struct po_external_lidx_entry *dst)
{
memset(dst, 0, sizeof(*dst));
dst->type = src->type;
H_PUT_32 (abfd, src->entry_length, &dst->entry_length);
H_PUT_32 (abfd, src->entry_offset, &dst->entry_offset);
}
static void
bfd_po_swap_psegm_out (bfd *abfd, struct po_internal_psegm *src,
struct po_external_psegm *dst)
{
memset(dst, 0, sizeof(*dst));
memcpy(dst->fixed_eyecatcher, src->fixed_eyecatcher, sizeof(dst->fixed_eyecatcher));
H_PUT_32 (abfd, src->length, &dst->length);
dst->version = src->version;
H_PUT_32 (abfd, src->entry_count, &dst->entry_count);
}
static void
bfd_po_swap_psegm_entry_out (bfd *abfd,
struct po_internal_psegm_entry *src,
struct po_external_psegm_entry *dst)
{
memset(dst, 0, sizeof(*dst));
H_PUT_32 (abfd, src->length, &dst->length);
H_PUT_32 (abfd, src->offset, &dst->offset);
dst->flags = src->flags;
}
static void
init_reloc_header (bfd *abfd,
enum po_reloc_type type, unsigned char ref_id,
unsigned short relcount,
struct po_external_prdt_reloc_header *header)
{
header->type = (unsigned char) type;
header->reference_id = ref_id;
bfd_h_put_16 (abfd, relcount, &header->reloc_count);
}
static bfd_boolean
po_mkobject (bfd *abfd)
{
/* Make idempodent for the multiple calls in elf_object_p.
z/OS TODO: Make sure that this doesn't break anything like
bfd_reinit() or related functionality. */
if (abfd->direction == read_direction
&& abfd->tdata.any != NULL)
return TRUE;
return bfd_elf_allocate_object (abfd, sizeof (struct po_s390_obj_tdata),
S390_ELF_DATA);
}
/* z/OS TODO: Make sure it's not possible for writes to happen before
elf_backend_begin_write_processing. */
static void
po_begin_write_processing (bfd *abfd,
struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
{
/* We output a full and proper elf executable contained in a
program object wrapper. To do that, we hack the bfd output
mechanisms so it appears we are outputting to an archive,
leaving space for the program object header. */
if (bfd_link_executable (link_info)
|| bfd_link_dll (link_info))
{
po_elf_offset (abfd) = 0;
BFD_ASSERT (abfd->my_archive == NULL);
abfd->my_archive = abfd;
/* abfd->origin = po_s390_tdata (abfd)->po_header_size; */
abfd->origin = po_elf_offset (abfd);
/* NOTE: arelt_data is mostly invalid, it's only there to satisfy
a check inside _bfd_generic_get_section_contents. */
abfd->arelt_data = bfd_zmalloc (sizeof (struct areltdata));
/* arelt_data->parsed_size must be set before a check in bfd_bread
can succeed, which is unrelated to the check mentioned above but
impossible to avoid while arelt_data is nonnull. */
arelt_size (abfd) = -1UL;
if (abfd->arelt_data == NULL)
abort ();
}
}
/* Shift the written contents of the BFD down by the specified
number of bytes. Shifts from po_elf_offset to the end of the
file. */
static bfd_boolean
shift_contents_down (bfd *abfd, ufile_ptr shift)
{
ufile_ptr size;
char *buf;
bfd_size_type this_size;
/* Flush pending I/O so bfd_get_size is accurate. */
bfd_flush (abfd);
if ((size = bfd_get_size (abfd)) == 0)
{
bfd_set_error (bfd_error_file_truncated);
return FALSE;
}
/* This size seems to work well. */
const bfd_size_type bufsize = min (0x8000, size);
if (!(buf = bfd_malloc (bufsize)))
return FALSE;
const ufile_ptr offset = 0;
ufile_ptr cursor = size;
while (cursor > offset)
{
this_size = min (bufsize, cursor - offset);
cursor -= this_size;
if (bfd_seek (abfd, cursor, SEEK_SET) < 0)
return FALSE;
if (!full_read (buf, this_size, abfd))
goto fail_truncated;
if (bfd_seek (abfd, cursor + shift, SEEK_SET) != 0)
goto fail_truncated;
if (!full_write (buf, this_size, abfd))
goto fail_truncated;
}
BFD_ASSERT (cursor == offset);
/* Zero the space between the start start of the file and where the
start of the file has been shifted. */
cursor = 0;
memset (buf, 0, bufsize);
if (bfd_seek (abfd, 0, SEEK_SET) != 0)
goto fail_truncated;
while (cursor < shift)
{
this_size = min (bufsize, shift - cursor);
if (!full_write (buf, this_size, abfd))
goto fail_truncated;
cursor += this_size;
}
BFD_ASSERT (cursor == shift);
free (buf);
return TRUE;
fail_truncated:
free (buf);
bfd_set_error (bfd_error_file_truncated);
return FALSE;
}
/* Special PO relocation processing.
z/OS TODO: This is a bit inefficient, but to do better we would need
our own howto table, which is possible.
z/OS TODO: we need to forbid pc-relative weak relocs (because they are
unimplementable for statically linked programs). */
static bfd_reloc_status_type
po_final_link_relocate (reloc_howto_type *howto,
bfd *input_bfd,
asection *input_section,
bfd_byte *contents,
bfd_vma address,
bfd_vma value,
bfd_vma addend)
{
bfd_vma full_offset;
switch (howto->type)
{
case R_390_32:
case R_390_64:
full_offset = (input_section->output_section->vma
+ input_section->output_offset
+ address);
if (input_section->flags & SEC_LOAD
&& !(value == 0 && addend == 0))
/* Have the OS do a runtime relocation.
z/OS TODO: This shouldn't be necessary for non-static
executables, we will be emitting corresponding ELF
relocs for the dynamic linker anyway. */
add_prdt_entry (input_section->output_section->symbol->the_bfd,
(int) howto->type, full_offset, value + addend);
break;
default:
break;
}
return _bfd_final_link_relocate (howto, input_bfd, input_section,
contents, address, value, addend);
}
/*
* This function finalizes the header of the program object, loading completed internal
* representations into the po_obj_tdata structure. To do so, it traverses the structures
* in order to compute their final lengths, uses these to compute the elements' offsets,
* and substitutes these values in the appropriate locations.
z/OS TODO: We need to reorganize this code so that each line occurs as
early in the function as possible. The dependency graph the code
represents is just too unintelligible otherwise. */
static bfd_boolean
finalize_header (bfd *abfd)
{
unsigned int rec_num = 0;
unsigned int file_pos = 0;
/* All pending writes need to be written before bfd_get_size actually
reflects the size of the file. */
bfd_flush (abfd);
const bfd_vma fsz = bfd_get_size (abfd);
/* z/OS TODO: We load the entire elf file right now. Needless to say,
we need to fix that. */
const bfd_vma load_size = fsz; /* Size of the elf file. */
/* Finalize header */
const unsigned int rec_count = 8;
po_header(abfd).length = PLMH_SIZE(rec_count);
po_header(abfd).rec_decl_count = rec_count;
po_rec_decl_count(abfd) = rec_count;
po_rec_decls(abfd) = bfd_zmalloc2(rec_count, sizeof(struct po_internal_header_rec_decl));
if (po_rec_decls(abfd) == NULL)
return FALSE;
/* Advance past header and record declarations */
file_pos += PLMH_SIZE(rec_count);
/* Create PO name/alias structures */
const char po_name[] = "HELLO ";
const unsigned int aliases = 1;
po_name_header(abfd).alias_count = aliases;
po_name_header_entries(abfd) = bfd_zmalloc2(aliases, PO_NAME_HEADER_ENTRY_SIZE);
if (po_name_header_entries(abfd) == NULL) /* TODO leaks */
return FALSE;
po_rec_decls(abfd)[rec_num ++] = (struct po_internal_header_rec_decl) {
.rec_type = PLMH_REC_TYPE_PO_NAME_HEADER,
.rec_offset = file_pos,
.rec_length = PO_NAME_HEADER_SIZE(aliases)
};
/* Advance past PO name header and entries */
file_pos += PO_NAME_HEADER_SIZE(aliases);
po_name_header_entries(abfd)[0] = (struct po_internal_po_name_header_entry) {
.alias_offset = file_pos,
.alias_length = strlen(po_name),
.flags = 0,
.alias_marker = { 0, 0 }
};
po_names(abfd) = bfd_zmalloc2(aliases, sizeof(char *));
if (po_names(abfd) == NULL)
return FALSE;
po_names(abfd)[0] = bfd_zmalloc(strlen(po_name));
if (po_names(abfd)[0] == NULL)
return FALSE;
memcpy(po_names(abfd)[0], po_name, strlen(po_name));
po_rec_decls(abfd)[rec_num ++] = (struct po_internal_header_rec_decl) {
.rec_type = PLMH_REC_TYPE_PO_NAME,
.rec_offset = file_pos,
.rec_length = po_name_header_entries(abfd)[0].alias_length
};
/* Advance past PO name */
file_pos += po_name_header_entries(abfd)[0].alias_length;
po_rec_decls(abfd)[rec_num ++] = (struct po_internal_header_rec_decl) {
.rec_type = PLMH_REC_TYPE_PMAR,
.rec_offset = file_pos,
.rec_length = PMAR_SIZE + PMARL_SIZE
};
/* Advance past PMAR and PMARL */
file_pos += PMAR_SIZE + PMARL_SIZE;
unsigned int pages_needed = ROUND_UP (load_size, 0x1000) / 0x1000;
/* Finalize the PRAT and PRDT info. */
/* TODO rlds? */
po_prat_entries (abfd) =
bfd_realloc2 (po_prat_entries (abfd), pages_needed + 1, sizeof (prat_ent));
/* We may have reserved room for more pages with relocations in the
PRAT than actually exist, so rectify that now. */
po_prat (abfd).total_entries
= pages_needed = min (po_prat (abfd).total_entries, pages_needed);
po_prat (abfd).length = PRAT_SIZE (pages_needed + 1);
po_prat (abfd).version = PRAT_VERSION;
po_prat (abfd).single_entry_length = PRAT_ENTRY_SIZE;
po_prat (abfd).unknown_flags = 0x00;
/* occupied_entries is already set up. */
memcpy (po_prat (abfd).fixed_eyecatcher, eyecatcher_prat,
sizeof (eyecatcher_prat));
po_prdt (abfd).version = PRDT_VERSION;
po_prdt (abfd).length = PRDT_BASE_SIZE;
memcpy (po_prdt (abfd).fixed_eyecatcher, eyecatcher_prdt,
sizeof (eyecatcher_prdt));
po_pmarl (abfd).prat_length = PRAT_BASE_SIZE;
po_pmarl (abfd).prat_offset = po_prat (abfd).length;
po_rec_decls (abfd)[rec_num++] = (struct po_internal_header_rec_decl) {
.rec_type = PLMH_REC_TYPE_PRAT,
.rec_offset = file_pos,
.rec_length = po_prat (abfd).length
};
/* Advance past PRAT */
file_pos += po_prat (abfd).length;
/* Calculate the size and position of the PRDT.
There's a header for each page, then a generic header for PO_32
and PO_64, in addition to the actual entries for both. PO_32_EXT
and PO_64_EXT have no headers. */
const bfd_vma prdt_offset = file_pos;
unsigned int prdt_pos = PRDT_BASE_SIZE;
po_pmarl(abfd).prdt_offset = prdt_offset;
file_pos += PRDT_BASE_SIZE;
for (unsigned int page = 0; page < pages_needed; page++)
{
unsigned int entries = po_prdt_pages (abfd)[page].count;
bfd_vma page_rel_size = PRDT_PAGE_HEADER_SIZE;
bfd_boolean found32 = FALSE, found64 = FALSE;
if (entries == 0)
{
po_prat_entries (abfd)[page] = 0;
continue;
}
/* z/OS TODO: This will really get complicated once we start using
proper segments. Can we prevent checksum checking by setting
all checksums to noch? */
if (!po_prdt_pages (abfd)[page].no_checksum)
{
char chsum[4];
if (bfd_seek (abfd, 0x1000 * page, SEEK_SET) != 0
|| bfd_bread (chsum, 4, abfd) != 4)
return FALSE;
memcpy (po_prdt_pages (abfd)[page].checksum, chsum, 4);
}
po_prat_entries (abfd)[page] = prdt_pos;
for (unsigned int ent_num = 0; ent_num < entries; ent_num++)
{
switch (po_prdt_pages (abfd)[page].relocs[ent_num].type)
{
case PO_32:
page_rel_size += 6;
found32 = TRUE;
break;
case PO_32_EXT:
page_rel_size += 8;
break;
case PO_64:
page_rel_size += 10;
found64 = TRUE;
break;
case PO_64_EXT:
page_rel_size += 12;
break;
}
}
if (found32)
page_rel_size += PRDT_RELOC_HEADER_SIZE;
if (found64)
page_rel_size += PRDT_RELOC_HEADER_SIZE;
BFD_ASSERT (page_rel_size <= MAX_PAGE_RELOCS_SIZE);
page_rel_size = ROUND_UP (page_rel_size, 4);
file_pos += page_rel_size;
prdt_pos += page_rel_size;
po_prdt (abfd).total_length += page_rel_size;
po_prat (abfd).occupied_entries++;
}
po_pmarl (abfd).prdt_length = po_prdt(abfd).total_length;
po_rec_decls (abfd)[rec_num].rec_type = PLMH_REC_TYPE_PRDT;
po_rec_decls (abfd)[rec_num].rec_offset = prdt_offset;
po_rec_decls (abfd)[rec_num].rec_length = po_prdt (abfd).total_length;
rec_num++;
/* Update PRAT pointers */
po_prat_entries (abfd)[pages_needed] = po_prdt (abfd).total_length;
/* Finalize LIDX */
const unsigned int lidx_elements = 1;
unsigned int lidx_element_num = 0;
po_lidx_entries(abfd) = bfd_zmalloc2(lidx_elements, sizeof(struct po_internal_lidx_entry));
if (po_lidx_entries(abfd) == NULL)
return FALSE;
po_lidx(abfd).element_count = lidx_elements;
po_rec_decls(abfd)[rec_num ++] = (struct po_internal_header_rec_decl) {
.rec_type = PLMH_REC_TYPE_LIDX,
.rec_offset = file_pos,
.rec_length = LIDX_HEADER_BASE_SIZE
};
po_pmarl(abfd).ls_loader_data_offset = file_pos;
/* Advance past LIDX and entries */
file_pos += LIDX_HEADER_SIZE(lidx_elements);
/* Finalize PSEGM */
const unsigned int segments = 1;
po_psegm_entries(abfd) = bfd_zmalloc2(segments, sizeof(struct po_internal_psegm_entry));
if (po_psegm_entries(abfd) == NULL)
return FALSE;
po_psegm(abfd).length = PSEGM_SIZE(segments);
po_psegm(abfd).entry_count =segments;
po_lidx_entries(abfd)[lidx_element_num ++] = (struct po_internal_lidx_entry) {
.type = LIDX_ENTRY_TYPE_PSEGM,
.entry_offset = file_pos,
.entry_length = PSEGM_SIZE(segments)
};
/* Advance past PSEGM */
file_pos += PSEGM_SIZE(segments);
BFD_ASSERT (lidx_element_num == lidx_elements);
/* z/OS TODO: If we were to emit one, we would put the binder index
either here or at the end of the file. Here is more convenient,
but it may have to go after the text for maximum compatibility. */
const unsigned int bxlf_pos = file_pos;
/* Advance past pad */
const unsigned int remainder_words = (16 - (file_pos - (file_pos / 16 * 16))) / 4;
for (unsigned int i = 0; i < remainder_words; i ++)
file_pos += sizeof(text_pad);
po_text_pad_words(abfd) = remainder_words;
/* Page align. */
file_pos = (file_pos + 0x1000 - 1) & ~(0x1000 - 1);
/* Now that the size of the Program Object header has been calculated,
we need to shift the rest of the file down. */
if (!shift_contents_down (abfd, file_pos))
return FALSE;
po_elf_offset (abfd) = file_pos;
/* Finalize entry point */
po_rec_decls(abfd)[rec_num ++] = (struct po_internal_header_rec_decl) {
.rec_type = PLMH_REC_TYPE_ENTRY,
.rec_offset = po_elf_offset (abfd),
.rec_length = load_size
};
file_pos += load_size;
/* z/OS TODO: We don't actually emit any BXLFs (the binder index),
instead we just let the binder index header location point
to somewhere random. While this doesn't cause problems for the
USS loader, it causes other parts of the OS to not be able to
interoperate with our executables. */
po_rec_decls(abfd)[rec_num ++] = (struct po_internal_header_rec_decl) {
.rec_type = PLMH_REC_TYPE_BXLF,
.rec_offset = bxlf_pos,
.rec_length = 0
};
/* Finalize PMAR */
const bfd_size_type module_size = ROUND_UP (fsz, 0x1000);
po_pmar(abfd).virtual_storage_required = load_size;
po_pmar(abfd).main_entry_point_offset = bfd_get_start_address (abfd);
po_pmar(abfd).this_entry_point_offset = bfd_get_start_address (abfd);
/* Finalize PMARL TODO */
po_pmarl(abfd).program_length_no_gas = module_size / 0x1000;
po_pmarl(abfd).length_text = load_size;
po_pmarl (abfd).offset_text = po_elf_offset (abfd);
po_pmarl(abfd).length_binder_index = 0; /* TODO */
po_pmarl(abfd).offset_binder_index = bxlf_pos; /* TODO */
po_pmarl(abfd).po_virtual_pages = module_size / 0x1000;
po_pmarl(abfd).loadable_segment_count = 1; /* TODO */
po_pmarl(abfd).gas_table_entry_count = 0;
po_pmarl (abfd).virtual_storage_for_first_segment =
ROUND_UP (load_size, 0x1000);
po_pmarl(abfd).virtual_storage_for_second_segment = 0;
po_pmarl(abfd).offset_to_second_text_segment = 0;
char date[] = { 0x20, 0x18, 0x10, 0x4F };
char time[] = { 0x01, 0x83, 0x00, 0x5F };
memcpy(po_pmarl(abfd).date_saved, date, sizeof(date));
memcpy(po_pmarl(abfd).time_saved, time, sizeof(time));
po_pmarl(abfd).deferred_class_count = 0;
po_pmarl(abfd).offset_to_first_deferred_class = 0;
po_pmarl(abfd).offset_blit = 0;
/* Last header details */
po_header(abfd).uncompressed_module_size = module_size;
/* Complete PSEGM */
po_psegm_entries(abfd)[0] = (struct po_internal_psegm_entry) {
.length = load_size,
.offset = po_elf_offset (abfd),
.flags = PSEGM_EXECUTABLE | PSEGM_UNKNOWN
};
BFD_ASSERT (rec_num == rec_count);
char zeros[0x1000];
memset(zeros, 0, sizeof (zeros));
bfd_size_type size_delta = ROUND_UP (fsz, 0x1000) - fsz;
if (bfd_seek (abfd, fsz + po_elf_offset (abfd), SEEK_SET) != 0
|| bfd_bwrite (zeros, size_delta, abfd) != size_delta)
return FALSE;
po_headers_computed(abfd) = TRUE;
return TRUE;
}
static ATTRIBUTE_UNUSED bfd_boolean
po_prep_headers (bfd *abfd)
{
po_sizes_computed (abfd) = FALSE;
po_headers_computed (abfd) = FALSE;
/* Initialize internal header */
memcpy(po_header(abfd).fixed_eyecatcher, eyecatcher_plmh, sizeof(eyecatcher_plmh));
po_header(abfd).version = PLMH_VERSION;
/* Initialize PMAR */
po_pmar(abfd).length = PMAR_SIZE;
po_pmar(abfd).po_level = PMAR_PO_LEVEL_PM4;
po_pmar(abfd).binder_level = PMAR_BINDER_LEVEL_B5;
/* Set default PMAR flags */
po_pmar(abfd).attr1 |= PMAR_ATTR1_EXECUTABLE;
po_pmar(abfd).attr2 |= PMAR_ATTR2_BINDER_F_LEVEL_REQ;
po_pmar(abfd).attr2 |= PMAR_ATTR2_ORG0;
// po_pmar(abfd).attr2 |= PMAR_ATTR2_NO_REPROCESS;
/* z/OS NOTE: We always require page alignment. */
po_pmar(abfd).attr3 |= (PMAR_ATTR3_PMARL_PRESENT
| PMAR_ATTR3_PAGE_ALIGNMENT_REQ);
po_pmar(abfd).attr4 |= PMAR_ATTR4_RMODE31;
po_pmar(abfd).attr4 |= PMAR_AMODE64;
/* Initialize PMARL */
po_pmarl(abfd).length = PMARL_SIZE;
/* Set default PMARL flags */
po_pmarl(abfd).attr1 |= PMARL_ATTR1_NO_PDS_CONVERT;
po_pmarl(abfd).attr2 |= PMARL_ATTR2_COMPRESSED;
po_pmarl(abfd).attr2 |= PMARL_ATTR2_SEG1_RMODE31;
memset(po_pmarl(abfd).userid, ' ', sizeof(po_pmarl(abfd).userid));
/* PRAT and PRDT are initialized elsewhere. */
/* Initialize LIDX */
memcpy(po_lidx(abfd).fixed_eyecatcher, eyecatcher_lidx, sizeof(eyecatcher_lidx));
po_lidx(abfd).length = LIDX_HEADER_BASE_SIZE;
po_lidx(abfd).version = LIDX_VERSION;
/* Initialize PSEGM */
memcpy(po_psegm(abfd).fixed_eyecatcher, eyecatcher_psegm, sizeof(eyecatcher_psegm));
po_psegm(abfd).version = PSEGM_VERSION;
return TRUE;
}
static bfd_boolean
bfd_po_output_psegm(bfd *abfd)
{
char psegm[PSEGM_BASE_SIZE];
bfd_po_swap_psegm_out(abfd, &po_psegm(abfd), (struct po_external_psegm *) psegm);
if (bfd_bwrite(psegm, PSEGM_BASE_SIZE, abfd) != PSEGM_BASE_SIZE)
return FALSE;
char psegm_entry[PSEGM_ENTRY_SIZE];
for (unsigned int i = 0; i < po_psegm(abfd).entry_count; i ++)
{
bfd_po_swap_psegm_entry_out(abfd, &po_psegm_entries(abfd)[i], (struct po_external_psegm_entry *) psegm_entry);
if (bfd_bwrite(psegm_entry, PSEGM_ENTRY_SIZE, abfd) != PSEGM_ENTRY_SIZE)
return FALSE;
}
return TRUE;
}
static bfd_boolean
bfd_po_output_header_lidx (bfd *abfd)
{
/* Output LIDX header */
char lidx[LIDX_HEADER_BASE_SIZE];
bfd_po_swap_lidx_out(abfd, &po_lidx(abfd), (struct po_external_lidx *) lidx);
if (bfd_bwrite(lidx, LIDX_HEADER_BASE_SIZE, abfd) != LIDX_HEADER_BASE_SIZE)
return FALSE;
/* Output LIDX header entries */
char lidx_entry[LIDX_HEADER_ENTRY_SIZE];
for (unsigned int i = 0; i < po_lidx(abfd).element_count; i ++)
{
bfd_po_swap_lidx_entry_out(abfd, &po_lidx_entries(abfd)[i], (struct po_external_lidx_entry *) lidx_entry);