~ubuntu-branches/debian/squeeze/binutils/squeeze

« back to all changes in this revision

Viewing changes to gold/object.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-09-10 17:05:30 UTC
  • mfrom: (1.4.5 upstream) (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090910170530-wa6gpju9pq5c56on
Tags: 2.19.91.20090910-1
* Snapshot, taken from the 2.20 release branch 20090910, corresponding
  to the 2.19.90 upstream snapshot.
* Fix Thumb-2 shared libraries (Daniel Jacobowitz), patch taken
  from the trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
132
132
 
133
133
// Class Object.
134
134
 
135
 
// Set the target based on fields in the ELF file header.
136
 
 
137
 
void
138
 
Object::set_target(int machine, int size, bool big_endian, int osabi,
139
 
                   int abiversion)
140
 
{
141
 
  Target* target = select_target(machine, size, big_endian, osabi, abiversion);
142
 
  if (target == NULL)
143
 
    gold_fatal(_("%s: unsupported ELF machine number %d"),
144
 
               this->name().c_str(), machine);
145
 
  this->target_ = target;
146
 
}
147
 
 
148
135
// Report an error for this object file.  This is used by the
149
136
// elfcpp::Elf_file interface, and also called by the Object code
150
137
// itself.
353
340
 
354
341
template<int size, bool big_endian>
355
342
void
356
 
Sized_relobj<size, big_endian>::setup(
357
 
    const elfcpp::Ehdr<size, big_endian>& ehdr)
 
343
Sized_relobj<size, big_endian>::setup(Target *target)
358
344
{
359
 
  this->set_target(ehdr.get_e_machine(), size, big_endian,
360
 
                   ehdr.get_e_ident()[elfcpp::EI_OSABI],
361
 
                   ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
 
345
  this->set_target(target);
362
346
 
363
347
  const unsigned int shnum = this->elf_file_.shnum();
364
348
  this->set_shnum(shnum);
914
898
// whether they should be included in the link.  If they should, we
915
899
// pass them to the Layout object, which will return an output section
916
900
// and an offset.  
917
 
// During garbage collection (gc-sections), this function is called
918
 
// twice.  When it is called the first time, it is for setting up some
919
 
// sections as roots to a work-list and to do comdat processing.  Actual
920
 
// layout happens the second time around after all the relevant sections
921
 
// have been determined.  The first time, is_worklist_ready is false.  
922
 
// It is then set to true after the worklist is processed and the relevant 
923
 
// sections are determined.  Then, this function is called again to 
924
 
// layout the sections.
 
901
// During garbage collection (--gc-sections) and identical code folding 
 
902
// (--icf), this function is called twice.  When it is called the first 
 
903
// time, it is for setting up some sections as roots to a work-list for
 
904
// --gc-sections and to do comdat processing.  Actual layout happens the 
 
905
// second time around after all the relevant sections have been determined.  
 
906
// The first time, is_worklist_ready or is_icf_ready is false. It is then 
 
907
// set to true after the garbage collection worklist or identical code 
 
908
// folding is processed and the relevant sections to be kept are 
 
909
// determined.  Then, this function is called again to layout the sections.
925
910
 
926
911
template<int size, bool big_endian>
927
912
void
930
915
                                          Read_symbols_data* sd)
931
916
{
932
917
  const unsigned int shnum = this->shnum();
933
 
  bool is_gc_pass_one = (parameters->options().gc_sections() 
934
 
                         && !symtab->gc()->is_worklist_ready());
935
 
  bool is_gc_pass_two = (parameters->options().gc_sections() 
936
 
                         && symtab->gc()->is_worklist_ready());
 
918
  bool is_gc_pass_one = ((parameters->options().gc_sections() 
 
919
                          && !symtab->gc()->is_worklist_ready())
 
920
                         || (parameters->options().icf()
 
921
                             && !symtab->icf()->is_icf_ready()));
 
922
 
 
923
  bool is_gc_pass_two = ((parameters->options().gc_sections() 
 
924
                          && symtab->gc()->is_worklist_ready())
 
925
                         || (parameters->options().icf()
 
926
                             && symtab->icf()->is_icf_ready()));
 
927
 
 
928
  bool is_gc_or_icf = (parameters->options().gc_sections()
 
929
                       || parameters->options().icf()); 
 
930
 
 
931
  // Both is_gc_pass_one and is_gc_pass_two should not be true.
 
932
  gold_assert(!(is_gc_pass_one  && is_gc_pass_two));
 
933
 
937
934
  if (shnum == 0)
938
935
    return;
939
936
  Symbols_data* gc_sd = NULL;
958
955
  const unsigned char* symbol_names_data = NULL;
959
956
  section_size_type symbol_names_size;
960
957
 
961
 
  if (parameters->options().gc_sections())
 
958
  if (is_gc_or_icf)
962
959
    {
963
960
      section_headers_data = gc_sd->section_headers_data;
964
961
      section_names_size = gc_sd->section_names_size;
986
983
  const unsigned char* pshdrs;
987
984
 
988
985
  // Get the section names.
989
 
  const unsigned char* pnamesu = parameters->options().gc_sections() ?
990
 
                                 gc_sd->section_names_data :
991
 
                                 sd->section_names->data();
 
986
  const unsigned char* pnamesu = (is_gc_or_icf) 
 
987
                                 ? gc_sd->section_names_data
 
988
                                 : sd->section_names->data();
 
989
 
992
990
  const char* pnames = reinterpret_cast<const char*>(pnamesu);
993
991
 
994
992
  // If any input files have been claimed by plugins, we need to defer
1141
1139
            }
1142
1140
        }
1143
1141
 
1144
 
      if (is_gc_pass_one)
 
1142
      if (is_gc_pass_one && parameters->options().gc_sections())
1145
1143
        {
1146
1144
          if (is_section_name_included(name)
1147
1145
              || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY 
1188
1186
          continue;
1189
1187
        }
1190
1188
 
1191
 
      if (is_gc_pass_two)
 
1189
      if (is_gc_pass_two && parameters->options().gc_sections())
1192
1190
        {
1193
1191
          // This is executed during the second pass of garbage 
1194
1192
          // collection. do_layout has been called before and some 
1199
1197
              gold_assert(out_section_offsets[i] == invalid_address);
1200
1198
              continue; 
1201
1199
            }
1202
 
          if ((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1203
 
            if (symtab->gc()->referenced_list().find(Section_id(this,i)) 
1204
 
                == symtab->gc()->referenced_list().end())
 
1200
          if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
 
1201
              && symtab->gc()->is_section_garbage(this, i))
1205
1202
              {
1206
1203
                if (parameters->options().print_gc_sections())
1207
1204
                  gold_info(_("%s: removing unused section from '%s'" 
1208
 
                              " in file '%s"),
 
1205
                              " in file '%s'"),
1209
1206
                            program_name, this->section_name(i).c_str(), 
1210
1207
                            this->name().c_str());
1211
1208
                out_sections[i] = NULL;
1213
1210
                continue;
1214
1211
              }
1215
1212
        }
 
1213
 
 
1214
      if (is_gc_pass_two && parameters->options().icf())
 
1215
        {
 
1216
          if (out_sections[i] == NULL)
 
1217
            {
 
1218
              gold_assert(out_section_offsets[i] == invalid_address);
 
1219
              continue;
 
1220
            }
 
1221
          if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
 
1222
              && symtab->icf()->is_section_folded(this, i))
 
1223
              {
 
1224
                if (parameters->options().print_icf_sections())
 
1225
                  {
 
1226
                    Section_id folded =
 
1227
                                symtab->icf()->get_folded_section(this, i);
 
1228
                    Relobj* folded_obj =
 
1229
                                reinterpret_cast<Relobj*>(folded.first);
 
1230
                    gold_info(_("%s: ICF folding section '%s' in file '%s'"
 
1231
                                "into '%s' in file '%s'"),
 
1232
                              program_name, this->section_name(i).c_str(),
 
1233
                              this->name().c_str(),
 
1234
                              folded_obj->section_name(folded.second).c_str(),
 
1235
                              folded_obj->name().c_str());
 
1236
                  }
 
1237
                out_sections[i] = NULL;
 
1238
                out_section_offsets[i] = invalid_address;
 
1239
                continue;
 
1240
              }
 
1241
        }
 
1242
 
1216
1243
      // Defer layout here if input files are claimed by plugins.  When gc
1217
1244
      // is turned on this function is called twice.  For the second call
1218
1245
      // should_defer_layout should be false.
1228
1255
          out_sections[i] = reinterpret_cast<Output_section*>(2);
1229
1256
          out_section_offsets[i] = invalid_address;
1230
1257
          continue;
1231
 
        }
 
1258
        }
 
1259
 
1232
1260
      // During gc_pass_two if a section that was previously deferred is
1233
1261
      // found, do not layout the section as layout_deferred_sections will
1234
1262
      // do it later from gold.cc.
1256
1284
    layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
1257
1285
 
1258
1286
  // When doing a relocatable link handle the reloc sections at the
1259
 
  // end.  Garbage collection is not turned on for relocatable code. 
 
1287
  // end.  Garbage collection  and Identical Code Folding is not 
 
1288
  // turned on for relocatable code. 
1260
1289
  if (emit_relocs)
1261
1290
    this->size_relocatable_relocs();
1262
 
  gold_assert(!parameters->options().gc_sections() || reloc_sections.empty());
 
1291
 
 
1292
  gold_assert(!(is_gc_or_icf) || reloc_sections.empty());
 
1293
 
1263
1294
  for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1264
1295
       p != reloc_sections.end();
1265
1296
       ++p)
1342
1373
      delete[] gc_sd->section_names_data;
1343
1374
      delete[] gc_sd->symbols_data;
1344
1375
      delete[] gc_sd->symbol_names_data;
 
1376
      this->set_symbols_data(NULL);
1345
1377
    }
1346
1378
  else
1347
1379
    {
1554
1586
template<int size, bool big_endian>
1555
1587
unsigned int
1556
1588
Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1557
 
                                                          off_t off)
 
1589
                                                          off_t off,
 
1590
                                                          Symbol_table* symtab)
1558
1591
{
1559
1592
  gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1560
1593
 
1596
1629
            }
1597
1630
 
1598
1631
          Output_section* os = out_sections[shndx];
 
1632
          Address secoffset = out_offsets[shndx];
 
1633
          if (symtab->is_section_folded(this, shndx))
 
1634
            {
 
1635
              gold_assert (os == NULL && secoffset == invalid_address);
 
1636
              // Get the os of the section it is folded onto.
 
1637
              Section_id folded = symtab->icf()->get_folded_section(this,
 
1638
                                                                    shndx);
 
1639
              gold_assert(folded.first != NULL);
 
1640
              Sized_relobj<size, big_endian>* folded_obj = reinterpret_cast
 
1641
                <Sized_relobj<size, big_endian>*>(folded.first);
 
1642
              os = folded_obj->output_section(folded.second);
 
1643
              gold_assert(os != NULL);
 
1644
              secoffset = folded_obj->get_output_section_offset(folded.second);
 
1645
              gold_assert(secoffset != invalid_address);
 
1646
            }
1599
1647
 
1600
1648
          if (os == NULL)
1601
1649
            {
1605
1653
              // so we leave the input value unchanged here.
1606
1654
              continue;
1607
1655
            }
1608
 
          else if (out_offsets[shndx] == invalid_address)
 
1656
          else if (secoffset == invalid_address)
1609
1657
            {
1610
1658
              uint64_t start;
1611
1659
 
1647
1695
            }
1648
1696
          else if (lv.is_tls_symbol())
1649
1697
            lv.set_output_value(os->tls_offset()
1650
 
                                + out_offsets[shndx]
 
1698
                                + secoffset
1651
1699
                                + lv.input_value());
1652
1700
          else
1653
1701
            lv.set_output_value((relocatable ? 0 : os->address())
1654
 
                                + out_offsets[shndx]
 
1702
                                + secoffset
1655
1703
                                + lv.input_value());
1656
1704
        }
1657
1705
 
1696
1744
  return this->output_local_dynsym_count_;
1697
1745
}
1698
1746
 
 
1747
// If Symbols_data is not NULL get the section flags from here otherwise
 
1748
// get it from the file.
 
1749
 
 
1750
template<int size, bool big_endian>
 
1751
uint64_t
 
1752
Sized_relobj<size, big_endian>::do_section_flags(unsigned int shndx)
 
1753
{
 
1754
  Symbols_data* sd = this->get_symbols_data();
 
1755
  if (sd != NULL)
 
1756
    {
 
1757
      const unsigned char* pshdrs = sd->section_headers_data
 
1758
                                    + This::shdr_size * shndx;
 
1759
      typename This::Shdr shdr(pshdrs);
 
1760
      return shdr.get_sh_flags(); 
 
1761
    }
 
1762
  // If sd is NULL, read the section header from the file.
 
1763
  return this->elf_file_.section_flags(shndx); 
 
1764
}
 
1765
 
 
1766
// Get the section's ent size from Symbols_data.  Called by get_section_contents
 
1767
// in icf.cc
 
1768
 
 
1769
template<int size, bool big_endian>
 
1770
uint64_t
 
1771
Sized_relobj<size, big_endian>::do_section_entsize(unsigned int shndx)
 
1772
{
 
1773
  Symbols_data* sd = this->get_symbols_data();
 
1774
  gold_assert (sd != NULL);
 
1775
 
 
1776
  const unsigned char* pshdrs = sd->section_headers_data
 
1777
                                + This::shdr_size * shndx;
 
1778
  typename This::Shdr shdr(pshdrs);
 
1779
  return shdr.get_sh_entsize(); 
 
1780
}
 
1781
 
 
1782
 
1699
1783
// Write out the local symbols.
1700
1784
 
1701
1785
template<int size, bool big_endian>
2137
2221
make_elf_sized_object(const std::string& name, Input_file* input_file,
2138
2222
                      off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2139
2223
{
2140
 
  int et = ehdr.get_e_type();
2141
 
  if (et == elfcpp::ET_REL)
2142
 
    {
2143
 
      Sized_relobj<size, big_endian>* obj =
2144
 
        new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
2145
 
      obj->setup(ehdr);
2146
 
      return obj;
2147
 
    }
2148
 
  else if (et == elfcpp::ET_DYN)
2149
 
    {
2150
 
      Sized_dynobj<size, big_endian>* obj =
2151
 
        new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2152
 
      obj->setup(ehdr);
2153
 
      return obj;
2154
 
    }
2155
 
  else
2156
 
    {
2157
 
      gold_error(_("%s: unsupported ELF file type %d"),
2158
 
                 name.c_str(), et);
2159
 
      return NULL;
2160
 
    }
 
2224
  Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
 
2225
                                 ehdr.get_e_ident()[elfcpp::EI_OSABI],
 
2226
                                 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
 
2227
  if (target == NULL)
 
2228
    gold_fatal(_("%s: unsupported ELF machine number %d"),
 
2229
               name.c_str(), ehdr.get_e_machine());
 
2230
  return target->make_elf_object<size, big_endian>(name, input_file, offset,
 
2231
                                                   ehdr);
2161
2232
}
2162
2233
 
2163
2234
} // End anonymous namespace.