Fix several mix up between octets and bytes in ELF program headers

Fixes additional locations not handled in the first patch.

When converting between addresses in ELF headers [octets] and bfd
LMA/VMA [bytes], the number of octets per byte needs to be incorporated.

include/
	* bfdlink.h (struct bfd_link_order): Add unit (bytes/octets) to
	offset and size members.
	* elf/internal.h (struct elf_internal_phdr): Likewise for
	p_align member.
	(struct elf_segment_map): Likewise for p_paddr and p_size
	members
bfd/
	* bfd.c (bfd_record_phdr): New local "opb".  Fix assignment of
	"p_paddr" from "at".
	* elfcode.h (bfd_from_remote_memory): Add units to several
	parameters.  New local "opb".  Fix usage of p_align.  Fix
	calculation of "localbase" from "ehdr_vma" and "p_vaddr".  Fix
	call of target_read_memory.
	* elflink.c (elf_fixup_link_order): Fix scope of "s" local.  Fix
	calculation of "offset" and "output_offset".
	(bfd_elf_final_link): New local "opb".  Fix calculation of "size"
	from "offset" and fix calculation of "end" from "vma+size".  Fix
	comparison between "sh_addr" and "vma"/"output_offset".
	(bfd_elf_discard_info): Fix calculation of "eh_alignment".
	* elf-bfd.h (struct elf_link_hash_table): Add unit to tls_size
	member.
	* elf.c (_bfd_elf_map_sections_to_segments): Add unit (bytes/
	octets) to "wrap_to2 and "phdr_size" locals.  Fix calculation of
	"wrap_to" value.  Add unit (bytes) to phdr_lma variable.  Fix
	assignment of p_paddr from phdr_lma.  Fix comparison between
	"lma+size" and "next->lma".
	(elf_sort_segments): Fix assignment from p_paddr to lma.
	(assign_file_positions_for_load_sections): Add unit (bytes) to
	local "align".  Fix calculation of local "off_adjust".  Fix
	calculation of local "filehdr_vaddr".
	(assign_file_positions_for_non_load_sections): New local "opb".
	Fix calculation of "end" from "p_size". Fix comparison between
	"vma+SECTION_SIZE" and "start".  Fix calculation of "p_memsz"
	from "end" and "p_vaddr".
	(rewrite_elf_program_header): Fix comparison between p_vaddr and
	vma.  Fix assignment to p_paddr from lma.  Fix comparison between
	p_paddr and lma.  Fix assignment to p_paddr from lma.
	* merge.c (sec_merge_emit): New local "opb". Convert
	"alignment_power" to octets.
	(_bfd_add_merge_section): New locals "alignment_power" and
	"opb".  Fix comparison between "alignment_power" and
	"sizeof(align)".
	(_bfd_merge_sections): New local "opb".  Divide size by opb
	before checking align mask.
This commit is contained in:
Christian Eggers
2020-03-02 20:17:00 +00:00
committed by Alan Modra
parent 502794d432
commit 666318230c
10 changed files with 133 additions and 69 deletions

View File

@@ -1664,10 +1664,11 @@ elf_debug_file (Elf_Internal_Ehdr *ehdrp)
bfd *
NAME(_bfd_elf,bfd_from_remote_memory)
(bfd *templ,
bfd_vma ehdr_vma,
bfd_size_type size,
bfd_vma *loadbasep,
bfd_vma ehdr_vma /* Bytes. */,
bfd_size_type size /* Octets. */,
bfd_vma *loadbasep /* Bytes. */,
int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
/* (Bytes , , octets ). */
{
Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
@@ -1680,9 +1681,10 @@ NAME(_bfd_elf,bfd_from_remote_memory)
unsigned int i;
bfd_vma high_offset;
bfd_vma shdr_end;
bfd_vma loadbase;
bfd_vma loadbase; /* Bytes. */
char *filename;
size_t amt;
unsigned int opb = bfd_octets_per_byte (templ, NULL);
/* Read in the ELF header in external format. */
err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
@@ -1780,17 +1782,17 @@ NAME(_bfd_elf,bfd_from_remote_memory)
header sits, then we can figure out the loadbase. */
if (first_phdr == NULL)
{
bfd_vma p_offset = i_phdrs[i].p_offset;
bfd_vma p_vaddr = i_phdrs[i].p_vaddr;
bfd_vma p_offset = i_phdrs[i].p_offset; /* Octets. */
bfd_vma p_vaddr = i_phdrs[i].p_vaddr; /* Octets. */
if (i_phdrs[i].p_align > 1)
{
p_offset &= -i_phdrs[i].p_align;
p_vaddr &= -i_phdrs[i].p_align;
p_offset &= -(i_phdrs[i].p_align * opb);
p_vaddr &= -(i_phdrs[i].p_align * opb);
}
if (p_offset == 0)
{
loadbase = ehdr_vma - p_vaddr;
loadbase = ehdr_vma - p_vaddr / opb;
first_phdr = &i_phdrs[i];
}
}
@@ -1846,9 +1848,9 @@ NAME(_bfd_elf,bfd_from_remote_memory)
for (i = 0; i < i_ehdr.e_phnum; ++i)
if (i_phdrs[i].p_type == PT_LOAD)
{
bfd_vma start = i_phdrs[i].p_offset;
bfd_vma end = start + i_phdrs[i].p_filesz;
bfd_vma vaddr = i_phdrs[i].p_vaddr;
bfd_vma start = i_phdrs[i].p_offset; /* Octets. */
bfd_vma end = start + i_phdrs[i].p_filesz; /* Octets. */
bfd_vma vaddr = i_phdrs[i].p_vaddr; /* Octets. */
/* Extend the beginning of the first pt_load to cover file
header and program headers, if we proved earlier that its
@@ -1861,7 +1863,7 @@ NAME(_bfd_elf,bfd_from_remote_memory)
/* Extend the end of the last pt_load to cover section headers. */
if (last_phdr == &i_phdrs[i])
end = high_offset;
err = target_read_memory (loadbase + vaddr,
err = target_read_memory (loadbase + vaddr / opb,
contents + start, end - start);
if (err)
{