Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD backend for hp-ux 9000/300 |
9553c638 | 2 | Copyright 1990, 1991, 1993, 1994, 1995, 1997, 1999, 2000, 2001, 2002, |
2c3fc389 | 3 | 2003, 2004, 2005, 2007, 2010, 2012 Free Software Foundation, Inc. |
252b5132 RH |
4 | Written by Glenn Engel. |
5 | ||
cd123cb7 | 6 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 7 | |
cd123cb7 NC |
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 3 of the License, or | |
11 | (at your option) any later version. | |
252b5132 | 12 | |
cd123cb7 NC |
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. | |
252b5132 | 17 | |
cd123cb7 NC |
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., 51 Franklin Street - Fifth Floor, Boston, | |
21 | MA 02110-1301, USA. */ | |
252b5132 | 22 | |
cd123cb7 NC |
23 | |
24 | /* hpux native ------------> | | | |
252b5132 RH |
25 | | hp300hpux bfd | ----------> hpux w/gnu ext |
26 | hpux w/gnu extension ----> | | | |
27 | ||
252b5132 RH |
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 | |
6cee3f79 | 97 | canonicalize_symtab |
252b5132 RH |
98 | get_symtab_upper_bound |
99 | canonicalize_reloc | |
100 | mkobject | |
101 | This should also be fixed. */ | |
102 | ||
103 | #define TARGETNAME "a.out-hp300hpux" | |
e43d48cc AM |
104 | |
105 | /* Do not "beautify" the CONCAT* macro args. Traditional C will not | |
106 | remove whitespace added here, and thus will fail to concatenate | |
107 | the tokens. */ | |
108 | #define MY(OP) CONCAT2 (hp300hpux_,OP) | |
252b5132 RH |
109 | |
110 | #define external_exec hp300hpux_exec_bytes | |
111 | #define external_nlist hp300hpux_nlist_bytes | |
112 | ||
113 | #include "aout/hp300hpux.h" | |
114 | ||
115 | /* define these so we can compile unused routines in aoutx.h */ | |
116 | #define e_strx e_shlib | |
117 | #define e_other e_length | |
118 | #define e_desc e_almod | |
119 | ||
120 | #define AR_PAD_CHAR '/' | |
121 | #define TARGET_IS_BIG_ENDIAN_P | |
122 | #define DEFAULT_ARCH bfd_arch_m68k | |
123 | ||
124 | #define MY_get_section_contents aout_32_get_section_contents | |
125 | #define MY_slurp_armap bfd_slurp_bsd_armap_f2 | |
126 | ||
127 | /***********************************************/ | |
128 | /* provide overrides for routines in this file */ | |
129 | /***********************************************/ | |
130 | /* these don't use MY because that causes problems within JUMP_TABLE | |
e43d48cc | 131 | (CONCAT2 winds up being expanded recursively, which ANSI C compilers |
252b5132 | 132 | will not do). */ |
6cee3f79 | 133 | #define MY_canonicalize_symtab hp300hpux_canonicalize_symtab |
252b5132 RH |
134 | #define MY_get_symtab_upper_bound hp300hpux_get_symtab_upper_bound |
135 | #define MY_canonicalize_reloc hp300hpux_canonicalize_reloc | |
136 | #define MY_write_object_contents hp300hpux_write_object_contents | |
137 | ||
138 | #define MY_read_minisymbols _bfd_generic_read_minisymbols | |
139 | #define MY_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol | |
140 | ||
141 | #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create | |
142 | #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols | |
143 | #define MY_final_link_callback unused | |
144 | #define MY_bfd_final_link _bfd_generic_final_link | |
145 | ||
146 | /* Until and unless we convert the slurp_reloc and slurp_symtab | |
147 | routines in this file, we can not use the default aout | |
148 | free_cached_info routine which assumes that the relocs and symtabs | |
149 | were allocated using malloc. */ | |
150 | #define MY_bfd_free_cached_info bfd_true | |
151 | ||
152 | #define hp300hpux_write_syms aout_32_write_syms | |
153 | ||
154 | #define MY_callback MY(callback) | |
155 | ||
156 | #define MY_exec_hdr_flags 0x2 | |
157 | ||
158 | #define NAME_swap_exec_header_in NAME(hp300hpux_32_,swap_exec_header_in) | |
159 | ||
160 | #define HP_SYMTYPE_UNDEFINED 0x00 | |
161 | #define HP_SYMTYPE_ABSOLUTE 0x01 | |
162 | #define HP_SYMTYPE_TEXT 0x02 | |
163 | #define HP_SYMTYPE_DATA 0x03 | |
164 | #define HP_SYMTYPE_BSS 0x04 | |
165 | #define HP_SYMTYPE_COMMON 0x05 | |
166 | ||
167 | #define HP_SYMTYPE_TYPE 0x0F | |
168 | #define HP_SYMTYPE_FILENAME 0x1F | |
169 | ||
170 | #define HP_SYMTYPE_ALIGN 0x10 | |
171 | #define HP_SYMTYPE_EXTERNAL 0x20 | |
172 | #define HP_SECONDARY_SYMBOL 0x40 | |
173 | ||
174 | /* RELOCATION DEFINITIONS */ | |
175 | #define HP_RSEGMENT_TEXT 0x00 | |
176 | #define HP_RSEGMENT_DATA 0x01 | |
177 | #define HP_RSEGMENT_BSS 0x02 | |
178 | #define HP_RSEGMENT_EXTERNAL 0x03 | |
179 | #define HP_RSEGMENT_PCREL 0x04 | |
180 | #define HP_RSEGMENT_RDLT 0x05 | |
181 | #define HP_RSEGMENT_RPLT 0x06 | |
182 | #define HP_RSEGMENT_NOOP 0x3F | |
183 | ||
184 | #define HP_RLENGTH_BYTE 0x00 | |
185 | #define HP_RLENGTH_WORD 0x01 | |
186 | #define HP_RLENGTH_LONG 0x02 | |
187 | #define HP_RLENGTH_ALIGN 0x03 | |
188 | ||
e43d48cc | 189 | #define NAME(x,y) CONCAT3 (hp300hpux,_32_,y) |
252b5132 RH |
190 | #define ARCH_SIZE 32 |
191 | ||
192 | /* aoutx.h requires definitions for BMAGIC and QMAGIC. */ | |
193 | #define BMAGIC HPUX_DOT_O_MAGIC | |
194 | #define QMAGIC 0314 | |
195 | ||
196 | #include "aoutx.h" | |
197 | ||
2c3fc389 NC |
198 | static const bfd_target * MY (callback) (bfd *); |
199 | static bfd_boolean MY (write_object_contents) (bfd *); | |
b34976b6 | 200 | static void convert_sym_type |
2c3fc389 | 201 | (struct external_nlist *, aout_symbol_type *, bfd *); |
b34976b6 | 202 | |
2c3fc389 | 203 | bfd_boolean MY (slurp_symbol_table) (bfd *); |
b34976b6 | 204 | void MY (swap_std_reloc_in) |
2c3fc389 | 205 | (bfd *, struct hp300hpux_reloc *, arelent *, asymbol **, bfd_size_type); |
b34976b6 | 206 | bfd_boolean MY (slurp_reloc_table) |
2c3fc389 NC |
207 | (bfd *, sec_ptr, asymbol **); |
208 | long MY (canonicalize_symtab) (bfd *, asymbol **); | |
209 | long MY (get_symtab_upper_bound) (bfd *); | |
210 | long MY (canonicalize_reloc) (bfd *, sec_ptr, arelent **, asymbol **); | |
e4b17274 | 211 | |
252b5132 RH |
212 | /* Since the hpux symbol table has nlist elements interspersed with |
213 | strings and we need to insert som strings for secondary symbols, we | |
214 | give ourselves a little extra padding up front to account for | |
215 | this. Note that for each non-secondary symbol we process, we gain | |
216 | 9 bytes of space for the discarded nlist element (one byte used for | |
217 | null). SYM_EXTRA_BYTES is the extra space. */ | |
218 | #define SYM_EXTRA_BYTES 1024 | |
219 | ||
220 | /* Set parameters about this a.out file that are machine-dependent. | |
221 | This routine is called from some_aout_object_p just before it returns. */ | |
222 | static const bfd_target * | |
2c3fc389 | 223 | MY (callback) (bfd *abfd) |
252b5132 RH |
224 | { |
225 | struct internal_exec *execp = exec_hdr (abfd); | |
226 | ||
227 | /* Calculate the file positions of the parts of a newly read aout header */ | |
eea6121a | 228 | obj_textsec (abfd)->size = N_TXTSIZE (*execp); |
252b5132 RH |
229 | |
230 | /* The virtual memory addresses of the sections */ | |
231 | obj_textsec (abfd)->vma = N_TXTADDR (*execp); | |
232 | obj_datasec (abfd)->vma = N_DATADDR (*execp); | |
233 | obj_bsssec (abfd)->vma = N_BSSADDR (*execp); | |
234 | ||
235 | obj_textsec (abfd)->lma = obj_textsec (abfd)->vma; | |
236 | obj_datasec (abfd)->lma = obj_datasec (abfd)->vma; | |
237 | obj_bsssec (abfd)->lma = obj_bsssec (abfd)->vma; | |
238 | ||
239 | /* The file offsets of the sections */ | |
240 | obj_textsec (abfd)->filepos = N_TXTOFF (*execp); | |
241 | obj_datasec (abfd)->filepos = N_DATOFF (*execp); | |
242 | ||
243 | /* The file offsets of the relocation info */ | |
244 | obj_textsec (abfd)->rel_filepos = N_TRELOFF (*execp); | |
245 | obj_datasec (abfd)->rel_filepos = N_DRELOFF (*execp); | |
246 | ||
247 | /* The file offsets of the string table and symbol table. */ | |
248 | obj_sym_filepos (abfd) = N_SYMOFF (*execp); | |
249 | obj_str_filepos (abfd) = N_STROFF (*execp); | |
250 | ||
251 | /* Determine the architecture and machine type of the object file. */ | |
252 | #ifdef SET_ARCH_MACH | |
253 | SET_ARCH_MACH (abfd, *execp); | |
254 | #else | |
255 | bfd_default_set_arch_mach (abfd, DEFAULT_ARCH, 0); | |
256 | #endif | |
257 | ||
252b5132 RH |
258 | if (obj_aout_subformat (abfd) == gnu_encap_format) |
259 | { | |
260 | /* The file offsets of the relocation info */ | |
261 | obj_textsec (abfd)->rel_filepos = N_GNU_TRELOFF (*execp); | |
262 | obj_datasec (abfd)->rel_filepos = N_GNU_DRELOFF (*execp); | |
263 | ||
264 | /* The file offsets of the string table and symbol table. */ | |
265 | obj_sym_filepos (abfd) = N_GNU_SYMOFF (*execp); | |
266 | obj_str_filepos (abfd) = (obj_sym_filepos (abfd) + execp->a_syms); | |
267 | ||
268 | abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS; | |
269 | bfd_get_symcount (abfd) = execp->a_syms / 12; | |
270 | obj_symbol_entry_size (abfd) = 12; | |
271 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; | |
272 | } | |
273 | ||
274 | return abfd->xvec; | |
275 | } | |
276 | ||
2c3fc389 | 277 | extern bfd_boolean aout_32_write_syms (bfd *); |
252b5132 | 278 | |
b34976b6 | 279 | static bfd_boolean |
2c3fc389 | 280 | MY (write_object_contents) (bfd * abfd) |
252b5132 RH |
281 | { |
282 | struct external_exec exec_bytes; | |
283 | struct internal_exec *execp = exec_hdr (abfd); | |
284 | bfd_size_type text_size; /* dummy vars */ | |
285 | file_ptr text_end; | |
286 | ||
287 | memset (&exec_bytes, 0, sizeof (exec_bytes)); | |
8ba8a462 | 288 | |
252b5132 | 289 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; |
252b5132 RH |
290 | |
291 | if (adata (abfd).magic == undecided_magic) | |
292 | NAME (aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end); | |
293 | execp->a_syms = 0; | |
294 | ||
295 | execp->a_entry = bfd_get_start_address (abfd); | |
296 | ||
297 | execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * | |
298 | obj_reloc_entry_size (abfd)); | |
299 | execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * | |
300 | obj_reloc_entry_size (abfd)); | |
301 | ||
302 | N_SET_MACHTYPE (*execp, 0xc); | |
303 | N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags); | |
304 | ||
305 | NAME (aout,swap_exec_header_out) (abfd, execp, &exec_bytes); | |
306 | ||
307 | /* update fields not covered by default swap_exec_header_out */ | |
308 | ||
309 | /* this is really the sym table size but we store it in drelocs */ | |
dc810e39 | 310 | H_PUT_32 (abfd, (bfd_get_symcount (abfd) * 12), exec_bytes.e_drelocs); |
252b5132 | 311 | |
b34976b6 | 312 | if (bfd_seek (abfd, (file_ptr) 0, FALSE) != 0 |
2c3fc389 | 313 | || (bfd_bwrite (&exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, abfd) |
252b5132 | 314 | != EXEC_BYTES_SIZE)) |
b34976b6 | 315 | return FALSE; |
252b5132 RH |
316 | |
317 | /* Write out the symbols, and then the relocs. We must write out | |
318 | the symbols first so that we know the symbol indices. */ | |
319 | ||
320 | if (bfd_get_symcount (abfd) != 0) | |
321 | { | |
322 | /* Skip the relocs to where we want to put the symbols. */ | |
dc810e39 | 323 | if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (*execp) + execp->a_drsize), |
252b5132 | 324 | SEEK_SET) != 0) |
b34976b6 | 325 | return FALSE; |
252b5132 RH |
326 | } |
327 | ||
328 | if (!MY (write_syms) (abfd)) | |
b34976b6 | 329 | return FALSE; |
252b5132 RH |
330 | |
331 | if (bfd_get_symcount (abfd) != 0) | |
332 | { | |
dc810e39 | 333 | if (bfd_seek (abfd, (file_ptr) N_TRELOFF (*execp), SEEK_CUR) != 0) |
b34976b6 | 334 | return FALSE; |
252b5132 | 335 | if (!NAME (aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) |
b34976b6 | 336 | return FALSE; |
dc810e39 | 337 | if (bfd_seek (abfd, (file_ptr) N_DRELOFF (*execp), SEEK_CUR) != 0) |
b34976b6 | 338 | return FALSE; |
252b5132 | 339 | if (!NAME (aout,squirt_out_relocs) (abfd, obj_datasec (abfd))) |
b34976b6 | 340 | return FALSE; |
252b5132 RH |
341 | } |
342 | ||
b34976b6 | 343 | return TRUE; |
252b5132 RH |
344 | } |
345 | ||
2c3fc389 NC |
346 | /* Convert the hp symbol type to be the same as aout64.h usage so we |
347 | can piggyback routines in aoutx.h. */ | |
252b5132 RH |
348 | |
349 | static void | |
2c3fc389 NC |
350 | convert_sym_type (struct external_nlist *sym_pointer ATTRIBUTE_UNUSED, |
351 | aout_symbol_type *cache_ptr, | |
352 | bfd *abfd ATTRIBUTE_UNUSED) | |
252b5132 RH |
353 | { |
354 | int name_type; | |
355 | int new_type; | |
356 | ||
357 | name_type = (cache_ptr->type); | |
358 | new_type = 0; | |
359 | ||
360 | if ((name_type & HP_SYMTYPE_ALIGN) != 0) | |
361 | { | |
362 | /* iou_error ("aligned symbol encountered: %s", name);*/ | |
363 | name_type = 0; | |
364 | } | |
365 | ||
366 | if (name_type == HP_SYMTYPE_FILENAME) | |
367 | new_type = N_FN; | |
368 | else | |
369 | { | |
370 | switch (name_type & HP_SYMTYPE_TYPE) | |
371 | { | |
372 | case HP_SYMTYPE_UNDEFINED: | |
373 | new_type = N_UNDF; | |
374 | break; | |
375 | ||
376 | case HP_SYMTYPE_ABSOLUTE: | |
377 | new_type = N_ABS; | |
378 | break; | |
379 | ||
380 | case HP_SYMTYPE_TEXT: | |
381 | new_type = N_TEXT; | |
382 | break; | |
383 | ||
384 | case HP_SYMTYPE_DATA: | |
385 | new_type = N_DATA; | |
386 | break; | |
387 | ||
388 | case HP_SYMTYPE_BSS: | |
389 | new_type = N_BSS; | |
390 | break; | |
391 | ||
392 | case HP_SYMTYPE_COMMON: | |
393 | new_type = N_COMM; | |
394 | break; | |
395 | ||
396 | default: | |
397 | abort (); | |
398 | break; | |
399 | } | |
400 | if (name_type & HP_SYMTYPE_EXTERNAL) | |
401 | new_type |= N_EXT; | |
402 | ||
403 | if (name_type & HP_SECONDARY_SYMBOL) | |
404 | { | |
405 | switch (new_type) | |
406 | { | |
407 | default: | |
408 | abort (); | |
409 | case N_UNDF | N_EXT: | |
410 | /* If the value is nonzero, then just treat this as a | |
411 | common symbol. I don't know if this is correct in | |
412 | all cases, but it is more correct than treating it as | |
413 | a weak undefined symbol. */ | |
414 | if (cache_ptr->symbol.value == 0) | |
415 | new_type = N_WEAKU; | |
416 | break; | |
417 | case N_ABS | N_EXT: | |
418 | new_type = N_WEAKA; | |
419 | break; | |
420 | case N_TEXT | N_EXT: | |
421 | new_type = N_WEAKT; | |
422 | break; | |
423 | case N_DATA | N_EXT: | |
424 | new_type = N_WEAKD; | |
425 | break; | |
426 | case N_BSS | N_EXT: | |
427 | new_type = N_WEAKB; | |
428 | break; | |
429 | } | |
430 | } | |
431 | } | |
432 | cache_ptr->type = new_type; | |
433 | ||
434 | } | |
435 | ||
252b5132 RH |
436 | /* |
437 | DESCRIPTION | |
438 | Swaps the information in an executable header taken from a raw | |
439 | byte stream memory image, into the internal exec_header | |
440 | structure. | |
441 | */ | |
442 | ||
443 | void | |
2c3fc389 NC |
444 | NAME (aout,swap_exec_header_in) (bfd *abfd, |
445 | struct external_exec *raw_bytes, | |
446 | struct internal_exec *execp) | |
252b5132 RH |
447 | { |
448 | struct external_exec *bytes = (struct external_exec *) raw_bytes; | |
449 | ||
450 | /* The internal_exec structure has some fields that are unused in this | |
451 | configuration (IE for i960), so ensure that all such uninitialized | |
452 | fields are zero'd out. There are places where two of these structs | |
453 | are memcmp'd, and thus the contents do matter. */ | |
454 | memset (execp, 0, sizeof (struct internal_exec)); | |
455 | /* Now fill in fields in the execp, from the bytes in the raw data. */ | |
dc810e39 | 456 | execp->a_info = H_GET_32 (abfd, bytes->e_info); |
252b5132 RH |
457 | execp->a_text = GET_WORD (abfd, bytes->e_text); |
458 | execp->a_data = GET_WORD (abfd, bytes->e_data); | |
459 | execp->a_bss = GET_WORD (abfd, bytes->e_bss); | |
460 | execp->a_syms = GET_WORD (abfd, bytes->e_syms); | |
461 | execp->a_entry = GET_WORD (abfd, bytes->e_entry); | |
462 | execp->a_trsize = GET_WORD (abfd, bytes->e_trsize); | |
463 | execp->a_drsize = GET_WORD (abfd, bytes->e_drsize); | |
464 | ||
465 | /***************************************************************/ | |
466 | /* check the header to see if it was generated by a bfd output */ | |
7dee875e | 467 | /* this is detected rather bizarrely by requiring a bunch of */ |
252b5132 RH |
468 | /* header fields to be zero and an old unused field (now used) */ |
469 | /* to be set. */ | |
470 | /***************************************************************/ | |
471 | do | |
472 | { | |
473 | long syms; | |
474 | struct aout_data_struct *rawptr; | |
dc810e39 AM |
475 | bfd_size_type amt; |
476 | ||
477 | if (H_GET_32 (abfd, bytes->e_passize) != 0) | |
252b5132 | 478 | break; |
dc810e39 | 479 | if (H_GET_32 (abfd, bytes->e_syms) != 0) |
252b5132 | 480 | break; |
dc810e39 | 481 | if (H_GET_32 (abfd, bytes->e_supsize) != 0) |
252b5132 RH |
482 | break; |
483 | ||
dc810e39 | 484 | syms = H_GET_32 (abfd, bytes->e_drelocs); |
252b5132 RH |
485 | if (syms == 0) |
486 | break; | |
487 | ||
488 | /* OK, we've passed the test as best as we can determine */ | |
489 | execp->a_syms = syms; | |
490 | ||
491 | /* allocate storage for where we will store this result */ | |
dc810e39 AM |
492 | amt = sizeof (*rawptr); |
493 | rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
494 | |
495 | if (rawptr == NULL) | |
496 | return; | |
497 | abfd->tdata.aout_data = rawptr; | |
498 | obj_aout_subformat (abfd) = gnu_encap_format; | |
499 | } | |
500 | while (0); | |
501 | } | |
502 | ||
252b5132 RH |
503 | /* The hp symbol table is a bit different than other a.out targets. Instead |
504 | of having an array of nlist items and an array of strings, hp's format | |
505 | has them mixed together in one structure. In addition, the strings are | |
506 | not null terminated. It looks something like this: | |
507 | ||
508 | nlist element 1 | |
509 | string1 | |
510 | nlist element 2 | |
511 | string2 | |
512 | ... | |
513 | ||
514 | The whole symbol table is read as one chunk and then we march thru it | |
515 | and convert it to canonical form. As we march thru the table, we copy | |
516 | the nlist data into the internal form and we compact the strings and null | |
517 | terminate them, using storage from the already allocated symbol table: | |
518 | ||
519 | string1 | |
520 | null | |
521 | string2 | |
522 | null | |
523 | ... | |
524 | */ | |
525 | ||
b34976b6 | 526 | bfd_boolean |
2c3fc389 | 527 | MY (slurp_symbol_table) (bfd *abfd) |
252b5132 RH |
528 | { |
529 | bfd_size_type symbol_bytes; | |
530 | struct external_nlist *syms; | |
531 | struct external_nlist *sym_pointer; | |
532 | struct external_nlist *sym_end; | |
533 | char *strings; | |
534 | aout_symbol_type *cached; | |
535 | unsigned num_syms = 0; | |
dc810e39 | 536 | bfd_size_type amt; |
252b5132 RH |
537 | |
538 | /* If there's no work to be done, don't do any */ | |
539 | if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL) | |
b34976b6 | 540 | return TRUE; |
252b5132 RH |
541 | symbol_bytes = exec_hdr (abfd)->a_syms; |
542 | ||
dc810e39 AM |
543 | amt = symbol_bytes + SYM_EXTRA_BYTES; |
544 | strings = (char *) bfd_alloc (abfd, amt); | |
252b5132 | 545 | if (!strings) |
b34976b6 | 546 | return FALSE; |
252b5132 RH |
547 | syms = (struct external_nlist *) (strings + SYM_EXTRA_BYTES); |
548 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0 | |
2c3fc389 | 549 | || bfd_bread (syms, symbol_bytes, abfd) != symbol_bytes) |
252b5132 RH |
550 | { |
551 | bfd_release (abfd, syms); | |
b34976b6 | 552 | return FALSE; |
252b5132 RH |
553 | } |
554 | ||
252b5132 RH |
555 | sym_end = (struct external_nlist *) (((char *) syms) + symbol_bytes); |
556 | ||
557 | /* first, march thru the table and figure out how many symbols there are */ | |
558 | for (sym_pointer = syms; sym_pointer < sym_end; sym_pointer++, num_syms++) | |
559 | { | |
560 | /* skip over the embedded symbol. */ | |
561 | sym_pointer = (struct external_nlist *) (((char *) sym_pointer) + | |
562 | sym_pointer->e_length[0]); | |
563 | } | |
564 | ||
565 | /* now that we know the symbol count, update the bfd header */ | |
566 | bfd_get_symcount (abfd) = num_syms; | |
567 | ||
dc810e39 AM |
568 | amt = num_syms; |
569 | amt *= sizeof (aout_symbol_type); | |
570 | cached = (aout_symbol_type *) bfd_zalloc (abfd, amt); | |
571 | if (cached == NULL && num_syms != 0) | |
b34976b6 | 572 | return FALSE; |
252b5132 RH |
573 | |
574 | /* as we march thru the hp symbol table, convert it into a list of | |
575 | null terminated strings to hold the symbol names. Make sure any | |
576 | assignment to the strings pointer is done after we're thru using | |
577 | the nlist so we don't overwrite anything important. */ | |
578 | ||
7dee875e | 579 | /* OK, now walk the new symtable, caching symbol properties */ |
252b5132 RH |
580 | { |
581 | aout_symbol_type *cache_ptr = cached; | |
252b5132 RH |
582 | /* Run through table and copy values */ |
583 | for (sym_pointer = syms, cache_ptr = cached; | |
584 | sym_pointer < sym_end; sym_pointer++, cache_ptr++) | |
585 | { | |
586 | unsigned int length; | |
587 | cache_ptr->symbol.the_bfd = abfd; | |
588 | cache_ptr->symbol.value = GET_SWORD (abfd, sym_pointer->e_value); | |
589 | cache_ptr->desc = bfd_get_16 (abfd, sym_pointer->e_almod); | |
590 | cache_ptr->type = bfd_get_8 (abfd, sym_pointer->e_type); | |
591 | cache_ptr->symbol.udata.p = NULL; | |
592 | length = bfd_get_8 (abfd, sym_pointer->e_length); | |
593 | cache_ptr->other = length; /* other not used, save length here */ | |
594 | ||
252b5132 RH |
595 | convert_sym_type (sym_pointer, cache_ptr, abfd); |
596 | if (!translate_from_native_sym_flags (abfd, cache_ptr)) | |
b34976b6 | 597 | return FALSE; |
252b5132 RH |
598 | |
599 | /********************************************************/ | |
7dee875e | 600 | /* for hpux, the 'length' value indicates the length of */ |
252b5132 RH |
601 | /* the symbol name which follows the nlist entry. */ |
602 | /********************************************************/ | |
603 | if (length) | |
604 | { | |
605 | /**************************************************************/ | |
606 | /* the hp string is not null terminated so we create a new one*/ | |
607 | /* by copying the string to overlap the just vacated nlist */ | |
608 | /* structure before it in memory. */ | |
609 | /**************************************************************/ | |
610 | cache_ptr->symbol.name = strings; | |
611 | memcpy (strings, sym_pointer + 1, length); | |
612 | strings[length] = '\0'; | |
613 | strings += length + 1; | |
614 | } | |
615 | else | |
616 | cache_ptr->symbol.name = (char *) NULL; | |
617 | ||
618 | /* skip over the embedded symbol. */ | |
619 | sym_pointer = (struct external_nlist *) (((char *) sym_pointer) + | |
620 | length); | |
621 | } | |
622 | } | |
623 | ||
624 | obj_aout_symbols (abfd) = cached; | |
625 | ||
b34976b6 | 626 | return TRUE; |
252b5132 RH |
627 | } |
628 | ||
252b5132 | 629 | void |
2c3fc389 NC |
630 | MY (swap_std_reloc_in) (bfd *abfd, |
631 | struct hp300hpux_reloc *bytes, | |
632 | arelent *cache_ptr, | |
633 | asymbol **symbols, | |
634 | bfd_size_type symcount ATTRIBUTE_UNUSED) | |
252b5132 RH |
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 | ||
dc810e39 AM |
642 | cache_ptr->address = H_GET_32 (abfd, bytes->r_address); |
643 | r_index = H_GET_16 (abfd, bytes->r_index); | |
252b5132 RH |
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 | ||
b34976b6 | 707 | bfd_boolean |
2c3fc389 | 708 | MY (slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols) |
252b5132 | 709 | { |
dc810e39 | 710 | bfd_size_type count; |
252b5132 | 711 | bfd_size_type reloc_size; |
2c3fc389 | 712 | void * relocs; |
252b5132 RH |
713 | arelent *reloc_cache; |
714 | size_t each_size; | |
715 | struct hp300hpux_reloc *rptr; | |
716 | unsigned int counter; | |
717 | arelent *cache_ptr; | |
718 | ||
719 | if (asect->relocation) | |
b34976b6 | 720 | return TRUE; |
252b5132 RH |
721 | |
722 | if (asect->flags & SEC_CONSTRUCTOR) | |
b34976b6 | 723 | return TRUE; |
252b5132 RH |
724 | |
725 | if (asect == obj_datasec (abfd)) | |
726 | { | |
727 | reloc_size = exec_hdr (abfd)->a_drsize; | |
728 | goto doit; | |
729 | } | |
730 | ||
731 | if (asect == obj_textsec (abfd)) | |
732 | { | |
733 | reloc_size = exec_hdr (abfd)->a_trsize; | |
734 | goto doit; | |
735 | } | |
736 | ||
737 | bfd_set_error (bfd_error_invalid_operation); | |
b34976b6 | 738 | return FALSE; |
252b5132 RH |
739 | |
740 | doit: | |
741 | if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0) | |
b34976b6 | 742 | return FALSE; |
252b5132 RH |
743 | each_size = obj_reloc_entry_size (abfd); |
744 | ||
745 | count = reloc_size / each_size; | |
746 | ||
dc810e39 | 747 | reloc_cache = (arelent *) bfd_zalloc (abfd, count * sizeof (arelent)); |
252b5132 | 748 | if (!reloc_cache && count != 0) |
b34976b6 | 749 | return FALSE; |
252b5132 | 750 | |
2c3fc389 | 751 | relocs = bfd_alloc (abfd, reloc_size); |
252b5132 RH |
752 | if (!relocs && reloc_size != 0) |
753 | { | |
754 | bfd_release (abfd, reloc_cache); | |
b34976b6 | 755 | return FALSE; |
252b5132 RH |
756 | } |
757 | ||
dc810e39 | 758 | if (bfd_bread (relocs, reloc_size, abfd) != reloc_size) |
252b5132 RH |
759 | { |
760 | bfd_release (abfd, relocs); | |
761 | bfd_release (abfd, reloc_cache); | |
b34976b6 | 762 | return FALSE; |
252b5132 RH |
763 | } |
764 | ||
765 | rptr = (struct hp300hpux_reloc *) relocs; | |
766 | counter = 0; | |
767 | cache_ptr = reloc_cache; | |
768 | ||
769 | for (; counter < count; counter++, rptr++, cache_ptr++) | |
770 | { | |
771 | MY (swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols, | |
dc810e39 | 772 | (bfd_size_type) bfd_get_symcount (abfd)); |
252b5132 RH |
773 | } |
774 | ||
252b5132 RH |
775 | bfd_release (abfd, relocs); |
776 | asect->relocation = reloc_cache; | |
777 | asect->reloc_count = count; | |
b34976b6 | 778 | return TRUE; |
252b5132 RH |
779 | } |
780 | ||
252b5132 RH |
781 | /************************************************************************/ |
782 | /* The following functions are identical to functions in aoutx.h except */ | |
783 | /* they refer to MY(func) rather than NAME(aout,func) and they also */ | |
784 | /* call aout_32 versions if the input file was generated by gcc */ | |
785 | /************************************************************************/ | |
786 | ||
2c3fc389 NC |
787 | long aout_32_canonicalize_symtab (bfd *, asymbol **); |
788 | long aout_32_get_symtab_upper_bound (bfd *); | |
789 | long aout_32_canonicalize_reloc (bfd *, sec_ptr, arelent **, asymbol **); | |
252b5132 RH |
790 | |
791 | long | |
2c3fc389 | 792 | MY (canonicalize_symtab) (bfd *abfd, asymbol **location) |
252b5132 RH |
793 | { |
794 | unsigned int counter = 0; | |
795 | aout_symbol_type *symbase; | |
796 | ||
797 | if (obj_aout_subformat (abfd) == gnu_encap_format) | |
6cee3f79 | 798 | return aout_32_canonicalize_symtab (abfd, location); |
252b5132 RH |
799 | |
800 | if (!MY (slurp_symbol_table) (abfd)) | |
801 | return -1; | |
802 | ||
803 | for (symbase = obj_aout_symbols (abfd); counter++ < bfd_get_symcount (abfd);) | |
804 | *(location++) = (asymbol *) (symbase++); | |
805 | *location++ = 0; | |
806 | return bfd_get_symcount (abfd); | |
807 | } | |
808 | ||
809 | long | |
2c3fc389 | 810 | MY (get_symtab_upper_bound) (bfd *abfd) |
252b5132 RH |
811 | { |
812 | if (obj_aout_subformat (abfd) == gnu_encap_format) | |
813 | return aout_32_get_symtab_upper_bound (abfd); | |
814 | if (!MY (slurp_symbol_table) (abfd)) | |
815 | return -1; | |
816 | ||
817 | return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *)); | |
818 | } | |
819 | ||
252b5132 | 820 | long |
2c3fc389 NC |
821 | MY (canonicalize_reloc) (bfd *abfd, |
822 | sec_ptr section, | |
823 | arelent **relptr, | |
824 | asymbol **symbols) | |
252b5132 RH |
825 | { |
826 | arelent *tblptr = section->relocation; | |
827 | unsigned int count; | |
2c3fc389 | 828 | |
252b5132 RH |
829 | if (obj_aout_subformat (abfd) == gnu_encap_format) |
830 | return aout_32_canonicalize_reloc (abfd, section, relptr, symbols); | |
831 | ||
832 | if (!(tblptr || MY (slurp_reloc_table) (abfd, section, symbols))) | |
833 | return -1; | |
834 | ||
835 | if (section->flags & SEC_CONSTRUCTOR) | |
836 | { | |
837 | arelent_chain *chain = section->constructor_chain; | |
838 | for (count = 0; count < section->reloc_count; count++) | |
839 | { | |
840 | *relptr++ = &chain->relent; | |
841 | chain = chain->next; | |
842 | } | |
843 | } | |
844 | else | |
845 | { | |
846 | tblptr = section->relocation; | |
847 | ||
848 | for (count = 0; count++ < section->reloc_count;) | |
849 | { | |
850 | *relptr++ = tblptr++; | |
851 | } | |
852 | } | |
853 | *relptr = 0; | |
854 | ||
855 | return section->reloc_count; | |
856 | } | |
857 | ||
252b5132 | 858 | #include "aout-target.h" |