Skip to content

Commit

Permalink
kexec/elf: assign one to align if sh_addralign equals zero
Browse files Browse the repository at this point in the history
According to ELF specification, if sh_addralign equals zero or one, then
the section has no alignment requirement on the start address. (I.e. it
can be aligned on 1 byte)

Since modern cpu asks the .text, .data, .bss to be aligned
on the machine word boundary at least, so in elf_rel_load(),
sh_addralign can not be zero, and
  align = shdr->sh_addralign;
  ...
  bufsz = _ALIGN(bufsz, align);

will not render a result of 'bufsz = 0'.

But it had better have a check on the case of 'sh_addralign == 0'
regardless of the assumption of machine word alignment.

This patch has no functional change.

Signed-off-by: Pingfan Liu <[email protected]>
Signed-off-by: Simon Horman <[email protected]>
  • Loading branch information
Pingfan Liu authored and horms committed Apr 1, 2022
1 parent 1d8273d commit 2b78a27
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions kexec/kexec-elf-rel.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ int build_elf_rel_info(const char *buf, off_t len, struct mem_ehdr *ehdr,
return 0;
}

static unsigned long get_section_addralign(struct mem_shdr *shdr)
{
return (shdr->sh_addralign == 0) ? 1 : shdr->sh_addralign;
}

int elf_rel_load(struct mem_ehdr *ehdr, struct kexec_info *info,
unsigned long min, unsigned long max, int end)
Expand Down Expand Up @@ -219,7 +223,7 @@ int elf_rel_load(struct mem_ehdr *ehdr, struct kexec_info *info,
}
if (shdr->sh_type != SHT_NOBITS) {
unsigned long align;
align = shdr->sh_addralign;
align = get_section_addralign(shdr);
/* See if I need more alignment */
if (buf_align < align) {
buf_align = align;
Expand All @@ -231,7 +235,7 @@ int elf_rel_load(struct mem_ehdr *ehdr, struct kexec_info *info,
}
else {
unsigned long align;
align = shdr->sh_addralign;
align = get_section_addralign(shdr);
/* See if I need more alignment */
if (bss_align < align) {
bss_align = align;
Expand Down Expand Up @@ -265,7 +269,7 @@ int elf_rel_load(struct mem_ehdr *ehdr, struct kexec_info *info,
if (!(shdr->sh_flags & SHF_ALLOC)) {
continue;
}
align = shdr->sh_addralign;
align = get_section_addralign(shdr);
if (shdr->sh_type != SHT_NOBITS) {
unsigned long off;
/* Adjust the address */
Expand Down

0 comments on commit 2b78a27

Please sign in to comment.