]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/obj-elf.c
Move .file and .loc pseudo ops into obj-elf.c
[thirdparty/binutils-gdb.git] / gas / config / obj-elf.c
1 /* ELF object file format
2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2,
10 or (at your option) any later version.
11
12 GAS is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #define OBJ_HEADER "obj-elf.h"
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "subsegs.h"
26 #include "obstack.h"
27 #include "struc-symbol.h"
28 #include "dwarf2dbg.h"
29
30 #ifndef ECOFF_DEBUGGING
31 #define ECOFF_DEBUGGING 0
32 #else
33 #define NEED_ECOFF_DEBUG
34 #endif
35
36 #ifdef NEED_ECOFF_DEBUG
37 #include "ecoff.h"
38 #endif
39
40 #ifdef TC_ALPHA
41 #include "elf/alpha.h"
42 #endif
43
44 #ifdef TC_MIPS
45 #include "elf/mips.h"
46 #endif
47
48 #ifdef TC_PPC
49 #include "elf/ppc.h"
50 #endif
51
52 #ifdef TC_I370
53 #include "elf/i370.h"
54 #endif
55
56 static bfd_vma elf_s_get_size PARAMS ((symbolS *));
57 static void elf_s_set_size PARAMS ((symbolS *, bfd_vma));
58 static bfd_vma elf_s_get_align PARAMS ((symbolS *));
59 static void elf_s_set_align PARAMS ((symbolS *, bfd_vma));
60 static void elf_s_set_other PARAMS ((symbolS *, int));
61 static int elf_sec_sym_ok_for_reloc PARAMS ((asection *));
62 static void adjust_stab_sections PARAMS ((bfd *, asection *, PTR));
63 static void build_group_lists PARAMS ((bfd *, asection *, PTR));
64 static int elf_separate_stab_sections PARAMS ((void));
65 static void elf_init_stab_section PARAMS ((segT));
66 static symbolS *elf_common PARAMS ((int));
67
68 #ifdef NEED_ECOFF_DEBUG
69 static bfd_boolean elf_get_extr PARAMS ((asymbol *, EXTR *));
70 static void elf_set_index PARAMS ((asymbol *, bfd_size_type));
71 #endif
72
73 static void obj_elf_line PARAMS ((int));
74 void obj_elf_version PARAMS ((int));
75 static void obj_elf_size PARAMS ((int));
76 static void obj_elf_type PARAMS ((int));
77 static void obj_elf_ident PARAMS ((int));
78 static void obj_elf_weak PARAMS ((int));
79 static void obj_elf_local PARAMS ((int));
80 static void obj_elf_visibility PARAMS ((int));
81 static int obj_elf_parse_section_letters PARAMS ((char *, size_t));
82 static int obj_elf_section_word PARAMS ((char *, size_t));
83 static char *obj_elf_section_name PARAMS ((void));
84 static int obj_elf_section_type PARAMS ((char *, size_t));
85 static void obj_elf_symver PARAMS ((int));
86 static void obj_elf_subsection PARAMS ((int));
87 static void obj_elf_popsection PARAMS ((int));
88 static void obj_elf_tls_common PARAMS ((int));
89
90 static const pseudo_typeS elf_pseudo_table[] =
91 {
92 {"comm", obj_elf_common, 0},
93 {"common", obj_elf_common, 1},
94 {"ident", obj_elf_ident, 0},
95 {"local", obj_elf_local, 0},
96 {"previous", obj_elf_previous, 0},
97 {"section", obj_elf_section, 0},
98 {"section.s", obj_elf_section, 0},
99 {"sect", obj_elf_section, 0},
100 {"sect.s", obj_elf_section, 0},
101 {"pushsection", obj_elf_section, 1},
102 {"popsection", obj_elf_popsection, 0},
103 {"size", obj_elf_size, 0},
104 {"type", obj_elf_type, 0},
105 {"version", obj_elf_version, 0},
106 {"weak", obj_elf_weak, 0},
107
108 /* These define symbol visibility. */
109 {"internal", obj_elf_visibility, STV_INTERNAL},
110 {"hidden", obj_elf_visibility, STV_HIDDEN},
111 {"protected", obj_elf_visibility, STV_PROTECTED},
112
113 /* These are used for stabs-in-elf configurations. */
114 {"line", obj_elf_line, 0},
115
116 /* This is a GNU extension to handle symbol versions. */
117 {"symver", obj_elf_symver, 0},
118
119 /* A GNU extension to change subsection only. */
120 {"subsection", obj_elf_subsection, 0},
121
122 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
123 {"vtable_inherit", (void (*) PARAMS ((int))) &obj_elf_vtable_inherit, 0},
124 {"vtable_entry", (void (*) PARAMS ((int))) &obj_elf_vtable_entry, 0},
125
126 /* These are used for dwarf. */
127 {"2byte", cons, 2},
128 {"4byte", cons, 4},
129 {"8byte", cons, 8},
130 /* These are used for dwarf2. */
131 { "file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0 },
132 { "loc", dwarf2_directive_loc, 0 },
133
134 /* We need to trap the section changing calls to handle .previous. */
135 {"data", obj_elf_data, 0},
136 {"text", obj_elf_text, 0},
137
138 {"tls_common", obj_elf_tls_common, 0},
139
140 /* End sentinel. */
141 {NULL, NULL, 0},
142 };
143
144 static const pseudo_typeS ecoff_debug_pseudo_table[] =
145 {
146 #ifdef NEED_ECOFF_DEBUG
147 /* COFF style debugging information for ECOFF. .ln is not used; .loc
148 is used instead. */
149 { "def", ecoff_directive_def, 0 },
150 { "dim", ecoff_directive_dim, 0 },
151 { "endef", ecoff_directive_endef, 0 },
152 { "file", ecoff_directive_file, 0 },
153 { "scl", ecoff_directive_scl, 0 },
154 { "tag", ecoff_directive_tag, 0 },
155 { "val", ecoff_directive_val, 0 },
156
157 /* COFF debugging requires pseudo-ops .size and .type, but ELF
158 already has meanings for those. We use .esize and .etype
159 instead. These are only generated by gcc anyhow. */
160 { "esize", ecoff_directive_size, 0 },
161 { "etype", ecoff_directive_type, 0 },
162
163 /* ECOFF specific debugging information. */
164 { "begin", ecoff_directive_begin, 0 },
165 { "bend", ecoff_directive_bend, 0 },
166 { "end", ecoff_directive_end, 0 },
167 { "ent", ecoff_directive_ent, 0 },
168 { "fmask", ecoff_directive_fmask, 0 },
169 { "frame", ecoff_directive_frame, 0 },
170 { "loc", ecoff_directive_loc, 0 },
171 { "mask", ecoff_directive_mask, 0 },
172
173 /* Other ECOFF directives. */
174 { "extern", ecoff_directive_extern, 0 },
175
176 /* These are used on Irix. I don't know how to implement them. */
177 { "alias", s_ignore, 0 },
178 { "bgnb", s_ignore, 0 },
179 { "endb", s_ignore, 0 },
180 { "lab", s_ignore, 0 },
181 { "noalias", s_ignore, 0 },
182 { "verstamp", s_ignore, 0 },
183 { "vreg", s_ignore, 0 },
184 #endif
185
186 {NULL, NULL, 0} /* end sentinel */
187 };
188
189 #undef NO_RELOC
190 #include "aout/aout64.h"
191
192 /* This is called when the assembler starts. */
193
194 void
195 elf_begin ()
196 {
197 /* Add symbols for the known sections to the symbol table. */
198 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
199 TEXT_SECTION_NAME)));
200 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
201 DATA_SECTION_NAME)));
202 symbol_table_insert (section_symbol (bfd_get_section_by_name (stdoutput,
203 BSS_SECTION_NAME)));
204 }
205
206 void
207 elf_pop_insert ()
208 {
209 pop_insert (elf_pseudo_table);
210 if (ECOFF_DEBUGGING)
211 pop_insert (ecoff_debug_pseudo_table);
212 }
213
214 static bfd_vma
215 elf_s_get_size (sym)
216 symbolS *sym;
217 {
218 return S_GET_SIZE (sym);
219 }
220
221 static void
222 elf_s_set_size (sym, sz)
223 symbolS *sym;
224 bfd_vma sz;
225 {
226 S_SET_SIZE (sym, sz);
227 }
228
229 static bfd_vma
230 elf_s_get_align (sym)
231 symbolS *sym;
232 {
233 return S_GET_ALIGN (sym);
234 }
235
236 static void
237 elf_s_set_align (sym, align)
238 symbolS *sym;
239 bfd_vma align;
240 {
241 S_SET_ALIGN (sym, align);
242 }
243
244 int
245 elf_s_get_other (sym)
246 symbolS *sym;
247 {
248 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
249 }
250
251 static void
252 elf_s_set_other (sym, other)
253 symbolS *sym;
254 int other;
255 {
256 S_SET_OTHER (sym, other);
257 }
258
259 static int
260 elf_sec_sym_ok_for_reloc (sec)
261 asection *sec;
262 {
263 return obj_sec_sym_ok_for_reloc (sec);
264 }
265
266 void
267 elf_file_symbol (s)
268 const char *s;
269 {
270 symbolS *sym;
271
272 sym = symbol_new (s, absolute_section, (valueT) 0, (struct frag *) 0);
273 symbol_set_frag (sym, &zero_address_frag);
274 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
275
276 if (symbol_rootP != sym)
277 {
278 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
279 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
280 #ifdef DEBUG
281 verify_symbol_chain (symbol_rootP, symbol_lastP);
282 #endif
283 }
284
285 #ifdef NEED_ECOFF_DEBUG
286 ecoff_new_file (s);
287 #endif
288 }
289
290 static symbolS *
291 elf_common (is_common)
292 int is_common;
293 {
294 char *name;
295 char c;
296 char *p;
297 offsetT temp, size, sign;
298 symbolS *symbolP;
299 int have_align;
300 expressionS exp;
301
302 if (flag_mri && is_common)
303 {
304 s_mri_common (0);
305 return NULL;
306 }
307
308 name = input_line_pointer;
309 c = get_symbol_end ();
310 /* just after name is now '\0' */
311 p = input_line_pointer;
312 *p = c;
313 SKIP_WHITESPACE ();
314 if (*input_line_pointer != ',')
315 {
316 as_bad (_("expected comma after symbol-name"));
317 ignore_rest_of_line ();
318 return NULL;
319 }
320 input_line_pointer++; /* skip ',' */
321 temp = get_absolute_expr (&exp);
322 sign = (offsetT) 1 << (stdoutput->arch_info->bits_per_address - 1);
323 size = temp & ((sign << 1) - 1);
324 if (temp != size || !exp.X_unsigned)
325 {
326 as_bad (_(".COMMon length (%ld) out of range, ignored."), (long) temp);
327 ignore_rest_of_line ();
328 return NULL;
329 }
330 *p = 0;
331 symbolP = symbol_find_or_make (name);
332 *p = c;
333 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
334 {
335 as_bad (_("symbol `%s' is already defined"), S_GET_NAME (symbolP));
336 ignore_rest_of_line ();
337 return NULL;
338 }
339 if (S_GET_VALUE (symbolP) != 0)
340 {
341 if (S_GET_VALUE (symbolP) != (valueT) size)
342 {
343 as_warn (_("length of .comm \"%s\" is already %ld; not changed to %ld"),
344 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP),
345 (long) size);
346 }
347 }
348 know (symbolP->sy_frag == &zero_address_frag);
349 if (*input_line_pointer != ',')
350 have_align = 0;
351 else
352 {
353 have_align = 1;
354 input_line_pointer++;
355 SKIP_WHITESPACE ();
356 }
357 if (! have_align || *input_line_pointer != '"')
358 {
359 if (! have_align)
360 temp = 0;
361 else
362 {
363 temp = get_absolute_expr (&exp);
364 if (!exp.X_unsigned)
365 {
366 temp = 0;
367 as_warn (_("common alignment negative; 0 assumed"));
368 }
369 }
370 if (symbol_get_obj (symbolP)->local)
371 {
372 segT old_sec;
373 int old_subsec;
374 char *pfrag;
375 int align;
376
377 /* allocate_bss: */
378 old_sec = now_seg;
379 old_subsec = now_subseg;
380 if (temp)
381 {
382 /* convert to a power of 2 alignment */
383 for (align = 0; (temp & 1) == 0; temp >>= 1, ++align);
384 if (temp != 1)
385 {
386 as_bad (_("common alignment not a power of 2"));
387 ignore_rest_of_line ();
388 return NULL;
389 }
390 }
391 else
392 align = 0;
393 record_alignment (bss_section, align);
394 subseg_set (bss_section, 0);
395 if (align)
396 frag_align (align, 0, 0);
397 if (S_GET_SEGMENT (symbolP) == bss_section)
398 symbol_get_frag (symbolP)->fr_symbol = 0;
399 symbol_set_frag (symbolP, frag_now);
400 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
401 (offsetT) size, (char *) 0);
402 *pfrag = 0;
403 S_SET_SIZE (symbolP, size);
404 S_SET_SEGMENT (symbolP, bss_section);
405 S_CLEAR_EXTERNAL (symbolP);
406 subseg_set (old_sec, old_subsec);
407 }
408 else
409 {
410 allocate_common:
411 S_SET_VALUE (symbolP, (valueT) size);
412 S_SET_ALIGN (symbolP, temp);
413 S_SET_EXTERNAL (symbolP);
414 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
415 }
416 }
417 else
418 {
419 input_line_pointer++;
420 /* @@ Some use the dot, some don't. Can we get some consistency?? */
421 if (*input_line_pointer == '.')
422 input_line_pointer++;
423 /* @@ Some say data, some say bss. */
424 if (strncmp (input_line_pointer, "bss\"", 4)
425 && strncmp (input_line_pointer, "data\"", 5))
426 {
427 while (*--input_line_pointer != '"')
428 ;
429 input_line_pointer--;
430 goto bad_common_segment;
431 }
432 while (*input_line_pointer++ != '"')
433 ;
434 goto allocate_common;
435 }
436
437 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
438
439 demand_empty_rest_of_line ();
440 return symbolP;
441
442 {
443 bad_common_segment:
444 p = input_line_pointer;
445 while (*p && *p != '\n')
446 p++;
447 c = *p;
448 *p = '\0';
449 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
450 *p = c;
451 input_line_pointer = p;
452 ignore_rest_of_line ();
453 return NULL;
454 }
455 }
456
457 void
458 obj_elf_common (is_common)
459 int is_common;
460 {
461 elf_common (is_common);
462 }
463
464 static void
465 obj_elf_tls_common (ignore)
466 int ignore ATTRIBUTE_UNUSED;
467 {
468 symbolS *symbolP = elf_common (0);
469
470 if (symbolP)
471 symbol_get_bfdsym (symbolP)->flags |= BSF_THREAD_LOCAL;
472 }
473
474 static void
475 obj_elf_local (ignore)
476 int ignore ATTRIBUTE_UNUSED;
477 {
478 char *name;
479 int c;
480 symbolS *symbolP;
481
482 do
483 {
484 name = input_line_pointer;
485 c = get_symbol_end ();
486 symbolP = symbol_find_or_make (name);
487 *input_line_pointer = c;
488 SKIP_WHITESPACE ();
489 S_CLEAR_EXTERNAL (symbolP);
490 symbol_get_obj (symbolP)->local = 1;
491 if (c == ',')
492 {
493 input_line_pointer++;
494 SKIP_WHITESPACE ();
495 if (*input_line_pointer == '\n')
496 c = '\n';
497 }
498 }
499 while (c == ',');
500 demand_empty_rest_of_line ();
501 }
502
503 static void
504 obj_elf_weak (ignore)
505 int ignore ATTRIBUTE_UNUSED;
506 {
507 char *name;
508 int c;
509 symbolS *symbolP;
510
511 do
512 {
513 name = input_line_pointer;
514 c = get_symbol_end ();
515 symbolP = symbol_find_or_make (name);
516 *input_line_pointer = c;
517 SKIP_WHITESPACE ();
518 S_SET_WEAK (symbolP);
519 symbol_get_obj (symbolP)->local = 1;
520 if (c == ',')
521 {
522 input_line_pointer++;
523 SKIP_WHITESPACE ();
524 if (*input_line_pointer == '\n')
525 c = '\n';
526 }
527 }
528 while (c == ',');
529 demand_empty_rest_of_line ();
530 }
531
532 static void
533 obj_elf_visibility (visibility)
534 int visibility;
535 {
536 char *name;
537 int c;
538 symbolS *symbolP;
539 asymbol *bfdsym;
540 elf_symbol_type *elfsym;
541
542 do
543 {
544 name = input_line_pointer;
545 c = get_symbol_end ();
546 symbolP = symbol_find_or_make (name);
547 *input_line_pointer = c;
548
549 SKIP_WHITESPACE ();
550
551 bfdsym = symbol_get_bfdsym (symbolP);
552 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
553
554 assert (elfsym);
555
556 elfsym->internal_elf_sym.st_other &= ~3;
557 elfsym->internal_elf_sym.st_other |= visibility;
558
559 if (c == ',')
560 {
561 input_line_pointer ++;
562
563 SKIP_WHITESPACE ();
564
565 if (*input_line_pointer == '\n')
566 c = '\n';
567 }
568 }
569 while (c == ',');
570
571 demand_empty_rest_of_line ();
572 }
573
574 static segT previous_section;
575 static int previous_subsection;
576
577 struct section_stack
578 {
579 struct section_stack *next;
580 segT seg, prev_seg;
581 int subseg, prev_subseg;
582 };
583
584 static struct section_stack *section_stack;
585
586 /* Handle the .section pseudo-op. This code supports two different
587 syntaxes.
588
589 The first is found on Solaris, and looks like
590 .section ".sec1",#alloc,#execinstr,#write
591 Here the names after '#' are the SHF_* flags to turn on for the
592 section. I'm not sure how it determines the SHT_* type (BFD
593 doesn't really give us control over the type, anyhow).
594
595 The second format is found on UnixWare, and probably most SVR4
596 machines, and looks like
597 .section .sec1,"a",@progbits
598 The quoted string may contain any combination of a, w, x, and
599 represents the SHF_* flags to turn on for the section. The string
600 beginning with '@' can be progbits or nobits. There should be
601 other possibilities, but I don't know what they are. In any case,
602 BFD doesn't really let us set the section type. */
603
604 /* Certain named sections have particular defined types, listed on p.
605 4-19 of the ABI. */
606 struct special_section
607 {
608 const char *name;
609 int type;
610 int attributes;
611 };
612
613 static struct special_section const special_sections[] =
614 {
615 { ".bss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
616 { ".comment", SHT_PROGBITS, 0 },
617 { ".data", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
618 { ".data1", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
619 { ".debug", SHT_PROGBITS, 0 },
620 #if defined (TC_HPPA) && !defined (TE_LINUX) && TARGET_ARCH_SIZE == 64
621 { ".fini", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
622 { ".init", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
623 #else
624 { ".fini", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
625 { ".init", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
626 #endif
627 { ".line", SHT_PROGBITS, 0 },
628 { ".note", SHT_NOTE, 0 },
629 { ".rodata", SHT_PROGBITS, SHF_ALLOC },
630 { ".rodata1", SHT_PROGBITS, SHF_ALLOC },
631 { ".tbss", SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
632 { ".tdata", SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
633 { ".text", SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
634 { ".init_array",SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
635 { ".fini_array",SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
636 { ".preinit_array",SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
637
638 #ifdef ELF_TC_SPECIAL_SECTIONS
639 ELF_TC_SPECIAL_SECTIONS
640 #endif
641
642 #if 0
643 /* The following section names are special, but they can not
644 reasonably appear in assembler code. Some of the attributes are
645 processor dependent. */
646 { ".dynamic", SHT_DYNAMIC, SHF_ALLOC /* + SHF_WRITE */ },
647 { ".dynstr", SHT_STRTAB, SHF_ALLOC },
648 { ".dynsym", SHT_DYNSYM, SHF_ALLOC },
649 { ".got", SHT_PROGBITS, 0 },
650 { ".hash", SHT_HASH, SHF_ALLOC },
651 { ".interp", SHT_PROGBITS, /* SHF_ALLOC */ },
652 { ".plt", SHT_PROGBITS, 0 },
653 { ".shstrtab",SHT_STRTAB, 0 },
654 { ".strtab", SHT_STRTAB, /* SHF_ALLOC */ },
655 { ".symtab", SHT_SYMTAB, /* SHF_ALLOC */ },
656 #endif
657
658 { NULL, 0, 0 }
659 };
660
661 void
662 obj_elf_change_section (name, type, attr, entsize, group_name, linkonce, push)
663 const char *name;
664 int type;
665 int attr;
666 int entsize;
667 const char *group_name;
668 int linkonce;
669 int push;
670 {
671 asection *old_sec;
672 segT sec;
673 flagword flags;
674 int i;
675
676 #ifdef md_flush_pending_output
677 md_flush_pending_output ();
678 #endif
679
680 /* Switch to the section, creating it if necessary. */
681 if (push)
682 {
683 struct section_stack *elt;
684 elt = xmalloc (sizeof (struct section_stack));
685 elt->next = section_stack;
686 elt->seg = now_seg;
687 elt->prev_seg = previous_section;
688 elt->subseg = now_subseg;
689 elt->prev_subseg = previous_subsection;
690 section_stack = elt;
691 }
692 previous_section = now_seg;
693 previous_subsection = now_subseg;
694
695 old_sec = bfd_get_section_by_name (stdoutput, name);
696 sec = subseg_new (name, 0);
697
698 /* See if this is one of the special sections. */
699 for (i = 0; special_sections[i].name != NULL; i++)
700 if (strcmp (name, special_sections[i].name) == 0)
701 {
702 if (type == SHT_NULL)
703 type = special_sections[i].type;
704 else if (type != special_sections[i].type)
705 {
706 if (old_sec == NULL
707 /* FIXME: gcc, as of 2002-10-22, will emit
708
709 .section .init_array,"aw",@progbits
710
711 for __attribute__ ((section (".init_array"))).
712 "@progbits" is incorrect. */
713 && special_sections[i].type != SHT_INIT_ARRAY
714 && special_sections[i].type != SHT_FINI_ARRAY
715 && special_sections[i].type != SHT_PREINIT_ARRAY)
716 {
717 as_warn (_("setting incorrect section type for %s"), name);
718 }
719 else
720 {
721 as_warn (_("ignoring incorrect section type for %s"), name);
722 type = special_sections[i].type;
723 }
724 }
725 if ((attr &~ special_sections[i].attributes) != 0
726 && old_sec == NULL)
727 {
728 /* As a GNU extension, we permit a .note section to be
729 allocatable. If the linker sees an allocateable .note
730 section, it will create a PT_NOTE segment in the output
731 file. */
732 if (strcmp (name, ".note") != 0
733 || attr != SHF_ALLOC)
734 as_warn (_("setting incorrect section attributes for %s"),
735 name);
736 }
737 attr |= special_sections[i].attributes;
738 break;
739 }
740
741 /* Convert ELF type and flags to BFD flags. */
742 flags = (SEC_RELOC
743 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
744 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
745 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
746 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0)
747 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
748 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0)
749 | ((attr & SHF_TLS) ? SEC_THREAD_LOCAL : 0));
750 #ifdef md_elf_section_flags
751 flags = md_elf_section_flags (flags, attr, type);
752 #endif
753
754 if (old_sec == NULL)
755 {
756 symbolS *secsym;
757
758 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
759 if (type == SHT_NOBITS)
760 seg_info (sec)->bss = 1;
761
762 if (linkonce)
763 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
764 bfd_set_section_flags (stdoutput, sec, flags);
765 if (flags & SEC_MERGE)
766 sec->entsize = entsize;
767 elf_group_name (sec) = group_name;
768
769 /* Add a symbol for this section to the symbol table. */
770 secsym = symbol_find (name);
771 if (secsym != NULL)
772 symbol_set_bfdsym (secsym, sec->symbol);
773 else
774 symbol_table_insert (section_symbol (sec));
775 }
776 else if (attr != 0)
777 {
778 /* If section attributes are specified the second time we see a
779 particular section, then check that they are the same as we
780 saw the first time. */
781 if (((old_sec->flags ^ flags)
782 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
783 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS
784 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
785 | SEC_THREAD_LOCAL)))
786 as_warn (_("ignoring changed section attributes for %s"), name);
787 if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
788 as_warn (_("ignoring changed section entity size for %s"), name);
789 if ((attr & SHF_GROUP) != 0
790 && strcmp (elf_group_name (old_sec), group_name) != 0)
791 as_warn (_("ignoring new section group for %s"), name);
792 }
793
794 #ifdef md_elf_section_change_hook
795 md_elf_section_change_hook ();
796 #endif
797 }
798
799 static int
800 obj_elf_parse_section_letters (str, len)
801 char *str;
802 size_t len;
803 {
804 int attr = 0;
805
806 while (len > 0)
807 {
808 switch (*str)
809 {
810 case 'a':
811 attr |= SHF_ALLOC;
812 break;
813 case 'w':
814 attr |= SHF_WRITE;
815 break;
816 case 'x':
817 attr |= SHF_EXECINSTR;
818 break;
819 case 'M':
820 attr |= SHF_MERGE;
821 break;
822 case 'S':
823 attr |= SHF_STRINGS;
824 break;
825 case 'G':
826 attr |= SHF_GROUP;
827 break;
828 case 'T':
829 attr |= SHF_TLS;
830 break;
831 /* Compatibility. */
832 case 'm':
833 if (*(str - 1) == 'a')
834 {
835 attr |= SHF_MERGE;
836 if (len > 1 && str[1] == 's')
837 {
838 attr |= SHF_STRINGS;
839 str++, len--;
840 }
841 break;
842 }
843 default:
844 {
845 char *bad_msg = _("unrecognized .section attribute: want a,w,x,M,S,G,T");
846 #ifdef md_elf_section_letter
847 int md_attr = md_elf_section_letter (*str, &bad_msg);
848 if (md_attr >= 0)
849 attr |= md_attr;
850 else
851 #endif
852 as_fatal ("%s", bad_msg);
853 }
854 break;
855 }
856 str++, len--;
857 }
858
859 return attr;
860 }
861
862 static int
863 obj_elf_section_word (str, len)
864 char *str;
865 size_t len;
866 {
867 if (len == 5 && strncmp (str, "write", 5) == 0)
868 return SHF_WRITE;
869 if (len == 5 && strncmp (str, "alloc", 5) == 0)
870 return SHF_ALLOC;
871 if (len == 9 && strncmp (str, "execinstr", 9) == 0)
872 return SHF_EXECINSTR;
873 if (len == 3 && strncmp (str, "tls", 3) == 0)
874 return SHF_TLS;
875
876 #ifdef md_elf_section_word
877 {
878 int md_attr = md_elf_section_word (str, len);
879 if (md_attr >= 0)
880 return md_attr;
881 }
882 #endif
883
884 as_warn (_("unrecognized section attribute"));
885 return 0;
886 }
887
888 static int
889 obj_elf_section_type (str, len)
890 char *str;
891 size_t len;
892 {
893 if (len == 8 && strncmp (str, "progbits", 8) == 0)
894 return SHT_PROGBITS;
895 if (len == 6 && strncmp (str, "nobits", 6) == 0)
896 return SHT_NOBITS;
897
898 #ifdef md_elf_section_type
899 {
900 int md_type = md_elf_section_type (str, len);
901 if (md_type >= 0)
902 return md_type;
903 }
904 #endif
905
906 as_warn (_("unrecognized section type"));
907 return 0;
908 }
909
910 /* Get name of section. */
911 static char *
912 obj_elf_section_name ()
913 {
914 char *name;
915
916 SKIP_WHITESPACE ();
917 if (*input_line_pointer == '"')
918 {
919 int dummy;
920
921 name = demand_copy_C_string (&dummy);
922 if (name == NULL)
923 {
924 ignore_rest_of_line ();
925 return NULL;
926 }
927 }
928 else
929 {
930 char *end = input_line_pointer;
931
932 while (0 == strchr ("\n\t,; ", *end))
933 end++;
934 if (end == input_line_pointer)
935 {
936 as_warn (_("missing name"));
937 ignore_rest_of_line ();
938 return NULL;
939 }
940
941 name = xmalloc (end - input_line_pointer + 1);
942 memcpy (name, input_line_pointer, end - input_line_pointer);
943 name[end - input_line_pointer] = '\0';
944 #ifdef tc_canonicalize_section_name
945 name = tc_canonicalize_section_name (name);
946 #endif
947 input_line_pointer = end;
948 }
949 SKIP_WHITESPACE ();
950 return name;
951 }
952
953 void
954 obj_elf_section (push)
955 int push;
956 {
957 char *name, *group_name, *beg;
958 int type, attr, dummy;
959 int entsize;
960 int linkonce;
961
962 #ifndef TC_I370
963 if (flag_mri)
964 {
965 char mri_type;
966
967 #ifdef md_flush_pending_output
968 md_flush_pending_output ();
969 #endif
970
971 previous_section = now_seg;
972 previous_subsection = now_subseg;
973
974 s_mri_sect (&mri_type);
975
976 #ifdef md_elf_section_change_hook
977 md_elf_section_change_hook ();
978 #endif
979
980 return;
981 }
982 #endif /* ! defined (TC_I370) */
983
984 name = obj_elf_section_name ();
985 if (name == NULL)
986 return;
987 type = SHT_NULL;
988 attr = 0;
989 group_name = NULL;
990 entsize = 0;
991 linkonce = 0;
992
993 if (*input_line_pointer == ',')
994 {
995 /* Skip the comma. */
996 ++input_line_pointer;
997 SKIP_WHITESPACE ();
998
999 if (*input_line_pointer == '"')
1000 {
1001 beg = demand_copy_C_string (&dummy);
1002 if (beg == NULL)
1003 {
1004 ignore_rest_of_line ();
1005 return;
1006 }
1007 attr |= obj_elf_parse_section_letters (beg, strlen (beg));
1008
1009 SKIP_WHITESPACE ();
1010 if (*input_line_pointer == ',')
1011 {
1012 char c;
1013 char *save = input_line_pointer;
1014
1015 ++input_line_pointer;
1016 SKIP_WHITESPACE ();
1017 c = *input_line_pointer;
1018 if (c == '"')
1019 {
1020 beg = demand_copy_C_string (&dummy);
1021 if (beg == NULL)
1022 {
1023 ignore_rest_of_line ();
1024 return;
1025 }
1026 type = obj_elf_section_type (beg, strlen (beg));
1027 }
1028 else if (c == '@' || c == '%')
1029 {
1030 beg = ++input_line_pointer;
1031 c = get_symbol_end ();
1032 *input_line_pointer = c;
1033 type = obj_elf_section_type (beg, input_line_pointer - beg);
1034 }
1035 else
1036 input_line_pointer = save;
1037 }
1038
1039 SKIP_WHITESPACE ();
1040 if ((attr & SHF_MERGE) != 0 && *input_line_pointer == ',')
1041 {
1042 ++input_line_pointer;
1043 SKIP_WHITESPACE ();
1044 entsize = get_absolute_expression ();
1045 SKIP_WHITESPACE ();
1046 if (entsize < 0)
1047 {
1048 as_warn (_("invalid merge entity size"));
1049 attr &= ~SHF_MERGE;
1050 entsize = 0;
1051 }
1052 }
1053 else if ((attr & SHF_MERGE) != 0)
1054 {
1055 as_warn (_("entity size for SHF_MERGE not specified"));
1056 attr &= ~SHF_MERGE;
1057 }
1058
1059 if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
1060 {
1061 ++input_line_pointer;
1062 group_name = obj_elf_section_name ();
1063 if (group_name == NULL)
1064 attr &= ~SHF_GROUP;
1065 else if (strncmp (input_line_pointer, ",comdat", 7) == 0)
1066 {
1067 input_line_pointer += 7;
1068 linkonce = 1;
1069 }
1070 else if (strncmp (name, ".gnu.linkonce", 13) == 0)
1071 linkonce = 1;
1072 }
1073 else if ((attr & SHF_GROUP) != 0)
1074 {
1075 as_warn (_("group name for SHF_GROUP not specified"));
1076 attr &= ~SHF_GROUP;
1077 }
1078 }
1079 else
1080 {
1081 do
1082 {
1083 char c;
1084
1085 SKIP_WHITESPACE ();
1086 if (*input_line_pointer != '#')
1087 {
1088 as_warn (_("character following name is not '#'"));
1089 ignore_rest_of_line ();
1090 return;
1091 }
1092 beg = ++input_line_pointer;
1093 c = get_symbol_end ();
1094 *input_line_pointer = c;
1095
1096 attr |= obj_elf_section_word (beg, input_line_pointer - beg);
1097
1098 SKIP_WHITESPACE ();
1099 }
1100 while (*input_line_pointer++ == ',');
1101 --input_line_pointer;
1102 }
1103 }
1104
1105 demand_empty_rest_of_line ();
1106
1107 obj_elf_change_section (name, type, attr, entsize, group_name, linkonce, push);
1108 }
1109
1110 /* Change to the .data section. */
1111
1112 void
1113 obj_elf_data (i)
1114 int i;
1115 {
1116 #ifdef md_flush_pending_output
1117 md_flush_pending_output ();
1118 #endif
1119
1120 previous_section = now_seg;
1121 previous_subsection = now_subseg;
1122 s_data (i);
1123
1124 #ifdef md_elf_section_change_hook
1125 md_elf_section_change_hook ();
1126 #endif
1127 }
1128
1129 /* Change to the .text section. */
1130
1131 void
1132 obj_elf_text (i)
1133 int i;
1134 {
1135 #ifdef md_flush_pending_output
1136 md_flush_pending_output ();
1137 #endif
1138
1139 previous_section = now_seg;
1140 previous_subsection = now_subseg;
1141 s_text (i);
1142
1143 #ifdef md_elf_section_change_hook
1144 md_elf_section_change_hook ();
1145 #endif
1146 }
1147
1148 static void
1149 obj_elf_subsection (ignore)
1150 int ignore ATTRIBUTE_UNUSED;
1151 {
1152 register int temp;
1153
1154 #ifdef md_flush_pending_output
1155 md_flush_pending_output ();
1156 #endif
1157
1158 previous_section = now_seg;
1159 previous_subsection = now_subseg;
1160
1161 temp = get_absolute_expression ();
1162 subseg_set (now_seg, (subsegT) temp);
1163 demand_empty_rest_of_line ();
1164
1165 #ifdef md_elf_section_change_hook
1166 md_elf_section_change_hook ();
1167 #endif
1168 }
1169
1170 /* This can be called from the processor backends if they change
1171 sections. */
1172
1173 void
1174 obj_elf_section_change_hook ()
1175 {
1176 previous_section = now_seg;
1177 previous_subsection = now_subseg;
1178 }
1179
1180 void
1181 obj_elf_previous (ignore)
1182 int ignore ATTRIBUTE_UNUSED;
1183 {
1184 segT new_section;
1185 int new_subsection;
1186
1187 if (previous_section == 0)
1188 {
1189 as_warn (_(".previous without corresponding .section; ignored"));
1190 return;
1191 }
1192
1193 #ifdef md_flush_pending_output
1194 md_flush_pending_output ();
1195 #endif
1196
1197 new_section = previous_section;
1198 new_subsection = previous_subsection;
1199 previous_section = now_seg;
1200 previous_subsection = now_subseg;
1201 subseg_set (new_section, new_subsection);
1202
1203 #ifdef md_elf_section_change_hook
1204 md_elf_section_change_hook ();
1205 #endif
1206 }
1207
1208 static void
1209 obj_elf_popsection (xxx)
1210 int xxx ATTRIBUTE_UNUSED;
1211 {
1212 struct section_stack *top = section_stack;
1213
1214 if (top == NULL)
1215 {
1216 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1217 return;
1218 }
1219
1220 #ifdef md_flush_pending_output
1221 md_flush_pending_output ();
1222 #endif
1223
1224 section_stack = top->next;
1225 previous_section = top->prev_seg;
1226 previous_subsection = top->prev_subseg;
1227 subseg_set (top->seg, top->subseg);
1228 free (top);
1229
1230 #ifdef md_elf_section_change_hook
1231 md_elf_section_change_hook ();
1232 #endif
1233 }
1234
1235 static void
1236 obj_elf_line (ignore)
1237 int ignore ATTRIBUTE_UNUSED;
1238 {
1239 /* Assume delimiter is part of expression. BSD4.2 as fails with
1240 delightful bug, so we are not being incompatible here. */
1241 new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
1242 demand_empty_rest_of_line ();
1243 }
1244
1245 /* This handles the .symver pseudo-op, which is used to specify a
1246 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1247 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1248 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1249 with the same value as the symbol NAME. */
1250
1251 static void
1252 obj_elf_symver (ignore)
1253 int ignore ATTRIBUTE_UNUSED;
1254 {
1255 char *name;
1256 char c;
1257 char old_lexat;
1258 symbolS *sym;
1259
1260 name = input_line_pointer;
1261 c = get_symbol_end ();
1262
1263 sym = symbol_find_or_make (name);
1264
1265 *input_line_pointer = c;
1266
1267 SKIP_WHITESPACE ();
1268 if (*input_line_pointer != ',')
1269 {
1270 as_bad (_("expected comma after name in .symver"));
1271 ignore_rest_of_line ();
1272 return;
1273 }
1274
1275 ++input_line_pointer;
1276 SKIP_WHITESPACE ();
1277 name = input_line_pointer;
1278
1279 /* Temporarily include '@' in symbol names. */
1280 old_lexat = lex_type[(unsigned char) '@'];
1281 lex_type[(unsigned char) '@'] |= LEX_NAME;
1282 c = get_symbol_end ();
1283 lex_type[(unsigned char) '@'] = old_lexat;
1284
1285 if (symbol_get_obj (sym)->versioned_name == NULL)
1286 {
1287 symbol_get_obj (sym)->versioned_name = xstrdup (name);
1288
1289 *input_line_pointer = c;
1290
1291 if (strchr (symbol_get_obj (sym)->versioned_name,
1292 ELF_VER_CHR) == NULL)
1293 {
1294 as_bad (_("missing version name in `%s' for symbol `%s'"),
1295 symbol_get_obj (sym)->versioned_name,
1296 S_GET_NAME (sym));
1297 ignore_rest_of_line ();
1298 return;
1299 }
1300 }
1301 else
1302 {
1303 if (strcmp (symbol_get_obj (sym)->versioned_name, name))
1304 {
1305 as_bad (_("multiple versions [`%s'|`%s'] for symbol `%s'"),
1306 name, symbol_get_obj (sym)->versioned_name,
1307 S_GET_NAME (sym));
1308 ignore_rest_of_line ();
1309 return;
1310 }
1311
1312 *input_line_pointer = c;
1313 }
1314
1315 demand_empty_rest_of_line ();
1316 }
1317
1318 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1319 to the linker the hierarchy in which a particular table resides. The
1320 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1321
1322 struct fix *
1323 obj_elf_vtable_inherit (ignore)
1324 int ignore ATTRIBUTE_UNUSED;
1325 {
1326 char *cname, *pname;
1327 symbolS *csym, *psym;
1328 char c, bad = 0;
1329
1330 if (*input_line_pointer == '#')
1331 ++input_line_pointer;
1332
1333 cname = input_line_pointer;
1334 c = get_symbol_end ();
1335 csym = symbol_find (cname);
1336
1337 /* GCFIXME: should check that we don't have two .vtable_inherits for
1338 the same child symbol. Also, we can currently only do this if the
1339 child symbol is already exists and is placed in a fragment. */
1340
1341 if (csym == NULL || symbol_get_frag (csym) == NULL)
1342 {
1343 as_bad ("expected `%s' to have already been set for .vtable_inherit",
1344 cname);
1345 bad = 1;
1346 }
1347
1348 *input_line_pointer = c;
1349
1350 SKIP_WHITESPACE ();
1351 if (*input_line_pointer != ',')
1352 {
1353 as_bad ("expected comma after name in .vtable_inherit");
1354 ignore_rest_of_line ();
1355 return NULL;
1356 }
1357
1358 ++input_line_pointer;
1359 SKIP_WHITESPACE ();
1360
1361 if (*input_line_pointer == '#')
1362 ++input_line_pointer;
1363
1364 if (input_line_pointer[0] == '0'
1365 && (input_line_pointer[1] == '\0'
1366 || ISSPACE (input_line_pointer[1])))
1367 {
1368 psym = section_symbol (absolute_section);
1369 ++input_line_pointer;
1370 }
1371 else
1372 {
1373 pname = input_line_pointer;
1374 c = get_symbol_end ();
1375 psym = symbol_find_or_make (pname);
1376 *input_line_pointer = c;
1377 }
1378
1379 demand_empty_rest_of_line ();
1380
1381 if (bad)
1382 return NULL;
1383
1384 assert (symbol_get_value_expression (csym)->X_op == O_constant);
1385 return fix_new (symbol_get_frag (csym),
1386 symbol_get_value_expression (csym)->X_add_number,
1387 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
1388 }
1389
1390 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1391 to the linker that a vtable slot was used. The syntax is
1392 ".vtable_entry tablename, offset". */
1393
1394 struct fix *
1395 obj_elf_vtable_entry (ignore)
1396 int ignore ATTRIBUTE_UNUSED;
1397 {
1398 char *name;
1399 symbolS *sym;
1400 offsetT offset;
1401 char c;
1402
1403 if (*input_line_pointer == '#')
1404 ++input_line_pointer;
1405
1406 name = input_line_pointer;
1407 c = get_symbol_end ();
1408 sym = symbol_find_or_make (name);
1409 *input_line_pointer = c;
1410
1411 SKIP_WHITESPACE ();
1412 if (*input_line_pointer != ',')
1413 {
1414 as_bad ("expected comma after name in .vtable_entry");
1415 ignore_rest_of_line ();
1416 return NULL;
1417 }
1418
1419 ++input_line_pointer;
1420 if (*input_line_pointer == '#')
1421 ++input_line_pointer;
1422
1423 offset = get_absolute_expression ();
1424
1425 demand_empty_rest_of_line ();
1426
1427 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1428 BFD_RELOC_VTABLE_ENTRY);
1429 }
1430
1431 void
1432 elf_obj_read_begin_hook ()
1433 {
1434 #ifdef NEED_ECOFF_DEBUG
1435 if (ECOFF_DEBUGGING)
1436 ecoff_read_begin_hook ();
1437 #endif
1438 }
1439
1440 void
1441 elf_obj_symbol_new_hook (symbolP)
1442 symbolS *symbolP;
1443 {
1444 struct elf_obj_sy *sy_obj;
1445
1446 sy_obj = symbol_get_obj (symbolP);
1447 sy_obj->size = NULL;
1448 sy_obj->versioned_name = NULL;
1449
1450 #ifdef NEED_ECOFF_DEBUG
1451 if (ECOFF_DEBUGGING)
1452 ecoff_symbol_new_hook (symbolP);
1453 #endif
1454 }
1455
1456 /* When setting one symbol equal to another, by default we probably
1457 want them to have the same "size", whatever it means in the current
1458 context. */
1459
1460 void
1461 elf_copy_symbol_attributes (dest, src)
1462 symbolS *dest, *src;
1463 {
1464 struct elf_obj_sy *srcelf = symbol_get_obj (src);
1465 struct elf_obj_sy *destelf = symbol_get_obj (dest);
1466 if (srcelf->size)
1467 {
1468 if (destelf->size == NULL)
1469 destelf->size =
1470 (expressionS *) xmalloc (sizeof (expressionS));
1471 *destelf->size = *srcelf->size;
1472 }
1473 else
1474 {
1475 if (destelf->size != NULL)
1476 free (destelf->size);
1477 destelf->size = NULL;
1478 }
1479 S_SET_SIZE (dest, S_GET_SIZE (src));
1480 /* Don't copy visibility. */
1481 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
1482 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
1483 }
1484
1485 void
1486 obj_elf_version (ignore)
1487 int ignore ATTRIBUTE_UNUSED;
1488 {
1489 char *name;
1490 unsigned int c;
1491 char *p;
1492 asection *seg = now_seg;
1493 subsegT subseg = now_subseg;
1494 Elf_Internal_Note i_note;
1495 Elf_External_Note e_note;
1496 asection *note_secp = (asection *) NULL;
1497 int len;
1498
1499 SKIP_WHITESPACE ();
1500 if (*input_line_pointer == '\"')
1501 {
1502 ++input_line_pointer; /* -> 1st char of string. */
1503 name = input_line_pointer;
1504
1505 while (is_a_char (c = next_char_of_string ()))
1506 ;
1507 c = *input_line_pointer;
1508 *input_line_pointer = '\0';
1509 *(input_line_pointer - 1) = '\0';
1510 *input_line_pointer = c;
1511
1512 /* create the .note section */
1513
1514 note_secp = subseg_new (".note", 0);
1515 bfd_set_section_flags (stdoutput,
1516 note_secp,
1517 SEC_HAS_CONTENTS | SEC_READONLY);
1518
1519 /* process the version string */
1520
1521 len = strlen (name);
1522
1523 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
1524 i_note.descsz = 0; /* no description */
1525 i_note.type = NT_VERSION;
1526 p = frag_more (sizeof (e_note.namesz));
1527 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
1528 p = frag_more (sizeof (e_note.descsz));
1529 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
1530 p = frag_more (sizeof (e_note.type));
1531 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
1532 p = frag_more (len + 1);
1533 strcpy (p, name);
1534
1535 frag_align (2, 0, 0);
1536
1537 subseg_set (seg, subseg);
1538 }
1539 else
1540 {
1541 as_bad (_("expected quoted string"));
1542 }
1543 demand_empty_rest_of_line ();
1544 }
1545
1546 static void
1547 obj_elf_size (ignore)
1548 int ignore ATTRIBUTE_UNUSED;
1549 {
1550 char *name = input_line_pointer;
1551 char c = get_symbol_end ();
1552 char *p;
1553 expressionS exp;
1554 symbolS *sym;
1555
1556 p = input_line_pointer;
1557 *p = c;
1558 SKIP_WHITESPACE ();
1559 if (*input_line_pointer != ',')
1560 {
1561 *p = 0;
1562 as_bad (_("expected comma after name `%s' in .size directive"), name);
1563 *p = c;
1564 ignore_rest_of_line ();
1565 return;
1566 }
1567 input_line_pointer++;
1568 expression (&exp);
1569 if (exp.X_op == O_absent)
1570 {
1571 as_bad (_("missing expression in .size directive"));
1572 exp.X_op = O_constant;
1573 exp.X_add_number = 0;
1574 }
1575 *p = 0;
1576 sym = symbol_find_or_make (name);
1577 *p = c;
1578 if (exp.X_op == O_constant)
1579 {
1580 S_SET_SIZE (sym, exp.X_add_number);
1581 if (symbol_get_obj (sym)->size)
1582 {
1583 xfree (symbol_get_obj (sym)->size);
1584 symbol_get_obj (sym)->size = NULL;
1585 }
1586 }
1587 else
1588 {
1589 symbol_get_obj (sym)->size =
1590 (expressionS *) xmalloc (sizeof (expressionS));
1591 *symbol_get_obj (sym)->size = exp;
1592 }
1593 demand_empty_rest_of_line ();
1594 }
1595
1596 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
1597 There are five syntaxes:
1598
1599 The first (used on Solaris) is
1600 .type SYM,#function
1601 The second (used on UnixWare) is
1602 .type SYM,@function
1603 The third (reportedly to be used on Irix 6.0) is
1604 .type SYM STT_FUNC
1605 The fourth (used on NetBSD/Arm and Linux/ARM) is
1606 .type SYM,%function
1607 The fifth (used on SVR4/860) is
1608 .type SYM,"function"
1609 */
1610
1611 static void
1612 obj_elf_type (ignore)
1613 int ignore ATTRIBUTE_UNUSED;
1614 {
1615 char *name;
1616 char c;
1617 int type;
1618 const char *typename;
1619 symbolS *sym;
1620 elf_symbol_type *elfsym;
1621
1622 name = input_line_pointer;
1623 c = get_symbol_end ();
1624 sym = symbol_find_or_make (name);
1625 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
1626 *input_line_pointer = c;
1627
1628 SKIP_WHITESPACE ();
1629 if (*input_line_pointer == ',')
1630 ++input_line_pointer;
1631
1632 SKIP_WHITESPACE ();
1633 if ( *input_line_pointer == '#'
1634 || *input_line_pointer == '@'
1635 || *input_line_pointer == '"'
1636 || *input_line_pointer == '%')
1637 ++input_line_pointer;
1638
1639 typename = input_line_pointer;
1640 c = get_symbol_end ();
1641
1642 type = 0;
1643 if (strcmp (typename, "function") == 0
1644 || strcmp (typename, "STT_FUNC") == 0)
1645 type = BSF_FUNCTION;
1646 else if (strcmp (typename, "object") == 0
1647 || strcmp (typename, "STT_OBJECT") == 0)
1648 type = BSF_OBJECT;
1649 else if (strcmp (typename, "tls_object") == 0
1650 || strcmp (typename, "STT_TLS") == 0)
1651 type = BSF_OBJECT | BSF_THREAD_LOCAL;
1652 else if (strcmp (typename, "notype") == 0
1653 || strcmp (typename, "STT_NOTYPE") == 0)
1654 ;
1655 #ifdef md_elf_symbol_type
1656 else if ((type = md_elf_symbol_type (typename, sym, elfsym)) != -1)
1657 ;
1658 #endif
1659 else
1660 as_bad (_("unrecognized symbol type \"%s\""), typename);
1661
1662 *input_line_pointer = c;
1663
1664 if (*input_line_pointer == '"')
1665 ++input_line_pointer;
1666
1667 elfsym->symbol.flags |= type;
1668
1669 demand_empty_rest_of_line ();
1670 }
1671
1672 static void
1673 obj_elf_ident (ignore)
1674 int ignore ATTRIBUTE_UNUSED;
1675 {
1676 static segT comment_section;
1677 segT old_section = now_seg;
1678 int old_subsection = now_subseg;
1679
1680 #ifdef md_flush_pending_output
1681 md_flush_pending_output ();
1682 #endif
1683
1684 if (!comment_section)
1685 {
1686 char *p;
1687 comment_section = subseg_new (".comment", 0);
1688 bfd_set_section_flags (stdoutput, comment_section,
1689 SEC_READONLY | SEC_HAS_CONTENTS);
1690 p = frag_more (1);
1691 *p = 0;
1692 }
1693 else
1694 subseg_set (comment_section, 0);
1695 stringer (1);
1696 subseg_set (old_section, old_subsection);
1697 }
1698
1699 #ifdef INIT_STAB_SECTION
1700
1701 /* The first entry in a .stabs section is special. */
1702
1703 void
1704 obj_elf_init_stab_section (seg)
1705 segT seg;
1706 {
1707 char *file;
1708 char *p;
1709 char *stabstr_name;
1710 unsigned int stroff;
1711
1712 /* Force the section to align to a longword boundary. Without this,
1713 UnixWare ar crashes. */
1714 bfd_set_section_alignment (stdoutput, seg, 2);
1715
1716 /* Make space for this first symbol. */
1717 p = frag_more (12);
1718 /* Zero it out. */
1719 memset (p, 0, 12);
1720 as_where (&file, (unsigned int *) NULL);
1721 stabstr_name = (char *) xmalloc (strlen (segment_name (seg)) + 4);
1722 strcpy (stabstr_name, segment_name (seg));
1723 strcat (stabstr_name, "str");
1724 stroff = get_stab_string_offset (file, stabstr_name);
1725 know (stroff == 1);
1726 md_number_to_chars (p, stroff, 4);
1727 seg_info (seg)->stabu.p = p;
1728 }
1729
1730 #endif
1731
1732 /* Fill in the counts in the first entry in a .stabs section. */
1733
1734 static void
1735 adjust_stab_sections (abfd, sec, xxx)
1736 bfd *abfd;
1737 asection *sec;
1738 PTR xxx ATTRIBUTE_UNUSED;
1739 {
1740 char *name;
1741 asection *strsec;
1742 char *p;
1743 int strsz, nsyms;
1744
1745 if (strncmp (".stab", sec->name, 5))
1746 return;
1747 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
1748 return;
1749
1750 name = (char *) alloca (strlen (sec->name) + 4);
1751 strcpy (name, sec->name);
1752 strcat (name, "str");
1753 strsec = bfd_get_section_by_name (abfd, name);
1754 if (strsec)
1755 strsz = bfd_section_size (abfd, strsec);
1756 else
1757 strsz = 0;
1758 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
1759
1760 p = seg_info (sec)->stabu.p;
1761 assert (p != 0);
1762
1763 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
1764 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
1765 }
1766
1767 #ifdef NEED_ECOFF_DEBUG
1768
1769 /* This function is called by the ECOFF code. It is supposed to
1770 record the external symbol information so that the backend can
1771 write it out correctly. The ELF backend doesn't actually handle
1772 this at the moment, so we do it ourselves. We save the information
1773 in the symbol. */
1774
1775 void
1776 elf_ecoff_set_ext (sym, ext)
1777 symbolS *sym;
1778 struct ecoff_extr *ext;
1779 {
1780 symbol_get_bfdsym (sym)->udata.p = (PTR) ext;
1781 }
1782
1783 /* This function is called by bfd_ecoff_debug_externals. It is
1784 supposed to *EXT to the external symbol information, and return
1785 whether the symbol should be used at all. */
1786
1787 static bfd_boolean
1788 elf_get_extr (sym, ext)
1789 asymbol *sym;
1790 EXTR *ext;
1791 {
1792 if (sym->udata.p == NULL)
1793 return FALSE;
1794 *ext = *(EXTR *) sym->udata.p;
1795 return TRUE;
1796 }
1797
1798 /* This function is called by bfd_ecoff_debug_externals. It has
1799 nothing to do for ELF. */
1800
1801 /*ARGSUSED*/
1802 static void
1803 elf_set_index (sym, indx)
1804 asymbol *sym ATTRIBUTE_UNUSED;
1805 bfd_size_type indx ATTRIBUTE_UNUSED;
1806 {
1807 }
1808
1809 #endif /* NEED_ECOFF_DEBUG */
1810
1811 void
1812 elf_frob_symbol (symp, puntp)
1813 symbolS *symp;
1814 int *puntp;
1815 {
1816 struct elf_obj_sy *sy_obj;
1817
1818 #ifdef NEED_ECOFF_DEBUG
1819 if (ECOFF_DEBUGGING)
1820 ecoff_frob_symbol (symp);
1821 #endif
1822
1823 sy_obj = symbol_get_obj (symp);
1824
1825 if (sy_obj->size != NULL)
1826 {
1827 switch (sy_obj->size->X_op)
1828 {
1829 case O_subtract:
1830 S_SET_SIZE (symp,
1831 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1832 + sy_obj->size->X_add_number
1833 - S_GET_VALUE (sy_obj->size->X_op_symbol)));
1834 break;
1835 case O_constant:
1836 S_SET_SIZE (symp,
1837 (S_GET_VALUE (sy_obj->size->X_add_symbol)
1838 + sy_obj->size->X_add_number));
1839 break;
1840 default:
1841 as_bad (_(".size expression too complicated to fix up"));
1842 break;
1843 }
1844 free (sy_obj->size);
1845 sy_obj->size = NULL;
1846 }
1847
1848 if (sy_obj->versioned_name != NULL)
1849 {
1850 char *p;
1851
1852 p = strchr (sy_obj->versioned_name, ELF_VER_CHR);
1853 know (p != NULL);
1854
1855 /* This symbol was given a new name with the .symver directive.
1856
1857 If this is an external reference, just rename the symbol to
1858 include the version string. This will make the relocs be
1859 against the correct versioned symbol.
1860
1861 If this is a definition, add an alias. FIXME: Using an alias
1862 will permit the debugging information to refer to the right
1863 symbol. However, it's not clear whether it is the best
1864 approach. */
1865
1866 if (! S_IS_DEFINED (symp))
1867 {
1868 /* Verify that the name isn't using the @@ syntax--this is
1869 reserved for definitions of the default version to link
1870 against. */
1871 if (p[1] == ELF_VER_CHR)
1872 {
1873 as_bad (_("invalid attempt to declare external version name as default in symbol `%s'"),
1874 sy_obj->versioned_name);
1875 *puntp = TRUE;
1876 }
1877 S_SET_NAME (symp, sy_obj->versioned_name);
1878 }
1879 else
1880 {
1881 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
1882 {
1883 size_t l;
1884
1885 /* The @@@ syntax is a special case. It renames the
1886 symbol name to versioned_name with one `@' removed. */
1887 l = strlen (&p[3]) + 1;
1888 memmove (&p [2], &p[3], l);
1889 S_SET_NAME (symp, sy_obj->versioned_name);
1890 }
1891 else
1892 {
1893 symbolS *symp2;
1894
1895 /* FIXME: Creating a new symbol here is risky. We're
1896 in the final loop over the symbol table. We can
1897 get away with it only because the symbol goes to
1898 the end of the list, where the loop will still see
1899 it. It would probably be better to do this in
1900 obj_frob_file_before_adjust. */
1901
1902 symp2 = symbol_find_or_make (sy_obj->versioned_name);
1903
1904 /* Now we act as though we saw symp2 = sym. */
1905
1906 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
1907
1908 /* Subtracting out the frag address here is a hack
1909 because we are in the middle of the final loop. */
1910 S_SET_VALUE (symp2,
1911 (S_GET_VALUE (symp)
1912 - symbol_get_frag (symp)->fr_address));
1913
1914 symbol_set_frag (symp2, symbol_get_frag (symp));
1915
1916 /* This will copy over the size information. */
1917 copy_symbol_attributes (symp2, symp);
1918
1919 S_SET_OTHER (symp2, S_GET_OTHER (symp));
1920
1921 if (S_IS_WEAK (symp))
1922 S_SET_WEAK (symp2);
1923
1924 if (S_IS_EXTERNAL (symp))
1925 S_SET_EXTERNAL (symp2);
1926 }
1927 }
1928 }
1929
1930 /* Double check weak symbols. */
1931 if (S_IS_WEAK (symp))
1932 {
1933 if (S_IS_COMMON (symp))
1934 as_bad (_("symbol `%s' can not be both weak and common"),
1935 S_GET_NAME (symp));
1936 }
1937
1938 #ifdef TC_MIPS
1939 /* The Irix 5 and 6 assemblers set the type of any common symbol and
1940 any undefined non-function symbol to STT_OBJECT. We try to be
1941 compatible, since newer Irix 5 and 6 linkers care. However, we
1942 only set undefined symbols to be STT_OBJECT if we are on Irix,
1943 because that is the only time gcc will generate the necessary
1944 .global directives to mark functions. */
1945
1946 if (S_IS_COMMON (symp))
1947 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1948
1949 if (strstr (TARGET_OS, "irix") != NULL
1950 && ! S_IS_DEFINED (symp)
1951 && (symbol_get_bfdsym (symp)->flags & BSF_FUNCTION) == 0)
1952 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1953 #endif
1954
1955 #if 0 /* TC_PPC */
1956 /* If TC_PPC is defined, we used to force the type of a symbol to be
1957 BSF_OBJECT if it was otherwise unset. This was required by some
1958 version of VxWorks. Thomas de Lellis <tdel@windriver.com> says
1959 that this is no longer needed, so it is now commented out. */
1960 if ((symbol_get_bfdsym (symp)->flags
1961 & (BSF_FUNCTION | BSF_FILE | BSF_SECTION_SYM)) == 0
1962 && S_IS_DEFINED (symp))
1963 symbol_get_bfdsym (symp)->flags |= BSF_OBJECT;
1964 #endif
1965 }
1966
1967 struct group_list
1968 {
1969 asection **head; /* Section lists. */
1970 unsigned int *elt_count; /* Number of sections in each list. */
1971 unsigned int num_group; /* Number of lists. */
1972 };
1973
1974 /* Called via bfd_map_over_sections. If SEC is a member of a group,
1975 add it to a list of sections belonging to the group. INF is a
1976 pointer to a struct group_list, which is where we store the head of
1977 each list. */
1978
1979 static void
1980 build_group_lists (abfd, sec, inf)
1981 bfd *abfd ATTRIBUTE_UNUSED;
1982 asection *sec;
1983 PTR inf;
1984 {
1985 struct group_list *list = (struct group_list *) inf;
1986 const char *group_name = elf_group_name (sec);
1987 unsigned int i;
1988
1989 if (group_name == NULL)
1990 return;
1991
1992 /* If this group already has a list, add the section to the head of
1993 the list. */
1994 for (i = 0; i < list->num_group; i++)
1995 {
1996 if (strcmp (group_name, elf_group_name (list->head[i])) == 0)
1997 {
1998 elf_next_in_group (sec) = list->head[i];
1999 list->head[i] = sec;
2000 list->elt_count[i] += 1;
2001 return;
2002 }
2003 }
2004
2005 /* New group. Make the arrays bigger in chunks to minimize calls to
2006 realloc. */
2007 i = list->num_group;
2008 if ((i & 127) == 0)
2009 {
2010 unsigned int newsize = i + 128;
2011 list->head = xrealloc (list->head, newsize * sizeof (*list->head));
2012 list->elt_count = xrealloc (list->elt_count,
2013 newsize * sizeof (*list->elt_count));
2014 }
2015 list->head[i] = sec;
2016 list->elt_count[i] = 1;
2017 list->num_group += 1;
2018 }
2019
2020 void
2021 elf_frob_file ()
2022 {
2023 struct group_list list;
2024 unsigned int i;
2025
2026 bfd_map_over_sections (stdoutput, adjust_stab_sections, (PTR) 0);
2027
2028 /* Go find section groups. */
2029 list.num_group = 0;
2030 list.head = NULL;
2031 list.elt_count = NULL;
2032 bfd_map_over_sections (stdoutput, build_group_lists, (PTR) &list);
2033
2034 /* Make the SHT_GROUP sections that describe each section group. We
2035 can't set up the section contents here yet, because elf section
2036 indices have yet to be calculated. elf.c:set_group_contents does
2037 the rest of the work. */
2038 for (i = 0; i < list.num_group; i++)
2039 {
2040 const char *group_name = elf_group_name (list.head[i]);
2041 const char *sec_name;
2042 asection *s;
2043 flagword flags;
2044 struct symbol *sy;
2045 int has_sym;
2046
2047 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
2048 for (s = list.head[i]; s != NULL; s = elf_next_in_group (s))
2049 if ((s->flags ^ flags) & SEC_LINK_ONCE)
2050 {
2051 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2052 if (s != list.head[i])
2053 {
2054 as_warn (_("assuming all members of group `%s' are COMDAT"),
2055 group_name);
2056 break;
2057 }
2058 }
2059
2060 sec_name = group_name;
2061 sy = symbol_find_exact (group_name);
2062 has_sym = 0;
2063 if (sy != NULL
2064 && (sy == symbol_lastP
2065 || (sy->sy_next != NULL
2066 && sy->sy_next->sy_previous == sy)))
2067 {
2068 has_sym = 1;
2069 sec_name = ".group";
2070 }
2071 s = subseg_force_new (sec_name, 0);
2072 if (s == NULL
2073 || !bfd_set_section_flags (stdoutput, s, flags)
2074 || !bfd_set_section_alignment (stdoutput, s, 2))
2075 {
2076 as_fatal (_("can't create group: %s"),
2077 bfd_errmsg (bfd_get_error ()));
2078 }
2079
2080 /* Pass a pointer to the first section in this group. */
2081 elf_next_in_group (s) = list.head[i];
2082 if (has_sym)
2083 elf_group_id (s) = sy->bsym;
2084
2085 s->_raw_size = 4 * (list.elt_count[i] + 1);
2086 s->contents = frag_more (s->_raw_size);
2087 frag_now->fr_fix = frag_now_fix_octets ();
2088 }
2089
2090 #ifdef elf_tc_final_processing
2091 elf_tc_final_processing ();
2092 #endif
2093 }
2094
2095 /* It removes any unneeded versioned symbols from the symbol table. */
2096
2097 void
2098 elf_frob_file_before_adjust ()
2099 {
2100 if (symbol_rootP)
2101 {
2102 symbolS *symp;
2103
2104 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2105 if (!S_IS_DEFINED (symp))
2106 {
2107 if (symbol_get_obj (symp)->versioned_name)
2108 {
2109 char *p;
2110
2111 /* The @@@ syntax is a special case. If the symbol is
2112 not defined, 2 `@'s will be removed from the
2113 versioned_name. */
2114
2115 p = strchr (symbol_get_obj (symp)->versioned_name,
2116 ELF_VER_CHR);
2117 know (p != NULL);
2118 if (p [1] == ELF_VER_CHR && p [2] == ELF_VER_CHR)
2119 {
2120 size_t l = strlen (&p[3]) + 1;
2121 memmove (&p [1], &p[3], l);
2122 }
2123 if (symbol_used_p (symp) == 0
2124 && symbol_used_in_reloc_p (symp) == 0)
2125 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2126 }
2127
2128 /* If there was .weak foo, but foo was neither defined nor
2129 used anywhere, remove it. */
2130
2131 else if (S_IS_WEAK (symp)
2132 && symbol_used_p (symp) == 0
2133 && symbol_used_in_reloc_p (symp) == 0)
2134 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2135 }
2136 }
2137 }
2138
2139 /* It is required that we let write_relocs have the opportunity to
2140 optimize away fixups before output has begun, since it is possible
2141 to eliminate all fixups for a section and thus we never should
2142 have generated the relocation section. */
2143
2144 void
2145 elf_frob_file_after_relocs ()
2146 {
2147 #ifdef NEED_ECOFF_DEBUG
2148 if (ECOFF_DEBUGGING)
2149 /* Generate the ECOFF debugging information. */
2150 {
2151 const struct ecoff_debug_swap *debug_swap;
2152 struct ecoff_debug_info debug;
2153 char *buf;
2154 asection *sec;
2155
2156 debug_swap
2157 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
2158 know (debug_swap != (const struct ecoff_debug_swap *) NULL);
2159 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
2160
2161 /* Set up the pointers in debug. */
2162 #define SET(ptr, offset, type) \
2163 debug.ptr = (type) (buf + debug.symbolic_header.offset)
2164
2165 SET (line, cbLineOffset, unsigned char *);
2166 SET (external_dnr, cbDnOffset, PTR);
2167 SET (external_pdr, cbPdOffset, PTR);
2168 SET (external_sym, cbSymOffset, PTR);
2169 SET (external_opt, cbOptOffset, PTR);
2170 SET (external_aux, cbAuxOffset, union aux_ext *);
2171 SET (ss, cbSsOffset, char *);
2172 SET (external_fdr, cbFdOffset, PTR);
2173 SET (external_rfd, cbRfdOffset, PTR);
2174 /* ssext and external_ext are set up just below. */
2175
2176 #undef SET
2177
2178 /* Set up the external symbols. */
2179 debug.ssext = debug.ssext_end = NULL;
2180 debug.external_ext = debug.external_ext_end = NULL;
2181 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, TRUE,
2182 elf_get_extr, elf_set_index))
2183 as_fatal (_("failed to set up debugging information: %s"),
2184 bfd_errmsg (bfd_get_error ()));
2185
2186 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
2187 assert (sec != NULL);
2188
2189 know (!stdoutput->output_has_begun);
2190
2191 /* We set the size of the section, call bfd_set_section_contents
2192 to force the ELF backend to allocate a file position, and then
2193 write out the data. FIXME: Is this really the best way to do
2194 this? */
2195 sec->_raw_size = bfd_ecoff_debug_size (stdoutput, &debug, debug_swap);
2196
2197 /* Pass BUF to bfd_set_section_contents because this will
2198 eventually become a call to fwrite, and ISO C prohibits
2199 passing a NULL pointer to a stdio function even if the
2200 pointer will not be used. */
2201 if (! bfd_set_section_contents (stdoutput, sec, (PTR) buf,
2202 (file_ptr) 0, (bfd_size_type) 0))
2203 as_fatal (_("can't start writing .mdebug section: %s"),
2204 bfd_errmsg (bfd_get_error ()));
2205
2206 know (stdoutput->output_has_begun);
2207 know (sec->filepos != 0);
2208
2209 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
2210 sec->filepos))
2211 as_fatal (_("could not write .mdebug section: %s"),
2212 bfd_errmsg (bfd_get_error ()));
2213 }
2214 #endif /* NEED_ECOFF_DEBUG */
2215 }
2216
2217 #ifdef SCO_ELF
2218
2219 /* Heavily plagarized from obj_elf_version. The idea is to emit the
2220 SCO specific identifier in the .notes section to satisfy the SCO
2221 linker.
2222
2223 This looks more complicated than it really is. As opposed to the
2224 "obvious" solution, this should handle the cross dev cases
2225 correctly. (i.e, hosting on a 64 bit big endian processor, but
2226 generating SCO Elf code) Efficiency isn't a concern, as there
2227 should be exactly one of these sections per object module.
2228
2229 SCO OpenServer 5 identifies it's ELF modules with a standard ELF
2230 .note section.
2231
2232 int_32 namesz = 4 ; Name size
2233 int_32 descsz = 12 ; Descriptive information
2234 int_32 type = 1 ;
2235 char name[4] = "SCO" ; Originator name ALWAYS SCO + NULL
2236 int_32 version = (major ver # << 16) | version of tools ;
2237 int_32 source = (tool_id << 16 ) | 1 ;
2238 int_32 info = 0 ; These are set by the SCO tools, but we
2239 don't know enough about the source
2240 environment to set them. SCO ld currently
2241 ignores them, and recommends we set them
2242 to zero. */
2243
2244 #define SCO_MAJOR_VERSION 0x1
2245 #define SCO_MINOR_VERSION 0x1
2246
2247 void
2248 sco_id ()
2249 {
2250
2251 char *name;
2252 unsigned int c;
2253 char ch;
2254 char *p;
2255 asection *seg = now_seg;
2256 subsegT subseg = now_subseg;
2257 Elf_Internal_Note i_note;
2258 Elf_External_Note e_note;
2259 asection *note_secp = (asection *) NULL;
2260 int i, len;
2261
2262 /* create the .note section */
2263
2264 note_secp = subseg_new (".note", 0);
2265 bfd_set_section_flags (stdoutput,
2266 note_secp,
2267 SEC_HAS_CONTENTS | SEC_READONLY);
2268
2269 /* process the version string */
2270
2271 i_note.namesz = 4;
2272 i_note.descsz = 12; /* 12 descriptive bytes */
2273 i_note.type = NT_VERSION; /* Contains a version string */
2274
2275 p = frag_more (sizeof (i_note.namesz));
2276 md_number_to_chars (p, (valueT) i_note.namesz, 4);
2277
2278 p = frag_more (sizeof (i_note.descsz));
2279 md_number_to_chars (p, (valueT) i_note.descsz, 4);
2280
2281 p = frag_more (sizeof (i_note.type));
2282 md_number_to_chars (p, (valueT) i_note.type, 4);
2283
2284 p = frag_more (4);
2285 strcpy (p, "SCO");
2286
2287 /* Note: this is the version number of the ELF we're representing */
2288 p = frag_more (4);
2289 md_number_to_chars (p, (SCO_MAJOR_VERSION << 16) | (SCO_MINOR_VERSION), 4);
2290
2291 /* Here, we pick a magic number for ourselves (yes, I "registered"
2292 it with SCO. The bottom bit shows that we are compat with the
2293 SCO ABI. */
2294 p = frag_more (4);
2295 md_number_to_chars (p, 0x4c520000 | 0x0001, 4);
2296
2297 /* If we knew (or cared) what the source language options were, we'd
2298 fill them in here. SCO has given us permission to ignore these
2299 and just set them to zero. */
2300 p = frag_more (4);
2301 md_number_to_chars (p, 0x0000, 4);
2302
2303 frag_align (2, 0, 0);
2304
2305 /* We probably can't restore the current segment, for there likely
2306 isn't one yet... */
2307 if (seg && subseg)
2308 subseg_set (seg, subseg);
2309
2310 }
2311
2312 #endif /* SCO_ELF */
2313
2314 static int
2315 elf_separate_stab_sections ()
2316 {
2317 #ifdef NEED_ECOFF_DEBUG
2318 return (!ECOFF_DEBUGGING);
2319 #else
2320 return 1;
2321 #endif
2322 }
2323
2324 static void
2325 elf_init_stab_section (seg)
2326 segT seg;
2327 {
2328 #ifdef NEED_ECOFF_DEBUG
2329 if (!ECOFF_DEBUGGING)
2330 #endif
2331 obj_elf_init_stab_section (seg);
2332 }
2333
2334 const struct format_ops elf_format_ops =
2335 {
2336 bfd_target_elf_flavour,
2337 0, /* dfl_leading_underscore */
2338 1, /* emit_section_symbols */
2339 elf_begin,
2340 elf_file_symbol,
2341 elf_frob_symbol,
2342 elf_frob_file,
2343 elf_frob_file_before_adjust,
2344 0, /* obj_frob_file_before_fix */
2345 elf_frob_file_after_relocs,
2346 elf_s_get_size, elf_s_set_size,
2347 elf_s_get_align, elf_s_set_align,
2348 elf_s_get_other,
2349 elf_s_set_other,
2350 0, /* s_get_desc */
2351 0, /* s_set_desc */
2352 0, /* s_get_type */
2353 0, /* s_set_type */
2354 elf_copy_symbol_attributes,
2355 #ifdef NEED_ECOFF_DEBUG
2356 ecoff_generate_asm_lineno,
2357 ecoff_stab,
2358 #else
2359 0, /* generate_asm_lineno */
2360 0, /* process_stab */
2361 #endif
2362 elf_separate_stab_sections,
2363 elf_init_stab_section,
2364 elf_sec_sym_ok_for_reloc,
2365 elf_pop_insert,
2366 #ifdef NEED_ECOFF_DEBUG
2367 elf_ecoff_set_ext,
2368 #else
2369 0, /* ecoff_set_ext */
2370 #endif
2371 elf_obj_read_begin_hook,
2372 elf_obj_symbol_new_hook
2373 };