40 #if defined(ENABLE_DISASSEMBLER)
42 #if !defined(WITH_BINUTILS_DISASSEMBLER)
43 #error ObjectFileWriterPass requires binutils bfd library.
49 #endif // defined(ENABLE_DISASSEMBLER)
51 #define DEBUG_NAME "compiler2/ObjectFileWriter"
53 #if defined(ENABLE_DISASSEMBLER)
54 RT_REGISTER_TIMER(obj_writter_timer,
"objectwriter",
"object writer execution time")
59 #if defined(ENABLE_DISASSEMBLER)
61 inline void check_bfd_error(T* t) {
64 ABORT_MSG(
"ObjectFileWriterPass Error",
"Error: " << bfd_errmsg(bfd_get_error()));
67 inline void check_bfd_error(bfd_boolean b) {
70 ABORT_MSG(
"ObjectFileWriterPass Error",
"Error: " << bfd_errmsg(bfd_get_error()));
73 #endif // defined(ENABLE_DISASSEMBLER)
83 #if defined(ENABLE_DISASSEMBLER)
89 const char* bfd_arch =
"i386:x86-64";
90 const char* bfd_target =
"elf64-x86-64";
93 std::string filename = class_name +
"." + symbol_name +
".o";
96 LOG(
"Object file name: " << filename.c_str() <<
nl);
97 LOG(
".text symbol name: " << symbol_name.c_str() <<
nl);
101 const char ** target_list = bfd_target_list();
102 while (*target_list) {
103 LOG(
"target: " << *target_list <<
nl);
108 const char ** arch_list = bfd_arch_list();
110 LOG(
"arch: " << *arch_list <<
nl);
117 bfd *abfd = bfd_openw(filename.c_str(),bfd_target);
118 check_bfd_error(abfd);
121 LOG2(
"set architecture" <<
nl);
122 const bfd_arch_info_type *arch_info = bfd_scan_arch (bfd_arch);
123 check_bfd_error(arch_info);
125 bfd_set_arch_info(abfd,arch_info);
127 bfd_set_format(abfd, bfd_object);
135 asection *data_section = NULL;
137 LOG2(
"create data_section" <<
nl);
138 data_section = bfd_make_section_anyway_with_flags(abfd,
".rodata",
139 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA );
140 check_bfd_error(data_section);
142 LOG2(
"set data_section size" <<
nl);
143 check_bfd_error(bfd_set_section_size(abfd, data_section, dseglen));
145 data_section->alignment_power = 2;
149 LOG2(
"create code_section" <<
nl);
150 asection *code_section = bfd_make_section_anyway_with_flags(abfd,
".text",
151 SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE );
152 check_bfd_error(code_section);
154 LOG2(
"set code_section size" <<
nl);
157 codelen = 8 + dseglen + codelen;
160 check_bfd_error(bfd_set_section_size(abfd, code_section,codelen));
162 code_section->alignment_power = 2;
166 reloc_howto_type* howto = bfd_reloc_type_lookup(abfd, BFD_RELOC_32_PCREL);
167 arelent reloc_entry = {
174 bfd_install_relocation(
183 bfd_reloc_status_type bfd_install_relocation
185 arelent *reloc_entry,
186 void *data, bfd_vma data_start,
187 asection *input_section,
188 char **error_message);
193 LOG2(
"create symbol" <<
nl);
194 asymbol *new_smbl = bfd_make_empty_symbol (abfd);
195 new_smbl->name = symbol_name.c_str();
196 new_smbl->section = code_section;
197 new_smbl->flags = BSF_GLOBAL;
205 LOG2(
"insert symbol list" <<
nl);
206 check_bfd_error(bfd_set_symtab(abfd, ptrs, 1));
211 LOG2(
"set data_section contents" <<
nl);
212 check_bfd_error(bfd_set_section_contents(abfd,
213 data_section, code->
mcode,0,dseglen));
215 LOG2(
"set code_section contents" <<
nl);
220 buffer[1] = ((dseglen + 3) >> 0) & 0xff;
221 buffer[2] = ((dseglen + 3) >> 8) & 0xff;
222 buffer[3] = ((dseglen + 3) >>16) & 0xff;
223 buffer[4] = ((dseglen + 3) >>32) & 0xff;
225 memcpy(&buffer[8],code->
mcode,dseglen);
227 memcpy(&buffer[8+dseglen],code->
entrypoint,codelen - (dseglen + 8));
228 check_bfd_error(bfd_set_section_contents(abfd,
229 code_section, &buffer.front(),0,codelen));
232 check_bfd_error(bfd_set_section_contents(abfd,
239 #endif // defined(ENABLE_DISASSEMBLER)
const Utf8String & get_name_utf8() const
#define RT_TIMER_STOP(var)
Stop the timer var.
CodeGenPass TODO: more info.
#define RT_TIMER_START(var)
Start the timer var.
#define DEBUG_COND_N(VERBOSE)
virtual PassUsage & get_PassUsage(PassUsage &PA) const
Set the requirements for the pass.
jitdata * get_jitdata() const
#define RT_REGISTER_TIMER(var, name, description)
Register a new (toplevel) timer.
static Option< bool > enabled
std::vector< T, Allocator< T > > type
Stores the interdependencies of a pass.
This file contains the real-time timing utilities.
#define LOG(STMT)
Analogous to DEBUG.
virtual bool run(JITData &JD)
Run the Pass.
const Utf8String & get_class_name_utf8() const
byte_iterator begin() const
#define ABORT_MSG(EXPR_SHORT, EXPR_LONG)
static PassRegistry< BasicBlockSchedulingPass > X("BasicBlockSchedulingPass")
void add_requires()
PassName is required.