Subversion Repositories HelenOS

Rev

Rev 1966 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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