1*3d8817e4Smiod /* BFD back-end for linux flavored m68k a.out binaries.
2*3d8817e4Smiod Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002,
3*3d8817e4Smiod 2003, 2004, 2006 Free Software Foundation, Inc.
4*3d8817e4Smiod
5*3d8817e4Smiod This file is part of BFD, the Binary File Descriptor library.
6*3d8817e4Smiod
7*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod (at your option) any later version.
11*3d8817e4Smiod
12*3d8817e4Smiod This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*3d8817e4Smiod GNU General Public License for more details.
16*3d8817e4Smiod
17*3d8817e4Smiod You should have received a copy of the GNU General Public License
18*3d8817e4Smiod along with this program; if not, write to the Free Software
19*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20*3d8817e4Smiod
21*3d8817e4Smiod #define TARGET_PAGE_SIZE 4096
22*3d8817e4Smiod #define ZMAGIC_DISK_BLOCK_SIZE 1024
23*3d8817e4Smiod #define SEGMENT_SIZE TARGET_PAGE_SIZE
24*3d8817e4Smiod #define TEXT_START_ADDR 0x0
25*3d8817e4Smiod #define N_SHARED_LIB(x) 0
26*3d8817e4Smiod
27*3d8817e4Smiod #define MACHTYPE_OK(mtype) ((mtype) == M_68020 || (mtype) == M_UNKNOWN)
28*3d8817e4Smiod
29*3d8817e4Smiod #include "bfd.h"
30*3d8817e4Smiod #include "sysdep.h"
31*3d8817e4Smiod #include "libbfd.h"
32*3d8817e4Smiod #include "aout/aout64.h"
33*3d8817e4Smiod #include "aout/stab_gnu.h"
34*3d8817e4Smiod #include "aout/ar.h"
35*3d8817e4Smiod #include "libaout.h" /* BFD a.out internal data structures */
36*3d8817e4Smiod
37*3d8817e4Smiod #define TARGET_IS_BIG_ENDIAN_P
38*3d8817e4Smiod #define DEFAULT_ARCH bfd_arch_m68k
39*3d8817e4Smiod
40*3d8817e4Smiod /* Do not "beautify" the CONCAT* macro args. Traditional C will not
41*3d8817e4Smiod remove whitespace added here, and thus will fail to concatenate
42*3d8817e4Smiod the tokens. */
43*3d8817e4Smiod #define MY(OP) CONCAT2 (m68klinux_,OP)
44*3d8817e4Smiod #define TARGETNAME "a.out-m68k-linux"
45*3d8817e4Smiod
46*3d8817e4Smiod extern const bfd_target MY(vec);
47*3d8817e4Smiod
48*3d8817e4Smiod /* We always generate QMAGIC files in preference to ZMAGIC files. It
49*3d8817e4Smiod would be possible to make this a linker option, if that ever
50*3d8817e4Smiod becomes important. */
51*3d8817e4Smiod
52*3d8817e4Smiod static void MY_final_link_callback
53*3d8817e4Smiod PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
54*3d8817e4Smiod static bfd_boolean m68klinux_bfd_final_link
55*3d8817e4Smiod PARAMS ((bfd *, struct bfd_link_info *));
56*3d8817e4Smiod static bfd_boolean m68klinux_write_object_contents PARAMS ((bfd *));
57*3d8817e4Smiod
58*3d8817e4Smiod static bfd_boolean
m68klinux_bfd_final_link(abfd,info)59*3d8817e4Smiod m68klinux_bfd_final_link (abfd, info)
60*3d8817e4Smiod bfd *abfd;
61*3d8817e4Smiod struct bfd_link_info *info;
62*3d8817e4Smiod {
63*3d8817e4Smiod obj_aout_subformat (abfd) = q_magic_format;
64*3d8817e4Smiod return NAME(aout,final_link) (abfd, info, MY_final_link_callback);
65*3d8817e4Smiod }
66*3d8817e4Smiod
67*3d8817e4Smiod #define MY_bfd_final_link m68klinux_bfd_final_link
68*3d8817e4Smiod
69*3d8817e4Smiod /* Set the machine type correctly. */
70*3d8817e4Smiod
71*3d8817e4Smiod static bfd_boolean
m68klinux_write_object_contents(abfd)72*3d8817e4Smiod m68klinux_write_object_contents (abfd)
73*3d8817e4Smiod bfd *abfd;
74*3d8817e4Smiod {
75*3d8817e4Smiod struct external_exec exec_bytes;
76*3d8817e4Smiod struct internal_exec *execp = exec_hdr (abfd);
77*3d8817e4Smiod
78*3d8817e4Smiod N_SET_MACHTYPE (*execp, M_68020);
79*3d8817e4Smiod
80*3d8817e4Smiod obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
81*3d8817e4Smiod
82*3d8817e4Smiod WRITE_HEADERS(abfd, execp);
83*3d8817e4Smiod
84*3d8817e4Smiod return TRUE;
85*3d8817e4Smiod }
86*3d8817e4Smiod
87*3d8817e4Smiod #define MY_write_object_contents m68klinux_write_object_contents
88*3d8817e4Smiod
89*3d8817e4Smiod /* Code to link against Linux a.out shared libraries. */
90*3d8817e4Smiod
91*3d8817e4Smiod /* See if a symbol name is a reference to the global offset table. */
92*3d8817e4Smiod
93*3d8817e4Smiod #ifndef GOT_REF_PREFIX
94*3d8817e4Smiod #define GOT_REF_PREFIX "__GOT_"
95*3d8817e4Smiod #endif
96*3d8817e4Smiod
97*3d8817e4Smiod #define IS_GOT_SYM(name) \
98*3d8817e4Smiod (strncmp (name, GOT_REF_PREFIX, sizeof GOT_REF_PREFIX - 1) == 0)
99*3d8817e4Smiod
100*3d8817e4Smiod /* See if a symbol name is a reference to the procedure linkage table. */
101*3d8817e4Smiod
102*3d8817e4Smiod #ifndef PLT_REF_PREFIX
103*3d8817e4Smiod #define PLT_REF_PREFIX "__PLT_"
104*3d8817e4Smiod #endif
105*3d8817e4Smiod
106*3d8817e4Smiod #define IS_PLT_SYM(name) \
107*3d8817e4Smiod (strncmp (name, PLT_REF_PREFIX, sizeof PLT_REF_PREFIX - 1) == 0)
108*3d8817e4Smiod
109*3d8817e4Smiod /* This string is used to generate specialized error messages. */
110*3d8817e4Smiod
111*3d8817e4Smiod #ifndef NEEDS_SHRLIB
112*3d8817e4Smiod #define NEEDS_SHRLIB "__NEEDS_SHRLIB_"
113*3d8817e4Smiod #endif
114*3d8817e4Smiod
115*3d8817e4Smiod /* This special symbol is a set vector that contains a list of
116*3d8817e4Smiod pointers to fixup tables. It will be present in any dynamically
117*3d8817e4Smiod linked file. The linker generated fixup table should also be added
118*3d8817e4Smiod to the list, and it should always appear in the second slot (the
119*3d8817e4Smiod first one is a dummy with a magic number that is defined in
120*3d8817e4Smiod crt0.o). */
121*3d8817e4Smiod
122*3d8817e4Smiod #ifndef SHARABLE_CONFLICTS
123*3d8817e4Smiod #define SHARABLE_CONFLICTS "__SHARABLE_CONFLICTS__"
124*3d8817e4Smiod #endif
125*3d8817e4Smiod
126*3d8817e4Smiod /* We keep a list of fixups. The terminology is a bit strange, but
127*3d8817e4Smiod each fixup contains two 32 bit numbers. A regular fixup contains
128*3d8817e4Smiod an address and a pointer, and at runtime we should store the
129*3d8817e4Smiod address at the location pointed to by the pointer. A builtin fixup
130*3d8817e4Smiod contains two pointers, and we should read the address using one
131*3d8817e4Smiod pointer and store it at the location pointed to by the other
132*3d8817e4Smiod pointer. Builtin fixups come into play when we have duplicate
133*3d8817e4Smiod __GOT__ symbols for the same variable. The builtin fixup will copy
134*3d8817e4Smiod the GOT pointer from one over into the other. */
135*3d8817e4Smiod
136*3d8817e4Smiod struct fixup
137*3d8817e4Smiod {
138*3d8817e4Smiod struct fixup *next;
139*3d8817e4Smiod struct linux_link_hash_entry *h;
140*3d8817e4Smiod bfd_vma value;
141*3d8817e4Smiod
142*3d8817e4Smiod /* Nonzero if this is a jump instruction that needs to be fixed,
143*3d8817e4Smiod zero if this is just a pointer */
144*3d8817e4Smiod char jump;
145*3d8817e4Smiod
146*3d8817e4Smiod char builtin;
147*3d8817e4Smiod };
148*3d8817e4Smiod
149*3d8817e4Smiod /* We don't need a special hash table entry structure, but we do need
150*3d8817e4Smiod to keep some information between linker passes, so we use a special
151*3d8817e4Smiod hash table. */
152*3d8817e4Smiod
153*3d8817e4Smiod struct linux_link_hash_entry
154*3d8817e4Smiod {
155*3d8817e4Smiod struct aout_link_hash_entry root;
156*3d8817e4Smiod };
157*3d8817e4Smiod
158*3d8817e4Smiod struct linux_link_hash_table
159*3d8817e4Smiod {
160*3d8817e4Smiod struct aout_link_hash_table root;
161*3d8817e4Smiod
162*3d8817e4Smiod /* First dynamic object found in link. */
163*3d8817e4Smiod bfd *dynobj;
164*3d8817e4Smiod
165*3d8817e4Smiod /* Number of fixups. */
166*3d8817e4Smiod size_t fixup_count;
167*3d8817e4Smiod
168*3d8817e4Smiod /* Number of builtin fixups. */
169*3d8817e4Smiod size_t local_builtins;
170*3d8817e4Smiod
171*3d8817e4Smiod /* List of fixups. */
172*3d8817e4Smiod struct fixup *fixup_list;
173*3d8817e4Smiod };
174*3d8817e4Smiod
175*3d8817e4Smiod static struct bfd_hash_entry *linux_link_hash_newfunc
176*3d8817e4Smiod PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
177*3d8817e4Smiod static struct bfd_link_hash_table *linux_link_hash_table_create
178*3d8817e4Smiod PARAMS ((bfd *));
179*3d8817e4Smiod static struct fixup *new_fixup
180*3d8817e4Smiod PARAMS ((struct bfd_link_info *, struct linux_link_hash_entry *,
181*3d8817e4Smiod bfd_vma, int));
182*3d8817e4Smiod static bfd_boolean linux_link_create_dynamic_sections
183*3d8817e4Smiod PARAMS ((bfd *, struct bfd_link_info *));
184*3d8817e4Smiod static bfd_boolean linux_add_one_symbol
185*3d8817e4Smiod PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
186*3d8817e4Smiod bfd_vma, const char *, bfd_boolean, bfd_boolean,
187*3d8817e4Smiod struct bfd_link_hash_entry **));
188*3d8817e4Smiod static bfd_boolean linux_tally_symbols
189*3d8817e4Smiod PARAMS ((struct linux_link_hash_entry *, PTR));
190*3d8817e4Smiod static bfd_boolean linux_finish_dynamic_link
191*3d8817e4Smiod PARAMS ((bfd *, struct bfd_link_info *));
192*3d8817e4Smiod
193*3d8817e4Smiod /* Routine to create an entry in an Linux link hash table. */
194*3d8817e4Smiod
195*3d8817e4Smiod static struct bfd_hash_entry *
linux_link_hash_newfunc(entry,table,string)196*3d8817e4Smiod linux_link_hash_newfunc (entry, table, string)
197*3d8817e4Smiod struct bfd_hash_entry *entry;
198*3d8817e4Smiod struct bfd_hash_table *table;
199*3d8817e4Smiod const char *string;
200*3d8817e4Smiod {
201*3d8817e4Smiod struct linux_link_hash_entry *ret = (struct linux_link_hash_entry *) entry;
202*3d8817e4Smiod
203*3d8817e4Smiod /* Allocate the structure if it has not already been allocated by a
204*3d8817e4Smiod subclass. */
205*3d8817e4Smiod if (ret == (struct linux_link_hash_entry *) NULL)
206*3d8817e4Smiod ret = ((struct linux_link_hash_entry *)
207*3d8817e4Smiod bfd_hash_allocate (table, sizeof (struct linux_link_hash_entry)));
208*3d8817e4Smiod if (ret == NULL)
209*3d8817e4Smiod return (struct bfd_hash_entry *) ret;
210*3d8817e4Smiod
211*3d8817e4Smiod /* Call the allocation method of the superclass. */
212*3d8817e4Smiod ret = ((struct linux_link_hash_entry *)
213*3d8817e4Smiod NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
214*3d8817e4Smiod table, string));
215*3d8817e4Smiod if (ret != NULL)
216*3d8817e4Smiod {
217*3d8817e4Smiod /* Set local fields; there aren't any. */
218*3d8817e4Smiod }
219*3d8817e4Smiod
220*3d8817e4Smiod return (struct bfd_hash_entry *) ret;
221*3d8817e4Smiod }
222*3d8817e4Smiod
223*3d8817e4Smiod /* Create a Linux link hash table. */
224*3d8817e4Smiod
225*3d8817e4Smiod static struct bfd_link_hash_table *
linux_link_hash_table_create(abfd)226*3d8817e4Smiod linux_link_hash_table_create (abfd)
227*3d8817e4Smiod bfd *abfd;
228*3d8817e4Smiod {
229*3d8817e4Smiod struct linux_link_hash_table *ret;
230*3d8817e4Smiod bfd_size_type amt = sizeof (struct linux_link_hash_table);
231*3d8817e4Smiod
232*3d8817e4Smiod ret = (struct linux_link_hash_table *) bfd_malloc (amt);
233*3d8817e4Smiod if (ret == (struct linux_link_hash_table *) NULL)
234*3d8817e4Smiod {
235*3d8817e4Smiod bfd_set_error (bfd_error_no_memory);
236*3d8817e4Smiod return (struct bfd_link_hash_table *) NULL;
237*3d8817e4Smiod }
238*3d8817e4Smiod if (!NAME(aout,link_hash_table_init) (&ret->root, abfd,
239*3d8817e4Smiod linux_link_hash_newfunc,
240*3d8817e4Smiod sizeof (struct linux_link_hash_entry)))
241*3d8817e4Smiod {
242*3d8817e4Smiod free (ret);
243*3d8817e4Smiod return (struct bfd_link_hash_table *) NULL;
244*3d8817e4Smiod }
245*3d8817e4Smiod
246*3d8817e4Smiod ret->dynobj = NULL;
247*3d8817e4Smiod ret->fixup_count = 0;
248*3d8817e4Smiod ret->local_builtins = 0;
249*3d8817e4Smiod ret->fixup_list = NULL;
250*3d8817e4Smiod
251*3d8817e4Smiod return &ret->root.root;
252*3d8817e4Smiod }
253*3d8817e4Smiod
254*3d8817e4Smiod /* Look up an entry in a Linux link hash table. */
255*3d8817e4Smiod
256*3d8817e4Smiod #define linux_link_hash_lookup(table, string, create, copy, follow) \
257*3d8817e4Smiod ((struct linux_link_hash_entry *) \
258*3d8817e4Smiod aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
259*3d8817e4Smiod (follow)))
260*3d8817e4Smiod
261*3d8817e4Smiod /* Traverse a Linux link hash table. */
262*3d8817e4Smiod
263*3d8817e4Smiod #define linux_link_hash_traverse(table, func, info) \
264*3d8817e4Smiod (aout_link_hash_traverse \
265*3d8817e4Smiod (&(table)->root, \
266*3d8817e4Smiod (bfd_boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
267*3d8817e4Smiod (info)))
268*3d8817e4Smiod
269*3d8817e4Smiod /* Get the Linux link hash table from the info structure. This is
270*3d8817e4Smiod just a cast. */
271*3d8817e4Smiod
272*3d8817e4Smiod #define linux_hash_table(p) ((struct linux_link_hash_table *) ((p)->hash))
273*3d8817e4Smiod
274*3d8817e4Smiod /* Store the information for a new fixup. */
275*3d8817e4Smiod
276*3d8817e4Smiod static struct fixup *
new_fixup(info,h,value,builtin)277*3d8817e4Smiod new_fixup (info, h, value, builtin)
278*3d8817e4Smiod struct bfd_link_info *info;
279*3d8817e4Smiod struct linux_link_hash_entry *h;
280*3d8817e4Smiod bfd_vma value;
281*3d8817e4Smiod int builtin;
282*3d8817e4Smiod {
283*3d8817e4Smiod struct fixup *f;
284*3d8817e4Smiod
285*3d8817e4Smiod f = (struct fixup *) bfd_hash_allocate (&info->hash->table,
286*3d8817e4Smiod sizeof (struct fixup));
287*3d8817e4Smiod if (f == NULL)
288*3d8817e4Smiod return f;
289*3d8817e4Smiod f->next = linux_hash_table (info)->fixup_list;
290*3d8817e4Smiod linux_hash_table (info)->fixup_list = f;
291*3d8817e4Smiod f->h = h;
292*3d8817e4Smiod f->value = value;
293*3d8817e4Smiod f->builtin = builtin;
294*3d8817e4Smiod f->jump = 0;
295*3d8817e4Smiod ++linux_hash_table (info)->fixup_count;
296*3d8817e4Smiod return f;
297*3d8817e4Smiod }
298*3d8817e4Smiod
299*3d8817e4Smiod /* We come here once we realize that we are going to link to a shared
300*3d8817e4Smiod library. We need to create a special section that contains the
301*3d8817e4Smiod fixup table, and we ultimately need to add a pointer to this into
302*3d8817e4Smiod the set vector for SHARABLE_CONFLICTS. At this point we do not
303*3d8817e4Smiod know the size of the section, but that's OK - we just need to
304*3d8817e4Smiod create it for now. */
305*3d8817e4Smiod
306*3d8817e4Smiod static bfd_boolean
linux_link_create_dynamic_sections(abfd,info)307*3d8817e4Smiod linux_link_create_dynamic_sections (abfd, info)
308*3d8817e4Smiod bfd *abfd;
309*3d8817e4Smiod struct bfd_link_info *info ATTRIBUTE_UNUSED;
310*3d8817e4Smiod {
311*3d8817e4Smiod flagword flags;
312*3d8817e4Smiod register asection *s;
313*3d8817e4Smiod
314*3d8817e4Smiod /* Note that we set the SEC_IN_MEMORY flag. */
315*3d8817e4Smiod flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
316*3d8817e4Smiod
317*3d8817e4Smiod /* We choose to use the name ".linux-dynamic" for the fixup table.
318*3d8817e4Smiod Why not? */
319*3d8817e4Smiod s = bfd_make_section (abfd, ".linux-dynamic");
320*3d8817e4Smiod if (s == NULL
321*3d8817e4Smiod || ! bfd_set_section_flags (abfd, s, flags)
322*3d8817e4Smiod || ! bfd_set_section_alignment (abfd, s, 2))
323*3d8817e4Smiod return FALSE;
324*3d8817e4Smiod s->size = 0;
325*3d8817e4Smiod s->contents = 0;
326*3d8817e4Smiod
327*3d8817e4Smiod return TRUE;
328*3d8817e4Smiod }
329*3d8817e4Smiod
330*3d8817e4Smiod /* Function to add a single symbol to the linker hash table. This is
331*3d8817e4Smiod a wrapper around _bfd_generic_link_add_one_symbol which handles the
332*3d8817e4Smiod tweaking needed for dynamic linking support. */
333*3d8817e4Smiod
334*3d8817e4Smiod static bfd_boolean
linux_add_one_symbol(info,abfd,name,flags,section,value,string,copy,collect,hashp)335*3d8817e4Smiod linux_add_one_symbol (info, abfd, name, flags, section, value, string,
336*3d8817e4Smiod copy, collect, hashp)
337*3d8817e4Smiod struct bfd_link_info *info;
338*3d8817e4Smiod bfd *abfd;
339*3d8817e4Smiod const char *name;
340*3d8817e4Smiod flagword flags;
341*3d8817e4Smiod asection *section;
342*3d8817e4Smiod bfd_vma value;
343*3d8817e4Smiod const char *string;
344*3d8817e4Smiod bfd_boolean copy;
345*3d8817e4Smiod bfd_boolean collect;
346*3d8817e4Smiod struct bfd_link_hash_entry **hashp;
347*3d8817e4Smiod {
348*3d8817e4Smiod struct linux_link_hash_entry *h;
349*3d8817e4Smiod bfd_boolean insert;
350*3d8817e4Smiod
351*3d8817e4Smiod /* Look up and see if we already have this symbol in the hash table.
352*3d8817e4Smiod If we do, and the defining entry is from a shared library, we
353*3d8817e4Smiod need to create the dynamic sections.
354*3d8817e4Smiod
355*3d8817e4Smiod FIXME: What if abfd->xvec != info->hash->creator? We may want to
356*3d8817e4Smiod be able to link Linux a.out and ELF objects together, but serious
357*3d8817e4Smiod confusion is possible. */
358*3d8817e4Smiod
359*3d8817e4Smiod insert = FALSE;
360*3d8817e4Smiod
361*3d8817e4Smiod if (! info->relocatable
362*3d8817e4Smiod && linux_hash_table (info)->dynobj == NULL
363*3d8817e4Smiod && strcmp (name, SHARABLE_CONFLICTS) == 0
364*3d8817e4Smiod && (flags & BSF_CONSTRUCTOR) != 0
365*3d8817e4Smiod && abfd->xvec == info->hash->creator)
366*3d8817e4Smiod {
367*3d8817e4Smiod if (! linux_link_create_dynamic_sections (abfd, info))
368*3d8817e4Smiod return FALSE;
369*3d8817e4Smiod linux_hash_table (info)->dynobj = abfd;
370*3d8817e4Smiod insert = TRUE;
371*3d8817e4Smiod }
372*3d8817e4Smiod
373*3d8817e4Smiod if (bfd_is_abs_section (section)
374*3d8817e4Smiod && abfd->xvec == info->hash->creator)
375*3d8817e4Smiod {
376*3d8817e4Smiod h = linux_link_hash_lookup (linux_hash_table (info), name, FALSE,
377*3d8817e4Smiod FALSE, FALSE);
378*3d8817e4Smiod if (h != NULL
379*3d8817e4Smiod && (h->root.root.type == bfd_link_hash_defined
380*3d8817e4Smiod || h->root.root.type == bfd_link_hash_defweak))
381*3d8817e4Smiod {
382*3d8817e4Smiod struct fixup *f;
383*3d8817e4Smiod
384*3d8817e4Smiod if (hashp != NULL)
385*3d8817e4Smiod *hashp = (struct bfd_link_hash_entry *) h;
386*3d8817e4Smiod
387*3d8817e4Smiod f = new_fixup (info, h, value, ! IS_PLT_SYM (name));
388*3d8817e4Smiod if (f == NULL)
389*3d8817e4Smiod return FALSE;
390*3d8817e4Smiod f->jump = IS_PLT_SYM (name);
391*3d8817e4Smiod
392*3d8817e4Smiod return TRUE;
393*3d8817e4Smiod }
394*3d8817e4Smiod }
395*3d8817e4Smiod
396*3d8817e4Smiod /* Do the usual procedure for adding a symbol. */
397*3d8817e4Smiod if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
398*3d8817e4Smiod value, string, copy, collect,
399*3d8817e4Smiod hashp))
400*3d8817e4Smiod return FALSE;
401*3d8817e4Smiod
402*3d8817e4Smiod /* Insert a pointer to our table in the set vector. The dynamic
403*3d8817e4Smiod linker requires this information */
404*3d8817e4Smiod if (insert)
405*3d8817e4Smiod {
406*3d8817e4Smiod asection *s;
407*3d8817e4Smiod
408*3d8817e4Smiod /* Here we do our special thing to add the pointer to the
409*3d8817e4Smiod dynamic section in the SHARABLE_CONFLICTS set vector. */
410*3d8817e4Smiod s = bfd_get_section_by_name (linux_hash_table (info)->dynobj,
411*3d8817e4Smiod ".linux-dynamic");
412*3d8817e4Smiod BFD_ASSERT (s != NULL);
413*3d8817e4Smiod
414*3d8817e4Smiod if (! (_bfd_generic_link_add_one_symbol
415*3d8817e4Smiod (info, linux_hash_table (info)->dynobj, SHARABLE_CONFLICTS,
416*3d8817e4Smiod BSF_GLOBAL | BSF_CONSTRUCTOR, s, (bfd_vma) 0, NULL,
417*3d8817e4Smiod FALSE, FALSE, NULL)))
418*3d8817e4Smiod return FALSE;
419*3d8817e4Smiod }
420*3d8817e4Smiod
421*3d8817e4Smiod return TRUE;
422*3d8817e4Smiod }
423*3d8817e4Smiod
424*3d8817e4Smiod /* We will crawl the hash table and come here for every global symbol.
425*3d8817e4Smiod We will examine each entry and see if there are indications that we
426*3d8817e4Smiod need to add a fixup. There are two possible cases - one is where
427*3d8817e4Smiod you have duplicate definitions of PLT or GOT symbols - these will
428*3d8817e4Smiod have already been caught and added as "builtin" fixups. If we find
429*3d8817e4Smiod that the corresponding non PLT/GOT symbol is also present, we
430*3d8817e4Smiod convert it to a regular fixup instead.
431*3d8817e4Smiod
432*3d8817e4Smiod This function is called via linux_link_hash_traverse. */
433*3d8817e4Smiod
434*3d8817e4Smiod static bfd_boolean
linux_tally_symbols(h,data)435*3d8817e4Smiod linux_tally_symbols (h, data)
436*3d8817e4Smiod struct linux_link_hash_entry *h;
437*3d8817e4Smiod PTR data;
438*3d8817e4Smiod {
439*3d8817e4Smiod struct bfd_link_info *info = (struct bfd_link_info *) data;
440*3d8817e4Smiod struct fixup *f, *f1;
441*3d8817e4Smiod int is_plt;
442*3d8817e4Smiod struct linux_link_hash_entry *h1, *h2;
443*3d8817e4Smiod bfd_boolean exists;
444*3d8817e4Smiod
445*3d8817e4Smiod if (h->root.root.type == bfd_link_hash_warning)
446*3d8817e4Smiod h = (struct linux_link_hash_entry *) h->root.root.u.i.link;
447*3d8817e4Smiod
448*3d8817e4Smiod if (h->root.root.type == bfd_link_hash_undefined
449*3d8817e4Smiod && strncmp (h->root.root.root.string, NEEDS_SHRLIB,
450*3d8817e4Smiod sizeof NEEDS_SHRLIB - 1) == 0)
451*3d8817e4Smiod {
452*3d8817e4Smiod const char *name;
453*3d8817e4Smiod char *p;
454*3d8817e4Smiod char *alloc = NULL;
455*3d8817e4Smiod
456*3d8817e4Smiod name = h->root.root.root.string + sizeof NEEDS_SHRLIB - 1;
457*3d8817e4Smiod p = strrchr (name, '_');
458*3d8817e4Smiod if (p != NULL)
459*3d8817e4Smiod alloc = (char *) bfd_malloc ((bfd_size_type) strlen (name) + 1);
460*3d8817e4Smiod
461*3d8817e4Smiod if (p == NULL || alloc == NULL)
462*3d8817e4Smiod (*_bfd_error_handler) (_("Output file requires shared library `%s'\n"),
463*3d8817e4Smiod name);
464*3d8817e4Smiod else
465*3d8817e4Smiod {
466*3d8817e4Smiod strcpy (alloc, name);
467*3d8817e4Smiod p = strrchr (alloc, '_');
468*3d8817e4Smiod *p++ = '\0';
469*3d8817e4Smiod (*_bfd_error_handler)
470*3d8817e4Smiod (_("Output file requires shared library `%s.so.%s'\n"),
471*3d8817e4Smiod alloc, p);
472*3d8817e4Smiod free (alloc);
473*3d8817e4Smiod }
474*3d8817e4Smiod
475*3d8817e4Smiod abort ();
476*3d8817e4Smiod }
477*3d8817e4Smiod
478*3d8817e4Smiod /* If this symbol is not a PLT/GOT, we do not even need to look at it */
479*3d8817e4Smiod is_plt = IS_PLT_SYM (h->root.root.root.string);
480*3d8817e4Smiod
481*3d8817e4Smiod if (is_plt || IS_GOT_SYM (h->root.root.root.string))
482*3d8817e4Smiod {
483*3d8817e4Smiod /* Look up this symbol twice. Once just as a regular lookup,
484*3d8817e4Smiod and then again following all of the indirect links until we
485*3d8817e4Smiod reach a real symbol. */
486*3d8817e4Smiod h1 = linux_link_hash_lookup (linux_hash_table (info),
487*3d8817e4Smiod (h->root.root.root.string
488*3d8817e4Smiod + sizeof PLT_REF_PREFIX - 1),
489*3d8817e4Smiod FALSE, FALSE, TRUE);
490*3d8817e4Smiod /* h2 does not follow indirect symbols. */
491*3d8817e4Smiod h2 = linux_link_hash_lookup (linux_hash_table (info),
492*3d8817e4Smiod (h->root.root.root.string
493*3d8817e4Smiod + sizeof PLT_REF_PREFIX - 1),
494*3d8817e4Smiod FALSE, FALSE, FALSE);
495*3d8817e4Smiod
496*3d8817e4Smiod /* The real symbol must exist but if it is also an ABS symbol,
497*3d8817e4Smiod there is no need to have a fixup. This is because they both
498*3d8817e4Smiod came from the same library. If on the other hand, we had to
499*3d8817e4Smiod use an indirect symbol to get to the real symbol, we add the
500*3d8817e4Smiod fixup anyway, since there are cases where these symbols come
501*3d8817e4Smiod from different shared libraries */
502*3d8817e4Smiod if (h1 != NULL
503*3d8817e4Smiod && (((h1->root.root.type == bfd_link_hash_defined
504*3d8817e4Smiod || h1->root.root.type == bfd_link_hash_defweak)
505*3d8817e4Smiod && ! bfd_is_abs_section (h1->root.root.u.def.section))
506*3d8817e4Smiod || h2->root.root.type == bfd_link_hash_indirect))
507*3d8817e4Smiod {
508*3d8817e4Smiod /* See if there is a "builtin" fixup already present
509*3d8817e4Smiod involving this symbol. If so, convert it to a regular
510*3d8817e4Smiod fixup. In the end, this relaxes some of the requirements
511*3d8817e4Smiod about the order of performing fixups. */
512*3d8817e4Smiod exists = FALSE;
513*3d8817e4Smiod for (f1 = linux_hash_table (info)->fixup_list;
514*3d8817e4Smiod f1 != NULL;
515*3d8817e4Smiod f1 = f1->next)
516*3d8817e4Smiod {
517*3d8817e4Smiod if ((f1->h != h && f1->h != h1)
518*3d8817e4Smiod || (! f1->builtin && ! f1->jump))
519*3d8817e4Smiod continue;
520*3d8817e4Smiod if (f1->h == h1)
521*3d8817e4Smiod exists = TRUE;
522*3d8817e4Smiod if (! exists
523*3d8817e4Smiod && bfd_is_abs_section (h->root.root.u.def.section))
524*3d8817e4Smiod {
525*3d8817e4Smiod f = new_fixup (info, h1, f1->h->root.root.u.def.value, 0);
526*3d8817e4Smiod f->jump = is_plt;
527*3d8817e4Smiod }
528*3d8817e4Smiod f1->h = h1;
529*3d8817e4Smiod f1->jump = is_plt;
530*3d8817e4Smiod f1->builtin = 0;
531*3d8817e4Smiod exists = TRUE;
532*3d8817e4Smiod }
533*3d8817e4Smiod if (! exists
534*3d8817e4Smiod && bfd_is_abs_section (h->root.root.u.def.section))
535*3d8817e4Smiod {
536*3d8817e4Smiod f = new_fixup (info, h1, h->root.root.u.def.value, 0);
537*3d8817e4Smiod if (f == NULL)
538*3d8817e4Smiod {
539*3d8817e4Smiod /* FIXME: No way to return error. */
540*3d8817e4Smiod abort ();
541*3d8817e4Smiod }
542*3d8817e4Smiod f->jump = is_plt;
543*3d8817e4Smiod }
544*3d8817e4Smiod }
545*3d8817e4Smiod
546*3d8817e4Smiod /* Quick and dirty way of stripping these symbols from the
547*3d8817e4Smiod symtab. */
548*3d8817e4Smiod if (bfd_is_abs_section (h->root.root.u.def.section))
549*3d8817e4Smiod h->root.written = TRUE;
550*3d8817e4Smiod }
551*3d8817e4Smiod
552*3d8817e4Smiod return TRUE;
553*3d8817e4Smiod }
554*3d8817e4Smiod
555*3d8817e4Smiod /* This is called to set the size of the .linux-dynamic section is.
556*3d8817e4Smiod It is called by the Linux linker emulation before_allocation
557*3d8817e4Smiod routine. We have finished reading all of the input files, and now
558*3d8817e4Smiod we just scan the hash tables to find out how many additional fixups
559*3d8817e4Smiod are required. */
560*3d8817e4Smiod
561*3d8817e4Smiod bfd_boolean
bfd_m68klinux_size_dynamic_sections(output_bfd,info)562*3d8817e4Smiod bfd_m68klinux_size_dynamic_sections (output_bfd, info)
563*3d8817e4Smiod bfd *output_bfd;
564*3d8817e4Smiod struct bfd_link_info *info;
565*3d8817e4Smiod {
566*3d8817e4Smiod struct fixup *f;
567*3d8817e4Smiod asection *s;
568*3d8817e4Smiod
569*3d8817e4Smiod if (output_bfd->xvec != &MY(vec))
570*3d8817e4Smiod return TRUE;
571*3d8817e4Smiod
572*3d8817e4Smiod /* First find the fixups... */
573*3d8817e4Smiod linux_link_hash_traverse (linux_hash_table (info),
574*3d8817e4Smiod linux_tally_symbols,
575*3d8817e4Smiod (PTR) info);
576*3d8817e4Smiod
577*3d8817e4Smiod /* If there are builtin fixups, leave room for a marker. This is
578*3d8817e4Smiod used by the dynamic linker so that it knows that all that follow
579*3d8817e4Smiod are builtin fixups instead of regular fixups. */
580*3d8817e4Smiod for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
581*3d8817e4Smiod {
582*3d8817e4Smiod if (f->builtin)
583*3d8817e4Smiod {
584*3d8817e4Smiod ++linux_hash_table (info)->fixup_count;
585*3d8817e4Smiod ++linux_hash_table (info)->local_builtins;
586*3d8817e4Smiod break;
587*3d8817e4Smiod }
588*3d8817e4Smiod }
589*3d8817e4Smiod
590*3d8817e4Smiod if (linux_hash_table (info)->dynobj == NULL)
591*3d8817e4Smiod {
592*3d8817e4Smiod if (linux_hash_table (info)->fixup_count > 0)
593*3d8817e4Smiod abort ();
594*3d8817e4Smiod return TRUE;
595*3d8817e4Smiod }
596*3d8817e4Smiod
597*3d8817e4Smiod /* Allocate memory for our fixup table. We will fill it in later. */
598*3d8817e4Smiod s = bfd_get_section_by_name (linux_hash_table (info)->dynobj,
599*3d8817e4Smiod ".linux-dynamic");
600*3d8817e4Smiod if (s != NULL)
601*3d8817e4Smiod {
602*3d8817e4Smiod s->size = linux_hash_table (info)->fixup_count + 1;
603*3d8817e4Smiod s->size *= 8;
604*3d8817e4Smiod s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->size);
605*3d8817e4Smiod if (s->contents == NULL)
606*3d8817e4Smiod {
607*3d8817e4Smiod bfd_set_error (bfd_error_no_memory);
608*3d8817e4Smiod return FALSE;
609*3d8817e4Smiod }
610*3d8817e4Smiod }
611*3d8817e4Smiod
612*3d8817e4Smiod return TRUE;
613*3d8817e4Smiod }
614*3d8817e4Smiod
615*3d8817e4Smiod /* We come here once we are ready to actually write the fixup table to
616*3d8817e4Smiod the output file. Scan the fixup tables and so forth and generate
617*3d8817e4Smiod the stuff we need. */
618*3d8817e4Smiod
619*3d8817e4Smiod static bfd_boolean
linux_finish_dynamic_link(output_bfd,info)620*3d8817e4Smiod linux_finish_dynamic_link (output_bfd, info)
621*3d8817e4Smiod bfd *output_bfd;
622*3d8817e4Smiod struct bfd_link_info *info;
623*3d8817e4Smiod {
624*3d8817e4Smiod asection *s, *os, *is;
625*3d8817e4Smiod bfd_byte *fixup_table;
626*3d8817e4Smiod struct linux_link_hash_entry *h;
627*3d8817e4Smiod struct fixup *f;
628*3d8817e4Smiod unsigned int new_addr;
629*3d8817e4Smiod int section_offset;
630*3d8817e4Smiod unsigned int fixups_written;
631*3d8817e4Smiod
632*3d8817e4Smiod if (linux_hash_table (info)->dynobj == NULL)
633*3d8817e4Smiod return TRUE;
634*3d8817e4Smiod
635*3d8817e4Smiod s = bfd_get_section_by_name (linux_hash_table (info)->dynobj,
636*3d8817e4Smiod ".linux-dynamic");
637*3d8817e4Smiod BFD_ASSERT (s != NULL);
638*3d8817e4Smiod os = s->output_section;
639*3d8817e4Smiod fixups_written = 0;
640*3d8817e4Smiod
641*3d8817e4Smiod #ifdef LINUX_LINK_DEBUG
642*3d8817e4Smiod printf ("Fixup table file offset: %x VMA: %x\n",
643*3d8817e4Smiod os->filepos + s->output_offset,
644*3d8817e4Smiod os->vma + s->output_offset);
645*3d8817e4Smiod #endif
646*3d8817e4Smiod
647*3d8817e4Smiod fixup_table = s->contents;
648*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) linux_hash_table (info)->fixup_count,
649*3d8817e4Smiod fixup_table);
650*3d8817e4Smiod fixup_table += 4;
651*3d8817e4Smiod
652*3d8817e4Smiod /* Fill in fixup table. */
653*3d8817e4Smiod for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
654*3d8817e4Smiod {
655*3d8817e4Smiod if (f->builtin)
656*3d8817e4Smiod continue;
657*3d8817e4Smiod
658*3d8817e4Smiod if (f->h->root.root.type != bfd_link_hash_defined
659*3d8817e4Smiod && f->h->root.root.type != bfd_link_hash_defweak)
660*3d8817e4Smiod {
661*3d8817e4Smiod (*_bfd_error_handler)
662*3d8817e4Smiod (_("Symbol %s not defined for fixups\n"),
663*3d8817e4Smiod f->h->root.root.root.string);
664*3d8817e4Smiod continue;
665*3d8817e4Smiod }
666*3d8817e4Smiod
667*3d8817e4Smiod is = f->h->root.root.u.def.section;
668*3d8817e4Smiod section_offset = is->output_section->vma + is->output_offset;
669*3d8817e4Smiod new_addr = f->h->root.root.u.def.value + section_offset;
670*3d8817e4Smiod
671*3d8817e4Smiod #ifdef LINUX_LINK_DEBUG
672*3d8817e4Smiod printf ("Fixup(%d) %s: %x %x\n",f->jump, f->h->root.root.string,
673*3d8817e4Smiod new_addr, f->value);
674*3d8817e4Smiod #endif
675*3d8817e4Smiod
676*3d8817e4Smiod if (f->jump)
677*3d8817e4Smiod {
678*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
679*3d8817e4Smiod fixup_table += 4;
680*3d8817e4Smiod bfd_put_32 (output_bfd, f->value + 2, fixup_table);
681*3d8817e4Smiod fixup_table += 4;
682*3d8817e4Smiod }
683*3d8817e4Smiod else
684*3d8817e4Smiod {
685*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
686*3d8817e4Smiod fixup_table += 4;
687*3d8817e4Smiod bfd_put_32 (output_bfd, f->value, fixup_table);
688*3d8817e4Smiod fixup_table += 4;
689*3d8817e4Smiod }
690*3d8817e4Smiod ++fixups_written;
691*3d8817e4Smiod }
692*3d8817e4Smiod
693*3d8817e4Smiod if (linux_hash_table (info)->local_builtins != 0)
694*3d8817e4Smiod {
695*3d8817e4Smiod /* Special marker so we know to switch to the other type of fixup */
696*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
697*3d8817e4Smiod fixup_table += 4;
698*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
699*3d8817e4Smiod fixup_table += 4;
700*3d8817e4Smiod ++fixups_written;
701*3d8817e4Smiod for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
702*3d8817e4Smiod {
703*3d8817e4Smiod if (! f->builtin)
704*3d8817e4Smiod continue;
705*3d8817e4Smiod
706*3d8817e4Smiod if (f->h->root.root.type != bfd_link_hash_defined
707*3d8817e4Smiod && f->h->root.root.type != bfd_link_hash_defweak)
708*3d8817e4Smiod {
709*3d8817e4Smiod (*_bfd_error_handler)
710*3d8817e4Smiod (_("Symbol %s not defined for fixups\n"),
711*3d8817e4Smiod f->h->root.root.root.string);
712*3d8817e4Smiod continue;
713*3d8817e4Smiod }
714*3d8817e4Smiod
715*3d8817e4Smiod is = f->h->root.root.u.def.section;
716*3d8817e4Smiod section_offset = is->output_section->vma + is->output_offset;
717*3d8817e4Smiod new_addr = f->h->root.root.u.def.value + section_offset;
718*3d8817e4Smiod
719*3d8817e4Smiod #ifdef LINUX_LINK_DEBUG
720*3d8817e4Smiod printf ("Fixup(B) %s: %x %x\n", f->h->root.root.string,
721*3d8817e4Smiod new_addr, f->value);
722*3d8817e4Smiod #endif
723*3d8817e4Smiod
724*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
725*3d8817e4Smiod fixup_table += 4;
726*3d8817e4Smiod bfd_put_32 (output_bfd, f->value, fixup_table);
727*3d8817e4Smiod fixup_table += 4;
728*3d8817e4Smiod ++fixups_written;
729*3d8817e4Smiod }
730*3d8817e4Smiod }
731*3d8817e4Smiod
732*3d8817e4Smiod if (linux_hash_table (info)->fixup_count != fixups_written)
733*3d8817e4Smiod {
734*3d8817e4Smiod (*_bfd_error_handler) (_("Warning: fixup count mismatch\n"));
735*3d8817e4Smiod while (linux_hash_table (info)->fixup_count > fixups_written)
736*3d8817e4Smiod {
737*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
738*3d8817e4Smiod fixup_table += 4;
739*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
740*3d8817e4Smiod fixup_table += 4;
741*3d8817e4Smiod ++fixups_written;
742*3d8817e4Smiod }
743*3d8817e4Smiod }
744*3d8817e4Smiod
745*3d8817e4Smiod h = linux_link_hash_lookup (linux_hash_table (info),
746*3d8817e4Smiod "__BUILTIN_FIXUPS__",
747*3d8817e4Smiod FALSE, FALSE, FALSE);
748*3d8817e4Smiod
749*3d8817e4Smiod if (h != NULL
750*3d8817e4Smiod && (h->root.root.type == bfd_link_hash_defined
751*3d8817e4Smiod || h->root.root.type == bfd_link_hash_defweak))
752*3d8817e4Smiod {
753*3d8817e4Smiod is = h->root.root.u.def.section;
754*3d8817e4Smiod section_offset = is->output_section->vma + is->output_offset;
755*3d8817e4Smiod new_addr = h->root.root.u.def.value + section_offset;
756*3d8817e4Smiod
757*3d8817e4Smiod #ifdef LINUX_LINK_DEBUG
758*3d8817e4Smiod printf ("Builtin fixup table at %x\n", new_addr);
759*3d8817e4Smiod #endif
760*3d8817e4Smiod
761*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
762*3d8817e4Smiod }
763*3d8817e4Smiod else
764*3d8817e4Smiod bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
765*3d8817e4Smiod
766*3d8817e4Smiod if (bfd_seek (output_bfd, (file_ptr) (os->filepos + s->output_offset),
767*3d8817e4Smiod SEEK_SET) != 0)
768*3d8817e4Smiod return FALSE;
769*3d8817e4Smiod
770*3d8817e4Smiod if (bfd_bwrite ((PTR) s->contents, s->size, output_bfd) != s->size)
771*3d8817e4Smiod return FALSE;
772*3d8817e4Smiod
773*3d8817e4Smiod return TRUE;
774*3d8817e4Smiod }
775*3d8817e4Smiod
776*3d8817e4Smiod #define MY_bfd_link_hash_table_create linux_link_hash_table_create
777*3d8817e4Smiod #define MY_add_one_symbol linux_add_one_symbol
778*3d8817e4Smiod #define MY_finish_dynamic_link linux_finish_dynamic_link
779*3d8817e4Smiod
780*3d8817e4Smiod #define MY_zmagic_contiguous 1
781*3d8817e4Smiod
782*3d8817e4Smiod #include "aout-target.h"
783