diff options
author | H.J. Lu <hjl.tools@gmail.com> | 2018-07-20 09:18:47 -0700 |
---|---|---|
committer | H.J. Lu <hjl.tools@gmail.com> | 2018-07-20 09:19:00 -0700 |
commit | 241e64e3b42cd9eba514b8e0ad2ef39a337f10a5 (patch) | |
tree | d62e37adef725407d44dd34d778ddda4c20a60cb /bfd/elfxx-x86.c | |
parent | cbb09508e4b515501273288aaa11a8ef5e15e0ff (diff) | |
download | gdb-241e64e3b42cd9eba514b8e0ad2ef39a337f10a5.zip gdb-241e64e3b42cd9eba514b8e0ad2ef39a337f10a5.tar.gz gdb-241e64e3b42cd9eba514b8e0ad2ef39a337f10a5.tar.bz2 |
x86: Add a GNU_PROPERTY_X86_ISA_1_USED note if needed
When -z separate-code, which is enabled by default for Linux/x86, is
used to create executable, ld won't place any data in the code-only
PT_LOAD segment. If there are no data sections placed before the
code-only PT_LOAD segment, the program headers won't be mapped into
any PT_LOAD segment. When the executable tries to access it (based
on the program header address passed in AT_PHDR), it will lead to
segfault. This patch inserts a GNU_PROPERTY_X86_ISA_1_USED note if
there may be no data sections before the text section so that the
first PT_LOAD segment won't be code-only and will contain the program
header.
Testcases are adjusted to either pass "-z noseparate-code" to ld or
discard the .note.gnu.property section. A Linux/x86 run-time test is
added.
bfd/
PR ld/23428
* elfxx-x86.c (_bfd_x86_elf_link_setup_gnu_properties): If the
separate code program header is needed, make sure that the first
read-only PT_LOAD segment has no code by adding a
GNU_PROPERTY_X86_ISA_1_USED note.
ld/
PR ld/23428
* testsuite/ld-elf/linux-x86.S: New file.
* testsuite/ld-elf/linux-x86.exp: Likewise.
* testsuite/ld-elf/pr23428.c: Likewise.
* testsuite/ld-elf/sec64k.exp: Pass "-z noseparate-code" to ld
for Linux/x86 targets.
* testsuite/ld-i386/abs-iamcu.d: Likewise.
* testsuite/ld-i386/abs.d: Likewise.
* testsuite/ld-i386/pr12718.d: Likewise.
* testsuite/ld-i386/pr12921.d: Likewise.
* testsuite/ld-x86-64/abs-k1om.d: Likewise.
* testsuite/ld-x86-64/abs-l1om.d: Likewise.
* testsuite/ld-x86-64/abs.d: Likewise.
* testsuite/ld-x86-64/pr12718.d: Likewise.
* testsuite/ld-x86-64/pr12921.d: Likewise.
* testsuite/ld-linkonce/zeroeh.ld: Discard .note.gnu.property
section.
* testsuite/ld-scripts/print-memory-usage.t: Likewise.
* testsuite/ld-scripts/size-2.t: Likewise.
* testsuite/lib/ld-lib.exp (run_ld_link_exec_tests): Use ld
to create executable if language is "asm".
Diffstat (limited to 'bfd/elfxx-x86.c')
-rw-r--r-- | bfd/elfxx-x86.c | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c index a2497aa..2e4ff88 100644 --- a/bfd/elfxx-x86.c +++ b/bfd/elfxx-x86.c @@ -2524,6 +2524,7 @@ _bfd_x86_elf_link_setup_gnu_properties const struct elf_backend_data *bed; unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2; unsigned int got_align; + bfd_boolean has_text = FALSE; features = 0; if (info->ibt) @@ -2538,24 +2539,59 @@ _bfd_x86_elf_link_setup_gnu_properties if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour && bfd_count_sections (pbfd) != 0) { + if (!has_text) + { + /* Check if there is no non-empty text section. */ + sec = bfd_get_section_by_name (pbfd, ".text"); + if (sec != NULL && sec->size != 0) + has_text = TRUE; + } + ebfd = pbfd; if (elf_properties (pbfd) != NULL) break; } - if (ebfd != NULL && features) + bed = get_elf_backend_data (info->output_bfd); + + htab = elf_x86_hash_table (info, bed->target_id); + if (htab == NULL) + return pbfd; + + if (ebfd != NULL) { - /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and - GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ - prop = _bfd_elf_get_property (ebfd, - GNU_PROPERTY_X86_FEATURE_1_AND, - 4); - prop->u.number |= features; - prop->pr_kind = property_number; + prop = NULL; + if (features) + { + /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and + GNU_PROPERTY_X86_FEATURE_1_SHSTK. */ + prop = _bfd_elf_get_property (ebfd, + GNU_PROPERTY_X86_FEATURE_1_AND, + 4); + prop->u.number |= features; + prop->pr_kind = property_number; + } + else if (has_text + && elf_properties (ebfd) == NULL + && elf_tdata (info->output_bfd)->o->build_id.sec == NULL + && !htab->elf.dynamic_sections_created + && !info->traditional_format + && (info->output_bfd->flags & D_PAGED) != 0 + && info->separate_code) + { + /* If the separate code program header is needed, make sure + that the first read-only PT_LOAD segment has no code by + adding a GNU_PROPERTY_X86_ISA_1_USED note. */ + prop = _bfd_elf_get_property (ebfd, + GNU_PROPERTY_X86_ISA_1_USED, + 4); + prop->u.number = GNU_PROPERTY_X86_ISA_1_486; + prop->pr_kind = property_number; + } /* Create the GNU property note section if needed. */ - if (pbfd == NULL) + if (prop != NULL && pbfd == NULL) { sec = bfd_make_section_with_flags (ebfd, NOTE_GNU_PROPERTY_SECTION_NAME, @@ -2581,12 +2617,6 @@ error_alignment: pbfd = _bfd_elf_link_setup_gnu_properties (info); - bed = get_elf_backend_data (info->output_bfd); - - htab = elf_x86_hash_table (info, bed->target_id); - if (htab == NULL) - return pbfd; - htab->r_info = init_table->r_info; htab->r_sym = init_table->r_sym; |