bfd/ 2011-05-05 H.J. Lu PR ld/12730 * elflink.c (elf_link_input_bfd): Reverse copy .ctors/.dtors sections if needed. include/ 2011-05-05 H.J. Lu PR ld/12730 * bfdlink.h (bfd_link_info): Add reverse_copy_ctors_dtors. ld/ 2011-05-05 H.J. Lu PR ld/12730 * emultempl/elf32.em (gld${EMULATION_NAME}_after_parse): New. (ld_${EMULATION_NAME}_emulation): Replace after_parse_default with gld${EMULATION_NAME}_after_parse. ld/testsuite/ 2011-05-05 H.J. Lu PR ld/12730 * ld-elf/elf.exp (array_tests): Add pr12730". (array_tests_static): Add "static pr12730". * ld-elf/init-mixed.c (ctor65535): Renamed to ... (ctor65535a): This. (ctor65535b): New. (ctors65535): Remove ctor65535. Add ctor65535b and ctor65535a. (dtor65535): Renamed to ... (dtor65535a): This. (dtor65535b): New. (dtors65535): Remove dtor65535. Add dtor65535b and dtor65535a. * ld-elf/pr12730.cc: New. * ld-elf/pr12730.out: Likewise. diff --git a/bfd/elflink.c b/bfd/elflink.c index 082355d..c7027a2 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -9876,12 +9876,50 @@ elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) default: { /* FIXME: octets_per_byte. */ - if (! (o->flags & SEC_EXCLUDE) - && ! bfd_set_section_contents (output_bfd, o->output_section, - contents, - (file_ptr) o->output_offset, - o->size)) - return FALSE; + if (! (o->flags & SEC_EXCLUDE)) + { + bfd_size_type address_size = bed->s->arch_size / 8; + file_ptr offset = (file_ptr) o->output_offset; + bfd_size_type todo = o->size; + /* Check if we need to reverse-copy .ctors/.dtors + sections. */ + if (finfo->info->reverse_copy_ctors_dtors + && todo > address_size + && o->name[0] == '.' + && (o->name[1] == 'c' || o->name[1] == 'd') + && strcmp (o->name + 2, "tors") == 0) + { + if ((o->size % address_size) != 0) + { + (*_bfd_error_handler) + (_("error: %B: size of section %A is not " + "multiple of address size"), + input_bfd, o); + bfd_set_error (bfd_error_on_input); + return FALSE; + } + + do + { + todo -= address_size; + if (! bfd_set_section_contents (output_bfd, + o->output_section, + contents + todo, + offset, + address_size)) + return FALSE; + if (todo == 0) + break; + offset += address_size; + } + while (1); + } + else if (! bfd_set_section_contents (output_bfd, + o->output_section, + contents, + offset, todo)) + return FALSE; + } } break; } diff --git a/include/bfdlink.h b/include/bfdlink.h index 50a1423..3f230ad 100644 --- a/include/bfdlink.h +++ b/include/bfdlink.h @@ -354,6 +354,9 @@ struct bfd_link_info --dynamic-list command line options. */ unsigned int dynamic: 1; + /* TRUE if BFD should reverse-copy .ctors/.dtors sections. */ + unsigned int reverse_copy_ctors_dtors: 1; + /* Non-NULL if .note.gnu.build-id section should be created. */ char *emit_note_gnu_build_id; diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em index 17fb8bf..ce8c21e 100644 --- a/ld/emultempl/elf32.em +++ b/ld/emultempl/elf32.em @@ -63,6 +63,7 @@ fragment < + +class Hello +{ +public: + Hello () + {} + + ~Hello () + {} + + void act () + { std::cout << "Hello, world!" << std::endl; } +}; + + +template +struct Foo +{ + T* _M_allocate_single_object () + { + return new T; + } +}; + +static void __attribute__ (( constructor )) PWLIB_StaticLoader() { + Foo allocator; + Hello* salut = allocator._M_allocate_single_object (); + salut->act (); +} + + +int +main (int /*argc*/, + char* /*argv*/[]) +{ + return 0; +} diff --git a/ld/testsuite/ld-elf/pr12730.out b/ld/testsuite/ld-elf/pr12730.out new file mode 100644 index 0000000..af5626b --- /dev/null +++ b/ld/testsuite/ld-elf/pr12730.out @@ -0,0 +1 @@ +Hello, world!