1/* BFD COFF object file private structure.
2   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
22   MA 02110-1301, USA.  */
23
24#include "bfdlink.h"
25
26/* Object file tdata; access macros.  */
27
28#define coff_data(bfd)		      ((bfd)->tdata.coff_obj_data)
29#define exec_hdr(bfd)		      (coff_data (bfd)->hdr)
30#define obj_pe(bfd)                   (coff_data (bfd)->pe)
31#define obj_symbols(bfd)	      (coff_data (bfd)->symbols)
32#define	obj_sym_filepos(bfd)	      (coff_data (bfd)->sym_filepos)
33#define obj_relocbase(bfd)	      (coff_data (bfd)->relocbase)
34#define obj_raw_syments(bfd)	      (coff_data (bfd)->raw_syments)
35#define obj_raw_syment_count(bfd)     (coff_data (bfd)->raw_syment_count)
36#define obj_convert(bfd)	      (coff_data (bfd)->conversion_table)
37#define obj_conv_table_size(bfd)      (coff_data (bfd)->conv_table_size)
38#define obj_coff_external_syms(bfd)   (coff_data (bfd)->external_syms)
39#define obj_coff_keep_syms(bfd)	      (coff_data (bfd)->keep_syms)
40#define obj_coff_strings(bfd)	      (coff_data (bfd)->strings)
41#define obj_coff_keep_strings(bfd)    (coff_data (bfd)->keep_strings)
42#define obj_coff_sym_hashes(bfd)      (coff_data (bfd)->sym_hashes)
43#define obj_coff_strings_written(bfd) (coff_data (bfd)->strings_written)
44#define obj_coff_local_toc_table(bfd) (coff_data (bfd)->local_toc_sym_map)
45
46/* `Tdata' information kept for COFF files.  */
47
48typedef struct coff_tdata
49{
50  struct coff_symbol_struct *symbols;	/* Symtab for input bfd.  */
51  unsigned int *conversion_table;
52  int conv_table_size;
53  file_ptr sym_filepos;
54
55  struct coff_ptr_struct *raw_syments;
56  unsigned long raw_syment_count;
57
58  /* These are only valid once writing has begun.  */
59  long int relocbase;
60
61  /* These members communicate important constants about the symbol table
62     to GDB's symbol-reading code.  These `constants' unfortunately vary
63     from coff implementation to implementation...  */
64  unsigned local_n_btmask;
65  unsigned local_n_btshft;
66  unsigned local_n_tmask;
67  unsigned local_n_tshift;
68  unsigned local_symesz;
69  unsigned local_auxesz;
70  unsigned local_linesz;
71
72  /* The unswapped external symbols.  May be NULL.  Read by
73     _bfd_coff_get_external_symbols.  */
74  void * external_syms;
75  /* If this is TRUE, the external_syms may not be freed.  */
76  bfd_boolean keep_syms;
77
78  /* The string table.  May be NULL.  Read by
79     _bfd_coff_read_string_table.  */
80  char *strings;
81  /* If this is TRUE, the strings may not be freed.  */
82  bfd_boolean keep_strings;
83  /* If this is TRUE, the strings have been written out already.  */
84  bfd_boolean strings_written;
85
86  /* Is this a PE format coff file?  */
87  int pe;
88  /* Used by the COFF backend linker.  */
89  struct coff_link_hash_entry **sym_hashes;
90
91  /* Used by the pe linker for PowerPC.  */
92  int *local_toc_sym_map;
93
94  struct bfd_link_info *link_info;
95
96  /* Used by coff_find_nearest_line.  */
97  void * line_info;
98
99  /* A place to stash dwarf2 info for this bfd.  */
100  void * dwarf2_find_line_info;
101
102  /* The timestamp from the COFF file header.  */
103  long timestamp;
104
105  /* Copy of some of the f_flags bits in the COFF filehdr structure,
106     used by ARM code.  */
107  flagword flags;
108
109  /* coff-stgo32 EXE stub header after BFD tdata has been allocated.  Its data
110     is kept in internal_filehdr.go32stub beforehand.  */
111  char *go32stub;
112} coff_data_type;
113
114/* Tdata for pe image files.  */
115typedef struct pe_tdata
116{
117  coff_data_type coff;
118  struct internal_extra_pe_aouthdr pe_opthdr;
119  int dll;
120  int has_reloc_section;
121  int dont_strip_reloc;
122  bfd_boolean (*in_reloc_p) (bfd *, reloc_howto_type *);
123  flagword real_flags;
124} pe_data_type;
125
126#define pe_data(bfd)		((bfd)->tdata.pe_obj_data)
127
128/* Tdata for XCOFF files.  */
129
130struct xcoff_tdata
131{
132  /* Basic COFF information.  */
133  coff_data_type coff;
134
135  /* TRUE if this is an XCOFF64 file. */
136  bfd_boolean xcoff64;
137
138  /* TRUE if a large a.out header should be generated.  */
139  bfd_boolean full_aouthdr;
140
141  /* TOC value.  */
142  bfd_vma toc;
143
144  /* Index of section holding TOC.  */
145  int sntoc;
146
147  /* Index of section holding entry point.  */
148  int snentry;
149
150  /* .text alignment from optional header.  */
151  int text_align_power;
152
153  /* .data alignment from optional header.  */
154  int data_align_power;
155
156  /* modtype from optional header.  */
157  short modtype;
158
159  /* cputype from optional header.  */
160  short cputype;
161
162  /* maxdata from optional header.  */
163  bfd_vma maxdata;
164
165  /* maxstack from optional header.  */
166  bfd_vma maxstack;
167
168  /* Used by the XCOFF backend linker.  */
169  asection **csects;
170  long *debug_indices;
171  unsigned int *lineno_counts;
172  unsigned int import_file_id;
173};
174
175#define xcoff_data(abfd) ((abfd)->tdata.xcoff_obj_data)
176
177/* We take the address of the first element of an asymbol to ensure that the
178   macro is only ever applied to an asymbol.  */
179#define coffsymbol(asymbol) ((coff_symbol_type *)(&((asymbol)->the_bfd)))
180
181/* The used_by_bfd field of a section may be set to a pointer to this
182   structure.  */
183
184struct coff_section_tdata
185{
186  /* The relocs, swapped into COFF internal form.  This may be NULL.  */
187  struct internal_reloc *relocs;
188  /* If this is TRUE, the relocs entry may not be freed.  */
189  bfd_boolean keep_relocs;
190  /* The section contents.  This may be NULL.  */
191  bfd_byte *contents;
192  /* If this is TRUE, the contents entry may not be freed.  */
193  bfd_boolean keep_contents;
194  /* Information cached by coff_find_nearest_line.  */
195  bfd_vma offset;
196  unsigned int i;
197  const char *function;
198  /* Optional information about a COMDAT entry; NULL if not COMDAT. */
199  struct coff_comdat_info *comdat;
200  int line_base;
201  /* A pointer used for .stab linking optimizations.  */
202  void * stab_info;
203  /* Available for individual backends.  */
204  void * tdata;
205};
206
207/* An accessor macro for the coff_section_tdata structure.  */
208#define coff_section_data(abfd, sec) \
209  ((struct coff_section_tdata *) (sec)->used_by_bfd)
210
211/* Tdata for sections in XCOFF files.  This is used by the linker.  */
212
213struct xcoff_section_tdata
214{
215  /* Used for XCOFF csects created by the linker; points to the real
216     XCOFF section which contains this csect.  */
217  asection *enclosing;
218  /* The lineno_count field for the enclosing section, because we are
219     going to clobber it there.  */
220  unsigned int lineno_count;
221  /* The first and last symbol indices for symbols used by this csect.  */
222  unsigned long first_symndx;
223  unsigned long last_symndx;
224};
225
226/* An accessor macro the xcoff_section_tdata structure.  */
227#define xcoff_section_data(abfd, sec) \
228  ((struct xcoff_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
229
230/* Tdata for sections in PE files.  */
231
232struct pei_section_tdata
233{
234  /* The virtual size of the section.  */
235  bfd_size_type virt_size;
236  /* The PE section flags.  */
237  long pe_flags;
238};
239
240/* An accessor macro for the pei_section_tdata structure.  */
241#define pei_section_data(abfd, sec) \
242  ((struct pei_section_tdata *) coff_section_data ((abfd), (sec))->tdata)
243
244/* COFF linker hash table entries.  */
245
246struct coff_link_hash_entry
247{
248  struct bfd_link_hash_entry root;
249
250  /* Symbol index in output file.  Set to -1 initially.  Set to -2 if
251     there is a reloc against this symbol.  */
252  long indx;
253
254  /* Symbol type.  */
255  unsigned short type;
256
257  /* Symbol class.  */
258  unsigned char symbol_class;
259
260  /* Number of auxiliary entries.  */
261  char numaux;
262
263  /* BFD to take auxiliary entries from.  */
264  bfd *auxbfd;
265
266  /* Pointer to array of auxiliary entries, if any.  */
267  union internal_auxent *aux;
268
269  /* Flag word; legal values follow.  */
270  unsigned short coff_link_hash_flags;
271  /* Symbol is a PE section symbol.  */
272#define COFF_LINK_HASH_PE_SECTION_SYMBOL (01)
273};
274
275/* COFF linker hash table.  */
276
277struct coff_link_hash_table
278{
279  struct bfd_link_hash_table root;
280  /* A pointer to information used to link stabs in sections.  */
281  struct stab_info stab_info;
282};
283
284/* Look up an entry in a COFF linker hash table.  */
285
286#define coff_link_hash_lookup(table, string, create, copy, follow)	\
287  ((struct coff_link_hash_entry *)					\
288   bfd_link_hash_lookup (&(table)->root, (string), (create),		\
289			 (copy), (follow)))
290
291/* Traverse a COFF linker hash table.  */
292
293#define coff_link_hash_traverse(table, func, info)			\
294  (bfd_link_hash_traverse						\
295   (&(table)->root,							\
296    (bfd_boolean (*) (struct bfd_link_hash_entry *, void *)) (func), \
297    (info)))
298
299/* Get the COFF linker hash table from a link_info structure.  */
300
301#define coff_hash_table(p) ((struct coff_link_hash_table *) ((p)->hash))
302
303/* Functions in coffgen.c.  */
304extern const bfd_target *coff_object_p
305  (bfd *);
306extern struct bfd_section *coff_section_from_bfd_index
307  (bfd *, int);
308extern long coff_get_symtab_upper_bound
309  (bfd *);
310extern long coff_canonicalize_symtab
311  (bfd *, asymbol **);
312extern int coff_count_linenumbers
313  (bfd *);
314extern struct coff_symbol_struct *coff_symbol_from
315  (bfd *, asymbol *);
316extern bfd_boolean coff_renumber_symbols
317  (bfd *, int *);
318extern void coff_mangle_symbols
319  (bfd *);
320extern bfd_boolean coff_write_symbols
321  (bfd *);
322extern bfd_boolean coff_write_linenumbers
323  (bfd *);
324extern alent *coff_get_lineno
325  (bfd *, asymbol *);
326extern asymbol *coff_section_symbol
327  (bfd *, char *);
328extern bfd_boolean _bfd_coff_get_external_symbols
329  (bfd *);
330extern const char *_bfd_coff_read_string_table
331  (bfd *);
332extern bfd_boolean _bfd_coff_free_symbols
333  (bfd *);
334extern struct coff_ptr_struct *coff_get_normalized_symtab
335  (bfd *);
336extern long coff_get_reloc_upper_bound
337  (bfd *, sec_ptr);
338extern asymbol *coff_make_empty_symbol
339  (bfd *);
340extern void coff_print_symbol
341  (bfd *, void * filep, asymbol *, bfd_print_symbol_type);
342extern void coff_get_symbol_info
343  (bfd *, asymbol *, symbol_info *ret);
344extern bfd_boolean _bfd_coff_is_local_label_name
345  (bfd *, const char *);
346extern asymbol *coff_bfd_make_debug_symbol
347  (bfd *, void *, unsigned long);
348extern bfd_boolean coff_find_nearest_line
349  (bfd *, asection *, asymbol **, bfd_vma, const char **,
350   const char **, unsigned int *);
351extern bfd_boolean coff_find_inliner_info
352  (bfd *, const char **, const char **, unsigned int *);
353extern int coff_sizeof_headers
354  (bfd *, struct bfd_link_info *);
355extern bfd_boolean bfd_coff_reloc16_relax_section
356  (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
357extern bfd_byte *bfd_coff_reloc16_get_relocated_section_contents
358  (bfd *, struct bfd_link_info *, struct bfd_link_order *,
359   bfd_byte *, bfd_boolean, asymbol **);
360extern bfd_vma bfd_coff_reloc16_get_value
361  (arelent *, struct bfd_link_info *, asection *);
362extern void bfd_perform_slip
363  (bfd *, unsigned int, asection *, bfd_vma);
364
365/* Functions and types in cofflink.c.  */
366
367#define STRING_SIZE_SIZE 4
368
369/* We use a hash table to merge identical enum, struct, and union
370   definitions in the linker.  */
371
372/* Information we keep for a single element (an enum value, a
373   structure or union field) in the debug merge hash table.  */
374
375struct coff_debug_merge_element
376{
377  /* Next element.  */
378  struct coff_debug_merge_element *next;
379
380  /* Name.  */
381  const char *name;
382
383  /* Type.  */
384  unsigned int type;
385
386  /* Symbol index for complex type.  */
387  long tagndx;
388};
389
390/* A linked list of debug merge entries for a given name.  */
391
392struct coff_debug_merge_type
393{
394  /* Next type with the same name.  */
395  struct coff_debug_merge_type *next;
396
397  /* Class of type.  */
398  int type_class;
399
400  /* Symbol index where this type is defined.  */
401  long indx;
402
403  /* List of elements.  */
404  struct coff_debug_merge_element *elements;
405};
406
407/* Information we store in the debug merge hash table.  */
408
409struct coff_debug_merge_hash_entry
410{
411  struct bfd_hash_entry root;
412
413  /* A list of types with this name.  */
414  struct coff_debug_merge_type *types;
415};
416
417/* The debug merge hash table.  */
418
419struct coff_debug_merge_hash_table
420{
421  struct bfd_hash_table root;
422};
423
424/* Initialize a COFF debug merge hash table.  */
425
426#define coff_debug_merge_hash_table_init(table) \
427  (bfd_hash_table_init (&(table)->root, _bfd_coff_debug_merge_hash_newfunc, \
428			sizeof (struct coff_debug_merge_hash_entry)))
429
430/* Free a COFF debug merge hash table.  */
431
432#define coff_debug_merge_hash_table_free(table) \
433  (bfd_hash_table_free (&(table)->root))
434
435/* Look up an entry in a COFF debug merge hash table.  */
436
437#define coff_debug_merge_hash_lookup(table, string, create, copy) \
438  ((struct coff_debug_merge_hash_entry *) \
439   bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
440
441/* Information we keep for each section in the output file when doing
442   a relocatable link.  */
443
444struct coff_link_section_info
445{
446  /* The relocs to be output.  */
447  struct internal_reloc *relocs;
448  /* For each reloc against a global symbol whose index was not known
449     when the reloc was handled, the global hash table entry.  */
450  struct coff_link_hash_entry **rel_hashes;
451};
452
453/* Information that we pass around while doing the final link step.  */
454
455struct coff_final_link_info
456{
457  /* General link information.  */
458  struct bfd_link_info *info;
459  /* Output BFD.  */
460  bfd *output_bfd;
461  /* Used to indicate failure in traversal routine.  */
462  bfd_boolean failed;
463  /* If doing "task linking" set only during the time when we want the
464     global symbol writer to convert the storage class of defined global
465     symbols from global to static. */
466  bfd_boolean global_to_static;
467  /* Hash table for long symbol names.  */
468  struct bfd_strtab_hash *strtab;
469  /* When doing a relocatable link, an array of information kept for
470     each output section, indexed by the target_index field.  */
471  struct coff_link_section_info *section_info;
472  /* Symbol index of last C_FILE symbol (-1 if none).  */
473  long last_file_index;
474  /* Contents of last C_FILE symbol.  */
475  struct internal_syment last_file;
476  /* Symbol index of first aux entry of last .bf symbol with an empty
477     endndx field (-1 if none).  */
478  long last_bf_index;
479  /* Contents of last_bf_index aux entry.  */
480  union internal_auxent last_bf;
481  /* Hash table used to merge debug information.  */
482  struct coff_debug_merge_hash_table debug_merge;
483  /* Buffer large enough to hold swapped symbols of any input file.  */
484  struct internal_syment *internal_syms;
485  /* Buffer large enough to hold sections of symbols of any input file.  */
486  asection **sec_ptrs;
487  /* Buffer large enough to hold output indices of symbols of any
488     input file.  */
489  long *sym_indices;
490  /* Buffer large enough to hold output symbols for any input file.  */
491  bfd_byte *outsyms;
492  /* Buffer large enough to hold external line numbers for any input
493     section.  */
494  bfd_byte *linenos;
495  /* Buffer large enough to hold any input section.  */
496  bfd_byte *contents;
497  /* Buffer large enough to hold external relocs of any input section.  */
498  bfd_byte *external_relocs;
499  /* Buffer large enough to hold swapped relocs of any input section.  */
500  struct internal_reloc *internal_relocs;
501};
502
503/* Most COFF variants have no way to record the alignment of a
504   section.  This struct is used to set a specific alignment based on
505   the name of the section.  */
506
507struct coff_section_alignment_entry
508{
509  /* The section name.  */
510  const char *name;
511
512  /* This is either (unsigned int) -1, indicating that the section
513     name must match exactly, or it is the number of letters which
514     must match at the start of the name.  */
515  unsigned int comparison_length;
516
517  /* These macros may be used to fill in the first two fields in a
518     structure initialization.  */
519#define COFF_SECTION_NAME_EXACT_MATCH(name) (name), ((unsigned int) -1)
520#define COFF_SECTION_NAME_PARTIAL_MATCH(name) (name), (sizeof (name) - 1)
521
522  /* Only use this entry if the default section alignment for this
523     target is at least that much (as a power of two).  If this field
524     is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored.  */
525  unsigned int default_alignment_min;
526
527  /* Only use this entry if the default section alignment for this
528     target is no greater than this (as a power of two).  If this
529     field is COFF_ALIGNMENT_FIELD_EMPTY, it should be ignored.  */
530  unsigned int default_alignment_max;
531
532#define COFF_ALIGNMENT_FIELD_EMPTY ((unsigned int) -1)
533
534  /* The desired alignment for this section (as a power of two).  */
535  unsigned int alignment_power;
536};
537
538extern struct bfd_hash_entry *_bfd_coff_link_hash_newfunc
539  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
540extern bfd_boolean _bfd_coff_link_hash_table_init
541  (struct coff_link_hash_table *, bfd *,
542   struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
543			       struct bfd_hash_table *,
544			       const char *),
545   unsigned int);
546extern struct bfd_link_hash_table *_bfd_coff_link_hash_table_create
547  (bfd *);
548extern const char *_bfd_coff_internal_syment_name
549  (bfd *, const struct internal_syment *, char *);
550extern bfd_boolean _bfd_coff_link_add_symbols
551  (bfd *, struct bfd_link_info *);
552extern bfd_boolean _bfd_coff_final_link
553  (bfd *, struct bfd_link_info *);
554extern struct internal_reloc *_bfd_coff_read_internal_relocs
555  (bfd *, asection *, bfd_boolean, bfd_byte *, bfd_boolean,
556   struct internal_reloc *);
557extern bfd_boolean _bfd_coff_generic_relocate_section
558  (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
559   struct internal_reloc *, struct internal_syment *, asection **);
560extern struct bfd_hash_entry *_bfd_coff_debug_merge_hash_newfunc
561  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
562extern bfd_boolean _bfd_coff_write_global_sym
563  (struct coff_link_hash_entry *, void *);
564extern bfd_boolean _bfd_coff_write_task_globals
565  (struct coff_link_hash_entry *, void *);
566extern bfd_boolean _bfd_coff_link_input_bfd
567  (struct coff_final_link_info *, bfd *);
568extern bfd_boolean _bfd_coff_reloc_link_order
569  (bfd *, struct coff_final_link_info *, asection *,
570   struct bfd_link_order *);
571
572
573#define coff_get_section_contents_in_window \
574  _bfd_generic_get_section_contents_in_window
575
576/* Functions in xcofflink.c.  */
577
578extern long _bfd_xcoff_get_dynamic_symtab_upper_bound
579  (bfd *);
580extern long _bfd_xcoff_canonicalize_dynamic_symtab
581  (bfd *, asymbol **);
582extern long _bfd_xcoff_get_dynamic_reloc_upper_bound
583  (bfd *);
584extern long _bfd_xcoff_canonicalize_dynamic_reloc
585  (bfd *, arelent **, asymbol **);
586extern struct bfd_link_hash_table *_bfd_xcoff_bfd_link_hash_table_create
587  (bfd *);
588extern void _bfd_xcoff_bfd_link_hash_table_free
589  (struct bfd_link_hash_table *);
590extern bfd_boolean _bfd_xcoff_bfd_link_add_symbols
591  (bfd *, struct bfd_link_info *);
592extern bfd_boolean _bfd_xcoff_bfd_final_link
593  (bfd *, struct bfd_link_info *);
594extern bfd_boolean _bfd_xcoff_define_common_symbol
595  (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *);
596extern bfd_boolean _bfd_ppc_xcoff_relocate_section
597  (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
598   struct internal_reloc *, struct internal_syment *, asection **);
599
600/* Functions in coff-ppc.c.  FIXME: These are called by pe.em in the
601   linker, and so should start with bfd and be declared in bfd.h.  */
602
603extern bfd_boolean ppc_allocate_toc_section
604  (struct bfd_link_info *);
605extern bfd_boolean ppc_process_before_allocation
606  (bfd *, struct bfd_link_info *);
607