This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[PATCH] Use offsets instead of addresses in ELF_SECTION_IN_SEGMENT
- From: Alan Hayward <alan dot hayward at arm dot com>
- To: binutils at sourceware dot org
- Cc: nd at arm dot com, Alan Hayward <alan dot hayward at arm dot com>
- Date: Tue, 22 May 2018 11:52:15 +0100
- Subject: [PATCH] Use offsets instead of addresses in ELF_SECTION_IN_SEGMENT
- Nodisclaimer: True
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
Use sh_offset and p_offset instead of sh_addr and p_vaddr when calculating if a section fits in a segment. Both methods are valid when using the GNU linker.
This change makes ELF_SECTION_IN_SEGMENT consistent with BFD _bfd_elf_make_section_from_shdr(), which has used offsets to calculate LMA since 2008.
bfd/elf.c _bfd_elf_make_section_from_shdr():
if ((flags & SEC_LOAD) == 0)
newsect->lma = (phdr->p_paddr
+ hdr->sh_addr - phdr->p_vaddr);
else
/* We used to use the same adjustment for SEC_LOAD
sections, but that doesn't work if the segment
is packed with code from multiple VMAs.
Instead we calculate the section LMA based on
the segment LMA. It is assumed that the
segment will contain sections with contiguous
LMAs, even if the VMAs are not. */
newsect->lma = (phdr->p_paddr
+ hdr->sh_offset - phdr->p_offset);
For more details on that change see https://sourceware.org/ml/binutils/2008-05/msg00008.html
The upshot of this change is that, along with gdb patch "[PATCH] Use ELF_SECTION_IN_SEGMENT to map segments", it will allow debugging of baremetal Arm binaries produced by the Arm Compiler. Binaries produced by Armlinker in Arm Compiler uses sh_addr, p_vaddr and p_paddr differently to the GNU linker, but both Armlink and the GNU linker are fully compatible with the ELF spec.
This change has been tested using make check x86 on a binutils+gdb build. It has also been manually tested by using gdb to debug a Arm Mbed board.
Alan.
2018-05-22 Alan Hayward <alan.hayward@arm.com>
* include/elf/internal.h (ELF_SECTION_IN_SEGMENT): Use offsets
instead of addresses.
---
include/elf/internal.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/elf/internal.h b/include/elf/internal.h
index 05f9fab89c..5d93e0070e 100644
--- a/include/elf/internal.h
+++ b/include/elf/internal.h
@@ -347,9 +347,9 @@ struct elf_segment_map
|| ((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \
|| ((sec_hdr)->sh_addr >= (segment)->p_vaddr \
&& (!(strict) \
- || ((sec_hdr)->sh_addr - (segment)->p_vaddr \
+ || ((sec_hdr)->sh_offset - (segment)->p_offset \
<= (segment)->p_memsz - 1)) \
- && (((sec_hdr)->sh_addr - (segment)->p_vaddr \
+ && (((sec_hdr)->sh_offset - (segment)->p_offset \
+ ELF_SECTION_SIZE(sec_hdr, segment)) \
<= (segment)->p_memsz))) \
/* No zero size sections at start or end of PT_DYNAMIC. */ \
--
2.15.1 (Apple Git-101)