#ifndef GOLD_LAYOUT_H
#define GOLD_LAYOUT_H
#include <cstring>
#include <list>
#include <string>
#include <utility>
#include <vector>
#include "script.h"
#include "workqueue.h"
#include "object.h"
#include "dynobj.h"
#include "stringpool.h"
namespace gold
{
class General_options;
class Input_objects;
class Mapfile;
class Symbol_table;
class Output_section_data;
class Output_section;
class Output_section_headers;
class Output_segment;
class Output_data;
class Output_data_dynamic;
class Output_symtab_xindex;
class Output_reduced_debug_abbrev_section;
class Output_reduced_debug_info_section;
class Eh_frame;
class Target;
class Layout_task_runner : public Task_function_runner
{
public:
Layout_task_runner(const General_options& options,
const Input_objects* input_objects,
Symbol_table* symtab,
Target* target,
Layout* layout,
Mapfile* mapfile)
: options_(options), input_objects_(input_objects), symtab_(symtab),
target_(target), layout_(layout), mapfile_(mapfile)
{ }
void
run(Workqueue*, const Task*);
private:
Layout_task_runner(const Layout_task_runner&);
Layout_task_runner& operator=(const Layout_task_runner&);
const General_options& options_;
const Input_objects* input_objects_;
Symbol_table* symtab_;
Target* target_;
Layout* layout_;
Mapfile* mapfile_;
};
class Layout
{
public:
Layout(const General_options& options, Script_options*);
template<int size, bool big_endian>
Output_section*
layout(Sized_relobj<size, big_endian> *object, unsigned int shndx,
const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
template<int size, bool big_endian>
Output_section*
layout_reloc(Sized_relobj<size, big_endian>* object,
unsigned int reloc_shndx,
const elfcpp::Shdr<size, big_endian>& shdr,
Output_section* data_section,
Relocatable_relocs* rr);
template<int size, bool big_endian>
void
layout_group(Symbol_table* symtab,
Sized_relobj<size, big_endian>* object,
unsigned int group_shndx,
const char* group_section_name,
const char* signature,
const elfcpp::Shdr<size, big_endian>& shdr,
elfcpp::Elf_Word flags,
std::vector<unsigned int>* shndxes);
template<int size, bool big_endian>
Output_section*
layout_eh_frame(Sized_relobj<size, big_endian>* object,
const unsigned char* symbols,
off_t symbols_size,
const unsigned char* symbol_names,
off_t symbol_names_size,
unsigned int shndx,
const elfcpp::Shdr<size, big_endian>& shdr,
unsigned int reloc_shndx, unsigned int reloc_type,
off_t* offset);
void
layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
Output_section*
add_output_section_data(const char* name, elfcpp::Elf_Word type,
elfcpp::Elf_Xword flags,
Output_section_data*);
void
create_initial_dynamic_sections(Symbol_table*);
void
define_section_symbols(Symbol_table*);
void
create_script_sections()
{ this->script_options_->create_script_sections(this); }
void
define_script_symbols(Symbol_table* symtab)
{ this->script_options_->add_symbols_to_table(symtab); }
void
define_group_signatures(Symbol_table*);
const Stringpool*
sympool() const
{ return &this->sympool_; }
const Stringpool*
dynpool() const
{ return &this->dynpool_; }
Output_symtab_xindex*
symtab_xindex() const
{ return this->symtab_xindex_; }
Output_symtab_xindex*
dynsym_xindex() const
{ return this->dynsym_xindex_; }
static inline bool
is_linkonce(const char* name)
{ return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
static inline bool
is_debug_info_section(const char* name)
{
return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
|| strncmp(name, ".gnu.linkonce.wi.",
sizeof(".gnu.linkonce.wi.") - 1) == 0
|| strncmp(name, ".line", sizeof(".line") - 1) == 0
|| strncmp(name, ".stab", sizeof(".stab") - 1) == 0);
}
bool
add_comdat(Relobj*, unsigned int, const std::string&, bool group);
Relobj*
find_kept_object(const std::string&, unsigned int*) const;
off_t
finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
bool
any_postprocessing_sections() const
{ return this->any_postprocessing_sections_; }
off_t
output_file_size() const
{ return this->output_file_size_; }
Output_segment*
tls_segment() const
{ return this->tls_segment_; }
Output_section*
symtab_section() const
{
gold_assert(this->symtab_section_ != NULL);
return this->symtab_section_;
}
Output_section*
dynsym_section() const
{
gold_assert(this->dynsym_section_ != NULL);
return this->dynsym_section_;
}
Output_data_dynamic*
dynamic_data() const
{ return this->dynamic_data_; }
void
write_output_sections(Output_file* of) const;
void
write_data(const Symbol_table*, Output_file*) const;
void
write_sections_after_input_sections(Output_file* of);
Output_section*
find_output_section(const char* name) const;
Output_segment*
find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
elfcpp::Elf_Word clear) const;
size_t
expected_segment_count() const;
void
set_has_static_tls()
{ this->has_static_tls_ = true; }
bool
has_static_tls() const
{ return this->has_static_tls_; }
Script_options*
script_options()
{ return this->script_options_; }
const Script_options*
script_options() const
{ return this->script_options_; }
void
write_build_id(Output_file*) const;
void
write_binary(Output_file* in) const;
void
print_to_mapfile(Mapfile*) const;
void
print_stats() const;
typedef std::vector<Output_segment*> Segment_list;
typedef std::vector<Output_section*> Section_list;
typedef std::vector<Output_data*> Data_list;
void
get_allocated_sections(Section_list*) const;
Output_section*
make_output_section_for_script(const char* name);
Output_segment*
make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
size_t
segment_count() const
{ return this->segment_list_.size(); }
static elfcpp::Elf_Word
section_flags_to_segment(elfcpp::Elf_Xword flags);
void
attach_sections_to_segments();
private:
Layout(const Layout&);
Layout& operator=(const Layout&);
struct Linkonce_mapping
{
const char* from;
int fromlen;
const char* to;
int tolen;
};
static const Linkonce_mapping linkonce_mapping[];
static const int linkonce_mapping_count;
struct Group_signature
{
Output_section* section;
const char* signature;
Group_signature()
: section(NULL), signature(NULL)
{ }
Group_signature(Output_section* sectiona, const char* signaturea)
: section(sectiona), signature(signaturea)
{ }
};
typedef std::vector<Group_signature> Group_signatures;
Output_section*
create_note(const char* name, int note_type, size_t descsz,
bool allocate, size_t* trailing_padding);
void
create_gold_note();
void
create_executable_stack_info(const Target*);
void
create_build_id();
Output_segment*
find_first_load_seg();
void
count_local_symbols(const Task*, const Input_objects*);
void
create_symtab_sections(const Input_objects*, Symbol_table*,
unsigned int, off_t*);
Output_section*
create_shstrtab();
void
create_shdrs(const Output_section* shstrtab_section, off_t*);
void
create_dynamic_symtab(const Input_objects*, Symbol_table*,
Output_section** pdynstr,
unsigned int* plocal_dynamic_count,
std::vector<Symbol*>* pdynamic_symbols,
Versions* versions);
void
assign_local_dynsym_offsets(const Input_objects*);
void
finish_dynamic_section(const Input_objects*, const Symbol_table*);
void
create_interp(const Target* target);
void
create_version_sections(const Versions*,
const Symbol_table*,
unsigned int local_symcount,
const std::vector<Symbol*>& dynamic_symbols,
const Output_section* dynstr);
template<int size, bool big_endian>
void
sized_create_version_sections(const Versions* versions,
const Symbol_table*,
unsigned int local_symcount,
const std::vector<Symbol*>& dynamic_symbols,
const Output_section* dynstr);
template<int size, bool big_endian>
bool
include_section(Sized_relobj<size, big_endian>* object, const char* name,
const elfcpp::Shdr<size, big_endian>&);
static const char*
output_section_name(const char* name, size_t* plen);
static const char*
linkonce_output_name(const char* name, size_t* plen);
size_t
allocated_output_section_count() const;
Output_section*
get_output_section(const char* name, Stringpool::Key name_key,
elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
Output_section*
choose_output_section(const Relobj* relobj, const char* name,
elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
bool is_input_section);
Output_section*
make_output_section(const char* name, elfcpp::Elf_Word type,
elfcpp::Elf_Xword flags);
void
attach_section_to_segment(Output_section*);
void
attach_allocated_section_to_segment(Output_section*);
off_t
set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
off_t
set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
enum Section_offset_pass
{
BEFORE_INPUT_SECTIONS_PASS,
POSTPROCESSING_SECTIONS_PASS,
STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
};
off_t
set_section_offsets(off_t, Section_offset_pass pass);
unsigned int
set_section_indexes(unsigned int pshndx);
Output_segment*
set_section_addresses_from_script(Symbol_table*);
static bool
segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
struct Kept_section
{
Kept_section()
: object_(NULL), shndx_(0), group_(false)
{ }
Kept_section(Relobj* object, unsigned int shndx, bool group)
: object_(object), shndx_(shndx), group_(group)
{ }
Relobj* object_;
unsigned int shndx_;
bool group_;
};
typedef Unordered_map<std::string, Kept_section> Signatures;
typedef std::pair<Stringpool::Key,
std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
struct Hash_key
{
size_t
operator()(const Key& k) const;
};
typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
struct Compare_segments
{
bool
operator()(const Output_segment* seg1, const Output_segment* seg2)
{ return Layout::segment_precedes(seg1, seg2); }
};
const General_options& options_;
Script_options* script_options_;
Stringpool namepool_;
Stringpool sympool_;
Stringpool dynpool_;
Signatures signatures_;
Section_name_map section_name_map_;
Segment_list segment_list_;
Section_list section_list_;
Section_list unattached_section_list_;
bool sections_are_attached_;
Data_list special_output_list_;
Output_section_headers* section_headers_;
Output_segment* tls_segment_;
Output_segment* relro_segment_;
Output_section* symtab_section_;
Output_symtab_xindex* symtab_xindex_;
Output_section* dynsym_section_;
Output_symtab_xindex* dynsym_xindex_;
Output_section* dynamic_section_;
Output_data_dynamic* dynamic_data_;
Output_section* eh_frame_section_;
Eh_frame* eh_frame_data_;
bool added_eh_frame_data_;
Output_section* eh_frame_hdr_section_;
Output_section_data* build_id_note_;
Output_reduced_debug_abbrev_section* debug_abbrev_;
Output_reduced_debug_info_section* debug_info_;
Group_signatures group_signatures_;
off_t output_file_size_;
bool input_requires_executable_stack_;
bool input_with_gnu_stack_note_;
bool input_without_gnu_stack_note_;
bool has_static_tls_;
bool any_postprocessing_sections_;
};
class Write_sections_task : public Task
{
public:
Write_sections_task(const Layout* layout, Output_file* of,
Task_token* output_sections_blocker,
Task_token* final_blocker)
: layout_(layout), of_(of),
output_sections_blocker_(output_sections_blocker),
final_blocker_(final_blocker)
{ }
Task_token*
is_runnable();
void
locks(Task_locker*);
void
run(Workqueue*);
std::string
get_name() const
{ return "Write_sections_task"; }
private:
class Write_sections_locker;
const Layout* layout_;
Output_file* of_;
Task_token* output_sections_blocker_;
Task_token* final_blocker_;
};
class Write_data_task : public Task
{
public:
Write_data_task(const Layout* layout, const Symbol_table* symtab,
Output_file* of, Task_token* final_blocker)
: layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
{ }
Task_token*
is_runnable();
void
locks(Task_locker*);
void
run(Workqueue*);
std::string
get_name() const
{ return "Write_data_task"; }
private:
const Layout* layout_;
const Symbol_table* symtab_;
Output_file* of_;
Task_token* final_blocker_;
};
class Write_symbols_task : public Task
{
public:
Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
const Input_objects* input_objects,
const Stringpool* sympool, const Stringpool* dynpool,
Output_file* of, Task_token* final_blocker)
: layout_(layout), symtab_(symtab), input_objects_(input_objects),
sympool_(sympool), dynpool_(dynpool), of_(of),
final_blocker_(final_blocker)
{ }
Task_token*
is_runnable();
void
locks(Task_locker*);
void
run(Workqueue*);
std::string
get_name() const
{ return "Write_symbols_task"; }
private:
const Layout* layout_;
const Symbol_table* symtab_;
const Input_objects* input_objects_;
const Stringpool* sympool_;
const Stringpool* dynpool_;
Output_file* of_;
Task_token* final_blocker_;
};
class Write_after_input_sections_task : public Task
{
public:
Write_after_input_sections_task(Layout* layout, Output_file* of,
Task_token* input_sections_blocker,
Task_token* final_blocker)
: layout_(layout), of_(of),
input_sections_blocker_(input_sections_blocker),
final_blocker_(final_blocker)
{ }
Task_token*
is_runnable();
void
locks(Task_locker*);
void
run(Workqueue*);
std::string
get_name() const
{ return "Write_after_input_sections_task"; }
private:
Layout* layout_;
Output_file* of_;
Task_token* input_sections_blocker_;
Task_token* final_blocker_;
};
class Close_task_runner : public Task_function_runner
{
public:
Close_task_runner(const General_options* options, const Layout* layout,
Output_file* of)
: options_(options), layout_(layout), of_(of)
{ }
void
run(Workqueue*, const Task*);
private:
const General_options* options_;
const Layout* layout_;
Output_file* of_;
};
inline uint64_t
align_address(uint64_t address, uint64_t addralign)
{
if (addralign != 0)
address = (address + addralign - 1) &~ (addralign - 1);
return address;
}
}
#endif