Make all callers of malloc or realloc (including via obstacks)
[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_close_and_cleanup aout_32_close_and_cleanup
123 #define MY_slurp_armap bfd_slurp_bsd_armap_f2
124
125 /***********************************************/
126 /* provide overrides for routines in this file */
127 /***********************************************/
128 /* these don't use MY because that causes problems within JUMP_TABLE
129    (CAT winds up being expanded recursively, which ANSI C compilers
130    will not do).  */
131 #define MY_get_symtab hp300hpux_get_symtab
132 #define MY_get_symtab_upper_bound hp300hpux_get_symtab_upper_bound
133 #define MY_canonicalize_reloc hp300hpux_canonicalize_reloc
134 #define MY_write_object_contents hp300hpux_write_object_contents
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_bfd_final_link _bfd_generic_final_link
139
140 #define hp300hpux_write_syms aout_32_write_syms
141
142 #define MY_callback MY(callback)
143
144 #define NAME_swap_exec_header_in NAME(hp300hpux_32_,swap_exec_header_in)
145
146 #define HP_SYMTYPE_UNDEFINED    0x00
147 #define HP_SYMTYPE_ABSOLUTE     0x01
148 #define HP_SYMTYPE_TEXT         0x02
149 #define HP_SYMTYPE_DATA         0x03
150 #define HP_SYMTYPE_BSS          0x04
151 #define HP_SYMTYPE_COMMON       0x05
152
153 #define HP_SYMTYPE_TYPE         0x0F
154 #define HP_SYMTYPE_FILENAME     0x1F
155
156 #define HP_SYMTYPE_ALIGN        0x10
157 #define HP_SYMTYPE_EXTERNAL     0x20
158 #define HP_SECONDARY_SYMBOL     0x40
159
160 /* RELOCATION DEFINITIONS */
161 #define HP_RSEGMENT_TEXT        0x00
162 #define HP_RSEGMENT_DATA        0x01
163 #define HP_RSEGMENT_BSS         0x02
164 #define HP_RSEGMENT_EXTERNAL    0x03
165 #define HP_RSEGMENT_PCREL       0x04
166 #define HP_RSEGMENT_RDLT        0x05
167 #define HP_RSEGMENT_RPLT        0x06
168 #define HP_RSEGMENT_NOOP        0x3F
169
170 #define HP_RLENGTH_BYTE         0x00
171 #define HP_RLENGTH_WORD         0x01
172 #define HP_RLENGTH_LONG         0x02
173 #define HP_RLENGTH_ALIGN        0x03
174
175 #define NAME(x,y) CAT3(hp300hpux,_32_,y)
176 #define ARCH_SIZE 32
177 #include "aoutx.h"
178
179 /* Since the hpux symbol table has nlist elements interspersed with
180    strings and we need to insert som strings for secondary symbols, we
181    give ourselves a little extra padding up front to account for
182    this.  Note that for each non-secondary symbol we process, we gain
183    9 bytes of space for the discarded nlist element (one byte used for
184    null).  SYM_EXTRA_BYTES is the extra space.  */
185 #define SYM_EXTRA_BYTES   1024
186
187 /* Set parameters about this a.out file that are machine-dependent.
188    This routine is called from some_aout_object_p just before it returns.  */
189 static bfd_target *
190 DEFUN(MY(callback),(abfd),
191       bfd *abfd)
192 {
193   struct internal_exec *execp = exec_hdr (abfd);
194
195   /* Calculate the file positions of the parts of a newly read aout header */
196   obj_textsec (abfd)->_raw_size = N_TXTSIZE(*execp);
197
198   /* The virtual memory addresses of the sections */
199   obj_textsec (abfd)->vma = N_TXTADDR(*execp);
200   obj_datasec (abfd)->vma = N_DATADDR(*execp);
201   obj_bsssec  (abfd)->vma = N_BSSADDR(*execp);
202
203   /* The file offsets of the sections */
204   obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
205   obj_datasec (abfd)->filepos = N_DATOFF (*execp);
206
207   /* The file offsets of the relocation info */
208   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
209   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
210
211   /* The file offsets of the string table and symbol table.  */
212   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
213   obj_str_filepos (abfd) = N_STROFF (*execp);
214
215   /* Determine the architecture and machine type of the object file.  */
216 #ifdef SET_ARCH_MACH
217   SET_ARCH_MACH(abfd, *execp);
218 #else
219   bfd_default_set_arch_mach(abfd, DEFAULT_ARCH, 0);
220 #endif
221
222
223   if (obj_aout_subformat(abfd) == gnu_encap_format)
224   {
225       /* The file offsets of the relocation info */
226       obj_textsec (abfd)->rel_filepos = N_GNU_TRELOFF(*execp);
227       obj_datasec (abfd)->rel_filepos = N_GNU_DRELOFF(*execp);
228
229       /* The file offsets of the string table and symbol table.  */
230       obj_sym_filepos (abfd) = N_GNU_SYMOFF(*execp);
231       obj_str_filepos (abfd) = (obj_sym_filepos (abfd) + execp->a_syms);
232
233       abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
234       bfd_get_symcount (abfd) = execp->a_syms / 12;
235       obj_symbol_entry_size (abfd) = 12;
236       obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
237   }
238
239   return abfd->xvec;
240 }
241
242 extern boolean aout_32_write_syms PARAMS ((bfd *abfd));
243
244 static boolean
245 DEFUN(MY(write_object_contents),(abfd),
246       bfd *abfd)
247 {
248     struct external_exec exec_bytes;
249     struct internal_exec *execp = exec_hdr (abfd);
250     bfd_size_type text_size; /* dummy vars */                   
251     file_ptr text_end;
252
253     memset (&exec_bytes, 0, sizeof (exec_bytes));
254 #if CHOOSE_RELOC_SIZE
255     CHOOSE_RELOC_SIZE(abfd);
256 #else
257     obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
258 #endif
259
260     if (adata(abfd).magic == undecided_magic)                   
261         NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
262     execp->a_syms = 0;
263
264     execp->a_entry = bfd_get_start_address (abfd);                      
265
266     execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *              
267                        obj_reloc_entry_size (abfd));            
268     execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *              
269                        obj_reloc_entry_size (abfd));            
270
271     N_SET_MACHTYPE(*execp, 0xc);
272     N_SET_FLAGS (*execp, 0x2);
273
274     NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes); 
275
276     /* update fields not covered by default swap_exec_header_out */
277
278     /* this is really the sym table size but we store it in drelocs */
279     bfd_h_put_32 (abfd,  bfd_get_symcount (abfd) * 12, exec_bytes.e_drelocs);
280
281     bfd_seek (abfd, 0L, false);                                 
282     bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
283
284     /* Write out the symbols, and then the relocs.  We must write out
285        the symbols first so that we know the symbol indices.  */
286
287     if (bfd_get_symcount (abfd) != 0)
288       {
289         /* Skip the relocs to where we want to put the symbols.  */
290         if (bfd_seek (abfd, (file_ptr) N_DRELOFF (*execp) + execp->a_drsize,
291                       SEEK_SET) != 0)
292           return false;
293       }
294
295     if (! MY(write_syms) (abfd))
296       return false;
297
298     if (bfd_get_symcount (abfd) != 0)                           
299     {                                                           
300         bfd_seek (abfd, (long)(N_TRELOFF(*execp)), false);      
301
302         if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) return false;
303         bfd_seek (abfd, (long)(N_DRELOFF(*execp)), false);      
304
305         if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd))) return false;
306     }
307
308     return true;
309 }                                                                       
310
311 /* convert the hp symbol type to be the same as aout64.h usage so we */
312 /* can piggyback routines in aoutx.h.                                */
313
314 static void
315 DEFUN(convert_sym_type,(sym_pointer, cache_ptr, abfd),
316       struct external_nlist *sym_pointer AND
317       aout_symbol_type *cache_ptr AND
318       bfd *abfd)
319 {
320     int name_type;
321     int new_type;
322
323     name_type = (cache_ptr->type);
324     new_type  = 0;
325
326     if ((name_type & HP_SYMTYPE_ALIGN) != 0)
327     {
328         /* iou_error ("aligned symbol encountered: %s", name);*/
329         name_type = 0;
330     }
331
332     if (name_type == HP_SYMTYPE_FILENAME)
333       new_type = N_FN;
334     else
335       {
336         switch (name_type & HP_SYMTYPE_TYPE)
337           {
338           case HP_SYMTYPE_UNDEFINED:
339             new_type = N_UNDF;
340             break;
341
342           case HP_SYMTYPE_ABSOLUTE:
343             new_type = N_ABS;
344             break;
345
346           case HP_SYMTYPE_TEXT:
347             new_type = N_TEXT;
348             break;
349
350           case HP_SYMTYPE_DATA:
351             new_type = N_DATA;
352             break;
353
354           case HP_SYMTYPE_BSS:
355             new_type = N_BSS;
356             break;
357
358           case HP_SYMTYPE_COMMON:
359             new_type = N_COMM;
360             break;
361
362           default:
363             fprintf (stderr, "unknown symbol type encountered: %x", name_type);
364           }
365         if (name_type & HP_SYMTYPE_EXTERNAL)
366           new_type |= N_EXT;
367
368         if (name_type & HP_SECONDARY_SYMBOL)
369             new_type = (new_type & ~N_TYPE) | N_INDR;
370       }
371     cache_ptr->type = new_type;
372
373 }
374
375
376 /*
377 DESCRIPTION
378         Swaps the information in an executable header taken from a raw
379         byte stream memory image, into the internal exec_header
380         structure.
381 */
382
383 void
384 DEFUN(NAME(aout,swap_exec_header_in),(abfd, raw_bytes, execp),
385       bfd *abfd AND
386       struct external_exec *raw_bytes AND
387       struct internal_exec *execp)
388 {
389   struct external_exec *bytes = (struct external_exec *)raw_bytes;
390
391   /* The internal_exec structure has some fields that are unused in this
392      configuration (IE for i960), so ensure that all such uninitialized
393      fields are zero'd out.  There are places where two of these structs
394      are memcmp'd, and thus the contents do matter. */
395   memset (execp, 0, sizeof (struct internal_exec));
396   /* Now fill in fields in the execp, from the bytes in the raw data.  */
397   execp->a_info   = bfd_h_get_32 (abfd, bytes->e_info);
398   execp->a_text   = GET_WORD (abfd, bytes->e_text);
399   execp->a_data   = GET_WORD (abfd, bytes->e_data);
400   execp->a_bss    = GET_WORD (abfd, bytes->e_bss);
401   execp->a_syms   = GET_WORD (abfd, bytes->e_syms);
402   execp->a_entry  = GET_WORD (abfd, bytes->e_entry);
403   execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
404   execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
405
406   /***************************************************************/
407   /* check the header to see if it was generated by a bfd output */
408   /* this is detected rather bizarely by requiring a bunch of    */
409   /* header fields to be zero and an old unused field (now used) */
410   /* to be set.                                                  */
411   /***************************************************************/
412   do
413   {
414       long syms;
415       struct aout_data_struct  *rawptr;
416       if (bfd_h_get_32 (abfd, bytes->e_passize) != 0) break;
417       if (bfd_h_get_32 (abfd, bytes->e_syms) != 0) break;
418       if (bfd_h_get_32 (abfd, bytes->e_supsize) != 0) break;
419
420       syms = bfd_h_get_32 (abfd, bytes->e_drelocs);
421       if (syms == 0) break;
422
423       /* OK, we've passed the test as best as we can determine */
424       execp->a_syms = syms;
425
426       /* allocate storage for where we will store this result */
427       rawptr = (struct aout_data_struct  *) bfd_zalloc (abfd, sizeof (*rawptr));
428
429       if (rawptr == NULL) {
430           bfd_error = no_memory;
431           return;
432       }
433       abfd->tdata.aout_data = rawptr;
434       obj_aout_subformat(abfd) = gnu_encap_format;
435   } while (0);
436 }
437
438
439 /* The hp symbol table is a bit different than other a.out targets.  Instead
440    of having an array of nlist items and an array of strings, hp's format
441    has them mixed together in one structure.  In addition, the strings are
442    not null terminated.  It looks something like this:
443
444    nlist element 1
445    string1
446    nlist element 2
447    string2
448    ...
449
450    The whole symbol table is read as one chunk and then we march thru it
451    and convert it to canonical form.  As we march thru the table, we copy
452    the nlist data into the internal form and we compact the strings and null
453    terminate them, using storage from the already allocated symbol table:
454
455    string1
456    null
457    string2
458    null
459    ...
460 */
461
462 boolean
463 DEFUN(MY(slurp_symbol_table),(abfd),
464       bfd *abfd)
465 {
466   bfd_size_type symbol_bytes;
467   struct external_nlist *syms;
468   struct external_nlist *sym_pointer;
469   struct external_nlist *sym_end;
470   char *strings;
471   aout_symbol_type *cached;
472   unsigned num_syms = 0;
473   unsigned num_secondary = 0;
474
475   /* If there's no work to be done, don't do any */
476   if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true;
477   symbol_bytes = exec_hdr(abfd)->a_syms;
478   if (symbol_bytes == 0) {
479     bfd_error = no_symbols;
480     return false;
481   }
482
483   strings = (char *) bfd_alloc(abfd,
484                                symbol_bytes + SYM_EXTRA_BYTES);
485   if (!strings)
486     {
487       bfd_error = no_memory;
488       return false;
489     }
490   syms = (struct external_nlist *) (strings + SYM_EXTRA_BYTES);
491   bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET);
492   if (bfd_read ((PTR)syms, symbol_bytes, 1, abfd) != symbol_bytes)
493   {
494       bfd_release (abfd, syms);
495       return false;
496   }
497
498
499   sym_end = (struct external_nlist *) (((char *)syms) + symbol_bytes);
500
501   /* first, march thru the table and figure out how many symbols there are */
502   for (sym_pointer = syms; sym_pointer < sym_end; sym_pointer++, num_syms++)
503   {
504       if (bfd_get_8(abfd,  sym_pointer->e_type) & HP_SECONDARY_SYMBOL)
505           num_secondary++;
506       /* skip over the embedded symbol. */
507       sym_pointer = (struct external_nlist *) (((char *)sym_pointer) +
508                                                      sym_pointer->e_length[0]);
509   }
510
511   /* now that we know the symbol count, update the bfd header */
512   bfd_get_symcount (abfd) = num_syms+num_secondary;
513
514   cached = (aout_symbol_type *)
515     bfd_zalloc(abfd, (bfd_size_type)(bfd_get_symcount (abfd) *
516                                      sizeof(aout_symbol_type)));
517   if (!cached)
518     {
519       bfd_error = no_memory;
520       return false;
521     }
522
523   /* as we march thru the hp symbol table, convert it into a list of
524      null terminated strings to hold the symbol names.  Make sure any
525      assignment to the strings pointer is done after we're thru using
526      the nlist so we don't overwrite anything important. */
527   num_secondary = 0;
528
529   /* OK, now walk the new symtable, cacheing symbol properties */
530     {
531       aout_symbol_type *cache_ptr = cached;
532       aout_symbol_type cache_save;
533       /* Run through table and copy values */
534       for (sym_pointer = syms, cache_ptr = cached;
535            sym_pointer < sym_end; sym_pointer++, cache_ptr++)
536           {
537             unsigned int length;
538             cache_ptr->symbol.the_bfd = abfd;
539             cache_ptr->symbol.value = GET_SWORD(abfd,  sym_pointer->e_value);
540             cache_ptr->desc = bfd_get_16(abfd, sym_pointer->e_almod);
541             cache_ptr->type = bfd_get_8(abfd,  sym_pointer->e_type);
542             cache_ptr->symbol.udata = 0;
543             length = bfd_get_8(abfd, sym_pointer->e_length);
544             cache_ptr->other = length;  /* other not used, save length here */
545
546             cache_save = *cache_ptr;
547             convert_sym_type(sym_pointer, cache_ptr, abfd);
548             if (!translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd))
549               return false;
550
551             /********************************************************/
552             /* for hpux, the 'lenght' value indicates the length of */
553             /* the symbol name which follows the nlist entry.       */
554             /********************************************************/
555             if (length)
556             {
557                 /**************************************************************/
558                 /* the hp string is not null terminated so we create a new one*/
559                 /* by copying the string to overlap the just vacated nlist    */
560                 /* structure before it in memory.                             */
561                 /**************************************************************/
562                 cache_ptr->symbol.name = strings;
563                 memcpy(strings, sym_pointer+1, length);
564                 strings[length] = '\0';
565                 strings += length + 1;
566             }
567             else
568               cache_ptr->symbol.name = (char *)NULL;
569
570             /**********************************************************/
571             /* this is a bit of a kludge, but a secondary hp symbol   */
572             /* gets translated into a gnu indirect symbol.  When this */
573             /* happens, we need to create a "dummy" record to which   */
574             /* we can point the indirect symbol to.                   */
575             /**********************************************************/
576             if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT))
577             {
578                 aout_symbol_type *cache_ptr2 = cached + num_syms+num_secondary;
579
580                 num_secondary++;
581
582                 /* aoutx.h assumes the "next" value is the indirect sym  */
583                 /* since we don't want to disturb the order by inserting */
584                 /* a new symbol, we tack on the created secondary syms   */
585                 /* at the end.                                           */
586                 cache_ptr->symbol.value = (bfd_vma)(cache_ptr2);
587                 *cache_ptr2 = cache_save;
588                 cache_ptr2->symbol.name = strings;
589                 memcpy(strings, cache_ptr->symbol.name, length);
590                 strcpy(strings+length,":secondry"); /* 9 max chars + null */
591                 strings += length+10;
592                 cache_ptr2->type &= ~HP_SECONDARY_SYMBOL;  /* clear secondary */
593                 convert_sym_type(sym_pointer, cache_ptr2, abfd);
594                 if (!translate_from_native_sym_flags (sym_pointer, cache_ptr2,
595                                                       abfd))
596                   return false;
597             }
598
599             /* skip over the embedded symbol. */
600             sym_pointer = (struct external_nlist *) (((char *)sym_pointer) +
601                                                  length);
602           }
603     }
604
605   obj_aout_symbols (abfd) =  cached;
606
607   return true;
608 }
609
610
611
612 void
613 DEFUN(MY(swap_std_reloc_in), (abfd, bytes, cache_ptr, symbols),
614   bfd *abfd AND
615   struct hp300hpux_reloc *bytes AND
616   arelent *cache_ptr AND
617   asymbol **symbols)
618 {
619   int r_index;
620   int r_extern = 0;
621   unsigned int r_length;
622   int r_pcrel = 0;
623   struct aoutdata  *su = &(abfd->tdata.aout_data->a);
624
625   cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
626   r_index = bfd_h_get_16(abfd, bytes->r_index);
627
628   switch (bytes->r_type[0])
629   {
630   case HP_RSEGMENT_TEXT:
631       r_index = N_TEXT;
632       break;
633   case HP_RSEGMENT_DATA:
634       r_index = N_DATA;
635       break;
636   case HP_RSEGMENT_BSS:
637       r_index = N_BSS;
638       break;
639   case HP_RSEGMENT_EXTERNAL:
640       r_extern = 1;
641       break;
642   case HP_RSEGMENT_PCREL:
643       r_extern = 1;
644       r_pcrel = 1;
645       break;
646   case HP_RSEGMENT_RDLT:
647       break;
648   case HP_RSEGMENT_RPLT:
649       break;
650   case HP_RSEGMENT_NOOP:
651       break;
652   default:
653       fprintf (stderr, "illegal relocation segment type: %x\n",
654                (bytes->r_type[0]));
655   }
656
657   switch (bytes->r_length[0])
658     {
659     case HP_RLENGTH_BYTE:
660       r_length = 0;
661       break;
662     case HP_RLENGTH_WORD:
663       r_length = 1;
664       break;
665     case HP_RLENGTH_LONG:
666       r_length = 2;
667       break;
668     default:
669       fprintf (stderr, "illegal relocation length: %x\n",bytes->r_length[0] );
670       r_length = 0;
671     }
672
673   cache_ptr->howto =  howto_table_std + r_length + 4 * r_pcrel;
674   /* FIXME-soon:  Roll baserel, jmptable, relative bits into howto setting */
675
676   /* This macro uses the r_index value computed above */
677   if (r_pcrel && r_extern)
678   {
679     /* The GNU linker assumes any offset from beginning of section */
680     /* is already incorporated into the image while the HP linker  */
681     /* adds this in later.  Add it in now...                       */
682     MOVE_ADDRESS( - cache_ptr->address);
683   }
684   else
685   {
686     MOVE_ADDRESS(0);
687   }
688 }
689
690 boolean
691 DEFUN(MY(slurp_reloc_table),(abfd, asect, symbols),
692       bfd *abfd AND
693       sec_ptr asect AND
694       asymbol **symbols)
695 {
696   unsigned int count;
697   bfd_size_type reloc_size;
698   PTR relocs;
699   arelent *reloc_cache;
700   size_t each_size;
701   struct hp300hpux_reloc *rptr;
702   unsigned int counter;
703   arelent *cache_ptr;
704
705   if (asect->relocation) return true;
706
707   if (asect->flags & SEC_CONSTRUCTOR) return true;
708
709   if (asect == obj_datasec (abfd)) {
710     reloc_size = exec_hdr(abfd)->a_drsize;
711     goto doit;
712   }
713
714   if (asect == obj_textsec (abfd)) {
715     reloc_size = exec_hdr(abfd)->a_trsize;
716     goto doit;
717   }
718
719   bfd_error = invalid_operation;
720   return false;
721
722  doit:
723   bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
724   each_size = obj_reloc_entry_size (abfd);
725
726   count = reloc_size / each_size;
727
728
729   reloc_cache = (arelent *) bfd_zalloc (abfd, (size_t)(count * sizeof
730                                                        (arelent)));
731   if (!reloc_cache) {
732 nomem:
733     bfd_error = no_memory;
734     return false;
735   }
736
737   relocs = (PTR) bfd_alloc (abfd, reloc_size);
738   if (!relocs) {
739     bfd_release (abfd, reloc_cache);
740     goto nomem;
741   }
742
743   if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size) {
744     bfd_release (abfd, relocs);
745     bfd_release (abfd, reloc_cache);
746     bfd_error = system_call_error;
747     return false;
748   }
749
750     rptr = (struct hp300hpux_reloc*) relocs;
751     counter = 0;
752     cache_ptr = reloc_cache;
753
754     for (; counter < count; counter++, rptr++, cache_ptr++) {
755         MY(swap_std_reloc_in)(abfd, rptr, cache_ptr, symbols);
756     }
757
758
759   bfd_release (abfd,relocs);
760   asect->relocation = reloc_cache;
761   asect->reloc_count = count;
762   return true;
763 }
764
765
766 /************************************************************************/
767 /* The following functions are identical to functions in aoutx.h except */
768 /* they refer to MY(func) rather than NAME(aout,func) and they also     */
769 /* call aout_32 versions if the input file was generated by gcc         */
770 /************************************************************************/
771
772 unsigned int aout_32_get_symtab PARAMS((bfd *abfd, asymbol **location));
773 unsigned int aout_32_get_symtab_upper_bound PARAMS ((bfd *abfd));
774
775 unsigned int aout_32_canonicalize_reloc PARAMS((bfd *abfd, sec_ptr section,
776                                                 arelent **relptr,
777                                                 asymbol **symbols));
778
779 unsigned int
780 DEFUN(MY(get_symtab),(abfd, location),
781       bfd *abfd AND
782       asymbol **location)
783 {
784     unsigned int counter = 0;
785     aout_symbol_type *symbase;
786
787     if (obj_aout_subformat(abfd) == gnu_encap_format)
788         return aout_32_get_symtab(abfd,location);
789
790     if (!MY(slurp_symbol_table)(abfd)) return 0;
791
792     for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
793       *(location++) = (asymbol *)( symbase++);
794     *location++ =0;
795     return bfd_get_symcount (abfd);
796 }
797
798  unsigned int
799 DEFUN(MY(get_symtab_upper_bound),(abfd),
800      bfd *abfd)
801 {
802     if (obj_aout_subformat(abfd) == gnu_encap_format)
803         return aout_32_get_symtab_upper_bound(abfd);
804     if (!MY(slurp_symbol_table)(abfd)) return 0;
805
806     return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
807 }
808
809
810
811
812 unsigned int
813 DEFUN(MY(canonicalize_reloc),(abfd, section, relptr, symbols),
814       bfd *abfd AND
815       sec_ptr section AND
816       arelent **relptr AND
817       asymbol **symbols)
818 {
819   arelent *tblptr = section->relocation;
820   unsigned int count;
821   if (obj_aout_subformat(abfd) == gnu_encap_format)
822       return aout_32_canonicalize_reloc(abfd,section,relptr,symbols);
823
824   if (!(tblptr || MY(slurp_reloc_table)(abfd, section, symbols)))
825     return 0;
826
827   if (section->flags & SEC_CONSTRUCTOR) {
828     arelent_chain *chain = section->constructor_chain;
829     for (count = 0; count < section->reloc_count; count ++) {
830       *relptr ++ = &chain->relent;
831       chain = chain->next;
832     }
833   }
834   else {
835     tblptr = section->relocation;
836     if (!tblptr) return 0;
837
838     for (count = 0; count++ < section->reloc_count;)
839       {
840         *relptr++ = tblptr++;
841       }
842   }
843   *relptr = 0;
844
845   return section->reloc_count;
846 }
847
848
849 #include "aout-target.h"