]>
Commit | Line | Data |
---|---|---|
1f29e30b | 1 | /* BFD semi-generic back-end for a.out binaries. |
9783e04a | 2 | Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
88dfcd68 | 3 | Written by Cygnus Support. |
7ed4093a | 4 | |
88dfcd68 | 5 | This file is part of BFD, the Binary File Descriptor library. |
7ed4093a | 6 | |
88dfcd68 | 7 | This program is free software; you can redistribute it and/or modify |
7ed4093a | 8 | it under the terms of the GNU General Public License as published by |
88dfcd68 SC |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
7ed4093a | 11 | |
88dfcd68 | 12 | This program is distributed in the hope that it will be useful, |
7ed4093a SC |
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 | |
88dfcd68 SC |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
7ed4093a | 20 | |
4e41b5aa SC |
21 | /* |
22 | SECTION | |
23 | a.out backends | |
6f715d66 | 24 | |
6f715d66 | 25 | |
4e41b5aa | 26 | DESCRIPTION |
6f715d66 | 27 | |
4e41b5aa SC |
28 | BFD supports a number of different flavours of a.out format, |
29 | though the major differences are only the sizes of the | |
30 | structures on disk, and the shape of the relocation | |
c188b0be | 31 | information. |
6f715d66 | 32 | |
c188b0be | 33 | The support is split into a basic support file @file{aoutx.h} |
4e41b5aa | 34 | and other files which derive functions from the base. One |
c188b0be | 35 | derivation file is @file{aoutf1.h} (for a.out flavour 1), and |
4e41b5aa SC |
36 | adds to the basic a.out functions support for sun3, sun4, 386 |
37 | and 29k a.out files, to create a target jump vector for a | |
c188b0be | 38 | specific target. |
6f715d66 | 39 | |
4e41b5aa | 40 | This information is further split out into more specific files |
c188b0be DM |
41 | for each machine, including @file{sunos.c} for sun3 and sun4, |
42 | @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a | |
4e41b5aa SC |
43 | demonstration of a 64 bit a.out format. |
44 | ||
c188b0be DM |
45 | The base file @file{aoutx.h} defines general mechanisms for |
46 | reading and writing records to and from disk and various | |
4e41b5aa | 47 | other methods which BFD requires. It is included by |
c188b0be DM |
48 | @file{aout32.c} and @file{aout64.c} to form the names |
49 | <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc. | |
4e41b5aa SC |
50 | |
51 | As an example, this is what goes on to make the back end for a | |
c188b0be | 52 | sun4, from @file{aout32.c}: |
4e41b5aa | 53 | |
3f7607af PB |
54 | | #define ARCH_SIZE 32 |
55 | | #include "aoutx.h" | |
4e41b5aa SC |
56 | |
57 | Which exports names: | |
58 | ||
3f7607af PB |
59 | | ... |
60 | | aout_32_canonicalize_reloc | |
61 | | aout_32_find_nearest_line | |
62 | | aout_32_get_lineno | |
63 | | aout_32_get_reloc_upper_bound | |
64 | | ... | |
6f715d66 | 65 | |
c188b0be | 66 | from @file{sunos.c}: |
4e41b5aa | 67 | |
3f7607af PB |
68 | | #define ARCH 32 |
69 | | #define TARGET_NAME "a.out-sunos-big" | |
70 | | #define VECNAME sunos_big_vec | |
71 | | #include "aoutf1.h" | |
4e41b5aa | 72 | |
c188b0be | 73 | requires all the names from @file{aout32.c}, and produces the jump vector |
6f715d66 | 74 | |
3f7607af | 75 | | sunos_big_vec |
c6705697 | 76 | |
c188b0be | 77 | The file @file{host-aout.c} is a special case. It is for a large set |
4e41b5aa SC |
78 | of hosts that use ``more or less standard'' a.out files, and |
79 | for which cross-debugging is not interesting. It uses the | |
80 | standard 32-bit a.out support routines, but determines the | |
81 | file offsets and addresses of the text, data, and BSS | |
82 | sections, the machine architecture and machine type, and the | |
83 | entry point address, in a host-dependent manner. Once these | |
84 | values have been determined, generic code is used to handle | |
c188b0be | 85 | the object file. |
c6705697 | 86 | |
4e41b5aa SC |
87 | When porting it to run on a new system, you must supply: |
88 | ||
3f7607af PB |
89 | | HOST_PAGE_SIZE |
90 | | HOST_SEGMENT_SIZE | |
91 | | HOST_MACHINE_ARCH (optional) | |
92 | | HOST_MACHINE_MACHINE (optional) | |
93 | | HOST_TEXT_START_ADDR | |
94 | | HOST_STACK_END_ADDR | |
c6705697 | 95 | |
4c3721d5 ILT |
96 | in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These |
97 | values, plus the structures and macros defined in @file{a.out.h} on | |
4e41b5aa SC |
98 | your host system, will produce a BFD target that will access |
99 | ordinary a.out files on your host. To configure a new machine | |
4c3721d5 | 100 | to use @file{host-aout.c}, specify: |
c6705697 | 101 | |
3f7607af PB |
102 | | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec |
103 | | TDEPFILES= host-aout.o trad-core.o | |
c6705697 | 104 | |
4c3721d5 ILT |
105 | in the @file{config/@var{XXX}.mt} file, and modify @file{configure.in} |
106 | to use the | |
107 | @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your | |
4e41b5aa | 108 | configuration is selected. |
c6705697 | 109 | |
6f715d66 SC |
110 | */ |
111 | ||
ce07dd7c KR |
112 | /* Some assumptions: |
113 | * Any BFD with D_PAGED set is ZMAGIC, and vice versa. | |
114 | Doesn't matter what the setting of WP_TEXT is on output, but it'll | |
115 | get set on input. | |
116 | * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC. | |
117 | * Any BFD with both flags clear is OMAGIC. | |
118 | (Just want to make these explicit, so the conditions tested in this | |
119 | file make sense if you're more familiar with a.out than with BFD.) */ | |
120 | ||
c618de01 SC |
121 | #define KEEPIT flags |
122 | #define KEEPITTYPE int | |
67c060c3 | 123 | |
0f213cc2 | 124 | #include <assert.h> |
a99c3d70 | 125 | #include <string.h> /* For strchr and friends */ |
67c060c3 | 126 | #include "bfd.h" |
7ed4093a SC |
127 | #include <sysdep.h> |
128 | #include <ansidecl.h> | |
4c3721d5 | 129 | #include "bfdlink.h" |
7ed4093a | 130 | |
6f715d66 | 131 | #include "libaout.h" |
7ed4093a | 132 | #include "libbfd.h" |
c3eb25fc SC |
133 | #include "aout/aout64.h" |
134 | #include "aout/stab_gnu.h" | |
135 | #include "aout/ar.h" | |
7ed4093a | 136 | |
0ee75d02 ILT |
137 | static boolean translate_symbol_table PARAMS ((bfd *, aout_symbol_type *, |
138 | struct external_nlist *, | |
139 | bfd_size_type, char *, | |
140 | bfd_size_type, | |
141 | boolean dynamic)); | |
142 | ||
4e41b5aa SC |
143 | /* |
144 | SUBSECTION | |
4c3721d5 | 145 | Relocations |
4e41b5aa SC |
146 | |
147 | DESCRIPTION | |
c188b0be | 148 | The file @file{aoutx.h} provides for both the @emph{standard} |
4e41b5aa SC |
149 | and @emph{extended} forms of a.out relocation records. |
150 | ||
c188b0be DM |
151 | The standard records contain only an |
152 | address, a symbol index, and a type field. The extended records | |
4e41b5aa | 153 | (used on 29ks and sparcs) also have a full integer for an |
c188b0be | 154 | addend. |
7ed4093a | 155 | |
6f715d66 | 156 | */ |
7ed4093a | 157 | #define CTOR_TABLE_RELOC_IDX 2 |
67c060c3 | 158 | |
ce07dd7c KR |
159 | #define howto_table_ext NAME(aout,ext_howto_table) |
160 | #define howto_table_std NAME(aout,std_howto_table) | |
67c060c3 | 161 | |
c188b0be | 162 | reloc_howto_type howto_table_ext[] = |
7ed4093a | 163 | { |
4c3721d5 | 164 | /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */ |
2e235c93 ILT |
165 | HOWTO(RELOC_8, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", false, 0,0x000000ff, false), |
166 | HOWTO(RELOC_16, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", false, 0,0x0000ffff, false), | |
167 | HOWTO(RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", false, 0,0xffffffff, false), | |
168 | HOWTO(RELOC_DISP8, 0, 0, 8, true, 0, complain_overflow_signed,0,"DISP8", false, 0,0x000000ff, false), | |
169 | HOWTO(RELOC_DISP16, 0, 1, 16, true, 0, complain_overflow_signed,0,"DISP16", false, 0,0x0000ffff, false), | |
170 | HOWTO(RELOC_DISP32, 0, 2, 32, true, 0, complain_overflow_signed,0,"DISP32", false, 0,0xffffffff, false), | |
171 | HOWTO(RELOC_WDISP30,2, 2, 30, true, 0, complain_overflow_signed,0,"WDISP30", false, 0,0x3fffffff, false), | |
172 | HOWTO(RELOC_WDISP22,2, 2, 22, true, 0, complain_overflow_signed,0,"WDISP22", false, 0,0x003fffff, false), | |
173 | HOWTO(RELOC_HI22, 10, 2, 22, false, 0, complain_overflow_bitfield,0,"HI22", false, 0,0x003fffff, false), | |
174 | HOWTO(RELOC_22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"22", false, 0,0x003fffff, false), | |
175 | HOWTO(RELOC_13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"13", false, 0,0x00001fff, false), | |
176 | HOWTO(RELOC_LO10, 0, 2, 10, false, 0, complain_overflow_dont,0,"LO10", false, 0,0x000003ff, false), | |
177 | HOWTO(RELOC_SFA_BASE,0, 2, 32, false, 0, complain_overflow_bitfield,0,"SFA_BASE", false, 0,0xffffffff, false), | |
178 | HOWTO(RELOC_SFA_OFF13,0,2, 32, false, 0, complain_overflow_bitfield,0,"SFA_OFF13",false, 0,0xffffffff, false), | |
179 | HOWTO(RELOC_BASE10, 0, 2, 16, false, 0, complain_overflow_bitfield,0,"BASE10", false, 0,0x0000ffff, false), | |
180 | HOWTO(RELOC_BASE13, 0, 2, 13, false, 0, complain_overflow_bitfield,0,"BASE13", false, 0,0x00001fff, false), | |
181 | HOWTO(RELOC_BASE22, 0, 2, 0, false, 0, complain_overflow_bitfield,0,"BASE22", false, 0,0x00000000, false), | |
182 | HOWTO(RELOC_PC10, 0, 2, 10, false, 0, complain_overflow_bitfield,0,"PC10", false, 0,0x000003ff, false), | |
183 | HOWTO(RELOC_PC22, 0, 2, 22, false, 0, complain_overflow_bitfield,0,"PC22", false, 0,0x003fffff, false), | |
184 | HOWTO(RELOC_JMP_TBL,0, 2, 32, false, 0, complain_overflow_bitfield,0,"JMP_TBL", false, 0,0xffffffff, false), | |
185 | HOWTO(RELOC_SEGOFF16,0, 2, 0, false, 0, complain_overflow_bitfield,0,"SEGOFF16", false, 0,0x00000000, false), | |
186 | HOWTO(RELOC_GLOB_DAT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"GLOB_DAT", false, 0,0x00000000, false), | |
187 | HOWTO(RELOC_JMP_SLOT,0, 2, 0, false, 0, complain_overflow_bitfield,0,"JMP_SLOT", false, 0,0x00000000, false), | |
188 | HOWTO(RELOC_RELATIVE,0, 2, 0, false, 0, complain_overflow_bitfield,0,"RELATIVE", false, 0,0x00000000, false), | |
7ed4093a SC |
189 | }; |
190 | ||
191 | /* Convert standard reloc records to "arelent" format (incl byte swap). */ | |
192 | ||
ce07dd7c | 193 | reloc_howto_type howto_table_std[] = { |
4c3721d5 | 194 | /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */ |
c188b0be | 195 | HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield,0,"8", true, 0x000000ff,0x000000ff, false), |
2e235c93 ILT |
196 | HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"16", true, 0x0000ffff,0x0000ffff, false), |
197 | HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"32", true, 0xffffffff,0xffffffff, false), | |
c188b0be DM |
198 | HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield,0,"64", true, 0xdeaddead,0xdeaddead, false), |
199 | HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed, 0,"DISP8", true, 0x000000ff,0x000000ff, false), | |
200 | HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false), | |
201 | HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed, 0,"DISP32", true, 0xffffffff,0xffffffff, false), | |
202 | HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed, 0,"DISP64", true, 0xfeedface,0xfeedface, false), | |
203 | { -1 }, | |
204 | HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield,0,"BASE16", false,0xffffffff,0xffffffff, false), | |
205 | HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield,0,"BASE32", false,0xffffffff,0xffffffff, false), | |
7ed4093a SC |
206 | }; |
207 | ||
c188b0be DM |
208 | #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0])) |
209 | ||
214f8f23 KR |
210 | CONST struct reloc_howto_struct * |
211 | DEFUN(NAME(aout,reloc_type_lookup),(abfd,code), | |
212 | bfd *abfd AND | |
213 | bfd_reloc_code_real_type code) | |
214 | { | |
215 | #define EXT(i,j) case i: return &howto_table_ext[j] | |
216 | #define STD(i,j) case i: return &howto_table_std[j] | |
217 | int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE; | |
218 | if (code == BFD_RELOC_CTOR) | |
219 | switch (bfd_get_arch_info (abfd)->bits_per_address) | |
220 | { | |
221 | case 32: | |
222 | code = BFD_RELOC_32; | |
223 | break; | |
224 | } | |
225 | if (ext) | |
226 | switch (code) | |
227 | { | |
228 | EXT (BFD_RELOC_32, 2); | |
229 | EXT (BFD_RELOC_HI22, 8); | |
230 | EXT (BFD_RELOC_LO10, 11); | |
231 | EXT (BFD_RELOC_32_PCREL_S2, 6); | |
c188b0be | 232 | EXT (BFD_RELOC_SPARC_WDISP22, 7); |
a99c3d70 | 233 | default: return (CONST struct reloc_howto_struct *) 0; |
214f8f23 KR |
234 | } |
235 | else | |
236 | /* std relocs */ | |
237 | switch (code) | |
238 | { | |
239 | STD (BFD_RELOC_16, 1); | |
240 | STD (BFD_RELOC_32, 2); | |
241 | STD (BFD_RELOC_8_PCREL, 4); | |
242 | STD (BFD_RELOC_16_PCREL, 5); | |
243 | STD (BFD_RELOC_32_PCREL, 6); | |
c188b0be DM |
244 | STD (BFD_RELOC_16_BASEREL, 9); |
245 | STD (BFD_RELOC_32_BASEREL, 10); | |
a99c3d70 | 246 | default: return (CONST struct reloc_howto_struct *) 0; |
214f8f23 | 247 | } |
214f8f23 | 248 | } |
7ed4093a | 249 | |
4e41b5aa SC |
250 | /* |
251 | SUBSECTION | |
4c3721d5 | 252 | Internal entry points |
4e41b5aa SC |
253 | |
254 | DESCRIPTION | |
c188b0be | 255 | @file{aoutx.h} exports several routines for accessing the |
4e41b5aa SC |
256 | contents of an a.out file, which are gathered and exported in |
257 | turn by various format specific files (eg sunos.c). | |
258 | ||
6f715d66 SC |
259 | */ |
260 | ||
4e41b5aa SC |
261 | /* |
262 | FUNCTION | |
c188b0be | 263 | aout_@var{size}_swap_exec_header_in |
4e41b5aa | 264 | |
fa2b89f1 | 265 | SYNOPSIS |
c188b0be | 266 | void aout_@var{size}_swap_exec_header_in, |
4e41b5aa SC |
267 | (bfd *abfd, |
268 | struct external_exec *raw_bytes, | |
269 | struct internal_exec *execp); | |
c188b0be DM |
270 | |
271 | DESCRIPTION | |
272 | Swap the information in an executable header @var{raw_bytes} taken | |
273 | from a raw byte stream memory image into the internal exec header | |
274 | structure @var{execp}. | |
6f715d66 | 275 | */ |
c188b0be | 276 | |
34dd8ba3 | 277 | #ifndef NAME_swap_exec_header_in |
7ed4093a SC |
278 | void |
279 | DEFUN(NAME(aout,swap_exec_header_in),(abfd, raw_bytes, execp), | |
280 | bfd *abfd AND | |
281 | struct external_exec *raw_bytes AND | |
282 | struct internal_exec *execp) | |
283 | { | |
284 | struct external_exec *bytes = (struct external_exec *)raw_bytes; | |
285 | ||
55c0061e FF |
286 | /* The internal_exec structure has some fields that are unused in this |
287 | configuration (IE for i960), so ensure that all such uninitialized | |
288 | fields are zero'd out. There are places where two of these structs | |
289 | are memcmp'd, and thus the contents do matter. */ | |
68241b2b | 290 | memset ((PTR) execp, 0, sizeof (struct internal_exec)); |
7ed4093a SC |
291 | /* Now fill in fields in the execp, from the bytes in the raw data. */ |
292 | execp->a_info = bfd_h_get_32 (abfd, bytes->e_info); | |
293 | execp->a_text = GET_WORD (abfd, bytes->e_text); | |
294 | execp->a_data = GET_WORD (abfd, bytes->e_data); | |
295 | execp->a_bss = GET_WORD (abfd, bytes->e_bss); | |
296 | execp->a_syms = GET_WORD (abfd, bytes->e_syms); | |
297 | execp->a_entry = GET_WORD (abfd, bytes->e_entry); | |
298 | execp->a_trsize = GET_WORD (abfd, bytes->e_trsize); | |
299 | execp->a_drsize = GET_WORD (abfd, bytes->e_drsize); | |
300 | } | |
34dd8ba3 JG |
301 | #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in) |
302 | #endif | |
7ed4093a | 303 | |
4e41b5aa SC |
304 | /* |
305 | FUNCTION | |
c188b0be | 306 | aout_@var{size}_swap_exec_header_out |
4e41b5aa | 307 | |
fa2b89f1 | 308 | SYNOPSIS |
c188b0be | 309 | void aout_@var{size}_swap_exec_header_out |
6f715d66 SC |
310 | (bfd *abfd, |
311 | struct internal_exec *execp, | |
4e41b5aa | 312 | struct external_exec *raw_bytes); |
c188b0be DM |
313 | |
314 | DESCRIPTION | |
315 | Swap the information in an internal exec header structure | |
316 | @var{execp} into the buffer @var{raw_bytes} ready for writing to disk. | |
6f715d66 | 317 | */ |
7ed4093a SC |
318 | void |
319 | DEFUN(NAME(aout,swap_exec_header_out),(abfd, execp, raw_bytes), | |
320 | bfd *abfd AND | |
c188b0be | 321 | struct internal_exec *execp AND |
7ed4093a SC |
322 | struct external_exec *raw_bytes) |
323 | { | |
324 | struct external_exec *bytes = (struct external_exec *)raw_bytes; | |
325 | ||
326 | /* Now fill in fields in the raw data, from the fields in the exec struct. */ | |
327 | bfd_h_put_32 (abfd, execp->a_info , bytes->e_info); | |
328 | PUT_WORD (abfd, execp->a_text , bytes->e_text); | |
329 | PUT_WORD (abfd, execp->a_data , bytes->e_data); | |
330 | PUT_WORD (abfd, execp->a_bss , bytes->e_bss); | |
331 | PUT_WORD (abfd, execp->a_syms , bytes->e_syms); | |
332 | PUT_WORD (abfd, execp->a_entry , bytes->e_entry); | |
333 | PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize); | |
334 | PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize); | |
335 | } | |
336 | ||
7ed4093a | 337 | |
6f715d66 | 338 | |
4e41b5aa SC |
339 | /* |
340 | FUNCTION | |
c188b0be | 341 | aout_@var{size}_some_aout_object_p |
6f715d66 | 342 | |
fa2b89f1 | 343 | SYNOPSIS |
c188b0be | 344 | bfd_target *aout_@var{size}_some_aout_object_p |
6f715d66 | 345 | (bfd *abfd, |
4e41b5aa | 346 | bfd_target *(*callback_to_real_object_p)()); |
c188b0be DM |
347 | |
348 | DESCRIPTION | |
349 | Some a.out variant thinks that the file open in @var{abfd} | |
350 | checking is an a.out file. Do some more checking, and set up | |
351 | for access if it really is. Call back to the calling | |
352 | environment's "finish up" function just before returning, to | |
353 | handle any last-minute setup. | |
6f715d66 | 354 | */ |
c188b0be | 355 | |
7ed4093a | 356 | bfd_target * |
7b02b4ed | 357 | DEFUN(NAME(aout,some_aout_object_p),(abfd, execp, callback_to_real_object_p), |
7ed4093a | 358 | bfd *abfd AND |
7b02b4ed | 359 | struct internal_exec *execp AND |
b86f998b | 360 | bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *))) |
7ed4093a | 361 | { |
214f8f23 | 362 | struct aout_data_struct *rawptr, *oldrawptr; |
e6e265ce | 363 | bfd_target *result; |
7ed4093a | 364 | |
6db82ea7 | 365 | rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (struct aout_data_struct )); |
7ed4093a | 366 | if (rawptr == NULL) { |
68241b2b | 367 | bfd_set_error (bfd_error_no_memory); |
7ed4093a SC |
368 | return 0; |
369 | } | |
370 | ||
214f8f23 | 371 | oldrawptr = abfd->tdata.aout_data; |
6db82ea7 | 372 | abfd->tdata.aout_data = rawptr; |
ebd24135 ILT |
373 | |
374 | /* Copy the contents of the old tdata struct. | |
375 | In particular, we want the subformat, since for hpux it was set in | |
376 | hp300hpux.c:swap_exec_header_in and will be used in | |
377 | hp300hpux.c:callback. */ | |
378 | if (oldrawptr != NULL) | |
379 | *abfd->tdata.aout_data = *oldrawptr; | |
380 | ||
6db82ea7 SC |
381 | abfd->tdata.aout_data->a.hdr = &rawptr->e; |
382 | *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */ | |
383 | execp = abfd->tdata.aout_data->a.hdr; | |
7ed4093a SC |
384 | |
385 | /* Set the file flags */ | |
386 | abfd->flags = NO_FLAGS; | |
387 | if (execp->a_drsize || execp->a_trsize) | |
388 | abfd->flags |= HAS_RELOC; | |
e6e265ce | 389 | /* Setting of EXEC_P has been deferred to the bottom of this function */ |
c188b0be | 390 | if (execp->a_syms) |
7ed4093a | 391 | abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS; |
e68de5d5 ILT |
392 | if (N_DYNAMIC(*execp)) |
393 | abfd->flags |= DYNAMIC; | |
7ed4093a | 394 | |
ce07dd7c KR |
395 | if (N_MAGIC (*execp) == ZMAGIC) |
396 | { | |
397 | abfd->flags |= D_PAGED|WP_TEXT; | |
398 | adata(abfd).magic = z_magic; | |
399 | } | |
400 | else if (N_MAGIC (*execp) == NMAGIC) | |
401 | { | |
402 | abfd->flags |= WP_TEXT; | |
403 | adata(abfd).magic = n_magic; | |
404 | } | |
405 | else | |
406 | adata(abfd).magic = o_magic; | |
7ed4093a SC |
407 | |
408 | bfd_get_start_address (abfd) = execp->a_entry; | |
409 | ||
410 | obj_aout_symbols (abfd) = (aout_symbol_type *)NULL; | |
411 | bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist); | |
412 | ||
7ed4093a SC |
413 | /* The default relocation entry size is that of traditional V7 Unix. */ |
414 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; | |
415 | ||
7b02b4ed JG |
416 | /* The default symbol entry size is that of traditional Unix. */ |
417 | obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE; | |
418 | ||
728472f1 ILT |
419 | obj_aout_external_syms (abfd) = NULL; |
420 | obj_aout_external_strings (abfd) = NULL; | |
421 | obj_aout_sym_hashes (abfd) = NULL; | |
422 | ||
2e235c93 ILT |
423 | /* Create the sections. This is raunchy, but bfd_close wants to reclaim |
424 | them. */ | |
6db82ea7 | 425 | |
214f8f23 KR |
426 | obj_textsec (abfd) = bfd_make_section_old_way (abfd, ".text"); |
427 | obj_datasec (abfd) = bfd_make_section_old_way (abfd, ".data"); | |
428 | obj_bsssec (abfd) = bfd_make_section_old_way (abfd, ".bss"); | |
429 | ||
430 | #if 0 | |
431 | (void)bfd_make_section (abfd, ".text"); | |
432 | (void)bfd_make_section (abfd, ".data"); | |
433 | (void)bfd_make_section (abfd, ".bss"); | |
434 | #endif | |
7ed4093a | 435 | |
6db82ea7 SC |
436 | obj_datasec (abfd)->_raw_size = execp->a_data; |
437 | obj_bsssec (abfd)->_raw_size = execp->a_bss; | |
7ed4093a | 438 | |
0ee75d02 ILT |
439 | /* If this object is dynamically linked, we assume that both |
440 | sections have relocs. This does no real harm, even though it may | |
441 | not be true. */ | |
442 | obj_textsec (abfd)->flags = | |
443 | (execp->a_trsize != 0 || (abfd->flags & DYNAMIC) != 0 | |
444 | ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC) | |
445 | : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS)); | |
446 | obj_datasec (abfd)->flags = | |
447 | (execp->a_drsize != 0 || (abfd->flags & DYNAMIC) != 0 | |
448 | ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC) | |
449 | : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS)); | |
7ed4093a SC |
450 | obj_bsssec (abfd)->flags = SEC_ALLOC; |
451 | ||
452 | #ifdef THIS_IS_ONLY_DOCUMENTATION | |
98d43107 JG |
453 | /* The common code can't fill in these things because they depend |
454 | on either the start address of the text segment, the rounding | |
9783e04a | 455 | up of virtual addresses between segments, or the starting file |
98d43107 JG |
456 | position of the text segment -- all of which varies among different |
457 | versions of a.out. */ | |
458 | ||
c188b0be | 459 | /* Call back to the format-dependent code to fill in the rest of the |
7ed4093a SC |
460 | fields and do any further cleanup. Things that should be filled |
461 | in by the callback: */ | |
462 | ||
463 | struct exec *execp = exec_hdr (abfd); | |
464 | ||
98d43107 | 465 | obj_textsec (abfd)->size = N_TXTSIZE(*execp); |
6db82ea7 | 466 | obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp); |
98d43107 JG |
467 | /* data and bss are already filled in since they're so standard */ |
468 | ||
7ed4093a | 469 | /* The virtual memory addresses of the sections */ |
7ed4093a | 470 | obj_textsec (abfd)->vma = N_TXTADDR(*execp); |
98d43107 JG |
471 | obj_datasec (abfd)->vma = N_DATADDR(*execp); |
472 | obj_bsssec (abfd)->vma = N_BSSADDR(*execp); | |
7ed4093a SC |
473 | |
474 | /* The file offsets of the sections */ | |
475 | obj_textsec (abfd)->filepos = N_TXTOFF(*execp); | |
476 | obj_datasec (abfd)->filepos = N_DATOFF(*execp); | |
477 | ||
478 | /* The file offsets of the relocation info */ | |
479 | obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp); | |
480 | obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp); | |
481 | ||
482 | /* The file offsets of the string table and symbol table. */ | |
483 | obj_str_filepos (abfd) = N_STROFF (*execp); | |
484 | obj_sym_filepos (abfd) = N_SYMOFF (*execp); | |
485 | ||
7ed4093a SC |
486 | /* Determine the architecture and machine type of the object file. */ |
487 | switch (N_MACHTYPE (*exec_hdr (abfd))) { | |
488 | default: | |
489 | abfd->obj_arch = bfd_arch_obscure; | |
490 | break; | |
491 | } | |
492 | ||
7b02b4ed JG |
493 | adata(abfd)->page_size = PAGE_SIZE; |
494 | adata(abfd)->segment_size = SEGMENT_SIZE; | |
495 | adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE; | |
496 | ||
7ed4093a SC |
497 | return abfd->xvec; |
498 | ||
499 | /* The architecture is encoded in various ways in various a.out variants, | |
500 | or is not encoded at all in some of them. The relocation size depends | |
501 | on the architecture and the a.out variant. Finally, the return value | |
502 | is the bfd_target vector in use. If an error occurs, return zero and | |
503 | set bfd_error to the appropriate error code. | |
c188b0be | 504 | |
7ed4093a SC |
505 | Formats such as b.out, which have additional fields in the a.out |
506 | header, should cope with them in this callback as well. */ | |
507 | #endif /* DOCUMENTATION */ | |
508 | ||
e6e265ce JG |
509 | result = (*callback_to_real_object_p)(abfd); |
510 | ||
511 | /* Now that the segment addresses have been worked out, take a better | |
512 | guess at whether the file is executable. If the entry point | |
513 | is within the text segment, assume it is. (This makes files | |
514 | executable even if their entry point address is 0, as long as | |
c188b0be | 515 | their text starts at zero.) |
e6e265ce JG |
516 | |
517 | At some point we should probably break down and stat the file and | |
518 | declare it executable if (one of) its 'x' bits are on... */ | |
519 | if ((execp->a_entry >= obj_textsec(abfd)->vma) && | |
6db82ea7 | 520 | (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size)) |
e6e265ce | 521 | abfd->flags |= EXEC_P; |
214f8f23 KR |
522 | if (result) |
523 | { | |
1f29e30b | 524 | #if 0 /* These should be set correctly anyways. */ |
214f8f23 KR |
525 | abfd->sections = obj_textsec (abfd); |
526 | obj_textsec (abfd)->next = obj_datasec (abfd); | |
527 | obj_datasec (abfd)->next = obj_bsssec (abfd); | |
1f29e30b | 528 | #endif |
214f8f23 KR |
529 | } |
530 | else | |
531 | { | |
532 | free (rawptr); | |
533 | abfd->tdata.aout_data = oldrawptr; | |
534 | } | |
e6e265ce | 535 | return result; |
7ed4093a SC |
536 | } |
537 | ||
4e41b5aa SC |
538 | /* |
539 | FUNCTION | |
c188b0be | 540 | aout_@var{size}_mkobject |
6f715d66 | 541 | |
fa2b89f1 | 542 | SYNOPSIS |
c188b0be DM |
543 | boolean aout_@var{size}_mkobject, (bfd *abfd); |
544 | ||
545 | DESCRIPTION | |
546 | Initialize BFD @var{abfd} for use with a.out files. | |
6f715d66 | 547 | */ |
7ed4093a SC |
548 | |
549 | boolean | |
550 | DEFUN(NAME(aout,mkobject),(abfd), | |
551 | bfd *abfd) | |
552 | { | |
6db82ea7 | 553 | struct aout_data_struct *rawptr; |
7ed4093a | 554 | |
68241b2b | 555 | bfd_set_error (bfd_error_system_call); |
7ed4093a SC |
556 | |
557 | /* Use an intermediate variable for clarity */ | |
2e235c93 | 558 | rawptr = (struct aout_data_struct *)bfd_zalloc (abfd, sizeof (struct aout_data_struct )); |
c188b0be | 559 | |
7ed4093a | 560 | if (rawptr == NULL) { |
68241b2b | 561 | bfd_set_error (bfd_error_no_memory); |
7ed4093a SC |
562 | return false; |
563 | } | |
c188b0be | 564 | |
6db82ea7 | 565 | abfd->tdata.aout_data = rawptr; |
7ed4093a | 566 | exec_hdr (abfd) = &(rawptr->e); |
c188b0be | 567 | |
7ed4093a | 568 | /* For simplicity's sake we just make all the sections right here. */ |
c188b0be | 569 | |
7ed4093a SC |
570 | obj_textsec (abfd) = (asection *)NULL; |
571 | obj_datasec (abfd) = (asection *)NULL; | |
572 | obj_bsssec (abfd) = (asection *)NULL; | |
573 | bfd_make_section (abfd, ".text"); | |
574 | bfd_make_section (abfd, ".data"); | |
575 | bfd_make_section (abfd, ".bss"); | |
6db82ea7 SC |
576 | bfd_make_section (abfd, BFD_ABS_SECTION_NAME); |
577 | bfd_make_section (abfd, BFD_UND_SECTION_NAME); | |
578 | bfd_make_section (abfd, BFD_COM_SECTION_NAME); | |
c188b0be | 579 | |
7ed4093a SC |
580 | return true; |
581 | } | |
582 | ||
6f715d66 | 583 | |
4e41b5aa SC |
584 | /* |
585 | FUNCTION | |
c188b0be DM |
586 | aout_@var{size}_machine_type |
587 | ||
588 | SYNOPSIS | |
589 | enum machine_type aout_@var{size}_machine_type | |
590 | (enum bfd_architecture arch, | |
591 | unsigned long machine)); | |
6f715d66 | 592 | |
4e41b5aa SC |
593 | DESCRIPTION |
594 | Keep track of machine architecture and machine type for | |
c188b0be DM |
595 | a.out's. Return the <<machine_type>> for a particular |
596 | architecture and machine, or <<M_UNKNOWN>> if that exact architecture | |
597 | and machine can't be represented in a.out format. | |
7ed4093a | 598 | |
4e41b5aa | 599 | If the architecture is understood, machine type 0 (default) |
c188b0be | 600 | is always understood. |
6f715d66 | 601 | */ |
7ed4093a SC |
602 | |
603 | enum machine_type | |
604 | DEFUN(NAME(aout,machine_type),(arch, machine), | |
605 | enum bfd_architecture arch AND | |
606 | unsigned long machine) | |
607 | { | |
608 | enum machine_type arch_flags; | |
c188b0be | 609 | |
7ed4093a | 610 | arch_flags = M_UNKNOWN; |
c188b0be | 611 | |
7ed4093a SC |
612 | switch (arch) { |
613 | case bfd_arch_sparc: | |
614 | if (machine == 0) arch_flags = M_SPARC; | |
615 | break; | |
c188b0be | 616 | |
7ed4093a SC |
617 | case bfd_arch_m68k: |
618 | switch (machine) { | |
619 | case 0: arch_flags = M_68010; break; | |
620 | case 68000: arch_flags = M_UNKNOWN; break; | |
621 | case 68010: arch_flags = M_68010; break; | |
622 | case 68020: arch_flags = M_68020; break; | |
623 | default: arch_flags = M_UNKNOWN; break; | |
624 | } | |
625 | break; | |
c188b0be | 626 | |
7ed4093a SC |
627 | case bfd_arch_i386: |
628 | if (machine == 0) arch_flags = M_386; | |
629 | break; | |
c188b0be | 630 | |
7ed4093a SC |
631 | case bfd_arch_a29k: |
632 | if (machine == 0) arch_flags = M_29K; | |
633 | break; | |
c188b0be | 634 | |
5cd3dcff KR |
635 | case bfd_arch_mips: |
636 | switch (machine) { | |
637 | case 0: | |
638 | case 2000: | |
639 | case 3000: arch_flags = M_MIPS1; break; | |
640 | case 4000: | |
641 | case 4400: | |
642 | case 6000: arch_flags = M_MIPS2; break; | |
643 | default: arch_flags = M_UNKNOWN; break; | |
644 | } | |
645 | break; | |
646 | ||
7ed4093a SC |
647 | default: |
648 | arch_flags = M_UNKNOWN; | |
7ed4093a SC |
649 | } |
650 | return arch_flags; | |
651 | } | |
652 | ||
9e2dad8e | 653 | |
4e41b5aa SC |
654 | /* |
655 | FUNCTION | |
c188b0be | 656 | aout_@var{size}_set_arch_mach |
6f715d66 | 657 | |
fa2b89f1 | 658 | SYNOPSIS |
c188b0be | 659 | boolean aout_@var{size}_set_arch_mach, |
6f715d66 | 660 | (bfd *, |
c188b0be | 661 | enum bfd_architecture arch, |
6f715d66 | 662 | unsigned long machine)); |
c188b0be DM |
663 | |
664 | DESCRIPTION | |
665 | Set the architecture and the machine of the BFD @var{abfd} to the | |
666 | values @var{arch} and @var{machine}. Verify that @var{abfd}'s format | |
667 | can support the architecture required. | |
6f715d66 SC |
668 | */ |
669 | ||
7ed4093a SC |
670 | boolean |
671 | DEFUN(NAME(aout,set_arch_mach),(abfd, arch, machine), | |
672 | bfd *abfd AND | |
673 | enum bfd_architecture arch AND | |
674 | unsigned long machine) | |
675 | { | |
2e235c93 ILT |
676 | if (! bfd_default_set_arch_mach (abfd, arch, machine)) |
677 | return false; | |
678 | ||
7ed4093a SC |
679 | if (arch != bfd_arch_unknown && |
680 | NAME(aout,machine_type) (arch, machine) == M_UNKNOWN) | |
681 | return false; /* We can't represent this type */ | |
ce07dd7c | 682 | |
214f8f23 KR |
683 | /* Determine the size of a relocation entry */ |
684 | switch (arch) { | |
685 | case bfd_arch_sparc: | |
686 | case bfd_arch_a29k: | |
5cd3dcff | 687 | case bfd_arch_mips: |
214f8f23 KR |
688 | obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE; |
689 | break; | |
690 | default: | |
691 | obj_reloc_entry_size (abfd) = RELOC_STD_SIZE; | |
692 | break; | |
693 | } | |
694 | ||
2768b3f7 | 695 | return (*aout_backend_info(abfd)->set_sizes) (abfd); |
7ed4093a | 696 | } |
7ed4093a | 697 | |
4c3721d5 ILT |
698 | static void |
699 | adjust_o_magic (abfd, execp) | |
700 | bfd *abfd; | |
701 | struct internal_exec *execp; | |
702 | { | |
703 | file_ptr pos = adata (abfd).exec_bytes_size; | |
704 | bfd_vma vma = 0; | |
705 | int pad = 0; | |
706 | ||
707 | /* Text. */ | |
708 | obj_textsec(abfd)->filepos = pos; | |
709 | pos += obj_textsec(abfd)->_raw_size; | |
710 | vma += obj_textsec(abfd)->_raw_size; | |
711 | ||
712 | /* Data. */ | |
713 | if (!obj_datasec(abfd)->user_set_vma) | |
714 | { | |
715 | #if 0 /* ?? Does alignment in the file image really matter? */ | |
716 | pad = align_power (vma, obj_datasec(abfd)->alignment_power) - vma; | |
717 | #endif | |
718 | obj_textsec(abfd)->_raw_size += pad; | |
719 | pos += pad; | |
720 | vma += pad; | |
721 | obj_datasec(abfd)->vma = vma; | |
722 | } | |
723 | obj_datasec(abfd)->filepos = pos; | |
724 | pos += obj_datasec(abfd)->_raw_size; | |
725 | vma += obj_datasec(abfd)->_raw_size; | |
726 | ||
727 | /* BSS. */ | |
728 | if (!obj_bsssec(abfd)->user_set_vma) | |
729 | { | |
730 | #if 0 | |
731 | pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma; | |
732 | #endif | |
733 | obj_datasec(abfd)->_raw_size += pad; | |
734 | pos += pad; | |
735 | vma += pad; | |
736 | obj_bsssec(abfd)->vma = vma; | |
737 | } | |
738 | obj_bsssec(abfd)->filepos = pos; | |
739 | ||
740 | /* Fix up the exec header. */ | |
741 | execp->a_text = obj_textsec(abfd)->_raw_size; | |
742 | execp->a_data = obj_datasec(abfd)->_raw_size; | |
743 | execp->a_bss = obj_bsssec(abfd)->_raw_size; | |
744 | N_SET_MAGIC (*execp, OMAGIC); | |
745 | } | |
746 | ||
747 | static void | |
748 | adjust_z_magic (abfd, execp) | |
749 | bfd *abfd; | |
750 | struct internal_exec *execp; | |
751 | { | |
752 | bfd_size_type data_pad, text_pad; | |
753 | file_ptr text_end; | |
754 | CONST struct aout_backend_data *abdp; | |
755 | int ztih; /* Nonzero if text includes exec header. */ | |
4c3721d5 ILT |
756 | |
757 | abdp = aout_backend_info (abfd); | |
758 | ||
759 | /* Text. */ | |
760 | ztih = abdp && abdp->text_includes_header; | |
761 | obj_textsec(abfd)->filepos = (ztih | |
762 | ? adata(abfd).exec_bytes_size | |
763 | : adata(abfd).page_size); | |
764 | if (! obj_textsec(abfd)->user_set_vma) | |
765 | /* ?? Do we really need to check for relocs here? */ | |
766 | obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC) | |
767 | ? 0 | |
768 | : (ztih | |
769 | ? (abdp->default_text_vma | |
770 | + adata(abfd).exec_bytes_size) | |
771 | : abdp->default_text_vma)); | |
772 | /* Could take strange alignment of text section into account here? */ | |
773 | ||
774 | /* Find start of data. */ | |
775 | text_end = obj_textsec(abfd)->filepos + obj_textsec(abfd)->_raw_size; | |
776 | text_pad = BFD_ALIGN (text_end, adata(abfd).page_size) - text_end; | |
777 | obj_textsec(abfd)->_raw_size += text_pad; | |
778 | text_end += text_pad; | |
779 | ||
780 | /* Data. */ | |
781 | if (!obj_datasec(abfd)->user_set_vma) | |
782 | { | |
783 | bfd_vma vma; | |
784 | vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size; | |
785 | obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size); | |
786 | } | |
4c3721d5 ILT |
787 | if (abdp && abdp->zmagic_mapped_contiguous) |
788 | { | |
789 | text_pad = (obj_datasec(abfd)->vma | |
790 | - obj_textsec(abfd)->vma | |
791 | - obj_textsec(abfd)->_raw_size); | |
792 | obj_textsec(abfd)->_raw_size += text_pad; | |
793 | } | |
794 | obj_datasec(abfd)->filepos = (obj_textsec(abfd)->filepos | |
795 | + obj_textsec(abfd)->_raw_size); | |
796 | ||
797 | /* Fix up exec header while we're at it. */ | |
798 | execp->a_text = obj_textsec(abfd)->_raw_size; | |
799 | if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted))) | |
800 | execp->a_text += adata(abfd).exec_bytes_size; | |
801 | N_SET_MAGIC (*execp, ZMAGIC); | |
5330499f | 802 | |
4c3721d5 | 803 | /* Spec says data section should be rounded up to page boundary. */ |
4c3721d5 ILT |
804 | obj_datasec(abfd)->_raw_size |
805 | = align_power (obj_datasec(abfd)->_raw_size, | |
806 | obj_bsssec(abfd)->alignment_power); | |
807 | execp->a_data = BFD_ALIGN (obj_datasec(abfd)->_raw_size, | |
808 | adata(abfd).page_size); | |
809 | data_pad = execp->a_data - obj_datasec(abfd)->_raw_size; | |
810 | ||
811 | /* BSS. */ | |
812 | if (!obj_bsssec(abfd)->user_set_vma) | |
813 | obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma | |
814 | + obj_datasec(abfd)->_raw_size); | |
5330499f DM |
815 | /* If the BSS immediately follows the data section and extra space |
816 | in the page is left after the data section, fudge data | |
817 | in the header so that the bss section looks smaller by that | |
818 | amount. We'll start the bss section there, and lie to the OS. | |
819 | (Note that a linker script, as well as the above assignment, | |
820 | could have explicitly set the BSS vma to immediately follow | |
821 | the data section.) */ | |
822 | if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power) | |
823 | == obj_datasec(abfd)->vma + obj_datasec(abfd)->_raw_size) | |
824 | execp->a_bss = (data_pad > obj_bsssec(abfd)->_raw_size) ? 0 : | |
825 | obj_bsssec(abfd)->_raw_size - data_pad; | |
826 | else | |
827 | execp->a_bss = obj_bsssec(abfd)->_raw_size; | |
4c3721d5 ILT |
828 | } |
829 | ||
830 | static void | |
831 | adjust_n_magic (abfd, execp) | |
832 | bfd *abfd; | |
833 | struct internal_exec *execp; | |
834 | { | |
835 | file_ptr pos = adata(abfd).exec_bytes_size; | |
836 | bfd_vma vma = 0; | |
837 | int pad; | |
838 | ||
839 | /* Text. */ | |
840 | obj_textsec(abfd)->filepos = pos; | |
841 | if (!obj_textsec(abfd)->user_set_vma) | |
842 | obj_textsec(abfd)->vma = vma; | |
843 | else | |
844 | vma = obj_textsec(abfd)->vma; | |
845 | pos += obj_textsec(abfd)->_raw_size; | |
846 | vma += obj_textsec(abfd)->_raw_size; | |
847 | ||
848 | /* Data. */ | |
849 | obj_datasec(abfd)->filepos = pos; | |
850 | if (!obj_datasec(abfd)->user_set_vma) | |
851 | obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size); | |
852 | vma = obj_datasec(abfd)->vma; | |
853 | ||
854 | /* Since BSS follows data immediately, see if it needs alignment. */ | |
855 | vma += obj_datasec(abfd)->_raw_size; | |
856 | pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma; | |
857 | obj_datasec(abfd)->_raw_size += pad; | |
858 | pos += obj_datasec(abfd)->_raw_size; | |
859 | ||
860 | /* BSS. */ | |
861 | if (!obj_bsssec(abfd)->user_set_vma) | |
862 | obj_bsssec(abfd)->vma = vma; | |
863 | else | |
864 | vma = obj_bsssec(abfd)->vma; | |
865 | ||
866 | /* Fix up exec header. */ | |
867 | execp->a_text = obj_textsec(abfd)->_raw_size; | |
868 | execp->a_data = obj_datasec(abfd)->_raw_size; | |
869 | execp->a_bss = obj_bsssec(abfd)->_raw_size; | |
870 | N_SET_MAGIC (*execp, NMAGIC); | |
871 | } | |
872 | ||
ce07dd7c | 873 | boolean |
4c3721d5 | 874 | DEFUN (NAME(aout,adjust_sizes_and_vmas), (abfd, text_size, text_end), |
ce07dd7c KR |
875 | bfd *abfd AND bfd_size_type *text_size AND file_ptr *text_end) |
876 | { | |
877 | struct internal_exec *execp = exec_hdr (abfd); | |
4c3721d5 | 878 | |
c188b0be | 879 | if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL)) |
ce07dd7c | 880 | { |
68241b2b | 881 | bfd_set_error (bfd_error_invalid_operation); |
ce07dd7c KR |
882 | return false; |
883 | } | |
884 | if (adata(abfd).magic != undecided_magic) return true; | |
4c3721d5 | 885 | |
c188b0be | 886 | obj_textsec(abfd)->_raw_size = |
ce07dd7c KR |
887 | align_power(obj_textsec(abfd)->_raw_size, |
888 | obj_textsec(abfd)->alignment_power); | |
889 | ||
890 | *text_size = obj_textsec (abfd)->_raw_size; | |
891 | /* Rule (heuristic) for when to pad to a new page. Note that there | |
4c3721d5 ILT |
892 | are (at least) two ways demand-paged (ZMAGIC) files have been |
893 | handled. Most Berkeley-based systems start the text segment at | |
894 | (PAGE_SIZE). However, newer versions of SUNOS start the text | |
895 | segment right after the exec header; the latter is counted in the | |
896 | text segment size, and is paged in by the kernel with the rest of | |
897 | the text. */ | |
ce07dd7c KR |
898 | |
899 | /* This perhaps isn't the right way to do this, but made it simpler for me | |
900 | to understand enough to implement it. Better would probably be to go | |
901 | right from BFD flags to alignment/positioning characteristics. But the | |
902 | old code was sloppy enough about handling the flags, and had enough | |
903 | other magic, that it was a little hard for me to understand. I think | |
904 | I understand it better now, but I haven't time to do the cleanup this | |
905 | minute. */ | |
4c3721d5 ILT |
906 | |
907 | if (abfd->flags & D_PAGED) | |
908 | /* Whether or not WP_TEXT is set -- let D_PAGED override. */ | |
909 | /* @@ What about QMAGIC? */ | |
910 | adata(abfd).magic = z_magic; | |
911 | else if (abfd->flags & WP_TEXT) | |
912 | adata(abfd).magic = n_magic; | |
913 | else | |
914 | adata(abfd).magic = o_magic; | |
ce07dd7c KR |
915 | |
916 | #ifdef BFD_AOUT_DEBUG /* requires gcc2 */ | |
917 | #if __GNUC__ >= 2 | |
918 | fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n", | |
919 | ({ char *str; | |
920 | switch (adata(abfd).magic) { | |
921 | case n_magic: str = "NMAGIC"; break; | |
922 | case o_magic: str = "OMAGIC"; break; | |
923 | case z_magic: str = "ZMAGIC"; break; | |
924 | default: abort (); | |
925 | } | |
926 | str; | |
927 | }), | |
4c3721d5 ILT |
928 | obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size, |
929 | obj_textsec(abfd)->alignment_power, | |
930 | obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size, | |
931 | obj_datasec(abfd)->alignment_power, | |
932 | obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size, | |
933 | obj_bsssec(abfd)->alignment_power); | |
ce07dd7c KR |
934 | #endif |
935 | #endif | |
936 | ||
937 | switch (adata(abfd).magic) | |
938 | { | |
939 | case o_magic: | |
4c3721d5 | 940 | adjust_o_magic (abfd, execp); |
ce07dd7c KR |
941 | break; |
942 | case z_magic: | |
4c3721d5 | 943 | adjust_z_magic (abfd, execp); |
ce07dd7c KR |
944 | break; |
945 | case n_magic: | |
4c3721d5 | 946 | adjust_n_magic (abfd, execp); |
ce07dd7c KR |
947 | break; |
948 | default: | |
949 | abort (); | |
950 | } | |
4c3721d5 | 951 | |
ce07dd7c KR |
952 | #ifdef BFD_AOUT_DEBUG |
953 | fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n", | |
4c3721d5 ILT |
954 | obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size, |
955 | obj_textsec(abfd)->filepos, | |
956 | obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size, | |
957 | obj_datasec(abfd)->filepos, | |
ce07dd7c KR |
958 | obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size); |
959 | #endif | |
4c3721d5 | 960 | |
d047d16a | 961 | return true; |
ce07dd7c KR |
962 | } |
963 | ||
4e41b5aa SC |
964 | /* |
965 | FUNCTION | |
c188b0be | 966 | aout_@var{size}_new_section_hook |
4e41b5aa | 967 | |
fa2b89f1 | 968 | SYNOPSIS |
c188b0be | 969 | boolean aout_@var{size}_new_section_hook, |
9e2dad8e JG |
970 | (bfd *abfd, |
971 | asection *newsect)); | |
c188b0be DM |
972 | |
973 | DESCRIPTION | |
974 | Called by the BFD in response to a @code{bfd_make_section} | |
975 | request. | |
6f715d66 | 976 | */ |
7ed4093a | 977 | boolean |
3f7607af | 978 | DEFUN(NAME(aout,new_section_hook),(abfd, newsect), |
9e2dad8e JG |
979 | bfd *abfd AND |
980 | asection *newsect) | |
7ed4093a | 981 | { |
6db82ea7 SC |
982 | /* align to double at least */ |
983 | newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power; | |
3f7607af | 984 | |
c188b0be DM |
985 | |
986 | if (bfd_get_format (abfd) == bfd_object) | |
6db82ea7 SC |
987 | { |
988 | if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) { | |
989 | obj_textsec(abfd)= newsect; | |
990 | newsect->target_index = N_TEXT | N_EXT; | |
991 | return true; | |
992 | } | |
c188b0be | 993 | |
6db82ea7 SC |
994 | if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) { |
995 | obj_datasec(abfd) = newsect; | |
996 | newsect->target_index = N_DATA | N_EXT; | |
997 | return true; | |
998 | } | |
c188b0be | 999 | |
6db82ea7 SC |
1000 | if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) { |
1001 | obj_bsssec(abfd) = newsect; | |
1002 | newsect->target_index = N_BSS | N_EXT; | |
1003 | return true; | |
1004 | } | |
1005 | ||
1006 | } | |
c188b0be | 1007 | |
6db82ea7 SC |
1008 | /* We allow more than three sections internally */ |
1009 | return true; | |
7ed4093a SC |
1010 | } |
1011 | ||
1012 | boolean | |
2e235c93 ILT |
1013 | DEFUN(NAME(aout,set_section_contents),(abfd, section, location, offset, count), |
1014 | bfd *abfd AND | |
1015 | sec_ptr section AND | |
1016 | PTR location AND | |
1017 | file_ptr offset AND | |
1018 | bfd_size_type count) | |
7ed4093a | 1019 | { |
7b02b4ed | 1020 | file_ptr text_end; |
7b02b4ed | 1021 | bfd_size_type text_size; |
ce07dd7c | 1022 | |
7ed4093a | 1023 | if (abfd->output_has_begun == false) |
ebd24135 ILT |
1024 | { |
1025 | if (NAME(aout,adjust_sizes_and_vmas) (abfd, | |
1026 | &text_size, | |
1027 | &text_end) == false) | |
1028 | return false; | |
9e2dad8e | 1029 | } |
12e7087f | 1030 | |
7ed4093a | 1031 | /* regardless, once we know what we're doing, we might as well get going */ |
c188b0be | 1032 | if (section != obj_bsssec(abfd)) |
7ed4093a SC |
1033 | { |
1034 | bfd_seek (abfd, section->filepos + offset, SEEK_SET); | |
c188b0be | 1035 | |
7ed4093a SC |
1036 | if (count) { |
1037 | return (bfd_write ((PTR)location, 1, count, abfd) == count) ? | |
1038 | true : false; | |
1039 | } | |
6db82ea7 | 1040 | return true; |
7ed4093a SC |
1041 | } |
1042 | return true; | |
1043 | } | |
1044 | \f | |
1045 | /* Classify stabs symbols */ | |
1046 | ||
1047 | #define sym_in_text_section(sym) \ | |
9e2dad8e | 1048 | (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT) |
7ed4093a SC |
1049 | |
1050 | #define sym_in_data_section(sym) \ | |
9e2dad8e | 1051 | (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA) |
7ed4093a SC |
1052 | |
1053 | #define sym_in_bss_section(sym) \ | |
9e2dad8e | 1054 | (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS) |
7ed4093a SC |
1055 | |
1056 | /* Symbol is undefined if type is N_UNDF|N_EXT and if it has | |
9e2dad8e JG |
1057 | zero in the "value" field. Nonzeroes there are fortrancommon |
1058 | symbols. */ | |
7ed4093a | 1059 | #define sym_is_undefined(sym) \ |
9e2dad8e | 1060 | ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0) |
7ed4093a SC |
1061 | |
1062 | /* Symbol is a global definition if N_EXT is on and if it has | |
9e2dad8e | 1063 | a nonzero type field. */ |
7ed4093a | 1064 | #define sym_is_global_defn(sym) \ |
9e2dad8e | 1065 | (((sym)->type & N_EXT) && (sym)->type & N_TYPE) |
7ed4093a SC |
1066 | |
1067 | /* Symbol is debugger info if any bits outside N_TYPE or N_EXT | |
9e2dad8e | 1068 | are on. */ |
7ed4093a | 1069 | #define sym_is_debugger_info(sym) \ |
c188b0be | 1070 | (((sym)->type & ~(N_EXT | N_TYPE)) || (sym)->type == N_FN) |
7ed4093a SC |
1071 | |
1072 | #define sym_is_fortrancommon(sym) \ | |
9e2dad8e | 1073 | (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0) |
7ed4093a SC |
1074 | |
1075 | /* Symbol is absolute if it has N_ABS set */ | |
1076 | #define sym_is_absolute(sym) \ | |
9e2dad8e | 1077 | (((sym)->type & N_TYPE)== N_ABS) |
7ed4093a SC |
1078 | |
1079 | ||
1080 | #define sym_is_indirect(sym) \ | |
9e2dad8e | 1081 | (((sym)->type & N_ABS)== N_ABS) |
7ed4093a SC |
1082 | |
1083 | /* Only in their own functions for ease of debugging; when sym flags have | |
9e2dad8e | 1084 | stabilised these should be inlined into their (single) caller */ |
c188b0be | 1085 | |
9783e04a | 1086 | static boolean |
ebd24135 | 1087 | DEFUN (translate_from_native_sym_flags, (sym_pointer, cache_ptr, abfd), |
a99c3d70 JG |
1088 | struct external_nlist *sym_pointer AND |
1089 | aout_symbol_type * cache_ptr AND | |
ebd24135 | 1090 | bfd * abfd) |
9e2dad8e | 1091 | { |
0f213cc2 | 1092 | cache_ptr->symbol.section = 0; |
ebd24135 | 1093 | switch (cache_ptr->type & N_TYPE) |
6db82ea7 | 1094 | { |
964affdc DM |
1095 | case N_SETA: case N_SETA | N_EXT: |
1096 | case N_SETT: case N_SETT | N_EXT: | |
1097 | case N_SETD: case N_SETD | N_EXT: | |
1098 | case N_SETB: case N_SETB | N_EXT: | |
ebd24135 ILT |
1099 | { |
1100 | char *copy = bfd_alloc (abfd, strlen (cache_ptr->symbol.name) + 1); | |
1101 | asection *section; | |
1102 | asection *into_section; | |
ebd24135 | 1103 | arelent_chain *reloc = (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain)); |
9783e04a DM |
1104 | |
1105 | if (!copy || !reloc) | |
1106 | { | |
68241b2b | 1107 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1108 | return false; |
1109 | } | |
1110 | ||
ebd24135 ILT |
1111 | strcpy (copy, cache_ptr->symbol.name); |
1112 | ||
1113 | /* Make sure that this bfd has a section with the right contructor | |
1114 | name */ | |
1115 | section = bfd_get_section_by_name (abfd, copy); | |
1116 | if (!section) | |
1117 | section = bfd_make_section (abfd, copy); | |
1118 | ||
1119 | /* Build a relocation entry for the constructor */ | |
1120 | switch ((cache_ptr->type & N_TYPE)) | |
a99c3d70 | 1121 | { |
964affdc | 1122 | case N_SETA: case N_SETA | N_EXT: |
ebd24135 ILT |
1123 | into_section = &bfd_abs_section; |
1124 | cache_ptr->type = N_ABS; | |
1125 | break; | |
964affdc | 1126 | case N_SETT: case N_SETT | N_EXT: |
ebd24135 ILT |
1127 | into_section = (asection *) obj_textsec (abfd); |
1128 | cache_ptr->type = N_TEXT; | |
1129 | break; | |
964affdc | 1130 | case N_SETD: case N_SETD | N_EXT: |
ebd24135 ILT |
1131 | into_section = (asection *) obj_datasec (abfd); |
1132 | cache_ptr->type = N_DATA; | |
1133 | break; | |
964affdc | 1134 | case N_SETB: case N_SETB | N_EXT: |
ebd24135 ILT |
1135 | into_section = (asection *) obj_bsssec (abfd); |
1136 | cache_ptr->type = N_BSS; | |
1137 | break; | |
1138 | default: | |
68241b2b | 1139 | bfd_set_error (bfd_error_bad_value); |
9783e04a | 1140 | return false; |
ebd24135 | 1141 | } |
88dfcd68 | 1142 | |
ebd24135 ILT |
1143 | /* Build a relocation pointing into the constuctor section |
1144 | pointing at the symbol in the set vector specified */ | |
6db82ea7 | 1145 | |
ebd24135 ILT |
1146 | reloc->relent.addend = cache_ptr->symbol.value; |
1147 | cache_ptr->symbol.section = into_section->symbol->section; | |
1148 | reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr; | |
6db82ea7 SC |
1149 | |
1150 | ||
ebd24135 ILT |
1151 | /* We modify the symbol to belong to a section depending upon the |
1152 | name of the symbol - probably __CTOR__ or __DTOR__ but we don't | |
1153 | really care, and add to the size of the section to contain a | |
1154 | pointer to the symbol. Build a reloc entry to relocate to this | |
1155 | symbol attached to this section. */ | |
a99c3d70 | 1156 | |
ebd24135 | 1157 | section->flags = SEC_CONSTRUCTOR; |
a99c3d70 JG |
1158 | |
1159 | ||
ebd24135 ILT |
1160 | section->reloc_count++; |
1161 | section->alignment_power = 2; | |
a99c3d70 | 1162 | |
ebd24135 ILT |
1163 | reloc->next = section->constructor_chain; |
1164 | section->constructor_chain = reloc; | |
1165 | reloc->relent.address = section->_raw_size; | |
1166 | section->_raw_size += sizeof (int *); | |
a99c3d70 | 1167 | |
ebd24135 ILT |
1168 | reloc->relent.howto |
1169 | = (obj_reloc_entry_size(abfd) == RELOC_EXT_SIZE | |
1170 | ? howto_table_ext : howto_table_std) | |
1171 | + CTOR_TABLE_RELOC_IDX; | |
1172 | cache_ptr->symbol.flags |= BSF_CONSTRUCTOR; | |
1173 | } | |
1174 | break; | |
1175 | default: | |
1176 | if (cache_ptr->type == N_WARNING) | |
1177 | { | |
1178 | /* This symbol is the text of a warning message, the next symbol | |
1179 | is the symbol to associate the warning with */ | |
1180 | cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING; | |
1181 | ||
1182 | /* @@ Stuffing pointers into integers is a no-no. | |
1183 | We can usually get away with it if the integer is | |
1184 | large enough though. */ | |
1185 | if (sizeof (cache_ptr + 1) > sizeof (bfd_vma)) | |
1186 | abort (); | |
1187 | cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1)); | |
1188 | ||
0c205af2 DM |
1189 | /* We don't use a warning symbol's section, but we need |
1190 | it to be nonzero for the sanity check below, so | |
1191 | pick one arbitrarily. */ | |
1192 | cache_ptr->symbol.section = &bfd_abs_section; | |
1193 | ||
ebd24135 ILT |
1194 | /* We furgle with the next symbol in place. |
1195 | We don't want it to be undefined, we'll trample the type */ | |
1196 | (sym_pointer + 1)->e_type[0] = 0xff; | |
a99c3d70 | 1197 | break; |
ebd24135 ILT |
1198 | } |
1199 | if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT)) | |
1200 | { | |
1201 | /* Two symbols in a row for an INDR message. The first symbol | |
1202 | contains the name we will match, the second symbol contains | |
1203 | the name the first name is translated into. It is supplied to | |
1204 | us undefined. This is good, since we want to pull in any files | |
1205 | which define it */ | |
1206 | cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT; | |
1207 | ||
1208 | /* @@ Stuffing pointers into integers is a no-no. | |
1209 | We can usually get away with it if the integer is | |
1210 | large enough though. */ | |
1211 | if (sizeof (cache_ptr + 1) > sizeof (bfd_vma)) | |
1212 | abort (); | |
1213 | ||
1214 | cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1)); | |
1215 | cache_ptr->symbol.section = &bfd_ind_section; | |
1216 | } | |
1217 | ||
1218 | else if (sym_is_debugger_info (cache_ptr)) | |
1219 | { | |
1220 | cache_ptr->symbol.flags = BSF_DEBUGGING; | |
1221 | /* Work out the section correct for this symbol */ | |
1222 | switch (cache_ptr->type & N_TYPE) | |
a99c3d70 | 1223 | { |
ebd24135 ILT |
1224 | case N_TEXT: |
1225 | case N_FN: | |
1226 | cache_ptr->symbol.section = obj_textsec (abfd); | |
1227 | cache_ptr->symbol.value -= obj_textsec (abfd)->vma; | |
a99c3d70 | 1228 | break; |
ebd24135 ILT |
1229 | case N_DATA: |
1230 | cache_ptr->symbol.value -= obj_datasec (abfd)->vma; | |
1231 | cache_ptr->symbol.section = obj_datasec (abfd); | |
1232 | break; | |
1233 | case N_BSS: | |
1234 | cache_ptr->symbol.section = obj_bsssec (abfd); | |
1235 | cache_ptr->symbol.value -= obj_bsssec (abfd)->vma; | |
1236 | break; | |
1237 | default: | |
1238 | case N_ABS: | |
ebd24135 ILT |
1239 | cache_ptr->symbol.section = &bfd_abs_section; |
1240 | break; | |
1241 | } | |
1242 | } | |
1243 | else | |
1244 | { | |
1245 | ||
1246 | if (sym_is_fortrancommon (cache_ptr)) | |
1247 | { | |
1248 | cache_ptr->symbol.flags = 0; | |
1249 | cache_ptr->symbol.section = &bfd_com_section; | |
1250 | } | |
1251 | else | |
1252 | { | |
1253 | ||
1254 | ||
a99c3d70 | 1255 | } |
ebd24135 ILT |
1256 | |
1257 | /* In a.out, the value of a symbol is always relative to the | |
1258 | * start of the file, if this is a data symbol we'll subtract | |
1259 | * the size of the text section to get the section relative | |
1260 | * value. If this is a bss symbol (which would be strange) | |
1261 | * we'll subtract the size of the previous two sections | |
1262 | * to find the section relative address. | |
1263 | */ | |
1264 | ||
1265 | if (sym_in_text_section (cache_ptr)) | |
a99c3d70 | 1266 | { |
ebd24135 ILT |
1267 | cache_ptr->symbol.value -= obj_textsec (abfd)->vma; |
1268 | cache_ptr->symbol.section = obj_textsec (abfd); | |
1269 | } | |
1270 | else if (sym_in_data_section (cache_ptr)) | |
1271 | { | |
1272 | cache_ptr->symbol.value -= obj_datasec (abfd)->vma; | |
1273 | cache_ptr->symbol.section = obj_datasec (abfd); | |
1274 | } | |
1275 | else if (sym_in_bss_section (cache_ptr)) | |
1276 | { | |
1277 | cache_ptr->symbol.section = obj_bsssec (abfd); | |
1278 | cache_ptr->symbol.value -= obj_bsssec (abfd)->vma; | |
1279 | } | |
1280 | else if (sym_is_undefined (cache_ptr)) | |
1281 | { | |
1282 | cache_ptr->symbol.flags = 0; | |
1283 | cache_ptr->symbol.section = &bfd_und_section; | |
1284 | } | |
1285 | else if (sym_is_absolute (cache_ptr)) | |
1286 | { | |
1287 | cache_ptr->symbol.section = &bfd_abs_section; | |
a99c3d70 JG |
1288 | } |
1289 | ||
ebd24135 | 1290 | if (sym_is_global_defn (cache_ptr)) |
a99c3d70 | 1291 | { |
ebd24135 | 1292 | cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT; |
a99c3d70 | 1293 | } |
0ee75d02 | 1294 | else if (! sym_is_undefined (cache_ptr)) |
a99c3d70 | 1295 | { |
ebd24135 | 1296 | cache_ptr->symbol.flags = BSF_LOCAL; |
a99c3d70 | 1297 | } |
7ed4093a | 1298 | } |
a99c3d70 | 1299 | } |
0f213cc2 KR |
1300 | if (cache_ptr->symbol.section == 0) |
1301 | abort (); | |
9783e04a | 1302 | return true; |
7ed4093a SC |
1303 | } |
1304 | ||
6db82ea7 | 1305 | |
4c3721d5 | 1306 | static boolean |
7ed4093a SC |
1307 | DEFUN(translate_to_native_sym_flags,(sym_pointer, cache_ptr, abfd), |
1308 | struct external_nlist *sym_pointer AND | |
1309 | asymbol *cache_ptr AND | |
1310 | bfd *abfd) | |
1311 | { | |
1312 | bfd_vma value = cache_ptr->value; | |
1313 | ||
10dea9ed DHW |
1314 | /* mask out any existing type bits in case copying from one section |
1315 | to another */ | |
1316 | sym_pointer->e_type[0] &= ~N_TYPE; | |
a99c3d70 | 1317 | |
3caa6924 DM |
1318 | /* We attempt to order these tests by decreasing frequency of success, |
1319 | according to tcov when linking the linker. */ | |
1320 | if (bfd_get_output_section(cache_ptr) == &bfd_abs_section) { | |
1321 | sym_pointer->e_type[0] |= N_ABS; | |
1322 | } | |
1323 | else if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) { | |
1324 | sym_pointer->e_type[0] |= N_TEXT; | |
a99c3d70 | 1325 | } |
6db82ea7 | 1326 | else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) { |
a99c3d70 JG |
1327 | sym_pointer->e_type[0] |= N_DATA; |
1328 | } | |
3caa6924 DM |
1329 | else if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) { |
1330 | sym_pointer->e_type[0] |= N_BSS; | |
7ed4093a | 1331 | } |
6f56c941 DM |
1332 | else if (bfd_get_output_section(cache_ptr) == &bfd_und_section) { |
1333 | sym_pointer->e_type[0] = (N_UNDF | N_EXT); | |
1334 | } | |
1335 | else if (bfd_get_output_section(cache_ptr) == &bfd_ind_section) { | |
1336 | sym_pointer->e_type[0] = N_INDR; | |
1337 | } | |
1338 | else if (bfd_get_output_section(cache_ptr) == NULL) { | |
1339 | /* Protect the bfd_is_com_section call. | |
1340 | This case occurs, e.g., for the *DEBUG* section of a COFF file. */ | |
68241b2b | 1341 | bfd_set_error (bfd_error_nonrepresentable_section); |
6f56c941 DM |
1342 | return false; |
1343 | } | |
a99c3d70 JG |
1344 | else if (bfd_is_com_section (bfd_get_output_section (cache_ptr))) { |
1345 | sym_pointer->e_type[0] = (N_UNDF | N_EXT); | |
c188b0be DM |
1346 | } |
1347 | else { | |
68241b2b | 1348 | bfd_set_error (bfd_error_nonrepresentable_section); |
4c3721d5 | 1349 | return false; |
a99c3d70 | 1350 | } |
6f56c941 | 1351 | |
6db82ea7 | 1352 | /* Turn the symbol from section relative to absolute again */ |
c188b0be | 1353 | |
6db82ea7 SC |
1354 | value += cache_ptr->section->output_section->vma + cache_ptr->section->output_offset ; |
1355 | ||
1356 | ||
1357 | if (cache_ptr->flags & (BSF_WARNING)) { | |
d7e34f67 | 1358 | sym_pointer->e_type[0] = N_WARNING; |
a99c3d70 | 1359 | (sym_pointer+1)->e_type[0] = 1; |
c188b0be DM |
1360 | } |
1361 | ||
6db82ea7 | 1362 | if (cache_ptr->flags & BSF_DEBUGGING) { |
34dd8ba3 JG |
1363 | sym_pointer->e_type[0] = ((aout_symbol_type *)cache_ptr)->type; |
1364 | } | |
3caa6924 DM |
1365 | else if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) { |
1366 | sym_pointer->e_type[0] |= N_EXT; | |
1367 | } | |
34dd8ba3 JG |
1368 | if (cache_ptr->flags & BSF_CONSTRUCTOR) { |
1369 | int type = ((aout_symbol_type *)cache_ptr)->type; | |
1370 | switch (type) | |
1371 | { | |
1372 | case N_ABS: type = N_SETA; break; | |
1373 | case N_TEXT: type = N_SETT; break; | |
1374 | case N_DATA: type = N_SETD; break; | |
1375 | case N_BSS: type = N_SETB; break; | |
1376 | } | |
1377 | sym_pointer->e_type[0] = type; | |
a99c3d70 | 1378 | } |
6db82ea7 | 1379 | |
7ed4093a | 1380 | PUT_WORD(abfd, value, sym_pointer->e_value); |
4c3721d5 ILT |
1381 | |
1382 | return true; | |
7ed4093a SC |
1383 | } |
1384 | \f | |
1385 | /* Native-level interface to symbols. */ | |
1386 | ||
7ed4093a SC |
1387 | |
1388 | asymbol * | |
1389 | DEFUN(NAME(aout,make_empty_symbol),(abfd), | |
1390 | bfd *abfd) | |
9e2dad8e JG |
1391 | { |
1392 | aout_symbol_type *new = | |
1393 | (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type)); | |
9783e04a DM |
1394 | if (!new) |
1395 | { | |
68241b2b | 1396 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1397 | return NULL; |
1398 | } | |
9e2dad8e | 1399 | new->symbol.the_bfd = abfd; |
fa2b89f1 | 1400 | |
9e2dad8e JG |
1401 | return &new->symbol; |
1402 | } | |
7ed4093a | 1403 | |
0ee75d02 ILT |
1404 | /* Translate a set of internal symbols into external symbols. */ |
1405 | ||
1406 | static boolean | |
1407 | translate_symbol_table (abfd, in, ext, count, str, strsize, dynamic) | |
1408 | bfd *abfd; | |
1409 | aout_symbol_type *in; | |
1410 | struct external_nlist *ext; | |
1411 | bfd_size_type count; | |
1412 | char *str; | |
1413 | bfd_size_type strsize; | |
1414 | boolean dynamic; | |
1415 | { | |
1416 | struct external_nlist *ext_end; | |
1417 | ||
1418 | ext_end = ext + count; | |
1419 | for (; ext < ext_end; ext++, in++) | |
1420 | { | |
1421 | bfd_vma x; | |
1422 | ||
1423 | x = GET_WORD (abfd, ext->e_strx); | |
1424 | in->symbol.the_bfd = abfd; | |
ca1c6bec ILT |
1425 | |
1426 | /* For the normal symbols, the zero index points at the number | |
1427 | of bytes in the string table but is to be interpreted as the | |
1428 | null string. For the dynamic symbols, the number of bytes in | |
1429 | the string table is stored in the __DYNAMIC structure and the | |
1430 | zero index points at an actual string. */ | |
1431 | if (x == 0 && ! dynamic) | |
1432 | in->symbol.name = ""; | |
1433 | else if (x < strsize) | |
0ee75d02 ILT |
1434 | in->symbol.name = str + x; |
1435 | else | |
1436 | return false; | |
1437 | ||
1438 | in->symbol.value = GET_SWORD (abfd, ext->e_value); | |
1439 | in->desc = bfd_h_get_16 (abfd, ext->e_desc); | |
1440 | in->other = bfd_h_get_8 (abfd, ext->e_other); | |
1441 | in->type = bfd_h_get_8 (abfd, ext->e_type); | |
1442 | in->symbol.udata = 0; | |
1443 | ||
9783e04a DM |
1444 | if (!translate_from_native_sym_flags (ext, in, abfd)) |
1445 | return false; | |
0ee75d02 ILT |
1446 | |
1447 | if (dynamic) | |
1448 | in->symbol.flags |= BSF_DYNAMIC; | |
1449 | } | |
1450 | ||
1451 | return true; | |
1452 | } | |
1453 | ||
1454 | /* We read the symbols into a buffer, which is discarded when this | |
1455 | function exits. We read the strings into a buffer large enough to | |
1456 | hold them all plus all the cached symbol entries. */ | |
1457 | ||
7ed4093a SC |
1458 | boolean |
1459 | DEFUN(NAME(aout,slurp_symbol_table),(abfd), | |
1460 | bfd *abfd) | |
9e2dad8e JG |
1461 | { |
1462 | bfd_size_type symbol_size; | |
1463 | bfd_size_type string_size; | |
1464 | unsigned char string_chars[BYTES_IN_WORD]; | |
1465 | struct external_nlist *syms; | |
1466 | char *strings; | |
1467 | aout_symbol_type *cached; | |
0ee75d02 ILT |
1468 | bfd_size_type dynsym_count = 0; |
1469 | struct external_nlist *dynsyms = NULL; | |
1470 | char *dynstrs = NULL; | |
1471 | bfd_size_type dynstr_size; | |
0f213cc2 | 1472 | |
9e2dad8e JG |
1473 | /* If there's no work to be done, don't do any */ |
1474 | if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true; | |
1475 | symbol_size = exec_hdr(abfd)->a_syms; | |
0f213cc2 KR |
1476 | if (symbol_size == 0) |
1477 | { | |
68241b2b | 1478 | bfd_set_error (bfd_error_no_symbols); |
0f213cc2 KR |
1479 | return false; |
1480 | } | |
1481 | ||
9e2dad8e JG |
1482 | bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET); |
1483 | if (bfd_read ((PTR)string_chars, BYTES_IN_WORD, 1, abfd) != BYTES_IN_WORD) | |
1484 | return false; | |
1485 | string_size = GET_WORD (abfd, string_chars); | |
0f213cc2 | 1486 | |
0ee75d02 ILT |
1487 | /* If this is a dynamic object, see if we can get the dynamic symbol |
1488 | table. */ | |
1489 | if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0 | |
1490 | && aout_backend_info (abfd)->read_dynamic_symbols) | |
1491 | { | |
1492 | dynsym_count = ((*aout_backend_info (abfd)->read_dynamic_symbols) | |
1493 | (abfd, &dynsyms, &dynstrs, &dynstr_size)); | |
1494 | if (dynsym_count == (bfd_size_type) -1) | |
1495 | return false; | |
1496 | } | |
9e2dad8e | 1497 | |
0ee75d02 ILT |
1498 | strings = (char *) bfd_alloc (abfd, string_size + 1); |
1499 | cached = ((aout_symbol_type *) | |
1500 | bfd_zalloc (abfd, | |
1501 | ((bfd_get_symcount (abfd) + dynsym_count) | |
1502 | * sizeof (aout_symbol_type)))); | |
1503 | ||
1504 | /* Don't allocate on the obstack, so we can free it easily. */ | |
9783e04a DM |
1505 | syms = (struct external_nlist *) malloc(symbol_size); |
1506 | if (!strings || !cached || !syms) | |
1507 | { | |
68241b2b | 1508 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1509 | return false; |
1510 | } | |
9e2dad8e | 1511 | bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET); |
0f213cc2 KR |
1512 | if (bfd_read ((PTR)syms, 1, symbol_size, abfd) != symbol_size) |
1513 | { | |
1514 | bailout: | |
1515 | if (syms) | |
1516 | free (syms); | |
1517 | if (cached) | |
1518 | bfd_release (abfd, cached); | |
1519 | if (strings) | |
1520 | bfd_release (abfd, strings); | |
1521 | return false; | |
1522 | } | |
1523 | ||
9e2dad8e | 1524 | bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET); |
0f213cc2 | 1525 | if (bfd_read ((PTR)strings, 1, string_size, abfd) != string_size) |
9e2dad8e | 1526 | { |
0f213cc2 | 1527 | goto bailout; |
9e2dad8e | 1528 | } |
0f213cc2 KR |
1529 | strings[string_size] = 0; /* Just in case. */ |
1530 | ||
1531 | /* OK, now walk the new symtable, cacheing symbol properties */ | |
0ee75d02 ILT |
1532 | if (! translate_symbol_table (abfd, cached, syms, bfd_get_symcount (abfd), |
1533 | strings, string_size, false)) | |
1534 | goto bailout; | |
1535 | if (dynsym_count > 0) | |
1536 | { | |
1537 | if (! translate_symbol_table (abfd, cached + bfd_get_symcount (abfd), | |
1538 | dynsyms, dynsym_count, dynstrs, | |
1539 | dynstr_size, true)) | |
1540 | goto bailout; | |
0f213cc2 | 1541 | |
0ee75d02 ILT |
1542 | bfd_get_symcount (abfd) += dynsym_count; |
1543 | } | |
0f213cc2 | 1544 | |
9e2dad8e JG |
1545 | obj_aout_symbols (abfd) = cached; |
1546 | free((PTR)syms); | |
0f213cc2 | 1547 | |
9e2dad8e JG |
1548 | return true; |
1549 | } | |
7ed4093a | 1550 | |
0f213cc2 KR |
1551 | \f |
1552 | /* Possible improvements: | |
1553 | + look for strings matching trailing substrings of other strings | |
1554 | + better data structures? balanced trees? | |
1555 | + smaller per-string or per-symbol data? re-use some of the symbol's | |
1556 | data fields? | |
1557 | + also look at reducing memory use elsewhere -- maybe if we didn't have to | |
1558 | construct the entire symbol table at once, we could get by with smaller | |
1559 | amounts of VM? (What effect does that have on the string table | |
1560 | reductions?) | |
1561 | + rip this out of here, put it into its own file in bfd or libiberty, so | |
1562 | coff and elf can use it too. I'll work on this soon, but have more | |
1563 | pressing tasks right now. | |
1564 | ||
1565 | A hash table might(?) be more efficient for handling exactly the cases that | |
1566 | are handled now, but for trailing substring matches, I think we want to | |
1567 | examine the `nearest' values (reverse-)lexically, not merely impose a strict | |
1568 | order, nor look only for exact-match or not-match. I don't think a hash | |
1569 | table would be very useful for that, and I don't feel like fleshing out two | |
1570 | completely different implementations. [raeburn:930419.0331EDT] */ | |
1571 | ||
0f213cc2 KR |
1572 | struct stringtab_entry { |
1573 | /* Hash value for this string. Only useful so long as we aren't doing | |
1574 | substring matches. */ | |
3caa6924 | 1575 | unsigned int hash; |
0f213cc2 KR |
1576 | |
1577 | /* Next node to look at, depending on whether the hash value of the string | |
1578 | being searched for is less than or greater than the hash value of the | |
1579 | current node. For now, `equal to' is lumped in with `greater than', for | |
1580 | space efficiency. It's not a common enough case to warrant another field | |
1581 | to be used for all nodes. */ | |
1582 | struct stringtab_entry *less; | |
1583 | struct stringtab_entry *greater; | |
1584 | ||
1585 | /* The string itself. */ | |
1586 | CONST char *string; | |
1587 | ||
1588 | /* The index allocated for this string. */ | |
1589 | bfd_size_type index; | |
1590 | ||
1591 | #ifdef GATHER_STATISTICS | |
1592 | /* How many references have there been to this string? (Not currently used; | |
1593 | could be dumped out for anaylsis, if anyone's interested.) */ | |
1594 | unsigned long count; | |
1595 | #endif | |
1596 | ||
1597 | /* Next node in linked list, in suggested output order. */ | |
1598 | struct stringtab_entry *next_to_output; | |
1599 | }; | |
1600 | ||
1601 | struct stringtab_data { | |
1602 | /* Tree of string table entries. */ | |
1603 | struct stringtab_entry *strings; | |
1604 | ||
1605 | /* Fudge factor used to center top node of tree. */ | |
1606 | int hash_zero; | |
1607 | ||
1608 | /* Next index value to issue. */ | |
1609 | bfd_size_type index; | |
1610 | ||
1611 | /* Index used for empty strings. Cached here because checking for them | |
1612 | is really easy, and we can avoid searching the tree. */ | |
1613 | bfd_size_type empty_string_index; | |
1614 | ||
1615 | /* These fields indicate the two ends of a singly-linked list that indicates | |
1616 | the order strings should be written out in. Use this order, and no | |
1617 | seeking will need to be done, so output efficiency should be maximized. */ | |
1618 | struct stringtab_entry **end; | |
1619 | struct stringtab_entry *output_order; | |
1620 | ||
1621 | #ifdef GATHER_STATISTICS | |
1622 | /* Number of strings which duplicate strings already in the table. */ | |
1623 | unsigned long duplicates; | |
1624 | ||
1625 | /* Number of bytes saved by not having to write all the duplicate strings. */ | |
1626 | unsigned long bytes_saved; | |
1627 | ||
1628 | /* Number of zero-length strings. Currently, these all turn into | |
1629 | references to the null byte at the end of the first string. In some | |
1630 | cases (possibly not all? explore this...), it should be possible to | |
1631 | simply write out a zero index value. */ | |
1632 | unsigned long empty_strings; | |
1633 | ||
1634 | /* Number of times the hash values matched but the strings were different. | |
1635 | Note that this includes the number of times the other string(s) occurs, so | |
1636 | there may only be two strings hashing to the same value, even if this | |
1637 | number is very large. */ | |
1638 | unsigned long bad_hash_matches; | |
1639 | ||
1640 | /* Null strings aren't counted in this one. | |
1641 | This will probably only be nonzero if we've got an input file | |
1642 | which was produced by `ld -r' (i.e., it's already been processed | |
1643 | through this code). Under some operating systems, native tools | |
1644 | may make all empty strings have the same index; but the pointer | |
1645 | check won't catch those, because to get to that stage we'd already | |
1646 | have to compute the checksum, which requires reading the string, | |
1647 | so we short-circuit that case with empty_string_index above. */ | |
1648 | unsigned long pointer_matches; | |
1649 | ||
1650 | /* Number of comparisons done. I figure with the algorithms in use below, | |
1651 | the average number of comparisons done (per symbol) should be roughly | |
1652 | log-base-2 of the number of unique strings. */ | |
1653 | unsigned long n_compares; | |
1654 | #endif | |
1655 | }; | |
1656 | ||
1657 | /* Some utility functions for the string table code. */ | |
1658 | ||
3caa6924 DM |
1659 | /* For speed, only hash on the first this many bytes of strings. |
1660 | This number was chosen by profiling ld linking itself, with -g. */ | |
1661 | #define HASHMAXLEN 25 | |
1662 | ||
1663 | #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c)) | |
1664 | ||
1665 | static INLINE unsigned int | |
1666 | hash (string, len) | |
1667 | unsigned char *string; | |
1668 | register unsigned int len; | |
0f213cc2 | 1669 | { |
3caa6924 DM |
1670 | register unsigned int sum = 0; |
1671 | ||
1672 | if (len > HASHMAXLEN) | |
0f213cc2 | 1673 | { |
3caa6924 DM |
1674 | HASH_CHAR (len); |
1675 | len = HASHMAXLEN; | |
1676 | } | |
1677 | ||
1678 | while (len--) | |
1679 | { | |
1680 | HASH_CHAR (*string++); | |
0f213cc2 KR |
1681 | } |
1682 | return sum; | |
1683 | } | |
1684 | ||
1685 | static INLINE void | |
1686 | stringtab_init (tab) | |
1687 | struct stringtab_data *tab; | |
1688 | { | |
1689 | tab->strings = 0; | |
1690 | tab->output_order = 0; | |
728472f1 | 1691 | tab->hash_zero = 0; |
0f213cc2 KR |
1692 | tab->end = &tab->output_order; |
1693 | ||
1694 | /* Initial string table length includes size of length field. */ | |
1695 | tab->index = BYTES_IN_WORD; | |
1696 | tab->empty_string_index = -1; | |
1697 | #ifdef GATHER_STATISTICS | |
1698 | tab->duplicates = 0; | |
1699 | tab->empty_strings = 0; | |
1700 | tab->bad_hash_matches = 0; | |
1701 | tab->pointer_matches = 0; | |
1702 | tab->bytes_saved = 0; | |
1703 | tab->n_compares = 0; | |
1704 | #endif | |
1705 | } | |
1706 | ||
1707 | static INLINE int | |
1708 | compare (entry, str, hash) | |
1709 | struct stringtab_entry *entry; | |
1710 | CONST char *str; | |
3caa6924 | 1711 | unsigned int hash; |
0f213cc2 | 1712 | { |
3caa6924 | 1713 | return hash - entry->hash; |
0f213cc2 KR |
1714 | } |
1715 | ||
1716 | #ifdef GATHER_STATISTICS | |
1717 | /* Don't want to have to link in math library with all bfd applications... */ | |
1718 | static INLINE double | |
1719 | log2 (num) | |
1720 | int num; | |
1721 | { | |
1722 | double d = num; | |
0f213cc2 KR |
1723 | int n = 0; |
1724 | while (d >= 2.0) | |
1725 | n++, d /= 2.0; | |
1726 | return ((d > 1.41) ? 0.5 : 0) + n; | |
0f213cc2 KR |
1727 | } |
1728 | #endif | |
1729 | ||
1730 | /* Main string table routines. */ | |
1731 | /* Returns index in string table. Whether or not this actually adds an | |
1732 | entry into the string table should be irrelevant -- it just has to | |
1733 | return a valid index. */ | |
1734 | static bfd_size_type | |
728472f1 | 1735 | add_to_stringtab (abfd, str, tab) |
0f213cc2 KR |
1736 | bfd *abfd; |
1737 | CONST char *str; | |
1738 | struct stringtab_data *tab; | |
0f213cc2 KR |
1739 | { |
1740 | struct stringtab_entry **ep; | |
3caa6924 DM |
1741 | register struct stringtab_entry *entry; |
1742 | unsigned int hashval, len; | |
0f213cc2 KR |
1743 | |
1744 | if (str[0] == 0) | |
1745 | { | |
1746 | bfd_size_type index; | |
1747 | CONST bfd_size_type minus_one = -1; | |
1748 | ||
1749 | #ifdef GATHER_STATISTICS | |
1750 | tab->empty_strings++; | |
1751 | #endif | |
1752 | index = tab->empty_string_index; | |
1753 | if (index != minus_one) | |
1754 | { | |
1755 | got_empty: | |
1756 | #ifdef GATHER_STATISTICS | |
1757 | tab->bytes_saved++; | |
1758 | tab->duplicates++; | |
1759 | #endif | |
1760 | return index; | |
1761 | } | |
1762 | ||
1763 | /* Need to find it. */ | |
1764 | entry = tab->strings; | |
1765 | if (entry) | |
1766 | { | |
1767 | index = entry->index + strlen (entry->string); | |
1768 | tab->empty_string_index = index; | |
1769 | goto got_empty; | |
1770 | } | |
1771 | len = 0; | |
1772 | } | |
1773 | else | |
1774 | len = strlen (str); | |
1775 | ||
1776 | /* The hash_zero value is chosen such that the first symbol gets a value of | |
1777 | zero. With a balanced tree, this wouldn't be very useful, but without it, | |
1778 | we might get a more even split at the top level, instead of skewing it | |
1779 | badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */ | |
3caa6924 | 1780 | hashval = hash (str, len) ^ tab->hash_zero; |
0f213cc2 KR |
1781 | ep = &tab->strings; |
1782 | if (!*ep) | |
1783 | { | |
1784 | tab->hash_zero = hashval; | |
1785 | hashval = 0; | |
1786 | goto add_it; | |
1787 | } | |
1788 | ||
1789 | while (*ep) | |
1790 | { | |
3caa6924 DM |
1791 | register int cmp; |
1792 | ||
0f213cc2 KR |
1793 | entry = *ep; |
1794 | #ifdef GATHER_STATISTICS | |
1795 | tab->n_compares++; | |
1796 | #endif | |
1797 | cmp = compare (entry, str, hashval); | |
3caa6924 DM |
1798 | /* The not-equal cases are more frequent, so check them first. */ |
1799 | if (cmp > 0) | |
1800 | ep = &entry->greater; | |
1801 | else if (cmp < 0) | |
1802 | ep = &entry->less; | |
1803 | else | |
0f213cc2 KR |
1804 | { |
1805 | if (entry->string == str) | |
1806 | { | |
1807 | #ifdef GATHER_STATISTICS | |
1808 | tab->pointer_matches++; | |
1809 | #endif | |
1810 | goto match; | |
1811 | } | |
3caa6924 DM |
1812 | /* Compare the first bytes to save a function call if they |
1813 | don't match. */ | |
1814 | if (entry->string[0] == str[0] && !strcmp (entry->string, str)) | |
0f213cc2 KR |
1815 | { |
1816 | match: | |
1817 | #ifdef GATHER_STATISTICS | |
1818 | entry->count++; | |
1819 | tab->bytes_saved += len + 1; | |
1820 | tab->duplicates++; | |
1821 | #endif | |
1822 | /* If we're in the linker, and the new string is from a new | |
1823 | input file which might have already had these reductions | |
1824 | run over it, we want to keep the new string pointer. I | |
1825 | don't think we're likely to see any (or nearly as many, | |
1826 | at least) cases where a later string is in the same location | |
1827 | as an earlier one rather than this one. */ | |
1828 | entry->string = str; | |
1829 | return entry->index; | |
1830 | } | |
1831 | #ifdef GATHER_STATISTICS | |
1832 | tab->bad_hash_matches++; | |
1833 | #endif | |
1834 | ep = &entry->greater; | |
1835 | } | |
0f213cc2 KR |
1836 | } |
1837 | ||
1838 | /* If we get here, nothing that's in the table already matched. | |
1839 | EP points to the `next' field at the end of the chain; stick a | |
1840 | new entry on here. */ | |
1841 | add_it: | |
3caa6924 DM |
1842 | entry = (struct stringtab_entry *) |
1843 | bfd_alloc_by_size_t (abfd, sizeof (struct stringtab_entry)); | |
9783e04a DM |
1844 | if (!entry) |
1845 | { | |
68241b2b | 1846 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1847 | abort(); /* FIXME */ |
1848 | } | |
0f213cc2 KR |
1849 | |
1850 | entry->less = entry->greater = 0; | |
1851 | entry->hash = hashval; | |
1852 | entry->index = tab->index; | |
1853 | entry->string = str; | |
1854 | entry->next_to_output = 0; | |
1855 | #ifdef GATHER_STATISTICS | |
1856 | entry->count = 1; | |
1857 | #endif | |
1858 | ||
1859 | assert (*tab->end == 0); | |
1860 | *(tab->end) = entry; | |
1861 | tab->end = &entry->next_to_output; | |
1862 | assert (*tab->end == 0); | |
1863 | ||
1864 | { | |
1865 | tab->index += len + 1; | |
1866 | if (len == 0) | |
1867 | tab->empty_string_index = entry->index; | |
1868 | } | |
1869 | assert (*ep == 0); | |
1870 | *ep = entry; | |
1871 | return entry->index; | |
1872 | } | |
1873 | ||
1874 | static void | |
1875 | emit_strtab (abfd, tab) | |
1876 | bfd *abfd; | |
1877 | struct stringtab_data *tab; | |
1878 | { | |
1879 | struct stringtab_entry *entry; | |
1880 | #ifdef GATHER_STATISTICS | |
1881 | int count = 0; | |
1882 | #endif | |
1883 | ||
1884 | /* Be sure to put string length into correct byte ordering before writing | |
1885 | it out. */ | |
1886 | char buffer[BYTES_IN_WORD]; | |
1887 | ||
1888 | PUT_WORD (abfd, tab->index, (unsigned char *) buffer); | |
1889 | bfd_write ((PTR) buffer, 1, BYTES_IN_WORD, abfd); | |
1890 | ||
1891 | for (entry = tab->output_order; entry; entry = entry->next_to_output) | |
1892 | { | |
1893 | bfd_write ((PTR) entry->string, 1, strlen (entry->string) + 1, abfd); | |
1894 | #ifdef GATHER_STATISTICS | |
1895 | count++; | |
1896 | #endif | |
1897 | } | |
1898 | ||
1899 | #ifdef GATHER_STATISTICS | |
1900 | /* Short form only, for now. | |
1901 | To do: Specify output file. Conditionalize on environment? Detailed | |
1902 | analysis if desired. */ | |
1903 | { | |
1904 | int n_syms = bfd_get_symcount (abfd); | |
1905 | ||
1906 | fprintf (stderr, "String table data for output file:\n"); | |
1907 | fprintf (stderr, " %8d symbols output\n", n_syms); | |
1908 | fprintf (stderr, " %8d duplicate strings\n", tab->duplicates); | |
1909 | fprintf (stderr, " %8d empty strings\n", tab->empty_strings); | |
1910 | fprintf (stderr, " %8d unique strings output\n", count); | |
1911 | fprintf (stderr, " %8d pointer matches\n", tab->pointer_matches); | |
1912 | fprintf (stderr, " %8d bytes saved\n", tab->bytes_saved); | |
1913 | fprintf (stderr, " %8d bad hash matches\n", tab->bad_hash_matches); | |
1914 | fprintf (stderr, " %8d hash-val comparisons\n", tab->n_compares); | |
1915 | if (n_syms) | |
1916 | { | |
1917 | double n_compares = tab->n_compares; | |
1918 | double avg_compares = n_compares / n_syms; | |
1919 | /* The second value here should usually be near one. */ | |
3caa6924 DM |
1920 | fprintf (stderr, |
1921 | "\t average %f comparisons per symbol (%f * log2 nstrings)\n", | |
0f213cc2 KR |
1922 | avg_compares, avg_compares / log2 (count)); |
1923 | } | |
1924 | } | |
1925 | #endif | |
1926 | ||
1927 | /* Old code: | |
1928 | unsigned int count; | |
1929 | generic = bfd_get_outsymbols(abfd); | |
1930 | for (count = 0; count < bfd_get_symcount(abfd); count++) | |
1931 | { | |
1932 | asymbol *g = *(generic++); | |
1933 | ||
1934 | if (g->name) | |
1935 | { | |
1936 | size_t length = strlen(g->name)+1; | |
1937 | bfd_write((PTR)g->name, 1, length, abfd); | |
1938 | } | |
1939 | g->KEEPIT = (KEEPITTYPE) count; | |
1940 | } */ | |
1941 | } | |
7ed4093a | 1942 | |
4c3721d5 | 1943 | boolean |
7ed4093a SC |
1944 | DEFUN(NAME(aout,write_syms),(abfd), |
1945 | bfd *abfd) | |
0f213cc2 KR |
1946 | { |
1947 | unsigned int count ; | |
1948 | asymbol **generic = bfd_get_outsymbols (abfd); | |
1949 | struct stringtab_data strtab; | |
1950 | ||
1951 | stringtab_init (&strtab); | |
1952 | ||
1953 | for (count = 0; count < bfd_get_symcount (abfd); count++) | |
1954 | { | |
7ed4093a SC |
1955 | asymbol *g = generic[count]; |
1956 | struct external_nlist nsp; | |
6db82ea7 | 1957 | |
0f213cc2 KR |
1958 | if (g->name) |
1959 | PUT_WORD (abfd, add_to_stringtab (abfd, g->name, &strtab), | |
1960 | (unsigned char *) nsp.e_strx); | |
1961 | else | |
1962 | PUT_WORD (abfd, 0, (unsigned char *)nsp.e_strx); | |
6db82ea7 | 1963 | |
0f213cc2 KR |
1964 | if (bfd_asymbol_flavour(g) == abfd->xvec->flavour) |
1965 | { | |
1966 | bfd_h_put_16(abfd, aout_symbol(g)->desc, nsp.e_desc); | |
1967 | bfd_h_put_8(abfd, aout_symbol(g)->other, nsp.e_other); | |
1968 | bfd_h_put_8(abfd, aout_symbol(g)->type, nsp.e_type); | |
1969 | } | |
7ed4093a | 1970 | else |
0f213cc2 KR |
1971 | { |
1972 | bfd_h_put_16(abfd,0, nsp.e_desc); | |
1973 | bfd_h_put_8(abfd, 0, nsp.e_other); | |
1974 | bfd_h_put_8(abfd, 0, nsp.e_type); | |
1975 | } | |
7b02b4ed | 1976 | |
4c3721d5 ILT |
1977 | if (! translate_to_native_sym_flags (&nsp, g, abfd)) |
1978 | return false; | |
7b02b4ed | 1979 | |
4c3721d5 ILT |
1980 | if (bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd) |
1981 | != EXTERNAL_NLIST_SIZE) | |
1982 | return false; | |
7ed4093a | 1983 | |
0f213cc2 KR |
1984 | /* NB: `KEEPIT' currently overlays `flags', so set this only |
1985 | here, at the end. */ | |
1986 | g->KEEPIT = count; | |
1987 | } | |
7ed4093a | 1988 | |
0f213cc2 | 1989 | emit_strtab (abfd, &strtab); |
4c3721d5 ILT |
1990 | |
1991 | return true; | |
0f213cc2 | 1992 | } |
7ed4093a | 1993 | |
0f213cc2 | 1994 | \f |
7ed4093a SC |
1995 | unsigned int |
1996 | DEFUN(NAME(aout,get_symtab),(abfd, location), | |
1997 | bfd *abfd AND | |
1998 | asymbol **location) | |
3f7607af | 1999 | { |
7ed4093a SC |
2000 | unsigned int counter = 0; |
2001 | aout_symbol_type *symbase; | |
ce07dd7c | 2002 | |
7ed4093a | 2003 | if (!NAME(aout,slurp_symbol_table)(abfd)) return 0; |
ce07dd7c | 2004 | |
7ed4093a SC |
2005 | for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);) |
2006 | *(location++) = (asymbol *)( symbase++); | |
2007 | *location++ =0; | |
ce07dd7c | 2008 | return bfd_get_symcount (abfd); |
3f7607af | 2009 | } |
7ed4093a SC |
2010 | |
2011 | \f | |
2012 | /* Standard reloc stuff */ | |
2013 | /* Output standard relocation information to a file in target byte order. */ | |
2014 | ||
2015 | void | |
2016 | DEFUN(NAME(aout,swap_std_reloc_out),(abfd, g, natptr), | |
2017 | bfd *abfd AND | |
2018 | arelent *g AND | |
2019 | struct reloc_std_external *natptr) | |
3f7607af | 2020 | { |
6db82ea7 SC |
2021 | int r_index; |
2022 | asymbol *sym = *(g->sym_ptr_ptr); | |
2023 | int r_extern; | |
2024 | unsigned int r_length; | |
2025 | int r_pcrel; | |
2026 | int r_baserel, r_jmptable, r_relative; | |
6db82ea7 | 2027 | asection *output_section = sym->section->output_section; |
ce07dd7c | 2028 | |
6db82ea7 | 2029 | PUT_WORD(abfd, g->address, natptr->r_address); |
ce07dd7c | 2030 | |
6db82ea7 SC |
2031 | r_length = g->howto->size ; /* Size as a power of two */ |
2032 | r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */ | |
c188b0be DM |
2033 | /* XXX This relies on relocs coming from a.out files. */ |
2034 | r_baserel = (g->howto->type & 8) != 0; | |
2035 | /* r_jmptable, r_relative??? FIXME-soon */ | |
6db82ea7 SC |
2036 | r_jmptable = 0; |
2037 | r_relative = 0; | |
c188b0be | 2038 | |
728472f1 ILT |
2039 | #if 0 |
2040 | /* For a standard reloc, the addend is in the object file. */ | |
6db82ea7 | 2041 | r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma; |
728472f1 | 2042 | #endif |
c188b0be | 2043 | |
6db82ea7 SC |
2044 | /* name was clobbered by aout_write_syms to be symbol index */ |
2045 | ||
c188b0be | 2046 | /* If this relocation is relative to a symbol then set the |
2768b3f7 SC |
2047 | r_index to the symbols index, and the r_extern bit. |
2048 | ||
2049 | Absolute symbols can come in in two ways, either as an offset | |
2050 | from the abs section, or as a symbol which has an abs value. | |
2051 | check for that here | |
2052 | */ | |
c188b0be | 2053 | |
2768b3f7 | 2054 | |
382f2a3d | 2055 | if (bfd_is_com_section (output_section) |
ce07dd7c | 2056 | || output_section == &bfd_abs_section |
c188b0be | 2057 | || output_section == &bfd_und_section) |
ce07dd7c | 2058 | { |
2768b3f7 SC |
2059 | if (bfd_abs_section.symbol == sym) |
2060 | { | |
2061 | /* Whoops, looked like an abs symbol, but is really an offset | |
2062 | from the abs section */ | |
2063 | r_index = 0; | |
2064 | r_extern = 0; | |
2065 | } | |
c188b0be | 2066 | else |
2768b3f7 SC |
2067 | { |
2068 | /* Fill in symbol */ | |
2069 | r_extern = 1; | |
2070 | r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT); | |
c188b0be | 2071 | |
2768b3f7 | 2072 | } |
ce07dd7c | 2073 | } |
c188b0be | 2074 | else |
ce07dd7c KR |
2075 | { |
2076 | /* Just an ordinary section */ | |
2077 | r_extern = 0; | |
c188b0be | 2078 | r_index = output_section->target_index; |
ce07dd7c KR |
2079 | } |
2080 | ||
6db82ea7 SC |
2081 | /* now the fun stuff */ |
2082 | if (abfd->xvec->header_byteorder_big_p != false) { | |
7ed4093a SC |
2083 | natptr->r_index[0] = r_index >> 16; |
2084 | natptr->r_index[1] = r_index >> 8; | |
2085 | natptr->r_index[2] = r_index; | |
2086 | natptr->r_type[0] = | |
6db82ea7 SC |
2087 | (r_extern? RELOC_STD_BITS_EXTERN_BIG: 0) |
2088 | | (r_pcrel? RELOC_STD_BITS_PCREL_BIG: 0) | |
2089 | | (r_baserel? RELOC_STD_BITS_BASEREL_BIG: 0) | |
2090 | | (r_jmptable? RELOC_STD_BITS_JMPTABLE_BIG: 0) | |
2091 | | (r_relative? RELOC_STD_BITS_RELATIVE_BIG: 0) | |
2092 | | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG); | |
7ed4093a | 2093 | } else { |
6db82ea7 SC |
2094 | natptr->r_index[2] = r_index >> 16; |
2095 | natptr->r_index[1] = r_index >> 8; | |
2096 | natptr->r_index[0] = r_index; | |
2097 | natptr->r_type[0] = | |
2098 | (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0) | |
7ed4093a | 2099 | | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0) |
6db82ea7 SC |
2100 | | (r_baserel? RELOC_STD_BITS_BASEREL_LITTLE: 0) |
2101 | | (r_jmptable? RELOC_STD_BITS_JMPTABLE_LITTLE: 0) | |
2102 | | (r_relative? RELOC_STD_BITS_RELATIVE_LITTLE: 0) | |
2103 | | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE); | |
2104 | } | |
3f7607af | 2105 | } |
7ed4093a SC |
2106 | |
2107 | ||
2108 | /* Extended stuff */ | |
2109 | /* Output extended relocation information to a file in target byte order. */ | |
2110 | ||
2111 | void | |
2112 | DEFUN(NAME(aout,swap_ext_reloc_out),(abfd, g, natptr), | |
2113 | bfd *abfd AND | |
2114 | arelent *g AND | |
2115 | register struct reloc_ext_external *natptr) | |
3f7607af | 2116 | { |
6db82ea7 SC |
2117 | int r_index; |
2118 | int r_extern; | |
2119 | unsigned int r_type; | |
2120 | unsigned int r_addend; | |
c188b0be | 2121 | asymbol *sym = *(g->sym_ptr_ptr); |
6db82ea7 | 2122 | asection *output_section = sym->section->output_section; |
c188b0be | 2123 | |
6db82ea7 | 2124 | PUT_WORD (abfd, g->address, natptr->r_address); |
c188b0be | 2125 | |
6db82ea7 | 2126 | r_type = (unsigned int) g->howto->type; |
7ed4093a | 2127 | |
c188b0be | 2128 | r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma; |
7ed4093a | 2129 | |
c188b0be | 2130 | /* If this relocation is relative to a symbol then set the |
2768b3f7 SC |
2131 | r_index to the symbols index, and the r_extern bit. |
2132 | ||
2133 | Absolute symbols can come in in two ways, either as an offset | |
2134 | from the abs section, or as a symbol which has an abs value. | |
c188b0be DM |
2135 | check for that here. */ |
2136 | ||
382f2a3d | 2137 | if (bfd_is_com_section (output_section) |
2768b3f7 | 2138 | || output_section == &bfd_abs_section |
0f213cc2 | 2139 | || output_section == &bfd_und_section) |
6db82ea7 | 2140 | { |
2768b3f7 SC |
2141 | if (bfd_abs_section.symbol == sym) |
2142 | { | |
2143 | /* Whoops, looked like an abs symbol, but is really an offset | |
2144 | from the abs section */ | |
2145 | r_index = 0; | |
2146 | r_extern = 0; | |
2147 | } | |
c188b0be | 2148 | else |
2768b3f7 SC |
2149 | { |
2150 | r_extern = 1; | |
2151 | r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT); | |
2152 | } | |
6db82ea7 | 2153 | } |
c188b0be | 2154 | else |
6db82ea7 SC |
2155 | { |
2156 | /* Just an ordinary section */ | |
2157 | r_extern = 0; | |
c188b0be | 2158 | r_index = output_section->target_index; |
6db82ea7 | 2159 | } |
c188b0be | 2160 | |
7ed4093a SC |
2161 | /* now the fun stuff */ |
2162 | if (abfd->xvec->header_byteorder_big_p != false) { | |
2768b3f7 SC |
2163 | natptr->r_index[0] = r_index >> 16; |
2164 | natptr->r_index[1] = r_index >> 8; | |
2165 | natptr->r_index[2] = r_index; | |
2166 | natptr->r_type[0] = | |
c188b0be DM |
2167 | ((r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0) |
2168 | | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG)); | |
2768b3f7 SC |
2169 | } else { |
2170 | natptr->r_index[2] = r_index >> 16; | |
2171 | natptr->r_index[1] = r_index >> 8; | |
2172 | natptr->r_index[0] = r_index; | |
2173 | natptr->r_type[0] = | |
2174 | (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0) | |
2175 | | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE); | |
2176 | } | |
7ed4093a SC |
2177 | |
2178 | PUT_WORD (abfd, r_addend, natptr->r_addend); | |
2179 | } | |
2180 | ||
6db82ea7 SC |
2181 | /* BFD deals internally with all things based from the section they're |
2182 | in. so, something in 10 bytes into a text section with a base of | |
c188b0be | 2183 | 50 would have a symbol (.text+10) and know .text vma was 50. |
6db82ea7 SC |
2184 | |
2185 | Aout keeps all it's symbols based from zero, so the symbol would | |
2186 | contain 60. This macro subs the base of each section from the value | |
2187 | to give the true offset from the section */ | |
2188 | ||
2189 | ||
7ed4093a SC |
2190 | #define MOVE_ADDRESS(ad) \ |
2191 | if (r_extern) { \ | |
6db82ea7 SC |
2192 | /* undefined symbol */ \ |
2193 | cache_ptr->sym_ptr_ptr = symbols + r_index; \ | |
2194 | cache_ptr->addend = ad; \ | |
2195 | } else { \ | |
2196 | /* defined, section relative. replace symbol with pointer to \ | |
2197 | symbol which points to section */ \ | |
7ed4093a SC |
2198 | switch (r_index) { \ |
2199 | case N_TEXT: \ | |
2200 | case N_TEXT | N_EXT: \ | |
6db82ea7 | 2201 | cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \ |
7ed4093a SC |
2202 | cache_ptr->addend = ad - su->textsec->vma; \ |
2203 | break; \ | |
2204 | case N_DATA: \ | |
2205 | case N_DATA | N_EXT: \ | |
6db82ea7 | 2206 | cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \ |
7ed4093a SC |
2207 | cache_ptr->addend = ad - su->datasec->vma; \ |
2208 | break; \ | |
2209 | case N_BSS: \ | |
2210 | case N_BSS | N_EXT: \ | |
6db82ea7 | 2211 | cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \ |
7ed4093a SC |
2212 | cache_ptr->addend = ad - su->bsssec->vma; \ |
2213 | break; \ | |
6db82ea7 | 2214 | default: \ |
7ed4093a SC |
2215 | case N_ABS: \ |
2216 | case N_ABS | N_EXT: \ | |
6db82ea7 SC |
2217 | cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \ |
2218 | cache_ptr->addend = ad; \ | |
7ed4093a SC |
2219 | break; \ |
2220 | } \ | |
2221 | } \ | |
2222 | ||
2223 | void | |
2224 | DEFUN(NAME(aout,swap_ext_reloc_in), (abfd, bytes, cache_ptr, symbols), | |
2225 | bfd *abfd AND | |
2226 | struct reloc_ext_external *bytes AND | |
2227 | arelent *cache_ptr AND | |
2228 | asymbol **symbols) | |
2229 | { | |
2230 | int r_index; | |
2231 | int r_extern; | |
2232 | unsigned int r_type; | |
6db82ea7 | 2233 | struct aoutdata *su = &(abfd->tdata.aout_data->a); |
7ed4093a SC |
2234 | |
2235 | cache_ptr->address = (GET_SWORD (abfd, bytes->r_address)); | |
2236 | ||
2237 | /* now the fun stuff */ | |
2238 | if (abfd->xvec->header_byteorder_big_p != false) { | |
382f2a3d ILT |
2239 | r_index = (bytes->r_index[0] << 16) |
2240 | | (bytes->r_index[1] << 8) | |
2241 | | bytes->r_index[2]; | |
7ed4093a SC |
2242 | r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG)); |
2243 | r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG) | |
2244 | >> RELOC_EXT_BITS_TYPE_SH_BIG; | |
2245 | } else { | |
382f2a3d ILT |
2246 | r_index = (bytes->r_index[2] << 16) |
2247 | | (bytes->r_index[1] << 8) | |
2248 | | bytes->r_index[0]; | |
7ed4093a SC |
2249 | r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE)); |
2250 | r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE) | |
2251 | >> RELOC_EXT_BITS_TYPE_SH_LITTLE; | |
2252 | } | |
2253 | ||
2254 | cache_ptr->howto = howto_table_ext + r_type; | |
6db82ea7 | 2255 | MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend)); |
7ed4093a SC |
2256 | } |
2257 | ||
2258 | void | |
2259 | DEFUN(NAME(aout,swap_std_reloc_in), (abfd, bytes, cache_ptr, symbols), | |
2260 | bfd *abfd AND | |
2261 | struct reloc_std_external *bytes AND | |
2262 | arelent *cache_ptr AND | |
2263 | asymbol **symbols) | |
2264 | { | |
2265 | int r_index; | |
2266 | int r_extern; | |
2267 | unsigned int r_length; | |
2268 | int r_pcrel; | |
2269 | int r_baserel, r_jmptable, r_relative; | |
6db82ea7 | 2270 | struct aoutdata *su = &(abfd->tdata.aout_data->a); |
c188b0be | 2271 | int howto_idx; |
7ed4093a | 2272 | |
34dd8ba3 | 2273 | cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address); |
7ed4093a SC |
2274 | |
2275 | /* now the fun stuff */ | |
2276 | if (abfd->xvec->header_byteorder_big_p != false) { | |
382f2a3d ILT |
2277 | r_index = (bytes->r_index[0] << 16) |
2278 | | (bytes->r_index[1] << 8) | |
2279 | | bytes->r_index[2]; | |
7ed4093a SC |
2280 | r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG)); |
2281 | r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG)); | |
2282 | r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG)); | |
2283 | r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG)); | |
2284 | r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG)); | |
c188b0be | 2285 | r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG) |
7ed4093a SC |
2286 | >> RELOC_STD_BITS_LENGTH_SH_BIG; |
2287 | } else { | |
382f2a3d ILT |
2288 | r_index = (bytes->r_index[2] << 16) |
2289 | | (bytes->r_index[1] << 8) | |
2290 | | bytes->r_index[0]; | |
7ed4093a SC |
2291 | r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE)); |
2292 | r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE)); | |
2293 | r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE)); | |
2294 | r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE)); | |
2295 | r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE)); | |
c188b0be | 2296 | r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE) |
7ed4093a SC |
2297 | >> RELOC_STD_BITS_LENGTH_SH_LITTLE; |
2298 | } | |
2299 | ||
c188b0be DM |
2300 | howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel; |
2301 | BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std)); | |
2302 | cache_ptr->howto = howto_table_std + howto_idx; | |
2303 | BFD_ASSERT (cache_ptr->howto->type != -1); | |
2304 | BFD_ASSERT (r_jmptable == 0); | |
2305 | BFD_ASSERT (r_relative == 0); | |
2306 | /* FIXME-soon: Roll jmptable, relative bits into howto setting */ | |
7ed4093a SC |
2307 | |
2308 | MOVE_ADDRESS(0); | |
2309 | } | |
2310 | ||
2311 | /* Reloc hackery */ | |
2312 | ||
2313 | boolean | |
2314 | DEFUN(NAME(aout,slurp_reloc_table),(abfd, asect, symbols), | |
2315 | bfd *abfd AND | |
2316 | sec_ptr asect AND | |
2317 | asymbol **symbols) | |
2318 | { | |
2319 | unsigned int count; | |
2320 | bfd_size_type reloc_size; | |
2321 | PTR relocs; | |
0ee75d02 ILT |
2322 | bfd_size_type dynrel_count = 0; |
2323 | PTR dynrels = NULL; | |
7ed4093a SC |
2324 | arelent *reloc_cache; |
2325 | size_t each_size; | |
0ee75d02 ILT |
2326 | unsigned int counter = 0; |
2327 | arelent *cache_ptr; | |
7ed4093a SC |
2328 | |
2329 | if (asect->relocation) return true; | |
2330 | ||
2331 | if (asect->flags & SEC_CONSTRUCTOR) return true; | |
2332 | ||
0ee75d02 | 2333 | if (asect == obj_datasec (abfd)) |
7ed4093a | 2334 | reloc_size = exec_hdr(abfd)->a_drsize; |
0ee75d02 | 2335 | else if (asect == obj_textsec (abfd)) |
7ed4093a | 2336 | reloc_size = exec_hdr(abfd)->a_trsize; |
0ee75d02 ILT |
2337 | else |
2338 | { | |
68241b2b | 2339 | bfd_set_error (bfd_error_invalid_operation); |
0ee75d02 ILT |
2340 | return false; |
2341 | } | |
2342 | ||
2343 | if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0 | |
2344 | && aout_backend_info (abfd)->read_dynamic_relocs) | |
2345 | { | |
2346 | dynrel_count = ((*aout_backend_info (abfd)->read_dynamic_relocs) | |
2347 | (abfd, &dynrels)); | |
2348 | if (dynrel_count == (bfd_size_type) -1) | |
2349 | return false; | |
2350 | } | |
7ed4093a | 2351 | |
7ed4093a SC |
2352 | bfd_seek (abfd, asect->rel_filepos, SEEK_SET); |
2353 | each_size = obj_reloc_entry_size (abfd); | |
2354 | ||
2355 | count = reloc_size / each_size; | |
2356 | ||
0ee75d02 ILT |
2357 | reloc_cache = ((arelent *) |
2358 | bfd_zalloc (abfd, | |
2359 | (size_t) ((count + dynrel_count) | |
2360 | * sizeof (arelent)))); | |
2361 | if (!reloc_cache) | |
2362 | { | |
2363 | nomem: | |
68241b2b | 2364 | bfd_set_error (bfd_error_no_memory); |
0ee75d02 ILT |
2365 | return false; |
2366 | } | |
7ed4093a SC |
2367 | |
2368 | relocs = (PTR) bfd_alloc (abfd, reloc_size); | |
0ee75d02 ILT |
2369 | if (!relocs) |
2370 | { | |
2371 | bfd_release (abfd, reloc_cache); | |
2372 | goto nomem; | |
2373 | } | |
7ed4093a | 2374 | |
0ee75d02 ILT |
2375 | if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size) |
2376 | { | |
2377 | bfd_release (abfd, relocs); | |
2378 | bfd_release (abfd, reloc_cache); | |
68241b2b | 2379 | bfd_set_error (bfd_error_system_call); |
0ee75d02 ILT |
2380 | return false; |
2381 | } | |
7ed4093a | 2382 | |
0ee75d02 ILT |
2383 | cache_ptr = reloc_cache; |
2384 | if (each_size == RELOC_EXT_SIZE) | |
2385 | { | |
2386 | register struct reloc_ext_external *rptr = | |
2387 | (struct reloc_ext_external *) relocs; | |
7ed4093a | 2388 | |
0ee75d02 ILT |
2389 | for (; counter < count; counter++, rptr++, cache_ptr++) |
2390 | NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, symbols); | |
7ed4093a | 2391 | } |
0ee75d02 ILT |
2392 | else |
2393 | { | |
2394 | register struct reloc_std_external *rptr | |
2395 | = (struct reloc_std_external *) relocs; | |
7ed4093a | 2396 | |
0ee75d02 ILT |
2397 | for (; counter < count; counter++, rptr++, cache_ptr++) |
2398 | NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols); | |
7ed4093a SC |
2399 | } |
2400 | ||
0ee75d02 ILT |
2401 | if (dynrel_count > 0) |
2402 | { | |
2403 | asymbol **dynsyms; | |
2404 | ||
2405 | /* The dynamic symbols are at the end of the symbol table. */ | |
2406 | for (dynsyms = symbols; | |
2407 | *dynsyms != NULL && ((*dynsyms)->flags & BSF_DYNAMIC) == 0; | |
2408 | ++dynsyms) | |
2409 | ; | |
2410 | ||
2411 | /* Swap in the dynamic relocs. These relocs may be for either | |
2412 | section, so we must discard ones we don't want. */ | |
2413 | counter = 0; | |
2414 | if (each_size == RELOC_EXT_SIZE) | |
2415 | { | |
2416 | register struct reloc_ext_external *rptr | |
2417 | = (struct reloc_ext_external *) dynrels; | |
2418 | ||
2419 | for (; counter < dynrel_count; counter++, rptr++, cache_ptr++) | |
2420 | { | |
2421 | NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, dynsyms); | |
2422 | cache_ptr->address -= bfd_get_section_vma (abfd, asect); | |
2423 | if (cache_ptr->address >= bfd_section_size (abfd, asect)) | |
2424 | --cache_ptr; | |
2425 | } | |
2426 | } | |
2427 | else | |
2428 | { | |
2429 | register struct reloc_std_external *rptr | |
2430 | = (struct reloc_std_external *) dynrels; | |
2431 | ||
2432 | for (; counter < dynrel_count; counter++, rptr++, cache_ptr++) | |
2433 | { | |
2434 | NAME(aout,swap_std_reloc_in) (abfd, rptr, cache_ptr, dynsyms); | |
2435 | cache_ptr->address -= bfd_get_section_vma (abfd, asect); | |
2436 | if (cache_ptr->address >= bfd_section_size (abfd, asect)) | |
2437 | --cache_ptr; | |
2438 | } | |
2439 | } | |
2440 | } | |
7ed4093a SC |
2441 | |
2442 | bfd_release (abfd,relocs); | |
2443 | asect->relocation = reloc_cache; | |
0ee75d02 | 2444 | asect->reloc_count = cache_ptr - reloc_cache; |
7ed4093a SC |
2445 | return true; |
2446 | } | |
2447 | ||
2448 | ||
2449 | ||
2450 | /* Write out a relocation section into an object file. */ | |
2451 | ||
2452 | boolean | |
2453 | DEFUN(NAME(aout,squirt_out_relocs),(abfd, section), | |
2454 | bfd *abfd AND | |
2455 | asection *section) | |
2456 | { | |
2457 | arelent **generic; | |
2458 | unsigned char *native, *natptr; | |
2459 | size_t each_size; | |
2460 | ||
2461 | unsigned int count = section->reloc_count; | |
2462 | size_t natsize; | |
2463 | ||
2464 | if (count == 0) return true; | |
2465 | ||
2466 | each_size = obj_reloc_entry_size (abfd); | |
2467 | natsize = each_size * count; | |
2468 | native = (unsigned char *) bfd_zalloc (abfd, natsize); | |
2469 | if (!native) { | |
68241b2b | 2470 | bfd_set_error (bfd_error_no_memory); |
7ed4093a SC |
2471 | return false; |
2472 | } | |
2473 | ||
2474 | generic = section->orelocation; | |
2475 | ||
c188b0be | 2476 | if (each_size == RELOC_EXT_SIZE) |
7ed4093a SC |
2477 | { |
2478 | for (natptr = native; | |
2479 | count != 0; | |
2480 | --count, natptr += each_size, ++generic) | |
2481 | NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr); | |
2482 | } | |
c188b0be | 2483 | else |
7ed4093a SC |
2484 | { |
2485 | for (natptr = native; | |
2486 | count != 0; | |
2487 | --count, natptr += each_size, ++generic) | |
2488 | NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr); | |
2489 | } | |
2490 | ||
2491 | if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) { | |
2492 | bfd_release(abfd, native); | |
2493 | return false; | |
2494 | } | |
2495 | bfd_release (abfd, native); | |
2496 | ||
2497 | return true; | |
2498 | } | |
2499 | ||
2500 | /* This is stupid. This function should be a boolean predicate */ | |
2501 | unsigned int | |
2502 | DEFUN(NAME(aout,canonicalize_reloc),(abfd, section, relptr, symbols), | |
2503 | bfd *abfd AND | |
2504 | sec_ptr section AND | |
2505 | arelent **relptr AND | |
2506 | asymbol **symbols) | |
2507 | { | |
2508 | arelent *tblptr = section->relocation; | |
2509 | unsigned int count; | |
2510 | ||
2511 | if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols))) | |
2512 | return 0; | |
2513 | ||
2514 | if (section->flags & SEC_CONSTRUCTOR) { | |
2515 | arelent_chain *chain = section->constructor_chain; | |
2516 | for (count = 0; count < section->reloc_count; count ++) { | |
2517 | *relptr ++ = &chain->relent; | |
2518 | chain = chain->next; | |
2519 | } | |
2520 | } | |
2521 | else { | |
2522 | tblptr = section->relocation; | |
2523 | if (!tblptr) return 0; | |
2524 | ||
c188b0be | 2525 | for (count = 0; count++ < section->reloc_count;) |
7ed4093a SC |
2526 | { |
2527 | *relptr++ = tblptr++; | |
2528 | } | |
2529 | } | |
2530 | *relptr = 0; | |
2531 | ||
2532 | return section->reloc_count; | |
2533 | } | |
2534 | ||
2535 | unsigned int | |
2536 | DEFUN(NAME(aout,get_reloc_upper_bound),(abfd, asect), | |
2537 | bfd *abfd AND | |
2538 | sec_ptr asect) | |
2539 | { | |
0ee75d02 ILT |
2540 | bfd_size_type dynrel_count = 0; |
2541 | ||
7ed4093a | 2542 | if (bfd_get_format (abfd) != bfd_object) { |
68241b2b | 2543 | bfd_set_error (bfd_error_invalid_operation); |
7ed4093a SC |
2544 | return 0; |
2545 | } | |
2546 | if (asect->flags & SEC_CONSTRUCTOR) { | |
2547 | return (sizeof (arelent *) * (asect->reloc_count+1)); | |
2548 | } | |
2549 | ||
0ee75d02 ILT |
2550 | if ((bfd_get_file_flags (abfd) & DYNAMIC) != 0 |
2551 | && aout_backend_info (abfd)->read_dynamic_relocs) | |
2552 | { | |
2553 | PTR dynrels; | |
2554 | ||
2555 | dynrel_count = ((*aout_backend_info (abfd)->read_dynamic_relocs) | |
2556 | (abfd, &dynrels)); | |
2557 | if (dynrel_count == (bfd_size_type) -1) | |
2558 | return 0; | |
2559 | } | |
7ed4093a SC |
2560 | |
2561 | if (asect == obj_datasec (abfd)) | |
2562 | return (sizeof (arelent *) * | |
0ee75d02 ILT |
2563 | ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd)) |
2564 | + dynrel_count + 1)); | |
7ed4093a SC |
2565 | |
2566 | if (asect == obj_textsec (abfd)) | |
2567 | return (sizeof (arelent *) * | |
0ee75d02 ILT |
2568 | ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd)) |
2569 | + dynrel_count + 1)); | |
7ed4093a | 2570 | |
68241b2b | 2571 | bfd_set_error (bfd_error_invalid_operation); |
7ed4093a SC |
2572 | return 0; |
2573 | } | |
2574 | ||
2575 | \f | |
2576 | unsigned int | |
2577 | DEFUN(NAME(aout,get_symtab_upper_bound),(abfd), | |
2578 | bfd *abfd) | |
2579 | { | |
2580 | if (!NAME(aout,slurp_symbol_table)(abfd)) return 0; | |
2581 | ||
2582 | return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *)); | |
2583 | } | |
728472f1 ILT |
2584 | |
2585 | /*ARGSUSED*/ | |
7ed4093a SC |
2586 | alent * |
2587 | DEFUN(NAME(aout,get_lineno),(ignore_abfd, ignore_symbol), | |
2588 | bfd *ignore_abfd AND | |
2589 | asymbol *ignore_symbol) | |
2590 | { | |
2591 | return (alent *)NULL; | |
2592 | } | |
2593 | ||
728472f1 | 2594 | /*ARGSUSED*/ |
c188b0be | 2595 | void |
34dd8ba3 JG |
2596 | DEFUN(NAME(aout,get_symbol_info),(ignore_abfd, symbol, ret), |
2597 | bfd *ignore_abfd AND | |
2598 | asymbol *symbol AND | |
2599 | symbol_info *ret) | |
2600 | { | |
2601 | bfd_symbol_info (symbol, ret); | |
2602 | ||
2603 | if (ret->type == '?') | |
2604 | { | |
2605 | int type_code = aout_symbol(symbol)->type & 0xff; | |
2606 | CONST char *stab_name = aout_stab_name(type_code); | |
2607 | static char buf[10]; | |
2608 | ||
2609 | if (stab_name == NULL) | |
2610 | { | |
2611 | sprintf(buf, "(%d)", type_code); | |
2612 | stab_name = buf; | |
2613 | } | |
2614 | ret->type = '-'; | |
2615 | ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff); | |
2616 | ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff); | |
2617 | ret->stab_name = stab_name; | |
2618 | } | |
2619 | } | |
7ed4093a | 2620 | |
728472f1 | 2621 | /*ARGSUSED*/ |
c188b0be | 2622 | void |
7ed4093a SC |
2623 | DEFUN(NAME(aout,print_symbol),(ignore_abfd, afile, symbol, how), |
2624 | bfd *ignore_abfd AND | |
2625 | PTR afile AND | |
2626 | asymbol *symbol AND | |
9e2dad8e | 2627 | bfd_print_symbol_type how) |
7ed4093a SC |
2628 | { |
2629 | FILE *file = (FILE *)afile; | |
2630 | ||
2631 | switch (how) { | |
9e2dad8e | 2632 | case bfd_print_symbol_name: |
fb3be09b JG |
2633 | if (symbol->name) |
2634 | fprintf(file,"%s", symbol->name); | |
7ed4093a | 2635 | break; |
9e2dad8e | 2636 | case bfd_print_symbol_more: |
7ed4093a SC |
2637 | fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff), |
2638 | (unsigned)(aout_symbol(symbol)->other & 0xff), | |
2639 | (unsigned)(aout_symbol(symbol)->type)); | |
2640 | break; | |
9e2dad8e | 2641 | case bfd_print_symbol_all: |
7ed4093a | 2642 | { |
6db82ea7 SC |
2643 | CONST char *section_name = symbol->section->name; |
2644 | ||
7ed4093a SC |
2645 | |
2646 | bfd_print_symbol_vandf((PTR)file,symbol); | |
2647 | ||
fb3be09b | 2648 | fprintf(file," %-5s %04x %02x %02x", |
7ed4093a SC |
2649 | section_name, |
2650 | (unsigned)(aout_symbol(symbol)->desc & 0xffff), | |
2651 | (unsigned)(aout_symbol(symbol)->other & 0xff), | |
9e2dad8e | 2652 | (unsigned)(aout_symbol(symbol)->type & 0xff)); |
fb3be09b JG |
2653 | if (symbol->name) |
2654 | fprintf(file," %s", symbol->name); | |
7ed4093a SC |
2655 | } |
2656 | break; | |
2657 | } | |
2658 | } | |
2659 | ||
c188b0be | 2660 | /* |
6724ff46 | 2661 | provided a BFD, a section and an offset into the section, calculate |
7ed4093a SC |
2662 | and return the name of the source file and the line nearest to the |
2663 | wanted location. | |
2664 | */ | |
c188b0be | 2665 | |
7ed4093a SC |
2666 | boolean |
2667 | DEFUN(NAME(aout,find_nearest_line),(abfd, | |
2668 | section, | |
2669 | symbols, | |
2670 | offset, | |
2671 | filename_ptr, | |
2672 | functionname_ptr, | |
2673 | line_ptr), | |
2674 | bfd *abfd AND | |
2675 | asection *section AND | |
2676 | asymbol **symbols AND | |
2677 | bfd_vma offset AND | |
2678 | CONST char **filename_ptr AND | |
2679 | CONST char **functionname_ptr AND | |
2680 | unsigned int *line_ptr) | |
2681 | { | |
2682 | /* Run down the file looking for the filename, function and linenumber */ | |
2683 | asymbol **p; | |
2684 | static char buffer[100]; | |
98d43107 | 2685 | static char filename_buffer[200]; |
6db82ea7 SC |
2686 | CONST char *directory_name = NULL; |
2687 | CONST char *main_file_name = NULL; | |
2688 | CONST char *current_file_name = NULL; | |
2689 | CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */ | |
7ed4093a SC |
2690 | bfd_vma high_line_vma = ~0; |
2691 | bfd_vma low_func_vma = 0; | |
2692 | asymbol *func = 0; | |
2693 | *filename_ptr = abfd->filename; | |
2694 | *functionname_ptr = 0; | |
2695 | *line_ptr = 0; | |
2696 | if (symbols != (asymbol **)NULL) { | |
2697 | for (p = symbols; *p; p++) { | |
2698 | aout_symbol_type *q = (aout_symbol_type *)(*p); | |
98d43107 | 2699 | next: |
7ed4093a SC |
2700 | switch (q->type){ |
2701 | case N_SO: | |
3f7607af | 2702 | main_file_name = current_file_name = q->symbol.name; |
98d43107 JG |
2703 | /* Look ahead to next symbol to check if that too is an N_SO. */ |
2704 | p++; | |
2705 | if (*p == NULL) | |
2706 | break; | |
2707 | q = (aout_symbol_type *)(*p); | |
6db82ea7 | 2708 | if (q->type != (int)N_SO) |
98d43107 JG |
2709 | goto next; |
2710 | ||
2711 | /* Found a second N_SO First is directory; second is filename. */ | |
3f7607af PB |
2712 | directory_name = current_file_name; |
2713 | main_file_name = current_file_name = q->symbol.name; | |
2714 | if (obj_textsec(abfd) != section) | |
2715 | goto done; | |
2716 | break; | |
2717 | case N_SOL: | |
2718 | current_file_name = q->symbol.name; | |
7ed4093a | 2719 | break; |
3f7607af | 2720 | |
7ed4093a SC |
2721 | case N_SLINE: |
2722 | ||
2723 | case N_DSLINE: | |
2724 | case N_BSLINE: | |
2725 | /* We'll keep this if it resolves nearer than the one we have already */ | |
2726 | if (q->symbol.value >= offset && | |
2727 | q->symbol.value < high_line_vma) { | |
2728 | *line_ptr = q->desc; | |
2729 | high_line_vma = q->symbol.value; | |
3f7607af | 2730 | line_file_name = current_file_name; |
7ed4093a SC |
2731 | } |
2732 | break; | |
2733 | case N_FUN: | |
2734 | { | |
2735 | /* We'll keep this if it is nearer than the one we have already */ | |
2736 | if (q->symbol.value >= low_func_vma && | |
2737 | q->symbol.value <= offset) { | |
2738 | low_func_vma = q->symbol.value; | |
2739 | func = (asymbol *)q; | |
2740 | } | |
2741 | if (*line_ptr && func) { | |
2742 | CONST char *function = func->name; | |
2743 | char *p; | |
2744 | strncpy(buffer, function, sizeof(buffer)-1); | |
2745 | buffer[sizeof(buffer)-1] = 0; | |
2746 | /* Have to remove : stuff */ | |
2747 | p = strchr(buffer,':'); | |
7b02b4ed | 2748 | if (p != NULL) { *p = '\0'; } |
7ed4093a | 2749 | *functionname_ptr = buffer; |
3f7607af | 2750 | goto done; |
7ed4093a SC |
2751 | |
2752 | } | |
2753 | } | |
2754 | break; | |
2755 | } | |
2756 | } | |
2757 | } | |
3f7607af PB |
2758 | |
2759 | done: | |
2760 | if (*line_ptr) | |
2761 | main_file_name = line_file_name; | |
2762 | if (main_file_name) { | |
2763 | if (main_file_name[0] == '/' || directory_name == NULL) | |
2764 | *filename_ptr = main_file_name; | |
2765 | else { | |
2766 | sprintf(filename_buffer, "%.140s%.50s", | |
2767 | directory_name, main_file_name); | |
2768 | *filename_ptr = filename_buffer; | |
2769 | } | |
2770 | } | |
7ed4093a SC |
2771 | return true; |
2772 | ||
2773 | } | |
2774 | ||
728472f1 | 2775 | /*ARGSUSED*/ |
c188b0be | 2776 | int |
cbdc7909 JG |
2777 | DEFUN(NAME(aout,sizeof_headers),(abfd, execable), |
2778 | bfd *abfd AND | |
9e2dad8e | 2779 | boolean execable) |
7ed4093a | 2780 | { |
6db82ea7 | 2781 | return adata(abfd).exec_bytes_size; |
7ed4093a | 2782 | } |
4c3721d5 ILT |
2783 | \f |
2784 | /* a.out link code. */ | |
2785 | ||
2786 | /* a.out linker hash table entries. */ | |
2787 | ||
2788 | struct aout_link_hash_entry | |
2789 | { | |
2790 | struct bfd_link_hash_entry root; | |
2791 | /* Symbol index in output file. */ | |
2792 | int indx; | |
2793 | }; | |
2794 | ||
2795 | /* a.out linker hash table. */ | |
2796 | ||
2797 | struct aout_link_hash_table | |
2798 | { | |
2799 | struct bfd_link_hash_table root; | |
2800 | }; | |
2801 | ||
2802 | static struct bfd_hash_entry *aout_link_hash_newfunc | |
2803 | PARAMS ((struct bfd_hash_entry *entry, | |
2804 | struct bfd_hash_table *table, | |
2805 | const char *string)); | |
2806 | static boolean aout_link_add_object_symbols | |
2807 | PARAMS ((bfd *, struct bfd_link_info *)); | |
2808 | static boolean aout_link_check_archive_element | |
2809 | PARAMS ((bfd *, struct bfd_link_info *, boolean *)); | |
2810 | static boolean aout_link_get_symbols PARAMS ((bfd *)); | |
2811 | static boolean aout_link_free_symbols PARAMS ((bfd *)); | |
2812 | static boolean aout_link_check_ar_symbols | |
2813 | PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded)); | |
2814 | static boolean aout_link_add_symbols | |
2815 | PARAMS ((bfd *, struct bfd_link_info *)); | |
2816 | ||
2817 | /* Routine to create an entry in an a.out link hash table. */ | |
2818 | ||
2819 | static struct bfd_hash_entry * | |
2820 | aout_link_hash_newfunc (entry, table, string) | |
2821 | struct bfd_hash_entry *entry; | |
2822 | struct bfd_hash_table *table; | |
2823 | const char *string; | |
2824 | { | |
2825 | struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry; | |
2826 | ||
2827 | /* Allocate the structure if it has not already been allocated by a | |
2828 | subclass. */ | |
2829 | if (ret == (struct aout_link_hash_entry *) NULL) | |
2830 | ret = ((struct aout_link_hash_entry *) | |
2831 | bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry))); | |
9783e04a DM |
2832 | if (ret == (struct aout_link_hash_entry *) NULL) |
2833 | { | |
68241b2b | 2834 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2835 | return (struct bfd_hash_entry *) ret; |
2836 | } | |
4c3721d5 ILT |
2837 | |
2838 | /* Call the allocation method of the superclass. */ | |
2839 | ret = ((struct aout_link_hash_entry *) | |
2840 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
2841 | table, string)); | |
9783e04a DM |
2842 | if (ret) |
2843 | /* Set local fields. */ | |
2844 | ret->indx = -1; | |
4c3721d5 ILT |
2845 | |
2846 | return (struct bfd_hash_entry *) ret; | |
2847 | } | |
2848 | ||
2849 | /* Create an a.out link hash table. */ | |
2850 | ||
2851 | struct bfd_link_hash_table * | |
2852 | NAME(aout,link_hash_table_create) (abfd) | |
2853 | bfd *abfd; | |
2854 | { | |
2855 | struct aout_link_hash_table *ret; | |
2856 | ||
2857 | ret = ((struct aout_link_hash_table *) | |
9783e04a DM |
2858 | malloc (sizeof (struct aout_link_hash_table))); |
2859 | if (ret == (struct aout_link_hash_table *) NULL) | |
2860 | { | |
68241b2b | 2861 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2862 | return (struct bfd_link_hash_table *) NULL; |
2863 | } | |
4c3721d5 ILT |
2864 | if (! _bfd_link_hash_table_init (&ret->root, abfd, |
2865 | aout_link_hash_newfunc)) | |
2866 | { | |
2867 | free (ret); | |
2868 | return (struct bfd_link_hash_table *) NULL; | |
2869 | } | |
2870 | return &ret->root; | |
2871 | } | |
2872 | ||
2873 | /* Look up an entry in an a.out link hash table. */ | |
2874 | ||
2875 | #define aout_link_hash_lookup(table, string, create, copy, follow) \ | |
2876 | ((struct aout_link_hash_entry *) \ | |
2877 | bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow))) | |
2878 | ||
2879 | /* Traverse an a.out link hash table. */ | |
2880 | ||
2881 | #define aout_link_hash_traverse(table, func, info) \ | |
2882 | (bfd_link_hash_traverse \ | |
2883 | (&(table)->root, \ | |
2884 | (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \ | |
2885 | (info))) | |
2886 | ||
2887 | /* Get the a.out link hash table from the info structure. This is | |
2888 | just a cast. */ | |
2889 | ||
2890 | #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash)) | |
2891 | ||
2892 | /* Given an a.out BFD, add symbols to the global hash table as | |
2893 | appropriate. */ | |
2894 | ||
2895 | boolean | |
2896 | NAME(aout,link_add_symbols) (abfd, info) | |
2897 | bfd *abfd; | |
2898 | struct bfd_link_info *info; | |
2899 | { | |
2900 | switch (bfd_get_format (abfd)) | |
2901 | { | |
2902 | case bfd_object: | |
2903 | return aout_link_add_object_symbols (abfd, info); | |
2904 | case bfd_archive: | |
2905 | return _bfd_generic_link_add_archive_symbols | |
2906 | (abfd, info, aout_link_check_archive_element); | |
2907 | default: | |
68241b2b | 2908 | bfd_set_error (bfd_error_wrong_format); |
4c3721d5 ILT |
2909 | return false; |
2910 | } | |
2911 | } | |
2912 | ||
2913 | /* Add symbols from an a.out object file. */ | |
2914 | ||
2915 | static boolean | |
2916 | aout_link_add_object_symbols (abfd, info) | |
2917 | bfd *abfd; | |
2918 | struct bfd_link_info *info; | |
2919 | { | |
2920 | if (! aout_link_get_symbols (abfd)) | |
2921 | return false; | |
2922 | if (! aout_link_add_symbols (abfd, info)) | |
2923 | return false; | |
2924 | if (! info->keep_memory) | |
2925 | { | |
2926 | if (! aout_link_free_symbols (abfd)) | |
2927 | return false; | |
2928 | } | |
2929 | return true; | |
2930 | } | |
2931 | ||
2932 | /* Check a single archive element to see if we need to include it in | |
2933 | the link. *PNEEDED is set according to whether this element is | |
2934 | needed in the link or not. This is called from | |
2935 | _bfd_generic_link_add_archive_symbols. */ | |
2936 | ||
2937 | static boolean | |
2938 | aout_link_check_archive_element (abfd, info, pneeded) | |
2939 | bfd *abfd; | |
2940 | struct bfd_link_info *info; | |
2941 | boolean *pneeded; | |
2942 | { | |
2943 | if (! aout_link_get_symbols (abfd)) | |
2944 | return false; | |
2945 | ||
2946 | if (! aout_link_check_ar_symbols (abfd, info, pneeded)) | |
2947 | return false; | |
2948 | ||
2949 | if (*pneeded) | |
2950 | { | |
2951 | if (! aout_link_add_symbols (abfd, info)) | |
2952 | return false; | |
2953 | } | |
2954 | ||
2955 | /* We keep around the symbols even if we aren't going to use this | |
2956 | object file, because we may want to reread it. This doesn't | |
2957 | waste too much memory, because it isn't all that common to read | |
2958 | an archive element but not need it. */ | |
2959 | if (! info->keep_memory) | |
2960 | { | |
2961 | if (! aout_link_free_symbols (abfd)) | |
2962 | return false; | |
2963 | } | |
2964 | ||
2965 | return true; | |
2966 | } | |
2967 | ||
2968 | /* Read the internal symbols from an a.out file. */ | |
2969 | ||
2970 | static boolean | |
2971 | aout_link_get_symbols (abfd) | |
2972 | bfd *abfd; | |
2973 | { | |
2974 | bfd_size_type count; | |
2975 | struct external_nlist *syms; | |
2976 | unsigned char string_chars[BYTES_IN_WORD]; | |
2977 | bfd_size_type stringsize; | |
2978 | char *strings; | |
2979 | ||
2980 | if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL) | |
2981 | { | |
2982 | /* We already have them. */ | |
2983 | return true; | |
2984 | } | |
2985 | ||
2986 | count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE; | |
2987 | ||
9783e04a | 2988 | /* We allocate using malloc to make the values easy to free |
4c3721d5 ILT |
2989 | later on. If we put them on the obstack it might not be possible |
2990 | to free them. */ | |
2991 | syms = ((struct external_nlist *) | |
9783e04a DM |
2992 | malloc ((size_t) count * EXTERNAL_NLIST_SIZE)); |
2993 | if (syms == (struct external_nlist *) NULL) | |
2994 | { | |
68241b2b | 2995 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2996 | return false; |
2997 | } | |
4c3721d5 ILT |
2998 | |
2999 | if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0 | |
3000 | || (bfd_read ((PTR) syms, 1, exec_hdr (abfd)->a_syms, abfd) | |
3001 | != exec_hdr (abfd)->a_syms)) | |
3002 | return false; | |
3003 | ||
3004 | /* Get the size of the strings. */ | |
3005 | if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0 | |
3006 | || (bfd_read ((PTR) string_chars, BYTES_IN_WORD, 1, abfd) | |
3007 | != BYTES_IN_WORD)) | |
3008 | return false; | |
3009 | stringsize = GET_WORD (abfd, string_chars); | |
9783e04a DM |
3010 | strings = (char *) malloc ((size_t) stringsize); |
3011 | if (strings == NULL) | |
3012 | { | |
68241b2b | 3013 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
3014 | return false; |
3015 | } | |
4c3721d5 ILT |
3016 | |
3017 | /* Skip space for the string count in the buffer for convenience | |
3018 | when using indexes. */ | |
3019 | if (bfd_read (strings + BYTES_IN_WORD, 1, stringsize - BYTES_IN_WORD, abfd) | |
3020 | != stringsize - BYTES_IN_WORD) | |
3021 | return false; | |
3022 | ||
3023 | /* Save the data. */ | |
3024 | obj_aout_external_syms (abfd) = syms; | |
3025 | obj_aout_external_sym_count (abfd) = count; | |
3026 | obj_aout_external_strings (abfd) = strings; | |
3027 | ||
3028 | return true; | |
3029 | } | |
3030 | ||
3031 | /* Free up the internal symbols read from an a.out file. */ | |
3032 | ||
3033 | static boolean | |
3034 | aout_link_free_symbols (abfd) | |
3035 | bfd *abfd; | |
3036 | { | |
3037 | if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL) | |
3038 | { | |
3039 | free ((PTR) obj_aout_external_syms (abfd)); | |
3040 | obj_aout_external_syms (abfd) = (struct external_nlist *) NULL; | |
3041 | } | |
3042 | if (obj_aout_external_strings (abfd) != (char *) NULL) | |
3043 | { | |
3044 | free ((PTR) obj_aout_external_strings (abfd)); | |
3045 | obj_aout_external_strings (abfd) = (char *) NULL; | |
3046 | } | |
3047 | return true; | |
3048 | } | |
3049 | ||
3050 | /* Look through the internal symbols to see if this object file should | |
3051 | be included in the link. We should include this object file if it | |
3052 | defines any symbols which are currently undefined. If this object | |
3053 | file defines a common symbol, then we may adjust the size of the | |
3054 | known symbol but we do not include the object file in the link | |
3055 | (unless there is some other reason to include it). */ | |
3056 | ||
3057 | static boolean | |
3058 | aout_link_check_ar_symbols (abfd, info, pneeded) | |
3059 | bfd *abfd; | |
3060 | struct bfd_link_info *info; | |
3061 | boolean *pneeded; | |
3062 | { | |
3063 | register struct external_nlist *p; | |
3064 | struct external_nlist *pend; | |
3065 | char *strings; | |
3066 | ||
3067 | *pneeded = false; | |
3068 | ||
3069 | /* Look through all the symbols. */ | |
3070 | p = obj_aout_external_syms (abfd); | |
3071 | pend = p + obj_aout_external_sym_count (abfd); | |
3072 | strings = obj_aout_external_strings (abfd); | |
3073 | for (; p < pend; p++) | |
3074 | { | |
3075 | int type = bfd_h_get_8 (abfd, p->e_type); | |
3076 | const char *name; | |
3077 | struct bfd_link_hash_entry *h; | |
3078 | ||
3079 | /* Ignore symbols that are not externally visible. */ | |
3080 | if ((type & N_EXT) == 0) | |
9b39ed6b ILT |
3081 | { |
3082 | if (type == N_WARNING | |
3083 | || type == N_INDR) | |
3084 | ++p; | |
3085 | continue; | |
3086 | } | |
4c3721d5 ILT |
3087 | |
3088 | name = strings + GET_WORD (abfd, p->e_strx); | |
3089 | h = bfd_link_hash_lookup (info->hash, name, false, false, true); | |
3090 | ||
3091 | /* We are only interested in symbols that are currently | |
3092 | undefined or common. */ | |
3093 | if (h == (struct bfd_link_hash_entry *) NULL | |
3094 | || (h->type != bfd_link_hash_undefined | |
3095 | && h->type != bfd_link_hash_common)) | |
9b39ed6b ILT |
3096 | { |
3097 | if (type == (N_INDR | N_EXT)) | |
3098 | ++p; | |
3099 | continue; | |
3100 | } | |
4c3721d5 | 3101 | |
9b39ed6b ILT |
3102 | if (type == (N_TEXT | N_EXT) |
3103 | || type == (N_DATA | N_EXT) | |
3104 | || type == (N_BSS | N_EXT) | |
3105 | || type == (N_ABS | N_EXT) | |
3106 | || type == (N_INDR | N_EXT)) | |
4c3721d5 ILT |
3107 | { |
3108 | /* This object file defines this symbol. We must link it | |
3109 | in. This is true regardless of whether the current | |
3110 | definition of the symbol is undefined or common. If the | |
3111 | current definition is common, we have a case in which we | |
3112 | have already seen an object file including | |
3113 | int a; | |
3114 | and this object file from the archive includes | |
3115 | int a = 5; | |
3116 | In such a case we must include this object file. */ | |
3117 | if (! (*info->callbacks->add_archive_element) (info, abfd, name)) | |
3118 | return false; | |
3119 | *pneeded = true; | |
3120 | return true; | |
3121 | } | |
3122 | ||
9b39ed6b | 3123 | if (type == (N_UNDF | N_EXT)) |
4c3721d5 ILT |
3124 | { |
3125 | bfd_vma value; | |
3126 | ||
3127 | value = GET_WORD (abfd, p->e_value); | |
3128 | if (value != 0) | |
3129 | { | |
3130 | /* This symbol is common in the object from the archive | |
3131 | file. */ | |
3132 | if (h->type == bfd_link_hash_undefined) | |
3133 | { | |
3134 | bfd *symbfd; | |
3135 | ||
3136 | symbfd = h->u.undef.abfd; | |
3137 | if (symbfd == (bfd *) NULL) | |
3138 | { | |
3139 | /* This symbol was created as undefined from | |
3140 | outside BFD. We assume that we should link | |
3141 | in the object file. This is done for the -u | |
3142 | option in the linker. */ | |
3143 | if (! (*info->callbacks->add_archive_element) (info, | |
3144 | abfd, | |
3145 | name)) | |
3146 | return false; | |
3147 | *pneeded = true; | |
3148 | return true; | |
3149 | } | |
3150 | /* Turn the current link symbol into a common | |
3151 | symbol. It is already on the undefs list. */ | |
3152 | h->type = bfd_link_hash_common; | |
3153 | h->u.c.size = value; | |
3154 | h->u.c.section = bfd_make_section_old_way (symbfd, | |
3155 | "COMMON"); | |
3156 | } | |
3157 | else | |
3158 | { | |
3159 | /* Adjust the size of the common symbol if | |
3160 | necessary. */ | |
3161 | if (value > h->u.c.size) | |
3162 | h->u.c.size = value; | |
3163 | } | |
3164 | } | |
3165 | } | |
3166 | } | |
3167 | ||
3168 | /* We do not need this object file. */ | |
3169 | return true; | |
3170 | } | |
3171 | ||
3172 | /* Add all symbols from an object file to the hash table. */ | |
3173 | ||
3174 | static boolean | |
3175 | aout_link_add_symbols (abfd, info) | |
3176 | bfd *abfd; | |
3177 | struct bfd_link_info *info; | |
3178 | { | |
3179 | bfd_size_type sym_count; | |
3180 | char *strings; | |
3181 | boolean copy; | |
3182 | struct aout_link_hash_entry **sym_hash; | |
3183 | register struct external_nlist *p; | |
3184 | struct external_nlist *pend; | |
3185 | ||
3186 | sym_count = obj_aout_external_sym_count (abfd); | |
3187 | strings = obj_aout_external_strings (abfd); | |
3188 | if (info->keep_memory) | |
3189 | copy = false; | |
3190 | else | |
3191 | copy = true; | |
3192 | ||
4c3721d5 ILT |
3193 | /* We keep a list of the linker hash table entries that correspond |
3194 | to particular symbols. We could just look them up in the hash | |
3195 | table, but keeping the list is more efficient. Perhaps this | |
3196 | should be conditional on info->keep_memory. */ | |
3197 | sym_hash = ((struct aout_link_hash_entry **) | |
3198 | bfd_alloc (abfd, | |
3199 | ((size_t) sym_count | |
3200 | * sizeof (struct aout_link_hash_entry *)))); | |
9783e04a DM |
3201 | if (!sym_hash) |
3202 | { | |
68241b2b | 3203 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
3204 | return false; |
3205 | } | |
4c3721d5 ILT |
3206 | obj_aout_sym_hashes (abfd) = sym_hash; |
3207 | ||
3208 | p = obj_aout_external_syms (abfd); | |
3209 | pend = p + sym_count; | |
3210 | for (; p < pend; p++, sym_hash++) | |
3211 | { | |
3212 | int type; | |
3213 | const char *name; | |
3214 | bfd_vma value; | |
3215 | asection *section; | |
3216 | flagword flags; | |
3217 | const char *string; | |
3218 | ||
3219 | *sym_hash = NULL; | |
3220 | ||
3221 | type = bfd_h_get_8 (abfd, p->e_type); | |
3222 | ||
3223 | /* Ignore debugging symbols. */ | |
3224 | if ((type & N_STAB) != 0) | |
3225 | continue; | |
3226 | ||
3227 | /* Ignore symbols that are not external. */ | |
3228 | if ((type & N_EXT) == 0 | |
3229 | && type != N_WARNING | |
3230 | && type != N_SETA | |
3231 | && type != N_SETT | |
3232 | && type != N_SETD | |
3233 | && type != N_SETB) | |
3234 | { | |
3235 | /* If this is an N_INDR symbol we must skip the next entry, | |
3236 | which is the symbol to indirect to (actually, an N_INDR | |
3237 | symbol without N_EXT set is pretty useless). */ | |
3238 | if (type == N_INDR) | |
53155af1 ILT |
3239 | { |
3240 | ++p; | |
3241 | ++sym_hash; | |
3242 | } | |
4c3721d5 ILT |
3243 | continue; |
3244 | } | |
3245 | ||
3246 | /* Ignore N_FN symbols (these appear to have N_EXT set). */ | |
3247 | if (type == N_FN) | |
3248 | continue; | |
3249 | ||
3250 | name = strings + GET_WORD (abfd, p->e_strx); | |
3251 | value = GET_WORD (abfd, p->e_value); | |
3252 | flags = BSF_GLOBAL; | |
3253 | string = NULL; | |
3254 | switch (type) | |
3255 | { | |
3256 | default: | |
3257 | abort (); | |
3258 | case N_UNDF | N_EXT: | |
3259 | if (value != 0) | |
3260 | section = &bfd_com_section; | |
3261 | else | |
3262 | section = &bfd_und_section; | |
3263 | break; | |
3264 | case N_ABS | N_EXT: | |
3265 | section = &bfd_abs_section; | |
3266 | break; | |
3267 | case N_TEXT | N_EXT: | |
3268 | section = obj_textsec (abfd); | |
3269 | value -= bfd_get_section_vma (abfd, section); | |
3270 | break; | |
3271 | case N_DATA | N_EXT: | |
3272 | section = obj_datasec (abfd); | |
3273 | value -= bfd_get_section_vma (abfd, section); | |
3274 | break; | |
3275 | case N_BSS | N_EXT: | |
3276 | section = obj_bsssec (abfd); | |
3277 | value -= bfd_get_section_vma (abfd, section); | |
3278 | break; | |
3279 | case N_INDR | N_EXT: | |
3280 | /* An indirect symbol. The next symbol is the symbol | |
3281 | which this one really is. */ | |
3282 | BFD_ASSERT (p + 1 < pend); | |
3283 | ++p; | |
3284 | string = strings + GET_WORD (abfd, p->e_strx); | |
3285 | section = &bfd_ind_section; | |
3286 | flags |= BSF_INDIRECT; | |
3287 | break; | |
3288 | case N_COMM | N_EXT: | |
3289 | section = &bfd_com_section; | |
3290 | break; | |
964affdc | 3291 | case N_SETA: case N_SETA | N_EXT: |
4c3721d5 ILT |
3292 | section = &bfd_abs_section; |
3293 | flags |= BSF_CONSTRUCTOR; | |
3294 | break; | |
964affdc | 3295 | case N_SETT: case N_SETT | N_EXT: |
4c3721d5 ILT |
3296 | section = obj_textsec (abfd); |
3297 | flags |= BSF_CONSTRUCTOR; | |
3298 | value -= bfd_get_section_vma (abfd, section); | |
3299 | break; | |
964affdc | 3300 | case N_SETD: case N_SETD | N_EXT: |
4c3721d5 ILT |
3301 | section = obj_datasec (abfd); |
3302 | flags |= BSF_CONSTRUCTOR; | |
3303 | value -= bfd_get_section_vma (abfd, section); | |
3304 | break; | |
964affdc | 3305 | case N_SETB: case N_SETB | N_EXT: |
4c3721d5 ILT |
3306 | section = obj_bsssec (abfd); |
3307 | flags |= BSF_CONSTRUCTOR; | |
3308 | value -= bfd_get_section_vma (abfd, section); | |
3309 | break; | |
3310 | case N_WARNING: | |
3311 | /* A warning symbol. The next symbol is the one to warn | |
3312 | about. */ | |
3313 | BFD_ASSERT (p + 1 < pend); | |
3314 | ++p; | |
3315 | string = name; | |
3316 | name = strings + GET_WORD (abfd, p->e_strx); | |
3317 | section = &bfd_und_section; | |
3318 | flags |= BSF_WARNING; | |
3319 | break; | |
3320 | } | |
3321 | ||
3322 | if (! (_bfd_generic_link_add_one_symbol | |
e68de5d5 ILT |
3323 | (info, abfd, name, flags, section, value, string, copy, false, |
3324 | ARCH_SIZE, (struct bfd_link_hash_entry **) sym_hash))) | |
4c3721d5 | 3325 | return false; |
53155af1 ILT |
3326 | |
3327 | if (type == (N_INDR | N_EXT) || type == N_WARNING) | |
3328 | ++sym_hash; | |
4c3721d5 ILT |
3329 | } |
3330 | ||
3331 | return true; | |
3332 | } | |
3333 | ||
3334 | /* During the final link step we need to pass around a bunch of | |
3335 | information, so we do it in an instance of this structure. */ | |
3336 | ||
3337 | struct aout_final_link_info | |
3338 | { | |
3339 | /* General link information. */ | |
3340 | struct bfd_link_info *info; | |
3341 | /* Output bfd. */ | |
3342 | bfd *output_bfd; | |
3343 | /* Reloc file positions. */ | |
3344 | file_ptr treloff, dreloff; | |
3345 | /* File position of symbols. */ | |
3346 | file_ptr symoff; | |
3347 | /* String table. */ | |
3348 | struct stringtab_data strtab; | |
3349 | }; | |
3350 | ||
3351 | static boolean aout_link_input_bfd | |
3352 | PARAMS ((struct aout_final_link_info *, bfd *input_bfd)); | |
3353 | static boolean aout_link_write_symbols | |
3354 | PARAMS ((struct aout_final_link_info *, bfd *input_bfd, int *symbol_map)); | |
3355 | static boolean aout_link_write_other_symbol | |
3356 | PARAMS ((struct aout_link_hash_entry *, PTR)); | |
3357 | static boolean aout_link_input_section | |
3358 | PARAMS ((struct aout_final_link_info *, bfd *input_bfd, | |
3359 | asection *input_section, file_ptr *reloff_ptr, | |
3360 | bfd_size_type rel_size, int *symbol_map)); | |
3361 | static boolean aout_link_input_section_std | |
3362 | PARAMS ((struct aout_final_link_info *, bfd *input_bfd, | |
3363 | asection *input_section, struct reloc_std_external *, | |
3364 | bfd_size_type rel_size, bfd_byte *contents, int *symbol_map)); | |
3365 | static boolean aout_link_input_section_ext | |
3366 | PARAMS ((struct aout_final_link_info *, bfd *input_bfd, | |
3367 | asection *input_section, struct reloc_ext_external *, | |
3368 | bfd_size_type rel_size, bfd_byte *contents, int *symbol_map)); | |
3369 | static INLINE asection *aout_reloc_index_to_section | |
3370 | PARAMS ((bfd *, int)); | |
3371 | ||
3372 | /* Do the final link step. This is called on the output BFD. The | |
3373 | INFO structure should point to a list of BFDs linked through the | |
3374 | link_next field which can be used to find each BFD which takes part | |
3375 | in the output. Also, each section in ABFD should point to a list | |
3376 | of bfd_link_order structures which list all the input sections for | |
3377 | the output section. */ | |
3378 | ||
3379 | boolean | |
3380 | NAME(aout,final_link) (abfd, info, callback) | |
3381 | bfd *abfd; | |
3382 | struct bfd_link_info *info; | |
3383 | void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *)); | |
3384 | { | |
3385 | struct aout_final_link_info aout_info; | |
3386 | register bfd *sub; | |
3387 | bfd_size_type text_size; | |
3388 | file_ptr text_end; | |
3389 | register struct bfd_link_order *p; | |
3390 | asection *o; | |
3391 | ||
3392 | aout_info.info = info; | |
3393 | aout_info.output_bfd = abfd; | |
3394 | ||
3395 | if (! info->relocateable) | |
3396 | { | |
3397 | exec_hdr (abfd)->a_trsize = 0; | |
3398 | exec_hdr (abfd)->a_drsize = 0; | |
3399 | } | |
3400 | else | |
3401 | { | |
3402 | bfd_size_type trsize, drsize; | |
3403 | ||
3404 | /* Count up the relocation sizes. */ | |
3405 | trsize = 0; | |
3406 | drsize = 0; | |
3407 | for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next) | |
3408 | { | |
3409 | if (bfd_get_flavour (abfd) == bfd_target_aout_flavour) | |
3410 | { | |
3411 | trsize += exec_hdr (sub)->a_trsize; | |
3412 | drsize += exec_hdr (sub)->a_drsize; | |
3413 | } | |
3414 | else | |
3415 | { | |
3416 | /* FIXME: We need to identify the .text and .data sections | |
3417 | and call get_reloc_upper_bound and canonicalize_reloc to | |
3418 | work out the number of relocs needed, and then multiply | |
3419 | by the reloc size. */ | |
3420 | abort (); | |
3421 | } | |
3422 | } | |
3423 | exec_hdr (abfd)->a_trsize = trsize; | |
3424 | exec_hdr (abfd)->a_drsize = drsize; | |
3425 | } | |
3426 | ||
964affdc DM |
3427 | exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd); |
3428 | ||
4c3721d5 ILT |
3429 | /* Adjust the section sizes and vmas according to the magic number. |
3430 | This sets a_text, a_data and a_bss in the exec_hdr and sets the | |
3431 | filepos for each section. */ | |
3432 | if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end)) | |
3433 | return false; | |
3434 | ||
3435 | /* The relocation and symbol file positions differ among a.out | |
3436 | targets. We are passed a callback routine from the backend | |
3437 | specific code to handle this. | |
3438 | FIXME: At this point we do not know how much space the symbol | |
3439 | table will require. This will not work for any (nonstandard) | |
3440 | a.out target that needs to know the symbol table size before it | |
3441 | can compute the relocation file positions. This may or may not | |
3442 | be the case for the hp300hpux target, for example. */ | |
3443 | (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff, | |
3444 | &aout_info.symoff); | |
3445 | obj_textsec (abfd)->rel_filepos = aout_info.treloff; | |
3446 | obj_datasec (abfd)->rel_filepos = aout_info.dreloff; | |
3447 | obj_sym_filepos (abfd) = aout_info.symoff; | |
3448 | ||
3449 | /* We keep a count of the symbols as we output them. */ | |
3450 | obj_aout_external_sym_count (abfd) = 0; | |
3451 | ||
3452 | /* We accumulate the string table as we write out the symbols. */ | |
3453 | stringtab_init (&aout_info.strtab); | |
3454 | ||
3455 | /* The most time efficient way to do the link would be to read all | |
3456 | the input object files into memory and then sort out the | |
3457 | information into the output file. Unfortunately, that will | |
3458 | probably use too much memory. Another method would be to step | |
3459 | through everything that composes the text section and write it | |
3460 | out, and then everything that composes the data section and write | |
3461 | it out, and then write out the relocs, and then write out the | |
3462 | symbols. Unfortunately, that requires reading stuff from each | |
3463 | input file several times, and we will not be able to keep all the | |
3464 | input files open simultaneously, and reopening them will be slow. | |
3465 | ||
3466 | What we do is basically process one input file at a time. We do | |
3467 | everything we need to do with an input file once--copy over the | |
3468 | section contents, handle the relocation information, and write | |
3469 | out the symbols--and then we throw away the information we read | |
3470 | from it. This approach requires a lot of lseeks of the output | |
3471 | file, which is unfortunate but still faster than reopening a lot | |
3472 | of files. | |
3473 | ||
3474 | We use the output_has_begun field of the input BFDs to see | |
3475 | whether we have already handled it. */ | |
3476 | for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next) | |
3477 | sub->output_has_begun = false; | |
3478 | ||
3479 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
3480 | { | |
4c3721d5 ILT |
3481 | for (p = o->link_order_head; |
3482 | p != (struct bfd_link_order *) NULL; | |
3483 | p = p->next) | |
3484 | { | |
3485 | /* If we might be using the C based alloca function, we need | |
3486 | to dump the memory allocated by aout_link_input_bfd. */ | |
3487 | #ifndef __GNUC__ | |
3488 | #ifndef alloca | |
3489 | (void) alloca (0); | |
3490 | #endif | |
3491 | #endif | |
e68de5d5 ILT |
3492 | if (p->type == bfd_indirect_link_order |
3493 | && (bfd_get_flavour (p->u.indirect.section->owner) | |
3494 | == bfd_target_aout_flavour)) | |
4c3721d5 | 3495 | { |
e68de5d5 ILT |
3496 | bfd *input_bfd; |
3497 | ||
4c3721d5 | 3498 | input_bfd = p->u.indirect.section->owner; |
e68de5d5 | 3499 | if (! input_bfd->output_has_begun) |
4c3721d5 | 3500 | { |
e68de5d5 ILT |
3501 | if (! aout_link_input_bfd (&aout_info, input_bfd)) |
3502 | return false; | |
3503 | input_bfd->output_has_begun = true; | |
4c3721d5 | 3504 | } |
e68de5d5 ILT |
3505 | } |
3506 | else | |
3507 | { | |
4c3721d5 ILT |
3508 | if (! _bfd_default_link_order (abfd, info, o, p)) |
3509 | return false; | |
3510 | } | |
3511 | } | |
3512 | } | |
3513 | ||
3514 | /* Write out any symbols that we have not already written out. */ | |
3515 | aout_link_hash_traverse (aout_hash_table (info), | |
3516 | aout_link_write_other_symbol, | |
3517 | (PTR) &aout_info); | |
3518 | ||
3519 | /* Update the header information. */ | |
3520 | abfd->symcount = obj_aout_external_sym_count (abfd); | |
3521 | exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE; | |
3522 | obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms; | |
3523 | obj_textsec (abfd)->reloc_count = | |
3524 | exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd); | |
3525 | obj_datasec (abfd)->reloc_count = | |
3526 | exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd); | |
3527 | ||
3528 | /* Write out the string table. */ | |
3529 | if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0) | |
3530 | return false; | |
3531 | emit_strtab (abfd, &aout_info.strtab); | |
3532 | ||
3533 | return true; | |
3534 | } | |
3535 | ||
3536 | /* Link an a.out input BFD into the output file. */ | |
3537 | ||
3538 | static boolean | |
3539 | aout_link_input_bfd (finfo, input_bfd) | |
3540 | struct aout_final_link_info *finfo; | |
3541 | bfd *input_bfd; | |
3542 | { | |
3543 | bfd_size_type sym_count; | |
3544 | int *symbol_map; | |
3545 | ||
3546 | BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object); | |
3547 | ||
3548 | /* Get the symbols. We probably have them already, unless | |
3549 | finfo->info->keep_memory is false. */ | |
3550 | if (! aout_link_get_symbols (input_bfd)) | |
3551 | return false; | |
3552 | ||
3553 | sym_count = obj_aout_external_sym_count (input_bfd); | |
3554 | symbol_map = (int *) alloca ((size_t) sym_count * sizeof (int)); | |
3555 | ||
3556 | /* Write out the symbols and get a map of the new indices. */ | |
3557 | if (! aout_link_write_symbols (finfo, input_bfd, symbol_map)) | |
3558 | return false; | |
3559 | ||
3560 | /* Relocate and write out the sections. */ | |
3561 | if (! aout_link_input_section (finfo, input_bfd, | |
3562 | obj_textsec (input_bfd), | |
3563 | &finfo->treloff, | |
3564 | exec_hdr (input_bfd)->a_trsize, | |
3565 | symbol_map) | |
3566 | || ! aout_link_input_section (finfo, input_bfd, | |
3567 | obj_datasec (input_bfd), | |
3568 | &finfo->dreloff, | |
3569 | exec_hdr (input_bfd)->a_drsize, | |
3570 | symbol_map)) | |
3571 | return false; | |
3572 | ||
3573 | /* If we are not keeping memory, we don't need the symbols any | |
3574 | longer. We still need them if we are keeping memory, because the | |
3575 | strings in the hash table point into them. */ | |
3576 | if (! finfo->info->keep_memory) | |
3577 | { | |
3578 | if (! aout_link_free_symbols (input_bfd)) | |
3579 | return false; | |
3580 | } | |
3581 | ||
3582 | return true; | |
3583 | } | |
3584 | ||
3585 | /* Adjust and write out the symbols for an a.out file. Set the new | |
3586 | symbol indices into a symbol_map. */ | |
3587 | ||
3588 | static boolean | |
3589 | aout_link_write_symbols (finfo, input_bfd, symbol_map) | |
3590 | struct aout_final_link_info *finfo; | |
3591 | bfd *input_bfd; | |
3592 | int *symbol_map; | |
3593 | { | |
3594 | bfd *output_bfd; | |
3595 | bfd_size_type sym_count; | |
3596 | char *strings; | |
3597 | enum bfd_link_strip strip; | |
3598 | enum bfd_link_discard discard; | |
3599 | struct external_nlist *output_syms; | |
3600 | struct external_nlist *outsym; | |
3601 | register struct external_nlist *sym; | |
3602 | struct external_nlist *sym_end; | |
3603 | struct aout_link_hash_entry **sym_hash; | |
3604 | boolean pass; | |
53155af1 | 3605 | boolean skip_indirect; |
4c3721d5 ILT |
3606 | |
3607 | output_bfd = finfo->output_bfd; | |
3608 | sym_count = obj_aout_external_sym_count (input_bfd); | |
3609 | strings = obj_aout_external_strings (input_bfd); | |
3610 | strip = finfo->info->strip; | |
3611 | discard = finfo->info->discard; | |
3612 | output_syms = ((struct external_nlist *) | |
3613 | alloca ((size_t) (sym_count + 1) * EXTERNAL_NLIST_SIZE)); | |
3614 | outsym = output_syms; | |
3615 | ||
3616 | /* First write out a symbol for this object file, unless we are | |
3617 | discarding such symbols. */ | |
3618 | if (strip != strip_all | |
3619 | && (strip != strip_some | |
3620 | || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename, | |
3621 | false, false) != NULL) | |
3622 | && discard != discard_all) | |
3623 | { | |
3624 | bfd_h_put_8 (output_bfd, N_TEXT, outsym->e_type); | |
3625 | bfd_h_put_8 (output_bfd, 0, outsym->e_other); | |
3626 | bfd_h_put_16 (output_bfd, (bfd_vma) 0, outsym->e_desc); | |
3627 | PUT_WORD (output_bfd, | |
3628 | add_to_stringtab (output_bfd, input_bfd->filename, | |
3629 | &finfo->strtab), | |
3630 | outsym->e_strx); | |
3631 | PUT_WORD (output_bfd, | |
2edc8357 ILT |
3632 | (bfd_get_section_vma (output_bfd, |
3633 | obj_textsec (input_bfd)->output_section) | |
3634 | + obj_textsec (input_bfd)->output_offset), | |
4c3721d5 ILT |
3635 | outsym->e_value); |
3636 | ++obj_aout_external_sym_count (output_bfd); | |
3637 | ++outsym; | |
3638 | } | |
3639 | ||
3640 | pass = false; | |
53155af1 | 3641 | skip_indirect = false; |
4c3721d5 ILT |
3642 | sym = obj_aout_external_syms (input_bfd); |
3643 | sym_end = sym + sym_count; | |
3644 | sym_hash = obj_aout_sym_hashes (input_bfd); | |
3645 | for (; sym < sym_end; sym++, sym_hash++, symbol_map++) | |
3646 | { | |
3647 | const char *name; | |
3648 | int type; | |
3649 | boolean skip; | |
3650 | asection *symsec; | |
3651 | bfd_vma val = 0; | |
3652 | ||
3653 | *symbol_map = -1; | |
3654 | ||
3655 | type = bfd_h_get_8 (input_bfd, sym->e_type); | |
3656 | name = strings + GET_WORD (input_bfd, sym->e_strx); | |
3657 | ||
3658 | if (pass) | |
3659 | { | |
53155af1 ILT |
3660 | /* Pass this symbol through. It is the target of an |
3661 | indirect or warning symbol. */ | |
4c3721d5 ILT |
3662 | val = GET_WORD (input_bfd, sym->e_value); |
3663 | pass = false; | |
3664 | } | |
53155af1 ILT |
3665 | else if (skip_indirect) |
3666 | { | |
3667 | /* Skip this symbol, which is the target of an indirect | |
3668 | symbol that we have changed to no longer be an indirect | |
3669 | symbol. */ | |
3670 | skip_indirect = false; | |
3671 | continue; | |
3672 | } | |
4c3721d5 ILT |
3673 | else |
3674 | { | |
3675 | struct aout_link_hash_entry *h; | |
53155af1 | 3676 | struct aout_link_hash_entry *hresolve; |
4c3721d5 ILT |
3677 | |
3678 | /* We have saved the hash table entry for this symbol, if | |
3679 | there is one. Note that we could just look it up again | |
3680 | in the hash table, provided we first check that it is an | |
3681 | external symbol. */ | |
3682 | h = *sym_hash; | |
3683 | ||
3a5b50f4 ILT |
3684 | /* If this is an indirect or warning symbol, then change |
3685 | hresolve to the base symbol. We also change *sym_hash so | |
3686 | that the relocation routines relocate against the real | |
3687 | symbol. */ | |
53155af1 ILT |
3688 | hresolve = h; |
3689 | if (h != (struct aout_link_hash_entry *) NULL | |
3a5b50f4 ILT |
3690 | && (h->root.type == bfd_link_hash_indirect |
3691 | || h->root.type == bfd_link_hash_warning)) | |
53155af1 ILT |
3692 | { |
3693 | hresolve = (struct aout_link_hash_entry *) h->root.u.i.link; | |
3694 | while (hresolve->root.type == bfd_link_hash_indirect) | |
3695 | hresolve = ((struct aout_link_hash_entry *) | |
3696 | hresolve->root.u.i.link); | |
3697 | *sym_hash = hresolve; | |
3698 | } | |
3699 | ||
4c3721d5 ILT |
3700 | /* If the symbol has already been written out, skip it. */ |
3701 | if (h != (struct aout_link_hash_entry *) NULL | |
3702 | && h->root.written) | |
3703 | { | |
3704 | *symbol_map = h->indx; | |
3705 | continue; | |
3706 | } | |
3707 | ||
3708 | /* See if we are stripping this symbol. */ | |
3709 | skip = false; | |
3710 | switch (strip) | |
3711 | { | |
3712 | case strip_none: | |
3713 | break; | |
3714 | case strip_debugger: | |
3715 | if ((type & N_STAB) != 0) | |
3716 | skip = true; | |
3717 | break; | |
3718 | case strip_some: | |
3719 | if (bfd_hash_lookup (finfo->info->keep_hash, name, false, false) | |
3720 | == NULL) | |
3721 | skip = true; | |
3722 | break; | |
3723 | case strip_all: | |
3724 | skip = true; | |
3725 | break; | |
3726 | } | |
3727 | if (skip) | |
3728 | { | |
3729 | if (h != (struct aout_link_hash_entry *) NULL) | |
3730 | h->root.written = true; | |
3731 | continue; | |
3732 | } | |
3733 | ||
3734 | /* Get the value of the symbol. */ | |
3735 | if ((type & N_TYPE) == N_TEXT) | |
3736 | symsec = obj_textsec (input_bfd); | |
3737 | else if ((type & N_TYPE) == N_DATA) | |
3738 | symsec = obj_datasec (input_bfd); | |
3739 | else if ((type & N_TYPE) == N_BSS) | |
3740 | symsec = obj_bsssec (input_bfd); | |
3741 | else if ((type & N_TYPE) == N_ABS) | |
3742 | symsec = &bfd_abs_section; | |
53155af1 ILT |
3743 | else if (((type & N_TYPE) == N_INDR |
3744 | && (hresolve == (struct aout_link_hash_entry *) NULL | |
3745 | || (hresolve->root.type != bfd_link_hash_defined | |
3746 | && hresolve->root.type != bfd_link_hash_common))) | |
4c3721d5 ILT |
3747 | || type == N_WARNING) |
3748 | { | |
53155af1 ILT |
3749 | /* Pass the next symbol through unchanged. The |
3750 | condition above for indirect symbols is so that if | |
3751 | the indirect symbol was defined, we output it with | |
3752 | the correct definition so the debugger will | |
3753 | understand it. */ | |
4c3721d5 ILT |
3754 | pass = true; |
3755 | val = GET_WORD (input_bfd, sym->e_value); | |
3756 | symsec = NULL; | |
3757 | } | |
3758 | else if ((type & N_STAB) != 0) | |
3759 | { | |
3760 | val = GET_WORD (input_bfd, sym->e_value); | |
3761 | symsec = NULL; | |
3762 | } | |
3763 | else | |
3764 | { | |
53155af1 ILT |
3765 | /* If we get here with an indirect symbol, it means that |
3766 | we are outputting it with a real definition. In such | |
3767 | a case we do not want to output the next symbol, | |
3768 | which is the target of the indirection. */ | |
3769 | if ((type & N_TYPE) == N_INDR) | |
3770 | skip_indirect = true; | |
3771 | ||
3772 | /* We need to get the value from the hash table. We use | |
3773 | hresolve so that if we have defined an indirect | |
3774 | symbol we output the final definition. */ | |
4c3721d5 ILT |
3775 | if (h == (struct aout_link_hash_entry *) NULL) |
3776 | val = 0; | |
53155af1 | 3777 | else if (hresolve->root.type == bfd_link_hash_defined) |
4c3721d5 | 3778 | { |
53155af1 | 3779 | asection *input_section; |
4c3721d5 ILT |
3780 | asection *output_section; |
3781 | ||
3782 | /* This case means a common symbol which was turned | |
3783 | into a defined symbol. */ | |
53155af1 ILT |
3784 | input_section = hresolve->root.u.def.section; |
3785 | output_section = input_section->output_section; | |
4c3721d5 ILT |
3786 | BFD_ASSERT (output_section == &bfd_abs_section |
3787 | || output_section->owner == output_bfd); | |
53155af1 | 3788 | val = (hresolve->root.u.def.value |
4c3721d5 | 3789 | + bfd_get_section_vma (output_bfd, output_section) |
53155af1 | 3790 | + input_section->output_offset); |
4c3721d5 ILT |
3791 | |
3792 | /* Get the correct type based on the section. If | |
3793 | this is a constructed set, force it to be | |
3794 | globally visible. */ | |
3795 | if (type == N_SETT | |
3796 | || type == N_SETD | |
3797 | || type == N_SETB | |
3798 | || type == N_SETA) | |
3799 | type |= N_EXT; | |
3800 | ||
3801 | type &=~ N_TYPE; | |
3802 | ||
3803 | if (output_section == obj_textsec (output_bfd)) | |
3804 | type |= N_TEXT; | |
3805 | else if (output_section == obj_datasec (output_bfd)) | |
3806 | type |= N_DATA; | |
3807 | else if (output_section == obj_bsssec (output_bfd)) | |
3808 | type |= N_BSS; | |
3809 | else | |
3810 | type |= N_ABS; | |
3811 | } | |
53155af1 ILT |
3812 | else if (hresolve->root.type == bfd_link_hash_common) |
3813 | val = hresolve->root.u.c.size; | |
4c3721d5 ILT |
3814 | else |
3815 | val = 0; | |
3816 | ||
3817 | symsec = NULL; | |
3818 | } | |
3819 | if (symsec != (asection *) NULL) | |
3820 | val = (symsec->output_section->vma | |
3821 | + symsec->output_offset | |
3822 | + (GET_WORD (input_bfd, sym->e_value) | |
3823 | - symsec->vma)); | |
3824 | ||
3825 | /* If this is a global symbol set the written flag, and if | |
3826 | it is a local symbol see if we should discard it. */ | |
3827 | if (h != (struct aout_link_hash_entry *) NULL) | |
3828 | { | |
3829 | h->root.written = true; | |
3830 | h->indx = obj_aout_external_sym_count (output_bfd); | |
3831 | } | |
3832 | else | |
3833 | { | |
3834 | switch (discard) | |
3835 | { | |
3836 | case discard_none: | |
3837 | break; | |
3838 | case discard_l: | |
3839 | if (*name == *finfo->info->lprefix | |
3840 | && (finfo->info->lprefix_len == 1 | |
3841 | || strncmp (name, finfo->info->lprefix, | |
3842 | finfo->info->lprefix_len) == 0)) | |
3843 | skip = true; | |
3844 | break; | |
3845 | case discard_all: | |
3846 | skip = true; | |
3847 | break; | |
3848 | } | |
3849 | if (skip) | |
3850 | { | |
3851 | pass = false; | |
3852 | continue; | |
3853 | } | |
3854 | } | |
3855 | } | |
3856 | ||
3857 | /* Copy this symbol into the list of symbols we are going to | |
3858 | write out. */ | |
3859 | bfd_h_put_8 (output_bfd, type, outsym->e_type); | |
3860 | bfd_h_put_8 (output_bfd, bfd_h_get_8 (input_bfd, sym->e_other), | |
3861 | outsym->e_other); | |
3862 | bfd_h_put_16 (output_bfd, bfd_h_get_16 (input_bfd, sym->e_desc), | |
3863 | outsym->e_desc); | |
3864 | PUT_WORD (output_bfd, | |
3865 | add_to_stringtab (output_bfd, name, &finfo->strtab), | |
3866 | outsym->e_strx); | |
3867 | PUT_WORD (output_bfd, val, outsym->e_value); | |
3868 | *symbol_map = obj_aout_external_sym_count (output_bfd); | |
3869 | ++obj_aout_external_sym_count (output_bfd); | |
3870 | ++outsym; | |
3871 | } | |
3872 | ||
3873 | /* Write out the output symbols we have just constructed. */ | |
3874 | if (outsym > output_syms) | |
3875 | { | |
3876 | bfd_size_type outsym_count; | |
3877 | ||
3878 | if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0) | |
3879 | return false; | |
3880 | outsym_count = outsym - output_syms; | |
3881 | if (bfd_write ((PTR) output_syms, (bfd_size_type) EXTERNAL_NLIST_SIZE, | |
3882 | (bfd_size_type) outsym_count, output_bfd) | |
3883 | != outsym_count * EXTERNAL_NLIST_SIZE) | |
3884 | return false; | |
3885 | finfo->symoff += outsym_count * EXTERNAL_NLIST_SIZE; | |
3886 | } | |
3887 | ||
3888 | return true; | |
3889 | } | |
3890 | ||
3891 | /* Write out a symbol that was not associated with an a.out input | |
3892 | object. */ | |
3893 | ||
3894 | static boolean | |
3895 | aout_link_write_other_symbol (h, data) | |
3896 | struct aout_link_hash_entry *h; | |
3897 | PTR data; | |
3898 | { | |
3899 | struct aout_final_link_info *finfo = (struct aout_final_link_info *) data; | |
3900 | bfd *output_bfd; | |
3901 | int type; | |
3902 | bfd_vma val; | |
3903 | struct external_nlist outsym; | |
3904 | ||
3905 | if (h->root.written) | |
3906 | return true; | |
3907 | ||
9783e04a DM |
3908 | h->root.written = true; |
3909 | ||
3910 | if (finfo->info->strip == strip_all | |
3911 | || (finfo->info->strip == strip_some | |
3912 | && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string, | |
3913 | false, false) == NULL)) | |
3914 | return true; | |
3915 | ||
4c3721d5 ILT |
3916 | output_bfd = finfo->output_bfd; |
3917 | ||
3918 | switch (h->root.type) | |
3919 | { | |
3920 | default: | |
3921 | case bfd_link_hash_new: | |
3922 | abort (); | |
3923 | /* Avoid variable not initialized warnings. */ | |
3924 | return true; | |
3925 | case bfd_link_hash_undefined: | |
3926 | type = N_UNDF | N_EXT; | |
3927 | val = 0; | |
3928 | break; | |
3929 | case bfd_link_hash_defined: | |
3930 | { | |
3931 | asection *sec; | |
3932 | ||
3933 | sec = h->root.u.def.section; | |
3934 | BFD_ASSERT (sec == &bfd_abs_section | |
3935 | || sec->owner == output_bfd); | |
3936 | if (sec == obj_textsec (output_bfd)) | |
3937 | type = N_TEXT | N_EXT; | |
3938 | else if (sec == obj_datasec (output_bfd)) | |
3939 | type = N_DATA | N_EXT; | |
3940 | else if (sec == obj_bsssec (output_bfd)) | |
3941 | type = N_BSS | N_EXT; | |
3942 | else | |
3943 | type = N_ABS | N_EXT; | |
3944 | val = (h->root.u.def.value | |
3945 | + sec->output_section->vma | |
3946 | + sec->output_offset); | |
3947 | } | |
3948 | break; | |
3949 | case bfd_link_hash_common: | |
3950 | type = N_UNDF | N_EXT; | |
3951 | val = h->root.u.c.size; | |
3952 | break; | |
3953 | case bfd_link_hash_indirect: | |
3954 | case bfd_link_hash_warning: | |
3955 | /* FIXME: Ignore these for now. The circumstances under which | |
3956 | they should be written out are not clear to me. */ | |
3957 | return true; | |
3958 | } | |
3959 | ||
3960 | bfd_h_put_8 (output_bfd, type, outsym.e_type); | |
3961 | bfd_h_put_8 (output_bfd, 0, outsym.e_other); | |
3962 | bfd_h_put_16 (output_bfd, 0, outsym.e_desc); | |
3963 | PUT_WORD (output_bfd, | |
3964 | add_to_stringtab (output_bfd, h->root.root.string, &finfo->strtab), | |
3965 | outsym.e_strx); | |
3966 | PUT_WORD (output_bfd, val, outsym.e_value); | |
3967 | ||
3968 | if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0 | |
3969 | || bfd_write ((PTR) &outsym, (bfd_size_type) EXTERNAL_NLIST_SIZE, | |
3970 | (bfd_size_type) 1, output_bfd) != EXTERNAL_NLIST_SIZE) | |
3971 | { | |
3972 | /* FIXME: No way to handle errors. */ | |
3973 | abort (); | |
3974 | } | |
3975 | ||
3976 | finfo->symoff += EXTERNAL_NLIST_SIZE; | |
3977 | h->indx = obj_aout_external_sym_count (output_bfd); | |
3978 | ++obj_aout_external_sym_count (output_bfd); | |
3979 | ||
3980 | return true; | |
3981 | } | |
3982 | ||
3983 | /* Link an a.out section into the output file. */ | |
3984 | ||
3985 | static boolean | |
3986 | aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr, | |
3987 | rel_size, symbol_map) | |
3988 | struct aout_final_link_info *finfo; | |
3989 | bfd *input_bfd; | |
3990 | asection *input_section; | |
3991 | file_ptr *reloff_ptr; | |
3992 | bfd_size_type rel_size; | |
3993 | int *symbol_map; | |
3994 | { | |
3995 | bfd_size_type input_size; | |
3996 | bfd_byte *contents; | |
3997 | PTR relocs; | |
3998 | ||
3999 | /* Get the section contents. */ | |
4000 | input_size = bfd_section_size (input_bfd, input_section); | |
4001 | contents = (bfd_byte *) alloca (input_size); | |
728472f1 | 4002 | if (! bfd_get_section_contents (input_bfd, input_section, (PTR) contents, |
4c3721d5 ILT |
4003 | (file_ptr) 0, input_size)) |
4004 | return false; | |
4005 | ||
4006 | /* Read in the relocs. */ | |
4007 | relocs = (PTR) alloca (rel_size); | |
4008 | if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0 | |
4009 | || bfd_read (relocs, 1, rel_size, input_bfd) != rel_size) | |
4010 | return false; | |
4011 | ||
4012 | /* Relocate the section contents. */ | |
4013 | if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE) | |
4014 | { | |
4015 | if (! aout_link_input_section_std (finfo, input_bfd, input_section, | |
4016 | (struct reloc_std_external *) relocs, | |
4017 | rel_size, contents, symbol_map)) | |
4018 | return false; | |
4019 | } | |
4020 | else | |
4021 | { | |
4022 | if (! aout_link_input_section_ext (finfo, input_bfd, input_section, | |
4023 | (struct reloc_ext_external *) relocs, | |
4024 | rel_size, contents, symbol_map)) | |
4025 | return false; | |
4026 | } | |
4027 | ||
4028 | /* Write out the section contents. */ | |
4029 | if (! bfd_set_section_contents (finfo->output_bfd, | |
4030 | input_section->output_section, | |
728472f1 ILT |
4031 | (PTR) contents, |
4032 | input_section->output_offset, | |
4c3721d5 ILT |
4033 | input_size)) |
4034 | return false; | |
4035 | ||
4036 | /* If we are producing relocateable output, the relocs were | |
4037 | modified, and we now write them out. */ | |
4038 | if (finfo->info->relocateable) | |
4039 | { | |
4040 | if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0) | |
4041 | return false; | |
4042 | if (bfd_write (relocs, (bfd_size_type) 1, rel_size, finfo->output_bfd) | |
4043 | != rel_size) | |
4044 | return false; | |
4045 | *reloff_ptr += rel_size; | |
4046 | ||
4047 | /* Assert that the relocs have not run into the symbols, and | |
4048 | that if these are the text relocs they have not run into the | |
4049 | data relocs. */ | |
4050 | BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd) | |
4051 | && (reloff_ptr != &finfo->treloff | |
4052 | || (*reloff_ptr | |
4053 | <= obj_datasec (finfo->output_bfd)->rel_filepos))); | |
4054 | } | |
4055 | ||
4056 | return true; | |
4057 | } | |
4058 | ||
4059 | /* Get the section corresponding to a reloc index. */ | |
4060 | ||
4061 | static INLINE asection * | |
4062 | aout_reloc_index_to_section (abfd, indx) | |
4063 | bfd *abfd; | |
4064 | int indx; | |
4065 | { | |
4066 | switch (indx & N_TYPE) | |
4067 | { | |
4068 | case N_TEXT: | |
4069 | return obj_textsec (abfd); | |
4070 | case N_DATA: | |
4071 | return obj_datasec (abfd); | |
4072 | case N_BSS: | |
4073 | return obj_bsssec (abfd); | |
4074 | case N_ABS: | |
fa2302b8 | 4075 | case N_UNDF: |
4c3721d5 ILT |
4076 | return &bfd_abs_section; |
4077 | default: | |
4078 | abort (); | |
4079 | } | |
4080 | } | |
4081 | ||
4082 | /* Relocate an a.out section using standard a.out relocs. */ | |
4083 | ||
4084 | static boolean | |
4085 | aout_link_input_section_std (finfo, input_bfd, input_section, relocs, | |
4086 | rel_size, contents, symbol_map) | |
4087 | struct aout_final_link_info *finfo; | |
4088 | bfd *input_bfd; | |
4089 | asection *input_section; | |
4090 | struct reloc_std_external *relocs; | |
4091 | bfd_size_type rel_size; | |
4092 | bfd_byte *contents; | |
4093 | int *symbol_map; | |
4094 | { | |
4095 | bfd *output_bfd; | |
4096 | boolean relocateable; | |
4097 | struct external_nlist *syms; | |
4098 | char *strings; | |
4099 | struct aout_link_hash_entry **sym_hashes; | |
4100 | bfd_size_type reloc_count; | |
4101 | register struct reloc_std_external *rel; | |
4102 | struct reloc_std_external *rel_end; | |
4103 | ||
4104 | output_bfd = finfo->output_bfd; | |
4105 | ||
4106 | BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE); | |
4107 | BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p | |
4108 | == output_bfd->xvec->header_byteorder_big_p); | |
4109 | ||
4110 | relocateable = finfo->info->relocateable; | |
4111 | syms = obj_aout_external_syms (input_bfd); | |
4112 | strings = obj_aout_external_strings (input_bfd); | |
4113 | sym_hashes = obj_aout_sym_hashes (input_bfd); | |
4114 | ||
4115 | reloc_count = rel_size / RELOC_STD_SIZE; | |
4116 | rel = relocs; | |
4117 | rel_end = rel + reloc_count; | |
4118 | for (; rel < rel_end; rel++) | |
4119 | { | |
4120 | bfd_vma r_addr; | |
4121 | int r_index; | |
4122 | int r_extern; | |
4123 | int r_pcrel; | |
4124 | int r_baserel; | |
4125 | int r_jmptable; | |
4126 | int r_relative; | |
4127 | int r_length; | |
4128 | int howto_idx; | |
4129 | bfd_vma relocation; | |
4130 | bfd_reloc_status_type r; | |
4131 | ||
4132 | r_addr = GET_SWORD (input_bfd, rel->r_address); | |
4133 | ||
4134 | if (input_bfd->xvec->header_byteorder_big_p) | |
4135 | { | |
4136 | r_index = ((rel->r_index[0] << 16) | |
4137 | | (rel->r_index[1] << 8) | |
4138 | | rel->r_index[2]); | |
4139 | r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG)); | |
4140 | r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG)); | |
4141 | r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG)); | |
4142 | r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG)); | |
4143 | r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG)); | |
4144 | r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG) | |
4145 | >> RELOC_STD_BITS_LENGTH_SH_BIG); | |
4146 | } | |
4147 | else | |
4148 | { | |
4149 | r_index = ((rel->r_index[2] << 16) | |
4150 | | (rel->r_index[1] << 8) | |
4151 | | rel->r_index[0]); | |
4152 | r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE)); | |
4153 | r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE)); | |
4154 | r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE)); | |
4155 | r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE)); | |
4156 | r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE)); | |
4157 | r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE) | |
4158 | >> RELOC_STD_BITS_LENGTH_SH_LITTLE); | |
4159 | } | |
4160 | ||
4161 | howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel; | |
4162 | BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std)); | |
4163 | BFD_ASSERT (r_jmptable == 0); | |
4164 | BFD_ASSERT (r_relative == 0); | |
4165 | ||
4166 | if (relocateable) | |
4167 | { | |
4168 | /* We are generating a relocateable output file, and must | |
4169 | modify the reloc accordingly. */ | |
4170 | if (r_extern) | |
4171 | { | |
4172 | struct aout_link_hash_entry *h; | |
4173 | ||
4174 | /* If we know the symbol this relocation is against, | |
4175 | convert it into a relocation against a section. This | |
4176 | is what the native linker does. */ | |
4177 | h = sym_hashes[r_index]; | |
4178 | if (h != (struct aout_link_hash_entry *) NULL | |
4179 | && h->root.type == bfd_link_hash_defined) | |
4180 | { | |
4181 | asection *output_section; | |
4182 | ||
4183 | /* Change the r_extern value. */ | |
4184 | if (output_bfd->xvec->header_byteorder_big_p) | |
4185 | rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG; | |
4186 | else | |
4187 | rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE; | |
4188 | ||
4189 | /* Compute a new r_index. */ | |
4190 | output_section = h->root.u.def.section->output_section; | |
4191 | if (output_section == obj_textsec (output_bfd)) | |
4192 | r_index = N_TEXT; | |
4193 | else if (output_section == obj_datasec (output_bfd)) | |
4194 | r_index = N_DATA; | |
4195 | else if (output_section == obj_bsssec (output_bfd)) | |
4196 | r_index = N_BSS; | |
4197 | else | |
4198 | r_index = N_ABS; | |
4199 | ||
4200 | /* Add the symbol value and the section VMA to the | |
4201 | addend stored in the contents. */ | |
4202 | relocation = (h->root.u.def.value | |
4203 | + output_section->vma | |
4204 | + h->root.u.def.section->output_offset); | |
4205 | } | |
4206 | else | |
4207 | { | |
4208 | /* We must change r_index according to the symbol | |
4209 | map. */ | |
4210 | r_index = symbol_map[r_index]; | |
4211 | ||
4212 | if (r_index == -1) | |
4213 | { | |
4214 | const char *name; | |
4215 | ||
4216 | name = strings + GET_WORD (input_bfd, | |
4217 | syms[r_index].e_strx); | |
4218 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
4219 | (finfo->info, name, input_bfd, input_section, | |
4220 | r_addr))) | |
4221 | return false; | |
4222 | r_index = 0; | |
4223 | } | |
4224 | ||
4225 | relocation = 0; | |
4226 | } | |
4227 | ||
4228 | /* Write out the new r_index value. */ | |
4229 | if (output_bfd->xvec->header_byteorder_big_p) | |
4230 | { | |
4231 | rel->r_index[0] = r_index >> 16; | |
4232 | rel->r_index[1] = r_index >> 8; | |
4233 | rel->r_index[2] = r_index; | |
4234 | } | |
4235 | else | |
4236 | { | |
4237 | rel->r_index[2] = r_index >> 16; | |
4238 | rel->r_index[1] = r_index >> 8; | |
4239 | rel->r_index[0] = r_index; | |
4240 | } | |
4241 | } | |
4242 | else | |
4243 | { | |
4244 | asection *section; | |
4245 | ||
4246 | /* This is a relocation against a section. We must | |
4247 | adjust by the amount that the section moved. */ | |
4248 | section = aout_reloc_index_to_section (input_bfd, r_index); | |
4249 | relocation = (section->output_section->vma | |
4250 | + section->output_offset | |
4251 | - section->vma); | |
4252 | } | |
4253 | ||
4254 | /* Change the address of the relocation. */ | |
4255 | PUT_WORD (output_bfd, | |
4256 | r_addr + input_section->output_offset, | |
4257 | rel->r_address); | |
4258 | ||
4259 | /* Adjust a PC relative relocation by removing the reference | |
e68de5d5 ILT |
4260 | to the original address in the section and including the |
4261 | reference to the new address. */ | |
4c3721d5 | 4262 | if (r_pcrel) |
e68de5d5 ILT |
4263 | relocation -= (input_section->output_section->vma |
4264 | + input_section->output_offset | |
4265 | - input_section->vma); | |
4c3721d5 ILT |
4266 | |
4267 | if (relocation == 0) | |
4268 | r = bfd_reloc_ok; | |
4269 | else | |
4270 | r = _bfd_relocate_contents (howto_table_std + howto_idx, | |
4271 | input_bfd, relocation, | |
4272 | contents + r_addr); | |
4273 | } | |
4274 | else | |
4275 | { | |
4276 | /* We are generating an executable, and must do a full | |
4277 | relocation. */ | |
4278 | if (r_extern) | |
4279 | { | |
4280 | struct aout_link_hash_entry *h; | |
4281 | ||
4282 | h = sym_hashes[r_index]; | |
4283 | if (h != (struct aout_link_hash_entry *) NULL | |
4284 | && h->root.type == bfd_link_hash_defined) | |
4285 | { | |
4286 | relocation = (h->root.u.def.value | |
4287 | + h->root.u.def.section->output_section->vma | |
4288 | + h->root.u.def.section->output_offset); | |
4289 | } | |
4290 | else | |
4291 | { | |
4292 | const char *name; | |
4293 | ||
4294 | name = strings + GET_WORD (input_bfd, syms[r_index].e_strx); | |
4295 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
4296 | (finfo->info, name, input_bfd, input_section, | |
4297 | r_addr))) | |
4298 | return false; | |
4299 | relocation = 0; | |
4300 | } | |
4301 | } | |
4302 | else | |
4303 | { | |
4304 | asection *section; | |
4305 | ||
4306 | section = aout_reloc_index_to_section (input_bfd, r_index); | |
4307 | relocation = (section->output_section->vma | |
4308 | + section->output_offset | |
4309 | - section->vma); | |
e68de5d5 ILT |
4310 | if (r_pcrel) |
4311 | relocation += input_section->vma; | |
4c3721d5 ILT |
4312 | } |
4313 | ||
4c3721d5 ILT |
4314 | r = _bfd_final_link_relocate (howto_table_std + howto_idx, |
4315 | input_bfd, input_section, | |
4316 | contents, r_addr, relocation, | |
4317 | (bfd_vma) 0); | |
4318 | } | |
4319 | ||
4320 | if (r != bfd_reloc_ok) | |
4321 | { | |
4322 | switch (r) | |
4323 | { | |
4324 | default: | |
4325 | case bfd_reloc_outofrange: | |
4326 | abort (); | |
4327 | case bfd_reloc_overflow: | |
4991ebb9 ILT |
4328 | { |
4329 | const char *name; | |
4330 | ||
4331 | if (r_extern) | |
4332 | name = strings + GET_WORD (input_bfd, | |
4333 | syms[r_index].e_strx); | |
4334 | else | |
4335 | { | |
4336 | asection *s; | |
4337 | ||
4338 | s = aout_reloc_index_to_section (input_bfd, r_index); | |
4339 | name = bfd_section_name (input_bfd, s); | |
4340 | } | |
4341 | if (! ((*finfo->info->callbacks->reloc_overflow) | |
4342 | (finfo->info, name, howto_table_std[howto_idx].name, | |
4343 | (bfd_vma) 0, input_bfd, input_section, r_addr))) | |
4344 | return false; | |
4345 | } | |
4c3721d5 ILT |
4346 | break; |
4347 | } | |
4348 | } | |
4349 | } | |
4350 | ||
4351 | return true; | |
4352 | } | |
4353 | ||
4354 | /* Relocate an a.out section using extended a.out relocs. */ | |
4355 | ||
4356 | static boolean | |
4357 | aout_link_input_section_ext (finfo, input_bfd, input_section, relocs, | |
4358 | rel_size, contents, symbol_map) | |
4359 | struct aout_final_link_info *finfo; | |
4360 | bfd *input_bfd; | |
4361 | asection *input_section; | |
4362 | struct reloc_ext_external *relocs; | |
4363 | bfd_size_type rel_size; | |
4364 | bfd_byte *contents; | |
4365 | int *symbol_map; | |
4366 | { | |
4367 | bfd *output_bfd; | |
4368 | boolean relocateable; | |
4369 | struct external_nlist *syms; | |
4370 | char *strings; | |
4371 | struct aout_link_hash_entry **sym_hashes; | |
4372 | bfd_size_type reloc_count; | |
4373 | register struct reloc_ext_external *rel; | |
4374 | struct reloc_ext_external *rel_end; | |
4375 | ||
4376 | output_bfd = finfo->output_bfd; | |
4377 | ||
4378 | BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE); | |
4379 | BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p | |
4380 | == output_bfd->xvec->header_byteorder_big_p); | |
4381 | ||
4382 | relocateable = finfo->info->relocateable; | |
4383 | syms = obj_aout_external_syms (input_bfd); | |
4384 | strings = obj_aout_external_strings (input_bfd); | |
4385 | sym_hashes = obj_aout_sym_hashes (input_bfd); | |
4386 | ||
4387 | reloc_count = rel_size / RELOC_EXT_SIZE; | |
4388 | rel = relocs; | |
4389 | rel_end = rel + reloc_count; | |
4390 | for (; rel < rel_end; rel++) | |
4391 | { | |
4392 | bfd_vma r_addr; | |
4393 | int r_index; | |
4394 | int r_extern; | |
4395 | int r_type; | |
4396 | bfd_vma r_addend; | |
4397 | bfd_vma relocation; | |
4398 | ||
4399 | r_addr = GET_SWORD (input_bfd, rel->r_address); | |
4400 | ||
4401 | if (input_bfd->xvec->header_byteorder_big_p) | |
4402 | { | |
4403 | r_index = ((rel->r_index[0] << 16) | |
4404 | | (rel->r_index[1] << 8) | |
4405 | | rel->r_index[2]); | |
4406 | r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG)); | |
4407 | r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG) | |
4408 | >> RELOC_EXT_BITS_TYPE_SH_BIG); | |
4409 | } | |
4410 | else | |
4411 | { | |
4412 | r_index = ((rel->r_index[2] << 16) | |
4413 | | (rel->r_index[1] << 8) | |
4414 | | rel->r_index[0]); | |
4415 | r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE)); | |
4416 | r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE) | |
4417 | >> RELOC_EXT_BITS_TYPE_SH_LITTLE); | |
4418 | } | |
4419 | ||
4420 | r_addend = GET_SWORD (input_bfd, rel->r_addend); | |
4421 | ||
e68de5d5 ILT |
4422 | BFD_ASSERT (r_type >= 0 |
4423 | && r_type < TABLE_SIZE (howto_table_ext)); | |
4424 | ||
4c3721d5 ILT |
4425 | if (relocateable) |
4426 | { | |
4427 | /* We are generating a relocateable output file, and must | |
4428 | modify the reloc accordingly. */ | |
4429 | if (r_extern) | |
4430 | { | |
4431 | struct aout_link_hash_entry *h; | |
4432 | ||
4433 | /* If we know the symbol this relocation is against, | |
4434 | convert it into a relocation against a section. This | |
4435 | is what the native linker does. */ | |
4436 | h = sym_hashes[r_index]; | |
4437 | if (h != (struct aout_link_hash_entry *) NULL | |
4438 | && h->root.type == bfd_link_hash_defined) | |
4439 | { | |
4440 | asection *output_section; | |
4441 | ||
4442 | /* Change the r_extern value. */ | |
4443 | if (output_bfd->xvec->header_byteorder_big_p) | |
4444 | rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG; | |
4445 | else | |
4446 | rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE; | |
4447 | ||
4448 | /* Compute a new r_index. */ | |
4449 | output_section = h->root.u.def.section->output_section; | |
4450 | if (output_section == obj_textsec (output_bfd)) | |
4451 | r_index = N_TEXT; | |
4452 | else if (output_section == obj_datasec (output_bfd)) | |
4453 | r_index = N_DATA; | |
4454 | else if (output_section == obj_bsssec (output_bfd)) | |
4455 | r_index = N_BSS; | |
4456 | else | |
4457 | r_index = N_ABS; | |
4458 | ||
4459 | /* Add the symbol value and the section VMA to the | |
4460 | addend. */ | |
4461 | relocation = (h->root.u.def.value | |
4462 | + output_section->vma | |
4463 | + h->root.u.def.section->output_offset); | |
e68de5d5 ILT |
4464 | |
4465 | /* Now RELOCATION is the VMA of the final | |
4466 | destination. If this is a PC relative reloc, | |
4467 | then ADDEND is the negative of the source VMA. | |
4468 | We want to set ADDEND to the difference between | |
4469 | the destination VMA and the source VMA, which | |
4470 | means we must adjust RELOCATION by the change in | |
4471 | the source VMA. This is done below. */ | |
4c3721d5 ILT |
4472 | } |
4473 | else | |
4474 | { | |
4475 | /* We must change r_index according to the symbol | |
4476 | map. */ | |
4477 | r_index = symbol_map[r_index]; | |
4478 | ||
4479 | if (r_index == -1) | |
4480 | { | |
4481 | const char *name; | |
4482 | ||
4483 | name = (strings | |
4484 | + GET_WORD (input_bfd, syms[r_index].e_strx)); | |
4485 | if (! ((*finfo->info->callbacks->unattached_reloc) | |
4486 | (finfo->info, name, input_bfd, input_section, | |
4487 | r_addr))) | |
4488 | return false; | |
4489 | r_index = 0; | |
4490 | } | |
4491 | ||
4492 | relocation = 0; | |
e68de5d5 ILT |
4493 | |
4494 | /* If this is a PC relative reloc, then the addend | |
4495 | is the negative of the source VMA. We must | |
4496 | adjust it by the change in the source VMA. This | |
4497 | is done below. */ | |
4c3721d5 ILT |
4498 | } |
4499 | ||
4500 | /* Write out the new r_index value. */ | |
4501 | if (output_bfd->xvec->header_byteorder_big_p) | |
4502 | { | |
4503 | rel->r_index[0] = r_index >> 16; | |
4504 | rel->r_index[1] = r_index >> 8; | |
4505 | rel->r_index[2] = r_index; | |
4506 | } | |
4507 | else | |
4508 | { | |
4509 | rel->r_index[2] = r_index >> 16; | |
4510 | rel->r_index[1] = r_index >> 8; | |
4511 | rel->r_index[0] = r_index; | |
4512 | } | |
4513 | } | |
4514 | else | |
4515 | { | |
4516 | asection *section; | |
4517 | ||
4518 | /* This is a relocation against a section. We must | |
4519 | adjust by the amount that the section moved. */ | |
4520 | section = aout_reloc_index_to_section (input_bfd, r_index); | |
4521 | relocation = (section->output_section->vma | |
4522 | + section->output_offset | |
4523 | - section->vma); | |
4c3721d5 | 4524 | |
e68de5d5 ILT |
4525 | /* If this is a PC relative reloc, then the addend is |
4526 | the difference in VMA between the destination and the | |
4527 | source. We have just adjusted for the change in VMA | |
4528 | of the destination, so we must also adjust by the | |
4529 | change in VMA of the source. This is done below. */ | |
4c3721d5 ILT |
4530 | } |
4531 | ||
e68de5d5 ILT |
4532 | /* As described above, we must always adjust a PC relative |
4533 | reloc by the change in VMA of the source. */ | |
4534 | if (howto_table_ext[r_type].pc_relative) | |
4535 | relocation -= (input_section->output_section->vma | |
4536 | + input_section->output_offset | |
4537 | - input_section->vma); | |
4538 | ||
4c3721d5 ILT |
4539 | /* Change the addend if necessary. */ |
4540 | if (relocation != 0) | |
4541 | PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend); | |
4542 | ||
4543 | /* Change the address of the relocation. */ | |
4544 | PUT_WORD (output_bfd, | |
4545 | r_addr + input_section->output_offset, | |
4546 | rel->r_address); | |
4547 | } | |
4548 | else | |
4549 | { | |
4550 | bfd_reloc_status_type r; | |
4551 | ||
4552 | /* We are generating an executable, and must do a full | |
4553 | relocation. */ | |
4554 | if (r_extern) | |
4555 | { | |
4556 | struct aout_link_hash_entry *h; | |
4557 | ||
4558 | h = sym_hashes[r_index]; | |
4559 | if (h != (struct aout_link_hash_entry *) NULL | |
4560 | && h->root.type == bfd_link_hash_defined) | |
4561 | { | |
4562 | relocation = (h->root.u.def.value | |
4563 | + h->root.u.def.section->output_section->vma | |
4564 | + h->root.u.def.section->output_offset); | |
4565 | } | |
4566 | else | |
4567 | { | |
4568 | const char *name; | |
4569 | ||
4570 | name = strings + GET_WORD (input_bfd, syms[r_index].e_strx); | |
4571 | if (! ((*finfo->info->callbacks->undefined_symbol) | |
4572 | (finfo->info, name, input_bfd, input_section, | |
4573 | r_addr))) | |
4574 | return false; | |
4575 | relocation = 0; | |
4576 | } | |
4577 | } | |
4578 | else | |
4579 | { | |
4580 | asection *section; | |
4581 | ||
4582 | section = aout_reloc_index_to_section (input_bfd, r_index); | |
e68de5d5 ILT |
4583 | |
4584 | /* If this is a PC relative reloc, then R_ADDEND is the | |
4585 | difference between the two vmas, or | |
4586 | old_dest_sec + old_dest_off - (old_src_sec + old_src_off) | |
4587 | where | |
4588 | old_dest_sec == section->vma | |
4589 | and | |
4590 | old_src_sec == input_section->vma | |
4591 | and | |
4592 | old_src_off == r_addr | |
4593 | ||
4594 | _bfd_final_link_relocate expects RELOCATION + | |
4595 | R_ADDEND to be the VMA of the destination minus | |
4596 | r_addr (the minus r_addr is because this relocation | |
4597 | is not pcrel_offset, which is a bit confusing and | |
4598 | should, perhaps, be changed), or | |
4599 | new_dest_sec | |
4600 | where | |
4601 | new_dest_sec == output_section->vma + output_offset | |
4602 | We arrange for this to happen by setting RELOCATION to | |
4603 | new_dest_sec + old_src_sec - old_dest_sec | |
4604 | ||
4605 | If this is not a PC relative reloc, then R_ADDEND is | |
4606 | simply the VMA of the destination, so we set | |
4607 | RELOCATION to the change in the destination VMA, or | |
4608 | new_dest_sec - old_dest_sec | |
4609 | */ | |
4c3721d5 ILT |
4610 | relocation = (section->output_section->vma |
4611 | + section->output_offset | |
4612 | - section->vma); | |
e68de5d5 ILT |
4613 | if (howto_table_ext[r_type].pc_relative) |
4614 | relocation += input_section->vma; | |
4c3721d5 ILT |
4615 | } |
4616 | ||
4c3721d5 ILT |
4617 | r = _bfd_final_link_relocate (howto_table_ext + r_type, |
4618 | input_bfd, input_section, | |
4619 | contents, r_addr, relocation, | |
4620 | r_addend); | |
4621 | if (r != bfd_reloc_ok) | |
4622 | { | |
4623 | switch (r) | |
4624 | { | |
4625 | default: | |
4626 | case bfd_reloc_outofrange: | |
4627 | abort (); | |
4628 | case bfd_reloc_overflow: | |
4991ebb9 ILT |
4629 | { |
4630 | const char *name; | |
4631 | ||
4632 | if (r_extern) | |
4633 | name = strings + GET_WORD (input_bfd, | |
4634 | syms[r_index].e_strx); | |
4635 | else | |
4636 | { | |
4637 | asection *s; | |
4638 | ||
4639 | s = aout_reloc_index_to_section (input_bfd, r_index); | |
4640 | name = bfd_section_name (input_bfd, s); | |
4641 | } | |
4642 | if (! ((*finfo->info->callbacks->reloc_overflow) | |
4643 | (finfo->info, name, howto_table_ext[r_type].name, | |
4644 | r_addend, input_bfd, input_section, r_addr))) | |
4645 | return false; | |
4646 | } | |
4c3721d5 ILT |
4647 | break; |
4648 | } | |
4649 | } | |
4650 | } | |
4651 | } | |
4652 | ||
4653 | return true; | |
4654 | } |