source: branches/libc-0.6/src/binutils/bfd/coffcode.h

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 153.8 KB
Line 
1/* Support for the generic parts of most COFF variants, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23/*
24Most of this hacked by Steve Chamberlain,
25 sac@cygnus.com
26*/
27/*
28
29SECTION
30 coff backends
31
32 BFD supports a number of different flavours of coff format.
33 The major differences between formats are the sizes and
34 alignments of fields in structures on disk, and the occasional
35 extra field.
36
37 Coff in all its varieties is implemented with a few common
38 files and a number of implementation specific files. For
39 example, The 88k bcs coff format is implemented in the file
40 @file{coff-m88k.c}. This file @code{#include}s
41 @file{coff/m88k.h} which defines the external structure of the
42 coff format for the 88k, and @file{coff/internal.h} which
43 defines the internal structure. @file{coff-m88k.c} also
44 defines the relocations used by the 88k format
45 @xref{Relocations}.
46
47 The Intel i960 processor version of coff is implemented in
48 @file{coff-i960.c}. This file has the same structure as
49 @file{coff-m88k.c}, except that it includes @file{coff/i960.h}
50 rather than @file{coff-m88k.h}.
51
52SUBSECTION
53 Porting to a new version of coff
54
55 The recommended method is to select from the existing
56 implementations the version of coff which is most like the one
57 you want to use. For example, we'll say that i386 coff is
58 the one you select, and that your coff flavour is called foo.
59 Copy @file{i386coff.c} to @file{foocoff.c}, copy
60 @file{../include/coff/i386.h} to @file{../include/coff/foo.h},
61 and add the lines to @file{targets.c} and @file{Makefile.in}
62 so that your new back end is used. Alter the shapes of the
63 structures in @file{../include/coff/foo.h} so that they match
64 what you need. You will probably also have to add
65 @code{#ifdef}s to the code in @file{coff/internal.h} and
66 @file{coffcode.h} if your version of coff is too wild.
67
68 You can verify that your new BFD backend works quite simply by
69 building @file{objdump} from the @file{binutils} directory,
70 and making sure that its version of what's going on and your
71 host system's idea (assuming it has the pretty standard coff
72 dump utility, usually called @code{att-dump} or just
73 @code{dump}) are the same. Then clean up your code, and send
74 what you've done to Cygnus. Then your stuff will be in the
75 next release, and you won't have to keep integrating it.
76
77SUBSECTION
78 How the coff backend works
79
80SUBSUBSECTION
81 File layout
82
83 The Coff backend is split into generic routines that are
84 applicable to any Coff target and routines that are specific
85 to a particular target. The target-specific routines are
86 further split into ones which are basically the same for all
87 Coff targets except that they use the external symbol format
88 or use different values for certain constants.
89
90 The generic routines are in @file{coffgen.c}. These routines
91 work for any Coff target. They use some hooks into the target
92 specific code; the hooks are in a @code{bfd_coff_backend_data}
93 structure, one of which exists for each target.
94
95 The essentially similar target-specific routines are in
96 @file{coffcode.h}. This header file includes executable C code.
97 The various Coff targets first include the appropriate Coff
98 header file, make any special defines that are needed, and
99 then include @file{coffcode.h}.
100
101 Some of the Coff targets then also have additional routines in
102 the target source file itself.
103
104 For example, @file{coff-i960.c} includes
105 @file{coff/internal.h} and @file{coff/i960.h}. It then
106 defines a few constants, such as @code{I960}, and includes
107 @file{coffcode.h}. Since the i960 has complex relocation
108 types, @file{coff-i960.c} also includes some code to
109 manipulate the i960 relocs. This code is not in
110 @file{coffcode.h} because it would not be used by any other
111 target.
112
113SUBSUBSECTION
114 Bit twiddling
115
116 Each flavour of coff supported in BFD has its own header file
117 describing the external layout of the structures. There is also
118 an internal description of the coff layout, in
119 @file{coff/internal.h}. A major function of the
120 coff backend is swapping the bytes and twiddling the bits to
121 translate the external form of the structures into the normal
122 internal form. This is all performed in the
123 @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
124 elements are different sizes between different versions of
125 coff; it is the duty of the coff version specific include file
126 to override the definitions of various packing routines in
127 @file{coffcode.h}. E.g., the size of line number entry in coff is
128 sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
129 @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
130 correct one. No doubt, some day someone will find a version of
131 coff which has a varying field size not catered to at the
132 moment. To port BFD, that person will have to add more @code{#defines}.
133 Three of the bit twiddling routines are exported to
134 @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
135 and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol
136 table on its own, but uses BFD to fix things up. More of the
137 bit twiddlers are exported for @code{gas};
138 @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
139 @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
140 @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
141 @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
142 of all the symbol table and reloc drudgery itself, thereby
143 saving the internal BFD overhead, but uses BFD to swap things
144 on the way out, making cross ports much safer. Doing so also
145 allows BFD (and thus the linker) to use the same header files
146 as @code{gas}, which makes one avenue to disaster disappear.
147
148SUBSUBSECTION
149 Symbol reading
150
151 The simple canonical form for symbols used by BFD is not rich
152 enough to keep all the information available in a coff symbol
153 table. The back end gets around this problem by keeping the original
154 symbol table around, "behind the scenes".
155
156 When a symbol table is requested (through a call to
157 @code{bfd_canonicalize_symtab}), a request gets through to
158 @code{coff_get_normalized_symtab}. This reads the symbol table from
159 the coff file and swaps all the structures inside into the
160 internal form. It also fixes up all the pointers in the table
161 (represented in the file by offsets from the first symbol in
162 the table) into physical pointers to elements in the new
163 internal table. This involves some work since the meanings of
164 fields change depending upon context: a field that is a
165 pointer to another structure in the symbol table at one moment
166 may be the size in bytes of a structure at the next. Another
167 pass is made over the table. All symbols which mark file names
168 (<<C_FILE>> symbols) are modified so that the internal
169 string points to the value in the auxent (the real filename)
170 rather than the normal text associated with the symbol
171 (@code{".file"}).
172
173 At this time the symbol names are moved around. Coff stores
174 all symbols less than nine characters long physically
175 within the symbol table; longer strings are kept at the end of
176 the file in the string table. This pass moves all strings
177 into memory and replaces them with pointers to the strings.
178
179 The symbol table is massaged once again, this time to create
180 the canonical table used by the BFD application. Each symbol
181 is inspected in turn, and a decision made (using the
182 @code{sclass} field) about the various flags to set in the
183 @code{asymbol}. @xref{Symbols}. The generated canonical table
184 shares strings with the hidden internal symbol table.
185
186 Any linenumbers are read from the coff file too, and attached
187 to the symbols which own the functions the linenumbers belong to.
188
189SUBSUBSECTION
190 Symbol writing
191
192 Writing a symbol to a coff file which didn't come from a coff
193 file will lose any debugging information. The @code{asymbol}
194 structure remembers the BFD from which the symbol was taken, and on
195 output the back end makes sure that the same destination target as
196 source target is present.
197
198 When the symbols have come from a coff file then all the
199 debugging information is preserved.
200
201 Symbol tables are provided for writing to the back end in a
202 vector of pointers to pointers. This allows applications like
203 the linker to accumulate and output large symbol tables
204 without having to do too much byte copying.
205
206 This function runs through the provided symbol table and
207 patches each symbol marked as a file place holder
208 (@code{C_FILE}) to point to the next file place holder in the
209 list. It also marks each @code{offset} field in the list with
210 the offset from the first symbol of the current symbol.
211
212 Another function of this procedure is to turn the canonical
213 value form of BFD into the form used by coff. Internally, BFD
214 expects symbol values to be offsets from a section base; so a
215 symbol physically at 0x120, but in a section starting at
216 0x100, would have the value 0x20. Coff expects symbols to
217 contain their final value, so symbols have their values
218 changed at this point to reflect their sum with their owning
219 section. This transformation uses the
220 <<output_section>> field of the @code{asymbol}'s
221 @code{asection} @xref{Sections}.
222
223 o <<coff_mangle_symbols>>
224
225 This routine runs though the provided symbol table and uses
226 the offsets generated by the previous pass and the pointers
227 generated when the symbol table was read in to create the
228 structured hierarchy required by coff. It changes each pointer
229 to a symbol into the index into the symbol table of the asymbol.
230
231 o <<coff_write_symbols>>
232
233 This routine runs through the symbol table and patches up the
234 symbols from their internal form into the coff way, calls the
235 bit twiddlers, and writes out the table to the file.
236
237*/
238
239/*
240INTERNAL_DEFINITION
241 coff_symbol_type
242
243DESCRIPTION
244 The hidden information for an <<asymbol>> is described in a
245 <<combined_entry_type>>:
246
247CODE_FRAGMENT
248.
249.typedef struct coff_ptr_struct
250.{
251. {* Remembers the offset from the first symbol in the file for
252. this symbol. Generated by coff_renumber_symbols. *}
253. unsigned int offset;
254.
255. {* Should the value of this symbol be renumbered. Used for
256. XCOFF C_BSTAT symbols. Set by coff_slurp_symbol_table. *}
257. unsigned int fix_value : 1;
258.
259. {* Should the tag field of this symbol be renumbered.
260. Created by coff_pointerize_aux. *}
261. unsigned int fix_tag : 1;
262.
263. {* Should the endidx field of this symbol be renumbered.
264. Created by coff_pointerize_aux. *}
265. unsigned int fix_end : 1;
266.
267. {* Should the x_csect.x_scnlen field be renumbered.
268. Created by coff_pointerize_aux. *}
269. unsigned int fix_scnlen : 1;
270.
271. {* Fix up an XCOFF C_BINCL/C_EINCL symbol. The value is the
272. index into the line number entries. Set by coff_slurp_symbol_table. *}
273. unsigned int fix_line : 1;
274.
275. {* The container for the symbol structure as read and translated
276. from the file. *}
277. union
278. {
279. union internal_auxent auxent;
280. struct internal_syment syment;
281. } u;
282.} combined_entry_type;
283.
284.
285.{* Each canonical asymbol really looks like this: *}
286.
287.typedef struct coff_symbol_struct
288.{
289. {* The actual symbol which the rest of BFD works with *}
290. asymbol symbol;
291.
292. {* A pointer to the hidden information for this symbol *}
293. combined_entry_type *native;
294.
295. {* A pointer to the linenumber information for this symbol *}
296. struct lineno_cache_entry *lineno;
297.
298. {* Have the line numbers been relocated yet ? *}
299. bfd_boolean done_lineno;
300.} coff_symbol_type;
301
302*/
303
304#ifdef COFF_WITH_PE
305#include "peicode.h"
306#else
307#include "coffswap.h"
308#endif
309
310#define STRING_SIZE_SIZE (4)
311
312static long sec_to_styp_flags
313 PARAMS ((const char *, flagword));
314static bfd_boolean styp_to_sec_flags
315 PARAMS ((bfd *, PTR, const char *, asection *, flagword *));
316static bfd_boolean coff_bad_format_hook
317 PARAMS ((bfd *, PTR));
318static void coff_set_custom_section_alignment
319 PARAMS ((bfd *, asection *, const struct coff_section_alignment_entry *,
320 const unsigned int));
321static bfd_boolean coff_new_section_hook
322 PARAMS ((bfd *, asection *));
323static bfd_boolean coff_set_arch_mach_hook
324 PARAMS ((bfd *, PTR));
325static bfd_boolean coff_write_relocs
326 PARAMS ((bfd *, int));
327static bfd_boolean coff_set_flags
328 PARAMS ((bfd *, unsigned int *, unsigned short *));
329static bfd_boolean coff_set_arch_mach
330 PARAMS ((bfd *, enum bfd_architecture, unsigned long)) ATTRIBUTE_UNUSED;
331static bfd_boolean coff_compute_section_file_positions
332 PARAMS ((bfd *));
333static bfd_boolean coff_write_object_contents
334 PARAMS ((bfd *)) ATTRIBUTE_UNUSED;
335static bfd_boolean coff_set_section_contents
336 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
337static PTR buy_and_read
338 PARAMS ((bfd *, file_ptr, bfd_size_type));
339static bfd_boolean coff_slurp_line_table
340 PARAMS ((bfd *, asection *));
341static bfd_boolean coff_slurp_symbol_table
342 PARAMS ((bfd *));
343static enum coff_symbol_classification coff_classify_symbol
344 PARAMS ((bfd *, struct internal_syment *));
345static bfd_boolean coff_slurp_reloc_table
346 PARAMS ((bfd *, asection *, asymbol **));
347static long coff_canonicalize_reloc
348 PARAMS ((bfd *, asection *, arelent **, asymbol **));
349#ifndef coff_mkobject_hook
350static PTR coff_mkobject_hook
351 PARAMS ((bfd *, PTR, PTR));
352#endif
353#ifdef COFF_WITH_PE
354static flagword handle_COMDAT
355 PARAMS ((bfd *, flagword, PTR, const char *, asection *));
356#endif
357#ifdef COFF_IMAGE_WITH_PE
358static bfd_boolean coff_read_word
359 PARAMS ((bfd *, unsigned int *));
360static unsigned int coff_compute_checksum
361 PARAMS ((bfd *));
362static bfd_boolean coff_apply_checksum
363 PARAMS ((bfd *));
364#endif
365
366
367/* void warning(); */
368
369/* Return a word with STYP_* (scnhdr.s_flags) flags set to represent
370 the incoming SEC_* flags. The inverse of this function is
371 styp_to_sec_flags(). NOTE: If you add to/change this routine, you
372 should probably mirror the changes in styp_to_sec_flags(). */
373
374#ifndef COFF_WITH_PE
375
376/* Macros for setting debugging flags. */
377#ifdef STYP_DEBUG
378#define STYP_XCOFF_DEBUG STYP_DEBUG
379#else
380#define STYP_XCOFF_DEBUG STYP_INFO
381#endif
382
383#ifdef COFF_ALIGN_IN_S_FLAGS
384#define STYP_DEBUG_INFO STYP_DSECT
385#else
386#define STYP_DEBUG_INFO STYP_INFO
387#endif
388
389static long
390sec_to_styp_flags (sec_name, sec_flags)
391 const char *sec_name;
392 flagword sec_flags;
393{
394 long styp_flags = 0;
395
396 if (!strcmp (sec_name, _TEXT))
397 {
398 styp_flags = STYP_TEXT;
399 }
400 else if (!strcmp (sec_name, _DATA))
401 {
402 styp_flags = STYP_DATA;
403 }
404 else if (!strcmp (sec_name, _BSS))
405 {
406 styp_flags = STYP_BSS;
407#ifdef _COMMENT
408 }
409 else if (!strcmp (sec_name, _COMMENT))
410 {
411 styp_flags = STYP_INFO;
412#endif /* _COMMENT */
413#ifdef _LIB
414 }
415 else if (!strcmp (sec_name, _LIB))
416 {
417 styp_flags = STYP_LIB;
418#endif /* _LIB */
419#ifdef _LIT
420 }
421 else if (!strcmp (sec_name, _LIT))
422 {
423 styp_flags = STYP_LIT;
424#endif /* _LIT */
425 }
426 else if (!strncmp (sec_name, ".debug", 6))
427 {
428 /* Handle the XCOFF debug section and DWARF2 debug sections. */
429 if (!sec_name[6])
430 styp_flags = STYP_XCOFF_DEBUG;
431 else
432 styp_flags = STYP_DEBUG_INFO;
433 }
434 else if (!strncmp (sec_name, ".stab", 5))
435 {
436 styp_flags = STYP_DEBUG_INFO;
437 }
438#ifdef COFF_LONG_SECTION_NAMES
439 else if (!strncmp (sec_name, ".gnu.linkonce.wi.", 17))
440 {
441 styp_flags = STYP_DEBUG_INFO;
442 }
443#endif
444#ifdef RS6000COFF_C
445 else if (!strcmp (sec_name, _PAD))
446 {
447 styp_flags = STYP_PAD;
448 }
449 else if (!strcmp (sec_name, _LOADER))
450 {
451 styp_flags = STYP_LOADER;
452 }
453 else if (!strcmp (sec_name, _EXCEPT))
454 {
455 styp_flags = STYP_EXCEPT;
456 }
457 else if (!strcmp (sec_name, _TYPCHK))
458 {
459 styp_flags = STYP_TYPCHK;
460 }
461#endif
462 /* Try and figure out what it should be */
463 else if (sec_flags & SEC_CODE)
464 {
465 styp_flags = STYP_TEXT;
466 }
467 else if (sec_flags & SEC_DATA)
468 {
469 styp_flags = STYP_DATA;
470 }
471 else if (sec_flags & SEC_READONLY)
472 {
473#ifdef STYP_LIT /* 29k readonly text/data section */
474 styp_flags = STYP_LIT;
475#else
476 styp_flags = STYP_TEXT;
477#endif /* STYP_LIT */
478 }
479 else if (sec_flags & SEC_LOAD)
480 {
481 styp_flags = STYP_TEXT;
482 }
483 else if (sec_flags & SEC_ALLOC)
484 {
485 styp_flags = STYP_BSS;
486 }
487
488#ifdef STYP_CLINK
489 if (sec_flags & SEC_CLINK)
490 styp_flags |= STYP_CLINK;
491#endif
492
493#ifdef STYP_BLOCK
494 if (sec_flags & SEC_BLOCK)
495 styp_flags |= STYP_BLOCK;
496#endif
497
498#ifdef STYP_NOLOAD
499 if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
500 styp_flags |= STYP_NOLOAD;
501#endif
502
503 return styp_flags;
504}
505
506#else /* COFF_WITH_PE */
507
508/* The PE version; see above for the general comments. The non-PE
509 case seems to be more guessing, and breaks PE format; specifically,
510 .rdata is readonly, but it sure ain't text. Really, all this
511 should be set up properly in gas (or whatever assembler is in use),
512 and honor whatever objcopy/strip, etc. sent us as input. */
513
514static long
515sec_to_styp_flags (sec_name, sec_flags)
516 const char *sec_name ATTRIBUTE_UNUSED;
517 flagword sec_flags;
518{
519 long styp_flags = 0;
520
521 /* caution: there are at least three groups of symbols that have
522 very similar bits and meanings: IMAGE_SCN*, SEC_*, and STYP_*.
523 SEC_* are the BFD internal flags, used for generic BFD
524 information. STYP_* are the COFF section flags which appear in
525 COFF files. IMAGE_SCN_* are the PE section flags which appear in
526 PE files. The STYP_* flags and the IMAGE_SCN_* flags overlap,
527 but there are more IMAGE_SCN_* flags. */
528
529 /* skip LOAD */
530 /* READONLY later */
531 /* skip RELOC */
532 if ((sec_flags & SEC_CODE) != 0)
533 styp_flags |= IMAGE_SCN_CNT_CODE;
534 if ((sec_flags & SEC_DATA) != 0)
535 styp_flags |= IMAGE_SCN_CNT_INITIALIZED_DATA;
536 if ((sec_flags & SEC_ALLOC) != 0 && (sec_flags & SEC_LOAD) == 0)
537 styp_flags |= IMAGE_SCN_CNT_UNINITIALIZED_DATA; /* ==STYP_BSS */
538 /* skip ROM */
539 /* skip constRUCTOR */
540 /* skip CONTENTS */
541#ifdef STYP_NOLOAD
542 if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
543 styp_flags |= STYP_NOLOAD;
544#endif
545 if ((sec_flags & SEC_IS_COMMON) != 0)
546 styp_flags |= IMAGE_SCN_LNK_COMDAT;
547 if ((sec_flags & SEC_DEBUGGING) != 0)
548 styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
549 if ((sec_flags & SEC_EXCLUDE) != 0)
550 styp_flags |= IMAGE_SCN_LNK_REMOVE;
551 if ((sec_flags & SEC_NEVER_LOAD) != 0)
552 styp_flags |= IMAGE_SCN_LNK_REMOVE;
553 /* skip IN_MEMORY */
554 /* skip SORT */
555 if (sec_flags & SEC_LINK_ONCE)
556 styp_flags |= IMAGE_SCN_LNK_COMDAT;
557 /* skip LINK_DUPLICATES */
558 /* skip LINKER_CREATED */
559
560 /* For now, the read/write bits are mapped onto SEC_READONLY, even
561 though the semantics don't quite match. The bits from the input
562 are retained in pei_section_data(abfd, section)->pe_flags. */
563
564 styp_flags |= IMAGE_SCN_MEM_READ; /* Always readable. */
565 if ((sec_flags & SEC_READONLY) == 0)
566 styp_flags |= IMAGE_SCN_MEM_WRITE; /* Invert READONLY for write. */
567 if (sec_flags & SEC_CODE)
568 styp_flags |= IMAGE_SCN_MEM_EXECUTE; /* CODE->EXECUTE. */
569 if (sec_flags & SEC_SHARED)
570 styp_flags |= IMAGE_SCN_MEM_SHARED; /* Shared remains meaningful. */
571
572 return styp_flags;
573}
574
575#endif /* COFF_WITH_PE */
576
577/* Return a word with SEC_* flags set to represent the incoming STYP_*
578 flags (from scnhdr.s_flags). The inverse of this function is
579 sec_to_styp_flags(). NOTE: If you add to/change this routine, you
580 should probably mirror the changes in sec_to_styp_flags(). */
581
582#ifndef COFF_WITH_PE
583
584static bfd_boolean
585styp_to_sec_flags (abfd, hdr, name, section, flags_ptr)
586 bfd *abfd ATTRIBUTE_UNUSED;
587 PTR hdr;
588 const char *name;
589 asection *section ATTRIBUTE_UNUSED;
590 flagword *flags_ptr;
591{
592 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
593 long styp_flags = internal_s->s_flags;
594 flagword sec_flags = 0;
595
596#ifdef STYP_BLOCK
597 if (styp_flags & STYP_BLOCK)
598 sec_flags |= SEC_BLOCK;
599#endif
600
601#ifdef STYP_CLINK
602 if (styp_flags & STYP_CLINK)
603 sec_flags |= SEC_CLINK;
604#endif
605
606#ifdef STYP_NOLOAD
607 if (styp_flags & STYP_NOLOAD)
608 sec_flags |= SEC_NEVER_LOAD;
609#endif /* STYP_NOLOAD */
610
611 /* For 386 COFF, at least, an unloadable text or data section is
612 actually a shared library section. */
613 if (styp_flags & STYP_TEXT)
614 {
615 if (sec_flags & SEC_NEVER_LOAD)
616 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
617 else
618 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
619 }
620 else if (styp_flags & STYP_DATA)
621 {
622 if (sec_flags & SEC_NEVER_LOAD)
623 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
624 else
625 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
626 }
627 else if (styp_flags & STYP_BSS)
628 {
629#ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
630 if (sec_flags & SEC_NEVER_LOAD)
631 sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
632 else
633#endif
634 sec_flags |= SEC_ALLOC;
635 }
636 else if (styp_flags & STYP_INFO)
637 {
638 /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
639 defined. coff_compute_section_file_positions uses
640 COFF_PAGE_SIZE to ensure that the low order bits of the
641 section VMA and the file offset match. If we don't know
642 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
643 and demand page loading of the file will fail. */
644#if defined (COFF_PAGE_SIZE) && !defined (COFF_ALIGN_IN_S_FLAGS)
645 sec_flags |= SEC_DEBUGGING;
646#endif
647 }
648 else if (styp_flags & STYP_PAD)
649 sec_flags = 0;
650 else if (strcmp (name, _TEXT) == 0)
651 {
652 if (sec_flags & SEC_NEVER_LOAD)
653 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
654 else
655 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
656 }
657 else if (strcmp (name, _DATA) == 0)
658 {
659 if (sec_flags & SEC_NEVER_LOAD)
660 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
661 else
662 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
663 }
664 else if (strcmp (name, _BSS) == 0)
665 {
666#ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
667 if (sec_flags & SEC_NEVER_LOAD)
668 sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
669 else
670#endif
671 sec_flags |= SEC_ALLOC;
672 }
673 else if (strncmp (name, ".debug", 6) == 0
674#ifdef _COMMENT
675 || strcmp (name, _COMMENT) == 0
676#endif
677#ifdef COFF_LONG_SECTION_NAMES
678 || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
679#endif
680 || strncmp (name, ".stab", 5) == 0)
681 {
682#ifdef COFF_PAGE_SIZE
683 sec_flags |= SEC_DEBUGGING;
684#endif
685 }
686#ifdef _LIB
687 else if (strcmp (name, _LIB) == 0)
688 ;
689#endif
690#ifdef _LIT
691 else if (strcmp (name, _LIT) == 0)
692 sec_flags = SEC_LOAD | SEC_ALLOC | SEC_READONLY;
693#endif
694 else
695 sec_flags |= SEC_ALLOC | SEC_LOAD;
696
697#ifdef STYP_LIT /* A29k readonly text/data section type. */
698 if ((styp_flags & STYP_LIT) == STYP_LIT)
699 sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
700#endif /* STYP_LIT */
701
702#ifdef STYP_OTHER_LOAD /* Other loaded sections. */
703 if (styp_flags & STYP_OTHER_LOAD)
704 sec_flags = (SEC_LOAD | SEC_ALLOC);
705#endif /* STYP_SDATA */
706
707#if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
708 /* As a GNU extension, if the name begins with .gnu.linkonce, we
709 only link a single copy of the section. This is used to support
710 g++. g++ will emit each template expansion in its own section.
711 The symbols will be defined as weak, so that multiple definitions
712 are permitted. The GNU linker extension is to actually discard
713 all but one of the sections. */
714 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
715 sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
716#endif
717
718 if (flags_ptr == NULL)
719 return FALSE;
720
721 * flags_ptr = sec_flags;
722 return TRUE;
723}
724
725#else /* COFF_WITH_PE */
726
727static flagword
728handle_COMDAT (abfd, sec_flags, hdr, name, section)
729 bfd * abfd;
730 flagword sec_flags;
731 PTR hdr;
732 const char *name;
733 asection *section;
734{
735 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
736 bfd_byte *esymstart, *esym, *esymend;
737 int seen_state = 0;
738 char *target_name = NULL;
739
740 sec_flags |= SEC_LINK_ONCE;
741
742 /* Unfortunately, the PE format stores essential information in
743 the symbol table, of all places. We need to extract that
744 information now, so that objdump and the linker will know how
745 to handle the section without worrying about the symbols. We
746 can't call slurp_symtab, because the linker doesn't want the
747 swapped symbols. */
748
749 /* COMDAT sections are special. The first symbol is the section
750 symbol, which tells what kind of COMDAT section it is. The
751 second symbol is the "comdat symbol" - the one with the
752 unique name. GNU uses the section symbol for the unique
753 name; MS uses ".text" for every comdat section. Sigh. - DJ */
754
755 /* This is not mirrored in sec_to_styp_flags(), but there
756 doesn't seem to be a need to, either, and it would at best be
757 rather messy. */
758
759 if (! _bfd_coff_get_external_symbols (abfd))
760 return sec_flags;
761
762 esymstart = esym = (bfd_byte *) obj_coff_external_syms (abfd);
763 esymend = esym + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
764
765 while (esym < esymend)
766 {
767 struct internal_syment isym;
768 char buf[SYMNMLEN + 1];
769 const char *symname;
770
771 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &isym);
772
773 if (sizeof (internal_s->s_name) > SYMNMLEN)
774 {
775 /* This case implies that the matching
776 symbol name will be in the string table. */
777 abort ();
778 }
779
780 if (isym.n_scnum == section->target_index)
781 {
782 /* According to the MSVC documentation, the first
783 TWO entries with the section # are both of
784 interest to us. The first one is the "section
785 symbol" (section name). The second is the comdat
786 symbol name. Here, we've found the first
787 qualifying entry; we distinguish it from the
788 second with a state flag.
789
790 In the case of gas-generated (at least until that
791 is fixed) .o files, it isn't necessarily the
792 second one. It may be some other later symbol.
793
794 Since gas also doesn't follow MS conventions and
795 emits the section similar to .text$<name>, where
796 <something> is the name we're looking for, we
797 distinguish the two as follows:
798
799 If the section name is simply a section name (no
800 $) we presume it's MS-generated, and look at
801 precisely the second symbol for the comdat name.
802 If the section name has a $, we assume it's
803 gas-generated, and look for <something> (whatever
804 follows the $) as the comdat symbol. */
805
806 /* All 3 branches use this. */
807 symname = _bfd_coff_internal_syment_name (abfd, &isym, buf);
808
809 if (symname == NULL)
810 abort ();
811
812 switch (seen_state)
813 {
814 case 0:
815 {
816 /* The first time we've seen the symbol. */
817 union internal_auxent aux;
818
819 seen_state = 1;
820
821 /* If it isn't the stuff we're expecting, die;
822 The MS documentation is vague, but it
823 appears that the second entry serves BOTH
824 as the comdat symbol and the defining
825 symbol record (either C_STAT or C_EXT,
826 possibly with an aux entry with debug
827 information if it's a function.) It
828 appears the only way to find the second one
829 is to count. (On Intel, they appear to be
830 adjacent, but on Alpha, they have been
831 found separated.)
832
833 Here, we think we've found the first one,
834 but there's some checking we can do to be
835 sure. */
836
837 if (! (isym.n_sclass == C_STAT
838 && isym.n_type == T_NULL
839 && isym.n_value == 0))
840 abort ();
841
842 /* FIXME LATER: MSVC generates section names
843 like .text for comdats. Gas generates
844 names like .text$foo__Fv (in the case of a
845 function). See comment above for more. */
846
847 if (strcmp (name, symname) != 0)
848 abort ();
849
850 /* This is the section symbol. */
851 bfd_coff_swap_aux_in (abfd, (PTR) (esym + bfd_coff_symesz (abfd)),
852 isym.n_type, isym.n_sclass,
853 0, isym.n_numaux, (PTR) &aux);
854
855 target_name = strchr (name, '$');
856 if (target_name != NULL)
857 {
858 /* Gas mode. */
859 seen_state = 2;
860 /* Skip the `$'. */
861 target_name += 1;
862 }
863
864 /* FIXME: Microsoft uses NODUPLICATES and
865 ASSOCIATIVE, but gnu uses ANY and
866 SAME_SIZE. Unfortunately, gnu doesn't do
867 the comdat symbols right. So, until we can
868 fix it to do the right thing, we are
869 temporarily disabling comdats for the MS
870 types (they're used in DLLs and C++, but we
871 don't support *their* C++ libraries anyway
872 - DJ. */
873
874 /* Cygwin does not follow the MS style, and
875 uses ANY and SAME_SIZE where NODUPLICATES
876 and ASSOCIATIVE should be used. For
877 Interix, we just do the right thing up
878 front. */
879
880 switch (aux.x_scn.x_comdat)
881 {
882 case IMAGE_COMDAT_SELECT_NODUPLICATES:
883#ifdef STRICT_PE_FORMAT
884 sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
885#else
886 sec_flags &= ~SEC_LINK_ONCE;
887#endif
888 break;
889
890 case IMAGE_COMDAT_SELECT_ANY:
891 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
892 break;
893
894 case IMAGE_COMDAT_SELECT_SAME_SIZE:
895 sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
896 break;
897
898 case IMAGE_COMDAT_SELECT_EXACT_MATCH:
899 /* Not yet fully implemented ??? */
900 sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
901 break;
902
903 /* debug$S gets this case; other
904 implications ??? */
905
906 /* There may be no symbol... we'll search
907 the whole table... Is this the right
908 place to play this game? Or should we do
909 it when reading it in. */
910 case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
911#ifdef STRICT_PE_FORMAT
912 /* FIXME: This is not currently implemented. */
913 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
914#else
915 sec_flags &= ~SEC_LINK_ONCE;
916#endif
917 break;
918
919 default: /* 0 means "no symbol" */
920 /* debug$F gets this case; other
921 implications ??? */
922 sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
923 break;
924 }
925 }
926 break;
927
928 case 2:
929 /* Gas mode: the first matching on partial name. */
930
931#ifndef TARGET_UNDERSCORE
932#define TARGET_UNDERSCORE 0
933#endif
934 /* Is this the name we're looking for ? */
935 if (strcmp (target_name,
936 symname + (TARGET_UNDERSCORE ? 1 : 0)) != 0)
937 {
938 /* Not the name we're looking for */
939 esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
940 continue;
941 }
942 /* Fall through. */
943 case 1:
944 /* MSVC mode: the lexically second symbol (or
945 drop through from the above). */
946 {
947 char *newname;
948 bfd_size_type amt;
949
950 /* This must the second symbol with the
951 section #. It is the actual symbol name.
952 Intel puts the two adjacent, but Alpha (at
953 least) spreads them out. */
954
955 amt = sizeof (struct bfd_comdat_info);
956 section->comdat = bfd_alloc (abfd, amt);
957 if (section->comdat == NULL)
958 abort ();
959
960 section->comdat->symbol =
961 (esym - esymstart) / bfd_coff_symesz (abfd);
962
963 amt = strlen (symname) + 1;
964 newname = bfd_alloc (abfd, amt);
965 if (newname == NULL)
966 abort ();
967
968 strcpy (newname, symname);
969 section->comdat->name = newname;
970 }
971
972 goto breakloop;
973 }
974 }
975
976 esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
977 }
978
979 breakloop:
980 return sec_flags;
981}
982
983
984/* The PE version; see above for the general comments.
985
986 Since to set the SEC_LINK_ONCE and associated flags, we have to
987 look at the symbol table anyway, we return the symbol table index
988 of the symbol being used as the COMDAT symbol. This is admittedly
989 ugly, but there's really nowhere else that we have access to the
990 required information. FIXME: Is the COMDAT symbol index used for
991 any purpose other than objdump? */
992
993static bfd_boolean
994styp_to_sec_flags (abfd, hdr, name, section, flags_ptr)
995 bfd *abfd;
996 PTR hdr;
997 const char *name;
998 asection *section;
999 flagword *flags_ptr;
1000{
1001 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
1002 long styp_flags = internal_s->s_flags;
1003 flagword sec_flags;
1004 bfd_boolean result = TRUE;
1005
1006 /* Assume read only unless IMAGE_SCN_MEM_WRITE is specified. */
1007 sec_flags = SEC_READONLY;
1008
1009 /* Process each flag bit in styp_flags in turn. */
1010 while (styp_flags)
1011 {
1012 long flag = styp_flags & - styp_flags;
1013 char * unhandled = NULL;
1014
1015 styp_flags &= ~ flag;
1016
1017 /* We infer from the distinct read/write/execute bits the settings
1018 of some of the bfd flags; the actual values, should we need them,
1019 are also in pei_section_data (abfd, section)->pe_flags. */
1020
1021 switch (flag)
1022 {
1023 case STYP_DSECT:
1024 unhandled = "STYP_DSECT";
1025 break;
1026 case STYP_GROUP:
1027 unhandled = "STYP_GROUP";
1028 break;
1029 case STYP_COPY:
1030 unhandled = "STYP_COPY";
1031 break;
1032 case STYP_OVER:
1033 unhandled = "STYP_OVER";
1034 break;
1035#ifdef SEC_NEVER_LOAD
1036 case STYP_NOLOAD:
1037 sec_flags |= SEC_NEVER_LOAD;
1038 break;
1039#endif
1040 case IMAGE_SCN_MEM_READ:
1041 /* Ignored, assume it always to be true. */
1042 break;
1043 case IMAGE_SCN_TYPE_NO_PAD:
1044 /* Skip. */
1045 break;
1046 case IMAGE_SCN_LNK_OTHER:
1047 unhandled = "IMAGE_SCN_LNK_OTHER";
1048 break;
1049 case IMAGE_SCN_MEM_NOT_CACHED:
1050 unhandled = "IMAGE_SCN_MEM_NOT_CACHED";
1051 break;
1052 case IMAGE_SCN_MEM_NOT_PAGED:
1053 unhandled = "IMAGE_SCN_MEM_NOT_PAGED";
1054 break;
1055 case IMAGE_SCN_MEM_EXECUTE:
1056 sec_flags |= SEC_CODE;
1057 break;
1058 case IMAGE_SCN_MEM_WRITE:
1059 sec_flags &= ~ SEC_READONLY;
1060 break;
1061 case IMAGE_SCN_MEM_DISCARDABLE:
1062 sec_flags |= SEC_DEBUGGING;
1063 break;
1064 case IMAGE_SCN_MEM_SHARED:
1065 sec_flags |= SEC_SHARED;
1066 break;
1067 case IMAGE_SCN_LNK_REMOVE:
1068 sec_flags |= SEC_EXCLUDE;
1069 break;
1070 case IMAGE_SCN_CNT_CODE:
1071 sec_flags |= SEC_CODE | SEC_ALLOC | SEC_LOAD;
1072 break;
1073 case IMAGE_SCN_CNT_INITIALIZED_DATA:
1074 sec_flags |= SEC_DATA | SEC_ALLOC | SEC_LOAD;
1075 break;
1076 case IMAGE_SCN_CNT_UNINITIALIZED_DATA:
1077 sec_flags |= SEC_ALLOC;
1078 break;
1079 case IMAGE_SCN_LNK_INFO:
1080 /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
1081 defined. coff_compute_section_file_positions uses
1082 COFF_PAGE_SIZE to ensure that the low order bits of the
1083 section VMA and the file offset match. If we don't know
1084 COFF_PAGE_SIZE, we can't ensure the correct correspondence,
1085 and demand page loading of the file will fail. */
1086#ifdef COFF_PAGE_SIZE
1087 sec_flags |= SEC_DEBUGGING;
1088#endif
1089 break;
1090 case IMAGE_SCN_LNK_COMDAT:
1091 /* COMDAT gets very special treatment. */
1092 sec_flags = handle_COMDAT (abfd, sec_flags, hdr, name, section);
1093 break;
1094 default:
1095 /* Silently ignore for now. */
1096 break;
1097 }
1098
1099 /* If the section flag was not handled, report it here. */
1100 if (unhandled != NULL)
1101 {
1102 (*_bfd_error_handler)
1103 (_("%s (%s): Section flag %s (0x%x) ignored"),
1104 bfd_archive_filename (abfd), name, unhandled, flag);
1105 result = FALSE;
1106 }
1107 }
1108
1109#if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
1116 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
1117 sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1118#endif
1119
1120 if (flags_ptr)
1121 * flags_ptr = sec_flags;
1122
1123 return result;
1124}
1125
1126#endif /* COFF_WITH_PE */
1127
1128#define get_index(symbol) ((symbol)->udata.i)
1129
1130/*
1131INTERNAL_DEFINITION
1132 bfd_coff_backend_data
1133
1134CODE_FRAGMENT
1135
1136.{* COFF symbol classifications. *}
1137.
1138.enum coff_symbol_classification
1139.{
1140. {* Global symbol. *}
1141. COFF_SYMBOL_GLOBAL,
1142. {* Common symbol. *}
1143. COFF_SYMBOL_COMMON,
1144. {* Undefined symbol. *}
1145. COFF_SYMBOL_UNDEFINED,
1146. {* Local symbol. *}
1147. COFF_SYMBOL_LOCAL,
1148. {* PE section symbol. *}
1149. COFF_SYMBOL_PE_SECTION
1150.};
1151.
1152Special entry points for gdb to swap in coff symbol table parts:
1153.typedef struct
1154.{
1155. void (*_bfd_coff_swap_aux_in)
1156. PARAMS ((bfd *, PTR, int, int, int, int, PTR));
1157.
1158. void (*_bfd_coff_swap_sym_in)
1159. PARAMS ((bfd *, PTR, PTR));
1160.
1161. void (*_bfd_coff_swap_lineno_in)
1162. PARAMS ((bfd *, PTR, PTR));
1163.
1164. unsigned int (*_bfd_coff_swap_aux_out)
1165. PARAMS ((bfd *, PTR, int, int, int, int, PTR));
1166.
1167. unsigned int (*_bfd_coff_swap_sym_out)
1168. PARAMS ((bfd *, PTR, PTR));
1169.
1170. unsigned int (*_bfd_coff_swap_lineno_out)
1171. PARAMS ((bfd *, PTR, PTR));
1172.
1173. unsigned int (*_bfd_coff_swap_reloc_out)
1174. PARAMS ((bfd *, PTR, PTR));
1175.
1176. unsigned int (*_bfd_coff_swap_filehdr_out)
1177. PARAMS ((bfd *, PTR, PTR));
1178.
1179. unsigned int (*_bfd_coff_swap_aouthdr_out)
1180. PARAMS ((bfd *, PTR, PTR));
1181.
1182. unsigned int (*_bfd_coff_swap_scnhdr_out)
1183. PARAMS ((bfd *, PTR, PTR));
1184.
1185. unsigned int _bfd_filhsz;
1186. unsigned int _bfd_aoutsz;
1187. unsigned int _bfd_scnhsz;
1188. unsigned int _bfd_symesz;
1189. unsigned int _bfd_auxesz;
1190. unsigned int _bfd_relsz;
1191. unsigned int _bfd_linesz;
1192. unsigned int _bfd_filnmlen;
1193. bfd_boolean _bfd_coff_long_filenames;
1194. bfd_boolean _bfd_coff_long_section_names;
1195. unsigned int _bfd_coff_default_section_alignment_power;
1196. bfd_boolean _bfd_coff_force_symnames_in_strings;
1197. unsigned int _bfd_coff_debug_string_prefix_length;
1198.
1199. void (*_bfd_coff_swap_filehdr_in)
1200. PARAMS ((bfd *, PTR, PTR));
1201.
1202. void (*_bfd_coff_swap_aouthdr_in)
1203. PARAMS ((bfd *, PTR, PTR));
1204.
1205. void (*_bfd_coff_swap_scnhdr_in)
1206. PARAMS ((bfd *, PTR, PTR));
1207.
1208. void (*_bfd_coff_swap_reloc_in)
1209. PARAMS ((bfd *abfd, PTR, PTR));
1210.
1211. bfd_boolean (*_bfd_coff_bad_format_hook)
1212. PARAMS ((bfd *, PTR));
1213.
1214. bfd_boolean (*_bfd_coff_set_arch_mach_hook)
1215. PARAMS ((bfd *, PTR));
1216.
1217. PTR (*_bfd_coff_mkobject_hook)
1218. PARAMS ((bfd *, PTR, PTR));
1219.
1220. bfd_boolean (*_bfd_styp_to_sec_flags_hook)
1221. PARAMS ((bfd *, PTR, const char *, asection *, flagword *));
1222.
1223. void (*_bfd_set_alignment_hook)
1224. PARAMS ((bfd *, asection *, PTR));
1225.
1226. bfd_boolean (*_bfd_coff_slurp_symbol_table)
1227. PARAMS ((bfd *));
1228.
1229. bfd_boolean (*_bfd_coff_symname_in_debug)
1230. PARAMS ((bfd *, struct internal_syment *));
1231.
1232. bfd_boolean (*_bfd_coff_pointerize_aux_hook)
1233. PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
1234. unsigned int, combined_entry_type *));
1235.
1236. bfd_boolean (*_bfd_coff_print_aux)
1237. PARAMS ((bfd *, FILE *, combined_entry_type *, combined_entry_type *,
1238. combined_entry_type *, unsigned int));
1239.
1240. void (*_bfd_coff_reloc16_extra_cases)
1241. PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
1242. bfd_byte *, unsigned int *, unsigned int *));
1243.
1244. int (*_bfd_coff_reloc16_estimate)
1245. PARAMS ((bfd *, asection *, arelent *, unsigned int,
1246. struct bfd_link_info *));
1247.
1248. enum coff_symbol_classification (*_bfd_coff_classify_symbol)
1249. PARAMS ((bfd *, struct internal_syment *));
1250.
1251. bfd_boolean (*_bfd_coff_compute_section_file_positions)
1252. PARAMS ((bfd *));
1253.
1254. bfd_boolean (*_bfd_coff_start_final_link)
1255. PARAMS ((bfd *, struct bfd_link_info *));
1256.
1257. bfd_boolean (*_bfd_coff_relocate_section)
1258. PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
1259. struct internal_reloc *, struct internal_syment *, asection **));
1260.
1261. reloc_howto_type *(*_bfd_coff_rtype_to_howto)
1262. PARAMS ((bfd *, asection *, struct internal_reloc *,
1263. struct coff_link_hash_entry *, struct internal_syment *,
1264. bfd_vma *));
1265.
1266. bfd_boolean (*_bfd_coff_adjust_symndx)
1267. PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
1268. struct internal_reloc *, bfd_boolean *));
1269.
1270. bfd_boolean (*_bfd_coff_link_add_one_symbol)
1271. PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword,
1272. asection *, bfd_vma, const char *, bfd_boolean, bfd_boolean,
1273. struct bfd_link_hash_entry **));
1274.
1275. bfd_boolean (*_bfd_coff_link_output_has_begun)
1276. PARAMS ((bfd *, struct coff_final_link_info *));
1277.
1278. bfd_boolean (*_bfd_coff_final_link_postscript)
1279. PARAMS ((bfd *, struct coff_final_link_info *));
1280.
1281.} bfd_coff_backend_data;
1282.
1283.#define coff_backend_info(abfd) \
1284. ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
1285.
1286.#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
1287. ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
1288.
1289.#define bfd_coff_swap_sym_in(a,e,i) \
1290. ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
1291.
1292.#define bfd_coff_swap_lineno_in(a,e,i) \
1293. ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
1294.
1295.#define bfd_coff_swap_reloc_out(abfd, i, o) \
1296. ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
1297.
1298.#define bfd_coff_swap_lineno_out(abfd, i, o) \
1299. ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
1300.
1301.#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
1302. ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
1303.
1304.#define bfd_coff_swap_sym_out(abfd, i,o) \
1305. ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
1306.
1307.#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
1308. ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
1309.
1310.#define bfd_coff_swap_filehdr_out(abfd, i,o) \
1311. ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
1312.
1313.#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
1314. ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
1315.
1316.#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
1317.#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
1318.#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
1319.#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
1320.#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
1321.#define bfd_coff_relsz(abfd) (coff_backend_info (abfd)->_bfd_relsz)
1322.#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
1323.#define bfd_coff_filnmlen(abfd) (coff_backend_info (abfd)->_bfd_filnmlen)
1324.#define bfd_coff_long_filenames(abfd) \
1325. (coff_backend_info (abfd)->_bfd_coff_long_filenames)
1326.#define bfd_coff_long_section_names(abfd) \
1327. (coff_backend_info (abfd)->_bfd_coff_long_section_names)
1328.#define bfd_coff_default_section_alignment_power(abfd) \
1329. (coff_backend_info (abfd)->_bfd_coff_default_section_alignment_power)
1330.#define bfd_coff_swap_filehdr_in(abfd, i,o) \
1331. ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
1332.
1333.#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
1334. ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
1335.
1336.#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
1337. ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
1338.
1339.#define bfd_coff_swap_reloc_in(abfd, i, o) \
1340. ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
1341.
1342.#define bfd_coff_bad_format_hook(abfd, filehdr) \
1343. ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
1344.
1345.#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
1346. ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
1347.#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
1348. ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook)\
1349. (abfd, filehdr, aouthdr))
1350.
1351.#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name, section, flags_ptr)\
1352. ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook)\
1353. (abfd, scnhdr, name, section, flags_ptr))
1354.
1355.#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
1356. ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
1357.
1358.#define bfd_coff_slurp_symbol_table(abfd)\
1359. ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
1360.
1361.#define bfd_coff_symname_in_debug(abfd, sym)\
1362. ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
1363.
1364.#define bfd_coff_force_symnames_in_strings(abfd)\
1365. (coff_backend_info (abfd)->_bfd_coff_force_symnames_in_strings)
1366.
1367.#define bfd_coff_debug_string_prefix_length(abfd)\
1368. (coff_backend_info (abfd)->_bfd_coff_debug_string_prefix_length)
1369.
1370.#define bfd_coff_print_aux(abfd, file, base, symbol, aux, indaux)\
1371. ((coff_backend_info (abfd)->_bfd_coff_print_aux)\
1372. (abfd, file, base, symbol, aux, indaux))
1373.
1374.#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order,\
1375. reloc, data, src_ptr, dst_ptr)\
1376. ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
1377. (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
1378.
1379.#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
1380. ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
1381. (abfd, section, reloc, shrink, link_info))
1382.
1383.#define bfd_coff_classify_symbol(abfd, sym)\
1384. ((coff_backend_info (abfd)->_bfd_coff_classify_symbol)\
1385. (abfd, sym))
1386.
1387.#define bfd_coff_compute_section_file_positions(abfd)\
1388. ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
1389. (abfd))
1390.
1391.#define bfd_coff_start_final_link(obfd, info)\
1392. ((coff_backend_info (obfd)->_bfd_coff_start_final_link)\
1393. (obfd, info))
1394.#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
1395. ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
1396. (obfd, info, ibfd, o, con, rel, isyms, secs))
1397.#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
1398. ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
1399. (abfd, sec, rel, h, sym, addendp))
1400.#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
1401. ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
1402. (obfd, info, ibfd, sec, rel, adjustedp))
1403.#define bfd_coff_link_add_one_symbol(info, abfd, name, flags, section,\
1404. value, string, cp, coll, hashp)\
1405. ((coff_backend_info (abfd)->_bfd_coff_link_add_one_symbol)\
1406. (info, abfd, name, flags, section, value, string, cp, coll, hashp))
1407.
1408.#define bfd_coff_link_output_has_begun(a,p) \
1409. ((coff_backend_info (a)->_bfd_coff_link_output_has_begun) (a,p))
1410.#define bfd_coff_final_link_postscript(a,p) \
1411. ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a,p))
1412.
1413*/
1414
1415/* See whether the magic number matches. */
1416
1417static bfd_boolean
1418coff_bad_format_hook (abfd, filehdr)
1419 bfd * abfd ATTRIBUTE_UNUSED;
1420 PTR filehdr;
1421{
1422 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1423
1424 if (BADMAG (*internal_f))
1425 return FALSE;
1426
1427 /* If the optional header is NULL or not the correct size then
1428 quit; the only difference I can see between m88k dgux headers (MC88DMAGIC)
1429 and Intel 960 readwrite headers (I960WRMAGIC) is that the
1430 optional header is of a different size.
1431
1432 But the mips keeps extra stuff in it's opthdr, so dont check
1433 when doing that. */
1434
1435#if defined(M88) || defined(I960)
1436 if (internal_f->f_opthdr != 0 && bfd_coff_aoutsz (abfd) != internal_f->f_opthdr)
1437 return FALSE;
1438#endif
1439
1440 return TRUE;
1441}
1442
1443/* Check whether this section uses an alignment other than the
1444 default. */
1445
1446static void
1447coff_set_custom_section_alignment (abfd, section, alignment_table, table_size)
1448 bfd *abfd ATTRIBUTE_UNUSED;
1449 asection *section;
1450 const struct coff_section_alignment_entry *alignment_table;
1451 const unsigned int table_size;
1452{
1453 const unsigned int default_alignment = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
1454 unsigned int i;
1455
1456 for (i = 0; i < table_size; ++i)
1457 {
1458 const char *secname = bfd_get_section_name (abfd, section);
1459
1460 if (alignment_table[i].comparison_length == (unsigned int) -1
1461 ? strcmp (alignment_table[i].name, secname) == 0
1462 : strncmp (alignment_table[i].name, secname,
1463 alignment_table[i].comparison_length) == 0)
1464 break;
1465 }
1466 if (i >= table_size)
1467 return;
1468
1469 if (alignment_table[i].default_alignment_min != COFF_ALIGNMENT_FIELD_EMPTY
1470 && default_alignment < alignment_table[i].default_alignment_min)
1471 return;
1472
1473 if (alignment_table[i].default_alignment_max != COFF_ALIGNMENT_FIELD_EMPTY
1474#if COFF_DEFAULT_SECTION_ALIGNMENT_POWER != 0
1475 && default_alignment > alignment_table[i].default_alignment_max
1476#endif
1477 )
1478 return;
1479
1480 section->alignment_power = alignment_table[i].alignment_power;
1481}
1482
1483/* Custom section alignment records. */
1484
1485static const struct coff_section_alignment_entry
1486coff_section_alignment_table[] =
1487{
1488#ifdef COFF_SECTION_ALIGNMENT_ENTRIES
1489 COFF_SECTION_ALIGNMENT_ENTRIES,
1490#endif
1491 /* There must not be any gaps between .stabstr sections. */
1492 { COFF_SECTION_NAME_PARTIAL_MATCH (".stabstr"),
1493 1, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
1494 /* The .stab section must be aligned to 2**2 at most, to avoid gaps. */
1495 { COFF_SECTION_NAME_PARTIAL_MATCH (".stab"),
1496 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
1497 /* Similarly for the .ctors and .dtors sections. */
1498 { COFF_SECTION_NAME_EXACT_MATCH (".ctors"),
1499 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
1500 { COFF_SECTION_NAME_EXACT_MATCH (".dtors"),
1501 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 }
1502};
1503
1504static const unsigned int coff_section_alignment_table_size =
1505 sizeof coff_section_alignment_table / sizeof coff_section_alignment_table[0];
1506
1507/* Initialize a section structure with information peculiar to this
1508 particular implementation of COFF. */
1509
1510static bfd_boolean
1511coff_new_section_hook (abfd, section)
1512 bfd * abfd;
1513 asection * section;
1514{
1515 combined_entry_type *native;
1516 bfd_size_type amt;
1517
1518 section->alignment_power = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
1519
1520#ifdef RS6000COFF_C
1521 if (bfd_xcoff_text_align_power (abfd) != 0
1522 && strcmp (bfd_get_section_name (abfd, section), ".text") == 0)
1523 section->alignment_power = bfd_xcoff_text_align_power (abfd);
1524 if (bfd_xcoff_data_align_power (abfd) != 0
1525 && strcmp (bfd_get_section_name (abfd, section), ".data") == 0)
1526 section->alignment_power = bfd_xcoff_data_align_power (abfd);
1527#endif
1528
1529 /* Allocate aux records for section symbols, to store size and
1530 related info.
1531
1532 @@ The 10 is a guess at a plausible maximum number of aux entries
1533 (but shouldn't be a constant). */
1534 amt = sizeof (combined_entry_type) * 10;
1535 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1536 if (native == NULL)
1537 return FALSE;
1538
1539 /* We don't need to set up n_name, n_value, or n_scnum in the native
1540 symbol information, since they'll be overriden by the BFD symbol
1541 anyhow. However, we do need to set the type and storage class,
1542 in case this symbol winds up getting written out. The value 0
1543 for n_numaux is already correct. */
1544
1545 native->u.syment.n_type = T_NULL;
1546 native->u.syment.n_sclass = C_STAT;
1547
1548 coffsymbol (section->symbol)->native = native;
1549
1550 coff_set_custom_section_alignment (abfd, section,
1551 coff_section_alignment_table,
1552 coff_section_alignment_table_size);
1553
1554 return TRUE;
1555}
1556
1557#ifdef COFF_ALIGN_IN_SECTION_HEADER
1558
1559/* Set the alignment of a BFD section. */
1560
1561static void coff_set_alignment_hook PARAMS ((bfd *, asection *, PTR));
1562
1563static void
1564coff_set_alignment_hook (abfd, section, scnhdr)
1565 bfd * abfd ATTRIBUTE_UNUSED;
1566 asection * section;
1567 PTR scnhdr;
1568{
1569 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1570 unsigned int i;
1571
1572#ifdef I960
1573 /* Extract ALIGN from 2**ALIGN stored in section header. */
1574 for (i = 0; i < 32; i++)
1575 if ((1 << i) >= hdr->s_align)
1576 break;
1577#endif
1578#ifdef TIC80COFF
1579 /* TI tools puts the alignment power in bits 8-11. */
1580 i = (hdr->s_flags >> 8) & 0xF ;
1581#endif
1582#ifdef COFF_DECODE_ALIGNMENT
1583 i = COFF_DECODE_ALIGNMENT(hdr->s_flags);
1584#endif
1585 section->alignment_power = i;
1586
1587#ifdef coff_set_section_load_page
1588 coff_set_section_load_page (section, hdr->s_page);
1589#endif
1590}
1591
1592#else /* ! COFF_ALIGN_IN_SECTION_HEADER */
1593#ifdef COFF_WITH_PE
1594
1595/* A couple of macros to help setting the alignment power field. */
1596#define ALIGN_SET(field,x,y) \
1597 if (((field) & IMAGE_SCN_ALIGN_64BYTES) == x )\
1598 {\
1599 section->alignment_power = y;\
1600 }
1601
1602#define ELIFALIGN_SET(field,x,y) \
1603 else if (( (field) & IMAGE_SCN_ALIGN_64BYTES) == x ) \
1604 {\
1605 section->alignment_power = y;\
1606 }
1607
1608static void coff_set_alignment_hook PARAMS ((bfd *, asection *, PTR));
1609
1610static void
1611coff_set_alignment_hook (abfd, section, scnhdr)
1612 bfd * abfd ATTRIBUTE_UNUSED;
1613 asection * section;
1614 PTR scnhdr;
1615{
1616 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1617 bfd_size_type amt;
1618
1619 ALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_64BYTES, 6)
1620 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_32BYTES, 5)
1621 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_16BYTES, 4)
1622 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_8BYTES, 3)
1623 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_4BYTES, 2)
1624 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_2BYTES, 1)
1625 ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_1BYTES, 0)
1626
1627 /* In a PE image file, the s_paddr field holds the virtual size of a
1628 section, while the s_size field holds the raw size. We also keep
1629 the original section flag value, since not every bit can be
1630 mapped onto a generic BFD section bit. */
1631 if (coff_section_data (abfd, section) == NULL)
1632 {
1633 amt = sizeof (struct coff_section_tdata);
1634 section->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
1635 if (section->used_by_bfd == NULL)
1636 {
1637 /* FIXME: Return error. */
1638 abort ();
1639 }
1640 }
1641 if (pei_section_data (abfd, section) == NULL)
1642 {
1643 amt = sizeof (struct pei_section_tdata);
1644 coff_section_data (abfd, section)->tdata = (PTR) bfd_zalloc (abfd, amt);
1645 if (coff_section_data (abfd, section)->tdata == NULL)
1646 {
1647 /* FIXME: Return error. */
1648 abort ();
1649 }
1650 }
1651 pei_section_data (abfd, section)->virt_size = hdr->s_paddr;
1652 pei_section_data (abfd, section)->pe_flags = hdr->s_flags;
1653
1654 section->lma = hdr->s_vaddr;
1655
1656 /* Check for extended relocs. */
1657 if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
1658 {
1659 struct external_reloc dst;
1660 struct internal_reloc n;
1661 file_ptr oldpos = bfd_tell (abfd);
1662 bfd_seek (abfd, (file_ptr) hdr->s_relptr, 0);
1663 if (bfd_bread ((PTR) &dst, (bfd_size_type) bfd_coff_relsz (abfd), abfd)
1664 != bfd_coff_relsz (abfd))
1665 return;
1666
1667 coff_swap_reloc_in (abfd, &dst, &n);
1668 bfd_seek (abfd, oldpos, 0);
1669 section->reloc_count = hdr->s_nreloc = n.r_vaddr;
1670 }
1671}
1672#undef ALIGN_SET
1673#undef ELIFALIGN_SET
1674
1675#else /* ! COFF_WITH_PE */
1676#ifdef RS6000COFF_C
1677
1678/* We grossly abuse this function to handle XCOFF overflow headers.
1679 When we see one, we correct the reloc and line number counts in the
1680 real header, and remove the section we just created. */
1681
1682static void coff_set_alignment_hook PARAMS ((bfd *, asection *, PTR));
1683
1684static void
1685coff_set_alignment_hook (abfd, section, scnhdr)
1686 bfd *abfd;
1687 asection *section;
1688 PTR scnhdr;
1689{
1690 struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
1691 asection *real_sec;
1692 asection **ps;
1693
1694 if ((hdr->s_flags & STYP_OVRFLO) == 0)
1695 return;
1696
1697 real_sec = coff_section_from_bfd_index (abfd, (int) hdr->s_nreloc);
1698 if (real_sec == NULL)
1699 return;
1700
1701 real_sec->reloc_count = hdr->s_paddr;
1702 real_sec->lineno_count = hdr->s_vaddr;
1703
1704 for (ps = &abfd->sections; *ps != NULL; ps = &(*ps)->next)
1705 {
1706 if (*ps == section)
1707 {
1708 bfd_section_list_remove (abfd, ps);
1709 --abfd->section_count;
1710 break;
1711 }
1712 }
1713}
1714
1715#else /* ! RS6000COFF_C */
1716
1717#define coff_set_alignment_hook \
1718 ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
1719
1720#endif /* ! RS6000COFF_C */
1721#endif /* ! COFF_WITH_PE */
1722#endif /* ! COFF_ALIGN_IN_SECTION_HEADER */
1723
1724#ifndef coff_mkobject
1725
1726static bfd_boolean coff_mkobject PARAMS ((bfd *));
1727
1728static bfd_boolean
1729coff_mkobject (abfd)
1730 bfd * abfd;
1731{
1732 coff_data_type *coff;
1733 bfd_size_type amt = sizeof (coff_data_type);
1734
1735 abfd->tdata.coff_obj_data = (struct coff_tdata *) bfd_zalloc (abfd, amt);
1736 if (abfd->tdata.coff_obj_data == 0)
1737 return FALSE;
1738 coff = coff_data (abfd);
1739 coff->symbols = (coff_symbol_type *) NULL;
1740 coff->conversion_table = (unsigned int *) NULL;
1741 coff->raw_syments = (struct coff_ptr_struct *) NULL;
1742 coff->relocbase = 0;
1743 coff->local_toc_sym_map = 0;
1744
1745/* make_abs_section(abfd);*/
1746
1747 return TRUE;
1748}
1749#endif
1750
1751/* Create the COFF backend specific information. */
1752
1753#ifndef coff_mkobject_hook
1754static PTR
1755coff_mkobject_hook (abfd, filehdr, aouthdr)
1756 bfd * abfd;
1757 PTR filehdr;
1758 PTR aouthdr ATTRIBUTE_UNUSED;
1759{
1760 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1761 coff_data_type *coff;
1762
1763 if (! coff_mkobject (abfd))
1764 return NULL;
1765
1766 coff = coff_data (abfd);
1767
1768 coff->sym_filepos = internal_f->f_symptr;
1769
1770 /* These members communicate important constants about the symbol
1771 table to GDB's symbol-reading code. These `constants'
1772 unfortunately vary among coff implementations... */
1773 coff->local_n_btmask = N_BTMASK;
1774 coff->local_n_btshft = N_BTSHFT;
1775 coff->local_n_tmask = N_TMASK;
1776 coff->local_n_tshift = N_TSHIFT;
1777 coff->local_symesz = bfd_coff_symesz (abfd);
1778 coff->local_auxesz = bfd_coff_auxesz (abfd);
1779 coff->local_linesz = bfd_coff_linesz (abfd);
1780
1781 coff->timestamp = internal_f->f_timdat;
1782
1783 obj_raw_syment_count (abfd) =
1784 obj_conv_table_size (abfd) =
1785 internal_f->f_nsyms;
1786
1787#ifdef RS6000COFF_C
1788 if ((internal_f->f_flags & F_SHROBJ) != 0)
1789 abfd->flags |= DYNAMIC;
1790 if (aouthdr != NULL && internal_f->f_opthdr >= bfd_coff_aoutsz (abfd))
1791 {
1792 struct internal_aouthdr *internal_a =
1793 (struct internal_aouthdr *) aouthdr;
1794 struct xcoff_tdata *xcoff;
1795
1796 xcoff = xcoff_data (abfd);
1797# ifdef U803XTOCMAGIC
1798 xcoff->xcoff64 = internal_f->f_magic == U803XTOCMAGIC;
1799# else
1800 xcoff->xcoff64 = 0;
1801# endif
1802 xcoff->full_aouthdr = TRUE;
1803 xcoff->toc = internal_a->o_toc;
1804 xcoff->sntoc = internal_a->o_sntoc;
1805 xcoff->snentry = internal_a->o_snentry;
1806 bfd_xcoff_text_align_power (abfd) = internal_a->o_algntext;
1807 bfd_xcoff_data_align_power (abfd) = internal_a->o_algndata;
1808 xcoff->modtype = internal_a->o_modtype;
1809 xcoff->cputype = internal_a->o_cputype;
1810 xcoff->maxdata = internal_a->o_maxdata;
1811 xcoff->maxstack = internal_a->o_maxstack;
1812 }
1813#endif
1814
1815#ifdef ARM
1816 /* Set the flags field from the COFF header read in. */
1817 if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
1818 coff->flags = 0;
1819#endif
1820
1821#ifdef COFF_WITH_PE
1822 /* FIXME: I'm not sure this is ever executed, since peicode.h
1823 defines coff_mkobject_hook. */
1824 if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
1825 abfd->flags |= HAS_DEBUG;
1826#endif
1827
1828 return (PTR) coff;
1829}
1830#endif
1831
1832/* Determine the machine architecture and type. FIXME: This is target
1833 dependent because the magic numbers are defined in the target
1834 dependent header files. But there is no particular need for this.
1835 If the magic numbers were moved to a separate file, this function
1836 would be target independent and would also be much more successful
1837 at linking together COFF files for different architectures. */
1838
1839static bfd_boolean
1840coff_set_arch_mach_hook (abfd, filehdr)
1841 bfd *abfd;
1842 PTR filehdr;
1843{
1844 unsigned long machine;
1845 enum bfd_architecture arch;
1846 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
1847
1848 /* Zero selects the default machine for an arch. */
1849 machine = 0;
1850 switch (internal_f->f_magic)
1851 {
1852#ifdef OR32_MAGIC_BIG
1853 case OR32_MAGIC_BIG:
1854 case OR32_MAGIC_LITTLE:
1855 arch = bfd_arch_or32;
1856 break;
1857#endif
1858#ifdef PPCMAGIC
1859 case PPCMAGIC:
1860 arch = bfd_arch_powerpc;
1861 break;
1862#endif
1863#ifdef I386MAGIC
1864 case I386MAGIC:
1865 case I386PTXMAGIC:
1866 case I386AIXMAGIC: /* Danbury PS/2 AIX C Compiler */
1867 case LYNXCOFFMAGIC: /* shadows the m68k Lynx number below, sigh */
1868 arch = bfd_arch_i386;
1869 break;
1870#endif
1871#ifdef IA64MAGIC
1872 case IA64MAGIC:
1873 arch = bfd_arch_ia64;
1874 break;
1875#endif
1876#ifdef A29K_MAGIC_BIG
1877 case A29K_MAGIC_BIG:
1878 case A29K_MAGIC_LITTLE:
1879 arch = bfd_arch_a29k;
1880 break;
1881#endif
1882#ifdef ARMMAGIC
1883 case ARMMAGIC:
1884 case ARMPEMAGIC:
1885 case THUMBPEMAGIC:
1886 arch = bfd_arch_arm;
1887 machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
1888 if (machine == bfd_mach_arm_unknown)
1889 {
1890 switch (internal_f->f_flags & F_ARM_ARCHITECTURE_MASK)
1891 {
1892 case F_ARM_2: machine = bfd_mach_arm_2; break;
1893 case F_ARM_2a: machine = bfd_mach_arm_2a; break;
1894 case F_ARM_3: machine = bfd_mach_arm_3; break;
1895 default:
1896 case F_ARM_3M: machine = bfd_mach_arm_3M; break;
1897 case F_ARM_4: machine = bfd_mach_arm_4; break;
1898 case F_ARM_4T: machine = bfd_mach_arm_4T; break;
1899 /* The COFF header does not have enough bits available
1900 to cover all the different ARM architectures. So
1901 we interpret F_ARM_5, the highest flag value to mean
1902 "the highest ARM architecture known to BFD" which is
1903 currently the XScale. */
1904 case F_ARM_5: machine = bfd_mach_arm_XScale; break;
1905 }
1906 }
1907 break;
1908#endif
1909#ifdef MC68MAGIC
1910 case MC68MAGIC:
1911 case M68MAGIC:
1912#ifdef MC68KBCSMAGIC
1913 case MC68KBCSMAGIC:
1914#endif
1915#ifdef APOLLOM68KMAGIC
1916 case APOLLOM68KMAGIC:
1917#endif
1918#ifdef LYNXCOFFMAGIC
1919 case LYNXCOFFMAGIC:
1920#endif
1921 arch = bfd_arch_m68k;
1922 machine = bfd_mach_m68020;
1923 break;
1924#endif
1925#ifdef MC88MAGIC
1926 case MC88MAGIC:
1927 case MC88DMAGIC:
1928 case MC88OMAGIC:
1929 arch = bfd_arch_m88k;
1930 machine = 88100;
1931 break;
1932#endif
1933#ifdef Z8KMAGIC
1934 case Z8KMAGIC:
1935 arch = bfd_arch_z8k;
1936 switch (internal_f->f_flags & F_MACHMASK)
1937 {
1938 case F_Z8001:
1939 machine = bfd_mach_z8001;
1940 break;
1941 case F_Z8002:
1942 machine = bfd_mach_z8002;
1943 break;
1944 default:
1945 return FALSE;
1946 }
1947 break;
1948#endif
1949#ifdef I860
1950 case I860MAGIC:
1951 arch = bfd_arch_i860;
1952 break;
1953#endif
1954#ifdef I960
1955#ifdef I960ROMAGIC
1956 case I960ROMAGIC:
1957 case I960RWMAGIC:
1958 arch = bfd_arch_i960;
1959 switch (F_I960TYPE & internal_f->f_flags)
1960 {
1961 default:
1962 case F_I960CORE:
1963 machine = bfd_mach_i960_core;
1964 break;
1965 case F_I960KB:
1966 machine = bfd_mach_i960_kb_sb;
1967 break;
1968 case F_I960MC:
1969 machine = bfd_mach_i960_mc;
1970 break;
1971 case F_I960XA:
1972 machine = bfd_mach_i960_xa;
1973 break;
1974 case F_I960CA:
1975 machine = bfd_mach_i960_ca;
1976 break;
1977 case F_I960KA:
1978 machine = bfd_mach_i960_ka_sa;
1979 break;
1980 case F_I960JX:
1981 machine = bfd_mach_i960_jx;
1982 break;
1983 case F_I960HX:
1984 machine = bfd_mach_i960_hx;
1985 break;
1986 }
1987 break;
1988#endif
1989#endif
1990
1991#ifdef RS6000COFF_C
1992#ifdef XCOFF64
1993 case U64_TOCMAGIC:
1994 case U803XTOCMAGIC:
1995#else
1996 case U802ROMAGIC:
1997 case U802WRMAGIC:
1998 case U802TOCMAGIC:
1999#endif
2000 {
2001 int cputype;
2002
2003 if (xcoff_data (abfd)->cputype != -1)
2004 cputype = xcoff_data (abfd)->cputype & 0xff;
2005 else
2006 {
2007 /* We did not get a value from the a.out header. If the
2008 file has not been stripped, we may be able to get the
2009 architecture information from the first symbol, if it
2010 is a .file symbol. */
2011 if (obj_raw_syment_count (abfd) == 0)
2012 cputype = 0;
2013 else
2014 {
2015 bfd_byte *buf;
2016 struct internal_syment sym;
2017 bfd_size_type amt = bfd_coff_symesz (abfd);
2018
2019 buf = (bfd_byte *) bfd_malloc (amt);
2020 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
2021 || bfd_bread (buf, amt, abfd) != amt)
2022 {
2023 free (buf);
2024 return FALSE;
2025 }
2026 bfd_coff_swap_sym_in (abfd, (PTR) buf, (PTR) &sym);
2027 if (sym.n_sclass == C_FILE)
2028 cputype = sym.n_type & 0xff;
2029 else
2030 cputype = 0;
2031 free (buf);
2032 }
2033 }
2034
2035 /* FIXME: We don't handle all cases here. */
2036 switch (cputype)
2037 {
2038 default:
2039 case 0:
2040 arch = bfd_xcoff_architecture (abfd);
2041 machine = bfd_xcoff_machine (abfd);
2042 break;
2043
2044 case 1:
2045 arch = bfd_arch_powerpc;
2046 machine = bfd_mach_ppc_601;
2047 break;
2048 case 2: /* 64 bit PowerPC */
2049 arch = bfd_arch_powerpc;
2050 machine = bfd_mach_ppc_620;
2051 break;
2052 case 3:
2053 arch = bfd_arch_powerpc;
2054 machine = bfd_mach_ppc;
2055 break;
2056 case 4:
2057 arch = bfd_arch_rs6000;
2058 machine = bfd_mach_rs6k;
2059 break;
2060 }
2061 }
2062 break;
2063#endif
2064
2065#ifdef WE32KMAGIC
2066 case WE32KMAGIC:
2067 arch = bfd_arch_we32k;
2068 break;
2069#endif
2070
2071#ifdef H8300MAGIC
2072 case H8300MAGIC:
2073 arch = bfd_arch_h8300;
2074 machine = bfd_mach_h8300;
2075 /* !! FIXME this probably isn't the right place for this. */
2076 abfd->flags |= BFD_IS_RELAXABLE;
2077 break;
2078#endif
2079
2080#ifdef H8300HMAGIC
2081 case H8300HMAGIC:
2082 arch = bfd_arch_h8300;
2083 machine = bfd_mach_h8300h;
2084 /* !! FIXME this probably isn't the right place for this. */
2085 abfd->flags |= BFD_IS_RELAXABLE;
2086 break;
2087#endif
2088
2089#ifdef H8300SMAGIC
2090 case H8300SMAGIC:
2091 arch = bfd_arch_h8300;
2092 machine = bfd_mach_h8300s;
2093 /* !! FIXME this probably isn't the right place for this. */
2094 abfd->flags |= BFD_IS_RELAXABLE;
2095 break;
2096#endif
2097
2098#ifdef H8300HNMAGIC
2099 case H8300HNMAGIC:
2100 arch = bfd_arch_h8300;
2101 machine = bfd_mach_h8300hn;
2102 /* !! FIXME this probably isn't the right place for this. */
2103 abfd->flags |= BFD_IS_RELAXABLE;
2104 break;
2105#endif
2106
2107#ifdef H8300SNMAGIC
2108 case H8300SNMAGIC:
2109 arch = bfd_arch_h8300;
2110 machine = bfd_mach_h8300sn;
2111 /* !! FIXME this probably isn't the right place for this. */
2112 abfd->flags |= BFD_IS_RELAXABLE;
2113 break;
2114#endif
2115
2116#ifdef SH_ARCH_MAGIC_BIG
2117 case SH_ARCH_MAGIC_BIG:
2118 case SH_ARCH_MAGIC_LITTLE:
2119#ifdef COFF_WITH_PE
2120 case SH_ARCH_MAGIC_WINCE:
2121#endif
2122 arch = bfd_arch_sh;
2123 break;
2124#endif
2125
2126#ifdef MIPS_ARCH_MAGIC_WINCE
2127 case MIPS_ARCH_MAGIC_WINCE:
2128 arch = bfd_arch_mips;
2129 break;
2130#endif
2131
2132#ifdef H8500MAGIC
2133 case H8500MAGIC:
2134 arch = bfd_arch_h8500;
2135 break;
2136#endif
2137
2138#ifdef SPARCMAGIC
2139 case SPARCMAGIC:
2140#ifdef LYNXCOFFMAGIC
2141 case LYNXCOFFMAGIC:
2142#endif
2143 arch = bfd_arch_sparc;
2144 break;
2145#endif
2146
2147#ifdef TIC30MAGIC
2148 case TIC30MAGIC:
2149 arch = bfd_arch_tic30;
2150 break;
2151#endif
2152
2153#ifdef TICOFF0MAGIC
2154#ifdef TICOFF_TARGET_ARCH
2155 /* This TI COFF section should be used by all new TI COFF v0 targets. */
2156 case TICOFF0MAGIC:
2157 arch = TICOFF_TARGET_ARCH;
2158 machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
2159 break;
2160#endif
2161#endif
2162
2163#ifdef TICOFF1MAGIC
2164 /* This TI COFF section should be used by all new TI COFF v1/2 targets. */
2165 /* TI COFF1 and COFF2 use the target_id field to specify which arch. */
2166 case TICOFF1MAGIC:
2167 case TICOFF2MAGIC:
2168 switch (internal_f->f_target_id)
2169 {
2170#ifdef TI_TARGET_ID
2171 case TI_TARGET_ID:
2172 arch = TICOFF_TARGET_ARCH;
2173 machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
2174 break;
2175#endif
2176 default:
2177 arch = bfd_arch_obscure;
2178 (*_bfd_error_handler)
2179 (_("Unrecognized TI COFF target id '0x%x'"),
2180 internal_f->f_target_id);
2181 break;
2182 }
2183 break;
2184#endif
2185
2186#ifdef TIC80_ARCH_MAGIC
2187 case TIC80_ARCH_MAGIC:
2188 arch = bfd_arch_tic80;
2189 break;
2190#endif
2191
2192#ifdef MCOREMAGIC
2193 case MCOREMAGIC:
2194 arch = bfd_arch_mcore;
2195 break;
2196#endif
2197
2198#ifdef W65MAGIC
2199 case W65MAGIC:
2200 arch = bfd_arch_w65;
2201 break;
2202#endif
2203
2204 default: /* Unreadable input file type. */
2205 arch = bfd_arch_obscure;
2206 break;
2207 }
2208
2209 bfd_default_set_arch_mach (abfd, arch, machine);
2210 return TRUE;
2211}
2212
2213#ifdef SYMNAME_IN_DEBUG
2214
2215static bfd_boolean symname_in_debug_hook
2216 PARAMS ((bfd *, struct internal_syment *));
2217
2218static bfd_boolean
2219symname_in_debug_hook (abfd, sym)
2220 bfd * abfd ATTRIBUTE_UNUSED;
2221 struct internal_syment *sym;
2222{
2223 return SYMNAME_IN_DEBUG (sym) != 0;
2224}
2225
2226#else
2227
2228#define symname_in_debug_hook \
2229 (bfd_boolean (*) PARAMS ((bfd *, struct internal_syment *))) bfd_false
2230
2231#endif
2232
2233#ifdef RS6000COFF_C
2234
2235#ifdef XCOFF64
2236#define FORCE_SYMNAMES_IN_STRINGS
2237#endif
2238
2239/* Handle the csect auxent of a C_EXT or C_HIDEXT symbol. */
2240
2241static bfd_boolean coff_pointerize_aux_hook
2242 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
2243 unsigned int, combined_entry_type *));
2244
2245static bfd_boolean
2246coff_pointerize_aux_hook (abfd, table_base, symbol, indaux, aux)
2247 bfd *abfd ATTRIBUTE_UNUSED;
2248 combined_entry_type *table_base;
2249 combined_entry_type *symbol;
2250 unsigned int indaux;
2251 combined_entry_type *aux;
2252{
2253 int class = symbol->u.syment.n_sclass;
2254
2255 if ((class == C_EXT || class == C_HIDEXT)
2256 && indaux + 1 == symbol->u.syment.n_numaux)
2257 {
2258 if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD)
2259 {
2260 aux->u.auxent.x_csect.x_scnlen.p =
2261 table_base + aux->u.auxent.x_csect.x_scnlen.l;
2262 aux->fix_scnlen = 1;
2263 }
2264
2265 /* Return TRUE to indicate that the caller should not do any
2266 further work on this auxent. */
2267 return TRUE;
2268 }
2269
2270 /* Return FALSE to indicate that this auxent should be handled by
2271 the caller. */
2272 return FALSE;
2273}
2274
2275#else
2276#ifdef I960
2277
2278/* We don't want to pointerize bal entries. */
2279
2280static bfd_boolean coff_pointerize_aux_hook
2281 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
2282 unsigned int, combined_entry_type *));
2283
2284static bfd_boolean
2285coff_pointerize_aux_hook (abfd, table_base, symbol, indaux, aux)
2286 bfd *abfd ATTRIBUTE_UNUSED;
2287 combined_entry_type *table_base ATTRIBUTE_UNUSED;
2288 combined_entry_type *symbol;
2289 unsigned int indaux;
2290 combined_entry_type *aux ATTRIBUTE_UNUSED;
2291{
2292 /* Return TRUE if we don't want to pointerize this aux entry, which
2293 is the case for the lastfirst aux entry for a C_LEAFPROC symbol. */
2294 return (indaux == 1
2295 && (symbol->u.syment.n_sclass == C_LEAFPROC
2296 || symbol->u.syment.n_sclass == C_LEAFSTAT
2297 || symbol->u.syment.n_sclass == C_LEAFEXT));
2298}
2299
2300#else /* ! I960 */
2301
2302#define coff_pointerize_aux_hook 0
2303
2304#endif /* ! I960 */
2305#endif /* ! RS6000COFF_C */
2306
2307/* Print an aux entry. This returns TRUE if it has printed it. */
2308
2309static bfd_boolean coff_print_aux
2310 PARAMS ((bfd *, FILE *, combined_entry_type *, combined_entry_type *,
2311 combined_entry_type *, unsigned int));
2312
2313static bfd_boolean
2314coff_print_aux (abfd, file, table_base, symbol, aux, indaux)
2315 bfd *abfd ATTRIBUTE_UNUSED;
2316 FILE *file ATTRIBUTE_UNUSED;
2317 combined_entry_type *table_base ATTRIBUTE_UNUSED;
2318 combined_entry_type *symbol ATTRIBUTE_UNUSED;
2319 combined_entry_type *aux ATTRIBUTE_UNUSED;
2320 unsigned int indaux ATTRIBUTE_UNUSED;
2321{
2322#ifdef RS6000COFF_C
2323 if ((symbol->u.syment.n_sclass == C_EXT
2324 || symbol->u.syment.n_sclass == C_HIDEXT)
2325 && indaux + 1 == symbol->u.syment.n_numaux)
2326 {
2327 /* This is a csect entry. */
2328 fprintf (file, "AUX ");
2329 if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) != XTY_LD)
2330 {
2331 BFD_ASSERT (! aux->fix_scnlen);
2332#ifdef XCOFF64
2333 fprintf (file, "val %5lld", aux->u.auxent.x_csect.x_scnlen.l);
2334#else
2335 fprintf (file, "val %5ld", (long) aux->u.auxent.x_csect.x_scnlen.l);
2336#endif
2337 }
2338 else
2339 {
2340 fprintf (file, "indx ");
2341 if (! aux->fix_scnlen)
2342#ifdef XCOFF64
2343 fprintf (file, "%4lld", aux->u.auxent.x_csect.x_scnlen.l);
2344#else
2345 fprintf (file, "%4ld", (long) aux->u.auxent.x_csect.x_scnlen.l);
2346#endif
2347 else
2348 fprintf (file, "%4ld",
2349 (long) (aux->u.auxent.x_csect.x_scnlen.p - table_base));
2350 }
2351 fprintf (file,
2352 " prmhsh %ld snhsh %u typ %d algn %d clss %u stb %ld snstb %u",
2353 aux->u.auxent.x_csect.x_parmhash,
2354 (unsigned int) aux->u.auxent.x_csect.x_snhash,
2355 SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp),
2356 SMTYP_ALIGN (aux->u.auxent.x_csect.x_smtyp),
2357 (unsigned int) aux->u.auxent.x_csect.x_smclas,
2358 aux->u.auxent.x_csect.x_stab,
2359 (unsigned int) aux->u.auxent.x_csect.x_snstab);
2360 return TRUE;
2361 }
2362#endif
2363
2364 /* Return FALSE to indicate that no special action was taken. */
2365 return FALSE;
2366}
2367
2368/*
2369SUBSUBSECTION
2370 Writing relocations
2371
2372 To write relocations, the back end steps though the
2373 canonical relocation table and create an
2374 @code{internal_reloc}. The symbol index to use is removed from
2375 the @code{offset} field in the symbol table supplied. The
2376 address comes directly from the sum of the section base
2377 address and the relocation offset; the type is dug directly
2378 from the howto field. Then the @code{internal_reloc} is
2379 swapped into the shape of an @code{external_reloc} and written
2380 out to disk.
2381
2382*/
2383
2384#ifdef TARG_AUX
2385
2386static int compare_arelent_ptr PARAMS ((const PTR, const PTR));
2387
2388/* AUX's ld wants relocations to be sorted. */
2389static int
2390compare_arelent_ptr (x, y)
2391 const PTR x;
2392 const PTR y;
2393{
2394 const arelent **a = (const arelent **) x;
2395 const arelent **b = (const arelent **) y;
2396 bfd_size_type aadr = (*a)->address;
2397 bfd_size_type badr = (*b)->address;
2398
2399 return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
2400}
2401
2402#endif /* TARG_AUX */
2403
2404static bfd_boolean
2405coff_write_relocs (abfd, first_undef)
2406 bfd * abfd;
2407 int first_undef;
2408{
2409 asection *s;
2410
2411 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
2412 {
2413 unsigned int i;
2414 struct external_reloc dst;
2415 arelent **p;
2416
2417#ifndef TARG_AUX
2418 p = s->orelocation;
2419#else
2420 {
2421 /* Sort relocations before we write them out. */
2422 bfd_size_type amt;
2423
2424 amt = s->reloc_count;
2425 amt *= sizeof (arelent *);
2426 p = (arelent **) bfd_malloc (amt);
2427 if (p == NULL && s->reloc_count > 0)
2428 return FALSE;
2429 memcpy (p, s->orelocation, (size_t) amt);
2430 qsort (p, s->reloc_count, sizeof (arelent *), compare_arelent_ptr);
2431 }
2432#endif
2433
2434 if (bfd_seek (abfd, s->rel_filepos, SEEK_SET) != 0)
2435 return FALSE;
2436
2437#ifdef COFF_WITH_PE
2438 if (obj_pe (abfd) && s->reloc_count >= 0xffff)
2439 {
2440 /* Encode real count here as first reloc. */
2441 struct internal_reloc n;
2442
2443 memset ((PTR) & n, 0, sizeof (n));
2444 /* Add one to count *this* reloc (grr). */
2445 n.r_vaddr = s->reloc_count + 1;
2446 coff_swap_reloc_out (abfd, &n, &dst);
2447 if (bfd_bwrite ((PTR) & dst, (bfd_size_type) bfd_coff_relsz (abfd),
2448 abfd) != bfd_coff_relsz (abfd))
2449 return FALSE;
2450 }
2451#endif
2452
2453 for (i = 0; i < s->reloc_count; i++)
2454 {
2455 struct internal_reloc n;
2456 arelent *q = p[i];
2457
2458 memset ((PTR) & n, 0, sizeof (n));
2459
2460 /* Now we've renumbered the symbols we know where the
2461 undefined symbols live in the table. Check the reloc
2462 entries for symbols who's output bfd isn't the right one.
2463 This is because the symbol was undefined (which means
2464 that all the pointers are never made to point to the same
2465 place). This is a bad thing,'cause the symbols attached
2466 to the output bfd are indexed, so that the relocation
2467 entries know which symbol index they point to. So we
2468 have to look up the output symbol here. */
2469
2470 if (q->sym_ptr_ptr[0]->the_bfd != abfd)
2471 {
2472 int j;
2473 const char *sname = q->sym_ptr_ptr[0]->name;
2474 asymbol **outsyms = abfd->outsymbols;
2475
2476 for (j = first_undef; outsyms[j]; j++)
2477 {
2478 const char *intable = outsyms[j]->name;
2479
2480 if (strcmp (intable, sname) == 0) {
2481 /* Got a hit, so repoint the reloc. */
2482 q->sym_ptr_ptr = outsyms + j;
2483 break;
2484 }
2485 }
2486 }
2487
2488 n.r_vaddr = q->address + s->vma;
2489
2490#ifdef R_IHCONST
2491 /* The 29k const/consth reloc pair is a real kludge. The consth
2492 part doesn't have a symbol; it has an offset. So rebuilt
2493 that here. */
2494 if (q->howto->type == R_IHCONST)
2495 n.r_symndx = q->addend;
2496 else
2497#endif
2498 if (q->sym_ptr_ptr)
2499 {
2500#ifdef SECTION_RELATIVE_ABSOLUTE_SYMBOL_P
2501 if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q,s))
2502#else
2503 if ((*q->sym_ptr_ptr)->section == bfd_abs_section_ptr
2504 && ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0)
2505#endif
2506 /* This is a relocation relative to the absolute symbol. */
2507 n.r_symndx = -1;
2508 else
2509 {
2510 n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
2511 /* Take notice if the symbol reloc points to a symbol
2512 we don't have in our symbol table. What should we
2513 do for this?? */
2514 if (n.r_symndx > obj_conv_table_size (abfd))
2515 abort ();
2516 }
2517 }
2518
2519#ifdef SWAP_OUT_RELOC_OFFSET
2520 n.r_offset = q->addend;
2521#endif
2522
2523#ifdef SELECT_RELOC
2524 /* Work out reloc type from what is required. */
2525 SELECT_RELOC (n, q->howto);
2526#else
2527 n.r_type = q->howto->type;
2528#endif
2529 coff_swap_reloc_out (abfd, &n, &dst);
2530
2531 if (bfd_bwrite ((PTR) & dst, (bfd_size_type) bfd_coff_relsz (abfd),
2532 abfd) != bfd_coff_relsz (abfd))
2533 return FALSE;
2534 }
2535
2536#ifdef TARG_AUX
2537 if (p != NULL)
2538 free (p);
2539#endif
2540 }
2541
2542 return TRUE;
2543}
2544
2545/* Set flags and magic number of a coff file from architecture and machine
2546 type. Result is TRUE if we can represent the arch&type, FALSE if not. */
2547
2548static bfd_boolean
2549coff_set_flags (abfd, magicp, flagsp)
2550 bfd * abfd;
2551 unsigned int *magicp ATTRIBUTE_UNUSED;
2552 unsigned short *flagsp ATTRIBUTE_UNUSED;
2553{
2554 switch (bfd_get_arch (abfd))
2555 {
2556#ifdef Z8KMAGIC
2557 case bfd_arch_z8k:
2558 *magicp = Z8KMAGIC;
2559 switch (bfd_get_mach (abfd))
2560 {
2561 case bfd_mach_z8001:
2562 *flagsp = F_Z8001;
2563 break;
2564 case bfd_mach_z8002:
2565 *flagsp = F_Z8002;
2566 break;
2567 default:
2568 return FALSE;
2569 }
2570 return TRUE;
2571#endif
2572#ifdef I960ROMAGIC
2573
2574 case bfd_arch_i960:
2575
2576 {
2577 unsigned flags;
2578 *magicp = I960ROMAGIC;
2579 /*
2580 ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
2581 I960RWMAGIC); FIXME???
2582 */
2583 switch (bfd_get_mach (abfd))
2584 {
2585 case bfd_mach_i960_core:
2586 flags = F_I960CORE;
2587 break;
2588 case bfd_mach_i960_kb_sb:
2589 flags = F_I960KB;
2590 break;
2591 case bfd_mach_i960_mc:
2592 flags = F_I960MC;
2593 break;
2594 case bfd_mach_i960_xa:
2595 flags = F_I960XA;
2596 break;
2597 case bfd_mach_i960_ca:
2598 flags = F_I960CA;
2599 break;
2600 case bfd_mach_i960_ka_sa:
2601 flags = F_I960KA;
2602 break;
2603 case bfd_mach_i960_jx:
2604 flags = F_I960JX;
2605 break;
2606 case bfd_mach_i960_hx:
2607 flags = F_I960HX;
2608 break;
2609 default:
2610 return FALSE;
2611 }
2612 *flagsp = flags;
2613 return TRUE;
2614 }
2615 break;
2616#endif
2617
2618#ifdef TIC30MAGIC
2619 case bfd_arch_tic30:
2620 *magicp = TIC30MAGIC;
2621 return TRUE;
2622#endif
2623
2624#ifdef TICOFF_DEFAULT_MAGIC
2625 case TICOFF_TARGET_ARCH:
2626 /* If there's no indication of which version we want, use the default. */
2627 if (!abfd->xvec )
2628 *magicp = TICOFF_DEFAULT_MAGIC;
2629 else
2630 {
2631 /* We may want to output in a different COFF version. */
2632 switch (abfd->xvec->name[4])
2633 {
2634 case '0':
2635 *magicp = TICOFF0MAGIC;
2636 break;
2637 case '1':
2638 *magicp = TICOFF1MAGIC;
2639 break;
2640 case '2':
2641 *magicp = TICOFF2MAGIC;
2642 break;
2643 default:
2644 return FALSE;
2645 }
2646 }
2647 TICOFF_TARGET_MACHINE_SET (flagsp, bfd_get_mach (abfd));
2648 return TRUE;
2649#endif
2650
2651#ifdef TIC80_ARCH_MAGIC
2652 case bfd_arch_tic80:
2653 *magicp = TIC80_ARCH_MAGIC;
2654 return TRUE;
2655#endif
2656#ifdef ARMMAGIC
2657 case bfd_arch_arm:
2658#ifdef ARM_WINCE
2659 * magicp = ARMPEMAGIC;
2660#else
2661 * magicp = ARMMAGIC;
2662#endif
2663 * flagsp = 0;
2664 if (APCS_SET (abfd))
2665 {
2666 if (APCS_26_FLAG (abfd))
2667 * flagsp |= F_APCS26;
2668
2669 if (APCS_FLOAT_FLAG (abfd))
2670 * flagsp |= F_APCS_FLOAT;
2671
2672 if (PIC_FLAG (abfd))
2673 * flagsp |= F_PIC;
2674 }
2675 if (INTERWORK_SET (abfd) && INTERWORK_FLAG (abfd))
2676 * flagsp |= F_INTERWORK;
2677 switch (bfd_get_mach (abfd))
2678 {
2679 case bfd_mach_arm_2: * flagsp |= F_ARM_2; break;
2680 case bfd_mach_arm_2a: * flagsp |= F_ARM_2a; break;
2681 case bfd_mach_arm_3: * flagsp |= F_ARM_3; break;
2682 case bfd_mach_arm_3M: * flagsp |= F_ARM_3M; break;
2683 case bfd_mach_arm_4: * flagsp |= F_ARM_4; break;
2684 case bfd_mach_arm_4T: * flagsp |= F_ARM_4T; break;
2685 case bfd_mach_arm_5: * flagsp |= F_ARM_5; break;
2686 /* FIXME: we do not have F_ARM vaues greater than F_ARM_5.
2687 See also the comment in coff_set_arch_mach_hook(). */
2688 case bfd_mach_arm_5T: * flagsp |= F_ARM_5; break;
2689 case bfd_mach_arm_5TE: * flagsp |= F_ARM_5; break;
2690 case bfd_mach_arm_XScale: * flagsp |= F_ARM_5; break;
2691 }
2692 return TRUE;
2693#endif
2694#ifdef PPCMAGIC
2695 case bfd_arch_powerpc:
2696 *magicp = PPCMAGIC;
2697 return TRUE;
2698 break;
2699#endif
2700#ifdef I386MAGIC
2701 case bfd_arch_i386:
2702 *magicp = I386MAGIC;
2703#ifdef LYNXOS
2704 /* Just overwrite the usual value if we're doing Lynx. */
2705 *magicp = LYNXCOFFMAGIC;
2706#endif
2707 return TRUE;
2708 break;
2709#endif
2710#ifdef I860MAGIC
2711 case bfd_arch_i860:
2712 *magicp = I860MAGIC;
2713 return TRUE;
2714 break;
2715#endif
2716#ifdef IA64MAGIC
2717 case bfd_arch_ia64:
2718 *magicp = IA64MAGIC;
2719 return TRUE;
2720 break;
2721#endif
2722#ifdef MC68MAGIC
2723 case bfd_arch_m68k:
2724#ifdef APOLLOM68KMAGIC
2725 *magicp = APOLLO_COFF_VERSION_NUMBER;
2726#else
2727 /* NAMES_HAVE_UNDERSCORE may be defined by coff-u68k.c. */
2728#ifdef NAMES_HAVE_UNDERSCORE
2729 *magicp = MC68KBCSMAGIC;
2730#else
2731 *magicp = MC68MAGIC;
2732#endif
2733#endif
2734#ifdef LYNXOS
2735 /* Just overwrite the usual value if we're doing Lynx. */
2736 *magicp = LYNXCOFFMAGIC;
2737#endif
2738 return TRUE;
2739 break;
2740#endif
2741
2742#ifdef MC88MAGIC
2743 case bfd_arch_m88k:
2744 *magicp = MC88OMAGIC;
2745 return TRUE;
2746 break;
2747#endif
2748#ifdef H8300MAGIC
2749 case bfd_arch_h8300:
2750 switch (bfd_get_mach (abfd))
2751 {
2752 case bfd_mach_h8300:
2753 *magicp = H8300MAGIC;
2754 return TRUE;
2755 case bfd_mach_h8300h:
2756 *magicp = H8300HMAGIC;
2757 return TRUE;
2758 case bfd_mach_h8300s:
2759 *magicp = H8300SMAGIC;
2760 return TRUE;
2761 case bfd_mach_h8300hn:
2762 *magicp = H8300HNMAGIC;
2763 return TRUE;
2764 case bfd_mach_h8300sn:
2765 *magicp = H8300SNMAGIC;
2766 return TRUE;
2767 }
2768 break;
2769#endif
2770
2771#ifdef SH_ARCH_MAGIC_BIG
2772 case bfd_arch_sh:
2773#ifdef COFF_IMAGE_WITH_PE
2774 *magicp = SH_ARCH_MAGIC_WINCE;
2775#else
2776 if (bfd_big_endian (abfd))
2777 *magicp = SH_ARCH_MAGIC_BIG;
2778 else
2779 *magicp = SH_ARCH_MAGIC_LITTLE;
2780#endif
2781 return TRUE;
2782 break;
2783#endif
2784
2785#ifdef MIPS_ARCH_MAGIC_WINCE
2786 case bfd_arch_mips:
2787 *magicp = MIPS_ARCH_MAGIC_WINCE;
2788 return TRUE;
2789 break;
2790#endif
2791
2792#ifdef SPARCMAGIC
2793 case bfd_arch_sparc:
2794 *magicp = SPARCMAGIC;
2795#ifdef LYNXOS
2796 /* Just overwrite the usual value if we're doing Lynx. */
2797 *magicp = LYNXCOFFMAGIC;
2798#endif
2799 return TRUE;
2800 break;
2801#endif
2802
2803#ifdef H8500MAGIC
2804 case bfd_arch_h8500:
2805 *magicp = H8500MAGIC;
2806 return TRUE;
2807 break;
2808#endif
2809#ifdef A29K_MAGIC_BIG
2810 case bfd_arch_a29k:
2811 if (bfd_big_endian (abfd))
2812 *magicp = A29K_MAGIC_BIG;
2813 else
2814 *magicp = A29K_MAGIC_LITTLE;
2815 return TRUE;
2816 break;
2817#endif
2818
2819#ifdef WE32KMAGIC
2820 case bfd_arch_we32k:
2821 *magicp = WE32KMAGIC;
2822 return TRUE;
2823 break;
2824#endif
2825
2826#ifdef RS6000COFF_C
2827 case bfd_arch_rs6000:
2828#ifndef PPCMAGIC
2829 case bfd_arch_powerpc:
2830#endif
2831 BFD_ASSERT (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
2832 *magicp = bfd_xcoff_magic_number (abfd);
2833 return TRUE;
2834 break;
2835#endif
2836
2837#ifdef MCOREMAGIC
2838 case bfd_arch_mcore:
2839 * magicp = MCOREMAGIC;
2840 return TRUE;
2841#endif
2842
2843#ifdef W65MAGIC
2844 case bfd_arch_w65:
2845 *magicp = W65MAGIC;
2846 return TRUE;
2847#endif
2848
2849#ifdef OR32_MAGIC_BIG
2850 case bfd_arch_or32:
2851 if (bfd_big_endian (abfd))
2852 * magicp = OR32_MAGIC_BIG;
2853 else
2854 * magicp = OR32_MAGIC_LITTLE;
2855 return TRUE;
2856#endif
2857
2858 default: /* Unknown architecture. */
2859 /* Fall through to "return FALSE" below, to avoid
2860 "statement never reached" errors on the one below. */
2861 break;
2862 }
2863
2864 return FALSE;
2865}
2866
2867static bfd_boolean
2868coff_set_arch_mach (abfd, arch, machine)
2869 bfd * abfd;
2870 enum bfd_architecture arch;
2871 unsigned long machine;
2872{
2873 unsigned dummy1;
2874 unsigned short dummy2;
2875
2876 if (! bfd_default_set_arch_mach (abfd, arch, machine))
2877 return FALSE;
2878
2879 if (arch != bfd_arch_unknown
2880 && ! coff_set_flags (abfd, &dummy1, &dummy2))
2881 return FALSE; /* We can't represent this type */
2882
2883 return TRUE; /* We're easy ... */
2884}
2885
2886#ifdef COFF_IMAGE_WITH_PE
2887
2888/* This is used to sort sections by VMA, as required by PE image
2889 files. */
2890
2891static int sort_by_secaddr PARAMS ((const PTR, const PTR));
2892
2893static int
2894sort_by_secaddr (arg1, arg2)
2895 const PTR arg1;
2896 const PTR arg2;
2897{
2898 const asection *a = *(const asection **) arg1;
2899 const asection *b = *(const asection **) arg2;
2900
2901 if (a->vma < b->vma)
2902 return -1;
2903 else if (a->vma > b->vma)
2904 return 1;
2905 else
2906 return 0;
2907}
2908
2909#endif /* COFF_IMAGE_WITH_PE */
2910
2911/* Calculate the file position for each section. */
2912
2913#ifndef I960
2914#define ALIGN_SECTIONS_IN_FILE
2915#endif
2916#if defined(TIC80COFF) || defined(TICOFF)
2917#undef ALIGN_SECTIONS_IN_FILE
2918#endif
2919
2920static bfd_boolean
2921coff_compute_section_file_positions (abfd)
2922 bfd * abfd;
2923{
2924 asection *current;
2925 asection *previous = (asection *) NULL;
2926 file_ptr sofar = bfd_coff_filhsz (abfd);
2927 bfd_boolean align_adjust;
2928#ifdef ALIGN_SECTIONS_IN_FILE
2929 file_ptr old_sofar;
2930#endif
2931
2932#ifdef RS6000COFF_C
2933 /* On XCOFF, if we have symbols, set up the .debug section. */
2934 if (bfd_get_symcount (abfd) > 0)
2935 {
2936 bfd_size_type sz;
2937 bfd_size_type i, symcount;
2938 asymbol **symp;
2939
2940 sz = 0;
2941 symcount = bfd_get_symcount (abfd);
2942 for (symp = abfd->outsymbols, i = 0; i < symcount; symp++, i++)
2943 {
2944 coff_symbol_type *cf;
2945
2946 cf = coff_symbol_from (abfd, *symp);
2947 if (cf != NULL
2948 && cf->native != NULL
2949 && SYMNAME_IN_DEBUG (&cf->native->u.syment))
2950 {
2951 size_t len;
2952
2953 len = strlen (bfd_asymbol_name (*symp));
2954 if (len > SYMNMLEN || bfd_coff_force_symnames_in_strings (abfd))
2955 sz += len + 1 + bfd_coff_debug_string_prefix_length (abfd);
2956 }
2957 }
2958 if (sz > 0)
2959 {
2960 asection *dsec;
2961
2962 dsec = bfd_make_section_old_way (abfd, ".debug");
2963 if (dsec == NULL)
2964 abort ();
2965 dsec->_raw_size = sz;
2966 dsec->flags |= SEC_HAS_CONTENTS;
2967 }
2968 }
2969#endif
2970
2971#ifdef COFF_IMAGE_WITH_PE
2972 int page_size;
2973 if (coff_data (abfd)->link_info)
2974 {
2975 page_size = pe_data (abfd)->pe_opthdr.FileAlignment;
2976 }
2977 else
2978 page_size = PE_DEF_FILE_ALIGNMENT;
2979#else
2980#ifdef COFF_PAGE_SIZE
2981 int page_size = COFF_PAGE_SIZE;
2982#endif
2983#endif
2984
2985 if (bfd_get_start_address (abfd))
2986 {
2987 /* A start address may have been added to the original file. In this
2988 case it will need an optional header to record it. */
2989 abfd->flags |= EXEC_P;
2990 }
2991
2992 if (abfd->flags & EXEC_P)
2993 sofar += bfd_coff_aoutsz (abfd);
2994#ifdef RS6000COFF_C
2995 else if (xcoff_data (abfd)->full_aouthdr)
2996 sofar += bfd_coff_aoutsz (abfd);
2997 else
2998 sofar += SMALL_AOUTSZ;
2999#endif
3000
3001 sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
3002
3003#ifdef RS6000COFF_C
3004 /* XCOFF handles overflows in the reloc and line number count fields
3005 by allocating a new section header to hold the correct counts. */
3006 for (current = abfd->sections; current != NULL; current = current->next)
3007 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3008 sofar += bfd_coff_scnhsz (abfd);
3009#endif
3010
3011#ifdef COFF_IMAGE_WITH_PE
3012 {
3013 /* PE requires the sections to be in memory order when listed in
3014 the section headers. It also does not like empty loadable
3015 sections. The sections apparently do not have to be in the
3016 right order in the image file itself, but we do need to get the
3017 target_index values right. */
3018
3019 unsigned int count;
3020 asection **section_list;
3021 unsigned int i;
3022 int target_index;
3023 bfd_size_type amt;
3024
3025 count = 0;
3026 for (current = abfd->sections; current != NULL; current = current->next)
3027 ++count;
3028
3029 /* We allocate an extra cell to simplify the final loop. */
3030 amt = sizeof (struct asection *) * (count + 1);
3031 section_list = bfd_malloc (amt);
3032 if (section_list == NULL)
3033 return FALSE;
3034
3035 i = 0;
3036 for (current = abfd->sections; current != NULL; current = current->next)
3037 {
3038 section_list[i] = current;
3039 ++i;
3040 }
3041 section_list[i] = NULL;
3042
3043 qsort (section_list, count, sizeof (asection *), sort_by_secaddr);
3044
3045 /* Rethread the linked list into sorted order; at the same time,
3046 assign target_index values. */
3047 target_index = 1;
3048 abfd->sections = section_list[0];
3049 for (i = 0; i < count; i++)
3050 {
3051 current = section_list[i];
3052 current->next = section_list[i + 1];
3053
3054 /* Later, if the section has zero size, we'll be throwing it
3055 away, so we don't want to number it now. Note that having
3056 a zero size and having real contents are different
3057 concepts: .bss has no contents, but (usually) non-zero
3058 size. */
3059 if (current->_raw_size == 0)
3060 {
3061 /* Discard. However, it still might have (valid) symbols
3062 in it, so arbitrarily set it to section 1 (indexing is
3063 1-based here; usually .text). __end__ and other
3064 contents of .endsection really have this happen.
3065 FIXME: This seems somewhat dubious. */
3066 current->target_index = 1;
3067 }
3068 else
3069 current->target_index = target_index++;
3070 }
3071 abfd->section_tail = &current->next;
3072
3073 free (section_list);
3074 }
3075#else /* ! COFF_IMAGE_WITH_PE */
3076 {
3077 /* Set the target_index field. */
3078 int target_index;
3079
3080 target_index = 1;
3081 for (current = abfd->sections; current != NULL; current = current->next)
3082 current->target_index = target_index++;
3083 }
3084#endif /* ! COFF_IMAGE_WITH_PE */
3085
3086 align_adjust = FALSE;
3087 for (current = abfd->sections;
3088 current != (asection *) NULL;
3089 current = current->next)
3090 {
3091#ifdef COFF_IMAGE_WITH_PE
3092 /* With PE we have to pad each section to be a multiple of its
3093 page size too, and remember both sizes. */
3094 if (coff_section_data (abfd, current) == NULL)
3095 {
3096 bfd_size_type amt = sizeof (struct coff_section_tdata);
3097 current->used_by_bfd = (PTR) bfd_zalloc (abfd, amt);
3098 if (current->used_by_bfd == NULL)
3099 return FALSE;
3100 }
3101 if (pei_section_data (abfd, current) == NULL)
3102 {
3103 bfd_size_type amt = sizeof (struct pei_section_tdata);
3104 coff_section_data (abfd, current)->tdata
3105 = (PTR) bfd_zalloc (abfd, amt);
3106 if (coff_section_data (abfd, current)->tdata == NULL)
3107 return FALSE;
3108 }
3109 if (pei_section_data (abfd, current)->virt_size == 0)
3110 pei_section_data (abfd, current)->virt_size = current->_raw_size;
3111#endif
3112
3113 /* Only deal with sections which have contents. */
3114 if (!(current->flags & SEC_HAS_CONTENTS))
3115 continue;
3116
3117#ifdef COFF_IMAGE_WITH_PE
3118 /* Make sure we skip empty sections in a PE image. */
3119 if (current->_raw_size == 0)
3120 continue;
3121#endif
3122
3123 /* Align the sections in the file to the same boundary on
3124 which they are aligned in virtual memory. I960 doesn't
3125 do this (FIXME) so we can stay in sync with Intel. 960
3126 doesn't yet page from files... */
3127#ifdef ALIGN_SECTIONS_IN_FILE
3128 if ((abfd->flags & EXEC_P) != 0)
3129 {
3130 /* Make sure this section is aligned on the right boundary - by
3131 padding the previous section up if necessary. */
3132
3133 old_sofar = sofar;
3134#ifdef RS6000COFF_C
3135 /* AIX loader checks the text section alignment of (vma - filepos)
3136 So even though the filepos may be aligned wrt the o_algntext, for
3137 AIX executables, this check fails. This shows up when a native
3138 AIX executable is stripped with gnu strip because the default vma
3139 of native is 0x10000150 but default for gnu is 0x10000140. Gnu
3140 stripped gnu excutable passes this check because the filepos is
3141 0x0140. This problem also show up with 64 bit shared objects. The
3142 data section must also be aligned. */
3143 if (!strcmp (current->name, _TEXT)
3144 || !strcmp (current->name, _DATA))
3145 {
3146 bfd_vma pad;
3147 bfd_vma align;
3148
3149 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3150
3151 align = 1 << current->alignment_power;
3152 pad = abs (current->vma - sofar) % align;
3153
3154 if (pad)
3155 {
3156 pad = align - pad;
3157 sofar += pad;
3158 }
3159 }
3160 else
3161#else
3162 {
3163 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3164 }
3165#endif
3166 if (previous != (asection *) NULL)
3167 previous->_raw_size += sofar - old_sofar;
3168 }
3169
3170#endif
3171
3172 /* In demand paged files the low order bits of the file offset
3173 must match the low order bits of the virtual address. */
3174#ifdef COFF_PAGE_SIZE
3175 if ((abfd->flags & D_PAGED) != 0
3176 && (current->flags & SEC_ALLOC) != 0)
3177 sofar += (current->vma - sofar) % page_size;
3178#endif
3179 current->filepos = sofar;
3180
3181#ifdef COFF_IMAGE_WITH_PE
3182 /* Set the padded size. */
3183 current->_raw_size = (current->_raw_size + page_size -1) & -page_size;
3184#endif
3185
3186 sofar += current->_raw_size;
3187
3188#ifdef ALIGN_SECTIONS_IN_FILE
3189 /* Make sure that this section is of the right size too. */
3190 if ((abfd->flags & EXEC_P) == 0)
3191 {
3192 bfd_size_type old_size;
3193
3194 old_size = current->_raw_size;
3195 current->_raw_size = BFD_ALIGN (current->_raw_size,
3196 1 << current->alignment_power);
3197 align_adjust = current->_raw_size != old_size;
3198 sofar += current->_raw_size - old_size;
3199 }
3200 else
3201 {
3202 old_sofar = sofar;
3203 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3204 align_adjust = sofar != old_sofar;
3205 current->_raw_size += sofar - old_sofar;
3206 }
3207#endif
3208
3209#ifdef COFF_IMAGE_WITH_PE
3210 /* For PE we need to make sure we pad out to the aligned
3211 _raw_size, in case the caller only writes out data to the
3212 unaligned _raw_size. */
3213 if (pei_section_data (abfd, current)->virt_size < current->_raw_size)
3214 align_adjust = TRUE;
3215#endif
3216
3217#ifdef _LIB
3218 /* Force .lib sections to start at zero. The vma is then
3219 incremented in coff_set_section_contents. This is right for
3220 SVR3.2. */
3221 if (strcmp (current->name, _LIB) == 0)
3222 bfd_set_section_vma (abfd, current, 0);
3223#endif
3224
3225 previous = current;
3226 }
3227
3228 /* It is now safe to write to the output file. If we needed an
3229 alignment adjustment for the last section, then make sure that
3230 there is a byte at offset sofar. If there are no symbols and no
3231 relocs, then nothing follows the last section. If we don't force
3232 the last byte out, then the file may appear to be truncated. */
3233 if (align_adjust)
3234 {
3235 bfd_byte b;
3236
3237 b = 0;
3238 if (bfd_seek (abfd, sofar - 1, SEEK_SET) != 0
3239 || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
3240 return FALSE;
3241 }
3242
3243 /* Make sure the relocations are aligned. We don't need to make
3244 sure that this byte exists, because it will only matter if there
3245 really are relocs. */
3246 sofar = BFD_ALIGN (sofar, 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
3247
3248 obj_relocbase (abfd) = sofar;
3249 abfd->output_has_begun = TRUE;
3250
3251 return TRUE;
3252}
3253
3254#if 0
3255
3256/* This can never work, because it is called too late--after the
3257 section positions have been set. I can't figure out what it is
3258 for, so I am going to disable it--Ian Taylor 20 March 1996. */
3259
3260/* If .file, .text, .data, .bss symbols are missing, add them. */
3261/* @@ Should we only be adding missing symbols, or overriding the aux
3262 values for existing section symbols? */
3263static bfd_boolean
3264coff_add_missing_symbols (abfd)
3265 bfd *abfd;
3266{
3267 unsigned int nsyms = bfd_get_symcount (abfd);
3268 asymbol **sympp = abfd->outsymbols;
3269 asymbol **sympp2;
3270 unsigned int i;
3271 int need_text = 1, need_data = 1, need_bss = 1, need_file = 1;
3272 bfd_size_type amt;
3273
3274 for (i = 0; i < nsyms; i++)
3275 {
3276 coff_symbol_type *csym = coff_symbol_from (abfd, sympp[i]);
3277 const char *name;
3278
3279 if (csym)
3280 {
3281 /* Only do this if there is a coff representation of the input
3282 symbol. */
3283 if (csym->native && csym->native->u.syment.n_sclass == C_FILE)
3284 {
3285 need_file = 0;
3286 continue;
3287 }
3288 name = csym->symbol.name;
3289 if (!name)
3290 continue;
3291 if (!strcmp (name, _TEXT))
3292 need_text = 0;
3293#ifdef APOLLO_M68
3294 else if (!strcmp (name, ".wtext"))
3295 need_text = 0;
3296#endif
3297 else if (!strcmp (name, _DATA))
3298 need_data = 0;
3299 else if (!strcmp (name, _BSS))
3300 need_bss = 0;
3301 }
3302 }
3303 /* Now i == bfd_get_symcount (abfd). */
3304 /* @@ For now, don't deal with .file symbol. */
3305 need_file = 0;
3306
3307 if (!need_text && !need_data && !need_bss && !need_file)
3308 return TRUE;
3309 nsyms += need_text + need_data + need_bss + need_file;
3310 amt = nsyms;
3311 amt *= sizeof (asymbol *);
3312 sympp2 = (asymbol **) bfd_alloc (abfd, amt);
3313 if (!sympp2)
3314 return FALSE;
3315 memcpy (sympp2, sympp, i * sizeof (asymbol *));
3316
3317 if (need_file)
3318 /* @@ Generate fake .file symbol, in sympp2[i], and increment i. */
3319 abort ();
3320
3321 if (need_text)
3322 sympp2[i++] = coff_section_symbol (abfd, _TEXT);
3323 if (need_data)
3324 sympp2[i++] = coff_section_symbol (abfd, _DATA);
3325 if (need_bss)
3326 sympp2[i++] = coff_section_symbol (abfd, _BSS);
3327 BFD_ASSERT (i == nsyms);
3328 bfd_set_symtab (abfd, sympp2, nsyms);
3329 return TRUE;
3330}
3331
3332#endif /* 0 */
3333
3334#ifdef COFF_IMAGE_WITH_PE
3335
3336static unsigned int pelength;
3337static unsigned int peheader;
3338
3339static bfd_boolean
3340coff_read_word (abfd, value)
3341 bfd *abfd;
3342 unsigned int *value;
3343{
3344 unsigned char b[2];
3345 int status;
3346
3347 status = bfd_bread (b, (bfd_size_type) 2, abfd);
3348 if (status < 1)
3349 {
3350 *value = 0;
3351 return FALSE;
3352 }
3353
3354 if (status == 1)
3355 *value = (unsigned int) b[0];
3356 else
3357 *value = (unsigned int) (b[0] + (b[1] << 8));
3358
3359 pelength += (unsigned int) status;
3360
3361 return TRUE;
3362}
3363
3364static unsigned int
3365coff_compute_checksum (abfd)
3366 bfd *abfd;
3367{
3368 bfd_boolean more_data;
3369 file_ptr filepos;
3370 unsigned int value;
3371 unsigned int total;
3372
3373 total = 0;
3374 pelength = 0;
3375 filepos = (file_ptr) 0;
3376
3377 do
3378 {
3379 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
3380 return 0;
3381
3382 more_data = coff_read_word (abfd, &value);
3383 total += value;
3384 total = 0xffff & (total + (total >> 0x10));
3385 filepos += 2;
3386 }
3387 while (more_data);
3388
3389 return (0xffff & (total + (total >> 0x10)));
3390}
3391
3392static bfd_boolean
3393coff_apply_checksum (abfd)
3394 bfd *abfd;
3395{
3396 unsigned int computed;
3397 unsigned int checksum = 0;
3398
3399 if (bfd_seek (abfd, 0x3c, SEEK_SET) != 0)
3400 return FALSE;
3401
3402 if (!coff_read_word (abfd, &peheader))
3403 return FALSE;
3404
3405 if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
3406 return FALSE;
3407
3408 checksum = 0;
3409 bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
3410
3411 if (bfd_seek (abfd, peheader, SEEK_SET) != 0)
3412 return FALSE;
3413
3414 computed = coff_compute_checksum (abfd);
3415
3416 checksum = computed + pelength;
3417
3418 if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
3419 return FALSE;
3420
3421 bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
3422
3423 return TRUE;
3424}
3425
3426#endif /* COFF_IMAGE_WITH_PE */
3427
3428/* SUPPRESS 558 */
3429/* SUPPRESS 529 */
3430static bfd_boolean
3431coff_write_object_contents (abfd)
3432 bfd * abfd;
3433{
3434 asection *current;
3435 bfd_boolean hasrelocs = FALSE;
3436 bfd_boolean haslinno = FALSE;
3437 bfd_boolean hasdebug = FALSE;
3438 file_ptr scn_base;
3439 file_ptr reloc_base;
3440 file_ptr lineno_base;
3441 file_ptr sym_base;
3442 unsigned long reloc_size = 0, reloc_count = 0;
3443 unsigned long lnno_size = 0;
3444 bfd_boolean long_section_names;
3445 asection *text_sec = NULL;
3446 asection *data_sec = NULL;
3447 asection *bss_sec = NULL;
3448 struct internal_filehdr internal_f;
3449 struct internal_aouthdr internal_a;
3450#ifdef COFF_LONG_SECTION_NAMES
3451 size_t string_size = STRING_SIZE_SIZE;
3452#endif
3453
3454 bfd_set_error (bfd_error_system_call);
3455
3456 /* Make a pass through the symbol table to count line number entries and
3457 put them into the correct asections. */
3458
3459 lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
3460
3461 if (! abfd->output_has_begun)
3462 {
3463 if (! coff_compute_section_file_positions (abfd))
3464 return FALSE;
3465 }
3466
3467 reloc_base = obj_relocbase (abfd);
3468
3469 /* Work out the size of the reloc and linno areas. */
3470
3471 for (current = abfd->sections; current != NULL; current =
3472 current->next)
3473 {
3474#ifdef COFF_WITH_PE
3475 /* We store the actual reloc count in the first reloc's addr. */
3476 if (obj_pe (abfd) && current->reloc_count >= 0xffff)
3477 reloc_count ++;
3478#endif
3479 reloc_count += current->reloc_count;
3480 }
3481
3482 reloc_size = reloc_count * bfd_coff_relsz (abfd);
3483
3484 lineno_base = reloc_base + reloc_size;
3485 sym_base = lineno_base + lnno_size;
3486
3487 /* Indicate in each section->line_filepos its actual file address. */
3488 for (current = abfd->sections; current != NULL; current =
3489 current->next)
3490 {
3491 if (current->lineno_count)
3492 {
3493 current->line_filepos = lineno_base;
3494 current->moving_line_filepos = lineno_base;
3495 lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
3496 }
3497 else
3498 {
3499 current->line_filepos = 0;
3500 }
3501 if (current->reloc_count)
3502 {
3503 current->rel_filepos = reloc_base;
3504 reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
3505#ifdef COFF_WITH_PE
3506 /* Extra reloc to hold real count. */
3507 if (obj_pe (abfd) && current->reloc_count >= 0xffff)
3508 reloc_base += bfd_coff_relsz (abfd);
3509#endif
3510 }
3511 else
3512 {
3513 current->rel_filepos = 0;
3514 }
3515 }
3516
3517 /* Write section headers to the file. */
3518 internal_f.f_nscns = 0;
3519
3520 if ((abfd->flags & EXEC_P) != 0)
3521 scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
3522 else
3523 {
3524 scn_base = bfd_coff_filhsz (abfd);
3525#ifdef RS6000COFF_C
3526#ifndef XCOFF64
3527 if (xcoff_data (abfd)->full_aouthdr)
3528 scn_base += bfd_coff_aoutsz (abfd);
3529 else
3530 scn_base += SMALL_AOUTSZ;
3531#endif
3532#endif
3533 }
3534
3535 if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
3536 return FALSE;
3537
3538 long_section_names = FALSE;
3539 for (current = abfd->sections;
3540 current != NULL;
3541 current = current->next)
3542 {
3543 struct internal_scnhdr section;
3544 bfd_boolean is_reloc_section = FALSE;
3545
3546#ifdef COFF_IMAGE_WITH_PE
3547 if (strcmp (current->name, ".reloc") == 0)
3548 {
3549 is_reloc_section = TRUE;
3550 hasrelocs = TRUE;
3551 pe_data (abfd)->has_reloc_section = 1;
3552 }
3553#endif
3554
3555 internal_f.f_nscns++;
3556
3557 strncpy (section.s_name, current->name, SCNNMLEN);
3558
3559#ifdef COFF_LONG_SECTION_NAMES
3560 /* Handle long section names as in PE. This must be compatible
3561 with the code in coff_write_symbols and _bfd_coff_final_link. */
3562 {
3563 size_t len;
3564
3565 len = strlen (current->name);
3566 if (len > SCNNMLEN)
3567 {
3568 memset (section.s_name, 0, SCNNMLEN);
3569 sprintf (section.s_name, "/%lu", (unsigned long) string_size);
3570 string_size += len + 1;
3571 long_section_names = TRUE;
3572 }
3573 }
3574#endif
3575
3576#ifdef _LIB
3577 /* Always set s_vaddr of .lib to 0. This is right for SVR3.2
3578 Ian Taylor <ian@cygnus.com>. */
3579 if (strcmp (current->name, _LIB) == 0)
3580 section.s_vaddr = 0;
3581 else
3582#endif
3583 section.s_vaddr = current->vma;
3584 section.s_paddr = current->lma;
3585 section.s_size = current->_raw_size;
3586#ifdef coff_get_section_load_page
3587 section.s_page = coff_get_section_load_page (current);
3588#endif
3589
3590#ifdef COFF_WITH_PE
3591 section.s_paddr = 0;
3592#endif
3593#ifdef COFF_IMAGE_WITH_PE
3594 /* Reminder: s_paddr holds the virtual size of the section. */
3595 if (coff_section_data (abfd, current) != NULL
3596 && pei_section_data (abfd, current) != NULL)
3597 section.s_paddr = pei_section_data (abfd, current)->virt_size;
3598 else
3599 section.s_paddr = 0;
3600#endif
3601
3602 /* If this section has no size or is unloadable then the scnptr
3603 will be 0 too. */
3604 if (current->_raw_size == 0 ||
3605 (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3606 section.s_scnptr = 0;
3607 else
3608 section.s_scnptr = current->filepos;
3609
3610 section.s_relptr = current->rel_filepos;
3611 section.s_lnnoptr = current->line_filepos;
3612 section.s_nreloc = current->reloc_count;
3613 section.s_nlnno = current->lineno_count;
3614#ifndef COFF_IMAGE_WITH_PE
3615 /* In PEI, relocs come in the .reloc section. */
3616 if (current->reloc_count != 0)
3617 hasrelocs = TRUE;
3618#endif
3619 if (current->lineno_count != 0)
3620 haslinno = TRUE;
3621 if ((current->flags & SEC_DEBUGGING) != 0
3622 && ! is_reloc_section)
3623 hasdebug = TRUE;
3624
3625#ifdef RS6000COFF_C
3626#ifndef XCOFF64
3627 /* Indicate the use of an XCOFF overflow section header. */
3628 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3629 {
3630 section.s_nreloc = 0xffff;
3631 section.s_nlnno = 0xffff;
3632 }
3633#endif
3634#endif
3635
3636 section.s_flags = sec_to_styp_flags (current->name, current->flags);
3637
3638 if (!strcmp (current->name, _TEXT))
3639 text_sec = current;
3640 else if (!strcmp (current->name, _DATA))
3641 data_sec = current;
3642 else if (!strcmp (current->name, _BSS))
3643 bss_sec = current;
3644
3645#ifdef I960
3646 section.s_align = (current->alignment_power
3647 ? 1 << current->alignment_power
3648 : 0);
3649#endif
3650#ifdef TIC80COFF
3651 /* TI COFF puts the alignment power in bits 8-11 of the flags. */
3652 section.s_flags |= (current->alignment_power & 0xF) << 8;
3653#endif
3654#ifdef COFF_ENCODE_ALIGNMENT
3655 COFF_ENCODE_ALIGNMENT(section, current->alignment_power);
3656#endif
3657
3658#ifdef COFF_IMAGE_WITH_PE
3659 /* Suppress output of the sections if they are null. ld
3660 includes the bss and data sections even if there is no size
3661 assigned to them. NT loader doesn't like it if these section
3662 headers are included if the sections themselves are not
3663 needed. See also coff_compute_section_file_positions. */
3664 if (section.s_size == 0)
3665 internal_f.f_nscns--;
3666 else
3667#endif
3668 {
3669 SCNHDR buff;
3670 bfd_size_type amt = bfd_coff_scnhsz (abfd);
3671
3672 if (coff_swap_scnhdr_out (abfd, &section, &buff) == 0
3673 || bfd_bwrite ((PTR) &buff, amt, abfd) != amt)
3674 return FALSE;
3675 }
3676
3677#ifdef COFF_WITH_PE
3678 /* PE stores COMDAT section information in the symbol table. If
3679 this section is supposed to have some COMDAT info, track down
3680 the symbol in the symbol table and modify it. */
3681 if ((current->flags & SEC_LINK_ONCE) != 0)
3682 {
3683 unsigned int i, count;
3684 asymbol **psym;
3685 coff_symbol_type *csym = NULL;
3686 asymbol **psymsec;
3687
3688 psymsec = NULL;
3689 count = bfd_get_symcount (abfd);
3690 for (i = 0, psym = abfd->outsymbols; i < count; i++, psym++)
3691 {
3692 if ((*psym)->section != current)
3693 continue;
3694
3695 /* Remember the location of the first symbol in this
3696 section. */
3697 if (psymsec == NULL)
3698 psymsec = psym;
3699
3700 /* See if this is the section symbol. */
3701 if (strcmp ((*psym)->name, current->name) == 0)
3702 {
3703 csym = coff_symbol_from (abfd, *psym);
3704 if (csym == NULL
3705 || csym->native == NULL
3706 || csym->native->u.syment.n_numaux < 1
3707 || csym->native->u.syment.n_sclass != C_STAT
3708 || csym->native->u.syment.n_type != T_NULL)
3709 continue;
3710
3711 /* Here *PSYM is the section symbol for CURRENT. */
3712
3713 break;
3714 }
3715 }
3716
3717 /* Did we find it?
3718 Note that we might not if we're converting the file from
3719 some other object file format. */
3720 if (i < count)
3721 {
3722 combined_entry_type *aux;
3723
3724 /* We don't touch the x_checksum field. The
3725 x_associated field is not currently supported. */
3726
3727 aux = csym->native + 1;
3728 switch (current->flags & SEC_LINK_DUPLICATES)
3729 {
3730 case SEC_LINK_DUPLICATES_DISCARD:
3731 aux->u.auxent.x_scn.x_comdat = IMAGE_COMDAT_SELECT_ANY;
3732 break;
3733
3734 case SEC_LINK_DUPLICATES_ONE_ONLY:
3735 aux->u.auxent.x_scn.x_comdat =
3736 IMAGE_COMDAT_SELECT_NODUPLICATES;
3737 break;
3738
3739 case SEC_LINK_DUPLICATES_SAME_SIZE:
3740 aux->u.auxent.x_scn.x_comdat =
3741 IMAGE_COMDAT_SELECT_SAME_SIZE;
3742 break;
3743
3744 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
3745 aux->u.auxent.x_scn.x_comdat =
3746 IMAGE_COMDAT_SELECT_EXACT_MATCH;
3747 break;
3748 }
3749
3750 /* The COMDAT symbol must be the first symbol from this
3751 section in the symbol table. In order to make this
3752 work, we move the COMDAT symbol before the first
3753 symbol we found in the search above. It's OK to
3754 rearrange the symbol table at this point, because
3755 coff_renumber_symbols is going to rearrange it
3756 further and fix up all the aux entries. */
3757 if (psym != psymsec)
3758 {
3759 asymbol *hold;
3760 asymbol **pcopy;
3761
3762 hold = *psym;
3763 for (pcopy = psym; pcopy > psymsec; pcopy--)
3764 pcopy[0] = pcopy[-1];
3765 *psymsec = hold;
3766 }
3767 }
3768 }
3769#endif /* COFF_WITH_PE */
3770 }
3771
3772#ifdef RS6000COFF_C
3773#ifndef XCOFF64
3774 /* XCOFF handles overflows in the reloc and line number count fields
3775 by creating a new section header to hold the correct values. */
3776 for (current = abfd->sections; current != NULL; current = current->next)
3777 {
3778 if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
3779 {
3780 struct internal_scnhdr scnhdr;
3781 SCNHDR buff;
3782 bfd_size_type amt;
3783
3784 internal_f.f_nscns++;
3785 strncpy (&(scnhdr.s_name[0]), current->name, 8);
3786 scnhdr.s_paddr = current->reloc_count;
3787 scnhdr.s_vaddr = current->lineno_count;
3788 scnhdr.s_size = 0;
3789 scnhdr.s_scnptr = 0;
3790 scnhdr.s_relptr = current->rel_filepos;
3791 scnhdr.s_lnnoptr = current->line_filepos;
3792 scnhdr.s_nreloc = current->target_index;
3793 scnhdr.s_nlnno = current->target_index;
3794 scnhdr.s_flags = STYP_OVRFLO;
3795 amt = bfd_coff_scnhsz (abfd);
3796 if (coff_swap_scnhdr_out (abfd, &scnhdr, &buff) == 0
3797 || bfd_bwrite ((PTR) &buff, amt, abfd) != amt)
3798 return FALSE;
3799 }
3800 }
3801#endif
3802#endif
3803
3804 /* OK, now set up the filehdr... */
3805
3806 /* Don't include the internal abs section in the section count */
3807
3808 /* We will NOT put a fucking timestamp in the header here. Every time you
3809 put it back, I will come in and take it out again. I'm sorry. This
3810 field does not belong here. We fill it with a 0 so it compares the
3811 same but is not a reasonable time. -- gnu@cygnus.com */
3812 internal_f.f_timdat = 0;
3813 internal_f.f_flags = 0;
3814
3815 if (abfd->flags & EXEC_P)
3816 internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
3817 else
3818 {
3819 internal_f.f_opthdr = 0;
3820#ifdef RS6000COFF_C
3821#ifndef XCOFF64
3822 if (xcoff_data (abfd)->full_aouthdr)
3823 internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
3824 else
3825 internal_f.f_opthdr = SMALL_AOUTSZ;
3826#endif
3827#endif
3828 }
3829
3830 if (!hasrelocs)
3831 internal_f.f_flags |= F_RELFLG;
3832 if (!haslinno)
3833 internal_f.f_flags |= F_LNNO;
3834 if (abfd->flags & EXEC_P)
3835 internal_f.f_flags |= F_EXEC;
3836#ifdef COFF_IMAGE_WITH_PE
3837 if (! hasdebug)
3838 internal_f.f_flags |= IMAGE_FILE_DEBUG_STRIPPED;
3839#endif
3840
3841#ifndef COFF_WITH_PE
3842 if (bfd_little_endian (abfd))
3843 internal_f.f_flags |= F_AR32WR;
3844 else
3845 internal_f.f_flags |= F_AR32W;
3846#endif
3847
3848#ifdef TI_TARGET_ID
3849 /* Target id is used in TI COFF v1 and later; COFF0 won't use this field,
3850 but it doesn't hurt to set it internally. */
3851 internal_f.f_target_id = TI_TARGET_ID;
3852#endif
3853#ifdef TIC80_TARGET_ID
3854 internal_f.f_target_id = TIC80_TARGET_ID;
3855#endif
3856
3857 /* FIXME, should do something about the other byte orders and
3858 architectures. */
3859
3860#ifdef RS6000COFF_C
3861 if ((abfd->flags & DYNAMIC) != 0)
3862 internal_f.f_flags |= F_SHROBJ;
3863 if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
3864 internal_f.f_flags |= F_DYNLOAD;
3865#endif
3866
3867 memset (&internal_a, 0, sizeof internal_a);
3868
3869 /* Set up architecture-dependent stuff. */
3870 {
3871 unsigned int magic = 0;
3872 unsigned short flags = 0;
3873
3874 coff_set_flags (abfd, &magic, &flags);
3875 internal_f.f_magic = magic;
3876 internal_f.f_flags |= flags;
3877 /* ...and the "opt"hdr... */
3878
3879#ifdef A29K
3880#ifdef ULTRA3 /* NYU's machine */
3881 /* FIXME: This is a bogus check. I really want to see if there
3882 is a .shbss or a .shdata section, if so then set the magic
3883 number to indicate a shared data executable. */
3884 if (internal_f.f_nscns >= 7)
3885 internal_a.magic = SHMAGIC; /* Shared magic. */
3886 else
3887#endif /* ULTRA3 */
3888 internal_a.magic = NMAGIC; /* Assume separate i/d. */
3889#define __A_MAGIC_SET__
3890#endif /* A29K */
3891#ifdef TICOFF_AOUT_MAGIC
3892 internal_a.magic = TICOFF_AOUT_MAGIC;
3893#define __A_MAGIC_SET__
3894#endif
3895#ifdef TIC80COFF
3896 internal_a.magic = TIC80_ARCH_MAGIC;
3897#define __A_MAGIC_SET__
3898#endif /* TIC80 */
3899#ifdef I860
3900 /* FIXME: What are the a.out magic numbers for the i860? */
3901 internal_a.magic = 0;
3902#define __A_MAGIC_SET__
3903#endif /* I860 */
3904#ifdef I960
3905 internal_a.magic = (magic == I960ROMAGIC ? NMAGIC : OMAGIC);
3906#define __A_MAGIC_SET__
3907#endif /* I960 */
3908#if M88
3909#define __A_MAGIC_SET__
3910 internal_a.magic = PAGEMAGICBCS;
3911#endif /* M88 */
3912
3913#if APOLLO_M68
3914#define __A_MAGIC_SET__
3915 internal_a.magic = APOLLO_COFF_VERSION_NUMBER;
3916#endif
3917
3918#if defined(M68) || defined(WE32K) || defined(M68K)
3919#define __A_MAGIC_SET__
3920#if defined(LYNXOS)
3921 internal_a.magic = LYNXCOFFMAGIC;
3922#else
3923#if defined(TARG_AUX)
3924 internal_a.magic = (abfd->flags & D_PAGED ? PAGEMAGICPEXECPAGED :
3925 abfd->flags & WP_TEXT ? PAGEMAGICPEXECSWAPPED :
3926 PAGEMAGICEXECSWAPPED);
3927#else
3928#if defined (PAGEMAGICPEXECPAGED)
3929 internal_a.magic = PAGEMAGICPEXECPAGED;
3930#endif
3931#endif /* TARG_AUX */
3932#endif /* LYNXOS */
3933#endif /* M68 || WE32K || M68K */
3934
3935#if defined(ARM)
3936#define __A_MAGIC_SET__
3937 internal_a.magic = ZMAGIC;
3938#endif
3939
3940#if defined(PPC_PE)
3941#define __A_MAGIC_SET__
3942 internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
3943#endif
3944
3945#if defined MCORE_PE
3946#define __A_MAGIC_SET__
3947 internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
3948#endif
3949
3950#if defined(I386)
3951#define __A_MAGIC_SET__
3952#if defined(LYNXOS)
3953 internal_a.magic = LYNXCOFFMAGIC;
3954#else /* LYNXOS */
3955 internal_a.magic = ZMAGIC;
3956#endif /* LYNXOS */
3957#endif /* I386 */
3958
3959#if defined(IA64)
3960#define __A_MAGIC_SET__
3961 internal_a.magic = ZMAGIC;
3962#endif /* IA64 */
3963
3964#if defined(SPARC)
3965#define __A_MAGIC_SET__
3966#if defined(LYNXOS)
3967 internal_a.magic = LYNXCOFFMAGIC;
3968#endif /* LYNXOS */
3969#endif /* SPARC */
3970
3971#ifdef RS6000COFF_C
3972#define __A_MAGIC_SET__
3973 internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
3974 (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
3975 RS6K_AOUTHDR_OMAGIC;
3976#endif
3977
3978#if defined(SH) && defined(COFF_WITH_PE)
3979#define __A_MAGIC_SET__
3980 internal_a.magic = SH_PE_MAGIC;
3981#endif
3982
3983#if defined(MIPS) && defined(COFF_WITH_PE)
3984#define __A_MAGIC_SET__
3985 internal_a.magic = MIPS_PE_MAGIC;
3986#endif
3987
3988#ifdef OR32
3989#define __A_MAGIC_SET__
3990 internal_a.magic = NMAGIC; /* Assume separate i/d. */
3991#endif
3992
3993#ifndef __A_MAGIC_SET__
3994#include "Your aouthdr magic number is not being set!"
3995#else
3996#undef __A_MAGIC_SET__
3997#endif
3998 }
3999
4000 /* FIXME: Does anybody ever set this to another value? */
4001 internal_a.vstamp = 0;
4002
4003 /* Now should write relocs, strings, syms. */
4004 obj_sym_filepos (abfd) = sym_base;
4005
4006 if (bfd_get_symcount (abfd) != 0)
4007 {
4008 int firstundef;
4009#if 0
4010 if (!coff_add_missing_symbols (abfd))
4011 return FALSE;
4012#endif
4013 if (!coff_renumber_symbols (abfd, &firstundef))
4014 return FALSE;
4015 coff_mangle_symbols (abfd);
4016 if (! coff_write_symbols (abfd))
4017 return FALSE;
4018 if (! coff_write_linenumbers (abfd))
4019 return FALSE;
4020 if (! coff_write_relocs (abfd, firstundef))
4021 return FALSE;
4022 }
4023#ifdef COFF_LONG_SECTION_NAMES
4024 else if (long_section_names && ! obj_coff_strings_written (abfd))
4025 {
4026 /* If we have long section names we have to write out the string
4027 table even if there are no symbols. */
4028 if (! coff_write_symbols (abfd))
4029 return FALSE;
4030 }
4031#endif
4032#ifdef COFF_IMAGE_WITH_PE
4033#ifdef PPC_PE
4034 else if ((abfd->flags & EXEC_P) != 0)
4035 {
4036 bfd_byte b;
4037
4038 /* PowerPC PE appears to require that all executable files be
4039 rounded up to the page size. */
4040 b = 0;
4041 if (bfd_seek (abfd,
4042 (file_ptr) BFD_ALIGN (sym_base, COFF_PAGE_SIZE) - 1,
4043 SEEK_SET) != 0
4044 || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
4045 return FALSE;
4046 }
4047#endif
4048#endif
4049
4050 /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
4051 backend linker, and obj_raw_syment_count is not valid until after
4052 coff_write_symbols is called. */
4053 if (obj_raw_syment_count (abfd) != 0)
4054 {
4055 internal_f.f_symptr = sym_base;
4056#ifdef RS6000COFF_C
4057 /* AIX appears to require that F_RELFLG not be set if there are
4058 local symbols but no relocations. */
4059 internal_f.f_flags &=~ F_RELFLG;
4060#endif
4061 }
4062 else
4063 {
4064 if (long_section_names)
4065 internal_f.f_symptr = sym_base;
4066 else
4067 internal_f.f_symptr = 0;
4068 internal_f.f_flags |= F_LSYMS;
4069 }
4070
4071 if (text_sec)
4072 {
4073 internal_a.tsize = bfd_get_section_size_before_reloc (text_sec);
4074 internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
4075 }
4076 if (data_sec)
4077 {
4078 internal_a.dsize = bfd_get_section_size_before_reloc (data_sec);
4079 internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
4080 }
4081 if (bss_sec)
4082 {
4083 internal_a.bsize = bfd_get_section_size_before_reloc (bss_sec);
4084 if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
4085 internal_a.data_start = bss_sec->vma;
4086 }
4087
4088 internal_a.entry = bfd_get_start_address (abfd);
4089 internal_f.f_nsyms = obj_raw_syment_count (abfd);
4090
4091#ifdef RS6000COFF_C
4092 if (xcoff_data (abfd)->full_aouthdr)
4093 {
4094 bfd_vma toc;
4095 asection *loader_sec;
4096
4097 internal_a.vstamp = 1;
4098
4099 internal_a.o_snentry = xcoff_data (abfd)->snentry;
4100 if (internal_a.o_snentry == 0)
4101 internal_a.entry = (bfd_vma) -1;
4102
4103 if (text_sec != NULL)
4104 {
4105 internal_a.o_sntext = text_sec->target_index;
4106 internal_a.o_algntext = bfd_get_section_alignment (abfd, text_sec);
4107 }
4108 else
4109 {
4110 internal_a.o_sntext = 0;
4111 internal_a.o_algntext = 0;
4112 }
4113 if (data_sec != NULL)
4114 {
4115 internal_a.o_sndata = data_sec->target_index;
4116 internal_a.o_algndata = bfd_get_section_alignment (abfd, data_sec);
4117 }
4118 else
4119 {
4120 internal_a.o_sndata = 0;
4121 internal_a.o_algndata = 0;
4122 }
4123 loader_sec = bfd_get_section_by_name (abfd, ".loader");
4124 if (loader_sec != NULL)
4125 internal_a.o_snloader = loader_sec->target_index;
4126 else
4127 internal_a.o_snloader = 0;
4128 if (bss_sec != NULL)
4129 internal_a.o_snbss = bss_sec->target_index;
4130 else
4131 internal_a.o_snbss = 0;
4132
4133 toc = xcoff_data (abfd)->toc;
4134 internal_a.o_toc = toc;
4135 internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
4136
4137 internal_a.o_modtype = xcoff_data (abfd)->modtype;
4138 if (xcoff_data (abfd)->cputype != -1)
4139 internal_a.o_cputype = xcoff_data (abfd)->cputype;
4140 else
4141 {
4142 switch (bfd_get_arch (abfd))
4143 {
4144 case bfd_arch_rs6000:
4145 internal_a.o_cputype = 4;
4146 break;
4147 case bfd_arch_powerpc:
4148 if (bfd_get_mach (abfd) == bfd_mach_ppc)
4149 internal_a.o_cputype = 3;
4150 else
4151 internal_a.o_cputype = 1;
4152 break;
4153 default:
4154 abort ();
4155 }
4156 }
4157 internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
4158 internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
4159 }
4160#endif
4161
4162 /* now write them */
4163 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
4164 return FALSE;
4165
4166 {
4167 char * buff;
4168 bfd_size_type amount = bfd_coff_filhsz (abfd);
4169
4170 buff = bfd_malloc (amount);
4171 if (buff == NULL)
4172 return FALSE;
4173
4174 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) buff);
4175 amount = bfd_bwrite ((PTR) buff, amount, abfd);
4176
4177 free (buff);
4178
4179 if (amount != bfd_coff_filhsz (abfd))
4180 return FALSE;
4181 }
4182
4183 if (abfd->flags & EXEC_P)
4184 {
4185 /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
4186 include/coff/pe.h sets AOUTSZ == sizeof (PEAOUTHDR)). */
4187 char * buff;
4188 bfd_size_type amount = bfd_coff_aoutsz (abfd);
4189
4190 buff = bfd_malloc (amount);
4191 if (buff == NULL)
4192 return FALSE;
4193
4194 coff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) buff);
4195 amount = bfd_bwrite ((PTR) buff, amount, abfd);
4196
4197 free (buff);
4198
4199 if (amount != bfd_coff_aoutsz (abfd))
4200 return FALSE;
4201
4202#ifdef COFF_IMAGE_WITH_PE
4203 if (! coff_apply_checksum (abfd))
4204 return FALSE;
4205#endif
4206 }
4207#ifdef RS6000COFF_C
4208 else
4209 {
4210 AOUTHDR buff;
4211 size_t size;
4212
4213 /* XCOFF seems to always write at least a small a.out header. */
4214 coff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
4215 if (xcoff_data (abfd)->full_aouthdr)
4216 size = bfd_coff_aoutsz (abfd);
4217 else
4218 size = SMALL_AOUTSZ;
4219 if (bfd_bwrite ((PTR) &buff, (bfd_size_type) size, abfd) != size)
4220 return FALSE;
4221 }
4222#endif
4223
4224 return TRUE;
4225}
4226
4227static bfd_boolean
4228coff_set_section_contents (abfd, section, location, offset, count)
4229 bfd * abfd;
4230 sec_ptr section;
4231 PTR location;
4232 file_ptr offset;
4233 bfd_size_type count;
4234{
4235 if (! abfd->output_has_begun) /* Set by bfd.c handler. */
4236 {
4237 if (! coff_compute_section_file_positions (abfd))
4238 return FALSE;
4239 }
4240
4241#if defined(_LIB) && !defined(TARG_AUX)
4242
4243 /* The physical address field of a .lib section is used to hold the
4244 number of shared libraries in the section. This code counts the
4245 number of sections being written, and increments the lma field
4246 with the number.
4247
4248 I have found no documentation on the contents of this section.
4249 Experimentation indicates that the section contains zero or more
4250 records, each of which has the following structure:
4251
4252 - a (four byte) word holding the length of this record, in words,
4253 - a word that always seems to be set to "2",
4254 - the path to a shared library, null-terminated and then padded
4255 to a whole word boundary.
4256
4257 bfd_assert calls have been added to alert if an attempt is made
4258 to write a section which doesn't follow these assumptions. The
4259 code has been tested on ISC 4.1 by me, and on SCO by Robert Lipe
4260 <robertl@arnet.com> (Thanks!).
4261
4262 Gvran Uddeborg <gvran@uddeborg.pp.se>. */
4263
4264 if (strcmp (section->name, _LIB) == 0)
4265 {
4266 bfd_byte *rec, *recend;
4267
4268 rec = (bfd_byte *) location;
4269 recend = rec + count;
4270 while (rec < recend)
4271 {
4272 ++section->lma;
4273 rec += bfd_get_32 (abfd, rec) * 4;
4274 }
4275
4276 BFD_ASSERT (rec == recend);
4277 }
4278
4279#endif
4280
4281 /* Don't write out bss sections - one way to do this is to
4282 see if the filepos has not been set. */
4283 if (section->filepos == 0)
4284 return TRUE;
4285
4286 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
4287 return FALSE;
4288
4289 if (count == 0)
4290 return TRUE;
4291
4292 return bfd_bwrite (location, count, abfd) == count;
4293}
4294#if 0
4295static bfd_boolean
4296coff_close_and_cleanup (abfd)
4297 bfd *abfd;
4298{
4299 if (!bfd_read_p (abfd))
4300 switch (abfd->format)
4301 {
4302 case bfd_archive:
4303 if (!_bfd_write_archive_contents (abfd))
4304 return FALSE;
4305 break;
4306 case bfd_object:
4307 if (!coff_write_object_contents (abfd))
4308 return FALSE;
4309 break;
4310 default:
4311 bfd_set_error (bfd_error_invalid_operation);
4312 return FALSE;
4313 }
4314
4315 /* We depend on bfd_close to free all the memory on the objalloc. */
4316 return TRUE;
4317}
4318
4319#endif
4320
4321static PTR
4322buy_and_read (abfd, where, size)
4323 bfd *abfd;
4324 file_ptr where;
4325 bfd_size_type size;
4326{
4327 PTR area = (PTR) bfd_alloc (abfd, size);
4328 if (!area)
4329 return (NULL);
4330 if (bfd_seek (abfd, where, SEEK_SET) != 0
4331 || bfd_bread (area, size, abfd) != size)
4332 return (NULL);
4333 return (area);
4334} /* buy_and_read() */
4335
4336/*
4337SUBSUBSECTION
4338 Reading linenumbers
4339
4340 Creating the linenumber table is done by reading in the entire
4341 coff linenumber table, and creating another table for internal use.
4342
4343 A coff linenumber table is structured so that each function
4344 is marked as having a line number of 0. Each line within the
4345 function is an offset from the first line in the function. The
4346 base of the line number information for the table is stored in
4347 the symbol associated with the function.
4348
4349 Note: The PE format uses line number 0 for a flag indicating a
4350 new source file.
4351
4352 The information is copied from the external to the internal
4353 table, and each symbol which marks a function is marked by
4354 pointing its...
4355
4356 How does this work ?
4357
4358*/
4359
4360static bfd_boolean
4361coff_slurp_line_table (abfd, asect)
4362 bfd *abfd;
4363 asection *asect;
4364{
4365 LINENO *native_lineno;
4366 alent *lineno_cache;
4367 bfd_size_type amt;
4368
4369 BFD_ASSERT (asect->lineno == (alent *) NULL);
4370
4371 amt = (bfd_size_type) bfd_coff_linesz (abfd) * asect->lineno_count;
4372 native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos, amt);
4373 amt = ((bfd_size_type) asect->lineno_count + 1) * sizeof (alent);
4374 lineno_cache = (alent *) bfd_alloc (abfd, amt);
4375 if (lineno_cache == NULL)
4376 return FALSE;
4377 else
4378 {
4379 unsigned int counter = 0;
4380 alent *cache_ptr = lineno_cache;
4381 LINENO *src = native_lineno;
4382
4383 while (counter < asect->lineno_count)
4384 {
4385 struct internal_lineno dst;
4386
4387 bfd_coff_swap_lineno_in (abfd, src, &dst);
4388 cache_ptr->line_number = dst.l_lnno;
4389
4390 if (cache_ptr->line_number == 0)
4391 {
4392 bfd_boolean warned;
4393 bfd_signed_vma symndx;
4394 coff_symbol_type *sym;
4395
4396 warned = FALSE;
4397 symndx = dst.l_addr.l_symndx;
4398 if (symndx < 0
4399 || (bfd_vma) symndx >= obj_raw_syment_count (abfd))
4400 {
4401 (*_bfd_error_handler)
4402 (_("%s: warning: illegal symbol index %ld in line numbers"),
4403 bfd_archive_filename (abfd), dst.l_addr.l_symndx);
4404 symndx = 0;
4405 warned = TRUE;
4406 }
4407 /* FIXME: We should not be casting between ints and
4408 pointers like this. */
4409 sym = ((coff_symbol_type *)
4410 ((symndx + obj_raw_syments (abfd))
4411 ->u.syment._n._n_n._n_zeroes));
4412 cache_ptr->u.sym = (asymbol *) sym;
4413 if (sym->lineno != NULL && ! warned)
4414 {
4415 (*_bfd_error_handler)
4416 (_("%s: warning: duplicate line number information for `%s'"),
4417 bfd_archive_filename (abfd),
4418 bfd_asymbol_name (&sym->symbol));
4419 }
4420 sym->lineno = cache_ptr;
4421 }
4422 else
4423 {
4424 cache_ptr->u.offset = dst.l_addr.l_paddr
4425 - bfd_section_vma (abfd, asect);
4426 } /* If no linenumber expect a symbol index */
4427
4428 cache_ptr++;
4429 src++;
4430 counter++;
4431 }
4432 cache_ptr->line_number = 0;
4433
4434 }
4435 asect->lineno = lineno_cache;
4436 /* FIXME, free native_lineno here, or use alloca or something. */
4437 return TRUE;
4438}
4439
4440/* Slurp in the symbol table, converting it to generic form. Note
4441 that if coff_relocate_section is defined, the linker will read
4442 symbols via coff_link_add_symbols, rather than via this routine. */
4443
4444static bfd_boolean
4445coff_slurp_symbol_table (abfd)
4446 bfd * abfd;
4447{
4448 combined_entry_type *native_symbols;
4449 coff_symbol_type *cached_area;
4450 unsigned int *table_ptr;
4451 bfd_size_type amt;
4452 unsigned int number_of_symbols = 0;
4453
4454 if (obj_symbols (abfd))
4455 return TRUE;
4456
4457 /* Read in the symbol table. */
4458 if ((native_symbols = coff_get_normalized_symtab (abfd)) == NULL)
4459 return FALSE;
4460
4461 /* Allocate enough room for all the symbols in cached form. */
4462 amt = obj_raw_syment_count (abfd);
4463 amt *= sizeof (coff_symbol_type);
4464 cached_area = (coff_symbol_type *) bfd_alloc (abfd, amt);
4465 if (cached_area == NULL)
4466 return FALSE;
4467
4468 amt = obj_raw_syment_count (abfd);
4469 amt *= sizeof (unsigned int);
4470 table_ptr = (unsigned int *) bfd_alloc (abfd, amt);
4471
4472 if (table_ptr == NULL)
4473 return FALSE;
4474 else
4475 {
4476 coff_symbol_type *dst = cached_area;
4477 unsigned int last_native_index = obj_raw_syment_count (abfd);
4478 unsigned int this_index = 0;
4479
4480 while (this_index < last_native_index)
4481 {
4482 combined_entry_type *src = native_symbols + this_index;
4483 table_ptr[this_index] = number_of_symbols;
4484 dst->symbol.the_bfd = abfd;
4485
4486 dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
4487 /* We use the native name field to point to the cached field. */
4488 src->u.syment._n._n_n._n_zeroes = (long) dst;
4489 dst->symbol.section = coff_section_from_bfd_index (abfd,
4490 src->u.syment.n_scnum);
4491 dst->symbol.flags = 0;
4492 dst->done_lineno = FALSE;
4493
4494 switch (src->u.syment.n_sclass)
4495 {
4496#ifdef I960
4497 case C_LEAFEXT:
4498#if 0
4499 dst->symbol.value = src->u.syment.n_value - dst->symbol.section->vma;
4500 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
4501 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4502#endif
4503 /* Fall through to next case. */
4504#endif
4505
4506 case C_EXT:
4507 case C_WEAKEXT:
4508#if defined ARM
4509 case C_THUMBEXT:
4510 case C_THUMBEXTFUNC:
4511#endif
4512#ifdef RS6000COFF_C
4513 case C_HIDEXT:
4514#endif
4515#ifdef C_SYSTEM
4516 case C_SYSTEM: /* System Wide variable. */
4517#endif
4518#ifdef COFF_WITH_PE
4519 /* In PE, 0x68 (104) denotes a section symbol. */
4520 case C_SECTION:
4521 /* In PE, 0x69 (105) denotes a weak external symbol. */
4522 case C_NT_WEAK:
4523#endif
4524 switch (coff_classify_symbol (abfd, &src->u.syment))
4525 {
4526 case COFF_SYMBOL_GLOBAL:
4527 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
4528#if defined COFF_WITH_PE
4529 /* PE sets the symbol to a value relative to the
4530 start of the section. */
4531 dst->symbol.value = src->u.syment.n_value;
4532#else
4533 dst->symbol.value = (src->u.syment.n_value
4534 - dst->symbol.section->vma);
4535#endif
4536 if (ISFCN ((src->u.syment.n_type)))
4537 {
4538 /* A function ext does not go at the end of a
4539 file. */
4540 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4541 }
4542 break;
4543
4544 case COFF_SYMBOL_COMMON:
4545 dst->symbol.section = bfd_com_section_ptr;
4546 dst->symbol.value = src->u.syment.n_value;
4547 break;
4548
4549 case COFF_SYMBOL_UNDEFINED:
4550 dst->symbol.section = bfd_und_section_ptr;
4551 dst->symbol.value = 0;
4552 break;
4553
4554 case COFF_SYMBOL_PE_SECTION:
4555 dst->symbol.flags |= BSF_EXPORT | BSF_SECTION_SYM;
4556 dst->symbol.value = 0;
4557 break;
4558
4559 case COFF_SYMBOL_LOCAL:
4560 dst->symbol.flags = BSF_LOCAL;
4561#if defined COFF_WITH_PE
4562 /* PE sets the symbol to a value relative to the
4563 start of the section. */
4564 dst->symbol.value = src->u.syment.n_value;
4565#else
4566 dst->symbol.value = (src->u.syment.n_value
4567 - dst->symbol.section->vma);
4568#endif
4569 if (ISFCN ((src->u.syment.n_type)))
4570 dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
4571 break;
4572 }
4573
4574#ifdef RS6000COFF_C
4575 /* A symbol with a csect entry should not go at the end. */
4576 if (src->u.syment.n_numaux > 0)
4577 dst->symbol.flags |= BSF_NOT_AT_END;
4578#endif
4579
4580#ifdef COFF_WITH_PE
4581 if (src->u.syment.n_sclass == C_NT_WEAK)
4582 dst->symbol.flags |= BSF_WEAK;
4583
4584 if (src->u.syment.n_sclass == C_SECTION
4585 && src->u.syment.n_scnum > 0)
4586 dst->symbol.flags = BSF_LOCAL;
4587#endif
4588 if (src->u.syment.n_sclass == C_WEAKEXT)
4589 dst->symbol.flags |= BSF_WEAK;
4590
4591 break;
4592
4593 case C_STAT: /* Static. */
4594#ifdef I960
4595 case C_LEAFSTAT: /* Static leaf procedure. */
4596#endif
4597#if defined ARM
4598 case C_THUMBSTAT: /* Thumb static. */
4599 case C_THUMBLABEL: /* Thumb label. */
4600 case C_THUMBSTATFUNC:/* Thumb static function. */
4601#endif
4602 case C_LABEL: /* Label. */
4603 if (src->u.syment.n_scnum == N_DEBUG)
4604 dst->symbol.flags = BSF_DEBUGGING;
4605 else
4606 dst->symbol.flags = BSF_LOCAL;
4607
4608 /* Base the value as an index from the base of the
4609 section, if there is one. */
4610 if (dst->symbol.section)
4611 {
4612#if defined COFF_WITH_PE
4613 /* PE sets the symbol to a value relative to the
4614 start of the section. */
4615 dst->symbol.value = src->u.syment.n_value;
4616#else
4617 dst->symbol.value = (src->u.syment.n_value
4618 - dst->symbol.section->vma);
4619#endif
4620 }
4621 else
4622 dst->symbol.value = src->u.syment.n_value;
4623 break;
4624
4625 case C_MOS: /* Member of structure. */
4626 case C_EOS: /* End of structure. */
4627#ifdef NOTDEF /* C_AUTOARG has the same value. */
4628#ifdef C_GLBLREG
4629 case C_GLBLREG: /* A29k-specific storage class. */
4630#endif
4631#endif
4632 case C_REGPARM: /* Register parameter. */
4633 case C_REG: /* register variable. */
4634 /* C_AUTOARG conflictes with TI COFF C_UEXT. */
4635#if !defined (TIC80COFF) && !defined (TICOFF)
4636#ifdef C_AUTOARG
4637 case C_AUTOARG: /* 960-specific storage class. */
4638#endif
4639#endif
4640 case C_TPDEF: /* Type definition. */
4641 case C_ARG:
4642 case C_AUTO: /* Automatic variable. */
4643 case C_FIELD: /* Bit field. */
4644 case C_ENTAG: /* Enumeration tag. */
4645 case C_MOE: /* Member of enumeration. */
4646 case C_MOU: /* Member of union. */
4647 case C_UNTAG: /* Union tag. */
4648 dst->symbol.flags = BSF_DEBUGGING;
4649 dst->symbol.value = (src->u.syment.n_value);
4650 break;
4651
4652 case C_FILE: /* File name. */
4653 case C_STRTAG: /* Structure tag. */
4654#ifdef RS6000COFF_C
4655 case C_GSYM:
4656 case C_LSYM:
4657 case C_PSYM:
4658 case C_RSYM:
4659 case C_RPSYM:
4660 case C_STSYM:
4661 case C_BCOMM:
4662 case C_ECOMM:
4663 case C_DECL:
4664 case C_ENTRY:
4665 case C_FUN:
4666 case C_ESTAT:
4667#endif
4668 dst->symbol.flags = BSF_DEBUGGING;
4669 dst->symbol.value = (src->u.syment.n_value);
4670 break;
4671
4672#ifdef RS6000COFF_C
4673 case C_BINCL: /* Beginning of include file. */
4674 case C_EINCL: /* Ending of include file. */
4675 /* The value is actually a pointer into the line numbers
4676 of the file. We locate the line number entry, and
4677 set the section to the section which contains it, and
4678 the value to the index in that section. */
4679 {
4680 asection *sec;
4681
4682 dst->symbol.flags = BSF_DEBUGGING;
4683 for (sec = abfd->sections; sec != NULL; sec = sec->next)
4684 if (sec->line_filepos <= (file_ptr) src->u.syment.n_value
4685 && ((file_ptr) (sec->line_filepos
4686 + sec->lineno_count * bfd_coff_linesz (abfd))
4687 > (file_ptr) src->u.syment.n_value))
4688 break;
4689 if (sec == NULL)
4690 dst->symbol.value = 0;
4691 else
4692 {
4693 dst->symbol.section = sec;
4694 dst->symbol.value = ((src->u.syment.n_value
4695 - sec->line_filepos)
4696 / bfd_coff_linesz (abfd));
4697 src->fix_line = 1;
4698 }
4699 }
4700 break;
4701
4702 case C_BSTAT:
4703 dst->symbol.flags = BSF_DEBUGGING;
4704
4705 /* The value is actually a symbol index. Save a pointer
4706 to the symbol instead of the index. FIXME: This
4707 should use a union. */
4708 src->u.syment.n_value =
4709 (long) (native_symbols + src->u.syment.n_value);
4710 dst->symbol.value = src->u.syment.n_value;
4711 src->fix_value = 1;
4712 break;
4713#endif
4714
4715 case C_BLOCK: /* ".bb" or ".eb". */
4716 case C_FCN: /* ".bf" or ".ef" (or PE ".lf"). */
4717 case C_EFCN: /* Physical end of function. */
4718#if defined COFF_WITH_PE
4719 /* PE sets the symbol to a value relative to the start
4720 of the section. */
4721 dst->symbol.value = src->u.syment.n_value;
4722 if (strcmp (dst->symbol.name, ".bf") != 0)
4723 {
4724 /* PE uses funny values for .ef and .lf; don't
4725 relocate them. */
4726 dst->symbol.flags = BSF_DEBUGGING;
4727 }
4728 else
4729 dst->symbol.flags = BSF_DEBUGGING | BSF_DEBUGGING_RELOC;
4730#else
4731 /* Base the value as an index from the base of the
4732 section. */
4733 dst->symbol.flags = BSF_LOCAL;
4734 dst->symbol.value = (src->u.syment.n_value
4735 - dst->symbol.section->vma);
4736#endif
4737 break;
4738
4739 case C_STATLAB: /* Static load time label. */
4740 dst->symbol.value = src->u.syment.n_value;
4741 dst->symbol.flags = BSF_GLOBAL;
4742 break;
4743
4744 case C_NULL:
4745 /* PE DLLs sometimes have zeroed out symbols for some
4746 reason. Just ignore them without a warning. */
4747 if (src->u.syment.n_type == 0
4748 && src->u.syment.n_value == 0
4749 && src->u.syment.n_scnum == 0)
4750 break;
4751 /* Fall through. */
4752 case C_EXTDEF: /* External definition. */
4753 case C_ULABEL: /* Undefined label. */
4754 case C_USTATIC: /* Undefined static. */
4755#ifndef COFF_WITH_PE
4756 /* C_LINE in regular coff is 0x68. NT has taken over this storage
4757 class to represent a section symbol. */
4758 case C_LINE: /* line # reformatted as symbol table entry. */
4759 /* NT uses 0x67 for a weak symbol, not C_ALIAS. */
4760 case C_ALIAS: /* Duplicate tag. */
4761#endif
4762 /* New storage classes for TI COFF. */
4763#if defined(TIC80COFF) || defined(TICOFF)
4764 case C_UEXT: /* Tentative external definition. */
4765#endif
4766 case C_EXTLAB: /* External load time label. */
4767 case C_HIDDEN: /* Ext symbol in dmert public lib. */
4768 default:
4769 (*_bfd_error_handler)
4770 (_("%s: Unrecognized storage class %d for %s symbol `%s'"),
4771 bfd_archive_filename (abfd), src->u.syment.n_sclass,
4772 dst->symbol.section->name, dst->symbol.name);
4773 dst->symbol.flags = BSF_DEBUGGING;
4774 dst->symbol.value = (src->u.syment.n_value);
4775 break;
4776 }
4777
4778/* BFD_ASSERT(dst->symbol.flags != 0);*/
4779
4780 dst->native = src;
4781
4782 dst->symbol.udata.i = 0;
4783 dst->lineno = (alent *) NULL;
4784 this_index += (src->u.syment.n_numaux) + 1;
4785 dst++;
4786 number_of_symbols++;
4787 }
4788 }
4789
4790 obj_symbols (abfd) = cached_area;
4791 obj_raw_syments (abfd) = native_symbols;
4792
4793 bfd_get_symcount (abfd) = number_of_symbols;
4794 obj_convert (abfd) = table_ptr;
4795 /* Slurp the line tables for each section too. */
4796 {
4797 asection *p;
4798
4799 p = abfd->sections;
4800 while (p)
4801 {
4802 coff_slurp_line_table (abfd, p);
4803 p = p->next;
4804 }
4805 }
4806
4807 return TRUE;
4808} /* coff_slurp_symbol_table() */
4809
4810/* Classify a COFF symbol. A couple of targets have globally visible
4811 symbols which are not class C_EXT, and this handles those. It also
4812 recognizes some special PE cases. */
4813
4814static enum coff_symbol_classification
4815coff_classify_symbol (abfd, syment)
4816 bfd *abfd;
4817 struct internal_syment *syment;
4818{
4819 /* FIXME: This partially duplicates the switch in
4820 coff_slurp_symbol_table. */
4821 switch (syment->n_sclass)
4822 {
4823 case C_EXT:
4824 case C_WEAKEXT:
4825#ifdef I960
4826 case C_LEAFEXT:
4827#endif
4828#ifdef ARM
4829 case C_THUMBEXT:
4830 case C_THUMBEXTFUNC:
4831#endif
4832#ifdef C_SYSTEM
4833 case C_SYSTEM:
4834#endif
4835#ifdef COFF_WITH_PE
4836 case C_NT_WEAK:
4837#endif
4838 if (syment->n_scnum == 0)
4839 {
4840 if (syment->n_value == 0)
4841 return COFF_SYMBOL_UNDEFINED;
4842 else
4843 return COFF_SYMBOL_COMMON;
4844 }
4845 return COFF_SYMBOL_GLOBAL;
4846
4847 default:
4848 break;
4849 }
4850
4851#ifdef COFF_WITH_PE
4852 if (syment->n_sclass == C_STAT)
4853 {
4854 if (syment->n_scnum == 0)
4855 {
4856 /* The Microsoft compiler sometimes generates these if a
4857 small static function is inlined every time it is used.
4858 The function is discarded, but the symbol table entry
4859 remains. */
4860 return COFF_SYMBOL_LOCAL;
4861 }
4862
4863#ifdef STRICT_PE_FORMAT
4864 /* This is correct for Microsoft generated objects, but it
4865 breaks gas generated objects. */
4866
4867 if (syment->n_value == 0)
4868 {
4869 asection *sec;
4870 char buf[SYMNMLEN + 1];
4871
4872 sec = coff_section_from_bfd_index (abfd, syment->n_scnum);
4873 if (sec != NULL
4874 && (strcmp (bfd_get_section_name (abfd, sec),
4875 _bfd_coff_internal_syment_name (abfd, syment, buf))
4876 == 0))
4877 return COFF_SYMBOL_PE_SECTION;
4878 }
4879#endif
4880
4881 return COFF_SYMBOL_LOCAL;
4882 }
4883
4884 if (syment->n_sclass == C_SECTION)
4885 {
4886 /* In some cases in a DLL generated by the Microsoft linker, the
4887 n_value field will contain garbage. FIXME: This should
4888 probably be handled by the swapping function instead. */
4889 syment->n_value = 0;
4890 if (syment->n_scnum == 0)
4891 return COFF_SYMBOL_UNDEFINED;
4892 return COFF_SYMBOL_PE_SECTION;
4893 }
4894#endif /* COFF_WITH_PE */
4895
4896 /* If it is not a global symbol, we presume it is a local symbol. */
4897
4898 if (syment->n_scnum == 0)
4899 {
4900 char buf[SYMNMLEN + 1];
4901
4902 (*_bfd_error_handler)
4903 (_("warning: %s: local symbol `%s' has no section"),
4904 bfd_archive_filename (abfd),
4905 _bfd_coff_internal_syment_name (abfd, syment, buf));
4906 }
4907
4908 return COFF_SYMBOL_LOCAL;
4909}
4910
4911/*
4912SUBSUBSECTION
4913 Reading relocations
4914
4915 Coff relocations are easily transformed into the internal BFD form
4916 (@code{arelent}).
4917
4918 Reading a coff relocation table is done in the following stages:
4919
4920 o Read the entire coff relocation table into memory.
4921
4922 o Process each relocation in turn; first swap it from the
4923 external to the internal form.
4924
4925 o Turn the symbol referenced in the relocation's symbol index
4926 into a pointer into the canonical symbol table.
4927 This table is the same as the one returned by a call to
4928 @code{bfd_canonicalize_symtab}. The back end will call that
4929 routine and save the result if a canonicalization hasn't been done.
4930
4931 o The reloc index is turned into a pointer to a howto
4932 structure, in a back end specific way. For instance, the 386
4933 and 960 use the @code{r_type} to directly produce an index
4934 into a howto table vector; the 88k subtracts a number from the
4935 @code{r_type} field and creates an addend field.
4936
4937*/
4938
4939#ifndef CALC_ADDEND
4940#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
4941 { \
4942 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
4943 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
4944 coffsym = (obj_symbols (abfd) \
4945 + (cache_ptr->sym_ptr_ptr - symbols)); \
4946 else if (ptr) \
4947 coffsym = coff_symbol_from (abfd, ptr); \
4948 if (coffsym != (coff_symbol_type *) NULL \
4949 && coffsym->native->u.syment.n_scnum == 0) \
4950 cache_ptr->addend = 0; \
4951 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
4952 && ptr->section != (asection *) NULL) \
4953 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
4954 else \
4955 cache_ptr->addend = 0; \
4956 }
4957#endif
4958
4959static bfd_boolean
4960coff_slurp_reloc_table (abfd, asect, symbols)
4961 bfd * abfd;
4962 sec_ptr asect;
4963 asymbol ** symbols;
4964{
4965 RELOC *native_relocs;
4966 arelent *reloc_cache;
4967 arelent *cache_ptr;
4968 unsigned int idx;
4969 bfd_size_type amt;
4970
4971 if (asect->relocation)
4972 return TRUE;
4973 if (asect->reloc_count == 0)
4974 return TRUE;
4975 if (asect->flags & SEC_CONSTRUCTOR)
4976 return TRUE;
4977 if (!coff_slurp_symbol_table (abfd))
4978 return FALSE;
4979 amt = (bfd_size_type) bfd_coff_relsz (abfd) * asect->reloc_count;
4980 native_relocs = (RELOC *) buy_and_read (abfd, asect->rel_filepos, amt);
4981 amt = (bfd_size_type) asect->reloc_count * sizeof (arelent);
4982 reloc_cache = (arelent *) bfd_alloc (abfd, amt);
4983
4984 if (reloc_cache == NULL)
4985 return FALSE;
4986
4987 for (idx = 0; idx < asect->reloc_count; idx++)
4988 {
4989 struct internal_reloc dst;
4990 struct external_reloc *src;
4991#ifndef RELOC_PROCESSING
4992 asymbol *ptr;
4993#endif
4994
4995 cache_ptr = reloc_cache + idx;
4996 src = native_relocs + idx;
4997
4998 coff_swap_reloc_in (abfd, src, &dst);
4999
5000#ifdef RELOC_PROCESSING
5001 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
5002#else
5003 cache_ptr->address = dst.r_vaddr;
5004
5005 if (dst.r_symndx != -1)
5006 {
5007 if (dst.r_symndx < 0 || dst.r_symndx >= obj_conv_table_size (abfd))
5008 {
5009 (*_bfd_error_handler)
5010 (_("%s: warning: illegal symbol index %ld in relocs"),
5011 bfd_archive_filename (abfd), dst.r_symndx);
5012 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
5013 ptr = NULL;
5014 }
5015 else
5016 {
5017 cache_ptr->sym_ptr_ptr = (symbols
5018 + obj_convert (abfd)[dst.r_symndx]);
5019 ptr = *(cache_ptr->sym_ptr_ptr);
5020 }
5021 }
5022 else
5023 {
5024 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
5025 ptr = NULL;
5026 }
5027
5028 /* The symbols definitions that we have read in have been
5029 relocated as if their sections started at 0. But the offsets
5030 refering to the symbols in the raw data have not been
5031 modified, so we have to have a negative addend to compensate.
5032
5033 Note that symbols which used to be common must be left alone. */
5034
5035 /* Calculate any reloc addend by looking at the symbol. */
5036 CALC_ADDEND (abfd, ptr, dst, cache_ptr);
5037
5038 cache_ptr->address -= asect->vma;
5039/* !! cache_ptr->section = (asection *) NULL;*/
5040
5041 /* Fill in the cache_ptr->howto field from dst.r_type. */
5042 RTYPE2HOWTO (cache_ptr, &dst);
5043#endif /* RELOC_PROCESSING */
5044
5045 if (cache_ptr->howto == NULL)
5046 {
5047 (*_bfd_error_handler)
5048 (_("%s: illegal relocation type %d at address 0x%lx"),
5049 bfd_archive_filename (abfd), dst.r_type, (long) dst.r_vaddr);
5050 bfd_set_error (bfd_error_bad_value);
5051 return FALSE;
5052 }
5053 }
5054
5055 asect->relocation = reloc_cache;
5056 return TRUE;
5057}
5058
5059#ifndef coff_rtype_to_howto
5060#ifdef RTYPE2HOWTO
5061
5062/* Get the howto structure for a reloc. This is only used if the file
5063 including this one defines coff_relocate_section to be
5064 _bfd_coff_generic_relocate_section, so it is OK if it does not
5065 always work. It is the responsibility of the including file to
5066 make sure it is reasonable if it is needed. */
5067
5068static reloc_howto_type *coff_rtype_to_howto
5069 PARAMS ((bfd *, asection *, struct internal_reloc *,
5070 struct coff_link_hash_entry *, struct internal_syment *,
5071 bfd_vma *));
5072
5073/*ARGSUSED*/
5074static reloc_howto_type *
5075coff_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
5076 bfd *abfd ATTRIBUTE_UNUSED;
5077 asection *sec ATTRIBUTE_UNUSED;
5078 struct internal_reloc *rel;
5079 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
5080 struct internal_syment *sym ATTRIBUTE_UNUSED;
5081 bfd_vma *addendp ATTRIBUTE_UNUSED;
5082{
5083 arelent genrel;
5084
5085 RTYPE2HOWTO (&genrel, rel);
5086 return genrel.howto;
5087}
5088
5089#else /* ! defined (RTYPE2HOWTO) */
5090
5091#define coff_rtype_to_howto NULL
5092
5093#endif /* ! defined (RTYPE2HOWTO) */
5094#endif /* ! defined (coff_rtype_to_howto) */
5095
5096/* This is stupid. This function should be a boolean predicate. */
5097static long
5098coff_canonicalize_reloc (abfd, section, relptr, symbols)
5099 bfd * abfd;
5100 sec_ptr section;
5101 arelent ** relptr;
5102 asymbol ** symbols;
5103{
5104 arelent *tblptr = section->relocation;
5105 unsigned int count = 0;
5106
5107 if (section->flags & SEC_CONSTRUCTOR)
5108 {
5109 /* This section has relocs made up by us, they are not in the
5110 file, so take them out of their chain and place them into
5111 the data area provided. */
5112 arelent_chain *chain = section->constructor_chain;
5113
5114 for (count = 0; count < section->reloc_count; count++)
5115 {
5116 *relptr++ = &chain->relent;
5117 chain = chain->next;
5118 }
5119 }
5120 else
5121 {
5122 if (! coff_slurp_reloc_table (abfd, section, symbols))
5123 return -1;
5124
5125 tblptr = section->relocation;
5126
5127 for (; count++ < section->reloc_count;)
5128 *relptr++ = tblptr++;
5129 }
5130 *relptr = 0;
5131 return section->reloc_count;
5132}
5133
5134#ifdef GNU960
5135file_ptr
5136coff_sym_filepos (abfd)
5137 bfd *abfd;
5138{
5139 return obj_sym_filepos (abfd);
5140}
5141#endif
5142
5143#ifndef coff_reloc16_estimate
5144#define coff_reloc16_estimate dummy_reloc16_estimate
5145
5146static int dummy_reloc16_estimate
5147 PARAMS ((bfd *, asection *, arelent *, unsigned int,
5148 struct bfd_link_info *));
5149
5150static int
5151dummy_reloc16_estimate (abfd, input_section, reloc, shrink, link_info)
5152 bfd *abfd ATTRIBUTE_UNUSED;
5153 asection *input_section ATTRIBUTE_UNUSED;
5154 arelent *reloc ATTRIBUTE_UNUSED;
5155 unsigned int shrink ATTRIBUTE_UNUSED;
5156 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
5157{
5158 abort ();
5159 return 0;
5160}
5161
5162#endif
5163
5164#ifndef coff_reloc16_extra_cases
5165
5166#define coff_reloc16_extra_cases dummy_reloc16_extra_cases
5167
5168/* This works even if abort is not declared in any header file. */
5169
5170static void dummy_reloc16_extra_cases
5171 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
5172 bfd_byte *, unsigned int *, unsigned int *));
5173
5174static void
5175dummy_reloc16_extra_cases (abfd, link_info, link_order, reloc, data, src_ptr,
5176 dst_ptr)
5177 bfd *abfd ATTRIBUTE_UNUSED;
5178 struct bfd_link_info *link_info ATTRIBUTE_UNUSED;
5179 struct bfd_link_order *link_order ATTRIBUTE_UNUSED;
5180 arelent *reloc ATTRIBUTE_UNUSED;
5181 bfd_byte *data ATTRIBUTE_UNUSED;
5182 unsigned int *src_ptr ATTRIBUTE_UNUSED;
5183 unsigned int *dst_ptr ATTRIBUTE_UNUSED;
5184{
5185 abort ();
5186}
5187#endif
5188
5189#ifndef coff_bfd_link_hash_table_free
5190#define coff_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
5191#endif
5192
5193/* If coff_relocate_section is defined, we can use the optimized COFF
5194 backend linker. Otherwise we must continue to use the old linker. */
5195#ifdef coff_relocate_section
5196#ifndef coff_bfd_link_hash_table_create
5197#define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
5198#endif
5199#ifndef coff_bfd_link_add_symbols
5200#define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
5201#endif
5202#ifndef coff_bfd_final_link
5203#define coff_bfd_final_link _bfd_coff_final_link
5204#endif
5205#else /* ! defined (coff_relocate_section) */
5206#define coff_relocate_section NULL
5207#ifndef coff_bfd_link_hash_table_create
5208#define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
5209#endif
5210#ifndef coff_bfd_link_add_symbols
5211#define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
5212#endif
5213#define coff_bfd_final_link _bfd_generic_final_link
5214#endif /* ! defined (coff_relocate_section) */
5215
5216#define coff_bfd_link_just_syms _bfd_generic_link_just_syms
5217#define coff_bfd_link_split_section _bfd_generic_link_split_section
5218
5219#ifndef coff_start_final_link
5220#define coff_start_final_link NULL
5221#endif
5222
5223#ifndef coff_adjust_symndx
5224#define coff_adjust_symndx NULL
5225#endif
5226
5227#ifndef coff_link_add_one_symbol
5228#define coff_link_add_one_symbol _bfd_generic_link_add_one_symbol
5229#endif
5230
5231#ifndef coff_link_output_has_begun
5232
5233static bfd_boolean coff_link_output_has_begun
5234 PARAMS ((bfd *, struct coff_final_link_info *));
5235
5236static bfd_boolean
5237coff_link_output_has_begun (abfd, info)
5238 bfd * abfd;
5239 struct coff_final_link_info * info ATTRIBUTE_UNUSED;
5240{
5241 return abfd->output_has_begun;
5242}
5243#endif
5244
5245#ifndef coff_final_link_postscript
5246
5247static bfd_boolean coff_final_link_postscript
5248 PARAMS ((bfd *, struct coff_final_link_info *));
5249
5250static bfd_boolean
5251coff_final_link_postscript (abfd, pfinfo)
5252 bfd * abfd ATTRIBUTE_UNUSED;
5253 struct coff_final_link_info * pfinfo ATTRIBUTE_UNUSED;
5254{
5255 return TRUE;
5256}
5257#endif
5258
5259#ifndef coff_SWAP_aux_in
5260#define coff_SWAP_aux_in coff_swap_aux_in
5261#endif
5262#ifndef coff_SWAP_sym_in
5263#define coff_SWAP_sym_in coff_swap_sym_in
5264#endif
5265#ifndef coff_SWAP_lineno_in
5266#define coff_SWAP_lineno_in coff_swap_lineno_in
5267#endif
5268#ifndef coff_SWAP_aux_out
5269#define coff_SWAP_aux_out coff_swap_aux_out
5270#endif
5271#ifndef coff_SWAP_sym_out
5272#define coff_SWAP_sym_out coff_swap_sym_out
5273#endif
5274#ifndef coff_SWAP_lineno_out
5275#define coff_SWAP_lineno_out coff_swap_lineno_out
5276#endif
5277#ifndef coff_SWAP_reloc_out
5278#define coff_SWAP_reloc_out coff_swap_reloc_out
5279#endif
5280#ifndef coff_SWAP_filehdr_out
5281#define coff_SWAP_filehdr_out coff_swap_filehdr_out
5282#endif
5283#ifndef coff_SWAP_aouthdr_out
5284#define coff_SWAP_aouthdr_out coff_swap_aouthdr_out
5285#endif
5286#ifndef coff_SWAP_scnhdr_out
5287#define coff_SWAP_scnhdr_out coff_swap_scnhdr_out
5288#endif
5289#ifndef coff_SWAP_reloc_in
5290#define coff_SWAP_reloc_in coff_swap_reloc_in
5291#endif
5292#ifndef coff_SWAP_filehdr_in
5293#define coff_SWAP_filehdr_in coff_swap_filehdr_in
5294#endif
5295#ifndef coff_SWAP_aouthdr_in
5296#define coff_SWAP_aouthdr_in coff_swap_aouthdr_in
5297#endif
5298#ifndef coff_SWAP_scnhdr_in
5299#define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
5300#endif
5301
5302static const bfd_coff_backend_data bfd_coff_std_swap_table =
5303{
5304 coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
5305 coff_SWAP_aux_out, coff_SWAP_sym_out,
5306 coff_SWAP_lineno_out, coff_SWAP_reloc_out,
5307 coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
5308 coff_SWAP_scnhdr_out,
5309 FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
5310#ifdef COFF_LONG_FILENAMES
5311 TRUE,
5312#else
5313 FALSE,
5314#endif
5315#ifdef COFF_LONG_SECTION_NAMES
5316 TRUE,
5317#else
5318 FALSE,
5319#endif
5320 COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
5321#ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
5322 TRUE,
5323#else
5324 FALSE,
5325#endif
5326#ifdef COFF_DEBUG_STRING_WIDE_PREFIX
5327 4,
5328#else
5329 2,
5330#endif
5331 coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
5332 coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
5333 coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
5334 coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
5335 coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
5336 coff_classify_symbol, coff_compute_section_file_positions,
5337 coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
5338 coff_adjust_symndx, coff_link_add_one_symbol,
5339 coff_link_output_has_begun, coff_final_link_postscript
5340};
5341
5342#ifndef coff_close_and_cleanup
5343#define coff_close_and_cleanup _bfd_generic_close_and_cleanup
5344#endif
5345
5346#ifndef coff_bfd_free_cached_info
5347#define coff_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
5348#endif
5349
5350#ifndef coff_get_section_contents
5351#define coff_get_section_contents _bfd_generic_get_section_contents
5352#endif
5353
5354#ifndef coff_bfd_copy_private_symbol_data
5355#define coff_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
5356#endif
5357
5358#ifndef coff_bfd_copy_private_section_data
5359#define coff_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
5360#endif
5361
5362#ifndef coff_bfd_copy_private_bfd_data
5363#define coff_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
5364#endif
5365
5366#ifndef coff_bfd_merge_private_bfd_data
5367#define coff_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
5368#endif
5369
5370#ifndef coff_bfd_set_private_flags
5371#define coff_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
5372#endif
5373
5374#ifndef coff_bfd_print_private_bfd_data
5375#define coff_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
5376#endif
5377
5378#ifndef coff_bfd_is_local_label_name
5379#define coff_bfd_is_local_label_name _bfd_coff_is_local_label_name
5380#endif
5381
5382#ifndef coff_read_minisymbols
5383#define coff_read_minisymbols _bfd_generic_read_minisymbols
5384#endif
5385
5386#ifndef coff_minisymbol_to_symbol
5387#define coff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
5388#endif
5389
5390/* The reloc lookup routine must be supplied by each individual COFF
5391 backend. */
5392#ifndef coff_bfd_reloc_type_lookup
5393#define coff_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
5394#endif
5395
5396#ifndef coff_bfd_get_relocated_section_contents
5397#define coff_bfd_get_relocated_section_contents \
5398 bfd_generic_get_relocated_section_contents
5399#endif
5400
5401#ifndef coff_bfd_relax_section
5402#define coff_bfd_relax_section bfd_generic_relax_section
5403#endif
5404
5405#ifndef coff_bfd_gc_sections
5406#define coff_bfd_gc_sections bfd_generic_gc_sections
5407#endif
5408
5409#ifndef coff_bfd_merge_sections
5410#define coff_bfd_merge_sections bfd_generic_merge_sections
5411#endif
5412
5413#ifndef coff_bfd_discard_group
5414#define coff_bfd_discard_group bfd_generic_discard_group
5415#endif
5416
5417#define CREATE_BIG_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE) \
5418const bfd_target VAR = \
5419{ \
5420 NAME , \
5421 bfd_target_coff_flavour, \
5422 BFD_ENDIAN_BIG, /* data byte order is big */ \
5423 BFD_ENDIAN_BIG, /* header byte order is big */ \
5424 /* object flags */ \
5425 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
5426 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
5427 /* section flags */ \
5428 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
5429 UNDER, /* leading symbol underscore */ \
5430 '/', /* ar_pad_char */ \
5431 15, /* ar_max_namelen */ \
5432 \
5433 /* Data conversion functions. */ \
5434 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
5435 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
5436 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
5437 \
5438 /* Header conversion functions. */ \
5439 bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
5440 bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
5441 bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
5442 \
5443 /* bfd_check_format */ \
5444 { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p, \
5445 _bfd_dummy_target }, \
5446 /* bfd_set_format */ \
5447 { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false }, \
5448 /* bfd_write_contents */ \
5449 { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, \
5450 bfd_false }, \
5451 \
5452 BFD_JUMP_TABLE_GENERIC (coff), \
5453 BFD_JUMP_TABLE_COPY (coff), \
5454 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
5455 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
5456 BFD_JUMP_TABLE_SYMBOLS (coff), \
5457 BFD_JUMP_TABLE_RELOCS (coff), \
5458 BFD_JUMP_TABLE_WRITE (coff), \
5459 BFD_JUMP_TABLE_LINK (coff), \
5460 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
5461 \
5462 ALTERNATIVE, \
5463 \
5464 COFF_SWAP_TABLE \
5465};
5466
5467#define CREATE_LITTLE_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE) \
5468const bfd_target VAR = \
5469{ \
5470 NAME , \
5471 bfd_target_coff_flavour, \
5472 BFD_ENDIAN_LITTLE, /* data byte order is little */ \
5473 BFD_ENDIAN_LITTLE, /* header byte order is little */ \
5474 /* object flags */ \
5475 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
5476 HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
5477 /* section flags */ \
5478 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
5479 UNDER, /* leading symbol underscore */ \
5480 '/', /* ar_pad_char */ \
5481 15, /* ar_max_namelen */ \
5482 \
5483 /* Data conversion functions. */ \
5484 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
5485 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
5486 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
5487 /* Header conversion functions. */ \
5488 bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
5489 bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
5490 bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
5491 /* bfd_check_format */ \
5492 { _bfd_dummy_target, coff_object_p, bfd_generic_archive_p, \
5493 _bfd_dummy_target }, \
5494 /* bfd_set_format */ \
5495 { bfd_false, coff_mkobject, _bfd_generic_mkarchive, bfd_false }, \
5496 /* bfd_write_contents */ \
5497 { bfd_false, coff_write_object_contents, _bfd_write_archive_contents, \
5498 bfd_false }, \
5499 \
5500 BFD_JUMP_TABLE_GENERIC (coff), \
5501 BFD_JUMP_TABLE_COPY (coff), \
5502 BFD_JUMP_TABLE_CORE (_bfd_nocore), \
5503 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
5504 BFD_JUMP_TABLE_SYMBOLS (coff), \
5505 BFD_JUMP_TABLE_RELOCS (coff), \
5506 BFD_JUMP_TABLE_WRITE (coff), \
5507 BFD_JUMP_TABLE_LINK (coff), \
5508 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
5509 \
5510 ALTERNATIVE, \
5511 \
5512 COFF_SWAP_TABLE \
5513};
Note: See TracBrowser for help on using the repository browser.