summaryrefslogtreecommitdiffstats
path: root/gas/config/obj-coff-seh.c
blob: 06394eb8e43b8fcbe3757739ecd19688a5b9cb92 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
/* seh pdata/xdata coff object file format
   Copyright 2009
   Free Software Foundation, Inc.

   This file is part of GAS.

   GAS 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, or (at your option)
   any later version.

   GAS 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 GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

#include "obj-coff-seh.h"

/* Forward declarations.  */
static seh_kind seh_get_target_kind (void);
static int seh_symbol (bfd *, const char *, const char *, const char *, asection *, int, int);
static void seh_reloc (bfd *, bfd_size_type, int, int);
static void save_relocs (asection *sec);
static asection *quick_section (bfd *abfd, const char *name, int flags, int align);
static void seh_symbol_init (bfd *abfd, unsigned int added);
static void seh_emit_rva (const char *);
static void seh_emit_long (const char *);
static void seh_make_globl (char *);
static segT seh_make_section (void);
static segT seh_make_section2 (const char *section_name, unsigned flags);
static char *seh_make_xlbl_name (seh_context *);
static char *make_seh_text_label (seh_context *c, symbolS **addr);

static void seh_write_text_eh_data (const char *hnd, const char *hnd_data);
static void seh_emit_rva (const char *name);
static int seh_needed_unwind_info (seh_context *);
static void seh_fill_pcsyms (const seh_context *c, char **, int *);
static size_t seh_getelm_data_size (const seh_context *, int, int, size_t *);
static void seh_store_elm_data (const seh_context *, int, int, unsigned char *, valueT base_off);

static size_t seh_getsize_of_unwind_entry (seh_context *, int, int, int);
static void seh_make_unwind_entry (const seh_context *, char *, int, int, int, unsigned char *, size_t *, int);
static size_t seh_getsize_unwind_data (seh_context *);
static void seh_create_unwind_data (seh_context *, unsigned char *, size_t);
static void seh_make_function_entry_xdata (seh_context *, char *, char *, char *, unsigned char *, size_t *,int);
static seh_scope_elem *seh_x64_makescope_elem (seh_context *, const char *, const char *, const char *, const char *);

/* Local data.  */
static asymbol **symtab;
static int symptr;
static arelent *reltab = 0;
static int relcount = 0, relsize = 0;

static seh_context *seh_ctx_root = NULL;
static seh_context *seh_ctx = NULL;
static seh_context *seh_ctx_cur = NULL;

/* Write xdata for arm, sh3, sh4, and ppc.  */

static void
seh_write_text_eh_data (const char *hnd, const char *hnd_data)
{
  if (!hnd || *hnd==0)
    return;
  if (hnd[0] == '@')
    seh_emit_long ("0");
  else
    seh_emit_long (hnd);
  if (!hnd_data || hnd_data[0] == '@')
    seh_emit_long ("0");
  else
    seh_emit_long (hnd_data);
}

/* Generate initial pdata for x64 and mips.  */
static void
make_function_entry_pdata (seh_context *c)
{
  segT sec = NULL;
  segT current_seg = now_seg;
  subsegT current_subseg = now_subseg;

  sec = seh_make_section ();
  switch (seh_get_target_kind ())
    {
    case seh_kind_x64:
      subseg_set (sec, 0);
      seh_emit_rva (c->func_name);
      seh_emit_rva (c->end_symbol);
      seh_emit_rva (c->xdata_first);
      break;
    case seh_kind_mips:
      subseg_set (sec, 0);
      seh_emit_long (c->func_name);
      seh_emit_long (c->end_symbol);
      if (c->handler_name == NULL)
	seh_emit_long ("0");
      else if (c->handler_name[0] == '@')
	{
	  if (strcasecmp (c->handler_name, "@1") == 0)
	    seh_emit_long ("1");
	  else
	    seh_emit_long ("0");
	}
      else
	seh_emit_long (c->handler_name);
      if (c->handler_data_name == NULL || c->handler_data_name[0] == '@')
	seh_emit_long ("0");
      else
	seh_emit_long (c->handler_data_name);
      seh_emit_long (c->endprologue_symbol ? c->endprologue_symbol : c->func_name);
      break;
    default:
      break;
    }
  subseg_set (current_seg, current_subseg);
}

/* Create and write xdata section.  */
static void
seh_x64_write_xdata (void)
{
  seh_context *h;
  size_t xdata_size = 0, count_syms = 0;
  size_t xdata_offs = 0;
  unsigned char *data;
  segT seg_xdata;
  bfd *abfd = stdoutput;

  h = seh_ctx_root;
  if (!h || h->done)
    return;
  while (h != NULL)
    {
      h->xdata_offset = xdata_size;
      xdata_size += seh_getsize_unwind_data (h);
      count_syms += h->count_syms;
      h = h->next;
    }

  if (xdata_size == 0)
    return;

  seh_symbol_init (abfd, count_syms);
  data = xmalloc (xdata_size);
  seg_xdata = quick_section (abfd, ".xdata", SEC_HAS_CONTENTS, 3);
  seg_xdata->contents = data;
  memset (data, 0, xdata_size);
  bfd_set_section_size (abfd, seg_xdata, xdata_size);
  h = seh_ctx_root;
  while (h != NULL)
    {
      xdata_offs = h->xdata_offset;
      h->section = seg_xdata;
      h->abfd = abfd;
      if (h->done == 0)
	{
	  h->done = 1;
	  seh_create_unwind_data (h, data, xdata_offs);
	  h->done = 1;
	}
      h = h->next;
    }
  save_relocs (seg_xdata);
  bfd_set_symtab (abfd, symtab, symptr);
  bfd_set_section_contents (abfd, seg_xdata, data, 0, xdata_size);
}

/* Create pdata section data for arm.  */

static void
seh_arm_create_pdata (seh_context *c, unsigned char *data, size_t pdata_offs)
{
  int idx;
  unsigned int val;
  valueT func_len = 0;
  valueT prolog_len = 0;
  valueT start_len = 0;

  func_len = resolve_symbol_value (c->end_addr);
  start_len = resolve_symbol_value (c->start_addr);
  if (c->endprologue_addr)
    prolog_len = resolve_symbol_value (c->endprologue_addr);
  else
    prolog_len = start_len;
  func_len -= start_len;
  prolog_len -= start_len;
  if (!c || !data)
    return;
  /* $$$$ */
  idx = seh_symbol (c->abfd, c->start_symbol, "", "", UNDSEC, BSF_GLOBAL, 0);
  seh_reloc (c->abfd, pdata_offs, BFD_RELOC_32, idx);
  val = (unsigned int) func_len;
  val <<= 8;
  val |= ((unsigned int) prolog_len & 0xffU);
  if (c->use_instruction_32)
    val |= 0x40000000U;
  if (c->handler_written)
    val |= 0x80000000U;
  bfd_put_32 (c->abfd, (bfd_vma) val, data + pdata_offs + 4);
}

/* Write pdata section for arm.  */
static void
seh_arm_write_pdata (void)
{
  seh_context *h;
  size_t pdata_size = 0, count_syms = 0;
  size_t pdata_offs = 0;
  unsigned char *data;
  segT seg_pdata;
  bfd *abfd = stdoutput;

  h = seh_ctx_root;
  if (h == NULL || h->done)
    return;
  while (h != NULL)
    {
      h->xdata_offset = pdata_size;
      pdata_size += 8;
      count_syms += 1;
      h = h->next;
    }

  if (pdata_size == 0)
    return;

  seh_symbol_init (abfd, count_syms);
  data = xmalloc (pdata_size);
  seg_pdata = quick_section (abfd, ".pdata", SEC_HAS_CONTENTS, 3);
  seg_pdata->contents = data;
  memset (data, 0, pdata_size);
  bfd_set_section_size (abfd, seg_pdata, pdata_size);
  h = seh_ctx_root;
  while (h != NULL)
    {
      pdata_offs = h->xdata_offset;
      h->section = seg_pdata;
      h->abfd = abfd;
      if (h->done != 0)
	{
	  seh_arm_create_pdata (h, data, pdata_offs);
	  h->done = 1;
	}
      h = h->next;
    }
  save_relocs (seg_pdata);
  bfd_set_symtab (abfd, symtab, symptr);
  bfd_set_section_contents (abfd, seg_pdata, data, 0, pdata_size);
}

/* Do object finalization to generate and write pdata/xdata
   for arm and x64 target.  */
void
obj_coff_seh_do_final (void)
{
  switch (seh_get_target_kind ())
    {
    case seh_kind_mips:
    default:
      break;
    case seh_kind_arm:
      seh_arm_write_pdata ();
      break;
    case seh_kind_x64:
      seh_x64_write_xdata ();
      break;
    }
}

/* Enter a prologue element into current context.  */
static void
seh_x64_make_prologue_element (int kind, int reg, bfd_vma off)
{
  seh_prologue_element *n;

  if (seh_ctx_cur == NULL)
    return;
  if (seh_ctx_cur->elems_count == seh_ctx_cur->elems_max)
    {
      seh_ctx_cur->elems = (seh_prologue_element *)
	xrealloc (seh_ctx_cur->elems,
		  ((seh_ctx_cur->elems_max + 8) * sizeof (seh_prologue_element)));
      seh_ctx_cur->elems_max += 8;
    }
  n = &seh_ctx_cur->elems[seh_ctx_cur->elems_count];
  memset (n, 0, sizeof (seh_prologue_element));
  n->kind = kind;
  n->reg = reg;
  n->offset = off;
  n->pc_symbol = make_seh_text_label (seh_ctx_cur, &(n->pc_addr));
  seh_ctx_cur->elems_count += 1;
}

/* Helper to read a register name from input stream.  */
static int
seh_x64_read_reg (const char *tok, int kind, int *regno)
{
  static const char *frame_regs[16] =
    { "cfa", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
      "r8","r9","r10","r11","r12","r13","r14","r15" };
  static const char *int_regs[16] =
    { "rax", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
      "r8","r9","r10","r11","r12","r13","r14","r15" };
  static const char *xmm_regs[16] =
    { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
      "xmm8", "xmm9", "xmm10","xmm11","xmm12","xmm13","xmm14","xmm15" };
  static const char *mm_regs[16] =
    { "xmm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
      "xmm8", "mm9", "mm10","mm11","mm12","mm13","mm14","mm15" };
  const char **p = NULL;
  char name_end;
  char *symbol_name = NULL;
  int i;

  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
    input_line_pointer++;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
    input_line_pointer++;
  switch (kind)
    {
    case 0:
      p = frame_regs;
      break;
    case 1:
      p = int_regs;
      break;
    case 2:
      p = mm_regs;
      break;
    case 3:
      p = xmm_regs;
      break;
    default:
      abort ();
    }

  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
    return 0;

  if (*input_line_pointer == '%')
    ++input_line_pointer;
  symbol_name = input_line_pointer;
  name_end = get_symbol_end ();

  for (i = 0; i < 16; i++)
    if (! strcasecmp (p[i], symbol_name))
      break;

  if (i == 16)
    as_warn (_("In %s we found the invalid register name %s.\n"),
	     tok, symbol_name);

  *input_line_pointer = name_end;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
    input_line_pointer++;
  if (*input_line_pointer == ',')
    ++input_line_pointer;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
    input_line_pointer++;
  *regno = i;
  return i != 16;
}

/* Read an numeric value from input stream.  */
static int
seh_read_offset (const char *tok, bfd_vma *off)
{
  bfd_vma r, v = 0, base = 10;
  int had_one = 0;
  int neg = 0;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
    input_line_pointer++;
  if (*input_line_pointer == '$')
    ++input_line_pointer;
  if (*input_line_pointer == '-')
    {
      neg = 1;
      ++input_line_pointer;
    }
  else if (*input_line_pointer == '+')
    {
      ++input_line_pointer;
    }
  if (*input_line_pointer == '0')
    {
      ++input_line_pointer;
      had_one = 1;
      base = 8;
      switch ((*input_line_pointer))
	{
	case 'x':
	case 'X':
	  base = 16;
	  ++input_line_pointer;
	  break;
	case 'd':
	case 'D':
	  base = 10;
	  input_line_pointer++;
	  break;
	case 'o':
	case 'O':
	  base = 8;
	  input_line_pointer++;
	  break;
	}
    }
  while (*input_line_pointer != 0)
    {
      if (input_line_pointer[0] >= '0' && input_line_pointer[0] <='9')
	r = (bfd_vma) (input_line_pointer[0] - '0');
      else if (base == 16 && input_line_pointer[0] >= 'a' && input_line_pointer[0] <='f')
	r = (bfd_vma) ((input_line_pointer[0] - 'a') + 10);
      else if (base == 16 && input_line_pointer[0] >= 'A' && input_line_pointer[0] <='F')
	r = (bfd_vma) ((input_line_pointer[0] - 'A') + 10);
      else
	break;
      input_line_pointer++;
      v *= base;
      v += r;
      had_one = 1;
    }

  if (neg)
    v = (bfd_vma) (v ^ ((bfd_vma) -1LL)) + (bfd_vma) 1ULL;
  *off = v;
  if (had_one == 0)
    {
      as_warn (_("In %s we expect a number.\n"),
	       tok);
    }
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
    input_line_pointer++;
  if (*input_line_pointer == ',')
    ++input_line_pointer;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t')
    input_line_pointer++;
  return had_one != 0;
}

/* Mark current context to use 32-bit instruction (arm).  */
static void
obj_coff_seh_32 (int what)
{
  if (seh_ctx_cur == NULL)
    {
      as_fatal (_(".seh_eh requires to be in .seh_proc/.seh_endproc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  seh_ctx_cur->use_instruction_32 = (what ? 1 : 0);
  if (seh_get_target_kind () == seh_kind_arm)
    as_warn (_(".seh_%s32 is ignored for this target."), (what ? "" : "no"));
  demand_empty_rest_of_line ();
}

/* Set for current context the handler and optional data (arm).  */
static void
obj_coff_seh_eh (int what ATTRIBUTE_UNUSED)
{
  if (seh_ctx_cur == NULL)
    {
      as_fatal (_(".seh_eh requires to be in .seh_proc/.seh_endproc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  if (seh_get_target_kind () == seh_kind_arm)
    {
      seh_ctx_cur->handler_written = 1;
      /* write block to .text if exception handler is set.  */
      seh_write_text_eh_data (seh_ctx_cur->handler_name, seh_ctx_cur->handler_data_name);
    }
  demand_empty_rest_of_line ();
}

/* Set for current context the default handler (x64).  */
static void
obj_coff_seh_handler (int what ATTRIBUTE_UNUSED)
{
  char *symbol_name;
  char name_end;

  if (seh_ctx_cur == NULL)
    {
      as_fatal (_(".seh_handler requires to be in .seh_proc/.seh_endproc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
    {
      as_fatal (_(".seh_handler requires a handler lable name.\n"));
      demand_empty_rest_of_line ();
      return;
    }

  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  symbol_name = input_line_pointer;
  name_end = get_symbol_end ();
  seh_ctx->handler_name = xstrdup (symbol_name);
  if (symbol_name[0] == '@')
    {
      if (strcasecmp (symbol_name, "@0") != 0 && strcasecmp (symbol_name, "@1") != 0
	  && strcasecmp (symbol_name, "@null") != 0)
	as_warn (_("Unknown constant value ,%s' for handler."), symbol_name);
    }
  *input_line_pointer = name_end;
  seh_ctx->handler_data_name = NULL;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  symbol_name = input_line_pointer;
  if (*input_line_pointer != '\n' && *input_line_pointer != 0)
    {
      name_end = get_symbol_end ();
      seh_ctx->handler_data_name = xstrdup (symbol_name);
      if (symbol_name[0] == '@')
	{
	  if (seh_get_target_kind () != seh_kind_x64)
	    as_fatal (_("For this target .seh_handler doesn't support constant user-data."));
	  else if (strcasecmp (symbol_name, "@unwind") != 0 &&
	           strcasecmp (symbol_name, "@except") != 0)
	    as_warn (_("For .seh_handler the constant ,%s' is ignored."), symbol_name);
	}
      *input_line_pointer = name_end;
    }
  if (seh_ctx_cur->handler_written)
    as_warn (_(".seh_handler is ignored as .seh_eh was seen before."));
  demand_empty_rest_of_line ();
}

/* Add a scope-element for exception region (x64).  */
static void
obj_coff_seh_scope (int what ATTRIBUTE_UNUSED)
{
  char *symbol_name,*beg = NULL,*end = NULL, *handl = NULL, *jmp = NULL;
  char name_end;

  if (seh_ctx_cur == NULL)
    {
      as_fatal (_(".seh_scope requires to be in .seh_proc/.seh_endproc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }

  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
    {
      as_fatal (_(".seh_scope requires four symbol names.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  symbol_name = input_line_pointer;
  name_end = get_symbol_end ();
  beg = xstrdup (symbol_name);
  *input_line_pointer = name_end;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
    {
      as_fatal (_(".seh_scope requires three more symbol names.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  symbol_name = input_line_pointer;
  name_end = get_symbol_end ();
  end = xstrdup (symbol_name);
  *input_line_pointer = name_end;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
    {
      as_fatal (_(".seh_scope requires two more symbol names.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  symbol_name = input_line_pointer;
  name_end = get_symbol_end ();
  handl = xstrdup (symbol_name);
  *input_line_pointer = name_end;
  if (*handl == '@')
    {
      if (strcasecmp (handl, "@0") != 0 && strcasecmp (handl, "@1") != 0
	  && strcasecmp (handl, "@null") != 0)
	as_warn (_("Unknown constant for handler ,%s'."), handl);
    }

  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
    {
      as_fatal (_(".seh_scope requires one more symbol names.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  symbol_name = input_line_pointer;
  name_end = get_symbol_end ();
  jmp = xstrdup (symbol_name);
  *input_line_pointer = name_end;
  if (*jmp == '@')
    {
      if (strcasecmp (jmp, "@0") != 0 && strcasecmp (handl, "@null") != 0)
	as_warn (_("Unknown constant for jump ,%s'."), jmp);
    }

  if (seh_get_target_kind () != seh_kind_x64)
    as_warn (_(".seh_scope is ignored for this target."));
  else
    seh_x64_makescope_elem (seh_ctx_cur, beg, end, handl, jmp);
  if (beg)
    free (beg);
  if (end)
    free (end);
  if (handl)
    free (handl);
  if (jmp)
    free (jmp);
  demand_empty_rest_of_line ();
}

/* Mark begin of new context.  */
static void
obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)
{
  char *symbol_name;
  char name_end;

  if (seh_ctx_cur != NULL)
    {
      as_warn (_(".seh_proc has to be closed by .seh_endprog\n"));
      obj_coff_seh_endproc (0);
    }

  if (*input_line_pointer == 0 || *input_line_pointer == '\n')
    {
      as_fatal (_(".seh_proc requires function lable name.\n"));
      demand_empty_rest_of_line ();
      return;
    }

  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  symbol_name = input_line_pointer;
  name_end = get_symbol_end ();

  if (seh_ctx == NULL)
    seh_ctx_root = seh_ctx = (seh_context *) xmalloc (sizeof (seh_context));
  else
    {
      seh_ctx->next = (seh_context *) xmalloc (sizeof (seh_context));
      seh_ctx = seh_ctx->next;
    }
  seh_ctx_cur = seh_ctx;
  memset (seh_ctx, 0, sizeof (seh_context));

  seh_ctx->func_name = xstrdup (symbol_name);
  *input_line_pointer = name_end;
  while (*input_line_pointer == ' ' || *input_line_pointer == '\t' || *input_line_pointer == ',')
    input_line_pointer++;
  seh_ctx->start_symbol = make_seh_text_label (seh_ctx_cur, &(seh_ctx_cur->start_addr));
  demand_empty_rest_of_line ();
}

/* Mark end of current context.  */
static void
obj_coff_seh_endproc  (int what ATTRIBUTE_UNUSED)
{
  if (seh_ctx_cur == NULL)
    {
      as_warn (_(".seh_endprog without prior .seh_proc (ignored)\n"));
      demand_empty_rest_of_line ();
      return;
    }
  seh_ctx->end_symbol = make_seh_text_label (seh_ctx, &(seh_ctx->end_addr));
  seh_ctx->xdata_first = seh_make_xlbl_name (seh_ctx);
  make_function_entry_pdata (seh_ctx);
  seh_ctx_cur = NULL;
  demand_empty_rest_of_line ();
}

/* Add a push-unwind-information to current context.
   what:0 push volatile reg, 1: push machine frame, 2: set fp register.  */
static void
obj_coff_seh_push  (int what)
{
  int reg = 0;
  int kind = -1;
  bfd_vma off = 0;

  if (seh_ctx_cur == NULL)
    {
      as_warn (_(".seh_push used outside of .seh_proc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  /* What 0:reg, 1:pushframe.  */
  switch (what)
    {
    case 0:
      if (seh_x64_read_reg (".seh_pushreg", 1, &reg))
	kind = UWOP_PUSH_NONVOL;
      else
	as_warn (_(".seh_pushreg expects register argument."));
      break;
    case 1:
      kind = UWOP_PUSH_MACHFRAME;
      break;
    default:
      abort ();
    }

  if (seh_get_target_kind () != seh_kind_x64)
    as_warn (_(".seh_save... is ignored for this target.\n"));
  else if (kind != -1)
    seh_x64_make_prologue_element (kind, reg, off);
  demand_empty_rest_of_line ();
}

/* Add a register save-unwind-information to current context.  */
static void
obj_coff_seh_save  (int what)
{
  int reg;
  bfd_vma off;
  int kind;
  int ok = 1;

  /* what 0:reg, 1:mm, 2:xmm.  */
  switch (what)
    {
    case 0:
      ok &= seh_x64_read_reg (".seh_savereg", 1, &reg);
      kind = UWOP_SAVE_NONVOL;
      break;
    case 1:
      ok &= seh_x64_read_reg (".seh_savemm", 2, &reg);
      kind = UWOP_SAVE_XMM;
      break;
    case 2:
      ok &= seh_x64_read_reg (".seh_savexmm", 3, &reg);
      kind = UWOP_SAVE_XMM128;
      break;
    default:
      abort ();
    }
  ok &= seh_read_offset (".seh_save", &off);
  if (seh_ctx_cur == NULL)
    {
      as_warn (_(".seh_save used outside of .seh_proc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  if (seh_get_target_kind () != seh_kind_x64)
    as_warn (_(".seh_save... is ignored for this target.\n"));
  else
    seh_x64_make_prologue_element (kind, reg, off);
  demand_empty_rest_of_line ();
}

/* Mark end of prologue for current context.  */
static void
obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED)
{
  if (seh_ctx_cur == NULL)
    {
      as_warn (_(".seh_endprologue used outside of .seh_proc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  if (seh_ctx_cur->endprologue_symbol != NULL)
    as_warn (_(".seh_endprologue used more then once in .seh_proc block.\n"));
  else
    seh_ctx_cur->endprologue_symbol = make_seh_text_label (seh_ctx_cur, &seh_ctx_cur->endprologue_addr);
}

/* Add a stack-allocation-unwind-information for current context.  */
static void
obj_coff_seh_stack_alloc (int what ATTRIBUTE_UNUSED)
{
  bfd_vma size;

  if (seh_ctx_cur == NULL)
    {
      as_warn (_(".seh_stackalloc used outside of .seh_proc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  if (seh_read_offset (".seh_stackalloc", &size))
    {
      if (seh_get_target_kind () != seh_kind_x64)
	as_warn (_(".seh_stackalloc is ignored for this target.\n"));
      else
	seh_x64_make_prologue_element (UWOP_ALLOC_LARGE, 0, size);
    }
}

/* Add frame-pointer-unwind-information to current context.  */
static void
obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED)
{
  int kind = -1;
  int reg;
  int ok = 1;
  bfd_vma off;

  ok &= seh_x64_read_reg (".seh_setframe", 0, &reg);
  ok &= seh_read_offset (".seh_setframe", &off);
  if (seh_ctx_cur == NULL)
    {
      as_warn (_(".seh_setframe used outside of .seh_proc block.\n"));
      demand_empty_rest_of_line ();
      return;
    }
  if (ok)
    {
      seh_ctx_cur->framereg = reg;
      seh_ctx_cur->frameoff = off;
      kind = UWOP_SET_FPREG;
    }
  if (seh_get_target_kind () != seh_kind_x64)
    as_warn (_(".seh_setframe is ignored for this target.\n"));
  else if (kind != -1)
    seh_x64_make_prologue_element (kind, reg, off);
  demand_empty_rest_of_line ();
}

/* Misc function helpers.  */
static void
seh_reloc (bfd *abfd, bfd_size_type address, int which_howto, int symidx)
{
  if (relcount >= relsize - 1)
    {
      relsize += 10;
      if (reltab)
	reltab = xrealloc (reltab, relsize * sizeof (arelent));
      else
	reltab = xmalloc (relsize * sizeof (arelent));
    }
  reltab[relcount].address = address;
  reltab[relcount].addend = 0;
  reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
  reltab[relcount].sym_ptr_ptr = symtab + symidx;
  relcount++;
}

static void
save_relocs (asection *sec)
{
  int i;

  sec->relocation = reltab;
  sec->reloc_count = relcount;
  sec->orelocation = xmalloc ((relcount + 1) * sizeof (arelent *));
  for (i = 0; i < relcount; i++)
    sec->orelocation[i] = sec->relocation + i;
  sec->orelocation[relcount] = 0;
  sec->flags |= SEC_RELOC;
  reltab = 0;
  relcount = relsize = 0;
}

static void
seh_symbol_init (bfd *abfd, unsigned int added)
{
  unsigned int oldcount;

  oldcount = bfd_get_symcount (abfd);
  symptr = oldcount;
  symtab = xmalloc ((oldcount + added + 6) * sizeof (asymbol *));
  if (oldcount > 0)
    memcpy (symtab, bfd_get_outsymbols (abfd), sizeof (asymbol *) * oldcount);
}

static int
seh_symbol (bfd *abfd, const char *n1, const char *n2, const char *n3,
            asection *sec, int flags, int addr)
{
  asymbol *sym;
  char *name = xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
  int ret = symptr;

  strcpy (name, n1);
  strcat (name, n2);
  strcat (name, n3);
  sym = bfd_make_empty_symbol (abfd);
  sym->name = name;
  sym->section = sec;
  sym->flags = flags;
  sym->value = addr;
  symtab[symptr++] = sym;
  return ret;
}

static asection *
quick_section (bfd *abfd, const char *name, int flags, int align)
{
  asection *sec;
  asymbol *sym;

  sec = seh_make_section2 (name, flags);
  bfd_set_section_alignment (abfd, sec, align);
  /* Remember to undo this before trying to link internally!  */

  sym = bfd_make_empty_symbol (abfd);
  symtab[symptr++] = sym;
  sym->name = sec->name;
  sym->section = sec;
  sym->flags = BSF_LOCAL;
  sym->value = 0;

  return sec;
}

static seh_kind
seh_get_target_kind (void)
{
  if (!stdoutput)
    return seh_kind_unknown;
  switch (bfd_get_arch (stdoutput))
    {
    case bfd_arch_arm:
    case bfd_arch_powerpc:
    case bfd_arch_sh:
      return seh_kind_arm;
    case bfd_arch_i386:
      switch (bfd_get_mach (stdoutput))
	{
	case bfd_mach_x86_64:
	case bfd_mach_x86_64_intel_syntax:
	  return seh_kind_x64;
	default:
	  break;
	}
      /* FALL THROUGH.  */
    case bfd_arch_mips:
      return seh_kind_mips;
    case bfd_arch_ia64:
      /* Should return seh_kind_x64.  But not implemented yet.  */
      return seh_kind_unknown;
    default:
      break;
    }
  return seh_kind_unknown;
}

static void
seh_emit_rva (const char *name)
{
  char *p = (char *) xmalloc (strlen (name) + 1);
  char *s = input_line_pointer;

  strcpy (p, name);
  input_line_pointer = p;
  s_rva (4);
  input_line_pointer = s;
}

static void
seh_emit_long (const char *name)
{
  char *p = (char *) xmalloc (strlen (name) + 1);
  char *s = input_line_pointer;

  strcpy (p, name);
  input_line_pointer = p;
  cons (4);
  input_line_pointer = s;
}

static void
seh_make_globl (char *sym_name)
{
  char *s = input_line_pointer;

  input_line_pointer = sym_name;
  s_globl (4);
  input_line_pointer = s;
}

static segT
seh_make_section2 (const char *section_name, unsigned flags)
{
  char *name;
  segT sec;

  name = xmalloc (strlen (section_name) + 1);
  strcpy (name, section_name);

  sec = subseg_new (name, (subsegT) 0);
  bfd_set_section_flags (stdoutput, sec,
			 ((SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA | flags)
			  & bfd_applicable_section_flags (stdoutput)));

  return sec;
}

static segT
seh_make_section (void)
{
  static segT seg_pdata = NULL;
  segT sec = NULL;

  if (!seg_pdata)
    seg_pdata = seh_make_section2 (".pdata", 0);
  sec = seg_pdata;
  return sec;
}

static char *
seh_make_xlbl_name (seh_context *c)
{
  size_t len = strlen (".seh_xlbl_") + strlen (c->func_name) + 9 + 1;
  char *ret = (char*) xmalloc (len);

  if (!ret)
    as_fatal (_("Out of memory for xdata lable for %s"), c->func_name);
  else
    sprintf (ret, ".seh_xlbl_%s_%x", c->func_name, + c->xlbl_count);
  c->xlbl_count += 1;
  return ret;
}

static char *
make_seh_text_label (seh_context *c, symbolS **addr)
{
  char *sym_name;
  size_t len = strlen (".seh_tlbl_") + strlen (c->func_name) + 9 + 1;

  sym_name = (char *) xmalloc (len);
  if (!sym_name)
    as_fatal (_("Allocating memory for SEH's text symbol for %s failed"), c->func_name);
  sprintf (sym_name, ".seh_tlbl_%s_%x", c->func_name, c->tlbl_count);
  c->tlbl_count += 1;
  if (addr)
    {
      seh_make_globl (sym_name);
      *addr = colon (sym_name);
    }
  return sym_name;
}

/* x64 secific functions.  */

static void
seh_fill_pcsyms (const seh_context *c, char **names, int *idx)
{
  size_t i;
  int count = 1;
  valueT start_off = resolve_symbol_value (c->start_addr);
  valueT un_off;
  seh_prologue_element *e = c->elems;

  names[0] = c->start_symbol;
  idx[0] = 0;
  if (c->elems_count == 0)
    return;
  for (i = 0; i < c->elems_count; i++)
    {
      un_off = resolve_symbol_value (e[i].pc_addr);
      if ((un_off - start_off) > 255)
	{
	  names[count] = e[i].pc_symbol;
	  idx[count] = (int) i;
	  count++;
	  start_off = un_off;
	}
    }
}

static int
seh_needed_unwind_info (seh_context *c)
{
  size_t i;
  int count = 1;
  valueT start_off = resolve_symbol_value (c->start_addr);
  valueT un_off;
  seh_prologue_element *e = c->elems;

  if (c->elems_count == 0)
    return count;
  for (i = 0; i < c->elems_count; i++)
    {
      un_off = resolve_symbol_value (e[i].pc_addr);
      if ((un_off - start_off) > 255)
	{
	  count++;
	  start_off = un_off;
	}
    }
  return count;
}

static void
seh_store_elm_data (const seh_context *c, int elm_start,int elm_end, unsigned char *puwop, valueT base)
{
  int i = elm_end;
  valueT off;

  /* We have to store in reverse order.  */
  while (i > elm_start)
    {
      --i;
      /* First comes byte offset in code.  */
      off = resolve_symbol_value (c->elems[i].pc_addr) - base;
      *puwop++ = (unsigned char) off;
      switch (c->elems[i].kind)
	{
        case UWOP_PUSH_NONVOL:
	  *puwop = UWOP_PUSH_NONVOL | (c->elems[i].reg << 4);
	  puwop++;
	  break;
	case UWOP_SET_FPREG:
	  *puwop++ = UWOP_SET_FPREG;
	  break;
	case UWOP_PUSH_MACHFRAME:
	  *puwop = UWOP_PUSH_MACHFRAME | (c->elems[i].reg << 4);
	  puwop++;
	  break;
	case UWOP_SAVE_NONVOL:
	  if ((c->elems[i].offset & 7) != 0 ||
	    ((c->elems[i].offset / 8) > 0xffff))
	  {
	    *puwop++ = UWOP_SAVE_NONVOL_FAR | (c->elems[i].reg << 4);
	    bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
	    puwop += 4;
	  }
	  else
	  {
	    *puwop++ = UWOP_SAVE_NONVOL | (c->elems[i].reg << 4);
	    bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
	    puwop += 2;
	  }
	  break;
	case UWOP_SAVE_XMM:
	  if ((c->elems[i].offset & 7) != 0 ||
	    ((c->elems[i].offset / 8) > 0xffff))
	  {
	    *puwop++ = UWOP_SAVE_XMM_FAR | (c->elems[i].reg << 4);
	    bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
	    puwop += 4;
	  }
	  else
	  {
	    *puwop++ = UWOP_SAVE_XMM | (c->elems[i].reg << 4);
	    bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
	    puwop += 2;
	  }
	  break;
	case UWOP_SAVE_XMM128:
	  if ((c->elems[i].offset & 7) != 0 ||
	    ((c->elems[i].offset / 8) > 0xffff))
	  {
	    *puwop++ = UWOP_SAVE_XMM128_FAR | (c->elems[i].reg << 4);
	    bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
	    puwop += 4;
	  }
	  else
	  {
	    *puwop++ = UWOP_SAVE_XMM128 | (c->elems[i].reg << 4);
	    bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
	    puwop += 2;
	  }
	  break;
	case UWOP_ALLOC_LARGE:
	  if ((c->elems[i].offset & 7) != 0 ||
	    ((c->elems[i].offset / 8) > 0xffff))
	  {
	    *puwop++ = UWOP_ALLOC_LARGE | (1 << 4);
	    bfd_putl32 ((bfd_vma) c->elems[i].offset, puwop);
	    puwop += 4;
	  }
	  else if ((c->elems[i].offset / 8) <= 0x10)
	  {
	    *puwop++ = UWOP_ALLOC_SMALL | (((c->elems[i].offset / 8) - 1) << 4);
	  }
	  else
	  {
	    *puwop++ = UWOP_ALLOC_LARGE;
	    bfd_putl16 ((bfd_vma) (c->elems[i].offset / 8), puwop);
	    puwop += 2;
	  }
	  break;
        default:
	  puwop++;
	  abort ();
	  break;
	}
    }
}

static size_t
seh_getelm_data_size (const seh_context *c, int elm_start, int elm_end, size_t *unaligned_uwcodes)
{
  size_t ret = 0;

  while (elm_start < elm_end)
    {
      switch (c->elems[elm_start].kind)
	{
        case UWOP_PUSH_NONVOL:
	case UWOP_PUSH_MACHFRAME:
	  ret += 2;
	  break;
	case UWOP_SAVE_NONVOL:
	case UWOP_SAVE_XMM:
	case UWOP_SAVE_XMM128:
	  if ((c->elems[elm_start].offset & 7) != 0 ||
	      ((c->elems[elm_start].offset / 8) > 0xffff))
	    ret += 6;
	  else
	    ret += 4;
	  break;
	case UWOP_ALLOC_LARGE:
	  if ((c->elems[elm_start].offset & 7) != 0 ||
	      ((c->elems[elm_start].offset / 8) > 0xffff))
	    ret += 6;
	  else if ((c->elems[elm_start].offset / 8) <= 0x10)
	    ret += 2;
	  else
	    ret += 4;
	  break;
	case UWOP_SET_FPREG:
	  ret += 2;
	  break;
        default:
	  ret += 2;
	  break;
	}
      elm_start++;
    }
  if (unaligned_uwcodes)
    *unaligned_uwcodes = ret;
  ret = PEX64_UWI_SIZEOF_UWCODE_ARRAY ((ret / 2));
  return ret;
}

static size_t
seh_getsize_of_unwind_entry (seh_context *c, int elm_start, int elm_end, int bechain)
{
  size_t ret = seh_getelm_data_size(c, elm_start, elm_end, NULL);

  c->count_syms += 1;
  if (bechain)
    {
      ret += 4 + 4;
      c->count_syms += 1;
      c->count_reloc += 1;
    }
  else
    {
      ret += 4;
      if (c->handler_name != NULL)
	{
	  if (c->handler_data_name != NULL
	      && c->handler_data_name[0] != '@')
	    {
	      ret += 4;
	      c->count_syms += 2;
	      c->count_reloc += 2;
	    }
	  else
	    {
	      ret += 8 + (c->scope_count * 4) * 4;
	      c->count_syms += (c->scope_count * 4) + 1;
	      c->count_reloc += (c->scope_count * 4) + 1;
	    }
	}
    }
  return ret;
}

static void
seh_make_unwind_entry (const seh_context *c, char *name, int elm_start, int elm_end, int bechain,
		       unsigned char *data, size_t *poffs, int no)
{
  size_t off = *poffs;
  size_t it;
  valueT start_off = resolve_symbol_value (c->start_addr);
  valueT end_prologue;
  size_t unaligned_uwcodes = 0;
  size_t uwcodes = seh_getelm_data_size(c, elm_start, elm_end, &unaligned_uwcodes);
  unsigned int flag = UNW_FLAG_NHANDLER;
  int idx;

  if (c->handler_name != NULL)
    {
      flag = UNW_FLAG_EHANDLER;
      if (c->handler_data_name != NULL && c->handler_data_name[0] != '@')
	flag = UNW_FLAG_FHANDLER;
      else if (c->handler_data_name != NULL &&
	       strcasecmp (c->handler_data_name, "@unwind") == 0)
	flag = UNW_FLAG_UHANDLER;
    }
  if (!c->endprologue_addr)
    end_prologue = start_off;
  else
    end_prologue = resolve_symbol_value (c->endprologue_addr);
  seh_symbol (c->abfd, name, "", "", c->section, BSF_GLOBAL, (int) off);
  data[off++] = (1 | ((bechain ? UNW_FLAG_CHAININFO : flag) << 3));
  if (elm_start != 0)
    start_off = (valueT) c->elems[elm_start].offset;
  end_prologue -= start_off;
  if (end_prologue > 255)
    end_prologue = 255;
  data[off++] = (unsigned char) end_prologue;
  data[off++] = (unsigned char) (unaligned_uwcodes / 2);
  data[off] = (unsigned char) c->framereg;
  data[off++] |= (unsigned char) ((c->frameoff / 16) << 4);
  seh_store_elm_data (c, elm_start, elm_end, &data[off], start_off);
  off += uwcodes;
  if (bechain)
    {
      char n[100];

      sprintf (n,"%x", no);
      idx = seh_symbol (c->abfd, ".xdata_fct", c->func_name, n, UNDSEC, BSF_GLOBAL, (int) off);
      seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
      off += 4;
    }
  else if (c->handler_name != NULL)
    {
      if (flag == UNW_FLAG_FHANDLER)
	{
	  if (strcasecmp (c->handler_name, "@1") == 0)
	    bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
	  else if (c->handler_name[0] != '@')
	    {
	      idx = seh_symbol (c->abfd, c->handler_name, "", "", UNDSEC, BSF_GLOBAL, 0);
	      seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
	    }
	  off += 4;
	  idx = seh_symbol (c->abfd, c->handler_data_name, "", "", UNDSEC, BSF_GLOBAL, 0);
	  seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
	  off += 4;
	}
      else if (flag == UNW_FLAG_UHANDLER || flag == UNW_FLAG_EHANDLER)
	{
	  if (strcasecmp (c->handler_name, "@1") == 0)
	    bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
	  else if (c->handler_name[0] != '@')
	    {
	      idx = seh_symbol (c->abfd, c->handler_name, "", "", UNDSEC, BSF_GLOBAL, 0);
	      seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
	    }
	  off += 4;
	  bfd_put_32 (c->abfd, (bfd_vma) c->scope_count, &data[off]);
	  off += 4;
	  for (it = 0; it < c->scope_count; it++)
	    {
	      idx = seh_symbol (c->abfd, c->scopes[it].begin_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
	      seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
	      off += 4;
	      idx = seh_symbol (c->abfd, c->scopes[it].end_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
	      seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
	      off += 4;
	      if (c->scopes[it].handler_addr[0] == '@')
		{
		  if (strcasecmp (c->scopes[it].handler_addr, "@1") == 0)
		    bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
		}
	      else
		{
		  idx = seh_symbol (c->abfd, c->scopes[it].handler_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
		  seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
		}
	      off += 4;
	      if (c->scopes[it].jump_addr[0] == '@')
		{
		  if (strcasecmp (c->scopes[it].jump_addr, "@1") == 0)
		    bfd_put_32 (c->abfd, (bfd_vma) 1, &data[off]);
		}
	      else
		{
		  idx = seh_symbol (c->abfd, c->scopes[it].jump_addr, "", "", UNDSEC, BSF_GLOBAL, 0);
		  seh_reloc (c->abfd, off, BFD_RELOC_RVA, idx);
		}
	      off += 4;
	    }
	}
    }
  *poffs = off;
}

static size_t
seh_getsize_unwind_data (seh_context *c)
{
  int need = seh_needed_unwind_info (c);
  int i;
  char **names = (char **) xmalloc (sizeof (char *) * need);
  char **pc_syms = (char **) xmalloc (sizeof (char *) * need);
  int *elm_start = (int *) xmalloc (sizeof (int) * (need + 1));
  size_t xdata_sz = 0;

  seh_fill_pcsyms (c, pc_syms, elm_start);
  elm_start[need] = c->elems_count;

  xdata_sz += ((12 * (size_t) need));
  c->count_syms += 5 * need;
  xdata_sz += (seh_getsize_of_unwind_entry (c, elm_start[0], elm_start[1], 1 != need) + 7) & ~7;
  for (i = 1; i < need; i++)
    xdata_sz += (seh_getsize_of_unwind_entry (c, elm_start[i], elm_start[i + 1], 1 != need) + 7) & ~7;

  /* Create lable names for .xdata unwind info.  */
  names[0] = c->xdata_first;
  for (i = 1; i < need; i++)
    names[i] = seh_make_xlbl_name (c);
  c->xdata_names = names;
  c->xdata_pcsyms = pc_syms;
  c->xdata_elm_start = elm_start;
  c->xdata_sz = xdata_sz;
  return xdata_sz;
}

static void
seh_create_unwind_data (seh_context *c, unsigned char *data, size_t offs)
{
  int need = seh_needed_unwind_info (c);
  int i;
  char **names = c->xdata_names;
  char **pc_syms = c->xdata_pcsyms;
  int *elm_start = c->xdata_elm_start;

  for (i = 1; i < need; i++)
    seh_make_function_entry_xdata (c, pc_syms[i], c->end_symbol, names[i], data, &offs, i);

  /* Generate the function entry. Remark, that just
     first is in .pdata section and already emitted.  */
  seh_make_unwind_entry (c, c->xdata_first, elm_start[0], elm_start[1], 1 != need, data, &offs, 1);
  for (i = 1; i < need; i++)
    seh_make_unwind_entry (c, names[i], elm_start[i], elm_start[i + 1], (i + 1) != need, data, &offs, i + 1);

  for (i = 1; i < need; i++)
    free (names[i]);
  free (names);
  free (pc_syms);
  free (elm_start);
  c->xdata_names = NULL;
  c->xdata_pcsyms = NULL;
  c->xdata_elm_start = NULL;
}

static void
seh_make_function_entry_xdata (seh_context *c, char *pc_start, char *pc_end, char *pc_xdata, unsigned char *data, size_t *poffs,int no)
{
  bfd_vma addr = (bfd_vma) *poffs;
  int idx;
  char s[100];

  if (!data)
    return;
  sprintf (s,"%x",no);
  seh_symbol (c->abfd, ".xdata_fct",c->func_name, s, c->section, BSF_GLOBAL, (int) poffs[0]);
  idx = seh_symbol (c->abfd, pc_start,"","", UNDSEC, BSF_GLOBAL,0);
  seh_reloc (c->abfd, addr, BFD_RELOC_RVA, idx);
  idx = seh_symbol (c->abfd, pc_end,"","", UNDSEC, BSF_GLOBAL,0);
  seh_reloc (c->abfd, addr + 4, BFD_RELOC_RVA, idx);
  idx = seh_symbol (c->abfd, pc_xdata,"","", UNDSEC, BSF_GLOBAL,0);
  seh_reloc (c->abfd, addr + 8, BFD_RELOC_RVA, idx);
  poffs[0] += 12;
}

static seh_scope_elem *
seh_x64_makescope_elem (seh_context *c, const char *begin, const char *end,
			const char *handler, const char *jmp)
{
  seh_scope_elem *r;

  if (!end || !begin)
    return NULL;
  if (c->scope_count >= c->scope_max)
    {
      seh_scope_elem *h = (seh_scope_elem *) xmalloc (sizeof (seh_scope_elem) * (c->scope_max + 8));
      memset (h, 0, sizeof (seh_scope_elem) * (c->scope_max + 8));
      if (c->scopes != NULL)
	memcpy (h, c->scopes, sizeof (seh_scope_elem) * c->scope_max);
      if (c->scopes != NULL)
	free (c->scopes);
      c->scopes = h;
      c->scope_max += 8;
    }
  r = &c->scopes[c->scope_count++];
  r->begin_addr = xstrdup (begin);
  r->end_addr = xstrdup (end);
  r->handler_addr = (!handler ? NULL : xstrdup (handler));
  r->jump_addr = (!jmp ? NULL : xstrdup (jmp));
  return r;
}
OpenPOWER on IntegriCloud