gas: utility notes memory alloc functions
[binutils-gdb.git] / gas / as.h
1 /* as.h - global header file
2 Copyright (C) 1987-2022 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #ifndef GAS
22 #define GAS 1
23 /* I think this stuff is largely out of date. xoxorich.
24
25 CAPITALISED names are #defined.
26 "lowercaseH" is #defined if "lowercase.h" has been #include-d.
27 "lowercaseT" is a typedef of "lowercase" objects.
28 "lowercaseP" is type "pointer to object of type 'lowercase'".
29 "lowercaseS" is typedef struct ... lowercaseS.
30
31 #define DEBUG to enable all the "know" assertion tests.
32 #define SUSPECT when debugging hash code.
33 #define COMMON as "extern" for all modules except one, where you #define
34 COMMON as "".
35 If TEST is #defined, then we are testing a module: #define COMMON as "". */
36
37 #include "config.h"
38
39 /* Now, tend to the rest of the configuration. */
40
41 /* System include files first... */
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #ifdef HAVE_SYS_TYPES_H
50 /* for size_t, pid_t */
51 #include <sys/types.h>
52 #endif
53
54 #include <errno.h>
55 #include <stdarg.h>
56
57 #include "getopt.h"
58 /* The first getopt value for machine-independent long options.
59 150 isn't special; it's just an arbitrary non-ASCII char value. */
60 #define OPTION_STD_BASE 150
61 /* The first getopt value for machine-dependent long options.
62 290 gives the standard options room to grow. */
63 #define OPTION_MD_BASE 290
64
65 #ifdef DEBUG
66 #undef NDEBUG
67 #endif
68 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6)
69 #define __PRETTY_FUNCTION__ ((char *) NULL)
70 #endif
71 #define gas_assert(P) ((void) ((P) ? 0 : (abort (), 0)))
72 #undef abort
73 #define abort() as_abort (__FILE__, __LINE__, __PRETTY_FUNCTION__)
74
75 /* Now GNU header files... */
76 #include "ansidecl.h"
77 #include "bfd.h"
78 #include "libiberty.h"
79
80 /* Define the standard progress macros. */
81 #include "progress.h"
82
83 /* Other stuff from config.h. */
84 #ifdef NEED_DECLARATION_ENVIRON
85 extern char **environ;
86 #endif
87 #ifdef NEED_DECLARATION_FFS
88 extern int ffs (int);
89 #endif
90
91 #if !HAVE_DECL_MEMPCPY
92 void *mempcpy(void *, const void *, size_t);
93 #endif
94
95 #ifndef __LINE__
96 #define __LINE__ "unknown"
97 #endif /* __LINE__ */
98
99 #ifndef __FILE__
100 #define __FILE__ "unknown"
101 #endif /* __FILE__ */
102
103 #ifndef FOPEN_WB
104 #ifdef USE_BINARY_FOPEN
105 #include "fopen-bin.h"
106 #else
107 #include "fopen-same.h"
108 #endif
109 #endif
110
111 #ifndef EXIT_SUCCESS
112 #define EXIT_SUCCESS 0
113 #define EXIT_FAILURE 1
114 #endif
115
116 #ifndef SEEK_SET
117 #define SEEK_SET 0
118 #endif
119
120 #define obstack_chunk_alloc xmalloc
121 #define obstack_chunk_free xfree
122
123 #define xfree free
124
125 #if GCC_VERSION >= 7000
126 #define gas_mul_overflow(a, b, res) __builtin_mul_overflow (a, b, res)
127 #else
128 /* Assumes unsigned values. Careful! Args evaluated multiple times. */
129 #define gas_mul_overflow(a, b, res) \
130 ((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a))
131 #endif
132
133 #include "asintl.h"
134
135 #define BAD_CASE(val) \
136 { \
137 as_fatal (_("Case value %ld unexpected at line %d of file \"%s\"\n"), \
138 (long) val, __LINE__, __FILE__); \
139 }
140 \f
141 #include "flonum.h"
142
143 /* These are assembler-wide concepts */
144
145 extern bfd *stdoutput;
146 typedef bfd_vma addressT;
147 typedef bfd_signed_vma offsetT;
148
149 /* Type of symbol value, etc. For use in prototypes. */
150 typedef addressT valueT;
151
152 #ifndef COMMON
153 #ifdef TEST
154 #define COMMON /* Declare our COMMONs storage here. */
155 #else
156 #define COMMON extern /* Our commons live elsewhere. */
157 #endif
158 #endif
159 /* COMMON now defined */
160
161 #ifndef ENABLE_CHECKING
162 #define ENABLE_CHECKING 0
163 #endif
164
165 #if ENABLE_CHECKING || defined (DEBUG)
166 #ifndef know
167 #define know(p) gas_assert(p) /* Verify our assumptions! */
168 #endif /* not yet defined */
169 #else
170 #define know(p) do {} while (0) /* know() checks are no-op.ed */
171 #endif
172 \f
173 /* input_scrub.c */
174
175 /* Supplies sanitised buffers to read.c.
176 Also understands printing line-number part of error messages. */
177 \f
178 /* subsegs.c Sub-segments. Also, segment(=expression type)s.*/
179
180 typedef asection *segT;
181 #define SEG_NORMAL(SEG) ( (SEG) != absolute_section \
182 && (SEG) != undefined_section \
183 && (SEG) != reg_section \
184 && (SEG) != expr_section)
185 typedef int subsegT;
186
187 /* What subseg we are accessing now? */
188 COMMON subsegT now_subseg;
189
190 /* Segment our instructions emit to. */
191 COMMON segT now_seg;
192
193 #define segment_name(SEG) bfd_section_name (SEG)
194
195 extern segT reg_section, expr_section;
196 /* Shouldn't these be eliminated someday? */
197 extern segT text_section, data_section, bss_section;
198 #define absolute_section bfd_abs_section_ptr
199 #define undefined_section bfd_und_section_ptr
200
201 enum _relax_state
202 {
203 /* Dummy frag used by listing code. */
204 rs_dummy = 0,
205
206 /* Variable chars to be repeated fr_offset times.
207 Fr_symbol unused. Used with fr_offset == 0 for a
208 constant length frag. */
209 rs_fill,
210
211 /* Align. The fr_offset field holds the power of 2 to which to
212 align. The fr_var field holds the number of characters in the
213 fill pattern. The fr_subtype field holds the maximum number of
214 bytes to skip when aligning, or 0 if there is no maximum. */
215 rs_align,
216
217 /* Align code. The fr_offset field holds the power of 2 to which
218 to align. This type is only generated by machine specific
219 code, which is normally responsible for handling the fill
220 pattern. The fr_subtype field holds the maximum number of
221 bytes to skip when aligning, or 0 if there is no maximum. */
222 rs_align_code,
223
224 /* Test for alignment. Like rs_align, but used by several targets
225 to warn if data is not properly aligned. */
226 rs_align_test,
227
228 /* Org: Fr_offset, fr_symbol: address. 1 variable char: fill
229 character. */
230 rs_org,
231
232 #ifndef WORKING_DOT_WORD
233 /* JF: gunpoint */
234 rs_broken_word,
235 #endif
236
237 /* Machine specific relaxable (or similarly alterable) instruction. */
238 rs_machine_dependent,
239
240 /* .space directive with expression operand that needs to be computed
241 later. Similar to rs_org, but different.
242 fr_symbol: operand
243 1 variable char: fill character */
244 rs_space,
245
246 /* .nop directive with expression operand that needs to be computed
247 later. Similar to rs_space, but different. It fills with no-op
248 instructions.
249 fr_symbol: operand
250 1 constant byte: no-op fill control byte. */
251 rs_space_nop,
252
253 /* Similar to rs_fill. It is used to implement .nop directive . */
254 rs_fill_nop,
255
256 /* A DWARF leb128 value; only ELF uses this. The subtype is 0 for
257 unsigned, 1 for signed. */
258 rs_leb128,
259
260 /* Exception frame information which we may be able to optimize. */
261 rs_cfa,
262
263 /* Cross-fragment dwarf2 line number optimization. */
264 rs_dwarf2dbg
265 };
266
267 typedef enum _relax_state relax_stateT;
268
269 /* This type is used in prototypes, so it can't be a type that will be
270 widened for argument passing. */
271 typedef unsigned int relax_substateT;
272
273 /* Enough bits for address, but still an integer type.
274 Could be a problem, cross-assembling for 64-bit machines. */
275 typedef addressT relax_addressT;
276
277 struct relax_type
278 {
279 /* Forward reach. Signed number. > 0. */
280 offsetT rlx_forward;
281 /* Backward reach. Signed number. < 0. */
282 offsetT rlx_backward;
283
284 /* Bytes length of this address. */
285 unsigned char rlx_length;
286
287 /* Next longer relax-state. 0 means there is no 'next' relax-state. */
288 relax_substateT rlx_more;
289 };
290
291 typedef struct relax_type relax_typeS;
292 \f
293 /* main program "as.c" (command arguments etc). */
294
295 COMMON unsigned char flag_no_comments; /* -f */
296 COMMON unsigned char flag_debug; /* -D */
297 COMMON unsigned char flag_signed_overflow_ok; /* -J */
298 #ifndef WORKING_DOT_WORD
299 COMMON unsigned char flag_warn_displacement; /* -K */
300 #endif
301
302 /* True if local symbols should be retained. */
303 COMMON int flag_keep_locals; /* -L */
304
305 /* True if we are assembling in MRI mode. */
306 COMMON int flag_mri;
307
308 /* Should the data section be made read-only and appended to the text
309 section? */
310 COMMON unsigned char flag_readonly_data_in_text; /* -R */
311
312 /* True if warnings should be inhibited. */
313 COMMON int flag_no_warnings; /* -W */
314
315 /* True if warnings count as errors. */
316 COMMON int flag_fatal_warnings; /* --fatal-warnings */
317
318 /* True if we should attempt to generate output even if non-fatal errors
319 are detected. */
320 COMMON unsigned char flag_always_generate_output; /* -Z */
321
322 /* This is true if the assembler should output time and space usage. */
323 COMMON unsigned char flag_print_statistics;
324
325 /* True if local absolute symbols are to be stripped. */
326 COMMON int flag_strip_local_absolute;
327
328 /* True if we should generate a traditional format object file. */
329 COMMON int flag_traditional_format;
330
331 /* Type of compressed debug sections we should generate. */
332 COMMON enum compressed_debug_section_type flag_compress_debug;
333
334 /* TRUE if .note.GNU-stack section with SEC_CODE should be created */
335 COMMON int flag_execstack;
336
337 /* TRUE if .note.GNU-stack section with SEC_CODE should be created */
338 COMMON int flag_noexecstack;
339
340 /* name of emitted object file */
341 COMMON const char *out_file_name;
342
343 /* name of file defining extensions to the basic instruction set */
344 COMMON char *insttbl_file_name;
345
346 /* TRUE if we need a second pass. */
347 COMMON int need_pass_2;
348
349 /* TRUE if we should do no relaxing, and
350 leave lots of padding. */
351 COMMON int linkrelax;
352
353 COMMON int do_not_pad_sections_to_alignment;
354
355 enum multibyte_input_handling
356 {
357 multibyte_allow = 0,
358 multibyte_warn,
359 multibyte_warn_syms
360 };
361 COMMON enum multibyte_input_handling multibyte_handling;
362
363 /* TRUE if we should produce a listing. */
364 extern int listing;
365
366 /* Type of debugging information we should generate. We currently support
367 stabs, ECOFF, and DWARF2.
368
369 NOTE! This means debug information about the assembly source code itself
370 and _not_ about possible debug information from a high-level language.
371 This is especially relevant to DWARF2, since the compiler may emit line
372 number directives that the assembler resolves. */
373
374 enum debug_info_type
375 {
376 DEBUG_UNSPECIFIED,
377 DEBUG_NONE,
378 DEBUG_STABS,
379 DEBUG_ECOFF,
380 DEBUG_DWARF,
381 DEBUG_DWARF2
382 };
383
384 extern enum debug_info_type debug_type;
385 extern int use_gnu_debug_info_extensions;
386 COMMON bool flag_dwarf_sections;
387 extern int flag_dwarf_cie_version;
388 extern unsigned int dwarf_level;
389 \f
390 /* Maximum level of macro nesting. */
391 extern int max_macro_nest;
392
393 /* Verbosity level. */
394 extern int verbose;
395
396 struct obstack;
397
398 /* Obstack chunk size. Keep large for efficient space use, make small to
399 increase malloc calls for monitoring memory allocation. */
400 extern int chunksize;
401
402 struct _pseudo_type
403 {
404 /* assembler mnemonic, lower case, no '.' */
405 const char *poc_name;
406 /* Do the work */
407 void (*poc_handler) (int);
408 /* Value to pass to handler */
409 int poc_val;
410 };
411
412 typedef struct _pseudo_type pseudo_typeS;
413
414 #if (__GNUC__ >= 2) && !defined(VMS)
415 /* for use with -Wformat */
416
417 #if __GNUC__ == 2 && __GNUC_MINOR__ < 6
418 /* Support for double underscores in attribute names was added in gcc
419 2.6, so avoid them if we are using an earlier version. */
420 #define __printf__ printf
421 #define __format__ format
422 #endif
423
424 #define PRINTF_LIKE(FCN) \
425 void FCN (const char *format, ...) \
426 __attribute__ ((__format__ (__printf__, 1, 2)))
427 #define PRINTF_WHERE_LIKE(FCN) \
428 void FCN (const char *file, unsigned int line, const char *format, ...) \
429 __attribute__ ((__format__ (__printf__, 3, 4)))
430
431 #else /* __GNUC__ < 2 || defined(VMS) */
432
433 #define PRINTF_LIKE(FCN) void FCN (const char *format, ...)
434 #define PRINTF_WHERE_LIKE(FCN) void FCN (const char *file, \
435 unsigned int line, \
436 const char *format, ...)
437
438 #endif /* __GNUC__ < 2 || defined(VMS) */
439
440 PRINTF_LIKE (as_bad);
441 PRINTF_LIKE (as_fatal) ATTRIBUTE_NORETURN;
442 PRINTF_LIKE (as_tsktsk);
443 PRINTF_LIKE (as_warn);
444 PRINTF_WHERE_LIKE (as_bad_where);
445 PRINTF_WHERE_LIKE (as_warn_where);
446
447 void as_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
448 void signal_init (void);
449 int had_errors (void);
450 int had_warnings (void);
451 void as_warn_value_out_of_range (const char *, offsetT, offsetT, offsetT,
452 const char *, unsigned);
453 void as_bad_value_out_of_range (const char *, offsetT, offsetT, offsetT,
454 const char *, unsigned);
455 void print_version_id (void);
456 char * app_push (void);
457
458 /* Number of littlenums required to hold an extended precision number. */
459 #define MAX_LITTLENUMS 6
460
461 char * atof_ieee (char *, int, LITTLENUM_TYPE *);
462 char * atof_ieee_detail (char *, int, int, LITTLENUM_TYPE *, FLONUM_TYPE *);
463 const char * ieee_md_atof (int, char *, int *, bool);
464 const char * vax_md_atof (int, char *, int *);
465 char * input_scrub_include_file (const char *, char *);
466 void input_scrub_insert_line (const char *);
467 void input_scrub_insert_file (char *);
468 char * input_scrub_new_file (const char *);
469 char * input_scrub_next_buffer (char **bufp);
470 size_t do_scrub_chars (size_t (*get) (char *, size_t), char *, size_t);
471 bool scan_for_multibyte_characters (const unsigned char *, const unsigned char *, bool);
472 int gen_to_words (LITTLENUM_TYPE *, int, long);
473 int had_err (void);
474 int ignore_input (void);
475 void cond_finish_check (int);
476 void cond_exit_macro (int);
477 int seen_at_least_1_file (void);
478 void app_pop (char *);
479 const char * as_where (unsigned int *);
480 const char * as_where_physical (unsigned int *);
481 void bump_line_counters (void);
482 void do_scrub_begin (int);
483 void input_scrub_begin (void);
484 void input_scrub_close (void);
485 void input_scrub_end (void);
486 void new_logical_line (const char *, int);
487 void new_logical_line_flags (const char *, int, int);
488 void subsegs_begin (void);
489 void subsegs_end (struct obstack **);
490 void subseg_change (segT, int);
491 segT subseg_new (const char *, subsegT);
492 segT subseg_force_new (const char *, subsegT);
493 void subseg_set (segT, subsegT);
494 int subseg_text_p (segT);
495 int seg_not_empty_p (segT);
496 void start_dependencies (char *);
497 void register_dependency (const char *);
498 void print_dependencies (void);
499 segT subseg_get (const char *, int);
500
501 char *remap_debug_filename (const char *);
502 void add_debug_prefix_map (const char *);
503
504 static inline char *
505 xmemdup0 (const char *in, size_t len)
506 {
507 return xmemdup (in, len, len + 1);
508 }
509
510 struct expressionS;
511 struct fix;
512 typedef struct symbol symbolS;
513 typedef struct frag fragS;
514
515 /* literal.c */
516 valueT add_to_literal_pool (symbolS *, valueT, segT, int);
517
518 int check_eh_frame (struct expressionS *, unsigned int *);
519 int eh_frame_estimate_size_before_relax (fragS *);
520 int eh_frame_relax_frag (fragS *);
521 void eh_frame_convert_frag (fragS *);
522 int generic_force_reloc (struct fix *);
523
524 #include "expr.h" /* Before targ-*.h */
525
526 /* This one starts the chain of target dependent headers. */
527 #include "targ-env.h"
528
529 #ifdef OBJ_MAYBE_ELF
530 #define IS_ELF (OUTPUT_FLAVOR == bfd_target_elf_flavour)
531 #else
532 #ifdef OBJ_ELF
533 #define IS_ELF 1
534 #else
535 #define IS_ELF 0
536 #endif
537 #endif
538
539 #include "write.h"
540 #include "frags.h"
541 #include "hashtab.h"
542 #include "hash.h"
543 #include "read.h"
544 #include "symbols.h"
545
546 #include "tc.h"
547 #include "obj.h"
548
549 #ifdef USE_EMULATIONS
550 #include "emul.h"
551 #endif
552 #include "listing.h"
553
554 #ifdef H_TICK_HEX
555 extern int enable_h_tick_hex;
556 #endif
557
558 #ifdef TC_M68K
559 /* True if we are assembling in m68k MRI mode. */
560 COMMON int flag_m68k_mri;
561 #define DOLLAR_AMBIGU flag_m68k_mri
562 #else
563 #define flag_m68k_mri 0
564 #endif
565
566 #ifndef TC_STRING_ESCAPES
567 #define TC_STRING_ESCAPES 1
568 #endif
569
570 #ifdef WARN_COMMENTS
571 COMMON int warn_comment;
572 COMMON unsigned int found_comment;
573 COMMON const char * found_comment_file;
574 #endif
575
576 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
577 /* If .size directive failure should be error or warning. */
578 COMMON int flag_allow_nonconst_size;
579
580 /* If we should generate ELF common symbols with the STT_COMMON type. */
581 extern int flag_use_elf_stt_common;
582
583 /* TRUE iff GNU Build attribute notes should
584 be generated if none are in the input files. */
585 extern bool flag_generate_build_notes;
586
587 /* If section name substitution sequences should be honored */
588 COMMON int flag_sectname_subst;
589 #endif
590
591 #ifndef DOLLAR_AMBIGU
592 #define DOLLAR_AMBIGU 0
593 #endif
594
595 #ifndef NUMBERS_WITH_SUFFIX
596 #define NUMBERS_WITH_SUFFIX 0
597 #endif
598
599 #ifndef LOCAL_LABELS_DOLLAR
600 #define LOCAL_LABELS_DOLLAR 0
601 #endif
602
603 #ifndef LOCAL_LABELS_FB
604 #define LOCAL_LABELS_FB 0
605 #endif
606
607 #ifndef LABELS_WITHOUT_COLONS
608 #define LABELS_WITHOUT_COLONS 0
609 #endif
610
611 #ifndef NO_PSEUDO_DOT
612 #define NO_PSEUDO_DOT 0
613 #endif
614
615 #ifndef TEXT_SECTION_NAME
616 #define TEXT_SECTION_NAME ".text"
617 #define DATA_SECTION_NAME ".data"
618 #define BSS_SECTION_NAME ".bss"
619 #endif
620
621 #ifndef OCTETS_PER_BYTE_POWER
622 #define OCTETS_PER_BYTE_POWER 0
623 #endif
624 #ifndef OCTETS_PER_BYTE
625 #define OCTETS_PER_BYTE (1<<OCTETS_PER_BYTE_POWER)
626 #endif
627 #if OCTETS_PER_BYTE != (1<<OCTETS_PER_BYTE_POWER)
628 #error "Octets per byte conflicts with its power-of-two definition!"
629 #endif
630
631 #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
632 /* On ELF platforms, mark debug sections with SEC_ELF_OCTETS */
633 #define SEC_OCTETS (IS_ELF ? SEC_ELF_OCTETS : 0)
634 #else
635 #define SEC_OCTETS 0
636 #endif
637
638 #define POISON_BFD_BOOLEAN 1
639
640 #endif /* GAS */