135
// Set the target based on fields in the ELF file header.
138
Object::set_target(int machine, int size, bool big_endian, int osabi,
141
Target* target = select_target(machine, size, big_endian, osabi, abiversion);
143
gold_fatal(_("%s: unsupported ELF machine number %d"),
144
this->name().c_str(), machine);
145
this->target_ = target;
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
354
341
template<int size, bool big_endian>
356
Sized_relobj<size, big_endian>::setup(
357
const elfcpp::Ehdr<size, big_endian>& ehdr)
343
Sized_relobj<size, big_endian>::setup(Target *target)
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);
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.
926
911
template<int size, bool big_endian>
930
915
Read_symbols_data* sd)
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()));
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()));
928
bool is_gc_or_icf = (parameters->options().gc_sections()
929
|| parameters->options().icf());
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));
939
936
Symbols_data* gc_sd = NULL;
958
955
const unsigned char* symbol_names_data = NULL;
959
956
section_size_type symbol_names_size;
961
if (parameters->options().gc_sections())
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;
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();
992
990
const char* pnames = reinterpret_cast<const char*>(pnamesu);
994
992
// If any input files have been claimed by plugins, we need to defer
1199
1197
gold_assert(out_section_offsets[i] == invalid_address);
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))
1206
1203
if (parameters->options().print_gc_sections())
1207
1204
gold_info(_("%s: removing unused section from '%s'"
1209
1206
program_name, this->section_name(i).c_str(),
1210
1207
this->name().c_str());
1211
1208
out_sections[i] = NULL;
1214
if (is_gc_pass_two && parameters->options().icf())
1216
if (out_sections[i] == NULL)
1218
gold_assert(out_section_offsets[i] == invalid_address);
1221
if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1222
&& symtab->icf()->is_section_folded(this, i))
1224
if (parameters->options().print_icf_sections())
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());
1237
out_sections[i] = NULL;
1238
out_section_offsets[i] = invalid_address;
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;
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);
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());
1292
gold_assert(!(is_gc_or_icf) || reloc_sections.empty());
1263
1294
for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1264
1295
p != reloc_sections.end();
1554
1586
template<int size, bool big_endian>
1556
1588
Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1590
Symbol_table* symtab)
1559
1592
gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1598
1631
Output_section* os = out_sections[shndx];
1632
Address secoffset = out_offsets[shndx];
1633
if (symtab->is_section_folded(this, shndx))
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,
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);
1600
1648
if (os == NULL)
1696
1744
return this->output_local_dynsym_count_;
1747
// If Symbols_data is not NULL get the section flags from here otherwise
1748
// get it from the file.
1750
template<int size, bool big_endian>
1752
Sized_relobj<size, big_endian>::do_section_flags(unsigned int shndx)
1754
Symbols_data* sd = this->get_symbols_data();
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();
1762
// If sd is NULL, read the section header from the file.
1763
return this->elf_file_.section_flags(shndx);
1766
// Get the section's ent size from Symbols_data. Called by get_section_contents
1769
template<int size, bool big_endian>
1771
Sized_relobj<size, big_endian>::do_section_entsize(unsigned int shndx)
1773
Symbols_data* sd = this->get_symbols_data();
1774
gold_assert (sd != NULL);
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();
1699
1783
// Write out the local symbols.
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)
2140
int et = ehdr.get_e_type();
2141
if (et == elfcpp::ET_REL)
2143
Sized_relobj<size, big_endian>* obj =
2144
new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
2148
else if (et == elfcpp::ET_DYN)
2150
Sized_dynobj<size, big_endian>* obj =
2151
new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2157
gold_error(_("%s: unsupported ELF file type %d"),
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]);
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,
2163
2234
} // End anonymous namespace.