Thu Jun 16 14:25:22 1994 Eric Youngdale (ericy@cais.cais.com)
[external/binutils.git] / bfd / i386linux.c
1 /* BFD back-end for linux flavored i386 a.out binaries.
2    Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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.  */
19
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
25 #define ARCH 32
26 #define BYTES_IN_WORD 4
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "libbfd.h"
31 #include "aout/aout64.h"
32 #include "aout/stab_gnu.h"
33 #include "aout/ar.h"
34 #include "libaout.h"           /* BFD a.out internal data structures */
35
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"
40
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
43    becomes important.  */
44
45 static void MY_final_link_callback
46   PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
47
48 static boolean
49 i386linux_bfd_final_link (abfd, info)
50      bfd *abfd;
51      struct bfd_link_info *info;
52 {
53   obj_aout_subformat (abfd) = q_magic_format;
54   return NAME(aout,final_link) (abfd, info, MY_final_link_callback);
55 }
56
57 #define MY_bfd_final_link i386linux_bfd_final_link
58
59 /* Set the machine type correctly.  */
60
61 static boolean
62 i386linux_write_object_contents (abfd)
63      bfd *abfd;
64 {
65   struct external_exec exec_bytes;
66   struct internal_exec *execp = exec_hdr (abfd);
67
68   N_SET_MACHTYPE (*execp, M_386);
69
70   obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
71
72   WRITE_HEADERS(abfd, execp);
73
74   return true;
75 }
76
77 #define MY_write_object_contents i386linux_write_object_contents
78 \f
79 /* Code to link against Linux a.out shared libraries.  */
80
81 /* See if a symbol name is a reference to the global offset table.  */
82
83 #ifndef GOT_REF_PREFIX
84 #define GOT_REF_PREFIX  "__GOT_"
85 #endif
86
87 #define IS_GOT_SYM(name) \
88   (strncmp (name, GOT_REF_PREFIX, sizeof GOT_REF_PREFIX - 1) == 0)
89
90 /* See if a symbol name is a reference to the procedure linkage table.  */
91
92 #ifndef PLT_REF_PREFIX
93 #define PLT_REF_PREFIX  "__PLT_"
94 #endif
95
96 #define IS_PLT_SYM(name) \
97   (strncmp (name, PLT_REF_PREFIX, sizeof PLT_REF_PREFIX - 1) == 0)
98
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
104    crt0.o).  */
105
106 #ifndef SHARABLE_CONFLICTS
107 #define SHARABLE_CONFLICTS "__SHARABLE_CONFLICTS__"
108 #endif
109
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.  */
119
120 struct fixup
121 {
122   struct fixup *next;
123   struct linux_link_hash_entry *h;
124   bfd_vma value;
125
126   /* Nonzero if this is a jump instruction that needs to be fixed,
127      zero if this is just a pointer */
128   char jump;
129
130   char builtin;
131 };
132
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
135    hash table.  */
136
137 struct linux_link_hash_entry
138 {
139   struct aout_link_hash_entry root;
140 };
141
142 struct linux_link_hash_table
143 {
144   struct aout_link_hash_table root;
145
146   /* First dynamic object found in link.  */
147   bfd *dynobj;
148
149   /* Number of fixups.  */
150   size_t fixup_count;
151
152   /* Number of builtin fixups.  */
153   size_t local_builtins;
154
155   /* List of fixups.  */
156   struct fixup *fixup_list;
157 };
158
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
162   PARAMS ((bfd *));
163 static struct fixup *new_fixup
164   PARAMS ((struct bfd_link_info *, struct linux_link_hash_entry *,
165            bfd_vma, int));
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 *));
176
177 /* Routine to create an entry in an Linux link hash table.  */
178
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;
183      const char *string;
184 {
185   struct linux_link_hash_entry *ret = (struct linux_link_hash_entry *) entry;
186
187   /* Allocate the structure if it has not already been allocated by a
188      subclass.  */
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)));
192   if (ret == NULL)
193     return (struct bfd_hash_entry *) ret;
194
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,
198                                        table, string));
199   if (ret != NULL)
200     {
201       /* Set local fields; there aren't any.  */
202     }
203
204   return (struct bfd_hash_entry *) ret;
205 }
206
207 /* Create a Linux link hash table.  */
208
209 static struct bfd_link_hash_table *
210 linux_link_hash_table_create (abfd)
211      bfd *abfd;
212 {
213   struct linux_link_hash_table *ret;
214
215   ret = ((struct linux_link_hash_table *)
216          malloc (sizeof (struct linux_link_hash_table)));
217   if (ret == (struct linux_link_hash_table *) NULL)
218     {
219       bfd_set_error (bfd_error_no_memory);
220       return (struct bfd_link_hash_table *) NULL;
221     }
222   if (! NAME(aout,link_hash_table_init) (&ret->root, abfd,
223                                          linux_link_hash_newfunc))
224     {
225       free (ret);
226       return (struct bfd_link_hash_table *) NULL;
227     }
228
229   ret->dynobj = NULL;
230   ret->fixup_count = 0;
231   ret->local_builtins = 0;
232   ret->fixup_list = NULL;
233
234   return &ret->root.root;
235 }
236
237 /* Look up an entry in a Linux link hash table.  */
238
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),\
242                           (follow)))
243
244 /* Traverse a Linux link hash table.  */
245
246 #define linux_link_hash_traverse(table, func, info)                     \
247   (aout_link_hash_traverse                                              \
248    (&(table)->root,                                                     \
249     (boolean (*) PARAMS ((struct aout_link_hash_entry *, PTR))) (func), \
250     (info)))
251
252 /* Get the Linux link hash table from the info structure.  This is
253    just a cast.  */
254
255 #define linux_hash_table(p) ((struct linux_link_hash_table *) ((p)->hash))
256
257 /* Store the information for a new fixup.  */
258
259 static struct fixup *
260 new_fixup (info, h, value, builtin)
261      struct bfd_link_info *info;
262      struct linux_link_hash_entry *h;
263      bfd_vma value;
264      int builtin;
265 {
266   struct fixup *f;
267
268   f = (struct fixup *) bfd_hash_allocate (&info->hash->table,
269                                           sizeof (struct fixup));
270   if (f == NULL)
271     return f;
272   f->next = linux_hash_table (info)->fixup_list;
273   linux_hash_table (info)->fixup_list = f;
274   f->h = h;
275   f->value = value;
276   f->builtin = builtin;
277   f->jump = 0;
278   ++linux_hash_table (info)->fixup_count;
279   return f;
280 }
281
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.  */
288
289 static boolean
290 linux_link_create_dynamic_sections (abfd, info)
291      bfd *abfd;
292      struct bfd_link_info *info;
293 {
294   flagword flags;
295   register asection *s;
296
297   /* Note that we set the SEC_IN_MEMORY flag.  */
298   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
299
300   /* We choose to use the name ".dynamic" for the fixup table.  Why
301      not? */
302   s = bfd_make_section (abfd, ".dynamic");
303   if (s == NULL
304       || ! bfd_set_section_flags (abfd, s, flags)
305       || ! bfd_set_section_alignment (abfd, s, 2))
306     return false;
307   s->_raw_size = 0;
308   s->contents = 0;
309
310   return true;
311 }
312
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.  */
316
317 static boolean
318 linux_add_one_symbol (info, abfd, name, flags, section, value, string,
319                       copy, collect, hashp)
320      struct bfd_link_info *info;
321      bfd *abfd;
322      const char *name;
323      flagword flags;
324      asection *section;
325      bfd_vma value;
326      const char *string;
327      boolean copy;
328      boolean collect;
329      struct bfd_link_hash_entry **hashp;
330 {
331   struct linux_link_hash_entry *h;
332   boolean insert;
333
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.
337
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.  */
341
342   insert = false;
343
344   if (! info->relocateable
345       && linux_hash_table (info)->dynobj == NULL
346       && strcmp (name, SHARABLE_CONFLICTS) == 0)
347     {
348       if (! linux_link_create_dynamic_sections (abfd, info))
349         return false;
350       linux_hash_table (info)->dynobj = abfd;
351       insert = true;
352     }
353
354   if (section == &bfd_abs_section
355       && (IS_GOT_SYM (name) || IS_PLT_SYM (name)))
356     {
357       h = linux_link_hash_lookup (linux_hash_table (info), name, false,
358                                   false, false);
359       if (h != NULL
360           && h->root.root.type == bfd_link_hash_defined)
361         {
362           if (new_fixup (info, h, value, ! IS_PLT_SYM(name)) == NULL)
363             return false;
364           return true;
365         }
366       if (hashp != NULL)
367         *hashp = (struct bfd_link_hash_entry *) h;
368     }
369
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,
373                                           hashp))
374     return false;
375
376   /* Insert a pointer to our table in the set vector.  The dynamic
377      linker requires this information */
378   if (insert)
379     {
380       asection *s;
381
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,
385                                    ".dynamic");
386       BFD_ASSERT (s != NULL);
387
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)))
391         return false;
392     }
393
394   return true;
395 }
396
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.
404
405    This function is called via linux_link_hash_traverse.  */
406
407 static boolean
408 linux_tally_symbols (h, data)
409      struct linux_link_hash_entry *h;
410      PTR data;
411 {
412   struct bfd_link_info *info = (struct bfd_link_info *) data;
413   struct fixup *f, *f1;
414   int is_plt;
415   struct linux_link_hash_entry *h1, *h2;
416
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);
419
420   if (is_plt || IS_GOT_SYM (h->root.root.root.string))
421     {
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),
428                                    false, false, true);
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);
434
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 */
441       if (h1 != NULL
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))
445         {
446           f = new_fixup (info, h1, h->root.root.u.def.value, 0);
447           if (f == NULL)
448             {
449               /* FIXME: No way to return error.  */
450               abort ();
451             }
452           f->jump = is_plt;
453
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;
459                f1 != NULL;
460                f1 = f1->next)
461             {
462               if (f1 == f)
463                 continue;
464               if (f1->h != h)
465                 continue;
466               f1->h = h1;
467               f1->jump = is_plt;
468               f1->builtin = 0;
469             }
470         }
471
472       /* Quick and dirty way of stripping these symbols from the
473          symtab. */
474       h->root.written = true;
475     }
476
477   return true;
478 }
479
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
484    required.  */
485
486 boolean
487 bfd_linux_size_dynamic_sections (output_bfd, info)
488      bfd *output_bfd;
489      struct bfd_link_info *info;
490 {
491   struct fixup *f;
492   asection *s;
493
494   /* First find the fixups... */
495   linux_link_hash_traverse (linux_hash_table (info),
496                             linux_tally_symbols,
497                             (PTR) info);
498
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)
503     {
504       if (f->builtin)
505         {
506           ++linux_hash_table (info)->fixup_count;
507           ++linux_hash_table (info)->local_builtins;
508           break;
509         }
510     }
511
512   if (linux_hash_table (info)->dynobj == NULL)
513     {
514       if (linux_hash_table (info)->fixup_count > 0)
515         abort ();
516       return true;
517     }
518
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");
521   if (s != NULL)
522     {
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)
526         {
527           bfd_set_error (bfd_error_no_memory);
528           return false;
529         }
530       memset (s->contents, 0, s->_raw_size);
531     }
532
533   return true;
534 }
535
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.  */
539
540 static boolean
541 linux_finish_dynamic_link (output_bfd, info)
542      bfd *output_bfd;
543      struct bfd_link_info *info;
544 {
545   asection *s, *os, *is;
546   bfd_byte *fixup_table;
547   struct linux_link_hash_entry *h;
548   struct fixup *f;
549   unsigned int new_addr;
550   int section_offset;
551   int fixups_written;
552
553   if (linux_hash_table (info)->dynobj == NULL)
554     return true;
555
556   s = bfd_get_section_by_name (linux_hash_table (info)->dynobj, ".dynamic");
557   BFD_ASSERT (s != NULL);
558   os = s->output_section;
559   fixups_written = 0;
560
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);
565 #endif
566
567   fixup_table = s->contents;
568   bfd_put_32 (output_bfd, linux_hash_table (info)->fixup_count, fixup_table);
569   fixup_table += 4;
570
571   /* Fill in fixup table.  */
572   for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
573     {
574       if (f->builtin)
575         continue;
576
577       if (f->h->root.root.type != bfd_link_hash_defined)
578         {
579           /* FIXME!  */
580           fprintf (stderr,
581                    "Symbol %s not defined for fixups\n",
582                    f->h->root.root.root.string);
583           continue;
584         }
585
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;
589
590 #ifdef LINUX_LINK_DEBUG
591       printf ("Fixup(%d) %s: %x %x\n",f->jump, f->h->root.root.string,
592               new_addr, f->value);
593 #endif
594
595       if (f->jump)
596         {
597           /* Relative address */
598           new_addr = new_addr - (f->value + 5); 
599           bfd_put_32 (output_bfd, new_addr, fixup_table);
600           fixup_table += 4;
601           bfd_put_32 (output_bfd, f->value + 1, fixup_table);
602           fixup_table += 4;
603         }
604       else
605         {
606           bfd_put_32 (output_bfd, new_addr, fixup_table);
607           fixup_table += 4;
608           bfd_put_32 (output_bfd, f->value, fixup_table);
609           fixup_table += 4;
610         }
611       ++fixups_written;
612     }
613
614   if (linux_hash_table (info)->local_builtins != 0)
615     {
616       /* Special marker so we know to switch to the other type of fixup */
617       bfd_put_32 (output_bfd, 0, fixup_table);
618       fixup_table += 4;
619       bfd_put_32 (output_bfd, 0, fixup_table);
620       fixup_table += 4;
621       ++fixups_written;
622       for (f = linux_hash_table (info)->fixup_list; f != NULL; f = f->next)
623         {
624           if (! f->builtin)
625             continue;
626
627           if (f->h->root.root.type != bfd_link_hash_defined)
628             {
629               /* FIXME!  */
630               fprintf (stderr,
631                        "Symbol %s not defined for fixups\n",
632                        f->h->root.root.root.string);
633               continue;
634             }
635
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;
639
640 #ifdef LINUX_LINK_DEBUG
641           printf ("Fixup(B) %s: %x %x\n", f->h->root.root.string,
642                   new_addr, f->value);
643 #endif
644
645           bfd_put_32 (output_bfd, new_addr, fixup_table);
646           fixup_table += 4;
647           bfd_put_32 (output_bfd, f->value, fixup_table);
648           fixup_table += 4;
649           ++fixups_written;
650         }
651   }
652
653   if (linux_hash_table (info)->fixup_count != fixups_written)
654     {
655       /* FIXME!  */
656       fprintf (stderr, "Warning: fixup count mismatch\n");
657       while (linux_hash_table (info)->fixup_count > fixups_written)
658         {
659           bfd_put_32 (output_bfd, 0, fixup_table);
660           fixup_table += 4;
661           bfd_put_32 (output_bfd, 0, fixup_table);
662           fixup_table += 4;
663           ++fixups_written;
664         }
665     }
666
667   h = linux_link_hash_lookup (linux_hash_table (info), 
668                               "__BUILTIN_FIXUPS__",
669                               false, false, false);
670
671   if (h != NULL && h->root.root.type == bfd_link_hash_defined)
672     {
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;
676
677 #ifdef LINUX_LINK_DEBUG
678       printf ("Builtin fixup table at %x\n", new_addr);
679 #endif
680
681       bfd_put_32 (output_bfd, new_addr, fixup_table);
682     }
683   else
684     bfd_put_32 (output_bfd, 0, fixup_table);
685
686   if (bfd_seek (output_bfd, os->filepos + s->output_offset, SEEK_SET) != 0)
687     return false;
688
689   if (bfd_write ((PTR) s->contents, 1, s->_raw_size, output_bfd)
690       != s->_raw_size)
691     return false;
692
693   return true;
694 }
695
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
699
700 #include "aout-target.h"