Subversion Repositories HelenOS-historic

Rev

Rev 869 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Swap VMA and LMA in ELF header.
  3.  *
  4.  *  by Jakub Jermar <jermar@itbs.cz>
  5.  *
  6.  *  GPL'ed, copyleft
  7.  */
  8.  
  9. /*
  10.  * HP's IA-64 simulator Ski seems to confuse VMA and LMA in the ELF header.
  11.  * Instead of using LMA, Ski loads sections at their VMA addresses.
  12.  * This short program provides a workaround for this bug by simply
  13.  * swapping VMA and LMA in the ELF header of the executable.
  14.  *
  15.  * Note that after applying this workaround, you will be able to load
  16.  * ELF objects with different VMA and LMA in Ski, but the executable
  17.  * will become wronged for other potential uses.
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/mman.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26.  
  27. void syntax(char *prg)
  28. {
  29.     printf("%s ELF-file\n", prg);
  30.     exit(1);
  31. }
  32.  
  33. void error(char *msg)
  34. {
  35.     printf("Error: %s\n", msg);
  36.     exit(2);
  37. }
  38.  
  39. #define ELF_VMA (0x88/sizeof(unsigned long long))
  40. #define ELF_LMA (0x90/sizeof(unsigned long long))
  41.  
  42. #define LENGTH  0x98
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46.     int fd;
  47.     unsigned long long vma, lma;
  48.     unsigned long long *elf;
  49.  
  50.     if (argc != 2)
  51.         syntax(argv[0]);
  52.    
  53.     fd = open(argv[1], O_RDWR);
  54.     if (fd == -1)
  55.         error("open failed");
  56.  
  57.     elf = mmap(NULL, LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  58.     if ((void *) elf  == (void *) -1)
  59.         error("map failed");
  60.        
  61.     vma = elf[ELF_VMA];
  62.     lma = elf[ELF_LMA];
  63.     elf[ELF_VMA] = lma;
  64.     elf[ELF_LMA] = vma;
  65.    
  66.     if (munmap(elf, LENGTH) == -1)
  67.         error("munmap failed");
  68.    
  69.     if (close(fd) == -1)
  70.         error("close failed");
  71.        
  72.     return 0;
  73. }
  74.