merge from gcc
[gdb/gnu.git] / bfd / i386linux.c
blob65ae5c9c6df82c714192c0a2649a1040b5ef9e05
1 /* BFD back-end for linux flavored i386 a.out binaries.
2 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define TARGET_PAGE_SIZE 4096
24 #define ZMAGIC_DISK_BLOCK_SIZE 1024
25 #define SEGMENT_SIZE TARGET_PAGE_SIZE
26 #define TEXT_START_ADDR 0x0
28 #define MACHTYPE_OK(mtype) ((mtype) == M_386 || (mtype) == M_UNKNOWN)
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "libbfd.h"
33 #include "aout/aout64.h"
34 #include "aout/stab_gnu.h"
35 #include "aout/ar.h"
36 #include "libaout.h" /* BFD a.out internal data structures */
38 #define DEFAULT_ARCH bfd_arch_i386
40 /* Do not "beautify" the CONCAT* macro args. Traditional C will not
41 remove whitespace added here, and thus will fail to concatenate
42 the tokens. */
43 #define MY(OP) CONCAT2 (i386linux_,OP)
44 #define TARGETNAME "a.out-i386-linux"
46 extern const bfd_target MY(vec);
48 /* We always generate QMAGIC files in preference to ZMAGIC files. It
49 would be possible to make this a linker option, if that ever
50 becomes important. */
52 static void MY_final_link_callback
53 (bfd *, file_ptr *, file_ptr *, file_ptr *);
54 static bfd_boolean i386linux_bfd_final_link
55 (bfd *, struct bfd_link_info *);
56 static bfd_boolean i386linux_write_object_contents (bfd *);
58 static bfd_boolean
59 i386linux_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
61 obj_aout_subformat (abfd) = q_magic_format;
62 return NAME(aout,final_link) (abfd, info, MY_final_link_callback);
65 #define MY_bfd_final_link i386linux_bfd_final_link
67 /* Set the machine type correctly. */
69 static bfd_boolean
70 i386linux_write_object_contents (bfd *abfd)
72 struct external_exec exec_bytes;
73 struct internal_exec *execp = exec_hdr (abfd);
75 N_SET_MACHTYPE (*execp, M_386);
77 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
79 WRITE_HEADERS(abfd, execp);
81 return TRUE;
84 #define MY_write_object_contents i386linux_write_object_contents
86 /* Code to link against Linux a.out shared libraries. */
88 /* See if a symbol name is a reference to the global offset table. */
90 #ifndef GOT_REF_PREFIX
91 #define GOT_REF_PREFIX "__GOT_"
92 #endif
94 #define IS_GOT_SYM(name) (CONST_STRNEQ (name, GOT_REF_PREFIX))
96 /* See if a symbol name is a reference to the procedure linkage table. */
98 #ifndef PLT_REF_PREFIX
99 #define PLT_REF_PREFIX "__PLT_"
100 #endif
102 #define IS_PLT_SYM(name) (CONST_STRNEQ (name, PLT_REF_PREFIX))
104 /* This string is used to generate specialized error messages. */
106 #ifndef NEEDS_SHRLIB
107 #define NEEDS_SHRLIB "__NEEDS_SHRLIB_"
108 #endif
110 /* This special symbol is a set vector that contains a list of
111 pointers to fixup tables. It will be present in any dynamically
112 linked file. The linker generated fixup table should also be added
113 to the list, and it should always appear in the second slot (the
114 first one is a dummy with a magic number that is defined in
115 crt0.o). */
117 #ifndef SHARABLE_CONFLICTS
118 #define SHARABLE_CONFLICTS "__SHARABLE_CONFLICTS__"
119 #endif
121 /* We keep a list of fixups. The terminology is a bit strange, but
122 each fixup contains two 32 bit numbers. A regular fixup contains
123 an address and a pointer, and at runtime we should store the
124 address at the location pointed to by the pointer. A builtin fixup
125 contains two pointers, and we should read the address using one
126 pointer and store it at the location pointed to by the other
127 pointer. Builtin fixups come into play when we have duplicate
128 __GOT__ symbols for the same variable. The builtin fixup will copy
129 the GOT pointer from one over into the other. */
131 struct fixup
133 struct fixup *next;
134 struct linux_link_hash_entry *h;
135 bfd_vma value;
137 /* Nonzero if this is a jump instruction that needs to be fixed,
138 zero if this is just a pointer */
139 char jump;
141 char builtin;
144 /* We don't need a special hash table entry structure, but we do need
145 to keep some information between linker passes, so we use a special
146 hash table. */
148 struct linux_link_hash_entry
150 struct aout_link_hash_entry root;
153 struct linux_link_hash_table
155 struct aout_link_hash_table root;
157 /* First dynamic object found in link. */
158 bfd *dynobj;
160 /* Number of fixups. */
161 size_t fixup_count;
163 /* Number of builtin fixups. */
164 size_t local_builtins;
166 /* List of fixups. */
167 struct fixup *fixup_list;
170 /* Routine to create an entry in an Linux link hash table. */
172 static struct bfd_hash_entry *
173 linux_link_hash_newfunc (struct bfd_hash_entry *entry,
174 struct bfd_hash_table *table,
175 const char *string)
177 struct linux_link_hash_entry *ret = (struct linux_link_hash_entry *) entry;
179 /* Allocate the structure if it has not already been allocated by a
180 subclass. */
181 if (ret == (struct linux_link_hash_entry *) NULL)
182 ret = ((struct linux_link_hash_entry *)
183 bfd_hash_allocate (table, sizeof (struct linux_link_hash_entry)));
184 if (ret == NULL)
185 return (struct bfd_hash_entry *) ret;
187 /* Call the allocation method of the superclass. */
188 ret = ((struct linux_link_hash_entry *)
189 NAME(aout,link_hash_newfunc) ((struct bfd_hash_entry *) ret,
190 table, string));
191 if (ret != NULL)
193 /* Set local fields; there aren't any. */
196 return (struct bfd_hash_entry *) ret;
199 /* Create a Linux link hash table. */
201 static struct bfd_link_hash_table *
202 linux_link_hash_table_create (bfd *abfd)
204 struct linux_link_hash_table *ret;
205 bfd_size_type amt = sizeof (struct linux_link_hash_table);
207 ret = (struct linux_link_hash_table *) bfd_zmalloc (amt);
208 if (ret == (struct linux_link_hash_table *) NULL)
209 return (struct bfd_link_hash_table *) NULL;
210 if (!NAME(aout,link_hash_table_init) (&ret->root, abfd,
211 linux_link_hash_newfunc,
212 sizeof (struct linux_link_hash_entry)))
214 free (ret);
215 return (struct bfd_link_hash_table *) NULL;
218 return &ret->root.root;
221 /* Look up an entry in a Linux link hash table. */
223 #define linux_link_hash_lookup(table, string, create, copy, follow) \
224 ((struct linux_link_hash_entry *) \
225 aout_link_hash_lookup (&(table)->root, (string), (create), (copy),\
226 (follow)))
228 /* Traverse a Linux link hash table. */
230 #define linux_link_hash_traverse(table, func, info) \
231 (aout_link_hash_traverse \
232 (&(table)->root, \
233 (bfd_boolean (*) (struct aout_link_hash_entry *, void *)) (func), \
234 (info)))
236 /* Get the Linux link hash table from the info structure. This is
237 just a cast. */
239 #define linux_hash_table(p) ((struct linux_link_hash_table *) ((p)->hash))
241 /* Store the information for a new fixup. */
243 static struct fixup *
244 new_fixup (struct bfd_link_info *info,
245 struct linux_link_hash_entry *h,
246 bfd_vma value,
247 int builtin)
249 struct fixup *f;
251 f = (struct fixup *) bfd_hash_allocate (&info->hash->table,
252 sizeof (struct fixup));
253 if (f == NULL)
254 return f;
255 f->next = linux_hash_table (info)->fixup_list;
256 linux_hash_table (info)->fixup_list = f;
257 f->h = h;
258 f->value = value;
259 f->builtin = builtin;
260 f->jump = 0;
261 ++linux_hash_table (info)->fixup_count;
262 return f;
265 /* We come here once we realize that we are going to link to a shared
266 library. We need to create a special section that contains the
267 fixup table, and we ultimately need to add a pointer to this into
268 the set vector for SHARABLE_CONFLICTS. At this point we do not
269 know the size of the section, but that's OK - we just need to
270 create it for now. */
272 static bfd_boolean
273 linux_link_create_dynamic_sections (bfd *abfd,
274 struct bfd_link_info *info ATTRIBUTE_UNUSED)
276 flagword flags;
277 asection *s;
279 /* Note that we set the SEC_IN_MEMORY flag. */
280 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
282 /* We choose to use the name ".linux-dynamic" for the fixup table.
283 Why not? */
284 s = bfd_make_section_with_flags (abfd, ".linux-dynamic", flags);
285 if (s == NULL
286 || ! bfd_set_section_alignment (abfd, s, 2))
287 return FALSE;
288 s->size = 0;
289 s->contents = 0;
291 return TRUE;
294 /* Function to add a single symbol to the linker hash table. This is
295 a wrapper around _bfd_generic_link_add_one_symbol which handles the
296 tweaking needed for dynamic linking support. */
298 static bfd_boolean
299 linux_add_one_symbol (struct bfd_link_info *info,
300 bfd *abfd,
301 const char *name,
302 flagword flags,
303 asection *section,
304 bfd_vma value,
305 const char *string,
306 bfd_boolean copy,
307 bfd_boolean collect,
308 struct bfd_link_hash_entry **hashp)
310 struct linux_link_hash_entry *h;
311 bfd_boolean insert;
313 /* Look up and see if we already have this symbol in the hash table.
314 If we do, and the defining entry is from a shared library, we
315 need to create the dynamic sections.
317 FIXME: What if abfd->xvec != info->output_bfd->xvec? We may
318 want to be able to link Linux a.out and ELF objects together,
319 but serious confusion is possible. */
321 insert = FALSE;
323 if (! info->relocatable
324 && linux_hash_table (info)->dynobj == NULL
325 && strcmp (name, SHARABLE_CONFLICTS) == 0
326 && (flags & BSF_CONSTRUCTOR) != 0
327 && abfd->xvec == info->output_bfd->xvec)
329 if (! linux_link_create_dynamic_sections (abfd, info))
330 return FALSE;
331 linux_hash_table (info)->dynobj = abfd;
332 insert = TRUE;
335 if (bfd_is_abs_section (section)
336 && abfd->xvec == info->output_bfd->xvec)
338 h = linux_link_hash_lookup (linux_hash_table (info), name, FALSE,
339 FALSE, FALSE);
340 if (h != NULL
341 && (h->root.root.type == bfd_link_hash_defined
342 || h->root.root.type == bfd_link_hash_defweak))
344 struct fixup *f;
346 if (hashp != NULL)
347 *hashp = (struct bfd_link_hash_entry *) h;
349 f = new_fixup (info, h, value, ! IS_PLT_SYM (name));
350 if (f == NULL)
351 return FALSE;
352 f->jump = IS_PLT_SYM (name);
354 return TRUE;
358 /* Do the usual procedure for adding a symbol. */
359 if (! _bfd_generic_link_add_one_symbol (info, abfd, name, flags, section,
360 value, string, copy, collect,
361 hashp))
362 return FALSE;
364 /* Insert a pointer to our table in the set vector. The dynamic
365 linker requires this information */
366 if (insert)
368 asection *s;
370 /* Here we do our special thing to add the pointer to the
371 dynamic section in the SHARABLE_CONFLICTS set vector. */
372 s = bfd_get_section_by_name (linux_hash_table (info)->dynobj,
373 ".linux-dynamic");
374 BFD_ASSERT (s != NULL);
376 if (! (_bfd_generic_link_add_one_symbol
377 (info, linux_hash_table (info)->dynobj, SHARABLE_CONFLICTS,
378 BSF_GLOBAL | BSF_CONSTRUCTOR, s, (bfd_vma) 0, NULL,
379 FALSE, FALSE, NULL)))
380 return FALSE;
383 return TRUE;
386 /* We will crawl the hash table and come here for every global symbol.
387 We will examine each entry and see if there are indications that we
388 need to add a fixup. There are two possible cases - one is where
389 you have duplicate definitions of PLT or GOT symbols - these will
390 have already been caught and added as "builtin" fixups. If we find
391 that the corresponding non PLT/GOT symbol is also present, we
392 convert it to a regular fixup instead.
394 This function is called via linux_link_hash_traverse. */
396 static bfd_boolean
397 linux_tally_symbols (struct linux_link_hash_entry *h, void * data)
399 struct bfd_link_info *info = (struct bfd_link_info *) data;
400 struct fixup *f, *f1;
401 int is_plt;
402 struct linux_link_hash_entry *h1, *h2;
403 bfd_boolean exists;
405 if (h->root.root.type == bfd_link_hash_undefined
406 && CONST_STRNEQ (h->root.root.root.string, NEEDS_SHRLIB))
408 const char *name;
409 char *p;
410 char *alloc = NULL;
412 name = h->root.root.root.string + sizeof NEEDS_SHRLIB - 1;
413 p = strrchr (name, '_');
414 if (p != NULL)
415 alloc = (char *) bfd_malloc ((bfd_size_type) strlen (name) + 1);
417 if (p == NULL || alloc == NULL)
418 (*_bfd_error_handler) (_("Output file requires shared library `%s'\n"),
419 name);
420 else
422 strcpy (alloc, name);
423 p = strrchr (alloc, '_');
424 *p++ = '\0';
425 (*_bfd_error_handler)
426 (_("Output file requires shared library `%s.so.%s'\n"),
427 alloc, p);
428 free (alloc);
431 abort ();
434 /* If this symbol is not a PLT/GOT, we do not even need to look at it */
435 is_plt = IS_PLT_SYM (h->root.root.root.string);
437 if (is_plt || IS_GOT_SYM (h->root.root.root.string))
439 /* Look up this symbol twice. Once just as a regular lookup,
440 and then again following all of the indirect links until we
441 reach a real symbol. */
442 h1 = linux_link_hash_lookup (linux_hash_table (info),
443 (h->root.root.root.string
444 + sizeof PLT_REF_PREFIX - 1),
445 FALSE, FALSE, TRUE);
446 /* h2 does not follow indirect symbols. */
447 h2 = linux_link_hash_lookup (linux_hash_table (info),
448 (h->root.root.root.string
449 + sizeof PLT_REF_PREFIX - 1),
450 FALSE, FALSE, FALSE);
452 /* The real symbol must exist but if it is also an ABS symbol,
453 there is no need to have a fixup. This is because they both
454 came from the same library. If on the other hand, we had to
455 use an indirect symbol to get to the real symbol, we add the
456 fixup anyway, since there are cases where these symbols come
457 from different shared libraries */
458 if (h1 != NULL
459 && (((h1->root.root.type == bfd_link_hash_defined
460 || h1->root.root.type == bfd_link_hash_defweak)
461 && ! bfd_is_abs_section (h1->root.root.u.def.section))
462 || h2->root.root.type == bfd_link_hash_indirect))
464 /* See if there is a "builtin" fixup already present
465 involving this symbol. If so, convert it to a regular
466 fixup. In the end, this relaxes some of the requirements
467 about the order of performing fixups. */
468 exists = FALSE;
469 for (f1 = linux_hash_table (info)->fixup_list;
470 f1 != NULL;
471 f1 = f1->next)
473 if ((f1->h != h && f1->h != h1)
474 || (! f1->builtin && ! f1->jump))
475 continue;
476 if (f1->h == h1)
477 exists = TRUE;
478 if (! exists
479 && bfd_is_abs_section (h->root.root.u.def.section))
481 f = new_fixup (info, h1, f1->h->root.root.u.def.value, 0);
482 f->jump = is_plt;
484 f1->h = h1;
485 f1->jump = is_plt;
486 f1->builtin = 0;
487 exists = TRUE;
489 if (! exists
490 && bfd_is_abs_section (h->root.root.u.def.section))
492 f = new_fixup (info, h1, h->root.root.u.def.value, 0);
493 if (f == NULL)
495 /* FIXME: No way to return error. */
496 abort ();
498 f->jump = is_plt;
502 /* Quick and dirty way of stripping these symbols from the
503 symtab. */
504 if (bfd_is_abs_section (h->root.root.u.def.section))
505 h->root.written = TRUE;
508 return TRUE;
511 /* This is called to set the size of the .linux-dynamic section is.
512 It is called by the Linux linker emulation before_allocation
513 routine. We have finished reading all of the input files, and now
514 we just scan the hash tables to find out how many additional fixups
515 are required. */
517 bfd_boolean
518 bfd_i386linux_size_dynamic_sections (bfd *output_bfd,
519 struct bfd_link_info *info)
521 struct fixup *f;
522 asection *s;
524 if (output_bfd->xvec != &MY(vec))
525 return TRUE;
527 /* First find the fixups... */
528 linux_link_hash_traverse (linux_hash_table (info),
529 linux_tally_symbols,
530 info);
532 /* If there are builtin fixups, leave room for a marker. This is
533 used by the dynamic linker so that it knows that all that follow
534 are builtin fixups instead of regular fixups. */
535 for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
537 if (f->builtin)
539 ++linux_hash_table (info)->fixup_count;
540 ++linux_hash_table (info)->local_builtins;
541 break;
545 if (linux_hash_table (info)->dynobj == NULL)
547 if (linux_hash_table (info)->fixup_count > 0)
548 abort ();
549 return TRUE;
552 /* Allocate memory for our fixup table. We will fill it in later. */
553 s = bfd_get_section_by_name (linux_hash_table (info)->dynobj,
554 ".linux-dynamic");
555 if (s != NULL)
557 s->size = linux_hash_table (info)->fixup_count + 1;
558 s->size *= 8;
559 s->contents = (bfd_byte *) bfd_zalloc (output_bfd, s->size);
560 if (s->contents == NULL)
561 return FALSE;
564 return TRUE;
567 /* We come here once we are ready to actually write the fixup table to
568 the output file. Scan the fixup tables and so forth and generate
569 the stuff we need. */
571 static bfd_boolean
572 linux_finish_dynamic_link (bfd *output_bfd,
573 struct bfd_link_info *info)
575 asection *s, *os, *is;
576 bfd_byte *fixup_table;
577 struct linux_link_hash_entry *h;
578 struct fixup *f;
579 unsigned int new_addr;
580 int section_offset;
581 unsigned int fixups_written;
583 if (linux_hash_table (info)->dynobj == NULL)
584 return TRUE;
586 s = bfd_get_section_by_name (linux_hash_table (info)->dynobj,
587 ".linux-dynamic");
588 BFD_ASSERT (s != NULL);
589 os = s->output_section;
590 fixups_written = 0;
592 #ifdef LINUX_LINK_DEBUG
593 printf ("Fixup table file offset: %x VMA: %x\n",
594 os->filepos + s->output_offset,
595 os->vma + s->output_offset);
596 #endif
598 fixup_table = s->contents;
599 bfd_put_32 (output_bfd,
600 (bfd_vma) linux_hash_table (info)->fixup_count, fixup_table);
601 fixup_table += 4;
603 /* Fill in fixup table. */
604 for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
606 if (f->builtin)
607 continue;
609 if (f->h->root.root.type != bfd_link_hash_defined
610 && f->h->root.root.type != bfd_link_hash_defweak)
612 (*_bfd_error_handler)
613 (_("Symbol %s not defined for fixups\n"),
614 f->h->root.root.root.string);
615 continue;
618 is = f->h->root.root.u.def.section;
619 section_offset = is->output_section->vma + is->output_offset;
620 new_addr = f->h->root.root.u.def.value + section_offset;
622 #ifdef LINUX_LINK_DEBUG
623 printf ("Fixup(%d) %s: %x %x\n",f->jump, f->h->root.root.string,
624 new_addr, f->value);
625 #endif
627 if (f->jump)
629 /* Relative address */
630 new_addr = new_addr - (f->value + 5);
631 bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
632 fixup_table += 4;
633 bfd_put_32 (output_bfd, f->value + 1, fixup_table);
634 fixup_table += 4;
636 else
638 bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
639 fixup_table += 4;
640 bfd_put_32 (output_bfd, f->value, fixup_table);
641 fixup_table += 4;
643 ++fixups_written;
646 if (linux_hash_table (info)->local_builtins != 0)
648 /* Special marker so we know to switch to the other type of fixup */
649 bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
650 fixup_table += 4;
651 bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
652 fixup_table += 4;
653 ++fixups_written;
654 for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
656 if (! f->builtin)
657 continue;
659 if (f->h->root.root.type != bfd_link_hash_defined
660 && f->h->root.root.type != bfd_link_hash_defweak)
662 (*_bfd_error_handler)
663 (_("Symbol %s not defined for fixups\n"),
664 f->h->root.root.root.string);
665 continue;
668 is = f->h->root.root.u.def.section;
669 section_offset = is->output_section->vma + is->output_offset;
670 new_addr = f->h->root.root.u.def.value + section_offset;
672 #ifdef LINUX_LINK_DEBUG
673 printf ("Fixup(B) %s: %x %x\n", f->h->root.root.string,
674 new_addr, f->value);
675 #endif
677 bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
678 fixup_table += 4;
679 bfd_put_32 (output_bfd, f->value, fixup_table);
680 fixup_table += 4;
681 ++fixups_written;
685 if (linux_hash_table (info)->fixup_count != fixups_written)
687 (*_bfd_error_handler) (_("Warning: fixup count mismatch\n"));
688 while (linux_hash_table (info)->fixup_count > fixups_written)
690 bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
691 fixup_table += 4;
692 bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
693 fixup_table += 4;
694 ++fixups_written;
698 h = linux_link_hash_lookup (linux_hash_table (info),
699 "__BUILTIN_FIXUPS__",
700 FALSE, FALSE, FALSE);
702 if (h != NULL
703 && (h->root.root.type == bfd_link_hash_defined
704 || h->root.root.type == bfd_link_hash_defweak))
706 is = h->root.root.u.def.section;
707 section_offset = is->output_section->vma + is->output_offset;
708 new_addr = h->root.root.u.def.value + section_offset;
710 #ifdef LINUX_LINK_DEBUG
711 printf ("Builtin fixup table at %x\n", new_addr);
712 #endif
714 bfd_put_32 (output_bfd, (bfd_vma) new_addr, fixup_table);
716 else
717 bfd_put_32 (output_bfd, (bfd_vma) 0, fixup_table);
719 if (bfd_seek (output_bfd, (file_ptr) (os->filepos + s->output_offset),
720 SEEK_SET) != 0)
721 return FALSE;
723 if (bfd_bwrite (s->contents, s->size, output_bfd) != s->size)
724 return FALSE;
726 return TRUE;
729 #define MY_bfd_link_hash_table_create linux_link_hash_table_create
730 #define MY_add_one_symbol linux_add_one_symbol
731 #define MY_finish_dynamic_link linux_finish_dynamic_link
733 #define MY_zmagic_contiguous 1
735 #include "aout-target.h"