Add bfd_free_cached_info support to a.out backends.
[external/binutils.git] / bfd / hp300hpux.c
1 /* BFD backend for hp-ux 9000/300
2    Copyright (C) 1990, 1991, 1994 Free Software Foundation, Inc.
3    Written by Glenn Engel.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /*
22
23     hpux native  ------------> |               |
24                                | hp300hpux bfd | ----------> hpux w/gnu ext
25     hpux w/gnu extension ----> |               |
26
27
28     Support for the 9000/[34]00 has several limitations.
29       1. Shared libraries are not supported.
30       2. The output format from this bfd is not usable by native tools.
31
32     The primary motivation for writing this bfd was to allow use of
33     gdb and gcc for host based debugging and not to mimic the hp-ux tools
34     in every detail.  This leads to a significant simplification of the
35     code and a leap in performance.  The decision to not output hp native
36     compatible objects was further strengthened by the fact that the richness
37     of the gcc compiled objects could not be represented without loss of
38     information.  For example, while the hp format supports the concept of
39     secondary symbols, it does not support indirect symbols.  Another
40     reason is to maintain backwards compatibility with older implementations
41     of gcc on hpux which used 'hpxt' to translate .a and .o files into a
42     format which could be readily understood by the gnu linker and gdb.
43     This allows reading hp secondary symbols and converting them into
44     indirect symbols but the reverse it not always possible.
45
46     Another example of differences is that the hp format stores symbol offsets
47     in the object code while the gnu utilities use a field in the
48     relocation record for this.  To support the hp native format, the object
49     code would need to be patched with the offsets when producing .o files.
50
51     The basic technique taken in this implementation is to #include the code
52     from aoutx.h and aout-target.h with appropriate #defines to override
53     code where a unique implementation is needed:
54
55     {
56         #define a bunch of stuff
57         #include <aoutx.h>
58
59         implement a bunch of functions
60
61         #include "aout-target.h"
62     }
63
64     The hp symbol table is a bit different than other a.out targets.  Instead
65     of having an array of nlist items and an array of strings, hp's format
66     has them mixed together in one structure.  In addition, the strings are
67     not null terminated.  It looks something like this:
68
69     nlist element 1
70     string1
71     nlist element 2
72     string2
73     ...
74
75     The whole symbol table is read as one chunk and then we march thru it
76     and convert it to canonical form.  As we march thru the table, we copy
77     the nlist data into the internal form and we compact the strings and null
78     terminate them, using storage from the already allocated symbol table:
79
80     string1
81     null
82     string2
83     null
84  */
85
86 /* @@ Is this really so different from normal a.out that it needs to include
87    aoutx.h?  We should go through this file sometime and see what can be made
88    more dependent on aout32.o and what might need to be broken off and accessed
89    through the backend_data field.  Or, maybe we really do need such a
90    completely separate implementation.  I don't have time to investigate this
91    much further right now.  [raeburn:19930428.2124EST] */
92 /* @@ Also, note that there wind up being two versions of some routines, with
93    different names, only one of which actually gets used.  For example:
94         slurp_symbol_table
95         swap_std_reloc_in
96         slurp_reloc_table
97         get_symtab
98         get_symtab_upper_bound
99         canonicalize_reloc
100         mkobject
101    This should also be fixed.  */
102
103 #define ARCH 32
104 #define TARGETNAME "a.out-hp300hpux"
105 #define MY(OP) CAT(hp300hpux_,OP)
106
107 #define external_exec hp300hpux_exec_bytes
108 #define external_nlist hp300hpux_nlist_bytes
109
110 #include "aout/hp300hpux.h"
111
112 /* define these so we can compile unused routines in aoutx.h */
113 #define e_strx  e_shlib
114 #define e_other e_length
115 #define e_desc  e_almod
116
117 #define AR_PAD_CHAR '/'
118 #define TARGET_IS_BIG_ENDIAN_P
119 #define DEFAULT_ARCH bfd_arch_m68k
120
121 #define MY_get_section_contents aout_32_get_section_contents
122 #define MY_slurp_armap bfd_slurp_bsd_armap_f2
123
124 /***********************************************/
125 /* provide overrides for routines in this file */
126 /***********************************************/
127 /* these don't use MY because that causes problems within JUMP_TABLE
128    (CAT winds up being expanded recursively, which ANSI C compilers
129    will not do).  */
130 #define MY_get_symtab hp300hpux_get_symtab
131 #define MY_get_symtab_upper_bound hp300hpux_get_symtab_upper_bound
132 #define MY_canonicalize_reloc hp300hpux_canonicalize_reloc
133 #define MY_write_object_contents hp300hpux_write_object_contents
134
135 #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
136 #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols
137 #define MY_bfd_final_link _bfd_generic_final_link
138
139 /* Until and unless we convert the slurp_reloc and slurp_symtab
140    routines in this file, we can not use the default aout
141    free_cached_info routine which assumes that the relocs and symtabs
142    were allocated using malloc.  */
143 #define MY_bfd_free_cached_info bfd_true
144
145 #define hp300hpux_write_syms aout_32_write_syms
146
147 #define MY_callback MY(callback)
148
149 #define NAME_swap_exec_header_in NAME(hp300hpux_32_,swap_exec_header_in)
150
151 #define HP_SYMTYPE_UNDEFINED    0x00
152 #define HP_SYMTYPE_ABSOLUTE     0x01
153 #define HP_SYMTYPE_TEXT         0x02
154 #define HP_SYMTYPE_DATA         0x03
155 #define HP_SYMTYPE_BSS          0x04
156 #define HP_SYMTYPE_COMMON       0x05
157
158 #define HP_SYMTYPE_TYPE         0x0F
159 #define HP_SYMTYPE_FILENAME     0x1F
160
161 #define HP_SYMTYPE_ALIGN        0x10
162 #define HP_SYMTYPE_EXTERNAL     0x20
163 #define HP_SECONDARY_SYMBOL     0x40
164
165 /* RELOCATION DEFINITIONS */
166 #define HP_RSEGMENT_TEXT        0x00
167 #define HP_RSEGMENT_DATA        0x01
168 #define HP_RSEGMENT_BSS         0x02
169 #define HP_RSEGMENT_EXTERNAL    0x03
170 #define HP_RSEGMENT_PCREL       0x04
171 #define HP_RSEGMENT_RDLT        0x05
172 #define HP_RSEGMENT_RPLT        0x06
173 #define HP_RSEGMENT_NOOP        0x3F
174
175 #define HP_RLENGTH_BYTE         0x00
176 #define HP_RLENGTH_WORD         0x01
177 #define HP_RLENGTH_LONG         0x02
178 #define HP_RLENGTH_ALIGN        0x03
179
180 #define NAME(x,y) CAT3(hp300hpux,_32_,y)
181 #define ARCH_SIZE 32
182 #include "aoutx.h"
183
184 /* Since the hpux symbol table has nlist elements interspersed with
185    strings and we need to insert som strings for secondary symbols, we
186    give ourselves a little extra padding up front to account for
187    this.  Note that for each non-secondary symbol we process, we gain
188    9 bytes of space for the discarded nlist element (one byte used for
189    null).  SYM_EXTRA_BYTES is the extra space.  */
190 #define SYM_EXTRA_BYTES   1024
191
192 /* Set parameters about this a.out file that are machine-dependent.
193    This routine is called from some_aout_object_p just before it returns.  */
194 static bfd_target *
195 MY (callback) (abfd)
196      bfd *abfd;
197 {
198   struct internal_exec *execp = exec_hdr (abfd);
199
200   /* Calculate the file positions of the parts of a newly read aout header */
201   obj_textsec (abfd)->_raw_size = N_TXTSIZE (*execp);
202
203   /* The virtual memory addresses of the sections */
204   obj_textsec (abfd)->vma = N_TXTADDR (*execp);
205   obj_datasec (abfd)->vma = N_DATADDR (*execp);
206   obj_bsssec (abfd)->vma = N_BSSADDR (*execp);
207
208   /* The file offsets of the sections */
209   obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
210   obj_datasec (abfd)->filepos = N_DATOFF (*execp);
211
212   /* The file offsets of the relocation info */
213   obj_textsec (abfd)->rel_filepos = N_TRELOFF (*execp);
214   obj_datasec (abfd)->rel_filepos = N_DRELOFF (*execp);
215
216   /* The file offsets of the string table and symbol table.  */
217   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
218   obj_str_filepos (abfd) = N_STROFF (*execp);
219
220   /* Determine the architecture and machine type of the object file.  */
221 #ifdef SET_ARCH_MACH
222   SET_ARCH_MACH (abfd, *execp);
223 #else
224   bfd_default_set_arch_mach (abfd, DEFAULT_ARCH, 0);
225 #endif
226
227
228   if (obj_aout_subformat (abfd) == gnu_encap_format)
229     {
230       /* The file offsets of the relocation info */
231       obj_textsec (abfd)->rel_filepos = N_GNU_TRELOFF (*execp);
232       obj_datasec (abfd)->rel_filepos = N_GNU_DRELOFF (*execp);
233
234       /* The file offsets of the string table and symbol table.  */
235       obj_sym_filepos (abfd) = N_GNU_SYMOFF (*execp);
236       obj_str_filepos (abfd) = (obj_sym_filepos (abfd) + execp->a_syms);
237
238       abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
239       bfd_get_symcount (abfd) = execp->a_syms / 12;
240       obj_symbol_entry_size (abfd) = 12;
241       obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
242     }
243
244   return abfd->xvec;
245 }
246
247 extern boolean aout_32_write_syms PARAMS ((bfd * abfd));
248
249 static boolean
250 MY (write_object_contents) (abfd)
251      bfd *abfd;
252 {
253   struct external_exec exec_bytes;
254   struct internal_exec *execp = exec_hdr (abfd);
255   bfd_size_type text_size;      /* dummy vars */
256   file_ptr text_end;
257
258   memset (&exec_bytes, 0, sizeof (exec_bytes));
259 #if CHOOSE_RELOC_SIZE
260   CHOOSE_RELOC_SIZE (abfd);
261 #else
262   obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
263 #endif
264
265   if (adata (abfd).magic == undecided_magic)
266     NAME (aout, adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
267   execp->a_syms = 0;
268
269   execp->a_entry = bfd_get_start_address (abfd);
270
271   execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *
272                      obj_reloc_entry_size (abfd));
273   execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *
274                      obj_reloc_entry_size (abfd));
275
276   N_SET_MACHTYPE (*execp, 0xc);
277   N_SET_FLAGS (*execp, 0x2);
278
279   NAME (aout, swap_exec_header_out) (abfd, execp, &exec_bytes);
280
281   /* update fields not covered by default swap_exec_header_out */
282
283   /* this is really the sym table size but we store it in drelocs */
284   bfd_h_put_32 (abfd, bfd_get_symcount (abfd) * 12, exec_bytes.e_drelocs);
285
286   bfd_seek (abfd, 0L, false);
287   bfd_write ((PTR) & exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
288
289   /* Write out the symbols, and then the relocs.  We must write out
290        the symbols first so that we know the symbol indices.  */
291
292   if (bfd_get_symcount (abfd) != 0)
293     {
294       /* Skip the relocs to where we want to put the symbols.  */
295       if (bfd_seek (abfd, (file_ptr) N_DRELOFF (*execp) + execp->a_drsize,
296                     SEEK_SET) != 0)
297         return false;
298     }
299
300   if (!MY (write_syms) (abfd))
301     return false;
302
303   if (bfd_get_symcount (abfd) != 0)
304     {
305       bfd_seek (abfd, (long) (N_TRELOFF (*execp)), false);
306
307       if (!NAME (aout, squirt_out_relocs) (abfd, obj_textsec (abfd)))
308         return false;
309       bfd_seek (abfd, (long) (N_DRELOFF (*execp)), false);
310
311       if (!NAME (aout, squirt_out_relocs) (abfd, obj_datasec (abfd)))
312         return false;
313     }
314
315   return true;
316 }
317
318 /* convert the hp symbol type to be the same as aout64.h usage so we */
319 /* can piggyback routines in aoutx.h.                                */
320
321 static void
322 convert_sym_type (sym_pointer, cache_ptr, abfd)
323      struct external_nlist *sym_pointer;
324      aout_symbol_type *cache_ptr;
325      bfd *abfd;
326 {
327   int name_type;
328   int new_type;
329
330   name_type = (cache_ptr->type);
331   new_type = 0;
332
333   if ((name_type & HP_SYMTYPE_ALIGN) != 0)
334     {
335       /* iou_error ("aligned symbol encountered: %s", name);*/
336       name_type = 0;
337     }
338
339   if (name_type == HP_SYMTYPE_FILENAME)
340     new_type = N_FN;
341   else
342     {
343       switch (name_type & HP_SYMTYPE_TYPE)
344         {
345         case HP_SYMTYPE_UNDEFINED:
346           new_type = N_UNDF;
347           break;
348
349         case HP_SYMTYPE_ABSOLUTE:
350           new_type = N_ABS;
351           break;
352
353         case HP_SYMTYPE_TEXT:
354           new_type = N_TEXT;
355           break;
356
357         case HP_SYMTYPE_DATA:
358           new_type = N_DATA;
359           break;
360
361         case HP_SYMTYPE_BSS:
362           new_type = N_BSS;
363           break;
364
365         case HP_SYMTYPE_COMMON:
366           new_type = N_COMM;
367           break;
368
369         default:
370           fprintf (stderr, "unknown symbol type encountered: %x", name_type);
371         }
372       if (name_type & HP_SYMTYPE_EXTERNAL)
373         new_type |= N_EXT;
374
375       if (name_type & HP_SECONDARY_SYMBOL)
376         new_type = (new_type & ~N_TYPE) | N_INDR;
377     }
378   cache_ptr->type = new_type;
379
380 }
381
382
383 /*
384 DESCRIPTION
385         Swaps the information in an executable header taken from a raw
386         byte stream memory image, into the internal exec_header
387         structure.
388 */
389
390 void
391   NAME (aout, swap_exec_header_in) (abfd, raw_bytes, execp)
392      bfd *abfd;
393      struct external_exec *raw_bytes;
394      struct internal_exec *execp;
395 {
396   struct external_exec *bytes = (struct external_exec *) raw_bytes;
397
398   /* The internal_exec structure has some fields that are unused in this
399      configuration (IE for i960), so ensure that all such uninitialized
400      fields are zero'd out.  There are places where two of these structs
401      are memcmp'd, and thus the contents do matter. */
402   memset (execp, 0, sizeof (struct internal_exec));
403   /* Now fill in fields in the execp, from the bytes in the raw data.  */
404   execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
405   execp->a_text = GET_WORD (abfd, bytes->e_text);
406   execp->a_data = GET_WORD (abfd, bytes->e_data);
407   execp->a_bss = GET_WORD (abfd, bytes->e_bss);
408   execp->a_syms = GET_WORD (abfd, bytes->e_syms);
409   execp->a_entry = GET_WORD (abfd, bytes->e_entry);
410   execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
411   execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
412
413   /***************************************************************/
414   /* check the header to see if it was generated by a bfd output */
415   /* this is detected rather bizarely by requiring a bunch of    */
416   /* header fields to be zero and an old unused field (now used) */
417   /* to be set.                                                  */
418   /***************************************************************/
419   do
420     {
421       long syms;
422       struct aout_data_struct *rawptr;
423       if (bfd_h_get_32 (abfd, bytes->e_passize) != 0)
424         break;
425       if (bfd_h_get_32 (abfd, bytes->e_syms) != 0)
426         break;
427       if (bfd_h_get_32 (abfd, bytes->e_supsize) != 0)
428         break;
429
430       syms = bfd_h_get_32 (abfd, bytes->e_drelocs);
431       if (syms == 0)
432         break;
433
434       /* OK, we've passed the test as best as we can determine */
435       execp->a_syms = syms;
436
437       /* allocate storage for where we will store this result */
438       rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (*rawptr));
439
440       if (rawptr == NULL)
441         {
442           bfd_set_error (bfd_error_no_memory);
443           return;
444         }
445       abfd->tdata.aout_data = rawptr;
446       obj_aout_subformat (abfd) = gnu_encap_format;
447     }
448   while (0);
449 }
450
451
452 /* The hp symbol table is a bit different than other a.out targets.  Instead
453    of having an array of nlist items and an array of strings, hp's format
454    has them mixed together in one structure.  In addition, the strings are
455    not null terminated.  It looks something like this:
456
457    nlist element 1
458    string1
459    nlist element 2
460    string2
461    ...
462
463    The whole symbol table is read as one chunk and then we march thru it
464    and convert it to canonical form.  As we march thru the table, we copy
465    the nlist data into the internal form and we compact the strings and null
466    terminate them, using storage from the already allocated symbol table:
467
468    string1
469    null
470    string2
471    null
472    ...
473 */
474
475 boolean
476 MY (slurp_symbol_table) (abfd)
477      bfd *abfd;
478 {
479   bfd_size_type symbol_bytes;
480   struct external_nlist *syms;
481   struct external_nlist *sym_pointer;
482   struct external_nlist *sym_end;
483   char *strings;
484   aout_symbol_type *cached;
485   unsigned num_syms = 0;
486   unsigned num_secondary = 0;
487
488   /* If there's no work to be done, don't do any */
489   if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
490     return true;
491   symbol_bytes = exec_hdr (abfd)->a_syms;
492   if (symbol_bytes == 0)
493     {
494       bfd_set_error (bfd_error_no_symbols);
495       return false;
496     }
497
498   strings = (char *) bfd_alloc (abfd,
499                                 symbol_bytes + SYM_EXTRA_BYTES);
500   if (!strings)
501     {
502       bfd_set_error (bfd_error_no_memory);
503       return false;
504     }
505   syms = (struct external_nlist *) (strings + SYM_EXTRA_BYTES);
506   bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET);
507   if (bfd_read ((PTR) syms, symbol_bytes, 1, abfd) != symbol_bytes)
508     {
509       bfd_release (abfd, syms);
510       return false;
511     }
512
513
514   sym_end = (struct external_nlist *) (((char *) syms) + symbol_bytes);
515
516   /* first, march thru the table and figure out how many symbols there are */
517   for (sym_pointer = syms; sym_pointer < sym_end; sym_pointer++, num_syms++)
518     {
519       if (bfd_get_8 (abfd, sym_pointer->e_type) & HP_SECONDARY_SYMBOL)
520         num_secondary++;
521       /* skip over the embedded symbol. */
522       sym_pointer = (struct external_nlist *) (((char *) sym_pointer) +
523                                                sym_pointer->e_length[0]);
524     }
525
526   /* now that we know the symbol count, update the bfd header */
527   bfd_get_symcount (abfd) = num_syms + num_secondary;
528
529   cached = (aout_symbol_type *)
530     bfd_zalloc (abfd, (bfd_size_type) (bfd_get_symcount (abfd) *
531                                        sizeof (aout_symbol_type)));
532   if (!cached)
533     {
534       bfd_set_error (bfd_error_no_memory);
535       return false;
536     }
537
538   /* as we march thru the hp symbol table, convert it into a list of
539      null terminated strings to hold the symbol names.  Make sure any
540      assignment to the strings pointer is done after we're thru using
541      the nlist so we don't overwrite anything important. */
542   num_secondary = 0;
543
544   /* OK, now walk the new symtable, cacheing symbol properties */
545   {
546     aout_symbol_type *cache_ptr = cached;
547     aout_symbol_type cache_save;
548     /* Run through table and copy values */
549     for (sym_pointer = syms, cache_ptr = cached;
550          sym_pointer < sym_end; sym_pointer++, cache_ptr++)
551       {
552         unsigned int length;
553         cache_ptr->symbol.the_bfd = abfd;
554         cache_ptr->symbol.value = GET_SWORD (abfd, sym_pointer->e_value);
555         cache_ptr->desc = bfd_get_16 (abfd, sym_pointer->e_almod);
556         cache_ptr->type = bfd_get_8 (abfd, sym_pointer->e_type);
557         cache_ptr->symbol.udata = 0;
558         length = bfd_get_8 (abfd, sym_pointer->e_length);
559         cache_ptr->other = length;      /* other not used, save length here */
560
561         cache_save = *cache_ptr;
562         convert_sym_type (sym_pointer, cache_ptr, abfd);
563         if (!translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd))
564           return false;
565
566         /********************************************************/
567         /* for hpux, the 'lenght' value indicates the length of */
568         /* the symbol name which follows the nlist entry.       */
569         /********************************************************/
570         if (length)
571           {
572             /**************************************************************/
573             /* the hp string is not null terminated so we create a new one*/
574             /* by copying the string to overlap the just vacated nlist    */
575             /* structure before it in memory.                             */
576             /**************************************************************/
577             cache_ptr->symbol.name = strings;
578             memcpy (strings, sym_pointer + 1, length);
579             strings[length] = '\0';
580             strings += length + 1;
581           }
582         else
583           cache_ptr->symbol.name = (char *) NULL;
584
585         /**********************************************************/
586         /* this is a bit of a kludge, but a secondary hp symbol   */
587         /* gets translated into a gnu indirect symbol.  When this */
588         /* happens, we need to create a "dummy" record to which   */
589         /* we can point the indirect symbol to.                   */
590         /**********************************************************/
591         if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT))
592           {
593             aout_symbol_type *cache_ptr2 = cached + num_syms + num_secondary;
594
595             num_secondary++;
596
597             /* aoutx.h assumes the "next" value is the indirect sym  */
598             /* since we don't want to disturb the order by inserting */
599             /* a new symbol, we tack on the created secondary syms   */
600             /* at the end.                                           */
601             cache_ptr->symbol.value = (bfd_vma) (cache_ptr2);
602             *cache_ptr2 = cache_save;
603             cache_ptr2->symbol.name = strings;
604             memcpy (strings, cache_ptr->symbol.name, length);
605             strcpy (strings + length, ":secondry");     /* 9 max chars + null */
606             strings += length + 10;
607             cache_ptr2->type &= ~HP_SECONDARY_SYMBOL;   /* clear secondary */
608             convert_sym_type (sym_pointer, cache_ptr2, abfd);
609             if (!translate_from_native_sym_flags (sym_pointer, cache_ptr2,
610                                                   abfd))
611               return false;
612           }
613
614         /* skip over the embedded symbol. */
615         sym_pointer = (struct external_nlist *) (((char *) sym_pointer) +
616                                                  length);
617       }
618   }
619
620   obj_aout_symbols (abfd) = cached;
621
622   return true;
623 }
624
625
626
627 void
628 MY (swap_std_reloc_in) (abfd, bytes, cache_ptr, symbols)
629      bfd *abfd;
630      struct hp300hpux_reloc *bytes;
631      arelent *cache_ptr;
632      asymbol **symbols;
633 {
634   int r_index;
635   int r_extern = 0;
636   unsigned int r_length;
637   int r_pcrel = 0;
638   struct aoutdata *su = &(abfd->tdata.aout_data->a);
639
640   cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
641   r_index = bfd_h_get_16 (abfd, bytes->r_index);
642
643   switch (bytes->r_type[0])
644     {
645     case HP_RSEGMENT_TEXT:
646       r_index = N_TEXT;
647       break;
648     case HP_RSEGMENT_DATA:
649       r_index = N_DATA;
650       break;
651     case HP_RSEGMENT_BSS:
652       r_index = N_BSS;
653       break;
654     case HP_RSEGMENT_EXTERNAL:
655       r_extern = 1;
656       break;
657     case HP_RSEGMENT_PCREL:
658       r_extern = 1;
659       r_pcrel = 1;
660       break;
661     case HP_RSEGMENT_RDLT:
662       break;
663     case HP_RSEGMENT_RPLT:
664       break;
665     case HP_RSEGMENT_NOOP:
666       break;
667     default:
668       fprintf (stderr, "illegal relocation segment type: %x\n",
669                (bytes->r_type[0]));
670     }
671
672   switch (bytes->r_length[0])
673     {
674     case HP_RLENGTH_BYTE:
675       r_length = 0;
676       break;
677     case HP_RLENGTH_WORD:
678       r_length = 1;
679       break;
680     case HP_RLENGTH_LONG:
681       r_length = 2;
682       break;
683     default:
684       fprintf (stderr, "illegal relocation length: %x\n", bytes->r_length[0]);
685       r_length = 0;
686     }
687
688   cache_ptr->howto = howto_table_std + r_length + 4 * r_pcrel;
689   /* FIXME-soon:  Roll baserel, jmptable, relative bits into howto setting */
690
691   /* This macro uses the r_index value computed above */
692   if (r_pcrel && r_extern)
693     {
694       /* The GNU linker assumes any offset from beginning of section */
695       /* is already incorporated into the image while the HP linker  */
696       /* adds this in later.  Add it in now...                       */
697       MOVE_ADDRESS (-cache_ptr->address);
698     }
699   else
700     {
701       MOVE_ADDRESS (0);
702     }
703 }
704
705 boolean
706 MY (slurp_reloc_table) (abfd, asect, symbols)
707      bfd *abfd;
708      sec_ptr asect;
709      asymbol **symbols;
710 {
711   unsigned int count;
712   bfd_size_type reloc_size;
713   PTR relocs;
714   arelent *reloc_cache;
715   size_t each_size;
716   struct hp300hpux_reloc *rptr;
717   unsigned int counter;
718   arelent *cache_ptr;
719
720   if (asect->relocation)
721     return true;
722
723   if (asect->flags & SEC_CONSTRUCTOR)
724     return true;
725
726   if (asect == obj_datasec (abfd))
727     {
728       reloc_size = exec_hdr (abfd)->a_drsize;
729       goto doit;
730     }
731
732   if (asect == obj_textsec (abfd))
733     {
734       reloc_size = exec_hdr (abfd)->a_trsize;
735       goto doit;
736     }
737
738   bfd_set_error (bfd_error_invalid_operation);
739   return false;
740
741 doit:
742   bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
743   each_size = obj_reloc_entry_size (abfd);
744
745   count = reloc_size / each_size;
746
747
748   reloc_cache = (arelent *) bfd_zalloc (abfd, (size_t) (count * sizeof
749                                                         (arelent)));
750   if (!reloc_cache)
751     {
752     nomem:
753       bfd_set_error (bfd_error_no_memory);
754       return false;
755     }
756
757   relocs = (PTR) bfd_alloc (abfd, reloc_size);
758   if (!relocs)
759     {
760       bfd_release (abfd, reloc_cache);
761       goto nomem;
762     }
763
764   if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size)
765     {
766       bfd_release (abfd, relocs);
767       bfd_release (abfd, reloc_cache);
768       return false;
769     }
770
771   rptr = (struct hp300hpux_reloc *) relocs;
772   counter = 0;
773   cache_ptr = reloc_cache;
774
775   for (; counter < count; counter++, rptr++, cache_ptr++)
776     {
777       MY (swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols);
778     }
779
780
781   bfd_release (abfd, relocs);
782   asect->relocation = reloc_cache;
783   asect->reloc_count = count;
784   return true;
785 }
786
787
788 /************************************************************************/
789 /* The following functions are identical to functions in aoutx.h except */
790 /* they refer to MY(func) rather than NAME(aout,func) and they also     */
791 /* call aout_32 versions if the input file was generated by gcc         */
792 /************************************************************************/
793
794 long aout_32_get_symtab PARAMS ((bfd * abfd, asymbol ** location));
795 long aout_32_get_symtab_upper_bound PARAMS ((bfd * abfd));
796
797 long aout_32_canonicalize_reloc PARAMS ((bfd * abfd, sec_ptr section,
798                                          arelent ** relptr,
799                                          asymbol ** symbols));
800
801 long
802 MY (get_symtab) (abfd, location)
803      bfd *abfd;
804      asymbol **location;
805 {
806   unsigned int counter = 0;
807   aout_symbol_type *symbase;
808
809   if (obj_aout_subformat (abfd) == gnu_encap_format)
810     return aout_32_get_symtab (abfd, location);
811
812   if (!MY (slurp_symbol_table) (abfd))
813     return -1;
814
815   for (symbase = obj_aout_symbols (abfd); counter++ < bfd_get_symcount (abfd);)
816     *(location++) = (asymbol *) (symbase++);
817   *location++ = 0;
818   return bfd_get_symcount (abfd);
819 }
820
821 long
822 MY (get_symtab_upper_bound) (abfd)
823      bfd *abfd;
824 {
825   if (obj_aout_subformat (abfd) == gnu_encap_format)
826     return aout_32_get_symtab_upper_bound (abfd);
827   if (!MY (slurp_symbol_table) (abfd))
828     return -1;
829
830   return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *));
831 }
832
833
834
835
836 long
837 MY (canonicalize_reloc) (abfd, section, relptr, symbols)
838      bfd *abfd;
839      sec_ptr section;
840      arelent **relptr;
841      asymbol **symbols;
842 {
843   arelent *tblptr = section->relocation;
844   unsigned int count;
845   if (obj_aout_subformat (abfd) == gnu_encap_format)
846     return aout_32_canonicalize_reloc (abfd, section, relptr, symbols);
847
848   if (!(tblptr || MY (slurp_reloc_table) (abfd, section, symbols)))
849     return -1;
850
851   if (section->flags & SEC_CONSTRUCTOR)
852     {
853       arelent_chain *chain = section->constructor_chain;
854       for (count = 0; count < section->reloc_count; count++)
855         {
856           *relptr++ = &chain->relent;
857           chain = chain->next;
858         }
859     }
860   else
861     {
862       tblptr = section->relocation;
863       if (!tblptr)
864         return -1;
865
866       for (count = 0; count++ < section->reloc_count;)
867         {
868           *relptr++ = tblptr++;
869         }
870     }
871   *relptr = 0;
872
873   return section->reloc_count;
874 }
875
876
877 #include "aout-target.h"