~ubuntu-branches/ubuntu/precise/ghc/precise

« back to all changes in this revision

Viewing changes to .pc/kfreebsd-_gnu_source-565818/rts/Linker.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2011-10-31 13:31:12 UTC
  • mfrom: (8.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20111031133112-qimkbmi8v4ajx5bc
Tags: 7.0.4-8ubuntu1
* Merge with Debian, remaining changes:
  + Pass --hash-style=both --no-copy-dt-needed-entries --as-needed to the
  linker.
* Use dh-autoreconf

Show diffs side-by-side

added added

removed removed

Lines of Context:
2207
2207
            //  stgFree(oc->image);
2208
2208
            // #endif
2209
2209
            stgFree(oc->fileName);
 
2210
            stgFree(oc->archiveMemberName);
2210
2211
            stgFree(oc->symbols);
2211
2212
            stgFree(oc->sections);
2212
2213
            stgFree(oc);
3539
3540
 * Generic ELF functions
3540
3541
 */
3541
3542
 
3542
 
static char *
3543
 
findElfSection ( void* objImage, Elf_Word sh_type )
3544
 
{
3545
 
   char* ehdrC = (char*)objImage;
3546
 
   Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3547
 
   Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3548
 
   char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3549
 
   char* ptr = NULL;
3550
 
   int i;
3551
 
 
3552
 
   for (i = 0; i < ehdr->e_shnum; i++) {
3553
 
      if (shdr[i].sh_type == sh_type
3554
 
          /* Ignore the section header's string table. */
3555
 
          && i != ehdr->e_shstrndx
3556
 
          /* Ignore string tables named .stabstr, as they contain
3557
 
             debugging info. */
3558
 
          && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3559
 
         ) {
3560
 
         ptr = ehdrC + shdr[i].sh_offset;
3561
 
         break;
3562
 
      }
3563
 
   }
3564
 
   return ptr;
3565
 
}
3566
 
 
3567
3543
static int
3568
3544
ocVerifyImage_ELF ( ObjectCode* oc )
3569
3545
{
3571
3547
   Elf_Sym*  stab;
3572
3548
   int i, j, nent, nstrtab, nsymtabs;
3573
3549
   char* sh_strtab;
3574
 
   char* strtab;
3575
3550
 
3576
3551
   char*     ehdrC = (char*)(oc->image);
3577
3552
   Elf_Ehdr* ehdr  = (Elf_Ehdr*)ehdrC;
3653
3628
               ehdrC + shdr[i].sh_offset,
3654
3629
                      ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3655
3630
 
3656
 
      if (shdr[i].sh_type == SHT_REL) {
3657
 
          IF_DEBUG(linker,debugBelch("Rel  " ));
3658
 
      } else if (shdr[i].sh_type == SHT_RELA) {
3659
 
          IF_DEBUG(linker,debugBelch("RelA " ));
3660
 
      } else {
3661
 
          IF_DEBUG(linker,debugBelch("     "));
 
3631
#define SECTION_INDEX_VALID(ndx) (ndx > SHN_UNDEF && ndx < ehdr->e_shnum)
 
3632
 
 
3633
      switch (shdr[i].sh_type) {
 
3634
 
 
3635
        case SHT_REL:
 
3636
        case SHT_RELA:
 
3637
          IF_DEBUG(linker,debugBelch( shdr[i].sh_type == SHT_REL ? "Rel  " : "RelA "));
 
3638
 
 
3639
          if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
 
3640
            if (shdr[i].sh_link == SHN_UNDEF)
 
3641
              errorBelch("\n%s: relocation section #%d has no symbol table\n"
 
3642
                         "This object file has probably been fully striped. "
 
3643
                         "Such files cannot be linked.\n",
 
3644
                         oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
 
3645
            else
 
3646
              errorBelch("\n%s: relocation section #%d has an invalid link field (%d)\n",
 
3647
                         oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
 
3648
                         i, shdr[i].sh_link);
 
3649
            return 0;
 
3650
          }
 
3651
          if (shdr[shdr[i].sh_link].sh_type != SHT_SYMTAB) {
 
3652
            errorBelch("\n%s: relocation section #%d does not link to a symbol table\n",
 
3653
                       oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
 
3654
            return 0;
 
3655
          }
 
3656
          if (!SECTION_INDEX_VALID(shdr[i].sh_info)) {
 
3657
            errorBelch("\n%s: relocation section #%d has an invalid info field (%d)\n",
 
3658
                       oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
 
3659
                       i, shdr[i].sh_info);
 
3660
            return 0;
 
3661
          }
 
3662
 
 
3663
          break;
 
3664
        case SHT_SYMTAB:
 
3665
          IF_DEBUG(linker,debugBelch("Sym  "));
 
3666
 
 
3667
          if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
 
3668
            errorBelch("\n%s: symbol table section #%d has an invalid link field (%d)\n",
 
3669
                       oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
 
3670
                       i, shdr[i].sh_link);
 
3671
            return 0;
 
3672
          }
 
3673
          if (shdr[shdr[i].sh_link].sh_type != SHT_STRTAB) {
 
3674
            errorBelch("\n%s: symbol table section #%d does not link to a string table\n",
 
3675
                       oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
 
3676
 
 
3677
            return 0;
 
3678
          }
 
3679
          break;
 
3680
        case SHT_STRTAB: IF_DEBUG(linker,debugBelch("Str  ")); break;
 
3681
        default:         IF_DEBUG(linker,debugBelch("     ")); break;
3662
3682
      }
3663
3683
      if (sh_strtab) {
3664
3684
          IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3665
3685
      }
3666
3686
   }
3667
3687
 
3668
 
   IF_DEBUG(linker,debugBelch( "\nString tables" ));
3669
 
   strtab = NULL;
 
3688
   IF_DEBUG(linker,debugBelch( "\nString tables\n" ));
3670
3689
   nstrtab = 0;
3671
3690
   for (i = 0; i < ehdr->e_shnum; i++) {
3672
3691
      if (shdr[i].sh_type == SHT_STRTAB
3676
3695
             debugging info. */
3677
3696
          && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3678
3697
         ) {
3679
 
         IF_DEBUG(linker,debugBelch("   section %d is a normal string table", i ));
3680
 
         strtab = ehdrC + shdr[i].sh_offset;
 
3698
         IF_DEBUG(linker,debugBelch("   section %d is a normal string table\n", i ));
3681
3699
         nstrtab++;
3682
3700
      }
3683
3701
   }
3684
 
   if (nstrtab != 1) {
3685
 
      errorBelch("%s: no string tables, or too many", oc->fileName);
3686
 
      return 0;
 
3702
   if (nstrtab == 0) {
 
3703
      IF_DEBUG(linker,debugBelch("   no normal string tables (potentially, but not necessarily a problem)\n"));
3687
3704
   }
3688
3705
 
3689
3706
   nsymtabs = 0;
3690
 
   IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
 
3707
   IF_DEBUG(linker,debugBelch( "Symbol tables\n" ));
3691
3708
   for (i = 0; i < ehdr->e_shnum; i++) {
3692
3709
      if (shdr[i].sh_type != SHT_SYMTAB) continue;
3693
3710
      IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3729
3746
         }
3730
3747
         IF_DEBUG(linker,debugBelch("  " ));
3731
3748
 
3732
 
         IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
 
3749
         IF_DEBUG(linker,debugBelch("name=%s\n",
 
3750
                        ehdrC + shdr[shdr[i].sh_link].sh_offset
 
3751
                              + stab[j].st_name ));
3733
3752
      }
3734
3753
   }
3735
3754
 
3736
3755
   if (nsymtabs == 0) {
3737
 
      errorBelch("%s: didn't find any symbol tables", oc->fileName);
3738
 
      return 0;
 
3756
     // Not having a symbol table is not in principle a problem.
 
3757
     // When an object file has no symbols then the 'strip' program
 
3758
     // typically will remove the symbol table entirely.
 
3759
     IF_DEBUG(linker,debugBelch("   no symbol tables (potentially, but not necessarily a problem)\n"));
3739
3760
   }
3740
3761
 
3741
3762
   return 1;
3782
3803
 
3783
3804
   char*     ehdrC    = (char*)(oc->image);
3784
3805
   Elf_Ehdr* ehdr     = (Elf_Ehdr*)ehdrC;
3785
 
   char*     strtab   = findElfSection ( ehdrC, SHT_STRTAB );
 
3806
   char*     strtab;
3786
3807
   Elf_Shdr* shdr     = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3787
3808
 
3788
3809
   ASSERT(symhash != NULL);
3789
3810
 
3790
 
   if (!strtab) {
3791
 
      errorBelch("%s: no strtab", oc->fileName);
3792
 
      return 0;
3793
 
   }
3794
 
 
3795
3811
   k = 0;
3796
3812
   for (i = 0; i < ehdr->e_shnum; i++) {
3797
3813
      /* Figure out what kind of section it is.  Logic derived from
3824
3840
 
3825
3841
      /* copy stuff into this module's object symbol table */
3826
3842
      stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
 
3843
      strtab = ehdrC + shdr[shdr[i].sh_link].sh_offset;
3827
3844
      nent = shdr[i].sh_size / sizeof(Elf_Sym);
3828
3845
 
3829
3846
      oc->n_symbols = nent;
3927
3944
   relocations appear to be of this form. */
3928
3945
static int
3929
3946
do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3930
 
                         Elf_Shdr* shdr, int shnum,
3931
 
                         Elf_Sym*  stab, char* strtab )
 
3947
                         Elf_Shdr* shdr, int shnum )
3932
3948
{
3933
3949
   int j;
3934
3950
   char *symbol;
3935
3951
   Elf_Word* targ;
3936
3952
   Elf_Rel*  rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
 
3953
   Elf_Sym*  stab;
 
3954
   char*     strtab;
3937
3955
   int         nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3938
3956
   int target_shndx = shdr[shnum].sh_info;
3939
3957
   int symtab_shndx = shdr[shnum].sh_link;
 
3958
   int strtab_shndx = shdr[symtab_shndx].sh_link;
3940
3959
 
3941
3960
   stab  = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
 
3961
   strtab= (char*)    (ehdrC + shdr[ strtab_shndx ].sh_offset);
3942
3962
   targ  = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3943
 
   IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3944
 
                          target_shndx, symtab_shndx ));
 
3963
   IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d and strtab %d\n",
 
3964
                          target_shndx, symtab_shndx, strtab_shndx ));
3945
3965
 
3946
3966
   /* Skip sections that we're not interested in. */
3947
3967
   {
4027
4047
   sparc-solaris relocations appear to be of this form. */
4028
4048
static int
4029
4049
do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
4030
 
                          Elf_Shdr* shdr, int shnum,
4031
 
                          Elf_Sym*  stab, char* strtab )
 
4050
                          Elf_Shdr* shdr, int shnum )
4032
4051
{
4033
4052
   int j;
4034
4053
   char *symbol = NULL;
4035
4054
   Elf_Addr targ;
4036
4055
   Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
 
4056
   Elf_Sym*  stab;
 
4057
   char*     strtab;
4037
4058
   int         nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
4038
4059
   int target_shndx = shdr[shnum].sh_info;
4039
4060
   int symtab_shndx = shdr[shnum].sh_link;
 
4061
   int strtab_shndx = shdr[symtab_shndx].sh_link;
4040
4062
 
4041
4063
   stab  = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
 
4064
   strtab= (char*)    (ehdrC + shdr[ strtab_shndx ].sh_offset);
4042
4065
   targ  = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
4043
4066
   IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
4044
4067
                          target_shndx, symtab_shndx ));
4307
4330
static int
4308
4331
ocResolve_ELF ( ObjectCode* oc )
4309
4332
{
4310
 
   char *strtab;
4311
4333
   int   shnum, ok;
4312
 
   Elf_Sym*  stab  = NULL;
4313
4334
   char*     ehdrC = (char*)(oc->image);
4314
4335
   Elf_Ehdr* ehdr  = (Elf_Ehdr*) ehdrC;
4315
4336
   Elf_Shdr* shdr  = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
4316
4337
 
4317
 
   /* first find "the" symbol table */
4318
 
   stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
4319
 
 
4320
 
   /* also go find the string table */
4321
 
   strtab = findElfSection ( ehdrC, SHT_STRTAB );
4322
 
 
4323
 
   if (stab == NULL || strtab == NULL) {
4324
 
      errorBelch("%s: can't find string or symbol table", oc->fileName);
4325
 
      return 0;
4326
 
   }
4327
 
 
4328
4338
   /* Process the relocation sections. */
4329
4339
   for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
4330
4340
      if (shdr[shnum].sh_type == SHT_REL) {
4331
 
         ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
4332
 
                                       shnum, stab, strtab );
 
4341
         ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr, shnum );
4333
4342
         if (!ok) return ok;
4334
4343
      }
4335
4344
      else
4336
4345
      if (shdr[shnum].sh_type == SHT_RELA) {
4337
 
         ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
4338
 
                                        shnum, stab, strtab );
 
4346
         ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr, shnum );
4339
4347
         if (!ok) return ok;
4340
4348
      }
4341
4349
   }
4368
4376
 
4369
4377
  if( i == ehdr->e_shnum )
4370
4378
  {
4371
 
    errorBelch( "This ELF file contains no symtab" );
4372
 
    return 0;
 
4379
    // Not having a symbol table is not in principle a problem.
 
4380
    // When an object file has no symbols then the 'strip' program
 
4381
    // typically will remove the symbol table entirely.
 
4382
    IF_DEBUG(linker, debugBelch( "The ELF file %s contains no symtab\n",
 
4383
             oc->archiveMemberName ? oc->archiveMemberName : oc->fileName ));
 
4384
    return 1;
4373
4385
  }
4374
4386
 
4375
4387
  if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )