File: obj-elf-attr.c

package info (click to toggle)
binutils 2.46-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 417,664 kB
  • sloc: ansic: 1,487,508; asm: 829,455; cpp: 216,692; exp: 80,524; makefile: 73,157; sh: 24,213; yacc: 15,060; lisp: 13,632; perl: 13,404; lex: 1,714; ada: 1,681; pascal: 1,446; cs: 879; python: 637; java: 478; sed: 191; xml: 95; awk: 25
file content (1291 lines) | stat: -rw-r--r-- 35,834 bytes parent folder | download | duplicates (4)
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
/* Object attributes parsing.
   Copyright (C) 2025-2026 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   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-elf-attr.h"

#ifdef TC_OBJ_ATTR

#include "obstack.h"
#include "safe-ctype.h"

/* A variant type to store information about known OAv2 identifiers.  */
typedef union {
  uint8_t u8;
  bool b;
} oav2_identifier_variant_value_t;

typedef enum {
  OAv2_ASM_ID_VALUE_UNDEFINED = 0,
  OAv2_ASM_ID_VALUE_U8,
  OAv2_ASM_ID_VALUE_BOOL,
} oav2_identifier_variant_type_info_t;

typedef struct {
  oav2_identifier_variant_value_t val;
  oav2_identifier_variant_type_info_t vtype;
} oav2_identifier_variant_t;

typedef struct {
  const char *const name;
  const oav2_identifier_variant_t value;
} oav2_identifier_t;

typedef struct {
  size_t len;
  struct arg_variant_t *elts;
} arg_variant_list_t;

typedef union {
  const char *string;
  uint64_t u64;
  int64_t i64;
  arg_variant_list_t list;
} arg_variant_value_t;

typedef enum {
  VALUE_UNDEFINED = 0,
  VALUE_U64,
  VALUE_I64,
  VALUE_UNSIGNED_INTEGER = VALUE_U64,
  VALUE_SIGNED_INTEGER = VALUE_I64,
  VALUE_STRING,
  VALUE_LIST,
  VALUE_OPTIONAL_ABSENT,
} arg_variant_type_info_t;

/* A variant type to store the argument values of an assembly directive.  */
typedef struct arg_variant_t {
  arg_variant_value_t val;
  arg_variant_type_info_t vtype;
} arg_variant_t;

typedef arg_variant_t arg_t;

#define skip_whitespace(str)  do { if (is_whitespace (*(str))) ++(str); } while (0)

static inline bool
skip_past_char (char **str, char c)
{
  if (**str == c)
    {
      (*str)++;
      return true;
    }
  return false;
}
#define skip_past_comma(str) skip_past_char (str, ',')

#if (TC_OBJ_ATTR_v1)

/* A list of attributes that have been explicitly set by the assembly code.
   VENDOR is the vendor id, BASE is the tag shifted right by the number
   of bits in MASK, and bit N of MASK is set if tag BASE+N has been set.  */
typedef struct recorded_attribute_info {
  struct recorded_attribute_info *next;
  obj_attr_vendor_t vendor;
  unsigned int base;
  unsigned long mask;
} recorded_attribute_info_t;
static recorded_attribute_info_t *recorded_attributes;

static void
oav1_attr_info_free (recorded_attribute_info_t *node)
{
  while (node != NULL)
    {
      recorded_attribute_info_t *next = node->next;
      free (node);
      node = next;
    }
}

void
oav1_attr_info_init (void)
{
  /* Note: this "constructor" was added for symmetry with oav1_attr_info_exit.
     recorded_attributes is a static variable which is automatically initialized
     to NULL.  There is no need to initialize it another time except for a
     cosmetic reason and to possibly help fuzzing.  */
  recorded_attributes = NULL;
}

void
oav1_attr_info_exit (void)
{
  oav1_attr_info_free (recorded_attributes);
}

/* Record that we have seen an explicit specification of attribute TAG
   for vendor VENDOR.  */

static void
oav1_attr_record_seen (obj_attr_vendor_t vendor, obj_attr_tag_t tag)
{
  unsigned int base;
  unsigned long mask;
  recorded_attribute_info_t *rai;

  base = tag / (8 * sizeof (rai->mask));
  mask = 1UL << (tag % (8 * sizeof (rai->mask)));
  for (rai = recorded_attributes; rai; rai = rai->next)
    if (rai->vendor == vendor && rai->base == base)
      {
	rai->mask |= mask;
	return;
      }

  rai = XNEW (recorded_attribute_info_t);
  rai->next = recorded_attributes;
  rai->vendor = vendor;
  rai->base = base;
  rai->mask = mask;
  recorded_attributes = rai;
}

/* Return true if we have seen an explicit specification of attribute TAG
   for vendor VENDOR.  */

bool
oav1_attr_seen (obj_attr_vendor_t vendor, obj_attr_tag_t tag)
{
  unsigned int base;
  unsigned long mask;
  recorded_attribute_info_t *rai;

  base = tag / (8 * sizeof (rai->mask));
  mask = 1UL << (tag % (8 * sizeof (rai->mask)));
  for (rai = recorded_attributes; rai; rai = rai->next)
    if (rai->vendor == vendor && rai->base == base)
      return (rai->mask & mask) != 0;
  return false;
}

#endif /* TC_OBJ_ATTR_v1 */

/* Expected argument tokens for object attribute directives.  */
typedef enum {
  /* Base types.  */
  IDENTIFIER = 0x1,
  UNSIGNED_INTEGER = 0x2,
  SIGNED_INTEGER = 0x4,
  STRING = 0x8,
  LIST = 0x10,
  LT_MASK = 0xFF,
  /* Higher types.  */
  SUBSECTION_NAME = 0x100,
  SUBSECTION_OPTION_1 = 0x200,
  SUBSECTION_OPTION_2 = 0x400,
  ATTRIBUTE_KEY = 0x800,
  ATTRIBUTE_VALUE = 0x1000,
  HT_MASK = 0xFF00,
} arg_token_t;

/* Free an arguments list of size N.  */
static void
args_list_free (arg_t *args, size_t n)
{
  for (size_t i = 0; i < n; ++i)
    if (args[i].vtype == VALUE_STRING)
      free ((void *) args[i].val.string);
    else if (args[i].vtype == VALUE_LIST)
      args_list_free (args[i].val.list.elts, args[i].val.list.len);
  free (args);
}

/* Extract a string literal from the input.  */
static bool
extract_string_literal (arg_t *arg_out)
{
  int len;
  char *obstack_buf = demand_copy_C_string (&len);
  if (obstack_buf != NULL)
    {
      arg_out->val.string = xstrdup (obstack_buf);
      obstack_free (&notes, obstack_buf);
      arg_out->vtype = VALUE_STRING;
      return true;
    }

  arg_out->val.string = NULL;
  return false;
}

/* Extract an integer literal from the input.
   Anything matched by O_constant is considered an integer literal (see the
   usage of O_constant in expr.c to see all the matches.
   Return true on success, false otherwise. If a signedness issue is detected,
   'signedness_issue' is also set to true.  */
static bool
extract_integer_literal (arg_t *arg_out,
			 bool want_unsigned,
			 bool *signedness_issue)
{
  const char *cursor_begin = input_line_pointer;
  expressionS exp;
  expression_and_evaluate (&exp);
  if (exp.X_op != O_constant)
    {
      char backup_c = *input_line_pointer;
      *input_line_pointer = '\0';
      as_bad (_("expression '%s' does not resolve to an integer"),
	      cursor_begin);
      /* Restore the character pointed by the current cursor position,
	 otherwise '\0' misleads ignore_rest_of_line().  */
      *input_line_pointer = backup_c;
      return false;
    }

  int64_t val = (int64_t) exp.X_add_number;
  if (want_unsigned)
    {
      if (! exp.X_unsigned && val < 0)
	{
	  as_bad (_("unexpected value '%" PRId64 "', expected `unsigned integer' instead"),
		  val);
	  *signedness_issue = true;
	  return false;
	}
      arg_out->val.u64 = val;
      arg_out->vtype = VALUE_UNSIGNED_INTEGER;
    }
  else
    {
      arg_out->val.i64 = val;
      arg_out->vtype = VALUE_SIGNED_INTEGER;
    }
  return true;
}

/* Extract an identifier based on the provided character matcher.  */
static bool
extract_identifier (bool (*char_predicate) (char), arg_t *arg_out)
{
  const char *s = input_line_pointer;
  unsigned int i = 0;
  for (; char_predicate (*input_line_pointer); ++input_line_pointer)
    i++;
  if (i == 0)
    {
      as_bad (_("invalid character '%c' in identifier"),
	      *input_line_pointer);
      return false;
    }

  arg_out->vtype = VALUE_STRING;
  arg_out->val.string = xmemdup0 (s, i);
  return true;
}

#if (TC_OBJ_ATTR_v2)
/* Resolve the identifier if it matches a known tag.  */
static bool
resolve_if_matching_known_tag (const char *identifier,
			       const obj_attr_tag_info_t *known_identifier,
			       arg_t *val_out)
{
  if (strcmp (known_identifier->name, identifier) != 0)
    return false;

  val_out->val.u64 = known_identifier->value;
  val_out->vtype = VALUE_UNSIGNED_INTEGER;
  return true;
}

/* Resolve the identifier if it matches the known one.  */
static bool
resolve_if_matching (const char *identifier,
		     const oav2_identifier_t *known_identifier,
		     arg_t *val_out)
{
  /* Lowercase the identifier before comparison.  */
  char normalized_identifier[100];
  unsigned int i = 0;
  for (; i < sizeof (normalized_identifier) && identifier[i] != '\0'; ++i)
    normalized_identifier[i] = TOLOWER (identifier[i]);
  if (i >= sizeof (normalized_identifier))
    /* Identifier is too long.  */
    return false;
  gas_assert (identifier[i] == '\0');
  normalized_identifier[i] = '\0';

  /* Comparison with normalized identifier.  */
  if (strcmp (known_identifier->name, normalized_identifier) != 0)
    return false;

  /* We only need U8 and Bool for now.  */
  switch (known_identifier->value.vtype)
    {
    case OAv2_ASM_ID_VALUE_U8:
      val_out->val.u64 = known_identifier->value.val.b;
      val_out->vtype = VALUE_UNSIGNED_INTEGER;
      break;
    case OAv2_ASM_ID_VALUE_BOOL:
      val_out->val.u64 = known_identifier->value.val.u8;
      val_out->vtype = VALUE_UNSIGNED_INTEGER;
      break;
    default:
      abort ();
    }

  return true;
}
#endif /* TC_OBJ_ATTR_v2 */

#if (TC_OBJ_ATTR_v1)
/* Look up attribute tags defined in the backend (object attribute v1).  */
static bool
obj_attr_v1_lookup_known_attr_tag_symbol
  (const char *identifier ATTRIBUTE_UNUSED,
   arg_token_t token_type,
   arg_t *val_out)
{
  gas_assert (token_type & UNSIGNED_INTEGER);

#ifndef CONVERT_SYMBOLIC_ATTRIBUTE
# define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
#endif
  int tag = CONVERT_SYMBOLIC_ATTRIBUTE (identifier);
  if (tag < 0)
    return false;
  val_out->val.u64 = tag;
  val_out->vtype = VALUE_UNSIGNED_INTEGER;
  return true;
}
#endif /* TC_OBJ_ATTR_v1 */

#if (TC_OBJ_ATTR_v2)
/* Look up attribute tags defined in the backend (object attribute v2).  */
static bool
obj_attr_v2_lookup_known_attr_tag_symbol (const char *identifier,
					  arg_token_t token_type,
					  arg_t *val_out)
{
  obj_attr_subsection_v2_t *subsec = elf_obj_attr_subsections (stdoutput).last;
  /* This function is called when setting a value for an attribute, and it
     requires an active subsection.  Calling this function without setting
     'subsec' is a logical error.  Invalid user code, where the subsection
     has not been selected must be handled by the caller.  We require 'subsec'
     to be non-NULL here.  */
  gas_assert (subsec != NULL);

  /* An attribute tag is an unsigned integer, so the expected token type should
     always have the base type UNSIGNED_INTEGER.  Otherwise, this function was
     called incorrectly.  */
  gas_assert (token_type & UNSIGNED_INTEGER);

  bool resolved = false;
  const struct elf_backend_data *be = get_elf_backend_data (stdoutput);
  const known_subsection_v2_t *known_subsec
    = bfd_obj_attr_v2_identify_subsection (be, subsec->name);
  if (known_subsec != NULL)
    {
      for (size_t i = 0; i < known_subsec->len && ! resolved; ++i)
	resolved
	  = resolve_if_matching_known_tag (identifier,
					   &known_subsec->known_attrs[i].tag,
					   val_out);
    }

  if (resolved)
    /* An attribute tag is an unsigned integer, so the type of the found value
       should be VALUE_UNSIGNED_INTEGER.  Otherwise, check if you set correctly
       the type of the value associated to the symbol.  */
    gas_assert (val_out->vtype == VALUE_UNSIGNED_INTEGER);

  return resolved;
}
#endif /* TC_OBJ_ATTR_v2 */

/* Look up known symbols, and try to resolve the given identifier.  */
static bool
lookup_known_symbols (const char *identifier,
		      arg_token_t token_type,
		      arg_t *val_out)
{
  if (identifier == NULL)
    return false;

  arg_token_t high_ttype = (token_type & HT_MASK);

#if (TC_OBJ_ATTR_v2)
  static const oav2_identifier_t known_identifiers_subsection_optional[] = {
    { "optional", {
	.val.b = true,
	.vtype = OAv2_ASM_ID_VALUE_BOOL
      }
    },
    { "required", {
	.val.b = false,
	.vtype = OAv2_ASM_ID_VALUE_BOOL
      }
    },
  };

  static const oav2_identifier_t known_identifiers_subsection_encoding[] = {
    { "uleb128", {
	.val.u8 = obj_attr_encoding_v2_to_u8 (OA_ENC_ULEB128),
	.vtype = OAv2_ASM_ID_VALUE_U8
      }
    },
    { "ntbs", {
	.val.u8 = obj_attr_encoding_v2_to_u8 (OA_ENC_NTBS),
	.vtype = OAv2_ASM_ID_VALUE_U8
      }
    },
  };
#endif /* TC_OBJ_ATTR_v2 */

  bool resolved = false;

#if (TC_OBJ_ATTR_v2)
  if (high_ttype == SUBSECTION_OPTION_1 || high_ttype == SUBSECTION_OPTION_2)
    {
      const oav2_identifier_t *known_identifiers
	= (high_ttype == SUBSECTION_OPTION_1
	   ? known_identifiers_subsection_optional
	   : known_identifiers_subsection_encoding);
      const size_t N_identifiers
	= (high_ttype == SUBSECTION_OPTION_1
	   ? ARRAY_SIZE (known_identifiers_subsection_optional)
	   : ARRAY_SIZE (known_identifiers_subsection_encoding));

      for (size_t i = 0; i < N_identifiers && ! resolved; ++i)
	resolved = resolve_if_matching (identifier,
					&known_identifiers[i],
					val_out);
    }
  else
#endif /* TC_OBJ_ATTR_v2 */
  if (high_ttype == ATTRIBUTE_KEY)
    {
      obj_attr_version_t version = elf_obj_attr_version (stdoutput);
      switch (version)
	{
#if (TC_OBJ_ATTR_v1)
	case OBJ_ATTR_V1:
	  resolved = obj_attr_v1_lookup_known_attr_tag_symbol
	    (identifier, token_type, val_out);
	  break;
#endif /* TC_OBJ_ATTR_v1 */
#if (TC_OBJ_ATTR_v2)
	case OBJ_ATTR_V2:
	  resolved = obj_attr_v2_lookup_known_attr_tag_symbol
	    (identifier, token_type, val_out);
	  break;
#endif /* TC_OBJ_ATTR_v2 */
	default:
	  abort ();
	}
    }
  else
    abort ();

  return resolved;
}

/* Look up the symbol table of this compilation unit, and try to resolve the
   given identifier.  */
static bool
lookup_symbol_table (const char *identifier,
		     const arg_token_t expected_ttype,
		     arg_t *val_out)
{
  if (identifier == NULL)
    return false;

  /* Note: signed integer are unsupported for now.  */
  gas_assert (expected_ttype & UNSIGNED_INTEGER);

  symbolS *symbolP = symbol_find (identifier);
  if (symbolP == NULL)
    return false;

  if (! S_IS_DEFINED (symbolP))
    return false;

  valueT val = S_GET_VALUE (symbolP);
  val_out->val.u64 = val;
  val_out->vtype = VALUE_UNSIGNED_INTEGER;

  return true;
}

/* Function similar to snprintf from the standard library, except that it
   also updates the buffer pointer to point to the last written character,
   and length to match the remaining space in the buffer.
   Return the number of bytes printed.  The function is assumed to always be
   successful, and a failure with vnsprintf() will trigger an assert().  */
static size_t
snprintf_append (char **sbuffer, size_t *length,
		 const char *format, ...)
{
  va_list args;
  va_start (args, format);
  int rval = vsnprintf (*sbuffer, *length, format, args);
  va_end (args);

  gas_assert (rval >= 0);
  size_t written = rval;
  *length -= written;
  *sbuffer += written;
  return written;
}

/* Return the list of comma-separated strings matching the expected types
   (i.e. the flags set in low_ttype).  */
static const char *
expectations_to_string (const arg_token_t low_ttype,
			char *sbuffer, size_t length)
{
  unsigned match_n = 0;
  char *sbuffer_start = sbuffer;
  size_t total = 0;
  if (low_ttype & IDENTIFIER)
    {
      ++match_n;
      total += snprintf_append (&sbuffer, &length, "`%s'", "identifier");
    }

#define EXP_APPEND_TYPE_STR(type_flag, type_str)			\
  if (low_ttype & type_flag)						\
    {									\
      if (match_n >= 1)							\
	total += snprintf_append (&sbuffer, &length, "%s", ", or ");	\
      ++match_n;							\
      total += snprintf_append (&sbuffer, &length, "`%s'", type_str);	\
    }

  EXP_APPEND_TYPE_STR (STRING, "string");
  EXP_APPEND_TYPE_STR (UNSIGNED_INTEGER, "unsigned integer");
  EXP_APPEND_TYPE_STR (SIGNED_INTEGER, "signed integer");
#undef APPEND_TYPE_STR

  gas_assert (total <= length);
  return sbuffer_start;
}

/* In the context of object attributes, an identifier is defined with the
   following lexical constraint: [a-zA-z_][a-zA-Z0-9_]*.  An identifier can
   be used to define the name of a subsection, its optionality, or its
   encoding, as well as for an attribute's tag.  */

static bool
is_identifier_beginner (char c)
{
  return ISALPHA (c) || c == '_';
}

static bool
is_part_of_identifier (char c)
{
  return ISALNUM (c) || c == '_';
}

/* Parse an argument, and set its type accordingly depending on the input
   value, and the constraints on the expected argument.  */
static bool
obj_attr_parse_arg (arg_token_t expected_ttype,
		    bool resolve_identifier,
		    bool optional,
		    arg_t *arg_out)
{
  const arg_token_t low_ttype = (expected_ttype & LT_MASK);

  if (optional && is_end_of_stmt (*input_line_pointer))
    {
      arg_out->vtype = VALUE_OPTIONAL_ABSENT;
      return true;
    }

  /* Check whether this looks like a string literal
     Note: symbol look-up for string literals is not available.  */
  if (*input_line_pointer == '"')
    {
      bool status = extract_string_literal (arg_out);
      if (status && (low_ttype & STRING))
	return true;

      if (status)
	{
	  char sbuffer[100];
	  as_bad (_("unexpected `string' \"%s\", expected %s instead"),
		  arg_out->val.string,
		  expectations_to_string (low_ttype, sbuffer, sizeof(sbuffer)));
	  free ((char *) arg_out->val.string);
	  arg_out->val.string = NULL;
	  arg_out->vtype = VALUE_UNDEFINED;
	}
      return false;
    }

  /* Check whether this looks like an identifier.  */
  if (is_identifier_beginner (*input_line_pointer))
    {
      bool status = extract_identifier (is_part_of_identifier, arg_out);
      /* is_identifier_beginner() confirmed that it was the beginning of an
	 identifier, so we don't expect the extraction to fail.  */
      gas_assert (status);
      gas_assert (arg_out->vtype == VALUE_STRING);

      if (! (low_ttype & IDENTIFIER))
	{
	  char sbuffer[100];
	  as_bad (_("unexpected `identifier' \"%s\", expected %s instead"),
		  arg_out->val.string,
		  expectations_to_string (low_ttype, sbuffer, sizeof(sbuffer)));
	  free ((char *) arg_out->val.string);
	  arg_out->val.string = NULL;
	  arg_out->vtype = VALUE_UNDEFINED;
	  return false;
	}

      /* In some cases, we don't want to resolve the identifier because it is the
	 actual value.  */
      if (! resolve_identifier)
	return true;

      /* Move the identifier out of arg_out.  */
      const char *identifier = arg_out->val.string;
      arg_out->val.string = NULL;
      arg_out->vtype = VALUE_UNDEFINED;
      bool resolved = true;

      /* The identifier is a symbol, let's try to resolve it by:
	 1. using the provided list of known symbols.
	   a) backend-independent
	   b) backend-specific.  */
      if (lookup_known_symbols (identifier, expected_ttype, arg_out))
	goto free_identifier;

      /* 2. using the symbol table for this compilation unit.
	 Note: this is the last attempt before failure.  */
      if (lookup_symbol_table (identifier, low_ttype, arg_out))
	goto free_identifier;

      as_bad (_("unknown identifier '%s' in this context"), identifier);
      resolved = false;

 free_identifier:
      free ((char *) identifier);
      return resolved;
    }

  /* If it is neither a string nor an identifier, it must be an expression.  */
  bool signedness_issue = false;
  bool success = extract_integer_literal (arg_out,
					  (low_ttype & UNSIGNED_INTEGER),
					  &signedness_issue);
  if (success && (low_ttype & (UNSIGNED_INTEGER | SIGNED_INTEGER)))
    return true;

  char sbuffer[100];
  if (success)
    as_bad (_("unexpected integer '%"PRIu64"', expected %s instead"),
	    arg_out->val.u64,
	    expectations_to_string (low_ttype, sbuffer, sizeof(sbuffer)));
  else if ((low_ttype & UNSIGNED_INTEGER) && signedness_issue)
    {
      /* Already handled by extract_integer_literal(), nothing to do.  */
    }
  else
    as_bad (_("fell back to integer literal extraction from expression, "
	      "but expected %s instead"),
	    expectations_to_string (low_ttype, sbuffer, sizeof(sbuffer)));

  arg_out->vtype = VALUE_UNDEFINED;
  return false;
}

/* Trim white space before a parameter.
   Error if it meets a parameter separator before a parameter.  */
static bool
trim_whitespace_before_param (void)
{
  skip_whitespace (input_line_pointer);
  if (*input_line_pointer == ',')
    {
      as_bad (_("syntax error, comma not expected here"));
      return false;
    }
  return true;
}

/* Skip white space + parameter separator after a parameter.
   Error if it does not meet a parameter separator after a parameter.  */
static bool
skip_whitespace_past_comma (void)
{
  skip_whitespace (input_line_pointer);
  if (! skip_past_comma (&input_line_pointer))
    {
      as_bad (_("syntax error, comma missing here"));
      return false;
    }
  return true;
}

/* Can parse a list of arguments with variable length.  */
static bool
obj_attr_parse_args (arg_token_t expected_ttype,
		     bool resolve_identifier,
		     arg_t *arg_out)
{
  if ((expected_ttype & LIST) == 0)
    return obj_attr_parse_arg (expected_ttype, resolve_identifier, false,
			       arg_out);

  static const size_t LIST_MAX_SIZE = 2;
  arg_t *arg_list = xcalloc (LIST_MAX_SIZE, sizeof (*arg_list));

  /* We don't want to support recursive lists.  */
  expected_ttype &= ~LIST;

  size_t n = 0;
  do {
    if (! trim_whitespace_before_param ())
      goto bad;

    if (! obj_attr_parse_arg (expected_ttype, resolve_identifier, false,
			      &arg_list[n]))
      goto bad;

    ++n;
    skip_whitespace (input_line_pointer);
    if (is_end_of_stmt (*input_line_pointer))
      break;

    if (! skip_whitespace_past_comma ())
      goto bad;

    if (n >= LIST_MAX_SIZE)
      {
	as_bad ("too many arguments for a list (max: %zu)", LIST_MAX_SIZE);
	goto bad;
      }
  } while (n < LIST_MAX_SIZE);

  arg_out->vtype = VALUE_LIST;
  arg_out->val.list.len = n;
  arg_out->val.list.elts = arg_list;
  return true;

 bad:
  args_list_free (arg_list, n);
  return false;
}

#if (TC_OBJ_ATTR_v2)
static bool
is_valid_boolean (uint64_t value)
{
  return value <= 1;
}

#define is_valid_comprehension is_valid_boolean

static bool
is_valid_encoding (uint64_t value)
{
  value = obj_attr_encoding_v2_from_u8 (value);
  return OA_ENC_UNSET < value && value <= OA_ENC_MAX;
}

#endif /* TC_OBJ_ATTR_v2 */

#if (TC_OBJ_ATTR_v1)
/* Determine the expected argument type based on the tag ID.  */
static arg_token_t
obj_attr_v1_get_arg_type (bfd *abfd,
			  obj_attr_vendor_t vendor,
			  obj_attr_tag_t tag)
{
  int attr_type = bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
  arg_token_t arg_type;
  if (attr_type == (ATTR_TYPE_FLAG_STR_VAL | ATTR_TYPE_FLAG_INT_VAL))
    arg_type = LIST | UNSIGNED_INTEGER | STRING;
  else if (attr_type == ATTR_TYPE_FLAG_STR_VAL)
    arg_type = STRING;
  else
    /* Covers the remaning cases:
       - ATTR_TYPE_FLAG_INT_VAL.
       - ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_NO_DEFAULT.  */
    arg_type = UNSIGNED_INTEGER;
  return arg_type;
}
#endif /* TC_OBJ_ATTR_v1 */

#if (TC_OBJ_ATTR_v2)
/* Determine the expected argument type based on the subsection encoding.  */
static arg_token_t
obj_attr_v2_get_arg_type (obj_attr_encoding_v2_t subsec_encoding)
{
  arg_token_t arg_type;
  switch (subsec_encoding)
    {
    case OA_ENC_ULEB128:
      arg_type = UNSIGNED_INTEGER;
      break;
    case OA_ENC_NTBS:
      arg_type = STRING;
      break;
    case OA_ENC_UNSET:
    default:
      abort ();
    }
  return arg_type;
}
#endif /* TC_OBJ_ATTR_v2 */

/* Parse the arguments of [vendor]_attribute directive.  */
static arg_t *
vendor_attribute_parse_args (obj_attr_vendor_t vendor ATTRIBUTE_UNUSED,
			     const obj_attr_subsection_v2_t *subsec ATTRIBUTE_UNUSED,
			     unsigned int nargs, ...)
{
  va_list args;
  va_start (args, nargs);

  arg_t *args_out = xcalloc (nargs, sizeof (arg_t));

  for (unsigned int n = 0; n < nargs; ++n)
    {
      if (! trim_whitespace_before_param ())
	goto bad;

      arg_t *arg_out = &args_out[n];

      arg_token_t expected_ttype = va_arg (args, arg_token_t);
      arg_token_t high_ttype = (expected_ttype & HT_MASK);
      /* Make sure that we called the right parse_args().  */
      gas_assert (high_ttype == ATTRIBUTE_KEY
	       || high_ttype == ATTRIBUTE_VALUE);

      if (high_ttype == ATTRIBUTE_VALUE)
	{
	  arg_token_t type_attr_value
#if (TC_OBJ_ATTR_v1 && TC_OBJ_ATTR_v2)
	    = (subsec != NULL)
	      ? obj_attr_v2_get_arg_type (subsec->encoding)
	      : obj_attr_v1_get_arg_type (stdoutput, vendor,
					  args_out[n - 1].val.u64);
#elif (TC_OBJ_ATTR_v1)
	    = obj_attr_v1_get_arg_type (stdoutput, vendor,
					args_out[n - 1].val.u64);
#else /* TC_OBJ_ATTR_v2 */
	    = obj_attr_v2_get_arg_type (subsec->encoding);
#endif
	  expected_ttype |= type_attr_value;
	}

      if (! obj_attr_parse_args (expected_ttype, true, arg_out))
	{
	  if (high_ttype == ATTRIBUTE_KEY)
	    {
	      as_bad (_("could not parse attribute tag"));
	      goto bad;
	    }
	  else
	    {
	      as_bad (_("could not parse attribute value"));
	      goto bad;
	    }
	}

      if (n + 1 < nargs && !skip_whitespace_past_comma ())
	goto bad;
    }

  demand_empty_rest_of_line ();
  va_end (args);
  return args_out;

 bad:
  ignore_rest_of_line ();
  args_list_free (args_out, nargs);
  va_end (args);
  return NULL;
}

#if (TC_OBJ_ATTR_v1)
/* Record an attribute (object attribute v1 only).  */
static obj_attribute *
obj_attr_v1_record (bfd *abfd,
		    const obj_attr_vendor_t vendor,
		    const obj_attr_tag_t tag,
		    arg_t *parsed_arg)
{
  obj_attribute *attr = bfd_elf_new_obj_attr (abfd, vendor, tag);
  if (attr != NULL)
    {
      attr->type = bfd_elf_obj_attrs_arg_type (abfd, vendor, tag);
      if (parsed_arg->vtype == VALUE_LIST)
	{
	  arg_variant_list_t *plist = &parsed_arg->val.list;
	  gas_assert (plist->len == 2
		      && plist->elts[0].vtype == VALUE_UNSIGNED_INTEGER
		      && plist->elts[1].vtype == VALUE_STRING);
	  attr->i = plist->elts[0].val.u64;
	  attr->s = (char *) plist->elts[1].val.string;
	  plist->elts[1].val.string = NULL;
	}
      else if (parsed_arg->vtype == VALUE_STRING)
	{
	  attr->s = (char *) parsed_arg->val.string;
	  parsed_arg->val.string = NULL;
	}
      else
	{
	  attr->i = parsed_arg->val.u64;
	}
    }
  return attr;
}
#endif /* TC_OBJ_ATTR_v1 */

#if (TC_OBJ_ATTR_v2)
/* Parse the arguments of [vendor]_subsection directive (v2 only).  */
static arg_t *
vendor_subsection_parse_args (unsigned int nargs, ...)
{
  va_list args;
  va_start (args, nargs);

  arg_t *args_out = xcalloc (nargs, sizeof (arg_t));

  for (unsigned int n = 0; n < nargs; ++n)
    {
      if (! trim_whitespace_before_param ())
	goto bad;

      arg_t *arg_out = &args_out[n];

      arg_token_t expected_ttype = va_arg (args, arg_token_t);
      arg_token_t high_ttype = (expected_ttype & HT_MASK);
      /* Make sure that we called the right parse_args().  */
      gas_assert (high_ttype == SUBSECTION_NAME
		  || high_ttype == SUBSECTION_OPTION_1
		  || high_ttype == SUBSECTION_OPTION_2);

      if (high_ttype == SUBSECTION_NAME)
	{
	  if (! obj_attr_parse_arg (expected_ttype, false, false, arg_out))
	    {
	      as_bad (_("expected <subsection_name>, <comprehension>, "
			"<encoding>"));
	      goto bad;
	    }
	}
      else if (high_ttype == SUBSECTION_OPTION_1
	    || high_ttype == SUBSECTION_OPTION_2)
	{
	  if (! obj_attr_parse_arg (expected_ttype, true, true, arg_out))
	    goto bad;

	  if (arg_out->vtype == VALUE_OPTIONAL_ABSENT)
	    continue;

	  if (high_ttype == SUBSECTION_OPTION_1
	      && ! is_valid_comprehension (arg_out->val.u64))
	    {
	      as_bad
		(_("invalid value '%" PRIu64 "', expected values for "
		   "<comprehension> are 0 (=`required') or 1 (=`optional')"),
		 arg_out->val.u64);
	      goto bad;
	    }
	  else if (high_ttype == SUBSECTION_OPTION_2
		&& ! is_valid_encoding (arg_out->val.u64))
	    {
	      as_bad
		(_("invalid value '%" PRIu64 "', expected values for <encoding>"
		   " are 0 (=`ULEB128') or 1 (=`NTBS')"),
		 arg_out->val.u64);
	      goto bad;
	    }
	}
      else
	abort ();

      if (n + 1 < nargs
	  && ! is_end_of_stmt (*input_line_pointer)
	  && ! skip_whitespace_past_comma ())
	goto bad;
    }

  va_end (args);
  demand_empty_rest_of_line ();
  return args_out;

 bad:
  ignore_rest_of_line ();
  args_list_free (args_out, nargs);
  va_end (args);
  return NULL;
}

/* Record an attribute (object attribute v2 only).  */
static void
obj_attr_v2_record (obj_attr_tag_t key, arg_t *arg_val)
{
  /* An OAv2 cannot be recorded unless a subsection has been recorded.  */
  gas_assert (elf_obj_attr_subsections (stdoutput).last != NULL);

  union obj_attr_value_v2 obj_attr_val;
  if (arg_val->vtype == VALUE_UNSIGNED_INTEGER)
    obj_attr_val.uint = arg_val->val.u64;
  else
    {
      /* Move the string.  */
      obj_attr_val.string = arg_val->val.string;
      arg_val->val.string = NULL;
    }

  obj_attr_v2_t *obj_attr = bfd_elf_obj_attr_v2_init (key, obj_attr_val);
  gas_assert (obj_attr != NULL);

  /* Go over the list of already recorded attributes and check for
     redefinitions (which are forbidden).  */
  bool skip_recording = false;
  obj_attr_v2_t *recorded_attr = bfd_obj_attr_v2_find_by_tag
    (elf_obj_attr_subsections (stdoutput).last, obj_attr->tag, false);
  if (recorded_attr != NULL)
    {
      if ((arg_val->vtype == VALUE_UNSIGNED_INTEGER
	   && recorded_attr->val.uint != obj_attr->val.uint)
	  || (arg_val->vtype == VALUE_STRING
	      && strcmp (recorded_attr->val.string, obj_attr->val.string) != 0))
	as_bad (_("attribute '%" PRIu64 "' cannot be redefined"),
		recorded_attr->tag);
      skip_recording = true;
    }

  if (skip_recording)
    {
      if (arg_val->vtype == VALUE_STRING)
	free ((void *) obj_attr->val.string);
      free (obj_attr);
      return;
    }

  bfd_obj_attr_subsection_v2_append
    (elf_obj_attr_subsections (stdoutput).last, obj_attr);
}

/* Record a subsection (object attribute v2 only).
   Note: this function takes the ownership of 'name', so is responsible to free
   it if an issue occurs.  */
static void
obj_attr_v2_subsection_record (const char *name,
			       arg_t *arg_comprehension,
			       arg_t *arg_encoding)
{
  obj_attr_subsection_v2_t *already_recorded_subsec
    = bfd_obj_attr_subsection_v2_find_by_name
      (elf_obj_attr_subsections (stdoutput).first, name, false);

  bool comprehension_optional = arg_comprehension->val.u64;
  obj_attr_encoding_v2_t encoding
    = obj_attr_encoding_v2_from_u8 (arg_encoding->val.u64);

  if (already_recorded_subsec != NULL)
    {
      bool error_redeclaration = false;

      if (arg_comprehension->vtype == VALUE_OPTIONAL_ABSENT)
	gas_assert (arg_encoding->vtype == VALUE_OPTIONAL_ABSENT);
      else if (comprehension_optional != already_recorded_subsec->optional)
	error_redeclaration = true;

      if (arg_encoding->vtype != VALUE_OPTIONAL_ABSENT
	  && encoding != already_recorded_subsec->encoding)
	error_redeclaration = true;

      /* Check for mismatching redefinition of the subsection, i.e. the names
	 match but the properties are different.  */
      if (error_redeclaration)
	{
	  const char *prev_comprehension = bfd_oav2_comprehension_to_string (
	    already_recorded_subsec->optional);
	  const char *prev_encoding = bfd_oav2_encoding_to_string (
	    already_recorded_subsec->encoding);
	  as_bad (_("incompatible redeclaration of subsection %s"), name);
	  as_info (1, _("previous declaration had properties: %s=%s, %s=%s"),
		   "comprehension", prev_comprehension,
		   "encoding", prev_encoding);
	  goto free_name;
	}

      /* Move the existing subsection to the last position.  */
      bfd_obj_attr_subsection_v2_list_remove
	(&elf_obj_attr_subsections (stdoutput), already_recorded_subsec);
      bfd_obj_attr_subsection_v2_list_append
	(&elf_obj_attr_subsections (stdoutput), already_recorded_subsec);
      /* Note: 'name' was unused, and will be freed on exit.  */
    }
  else
    {
      if (arg_comprehension->vtype == VALUE_OPTIONAL_ABSENT
	  || arg_encoding->vtype == VALUE_OPTIONAL_ABSENT)
	{
	  as_bad (_("comprehension and encoding of a subsection cannot be "
		    "omitted on the first declaration"));
	  goto free_name;
	}

      obj_attr_subsection_scope_v2_t scope
	= bfd_elf_obj_attr_subsection_v2_scope (stdoutput, name);

      /* Note: ownership of 'name' is transfered to the callee when initializing
	 the subsection.  That is why we skip free() at the end.  */
      obj_attr_subsection_v2_t *new_subsection
	= bfd_elf_obj_attr_subsection_v2_init (name, scope,
					       comprehension_optional,
					       encoding);
      bfd_obj_attr_subsection_v2_list_append
	(&elf_obj_attr_subsections (stdoutput), new_subsection);
      return;
    }

 free_name:
  free ((void *) name);
}
#endif /* TC_OBJ_ATTR_v2 */

/* Parse an attribute directive (supports both v1 & v2).  */
obj_attr_tag_t
obj_attr_process_attribute (obj_attr_vendor_t vendor)
{
  obj_attr_version_t version = elf_obj_attr_version (stdoutput);
  obj_attr_subsection_v2_t *subsec = NULL;

#if (TC_OBJ_ATTR_v2)
  if (version == OBJ_ATTR_V2)
    {
      subsec = elf_obj_attr_subsections (stdoutput).last;
      if (subsec == NULL)
	{
	  as_bad (_("declaration of an attribute outside the scope of an "
		    "attribute subsection"));
	  ignore_rest_of_line ();
	  return 0;
	}
    }
#endif /* TC_OBJ_ATTR_v2 */

  const size_t N_ARGS = 2;
  arg_t *args = vendor_attribute_parse_args (
    vendor, subsec, N_ARGS,
    ATTRIBUTE_KEY | IDENTIFIER | UNSIGNED_INTEGER,
    ATTRIBUTE_VALUE);

  if (args == NULL)
    return 0;

  obj_attr_tag_t tag = args[0].val.u64;
  switch (version)
    {
#if (TC_OBJ_ATTR_v1)
    case OBJ_ATTR_V1:
      oav1_attr_record_seen (vendor, tag);
      obj_attr_v1_record (stdoutput, vendor, tag, &args[1]);
      break;
#endif /* TC_OBJ_ATTR_v1 */
#if (TC_OBJ_ATTR_v2)
    case OBJ_ATTR_V2:
      obj_attr_v2_record (tag, &args[1]);
      break;
#endif /* TC_OBJ_ATTR_v2 */
    default:
      abort ();
    }

  args_list_free (args, N_ARGS);

  return tag;
}

#if (TC_OBJ_ATTR_v2)
/* Parse an object attribute v2's subsection directive.  */
void
obj_attr_process_subsection (void)
{
  const size_t N_ARGS = 3;
  arg_t *args = vendor_subsection_parse_args (
    N_ARGS,
    SUBSECTION_NAME | IDENTIFIER,
    SUBSECTION_OPTION_1 | IDENTIFIER | UNSIGNED_INTEGER,
    SUBSECTION_OPTION_2 | IDENTIFIER | UNSIGNED_INTEGER);

  if (args == NULL)
    return;

  /* Note: move the value to avoid double free.  */
  const char *name = args[0].val.string;
  args[0].val.string = NULL;

  /* Note: ownership of 'name' is transferred to the callee.  */
  obj_attr_v2_subsection_record (name, &args[1], &args[2]);
  args_list_free (args, N_ARGS);
}
#endif /* TC_OBJ_ATTR_v2 */

/* Parse a .gnu_attribute directive.  */

void
obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
{
  obj_attr_process_attribute (OBJ_ATTR_GNU);
}

#if (TC_OBJ_ATTR_v2)
/* Return True if the VERSION of object attributes supports subsections, False
   otherwise.  */

static inline bool
attr_fmt_has_subsections (obj_attr_version_t version)
{
  switch (version)
  {
  case OBJ_ATTR_V1:
    return false;
  case OBJ_ATTR_V2:
    return true;
  default:
    abort ();  /* Unsupported format.  */
  }
}

/* Parse a .gnu_subsection directive.  */

void
obj_elf_gnu_subsection (int ignored ATTRIBUTE_UNUSED)
{
  obj_attr_version_t version = elf_obj_attr_version (stdoutput);
  if (! attr_fmt_has_subsections (version))
    {
      as_bad (_(".gnu_subsection is only available with object attributes v2"));
      ignore_rest_of_line ();
      return;
    }
  obj_attr_process_subsection ();
}
#endif /* TC_OBJ_ATTR_v2 */

#endif /* TC_OBJ_ATTR */