OSDN Git Service

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