]>
Commit | Line | Data |
---|---|---|
bafe0602 | 1 | /* BFD backend for hp-ux 9000/300 |
9783e04a | 2 | Copyright (C) 1990, 1991, 1994 Free Software Foundation, Inc. |
bafe0602 KR |
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 | /* | |
34dd8ba3 | 22 | |
bafe0602 KR |
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. | |
bafe0602 KR |
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 | |
34dd8ba3 | 47 | in the object code while the gnu utilities use a field in the |
bafe0602 KR |
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 | |
34dd8ba3 | 60 | |
bafe0602 KR |
61 | #include "aout-target.h" |
62 | } | |
34dd8ba3 | 63 | |
bafe0602 KR |
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: | |
34dd8ba3 | 68 | |
bafe0602 KR |
69 | nlist element 1 |
70 | string1 | |
71 | nlist element 2 | |
72 | string2 | |
73 | ... | |
34dd8ba3 | 74 | |
bafe0602 KR |
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: | |
34dd8ba3 | 79 | |
bafe0602 KR |
80 | string1 |
81 | null | |
82 | string2 | |
83 | null | |
bafe0602 KR |
84 | */ |
85 | ||
34dd8ba3 JG |
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 | ||
bafe0602 KR |
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 '/' | |
34dd8ba3 | 118 | #define TARGET_IS_BIG_ENDIAN_P |
bafe0602 KR |
119 | #define DEFAULT_ARCH bfd_arch_m68k |
120 | ||
121 | #define MY_get_section_contents aout_32_get_section_contents | |
bafe0602 KR |
122 | #define MY_slurp_armap bfd_slurp_bsd_armap_f2 |
123 | ||
124 | /***********************************************/ | |
125 | /* provide overrides for routines in this file */ | |
126 | /***********************************************/ | |
9783e04a DM |
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 | |
bafe0602 | 138 | |
5c8444f8 ILT |
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 | ||
bafe0602 KR |
145 | #define hp300hpux_write_syms aout_32_write_syms |
146 | ||
147 | #define MY_callback MY(callback) | |
148 | ||
34dd8ba3 | 149 | #define NAME_swap_exec_header_in NAME(hp300hpux_32_,swap_exec_header_in) |
bafe0602 KR |
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 | ||
34dd8ba3 | 180 | #define NAME(x,y) CAT3(hp300hpux,_32_,y) |
9783e04a | 181 | #define ARCH_SIZE 32 |
bafe0602 KR |
182 | #include "aoutx.h" |
183 | ||
34dd8ba3 JG |
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. */ | |
bafe0602 KR |
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 * | |
25057836 JL |
195 | MY (callback) (abfd) |
196 | bfd *abfd; | |
bafe0602 KR |
197 | { |
198 | struct internal_exec *execp = exec_hdr (abfd); | |
199 | ||
200 | /* Calculate the file positions of the parts of a newly read aout header */ | |
25057836 | 201 | obj_textsec (abfd)->_raw_size = N_TXTSIZE (*execp); |
bafe0602 KR |
202 | |
203 | /* The virtual memory addresses of the sections */ | |
25057836 JL |
204 | obj_textsec (abfd)->vma = N_TXTADDR (*execp); |
205 | obj_datasec (abfd)->vma = N_DATADDR (*execp); | |
206 | obj_bsssec (abfd)->vma = N_BSSADDR (*execp); | |
bafe0602 KR |
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 */ | |
25057836 JL |
213 | obj_textsec (abfd)->rel_filepos = N_TRELOFF (*execp); |
214 | obj_datasec (abfd)->rel_filepos = N_DRELOFF (*execp); | |
bafe0602 KR |
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); | |
34dd8ba3 | 219 | |
bafe0602 KR |
220 | /* Determine the architecture and machine type of the object file. */ |
221 | #ifdef SET_ARCH_MACH | |
25057836 | 222 | SET_ARCH_MACH (abfd, *execp); |
bafe0602 | 223 | #else |
25057836 | 224 | bfd_default_set_arch_mach (abfd, DEFAULT_ARCH, 0); |
bafe0602 KR |
225 | #endif |
226 | ||
227 | ||
25057836 JL |
228 | if (obj_aout_subformat (abfd) == gnu_encap_format) |
229 | { | |
bafe0602 | 230 | /* The file offsets of the relocation info */ |
25057836 JL |
231 | obj_textsec (abfd)->rel_filepos = N_GNU_TRELOFF (*execp); |
232 | obj_datasec (abfd)->rel_filepos = N_GNU_DRELOFF (*execp); | |
34dd8ba3 | 233 | |
bafe0602 | 234 | /* The file offsets of the string table and symbol table. */ |
25057836 | 235 | obj_sym_filepos (abfd) = N_GNU_SYMOFF (*execp); |
bafe0602 | 236 | obj_str_filepos (abfd) = (obj_sym_filepos (abfd) + execp->a_syms); |
34dd8ba3 | 237 | |
bafe0602 KR |
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; | |
25057836 | 242 | } |
34dd8ba3 | 243 | |
bafe0602 KR |
244 | return abfd->xvec; |
245 | } | |
246 | ||
25057836 | 247 | extern boolean aout_32_write_syms PARAMS ((bfd * abfd)); |
9783e04a | 248 | |
bafe0602 | 249 | static boolean |
25057836 JL |
250 | MY (write_object_contents) (abfd) |
251 | bfd *abfd; | |
bafe0602 | 252 | { |
25057836 JL |
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; | |
34dd8ba3 | 257 | |
25057836 | 258 | memset (&exec_bytes, 0, sizeof (exec_bytes)); |
bafe0602 | 259 | #if CHOOSE_RELOC_SIZE |
25057836 | 260 | CHOOSE_RELOC_SIZE (abfd); |
bafe0602 | 261 | #else |
25057836 | 262 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; |
bafe0602 | 263 | #endif |
34dd8ba3 | 264 | |
25057836 JL |
265 | if (adata (abfd).magic == undecided_magic) |
266 | NAME (aout, adjust_sizes_and_vmas) (abfd, &text_size, &text_end); | |
267 | execp->a_syms = 0; | |
34dd8ba3 | 268 | |
25057836 | 269 | execp->a_entry = bfd_get_start_address (abfd); |
34dd8ba3 | 270 | |
25057836 JL |
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)); | |
34dd8ba3 | 275 | |
25057836 JL |
276 | N_SET_MACHTYPE (*execp, 0xc); |
277 | N_SET_FLAGS (*execp, 0x2); | |
34dd8ba3 | 278 | |
25057836 | 279 | NAME (aout, swap_exec_header_out) (abfd, execp, &exec_bytes); |
34dd8ba3 | 280 | |
25057836 | 281 | /* update fields not covered by default swap_exec_header_out */ |
bafe0602 | 282 | |
25057836 JL |
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); | |
34dd8ba3 | 285 | |
4002f18a ILT |
286 | if (bfd_seek (abfd, 0L, false) != 0 |
287 | || (bfd_write ((PTR) & exec_bytes, 1, EXEC_BYTES_SIZE, abfd) | |
288 | != EXEC_BYTES_SIZE)) | |
289 | return false; | |
34dd8ba3 | 290 | |
25057836 | 291 | /* Write out the symbols, and then the relocs. We must write out |
9783e04a DM |
292 | the symbols first so that we know the symbol indices. */ |
293 | ||
25057836 JL |
294 | if (bfd_get_symcount (abfd) != 0) |
295 | { | |
296 | /* Skip the relocs to where we want to put the symbols. */ | |
297 | if (bfd_seek (abfd, (file_ptr) N_DRELOFF (*execp) + execp->a_drsize, | |
298 | SEEK_SET) != 0) | |
299 | return false; | |
300 | } | |
9783e04a | 301 | |
25057836 JL |
302 | if (!MY (write_syms) (abfd)) |
303 | return false; | |
34dd8ba3 | 304 | |
25057836 JL |
305 | if (bfd_get_symcount (abfd) != 0) |
306 | { | |
4002f18a ILT |
307 | if (bfd_seek (abfd, (long) (N_TRELOFF (*execp)), false) != 0) |
308 | return false; | |
25057836 JL |
309 | if (!NAME (aout, squirt_out_relocs) (abfd, obj_textsec (abfd))) |
310 | return false; | |
4002f18a ILT |
311 | if (bfd_seek (abfd, (long) (N_DRELOFF (*execp)), false) != 0) |
312 | return false; | |
25057836 JL |
313 | if (!NAME (aout, squirt_out_relocs) (abfd, obj_datasec (abfd))) |
314 | return false; | |
bafe0602 KR |
315 | } |
316 | ||
25057836 JL |
317 | return true; |
318 | } | |
bafe0602 KR |
319 | |
320 | /* convert the hp symbol type to be the same as aout64.h usage so we */ | |
321 | /* can piggyback routines in aoutx.h. */ | |
322 | ||
323 | static void | |
25057836 JL |
324 | convert_sym_type (sym_pointer, cache_ptr, abfd) |
325 | struct external_nlist *sym_pointer; | |
326 | aout_symbol_type *cache_ptr; | |
327 | bfd *abfd; | |
bafe0602 | 328 | { |
25057836 JL |
329 | int name_type; |
330 | int new_type; | |
bafe0602 | 331 | |
25057836 JL |
332 | name_type = (cache_ptr->type); |
333 | new_type = 0; | |
34dd8ba3 | 334 | |
25057836 | 335 | if ((name_type & HP_SYMTYPE_ALIGN) != 0) |
bafe0602 | 336 | { |
25057836 JL |
337 | /* iou_error ("aligned symbol encountered: %s", name);*/ |
338 | name_type = 0; | |
bafe0602 KR |
339 | } |
340 | ||
25057836 JL |
341 | if (name_type == HP_SYMTYPE_FILENAME) |
342 | new_type = N_FN; | |
343 | else | |
344 | { | |
345 | switch (name_type & HP_SYMTYPE_TYPE) | |
346 | { | |
347 | case HP_SYMTYPE_UNDEFINED: | |
348 | new_type = N_UNDF; | |
349 | break; | |
350 | ||
351 | case HP_SYMTYPE_ABSOLUTE: | |
352 | new_type = N_ABS; | |
353 | break; | |
354 | ||
355 | case HP_SYMTYPE_TEXT: | |
356 | new_type = N_TEXT; | |
357 | break; | |
358 | ||
359 | case HP_SYMTYPE_DATA: | |
360 | new_type = N_DATA; | |
361 | break; | |
362 | ||
363 | case HP_SYMTYPE_BSS: | |
364 | new_type = N_BSS; | |
365 | break; | |
366 | ||
367 | case HP_SYMTYPE_COMMON: | |
368 | new_type = N_COMM; | |
369 | break; | |
370 | ||
371 | default: | |
372 | fprintf (stderr, "unknown symbol type encountered: %x", name_type); | |
373 | } | |
374 | if (name_type & HP_SYMTYPE_EXTERNAL) | |
375 | new_type |= N_EXT; | |
376 | ||
377 | if (name_type & HP_SECONDARY_SYMBOL) | |
378 | new_type = (new_type & ~N_TYPE) | N_INDR; | |
379 | } | |
380 | cache_ptr->type = new_type; | |
34dd8ba3 | 381 | |
bafe0602 KR |
382 | } |
383 | ||
384 | ||
385 | /* | |
386 | DESCRIPTION | |
387 | Swaps the information in an executable header taken from a raw | |
388 | byte stream memory image, into the internal exec_header | |
389 | structure. | |
390 | */ | |
391 | ||
392 | void | |
25057836 JL |
393 | NAME (aout, swap_exec_header_in) (abfd, raw_bytes, execp) |
394 | bfd *abfd; | |
395 | struct external_exec *raw_bytes; | |
396 | struct internal_exec *execp; | |
bafe0602 | 397 | { |
25057836 | 398 | struct external_exec *bytes = (struct external_exec *) raw_bytes; |
bafe0602 KR |
399 | |
400 | /* The internal_exec structure has some fields that are unused in this | |
401 | configuration (IE for i960), so ensure that all such uninitialized | |
402 | fields are zero'd out. There are places where two of these structs | |
403 | are memcmp'd, and thus the contents do matter. */ | |
404 | memset (execp, 0, sizeof (struct internal_exec)); | |
405 | /* Now fill in fields in the execp, from the bytes in the raw data. */ | |
25057836 JL |
406 | execp->a_info = bfd_h_get_32 (abfd, bytes->e_info); |
407 | execp->a_text = GET_WORD (abfd, bytes->e_text); | |
408 | execp->a_data = GET_WORD (abfd, bytes->e_data); | |
409 | execp->a_bss = GET_WORD (abfd, bytes->e_bss); | |
410 | execp->a_syms = GET_WORD (abfd, bytes->e_syms); | |
411 | execp->a_entry = GET_WORD (abfd, bytes->e_entry); | |
bafe0602 KR |
412 | execp->a_trsize = GET_WORD (abfd, bytes->e_trsize); |
413 | execp->a_drsize = GET_WORD (abfd, bytes->e_drsize); | |
414 | ||
415 | /***************************************************************/ | |
416 | /* check the header to see if it was generated by a bfd output */ | |
417 | /* this is detected rather bizarely by requiring a bunch of */ | |
418 | /* header fields to be zero and an old unused field (now used) */ | |
419 | /* to be set. */ | |
420 | /***************************************************************/ | |
421 | do | |
25057836 | 422 | { |
bafe0602 | 423 | long syms; |
25057836 JL |
424 | struct aout_data_struct *rawptr; |
425 | if (bfd_h_get_32 (abfd, bytes->e_passize) != 0) | |
426 | break; | |
427 | if (bfd_h_get_32 (abfd, bytes->e_syms) != 0) | |
428 | break; | |
429 | if (bfd_h_get_32 (abfd, bytes->e_supsize) != 0) | |
430 | break; | |
34dd8ba3 | 431 | |
bafe0602 | 432 | syms = bfd_h_get_32 (abfd, bytes->e_drelocs); |
25057836 JL |
433 | if (syms == 0) |
434 | break; | |
bafe0602 KR |
435 | |
436 | /* OK, we've passed the test as best as we can determine */ | |
437 | execp->a_syms = syms; | |
438 | ||
439 | /* allocate storage for where we will store this result */ | |
25057836 | 440 | rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (*rawptr)); |
bafe0602 | 441 | |
25057836 JL |
442 | if (rawptr == NULL) |
443 | { | |
444 | bfd_set_error (bfd_error_no_memory); | |
445 | return; | |
446 | } | |
bafe0602 | 447 | abfd->tdata.aout_data = rawptr; |
25057836 JL |
448 | obj_aout_subformat (abfd) = gnu_encap_format; |
449 | } | |
450 | while (0); | |
bafe0602 KR |
451 | } |
452 | ||
453 | ||
454 | /* The hp symbol table is a bit different than other a.out targets. Instead | |
455 | of having an array of nlist items and an array of strings, hp's format | |
456 | has them mixed together in one structure. In addition, the strings are | |
457 | not null terminated. It looks something like this: | |
458 | ||
459 | nlist element 1 | |
460 | string1 | |
461 | nlist element 2 | |
462 | string2 | |
463 | ... | |
464 | ||
465 | The whole symbol table is read as one chunk and then we march thru it | |
466 | and convert it to canonical form. As we march thru the table, we copy | |
467 | the nlist data into the internal form and we compact the strings and null | |
468 | terminate them, using storage from the already allocated symbol table: | |
469 | ||
470 | string1 | |
471 | null | |
472 | string2 | |
473 | null | |
474 | ... | |
475 | */ | |
476 | ||
477 | boolean | |
25057836 JL |
478 | MY (slurp_symbol_table) (abfd) |
479 | bfd *abfd; | |
bafe0602 KR |
480 | { |
481 | bfd_size_type symbol_bytes; | |
482 | struct external_nlist *syms; | |
483 | struct external_nlist *sym_pointer; | |
484 | struct external_nlist *sym_end; | |
485 | char *strings; | |
486 | aout_symbol_type *cached; | |
487 | unsigned num_syms = 0; | |
488 | unsigned num_secondary = 0; | |
34dd8ba3 | 489 | |
bafe0602 | 490 | /* If there's no work to be done, don't do any */ |
25057836 JL |
491 | if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL) |
492 | return true; | |
493 | symbol_bytes = exec_hdr (abfd)->a_syms; | |
494 | if (symbol_bytes == 0) | |
495 | { | |
496 | bfd_set_error (bfd_error_no_symbols); | |
497 | return false; | |
498 | } | |
bafe0602 | 499 | |
25057836 JL |
500 | strings = (char *) bfd_alloc (abfd, |
501 | symbol_bytes + SYM_EXTRA_BYTES); | |
9783e04a DM |
502 | if (!strings) |
503 | { | |
25057836 | 504 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
505 | return false; |
506 | } | |
bafe0602 | 507 | syms = (struct external_nlist *) (strings + SYM_EXTRA_BYTES); |
4002f18a ILT |
508 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0 |
509 | || bfd_read ((PTR) syms, symbol_bytes, 1, abfd) != symbol_bytes) | |
25057836 | 510 | { |
bafe0602 KR |
511 | bfd_release (abfd, syms); |
512 | return false; | |
25057836 | 513 | } |
34dd8ba3 | 514 | |
bafe0602 | 515 | |
25057836 | 516 | sym_end = (struct external_nlist *) (((char *) syms) + symbol_bytes); |
34dd8ba3 | 517 | |
bafe0602 | 518 | /* first, march thru the table and figure out how many symbols there are */ |
34dd8ba3 | 519 | for (sym_pointer = syms; sym_pointer < sym_end; sym_pointer++, num_syms++) |
25057836 JL |
520 | { |
521 | if (bfd_get_8 (abfd, sym_pointer->e_type) & HP_SECONDARY_SYMBOL) | |
522 | num_secondary++; | |
bafe0602 | 523 | /* skip over the embedded symbol. */ |
25057836 JL |
524 | sym_pointer = (struct external_nlist *) (((char *) sym_pointer) + |
525 | sym_pointer->e_length[0]); | |
526 | } | |
bafe0602 KR |
527 | |
528 | /* now that we know the symbol count, update the bfd header */ | |
25057836 | 529 | bfd_get_symcount (abfd) = num_syms + num_secondary; |
34dd8ba3 | 530 | |
bafe0602 | 531 | cached = (aout_symbol_type *) |
25057836 JL |
532 | bfd_zalloc (abfd, (bfd_size_type) (bfd_get_symcount (abfd) * |
533 | sizeof (aout_symbol_type))); | |
9783e04a DM |
534 | if (!cached) |
535 | { | |
25057836 | 536 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
537 | return false; |
538 | } | |
34dd8ba3 | 539 | |
bafe0602 KR |
540 | /* as we march thru the hp symbol table, convert it into a list of |
541 | null terminated strings to hold the symbol names. Make sure any | |
542 | assignment to the strings pointer is done after we're thru using | |
543 | the nlist so we don't overwrite anything important. */ | |
544 | num_secondary = 0; | |
34dd8ba3 | 545 | |
bafe0602 | 546 | /* OK, now walk the new symtable, cacheing symbol properties */ |
25057836 JL |
547 | { |
548 | aout_symbol_type *cache_ptr = cached; | |
549 | aout_symbol_type cache_save; | |
550 | /* Run through table and copy values */ | |
551 | for (sym_pointer = syms, cache_ptr = cached; | |
552 | sym_pointer < sym_end; sym_pointer++, cache_ptr++) | |
553 | { | |
554 | unsigned int length; | |
555 | cache_ptr->symbol.the_bfd = abfd; | |
556 | cache_ptr->symbol.value = GET_SWORD (abfd, sym_pointer->e_value); | |
557 | cache_ptr->desc = bfd_get_16 (abfd, sym_pointer->e_almod); | |
558 | cache_ptr->type = bfd_get_8 (abfd, sym_pointer->e_type); | |
559 | cache_ptr->symbol.udata = 0; | |
560 | length = bfd_get_8 (abfd, sym_pointer->e_length); | |
561 | cache_ptr->other = length; /* other not used, save length here */ | |
562 | ||
563 | cache_save = *cache_ptr; | |
564 | convert_sym_type (sym_pointer, cache_ptr, abfd); | |
565 | if (!translate_from_native_sym_flags (sym_pointer, cache_ptr, abfd)) | |
566 | return false; | |
567 | ||
568 | /********************************************************/ | |
569 | /* for hpux, the 'lenght' value indicates the length of */ | |
570 | /* the symbol name which follows the nlist entry. */ | |
571 | /********************************************************/ | |
572 | if (length) | |
bafe0602 | 573 | { |
25057836 JL |
574 | /**************************************************************/ |
575 | /* the hp string is not null terminated so we create a new one*/ | |
576 | /* by copying the string to overlap the just vacated nlist */ | |
577 | /* structure before it in memory. */ | |
578 | /**************************************************************/ | |
579 | cache_ptr->symbol.name = strings; | |
580 | memcpy (strings, sym_pointer + 1, length); | |
581 | strings[length] = '\0'; | |
582 | strings += length + 1; | |
583 | } | |
584 | else | |
585 | cache_ptr->symbol.name = (char *) NULL; | |
586 | ||
587 | /**********************************************************/ | |
588 | /* this is a bit of a kludge, but a secondary hp symbol */ | |
589 | /* gets translated into a gnu indirect symbol. When this */ | |
590 | /* happens, we need to create a "dummy" record to which */ | |
591 | /* we can point the indirect symbol to. */ | |
592 | /**********************************************************/ | |
593 | if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT)) | |
594 | { | |
595 | aout_symbol_type *cache_ptr2 = cached + num_syms + num_secondary; | |
596 | ||
597 | num_secondary++; | |
598 | ||
599 | /* aoutx.h assumes the "next" value is the indirect sym */ | |
600 | /* since we don't want to disturb the order by inserting */ | |
601 | /* a new symbol, we tack on the created secondary syms */ | |
602 | /* at the end. */ | |
603 | cache_ptr->symbol.value = (bfd_vma) (cache_ptr2); | |
604 | *cache_ptr2 = cache_save; | |
605 | cache_ptr2->symbol.name = strings; | |
606 | memcpy (strings, cache_ptr->symbol.name, length); | |
607 | strcpy (strings + length, ":secondry"); /* 9 max chars + null */ | |
608 | strings += length + 10; | |
609 | cache_ptr2->type &= ~HP_SECONDARY_SYMBOL; /* clear secondary */ | |
610 | convert_sym_type (sym_pointer, cache_ptr2, abfd); | |
611 | if (!translate_from_native_sym_flags (sym_pointer, cache_ptr2, | |
612 | abfd)) | |
9783e04a | 613 | return false; |
bafe0602 | 614 | } |
bafe0602 | 615 | |
25057836 JL |
616 | /* skip over the embedded symbol. */ |
617 | sym_pointer = (struct external_nlist *) (((char *) sym_pointer) + | |
618 | length); | |
619 | } | |
620 | } | |
621 | ||
622 | obj_aout_symbols (abfd) = cached; | |
34dd8ba3 | 623 | |
bafe0602 KR |
624 | return true; |
625 | } | |
626 | ||
627 | ||
628 | ||
629 | void | |
25057836 JL |
630 | MY (swap_std_reloc_in) (abfd, bytes, cache_ptr, symbols) |
631 | bfd *abfd; | |
632 | struct hp300hpux_reloc *bytes; | |
633 | arelent *cache_ptr; | |
634 | asymbol **symbols; | |
bafe0602 KR |
635 | { |
636 | int r_index; | |
637 | int r_extern = 0; | |
638 | unsigned int r_length; | |
639 | int r_pcrel = 0; | |
25057836 | 640 | struct aoutdata *su = &(abfd->tdata.aout_data->a); |
bafe0602 | 641 | |
34dd8ba3 | 642 | cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address); |
25057836 | 643 | r_index = bfd_h_get_16 (abfd, bytes->r_index); |
34dd8ba3 | 644 | |
bafe0602 | 645 | switch (bytes->r_type[0]) |
25057836 JL |
646 | { |
647 | case HP_RSEGMENT_TEXT: | |
bafe0602 KR |
648 | r_index = N_TEXT; |
649 | break; | |
25057836 | 650 | case HP_RSEGMENT_DATA: |
bafe0602 KR |
651 | r_index = N_DATA; |
652 | break; | |
25057836 | 653 | case HP_RSEGMENT_BSS: |
bafe0602 KR |
654 | r_index = N_BSS; |
655 | break; | |
25057836 | 656 | case HP_RSEGMENT_EXTERNAL: |
bafe0602 KR |
657 | r_extern = 1; |
658 | break; | |
25057836 | 659 | case HP_RSEGMENT_PCREL: |
bafe0602 KR |
660 | r_extern = 1; |
661 | r_pcrel = 1; | |
662 | break; | |
25057836 | 663 | case HP_RSEGMENT_RDLT: |
bafe0602 | 664 | break; |
25057836 | 665 | case HP_RSEGMENT_RPLT: |
bafe0602 | 666 | break; |
25057836 | 667 | case HP_RSEGMENT_NOOP: |
bafe0602 | 668 | break; |
25057836 | 669 | default: |
9783e04a DM |
670 | fprintf (stderr, "illegal relocation segment type: %x\n", |
671 | (bytes->r_type[0])); | |
25057836 | 672 | } |
34dd8ba3 | 673 | |
bafe0602 KR |
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: | |
25057836 | 686 | fprintf (stderr, "illegal relocation length: %x\n", bytes->r_length[0]); |
ebd24135 | 687 | r_length = 0; |
bafe0602 KR |
688 | } |
689 | ||
25057836 | 690 | cache_ptr->howto = howto_table_std + r_length + 4 * r_pcrel; |
bafe0602 KR |
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) | |
25057836 JL |
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 | } | |
bafe0602 | 701 | else |
25057836 JL |
702 | { |
703 | MOVE_ADDRESS (0); | |
704 | } | |
bafe0602 KR |
705 | } |
706 | ||
707 | boolean | |
25057836 JL |
708 | MY (slurp_reloc_table) (abfd, asect, symbols) |
709 | bfd *abfd; | |
710 | sec_ptr asect; | |
711 | asymbol **symbols; | |
bafe0602 KR |
712 | { |
713 | unsigned int 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; | |
34dd8ba3 | 721 | |
25057836 JL |
722 | if (asect->relocation) |
723 | return true; | |
bafe0602 | 724 | |
25057836 JL |
725 | if (asect->flags & SEC_CONSTRUCTOR) |
726 | return true; | |
bafe0602 | 727 | |
25057836 JL |
728 | if (asect == obj_datasec (abfd)) |
729 | { | |
730 | reloc_size = exec_hdr (abfd)->a_drsize; | |
731 | goto doit; | |
732 | } | |
bafe0602 | 733 | |
25057836 JL |
734 | if (asect == obj_textsec (abfd)) |
735 | { | |
736 | reloc_size = exec_hdr (abfd)->a_trsize; | |
737 | goto doit; | |
738 | } | |
bafe0602 | 739 | |
25057836 | 740 | bfd_set_error (bfd_error_invalid_operation); |
bafe0602 KR |
741 | return false; |
742 | ||
25057836 | 743 | doit: |
4002f18a ILT |
744 | if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0) |
745 | return false; | |
bafe0602 KR |
746 | each_size = obj_reloc_entry_size (abfd); |
747 | ||
748 | count = reloc_size / each_size; | |
749 | ||
750 | ||
25057836 JL |
751 | reloc_cache = (arelent *) bfd_zalloc (abfd, (size_t) (count * sizeof |
752 | (arelent))); | |
b3cee0a9 | 753 | if (!reloc_cache && count != 0) |
25057836 JL |
754 | { |
755 | nomem: | |
756 | bfd_set_error (bfd_error_no_memory); | |
757 | return false; | |
758 | } | |
bafe0602 KR |
759 | |
760 | relocs = (PTR) bfd_alloc (abfd, reloc_size); | |
b3cee0a9 | 761 | if (!relocs && reloc_size != 0) |
25057836 JL |
762 | { |
763 | bfd_release (abfd, reloc_cache); | |
764 | goto nomem; | |
765 | } | |
bafe0602 | 766 | |
25057836 JL |
767 | if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size) |
768 | { | |
769 | bfd_release (abfd, relocs); | |
770 | bfd_release (abfd, reloc_cache); | |
771 | return false; | |
772 | } | |
bafe0602 | 773 | |
25057836 JL |
774 | rptr = (struct hp300hpux_reloc *) relocs; |
775 | counter = 0; | |
776 | cache_ptr = reloc_cache; | |
bafe0602 | 777 | |
25057836 JL |
778 | for (; counter < count; counter++, rptr++, cache_ptr++) |
779 | { | |
780 | MY (swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols); | |
bafe0602 KR |
781 | } |
782 | ||
783 | ||
25057836 | 784 | bfd_release (abfd, relocs); |
bafe0602 KR |
785 | asect->relocation = reloc_cache; |
786 | asect->reloc_count = count; | |
787 | return true; | |
788 | } | |
789 | ||
790 | ||
791 | /************************************************************************/ | |
792 | /* The following functions are identical to functions in aoutx.h except */ | |
793 | /* they refer to MY(func) rather than NAME(aout,func) and they also */ | |
794 | /* call aout_32 versions if the input file was generated by gcc */ | |
795 | /************************************************************************/ | |
796 | ||
326e32d7 ILT |
797 | long aout_32_get_symtab PARAMS ((bfd * abfd, asymbol ** location)); |
798 | long aout_32_get_symtab_upper_bound PARAMS ((bfd * abfd)); | |
bafe0602 | 799 | |
326e32d7 ILT |
800 | long aout_32_canonicalize_reloc PARAMS ((bfd * abfd, sec_ptr section, |
801 | arelent ** relptr, | |
802 | asymbol ** symbols)); | |
bafe0602 | 803 | |
326e32d7 | 804 | long |
25057836 JL |
805 | MY (get_symtab) (abfd, location) |
806 | bfd *abfd; | |
807 | asymbol **location; | |
bafe0602 | 808 | { |
25057836 JL |
809 | unsigned int counter = 0; |
810 | aout_symbol_type *symbase; | |
bafe0602 | 811 | |
25057836 JL |
812 | if (obj_aout_subformat (abfd) == gnu_encap_format) |
813 | return aout_32_get_symtab (abfd, location); | |
34dd8ba3 | 814 | |
25057836 | 815 | if (!MY (slurp_symbol_table) (abfd)) |
326e32d7 | 816 | return -1; |
bafe0602 | 817 | |
25057836 JL |
818 | for (symbase = obj_aout_symbols (abfd); counter++ < bfd_get_symcount (abfd);) |
819 | *(location++) = (asymbol *) (symbase++); | |
820 | *location++ = 0; | |
821 | return bfd_get_symcount (abfd); | |
bafe0602 KR |
822 | } |
823 | ||
326e32d7 | 824 | long |
25057836 JL |
825 | MY (get_symtab_upper_bound) (abfd) |
826 | bfd *abfd; | |
bafe0602 | 827 | { |
25057836 JL |
828 | if (obj_aout_subformat (abfd) == gnu_encap_format) |
829 | return aout_32_get_symtab_upper_bound (abfd); | |
830 | if (!MY (slurp_symbol_table) (abfd)) | |
326e32d7 | 831 | return -1; |
bafe0602 | 832 | |
25057836 | 833 | return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *)); |
bafe0602 KR |
834 | } |
835 | ||
836 | ||
837 | ||
838 | ||
326e32d7 | 839 | long |
25057836 JL |
840 | MY (canonicalize_reloc) (abfd, section, relptr, symbols) |
841 | bfd *abfd; | |
842 | sec_ptr section; | |
843 | arelent **relptr; | |
844 | asymbol **symbols; | |
bafe0602 KR |
845 | { |
846 | arelent *tblptr = section->relocation; | |
847 | unsigned int count; | |
25057836 JL |
848 | if (obj_aout_subformat (abfd) == gnu_encap_format) |
849 | return aout_32_canonicalize_reloc (abfd, section, relptr, symbols); | |
bafe0602 | 850 | |
25057836 | 851 | if (!(tblptr || MY (slurp_reloc_table) (abfd, section, symbols))) |
326e32d7 | 852 | return -1; |
bafe0602 | 853 | |
25057836 JL |
854 | if (section->flags & SEC_CONSTRUCTOR) |
855 | { | |
856 | arelent_chain *chain = section->constructor_chain; | |
857 | for (count = 0; count < section->reloc_count; count++) | |
858 | { | |
859 | *relptr++ = &chain->relent; | |
860 | chain = chain->next; | |
861 | } | |
862 | } | |
863 | else | |
864 | { | |
865 | tblptr = section->relocation; | |
25057836 JL |
866 | |
867 | for (count = 0; count++ < section->reloc_count;) | |
868 | { | |
869 | *relptr++ = tblptr++; | |
870 | } | |
bafe0602 | 871 | } |
bafe0602 KR |
872 | *relptr = 0; |
873 | ||
874 | return section->reloc_count; | |
875 | } | |
876 | ||
877 | ||
878 | #include "aout-target.h" |