1 /* BFD back-end for linux flavored i386 a.out binaries.
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 #define PAGE_SIZE 4096
21 #define ZMAGIC_DISK_BLOCK_SIZE 1024
22 #define SEGMENT_SIZE 4096
23 #define TEXT_START_ADDR 0x0
24 #define N_SHARED_LIB(x) 0
26 #define BYTES_IN_WORD 4
31 #include "aout/aout64.h"
32 #include "aout/stab_gnu.h"
34 #include "libaout.h" /* BFD a.out internal data structures */
36 #define DEFAULT_ARCH bfd_arch_i386
37 #define MY(OP) CAT(i386linux_,OP)
38 #define MY_text_includes_header 1
39 #define TARGETNAME "a.out-i386-linux"
41 /* We always generate QMAGIC files in preference to ZMAGIC files. It
42 would be possible to make this a linker option, if that ever
45 static void MY_final_link_callback
46 PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
49 i386linux_bfd_final_link (abfd, info)
51 struct bfd_link_info *info;
53 obj_aout_subformat (abfd) = q_magic_format;
54 return NAME(aout,final_link) (abfd, info, MY_final_link_callback);
57 #define MY_bfd_final_link i386linux_bfd_final_link
59 /* Set the machine type correctly. */
62 i386linux_write_object_contents (abfd)
65 struct external_exec exec_bytes;
66 struct internal_exec *execp = exec_hdr (abfd);
68 N_SET_MACHTYPE (*execp, M_386);
70 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
72 WRITE_HEADERS(abfd, execp);
77 #define MY_write_object_contents i386linux_write_object_contents
79 /* Code to link against Linux a.out shared libraries. */
81 /* See if a symbol name is a reference to the global offset table. */
83 #ifndef GOT_REF_PREFIX
84 #define GOT_REF_PREFIX "__GOT_"
87 #define IS_GOT_SYM(name) \
88 (strncmp (name, GOT_REF_PREFIX, sizeof GOT_REF_PREFIX - 1) == 0)
90 /* See if a symbol name is a reference to the procedure linkage table. */
92 #ifndef PLT_REF_PREFIX
93 #define PLT_REF_PREFIX "__PLT_"
96 #define IS_PLT_SYM(name) \
97 (strncmp (name, PLT_REF_PREFIX, sizeof PLT_REF_PREFIX - 1) == 0)
99 /* This special symbol is a set vector that contains a list of
100 pointers to fixup tables. It will be present in any dynamicly
101 linked file. The linker generated fixup table should also be added
102 to the list, and it should always appear in the second slot (the
103 first one is a dummy with a magic number that is defined in
106 #ifndef SHARABLE_CONFLICTS
107 #define SHARABLE_CONFLICTS "__SHARABLE_CONFLICTS__"
110 /* We keep a list of fixups. The terminology is a bit strange, but
111 each fixup contains two 32 bit numbers. A regular fixup contains
112 an address and a pointer, and at runtime we should store the
113 address at the location pointed to by the pointer. A builtin fixup
114 contains two pointers, and we should read the address using one
115 pointer and store it at the location pointed to by the other
116 pointer. Builtin fixups come into play when we have duplicate
117 __GOT__ symbols for the same variable. The builtin fixup will copy
118 the GOT pointer from one over into the other. */
123 struct linux_link_hash_entry *h;
126 /* Nonzero if this is a jump instruction that needs to be fixed,
127 zero if this is just a pointer */
133 /* We don't need a special hash table entry structure, but we do need
134 to keep some information between linker passes, so we use a special
137 struct linux_link_hash_entry
139 struct aout_link_hash_entry root;
142 struct linux_link_hash_table
144 struct aout_link_hash_table root;
146 /* First dynamic object found in link. */
149 /* Number of fixups. */
152 /* Number of builtin fixups. */
153 size_t local_builtins;
155 /* List of fixups. */
156 struct fixup *fixup_list;
159 static struct bfd_hash_entry *linux_link_hash_newfunc
160 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
161 static struct bfd_link_hash_table *linux_link_hash_table_create
163 static struct fixup *new_fixup
164 PARAMS ((struct bfd_link_info *, struct linux_link_hash_entry *,
166 static boolean linux_link_create_dynamic_sections
167 PARAMS ((bfd *, struct bfd_link_info *));
168 static boolean linux_add_one_symbol
169 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
170 bfd_vma, const char *, boolean, boolean,
171 struct bfd_link_hash_entry **));
172 static boolean linux_tally_symbols
173 PARAMS ((struct linux_link_hash_entry *, PTR));
174 static boolean linux_finish_dynamic_link
175 PARAMS ((bfd *, struct bfd_link_info *));
177 /* Routine to create an entry in an Linux link hash table. */
179 static struct bfd_hash_entry *
180 linux_link_hash_newfunc (entry, table, string)
181 struct bfd_hash_entry *entry;
182 struct bfd_hash_table *table;
185 struct linux_link_hash_entry *ret = (struct linux_link_hash_entry *) entry;
187 /* Allocate the structure if it has not already been allocated by a
189 if (ret == (struct linux_link_hash_entry *) NULL)
190 ret = ((struct linux_link_hash_entry *)
191 bfd_hash_allocate (table, sizeof (struct linux_link_hash_entry)));
193 return (struct bfd_hash_entry *) ret;
195 /* Call the allocation method of the superclass. */
196 ret = ((struct linux_link_hash_entry *)
197 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
201 /* Set local fields; there aren't any. */
204 return (struct bfd_hash_entry *) ret;
207 /* Create a Linux link hash table. */
209 static struct bfd_link_hash_table *
210 linux_link_hash_table_create (abfd)
213 struct linux_link_hash_table *ret;
215 ret = ((struct linux_link_hash_table *)
216 malloc (sizeof (struct linux_link_hash_table)));
217 if (ret == (struct linux_link_hash_table *) NULL)
219 bfd_set_error (bfd_error_no_memory);
220 return (struct bfd_link_hash_table *) NULL;
222 if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
223 linux_link_hash_newfunc))
226 return (struct bfd_link_hash_table *) NULL;
230 ret->fixup_count = 0;
231 ret->local_builtins = 0;
232 ret->fixup_list = NULL;
234 return &ret->root.root;
237 /* Look up an entry in a Linux link hash table. */
239 #define linux_link_hash_lookup(table, string, create, copy, follow) \
240 ((struct linux_link_hash_entry *) \
241 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
244 /* Traverse a Linux link hash table. */
246 #define linux_link_hash_traverse(table, func, info) \
247 (aout_link_hash_traverse \
249 (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
252 /* Get the Linux link hash table from the info structure. This is
255 #define linux_hash_table(p) ((struct linux_link_hash_table *) ((p)->hash))
257 /* Store the information for a new fixup. */
259 static struct fixup *
260 new_fixup (info, h, value, builtin)
261 struct bfd_link_info *info;
262 struct linux_link_hash_entry *h;
268 f = (struct fixup *) bfd_hash_allocate (&info->hash->table,
269 sizeof (struct fixup));
272 f->next = linux_hash_table (info)->fixup_list;
273 linux_hash_table (info)->fixup_list = f;
276 f->builtin = builtin;
278 ++linux_hash_table (info)->fixup_count;
282 /* We come here once we realize that we are going to link to a shared
283 library. We need to create a special section that contains the
284 fixup table, and we ultimately need to add a pointer to this into
285 the set vector for SHARABLE_CONFLICTS. At this point we do not
286 know the size of the section, but that's OK - we just need to
287 create it for now. */
290 linux_link_create_dynamic_sections (abfd, info)
292 struct bfd_link_info *info;
295 register asection *s;
297 /* Note that we set the SEC_IN_MEMORY flag. */
298 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
300 /* We choose to use the name ".dynamic" for the fixup table. Why
302 s = bfd_make_section (abfd, ".dynamic");
304 || ! bfd_set_section_flags (abfd, s, flags)
305 || ! bfd_set_section_alignment (abfd, s, 2))
313 /* Function to add a single symbol to the linker hash table. This is
314 a wrapper around _bfd_generic_link_add_one_symbol which handles the
315 tweaking needed for dynamic linking support. */
318 linux_add_one_symbol (info, abfd, name, flags, section, value, string,
319 copy, collect, hashp)
320 struct bfd_link_info *info;
329 struct bfd_link_hash_entry **hashp;
331 struct linux_link_hash_entry *h;
334 /* Look up and see if we already have this symbol in the hash table.
335 If we do, and the defining entry is from a shared library, we
336 need to create the dynamic sections.
338 FIXME: What if abfd->xvec != info->hash->creator? We may want to
339 be able to link Linux a.out and ELF objects together, but serious
340 confusion is possible. */
344 if (! info->relocateable
345 && linux_hash_table (info)->dynobj == NULL
346 && strcmp (name, SHARABLE_CONFLICTS) == 0)
348 if (! linux_link_create_dynamic_sections (abfd, info))
350 linux_hash_table (info)->dynobj = abfd;
354 if (section == &bfd_abs_section
355 && (IS_GOT_SYM (name) || IS_PLT_SYM (name)))
357 h = linux_link_hash_lookup (linux_hash_table (info), name, false,
360 && h->root.root.type == bfd_link_hash_defined)
362 if (new_fixup (info, h, value, ! IS_PLT_SYM(name)) == NULL)
367 *hashp = (struct bfd_link_hash_entry *) h;
370 /* Do the usual procedure for adding a symbol. */
371 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
372 value, string, copy, collect,
376 /* Insert a pointer to our table in the set vector. The dynamic
377 linker requires this information */
382 /* Here we do our special thing to add the pointer to the
383 dynamic section in the SHARABLE_CONFLICTS set vector. */
384 s = bfd_get_section_by_name (linux_hash_table (info)->dynobj,
386 BFD_ASSERT (s != NULL);
388 if (! (_bfd_generic_link_add_one_symbol
389 (info, linux_hash_table (info)->dynobj, SHARABLE_CONFLICTS,
390 BSF_GLOBAL | BSF_CONSTRUCTOR, s, 0, NULL, false, false, NULL)))
397 /* We will crawl the hash table and come here for every global symbol.
398 We will examine each entry and see if there are indications that we
399 need to add a fixup. There are two possible cases - one is where
400 you have duplicate definitions of PLT or GOT symbols - these will
401 have already been caught and added as "builtin" fixups. If we find
402 that the corresponding non PLT/GOT symbol is also present, we
403 convert it to a regular fixup instead.
405 This function is called via linux_link_hash_traverse. */
408 linux_tally_symbols (h, data)
409 struct linux_link_hash_entry *h;
412 struct bfd_link_info *info = (struct bfd_link_info *) data;
413 struct fixup *f, *f1;
415 struct linux_link_hash_entry *h1, *h2;
417 /* If this symbol is not a PLT/GOT, we do not even need to look at it */
418 is_plt = IS_PLT_SYM (h->root.root.root.string);
420 if (is_plt || IS_GOT_SYM (h->root.root.root.string))
422 /* Look up this symbol twice. Once just as a regular lookup,
423 and then again following all of the indirect links until we
424 reach a real symbol. */
425 h1 = linux_link_hash_lookup (linux_hash_table (info),
426 (h->root.root.root.string
427 + sizeof PLT_REF_PREFIX - 1),
429 /* h2 does not follow indirect symbols. */
430 h2 = linux_link_hash_lookup (linux_hash_table (info),
431 (h->root.root.root.string
432 + sizeof PLT_REF_PREFIX - 1),
433 false, false, false);
435 /* The real symbol must exist but if it is also an ABS symbol,
436 there is no need to have a fixup. This is because they both
437 came from the same library. If on the other hand, we had to
438 use an indirect symbol to get to the real symbol, we add the
439 fixup anyway, since there are cases where these symbols come
440 from different shared libraries */
442 && ((h1->root.root.type == bfd_link_hash_defined
443 && h1->root.root.u.def.section != &bfd_abs_section)
444 || h2->root.root.type == bfd_link_hash_indirect))
446 f = new_fixup (info, h1, h->root.root.u.def.value, 0);
449 /* FIXME: No way to return error. */
454 /* See if there is a "builtin" fixup already present
455 involving this symbol. If so, convert it to a regular
456 fixup. In the end, this relaxes some of the requirements
457 about the order of performing fixups. */
458 for (f1 = linux_hash_table (info)->fixup_list;
472 /* Quick and dirty way of stripping these symbols from the
474 h->root.written = true;
480 /* This is called to set the size of the .dynamic section is. It is
481 called by the Linux linker emulation before_allocation routine. We
482 have finished reading all of the input files, and now we just scan
483 the hash tables to find out how many additional fixups are
487 bfd_linux_size_dynamic_sections (output_bfd, info)
489 struct bfd_link_info *info;
494 /* First find the fixups... */
495 linux_link_hash_traverse (linux_hash_table (info),
499 /* If there are builtin fixups, leave room for a marker. This is
500 used by the dynamic linker so that it knows that all that follow
501 are builtin fixups instead of regular fixups. */
502 for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
506 ++linux_hash_table (info)->fixup_count;
507 ++linux_hash_table (info)->local_builtins;
512 if (linux_hash_table (info)->dynobj == NULL)
514 if (linux_hash_table (info)->fixup_count > 0)
519 /* Allocate memory for our fixup table. We will fill it in later. */
520 s = bfd_get_section_by_name (linux_hash_table (info)->dynobj, ".dynamic");
523 s->_raw_size = 8 + linux_hash_table (info)->fixup_count * 8;
524 s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size);
525 if (s->contents == NULL)
527 bfd_set_error (bfd_error_no_memory);
530 memset (s->contents, 0, s->_raw_size);
536 /* We come here once we are ready to actually write the fixup table to
537 the output file. Scan the fixup tables and so forth and generate
538 the stuff we need. */
541 linux_finish_dynamic_link (output_bfd, info)
543 struct bfd_link_info *info;
545 asection *s, *os, *is;
546 bfd_byte *fixup_table;
547 struct linux_link_hash_entry *h;
549 unsigned int new_addr;
553 if (linux_hash_table (info)->dynobj == NULL)
556 s = bfd_get_section_by_name (linux_hash_table (info)->dynobj, ".dynamic");
557 BFD_ASSERT (s != NULL);
558 os = s->output_section;
561 #ifdef LINUX_LINK_DEBUG
562 printf ("Fixup table file offset: %x VMA: %x\n",
563 os->filepos + s->output_offset,
564 os->vma + s->output_offset);
567 fixup_table = s->contents;
568 bfd_put_32 (output_bfd, linux_hash_table (info)->fixup_count, fixup_table);
571 /* Fill in fixup table. */
572 for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
577 if (f->h->root.root.type != bfd_link_hash_defined)
581 "Symbol %s not defined for fixups\n",
582 f->h->root.root.root.string);
586 is = f->h->root.root.u.def.section;
587 section_offset = is->output_section->vma + is->output_offset;
588 new_addr = f->h->root.root.u.def.value + section_offset;
590 #ifdef LINUX_LINK_DEBUG
591 printf ("Fixup(%d) %s: %x %x\n",f->jump, f->h->root.root.string,
597 /* Relative address */
598 new_addr = new_addr - (f->value + 5);
599 bfd_put_32 (output_bfd, new_addr, fixup_table);
601 bfd_put_32 (output_bfd, f->value + 1, fixup_table);
606 bfd_put_32 (output_bfd, new_addr, fixup_table);
608 bfd_put_32 (output_bfd, f->value, fixup_table);
614 if (linux_hash_table (info)->local_builtins != 0)
616 /* Special marker so we know to switch to the other type of fixup */
617 bfd_put_32 (output_bfd, 0, fixup_table);
619 bfd_put_32 (output_bfd, 0, fixup_table);
622 for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
627 if (f->h->root.root.type != bfd_link_hash_defined)
631 "Symbol %s not defined for fixups\n",
632 f->h->root.root.root.string);
636 is = f->h->root.root.u.def.section;
637 section_offset = is->output_section->vma + is->output_offset;
638 new_addr = f->h->root.root.u.def.value + section_offset;
640 #ifdef LINUX_LINK_DEBUG
641 printf ("Fixup(B) %s: %x %x\n", f->h->root.root.string,
645 bfd_put_32 (output_bfd, new_addr, fixup_table);
647 bfd_put_32 (output_bfd, f->value, fixup_table);
653 if (linux_hash_table (info)->fixup_count != fixups_written)
656 fprintf (stderr, "Warning: fixup count mismatch\n");
657 while (linux_hash_table (info)->fixup_count > fixups_written)
659 bfd_put_32 (output_bfd, 0, fixup_table);
661 bfd_put_32 (output_bfd, 0, fixup_table);
667 h = linux_link_hash_lookup (linux_hash_table (info),
668 "__BUILTIN_FIXUPS__",
669 false, false, false);
671 if (h != NULL && h->root.root.type == bfd_link_hash_defined)
673 is = h->root.root.u.def.section;
674 section_offset = is->output_section->vma + is->output_offset;
675 new_addr = h->root.root.u.def.value + section_offset;
677 #ifdef LINUX_LINK_DEBUG
678 printf ("Builtin fixup table at %x\n", new_addr);
681 bfd_put_32 (output_bfd, new_addr, fixup_table);
684 bfd_put_32 (output_bfd, 0, fixup_table);
686 if (bfd_seek (output_bfd, os->filepos + s->output_offset, SEEK_SET) != 0)
689 if (bfd_write ((PTR) s->contents, 1, s->_raw_size, output_bfd)
696 #define MY_bfd_link_hash_table_create linux_link_hash_table_create
697 #define MY_add_one_symbol linux_add_one_symbol
698 #define MY_finish_dynamic_link linux_finish_dynamic_link
700 #include "aout-target.h"