All of lore.kernel.org
 help / color / mirror / Atom feed
* Endian safe mkelf32.c
@ 2005-02-23 18:37 Jimi Xenidis
  2005-02-24 12:10 ` Vincent Hanquez
  0 siblings, 1 reply; 4+ messages in thread
From: Jimi Xenidis @ 2005-02-23 18:37 UTC (permalink / raw)
  To: xen-devel

[-- Attachment #1: Type: text/plain, Size: 28 bytes --]

apply in xen/arch/x86/boot


[-- Attachment #2: mkelf32.c.patch --]
[-- Type: application/octet-stream, Size: 5654 bytes --]

--- /tmp/geta23530	2005-02-23 13:02:56.000000000 -0500
+++ mkelf32.c	2005-02-23 13:02:54.000000000 -0500
@@ -107,6 +107,119 @@
     }
 };
 
+static u16 swap16(u16 v)
+{
+    u16 val;
+
+    val = (v & 0xff00) >> 8;
+    val |= (v & 0x00ff) << 8;
+    v = val;
+
+    return v;
+}
+
+static u32 swap32(u32 v)
+{
+    u32 val;
+	
+    val = (v & 0xff000000) >> 24;
+    val |= (v & 0x00ff0000) >> 8;
+    val |= (v & 0x0000ff00) << 8;
+    val |= (v & 0x000000ff) << 24;
+    v = val;
+
+    return v;
+}
+
+static u64 swap64(u64 v)
+{
+    u64 val;
+
+    val = (v & (0xffULL << 56)) >> 56;
+    val |= (v & (0xffULL << 48)) >> 40;
+    val |= (v & (0xffULL << 40)) >> 24;
+    val |= (v & (0xffULL << 32)) >> 8;
+    val |= (v & (0xffULL << 24)) >> 8;
+    val |= (v & (0xffULL << 16)) >> 24;
+    val |= (v & (0xffULL <<  8)) >> 40;
+    val |= (v & (0xffULL <<  0)) >> 56;
+	
+    v = val;
+    return v;
+}
+
+static void swap_ehdr32(Elf32_Ehdr *eh)
+{
+    eh->e_type = swap16(eh->e_type);
+    eh->e_machine = swap16(eh->e_machine);	
+    eh->e_version = swap32(eh->e_version);	
+    eh->e_entry = swap32(eh->e_entry);	
+    eh->e_phoff = swap32(eh->e_phoff);	
+    eh->e_shoff = swap32(eh->e_shoff);	
+    eh->e_flags = swap32(eh->e_flags);	
+    eh->e_ehsize = swap16(eh->e_ehsize);	
+    eh->e_phentsize = swap16(eh->e_phentsize);	
+    eh->e_phnum = swap16(eh->e_phnum);	
+    eh->e_shentsize = swap16(eh->e_shentsize);	
+    eh->e_shnum = swap16(eh->e_shnum);	
+    eh->e_shstrndx = swap16(eh->e_shstrndx);	
+}
+
+static void swap_ehdr64(Elf64_Ehdr *eh)
+{
+    eh->e_type = swap16(eh->e_type);
+    eh->e_machine = swap16(eh->e_machine);	
+    eh->e_version = swap32(eh->e_version);	
+    eh->e_entry = swap64(eh->e_entry);	
+    eh->e_phoff = swap64(eh->e_phoff);	
+    eh->e_shoff = swap64(eh->e_shoff);	
+    eh->e_flags = swap32(eh->e_flags);	
+    eh->e_ehsize = swap16(eh->e_ehsize);	
+    eh->e_phentsize = swap16(eh->e_phentsize);	
+    eh->e_phnum = swap16(eh->e_phnum);	
+    eh->e_shentsize = swap16(eh->e_shentsize);	
+    eh->e_shnum = swap16(eh->e_shnum);	
+    eh->e_shstrndx = swap16(eh->e_shstrndx);	
+}
+
+static void swap_phdr32(Elf32_Phdr *ph)
+{
+    ph->p_type = swap32(ph->p_type);		
+    ph->p_offset = swap32(ph->p_offset);	
+    ph->p_vaddr = swap32(ph->p_vaddr);	
+    ph->p_paddr = swap32(ph->p_paddr);	
+    ph->p_filesz = swap32(ph->p_filesz);	
+    ph->p_memsz = swap32(ph->p_memsz);	
+    ph->p_flags = swap32(ph->p_flags);	
+    ph->p_align = swap32(ph->p_align);       
+}
+
+static void swap_phdr64(Elf64_Phdr *ph)
+{
+    ph->p_type = swap32(ph->p_type);		
+    ph->p_offset = swap32(ph->p_offset);	
+    ph->p_vaddr = swap64(ph->p_vaddr);	
+    ph->p_paddr = swap64(ph->p_paddr);	
+    ph->p_filesz = swap64(ph->p_filesz);	
+    ph->p_memsz = swap64(ph->p_memsz);	
+    ph->p_flags = swap64(ph->p_flags);	
+    ph->p_align = swap64(ph->p_align);       
+}
+
+static void swap_shdr32(Elf32_Shdr *sh)
+{
+    sh->sh_name = swap32(sh->sh_name);	
+    sh->sh_type = swap32(sh->sh_type);	
+    sh->sh_flags = swap32(sh->sh_flags);	
+    sh->sh_addr = swap32(sh->sh_addr);	
+    sh->sh_offset = swap32(sh->sh_offset);	
+    sh->sh_size = swap32(sh->sh_size);	
+    sh->sh_link = swap32(sh->sh_link);	
+    sh->sh_info = swap32(sh->sh_info);	
+    sh->sh_addralign = swap32(sh->sh_addralign);	
+    sh->sh_entsize = swap32(sh->sh_entsize);	
+}
+
 static void do_write(int fd, void *data, int len)
 {
     int   done, left = len;
@@ -156,6 +269,7 @@
     int        infd, outfd;
     char       buffer[1024];
     int        bytes, todo;
+    int	       swap;
 
     Elf32_Ehdr in32_ehdr;
     Elf32_Phdr in32_phdr;
@@ -189,6 +303,24 @@
         return 1;
     }
 
+    if ( (in32_ehdr.e_type >> 8) == ET_EXEC &&
+	 (in32_ehdr.e_type & 0xff) != ET_EXEC )
+    {
+	swap = 1;
+	swap_ehdr32(&in32_ehdr);
+    }
+    else if ( (in32_ehdr.e_type >> 8) != ET_EXEC &&
+	      (in32_ehdr.e_type & 0xff) == ET_EXEC )
+    {
+	swap = 0;
+    }
+    else
+    {
+	fprintf(stderr, "Cannot figure out endianess.\n");
+        return 1;
+    }
+
+
     switch ( in32_ehdr.e_ident[EI_CLASS] )
     {
     case ELFCLASS32:
@@ -208,6 +340,7 @@
 
         (void)lseek(infd, in32_ehdr.e_phoff, SEEK_SET);
         do_read(infd, &in32_phdr, sizeof(in32_phdr));
+	if (swap) swap_phdr32(&in32_phdr);
 
         (void)lseek(infd, in32_phdr.p_offset, SEEK_SET);
         dat_siz = (u32)in32_phdr.p_filesz;
@@ -217,6 +350,7 @@
     case ELFCLASS64:
         (void)lseek(infd, 0, SEEK_SET);
         do_read(infd, &in64_ehdr, sizeof(in64_ehdr));
+	if (swap) swap_ehdr64(&in64_ehdr);
 
         if ( in64_ehdr.e_phentsize != sizeof(in64_phdr) )
         {
@@ -234,6 +368,7 @@
 
         (void)lseek(infd, in64_ehdr.e_phoff, SEEK_SET);
         do_read(infd, &in64_phdr, sizeof(in64_phdr));
+	if (swap) swap_phdr64(&in64_phdr);
 
         (void)lseek(infd, in64_phdr.p_offset, SEEK_SET);
         dat_siz = (u32)in64_phdr.p_filesz;
@@ -271,7 +406,10 @@
         return 1;
     }
 
+    if (swap) swap_ehdr32(&out_ehdr);
     do_write(outfd, &out_ehdr, sizeof(out_ehdr));
+
+    if (swap) swap_phdr32(&out_phdr);
     do_write(outfd, &out_phdr, sizeof(out_phdr));
     
     if ( (bytes = RAW_OFFSET - sizeof(out_ehdr) - sizeof(out_phdr)) < 0 )
@@ -289,7 +427,17 @@
         do_write(outfd, buffer, todo);
     }
 
+    if (swap)
+    {
+	int i;
+
+	for (i = 0; i < sizeof(out_shdr) / sizeof(out_shdr[0]); i++)
+	{
+	    swap_shdr32(&out_shdr[i]);
+	}
+    }
     do_write(outfd, &out_shdr[0], sizeof(out_shdr));
+
     do_write(outfd, out_shstrtab, sizeof(out_shstrtab));
     do_write(outfd, buffer, 4-((sizeof(out_shstrtab)+dat_siz)&3));
 

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

end of thread, other threads:[~2005-02-24 13:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-02-23 18:37 Endian safe mkelf32.c Jimi Xenidis
2005-02-24 12:10 ` Vincent Hanquez
2005-02-24 12:31   ` Jimi Xenidis
2005-02-24 13:52     ` Vincent Hanquez

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.