linux-openrisc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [QUESTION] Does or1k-elf-gcc handle OpenRISC 4B alignment requirement?
@ 2025-07-11  6:20 48494605
  2025-07-12 10:14 ` Stafford Horne
  0 siblings, 1 reply; 3+ messages in thread
From: 48494605 @ 2025-07-11  6:20 UTC (permalink / raw)
  To: linux-openrisc

Hi OpenRISC maintainers,

I encountered an alignment issue with `or1k-elf-gcc` and would like to confirm if it handles OpenRISC's 4B alignment requirement correctly. Here's the problem:

When using a recent version of `or1k-elf-gcc`, the following code triggers an alignment exception:
          Alignment 0x600: Load/store access to unaligned location.

The issue occurs in a `memcpy()` operation within an ELF image loader (code snippet below). Interestingly, the same code works fine with the older `or32-elf-gcc 4.2.2`.

--- Code snippet ---
static unsigned long load_elf_image_phdr(unsigned long addr)
{
      Elf32_Ehdr *ehdr;       /* Elf header structure pointer     */
      Elf32_Phdr *phdr;       /* Program header structure pointer */
      int i;


      ehdr = (Elf32_Ehdr *) addr;
      phdr = (Elf32_Phdr *) (addr + ehdr->e_phoff);
      
      printf("## phdr at 0x%08lx ...\n", phdr);


      /* Load each program header */
      for (i = 0; i < ehdr->e_phnum; ++i) {
            void *dst = (void *)(uintptr_t) phdr->p_paddr;
            void *src = (void *) addr + phdr->p_offset;
            printf("Loading phdr %i to %p (%i bytes)\n",
                  i, dst, phdr->p_filesz);
            if (phdr->p_filesz)
                  memcpy(dst, src, phdr->p_filesz);
            if (phdr->p_filesz != phdr->p_memsz)
                  memset(dst + phdr->p_filesz, 0x00,
                        phdr->p_memsz - phdr->p_filesz);
            //flush_cache((unsigned long)dst, phdr->p_filesz);
            ++phdr;
      }


      return ehdr->e_entry;
}
---
Questions:
1. Should `or1k-elf-gcc` generate code that respects OpenRISC's 4B alignment by default?
2. Is there a compiler flag (e.g., `-mstrict-align`) or workaround to enforce safe unaligned access?
3. Could this be a regression compared to the older `or32-elf-gcc`?

Any insights would be greatly appreciated!

Best regards,
zgliu


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-07-14 14:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-11  6:20 [QUESTION] Does or1k-elf-gcc handle OpenRISC 4B alignment requirement? 48494605
2025-07-12 10:14 ` Stafford Horne
     [not found]   ` <tencent_FEB56C143F39CEE7BB1C5B3A1C7E82940909@qq.com>
2025-07-14 14:59     ` [QUESTION] Does or1k-elf-gcc handle OpenRISC 4B alignmentrequirement? Stafford Horne

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).